ab8500.c 12 KB

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