hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/drivers/regulator/max8998.c
....@@ -33,70 +33,8 @@
3333 unsigned int buck2_idx;
3434 };
3535
36
-struct voltage_map_desc {
37
- int min;
38
- int max;
39
- int step;
40
-};
41
-
42
-/* Voltage maps in uV*/
43
-static const struct voltage_map_desc ldo23_voltage_map_desc = {
44
- .min = 800000, .step = 50000, .max = 1300000,
45
-};
46
-static const struct voltage_map_desc ldo456711_voltage_map_desc = {
47
- .min = 1600000, .step = 100000, .max = 3600000,
48
-};
49
-static const struct voltage_map_desc ldo8_voltage_map_desc = {
50
- .min = 3000000, .step = 100000, .max = 3600000,
51
-};
52
-static const struct voltage_map_desc ldo9_voltage_map_desc = {
53
- .min = 2800000, .step = 100000, .max = 3100000,
54
-};
55
-static const struct voltage_map_desc ldo10_voltage_map_desc = {
56
- .min = 950000, .step = 50000, .max = 1300000,
57
-};
58
-static const struct voltage_map_desc ldo1213_voltage_map_desc = {
59
- .min = 800000, .step = 100000, .max = 3300000,
60
-};
61
-static const struct voltage_map_desc ldo1415_voltage_map_desc = {
62
- .min = 1200000, .step = 100000, .max = 3300000,
63
-};
64
-static const struct voltage_map_desc ldo1617_voltage_map_desc = {
65
- .min = 1600000, .step = 100000, .max = 3600000,
66
-};
67
-static const struct voltage_map_desc buck12_voltage_map_desc = {
68
- .min = 750000, .step = 25000, .max = 1525000,
69
-};
70
-static const struct voltage_map_desc buck3_voltage_map_desc = {
71
- .min = 1600000, .step = 100000, .max = 3600000,
72
-};
73
-static const struct voltage_map_desc buck4_voltage_map_desc = {
74
- .min = 800000, .step = 100000, .max = 2300000,
75
-};
76
-
77
-static const struct voltage_map_desc *ldo_voltage_map[] = {
78
- NULL,
79
- NULL,
80
- &ldo23_voltage_map_desc, /* LDO2 */
81
- &ldo23_voltage_map_desc, /* LDO3 */
82
- &ldo456711_voltage_map_desc, /* LDO4 */
83
- &ldo456711_voltage_map_desc, /* LDO5 */
84
- &ldo456711_voltage_map_desc, /* LDO6 */
85
- &ldo456711_voltage_map_desc, /* LDO7 */
86
- &ldo8_voltage_map_desc, /* LDO8 */
87
- &ldo9_voltage_map_desc, /* LDO9 */
88
- &ldo10_voltage_map_desc, /* LDO10 */
89
- &ldo456711_voltage_map_desc, /* LDO11 */
90
- &ldo1213_voltage_map_desc, /* LDO12 */
91
- &ldo1213_voltage_map_desc, /* LDO13 */
92
- &ldo1415_voltage_map_desc, /* LDO14 */
93
- &ldo1415_voltage_map_desc, /* LDO15 */
94
- &ldo1617_voltage_map_desc, /* LDO16 */
95
- &ldo1617_voltage_map_desc, /* LDO17 */
96
- &buck12_voltage_map_desc, /* BUCK1 */
97
- &buck12_voltage_map_desc, /* BUCK2 */
98
- &buck3_voltage_map_desc, /* BUCK3 */
99
- &buck4_voltage_map_desc, /* BUCK4 */
36
+static const unsigned int charger_current_table[] = {
37
+ 90000, 380000, 475000, 550000, 570000, 600000, 700000, 800000,
10038 };
10139
10240 static int max8998_get_enable_register(struct regulator_dev *rdev,
....@@ -129,6 +67,10 @@
12967 *reg = MAX8998_REG_CHGR2;
13068 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
13169 break;
70
+ case MAX8998_CHARGER:
71
+ *reg = MAX8998_REG_CHGR2;
72
+ *shift = 0;
73
+ break;
13274 default:
13375 return -EINVAL;
13476 }
....@@ -152,6 +94,11 @@
15294 return ret;
15395
15496 return val & (1 << shift);
97
+}
98
+
99
+static int max8998_ldo_is_enabled_inverted(struct regulator_dev *rdev)
100
+{
101
+ return (!max8998_ldo_is_enabled(rdev));
155102 }
156103
157104 static int max8998_ldo_enable(struct regulator_dev *rdev)
....@@ -400,15 +347,12 @@
400347 {
401348 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
402349 struct i2c_client *i2c = max8998->iodev->i2c;
403
- const struct voltage_map_desc *desc;
404350 int buck = rdev_get_id(rdev);
405351 u8 val = 0;
406352 int difference, ret;
407353
408354 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
409355 return -EINVAL;
410
-
411
- desc = ldo_voltage_map[buck];
412356
413357 /* Voltage stabilization */
414358 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
....@@ -420,14 +364,82 @@
420364 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
421365 return 0;
422366
423
- difference = (new_selector - old_selector) * desc->step / 1000;
367
+ difference = (new_selector - old_selector) * rdev->desc->uV_step / 1000;
424368 if (difference > 0)
425369 return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
426370
427371 return 0;
428372 }
429373
430
-static struct regulator_ops max8998_ldo_ops = {
374
+static int max8998_set_current_limit(struct regulator_dev *rdev,
375
+ int min_uA, int max_uA)
376
+{
377
+ struct max8998_data *max8998 = rdev_get_drvdata(rdev);
378
+ struct i2c_client *i2c = max8998->iodev->i2c;
379
+ unsigned int n_currents = rdev->desc->n_current_limits;
380
+ int i, sel = -1;
381
+
382
+ if (n_currents == 0)
383
+ return -EINVAL;
384
+
385
+ if (rdev->desc->curr_table) {
386
+ const unsigned int *curr_table = rdev->desc->curr_table;
387
+ bool ascend = curr_table[n_currents - 1] > curr_table[0];
388
+
389
+ /* search for closest to maximum */
390
+ if (ascend) {
391
+ for (i = n_currents - 1; i >= 0; i--) {
392
+ if (min_uA <= curr_table[i] &&
393
+ curr_table[i] <= max_uA) {
394
+ sel = i;
395
+ break;
396
+ }
397
+ }
398
+ } else {
399
+ for (i = 0; i < n_currents; i++) {
400
+ if (min_uA <= curr_table[i] &&
401
+ curr_table[i] <= max_uA) {
402
+ sel = i;
403
+ break;
404
+ }
405
+ }
406
+ }
407
+ }
408
+
409
+ if (sel < 0)
410
+ return -EINVAL;
411
+
412
+ sel <<= ffs(rdev->desc->csel_mask) - 1;
413
+
414
+ return max8998_update_reg(i2c, rdev->desc->csel_reg,
415
+ sel, rdev->desc->csel_mask);
416
+}
417
+
418
+static int max8998_get_current_limit(struct regulator_dev *rdev)
419
+{
420
+ struct max8998_data *max8998 = rdev_get_drvdata(rdev);
421
+ struct i2c_client *i2c = max8998->iodev->i2c;
422
+ u8 val;
423
+ int ret;
424
+
425
+ ret = max8998_read_reg(i2c, rdev->desc->csel_reg, &val);
426
+ if (ret != 0)
427
+ return ret;
428
+
429
+ val &= rdev->desc->csel_mask;
430
+ val >>= ffs(rdev->desc->csel_mask) - 1;
431
+
432
+ if (rdev->desc->curr_table) {
433
+ if (val >= rdev->desc->n_current_limits)
434
+ return -EINVAL;
435
+
436
+ return rdev->desc->curr_table[val];
437
+ }
438
+
439
+ return -EINVAL;
440
+}
441
+
442
+static const struct regulator_ops max8998_ldo_ops = {
431443 .list_voltage = regulator_list_voltage_linear,
432444 .map_voltage = regulator_map_voltage_linear,
433445 .is_enabled = max8998_ldo_is_enabled,
....@@ -437,7 +449,7 @@
437449 .set_voltage_sel = max8998_set_voltage_ldo_sel,
438450 };
439451
440
-static struct regulator_ops max8998_buck_ops = {
452
+static const struct regulator_ops max8998_buck_ops = {
441453 .list_voltage = regulator_list_voltage_linear,
442454 .map_voltage = regulator_map_voltage_linear,
443455 .is_enabled = max8998_ldo_is_enabled,
....@@ -448,164 +460,83 @@
448460 .set_voltage_time_sel = max8998_set_voltage_buck_time_sel,
449461 };
450462
451
-static struct regulator_ops max8998_others_ops = {
463
+static const struct regulator_ops max8998_charger_ops = {
464
+ .set_current_limit = max8998_set_current_limit,
465
+ .get_current_limit = max8998_get_current_limit,
466
+ .is_enabled = max8998_ldo_is_enabled_inverted,
467
+ /* Swapped as register is inverted */
468
+ .enable = max8998_ldo_disable,
469
+ .disable = max8998_ldo_enable,
470
+};
471
+
472
+static const struct regulator_ops max8998_others_ops = {
452473 .is_enabled = max8998_ldo_is_enabled,
453474 .enable = max8998_ldo_enable,
454475 .disable = max8998_ldo_disable,
455476 };
456477
457
-static struct regulator_desc regulators[] = {
458
- {
459
- .name = "LDO2",
460
- .id = MAX8998_LDO2,
461
- .ops = &max8998_ldo_ops,
462
- .type = REGULATOR_VOLTAGE,
463
- .owner = THIS_MODULE,
464
- }, {
465
- .name = "LDO3",
466
- .id = MAX8998_LDO3,
467
- .ops = &max8998_ldo_ops,
468
- .type = REGULATOR_VOLTAGE,
469
- .owner = THIS_MODULE,
470
- }, {
471
- .name = "LDO4",
472
- .id = MAX8998_LDO4,
473
- .ops = &max8998_ldo_ops,
474
- .type = REGULATOR_VOLTAGE,
475
- .owner = THIS_MODULE,
476
- }, {
477
- .name = "LDO5",
478
- .id = MAX8998_LDO5,
479
- .ops = &max8998_ldo_ops,
480
- .type = REGULATOR_VOLTAGE,
481
- .owner = THIS_MODULE,
482
- }, {
483
- .name = "LDO6",
484
- .id = MAX8998_LDO6,
485
- .ops = &max8998_ldo_ops,
486
- .type = REGULATOR_VOLTAGE,
487
- .owner = THIS_MODULE,
488
- }, {
489
- .name = "LDO7",
490
- .id = MAX8998_LDO7,
491
- .ops = &max8998_ldo_ops,
492
- .type = REGULATOR_VOLTAGE,
493
- .owner = THIS_MODULE,
494
- }, {
495
- .name = "LDO8",
496
- .id = MAX8998_LDO8,
497
- .ops = &max8998_ldo_ops,
498
- .type = REGULATOR_VOLTAGE,
499
- .owner = THIS_MODULE,
500
- }, {
501
- .name = "LDO9",
502
- .id = MAX8998_LDO9,
503
- .ops = &max8998_ldo_ops,
504
- .type = REGULATOR_VOLTAGE,
505
- .owner = THIS_MODULE,
506
- }, {
507
- .name = "LDO10",
508
- .id = MAX8998_LDO10,
509
- .ops = &max8998_ldo_ops,
510
- .type = REGULATOR_VOLTAGE,
511
- .owner = THIS_MODULE,
512
- }, {
513
- .name = "LDO11",
514
- .id = MAX8998_LDO11,
515
- .ops = &max8998_ldo_ops,
516
- .type = REGULATOR_VOLTAGE,
517
- .owner = THIS_MODULE,
518
- }, {
519
- .name = "LDO12",
520
- .id = MAX8998_LDO12,
521
- .ops = &max8998_ldo_ops,
522
- .type = REGULATOR_VOLTAGE,
523
- .owner = THIS_MODULE,
524
- }, {
525
- .name = "LDO13",
526
- .id = MAX8998_LDO13,
527
- .ops = &max8998_ldo_ops,
528
- .type = REGULATOR_VOLTAGE,
529
- .owner = THIS_MODULE,
530
- }, {
531
- .name = "LDO14",
532
- .id = MAX8998_LDO14,
533
- .ops = &max8998_ldo_ops,
534
- .type = REGULATOR_VOLTAGE,
535
- .owner = THIS_MODULE,
536
- }, {
537
- .name = "LDO15",
538
- .id = MAX8998_LDO15,
539
- .ops = &max8998_ldo_ops,
540
- .type = REGULATOR_VOLTAGE,
541
- .owner = THIS_MODULE,
542
- }, {
543
- .name = "LDO16",
544
- .id = MAX8998_LDO16,
545
- .ops = &max8998_ldo_ops,
546
- .type = REGULATOR_VOLTAGE,
547
- .owner = THIS_MODULE,
548
- }, {
549
- .name = "LDO17",
550
- .id = MAX8998_LDO17,
551
- .ops = &max8998_ldo_ops,
552
- .type = REGULATOR_VOLTAGE,
553
- .owner = THIS_MODULE,
554
- }, {
555
- .name = "BUCK1",
556
- .id = MAX8998_BUCK1,
557
- .ops = &max8998_buck_ops,
558
- .type = REGULATOR_VOLTAGE,
559
- .owner = THIS_MODULE,
560
- }, {
561
- .name = "BUCK2",
562
- .id = MAX8998_BUCK2,
563
- .ops = &max8998_buck_ops,
564
- .type = REGULATOR_VOLTAGE,
565
- .owner = THIS_MODULE,
566
- }, {
567
- .name = "BUCK3",
568
- .id = MAX8998_BUCK3,
569
- .ops = &max8998_buck_ops,
570
- .type = REGULATOR_VOLTAGE,
571
- .owner = THIS_MODULE,
572
- }, {
573
- .name = "BUCK4",
574
- .id = MAX8998_BUCK4,
575
- .ops = &max8998_buck_ops,
576
- .type = REGULATOR_VOLTAGE,
577
- .owner = THIS_MODULE,
578
- }, {
579
- .name = "EN32KHz-AP",
580
- .id = MAX8998_EN32KHZ_AP,
581
- .ops = &max8998_others_ops,
582
- .type = REGULATOR_VOLTAGE,
583
- .owner = THIS_MODULE,
584
- }, {
585
- .name = "EN32KHz-CP",
586
- .id = MAX8998_EN32KHZ_CP,
587
- .ops = &max8998_others_ops,
588
- .type = REGULATOR_VOLTAGE,
589
- .owner = THIS_MODULE,
590
- }, {
591
- .name = "ENVICHG",
592
- .id = MAX8998_ENVICHG,
593
- .ops = &max8998_others_ops,
594
- .type = REGULATOR_VOLTAGE,
595
- .owner = THIS_MODULE,
596
- }, {
597
- .name = "ESAFEOUT1",
598
- .id = MAX8998_ESAFEOUT1,
599
- .ops = &max8998_others_ops,
600
- .type = REGULATOR_VOLTAGE,
601
- .owner = THIS_MODULE,
602
- }, {
603
- .name = "ESAFEOUT2",
604
- .id = MAX8998_ESAFEOUT2,
605
- .ops = &max8998_others_ops,
606
- .type = REGULATOR_VOLTAGE,
607
- .owner = THIS_MODULE,
478
+#define MAX8998_LINEAR_REG(_name, _ops, _min, _step, _max) \
479
+ { \
480
+ .name = #_name, \
481
+ .id = MAX8998_##_name, \
482
+ .ops = _ops, \
483
+ .min_uV = (_min), \
484
+ .uV_step = (_step), \
485
+ .n_voltages = ((_max) - (_min)) / (_step) + 1, \
486
+ .type = REGULATOR_VOLTAGE, \
487
+ .owner = THIS_MODULE, \
608488 }
489
+
490
+#define MAX8998_CURRENT_REG(_name, _ops, _table, _reg, _mask) \
491
+ { \
492
+ .name = #_name, \
493
+ .id = MAX8998_##_name, \
494
+ .ops = _ops, \
495
+ .curr_table = _table, \
496
+ .n_current_limits = ARRAY_SIZE(_table), \
497
+ .csel_reg = _reg, \
498
+ .csel_mask = _mask, \
499
+ .type = REGULATOR_CURRENT, \
500
+ .owner = THIS_MODULE, \
501
+ }
502
+
503
+#define MAX8998_OTHERS_REG(_name, _id) \
504
+ { \
505
+ .name = #_name, \
506
+ .id = _id, \
507
+ .ops = &max8998_others_ops, \
508
+ .type = REGULATOR_VOLTAGE, \
509
+ .owner = THIS_MODULE, \
510
+ }
511
+
512
+static const struct regulator_desc regulators[] = {
513
+ MAX8998_LINEAR_REG(LDO2, &max8998_ldo_ops, 800000, 50000, 1300000),
514
+ MAX8998_LINEAR_REG(LDO3, &max8998_ldo_ops, 800000, 50000, 1300000),
515
+ MAX8998_LINEAR_REG(LDO4, &max8998_ldo_ops, 1600000, 100000, 3600000),
516
+ MAX8998_LINEAR_REG(LDO5, &max8998_ldo_ops, 1600000, 100000, 3600000),
517
+ MAX8998_LINEAR_REG(LDO6, &max8998_ldo_ops, 1600000, 100000, 3600000),
518
+ MAX8998_LINEAR_REG(LDO7, &max8998_ldo_ops, 1600000, 100000, 3600000),
519
+ MAX8998_LINEAR_REG(LDO8, &max8998_ldo_ops, 3000000, 100000, 3600000),
520
+ MAX8998_LINEAR_REG(LDO9, &max8998_ldo_ops, 2800000, 100000, 3100000),
521
+ MAX8998_LINEAR_REG(LDO10, &max8998_ldo_ops, 950000, 50000, 1300000),
522
+ MAX8998_LINEAR_REG(LDO11, &max8998_ldo_ops, 1600000, 100000, 3600000),
523
+ MAX8998_LINEAR_REG(LDO12, &max8998_ldo_ops, 800000, 100000, 3300000),
524
+ MAX8998_LINEAR_REG(LDO13, &max8998_ldo_ops, 800000, 100000, 3300000),
525
+ MAX8998_LINEAR_REG(LDO14, &max8998_ldo_ops, 1200000, 100000, 3300000),
526
+ MAX8998_LINEAR_REG(LDO15, &max8998_ldo_ops, 1200000, 100000, 3300000),
527
+ MAX8998_LINEAR_REG(LDO16, &max8998_ldo_ops, 1600000, 100000, 3600000),
528
+ MAX8998_LINEAR_REG(LDO17, &max8998_ldo_ops, 1600000, 100000, 3600000),
529
+ MAX8998_LINEAR_REG(BUCK1, &max8998_buck_ops, 750000, 25000, 1525000),
530
+ MAX8998_LINEAR_REG(BUCK2, &max8998_buck_ops, 750000, 25000, 1525000),
531
+ MAX8998_LINEAR_REG(BUCK3, &max8998_buck_ops, 1600000, 100000, 3600000),
532
+ MAX8998_LINEAR_REG(BUCK4, &max8998_buck_ops, 800000, 100000, 2300000),
533
+ MAX8998_OTHERS_REG(EN32KHz-AP, MAX8998_EN32KHZ_AP),
534
+ MAX8998_OTHERS_REG(EN32KHz-CP, MAX8998_EN32KHZ_CP),
535
+ MAX8998_OTHERS_REG(ENVICHG, MAX8998_ENVICHG),
536
+ MAX8998_OTHERS_REG(ESAFEOUT1, MAX8998_ESAFEOUT1),
537
+ MAX8998_OTHERS_REG(ESAFEOUT2, MAX8998_ESAFEOUT2),
538
+ MAX8998_CURRENT_REG(CHARGER, &max8998_charger_ops,
539
+ charger_current_table, MAX8998_REG_CHGR1, 0x7),
609540 };
610541
611542 static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
....@@ -796,9 +727,11 @@
796727
797728 /* Set predefined values for BUCK1 registers */
798729 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
730
+ int index = MAX8998_BUCK1 - MAX8998_LDO2;
731
+
799732 i = 0;
800
- while (buck12_voltage_map_desc.min +
801
- buck12_voltage_map_desc.step*i
733
+ while (regulators[index].min_uV +
734
+ regulators[index].uV_step * i
802735 < pdata->buck1_voltage[v])
803736 i++;
804737
....@@ -824,9 +757,11 @@
824757
825758 /* Set predefined values for BUCK2 registers */
826759 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
760
+ int index = MAX8998_BUCK2 - MAX8998_LDO2;
761
+
827762 i = 0;
828
- while (buck12_voltage_map_desc.min +
829
- buck12_voltage_map_desc.step*i
763
+ while (regulators[index].min_uV +
764
+ regulators[index].uV_step * i
830765 < pdata->buck2_voltage[v])
831766 i++;
832767
....@@ -839,18 +774,7 @@
839774 }
840775
841776 for (i = 0; i < pdata->num_regulators; i++) {
842
- const struct voltage_map_desc *desc;
843
- int id = pdata->regulators[i].id;
844
- int index = id - MAX8998_LDO2;
845
-
846
- desc = ldo_voltage_map[id];
847
- if (desc && regulators[index].ops != &max8998_others_ops) {
848
- int count = (desc->max - desc->min) / desc->step + 1;
849
-
850
- regulators[index].n_voltages = count;
851
- regulators[index].min_uV = desc->min;
852
- regulators[index].uV_step = desc->step;
853
- }
777
+ int index = pdata->regulators[i].id - MAX8998_LDO2;
854778
855779 config.dev = max8998->dev;
856780 config.of_node = pdata->regulators[i].reg_node;
....@@ -866,7 +790,6 @@
866790 return ret;
867791 }
868792 }
869
-
870793
871794 return 0;
872795 }