mcp16502.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // MCP16502 PMIC driver
  4. //
  5. // Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries
  6. //
  7. // Author: Andrei Stefanescu <andrei.stefanescu@microchip.com>
  8. //
  9. // Inspired from tps65086-regulator.c
  10. #include <linux/gpio.h>
  11. #include <linux/i2c.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/driver.h>
  18. #include <linux/suspend.h>
  19. #include <linux/gpio/consumer.h>
  20. #define VDD_LOW_SEL 0x0D
  21. #define VDD_HIGH_SEL 0x3F
  22. #define MCP16502_FLT BIT(7)
  23. #define MCP16502_ENS BIT(0)
  24. /*
  25. * The PMIC has four sets of registers corresponding to four power modes:
  26. * Performance, Active, Low-power, Hibernate.
  27. *
  28. * Registers:
  29. * Each regulator has a register for each power mode. To access a register
  30. * for a specific regulator and mode BASE_* and OFFSET_* need to be added.
  31. *
  32. * Operating modes:
  33. * In order for the PMIC to transition to operating modes it has to be
  34. * controlled via GPIO lines called LPM and HPM.
  35. *
  36. * The registers are fully configurable such that you can put all regulators in
  37. * a low-power state while the PMIC is in Active mode. They are supposed to be
  38. * configured at startup and then simply transition to/from a global low-power
  39. * state by setting the GPIO lpm pin high/low.
  40. *
  41. * This driver keeps the PMIC in Active mode, Low-power state is set for the
  42. * regulators by enabling/disabling operating mode (FPWM or Auto PFM).
  43. *
  44. * The PMIC's Low-power and Hibernate modes are used during standby/suspend.
  45. * To enter standby/suspend the PMIC will go to Low-power mode. From there, it
  46. * will transition to Hibernate when the PWRHLD line is set to low by the MPU.
  47. */
  48. /*
  49. * This function is useful for iterating over all regulators and accessing their
  50. * registers in a generic way or accessing a regulator device by its id.
  51. */
  52. #define MCP16502_BASE(i) (((i) + 1) << 4)
  53. #define MCP16502_STAT_BASE(i) ((i) + 5)
  54. #define MCP16502_OFFSET_MODE_A 0
  55. #define MCP16502_OFFSET_MODE_LPM 1
  56. #define MCP16502_OFFSET_MODE_HIB 2
  57. #define MCP16502_OPMODE_ACTIVE REGULATOR_MODE_NORMAL
  58. #define MCP16502_OPMODE_LPM REGULATOR_MODE_IDLE
  59. #define MCP16502_OPMODE_HIB REGULATOR_MODE_STANDBY
  60. #define MCP16502_MODE_AUTO_PFM 0
  61. #define MCP16502_MODE_FPWM BIT(6)
  62. #define MCP16502_VSEL 0x3F
  63. #define MCP16502_EN BIT(7)
  64. #define MCP16502_MODE BIT(6)
  65. #define MCP16502_MIN_REG 0x0
  66. #define MCP16502_MAX_REG 0x65
  67. static unsigned int mcp16502_of_map_mode(unsigned int mode)
  68. {
  69. if (mode == REGULATOR_MODE_NORMAL || mode == REGULATOR_MODE_IDLE)
  70. return mode;
  71. return REGULATOR_MODE_INVALID;
  72. }
  73. #define MCP16502_REGULATOR(_name, _id, _ranges, _ops) \
  74. [_id] = { \
  75. .name = _name, \
  76. .regulators_node = of_match_ptr("regulators"), \
  77. .id = _id, \
  78. .ops = &(_ops), \
  79. .type = REGULATOR_VOLTAGE, \
  80. .owner = THIS_MODULE, \
  81. .n_voltages = MCP16502_VSEL + 1, \
  82. .linear_ranges = _ranges, \
  83. .n_linear_ranges = ARRAY_SIZE(_ranges), \
  84. .of_match = of_match_ptr(_name), \
  85. .of_map_mode = mcp16502_of_map_mode, \
  86. .vsel_reg = (((_id) + 1) << 4), \
  87. .vsel_mask = MCP16502_VSEL, \
  88. .enable_reg = (((_id) + 1) << 4), \
  89. .enable_mask = MCP16502_EN, \
  90. }
  91. enum {
  92. BUCK1 = 0,
  93. BUCK2,
  94. BUCK3,
  95. BUCK4,
  96. LDO1,
  97. LDO2,
  98. NUM_REGULATORS
  99. };
  100. /*
  101. * struct mcp16502 - PMIC representation
  102. * @rdev: the regulators belonging to this chip
  103. * @rmap: regmap to be used for I2C communication
  104. * @lpm: LPM GPIO descriptor
  105. */
  106. struct mcp16502 {
  107. struct gpio_desc *lpm;
  108. };
  109. /*
  110. * mcp16502_gpio_set_mode() - set the GPIO corresponding value
  111. *
  112. * Used to prepare transitioning into hibernate or resuming from it.
  113. */
  114. static void mcp16502_gpio_set_mode(struct mcp16502 *mcp, int mode)
  115. {
  116. switch (mode) {
  117. case MCP16502_OPMODE_ACTIVE:
  118. gpiod_set_value(mcp->lpm, 0);
  119. break;
  120. case MCP16502_OPMODE_LPM:
  121. case MCP16502_OPMODE_HIB:
  122. gpiod_set_value(mcp->lpm, 1);
  123. break;
  124. default:
  125. pr_err("%s: %d invalid\n", __func__, mode);
  126. }
  127. }
  128. /*
  129. * mcp16502_get_reg() - get the PMIC's configuration register for opmode
  130. *
  131. * @rdev: the regulator whose register we are searching
  132. * @opmode: the PMIC's operating mode ACTIVE, Low-power, Hibernate
  133. */
  134. static int mcp16502_get_reg(struct regulator_dev *rdev, int opmode)
  135. {
  136. int reg = MCP16502_BASE(rdev_get_id(rdev));
  137. switch (opmode) {
  138. case MCP16502_OPMODE_ACTIVE:
  139. return reg + MCP16502_OFFSET_MODE_A;
  140. case MCP16502_OPMODE_LPM:
  141. return reg + MCP16502_OFFSET_MODE_LPM;
  142. case MCP16502_OPMODE_HIB:
  143. return reg + MCP16502_OFFSET_MODE_HIB;
  144. default:
  145. return -EINVAL;
  146. }
  147. }
  148. /*
  149. * mcp16502_get_mode() - return the current operating mode of a regulator
  150. *
  151. * Note: all functions that are not part of entering/exiting standby/suspend
  152. * use the Active mode registers.
  153. *
  154. * Note: this is different from the PMIC's operatig mode, it is the
  155. * MODE bit from the regulator's register.
  156. */
  157. static unsigned int mcp16502_get_mode(struct regulator_dev *rdev)
  158. {
  159. unsigned int val;
  160. int ret, reg;
  161. reg = mcp16502_get_reg(rdev, MCP16502_OPMODE_ACTIVE);
  162. if (reg < 0)
  163. return reg;
  164. ret = regmap_read(rdev->regmap, reg, &val);
  165. if (ret)
  166. return ret;
  167. switch (val & MCP16502_MODE) {
  168. case MCP16502_MODE_FPWM:
  169. return REGULATOR_MODE_NORMAL;
  170. case MCP16502_MODE_AUTO_PFM:
  171. return REGULATOR_MODE_IDLE;
  172. default:
  173. return REGULATOR_MODE_INVALID;
  174. }
  175. }
  176. /*
  177. * _mcp16502_set_mode() - helper for set_mode and set_suspend_mode
  178. *
  179. * @rdev: the regulator for which we are setting the mode
  180. * @mode: the regulator's mode (the one from MODE bit)
  181. * @opmode: the PMIC's operating mode: Active/Low-power/Hibernate
  182. */
  183. static int _mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode,
  184. unsigned int op_mode)
  185. {
  186. int val;
  187. int reg;
  188. reg = mcp16502_get_reg(rdev, op_mode);
  189. if (reg < 0)
  190. return reg;
  191. switch (mode) {
  192. case REGULATOR_MODE_NORMAL:
  193. val = MCP16502_MODE_FPWM;
  194. break;
  195. case REGULATOR_MODE_IDLE:
  196. val = MCP16502_MODE_AUTO_PFM;
  197. break;
  198. default:
  199. return -EINVAL;
  200. }
  201. reg = regmap_update_bits(rdev->regmap, reg, MCP16502_MODE, val);
  202. return reg;
  203. }
  204. /*
  205. * mcp16502_set_mode() - regulator_ops set_mode
  206. */
  207. static int mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode)
  208. {
  209. return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_ACTIVE);
  210. }
  211. /*
  212. * mcp16502_get_status() - regulator_ops get_status
  213. */
  214. static int mcp16502_get_status(struct regulator_dev *rdev)
  215. {
  216. int ret;
  217. unsigned int val;
  218. ret = regmap_read(rdev->regmap, MCP16502_STAT_BASE(rdev_get_id(rdev)),
  219. &val);
  220. if (ret)
  221. return ret;
  222. if (val & MCP16502_FLT)
  223. return REGULATOR_STATUS_ERROR;
  224. else if (val & MCP16502_ENS)
  225. return REGULATOR_STATUS_ON;
  226. else if (!(val & MCP16502_ENS))
  227. return REGULATOR_STATUS_OFF;
  228. return REGULATOR_STATUS_UNDEFINED;
  229. }
  230. #ifdef CONFIG_SUSPEND
  231. /*
  232. * mcp16502_suspend_get_target_reg() - get the reg of the target suspend PMIC
  233. * mode
  234. */
  235. static int mcp16502_suspend_get_target_reg(struct regulator_dev *rdev)
  236. {
  237. switch (pm_suspend_target_state) {
  238. case PM_SUSPEND_STANDBY:
  239. return mcp16502_get_reg(rdev, MCP16502_OPMODE_LPM);
  240. case PM_SUSPEND_ON:
  241. case PM_SUSPEND_MEM:
  242. return mcp16502_get_reg(rdev, MCP16502_OPMODE_HIB);
  243. default:
  244. dev_err(&rdev->dev, "invalid suspend target: %d\n",
  245. pm_suspend_target_state);
  246. }
  247. return -EINVAL;
  248. }
  249. /*
  250. * mcp16502_set_suspend_voltage() - regulator_ops set_suspend_voltage
  251. */
  252. static int mcp16502_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  253. {
  254. int sel = regulator_map_voltage_linear_range(rdev, uV, uV);
  255. int reg = mcp16502_suspend_get_target_reg(rdev);
  256. if (sel < 0)
  257. return sel;
  258. if (reg < 0)
  259. return reg;
  260. return regmap_update_bits(rdev->regmap, reg, MCP16502_VSEL, sel);
  261. }
  262. /*
  263. * mcp16502_set_suspend_mode() - regulator_ops set_suspend_mode
  264. */
  265. static int mcp16502_set_suspend_mode(struct regulator_dev *rdev,
  266. unsigned int mode)
  267. {
  268. switch (pm_suspend_target_state) {
  269. case PM_SUSPEND_STANDBY:
  270. return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_LPM);
  271. case PM_SUSPEND_ON:
  272. case PM_SUSPEND_MEM:
  273. return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_HIB);
  274. default:
  275. dev_err(&rdev->dev, "invalid suspend target: %d\n",
  276. pm_suspend_target_state);
  277. }
  278. return -EINVAL;
  279. }
  280. /*
  281. * mcp16502_set_suspend_enable() - regulator_ops set_suspend_enable
  282. */
  283. static int mcp16502_set_suspend_enable(struct regulator_dev *rdev)
  284. {
  285. int reg = mcp16502_suspend_get_target_reg(rdev);
  286. if (reg < 0)
  287. return reg;
  288. return regmap_update_bits(rdev->regmap, reg, MCP16502_EN, MCP16502_EN);
  289. }
  290. /*
  291. * mcp16502_set_suspend_disable() - regulator_ops set_suspend_disable
  292. */
  293. static int mcp16502_set_suspend_disable(struct regulator_dev *rdev)
  294. {
  295. int reg = mcp16502_suspend_get_target_reg(rdev);
  296. if (reg < 0)
  297. return reg;
  298. return regmap_update_bits(rdev->regmap, reg, MCP16502_EN, 0);
  299. }
  300. #endif /* CONFIG_SUSPEND */
  301. static const struct regulator_ops mcp16502_buck_ops = {
  302. .list_voltage = regulator_list_voltage_linear_range,
  303. .map_voltage = regulator_map_voltage_linear_range,
  304. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  305. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  306. .enable = regulator_enable_regmap,
  307. .disable = regulator_disable_regmap,
  308. .is_enabled = regulator_is_enabled_regmap,
  309. .get_status = mcp16502_get_status,
  310. .set_mode = mcp16502_set_mode,
  311. .get_mode = mcp16502_get_mode,
  312. #ifdef CONFIG_SUSPEND
  313. .set_suspend_voltage = mcp16502_set_suspend_voltage,
  314. .set_suspend_mode = mcp16502_set_suspend_mode,
  315. .set_suspend_enable = mcp16502_set_suspend_enable,
  316. .set_suspend_disable = mcp16502_set_suspend_disable,
  317. #endif /* CONFIG_SUSPEND */
  318. };
  319. /*
  320. * LDOs cannot change operating modes.
  321. */
  322. static const struct regulator_ops mcp16502_ldo_ops = {
  323. .list_voltage = regulator_list_voltage_linear_range,
  324. .map_voltage = regulator_map_voltage_linear_range,
  325. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  326. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  327. .enable = regulator_enable_regmap,
  328. .disable = regulator_disable_regmap,
  329. .is_enabled = regulator_is_enabled_regmap,
  330. .get_status = mcp16502_get_status,
  331. #ifdef CONFIG_SUSPEND
  332. .set_suspend_voltage = mcp16502_set_suspend_voltage,
  333. .set_suspend_enable = mcp16502_set_suspend_enable,
  334. .set_suspend_disable = mcp16502_set_suspend_disable,
  335. #endif /* CONFIG_SUSPEND */
  336. };
  337. static const struct of_device_id mcp16502_ids[] = {
  338. { .compatible = "microchip,mcp16502", },
  339. {}
  340. };
  341. MODULE_DEVICE_TABLE(of, mcp16502_ids);
  342. static const struct linear_range b1l12_ranges[] = {
  343. REGULATOR_LINEAR_RANGE(1200000, VDD_LOW_SEL, VDD_HIGH_SEL, 50000),
  344. };
  345. static const struct linear_range b234_ranges[] = {
  346. REGULATOR_LINEAR_RANGE(600000, VDD_LOW_SEL, VDD_HIGH_SEL, 25000),
  347. };
  348. static const struct regulator_desc mcp16502_desc[] = {
  349. /* MCP16502_REGULATOR(_name, _id, ranges, regulator_ops) */
  350. MCP16502_REGULATOR("VDD_IO", BUCK1, b1l12_ranges, mcp16502_buck_ops),
  351. MCP16502_REGULATOR("VDD_DDR", BUCK2, b234_ranges, mcp16502_buck_ops),
  352. MCP16502_REGULATOR("VDD_CORE", BUCK3, b234_ranges, mcp16502_buck_ops),
  353. MCP16502_REGULATOR("VDD_OTHER", BUCK4, b234_ranges, mcp16502_buck_ops),
  354. MCP16502_REGULATOR("LDO1", LDO1, b1l12_ranges, mcp16502_ldo_ops),
  355. MCP16502_REGULATOR("LDO2", LDO2, b1l12_ranges, mcp16502_ldo_ops)
  356. };
  357. static const struct regmap_range mcp16502_ranges[] = {
  358. regmap_reg_range(MCP16502_MIN_REG, MCP16502_MAX_REG)
  359. };
  360. static const struct regmap_access_table mcp16502_yes_reg_table = {
  361. .yes_ranges = mcp16502_ranges,
  362. .n_yes_ranges = ARRAY_SIZE(mcp16502_ranges),
  363. };
  364. static const struct regmap_config mcp16502_regmap_config = {
  365. .reg_bits = 8,
  366. .val_bits = 8,
  367. .max_register = MCP16502_MAX_REG,
  368. .cache_type = REGCACHE_NONE,
  369. .rd_table = &mcp16502_yes_reg_table,
  370. .wr_table = &mcp16502_yes_reg_table,
  371. };
  372. static int mcp16502_probe(struct i2c_client *client,
  373. const struct i2c_device_id *id)
  374. {
  375. struct regulator_config config = { };
  376. struct regulator_dev *rdev;
  377. struct device *dev;
  378. struct mcp16502 *mcp;
  379. struct regmap *rmap;
  380. int i, ret;
  381. dev = &client->dev;
  382. config.dev = dev;
  383. mcp = devm_kzalloc(dev, sizeof(*mcp), GFP_KERNEL);
  384. if (!mcp)
  385. return -ENOMEM;
  386. rmap = devm_regmap_init_i2c(client, &mcp16502_regmap_config);
  387. if (IS_ERR(rmap)) {
  388. ret = PTR_ERR(rmap);
  389. dev_err(dev, "regmap init failed: %d\n", ret);
  390. return ret;
  391. }
  392. i2c_set_clientdata(client, mcp);
  393. config.regmap = rmap;
  394. config.driver_data = mcp;
  395. mcp->lpm = devm_gpiod_get(dev, "lpm", GPIOD_OUT_LOW);
  396. if (IS_ERR(mcp->lpm)) {
  397. dev_err(dev, "failed to get lpm pin: %ld\n", PTR_ERR(mcp->lpm));
  398. return PTR_ERR(mcp->lpm);
  399. }
  400. for (i = 0; i < NUM_REGULATORS; i++) {
  401. rdev = devm_regulator_register(dev, &mcp16502_desc[i], &config);
  402. if (IS_ERR(rdev)) {
  403. dev_err(dev,
  404. "failed to register %s regulator %ld\n",
  405. mcp16502_desc[i].name, PTR_ERR(rdev));
  406. return PTR_ERR(rdev);
  407. }
  408. }
  409. mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
  410. return 0;
  411. }
  412. #ifdef CONFIG_PM_SLEEP
  413. static int mcp16502_suspend_noirq(struct device *dev)
  414. {
  415. struct i2c_client *client = to_i2c_client(dev);
  416. struct mcp16502 *mcp = i2c_get_clientdata(client);
  417. mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_LPM);
  418. return 0;
  419. }
  420. static int mcp16502_resume_noirq(struct device *dev)
  421. {
  422. struct i2c_client *client = to_i2c_client(dev);
  423. struct mcp16502 *mcp = i2c_get_clientdata(client);
  424. mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
  425. return 0;
  426. }
  427. #endif
  428. #ifdef CONFIG_PM
  429. static const struct dev_pm_ops mcp16502_pm_ops = {
  430. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mcp16502_suspend_noirq,
  431. mcp16502_resume_noirq)
  432. };
  433. #endif
  434. static const struct i2c_device_id mcp16502_i2c_id[] = {
  435. { "mcp16502", 0 },
  436. { }
  437. };
  438. MODULE_DEVICE_TABLE(i2c, mcp16502_i2c_id);
  439. static struct i2c_driver mcp16502_drv = {
  440. .probe = mcp16502_probe,
  441. .driver = {
  442. .name = "mcp16502-regulator",
  443. .of_match_table = of_match_ptr(mcp16502_ids),
  444. #ifdef CONFIG_PM
  445. .pm = &mcp16502_pm_ops,
  446. #endif
  447. },
  448. .id_table = mcp16502_i2c_id,
  449. };
  450. module_i2c_driver(mcp16502_drv);
  451. MODULE_LICENSE("GPL v2");
  452. MODULE_DESCRIPTION("MCP16502 PMIC driver");
  453. MODULE_AUTHOR("Andrei Stefanescu andrei.stefanescu@microchip.com");