2 * drivers/media/i2c/adp1653.c
4 * Copyright (C) 2008--2011 Nokia Corporation
6 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
9 * Sakari Ailus <sakari.ailus@iki.fi>
10 * Tuukka Toivonen <tuukkat76@gmail.com>
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.
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.
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
27 * - fault interrupt handling
29 * - power doesn't need to be ON if all lights are off
33 #include <linux/delay.h>
34 #include <linux/module.h>
35 #include <linux/i2c.h>
36 #include <linux/slab.h>
37 #include <media/adp1653.h>
38 #include <media/v4l2-device.h>
40 #define TIMEOUT_MAX 820000
41 #define TIMEOUT_STEP 54600
42 #define TIMEOUT_MIN (TIMEOUT_MAX - ADP1653_REG_CONFIG_TMR_SET_MAX \
44 #define TIMEOUT_US_TO_CODE(t) ((TIMEOUT_MAX + (TIMEOUT_STEP / 2) - (t)) \
46 #define TIMEOUT_CODE_TO_US(c) (TIMEOUT_MAX - (c) * TIMEOUT_STEP)
48 /* Write values into ADP1653 registers. */
49 static int adp1653_update_hw(struct adp1653_flash
*flash
)
51 struct i2c_client
*client
= v4l2_get_subdevdata(&flash
->subdev
);
56 out_sel
= ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
57 flash
->indicator_intensity
->val
)
58 << ADP1653_REG_OUT_SEL_ILED_SHIFT
;
60 switch (flash
->led_mode
->val
) {
61 case V4L2_FLASH_LED_MODE_NONE
:
63 case V4L2_FLASH_LED_MODE_FLASH
:
64 /* Flash mode, light on with strobe, duration from timer */
65 config
= ADP1653_REG_CONFIG_TMR_CFG
;
66 config
|= TIMEOUT_US_TO_CODE(flash
->flash_timeout
->val
)
67 << ADP1653_REG_CONFIG_TMR_SET_SHIFT
;
69 case V4L2_FLASH_LED_MODE_TORCH
:
70 /* Torch mode, light immediately on, duration indefinite */
71 out_sel
|= ADP1653_FLASH_INTENSITY_mA_TO_REG(
72 flash
->torch_intensity
->val
)
73 << ADP1653_REG_OUT_SEL_HPLED_SHIFT
;
77 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_OUT_SEL
, out_sel
);
81 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_CONFIG
, config
);
88 static int adp1653_get_fault(struct adp1653_flash
*flash
)
90 struct i2c_client
*client
= v4l2_get_subdevdata(&flash
->subdev
);
94 fault
= i2c_smbus_read_byte_data(client
, ADP1653_REG_FAULT
);
95 if (IS_ERR_VALUE(fault
))
98 flash
->fault
|= fault
;
104 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_OUT_SEL
, 0);
105 if (IS_ERR_VALUE(rval
))
108 flash
->led_mode
->val
= V4L2_FLASH_LED_MODE_NONE
;
110 rval
= adp1653_update_hw(flash
);
111 if (IS_ERR_VALUE(rval
))
117 static int adp1653_strobe(struct adp1653_flash
*flash
, int enable
)
119 struct i2c_client
*client
= v4l2_get_subdevdata(&flash
->subdev
);
120 u8 out_sel
= ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
121 flash
->indicator_intensity
->val
)
122 << ADP1653_REG_OUT_SEL_ILED_SHIFT
;
125 if (flash
->led_mode
->val
!= V4L2_FLASH_LED_MODE_FLASH
)
129 return i2c_smbus_write_byte_data(client
, ADP1653_REG_OUT_SEL
,
132 out_sel
|= ADP1653_FLASH_INTENSITY_mA_TO_REG(
133 flash
->flash_intensity
->val
)
134 << ADP1653_REG_OUT_SEL_HPLED_SHIFT
;
135 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_OUT_SEL
, out_sel
);
139 /* Software strobe using i2c */
140 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_SW_STROBE
,
141 ADP1653_REG_SW_STROBE_SW_STROBE
);
144 return i2c_smbus_write_byte_data(client
, ADP1653_REG_SW_STROBE
, 0);
147 /* --------------------------------------------------------------------------
151 static int adp1653_get_ctrl(struct v4l2_ctrl
*ctrl
)
153 struct adp1653_flash
*flash
=
154 container_of(ctrl
->handler
, struct adp1653_flash
, ctrls
);
157 rval
= adp1653_get_fault(flash
);
158 if (IS_ERR_VALUE(rval
))
163 if (flash
->fault
& ADP1653_REG_FAULT_FLT_SCP
)
164 ctrl
->cur
.val
|= V4L2_FLASH_FAULT_SHORT_CIRCUIT
;
165 if (flash
->fault
& ADP1653_REG_FAULT_FLT_OT
)
166 ctrl
->cur
.val
|= V4L2_FLASH_FAULT_OVER_TEMPERATURE
;
167 if (flash
->fault
& ADP1653_REG_FAULT_FLT_TMR
)
168 ctrl
->cur
.val
|= V4L2_FLASH_FAULT_TIMEOUT
;
169 if (flash
->fault
& ADP1653_REG_FAULT_FLT_OV
)
170 ctrl
->cur
.val
|= V4L2_FLASH_FAULT_OVER_VOLTAGE
;
177 static int adp1653_set_ctrl(struct v4l2_ctrl
*ctrl
)
179 struct adp1653_flash
*flash
=
180 container_of(ctrl
->handler
, struct adp1653_flash
, ctrls
);
183 rval
= adp1653_get_fault(flash
);
184 if (IS_ERR_VALUE(rval
))
186 if ((rval
& (ADP1653_REG_FAULT_FLT_SCP
|
187 ADP1653_REG_FAULT_FLT_OT
|
188 ADP1653_REG_FAULT_FLT_OV
)) &&
189 (ctrl
->id
== V4L2_CID_FLASH_STROBE
||
190 ctrl
->id
== V4L2_CID_FLASH_TORCH_INTENSITY
||
191 ctrl
->id
== V4L2_CID_FLASH_LED_MODE
))
195 case V4L2_CID_FLASH_STROBE
:
196 return adp1653_strobe(flash
, 1);
197 case V4L2_CID_FLASH_STROBE_STOP
:
198 return adp1653_strobe(flash
, 0);
201 return adp1653_update_hw(flash
);
204 static const struct v4l2_ctrl_ops adp1653_ctrl_ops
= {
205 .g_volatile_ctrl
= adp1653_get_ctrl
,
206 .s_ctrl
= adp1653_set_ctrl
,
209 static int adp1653_init_controls(struct adp1653_flash
*flash
)
211 struct v4l2_ctrl
*fault
;
213 v4l2_ctrl_handler_init(&flash
->ctrls
, 9);
216 v4l2_ctrl_new_std_menu(&flash
->ctrls
, &adp1653_ctrl_ops
,
217 V4L2_CID_FLASH_LED_MODE
,
218 V4L2_FLASH_LED_MODE_TORCH
, ~0x7, 0);
219 v4l2_ctrl_new_std_menu(&flash
->ctrls
, &adp1653_ctrl_ops
,
220 V4L2_CID_FLASH_STROBE_SOURCE
,
221 V4L2_FLASH_STROBE_SOURCE_SOFTWARE
, ~0x1, 0);
222 v4l2_ctrl_new_std(&flash
->ctrls
, &adp1653_ctrl_ops
,
223 V4L2_CID_FLASH_STROBE
, 0, 0, 0, 0);
224 v4l2_ctrl_new_std(&flash
->ctrls
, &adp1653_ctrl_ops
,
225 V4L2_CID_FLASH_STROBE_STOP
, 0, 0, 0, 0);
226 flash
->flash_timeout
=
227 v4l2_ctrl_new_std(&flash
->ctrls
, &adp1653_ctrl_ops
,
228 V4L2_CID_FLASH_TIMEOUT
, TIMEOUT_MIN
,
229 flash
->platform_data
->max_flash_timeout
,
231 flash
->platform_data
->max_flash_timeout
);
232 flash
->flash_intensity
=
233 v4l2_ctrl_new_std(&flash
->ctrls
, &adp1653_ctrl_ops
,
234 V4L2_CID_FLASH_INTENSITY
,
235 ADP1653_FLASH_INTENSITY_MIN
,
236 flash
->platform_data
->max_flash_intensity
,
237 1, flash
->platform_data
->max_flash_intensity
);
238 flash
->torch_intensity
=
239 v4l2_ctrl_new_std(&flash
->ctrls
, &adp1653_ctrl_ops
,
240 V4L2_CID_FLASH_TORCH_INTENSITY
,
241 ADP1653_TORCH_INTENSITY_MIN
,
242 flash
->platform_data
->max_torch_intensity
,
243 ADP1653_FLASH_INTENSITY_STEP
,
244 flash
->platform_data
->max_torch_intensity
);
245 flash
->indicator_intensity
=
246 v4l2_ctrl_new_std(&flash
->ctrls
, &adp1653_ctrl_ops
,
247 V4L2_CID_FLASH_INDICATOR_INTENSITY
,
248 ADP1653_INDICATOR_INTENSITY_MIN
,
249 flash
->platform_data
->max_indicator_intensity
,
250 ADP1653_INDICATOR_INTENSITY_STEP
,
251 ADP1653_INDICATOR_INTENSITY_MIN
);
252 fault
= v4l2_ctrl_new_std(&flash
->ctrls
, &adp1653_ctrl_ops
,
253 V4L2_CID_FLASH_FAULT
, 0,
254 V4L2_FLASH_FAULT_OVER_VOLTAGE
255 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
256 | V4L2_FLASH_FAULT_SHORT_CIRCUIT
, 0, 0);
258 if (flash
->ctrls
.error
)
259 return flash
->ctrls
.error
;
261 fault
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
263 flash
->subdev
.ctrl_handler
= &flash
->ctrls
;
267 /* --------------------------------------------------------------------------
268 * V4L2 subdev operations
272 adp1653_init_device(struct adp1653_flash
*flash
)
274 struct i2c_client
*client
= v4l2_get_subdevdata(&flash
->subdev
);
277 /* Clear FAULT register by writing zero to OUT_SEL */
278 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_OUT_SEL
, 0);
280 dev_err(&client
->dev
, "failed writing fault register\n");
284 mutex_lock(flash
->ctrls
.lock
);
285 /* Reset faults before reading new ones. */
287 rval
= adp1653_get_fault(flash
);
288 mutex_unlock(flash
->ctrls
.lock
);
290 dev_err(&client
->dev
, "faults detected: 0x%1.1x\n", rval
);
294 mutex_lock(flash
->ctrls
.lock
);
295 rval
= adp1653_update_hw(flash
);
296 mutex_unlock(flash
->ctrls
.lock
);
298 dev_err(&client
->dev
,
299 "adp1653_update_hw failed at %s\n", __func__
);
307 __adp1653_set_power(struct adp1653_flash
*flash
, int on
)
311 ret
= flash
->platform_data
->power(&flash
->subdev
, on
);
318 ret
= adp1653_init_device(flash
);
320 flash
->platform_data
->power(&flash
->subdev
, 0);
326 adp1653_set_power(struct v4l2_subdev
*subdev
, int on
)
328 struct adp1653_flash
*flash
= to_adp1653_flash(subdev
);
331 mutex_lock(&flash
->power_lock
);
333 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
334 * update the power state.
336 if (flash
->power_count
== !on
) {
337 ret
= __adp1653_set_power(flash
, !!on
);
342 /* Update the power count. */
343 flash
->power_count
+= on
? 1 : -1;
344 WARN_ON(flash
->power_count
< 0);
347 mutex_unlock(&flash
->power_lock
);
351 static int adp1653_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
353 return adp1653_set_power(sd
, 1);
356 static int adp1653_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
358 return adp1653_set_power(sd
, 0);
361 static const struct v4l2_subdev_core_ops adp1653_core_ops
= {
362 .s_power
= adp1653_set_power
,
365 static const struct v4l2_subdev_ops adp1653_ops
= {
366 .core
= &adp1653_core_ops
,
369 static const struct v4l2_subdev_internal_ops adp1653_internal_ops
= {
370 .open
= adp1653_open
,
371 .close
= adp1653_close
,
374 /* --------------------------------------------------------------------------
379 static int adp1653_suspend(struct device
*dev
)
381 struct i2c_client
*client
= to_i2c_client(dev
);
382 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
383 struct adp1653_flash
*flash
= to_adp1653_flash(subdev
);
385 if (!flash
->power_count
)
388 return __adp1653_set_power(flash
, 0);
391 static int adp1653_resume(struct device
*dev
)
393 struct i2c_client
*client
= to_i2c_client(dev
);
394 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
395 struct adp1653_flash
*flash
= to_adp1653_flash(subdev
);
397 if (!flash
->power_count
)
400 return __adp1653_set_power(flash
, 1);
405 #define adp1653_suspend NULL
406 #define adp1653_resume NULL
408 #endif /* CONFIG_PM */
410 static int adp1653_probe(struct i2c_client
*client
,
411 const struct i2c_device_id
*devid
)
413 struct adp1653_flash
*flash
;
416 /* we couldn't work without platform data */
417 if (client
->dev
.platform_data
== NULL
)
420 flash
= kzalloc(sizeof(*flash
), GFP_KERNEL
);
424 flash
->platform_data
= client
->dev
.platform_data
;
426 mutex_init(&flash
->power_lock
);
428 v4l2_i2c_subdev_init(&flash
->subdev
, client
, &adp1653_ops
);
429 flash
->subdev
.internal_ops
= &adp1653_internal_ops
;
430 flash
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
432 ret
= adp1653_init_controls(flash
);
436 ret
= media_entity_init(&flash
->subdev
.entity
, 0, NULL
, 0);
440 flash
->subdev
.entity
.type
= MEDIA_ENT_T_V4L2_SUBDEV_FLASH
;
445 v4l2_ctrl_handler_free(&flash
->ctrls
);
450 static int adp1653_remove(struct i2c_client
*client
)
452 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
453 struct adp1653_flash
*flash
= to_adp1653_flash(subdev
);
455 v4l2_device_unregister_subdev(&flash
->subdev
);
456 v4l2_ctrl_handler_free(&flash
->ctrls
);
457 media_entity_cleanup(&flash
->subdev
.entity
);
462 static const struct i2c_device_id adp1653_id_table
[] = {
466 MODULE_DEVICE_TABLE(i2c
, adp1653_id_table
);
468 static struct dev_pm_ops adp1653_pm_ops
= {
469 .suspend
= adp1653_suspend
,
470 .resume
= adp1653_resume
,
473 static struct i2c_driver adp1653_i2c_driver
= {
475 .name
= ADP1653_NAME
,
476 .pm
= &adp1653_pm_ops
,
478 .probe
= adp1653_probe
,
479 .remove
= adp1653_remove
,
480 .id_table
= adp1653_id_table
,
483 module_i2c_driver(adp1653_i2c_driver
);
485 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
486 MODULE_DESCRIPTION("Analog Devices ADP1653 LED flash driver");
487 MODULE_LICENSE("GPL");