ab3100.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 struct regulator_ops regulator_ops_fixed = {
  307. .list_voltage = regulator_list_voltage_linear,
  308. .enable = ab3100_enable_regulator,
  309. .disable = ab3100_disable_regulator,
  310. .is_enabled = ab3100_is_enabled_regulator,
  311. };
  312. static struct regulator_ops regulator_ops_variable = {
  313. .enable = ab3100_enable_regulator,
  314. .disable = ab3100_disable_regulator,
  315. .is_enabled = ab3100_is_enabled_regulator,
  316. .get_voltage = ab3100_get_voltage_regulator,
  317. .set_voltage_sel = ab3100_set_voltage_regulator_sel,
  318. .list_voltage = regulator_list_voltage_table,
  319. };
  320. static struct regulator_ops regulator_ops_variable_sleepable = {
  321. .enable = ab3100_enable_regulator,
  322. .disable = ab3100_disable_regulator,
  323. .is_enabled = ab3100_is_enabled_regulator,
  324. .get_voltage = ab3100_get_voltage_regulator,
  325. .set_voltage_sel = ab3100_set_voltage_regulator_sel,
  326. .set_suspend_voltage = ab3100_set_suspend_voltage_regulator,
  327. .list_voltage = regulator_list_voltage_table,
  328. };
  329. /*
  330. * LDO EXT is an external regulator so it is really
  331. * not possible to set any voltage locally here, AB3100
  332. * is an on/off switch plain an simple. The external
  333. * voltage is defined in the board set-up if any.
  334. */
  335. static struct regulator_ops regulator_ops_external = {
  336. .enable = ab3100_enable_regulator,
  337. .disable = ab3100_disable_regulator,
  338. .is_enabled = ab3100_is_enabled_regulator,
  339. .get_voltage = ab3100_get_voltage_regulator_external,
  340. };
  341. static struct regulator_desc
  342. ab3100_regulator_desc[AB3100_NUM_REGULATORS] = {
  343. {
  344. .name = "LDO_A",
  345. .id = AB3100_LDO_A,
  346. .ops = &regulator_ops_fixed,
  347. .n_voltages = 1,
  348. .type = REGULATOR_VOLTAGE,
  349. .owner = THIS_MODULE,
  350. .min_uV = LDO_A_VOLTAGE,
  351. .enable_time = 200,
  352. },
  353. {
  354. .name = "LDO_C",
  355. .id = AB3100_LDO_C,
  356. .ops = &regulator_ops_fixed,
  357. .n_voltages = 1,
  358. .type = REGULATOR_VOLTAGE,
  359. .owner = THIS_MODULE,
  360. .min_uV = LDO_C_VOLTAGE,
  361. .enable_time = 200,
  362. },
  363. {
  364. .name = "LDO_D",
  365. .id = AB3100_LDO_D,
  366. .ops = &regulator_ops_fixed,
  367. .n_voltages = 1,
  368. .type = REGULATOR_VOLTAGE,
  369. .owner = THIS_MODULE,
  370. .min_uV = LDO_D_VOLTAGE,
  371. .enable_time = 200,
  372. },
  373. {
  374. .name = "LDO_E",
  375. .id = AB3100_LDO_E,
  376. .ops = &regulator_ops_variable_sleepable,
  377. .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages),
  378. .volt_table = ldo_e_buck_typ_voltages,
  379. .type = REGULATOR_VOLTAGE,
  380. .owner = THIS_MODULE,
  381. .enable_time = 200,
  382. },
  383. {
  384. .name = "LDO_F",
  385. .id = AB3100_LDO_F,
  386. .ops = &regulator_ops_variable,
  387. .n_voltages = ARRAY_SIZE(ldo_f_typ_voltages),
  388. .volt_table = ldo_f_typ_voltages,
  389. .type = REGULATOR_VOLTAGE,
  390. .owner = THIS_MODULE,
  391. .enable_time = 600,
  392. },
  393. {
  394. .name = "LDO_G",
  395. .id = AB3100_LDO_G,
  396. .ops = &regulator_ops_variable,
  397. .n_voltages = ARRAY_SIZE(ldo_g_typ_voltages),
  398. .volt_table = ldo_g_typ_voltages,
  399. .type = REGULATOR_VOLTAGE,
  400. .owner = THIS_MODULE,
  401. .enable_time = 400,
  402. },
  403. {
  404. .name = "LDO_H",
  405. .id = AB3100_LDO_H,
  406. .ops = &regulator_ops_variable,
  407. .n_voltages = ARRAY_SIZE(ldo_h_typ_voltages),
  408. .volt_table = ldo_h_typ_voltages,
  409. .type = REGULATOR_VOLTAGE,
  410. .owner = THIS_MODULE,
  411. .enable_time = 200,
  412. },
  413. {
  414. .name = "LDO_K",
  415. .id = AB3100_LDO_K,
  416. .ops = &regulator_ops_variable,
  417. .n_voltages = ARRAY_SIZE(ldo_k_typ_voltages),
  418. .volt_table = ldo_k_typ_voltages,
  419. .type = REGULATOR_VOLTAGE,
  420. .owner = THIS_MODULE,
  421. .enable_time = 200,
  422. },
  423. {
  424. .name = "LDO_EXT",
  425. .id = AB3100_LDO_EXT,
  426. .ops = &regulator_ops_external,
  427. .type = REGULATOR_VOLTAGE,
  428. .owner = THIS_MODULE,
  429. },
  430. {
  431. .name = "BUCK",
  432. .id = AB3100_BUCK,
  433. .ops = &regulator_ops_variable_sleepable,
  434. .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages),
  435. .volt_table = ldo_e_buck_typ_voltages,
  436. .type = REGULATOR_VOLTAGE,
  437. .owner = THIS_MODULE,
  438. .enable_time = 1000,
  439. },
  440. };
  441. /*
  442. * NOTE: the following functions are regulators pluralis - it is the
  443. * binding to the AB3100 core driver and the parent platform device
  444. * for all the different regulators.
  445. */
  446. static int __devinit ab3100_regulators_probe(struct platform_device *pdev)
  447. {
  448. struct ab3100_platform_data *plfdata = pdev->dev.platform_data;
  449. struct regulator_config config = { };
  450. int err = 0;
  451. u8 data;
  452. int i;
  453. /* Check chip state */
  454. err = abx500_get_register_interruptible(&pdev->dev, 0,
  455. AB3100_LDO_D, &data);
  456. if (err) {
  457. dev_err(&pdev->dev, "could not read initial status of LDO_D\n");
  458. return err;
  459. }
  460. if (data & 0x10)
  461. dev_notice(&pdev->dev,
  462. "chip is already in active mode (Warm start)\n");
  463. else
  464. dev_notice(&pdev->dev,
  465. "chip is in inactive mode (Cold start)\n");
  466. /* Set up regulators */
  467. for (i = 0; i < ARRAY_SIZE(ab3100_reg_init_order); i++) {
  468. err = abx500_set_register_interruptible(&pdev->dev, 0,
  469. ab3100_reg_init_order[i],
  470. plfdata->reg_initvals[i]);
  471. if (err) {
  472. dev_err(&pdev->dev, "regulator initialization failed with error %d\n",
  473. err);
  474. return err;
  475. }
  476. }
  477. /* Register the regulators */
  478. for (i = 0; i < AB3100_NUM_REGULATORS; i++) {
  479. struct ab3100_regulator *reg = &ab3100_regulators[i];
  480. struct regulator_dev *rdev;
  481. /*
  482. * Initialize per-regulator struct.
  483. * Inherit platform data, this comes down from the
  484. * i2c boarddata, from the machine. So if you want to
  485. * see what it looks like for a certain machine, go
  486. * into the machine I2C setup.
  487. */
  488. reg->dev = &pdev->dev;
  489. reg->plfdata = plfdata;
  490. config.dev = &pdev->dev;
  491. config.driver_data = reg;
  492. config.init_data = &plfdata->reg_constraints[i];
  493. /*
  494. * Register the regulator, pass around
  495. * the ab3100_regulator struct
  496. */
  497. rdev = regulator_register(&ab3100_regulator_desc[i], &config);
  498. if (IS_ERR(rdev)) {
  499. err = PTR_ERR(rdev);
  500. dev_err(&pdev->dev,
  501. "%s: failed to register regulator %s err %d\n",
  502. __func__, ab3100_regulator_desc[i].name,
  503. err);
  504. /* remove the already registered regulators */
  505. while (--i >= 0)
  506. regulator_unregister(ab3100_regulators[i].rdev);
  507. return err;
  508. }
  509. /* Then set a pointer back to the registered regulator */
  510. reg->rdev = rdev;
  511. }
  512. return 0;
  513. }
  514. static int __devexit ab3100_regulators_remove(struct platform_device *pdev)
  515. {
  516. int i;
  517. for (i = 0; i < AB3100_NUM_REGULATORS; i++) {
  518. struct ab3100_regulator *reg = &ab3100_regulators[i];
  519. regulator_unregister(reg->rdev);
  520. }
  521. return 0;
  522. }
  523. static struct platform_driver ab3100_regulators_driver = {
  524. .driver = {
  525. .name = "ab3100-regulators",
  526. .owner = THIS_MODULE,
  527. },
  528. .probe = ab3100_regulators_probe,
  529. .remove = __devexit_p(ab3100_regulators_remove),
  530. };
  531. static __init int ab3100_regulators_init(void)
  532. {
  533. return platform_driver_register(&ab3100_regulators_driver);
  534. }
  535. static __exit void ab3100_regulators_exit(void)
  536. {
  537. platform_driver_unregister(&ab3100_regulators_driver);
  538. }
  539. subsys_initcall(ab3100_regulators_init);
  540. module_exit(ab3100_regulators_exit);
  541. MODULE_AUTHOR("Mattias Wallin <mattias.wallin@stericsson.com>");
  542. MODULE_DESCRIPTION("AB3100 Regulator driver");
  543. MODULE_LICENSE("GPL");
  544. MODULE_ALIAS("platform:ab3100-regulators");