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