1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/media/i2c/lm3560.c
4 * General device driver for TI lm3559, lm3560, FLASH LED Driver
6 * Copyright (C) 2013 Texas Instruments
8 * Contact: Daniel Jeong <gshark.jeong@gmail.com>
9 * Ldd-Mlp <ldd-mlp@list.ti.com>
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/slab.h>
16 #include <linux/mutex.h>
17 #include <linux/regmap.h>
18 #include <linux/videodev2.h>
19 #include <media/i2c/lm3560.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
23 /* registers definitions */
24 #define REG_ENABLE 0x10
25 #define REG_TORCH_BR 0xa0
26 #define REG_FLASH_BR 0xb0
27 #define REG_FLASH_TOUT 0xc0
29 #define REG_CONFIG1 0xe0
32 #define FAULT_TIMEOUT (1<<0)
33 #define FAULT_OVERTEMP (1<<1)
34 #define FAULT_SHORT_CIRCUIT (1<<2)
45 * @dev: pointer to &struct device
46 * @pdata: platform data
47 * @regmap: reg. map for i2c
48 * @lock: muxtex for serial access.
49 * @led_mode: V4L2 LED mode
50 * @ctrls_led: V4L2 controls
51 * @subdev_led: V4L2 subdev
55 struct lm3560_platform_data
*pdata
;
56 struct regmap
*regmap
;
59 enum v4l2_flash_led_mode led_mode
;
60 struct v4l2_ctrl_handler ctrls_led
[LM3560_LED_MAX
];
61 struct v4l2_subdev subdev_led
[LM3560_LED_MAX
];
64 #define to_lm3560_flash(_ctrl, _no) \
65 container_of(_ctrl->handler, struct lm3560_flash, ctrls_led[_no])
67 /* enable mode control */
68 static int lm3560_mode_ctrl(struct lm3560_flash
*flash
)
72 switch (flash
->led_mode
) {
73 case V4L2_FLASH_LED_MODE_NONE
:
74 rval
= regmap_update_bits(flash
->regmap
,
75 REG_ENABLE
, 0x03, MODE_SHDN
);
77 case V4L2_FLASH_LED_MODE_TORCH
:
78 rval
= regmap_update_bits(flash
->regmap
,
79 REG_ENABLE
, 0x03, MODE_TORCH
);
81 case V4L2_FLASH_LED_MODE_FLASH
:
82 rval
= regmap_update_bits(flash
->regmap
,
83 REG_ENABLE
, 0x03, MODE_FLASH
);
89 /* led1/2 enable/disable */
90 static int lm3560_enable_ctrl(struct lm3560_flash
*flash
,
91 enum lm3560_led_id led_no
, bool on
)
95 if (led_no
== LM3560_LED0
) {
97 rval
= regmap_update_bits(flash
->regmap
,
98 REG_ENABLE
, 0x08, 0x08);
100 rval
= regmap_update_bits(flash
->regmap
,
101 REG_ENABLE
, 0x08, 0x00);
104 rval
= regmap_update_bits(flash
->regmap
,
105 REG_ENABLE
, 0x10, 0x10);
107 rval
= regmap_update_bits(flash
->regmap
,
108 REG_ENABLE
, 0x10, 0x00);
113 /* torch1/2 brightness control */
114 static int lm3560_torch_brt_ctrl(struct lm3560_flash
*flash
,
115 enum lm3560_led_id led_no
, unsigned int brt
)
120 if (brt
< LM3560_TORCH_BRT_MIN
)
121 return lm3560_enable_ctrl(flash
, led_no
, false);
123 rval
= lm3560_enable_ctrl(flash
, led_no
, true);
125 br_bits
= LM3560_TORCH_BRT_uA_TO_REG(brt
);
126 if (led_no
== LM3560_LED0
)
127 rval
= regmap_update_bits(flash
->regmap
,
128 REG_TORCH_BR
, 0x07, br_bits
);
130 rval
= regmap_update_bits(flash
->regmap
,
131 REG_TORCH_BR
, 0x38, br_bits
<< 3);
136 /* flash1/2 brightness control */
137 static int lm3560_flash_brt_ctrl(struct lm3560_flash
*flash
,
138 enum lm3560_led_id led_no
, unsigned int brt
)
143 if (brt
< LM3560_FLASH_BRT_MIN
)
144 return lm3560_enable_ctrl(flash
, led_no
, false);
146 rval
= lm3560_enable_ctrl(flash
, led_no
, true);
148 br_bits
= LM3560_FLASH_BRT_uA_TO_REG(brt
);
149 if (led_no
== LM3560_LED0
)
150 rval
= regmap_update_bits(flash
->regmap
,
151 REG_FLASH_BR
, 0x0f, br_bits
);
153 rval
= regmap_update_bits(flash
->regmap
,
154 REG_FLASH_BR
, 0xf0, br_bits
<< 4);
160 static int lm3560_get_ctrl(struct v4l2_ctrl
*ctrl
, enum lm3560_led_id led_no
)
162 struct lm3560_flash
*flash
= to_lm3560_flash(ctrl
, led_no
);
165 mutex_lock(&flash
->lock
);
167 if (ctrl
->id
== V4L2_CID_FLASH_FAULT
) {
169 unsigned int reg_val
;
170 rval
= regmap_read(flash
->regmap
, REG_FLAG
, ®_val
);
173 if (reg_val
& FAULT_SHORT_CIRCUIT
)
174 fault
|= V4L2_FLASH_FAULT_SHORT_CIRCUIT
;
175 if (reg_val
& FAULT_OVERTEMP
)
176 fault
|= V4L2_FLASH_FAULT_OVER_TEMPERATURE
;
177 if (reg_val
& FAULT_TIMEOUT
)
178 fault
|= V4L2_FLASH_FAULT_TIMEOUT
;
179 ctrl
->cur
.val
= fault
;
183 mutex_unlock(&flash
->lock
);
187 static int lm3560_set_ctrl(struct v4l2_ctrl
*ctrl
, enum lm3560_led_id led_no
)
189 struct lm3560_flash
*flash
= to_lm3560_flash(ctrl
, led_no
);
193 mutex_lock(&flash
->lock
);
196 case V4L2_CID_FLASH_LED_MODE
:
197 flash
->led_mode
= ctrl
->val
;
198 if (flash
->led_mode
!= V4L2_FLASH_LED_MODE_FLASH
)
199 rval
= lm3560_mode_ctrl(flash
);
202 case V4L2_CID_FLASH_STROBE_SOURCE
:
203 rval
= regmap_update_bits(flash
->regmap
,
204 REG_CONFIG1
, 0x04, (ctrl
->val
) << 2);
209 case V4L2_CID_FLASH_STROBE
:
210 if (flash
->led_mode
!= V4L2_FLASH_LED_MODE_FLASH
) {
214 flash
->led_mode
= V4L2_FLASH_LED_MODE_FLASH
;
215 rval
= lm3560_mode_ctrl(flash
);
218 case V4L2_CID_FLASH_STROBE_STOP
:
219 if (flash
->led_mode
!= V4L2_FLASH_LED_MODE_FLASH
) {
223 flash
->led_mode
= V4L2_FLASH_LED_MODE_NONE
;
224 rval
= lm3560_mode_ctrl(flash
);
227 case V4L2_CID_FLASH_TIMEOUT
:
228 tout_bits
= LM3560_FLASH_TOUT_ms_TO_REG(ctrl
->val
);
229 rval
= regmap_update_bits(flash
->regmap
,
230 REG_FLASH_TOUT
, 0x1f, tout_bits
);
233 case V4L2_CID_FLASH_INTENSITY
:
234 rval
= lm3560_flash_brt_ctrl(flash
, led_no
, ctrl
->val
);
237 case V4L2_CID_FLASH_TORCH_INTENSITY
:
238 rval
= lm3560_torch_brt_ctrl(flash
, led_no
, ctrl
->val
);
243 mutex_unlock(&flash
->lock
);
247 static int lm3560_led1_get_ctrl(struct v4l2_ctrl
*ctrl
)
249 return lm3560_get_ctrl(ctrl
, LM3560_LED1
);
252 static int lm3560_led1_set_ctrl(struct v4l2_ctrl
*ctrl
)
254 return lm3560_set_ctrl(ctrl
, LM3560_LED1
);
257 static int lm3560_led0_get_ctrl(struct v4l2_ctrl
*ctrl
)
259 return lm3560_get_ctrl(ctrl
, LM3560_LED0
);
262 static int lm3560_led0_set_ctrl(struct v4l2_ctrl
*ctrl
)
264 return lm3560_set_ctrl(ctrl
, LM3560_LED0
);
267 static const struct v4l2_ctrl_ops lm3560_led_ctrl_ops
[LM3560_LED_MAX
] = {
269 .g_volatile_ctrl
= lm3560_led0_get_ctrl
,
270 .s_ctrl
= lm3560_led0_set_ctrl
,
273 .g_volatile_ctrl
= lm3560_led1_get_ctrl
,
274 .s_ctrl
= lm3560_led1_set_ctrl
,
278 static int lm3560_init_controls(struct lm3560_flash
*flash
,
279 enum lm3560_led_id led_no
)
281 struct v4l2_ctrl
*fault
;
282 u32 max_flash_brt
= flash
->pdata
->max_flash_brt
[led_no
];
283 u32 max_torch_brt
= flash
->pdata
->max_torch_brt
[led_no
];
284 struct v4l2_ctrl_handler
*hdl
= &flash
->ctrls_led
[led_no
];
285 const struct v4l2_ctrl_ops
*ops
= &lm3560_led_ctrl_ops
[led_no
];
287 v4l2_ctrl_handler_init(hdl
, 8);
290 v4l2_ctrl_new_std_menu(hdl
, ops
, V4L2_CID_FLASH_LED_MODE
,
291 V4L2_FLASH_LED_MODE_TORCH
, ~0x7,
292 V4L2_FLASH_LED_MODE_NONE
);
293 flash
->led_mode
= V4L2_FLASH_LED_MODE_NONE
;
296 v4l2_ctrl_new_std_menu(hdl
, ops
, V4L2_CID_FLASH_STROBE_SOURCE
,
297 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE
);
300 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_STROBE
, 0, 0, 0, 0);
302 /* flash strobe stop */
303 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_STROBE_STOP
, 0, 0, 0, 0);
305 /* flash strobe timeout */
306 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_TIMEOUT
,
307 LM3560_FLASH_TOUT_MIN
,
308 flash
->pdata
->max_flash_timeout
,
309 LM3560_FLASH_TOUT_STEP
,
310 flash
->pdata
->max_flash_timeout
);
313 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_INTENSITY
,
314 LM3560_FLASH_BRT_MIN
, max_flash_brt
,
315 LM3560_FLASH_BRT_STEP
, max_flash_brt
);
318 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_TORCH_INTENSITY
,
319 LM3560_TORCH_BRT_MIN
, max_torch_brt
,
320 LM3560_TORCH_BRT_STEP
, max_torch_brt
);
323 fault
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FLASH_FAULT
, 0,
324 V4L2_FLASH_FAULT_OVER_VOLTAGE
325 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
326 | V4L2_FLASH_FAULT_SHORT_CIRCUIT
327 | V4L2_FLASH_FAULT_TIMEOUT
, 0, 0);
329 fault
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
334 flash
->subdev_led
[led_no
].ctrl_handler
= hdl
;
338 /* initialize device */
339 static const struct v4l2_subdev_ops lm3560_ops
= {
343 static const struct regmap_config lm3560_regmap
= {
346 .max_register
= 0xFF,
349 static int lm3560_subdev_init(struct lm3560_flash
*flash
,
350 enum lm3560_led_id led_no
, char *led_name
)
352 struct i2c_client
*client
= to_i2c_client(flash
->dev
);
355 v4l2_i2c_subdev_init(&flash
->subdev_led
[led_no
], client
, &lm3560_ops
);
356 flash
->subdev_led
[led_no
].flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
357 strscpy(flash
->subdev_led
[led_no
].name
, led_name
,
358 sizeof(flash
->subdev_led
[led_no
].name
));
359 rval
= lm3560_init_controls(flash
, led_no
);
362 rval
= media_entity_pads_init(&flash
->subdev_led
[led_no
].entity
, 0, NULL
);
365 flash
->subdev_led
[led_no
].entity
.function
= MEDIA_ENT_F_FLASH
;
370 v4l2_ctrl_handler_free(&flash
->ctrls_led
[led_no
]);
374 static int lm3560_init_device(struct lm3560_flash
*flash
)
377 unsigned int reg_val
;
379 /* set peak current */
380 rval
= regmap_update_bits(flash
->regmap
,
381 REG_FLASH_TOUT
, 0x60, flash
->pdata
->peak
);
385 flash
->led_mode
= V4L2_FLASH_LED_MODE_NONE
;
386 rval
= lm3560_mode_ctrl(flash
);
390 rval
= regmap_read(flash
->regmap
, REG_FLAG
, ®_val
);
394 static int lm3560_probe(struct i2c_client
*client
,
395 const struct i2c_device_id
*devid
)
397 struct lm3560_flash
*flash
;
398 struct lm3560_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
401 flash
= devm_kzalloc(&client
->dev
, sizeof(*flash
), GFP_KERNEL
);
405 flash
->regmap
= devm_regmap_init_i2c(client
, &lm3560_regmap
);
406 if (IS_ERR(flash
->regmap
)) {
407 rval
= PTR_ERR(flash
->regmap
);
411 /* if there is no platform data, use chip default value */
413 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
416 pdata
->peak
= LM3560_PEAK_3600mA
;
417 pdata
->max_flash_timeout
= LM3560_FLASH_TOUT_MAX
;
419 pdata
->max_flash_brt
[LM3560_LED0
] = LM3560_FLASH_BRT_MAX
;
420 pdata
->max_torch_brt
[LM3560_LED0
] = LM3560_TORCH_BRT_MAX
;
422 pdata
->max_flash_brt
[LM3560_LED1
] = LM3560_FLASH_BRT_MAX
;
423 pdata
->max_torch_brt
[LM3560_LED1
] = LM3560_TORCH_BRT_MAX
;
425 flash
->pdata
= pdata
;
426 flash
->dev
= &client
->dev
;
427 mutex_init(&flash
->lock
);
429 rval
= lm3560_subdev_init(flash
, LM3560_LED0
, "lm3560-led0");
433 rval
= lm3560_subdev_init(flash
, LM3560_LED1
, "lm3560-led1");
437 rval
= lm3560_init_device(flash
);
441 i2c_set_clientdata(client
, flash
);
446 static int lm3560_remove(struct i2c_client
*client
)
448 struct lm3560_flash
*flash
= i2c_get_clientdata(client
);
451 for (i
= LM3560_LED0
; i
< LM3560_LED_MAX
; i
++) {
452 v4l2_device_unregister_subdev(&flash
->subdev_led
[i
]);
453 v4l2_ctrl_handler_free(&flash
->ctrls_led
[i
]);
454 media_entity_cleanup(&flash
->subdev_led
[i
].entity
);
460 static const struct i2c_device_id lm3560_id_table
[] = {
466 MODULE_DEVICE_TABLE(i2c
, lm3560_id_table
);
468 static struct i2c_driver lm3560_i2c_driver
= {
473 .probe
= lm3560_probe
,
474 .remove
= lm3560_remove
,
475 .id_table
= lm3560_id_table
,
478 module_i2c_driver(lm3560_i2c_driver
);
480 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
481 MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
482 MODULE_DESCRIPTION("Texas Instruments LM3560 LED flash driver");
483 MODULE_LICENSE("GPL");