diff options
Diffstat (limited to 'drivers/regulator/tps65090-regulator.c')
-rw-r--r-- | drivers/regulator/tps65090-regulator.c | 214 |
1 files changed, 199 insertions, 15 deletions
diff --git a/drivers/regulator/tps65090-regulator.c b/drivers/regulator/tps65090-regulator.c index 2e92ef68574d..2064b3fd45f7 100644 --- a/drivers/regulator/tps65090-regulator.c +++ b/drivers/regulator/tps65090-regulator.c | |||
@@ -17,6 +17,7 @@ | |||
17 | */ | 17 | */ |
18 | 18 | ||
19 | #include <linux/module.h> | 19 | #include <linux/module.h> |
20 | #include <linux/delay.h> | ||
20 | #include <linux/init.h> | 21 | #include <linux/init.h> |
21 | #include <linux/gpio.h> | 22 | #include <linux/gpio.h> |
22 | #include <linux/of_gpio.h> | 23 | #include <linux/of_gpio.h> |
@@ -28,49 +29,216 @@ | |||
28 | #include <linux/regulator/of_regulator.h> | 29 | #include <linux/regulator/of_regulator.h> |
29 | #include <linux/mfd/tps65090.h> | 30 | #include <linux/mfd/tps65090.h> |
30 | 31 | ||
32 | #define MAX_CTRL_READ_TRIES 5 | ||
33 | #define MAX_FET_ENABLE_TRIES 1000 | ||
34 | |||
35 | #define CTRL_EN_BIT 0 /* Regulator enable bit, active high */ | ||
36 | #define CTRL_WT_BIT 2 /* Regulator wait time 0 bit */ | ||
37 | #define CTRL_PG_BIT 4 /* Regulator power good bit, 1=good */ | ||
38 | #define CTRL_TO_BIT 7 /* Regulator timeout bit, 1=wait */ | ||
39 | |||
40 | #define MAX_OVERCURRENT_WAIT 3 /* Overcurrent wait must be <= this */ | ||
41 | |||
42 | /** | ||
43 | * struct tps65090_regulator - Per-regulator data for a tps65090 regulator | ||
44 | * | ||
45 | * @dev: Pointer to our device. | ||
46 | * @desc: The struct regulator_desc for the regulator. | ||
47 | * @rdev: The struct regulator_dev for the regulator. | ||
48 | * @overcurrent_wait_valid: True if overcurrent_wait is valid. | ||
49 | * @overcurrent_wait: For FETs, the value to put in the WTFET bitfield. | ||
50 | */ | ||
51 | |||
31 | struct tps65090_regulator { | 52 | struct tps65090_regulator { |
32 | struct device *dev; | 53 | struct device *dev; |
33 | struct regulator_desc *desc; | 54 | struct regulator_desc *desc; |
34 | struct regulator_dev *rdev; | 55 | struct regulator_dev *rdev; |
56 | bool overcurrent_wait_valid; | ||
57 | int overcurrent_wait; | ||
35 | }; | 58 | }; |
36 | 59 | ||
37 | static struct regulator_ops tps65090_ext_control_ops = { | 60 | static struct regulator_ops tps65090_ext_control_ops = { |
38 | }; | 61 | }; |
39 | 62 | ||
40 | static struct regulator_ops tps65090_reg_contol_ops = { | 63 | /** |
64 | * tps65090_reg_set_overcurrent_wait - Setup overcurrent wait | ||
65 | * | ||
66 | * This will set the overcurrent wait time based on what's in the regulator | ||
67 | * info. | ||
68 | * | ||
69 | * @ri: Overall regulator data | ||
70 | * @rdev: Regulator device | ||
71 | * | ||
72 | * Return: 0 if no error, non-zero if there was an error writing the register. | ||
73 | */ | ||
74 | static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator *ri, | ||
75 | struct regulator_dev *rdev) | ||
76 | { | ||
77 | int ret; | ||
78 | |||
79 | ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, | ||
80 | MAX_OVERCURRENT_WAIT << CTRL_WT_BIT, | ||
81 | ri->overcurrent_wait << CTRL_WT_BIT); | ||
82 | if (ret) { | ||
83 | dev_err(&rdev->dev, "Error updating overcurrent wait %#x\n", | ||
84 | rdev->desc->enable_reg); | ||
85 | } | ||
86 | |||
87 | return ret; | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * tps65090_try_enable_fet - Try to enable a FET | ||
92 | * | ||
93 | * @rdev: Regulator device | ||
94 | * | ||
95 | * Return: 0 if ok, -ENOTRECOVERABLE if the FET power good bit did not get | ||
96 | * set, or some other -ve value if another error occurred (e.g. i2c error) | ||
97 | */ | ||
98 | static int tps65090_try_enable_fet(struct regulator_dev *rdev) | ||
99 | { | ||
100 | unsigned int control; | ||
101 | int ret, i; | ||
102 | |||
103 | ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, | ||
104 | rdev->desc->enable_mask, | ||
105 | rdev->desc->enable_mask); | ||
106 | if (ret < 0) { | ||
107 | dev_err(&rdev->dev, "Error in updating reg %#x\n", | ||
108 | rdev->desc->enable_reg); | ||
109 | return ret; | ||
110 | } | ||
111 | |||
112 | for (i = 0; i < MAX_CTRL_READ_TRIES; i++) { | ||
113 | ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, | ||
114 | &control); | ||
115 | if (ret < 0) | ||
116 | return ret; | ||
117 | |||
118 | if (!(control & BIT(CTRL_TO_BIT))) | ||
119 | break; | ||
120 | |||
121 | usleep_range(1000, 1500); | ||
122 | } | ||
123 | if (!(control & BIT(CTRL_PG_BIT))) | ||
124 | return -ENOTRECOVERABLE; | ||
125 | |||
126 | return 0; | ||
127 | } | ||
128 | |||
129 | /** | ||
130 | * tps65090_fet_enable - Enable a FET, trying a few times if it fails | ||
131 | * | ||
132 | * Some versions of the tps65090 have issues when turning on the FETs. | ||
133 | * This function goes through several steps to ensure the best chance of the | ||
134 | * FET going on. Specifically: | ||
135 | * - We'll make sure that we bump the "overcurrent wait" to the maximum, which | ||
136 | * increases the chances that we'll turn on properly. | ||
137 | * - We'll retry turning the FET on multiple times (turning off in between). | ||
138 | * | ||
139 | * @rdev: Regulator device | ||
140 | * | ||
141 | * Return: 0 if ok, non-zero if it fails. | ||
142 | */ | ||
143 | static int tps65090_fet_enable(struct regulator_dev *rdev) | ||
144 | { | ||
145 | int ret, tries; | ||
146 | |||
147 | /* | ||
148 | * Try enabling multiple times until we succeed since sometimes the | ||
149 | * first try times out. | ||
150 | */ | ||
151 | tries = 0; | ||
152 | while (true) { | ||
153 | ret = tps65090_try_enable_fet(rdev); | ||
154 | if (!ret) | ||
155 | break; | ||
156 | if (ret != -ENOTRECOVERABLE || tries == MAX_FET_ENABLE_TRIES) | ||
157 | goto err; | ||
158 | |||
159 | /* Try turning the FET off (and then on again) */ | ||
160 | ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, | ||
161 | rdev->desc->enable_mask, 0); | ||
162 | if (ret) | ||
163 | goto err; | ||
164 | |||
165 | tries++; | ||
166 | } | ||
167 | |||
168 | if (tries) | ||
169 | dev_warn(&rdev->dev, "reg %#x enable ok after %d tries\n", | ||
170 | rdev->desc->enable_reg, tries); | ||
171 | |||
172 | return 0; | ||
173 | err: | ||
174 | dev_warn(&rdev->dev, "reg %#x enable failed\n", rdev->desc->enable_reg); | ||
175 | WARN_ON(1); | ||
176 | |||
177 | return ret; | ||
178 | } | ||
179 | |||
180 | static struct regulator_ops tps65090_reg_control_ops = { | ||
41 | .enable = regulator_enable_regmap, | 181 | .enable = regulator_enable_regmap, |
42 | .disable = regulator_disable_regmap, | 182 | .disable = regulator_disable_regmap, |
43 | .is_enabled = regulator_is_enabled_regmap, | 183 | .is_enabled = regulator_is_enabled_regmap, |
44 | }; | 184 | }; |
45 | 185 | ||
186 | static struct regulator_ops tps65090_fet_control_ops = { | ||
187 | .enable = tps65090_fet_enable, | ||
188 | .disable = regulator_disable_regmap, | ||
189 | .is_enabled = regulator_is_enabled_regmap, | ||
190 | }; | ||
191 | |||
46 | static struct regulator_ops tps65090_ldo_ops = { | 192 | static struct regulator_ops tps65090_ldo_ops = { |
47 | }; | 193 | }; |
48 | 194 | ||
49 | #define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \ | 195 | #define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _ops) \ |
50 | { \ | 196 | { \ |
51 | .name = "TPS65090_RAILS"#_id, \ | 197 | .name = "TPS65090_RAILS"#_id, \ |
52 | .supply_name = _sname, \ | 198 | .supply_name = _sname, \ |
53 | .id = TPS65090_REGULATOR_##_id, \ | 199 | .id = TPS65090_REGULATOR_##_id, \ |
54 | .ops = &_ops, \ | 200 | .ops = &_ops, \ |
55 | .enable_reg = _en_reg, \ | 201 | .enable_reg = _en_reg, \ |
56 | .enable_mask = BIT(0), \ | 202 | .enable_val = _en_bits, \ |
203 | .enable_mask = _en_bits, \ | ||
57 | .type = REGULATOR_VOLTAGE, \ | 204 | .type = REGULATOR_VOLTAGE, \ |
58 | .owner = THIS_MODULE, \ | 205 | .owner = THIS_MODULE, \ |
59 | } | 206 | } |
60 | 207 | ||
61 | static struct regulator_desc tps65090_regulator_desc[] = { | 208 | static struct regulator_desc tps65090_regulator_desc[] = { |
62 | tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, tps65090_reg_contol_ops), | 209 | tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, BIT(CTRL_EN_BIT), |
63 | tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, tps65090_reg_contol_ops), | 210 | tps65090_reg_control_ops), |
64 | tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, tps65090_reg_contol_ops), | 211 | tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, BIT(CTRL_EN_BIT), |
65 | tps65090_REG_DESC(FET1, "infet1", 0x0F, tps65090_reg_contol_ops), | 212 | tps65090_reg_control_ops), |
66 | tps65090_REG_DESC(FET2, "infet2", 0x10, tps65090_reg_contol_ops), | 213 | tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, BIT(CTRL_EN_BIT), |
67 | tps65090_REG_DESC(FET3, "infet3", 0x11, tps65090_reg_contol_ops), | 214 | tps65090_reg_control_ops), |
68 | tps65090_REG_DESC(FET4, "infet4", 0x12, tps65090_reg_contol_ops), | 215 | |
69 | tps65090_REG_DESC(FET5, "infet5", 0x13, tps65090_reg_contol_ops), | 216 | tps65090_REG_DESC(FET1, "infet1", 0x0F, |
70 | tps65090_REG_DESC(FET6, "infet6", 0x14, tps65090_reg_contol_ops), | 217 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), |
71 | tps65090_REG_DESC(FET7, "infet7", 0x15, tps65090_reg_contol_ops), | 218 | tps65090_fet_control_ops), |
72 | tps65090_REG_DESC(LDO1, "vsys-l1", 0, tps65090_ldo_ops), | 219 | tps65090_REG_DESC(FET2, "infet2", 0x10, |
73 | tps65090_REG_DESC(LDO2, "vsys-l2", 0, tps65090_ldo_ops), | 220 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), |
221 | tps65090_fet_control_ops), | ||
222 | tps65090_REG_DESC(FET3, "infet3", 0x11, | ||
223 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), | ||
224 | tps65090_fet_control_ops), | ||
225 | tps65090_REG_DESC(FET4, "infet4", 0x12, | ||
226 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), | ||
227 | tps65090_fet_control_ops), | ||
228 | tps65090_REG_DESC(FET5, "infet5", 0x13, | ||
229 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), | ||
230 | tps65090_fet_control_ops), | ||
231 | tps65090_REG_DESC(FET6, "infet6", 0x14, | ||
232 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), | ||
233 | tps65090_fet_control_ops), | ||
234 | tps65090_REG_DESC(FET7, "infet7", 0x15, | ||
235 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), | ||
236 | tps65090_fet_control_ops), | ||
237 | |||
238 | tps65090_REG_DESC(LDO1, "vsys-l1", 0, 0, | ||
239 | tps65090_ldo_ops), | ||
240 | tps65090_REG_DESC(LDO2, "vsys-l2", 0, 0, | ||
241 | tps65090_ldo_ops), | ||
74 | }; | 242 | }; |
75 | 243 | ||
76 | static inline bool is_dcdc(int id) | 244 | static inline bool is_dcdc(int id) |
@@ -209,6 +377,11 @@ static struct tps65090_platform_data *tps65090_parse_dt_reg_data( | |||
209 | rpdata->gpio = of_get_named_gpio(np, | 377 | rpdata->gpio = of_get_named_gpio(np, |
210 | "dcdc-ext-control-gpios", 0); | 378 | "dcdc-ext-control-gpios", 0); |
211 | 379 | ||
380 | if (of_property_read_u32(tps65090_matches[idx].of_node, | ||
381 | "ti,overcurrent-wait", | ||
382 | &rpdata->overcurrent_wait) == 0) | ||
383 | rpdata->overcurrent_wait_valid = true; | ||
384 | |||
212 | tps65090_pdata->reg_pdata[idx] = rpdata; | 385 | tps65090_pdata->reg_pdata[idx] = rpdata; |
213 | } | 386 | } |
214 | return tps65090_pdata; | 387 | return tps65090_pdata; |
@@ -258,6 +431,11 @@ static int tps65090_regulator_probe(struct platform_device *pdev) | |||
258 | ri = &pmic[num]; | 431 | ri = &pmic[num]; |
259 | ri->dev = &pdev->dev; | 432 | ri->dev = &pdev->dev; |
260 | ri->desc = &tps65090_regulator_desc[num]; | 433 | ri->desc = &tps65090_regulator_desc[num]; |
434 | if (tps_pdata) { | ||
435 | ri->overcurrent_wait_valid = | ||
436 | tps_pdata->overcurrent_wait_valid; | ||
437 | ri->overcurrent_wait = tps_pdata->overcurrent_wait; | ||
438 | } | ||
261 | 439 | ||
262 | /* | 440 | /* |
263 | * TPS5090 DCDC support the control from external digital input. | 441 | * TPS5090 DCDC support the control from external digital input. |
@@ -299,6 +477,12 @@ static int tps65090_regulator_probe(struct platform_device *pdev) | |||
299 | } | 477 | } |
300 | ri->rdev = rdev; | 478 | ri->rdev = rdev; |
301 | 479 | ||
480 | if (ri->overcurrent_wait_valid) { | ||
481 | ret = tps65090_reg_set_overcurrent_wait(ri, rdev); | ||
482 | if (ret < 0) | ||
483 | return ret; | ||
484 | } | ||
485 | |||
302 | /* Enable external control if it is require */ | 486 | /* Enable external control if it is require */ |
303 | if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data && | 487 | if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data && |
304 | tps_pdata->enable_ext_control) { | 488 | tps_pdata->enable_ext_control) { |