Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Texas Instruments SoC Adaptive Body Bias(ABB) Regulator
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Mike Turquette <mturquette@ti.com>
7 *
8 * Copyright (C) 2012-2013 Texas Instruments, Inc.
9 * Andrii Tseglytskyi <andrii.tseglytskyi@ti.com>
10 * Nishanth Menon <nm@ti.com>
11 */
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/err.h>
15#include <linux/io.h>
16#include <linux/module.h>
17#include <linux/of_device.h>
18#include <linux/of.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
21#include <linux/regulator/machine.h>
22#include <linux/regulator/of_regulator.h>
23
24/*
25 * ABB LDO operating states:
26 * NOMINAL_OPP: bypasses the ABB LDO
27 * FAST_OPP: sets ABB LDO to Forward Body-Bias
28 * SLOW_OPP: sets ABB LDO to Reverse Body-Bias
29 */
30#define TI_ABB_NOMINAL_OPP 0
31#define TI_ABB_FAST_OPP 1
32#define TI_ABB_SLOW_OPP 3
33
34/**
35 * struct ti_abb_info - ABB information per voltage setting
36 * @opp_sel: one of TI_ABB macro
37 * @vset: (optional) vset value that LDOVBB needs to be overridden with.
38 *
39 * Array of per voltage entries organized in the same order as regulator_desc's
40 * volt_table list. (selector is used to index from this array)
41 */
42struct ti_abb_info {
43 u32 opp_sel;
44 u32 vset;
45};
46
47/**
48 * struct ti_abb_reg - Register description for ABB block
49 * @setup_off: setup register offset from base
50 * @control_off: control register offset from base
51 * @sr2_wtcnt_value_mask: setup register- sr2_wtcnt_value mask
52 * @fbb_sel_mask: setup register- FBB sel mask
53 * @rbb_sel_mask: setup register- RBB sel mask
54 * @sr2_en_mask: setup register- enable mask
55 * @opp_change_mask: control register - mask to trigger LDOVBB change
56 * @opp_sel_mask: control register - mask for mode to operate
57 */
58struct ti_abb_reg {
59 u32 setup_off;
60 u32 control_off;
61
62 /* Setup register fields */
63 u32 sr2_wtcnt_value_mask;
64 u32 fbb_sel_mask;
65 u32 rbb_sel_mask;
66 u32 sr2_en_mask;
67
68 /* Control register fields */
69 u32 opp_change_mask;
70 u32 opp_sel_mask;
71};
72
73/**
74 * struct ti_abb - ABB instance data
75 * @rdesc: regulator descriptor
76 * @clk: clock(usually sysclk) supplying ABB block
77 * @base: base address of ABB block
78 * @setup_reg: setup register of ABB block
79 * @control_reg: control register of ABB block
80 * @int_base: interrupt register base address
81 * @efuse_base: (optional) efuse base address for ABB modes
82 * @ldo_base: (optional) LDOVBB vset override base address
83 * @regs: pointer to struct ti_abb_reg for ABB block
84 * @txdone_mask: mask on int_base for tranxdone interrupt
85 * @ldovbb_override_mask: mask to ldo_base for overriding default LDO VBB
86 * vset with value from efuse
87 * @ldovbb_vset_mask: mask to ldo_base for providing the VSET override
88 * @info: array to per voltage ABB configuration
89 * @current_info_idx: current index to info
90 * @settling_time: SoC specific settling time for LDO VBB
91 */
92struct ti_abb {
93 struct regulator_desc rdesc;
94 struct clk *clk;
95 void __iomem *base;
96 void __iomem *setup_reg;
97 void __iomem *control_reg;
98 void __iomem *int_base;
99 void __iomem *efuse_base;
100 void __iomem *ldo_base;
101
102 const struct ti_abb_reg *regs;
103 u32 txdone_mask;
104 u32 ldovbb_override_mask;
105 u32 ldovbb_vset_mask;
106
107 struct ti_abb_info *info;
108 int current_info_idx;
109
110 u32 settling_time;
111};
112
113/**
114 * ti_abb_rmw() - handy wrapper to set specific register bits
115 * @mask: mask for register field
116 * @value: value shifted to mask location and written
117 * @reg: register address
118 *
119 * Return: final register value (may be unused)
120 */
121static inline u32 ti_abb_rmw(u32 mask, u32 value, void __iomem *reg)
122{
123 u32 val;
124
125 val = readl(reg);
126 val &= ~mask;
127 val |= (value << __ffs(mask)) & mask;
128 writel(val, reg);
129
130 return val;
131}
132
133/**
134 * ti_abb_check_txdone() - handy wrapper to check ABB tranxdone status
135 * @abb: pointer to the abb instance
136 *
137 * Return: true or false
138 */
139static inline bool ti_abb_check_txdone(const struct ti_abb *abb)
140{
141 return !!(readl(abb->int_base) & abb->txdone_mask);
142}
143
144/**
145 * ti_abb_clear_txdone() - handy wrapper to clear ABB tranxdone status
146 * @abb: pointer to the abb instance
147 */
148static inline void ti_abb_clear_txdone(const struct ti_abb *abb)
149{
150 writel(abb->txdone_mask, abb->int_base);
151};
152
153/**
154 * ti_abb_wait_txdone() - waits for ABB tranxdone event
155 * @dev: device
156 * @abb: pointer to the abb instance
157 *
158 * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
159 */
160static int ti_abb_wait_txdone(struct device *dev, struct ti_abb *abb)
161{
162 int timeout = 0;
163 bool status;
164
165 while (timeout++ <= abb->settling_time) {
166 status = ti_abb_check_txdone(abb);
167 if (status)
168 return 0;
169
170 udelay(1);
171 }
172
173 dev_warn_ratelimited(dev, "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
174 __func__, timeout, readl(abb->int_base));
175 return -ETIMEDOUT;
176}
177
178/**
179 * ti_abb_clear_all_txdone() - clears ABB tranxdone event
180 * @dev: device
181 * @abb: pointer to the abb instance
182 *
183 * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
184 */
185static int ti_abb_clear_all_txdone(struct device *dev, const struct ti_abb *abb)
186{
187 int timeout = 0;
188 bool status;
189
190 while (timeout++ <= abb->settling_time) {
191 ti_abb_clear_txdone(abb);
192
193 status = ti_abb_check_txdone(abb);
194 if (!status)
195 return 0;
196
197 udelay(1);
198 }
199
200 dev_warn_ratelimited(dev, "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
201 __func__, timeout, readl(abb->int_base));
202 return -ETIMEDOUT;
203}
204
205/**
206 * ti_abb_program_ldovbb() - program LDOVBB register for override value
207 * @dev: device
208 * @abb: pointer to the abb instance
209 * @info: ABB info to program
210 */
211static void ti_abb_program_ldovbb(struct device *dev, const struct ti_abb *abb,
212 struct ti_abb_info *info)
213{
214 u32 val;
215
216 val = readl(abb->ldo_base);
217 /* clear up previous values */
218 val &= ~(abb->ldovbb_override_mask | abb->ldovbb_vset_mask);
219
220 switch (info->opp_sel) {
221 case TI_ABB_SLOW_OPP:
222 case TI_ABB_FAST_OPP:
223 val |= abb->ldovbb_override_mask;
224 val |= info->vset << __ffs(abb->ldovbb_vset_mask);
225 break;
226 }
227
228 writel(val, abb->ldo_base);
229}
230
231/**
232 * ti_abb_set_opp() - Setup ABB and LDO VBB for required bias
233 * @rdev: regulator device
234 * @abb: pointer to the abb instance
235 * @info: ABB info to program
236 *
237 * Return: 0 on success or appropriate error value when fails
238 */
239static int ti_abb_set_opp(struct regulator_dev *rdev, struct ti_abb *abb,
240 struct ti_abb_info *info)
241{
242 const struct ti_abb_reg *regs = abb->regs;
243 struct device *dev = &rdev->dev;
244 int ret;
245
246 ret = ti_abb_clear_all_txdone(dev, abb);
247 if (ret)
248 goto out;
249
250 ti_abb_rmw(regs->fbb_sel_mask | regs->rbb_sel_mask, 0, abb->setup_reg);
251
252 switch (info->opp_sel) {
253 case TI_ABB_SLOW_OPP:
254 ti_abb_rmw(regs->rbb_sel_mask, 1, abb->setup_reg);
255 break;
256 case TI_ABB_FAST_OPP:
257 ti_abb_rmw(regs->fbb_sel_mask, 1, abb->setup_reg);
258 break;
259 }
260
261 /* program next state of ABB ldo */
262 ti_abb_rmw(regs->opp_sel_mask, info->opp_sel, abb->control_reg);
263
264 /*
265 * program LDO VBB vset override if needed for !bypass mode
266 * XXX: Do not switch sequence - for !bypass, LDO override reset *must*
267 * be performed *before* switch to bias mode else VBB glitches.
268 */
269 if (abb->ldo_base && info->opp_sel != TI_ABB_NOMINAL_OPP)
270 ti_abb_program_ldovbb(dev, abb, info);
271
272 /* Initiate ABB ldo change */
273 ti_abb_rmw(regs->opp_change_mask, 1, abb->control_reg);
274
275 /* Wait for ABB LDO to complete transition to new Bias setting */
276 ret = ti_abb_wait_txdone(dev, abb);
277 if (ret)
278 goto out;
279
280 ret = ti_abb_clear_all_txdone(dev, abb);
281 if (ret)
282 goto out;
283
284 /*
285 * Reset LDO VBB vset override bypass mode
286 * XXX: Do not switch sequence - for bypass, LDO override reset *must*
287 * be performed *after* switch to bypass else VBB glitches.
288 */
289 if (abb->ldo_base && info->opp_sel == TI_ABB_NOMINAL_OPP)
290 ti_abb_program_ldovbb(dev, abb, info);
291
292out:
293 return ret;
294}
295
296/**
297 * ti_abb_set_voltage_sel() - regulator accessor function to set ABB LDO
298 * @rdev: regulator device
299 * @sel: selector to index into required ABB LDO settings (maps to
300 * regulator descriptor's volt_table)
301 *
302 * Return: 0 on success or appropriate error value when fails
303 */
304static int ti_abb_set_voltage_sel(struct regulator_dev *rdev, unsigned int sel)
305{
306 const struct regulator_desc *desc = rdev->desc;
307 struct ti_abb *abb = rdev_get_drvdata(rdev);
308 struct device *dev = &rdev->dev;
309 struct ti_abb_info *info, *oinfo;
310 int ret = 0;
311
312 if (!abb) {
313 dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
314 __func__);
315 return -ENODEV;
316 }
317
318 if (!desc->n_voltages || !abb->info) {
319 dev_err_ratelimited(dev,
320 "%s: No valid voltage table entries?\n",
321 __func__);
322 return -EINVAL;
323 }
324
325 if (sel >= desc->n_voltages) {
326 dev_err(dev, "%s: sel idx(%d) >= n_voltages(%d)\n", __func__,
327 sel, desc->n_voltages);
328 return -EINVAL;
329 }
330
331 /* If we are in the same index as we were, nothing to do here! */
332 if (sel == abb->current_info_idx) {
333 dev_dbg(dev, "%s: Already at sel=%d\n", __func__, sel);
334 return ret;
335 }
336
337 info = &abb->info[sel];
338 /*
339 * When Linux kernel is starting up, we aren't sure of the
340 * Bias configuration that bootloader has configured.
341 * So, we get to know the actual setting the first time
342 * we are asked to transition.
343 */
344 if (abb->current_info_idx == -EINVAL)
345 goto just_set_abb;
346
347 /* If data is exactly the same, then just update index, no change */
348 oinfo = &abb->info[abb->current_info_idx];
349 if (!memcmp(info, oinfo, sizeof(*info))) {
350 dev_dbg(dev, "%s: Same data new idx=%d, old idx=%d\n", __func__,
351 sel, abb->current_info_idx);
352 goto out;
353 }
354
355just_set_abb:
356 ret = ti_abb_set_opp(rdev, abb, info);
357
358out:
359 if (!ret)
360 abb->current_info_idx = sel;
361 else
362 dev_err_ratelimited(dev,
363 "%s: Volt[%d] idx[%d] mode[%d] Fail(%d)\n",
364 __func__, desc->volt_table[sel], sel,
365 info->opp_sel, ret);
366 return ret;
367}
368
369/**
370 * ti_abb_get_voltage_sel() - Regulator accessor to get current ABB LDO setting
371 * @rdev: regulator device
372 *
373 * Return: 0 on success or appropriate error value when fails
374 */
375static int ti_abb_get_voltage_sel(struct regulator_dev *rdev)
376{
377 const struct regulator_desc *desc = rdev->desc;
378 struct ti_abb *abb = rdev_get_drvdata(rdev);
379 struct device *dev = &rdev->dev;
380
381 if (!abb) {
382 dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
383 __func__);
384 return -ENODEV;
385 }
386
387 if (!desc->n_voltages || !abb->info) {
388 dev_err_ratelimited(dev,
389 "%s: No valid voltage table entries?\n",
390 __func__);
391 return -EINVAL;
392 }
393
394 if (abb->current_info_idx >= (int)desc->n_voltages) {
395 dev_err(dev, "%s: Corrupted data? idx(%d) >= n_voltages(%d)\n",
396 __func__, abb->current_info_idx, desc->n_voltages);
397 return -EINVAL;
398 }
399
400 return abb->current_info_idx;
401}
402
403/**
404 * ti_abb_init_timings() - setup ABB clock timing for the current platform
405 * @dev: device
406 * @abb: pointer to the abb instance
407 *
408 * Return: 0 if timing is updated, else returns error result.
409 */
410static int ti_abb_init_timings(struct device *dev, struct ti_abb *abb)
411{
412 u32 clock_cycles;
413 u32 clk_rate, sr2_wt_cnt_val, cycle_rate;
414 const struct ti_abb_reg *regs = abb->regs;
415 int ret;
416 char *pname = "ti,settling-time";
417
418 /* read device tree properties */
419 ret = of_property_read_u32(dev->of_node, pname, &abb->settling_time);
420 if (ret) {
421 dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
422 return ret;
423 }
424
425 /* ABB LDO cannot be settle in 0 time */
426 if (!abb->settling_time) {
427 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
428 return -EINVAL;
429 }
430
431 pname = "ti,clock-cycles";
432 ret = of_property_read_u32(dev->of_node, pname, &clock_cycles);
433 if (ret) {
434 dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
435 return ret;
436 }
437 /* ABB LDO cannot be settle in 0 clock cycles */
438 if (!clock_cycles) {
439 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
440 return -EINVAL;
441 }
442
443 abb->clk = devm_clk_get(dev, NULL);
444 if (IS_ERR(abb->clk)) {
445 ret = PTR_ERR(abb->clk);
446 dev_err(dev, "%s: Unable to get clk(%d)\n", __func__, ret);
447 return ret;
448 }
449
450 /*
451 * SR2_WTCNT_VALUE is the settling time for the ABB ldo after a
452 * transition and must be programmed with the correct time at boot.
453 * The value programmed into the register is the number of SYS_CLK
454 * clock cycles that match a given wall time profiled for the ldo.
455 * This value depends on:
456 * settling time of ldo in micro-seconds (varies per OMAP family)
457 * # of clock cycles per SYS_CLK period (varies per OMAP family)
458 * the SYS_CLK frequency in MHz (varies per board)
459 * The formula is:
460 *
461 * ldo settling time (in micro-seconds)
462 * SR2_WTCNT_VALUE = ------------------------------------------
463 * (# system clock cycles) * (sys_clk period)
464 *
465 * Put another way:
466 *
467 * SR2_WTCNT_VALUE = settling time / (# SYS_CLK cycles / SYS_CLK rate))
468 *
469 * To avoid dividing by zero multiply both "# clock cycles" and
470 * "settling time" by 10 such that the final result is the one we want.
471 */
472
473 /* Convert SYS_CLK rate to MHz & prevent divide by zero */
474 clk_rate = DIV_ROUND_CLOSEST(clk_get_rate(abb->clk), 1000000);
475
476 /* Calculate cycle rate */
477 cycle_rate = DIV_ROUND_CLOSEST(clock_cycles * 10, clk_rate);
478
479 /* Calculate SR2_WTCNT_VALUE */
480 sr2_wt_cnt_val = DIV_ROUND_CLOSEST(abb->settling_time * 10, cycle_rate);
481
482 dev_dbg(dev, "%s: Clk_rate=%ld, sr2_cnt=0x%08x\n", __func__,
483 clk_get_rate(abb->clk), sr2_wt_cnt_val);
484
485 ti_abb_rmw(regs->sr2_wtcnt_value_mask, sr2_wt_cnt_val, abb->setup_reg);
486
487 return 0;
488}
489
490/**
491 * ti_abb_init_table() - Initialize ABB table from device tree
492 * @dev: device
493 * @abb: pointer to the abb instance
494 * @rinit_data: regulator initdata
495 *
496 * Return: 0 on success or appropriate error value when fails
497 */
498static int ti_abb_init_table(struct device *dev, struct ti_abb *abb,
499 struct regulator_init_data *rinit_data)
500{
501 struct ti_abb_info *info;
502 const u32 num_values = 6;
503 char *pname = "ti,abb_info";
504 u32 i;
505 unsigned int *volt_table;
506 int num_entries, min_uV = INT_MAX, max_uV = 0;
507 struct regulation_constraints *c = &rinit_data->constraints;
508
509 /*
510 * Each abb_info is a set of n-tuple, where n is num_values, consisting
511 * of voltage and a set of detection logic for ABB information for that
512 * voltage to apply.
513 */
514 num_entries = of_property_count_u32_elems(dev->of_node, pname);
515 if (num_entries < 0) {
516 dev_err(dev, "No '%s' property?\n", pname);
517 return num_entries;
518 }
519
520 if (!num_entries || (num_entries % num_values)) {
521 dev_err(dev, "All '%s' list entries need %d vals\n", pname,
522 num_values);
523 return -EINVAL;
524 }
525 num_entries /= num_values;
526
527 info = devm_kcalloc(dev, num_entries, sizeof(*info), GFP_KERNEL);
528 if (!info)
529 return -ENOMEM;
530
531 abb->info = info;
532
533 volt_table = devm_kcalloc(dev, num_entries, sizeof(unsigned int),
534 GFP_KERNEL);
535 if (!volt_table)
536 return -ENOMEM;
537
538 abb->rdesc.n_voltages = num_entries;
539 abb->rdesc.volt_table = volt_table;
540 /* We do not know where the OPP voltage is at the moment */
541 abb->current_info_idx = -EINVAL;
542
543 for (i = 0; i < num_entries; i++, info++, volt_table++) {
544 u32 efuse_offset, rbb_mask, fbb_mask, vset_mask;
545 u32 efuse_val;
546
547 /* NOTE: num_values should equal to entries picked up here */
548 of_property_read_u32_index(dev->of_node, pname, i * num_values,
549 volt_table);
550 of_property_read_u32_index(dev->of_node, pname,
551 i * num_values + 1, &info->opp_sel);
552 of_property_read_u32_index(dev->of_node, pname,
553 i * num_values + 2, &efuse_offset);
554 of_property_read_u32_index(dev->of_node, pname,
555 i * num_values + 3, &rbb_mask);
556 of_property_read_u32_index(dev->of_node, pname,
557 i * num_values + 4, &fbb_mask);
558 of_property_read_u32_index(dev->of_node, pname,
559 i * num_values + 5, &vset_mask);
560
561 dev_dbg(dev,
562 "[%d]v=%d ABB=%d ef=0x%x rbb=0x%x fbb=0x%x vset=0x%x\n",
563 i, *volt_table, info->opp_sel, efuse_offset, rbb_mask,
564 fbb_mask, vset_mask);
565
566 /* Find min/max for voltage set */
567 if (min_uV > *volt_table)
568 min_uV = *volt_table;
569 if (max_uV < *volt_table)
570 max_uV = *volt_table;
571
572 if (!abb->efuse_base) {
573 /* Ignore invalid data, but warn to help cleanup */
574 if (efuse_offset || rbb_mask || fbb_mask || vset_mask)
575 dev_err(dev, "prop '%s': v=%d,bad efuse/mask\n",
576 pname, *volt_table);
577 goto check_abb;
578 }
579
580 efuse_val = readl(abb->efuse_base + efuse_offset);
581
582 /* Use ABB recommendation from Efuse */
583 if (efuse_val & rbb_mask)
584 info->opp_sel = TI_ABB_SLOW_OPP;
585 else if (efuse_val & fbb_mask)
586 info->opp_sel = TI_ABB_FAST_OPP;
587 else if (rbb_mask || fbb_mask)
588 info->opp_sel = TI_ABB_NOMINAL_OPP;
589
590 dev_dbg(dev,
591 "[%d]v=%d efusev=0x%x final ABB=%d\n",
592 i, *volt_table, efuse_val, info->opp_sel);
593
594 /* Use recommended Vset bits from Efuse */
595 if (!abb->ldo_base) {
596 if (vset_mask)
597 dev_err(dev, "prop'%s':v=%d vst=%x LDO base?\n",
598 pname, *volt_table, vset_mask);
599 continue;
600 }
601 info->vset = (efuse_val & vset_mask) >> __ffs(vset_mask);
602 dev_dbg(dev, "[%d]v=%d vset=%x\n", i, *volt_table, info->vset);
603check_abb:
604 switch (info->opp_sel) {
605 case TI_ABB_NOMINAL_OPP:
606 case TI_ABB_FAST_OPP:
607 case TI_ABB_SLOW_OPP:
608 /* Valid values */
609 break;
610 default:
611 dev_err(dev, "%s:[%d]v=%d, ABB=%d is invalid! Abort!\n",
612 __func__, i, *volt_table, info->opp_sel);
613 return -EINVAL;
614 }
615 }
616
617 /* Setup the min/max voltage constraints from the supported list */
618 c->min_uV = min_uV;
619 c->max_uV = max_uV;
620
621 return 0;
622}
623
624static const struct regulator_ops ti_abb_reg_ops = {
625 .list_voltage = regulator_list_voltage_table,
626
627 .set_voltage_sel = ti_abb_set_voltage_sel,
628 .get_voltage_sel = ti_abb_get_voltage_sel,
629};
630
631/* Default ABB block offsets, IF this changes in future, create new one */
632static const struct ti_abb_reg abb_regs_v1 = {
633 /* WARNING: registers are wrongly documented in TRM */
634 .setup_off = 0x04,
635 .control_off = 0x00,
636
637 .sr2_wtcnt_value_mask = (0xff << 8),
638 .fbb_sel_mask = (0x01 << 2),
639 .rbb_sel_mask = (0x01 << 1),
640 .sr2_en_mask = (0x01 << 0),
641
642 .opp_change_mask = (0x01 << 2),
643 .opp_sel_mask = (0x03 << 0),
644};
645
646static const struct ti_abb_reg abb_regs_v2 = {
647 .setup_off = 0x00,
648 .control_off = 0x04,
649
650 .sr2_wtcnt_value_mask = (0xff << 8),
651 .fbb_sel_mask = (0x01 << 2),
652 .rbb_sel_mask = (0x01 << 1),
653 .sr2_en_mask = (0x01 << 0),
654
655 .opp_change_mask = (0x01 << 2),
656 .opp_sel_mask = (0x03 << 0),
657};
658
659static const struct ti_abb_reg abb_regs_generic = {
660 .sr2_wtcnt_value_mask = (0xff << 8),
661 .fbb_sel_mask = (0x01 << 2),
662 .rbb_sel_mask = (0x01 << 1),
663 .sr2_en_mask = (0x01 << 0),
664
665 .opp_change_mask = (0x01 << 2),
666 .opp_sel_mask = (0x03 << 0),
667};
668
669static const struct of_device_id ti_abb_of_match[] = {
670 {.compatible = "ti,abb-v1", .data = &abb_regs_v1},
671 {.compatible = "ti,abb-v2", .data = &abb_regs_v2},
672 {.compatible = "ti,abb-v3", .data = &abb_regs_generic},
673 { },
674};
675
676MODULE_DEVICE_TABLE(of, ti_abb_of_match);
677
678/**
679 * ti_abb_probe() - Initialize an ABB ldo instance
680 * @pdev: ABB platform device
681 *
682 * Initializes an individual ABB LDO for required Body-Bias. ABB is used to
683 * additional bias supply to SoC modules for power savings or mandatory stability
684 * configuration at certain Operating Performance Points(OPPs).
685 *
686 * Return: 0 on success or appropriate error value when fails
687 */
688static int ti_abb_probe(struct platform_device *pdev)
689{
690 struct device *dev = &pdev->dev;
691 const struct of_device_id *match;
692 struct resource *res;
693 struct ti_abb *abb;
694 struct regulator_init_data *initdata = NULL;
695 struct regulator_dev *rdev = NULL;
696 struct regulator_desc *desc;
697 struct regulation_constraints *c;
698 struct regulator_config config = { };
699 char *pname;
700 int ret = 0;
701
702 match = of_match_device(ti_abb_of_match, dev);
703 if (!match) {
704 /* We do not expect this to happen */
705 dev_err(dev, "%s: Unable to match device\n", __func__);
706 return -ENODEV;
707 }
708 if (!match->data) {
709 dev_err(dev, "%s: Bad data in match\n", __func__);
710 return -EINVAL;
711 }
712
713 abb = devm_kzalloc(dev, sizeof(struct ti_abb), GFP_KERNEL);
714 if (!abb)
715 return -ENOMEM;
716 abb->regs = match->data;
717
718 /* Map ABB resources */
719 if (abb->regs->setup_off || abb->regs->control_off) {
720 abb->base = devm_platform_ioremap_resource_byname(pdev, "base-address");
721 if (IS_ERR(abb->base))
722 return PTR_ERR(abb->base);
723
724 abb->setup_reg = abb->base + abb->regs->setup_off;
725 abb->control_reg = abb->base + abb->regs->control_off;
726
727 } else {
728 abb->control_reg = devm_platform_ioremap_resource_byname(pdev, "control-address");
729 if (IS_ERR(abb->control_reg))
730 return PTR_ERR(abb->control_reg);
731
732 abb->setup_reg = devm_platform_ioremap_resource_byname(pdev, "setup-address");
733 if (IS_ERR(abb->setup_reg))
734 return PTR_ERR(abb->setup_reg);
735 }
736
737 abb->int_base = devm_platform_ioremap_resource_byname(pdev, "int-address");
738 if (IS_ERR(abb->int_base))
739 return PTR_ERR(abb->int_base);
740
741 /* Map Optional resources */
742 pname = "efuse-address";
743 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
744 if (!res) {
745 dev_dbg(dev, "Missing '%s' IO resource\n", pname);
746 ret = -ENODEV;
747 goto skip_opt;
748 }
749
750 /*
751 * We may have shared efuse register offsets which are read-only
752 * between domains
753 */
754 abb->efuse_base = devm_ioremap(dev, res->start,
755 resource_size(res));
756 if (!abb->efuse_base) {
757 dev_err(dev, "Unable to map '%s'\n", pname);
758 return -ENOMEM;
759 }
760
761 pname = "ldo-address";
762 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
763 if (!res) {
764 dev_dbg(dev, "Missing '%s' IO resource\n", pname);
765 ret = -ENODEV;
766 goto skip_opt;
767 }
768 abb->ldo_base = devm_ioremap_resource(dev, res);
769 if (IS_ERR(abb->ldo_base))
770 return PTR_ERR(abb->ldo_base);
771
772 /* IF ldo_base is set, the following are mandatory */
773 pname = "ti,ldovbb-override-mask";
774 ret =
775 of_property_read_u32(pdev->dev.of_node, pname,
776 &abb->ldovbb_override_mask);
777 if (ret) {
778 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
779 return ret;
780 }
781 if (!abb->ldovbb_override_mask) {
782 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
783 return -EINVAL;
784 }
785
786 pname = "ti,ldovbb-vset-mask";
787 ret =
788 of_property_read_u32(pdev->dev.of_node, pname,
789 &abb->ldovbb_vset_mask);
790 if (ret) {
791 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
792 return ret;
793 }
794 if (!abb->ldovbb_vset_mask) {
795 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
796 return -EINVAL;
797 }
798
799skip_opt:
800 pname = "ti,tranxdone-status-mask";
801 ret =
802 of_property_read_u32(pdev->dev.of_node, pname,
803 &abb->txdone_mask);
804 if (ret) {
805 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
806 return ret;
807 }
808 if (!abb->txdone_mask) {
809 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
810 return -EINVAL;
811 }
812
813 initdata = of_get_regulator_init_data(dev, pdev->dev.of_node,
814 &abb->rdesc);
815 if (!initdata) {
816 dev_err(dev, "%s: Unable to alloc regulator init data\n",
817 __func__);
818 return -ENOMEM;
819 }
820
821 /* init ABB opp_sel table */
822 ret = ti_abb_init_table(dev, abb, initdata);
823 if (ret)
824 return ret;
825
826 /* init ABB timing */
827 ret = ti_abb_init_timings(dev, abb);
828 if (ret)
829 return ret;
830
831 desc = &abb->rdesc;
832 desc->name = dev_name(dev);
833 desc->owner = THIS_MODULE;
834 desc->type = REGULATOR_VOLTAGE;
835 desc->ops = &ti_abb_reg_ops;
836
837 c = &initdata->constraints;
838 if (desc->n_voltages > 1)
839 c->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
840 c->always_on = true;
841
842 config.dev = dev;
843 config.init_data = initdata;
844 config.driver_data = abb;
845 config.of_node = pdev->dev.of_node;
846
847 rdev = devm_regulator_register(dev, desc, &config);
848 if (IS_ERR(rdev)) {
849 ret = PTR_ERR(rdev);
850 dev_err(dev, "%s: failed to register regulator(%d)\n",
851 __func__, ret);
852 return ret;
853 }
854 platform_set_drvdata(pdev, rdev);
855
856 /* Enable the ldo if not already done by bootloader */
857 ti_abb_rmw(abb->regs->sr2_en_mask, 1, abb->setup_reg);
858
859 return 0;
860}
861
862MODULE_ALIAS("platform:ti_abb");
863
864static struct platform_driver ti_abb_driver = {
865 .probe = ti_abb_probe,
866 .driver = {
867 .name = "ti_abb",
868 .of_match_table = of_match_ptr(ti_abb_of_match),
869 },
870};
871module_platform_driver(ti_abb_driver);
872
873MODULE_DESCRIPTION("Texas Instruments ABB LDO regulator driver");
874MODULE_AUTHOR("Texas Instruments Inc.");
875MODULE_LICENSE("GPL v2");
1/*
2 * Texas Instruments SoC Adaptive Body Bias(ABB) Regulator
3 *
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Mike Turquette <mturquette@ti.com>
6 *
7 * Copyright (C) 2012-2013 Texas Instruments, Inc.
8 * Andrii Tseglytskyi <andrii.tseglytskyi@ti.com>
9 * Nishanth Menon <nm@ti.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
16 * kind, whether express or implied; without even the implied warranty
17 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20#include <linux/clk.h>
21#include <linux/delay.h>
22#include <linux/err.h>
23#include <linux/io.h>
24#include <linux/module.h>
25#include <linux/of_device.h>
26#include <linux/of.h>
27#include <linux/platform_device.h>
28#include <linux/regulator/driver.h>
29#include <linux/regulator/machine.h>
30#include <linux/regulator/of_regulator.h>
31
32/*
33 * ABB LDO operating states:
34 * NOMINAL_OPP: bypasses the ABB LDO
35 * FAST_OPP: sets ABB LDO to Forward Body-Bias
36 * SLOW_OPP: sets ABB LDO to Reverse Body-Bias
37 */
38#define TI_ABB_NOMINAL_OPP 0
39#define TI_ABB_FAST_OPP 1
40#define TI_ABB_SLOW_OPP 3
41
42/**
43 * struct ti_abb_info - ABB information per voltage setting
44 * @opp_sel: one of TI_ABB macro
45 * @vset: (optional) vset value that LDOVBB needs to be overriden with.
46 *
47 * Array of per voltage entries organized in the same order as regulator_desc's
48 * volt_table list. (selector is used to index from this array)
49 */
50struct ti_abb_info {
51 u32 opp_sel;
52 u32 vset;
53};
54
55/**
56 * struct ti_abb_reg - Register description for ABB block
57 * @setup_off: setup register offset from base
58 * @control_off: control register offset from base
59 * @sr2_wtcnt_value_mask: setup register- sr2_wtcnt_value mask
60 * @fbb_sel_mask: setup register- FBB sel mask
61 * @rbb_sel_mask: setup register- RBB sel mask
62 * @sr2_en_mask: setup register- enable mask
63 * @opp_change_mask: control register - mask to trigger LDOVBB change
64 * @opp_sel_mask: control register - mask for mode to operate
65 */
66struct ti_abb_reg {
67 u32 setup_off;
68 u32 control_off;
69
70 /* Setup register fields */
71 u32 sr2_wtcnt_value_mask;
72 u32 fbb_sel_mask;
73 u32 rbb_sel_mask;
74 u32 sr2_en_mask;
75
76 /* Control register fields */
77 u32 opp_change_mask;
78 u32 opp_sel_mask;
79};
80
81/**
82 * struct ti_abb - ABB instance data
83 * @rdesc: regulator descriptor
84 * @clk: clock(usually sysclk) supplying ABB block
85 * @base: base address of ABB block
86 * @setup_reg: setup register of ABB block
87 * @control_reg: control register of ABB block
88 * @int_base: interrupt register base address
89 * @efuse_base: (optional) efuse base address for ABB modes
90 * @ldo_base: (optional) LDOVBB vset override base address
91 * @regs: pointer to struct ti_abb_reg for ABB block
92 * @txdone_mask: mask on int_base for tranxdone interrupt
93 * @ldovbb_override_mask: mask to ldo_base for overriding default LDO VBB
94 * vset with value from efuse
95 * @ldovbb_vset_mask: mask to ldo_base for providing the VSET override
96 * @info: array to per voltage ABB configuration
97 * @current_info_idx: current index to info
98 * @settling_time: SoC specific settling time for LDO VBB
99 */
100struct ti_abb {
101 struct regulator_desc rdesc;
102 struct clk *clk;
103 void __iomem *base;
104 void __iomem *setup_reg;
105 void __iomem *control_reg;
106 void __iomem *int_base;
107 void __iomem *efuse_base;
108 void __iomem *ldo_base;
109
110 const struct ti_abb_reg *regs;
111 u32 txdone_mask;
112 u32 ldovbb_override_mask;
113 u32 ldovbb_vset_mask;
114
115 struct ti_abb_info *info;
116 int current_info_idx;
117
118 u32 settling_time;
119};
120
121/**
122 * ti_abb_rmw() - handy wrapper to set specific register bits
123 * @mask: mask for register field
124 * @value: value shifted to mask location and written
125 * @reg: register address
126 *
127 * Return: final register value (may be unused)
128 */
129static inline u32 ti_abb_rmw(u32 mask, u32 value, void __iomem *reg)
130{
131 u32 val;
132
133 val = readl(reg);
134 val &= ~mask;
135 val |= (value << __ffs(mask)) & mask;
136 writel(val, reg);
137
138 return val;
139}
140
141/**
142 * ti_abb_check_txdone() - handy wrapper to check ABB tranxdone status
143 * @abb: pointer to the abb instance
144 *
145 * Return: true or false
146 */
147static inline bool ti_abb_check_txdone(const struct ti_abb *abb)
148{
149 return !!(readl(abb->int_base) & abb->txdone_mask);
150}
151
152/**
153 * ti_abb_clear_txdone() - handy wrapper to clear ABB tranxdone status
154 * @abb: pointer to the abb instance
155 */
156static inline void ti_abb_clear_txdone(const struct ti_abb *abb)
157{
158 writel(abb->txdone_mask, abb->int_base);
159};
160
161/**
162 * ti_abb_wait_tranx() - waits for ABB tranxdone event
163 * @dev: device
164 * @abb: pointer to the abb instance
165 *
166 * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
167 */
168static int ti_abb_wait_txdone(struct device *dev, struct ti_abb *abb)
169{
170 int timeout = 0;
171 bool status;
172
173 while (timeout++ <= abb->settling_time) {
174 status = ti_abb_check_txdone(abb);
175 if (status)
176 return 0;
177
178 udelay(1);
179 }
180
181 dev_warn_ratelimited(dev, "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
182 __func__, timeout, readl(abb->int_base));
183 return -ETIMEDOUT;
184}
185
186/**
187 * ti_abb_clear_all_txdone() - clears ABB tranxdone event
188 * @dev: device
189 * @abb: pointer to the abb instance
190 *
191 * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
192 */
193static int ti_abb_clear_all_txdone(struct device *dev, const struct ti_abb *abb)
194{
195 int timeout = 0;
196 bool status;
197
198 while (timeout++ <= abb->settling_time) {
199 ti_abb_clear_txdone(abb);
200
201 status = ti_abb_check_txdone(abb);
202 if (!status)
203 return 0;
204
205 udelay(1);
206 }
207
208 dev_warn_ratelimited(dev, "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
209 __func__, timeout, readl(abb->int_base));
210 return -ETIMEDOUT;
211}
212
213/**
214 * ti_abb_program_ldovbb() - program LDOVBB register for override value
215 * @dev: device
216 * @abb: pointer to the abb instance
217 * @info: ABB info to program
218 */
219static void ti_abb_program_ldovbb(struct device *dev, const struct ti_abb *abb,
220 struct ti_abb_info *info)
221{
222 u32 val;
223
224 val = readl(abb->ldo_base);
225 /* clear up previous values */
226 val &= ~(abb->ldovbb_override_mask | abb->ldovbb_vset_mask);
227
228 switch (info->opp_sel) {
229 case TI_ABB_SLOW_OPP:
230 case TI_ABB_FAST_OPP:
231 val |= abb->ldovbb_override_mask;
232 val |= info->vset << __ffs(abb->ldovbb_vset_mask);
233 break;
234 }
235
236 writel(val, abb->ldo_base);
237}
238
239/**
240 * ti_abb_set_opp() - Setup ABB and LDO VBB for required bias
241 * @rdev: regulator device
242 * @abb: pointer to the abb instance
243 * @info: ABB info to program
244 *
245 * Return: 0 on success or appropriate error value when fails
246 */
247static int ti_abb_set_opp(struct regulator_dev *rdev, struct ti_abb *abb,
248 struct ti_abb_info *info)
249{
250 const struct ti_abb_reg *regs = abb->regs;
251 struct device *dev = &rdev->dev;
252 int ret;
253
254 ret = ti_abb_clear_all_txdone(dev, abb);
255 if (ret)
256 goto out;
257
258 ti_abb_rmw(regs->fbb_sel_mask | regs->rbb_sel_mask, 0, abb->setup_reg);
259
260 switch (info->opp_sel) {
261 case TI_ABB_SLOW_OPP:
262 ti_abb_rmw(regs->rbb_sel_mask, 1, abb->setup_reg);
263 break;
264 case TI_ABB_FAST_OPP:
265 ti_abb_rmw(regs->fbb_sel_mask, 1, abb->setup_reg);
266 break;
267 }
268
269 /* program next state of ABB ldo */
270 ti_abb_rmw(regs->opp_sel_mask, info->opp_sel, abb->control_reg);
271
272 /*
273 * program LDO VBB vset override if needed for !bypass mode
274 * XXX: Do not switch sequence - for !bypass, LDO override reset *must*
275 * be performed *before* switch to bias mode else VBB glitches.
276 */
277 if (abb->ldo_base && info->opp_sel != TI_ABB_NOMINAL_OPP)
278 ti_abb_program_ldovbb(dev, abb, info);
279
280 /* Initiate ABB ldo change */
281 ti_abb_rmw(regs->opp_change_mask, 1, abb->control_reg);
282
283 /* Wait for ABB LDO to complete transition to new Bias setting */
284 ret = ti_abb_wait_txdone(dev, abb);
285 if (ret)
286 goto out;
287
288 ret = ti_abb_clear_all_txdone(dev, abb);
289 if (ret)
290 goto out;
291
292 /*
293 * Reset LDO VBB vset override bypass mode
294 * XXX: Do not switch sequence - for bypass, LDO override reset *must*
295 * be performed *after* switch to bypass else VBB glitches.
296 */
297 if (abb->ldo_base && info->opp_sel == TI_ABB_NOMINAL_OPP)
298 ti_abb_program_ldovbb(dev, abb, info);
299
300out:
301 return ret;
302}
303
304/**
305 * ti_abb_set_voltage_sel() - regulator accessor function to set ABB LDO
306 * @rdev: regulator device
307 * @sel: selector to index into required ABB LDO settings (maps to
308 * regulator descriptor's volt_table)
309 *
310 * Return: 0 on success or appropriate error value when fails
311 */
312static int ti_abb_set_voltage_sel(struct regulator_dev *rdev, unsigned sel)
313{
314 const struct regulator_desc *desc = rdev->desc;
315 struct ti_abb *abb = rdev_get_drvdata(rdev);
316 struct device *dev = &rdev->dev;
317 struct ti_abb_info *info, *oinfo;
318 int ret = 0;
319
320 if (!abb) {
321 dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
322 __func__);
323 return -ENODEV;
324 }
325
326 if (!desc->n_voltages || !abb->info) {
327 dev_err_ratelimited(dev,
328 "%s: No valid voltage table entries?\n",
329 __func__);
330 return -EINVAL;
331 }
332
333 if (sel >= desc->n_voltages) {
334 dev_err(dev, "%s: sel idx(%d) >= n_voltages(%d)\n", __func__,
335 sel, desc->n_voltages);
336 return -EINVAL;
337 }
338
339 /* If we are in the same index as we were, nothing to do here! */
340 if (sel == abb->current_info_idx) {
341 dev_dbg(dev, "%s: Already at sel=%d\n", __func__, sel);
342 return ret;
343 }
344
345 /* If data is exactly the same, then just update index, no change */
346 info = &abb->info[sel];
347 oinfo = &abb->info[abb->current_info_idx];
348 if (!memcmp(info, oinfo, sizeof(*info))) {
349 dev_dbg(dev, "%s: Same data new idx=%d, old idx=%d\n", __func__,
350 sel, abb->current_info_idx);
351 goto out;
352 }
353
354 ret = ti_abb_set_opp(rdev, abb, info);
355
356out:
357 if (!ret)
358 abb->current_info_idx = sel;
359 else
360 dev_err_ratelimited(dev,
361 "%s: Volt[%d] idx[%d] mode[%d] Fail(%d)\n",
362 __func__, desc->volt_table[sel], sel,
363 info->opp_sel, ret);
364 return ret;
365}
366
367/**
368 * ti_abb_get_voltage_sel() - Regulator accessor to get current ABB LDO setting
369 * @rdev: regulator device
370 *
371 * Return: 0 on success or appropriate error value when fails
372 */
373static int ti_abb_get_voltage_sel(struct regulator_dev *rdev)
374{
375 const struct regulator_desc *desc = rdev->desc;
376 struct ti_abb *abb = rdev_get_drvdata(rdev);
377 struct device *dev = &rdev->dev;
378
379 if (!abb) {
380 dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
381 __func__);
382 return -ENODEV;
383 }
384
385 if (!desc->n_voltages || !abb->info) {
386 dev_err_ratelimited(dev,
387 "%s: No valid voltage table entries?\n",
388 __func__);
389 return -EINVAL;
390 }
391
392 if (abb->current_info_idx >= (int)desc->n_voltages) {
393 dev_err(dev, "%s: Corrupted data? idx(%d) >= n_voltages(%d)\n",
394 __func__, abb->current_info_idx, desc->n_voltages);
395 return -EINVAL;
396 }
397
398 return abb->current_info_idx;
399}
400
401/**
402 * ti_abb_init_timings() - setup ABB clock timing for the current platform
403 * @dev: device
404 * @abb: pointer to the abb instance
405 *
406 * Return: 0 if timing is updated, else returns error result.
407 */
408static int ti_abb_init_timings(struct device *dev, struct ti_abb *abb)
409{
410 u32 clock_cycles;
411 u32 clk_rate, sr2_wt_cnt_val, cycle_rate;
412 const struct ti_abb_reg *regs = abb->regs;
413 int ret;
414 char *pname = "ti,settling-time";
415
416 /* read device tree properties */
417 ret = of_property_read_u32(dev->of_node, pname, &abb->settling_time);
418 if (ret) {
419 dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
420 return ret;
421 }
422
423 /* ABB LDO cannot be settle in 0 time */
424 if (!abb->settling_time) {
425 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
426 return -EINVAL;
427 }
428
429 pname = "ti,clock-cycles";
430 ret = of_property_read_u32(dev->of_node, pname, &clock_cycles);
431 if (ret) {
432 dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
433 return ret;
434 }
435 /* ABB LDO cannot be settle in 0 clock cycles */
436 if (!clock_cycles) {
437 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
438 return -EINVAL;
439 }
440
441 abb->clk = devm_clk_get(dev, NULL);
442 if (IS_ERR(abb->clk)) {
443 ret = PTR_ERR(abb->clk);
444 dev_err(dev, "%s: Unable to get clk(%d)\n", __func__, ret);
445 return ret;
446 }
447
448 /*
449 * SR2_WTCNT_VALUE is the settling time for the ABB ldo after a
450 * transition and must be programmed with the correct time at boot.
451 * The value programmed into the register is the number of SYS_CLK
452 * clock cycles that match a given wall time profiled for the ldo.
453 * This value depends on:
454 * settling time of ldo in micro-seconds (varies per OMAP family)
455 * # of clock cycles per SYS_CLK period (varies per OMAP family)
456 * the SYS_CLK frequency in MHz (varies per board)
457 * The formula is:
458 *
459 * ldo settling time (in micro-seconds)
460 * SR2_WTCNT_VALUE = ------------------------------------------
461 * (# system clock cycles) * (sys_clk period)
462 *
463 * Put another way:
464 *
465 * SR2_WTCNT_VALUE = settling time / (# SYS_CLK cycles / SYS_CLK rate))
466 *
467 * To avoid dividing by zero multiply both "# clock cycles" and
468 * "settling time" by 10 such that the final result is the one we want.
469 */
470
471 /* Convert SYS_CLK rate to MHz & prevent divide by zero */
472 clk_rate = DIV_ROUND_CLOSEST(clk_get_rate(abb->clk), 1000000);
473
474 /* Calculate cycle rate */
475 cycle_rate = DIV_ROUND_CLOSEST(clock_cycles * 10, clk_rate);
476
477 /* Calulate SR2_WTCNT_VALUE */
478 sr2_wt_cnt_val = DIV_ROUND_CLOSEST(abb->settling_time * 10, cycle_rate);
479
480 dev_dbg(dev, "%s: Clk_rate=%ld, sr2_cnt=0x%08x\n", __func__,
481 clk_get_rate(abb->clk), sr2_wt_cnt_val);
482
483 ti_abb_rmw(regs->sr2_wtcnt_value_mask, sr2_wt_cnt_val, abb->setup_reg);
484
485 return 0;
486}
487
488/**
489 * ti_abb_init_table() - Initialize ABB table from device tree
490 * @dev: device
491 * @abb: pointer to the abb instance
492 * @rinit_data: regulator initdata
493 *
494 * Return: 0 on success or appropriate error value when fails
495 */
496static int ti_abb_init_table(struct device *dev, struct ti_abb *abb,
497 struct regulator_init_data *rinit_data)
498{
499 struct ti_abb_info *info;
500 const u32 num_values = 6;
501 char *pname = "ti,abb_info";
502 u32 i;
503 unsigned int *volt_table;
504 int num_entries, min_uV = INT_MAX, max_uV = 0;
505 struct regulation_constraints *c = &rinit_data->constraints;
506
507 /*
508 * Each abb_info is a set of n-tuple, where n is num_values, consisting
509 * of voltage and a set of detection logic for ABB information for that
510 * voltage to apply.
511 */
512 num_entries = of_property_count_u32_elems(dev->of_node, pname);
513 if (num_entries < 0) {
514 dev_err(dev, "No '%s' property?\n", pname);
515 return num_entries;
516 }
517
518 if (!num_entries || (num_entries % num_values)) {
519 dev_err(dev, "All '%s' list entries need %d vals\n", pname,
520 num_values);
521 return -EINVAL;
522 }
523 num_entries /= num_values;
524
525 info = devm_kcalloc(dev, num_entries, sizeof(*info), GFP_KERNEL);
526 if (!info)
527 return -ENOMEM;
528
529 abb->info = info;
530
531 volt_table = devm_kcalloc(dev, num_entries, sizeof(unsigned int),
532 GFP_KERNEL);
533 if (!volt_table)
534 return -ENOMEM;
535
536 abb->rdesc.n_voltages = num_entries;
537 abb->rdesc.volt_table = volt_table;
538 /* We do not know where the OPP voltage is at the moment */
539 abb->current_info_idx = -EINVAL;
540
541 for (i = 0; i < num_entries; i++, info++, volt_table++) {
542 u32 efuse_offset, rbb_mask, fbb_mask, vset_mask;
543 u32 efuse_val;
544
545 /* NOTE: num_values should equal to entries picked up here */
546 of_property_read_u32_index(dev->of_node, pname, i * num_values,
547 volt_table);
548 of_property_read_u32_index(dev->of_node, pname,
549 i * num_values + 1, &info->opp_sel);
550 of_property_read_u32_index(dev->of_node, pname,
551 i * num_values + 2, &efuse_offset);
552 of_property_read_u32_index(dev->of_node, pname,
553 i * num_values + 3, &rbb_mask);
554 of_property_read_u32_index(dev->of_node, pname,
555 i * num_values + 4, &fbb_mask);
556 of_property_read_u32_index(dev->of_node, pname,
557 i * num_values + 5, &vset_mask);
558
559 dev_dbg(dev,
560 "[%d]v=%d ABB=%d ef=0x%x rbb=0x%x fbb=0x%x vset=0x%x\n",
561 i, *volt_table, info->opp_sel, efuse_offset, rbb_mask,
562 fbb_mask, vset_mask);
563
564 /* Find min/max for voltage set */
565 if (min_uV > *volt_table)
566 min_uV = *volt_table;
567 if (max_uV < *volt_table)
568 max_uV = *volt_table;
569
570 if (!abb->efuse_base) {
571 /* Ignore invalid data, but warn to help cleanup */
572 if (efuse_offset || rbb_mask || fbb_mask || vset_mask)
573 dev_err(dev, "prop '%s': v=%d,bad efuse/mask\n",
574 pname, *volt_table);
575 goto check_abb;
576 }
577
578 efuse_val = readl(abb->efuse_base + efuse_offset);
579
580 /* Use ABB recommendation from Efuse */
581 if (efuse_val & rbb_mask)
582 info->opp_sel = TI_ABB_SLOW_OPP;
583 else if (efuse_val & fbb_mask)
584 info->opp_sel = TI_ABB_FAST_OPP;
585 else if (rbb_mask || fbb_mask)
586 info->opp_sel = TI_ABB_NOMINAL_OPP;
587
588 dev_dbg(dev,
589 "[%d]v=%d efusev=0x%x final ABB=%d\n",
590 i, *volt_table, efuse_val, info->opp_sel);
591
592 /* Use recommended Vset bits from Efuse */
593 if (!abb->ldo_base) {
594 if (vset_mask)
595 dev_err(dev, "prop'%s':v=%d vst=%x LDO base?\n",
596 pname, *volt_table, vset_mask);
597 continue;
598 }
599 info->vset = (efuse_val & vset_mask) >> __ffs(vset_mask);
600 dev_dbg(dev, "[%d]v=%d vset=%x\n", i, *volt_table, info->vset);
601check_abb:
602 switch (info->opp_sel) {
603 case TI_ABB_NOMINAL_OPP:
604 case TI_ABB_FAST_OPP:
605 case TI_ABB_SLOW_OPP:
606 /* Valid values */
607 break;
608 default:
609 dev_err(dev, "%s:[%d]v=%d, ABB=%d is invalid! Abort!\n",
610 __func__, i, *volt_table, info->opp_sel);
611 return -EINVAL;
612 }
613 }
614
615 /* Setup the min/max voltage constraints from the supported list */
616 c->min_uV = min_uV;
617 c->max_uV = max_uV;
618
619 return 0;
620}
621
622static struct regulator_ops ti_abb_reg_ops = {
623 .list_voltage = regulator_list_voltage_table,
624
625 .set_voltage_sel = ti_abb_set_voltage_sel,
626 .get_voltage_sel = ti_abb_get_voltage_sel,
627};
628
629/* Default ABB block offsets, IF this changes in future, create new one */
630static const struct ti_abb_reg abb_regs_v1 = {
631 /* WARNING: registers are wrongly documented in TRM */
632 .setup_off = 0x04,
633 .control_off = 0x00,
634
635 .sr2_wtcnt_value_mask = (0xff << 8),
636 .fbb_sel_mask = (0x01 << 2),
637 .rbb_sel_mask = (0x01 << 1),
638 .sr2_en_mask = (0x01 << 0),
639
640 .opp_change_mask = (0x01 << 2),
641 .opp_sel_mask = (0x03 << 0),
642};
643
644static const struct ti_abb_reg abb_regs_v2 = {
645 .setup_off = 0x00,
646 .control_off = 0x04,
647
648 .sr2_wtcnt_value_mask = (0xff << 8),
649 .fbb_sel_mask = (0x01 << 2),
650 .rbb_sel_mask = (0x01 << 1),
651 .sr2_en_mask = (0x01 << 0),
652
653 .opp_change_mask = (0x01 << 2),
654 .opp_sel_mask = (0x03 << 0),
655};
656
657static const struct ti_abb_reg abb_regs_generic = {
658 .sr2_wtcnt_value_mask = (0xff << 8),
659 .fbb_sel_mask = (0x01 << 2),
660 .rbb_sel_mask = (0x01 << 1),
661 .sr2_en_mask = (0x01 << 0),
662
663 .opp_change_mask = (0x01 << 2),
664 .opp_sel_mask = (0x03 << 0),
665};
666
667static const struct of_device_id ti_abb_of_match[] = {
668 {.compatible = "ti,abb-v1", .data = &abb_regs_v1},
669 {.compatible = "ti,abb-v2", .data = &abb_regs_v2},
670 {.compatible = "ti,abb-v3", .data = &abb_regs_generic},
671 { },
672};
673
674MODULE_DEVICE_TABLE(of, ti_abb_of_match);
675
676/**
677 * ti_abb_probe() - Initialize an ABB ldo instance
678 * @pdev: ABB platform device
679 *
680 * Initializes an individual ABB LDO for required Body-Bias. ABB is used to
681 * addional bias supply to SoC modules for power savings or mandatory stability
682 * configuration at certain Operating Performance Points(OPPs).
683 *
684 * Return: 0 on success or appropriate error value when fails
685 */
686static int ti_abb_probe(struct platform_device *pdev)
687{
688 struct device *dev = &pdev->dev;
689 const struct of_device_id *match;
690 struct resource *res;
691 struct ti_abb *abb;
692 struct regulator_init_data *initdata = NULL;
693 struct regulator_dev *rdev = NULL;
694 struct regulator_desc *desc;
695 struct regulation_constraints *c;
696 struct regulator_config config = { };
697 char *pname;
698 int ret = 0;
699
700 match = of_match_device(ti_abb_of_match, dev);
701 if (!match) {
702 /* We do not expect this to happen */
703 dev_err(dev, "%s: Unable to match device\n", __func__);
704 return -ENODEV;
705 }
706 if (!match->data) {
707 dev_err(dev, "%s: Bad data in match\n", __func__);
708 return -EINVAL;
709 }
710
711 abb = devm_kzalloc(dev, sizeof(struct ti_abb), GFP_KERNEL);
712 if (!abb)
713 return -ENOMEM;
714 abb->regs = match->data;
715
716 /* Map ABB resources */
717 if (abb->regs->setup_off || abb->regs->control_off) {
718 pname = "base-address";
719 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
720 abb->base = devm_ioremap_resource(dev, res);
721 if (IS_ERR(abb->base))
722 return PTR_ERR(abb->base);
723
724 abb->setup_reg = abb->base + abb->regs->setup_off;
725 abb->control_reg = abb->base + abb->regs->control_off;
726
727 } else {
728 pname = "control-address";
729 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
730 abb->control_reg = devm_ioremap_resource(dev, res);
731 if (IS_ERR(abb->control_reg))
732 return PTR_ERR(abb->control_reg);
733
734 pname = "setup-address";
735 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
736 abb->setup_reg = devm_ioremap_resource(dev, res);
737 if (IS_ERR(abb->setup_reg))
738 return PTR_ERR(abb->setup_reg);
739 }
740
741 pname = "int-address";
742 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
743 if (!res) {
744 dev_err(dev, "Missing '%s' IO resource\n", pname);
745 return -ENODEV;
746 }
747 /*
748 * We may have shared interrupt register offsets which are
749 * write-1-to-clear between domains ensuring exclusivity.
750 */
751 abb->int_base = devm_ioremap_nocache(dev, res->start,
752 resource_size(res));
753 if (!abb->int_base) {
754 dev_err(dev, "Unable to map '%s'\n", pname);
755 return -ENOMEM;
756 }
757
758 /* Map Optional resources */
759 pname = "efuse-address";
760 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
761 if (!res) {
762 dev_dbg(dev, "Missing '%s' IO resource\n", pname);
763 ret = -ENODEV;
764 goto skip_opt;
765 }
766
767 /*
768 * We may have shared efuse register offsets which are read-only
769 * between domains
770 */
771 abb->efuse_base = devm_ioremap_nocache(dev, res->start,
772 resource_size(res));
773 if (!abb->efuse_base) {
774 dev_err(dev, "Unable to map '%s'\n", pname);
775 return -ENOMEM;
776 }
777
778 pname = "ldo-address";
779 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
780 if (!res) {
781 dev_dbg(dev, "Missing '%s' IO resource\n", pname);
782 ret = -ENODEV;
783 goto skip_opt;
784 }
785 abb->ldo_base = devm_ioremap_resource(dev, res);
786 if (IS_ERR(abb->ldo_base))
787 return PTR_ERR(abb->ldo_base);
788
789 /* IF ldo_base is set, the following are mandatory */
790 pname = "ti,ldovbb-override-mask";
791 ret =
792 of_property_read_u32(pdev->dev.of_node, pname,
793 &abb->ldovbb_override_mask);
794 if (ret) {
795 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
796 return ret;
797 }
798 if (!abb->ldovbb_override_mask) {
799 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
800 return -EINVAL;
801 }
802
803 pname = "ti,ldovbb-vset-mask";
804 ret =
805 of_property_read_u32(pdev->dev.of_node, pname,
806 &abb->ldovbb_vset_mask);
807 if (ret) {
808 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
809 return ret;
810 }
811 if (!abb->ldovbb_vset_mask) {
812 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
813 return -EINVAL;
814 }
815
816skip_opt:
817 pname = "ti,tranxdone-status-mask";
818 ret =
819 of_property_read_u32(pdev->dev.of_node, pname,
820 &abb->txdone_mask);
821 if (ret) {
822 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
823 return ret;
824 }
825 if (!abb->txdone_mask) {
826 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
827 return -EINVAL;
828 }
829
830 initdata = of_get_regulator_init_data(dev, pdev->dev.of_node,
831 &abb->rdesc);
832 if (!initdata) {
833 dev_err(dev, "%s: Unable to alloc regulator init data\n",
834 __func__);
835 return -ENOMEM;
836 }
837
838 /* init ABB opp_sel table */
839 ret = ti_abb_init_table(dev, abb, initdata);
840 if (ret)
841 return ret;
842
843 /* init ABB timing */
844 ret = ti_abb_init_timings(dev, abb);
845 if (ret)
846 return ret;
847
848 desc = &abb->rdesc;
849 desc->name = dev_name(dev);
850 desc->owner = THIS_MODULE;
851 desc->type = REGULATOR_VOLTAGE;
852 desc->ops = &ti_abb_reg_ops;
853
854 c = &initdata->constraints;
855 if (desc->n_voltages > 1)
856 c->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
857 c->always_on = true;
858
859 config.dev = dev;
860 config.init_data = initdata;
861 config.driver_data = abb;
862 config.of_node = pdev->dev.of_node;
863
864 rdev = devm_regulator_register(dev, desc, &config);
865 if (IS_ERR(rdev)) {
866 ret = PTR_ERR(rdev);
867 dev_err(dev, "%s: failed to register regulator(%d)\n",
868 __func__, ret);
869 return ret;
870 }
871 platform_set_drvdata(pdev, rdev);
872
873 /* Enable the ldo if not already done by bootloader */
874 ti_abb_rmw(abb->regs->sr2_en_mask, 1, abb->setup_reg);
875
876 return 0;
877}
878
879MODULE_ALIAS("platform:ti_abb");
880
881static struct platform_driver ti_abb_driver = {
882 .probe = ti_abb_probe,
883 .driver = {
884 .name = "ti_abb",
885 .of_match_table = of_match_ptr(ti_abb_of_match),
886 },
887};
888module_platform_driver(ti_abb_driver);
889
890MODULE_DESCRIPTION("Texas Instruments ABB LDO regulator driver");
891MODULE_AUTHOR("Texas Instruments Inc.");
892MODULE_LICENSE("GPL v2");