Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 |
| 4 | * Texas Instruments Incorporated, <www.ti.com> |
| 5 | * |
| 6 | * Keerthy <j-keerthy@ti.com> |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 9 | #include <fdtdec.h> |
| 10 | #include <errno.h> |
| 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 13 | #include <power/pmic.h> |
| 14 | #include <power/regulator.h> |
| 15 | #include <power/lp87565.h> |
| 16 | |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 17 | static const char lp87565_buck_ctrl1[LP87565_BUCK_NUM] = {0x2, 0x4, 0x6, 0x8, 0x2, 0x6}; |
| 18 | static const char lp87565_buck_vout[LP87565_BUCK_NUM] = {0xA, 0xC, 0xE, 0x10, 0xA, 0xE }; |
| 19 | |
| 20 | static int lp87565_buck_enable(struct udevice *dev, int op, bool *enable) |
| 21 | { |
| 22 | int ret; |
| 23 | unsigned int adr; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 24 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 25 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 26 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 27 | adr = uc_pdata->ctrl_reg; |
| 28 | |
| 29 | ret = pmic_reg_read(dev->parent, adr); |
| 30 | if (ret < 0) |
| 31 | return ret; |
| 32 | |
| 33 | if (op == PMIC_OP_GET) { |
| 34 | ret &= LP87565_BUCK_MODE_MASK; |
| 35 | |
| 36 | if (ret) |
| 37 | *enable = true; |
| 38 | else |
| 39 | *enable = false; |
| 40 | |
| 41 | return 0; |
| 42 | } else if (op == PMIC_OP_SET) { |
| 43 | if (*enable) |
| 44 | ret |= LP87565_BUCK_MODE_MASK; |
| 45 | else |
| 46 | ret &= ~LP87565_BUCK_MODE_MASK; |
| 47 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 48 | if (ret) |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int lp87565_buck_volt2val(int uV) |
| 56 | { |
| 57 | if (uV > LP87565_BUCK_VOLT_MAX) |
| 58 | return -EINVAL; |
| 59 | else if (uV > 1400000) |
| 60 | return (uV - 1420000) / 20000 + 0x9E; |
| 61 | else if (uV > 730000) |
| 62 | return (uV - 735000) / 5000 + 0x18; |
| 63 | else if (uV >= 500000) |
| 64 | return (uV - 500000) / 10000; |
| 65 | else |
| 66 | return -EINVAL; |
| 67 | } |
| 68 | |
| 69 | static int lp87565_buck_val2volt(int val) |
| 70 | { |
| 71 | if (val > LP87565_BUCK_VOLT_MAX_HEX) |
| 72 | return -EINVAL; |
| 73 | else if (val > 0x9D) |
| 74 | return 1400000 + (val - 0x9D) * 20000; |
| 75 | else if (val > 0x17) |
| 76 | return 730000 + (val - 0x17) * 5000; |
| 77 | else if (val >= 0x0) |
| 78 | return 500000 + val * 10000; |
| 79 | else |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
| 83 | static int lp87565_buck_val(struct udevice *dev, int op, int *uV) |
| 84 | { |
| 85 | unsigned int hex, adr; |
| 86 | int ret; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 87 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 88 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 89 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 90 | |
| 91 | if (op == PMIC_OP_GET) |
| 92 | *uV = 0; |
| 93 | |
| 94 | adr = uc_pdata->volt_reg; |
| 95 | |
| 96 | ret = pmic_reg_read(dev->parent, adr); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | |
| 100 | if (op == PMIC_OP_GET) { |
| 101 | ret &= LP87565_BUCK_VOLT_MASK; |
| 102 | ret = lp87565_buck_val2volt(ret); |
| 103 | if (ret < 0) |
| 104 | return ret; |
| 105 | *uV = ret; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | hex = lp87565_buck_volt2val(*uV); |
| 111 | if (hex < 0) |
| 112 | return hex; |
| 113 | |
| 114 | ret &= 0x0; |
| 115 | ret = hex; |
| 116 | |
| 117 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static int lp87565_buck_probe(struct udevice *dev) |
| 123 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 124 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 125 | int idx; |
| 126 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 127 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 128 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 129 | |
| 130 | idx = dev->driver_data; |
| 131 | if (idx == 0 || idx == 1 || idx == 2 || idx == 3) { |
| 132 | debug("Single phase regulator\n"); |
| 133 | } else if (idx == 23) { |
| 134 | idx = 5; |
| 135 | } else if (idx == 10) { |
| 136 | idx = 4; |
| 137 | } else { |
| 138 | printf("Wrong ID for regulator\n"); |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | |
| 142 | uc_pdata->ctrl_reg = lp87565_buck_ctrl1[idx]; |
| 143 | uc_pdata->volt_reg = lp87565_buck_vout[idx]; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int buck_get_value(struct udevice *dev) |
| 149 | { |
| 150 | int uV; |
| 151 | int ret; |
| 152 | |
| 153 | ret = lp87565_buck_val(dev, PMIC_OP_GET, &uV); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | return uV; |
| 158 | } |
| 159 | |
| 160 | static int buck_set_value(struct udevice *dev, int uV) |
| 161 | { |
| 162 | return lp87565_buck_val(dev, PMIC_OP_SET, &uV); |
| 163 | } |
| 164 | |
Keerthy | 968771a | 2017-06-13 09:53:56 +0530 | [diff] [blame] | 165 | static int buck_get_enable(struct udevice *dev) |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 166 | { |
| 167 | bool enable = false; |
| 168 | int ret; |
| 169 | |
Keerthy | 2283166 | 2017-06-07 19:08:29 +0530 | [diff] [blame] | 170 | ret = lp87565_buck_enable(dev, PMIC_OP_GET, &enable); |
| 171 | if (ret) |
| 172 | return ret; |
| 173 | |
| 174 | return enable; |
| 175 | } |
| 176 | |
| 177 | static int buck_set_enable(struct udevice *dev, bool enable) |
| 178 | { |
| 179 | return lp87565_buck_enable(dev, PMIC_OP_SET, &enable); |
| 180 | } |
| 181 | |
| 182 | static const struct dm_regulator_ops lp87565_buck_ops = { |
| 183 | .get_value = buck_get_value, |
| 184 | .set_value = buck_set_value, |
| 185 | .get_enable = buck_get_enable, |
| 186 | .set_enable = buck_set_enable, |
| 187 | }; |
| 188 | |
| 189 | U_BOOT_DRIVER(lp87565_buck) = { |
| 190 | .name = LP87565_BUCK_DRIVER, |
| 191 | .id = UCLASS_REGULATOR, |
| 192 | .ops = &lp87565_buck_ops, |
| 193 | .probe = lp87565_buck_probe, |
| 194 | }; |