blob: 3c4eb83be7744381a3e2f7450085c7cf6dfe7c80 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Keerthyffed0ea2016-09-30 09:20:44 +05302/*
3 * (C) Copyright 2016
4 * Texas Instruments Incorporated, <www.ti.com>
5 *
6 * Keerthy <j-keerthy@ti.com>
Keerthyffed0ea2016-09-30 09:20:44 +05307 */
8
9#include <common.h>
10#include <fdtdec.h>
11#include <errno.h>
12#include <dm.h>
Keerthyffed0ea2016-09-30 09:20:44 +053013#include <power/pmic.h>
14#include <power/regulator.h>
15#include <power/palmas.h>
16
Keerthyffed0ea2016-09-30 09:20:44 +053017#define REGULATOR_ON 0x1
18#define REGULATOR_OFF 0x0
19
20#define SMPS_MODE_MASK 0x3
21#define SMPS_MODE_SHIFT 0x0
22#define LDO_MODE_MASK 0x1
23#define LDO_MODE_SHIFT 0x0
24
25static const char palmas_smps_ctrl[][PALMAS_SMPS_NUM] = {
26 {0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 0x3c},
27 {0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38},
28 {0x20, 0x24, 0x2c, 0x30, 0x38},
29};
30
31static const char palmas_smps_volt[][PALMAS_SMPS_NUM] = {
32 {0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b, 0x3c},
33 {0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b},
34 {0x23, 0x27, 0x2f, 0x33, 0x3B}
35};
36
37static const char palmas_ldo_ctrl[][PALMAS_LDO_NUM] = {
38 {0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x62, 0x64},
39 {0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x62, 0x64},
40 {0x50, 0x52, 0x54, 0x5e, 0x62}
41};
42
43static const char palmas_ldo_volt[][PALMAS_LDO_NUM] = {
44 {0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65},
45 {0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65},
46 {0x51, 0x53, 0x55, 0x5f, 0x63}
47};
48
49static int palmas_smps_enable(struct udevice *dev, int op, bool *enable)
50{
51 int ret;
52 unsigned int adr;
Simon Glass71fa5b42020-12-03 16:55:18 -070053 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyffed0ea2016-09-30 09:20:44 +053054
Simon Glass71fa5b42020-12-03 16:55:18 -070055 uc_pdata = dev_get_uclass_plat(dev);
Keerthyffed0ea2016-09-30 09:20:44 +053056 adr = uc_pdata->ctrl_reg;
57
58 ret = pmic_reg_read(dev->parent, adr);
59 if (ret < 0)
60 return ret;
61
62 if (op == PMIC_OP_GET) {
63 ret &= PALMAS_SMPS_STATUS_MASK;
64
65 if (ret)
66 *enable = true;
67 else
68 *enable = false;
69
70 return 0;
71 } else if (op == PMIC_OP_SET) {
72 if (*enable)
73 ret |= PALMAS_SMPS_MODE_MASK;
74 else
75 ret &= ~(PALMAS_SMPS_MODE_MASK);
76
77 ret = pmic_reg_write(dev->parent, adr, ret);
78 if (ret)
79 return ret;
80 }
81
82 return 0;
83}
84
85static int palmas_smps_volt2hex(int uV)
86{
87 if (uV > PALMAS_LDO_VOLT_MAX)
88 return -EINVAL;
89
90 if (uV > 1650000)
91 return (uV - 1000000) / 20000 + 0x6;
92
93 if (uV == 500000)
94 return 0x6;
95 else
96 return 0x6 + ((uV - 500000) / 10000);
97}
98
99static int palmas_smps_hex2volt(int hex, bool range)
100{
101 unsigned int uV = 0;
102
103 if (hex > PALMAS_SMPS_VOLT_MAX_HEX)
104 return -EINVAL;
105
106 if (hex < 0x7)
107 uV = 500000;
108 else
109 uV = 500000 + (hex - 0x6) * 10000;
110
111 if (range)
112 uV *= 2;
113
114 return uV;
115}
116
117static int palmas_smps_val(struct udevice *dev, int op, int *uV)
118{
119 unsigned int hex, adr;
120 int ret;
121 bool range;
Simon Glass71fa5b42020-12-03 16:55:18 -0700122 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyffed0ea2016-09-30 09:20:44 +0530123
Simon Glass71fa5b42020-12-03 16:55:18 -0700124 uc_pdata = dev_get_uclass_plat(dev);
Keerthyffed0ea2016-09-30 09:20:44 +0530125
126 if (op == PMIC_OP_GET)
127 *uV = 0;
128
129 adr = uc_pdata->volt_reg;
130
131 ret = pmic_reg_read(dev->parent, adr);
132 if (ret < 0)
133 return ret;
134
135 if (op == PMIC_OP_GET) {
136 if (ret & PALMAS_SMPS_RANGE_MASK)
137 range = true;
138 else
139 range = false;
140
141 ret &= PALMAS_SMPS_VOLT_MASK;
142 ret = palmas_smps_hex2volt(ret, range);
143 if (ret < 0)
144 return ret;
145 *uV = ret;
146
147 return 0;
148 }
149
150 hex = palmas_smps_volt2hex(*uV);
151 if (hex < 0)
152 return hex;
153
154 ret &= ~PALMAS_SMPS_VOLT_MASK;
155 ret |= hex;
156 if (*uV > 1650000)
157 ret |= PALMAS_SMPS_RANGE_MASK;
158
159 return pmic_reg_write(dev->parent, adr, ret);
160}
161
Jean-Jacques Hiblot91827412017-07-12 11:42:47 +0200162static int palmas_ldo_bypass_enable(struct udevice *dev, bool enabled)
163{
164 int type = dev_get_driver_data(dev_get_parent(dev));
Simon Glass71fa5b42020-12-03 16:55:18 -0700165 struct dm_regulator_uclass_plat *p;
Jean-Jacques Hiblot91827412017-07-12 11:42:47 +0200166 unsigned int adr;
167 int reg;
168
169 if (type == TPS65917) {
170 /* bypass available only on LDO1 and LDO2 */
171 if (dev->driver_data > 2)
172 return -ENOTSUPP;
173 } else if (type == TPS659038) {
174 /* bypass available only on LDO9 */
175 if (dev->driver_data != 9)
176 return -ENOTSUPP;
177 }
178
Simon Glass71fa5b42020-12-03 16:55:18 -0700179 p = dev_get_uclass_plat(dev);
Jean-Jacques Hiblot91827412017-07-12 11:42:47 +0200180 adr = p->ctrl_reg;
181
182 reg = pmic_reg_read(dev->parent, adr);
183 if (reg < 0)
184 return reg;
185
186 if (enabled)
187 reg |= PALMAS_LDO_BYPASS_EN;
188 else
189 reg &= ~PALMAS_LDO_BYPASS_EN;
190
191 return pmic_reg_write(dev->parent, adr, reg);
192}
193
Keerthyffed0ea2016-09-30 09:20:44 +0530194static int palmas_ldo_enable(struct udevice *dev, int op, bool *enable)
195{
196 int ret;
197 unsigned int adr;
Simon Glass71fa5b42020-12-03 16:55:18 -0700198 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyffed0ea2016-09-30 09:20:44 +0530199
Simon Glass71fa5b42020-12-03 16:55:18 -0700200 uc_pdata = dev_get_uclass_plat(dev);
Keerthyffed0ea2016-09-30 09:20:44 +0530201 adr = uc_pdata->ctrl_reg;
202
203 ret = pmic_reg_read(dev->parent, adr);
204 if (ret < 0)
205 return ret;
206
207 if (op == PMIC_OP_GET) {
208 ret &= PALMAS_LDO_STATUS_MASK;
209
210 if (ret)
211 *enable = true;
212 else
213 *enable = false;
214
215 return 0;
216 } else if (op == PMIC_OP_SET) {
217 if (*enable)
218 ret |= PALMAS_LDO_MODE_MASK;
219 else
220 ret &= ~(PALMAS_LDO_MODE_MASK);
221
222 ret = pmic_reg_write(dev->parent, adr, ret);
223 if (ret)
224 return ret;
Jean-Jacques Hiblot91827412017-07-12 11:42:47 +0200225
226 ret = palmas_ldo_bypass_enable(dev, false);
227 if (ret && (ret != -ENOTSUPP))
228 return ret;
Keerthyffed0ea2016-09-30 09:20:44 +0530229 }
230
231 return 0;
232}
233
234static int palmas_ldo_volt2hex(int uV)
235{
236 if (uV > PALMAS_LDO_VOLT_MAX)
237 return -EINVAL;
238
239 return (uV - 850000) / 50000;
240}
241
242static int palmas_ldo_hex2volt(int hex)
243{
244 if (hex > PALMAS_LDO_VOLT_MAX_HEX)
245 return -EINVAL;
246
247 if (!hex)
248 return 0;
249
250 return (hex * 50000) + 850000;
251}
252
253static int palmas_ldo_val(struct udevice *dev, int op, int *uV)
254{
255 unsigned int hex, adr;
256 int ret;
257
Simon Glass71fa5b42020-12-03 16:55:18 -0700258 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyffed0ea2016-09-30 09:20:44 +0530259
260 if (op == PMIC_OP_GET)
261 *uV = 0;
262
Simon Glass71fa5b42020-12-03 16:55:18 -0700263 uc_pdata = dev_get_uclass_plat(dev);
Keerthyffed0ea2016-09-30 09:20:44 +0530264
265 adr = uc_pdata->volt_reg;
266
267 ret = pmic_reg_read(dev->parent, adr);
268 if (ret < 0)
269 return ret;
270
271 if (op == PMIC_OP_GET) {
272 ret &= PALMAS_LDO_VOLT_MASK;
273 ret = palmas_ldo_hex2volt(ret);
274 if (ret < 0)
275 return ret;
276 *uV = ret;
277 return 0;
278 }
279
280 hex = palmas_ldo_volt2hex(*uV);
281 if (hex < 0)
282 return hex;
283
284 ret &= ~PALMAS_LDO_VOLT_MASK;
285 ret |= hex;
286 if (*uV > 1650000)
287 ret |= 0x80;
288
289 return pmic_reg_write(dev->parent, adr, ret);
290}
291
292static int palmas_ldo_probe(struct udevice *dev)
293{
Simon Glass71fa5b42020-12-03 16:55:18 -0700294 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyffed0ea2016-09-30 09:20:44 +0530295 struct udevice *parent;
296
Simon Glass71fa5b42020-12-03 16:55:18 -0700297 uc_pdata = dev_get_uclass_plat(dev);
Keerthyffed0ea2016-09-30 09:20:44 +0530298
299 parent = dev_get_parent(dev);
300 int type = dev_get_driver_data(parent);
301
302 uc_pdata->type = REGULATOR_TYPE_LDO;
303
304 if (dev->driver_data) {
305 u8 idx = dev->driver_data - 1;
306 uc_pdata->ctrl_reg = palmas_ldo_ctrl[type][idx];
307 uc_pdata->volt_reg = palmas_ldo_volt[type][idx];
308 } else {
309 /* check for ldoln and ldousb cases */
310 if (!strcmp("ldoln", dev->name)) {
311 uc_pdata->ctrl_reg = palmas_ldo_ctrl[type][9];
312 uc_pdata->volt_reg = palmas_ldo_volt[type][9];
313 } else if (!strcmp("ldousb", dev->name)) {
314 uc_pdata->ctrl_reg = palmas_ldo_ctrl[type][10];
315 uc_pdata->volt_reg = palmas_ldo_volt[type][10];
316 }
317 }
318
319 return 0;
320}
321
322static int ldo_get_value(struct udevice *dev)
323{
324 int uV;
325 int ret;
326
327 ret = palmas_ldo_val(dev, PMIC_OP_GET, &uV);
328 if (ret)
329 return ret;
330
331 return uV;
332}
333
334static int ldo_set_value(struct udevice *dev, int uV)
335{
336 return palmas_ldo_val(dev, PMIC_OP_SET, &uV);
337}
338
Keerthye8de8912017-06-13 09:53:49 +0530339static int ldo_get_enable(struct udevice *dev)
Keerthyffed0ea2016-09-30 09:20:44 +0530340{
341 bool enable = false;
342 int ret;
343
344 ret = palmas_ldo_enable(dev, PMIC_OP_GET, &enable);
345 if (ret)
346 return ret;
347
348 return enable;
349}
350
351static int ldo_set_enable(struct udevice *dev, bool enable)
352{
353 return palmas_ldo_enable(dev, PMIC_OP_SET, &enable);
354}
355
356static int palmas_smps_probe(struct udevice *dev)
357{
Simon Glass71fa5b42020-12-03 16:55:18 -0700358 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyffed0ea2016-09-30 09:20:44 +0530359 struct udevice *parent;
360 int idx;
361
Simon Glass71fa5b42020-12-03 16:55:18 -0700362 uc_pdata = dev_get_uclass_plat(dev);
Keerthyffed0ea2016-09-30 09:20:44 +0530363
364 parent = dev_get_parent(dev);
365 int type = dev_get_driver_data(parent);
366
367 uc_pdata->type = REGULATOR_TYPE_BUCK;
368
369 switch (type) {
370 case PALMAS:
371 case TPS659038:
372 switch (dev->driver_data) {
373 case 123:
374 case 12:
375 uc_pdata->ctrl_reg = palmas_smps_ctrl[type][0];
376 uc_pdata->volt_reg = palmas_smps_volt[type][0];
377 break;
378 case 3:
379 uc_pdata->ctrl_reg = palmas_smps_ctrl[type][1];
380 uc_pdata->volt_reg = palmas_smps_volt[type][1];
381 break;
382 case 45:
383 uc_pdata->ctrl_reg = palmas_smps_ctrl[type][2];
384 uc_pdata->volt_reg = palmas_smps_volt[type][2];
385 break;
386 case 6:
387 case 7:
388 case 8:
389 case 9:
390 case 10:
Keerthyc89f7ce2017-02-03 17:04:08 +0530391 idx = dev->driver_data - 3;
Keerthyffed0ea2016-09-30 09:20:44 +0530392 uc_pdata->ctrl_reg = palmas_smps_ctrl[type][idx];
393 uc_pdata->volt_reg = palmas_smps_volt[type][idx];
394 break;
395
396 default:
397 printf("Wrong ID for regulator\n");
398 }
399 break;
400
401 case TPS65917:
402 switch (dev->driver_data) {
403 case 1:
404 case 2:
405 case 3:
406 case 4:
407 case 5:
408 idx = dev->driver_data - 1;
409 uc_pdata->ctrl_reg = palmas_smps_ctrl[type][idx];
410 uc_pdata->volt_reg = palmas_smps_volt[type][idx];
411 break;
Keerthy879cbd02017-06-02 10:51:51 +0530412 case 12:
413 idx = 0;
414 uc_pdata->ctrl_reg = palmas_smps_ctrl[type][idx];
415 uc_pdata->volt_reg = palmas_smps_volt[type][idx];
416 break;
Keerthyffed0ea2016-09-30 09:20:44 +0530417 default:
418 printf("Wrong ID for regulator\n");
419 }
420 break;
421
422 default:
423 printf("Invalid PMIC ID\n");
424 }
425
426 return 0;
427}
428
429static int smps_get_value(struct udevice *dev)
430{
431 int uV;
432 int ret;
433
434 ret = palmas_smps_val(dev, PMIC_OP_GET, &uV);
435 if (ret)
436 return ret;
437
438 return uV;
439}
440
441static int smps_set_value(struct udevice *dev, int uV)
442{
443 return palmas_smps_val(dev, PMIC_OP_SET, &uV);
444}
445
Keerthye8de8912017-06-13 09:53:49 +0530446static int smps_get_enable(struct udevice *dev)
Keerthyffed0ea2016-09-30 09:20:44 +0530447{
448 bool enable = false;
449 int ret;
450
451 ret = palmas_smps_enable(dev, PMIC_OP_GET, &enable);
452 if (ret)
453 return ret;
454
455 return enable;
456}
457
458static int smps_set_enable(struct udevice *dev, bool enable)
459{
460 return palmas_smps_enable(dev, PMIC_OP_SET, &enable);
461}
462
463static const struct dm_regulator_ops palmas_ldo_ops = {
464 .get_value = ldo_get_value,
465 .set_value = ldo_set_value,
466 .get_enable = ldo_get_enable,
467 .set_enable = ldo_set_enable,
468};
469
470U_BOOT_DRIVER(palmas_ldo) = {
471 .name = PALMAS_LDO_DRIVER,
472 .id = UCLASS_REGULATOR,
473 .ops = &palmas_ldo_ops,
474 .probe = palmas_ldo_probe,
475};
476
477static const struct dm_regulator_ops palmas_smps_ops = {
478 .get_value = smps_get_value,
479 .set_value = smps_set_value,
480 .get_enable = smps_get_enable,
481 .set_enable = smps_set_enable,
482};
483
484U_BOOT_DRIVER(palmas_smps) = {
485 .name = PALMAS_SMPS_DRIVER,
486 .id = UCLASS_REGULATOR,
487 .ops = &palmas_smps_ops,
488 .probe = palmas_smps_probe,
489};