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