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>
11 * Pavel Machek <pavel@ucw.cz>
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.
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.
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
28 * - fault interrupt handling
30 * - power doesn't need to be ON if all lights are off
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
39 #include <linux/gpio/consumer.h>
40 #include <media/adp1653.h>
41 #include <media/v4l2-device.h>
43 #define TIMEOUT_MAX 820000
44 #define TIMEOUT_STEP 54600
45 #define TIMEOUT_MIN (TIMEOUT_MAX - ADP1653_REG_CONFIG_TMR_SET_MAX \
47 #define TIMEOUT_US_TO_CODE(t) ((TIMEOUT_MAX + (TIMEOUT_STEP / 2) - (t)) \
49 #define TIMEOUT_CODE_TO_US(c) (TIMEOUT_MAX - (c) * TIMEOUT_STEP)
51 /* Write values into ADP1653 registers. */
52 static int adp1653_update_hw(struct adp1653_flash
*flash
)
54 struct i2c_client
*client
= v4l2_get_subdevdata(&flash
->subdev
);
59 out_sel
= ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
60 flash
->indicator_intensity
->val
)
61 << ADP1653_REG_OUT_SEL_ILED_SHIFT
;
63 switch (flash
->led_mode
->val
) {
64 case V4L2_FLASH_LED_MODE_NONE
:
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
;
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
;
80 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_OUT_SEL
, out_sel
);
84 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_CONFIG
, config
);
91 static int adp1653_get_fault(struct adp1653_flash
*flash
)
93 struct i2c_client
*client
= v4l2_get_subdevdata(&flash
->subdev
);
97 fault
= i2c_smbus_read_byte_data(client
, ADP1653_REG_FAULT
);
98 if (IS_ERR_VALUE(fault
))
101 flash
->fault
|= fault
;
107 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_OUT_SEL
, 0);
108 if (IS_ERR_VALUE(rval
))
111 flash
->led_mode
->val
= V4L2_FLASH_LED_MODE_NONE
;
113 rval
= adp1653_update_hw(flash
);
114 if (IS_ERR_VALUE(rval
))
120 static int adp1653_strobe(struct adp1653_flash
*flash
, int enable
)
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
;
128 if (flash
->led_mode
->val
!= V4L2_FLASH_LED_MODE_FLASH
)
132 return i2c_smbus_write_byte_data(client
, ADP1653_REG_OUT_SEL
,
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
);
142 /* Software strobe using i2c */
143 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_SW_STROBE
,
144 ADP1653_REG_SW_STROBE_SW_STROBE
);
147 return i2c_smbus_write_byte_data(client
, ADP1653_REG_SW_STROBE
, 0);
150 /* --------------------------------------------------------------------------
154 static int adp1653_get_ctrl(struct v4l2_ctrl
*ctrl
)
156 struct adp1653_flash
*flash
=
157 container_of(ctrl
->handler
, struct adp1653_flash
, ctrls
);
160 rval
= adp1653_get_fault(flash
);
161 if (IS_ERR_VALUE(rval
))
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
;
180 static int adp1653_set_ctrl(struct v4l2_ctrl
*ctrl
)
182 struct adp1653_flash
*flash
=
183 container_of(ctrl
->handler
, struct adp1653_flash
, ctrls
);
186 rval
= adp1653_get_fault(flash
);
187 if (IS_ERR_VALUE(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
))
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);
204 return adp1653_update_hw(flash
);
207 static const struct v4l2_ctrl_ops adp1653_ctrl_ops
= {
208 .g_volatile_ctrl
= adp1653_get_ctrl
,
209 .s_ctrl
= adp1653_set_ctrl
,
212 static int adp1653_init_controls(struct adp1653_flash
*flash
)
214 struct v4l2_ctrl
*fault
;
216 v4l2_ctrl_handler_init(&flash
->ctrls
, 9);
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
,
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);
261 if (flash
->ctrls
.error
)
262 return flash
->ctrls
.error
;
264 fault
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
266 flash
->subdev
.ctrl_handler
= &flash
->ctrls
;
270 /* --------------------------------------------------------------------------
271 * V4L2 subdev operations
275 adp1653_init_device(struct adp1653_flash
*flash
)
277 struct i2c_client
*client
= v4l2_get_subdevdata(&flash
->subdev
);
280 /* Clear FAULT register by writing zero to OUT_SEL */
281 rval
= i2c_smbus_write_byte_data(client
, ADP1653_REG_OUT_SEL
, 0);
283 dev_err(&client
->dev
, "failed writing fault register\n");
287 mutex_lock(flash
->ctrls
.lock
);
288 /* Reset faults before reading new ones. */
290 rval
= adp1653_get_fault(flash
);
291 mutex_unlock(flash
->ctrls
.lock
);
293 dev_err(&client
->dev
, "faults detected: 0x%1.1x\n", rval
);
297 mutex_lock(flash
->ctrls
.lock
);
298 rval
= adp1653_update_hw(flash
);
299 mutex_unlock(flash
->ctrls
.lock
);
301 dev_err(&client
->dev
,
302 "adp1653_update_hw failed at %s\n", __func__
);
310 __adp1653_set_power(struct adp1653_flash
*flash
, int on
)
314 if (flash
->platform_data
->power
) {
315 ret
= flash
->platform_data
->power(&flash
->subdev
, on
);
319 gpiod_set_value(flash
->platform_data
->enable_gpio
, on
);
321 /* Some delay is apparently required. */
328 ret
= adp1653_init_device(flash
);
332 if (flash
->platform_data
->power
)
333 flash
->platform_data
->power(&flash
->subdev
, 0);
335 gpiod_set_value(flash
->platform_data
->enable_gpio
, 0);
341 adp1653_set_power(struct v4l2_subdev
*subdev
, int on
)
343 struct adp1653_flash
*flash
= to_adp1653_flash(subdev
);
346 mutex_lock(&flash
->power_lock
);
348 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
349 * update the power state.
351 if (flash
->power_count
== !on
) {
352 ret
= __adp1653_set_power(flash
, !!on
);
357 /* Update the power count. */
358 flash
->power_count
+= on
? 1 : -1;
359 WARN_ON(flash
->power_count
< 0);
362 mutex_unlock(&flash
->power_lock
);
366 static int adp1653_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
368 return adp1653_set_power(sd
, 1);
371 static int adp1653_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
373 return adp1653_set_power(sd
, 0);
376 static const struct v4l2_subdev_core_ops adp1653_core_ops
= {
377 .s_power
= adp1653_set_power
,
380 static const struct v4l2_subdev_ops adp1653_ops
= {
381 .core
= &adp1653_core_ops
,
384 static const struct v4l2_subdev_internal_ops adp1653_internal_ops
= {
385 .open
= adp1653_open
,
386 .close
= adp1653_close
,
389 /* --------------------------------------------------------------------------
394 static int adp1653_suspend(struct device
*dev
)
396 struct i2c_client
*client
= to_i2c_client(dev
);
397 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
398 struct adp1653_flash
*flash
= to_adp1653_flash(subdev
);
400 if (!flash
->power_count
)
403 return __adp1653_set_power(flash
, 0);
406 static int adp1653_resume(struct device
*dev
)
408 struct i2c_client
*client
= to_i2c_client(dev
);
409 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
410 struct adp1653_flash
*flash
= to_adp1653_flash(subdev
);
412 if (!flash
->power_count
)
415 return __adp1653_set_power(flash
, 1);
420 #define adp1653_suspend NULL
421 #define adp1653_resume NULL
423 #endif /* CONFIG_PM */
425 static int adp1653_of_init(struct i2c_client
*client
,
426 struct adp1653_flash
*flash
,
427 struct device_node
*node
)
429 struct adp1653_platform_data
*pd
;
430 struct device_node
*child
;
432 pd
= devm_kzalloc(&client
->dev
, sizeof(*pd
), GFP_KERNEL
);
435 flash
->platform_data
= pd
;
437 child
= of_get_child_by_name(node
, "flash");
441 if (of_property_read_u32(child
, "flash-timeout-us",
442 &pd
->max_flash_timeout
))
445 if (of_property_read_u32(child
, "flash-max-microamp",
446 &pd
->max_flash_intensity
))
449 pd
->max_flash_intensity
/= 1000;
451 if (of_property_read_u32(child
, "led-max-microamp",
452 &pd
->max_torch_intensity
))
455 pd
->max_torch_intensity
/= 1000;
458 child
= of_get_child_by_name(node
, "indicator");
462 if (of_property_read_u32(child
, "led-max-microamp",
463 &pd
->max_indicator_intensity
))
468 pd
->enable_gpio
= devm_gpiod_get(&client
->dev
, "enable", GPIOD_OUT_LOW
);
469 if (!pd
->enable_gpio
) {
470 dev_err(&client
->dev
, "Error getting GPIO\n");
476 dev_err(&client
->dev
, "Required property not found\n");
482 static int adp1653_probe(struct i2c_client
*client
,
483 const struct i2c_device_id
*devid
)
485 struct adp1653_flash
*flash
;
488 flash
= devm_kzalloc(&client
->dev
, sizeof(*flash
), GFP_KERNEL
);
492 if (client
->dev
.of_node
) {
493 ret
= adp1653_of_init(client
, flash
, client
->dev
.of_node
);
497 if (!client
->dev
.platform_data
) {
498 dev_err(&client
->dev
,
499 "Neither DT not platform data provided\n");
502 flash
->platform_data
= client
->dev
.platform_data
;
505 mutex_init(&flash
->power_lock
);
507 v4l2_i2c_subdev_init(&flash
->subdev
, client
, &adp1653_ops
);
508 flash
->subdev
.internal_ops
= &adp1653_internal_ops
;
509 flash
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
511 ret
= adp1653_init_controls(flash
);
515 ret
= media_entity_init(&flash
->subdev
.entity
, 0, NULL
, 0);
519 flash
->subdev
.entity
.type
= MEDIA_ENT_T_V4L2_SUBDEV_FLASH
;
524 dev_err(&client
->dev
, "adp1653: failed to register device\n");
525 v4l2_ctrl_handler_free(&flash
->ctrls
);
529 static int adp1653_remove(struct i2c_client
*client
)
531 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
532 struct adp1653_flash
*flash
= to_adp1653_flash(subdev
);
534 v4l2_device_unregister_subdev(&flash
->subdev
);
535 v4l2_ctrl_handler_free(&flash
->ctrls
);
536 media_entity_cleanup(&flash
->subdev
.entity
);
541 static const struct i2c_device_id adp1653_id_table
[] = {
545 MODULE_DEVICE_TABLE(i2c
, adp1653_id_table
);
547 static const struct dev_pm_ops adp1653_pm_ops
= {
548 .suspend
= adp1653_suspend
,
549 .resume
= adp1653_resume
,
552 static struct i2c_driver adp1653_i2c_driver
= {
554 .name
= ADP1653_NAME
,
555 .pm
= &adp1653_pm_ops
,
557 .probe
= adp1653_probe
,
558 .remove
= adp1653_remove
,
559 .id_table
= adp1653_id_table
,
562 module_i2c_driver(adp1653_i2c_driver
);
564 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
565 MODULE_DESCRIPTION("Analog Devices ADP1653 LED flash driver");
566 MODULE_LICENSE("GPL");