ab8500.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. *
  6. * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
  7. * Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
  8. *
  9. * AB8500 peripheral regulators
  10. *
  11. * AB8500 supports the following regulators:
  12. * VAUX1/2/3, VINTCORE, VTVOUT, VAUDIO, VAMIC1/2, VDMIC, VANA
  13. */
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/err.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mfd/ab8500.h>
  19. #include <linux/mfd/abx500.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/regulator/ab8500.h>
  23. /**
  24. * struct ab8500_regulator_info - ab8500 regulator information
  25. * @dev: device pointer
  26. * @desc: regulator description
  27. * @regulator_dev: regulator device
  28. * @max_uV: maximum voltage (for variable voltage supplies)
  29. * @min_uV: minimum voltage (for variable voltage supplies)
  30. * @fixed_uV: typical voltage (for fixed voltage supplies)
  31. * @update_bank: bank to control on/off
  32. * @update_reg: register to control on/off
  33. * @update_mask: mask to enable/disable regulator
  34. * @update_val_enable: bits to enable the regulator in normal (high power) mode
  35. * @voltage_bank: bank to control regulator voltage
  36. * @voltage_reg: register to control regulator voltage
  37. * @voltage_mask: mask to control regulator voltage
  38. * @voltages: supported voltage table
  39. * @voltages_len: number of supported voltages for the regulator
  40. */
  41. struct ab8500_regulator_info {
  42. struct device *dev;
  43. struct regulator_desc desc;
  44. struct regulator_dev *regulator;
  45. int max_uV;
  46. int min_uV;
  47. int fixed_uV;
  48. u8 update_bank;
  49. u8 update_reg;
  50. u8 update_mask;
  51. u8 update_val_enable;
  52. u8 voltage_bank;
  53. u8 voltage_reg;
  54. u8 voltage_mask;
  55. int const *voltages;
  56. int voltages_len;
  57. };
  58. /* voltage tables for the vauxn/vintcore supplies */
  59. static const int ldo_vauxn_voltages[] = {
  60. 1100000,
  61. 1200000,
  62. 1300000,
  63. 1400000,
  64. 1500000,
  65. 1800000,
  66. 1850000,
  67. 1900000,
  68. 2500000,
  69. 2650000,
  70. 2700000,
  71. 2750000,
  72. 2800000,
  73. 2900000,
  74. 3000000,
  75. 3300000,
  76. };
  77. static const int ldo_vaux3_voltages[] = {
  78. 1200000,
  79. 1500000,
  80. 1800000,
  81. 2100000,
  82. 2500000,
  83. 2750000,
  84. 2790000,
  85. 2910000,
  86. };
  87. static const int ldo_vintcore_voltages[] = {
  88. 1200000,
  89. 1225000,
  90. 1250000,
  91. 1275000,
  92. 1300000,
  93. 1325000,
  94. 1350000,
  95. };
  96. static int ab8500_regulator_enable(struct regulator_dev *rdev)
  97. {
  98. int regulator_id, ret;
  99. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  100. regulator_id = rdev_get_id(rdev);
  101. if (regulator_id >= AB8500_NUM_REGULATORS)
  102. return -EINVAL;
  103. ret = abx500_mask_and_set_register_interruptible(info->dev,
  104. info->update_bank, info->update_reg,
  105. info->update_mask, info->update_val_enable);
  106. if (ret < 0)
  107. dev_err(rdev_get_dev(rdev),
  108. "couldn't set enable bits for regulator\n");
  109. return ret;
  110. }
  111. static int ab8500_regulator_disable(struct regulator_dev *rdev)
  112. {
  113. int regulator_id, ret;
  114. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  115. regulator_id = rdev_get_id(rdev);
  116. if (regulator_id >= AB8500_NUM_REGULATORS)
  117. return -EINVAL;
  118. ret = abx500_mask_and_set_register_interruptible(info->dev,
  119. info->update_bank, info->update_reg,
  120. info->update_mask, 0x0);
  121. if (ret < 0)
  122. dev_err(rdev_get_dev(rdev),
  123. "couldn't set disable bits for regulator\n");
  124. return ret;
  125. }
  126. static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
  127. {
  128. int regulator_id, ret;
  129. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  130. u8 value;
  131. regulator_id = rdev_get_id(rdev);
  132. if (regulator_id >= AB8500_NUM_REGULATORS)
  133. return -EINVAL;
  134. ret = abx500_get_register_interruptible(info->dev,
  135. info->update_bank, info->update_reg, &value);
  136. if (ret < 0) {
  137. dev_err(rdev_get_dev(rdev),
  138. "couldn't read 0x%x register\n", info->update_reg);
  139. return ret;
  140. }
  141. if (value & info->update_mask)
  142. return true;
  143. else
  144. return false;
  145. }
  146. static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
  147. {
  148. int regulator_id;
  149. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  150. regulator_id = rdev_get_id(rdev);
  151. if (regulator_id >= AB8500_NUM_REGULATORS)
  152. return -EINVAL;
  153. /* return the uV for the fixed regulators */
  154. if (info->fixed_uV)
  155. return info->fixed_uV;
  156. if (selector >= info->voltages_len)
  157. return -EINVAL;
  158. return info->voltages[selector];
  159. }
  160. static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
  161. {
  162. int regulator_id, ret;
  163. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  164. u8 value;
  165. regulator_id = rdev_get_id(rdev);
  166. if (regulator_id >= AB8500_NUM_REGULATORS)
  167. return -EINVAL;
  168. ret = abx500_get_register_interruptible(info->dev, info->voltage_bank,
  169. info->voltage_reg, &value);
  170. if (ret < 0) {
  171. dev_err(rdev_get_dev(rdev),
  172. "couldn't read voltage reg for regulator\n");
  173. return ret;
  174. }
  175. /* vintcore has a different layout */
  176. value &= info->voltage_mask;
  177. if (regulator_id == AB8500_LDO_INTCORE)
  178. ret = info->voltages[value >> 0x3];
  179. else
  180. ret = info->voltages[value];
  181. return ret;
  182. }
  183. static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
  184. int min_uV, int max_uV)
  185. {
  186. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  187. int i;
  188. /* check the supported voltage */
  189. for (i = 0; i < info->voltages_len; i++) {
  190. if ((info->voltages[i] >= min_uV) &&
  191. (info->voltages[i] <= max_uV))
  192. return i;
  193. }
  194. return -EINVAL;
  195. }
  196. static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
  197. int min_uV, int max_uV,
  198. unsigned *selector)
  199. {
  200. int regulator_id, ret;
  201. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  202. regulator_id = rdev_get_id(rdev);
  203. if (regulator_id >= AB8500_NUM_REGULATORS)
  204. return -EINVAL;
  205. /* get the appropriate voltages within the range */
  206. ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
  207. if (ret < 0) {
  208. dev_err(rdev_get_dev(rdev),
  209. "couldn't get best voltage for regulator\n");
  210. return ret;
  211. }
  212. *selector = ret;
  213. /* set the registers for the request */
  214. ret = abx500_mask_and_set_register_interruptible(info->dev,
  215. info->voltage_bank, info->voltage_reg,
  216. info->voltage_mask, (u8)ret);
  217. if (ret < 0)
  218. dev_err(rdev_get_dev(rdev),
  219. "couldn't set voltage reg for regulator\n");
  220. return ret;
  221. }
  222. static struct regulator_ops ab8500_regulator_ops = {
  223. .enable = ab8500_regulator_enable,
  224. .disable = ab8500_regulator_disable,
  225. .is_enabled = ab8500_regulator_is_enabled,
  226. .get_voltage = ab8500_regulator_get_voltage,
  227. .set_voltage = ab8500_regulator_set_voltage,
  228. .list_voltage = ab8500_list_voltage,
  229. };
  230. static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
  231. {
  232. int regulator_id;
  233. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  234. regulator_id = rdev_get_id(rdev);
  235. if (regulator_id >= AB8500_NUM_REGULATORS)
  236. return -EINVAL;
  237. return info->fixed_uV;
  238. }
  239. static struct regulator_ops ab8500_ldo_fixed_ops = {
  240. .enable = ab8500_regulator_enable,
  241. .disable = ab8500_regulator_disable,
  242. .is_enabled = ab8500_regulator_is_enabled,
  243. .get_voltage = ab8500_fixed_get_voltage,
  244. .list_voltage = ab8500_list_voltage,
  245. };
  246. #define AB8500_LDO(_id, _min_mV, _max_mV, \
  247. _u_bank, _u_reg, _u_mask, _u_val_enable, \
  248. _v_bank, _v_reg, _v_mask, _v_table, _v_table_len) \
  249. [AB8500_LDO_##_id] = { \
  250. .desc = { \
  251. .name = "LDO-" #_id, \
  252. .ops = &ab8500_regulator_ops, \
  253. .type = REGULATOR_VOLTAGE, \
  254. .id = AB8500_LDO_##_id, \
  255. .owner = THIS_MODULE, \
  256. }, \
  257. .min_uV = (_min_mV) * 1000, \
  258. .max_uV = (_max_mV) * 1000, \
  259. .update_bank = _u_bank, \
  260. .update_reg = _u_reg, \
  261. .update_mask = _u_mask, \
  262. .update_val_enable = _u_val_enable, \
  263. .voltage_bank = _v_bank, \
  264. .voltage_reg = _v_reg, \
  265. .voltage_mask = _v_mask, \
  266. .voltages = _v_table, \
  267. .voltages_len = _v_table_len, \
  268. .fixed_uV = 0, \
  269. }
  270. #define AB8500_FIXED_LDO(_id, _fixed_mV, \
  271. _u_bank, _u_reg, _u_mask, _u_val_enable) \
  272. [AB8500_LDO_##_id] = { \
  273. .desc = { \
  274. .name = "LDO-" #_id, \
  275. .ops = &ab8500_ldo_fixed_ops, \
  276. .type = REGULATOR_VOLTAGE, \
  277. .id = AB8500_LDO_##_id, \
  278. .owner = THIS_MODULE, \
  279. }, \
  280. .fixed_uV = (_fixed_mV) * 1000, \
  281. .update_bank = _u_bank, \
  282. .update_reg = _u_reg, \
  283. .update_mask = _u_mask, \
  284. .update_val_enable = _u_val_enable, \
  285. }
  286. static struct ab8500_regulator_info ab8500_regulator_info[] = {
  287. /*
  288. * Variable Voltage Regulators
  289. * name, min mV, max mV,
  290. * update bank, reg, mask, enable val
  291. * volt bank, reg, mask, table, table length
  292. */
  293. AB8500_LDO(AUX1, 1100, 3300,
  294. 0x04, 0x09, 0x03, 0x01, 0x04, 0x1f, 0x0f,
  295. ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
  296. AB8500_LDO(AUX2, 1100, 3300,
  297. 0x04, 0x09, 0x0c, 0x04, 0x04, 0x20, 0x0f,
  298. ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
  299. AB8500_LDO(AUX3, 1100, 3300,
  300. 0x04, 0x0a, 0x03, 0x01, 0x04, 0x21, 0x07,
  301. ldo_vaux3_voltages, ARRAY_SIZE(ldo_vaux3_voltages)),
  302. AB8500_LDO(INTCORE, 1100, 3300,
  303. 0x03, 0x80, 0x44, 0x04, 0x03, 0x80, 0x38,
  304. ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
  305. /*
  306. * Fixed Voltage Regulators
  307. * name, fixed mV,
  308. * update bank, reg, mask, enable val
  309. */
  310. AB8500_FIXED_LDO(TVOUT, 2000, 0x03, 0x80, 0x82, 0x02),
  311. AB8500_FIXED_LDO(AUDIO, 2000, 0x03, 0x83, 0x02, 0x02),
  312. AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03, 0x83, 0x08, 0x08),
  313. AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03, 0x83, 0x10, 0x10),
  314. AB8500_FIXED_LDO(DMIC, 1800, 0x03, 0x83, 0x04, 0x04),
  315. AB8500_FIXED_LDO(ANA, 1200, 0x04, 0x06, 0x0c, 0x04),
  316. };
  317. static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
  318. {
  319. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  320. struct ab8500_platform_data *pdata;
  321. int i, err;
  322. if (!ab8500) {
  323. dev_err(&pdev->dev, "null mfd parent\n");
  324. return -EINVAL;
  325. }
  326. pdata = dev_get_platdata(ab8500->dev);
  327. /* make sure the platform data has the correct size */
  328. if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
  329. dev_err(&pdev->dev, "platform configuration error\n");
  330. return -EINVAL;
  331. }
  332. /* register all regulators */
  333. for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
  334. struct ab8500_regulator_info *info = NULL;
  335. /* assign per-regulator data */
  336. info = &ab8500_regulator_info[i];
  337. info->dev = &pdev->dev;
  338. /* fix for hardware before ab8500v2.0 */
  339. if (abx500_get_chip_id(info->dev) < 0x20) {
  340. if (info->desc.id == AB8500_LDO_AUX3) {
  341. info->desc.n_voltages =
  342. ARRAY_SIZE(ldo_vauxn_voltages);
  343. info->voltages = ldo_vauxn_voltages;
  344. info->voltages_len =
  345. ARRAY_SIZE(ldo_vauxn_voltages);
  346. info->voltage_mask = 0xf;
  347. }
  348. }
  349. /* register regulator with framework */
  350. info->regulator = regulator_register(&info->desc, &pdev->dev,
  351. &pdata->regulator[i], info);
  352. if (IS_ERR(info->regulator)) {
  353. err = PTR_ERR(info->regulator);
  354. dev_err(&pdev->dev, "failed to register regulator %s\n",
  355. info->desc.name);
  356. /* when we fail, un-register all earlier regulators */
  357. while (--i >= 0) {
  358. info = &ab8500_regulator_info[i];
  359. regulator_unregister(info->regulator);
  360. }
  361. return err;
  362. }
  363. }
  364. return 0;
  365. }
  366. static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
  367. {
  368. int i;
  369. for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
  370. struct ab8500_regulator_info *info = NULL;
  371. info = &ab8500_regulator_info[i];
  372. regulator_unregister(info->regulator);
  373. }
  374. return 0;
  375. }
  376. static struct platform_driver ab8500_regulator_driver = {
  377. .probe = ab8500_regulator_probe,
  378. .remove = __devexit_p(ab8500_regulator_remove),
  379. .driver = {
  380. .name = "ab8500-regulator",
  381. .owner = THIS_MODULE,
  382. },
  383. };
  384. static int __init ab8500_regulator_init(void)
  385. {
  386. int ret;
  387. ret = platform_driver_register(&ab8500_regulator_driver);
  388. if (ret != 0)
  389. pr_err("Failed to register ab8500 regulator: %d\n", ret);
  390. return ret;
  391. }
  392. subsys_initcall(ab8500_regulator_init);
  393. static void __exit ab8500_regulator_exit(void)
  394. {
  395. platform_driver_unregister(&ab8500_regulator_driver);
  396. }
  397. module_exit(ab8500_regulator_exit);
  398. MODULE_LICENSE("GPL v2");
  399. MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
  400. MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
  401. MODULE_ALIAS("platform:ab8500-regulator");