ab3100.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * drivers/regulator/ab3100.c
  3. *
  4. * Copyright (C) 2008-2009 ST-Ericsson AB
  5. * License terms: GNU General Public License (GPL) version 2
  6. * Low-level control of the AB3100 IC Low Dropout (LDO)
  7. * regulators, external regulator and buck converter
  8. * Author: Mattias Wallin <mattias.wallin@stericsson.com>
  9. * Author: Linus Walleij <linus.walleij@stericsson.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/err.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/mfd/ab3100.h>
  18. #include <linux/mfd/abx500.h>
  19. /* LDO registers and some handy masking definitions for AB3100 */
  20. #define AB3100_LDO_A 0x40
  21. #define AB3100_LDO_C 0x41
  22. #define AB3100_LDO_D 0x42
  23. #define AB3100_LDO_E 0x43
  24. #define AB3100_LDO_E_SLEEP 0x44
  25. #define AB3100_LDO_F 0x45
  26. #define AB3100_LDO_G 0x46
  27. #define AB3100_LDO_H 0x47
  28. #define AB3100_LDO_H_SLEEP_MODE 0
  29. #define AB3100_LDO_H_SLEEP_EN 2
  30. #define AB3100_LDO_ON 4
  31. #define AB3100_LDO_H_VSEL_AC 5
  32. #define AB3100_LDO_K 0x48
  33. #define AB3100_LDO_EXT 0x49
  34. #define AB3100_BUCK 0x4A
  35. #define AB3100_BUCK_SLEEP 0x4B
  36. #define AB3100_REG_ON_MASK 0x10
  37. /**
  38. * struct ab3100_regulator
  39. * A struct passed around the individual regulator functions
  40. * @platform_device: platform device holding this regulator
  41. * @dev: handle to the device
  42. * @plfdata: AB3100 platform data passed in at probe time
  43. * @regreg: regulator register number in the AB3100
  44. */
  45. struct ab3100_regulator {
  46. struct regulator_dev *rdev;
  47. struct device *dev;
  48. struct ab3100_platform_data *plfdata;
  49. u8 regreg;
  50. };
  51. /* The order in which registers are initialized */
  52. static const u8 ab3100_reg_init_order[AB3100_NUM_REGULATORS+2] = {
  53. AB3100_LDO_A,
  54. AB3100_LDO_C,
  55. AB3100_LDO_E,
  56. AB3100_LDO_E_SLEEP,
  57. AB3100_LDO_F,
  58. AB3100_LDO_G,
  59. AB3100_LDO_H,
  60. AB3100_LDO_K,
  61. AB3100_LDO_EXT,
  62. AB3100_BUCK,
  63. AB3100_BUCK_SLEEP,
  64. AB3100_LDO_D,
  65. };
  66. /* Preset (hardware defined) voltages for these regulators */
  67. #define LDO_A_VOLTAGE 2750000
  68. #define LDO_C_VOLTAGE 2650000
  69. #define LDO_D_VOLTAGE 2650000
  70. static const unsigned int ldo_e_buck_typ_voltages[] = {
  71. 1800000,
  72. 1400000,
  73. 1300000,
  74. 1200000,
  75. 1100000,
  76. 1050000,
  77. 900000,
  78. };
  79. static const unsigned int ldo_f_typ_voltages[] = {
  80. 1800000,
  81. 1400000,
  82. 1300000,
  83. 1200000,
  84. 1100000,
  85. 1050000,
  86. 2500000,
  87. 2650000,
  88. };
  89. static const unsigned int ldo_g_typ_voltages[] = {
  90. 2850000,
  91. 2750000,
  92. 1800000,
  93. 1500000,
  94. };
  95. static const unsigned int ldo_h_typ_voltages[] = {
  96. 2750000,
  97. 1800000,
  98. 1500000,
  99. 1200000,
  100. };
  101. static const unsigned int ldo_k_typ_voltages[] = {
  102. 2750000,
  103. 1800000,
  104. };
  105. /* The regulator devices */
  106. static struct ab3100_regulator
  107. ab3100_regulators[AB3100_NUM_REGULATORS] = {
  108. {
  109. .regreg = AB3100_LDO_A,
  110. },
  111. {
  112. .regreg = AB3100_LDO_C,
  113. },
  114. {
  115. .regreg = AB3100_LDO_D,
  116. },
  117. {
  118. .regreg = AB3100_LDO_E,
  119. },
  120. {
  121. .regreg = AB3100_LDO_F,
  122. },
  123. {
  124. .regreg = AB3100_LDO_G,
  125. },
  126. {
  127. .regreg = AB3100_LDO_H,
  128. },
  129. {
  130. .regreg = AB3100_LDO_K,
  131. },
  132. {
  133. .regreg = AB3100_LDO_EXT,
  134. /* No voltages for the external regulator */
  135. },
  136. {
  137. .regreg = AB3100_BUCK,
  138. },
  139. };
  140. /*
  141. * General functions for enable, disable and is_enabled used for
  142. * LDO: A,C,E,F,G,H,K,EXT and BUCK
  143. */
  144. static int ab3100_enable_regulator(struct regulator_dev *reg)
  145. {
  146. struct ab3100_regulator *abreg = rdev_get_drvdata(reg);
  147. int err;
  148. u8 regval;
  149. err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg,
  150. &regval);
  151. if (err) {
  152. dev_warn(&reg->dev, "failed to get regid %d value\n",
  153. abreg->regreg);
  154. return err;
  155. }
  156. /* The regulator is already on, no reason to go further */
  157. if (regval & AB3100_REG_ON_MASK)
  158. return 0;
  159. regval |= AB3100_REG_ON_MASK;
  160. err = abx500_set_register_interruptible(abreg->dev, 0, abreg->regreg,
  161. regval);
  162. if (err) {
  163. dev_warn(&reg->dev, "failed to set regid %d value\n",
  164. abreg->regreg);
  165. return err;
  166. }
  167. return 0;
  168. }
  169. static int ab3100_disable_regulator(struct regulator_dev *reg)
  170. {
  171. struct ab3100_regulator *abreg = rdev_get_drvdata(reg);
  172. int err;
  173. u8 regval;
  174. /*
  175. * LDO D is a special regulator. When it is disabled, the entire
  176. * system is shut down. So this is handled specially.
  177. */
  178. pr_info("Called ab3100_disable_regulator\n");
  179. if (abreg->regreg == AB3100_LDO_D) {
  180. dev_info(&reg->dev, "disabling LDO D - shut down system\n");
  181. /* Setting LDO D to 0x00 cuts the power to the SoC */
  182. return abx500_set_register_interruptible(abreg->dev, 0,
  183. AB3100_LDO_D, 0x00U);
  184. }
  185. /*
  186. * All other regulators are handled here
  187. */
  188. err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg,
  189. &regval);
  190. if (err) {
  191. dev_err(&reg->dev, "unable to get register 0x%x\n",
  192. abreg->regreg);
  193. return err;
  194. }
  195. regval &= ~AB3100_REG_ON_MASK;
  196. return abx500_set_register_interruptible(abreg->dev, 0, abreg->regreg,
  197. regval);
  198. }
  199. static int ab3100_is_enabled_regulator(struct regulator_dev *reg)
  200. {
  201. struct ab3100_regulator *abreg = rdev_get_drvdata(reg);
  202. u8 regval;
  203. int err;
  204. err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg,
  205. &regval);
  206. if (err) {
  207. dev_err(&reg->dev, "unable to get register 0x%x\n",
  208. abreg->regreg);
  209. return err;
  210. }
  211. return regval & AB3100_REG_ON_MASK;
  212. }
  213. static int ab3100_get_voltage_regulator(struct regulator_dev *reg)
  214. {
  215. struct ab3100_regulator *abreg = rdev_get_drvdata(reg);
  216. u8 regval;
  217. int err;
  218. /*
  219. * For variable types, read out setting and index into
  220. * supplied voltage list.
  221. */
  222. err = abx500_get_register_interruptible(abreg->dev, 0,
  223. abreg->regreg, &regval);
  224. if (err) {
  225. dev_warn(&reg->dev,
  226. "failed to get regulator value in register %02x\n",
  227. abreg->regreg);
  228. return err;
  229. }
  230. /* The 3 highest bits index voltages */
  231. regval &= 0xE0;
  232. regval >>= 5;
  233. if (regval >= reg->desc->n_voltages) {
  234. dev_err(&reg->dev,
  235. "regulator register %02x contains an illegal voltage setting\n",
  236. abreg->regreg);
  237. return -EINVAL;
  238. }
  239. return reg->desc->volt_table[regval];
  240. }
  241. static int ab3100_set_voltage_regulator_sel(struct regulator_dev *reg,
  242. unsigned selector)
  243. {
  244. struct ab3100_regulator *abreg = rdev_get_drvdata(reg);
  245. u8 regval;
  246. int err;
  247. err = abx500_get_register_interruptible(abreg->dev, 0,
  248. abreg->regreg, &regval);
  249. if (err) {
  250. dev_warn(&reg->dev,
  251. "failed to get regulator register %02x\n",
  252. abreg->regreg);
  253. return err;
  254. }
  255. /* The highest three bits control the variable regulators */
  256. regval &= ~0xE0;
  257. regval |= (selector << 5);
  258. err = abx500_set_register_interruptible(abreg->dev, 0,
  259. abreg->regreg, regval);
  260. if (err)
  261. dev_warn(&reg->dev, "failed to set regulator register %02x\n",
  262. abreg->regreg);
  263. return err;
  264. }
  265. static int ab3100_set_suspend_voltage_regulator(struct regulator_dev *reg,
  266. int uV)
  267. {
  268. struct ab3100_regulator *abreg = rdev_get_drvdata(reg);
  269. u8 regval;
  270. int err;
  271. int bestindex;
  272. u8 targetreg;
  273. if (abreg->regreg == AB3100_LDO_E)
  274. targetreg = AB3100_LDO_E_SLEEP;
  275. else if (abreg->regreg == AB3100_BUCK)
  276. targetreg = AB3100_BUCK_SLEEP;
  277. else
  278. return -EINVAL;
  279. /* LDO E and BUCK have special suspend voltages you can set */
  280. bestindex = regulator_map_voltage_iterate(reg, uV, uV);
  281. err = abx500_get_register_interruptible(abreg->dev, 0,
  282. targetreg, &regval);
  283. if (err) {
  284. dev_warn(&reg->dev,
  285. "failed to get regulator register %02x\n",
  286. targetreg);
  287. return err;
  288. }
  289. /* The highest three bits control the variable regulators */
  290. regval &= ~0xE0;
  291. regval |= (bestindex << 5);
  292. err = abx500_set_register_interruptible(abreg->dev, 0,
  293. targetreg, regval);
  294. if (err)
  295. dev_warn(&reg->dev, "failed to set regulator register %02x\n",
  296. abreg->regreg);
  297. return err;
  298. }
  299. /*
  300. * The external regulator can just define a fixed voltage.
  301. */
  302. static int ab3100_get_voltage_regulator_external(struct regulator_dev *reg)
  303. {
  304. struct ab3100_regulator *abreg = rdev_get_drvdata(reg);
  305. return abreg->plfdata->external_voltage;
  306. }
  307. static struct regulator_ops regulator_ops_fixed = {
  308. .list_voltage = regulator_list_voltage_linear,
  309. .enable = ab3100_enable_regulator,
  310. .disable = ab3100_disable_regulator,
  311. .is_enabled = ab3100_is_enabled_regulator,
  312. };
  313. static struct regulator_ops regulator_ops_variable = {
  314. .enable = ab3100_enable_regulator,
  315. .disable = ab3100_disable_regulator,
  316. .is_enabled = ab3100_is_enabled_regulator,
  317. .get_voltage = ab3100_get_voltage_regulator,
  318. .set_voltage_sel = ab3100_set_voltage_regulator_sel,
  319. .list_voltage = regulator_list_voltage_table,
  320. };
  321. static struct regulator_ops regulator_ops_variable_sleepable = {
  322. .enable = ab3100_enable_regulator,
  323. .disable = ab3100_disable_regulator,
  324. .is_enabled = ab3100_is_enabled_regulator,
  325. .get_voltage = ab3100_get_voltage_regulator,
  326. .set_voltage_sel = ab3100_set_voltage_regulator_sel,
  327. .set_suspend_voltage = ab3100_set_suspend_voltage_regulator,
  328. .list_voltage = regulator_list_voltage_table,
  329. };
  330. /*
  331. * LDO EXT is an external regulator so it is really
  332. * not possible to set any voltage locally here, AB3100
  333. * is an on/off switch plain an simple. The external
  334. * voltage is defined in the board set-up if any.
  335. */
  336. static struct regulator_ops regulator_ops_external = {
  337. .enable = ab3100_enable_regulator,
  338. .disable = ab3100_disable_regulator,
  339. .is_enabled = ab3100_is_enabled_regulator,
  340. .get_voltage = ab3100_get_voltage_regulator_external,
  341. };
  342. static struct regulator_desc
  343. ab3100_regulator_desc[AB3100_NUM_REGULATORS] = {
  344. {
  345. .name = "LDO_A",
  346. .id = AB3100_LDO_A,
  347. .ops = &regulator_ops_fixed,
  348. .n_voltages = 1,
  349. .type = REGULATOR_VOLTAGE,
  350. .owner = THIS_MODULE,
  351. .min_uV = LDO_A_VOLTAGE,
  352. .enable_time = 200,
  353. },
  354. {
  355. .name = "LDO_C",
  356. .id = AB3100_LDO_C,
  357. .ops = &regulator_ops_fixed,
  358. .n_voltages = 1,
  359. .type = REGULATOR_VOLTAGE,
  360. .owner = THIS_MODULE,
  361. .min_uV = LDO_C_VOLTAGE,
  362. .enable_time = 200,
  363. },
  364. {
  365. .name = "LDO_D",
  366. .id = AB3100_LDO_D,
  367. .ops = &regulator_ops_fixed,
  368. .n_voltages = 1,
  369. .type = REGULATOR_VOLTAGE,
  370. .owner = THIS_MODULE,
  371. .min_uV = LDO_D_VOLTAGE,
  372. .enable_time = 200,
  373. },
  374. {
  375. .name = "LDO_E",
  376. .id = AB3100_LDO_E,
  377. .ops = &regulator_ops_variable_sleepable,
  378. .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages),
  379. .volt_table = ldo_e_buck_typ_voltages,
  380. .type = REGULATOR_VOLTAGE,
  381. .owner = THIS_MODULE,
  382. .enable_time = 200,
  383. },
  384. {
  385. .name = "LDO_F",
  386. .id = AB3100_LDO_F,
  387. .ops = &regulator_ops_variable,
  388. .n_voltages = ARRAY_SIZE(ldo_f_typ_voltages),
  389. .volt_table = ldo_f_typ_voltages,
  390. .type = REGULATOR_VOLTAGE,
  391. .owner = THIS_MODULE,
  392. .enable_time = 600,
  393. },
  394. {
  395. .name = "LDO_G",
  396. .id = AB3100_LDO_G,
  397. .ops = &regulator_ops_variable,
  398. .n_voltages = ARRAY_SIZE(ldo_g_typ_voltages),
  399. .volt_table = ldo_g_typ_voltages,
  400. .type = REGULATOR_VOLTAGE,
  401. .owner = THIS_MODULE,
  402. .enable_time = 400,
  403. },
  404. {
  405. .name = "LDO_H",
  406. .id = AB3100_LDO_H,
  407. .ops = &regulator_ops_variable,
  408. .n_voltages = ARRAY_SIZE(ldo_h_typ_voltages),
  409. .volt_table = ldo_h_typ_voltages,
  410. .type = REGULATOR_VOLTAGE,
  411. .owner = THIS_MODULE,
  412. .enable_time = 200,
  413. },
  414. {
  415. .name = "LDO_K",
  416. .id = AB3100_LDO_K,
  417. .ops = &regulator_ops_variable,
  418. .n_voltages = ARRAY_SIZE(ldo_k_typ_voltages),
  419. .volt_table = ldo_k_typ_voltages,
  420. .type = REGULATOR_VOLTAGE,
  421. .owner = THIS_MODULE,
  422. .enable_time = 200,
  423. },
  424. {
  425. .name = "LDO_EXT",
  426. .id = AB3100_LDO_EXT,
  427. .ops = &regulator_ops_external,
  428. .type = REGULATOR_VOLTAGE,
  429. .owner = THIS_MODULE,
  430. },
  431. {
  432. .name = "BUCK",
  433. .id = AB3100_BUCK,
  434. .ops = &regulator_ops_variable_sleepable,
  435. .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages),
  436. .volt_table = ldo_e_buck_typ_voltages,
  437. .type = REGULATOR_VOLTAGE,
  438. .owner = THIS_MODULE,
  439. .enable_time = 1000,
  440. },
  441. };
  442. /*
  443. * NOTE: the following functions are regulators pluralis - it is the
  444. * binding to the AB3100 core driver and the parent platform device
  445. * for all the different regulators.
  446. */
  447. static int ab3100_regulators_probe(struct platform_device *pdev)
  448. {
  449. struct ab3100_platform_data *plfdata = pdev->dev.platform_data;
  450. struct regulator_config config = { };
  451. int err = 0;
  452. u8 data;
  453. int i;
  454. /* Check chip state */
  455. err = abx500_get_register_interruptible(&pdev->dev, 0,
  456. AB3100_LDO_D, &data);
  457. if (err) {
  458. dev_err(&pdev->dev, "could not read initial status of LDO_D\n");
  459. return err;
  460. }
  461. if (data & 0x10)
  462. dev_notice(&pdev->dev,
  463. "chip is already in active mode (Warm start)\n");
  464. else
  465. dev_notice(&pdev->dev,
  466. "chip is in inactive mode (Cold start)\n");
  467. /* Set up regulators */
  468. for (i = 0; i < ARRAY_SIZE(ab3100_reg_init_order); i++) {
  469. err = abx500_set_register_interruptible(&pdev->dev, 0,
  470. ab3100_reg_init_order[i],
  471. plfdata->reg_initvals[i]);
  472. if (err) {
  473. dev_err(&pdev->dev, "regulator initialization failed with error %d\n",
  474. err);
  475. return err;
  476. }
  477. }
  478. /* Register the regulators */
  479. for (i = 0; i < AB3100_NUM_REGULATORS; i++) {
  480. struct ab3100_regulator *reg = &ab3100_regulators[i];
  481. struct regulator_dev *rdev;
  482. /*
  483. * Initialize per-regulator struct.
  484. * Inherit platform data, this comes down from the
  485. * i2c boarddata, from the machine. So if you want to
  486. * see what it looks like for a certain machine, go
  487. * into the machine I2C setup.
  488. */
  489. reg->dev = &pdev->dev;
  490. reg->plfdata = plfdata;
  491. config.dev = &pdev->dev;
  492. config.driver_data = reg;
  493. config.init_data = &plfdata->reg_constraints[i];
  494. /*
  495. * Register the regulator, pass around
  496. * the ab3100_regulator struct
  497. */
  498. rdev = regulator_register(&ab3100_regulator_desc[i], &config);
  499. if (IS_ERR(rdev)) {
  500. err = PTR_ERR(rdev);
  501. dev_err(&pdev->dev,
  502. "%s: failed to register regulator %s err %d\n",
  503. __func__, ab3100_regulator_desc[i].name,
  504. err);
  505. /* remove the already registered regulators */
  506. while (--i >= 0)
  507. regulator_unregister(ab3100_regulators[i].rdev);
  508. return err;
  509. }
  510. /* Then set a pointer back to the registered regulator */
  511. reg->rdev = rdev;
  512. }
  513. return 0;
  514. }
  515. static int ab3100_regulators_remove(struct platform_device *pdev)
  516. {
  517. int i;
  518. for (i = 0; i < AB3100_NUM_REGULATORS; i++) {
  519. struct ab3100_regulator *reg = &ab3100_regulators[i];
  520. regulator_unregister(reg->rdev);
  521. }
  522. return 0;
  523. }
  524. static struct platform_driver ab3100_regulators_driver = {
  525. .driver = {
  526. .name = "ab3100-regulators",
  527. .owner = THIS_MODULE,
  528. },
  529. .probe = ab3100_regulators_probe,
  530. .remove = ab3100_regulators_remove,
  531. };
  532. static __init int ab3100_regulators_init(void)
  533. {
  534. return platform_driver_register(&ab3100_regulators_driver);
  535. }
  536. static __exit void ab3100_regulators_exit(void)
  537. {
  538. platform_driver_unregister(&ab3100_regulators_driver);
  539. }
  540. subsys_initcall(ab3100_regulators_init);
  541. module_exit(ab3100_regulators_exit);
  542. MODULE_AUTHOR("Mattias Wallin <mattias.wallin@stericsson.com>");
  543. MODULE_DESCRIPTION("AB3100 Regulator driver");
  544. MODULE_LICENSE("GPL");
  545. MODULE_ALIAS("platform:ab3100-regulators");