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