1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
4 #include <linux/acpi.h>
5 #include <linux/delay.h>
7 #include <linux/module.h>
8 #include <linux/pm_runtime.h>
9 #include <linux/regulator/consumer.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-device.h>
13 #define AK7375_MAX_FOCUS_POS 4095
15 * This sets the minimum granularity for the focus positions.
16 * A value of 1 gives maximum accuracy for a desired focus position
18 #define AK7375_FOCUS_STEPS 1
20 * This acts as the minimum granularity of lens movement.
21 * Keep this value power of 2, so the control steps can be
22 * uniformly adjusted for gradual lens movement, with desired
23 * number of control steps.
25 #define AK7375_CTRL_STEPS 64
26 #define AK7375_CTRL_DELAY_US 1000
28 * The vcm may take up 10 ms (tDELAY) to power on and start taking
29 * I2C messages. Based on AK7371 datasheet.
31 #define AK7375_POWER_DELAY_US 10000
33 #define AK7375_REG_POSITION 0x0
34 #define AK7375_REG_CONT 0x2
35 #define AK7375_MODE_ACTIVE 0x0
36 #define AK7375_MODE_STANDBY 0x40
38 static const char * const ak7375_supply_names[] = {
43 /* ak7375 device structure */
44 struct ak7375_device {
45 struct v4l2_ctrl_handler ctrls_vcm;
46 struct v4l2_subdev sd;
47 struct v4l2_ctrl *focus;
48 struct regulator_bulk_data supplies[ARRAY_SIZE(ak7375_supply_names)];
50 /* active or standby mode */
54 static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl)
56 return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm);
59 static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev)
61 return container_of(subdev, struct ak7375_device, sd);
64 static int ak7375_i2c_write(struct ak7375_device *ak7375,
65 u8 addr, u16 data, u8 size)
67 struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd);
71 if (size != 1 && size != 2)
74 buf[size] = data & 0xff;
76 buf[1] = (data >> 8) & 0xff;
77 ret = i2c_master_send(client, (const char *)buf, size + 1);
86 static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl)
88 struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl);
90 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
91 return ak7375_i2c_write(dev_vcm, AK7375_REG_POSITION,
97 static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = {
98 .s_ctrl = ak7375_set_ctrl,
101 static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
103 return pm_runtime_resume_and_get(sd->dev);
106 static int ak7375_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
108 pm_runtime_put(sd->dev);
113 static const struct v4l2_subdev_internal_ops ak7375_int_ops = {
115 .close = ak7375_close,
118 static const struct v4l2_subdev_ops ak7375_ops = { };
120 static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev)
122 v4l2_async_unregister_subdev(&ak7375_dev->sd);
123 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
124 media_entity_cleanup(&ak7375_dev->sd.entity);
127 static int ak7375_init_controls(struct ak7375_device *dev_vcm)
129 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
130 const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops;
132 v4l2_ctrl_handler_init(hdl, 1);
134 dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
135 0, AK7375_MAX_FOCUS_POS, AK7375_FOCUS_STEPS, 0);
138 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
139 __func__, hdl->error);
140 dev_vcm->sd.ctrl_handler = hdl;
145 static int ak7375_probe(struct i2c_client *client)
147 struct ak7375_device *ak7375_dev;
151 ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
156 for (i = 0; i < ARRAY_SIZE(ak7375_supply_names); i++)
157 ak7375_dev->supplies[i].supply = ak7375_supply_names[i];
159 ret = devm_regulator_bulk_get(&client->dev,
160 ARRAY_SIZE(ak7375_supply_names),
161 ak7375_dev->supplies);
163 dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
167 v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops);
168 ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
169 ak7375_dev->sd.internal_ops = &ak7375_int_ops;
170 ak7375_dev->sd.entity.function = MEDIA_ENT_F_LENS;
172 ret = ak7375_init_controls(ak7375_dev);
176 ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL);
180 ret = v4l2_async_register_subdev(&ak7375_dev->sd);
184 pm_runtime_set_active(&client->dev);
185 pm_runtime_enable(&client->dev);
186 pm_runtime_idle(&client->dev);
191 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
192 media_entity_cleanup(&ak7375_dev->sd.entity);
197 static void ak7375_remove(struct i2c_client *client)
199 struct v4l2_subdev *sd = i2c_get_clientdata(client);
200 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
202 ak7375_subdev_cleanup(ak7375_dev);
203 pm_runtime_disable(&client->dev);
204 pm_runtime_set_suspended(&client->dev);
208 * This function sets the vcm position, so it consumes least current
209 * The lens position is gradually moved in units of AK7375_CTRL_STEPS,
210 * to make the movements smoothly.
212 static int __maybe_unused ak7375_vcm_suspend(struct device *dev)
214 struct v4l2_subdev *sd = dev_get_drvdata(dev);
215 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
218 if (!ak7375_dev->active)
221 for (val = ak7375_dev->focus->val & ~(AK7375_CTRL_STEPS - 1);
222 val >= 0; val -= AK7375_CTRL_STEPS) {
223 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
226 dev_err_once(dev, "%s I2C failure: %d\n",
228 usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
231 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
232 AK7375_MODE_STANDBY, 1);
234 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
236 ret = regulator_bulk_disable(ARRAY_SIZE(ak7375_supply_names),
237 ak7375_dev->supplies);
241 ak7375_dev->active = false;
247 * This function sets the vcm position to the value set by the user
248 * through v4l2_ctrl_ops s_ctrl handler
249 * The lens position is gradually moved in units of AK7375_CTRL_STEPS,
250 * to make the movements smoothly.
252 static int __maybe_unused ak7375_vcm_resume(struct device *dev)
254 struct v4l2_subdev *sd = dev_get_drvdata(dev);
255 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
258 if (ak7375_dev->active)
261 ret = regulator_bulk_enable(ARRAY_SIZE(ak7375_supply_names),
262 ak7375_dev->supplies);
266 /* Wait for vcm to become ready */
267 usleep_range(AK7375_POWER_DELAY_US, AK7375_POWER_DELAY_US + 500);
269 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
270 AK7375_MODE_ACTIVE, 1);
272 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
276 for (val = ak7375_dev->focus->val % AK7375_CTRL_STEPS;
277 val <= ak7375_dev->focus->val;
278 val += AK7375_CTRL_STEPS) {
279 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
282 dev_err_ratelimited(dev, "%s I2C failure: %d\n",
284 usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
287 ak7375_dev->active = true;
292 static const struct of_device_id ak7375_of_table[] = {
293 { .compatible = "asahi-kasei,ak7375" },
296 MODULE_DEVICE_TABLE(of, ak7375_of_table);
298 static const struct dev_pm_ops ak7375_pm_ops = {
299 SET_SYSTEM_SLEEP_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume)
300 SET_RUNTIME_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume, NULL)
303 static struct i2c_driver ak7375_i2c_driver = {
306 .pm = &ak7375_pm_ops,
307 .of_match_table = ak7375_of_table,
309 .probe_new = ak7375_probe,
310 .remove = ak7375_remove,
312 module_i2c_driver(ak7375_i2c_driver);
316 MODULE_DESCRIPTION("AK7375 VCM driver");
317 MODULE_LICENSE("GPL v2");