Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / media / i2c / ak7375.c
blob9a2432cea3fff9f0ff41796f97236449792dccfc
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
4 #include <linux/acpi.h>
5 #include <linux/delay.h>
6 #include <linux/i2c.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 struct ak73xx_chipdef {
14 u8 reg_position;
15 u8 reg_cont;
16 u8 shift_pos;
17 u8 mode_active;
18 u8 mode_standby;
19 bool has_standby; /* Some chips may not have standby mode */
20 u16 focus_pos_max;
22 * This sets the minimum granularity for the focus positions.
23 * A value of 1 gives maximum accuracy for a desired focus position
25 u16 focus_steps;
27 * This acts as the minimum granularity of lens movement.
28 * Keep this value power of 2, so the control steps can be
29 * uniformly adjusted for gradual lens movement, with desired
30 * number of control steps.
32 u16 ctrl_steps;
33 u16 ctrl_delay_us;
35 * The vcm may take time (tDELAY) to power on and start taking
36 * I2C messages.
38 u16 power_delay_us;
41 static const struct ak73xx_chipdef ak7345_cdef = {
42 .reg_position = 0x0,
43 .reg_cont = 0x2,
44 .shift_pos = 7, /* 9 bits position values, need to << 7 */
45 .mode_active = 0x0,
46 .has_standby = false,
47 .focus_pos_max = 511,
48 .focus_steps = 1,
49 .ctrl_steps = 16,
50 .ctrl_delay_us = 1000,
51 .power_delay_us = 20000,
54 static const struct ak73xx_chipdef ak7375_cdef = {
55 .reg_position = 0x0,
56 .reg_cont = 0x2,
57 .shift_pos = 4, /* 12 bits position values, need to << 4 */
58 .mode_active = 0x0,
59 .mode_standby = 0x40,
60 .has_standby = true,
61 .focus_pos_max = 4095,
62 .focus_steps = 1,
63 .ctrl_steps = 64,
64 .ctrl_delay_us = 1000,
65 .power_delay_us = 10000,
68 static const char * const ak7375_supply_names[] = {
69 "vdd",
70 "vio",
73 /* ak7375 device structure */
74 struct ak7375_device {
75 const struct ak73xx_chipdef *cdef;
76 struct v4l2_ctrl_handler ctrls_vcm;
77 struct v4l2_subdev sd;
78 struct v4l2_ctrl *focus;
79 struct regulator_bulk_data supplies[ARRAY_SIZE(ak7375_supply_names)];
81 /* active or standby mode */
82 bool active;
85 static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl)
87 return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm);
90 static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev)
92 return container_of(subdev, struct ak7375_device, sd);
95 static int ak7375_i2c_write(struct ak7375_device *ak7375,
96 u8 addr, u16 data, u8 size)
98 struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd);
99 u8 buf[3];
100 int ret;
102 if (size != 1 && size != 2)
103 return -EINVAL;
104 buf[0] = addr;
105 buf[size] = data & 0xff;
106 if (size == 2)
107 buf[1] = (data >> 8) & 0xff;
108 ret = i2c_master_send(client, (const char *)buf, size + 1);
109 if (ret < 0)
110 return ret;
111 if (ret != size + 1)
112 return -EIO;
114 return 0;
117 static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl)
119 struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl);
120 const struct ak73xx_chipdef *cdef = dev_vcm->cdef;
122 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
123 return ak7375_i2c_write(dev_vcm, cdef->reg_position,
124 ctrl->val << cdef->shift_pos, 2);
126 return -EINVAL;
129 static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = {
130 .s_ctrl = ak7375_set_ctrl,
133 static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
135 return pm_runtime_resume_and_get(sd->dev);
138 static int ak7375_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
140 pm_runtime_put(sd->dev);
142 return 0;
145 static const struct v4l2_subdev_internal_ops ak7375_int_ops = {
146 .open = ak7375_open,
147 .close = ak7375_close,
150 static const struct v4l2_subdev_ops ak7375_ops = { };
152 static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev)
154 v4l2_async_unregister_subdev(&ak7375_dev->sd);
155 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
156 media_entity_cleanup(&ak7375_dev->sd.entity);
159 static int ak7375_init_controls(struct ak7375_device *dev_vcm)
161 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
162 const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops;
163 const struct ak73xx_chipdef *cdef = dev_vcm->cdef;
165 v4l2_ctrl_handler_init(hdl, 1);
167 dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
168 0, cdef->focus_pos_max, cdef->focus_steps, 0);
170 if (hdl->error)
171 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
172 __func__, hdl->error);
173 dev_vcm->sd.ctrl_handler = hdl;
175 return hdl->error;
178 static int ak7375_probe(struct i2c_client *client)
180 struct ak7375_device *ak7375_dev;
181 int ret;
182 unsigned int i;
184 ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
185 GFP_KERNEL);
186 if (!ak7375_dev)
187 return -ENOMEM;
189 ak7375_dev->cdef = device_get_match_data(&client->dev);
191 for (i = 0; i < ARRAY_SIZE(ak7375_supply_names); i++)
192 ak7375_dev->supplies[i].supply = ak7375_supply_names[i];
194 ret = devm_regulator_bulk_get(&client->dev,
195 ARRAY_SIZE(ak7375_supply_names),
196 ak7375_dev->supplies);
197 if (ret) {
198 dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
199 return ret;
202 v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops);
203 ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
204 ak7375_dev->sd.internal_ops = &ak7375_int_ops;
205 ak7375_dev->sd.entity.function = MEDIA_ENT_F_LENS;
207 ret = ak7375_init_controls(ak7375_dev);
208 if (ret)
209 goto err_cleanup;
211 ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL);
212 if (ret < 0)
213 goto err_cleanup;
215 ret = v4l2_async_register_subdev(&ak7375_dev->sd);
216 if (ret < 0)
217 goto err_cleanup;
219 pm_runtime_set_active(&client->dev);
220 pm_runtime_enable(&client->dev);
221 pm_runtime_idle(&client->dev);
223 return 0;
225 err_cleanup:
226 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
227 media_entity_cleanup(&ak7375_dev->sd.entity);
229 return ret;
232 static void ak7375_remove(struct i2c_client *client)
234 struct v4l2_subdev *sd = i2c_get_clientdata(client);
235 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
237 ak7375_subdev_cleanup(ak7375_dev);
238 pm_runtime_disable(&client->dev);
239 pm_runtime_set_suspended(&client->dev);
243 * This function sets the vcm position, so it consumes least current
244 * The lens position is gradually moved in units of ctrl_steps,
245 * to make the movements smoothly.
247 static int __maybe_unused ak7375_vcm_suspend(struct device *dev)
249 struct v4l2_subdev *sd = dev_get_drvdata(dev);
250 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
251 const struct ak73xx_chipdef *cdef = ak7375_dev->cdef;
252 int ret, val;
254 if (!ak7375_dev->active)
255 return 0;
257 for (val = ak7375_dev->focus->val & ~(cdef->ctrl_steps - 1);
258 val >= 0; val -= cdef->ctrl_steps) {
259 ret = ak7375_i2c_write(ak7375_dev, cdef->reg_position,
260 val << cdef->shift_pos, 2);
261 if (ret)
262 dev_err_once(dev, "%s I2C failure: %d\n",
263 __func__, ret);
264 usleep_range(cdef->ctrl_delay_us, cdef->ctrl_delay_us + 10);
267 if (cdef->has_standby) {
268 ret = ak7375_i2c_write(ak7375_dev, cdef->reg_cont,
269 cdef->mode_standby, 1);
270 if (ret)
271 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
274 ret = regulator_bulk_disable(ARRAY_SIZE(ak7375_supply_names),
275 ak7375_dev->supplies);
276 if (ret)
277 return ret;
279 ak7375_dev->active = false;
281 return 0;
285 * This function sets the vcm position to the value set by the user
286 * through v4l2_ctrl_ops s_ctrl handler
287 * The lens position is gradually moved in units of ctrl_steps,
288 * to make the movements smoothly.
290 static int __maybe_unused ak7375_vcm_resume(struct device *dev)
292 struct v4l2_subdev *sd = dev_get_drvdata(dev);
293 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
294 const struct ak73xx_chipdef *cdef = ak7375_dev->cdef;
295 int ret, val;
297 if (ak7375_dev->active)
298 return 0;
300 ret = regulator_bulk_enable(ARRAY_SIZE(ak7375_supply_names),
301 ak7375_dev->supplies);
302 if (ret)
303 return ret;
305 /* Wait for vcm to become ready */
306 usleep_range(cdef->power_delay_us, cdef->power_delay_us + 500);
308 ret = ak7375_i2c_write(ak7375_dev, cdef->reg_cont,
309 cdef->mode_active, 1);
310 if (ret) {
311 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
312 return ret;
315 for (val = ak7375_dev->focus->val % cdef->ctrl_steps;
316 val <= ak7375_dev->focus->val;
317 val += cdef->ctrl_steps) {
318 ret = ak7375_i2c_write(ak7375_dev, cdef->reg_position,
319 val << cdef->shift_pos, 2);
320 if (ret)
321 dev_err_ratelimited(dev, "%s I2C failure: %d\n",
322 __func__, ret);
323 usleep_range(cdef->ctrl_delay_us, cdef->ctrl_delay_us + 10);
326 ak7375_dev->active = true;
328 return 0;
331 static const struct of_device_id ak7375_of_table[] = {
332 { .compatible = "asahi-kasei,ak7345", .data = &ak7345_cdef, },
333 { .compatible = "asahi-kasei,ak7375", .data = &ak7375_cdef, },
334 { /* sentinel */ }
336 MODULE_DEVICE_TABLE(of, ak7375_of_table);
338 static const struct dev_pm_ops ak7375_pm_ops = {
339 SET_SYSTEM_SLEEP_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume)
340 SET_RUNTIME_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume, NULL)
343 static struct i2c_driver ak7375_i2c_driver = {
344 .driver = {
345 .name = "ak7375",
346 .pm = &ak7375_pm_ops,
347 .of_match_table = ak7375_of_table,
349 .probe = ak7375_probe,
350 .remove = ak7375_remove,
352 module_i2c_driver(ak7375_i2c_driver);
354 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
355 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
356 MODULE_DESCRIPTION("AK7375 VCM driver");
357 MODULE_LICENSE("GPL v2");