max77620-regulator.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * Maxim MAX77620 Regulator driver
  3. *
  4. * Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Author: Mallikarjun Kasoju <mkasoju@nvidia.com>
  7. * Laxman Dewangan <ldewangan@nvidia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/mfd/max77620.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/regulator/of_regulator.h>
  22. #define max77620_rails(_name) "max77620-"#_name
  23. /* Power Mode */
  24. #define MAX77620_POWER_MODE_NORMAL 3
  25. #define MAX77620_POWER_MODE_LPM 2
  26. #define MAX77620_POWER_MODE_GLPM 1
  27. #define MAX77620_POWER_MODE_DISABLE 0
  28. /* SD Slew Rate */
  29. #define MAX77620_SD_SR_13_75 0
  30. #define MAX77620_SD_SR_27_5 1
  31. #define MAX77620_SD_SR_55 2
  32. #define MAX77620_SD_SR_100 3
  33. enum max77620_regulators {
  34. MAX77620_REGULATOR_ID_SD0,
  35. MAX77620_REGULATOR_ID_SD1,
  36. MAX77620_REGULATOR_ID_SD2,
  37. MAX77620_REGULATOR_ID_SD3,
  38. MAX77620_REGULATOR_ID_SD4,
  39. MAX77620_REGULATOR_ID_LDO0,
  40. MAX77620_REGULATOR_ID_LDO1,
  41. MAX77620_REGULATOR_ID_LDO2,
  42. MAX77620_REGULATOR_ID_LDO3,
  43. MAX77620_REGULATOR_ID_LDO4,
  44. MAX77620_REGULATOR_ID_LDO5,
  45. MAX77620_REGULATOR_ID_LDO6,
  46. MAX77620_REGULATOR_ID_LDO7,
  47. MAX77620_REGULATOR_ID_LDO8,
  48. MAX77620_NUM_REGS,
  49. };
  50. /* Regulator types */
  51. enum max77620_regulator_type {
  52. MAX77620_REGULATOR_TYPE_SD,
  53. MAX77620_REGULATOR_TYPE_LDO_N,
  54. MAX77620_REGULATOR_TYPE_LDO_P,
  55. };
  56. struct max77620_regulator_info {
  57. u8 type;
  58. u8 fps_addr;
  59. u8 volt_addr;
  60. u8 cfg_addr;
  61. u8 power_mode_mask;
  62. u8 power_mode_shift;
  63. u8 remote_sense_addr;
  64. u8 remote_sense_mask;
  65. struct regulator_desc desc;
  66. };
  67. struct max77620_regulator_pdata {
  68. int active_fps_src;
  69. int active_fps_pd_slot;
  70. int active_fps_pu_slot;
  71. int suspend_fps_src;
  72. int suspend_fps_pd_slot;
  73. int suspend_fps_pu_slot;
  74. int current_mode;
  75. int power_ok;
  76. int ramp_rate_setting;
  77. };
  78. struct max77620_regulator {
  79. struct device *dev;
  80. struct regmap *rmap;
  81. struct max77620_regulator_info *rinfo[MAX77620_NUM_REGS];
  82. struct max77620_regulator_pdata reg_pdata[MAX77620_NUM_REGS];
  83. int enable_power_mode[MAX77620_NUM_REGS];
  84. int current_power_mode[MAX77620_NUM_REGS];
  85. int active_fps_src[MAX77620_NUM_REGS];
  86. };
  87. #define fps_src_name(fps_src) \
  88. (fps_src == MAX77620_FPS_SRC_0 ? "FPS_SRC_0" : \
  89. fps_src == MAX77620_FPS_SRC_1 ? "FPS_SRC_1" : \
  90. fps_src == MAX77620_FPS_SRC_2 ? "FPS_SRC_2" : "FPS_SRC_NONE")
  91. static int max77620_regulator_get_fps_src(struct max77620_regulator *pmic,
  92. int id)
  93. {
  94. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  95. unsigned int val;
  96. int ret;
  97. ret = regmap_read(pmic->rmap, rinfo->fps_addr, &val);
  98. if (ret < 0) {
  99. dev_err(pmic->dev, "Reg 0x%02x read failed %d\n",
  100. rinfo->fps_addr, ret);
  101. return ret;
  102. }
  103. return (val & MAX77620_FPS_SRC_MASK) >> MAX77620_FPS_SRC_SHIFT;
  104. }
  105. static int max77620_regulator_set_fps_src(struct max77620_regulator *pmic,
  106. int fps_src, int id)
  107. {
  108. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  109. unsigned int val;
  110. int ret;
  111. if (!rinfo)
  112. return 0;
  113. switch (fps_src) {
  114. case MAX77620_FPS_SRC_0:
  115. case MAX77620_FPS_SRC_1:
  116. case MAX77620_FPS_SRC_2:
  117. case MAX77620_FPS_SRC_NONE:
  118. break;
  119. case MAX77620_FPS_SRC_DEF:
  120. ret = regmap_read(pmic->rmap, rinfo->fps_addr, &val);
  121. if (ret < 0) {
  122. dev_err(pmic->dev, "Reg 0x%02x read failed %d\n",
  123. rinfo->fps_addr, ret);
  124. return ret;
  125. }
  126. ret = (val & MAX77620_FPS_SRC_MASK) >> MAX77620_FPS_SRC_SHIFT;
  127. pmic->active_fps_src[id] = ret;
  128. return 0;
  129. default:
  130. dev_err(pmic->dev, "Invalid FPS %d for regulator %d\n",
  131. fps_src, id);
  132. return -EINVAL;
  133. }
  134. ret = regmap_update_bits(pmic->rmap, rinfo->fps_addr,
  135. MAX77620_FPS_SRC_MASK,
  136. fps_src << MAX77620_FPS_SRC_SHIFT);
  137. if (ret < 0) {
  138. dev_err(pmic->dev, "Reg 0x%02x update failed %d\n",
  139. rinfo->fps_addr, ret);
  140. return ret;
  141. }
  142. pmic->active_fps_src[id] = fps_src;
  143. return 0;
  144. }
  145. static int max77620_regulator_set_fps_slots(struct max77620_regulator *pmic,
  146. int id, bool is_suspend)
  147. {
  148. struct max77620_regulator_pdata *rpdata = &pmic->reg_pdata[id];
  149. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  150. unsigned int val = 0;
  151. unsigned int mask = 0;
  152. int pu = rpdata->active_fps_pu_slot;
  153. int pd = rpdata->active_fps_pd_slot;
  154. int ret = 0;
  155. if (!rinfo)
  156. return 0;
  157. if (is_suspend) {
  158. pu = rpdata->suspend_fps_pu_slot;
  159. pd = rpdata->suspend_fps_pd_slot;
  160. }
  161. /* FPS power up period setting */
  162. if (pu >= 0) {
  163. val |= (pu << MAX77620_FPS_PU_PERIOD_SHIFT);
  164. mask |= MAX77620_FPS_PU_PERIOD_MASK;
  165. }
  166. /* FPS power down period setting */
  167. if (pd >= 0) {
  168. val |= (pd << MAX77620_FPS_PD_PERIOD_SHIFT);
  169. mask |= MAX77620_FPS_PD_PERIOD_MASK;
  170. }
  171. if (mask) {
  172. ret = regmap_update_bits(pmic->rmap, rinfo->fps_addr,
  173. mask, val);
  174. if (ret < 0) {
  175. dev_err(pmic->dev, "Reg 0x%02x update failed: %d\n",
  176. rinfo->fps_addr, ret);
  177. return ret;
  178. }
  179. }
  180. return ret;
  181. }
  182. static int max77620_regulator_set_power_mode(struct max77620_regulator *pmic,
  183. int power_mode, int id)
  184. {
  185. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  186. u8 mask = rinfo->power_mode_mask;
  187. u8 shift = rinfo->power_mode_shift;
  188. u8 addr;
  189. int ret;
  190. switch (rinfo->type) {
  191. case MAX77620_REGULATOR_TYPE_SD:
  192. addr = rinfo->cfg_addr;
  193. break;
  194. default:
  195. addr = rinfo->volt_addr;
  196. break;
  197. }
  198. ret = regmap_update_bits(pmic->rmap, addr, mask, power_mode << shift);
  199. if (ret < 0) {
  200. dev_err(pmic->dev, "Regulator %d mode set failed: %d\n",
  201. id, ret);
  202. return ret;
  203. }
  204. pmic->current_power_mode[id] = power_mode;
  205. return ret;
  206. }
  207. static int max77620_regulator_get_power_mode(struct max77620_regulator *pmic,
  208. int id)
  209. {
  210. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  211. unsigned int val, addr;
  212. u8 mask = rinfo->power_mode_mask;
  213. u8 shift = rinfo->power_mode_shift;
  214. int ret;
  215. switch (rinfo->type) {
  216. case MAX77620_REGULATOR_TYPE_SD:
  217. addr = rinfo->cfg_addr;
  218. break;
  219. default:
  220. addr = rinfo->volt_addr;
  221. break;
  222. }
  223. ret = regmap_read(pmic->rmap, addr, &val);
  224. if (ret < 0) {
  225. dev_err(pmic->dev, "Regulator %d: Reg 0x%02x read failed: %d\n",
  226. id, addr, ret);
  227. return ret;
  228. }
  229. return (val & mask) >> shift;
  230. }
  231. static int max77620_read_slew_rate(struct max77620_regulator *pmic, int id)
  232. {
  233. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  234. unsigned int rval;
  235. int slew_rate;
  236. int ret;
  237. ret = regmap_read(pmic->rmap, rinfo->cfg_addr, &rval);
  238. if (ret < 0) {
  239. dev_err(pmic->dev, "Register 0x%02x read failed: %d\n",
  240. rinfo->cfg_addr, ret);
  241. return ret;
  242. }
  243. switch (rinfo->type) {
  244. case MAX77620_REGULATOR_TYPE_SD:
  245. slew_rate = (rval >> MAX77620_SD_SR_SHIFT) & 0x3;
  246. switch (slew_rate) {
  247. case 0:
  248. slew_rate = 13750;
  249. break;
  250. case 1:
  251. slew_rate = 27500;
  252. break;
  253. case 2:
  254. slew_rate = 55000;
  255. break;
  256. case 3:
  257. slew_rate = 100000;
  258. break;
  259. }
  260. rinfo->desc.ramp_delay = slew_rate;
  261. break;
  262. default:
  263. slew_rate = rval & 0x1;
  264. switch (slew_rate) {
  265. case 0:
  266. slew_rate = 100000;
  267. break;
  268. case 1:
  269. slew_rate = 5000;
  270. break;
  271. }
  272. rinfo->desc.ramp_delay = slew_rate;
  273. break;
  274. }
  275. return 0;
  276. }
  277. static int max77620_set_slew_rate(struct max77620_regulator *pmic, int id,
  278. int slew_rate)
  279. {
  280. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  281. unsigned int val;
  282. int ret;
  283. u8 mask;
  284. if (rinfo->type == MAX77620_REGULATOR_TYPE_SD) {
  285. if (slew_rate <= 13750)
  286. val = 0;
  287. else if (slew_rate <= 27500)
  288. val = 1;
  289. else if (slew_rate <= 55000)
  290. val = 2;
  291. else
  292. val = 3;
  293. val <<= MAX77620_SD_SR_SHIFT;
  294. mask = MAX77620_SD_SR_MASK;
  295. } else {
  296. if (slew_rate <= 5000)
  297. val = 1;
  298. else
  299. val = 0;
  300. mask = MAX77620_LDO_SLEW_RATE_MASK;
  301. }
  302. ret = regmap_update_bits(pmic->rmap, rinfo->cfg_addr, mask, val);
  303. if (ret < 0) {
  304. dev_err(pmic->dev, "Regulator %d slew rate set failed: %d\n",
  305. id, ret);
  306. return ret;
  307. }
  308. return 0;
  309. }
  310. static int max77620_config_power_ok(struct max77620_regulator *pmic, int id)
  311. {
  312. struct max77620_regulator_pdata *rpdata = &pmic->reg_pdata[id];
  313. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  314. struct max77620_chip *chip = dev_get_drvdata(pmic->dev->parent);
  315. u8 val, mask;
  316. int ret;
  317. switch (chip->chip_id) {
  318. case MAX20024:
  319. if (rpdata->power_ok >= 0) {
  320. if (rinfo->type == MAX77620_REGULATOR_TYPE_SD)
  321. mask = MAX20024_SD_CFG1_MPOK_MASK;
  322. else
  323. mask = MAX20024_LDO_CFG2_MPOK_MASK;
  324. val = rpdata->power_ok ? mask : 0;
  325. ret = regmap_update_bits(pmic->rmap, rinfo->cfg_addr,
  326. mask, val);
  327. if (ret < 0) {
  328. dev_err(pmic->dev, "Reg 0x%02x update failed %d\n",
  329. rinfo->cfg_addr, ret);
  330. return ret;
  331. }
  332. }
  333. break;
  334. default:
  335. break;
  336. }
  337. return 0;
  338. }
  339. static int max77620_init_pmic(struct max77620_regulator *pmic, int id)
  340. {
  341. struct max77620_regulator_pdata *rpdata = &pmic->reg_pdata[id];
  342. int ret;
  343. max77620_config_power_ok(pmic, id);
  344. /* Update power mode */
  345. ret = max77620_regulator_get_power_mode(pmic, id);
  346. if (ret < 0)
  347. return ret;
  348. pmic->current_power_mode[id] = ret;
  349. pmic->enable_power_mode[id] = MAX77620_POWER_MODE_NORMAL;
  350. if (rpdata->active_fps_src == MAX77620_FPS_SRC_DEF) {
  351. ret = max77620_regulator_get_fps_src(pmic, id);
  352. if (ret < 0)
  353. return ret;
  354. rpdata->active_fps_src = ret;
  355. }
  356. /* If rails are externally control of FPS then enable it always. */
  357. if (rpdata->active_fps_src == MAX77620_FPS_SRC_NONE) {
  358. ret = max77620_regulator_set_power_mode(pmic,
  359. pmic->enable_power_mode[id], id);
  360. if (ret < 0)
  361. return ret;
  362. } else {
  363. if (pmic->current_power_mode[id] !=
  364. pmic->enable_power_mode[id]) {
  365. ret = max77620_regulator_set_power_mode(pmic,
  366. pmic->enable_power_mode[id], id);
  367. if (ret < 0)
  368. return ret;
  369. }
  370. }
  371. ret = max77620_regulator_set_fps_src(pmic, rpdata->active_fps_src, id);
  372. if (ret < 0)
  373. return ret;
  374. ret = max77620_regulator_set_fps_slots(pmic, id, false);
  375. if (ret < 0)
  376. return ret;
  377. if (rpdata->ramp_rate_setting) {
  378. ret = max77620_set_slew_rate(pmic, id,
  379. rpdata->ramp_rate_setting);
  380. if (ret < 0)
  381. return ret;
  382. }
  383. return 0;
  384. }
  385. static int max77620_regulator_enable(struct regulator_dev *rdev)
  386. {
  387. struct max77620_regulator *pmic = rdev_get_drvdata(rdev);
  388. int id = rdev_get_id(rdev);
  389. if (pmic->active_fps_src[id] != MAX77620_FPS_SRC_NONE)
  390. return 0;
  391. return max77620_regulator_set_power_mode(pmic,
  392. pmic->enable_power_mode[id], id);
  393. }
  394. static int max77620_regulator_disable(struct regulator_dev *rdev)
  395. {
  396. struct max77620_regulator *pmic = rdev_get_drvdata(rdev);
  397. int id = rdev_get_id(rdev);
  398. if (pmic->active_fps_src[id] != MAX77620_FPS_SRC_NONE)
  399. return 0;
  400. return max77620_regulator_set_power_mode(pmic,
  401. MAX77620_POWER_MODE_DISABLE, id);
  402. }
  403. static int max77620_regulator_is_enabled(struct regulator_dev *rdev)
  404. {
  405. struct max77620_regulator *pmic = rdev_get_drvdata(rdev);
  406. int id = rdev_get_id(rdev);
  407. int ret = 1;
  408. if (pmic->active_fps_src[id] != MAX77620_FPS_SRC_NONE)
  409. return 1;
  410. ret = max77620_regulator_get_power_mode(pmic, id);
  411. if (ret < 0)
  412. return ret;
  413. if (ret != MAX77620_POWER_MODE_DISABLE)
  414. return 1;
  415. return 0;
  416. }
  417. static int max77620_regulator_set_mode(struct regulator_dev *rdev,
  418. unsigned int mode)
  419. {
  420. struct max77620_regulator *pmic = rdev_get_drvdata(rdev);
  421. int id = rdev_get_id(rdev);
  422. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  423. struct max77620_regulator_pdata *rpdata = &pmic->reg_pdata[id];
  424. bool fpwm = false;
  425. int power_mode;
  426. int ret;
  427. u8 val;
  428. switch (mode) {
  429. case REGULATOR_MODE_FAST:
  430. fpwm = true;
  431. power_mode = MAX77620_POWER_MODE_NORMAL;
  432. break;
  433. case REGULATOR_MODE_NORMAL:
  434. power_mode = MAX77620_POWER_MODE_NORMAL;
  435. break;
  436. case REGULATOR_MODE_IDLE:
  437. power_mode = MAX77620_POWER_MODE_LPM;
  438. break;
  439. default:
  440. dev_err(pmic->dev, "Regulator %d mode %d is invalid\n",
  441. id, mode);
  442. return -EINVAL;
  443. }
  444. if (rinfo->type != MAX77620_REGULATOR_TYPE_SD)
  445. goto skip_fpwm;
  446. val = (fpwm) ? MAX77620_SD_FPWM_MASK : 0;
  447. ret = regmap_update_bits(pmic->rmap, rinfo->cfg_addr,
  448. MAX77620_SD_FPWM_MASK, val);
  449. if (ret < 0) {
  450. dev_err(pmic->dev, "Reg 0x%02x update failed: %d\n",
  451. rinfo->cfg_addr, ret);
  452. return ret;
  453. }
  454. rpdata->current_mode = mode;
  455. skip_fpwm:
  456. ret = max77620_regulator_set_power_mode(pmic, power_mode, id);
  457. if (ret < 0)
  458. return ret;
  459. pmic->enable_power_mode[id] = power_mode;
  460. return 0;
  461. }
  462. static unsigned int max77620_regulator_get_mode(struct regulator_dev *rdev)
  463. {
  464. struct max77620_regulator *pmic = rdev_get_drvdata(rdev);
  465. int id = rdev_get_id(rdev);
  466. struct max77620_regulator_info *rinfo = pmic->rinfo[id];
  467. int fpwm = 0;
  468. int ret;
  469. int pm_mode, reg_mode;
  470. unsigned int val;
  471. ret = max77620_regulator_get_power_mode(pmic, id);
  472. if (ret < 0)
  473. return 0;
  474. pm_mode = ret;
  475. if (rinfo->type == MAX77620_REGULATOR_TYPE_SD) {
  476. ret = regmap_read(pmic->rmap, rinfo->cfg_addr, &val);
  477. if (ret < 0) {
  478. dev_err(pmic->dev, "Reg 0x%02x read failed: %d\n",
  479. rinfo->cfg_addr, ret);
  480. return ret;
  481. }
  482. fpwm = !!(val & MAX77620_SD_FPWM_MASK);
  483. }
  484. switch (pm_mode) {
  485. case MAX77620_POWER_MODE_NORMAL:
  486. case MAX77620_POWER_MODE_DISABLE:
  487. if (fpwm)
  488. reg_mode = REGULATOR_MODE_FAST;
  489. else
  490. reg_mode = REGULATOR_MODE_NORMAL;
  491. break;
  492. case MAX77620_POWER_MODE_LPM:
  493. case MAX77620_POWER_MODE_GLPM:
  494. reg_mode = REGULATOR_MODE_IDLE;
  495. break;
  496. default:
  497. return 0;
  498. }
  499. return reg_mode;
  500. }
  501. static int max77620_regulator_set_ramp_delay(struct regulator_dev *rdev,
  502. int ramp_delay)
  503. {
  504. struct max77620_regulator *pmic = rdev_get_drvdata(rdev);
  505. int id = rdev_get_id(rdev);
  506. struct max77620_regulator_pdata *rpdata = &pmic->reg_pdata[id];
  507. /* Device specific ramp rate setting tells that platform has
  508. * different ramp rate from advertised value. In this case,
  509. * do not configure anything and just return success.
  510. */
  511. if (rpdata->ramp_rate_setting)
  512. return 0;
  513. return max77620_set_slew_rate(pmic, id, ramp_delay);
  514. }
  515. static int max77620_of_parse_cb(struct device_node *np,
  516. const struct regulator_desc *desc,
  517. struct regulator_config *config)
  518. {
  519. struct max77620_regulator *pmic = config->driver_data;
  520. struct max77620_regulator_pdata *rpdata = &pmic->reg_pdata[desc->id];
  521. u32 pval;
  522. int ret;
  523. ret = of_property_read_u32(np, "maxim,active-fps-source", &pval);
  524. rpdata->active_fps_src = (!ret) ? pval : MAX77620_FPS_SRC_DEF;
  525. ret = of_property_read_u32(np, "maxim,active-fps-power-up-slot", &pval);
  526. rpdata->active_fps_pu_slot = (!ret) ? pval : -1;
  527. ret = of_property_read_u32(
  528. np, "maxim,active-fps-power-down-slot", &pval);
  529. rpdata->active_fps_pd_slot = (!ret) ? pval : -1;
  530. ret = of_property_read_u32(np, "maxim,suspend-fps-source", &pval);
  531. rpdata->suspend_fps_src = (!ret) ? pval : -1;
  532. ret = of_property_read_u32(
  533. np, "maxim,suspend-fps-power-up-slot", &pval);
  534. rpdata->suspend_fps_pu_slot = (!ret) ? pval : -1;
  535. ret = of_property_read_u32(
  536. np, "maxim,suspend-fps-power-down-slot", &pval);
  537. rpdata->suspend_fps_pd_slot = (!ret) ? pval : -1;
  538. ret = of_property_read_u32(np, "maxim,power-ok-control", &pval);
  539. if (!ret)
  540. rpdata->power_ok = pval;
  541. else
  542. rpdata->power_ok = -1;
  543. ret = of_property_read_u32(np, "maxim,ramp-rate-setting", &pval);
  544. rpdata->ramp_rate_setting = (!ret) ? pval : 0;
  545. return max77620_init_pmic(pmic, desc->id);
  546. }
  547. static const struct regulator_ops max77620_regulator_ops = {
  548. .is_enabled = max77620_regulator_is_enabled,
  549. .enable = max77620_regulator_enable,
  550. .disable = max77620_regulator_disable,
  551. .list_voltage = regulator_list_voltage_linear,
  552. .map_voltage = regulator_map_voltage_linear,
  553. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  554. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  555. .set_mode = max77620_regulator_set_mode,
  556. .get_mode = max77620_regulator_get_mode,
  557. .set_ramp_delay = max77620_regulator_set_ramp_delay,
  558. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  559. .set_active_discharge = regulator_set_active_discharge_regmap,
  560. };
  561. #define MAX77620_SD_CNF2_ROVS_EN_NONE 0
  562. #define RAIL_SD(_id, _name, _sname, _volt_mask, _min_uV, _max_uV, \
  563. _step_uV, _rs_add, _rs_mask) \
  564. [MAX77620_REGULATOR_ID_##_id] = { \
  565. .type = MAX77620_REGULATOR_TYPE_SD, \
  566. .volt_addr = MAX77620_REG_##_id, \
  567. .cfg_addr = MAX77620_REG_##_id##_CFG, \
  568. .fps_addr = MAX77620_REG_FPS_##_id, \
  569. .remote_sense_addr = _rs_add, \
  570. .remote_sense_mask = MAX77620_SD_CNF2_ROVS_EN_##_rs_mask, \
  571. .power_mode_mask = MAX77620_SD_POWER_MODE_MASK, \
  572. .power_mode_shift = MAX77620_SD_POWER_MODE_SHIFT, \
  573. .desc = { \
  574. .name = max77620_rails(_name), \
  575. .of_match = of_match_ptr(#_name), \
  576. .regulators_node = of_match_ptr("regulators"), \
  577. .of_parse_cb = max77620_of_parse_cb, \
  578. .supply_name = _sname, \
  579. .id = MAX77620_REGULATOR_ID_##_id, \
  580. .ops = &max77620_regulator_ops, \
  581. .n_voltages = ((_max_uV - _min_uV) / _step_uV) + 1, \
  582. .min_uV = _min_uV, \
  583. .uV_step = _step_uV, \
  584. .enable_time = 500, \
  585. .vsel_mask = MAX77620_##_volt_mask##_VOLT_MASK, \
  586. .vsel_reg = MAX77620_REG_##_id, \
  587. .active_discharge_off = 0, \
  588. .active_discharge_on = MAX77620_SD_CFG1_ADE_ENABLE, \
  589. .active_discharge_mask = MAX77620_SD_CFG1_ADE_MASK, \
  590. .active_discharge_reg = MAX77620_REG_##_id##_CFG, \
  591. .type = REGULATOR_VOLTAGE, \
  592. }, \
  593. }
  594. #define RAIL_LDO(_id, _name, _sname, _type, _min_uV, _max_uV, _step_uV) \
  595. [MAX77620_REGULATOR_ID_##_id] = { \
  596. .type = MAX77620_REGULATOR_TYPE_LDO_##_type, \
  597. .volt_addr = MAX77620_REG_##_id##_CFG, \
  598. .cfg_addr = MAX77620_REG_##_id##_CFG2, \
  599. .fps_addr = MAX77620_REG_FPS_##_id, \
  600. .remote_sense_addr = 0xFF, \
  601. .power_mode_mask = MAX77620_LDO_POWER_MODE_MASK, \
  602. .power_mode_shift = MAX77620_LDO_POWER_MODE_SHIFT, \
  603. .desc = { \
  604. .name = max77620_rails(_name), \
  605. .of_match = of_match_ptr(#_name), \
  606. .regulators_node = of_match_ptr("regulators"), \
  607. .of_parse_cb = max77620_of_parse_cb, \
  608. .supply_name = _sname, \
  609. .id = MAX77620_REGULATOR_ID_##_id, \
  610. .ops = &max77620_regulator_ops, \
  611. .n_voltages = ((_max_uV - _min_uV) / _step_uV) + 1, \
  612. .min_uV = _min_uV, \
  613. .uV_step = _step_uV, \
  614. .enable_time = 500, \
  615. .vsel_mask = MAX77620_LDO_VOLT_MASK, \
  616. .vsel_reg = MAX77620_REG_##_id##_CFG, \
  617. .active_discharge_off = 0, \
  618. .active_discharge_on = MAX77620_LDO_CFG2_ADE_ENABLE, \
  619. .active_discharge_mask = MAX77620_LDO_CFG2_ADE_MASK, \
  620. .active_discharge_reg = MAX77620_REG_##_id##_CFG2, \
  621. .type = REGULATOR_VOLTAGE, \
  622. }, \
  623. }
  624. static struct max77620_regulator_info max77620_regs_info[MAX77620_NUM_REGS] = {
  625. RAIL_SD(SD0, sd0, "in-sd0", SD0, 600000, 1400000, 12500, 0x22, SD0),
  626. RAIL_SD(SD1, sd1, "in-sd1", SD1, 600000, 1550000, 12500, 0x22, SD1),
  627. RAIL_SD(SD2, sd2, "in-sd2", SDX, 600000, 3787500, 12500, 0xFF, NONE),
  628. RAIL_SD(SD3, sd3, "in-sd3", SDX, 600000, 3787500, 12500, 0xFF, NONE),
  629. RAIL_LDO(LDO0, ldo0, "in-ldo0-1", N, 800000, 2375000, 25000),
  630. RAIL_LDO(LDO1, ldo1, "in-ldo0-1", N, 800000, 2375000, 25000),
  631. RAIL_LDO(LDO2, ldo2, "in-ldo2", P, 800000, 3950000, 50000),
  632. RAIL_LDO(LDO3, ldo3, "in-ldo3-5", P, 800000, 3950000, 50000),
  633. RAIL_LDO(LDO4, ldo4, "in-ldo4-6", P, 800000, 1587500, 12500),
  634. RAIL_LDO(LDO5, ldo5, "in-ldo3-5", P, 800000, 3950000, 50000),
  635. RAIL_LDO(LDO6, ldo6, "in-ldo4-6", P, 800000, 3950000, 50000),
  636. RAIL_LDO(LDO7, ldo7, "in-ldo7-8", N, 800000, 3950000, 50000),
  637. RAIL_LDO(LDO8, ldo8, "in-ldo7-8", N, 800000, 3950000, 50000),
  638. };
  639. static struct max77620_regulator_info max20024_regs_info[MAX77620_NUM_REGS] = {
  640. RAIL_SD(SD0, sd0, "in-sd0", SD0, 800000, 1587500, 12500, 0x22, SD0),
  641. RAIL_SD(SD1, sd1, "in-sd1", SD1, 600000, 3387500, 12500, 0x22, SD1),
  642. RAIL_SD(SD2, sd2, "in-sd2", SDX, 600000, 3787500, 12500, 0xFF, NONE),
  643. RAIL_SD(SD3, sd3, "in-sd3", SDX, 600000, 3787500, 12500, 0xFF, NONE),
  644. RAIL_SD(SD4, sd4, "in-sd4", SDX, 600000, 3787500, 12500, 0xFF, NONE),
  645. RAIL_LDO(LDO0, ldo0, "in-ldo0-1", N, 800000, 2375000, 25000),
  646. RAIL_LDO(LDO1, ldo1, "in-ldo0-1", N, 800000, 2375000, 25000),
  647. RAIL_LDO(LDO2, ldo2, "in-ldo2", P, 800000, 3950000, 50000),
  648. RAIL_LDO(LDO3, ldo3, "in-ldo3-5", P, 800000, 3950000, 50000),
  649. RAIL_LDO(LDO4, ldo4, "in-ldo4-6", P, 800000, 1587500, 12500),
  650. RAIL_LDO(LDO5, ldo5, "in-ldo3-5", P, 800000, 3950000, 50000),
  651. RAIL_LDO(LDO6, ldo6, "in-ldo4-6", P, 800000, 3950000, 50000),
  652. RAIL_LDO(LDO7, ldo7, "in-ldo7-8", N, 800000, 3950000, 50000),
  653. RAIL_LDO(LDO8, ldo8, "in-ldo7-8", N, 800000, 3950000, 50000),
  654. };
  655. static int max77620_regulator_probe(struct platform_device *pdev)
  656. {
  657. struct max77620_chip *max77620_chip = dev_get_drvdata(pdev->dev.parent);
  658. struct max77620_regulator_info *rinfo;
  659. struct device *dev = &pdev->dev;
  660. struct regulator_config config = { };
  661. struct max77620_regulator *pmic;
  662. int ret = 0;
  663. int id;
  664. pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
  665. if (!pmic)
  666. return -ENOMEM;
  667. platform_set_drvdata(pdev, pmic);
  668. pmic->dev = dev;
  669. pmic->rmap = max77620_chip->rmap;
  670. if (!dev->of_node)
  671. dev->of_node = pdev->dev.parent->of_node;
  672. switch (max77620_chip->chip_id) {
  673. case MAX77620:
  674. rinfo = max77620_regs_info;
  675. break;
  676. default:
  677. rinfo = max20024_regs_info;
  678. break;
  679. }
  680. config.regmap = pmic->rmap;
  681. config.dev = dev;
  682. config.driver_data = pmic;
  683. /*
  684. * Set of_node_reuse flag to prevent driver core from attempting to
  685. * claim any pinmux resources already claimed by the parent device.
  686. * Otherwise PMIC driver will fail to re-probe.
  687. */
  688. device_set_of_node_from_dev(&pdev->dev, pdev->dev.parent);
  689. for (id = 0; id < MAX77620_NUM_REGS; id++) {
  690. struct regulator_dev *rdev;
  691. struct regulator_desc *rdesc;
  692. if ((max77620_chip->chip_id == MAX77620) &&
  693. (id == MAX77620_REGULATOR_ID_SD4))
  694. continue;
  695. rdesc = &rinfo[id].desc;
  696. pmic->rinfo[id] = &max77620_regs_info[id];
  697. pmic->enable_power_mode[id] = MAX77620_POWER_MODE_NORMAL;
  698. pmic->reg_pdata[id].active_fps_src = -1;
  699. pmic->reg_pdata[id].active_fps_pd_slot = -1;
  700. pmic->reg_pdata[id].active_fps_pu_slot = -1;
  701. pmic->reg_pdata[id].suspend_fps_src = -1;
  702. pmic->reg_pdata[id].suspend_fps_pd_slot = -1;
  703. pmic->reg_pdata[id].suspend_fps_pu_slot = -1;
  704. pmic->reg_pdata[id].power_ok = -1;
  705. pmic->reg_pdata[id].ramp_rate_setting = -1;
  706. ret = max77620_read_slew_rate(pmic, id);
  707. if (ret < 0)
  708. return ret;
  709. rdev = devm_regulator_register(dev, rdesc, &config);
  710. if (IS_ERR(rdev)) {
  711. ret = PTR_ERR(rdev);
  712. dev_err(dev, "Regulator registration %s failed: %d\n",
  713. rdesc->name, ret);
  714. return ret;
  715. }
  716. }
  717. return 0;
  718. }
  719. #ifdef CONFIG_PM_SLEEP
  720. static int max77620_regulator_suspend(struct device *dev)
  721. {
  722. struct max77620_regulator *pmic = dev_get_drvdata(dev);
  723. struct max77620_regulator_pdata *reg_pdata;
  724. int id;
  725. for (id = 0; id < MAX77620_NUM_REGS; id++) {
  726. reg_pdata = &pmic->reg_pdata[id];
  727. max77620_regulator_set_fps_slots(pmic, id, true);
  728. if (reg_pdata->suspend_fps_src < 0)
  729. continue;
  730. max77620_regulator_set_fps_src(pmic, reg_pdata->suspend_fps_src,
  731. id);
  732. }
  733. return 0;
  734. }
  735. static int max77620_regulator_resume(struct device *dev)
  736. {
  737. struct max77620_regulator *pmic = dev_get_drvdata(dev);
  738. struct max77620_regulator_pdata *reg_pdata;
  739. int id;
  740. for (id = 0; id < MAX77620_NUM_REGS; id++) {
  741. reg_pdata = &pmic->reg_pdata[id];
  742. max77620_config_power_ok(pmic, id);
  743. max77620_regulator_set_fps_slots(pmic, id, false);
  744. if (reg_pdata->active_fps_src < 0)
  745. continue;
  746. max77620_regulator_set_fps_src(pmic, reg_pdata->active_fps_src,
  747. id);
  748. }
  749. return 0;
  750. }
  751. #endif
  752. static const struct dev_pm_ops max77620_regulator_pm_ops = {
  753. SET_SYSTEM_SLEEP_PM_OPS(max77620_regulator_suspend,
  754. max77620_regulator_resume)
  755. };
  756. static const struct platform_device_id max77620_regulator_devtype[] = {
  757. { .name = "max77620-pmic", },
  758. { .name = "max20024-pmic", },
  759. {},
  760. };
  761. MODULE_DEVICE_TABLE(platform, max77620_regulator_devtype);
  762. static struct platform_driver max77620_regulator_driver = {
  763. .probe = max77620_regulator_probe,
  764. .id_table = max77620_regulator_devtype,
  765. .driver = {
  766. .name = "max77620-pmic",
  767. .pm = &max77620_regulator_pm_ops,
  768. },
  769. };
  770. module_platform_driver(max77620_regulator_driver);
  771. MODULE_DESCRIPTION("MAX77620/MAX20024 regulator driver");
  772. MODULE_AUTHOR("Mallikarjun Kasoju <mkasoju@nvidia.com>");
  773. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  774. MODULE_LICENSE("GPL v2");