xref: /linux/drivers/media/i2c/ak7375.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
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 <linux/regulator/consumer.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-device.h>
12 
13 #define AK7375_MAX_FOCUS_POS	4095
14 /*
15  * This sets the minimum granularity for the focus positions.
16  * A value of 1 gives maximum accuracy for a desired focus position
17  */
18 #define AK7375_FOCUS_STEPS	1
19 /*
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.
24  */
25 #define AK7375_CTRL_STEPS	64
26 #define AK7375_CTRL_DELAY_US	1000
27 /*
28  * The vcm may take up 10 ms (tDELAY) to power on and start taking
29  * I2C messages. Based on AK7371 datasheet.
30  */
31 #define AK7375_POWER_DELAY_US	10000
32 
33 #define AK7375_REG_POSITION	0x0
34 #define AK7375_REG_CONT		0x2
35 #define AK7375_MODE_ACTIVE	0x0
36 #define AK7375_MODE_STANDBY	0x40
37 
38 static const char * const ak7375_supply_names[] = {
39 	"vdd",
40 	"vio",
41 };
42 
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)];
49 
50 	/* active or standby mode */
51 	bool active;
52 };
53 
54 static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl)
55 {
56 	return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm);
57 }
58 
59 static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev)
60 {
61 	return container_of(subdev, struct ak7375_device, sd);
62 }
63 
64 static int ak7375_i2c_write(struct ak7375_device *ak7375,
65 	u8 addr, u16 data, u8 size)
66 {
67 	struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd);
68 	u8 buf[3];
69 	int ret;
70 
71 	if (size != 1 && size != 2)
72 		return -EINVAL;
73 	buf[0] = addr;
74 	buf[size] = data & 0xff;
75 	if (size == 2)
76 		buf[1] = (data >> 8) & 0xff;
77 	ret = i2c_master_send(client, (const char *)buf, size + 1);
78 	if (ret < 0)
79 		return ret;
80 	if (ret != size + 1)
81 		return -EIO;
82 
83 	return 0;
84 }
85 
86 static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl)
87 {
88 	struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl);
89 
90 	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
91 		return ak7375_i2c_write(dev_vcm, AK7375_REG_POSITION,
92 					ctrl->val << 4, 2);
93 
94 	return -EINVAL;
95 }
96 
97 static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = {
98 	.s_ctrl = ak7375_set_ctrl,
99 };
100 
101 static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
102 {
103 	return pm_runtime_resume_and_get(sd->dev);
104 }
105 
106 static int ak7375_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
107 {
108 	pm_runtime_put(sd->dev);
109 
110 	return 0;
111 }
112 
113 static const struct v4l2_subdev_internal_ops ak7375_int_ops = {
114 	.open = ak7375_open,
115 	.close = ak7375_close,
116 };
117 
118 static const struct v4l2_subdev_ops ak7375_ops = { };
119 
120 static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev)
121 {
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);
125 }
126 
127 static int ak7375_init_controls(struct ak7375_device *dev_vcm)
128 {
129 	struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
130 	const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops;
131 
132 	v4l2_ctrl_handler_init(hdl, 1);
133 
134 	dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
135 		0, AK7375_MAX_FOCUS_POS, AK7375_FOCUS_STEPS, 0);
136 
137 	if (hdl->error)
138 		dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
139 			__func__, hdl->error);
140 	dev_vcm->sd.ctrl_handler = hdl;
141 
142 	return hdl->error;
143 }
144 
145 static int ak7375_probe(struct i2c_client *client)
146 {
147 	struct ak7375_device *ak7375_dev;
148 	int ret;
149 	unsigned int i;
150 
151 	ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
152 				  GFP_KERNEL);
153 	if (!ak7375_dev)
154 		return -ENOMEM;
155 
156 	for (i = 0; i < ARRAY_SIZE(ak7375_supply_names); i++)
157 		ak7375_dev->supplies[i].supply = ak7375_supply_names[i];
158 
159 	ret = devm_regulator_bulk_get(&client->dev,
160 				      ARRAY_SIZE(ak7375_supply_names),
161 				      ak7375_dev->supplies);
162 	if (ret) {
163 		dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
164 		return ret;
165 	}
166 
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;
171 
172 	ret = ak7375_init_controls(ak7375_dev);
173 	if (ret)
174 		goto err_cleanup;
175 
176 	ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL);
177 	if (ret < 0)
178 		goto err_cleanup;
179 
180 	ret = v4l2_async_register_subdev(&ak7375_dev->sd);
181 	if (ret < 0)
182 		goto err_cleanup;
183 
184 	pm_runtime_set_active(&client->dev);
185 	pm_runtime_enable(&client->dev);
186 	pm_runtime_idle(&client->dev);
187 
188 	return 0;
189 
190 err_cleanup:
191 	v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
192 	media_entity_cleanup(&ak7375_dev->sd.entity);
193 
194 	return ret;
195 }
196 
197 static void ak7375_remove(struct i2c_client *client)
198 {
199 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
200 	struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
201 
202 	ak7375_subdev_cleanup(ak7375_dev);
203 	pm_runtime_disable(&client->dev);
204 	pm_runtime_set_suspended(&client->dev);
205 }
206 
207 /*
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.
211  */
212 static int __maybe_unused ak7375_vcm_suspend(struct device *dev)
213 {
214 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
215 	struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
216 	int ret, val;
217 
218 	if (!ak7375_dev->active)
219 		return 0;
220 
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,
224 				       val << 4, 2);
225 		if (ret)
226 			dev_err_once(dev, "%s I2C failure: %d\n",
227 				     __func__, ret);
228 		usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
229 	}
230 
231 	ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
232 			       AK7375_MODE_STANDBY, 1);
233 	if (ret)
234 		dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
235 
236 	ret = regulator_bulk_disable(ARRAY_SIZE(ak7375_supply_names),
237 				     ak7375_dev->supplies);
238 	if (ret)
239 		return ret;
240 
241 	ak7375_dev->active = false;
242 
243 	return 0;
244 }
245 
246 /*
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.
251  */
252 static int __maybe_unused ak7375_vcm_resume(struct device *dev)
253 {
254 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
255 	struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
256 	int ret, val;
257 
258 	if (ak7375_dev->active)
259 		return 0;
260 
261 	ret = regulator_bulk_enable(ARRAY_SIZE(ak7375_supply_names),
262 				    ak7375_dev->supplies);
263 	if (ret)
264 		return ret;
265 
266 	/* Wait for vcm to become ready */
267 	usleep_range(AK7375_POWER_DELAY_US, AK7375_POWER_DELAY_US + 500);
268 
269 	ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
270 		AK7375_MODE_ACTIVE, 1);
271 	if (ret) {
272 		dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
273 		return ret;
274 	}
275 
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,
280 				       val << 4, 2);
281 		if (ret)
282 			dev_err_ratelimited(dev, "%s I2C failure: %d\n",
283 						__func__, ret);
284 		usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
285 	}
286 
287 	ak7375_dev->active = true;
288 
289 	return 0;
290 }
291 
292 static const struct of_device_id ak7375_of_table[] = {
293 	{ .compatible = "asahi-kasei,ak7375" },
294 	{ /* sentinel */ }
295 };
296 MODULE_DEVICE_TABLE(of, ak7375_of_table);
297 
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)
301 };
302 
303 static struct i2c_driver ak7375_i2c_driver = {
304 	.driver = {
305 		.name = "ak7375",
306 		.pm = &ak7375_pm_ops,
307 		.of_match_table = ak7375_of_table,
308 	},
309 	.probe_new = ak7375_probe,
310 	.remove = ak7375_remove,
311 };
312 module_i2c_driver(ak7375_i2c_driver);
313 
314 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
315 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
316 MODULE_DESCRIPTION("AK7375 VCM driver");
317 MODULE_LICENSE("GPL v2");
318