ab3100.c 15 KB

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