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