blob: fd4c5f67163de59323d2d1851615e11fad8eec65 [file] [log] [blame]
Pavel Machekbee3d512016-08-05 07:26:11 -03001/*
2 * drivers/media/i2c/ad5820.c
3 *
4 * AD5820 DAC driver for camera voice coil focus.
5 *
6 * Copyright (C) 2008 Nokia Corporation
7 * Copyright (C) 2007 Texas Instruments
8 * Copyright (C) 2016 Pavel Machek <pavel@ucw.cz>
9 *
10 * Contact: Tuukka Toivonen <tuukkat76@gmail.com>
11 * Sakari Ailus <sakari.ailus@iki.fi>
12 *
13 * Based on af_d88.c by Texas Instruments.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 */
24
25#include <linux/errno.h>
26#include <linux/i2c.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/regulator/consumer.h>
30
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-device.h>
33#include <media/v4l2-subdev.h>
34
35#define AD5820_NAME "ad5820"
36
37/* Register definitions */
38#define AD5820_POWER_DOWN (1 << 15)
39#define AD5820_DAC_SHIFT 4
40#define AD5820_RAMP_MODE_LINEAR (0 << 3)
41#define AD5820_RAMP_MODE_64_16 (1 << 3)
42
43#define CODE_TO_RAMP_US(s) ((s) == 0 ? 0 : (1 << ((s) - 1)) * 50)
44#define RAMP_US_TO_CODE(c) fls(((c) + ((c)>>1)) / 50)
45
46#define to_ad5820_device(sd) container_of(sd, struct ad5820_device, subdev)
47
48struct ad5820_device {
49 struct v4l2_subdev subdev;
50 struct ad5820_platform_data *platform_data;
51 struct regulator *vana;
52
53 struct v4l2_ctrl_handler ctrls;
54 u32 focus_absolute;
55 u32 focus_ramp_time;
56 u32 focus_ramp_mode;
57
58 struct mutex power_lock;
59 int power_count;
60
Sakari Ailus39331862016-09-05 04:09:42 -030061 bool standby;
Pavel Machekbee3d512016-08-05 07:26:11 -030062};
63
64static int ad5820_write(struct ad5820_device *coil, u16 data)
65{
66 struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
67 struct i2c_msg msg;
68 int r;
69
70 if (!client->adapter)
71 return -ENODEV;
72
73 data = cpu_to_be16(data);
74 msg.addr = client->addr;
75 msg.flags = 0;
76 msg.len = 2;
77 msg.buf = (u8 *)&data;
78
79 r = i2c_transfer(client->adapter, &msg, 1);
80 if (r < 0) {
81 dev_err(&client->dev, "write failed, error %d\n", r);
82 return r;
83 }
84
85 return 0;
86}
87
88/*
89 * Calculate status word and write it to the device based on current
90 * values of V4L2 controls. It is assumed that the stored V4L2 control
91 * values are properly limited and rounded.
92 */
93static int ad5820_update_hw(struct ad5820_device *coil)
94{
95 u16 status;
96
97 status = RAMP_US_TO_CODE(coil->focus_ramp_time);
98 status |= coil->focus_ramp_mode
99 ? AD5820_RAMP_MODE_64_16 : AD5820_RAMP_MODE_LINEAR;
100 status |= coil->focus_absolute << AD5820_DAC_SHIFT;
101
102 if (coil->standby)
103 status |= AD5820_POWER_DOWN;
104
105 return ad5820_write(coil, status);
106}
107
108/*
109 * Power handling
110 */
Sakari Ailus39331862016-09-05 04:09:42 -0300111static int ad5820_power_off(struct ad5820_device *coil, bool standby)
Pavel Machekbee3d512016-08-05 07:26:11 -0300112{
113 int ret = 0, ret2;
114
115 /*
116 * Go to standby first as real power off my be denied by the hardware
117 * (single power line control for both coil and sensor).
118 */
119 if (standby) {
Sakari Ailus39331862016-09-05 04:09:42 -0300120 coil->standby = true;
Pavel Machekbee3d512016-08-05 07:26:11 -0300121 ret = ad5820_update_hw(coil);
122 }
123
124 ret2 = regulator_disable(coil->vana);
125 if (ret)
126 return ret;
127 return ret2;
128}
129
Sakari Ailus39331862016-09-05 04:09:42 -0300130static int ad5820_power_on(struct ad5820_device *coil, bool restore)
Pavel Machekbee3d512016-08-05 07:26:11 -0300131{
132 int ret;
133
134 ret = regulator_enable(coil->vana);
135 if (ret < 0)
136 return ret;
137
138 if (restore) {
139 /* Restore the hardware settings. */
Sakari Ailus39331862016-09-05 04:09:42 -0300140 coil->standby = false;
Pavel Machekbee3d512016-08-05 07:26:11 -0300141 ret = ad5820_update_hw(coil);
142 if (ret)
143 goto fail;
144 }
145 return 0;
146
147fail:
Sakari Ailus39331862016-09-05 04:09:42 -0300148 coil->standby = true;
Pavel Machekbee3d512016-08-05 07:26:11 -0300149 regulator_disable(coil->vana);
150
151 return ret;
152}
153
154/*
155 * V4L2 controls
156 */
157static int ad5820_set_ctrl(struct v4l2_ctrl *ctrl)
158{
159 struct ad5820_device *coil =
160 container_of(ctrl->handler, struct ad5820_device, ctrls);
161
162 switch (ctrl->id) {
163 case V4L2_CID_FOCUS_ABSOLUTE:
164 coil->focus_absolute = ctrl->val;
165 return ad5820_update_hw(coil);
166 }
167
168 return 0;
169}
170
171static const struct v4l2_ctrl_ops ad5820_ctrl_ops = {
172 .s_ctrl = ad5820_set_ctrl,
173};
174
175
176static int ad5820_init_controls(struct ad5820_device *coil)
177{
178 v4l2_ctrl_handler_init(&coil->ctrls, 1);
179
180 /*
181 * V4L2_CID_FOCUS_ABSOLUTE
182 *
183 * Minimum current is 0 mA, maximum is 100 mA. Thus, 1 code is
184 * equivalent to 100/1023 = 0.0978 mA. Nevertheless, we do not use [mA]
185 * for focus position, because it is meaningless for user. Meaningful
186 * would be to use focus distance or even its inverse, but since the
187 * driver doesn't have sufficiently knowledge to do the conversion, we
188 * will just use abstract codes here. In any case, smaller value = focus
189 * position farther from camera. The default zero value means focus at
190 * infinity, and also least current consumption.
191 */
192 v4l2_ctrl_new_std(&coil->ctrls, &ad5820_ctrl_ops,
193 V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
194
195 if (coil->ctrls.error)
196 return coil->ctrls.error;
197
198 coil->focus_absolute = 0;
199 coil->focus_ramp_time = 0;
200 coil->focus_ramp_mode = 0;
201
202 coil->subdev.ctrl_handler = &coil->ctrls;
203
204 return 0;
205}
206
207/*
208 * V4L2 subdev operations
209 */
210static int ad5820_registered(struct v4l2_subdev *subdev)
211{
212 struct ad5820_device *coil = to_ad5820_device(subdev);
213
214 return ad5820_init_controls(coil);
215}
216
217static int
218ad5820_set_power(struct v4l2_subdev *subdev, int on)
219{
220 struct ad5820_device *coil = to_ad5820_device(subdev);
221 int ret = 0;
222
223 mutex_lock(&coil->power_lock);
224
225 /*
226 * If the power count is modified from 0 to != 0 or from != 0 to 0,
227 * update the power state.
228 */
229 if (coil->power_count == !on) {
Sakari Ailus39331862016-09-05 04:09:42 -0300230 ret = on ? ad5820_power_on(coil, true) :
231 ad5820_power_off(coil, true);
Pavel Machekbee3d512016-08-05 07:26:11 -0300232 if (ret < 0)
233 goto done;
234 }
235
236 /* Update the power count. */
237 coil->power_count += on ? 1 : -1;
238 WARN_ON(coil->power_count < 0);
239
240done:
241 mutex_unlock(&coil->power_lock);
242 return ret;
243}
244
245static int ad5820_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
246{
247 return ad5820_set_power(sd, 1);
248}
249
250static int ad5820_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
251{
252 return ad5820_set_power(sd, 0);
253}
254
255static const struct v4l2_subdev_core_ops ad5820_core_ops = {
256 .s_power = ad5820_set_power,
257};
258
259static const struct v4l2_subdev_ops ad5820_ops = {
260 .core = &ad5820_core_ops,
261};
262
263static const struct v4l2_subdev_internal_ops ad5820_internal_ops = {
264 .registered = ad5820_registered,
265 .open = ad5820_open,
266 .close = ad5820_close,
267};
268
269/*
270 * I2C driver
271 */
272#ifdef CONFIG_PM
273
274static int ad5820_suspend(struct device *dev)
275{
276 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
277 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
278 struct ad5820_device *coil = to_ad5820_device(subdev);
279
280 if (!coil->power_count)
281 return 0;
282
Sakari Ailus39331862016-09-05 04:09:42 -0300283 return ad5820_power_off(coil, false);
Pavel Machekbee3d512016-08-05 07:26:11 -0300284}
285
286static int ad5820_resume(struct device *dev)
287{
288 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
289 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
290 struct ad5820_device *coil = to_ad5820_device(subdev);
291
292 if (!coil->power_count)
293 return 0;
294
Sakari Ailus39331862016-09-05 04:09:42 -0300295 return ad5820_power_on(coil, true);
Pavel Machekbee3d512016-08-05 07:26:11 -0300296}
297
298#else
299
300#define ad5820_suspend NULL
301#define ad5820_resume NULL
302
303#endif /* CONFIG_PM */
304
305static int ad5820_probe(struct i2c_client *client,
306 const struct i2c_device_id *devid)
307{
308 struct ad5820_device *coil;
309 int ret;
310
311 coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
312 if (!coil)
313 return -ENOMEM;
314
315 coil->vana = devm_regulator_get(&client->dev, "VANA");
316 if (IS_ERR(coil->vana)) {
317 ret = PTR_ERR(coil->vana);
318 if (ret != -EPROBE_DEFER)
319 dev_err(&client->dev, "could not get regulator for vana\n");
320 return ret;
321 }
322
323 mutex_init(&coil->power_lock);
324
325 v4l2_i2c_subdev_init(&coil->subdev, client, &ad5820_ops);
326 coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
327 coil->subdev.internal_ops = &ad5820_internal_ops;
328 strcpy(coil->subdev.name, "ad5820 focus");
329
330 ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
331 if (ret < 0)
332 goto cleanup2;
333
334 ret = v4l2_async_register_subdev(&coil->subdev);
335 if (ret < 0)
336 goto cleanup;
337
338 return ret;
339
340cleanup2:
341 mutex_destroy(&coil->power_lock);
342cleanup:
343 media_entity_cleanup(&coil->subdev.entity);
344 return ret;
345}
346
347static int __exit ad5820_remove(struct i2c_client *client)
348{
349 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
350 struct ad5820_device *coil = to_ad5820_device(subdev);
351
352 v4l2_device_unregister_subdev(&coil->subdev);
353 v4l2_ctrl_handler_free(&coil->ctrls);
354 media_entity_cleanup(&coil->subdev.entity);
355 mutex_destroy(&coil->power_lock);
356 return 0;
357}
358
359static const struct i2c_device_id ad5820_id_table[] = {
360 { AD5820_NAME, 0 },
361 { }
362};
363MODULE_DEVICE_TABLE(i2c, ad5820_id_table);
364
365static SIMPLE_DEV_PM_OPS(ad5820_pm, ad5820_suspend, ad5820_resume);
366
367static struct i2c_driver ad5820_i2c_driver = {
368 .driver = {
369 .name = AD5820_NAME,
370 .pm = &ad5820_pm,
371 },
372 .probe = ad5820_probe,
373 .remove = __exit_p(ad5820_remove),
374 .id_table = ad5820_id_table,
375};
376
377module_i2c_driver(ad5820_i2c_driver);
378
379MODULE_AUTHOR("Tuukka Toivonen");
380MODULE_DESCRIPTION("AD5820 camera lens driver");
381MODULE_LICENSE("GPL");