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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
|
/*
* max77705.c - mfd core driver for the Maxim 77705
*
* Copyright (C) 2016 Samsung Electronics
* Insun Choi <insun77.choi@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* This driver is based on max8997.c
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/mutex.h>
#include <linux/mfd/core.h>
#include <linux/mfd/max77705.h>
#include <linux/mfd/max77705-private.h>
#include <linux/regulator/machine.h>
#include <linux/delay.h>
#include <linux/workqueue.h>
#include <linux/muic/muic.h>
#include <linux/muic/max77705-muic.h>
#include <linux/ccic/max77705_usbc.h>
//#include <linux/ccic/max77705_pass2.h>
#include <linux/ccic/max77705_pass3.h>
#include <linux/ccic/max77705_pass4.h>
#if defined(CONFIG_MAX77705_FW_PID03_SUPPORT)
#include <linux/ccic/max77705C_pass2_PID03.h>
#else
#include <linux/ccic/max77705C_pass2.h>
#endif
#include <linux/usb_notify.h>
#if defined(CONFIG_OF)
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#endif /* CONFIG_OF */
#include "../battery_v2/include/sec_charging_common.h"
#define I2C_ADDR_PMIC (0xCC >> 1) /* Top sys, Haptic */
#define I2C_ADDR_MUIC (0x4A >> 1)
#define I2C_ADDR_CHG (0xD2 >> 1)
#define I2C_ADDR_FG (0x6C >> 1)
#define I2C_ADDR_DEBUG (0xC4 >> 1)
#define I2C_RETRY_CNT 3
/*
* pmic revision information
*/
struct max77705_revision_struct {
u8 id;
u8 rev;
u8 logical_id;
};
static struct max77705_revision_struct max77705_revision[3] = {
{ 0x5, 0x3, 0x3}, // MD05 PASS3
{ 0x5, 0x4, 0x4}, // MD05 PASS4
{ 0x15,0x2, 0x5}, // MD15 PASS2
};
static struct mfd_cell max77705_devs[] = {
#if defined(CONFIG_CCIC_MAX77705)
{ .name = "max77705-usbc", },
#endif
#if defined(CONFIG_REGULATOR_MAX77705)
{ .name = "max77705-safeout", },
#endif /* CONFIG_REGULATOR_MAX77705 */
#if defined(CONFIG_FUELGAUGE_MAX77705)
{ .name = "max77705-fuelgauge", },
#endif
#if defined(CONFIG_CHARGER_MAX77705)
{ .name = "max77705-charger", },
#endif
#if defined(CONFIG_MOTOR_DRV_MAX77705)
{ .name = "max77705-haptic", },
#endif /* CONFIG_MAX77705_HAPTIC */
#if defined(CONFIG_LEDS_MAX77705_RGB)
{ .name = "leds-max77705-rgb", },
#endif /* CONFIG_LEDS_MAX77705_RGB */
#if defined(CONFIG_LEDS_MAX77705_FLASH)
{ .name = "max77705-flash", },
#endif
};
int max77705_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
{
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
struct otg_notify *o_notify = get_otg_notify();
int ret, i;
mutex_lock(&max77705->i2c_lock);
for (i = 0; i < I2C_RETRY_CNT; ++i) {
ret = i2c_smbus_read_byte_data(i2c, reg);
if (ret >= 0)
break;
pr_info("%s:%s reg(0x%x), ret(%d), i2c_retry_cnt(%d/%d)\n",
MFD_DEV_NAME, __func__, reg, ret, i + 1, I2C_RETRY_CNT);
}
mutex_unlock(&max77705->i2c_lock);
if (ret < 0) {
pr_info("%s:%s reg(0x%x), ret(%d)\n", MFD_DEV_NAME, __func__, reg, ret);
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify)
inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT);
#endif
return ret;
}
ret &= 0xff;
*dest = ret;
return 0;
}
EXPORT_SYMBOL_GPL(max77705_read_reg);
int max77705_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
{
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
struct otg_notify *o_notify = get_otg_notify();
int ret, i;
mutex_lock(&max77705->i2c_lock);
for (i = 0; i < I2C_RETRY_CNT; ++i) {
ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
if (ret >= 0)
break;
pr_info("%s:%s reg(0x%x), ret(%d), i2c_retry_cnt(%d/%d)\n",
MFD_DEV_NAME, __func__, reg, ret, i + 1, I2C_RETRY_CNT);
}
mutex_unlock(&max77705->i2c_lock);
if (ret < 0) {
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify)
inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT);
#endif
return ret;
}
return 0;
}
EXPORT_SYMBOL_GPL(max77705_bulk_read);
int max77705_read_word(struct i2c_client *i2c, u8 reg)
{
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
struct otg_notify *o_notify = get_otg_notify();
int ret, i;
mutex_lock(&max77705->i2c_lock);
for (i = 0; i < I2C_RETRY_CNT; ++i) {
ret = i2c_smbus_read_word_data(i2c, reg);
if (ret >= 0)
break;
pr_info("%s:%s reg(0x%x), ret(%d), i2c_retry_cnt(%d/%d)\n",
MFD_DEV_NAME, __func__, reg, ret, i + 1, I2C_RETRY_CNT);
}
mutex_unlock(&max77705->i2c_lock);
#if defined(CONFIG_USB_HW_PARAM)
if (ret < 0) {
if (o_notify)
inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT);
}
#endif
return ret;
}
EXPORT_SYMBOL_GPL(max77705_read_word);
int max77705_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
{
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
struct otg_notify *o_notify = get_otg_notify();
int ret = -EIO, i;
int timeout = 2000; /* 2sec */
int interval = 100;
while (ret == -EIO) {
mutex_lock(&max77705->i2c_lock);
for (i = 0; i < I2C_RETRY_CNT; ++i) {
ret = i2c_smbus_write_byte_data(i2c, reg, value);
if ((ret >= 0) || (ret == -EIO))
break;
pr_info("%s:%s reg(0x%02x), ret(%d), i2c_retry_cnt(%d/%d)\n",
MFD_DEV_NAME, __func__, reg, ret, i + 1, I2C_RETRY_CNT);
}
mutex_unlock(&max77705->i2c_lock);
if (ret < 0) {
pr_info("%s:%s reg(0x%x), ret(%d), timeout %d\n",
MFD_DEV_NAME, __func__, reg, ret, timeout);
if (timeout < 0)
break;
msleep(interval);
timeout -= interval;
}
}
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify && ret < 0)
inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT);
#endif
return ret;
}
EXPORT_SYMBOL_GPL(max77705_write_reg);
int max77705_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
{
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
struct otg_notify *o_notify = get_otg_notify();
int ret = -EIO, i;
int timeout = 2000; /* 2sec */
int interval = 100;
while (ret == -EIO) {
mutex_lock(&max77705->i2c_lock);
for (i = 0; i < I2C_RETRY_CNT; ++i) {
ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
if ((ret >= 0) || (ret == -EIO))
break;
pr_info("%s:%s reg(0x%x), ret(%d), i2c_retry_cnt(%d/%d)\n",
MFD_DEV_NAME, __func__, reg, ret, i + 1, I2C_RETRY_CNT);
}
mutex_unlock(&max77705->i2c_lock);
if (ret < 0) {
pr_info("%s:%s reg(0x%x), ret(%d), timeout %d\n",
MFD_DEV_NAME, __func__, reg, ret, timeout);
if (timeout < 0)
break;
msleep(interval);
timeout -= interval;
}
}
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify && ret < 0)
inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT);
#endif
return ret;
}
EXPORT_SYMBOL_GPL(max77705_bulk_write);
int max77705_write_word(struct i2c_client *i2c, u8 reg, u16 value)
{
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
struct otg_notify *o_notify = get_otg_notify();
int ret, i;
mutex_lock(&max77705->i2c_lock);
for (i = 0; i < I2C_RETRY_CNT; ++i) {
ret = i2c_smbus_write_word_data(i2c, reg, value);
if (ret >= 0)
break;
pr_info("%s:%s reg(0x%x), ret(%d), i2c_retry_cnt(%d/%d)\n",
MFD_DEV_NAME, __func__, reg, ret, i + 1, I2C_RETRY_CNT);
}
mutex_unlock(&max77705->i2c_lock);
if (ret < 0) {
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify)
inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT);
#endif
return ret;
}
return 0;
}
EXPORT_SYMBOL_GPL(max77705_write_word);
int max77705_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
{
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
struct otg_notify *o_notify = get_otg_notify();
int ret, i;
u8 old_val, new_val;
mutex_lock(&max77705->i2c_lock);
for (i = 0; i < I2C_RETRY_CNT; ++i) {
ret = i2c_smbus_read_byte_data(i2c, reg);
if (ret >= 0)
break;
pr_info("%s:%s read reg(0x%x), ret(%d), i2c_retry_cnt(%d/%d)\n",
MFD_DEV_NAME, __func__, reg, ret, i + 1, I2C_RETRY_CNT);
}
if (ret < 0) {
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify)
inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT);
#endif
goto err;
}
if (ret >= 0) {
old_val = ret & 0xff;
new_val = (val & mask) | (old_val & (~mask));
for (i = 0; i < I2C_RETRY_CNT; ++i) {
ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
if (ret >= 0)
break;
pr_info("%s:%s write reg(0x%x), ret(%d), i2c_retry_cnt(%d/%d)\n",
MFD_DEV_NAME, __func__, reg, ret, i + 1, I2C_RETRY_CNT);
}
if (ret < 0) {
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify)
inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT);
#endif
goto err;
}
}
err:
mutex_unlock(&max77705->i2c_lock);
return ret;
}
EXPORT_SYMBOL_GPL(max77705_update_reg);
#if defined(CONFIG_OF)
static int of_max77705_dt(struct device *dev, struct max77705_platform_data *pdata)
{
struct device_node *np_max77705 = dev->of_node;
struct device_node *np_battery;
int ret = 0;
if (!np_max77705)
return -EINVAL;
pdata->irq_gpio = of_get_named_gpio(np_max77705, "max77705,irq-gpio", 0);
pdata->wakeup = of_property_read_bool(np_max77705, "max77705,wakeup");
if (of_property_read_u32(np_max77705, "max77705,fw_product_id", &pdata->fw_product_id))
pdata->fw_product_id = 0;
#if defined(CONFIG_SEC_FACTORY)
pdata->blocking_waterevent = 0;
#else
pdata->blocking_waterevent = of_property_read_bool(np_max77705, "max77705,blocking_waterevent");
#endif
pr_info("%s: irq-gpio: %u\n", __func__, pdata->irq_gpio);
np_battery = of_find_node_by_name(NULL, "battery");
if (!np_battery) {
pr_info("%s: np_battery NULL\n", __func__);
} else {
pdata->wpc_en = of_get_named_gpio(np_battery, "battery,wpc_en", 0);
if (pdata->wpc_en < 0) {
pr_info("%s: can't get wpc_en (%d)\n", __func__, pdata->wpc_en);
pdata->wpc_en = 0;
}
ret = of_property_read_string(np_battery,
"battery,wireless_charger_name", (char const **)&pdata->wireless_charger_name);
if (ret)
pr_info("%s: Wireless charger name is Empty\n", __func__);
}
pdata->support_audio = of_property_read_bool(np_max77705, "max77705,support-audio");
pr_info("%s: support_audio %d\n", __func__, pdata->support_audio);
return 0;
}
#endif /* CONFIG_OF */
/* samsung */
#ifdef CONFIG_CCIC_MAX77705
static void max77705_reset_ic(struct max77705_dev *max77705)
{
msg_maxim("Reset!!");
max77705_write_reg(max77705->muic, 0x80, 0x0F);
msleep(100);
}
static void max77705_usbc_wait_response_q(struct work_struct *work)
{
struct max77705_dev *max77705;
u8 read_value = 0x00;
u8 dummy[2] = { 0, };
max77705 = container_of(work, struct max77705_dev, fw_work);
while (max77705->fw_update_state == FW_UPDATE_WAIT_RESP_START) {
switch (max77705->pmic_rev) {
case MAX77705_PASS1:
max77705_bulk_read(max77705->muic, REG_UIC_INT-1, 2, dummy);
read_value = dummy[1];
break;
default:
max77705_bulk_read(max77705->muic, REG_UIC_INT, 1, dummy);
read_value = dummy[0];
break;
}
if ((read_value & BIT_APCmdResI) == BIT_APCmdResI)
break;
}
complete_all(&max77705->fw_completion);
}
static int max77705_usbc_wait_response(struct max77705_dev *max77705)
{
unsigned long time_remaining = 0;
max77705->fw_update_state = FW_UPDATE_WAIT_RESP_START;
init_completion(&max77705->fw_completion);
queue_work(max77705->fw_workqueue, &max77705->fw_work);
time_remaining = wait_for_completion_timeout(
&max77705->fw_completion,
msecs_to_jiffies(FW_WAIT_TIMEOUT));
max77705->fw_update_state = FW_UPDATE_WAIT_RESP_STOP;
if (!time_remaining) {
msg_maxim("Failed to update due to timeout");
cancel_work_sync(&max77705->fw_work);
return FW_UPDATE_TIMEOUT_FAIL;
}
return 0;
}
static int __max77705_usbc_fw_update(
struct max77705_dev *max77705, const u8 *fw_bin)
{
u8 fw_cmd = FW_CMD_END;
u8 fw_len = 0;
u8 fw_opcode = 0;
u8 fw_data_len = 0;
u8 fw_data[FW_CMD_WRITE_SIZE] = { 0, };
u8 verify_data[FW_VERIFY_DATA_SIZE] = { 0, };
int ret = -FW_UPDATE_CMD_FAIL;
/*
* fw_bin[0] = Write Command (0x01)
* or
* fw_bin[0] = Read Command (0x03)
* or
* fw_bin[0] = End Command (0x00)
*/
fw_cmd = fw_bin[0];
/*
* Check FW Command
*/
if (fw_cmd == FW_CMD_END) {
max77705_reset_ic(max77705);
max77705->fw_update_state = FW_UPDATE_END;
return FW_UPDATE_END;
}
/*
* fw_bin[1] = Length ( OPCode + Data )
*/
fw_len = fw_bin[1];
/*
* Check fw data length
* We support 0x22 or 0x04 only
*/
if (fw_len != 0x22 && fw_len != 0x04)
return FW_UPDATE_MAX_LENGTH_FAIL;
/*
* fw_bin[2] = OPCode
*/
fw_opcode = fw_bin[2];
/*
* In case write command,
* fw_bin[35:3] = Data
*
* In case read command,
* fw_bin[5:3] = Data
*/
fw_data_len = fw_len - 1; /* exclude opcode */
memcpy(fw_data, &fw_bin[3], fw_data_len);
switch (fw_cmd) {
case FW_CMD_WRITE:
if (fw_data_len > I2C_SMBUS_BLOCK_MAX) {
/* write the half data */
max77705_bulk_write(max77705->muic,
fw_opcode,
I2C_SMBUS_BLOCK_HALF,
fw_data);
max77705_bulk_write(max77705->muic,
fw_opcode + I2C_SMBUS_BLOCK_HALF,
fw_data_len - I2C_SMBUS_BLOCK_HALF,
&fw_data[I2C_SMBUS_BLOCK_HALF]);
} else
max77705_bulk_write(max77705->muic,
fw_opcode,
fw_data_len,
fw_data);
ret = max77705_usbc_wait_response(max77705);
if (ret)
return ret;
/*
* Why do we need 1ms sleep in case MQ81?
*/
/* msleep(1); */
return FW_CMD_WRITE_SIZE;
case FW_CMD_READ:
max77705_bulk_read(max77705->muic,
fw_opcode,
fw_data_len,
verify_data);
/*
* Check fw data sequence number
* It should be increased from 1 step by step.
*/
if (memcmp(verify_data, &fw_data[1], 2)) {
msg_maxim("[0x%02x 0x%02x], [0x%02x, 0x%02x], [0x%02x, 0x%02x]",
verify_data[0], fw_data[0],
verify_data[1], fw_data[1],
verify_data[2], fw_data[2]);
return FW_UPDATE_VERIFY_FAIL;
}
return FW_CMD_READ_SIZE;
}
msg_maxim("Command error");
return ret;
}
int max77705_write_jig_on(struct max77705_dev *max77705)
{
u8 write_values[OPCODE_MAX_LENGTH] = { 0, };
int ret = 0;
int length = 0x2;
int i = 0;
write_values[0] = OPCODE_CTRL3_W;
write_values[1] = 0x1;
write_values[2] = 0x1;
for (i = 0; i < length + OPCODE_SIZE; i++)
msg_maxim("[%d], 0x[%x]", i, write_values[i]);
/* Write opcode and data */
ret = max77705_bulk_write(max77705->muic, OPCODE_WRITE,
length + OPCODE_SIZE, write_values);
/* Write end of data by 0x00 */
if (length < OPCODE_DATA_LENGTH)
max77705_write_reg(max77705->muic, OPCODE_WRITE_END, 0x00);
return 0;
}
static int max77705_fuelgauge_read_vcell(struct max77705_dev *max77705)
{
u8 data[2];
u32 vcell;
u16 w_data;
u32 temp;
u32 temp2;
if (max77705_bulk_read(max77705->fuelgauge, VCELL_REG, 2, data) < 0) {
pr_err("%s: Failed to read VCELL\n", __func__);
return -1;
}
w_data = (data[1] << 8) | data[0];
temp = (w_data & 0xFFF) * 78125;
vcell = temp / 1000000;
temp = ((w_data & 0xF000) >> 4) * 78125;
temp2 = temp / 1000000;
vcell += (temp2 << 4);
return vcell;
}
static void max77705_wc_control(struct max77705_dev *max77705, bool enable)
{
struct power_supply *psy = NULL;
union power_supply_propval value = {0, };
char wpc_en_status[2];
psy = get_power_supply_by_name(max77705->pdata->wireless_charger_name);
if (psy) {
wpc_en_status[0] = WPC_EN_CCIC;
wpc_en_status[1] = enable ? true : false;
value.strval= wpc_en_status;
if ((psy->desc->set_property != NULL) &&
(psy->desc->set_property(psy, (enum power_supply_property)POWER_SUPPLY_EXT_PROP_WPC_EN, &value) >= 0))
pr_info("%s: WC CONTROL: %s\n", __func__, wpc_en_status[1] ? "Enable" : "Disable");
power_supply_put(psy);
} else {
if (max77705->pdata->wpc_en) {
if (enable) {
gpio_direction_output(max77705->pdata->wpc_en, 0);
pr_info("%s: WC CONTROL: ENABLE\n", __func__);
} else {
gpio_direction_output(max77705->pdata->wpc_en, 1);
pr_info("%s: WC CONTROL: DISABLE\n", __func__);
}
} else {
pr_info("%s : no wpc_en\n", __func__);
}
}
pr_info("%s: wpc_en(%d)\n", __func__, gpio_get_value(max77705->pdata->wpc_en));
}
int max77705_usbc_fw_update(struct max77705_dev *max77705,
const u8 *fw_bin, int fw_bin_len, int enforce_do)
{
max77705_fw_header *fw_header;
struct otg_notify *o_notify = get_otg_notify();
int offset = 0;
unsigned long duration = 0;
int size = 0;
int try_count = 0;
int ret = 0;
u8 pd_status1 = 0x0;
static u8 fct_id; /* FCT cable */
u8 try_command = 0;
u8 sw_boot = 0;
u8 chg_cnfg_00 = 0;
bool chg_mode_changed = 0;
bool wpc_en_changed = 0;
int vcell = 0;
u8 vbvolt = 0;
u8 wcin_dtls = 0;
u8 chg_curr = 0;
u8 vchgin = 0;
int error = 0;
fw_header = (max77705_fw_header *)fw_bin;
msg_maxim("FW: magic/%x/ major/%x/ minor/%x/ product_id/%x/ rev/%x/ id/%x/",
fw_header->magic, fw_header->major, fw_header->minor, fw_header->product_id, fw_header->rev, fw_header->id);
if(max77705->device_product_id != fw_header->product_id) {
msg_maxim("product indicator mismatch");
return 0;
}
if(fw_header->magic == MAX77705_SIGN)
msg_maxim("FW: matched");
max77705_read_reg(max77705->charger, MAX77705_CHG_REG_CNFG_00, &chg_cnfg_00);
retry:
disable_irq(max77705->irq);
max77705_write_reg(max77705->muic, REG_PD_INT_M, 0xFF);
max77705_write_reg(max77705->muic, REG_CC_INT_M, 0xFF);
max77705_write_reg(max77705->muic, REG_UIC_INT_M, 0xFF);
max77705_write_reg(max77705->muic, REG_VDM_INT_M, 0xFF);
offset = 0;
duration = 0;
size = 0;
ret = 0;
/* to do (unmask interrupt) */
ret = max77705_read_reg(max77705->muic, REG_UIC_FW_REV, &max77705->FW_Revision);
ret = max77705_read_reg(max77705->muic, REG_UIC_FW_MINOR, &max77705->FW_Minor_Revision);
if (ret < 0 && (try_count == 0 && try_command == 0)) {
msg_maxim("Failed to read FW_REV");
error = -EIO;
goto out;
}
duration = jiffies;
max77705->FW_Product_ID = max77705->FW_Minor_Revision >> FW_PRODUCT_ID_REG;
max77705->FW_Minor_Revision &= MINOR_VERSION_MASK;
msg_maxim("chip : %02X.%02X(PID 0x%x), FW : %02X.%02X(PID 0x%x)",
max77705->FW_Revision, max77705->FW_Minor_Revision, max77705->FW_Product_ID,
fw_header->major, fw_header->minor, fw_header->product_id);
store_ccic_bin_version(&fw_header->major, &sw_boot);
#ifdef CONFIG_SEC_FACTORY
if ((max77705->FW_Revision != fw_header->major) || (max77705->FW_Minor_Revision != fw_header->minor)) {
#else
if ((max77705->FW_Revision == 0xff) || (max77705->FW_Revision < fw_header->major) ||
((max77705->FW_Revision == fw_header->major) && (max77705->FW_Minor_Revision < fw_header->minor)) ||
(max77705->FW_Product_ID != fw_header->product_id) ||
enforce_do) {
#endif
if (!enforce_do && max77705->pmic_rev > MAX77705_PASS1) { /* on Booting time */
max77705_read_reg(max77705->muic, REG_PD_STATUS1, &pd_status1);
fct_id = (pd_status1 & BIT_FCT_ID) >> FFS(BIT_FCT_ID);
msg_maxim("FCT_ID : 0x%x", fct_id);
}
if (try_count == 0 && try_command == 0) {
/* change chg_mode during FW update */
vcell = max77705_fuelgauge_read_vcell(max77705);
if (vcell < 3600) {
pr_info("%s: keep chg_mode(0x%x), vcell(%dmv)\n",
__func__, chg_cnfg_00 & 0x0F, vcell);
error = -EAGAIN;
goto out;
}
}
max77705_read_reg(max77705->charger, MAX77705_CHG_REG_DETAILS_00, &wcin_dtls);
wcin_dtls = (wcin_dtls & 0x18) >> 3;
wpc_en_changed = true;
max77705_wc_control(max77705, false);
max77705_read_reg(max77705->muic, MAX77705_USBC_REG_BC_STATUS, &vbvolt);
pr_info("%s: BC:0x%02x, vbvolt:0x%x, wcin_dtls:0x%x\n",
__func__, vbvolt, ((vbvolt & 0x80) >> 7), wcin_dtls);
if (!(vbvolt & 0x80) && (wcin_dtls != 0x3)) {
chg_mode_changed = true;
/* Switching Frequency : 3MHz */
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_08, 0x00, 0x3);
pr_info("%s: +set Switching Frequency 3Mhz\n", __func__);
/* Disable skip mode */
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_12, 0x1, 0x1);
pr_info("%s: +set Disable skip mode\n", __func__);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_00, 0x09, 0x0F);
pr_info("%s: +change chg_mode(0x9), vcell(%dmv)\n",
__func__, vcell);
} else {
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_12, 0x18, 0x18);
max77705_read_reg(max77705->charger, MAX77705_CHG_REG_CNFG_12, &vchgin);
pr_info("%s: -set aicl, (0x%02x)\n", __func__, vchgin);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_02, 0x0, 0x3F);
max77705_read_reg(max77705->charger, MAX77705_CHG_REG_CNFG_02, &chg_curr);
pr_info("%s: -set charge curr 100mA, (0x%02x)\n", __func__, chg_curr);
if (chg_mode_changed) {
chg_mode_changed = false;
/* Auto skip mode */
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_12, 0x0, 0x1);
pr_info("%s: -set Auto skip mode\n", __func__);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_12, 0x0, 0x20);
pr_info("%s: -disable CHGINSEL\n", __func__);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_00, 0x4, 0x0F);
pr_info("%s: -set chg_mode(0x4)\n", __func__);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_12, 0x20, 0x20);
pr_info("%s: -enable CHGINSEL\n", __func__);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_00, chg_cnfg_00, 0x0F);
pr_info("%s: -recover chg_mode(0x%x), vcell(%dmv)\n",
__func__, chg_cnfg_00 & 0x0F, vcell);
/* Switching Frequency : 1.5MHz */
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_08, 0x02, 0x3);
pr_info("%s: -set Switching Frequency 1.5MHz\n", __func__);
}
}
msleep(500);
max77705_write_reg(max77705->muic, 0x21, 0xD0);
max77705_write_reg(max77705->muic, 0x41, 0x00);
msleep(500);
max77705_read_reg(max77705->muic, REG_UIC_FW_REV, &max77705->FW_Revision);
max77705_read_reg(max77705->muic, REG_UIC_FW_MINOR, &max77705->FW_Minor_Revision);
max77705->FW_Minor_Revision &= MINOR_VERSION_MASK;
msg_maxim("Start FW updating (%02X.%02X)", max77705->FW_Revision, max77705->FW_Minor_Revision);
if (max77705->FW_Revision != 0xFF && try_command < 3) {
try_command++;
max77705_reset_ic(max77705);
msleep(1000);
goto retry;
}
if (max77705->FW_Revision != 0xFF) {
if (++try_count < FW_VERIFY_TRY_COUNT) {
msg_maxim("the Fail to enter secure mode %d",
try_count);
max77705_reset_ic(max77705);
msleep(1000);
goto retry;
} else {
msg_maxim("the Secure Update Fail!!");
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify)
inc_hw_param(o_notify, USB_CCIC_FWUP_ERROR_COUNT);
#endif
error = -EIO;
goto out;
}
}
for (offset = FW_HEADER_SIZE;
offset < fw_bin_len && size != FW_UPDATE_END;) {
size = __max77705_usbc_fw_update(max77705, &fw_bin[offset]);
switch (size) {
case FW_UPDATE_VERIFY_FAIL:
offset -= FW_CMD_WRITE_SIZE;
/* FALLTHROUGH */
case FW_UPDATE_TIMEOUT_FAIL:
/*
* Retry FW updating
*/
if (++try_count < FW_VERIFY_TRY_COUNT) {
msg_maxim("Retry fw write. ret %d, count %d, offset %d",
size, try_count, offset);
max77705_reset_ic(max77705);
msleep(1000);
goto retry;
} else {
msg_maxim("Failed to update FW. ret %d, offset %d",
size, (offset + size));
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify)
inc_hw_param(o_notify, USB_CCIC_FWUP_ERROR_COUNT);
#endif
error = -EIO;
goto out;
}
break;
case FW_UPDATE_CMD_FAIL:
case FW_UPDATE_MAX_LENGTH_FAIL:
msg_maxim("Failed to update FW. ret %d, offset %d",
size, (offset + size));
#if defined(CONFIG_USB_HW_PARAM)
if (o_notify)
inc_hw_param(o_notify, USB_CCIC_FWUP_ERROR_COUNT);
#endif
error = -EIO;
goto out;
case FW_UPDATE_END: /* 0x00 */
/* JIG PIN for setting HIGH. */
if (fct_id == FCT_523Kohm && !enforce_do)
max77705_write_jig_on(max77705);
max77705_read_reg(max77705->muic,
REG_UIC_FW_REV, &max77705->FW_Revision);
max77705_read_reg(max77705->muic,
REG_UIC_FW_MINOR, &max77705->FW_Minor_Revision);
max77705->FW_Minor_Revision &= MINOR_VERSION_MASK;
msg_maxim("chip : %02X.%02X, FW : %02X.%02X",
max77705->FW_Revision, max77705->FW_Minor_Revision,
fw_header->major, fw_header->minor);
msg_maxim("Completed");
break;
default:
offset += size;
break;
}
if (offset == fw_bin_len) {
max77705_reset_ic(max77705);
max77705_read_reg(max77705->muic,
REG_UIC_FW_REV, &max77705->FW_Revision);
max77705_read_reg(max77705->muic,
REG_UIC_FW_MINOR, &max77705->FW_Minor_Revision);
max77705->FW_Minor_Revision &= MINOR_VERSION_MASK;
msg_maxim("chip : %02X.%02X, FW : %02X.%02X",
max77705->FW_Revision, max77705->FW_Minor_Revision,
fw_header->major, fw_header->minor);
msg_maxim("Completed via SYS path");
}
}
} else {
msg_maxim("Don't need to update!");
goto out;
}
duration = jiffies - duration;
msg_maxim("Duration : %dms", jiffies_to_msecs(duration));
out:
if (chg_mode_changed) {
vcell = max77705_fuelgauge_read_vcell(max77705);
/* Auto skip mode */
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_12, 0x0, 0x1);
pr_info("%s: -set Auto skip mode\n", __func__);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_12, 0x0, 0x20);
pr_info("%s: -disable CHGINSEL\n", __func__);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_00, 0x4, 0x0F);
pr_info("%s: -set chg_mode(0x4)\n", __func__);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_12, 0x20, 0x20);
pr_info("%s: -enable CHGINSEL\n", __func__);
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_00, chg_cnfg_00, 0x0F);
pr_info("%s: -recover chg_mode(0x%x), vcell(%dmv)\n",
__func__, chg_cnfg_00 & 0x0F, vcell);
/* Switching Frequency : 1.5MHz */
max77705_update_reg(max77705->charger,
MAX77705_CHG_REG_CNFG_08, 0x02, 0x3);
pr_info("%s: -set Switching Frequency 1.5MHz\n", __func__);
}
if (wpc_en_changed) {
max77705_wc_control(max77705, true);
}
enable_irq(max77705->irq);
return error;
}
EXPORT_SYMBOL_GPL(max77705_usbc_fw_update);
void max77705_usbc_fw_setting(struct max77705_dev *max77705, int enforce_do)
{
switch (max77705->pmic_rev) {
case MAX77705_PASS1:
pr_info("[MAX77705] Doesn't update the MAX77705_PASS1\n");
break;
case MAX77705_PASS2:
pr_info("[MAX77705] Doesn't update the MAX77705_PASS2\n");
break;
case MAX77705_PASS3:
max77705_usbc_fw_update(max77705, BOOT_FLASH_FW_PASS3, ARRAY_SIZE(BOOT_FLASH_FW_PASS3), enforce_do);
break;
case MAX77705_PASS4:
max77705_usbc_fw_update(max77705, BOOT_FLASH_FW_PASS4, ARRAY_SIZE(BOOT_FLASH_FW_PASS4), enforce_do);
break;
case MAX77705_PASS5:
max77705_usbc_fw_update(max77705, BOOT_FLASH_FW_PASS2, ARRAY_SIZE(BOOT_FLASH_FW_PASS2), enforce_do);
break;
default:
pr_info("[MAX77705] FAIL F/W ON BOOTING TIME and PMIC_REVISION isn't valid\n");
break;
};
}
EXPORT_SYMBOL_GPL(max77705_usbc_fw_setting);
#endif
static u8 max77705_revision_check(u8 pmic_id, u8 pmic_rev) {
int i, logical_id = 0;
int pmic_arrary = ARRAY_SIZE(max77705_revision);
for (i = 0; i < pmic_arrary; i++) {
if (max77705_revision[i].id == pmic_id &&
max77705_revision[i].rev == pmic_rev)
logical_id = max77705_revision[i].logical_id;
}
return logical_id;
}
void max77705_register_pdmsg_func(struct max77705_dev *max77705,
void (*check_pdmsg)(void *data, u8 pdmsg), void *data)
{
if (!max77705) {
pr_err("%s max77705 is null\n", __func__);
return;
}
max77705->check_pdmsg = check_pdmsg;
max77705->usbc_data = data;
}
EXPORT_SYMBOL_GPL(max77705_register_pdmsg_func);
static int max77705_i2c_probe(struct i2c_client *i2c,
const struct i2c_device_id *dev_id)
{
struct max77705_dev *max77705;
struct max77705_platform_data *pdata = i2c->dev.platform_data;
int ret = 0;
u8 pmic_id, pmic_rev = 0;
pr_info("%s:%s\n", MFD_DEV_NAME, __func__);
max77705 = kzalloc(sizeof(struct max77705_dev), GFP_KERNEL);
if (!max77705)
return -ENOMEM;
if (i2c->dev.of_node) {
pdata = devm_kzalloc(&i2c->dev, sizeof(struct max77705_platform_data),
GFP_KERNEL);
if (!pdata) {
ret = -ENOMEM;
goto err;
}
ret = of_max77705_dt(&i2c->dev, pdata);
if (ret < 0) {
dev_err(&i2c->dev, "Failed to get device of_node\n");
goto err;
}
i2c->dev.platform_data = pdata;
} else
pdata = i2c->dev.platform_data;
max77705->dev = &i2c->dev;
max77705->i2c = i2c;
max77705->irq = i2c->irq;
if (pdata) {
max77705->pdata = pdata;
pdata->irq_base = irq_alloc_descs(-1, 0, MAX77705_IRQ_NR, -1);
if (pdata->irq_base < 0) {
pr_err("%s:%s irq_alloc_descs Fail! ret(%d)\n",
MFD_DEV_NAME, __func__, pdata->irq_base);
ret = -EINVAL;
goto err;
} else
max77705->irq_base = pdata->irq_base;
max77705->irq_gpio = pdata->irq_gpio;
max77705->wakeup = pdata->wakeup;
max77705->blocking_waterevent = pdata->blocking_waterevent;
max77705->device_product_id = pdata->fw_product_id;
} else {
ret = -EINVAL;
goto err;
}
mutex_init(&max77705->i2c_lock);
i2c_set_clientdata(i2c, max77705);
if (max77705_read_reg(i2c, MAX77705_PMIC_REG_PMICID1, &pmic_id) < 0) {
dev_err(max77705->dev, "device not found on this channel (this is not an error)\n");
ret = -ENODEV;
goto err_w_lock;
}
if (max77705_read_reg(i2c, MAX77705_PMIC_REG_PMICREV, &pmic_rev) < 0) {
dev_err(max77705->dev, "device not found on this channel (this is not an error)\n");
ret = -ENODEV;
goto err_w_lock;
}
pr_info("%s:%s pmic_id:%x, pmic_rev:%x\n",
MFD_DEV_NAME, __func__, pmic_id, pmic_rev);
max77705->pmic_rev = max77705_revision_check(pmic_id, pmic_rev & 0x7);
if (max77705->pmic_rev == 0) {
dev_err(max77705->dev, "Can not find matched revision\n");
ret = -ENODEV;
goto err_w_lock;
}
max77705->pmic_ver = ((pmic_rev & 0xF8) >> 0x3);
/* print rev */
pr_info("%s:%s device found: rev:%x ver:%x\n",
MFD_DEV_NAME, __func__, max77705->pmic_rev, max77705->pmic_ver);
/* No active discharge on safeout ldo 1,2 */
/* max77705_update_reg(i2c, MAX77705_PMIC_REG_SAFEOUT_CTRL, 0x00, 0x30); */
#ifdef CONFIG_CCIC_MAX77705
init_completion(&max77705->fw_completion);
max77705->fw_workqueue = create_singlethread_workqueue("fw_update");
if (max77705->fw_workqueue == NULL)
return -ENOMEM;
INIT_WORK(&max77705->fw_work, max77705_usbc_wait_response_q);
#endif
max77705->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
i2c_set_clientdata(max77705->muic, max77705);
max77705->charger = i2c_new_dummy(i2c->adapter, I2C_ADDR_CHG);
i2c_set_clientdata(max77705->charger, max77705);
max77705->fuelgauge = i2c_new_dummy(i2c->adapter, I2C_ADDR_FG);
i2c_set_clientdata(max77705->fuelgauge, max77705);
#ifdef CONFIG_CCIC_MAX77705
max77705_usbc_fw_setting(max77705, 0);
#endif
max77705->debug = i2c_new_dummy(i2c->adapter, I2C_ADDR_DEBUG);
i2c_set_clientdata(max77705->debug, max77705);
disable_irq(max77705->irq);
ret = max77705_irq_init(max77705);
if (ret < 0)
goto err_irq_init;
ret = mfd_add_devices(max77705->dev, -1, max77705_devs,
ARRAY_SIZE(max77705_devs), NULL, 0, NULL);
if (ret < 0)
goto err_mfd;
device_init_wakeup(max77705->dev, pdata->wakeup);
return ret;
err_mfd:
mfd_remove_devices(max77705->dev);
err_irq_init:
i2c_unregister_device(max77705->muic);
i2c_unregister_device(max77705->charger);
i2c_unregister_device(max77705->fuelgauge);
i2c_unregister_device(max77705->debug);
err_w_lock:
mutex_destroy(&max77705->i2c_lock);
err:
kfree(max77705);
return ret;
}
static int max77705_i2c_remove(struct i2c_client *i2c)
{
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
device_init_wakeup(max77705->dev, 0);
max77705_irq_exit(max77705);
mfd_remove_devices(max77705->dev);
i2c_unregister_device(max77705->muic);
i2c_unregister_device(max77705->charger);
i2c_unregister_device(max77705->fuelgauge);
i2c_unregister_device(max77705->debug);
kfree(max77705);
return 0;
}
static const struct i2c_device_id max77705_i2c_id[] = {
{ MFD_DEV_NAME, TYPE_MAX77705 },
{ }
};
MODULE_DEVICE_TABLE(i2c, max77705_i2c_id);
#if defined(CONFIG_OF)
static const struct of_device_id max77705_i2c_dt_ids[] = {
{ .compatible = "maxim,max77705" },
{ },
};
MODULE_DEVICE_TABLE(of, max77705_i2c_dt_ids);
#endif /* CONFIG_OF */
#if defined(CONFIG_PM)
static int max77705_suspend(struct device *dev)
{
struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
pr_info("%s:%s\n", MFD_DEV_NAME, __func__);
if (device_may_wakeup(dev))
enable_irq_wake(max77705->irq);
disable_irq(max77705->irq);
return 0;
}
static int max77705_resume(struct device *dev)
{
struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
pr_info("%s:%s\n", MFD_DEV_NAME, __func__);
if (device_may_wakeup(dev))
disable_irq_wake(max77705->irq);
enable_irq(max77705->irq);
return 0;
}
#else
#define max77705_suspend NULL
#define max77705_resume NULL
#endif /* CONFIG_PM */
#ifdef CONFIG_HIBERNATION
#if 0
u8 max77705_dumpaddr_pmic[] = {
#if 0
MAX77705_LED_REG_IFLASH,
MAX77705_LED_REG_IFLASH1,
MAX77705_LED_REG_IFLASH2,
MAX77705_LED_REG_ITORCH,
MAX77705_LED_REG_ITORCHTORCHTIMER,
MAX77705_LED_REG_FLASH_TIMER,
MAX77705_LED_REG_FLASH_EN,
MAX77705_LED_REG_MAX_FLASH1,
MAX77705_LED_REG_MAX_FLASH2,
MAX77705_LED_REG_VOUT_CNTL,
MAX77705_LED_REG_VOUT_FLASH,
MAX77705_LED_REG_VOUT_FLASH1,
MAX77705_LED_REG_FLASH_INT_STATUS,
#endif
MAX77705_PMIC_REG_PMICID1,
MAX77705_PMIC_REG_PMICREV,
MAX77705_PMIC_REG_MAINCTRL1,
MAX77705_PMIC_REG_MCONFIG,
};
#endif
u8 max77705_dumpaddr_muic[] = {
MAX77705_MUIC_REG_INTMASK_MAIN,
MAX77705_MUIC_REG_INTMASK_BC,
MAX77705_MUIC_REG_INTMASK_FC,
MAX77705_MUIC_REG_INTMASK_GP,
MAX77705_MUIC_REG_STATUS1_BC,
MAX77705_MUIC_REG_STATUS2_BC,
MAX77705_MUIC_REG_STATUS_GP,
MAX77705_MUIC_REG_CONTROL1_BC,
MAX77705_MUIC_REG_CONTROL2_BC,
MAX77705_MUIC_REG_CONTROL1,
MAX77705_MUIC_REG_CONTROL2,
MAX77705_MUIC_REG_CONTROL3,
MAX77705_MUIC_REG_CONTROL4,
MAX77705_MUIC_REG_HVCONTROL1,
MAX77705_MUIC_REG_HVCONTROL2,
};
#if 0
u8 max77705_dumpaddr_haptic[] = {
MAX77705_HAPTIC_REG_CONFIG1,
MAX77705_HAPTIC_REG_CONFIG2,
MAX77705_HAPTIC_REG_CONFIG_CHNL,
MAX77705_HAPTIC_REG_CONFG_CYC1,
MAX77705_HAPTIC_REG_CONFG_CYC2,
MAX77705_HAPTIC_REG_CONFIG_PER1,
MAX77705_HAPTIC_REG_CONFIG_PER2,
MAX77705_HAPTIC_REG_CONFIG_PER3,
MAX77705_HAPTIC_REG_CONFIG_PER4,
MAX77705_HAPTIC_REG_CONFIG_DUTY1,
MAX77705_HAPTIC_REG_CONFIG_DUTY2,
MAX77705_HAPTIC_REG_CONFIG_PWM1,
MAX77705_HAPTIC_REG_CONFIG_PWM2,
MAX77705_HAPTIC_REG_CONFIG_PWM3,
MAX77705_HAPTIC_REG_CONFIG_PWM4,
};
#endif
u8 max77705_dumpaddr_led[] = {
MAX77705_RGBLED_REG_LEDEN,
MAX77705_RGBLED_REG_LED0BRT,
MAX77705_RGBLED_REG_LED1BRT,
MAX77705_RGBLED_REG_LED2BRT,
MAX77705_RGBLED_REG_LED3BRT,
MAX77705_RGBLED_REG_LEDBLNK,
MAX77705_RGBLED_REG_LEDRMP,
};
static int max77705_freeze(struct device *dev)
{
struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
int i;
for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_pmic); i++)
max77705_read_reg(i2c, max77705_dumpaddr_pmic[i],
&max77705->reg_pmic_dump[i]);
for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_muic); i++)
max77705_read_reg(i2c, max77705_dumpaddr_muic[i],
&max77705->reg_muic_dump[i]);
for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_led); i++)
max77705_read_reg(i2c, max77705_dumpaddr_led[i],
&max77705->reg_led_dump[i]);
disable_irq(max77705->irq);
return 0;
}
static int max77705_restore(struct device *dev)
{
struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
struct max77705_dev *max77705 = i2c_get_clientdata(i2c);
int i;
enable_irq(max77705->irq);
for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_pmic); i++)
max77705_write_reg(i2c, max77705_dumpaddr_pmic[i],
max77705->reg_pmic_dump[i]);
for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_muic); i++)
max77705_write_reg(i2c, max77705_dumpaddr_muic[i],
max77705->reg_muic_dump[i]);
for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_led); i++)
max77705_write_reg(i2c, max77705_dumpaddr_led[i],
max77705->reg_led_dump[i]);
return 0;
}
#endif
const struct dev_pm_ops max77705_pm = {
.suspend = max77705_suspend,
.resume = max77705_resume,
#ifdef CONFIG_HIBERNATION
.freeze = max77705_freeze,
.thaw = max77705_restore,
.restore = max77705_restore,
#endif
};
static struct i2c_driver max77705_i2c_driver = {
.driver = {
.name = MFD_DEV_NAME,
.owner = THIS_MODULE,
#if defined(CONFIG_PM)
.pm = &max77705_pm,
#endif /* CONFIG_PM */
#if defined(CONFIG_OF)
.of_match_table = max77705_i2c_dt_ids,
#endif /* CONFIG_OF */
},
.probe = max77705_i2c_probe,
.remove = max77705_i2c_remove,
.id_table = max77705_i2c_id,
};
static int __init max77705_i2c_init(void)
{
pr_info("%s:%s\n", MFD_DEV_NAME, __func__);
return i2c_add_driver(&max77705_i2c_driver);
}
/* init early so consumer devices can complete system boot */
subsys_initcall(max77705_i2c_init);
static void __exit max77705_i2c_exit(void)
{
i2c_del_driver(&max77705_i2c_driver);
}
module_exit(max77705_i2c_exit);
MODULE_DESCRIPTION("MAXIM 77705 multi-function core driver");
MODULE_AUTHOR("Insun Choi <insun77.choi@samsung.com>");
MODULE_LICENSE("GPL");
|