88pm800.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Regulators driver for Marvell 88PM800
  3. *
  4. * Copyright (C) 2012 Marvell International Ltd.
  5. * Joseph(Yossi) Hanin <yhanin@marvell.com>
  6. * Yi Zhang <yizhang@marvell.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/driver.h>
  18. #include <linux/regulator/machine.h>
  19. #include <linux/mfd/88pm80x.h>
  20. #include <linux/delay.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #include <linux/regulator/of_regulator.h>
  24. /* LDO1 with DVC[0..3] */
  25. #define PM800_LDO1_VOUT (0x08) /* VOUT1 */
  26. #define PM800_LDO1_VOUT_2 (0x09)
  27. #define PM800_LDO1_VOUT_3 (0x0A)
  28. #define PM800_LDO2_VOUT (0x0B)
  29. #define PM800_LDO3_VOUT (0x0C)
  30. #define PM800_LDO4_VOUT (0x0D)
  31. #define PM800_LDO5_VOUT (0x0E)
  32. #define PM800_LDO6_VOUT (0x0F)
  33. #define PM800_LDO7_VOUT (0x10)
  34. #define PM800_LDO8_VOUT (0x11)
  35. #define PM800_LDO9_VOUT (0x12)
  36. #define PM800_LDO10_VOUT (0x13)
  37. #define PM800_LDO11_VOUT (0x14)
  38. #define PM800_LDO12_VOUT (0x15)
  39. #define PM800_LDO13_VOUT (0x16)
  40. #define PM800_LDO14_VOUT (0x17)
  41. #define PM800_LDO15_VOUT (0x18)
  42. #define PM800_LDO16_VOUT (0x19)
  43. #define PM800_LDO17_VOUT (0x1A)
  44. #define PM800_LDO18_VOUT (0x1B)
  45. #define PM800_LDO19_VOUT (0x1C)
  46. /* BUCK1 with DVC[0..3] */
  47. #define PM800_BUCK1 (0x3C)
  48. #define PM800_BUCK1_1 (0x3D)
  49. #define PM800_BUCK1_2 (0x3E)
  50. #define PM800_BUCK1_3 (0x3F)
  51. #define PM800_BUCK2 (0x40)
  52. #define PM800_BUCK3 (0x41)
  53. #define PM800_BUCK4 (0x42)
  54. #define PM800_BUCK4_1 (0x43)
  55. #define PM800_BUCK4_2 (0x44)
  56. #define PM800_BUCK4_3 (0x45)
  57. #define PM800_BUCK5 (0x46)
  58. #define PM800_BUCK_ENA (0x50)
  59. #define PM800_LDO_ENA1_1 (0x51)
  60. #define PM800_LDO_ENA1_2 (0x52)
  61. #define PM800_LDO_ENA1_3 (0x53)
  62. #define PM800_LDO_ENA2_1 (0x56)
  63. #define PM800_LDO_ENA2_2 (0x57)
  64. #define PM800_LDO_ENA2_3 (0x58)
  65. #define PM800_BUCK1_MISC1 (0x78)
  66. #define PM800_BUCK3_MISC1 (0x7E)
  67. #define PM800_BUCK4_MISC1 (0x81)
  68. #define PM800_BUCK5_MISC1 (0x84)
  69. struct pm800_regulator_info {
  70. struct regulator_desc desc;
  71. int max_ua;
  72. };
  73. struct pm800_regulators {
  74. struct regulator_dev *regulators[PM800_ID_RG_MAX];
  75. struct pm80x_chip *chip;
  76. struct regmap *map;
  77. };
  78. /*
  79. * vreg - the buck regs string.
  80. * ereg - the string for the enable register.
  81. * ebit - the bit number in the enable register.
  82. * amax - the current
  83. * Buck has 2 kinds of voltage steps. It is easy to find voltage by ranges,
  84. * not the constant voltage table.
  85. * n_volt - Number of available selectors
  86. */
  87. #define PM800_BUCK(vreg, ereg, ebit, amax, volt_ranges, n_volt) \
  88. { \
  89. .desc = { \
  90. .name = #vreg, \
  91. .ops = &pm800_volt_range_ops, \
  92. .type = REGULATOR_VOLTAGE, \
  93. .id = PM800_ID_##vreg, \
  94. .owner = THIS_MODULE, \
  95. .n_voltages = n_volt, \
  96. .linear_ranges = volt_ranges, \
  97. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  98. .vsel_reg = PM800_##vreg, \
  99. .vsel_mask = 0x7f, \
  100. .enable_reg = PM800_##ereg, \
  101. .enable_mask = 1 << (ebit), \
  102. }, \
  103. .max_ua = (amax), \
  104. }
  105. /*
  106. * vreg - the LDO regs string
  107. * ereg - the string for the enable register.
  108. * ebit - the bit number in the enable register.
  109. * amax - the current
  110. * volt_table - the LDO voltage table
  111. * For all the LDOes, there are too many ranges. Using volt_table will be
  112. * simpler and faster.
  113. */
  114. #define PM800_LDO(vreg, ereg, ebit, amax, ldo_volt_table) \
  115. { \
  116. .desc = { \
  117. .name = #vreg, \
  118. .ops = &pm800_volt_table_ops, \
  119. .type = REGULATOR_VOLTAGE, \
  120. .id = PM800_ID_##vreg, \
  121. .owner = THIS_MODULE, \
  122. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  123. .vsel_reg = PM800_##vreg##_VOUT, \
  124. .vsel_mask = 0x1f, \
  125. .enable_reg = PM800_##ereg, \
  126. .enable_mask = 1 << (ebit), \
  127. .volt_table = ldo_volt_table, \
  128. }, \
  129. .max_ua = (amax), \
  130. }
  131. /* Ranges are sorted in ascending order. */
  132. static const struct regulator_linear_range buck1_volt_range[] = {
  133. REGULATOR_LINEAR_RANGE(600000, 0, 0x4f, 12500),
  134. REGULATOR_LINEAR_RANGE(1600000, 0x50, 0x54, 50000),
  135. };
  136. /* BUCK 2~5 have same ranges. */
  137. static const struct regulator_linear_range buck2_5_volt_range[] = {
  138. REGULATOR_LINEAR_RANGE(600000, 0, 0x4f, 12500),
  139. REGULATOR_LINEAR_RANGE(1600000, 0x50, 0x72, 50000),
  140. };
  141. static const unsigned int ldo1_volt_table[] = {
  142. 600000, 650000, 700000, 750000, 800000, 850000, 900000, 950000,
  143. 1000000, 1050000, 1100000, 1150000, 1200000, 1300000, 1400000, 1500000,
  144. };
  145. static const unsigned int ldo2_volt_table[] = {
  146. 1700000, 1800000, 1900000, 2000000, 2100000, 2500000, 2700000, 2800000,
  147. };
  148. /* LDO 3~17 have same voltage table. */
  149. static const unsigned int ldo3_17_volt_table[] = {
  150. 1200000, 1250000, 1700000, 1800000, 1850000, 1900000, 2500000, 2600000,
  151. 2700000, 2750000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
  152. };
  153. /* LDO 18~19 have same voltage table. */
  154. static const unsigned int ldo18_19_volt_table[] = {
  155. 1700000, 1800000, 1900000, 2500000, 2800000, 2900000, 3100000, 3300000,
  156. };
  157. static int pm800_get_current_limit(struct regulator_dev *rdev)
  158. {
  159. struct pm800_regulator_info *info = rdev_get_drvdata(rdev);
  160. return info->max_ua;
  161. }
  162. static struct regulator_ops pm800_volt_range_ops = {
  163. .list_voltage = regulator_list_voltage_linear_range,
  164. .map_voltage = regulator_map_voltage_linear_range,
  165. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  166. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  167. .enable = regulator_enable_regmap,
  168. .disable = regulator_disable_regmap,
  169. .is_enabled = regulator_is_enabled_regmap,
  170. .get_current_limit = pm800_get_current_limit,
  171. };
  172. static struct regulator_ops pm800_volt_table_ops = {
  173. .list_voltage = regulator_list_voltage_table,
  174. .map_voltage = regulator_map_voltage_iterate,
  175. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  176. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  177. .enable = regulator_enable_regmap,
  178. .disable = regulator_disable_regmap,
  179. .is_enabled = regulator_is_enabled_regmap,
  180. .get_current_limit = pm800_get_current_limit,
  181. };
  182. /* The array is indexed by id(PM800_ID_XXX) */
  183. static struct pm800_regulator_info pm800_regulator_info[] = {
  184. PM800_BUCK(BUCK1, BUCK_ENA, 0, 3000000, buck1_volt_range, 0x55),
  185. PM800_BUCK(BUCK2, BUCK_ENA, 1, 1200000, buck2_5_volt_range, 0x73),
  186. PM800_BUCK(BUCK3, BUCK_ENA, 2, 1200000, buck2_5_volt_range, 0x73),
  187. PM800_BUCK(BUCK4, BUCK_ENA, 3, 1200000, buck2_5_volt_range, 0x73),
  188. PM800_BUCK(BUCK5, BUCK_ENA, 4, 1200000, buck2_5_volt_range, 0x73),
  189. PM800_LDO(LDO1, LDO_ENA1_1, 0, 200000, ldo1_volt_table),
  190. PM800_LDO(LDO2, LDO_ENA1_1, 1, 10000, ldo2_volt_table),
  191. PM800_LDO(LDO3, LDO_ENA1_1, 2, 300000, ldo3_17_volt_table),
  192. PM800_LDO(LDO4, LDO_ENA1_1, 3, 300000, ldo3_17_volt_table),
  193. PM800_LDO(LDO5, LDO_ENA1_1, 4, 300000, ldo3_17_volt_table),
  194. PM800_LDO(LDO6, LDO_ENA1_1, 5, 300000, ldo3_17_volt_table),
  195. PM800_LDO(LDO7, LDO_ENA1_1, 6, 300000, ldo3_17_volt_table),
  196. PM800_LDO(LDO8, LDO_ENA1_1, 7, 300000, ldo3_17_volt_table),
  197. PM800_LDO(LDO9, LDO_ENA1_2, 0, 300000, ldo3_17_volt_table),
  198. PM800_LDO(LDO10, LDO_ENA1_2, 1, 300000, ldo3_17_volt_table),
  199. PM800_LDO(LDO11, LDO_ENA1_2, 2, 300000, ldo3_17_volt_table),
  200. PM800_LDO(LDO12, LDO_ENA1_2, 3, 300000, ldo3_17_volt_table),
  201. PM800_LDO(LDO13, LDO_ENA1_2, 4, 300000, ldo3_17_volt_table),
  202. PM800_LDO(LDO14, LDO_ENA1_2, 5, 300000, ldo3_17_volt_table),
  203. PM800_LDO(LDO15, LDO_ENA1_2, 6, 300000, ldo3_17_volt_table),
  204. PM800_LDO(LDO16, LDO_ENA1_2, 7, 300000, ldo3_17_volt_table),
  205. PM800_LDO(LDO17, LDO_ENA1_3, 0, 300000, ldo3_17_volt_table),
  206. PM800_LDO(LDO18, LDO_ENA1_3, 1, 200000, ldo18_19_volt_table),
  207. PM800_LDO(LDO19, LDO_ENA1_3, 2, 200000, ldo18_19_volt_table),
  208. };
  209. #define PM800_REGULATOR_OF_MATCH(_name, _id) \
  210. [PM800_ID_##_id] = { \
  211. .name = #_name, \
  212. .driver_data = &pm800_regulator_info[PM800_ID_##_id], \
  213. }
  214. static struct of_regulator_match pm800_regulator_matches[] = {
  215. PM800_REGULATOR_OF_MATCH(buck1, BUCK1),
  216. PM800_REGULATOR_OF_MATCH(buck2, BUCK2),
  217. PM800_REGULATOR_OF_MATCH(buck3, BUCK3),
  218. PM800_REGULATOR_OF_MATCH(buck4, BUCK4),
  219. PM800_REGULATOR_OF_MATCH(buck5, BUCK5),
  220. PM800_REGULATOR_OF_MATCH(ldo1, LDO1),
  221. PM800_REGULATOR_OF_MATCH(ldo2, LDO2),
  222. PM800_REGULATOR_OF_MATCH(ldo3, LDO3),
  223. PM800_REGULATOR_OF_MATCH(ldo4, LDO4),
  224. PM800_REGULATOR_OF_MATCH(ldo5, LDO5),
  225. PM800_REGULATOR_OF_MATCH(ldo6, LDO6),
  226. PM800_REGULATOR_OF_MATCH(ldo7, LDO7),
  227. PM800_REGULATOR_OF_MATCH(ldo8, LDO8),
  228. PM800_REGULATOR_OF_MATCH(ldo9, LDO9),
  229. PM800_REGULATOR_OF_MATCH(ldo10, LDO10),
  230. PM800_REGULATOR_OF_MATCH(ldo11, LDO11),
  231. PM800_REGULATOR_OF_MATCH(ldo12, LDO12),
  232. PM800_REGULATOR_OF_MATCH(ldo13, LDO13),
  233. PM800_REGULATOR_OF_MATCH(ldo14, LDO14),
  234. PM800_REGULATOR_OF_MATCH(ldo15, LDO15),
  235. PM800_REGULATOR_OF_MATCH(ldo16, LDO16),
  236. PM800_REGULATOR_OF_MATCH(ldo17, LDO17),
  237. PM800_REGULATOR_OF_MATCH(ldo18, LDO18),
  238. PM800_REGULATOR_OF_MATCH(ldo19, LDO19),
  239. };
  240. static int pm800_regulator_dt_init(struct platform_device *pdev)
  241. {
  242. struct device_node *np = pdev->dev.of_node;
  243. int ret;
  244. ret = of_regulator_match(&pdev->dev, np,
  245. pm800_regulator_matches,
  246. ARRAY_SIZE(pm800_regulator_matches));
  247. if (ret < 0)
  248. return ret;
  249. return 0;
  250. }
  251. static int pm800_regulator_probe(struct platform_device *pdev)
  252. {
  253. struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  254. struct pm80x_platform_data *pdata = dev_get_platdata(pdev->dev.parent);
  255. struct pm800_regulators *pm800_data;
  256. struct pm800_regulator_info *info;
  257. struct regulator_config config = { };
  258. struct regulator_init_data *init_data;
  259. int i, ret;
  260. if (!pdata || pdata->num_regulators == 0) {
  261. if (IS_ENABLED(CONFIG_OF)) {
  262. ret = pm800_regulator_dt_init(pdev);
  263. if (ret)
  264. return ret;
  265. } else {
  266. return -ENODEV;
  267. }
  268. } else if (pdata->num_regulators) {
  269. unsigned int count = 0;
  270. /* Check whether num_regulator is valid. */
  271. for (i = 0; i < ARRAY_SIZE(pdata->regulators); i++) {
  272. if (pdata->regulators[i])
  273. count++;
  274. }
  275. if (count != pdata->num_regulators)
  276. return -EINVAL;
  277. } else {
  278. return -EINVAL;
  279. }
  280. pm800_data = devm_kzalloc(&pdev->dev, sizeof(*pm800_data),
  281. GFP_KERNEL);
  282. if (!pm800_data)
  283. return -ENOMEM;
  284. pm800_data->map = chip->subchip->regmap_power;
  285. pm800_data->chip = chip;
  286. platform_set_drvdata(pdev, pm800_data);
  287. for (i = 0; i < PM800_ID_RG_MAX; i++) {
  288. if (!pdata || pdata->num_regulators == 0)
  289. init_data = pm800_regulator_matches[i].init_data;
  290. else
  291. init_data = pdata->regulators[i];
  292. if (!init_data)
  293. continue;
  294. info = pm800_regulator_matches[i].driver_data;
  295. config.dev = &pdev->dev;
  296. config.init_data = init_data;
  297. config.driver_data = info;
  298. config.regmap = pm800_data->map;
  299. config.of_node = pm800_regulator_matches[i].of_node;
  300. pm800_data->regulators[i] =
  301. regulator_register(&info->desc, &config);
  302. if (IS_ERR(pm800_data->regulators[i])) {
  303. ret = PTR_ERR(pm800_data->regulators[i]);
  304. dev_err(&pdev->dev, "Failed to register %s\n",
  305. info->desc.name);
  306. while (--i >= 0)
  307. regulator_unregister(pm800_data->regulators[i]);
  308. return ret;
  309. }
  310. }
  311. return 0;
  312. }
  313. static int pm800_regulator_remove(struct platform_device *pdev)
  314. {
  315. struct pm800_regulators *pm800_data = platform_get_drvdata(pdev);
  316. int i;
  317. for (i = 0; i < PM800_ID_RG_MAX; i++)
  318. regulator_unregister(pm800_data->regulators[i]);
  319. return 0;
  320. }
  321. static struct platform_driver pm800_regulator_driver = {
  322. .driver = {
  323. .name = "88pm80x-regulator",
  324. },
  325. .probe = pm800_regulator_probe,
  326. .remove = pm800_regulator_remove,
  327. };
  328. module_platform_driver(pm800_regulator_driver);
  329. MODULE_LICENSE("GPL");
  330. MODULE_AUTHOR("Joseph(Yossi) Hanin <yhanin@marvell.com>");
  331. MODULE_DESCRIPTION("Regulator Driver for Marvell 88PM800 PMIC");
  332. MODULE_ALIAS("platform:88pm800-regulator");