ab8500-ext.c 11 KB

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