xref: /openbmc/linux/drivers/media/i2c/ak7375.c (revision aa0dc6a7)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
3 
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>
11 
12 #define AK7375_MAX_FOCUS_POS	4095
13 /*
14  * This sets the minimum granularity for the focus positions.
15  * A value of 1 gives maximum accuracy for a desired focus position
16  */
17 #define AK7375_FOCUS_STEPS	1
18 /*
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.
23  */
24 #define AK7375_CTRL_STEPS	64
25 #define AK7375_CTRL_DELAY_US	1000
26 
27 #define AK7375_REG_POSITION	0x0
28 #define AK7375_REG_CONT		0x2
29 #define AK7375_MODE_ACTIVE	0x0
30 #define AK7375_MODE_STANDBY	0x40
31 
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;
39 };
40 
41 static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl)
42 {
43 	return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm);
44 }
45 
46 static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev)
47 {
48 	return container_of(subdev, struct ak7375_device, sd);
49 }
50 
51 static int ak7375_i2c_write(struct ak7375_device *ak7375,
52 	u8 addr, u16 data, u8 size)
53 {
54 	struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd);
55 	u8 buf[3];
56 	int ret;
57 
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;
69 
70 	return 0;
71 }
72 
73 static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl)
74 {
75 	struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl);
76 
77 	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
78 		return ak7375_i2c_write(dev_vcm, AK7375_REG_POSITION,
79 					ctrl->val << 4, 2);
80 
81 	return -EINVAL;
82 }
83 
84 static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = {
85 	.s_ctrl = ak7375_set_ctrl,
86 };
87 
88 static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
89 {
90 	return pm_runtime_resume_and_get(sd->dev);
91 }
92 
93 static int ak7375_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
94 {
95 	pm_runtime_put(sd->dev);
96 
97 	return 0;
98 }
99 
100 static const struct v4l2_subdev_internal_ops ak7375_int_ops = {
101 	.open = ak7375_open,
102 	.close = ak7375_close,
103 };
104 
105 static const struct v4l2_subdev_ops ak7375_ops = { };
106 
107 static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev)
108 {
109 	v4l2_async_unregister_subdev(&ak7375_dev->sd);
110 	v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
111 	media_entity_cleanup(&ak7375_dev->sd.entity);
112 }
113 
114 static int ak7375_init_controls(struct ak7375_device *dev_vcm)
115 {
116 	struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
117 	const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops;
118 
119 	v4l2_ctrl_handler_init(hdl, 1);
120 
121 	dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
122 		0, AK7375_MAX_FOCUS_POS, AK7375_FOCUS_STEPS, 0);
123 
124 	if (hdl->error)
125 		dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
126 			__func__, hdl->error);
127 	dev_vcm->sd.ctrl_handler = hdl;
128 
129 	return hdl->error;
130 }
131 
132 static int ak7375_probe(struct i2c_client *client)
133 {
134 	struct ak7375_device *ak7375_dev;
135 	int ret;
136 
137 	ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
138 				  GFP_KERNEL);
139 	if (!ak7375_dev)
140 		return -ENOMEM;
141 
142 	v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops);
143 	ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
144 	ak7375_dev->sd.internal_ops = &ak7375_int_ops;
145 	ak7375_dev->sd.entity.function = MEDIA_ENT_F_LENS;
146 
147 	ret = ak7375_init_controls(ak7375_dev);
148 	if (ret)
149 		goto err_cleanup;
150 
151 	ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL);
152 	if (ret < 0)
153 		goto err_cleanup;
154 
155 	ret = v4l2_async_register_subdev(&ak7375_dev->sd);
156 	if (ret < 0)
157 		goto err_cleanup;
158 
159 	pm_runtime_set_active(&client->dev);
160 	pm_runtime_enable(&client->dev);
161 	pm_runtime_idle(&client->dev);
162 
163 	return 0;
164 
165 err_cleanup:
166 	v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
167 	media_entity_cleanup(&ak7375_dev->sd.entity);
168 
169 	return ret;
170 }
171 
172 static int ak7375_remove(struct i2c_client *client)
173 {
174 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
175 	struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
176 
177 	ak7375_subdev_cleanup(ak7375_dev);
178 	pm_runtime_disable(&client->dev);
179 	pm_runtime_set_suspended(&client->dev);
180 
181 	return 0;
182 }
183 
184 /*
185  * This function sets the vcm position, so it consumes least current
186  * The lens position is gradually moved in units of AK7375_CTRL_STEPS,
187  * to make the movements smoothly.
188  */
189 static int __maybe_unused ak7375_vcm_suspend(struct device *dev)
190 {
191 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
192 	struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
193 	int ret, val;
194 
195 	if (!ak7375_dev->active)
196 		return 0;
197 
198 	for (val = ak7375_dev->focus->val & ~(AK7375_CTRL_STEPS - 1);
199 	     val >= 0; val -= AK7375_CTRL_STEPS) {
200 		ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
201 				       val << 4, 2);
202 		if (ret)
203 			dev_err_once(dev, "%s I2C failure: %d\n",
204 				     __func__, ret);
205 		usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
206 	}
207 
208 	ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
209 			       AK7375_MODE_STANDBY, 1);
210 	if (ret)
211 		dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
212 
213 	ak7375_dev->active = false;
214 
215 	return 0;
216 }
217 
218 /*
219  * This function sets the vcm position to the value set by the user
220  * through v4l2_ctrl_ops s_ctrl handler
221  * The lens position is gradually moved in units of AK7375_CTRL_STEPS,
222  * to make the movements smoothly.
223  */
224 static int __maybe_unused ak7375_vcm_resume(struct device *dev)
225 {
226 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
227 	struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
228 	int ret, val;
229 
230 	if (ak7375_dev->active)
231 		return 0;
232 
233 	ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
234 		AK7375_MODE_ACTIVE, 1);
235 	if (ret) {
236 		dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
237 		return ret;
238 	}
239 
240 	for (val = ak7375_dev->focus->val % AK7375_CTRL_STEPS;
241 	     val <= ak7375_dev->focus->val;
242 	     val += AK7375_CTRL_STEPS) {
243 		ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
244 				       val << 4, 2);
245 		if (ret)
246 			dev_err_ratelimited(dev, "%s I2C failure: %d\n",
247 						__func__, ret);
248 		usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
249 	}
250 
251 	ak7375_dev->active = true;
252 
253 	return 0;
254 }
255 
256 static const struct of_device_id ak7375_of_table[] = {
257 	{ .compatible = "asahi-kasei,ak7375" },
258 	{ /* sentinel */ }
259 };
260 MODULE_DEVICE_TABLE(of, ak7375_of_table);
261 
262 static const struct dev_pm_ops ak7375_pm_ops = {
263 	SET_SYSTEM_SLEEP_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume)
264 	SET_RUNTIME_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume, NULL)
265 };
266 
267 static struct i2c_driver ak7375_i2c_driver = {
268 	.driver = {
269 		.name = "ak7375",
270 		.pm = &ak7375_pm_ops,
271 		.of_match_table = ak7375_of_table,
272 	},
273 	.probe_new = ak7375_probe,
274 	.remove = ak7375_remove,
275 };
276 module_i2c_driver(ak7375_i2c_driver);
277 
278 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
279 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
280 MODULE_DESCRIPTION("AK7375 VCM driver");
281 MODULE_LICENSE("GPL v2");
282