ab8500.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. unsigned *selector)
  193. {
  194. int regulator_id, ret;
  195. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  196. regulator_id = rdev_get_id(rdev);
  197. if (regulator_id >= AB8500_NUM_REGULATORS)
  198. return -EINVAL;
  199. /* get the appropriate voltages within the range */
  200. ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
  201. if (ret < 0) {
  202. dev_err(rdev_get_dev(rdev),
  203. "couldn't get best voltage for regulator\n");
  204. return ret;
  205. }
  206. *selector = ret;
  207. /* set the registers for the request */
  208. ret = abx500_mask_and_set_register_interruptible(info->dev,
  209. info->voltage_bank, info->voltage_reg,
  210. info->voltage_mask, (u8)ret);
  211. if (ret < 0)
  212. dev_err(rdev_get_dev(rdev),
  213. "couldn't set voltage reg for regulator\n");
  214. return ret;
  215. }
  216. static struct regulator_ops ab8500_regulator_ops = {
  217. .enable = ab8500_regulator_enable,
  218. .disable = ab8500_regulator_disable,
  219. .is_enabled = ab8500_regulator_is_enabled,
  220. .get_voltage = ab8500_regulator_get_voltage,
  221. .set_voltage = ab8500_regulator_set_voltage,
  222. .list_voltage = ab8500_list_voltage,
  223. };
  224. static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
  225. {
  226. int regulator_id;
  227. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  228. regulator_id = rdev_get_id(rdev);
  229. if (regulator_id >= AB8500_NUM_REGULATORS)
  230. return -EINVAL;
  231. return info->fixed_uV;
  232. }
  233. static struct regulator_ops ab8500_ldo_fixed_ops = {
  234. .enable = ab8500_regulator_enable,
  235. .disable = ab8500_regulator_disable,
  236. .is_enabled = ab8500_regulator_is_enabled,
  237. .get_voltage = ab8500_fixed_get_voltage,
  238. .list_voltage = ab8500_list_voltage,
  239. };
  240. #define AB8500_LDO(_id, min, max, bank, reg, reg_mask, \
  241. reg_enable, volt_bank, volt_reg, volt_mask, \
  242. voltages, len_volts) \
  243. { \
  244. .desc = { \
  245. .name = "LDO-" #_id, \
  246. .ops = &ab8500_regulator_ops, \
  247. .type = REGULATOR_VOLTAGE, \
  248. .id = AB8500_LDO_##_id, \
  249. .owner = THIS_MODULE, \
  250. }, \
  251. .min_uV = (min) * 1000, \
  252. .max_uV = (max) * 1000, \
  253. .update_bank = bank, \
  254. .update_reg = reg, \
  255. .mask = reg_mask, \
  256. .enable = reg_enable, \
  257. .voltage_bank = volt_bank, \
  258. .voltage_reg = volt_reg, \
  259. .voltage_mask = volt_mask, \
  260. .supported_voltages = voltages, \
  261. .voltages_len = len_volts, \
  262. .fixed_uV = 0, \
  263. }
  264. #define AB8500_FIXED_LDO(_id, fixed, bank, reg, \
  265. reg_mask, reg_enable) \
  266. { \
  267. .desc = { \
  268. .name = "LDO-" #_id, \
  269. .ops = &ab8500_ldo_fixed_ops, \
  270. .type = REGULATOR_VOLTAGE, \
  271. .id = AB8500_LDO_##_id, \
  272. .owner = THIS_MODULE, \
  273. }, \
  274. .fixed_uV = fixed * 1000, \
  275. .update_bank = bank, \
  276. .update_reg = reg, \
  277. .mask = reg_mask, \
  278. .enable = reg_enable, \
  279. }
  280. static struct ab8500_regulator_info ab8500_regulator_info[] = {
  281. /*
  282. * Variable Voltage LDOs
  283. * name, min uV, max uV, ctrl bank, ctrl reg, reg mask, enable mask,
  284. * volt ctrl bank, volt ctrl reg, volt ctrl mask, volt table,
  285. * num supported volts
  286. */
  287. AB8500_LDO(AUX1, 1100, 3300, 0x04, 0x09, 0x3, 0x1, 0x04, 0x1f, 0xf,
  288. ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
  289. AB8500_LDO(AUX2, 1100, 3300, 0x04, 0x09, 0xc, 0x4, 0x04, 0x20, 0xf,
  290. ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
  291. AB8500_LDO(AUX3, 1100, 3300, 0x04, 0x0a, 0x3, 0x1, 0x04, 0x21, 0xf,
  292. ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
  293. AB8500_LDO(INTCORE, 1100, 3300, 0x03, 0x80, 0x44, 0x4, 0x03, 0x80, 0x38,
  294. ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
  295. /*
  296. * Fixed Voltage LDOs
  297. * name, o/p uV, ctrl bank, ctrl reg, enable, disable
  298. */
  299. AB8500_FIXED_LDO(TVOUT, 2000, 0x03, 0x80, 0x82, 0x2),
  300. AB8500_FIXED_LDO(AUDIO, 2000, 0x03, 0x83, 0x2, 0x2),
  301. AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03, 0x83, 0x08, 0x08),
  302. AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03, 0x83, 0x10, 0x10),
  303. AB8500_FIXED_LDO(DMIC, 1800, 0x03, 0x83, 0x04, 0x04),
  304. AB8500_FIXED_LDO(ANA, 1200, 0x04, 0x06, 0xc, 0x4),
  305. };
  306. static inline struct ab8500_regulator_info *find_regulator_info(int id)
  307. {
  308. struct ab8500_regulator_info *info;
  309. int i;
  310. for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
  311. info = &ab8500_regulator_info[i];
  312. if (info->desc.id == id)
  313. return info;
  314. }
  315. return NULL;
  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. info->ab8500 = ab8500;
  339. info->regulator = regulator_register(&info->desc, &pdev->dev,
  340. &pdata->regulator[i], info);
  341. if (IS_ERR(info->regulator)) {
  342. err = PTR_ERR(info->regulator);
  343. dev_err(&pdev->dev, "failed to register regulator %s\n",
  344. info->desc.name);
  345. /* when we fail, un-register all earlier regulators */
  346. while (--i >= 0) {
  347. info = &ab8500_regulator_info[i];
  348. regulator_unregister(info->regulator);
  349. }
  350. return err;
  351. }
  352. }
  353. return 0;
  354. }
  355. static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
  356. {
  357. int i;
  358. for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
  359. struct ab8500_regulator_info *info = NULL;
  360. info = &ab8500_regulator_info[i];
  361. regulator_unregister(info->regulator);
  362. }
  363. return 0;
  364. }
  365. static struct platform_driver ab8500_regulator_driver = {
  366. .probe = ab8500_regulator_probe,
  367. .remove = __devexit_p(ab8500_regulator_remove),
  368. .driver = {
  369. .name = "ab8500-regulator",
  370. .owner = THIS_MODULE,
  371. },
  372. };
  373. static int __init ab8500_regulator_init(void)
  374. {
  375. int ret;
  376. ret = platform_driver_register(&ab8500_regulator_driver);
  377. if (ret != 0)
  378. pr_err("Failed to register ab8500 regulator: %d\n", ret);
  379. return ret;
  380. }
  381. subsys_initcall(ab8500_regulator_init);
  382. static void __exit ab8500_regulator_exit(void)
  383. {
  384. platform_driver_unregister(&ab8500_regulator_driver);
  385. }
  386. module_exit(ab8500_regulator_exit);
  387. MODULE_LICENSE("GPL v2");
  388. MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
  389. MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
  390. MODULE_ALIAS("platform:ab8500-regulator");