]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/regulator/ti-abb-regulator.c
regulator: ti-abb-regulator: do not open-code counting and access of dt array elements
[mirror_ubuntu-zesty-kernel.git] / drivers / regulator / ti-abb-regulator.c
CommitLineData
40b1936e
AT
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
6127daa8
NM
57 * @setup_off: setup register offset from base
58 * @control_off: control register offset from base
40b1936e
AT
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 {
6127daa8
NM
67 u32 setup_off;
68 u32 control_off;
40b1936e
AT
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
6127daa8
NM
86 * @setup_reg: setup register of ABB block
87 * @control_reg: control register of ABB block
40b1936e
AT
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;
6127daa8
NM
104 void __iomem *setup_reg;
105 void __iomem *control_reg;
40b1936e
AT
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
6127daa8 125 * @reg: register address
40b1936e
AT
126 *
127 * Return: final register value (may be unused)
128 */
6127daa8 129static inline u32 ti_abb_rmw(u32 mask, u32 value, void __iomem *reg)
40b1936e
AT
130{
131 u32 val;
132
6127daa8 133 val = readl(reg);
40b1936e
AT
134 val &= ~mask;
135 val |= (value << __ffs(mask)) & mask;
6127daa8 136 writel(val, reg);
40b1936e
AT
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
6127daa8 268 ti_abb_rmw(regs->fbb_sel_mask | regs->rbb_sel_mask, 0, abb->setup_reg);
40b1936e
AT
269
270 switch (info->opp_sel) {
271 case TI_ABB_SLOW_OPP:
6127daa8 272 ti_abb_rmw(regs->rbb_sel_mask, 1, abb->setup_reg);
40b1936e
AT
273 break;
274 case TI_ABB_FAST_OPP:
6127daa8 275 ti_abb_rmw(regs->fbb_sel_mask, 1, abb->setup_reg);
40b1936e
AT
276 break;
277 }
278
279 /* program next state of ABB ldo */
6127daa8 280 ti_abb_rmw(regs->opp_sel_mask, info->opp_sel, abb->control_reg);
40b1936e 281
bf00ca35
NM
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)
40b1936e
AT
288 ti_abb_program_ldovbb(dev, abb, info);
289
290 /* Initiate ABB ldo change */
6127daa8 291 ti_abb_rmw(regs->opp_change_mask, 1, abb->control_reg);
40b1936e
AT
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
bf00ca35
NM
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
40b1936e
AT
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
f5cd8de2
AL
402 if (abb->current_info_idx >= (int)desc->n_voltages) {
403 dev_err(dev, "%s: Corrupted data? idx(%d) >= n_voltages(%d)\n",
40b1936e
AT
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
6127daa8 493 ti_abb_rmw(regs->sr2_wtcnt_value_mask, sr2_wt_cnt_val, abb->setup_reg);
40b1936e
AT
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;
40b1936e
AT
510 const u32 num_values = 6;
511 char *pname = "ti,abb_info";
5c24d355 512 u32 i;
40b1936e 513 unsigned int *volt_table;
5c24d355 514 int num_entries, min_uV = INT_MAX, max_uV = 0;
40b1936e
AT
515 struct regulation_constraints *c = &rinit_data->constraints;
516
40b1936e
AT
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 */
5c24d355
HS
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 -ENODEV;
526 }
527
40b1936e
AT
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 dev_err(dev, "Can't allocate info table for '%s' property\n",
538 pname);
539 return -ENOMEM;
540 }
541 abb->info = info;
542
543 volt_table = devm_kzalloc(dev, sizeof(unsigned int) * num_entries,
544 GFP_KERNEL);
545 if (!volt_table) {
546 dev_err(dev, "Can't allocate voltage table for '%s' property\n",
547 pname);
548 return -ENOMEM;
549 }
550
551 abb->rdesc.n_voltages = num_entries;
552 abb->rdesc.volt_table = volt_table;
553 /* We do not know where the OPP voltage is at the moment */
554 abb->current_info_idx = -EINVAL;
555
40b1936e
AT
556 for (i = 0; i < num_entries; i++, info++, volt_table++) {
557 u32 efuse_offset, rbb_mask, fbb_mask, vset_mask;
558 u32 efuse_val;
559
560 /* NOTE: num_values should equal to entries picked up here */
5c24d355
HS
561 of_property_read_u32_index(dev->of_node, pname, i * num_values,
562 volt_table);
563 of_property_read_u32_index(dev->of_node, pname,
564 i * num_values + 1, &info->opp_sel);
565 of_property_read_u32_index(dev->of_node, pname,
566 i * num_values + 2, &efuse_offset);
567 of_property_read_u32_index(dev->of_node, pname,
568 i * num_values + 3, &rbb_mask);
569 of_property_read_u32_index(dev->of_node, pname,
570 i * num_values + 4, &fbb_mask);
571 of_property_read_u32_index(dev->of_node, pname,
572 i * num_values + 5, &vset_mask);
40b1936e
AT
573
574 dev_dbg(dev,
575 "[%d]v=%d ABB=%d ef=0x%x rbb=0x%x fbb=0x%x vset=0x%x\n",
576 i, *volt_table, info->opp_sel, efuse_offset, rbb_mask,
577 fbb_mask, vset_mask);
578
579 /* Find min/max for voltage set */
580 if (min_uV > *volt_table)
581 min_uV = *volt_table;
582 if (max_uV < *volt_table)
583 max_uV = *volt_table;
584
585 if (!abb->efuse_base) {
586 /* Ignore invalid data, but warn to help cleanup */
587 if (efuse_offset || rbb_mask || fbb_mask || vset_mask)
588 dev_err(dev, "prop '%s': v=%d,bad efuse/mask\n",
589 pname, *volt_table);
590 goto check_abb;
591 }
592
593 efuse_val = readl(abb->efuse_base + efuse_offset);
594
595 /* Use ABB recommendation from Efuse */
596 if (efuse_val & rbb_mask)
597 info->opp_sel = TI_ABB_SLOW_OPP;
598 else if (efuse_val & fbb_mask)
599 info->opp_sel = TI_ABB_FAST_OPP;
600 else if (rbb_mask || fbb_mask)
601 info->opp_sel = TI_ABB_NOMINAL_OPP;
602
603 dev_dbg(dev,
604 "[%d]v=%d efusev=0x%x final ABB=%d\n",
605 i, *volt_table, efuse_val, info->opp_sel);
606
607 /* Use recommended Vset bits from Efuse */
608 if (!abb->ldo_base) {
609 if (vset_mask)
610 dev_err(dev, "prop'%s':v=%d vst=%x LDO base?\n",
611 pname, *volt_table, vset_mask);
612 continue;
613 }
9a633a2b 614 info->vset = (efuse_val & vset_mask) >> __ffs(vset_mask);
40b1936e
AT
615 dev_dbg(dev, "[%d]v=%d vset=%x\n", i, *volt_table, info->vset);
616check_abb:
617 switch (info->opp_sel) {
618 case TI_ABB_NOMINAL_OPP:
619 case TI_ABB_FAST_OPP:
620 case TI_ABB_SLOW_OPP:
621 /* Valid values */
622 break;
623 default:
624 dev_err(dev, "%s:[%d]v=%d, ABB=%d is invalid! Abort!\n",
625 __func__, i, *volt_table, info->opp_sel);
626 return -EINVAL;
627 }
628 }
629
630 /* Setup the min/max voltage constraints from the supported list */
631 c->min_uV = min_uV;
632 c->max_uV = max_uV;
633
634 return 0;
635}
636
637static struct regulator_ops ti_abb_reg_ops = {
638 .list_voltage = regulator_list_voltage_table,
639
640 .set_voltage_sel = ti_abb_set_voltage_sel,
641 .get_voltage_sel = ti_abb_get_voltage_sel,
642};
643
644/* Default ABB block offsets, IF this changes in future, create new one */
645static const struct ti_abb_reg abb_regs_v1 = {
646 /* WARNING: registers are wrongly documented in TRM */
6127daa8
NM
647 .setup_off = 0x04,
648 .control_off = 0x00,
40b1936e
AT
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_v2 = {
6127daa8
NM
660 .setup_off = 0x00,
661 .control_off = 0x04,
40b1936e
AT
662
663 .sr2_wtcnt_value_mask = (0xff << 8),
664 .fbb_sel_mask = (0x01 << 2),
665 .rbb_sel_mask = (0x01 << 1),
666 .sr2_en_mask = (0x01 << 0),
667
668 .opp_change_mask = (0x01 << 2),
669 .opp_sel_mask = (0x03 << 0),
670};
671
6127daa8
NM
672static const struct ti_abb_reg abb_regs_generic = {
673 .sr2_wtcnt_value_mask = (0xff << 8),
674 .fbb_sel_mask = (0x01 << 2),
675 .rbb_sel_mask = (0x01 << 1),
676 .sr2_en_mask = (0x01 << 0),
677
678 .opp_change_mask = (0x01 << 2),
679 .opp_sel_mask = (0x03 << 0),
680};
681
40b1936e
AT
682static const struct of_device_id ti_abb_of_match[] = {
683 {.compatible = "ti,abb-v1", .data = &abb_regs_v1},
684 {.compatible = "ti,abb-v2", .data = &abb_regs_v2},
6127daa8 685 {.compatible = "ti,abb-v3", .data = &abb_regs_generic},
40b1936e
AT
686 { },
687};
688
689MODULE_DEVICE_TABLE(of, ti_abb_of_match);
690
691/**
692 * ti_abb_probe() - Initialize an ABB ldo instance
693 * @pdev: ABB platform device
694 *
695 * Initializes an individual ABB LDO for required Body-Bias. ABB is used to
696 * addional bias supply to SoC modules for power savings or mandatory stability
697 * configuration at certain Operating Performance Points(OPPs).
698 *
699 * Return: 0 on success or appropriate error value when fails
700 */
701static int ti_abb_probe(struct platform_device *pdev)
702{
703 struct device *dev = &pdev->dev;
704 const struct of_device_id *match;
705 struct resource *res;
706 struct ti_abb *abb;
707 struct regulator_init_data *initdata = NULL;
708 struct regulator_dev *rdev = NULL;
709 struct regulator_desc *desc;
710 struct regulation_constraints *c;
711 struct regulator_config config = { };
712 char *pname;
713 int ret = 0;
714
715 match = of_match_device(ti_abb_of_match, dev);
716 if (!match) {
717 /* We do not expect this to happen */
40b1936e 718 dev_err(dev, "%s: Unable to match device\n", __func__);
91dfc80d 719 return -ENODEV;
40b1936e
AT
720 }
721 if (!match->data) {
40b1936e 722 dev_err(dev, "%s: Bad data in match\n", __func__);
91dfc80d 723 return -EINVAL;
40b1936e
AT
724 }
725
726 abb = devm_kzalloc(dev, sizeof(struct ti_abb), GFP_KERNEL);
91dfc80d
SK
727 if (!abb)
728 return -ENOMEM;
40b1936e
AT
729 abb->regs = match->data;
730
731 /* Map ABB resources */
6127daa8
NM
732 if (abb->regs->setup_off || abb->regs->control_off) {
733 pname = "base-address";
734 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
735 abb->base = devm_ioremap_resource(dev, res);
736 if (IS_ERR(abb->base))
737 return PTR_ERR(abb->base);
738
739 abb->setup_reg = abb->base + abb->regs->setup_off;
740 abb->control_reg = abb->base + abb->regs->control_off;
741
742 } else {
743 pname = "control-address";
744 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
745 abb->control_reg = devm_ioremap_resource(dev, res);
746 if (IS_ERR(abb->control_reg))
747 return PTR_ERR(abb->control_reg);
748
749 pname = "setup-address";
750 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
751 abb->setup_reg = devm_ioremap_resource(dev, res);
752 if (IS_ERR(abb->setup_reg))
753 return PTR_ERR(abb->setup_reg);
754 }
40b1936e
AT
755
756 pname = "int-address";
757 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
758 if (!res) {
759 dev_err(dev, "Missing '%s' IO resource\n", pname);
91dfc80d 760 return -ENODEV;
40b1936e
AT
761 }
762 /*
763 * We may have shared interrupt register offsets which are
764 * write-1-to-clear between domains ensuring exclusivity.
765 */
766 abb->int_base = devm_ioremap_nocache(dev, res->start,
767 resource_size(res));
768 if (!abb->int_base) {
769 dev_err(dev, "Unable to map '%s'\n", pname);
91dfc80d 770 return -ENOMEM;
40b1936e
AT
771 }
772
773 /* Map Optional resources */
774 pname = "efuse-address";
775 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
776 if (!res) {
777 dev_dbg(dev, "Missing '%s' IO resource\n", pname);
778 ret = -ENODEV;
779 goto skip_opt;
780 }
781
782 /*
783 * We may have shared efuse register offsets which are read-only
784 * between domains
785 */
786 abb->efuse_base = devm_ioremap_nocache(dev, res->start,
787 resource_size(res));
788 if (!abb->efuse_base) {
789 dev_err(dev, "Unable to map '%s'\n", pname);
91dfc80d 790 return -ENOMEM;
40b1936e
AT
791 }
792
793 pname = "ldo-address";
794 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
bde251a9
NM
795 if (!res) {
796 dev_dbg(dev, "Missing '%s' IO resource\n", pname);
797 ret = -ENODEV;
798 goto skip_opt;
799 }
d26ec830 800 abb->ldo_base = devm_ioremap_resource(dev, res);
91dfc80d
SK
801 if (IS_ERR(abb->ldo_base))
802 return PTR_ERR(abb->ldo_base);
40b1936e
AT
803
804 /* IF ldo_base is set, the following are mandatory */
805 pname = "ti,ldovbb-override-mask";
806 ret =
807 of_property_read_u32(pdev->dev.of_node, pname,
808 &abb->ldovbb_override_mask);
809 if (ret) {
810 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
91dfc80d 811 return ret;
40b1936e
AT
812 }
813 if (!abb->ldovbb_override_mask) {
814 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
91dfc80d 815 return -EINVAL;
40b1936e
AT
816 }
817
818 pname = "ti,ldovbb-vset-mask";
819 ret =
820 of_property_read_u32(pdev->dev.of_node, pname,
821 &abb->ldovbb_vset_mask);
822 if (ret) {
823 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
91dfc80d 824 return ret;
40b1936e
AT
825 }
826 if (!abb->ldovbb_vset_mask) {
827 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
91dfc80d 828 return -EINVAL;
40b1936e
AT
829 }
830
831skip_opt:
832 pname = "ti,tranxdone-status-mask";
833 ret =
834 of_property_read_u32(pdev->dev.of_node, pname,
835 &abb->txdone_mask);
836 if (ret) {
837 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
91dfc80d 838 return ret;
40b1936e
AT
839 }
840 if (!abb->txdone_mask) {
841 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
91dfc80d 842 return -EINVAL;
40b1936e
AT
843 }
844
845 initdata = of_get_regulator_init_data(dev, pdev->dev.of_node);
846 if (!initdata) {
40b1936e
AT
847 dev_err(dev, "%s: Unable to alloc regulator init data\n",
848 __func__);
91dfc80d 849 return -ENOMEM;
40b1936e
AT
850 }
851
852 /* init ABB opp_sel table */
853 ret = ti_abb_init_table(dev, abb, initdata);
854 if (ret)
91dfc80d 855 return ret;
40b1936e
AT
856
857 /* init ABB timing */
858 ret = ti_abb_init_timings(dev, abb);
859 if (ret)
91dfc80d 860 return ret;
40b1936e
AT
861
862 desc = &abb->rdesc;
863 desc->name = dev_name(dev);
864 desc->owner = THIS_MODULE;
865 desc->type = REGULATOR_VOLTAGE;
866 desc->ops = &ti_abb_reg_ops;
867
868 c = &initdata->constraints;
869 if (desc->n_voltages > 1)
870 c->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
871 c->always_on = true;
872
873 config.dev = dev;
874 config.init_data = initdata;
875 config.driver_data = abb;
876 config.of_node = pdev->dev.of_node;
877
91dfc80d 878 rdev = devm_regulator_register(dev, desc, &config);
40b1936e
AT
879 if (IS_ERR(rdev)) {
880 ret = PTR_ERR(rdev);
881 dev_err(dev, "%s: failed to register regulator(%d)\n",
882 __func__, ret);
91dfc80d 883 return ret;
40b1936e
AT
884 }
885 platform_set_drvdata(pdev, rdev);
886
887 /* Enable the ldo if not already done by bootloader */
6127daa8 888 ti_abb_rmw(abb->regs->sr2_en_mask, 1, abb->setup_reg);
40b1936e
AT
889
890 return 0;
40b1936e
AT
891}
892
893MODULE_ALIAS("platform:ti_abb");
894
895static struct platform_driver ti_abb_driver = {
896 .probe = ti_abb_probe,
40b1936e
AT
897 .driver = {
898 .name = "ti_abb",
899 .owner = THIS_MODULE,
900 .of_match_table = of_match_ptr(ti_abb_of_match),
901 },
902};
903module_platform_driver(ti_abb_driver);
904
905MODULE_DESCRIPTION("Texas Instruments ABB LDO regulator driver");
906MODULE_AUTHOR("Texas Instruments Inc.");
907MODULE_LICENSE("GPL v2");