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 #define MODE(_id, _val, _name) { \
21 .register_value = _val, \
25 /* LDO: 1,3,4,5,9,17,18,19,20,21,22,23,24,26,26,27 */
26 static struct dm_regulator_mode max77686_ldo_mode_standby1[] = {
27 MODE(OPMODE_OFF, MAX77686_LDO_MODE_OFF, "OFF"),
28 MODE(OPMODE_LPM, MAX77686_LDO_MODE_LPM, "LPM"),
29 MODE(OPMODE_STANDBY_LPM, MAX77686_LDO_MODE_STANDBY_LPM, "ON/LPM"),
30 MODE(OPMODE_ON, MAX77686_LDO_MODE_ON, "ON"),
33 /* LDO: 2,6,7,8,10,11,12,14,15,16 */
34 static struct dm_regulator_mode max77686_ldo_mode_standby2[] = {
35 MODE(OPMODE_OFF, MAX77686_LDO_MODE_OFF, "OFF"),
36 MODE(OPMODE_STANDBY, MAX77686_LDO_MODE_STANDBY, "ON/OFF"),
37 MODE(OPMODE_STANDBY_LPM, MAX77686_LDO_MODE_STANDBY_LPM, "ON/LPM"),
38 MODE(OPMODE_ON, MAX77686_LDO_MODE_ON, "ON"),
42 static struct dm_regulator_mode max77686_buck_mode_standby[] = {
43 MODE(OPMODE_OFF, MAX77686_BUCK_MODE_OFF, "OFF"),
44 MODE(OPMODE_STANDBY, MAX77686_BUCK_MODE_STANDBY, "ON/OFF"),
45 MODE(OPMODE_ON, MAX77686_BUCK_MODE_ON, "ON"),
49 static struct dm_regulator_mode max77686_buck_mode_lpm[] = {
50 MODE(OPMODE_OFF, MAX77686_BUCK_MODE_OFF, "OFF"),
51 MODE(OPMODE_STANDBY, MAX77686_BUCK_MODE_STANDBY, "ON/OFF"),
52 MODE(OPMODE_LPM, MAX77686_BUCK_MODE_LPM, "LPM"),
53 MODE(OPMODE_ON, MAX77686_BUCK_MODE_ON, "ON"),
57 static struct dm_regulator_mode max77686_buck_mode_onoff[] = {
58 MODE(OPMODE_OFF, MAX77686_BUCK_MODE_OFF, "OFF"),
59 MODE(OPMODE_ON, MAX77686_BUCK_MODE_ON, "ON"),
62 static const char max77686_buck_ctrl[] = {
63 0xff, 0x10, 0x12, 0x1c, 0x26, 0x30, 0x32, 0x34, 0x36, 0x38
66 static const char max77686_buck_out[] = {
67 0xff, 0x11, 0x14, 0x1e, 0x28, 0x31, 0x33, 0x35, 0x37, 0x39
70 static int max77686_buck_volt2hex(int buck, int uV)
79 /* hex = (uV - 600000) / 12500; */
80 hex = (uV - MAX77686_BUCK_UV_LMIN) / MAX77686_BUCK_UV_LSTEP;
81 hex_max = MAX77686_BUCK234_VOLT_MAX_HEX;
85 * hex = (uV - 750000) / 50000. We assume that dynamic voltage
86 * scaling via GPIOs is not enabled and don't support that.
87 * If this is enabled then the driver will need to take that
88 * into account and check different registers depending on
89 * the current setting. See the datasheet for details.
91 hex = (uV - MAX77686_BUCK_UV_HMIN) / MAX77686_BUCK_UV_HSTEP;
92 hex_max = MAX77686_BUCK_VOLT_MAX_HEX;
96 if (hex >= 0 && hex <= hex_max)
99 pr_err("Value: %d uV is wrong for BUCK%d", uV, buck);
103 static int max77686_buck_hex2volt(int buck, int hex)
115 hex_max = MAX77686_BUCK234_VOLT_MAX_HEX;
119 /* uV = hex * 12500 + 600000; */
120 uV = hex * MAX77686_BUCK_UV_LSTEP + MAX77686_BUCK_UV_LMIN;
123 hex_max = MAX77686_BUCK_VOLT_MAX_HEX;
127 /* uV = hex * 50000 + 750000; */
128 uV = hex * MAX77686_BUCK_UV_HSTEP + MAX77686_BUCK_UV_HMIN;
135 pr_err("Value: %#x is wrong for BUCK%d", hex, buck);
139 static int max77686_ldo_volt2hex(int ldo, int uV)
150 hex = (uV - MAX77686_LDO_UV_MIN) / MAX77686_LDO_UV_LSTEP;
151 /* hex = (uV - 800000) / 25000; */
154 hex = (uV - MAX77686_LDO_UV_MIN) / MAX77686_LDO_UV_HSTEP;
155 /* hex = (uV - 800000) / 50000; */
158 if (hex >= 0 && hex <= MAX77686_LDO_VOLT_MAX_HEX)
161 pr_err("Value: %d uV is wrong for LDO%d", uV, ldo);
165 static int max77686_ldo_hex2volt(int ldo, int hex)
169 if (hex > MAX77686_LDO_VOLT_MAX_HEX)
179 /* uV = hex * 25000 + 800000; */
180 uV = hex * MAX77686_LDO_UV_LSTEP + MAX77686_LDO_UV_MIN;
183 /* uV = hex * 50000 + 800000; */
184 uV = hex * MAX77686_LDO_UV_HSTEP + MAX77686_LDO_UV_MIN;
190 pr_err("Value: %#x is wrong for ldo%d", hex, ldo);
194 static int max77686_ldo_hex2mode(int ldo, int hex)
196 if (hex > MAX77686_LDO_MODE_MASK)
200 case MAX77686_LDO_MODE_OFF:
202 case MAX77686_LDO_MODE_LPM: /* == MAX77686_LDO_MODE_STANDBY: */
203 /* The same mode values but different meaning for each ldo */
215 return OPMODE_STANDBY;
219 case MAX77686_LDO_MODE_STANDBY_LPM:
220 return OPMODE_STANDBY_LPM;
221 case MAX77686_LDO_MODE_ON:
228 static int max77686_buck_hex2mode(int buck, int hex)
230 if (hex > MAX77686_BUCK_MODE_MASK)
234 case MAX77686_BUCK_MODE_OFF:
236 case MAX77686_BUCK_MODE_ON:
238 case MAX77686_BUCK_MODE_STANDBY:
244 return OPMODE_STANDBY;
248 case MAX77686_BUCK_MODE_LPM:
262 static int max77686_buck_modes(int buck, struct dm_regulator_mode **modesp)
266 if (buck < 1 || buck > MAX77686_BUCK_NUM)
271 *modesp = max77686_buck_mode_standby;
272 ret = ARRAY_SIZE(max77686_buck_mode_standby);
277 *modesp = max77686_buck_mode_lpm;
278 ret = ARRAY_SIZE(max77686_buck_mode_lpm);
281 *modesp = max77686_buck_mode_onoff;
282 ret = ARRAY_SIZE(max77686_buck_mode_onoff);
288 static int max77686_ldo_modes(int ldo, struct dm_regulator_mode **modesp,
293 if (ldo < 1 || ldo > MAX77686_LDO_NUM)
307 *modesp = max77686_ldo_mode_standby2;
308 ret = ARRAY_SIZE(max77686_ldo_mode_standby2);
311 *modesp = max77686_ldo_mode_standby1;
312 ret = ARRAY_SIZE(max77686_ldo_mode_standby1);
318 static int max77686_ldo_val(struct udevice *dev, int op, int *uV)
324 if (op == PMIC_OP_GET)
327 ldo = dev->driver_data;
328 if (ldo < 1 || ldo > MAX77686_LDO_NUM) {
329 pr_err("Wrong ldo number: %d", ldo);
333 adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
335 ret = pmic_read(dev->parent, adr, &val, 1);
339 if (op == PMIC_OP_GET) {
340 val &= MAX77686_LDO_VOLT_MASK;
341 ret = max77686_ldo_hex2volt(ldo, val);
348 hex = max77686_ldo_volt2hex(ldo, *uV);
352 val &= ~MAX77686_LDO_VOLT_MASK;
354 ret = pmic_write(dev->parent, adr, &val, 1);
359 static int max77686_buck_val(struct udevice *dev, int op, int *uV)
361 unsigned int mask, adr;
365 buck = dev->driver_data;
366 if (buck < 1 || buck > MAX77686_BUCK_NUM) {
367 pr_err("Wrong buck number: %d", buck);
371 if (op == PMIC_OP_GET)
374 /* &buck_out = ctrl + 1 */
375 adr = max77686_buck_out[buck];
382 mask = MAX77686_BUCK234_VOLT_MASK;
385 mask = MAX77686_BUCK_VOLT_MASK;
389 ret = pmic_read(dev->parent, adr, &val, 1);
393 if (op == PMIC_OP_GET) {
395 ret = max77686_buck_hex2volt(buck, val);
402 hex = max77686_buck_volt2hex(buck, *uV);
408 ret = pmic_write(dev->parent, adr, &val, 1);
413 static int max77686_ldo_mode(struct udevice *dev, int op, int *opmode)
415 unsigned int adr, mode;
419 if (op == PMIC_OP_GET)
422 ldo = dev->driver_data;
423 if (ldo < 1 || ldo > MAX77686_LDO_NUM) {
424 pr_err("Wrong ldo number: %d", ldo);
428 adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
430 ret = pmic_read(dev->parent, adr, &val, 1);
434 if (op == PMIC_OP_GET) {
435 val &= MAX77686_LDO_MODE_MASK;
436 ret = max77686_ldo_hex2mode(ldo, val);
446 mode = MAX77686_LDO_MODE_OFF;
462 mode = MAX77686_LDO_MODE_LPM;
477 mode = MAX77686_LDO_MODE_STANDBY;
483 case OPMODE_STANDBY_LPM:
484 mode = MAX77686_LDO_MODE_STANDBY_LPM;
487 mode = MAX77686_LDO_MODE_ON;
494 pr_err("Wrong mode: %d for ldo%d", *opmode, ldo);
498 val &= ~MAX77686_LDO_MODE_MASK;
500 ret = pmic_write(dev->parent, adr, &val, 1);
505 static int max77686_ldo_enable(struct udevice *dev, int op, bool *enable)
509 if (op == PMIC_OP_GET) {
510 ret = max77686_ldo_mode(dev, op, &on_off);
524 } else if (op == PMIC_OP_SET) {
530 ret = max77686_ldo_mode(dev, op, &on_off);
538 static int max77686_buck_mode(struct udevice *dev, int op, int *opmode)
540 unsigned int mask, adr, mode, mode_shift;
544 buck = dev->driver_data;
545 if (buck < 1 || buck > MAX77686_BUCK_NUM) {
546 pr_err("Wrong buck number: %d", buck);
550 adr = max77686_buck_ctrl[buck];
557 mode_shift = MAX77686_BUCK_MODE_SHIFT_2;
560 mode_shift = MAX77686_BUCK_MODE_SHIFT_1;
563 mask = MAX77686_BUCK_MODE_MASK << mode_shift;
565 ret = pmic_read(dev->parent, adr, &val, 1);
569 if (op == PMIC_OP_GET) {
572 ret = max77686_buck_hex2mode(buck, val);
582 mode = MAX77686_BUCK_MODE_OFF;
590 mode = MAX77686_BUCK_MODE_STANDBY << mode_shift;
601 mode = MAX77686_BUCK_MODE_LPM << mode_shift;
608 mode = MAX77686_BUCK_MODE_ON << mode_shift;
615 pr_err("Wrong mode: %d for buck: %d\n", *opmode, buck);
621 ret = pmic_write(dev->parent, adr, &val, 1);
626 static int max77686_buck_enable(struct udevice *dev, int op, bool *enable)
630 if (op == PMIC_OP_GET) {
631 ret = max77686_buck_mode(dev, op, &on_off);
645 } else if (op == PMIC_OP_SET) {
651 ret = max77686_buck_mode(dev, op, &on_off);
659 static int max77686_ldo_probe(struct udevice *dev)
661 struct dm_regulator_uclass_platdata *uc_pdata;
663 uc_pdata = dev_get_uclass_platdata(dev);
665 uc_pdata->type = REGULATOR_TYPE_LDO;
666 uc_pdata->mode_count = max77686_ldo_modes(dev->driver_data,
667 &uc_pdata->mode, dev);
672 static int ldo_get_value(struct udevice *dev)
677 ret = max77686_ldo_val(dev, PMIC_OP_GET, &uV);
684 static int ldo_set_value(struct udevice *dev, int uV)
686 return max77686_ldo_val(dev, PMIC_OP_SET, &uV);
689 static int ldo_get_enable(struct udevice *dev)
694 ret = max77686_ldo_enable(dev, PMIC_OP_GET, &enable);
701 static int ldo_set_enable(struct udevice *dev, bool enable)
703 return max77686_ldo_enable(dev, PMIC_OP_SET, &enable);
706 static int ldo_get_mode(struct udevice *dev)
711 ret = max77686_ldo_mode(dev, PMIC_OP_GET, &mode);
718 static int ldo_set_mode(struct udevice *dev, int mode)
720 return max77686_ldo_mode(dev, PMIC_OP_SET, &mode);
723 static int max77686_buck_probe(struct udevice *dev)
725 struct dm_regulator_uclass_platdata *uc_pdata;
727 uc_pdata = dev_get_uclass_platdata(dev);
729 uc_pdata->type = REGULATOR_TYPE_BUCK;
730 uc_pdata->mode_count = max77686_buck_modes(dev->driver_data,
736 static int buck_get_value(struct udevice *dev)
741 ret = max77686_buck_val(dev, PMIC_OP_GET, &uV);
748 static int buck_set_value(struct udevice *dev, int uV)
750 return max77686_buck_val(dev, PMIC_OP_SET, &uV);
753 static int buck_get_enable(struct udevice *dev)
758 ret = max77686_buck_enable(dev, PMIC_OP_GET, &enable);
765 static int buck_set_enable(struct udevice *dev, bool enable)
767 return max77686_buck_enable(dev, PMIC_OP_SET, &enable);
770 static int buck_get_mode(struct udevice *dev)
775 ret = max77686_buck_mode(dev, PMIC_OP_GET, &mode);
782 static int buck_set_mode(struct udevice *dev, int mode)
784 return max77686_buck_mode(dev, PMIC_OP_SET, &mode);
787 static const struct dm_regulator_ops max77686_ldo_ops = {
788 .get_value = ldo_get_value,
789 .set_value = ldo_set_value,
790 .get_enable = ldo_get_enable,
791 .set_enable = ldo_set_enable,
792 .get_mode = ldo_get_mode,
793 .set_mode = ldo_set_mode,
796 U_BOOT_DRIVER(max77686_ldo) = {
797 .name = MAX77686_LDO_DRIVER,
798 .id = UCLASS_REGULATOR,
799 .ops = &max77686_ldo_ops,
800 .probe = max77686_ldo_probe,
803 static const struct dm_regulator_ops max77686_buck_ops = {
804 .get_value = buck_get_value,
805 .set_value = buck_set_value,
806 .get_enable = buck_get_enable,
807 .set_enable = buck_set_enable,
808 .get_mode = buck_get_mode,
809 .set_mode = buck_set_mode,
812 U_BOOT_DRIVER(max77686_buck) = {
813 .name = MAX77686_BUCK_DRIVER,
814 .id = UCLASS_REGULATOR,
815 .ops = &max77686_buck_ops,
816 .probe = max77686_buck_probe,