2 * Copyright (C) 2012-2015 Samsung Electronics
7 * SPDX-License-Identifier: GPL-2.0+
15 #include <power/pmic.h>
16 #include <power/regulator.h>
17 #include <power/max77686_pmic.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #define MODE(_id, _val, _name) { \
23 .register_value = _val, \
27 /* LDO: 1,3,4,5,9,17,18,19,20,21,22,23,24,26,26,27 */
28 static struct dm_regulator_mode max77686_ldo_mode_standby1[] = {
29 MODE(OPMODE_OFF, MAX77686_LDO_MODE_OFF, "OFF"),
30 MODE(OPMODE_LPM, MAX77686_LDO_MODE_LPM, "LPM"),
31 MODE(OPMODE_STANDBY_LPM, MAX77686_LDO_MODE_STANDBY_LPM, "ON/LPM"),
32 MODE(OPMODE_ON, MAX77686_LDO_MODE_ON, "ON"),
35 /* LDO: 2,6,7,8,10,11,12,14,15,16 */
36 static struct dm_regulator_mode max77686_ldo_mode_standby2[] = {
37 MODE(OPMODE_OFF, MAX77686_LDO_MODE_OFF, "OFF"),
38 MODE(OPMODE_STANDBY, MAX77686_LDO_MODE_STANDBY, "ON/OFF"),
39 MODE(OPMODE_STANDBY_LPM, MAX77686_LDO_MODE_STANDBY_LPM, "ON/LPM"),
40 MODE(OPMODE_ON, MAX77686_LDO_MODE_ON, "ON"),
44 static struct dm_regulator_mode max77686_buck_mode_standby[] = {
45 MODE(OPMODE_OFF, MAX77686_BUCK_MODE_OFF, "OFF"),
46 MODE(OPMODE_STANDBY, MAX77686_BUCK_MODE_STANDBY, "ON/OFF"),
47 MODE(OPMODE_ON, MAX77686_BUCK_MODE_ON, "ON"),
51 static struct dm_regulator_mode max77686_buck_mode_lpm[] = {
52 MODE(OPMODE_OFF, MAX77686_BUCK_MODE_OFF, "OFF"),
53 MODE(OPMODE_STANDBY, MAX77686_BUCK_MODE_STANDBY, "ON/OFF"),
54 MODE(OPMODE_LPM, MAX77686_BUCK_MODE_LPM, "LPM"),
55 MODE(OPMODE_ON, MAX77686_BUCK_MODE_ON, "ON"),
59 static struct dm_regulator_mode max77686_buck_mode_onoff[] = {
60 MODE(OPMODE_OFF, MAX77686_BUCK_MODE_OFF, "OFF"),
61 MODE(OPMODE_ON, MAX77686_BUCK_MODE_ON, "ON"),
64 static const char max77686_buck_addr[] = {
65 0xff, 0x10, 0x12, 0x1c, 0x26, 0x30, 0x32, 0x34, 0x36, 0x38
68 static int max77686_buck_volt2hex(int buck, int uV)
71 unsigned int hex_max = 0;
77 /* hex = (uV - 600000) / 12500; */
78 hex = (uV - MAX77686_BUCK_UV_LMIN) / MAX77686_BUCK_UV_LSTEP;
79 hex_max = MAX77686_BUCK234_VOLT_MAX_HEX;
81 * Those use voltage scaller - temporary not implemented
86 /* hex = (uV - 750000) / 50000; */
87 hex = (uV - MAX77686_BUCK_UV_HMIN) / MAX77686_BUCK_UV_HSTEP;
88 hex_max = MAX77686_BUCK_VOLT_MAX_HEX;
92 if (hex >= 0 && hex <= hex_max)
95 error("Value: %d uV is wrong for BUCK%d", uV, buck);
99 static int max77686_buck_hex2volt(int buck, int hex)
102 unsigned int hex_max = 0;
111 hex_max = MAX77686_BUCK234_VOLT_MAX_HEX;
115 /* uV = hex * 12500 + 600000; */
116 uV = hex * MAX77686_BUCK_UV_LSTEP + MAX77686_BUCK_UV_LMIN;
119 hex_max = MAX77686_BUCK_VOLT_MAX_HEX;
123 /* uV = hex * 50000 + 750000; */
124 uV = hex * MAX77686_BUCK_UV_HSTEP + MAX77686_BUCK_UV_HMIN;
131 error("Value: %#x is wrong for BUCK%d", hex, buck);
135 static int max77686_ldo_volt2hex(int ldo, int uV)
137 unsigned int hex = 0;
146 hex = (uV - MAX77686_LDO_UV_MIN) / MAX77686_LDO_UV_LSTEP;
147 /* hex = (uV - 800000) / 25000; */
150 hex = (uV - MAX77686_LDO_UV_MIN) / MAX77686_LDO_UV_HSTEP;
151 /* hex = (uV - 800000) / 50000; */
154 if (hex >= 0 && hex <= MAX77686_LDO_VOLT_MAX_HEX)
157 error("Value: %d uV is wrong for LDO%d", uV, ldo);
161 static int max77686_ldo_hex2volt(int ldo, int hex)
165 if (hex > MAX77686_LDO_VOLT_MAX_HEX)
175 /* uV = hex * 25000 + 800000; */
176 uV = hex * MAX77686_LDO_UV_LSTEP + MAX77686_LDO_UV_MIN;
179 /* uV = hex * 50000 + 800000; */
180 uV = hex * MAX77686_LDO_UV_HSTEP + MAX77686_LDO_UV_MIN;
186 error("Value: %#x is wrong for ldo%d", hex, ldo);
190 static int max77686_ldo_hex2mode(int ldo, int hex)
192 if (hex > MAX77686_LDO_MODE_MASK)
196 case MAX77686_LDO_MODE_OFF:
198 case MAX77686_LDO_MODE_LPM: /* == MAX77686_LDO_MODE_STANDBY: */
199 /* The same mode values but different meaning for each ldo */
211 return OPMODE_STANDBY;
215 case MAX77686_LDO_MODE_STANDBY_LPM:
216 return OPMODE_STANDBY_LPM;
217 case MAX77686_LDO_MODE_ON:
224 static int max77686_buck_hex2mode(int buck, int hex)
226 if (hex > MAX77686_BUCK_MODE_MASK)
230 case MAX77686_BUCK_MODE_OFF:
232 case MAX77686_BUCK_MODE_ON:
234 case MAX77686_BUCK_MODE_STANDBY:
240 return OPMODE_STANDBY;
244 case MAX77686_BUCK_MODE_LPM:
258 static int max77686_buck_modes(int buck, struct dm_regulator_mode **modesp)
262 if (buck < 1 || buck > MAX77686_BUCK_NUM)
267 *modesp = max77686_buck_mode_standby;
268 ret = ARRAY_SIZE(max77686_buck_mode_standby);
273 *modesp = max77686_buck_mode_lpm;
274 ret = ARRAY_SIZE(max77686_buck_mode_lpm);
277 *modesp = max77686_buck_mode_onoff;
278 ret = ARRAY_SIZE(max77686_buck_mode_onoff);
284 static int max77686_ldo_modes(int ldo, struct dm_regulator_mode **modesp,
289 if (ldo < 1 || ldo > MAX77686_LDO_NUM)
303 *modesp = max77686_ldo_mode_standby2;
304 ret = ARRAY_SIZE(max77686_ldo_mode_standby2);
307 *modesp = max77686_ldo_mode_standby1;
308 ret = ARRAY_SIZE(max77686_ldo_mode_standby1);
314 static int max77686_ldo_val(struct udevice *dev, int op, int *uV)
316 unsigned int ret, hex, adr;
320 if (op == PMIC_OP_GET)
323 ldo = dev->driver_data;
324 if (ldo < 1 || ldo > MAX77686_LDO_NUM) {
325 error("Wrong ldo number: %d", ldo);
329 adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
331 ret = pmic_read(dev->parent, adr, &val, 1);
335 if (op == PMIC_OP_GET) {
336 val &= MAX77686_LDO_VOLT_MASK;
337 ret = max77686_ldo_hex2volt(ldo, val);
344 hex = max77686_ldo_volt2hex(ldo, *uV);
348 val &= ~MAX77686_LDO_VOLT_MASK;
350 ret = pmic_write(dev->parent, adr, &val, 1);
355 static int max77686_buck_val(struct udevice *dev, int op, int *uV)
357 unsigned int hex, ret, mask, adr;
361 buck = dev->driver_data;
362 if (buck < 1 || buck > MAX77686_BUCK_NUM) {
363 error("Wrong buck number: %d", buck);
367 if (op == PMIC_OP_GET)
370 /* &buck_out = ctrl + 1 */
371 adr = max77686_buck_addr[buck] + 1;
378 /* Those use voltage scallers - will support in the future */
379 mask = MAX77686_BUCK234_VOLT_MASK;
382 mask = MAX77686_BUCK_VOLT_MASK;
385 ret = pmic_read(dev->parent, adr, &val, 1);
389 if (op == PMIC_OP_GET) {
391 ret = max77686_buck_hex2volt(buck, val);
398 hex = max77686_buck_volt2hex(buck, *uV);
404 ret = pmic_write(dev->parent, adr, &val, 1);
409 static int max77686_ldo_mode(struct udevice *dev, int op, int *opmode)
411 unsigned int ret, adr, mode;
415 if (op == PMIC_OP_GET)
418 ldo = dev->driver_data;
419 if (ldo < 1 || ldo > MAX77686_LDO_NUM) {
420 error("Wrong ldo number: %d", ldo);
424 adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
426 ret = pmic_read(dev->parent, adr, &val, 1);
430 if (op == PMIC_OP_GET) {
431 val &= MAX77686_LDO_MODE_MASK;
432 ret = max77686_ldo_hex2mode(ldo, val);
442 mode = MAX77686_LDO_MODE_OFF;
458 mode = MAX77686_LDO_MODE_LPM;
473 mode = MAX77686_LDO_MODE_STANDBY;
479 case OPMODE_STANDBY_LPM:
480 mode = MAX77686_LDO_MODE_STANDBY_LPM;
483 mode = MAX77686_LDO_MODE_ON;
490 error("Wrong mode: %d for ldo%d", *opmode, ldo);
494 val &= ~MAX77686_LDO_MODE_MASK;
496 ret = pmic_write(dev->parent, adr, &val, 1);
501 static int max77686_ldo_enable(struct udevice *dev, int op, bool *enable)
505 if (op == PMIC_OP_GET) {
506 ret = max77686_ldo_mode(dev, op, &on_off);
520 } else if (op == PMIC_OP_SET) {
532 ret = max77686_ldo_mode(dev, op, &on_off);
540 static int max77686_buck_mode(struct udevice *dev, int op, int *opmode)
542 unsigned int ret, mask, adr, mode, mode_shift;
546 buck = dev->driver_data;
547 if (buck < 1 || buck > MAX77686_BUCK_NUM) {
548 error("Wrong buck number: %d", buck);
552 adr = max77686_buck_addr[buck];
559 mode_shift = MAX77686_BUCK_MODE_SHIFT_2;
562 mode_shift = MAX77686_BUCK_MODE_SHIFT_1;
565 mask = MAX77686_BUCK_MODE_MASK << mode_shift;
567 ret = pmic_read(dev->parent, adr, &val, 1);
571 if (op == PMIC_OP_GET) {
574 ret = max77686_buck_hex2mode(buck, val);
584 mode = MAX77686_BUCK_MODE_OFF;
592 mode = MAX77686_BUCK_MODE_STANDBY << mode_shift;
603 mode = MAX77686_BUCK_MODE_LPM << mode_shift;
610 mode = MAX77686_BUCK_MODE_ON << mode_shift;
617 error("Wrong mode: %d for buck: %d\n", *opmode, buck);
623 ret = pmic_write(dev->parent, adr, &val, 1);
628 static int max77686_buck_enable(struct udevice *dev, int op, bool *enable)
632 if (op == PMIC_OP_GET) {
633 ret = max77686_buck_mode(dev, op, &on_off);
647 } else if (op == PMIC_OP_SET) {
659 ret = max77686_buck_mode(dev, op, &on_off);
667 static int max77686_ldo_probe(struct udevice *dev)
669 struct dm_regulator_uclass_platdata *uc_pdata;
671 uc_pdata = dev_get_uclass_platdata(dev);
673 uc_pdata->type = REGULATOR_TYPE_LDO;
674 uc_pdata->mode_count = max77686_ldo_modes(dev->driver_data,
675 &uc_pdata->mode, dev);
680 static int ldo_get_value(struct udevice *dev)
685 ret = max77686_ldo_val(dev, PMIC_OP_GET, &uV);
692 static int ldo_set_value(struct udevice *dev, int uV)
694 return max77686_ldo_val(dev, PMIC_OP_SET, &uV);
697 static bool ldo_get_enable(struct udevice *dev)
702 ret = max77686_ldo_enable(dev, PMIC_OP_GET, &enable);
709 static int ldo_set_enable(struct udevice *dev, bool enable)
711 return max77686_ldo_enable(dev, PMIC_OP_SET, &enable);
714 static int ldo_get_mode(struct udevice *dev)
719 ret = max77686_ldo_mode(dev, PMIC_OP_GET, &mode);
726 static int ldo_set_mode(struct udevice *dev, int mode)
728 return max77686_ldo_mode(dev, PMIC_OP_SET, &mode);
731 static int max77686_buck_probe(struct udevice *dev)
733 struct dm_regulator_uclass_platdata *uc_pdata;
735 uc_pdata = dev_get_uclass_platdata(dev);
737 uc_pdata->type = REGULATOR_TYPE_BUCK;
738 uc_pdata->mode_count = max77686_buck_modes(dev->driver_data,
744 static int buck_get_value(struct udevice *dev)
749 ret = max77686_buck_val(dev, PMIC_OP_GET, &uV);
756 static int buck_set_value(struct udevice *dev, int uV)
758 return max77686_buck_val(dev, PMIC_OP_SET, &uV);
761 static bool buck_get_enable(struct udevice *dev)
766 ret = max77686_buck_enable(dev, PMIC_OP_GET, &enable);
773 static int buck_set_enable(struct udevice *dev, bool enable)
775 return max77686_buck_enable(dev, PMIC_OP_SET, &enable);
778 static int buck_get_mode(struct udevice *dev)
783 ret = max77686_buck_mode(dev, PMIC_OP_GET, &mode);
790 static int buck_set_mode(struct udevice *dev, int mode)
792 return max77686_buck_mode(dev, PMIC_OP_SET, &mode);
795 static const struct dm_regulator_ops max77686_ldo_ops = {
796 .get_value = ldo_get_value,
797 .set_value = ldo_set_value,
798 .get_enable = ldo_get_enable,
799 .set_enable = ldo_set_enable,
800 .get_mode = ldo_get_mode,
801 .set_mode = ldo_set_mode,
804 U_BOOT_DRIVER(max77686_ldo) = {
805 .name = MAX77686_LDO_DRIVER,
806 .id = UCLASS_REGULATOR,
807 .ops = &max77686_ldo_ops,
808 .probe = max77686_ldo_probe,
811 static const struct dm_regulator_ops max77686_buck_ops = {
812 .get_value = buck_get_value,
813 .set_value = buck_set_value,
814 .get_enable = buck_get_enable,
815 .set_enable = buck_set_enable,
816 .get_mode = buck_get_mode,
817 .set_mode = buck_set_mode,
820 U_BOOT_DRIVER(max77686_buck) = {
821 .name = MAX77686_BUCK_DRIVER,
822 .id = UCLASS_REGULATOR,
823 .ops = &max77686_buck_ops,
824 .probe = max77686_buck_probe,