2 * drivers/media/i2c/lm3560.c
3 * General device driver for TI lm3560, FLASH LED Driver
5 * Copyright (C) 2013 Texas Instruments
7 * Contact: Daniel Jeong <gshark.jeong@gmail.com>
8 * Ldd-Mlp <ldd-mlp@list.ti.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
20 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/slab.h>
24 #include <linux/mutex.h>
25 #include <linux/regmap.h>
26 #include <linux/videodev2.h>
27 #include <media/lm3560.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
31 /* registers definitions */
32 #define REG_ENABLE 0x10
33 #define REG_TORCH_BR 0xa0
34 #define REG_FLASH_BR 0xb0
35 #define REG_FLASH_TOUT 0xc0
37 #define REG_CONFIG1 0xe0
40 #define FAULT_TIMEOUT (1<<0)
41 #define FAULT_OVERTEMP (1<<1)
42 #define FAULT_SHORT_CIRCUIT (1<<2)
53 * @pdata: platform data
54 * @regmap: reg. map for i2c
55 * @lock: muxtex for serial access.
56 * @led_mode: V4L2 LED mode
57 * @ctrls_led: V4L2 contols
58 * @subdev_led: V4L2 subdev
62 struct lm3560_platform_data
*pdata
;
63 struct regmap
*regmap
;
66 enum v4l2_flash_led_mode led_mode
;
67 struct v4l2_ctrl_handler ctrls_led
[LM3560_LED_MAX
];
68 struct v4l2_subdev subdev_led
[LM3560_LED_MAX
];
71 #define to_lm3560_flash(_ctrl, _no) \
72 container_of(_ctrl->handler, struct lm3560_flash, ctrls_led[_no])
74 /* enable mode control */
75 static int lm3560_mode_ctrl(struct lm3560_flash
*flash
)
79 switch (flash
->led_mode
) {
80 case V4L2_FLASH_LED_MODE_NONE
:
81 rval
= regmap_update_bits(flash
->regmap
,
82 REG_ENABLE
, 0x03, MODE_SHDN
);
84 case V4L2_FLASH_LED_MODE_TORCH
:
85 rval
= regmap_update_bits(flash
->regmap
,
86 REG_ENABLE
, 0x03, MODE_TORCH
);
88 case V4L2_FLASH_LED_MODE_FLASH
:
89 rval
= regmap_update_bits(flash
->regmap
,
90 REG_ENABLE
, 0x03, MODE_FLASH
);
96 /* led1/2 enable/disable */
97 static int lm3560_enable_ctrl(struct lm3560_flash
*flash
,
98 enum lm3560_led_id led_no
, bool on
)
102 if (led_no
== LM3560_LED0
) {
104 rval
= regmap_update_bits(flash
->regmap
,
105 REG_ENABLE
, 0x08, 0x08);
107 rval
= regmap_update_bits(flash
->regmap
,
108 REG_ENABLE
, 0x08, 0x00);
111 rval
= regmap_update_bits(flash
->regmap
,
112 REG_ENABLE
, 0x10, 0x10);
114 rval
= regmap_update_bits(flash
->regmap
,
115 REG_ENABLE
, 0x10, 0x00);
120 /* torch1/2 brightness control */
121 static int lm3560_torch_brt_ctrl(struct lm3560_flash
*flash
,
122 enum lm3560_led_id led_no
, unsigned int brt
)
127 if (brt
< LM3560_TORCH_BRT_MIN
)
128 return lm3560_enable_ctrl(flash
, led_no
, false);
130 rval
= lm3560_enable_ctrl(flash
, led_no
, true);
132 br_bits
= LM3560_TORCH_BRT_uA_TO_REG(brt
);
133 if (led_no
== LM3560_LED0
)
134 rval
= regmap_update_bits(flash
->regmap
,
135 REG_TORCH_BR
, 0x07, br_bits
);
137 rval
= regmap_update_bits(flash
->regmap
,
138 REG_TORCH_BR
, 0x38, br_bits
<< 3);
143 /* flash1/2 brightness control */
144 static int lm3560_flash_brt_ctrl(struct lm3560_flash
*flash
,
145 enum lm3560_led_id led_no
, unsigned int brt
)
150 if (brt
< LM3560_FLASH_BRT_MIN
)
151 return lm3560_enable_ctrl(flash
, led_no
, false);
153 rval
= lm3560_enable_ctrl(flash
, led_no
, true);
155 br_bits
= LM3560_FLASH_BRT_uA_TO_REG(brt
);
156 if (led_no
== LM3560_LED0
)
157 rval
= regmap_update_bits(flash
->regmap
,
158 REG_FLASH_BR
, 0x0f, br_bits
);
160 rval
= regmap_update_bits(flash
->regmap
,
161 REG_FLASH_BR
, 0xf0, br_bits
<< 4);
167 static int lm3560_get_ctrl(struct v4l2_ctrl
*ctrl
, enum lm3560_led_id led_no
)
169 struct lm3560_flash
*flash
= to_lm3560_flash(ctrl
, led_no
);
172 mutex_lock(&flash
->lock
);
174 if (ctrl
->id
== V4L2_CID_FLASH_FAULT
) {
176 unsigned int reg_val
;
177 rval
= regmap_read(flash
->regmap
, REG_FLAG
, ®_val
);
180 if (reg_val
& FAULT_SHORT_CIRCUIT
)
181 fault
|= V4L2_FLASH_FAULT_SHORT_CIRCUIT
;
182 if (reg_val
& FAULT_OVERTEMP
)
183 fault
|= V4L2_FLASH_FAULT_OVER_TEMPERATURE
;
184 if (reg_val
& FAULT_TIMEOUT
)
185 fault
|= V4L2_FLASH_FAULT_TIMEOUT
;
186 ctrl
->cur
.val
= fault
;
190 mutex_unlock(&flash
->lock
);
194 static int lm3560_set_ctrl(struct v4l2_ctrl
*ctrl
, enum lm3560_led_id led_no
)
196 struct lm3560_flash
*flash
= to_lm3560_flash(ctrl
, led_no
);
200 mutex_lock(&flash
->lock
);
203 case V4L2_CID_FLASH_LED_MODE
:
204 flash
->led_mode
= ctrl
->val
;
205 if (flash
->led_mode
!= V4L2_FLASH_LED_MODE_FLASH
)
206 rval
= lm3560_mode_ctrl(flash
);
209 case V4L2_CID_FLASH_STROBE_SOURCE
:
210 rval
= regmap_update_bits(flash
->regmap
,
211 REG_CONFIG1
, 0x04, (ctrl
->val
) << 2);
216 case V4L2_CID_FLASH_STROBE
:
217 if (flash
->led_mode
!= V4L2_FLASH_LED_MODE_FLASH
) {
221 flash
->led_mode
= V4L2_FLASH_LED_MODE_FLASH
;
222 rval
= lm3560_mode_ctrl(flash
);
225 case V4L2_CID_FLASH_STROBE_STOP
:
226 if (flash
->led_mode
!= V4L2_FLASH_LED_MODE_FLASH
) {
230 flash
->led_mode
= V4L2_FLASH_LED_MODE_NONE
;
231 rval
= lm3560_mode_ctrl(flash
);
234 case V4L2_CID_FLASH_TIMEOUT
:
235 tout_bits
= LM3560_FLASH_TOUT_ms_TO_REG(ctrl
->val
);
236 rval
= regmap_update_bits(flash
->regmap
,
237 REG_FLASH_TOUT
, 0x1f, tout_bits
);
240 case V4L2_CID_FLASH_INTENSITY
:
241 rval
= lm3560_flash_brt_ctrl(flash
, led_no
, ctrl
->val
);
244 case V4L2_CID_FLASH_TORCH_INTENSITY
:
245 rval
= lm3560_torch_brt_ctrl(flash
, led_no
, ctrl
->val
);
250 mutex_unlock(&flash
->lock
);
254 static int lm3560_led1_get_ctrl(struct v4l2_ctrl
*ctrl
)
256 return lm3560_get_ctrl(ctrl
, LM3560_LED1
);
259 static int lm3560_led1_set_ctrl(struct v4l2_ctrl
*ctrl
)
261 return lm3560_set_ctrl(ctrl
, LM3560_LED1
);
264 static int lm3560_led0_get_ctrl(struct v4l2_ctrl
*ctrl
)
266 return lm3560_get_ctrl(ctrl
, LM3560_LED0
);
269 static int lm3560_led0_set_ctrl(struct v4l2_ctrl
*ctrl
)
271 return lm3560_set_ctrl(ctrl
, LM3560_LED0
);
274 static const struct v4l2_ctrl_ops lm3560_led_ctrl_ops
[LM3560_LED_MAX
] = {
276 .g_volatile_ctrl
= lm3560_led0_get_ctrl
,
277 .s_ctrl
= lm3560_led0_set_ctrl
,
280 .g_volatile_ctrl
= lm3560_led1_get_ctrl
,
281 .s_ctrl
= lm3560_led1_set_ctrl
,
285 static int lm3560_init_controls(struct lm3560_flash
*flash
,
286 enum lm3560_led_id led_no
)
288 struct v4l2_ctrl
*fault
;
289 u32 max_flash_brt
= flash
->pdata
->max_flash_brt
[led_no
];
290 u32 max_torch_brt
= flash
->pdata
->max_torch_brt
[led_no
];
291 struct v4l2_ctrl_handler
*hdl
= &flash
->ctrls_led
[led_no
];
292 const struct v4l2_ctrl_ops
*ops
= &lm3560_led_ctrl_ops
[led_no
];
294 v4l2_ctrl_handler_init(hdl
, 8);
297 v4l2_ctrl_new_std_menu(hdl
, ops
, V4L2_CID_FLASH_LED_MODE
,
298 V4L2_FLASH_LED_MODE_TORCH
, ~0x7,
299 V4L2_FLASH_LED_MODE_NONE
);
300 flash
->led_mode
= V4L2_FLASH_LED_MODE_NONE
;
303 v4l2_ctrl_new_std_menu(hdl
, ops
, V4L2_CID_FLASH_STROBE_SOURCE
,
304 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE
);
307 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_STROBE
, 0, 0, 0, 0);
309 /* flash strobe stop */
310 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_STROBE_STOP
, 0, 0, 0, 0);
312 /* flash strobe timeout */
313 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_TIMEOUT
,
314 LM3560_FLASH_TOUT_MIN
,
315 flash
->pdata
->max_flash_timeout
,
316 LM3560_FLASH_TOUT_STEP
,
317 flash
->pdata
->max_flash_timeout
);
320 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_INTENSITY
,
321 LM3560_FLASH_BRT_MIN
, max_flash_brt
,
322 LM3560_FLASH_BRT_STEP
, max_flash_brt
);
325 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_TORCH_INTENSITY
,
326 LM3560_TORCH_BRT_MIN
, max_torch_brt
,
327 LM3560_TORCH_BRT_STEP
, max_torch_brt
);
330 fault
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_FAULT
, 0,
331 V4L2_FLASH_FAULT_OVER_VOLTAGE
332 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
333 | V4L2_FLASH_FAULT_SHORT_CIRCUIT
334 | V4L2_FLASH_FAULT_TIMEOUT
, 0, 0);
336 fault
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
341 flash
->subdev_led
[led_no
].ctrl_handler
= hdl
;
345 /* initialize device */
346 static const struct v4l2_subdev_ops lm3560_ops
= {
350 static const struct regmap_config lm3560_regmap
= {
353 .max_register
= 0xFF,
356 static int lm3560_subdev_init(struct lm3560_flash
*flash
,
357 enum lm3560_led_id led_no
, char *led_name
)
359 struct i2c_client
*client
= to_i2c_client(flash
->dev
);
362 v4l2_i2c_subdev_init(&flash
->subdev_led
[led_no
], client
, &lm3560_ops
);
363 flash
->subdev_led
[led_no
].flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
364 strcpy(flash
->subdev_led
[led_no
].name
, led_name
);
365 rval
= lm3560_init_controls(flash
, led_no
);
368 rval
= media_entity_init(&flash
->subdev_led
[led_no
].entity
, 0, NULL
, 0);
371 flash
->subdev_led
[led_no
].entity
.type
= MEDIA_ENT_T_V4L2_SUBDEV_FLASH
;
376 v4l2_ctrl_handler_free(&flash
->ctrls_led
[led_no
]);
380 static int lm3560_init_device(struct lm3560_flash
*flash
)
383 unsigned int reg_val
;
385 /* set peak current */
386 rval
= regmap_update_bits(flash
->regmap
,
387 REG_FLASH_TOUT
, 0x60, flash
->pdata
->peak
);
391 flash
->led_mode
= V4L2_FLASH_LED_MODE_NONE
;
392 rval
= lm3560_mode_ctrl(flash
);
396 rval
= regmap_read(flash
->regmap
, REG_FLAG
, ®_val
);
400 static int lm3560_probe(struct i2c_client
*client
,
401 const struct i2c_device_id
*devid
)
403 struct lm3560_flash
*flash
;
404 struct lm3560_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
407 flash
= devm_kzalloc(&client
->dev
, sizeof(*flash
), GFP_KERNEL
);
411 flash
->regmap
= devm_regmap_init_i2c(client
, &lm3560_regmap
);
412 if (IS_ERR(flash
->regmap
)) {
413 rval
= PTR_ERR(flash
->regmap
);
417 /* if there is no platform data, use chip default value */
419 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
422 pdata
->peak
= LM3560_PEAK_3600mA
;
423 pdata
->max_flash_timeout
= LM3560_FLASH_TOUT_MAX
;
425 pdata
->max_flash_brt
[LM3560_LED0
] = LM3560_FLASH_BRT_MAX
;
426 pdata
->max_torch_brt
[LM3560_LED0
] = LM3560_TORCH_BRT_MAX
;
428 pdata
->max_flash_brt
[LM3560_LED1
] = LM3560_FLASH_BRT_MAX
;
429 pdata
->max_torch_brt
[LM3560_LED1
] = LM3560_TORCH_BRT_MAX
;
431 flash
->pdata
= pdata
;
432 flash
->dev
= &client
->dev
;
433 mutex_init(&flash
->lock
);
435 rval
= lm3560_subdev_init(flash
, LM3560_LED0
, "lm3560-led0");
439 rval
= lm3560_subdev_init(flash
, LM3560_LED1
, "lm3560-led1");
443 rval
= lm3560_init_device(flash
);
447 i2c_set_clientdata(client
, flash
);
452 static int lm3560_remove(struct i2c_client
*client
)
454 struct lm3560_flash
*flash
= i2c_get_clientdata(client
);
457 for (i
= LM3560_LED0
; i
< LM3560_LED_MAX
; i
++) {
458 v4l2_device_unregister_subdev(&flash
->subdev_led
[i
]);
459 v4l2_ctrl_handler_free(&flash
->ctrls_led
[i
]);
460 media_entity_cleanup(&flash
->subdev_led
[i
].entity
);
466 static const struct i2c_device_id lm3560_id_table
[] = {
471 MODULE_DEVICE_TABLE(i2c
, lm3560_id_table
);
473 static struct i2c_driver lm3560_i2c_driver
= {
478 .probe
= lm3560_probe
,
479 .remove
= lm3560_remove
,
480 .id_table
= lm3560_id_table
,
483 module_i2c_driver(lm3560_i2c_driver
);
485 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
486 MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
487 MODULE_DESCRIPTION("Texas Instruments LM3560 LED flash driver");
488 MODULE_LICENSE("GPL");