ab8500-ext.c 10.0 KB

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