bd9571mwv-regulator.c 8.7 KB

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