ti-abb-regulator.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. * Texas Instruments SoC Adaptive Body Bias(ABB) Regulator
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Mike Turquette <mturquette@ti.com>
  6. *
  7. * Copyright (C) 2012-2013 Texas Instruments, Inc.
  8. * Andrii Tseglytskyi <andrii.tseglytskyi@ti.com>
  9. * Nishanth Menon <nm@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  16. * kind, whether express or implied; without even the implied warranty
  17. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/err.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/regulator/driver.h>
  29. #include <linux/regulator/machine.h>
  30. #include <linux/regulator/of_regulator.h>
  31. /*
  32. * 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. break;
  161. udelay(1);
  162. }
  163. if (timeout > abb->settling_time) {
  164. dev_warn_ratelimited(dev,
  165. "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
  166. __func__, timeout, readl(abb->int_base));
  167. return -ETIMEDOUT;
  168. }
  169. return 0;
  170. }
  171. /**
  172. * ti_abb_clear_all_txdone() - clears ABB tranxdone event
  173. * @dev: device
  174. * @abb: pointer to the abb instance
  175. *
  176. * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
  177. */
  178. static int ti_abb_clear_all_txdone(struct device *dev, const struct ti_abb *abb)
  179. {
  180. int timeout = 0;
  181. bool status;
  182. while (timeout++ <= abb->settling_time) {
  183. ti_abb_clear_txdone(abb);
  184. status = ti_abb_check_txdone(abb);
  185. if (!status)
  186. break;
  187. udelay(1);
  188. }
  189. if (timeout > abb->settling_time) {
  190. dev_warn_ratelimited(dev,
  191. "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
  192. __func__, timeout, readl(abb->int_base));
  193. return -ETIMEDOUT;
  194. }
  195. return 0;
  196. }
  197. /**
  198. * ti_abb_program_ldovbb() - program LDOVBB register for override value
  199. * @dev: device
  200. * @abb: pointer to the abb instance
  201. * @info: ABB info to program
  202. */
  203. static void ti_abb_program_ldovbb(struct device *dev, const struct ti_abb *abb,
  204. struct ti_abb_info *info)
  205. {
  206. u32 val;
  207. val = readl(abb->ldo_base);
  208. /* clear up previous values */
  209. val &= ~(abb->ldovbb_override_mask | abb->ldovbb_vset_mask);
  210. switch (info->opp_sel) {
  211. case TI_ABB_SLOW_OPP:
  212. case TI_ABB_FAST_OPP:
  213. val |= abb->ldovbb_override_mask;
  214. val |= info->vset << __ffs(abb->ldovbb_vset_mask);
  215. break;
  216. }
  217. writel(val, abb->ldo_base);
  218. }
  219. /**
  220. * ti_abb_set_opp() - Setup ABB and LDO VBB for required bias
  221. * @rdev: regulator device
  222. * @abb: pointer to the abb instance
  223. * @info: ABB info to program
  224. *
  225. * Return: 0 on success or appropriate error value when fails
  226. */
  227. static int ti_abb_set_opp(struct regulator_dev *rdev, struct ti_abb *abb,
  228. struct ti_abb_info *info)
  229. {
  230. const struct ti_abb_reg *regs = abb->regs;
  231. struct device *dev = &rdev->dev;
  232. int ret;
  233. ret = ti_abb_clear_all_txdone(dev, abb);
  234. if (ret)
  235. goto out;
  236. ti_abb_rmw(regs->fbb_sel_mask | regs->rbb_sel_mask, 0, abb->setup_reg);
  237. switch (info->opp_sel) {
  238. case TI_ABB_SLOW_OPP:
  239. ti_abb_rmw(regs->rbb_sel_mask, 1, abb->setup_reg);
  240. break;
  241. case TI_ABB_FAST_OPP:
  242. ti_abb_rmw(regs->fbb_sel_mask, 1, abb->setup_reg);
  243. break;
  244. }
  245. /* program next state of ABB ldo */
  246. ti_abb_rmw(regs->opp_sel_mask, info->opp_sel, abb->control_reg);
  247. /*
  248. * program LDO VBB vset override if needed for !bypass mode
  249. * XXX: Do not switch sequence - for !bypass, LDO override reset *must*
  250. * be performed *before* switch to bias mode 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. /* Initiate ABB ldo change */
  255. ti_abb_rmw(regs->opp_change_mask, 1, abb->control_reg);
  256. /* Wait for ABB LDO to complete transition to new Bias setting */
  257. ret = ti_abb_wait_txdone(dev, abb);
  258. if (ret)
  259. goto out;
  260. ret = ti_abb_clear_all_txdone(dev, abb);
  261. if (ret)
  262. goto out;
  263. /*
  264. * Reset LDO VBB vset override bypass mode
  265. * XXX: Do not switch sequence - for bypass, LDO override reset *must*
  266. * be performed *after* switch to bypass else VBB glitches.
  267. */
  268. if (abb->ldo_base && info->opp_sel == TI_ABB_NOMINAL_OPP)
  269. ti_abb_program_ldovbb(dev, abb, info);
  270. out:
  271. return ret;
  272. }
  273. /**
  274. * ti_abb_set_voltage_sel() - regulator accessor function to set ABB LDO
  275. * @rdev: regulator device
  276. * @sel: selector to index into required ABB LDO settings (maps to
  277. * regulator descriptor's volt_table)
  278. *
  279. * Return: 0 on success or appropriate error value when fails
  280. */
  281. static int ti_abb_set_voltage_sel(struct regulator_dev *rdev, unsigned sel)
  282. {
  283. const struct regulator_desc *desc = rdev->desc;
  284. struct ti_abb *abb = rdev_get_drvdata(rdev);
  285. struct device *dev = &rdev->dev;
  286. struct ti_abb_info *info, *oinfo;
  287. int ret = 0;
  288. if (!abb) {
  289. dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
  290. __func__);
  291. return -ENODEV;
  292. }
  293. if (!desc->n_voltages || !abb->info) {
  294. dev_err_ratelimited(dev,
  295. "%s: No valid voltage table entries?\n",
  296. __func__);
  297. return -EINVAL;
  298. }
  299. if (sel >= desc->n_voltages) {
  300. dev_err(dev, "%s: sel idx(%d) >= n_voltages(%d)\n", __func__,
  301. sel, desc->n_voltages);
  302. return -EINVAL;
  303. }
  304. /* If we are in the same index as we were, nothing to do here! */
  305. if (sel == abb->current_info_idx) {
  306. dev_dbg(dev, "%s: Already at sel=%d\n", __func__, sel);
  307. return ret;
  308. }
  309. /* If data is exactly the same, then just update index, no change */
  310. info = &abb->info[sel];
  311. oinfo = &abb->info[abb->current_info_idx];
  312. if (!memcmp(info, oinfo, sizeof(*info))) {
  313. dev_dbg(dev, "%s: Same data new idx=%d, old idx=%d\n", __func__,
  314. sel, abb->current_info_idx);
  315. goto out;
  316. }
  317. ret = ti_abb_set_opp(rdev, abb, info);
  318. out:
  319. if (!ret)
  320. abb->current_info_idx = sel;
  321. else
  322. dev_err_ratelimited(dev,
  323. "%s: Volt[%d] idx[%d] mode[%d] Fail(%d)\n",
  324. __func__, desc->volt_table[sel], sel,
  325. info->opp_sel, ret);
  326. return ret;
  327. }
  328. /**
  329. * ti_abb_get_voltage_sel() - Regulator accessor to get current ABB LDO setting
  330. * @rdev: regulator device
  331. *
  332. * Return: 0 on success or appropriate error value when fails
  333. */
  334. static int ti_abb_get_voltage_sel(struct regulator_dev *rdev)
  335. {
  336. const struct regulator_desc *desc = rdev->desc;
  337. struct ti_abb *abb = rdev_get_drvdata(rdev);
  338. struct device *dev = &rdev->dev;
  339. if (!abb) {
  340. dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
  341. __func__);
  342. return -ENODEV;
  343. }
  344. if (!desc->n_voltages || !abb->info) {
  345. dev_err_ratelimited(dev,
  346. "%s: No valid voltage table entries?\n",
  347. __func__);
  348. return -EINVAL;
  349. }
  350. if (abb->current_info_idx >= (int)desc->n_voltages) {
  351. dev_err(dev, "%s: Corrupted data? idx(%d) >= n_voltages(%d)\n",
  352. __func__, abb->current_info_idx, desc->n_voltages);
  353. return -EINVAL;
  354. }
  355. return abb->current_info_idx;
  356. }
  357. /**
  358. * ti_abb_init_timings() - setup ABB clock timing for the current platform
  359. * @dev: device
  360. * @abb: pointer to the abb instance
  361. *
  362. * Return: 0 if timing is updated, else returns error result.
  363. */
  364. static int ti_abb_init_timings(struct device *dev, struct ti_abb *abb)
  365. {
  366. u32 clock_cycles;
  367. u32 clk_rate, sr2_wt_cnt_val, cycle_rate;
  368. const struct ti_abb_reg *regs = abb->regs;
  369. int ret;
  370. char *pname = "ti,settling-time";
  371. /* read device tree properties */
  372. ret = of_property_read_u32(dev->of_node, pname, &abb->settling_time);
  373. if (ret) {
  374. dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
  375. return ret;
  376. }
  377. /* ABB LDO cannot be settle in 0 time */
  378. if (!abb->settling_time) {
  379. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  380. return -EINVAL;
  381. }
  382. pname = "ti,clock-cycles";
  383. ret = of_property_read_u32(dev->of_node, pname, &clock_cycles);
  384. if (ret) {
  385. dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
  386. return ret;
  387. }
  388. /* ABB LDO cannot be settle in 0 clock cycles */
  389. if (!clock_cycles) {
  390. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  391. return -EINVAL;
  392. }
  393. abb->clk = devm_clk_get(dev, NULL);
  394. if (IS_ERR(abb->clk)) {
  395. ret = PTR_ERR(abb->clk);
  396. dev_err(dev, "%s: Unable to get clk(%d)\n", __func__, ret);
  397. return ret;
  398. }
  399. /*
  400. * SR2_WTCNT_VALUE is the settling time for the ABB ldo after a
  401. * transition and must be programmed with the correct time at boot.
  402. * The value programmed into the register is the number of SYS_CLK
  403. * clock cycles that match a given wall time profiled for the ldo.
  404. * This value depends on:
  405. * settling time of ldo in micro-seconds (varies per OMAP family)
  406. * # of clock cycles per SYS_CLK period (varies per OMAP family)
  407. * the SYS_CLK frequency in MHz (varies per board)
  408. * The formula is:
  409. *
  410. * ldo settling time (in micro-seconds)
  411. * SR2_WTCNT_VALUE = ------------------------------------------
  412. * (# system clock cycles) * (sys_clk period)
  413. *
  414. * Put another way:
  415. *
  416. * SR2_WTCNT_VALUE = settling time / (# SYS_CLK cycles / SYS_CLK rate))
  417. *
  418. * To avoid dividing by zero multiply both "# clock cycles" and
  419. * "settling time" by 10 such that the final result is the one we want.
  420. */
  421. /* Convert SYS_CLK rate to MHz & prevent divide by zero */
  422. clk_rate = DIV_ROUND_CLOSEST(clk_get_rate(abb->clk), 1000000);
  423. /* Calculate cycle rate */
  424. cycle_rate = DIV_ROUND_CLOSEST(clock_cycles * 10, clk_rate);
  425. /* Calulate SR2_WTCNT_VALUE */
  426. sr2_wt_cnt_val = DIV_ROUND_CLOSEST(abb->settling_time * 10, cycle_rate);
  427. dev_dbg(dev, "%s: Clk_rate=%ld, sr2_cnt=0x%08x\n", __func__,
  428. clk_get_rate(abb->clk), sr2_wt_cnt_val);
  429. ti_abb_rmw(regs->sr2_wtcnt_value_mask, sr2_wt_cnt_val, abb->setup_reg);
  430. return 0;
  431. }
  432. /**
  433. * ti_abb_init_table() - Initialize ABB table from device tree
  434. * @dev: device
  435. * @abb: pointer to the abb instance
  436. * @rinit_data: regulator initdata
  437. *
  438. * Return: 0 on success or appropriate error value when fails
  439. */
  440. static int ti_abb_init_table(struct device *dev, struct ti_abb *abb,
  441. struct regulator_init_data *rinit_data)
  442. {
  443. struct ti_abb_info *info;
  444. const u32 num_values = 6;
  445. char *pname = "ti,abb_info";
  446. u32 i;
  447. unsigned int *volt_table;
  448. int num_entries, min_uV = INT_MAX, max_uV = 0;
  449. struct regulation_constraints *c = &rinit_data->constraints;
  450. /*
  451. * Each abb_info is a set of n-tuple, where n is num_values, consisting
  452. * of voltage and a set of detection logic for ABB information for that
  453. * voltage to apply.
  454. */
  455. num_entries = of_property_count_u32_elems(dev->of_node, pname);
  456. if (num_entries < 0) {
  457. dev_err(dev, "No '%s' property?\n", pname);
  458. return num_entries;
  459. }
  460. if (!num_entries || (num_entries % num_values)) {
  461. dev_err(dev, "All '%s' list entries need %d vals\n", pname,
  462. num_values);
  463. return -EINVAL;
  464. }
  465. num_entries /= num_values;
  466. info = devm_kzalloc(dev, sizeof(*info) * num_entries, GFP_KERNEL);
  467. if (!info)
  468. return -ENOMEM;
  469. abb->info = info;
  470. volt_table = devm_kzalloc(dev, sizeof(unsigned int) * num_entries,
  471. GFP_KERNEL);
  472. if (!volt_table)
  473. return -ENOMEM;
  474. abb->rdesc.n_voltages = num_entries;
  475. abb->rdesc.volt_table = volt_table;
  476. /* We do not know where the OPP voltage is at the moment */
  477. abb->current_info_idx = -EINVAL;
  478. for (i = 0; i < num_entries; i++, info++, volt_table++) {
  479. u32 efuse_offset, rbb_mask, fbb_mask, vset_mask;
  480. u32 efuse_val;
  481. /* NOTE: num_values should equal to entries picked up here */
  482. of_property_read_u32_index(dev->of_node, pname, i * num_values,
  483. volt_table);
  484. of_property_read_u32_index(dev->of_node, pname,
  485. i * num_values + 1, &info->opp_sel);
  486. of_property_read_u32_index(dev->of_node, pname,
  487. i * num_values + 2, &efuse_offset);
  488. of_property_read_u32_index(dev->of_node, pname,
  489. i * num_values + 3, &rbb_mask);
  490. of_property_read_u32_index(dev->of_node, pname,
  491. i * num_values + 4, &fbb_mask);
  492. of_property_read_u32_index(dev->of_node, pname,
  493. i * num_values + 5, &vset_mask);
  494. dev_dbg(dev,
  495. "[%d]v=%d ABB=%d ef=0x%x rbb=0x%x fbb=0x%x vset=0x%x\n",
  496. i, *volt_table, info->opp_sel, efuse_offset, rbb_mask,
  497. fbb_mask, vset_mask);
  498. /* Find min/max for voltage set */
  499. if (min_uV > *volt_table)
  500. min_uV = *volt_table;
  501. if (max_uV < *volt_table)
  502. max_uV = *volt_table;
  503. if (!abb->efuse_base) {
  504. /* Ignore invalid data, but warn to help cleanup */
  505. if (efuse_offset || rbb_mask || fbb_mask || vset_mask)
  506. dev_err(dev, "prop '%s': v=%d,bad efuse/mask\n",
  507. pname, *volt_table);
  508. goto check_abb;
  509. }
  510. efuse_val = readl(abb->efuse_base + efuse_offset);
  511. /* Use ABB recommendation from Efuse */
  512. if (efuse_val & rbb_mask)
  513. info->opp_sel = TI_ABB_SLOW_OPP;
  514. else if (efuse_val & fbb_mask)
  515. info->opp_sel = TI_ABB_FAST_OPP;
  516. else if (rbb_mask || fbb_mask)
  517. info->opp_sel = TI_ABB_NOMINAL_OPP;
  518. dev_dbg(dev,
  519. "[%d]v=%d efusev=0x%x final ABB=%d\n",
  520. i, *volt_table, efuse_val, info->opp_sel);
  521. /* Use recommended Vset bits from Efuse */
  522. if (!abb->ldo_base) {
  523. if (vset_mask)
  524. dev_err(dev, "prop'%s':v=%d vst=%x LDO base?\n",
  525. pname, *volt_table, vset_mask);
  526. continue;
  527. }
  528. info->vset = (efuse_val & vset_mask) >> __ffs(vset_mask);
  529. dev_dbg(dev, "[%d]v=%d vset=%x\n", i, *volt_table, info->vset);
  530. check_abb:
  531. switch (info->opp_sel) {
  532. case TI_ABB_NOMINAL_OPP:
  533. case TI_ABB_FAST_OPP:
  534. case TI_ABB_SLOW_OPP:
  535. /* Valid values */
  536. break;
  537. default:
  538. dev_err(dev, "%s:[%d]v=%d, ABB=%d is invalid! Abort!\n",
  539. __func__, i, *volt_table, info->opp_sel);
  540. return -EINVAL;
  541. }
  542. }
  543. /* Setup the min/max voltage constraints from the supported list */
  544. c->min_uV = min_uV;
  545. c->max_uV = max_uV;
  546. return 0;
  547. }
  548. static struct regulator_ops ti_abb_reg_ops = {
  549. .list_voltage = regulator_list_voltage_table,
  550. .set_voltage_sel = ti_abb_set_voltage_sel,
  551. .get_voltage_sel = ti_abb_get_voltage_sel,
  552. };
  553. /* Default ABB block offsets, IF this changes in future, create new one */
  554. static const struct ti_abb_reg abb_regs_v1 = {
  555. /* WARNING: registers are wrongly documented in TRM */
  556. .setup_off = 0x04,
  557. .control_off = 0x00,
  558. .sr2_wtcnt_value_mask = (0xff << 8),
  559. .fbb_sel_mask = (0x01 << 2),
  560. .rbb_sel_mask = (0x01 << 1),
  561. .sr2_en_mask = (0x01 << 0),
  562. .opp_change_mask = (0x01 << 2),
  563. .opp_sel_mask = (0x03 << 0),
  564. };
  565. static const struct ti_abb_reg abb_regs_v2 = {
  566. .setup_off = 0x00,
  567. .control_off = 0x04,
  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 ti_abb_reg abb_regs_generic = {
  576. .sr2_wtcnt_value_mask = (0xff << 8),
  577. .fbb_sel_mask = (0x01 << 2),
  578. .rbb_sel_mask = (0x01 << 1),
  579. .sr2_en_mask = (0x01 << 0),
  580. .opp_change_mask = (0x01 << 2),
  581. .opp_sel_mask = (0x03 << 0),
  582. };
  583. static const struct of_device_id ti_abb_of_match[] = {
  584. {.compatible = "ti,abb-v1", .data = &abb_regs_v1},
  585. {.compatible = "ti,abb-v2", .data = &abb_regs_v2},
  586. {.compatible = "ti,abb-v3", .data = &abb_regs_generic},
  587. { },
  588. };
  589. MODULE_DEVICE_TABLE(of, ti_abb_of_match);
  590. /**
  591. * ti_abb_probe() - Initialize an ABB ldo instance
  592. * @pdev: ABB platform device
  593. *
  594. * Initializes an individual ABB LDO for required Body-Bias. ABB is used to
  595. * addional bias supply to SoC modules for power savings or mandatory stability
  596. * configuration at certain Operating Performance Points(OPPs).
  597. *
  598. * Return: 0 on success or appropriate error value when fails
  599. */
  600. static int ti_abb_probe(struct platform_device *pdev)
  601. {
  602. struct device *dev = &pdev->dev;
  603. const struct of_device_id *match;
  604. struct resource *res;
  605. struct ti_abb *abb;
  606. struct regulator_init_data *initdata = NULL;
  607. struct regulator_dev *rdev = NULL;
  608. struct regulator_desc *desc;
  609. struct regulation_constraints *c;
  610. struct regulator_config config = { };
  611. char *pname;
  612. int ret = 0;
  613. match = of_match_device(ti_abb_of_match, dev);
  614. if (!match) {
  615. /* We do not expect this to happen */
  616. dev_err(dev, "%s: Unable to match device\n", __func__);
  617. return -ENODEV;
  618. }
  619. if (!match->data) {
  620. dev_err(dev, "%s: Bad data in match\n", __func__);
  621. return -EINVAL;
  622. }
  623. abb = devm_kzalloc(dev, sizeof(struct ti_abb), GFP_KERNEL);
  624. if (!abb)
  625. return -ENOMEM;
  626. abb->regs = match->data;
  627. /* Map ABB resources */
  628. if (abb->regs->setup_off || abb->regs->control_off) {
  629. pname = "base-address";
  630. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  631. abb->base = devm_ioremap_resource(dev, res);
  632. if (IS_ERR(abb->base))
  633. return PTR_ERR(abb->base);
  634. abb->setup_reg = abb->base + abb->regs->setup_off;
  635. abb->control_reg = abb->base + abb->regs->control_off;
  636. } else {
  637. pname = "control-address";
  638. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  639. abb->control_reg = devm_ioremap_resource(dev, res);
  640. if (IS_ERR(abb->control_reg))
  641. return PTR_ERR(abb->control_reg);
  642. pname = "setup-address";
  643. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  644. abb->setup_reg = devm_ioremap_resource(dev, res);
  645. if (IS_ERR(abb->setup_reg))
  646. return PTR_ERR(abb->setup_reg);
  647. }
  648. pname = "int-address";
  649. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  650. if (!res) {
  651. dev_err(dev, "Missing '%s' IO resource\n", pname);
  652. return -ENODEV;
  653. }
  654. /*
  655. * We may have shared interrupt register offsets which are
  656. * write-1-to-clear between domains ensuring exclusivity.
  657. */
  658. abb->int_base = devm_ioremap_nocache(dev, res->start,
  659. resource_size(res));
  660. if (!abb->int_base) {
  661. dev_err(dev, "Unable to map '%s'\n", pname);
  662. return -ENOMEM;
  663. }
  664. /* Map Optional resources */
  665. pname = "efuse-address";
  666. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  667. if (!res) {
  668. dev_dbg(dev, "Missing '%s' IO resource\n", pname);
  669. ret = -ENODEV;
  670. goto skip_opt;
  671. }
  672. /*
  673. * We may have shared efuse register offsets which are read-only
  674. * between domains
  675. */
  676. abb->efuse_base = devm_ioremap_nocache(dev, res->start,
  677. resource_size(res));
  678. if (!abb->efuse_base) {
  679. dev_err(dev, "Unable to map '%s'\n", pname);
  680. return -ENOMEM;
  681. }
  682. pname = "ldo-address";
  683. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  684. if (!res) {
  685. dev_dbg(dev, "Missing '%s' IO resource\n", pname);
  686. ret = -ENODEV;
  687. goto skip_opt;
  688. }
  689. abb->ldo_base = devm_ioremap_resource(dev, res);
  690. if (IS_ERR(abb->ldo_base))
  691. return PTR_ERR(abb->ldo_base);
  692. /* IF ldo_base is set, the following are mandatory */
  693. pname = "ti,ldovbb-override-mask";
  694. ret =
  695. of_property_read_u32(pdev->dev.of_node, pname,
  696. &abb->ldovbb_override_mask);
  697. if (ret) {
  698. dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
  699. return ret;
  700. }
  701. if (!abb->ldovbb_override_mask) {
  702. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  703. return -EINVAL;
  704. }
  705. pname = "ti,ldovbb-vset-mask";
  706. ret =
  707. of_property_read_u32(pdev->dev.of_node, pname,
  708. &abb->ldovbb_vset_mask);
  709. if (ret) {
  710. dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
  711. return ret;
  712. }
  713. if (!abb->ldovbb_vset_mask) {
  714. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  715. return -EINVAL;
  716. }
  717. skip_opt:
  718. pname = "ti,tranxdone-status-mask";
  719. ret =
  720. of_property_read_u32(pdev->dev.of_node, pname,
  721. &abb->txdone_mask);
  722. if (ret) {
  723. dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
  724. return ret;
  725. }
  726. if (!abb->txdone_mask) {
  727. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  728. return -EINVAL;
  729. }
  730. initdata = of_get_regulator_init_data(dev, pdev->dev.of_node,
  731. &abb->rdesc);
  732. if (!initdata) {
  733. dev_err(dev, "%s: Unable to alloc regulator init data\n",
  734. __func__);
  735. return -ENOMEM;
  736. }
  737. /* init ABB opp_sel table */
  738. ret = ti_abb_init_table(dev, abb, initdata);
  739. if (ret)
  740. return ret;
  741. /* init ABB timing */
  742. ret = ti_abb_init_timings(dev, abb);
  743. if (ret)
  744. return ret;
  745. desc = &abb->rdesc;
  746. desc->name = dev_name(dev);
  747. desc->owner = THIS_MODULE;
  748. desc->type = REGULATOR_VOLTAGE;
  749. desc->ops = &ti_abb_reg_ops;
  750. c = &initdata->constraints;
  751. if (desc->n_voltages > 1)
  752. c->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  753. c->always_on = true;
  754. config.dev = dev;
  755. config.init_data = initdata;
  756. config.driver_data = abb;
  757. config.of_node = pdev->dev.of_node;
  758. rdev = devm_regulator_register(dev, desc, &config);
  759. if (IS_ERR(rdev)) {
  760. ret = PTR_ERR(rdev);
  761. dev_err(dev, "%s: failed to register regulator(%d)\n",
  762. __func__, ret);
  763. return ret;
  764. }
  765. platform_set_drvdata(pdev, rdev);
  766. /* Enable the ldo if not already done by bootloader */
  767. ti_abb_rmw(abb->regs->sr2_en_mask, 1, abb->setup_reg);
  768. return 0;
  769. }
  770. MODULE_ALIAS("platform:ti_abb");
  771. static struct platform_driver ti_abb_driver = {
  772. .probe = ti_abb_probe,
  773. .driver = {
  774. .name = "ti_abb",
  775. .of_match_table = of_match_ptr(ti_abb_of_match),
  776. },
  777. };
  778. module_platform_driver(ti_abb_driver);
  779. MODULE_DESCRIPTION("Texas Instruments ABB LDO regulator driver");
  780. MODULE_AUTHOR("Texas Instruments Inc.");
  781. MODULE_LICENSE("GPL v2");