ti-abb-regulator.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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_reg: setup register offset from base
  55. * @control_reg: 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_reg;
  65. u32 control_reg;
  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. * @int_base: interrupt register base address
  81. * @efuse_base: (optional) efuse base address for ABB modes
  82. * @ldo_base: (optional) LDOVBB vset override base address
  83. * @regs: pointer to struct ti_abb_reg for ABB block
  84. * @txdone_mask: mask on int_base for tranxdone interrupt
  85. * @ldovbb_override_mask: mask to ldo_base for overriding default LDO VBB
  86. * vset with value from efuse
  87. * @ldovbb_vset_mask: mask to ldo_base for providing the VSET override
  88. * @info: array to per voltage ABB configuration
  89. * @current_info_idx: current index to info
  90. * @settling_time: SoC specific settling time for LDO VBB
  91. */
  92. struct ti_abb {
  93. struct regulator_desc rdesc;
  94. struct clk *clk;
  95. void __iomem *base;
  96. void __iomem *int_base;
  97. void __iomem *efuse_base;
  98. void __iomem *ldo_base;
  99. const struct ti_abb_reg *regs;
  100. u32 txdone_mask;
  101. u32 ldovbb_override_mask;
  102. u32 ldovbb_vset_mask;
  103. struct ti_abb_info *info;
  104. int current_info_idx;
  105. u32 settling_time;
  106. };
  107. /**
  108. * ti_abb_rmw() - handy wrapper to set specific register bits
  109. * @mask: mask for register field
  110. * @value: value shifted to mask location and written
  111. * @offset: offset of register
  112. * @base: base address
  113. *
  114. * Return: final register value (may be unused)
  115. */
  116. static inline u32 ti_abb_rmw(u32 mask, u32 value, u32 offset,
  117. void __iomem *base)
  118. {
  119. u32 val;
  120. val = readl(base + offset);
  121. val &= ~mask;
  122. val |= (value << __ffs(mask)) & mask;
  123. writel(val, base + offset);
  124. return val;
  125. }
  126. /**
  127. * ti_abb_check_txdone() - handy wrapper to check ABB tranxdone status
  128. * @abb: pointer to the abb instance
  129. *
  130. * Return: true or false
  131. */
  132. static inline bool ti_abb_check_txdone(const struct ti_abb *abb)
  133. {
  134. return !!(readl(abb->int_base) & abb->txdone_mask);
  135. }
  136. /**
  137. * ti_abb_clear_txdone() - handy wrapper to clear ABB tranxdone status
  138. * @abb: pointer to the abb instance
  139. */
  140. static inline void ti_abb_clear_txdone(const struct ti_abb *abb)
  141. {
  142. writel(abb->txdone_mask, abb->int_base);
  143. };
  144. /**
  145. * ti_abb_wait_tranx() - waits for ABB tranxdone event
  146. * @dev: device
  147. * @abb: pointer to the abb instance
  148. *
  149. * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
  150. */
  151. static int ti_abb_wait_txdone(struct device *dev, struct ti_abb *abb)
  152. {
  153. int timeout = 0;
  154. bool status;
  155. while (timeout++ <= abb->settling_time) {
  156. status = ti_abb_check_txdone(abb);
  157. if (status)
  158. break;
  159. udelay(1);
  160. }
  161. if (timeout > abb->settling_time) {
  162. dev_warn_ratelimited(dev,
  163. "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
  164. __func__, timeout, readl(abb->int_base));
  165. return -ETIMEDOUT;
  166. }
  167. return 0;
  168. }
  169. /**
  170. * ti_abb_clear_all_txdone() - clears ABB tranxdone event
  171. * @dev: device
  172. * @abb: pointer to the abb instance
  173. *
  174. * Return: 0 on success or -ETIMEDOUT if the event is not cleared on time.
  175. */
  176. static int ti_abb_clear_all_txdone(struct device *dev, const struct ti_abb *abb)
  177. {
  178. int timeout = 0;
  179. bool status;
  180. while (timeout++ <= abb->settling_time) {
  181. ti_abb_clear_txdone(abb);
  182. status = ti_abb_check_txdone(abb);
  183. if (!status)
  184. break;
  185. udelay(1);
  186. }
  187. if (timeout > abb->settling_time) {
  188. dev_warn_ratelimited(dev,
  189. "%s:TRANXDONE timeout(%duS) int=0x%08x\n",
  190. __func__, timeout, readl(abb->int_base));
  191. return -ETIMEDOUT;
  192. }
  193. return 0;
  194. }
  195. /**
  196. * ti_abb_program_ldovbb() - program LDOVBB register for override value
  197. * @dev: device
  198. * @abb: pointer to the abb instance
  199. * @info: ABB info to program
  200. */
  201. static void ti_abb_program_ldovbb(struct device *dev, const struct ti_abb *abb,
  202. struct ti_abb_info *info)
  203. {
  204. u32 val;
  205. val = readl(abb->ldo_base);
  206. /* clear up previous values */
  207. val &= ~(abb->ldovbb_override_mask | abb->ldovbb_vset_mask);
  208. switch (info->opp_sel) {
  209. case TI_ABB_SLOW_OPP:
  210. case TI_ABB_FAST_OPP:
  211. val |= abb->ldovbb_override_mask;
  212. val |= info->vset << __ffs(abb->ldovbb_vset_mask);
  213. break;
  214. }
  215. writel(val, abb->ldo_base);
  216. }
  217. /**
  218. * ti_abb_set_opp() - Setup ABB and LDO VBB for required bias
  219. * @rdev: regulator device
  220. * @abb: pointer to the abb instance
  221. * @info: ABB info to program
  222. *
  223. * Return: 0 on success or appropriate error value when fails
  224. */
  225. static int ti_abb_set_opp(struct regulator_dev *rdev, struct ti_abb *abb,
  226. struct ti_abb_info *info)
  227. {
  228. const struct ti_abb_reg *regs = abb->regs;
  229. struct device *dev = &rdev->dev;
  230. int ret;
  231. ret = ti_abb_clear_all_txdone(dev, abb);
  232. if (ret)
  233. goto out;
  234. ti_abb_rmw(regs->fbb_sel_mask | regs->rbb_sel_mask, 0, regs->setup_reg,
  235. abb->base);
  236. switch (info->opp_sel) {
  237. case TI_ABB_SLOW_OPP:
  238. ti_abb_rmw(regs->rbb_sel_mask, 1, regs->setup_reg, abb->base);
  239. break;
  240. case TI_ABB_FAST_OPP:
  241. ti_abb_rmw(regs->fbb_sel_mask, 1, regs->setup_reg, abb->base);
  242. break;
  243. }
  244. /* program next state of ABB ldo */
  245. ti_abb_rmw(regs->opp_sel_mask, info->opp_sel, regs->control_reg,
  246. abb->base);
  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, regs->control_reg, abb->base);
  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, regs->setup_reg,
  430. abb->base);
  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 struct property *prop;
  446. const __be32 *abb_info;
  447. const u32 num_values = 6;
  448. char *pname = "ti,abb_info";
  449. u32 num_entries, i;
  450. unsigned int *volt_table;
  451. int min_uV = INT_MAX, max_uV = 0;
  452. struct regulation_constraints *c = &rinit_data->constraints;
  453. prop = of_find_property(dev->of_node, pname, NULL);
  454. if (!prop) {
  455. dev_err(dev, "No '%s' property?\n", pname);
  456. return -ENODEV;
  457. }
  458. if (!prop->value) {
  459. dev_err(dev, "Empty '%s' property?\n", pname);
  460. return -ENODATA;
  461. }
  462. /*
  463. * Each abb_info is a set of n-tuple, where n is num_values, consisting
  464. * of voltage and a set of detection logic for ABB information for that
  465. * voltage to apply.
  466. */
  467. num_entries = prop->length / sizeof(u32);
  468. if (!num_entries || (num_entries % num_values)) {
  469. dev_err(dev, "All '%s' list entries need %d vals\n", pname,
  470. num_values);
  471. return -EINVAL;
  472. }
  473. num_entries /= num_values;
  474. info = devm_kzalloc(dev, sizeof(*info) * num_entries, GFP_KERNEL);
  475. if (!info) {
  476. dev_err(dev, "Can't allocate info table for '%s' property\n",
  477. pname);
  478. return -ENOMEM;
  479. }
  480. abb->info = info;
  481. volt_table = devm_kzalloc(dev, sizeof(unsigned int) * num_entries,
  482. GFP_KERNEL);
  483. if (!volt_table) {
  484. dev_err(dev, "Can't allocate voltage table for '%s' property\n",
  485. pname);
  486. return -ENOMEM;
  487. }
  488. abb->rdesc.n_voltages = num_entries;
  489. abb->rdesc.volt_table = volt_table;
  490. /* We do not know where the OPP voltage is at the moment */
  491. abb->current_info_idx = -EINVAL;
  492. abb_info = prop->value;
  493. for (i = 0; i < num_entries; i++, info++, volt_table++) {
  494. u32 efuse_offset, rbb_mask, fbb_mask, vset_mask;
  495. u32 efuse_val;
  496. /* NOTE: num_values should equal to entries picked up here */
  497. *volt_table = be32_to_cpup(abb_info++);
  498. info->opp_sel = be32_to_cpup(abb_info++);
  499. efuse_offset = be32_to_cpup(abb_info++);
  500. rbb_mask = be32_to_cpup(abb_info++);
  501. fbb_mask = be32_to_cpup(abb_info++);
  502. vset_mask = be32_to_cpup(abb_info++);
  503. dev_dbg(dev,
  504. "[%d]v=%d ABB=%d ef=0x%x rbb=0x%x fbb=0x%x vset=0x%x\n",
  505. i, *volt_table, info->opp_sel, efuse_offset, rbb_mask,
  506. fbb_mask, vset_mask);
  507. /* Find min/max for voltage set */
  508. if (min_uV > *volt_table)
  509. min_uV = *volt_table;
  510. if (max_uV < *volt_table)
  511. max_uV = *volt_table;
  512. if (!abb->efuse_base) {
  513. /* Ignore invalid data, but warn to help cleanup */
  514. if (efuse_offset || rbb_mask || fbb_mask || vset_mask)
  515. dev_err(dev, "prop '%s': v=%d,bad efuse/mask\n",
  516. pname, *volt_table);
  517. goto check_abb;
  518. }
  519. efuse_val = readl(abb->efuse_base + efuse_offset);
  520. /* Use ABB recommendation from Efuse */
  521. if (efuse_val & rbb_mask)
  522. info->opp_sel = TI_ABB_SLOW_OPP;
  523. else if (efuse_val & fbb_mask)
  524. info->opp_sel = TI_ABB_FAST_OPP;
  525. else if (rbb_mask || fbb_mask)
  526. info->opp_sel = TI_ABB_NOMINAL_OPP;
  527. dev_dbg(dev,
  528. "[%d]v=%d efusev=0x%x final ABB=%d\n",
  529. i, *volt_table, efuse_val, info->opp_sel);
  530. /* Use recommended Vset bits from Efuse */
  531. if (!abb->ldo_base) {
  532. if (vset_mask)
  533. dev_err(dev, "prop'%s':v=%d vst=%x LDO base?\n",
  534. pname, *volt_table, vset_mask);
  535. continue;
  536. }
  537. info->vset = efuse_val & vset_mask >> __ffs(vset_mask);
  538. dev_dbg(dev, "[%d]v=%d vset=%x\n", i, *volt_table, info->vset);
  539. check_abb:
  540. switch (info->opp_sel) {
  541. case TI_ABB_NOMINAL_OPP:
  542. case TI_ABB_FAST_OPP:
  543. case TI_ABB_SLOW_OPP:
  544. /* Valid values */
  545. break;
  546. default:
  547. dev_err(dev, "%s:[%d]v=%d, ABB=%d is invalid! Abort!\n",
  548. __func__, i, *volt_table, info->opp_sel);
  549. return -EINVAL;
  550. }
  551. }
  552. /* Setup the min/max voltage constraints from the supported list */
  553. c->min_uV = min_uV;
  554. c->max_uV = max_uV;
  555. return 0;
  556. }
  557. static struct regulator_ops ti_abb_reg_ops = {
  558. .list_voltage = regulator_list_voltage_table,
  559. .set_voltage_sel = ti_abb_set_voltage_sel,
  560. .get_voltage_sel = ti_abb_get_voltage_sel,
  561. };
  562. /* Default ABB block offsets, IF this changes in future, create new one */
  563. static const struct ti_abb_reg abb_regs_v1 = {
  564. /* WARNING: registers are wrongly documented in TRM */
  565. .setup_reg = 0x04,
  566. .control_reg = 0x00,
  567. .sr2_wtcnt_value_mask = (0xff << 8),
  568. .fbb_sel_mask = (0x01 << 2),
  569. .rbb_sel_mask = (0x01 << 1),
  570. .sr2_en_mask = (0x01 << 0),
  571. .opp_change_mask = (0x01 << 2),
  572. .opp_sel_mask = (0x03 << 0),
  573. };
  574. static const struct ti_abb_reg abb_regs_v2 = {
  575. .setup_reg = 0x00,
  576. .control_reg = 0x04,
  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. { },
  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. ret = -ENODEV;
  617. dev_err(dev, "%s: Unable to match device\n", __func__);
  618. goto err;
  619. }
  620. if (!match->data) {
  621. ret = -EINVAL;
  622. dev_err(dev, "%s: Bad data in match\n", __func__);
  623. goto err;
  624. }
  625. abb = devm_kzalloc(dev, sizeof(struct ti_abb), GFP_KERNEL);
  626. if (!abb) {
  627. dev_err(dev, "%s: Unable to allocate ABB struct\n", __func__);
  628. ret = -ENOMEM;
  629. goto err;
  630. }
  631. abb->regs = match->data;
  632. /* Map ABB resources */
  633. pname = "base-address";
  634. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  635. abb->base = devm_ioremap_resource(dev, res);
  636. if (IS_ERR(abb->base)) {
  637. ret = PTR_ERR(abb->base);
  638. goto err;
  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. ret = -ENODEV;
  645. goto err;
  646. }
  647. /*
  648. * We may have shared interrupt register offsets which are
  649. * write-1-to-clear between domains ensuring exclusivity.
  650. */
  651. abb->int_base = devm_ioremap_nocache(dev, res->start,
  652. resource_size(res));
  653. if (!abb->int_base) {
  654. dev_err(dev, "Unable to map '%s'\n", pname);
  655. ret = -ENOMEM;
  656. goto err;
  657. }
  658. /* Map Optional resources */
  659. pname = "efuse-address";
  660. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  661. if (!res) {
  662. dev_dbg(dev, "Missing '%s' IO resource\n", pname);
  663. ret = -ENODEV;
  664. goto skip_opt;
  665. }
  666. /*
  667. * We may have shared efuse register offsets which are read-only
  668. * between domains
  669. */
  670. abb->efuse_base = devm_ioremap_nocache(dev, res->start,
  671. resource_size(res));
  672. if (!abb->efuse_base) {
  673. dev_err(dev, "Unable to map '%s'\n", pname);
  674. ret = -ENOMEM;
  675. goto err;
  676. }
  677. pname = "ldo-address";
  678. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, pname);
  679. abb->ldo_base = devm_ioremap_resource(dev, res);
  680. if (IS_ERR(abb->ldo_base)) {
  681. ret = PTR_ERR(abb->ldo_base);
  682. goto err;
  683. }
  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. goto err;
  692. }
  693. if (!abb->ldovbb_override_mask) {
  694. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  695. ret = -EINVAL;
  696. goto err;
  697. }
  698. pname = "ti,ldovbb-vset-mask";
  699. ret =
  700. of_property_read_u32(pdev->dev.of_node, pname,
  701. &abb->ldovbb_vset_mask);
  702. if (ret) {
  703. dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
  704. goto err;
  705. }
  706. if (!abb->ldovbb_vset_mask) {
  707. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  708. ret = -EINVAL;
  709. goto err;
  710. }
  711. skip_opt:
  712. pname = "ti,tranxdone-status-mask";
  713. ret =
  714. of_property_read_u32(pdev->dev.of_node, pname,
  715. &abb->txdone_mask);
  716. if (ret) {
  717. dev_err(dev, "Missing '%s' (%d)\n", pname, ret);
  718. goto err;
  719. }
  720. if (!abb->txdone_mask) {
  721. dev_err(dev, "Invalid property:'%s' set as 0!\n", pname);
  722. ret = -EINVAL;
  723. goto err;
  724. }
  725. initdata = of_get_regulator_init_data(dev, pdev->dev.of_node);
  726. if (!initdata) {
  727. ret = -ENOMEM;
  728. dev_err(dev, "%s: Unable to alloc regulator init data\n",
  729. __func__);
  730. goto err;
  731. }
  732. /* init ABB opp_sel table */
  733. ret = ti_abb_init_table(dev, abb, initdata);
  734. if (ret)
  735. goto err;
  736. /* init ABB timing */
  737. ret = ti_abb_init_timings(dev, abb);
  738. if (ret)
  739. goto err;
  740. desc = &abb->rdesc;
  741. desc->name = dev_name(dev);
  742. desc->owner = THIS_MODULE;
  743. desc->type = REGULATOR_VOLTAGE;
  744. desc->ops = &ti_abb_reg_ops;
  745. c = &initdata->constraints;
  746. if (desc->n_voltages > 1)
  747. c->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  748. c->always_on = true;
  749. config.dev = dev;
  750. config.init_data = initdata;
  751. config.driver_data = abb;
  752. config.of_node = pdev->dev.of_node;
  753. rdev = regulator_register(desc, &config);
  754. if (IS_ERR(rdev)) {
  755. ret = PTR_ERR(rdev);
  756. dev_err(dev, "%s: failed to register regulator(%d)\n",
  757. __func__, ret);
  758. goto err;
  759. }
  760. platform_set_drvdata(pdev, rdev);
  761. /* Enable the ldo if not already done by bootloader */
  762. ti_abb_rmw(abb->regs->sr2_en_mask, 1, abb->regs->setup_reg, abb->base);
  763. return 0;
  764. err:
  765. dev_err(dev, "%s: Failed to initialize(%d)\n", __func__, ret);
  766. return ret;
  767. }
  768. /**
  769. * ti_abb_remove() - cleanups
  770. * @pdev: ABB platform device
  771. *
  772. * Return: 0
  773. */
  774. static int ti_abb_remove(struct platform_device *pdev)
  775. {
  776. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  777. regulator_unregister(rdev);
  778. return 0;
  779. }
  780. MODULE_ALIAS("platform:ti_abb");
  781. static struct platform_driver ti_abb_driver = {
  782. .probe = ti_abb_probe,
  783. .remove = ti_abb_remove,
  784. .driver = {
  785. .name = "ti_abb",
  786. .owner = THIS_MODULE,
  787. .of_match_table = of_match_ptr(ti_abb_of_match),
  788. },
  789. };
  790. module_platform_driver(ti_abb_driver);
  791. MODULE_DESCRIPTION("Texas Instruments ABB LDO regulator driver");
  792. MODULE_AUTHOR("Texas Instruments Inc.");
  793. MODULE_LICENSE("GPL v2");