1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <linux/delay.h>
29#include <linux/i2c.h>
30#include <linux/module.h>
31#include <linux/mutex.h>
32#include <linux/slab.h>
33
34#include <media/as3645a.h>
35#include <media/v4l2-ctrls.h>
36#include <media/v4l2-device.h>
37
38#define AS_TIMER_MS_TO_CODE(t) (((t) - 100) / 50)
39#define AS_TIMER_CODE_TO_MS(c) (50 * (c) + 100)
40
41
42
43
44#define AS_DESIGN_INFO_REG 0x00
45#define AS_DESIGN_INFO_FACTORY(x) (((x) >> 4))
46#define AS_DESIGN_INFO_MODEL(x) ((x) & 0x0f)
47
48
49
50
51#define AS_VERSION_CONTROL_REG 0x01
52#define AS_VERSION_CONTROL_RFU(x) (((x) >> 4))
53#define AS_VERSION_CONTROL_VERSION(x) ((x) & 0x0f)
54
55
56#define AS_INDICATOR_AND_TIMER_REG 0x02
57#define AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT 0
58#define AS_INDICATOR_AND_TIMER_VREF_SHIFT 4
59#define AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT 6
60
61
62#define AS_CURRENT_SET_REG 0x03
63#define AS_CURRENT_ASSIST_LIGHT_SHIFT 0
64#define AS_CURRENT_LED_DET_ON (1 << 3)
65#define AS_CURRENT_FLASH_CURRENT_SHIFT 4
66
67
68#define AS_CONTROL_REG 0x04
69#define AS_CONTROL_MODE_SETTING_SHIFT 0
70#define AS_CONTROL_STROBE_ON (1 << 2)
71#define AS_CONTROL_OUT_ON (1 << 3)
72#define AS_CONTROL_EXT_TORCH_ON (1 << 4)
73#define AS_CONTROL_STROBE_TYPE_EDGE (0 << 5)
74#define AS_CONTROL_STROBE_TYPE_LEVEL (1 << 5)
75#define AS_CONTROL_COIL_PEAK_SHIFT 6
76
77
78#define AS_FAULT_INFO_REG 0x05
79#define AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT (1 << 1)
80#define AS_FAULT_INFO_INDICATOR_LED (1 << 2)
81#define AS_FAULT_INFO_LED_AMOUNT (1 << 3)
82#define AS_FAULT_INFO_TIMEOUT (1 << 4)
83#define AS_FAULT_INFO_OVER_TEMPERATURE (1 << 5)
84#define AS_FAULT_INFO_SHORT_CIRCUIT (1 << 6)
85#define AS_FAULT_INFO_OVER_VOLTAGE (1 << 7)
86
87
88#define AS_BOOST_REG 0x0d
89#define AS_BOOST_CURRENT_DISABLE (0 << 0)
90#define AS_BOOST_CURRENT_ENABLE (1 << 0)
91
92
93#define AS_PASSWORD_REG 0x0f
94#define AS_PASSWORD_UNLOCK_VALUE 0x55
95
96enum as_mode {
97 AS_MODE_EXT_TORCH = 0 << AS_CONTROL_MODE_SETTING_SHIFT,
98 AS_MODE_INDICATOR = 1 << AS_CONTROL_MODE_SETTING_SHIFT,
99 AS_MODE_ASSIST = 2 << AS_CONTROL_MODE_SETTING_SHIFT,
100 AS_MODE_FLASH = 3 << AS_CONTROL_MODE_SETTING_SHIFT,
101};
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118struct as3645a {
119 struct v4l2_subdev subdev;
120 const struct as3645a_platform_data *pdata;
121
122 struct mutex power_lock;
123 int power_count;
124
125
126 struct v4l2_ctrl_handler ctrls;
127
128 enum v4l2_flash_led_mode led_mode;
129 unsigned int timeout;
130 u8 flash_current;
131 u8 assist_current;
132 u8 indicator_current;
133 enum v4l2_flash_strobe_source strobe_source;
134};
135
136#define to_as3645a(sd) container_of(sd, struct as3645a, subdev)
137
138
139static int as3645a_write(struct as3645a *flash, u8 addr, u8 val)
140{
141 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
142 int rval;
143
144 rval = i2c_smbus_write_byte_data(client, addr, val);
145
146 dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
147 rval < 0 ? "fail" : "ok");
148
149 return rval;
150}
151
152
153static int as3645a_read(struct as3645a *flash, u8 addr)
154{
155 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
156 int rval;
157
158 rval = i2c_smbus_read_byte_data(client, addr);
159
160 dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval,
161 rval < 0 ? "fail" : "ok");
162
163 return rval;
164}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180static int as3645a_set_config(struct as3645a *flash)
181{
182 int ret;
183 u8 val;
184
185 val = (flash->flash_current << AS_CURRENT_FLASH_CURRENT_SHIFT)
186 | (flash->assist_current << AS_CURRENT_ASSIST_LIGHT_SHIFT)
187 | AS_CURRENT_LED_DET_ON;
188
189 ret = as3645a_write(flash, AS_CURRENT_SET_REG, val);
190 if (ret < 0)
191 return ret;
192
193 val = AS_TIMER_MS_TO_CODE(flash->timeout / 1000)
194 << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT;
195
196 val |= (flash->pdata->vref << AS_INDICATOR_AND_TIMER_VREF_SHIFT)
197 | ((flash->indicator_current ? flash->indicator_current - 1 : 0)
198 << AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT);
199
200 return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val);
201}
202
203
204
205
206
207
208
209
210
211
212
213
214static int
215as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on)
216{
217 u8 reg;
218
219
220 reg = (flash->pdata->peak << AS_CONTROL_COIL_PEAK_SHIFT)
221 | (on ? AS_CONTROL_OUT_ON : 0)
222 | mode;
223
224 if (flash->led_mode == V4L2_FLASH_LED_MODE_FLASH &&
225 flash->strobe_source == V4L2_FLASH_STROBE_SOURCE_EXTERNAL) {
226 reg |= AS_CONTROL_STROBE_TYPE_LEVEL
227 | AS_CONTROL_STROBE_ON;
228 }
229
230 return as3645a_write(flash, AS_CONTROL_REG, reg);
231}
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250static int as3645a_set_output(struct as3645a *flash, bool strobe)
251{
252 enum as_mode mode;
253 bool on;
254
255 switch (flash->led_mode) {
256 case V4L2_FLASH_LED_MODE_NONE:
257 on = flash->indicator_current != 0;
258 mode = AS_MODE_INDICATOR;
259 break;
260 case V4L2_FLASH_LED_MODE_TORCH:
261 on = true;
262 mode = AS_MODE_ASSIST;
263 break;
264 case V4L2_FLASH_LED_MODE_FLASH:
265 on = strobe;
266 mode = AS_MODE_FLASH;
267 break;
268 default:
269 BUG();
270 }
271
272
273 return as3645a_set_control(flash, mode, on);
274}
275
276
277
278
279
280static int as3645a_is_active(struct as3645a *flash)
281{
282 int ret;
283
284 ret = as3645a_read(flash, AS_CONTROL_REG);
285 return ret < 0 ? ret : !!(ret & AS_CONTROL_OUT_ON);
286}
287
288static int as3645a_read_fault(struct as3645a *flash)
289{
290 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
291 int rval;
292
293
294 rval = as3645a_read(flash, AS_FAULT_INFO_REG);
295 if (rval < 0)
296 return rval;
297
298 if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
299 dev_dbg(&client->dev, "Inductor Peak limit fault\n");
300
301 if (rval & AS_FAULT_INFO_INDICATOR_LED)
302 dev_dbg(&client->dev, "Indicator LED fault: "
303 "Short circuit or open loop\n");
304
305 dev_dbg(&client->dev, "%u connected LEDs\n",
306 rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1);
307
308 if (rval & AS_FAULT_INFO_TIMEOUT)
309 dev_dbg(&client->dev, "Timeout fault\n");
310
311 if (rval & AS_FAULT_INFO_OVER_TEMPERATURE)
312 dev_dbg(&client->dev, "Over temperature fault\n");
313
314 if (rval & AS_FAULT_INFO_SHORT_CIRCUIT)
315 dev_dbg(&client->dev, "Short circuit fault\n");
316
317 if (rval & AS_FAULT_INFO_OVER_VOLTAGE)
318 dev_dbg(&client->dev, "Over voltage fault: "
319 "Indicates missing capacitor or open connection\n");
320
321 return rval;
322}
323
324static int as3645a_get_ctrl(struct v4l2_ctrl *ctrl)
325{
326 struct as3645a *flash =
327 container_of(ctrl->handler, struct as3645a, ctrls);
328 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
329 int value;
330
331 switch (ctrl->id) {
332 case V4L2_CID_FLASH_FAULT:
333 value = as3645a_read_fault(flash);
334 if (value < 0)
335 return value;
336
337 ctrl->cur.val = 0;
338 if (value & AS_FAULT_INFO_SHORT_CIRCUIT)
339 ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
340 if (value & AS_FAULT_INFO_OVER_TEMPERATURE)
341 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
342 if (value & AS_FAULT_INFO_TIMEOUT)
343 ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT;
344 if (value & AS_FAULT_INFO_OVER_VOLTAGE)
345 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
346 if (value & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
347 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_CURRENT;
348 if (value & AS_FAULT_INFO_INDICATOR_LED)
349 ctrl->cur.val |= V4L2_FLASH_FAULT_INDICATOR;
350 break;
351
352 case V4L2_CID_FLASH_STROBE_STATUS:
353 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
354 ctrl->cur.val = 0;
355 break;
356 }
357
358 value = as3645a_is_active(flash);
359 if (value < 0)
360 return value;
361
362 ctrl->cur.val = value;
363 break;
364 }
365
366 dev_dbg(&client->dev, "G_CTRL %08x:%d\n", ctrl->id, ctrl->cur.val);
367
368 return 0;
369}
370
371static int as3645a_set_ctrl(struct v4l2_ctrl *ctrl)
372{
373 struct as3645a *flash =
374 container_of(ctrl->handler, struct as3645a, ctrls);
375 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
376 int ret;
377
378 dev_dbg(&client->dev, "S_CTRL %08x:%d\n", ctrl->id, ctrl->val);
379
380
381
382
383
384
385
386 switch (ctrl->id) {
387 case V4L2_CID_FLASH_LED_MODE:
388 if (flash->indicator_current)
389 return -EBUSY;
390
391 ret = as3645a_set_config(flash);
392 if (ret < 0)
393 return ret;
394
395 flash->led_mode = ctrl->val;
396 return as3645a_set_output(flash, false);
397
398 case V4L2_CID_FLASH_STROBE_SOURCE:
399 flash->strobe_source = ctrl->val;
400
401
402 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
403 break;
404
405 return as3645a_set_output(flash, false);
406
407 case V4L2_CID_FLASH_STROBE:
408 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
409 return -EBUSY;
410
411 return as3645a_set_output(flash, true);
412
413 case V4L2_CID_FLASH_STROBE_STOP:
414 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
415 return -EBUSY;
416
417 return as3645a_set_output(flash, false);
418
419 case V4L2_CID_FLASH_TIMEOUT:
420 flash->timeout = ctrl->val;
421
422
423 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
424 break;
425
426 return as3645a_set_config(flash);
427
428 case V4L2_CID_FLASH_INTENSITY:
429 flash->flash_current = (ctrl->val - AS3645A_FLASH_INTENSITY_MIN)
430 / AS3645A_FLASH_INTENSITY_STEP;
431
432
433 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
434 break;
435
436 return as3645a_set_config(flash);
437
438 case V4L2_CID_FLASH_TORCH_INTENSITY:
439 flash->assist_current =
440 (ctrl->val - AS3645A_TORCH_INTENSITY_MIN)
441 / AS3645A_TORCH_INTENSITY_STEP;
442
443
444 if (flash->led_mode != V4L2_FLASH_LED_MODE_TORCH)
445 break;
446
447 return as3645a_set_config(flash);
448
449 case V4L2_CID_FLASH_INDICATOR_INTENSITY:
450 if (flash->led_mode != V4L2_FLASH_LED_MODE_NONE)
451 return -EBUSY;
452
453 flash->indicator_current =
454 (ctrl->val - AS3645A_INDICATOR_INTENSITY_MIN)
455 / AS3645A_INDICATOR_INTENSITY_STEP;
456
457 ret = as3645a_set_config(flash);
458 if (ret < 0)
459 return ret;
460
461 if ((ctrl->val == 0) == (ctrl->cur.val == 0))
462 break;
463
464 return as3645a_set_output(flash, false);
465 }
466
467 return 0;
468}
469
470static const struct v4l2_ctrl_ops as3645a_ctrl_ops = {
471 .g_volatile_ctrl = as3645a_get_ctrl,
472 .s_ctrl = as3645a_set_ctrl,
473};
474
475
476
477
478
479
480static int as3645a_setup(struct as3645a *flash)
481{
482 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
483 int ret;
484
485
486 ret = as3645a_read(flash, AS_FAULT_INFO_REG);
487 if (ret < 0)
488 return ret;
489
490 dev_dbg(&client->dev, "Fault info: %02x\n", ret);
491
492 ret = as3645a_set_config(flash);
493 if (ret < 0)
494 return ret;
495
496 ret = as3645a_set_output(flash, false);
497 if (ret < 0)
498 return ret;
499
500
501 ret = as3645a_read_fault(flash);
502 if (ret < 0)
503 return ret;
504
505 dev_dbg(&client->dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n",
506 as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG));
507 dev_dbg(&client->dev, "AS_CURRENT_SET_REG: %02x\n",
508 as3645a_read(flash, AS_CURRENT_SET_REG));
509 dev_dbg(&client->dev, "AS_CONTROL_REG: %02x\n",
510 as3645a_read(flash, AS_CONTROL_REG));
511
512 return ret & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0;
513}
514
515static int __as3645a_set_power(struct as3645a *flash, int on)
516{
517 int ret;
518
519 if (!on)
520 as3645a_set_control(flash, AS_MODE_EXT_TORCH, false);
521
522 if (flash->pdata->set_power) {
523 ret = flash->pdata->set_power(&flash->subdev, on);
524 if (ret < 0)
525 return ret;
526 }
527
528 if (!on)
529 return 0;
530
531 ret = as3645a_setup(flash);
532 if (ret < 0) {
533 if (flash->pdata->set_power)
534 flash->pdata->set_power(&flash->subdev, 0);
535 }
536
537 return ret;
538}
539
540static int as3645a_set_power(struct v4l2_subdev *sd, int on)
541{
542 struct as3645a *flash = to_as3645a(sd);
543 int ret = 0;
544
545 mutex_lock(&flash->power_lock);
546
547 if (flash->power_count == !on) {
548 ret = __as3645a_set_power(flash, !!on);
549 if (ret < 0)
550 goto done;
551 }
552
553 flash->power_count += on ? 1 : -1;
554 WARN_ON(flash->power_count < 0);
555
556done:
557 mutex_unlock(&flash->power_lock);
558 return ret;
559}
560
561static int as3645a_registered(struct v4l2_subdev *sd)
562{
563 struct as3645a *flash = to_as3645a(sd);
564 struct i2c_client *client = v4l2_get_subdevdata(sd);
565 int rval, man, model, rfu, version;
566 const char *vendor;
567
568
569
570
571 rval = as3645a_set_power(&flash->subdev, 1);
572 if (rval < 0)
573 return rval;
574
575 rval = as3645a_read(flash, AS_DESIGN_INFO_REG);
576 if (rval < 0)
577 goto power_off;
578
579 man = AS_DESIGN_INFO_FACTORY(rval);
580 model = AS_DESIGN_INFO_MODEL(rval);
581
582 rval = as3645a_read(flash, AS_VERSION_CONTROL_REG);
583 if (rval < 0)
584 goto power_off;
585
586 rfu = AS_VERSION_CONTROL_RFU(rval);
587 version = AS_VERSION_CONTROL_VERSION(rval);
588
589
590 if (model != 0x01 || rfu != 0x00) {
591 dev_err(&client->dev, "AS3645A not detected "
592 "(model %d rfu %d)\n", model, rfu);
593 rval = -ENODEV;
594 goto power_off;
595 }
596
597 switch (man) {
598 case 1:
599 vendor = "AMS, Austria Micro Systems";
600 break;
601 case 2:
602 vendor = "ADI, Analog Devices Inc.";
603 break;
604 case 3:
605 vendor = "NSC, National Semiconductor";
606 break;
607 case 4:
608 vendor = "NXP";
609 break;
610 case 5:
611 vendor = "TI, Texas Instrument";
612 break;
613 default:
614 vendor = "Unknown";
615 }
616
617 dev_info(&client->dev, "Chip vendor: %s (%d) Version: %d\n", vendor,
618 man, version);
619
620 rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE);
621 if (rval < 0)
622 goto power_off;
623
624 rval = as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
625 if (rval < 0)
626 goto power_off;
627
628
629
630
631 rval = as3645a_setup(flash);
632
633power_off:
634 as3645a_set_power(&flash->subdev, 0);
635
636 return rval;
637}
638
639static int as3645a_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
640{
641 return as3645a_set_power(sd, 1);
642}
643
644static int as3645a_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
645{
646 return as3645a_set_power(sd, 0);
647}
648
649static const struct v4l2_subdev_core_ops as3645a_core_ops = {
650 .s_power = as3645a_set_power,
651};
652
653static const struct v4l2_subdev_ops as3645a_ops = {
654 .core = &as3645a_core_ops,
655};
656
657static const struct v4l2_subdev_internal_ops as3645a_internal_ops = {
658 .registered = as3645a_registered,
659 .open = as3645a_open,
660 .close = as3645a_close,
661};
662
663
664
665
666#ifdef CONFIG_PM
667
668static int as3645a_suspend(struct device *dev)
669{
670 struct i2c_client *client = to_i2c_client(dev);
671 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
672 struct as3645a *flash = to_as3645a(subdev);
673 int rval;
674
675 if (flash->power_count == 0)
676 return 0;
677
678 rval = __as3645a_set_power(flash, 0);
679
680 dev_dbg(&client->dev, "Suspend %s\n", rval < 0 ? "failed" : "ok");
681
682 return rval;
683}
684
685static int as3645a_resume(struct device *dev)
686{
687 struct i2c_client *client = to_i2c_client(dev);
688 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
689 struct as3645a *flash = to_as3645a(subdev);
690 int rval;
691
692 if (flash->power_count == 0)
693 return 0;
694
695 rval = __as3645a_set_power(flash, 1);
696
697 dev_dbg(&client->dev, "Resume %s\n", rval < 0 ? "fail" : "ok");
698
699 return rval;
700}
701
702#else
703
704#define as3645a_suspend NULL
705#define as3645a_resume NULL
706
707#endif
708
709
710
711
712
713
714
715
716static int as3645a_init_controls(struct as3645a *flash)
717{
718 const struct as3645a_platform_data *pdata = flash->pdata;
719 struct v4l2_ctrl *ctrl;
720 int maximum;
721
722 v4l2_ctrl_handler_init(&flash->ctrls, 10);
723
724
725 v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops,
726 V4L2_CID_FLASH_LED_MODE, 2, ~7,
727 V4L2_FLASH_LED_MODE_NONE);
728
729
730 v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops,
731 V4L2_CID_FLASH_STROBE_SOURCE,
732 pdata->ext_strobe ? 1 : 0,
733 pdata->ext_strobe ? ~3 : ~1,
734 V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
735
736 flash->strobe_source = V4L2_FLASH_STROBE_SOURCE_SOFTWARE;
737
738
739 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
740 V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
741
742
743 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
744 V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
745
746
747 ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
748 V4L2_CID_FLASH_STROBE_STATUS, 0, 1, 1, 1);
749 if (ctrl != NULL)
750 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
751
752
753 maximum = pdata->timeout_max;
754
755 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
756 V4L2_CID_FLASH_TIMEOUT, AS3645A_FLASH_TIMEOUT_MIN,
757 maximum, AS3645A_FLASH_TIMEOUT_STEP, maximum);
758
759 flash->timeout = maximum;
760
761
762 maximum = pdata->flash_max_current;
763
764 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
765 V4L2_CID_FLASH_INTENSITY, AS3645A_FLASH_INTENSITY_MIN,
766 maximum, AS3645A_FLASH_INTENSITY_STEP, maximum);
767
768 flash->flash_current = (maximum - AS3645A_FLASH_INTENSITY_MIN)
769 / AS3645A_FLASH_INTENSITY_STEP;
770
771
772 maximum = pdata->torch_max_current;
773
774 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
775 V4L2_CID_FLASH_TORCH_INTENSITY,
776 AS3645A_TORCH_INTENSITY_MIN, maximum,
777 AS3645A_TORCH_INTENSITY_STEP,
778 AS3645A_TORCH_INTENSITY_MIN);
779
780 flash->assist_current = 0;
781
782
783 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
784 V4L2_CID_FLASH_INDICATOR_INTENSITY,
785 AS3645A_INDICATOR_INTENSITY_MIN,
786 AS3645A_INDICATOR_INTENSITY_MAX,
787 AS3645A_INDICATOR_INTENSITY_STEP,
788 AS3645A_INDICATOR_INTENSITY_MIN);
789
790 flash->indicator_current = 0;
791
792
793 ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
794 V4L2_CID_FLASH_FAULT, 0,
795 V4L2_FLASH_FAULT_OVER_VOLTAGE |
796 V4L2_FLASH_FAULT_TIMEOUT |
797 V4L2_FLASH_FAULT_OVER_TEMPERATURE |
798 V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
799 if (ctrl != NULL)
800 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
801
802 flash->subdev.ctrl_handler = &flash->ctrls;
803
804 return flash->ctrls.error;
805}
806
807static int as3645a_probe(struct i2c_client *client,
808 const struct i2c_device_id *devid)
809{
810 struct as3645a *flash;
811 int ret;
812
813 if (client->dev.platform_data == NULL)
814 return -ENODEV;
815
816 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
817 if (flash == NULL)
818 return -ENOMEM;
819
820 flash->pdata = client->dev.platform_data;
821
822 v4l2_i2c_subdev_init(&flash->subdev, client, &as3645a_ops);
823 flash->subdev.internal_ops = &as3645a_internal_ops;
824 flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
825
826 ret = as3645a_init_controls(flash);
827 if (ret < 0)
828 goto done;
829
830 ret = media_entity_init(&flash->subdev.entity, 0, NULL, 0);
831 if (ret < 0)
832 goto done;
833
834 flash->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
835
836 mutex_init(&flash->power_lock);
837
838 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
839
840done:
841 if (ret < 0)
842 v4l2_ctrl_handler_free(&flash->ctrls);
843
844 return ret;
845}
846
847static int as3645a_remove(struct i2c_client *client)
848{
849 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
850 struct as3645a *flash = to_as3645a(subdev);
851
852 v4l2_device_unregister_subdev(subdev);
853 v4l2_ctrl_handler_free(&flash->ctrls);
854 media_entity_cleanup(&flash->subdev.entity);
855 mutex_destroy(&flash->power_lock);
856
857 return 0;
858}
859
860static const struct i2c_device_id as3645a_id_table[] = {
861 { AS3645A_NAME, 0 },
862 { },
863};
864MODULE_DEVICE_TABLE(i2c, as3645a_id_table);
865
866static const struct dev_pm_ops as3645a_pm_ops = {
867 .suspend = as3645a_suspend,
868 .resume = as3645a_resume,
869};
870
871static struct i2c_driver as3645a_i2c_driver = {
872 .driver = {
873 .name = AS3645A_NAME,
874 .pm = &as3645a_pm_ops,
875 },
876 .probe = as3645a_probe,
877 .remove = as3645a_remove,
878 .id_table = as3645a_id_table,
879};
880
881module_i2c_driver(as3645a_i2c_driver);
882
883MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
884MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones");
885MODULE_LICENSE("GPL");
886