fixing LM3648 flash driver (v4l2) after testing LM3643EVM
[mlp-open-source/kernel.git] / drivers / media / i2c / lm3648.c
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;
79 struct v4l2_device v4l2_dev;
81         u8 mode_reg;
82 };
84 #define to_lm3648_flash(_ctrl)  \
85         container_of(_ctrl->handler, struct lm3648_flash, ctrls_led)
87 /* enable mode control */
88 static int lm3648_mode_ctrl(struct lm3648_flash *flash,
89                             enum v4l2_flash_led_mode led_mode)
90 {
91         switch (led_mode) {
92         case V4L2_FLASH_LED_MODE_NONE:
93                 return regmap_write(flash->regmap,
94                                     REG_ENABLE, flash->mode_reg | MODE_SHDN);
95         case V4L2_FLASH_LED_MODE_IR:
96                 return regmap_write(flash->regmap,
97                                     REG_ENABLE, flash->mode_reg | MODE_IR_DRV);
98         case V4L2_FLASH_LED_MODE_TORCH:
99                 return regmap_write(flash->regmap,
100                                     REG_ENABLE, flash->mode_reg | MODE_TORCH);
101         case V4L2_FLASH_LED_MODE_FLASH:
102                 return regmap_write(flash->regmap,
103                                     REG_ENABLE, flash->mode_reg | MODE_FLASH);
104         }
105         return -EINVAL;
108 /* V4L2 controls  */
109 static int lm3648_get_ctrl(struct v4l2_ctrl *ctrl)
111         struct lm3648_flash *flash = to_lm3648_flash(ctrl);
112         unsigned int reg_val;
113         int rval;
115         if (ctrl->id != V4L2_CID_FLASH_FAULT)
116                 return -EINVAL;
118         rval = regmap_read(flash->regmap, REG_FLAG1, &reg_val);
119         if (rval < 0)
120                 return rval;
121         ctrl->val = 0;
122         if (reg_val & FAULT_TIMEOUT)
123                 ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
124         if (reg_val & FAULT_UVLO)
125                 ctrl->val |= V4L2_FLASH_FAULT_UNDER_VOLTAGE;
126         if (reg_val & FAULT_OVERTEMP)
127                 ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
128         if (reg_val & FAULT_OCP)
129                 ctrl->val |= V4L2_FLASH_FAULT_OVER_CURRENT;
130         if (reg_val & FAULT_SHORT_CIRCUIT)
131                 ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
133         rval = regmap_read(flash->regmap, REG_FLAG2, &reg_val);
134         if (rval < 0)
135                 return rval;
136         if (reg_val & FAULT_NTC_TRIP)
137                 ctrl->val |= V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE;
138         if (reg_val & FAULT_OVP)
139                 ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
140         if (reg_val & FAULT_IVFM)
141                 ctrl->val |= V4L2_FLASH_FAULT_INPUT_VOLTAGE;
143         return 0;
146 static int lm3648_set_ctrl(struct v4l2_ctrl *ctrl)
148         struct lm3648_flash *flash = to_lm3648_flash(ctrl);
149         unsigned int reg_val;
150         int rval = -EINVAL;
152         switch (ctrl->id) {
153         case V4L2_CID_FLASH_LED_MODE:
154                 if (ctrl->val != V4L2_FLASH_LED_MODE_FLASH)
155                         return lm3648_mode_ctrl(flash, ctrl->val);
156                 /* switch to SHDN mode before flash strobe on */
157                 return lm3648_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
159         case V4L2_CID_FLASH_STROBE_SOURCE:
160                 return regmap_update_bits(flash->regmap,
161                                           REG_STROBE_SRC, MASK_STROBE_SRC,
162                                           (ctrl->val) << 5);
164         case V4L2_CID_FLASH_STROBE:
165                 /* read and check current mode of chip to start flash */
166                 rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
167                 if (rval < 0 || ((reg_val & MASK_ENABLE) != MODE_SHDN))
168                         return rval;
169                 /* flash on */
170                 return lm3648_mode_ctrl(flash, V4L2_FLASH_LED_MODE_FLASH);
172         case V4L2_CID_FLASH_STROBE_STOP:
173                 /*
174                  * flash mode will be turned automatically
175                  * from FLASH mode to SHDN mode after flash duration timeout
176                  * read and check current mode of chip to stop flash
177                  */
178                 rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
179                 if (rval < 0)
180                         return rval;
181                 if ((reg_val & MASK_ENABLE) == MODE_FLASH)
182                         return lm3648_mode_ctrl(flash,
183                                                 V4L2_FLASH_LED_MODE_NONE);
184                 return rval;
186         case V4L2_CID_FLASH_TIMEOUT:
187                 return regmap_update_bits(flash->regmap,
188                                           REG_FLASH_TOUT, MASK_FLASH_TOUT,
189                                           LM3648_FLASH_TOUT_ms_TO_REG
190                                           (ctrl->val));
192         case V4L2_CID_FLASH_INTENSITY:
193                 return regmap_update_bits(flash->regmap,
194                                           REG_FLASH_BR, MASK_FLASH_BR,
195                                           LM3648_FLASH_BRT_uA_TO_REG
196                                           (ctrl->val));
198         case V4L2_CID_FLASH_TORCH_INTENSITY:
199                 return regmap_update_bits(flash->regmap,
200                                           REG_TORCH_BR, MASK_TORCH_BR,
201                                           LM3648_TORCH_BRT_uA_TO_REG
202                                           (ctrl->val));
203         }
205         return -EINVAL;
208 static const struct v4l2_ctrl_ops lm3648_led_ctrl_ops = {
209         .g_volatile_ctrl = lm3648_get_ctrl,
210         .s_ctrl = lm3648_set_ctrl,
211 };
213 static int lm3648_init_controls(struct lm3648_flash *flash)
215         struct v4l2_ctrl *fault;
216         struct v4l2_ctrl_handler *hdl = &flash->ctrls_led;
217         const struct v4l2_ctrl_ops *ops = &lm3648_led_ctrl_ops;
219         v4l2_ctrl_handler_init(hdl, 8);
220         /* flash mode */
221         v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE,
222                                V4L2_FLASH_LED_MODE_IR, ~0x7,
223                                V4L2_FLASH_LED_MODE_NONE);
225         /* flash source */
226         v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
227                                0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
229         /* flash strobe */
230         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
231         /* flash strobe stop */
232         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
234         /* flash strobe timeout */
235         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
236                           LM3648_FLASH_TOUT_MIN,
237                           LM3648_FLASH_TOUT_MAX,
238                           LM3648_FLASH_TOUT_LOW_STEP,
239                           LM3648_FLASH_TOUT_MAX);
241         /* max flash current */
242         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
243                           LM3648_FLASH_BRT_MIN,
244                           LM3648_FLASH_BRT_MAX,
245                           LM3648_FLASH_BRT_STEP,
246                           LM3648_FLASH_BRT_MAX);
248         /* max torch current */
249         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
250                           LM3648_TORCH_BRT_MIN,
251                           LM3648_TORCH_BRT_MAX,
252                           LM3648_TORCH_BRT_STEP,
253                           LM3648_TORCH_BRT_MAX);
255         /* fault */
256         fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
257                                   V4L2_FLASH_FAULT_TIMEOUT
258                                   | V4L2_FLASH_FAULT_SHORT_CIRCUIT
259                                   | V4L2_FLASH_FAULT_UNDER_VOLTAGE
260                                   | V4L2_FLASH_FAULT_INPUT_VOLTAGE
261                                   | V4L2_FLASH_FAULT_OVER_CURRENT
262                                   | V4L2_FLASH_FAULT_OVER_TEMPERATURE
263                                   | V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE
264                                   | V4L2_FLASH_FAULT_OVER_VOLTAGE, 0, 0);
265         if (fault != NULL)
266                 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
268         if (hdl->error)
269                 return hdl->error;
271         flash->subdev_led.ctrl_handler = hdl;
272         return 0;
275 /* initialize device */
276 static const struct v4l2_subdev_ops lm3648_ops = {
277         .core = NULL,
278 };
280 static const struct regmap_config lm3648_regmap = {
281         .reg_bits = 8,
282         .val_bits = 8,
283         .max_register = 0xFF,
284 };
286 static int lm3648_subdev_init(struct lm3648_flash *flash)
288         struct i2c_client *client = to_i2c_client(flash->dev);
289         int rval;
291 rval = v4l2_device_register(&client->dev, &flash->v4l2_dev);
292         v4l2_i2c_subdev_init(&flash->subdev_led, client, &lm3648_ops);
293         flash->subdev_led.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
294         strcpy(flash->subdev_led.name, LM3648_NAME);
295         rval = lm3648_init_controls(flash);
296         if (rval)
297                 goto err_out;
298         rval = media_entity_init(&flash->subdev_led.entity, 0, NULL, 0);
299         if (rval < 0)
300                 goto err_out;
301         flash->subdev_led.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
302 rval = v4l2_device_register_subdev(&flash->v4l2_dev, &flash->subdev_led);
303 rval = v4l2_device_register_subdev_nodes(&flash->v4l2_dev);
304         return rval;
306 err_out:
307         v4l2_ctrl_handler_free(&flash->ctrls_led);
308         return rval;
311 static int lm3648_init_device(struct lm3648_flash *flash)
313         unsigned int reg_val;
314         int rval;
316         /* LED Enable - on */
317         rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x03, 0x03);
318         rval |= regmap_update_bits(flash->regmap, REG_FLASH_BR, 0xc0, 0x80);
319         rval |= regmap_update_bits(flash->regmap, REG_TORCH_BR, 0x80, 0x80);
320         if (rval < 0)
321                 return rval;
322         /* read the value of mode register to reduce redundant i2c accesses */
323         rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
324         if (rval < 0)
325                 return rval;
326         flash->mode_reg = reg_val & 0xf3;
328         /* output disable */
329         rval = lm3648_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
330         if (rval < 0)
331                 return rval;
333         /* Reset flag register */
334         rval = regmap_read(flash->regmap, REG_FLAG1, &reg_val);
335         if (rval < 0)
336                 return rval;
337         return regmap_read(flash->regmap, REG_FLAG2, &reg_val);
340 static int lm3648_probe(struct i2c_client *client,
341                         const struct i2c_device_id *devid)
343         struct lm3648_flash *flash;
344         struct lm3648_platform_data *pdata = dev_get_platdata(&client->dev);
345         int rval;
347         flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
348         if (flash == NULL)
349                 return -ENOMEM;
351         flash->regmap = devm_regmap_init_i2c(client, &lm3648_regmap);
352         if (IS_ERR(flash->regmap))
353                 return PTR_ERR(flash->regmap);
355         /* check device tree if there is no platform data */
356         if (pdata == NULL) {
357                 pdata = devm_kzalloc(&client->dev,
358                                      sizeof(struct lm3648_platform_data),
359                                      GFP_KERNEL);
360                 if (pdata == NULL)
361                         return -ENOMEM;
362                 /* use default data in case of no platform data */
363                 pdata->flash_timeout = LM3648_FLASH_TOUT_MAX;
364         }
365         flash->pdata = pdata;
366         flash->dev = &client->dev;
368         rval = lm3648_subdev_init(flash);
369         if (rval < 0)
370                 return rval;
372         rval = lm3648_init_device(flash);
373         if (rval < 0)
374                 return rval;
376         i2c_set_clientdata(client, flash);
378         return 0;
381 static int lm3648_remove(struct i2c_client *client)
383         struct lm3648_flash *flash = i2c_get_clientdata(client);
385         v4l2_device_unregister_subdev(&flash->subdev_led);
386         v4l2_ctrl_handler_free(&flash->ctrls_led);
387         media_entity_cleanup(&flash->subdev_led.entity);
389         return 0;
392 static const struct i2c_device_id lm3648_id_table[] = {
393         {LM3648_NAME, 0},
394         {}
395 };
397 MODULE_DEVICE_TABLE(i2c, lm3648_id_table);
399 static struct i2c_driver lm3648_i2c_driver = {
400         .driver = {
401                    .name = LM3648_NAME,
402                    },
403         .probe = lm3648_probe,
404         .remove = lm3648_remove,
405         .id_table = lm3648_id_table,
406 };
408 module_i2c_driver(lm3648_i2c_driver);
410 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
411 MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
412 MODULE_DESCRIPTION("Texas Instruments LM3648 Flash LED driver");
413 MODULE_LICENSE("GPL");