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