1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/media/i2c/lm3646.c
4 * General device driver for TI lm3646, Dual FLASH LED Driver
6 * Copyright (C) 2014 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/i2c.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/regmap.h>
17 #include <linux/videodev2.h>
18 #include <media/i2c/lm3646.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
22 /* registers definitions */
23 #define REG_ENABLE 0x01
24 #define REG_TORCH_BR 0x05
25 #define REG_FLASH_BR 0x05
26 #define REG_FLASH_TOUT 0x04
28 #define REG_STROBE_SRC 0x06
29 #define REG_LED1_FLASH_BR 0x06
30 #define REG_LED1_TORCH_BR 0x07
32 #define MASK_ENABLE 0x03
33 #define MASK_TORCH_BR 0x70
34 #define MASK_FLASH_BR 0x0F
35 #define MASK_FLASH_TOUT 0x07
36 #define MASK_FLAG 0xFF
37 #define MASK_STROBE_SRC 0x80
40 #define FAULT_TIMEOUT (1<<0)
41 #define FAULT_SHORT_CIRCUIT (1<<1)
42 #define FAULT_UVLO (1<<2)
43 #define FAULT_IVFM (1<<3)
44 #define FAULT_OCP (1<<4)
45 #define FAULT_OVERTEMP (1<<5)
46 #define FAULT_NTC_TRIP (1<<6)
47 #define FAULT_OVP (1<<7)
58 * @pdata: platform data
59 * @regmap: reg. map for i2c
60 * @lock: muxtex for serial access.
61 * @led_mode: V4L2 LED mode
62 * @ctrls_led: V4L2 controls
63 * @subdev_led: V4L2 subdev
64 * @mode_reg : mode register value
68 struct lm3646_platform_data *pdata;
69 struct regmap *regmap;
71 struct v4l2_ctrl_handler ctrls_led;
72 struct v4l2_subdev subdev_led;
77 #define to_lm3646_flash(_ctrl) \
78 container_of(_ctrl->handler, struct lm3646_flash, ctrls_led)
80 /* enable mode control */
81 static int lm3646_mode_ctrl(struct lm3646_flash *flash,
82 enum v4l2_flash_led_mode led_mode)
85 case V4L2_FLASH_LED_MODE_NONE:
86 return regmap_write(flash->regmap,
87 REG_ENABLE, flash->mode_reg | MODE_SHDN);
88 case V4L2_FLASH_LED_MODE_TORCH:
89 return regmap_write(flash->regmap,
90 REG_ENABLE, flash->mode_reg | MODE_TORCH);
91 case V4L2_FLASH_LED_MODE_FLASH:
92 return regmap_write(flash->regmap,
93 REG_ENABLE, flash->mode_reg | MODE_FLASH);
99 static int lm3646_get_ctrl(struct v4l2_ctrl *ctrl)
101 struct lm3646_flash *flash = to_lm3646_flash(ctrl);
102 unsigned int reg_val;
105 if (ctrl->id != V4L2_CID_FLASH_FAULT)
108 rval = regmap_read(flash->regmap, REG_FLAG, ®_val);
113 if (reg_val & FAULT_TIMEOUT)
114 ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
115 if (reg_val & FAULT_SHORT_CIRCUIT)
116 ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
117 if (reg_val & FAULT_UVLO)
118 ctrl->val |= V4L2_FLASH_FAULT_UNDER_VOLTAGE;
119 if (reg_val & FAULT_IVFM)
120 ctrl->val |= V4L2_FLASH_FAULT_INPUT_VOLTAGE;
121 if (reg_val & FAULT_OCP)
122 ctrl->val |= V4L2_FLASH_FAULT_OVER_CURRENT;
123 if (reg_val & FAULT_OVERTEMP)
124 ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
125 if (reg_val & FAULT_NTC_TRIP)
126 ctrl->val |= V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE;
127 if (reg_val & FAULT_OVP)
128 ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
133 static int lm3646_set_ctrl(struct v4l2_ctrl *ctrl)
135 struct lm3646_flash *flash = to_lm3646_flash(ctrl);
136 unsigned int reg_val;
140 case V4L2_CID_FLASH_LED_MODE:
142 if (ctrl->val != V4L2_FLASH_LED_MODE_FLASH)
143 return lm3646_mode_ctrl(flash, ctrl->val);
144 /* switch to SHDN mode before flash strobe on */
145 return lm3646_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
147 case V4L2_CID_FLASH_STROBE_SOURCE:
148 return regmap_update_bits(flash->regmap,
149 REG_STROBE_SRC, MASK_STROBE_SRC,
152 case V4L2_CID_FLASH_STROBE:
154 /* read and check current mode of chip to start flash */
155 rval = regmap_read(flash->regmap, REG_ENABLE, ®_val);
156 if (rval < 0 || ((reg_val & MASK_ENABLE) != MODE_SHDN))
159 return lm3646_mode_ctrl(flash, V4L2_FLASH_LED_MODE_FLASH);
161 case V4L2_CID_FLASH_STROBE_STOP:
164 * flash mode will be turned automatically
165 * from FLASH mode to SHDN mode after flash duration timeout
166 * read and check current mode of chip to stop flash
168 rval = regmap_read(flash->regmap, REG_ENABLE, ®_val);
171 if ((reg_val & MASK_ENABLE) == MODE_FLASH)
172 return lm3646_mode_ctrl(flash,
173 V4L2_FLASH_LED_MODE_NONE);
176 case V4L2_CID_FLASH_TIMEOUT:
177 return regmap_update_bits(flash->regmap,
178 REG_FLASH_TOUT, MASK_FLASH_TOUT,
179 LM3646_FLASH_TOUT_ms_TO_REG
182 case V4L2_CID_FLASH_INTENSITY:
183 return regmap_update_bits(flash->regmap,
184 REG_FLASH_BR, MASK_FLASH_BR,
185 LM3646_TOTAL_FLASH_BRT_uA_TO_REG
188 case V4L2_CID_FLASH_TORCH_INTENSITY:
189 return regmap_update_bits(flash->regmap,
190 REG_TORCH_BR, MASK_TORCH_BR,
191 LM3646_TOTAL_TORCH_BRT_uA_TO_REG
198 static const struct v4l2_ctrl_ops lm3646_led_ctrl_ops = {
199 .g_volatile_ctrl = lm3646_get_ctrl,
200 .s_ctrl = lm3646_set_ctrl,
203 static int lm3646_init_controls(struct lm3646_flash *flash)
205 struct v4l2_ctrl *fault;
206 struct v4l2_ctrl_handler *hdl = &flash->ctrls_led;
207 const struct v4l2_ctrl_ops *ops = &lm3646_led_ctrl_ops;
209 v4l2_ctrl_handler_init(hdl, 8);
211 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE,
212 V4L2_FLASH_LED_MODE_TORCH, ~0x7,
213 V4L2_FLASH_LED_MODE_NONE);
216 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
217 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
220 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
221 /* flash strobe stop */
222 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
224 /* flash strobe timeout */
225 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
226 LM3646_FLASH_TOUT_MIN,
227 LM3646_FLASH_TOUT_MAX,
228 LM3646_FLASH_TOUT_STEP, flash->pdata->flash_timeout);
230 /* max flash current */
231 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
232 LM3646_TOTAL_FLASH_BRT_MIN,
233 LM3646_TOTAL_FLASH_BRT_MAX,
234 LM3646_TOTAL_FLASH_BRT_STEP,
235 LM3646_TOTAL_FLASH_BRT_MAX);
237 /* max torch current */
238 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
239 LM3646_TOTAL_TORCH_BRT_MIN,
240 LM3646_TOTAL_TORCH_BRT_MAX,
241 LM3646_TOTAL_TORCH_BRT_STEP,
242 LM3646_TOTAL_TORCH_BRT_MAX);
245 fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
246 V4L2_FLASH_FAULT_OVER_VOLTAGE
247 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
248 | V4L2_FLASH_FAULT_SHORT_CIRCUIT
249 | V4L2_FLASH_FAULT_TIMEOUT, 0, 0);
251 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
256 flash->subdev_led.ctrl_handler = hdl;
260 /* initialize device */
261 static const struct v4l2_subdev_ops lm3646_ops = {
265 static const struct regmap_config lm3646_regmap = {
268 .max_register = 0xFF,
271 static int lm3646_subdev_init(struct lm3646_flash *flash)
273 struct i2c_client *client = to_i2c_client(flash->dev);
276 v4l2_i2c_subdev_init(&flash->subdev_led, client, &lm3646_ops);
277 flash->subdev_led.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
278 strscpy(flash->subdev_led.name, LM3646_NAME,
279 sizeof(flash->subdev_led.name));
280 rval = lm3646_init_controls(flash);
283 rval = media_entity_pads_init(&flash->subdev_led.entity, 0, NULL);
286 flash->subdev_led.entity.function = MEDIA_ENT_F_FLASH;
290 v4l2_ctrl_handler_free(&flash->ctrls_led);
294 static int lm3646_init_device(struct lm3646_flash *flash)
296 unsigned int reg_val;
299 /* read the value of mode register to reduce redundant i2c accesses */
300 rval = regmap_read(flash->regmap, REG_ENABLE, ®_val);
303 flash->mode_reg = reg_val & 0xfc;
306 rval = lm3646_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
311 * LED1 flash current setting
312 * LED2 flash current = Total(Max) flash current - LED1 flash current
314 rval = regmap_update_bits(flash->regmap,
315 REG_LED1_FLASH_BR, 0x7F,
316 LM3646_LED1_FLASH_BRT_uA_TO_REG
317 (flash->pdata->led1_flash_brt));
323 * LED1 torch current setting
324 * LED2 torch current = Total(Max) torch current - LED1 torch current
326 rval = regmap_update_bits(flash->regmap,
327 REG_LED1_TORCH_BR, 0x7F,
328 LM3646_LED1_TORCH_BRT_uA_TO_REG
329 (flash->pdata->led1_torch_brt));
333 /* Reset flag register */
334 return regmap_read(flash->regmap, REG_FLAG, ®_val);
337 static int lm3646_probe(struct i2c_client *client)
339 struct lm3646_flash *flash;
340 struct lm3646_platform_data *pdata = dev_get_platdata(&client->dev);
343 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
347 flash->regmap = devm_regmap_init_i2c(client, &lm3646_regmap);
348 if (IS_ERR(flash->regmap))
349 return PTR_ERR(flash->regmap);
351 /* check device tree if there is no platform data */
353 pdata = devm_kzalloc(&client->dev,
354 sizeof(struct lm3646_platform_data),
358 /* use default data in case of no platform data */
359 pdata->flash_timeout = LM3646_FLASH_TOUT_MAX;
360 pdata->led1_torch_brt = LM3646_LED1_TORCH_BRT_MAX;
361 pdata->led1_flash_brt = LM3646_LED1_FLASH_BRT_MAX;
363 flash->pdata = pdata;
364 flash->dev = &client->dev;
366 rval = lm3646_subdev_init(flash);
370 rval = lm3646_init_device(flash);
374 i2c_set_clientdata(client, flash);
379 static void lm3646_remove(struct i2c_client *client)
381 struct lm3646_flash *flash = i2c_get_clientdata(client);
383 v4l2_device_unregister_subdev(&flash->subdev_led);
384 v4l2_ctrl_handler_free(&flash->ctrls_led);
385 media_entity_cleanup(&flash->subdev_led.entity);
388 static const struct i2c_device_id lm3646_id_table[] = {
393 MODULE_DEVICE_TABLE(i2c, lm3646_id_table);
395 static struct i2c_driver lm3646_i2c_driver = {
399 .probe = lm3646_probe,
400 .remove = lm3646_remove,
401 .id_table = lm3646_id_table,
404 module_i2c_driver(lm3646_i2c_driver);
406 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
407 MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
408 MODULE_DESCRIPTION("Texas Instruments LM3646 Dual Flash LED driver");
409 MODULE_LICENSE("GPL");