88pm8607.c 15 KB

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