1 /*
2  * LED flash driver for LM3554
3  *
4  * Copyright (c) 2010-2012 Intel Corporation. All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version
8  * 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  *
16  */
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/mutex.h>
20 #include <linux/delay.h>
21 #include <linux/gpio.h>
22 #include <linux/slab.h>
23 
24 #include "../include/media/lm3554.h"
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
27 #include <linux/acpi.h>
28 #include <linux/gpio/consumer.h>
29 #include "../include/linux/atomisp_gmin_platform.h"
30 #include "../include/linux/atomisp.h"
31 
32 /* Registers */
33 
34 #define LM3554_TORCH_BRIGHTNESS_REG	0xA0
35 #define LM3554_TORCH_MODE_SHIFT		0
36 #define LM3554_TORCH_CURRENT_SHIFT	3
37 #define LM3554_INDICATOR_CURRENT_SHIFT	6
38 
39 #define LM3554_FLASH_BRIGHTNESS_REG	0xB0
40 #define LM3554_FLASH_MODE_SHIFT		0
41 #define LM3554_FLASH_CURRENT_SHIFT	3
42 #define LM3554_STROBE_SENSITIVITY_SHIFT	7
43 
44 #define LM3554_FLASH_DURATION_REG	0xC0
45 #define LM3554_FLASH_TIMEOUT_SHIFT	0
46 #define LM3554_CURRENT_LIMIT_SHIFT	5
47 
48 #define LM3554_FLAGS_REG		0xD0
49 #define LM3554_FLAG_TIMEOUT		BIT(0)
50 #define LM3554_FLAG_THERMAL_SHUTDOWN	BIT(1)
51 #define LM3554_FLAG_LED_FAULT		BIT(2)
52 #define LM3554_FLAG_TX1_INTERRUPT	BIT(3)
53 #define LM3554_FLAG_TX2_INTERRUPT	BIT(4)
54 #define LM3554_FLAG_LED_THERMAL_FAULT	BIT(5)
55 #define LM3554_FLAG_UNUSED		BIT(6)
56 #define LM3554_FLAG_INPUT_VOLTAGE_LOW	BIT(7)
57 
58 #define LM3554_CONFIG_REG_1		0xE0
59 #define LM3554_ENVM_TX2_SHIFT		5
60 #define LM3554_TX2_POLARITY_SHIFT	6
61 
62 struct lm3554 {
63 	struct v4l2_subdev sd;
64 
65 	struct mutex power_lock;
66 	struct v4l2_ctrl_handler ctrl_handler;
67 	int power_count;
68 
69 	unsigned int mode;
70 	int timeout;
71 	u8 torch_current;
72 	u8 indicator_current;
73 	u8 flash_current;
74 
75 	struct timer_list flash_off_delay;
76 	struct lm3554_platform_data *pdata;
77 };
78 
79 #define to_lm3554(p_sd)	container_of(p_sd, struct lm3554, sd)
80 
81 /* Return negative errno else zero on success */
82 static int lm3554_write(struct lm3554 *flash, u8 addr, u8 val)
83 {
84 	struct i2c_client *client = v4l2_get_subdevdata(&flash->sd);
85 	int ret;
86 
87 	ret = i2c_smbus_write_byte_data(client, addr, val);
88 
89 	dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
90 		ret < 0 ? "fail" : "ok");
91 
92 	return ret;
93 }
94 
95 /* Return negative errno else a data byte received from the device. */
96 static int lm3554_read(struct lm3554 *flash, u8 addr)
97 {
98 	struct i2c_client *client = v4l2_get_subdevdata(&flash->sd);
99 	int ret;
100 
101 	ret = i2c_smbus_read_byte_data(client, addr);
102 
103 	dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, ret,
104 		ret < 0 ? "fail" : "ok");
105 
106 	return ret;
107 }
108 
109 /* -----------------------------------------------------------------------------
110  * Hardware configuration
111  */
112 
113 static int lm3554_set_mode(struct lm3554 *flash, unsigned int mode)
114 {
115 	u8 val;
116 	int ret;
117 
118 	val = (mode << LM3554_FLASH_MODE_SHIFT) |
119 	      (flash->flash_current << LM3554_FLASH_CURRENT_SHIFT);
120 
121 	ret = lm3554_write(flash, LM3554_FLASH_BRIGHTNESS_REG, val);
122 	if (ret == 0)
123 		flash->mode = mode;
124 	return ret;
125 }
126 
127 static int lm3554_set_torch(struct lm3554 *flash)
128 {
129 	u8 val;
130 
131 	val = (flash->mode << LM3554_TORCH_MODE_SHIFT) |
132 	      (flash->torch_current << LM3554_TORCH_CURRENT_SHIFT) |
133 	      (flash->indicator_current << LM3554_INDICATOR_CURRENT_SHIFT);
134 
135 	return lm3554_write(flash, LM3554_TORCH_BRIGHTNESS_REG, val);
136 }
137 
138 static int lm3554_set_flash(struct lm3554 *flash)
139 {
140 	u8 val;
141 
142 	val = (flash->mode << LM3554_FLASH_MODE_SHIFT) |
143 	      (flash->flash_current << LM3554_FLASH_CURRENT_SHIFT);
144 
145 	return lm3554_write(flash, LM3554_FLASH_BRIGHTNESS_REG, val);
146 }
147 
148 static int lm3554_set_duration(struct lm3554 *flash)
149 {
150 	u8 val;
151 
152 	val = (flash->timeout << LM3554_FLASH_TIMEOUT_SHIFT) |
153 	      (flash->pdata->current_limit << LM3554_CURRENT_LIMIT_SHIFT);
154 
155 	return lm3554_write(flash, LM3554_FLASH_DURATION_REG, val);
156 }
157 
158 static int lm3554_set_config1(struct lm3554 *flash)
159 {
160 	u8 val;
161 
162 	val = (flash->pdata->envm_tx2 << LM3554_ENVM_TX2_SHIFT) |
163 	      (flash->pdata->tx2_polarity << LM3554_TX2_POLARITY_SHIFT);
164 	return lm3554_write(flash, LM3554_CONFIG_REG_1, val);
165 }
166 
167 /* -----------------------------------------------------------------------------
168  * Hardware trigger
169  */
170 static void lm3554_flash_off_delay(struct timer_list *t)
171 {
172 	struct lm3554 *flash = from_timer(flash, t, flash_off_delay);
173 	struct lm3554_platform_data *pdata = flash->pdata;
174 
175 	gpio_set_value(pdata->gpio_strobe, 0);
176 }
177 
178 static int lm3554_hw_strobe(struct i2c_client *client, bool strobe)
179 {
180 	int ret, timer_pending;
181 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
182 	struct lm3554 *flash = to_lm3554(sd);
183 	struct lm3554_platform_data *pdata = flash->pdata;
184 
185 	/*
186 	 * An abnormal high flash current is observed when strobe off the
187 	 * flash. Workaround here is firstly set flash current to lower level,
188 	 * wait a short moment, and then strobe off the flash.
189 	 */
190 
191 	timer_pending = del_timer_sync(&flash->flash_off_delay);
192 
193 	/* Flash off */
194 	if (!strobe) {
195 		/* set current to 70mA and wait a while */
196 		ret = lm3554_write(flash, LM3554_FLASH_BRIGHTNESS_REG, 0);
197 		if (ret < 0)
198 			goto err;
199 		mod_timer(&flash->flash_off_delay,
200 			  jiffies + msecs_to_jiffies(LM3554_TIMER_DELAY));
201 		return 0;
202 	}
203 
204 	/* Flash on */
205 
206 	/*
207 	 * If timer is killed before run, flash is not strobe off,
208 	 * so must strobe off here
209 	 */
210 	if (timer_pending)
211 		gpio_set_value(pdata->gpio_strobe, 0);
212 
213 	/* Restore flash current settings */
214 	ret = lm3554_set_flash(flash);
215 	if (ret < 0)
216 		goto err;
217 
218 	/* Strobe on Flash */
219 	gpio_set_value(pdata->gpio_strobe, 1);
220 
221 	return 0;
222 err:
223 	dev_err(&client->dev, "failed to %s flash strobe (%d)\n",
224 		strobe ? "on" : "off", ret);
225 	return ret;
226 }
227 
228 /* -----------------------------------------------------------------------------
229  * V4L2 controls
230  */
231 
232 static int lm3554_read_status(struct lm3554 *flash)
233 {
234 	int ret;
235 	struct i2c_client *client = v4l2_get_subdevdata(&flash->sd);
236 
237 	/* NOTE: reading register clear fault status */
238 	ret = lm3554_read(flash, LM3554_FLAGS_REG);
239 	if (ret < 0)
240 		return ret;
241 
242 	/*
243 	 * Accordingly to datasheet we read back '1' in bit 6.
244 	 * Clear it first.
245 	 */
246 	ret &= ~LM3554_FLAG_UNUSED;
247 
248 	/*
249 	 * Do not take TX1/TX2 signal as an error
250 	 * because MSIC will not turn off flash, but turn to
251 	 * torch mode according to gsm modem signal by hardware.
252 	 */
253 	ret &= ~(LM3554_FLAG_TX1_INTERRUPT | LM3554_FLAG_TX2_INTERRUPT);
254 
255 	if (ret > 0)
256 		dev_dbg(&client->dev, "LM3554 flag status: %02x\n", ret);
257 
258 	return ret;
259 }
260 
261 static int lm3554_s_flash_timeout(struct v4l2_subdev *sd, u32 val)
262 {
263 	struct lm3554 *flash = to_lm3554(sd);
264 
265 	val = clamp(val, LM3554_MIN_TIMEOUT, LM3554_MAX_TIMEOUT);
266 	val = val / LM3554_TIMEOUT_STEPSIZE - 1;
267 
268 	flash->timeout = val;
269 
270 	return lm3554_set_duration(flash);
271 }
272 
273 static int lm3554_g_flash_timeout(struct v4l2_subdev *sd, s32 *val)
274 {
275 	struct lm3554 *flash = to_lm3554(sd);
276 
277 	*val = (u32)(flash->timeout + 1) * LM3554_TIMEOUT_STEPSIZE;
278 
279 	return 0;
280 }
281 
282 static int lm3554_s_flash_intensity(struct v4l2_subdev *sd, u32 intensity)
283 {
284 	struct lm3554 *flash = to_lm3554(sd);
285 
286 	intensity = LM3554_CLAMP_PERCENTAGE(intensity);
287 	intensity = LM3554_PERCENT_TO_VALUE(intensity, LM3554_FLASH_STEP);
288 
289 	flash->flash_current = intensity;
290 
291 	return lm3554_set_flash(flash);
292 }
293 
294 static int lm3554_g_flash_intensity(struct v4l2_subdev *sd, s32 *val)
295 {
296 	struct lm3554 *flash = to_lm3554(sd);
297 
298 	*val = LM3554_VALUE_TO_PERCENT((u32)flash->flash_current,
299 				       LM3554_FLASH_STEP);
300 
301 	return 0;
302 }
303 
304 static int lm3554_s_torch_intensity(struct v4l2_subdev *sd, u32 intensity)
305 {
306 	struct lm3554 *flash = to_lm3554(sd);
307 
308 	intensity = LM3554_CLAMP_PERCENTAGE(intensity);
309 	intensity = LM3554_PERCENT_TO_VALUE(intensity, LM3554_TORCH_STEP);
310 
311 	flash->torch_current = intensity;
312 
313 	return lm3554_set_torch(flash);
314 }
315 
316 static int lm3554_g_torch_intensity(struct v4l2_subdev *sd, s32 *val)
317 {
318 	struct lm3554 *flash = to_lm3554(sd);
319 
320 	*val = LM3554_VALUE_TO_PERCENT((u32)flash->torch_current,
321 				       LM3554_TORCH_STEP);
322 
323 	return 0;
324 }
325 
326 static int lm3554_s_indicator_intensity(struct v4l2_subdev *sd, u32 intensity)
327 {
328 	struct lm3554 *flash = to_lm3554(sd);
329 
330 	intensity = LM3554_CLAMP_PERCENTAGE(intensity);
331 	intensity = LM3554_PERCENT_TO_VALUE(intensity, LM3554_INDICATOR_STEP);
332 
333 	flash->indicator_current = intensity;
334 
335 	return lm3554_set_torch(flash);
336 }
337 
338 static int lm3554_g_indicator_intensity(struct v4l2_subdev *sd, s32 *val)
339 {
340 	struct lm3554 *flash = to_lm3554(sd);
341 
342 	*val = LM3554_VALUE_TO_PERCENT((u32)flash->indicator_current,
343 				       LM3554_INDICATOR_STEP);
344 
345 	return 0;
346 }
347 
348 static int lm3554_s_flash_strobe(struct v4l2_subdev *sd, u32 val)
349 {
350 	struct i2c_client *client = v4l2_get_subdevdata(sd);
351 
352 	return lm3554_hw_strobe(client, val);
353 }
354 
355 static int lm3554_s_flash_mode(struct v4l2_subdev *sd, u32 new_mode)
356 {
357 	struct lm3554 *flash = to_lm3554(sd);
358 	unsigned int mode;
359 
360 	switch (new_mode) {
361 	case ATOMISP_FLASH_MODE_OFF:
362 		mode = LM3554_MODE_SHUTDOWN;
363 		break;
364 	case ATOMISP_FLASH_MODE_FLASH:
365 		mode = LM3554_MODE_FLASH;
366 		break;
367 	case ATOMISP_FLASH_MODE_INDICATOR:
368 		mode = LM3554_MODE_INDICATOR;
369 		break;
370 	case ATOMISP_FLASH_MODE_TORCH:
371 		mode = LM3554_MODE_TORCH;
372 		break;
373 	default:
374 		return -EINVAL;
375 	}
376 
377 	return lm3554_set_mode(flash, mode);
378 }
379 
380 static int lm3554_g_flash_mode(struct v4l2_subdev *sd, s32 *val)
381 {
382 	struct lm3554 *flash = to_lm3554(sd);
383 	*val = flash->mode;
384 	return 0;
385 }
386 
387 static int lm3554_g_flash_status(struct v4l2_subdev *sd, s32 *val)
388 {
389 	struct lm3554 *flash = to_lm3554(sd);
390 	int value;
391 
392 	value = lm3554_read_status(flash);
393 	if (value < 0)
394 		return value;
395 
396 	if (value & LM3554_FLAG_TIMEOUT)
397 		*val = ATOMISP_FLASH_STATUS_TIMEOUT;
398 	else if (value > 0)
399 		*val = ATOMISP_FLASH_STATUS_HW_ERROR;
400 	else
401 		*val = ATOMISP_FLASH_STATUS_OK;
402 
403 	return 0;
404 }
405 
406 static int lm3554_g_flash_status_register(struct v4l2_subdev *sd, s32 *val)
407 {
408 	struct lm3554 *flash = to_lm3554(sd);
409 	int ret;
410 
411 	ret = lm3554_read(flash, LM3554_FLAGS_REG);
412 
413 	if (ret < 0)
414 		return ret;
415 
416 	*val = ret;
417 	return 0;
418 }
419 
420 static int lm3554_s_ctrl(struct v4l2_ctrl *ctrl)
421 {
422 	struct lm3554 *dev =
423 	    container_of(ctrl->handler, struct lm3554, ctrl_handler);
424 	int ret = 0;
425 
426 	switch (ctrl->id) {
427 	case V4L2_CID_FLASH_TIMEOUT:
428 		ret = lm3554_s_flash_timeout(&dev->sd, ctrl->val);
429 		break;
430 	case V4L2_CID_FLASH_INTENSITY:
431 		ret = lm3554_s_flash_intensity(&dev->sd, ctrl->val);
432 		break;
433 	case V4L2_CID_FLASH_TORCH_INTENSITY:
434 		ret = lm3554_s_torch_intensity(&dev->sd, ctrl->val);
435 		break;
436 	case V4L2_CID_FLASH_INDICATOR_INTENSITY:
437 		ret = lm3554_s_indicator_intensity(&dev->sd, ctrl->val);
438 		break;
439 	case V4L2_CID_FLASH_STROBE:
440 		ret = lm3554_s_flash_strobe(&dev->sd, ctrl->val);
441 		break;
442 	case V4L2_CID_FLASH_MODE:
443 		ret = lm3554_s_flash_mode(&dev->sd, ctrl->val);
444 		break;
445 	default:
446 		ret = -EINVAL;
447 	}
448 	return ret;
449 }
450 
451 static int lm3554_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
452 {
453 	struct lm3554 *dev =
454 	    container_of(ctrl->handler, struct lm3554, ctrl_handler);
455 	int ret = 0;
456 
457 	switch (ctrl->id) {
458 	case V4L2_CID_FLASH_TIMEOUT:
459 		ret = lm3554_g_flash_timeout(&dev->sd, &ctrl->val);
460 		break;
461 	case V4L2_CID_FLASH_INTENSITY:
462 		ret = lm3554_g_flash_intensity(&dev->sd, &ctrl->val);
463 		break;
464 	case V4L2_CID_FLASH_TORCH_INTENSITY:
465 		ret = lm3554_g_torch_intensity(&dev->sd, &ctrl->val);
466 		break;
467 	case V4L2_CID_FLASH_INDICATOR_INTENSITY:
468 		ret = lm3554_g_indicator_intensity(&dev->sd, &ctrl->val);
469 		break;
470 	case V4L2_CID_FLASH_MODE:
471 		ret = lm3554_g_flash_mode(&dev->sd, &ctrl->val);
472 		break;
473 	case V4L2_CID_FLASH_STATUS:
474 		ret = lm3554_g_flash_status(&dev->sd, &ctrl->val);
475 		break;
476 	case V4L2_CID_FLASH_STATUS_REGISTER:
477 		ret = lm3554_g_flash_status_register(&dev->sd, &ctrl->val);
478 		break;
479 	default:
480 		ret = -EINVAL;
481 	}
482 
483 	return ret;
484 }
485 
486 static const struct v4l2_ctrl_ops ctrl_ops = {
487 	.s_ctrl = lm3554_s_ctrl,
488 	.g_volatile_ctrl = lm3554_g_volatile_ctrl
489 };
490 
491 static const struct v4l2_ctrl_config lm3554_controls[] = {
492 	{
493 		.ops = &ctrl_ops,
494 		.id = V4L2_CID_FLASH_TIMEOUT,
495 		.type = V4L2_CTRL_TYPE_INTEGER,
496 		.name = "Flash Timeout",
497 		.min = 0x0,
498 		.max = LM3554_MAX_TIMEOUT,
499 		.step = 0x01,
500 		.def = LM3554_DEFAULT_TIMEOUT,
501 		.flags = 0,
502 	},
503 	{
504 		.ops = &ctrl_ops,
505 		.id = V4L2_CID_FLASH_INTENSITY,
506 		.type = V4L2_CTRL_TYPE_INTEGER,
507 		.name = "Flash Intensity",
508 		.min = LM3554_MIN_PERCENT,
509 		.max = LM3554_MAX_PERCENT,
510 		.step = 0x01,
511 		.def = LM3554_FLASH_DEFAULT_BRIGHTNESS,
512 		.flags = 0,
513 	},
514 	{
515 		.ops = &ctrl_ops,
516 		.id = V4L2_CID_FLASH_TORCH_INTENSITY,
517 		.type = V4L2_CTRL_TYPE_INTEGER,
518 		.name = "Torch Intensity",
519 		.min = LM3554_MIN_PERCENT,
520 		.max = LM3554_MAX_PERCENT,
521 		.step = 0x01,
522 		.def = LM3554_TORCH_DEFAULT_BRIGHTNESS,
523 		.flags = 0,
524 	},
525 	{
526 		.ops = &ctrl_ops,
527 		.id = V4L2_CID_FLASH_INDICATOR_INTENSITY,
528 		.type = V4L2_CTRL_TYPE_INTEGER,
529 		.name = "Indicator Intensity",
530 		.min = LM3554_MIN_PERCENT,
531 		.max = LM3554_MAX_PERCENT,
532 		.step = 0x01,
533 		.def = LM3554_INDICATOR_DEFAULT_BRIGHTNESS,
534 		.flags = 0,
535 	},
536 	{
537 		.ops = &ctrl_ops,
538 		.id = V4L2_CID_FLASH_STROBE,
539 		.type = V4L2_CTRL_TYPE_BOOLEAN,
540 		.name = "Flash Strobe",
541 		.min = 0,
542 		.max = 1,
543 		.step = 1,
544 		.def = 0,
545 		.flags = 0,
546 	},
547 	{
548 		.ops = &ctrl_ops,
549 		.id = V4L2_CID_FLASH_MODE,
550 		.type = V4L2_CTRL_TYPE_INTEGER,
551 		.name = "Flash Mode",
552 		.min = 0,
553 		.max = 100,
554 		.step = 1,
555 		.def = ATOMISP_FLASH_MODE_OFF,
556 		.flags = 0,
557 	},
558 	{
559 		.ops = &ctrl_ops,
560 		.id = V4L2_CID_FLASH_STATUS,
561 		.type = V4L2_CTRL_TYPE_INTEGER,
562 		.name = "Flash Status",
563 		.min = ATOMISP_FLASH_STATUS_OK,
564 		.max = ATOMISP_FLASH_STATUS_TIMEOUT,
565 		.step = 1,
566 		.def = ATOMISP_FLASH_STATUS_OK,
567 		.flags = 0,
568 	},
569 	{
570 		.ops = &ctrl_ops,
571 		.id = V4L2_CID_FLASH_STATUS_REGISTER,
572 		.type = V4L2_CTRL_TYPE_INTEGER,
573 		.name = "Flash Status Register",
574 		.min = 0,
575 		.max = 255,
576 		.step = 1,
577 		.def = 0,
578 		.flags = 0,
579 	},
580 };
581 
582 /* -----------------------------------------------------------------------------
583  * V4L2 subdev core operations
584  */
585 
586 /* Put device into known state. */
587 static int lm3554_setup(struct lm3554 *flash)
588 {
589 	struct i2c_client *client = v4l2_get_subdevdata(&flash->sd);
590 	int ret;
591 
592 	/* clear the flags register */
593 	ret = lm3554_read(flash, LM3554_FLAGS_REG);
594 	if (ret < 0)
595 		return ret;
596 
597 	dev_dbg(&client->dev, "Fault info: %02x\n", ret);
598 
599 	ret = lm3554_set_config1(flash);
600 	if (ret < 0)
601 		return ret;
602 
603 	ret = lm3554_set_duration(flash);
604 	if (ret < 0)
605 		return ret;
606 
607 	ret = lm3554_set_torch(flash);
608 	if (ret < 0)
609 		return ret;
610 
611 	ret = lm3554_set_flash(flash);
612 	if (ret < 0)
613 		return ret;
614 
615 	/* read status */
616 	ret = lm3554_read_status(flash);
617 	if (ret < 0)
618 		return ret;
619 
620 	return ret ? -EIO : 0;
621 }
622 
623 static int __lm3554_s_power(struct lm3554 *flash, int power)
624 {
625 	struct lm3554_platform_data *pdata = flash->pdata;
626 	int ret;
627 
628 	/*initialize flash driver*/
629 	gpio_set_value(pdata->gpio_reset, power);
630 	usleep_range(100, 100 + 1);
631 
632 	if (power) {
633 		/* Setup default values. This makes sure that the chip
634 		 * is in a known state.
635 		 */
636 		ret = lm3554_setup(flash);
637 		if (ret < 0) {
638 			__lm3554_s_power(flash, 0);
639 			return ret;
640 		}
641 	}
642 
643 	return 0;
644 }
645 
646 static int lm3554_s_power(struct v4l2_subdev *sd, int power)
647 {
648 	struct lm3554 *flash = to_lm3554(sd);
649 	int ret = 0;
650 
651 	mutex_lock(&flash->power_lock);
652 
653 	if (flash->power_count == !power) {
654 		ret = __lm3554_s_power(flash, !!power);
655 		if (ret < 0)
656 			goto done;
657 	}
658 
659 	flash->power_count += power ? 1 : -1;
660 	WARN_ON(flash->power_count < 0);
661 
662 done:
663 	mutex_unlock(&flash->power_lock);
664 	return ret;
665 }
666 
667 static const struct v4l2_subdev_core_ops lm3554_core_ops = {
668 	.s_power = lm3554_s_power,
669 };
670 
671 static const struct v4l2_subdev_ops lm3554_ops = {
672 	.core = &lm3554_core_ops,
673 };
674 
675 static int lm3554_detect(struct v4l2_subdev *sd)
676 {
677 	struct i2c_client *client = v4l2_get_subdevdata(sd);
678 	struct i2c_adapter *adapter = client->adapter;
679 	struct lm3554 *flash = to_lm3554(sd);
680 	int ret;
681 
682 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
683 		dev_err(&client->dev, "lm3554_detect i2c error\n");
684 		return -ENODEV;
685 	}
686 
687 	/* Power up the flash driver and reset it */
688 	ret = lm3554_s_power(&flash->sd, 1);
689 	if (ret < 0) {
690 		dev_err(&client->dev, "Failed to power on lm3554 LED flash\n");
691 	} else {
692 		dev_dbg(&client->dev, "Successfully detected lm3554 LED flash\n");
693 		lm3554_s_power(&flash->sd, 0);
694 	}
695 
696 	return ret;
697 }
698 
699 static int lm3554_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
700 {
701 	return lm3554_s_power(sd, 1);
702 }
703 
704 static int lm3554_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
705 {
706 	return lm3554_s_power(sd, 0);
707 }
708 
709 static const struct v4l2_subdev_internal_ops lm3554_internal_ops = {
710 	.registered = lm3554_detect,
711 	.open = lm3554_open,
712 	.close = lm3554_close,
713 };
714 
715 /* -----------------------------------------------------------------------------
716  *  I2C driver
717  */
718 #ifdef CONFIG_PM
719 
720 static int lm3554_suspend(struct device *dev)
721 {
722 	struct i2c_client *client = to_i2c_client(dev);
723 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
724 	struct lm3554 *flash = to_lm3554(subdev);
725 	int rval;
726 
727 	if (flash->power_count == 0)
728 		return 0;
729 
730 	rval = __lm3554_s_power(flash, 0);
731 
732 	dev_dbg(&client->dev, "Suspend %s\n", rval < 0 ? "failed" : "ok");
733 
734 	return rval;
735 }
736 
737 static int lm3554_resume(struct device *dev)
738 {
739 	struct i2c_client *client = to_i2c_client(dev);
740 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
741 	struct lm3554 *flash = to_lm3554(subdev);
742 	int rval;
743 
744 	if (flash->power_count == 0)
745 		return 0;
746 
747 	rval = __lm3554_s_power(flash, 1);
748 
749 	dev_dbg(&client->dev, "Resume %s\n", rval < 0 ? "fail" : "ok");
750 
751 	return rval;
752 }
753 
754 #else
755 
756 #define lm3554_suspend NULL
757 #define lm3554_resume  NULL
758 
759 #endif /* CONFIG_PM */
760 
761 static int lm3554_gpio_init(struct i2c_client *client)
762 {
763 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
764 	struct lm3554 *flash = to_lm3554(sd);
765 	struct lm3554_platform_data *pdata = flash->pdata;
766 	int ret;
767 
768 	if (!gpio_is_valid(pdata->gpio_reset))
769 		return -EINVAL;
770 
771 	ret = gpio_direction_output(pdata->gpio_reset, 0);
772 	if (ret < 0)
773 		goto err_gpio_reset;
774 	dev_info(&client->dev, "flash led reset successfully\n");
775 
776 	if (!gpio_is_valid(pdata->gpio_strobe)) {
777 		ret = -EINVAL;
778 		goto err_gpio_dir_reset;
779 	}
780 
781 	ret = gpio_direction_output(pdata->gpio_strobe, 0);
782 	if (ret < 0)
783 		goto err_gpio_strobe;
784 
785 	return 0;
786 
787 err_gpio_strobe:
788 	gpio_free(pdata->gpio_strobe);
789 err_gpio_dir_reset:
790 	gpio_direction_output(pdata->gpio_reset, 0);
791 err_gpio_reset:
792 	gpio_free(pdata->gpio_reset);
793 
794 	return ret;
795 }
796 
797 static int lm3554_gpio_uninit(struct i2c_client *client)
798 {
799 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
800 	struct lm3554 *flash = to_lm3554(sd);
801 	struct lm3554_platform_data *pdata = flash->pdata;
802 	int ret;
803 
804 	ret = gpio_direction_output(pdata->gpio_strobe, 0);
805 	if (ret < 0)
806 		return ret;
807 
808 	ret = gpio_direction_output(pdata->gpio_reset, 0);
809 	if (ret < 0)
810 		return ret;
811 
812 	gpio_free(pdata->gpio_strobe);
813 	gpio_free(pdata->gpio_reset);
814 	return 0;
815 }
816 
817 static void *lm3554_platform_data_func(struct i2c_client *client)
818 {
819 	static struct lm3554_platform_data platform_data;
820 
821 	platform_data.gpio_reset =
822 	    desc_to_gpio(gpiod_get_index(&client->dev,
823 					 NULL, 2, GPIOD_OUT_LOW));
824 	platform_data.gpio_strobe =
825 	    desc_to_gpio(gpiod_get_index(&client->dev,
826 					 NULL, 0, GPIOD_OUT_LOW));
827 	platform_data.gpio_torch =
828 	    desc_to_gpio(gpiod_get_index(&client->dev,
829 					 NULL, 1, GPIOD_OUT_LOW));
830 	dev_info(&client->dev, "camera pdata: lm3554: reset: %d strobe %d torch %d\n",
831 		 platform_data.gpio_reset, platform_data.gpio_strobe,
832 		 platform_data.gpio_torch);
833 
834 	/* Set to TX2 mode, then ENVM/TX2 pin is a power amplifier sync input:
835 	 * ENVM/TX pin asserted, flash forced into torch;
836 	 * ENVM/TX pin desserted, flash set back;
837 	 */
838 	platform_data.envm_tx2 = 1;
839 	platform_data.tx2_polarity = 0;
840 
841 	/* set peak current limit to be 1000mA */
842 	platform_data.current_limit = 0;
843 
844 	return &platform_data;
845 }
846 
847 static int lm3554_probe(struct i2c_client *client)
848 {
849 	int err = 0;
850 	struct lm3554 *flash;
851 	unsigned int i;
852 	int ret;
853 	acpi_handle handle;
854 	struct acpi_device *adev;
855 
856 	handle = ACPI_HANDLE(&client->dev);
857 	if (!handle || acpi_bus_get_device(handle, &adev)) {
858 		dev_err(&client->dev, "Error could not get ACPI device\n");
859 		return -ENODEV;
860 	}
861 	pr_info("%s: ACPI detected it on bus ID=%s, HID=%s\n",
862 		__func__, acpi_device_bid(adev), acpi_device_hid(adev));
863 	// FIXME: may need to release resources allocated by acpi_bus_get_device()
864 
865 	flash = kzalloc(sizeof(*flash), GFP_KERNEL);
866 	if (!flash)
867 		return -ENOMEM;
868 
869 	flash->pdata = lm3554_platform_data_func(client);
870 
871 	v4l2_i2c_subdev_init(&flash->sd, client, &lm3554_ops);
872 	flash->sd.internal_ops = &lm3554_internal_ops;
873 	flash->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
874 	flash->mode = ATOMISP_FLASH_MODE_OFF;
875 	flash->timeout = LM3554_MAX_TIMEOUT / LM3554_TIMEOUT_STEPSIZE - 1;
876 	ret =
877 	    v4l2_ctrl_handler_init(&flash->ctrl_handler,
878 				   ARRAY_SIZE(lm3554_controls));
879 	if (ret) {
880 		dev_err(&client->dev, "error initialize a ctrl_handler.\n");
881 		goto fail2;
882 	}
883 
884 	for (i = 0; i < ARRAY_SIZE(lm3554_controls); i++)
885 		v4l2_ctrl_new_custom(&flash->ctrl_handler, &lm3554_controls[i],
886 				     NULL);
887 
888 	if (flash->ctrl_handler.error) {
889 		dev_err(&client->dev, "ctrl_handler error.\n");
890 		goto fail2;
891 	}
892 
893 	flash->sd.ctrl_handler = &flash->ctrl_handler;
894 	err = media_entity_pads_init(&flash->sd.entity, 0, NULL);
895 	if (err) {
896 		dev_err(&client->dev, "error initialize a media entity.\n");
897 		goto fail1;
898 	}
899 
900 	flash->sd.entity.function = MEDIA_ENT_F_FLASH;
901 
902 	mutex_init(&flash->power_lock);
903 
904 	timer_setup(&flash->flash_off_delay, lm3554_flash_off_delay, 0);
905 
906 	err = lm3554_gpio_init(client);
907 	if (err) {
908 		dev_err(&client->dev, "gpio request/direction_output fail");
909 		goto fail2;
910 	}
911 	return atomisp_register_i2c_module(&flash->sd, NULL, LED_FLASH);
912 fail2:
913 	media_entity_cleanup(&flash->sd.entity);
914 	v4l2_ctrl_handler_free(&flash->ctrl_handler);
915 fail1:
916 	v4l2_device_unregister_subdev(&flash->sd);
917 	kfree(flash);
918 
919 	return err;
920 }
921 
922 static int lm3554_remove(struct i2c_client *client)
923 {
924 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
925 	struct lm3554 *flash = to_lm3554(sd);
926 	int ret;
927 
928 	media_entity_cleanup(&flash->sd.entity);
929 	v4l2_ctrl_handler_free(&flash->ctrl_handler);
930 	v4l2_device_unregister_subdev(sd);
931 
932 	atomisp_gmin_remove_subdev(sd);
933 
934 	del_timer_sync(&flash->flash_off_delay);
935 
936 	ret = lm3554_gpio_uninit(client);
937 	if (ret < 0)
938 		goto fail;
939 
940 	kfree(flash);
941 
942 	return 0;
943 fail:
944 	dev_err(&client->dev, "gpio request/direction_output fail");
945 	return ret;
946 }
947 
948 static const struct dev_pm_ops lm3554_pm_ops = {
949 	.suspend = lm3554_suspend,
950 	.resume = lm3554_resume,
951 };
952 
953 static const struct acpi_device_id lm3554_acpi_match[] = {
954 	{ "INTCF1C" },
955 	{},
956 };
957 MODULE_DEVICE_TABLE(acpi, lm3554_acpi_match);
958 
959 static struct i2c_driver lm3554_driver = {
960 	.driver = {
961 		.name = "lm3554",
962 		.pm   = &lm3554_pm_ops,
963 		.acpi_match_table = lm3554_acpi_match,
964 	},
965 	.probe_new = lm3554_probe,
966 	.remove = lm3554_remove,
967 };
968 module_i2c_driver(lm3554_driver);
969 
970 MODULE_AUTHOR("Jing Tao <jing.tao@intel.com>");
971 MODULE_DESCRIPTION("LED flash driver for LM3554");
972 MODULE_LICENSE("GPL");
973