ab8500.c 12 KB

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