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
|
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/mfd/core.h>
#include <misc/wl2866d.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <asm/unaligned.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/pm.h>
#include <linux/regulator/consumer.h>
#define WL2866D_ID 0x64
#define WL2866D_ID1 0x55
#define AW_I2C_RETRIES 2
#define AW_I2C_RETRY_DELAY 2
//#ifdef __XIAOMI_CAMERA__
struct wl2866d_chip *camera_chip;
//#endif
static const struct wl2866d_map wl2866d_on_config[] = {
{0x03, 0x55},
{0x04, 0x55},
{0x05, 0x80},
{0x06, 0x88},
{0x0E, 0x0F},
{0x0E, 0x00},
};
static int wl2866d_i2c_write(struct wl2866d_chip *chip,
unsigned char reg_addr, unsigned char reg_data)
{
int ret = -1;
unsigned char cnt = 0;
while (cnt < AW_I2C_RETRIES) {
ret =
i2c_smbus_write_byte_data(chip->client, reg_addr, reg_data);
if (ret < 0) {
pr_err("%s: i2c_write cnt=%d error=%d\n", __func__, cnt,
ret);
} else {
break;
}
cnt++;
msleep(AW_I2C_RETRY_DELAY);
}
return ret;
}
static int wl2866d_i2c_read(struct wl2866d_chip *chip,
unsigned char reg_addr, unsigned char *reg_data)
{
int ret = -1;
unsigned char cnt = 0;
while (cnt < AW_I2C_RETRIES) {
ret = i2c_smbus_read_byte_data(chip->client, reg_addr);
if (ret < 0) {
pr_err("%s: i2c_read cnt=%d error=%d\n", __func__, cnt,
ret);
} else {
*reg_data = ret;
break;
}
cnt++;
msleep(AW_I2C_RETRY_DELAY);
}
return ret;
}
int wl2866d_camera_power_down_all(void)
{
int ret = -1;
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, 0);//bit1
if (ret < 0) {
pr_err("xyz wl2866d set enable failed\n");
return ret;
}
return ret;
}
//#ifdef __XIAOMI_CAMERA__
//bit0:DVDD1, bit1:DVDD2, bit2:AVDD1, bit3:AVDD2
//{0x03, 0x55}, OUT_DVDD1
//{0x04, 0x55}, OUT_DVDD2
//{0x05, 0x80}, OUT_AVDD1
//{0x06, 0x88}, OUT_AVDD2
//{0x0E, 0x0F}, VOL_ENABLE
//{0x0E, 0x00}, VOL_DISABLE
int wl2866d_camera_power_up(uint16_t camera_id)
{
int ret = -1;
unsigned char reg_val = 0;
if (0 == camera_id) {
//camera_id 0 --> imx682
pr_debug("xyz wide:imx682 camera_id=[%d] power up\n", camera_id);
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[OUT_DVDD2].reg, 0x55);//bit1
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[OUT_AVDD2].reg, 0x88);//bit3
if (ret < 0) {
pr_err("xyz wl2866d set avdd2/dvdd2 failed\n");
return ret;
}
ret = wl2866d_i2c_read(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, ®_val);
if (ret < 0) {
pr_err("xyz wl2866d read enable failed\n");
return ret;
}
pr_debug("xyz before set enable value = 0x%x\n", reg_val);
reg_val |= 0b1010;//bit1,bit3
pr_debug("xyz after set enable value = 0x%x\n", reg_val);
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, reg_val);//bit1
if (ret < 0) {
pr_err("xyz wl2866d set enable failed\n");
return ret;
}
}
else if ((1 == camera_id) || (2 == camera_id) || (4 == camera_id)) {
//camera_id 2 --> s5k3t2
//camera_id 1 --> ov02b1b/gc02m1b
//camera_id 4 --> hi259
if (2 == camera_id) {
pr_debug("xyz front:s5k3t2 camera_id=[%d] power up\n", camera_id);
} else if(1 == camera_id) {
pr_debug("xyz depth:ov02b1b/gc02m1b camera_id=[%d] power up\n", camera_id);
} else if(4 == camera_id) {
pr_debug("xyz macro:hi259 camera_id=[%d] power up\n", camera_id);
}
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[OUT_AVDD1].reg, 0x80);//bit2
if (ret < 0) {
pr_err("xyz wl2866d set avdd1 failed\n");
return ret;
}
ret = wl2866d_i2c_read(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, ®_val);
if (ret < 0) {
pr_err("xyz wl2866d read enable failed\n");
return ret;
}
pr_debug("xyz before set enable value = 0x%x\n", reg_val);
reg_val |= 0b0100;//bit2
pr_debug("xyz after set enable value = 0x%x\n", reg_val);
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, reg_val);//bit1
if (ret < 0) {
pr_err("xyz wl2866d set enable failed\n");
return ret;
}
}
else if ((3 == camera_id) || (5 == camera_id)) {
//camera_id 5 --> hi847
//camera_id 3 --> hi1337
if (3 == camera_id) {
pr_debug("xyz ulta wide:hi1337 camera_id=[%d] power up\n", camera_id);
} else if(5 == camera_id) {
pr_debug("xyz tele:hi847 camera_id=[%d] power up\n", camera_id);
}
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[OUT_DVDD1].reg, 0x55);//bit2
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[OUT_AVDD1].reg, 0x80);//bit2
if (ret < 0) {
pr_err("xyz wl2866d set avdd1 failed\n");
return ret;
}
ret = wl2866d_i2c_read(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, ®_val);
if (ret < 0) {
pr_err("xyz wl2866d read enable failed\n");
return ret;
}
pr_debug("xyz before set enable value = 0x%x\n", reg_val);
reg_val |= 0b0101;//bit0,bit2
pr_debug("xyz after set enable value = 0x%x\n", reg_val);
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, reg_val);//bit1
if (ret < 0) {
pr_err("xyz wl2866d set enable failed\n");
return ret;
}
} else {
pr_err("xyz wl2866d unknown camera!!!\n");
}
pr_debug("xyz wl2866d result = %d\n", ret);
return ret;
}
EXPORT_SYMBOL(wl2866d_camera_power_up);
int wl2866d_camera_power_down(uint16_t camera_id)
{
int ret = -1;
unsigned char reg_val = 0;
if (0 == camera_id) {
//camera_id 0 --> imx682
pr_debug("xyz wide:imx682 camera_id=[%d] power down\n", camera_id);
ret = wl2866d_i2c_read(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, ®_val);
if (ret < 0) {
pr_err("xyz wl2866d read enable failed\n");
return ret;
}
pr_debug("xyz before set enable value = 0x%x\n", reg_val);
reg_val &= 0b0101;//bit1,bit3
pr_debug("xyz after set enable value = 0x%x\n", reg_val);
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, reg_val);//bit1
if (ret < 0) {
pr_err("xyz wl2866d set enable failed\n");
return ret;
}
}
else if ((1 == camera_id) || (2 == camera_id) || (4 == camera_id)) {
//camera_id 2 --> s5k3t2
//camera_id 1 --> ov02b1b/gc02m1b
//camera_id 4 --> hi259
if (2 == camera_id) {
pr_debug("xyz front:s5k3t2 camera_id=[%d] power down\n", camera_id);
} else if(1 == camera_id) {
pr_debug("xyz depth:ov02b1b/gc02m1b camera_id=[%d] power down\n", camera_id);
} else if(4 == camera_id) {
pr_debug("xyz macro:hi259 camera_id=[%d] power down\n", camera_id);
}
ret = wl2866d_i2c_read(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, ®_val);
if (ret < 0) {
pr_err("xyz wl2866d read enable failed\n");
return ret;
}
pr_debug("xyz before set enable value = 0x%x\n", reg_val);
reg_val &= 0b1011;//bit2
pr_debug("xyz after set enable value = 0x%x\n", reg_val);
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, reg_val);//bit1
if (ret < 0) {
pr_err("xyz wl2866d set enable failed\n");
return ret;
}
}
else if ((3 == camera_id) || (5 == camera_id)) {
//camera_id 5 --> hi847
//camera_id 3 --> hi1337
if (3 == camera_id) {
pr_debug("xyz ulta wide:hi1337 camera_id=[%d] power up\n", camera_id);
} else if(5 == camera_id) {
pr_debug("xyz tele:hi847 camera_id=[%d] power up\n", camera_id);
}
ret = wl2866d_i2c_read(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, ®_val);
if (ret < 0) {
pr_err("xyz wl2866d read enable failed\n");
return ret;
}
pr_debug("xyz before set enable value = 0x%x\n", reg_val);
reg_val &= 0b1010;//bit0,bit2
pr_debug("xyz after set enable value = 0x%x\n", reg_val);
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, reg_val);//bit1
if (ret < 0) {
pr_err("xyz wl2866d set enable failed\n");
return ret;
}
} else {
pr_err("xyz wl2866d unknown camera!!!\n");
}
pr_debug("xyz wl2866d result = %d\n", ret);
return ret;
}
EXPORT_SYMBOL(wl2866d_camera_power_down);
//#endif
static ssize_t wl2866d_vol_enable_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct wl2866d_chip *chip = dev_get_drvdata(dev);
unsigned char reg_val = 0;
wl2866d_i2c_read(chip, wl2866d_on_config[VOL_ENABLE].reg, ®_val);
return snprintf(buf, 10, "%d\n", reg_val);
}
static ssize_t wl2866d_vol_enable_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
int ret = 0;
unsigned char reg_val = 0;
struct wl2866d_chip *chip = dev_get_drvdata(dev);
ret = kstrtou8(buf, 0, ®_val);
if (ret < 0)
return ret;
ret = wl2866d_i2c_write(chip, wl2866d_on_config[VOL_ENABLE].reg, reg_val);
if (ret < 0) {
pr_err("wl2866d set enable failed\n");
return ret;
}
return count;
}
static ssize_t wl2866d_out_avdd2_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct wl2866d_chip *chip = dev_get_drvdata(dev);
unsigned char reg_val = 0;
wl2866d_i2c_read(chip, wl2866d_on_config[OUT_AVDD2].reg, ®_val);
return snprintf(buf, 10, "%d\n", reg_val);
}
static ssize_t wl2866d_out_avdd2_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
int ret = 0;
unsigned char reg_val = 0;
struct wl2866d_chip *chip = dev_get_drvdata(dev);
ret = kstrtou8(buf, 0, ®_val);
if (ret < 0)
return ret;
ret = wl2866d_i2c_write(chip, wl2866d_on_config[OUT_AVDD2].reg, reg_val);
if (ret < 0) {
pr_err("wl2866d open avdd2 failed\n");
return ret;
}
return count;
}
static ssize_t wl2866d_out_avdd1_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct wl2866d_chip *chip = dev_get_drvdata(dev);
unsigned char reg_val = 0;
wl2866d_i2c_read(chip, wl2866d_on_config[OUT_AVDD1].reg, ®_val);
return snprintf(buf, 10, "%d\n", reg_val);
}
static ssize_t wl2866d_out_avdd1_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
int ret = 0;
unsigned char reg_val = 0;
struct wl2866d_chip *chip = dev_get_drvdata(dev);
ret = kstrtou8(buf, 0, ®_val);
if (ret < 0)
return ret;
ret = wl2866d_i2c_write(chip, wl2866d_on_config[OUT_AVDD1].reg, reg_val);
if (ret < 0) {
pr_err("wl2866d open avdd1 failed\n");
return ret;
}
return count;
}
static ssize_t wl2866d_out_dvdd2_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct wl2866d_chip *chip = dev_get_drvdata(dev);
unsigned char reg_val = 0;
wl2866d_i2c_read(chip, wl2866d_on_config[OUT_DVDD1].reg, ®_val);
return snprintf(buf, 10, "%d\n", reg_val);
}
static ssize_t wl2866d_out_dvdd2_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
int ret = 0;
unsigned char reg_val = 0;
struct wl2866d_chip *chip = dev_get_drvdata(dev);
ret = kstrtou8(buf, 0, ®_val);
if (ret < 0)
return ret;
ret = wl2866d_i2c_write(chip, wl2866d_on_config[OUT_DVDD1].reg, reg_val);
if (ret < 0) {
pr_err("wl2866d open dvdd2 failed\n");
return ret;
}
return count;
}
static ssize_t wl2866d_out_dvdd1_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct wl2866d_chip *chip = dev_get_drvdata(dev);
unsigned char reg_val = 0;
wl2866d_i2c_read(chip, wl2866d_on_config[OUT_DVDD1].reg, ®_val);
return snprintf(buf, 10, "%d\n", reg_val);
}
static ssize_t wl2866d_out_dvdd1_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
int ret = 0;
unsigned char reg_val = 0;
struct wl2866d_chip *chip = dev_get_drvdata(dev);
ret = kstrtou8(buf, 0, ®_val);
if (ret < 0)
return ret;
ret = wl2866d_i2c_write(chip, wl2866d_on_config[OUT_DVDD1].reg, reg_val);
if (ret < 0) {
pr_err("wl2866d open dvdd1 failed\n");
return ret;
}
return count;
}
static DEVICE_ATTR(vol_enable, 0664, wl2866d_vol_enable_show, wl2866d_vol_enable_store);
static DEVICE_ATTR(out_avdd2, 0664, wl2866d_out_avdd2_show, wl2866d_out_avdd2_store);
static DEVICE_ATTR(out_avdd1, 0664, wl2866d_out_avdd1_show, wl2866d_out_avdd1_store);
static DEVICE_ATTR(out_dvdd2, 0664, wl2866d_out_dvdd2_show, wl2866d_out_dvdd2_store);
static DEVICE_ATTR(out_dvdd1, 0664, wl2866d_out_dvdd1_show, wl2866d_out_dvdd1_store);
static struct attribute *wl2866d_attributes[] = {
&dev_attr_out_dvdd1.attr,
&dev_attr_out_dvdd2.attr,
&dev_attr_out_avdd1.attr,
&dev_attr_out_avdd2.attr,
&dev_attr_vol_enable.attr,
NULL
};
static struct attribute_group wl2866d_attribute_group = {
.attrs = wl2866d_attributes
};
static int wl2866d_get_id(struct wl2866d_chip *chip)
{
unsigned char reg_val = 0;
int ret = 0;
wl2866d_i2c_read(chip, wl2866d_on_config[OUT_DVDD1].reg, ®_val);
pr_debug("%s:wl2866d id is %d\n", __func__, reg_val);
if ((reg_val != WL2866D_ID) && (reg_val != WL2866D_ID1)) {
ret = -1;
return ret;
}
return 0;
}
static int set_init_voltage(struct wl2866d_chip *chip)
{
int ret = 0;
int i;
for (i = 0 ; i < (ARRAY_SIZE(wl2866d_on_config) - 1); i++) {
ret = wl2866d_i2c_write(chip, wl2866d_on_config[i].reg, wl2866d_on_config[i].value);
if (ret < 0) {
pr_err("wl2866d init voltage failed\n");
return ret;
}
}
return 0;
}
int wl2866d_camera_power_up_eeprom(void)
{
int ret = -1;
ret = set_init_voltage(camera_chip);
return ret;
}
EXPORT_SYMBOL(wl2866d_camera_power_up_eeprom);
int wl2866d_camera_power_up_all(void)
{
int ret = -1;
ret = set_init_voltage(camera_chip);
return ret;
}
EXPORT_SYMBOL(wl2866d_camera_power_up_all);
int wl2866d_camera_power_down_eeprom(void)
{
int ret = -1;
ret = wl2866d_i2c_write(camera_chip, wl2866d_on_config[VOL_ENABLE].reg, 0);//bit1
return ret;
}
EXPORT_SYMBOL(wl2866d_camera_power_down_eeprom);
void wl2866d_print_reg(struct wl2866d_chip *chip)
{
int i;
unsigned char reg_val = 0;
for (i = 0 ; i < ARRAY_SIZE(wl2866d_on_config); i++) {
wl2866d_i2c_read(chip, wl2866d_on_config[i].reg, ®_val);
pr_debug("%s:wl2866d info is reg %d, value %d\n", __func__, wl2866d_on_config[i].reg, reg_val);
}
}
static int wl2866d_init(struct wl2866d_chip *chip)
{
int ret = 0;
chip->en_gpio = of_get_named_gpio(chip->dev->of_node,
"en-gpio", 0);
if (!gpio_is_valid(chip->en_gpio)) {
pr_err("%s:%d, en gpio not specified\n",
__func__, __LINE__);
return -EINVAL;
}
pr_debug("%s: en_gpio is %d\n", __func__, chip->en_gpio);
ret = gpio_request(chip->en_gpio, "wl2866d_en");
//ret = devm_gpio_request_one(chip->dev, chip->en_gpio,
// GPIOF_OUT_INIT_LOW,
// "wl2866d_en");
if (ret < 0) {
pr_err("wl2866d enable gpio request failed\n");
return ret;
}
gpio_direction_output(chip->en_gpio, 0);
//if (chip && gpio_is_valid(chip->en_gpio)) {
// gpio_set_value_cansleep(chip->en_gpio, 1);
//}
msleep(10);
ret = wl2866d_get_id(chip);
if (ret < 0) {
pr_err("wl2866d read id failed\n");
return ret;
}
ret = set_init_voltage(chip);
if (ret < 0)
pr_err("wl2866d init failed\n");
if (WL2866D_DEBUG) {
msleep(10);
wl2866d_print_reg(chip);
}
return 0;
}
static int wl2866d_disable_power(struct wl2866d_chip *chip)
{
int ret = 0;
ret = regulator_disable(chip->vin1);
if (ret)
dev_err(chip->dev, "Unable to disable vin1:%d\n", ret);
if (!regulator_is_enabled(chip->vin1)) {
ret = regulator_set_voltage(chip->vin1, 0,
VIN1_1P35_VOL_MAX);
if (ret)
dev_err(chip->dev,
"Unable to set (0) voltage for vin1:%d\n", ret);
}
ret = regulator_disable(chip->vin2);
if (ret)
dev_err(chip->dev, "Unable to disable vin2:%d\n", ret);
if (!regulator_is_enabled(chip->vin2)) {
ret = regulator_set_voltage(chip->vin2, 0,
VIN2_3P3_VOL_MAX);
if (ret)
dev_err(chip->dev,
"Unable to set (0) voltage for vin2:%d\n", ret);
}
return 0;
}
static int wl2866d_enable_power(struct wl2866d_chip *chip)
{
int ret = 0;
ret = regulator_set_voltage(chip->vin1, VIN1_1P35_VOL_MIN,
VIN1_1P35_VOL_MAX);
if (ret) {
dev_err(chip->dev,
"Unable to set voltage for vin1:%d\n", ret);
goto put_vin1;
}
ret = regulator_enable(chip->vin1);
if (ret) {
dev_err(chip->dev, "Unable to enable vin1:%d\n", ret);
goto unset_vin1;
}
ret = regulator_set_voltage(chip->vin2, VIN2_3P3_VOL_MIN,
VIN2_3P3_VOL_MAX);
if (ret) {
dev_err(chip->dev,
"Unable to set voltage for vin2:%d\n", ret);
goto disable_vin1;
}
ret = regulator_enable(chip->vin2);
if (ret) {
dev_err(chip->dev, "Unable to enable vin2:%d\n", ret);
goto unset_vin2;
}
return 0;
unset_vin2:
ret = regulator_set_voltage(chip->vin2, 0, VIN2_3P3_VOL_MAX);
if (ret)
dev_err(chip->dev,
"Unable to set (0) voltage for vin2:%d\n", ret);
disable_vin1:
ret = regulator_disable(chip->vin1);
if (ret)
dev_err(chip->dev, "Unable to disable vin1:%d\n", ret);
unset_vin1:
ret = regulator_set_voltage(chip->vin1, 0, VIN1_1P35_VOL_MAX);
if (ret)
dev_err(chip->dev,
"Unable to set (0) voltage for vin1:%d\n", ret);
put_vin1:
return ret;
}
static int wl2866d_vreg_init(struct wl2866d_chip *chip)
{
int ret = 0;
chip->vin1 = devm_regulator_get(chip->dev, "vin1");
if (IS_ERR(chip->vin1)) {
ret = PTR_ERR(chip->vin1);
dev_err(chip->dev, "%s: can't get VIN1,%d\n", __func__, ret);
goto err_vin1;
}
chip->vin2 = devm_regulator_get(chip->dev, "vin2");
if (IS_ERR(chip->vin2)) {
ret = PTR_ERR(chip->vin2);
dev_err(chip->dev, "%s: can't get VIN2,%d\n", __func__, ret);
goto err_vin2;
}
return 0;
err_vin2:
devm_regulator_put(chip->vin1);
err_vin1:
return ret;
}
static int wl2866d_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int ret = 0;
struct wl2866d_chip *chip;
pr_debug("%s,enrty\n", __func__);
chip = devm_kzalloc(&client->dev, sizeof(struct wl2866d_chip), GFP_KERNEL);
if (!chip) {
ret = -ENOMEM;
goto err_mem;
}
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
dev_err(&client->dev, "check_functionality failed\n");
ret = -EIO;
goto init_err;
}
chip->client = client;
chip->dev = &client->dev;
dev_set_drvdata(chip->dev, chip);
i2c_set_clientdata(chip->client, chip);
ret = wl2866d_vreg_init(chip);
if (ret < 0) {
dev_err(&client->dev, "get vreg failed\n");
goto vreg_init_err;
}
ret = wl2866d_enable_power(chip);
if (ret) {
dev_err(&client->dev, "enable power failed\n");
ret = -1;
goto vreg_enable_err;
}
ret = wl2866d_init(chip);
if (ret < 0) {
dev_err(&client->dev, "wl2866d init fail!\n");
ret = -ENODEV;
goto init_err;
}
ret = sysfs_create_group(&client->dev.kobj,
&wl2866d_attribute_group);
if (ret < 0) {
dev_info(&client->dev, "%s error creating sysfs attr files\n",
__func__);
goto err_sysfs;
}
//#ifdef __XIAOMI_CAMERA__
camera_chip = chip;
//#endif
pr_info("%s, successfully\n", __func__);
return 0;
err_sysfs:
init_err:
vreg_enable_err:
devm_regulator_put(chip->vin1);
devm_regulator_put(chip->vin2);
vreg_init_err:
devm_kfree(chip->dev, chip);
chip = NULL;
err_mem:
return ret;
}
static int wl2866d_remove(struct i2c_client *client)
{
struct wl2866d_chip *chip = i2c_get_clientdata(client);
devm_kfree(chip->dev, chip);
chip = NULL;
return 0;
}
#ifdef CONFIG_PM_SLEEP
static int wl2866d_suspend(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct wl2866d_chip *chip = i2c_get_clientdata(client);
int ret = 0;
ret = wl2866d_i2c_write(chip, wl2866d_on_config[VOL_DISABLE].reg, wl2866d_on_config[VOL_DISABLE].value);
if (ret < 0)
pr_err("wl2866d close voltage failed\n");
wl2866d_disable_power(chip);
return 0;
}
static int wl2866d_resume(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct wl2866d_chip *chip = i2c_get_clientdata(client);
//#ifdef __XIAOMI_CAMERA__
//int ret = 0;
wl2866d_enable_power(chip);
gpio_direction_output(chip->en_gpio, 0);
/*
ret = wl2866d_i2c_write(chip, wl2866d_on_config[VOL_ENABLE].reg, wl2866d_on_config[VOL_ENABLE].value);
if (ret < 0) {
pr_err("wl2866d set enable failed\n");
return ret;
}
*/
//#endif
return 0;
}
#endif
static const struct dev_pm_ops wl2866d_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(wl2866d_suspend, wl2866d_resume)
};
static const struct i2c_device_id wl2866d_id_table[] = {
{"longcheer,wl2866d", 0},
{} /* NULL terminated */
};
MODULE_DEVICE_TABLE(i2c, wl2866d_id_table);
#ifdef CONFIG_OF
static const struct of_device_id wl2866d_i2c_of_match_table[] = {
{ .compatible = "longcheer,wl2866d" },
{},
};
MODULE_DEVICE_TABLE(of, wl2866d_i2c_of_match_table);
#endif
static struct i2c_driver wl2866d_driver = {
.driver = {
.name = "longcheer,wl2866d",
.pm = &wl2866d_pm_ops,
.of_match_table = of_match_ptr(wl2866d_i2c_of_match_table),
},
.probe = wl2866d_probe,
.remove = wl2866d_remove,
.id_table = wl2866d_id_table,
};
static int __init wl2866d_i2c_init(void)
{
return i2c_add_driver(&wl2866d_driver);
}
subsys_initcall(wl2866d_i2c_init);
//module_init(wl2866d_i2c_init);
static void __exit wl2866d_i2c_exit(void)
{
i2c_del_driver(&wl2866d_driver);
}
module_exit(wl2866d_i2c_exit);
MODULE_DESCRIPTION("PMIC Driver for Longcheer wl2866d");
MODULE_AUTHOR("Li Wei <liwei6@longcheer.com>");
MODULE_LICENSE("GPL");
|