1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/module.h>
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/of.h>
23#include <linux/gpio/consumer.h>
24#include <linux/slab.h>
25#include <linux/err.h>
26#include <linux/platform_device.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/machine.h>
29#include <linux/regulator/of_regulator.h>
30#include <linux/mfd/tps65090.h>
31
32#define MAX_CTRL_READ_TRIES 5
33#define MAX_FET_ENABLE_TRIES 1000
34
35#define CTRL_EN_BIT 0
36#define CTRL_WT_BIT 2
37#define CTRL_PG_BIT 4
38#define CTRL_TO_BIT 7
39
40#define MAX_OVERCURRENT_WAIT 3
41
42
43
44
45
46
47
48
49
50
51
52struct tps65090_regulator {
53 struct device *dev;
54 struct regulator_desc *desc;
55 struct regulator_dev *rdev;
56 bool overcurrent_wait_valid;
57 int overcurrent_wait;
58};
59
60static struct regulator_ops tps65090_ext_control_ops = {
61};
62
63
64
65
66
67
68
69
70
71
72
73
74static 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
92
93
94
95
96
97
98static 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
131
132
133
134
135
136
137
138
139
140
141
142
143static int tps65090_fet_enable(struct regulator_dev *rdev)
144{
145 int ret, tries;
146
147
148
149
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
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;
173err:
174 dev_warn(&rdev->dev, "reg %#x enable failed\n", rdev->desc->enable_reg);
175 WARN_ON(1);
176
177 return ret;
178}
179
180static struct regulator_ops tps65090_reg_control_ops = {
181 .enable = regulator_enable_regmap,
182 .disable = regulator_disable_regmap,
183 .is_enabled = regulator_is_enabled_regmap,
184};
185
186static 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
192static struct regulator_ops tps65090_ldo_ops = {
193};
194
195#define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _nvolt, _volt, _ops) \
196{ \
197 .name = "TPS65090_RAILS"#_id, \
198 .supply_name = _sname, \
199 .id = TPS65090_REGULATOR_##_id, \
200 .n_voltages = _nvolt, \
201 .ops = &_ops, \
202 .fixed_uV = _volt, \
203 .enable_reg = _en_reg, \
204 .enable_val = _en_bits, \
205 .enable_mask = _en_bits, \
206 .type = REGULATOR_VOLTAGE, \
207 .owner = THIS_MODULE, \
208}
209
210#define tps65090_REG_FIXEDV(_id, _sname, en_reg, _en_bits, _volt, _ops) \
211 tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 1, _volt, _ops)
212
213#define tps65090_REG_SWITCH(_id, _sname, en_reg, _en_bits, _ops) \
214 tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 0, 0, _ops)
215
216static struct regulator_desc tps65090_regulator_desc[] = {
217 tps65090_REG_FIXEDV(DCDC1, "vsys1", 0x0C, BIT(CTRL_EN_BIT), 5000000,
218 tps65090_reg_control_ops),
219 tps65090_REG_FIXEDV(DCDC2, "vsys2", 0x0D, BIT(CTRL_EN_BIT), 3300000,
220 tps65090_reg_control_ops),
221 tps65090_REG_SWITCH(DCDC3, "vsys3", 0x0E, BIT(CTRL_EN_BIT),
222 tps65090_reg_control_ops),
223
224 tps65090_REG_SWITCH(FET1, "infet1", 0x0F,
225 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
226 tps65090_fet_control_ops),
227 tps65090_REG_SWITCH(FET2, "infet2", 0x10,
228 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
229 tps65090_fet_control_ops),
230 tps65090_REG_SWITCH(FET3, "infet3", 0x11,
231 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
232 tps65090_fet_control_ops),
233 tps65090_REG_SWITCH(FET4, "infet4", 0x12,
234 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
235 tps65090_fet_control_ops),
236 tps65090_REG_SWITCH(FET5, "infet5", 0x13,
237 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
238 tps65090_fet_control_ops),
239 tps65090_REG_SWITCH(FET6, "infet6", 0x14,
240 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
241 tps65090_fet_control_ops),
242 tps65090_REG_SWITCH(FET7, "infet7", 0x15,
243 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
244 tps65090_fet_control_ops),
245
246 tps65090_REG_FIXEDV(LDO1, "vsys-l1", 0, 0, 5000000,
247 tps65090_ldo_ops),
248 tps65090_REG_FIXEDV(LDO2, "vsys-l2", 0, 0, 3300000,
249 tps65090_ldo_ops),
250};
251
252static inline bool is_dcdc(int id)
253{
254 switch (id) {
255 case TPS65090_REGULATOR_DCDC1:
256 case TPS65090_REGULATOR_DCDC2:
257 case TPS65090_REGULATOR_DCDC3:
258 return true;
259 default:
260 return false;
261 }
262}
263
264static int tps65090_config_ext_control(
265 struct tps65090_regulator *ri, bool enable)
266{
267 int ret;
268 struct device *parent = ri->dev->parent;
269 unsigned int reg_en_reg = ri->desc->enable_reg;
270
271 if (enable)
272 ret = tps65090_set_bits(parent, reg_en_reg, 1);
273 else
274 ret = tps65090_clr_bits(parent, reg_en_reg, 1);
275 if (ret < 0)
276 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
277 return ret;
278}
279
280static int tps65090_regulator_disable_ext_control(
281 struct tps65090_regulator *ri,
282 struct tps65090_regulator_plat_data *tps_pdata)
283{
284 int ret = 0;
285 struct device *parent = ri->dev->parent;
286 unsigned int reg_en_reg = ri->desc->enable_reg;
287
288
289
290
291
292 if (tps_pdata->reg_init_data->constraints.always_on ||
293 tps_pdata->reg_init_data->constraints.boot_on) {
294 ret = tps65090_set_bits(parent, reg_en_reg, 0);
295 if (ret < 0) {
296 dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
297 return ret;
298 }
299 }
300 return tps65090_config_ext_control(ri, false);
301}
302
303#ifdef CONFIG_OF
304static struct of_regulator_match tps65090_matches[] = {
305 { .name = "dcdc1", },
306 { .name = "dcdc2", },
307 { .name = "dcdc3", },
308 { .name = "fet1", },
309 { .name = "fet2", },
310 { .name = "fet3", },
311 { .name = "fet4", },
312 { .name = "fet5", },
313 { .name = "fet6", },
314 { .name = "fet7", },
315 { .name = "ldo1", },
316 { .name = "ldo2", },
317};
318
319static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
320 struct platform_device *pdev,
321 struct of_regulator_match **tps65090_reg_matches)
322{
323 struct tps65090_platform_data *tps65090_pdata;
324 struct device_node *np = pdev->dev.parent->of_node;
325 struct device_node *regulators;
326 int idx = 0, ret;
327 struct tps65090_regulator_plat_data *reg_pdata;
328
329 tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
330 GFP_KERNEL);
331 if (!tps65090_pdata)
332 return ERR_PTR(-ENOMEM);
333
334 reg_pdata = devm_kcalloc(&pdev->dev,
335 TPS65090_REGULATOR_MAX, sizeof(*reg_pdata),
336 GFP_KERNEL);
337 if (!reg_pdata)
338 return ERR_PTR(-ENOMEM);
339
340 regulators = of_get_child_by_name(np, "regulators");
341 if (!regulators) {
342 dev_err(&pdev->dev, "regulator node not found\n");
343 return ERR_PTR(-ENODEV);
344 }
345
346 ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
347 ARRAY_SIZE(tps65090_matches));
348 of_node_put(regulators);
349 if (ret < 0) {
350 dev_err(&pdev->dev,
351 "Error parsing regulator init data: %d\n", ret);
352 return ERR_PTR(ret);
353 }
354
355 *tps65090_reg_matches = tps65090_matches;
356 for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
357 struct regulator_init_data *ri_data;
358 struct tps65090_regulator_plat_data *rpdata;
359
360 rpdata = ®_pdata[idx];
361 ri_data = tps65090_matches[idx].init_data;
362 if (!ri_data || !tps65090_matches[idx].of_node)
363 continue;
364
365 rpdata->reg_init_data = ri_data;
366 rpdata->enable_ext_control = of_property_read_bool(
367 tps65090_matches[idx].of_node,
368 "ti,enable-ext-control");
369 if (rpdata->enable_ext_control) {
370 enum gpiod_flags gflags;
371
372 if (ri_data->constraints.always_on ||
373 ri_data->constraints.boot_on)
374 gflags = GPIOD_OUT_HIGH;
375 else
376 gflags = GPIOD_OUT_LOW;
377 gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
378
379 rpdata->gpiod = devm_gpiod_get_from_of_node(&pdev->dev,
380 tps65090_matches[idx].of_node,
381 "dcdc-ext-control-gpios", 0,
382 gflags,
383 "tps65090");
384 if (IS_ERR(rpdata->gpiod))
385 return ERR_CAST(rpdata->gpiod);
386 if (!rpdata->gpiod)
387 dev_err(&pdev->dev,
388 "could not find DCDC external control GPIO\n");
389 }
390
391 if (of_property_read_u32(tps65090_matches[idx].of_node,
392 "ti,overcurrent-wait",
393 &rpdata->overcurrent_wait) == 0)
394 rpdata->overcurrent_wait_valid = true;
395
396 tps65090_pdata->reg_pdata[idx] = rpdata;
397 }
398 return tps65090_pdata;
399}
400#else
401static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
402 struct platform_device *pdev,
403 struct of_regulator_match **tps65090_reg_matches)
404{
405 *tps65090_reg_matches = NULL;
406 return NULL;
407}
408#endif
409
410static int tps65090_regulator_probe(struct platform_device *pdev)
411{
412 struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
413 struct tps65090_regulator *ri = NULL;
414 struct regulator_config config = { };
415 struct regulator_dev *rdev;
416 struct tps65090_regulator_plat_data *tps_pdata;
417 struct tps65090_regulator *pmic;
418 struct tps65090_platform_data *tps65090_pdata;
419 struct of_regulator_match *tps65090_reg_matches = NULL;
420 int num;
421 int ret;
422
423 dev_dbg(&pdev->dev, "Probing regulator\n");
424
425 tps65090_pdata = dev_get_platdata(pdev->dev.parent);
426 if (!tps65090_pdata && tps65090_mfd->dev->of_node)
427 tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
428 &tps65090_reg_matches);
429 if (IS_ERR_OR_NULL(tps65090_pdata)) {
430 dev_err(&pdev->dev, "Platform data missing\n");
431 return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
432 }
433
434 pmic = devm_kcalloc(&pdev->dev,
435 TPS65090_REGULATOR_MAX, sizeof(*pmic),
436 GFP_KERNEL);
437 if (!pmic)
438 return -ENOMEM;
439
440 for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
441 tps_pdata = tps65090_pdata->reg_pdata[num];
442
443 ri = &pmic[num];
444 ri->dev = &pdev->dev;
445 ri->desc = &tps65090_regulator_desc[num];
446 if (tps_pdata) {
447 ri->overcurrent_wait_valid =
448 tps_pdata->overcurrent_wait_valid;
449 ri->overcurrent_wait = tps_pdata->overcurrent_wait;
450 }
451
452
453
454
455
456 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
457 if (tps_pdata->enable_ext_control) {
458 config.ena_gpiod = tps_pdata->gpiod;
459 ri->desc->ops = &tps65090_ext_control_ops;
460 } else {
461 ret = tps65090_regulator_disable_ext_control(
462 ri, tps_pdata);
463 if (ret < 0) {
464 dev_err(&pdev->dev,
465 "failed disable ext control\n");
466 return ret;
467 }
468 }
469 }
470
471 config.dev = pdev->dev.parent;
472 config.driver_data = ri;
473 config.regmap = tps65090_mfd->rmap;
474 if (tps_pdata)
475 config.init_data = tps_pdata->reg_init_data;
476 else
477 config.init_data = NULL;
478 if (tps65090_reg_matches)
479 config.of_node = tps65090_reg_matches[num].of_node;
480 else
481 config.of_node = NULL;
482
483
484
485
486
487 if (config.ena_gpiod)
488 devm_gpiod_unhinge(&pdev->dev, config.ena_gpiod);
489 rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
490 if (IS_ERR(rdev)) {
491 dev_err(&pdev->dev, "failed to register regulator %s\n",
492 ri->desc->name);
493 return PTR_ERR(rdev);
494 }
495 ri->rdev = rdev;
496
497 if (ri->overcurrent_wait_valid) {
498 ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
499 if (ret < 0)
500 return ret;
501 }
502
503
504 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
505 tps_pdata->enable_ext_control) {
506 ret = tps65090_config_ext_control(ri, true);
507 if (ret < 0)
508 return ret;
509 }
510 }
511
512 platform_set_drvdata(pdev, pmic);
513 return 0;
514}
515
516static struct platform_driver tps65090_regulator_driver = {
517 .driver = {
518 .name = "tps65090-pmic",
519 },
520 .probe = tps65090_regulator_probe,
521};
522
523static int __init tps65090_regulator_init(void)
524{
525 return platform_driver_register(&tps65090_regulator_driver);
526}
527subsys_initcall(tps65090_regulator_init);
528
529static void __exit tps65090_regulator_exit(void)
530{
531 platform_driver_unregister(&tps65090_regulator_driver);
532}
533module_exit(tps65090_regulator_exit);
534
535MODULE_DESCRIPTION("tps65090 regulator driver");
536MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
537MODULE_LICENSE("GPL v2");
538MODULE_ALIAS("platform:tps65090-pmic");
539