ab8500-ext.c 10 KB

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