as3722-regulator.c 25 KB

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