hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
// SPDX-License-Identifier: GPL-2.0
/*
 * General device driver for awinic aw36518, FLASH LED Driver
 *
 * Copyright (C) 2022 Fuzhou Rockchip Electronics Co., Ltd.
 *
 * V0.0X01.0X00 init version.
 * V0.0X01.0X01 fix power off torch not off issue.
 * V0.0X01.0X02 fix get wrong time info issue.
 */
 
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/pm_runtime.h>
#include <linux/rk-camera-module.h>
#include <linux/rk-led-flash.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <linux/compat.h>
 
#define DRIVER_VERSION            KERNEL_VERSION(0, 0x01, 0x02)
#define AW36518_NAME            "aw36518"
 
#define AW36518_REG_ID            0x00
#define AW36518_ID            0x30
 
#define AW36518_REG_MODE        0x01
#define AW36518_REG_LED0_FLASH_CUR    0x03
#define AW36518_REG_LED0_TORCH_CUR    0x05
#define AW36518_REG_LED1_FLASH_CUR    0x03
#define AW36518_REG_LED1_TORCH_CUR    0x05
#define AW36518_HW_TORCH        BIT(4)
#define AW36518_HW_STROBE        BIT(5)
 
#define AW36518_REG_FAULT        0x0A
#define AW36518_REG_FL_SC        0x70
#define AW36518_REG_FL_OT        BIT(2)
#define AW36518_REG_FL_TO        BIT(0)
 
#define AW36518_REG_FAULT2        0x0B
#define AW36518_REG_FL_OVP        BIT(1)
 
#define AW36518_REG_ENABLE        0x01
#define LED_ON                0x03
#define LED_OFF                0x00
 
/*  FLASH Brightness
 *    min 2940uA, step 5870uA, max 1500000uA
 */
#define AW36518_MIN_FLASH_INTENSITY    2940
#define AW36518_MAX_FLASH_INTENSITY    1500000
#define AW36518_FLASH_INTENSITY_DEFAULT    748430
#define AW36518_FLASH_INTENSITY_STEP    5870
#define AW36518_FLASH_BRT_uA_TO_REG(a)    \
   ((a) < AW36518_MIN_FLASH_INTENSITY ? 0 :    \
    (((a) - AW36518_MIN_FLASH_INTENSITY) / AW36518_FLASH_INTENSITY_STEP))
#define AW36518_FLASH_BRT_REG_TO_uA(a)        \
   ((a) * AW36518_FLASH_INTENSITY_STEP + AW36518_MIN_FLASH_INTENSITY)
 
/*  TORCH BRT
 *    min 750uA, step 1510uA, max 3860000uA
 */
#define AW36518_MIN_TORCH_INTENSITY    750
#define AW36518_MAX_TORCH_INTENSITY    386000
#define AW36518_TORCH_INTENSITY_DEFAULT    192000
#define AW36518_TORCH_INTENSITY_STEP    1510
#define AW36518_TORCH_BRT_uA_TO_REG(a)    \
   ((a) < AW36518_MIN_TORCH_INTENSITY ? 0 :    \
    (((a) - AW36518_MIN_TORCH_INTENSITY) / AW36518_TORCH_INTENSITY_STEP))
#define AW36518_TORCH_BRT_REG_TO_uA(a)        \
   ((a) * AW36518_TORCH_INTENSITY_STEP + AW36518_MIN_TORCH_INTENSITY)
 
 
/*  FLASH TIMEOUT DURATION
 *    min 40ms, step 40 or 200ms, max 1600ms
 */
#define TIMEOUT_MAX            1600000
#define TIMEOUT_STEP            40000
#define TIMEOUT_MIN            40000
#define TIMEOUT_STEP2            200000
#define TIMEOUT_DEFAULT            600000
 
#define AW36518_FLASH_TOUT_ms_TO_REG(a)    \
   ((a) < TIMEOUT_MIN ? 0 :    \
    (((a) - TIMEOUT_MIN) / TIMEOUT_STEP))
#define AW36518_FLASH_TOUT_REG_TO_ms(a)        \
   ((a) * TIMEOUT_STEP + TIMEOUT_MIN)
 
static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "debug level (0-2)");
 
enum aw36518_led_id {
   LED0 = 0,
   LED1,
   LED_MAX
};
 
struct aw36518_led {
   struct v4l2_subdev sd;
   struct v4l2_ctrl_handler ctrls;
   struct v4l2_ctrl *flash_brt;
   struct v4l2_ctrl *torch_brt;
   struct __kernel_old_timeval timestamp;
   u32 max_flash_timeout;
   u32 max_flash_intensity;
   u32 max_torch_intensity;
};
 
struct aw36518_flash {
   struct i2c_client *client;
   struct aw36518_led leds[LED_MAX];
 
   struct gpio_desc *en_gpio;
   struct gpio_desc *torch_gpio;
   struct gpio_desc *strobe_gpio;
   struct gpio_desc *tx_gpio;
 
   struct mutex lock;
 
   u32 flash_timeout;
   enum v4l2_flash_led_mode led_mode;
 
   u32 module_index;
   const char *module_facing;
   bool            power_on;
};
 
#define to_led(sd) container_of(sd, struct aw36518_led, sd)
 
static int aw36518_i2c_write(struct aw36518_flash *flash, u8 reg, u8 val)
{
   struct i2c_client *client = flash->client;
   int ret;
 
   ret = i2c_smbus_write_byte_data(client, reg, val);
   if (ret < 0)
       dev_err(&client->dev,
            "%s: reg:0x%x val:0x%x failed\n",
            __func__, reg, val);
 
   v4l2_dbg(2, debug, &flash->leds[0].sd,
        "%s: reg:0x%x val:0x%x\n",
         __func__, reg, val);
 
   return ret;
}
 
static int aw36518_i2c_read(struct aw36518_flash *flash, u8 reg)
{
   struct i2c_client *client = flash->client;
   int ret;
 
   ret = i2c_smbus_read_byte_data(client, reg);
   if (ret < 0)
       dev_err(&client->dev,
            "%s: reg:0x%x failed\n",
            __func__, reg);
 
   dev_dbg(&client->dev, "%s: reg:0x%x val:0x%x\n",
        __func__, reg, ret);
   return ret;
}
 
static int aw36518_led_on(struct aw36518_flash *flash, bool on)
{
   int ret;
   struct i2c_client *client = flash->client;
   int temp;
   u8 val = 0;
 
   v4l2_dbg(1, debug, &flash->leds[0].sd,
        "%s: on:%d\n", __func__, on);
 
   temp = aw36518_i2c_read(flash, AW36518_REG_ENABLE);
   if (temp < 0)
       dev_err(&client->dev,
            "%s: read reg:0x%x failed.\n",
            __func__, AW36518_REG_ENABLE);
 
   if (flash->led_mode == V4L2_FLASH_LED_MODE_FLASH && on)
       temp = temp | 0x0c;
   else
       temp = temp & 0xfc;
 
   v4l2_dbg(1, debug, &flash->leds[0].sd,
        "%s: temp:%d\n", __func__, temp);
   val = on ? LED_ON : LED_OFF;
   ret = aw36518_i2c_write(flash, AW36518_REG_ENABLE, val | temp);
   flash->leds[0].timestamp = ns_to_kernel_old_timeval(ktime_get_ns());
   flash->leds[1].timestamp = flash->leds[0].timestamp;
   return ret;
}
 
static int aw36518_get_fault(struct aw36518_flash *flash)
{
   int fault = 0;
   int temp = 0;
 
   fault = aw36518_i2c_read(flash, AW36518_REG_FAULT);
 
   v4l2_dbg(1, debug, &flash->leds[0].sd,
        "%s: 0x%x\n", __func__, fault);
   fault = fault & 0xfd;
 
   temp = aw36518_i2c_read(flash, AW36518_REG_FAULT2);
   fault = fault | (temp & 0x2);
 
   return fault;
}
 
static int aw36518_timeout_cal(struct aw36518_flash *flash)
{
   u8 val;
 
   v4l2_dbg(1, debug, &flash->leds[0].sd,
        "%s: timeout:%dUS\n", __func__, flash->flash_timeout);
 
   if (flash->flash_timeout < 400000)
       val =  AW36518_FLASH_TOUT_ms_TO_REG(flash->flash_timeout);
   else {
       val =  (flash->flash_timeout - 0x400000) / TIMEOUT_STEP2;
       val = val + 0x09;
   }
 
   return val;
}
 
static int aw36518_set_timeout(struct aw36518_flash *flash, u32 timeout)
{
   int ret;
   u8 val;
 
   v4l2_dbg(1, debug, &flash->leds[0].sd,
        "%s: %d\n",
        __func__, flash->flash_timeout);
 
   flash->flash_timeout = timeout;
   ret = aw36518_i2c_read(flash, 0x08);
   if (ret < 0)
       return ret;
 
   val =  aw36518_timeout_cal(flash);
 
   return aw36518_i2c_write(flash, 0x08, val | (ret & 0xf0));
}
 
static int aw36518_torch_brt(struct aw36518_flash *flash,
                enum aw36518_led_id id)
{
   struct aw36518_led *led = &flash->leds[id];
   u8 val, reg;
 
   v4l2_dbg(1, debug, &led->sd,
        "%s: %d\n", __func__, led->torch_brt->val);
 
   val = AW36518_TORCH_BRT_uA_TO_REG(led->torch_brt->val);
   reg = id ? AW36518_REG_LED1_TORCH_CUR : AW36518_REG_LED0_TORCH_CUR;
   return aw36518_i2c_write(flash, reg, val);
}
 
static int aw36518_flash_brt(struct aw36518_flash *flash,
                enum aw36518_led_id id)
{
   struct aw36518_led *led = &flash->leds[id];
   u8 val, reg;
 
   v4l2_dbg(1, debug, &led->sd,
        "%s: %d\n", __func__, led->flash_brt->val);
 
   val = AW36518_FLASH_BRT_uA_TO_REG(led->flash_brt->val);
   reg = id ? AW36518_REG_LED1_FLASH_CUR : AW36518_REG_LED0_FLASH_CUR;
   return aw36518_i2c_write(flash, reg, val);
}
 
static int aw36518_set_mode(struct aw36518_flash *flash,
               enum aw36518_led_id id, unsigned int mode)
{
   int ret = 0;
 
   v4l2_dbg(1, debug, &flash->leds[id].sd,
        "%s: %d cur:%d\n", __func__,
        mode, flash->led_mode);
 
   if (flash->led_mode == mode)
       return 0;
 
   aw36518_led_on(flash, false);
 
   flash->led_mode = mode;
 
   if (mode == V4L2_FLASH_LED_MODE_FLASH) {
       ret = aw36518_i2c_write(flash, 0x01, 0x0C);
       ret |= aw36518_flash_brt(flash, LED0);
   } else if (mode == V4L2_FLASH_LED_MODE_TORCH) {
       //ret = aw36518_i2c_write(flash, 0x01, 0x08);
       /* hw torch/strobe io trigger torch */
       ret = aw36518_i2c_write(flash, 0x01, AW36518_HW_TORCH);
       ret |= aw36518_torch_brt(flash, LED0);
       ret |= aw36518_led_on(flash, true);
       if (flash->torch_gpio) {
           v4l2_dbg(1, debug, &flash->leds[id].sd,
                "%s:set torch gpio high.\n", __func__);
           gpiod_set_value_cansleep(flash->torch_gpio, 1);
       }
 
   } else {
       ret = aw36518_i2c_write(flash, 0x01, 0x00);
       if (flash->torch_gpio) {
           v4l2_dbg(1, debug, &flash->leds[id].sd,
                "%s:set torch gpio low.\n", __func__);
           gpiod_set_value_cansleep(flash->torch_gpio, 0);
       }
 
   }
 
   return ret;
}
 
static int aw36518_strobe(struct aw36518_flash *flash, bool on)
{
   int ret;
 
   v4l2_dbg(1, debug, &flash->leds[0].sd,
        "%s: on %d\n", __func__, on);
 
   if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
       return -EBUSY;
   ret = aw36518_led_on(flash, on);
 
   return ret;
}
 
static int aw36518_get_ctrl(struct v4l2_ctrl *ctrl, enum aw36518_led_id id)
{
   struct aw36518_led *led =
       container_of(ctrl->handler, struct aw36518_led, ctrls);
   struct aw36518_flash *flash =
       container_of(led, struct aw36518_flash, leds[id]);
   int ret = 0;
   struct i2c_client *client = flash->client;
 
   v4l2_dbg(1, debug, &flash->leds[id].sd,
        "%s: id 0x%x\n", __func__, ctrl->id);
 
   mutex_lock(&flash->lock);
   switch (ctrl->id) {
   case V4L2_CID_FLASH_FAULT:
       ret = aw36518_get_fault(flash);
       ctrl->val = 0;
       if (ret & AW36518_REG_FL_SC)
           ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
       if (ret & AW36518_REG_FL_OT)
           ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
       if (ret & AW36518_REG_FL_TO)
           ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
       if (ret & AW36518_REG_FL_OVP)
           ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
       ret = 0;
       break;
   default:
       dev_err(&client->dev,
            "ctrl 0x%x not supported\n", ctrl->id);
       ret = -EINVAL;
       break;
   }
 
   mutex_unlock(&flash->lock);
   return ret;
}
 
static int aw36518_set_ctrl(struct v4l2_ctrl *ctrl, enum aw36518_led_id id)
{
   struct aw36518_led *led =
       container_of(ctrl->handler, struct aw36518_led, ctrls);
   struct aw36518_flash *flash =
       container_of(led, struct aw36518_flash, leds[id]);
   int ret = 0;
   struct i2c_client *client = flash->client;
 
   v4l2_dbg(1, debug, &led->sd,
        "%s: id 0x%x val 0x%x\n",
        __func__, ctrl->id, ctrl->val);
 
   mutex_lock(&flash->lock);
 
   ret = aw36518_get_fault(flash);
   if ((ret & (AW36518_REG_FL_OVP |
            AW36518_REG_FL_OT |
            AW36518_REG_FL_SC)) &&
       (ctrl->id == V4L2_CID_FLASH_STROBE ||
        ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
        ctrl->id == V4L2_CID_FLASH_LED_MODE)) {
       ret = -EBUSY;
       goto err;
   }
 
   switch (ctrl->id) {
   case V4L2_CID_FLASH_LED_MODE:
       ret = aw36518_set_mode(flash, id, ctrl->val);
       break;
   case V4L2_CID_FLASH_STROBE:
       ret = aw36518_strobe(flash, true);
       break;
   case V4L2_CID_FLASH_STROBE_STOP:
       ret = aw36518_strobe(flash, false);
       break;
   case V4L2_CID_FLASH_TIMEOUT:
       ret = aw36518_set_timeout(flash, ctrl->val);
       break;
   case V4L2_CID_FLASH_INTENSITY:
       ret = aw36518_flash_brt(flash, id);
       break;
   case V4L2_CID_FLASH_TORCH_INTENSITY:
       ret = aw36518_torch_brt(flash, id);
       break;
   default:
       dev_err(&client->dev,
           "ctrl 0x%x not supported\n", ctrl->id);
       ret = -EINVAL;
       break;
   }
 
err:
   mutex_unlock(&flash->lock);
   return ret;
}
 
static int aw36518_led0_get_ctrl(struct v4l2_ctrl *ctrl)
{
   return aw36518_get_ctrl(ctrl, LED0);
}
 
static int aw36518_led0_set_ctrl(struct v4l2_ctrl *ctrl)
{
   return aw36518_set_ctrl(ctrl, LED0);
}
 
static int aw36518_led1_get_ctrl(struct v4l2_ctrl *ctrl)
{
   return aw36518_get_ctrl(ctrl, LED1);
}
 
static int aw36518_led1_set_ctrl(struct v4l2_ctrl *ctrl)
{
   return aw36518_set_ctrl(ctrl, LED1);
}
 
static const struct v4l2_ctrl_ops aw36518_ctrl_ops[LED_MAX] = {
   [LED0] = {
       .g_volatile_ctrl = aw36518_led0_get_ctrl,
       .s_ctrl = aw36518_led0_set_ctrl,
   },
   [LED1] = {
       .g_volatile_ctrl = aw36518_led1_get_ctrl,
       .s_ctrl = aw36518_led1_set_ctrl,
   }
};
 
static int aw36518_init_controls(struct aw36518_flash *flash,
                enum aw36518_led_id id)
{
   struct v4l2_ctrl *fault;
   struct v4l2_ctrl_handler *hdl = &flash->leds[id].ctrls;
   const struct v4l2_ctrl_ops *ops = &aw36518_ctrl_ops[id];
   struct aw36518_led *led = &flash->leds[id];
 
   v4l2_ctrl_handler_init(hdl, 8);
 
   v4l2_ctrl_new_std_menu(hdl, ops,
                  V4L2_CID_FLASH_LED_MODE,
                  V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
   flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
 
   v4l2_ctrl_new_std_menu(hdl, ops,
                  V4L2_CID_FLASH_STROBE_SOURCE,
                  V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
 
   v4l2_ctrl_new_std(hdl, ops,
             V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
 
   v4l2_ctrl_new_std(hdl, ops,
             V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
 
   v4l2_ctrl_new_std(hdl, ops,
             V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
             led->max_flash_timeout,
             TIMEOUT_STEP,
             led->max_flash_timeout);
   flash->flash_timeout = led->max_flash_timeout;
 
   flash->leds[id].flash_brt =
       v4l2_ctrl_new_std(hdl, ops,
                 V4L2_CID_FLASH_INTENSITY,
                 AW36518_MIN_FLASH_INTENSITY,
                 led->max_flash_intensity,
                 AW36518_FLASH_INTENSITY_STEP,
                 AW36518_FLASH_INTENSITY_DEFAULT);
 
   flash->leds[id].torch_brt =
       v4l2_ctrl_new_std(hdl, ops,
                 V4L2_CID_FLASH_TORCH_INTENSITY,
                 AW36518_MIN_TORCH_INTENSITY,
                 led->max_torch_intensity,
                 AW36518_TORCH_INTENSITY_STEP,
                 AW36518_TORCH_INTENSITY_DEFAULT);
 
   fault = v4l2_ctrl_new_std(hdl, ops,
                 V4L2_CID_FLASH_FAULT, 0,
                 V4L2_FLASH_FAULT_OVER_VOLTAGE
                 | V4L2_FLASH_FAULT_TIMEOUT
                 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
                 | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
 
   if (hdl->error)
       return hdl->error;
 
   fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
 
   flash->leds[id].sd.ctrl_handler = hdl;
   return 0;
}
 
static void aw36518_get_time_info(struct v4l2_subdev *sd,
                 struct __kernel_old_timeval *ti)
{
   struct aw36518_led *led =
       container_of(sd, struct aw36518_led, sd);
 
   memset(ti, 0, sizeof(*ti));
   ti->tv_sec = led->timestamp.tv_sec;
   ti->tv_usec = led->timestamp.tv_usec;
   v4l2_dbg(1, debug, sd,
        "%s: tv_sec:%ld, tv_usec:%ld\n", __func__, ti->tv_sec, ti->tv_usec);
}
 
static long aw36518_ioctl(struct v4l2_subdev *sd,
            unsigned int cmd, void *arg)
{
   long ret = 0;
 
   switch (cmd) {
   case RK_VIDIOC_FLASH_TIMEINFO:
       aw36518_get_time_info(sd, (struct __kernel_old_timeval *)arg);
       break;
 
   default:
       ret = -ENOIOCTLCMD;
       break;
   }
   return ret;
}
 
#ifdef CONFIG_COMPAT
#define RK_VIDIOC_COMPAT_FLASH_TIMEINFO \
   _IOR('V', BASE_VIDIOC_PRIVATE + 0, struct old_timeval32)
 
static long aw36518_compat_ioctl32(struct v4l2_subdev *sd,
                 unsigned int cmd,
                 unsigned long arg)
{
   void __user *up = compat_ptr(arg);
   struct old_timeval32 *compat_t;
   long ret;
   struct __kernel_old_timeval t;
 
   switch (cmd) {
   case RK_VIDIOC_COMPAT_FLASH_TIMEINFO:
       compat_t = kzalloc(sizeof(*compat_t), GFP_KERNEL);
       if (!compat_t) {
           ret = -ENOMEM;
           return ret;
       }
       ret = aw36518_ioctl(sd, RK_VIDIOC_FLASH_TIMEINFO, &t);
       if (!ret) {
           compat_t->tv_sec = t.tv_sec;
           compat_t->tv_usec = t.tv_usec;
           ret = copy_to_user(up, compat_t, sizeof(*compat_t));
           if (ret)
               ret = -EFAULT;
       }
       kfree(compat_t);
       break;
 
   default:
       ret = -ENOIOCTLCMD;
       break;
   }
 
   return ret;
}
#endif
 
static int aw36518_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
   int rval;
 
   rval = pm_runtime_get_sync(sd->dev);
   if (rval < 0) {
       pm_runtime_put_noidle(sd->dev);
       return rval;
   }
 
   return 0;
}
 
static int aw36518_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
   pm_runtime_put(sd->dev);
 
   return 0;
}
 
static int aw36518_s_power(struct v4l2_subdev *sd, int on)
{
   struct aw36518_led *led = to_led(sd);
   struct aw36518_flash *flash =
       container_of(led, struct aw36518_flash, leds[0]);
   int ret = 0;
   struct i2c_client *client = flash->client;
 
   dev_info(&client->dev, "%s on(%d)\n", __func__, on);
   mutex_lock(&flash->lock);
 
   /* If the power state is not modified - no work to do. */
   if (flash->power_on == !!on)
       goto unlock_and_return;
 
   if (on) {
       ret = pm_runtime_get_sync(&client->dev);
       if (ret < 0) {
           pm_runtime_put_noidle(&client->dev);
           goto unlock_and_return;
       }
       flash->power_on = true;
   } else {
       pm_runtime_put(&client->dev);
       flash->power_on = false;
   }
 
unlock_and_return:
   mutex_unlock(&flash->lock);
 
   return ret;
}
 
static const struct v4l2_subdev_core_ops aw36518_core_ops = {
   .s_power = aw36518_s_power,
   .ioctl = aw36518_ioctl,
#ifdef CONFIG_COMPAT
   .compat_ioctl32 = aw36518_compat_ioctl32
#endif
};
 
static const struct v4l2_subdev_ops aw36518_ops = {
   .core = &aw36518_core_ops,
};
 
static const struct v4l2_subdev_internal_ops aw36518_internal_ops = {
   .open = aw36518_open,
   .close = aw36518_close,
};
 
static int __aw36518_set_power(struct aw36518_flash *flash, bool on)
{
   gpiod_direction_output(flash->en_gpio, on);
 
   return 0;
}
 
static int aw36518_check_id(struct aw36518_flash *flash)
{
   int ret = 0;
 
   __aw36518_set_power(flash, true);
   ret = aw36518_i2c_read(flash, AW36518_REG_ID);
   if (ret != AW36518_ID) {
       dev_err(&flash->client->dev,
           "Read chip id error\n");
       return -ENODEV;
   }
 
   dev_info(&flash->client->dev,
        "Detected aw36518 flash id:0x%x\n", ret);
   return 0;
}
 
static int aw36518_of_init(struct i2c_client *client,
              struct aw36518_flash *flash)
{
   struct device_node *node = client->dev.of_node;
   struct device_node *child;
   struct aw36518_led *led;
   int ret = 0;
 
   if (!node) {
       dev_err(&client->dev,
           "get device node failed\n");
       goto err;
   }
 
   ret = of_property_read_u32(node,
                  RKMODULE_CAMERA_MODULE_INDEX,
                  &flash->module_index);
   ret |= of_property_read_string(node,
                      RKMODULE_CAMERA_MODULE_FACING,
                      &flash->module_facing);
   if (ret) {
       dev_err(&client->dev,
           "could not get module information!\n");
       goto err;
   }
 
   flash->en_gpio = devm_gpiod_get(&client->dev,
                   "enable", GPIOD_OUT_LOW);
   if (IS_ERR(flash->en_gpio)) {
       flash->en_gpio = NULL;
       dev_warn(&client->dev,
            "get enable-gpio failed, using assist light mode\n");
   }
 
   flash->torch_gpio = devm_gpiod_get(&client->dev,
                      "torch", GPIOD_OUT_LOW);
   if (IS_ERR(flash->torch_gpio)) {
       flash->torch_gpio = NULL;
       dev_warn(&client->dev,
            "get torch-gpio failed, using assist light mode\n");
   }
 
   flash->strobe_gpio = devm_gpiod_get(&client->dev,
                       "strobe", GPIOD_OUT_LOW);
   if (IS_ERR(flash->strobe_gpio)) {
       flash->strobe_gpio = NULL;
       dev_warn(&client->dev,
            "get strobe-gpio failed, using assist light mode\n");
 
   }
   flash->tx_gpio = devm_gpiod_get(&client->dev,
                       "tx", GPIOD_OUT_LOW);
   if (IS_ERR(flash->tx_gpio)) {
       flash->tx_gpio = NULL;
       dev_warn(&client->dev,
            "get tx-gpio failed, using assist light mode\n");
   }
 
   for_each_child_of_node(node, child) {
       u32 id = 0;
 
       of_property_read_u32(child, "reg", &id);
       if (id >= LED_MAX) {
           dev_err(&client->dev, "only support 2 leds\n");
           goto err;
       }
       led = &flash->leds[id];
       led->sd.fwnode = of_fwnode_handle(child);
       if (of_property_read_u32(child, "flash-max-timeout-us",
                    &led->max_flash_timeout)) {
           dev_err(&client->dev,
               "get led%d flash-max-timeout-us fail\n", id);
           goto err;
       }
       if (led->max_flash_timeout > TIMEOUT_MAX)
           led->max_flash_timeout = TIMEOUT_MAX;
 
       if (of_property_read_u32(child, "flash-max-microamp",
                    &led->max_flash_intensity)) {
           dev_err(&client->dev,
               "get led%d flash-max-microamp fail\n", id);
           goto err;
       }
       if (led->max_flash_intensity > AW36518_MAX_FLASH_INTENSITY)
           led->max_flash_intensity = AW36518_MAX_FLASH_INTENSITY;
 
       if (of_property_read_u32(child, "led-max-microamp",
                     &led->max_torch_intensity)) {
           dev_err(&client->dev,
               "get led%d led-max-microamp fail\n", id);
           goto err;
       }
       if (led->max_torch_intensity > AW36518_MAX_TORCH_INTENSITY)
           led->max_torch_intensity = AW36518_MAX_TORCH_INTENSITY;
 
       v4l2_dbg(1, debug, &led->sd,
            "led%d max torch:%dUA flash:%dUA timeout:%dUS\n",
            id, led->max_torch_intensity,
            led->max_flash_intensity,
            led->max_flash_timeout);
   }
 
   return 0;
err:
   return -EINVAL;
}
 
static int aw36518_init_device(struct aw36518_flash *flash)
{
   int ret;
   unsigned int reg_val;
   struct i2c_client *client = flash->client;
 
   /* output disable */
   flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
   ret = aw36518_set_mode(flash, 0, 0);
   if (ret < 0)
       return ret;
   /* reset faults */
   reg_val = aw36518_i2c_read(flash, AW36518_REG_FAULT);
   dev_info(&client->dev, "%s: fault: 0x%x.\n", __func__, reg_val);
   /* tx input default low */
   if (flash->tx_gpio)
       gpiod_direction_output(flash->tx_gpio, 0);
   /* STROBE/Torch input default low */
   if (flash->torch_gpio)
       gpiod_set_value_cansleep(flash->torch_gpio, 0);
 
   return ret;
}
 
static int aw36518_probe(struct i2c_client *client,
            const struct i2c_device_id *devid)
{
   struct aw36518_flash *flash;
   struct v4l2_subdev *sd;
   char facing[2];
   int i, ret;
 
   dev_info(&client->dev, "driver version: %02x.%02x.%02x",
        DRIVER_VERSION >> 16,
        (DRIVER_VERSION & 0xff00) >> 8,
        DRIVER_VERSION & 0x00ff);
 
   flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
   if (!flash)
       return -ENOMEM;
 
   flash->client = client;
 
   ret = aw36518_of_init(client, flash);
   if (ret)
       return ret;
 
   ret = aw36518_check_id(flash);
   if (ret)
       goto err_power_off;
 
   for (i = 0; i < LED_MAX; i++) {
       sd = &flash->leds[i].sd;
       v4l2_i2c_subdev_init(sd, client, &aw36518_ops);
       sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
       sd->internal_ops = &aw36518_internal_ops;
 
       memset(facing, 0, sizeof(facing));
       if (strcmp(flash->module_facing, "back") == 0)
           facing[0] = 'b';
       else
           facing[0] = 'f';
       /* NOTE: to distinguish between two led
        * name: led0 meet the main led
        * name: led1 meet the secondary led
        */
       snprintf(sd->name, sizeof(sd->name),
            "m%02d_%s_%s_led%d %s",
            flash->module_index, facing,
            AW36518_NAME, i, dev_name(sd->dev));
 
       ret = aw36518_init_controls(flash, i);
       if (ret)
           goto err;
 
       ret = media_entity_pads_init(&sd->entity, 0, NULL);
       if (ret < 0)
           goto free_ctl;
 
       sd->entity.function = MEDIA_ENT_F_FLASH;
       ret = v4l2_async_register_subdev(sd);
       if (ret)
           goto free_media;
   }
 
   ret = aw36518_init_device(flash);
   if (ret < 0)
       goto free_media;
 
   i2c_set_clientdata(client, flash);
 
   mutex_init(&flash->lock);
 
   pm_runtime_set_active(&client->dev);
   pm_runtime_enable(&client->dev);
   pm_runtime_idle(&client->dev);
 
   dev_info(&client->dev, "probing successful\n");
 
   return 0;
free_media:
   media_entity_cleanup(&flash->leds[i].sd.entity);
free_ctl:
   v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
err:
   for (--i; i >= 0; --i) {
       v4l2_device_unregister_subdev(&flash->leds[i].sd);
       media_entity_cleanup(&flash->leds[i].sd.entity);
       v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
   }
err_power_off:
   __aw36518_set_power(flash, false);
   return ret;
}
 
static int aw36518_remove(struct i2c_client *client)
{
   struct aw36518_flash *flash = i2c_get_clientdata(client);
   unsigned int i;
 
   pm_runtime_disable(&client->dev);
   for (i = 0; i < LED_MAX; i++) {
       v4l2_device_unregister_subdev(&flash->leds[i].sd);
       v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
       media_entity_cleanup(&flash->leds[i].sd.entity);
   }
   mutex_destroy(&flash->lock);
   return 0;
}
 
static int __maybe_unused aw36518_runtime_suspend(struct device *dev)
{
   struct i2c_client *client = to_i2c_client(dev);
   struct aw36518_flash *flash = i2c_get_clientdata(client);
 
   return __aw36518_set_power(flash, false);
}
 
static int __maybe_unused aw36518_runtime_resume(struct device *dev)
{
   struct i2c_client *client = to_i2c_client(dev);
   struct aw36518_flash *flash = i2c_get_clientdata(client);
 
   return __aw36518_set_power(flash, true);
}
 
static const struct i2c_device_id aw36518_id_table[] = {
   { AW36518_NAME, 0 },
   { { 0 } }
};
MODULE_DEVICE_TABLE(i2c, aw36518_id_table);
 
static const struct of_device_id aw36518_of_table[] = {
   { .compatible = "awinic,aw36518" },
   { { 0 } }
};
 
static const struct dev_pm_ops aw36518_pm_ops = {
   SET_RUNTIME_PM_OPS(aw36518_runtime_suspend,
              aw36518_runtime_resume, NULL)
};
 
static struct i2c_driver aw36518_i2c_driver = {
   .driver    = {
       .name = AW36518_NAME,
       .pm = &aw36518_pm_ops,
       .of_match_table = aw36518_of_table,
   },
   .probe = aw36518_probe,
   .remove    = aw36518_remove,
   .id_table = aw36518_id_table,
};
 
module_i2c_driver(aw36518_i2c_driver);
 
MODULE_DESCRIPTION("AW36518 LED flash driver");
MODULE_LICENSE("GPL");