xref: /openbmc/linux/drivers/media/i2c/dw9714.c (revision 7ae5c03a)
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 int 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 	return 0;
211 }
212 
213 /*
214  * This function sets the vcm position, so it consumes least current
215  * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
216  * to make the movements smoothly.
217  */
218 static int __maybe_unused dw9714_vcm_suspend(struct device *dev)
219 {
220 	struct i2c_client *client = to_i2c_client(dev);
221 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
222 	struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
223 	int ret, val;
224 
225 	if (pm_runtime_suspended(&client->dev))
226 		return 0;
227 
228 	for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
229 	     val >= 0; val -= DW9714_CTRL_STEPS) {
230 		ret = dw9714_i2c_write(client,
231 				       DW9714_VAL(val, DW9714_DEFAULT_S));
232 		if (ret)
233 			dev_err_once(dev, "%s I2C failure: %d", __func__, ret);
234 		usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
235 	}
236 
237 	ret = regulator_disable(dw9714_dev->vcc);
238 	if (ret)
239 		dev_err(dev, "Failed to disable vcc: %d\n", ret);
240 
241 	return ret;
242 }
243 
244 /*
245  * This function sets the vcm position to the value set by the user
246  * through v4l2_ctrl_ops s_ctrl handler
247  * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
248  * to make the movements smoothly.
249  */
250 static int  __maybe_unused dw9714_vcm_resume(struct device *dev)
251 {
252 	struct i2c_client *client = to_i2c_client(dev);
253 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
254 	struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
255 	int ret, val;
256 
257 	if (pm_runtime_suspended(&client->dev))
258 		return 0;
259 
260 	ret = regulator_enable(dw9714_dev->vcc);
261 	if (ret) {
262 		dev_err(dev, "Failed to enable vcc: %d\n", ret);
263 		return ret;
264 	}
265 	usleep_range(1000, 2000);
266 
267 	for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
268 	     val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
269 	     val += DW9714_CTRL_STEPS) {
270 		ret = dw9714_i2c_write(client,
271 				       DW9714_VAL(val, DW9714_DEFAULT_S));
272 		if (ret)
273 			dev_err_ratelimited(dev, "%s I2C failure: %d",
274 						__func__, ret);
275 		usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
276 	}
277 
278 	return 0;
279 }
280 
281 static const struct i2c_device_id dw9714_id_table[] = {
282 	{ DW9714_NAME, 0 },
283 	{ { 0 } }
284 };
285 MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
286 
287 static const struct of_device_id dw9714_of_table[] = {
288 	{ .compatible = "dongwoon,dw9714" },
289 	{ { 0 } }
290 };
291 MODULE_DEVICE_TABLE(of, dw9714_of_table);
292 
293 static const struct dev_pm_ops dw9714_pm_ops = {
294 	SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume)
295 	SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL)
296 };
297 
298 static struct i2c_driver dw9714_i2c_driver = {
299 	.driver = {
300 		.name = DW9714_NAME,
301 		.pm = &dw9714_pm_ops,
302 		.of_match_table = dw9714_of_table,
303 	},
304 	.probe_new = dw9714_probe,
305 	.remove = dw9714_remove,
306 	.id_table = dw9714_id_table,
307 };
308 
309 module_i2c_driver(dw9714_i2c_driver);
310 
311 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
312 MODULE_AUTHOR("Jian Xu Zheng");
313 MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>");
314 MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>");
315 MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>");
316 MODULE_DESCRIPTION("DW9714 VCM driver");
317 MODULE_LICENSE("GPL v2");
318