ab8500-ext.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. * @is_enabled: status of regulator (on/off)
  32. * @update_bank: bank to control on/off
  33. * @update_reg: register to control on/off
  34. * @update_mask: mask to enable/disable and set mode of regulator
  35. * @update_val: bits holding the regulator current mode
  36. * @update_val_hp: bits to set EN pin active (LPn pin deactive)
  37. * normally this means high power mode
  38. * @update_val_lp: bits to set EN pin active and LPn pin active
  39. * normally this means low power mode
  40. * @update_val_hw: bits to set regulator pins in HW control
  41. * SysClkReq pins and logic will choose mode
  42. */
  43. struct ab8500_ext_regulator_info {
  44. struct device *dev;
  45. struct regulator_desc desc;
  46. struct regulator_dev *rdev;
  47. struct ab8500_ext_regulator_cfg *cfg;
  48. bool is_enabled;
  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 enable(struct ab8500_ext_regulator_info *info, u8 *regval)
  58. {
  59. int ret;
  60. *regval = info->update_val;
  61. /*
  62. * To satisfy both HW high power request and SW request, the regulator
  63. * must be on in high power.
  64. */
  65. if (info->cfg && info->cfg->hwreq)
  66. *regval = info->update_val_hp;
  67. ret = abx500_mask_and_set_register_interruptible(info->dev,
  68. info->update_bank, info->update_reg,
  69. info->update_mask, *regval);
  70. if (ret < 0)
  71. dev_err(rdev_get_dev(info->rdev),
  72. "couldn't set enable bits for regulator\n");
  73. info->is_enabled = true;
  74. return ret;
  75. }
  76. static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
  77. {
  78. int ret;
  79. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  80. u8 regval;
  81. if (info == NULL) {
  82. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  83. return -EINVAL;
  84. }
  85. ret = enable(info, &regval);
  86. dev_dbg(rdev_get_dev(rdev), "%s-enable (bank, reg, mask, value):"
  87. " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
  88. info->desc.name, info->update_bank, info->update_reg,
  89. info->update_mask, regval);
  90. return ret;
  91. }
  92. static int disable(struct ab8500_ext_regulator_info *info, u8 *regval)
  93. {
  94. int ret;
  95. *regval = 0x0;
  96. /*
  97. * Set the regulator in HW request mode if configured
  98. */
  99. if (info->cfg && info->cfg->hwreq)
  100. *regval = info->update_val_hw;
  101. ret = abx500_mask_and_set_register_interruptible(info->dev,
  102. info->update_bank, info->update_reg,
  103. info->update_mask, *regval);
  104. if (ret < 0)
  105. dev_err(rdev_get_dev(info->rdev),
  106. "couldn't set disable bits for regulator\n");
  107. info->is_enabled = false;
  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. info->is_enabled = true;
  149. else
  150. info->is_enabled = false;
  151. return info->is_enabled;
  152. }
  153. static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
  154. unsigned int mode)
  155. {
  156. int ret = 0;
  157. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  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. info->update_val = info->update_val_hp;
  165. break;
  166. case REGULATOR_MODE_IDLE:
  167. info->update_val = info->update_val_lp;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. if (info->is_enabled) {
  173. u8 regval;
  174. ret = enable(info, &regval);
  175. if (ret < 0)
  176. dev_err(rdev_get_dev(rdev),
  177. "Could not set regulator mode.\n");
  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. return ret;
  185. }
  186. static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
  187. {
  188. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  189. int ret;
  190. if (info == NULL) {
  191. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  192. return -EINVAL;
  193. }
  194. if (info->update_val == info->update_val_hp)
  195. ret = REGULATOR_MODE_NORMAL;
  196. else if (info->update_val == info->update_val_lp)
  197. ret = REGULATOR_MODE_IDLE;
  198. else
  199. ret = -EINVAL;
  200. return ret;
  201. }
  202. static int ab8500_ext_fixed_get_voltage(struct regulator_dev *rdev)
  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. if (regu_constraints->min_uV && regu_constraints->max_uV) {
  210. if (regu_constraints->min_uV == regu_constraints->max_uV)
  211. return regu_constraints->min_uV;
  212. }
  213. return -EINVAL;
  214. }
  215. static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
  216. unsigned selector)
  217. {
  218. struct regulation_constraints *regu_constraints = rdev->constraints;
  219. if (regu_constraints == NULL) {
  220. dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
  221. return -EINVAL;
  222. }
  223. /* return the uV for the fixed regulators */
  224. if (regu_constraints->min_uV && regu_constraints->max_uV) {
  225. if (regu_constraints->min_uV == regu_constraints->max_uV)
  226. return regu_constraints->min_uV;
  227. }
  228. return -EINVAL;
  229. }
  230. static struct regulator_ops ab8500_ext_regulator_ops = {
  231. .enable = ab8500_ext_regulator_enable,
  232. .disable = ab8500_ext_regulator_disable,
  233. .is_enabled = ab8500_ext_regulator_is_enabled,
  234. .set_mode = ab8500_ext_regulator_set_mode,
  235. .get_mode = ab8500_ext_regulator_get_mode,
  236. .get_voltage = ab8500_ext_fixed_get_voltage,
  237. .list_voltage = ab8500_ext_list_voltage,
  238. };
  239. static struct ab8500_ext_regulator_info
  240. ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
  241. [AB8500_EXT_SUPPLY1] = {
  242. .desc = {
  243. .name = "VEXTSUPPLY1",
  244. .ops = &ab8500_ext_regulator_ops,
  245. .type = REGULATOR_VOLTAGE,
  246. .id = AB8500_EXT_SUPPLY1,
  247. .owner = THIS_MODULE,
  248. .n_voltages = 1,
  249. },
  250. .update_bank = 0x04,
  251. .update_reg = 0x08,
  252. .update_mask = 0x03,
  253. .update_val = 0x01,
  254. .update_val_hp = 0x01,
  255. .update_val_lp = 0x03,
  256. .update_val_hw = 0x02,
  257. },
  258. [AB8500_EXT_SUPPLY2] = {
  259. .desc = {
  260. .name = "VEXTSUPPLY2",
  261. .ops = &ab8500_ext_regulator_ops,
  262. .type = REGULATOR_VOLTAGE,
  263. .id = AB8500_EXT_SUPPLY2,
  264. .owner = THIS_MODULE,
  265. .n_voltages = 1,
  266. },
  267. .update_bank = 0x04,
  268. .update_reg = 0x08,
  269. .update_mask = 0x0c,
  270. .update_val = 0x04,
  271. .update_val_hp = 0x04,
  272. .update_val_lp = 0x0c,
  273. .update_val_hw = 0x08,
  274. },
  275. [AB8500_EXT_SUPPLY3] = {
  276. .desc = {
  277. .name = "VEXTSUPPLY3",
  278. .ops = &ab8500_ext_regulator_ops,
  279. .type = REGULATOR_VOLTAGE,
  280. .id = AB8500_EXT_SUPPLY3,
  281. .owner = THIS_MODULE,
  282. .n_voltages = 1,
  283. },
  284. .update_bank = 0x04,
  285. .update_reg = 0x08,
  286. .update_mask = 0x30,
  287. .update_val = 0x10,
  288. .update_val_hp = 0x10,
  289. .update_val_lp = 0x30,
  290. .update_val_hw = 0x20,
  291. },
  292. };
  293. int ab8500_ext_regulator_init(struct platform_device *pdev)
  294. {
  295. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  296. struct ab8500_platform_data *ppdata;
  297. struct ab8500_regulator_platform_data *pdata;
  298. struct regulator_config config = { };
  299. int i, err;
  300. if (!ab8500) {
  301. dev_err(&pdev->dev, "null mfd parent\n");
  302. return -EINVAL;
  303. }
  304. ppdata = dev_get_platdata(ab8500->dev);
  305. if (!ppdata) {
  306. dev_err(&pdev->dev, "null parent pdata\n");
  307. return -EINVAL;
  308. }
  309. pdata = ppdata->regulator;
  310. if (!pdata) {
  311. dev_err(&pdev->dev, "null pdata\n");
  312. return -EINVAL;
  313. }
  314. /* make sure the platform data has the correct size */
  315. if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
  316. dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
  317. return -EINVAL;
  318. }
  319. /* check for AB8500 2.x */
  320. if (abx500_get_chip_id(&pdev->dev) < 0x30) {
  321. struct ab8500_ext_regulator_info *info;
  322. /* VextSupply3LPn is inverted on AB8500 2.x */
  323. info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
  324. info->update_val = 0x30;
  325. info->update_val_hp = 0x30;
  326. info->update_val_lp = 0x10;
  327. }
  328. /* register all regulators */
  329. for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
  330. struct ab8500_ext_regulator_info *info = NULL;
  331. /* assign per-regulator data */
  332. info = &ab8500_ext_regulator_info[i];
  333. info->dev = &pdev->dev;
  334. info->cfg = (struct ab8500_ext_regulator_cfg *)
  335. pdata->ext_regulator[i].driver_data;
  336. config.dev = &pdev->dev;
  337. config.init_data = &pdata->ext_regulator[i];
  338. config.driver_data = info;
  339. /* register regulator with framework */
  340. info->rdev = regulator_register(&info->desc, &config);
  341. if (IS_ERR(info->rdev)) {
  342. err = PTR_ERR(info->rdev);
  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_ext_regulator_info[i];
  348. regulator_unregister(info->rdev);
  349. }
  350. return err;
  351. }
  352. dev_dbg(rdev_get_dev(info->rdev),
  353. "%s-probed\n", info->desc.name);
  354. }
  355. return 0;
  356. }
  357. int ab8500_ext_regulator_exit(struct platform_device *pdev)
  358. {
  359. int i;
  360. for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
  361. struct ab8500_ext_regulator_info *info = NULL;
  362. info = &ab8500_ext_regulator_info[i];
  363. dev_vdbg(rdev_get_dev(info->rdev),
  364. "%s-remove\n", info->desc.name);
  365. regulator_unregister(info->rdev);
  366. }
  367. return 0;
  368. }
  369. MODULE_LICENSE("GPL v2");
  370. MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
  371. MODULE_DESCRIPTION("AB8500 external regulator driver");
  372. MODULE_ALIAS("platform:ab8500-ext-regulator");