Merge tag 'csky-for-linus-4.20-fixup-dtb' of https://github.com/c-sky/csky-linux
[linux-block.git] / drivers / media / i2c / lm3560.c
CommitLineData
7f6b11a1
DJ
1/*
2 * drivers/media/i2c/lm3560.c
653d500c 3 * General device driver for TI lm3559, lm3560, FLASH LED Driver
7f6b11a1
DJ
4 *
5 * Copyright (C) 2013 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.
7f6b11a1
DJ
18 */
19
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>
b5dcee22 27#include <media/i2c/lm3560.h>
7f6b11a1
DJ
28#include <media/v4l2-ctrls.h>
29#include <media/v4l2-device.h>
30
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
38
d1166b0f 39/* fault mask */
7f6b11a1
DJ
40#define FAULT_TIMEOUT (1<<0)
41#define FAULT_OVERTEMP (1<<1)
42#define FAULT_SHORT_CIRCUIT (1<<2)
43
44enum led_enable {
45 MODE_SHDN = 0x0,
46 MODE_TORCH = 0x2,
47 MODE_FLASH = 0x3,
48};
49
d1166b0f
AS
50/**
51 * struct lm3560_flash
7f6b11a1 52 *
ed050c21 53 * @dev: pointer to &struct device
7f6b11a1
DJ
54 * @pdata: platform data
55 * @regmap: reg. map for i2c
56 * @lock: muxtex for serial access.
57 * @led_mode: V4L2 LED mode
58 * @ctrls_led: V4L2 contols
59 * @subdev_led: V4L2 subdev
60 */
61struct lm3560_flash {
62 struct device *dev;
63 struct lm3560_platform_data *pdata;
64 struct regmap *regmap;
65 struct mutex lock;
66
67 enum v4l2_flash_led_mode led_mode;
68 struct v4l2_ctrl_handler ctrls_led[LM3560_LED_MAX];
69 struct v4l2_subdev subdev_led[LM3560_LED_MAX];
70};
71
72#define to_lm3560_flash(_ctrl, _no) \
73 container_of(_ctrl->handler, struct lm3560_flash, ctrls_led[_no])
74
75/* enable mode control */
76static int lm3560_mode_ctrl(struct lm3560_flash *flash)
77{
78 int rval = -EINVAL;
79
80 switch (flash->led_mode) {
81 case V4L2_FLASH_LED_MODE_NONE:
82 rval = regmap_update_bits(flash->regmap,
83 REG_ENABLE, 0x03, MODE_SHDN);
84 break;
85 case V4L2_FLASH_LED_MODE_TORCH:
86 rval = regmap_update_bits(flash->regmap,
87 REG_ENABLE, 0x03, MODE_TORCH);
88 break;
89 case V4L2_FLASH_LED_MODE_FLASH:
90 rval = regmap_update_bits(flash->regmap,
91 REG_ENABLE, 0x03, MODE_FLASH);
92 break;
93 }
94 return rval;
95}
96
d1166b0f 97/* led1/2 enable/disable */
7f6b11a1
DJ
98static int lm3560_enable_ctrl(struct lm3560_flash *flash,
99 enum lm3560_led_id led_no, bool on)
100{
101 int rval;
102
103 if (led_no == LM3560_LED0) {
afb666d1 104 if (on)
7f6b11a1
DJ
105 rval = regmap_update_bits(flash->regmap,
106 REG_ENABLE, 0x08, 0x08);
107 else
108 rval = regmap_update_bits(flash->regmap,
109 REG_ENABLE, 0x08, 0x00);
110 } else {
afb666d1 111 if (on)
7f6b11a1
DJ
112 rval = regmap_update_bits(flash->regmap,
113 REG_ENABLE, 0x10, 0x10);
114 else
115 rval = regmap_update_bits(flash->regmap,
116 REG_ENABLE, 0x10, 0x00);
117 }
118 return rval;
119}
120
121/* torch1/2 brightness control */
122static int lm3560_torch_brt_ctrl(struct lm3560_flash *flash,
123 enum lm3560_led_id led_no, unsigned int brt)
124{
125 int rval;
126 u8 br_bits;
127
128 if (brt < LM3560_TORCH_BRT_MIN)
129 return lm3560_enable_ctrl(flash, led_no, false);
130 else
131 rval = lm3560_enable_ctrl(flash, led_no, true);
132
133 br_bits = LM3560_TORCH_BRT_uA_TO_REG(brt);
134 if (led_no == LM3560_LED0)
135 rval = regmap_update_bits(flash->regmap,
136 REG_TORCH_BR, 0x07, br_bits);
137 else
138 rval = regmap_update_bits(flash->regmap,
139 REG_TORCH_BR, 0x38, br_bits << 3);
140
141 return rval;
142}
143
144/* flash1/2 brightness control */
145static int lm3560_flash_brt_ctrl(struct lm3560_flash *flash,
146 enum lm3560_led_id led_no, unsigned int brt)
147{
148 int rval;
149 u8 br_bits;
150
151 if (brt < LM3560_FLASH_BRT_MIN)
152 return lm3560_enable_ctrl(flash, led_no, false);
153 else
154 rval = lm3560_enable_ctrl(flash, led_no, true);
155
156 br_bits = LM3560_FLASH_BRT_uA_TO_REG(brt);
157 if (led_no == LM3560_LED0)
158 rval = regmap_update_bits(flash->regmap,
159 REG_FLASH_BR, 0x0f, br_bits);
160 else
161 rval = regmap_update_bits(flash->regmap,
162 REG_FLASH_BR, 0xf0, br_bits << 4);
163
164 return rval;
165}
166
d1166b0f 167/* v4l2 controls */
7f6b11a1
DJ
168static int lm3560_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
169{
170 struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
e1c759bd 171 int rval = -EINVAL;
7f6b11a1
DJ
172
173 mutex_lock(&flash->lock);
174
175 if (ctrl->id == V4L2_CID_FLASH_FAULT) {
7f6b11a1
DJ
176 s32 fault = 0;
177 unsigned int reg_val;
178 rval = regmap_read(flash->regmap, REG_FLAG, &reg_val);
179 if (rval < 0)
e1c759bd
DJ
180 goto out;
181 if (reg_val & FAULT_SHORT_CIRCUIT)
7f6b11a1 182 fault |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
e1c759bd 183 if (reg_val & FAULT_OVERTEMP)
7f6b11a1 184 fault |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
e1c759bd 185 if (reg_val & FAULT_TIMEOUT)
7f6b11a1
DJ
186 fault |= V4L2_FLASH_FAULT_TIMEOUT;
187 ctrl->cur.val = fault;
7f6b11a1
DJ
188 }
189
e1c759bd 190out:
7f6b11a1 191 mutex_unlock(&flash->lock);
e1c759bd 192 return rval;
7f6b11a1
DJ
193}
194
195static int lm3560_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
196{
197 struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
198 u8 tout_bits;
199 int rval = -EINVAL;
200
201 mutex_lock(&flash->lock);
202
203 switch (ctrl->id) {
204 case V4L2_CID_FLASH_LED_MODE:
205 flash->led_mode = ctrl->val;
206 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
207 rval = lm3560_mode_ctrl(flash);
208 break;
209
210 case V4L2_CID_FLASH_STROBE_SOURCE:
211 rval = regmap_update_bits(flash->regmap,
212 REG_CONFIG1, 0x04, (ctrl->val) << 2);
213 if (rval < 0)
214 goto err_out;
215 break;
216
217 case V4L2_CID_FLASH_STROBE:
eed8c3ee
WY
218 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
219 rval = -EBUSY;
220 goto err_out;
221 }
7f6b11a1
DJ
222 flash->led_mode = V4L2_FLASH_LED_MODE_FLASH;
223 rval = lm3560_mode_ctrl(flash);
224 break;
225
226 case V4L2_CID_FLASH_STROBE_STOP:
eed8c3ee
WY
227 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
228 rval = -EBUSY;
229 goto err_out;
230 }
7f6b11a1
DJ
231 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
232 rval = lm3560_mode_ctrl(flash);
233 break;
234
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);
239 break;
240
241 case V4L2_CID_FLASH_INTENSITY:
242 rval = lm3560_flash_brt_ctrl(flash, led_no, ctrl->val);
243 break;
244
245 case V4L2_CID_FLASH_TORCH_INTENSITY:
246 rval = lm3560_torch_brt_ctrl(flash, led_no, ctrl->val);
247 break;
248 }
249
7f6b11a1 250err_out:
eed8c3ee 251 mutex_unlock(&flash->lock);
7f6b11a1
DJ
252 return rval;
253}
254
255static int lm3560_led1_get_ctrl(struct v4l2_ctrl *ctrl)
256{
257 return lm3560_get_ctrl(ctrl, LM3560_LED1);
258}
259
260static int lm3560_led1_set_ctrl(struct v4l2_ctrl *ctrl)
261{
262 return lm3560_set_ctrl(ctrl, LM3560_LED1);
263}
264
265static int lm3560_led0_get_ctrl(struct v4l2_ctrl *ctrl)
266{
267 return lm3560_get_ctrl(ctrl, LM3560_LED0);
268}
269
270static int lm3560_led0_set_ctrl(struct v4l2_ctrl *ctrl)
271{
272 return lm3560_set_ctrl(ctrl, LM3560_LED0);
273}
274
275static const struct v4l2_ctrl_ops lm3560_led_ctrl_ops[LM3560_LED_MAX] = {
276 [LM3560_LED0] = {
277 .g_volatile_ctrl = lm3560_led0_get_ctrl,
278 .s_ctrl = lm3560_led0_set_ctrl,
279 },
280 [LM3560_LED1] = {
281 .g_volatile_ctrl = lm3560_led1_get_ctrl,
282 .s_ctrl = lm3560_led1_set_ctrl,
283 }
284};
285
286static int lm3560_init_controls(struct lm3560_flash *flash,
287 enum lm3560_led_id led_no)
288{
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];
294
295 v4l2_ctrl_handler_init(hdl, 8);
d1166b0f 296
7f6b11a1
DJ
297 /* flash mode */
298 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE,
299 V4L2_FLASH_LED_MODE_TORCH, ~0x7,
300 V4L2_FLASH_LED_MODE_NONE);
301 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
302
303 /* flash source */
304 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
305 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
306
307 /* flash strobe */
308 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
d1166b0f 309
7f6b11a1
DJ
310 /* flash strobe stop */
311 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
312
313 /* flash strobe timeout */
314 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
315 LM3560_FLASH_TOUT_MIN,
316 flash->pdata->max_flash_timeout,
317 LM3560_FLASH_TOUT_STEP,
318 flash->pdata->max_flash_timeout);
319
320 /* flash brt */
321 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
322 LM3560_FLASH_BRT_MIN, max_flash_brt,
323 LM3560_FLASH_BRT_STEP, max_flash_brt);
324
325 /* torch brt */
326 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
327 LM3560_TORCH_BRT_MIN, max_torch_brt,
328 LM3560_TORCH_BRT_STEP, max_torch_brt);
329
330 /* fault */
331 fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
332 V4L2_FLASH_FAULT_OVER_VOLTAGE
333 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
334 | V4L2_FLASH_FAULT_SHORT_CIRCUIT
335 | V4L2_FLASH_FAULT_TIMEOUT, 0, 0);
336 if (fault != NULL)
337 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
338
339 if (hdl->error)
340 return hdl->error;
341
342 flash->subdev_led[led_no].ctrl_handler = hdl;
343 return 0;
344}
345
346/* initialize device */
347static const struct v4l2_subdev_ops lm3560_ops = {
348 .core = NULL,
349};
350
351static const struct regmap_config lm3560_regmap = {
352 .reg_bits = 8,
353 .val_bits = 8,
354 .max_register = 0xFF,
355};
356
357static int lm3560_subdev_init(struct lm3560_flash *flash,
358 enum lm3560_led_id led_no, char *led_name)
359{
360 struct i2c_client *client = to_i2c_client(flash->dev);
361 int rval;
362
363 v4l2_i2c_subdev_init(&flash->subdev_led[led_no], client, &lm3560_ops);
364 flash->subdev_led[led_no].flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
cc1e6315
MCC
365 strscpy(flash->subdev_led[led_no].name, led_name,
366 sizeof(flash->subdev_led[led_no].name));
7f6b11a1
DJ
367 rval = lm3560_init_controls(flash, led_no);
368 if (rval)
369 goto err_out;
ab22e77c 370 rval = media_entity_pads_init(&flash->subdev_led[led_no].entity, 0, NULL);
7f6b11a1
DJ
371 if (rval < 0)
372 goto err_out;
4ca72efa 373 flash->subdev_led[led_no].entity.function = MEDIA_ENT_F_FLASH;
7f6b11a1
DJ
374
375 return rval;
376
377err_out:
378 v4l2_ctrl_handler_free(&flash->ctrls_led[led_no]);
379 return rval;
380}
381
382static int lm3560_init_device(struct lm3560_flash *flash)
383{
384 int rval;
385 unsigned int reg_val;
386
387 /* set peak current */
388 rval = regmap_update_bits(flash->regmap,
389 REG_FLASH_TOUT, 0x60, flash->pdata->peak);
390 if (rval < 0)
391 return rval;
392 /* output disable */
393 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
394 rval = lm3560_mode_ctrl(flash);
395 if (rval < 0)
396 return rval;
d1166b0f 397 /* reset faults */
7f6b11a1
DJ
398 rval = regmap_read(flash->regmap, REG_FLAG, &reg_val);
399 return rval;
400}
401
402static int lm3560_probe(struct i2c_client *client,
403 const struct i2c_device_id *devid)
404{
405 struct lm3560_flash *flash;
406 struct lm3560_platform_data *pdata = dev_get_platdata(&client->dev);
407 int rval;
408
409 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
410 if (flash == NULL)
411 return -ENOMEM;
412
413 flash->regmap = devm_regmap_init_i2c(client, &lm3560_regmap);
414 if (IS_ERR(flash->regmap)) {
415 rval = PTR_ERR(flash->regmap);
416 return rval;
417 }
418
419 /* if there is no platform data, use chip default value */
420 if (pdata == NULL) {
341ef565 421 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
7f6b11a1
DJ
422 if (pdata == NULL)
423 return -ENODEV;
424 pdata->peak = LM3560_PEAK_3600mA;
425 pdata->max_flash_timeout = LM3560_FLASH_TOUT_MAX;
426 /* led 1 */
427 pdata->max_flash_brt[LM3560_LED0] = LM3560_FLASH_BRT_MAX;
428 pdata->max_torch_brt[LM3560_LED0] = LM3560_TORCH_BRT_MAX;
429 /* led 2 */
430 pdata->max_flash_brt[LM3560_LED1] = LM3560_FLASH_BRT_MAX;
431 pdata->max_torch_brt[LM3560_LED1] = LM3560_TORCH_BRT_MAX;
432 }
433 flash->pdata = pdata;
434 flash->dev = &client->dev;
435 mutex_init(&flash->lock);
436
437 rval = lm3560_subdev_init(flash, LM3560_LED0, "lm3560-led0");
438 if (rval < 0)
439 return rval;
440
441 rval = lm3560_subdev_init(flash, LM3560_LED1, "lm3560-led1");
442 if (rval < 0)
443 return rval;
444
445 rval = lm3560_init_device(flash);
446 if (rval < 0)
447 return rval;
448
cc58f4db
WY
449 i2c_set_clientdata(client, flash);
450
7f6b11a1
DJ
451 return 0;
452}
453
454static int lm3560_remove(struct i2c_client *client)
455{
cc58f4db 456 struct lm3560_flash *flash = i2c_get_clientdata(client);
7f6b11a1
DJ
457 unsigned int i;
458
459 for (i = LM3560_LED0; i < LM3560_LED_MAX; i++) {
460 v4l2_device_unregister_subdev(&flash->subdev_led[i]);
461 v4l2_ctrl_handler_free(&flash->ctrls_led[i]);
462 media_entity_cleanup(&flash->subdev_led[i].entity);
463 }
464
465 return 0;
466}
467
468static const struct i2c_device_id lm3560_id_table[] = {
653d500c 469 {LM3559_NAME, 0},
7f6b11a1
DJ
470 {LM3560_NAME, 0},
471 {}
472};
473
474MODULE_DEVICE_TABLE(i2c, lm3560_id_table);
475
476static struct i2c_driver lm3560_i2c_driver = {
477 .driver = {
478 .name = LM3560_NAME,
479 .pm = NULL,
480 },
481 .probe = lm3560_probe,
482 .remove = lm3560_remove,
483 .id_table = lm3560_id_table,
484};
485
486module_i2c_driver(lm3560_i2c_driver);
487
488MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
489MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
490MODULE_DESCRIPTION("Texas Instruments LM3560 LED flash driver");
491MODULE_LICENSE("GPL");