ti-abb-regulator.c 24 KB

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