ab8500-ext.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * Authors: Bengt Jonsson <[email protected]>
  6. *
  7. * This file is based on drivers/regulator/ab8500.c
  8. *
  9. * AB8500 external regulators
  10. *
  11. * ab8500-ext supports the following regulators:
  12. * - VextSupply3
  13. */
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/regulator/of_regulator.h>
  23. #include <linux/mfd/abx500.h>
  24. #include <linux/mfd/abx500/ab8500.h>
  25. /* AB8500 external regulators */
  26. enum ab8500_ext_regulator_id {
  27. AB8500_EXT_SUPPLY1,
  28. AB8500_EXT_SUPPLY2,
  29. AB8500_EXT_SUPPLY3,
  30. AB8500_NUM_EXT_REGULATORS,
  31. };
  32. struct ab8500_ext_regulator_cfg {
  33. bool hwreq; /* requires hw mode or high power mode */
  34. };
  35. /* supply for VextSupply3 */
  36. static struct regulator_consumer_supply ab8500_ext_supply3_consumers[] = {
  37. /* SIM supply for 3 V SIM cards */
  38. REGULATOR_SUPPLY("vinvsim", "sim-detect.0"),
  39. };
  40. /*
  41. * AB8500 external regulators
  42. */
  43. static struct regulator_init_data ab8500_ext_regulators[] = {
  44. /* fixed Vbat supplies VSMPS1_EXT_1V8 */
  45. [AB8500_EXT_SUPPLY1] = {
  46. .constraints = {
  47. .name = "ab8500-ext-supply1",
  48. .min_uV = 1800000,
  49. .max_uV = 1800000,
  50. .initial_mode = REGULATOR_MODE_IDLE,
  51. .boot_on = 1,
  52. .always_on = 1,
  53. },
  54. },
  55. /* fixed Vbat supplies VSMPS2_EXT_1V36 and VSMPS5_EXT_1V15 */
  56. [AB8500_EXT_SUPPLY2] = {
  57. .constraints = {
  58. .name = "ab8500-ext-supply2",
  59. .min_uV = 1360000,
  60. .max_uV = 1360000,
  61. },
  62. },
  63. /* fixed Vbat supplies VSMPS3_EXT_3V4 and VSMPS4_EXT_3V4 */
  64. [AB8500_EXT_SUPPLY3] = {
  65. .constraints = {
  66. .name = "ab8500-ext-supply3",
  67. .min_uV = 3400000,
  68. .max_uV = 3400000,
  69. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  70. .boot_on = 1,
  71. },
  72. .num_consumer_supplies =
  73. ARRAY_SIZE(ab8500_ext_supply3_consumers),
  74. .consumer_supplies = ab8500_ext_supply3_consumers,
  75. },
  76. };
  77. /**
  78. * struct ab8500_ext_regulator_info - ab8500 regulator information
  79. * @dev: device pointer
  80. * @desc: regulator description
  81. * @cfg: regulator configuration (extension of regulator FW configuration)
  82. * @update_bank: bank to control on/off
  83. * @update_reg: register to control on/off
  84. * @update_mask: mask to enable/disable and set mode of regulator
  85. * @update_val: bits holding the regulator current mode
  86. * @update_val_hp: bits to set EN pin active (LPn pin deactive)
  87. * normally this means high power mode
  88. * @update_val_lp: bits to set EN pin active and LPn pin active
  89. * normally this means low power mode
  90. * @update_val_hw: bits to set regulator pins in HW control
  91. * SysClkReq pins and logic will choose mode
  92. */
  93. struct ab8500_ext_regulator_info {
  94. struct device *dev;
  95. struct regulator_desc desc;
  96. struct ab8500_ext_regulator_cfg *cfg;
  97. u8 update_bank;
  98. u8 update_reg;
  99. u8 update_mask;
  100. u8 update_val;
  101. u8 update_val_hp;
  102. u8 update_val_lp;
  103. u8 update_val_hw;
  104. };
  105. static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
  106. {
  107. int ret;
  108. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  109. u8 regval;
  110. if (info == NULL) {
  111. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  112. return -EINVAL;
  113. }
  114. /*
  115. * To satisfy both HW high power request and SW request, the regulator
  116. * must be on in high power.
  117. */
  118. if (info->cfg && info->cfg->hwreq)
  119. regval = info->update_val_hp;
  120. else
  121. regval = info->update_val;
  122. ret = abx500_mask_and_set_register_interruptible(info->dev,
  123. info->update_bank, info->update_reg,
  124. info->update_mask, regval);
  125. if (ret < 0) {
  126. dev_err(rdev_get_dev(rdev),
  127. "couldn't set enable bits for regulator\n");
  128. return ret;
  129. }
  130. dev_dbg(rdev_get_dev(rdev),
  131. "%s-enable (bank, reg, mask, value): 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
  132. info->desc.name, info->update_bank, info->update_reg,
  133. info->update_mask, regval);
  134. return 0;
  135. }
  136. static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
  137. {
  138. int ret;
  139. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  140. u8 regval;
  141. if (info == NULL) {
  142. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  143. return -EINVAL;
  144. }
  145. /*
  146. * Set the regulator in HW request mode if configured
  147. */
  148. if (info->cfg && info->cfg->hwreq)
  149. regval = info->update_val_hw;
  150. else
  151. regval = 0;
  152. ret = abx500_mask_and_set_register_interruptible(info->dev,
  153. info->update_bank, info->update_reg,
  154. info->update_mask, regval);
  155. if (ret < 0) {
  156. dev_err(rdev_get_dev(rdev),
  157. "couldn't set disable bits for regulator\n");
  158. return ret;
  159. }
  160. dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
  161. " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
  162. info->desc.name, info->update_bank, info->update_reg,
  163. info->update_mask, regval);
  164. return 0;
  165. }
  166. static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
  167. {
  168. int ret;
  169. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  170. u8 regval;
  171. if (info == NULL) {
  172. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  173. return -EINVAL;
  174. }
  175. ret = abx500_get_register_interruptible(info->dev,
  176. info->update_bank, info->update_reg, &regval);
  177. if (ret < 0) {
  178. dev_err(rdev_get_dev(rdev),
  179. "couldn't read 0x%x register\n", info->update_reg);
  180. return ret;
  181. }
  182. dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
  183. " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
  184. info->desc.name, info->update_bank, info->update_reg,
  185. info->update_mask, regval);
  186. if (((regval & info->update_mask) == info->update_val_lp) ||
  187. ((regval & info->update_mask) == info->update_val_hp))
  188. return 1;
  189. else
  190. return 0;
  191. }
  192. static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
  193. unsigned int mode)
  194. {
  195. int ret = 0;
  196. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  197. u8 regval;
  198. if (info == NULL) {
  199. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  200. return -EINVAL;
  201. }
  202. switch (mode) {
  203. case REGULATOR_MODE_NORMAL:
  204. regval = info->update_val_hp;
  205. break;
  206. case REGULATOR_MODE_IDLE:
  207. regval = info->update_val_lp;
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. /* If regulator is enabled and info->cfg->hwreq is set, the regulator
  213. must be on in high power, so we don't need to write the register with
  214. the same value.
  215. */
  216. if (ab8500_ext_regulator_is_enabled(rdev) &&
  217. !(info->cfg && info->cfg->hwreq)) {
  218. ret = abx500_mask_and_set_register_interruptible(info->dev,
  219. info->update_bank, info->update_reg,
  220. info->update_mask, regval);
  221. if (ret < 0) {
  222. dev_err(rdev_get_dev(rdev),
  223. "Could not set regulator mode.\n");
  224. return ret;
  225. }
  226. dev_dbg(rdev_get_dev(rdev),
  227. "%s-set_mode (bank, reg, mask, value): "
  228. "0x%x, 0x%x, 0x%x, 0x%x\n",
  229. info->desc.name, info->update_bank, info->update_reg,
  230. info->update_mask, regval);
  231. }
  232. info->update_val = regval;
  233. return 0;
  234. }
  235. static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
  236. {
  237. struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
  238. int ret;
  239. if (info == NULL) {
  240. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  241. return -EINVAL;
  242. }
  243. if (info->update_val == info->update_val_hp)
  244. ret = REGULATOR_MODE_NORMAL;
  245. else if (info->update_val == info->update_val_lp)
  246. ret = REGULATOR_MODE_IDLE;
  247. else
  248. ret = -EINVAL;
  249. return ret;
  250. }
  251. static int ab8500_ext_set_voltage(struct regulator_dev *rdev, int min_uV,
  252. int max_uV, unsigned *selector)
  253. {
  254. struct regulation_constraints *regu_constraints = rdev->constraints;
  255. if (!regu_constraints) {
  256. dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
  257. return -EINVAL;
  258. }
  259. if (regu_constraints->min_uV == min_uV &&
  260. regu_constraints->max_uV == max_uV)
  261. return 0;
  262. dev_err(rdev_get_dev(rdev),
  263. "Requested min %duV max %duV != constrained min %duV max %duV\n",
  264. min_uV, max_uV,
  265. regu_constraints->min_uV, regu_constraints->max_uV);
  266. return -EINVAL;
  267. }
  268. static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
  269. unsigned selector)
  270. {
  271. struct regulation_constraints *regu_constraints = rdev->constraints;
  272. if (regu_constraints == NULL) {
  273. dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
  274. return -EINVAL;
  275. }
  276. /* return the uV for the fixed regulators */
  277. if (regu_constraints->min_uV && regu_constraints->max_uV) {
  278. if (regu_constraints->min_uV == regu_constraints->max_uV)
  279. return regu_constraints->min_uV;
  280. }
  281. return -EINVAL;
  282. }
  283. static const struct regulator_ops ab8500_ext_regulator_ops = {
  284. .enable = ab8500_ext_regulator_enable,
  285. .disable = ab8500_ext_regulator_disable,
  286. .is_enabled = ab8500_ext_regulator_is_enabled,
  287. .set_mode = ab8500_ext_regulator_set_mode,
  288. .get_mode = ab8500_ext_regulator_get_mode,
  289. .set_voltage = ab8500_ext_set_voltage,
  290. .list_voltage = ab8500_ext_list_voltage,
  291. };
  292. static struct ab8500_ext_regulator_info
  293. ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
  294. [AB8500_EXT_SUPPLY1] = {
  295. .desc = {
  296. .name = "VEXTSUPPLY1",
  297. .of_match = of_match_ptr("ab8500_ext1"),
  298. .ops = &ab8500_ext_regulator_ops,
  299. .type = REGULATOR_VOLTAGE,
  300. .id = AB8500_EXT_SUPPLY1,
  301. .owner = THIS_MODULE,
  302. .n_voltages = 1,
  303. },
  304. .update_bank = 0x04,
  305. .update_reg = 0x08,
  306. .update_mask = 0x03,
  307. .update_val = 0x01,
  308. .update_val_hp = 0x01,
  309. .update_val_lp = 0x03,
  310. .update_val_hw = 0x02,
  311. },
  312. [AB8500_EXT_SUPPLY2] = {
  313. .desc = {
  314. .name = "VEXTSUPPLY2",
  315. .of_match = of_match_ptr("ab8500_ext2"),
  316. .ops = &ab8500_ext_regulator_ops,
  317. .type = REGULATOR_VOLTAGE,
  318. .id = AB8500_EXT_SUPPLY2,
  319. .owner = THIS_MODULE,
  320. .n_voltages = 1,
  321. },
  322. .update_bank = 0x04,
  323. .update_reg = 0x08,
  324. .update_mask = 0x0c,
  325. .update_val = 0x04,
  326. .update_val_hp = 0x04,
  327. .update_val_lp = 0x0c,
  328. .update_val_hw = 0x08,
  329. },
  330. [AB8500_EXT_SUPPLY3] = {
  331. .desc = {
  332. .name = "VEXTSUPPLY3",
  333. .of_match = of_match_ptr("ab8500_ext3"),
  334. .ops = &ab8500_ext_regulator_ops,
  335. .type = REGULATOR_VOLTAGE,
  336. .id = AB8500_EXT_SUPPLY3,
  337. .owner = THIS_MODULE,
  338. .n_voltages = 1,
  339. },
  340. .update_bank = 0x04,
  341. .update_reg = 0x08,
  342. .update_mask = 0x30,
  343. .update_val = 0x10,
  344. .update_val_hp = 0x10,
  345. .update_val_lp = 0x30,
  346. .update_val_hw = 0x20,
  347. },
  348. };
  349. static int ab8500_ext_regulator_probe(struct platform_device *pdev)
  350. {
  351. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  352. struct regulator_config config = { };
  353. struct regulator_dev *rdev;
  354. int i;
  355. if (!ab8500) {
  356. dev_err(&pdev->dev, "null mfd parent\n");
  357. return -EINVAL;
  358. }
  359. /* check for AB8500 2.x */
  360. if (is_ab8500_2p0_or_earlier(ab8500)) {
  361. struct ab8500_ext_regulator_info *info;
  362. /* VextSupply3LPn is inverted on AB8500 2.x */
  363. info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
  364. info->update_val = 0x30;
  365. info->update_val_hp = 0x30;
  366. info->update_val_lp = 0x10;
  367. }
  368. /* register all regulators */
  369. for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
  370. struct ab8500_ext_regulator_info *info = NULL;
  371. /* assign per-regulator data */
  372. info = &ab8500_ext_regulator_info[i];
  373. info->dev = &pdev->dev;
  374. info->cfg = (struct ab8500_ext_regulator_cfg *)
  375. ab8500_ext_regulators[i].driver_data;
  376. config.dev = &pdev->dev;
  377. config.driver_data = info;
  378. config.init_data = &ab8500_ext_regulators[i];
  379. /* register regulator with framework */
  380. rdev = devm_regulator_register(&pdev->dev, &info->desc,
  381. &config);
  382. if (IS_ERR(rdev)) {
  383. dev_err(&pdev->dev, "failed to register regulator %s\n",
  384. info->desc.name);
  385. return PTR_ERR(rdev);
  386. }
  387. dev_dbg(&pdev->dev, "%s-probed\n", info->desc.name);
  388. }
  389. return 0;
  390. }
  391. static struct platform_driver ab8500_ext_regulator_driver = {
  392. .probe = ab8500_ext_regulator_probe,
  393. .driver = {
  394. .name = "ab8500-ext-regulator",
  395. },
  396. };
  397. static int __init ab8500_ext_regulator_init(void)
  398. {
  399. int ret;
  400. ret = platform_driver_register(&ab8500_ext_regulator_driver);
  401. if (ret)
  402. pr_err("Failed to register ab8500 ext regulator: %d\n", ret);
  403. return ret;
  404. }
  405. subsys_initcall(ab8500_ext_regulator_init);
  406. static void __exit ab8500_ext_regulator_exit(void)
  407. {
  408. platform_driver_unregister(&ab8500_ext_regulator_driver);
  409. }
  410. module_exit(ab8500_ext_regulator_exit);
  411. MODULE_LICENSE("GPL v2");
  412. MODULE_AUTHOR("Bengt Jonsson <[email protected]>");
  413. MODULE_DESCRIPTION("AB8500 external regulator driver");
  414. MODULE_ALIAS("platform:ab8500-ext-regulator");