net: qmi_wwan: add Olivetti Olicard 500
[linux/fpc-iii.git] / drivers / media / i2c / lm3560.c
blobc23de593c17d5572ff2123403ad6e4e006d21e55
1 /*
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.
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/lm3560.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
31 /* registers definitions */
32 #define REG_ENABLE 0x10
33 #define REG_TORCH_BR 0xa0
34 #define REG_FLASH_BR 0xb0
35 #define REG_FLASH_TOUT 0xc0
36 #define REG_FLAG 0xd0
37 #define REG_CONFIG1 0xe0
39 /* fault mask */
40 #define FAULT_TIMEOUT (1<<0)
41 #define FAULT_OVERTEMP (1<<1)
42 #define FAULT_SHORT_CIRCUIT (1<<2)
44 enum led_enable {
45 MODE_SHDN = 0x0,
46 MODE_TORCH = 0x2,
47 MODE_FLASH = 0x3,
50 /**
51 * struct lm3560_flash
53 * @pdata: platform data
54 * @regmap: reg. map for i2c
55 * @lock: muxtex for serial access.
56 * @led_mode: V4L2 LED mode
57 * @ctrls_led: V4L2 contols
58 * @subdev_led: V4L2 subdev
60 struct lm3560_flash {
61 struct device *dev;
62 struct lm3560_platform_data *pdata;
63 struct regmap *regmap;
64 struct mutex lock;
66 enum v4l2_flash_led_mode led_mode;
67 struct v4l2_ctrl_handler ctrls_led[LM3560_LED_MAX];
68 struct v4l2_subdev subdev_led[LM3560_LED_MAX];
71 #define to_lm3560_flash(_ctrl, _no) \
72 container_of(_ctrl->handler, struct lm3560_flash, ctrls_led[_no])
74 /* enable mode control */
75 static int lm3560_mode_ctrl(struct lm3560_flash *flash)
77 int rval = -EINVAL;
79 switch (flash->led_mode) {
80 case V4L2_FLASH_LED_MODE_NONE:
81 rval = regmap_update_bits(flash->regmap,
82 REG_ENABLE, 0x03, MODE_SHDN);
83 break;
84 case V4L2_FLASH_LED_MODE_TORCH:
85 rval = regmap_update_bits(flash->regmap,
86 REG_ENABLE, 0x03, MODE_TORCH);
87 break;
88 case V4L2_FLASH_LED_MODE_FLASH:
89 rval = regmap_update_bits(flash->regmap,
90 REG_ENABLE, 0x03, MODE_FLASH);
91 break;
93 return rval;
96 /* led1/2 enable/disable */
97 static int lm3560_enable_ctrl(struct lm3560_flash *flash,
98 enum lm3560_led_id led_no, bool on)
100 int rval;
102 if (led_no == LM3560_LED0) {
103 if (on == true)
104 rval = regmap_update_bits(flash->regmap,
105 REG_ENABLE, 0x08, 0x08);
106 else
107 rval = regmap_update_bits(flash->regmap,
108 REG_ENABLE, 0x08, 0x00);
109 } else {
110 if (on == true)
111 rval = regmap_update_bits(flash->regmap,
112 REG_ENABLE, 0x10, 0x10);
113 else
114 rval = regmap_update_bits(flash->regmap,
115 REG_ENABLE, 0x10, 0x00);
117 return rval;
120 /* torch1/2 brightness control */
121 static int lm3560_torch_brt_ctrl(struct lm3560_flash *flash,
122 enum lm3560_led_id led_no, unsigned int brt)
124 int rval;
125 u8 br_bits;
127 if (brt < LM3560_TORCH_BRT_MIN)
128 return lm3560_enable_ctrl(flash, led_no, false);
129 else
130 rval = lm3560_enable_ctrl(flash, led_no, true);
132 br_bits = LM3560_TORCH_BRT_uA_TO_REG(brt);
133 if (led_no == LM3560_LED0)
134 rval = regmap_update_bits(flash->regmap,
135 REG_TORCH_BR, 0x07, br_bits);
136 else
137 rval = regmap_update_bits(flash->regmap,
138 REG_TORCH_BR, 0x38, br_bits << 3);
140 return rval;
143 /* flash1/2 brightness control */
144 static int lm3560_flash_brt_ctrl(struct lm3560_flash *flash,
145 enum lm3560_led_id led_no, unsigned int brt)
147 int rval;
148 u8 br_bits;
150 if (brt < LM3560_FLASH_BRT_MIN)
151 return lm3560_enable_ctrl(flash, led_no, false);
152 else
153 rval = lm3560_enable_ctrl(flash, led_no, true);
155 br_bits = LM3560_FLASH_BRT_uA_TO_REG(brt);
156 if (led_no == LM3560_LED0)
157 rval = regmap_update_bits(flash->regmap,
158 REG_FLASH_BR, 0x0f, br_bits);
159 else
160 rval = regmap_update_bits(flash->regmap,
161 REG_FLASH_BR, 0xf0, br_bits << 4);
163 return rval;
166 /* v4l2 controls */
167 static int lm3560_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
169 struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
170 int rval = -EINVAL;
172 mutex_lock(&flash->lock);
174 if (ctrl->id == V4L2_CID_FLASH_FAULT) {
175 s32 fault = 0;
176 unsigned int reg_val;
177 rval = regmap_read(flash->regmap, REG_FLAG, &reg_val);
178 if (rval < 0)
179 goto out;
180 if (reg_val & FAULT_SHORT_CIRCUIT)
181 fault |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
182 if (reg_val & FAULT_OVERTEMP)
183 fault |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
184 if (reg_val & FAULT_TIMEOUT)
185 fault |= V4L2_FLASH_FAULT_TIMEOUT;
186 ctrl->cur.val = fault;
189 out:
190 mutex_unlock(&flash->lock);
191 return rval;
194 static int lm3560_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
196 struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
197 u8 tout_bits;
198 int rval = -EINVAL;
200 mutex_lock(&flash->lock);
202 switch (ctrl->id) {
203 case V4L2_CID_FLASH_LED_MODE:
204 flash->led_mode = ctrl->val;
205 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
206 rval = lm3560_mode_ctrl(flash);
207 break;
209 case V4L2_CID_FLASH_STROBE_SOURCE:
210 rval = regmap_update_bits(flash->regmap,
211 REG_CONFIG1, 0x04, (ctrl->val) << 2);
212 if (rval < 0)
213 goto err_out;
214 break;
216 case V4L2_CID_FLASH_STROBE:
217 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
218 rval = -EBUSY;
219 goto err_out;
221 flash->led_mode = V4L2_FLASH_LED_MODE_FLASH;
222 rval = lm3560_mode_ctrl(flash);
223 break;
225 case V4L2_CID_FLASH_STROBE_STOP:
226 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
227 rval = -EBUSY;
228 goto err_out;
230 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
231 rval = lm3560_mode_ctrl(flash);
232 break;
234 case V4L2_CID_FLASH_TIMEOUT:
235 tout_bits = LM3560_FLASH_TOUT_ms_TO_REG(ctrl->val);
236 rval = regmap_update_bits(flash->regmap,
237 REG_FLASH_TOUT, 0x1f, tout_bits);
238 break;
240 case V4L2_CID_FLASH_INTENSITY:
241 rval = lm3560_flash_brt_ctrl(flash, led_no, ctrl->val);
242 break;
244 case V4L2_CID_FLASH_TORCH_INTENSITY:
245 rval = lm3560_torch_brt_ctrl(flash, led_no, ctrl->val);
246 break;
249 err_out:
250 mutex_unlock(&flash->lock);
251 return rval;
254 static int lm3560_led1_get_ctrl(struct v4l2_ctrl *ctrl)
256 return lm3560_get_ctrl(ctrl, LM3560_LED1);
259 static int lm3560_led1_set_ctrl(struct v4l2_ctrl *ctrl)
261 return lm3560_set_ctrl(ctrl, LM3560_LED1);
264 static int lm3560_led0_get_ctrl(struct v4l2_ctrl *ctrl)
266 return lm3560_get_ctrl(ctrl, LM3560_LED0);
269 static int lm3560_led0_set_ctrl(struct v4l2_ctrl *ctrl)
271 return lm3560_set_ctrl(ctrl, LM3560_LED0);
274 static const struct v4l2_ctrl_ops lm3560_led_ctrl_ops[LM3560_LED_MAX] = {
275 [LM3560_LED0] = {
276 .g_volatile_ctrl = lm3560_led0_get_ctrl,
277 .s_ctrl = lm3560_led0_set_ctrl,
279 [LM3560_LED1] = {
280 .g_volatile_ctrl = lm3560_led1_get_ctrl,
281 .s_ctrl = lm3560_led1_set_ctrl,
285 static int lm3560_init_controls(struct lm3560_flash *flash,
286 enum lm3560_led_id led_no)
288 struct v4l2_ctrl *fault;
289 u32 max_flash_brt = flash->pdata->max_flash_brt[led_no];
290 u32 max_torch_brt = flash->pdata->max_torch_brt[led_no];
291 struct v4l2_ctrl_handler *hdl = &flash->ctrls_led[led_no];
292 const struct v4l2_ctrl_ops *ops = &lm3560_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_TORCH, ~0x7,
299 V4L2_FLASH_LED_MODE_NONE);
300 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
302 /* flash source */
303 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
304 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
306 /* flash strobe */
307 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
309 /* flash strobe stop */
310 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
312 /* flash strobe timeout */
313 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
314 LM3560_FLASH_TOUT_MIN,
315 flash->pdata->max_flash_timeout,
316 LM3560_FLASH_TOUT_STEP,
317 flash->pdata->max_flash_timeout);
319 /* flash brt */
320 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
321 LM3560_FLASH_BRT_MIN, max_flash_brt,
322 LM3560_FLASH_BRT_STEP, max_flash_brt);
324 /* torch brt */
325 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
326 LM3560_TORCH_BRT_MIN, max_torch_brt,
327 LM3560_TORCH_BRT_STEP, max_torch_brt);
329 /* fault */
330 fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
331 V4L2_FLASH_FAULT_OVER_VOLTAGE
332 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
333 | V4L2_FLASH_FAULT_SHORT_CIRCUIT
334 | V4L2_FLASH_FAULT_TIMEOUT, 0, 0);
335 if (fault != NULL)
336 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
338 if (hdl->error)
339 return hdl->error;
341 flash->subdev_led[led_no].ctrl_handler = hdl;
342 return 0;
345 /* initialize device */
346 static const struct v4l2_subdev_ops lm3560_ops = {
347 .core = NULL,
350 static const struct regmap_config lm3560_regmap = {
351 .reg_bits = 8,
352 .val_bits = 8,
353 .max_register = 0xFF,
356 static int lm3560_subdev_init(struct lm3560_flash *flash,
357 enum lm3560_led_id led_no, char *led_name)
359 struct i2c_client *client = to_i2c_client(flash->dev);
360 int rval;
362 v4l2_i2c_subdev_init(&flash->subdev_led[led_no], client, &lm3560_ops);
363 flash->subdev_led[led_no].flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
364 strcpy(flash->subdev_led[led_no].name, led_name);
365 rval = lm3560_init_controls(flash, led_no);
366 if (rval)
367 goto err_out;
368 rval = media_entity_init(&flash->subdev_led[led_no].entity, 0, NULL, 0);
369 if (rval < 0)
370 goto err_out;
371 flash->subdev_led[led_no].entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
373 return rval;
375 err_out:
376 v4l2_ctrl_handler_free(&flash->ctrls_led[led_no]);
377 return rval;
380 static int lm3560_init_device(struct lm3560_flash *flash)
382 int rval;
383 unsigned int reg_val;
385 /* set peak current */
386 rval = regmap_update_bits(flash->regmap,
387 REG_FLASH_TOUT, 0x60, flash->pdata->peak);
388 if (rval < 0)
389 return rval;
390 /* output disable */
391 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
392 rval = lm3560_mode_ctrl(flash);
393 if (rval < 0)
394 return rval;
395 /* reset faults */
396 rval = regmap_read(flash->regmap, REG_FLAG, &reg_val);
397 return rval;
400 static int lm3560_probe(struct i2c_client *client,
401 const struct i2c_device_id *devid)
403 struct lm3560_flash *flash;
404 struct lm3560_platform_data *pdata = dev_get_platdata(&client->dev);
405 int rval;
407 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
408 if (flash == NULL)
409 return -ENOMEM;
411 flash->regmap = devm_regmap_init_i2c(client, &lm3560_regmap);
412 if (IS_ERR(flash->regmap)) {
413 rval = PTR_ERR(flash->regmap);
414 return rval;
417 /* if there is no platform data, use chip default value */
418 if (pdata == NULL) {
419 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
420 if (pdata == NULL)
421 return -ENODEV;
422 pdata->peak = LM3560_PEAK_3600mA;
423 pdata->max_flash_timeout = LM3560_FLASH_TOUT_MAX;
424 /* led 1 */
425 pdata->max_flash_brt[LM3560_LED0] = LM3560_FLASH_BRT_MAX;
426 pdata->max_torch_brt[LM3560_LED0] = LM3560_TORCH_BRT_MAX;
427 /* led 2 */
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");
436 if (rval < 0)
437 return rval;
439 rval = lm3560_subdev_init(flash, LM3560_LED1, "lm3560-led1");
440 if (rval < 0)
441 return rval;
443 rval = lm3560_init_device(flash);
444 if (rval < 0)
445 return rval;
447 i2c_set_clientdata(client, flash);
449 return 0;
452 static int lm3560_remove(struct i2c_client *client)
454 struct lm3560_flash *flash = i2c_get_clientdata(client);
455 unsigned int i;
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);
463 return 0;
466 static const struct i2c_device_id lm3560_id_table[] = {
467 {LM3560_NAME, 0},
471 MODULE_DEVICE_TABLE(i2c, lm3560_id_table);
473 static struct i2c_driver lm3560_i2c_driver = {
474 .driver = {
475 .name = LM3560_NAME,
476 .pm = NULL,
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");