ti-abb-regulator.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. 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. info = &abb->info[sel];
  302. /*
  303. * When Linux kernel is starting up, we are'nt sure of the
  304. * Bias configuration that bootloader has configured.
  305. * So, we get to know the actual setting the first time
  306. * we are asked to transition.
  307. */
  308. if (abb->current_info_idx == -EINVAL)
  309. goto just_set_abb;
  310. /* If data is exactly the same, then just update index, no change */
  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. just_set_abb:
  318. ret = ti_abb_set_opp(rdev, abb, info);
  319. out:
  320. if (!ret)
  321. abb->current_info_idx = sel;
  322. else
  323. dev_err_ratelimited(dev,
  324. "%s: Volt[%d] idx[%d] mode[%d] Fail(%d)\n",
  325. __func__, desc->volt_table[sel], sel,
  326. info->opp_sel, ret);
  327. return ret;
  328. }
  329. /**
  330. * ti_abb_get_voltage_sel() - Regulator accessor to get current ABB LDO setting
  331. * @rdev: regulator device
  332. *
  333. * Return: 0 on success or appropriate error value when fails
  334. */
  335. static int ti_abb_get_voltage_sel(struct regulator_dev *rdev)
  336. {
  337. const struct regulator_desc *desc = rdev->desc;
  338. struct ti_abb *abb = rdev_get_drvdata(rdev);
  339. struct device *dev = &rdev->dev;
  340. if (!abb) {
  341. dev_err_ratelimited(dev, "%s: No regulator drvdata\n",
  342. __func__);
  343. return -ENODEV;
  344. }
  345. if (!desc->n_voltages || !abb->info) {
  346. dev_err_ratelimited(dev,
  347. "%s: No valid voltage table entries?\n",
  348. __func__);
  349. return -EINVAL;
  350. }
  351. if (abb->current_info_idx >= (int)desc->n_voltages) {
  352. dev_err(dev, "%s: Corrupted data? idx(%d) >= n_voltages(%d)\n",
  353. __func__, abb->current_info_idx, desc->n_voltages);
  354. return -EINVAL;
  355. }
  356. return abb->current_info_idx;
  357. }
  358. /**
  359. * ti_abb_init_timings() - setup ABB clock timing for the current platform
  360. * @dev: device
  361. * @abb: pointer to the abb instance
  362. *
  363. * Return: 0 if timing is updated, else returns error result.
  364. */
  365. static int ti_abb_init_timings(struct device *dev, struct ti_abb *abb)
  366. {
  367. u32 clock_cycles;
  368. u32 clk_rate, sr2_wt_cnt_val, cycle_rate;
  369. const struct ti_abb_reg *regs = abb->regs;
  370. int ret;
  371. char *pname = "ti,settling-time";
  372. /* read device tree properties */
  373. ret = of_property_read_u32(dev->of_node, pname, &abb->settling_time);
  374. if (ret) {
  375. dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
  376. return ret;
  377. }
  378. /* ABB LDO cannot be settle in 0 time */
  379. if (!abb->settling_time) {
  380. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  381. return -EINVAL;
  382. }
  383. pname = "ti,clock-cycles";
  384. ret = of_property_read_u32(dev->of_node, pname, &clock_cycles);
  385. if (ret) {
  386. dev_err(dev, "Unable to get property '%s'(%d)\n", pname, ret);
  387. return ret;
  388. }
  389. /* ABB LDO cannot be settle in 0 clock cycles */
  390. if (!clock_cycles) {
  391. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  392. return -EINVAL;
  393. }
  394. abb->clk = devm_clk_get(dev, NULL);
  395. if (IS_ERR(abb->clk)) {
  396. ret = PTR_ERR(abb->clk);
  397. dev_err(dev, "%s: Unable to get clk(%d)\n", __func__, ret);
  398. return ret;
  399. }
  400. /*
  401. * SR2_WTCNT_VALUE is the settling time for the ABB ldo after a
  402. * transition and must be programmed with the correct time at boot.
  403. * The value programmed into the register is the number of SYS_CLK
  404. * clock cycles that match a given wall time profiled for the ldo.
  405. * This value depends on:
  406. * settling time of ldo in micro-seconds (varies per OMAP family)
  407. * # of clock cycles per SYS_CLK period (varies per OMAP family)
  408. * the SYS_CLK frequency in MHz (varies per board)
  409. * The formula is:
  410. *
  411. * ldo settling time (in micro-seconds)
  412. * SR2_WTCNT_VALUE = ------------------------------------------
  413. * (# system clock cycles) * (sys_clk period)
  414. *
  415. * Put another way:
  416. *
  417. * SR2_WTCNT_VALUE = settling time / (# SYS_CLK cycles / SYS_CLK rate))
  418. *
  419. * To avoid dividing by zero multiply both "# clock cycles" and
  420. * "settling time" by 10 such that the final result is the one we want.
  421. */
  422. /* Convert SYS_CLK rate to MHz & prevent divide by zero */
  423. clk_rate = DIV_ROUND_CLOSEST(clk_get_rate(abb->clk), 1000000);
  424. /* Calculate cycle rate */
  425. cycle_rate = DIV_ROUND_CLOSEST(clock_cycles * 10, clk_rate);
  426. /* Calulate SR2_WTCNT_VALUE */
  427. sr2_wt_cnt_val = DIV_ROUND_CLOSEST(abb->settling_time * 10, cycle_rate);
  428. dev_dbg(dev, "%s: Clk_rate=%ld, sr2_cnt=0x%08x\n", __func__,
  429. clk_get_rate(abb->clk), sr2_wt_cnt_val);
  430. ti_abb_rmw(regs->sr2_wtcnt_value_mask, sr2_wt_cnt_val, abb->setup_reg);
  431. return 0;
  432. }
  433. /**
  434. * ti_abb_init_table() - Initialize ABB table from device tree
  435. * @dev: device
  436. * @abb: pointer to the abb instance
  437. * @rinit_data: regulator initdata
  438. *
  439. * Return: 0 on success or appropriate error value when fails
  440. */
  441. static int ti_abb_init_table(struct device *dev, struct ti_abb *abb,
  442. struct regulator_init_data *rinit_data)
  443. {
  444. struct ti_abb_info *info;
  445. const u32 num_values = 6;
  446. char *pname = "ti,abb_info";
  447. u32 i;
  448. unsigned int *volt_table;
  449. int num_entries, min_uV = INT_MAX, max_uV = 0;
  450. struct regulation_constraints *c = &rinit_data->constraints;
  451. /*
  452. * Each abb_info is a set of n-tuple, where n is num_values, consisting
  453. * of voltage and a set of detection logic for ABB information for that
  454. * voltage to apply.
  455. */
  456. num_entries = of_property_count_u32_elems(dev->of_node, pname);
  457. if (num_entries < 0) {
  458. dev_err(dev, "No '%s' property?\n", pname);
  459. return num_entries;
  460. }
  461. if (!num_entries || (num_entries % num_values)) {
  462. dev_err(dev, "All '%s' list entries need %d vals\n", pname,
  463. num_values);
  464. return -EINVAL;
  465. }
  466. num_entries /= num_values;
  467. info = devm_kcalloc(dev, num_entries, sizeof(*info), GFP_KERNEL);
  468. if (!info)
  469. return -ENOMEM;
  470. abb->info = info;
  471. volt_table = devm_kcalloc(dev, num_entries, sizeof(unsigned int),
  472. GFP_KERNEL);
  473. if (!volt_table)
  474. return -ENOMEM;
  475. abb->rdesc.n_voltages = num_entries;
  476. abb->rdesc.volt_table = volt_table;
  477. /* We do not know where the OPP voltage is at the moment */
  478. abb->current_info_idx = -EINVAL;
  479. for (i = 0; i < num_entries; i++, info++, volt_table++) {
  480. u32 efuse_offset, rbb_mask, fbb_mask, vset_mask;
  481. u32 efuse_val;
  482. /* NOTE: num_values should equal to entries picked up here */
  483. of_property_read_u32_index(dev->of_node, pname, i * num_values,
  484. volt_table);
  485. of_property_read_u32_index(dev->of_node, pname,
  486. i * num_values + 1, &info->opp_sel);
  487. of_property_read_u32_index(dev->of_node, pname,
  488. i * num_values + 2, &efuse_offset);
  489. of_property_read_u32_index(dev->of_node, pname,
  490. i * num_values + 3, &rbb_mask);
  491. of_property_read_u32_index(dev->of_node, pname,
  492. i * num_values + 4, &fbb_mask);
  493. of_property_read_u32_index(dev->of_node, pname,
  494. i * num_values + 5, &vset_mask);
  495. dev_dbg(dev,
  496. "[%d]v=%d ABB=%d ef=0x%x rbb=0x%x fbb=0x%x vset=0x%x\n",
  497. i, *volt_table, info->opp_sel, efuse_offset, rbb_mask,
  498. fbb_mask, vset_mask);
  499. /* Find min/max for voltage set */
  500. if (min_uV > *volt_table)
  501. min_uV = *volt_table;
  502. if (max_uV < *volt_table)
  503. max_uV = *volt_table;
  504. if (!abb->efuse_base) {
  505. /* Ignore invalid data, but warn to help cleanup */
  506. if (efuse_offset || rbb_mask || fbb_mask || vset_mask)
  507. dev_err(dev, "prop '%s': v=%d,bad efuse/mask\n",
  508. pname, *volt_table);
  509. goto check_abb;
  510. }
  511. efuse_val = readl(abb->efuse_base + efuse_offset);
  512. /* Use ABB recommendation from Efuse */
  513. if (efuse_val & rbb_mask)
  514. info->opp_sel = TI_ABB_SLOW_OPP;
  515. else if (efuse_val & fbb_mask)
  516. info->opp_sel = TI_ABB_FAST_OPP;
  517. else if (rbb_mask || fbb_mask)
  518. info->opp_sel = TI_ABB_NOMINAL_OPP;
  519. dev_dbg(dev,
  520. "[%d]v=%d efusev=0x%x final ABB=%d\n",
  521. i, *volt_table, efuse_val, info->opp_sel);
  522. /* Use recommended Vset bits from Efuse */
  523. if (!abb->ldo_base) {
  524. if (vset_mask)
  525. dev_err(dev, "prop'%s':v=%d vst=%x LDO base?\n",
  526. pname, *volt_table, vset_mask);
  527. continue;
  528. }
  529. info->vset = (efuse_val & vset_mask) >> __ffs(vset_mask);
  530. dev_dbg(dev, "[%d]v=%d vset=%x\n", i, *volt_table, info->vset);
  531. check_abb:
  532. switch (info->opp_sel) {
  533. case TI_ABB_NOMINAL_OPP:
  534. case TI_ABB_FAST_OPP:
  535. case TI_ABB_SLOW_OPP:
  536. /* Valid values */
  537. break;
  538. default:
  539. dev_err(dev, "%s:[%d]v=%d, ABB=%d is invalid! Abort!\n",
  540. __func__, i, *volt_table, info->opp_sel);
  541. return -EINVAL;
  542. }
  543. }
  544. /* Setup the min/max voltage constraints from the supported list */
  545. c->min_uV = min_uV;
  546. c->max_uV = max_uV;
  547. return 0;
  548. }
  549. static const struct regulator_ops ti_abb_reg_ops = {
  550. .list_voltage = regulator_list_voltage_table,
  551. .set_voltage_sel = ti_abb_set_voltage_sel,
  552. .get_voltage_sel = ti_abb_get_voltage_sel,
  553. };
  554. /* Default ABB block offsets, IF this changes in future, create new one */
  555. static const struct ti_abb_reg abb_regs_v1 = {
  556. /* WARNING: registers are wrongly documented in TRM */
  557. .setup_off = 0x04,
  558. .control_off = 0x00,
  559. .sr2_wtcnt_value_mask = (0xff << 8),
  560. .fbb_sel_mask = (0x01 << 2),
  561. .rbb_sel_mask = (0x01 << 1),
  562. .sr2_en_mask = (0x01 << 0),
  563. .opp_change_mask = (0x01 << 2),
  564. .opp_sel_mask = (0x03 << 0),
  565. };
  566. static const struct ti_abb_reg abb_regs_v2 = {
  567. .setup_off = 0x00,
  568. .control_off = 0x04,
  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 ti_abb_reg abb_regs_generic = {
  577. .sr2_wtcnt_value_mask = (0xff << 8),
  578. .fbb_sel_mask = (0x01 << 2),
  579. .rbb_sel_mask = (0x01 << 1),
  580. .sr2_en_mask = (0x01 << 0),
  581. .opp_change_mask = (0x01 << 2),
  582. .opp_sel_mask = (0x03 << 0),
  583. };
  584. static const struct of_device_id ti_abb_of_match[] = {
  585. {.compatible = "ti,abb-v1", .data = &abb_regs_v1},
  586. {.compatible = "ti,abb-v2", .data = &abb_regs_v2},
  587. {.compatible = "ti,abb-v3", .data = &abb_regs_generic},
  588. { },
  589. };
  590. MODULE_DEVICE_TABLE(of, ti_abb_of_match);
  591. /**
  592. * ti_abb_probe() - Initialize an ABB ldo instance
  593. * @pdev: ABB platform device
  594. *
  595. * Initializes an individual ABB LDO for required Body-Bias. ABB is used to
  596. * addional bias supply to SoC modules for power savings or mandatory stability
  597. * configuration at certain Operating Performance Points(OPPs).
  598. *
  599. * Return: 0 on success or appropriate error value when fails
  600. */
  601. static int ti_abb_probe(struct platform_device *pdev)
  602. {
  603. struct device *dev = &pdev->dev;
  604. const struct of_device_id *match;
  605. struct resource *res;
  606. struct ti_abb *abb;
  607. struct regulator_init_data *initdata = NULL;
  608. struct regulator_dev *rdev = NULL;
  609. struct regulator_desc *desc;
  610. struct regulation_constraints *c;
  611. struct regulator_config config = { };
  612. char *pname;
  613. int ret = 0;
  614. match = of_match_device(ti_abb_of_match, dev);
  615. if (!match) {
  616. /* We do not expect this to happen */
  617. dev_err(dev, "%s: Unable to match device\n", __func__);
  618. return -ENODEV;
  619. }
  620. if (!match->data) {
  621. dev_err(dev, "%s: Bad data in match\n", __func__);
  622. return -EINVAL;
  623. }
  624. abb = devm_kzalloc(dev, sizeof(struct ti_abb), GFP_KERNEL);
  625. if (!abb)
  626. return -ENOMEM;
  627. abb->regs = match->data;
  628. /* Map ABB resources */
  629. if (abb->regs->setup_off || abb->regs->control_off) {
  630. pname = "base-address";
  631. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  632. abb->base = devm_ioremap_resource(dev, res);
  633. if (IS_ERR(abb->base))
  634. return PTR_ERR(abb->base);
  635. abb->setup_reg = abb->base + abb->regs->setup_off;
  636. abb->control_reg = abb->base + abb->regs->control_off;
  637. } else {
  638. pname = "control-address";
  639. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  640. abb->control_reg = devm_ioremap_resource(dev, res);
  641. if (IS_ERR(abb->control_reg))
  642. return PTR_ERR(abb->control_reg);
  643. pname = "setup-address";
  644. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  645. abb->setup_reg = devm_ioremap_resource(dev, res);
  646. if (IS_ERR(abb->setup_reg))
  647. return PTR_ERR(abb->setup_reg);
  648. }
  649. pname = "int-address";
  650. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  651. if (!res) {
  652. dev_err(dev, "Missing '%s' IO resource\n", pname);
  653. return -ENODEV;
  654. }
  655. /*
  656. * We may have shared interrupt register offsets which are
  657. * write-1-to-clear between domains ensuring exclusivity.
  658. */
  659. abb->int_base = devm_ioremap(dev, res->start,
  660. resource_size(res));
  661. if (!abb->int_base) {
  662. dev_err(dev, "Unable to map '%s'\n", pname);
  663. return -ENOMEM;
  664. }
  665. /* Map Optional resources */
  666. pname = "efuse-address";
  667. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  668. if (!res) {
  669. dev_dbg(dev, "Missing '%s' IO resource\n", pname);
  670. ret = -ENODEV;
  671. goto skip_opt;
  672. }
  673. /*
  674. * We may have shared efuse register offsets which are read-only
  675. * between domains
  676. */
  677. abb->efuse_base = devm_ioremap(dev, res->start,
  678. resource_size(res));
  679. if (!abb->efuse_base) {
  680. dev_err(dev, "Unable to map '%s'\n", pname);
  681. return -ENOMEM;
  682. }
  683. pname = "ldo-address";
  684. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  685. if (!res) {
  686. dev_dbg(dev, "Missing '%s' IO resource\n", pname);
  687. ret = -ENODEV;
  688. goto skip_opt;
  689. }
  690. abb->ldo_base = devm_ioremap_resource(dev, res);
  691. if (IS_ERR(abb->ldo_base))
  692. return PTR_ERR(abb->ldo_base);
  693. /* IF ldo_base is set, the following are mandatory */
  694. pname = "ti,ldovbb-override-mask";
  695. ret =
  696. of_property_read_u32(pdev->dev.of_node, pname,
  697. &abb->ldovbb_override_mask);
  698. if (ret) {
  699. dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
  700. return ret;
  701. }
  702. if (!abb->ldovbb_override_mask) {
  703. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  704. return -EINVAL;
  705. }
  706. pname = "ti,ldovbb-vset-mask";
  707. ret =
  708. of_property_read_u32(pdev->dev.of_node, pname,
  709. &abb->ldovbb_vset_mask);
  710. if (ret) {
  711. dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
  712. return ret;
  713. }
  714. if (!abb->ldovbb_vset_mask) {
  715. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  716. return -EINVAL;
  717. }
  718. skip_opt:
  719. pname = "ti,tranxdone-status-mask";
  720. ret =
  721. of_property_read_u32(pdev->dev.of_node, pname,
  722. &abb->txdone_mask);
  723. if (ret) {
  724. dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
  725. return ret;
  726. }
  727. if (!abb->txdone_mask) {
  728. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  729. return -EINVAL;
  730. }
  731. initdata = of_get_regulator_init_data(dev, pdev->dev.of_node,
  732. &abb->rdesc);
  733. if (!initdata) {
  734. dev_err(dev, "%s: Unable to alloc regulator init data\n",
  735. __func__);
  736. return -ENOMEM;
  737. }
  738. /* init ABB opp_sel table */
  739. ret = ti_abb_init_table(dev, abb, initdata);
  740. if (ret)
  741. return ret;
  742. /* init ABB timing */
  743. ret = ti_abb_init_timings(dev, abb);
  744. if (ret)
  745. return ret;
  746. desc = &abb->rdesc;
  747. desc->name = dev_name(dev);
  748. desc->owner = THIS_MODULE;
  749. desc->type = REGULATOR_VOLTAGE;
  750. desc->ops = &ti_abb_reg_ops;
  751. c = &initdata->constraints;
  752. if (desc->n_voltages > 1)
  753. c->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  754. c->always_on = true;
  755. config.dev = dev;
  756. config.init_data = initdata;
  757. config.driver_data = abb;
  758. config.of_node = pdev->dev.of_node;
  759. rdev = devm_regulator_register(dev, desc, &config);
  760. if (IS_ERR(rdev)) {
  761. ret = PTR_ERR(rdev);
  762. dev_err(dev, "%s: failed to register regulator(%d)\n",
  763. __func__, ret);
  764. return ret;
  765. }
  766. platform_set_drvdata(pdev, rdev);
  767. /* Enable the ldo if not already done by bootloader */
  768. ti_abb_rmw(abb->regs->sr2_en_mask, 1, abb->setup_reg);
  769. return 0;
  770. }
  771. MODULE_ALIAS("platform:ti_abb");
  772. static struct platform_driver ti_abb_driver = {
  773. .probe = ti_abb_probe,
  774. .driver = {
  775. .name = "ti_abb",
  776. .of_match_table = of_match_ptr(ti_abb_of_match),
  777. },
  778. };
  779. module_platform_driver(ti_abb_driver);
  780. MODULE_DESCRIPTION("Texas Instruments ABB LDO regulator driver");
  781. MODULE_AUTHOR("Texas Instruments Inc.");
  782. MODULE_LICENSE("GPL v2");