1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * LED flash driver for LM3554
4  *
5  * Copyright (c) 2010-2012 Intel Corporation. All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version
9  * 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  *
17  */
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/mutex.h>
21 #include <linux/delay.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/slab.h>
24 
25 #include "../include/media/lm3554.h"
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <linux/acpi.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 */
lm3554_write(struct lm3554 * flash,u8 addr,u8 val)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. */
lm3554_read(struct lm3554 * flash,u8 addr)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 
lm3554_set_mode(struct lm3554 * flash,unsigned int mode)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 
lm3554_set_torch(struct lm3554 * flash)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 
lm3554_set_flash(struct lm3554 * flash)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 
lm3554_set_duration(struct lm3554 * flash)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 
lm3554_set_config1(struct lm3554 * flash)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  */
lm3554_flash_off_delay(struct timer_list * t)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 	gpiod_set_value(pdata->gpio_strobe, 0);
176 }
177 
lm3554_hw_strobe(struct i2c_client * client,bool strobe)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 		gpiod_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 	gpiod_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 
lm3554_read_status(struct lm3554 * flash)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 
lm3554_s_flash_timeout(struct v4l2_subdev * sd,u32 val)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 
lm3554_g_flash_timeout(struct v4l2_subdev * sd,s32 * val)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 
lm3554_s_flash_intensity(struct v4l2_subdev * sd,u32 intensity)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 
lm3554_g_flash_intensity(struct v4l2_subdev * sd,s32 * val)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 
lm3554_s_torch_intensity(struct v4l2_subdev * sd,u32 intensity)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 
lm3554_g_torch_intensity(struct v4l2_subdev * sd,s32 * val)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 
lm3554_s_indicator_intensity(struct v4l2_subdev * sd,u32 intensity)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 
lm3554_g_indicator_intensity(struct v4l2_subdev * sd,s32 * val)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 
lm3554_s_flash_strobe(struct v4l2_subdev * sd,u32 val)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 
lm3554_s_flash_mode(struct v4l2_subdev * sd,u32 new_mode)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 
lm3554_g_flash_mode(struct v4l2_subdev * sd,s32 * val)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 
lm3554_g_flash_status(struct v4l2_subdev * sd,s32 * val)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 
lm3554_g_flash_status_register(struct v4l2_subdev * sd,s32 * val)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 
lm3554_s_ctrl(struct v4l2_ctrl * ctrl)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 
lm3554_g_volatile_ctrl(struct v4l2_ctrl * ctrl)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. */
lm3554_setup(struct lm3554 * flash)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 
__lm3554_s_power(struct lm3554 * flash,int power)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 	gpiod_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 
lm3554_s_power(struct v4l2_subdev * sd,int power)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 
lm3554_detect(struct v4l2_subdev * sd)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 
lm3554_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)699 static int lm3554_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
700 {
701 	return lm3554_s_power(sd, 1);
702 }
703 
lm3554_close(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)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 
lm3554_suspend(struct device * dev)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 
lm3554_resume(struct device * dev)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 
lm3554_gpio_init(struct i2c_client * client)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 (!pdata->gpio_reset)
769 		return -EINVAL;
770 
771 	ret = gpiod_direction_output(pdata->gpio_reset, 0);
772 	if (ret < 0)
773 		return ret;
774 
775 	if (!pdata->gpio_strobe)
776 		return -EINVAL;
777 
778 	ret = gpiod_direction_output(pdata->gpio_strobe, 0);
779 	if (ret < 0)
780 		return ret;
781 
782 	return 0;
783 }
784 
lm3554_gpio_uninit(struct i2c_client * client)785 static void lm3554_gpio_uninit(struct i2c_client *client)
786 {
787 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
788 	struct lm3554 *flash = to_lm3554(sd);
789 	struct lm3554_platform_data *pdata = flash->pdata;
790 	int ret;
791 
792 	ret = gpiod_direction_output(pdata->gpio_strobe, 0);
793 	if (ret < 0)
794 		dev_err(&client->dev,
795 			"gpio request/direction_output fail for gpio_strobe");
796 
797 	ret = gpiod_direction_output(pdata->gpio_reset, 0);
798 	if (ret < 0)
799 		dev_err(&client->dev,
800 			"gpio request/direction_output fail for gpio_reset");
801 }
802 
lm3554_platform_data_func(struct i2c_client * client)803 static void *lm3554_platform_data_func(struct i2c_client *client)
804 {
805 	static struct lm3554_platform_data platform_data;
806 
807 	platform_data.gpio_reset = gpiod_get_index(&client->dev,
808 						   NULL, 2, GPIOD_OUT_LOW);
809 	if (IS_ERR(platform_data.gpio_reset))
810 		return ERR_CAST(platform_data.gpio_reset);
811 	platform_data.gpio_strobe = gpiod_get_index(&client->dev,
812 						    NULL, 0, GPIOD_OUT_LOW);
813 	if (IS_ERR(platform_data.gpio_strobe))
814 		return ERR_CAST(platform_data.gpio_strobe);
815 	platform_data.gpio_torch = gpiod_get_index(&client->dev,
816 						   NULL, 1, GPIOD_OUT_LOW);
817 	if (IS_ERR(platform_data.gpio_torch))
818 		return ERR_CAST(platform_data.gpio_torch);
819 
820 	/* Set to TX2 mode, then ENVM/TX2 pin is a power amplifier sync input:
821 	 * ENVM/TX pin asserted, flash forced into torch;
822 	 * ENVM/TX pin desserted, flash set back;
823 	 */
824 	platform_data.envm_tx2 = 1;
825 	platform_data.tx2_polarity = 0;
826 
827 	/* set peak current limit to be 1000mA */
828 	platform_data.current_limit = 0;
829 
830 	return &platform_data;
831 }
832 
lm3554_probe(struct i2c_client * client)833 static int lm3554_probe(struct i2c_client *client)
834 {
835 	int err = 0;
836 	struct lm3554 *flash;
837 	unsigned int i;
838 	int ret;
839 
840 	flash = kzalloc(sizeof(*flash), GFP_KERNEL);
841 	if (!flash)
842 		return -ENOMEM;
843 
844 	flash->pdata = lm3554_platform_data_func(client);
845 	if (IS_ERR(flash->pdata)) {
846 		err = PTR_ERR(flash->pdata);
847 		goto fail1;
848 	}
849 
850 	v4l2_i2c_subdev_init(&flash->sd, client, &lm3554_ops);
851 	flash->sd.internal_ops = &lm3554_internal_ops;
852 	flash->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
853 	flash->mode = ATOMISP_FLASH_MODE_OFF;
854 	flash->timeout = LM3554_MAX_TIMEOUT / LM3554_TIMEOUT_STEPSIZE - 1;
855 	ret =
856 	    v4l2_ctrl_handler_init(&flash->ctrl_handler,
857 				   ARRAY_SIZE(lm3554_controls));
858 	if (ret) {
859 		dev_err(&client->dev, "error initialize a ctrl_handler.\n");
860 		goto fail3;
861 	}
862 
863 	for (i = 0; i < ARRAY_SIZE(lm3554_controls); i++)
864 		v4l2_ctrl_new_custom(&flash->ctrl_handler, &lm3554_controls[i],
865 				     NULL);
866 
867 	if (flash->ctrl_handler.error) {
868 		dev_err(&client->dev, "ctrl_handler error.\n");
869 		goto fail3;
870 	}
871 
872 	flash->sd.ctrl_handler = &flash->ctrl_handler;
873 	err = media_entity_pads_init(&flash->sd.entity, 0, NULL);
874 	if (err) {
875 		dev_err(&client->dev, "error initialize a media entity.\n");
876 		goto fail2;
877 	}
878 
879 	flash->sd.entity.function = MEDIA_ENT_F_FLASH;
880 
881 	mutex_init(&flash->power_lock);
882 
883 	timer_setup(&flash->flash_off_delay, lm3554_flash_off_delay, 0);
884 
885 	err = lm3554_gpio_init(client);
886 	if (err) {
887 		dev_err(&client->dev, "gpio request/direction_output fail");
888 		goto fail3;
889 	}
890 	return atomisp_register_i2c_module(&flash->sd, NULL, LED_FLASH);
891 fail3:
892 	media_entity_cleanup(&flash->sd.entity);
893 	v4l2_ctrl_handler_free(&flash->ctrl_handler);
894 fail2:
895 	v4l2_device_unregister_subdev(&flash->sd);
896 fail1:
897 	kfree(flash);
898 
899 	return err;
900 }
901 
lm3554_remove(struct i2c_client * client)902 static int lm3554_remove(struct i2c_client *client)
903 {
904 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
905 	struct lm3554 *flash = to_lm3554(sd);
906 
907 	media_entity_cleanup(&flash->sd.entity);
908 	v4l2_ctrl_handler_free(&flash->ctrl_handler);
909 	v4l2_device_unregister_subdev(sd);
910 
911 	atomisp_gmin_remove_subdev(sd);
912 
913 	del_timer_sync(&flash->flash_off_delay);
914 
915 	lm3554_gpio_uninit(client);
916 
917 	kfree(flash);
918 
919 	return 0;
920 }
921 
922 static const struct dev_pm_ops lm3554_pm_ops = {
923 	.suspend = lm3554_suspend,
924 	.resume = lm3554_resume,
925 };
926 
927 static const struct acpi_device_id lm3554_acpi_match[] = {
928 	{ "INTCF1C" },
929 	{},
930 };
931 MODULE_DEVICE_TABLE(acpi, lm3554_acpi_match);
932 
933 static struct i2c_driver lm3554_driver = {
934 	.driver = {
935 		.name = "lm3554",
936 		.pm   = &lm3554_pm_ops,
937 		.acpi_match_table = lm3554_acpi_match,
938 	},
939 	.probe_new = lm3554_probe,
940 	.remove = lm3554_remove,
941 };
942 module_i2c_driver(lm3554_driver);
943 
944 MODULE_AUTHOR("Jing Tao <jing.tao@intel.com>");
945 MODULE_DESCRIPTION("LED flash driver for LM3554");
946 MODULE_LICENSE("GPL");
947