88pm8607.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. struct pm8607_regulator_info {
  20. struct regulator_desc desc;
  21. struct pm860x_chip *chip;
  22. struct regulator_dev *regulator;
  23. struct i2c_client *i2c;
  24. unsigned int *vol_table;
  25. unsigned int *vol_suspend;
  26. int vol_reg;
  27. int vol_shift;
  28. int vol_nbits;
  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 < (1 << info->vol_nbits))) {
  182. ret = info->vol_table[index];
  183. if (info->slope_double)
  184. ret <<= 1;
  185. }
  186. return ret;
  187. }
  188. static int choose_voltage(struct regulator_dev *rdev, int min_uV, int max_uV)
  189. {
  190. struct pm8607_regulator_info *info = rdev_get_drvdata(rdev);
  191. int i, ret = -ENOENT;
  192. if (info->slope_double) {
  193. min_uV = min_uV >> 1;
  194. max_uV = max_uV >> 1;
  195. }
  196. if (info->vol_table) {
  197. for (i = 0; i < (1 << info->vol_nbits); i++) {
  198. if (!info->vol_table[i])
  199. break;
  200. if ((min_uV <= info->vol_table[i])
  201. && (max_uV >= info->vol_table[i])) {
  202. ret = i;
  203. break;
  204. }
  205. }
  206. }
  207. if (ret < 0)
  208. pr_err("invalid voltage range (%d %d) uV\n", min_uV, max_uV);
  209. return ret;
  210. }
  211. static int pm8607_set_voltage(struct regulator_dev *rdev,
  212. int min_uV, int max_uV, unsigned *selector)
  213. {
  214. struct pm8607_regulator_info *info = rdev_get_drvdata(rdev);
  215. uint8_t val, mask;
  216. int ret;
  217. if (min_uV > max_uV) {
  218. pr_err("invalid voltage range (%d, %d) uV\n", min_uV, max_uV);
  219. return -EINVAL;
  220. }
  221. ret = choose_voltage(rdev, min_uV, max_uV);
  222. if (ret < 0)
  223. return -EINVAL;
  224. *selector = ret;
  225. val = (uint8_t)(ret << info->vol_shift);
  226. mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
  227. ret = pm860x_set_bits(info->i2c, info->vol_reg, mask, val);
  228. if (ret)
  229. return ret;
  230. switch (info->desc.id) {
  231. case PM8607_ID_BUCK1:
  232. case PM8607_ID_BUCK3:
  233. ret = pm860x_set_bits(info->i2c, info->update_reg,
  234. 1 << info->update_bit,
  235. 1 << info->update_bit);
  236. break;
  237. }
  238. return ret;
  239. }
  240. static int pm8607_get_voltage(struct regulator_dev *rdev)
  241. {
  242. struct pm8607_regulator_info *info = rdev_get_drvdata(rdev);
  243. uint8_t val, mask;
  244. int ret;
  245. ret = pm860x_reg_read(info->i2c, info->vol_reg);
  246. if (ret < 0)
  247. return ret;
  248. mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
  249. val = ((unsigned char)ret & mask) >> info->vol_shift;
  250. return pm8607_list_voltage(rdev, val);
  251. }
  252. static int pm8607_enable(struct regulator_dev *rdev)
  253. {
  254. struct pm8607_regulator_info *info = rdev_get_drvdata(rdev);
  255. return pm860x_set_bits(info->i2c, info->enable_reg,
  256. 1 << info->enable_bit,
  257. 1 << info->enable_bit);
  258. }
  259. static int pm8607_disable(struct regulator_dev *rdev)
  260. {
  261. struct pm8607_regulator_info *info = rdev_get_drvdata(rdev);
  262. return pm860x_set_bits(info->i2c, info->enable_reg,
  263. 1 << info->enable_bit, 0);
  264. }
  265. static int pm8607_is_enabled(struct regulator_dev *rdev)
  266. {
  267. struct pm8607_regulator_info *info = rdev_get_drvdata(rdev);
  268. int ret;
  269. ret = pm860x_reg_read(info->i2c, info->enable_reg);
  270. if (ret < 0)
  271. return ret;
  272. return !!((unsigned char)ret & (1 << info->enable_bit));
  273. }
  274. static struct regulator_ops pm8607_regulator_ops = {
  275. .set_voltage = pm8607_set_voltage,
  276. .get_voltage = pm8607_get_voltage,
  277. .enable = pm8607_enable,
  278. .disable = pm8607_disable,
  279. .is_enabled = pm8607_is_enabled,
  280. };
  281. #define PM8607_DVC(vreg, nbits, ureg, ubit, ereg, ebit) \
  282. { \
  283. .desc = { \
  284. .name = #vreg, \
  285. .ops = &pm8607_regulator_ops, \
  286. .type = REGULATOR_VOLTAGE, \
  287. .id = PM8607_ID_##vreg, \
  288. .owner = THIS_MODULE, \
  289. }, \
  290. .vol_reg = PM8607_##vreg, \
  291. .vol_shift = (0), \
  292. .vol_nbits = (nbits), \
  293. .update_reg = PM8607_##ureg, \
  294. .update_bit = (ubit), \
  295. .enable_reg = PM8607_##ereg, \
  296. .enable_bit = (ebit), \
  297. .slope_double = (0), \
  298. .vol_table = (unsigned int *)&vreg##_table, \
  299. .vol_suspend = (unsigned int *)&vreg##_suspend_table, \
  300. }
  301. #define PM8607_LDO(_id, vreg, shift, nbits, ereg, ebit) \
  302. { \
  303. .desc = { \
  304. .name = "LDO" #_id, \
  305. .ops = &pm8607_regulator_ops, \
  306. .type = REGULATOR_VOLTAGE, \
  307. .id = PM8607_ID_LDO##_id, \
  308. .owner = THIS_MODULE, \
  309. }, \
  310. .vol_reg = PM8607_##vreg, \
  311. .vol_shift = (shift), \
  312. .vol_nbits = (nbits), \
  313. .enable_reg = PM8607_##ereg, \
  314. .enable_bit = (ebit), \
  315. .slope_double = (0), \
  316. .vol_table = (unsigned int *)&LDO##_id##_table, \
  317. .vol_suspend = (unsigned int *)&LDO##_id##_suspend_table, \
  318. }
  319. static struct pm8607_regulator_info pm8607_regulator_info[] = {
  320. PM8607_DVC(BUCK1, 6, GO, 0, SUPPLIES_EN11, 0),
  321. PM8607_DVC(BUCK2, 6, GO, 1, SUPPLIES_EN11, 1),
  322. PM8607_DVC(BUCK3, 6, GO, 2, SUPPLIES_EN11, 2),
  323. PM8607_LDO( 1, LDO1, 0, 2, SUPPLIES_EN11, 3),
  324. PM8607_LDO( 2, LDO2, 0, 3, SUPPLIES_EN11, 4),
  325. PM8607_LDO( 3, LDO3, 0, 3, SUPPLIES_EN11, 5),
  326. PM8607_LDO( 4, LDO4, 0, 3, SUPPLIES_EN11, 6),
  327. PM8607_LDO( 5, LDO5, 0, 2, SUPPLIES_EN11, 7),
  328. PM8607_LDO( 6, LDO6, 0, 3, SUPPLIES_EN12, 0),
  329. PM8607_LDO( 7, LDO7, 0, 3, SUPPLIES_EN12, 1),
  330. PM8607_LDO( 8, LDO8, 0, 3, SUPPLIES_EN12, 2),
  331. PM8607_LDO( 9, LDO9, 0, 3, SUPPLIES_EN12, 3),
  332. PM8607_LDO(10, LDO10, 0, 4, SUPPLIES_EN12, 4),
  333. PM8607_LDO(12, LDO12, 0, 4, SUPPLIES_EN12, 5),
  334. PM8607_LDO(13, VIBRATOR_SET, 1, 3, VIBRATOR_SET, 0),
  335. PM8607_LDO(14, LDO14, 0, 3, SUPPLIES_EN12, 6),
  336. };
  337. static int __devinit pm8607_regulator_probe(struct platform_device *pdev)
  338. {
  339. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  340. struct pm8607_regulator_info *info = NULL;
  341. struct regulator_init_data *pdata = pdev->dev.platform_data;
  342. struct resource *res;
  343. int i;
  344. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  345. if (res == NULL) {
  346. dev_err(&pdev->dev, "No I/O resource!\n");
  347. return -EINVAL;
  348. }
  349. for (i = 0; i < ARRAY_SIZE(pm8607_regulator_info); i++) {
  350. info = &pm8607_regulator_info[i];
  351. if (info->desc.id == res->start)
  352. break;
  353. }
  354. if ((i < 0) || (i > PM8607_ID_RG_MAX)) {
  355. dev_err(&pdev->dev, "Failed to find regulator %llu\n",
  356. (unsigned long long)res->start);
  357. return -EINVAL;
  358. }
  359. info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  360. info->chip = chip;
  361. /* check DVC ramp slope double */
  362. if ((i == PM8607_ID_BUCK3) && info->chip->buck3_double)
  363. info->slope_double = 1;
  364. /* replace driver_data with info */
  365. info->regulator = regulator_register(&info->desc, &pdev->dev,
  366. pdata, info);
  367. if (IS_ERR(info->regulator)) {
  368. dev_err(&pdev->dev, "failed to register regulator %s\n",
  369. info->desc.name);
  370. return PTR_ERR(info->regulator);
  371. }
  372. platform_set_drvdata(pdev, info);
  373. return 0;
  374. }
  375. static int __devexit pm8607_regulator_remove(struct platform_device *pdev)
  376. {
  377. struct pm8607_regulator_info *info = platform_get_drvdata(pdev);
  378. platform_set_drvdata(pdev, NULL);
  379. regulator_unregister(info->regulator);
  380. return 0;
  381. }
  382. static struct platform_driver pm8607_regulator_driver = {
  383. .driver = {
  384. .name = "88pm860x-regulator",
  385. .owner = THIS_MODULE,
  386. },
  387. .probe = pm8607_regulator_probe,
  388. .remove = __devexit_p(pm8607_regulator_remove),
  389. };
  390. static int __init pm8607_regulator_init(void)
  391. {
  392. return platform_driver_register(&pm8607_regulator_driver);
  393. }
  394. subsys_initcall(pm8607_regulator_init);
  395. static void __exit pm8607_regulator_exit(void)
  396. {
  397. platform_driver_unregister(&pm8607_regulator_driver);
  398. }
  399. module_exit(pm8607_regulator_exit);
  400. MODULE_LICENSE("GPL");
  401. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  402. MODULE_DESCRIPTION("Regulator Driver for Marvell 88PM8607 PMIC");
  403. MODULE_ALIAS("platform:88pm8607-regulator");