ti-abb-regulator.c 23 KB

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