Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / i2c / ak7375.c
blobe1f94ee0f48f5c0f9f1a34d793964507423e3197
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 <media/v4l2-ctrls.h>
10 #include <media/v4l2-device.h>
12 #define AK7375_MAX_FOCUS_POS 4095
14 * This sets the minimum granularity for the focus positions.
15 * A value of 1 gives maximum accuracy for a desired focus position
17 #define AK7375_FOCUS_STEPS 1
19 * This acts as the minimum granularity of lens movement.
20 * Keep this value power of 2, so the control steps can be
21 * uniformly adjusted for gradual lens movement, with desired
22 * number of control steps.
24 #define AK7375_CTRL_STEPS 64
25 #define AK7375_CTRL_DELAY_US 1000
27 #define AK7375_REG_POSITION 0x0
28 #define AK7375_REG_CONT 0x2
29 #define AK7375_MODE_ACTIVE 0x0
30 #define AK7375_MODE_STANDBY 0x40
32 /* ak7375 device structure */
33 struct ak7375_device {
34 struct v4l2_ctrl_handler ctrls_vcm;
35 struct v4l2_subdev sd;
36 struct v4l2_ctrl *focus;
37 /* active or standby mode */
38 bool active;
41 static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl)
43 return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm);
46 static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev)
48 return container_of(subdev, struct ak7375_device, sd);
51 static int ak7375_i2c_write(struct ak7375_device *ak7375,
52 u8 addr, u16 data, u8 size)
54 struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd);
55 u8 buf[3];
56 int ret;
58 if (size != 1 && size != 2)
59 return -EINVAL;
60 buf[0] = addr;
61 buf[size] = data & 0xff;
62 if (size == 2)
63 buf[1] = (data >> 8) & 0xff;
64 ret = i2c_master_send(client, (const char *)buf, size + 1);
65 if (ret < 0)
66 return ret;
67 if (ret != size + 1)
68 return -EIO;
70 return 0;
73 static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl)
75 struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl);
77 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
78 return ak7375_i2c_write(dev_vcm, AK7375_REG_POSITION,
79 ctrl->val << 4, 2);
81 return -EINVAL;
84 static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = {
85 .s_ctrl = ak7375_set_ctrl,
88 static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
90 int ret;
92 ret = pm_runtime_get_sync(sd->dev);
93 if (ret < 0) {
94 pm_runtime_put_noidle(sd->dev);
95 return ret;
98 return 0;
101 static int ak7375_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
103 pm_runtime_put(sd->dev);
105 return 0;
108 static const struct v4l2_subdev_internal_ops ak7375_int_ops = {
109 .open = ak7375_open,
110 .close = ak7375_close,
113 static const struct v4l2_subdev_ops ak7375_ops = { };
115 static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev)
117 v4l2_async_unregister_subdev(&ak7375_dev->sd);
118 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
119 media_entity_cleanup(&ak7375_dev->sd.entity);
122 static int ak7375_init_controls(struct ak7375_device *dev_vcm)
124 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
125 const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops;
127 v4l2_ctrl_handler_init(hdl, 1);
129 dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
130 0, AK7375_MAX_FOCUS_POS, AK7375_FOCUS_STEPS, 0);
132 if (hdl->error)
133 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
134 __func__, hdl->error);
135 dev_vcm->sd.ctrl_handler = hdl;
137 return hdl->error;
140 static int ak7375_probe(struct i2c_client *client)
142 struct ak7375_device *ak7375_dev;
143 int ret;
145 ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
146 GFP_KERNEL);
147 if (!ak7375_dev)
148 return -ENOMEM;
150 v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops);
151 ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
152 ak7375_dev->sd.internal_ops = &ak7375_int_ops;
153 ak7375_dev->sd.entity.function = MEDIA_ENT_F_LENS;
155 ret = ak7375_init_controls(ak7375_dev);
156 if (ret)
157 goto err_cleanup;
159 ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL);
160 if (ret < 0)
161 goto err_cleanup;
163 ret = v4l2_async_register_subdev(&ak7375_dev->sd);
164 if (ret < 0)
165 goto err_cleanup;
167 pm_runtime_set_active(&client->dev);
168 pm_runtime_enable(&client->dev);
169 pm_runtime_idle(&client->dev);
171 return 0;
173 err_cleanup:
174 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
175 media_entity_cleanup(&ak7375_dev->sd.entity);
177 return ret;
180 static int ak7375_remove(struct i2c_client *client)
182 struct v4l2_subdev *sd = i2c_get_clientdata(client);
183 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
185 ak7375_subdev_cleanup(ak7375_dev);
186 pm_runtime_disable(&client->dev);
187 pm_runtime_set_suspended(&client->dev);
189 return 0;
193 * This function sets the vcm position, so it consumes least current
194 * The lens position is gradually moved in units of AK7375_CTRL_STEPS,
195 * to make the movements smoothly.
197 static int __maybe_unused ak7375_vcm_suspend(struct device *dev)
199 struct v4l2_subdev *sd = dev_get_drvdata(dev);
200 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
201 int ret, val;
203 if (!ak7375_dev->active)
204 return 0;
206 for (val = ak7375_dev->focus->val & ~(AK7375_CTRL_STEPS - 1);
207 val >= 0; val -= AK7375_CTRL_STEPS) {
208 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
209 val << 4, 2);
210 if (ret)
211 dev_err_once(dev, "%s I2C failure: %d\n",
212 __func__, ret);
213 usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
216 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
217 AK7375_MODE_STANDBY, 1);
218 if (ret)
219 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
221 ak7375_dev->active = false;
223 return 0;
227 * This function sets the vcm position to the value set by the user
228 * through v4l2_ctrl_ops s_ctrl handler
229 * The lens position is gradually moved in units of AK7375_CTRL_STEPS,
230 * to make the movements smoothly.
232 static int __maybe_unused ak7375_vcm_resume(struct device *dev)
234 struct v4l2_subdev *sd = dev_get_drvdata(dev);
235 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
236 int ret, val;
238 if (ak7375_dev->active)
239 return 0;
241 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
242 AK7375_MODE_ACTIVE, 1);
243 if (ret) {
244 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
245 return ret;
248 for (val = ak7375_dev->focus->val % AK7375_CTRL_STEPS;
249 val <= ak7375_dev->focus->val;
250 val += AK7375_CTRL_STEPS) {
251 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
252 val << 4, 2);
253 if (ret)
254 dev_err_ratelimited(dev, "%s I2C failure: %d\n",
255 __func__, ret);
256 usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
259 ak7375_dev->active = true;
261 return 0;
264 static const struct of_device_id ak7375_of_table[] = {
265 { .compatible = "asahi-kasei,ak7375" },
266 { /* sentinel */ }
268 MODULE_DEVICE_TABLE(of, ak7375_of_table);
270 static const struct dev_pm_ops ak7375_pm_ops = {
271 SET_SYSTEM_SLEEP_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume)
272 SET_RUNTIME_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume, NULL)
275 static struct i2c_driver ak7375_i2c_driver = {
276 .driver = {
277 .name = "ak7375",
278 .pm = &ak7375_pm_ops,
279 .of_match_table = ak7375_of_table,
281 .probe_new = ak7375_probe,
282 .remove = ak7375_remove,
284 module_i2c_driver(ak7375_i2c_driver);
286 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
287 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
288 MODULE_DESCRIPTION("AK7375 VCM driver");
289 MODULE_LICENSE("GPL v2");