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