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