as3722-regulator.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * Voltage regulator support for AMS AS3722 PMIC
  3. *
  4. * Copyright (C) 2013 ams
  5. *
  6. * Author: Florian Lobmaier <florian.lobmaier@ams.com>
  7. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <linux/err.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/mfd/as3722.h>
  28. #include <linux/of.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/regulator/driver.h>
  32. #include <linux/regulator/machine.h>
  33. #include <linux/regulator/of_regulator.h>
  34. #include <linux/slab.h>
  35. /* Regulator IDs */
  36. enum as3722_regulators_id {
  37. AS3722_REGULATOR_ID_SD0,
  38. AS3722_REGULATOR_ID_SD1,
  39. AS3722_REGULATOR_ID_SD2,
  40. AS3722_REGULATOR_ID_SD3,
  41. AS3722_REGULATOR_ID_SD4,
  42. AS3722_REGULATOR_ID_SD5,
  43. AS3722_REGULATOR_ID_SD6,
  44. AS3722_REGULATOR_ID_LDO0,
  45. AS3722_REGULATOR_ID_LDO1,
  46. AS3722_REGULATOR_ID_LDO2,
  47. AS3722_REGULATOR_ID_LDO3,
  48. AS3722_REGULATOR_ID_LDO4,
  49. AS3722_REGULATOR_ID_LDO5,
  50. AS3722_REGULATOR_ID_LDO6,
  51. AS3722_REGULATOR_ID_LDO7,
  52. AS3722_REGULATOR_ID_LDO9,
  53. AS3722_REGULATOR_ID_LDO10,
  54. AS3722_REGULATOR_ID_LDO11,
  55. AS3722_REGULATOR_ID_MAX,
  56. };
  57. struct as3722_register_mapping {
  58. u8 regulator_id;
  59. const char *name;
  60. const char *sname;
  61. u8 vsel_reg;
  62. u8 vsel_mask;
  63. int n_voltages;
  64. u32 enable_reg;
  65. u8 enable_mask;
  66. u32 control_reg;
  67. u8 mode_mask;
  68. u32 sleep_ctrl_reg;
  69. u8 sleep_ctrl_mask;
  70. };
  71. struct as3722_regulator_config_data {
  72. struct regulator_init_data *reg_init;
  73. bool enable_tracking;
  74. int ext_control;
  75. };
  76. struct as3722_regulators {
  77. struct device *dev;
  78. struct as3722 *as3722;
  79. struct regulator_dev *rdevs[AS3722_REGULATOR_ID_MAX];
  80. struct regulator_desc desc[AS3722_REGULATOR_ID_MAX];
  81. struct as3722_regulator_config_data
  82. reg_config_data[AS3722_REGULATOR_ID_MAX];
  83. };
  84. static const struct as3722_register_mapping as3722_reg_lookup[] = {
  85. {
  86. .regulator_id = AS3722_REGULATOR_ID_SD0,
  87. .name = "as3722-sd0",
  88. .vsel_reg = AS3722_SD0_VOLTAGE_REG,
  89. .vsel_mask = AS3722_SD_VSEL_MASK,
  90. .enable_reg = AS3722_SD_CONTROL_REG,
  91. .enable_mask = AS3722_SDn_CTRL(0),
  92. .sleep_ctrl_reg = AS3722_ENABLE_CTRL1_REG,
  93. .sleep_ctrl_mask = AS3722_SD0_EXT_ENABLE_MASK,
  94. .control_reg = AS3722_SD0_CONTROL_REG,
  95. .mode_mask = AS3722_SD0_MODE_FAST,
  96. },
  97. {
  98. .regulator_id = AS3722_REGULATOR_ID_SD1,
  99. .name = "as3722-sd1",
  100. .vsel_reg = AS3722_SD1_VOLTAGE_REG,
  101. .vsel_mask = AS3722_SD_VSEL_MASK,
  102. .enable_reg = AS3722_SD_CONTROL_REG,
  103. .enable_mask = AS3722_SDn_CTRL(1),
  104. .sleep_ctrl_reg = AS3722_ENABLE_CTRL1_REG,
  105. .sleep_ctrl_mask = AS3722_SD1_EXT_ENABLE_MASK,
  106. .control_reg = AS3722_SD1_CONTROL_REG,
  107. .mode_mask = AS3722_SD1_MODE_FAST,
  108. },
  109. {
  110. .regulator_id = AS3722_REGULATOR_ID_SD2,
  111. .name = "as3722-sd2",
  112. .sname = "vsup-sd2",
  113. .vsel_reg = AS3722_SD2_VOLTAGE_REG,
  114. .vsel_mask = AS3722_SD_VSEL_MASK,
  115. .enable_reg = AS3722_SD_CONTROL_REG,
  116. .enable_mask = AS3722_SDn_CTRL(2),
  117. .sleep_ctrl_reg = AS3722_ENABLE_CTRL1_REG,
  118. .sleep_ctrl_mask = AS3722_SD2_EXT_ENABLE_MASK,
  119. .control_reg = AS3722_SD23_CONTROL_REG,
  120. .mode_mask = AS3722_SD2_MODE_FAST,
  121. .n_voltages = AS3722_SD2_VSEL_MAX + 1,
  122. },
  123. {
  124. .regulator_id = AS3722_REGULATOR_ID_SD3,
  125. .name = "as3722-sd3",
  126. .sname = "vsup-sd3",
  127. .vsel_reg = AS3722_SD3_VOLTAGE_REG,
  128. .vsel_mask = AS3722_SD_VSEL_MASK,
  129. .enable_reg = AS3722_SD_CONTROL_REG,
  130. .enable_mask = AS3722_SDn_CTRL(3),
  131. .sleep_ctrl_reg = AS3722_ENABLE_CTRL1_REG,
  132. .sleep_ctrl_mask = AS3722_SD3_EXT_ENABLE_MASK,
  133. .control_reg = AS3722_SD23_CONTROL_REG,
  134. .mode_mask = AS3722_SD3_MODE_FAST,
  135. .n_voltages = AS3722_SD2_VSEL_MAX + 1,
  136. },
  137. {
  138. .regulator_id = AS3722_REGULATOR_ID_SD4,
  139. .name = "as3722-sd4",
  140. .sname = "vsup-sd4",
  141. .vsel_reg = AS3722_SD4_VOLTAGE_REG,
  142. .vsel_mask = AS3722_SD_VSEL_MASK,
  143. .enable_reg = AS3722_SD_CONTROL_REG,
  144. .enable_mask = AS3722_SDn_CTRL(4),
  145. .sleep_ctrl_reg = AS3722_ENABLE_CTRL2_REG,
  146. .sleep_ctrl_mask = AS3722_SD4_EXT_ENABLE_MASK,
  147. .control_reg = AS3722_SD4_CONTROL_REG,
  148. .mode_mask = AS3722_SD4_MODE_FAST,
  149. .n_voltages = AS3722_SD2_VSEL_MAX + 1,
  150. },
  151. {
  152. .regulator_id = AS3722_REGULATOR_ID_SD5,
  153. .name = "as3722-sd5",
  154. .sname = "vsup-sd5",
  155. .vsel_reg = AS3722_SD5_VOLTAGE_REG,
  156. .vsel_mask = AS3722_SD_VSEL_MASK,
  157. .enable_reg = AS3722_SD_CONTROL_REG,
  158. .enable_mask = AS3722_SDn_CTRL(5),
  159. .sleep_ctrl_reg = AS3722_ENABLE_CTRL2_REG,
  160. .sleep_ctrl_mask = AS3722_SD5_EXT_ENABLE_MASK,
  161. .control_reg = AS3722_SD5_CONTROL_REG,
  162. .mode_mask = AS3722_SD5_MODE_FAST,
  163. .n_voltages = AS3722_SD2_VSEL_MAX + 1,
  164. },
  165. {
  166. .regulator_id = AS3722_REGULATOR_ID_SD6,
  167. .name = "as3722-sd6",
  168. .vsel_reg = AS3722_SD6_VOLTAGE_REG,
  169. .vsel_mask = AS3722_SD_VSEL_MASK,
  170. .enable_reg = AS3722_SD_CONTROL_REG,
  171. .enable_mask = AS3722_SDn_CTRL(6),
  172. .sleep_ctrl_reg = AS3722_ENABLE_CTRL2_REG,
  173. .sleep_ctrl_mask = AS3722_SD6_EXT_ENABLE_MASK,
  174. .control_reg = AS3722_SD6_CONTROL_REG,
  175. .mode_mask = AS3722_SD6_MODE_FAST,
  176. },
  177. {
  178. .regulator_id = AS3722_REGULATOR_ID_LDO0,
  179. .name = "as3722-ldo0",
  180. .sname = "vin-ldo0",
  181. .vsel_reg = AS3722_LDO0_VOLTAGE_REG,
  182. .vsel_mask = AS3722_LDO0_VSEL_MASK,
  183. .enable_reg = AS3722_LDOCONTROL0_REG,
  184. .enable_mask = AS3722_LDO0_CTRL,
  185. .sleep_ctrl_reg = AS3722_ENABLE_CTRL3_REG,
  186. .sleep_ctrl_mask = AS3722_LDO0_EXT_ENABLE_MASK,
  187. .n_voltages = AS3722_LDO0_NUM_VOLT,
  188. },
  189. {
  190. .regulator_id = AS3722_REGULATOR_ID_LDO1,
  191. .name = "as3722-ldo1",
  192. .sname = "vin-ldo1-6",
  193. .vsel_reg = AS3722_LDO1_VOLTAGE_REG,
  194. .vsel_mask = AS3722_LDO_VSEL_MASK,
  195. .enable_reg = AS3722_LDOCONTROL0_REG,
  196. .enable_mask = AS3722_LDO1_CTRL,
  197. .sleep_ctrl_reg = AS3722_ENABLE_CTRL3_REG,
  198. .sleep_ctrl_mask = AS3722_LDO1_EXT_ENABLE_MASK,
  199. .n_voltages = AS3722_LDO_NUM_VOLT,
  200. },
  201. {
  202. .regulator_id = AS3722_REGULATOR_ID_LDO2,
  203. .name = "as3722-ldo2",
  204. .sname = "vin-ldo2-5-7",
  205. .vsel_reg = AS3722_LDO2_VOLTAGE_REG,
  206. .vsel_mask = AS3722_LDO_VSEL_MASK,
  207. .enable_reg = AS3722_LDOCONTROL0_REG,
  208. .enable_mask = AS3722_LDO2_CTRL,
  209. .sleep_ctrl_reg = AS3722_ENABLE_CTRL3_REG,
  210. .sleep_ctrl_mask = AS3722_LDO2_EXT_ENABLE_MASK,
  211. .n_voltages = AS3722_LDO_NUM_VOLT,
  212. },
  213. {
  214. .regulator_id = AS3722_REGULATOR_ID_LDO3,
  215. .name = "as3722-ldo3",
  216. .sname = "vin-ldo3-4",
  217. .vsel_reg = AS3722_LDO3_VOLTAGE_REG,
  218. .vsel_mask = AS3722_LDO3_VSEL_MASK,
  219. .enable_reg = AS3722_LDOCONTROL0_REG,
  220. .enable_mask = AS3722_LDO3_CTRL,
  221. .sleep_ctrl_reg = AS3722_ENABLE_CTRL3_REG,
  222. .sleep_ctrl_mask = AS3722_LDO3_EXT_ENABLE_MASK,
  223. .n_voltages = AS3722_LDO3_NUM_VOLT,
  224. },
  225. {
  226. .regulator_id = AS3722_REGULATOR_ID_LDO4,
  227. .name = "as3722-ldo4",
  228. .sname = "vin-ldo3-4",
  229. .vsel_reg = AS3722_LDO4_VOLTAGE_REG,
  230. .vsel_mask = AS3722_LDO_VSEL_MASK,
  231. .enable_reg = AS3722_LDOCONTROL0_REG,
  232. .enable_mask = AS3722_LDO4_CTRL,
  233. .sleep_ctrl_reg = AS3722_ENABLE_CTRL4_REG,
  234. .sleep_ctrl_mask = AS3722_LDO4_EXT_ENABLE_MASK,
  235. .n_voltages = AS3722_LDO_NUM_VOLT,
  236. },
  237. {
  238. .regulator_id = AS3722_REGULATOR_ID_LDO5,
  239. .name = "as3722-ldo5",
  240. .sname = "vin-ldo2-5-7",
  241. .vsel_reg = AS3722_LDO5_VOLTAGE_REG,
  242. .vsel_mask = AS3722_LDO_VSEL_MASK,
  243. .enable_reg = AS3722_LDOCONTROL0_REG,
  244. .enable_mask = AS3722_LDO5_CTRL,
  245. .sleep_ctrl_reg = AS3722_ENABLE_CTRL4_REG,
  246. .sleep_ctrl_mask = AS3722_LDO5_EXT_ENABLE_MASK,
  247. .n_voltages = AS3722_LDO_NUM_VOLT,
  248. },
  249. {
  250. .regulator_id = AS3722_REGULATOR_ID_LDO6,
  251. .name = "as3722-ldo6",
  252. .sname = "vin-ldo1-6",
  253. .vsel_reg = AS3722_LDO6_VOLTAGE_REG,
  254. .vsel_mask = AS3722_LDO_VSEL_MASK,
  255. .enable_reg = AS3722_LDOCONTROL0_REG,
  256. .enable_mask = AS3722_LDO6_CTRL,
  257. .sleep_ctrl_reg = AS3722_ENABLE_CTRL4_REG,
  258. .sleep_ctrl_mask = AS3722_LDO6_EXT_ENABLE_MASK,
  259. .n_voltages = AS3722_LDO_NUM_VOLT,
  260. },
  261. {
  262. .regulator_id = AS3722_REGULATOR_ID_LDO7,
  263. .name = "as3722-ldo7",
  264. .sname = "vin-ldo2-5-7",
  265. .vsel_reg = AS3722_LDO7_VOLTAGE_REG,
  266. .vsel_mask = AS3722_LDO_VSEL_MASK,
  267. .enable_reg = AS3722_LDOCONTROL0_REG,
  268. .enable_mask = AS3722_LDO7_CTRL,
  269. .sleep_ctrl_reg = AS3722_ENABLE_CTRL4_REG,
  270. .sleep_ctrl_mask = AS3722_LDO7_EXT_ENABLE_MASK,
  271. .n_voltages = AS3722_LDO_NUM_VOLT,
  272. },
  273. {
  274. .regulator_id = AS3722_REGULATOR_ID_LDO9,
  275. .name = "as3722-ldo9",
  276. .sname = "vin-ldo9-10",
  277. .vsel_reg = AS3722_LDO9_VOLTAGE_REG,
  278. .vsel_mask = AS3722_LDO_VSEL_MASK,
  279. .enable_reg = AS3722_LDOCONTROL1_REG,
  280. .enable_mask = AS3722_LDO9_CTRL,
  281. .sleep_ctrl_reg = AS3722_ENABLE_CTRL5_REG,
  282. .sleep_ctrl_mask = AS3722_LDO9_EXT_ENABLE_MASK,
  283. .n_voltages = AS3722_LDO_NUM_VOLT,
  284. },
  285. {
  286. .regulator_id = AS3722_REGULATOR_ID_LDO10,
  287. .name = "as3722-ldo10",
  288. .sname = "vin-ldo9-10",
  289. .vsel_reg = AS3722_LDO10_VOLTAGE_REG,
  290. .vsel_mask = AS3722_LDO_VSEL_MASK,
  291. .enable_reg = AS3722_LDOCONTROL1_REG,
  292. .enable_mask = AS3722_LDO10_CTRL,
  293. .sleep_ctrl_reg = AS3722_ENABLE_CTRL5_REG,
  294. .sleep_ctrl_mask = AS3722_LDO10_EXT_ENABLE_MASK,
  295. .n_voltages = AS3722_LDO_NUM_VOLT,
  296. },
  297. {
  298. .regulator_id = AS3722_REGULATOR_ID_LDO11,
  299. .name = "as3722-ldo11",
  300. .sname = "vin-ldo11",
  301. .vsel_reg = AS3722_LDO11_VOLTAGE_REG,
  302. .vsel_mask = AS3722_LDO_VSEL_MASK,
  303. .enable_reg = AS3722_LDOCONTROL1_REG,
  304. .enable_mask = AS3722_LDO11_CTRL,
  305. .sleep_ctrl_reg = AS3722_ENABLE_CTRL5_REG,
  306. .sleep_ctrl_mask = AS3722_LDO11_EXT_ENABLE_MASK,
  307. .n_voltages = AS3722_LDO_NUM_VOLT,
  308. },
  309. };
  310. static const int as3722_ldo_current[] = { 150000, 300000 };
  311. static const int as3722_sd016_current[] = { 2500000, 3000000, 3500000 };
  312. static int as3722_current_to_index(int min_uA, int max_uA,
  313. const int *curr_table, int n_currents)
  314. {
  315. int i;
  316. for (i = n_currents - 1; i >= 0; i--) {
  317. if ((min_uA <= curr_table[i]) && (curr_table[i] <= max_uA))
  318. return i;
  319. }
  320. return -EINVAL;
  321. }
  322. static int as3722_ldo_get_current_limit(struct regulator_dev *rdev)
  323. {
  324. struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
  325. struct as3722 *as3722 = as3722_regs->as3722;
  326. int id = rdev_get_id(rdev);
  327. u32 val;
  328. int ret;
  329. ret = as3722_read(as3722, as3722_reg_lookup[id].vsel_reg, &val);
  330. if (ret < 0) {
  331. dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
  332. as3722_reg_lookup[id].vsel_reg, ret);
  333. return ret;
  334. }
  335. if (val & AS3722_LDO_ILIMIT_MASK)
  336. return 300000;
  337. return 150000;
  338. }
  339. static int as3722_ldo_set_current_limit(struct regulator_dev *rdev,
  340. int min_uA, int max_uA)
  341. {
  342. struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
  343. struct as3722 *as3722 = as3722_regs->as3722;
  344. int id = rdev_get_id(rdev);
  345. int ret;
  346. u32 reg = 0;
  347. ret = as3722_current_to_index(min_uA, max_uA, as3722_ldo_current,
  348. ARRAY_SIZE(as3722_ldo_current));
  349. if (ret < 0) {
  350. dev_err(as3722_regs->dev,
  351. "Current range min:max = %d:%d does not support\n",
  352. min_uA, max_uA);
  353. return ret;
  354. }
  355. if (ret)
  356. reg = AS3722_LDO_ILIMIT_BIT;
  357. return as3722_update_bits(as3722, as3722_reg_lookup[id].vsel_reg,
  358. AS3722_LDO_ILIMIT_MASK, reg);
  359. }
  360. static const struct regulator_ops as3722_ldo0_ops = {
  361. .is_enabled = regulator_is_enabled_regmap,
  362. .enable = regulator_enable_regmap,
  363. .disable = regulator_disable_regmap,
  364. .list_voltage = regulator_list_voltage_linear,
  365. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  366. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  367. .get_current_limit = as3722_ldo_get_current_limit,
  368. .set_current_limit = as3722_ldo_set_current_limit,
  369. };
  370. static const struct regulator_ops as3722_ldo0_extcntrl_ops = {
  371. .list_voltage = regulator_list_voltage_linear,
  372. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  373. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  374. .get_current_limit = as3722_ldo_get_current_limit,
  375. .set_current_limit = as3722_ldo_set_current_limit,
  376. };
  377. static int as3722_ldo3_set_tracking_mode(struct as3722_regulators *as3722_reg,
  378. int id, u8 mode)
  379. {
  380. struct as3722 *as3722 = as3722_reg->as3722;
  381. switch (mode) {
  382. case AS3722_LDO3_MODE_PMOS:
  383. case AS3722_LDO3_MODE_PMOS_TRACKING:
  384. case AS3722_LDO3_MODE_NMOS:
  385. case AS3722_LDO3_MODE_SWITCH:
  386. return as3722_update_bits(as3722,
  387. as3722_reg_lookup[id].vsel_reg,
  388. AS3722_LDO3_MODE_MASK, mode);
  389. default:
  390. return -EINVAL;
  391. }
  392. }
  393. static int as3722_ldo3_get_current_limit(struct regulator_dev *rdev)
  394. {
  395. return 150000;
  396. }
  397. static const struct regulator_ops as3722_ldo3_ops = {
  398. .is_enabled = regulator_is_enabled_regmap,
  399. .enable = regulator_enable_regmap,
  400. .disable = regulator_disable_regmap,
  401. .list_voltage = regulator_list_voltage_linear,
  402. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  403. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  404. .get_current_limit = as3722_ldo3_get_current_limit,
  405. };
  406. static const struct regulator_ops as3722_ldo3_extcntrl_ops = {
  407. .list_voltage = regulator_list_voltage_linear,
  408. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  409. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  410. .get_current_limit = as3722_ldo3_get_current_limit,
  411. };
  412. static const struct regulator_ops as3722_ldo6_ops = {
  413. .is_enabled = regulator_is_enabled_regmap,
  414. .enable = regulator_enable_regmap,
  415. .disable = regulator_disable_regmap,
  416. .map_voltage = regulator_map_voltage_linear_range,
  417. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  418. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  419. .list_voltage = regulator_list_voltage_linear_range,
  420. .get_current_limit = as3722_ldo_get_current_limit,
  421. .set_current_limit = as3722_ldo_set_current_limit,
  422. .get_bypass = regulator_get_bypass_regmap,
  423. .set_bypass = regulator_set_bypass_regmap,
  424. };
  425. static const struct regulator_ops as3722_ldo6_extcntrl_ops = {
  426. .map_voltage = regulator_map_voltage_linear_range,
  427. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  428. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  429. .list_voltage = regulator_list_voltage_linear_range,
  430. .get_current_limit = as3722_ldo_get_current_limit,
  431. .set_current_limit = as3722_ldo_set_current_limit,
  432. .get_bypass = regulator_get_bypass_regmap,
  433. .set_bypass = regulator_set_bypass_regmap,
  434. };
  435. static const struct regulator_linear_range as3722_ldo_ranges[] = {
  436. REGULATOR_LINEAR_RANGE(0, 0x00, 0x00, 0),
  437. REGULATOR_LINEAR_RANGE(825000, 0x01, 0x24, 25000),
  438. REGULATOR_LINEAR_RANGE(1725000, 0x40, 0x7F, 25000),
  439. };
  440. static const struct regulator_ops as3722_ldo_ops = {
  441. .is_enabled = regulator_is_enabled_regmap,
  442. .enable = regulator_enable_regmap,
  443. .disable = regulator_disable_regmap,
  444. .map_voltage = regulator_map_voltage_linear_range,
  445. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  446. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  447. .list_voltage = regulator_list_voltage_linear_range,
  448. .get_current_limit = as3722_ldo_get_current_limit,
  449. .set_current_limit = as3722_ldo_set_current_limit,
  450. };
  451. static const struct regulator_ops as3722_ldo_extcntrl_ops = {
  452. .map_voltage = regulator_map_voltage_linear_range,
  453. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  454. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  455. .list_voltage = regulator_list_voltage_linear_range,
  456. .get_current_limit = as3722_ldo_get_current_limit,
  457. .set_current_limit = as3722_ldo_set_current_limit,
  458. };
  459. static unsigned int as3722_sd_get_mode(struct regulator_dev *rdev)
  460. {
  461. struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
  462. struct as3722 *as3722 = as3722_regs->as3722;
  463. int id = rdev_get_id(rdev);
  464. u32 val;
  465. int ret;
  466. if (!as3722_reg_lookup[id].control_reg)
  467. return -ENOTSUPP;
  468. ret = as3722_read(as3722, as3722_reg_lookup[id].control_reg, &val);
  469. if (ret < 0) {
  470. dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
  471. as3722_reg_lookup[id].control_reg, ret);
  472. return ret;
  473. }
  474. if (val & as3722_reg_lookup[id].mode_mask)
  475. return REGULATOR_MODE_FAST;
  476. else
  477. return REGULATOR_MODE_NORMAL;
  478. }
  479. static int as3722_sd_set_mode(struct regulator_dev *rdev,
  480. unsigned int mode)
  481. {
  482. struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
  483. struct as3722 *as3722 = as3722_regs->as3722;
  484. u8 id = rdev_get_id(rdev);
  485. u8 val = 0;
  486. int ret;
  487. if (!as3722_reg_lookup[id].control_reg)
  488. return -ERANGE;
  489. switch (mode) {
  490. case REGULATOR_MODE_FAST:
  491. val = as3722_reg_lookup[id].mode_mask;
  492. case REGULATOR_MODE_NORMAL: /* fall down */
  493. break;
  494. default:
  495. return -EINVAL;
  496. }
  497. ret = as3722_update_bits(as3722, as3722_reg_lookup[id].control_reg,
  498. as3722_reg_lookup[id].mode_mask, val);
  499. if (ret < 0) {
  500. dev_err(as3722_regs->dev, "Reg 0x%02x update failed: %d\n",
  501. as3722_reg_lookup[id].control_reg, ret);
  502. return ret;
  503. }
  504. return ret;
  505. }
  506. static int as3722_sd016_get_current_limit(struct regulator_dev *rdev)
  507. {
  508. struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
  509. struct as3722 *as3722 = as3722_regs->as3722;
  510. int id = rdev_get_id(rdev);
  511. u32 val, reg;
  512. int mask;
  513. int ret;
  514. switch (id) {
  515. case AS3722_REGULATOR_ID_SD0:
  516. reg = AS3722_OVCURRENT_REG;
  517. mask = AS3722_OVCURRENT_SD0_TRIP_MASK;
  518. break;
  519. case AS3722_REGULATOR_ID_SD1:
  520. reg = AS3722_OVCURRENT_REG;
  521. mask = AS3722_OVCURRENT_SD1_TRIP_MASK;
  522. break;
  523. case AS3722_REGULATOR_ID_SD6:
  524. reg = AS3722_OVCURRENT_DEB_REG;
  525. mask = AS3722_OVCURRENT_SD6_TRIP_MASK;
  526. break;
  527. default:
  528. return -EINVAL;
  529. }
  530. ret = as3722_read(as3722, reg, &val);
  531. if (ret < 0) {
  532. dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
  533. reg, ret);
  534. return ret;
  535. }
  536. val &= mask;
  537. val >>= ffs(mask) - 1;
  538. if (val == 3)
  539. return -EINVAL;
  540. return as3722_sd016_current[val];
  541. }
  542. static int as3722_sd016_set_current_limit(struct regulator_dev *rdev,
  543. int min_uA, int max_uA)
  544. {
  545. struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
  546. struct as3722 *as3722 = as3722_regs->as3722;
  547. int id = rdev_get_id(rdev);
  548. int ret;
  549. int val;
  550. int mask;
  551. u32 reg;
  552. ret = as3722_current_to_index(min_uA, max_uA, as3722_sd016_current,
  553. ARRAY_SIZE(as3722_sd016_current));
  554. if (ret < 0) {
  555. dev_err(as3722_regs->dev,
  556. "Current range min:max = %d:%d does not support\n",
  557. min_uA, max_uA);
  558. return ret;
  559. }
  560. switch (id) {
  561. case AS3722_REGULATOR_ID_SD0:
  562. reg = AS3722_OVCURRENT_REG;
  563. mask = AS3722_OVCURRENT_SD0_TRIP_MASK;
  564. break;
  565. case AS3722_REGULATOR_ID_SD1:
  566. reg = AS3722_OVCURRENT_REG;
  567. mask = AS3722_OVCURRENT_SD1_TRIP_MASK;
  568. break;
  569. case AS3722_REGULATOR_ID_SD6:
  570. reg = AS3722_OVCURRENT_DEB_REG;
  571. mask = AS3722_OVCURRENT_SD6_TRIP_MASK;
  572. break;
  573. default:
  574. return -EINVAL;
  575. }
  576. ret <<= ffs(mask) - 1;
  577. val = ret & mask;
  578. return as3722_update_bits(as3722, reg, mask, val);
  579. }
  580. static bool as3722_sd0_is_low_voltage(struct as3722_regulators *as3722_regs)
  581. {
  582. int err;
  583. unsigned val;
  584. err = as3722_read(as3722_regs->as3722, AS3722_FUSE7_REG, &val);
  585. if (err < 0) {
  586. dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
  587. AS3722_FUSE7_REG, err);
  588. return false;
  589. }
  590. if (val & AS3722_FUSE7_SD0_LOW_VOLTAGE)
  591. return true;
  592. return false;
  593. }
  594. static const struct regulator_linear_range as3722_sd2345_ranges[] = {
  595. REGULATOR_LINEAR_RANGE(0, 0x00, 0x00, 0),
  596. REGULATOR_LINEAR_RANGE(612500, 0x01, 0x40, 12500),
  597. REGULATOR_LINEAR_RANGE(1425000, 0x41, 0x70, 25000),
  598. REGULATOR_LINEAR_RANGE(2650000, 0x71, 0x7F, 50000),
  599. };
  600. static const struct regulator_ops as3722_sd016_ops = {
  601. .is_enabled = regulator_is_enabled_regmap,
  602. .enable = regulator_enable_regmap,
  603. .disable = regulator_disable_regmap,
  604. .list_voltage = regulator_list_voltage_linear,
  605. .map_voltage = regulator_map_voltage_linear,
  606. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  607. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  608. .get_current_limit = as3722_sd016_get_current_limit,
  609. .set_current_limit = as3722_sd016_set_current_limit,
  610. .get_mode = as3722_sd_get_mode,
  611. .set_mode = as3722_sd_set_mode,
  612. };
  613. static const struct regulator_ops as3722_sd016_extcntrl_ops = {
  614. .list_voltage = regulator_list_voltage_linear,
  615. .map_voltage = regulator_map_voltage_linear,
  616. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  617. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  618. .get_current_limit = as3722_sd016_get_current_limit,
  619. .set_current_limit = as3722_sd016_set_current_limit,
  620. .get_mode = as3722_sd_get_mode,
  621. .set_mode = as3722_sd_set_mode,
  622. };
  623. static const struct regulator_ops as3722_sd2345_ops = {
  624. .is_enabled = regulator_is_enabled_regmap,
  625. .enable = regulator_enable_regmap,
  626. .disable = regulator_disable_regmap,
  627. .list_voltage = regulator_list_voltage_linear_range,
  628. .map_voltage = regulator_map_voltage_linear_range,
  629. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  630. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  631. .get_mode = as3722_sd_get_mode,
  632. .set_mode = as3722_sd_set_mode,
  633. };
  634. static const struct regulator_ops as3722_sd2345_extcntrl_ops = {
  635. .list_voltage = regulator_list_voltage_linear_range,
  636. .map_voltage = regulator_map_voltage_linear_range,
  637. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  638. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  639. .get_mode = as3722_sd_get_mode,
  640. .set_mode = as3722_sd_set_mode,
  641. };
  642. static int as3722_extreg_init(struct as3722_regulators *as3722_regs, int id,
  643. int ext_pwr_ctrl)
  644. {
  645. int ret;
  646. unsigned int val;
  647. if ((ext_pwr_ctrl < AS3722_EXT_CONTROL_ENABLE1) ||
  648. (ext_pwr_ctrl > AS3722_EXT_CONTROL_ENABLE3))
  649. return -EINVAL;
  650. val = ext_pwr_ctrl << (ffs(as3722_reg_lookup[id].sleep_ctrl_mask) - 1);
  651. ret = as3722_update_bits(as3722_regs->as3722,
  652. as3722_reg_lookup[id].sleep_ctrl_reg,
  653. as3722_reg_lookup[id].sleep_ctrl_mask, val);
  654. if (ret < 0)
  655. dev_err(as3722_regs->dev, "Reg 0x%02x update failed: %d\n",
  656. as3722_reg_lookup[id].sleep_ctrl_reg, ret);
  657. return ret;
  658. }
  659. static struct of_regulator_match as3722_regulator_matches[] = {
  660. { .name = "sd0", },
  661. { .name = "sd1", },
  662. { .name = "sd2", },
  663. { .name = "sd3", },
  664. { .name = "sd4", },
  665. { .name = "sd5", },
  666. { .name = "sd6", },
  667. { .name = "ldo0", },
  668. { .name = "ldo1", },
  669. { .name = "ldo2", },
  670. { .name = "ldo3", },
  671. { .name = "ldo4", },
  672. { .name = "ldo5", },
  673. { .name = "ldo6", },
  674. { .name = "ldo7", },
  675. { .name = "ldo9", },
  676. { .name = "ldo10", },
  677. { .name = "ldo11", },
  678. };
  679. static int as3722_get_regulator_dt_data(struct platform_device *pdev,
  680. struct as3722_regulators *as3722_regs)
  681. {
  682. struct device_node *np;
  683. struct as3722_regulator_config_data *reg_config;
  684. u32 prop;
  685. int id;
  686. int ret;
  687. np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
  688. if (!np) {
  689. dev_err(&pdev->dev, "Device is not having regulators node\n");
  690. return -ENODEV;
  691. }
  692. pdev->dev.of_node = np;
  693. ret = of_regulator_match(&pdev->dev, np, as3722_regulator_matches,
  694. ARRAY_SIZE(as3722_regulator_matches));
  695. of_node_put(np);
  696. if (ret < 0) {
  697. dev_err(&pdev->dev, "Parsing of regulator node failed: %d\n",
  698. ret);
  699. return ret;
  700. }
  701. for (id = 0; id < ARRAY_SIZE(as3722_regulator_matches); ++id) {
  702. struct device_node *reg_node;
  703. reg_config = &as3722_regs->reg_config_data[id];
  704. reg_config->reg_init = as3722_regulator_matches[id].init_data;
  705. reg_node = as3722_regulator_matches[id].of_node;
  706. if (!reg_config->reg_init || !reg_node)
  707. continue;
  708. ret = of_property_read_u32(reg_node, "ams,ext-control", &prop);
  709. if (!ret) {
  710. if (prop < 3)
  711. reg_config->ext_control = prop;
  712. else
  713. dev_warn(&pdev->dev,
  714. "ext-control have invalid option: %u\n",
  715. prop);
  716. }
  717. reg_config->enable_tracking =
  718. of_property_read_bool(reg_node, "ams,enable-tracking");
  719. }
  720. return 0;
  721. }
  722. static int as3722_regulator_probe(struct platform_device *pdev)
  723. {
  724. struct as3722 *as3722 = dev_get_drvdata(pdev->dev.parent);
  725. struct as3722_regulators *as3722_regs;
  726. struct as3722_regulator_config_data *reg_config;
  727. struct regulator_dev *rdev;
  728. struct regulator_config config = { };
  729. const struct regulator_ops *ops;
  730. int id;
  731. int ret;
  732. as3722_regs = devm_kzalloc(&pdev->dev, sizeof(*as3722_regs),
  733. GFP_KERNEL);
  734. if (!as3722_regs)
  735. return -ENOMEM;
  736. as3722_regs->dev = &pdev->dev;
  737. as3722_regs->as3722 = as3722;
  738. platform_set_drvdata(pdev, as3722_regs);
  739. ret = as3722_get_regulator_dt_data(pdev, as3722_regs);
  740. if (ret < 0)
  741. return ret;
  742. config.dev = &pdev->dev;
  743. config.driver_data = as3722_regs;
  744. config.regmap = as3722->regmap;
  745. for (id = 0; id < AS3722_REGULATOR_ID_MAX; id++) {
  746. reg_config = &as3722_regs->reg_config_data[id];
  747. as3722_regs->desc[id].name = as3722_reg_lookup[id].name;
  748. as3722_regs->desc[id].supply_name = as3722_reg_lookup[id].sname;
  749. as3722_regs->desc[id].id = as3722_reg_lookup[id].regulator_id;
  750. as3722_regs->desc[id].n_voltages =
  751. as3722_reg_lookup[id].n_voltages;
  752. as3722_regs->desc[id].type = REGULATOR_VOLTAGE;
  753. as3722_regs->desc[id].owner = THIS_MODULE;
  754. as3722_regs->desc[id].enable_reg =
  755. as3722_reg_lookup[id].enable_reg;
  756. as3722_regs->desc[id].enable_mask =
  757. as3722_reg_lookup[id].enable_mask;
  758. as3722_regs->desc[id].vsel_reg = as3722_reg_lookup[id].vsel_reg;
  759. as3722_regs->desc[id].vsel_mask =
  760. as3722_reg_lookup[id].vsel_mask;
  761. switch (id) {
  762. case AS3722_REGULATOR_ID_LDO0:
  763. if (reg_config->ext_control)
  764. ops = &as3722_ldo0_extcntrl_ops;
  765. else
  766. ops = &as3722_ldo0_ops;
  767. as3722_regs->desc[id].min_uV = 825000;
  768. as3722_regs->desc[id].uV_step = 25000;
  769. as3722_regs->desc[id].linear_min_sel = 1;
  770. as3722_regs->desc[id].enable_time = 500;
  771. break;
  772. case AS3722_REGULATOR_ID_LDO3:
  773. if (reg_config->ext_control)
  774. ops = &as3722_ldo3_extcntrl_ops;
  775. else
  776. ops = &as3722_ldo3_ops;
  777. as3722_regs->desc[id].min_uV = 620000;
  778. as3722_regs->desc[id].uV_step = 20000;
  779. as3722_regs->desc[id].linear_min_sel = 1;
  780. as3722_regs->desc[id].enable_time = 500;
  781. if (reg_config->enable_tracking) {
  782. ret = as3722_ldo3_set_tracking_mode(as3722_regs,
  783. id, AS3722_LDO3_MODE_PMOS_TRACKING);
  784. if (ret < 0) {
  785. dev_err(&pdev->dev,
  786. "LDO3 tracking failed: %d\n",
  787. ret);
  788. return ret;
  789. }
  790. }
  791. break;
  792. case AS3722_REGULATOR_ID_LDO6:
  793. if (reg_config->ext_control)
  794. ops = &as3722_ldo6_extcntrl_ops;
  795. else
  796. ops = &as3722_ldo6_ops;
  797. as3722_regs->desc[id].enable_time = 500;
  798. as3722_regs->desc[id].bypass_reg =
  799. AS3722_LDO6_VOLTAGE_REG;
  800. as3722_regs->desc[id].bypass_mask =
  801. AS3722_LDO_VSEL_MASK;
  802. as3722_regs->desc[id].bypass_val_on =
  803. AS3722_LDO6_VSEL_BYPASS;
  804. as3722_regs->desc[id].bypass_val_off =
  805. AS3722_LDO6_VSEL_BYPASS;
  806. as3722_regs->desc[id].linear_ranges = as3722_ldo_ranges;
  807. as3722_regs->desc[id].n_linear_ranges =
  808. ARRAY_SIZE(as3722_ldo_ranges);
  809. break;
  810. case AS3722_REGULATOR_ID_SD0:
  811. case AS3722_REGULATOR_ID_SD1:
  812. case AS3722_REGULATOR_ID_SD6:
  813. if (reg_config->ext_control)
  814. ops = &as3722_sd016_extcntrl_ops;
  815. else
  816. ops = &as3722_sd016_ops;
  817. if (id == AS3722_REGULATOR_ID_SD0 &&
  818. as3722_sd0_is_low_voltage(as3722_regs)) {
  819. as3722_regs->desc[id].n_voltages =
  820. AS3722_SD0_VSEL_LOW_VOL_MAX + 1;
  821. as3722_regs->desc[id].min_uV = 410000;
  822. } else {
  823. as3722_regs->desc[id].n_voltages =
  824. AS3722_SD0_VSEL_MAX + 1,
  825. as3722_regs->desc[id].min_uV = 610000;
  826. }
  827. as3722_regs->desc[id].uV_step = 10000;
  828. as3722_regs->desc[id].linear_min_sel = 1;
  829. as3722_regs->desc[id].enable_time = 600;
  830. break;
  831. case AS3722_REGULATOR_ID_SD2:
  832. case AS3722_REGULATOR_ID_SD3:
  833. case AS3722_REGULATOR_ID_SD4:
  834. case AS3722_REGULATOR_ID_SD5:
  835. if (reg_config->ext_control)
  836. ops = &as3722_sd2345_extcntrl_ops;
  837. else
  838. ops = &as3722_sd2345_ops;
  839. as3722_regs->desc[id].linear_ranges =
  840. as3722_sd2345_ranges;
  841. as3722_regs->desc[id].n_linear_ranges =
  842. ARRAY_SIZE(as3722_sd2345_ranges);
  843. break;
  844. default:
  845. if (reg_config->ext_control)
  846. ops = &as3722_ldo_extcntrl_ops;
  847. else
  848. ops = &as3722_ldo_ops;
  849. as3722_regs->desc[id].enable_time = 500;
  850. as3722_regs->desc[id].linear_ranges = as3722_ldo_ranges;
  851. as3722_regs->desc[id].n_linear_ranges =
  852. ARRAY_SIZE(as3722_ldo_ranges);
  853. break;
  854. }
  855. as3722_regs->desc[id].ops = ops;
  856. config.init_data = reg_config->reg_init;
  857. config.of_node = as3722_regulator_matches[id].of_node;
  858. rdev = devm_regulator_register(&pdev->dev,
  859. &as3722_regs->desc[id], &config);
  860. if (IS_ERR(rdev)) {
  861. ret = PTR_ERR(rdev);
  862. dev_err(&pdev->dev, "regulator %d register failed %d\n",
  863. id, ret);
  864. return ret;
  865. }
  866. as3722_regs->rdevs[id] = rdev;
  867. if (reg_config->ext_control) {
  868. ret = regulator_enable_regmap(rdev);
  869. if (ret < 0) {
  870. dev_err(&pdev->dev,
  871. "Regulator %d enable failed: %d\n",
  872. id, ret);
  873. return ret;
  874. }
  875. ret = as3722_extreg_init(as3722_regs, id,
  876. reg_config->ext_control);
  877. if (ret < 0) {
  878. dev_err(&pdev->dev,
  879. "AS3722 ext control failed: %d", ret);
  880. return ret;
  881. }
  882. }
  883. }
  884. return 0;
  885. }
  886. static const struct of_device_id of_as3722_regulator_match[] = {
  887. { .compatible = "ams,as3722-regulator", },
  888. {},
  889. };
  890. MODULE_DEVICE_TABLE(of, of_as3722_regulator_match);
  891. static struct platform_driver as3722_regulator_driver = {
  892. .driver = {
  893. .name = "as3722-regulator",
  894. .of_match_table = of_as3722_regulator_match,
  895. },
  896. .probe = as3722_regulator_probe,
  897. };
  898. module_platform_driver(as3722_regulator_driver);
  899. MODULE_ALIAS("platform:as3722-regulator");
  900. MODULE_DESCRIPTION("AS3722 regulator driver");
  901. MODULE_AUTHOR("Florian Lobmaier <florian.lobmaier@ams.com>");
  902. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  903. MODULE_LICENSE("GPL");