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