88pm8607.c 14 KB

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