ab3100.c 15 KB

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