Add LM3643 new flash driver using v4l2-subsystem
[mlp-open-source/kernel.git] / drivers / media / i2c / lm3643.c
1 /*
2  * drivers/media/i2c/lm3643.c
3  * General device driver for TI LM3643, 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  *
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.
18  */
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/lm3643.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
31 /* registers definitions */
32 #define REG_ENABLE              0x01
33 #define REG_FLASH_LED0_BR       0x03
34 #define REG_FLASH_LED1_BR       0x04
35 #define REG_TORCH_LED0_BR       0x05
36 #define REG_TORCH_LED1_BR       0x06
37 #define REG_FLASH_TOUT          0x08
38 #define REG_FLAG0               0x0a
39 #define REG_FLAG1               0x0b
41 /* Fault Mask */
42 #define FAULT_TIMEOUT   (1<<0)
43 #define FAULT_SHORT_CIRCUIT     (0x70)
44 #define FAULT_UVLO              (1<<1)
45 #define FAULT_IVFM              (1<<2)
46 #define FAULT_OCP               (1<<3)
47 #define FAULT_OVERTEMP  (1<<2)
48 #define FAULT_NTC_TRIP  (1<<0)
49 #define FAULT_OVP               (1<<1)
51 #define MASK_MODE       0x0C
52 enum led_mode {
53         MODE_SHDN = 0x00,
54         MODE_IR_DRV = 0x04,
55         MODE_TORCH = 0x08,
56         MODE_FLASH = 0x0C,
57 };
59 /*
60  * struct lm3643_flash
61  *
62  * @pdata: platform data
63  * @regmap: reg. map for i2c
64  * @lock: muxtex for serial access.
65  * @led_mode: V4L2 LED mode
66  * @ctrls_led: V4L2 contols
67  * @subdev_led: V4L2 subdev
68  */
69 struct lm3643_flash {
70         struct device *dev;
71         struct lm3643_platform_data *pdata;
72         struct regmap *regmap;
73         struct mutex lock;
75         struct v4l2_ctrl_handler ctrls_led[LM3643_LED_MAX];
76         struct v4l2_subdev subdev_led[LM3643_LED_MAX];
77         /* caching control register to reduce redundant i2c accesses */
78         u8 enable_reg;
80 };
82 #define to_lm3643_flash(_ctrl, _no)     \
83         container_of(_ctrl->handler, struct lm3643_flash, ctrls_led[_no])
85 /* enable mode control */
86 static int lm3643_mode_ctrl(struct lm3643_flash *flash,
87                             enum v4l2_flash_led_mode led_mode)
88 {
89         switch (led_mode) {
90         case V4L2_FLASH_LED_MODE_NONE:
91                 return regmap_write(flash->regmap,
92                                     REG_ENABLE, flash->enable_reg | MODE_SHDN);
93         case V4L2_FLASH_LED_MODE_IR:
94                 return regmap_write(flash->regmap,
95                                     REG_ENABLE,
96                                     flash->enable_reg | MODE_IR_DRV);
97         case V4L2_FLASH_LED_MODE_TORCH:
98                 return regmap_write(flash->regmap,
99                                     REG_ENABLE, flash->enable_reg | MODE_TORCH);
100         case V4L2_FLASH_LED_MODE_FLASH:
101                 return regmap_write(flash->regmap,
102                                     REG_ENABLE, flash->enable_reg | MODE_FLASH);
103         }
104         return -EINVAL;
107 /* torch0/1 brightness control */
108 static int lm3643_torch_brt_ctrl(struct lm3643_flash *flash,
109                                  enum lm3643_led_id led_no, unsigned int brt)
111         int rval;
112         u8 br_bits;
114         br_bits = LM3643_TORCH_BRT_uA_TO_REG(brt);
115         if (led_no == LM3643_LED0)
116                 rval = regmap_update_bits(flash->regmap,
117                                           REG_TORCH_LED0_BR, 0x7f, br_bits);
118         else
119                 rval = regmap_update_bits(flash->regmap,
120                                           REG_TORCH_LED1_BR, 0x7f, br_bits);
121         return rval;
124 /* flash0/1 brightness control */
125 static int lm3643_flash_brt_ctrl(struct lm3643_flash *flash,
126                                  enum lm3643_led_id led_no, unsigned int brt)
128         int rval;
129         u8 br_bits;
131         br_bits = LM3643_FLASH_BRT_uA_TO_REG(brt);
132         if (led_no == LM3643_LED0)
133                 rval = regmap_update_bits(flash->regmap,
134                                           REG_FLASH_LED0_BR, 0x7f, br_bits);
135         else
136                 rval = regmap_update_bits(flash->regmap,
137                                           REG_FLASH_LED1_BR, 0x7f, br_bits);
138         return rval;
141 static int lm3643_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3643_led_id led_no)
143         struct lm3643_flash *flash = to_lm3643_flash(ctrl, led_no);
144         int rval = -EINVAL;
146         mutex_lock(&flash->lock);
148         if (ctrl->id == V4L2_CID_FLASH_FAULT) {
149                 unsigned int reg_val;
151                 rval = regmap_read(flash->regmap, REG_FLAG0, &reg_val);
152                 if (rval < 0)
153                         goto out;
154                 ctrl->val = 0;
155                 if (reg_val & FAULT_TIMEOUT)
156                         ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
157                 if (reg_val & FAULT_UVLO)
158                         ctrl->val |= V4L2_FLASH_FAULT_UNDER_VOLTAGE;
159                 if (reg_val & FAULT_OVERTEMP)
160                         ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
161                 if (reg_val & FAULT_OCP)
162                         ctrl->val |= V4L2_FLASH_FAULT_OVER_CURRENT;
163                 if (reg_val & FAULT_SHORT_CIRCUIT)
164                         ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
166                 rval = regmap_read(flash->regmap, REG_FLAG1, &reg_val);
167                 if (rval < 0)
168                         goto out;
169                 if (reg_val & FAULT_IVFM)
170                         ctrl->val |= V4L2_FLASH_FAULT_INPUT_VOLTAGE;
171                 if (reg_val & FAULT_NTC_TRIP)
172                         ctrl->val |= V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE;
173                 if (reg_val & FAULT_OVP)
174                         ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
175         }
177 out:
178         mutex_unlock(&flash->lock);
179         return rval;
182 static int lm3643_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3643_led_id led_no)
184         struct lm3643_flash *flash = to_lm3643_flash(ctrl, led_no);
185         u8 tout_bits;
186         int rval = -EINVAL;
187         unsigned int reg_val;
189         mutex_lock(&flash->lock);
191         switch (ctrl->id) {
192         case V4L2_CID_FLASH_LED_MODE:
193                 if (ctrl->val != V4L2_FLASH_LED_MODE_FLASH) {
194                         rval = lm3643_mode_ctrl(flash, ctrl->val);
195                         break;
196                 }
197                 /* switch to SHDN mode before flash strobe on */
198                 rval = lm3643_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
199                 break;
201         case V4L2_CID_FLASH_STROBE_SOURCE:
202                 rval = regmap_update_bits(flash->regmap,
203                                           REG_ENABLE, 0x20, (ctrl->val) << 6);
204                 if (rval < 0)
205                         goto err_out;
206                 rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
207                 if (rval < 0)
208                         goto err_out;
209                 flash->enable_reg = reg_val & (~MASK_MODE);
210                 break;
212         case V4L2_CID_FLASH_STROBE:
214                 /* read and check current mode of chip to start flash */
215                 rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
216                 if (rval < 0 || ((reg_val & MASK_MODE) != MODE_SHDN))
217                         goto err_out;
218                 /* flash on */
219                 rval = lm3643_mode_ctrl(flash, V4L2_FLASH_LED_MODE_FLASH);
220                 break;
222         case V4L2_CID_FLASH_STROBE_STOP:
223                 /*
224                  * flash mode will be turned automatically
225                  * from FLASH mode to SHDN mode after flash duration timeout
226                  * read and check current mode of chip to stop flash
227                  */
228                 rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
229                 if (rval < 0)
230                         goto err_out;
231                 if ((reg_val & MASK_MODE) == MODE_FLASH)
232                         rval = lm3643_mode_ctrl(flash,
233                                                 V4L2_FLASH_LED_MODE_NONE);
234                 break;
236         case V4L2_CID_FLASH_TIMEOUT:
237                 tout_bits = LM3643_FLASH_TOUT_ms_TO_REG(ctrl->val);
238                 rval = regmap_update_bits(flash->regmap,
239                                           REG_FLASH_TOUT, 0x0f, tout_bits);
240                 break;
242         case V4L2_CID_FLASH_INTENSITY:
243                 rval = lm3643_flash_brt_ctrl(flash, led_no, ctrl->val);
244                 break;
246         case V4L2_CID_FLASH_TORCH_INTENSITY:
247                 rval = lm3643_torch_brt_ctrl(flash, led_no, ctrl->val);
248                 break;
249         }
251 err_out:
252         mutex_unlock(&flash->lock);
253         return rval;
256 static int lm3643_led1_get_ctrl(struct v4l2_ctrl *ctrl)
258         return lm3643_get_ctrl(ctrl, LM3643_LED1);
261 static int lm3643_led1_set_ctrl(struct v4l2_ctrl *ctrl)
263         return lm3643_set_ctrl(ctrl, LM3643_LED1);
266 static int lm3643_led0_get_ctrl(struct v4l2_ctrl *ctrl)
268         return lm3643_get_ctrl(ctrl, LM3643_LED0);
271 static int lm3643_led0_set_ctrl(struct v4l2_ctrl *ctrl)
273         return lm3643_set_ctrl(ctrl, LM3643_LED0);
276 static const struct v4l2_ctrl_ops lm3643_led_ctrl_ops[LM3643_LED_MAX] = {
277         [LM3643_LED0] = {
278                          .g_volatile_ctrl = lm3643_led0_get_ctrl,
279                          .s_ctrl = lm3643_led0_set_ctrl,
280                          },
281         [LM3643_LED1] = {
282                          .g_volatile_ctrl = lm3643_led1_get_ctrl,
283                          .s_ctrl = lm3643_led1_set_ctrl,
284                          }
285 };
287 static int lm3643_init_controls(struct lm3643_flash *flash,
288                                 enum lm3643_led_id led_no)
290         struct v4l2_ctrl *fault;
291         struct v4l2_ctrl_handler *hdl = &flash->ctrls_led[led_no];
292         const struct v4l2_ctrl_ops *ops = &lm3643_led_ctrl_ops[led_no];
294         v4l2_ctrl_handler_init(hdl, 8);
296         /* flash mode */
297         v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE,
298                                V4L2_FLASH_LED_MODE_IR, ~0x7,
299                                V4L2_FLASH_LED_MODE_NONE);
301         /* flash source */
302         v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
303                                0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
305         /* flash strobe */
306         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                           LM3643_FLASH_TOUT_MIN,
314                           LM3643_FLASH_TOUT_MAX,
315                           LM3643_FLASH_TOUT_LOW_STEP, LM3643_FLASH_TOUT_MAX);
317         /* flash brt */
318         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
319                           LM3643_FLASH_BRT_MIN, LM3643_FLASH_BRT_MAX,
320                           LM3643_FLASH_BRT_STEP, LM3643_FLASH_BRT_MAX);
322         /* torch brt */
323         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
324                           LM3643_TORCH_BRT_MIN, LM3643_TORCH_BRT_MAX,
325                           LM3643_TORCH_BRT_STEP, LM3643_TORCH_BRT_MAX);
327         /* fault */
328         fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
329                                   V4L2_FLASH_FAULT_TIMEOUT
330                                   | V4L2_FLASH_FAULT_SHORT_CIRCUIT
331                                   | V4L2_FLASH_FAULT_UNDER_VOLTAGE
332                                   | V4L2_FLASH_FAULT_INPUT_VOLTAGE
333                                   | V4L2_FLASH_FAULT_OVER_CURRENT
334                                   | V4L2_FLASH_FAULT_OVER_TEMPERATURE
335                                   | V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE
336                                   | V4L2_FLASH_FAULT_OVER_VOLTAGE, 0, 0);
337         if (fault != NULL)
338                 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
340         if (hdl->error)
341                 return hdl->error;
343         flash->subdev_led[led_no].ctrl_handler = hdl;
344         return 0;
347 /* initialize device */
348 static const struct v4l2_subdev_ops lm3643_ops = {
349         .core = NULL,
350 };
352 static const struct regmap_config lm3643_regmap = {
353         .reg_bits = 8,
354         .val_bits = 8,
355         .max_register = 0xFF,
356 };
358 static int lm3643_subdev_init(struct lm3643_flash *flash,
359                               enum lm3643_led_id led_no, char *led_name)
361         struct i2c_client *client = to_i2c_client(flash->dev);
362         int rval;
364         v4l2_i2c_subdev_init(&flash->subdev_led[led_no], client, &lm3643_ops);
365         flash->subdev_led[led_no].flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
366         strcpy(flash->subdev_led[led_no].name, led_name);
367         rval = lm3643_init_controls(flash, led_no);
368         if (rval)
369                 goto err_out;
370         rval = media_entity_init(&flash->subdev_led[led_no].entity, 0, NULL, 0);
371         if (rval < 0)
372                 goto err_out;
373         flash->subdev_led[led_no].entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
375         return rval;
377 err_out:
378         v4l2_ctrl_handler_free(&flash->ctrls_led[led_no]);
379         return rval;
382 static int lm3643_init_device(struct lm3643_flash *flash)
384         int rval;
385         unsigned int reg_val;
387         rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
388         if (rval < 0)
389                 return rval;
390         /* set led0/1 enable bits */
391         reg_val |= flash->pdata->led0_enable;
392         reg_val |= flash->pdata->led1_enable;
393         /* save enable register to reduce redundant i2c accesses */
394         flash->enable_reg = reg_val & 0xf3;
396         rval = regmap_write(flash->regmap, REG_ENABLE, reg_val);
397         if (rval < 0)
398                 return rval;
400         /* set output mode */
401         rval = lm3643_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
402         if (rval < 0)
403                 return rval;
405         /* Reset flag 0 register */
406         rval = regmap_read(flash->regmap, REG_FLAG0, &reg_val);
407         if (rval < 0)
408                 return rval;
410         /* Reset flag 1 register */
411         return regmap_read(flash->regmap, REG_FLAG1, &reg_val);
415 static int lm3643_probe(struct i2c_client *client,
416                         const struct i2c_device_id *devid)
418         struct lm3643_flash *flash;
419         struct lm3643_platform_data *pdata = dev_get_platdata(&client->dev);
420         int rval;
422         flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
423         if (flash == NULL)
424                 return -ENOMEM;
426         flash->regmap = devm_regmap_init_i2c(client, &lm3643_regmap);
427         if (IS_ERR(flash->regmap))
428                 return PTR_ERR(flash->regmap);
430         /* if there is no platform data, use chip default value */
431         if (pdata == NULL) {
432                 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
433                 if (pdata == NULL)
434                         return -ENODEV;
435                 pdata->led0_enable = 0x01;
436                 pdata->led1_enable = 0x01 << 1;
437                 pdata->flash1_override = 0x01 << 7;
438                 pdata->torch1_override = 0x01 << 7;
439         }
440         flash->pdata = pdata;
441         flash->dev = &client->dev;
442         mutex_init(&flash->lock);
444         rval = lm3643_subdev_init(flash, LM3643_LED0, "lm3643-led0");
445         if (rval < 0)
446                 return rval;
448         rval = lm3643_subdev_init(flash, LM3643_LED1, "lm3643-led1");
449         if (rval < 0)
450                 return rval;
452         rval = lm3643_init_device(flash);
453         if (rval < 0)
454                 return rval;
456         i2c_set_clientdata(client, flash);
458         return 0;
461 static int lm3643_remove(struct i2c_client *client)
463         struct lm3643_flash *flash = i2c_get_clientdata(client);
464         unsigned int i;
466         for (i = LM3643_LED0; i < LM3643_LED_MAX; i++) {
467                 v4l2_device_unregister_subdev(&flash->subdev_led[i]);
468                 v4l2_ctrl_handler_free(&flash->ctrls_led[i]);
469                 media_entity_cleanup(&flash->subdev_led[i].entity);
470         }
472         return 0;
475 static const struct i2c_device_id lm3643_id_table[] = {
476         {LM3643_NAME, 0},
477         {}
478 };
480 MODULE_DEVICE_TABLE(i2c, lm3643_id_table);
482 static struct i2c_driver lm3643_i2c_driver = {
483         .driver = {
484                    .name = LM3643_NAME,
485                    .pm = NULL,
486                    },
487         .probe = lm3643_probe,
488         .remove = lm3643_remove,
489         .id_table = lm3643_id_table,
490 };
492 module_i2c_driver(lm3643_i2c_driver);
494 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
495 MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
496 MODULE_DESCRIPTION("Texas Instruments LM3643 LED flash driver");
497 MODULE_LICENSE("GPL");