Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Texas Instruments SoC Adaptive Body Bias(ABB) Regulator
0004  *
0005  * Copyright (C) 2011 Texas Instruments, Inc.
0006  * Mike Turquette <mturquette@ti.com>
0007  *
0008  * Copyright (C) 2012-2013 Texas Instruments, Inc.
0009  * Andrii Tseglytskyi <andrii.tseglytskyi@ti.com>
0010  * Nishanth Menon <nm@ti.com>
0011  */
0012 #include <linux/clk.h>
0013 #include <linux/delay.h>
0014 #include <linux/err.h>
0015 #include <linux/io.h>
0016 #include <linux/module.h>
0017 #include <linux/of_device.h>
0018 #include <linux/of.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/regulator/driver.h>
0021 #include <linux/regulator/machine.h>
0022 #include <linux/regulator/of_regulator.h>
0023 
0024 /*
0025  * ABB LDO operating states:
0026  * NOMINAL_OPP: bypasses the ABB LDO
0027  * FAST_OPP:    sets ABB LDO to Forward Body-Bias
0028  * SLOW_OPP:    sets ABB LDO to Reverse Body-Bias
0029  */
0030 #define TI_ABB_NOMINAL_OPP  0
0031 #define TI_ABB_FAST_OPP     1
0032 #define TI_ABB_SLOW_OPP     3
0033 
0034 /**
0035  * struct ti_abb_info - ABB information per voltage setting
0036  * @opp_sel:    one of TI_ABB macro
0037  * @vset:   (optional) vset value that LDOVBB needs to be overridden with.
0038  *
0039  * Array of per voltage entries organized in the same order as regulator_desc's
0040  * volt_table list. (selector is used to index from this array)
0041  */
0042 struct ti_abb_info {
0043     u32 opp_sel;
0044     u32 vset;
0045 };
0046 
0047 /**
0048  * struct ti_abb_reg - Register description for ABB block
0049  * @setup_off:          setup register offset from base
0050  * @control_off:        control register offset from base
0051  * @sr2_wtcnt_value_mask:   setup register- sr2_wtcnt_value mask
0052  * @fbb_sel_mask:       setup register- FBB sel mask
0053  * @rbb_sel_mask:       setup register- RBB sel mask
0054  * @sr2_en_mask:        setup register- enable mask
0055  * @opp_change_mask:        control register - mask to trigger LDOVBB change
0056  * @opp_sel_mask:       control register - mask for mode to operate
0057  */
0058 struct ti_abb_reg {
0059     u32 setup_off;
0060     u32 control_off;
0061 
0062     /* Setup register fields */
0063     u32 sr2_wtcnt_value_mask;
0064     u32 fbb_sel_mask;
0065     u32 rbb_sel_mask;
0066     u32 sr2_en_mask;
0067 
0068     /* Control register fields */
0069     u32 opp_change_mask;
0070     u32 opp_sel_mask;
0071 };
0072 
0073 /**
0074  * struct ti_abb - ABB instance data
0075  * @rdesc:          regulator descriptor
0076  * @clk:            clock(usually sysclk) supplying ABB block
0077  * @base:           base address of ABB block
0078  * @setup_reg:          setup register of ABB block
0079  * @control_reg:        control register of ABB block
0080  * @int_base:           interrupt register base address
0081  * @efuse_base:         (optional) efuse base address for ABB modes
0082  * @ldo_base:           (optional) LDOVBB vset override base address
0083  * @regs:           pointer to struct ti_abb_reg for ABB block
0084  * @txdone_mask:        mask on int_base for tranxdone interrupt
0085  * @ldovbb_override_mask:   mask to ldo_base for overriding default LDO VBB
0086  *              vset with value from efuse
0087  * @ldovbb_vset_mask:       mask to ldo_base for providing the VSET override
0088  * @info:           array to per voltage ABB configuration
0089  * @current_info_idx:       current index to info
0090  * @settling_time:      SoC specific settling time for LDO VBB
0091  */
0092 struct ti_abb {
0093     struct regulator_desc rdesc;
0094     struct clk *clk;
0095     void __iomem *base;
0096     void __iomem *setup_reg;
0097     void __iomem *control_reg;
0098     void __iomem *int_base;
0099     void __iomem *efuse_base;
0100     void __iomem *ldo_base;
0101 
0102     const struct ti_abb_reg *regs;
0103     u32 txdone_mask;
0104     u32 ldovbb_override_mask;
0105     u32 ldovbb_vset_mask;
0106 
0107     struct ti_abb_info *info;
0108     int current_info_idx;
0109 
0110     u32 settling_time;
0111 };
0112 
0113 /**
0114  * ti_abb_rmw() - handy wrapper to set specific register bits
0115  * @mask:   mask for register field
0116  * @value:  value shifted to mask location and written
0117  * @reg:    register address
0118  *
0119  * Return: final register value (may be unused)
0120  */
0121 static inline u32 ti_abb_rmw(u32 mask, u32 value, void __iomem *reg)
0122 {
0123     u32 val;
0124 
0125     val = readl(reg);
0126     val &= ~mask;
0127     val |= (value << __ffs(mask)) & mask;
0128     writel(val, reg);
0129 
0130     return val;
0131 }
0132 
0133 /**
0134  * ti_abb_check_txdone() - handy wrapper to check ABB tranxdone status
0135  * @abb:    pointer to the abb instance
0136  *
0137  * Return: true or false
0138  */
0139 static inline bool ti_abb_check_txdone(const struct ti_abb *abb)
0140 {
0141     return !!(readl(abb->int_base) & abb->txdone_mask);
0142 }
0143 
0144 /**
0145  * ti_abb_clear_txdone() - handy wrapper to clear ABB tranxdone status
0146  * @abb:    pointer to the abb instance
0147  */
0148 static inline void ti_abb_clear_txdone(const struct ti_abb *abb)
0149 {
0150     writel(abb->txdone_mask, abb->int_base);
0151 };
0152 
0153 /**
0154  * ti_abb_wait_tranx() - waits for ABB tranxdone event
0155  * @dev:    device
0156  * @abb:    pointer to the abb instance
0157  *
0158  * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
0159  */
0160 static int ti_abb_wait_txdone(struct device *dev, struct ti_abb *abb)
0161 {
0162     int timeout = 0;
0163     bool status;
0164 
0165     while (timeout++ <= abb->settling_time) {
0166         status = ti_abb_check_txdone(abb);
0167         if (status)
0168             return 0;
0169 
0170         udelay(1);
0171     }
0172 
0173     dev_warn_ratelimited(dev, "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
0174                  __func__, timeout, readl(abb->int_base));
0175     return -ETIMEDOUT;
0176 }
0177 
0178 /**
0179  * ti_abb_clear_all_txdone() - clears ABB tranxdone event
0180  * @dev:    device
0181  * @abb:    pointer to the abb instance
0182  *
0183  * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
0184  */
0185 static int ti_abb_clear_all_txdone(struct device *dev, const struct ti_abb *abb)
0186 {
0187     int timeout = 0;
0188     bool status;
0189 
0190     while (timeout++ <= abb->settling_time) {
0191         ti_abb_clear_txdone(abb);
0192 
0193         status = ti_abb_check_txdone(abb);
0194         if (!status)
0195             return 0;
0196 
0197         udelay(1);
0198     }
0199 
0200     dev_warn_ratelimited(dev, "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
0201                  __func__, timeout, readl(abb->int_base));
0202     return -ETIMEDOUT;
0203 }
0204 
0205 /**
0206  * ti_abb_program_ldovbb() - program LDOVBB register for override value
0207  * @dev:    device
0208  * @abb:    pointer to the abb instance
0209  * @info:   ABB info to program
0210  */
0211 static void ti_abb_program_ldovbb(struct device *dev, const struct ti_abb *abb,
0212                   struct ti_abb_info *info)
0213 {
0214     u32 val;
0215 
0216     val = readl(abb->ldo_base);
0217     /* clear up previous values */
0218     val &= ~(abb->ldovbb_override_mask | abb->ldovbb_vset_mask);
0219 
0220     switch (info->opp_sel) {
0221     case TI_ABB_SLOW_OPP:
0222     case TI_ABB_FAST_OPP:
0223         val |= abb->ldovbb_override_mask;
0224         val |= info->vset << __ffs(abb->ldovbb_vset_mask);
0225         break;
0226     }
0227 
0228     writel(val, abb->ldo_base);
0229 }
0230 
0231 /**
0232  * ti_abb_set_opp() - Setup ABB and LDO VBB for required bias
0233  * @rdev:   regulator device
0234  * @abb:    pointer to the abb instance
0235  * @info:   ABB info to program
0236  *
0237  * Return: 0 on success or appropriate error value when fails
0238  */
0239 static int ti_abb_set_opp(struct regulator_dev *rdev, struct ti_abb *abb,
0240               struct ti_abb_info *info)
0241 {
0242     const struct ti_abb_reg *regs = abb->regs;
0243     struct device *dev = &rdev->dev;
0244     int ret;
0245 
0246     ret = ti_abb_clear_all_txdone(dev, abb);
0247     if (ret)
0248         goto out;
0249 
0250     ti_abb_rmw(regs->fbb_sel_mask | regs->rbb_sel_mask, 0, abb->setup_reg);
0251 
0252     switch (info->opp_sel) {
0253     case TI_ABB_SLOW_OPP:
0254         ti_abb_rmw(regs->rbb_sel_mask, 1, abb->setup_reg);
0255         break;
0256     case TI_ABB_FAST_OPP:
0257         ti_abb_rmw(regs->fbb_sel_mask, 1, abb->setup_reg);
0258         break;
0259     }
0260 
0261     /* program next state of ABB ldo */
0262     ti_abb_rmw(regs->opp_sel_mask, info->opp_sel, abb->control_reg);
0263 
0264     /*
0265      * program LDO VBB vset override if needed for !bypass mode
0266      * XXX: Do not switch sequence - for !bypass, LDO override reset *must*
0267      * be performed *before* switch to bias mode else VBB glitches.
0268      */
0269     if (abb->ldo_base && info->opp_sel != TI_ABB_NOMINAL_OPP)
0270         ti_abb_program_ldovbb(dev, abb, info);
0271 
0272     /* Initiate ABB ldo change */
0273     ti_abb_rmw(regs->opp_change_mask, 1, abb->control_reg);
0274 
0275     /* Wait for ABB LDO to complete transition to new Bias setting */
0276     ret = ti_abb_wait_txdone(dev, abb);
0277     if (ret)
0278         goto out;
0279 
0280     ret = ti_abb_clear_all_txdone(dev, abb);
0281     if (ret)
0282         goto out;
0283 
0284     /*
0285      * Reset LDO VBB vset override bypass mode
0286      * XXX: Do not switch sequence - for bypass, LDO override reset *must*
0287      * be performed *after* switch to bypass else VBB glitches.
0288      */
0289     if (abb->ldo_base && info->opp_sel == TI_ABB_NOMINAL_OPP)
0290         ti_abb_program_ldovbb(dev, abb, info);
0291 
0292 out:
0293     return ret;
0294 }
0295 
0296 /**
0297  * ti_abb_set_voltage_sel() - regulator accessor function to set ABB LDO
0298  * @rdev:   regulator device
0299  * @sel:    selector to index into required ABB LDO settings (maps to
0300  *      regulator descriptor's volt_table)
0301  *
0302  * Return: 0 on success or appropriate error value when fails
0303  */
0304 static int ti_abb_set_voltage_sel(struct regulator_dev *rdev, unsigned int sel)
0305 {
0306     const struct regulator_desc *desc = rdev->desc;
0307     struct ti_abb *abb = rdev_get_drvdata(rdev);
0308     struct device *dev = &rdev->dev;
0309     struct ti_abb_info *info, *oinfo;
0310     int ret = 0;
0311 
0312     if (!abb) {
0313         dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
0314                     __func__);
0315         return -ENODEV;
0316     }
0317 
0318     if (!desc->n_voltages || !abb->info) {
0319         dev_err_ratelimited(dev,
0320                     "%s: No valid voltage table entries?\n",
0321                     __func__);
0322         return -EINVAL;
0323     }
0324 
0325     if (sel >= desc->n_voltages) {
0326         dev_err(dev, "%s: sel idx(%d) >= n_voltages(%d)\n", __func__,
0327             sel, desc->n_voltages);
0328         return -EINVAL;
0329     }
0330 
0331     /* If we are in the same index as we were, nothing to do here! */
0332     if (sel == abb->current_info_idx) {
0333         dev_dbg(dev, "%s: Already at sel=%d\n", __func__, sel);
0334         return ret;
0335     }
0336 
0337     info = &abb->info[sel];
0338     /*
0339      * When Linux kernel is starting up, we aren't sure of the
0340      * Bias configuration that bootloader has configured.
0341      * So, we get to know the actual setting the first time
0342      * we are asked to transition.
0343      */
0344     if (abb->current_info_idx == -EINVAL)
0345         goto just_set_abb;
0346 
0347     /* If data is exactly the same, then just update index, no change */
0348     oinfo = &abb->info[abb->current_info_idx];
0349     if (!memcmp(info, oinfo, sizeof(*info))) {
0350         dev_dbg(dev, "%s: Same data new idx=%d, old idx=%d\n", __func__,
0351             sel, abb->current_info_idx);
0352         goto out;
0353     }
0354 
0355 just_set_abb:
0356     ret = ti_abb_set_opp(rdev, abb, info);
0357 
0358 out:
0359     if (!ret)
0360         abb->current_info_idx = sel;
0361     else
0362         dev_err_ratelimited(dev,
0363                     "%s: Volt[%d] idx[%d] mode[%d] Fail(%d)\n",
0364                     __func__, desc->volt_table[sel], sel,
0365                     info->opp_sel, ret);
0366     return ret;
0367 }
0368 
0369 /**
0370  * ti_abb_get_voltage_sel() - Regulator accessor to get current ABB LDO setting
0371  * @rdev:   regulator device
0372  *
0373  * Return: 0 on success or appropriate error value when fails
0374  */
0375 static int ti_abb_get_voltage_sel(struct regulator_dev *rdev)
0376 {
0377     const struct regulator_desc *desc = rdev->desc;
0378     struct ti_abb *abb = rdev_get_drvdata(rdev);
0379     struct device *dev = &rdev->dev;
0380 
0381     if (!abb) {
0382         dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
0383                     __func__);
0384         return -ENODEV;
0385     }
0386 
0387     if (!desc->n_voltages || !abb->info) {
0388         dev_err_ratelimited(dev,
0389                     "%s: No valid voltage table entries?\n",
0390                     __func__);
0391         return -EINVAL;
0392     }
0393 
0394     if (abb->current_info_idx >= (int)desc->n_voltages) {
0395         dev_err(dev, "%s: Corrupted data? idx(%d) >= n_voltages(%d)\n",
0396             __func__, abb->current_info_idx, desc->n_voltages);
0397         return -EINVAL;
0398     }
0399 
0400     return abb->current_info_idx;
0401 }
0402 
0403 /**
0404  * ti_abb_init_timings() - setup ABB clock timing for the current platform
0405  * @dev:    device
0406  * @abb:    pointer to the abb instance
0407  *
0408  * Return: 0 if timing is updated, else returns error result.
0409  */
0410 static int ti_abb_init_timings(struct device *dev, struct ti_abb *abb)
0411 {
0412     u32 clock_cycles;
0413     u32 clk_rate, sr2_wt_cnt_val, cycle_rate;
0414     const struct ti_abb_reg *regs = abb->regs;
0415     int ret;
0416     char *pname = "ti,settling-time";
0417 
0418     /* read device tree properties */
0419     ret = of_property_read_u32(dev->of_node, pname, &abb->settling_time);
0420     if (ret) {
0421         dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
0422         return ret;
0423     }
0424 
0425     /* ABB LDO cannot be settle in 0 time */
0426     if (!abb->settling_time) {
0427         dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
0428         return -EINVAL;
0429     }
0430 
0431     pname = "ti,clock-cycles";
0432     ret = of_property_read_u32(dev->of_node, pname, &clock_cycles);
0433     if (ret) {
0434         dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
0435         return ret;
0436     }
0437     /* ABB LDO cannot be settle in 0 clock cycles */
0438     if (!clock_cycles) {
0439         dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
0440         return -EINVAL;
0441     }
0442 
0443     abb->clk = devm_clk_get(dev, NULL);
0444     if (IS_ERR(abb->clk)) {
0445         ret = PTR_ERR(abb->clk);
0446         dev_err(dev, "%s: Unable to get clk(%d)\n", __func__, ret);
0447         return ret;
0448     }
0449 
0450     /*
0451      * SR2_WTCNT_VALUE is the settling time for the ABB ldo after a
0452      * transition and must be programmed with the correct time at boot.
0453      * The value programmed into the register is the number of SYS_CLK
0454      * clock cycles that match a given wall time profiled for the ldo.
0455      * This value depends on:
0456      * settling time of ldo in micro-seconds (varies per OMAP family)
0457      * # of clock cycles per SYS_CLK period (varies per OMAP family)
0458      * the SYS_CLK frequency in MHz (varies per board)
0459      * The formula is:
0460      *
0461      *                      ldo settling time (in micro-seconds)
0462      * SR2_WTCNT_VALUE = ------------------------------------------
0463      *                   (# system clock cycles) * (sys_clk period)
0464      *
0465      * Put another way:
0466      *
0467      * SR2_WTCNT_VALUE = settling time / (# SYS_CLK cycles / SYS_CLK rate))
0468      *
0469      * To avoid dividing by zero multiply both "# clock cycles" and
0470      * "settling time" by 10 such that the final result is the one we want.
0471      */
0472 
0473     /* Convert SYS_CLK rate to MHz & prevent divide by zero */
0474     clk_rate = DIV_ROUND_CLOSEST(clk_get_rate(abb->clk), 1000000);
0475 
0476     /* Calculate cycle rate */
0477     cycle_rate = DIV_ROUND_CLOSEST(clock_cycles * 10, clk_rate);
0478 
0479     /* Calculate SR2_WTCNT_VALUE */
0480     sr2_wt_cnt_val = DIV_ROUND_CLOSEST(abb->settling_time * 10, cycle_rate);
0481 
0482     dev_dbg(dev, "%s: Clk_rate=%ld, sr2_cnt=0x%08x\n", __func__,
0483         clk_get_rate(abb->clk), sr2_wt_cnt_val);
0484 
0485     ti_abb_rmw(regs->sr2_wtcnt_value_mask, sr2_wt_cnt_val, abb->setup_reg);
0486 
0487     return 0;
0488 }
0489 
0490 /**
0491  * ti_abb_init_table() - Initialize ABB table from device tree
0492  * @dev:    device
0493  * @abb:    pointer to the abb instance
0494  * @rinit_data: regulator initdata
0495  *
0496  * Return: 0 on success or appropriate error value when fails
0497  */
0498 static int ti_abb_init_table(struct device *dev, struct ti_abb *abb,
0499                  struct regulator_init_data *rinit_data)
0500 {
0501     struct ti_abb_info *info;
0502     const u32 num_values = 6;
0503     char *pname = "ti,abb_info";
0504     u32 i;
0505     unsigned int *volt_table;
0506     int num_entries, min_uV = INT_MAX, max_uV = 0;
0507     struct regulation_constraints *c = &rinit_data->constraints;
0508 
0509     /*
0510      * Each abb_info is a set of n-tuple, where n is num_values, consisting
0511      * of voltage and a set of detection logic for ABB information for that
0512      * voltage to apply.
0513      */
0514     num_entries = of_property_count_u32_elems(dev->of_node, pname);
0515     if (num_entries < 0) {
0516         dev_err(dev, "No '%s' property?\n", pname);
0517         return num_entries;
0518     }
0519 
0520     if (!num_entries || (num_entries % num_values)) {
0521         dev_err(dev, "All '%s' list entries need %d vals\n", pname,
0522             num_values);
0523         return -EINVAL;
0524     }
0525     num_entries /= num_values;
0526 
0527     info = devm_kcalloc(dev, num_entries, sizeof(*info), GFP_KERNEL);
0528     if (!info)
0529         return -ENOMEM;
0530 
0531     abb->info = info;
0532 
0533     volt_table = devm_kcalloc(dev, num_entries, sizeof(unsigned int),
0534                   GFP_KERNEL);
0535     if (!volt_table)
0536         return -ENOMEM;
0537 
0538     abb->rdesc.n_voltages = num_entries;
0539     abb->rdesc.volt_table = volt_table;
0540     /* We do not know where the OPP voltage is at the moment */
0541     abb->current_info_idx = -EINVAL;
0542 
0543     for (i = 0; i < num_entries; i++, info++, volt_table++) {
0544         u32 efuse_offset, rbb_mask, fbb_mask, vset_mask;
0545         u32 efuse_val;
0546 
0547         /* NOTE: num_values should equal to entries picked up here */
0548         of_property_read_u32_index(dev->of_node, pname, i * num_values,
0549                        volt_table);
0550         of_property_read_u32_index(dev->of_node, pname,
0551                        i * num_values + 1, &info->opp_sel);
0552         of_property_read_u32_index(dev->of_node, pname,
0553                        i * num_values + 2, &efuse_offset);
0554         of_property_read_u32_index(dev->of_node, pname,
0555                        i * num_values + 3, &rbb_mask);
0556         of_property_read_u32_index(dev->of_node, pname,
0557                        i * num_values + 4, &fbb_mask);
0558         of_property_read_u32_index(dev->of_node, pname,
0559                        i * num_values + 5, &vset_mask);
0560 
0561         dev_dbg(dev,
0562             "[%d]v=%d ABB=%d ef=0x%x rbb=0x%x fbb=0x%x vset=0x%x\n",
0563             i, *volt_table, info->opp_sel, efuse_offset, rbb_mask,
0564             fbb_mask, vset_mask);
0565 
0566         /* Find min/max for voltage set */
0567         if (min_uV > *volt_table)
0568             min_uV = *volt_table;
0569         if (max_uV < *volt_table)
0570             max_uV = *volt_table;
0571 
0572         if (!abb->efuse_base) {
0573             /* Ignore invalid data, but warn to help cleanup */
0574             if (efuse_offset || rbb_mask || fbb_mask || vset_mask)
0575                 dev_err(dev, "prop '%s': v=%d,bad efuse/mask\n",
0576                     pname, *volt_table);
0577             goto check_abb;
0578         }
0579 
0580         efuse_val = readl(abb->efuse_base + efuse_offset);
0581 
0582         /* Use ABB recommendation from Efuse */
0583         if (efuse_val & rbb_mask)
0584             info->opp_sel = TI_ABB_SLOW_OPP;
0585         else if (efuse_val & fbb_mask)
0586             info->opp_sel = TI_ABB_FAST_OPP;
0587         else if (rbb_mask || fbb_mask)
0588             info->opp_sel = TI_ABB_NOMINAL_OPP;
0589 
0590         dev_dbg(dev,
0591             "[%d]v=%d efusev=0x%x final ABB=%d\n",
0592             i, *volt_table, efuse_val, info->opp_sel);
0593 
0594         /* Use recommended Vset bits from Efuse */
0595         if (!abb->ldo_base) {
0596             if (vset_mask)
0597                 dev_err(dev, "prop'%s':v=%d vst=%x LDO base?\n",
0598                     pname, *volt_table, vset_mask);
0599             continue;
0600         }
0601         info->vset = (efuse_val & vset_mask) >> __ffs(vset_mask);
0602         dev_dbg(dev, "[%d]v=%d vset=%x\n", i, *volt_table, info->vset);
0603 check_abb:
0604         switch (info->opp_sel) {
0605         case TI_ABB_NOMINAL_OPP:
0606         case TI_ABB_FAST_OPP:
0607         case TI_ABB_SLOW_OPP:
0608             /* Valid values */
0609             break;
0610         default:
0611             dev_err(dev, "%s:[%d]v=%d, ABB=%d is invalid! Abort!\n",
0612                 __func__, i, *volt_table, info->opp_sel);
0613             return -EINVAL;
0614         }
0615     }
0616 
0617     /* Setup the min/max voltage constraints from the supported list */
0618     c->min_uV = min_uV;
0619     c->max_uV = max_uV;
0620 
0621     return 0;
0622 }
0623 
0624 static const struct regulator_ops ti_abb_reg_ops = {
0625     .list_voltage = regulator_list_voltage_table,
0626 
0627     .set_voltage_sel = ti_abb_set_voltage_sel,
0628     .get_voltage_sel = ti_abb_get_voltage_sel,
0629 };
0630 
0631 /* Default ABB block offsets, IF this changes in future, create new one */
0632 static const struct ti_abb_reg abb_regs_v1 = {
0633     /* WARNING: registers are wrongly documented in TRM */
0634     .setup_off      = 0x04,
0635     .control_off        = 0x00,
0636 
0637     .sr2_wtcnt_value_mask   = (0xff << 8),
0638     .fbb_sel_mask       = (0x01 << 2),
0639     .rbb_sel_mask       = (0x01 << 1),
0640     .sr2_en_mask        = (0x01 << 0),
0641 
0642     .opp_change_mask    = (0x01 << 2),
0643     .opp_sel_mask       = (0x03 << 0),
0644 };
0645 
0646 static const struct ti_abb_reg abb_regs_v2 = {
0647     .setup_off      = 0x00,
0648     .control_off        = 0x04,
0649 
0650     .sr2_wtcnt_value_mask   = (0xff << 8),
0651     .fbb_sel_mask       = (0x01 << 2),
0652     .rbb_sel_mask       = (0x01 << 1),
0653     .sr2_en_mask        = (0x01 << 0),
0654 
0655     .opp_change_mask    = (0x01 << 2),
0656     .opp_sel_mask       = (0x03 << 0),
0657 };
0658 
0659 static const struct ti_abb_reg abb_regs_generic = {
0660     .sr2_wtcnt_value_mask   = (0xff << 8),
0661     .fbb_sel_mask       = (0x01 << 2),
0662     .rbb_sel_mask       = (0x01 << 1),
0663     .sr2_en_mask        = (0x01 << 0),
0664 
0665     .opp_change_mask    = (0x01 << 2),
0666     .opp_sel_mask       = (0x03 << 0),
0667 };
0668 
0669 static const struct of_device_id ti_abb_of_match[] = {
0670     {.compatible = "ti,abb-v1", .data = &abb_regs_v1},
0671     {.compatible = "ti,abb-v2", .data = &abb_regs_v2},
0672     {.compatible = "ti,abb-v3", .data = &abb_regs_generic},
0673     { },
0674 };
0675 
0676 MODULE_DEVICE_TABLE(of, ti_abb_of_match);
0677 
0678 /**
0679  * ti_abb_probe() - Initialize an ABB ldo instance
0680  * @pdev: ABB platform device
0681  *
0682  * Initializes an individual ABB LDO for required Body-Bias. ABB is used to
0683  * additional bias supply to SoC modules for power savings or mandatory stability
0684  * configuration at certain Operating Performance Points(OPPs).
0685  *
0686  * Return: 0 on success or appropriate error value when fails
0687  */
0688 static int ti_abb_probe(struct platform_device *pdev)
0689 {
0690     struct device *dev = &pdev->dev;
0691     const struct of_device_id *match;
0692     struct resource *res;
0693     struct ti_abb *abb;
0694     struct regulator_init_data *initdata = NULL;
0695     struct regulator_dev *rdev = NULL;
0696     struct regulator_desc *desc;
0697     struct regulation_constraints *c;
0698     struct regulator_config config = { };
0699     char *pname;
0700     int ret = 0;
0701 
0702     match = of_match_device(ti_abb_of_match, dev);
0703     if (!match) {
0704         /* We do not expect this to happen */
0705         dev_err(dev, "%s: Unable to match device\n", __func__);
0706         return -ENODEV;
0707     }
0708     if (!match->data) {
0709         dev_err(dev, "%s: Bad data in match\n", __func__);
0710         return -EINVAL;
0711     }
0712 
0713     abb = devm_kzalloc(dev, sizeof(struct ti_abb), GFP_KERNEL);
0714     if (!abb)
0715         return -ENOMEM;
0716     abb->regs = match->data;
0717 
0718     /* Map ABB resources */
0719     if (abb->regs->setup_off || abb->regs->control_off) {
0720         abb->base = devm_platform_ioremap_resource_byname(pdev, "base-address");
0721         if (IS_ERR(abb->base))
0722             return PTR_ERR(abb->base);
0723 
0724         abb->setup_reg = abb->base + abb->regs->setup_off;
0725         abb->control_reg = abb->base + abb->regs->control_off;
0726 
0727     } else {
0728         abb->control_reg = devm_platform_ioremap_resource_byname(pdev, "control-address");
0729         if (IS_ERR(abb->control_reg))
0730             return PTR_ERR(abb->control_reg);
0731 
0732         abb->setup_reg = devm_platform_ioremap_resource_byname(pdev, "setup-address");
0733         if (IS_ERR(abb->setup_reg))
0734             return PTR_ERR(abb->setup_reg);
0735     }
0736 
0737     abb->int_base = devm_platform_ioremap_resource_byname(pdev, "int-address");
0738     if (IS_ERR(abb->int_base))
0739         return PTR_ERR(abb->int_base);
0740 
0741     /* Map Optional resources */
0742     pname = "efuse-address";
0743     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
0744     if (!res) {
0745         dev_dbg(dev, "Missing '%s' IO resource\n", pname);
0746         ret = -ENODEV;
0747         goto skip_opt;
0748     }
0749 
0750     /*
0751      * We may have shared efuse register offsets which are read-only
0752      * between domains
0753      */
0754     abb->efuse_base = devm_ioremap(dev, res->start,
0755                            resource_size(res));
0756     if (!abb->efuse_base) {
0757         dev_err(dev, "Unable to map '%s'\n", pname);
0758         return -ENOMEM;
0759     }
0760 
0761     pname = "ldo-address";
0762     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
0763     if (!res) {
0764         dev_dbg(dev, "Missing '%s' IO resource\n", pname);
0765         ret = -ENODEV;
0766         goto skip_opt;
0767     }
0768     abb->ldo_base = devm_ioremap_resource(dev, res);
0769     if (IS_ERR(abb->ldo_base))
0770         return PTR_ERR(abb->ldo_base);
0771 
0772     /* IF ldo_base is set, the following are mandatory */
0773     pname = "ti,ldovbb-override-mask";
0774     ret =
0775         of_property_read_u32(pdev->dev.of_node, pname,
0776                  &abb->ldovbb_override_mask);
0777     if (ret) {
0778         dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
0779         return ret;
0780     }
0781     if (!abb->ldovbb_override_mask) {
0782         dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
0783         return -EINVAL;
0784     }
0785 
0786     pname = "ti,ldovbb-vset-mask";
0787     ret =
0788         of_property_read_u32(pdev->dev.of_node, pname,
0789                  &abb->ldovbb_vset_mask);
0790     if (ret) {
0791         dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
0792         return ret;
0793     }
0794     if (!abb->ldovbb_vset_mask) {
0795         dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
0796         return -EINVAL;
0797     }
0798 
0799 skip_opt:
0800     pname = "ti,tranxdone-status-mask";
0801     ret =
0802         of_property_read_u32(pdev->dev.of_node, pname,
0803                  &abb->txdone_mask);
0804     if (ret) {
0805         dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
0806         return ret;
0807     }
0808     if (!abb->txdone_mask) {
0809         dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
0810         return -EINVAL;
0811     }
0812 
0813     initdata = of_get_regulator_init_data(dev, pdev->dev.of_node,
0814                           &abb->rdesc);
0815     if (!initdata) {
0816         dev_err(dev, "%s: Unable to alloc regulator init data\n",
0817             __func__);
0818         return -ENOMEM;
0819     }
0820 
0821     /* init ABB opp_sel table */
0822     ret = ti_abb_init_table(dev, abb, initdata);
0823     if (ret)
0824         return ret;
0825 
0826     /* init ABB timing */
0827     ret = ti_abb_init_timings(dev, abb);
0828     if (ret)
0829         return ret;
0830 
0831     desc = &abb->rdesc;
0832     desc->name = dev_name(dev);
0833     desc->owner = THIS_MODULE;
0834     desc->type = REGULATOR_VOLTAGE;
0835     desc->ops = &ti_abb_reg_ops;
0836 
0837     c = &initdata->constraints;
0838     if (desc->n_voltages > 1)
0839         c->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
0840     c->always_on = true;
0841 
0842     config.dev = dev;
0843     config.init_data = initdata;
0844     config.driver_data = abb;
0845     config.of_node = pdev->dev.of_node;
0846 
0847     rdev = devm_regulator_register(dev, desc, &config);
0848     if (IS_ERR(rdev)) {
0849         ret = PTR_ERR(rdev);
0850         dev_err(dev, "%s: failed to register regulator(%d)\n",
0851             __func__, ret);
0852         return ret;
0853     }
0854     platform_set_drvdata(pdev, rdev);
0855 
0856     /* Enable the ldo if not already done by bootloader */
0857     ti_abb_rmw(abb->regs->sr2_en_mask, 1, abb->setup_reg);
0858 
0859     return 0;
0860 }
0861 
0862 MODULE_ALIAS("platform:ti_abb");
0863 
0864 static struct platform_driver ti_abb_driver = {
0865     .probe = ti_abb_probe,
0866     .driver = {
0867            .name = "ti_abb",
0868            .of_match_table = of_match_ptr(ti_abb_of_match),
0869            },
0870 };
0871 module_platform_driver(ti_abb_driver);
0872 
0873 MODULE_DESCRIPTION("Texas Instruments ABB LDO regulator driver");
0874 MODULE_AUTHOR("Texas Instruments Inc.");
0875 MODULE_LICENSE("GPL v2");