1 /*
2 * drivers/media/i2c/lm3648.c
3 * General device driver for TI lm3648, Dual FLASH LED Driver
4 *
5 * Copyright (C) 2014 Texas Instruments
6 *
7 * Contact: Daniel Jeong <gshark.jeong@gmail.com>
8 * Ldd-Mlp <ldd-mlp@list.ti.com>
9 *
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.
13 */
15 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <media/lm3648.h>
18 #include <linux/module.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <linux/videodev2.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
25 /* registers definitions */
26 #define REG_ENABLE 0x01
27 #define REG_FLASH_BR 0x03
28 #define REG_TORCH_BR 0x05
29 #define REG_FLASH_TOUT 0x08
30 #define REG_FLAG1 0x0a
31 #define REG_FLAG2 0x0b
32 #define REG_STROBE_SRC 0x01
34 #define MASK_ENABLE 0x0c
35 #define MASK_TORCH_BR 0x7f
36 #define MASK_FLASH_BR 0x3f
37 #define MASK_FLASH_TOUT 0x0f
38 #define MASK_FLAG1 0xff
39 #define MASK_FLAG2 0x1f
40 #define MASK_STROBE_SRC 0x20
42 /* Fault1 Mask */
43 #define FAULT_TIMEOUT (1<<0)
44 #define FAULT_UVLO (1<<1)
45 #define FAULT_OVERTEMP (1<<2)
46 #define FAULT_OCP (1<<3)
47 #define FAULT_SHORT_CIRCUIT (0x7<<4)
49 /* Fault2 Mask */
50 #define FAULT_NTC_TRIP (0x19<<0)
51 #define FAULT_OVP (1<<1)
52 #define FAULT_IVFM (1<<2)
54 enum led_mode {
55 MODE_SHDN = 0x00,
56 MODE_IR_DRV = 0x04,
57 MODE_TORCH = 0x08,
58 MODE_FLASH = 0x0c,
59 };
61 /*
62 * struct lm3648_flash
63 *
64 * @pdata: platform data
65 * @regmap: reg. map for i2c
66 * @lock: muxtex for serial access.
67 * @led_mode: V4L2 LED mode
68 * @ctrls_led: V4L2 contols
69 * @subdev_led: V4L2 subdev
70 * @mode_reg : mode register value
71 */
72 struct lm3648_flash {
73 struct device *dev;
74 struct lm3648_platform_data *pdata;
75 struct regmap *regmap;
77 struct v4l2_ctrl_handler ctrls_led;
78 struct v4l2_subdev subdev_led;
80 u8 mode_reg;
81 };
83 #define to_lm3648_flash(_ctrl) \
84 container_of(_ctrl->handler, struct lm3648_flash, ctrls_led)
86 /* enable mode control */
87 static int lm3648_mode_ctrl(struct lm3648_flash *flash,
88 enum v4l2_flash_led_mode led_mode)
89 {
90 switch (led_mode) {
91 case V4L2_FLASH_LED_MODE_NONE:
92 return regmap_write(flash->regmap,
93 REG_ENABLE, flash->mode_reg | MODE_SHDN);
94 case V4L2_FLASH_LED_MODE_IR:
95 return regmap_write(flash->regmap,
96 REG_ENABLE, flash->mode_reg | MODE_IR_DRV);
97 case V4L2_FLASH_LED_MODE_TORCH:
98 return regmap_write(flash->regmap,
99 REG_ENABLE, flash->mode_reg | MODE_TORCH);
100 case V4L2_FLASH_LED_MODE_FLASH:
101 return regmap_write(flash->regmap,
102 REG_ENABLE, flash->mode_reg | MODE_FLASH);
103 }
104 return -EINVAL;
105 }
107 /* V4L2 controls */
108 static int lm3648_get_ctrl(struct v4l2_ctrl *ctrl)
109 {
110 struct lm3648_flash *flash = to_lm3648_flash(ctrl);
111 unsigned int reg_val;
112 int rval;
114 if (ctrl->id != V4L2_CID_FLASH_FAULT)
115 return -EINVAL;
117 rval = regmap_read(flash->regmap, REG_FLAG1, ®_val);
118 if (rval < 0)
119 return rval;
120 ctrl->val = 0;
121 if (reg_val & FAULT_TIMEOUT)
122 ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
123 if (reg_val & FAULT_UVLO)
124 ctrl->val |= V4L2_FLASH_FAULT_UNDER_VOLTAGE;
125 if (reg_val & FAULT_OVERTEMP)
126 ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
127 if (reg_val & FAULT_OCP)
128 ctrl->val |= V4L2_FLASH_FAULT_OVER_CURRENT;
129 if (reg_val & FAULT_SHORT_CIRCUIT)
130 ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
132 rval = regmap_read(flash->regmap, REG_FLAG2, ®_val);
133 if (rval < 0)
134 return rval;
135 if (reg_val & FAULT_NTC_TRIP)
136 ctrl->val |= V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE;
137 if (reg_val & FAULT_OVP)
138 ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
139 if (reg_val & FAULT_IVFM)
140 ctrl->val |= V4L2_FLASH_FAULT_INPUT_VOLTAGE;
142 return 0;
143 }
145 static int lm3648_set_ctrl(struct v4l2_ctrl *ctrl)
146 {
147 struct lm3648_flash *flash = to_lm3648_flash(ctrl);
148 unsigned int reg_val;
149 int rval = -EINVAL;
151 switch (ctrl->id) {
152 case V4L2_CID_FLASH_LED_MODE:
153 if (ctrl->val != V4L2_FLASH_LED_MODE_FLASH)
154 return lm3648_mode_ctrl(flash, ctrl->val);
155 /* switch to SHDN mode before flash strobe on */
156 return lm3648_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
158 case V4L2_CID_FLASH_STROBE_SOURCE:
159 return regmap_update_bits(flash->regmap,
160 REG_STROBE_SRC, MASK_STROBE_SRC,
161 (ctrl->val) << 5);
163 case V4L2_CID_FLASH_STROBE:
164 /* read and check current mode of chip to start flash */
165 rval = regmap_read(flash->regmap, REG_ENABLE, ®_val);
166 if (rval < 0 || ((reg_val & MASK_ENABLE) != MODE_SHDN))
167 return rval;
168 /* flash on */
169 return lm3648_mode_ctrl(flash, V4L2_FLASH_LED_MODE_FLASH);
171 case V4L2_CID_FLASH_STROBE_STOP:
172 /*
173 * flash mode will be turned automatically
174 * from FLASH mode to SHDN mode after flash duration timeout
175 * read and check current mode of chip to stop flash
176 */
177 rval = regmap_read(flash->regmap, REG_ENABLE, ®_val);
178 if (rval < 0)
179 return rval;
180 if ((reg_val & MASK_ENABLE) == MODE_FLASH)
181 return lm3648_mode_ctrl(flash,
182 V4L2_FLASH_LED_MODE_NONE);
183 return rval;
185 case V4L2_CID_FLASH_TIMEOUT:
186 return regmap_update_bits(flash->regmap,
187 REG_FLASH_TOUT, MASK_FLASH_TOUT,
188 LM3648_FLASH_TOUT_ms_TO_REG
189 (ctrl->val));
191 case V4L2_CID_FLASH_INTENSITY:
192 return regmap_update_bits(flash->regmap,
193 REG_FLASH_BR, MASK_FLASH_BR,
194 LM3648_FLASH_BRT_uA_TO_REG
195 (ctrl->val));
197 case V4L2_CID_FLASH_TORCH_INTENSITY:
198 return regmap_update_bits(flash->regmap,
199 REG_TORCH_BR, MASK_TORCH_BR,
200 LM3648_TORCH_BRT_uA_TO_REG
201 (ctrl->val));
202 }
204 return -EINVAL;
205 }
207 static const struct v4l2_ctrl_ops lm3648_led_ctrl_ops = {
208 .g_volatile_ctrl = lm3648_get_ctrl,
209 .s_ctrl = lm3648_set_ctrl,
210 };
212 static int lm3648_init_controls(struct lm3648_flash *flash)
213 {
214 struct v4l2_ctrl *fault;
215 struct v4l2_ctrl_handler *hdl = &flash->ctrls_led;
216 const struct v4l2_ctrl_ops *ops = &lm3648_led_ctrl_ops;
218 v4l2_ctrl_handler_init(hdl, 8);
219 /* flash mode */
220 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE,
221 V4L2_FLASH_LED_MODE_IR, ~0x7,
222 V4L2_FLASH_LED_MODE_NONE);
224 /* flash source */
225 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
226 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
228 /* flash strobe */
229 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
230 /* flash strobe stop */
231 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
233 /* flash strobe timeout */
234 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
235 LM3648_FLASH_TOUT_MIN,
236 LM3648_FLASH_TOUT_MAX,
237 LM3648_FLASH_TOUT_LOW_STEP,
238 LM3648_FLASH_TOUT_MAX);
240 /* max flash current */
241 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
242 LM3648_FLASH_BRT_MIN,
243 LM3648_FLASH_BRT_MAX,
244 LM3648_FLASH_BRT_STEP,
245 LM3648_FLASH_BRT_MAX);
247 /* max torch current */
248 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
249 LM3648_TORCH_BRT_MIN,
250 LM3648_TORCH_BRT_MAX,
251 LM3648_TORCH_BRT_STEP,
252 LM3648_TORCH_BRT_MAX);
254 /* fault */
255 fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
256 V4L2_FLASH_FAULT_TIMEOUT
257 | V4L2_FLASH_FAULT_SHORT_CIRCUIT
258 | V4L2_FLASH_FAULT_UNDER_VOLTAGE
259 | V4L2_FLASH_FAULT_INPUT_VOLTAGE
260 | V4L2_FLASH_FAULT_OVER_CURRENT
261 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
262 | V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE
263 | V4L2_FLASH_FAULT_OVER_VOLTAGE, 0, 0);
264 if (fault != NULL)
265 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
267 if (hdl->error)
268 return hdl->error;
270 flash->subdev_led.ctrl_handler = hdl;
271 return 0;
272 }
274 /* initialize device */
275 static const struct v4l2_subdev_ops lm3648_ops = {
276 .core = NULL,
277 };
279 static const struct regmap_config lm3648_regmap = {
280 .reg_bits = 8,
281 .val_bits = 8,
282 .max_register = 0xFF,
283 };
285 static int lm3648_subdev_init(struct lm3648_flash *flash)
286 {
287 struct i2c_client *client = to_i2c_client(flash->dev);
288 int rval;
290 v4l2_i2c_subdev_init(&flash->subdev_led, client, &lm3648_ops);
291 flash->subdev_led.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
292 strcpy(flash->subdev_led.name, LM3648_NAME);
293 rval = lm3648_init_controls(flash);
294 if (rval)
295 goto err_out;
296 rval = media_entity_init(&flash->subdev_led.entity, 0, NULL, 0);
297 if (rval < 0)
298 goto err_out;
299 flash->subdev_led.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
300 return rval;
302 err_out:
303 v4l2_ctrl_handler_free(&flash->ctrls_led);
304 return rval;
305 }
307 static int lm3648_init_device(struct lm3648_flash *flash)
308 {
309 unsigned int reg_val;
310 int rval;
312 /* LED Enable - on */
313 rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x03, 0x03);
314 rval |= regmap_update_bits(flash->regmap, REG_FLASH_BR, 0xc0, 0x80);
315 rval |= regmap_update_bits(flash->regmap, REG_TORCH_BR, 0x80, 0x80);
316 if (rval < 0)
317 return rval;
318 /* read the value of mode register to reduce redundant i2c accesses */
319 rval = regmap_read(flash->regmap, REG_ENABLE, ®_val);
320 if (rval < 0)
321 return rval;
322 flash->mode_reg = reg_val & 0xf3;
324 /* output disable */
325 rval = lm3648_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
326 if (rval < 0)
327 return rval;
329 /* Reset flag register */
330 rval = regmap_read(flash->regmap, REG_FLAG1, ®_val);
331 if (rval < 0)
332 return rval;
333 return regmap_read(flash->regmap, REG_FLAG2, ®_val);
334 }
336 static int lm3648_probe(struct i2c_client *client,
337 const struct i2c_device_id *devid)
338 {
339 struct lm3648_flash *flash;
340 struct lm3648_platform_data *pdata = dev_get_platdata(&client->dev);
341 int rval;
343 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
344 if (flash == NULL)
345 return -ENOMEM;
347 flash->regmap = devm_regmap_init_i2c(client, &lm3648_regmap);
348 if (IS_ERR(flash->regmap))
349 return PTR_ERR(flash->regmap);
351 /* check device tree if there is no platform data */
352 if (pdata == NULL) {
353 pdata = devm_kzalloc(&client->dev,
354 sizeof(struct lm3648_platform_data),
355 GFP_KERNEL);
356 if (pdata == NULL)
357 return -ENOMEM;
358 /* use default data in case of no platform data */
359 pdata->flash_timeout = LM3648_FLASH_TOUT_MAX;
360 }
361 flash->pdata = pdata;
362 flash->dev = &client->dev;
364 rval = lm3648_subdev_init(flash);
365 if (rval < 0)
366 return rval;
368 rval = lm3648_init_device(flash);
369 if (rval < 0)
370 return rval;
372 i2c_set_clientdata(client, flash);
374 return 0;
375 }
377 static int lm3648_remove(struct i2c_client *client)
378 {
379 struct lm3648_flash *flash = i2c_get_clientdata(client);
381 v4l2_device_unregister_subdev(&flash->subdev_led);
382 v4l2_ctrl_handler_free(&flash->ctrls_led);
383 media_entity_cleanup(&flash->subdev_led.entity);
385 return 0;
386 }
388 static const struct i2c_device_id lm3648_id_table[] = {
389 {LM3648_NAME, 0},
390 {}
391 };
393 MODULE_DEVICE_TABLE(i2c, lm3648_id_table);
395 static struct i2c_driver lm3648_i2c_driver = {
396 .driver = {
397 .name = LM3648_NAME,
398 },
399 .probe = lm3648_probe,
400 .remove = lm3648_remove,
401 .id_table = lm3648_id_table,
402 };
404 module_i2c_driver(lm3648_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 LM3648 Flash LED driver");
409 MODULE_LICENSE("GPL");