[media] Add device tree support to adp1653 flash driver
[firefly-linux-kernel-4.4.55.git] / drivers / media / i2c / adp1653.c
1 /*
2  * drivers/media/i2c/adp1653.c
3  *
4  * Copyright (C) 2008--2011 Nokia Corporation
5  *
6  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
7  *
8  * Contributors:
9  *      Sakari Ailus <sakari.ailus@iki.fi>
10  *      Tuukka Toivonen <tuukkat76@gmail.com>
11  *      Pavel Machek <pavel@ucw.cz>
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * version 2 as published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25  * 02110-1301 USA
26  *
27  * TODO:
28  * - fault interrupt handling
29  * - hardware strobe
30  * - power doesn't need to be ON if all lights are off
31  *
32  */
33
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
38 #include <linux/of_gpio.h>
39 #include <linux/gpio.h>
40 #include <media/adp1653.h>
41 #include <media/v4l2-device.h>
42
43 #define TIMEOUT_MAX             820000
44 #define TIMEOUT_STEP            54600
45 #define TIMEOUT_MIN             (TIMEOUT_MAX - ADP1653_REG_CONFIG_TMR_SET_MAX \
46                                  * TIMEOUT_STEP)
47 #define TIMEOUT_US_TO_CODE(t)   ((TIMEOUT_MAX + (TIMEOUT_STEP / 2) - (t)) \
48                                  / TIMEOUT_STEP)
49 #define TIMEOUT_CODE_TO_US(c)   (TIMEOUT_MAX - (c) * TIMEOUT_STEP)
50
51 /* Write values into ADP1653 registers. */
52 static int adp1653_update_hw(struct adp1653_flash *flash)
53 {
54         struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
55         u8 out_sel;
56         u8 config = 0;
57         int rval;
58
59         out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
60                 flash->indicator_intensity->val)
61                 << ADP1653_REG_OUT_SEL_ILED_SHIFT;
62
63         switch (flash->led_mode->val) {
64         case V4L2_FLASH_LED_MODE_NONE:
65                 break;
66         case V4L2_FLASH_LED_MODE_FLASH:
67                 /* Flash mode, light on with strobe, duration from timer */
68                 config = ADP1653_REG_CONFIG_TMR_CFG;
69                 config |= TIMEOUT_US_TO_CODE(flash->flash_timeout->val)
70                           << ADP1653_REG_CONFIG_TMR_SET_SHIFT;
71                 break;
72         case V4L2_FLASH_LED_MODE_TORCH:
73                 /* Torch mode, light immediately on, duration indefinite */
74                 out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
75                         flash->torch_intensity->val)
76                         << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
77                 break;
78         }
79
80         rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
81         if (rval < 0)
82                 return rval;
83
84         rval = i2c_smbus_write_byte_data(client, ADP1653_REG_CONFIG, config);
85         if (rval < 0)
86                 return rval;
87
88         return 0;
89 }
90
91 static int adp1653_get_fault(struct adp1653_flash *flash)
92 {
93         struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
94         int fault;
95         int rval;
96
97         fault = i2c_smbus_read_byte_data(client, ADP1653_REG_FAULT);
98         if (IS_ERR_VALUE(fault))
99                 return fault;
100
101         flash->fault |= fault;
102
103         if (!flash->fault)
104                 return 0;
105
106         /* Clear faults. */
107         rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
108         if (IS_ERR_VALUE(rval))
109                 return rval;
110
111         flash->led_mode->val = V4L2_FLASH_LED_MODE_NONE;
112
113         rval = adp1653_update_hw(flash);
114         if (IS_ERR_VALUE(rval))
115                 return rval;
116
117         return flash->fault;
118 }
119
120 static int adp1653_strobe(struct adp1653_flash *flash, int enable)
121 {
122         struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
123         u8 out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
124                 flash->indicator_intensity->val)
125                 << ADP1653_REG_OUT_SEL_ILED_SHIFT;
126         int rval;
127
128         if (flash->led_mode->val != V4L2_FLASH_LED_MODE_FLASH)
129                 return -EBUSY;
130
131         if (!enable)
132                 return i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL,
133                                                  out_sel);
134
135         out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
136                 flash->flash_intensity->val)
137                 << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
138         rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
139         if (rval)
140                 return rval;
141
142         /* Software strobe using i2c */
143         rval = i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE,
144                 ADP1653_REG_SW_STROBE_SW_STROBE);
145         if (rval)
146                 return rval;
147         return i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE, 0);
148 }
149
150 /* --------------------------------------------------------------------------
151  * V4L2 controls
152  */
153
154 static int adp1653_get_ctrl(struct v4l2_ctrl *ctrl)
155 {
156         struct adp1653_flash *flash =
157                 container_of(ctrl->handler, struct adp1653_flash, ctrls);
158         int rval;
159
160         rval = adp1653_get_fault(flash);
161         if (IS_ERR_VALUE(rval))
162                 return rval;
163
164         ctrl->cur.val = 0;
165
166         if (flash->fault & ADP1653_REG_FAULT_FLT_SCP)
167                 ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
168         if (flash->fault & ADP1653_REG_FAULT_FLT_OT)
169                 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
170         if (flash->fault & ADP1653_REG_FAULT_FLT_TMR)
171                 ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT;
172         if (flash->fault & ADP1653_REG_FAULT_FLT_OV)
173                 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
174
175         flash->fault = 0;
176
177         return 0;
178 }
179
180 static int adp1653_set_ctrl(struct v4l2_ctrl *ctrl)
181 {
182         struct adp1653_flash *flash =
183                 container_of(ctrl->handler, struct adp1653_flash, ctrls);
184         int rval;
185
186         rval = adp1653_get_fault(flash);
187         if (IS_ERR_VALUE(rval))
188                 return rval;
189         if ((rval & (ADP1653_REG_FAULT_FLT_SCP |
190                      ADP1653_REG_FAULT_FLT_OT |
191                      ADP1653_REG_FAULT_FLT_OV)) &&
192             (ctrl->id == V4L2_CID_FLASH_STROBE ||
193              ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
194              ctrl->id == V4L2_CID_FLASH_LED_MODE))
195                 return -EBUSY;
196
197         switch (ctrl->id) {
198         case V4L2_CID_FLASH_STROBE:
199                 return adp1653_strobe(flash, 1);
200         case V4L2_CID_FLASH_STROBE_STOP:
201                 return adp1653_strobe(flash, 0);
202         }
203
204         return adp1653_update_hw(flash);
205 }
206
207 static const struct v4l2_ctrl_ops adp1653_ctrl_ops = {
208         .g_volatile_ctrl = adp1653_get_ctrl,
209         .s_ctrl = adp1653_set_ctrl,
210 };
211
212 static int adp1653_init_controls(struct adp1653_flash *flash)
213 {
214         struct v4l2_ctrl *fault;
215
216         v4l2_ctrl_handler_init(&flash->ctrls, 9);
217
218         flash->led_mode =
219                 v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
220                                        V4L2_CID_FLASH_LED_MODE,
221                                        V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
222         v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
223                                V4L2_CID_FLASH_STROBE_SOURCE,
224                                V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
225         v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
226                           V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
227         v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
228                           V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
229         flash->flash_timeout =
230                 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
231                                   V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
232                                   flash->platform_data->max_flash_timeout,
233                                   TIMEOUT_STEP,
234                                   flash->platform_data->max_flash_timeout);
235         flash->flash_intensity =
236                 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
237                                   V4L2_CID_FLASH_INTENSITY,
238                                   ADP1653_FLASH_INTENSITY_MIN,
239                                   flash->platform_data->max_flash_intensity,
240                                   1, flash->platform_data->max_flash_intensity);
241         flash->torch_intensity =
242                 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
243                                   V4L2_CID_FLASH_TORCH_INTENSITY,
244                                   ADP1653_TORCH_INTENSITY_MIN,
245                                   flash->platform_data->max_torch_intensity,
246                                   ADP1653_FLASH_INTENSITY_STEP,
247                                   flash->platform_data->max_torch_intensity);
248         flash->indicator_intensity =
249                 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
250                                   V4L2_CID_FLASH_INDICATOR_INTENSITY,
251                                   ADP1653_INDICATOR_INTENSITY_MIN,
252                                   flash->platform_data->max_indicator_intensity,
253                                   ADP1653_INDICATOR_INTENSITY_STEP,
254                                   ADP1653_INDICATOR_INTENSITY_MIN);
255         fault = v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
256                                   V4L2_CID_FLASH_FAULT, 0,
257                                   V4L2_FLASH_FAULT_OVER_VOLTAGE
258                                   | V4L2_FLASH_FAULT_OVER_TEMPERATURE
259                                   | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
260
261         if (flash->ctrls.error)
262                 return flash->ctrls.error;
263
264         fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
265
266         flash->subdev.ctrl_handler = &flash->ctrls;
267         return 0;
268 }
269
270 /* --------------------------------------------------------------------------
271  * V4L2 subdev operations
272  */
273
274 static int
275 adp1653_init_device(struct adp1653_flash *flash)
276 {
277         struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
278         int rval;
279
280         /* Clear FAULT register by writing zero to OUT_SEL */
281         rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
282         if (rval < 0) {
283                 dev_err(&client->dev, "failed writing fault register\n");
284                 return -EIO;
285         }
286
287         mutex_lock(flash->ctrls.lock);
288         /* Reset faults before reading new ones. */
289         flash->fault = 0;
290         rval = adp1653_get_fault(flash);
291         mutex_unlock(flash->ctrls.lock);
292         if (rval > 0) {
293                 dev_err(&client->dev, "faults detected: 0x%1.1x\n", rval);
294                 return -EIO;
295         }
296
297         mutex_lock(flash->ctrls.lock);
298         rval = adp1653_update_hw(flash);
299         mutex_unlock(flash->ctrls.lock);
300         if (rval) {
301                 dev_err(&client->dev,
302                         "adp1653_update_hw failed at %s\n", __func__);
303                 return -EIO;
304         }
305
306         return 0;
307 }
308
309 static int
310 __adp1653_set_power(struct adp1653_flash *flash, int on)
311 {
312         int ret = 0;
313
314         if (flash->platform_data->power) {
315                 ret = flash->platform_data->power(&flash->subdev, on);
316         } else {
317                 gpio_set_value(flash->platform_data->power_gpio, on);
318                 if (on)
319                         /* Some delay is apparently required. */
320                         udelay(20);
321         }
322
323         if (ret < 0)
324                 return ret;
325
326         if (!on)
327                 return 0;
328
329         ret = adp1653_init_device(flash);
330         if (ret >= 0)
331                 return ret;
332
333         if (flash->platform_data->power)
334                 flash->platform_data->power(&flash->subdev, 0);
335         else
336                 gpio_set_value(flash->platform_data->power_gpio, 0);
337
338         return ret;
339 }
340
341 static int
342 adp1653_set_power(struct v4l2_subdev *subdev, int on)
343 {
344         struct adp1653_flash *flash = to_adp1653_flash(subdev);
345         int ret = 0;
346
347         mutex_lock(&flash->power_lock);
348
349         /* If the power count is modified from 0 to != 0 or from != 0 to 0,
350          * update the power state.
351          */
352         if (flash->power_count == !on) {
353                 ret = __adp1653_set_power(flash, !!on);
354                 if (ret < 0)
355                         goto done;
356         }
357
358         /* Update the power count. */
359         flash->power_count += on ? 1 : -1;
360         WARN_ON(flash->power_count < 0);
361
362 done:
363         mutex_unlock(&flash->power_lock);
364         return ret;
365 }
366
367 static int adp1653_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
368 {
369         return adp1653_set_power(sd, 1);
370 }
371
372 static int adp1653_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
373 {
374         return adp1653_set_power(sd, 0);
375 }
376
377 static const struct v4l2_subdev_core_ops adp1653_core_ops = {
378         .s_power = adp1653_set_power,
379 };
380
381 static const struct v4l2_subdev_ops adp1653_ops = {
382         .core = &adp1653_core_ops,
383 };
384
385 static const struct v4l2_subdev_internal_ops adp1653_internal_ops = {
386         .open = adp1653_open,
387         .close = adp1653_close,
388 };
389
390 /* --------------------------------------------------------------------------
391  * I2C driver
392  */
393 #ifdef CONFIG_PM
394
395 static int adp1653_suspend(struct device *dev)
396 {
397         struct i2c_client *client = to_i2c_client(dev);
398         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
399         struct adp1653_flash *flash = to_adp1653_flash(subdev);
400
401         if (!flash->power_count)
402                 return 0;
403
404         return __adp1653_set_power(flash, 0);
405 }
406
407 static int adp1653_resume(struct device *dev)
408 {
409         struct i2c_client *client = to_i2c_client(dev);
410         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
411         struct adp1653_flash *flash = to_adp1653_flash(subdev);
412
413         if (!flash->power_count)
414                 return 0;
415
416         return __adp1653_set_power(flash, 1);
417 }
418
419 #else
420
421 #define adp1653_suspend NULL
422 #define adp1653_resume  NULL
423
424 #endif /* CONFIG_PM */
425
426 static int adp1653_of_init(struct i2c_client *client,
427                            struct adp1653_flash *flash,
428                            struct device_node *node)
429 {
430         u32 val;
431         struct adp1653_platform_data *pd;
432         enum of_gpio_flags flags;
433         int gpio;
434         struct device_node *child;
435
436         if (!node)
437                 return -EINVAL;
438
439         pd = devm_kzalloc(&client->dev, sizeof(*pd), GFP_KERNEL);
440         if (!pd)
441                 return -ENOMEM;
442         flash->platform_data = pd;
443
444         child = of_get_child_by_name(node, "flash");
445         if (!child)
446                 return -EINVAL;
447         if (of_property_read_u32(child, "flash-timeout-microsec", &val))
448                 return -EINVAL;
449
450         pd->max_flash_timeout = val;
451         if (of_property_read_u32(child, "flash-max-microamp", &val))
452                 return -EINVAL;
453         pd->max_flash_intensity = val/1000;
454
455         if (of_property_read_u32(child, "max-microamp", &val))
456                 return -EINVAL;
457         pd->max_torch_intensity = val/1000;
458
459         child = of_get_child_by_name(node, "indicator");
460         if (!child)
461                 return -EINVAL;
462         if (of_property_read_u32(child, "max-microamp", &val))
463                 return -EINVAL;
464         pd->max_indicator_intensity = val;
465
466         if (!of_find_property(node, "gpios", NULL)) {
467                 dev_err(&client->dev, "No gpio node\n");
468                 return -EINVAL;
469         }
470
471         pd->power_gpio = of_get_gpio_flags(node, 0, &flags);
472         if (pd->power_gpio < 0) {
473                 dev_err(&client->dev, "Error getting GPIO\n");
474                 return -EINVAL;
475         }
476
477         return 0;
478 }
479
480
481 static int adp1653_probe(struct i2c_client *client,
482                          const struct i2c_device_id *devid)
483 {
484         struct adp1653_flash *flash;
485         int ret;
486
487         flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
488         if (flash == NULL)
489                 return -ENOMEM;
490
491         flash->platform_data = client->dev.platform_data;
492         if (!flash->platform_data) {
493                 ret = adp1653_of_init(client, flash, client->dev.of_node);
494                 if (ret)
495                         return ret;
496         }
497
498         mutex_init(&flash->power_lock);
499
500         v4l2_i2c_subdev_init(&flash->subdev, client, &adp1653_ops);
501         flash->subdev.internal_ops = &adp1653_internal_ops;
502         flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
503
504         ret = adp1653_init_controls(flash);
505         if (ret)
506                 goto free_and_quit;
507
508         ret = media_entity_init(&flash->subdev.entity, 0, NULL, 0);
509         if (ret < 0)
510                 goto free_and_quit;
511
512         flash->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
513         return 0;
514
515 free_and_quit:
516         dev_err(&client->dev, "adp1653: failed to register device\n");
517         v4l2_ctrl_handler_free(&flash->ctrls);
518         return ret;
519 }
520
521 static int adp1653_remove(struct i2c_client *client)
522 {
523         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
524         struct adp1653_flash *flash = to_adp1653_flash(subdev);
525
526         v4l2_device_unregister_subdev(&flash->subdev);
527         v4l2_ctrl_handler_free(&flash->ctrls);
528         media_entity_cleanup(&flash->subdev.entity);
529
530         return 0;
531 }
532
533 static const struct i2c_device_id adp1653_id_table[] = {
534         { ADP1653_NAME, 0 },
535         { }
536 };
537 MODULE_DEVICE_TABLE(i2c, adp1653_id_table);
538
539 static const struct dev_pm_ops adp1653_pm_ops = {
540         .suspend        = adp1653_suspend,
541         .resume         = adp1653_resume,
542 };
543
544 static struct i2c_driver adp1653_i2c_driver = {
545         .driver         = {
546                 .name   = ADP1653_NAME,
547                 .pm     = &adp1653_pm_ops,
548         },
549         .probe          = adp1653_probe,
550         .remove         = adp1653_remove,
551         .id_table       = adp1653_id_table,
552 };
553
554 module_i2c_driver(adp1653_i2c_driver);
555
556 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
557 MODULE_DESCRIPTION("Analog Devices ADP1653 LED flash driver");
558 MODULE_LICENSE("GPL");