ab3100.c 16 KB

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