88pm8607.c 14 KB

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