ti-abb-regulator.c 23 KB

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