xref: /openbmc/linux/drivers/media/i2c/lm3560.c (revision c819e2cf)
1 /*
2  * drivers/media/i2c/lm3560.c
3  * General device driver for TI lm3560, FLASH LED Driver
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.
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>
27 #include <media/lm3560.h>
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 
39 /* fault mask */
40 #define FAULT_TIMEOUT	(1<<0)
41 #define FAULT_OVERTEMP	(1<<1)
42 #define FAULT_SHORT_CIRCUIT	(1<<2)
43 
44 enum led_enable {
45 	MODE_SHDN = 0x0,
46 	MODE_TORCH = 0x2,
47 	MODE_FLASH = 0x3,
48 };
49 
50 /**
51  * struct lm3560_flash
52  *
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
59  */
60 struct lm3560_flash {
61 	struct device *dev;
62 	struct lm3560_platform_data *pdata;
63 	struct regmap *regmap;
64 	struct mutex lock;
65 
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];
69 };
70 
71 #define to_lm3560_flash(_ctrl, _no)	\
72 	container_of(_ctrl->handler, struct lm3560_flash, ctrls_led[_no])
73 
74 /* enable mode control */
75 static int lm3560_mode_ctrl(struct lm3560_flash *flash)
76 {
77 	int rval = -EINVAL;
78 
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;
92 	}
93 	return rval;
94 }
95 
96 /* led1/2 enable/disable */
97 static int lm3560_enable_ctrl(struct lm3560_flash *flash,
98 			      enum lm3560_led_id led_no, bool on)
99 {
100 	int rval;
101 
102 	if (led_no == LM3560_LED0) {
103 		if (on)
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)
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);
116 	}
117 	return rval;
118 }
119 
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)
123 {
124 	int rval;
125 	u8 br_bits;
126 
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);
131 
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);
139 
140 	return rval;
141 }
142 
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)
146 {
147 	int rval;
148 	u8 br_bits;
149 
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);
154 
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);
162 
163 	return rval;
164 }
165 
166 /* v4l2 controls  */
167 static int lm3560_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
168 {
169 	struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
170 	int rval = -EINVAL;
171 
172 	mutex_lock(&flash->lock);
173 
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;
187 	}
188 
189 out:
190 	mutex_unlock(&flash->lock);
191 	return rval;
192 }
193 
194 static int lm3560_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
195 {
196 	struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
197 	u8 tout_bits;
198 	int rval = -EINVAL;
199 
200 	mutex_lock(&flash->lock);
201 
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;
208 
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;
215 
216 	case V4L2_CID_FLASH_STROBE:
217 		if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
218 			rval = -EBUSY;
219 			goto err_out;
220 		}
221 		flash->led_mode = V4L2_FLASH_LED_MODE_FLASH;
222 		rval = lm3560_mode_ctrl(flash);
223 		break;
224 
225 	case V4L2_CID_FLASH_STROBE_STOP:
226 		if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
227 			rval = -EBUSY;
228 			goto err_out;
229 		}
230 		flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
231 		rval = lm3560_mode_ctrl(flash);
232 		break;
233 
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;
239 
240 	case V4L2_CID_FLASH_INTENSITY:
241 		rval = lm3560_flash_brt_ctrl(flash, led_no, ctrl->val);
242 		break;
243 
244 	case V4L2_CID_FLASH_TORCH_INTENSITY:
245 		rval = lm3560_torch_brt_ctrl(flash, led_no, ctrl->val);
246 		break;
247 	}
248 
249 err_out:
250 	mutex_unlock(&flash->lock);
251 	return rval;
252 }
253 
254 static int lm3560_led1_get_ctrl(struct v4l2_ctrl *ctrl)
255 {
256 	return lm3560_get_ctrl(ctrl, LM3560_LED1);
257 }
258 
259 static int lm3560_led1_set_ctrl(struct v4l2_ctrl *ctrl)
260 {
261 	return lm3560_set_ctrl(ctrl, LM3560_LED1);
262 }
263 
264 static int lm3560_led0_get_ctrl(struct v4l2_ctrl *ctrl)
265 {
266 	return lm3560_get_ctrl(ctrl, LM3560_LED0);
267 }
268 
269 static int lm3560_led0_set_ctrl(struct v4l2_ctrl *ctrl)
270 {
271 	return lm3560_set_ctrl(ctrl, LM3560_LED0);
272 }
273 
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,
278 			 },
279 	[LM3560_LED1] = {
280 			 .g_volatile_ctrl = lm3560_led1_get_ctrl,
281 			 .s_ctrl = lm3560_led1_set_ctrl,
282 			 }
283 };
284 
285 static int lm3560_init_controls(struct lm3560_flash *flash,
286 				enum lm3560_led_id led_no)
287 {
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];
293 
294 	v4l2_ctrl_handler_init(hdl, 8);
295 
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;
301 
302 	/* flash source */
303 	v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
304 			       0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
305 
306 	/* flash strobe */
307 	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
308 
309 	/* flash strobe stop */
310 	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
311 
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);
318 
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);
323 
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);
328 
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;
337 
338 	if (hdl->error)
339 		return hdl->error;
340 
341 	flash->subdev_led[led_no].ctrl_handler = hdl;
342 	return 0;
343 }
344 
345 /* initialize device */
346 static const struct v4l2_subdev_ops lm3560_ops = {
347 	.core = NULL,
348 };
349 
350 static const struct regmap_config lm3560_regmap = {
351 	.reg_bits = 8,
352 	.val_bits = 8,
353 	.max_register = 0xFF,
354 };
355 
356 static int lm3560_subdev_init(struct lm3560_flash *flash,
357 			      enum lm3560_led_id led_no, char *led_name)
358 {
359 	struct i2c_client *client = to_i2c_client(flash->dev);
360 	int rval;
361 
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;
372 
373 	return rval;
374 
375 err_out:
376 	v4l2_ctrl_handler_free(&flash->ctrls_led[led_no]);
377 	return rval;
378 }
379 
380 static int lm3560_init_device(struct lm3560_flash *flash)
381 {
382 	int rval;
383 	unsigned int reg_val;
384 
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;
398 }
399 
400 static int lm3560_probe(struct i2c_client *client,
401 			const struct i2c_device_id *devid)
402 {
403 	struct lm3560_flash *flash;
404 	struct lm3560_platform_data *pdata = dev_get_platdata(&client->dev);
405 	int rval;
406 
407 	flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
408 	if (flash == NULL)
409 		return -ENOMEM;
410 
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;
415 	}
416 
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;
430 	}
431 	flash->pdata = pdata;
432 	flash->dev = &client->dev;
433 	mutex_init(&flash->lock);
434 
435 	rval = lm3560_subdev_init(flash, LM3560_LED0, "lm3560-led0");
436 	if (rval < 0)
437 		return rval;
438 
439 	rval = lm3560_subdev_init(flash, LM3560_LED1, "lm3560-led1");
440 	if (rval < 0)
441 		return rval;
442 
443 	rval = lm3560_init_device(flash);
444 	if (rval < 0)
445 		return rval;
446 
447 	i2c_set_clientdata(client, flash);
448 
449 	return 0;
450 }
451 
452 static int lm3560_remove(struct i2c_client *client)
453 {
454 	struct lm3560_flash *flash = i2c_get_clientdata(client);
455 	unsigned int i;
456 
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);
461 	}
462 
463 	return 0;
464 }
465 
466 static const struct i2c_device_id lm3560_id_table[] = {
467 	{LM3560_NAME, 0},
468 	{}
469 };
470 
471 MODULE_DEVICE_TABLE(i2c, lm3560_id_table);
472 
473 static struct i2c_driver lm3560_i2c_driver = {
474 	.driver = {
475 		   .name = LM3560_NAME,
476 		   .pm = NULL,
477 		   },
478 	.probe = lm3560_probe,
479 	.remove = lm3560_remove,
480 	.id_table = lm3560_id_table,
481 };
482 
483 module_i2c_driver(lm3560_i2c_driver);
484 
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");
489