ab8500.c 11 KB

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