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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include <linux/delay.h>
27 #include <linux/module.h>
28 #include <linux/i2c.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <linux/regmap.h>
32 #include <linux/videodev2.h>
33 #include <media/lm3560.h>
34 #include <media/v4l2-ctrls.h>
35 #include <media/v4l2-device.h>
37 /* registers definitions */
38 #define REG_ENABLE 0x10
39 #define REG_TORCH_BR 0xa0
40 #define REG_FLASH_BR 0xb0
41 #define REG_FLASH_TOUT 0xc0
43 #define REG_CONFIG1 0xe0
46 #define FAULT_TIMEOUT (1<<0)
47 #define FAULT_OVERTEMP (1<<1)
48 #define FAULT_SHORT_CIRCUIT (1<<2)
56 /* struct lm3560_flash
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 contols
63 * @subdev_led: V4L2 subdev
67 struct lm3560_platform_data *pdata;
68 struct regmap *regmap;
71 enum v4l2_flash_led_mode led_mode;
72 struct v4l2_ctrl_handler ctrls_led[LM3560_LED_MAX];
73 struct v4l2_subdev subdev_led[LM3560_LED_MAX];
76 #define to_lm3560_flash(_ctrl, _no) \
77 container_of(_ctrl->handler, struct lm3560_flash, ctrls_led[_no])
79 /* enable mode control */
80 static int lm3560_mode_ctrl(struct lm3560_flash *flash)
84 switch (flash->led_mode) {
85 case V4L2_FLASH_LED_MODE_NONE:
86 rval = regmap_update_bits(flash->regmap,
87 REG_ENABLE, 0x03, MODE_SHDN);
89 case V4L2_FLASH_LED_MODE_TORCH:
90 rval = regmap_update_bits(flash->regmap,
91 REG_ENABLE, 0x03, MODE_TORCH);
93 case V4L2_FLASH_LED_MODE_FLASH:
94 rval = regmap_update_bits(flash->regmap,
95 REG_ENABLE, 0x03, MODE_FLASH);
101 /* led1/2 enable/disable */
102 static int lm3560_enable_ctrl(struct lm3560_flash *flash,
103 enum lm3560_led_id led_no, bool on)
107 if (led_no == LM3560_LED0) {
109 rval = regmap_update_bits(flash->regmap,
110 REG_ENABLE, 0x08, 0x08);
112 rval = regmap_update_bits(flash->regmap,
113 REG_ENABLE, 0x08, 0x00);
116 rval = regmap_update_bits(flash->regmap,
117 REG_ENABLE, 0x10, 0x10);
119 rval = regmap_update_bits(flash->regmap,
120 REG_ENABLE, 0x10, 0x00);
125 /* torch1/2 brightness control */
126 static int lm3560_torch_brt_ctrl(struct lm3560_flash *flash,
127 enum lm3560_led_id led_no, unsigned int brt)
132 if (brt < LM3560_TORCH_BRT_MIN)
133 return lm3560_enable_ctrl(flash, led_no, false);
135 rval = lm3560_enable_ctrl(flash, led_no, true);
137 br_bits = LM3560_TORCH_BRT_uA_TO_REG(brt);
138 if (led_no == LM3560_LED0)
139 rval = regmap_update_bits(flash->regmap,
140 REG_TORCH_BR, 0x07, br_bits);
142 rval = regmap_update_bits(flash->regmap,
143 REG_TORCH_BR, 0x38, br_bits << 3);
148 /* flash1/2 brightness control */
149 static int lm3560_flash_brt_ctrl(struct lm3560_flash *flash,
150 enum lm3560_led_id led_no, unsigned int brt)
155 if (brt < LM3560_FLASH_BRT_MIN)
156 return lm3560_enable_ctrl(flash, led_no, false);
158 rval = lm3560_enable_ctrl(flash, led_no, true);
160 br_bits = LM3560_FLASH_BRT_uA_TO_REG(brt);
161 if (led_no == LM3560_LED0)
162 rval = regmap_update_bits(flash->regmap,
163 REG_FLASH_BR, 0x0f, br_bits);
165 rval = regmap_update_bits(flash->regmap,
166 REG_FLASH_BR, 0xf0, br_bits << 4);
172 static int lm3560_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
174 struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
176 mutex_lock(&flash->lock);
178 if (ctrl->id == V4L2_CID_FLASH_FAULT) {
181 unsigned int reg_val;
182 rval = regmap_read(flash->regmap, REG_FLAG, ®_val);
185 if (rval & FAULT_SHORT_CIRCUIT)
186 fault |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
187 if (rval & FAULT_OVERTEMP)
188 fault |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
189 if (rval & FAULT_TIMEOUT)
190 fault |= V4L2_FLASH_FAULT_TIMEOUT;
191 ctrl->cur.val = fault;
195 mutex_unlock(&flash->lock);
199 static int lm3560_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
201 struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
205 mutex_lock(&flash->lock);
208 case V4L2_CID_FLASH_LED_MODE:
209 flash->led_mode = ctrl->val;
210 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
211 rval = lm3560_mode_ctrl(flash);
214 case V4L2_CID_FLASH_STROBE_SOURCE:
215 rval = regmap_update_bits(flash->regmap,
216 REG_CONFIG1, 0x04, (ctrl->val) << 2);
221 case V4L2_CID_FLASH_STROBE:
222 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
224 flash->led_mode = V4L2_FLASH_LED_MODE_FLASH;
225 rval = lm3560_mode_ctrl(flash);
228 case V4L2_CID_FLASH_STROBE_STOP:
229 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
231 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
232 rval = lm3560_mode_ctrl(flash);
235 case V4L2_CID_FLASH_TIMEOUT:
236 tout_bits = LM3560_FLASH_TOUT_ms_TO_REG(ctrl->val);
237 rval = regmap_update_bits(flash->regmap,
238 REG_FLASH_TOUT, 0x1f, tout_bits);
241 case V4L2_CID_FLASH_INTENSITY:
242 rval = lm3560_flash_brt_ctrl(flash, led_no, ctrl->val);
245 case V4L2_CID_FLASH_TORCH_INTENSITY:
246 rval = lm3560_torch_brt_ctrl(flash, led_no, ctrl->val);
250 mutex_unlock(&flash->lock);
255 static int lm3560_led1_get_ctrl(struct v4l2_ctrl *ctrl)
257 return lm3560_get_ctrl(ctrl, LM3560_LED1);
260 static int lm3560_led1_set_ctrl(struct v4l2_ctrl *ctrl)
262 return lm3560_set_ctrl(ctrl, LM3560_LED1);
265 static int lm3560_led0_get_ctrl(struct v4l2_ctrl *ctrl)
267 return lm3560_get_ctrl(ctrl, LM3560_LED0);
270 static int lm3560_led0_set_ctrl(struct v4l2_ctrl *ctrl)
272 return lm3560_set_ctrl(ctrl, LM3560_LED0);
275 static const struct v4l2_ctrl_ops lm3560_led_ctrl_ops[LM3560_LED_MAX] = {
277 .g_volatile_ctrl = lm3560_led0_get_ctrl,
278 .s_ctrl = lm3560_led0_set_ctrl,
281 .g_volatile_ctrl = lm3560_led1_get_ctrl,
282 .s_ctrl = lm3560_led1_set_ctrl,
286 static int lm3560_init_controls(struct lm3560_flash *flash,
287 enum lm3560_led_id led_no)
289 struct v4l2_ctrl *fault;
290 u32 max_flash_brt = flash->pdata->max_flash_brt[led_no];
291 u32 max_torch_brt = flash->pdata->max_torch_brt[led_no];
292 struct v4l2_ctrl_handler *hdl = &flash->ctrls_led[led_no];
293 const struct v4l2_ctrl_ops *ops = &lm3560_led_ctrl_ops[led_no];
295 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);
308 /* flash strobe stop */
309 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
311 /* flash strobe timeout */
312 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
313 LM3560_FLASH_TOUT_MIN,
314 flash->pdata->max_flash_timeout,
315 LM3560_FLASH_TOUT_STEP,
316 flash->pdata->max_flash_timeout);
319 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
320 LM3560_FLASH_BRT_MIN, max_flash_brt,
321 LM3560_FLASH_BRT_STEP, max_flash_brt);
324 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
325 LM3560_TORCH_BRT_MIN, max_torch_brt,
326 LM3560_TORCH_BRT_STEP, max_torch_brt);
329 fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
330 V4L2_FLASH_FAULT_OVER_VOLTAGE
331 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
332 | V4L2_FLASH_FAULT_SHORT_CIRCUIT
333 | V4L2_FLASH_FAULT_TIMEOUT, 0, 0);
335 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
340 flash->subdev_led[led_no].ctrl_handler = hdl;
344 /* initialize device */
345 static const struct v4l2_subdev_ops lm3560_ops = {
349 static const struct regmap_config lm3560_regmap = {
352 .max_register = 0xFF,
355 static int lm3560_subdev_init(struct lm3560_flash *flash,
356 enum lm3560_led_id led_no, char *led_name)
358 struct i2c_client *client = to_i2c_client(flash->dev);
361 v4l2_i2c_subdev_init(&flash->subdev_led[led_no], client, &lm3560_ops);
362 flash->subdev_led[led_no].flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
363 strcpy(flash->subdev_led[led_no].name, led_name);
364 rval = lm3560_init_controls(flash, led_no);
367 rval = media_entity_init(&flash->subdev_led[led_no].entity, 0, NULL, 0);
370 flash->subdev_led[led_no].entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
375 v4l2_ctrl_handler_free(&flash->ctrls_led[led_no]);
379 static int lm3560_init_device(struct lm3560_flash *flash)
382 unsigned int reg_val;
384 /* set peak current */
385 rval = regmap_update_bits(flash->regmap,
386 REG_FLASH_TOUT, 0x60, flash->pdata->peak);
390 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
391 rval = lm3560_mode_ctrl(flash);
395 rval = regmap_read(flash->regmap, REG_FLAG, ®_val);
399 static int lm3560_probe(struct i2c_client *client,
400 const struct i2c_device_id *devid)
402 struct lm3560_flash *flash;
403 struct lm3560_platform_data *pdata = dev_get_platdata(&client->dev);
406 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
410 flash->regmap = devm_regmap_init_i2c(client, &lm3560_regmap);
411 if (IS_ERR(flash->regmap)) {
412 rval = PTR_ERR(flash->regmap);
416 /* if there is no platform data, use chip default value */
419 kzalloc(sizeof(struct lm3560_platform_data), 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);
450 static int lm3560_remove(struct i2c_client *client)
452 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
453 struct lm3560_flash *flash = container_of(subdev, struct lm3560_flash,
454 subdev_led[LM3560_LED_MAX]);
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");