88pm8607.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Regulators driver for Marvell 88PM8607
  3. *
  4. * Copyright (C) 2009 Marvell International Ltd.
  5. * Haojian Zhuang <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/i2c.h>
  15. #include <linux/of.h>
  16. #include <linux/regulator/of_regulator.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/regulator/machine.h>
  20. #include <linux/mfd/88pm860x.h>
  21. #include <linux/module.h>
  22. struct pm8607_regulator_info {
  23. struct regulator_desc desc;
  24. struct pm860x_chip *chip;
  25. struct regulator_dev *regulator;
  26. struct i2c_client *i2c;
  27. struct i2c_client *i2c_8606;
  28. unsigned int *vol_table;
  29. unsigned int *vol_suspend;
  30. int slope_double;
  31. };
  32. static const unsigned int BUCK1_table[] = {
  33. 725000, 750000, 775000, 800000, 825000, 850000, 875000, 900000,
  34. 925000, 950000, 975000, 1000000, 1025000, 1050000, 1075000, 1100000,
  35. 1125000, 1150000, 1175000, 1200000, 1225000, 1250000, 1275000, 1300000,
  36. 1325000, 1350000, 1375000, 1400000, 1425000, 1450000, 1475000, 1500000,
  37. 0, 25000, 50000, 75000, 100000, 125000, 150000, 175000,
  38. 200000, 225000, 250000, 275000, 300000, 325000, 350000, 375000,
  39. 400000, 425000, 450000, 475000, 500000, 525000, 550000, 575000,
  40. 600000, 625000, 650000, 675000, 700000, 725000, 750000, 775000,
  41. };
  42. static const unsigned int BUCK1_suspend_table[] = {
  43. 0, 25000, 50000, 75000, 100000, 125000, 150000, 175000,
  44. 200000, 225000, 250000, 275000, 300000, 325000, 350000, 375000,
  45. 400000, 425000, 450000, 475000, 500000, 525000, 550000, 575000,
  46. 600000, 625000, 650000, 675000, 700000, 725000, 750000, 775000,
  47. 800000, 825000, 850000, 875000, 900000, 925000, 950000, 975000,
  48. 1000000, 1025000, 1050000, 1075000, 1100000, 1125000, 1150000, 1175000,
  49. 1200000, 1225000, 1250000, 1275000, 1300000, 1325000, 1350000, 1375000,
  50. 1400000, 1425000, 1450000, 1475000, 1500000, 1500000, 1500000, 1500000,
  51. };
  52. static const unsigned int BUCK2_table[] = {
  53. 0, 50000, 100000, 150000, 200000, 250000, 300000, 350000,
  54. 400000, 450000, 500000, 550000, 600000, 650000, 700000, 750000,
  55. 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
  56. 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
  57. 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 1950000,
  58. 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000, 2350000,
  59. 2400000, 2450000, 2500000, 2550000, 2600000, 2650000, 2700000, 2750000,
  60. 2800000, 2850000, 2900000, 2950000, 3000000, 3000000, 3000000, 3000000,
  61. };
  62. static const unsigned int BUCK2_suspend_table[] = {
  63. 0, 50000, 100000, 150000, 200000, 250000, 300000, 350000,
  64. 400000, 450000, 500000, 550000, 600000, 650000, 700000, 750000,
  65. 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
  66. 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
  67. 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 1950000,
  68. 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000, 2350000,
  69. 2400000, 2450000, 2500000, 2550000, 2600000, 2650000, 2700000, 2750000,
  70. 2800000, 2850000, 2900000, 2950000, 3000000, 3000000, 3000000, 3000000,
  71. };
  72. static const unsigned int BUCK3_table[] = {
  73. 0, 25000, 50000, 75000, 100000, 125000, 150000, 175000,
  74. 200000, 225000, 250000, 275000, 300000, 325000, 350000, 375000,
  75. 400000, 425000, 450000, 475000, 500000, 525000, 550000, 575000,
  76. 600000, 625000, 650000, 675000, 700000, 725000, 750000, 775000,
  77. 800000, 825000, 850000, 875000, 900000, 925000, 950000, 975000,
  78. 1000000, 1025000, 1050000, 1075000, 1100000, 1125000, 1150000, 1175000,
  79. 1200000, 1225000, 1250000, 1275000, 1300000, 1325000, 1350000, 1375000,
  80. 1400000, 1425000, 1450000, 1475000, 1500000, 1500000, 1500000, 1500000,
  81. };
  82. static const unsigned int BUCK3_suspend_table[] = {
  83. 0, 25000, 50000, 75000, 100000, 125000, 150000, 175000,
  84. 200000, 225000, 250000, 275000, 300000, 325000, 350000, 375000,
  85. 400000, 425000, 450000, 475000, 500000, 525000, 550000, 575000,
  86. 600000, 625000, 650000, 675000, 700000, 725000, 750000, 775000,
  87. 800000, 825000, 850000, 875000, 900000, 925000, 950000, 975000,
  88. 1000000, 1025000, 1050000, 1075000, 1100000, 1125000, 1150000, 1175000,
  89. 1200000, 1225000, 1250000, 1275000, 1300000, 1325000, 1350000, 1375000,
  90. 1400000, 1425000, 1450000, 1475000, 1500000, 1500000, 1500000, 1500000,
  91. };
  92. static const unsigned int LDO1_table[] = {
  93. 1800000, 1200000, 2800000, 0,
  94. };
  95. static const unsigned int LDO1_suspend_table[] = {
  96. 1800000, 1200000, 0, 0,
  97. };
  98. static const unsigned int LDO2_table[] = {
  99. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 3300000,
  100. };
  101. static const unsigned int LDO2_suspend_table[] = {
  102. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 2900000,
  103. };
  104. static const unsigned int LDO3_table[] = {
  105. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 3300000,
  106. };
  107. static const unsigned int LDO3_suspend_table[] = {
  108. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 2900000,
  109. };
  110. static const unsigned int LDO4_table[] = {
  111. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2900000, 3300000,
  112. };
  113. static const unsigned int LDO4_suspend_table[] = {
  114. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2900000, 2900000,
  115. };
  116. static const unsigned int LDO5_table[] = {
  117. 2900000, 3000000, 3100000, 3300000,
  118. };
  119. static const unsigned int LDO5_suspend_table[] = {
  120. 2900000, 0, 0, 0,
  121. };
  122. static const unsigned int LDO6_table[] = {
  123. 1800000, 1850000, 2600000, 2650000, 2700000, 2750000, 2800000, 3300000,
  124. };
  125. static const unsigned int LDO6_suspend_table[] = {
  126. 1800000, 1850000, 2600000, 2650000, 2700000, 2750000, 2800000, 2900000,
  127. };
  128. static const unsigned int LDO7_table[] = {
  129. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 2900000,
  130. };
  131. static const unsigned int LDO7_suspend_table[] = {
  132. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 2900000,
  133. };
  134. static const unsigned int LDO8_table[] = {
  135. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 2900000,
  136. };
  137. static const unsigned int LDO8_suspend_table[] = {
  138. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 2900000,
  139. };
  140. static const unsigned int LDO9_table[] = {
  141. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 3300000,
  142. };
  143. static const unsigned int LDO9_suspend_table[] = {
  144. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 2900000,
  145. };
  146. static const unsigned int LDO10_table[] = {
  147. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 3300000,
  148. 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000,
  149. };
  150. static const unsigned int LDO10_suspend_table[] = {
  151. 1800000, 1850000, 1900000, 2700000, 2750000, 2800000, 2850000, 2900000,
  152. 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000,
  153. };
  154. static const unsigned int LDO12_table[] = {
  155. 1800000, 1900000, 2700000, 2800000, 2900000, 3000000, 3100000, 3300000,
  156. 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000,
  157. };
  158. static const unsigned int LDO12_suspend_table[] = {
  159. 1800000, 1900000, 2700000, 2800000, 2900000, 2900000, 2900000, 2900000,
  160. 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000,
  161. };
  162. static const unsigned int LDO13_table[] = {
  163. 1200000, 1300000, 1800000, 2000000, 2500000, 2800000, 3000000, 0,
  164. };
  165. static const unsigned int LDO13_suspend_table[] = {
  166. 0,
  167. };
  168. static const unsigned int LDO14_table[] = {
  169. 1800000, 1850000, 2700000, 2750000, 2800000, 2850000, 2900000, 3300000,
  170. };
  171. static const unsigned int LDO14_suspend_table[] = {
  172. 1800000, 1850000, 2700000, 2750000, 2800000, 2850000, 2900000, 2900000,
  173. };
  174. static int pm8607_list_voltage(struct regulator_dev *rdev, unsigned index)
  175. {
  176. struct pm8607_regulator_info *info = rdev_get_drvdata(rdev);
  177. int ret = -EINVAL;
  178. if (info->vol_table && (index < rdev->desc->n_voltages)) {
  179. ret = info->vol_table[index];
  180. if (info->slope_double)
  181. ret <<= 1;
  182. }
  183. return ret;
  184. }
  185. static struct regulator_ops pm8607_regulator_ops = {
  186. .list_voltage = pm8607_list_voltage,
  187. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  188. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  189. .enable = regulator_enable_regmap,
  190. .disable = regulator_disable_regmap,
  191. .is_enabled = regulator_is_enabled_regmap,
  192. };
  193. static struct regulator_ops pm8606_preg_ops = {
  194. .enable = regulator_enable_regmap,
  195. .disable = regulator_disable_regmap,
  196. .is_enabled = regulator_is_enabled_regmap,
  197. };
  198. #define PM8606_PREG(ereg, ebit) \
  199. { \
  200. .desc = { \
  201. .name = "PREG", \
  202. .ops = &pm8606_preg_ops, \
  203. .type = REGULATOR_CURRENT, \
  204. .id = PM8606_ID_PREG, \
  205. .owner = THIS_MODULE, \
  206. .enable_reg = PM8606_##ereg, \
  207. .enable_mask = (ebit), \
  208. .enable_is_inverted = true, \
  209. }, \
  210. }
  211. #define PM8607_DVC(vreg, ureg, ubit, ereg, ebit) \
  212. { \
  213. .desc = { \
  214. .name = #vreg, \
  215. .ops = &pm8607_regulator_ops, \
  216. .type = REGULATOR_VOLTAGE, \
  217. .id = PM8607_ID_##vreg, \
  218. .owner = THIS_MODULE, \
  219. .n_voltages = ARRAY_SIZE(vreg##_table), \
  220. .vsel_reg = PM8607_##vreg, \
  221. .vsel_mask = ARRAY_SIZE(vreg##_table) - 1, \
  222. .apply_reg = PM8607_##ureg, \
  223. .apply_bit = (ubit), \
  224. .enable_reg = PM8607_##ereg, \
  225. .enable_mask = 1 << (ebit), \
  226. }, \
  227. .slope_double = (0), \
  228. .vol_table = (unsigned int *)&vreg##_table, \
  229. .vol_suspend = (unsigned int *)&vreg##_suspend_table, \
  230. }
  231. #define PM8607_LDO(_id, vreg, shift, ereg, ebit) \
  232. { \
  233. .desc = { \
  234. .name = "LDO" #_id, \
  235. .ops = &pm8607_regulator_ops, \
  236. .type = REGULATOR_VOLTAGE, \
  237. .id = PM8607_ID_LDO##_id, \
  238. .owner = THIS_MODULE, \
  239. .n_voltages = ARRAY_SIZE(LDO##_id##_table), \
  240. .vsel_reg = PM8607_##vreg, \
  241. .vsel_mask = (ARRAY_SIZE(LDO##_id##_table) - 1) << (shift), \
  242. .enable_reg = PM8607_##ereg, \
  243. .enable_mask = 1 << (ebit), \
  244. }, \
  245. .slope_double = (0), \
  246. .vol_table = (unsigned int *)&LDO##_id##_table, \
  247. .vol_suspend = (unsigned int *)&LDO##_id##_suspend_table, \
  248. }
  249. static struct pm8607_regulator_info pm8607_regulator_info[] = {
  250. PM8607_DVC(BUCK1, GO, BIT(0), SUPPLIES_EN11, 0),
  251. PM8607_DVC(BUCK2, GO, BIT(1), SUPPLIES_EN11, 1),
  252. PM8607_DVC(BUCK3, GO, BIT(2), SUPPLIES_EN11, 2),
  253. PM8607_LDO(1, LDO1, 0, SUPPLIES_EN11, 3),
  254. PM8607_LDO(2, LDO2, 0, SUPPLIES_EN11, 4),
  255. PM8607_LDO(3, LDO3, 0, SUPPLIES_EN11, 5),
  256. PM8607_LDO(4, LDO4, 0, SUPPLIES_EN11, 6),
  257. PM8607_LDO(5, LDO5, 0, SUPPLIES_EN11, 7),
  258. PM8607_LDO(6, LDO6, 0, SUPPLIES_EN12, 0),
  259. PM8607_LDO(7, LDO7, 0, SUPPLIES_EN12, 1),
  260. PM8607_LDO(8, LDO8, 0, SUPPLIES_EN12, 2),
  261. PM8607_LDO(9, LDO9, 0, SUPPLIES_EN12, 3),
  262. PM8607_LDO(10, LDO10, 0, SUPPLIES_EN12, 4),
  263. PM8607_LDO(12, LDO12, 0, SUPPLIES_EN12, 5),
  264. PM8607_LDO(13, VIBRATOR_SET, 1, VIBRATOR_SET, 0),
  265. PM8607_LDO(14, LDO14, 0, SUPPLIES_EN12, 6),
  266. };
  267. static struct pm8607_regulator_info pm8606_regulator_info[] = {
  268. PM8606_PREG(PREREGULATORB, 5),
  269. };
  270. #ifdef CONFIG_OF
  271. static int pm8607_regulator_dt_init(struct platform_device *pdev,
  272. struct pm8607_regulator_info *info,
  273. struct regulator_config *config)
  274. {
  275. struct device_node *nproot, *np;
  276. nproot = pdev->dev.parent->of_node;
  277. if (!nproot)
  278. return -ENODEV;
  279. nproot = of_get_child_by_name(nproot, "regulators");
  280. if (!nproot) {
  281. dev_err(&pdev->dev, "failed to find regulators node\n");
  282. return -ENODEV;
  283. }
  284. for_each_child_of_node(nproot, np) {
  285. if (!of_node_cmp(np->name, info->desc.name)) {
  286. config->init_data =
  287. of_get_regulator_init_data(&pdev->dev, np,
  288. &info->desc);
  289. config->of_node = np;
  290. break;
  291. }
  292. }
  293. of_node_put(nproot);
  294. return 0;
  295. }
  296. #else
  297. #define pm8607_regulator_dt_init(x, y, z) (-1)
  298. #endif
  299. static int pm8607_regulator_probe(struct platform_device *pdev)
  300. {
  301. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  302. struct pm8607_regulator_info *info = NULL;
  303. struct regulator_init_data *pdata = dev_get_platdata(&pdev->dev);
  304. struct regulator_config config = { };
  305. struct resource *res;
  306. int i;
  307. res = platform_get_resource(pdev, IORESOURCE_REG, 0);
  308. if (res) {
  309. /* There're resources in 88PM8607 regulator driver */
  310. for (i = 0; i < ARRAY_SIZE(pm8607_regulator_info); i++) {
  311. info = &pm8607_regulator_info[i];
  312. if (info->desc.vsel_reg == res->start)
  313. break;
  314. }
  315. if (i == ARRAY_SIZE(pm8607_regulator_info)) {
  316. dev_err(&pdev->dev, "Failed to find regulator %llu\n",
  317. (unsigned long long)res->start);
  318. return -EINVAL;
  319. }
  320. } else {
  321. /* There's no resource in 88PM8606 PREG regulator driver */
  322. info = &pm8606_regulator_info[0];
  323. /* i is used to check regulator ID */
  324. i = -1;
  325. }
  326. info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  327. info->i2c_8606 = (chip->id == CHIP_PM8607) ? chip->companion :
  328. chip->client;
  329. info->chip = chip;
  330. /* check DVC ramp slope double */
  331. if ((i == PM8607_ID_BUCK3) && info->chip->buck3_double)
  332. info->slope_double = 1;
  333. config.dev = &pdev->dev;
  334. config.driver_data = info;
  335. if (pm8607_regulator_dt_init(pdev, info, &config))
  336. if (pdata)
  337. config.init_data = pdata;
  338. if (chip->id == CHIP_PM8607)
  339. config.regmap = chip->regmap;
  340. else
  341. config.regmap = chip->regmap_companion;
  342. info->regulator = devm_regulator_register(&pdev->dev, &info->desc,
  343. &config);
  344. if (IS_ERR(info->regulator)) {
  345. dev_err(&pdev->dev, "failed to register regulator %s\n",
  346. info->desc.name);
  347. return PTR_ERR(info->regulator);
  348. }
  349. platform_set_drvdata(pdev, info);
  350. return 0;
  351. }
  352. static const struct platform_device_id pm8607_regulator_driver_ids[] = {
  353. {
  354. .name = "88pm860x-regulator",
  355. .driver_data = 0,
  356. }, {
  357. .name = "88pm860x-preg",
  358. .driver_data = 0,
  359. },
  360. { },
  361. };
  362. MODULE_DEVICE_TABLE(platform, pm8607_regulator_driver_ids);
  363. static struct platform_driver pm8607_regulator_driver = {
  364. .driver = {
  365. .name = "88pm860x-regulator",
  366. },
  367. .probe = pm8607_regulator_probe,
  368. .id_table = pm8607_regulator_driver_ids,
  369. };
  370. static int __init pm8607_regulator_init(void)
  371. {
  372. return platform_driver_register(&pm8607_regulator_driver);
  373. }
  374. subsys_initcall(pm8607_regulator_init);
  375. static void __exit pm8607_regulator_exit(void)
  376. {
  377. platform_driver_unregister(&pm8607_regulator_driver);
  378. }
  379. module_exit(pm8607_regulator_exit);
  380. MODULE_LICENSE("GPL");
  381. MODULE_AUTHOR("Haojian Zhuang <[email protected]>");
  382. MODULE_DESCRIPTION("Regulator Driver for Marvell 88PM8607 PMIC");
  383. MODULE_ALIAS("platform:88pm8607-regulator");