ab8500-ext.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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_set_voltage(struct regulator_dev *rdev, int min_uV,
  202. int max_uV, unsigned *selector)
  203. {
  204. struct regulation_constraints *regu_constraints = rdev->constraints;
  205. if (!regu_constraints) {
  206. dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
  207. return -EINVAL;
  208. }
  209. if (regu_constraints->min_uV == min_uV &&
  210. regu_constraints->max_uV == max_uV)
  211. return 0;
  212. dev_err(rdev_get_dev(rdev),
  213. "Requested min %duV max %duV != constrained min %duV max %duV\n",
  214. min_uV, max_uV,
  215. regu_constraints->min_uV, regu_constraints->max_uV);
  216. return -EINVAL;
  217. }
  218. static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
  219. unsigned selector)
  220. {
  221. struct regulation_constraints *regu_constraints = rdev->constraints;
  222. if (regu_constraints == NULL) {
  223. dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
  224. return -EINVAL;
  225. }
  226. /* return the uV for the fixed regulators */
  227. if (regu_constraints->min_uV && regu_constraints->max_uV) {
  228. if (regu_constraints->min_uV == regu_constraints->max_uV)
  229. return regu_constraints->min_uV;
  230. }
  231. return -EINVAL;
  232. }
  233. static struct regulator_ops ab8500_ext_regulator_ops = {
  234. .enable = ab8500_ext_regulator_enable,
  235. .disable = ab8500_ext_regulator_disable,
  236. .is_enabled = ab8500_ext_regulator_is_enabled,
  237. .set_mode = ab8500_ext_regulator_set_mode,
  238. .get_mode = ab8500_ext_regulator_get_mode,
  239. .set_voltage = ab8500_ext_set_voltage,
  240. .list_voltage = ab8500_ext_list_voltage,
  241. };
  242. static struct ab8500_ext_regulator_info
  243. ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
  244. [AB8500_EXT_SUPPLY1] = {
  245. .desc = {
  246. .name = "VEXTSUPPLY1",
  247. .ops = &ab8500_ext_regulator_ops,
  248. .type = REGULATOR_VOLTAGE,
  249. .id = AB8500_EXT_SUPPLY1,
  250. .owner = THIS_MODULE,
  251. .n_voltages = 1,
  252. },
  253. .update_bank = 0x04,
  254. .update_reg = 0x08,
  255. .update_mask = 0x03,
  256. .update_val = 0x01,
  257. .update_val_hp = 0x01,
  258. .update_val_lp = 0x03,
  259. .update_val_hw = 0x02,
  260. },
  261. [AB8500_EXT_SUPPLY2] = {
  262. .desc = {
  263. .name = "VEXTSUPPLY2",
  264. .ops = &ab8500_ext_regulator_ops,
  265. .type = REGULATOR_VOLTAGE,
  266. .id = AB8500_EXT_SUPPLY2,
  267. .owner = THIS_MODULE,
  268. .n_voltages = 1,
  269. },
  270. .update_bank = 0x04,
  271. .update_reg = 0x08,
  272. .update_mask = 0x0c,
  273. .update_val = 0x04,
  274. .update_val_hp = 0x04,
  275. .update_val_lp = 0x0c,
  276. .update_val_hw = 0x08,
  277. },
  278. [AB8500_EXT_SUPPLY3] = {
  279. .desc = {
  280. .name = "VEXTSUPPLY3",
  281. .ops = &ab8500_ext_regulator_ops,
  282. .type = REGULATOR_VOLTAGE,
  283. .id = AB8500_EXT_SUPPLY3,
  284. .owner = THIS_MODULE,
  285. .n_voltages = 1,
  286. },
  287. .update_bank = 0x04,
  288. .update_reg = 0x08,
  289. .update_mask = 0x30,
  290. .update_val = 0x10,
  291. .update_val_hp = 0x10,
  292. .update_val_lp = 0x30,
  293. .update_val_hw = 0x20,
  294. },
  295. };
  296. int ab8500_ext_regulator_init(struct platform_device *pdev)
  297. {
  298. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  299. struct ab8500_platform_data *ppdata;
  300. struct ab8500_regulator_platform_data *pdata;
  301. struct regulator_config config = { };
  302. int i, err;
  303. if (!ab8500) {
  304. dev_err(&pdev->dev, "null mfd parent\n");
  305. return -EINVAL;
  306. }
  307. ppdata = dev_get_platdata(ab8500->dev);
  308. if (!ppdata) {
  309. dev_err(&pdev->dev, "null parent pdata\n");
  310. return -EINVAL;
  311. }
  312. pdata = ppdata->regulator;
  313. if (!pdata) {
  314. dev_err(&pdev->dev, "null pdata\n");
  315. return -EINVAL;
  316. }
  317. /* make sure the platform data has the correct size */
  318. if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
  319. dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
  320. return -EINVAL;
  321. }
  322. /* check for AB8500 2.x */
  323. if (is_ab8500_2p0_or_earlier(ab8500)) {
  324. struct ab8500_ext_regulator_info *info;
  325. /* VextSupply3LPn is inverted on AB8500 2.x */
  326. info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
  327. info->update_val = 0x30;
  328. info->update_val_hp = 0x30;
  329. info->update_val_lp = 0x10;
  330. }
  331. /* register all regulators */
  332. for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
  333. struct ab8500_ext_regulator_info *info = NULL;
  334. /* assign per-regulator data */
  335. info = &ab8500_ext_regulator_info[i];
  336. info->dev = &pdev->dev;
  337. info->cfg = (struct ab8500_ext_regulator_cfg *)
  338. pdata->ext_regulator[i].driver_data;
  339. config.dev = &pdev->dev;
  340. config.init_data = &pdata->ext_regulator[i];
  341. config.driver_data = info;
  342. /* register regulator with framework */
  343. info->rdev = regulator_register(&info->desc, &config);
  344. if (IS_ERR(info->rdev)) {
  345. err = PTR_ERR(info->rdev);
  346. dev_err(&pdev->dev, "failed to register regulator %s\n",
  347. info->desc.name);
  348. /* when we fail, un-register all earlier regulators */
  349. while (--i >= 0) {
  350. info = &ab8500_ext_regulator_info[i];
  351. regulator_unregister(info->rdev);
  352. }
  353. return err;
  354. }
  355. dev_dbg(rdev_get_dev(info->rdev),
  356. "%s-probed\n", info->desc.name);
  357. }
  358. return 0;
  359. }
  360. void ab8500_ext_regulator_exit(struct platform_device *pdev)
  361. {
  362. int i;
  363. for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
  364. struct ab8500_ext_regulator_info *info = NULL;
  365. info = &ab8500_ext_regulator_info[i];
  366. dev_vdbg(rdev_get_dev(info->rdev),
  367. "%s-remove\n", info->desc.name);
  368. regulator_unregister(info->rdev);
  369. }
  370. }
  371. MODULE_LICENSE("GPL v2");
  372. MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
  373. MODULE_DESCRIPTION("AB8500 external regulator driver");
  374. MODULE_ALIAS("platform:ab8500-ext-regulator");