xref: /linux/drivers/media/i2c/dw9714.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2015--2017 Intel Corporation.
3 
4 #include <linux/delay.h>
5 #include <linux/i2c.h>
6 #include <linux/module.h>
7 #include <linux/pm_runtime.h>
8 #include <linux/regulator/consumer.h>
9 #include <media/v4l2-ctrls.h>
10 #include <media/v4l2-device.h>
11 #include <media/v4l2-event.h>
12 
13 #define DW9714_NAME		"dw9714"
14 #define DW9714_MAX_FOCUS_POS	1023
15 /*
16  * This sets the minimum granularity for the focus positions.
17  * A value of 1 gives maximum accuracy for a desired focus position
18  */
19 #define DW9714_FOCUS_STEPS	1
20 /*
21  * This acts as the minimum granularity of lens movement.
22  * Keep this value power of 2, so the control steps can be
23  * uniformly adjusted for gradual lens movement, with desired
24  * number of control steps.
25  */
26 #define DW9714_CTRL_STEPS	16
27 #define DW9714_CTRL_DELAY_US	1000
28 /*
29  * S[3:2] = 0x00, codes per step for "Linear Slope Control"
30  * S[1:0] = 0x00, step period
31  */
32 #define DW9714_DEFAULT_S 0x0
33 #define DW9714_VAL(data, s) ((data) << 4 | (s))
34 
35 /* dw9714 device structure */
36 struct dw9714_device {
37 	struct v4l2_ctrl_handler ctrls_vcm;
38 	struct v4l2_subdev sd;
39 	u16 current_val;
40 	struct regulator *vcc;
41 };
42 
43 static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl)
44 {
45 	return container_of(ctrl->handler, struct dw9714_device, ctrls_vcm);
46 }
47 
48 static inline struct dw9714_device *sd_to_dw9714_vcm(struct v4l2_subdev *subdev)
49 {
50 	return container_of(subdev, struct dw9714_device, sd);
51 }
52 
53 static int dw9714_i2c_write(struct i2c_client *client, u16 data)
54 {
55 	int ret;
56 	__be16 val = cpu_to_be16(data);
57 
58 	ret = i2c_master_send(client, (const char *)&val, sizeof(val));
59 	if (ret != sizeof(val)) {
60 		dev_err(&client->dev, "I2C write fail\n");
61 		return -EIO;
62 	}
63 	return 0;
64 }
65 
66 static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val)
67 {
68 	struct i2c_client *client = v4l2_get_subdevdata(&dw9714_dev->sd);
69 
70 	dw9714_dev->current_val = val;
71 
72 	return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));
73 }
74 
75 static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl)
76 {
77 	struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl);
78 
79 	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
80 		return dw9714_t_focus_vcm(dev_vcm, ctrl->val);
81 
82 	return -EINVAL;
83 }
84 
85 static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = {
86 	.s_ctrl = dw9714_set_ctrl,
87 };
88 
89 static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
90 {
91 	return pm_runtime_resume_and_get(sd->dev);
92 }
93 
94 static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
95 {
96 	pm_runtime_put(sd->dev);
97 
98 	return 0;
99 }
100 
101 static const struct v4l2_subdev_internal_ops dw9714_int_ops = {
102 	.open = dw9714_open,
103 	.close = dw9714_close,
104 };
105 
106 static const struct v4l2_subdev_core_ops dw9714_core_ops = {
107 	.log_status = v4l2_ctrl_subdev_log_status,
108 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
109 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
110 };
111 
112 static const struct v4l2_subdev_ops dw9714_ops = {
113 	.core = &dw9714_core_ops,
114 };
115 
116 static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev)
117 {
118 	v4l2_async_unregister_subdev(&dw9714_dev->sd);
119 	v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);
120 	media_entity_cleanup(&dw9714_dev->sd.entity);
121 }
122 
123 static int dw9714_init_controls(struct dw9714_device *dev_vcm)
124 {
125 	struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
126 	const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops;
127 
128 	v4l2_ctrl_handler_init(hdl, 1);
129 
130 	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
131 			  0, DW9714_MAX_FOCUS_POS, DW9714_FOCUS_STEPS, 0);
132 
133 	if (hdl->error)
134 		dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
135 			__func__, hdl->error);
136 	dev_vcm->sd.ctrl_handler = hdl;
137 	return hdl->error;
138 }
139 
140 static int dw9714_probe(struct i2c_client *client)
141 {
142 	struct dw9714_device *dw9714_dev;
143 	int rval;
144 
145 	dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev),
146 				  GFP_KERNEL);
147 	if (dw9714_dev == NULL)
148 		return -ENOMEM;
149 
150 	dw9714_dev->vcc = devm_regulator_get(&client->dev, "vcc");
151 	if (IS_ERR(dw9714_dev->vcc))
152 		return PTR_ERR(dw9714_dev->vcc);
153 
154 	rval = regulator_enable(dw9714_dev->vcc);
155 	if (rval < 0) {
156 		dev_err(&client->dev, "failed to enable vcc: %d\n", rval);
157 		return rval;
158 	}
159 
160 	v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops);
161 	dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
162 				V4L2_SUBDEV_FL_HAS_EVENTS;
163 	dw9714_dev->sd.internal_ops = &dw9714_int_ops;
164 
165 	rval = dw9714_init_controls(dw9714_dev);
166 	if (rval)
167 		goto err_cleanup;
168 
169 	rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL);
170 	if (rval < 0)
171 		goto err_cleanup;
172 
173 	dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
174 
175 	rval = v4l2_async_register_subdev(&dw9714_dev->sd);
176 	if (rval < 0)
177 		goto err_cleanup;
178 
179 	pm_runtime_set_active(&client->dev);
180 	pm_runtime_enable(&client->dev);
181 	pm_runtime_idle(&client->dev);
182 
183 	return 0;
184 
185 err_cleanup:
186 	regulator_disable(dw9714_dev->vcc);
187 	v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);
188 	media_entity_cleanup(&dw9714_dev->sd.entity);
189 
190 	return rval;
191 }
192 
193 static void dw9714_remove(struct i2c_client *client)
194 {
195 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
196 	struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
197 	int ret;
198 
199 	pm_runtime_disable(&client->dev);
200 	if (!pm_runtime_status_suspended(&client->dev)) {
201 		ret = regulator_disable(dw9714_dev->vcc);
202 		if (ret) {
203 			dev_err(&client->dev,
204 				"Failed to disable vcc: %d\n", ret);
205 		}
206 	}
207 	pm_runtime_set_suspended(&client->dev);
208 	dw9714_subdev_cleanup(dw9714_dev);
209 }
210 
211 /*
212  * This function sets the vcm position, so it consumes least current
213  * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
214  * to make the movements smoothly.
215  */
216 static int __maybe_unused dw9714_vcm_suspend(struct device *dev)
217 {
218 	struct i2c_client *client = to_i2c_client(dev);
219 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
220 	struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
221 	int ret, val;
222 
223 	if (pm_runtime_suspended(&client->dev))
224 		return 0;
225 
226 	for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
227 	     val >= 0; val -= DW9714_CTRL_STEPS) {
228 		ret = dw9714_i2c_write(client,
229 				       DW9714_VAL(val, DW9714_DEFAULT_S));
230 		if (ret)
231 			dev_err_once(dev, "%s I2C failure: %d", __func__, ret);
232 		usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
233 	}
234 
235 	ret = regulator_disable(dw9714_dev->vcc);
236 	if (ret)
237 		dev_err(dev, "Failed to disable vcc: %d\n", ret);
238 
239 	return ret;
240 }
241 
242 /*
243  * This function sets the vcm position to the value set by the user
244  * through v4l2_ctrl_ops s_ctrl handler
245  * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
246  * to make the movements smoothly.
247  */
248 static int  __maybe_unused dw9714_vcm_resume(struct device *dev)
249 {
250 	struct i2c_client *client = to_i2c_client(dev);
251 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
252 	struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
253 	int ret, val;
254 
255 	if (pm_runtime_suspended(&client->dev))
256 		return 0;
257 
258 	ret = regulator_enable(dw9714_dev->vcc);
259 	if (ret) {
260 		dev_err(dev, "Failed to enable vcc: %d\n", ret);
261 		return ret;
262 	}
263 	usleep_range(1000, 2000);
264 
265 	for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
266 	     val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
267 	     val += DW9714_CTRL_STEPS) {
268 		ret = dw9714_i2c_write(client,
269 				       DW9714_VAL(val, DW9714_DEFAULT_S));
270 		if (ret)
271 			dev_err_ratelimited(dev, "%s I2C failure: %d",
272 						__func__, ret);
273 		usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
274 	}
275 
276 	return 0;
277 }
278 
279 static const struct i2c_device_id dw9714_id_table[] = {
280 	{ DW9714_NAME, 0 },
281 	{ { 0 } }
282 };
283 MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
284 
285 static const struct of_device_id dw9714_of_table[] = {
286 	{ .compatible = "dongwoon,dw9714" },
287 	{ { 0 } }
288 };
289 MODULE_DEVICE_TABLE(of, dw9714_of_table);
290 
291 static const struct dev_pm_ops dw9714_pm_ops = {
292 	SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume)
293 	SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL)
294 };
295 
296 static struct i2c_driver dw9714_i2c_driver = {
297 	.driver = {
298 		.name = DW9714_NAME,
299 		.pm = &dw9714_pm_ops,
300 		.of_match_table = dw9714_of_table,
301 	},
302 	.probe_new = dw9714_probe,
303 	.remove = dw9714_remove,
304 	.id_table = dw9714_id_table,
305 };
306 
307 module_i2c_driver(dw9714_i2c_driver);
308 
309 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
310 MODULE_AUTHOR("Jian Xu Zheng");
311 MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>");
312 MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>");
313 MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>");
314 MODULE_DESCRIPTION("DW9714 VCM driver");
315 MODULE_LICENSE("GPL v2");
316