libertas sdio: claim device before calling sdio_disable_func()
[linux-2.6/btrfs-unstable.git] / drivers / regulator / ti-abb-regulator.c
blobb187b6bba7ad485a9a8d97ba248b0a70de89c5c8
1 /*
2 * Texas Instruments SoC Adaptive Body Bias(ABB) Regulator
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Mike Turquette <mturquette@ti.com>
7 * Copyright (C) 2012-2013 Texas Instruments, Inc.
8 * Andrii Tseglytskyi <andrii.tseglytskyi@ti.com>
9 * Nishanth Menon <nm@ti.com>
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.
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.
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>
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
38 #define TI_ABB_NOMINAL_OPP 0
39 #define TI_ABB_FAST_OPP 1
40 #define TI_ABB_SLOW_OPP 3
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.
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)
50 struct ti_abb_info {
51 u32 opp_sel;
52 u32 vset;
55 /**
56 * struct ti_abb_reg - Register description for ABB block
57 * @setup_reg: setup register offset from base
58 * @control_reg: 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
66 struct ti_abb_reg {
67 u32 setup_reg;
68 u32 control_reg;
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;
76 /* Control register fields */
77 u32 opp_change_mask;
78 u32 opp_sel_mask;
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 * @int_base: interrupt register base address
87 * @efuse_base: (optional) efuse base address for ABB modes
88 * @ldo_base: (optional) LDOVBB vset override base address
89 * @regs: pointer to struct ti_abb_reg for ABB block
90 * @txdone_mask: mask on int_base for tranxdone interrupt
91 * @ldovbb_override_mask: mask to ldo_base for overriding default LDO VBB
92 * vset with value from efuse
93 * @ldovbb_vset_mask: mask to ldo_base for providing the VSET override
94 * @info: array to per voltage ABB configuration
95 * @current_info_idx: current index to info
96 * @settling_time: SoC specific settling time for LDO VBB
98 struct ti_abb {
99 struct regulator_desc rdesc;
100 struct clk *clk;
101 void __iomem *base;
102 void __iomem *int_base;
103 void __iomem *efuse_base;
104 void __iomem *ldo_base;
106 const struct ti_abb_reg *regs;
107 u32 txdone_mask;
108 u32 ldovbb_override_mask;
109 u32 ldovbb_vset_mask;
111 struct ti_abb_info *info;
112 int current_info_idx;
114 u32 settling_time;
118 * ti_abb_rmw() - handy wrapper to set specific register bits
119 * @mask: mask for register field
120 * @value: value shifted to mask location and written
121 * @offset: offset of register
122 * @base: base address
124 * Return: final register value (may be unused)
126 static inline u32 ti_abb_rmw(u32 mask, u32 value, u32 offset,
127 void __iomem *base)
129 u32 val;
131 val = readl(base + offset);
132 val &= ~mask;
133 val |= (value << __ffs(mask)) & mask;
134 writel(val, base + offset);
136 return val;
140 * ti_abb_check_txdone() - handy wrapper to check ABB tranxdone status
141 * @abb: pointer to the abb instance
143 * Return: true or false
145 static inline bool ti_abb_check_txdone(const struct ti_abb *abb)
147 return !!(readl(abb->int_base) & abb->txdone_mask);
151 * ti_abb_clear_txdone() - handy wrapper to clear ABB tranxdone status
152 * @abb: pointer to the abb instance
154 static inline void ti_abb_clear_txdone(const struct ti_abb *abb)
156 writel(abb->txdone_mask, abb->int_base);
160 * ti_abb_wait_tranx() - waits for ABB tranxdone event
161 * @dev: device
162 * @abb: pointer to the abb instance
164 * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
166 static int ti_abb_wait_txdone(struct device *dev, struct ti_abb *abb)
168 int timeout = 0;
169 bool status;
171 while (timeout++ <= abb->settling_time) {
172 status = ti_abb_check_txdone(abb);
173 if (status)
174 break;
176 udelay(1);
179 if (timeout > abb->settling_time) {
180 dev_warn_ratelimited(dev,
181 "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
182 __func__, timeout, readl(abb->int_base));
183 return -ETIMEDOUT;
186 return 0;
190 * ti_abb_clear_all_txdone() - clears ABB tranxdone event
191 * @dev: device
192 * @abb: pointer to the abb instance
194 * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
196 static int ti_abb_clear_all_txdone(struct device *dev, const struct ti_abb *abb)
198 int timeout = 0;
199 bool status;
201 while (timeout++ <= abb->settling_time) {
202 ti_abb_clear_txdone(abb);
204 status = ti_abb_check_txdone(abb);
205 if (!status)
206 break;
208 udelay(1);
211 if (timeout > abb->settling_time) {
212 dev_warn_ratelimited(dev,
213 "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
214 __func__, timeout, readl(abb->int_base));
215 return -ETIMEDOUT;
218 return 0;
222 * ti_abb_program_ldovbb() - program LDOVBB register for override value
223 * @dev: device
224 * @abb: pointer to the abb instance
225 * @info: ABB info to program
227 static void ti_abb_program_ldovbb(struct device *dev, const struct ti_abb *abb,
228 struct ti_abb_info *info)
230 u32 val;
232 val = readl(abb->ldo_base);
233 /* clear up previous values */
234 val &= ~(abb->ldovbb_override_mask | abb->ldovbb_vset_mask);
236 switch (info->opp_sel) {
237 case TI_ABB_SLOW_OPP:
238 case TI_ABB_FAST_OPP:
239 val |= abb->ldovbb_override_mask;
240 val |= info->vset << __ffs(abb->ldovbb_vset_mask);
241 break;
244 writel(val, abb->ldo_base);
248 * ti_abb_set_opp() - Setup ABB and LDO VBB for required bias
249 * @rdev: regulator device
250 * @abb: pointer to the abb instance
251 * @info: ABB info to program
253 * Return: 0 on success or appropriate error value when fails
255 static int ti_abb_set_opp(struct regulator_dev *rdev, struct ti_abb *abb,
256 struct ti_abb_info *info)
258 const struct ti_abb_reg *regs = abb->regs;
259 struct device *dev = &rdev->dev;
260 int ret;
262 ret = ti_abb_clear_all_txdone(dev, abb);
263 if (ret)
264 goto out;
266 ti_abb_rmw(regs->fbb_sel_mask | regs->rbb_sel_mask, 0, regs->setup_reg,
267 abb->base);
269 switch (info->opp_sel) {
270 case TI_ABB_SLOW_OPP:
271 ti_abb_rmw(regs->rbb_sel_mask, 1, regs->setup_reg, abb->base);
272 break;
273 case TI_ABB_FAST_OPP:
274 ti_abb_rmw(regs->fbb_sel_mask, 1, regs->setup_reg, abb->base);
275 break;
278 /* program next state of ABB ldo */
279 ti_abb_rmw(regs->opp_sel_mask, info->opp_sel, regs->control_reg,
280 abb->base);
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.
287 if (abb->ldo_base && info->opp_sel != TI_ABB_NOMINAL_OPP)
288 ti_abb_program_ldovbb(dev, abb, info);
290 /* Initiate ABB ldo change */
291 ti_abb_rmw(regs->opp_change_mask, 1, regs->control_reg, abb->base);
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;
298 ret = ti_abb_clear_all_txdone(dev, abb);
299 if (ret)
300 goto out;
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.
307 if (abb->ldo_base && info->opp_sel == TI_ABB_NOMINAL_OPP)
308 ti_abb_program_ldovbb(dev, abb, info);
310 out:
311 return ret;
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)
320 * Return: 0 on success or appropriate error value when fails
322 static int ti_abb_set_voltage_sel(struct regulator_dev *rdev, unsigned sel)
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;
330 if (!abb) {
331 dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
332 __func__);
333 return -ENODEV;
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;
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;
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;
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;
364 ret = ti_abb_set_opp(rdev, abb, info);
366 out:
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;
378 * ti_abb_get_voltage_sel() - Regulator accessor to get current ABB LDO setting
379 * @rdev: regulator device
381 * Return: 0 on success or appropriate error value when fails
383 static int ti_abb_get_voltage_sel(struct regulator_dev *rdev)
385 const struct regulator_desc *desc = rdev->desc;
386 struct ti_abb *abb = rdev_get_drvdata(rdev);
387 struct device *dev = &rdev->dev;
389 if (!abb) {
390 dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
391 __func__);
392 return -ENODEV;
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;
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;
408 return abb->current_info_idx;
412 * ti_abb_init_timings() - setup ABB clock timing for the current platform
413 * @dev: device
414 * @abb: pointer to the abb instance
416 * Return: 0 if timing is updated, else returns error result.
418 static int ti_abb_init_timings(struct device *dev, struct ti_abb *abb)
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";
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;
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;
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;
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;
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;
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:
469 * ldo settling time (in micro-seconds)
470 * SR2_WTCNT_VALUE = ------------------------------------------
471 * (# system clock cycles) * (sys_clk period)
473 * Put another way:
475 * SR2_WTCNT_VALUE = settling time / (# SYS_CLK cycles / SYS_CLK rate))
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.
481 /* Convert SYS_CLK rate to MHz & prevent divide by zero */
482 clk_rate = DIV_ROUND_CLOSEST(clk_get_rate(abb->clk), 1000000);
484 /* Calculate cycle rate */
485 cycle_rate = DIV_ROUND_CLOSEST(clock_cycles * 10, clk_rate);
487 /* Calulate SR2_WTCNT_VALUE */
488 sr2_wt_cnt_val = DIV_ROUND_CLOSEST(abb->settling_time * 10, cycle_rate);
490 dev_dbg(dev, "%s: Clk_rate=%ld, sr2_cnt=0x%08x\n", __func__,
491 clk_get_rate(abb->clk), sr2_wt_cnt_val);
493 ti_abb_rmw(regs->sr2_wtcnt_value_mask, sr2_wt_cnt_val, regs->setup_reg,
494 abb->base);
496 return 0;
500 * ti_abb_init_table() - Initialize ABB table from device tree
501 * @dev: device
502 * @abb: pointer to the abb instance
503 * @rinit_data: regulator initdata
505 * Return: 0 on success or appropriate error value when fails
507 static int ti_abb_init_table(struct device *dev, struct ti_abb *abb,
508 struct regulator_init_data *rinit_data)
510 struct ti_abb_info *info;
511 const struct property *prop;
512 const __be32 *abb_info;
513 const u32 num_values = 6;
514 char *pname = "ti,abb_info";
515 u32 num_entries, i;
516 unsigned int *volt_table;
517 int min_uV = INT_MAX, max_uV = 0;
518 struct regulation_constraints *c = &rinit_data->constraints;
520 prop = of_find_property(dev->of_node, pname, NULL);
521 if (!prop) {
522 dev_err(dev, "No '%s' property?\n", pname);
523 return -ENODEV;
526 if (!prop->value) {
527 dev_err(dev, "Empty '%s' property?\n", pname);
528 return -ENODATA;
532 * Each abb_info is a set of n-tuple, where n is num_values, consisting
533 * of voltage and a set of detection logic for ABB information for that
534 * voltage to apply.
536 num_entries = prop->length / sizeof(u32);
537 if (!num_entries || (num_entries % num_values)) {
538 dev_err(dev, "All '%s' list entries need %d vals\n", pname,
539 num_values);
540 return -EINVAL;
542 num_entries /= num_values;
544 info = devm_kzalloc(dev, sizeof(*info) * num_entries, GFP_KERNEL);
545 if (!info) {
546 dev_err(dev, "Can't allocate info table for '%s' property\n",
547 pname);
548 return -ENOMEM;
550 abb->info = info;
552 volt_table = devm_kzalloc(dev, sizeof(unsigned int) * num_entries,
553 GFP_KERNEL);
554 if (!volt_table) {
555 dev_err(dev, "Can't allocate voltage table for '%s' property\n",
556 pname);
557 return -ENOMEM;
560 abb->rdesc.n_voltages = num_entries;
561 abb->rdesc.volt_table = volt_table;
562 /* We do not know where the OPP voltage is at the moment */
563 abb->current_info_idx = -EINVAL;
565 abb_info = prop->value;
566 for (i = 0; i < num_entries; i++, info++, volt_table++) {
567 u32 efuse_offset, rbb_mask, fbb_mask, vset_mask;
568 u32 efuse_val;
570 /* NOTE: num_values should equal to entries picked up here */
571 *volt_table = be32_to_cpup(abb_info++);
572 info->opp_sel = be32_to_cpup(abb_info++);
573 efuse_offset = be32_to_cpup(abb_info++);
574 rbb_mask = be32_to_cpup(abb_info++);
575 fbb_mask = be32_to_cpup(abb_info++);
576 vset_mask = be32_to_cpup(abb_info++);
578 dev_dbg(dev,
579 "[%d]v=%d ABB=%d ef=0x%x rbb=0x%x fbb=0x%x vset=0x%x\n",
580 i, *volt_table, info->opp_sel, efuse_offset, rbb_mask,
581 fbb_mask, vset_mask);
583 /* Find min/max for voltage set */
584 if (min_uV > *volt_table)
585 min_uV = *volt_table;
586 if (max_uV < *volt_table)
587 max_uV = *volt_table;
589 if (!abb->efuse_base) {
590 /* Ignore invalid data, but warn to help cleanup */
591 if (efuse_offset || rbb_mask || fbb_mask || vset_mask)
592 dev_err(dev, "prop '%s': v=%d,bad efuse/mask\n",
593 pname, *volt_table);
594 goto check_abb;
597 efuse_val = readl(abb->efuse_base + efuse_offset);
599 /* Use ABB recommendation from Efuse */
600 if (efuse_val & rbb_mask)
601 info->opp_sel = TI_ABB_SLOW_OPP;
602 else if (efuse_val & fbb_mask)
603 info->opp_sel = TI_ABB_FAST_OPP;
604 else if (rbb_mask || fbb_mask)
605 info->opp_sel = TI_ABB_NOMINAL_OPP;
607 dev_dbg(dev,
608 "[%d]v=%d efusev=0x%x final ABB=%d\n",
609 i, *volt_table, efuse_val, info->opp_sel);
611 /* Use recommended Vset bits from Efuse */
612 if (!abb->ldo_base) {
613 if (vset_mask)
614 dev_err(dev, "prop'%s':v=%d vst=%x LDO base?\n",
615 pname, *volt_table, vset_mask);
616 continue;
618 info->vset = (efuse_val & vset_mask) >> __ffs(vset_mask);
619 dev_dbg(dev, "[%d]v=%d vset=%x\n", i, *volt_table, info->vset);
620 check_abb:
621 switch (info->opp_sel) {
622 case TI_ABB_NOMINAL_OPP:
623 case TI_ABB_FAST_OPP:
624 case TI_ABB_SLOW_OPP:
625 /* Valid values */
626 break;
627 default:
628 dev_err(dev, "%s:[%d]v=%d, ABB=%d is invalid! Abort!\n",
629 __func__, i, *volt_table, info->opp_sel);
630 return -EINVAL;
634 /* Setup the min/max voltage constraints from the supported list */
635 c->min_uV = min_uV;
636 c->max_uV = max_uV;
638 return 0;
641 static struct regulator_ops ti_abb_reg_ops = {
642 .list_voltage = regulator_list_voltage_table,
644 .set_voltage_sel = ti_abb_set_voltage_sel,
645 .get_voltage_sel = ti_abb_get_voltage_sel,
648 /* Default ABB block offsets, IF this changes in future, create new one */
649 static const struct ti_abb_reg abb_regs_v1 = {
650 /* WARNING: registers are wrongly documented in TRM */
651 .setup_reg = 0x04,
652 .control_reg = 0x00,
654 .sr2_wtcnt_value_mask = (0xff << 8),
655 .fbb_sel_mask = (0x01 << 2),
656 .rbb_sel_mask = (0x01 << 1),
657 .sr2_en_mask = (0x01 << 0),
659 .opp_change_mask = (0x01 << 2),
660 .opp_sel_mask = (0x03 << 0),
663 static const struct ti_abb_reg abb_regs_v2 = {
664 .setup_reg = 0x00,
665 .control_reg = 0x04,
667 .sr2_wtcnt_value_mask = (0xff << 8),
668 .fbb_sel_mask = (0x01 << 2),
669 .rbb_sel_mask = (0x01 << 1),
670 .sr2_en_mask = (0x01 << 0),
672 .opp_change_mask = (0x01 << 2),
673 .opp_sel_mask = (0x03 << 0),
676 static const struct of_device_id ti_abb_of_match[] = {
677 {.compatible = "ti,abb-v1", .data = &abb_regs_v1},
678 {.compatible = "ti,abb-v2", .data = &abb_regs_v2},
679 { },
682 MODULE_DEVICE_TABLE(of, ti_abb_of_match);
685 * ti_abb_probe() - Initialize an ABB ldo instance
686 * @pdev: ABB platform device
688 * Initializes an individual ABB LDO for required Body-Bias. ABB is used to
689 * addional bias supply to SoC modules for power savings or mandatory stability
690 * configuration at certain Operating Performance Points(OPPs).
692 * Return: 0 on success or appropriate error value when fails
694 static int ti_abb_probe(struct platform_device *pdev)
696 struct device *dev = &pdev->dev;
697 const struct of_device_id *match;
698 struct resource *res;
699 struct ti_abb *abb;
700 struct regulator_init_data *initdata = NULL;
701 struct regulator_dev *rdev = NULL;
702 struct regulator_desc *desc;
703 struct regulation_constraints *c;
704 struct regulator_config config = { };
705 char *pname;
706 int ret = 0;
708 match = of_match_device(ti_abb_of_match, dev);
709 if (!match) {
710 /* We do not expect this to happen */
711 dev_err(dev, "%s: Unable to match device\n", __func__);
712 return -ENODEV;
714 if (!match->data) {
715 dev_err(dev, "%s: Bad data in match\n", __func__);
716 return -EINVAL;
719 abb = devm_kzalloc(dev, sizeof(struct ti_abb), GFP_KERNEL);
720 if (!abb)
721 return -ENOMEM;
722 abb->regs = match->data;
724 /* Map ABB resources */
725 pname = "base-address";
726 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
727 abb->base = devm_ioremap_resource(dev, res);
728 if (IS_ERR(abb->base))
729 return PTR_ERR(abb->base);
731 pname = "int-address";
732 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
733 if (!res) {
734 dev_err(dev, "Missing '%s' IO resource\n", pname);
735 return -ENODEV;
738 * We may have shared interrupt register offsets which are
739 * write-1-to-clear between domains ensuring exclusivity.
741 abb->int_base = devm_ioremap_nocache(dev, res->start,
742 resource_size(res));
743 if (!abb->int_base) {
744 dev_err(dev, "Unable to map '%s'\n", pname);
745 return -ENOMEM;
748 /* Map Optional resources */
749 pname = "efuse-address";
750 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
751 if (!res) {
752 dev_dbg(dev, "Missing '%s' IO resource\n", pname);
753 ret = -ENODEV;
754 goto skip_opt;
758 * We may have shared efuse register offsets which are read-only
759 * between domains
761 abb->efuse_base = devm_ioremap_nocache(dev, res->start,
762 resource_size(res));
763 if (!abb->efuse_base) {
764 dev_err(dev, "Unable to map '%s'\n", pname);
765 return -ENOMEM;
768 pname = "ldo-address";
769 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
770 if (!res) {
771 dev_dbg(dev, "Missing '%s' IO resource\n", pname);
772 ret = -ENODEV;
773 goto skip_opt;
775 abb->ldo_base = devm_ioremap_resource(dev, res);
776 if (IS_ERR(abb->ldo_base))
777 return PTR_ERR(abb->ldo_base);
779 /* IF ldo_base is set, the following are mandatory */
780 pname = "ti,ldovbb-override-mask";
781 ret =
782 of_property_read_u32(pdev->dev.of_node, pname,
783 &abb->ldovbb_override_mask);
784 if (ret) {
785 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
786 return ret;
788 if (!abb->ldovbb_override_mask) {
789 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
790 return -EINVAL;
793 pname = "ti,ldovbb-vset-mask";
794 ret =
795 of_property_read_u32(pdev->dev.of_node, pname,
796 &abb->ldovbb_vset_mask);
797 if (ret) {
798 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
799 return ret;
801 if (!abb->ldovbb_vset_mask) {
802 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
803 return -EINVAL;
806 skip_opt:
807 pname = "ti,tranxdone-status-mask";
808 ret =
809 of_property_read_u32(pdev->dev.of_node, pname,
810 &abb->txdone_mask);
811 if (ret) {
812 dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
813 return ret;
815 if (!abb->txdone_mask) {
816 dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
817 return -EINVAL;
820 initdata = of_get_regulator_init_data(dev, pdev->dev.of_node);
821 if (!initdata) {
822 dev_err(dev, "%s: Unable to alloc regulator init data\n",
823 __func__);
824 return -ENOMEM;
827 /* init ABB opp_sel table */
828 ret = ti_abb_init_table(dev, abb, initdata);
829 if (ret)
830 return ret;
832 /* init ABB timing */
833 ret = ti_abb_init_timings(dev, abb);
834 if (ret)
835 return ret;
837 desc = &abb->rdesc;
838 desc->name = dev_name(dev);
839 desc->owner = THIS_MODULE;
840 desc->type = REGULATOR_VOLTAGE;
841 desc->ops = &ti_abb_reg_ops;
843 c = &initdata->constraints;
844 if (desc->n_voltages > 1)
845 c->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
846 c->always_on = true;
848 config.dev = dev;
849 config.init_data = initdata;
850 config.driver_data = abb;
851 config.of_node = pdev->dev.of_node;
853 rdev = devm_regulator_register(dev, desc, &config);
854 if (IS_ERR(rdev)) {
855 ret = PTR_ERR(rdev);
856 dev_err(dev, "%s: failed to register regulator(%d)\n",
857 __func__, ret);
858 return ret;
860 platform_set_drvdata(pdev, rdev);
862 /* Enable the ldo if not already done by bootloader */
863 ti_abb_rmw(abb->regs->sr2_en_mask, 1, abb->regs->setup_reg, abb->base);
865 return 0;
868 MODULE_ALIAS("platform:ti_abb");
870 static struct platform_driver ti_abb_driver = {
871 .probe = ti_abb_probe,
872 .driver = {
873 .name = "ti_abb",
874 .owner = THIS_MODULE,
875 .of_match_table = of_match_ptr(ti_abb_of_match),
878 module_platform_driver(ti_abb_driver);
880 MODULE_DESCRIPTION("Texas Instruments ABB LDO regulator driver");
881 MODULE_AUTHOR("Texas Instruments Inc.");
882 MODULE_LICENSE("GPL v2");