bd9571mwv-regulator.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * ROHM BD9571MWV-M regulator driver
  3. *
  4. * Copyright (C) 2017 Marek Vasut <marek.vasut+renesas@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether expressed or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License version 2 for more details.
  14. *
  15. * Based on the TPS65086 driver
  16. *
  17. * NOTE: VD09 is missing
  18. */
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/mfd/bd9571mwv.h>
  24. struct bd9571mwv_reg {
  25. struct bd9571mwv *bd;
  26. /* DDR Backup Power */
  27. u8 bkup_mode_cnt_keepon; /* from "rohm,ddr-backup-power" */
  28. u8 bkup_mode_cnt_saved;
  29. bool bkup_mode_enabled;
  30. /* Power switch type */
  31. bool rstbmode_level;
  32. bool rstbmode_pulse;
  33. };
  34. enum bd9571mwv_regulators { VD09, VD18, VD25, VD33, DVFS };
  35. #define BD9571MWV_REG(_name, _of, _id, _ops, _vr, _vm, _nv, _min, _step, _lmin)\
  36. { \
  37. .name = _name, \
  38. .of_match = of_match_ptr(_of), \
  39. .regulators_node = "regulators", \
  40. .id = _id, \
  41. .ops = &_ops, \
  42. .n_voltages = _nv, \
  43. .type = REGULATOR_VOLTAGE, \
  44. .owner = THIS_MODULE, \
  45. .vsel_reg = _vr, \
  46. .vsel_mask = _vm, \
  47. .min_uV = _min, \
  48. .uV_step = _step, \
  49. .linear_min_sel = _lmin, \
  50. }
  51. static int bd9571mwv_avs_get_moni_state(struct regulator_dev *rdev)
  52. {
  53. unsigned int val;
  54. int ret;
  55. ret = regmap_read(rdev->regmap, BD9571MWV_AVS_SET_MONI, &val);
  56. if (ret != 0)
  57. return ret;
  58. return val & BD9571MWV_AVS_SET_MONI_MASK;
  59. }
  60. static int bd9571mwv_avs_set_voltage_sel_regmap(struct regulator_dev *rdev,
  61. unsigned int sel)
  62. {
  63. int ret;
  64. ret = bd9571mwv_avs_get_moni_state(rdev);
  65. if (ret < 0)
  66. return ret;
  67. return regmap_write_bits(rdev->regmap, BD9571MWV_AVS_VD09_VID(ret),
  68. rdev->desc->vsel_mask, sel);
  69. }
  70. static int bd9571mwv_avs_get_voltage_sel_regmap(struct regulator_dev *rdev)
  71. {
  72. unsigned int val;
  73. int ret;
  74. ret = bd9571mwv_avs_get_moni_state(rdev);
  75. if (ret < 0)
  76. return ret;
  77. ret = regmap_read(rdev->regmap, BD9571MWV_AVS_VD09_VID(ret), &val);
  78. if (ret != 0)
  79. return ret;
  80. val &= rdev->desc->vsel_mask;
  81. val >>= ffs(rdev->desc->vsel_mask) - 1;
  82. return val;
  83. }
  84. static int bd9571mwv_reg_set_voltage_sel_regmap(struct regulator_dev *rdev,
  85. unsigned int sel)
  86. {
  87. return regmap_write_bits(rdev->regmap, BD9571MWV_DVFS_SETVID,
  88. rdev->desc->vsel_mask, sel);
  89. }
  90. /* Operations permitted on AVS voltage regulator */
  91. static struct regulator_ops avs_ops = {
  92. .set_voltage_sel = bd9571mwv_avs_set_voltage_sel_regmap,
  93. .map_voltage = regulator_map_voltage_linear,
  94. .get_voltage_sel = bd9571mwv_avs_get_voltage_sel_regmap,
  95. .list_voltage = regulator_list_voltage_linear,
  96. };
  97. /* Operations permitted on voltage regulators */
  98. static struct regulator_ops reg_ops = {
  99. .set_voltage_sel = bd9571mwv_reg_set_voltage_sel_regmap,
  100. .map_voltage = regulator_map_voltage_linear,
  101. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  102. .list_voltage = regulator_list_voltage_linear,
  103. };
  104. /* Operations permitted on voltage monitors */
  105. static struct regulator_ops vid_ops = {
  106. .map_voltage = regulator_map_voltage_linear,
  107. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  108. .list_voltage = regulator_list_voltage_linear,
  109. };
  110. static struct regulator_desc regulators[] = {
  111. BD9571MWV_REG("VD09", "vd09", VD09, avs_ops, 0, 0x7f,
  112. 0x6f, 600000, 10000, 0x3c),
  113. BD9571MWV_REG("VD18", "vd18", VD18, vid_ops, BD9571MWV_VD18_VID, 0xf,
  114. 16, 1625000, 25000, 0),
  115. BD9571MWV_REG("VD25", "vd25", VD25, vid_ops, BD9571MWV_VD25_VID, 0xf,
  116. 16, 2150000, 50000, 0),
  117. BD9571MWV_REG("VD33", "vd33", VD33, vid_ops, BD9571MWV_VD33_VID, 0xf,
  118. 11, 2800000, 100000, 0),
  119. BD9571MWV_REG("DVFS", "dvfs", DVFS, reg_ops,
  120. BD9571MWV_DVFS_MONIVDAC, 0x7f,
  121. 0x6f, 600000, 10000, 0x3c),
  122. };
  123. #ifdef CONFIG_PM_SLEEP
  124. static int bd9571mwv_bkup_mode_read(struct bd9571mwv *bd, unsigned int *mode)
  125. {
  126. int ret;
  127. ret = regmap_read(bd->regmap, BD9571MWV_BKUP_MODE_CNT, mode);
  128. if (ret) {
  129. dev_err(bd->dev, "failed to read backup mode (%d)\n", ret);
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. static int bd9571mwv_bkup_mode_write(struct bd9571mwv *bd, unsigned int mode)
  135. {
  136. int ret;
  137. ret = regmap_write(bd->regmap, BD9571MWV_BKUP_MODE_CNT, mode);
  138. if (ret) {
  139. dev_err(bd->dev, "failed to configure backup mode 0x%x (%d)\n",
  140. mode, ret);
  141. return ret;
  142. }
  143. return 0;
  144. }
  145. static ssize_t backup_mode_show(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
  149. return sprintf(buf, "%s\n", bdreg->bkup_mode_enabled ? "on" : "off");
  150. }
  151. static ssize_t backup_mode_store(struct device *dev,
  152. struct device_attribute *attr,
  153. const char *buf, size_t count)
  154. {
  155. struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
  156. unsigned int mode;
  157. int ret;
  158. if (!count)
  159. return 0;
  160. ret = kstrtobool(buf, &bdreg->bkup_mode_enabled);
  161. if (ret)
  162. return ret;
  163. if (!bdreg->rstbmode_level)
  164. return count;
  165. /*
  166. * Configure DDR Backup Mode, to change the role of the accessory power
  167. * switch from a power switch to a wake-up switch, or vice versa
  168. */
  169. ret = bd9571mwv_bkup_mode_read(bdreg->bd, &mode);
  170. if (ret)
  171. return ret;
  172. mode &= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK;
  173. if (bdreg->bkup_mode_enabled)
  174. mode |= bdreg->bkup_mode_cnt_keepon;
  175. ret = bd9571mwv_bkup_mode_write(bdreg->bd, mode);
  176. if (ret)
  177. return ret;
  178. return count;
  179. }
  180. static DEVICE_ATTR_RW(backup_mode);
  181. static int bd9571mwv_suspend(struct device *dev)
  182. {
  183. struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
  184. unsigned int mode;
  185. int ret;
  186. if (!bdreg->bkup_mode_enabled)
  187. return 0;
  188. /* Save DDR Backup Mode */
  189. ret = bd9571mwv_bkup_mode_read(bdreg->bd, &mode);
  190. if (ret)
  191. return ret;
  192. bdreg->bkup_mode_cnt_saved = mode;
  193. if (!bdreg->rstbmode_pulse)
  194. return 0;
  195. /* Enable DDR Backup Mode */
  196. mode &= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK;
  197. mode |= bdreg->bkup_mode_cnt_keepon;
  198. if (mode != bdreg->bkup_mode_cnt_saved)
  199. return bd9571mwv_bkup_mode_write(bdreg->bd, mode);
  200. return 0;
  201. }
  202. static int bd9571mwv_resume(struct device *dev)
  203. {
  204. struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
  205. if (!bdreg->bkup_mode_enabled)
  206. return 0;
  207. /* Restore DDR Backup Mode */
  208. return bd9571mwv_bkup_mode_write(bdreg->bd, bdreg->bkup_mode_cnt_saved);
  209. }
  210. static const struct dev_pm_ops bd9571mwv_pm = {
  211. SET_SYSTEM_SLEEP_PM_OPS(bd9571mwv_suspend, bd9571mwv_resume)
  212. };
  213. static int bd9571mwv_regulator_remove(struct platform_device *pdev)
  214. {
  215. device_remove_file(&pdev->dev, &dev_attr_backup_mode);
  216. return 0;
  217. }
  218. #define DEV_PM_OPS &bd9571mwv_pm
  219. #else
  220. #define DEV_PM_OPS NULL
  221. #define bd9571mwv_regulator_remove NULL
  222. #endif /* CONFIG_PM_SLEEP */
  223. static int bd9571mwv_regulator_probe(struct platform_device *pdev)
  224. {
  225. struct bd9571mwv *bd = dev_get_drvdata(pdev->dev.parent);
  226. struct regulator_config config = { };
  227. struct bd9571mwv_reg *bdreg;
  228. struct regulator_dev *rdev;
  229. unsigned int val;
  230. int i;
  231. bdreg = devm_kzalloc(&pdev->dev, sizeof(*bdreg), GFP_KERNEL);
  232. if (!bdreg)
  233. return -ENOMEM;
  234. bdreg->bd = bd;
  235. platform_set_drvdata(pdev, bdreg);
  236. config.dev = &pdev->dev;
  237. config.dev->of_node = bd->dev->of_node;
  238. config.driver_data = bd;
  239. config.regmap = bd->regmap;
  240. for (i = 0; i < ARRAY_SIZE(regulators); i++) {
  241. rdev = devm_regulator_register(&pdev->dev, &regulators[i],
  242. &config);
  243. if (IS_ERR(rdev)) {
  244. dev_err(bd->dev, "failed to register %s regulator\n",
  245. pdev->name);
  246. return PTR_ERR(rdev);
  247. }
  248. }
  249. val = 0;
  250. of_property_read_u32(bd->dev->of_node, "rohm,ddr-backup-power", &val);
  251. if (val & ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK) {
  252. dev_err(bd->dev, "invalid %s mode %u\n",
  253. "rohm,ddr-backup-power", val);
  254. return -EINVAL;
  255. }
  256. bdreg->bkup_mode_cnt_keepon = val;
  257. bdreg->rstbmode_level = of_property_read_bool(bd->dev->of_node,
  258. "rohm,rstbmode-level");
  259. bdreg->rstbmode_pulse = of_property_read_bool(bd->dev->of_node,
  260. "rohm,rstbmode-pulse");
  261. if (bdreg->rstbmode_level && bdreg->rstbmode_pulse) {
  262. dev_err(bd->dev, "only one rohm,rstbmode-* may be specified");
  263. return -EINVAL;
  264. }
  265. #ifdef CONFIG_PM_SLEEP
  266. if (bdreg->bkup_mode_cnt_keepon) {
  267. int ret;
  268. /*
  269. * Backup mode is enabled by default in pulse mode, but needs
  270. * explicit user setup in level mode.
  271. */
  272. bdreg->bkup_mode_enabled = bdreg->rstbmode_pulse;
  273. ret = device_create_file(&pdev->dev, &dev_attr_backup_mode);
  274. if (ret)
  275. return ret;
  276. }
  277. #endif /* CONFIG_PM_SLEEP */
  278. return 0;
  279. }
  280. static const struct platform_device_id bd9571mwv_regulator_id_table[] = {
  281. { "bd9571mwv-regulator", },
  282. { /* sentinel */ }
  283. };
  284. MODULE_DEVICE_TABLE(platform, bd9571mwv_regulator_id_table);
  285. static struct platform_driver bd9571mwv_regulator_driver = {
  286. .driver = {
  287. .name = "bd9571mwv-regulator",
  288. .pm = DEV_PM_OPS,
  289. },
  290. .probe = bd9571mwv_regulator_probe,
  291. .remove = bd9571mwv_regulator_remove,
  292. .id_table = bd9571mwv_regulator_id_table,
  293. };
  294. module_platform_driver(bd9571mwv_regulator_driver);
  295. MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
  296. MODULE_DESCRIPTION("BD9571MWV Regulator driver");
  297. MODULE_LICENSE("GPL v2");