ab8500-ext.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. *
  6. * Authors: Bengt Jonsson <bengt.g.jonsson@stericsson.com>
  7. *
  8. * This file is based on drivers/regulator/ab8500.c
  9. *
  10. * AB8500 external regulators
  11. *
  12. * ab8500-ext supports the following regulators:
  13. * - VextSupply3
  14. */
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/err.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regulator/driver.h>
  22. #include <linux/regulator/machine.h>
  23. #include <linux/regulator/of_regulator.h>
  24. #include <linux/mfd/abx500.h>
  25. #include <linux/mfd/abx500/ab8500.h>
  26. #include <linux/regulator/ab8500.h>
  27. /**
  28. * struct ab8500_ext_regulator_info - ab8500 regulator information
  29. * @dev: device pointer
  30. * @desc: regulator description
  31. * @rdev: regulator device
  32. * @cfg: regulator configuration (extension of regulator FW configuration)
  33. * @update_bank: bank to control on/off
  34. * @update_reg: register to control on/off
  35. * @update_mask: mask to enable/disable and set mode of regulator
  36. * @update_val: bits holding the regulator current mode
  37. * @update_val_hp: bits to set EN pin active (LPn pin deactive)
  38. * normally this means high power mode
  39. * @update_val_lp: bits to set EN pin active and LPn pin active
  40. * normally this means low power mode
  41. * @update_val_hw: bits to set regulator pins in HW control
  42. * SysClkReq pins and logic will choose mode
  43. */
  44. struct ab8500_ext_regulator_info {
  45. struct device *dev;
  46. struct regulator_desc desc;
  47. struct regulator_dev *rdev;
  48. struct ab8500_ext_regulator_cfg *cfg;
  49. u8 update_bank;
  50. u8 update_reg;
  51. u8 update_mask;
  52. u8 update_val;
  53. u8 update_val_hp;
  54. u8 update_val_lp;
  55. u8 update_val_hw;
  56. };
  57. static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
  58. {
  59. int ret;
  60. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  61. u8 regval;
  62. if (info == NULL) {
  63. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  64. return -EINVAL;
  65. }
  66. /*
  67. * To satisfy both HW high power request and SW request, the regulator
  68. * must be on in high power.
  69. */
  70. if (info->cfg && info->cfg->hwreq)
  71. regval = info->update_val_hp;
  72. else
  73. regval = info->update_val;
  74. ret = abx500_mask_and_set_register_interruptible(info->dev,
  75. info->update_bank, info->update_reg,
  76. info->update_mask, regval);
  77. if (ret < 0) {
  78. dev_err(rdev_get_dev(info->rdev),
  79. "couldn't set enable bits for regulator\n");
  80. return ret;
  81. }
  82. dev_dbg(rdev_get_dev(rdev),
  83. "%s-enable (bank, reg, mask, value): 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
  84. info->desc.name, info->update_bank, info->update_reg,
  85. info->update_mask, regval);
  86. return 0;
  87. }
  88. static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
  89. {
  90. int ret;
  91. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  92. u8 regval;
  93. if (info == NULL) {
  94. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  95. return -EINVAL;
  96. }
  97. /*
  98. * Set the regulator in HW request mode if configured
  99. */
  100. if (info->cfg && info->cfg->hwreq)
  101. regval = info->update_val_hw;
  102. else
  103. regval = 0;
  104. ret = abx500_mask_and_set_register_interruptible(info->dev,
  105. info->update_bank, info->update_reg,
  106. info->update_mask, regval);
  107. if (ret < 0) {
  108. dev_err(rdev_get_dev(info->rdev),
  109. "couldn't set disable bits for regulator\n");
  110. return ret;
  111. }
  112. dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
  113. " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
  114. info->desc.name, info->update_bank, info->update_reg,
  115. info->update_mask, regval);
  116. return 0;
  117. }
  118. static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
  119. {
  120. int ret;
  121. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  122. u8 regval;
  123. if (info == NULL) {
  124. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  125. return -EINVAL;
  126. }
  127. ret = abx500_get_register_interruptible(info->dev,
  128. info->update_bank, info->update_reg, &regval);
  129. if (ret < 0) {
  130. dev_err(rdev_get_dev(rdev),
  131. "couldn't read 0x%x register\n", info->update_reg);
  132. return ret;
  133. }
  134. dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
  135. " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
  136. info->desc.name, info->update_bank, info->update_reg,
  137. info->update_mask, regval);
  138. if (((regval & info->update_mask) == info->update_val_lp) ||
  139. ((regval & info->update_mask) == info->update_val_hp))
  140. return 1;
  141. else
  142. return 0;
  143. }
  144. static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
  145. unsigned int mode)
  146. {
  147. int ret = 0;
  148. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  149. u8 regval;
  150. if (info == NULL) {
  151. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  152. return -EINVAL;
  153. }
  154. switch (mode) {
  155. case REGULATOR_MODE_NORMAL:
  156. regval = info->update_val_hp;
  157. break;
  158. case REGULATOR_MODE_IDLE:
  159. regval = info->update_val_lp;
  160. break;
  161. default:
  162. return -EINVAL;
  163. }
  164. /* If regulator is enabled and info->cfg->hwreq is set, the regulator
  165. must be on in high power, so we don't need to write the register with
  166. the same value.
  167. */
  168. if (ab8500_ext_regulator_is_enabled(rdev) &&
  169. !(info->cfg && info->cfg->hwreq)) {
  170. ret = abx500_mask_and_set_register_interruptible(info->dev,
  171. info->update_bank, info->update_reg,
  172. info->update_mask, regval);
  173. if (ret < 0) {
  174. dev_err(rdev_get_dev(rdev),
  175. "Could not set regulator mode.\n");
  176. return ret;
  177. }
  178. dev_dbg(rdev_get_dev(rdev),
  179. "%s-set_mode (bank, reg, mask, value): "
  180. "0x%x, 0x%x, 0x%x, 0x%x\n",
  181. info->desc.name, info->update_bank, info->update_reg,
  182. info->update_mask, regval);
  183. }
  184. info->update_val = regval;
  185. return 0;
  186. }
  187. static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
  188. {
  189. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  190. int ret;
  191. if (info == NULL) {
  192. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  193. return -EINVAL;
  194. }
  195. if (info->update_val == info->update_val_hp)
  196. ret = REGULATOR_MODE_NORMAL;
  197. else if (info->update_val == info->update_val_lp)
  198. ret = REGULATOR_MODE_IDLE;
  199. else
  200. ret = -EINVAL;
  201. return ret;
  202. }
  203. static int ab8500_ext_set_voltage(struct regulator_dev *rdev, int min_uV,
  204. int max_uV, unsigned *selector)
  205. {
  206. struct regulation_constraints *regu_constraints = rdev->constraints;
  207. if (!regu_constraints) {
  208. dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
  209. return -EINVAL;
  210. }
  211. if (regu_constraints->min_uV == min_uV &&
  212. regu_constraints->max_uV == max_uV)
  213. return 0;
  214. dev_err(rdev_get_dev(rdev),
  215. "Requested min %duV max %duV != constrained min %duV max %duV\n",
  216. min_uV, max_uV,
  217. regu_constraints->min_uV, regu_constraints->max_uV);
  218. return -EINVAL;
  219. }
  220. static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
  221. unsigned selector)
  222. {
  223. struct regulation_constraints *regu_constraints = rdev->constraints;
  224. if (regu_constraints == NULL) {
  225. dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
  226. return -EINVAL;
  227. }
  228. /* return the uV for the fixed regulators */
  229. if (regu_constraints->min_uV && regu_constraints->max_uV) {
  230. if (regu_constraints->min_uV == regu_constraints->max_uV)
  231. return regu_constraints->min_uV;
  232. }
  233. return -EINVAL;
  234. }
  235. static struct regulator_ops ab8500_ext_regulator_ops = {
  236. .enable = ab8500_ext_regulator_enable,
  237. .disable = ab8500_ext_regulator_disable,
  238. .is_enabled = ab8500_ext_regulator_is_enabled,
  239. .set_mode = ab8500_ext_regulator_set_mode,
  240. .get_mode = ab8500_ext_regulator_get_mode,
  241. .set_voltage = ab8500_ext_set_voltage,
  242. .list_voltage = ab8500_ext_list_voltage,
  243. };
  244. static struct ab8500_ext_regulator_info
  245. ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
  246. [AB8500_EXT_SUPPLY1] = {
  247. .desc = {
  248. .name = "VEXTSUPPLY1",
  249. .ops = &ab8500_ext_regulator_ops,
  250. .type = REGULATOR_VOLTAGE,
  251. .id = AB8500_EXT_SUPPLY1,
  252. .owner = THIS_MODULE,
  253. .n_voltages = 1,
  254. },
  255. .update_bank = 0x04,
  256. .update_reg = 0x08,
  257. .update_mask = 0x03,
  258. .update_val = 0x01,
  259. .update_val_hp = 0x01,
  260. .update_val_lp = 0x03,
  261. .update_val_hw = 0x02,
  262. },
  263. [AB8500_EXT_SUPPLY2] = {
  264. .desc = {
  265. .name = "VEXTSUPPLY2",
  266. .ops = &ab8500_ext_regulator_ops,
  267. .type = REGULATOR_VOLTAGE,
  268. .id = AB8500_EXT_SUPPLY2,
  269. .owner = THIS_MODULE,
  270. .n_voltages = 1,
  271. },
  272. .update_bank = 0x04,
  273. .update_reg = 0x08,
  274. .update_mask = 0x0c,
  275. .update_val = 0x04,
  276. .update_val_hp = 0x04,
  277. .update_val_lp = 0x0c,
  278. .update_val_hw = 0x08,
  279. },
  280. [AB8500_EXT_SUPPLY3] = {
  281. .desc = {
  282. .name = "VEXTSUPPLY3",
  283. .ops = &ab8500_ext_regulator_ops,
  284. .type = REGULATOR_VOLTAGE,
  285. .id = AB8500_EXT_SUPPLY3,
  286. .owner = THIS_MODULE,
  287. .n_voltages = 1,
  288. },
  289. .update_bank = 0x04,
  290. .update_reg = 0x08,
  291. .update_mask = 0x30,
  292. .update_val = 0x10,
  293. .update_val_hp = 0x10,
  294. .update_val_lp = 0x30,
  295. .update_val_hw = 0x20,
  296. },
  297. };
  298. static struct of_regulator_match ab8500_ext_regulator_match[] = {
  299. { .name = "ab8500_ext1", .driver_data = (void *) AB8500_EXT_SUPPLY1, },
  300. { .name = "ab8500_ext2", .driver_data = (void *) AB8500_EXT_SUPPLY2, },
  301. { .name = "ab8500_ext3", .driver_data = (void *) AB8500_EXT_SUPPLY3, },
  302. };
  303. static int ab8500_ext_regulator_probe(struct platform_device *pdev)
  304. {
  305. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  306. struct ab8500_platform_data *ppdata;
  307. struct ab8500_regulator_platform_data *pdata;
  308. struct device_node *np = pdev->dev.of_node;
  309. struct regulator_config config = { };
  310. int i, err;
  311. if (np) {
  312. err = of_regulator_match(&pdev->dev, np,
  313. ab8500_ext_regulator_match,
  314. ARRAY_SIZE(ab8500_ext_regulator_match));
  315. if (err < 0) {
  316. dev_err(&pdev->dev,
  317. "Error parsing regulator init data: %d\n", err);
  318. return err;
  319. }
  320. }
  321. if (!ab8500) {
  322. dev_err(&pdev->dev, "null mfd parent\n");
  323. return -EINVAL;
  324. }
  325. ppdata = dev_get_platdata(ab8500->dev);
  326. if (!ppdata) {
  327. dev_err(&pdev->dev, "null parent pdata\n");
  328. return -EINVAL;
  329. }
  330. pdata = ppdata->regulator;
  331. if (!pdata) {
  332. dev_err(&pdev->dev, "null pdata\n");
  333. return -EINVAL;
  334. }
  335. /* make sure the platform data has the correct size */
  336. if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
  337. dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
  338. return -EINVAL;
  339. }
  340. /* check for AB8500 2.x */
  341. if (is_ab8500_2p0_or_earlier(ab8500)) {
  342. struct ab8500_ext_regulator_info *info;
  343. /* VextSupply3LPn is inverted on AB8500 2.x */
  344. info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
  345. info->update_val = 0x30;
  346. info->update_val_hp = 0x30;
  347. info->update_val_lp = 0x10;
  348. }
  349. /* register all regulators */
  350. for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
  351. struct ab8500_ext_regulator_info *info = NULL;
  352. /* assign per-regulator data */
  353. info = &ab8500_ext_regulator_info[i];
  354. info->dev = &pdev->dev;
  355. info->cfg = (struct ab8500_ext_regulator_cfg *)
  356. pdata->ext_regulator[i].driver_data;
  357. config.dev = &pdev->dev;
  358. config.driver_data = info;
  359. config.of_node = ab8500_ext_regulator_match[i].of_node;
  360. config.init_data = (np) ?
  361. ab8500_ext_regulator_match[i].init_data :
  362. &pdata->ext_regulator[i];
  363. /* register regulator with framework */
  364. info->rdev = devm_regulator_register(&pdev->dev, &info->desc,
  365. &config);
  366. if (IS_ERR(info->rdev)) {
  367. err = PTR_ERR(info->rdev);
  368. dev_err(&pdev->dev, "failed to register regulator %s\n",
  369. info->desc.name);
  370. return err;
  371. }
  372. dev_dbg(rdev_get_dev(info->rdev),
  373. "%s-probed\n", info->desc.name);
  374. }
  375. return 0;
  376. }
  377. static struct platform_driver ab8500_ext_regulator_driver = {
  378. .probe = ab8500_ext_regulator_probe,
  379. .driver = {
  380. .name = "ab8500-ext-regulator",
  381. },
  382. };
  383. static int __init ab8500_ext_regulator_init(void)
  384. {
  385. int ret;
  386. ret = platform_driver_register(&ab8500_ext_regulator_driver);
  387. if (ret)
  388. pr_err("Failed to register ab8500 ext regulator: %d\n", ret);
  389. return ret;
  390. }
  391. subsys_initcall(ab8500_ext_regulator_init);
  392. static void __exit ab8500_ext_regulator_exit(void)
  393. {
  394. platform_driver_unregister(&ab8500_ext_regulator_driver);
  395. }
  396. module_exit(ab8500_ext_regulator_exit);
  397. MODULE_LICENSE("GPL v2");
  398. MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
  399. MODULE_DESCRIPTION("AB8500 external regulator driver");
  400. MODULE_ALIAS("platform:ab8500-ext-regulator");