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