ab8500.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. *
  6. * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
  7. * Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
  8. *
  9. * AB8500 peripheral regulators
  10. *
  11. * AB8500 supports the following regulators:
  12. * VAUX1/2/3, VINTCORE, VTVOUT, VAUDIO, VAMIC1/2, VDMIC, VANA
  13. */
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/err.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mfd/ab8500.h>
  19. #include <linux/mfd/abx500.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/regulator/ab8500.h>
  23. /**
  24. * struct ab8500_regulator_info - ab8500 regulator information
  25. * @dev: device pointer
  26. * @desc: regulator description
  27. * @regulator_dev: regulator device
  28. * @max_uV: maximum voltage (for variable voltage supplies)
  29. * @min_uV: minimum voltage (for variable voltage supplies)
  30. * @fixed_uV: typical voltage (for fixed voltage supplies)
  31. * @update_bank: bank to control on/off
  32. * @update_reg: register to control on/off
  33. * @update_mask: mask to enable/disable regulator
  34. * @update_val_enable: bits to enable the regulator in normal (high power) mode
  35. * @voltage_bank: bank to control regulator voltage
  36. * @voltage_reg: register to control regulator voltage
  37. * @voltage_mask: mask to control regulator voltage
  38. * @voltages: supported voltage table
  39. * @voltages_len: number of supported voltages for the regulator
  40. */
  41. struct ab8500_regulator_info {
  42. struct device *dev;
  43. struct regulator_desc desc;
  44. struct regulator_dev *regulator;
  45. int max_uV;
  46. int min_uV;
  47. int fixed_uV;
  48. u8 update_bank;
  49. u8 update_reg;
  50. u8 update_mask;
  51. u8 update_val_enable;
  52. u8 voltage_bank;
  53. u8 voltage_reg;
  54. u8 voltage_mask;
  55. int const *voltages;
  56. int voltages_len;
  57. };
  58. /* voltage tables for the vauxn/vintcore supplies */
  59. static const int ldo_vauxn_voltages[] = {
  60. 1100000,
  61. 1200000,
  62. 1300000,
  63. 1400000,
  64. 1500000,
  65. 1800000,
  66. 1850000,
  67. 1900000,
  68. 2500000,
  69. 2650000,
  70. 2700000,
  71. 2750000,
  72. 2800000,
  73. 2900000,
  74. 3000000,
  75. 3300000,
  76. };
  77. static const int ldo_vaux3_voltages[] = {
  78. 1200000,
  79. 1500000,
  80. 1800000,
  81. 2100000,
  82. 2500000,
  83. 2750000,
  84. 2790000,
  85. 2910000,
  86. };
  87. static const int ldo_vintcore_voltages[] = {
  88. 1200000,
  89. 1225000,
  90. 1250000,
  91. 1275000,
  92. 1300000,
  93. 1325000,
  94. 1350000,
  95. };
  96. static int ab8500_regulator_enable(struct regulator_dev *rdev)
  97. {
  98. int ret;
  99. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  100. if (info == NULL) {
  101. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  102. return -EINVAL;
  103. }
  104. ret = abx500_mask_and_set_register_interruptible(info->dev,
  105. info->update_bank, info->update_reg,
  106. info->update_mask, info->update_val_enable);
  107. if (ret < 0)
  108. dev_err(rdev_get_dev(rdev),
  109. "couldn't set enable bits for regulator\n");
  110. dev_vdbg(rdev_get_dev(rdev),
  111. "%s-enable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
  112. info->desc.name, info->update_bank, info->update_reg,
  113. info->update_mask, info->update_val_enable);
  114. return ret;
  115. }
  116. static int ab8500_regulator_disable(struct regulator_dev *rdev)
  117. {
  118. int ret;
  119. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  120. if (info == NULL) {
  121. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  122. return -EINVAL;
  123. }
  124. ret = abx500_mask_and_set_register_interruptible(info->dev,
  125. info->update_bank, info->update_reg,
  126. info->update_mask, 0x0);
  127. if (ret < 0)
  128. dev_err(rdev_get_dev(rdev),
  129. "couldn't set disable bits for regulator\n");
  130. dev_vdbg(rdev_get_dev(rdev),
  131. "%s-disable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
  132. info->desc.name, info->update_bank, info->update_reg,
  133. info->update_mask, 0x0);
  134. return ret;
  135. }
  136. static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
  137. {
  138. int ret;
  139. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  140. u8 regval;
  141. if (info == NULL) {
  142. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  143. return -EINVAL;
  144. }
  145. ret = abx500_get_register_interruptible(info->dev,
  146. info->update_bank, info->update_reg, &regval);
  147. if (ret < 0) {
  148. dev_err(rdev_get_dev(rdev),
  149. "couldn't read 0x%x register\n", info->update_reg);
  150. return ret;
  151. }
  152. dev_vdbg(rdev_get_dev(rdev),
  153. "%s-is_enabled (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
  154. " 0x%x\n",
  155. info->desc.name, info->update_bank, info->update_reg,
  156. info->update_mask, regval);
  157. if (regval & info->update_mask)
  158. return true;
  159. else
  160. return false;
  161. }
  162. static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
  163. {
  164. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  165. if (info == NULL) {
  166. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  167. return -EINVAL;
  168. }
  169. /* return the uV for the fixed regulators */
  170. if (info->fixed_uV)
  171. return info->fixed_uV;
  172. if (selector >= info->voltages_len)
  173. return -EINVAL;
  174. return info->voltages[selector];
  175. }
  176. static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
  177. {
  178. int ret, val;
  179. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  180. u8 regval;
  181. if (info == NULL) {
  182. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  183. return -EINVAL;
  184. }
  185. ret = abx500_get_register_interruptible(info->dev,
  186. info->voltage_bank, info->voltage_reg, &regval);
  187. if (ret < 0) {
  188. dev_err(rdev_get_dev(rdev),
  189. "couldn't read voltage reg for regulator\n");
  190. return ret;
  191. }
  192. dev_vdbg(rdev_get_dev(rdev),
  193. "%s-get_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
  194. " 0x%x\n",
  195. info->desc.name, info->voltage_bank, info->voltage_reg,
  196. info->voltage_mask, regval);
  197. /* vintcore has a different layout */
  198. val = regval & info->voltage_mask;
  199. if (info->desc.id == AB8500_LDO_INTCORE)
  200. ret = info->voltages[val >> 0x3];
  201. else
  202. ret = info->voltages[val];
  203. return ret;
  204. }
  205. static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
  206. int min_uV, int max_uV)
  207. {
  208. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  209. int i;
  210. /* check the supported voltage */
  211. for (i = 0; i < info->voltages_len; i++) {
  212. if ((info->voltages[i] >= min_uV) &&
  213. (info->voltages[i] <= max_uV))
  214. return i;
  215. }
  216. return -EINVAL;
  217. }
  218. static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
  219. int min_uV, int max_uV,
  220. unsigned *selector)
  221. {
  222. int ret;
  223. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  224. u8 regval;
  225. if (info == NULL) {
  226. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  227. return -EINVAL;
  228. }
  229. /* get the appropriate voltages within the range */
  230. ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
  231. if (ret < 0) {
  232. dev_err(rdev_get_dev(rdev),
  233. "couldn't get best voltage for regulator\n");
  234. return ret;
  235. }
  236. *selector = ret;
  237. /* set the registers for the request */
  238. regval = (u8)ret;
  239. ret = abx500_mask_and_set_register_interruptible(info->dev,
  240. info->voltage_bank, info->voltage_reg,
  241. info->voltage_mask, regval);
  242. if (ret < 0)
  243. dev_err(rdev_get_dev(rdev),
  244. "couldn't set voltage reg for regulator\n");
  245. dev_vdbg(rdev_get_dev(rdev),
  246. "%s-set_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
  247. " 0x%x\n",
  248. info->desc.name, info->voltage_bank, info->voltage_reg,
  249. info->voltage_mask, regval);
  250. return ret;
  251. }
  252. static struct regulator_ops ab8500_regulator_ops = {
  253. .enable = ab8500_regulator_enable,
  254. .disable = ab8500_regulator_disable,
  255. .is_enabled = ab8500_regulator_is_enabled,
  256. .get_voltage = ab8500_regulator_get_voltage,
  257. .set_voltage = ab8500_regulator_set_voltage,
  258. .list_voltage = ab8500_list_voltage,
  259. };
  260. static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
  261. {
  262. struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
  263. if (info == NULL) {
  264. dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
  265. return -EINVAL;
  266. }
  267. return info->fixed_uV;
  268. }
  269. static struct regulator_ops ab8500_regulator_fixed_ops = {
  270. .enable = ab8500_regulator_enable,
  271. .disable = ab8500_regulator_disable,
  272. .is_enabled = ab8500_regulator_is_enabled,
  273. .get_voltage = ab8500_fixed_get_voltage,
  274. .list_voltage = ab8500_list_voltage,
  275. };
  276. static struct ab8500_regulator_info
  277. ab8500_regulator_info[AB8500_NUM_REGULATORS] = {
  278. /*
  279. * Variable Voltage Regulators
  280. * name, min mV, max mV,
  281. * update bank, reg, mask, enable val
  282. * volt bank, reg, mask, table, table length
  283. */
  284. [AB8500_LDO_AUX1] = {
  285. .desc = {
  286. .name = "LDO-AUX1",
  287. .ops = &ab8500_regulator_ops,
  288. .type = REGULATOR_VOLTAGE,
  289. .id = AB8500_LDO_AUX1,
  290. .owner = THIS_MODULE,
  291. .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
  292. },
  293. .min_uV = 1100000,
  294. .max_uV = 3300000,
  295. .update_bank = 0x04,
  296. .update_reg = 0x09,
  297. .update_mask = 0x03,
  298. .update_val_enable = 0x01,
  299. .voltage_bank = 0x04,
  300. .voltage_reg = 0x1f,
  301. .voltage_mask = 0x0f,
  302. .voltages = ldo_vauxn_voltages,
  303. .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
  304. },
  305. [AB8500_LDO_AUX2] = {
  306. .desc = {
  307. .name = "LDO-AUX2",
  308. .ops = &ab8500_regulator_ops,
  309. .type = REGULATOR_VOLTAGE,
  310. .id = AB8500_LDO_AUX2,
  311. .owner = THIS_MODULE,
  312. .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
  313. },
  314. .min_uV = 1100000,
  315. .max_uV = 3300000,
  316. .update_bank = 0x04,
  317. .update_reg = 0x09,
  318. .update_mask = 0x0c,
  319. .update_val_enable = 0x04,
  320. .voltage_bank = 0x04,
  321. .voltage_reg = 0x20,
  322. .voltage_mask = 0x0f,
  323. .voltages = ldo_vauxn_voltages,
  324. .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
  325. },
  326. [AB8500_LDO_AUX3] = {
  327. .desc = {
  328. .name = "LDO-AUX3",
  329. .ops = &ab8500_regulator_ops,
  330. .type = REGULATOR_VOLTAGE,
  331. .id = AB8500_LDO_AUX3,
  332. .owner = THIS_MODULE,
  333. .n_voltages = ARRAY_SIZE(ldo_vaux3_voltages),
  334. },
  335. .min_uV = 1100000,
  336. .max_uV = 3300000,
  337. .update_bank = 0x04,
  338. .update_reg = 0x0a,
  339. .update_mask = 0x03,
  340. .update_val_enable = 0x01,
  341. .voltage_bank = 0x04,
  342. .voltage_reg = 0x21,
  343. .voltage_mask = 0x07,
  344. .voltages = ldo_vaux3_voltages,
  345. .voltages_len = ARRAY_SIZE(ldo_vaux3_voltages),
  346. },
  347. [AB8500_LDO_INTCORE] = {
  348. .desc = {
  349. .name = "LDO-INTCORE",
  350. .ops = &ab8500_regulator_ops,
  351. .type = REGULATOR_VOLTAGE,
  352. .id = AB8500_LDO_INTCORE,
  353. .owner = THIS_MODULE,
  354. .n_voltages = ARRAY_SIZE(ldo_vintcore_voltages),
  355. },
  356. .min_uV = 1100000,
  357. .max_uV = 3300000,
  358. .update_bank = 0x03,
  359. .update_reg = 0x80,
  360. .update_mask = 0x44,
  361. .update_val_enable = 0x04,
  362. .voltage_bank = 0x03,
  363. .voltage_reg = 0x80,
  364. .voltage_mask = 0x38,
  365. .voltages = ldo_vintcore_voltages,
  366. .voltages_len = ARRAY_SIZE(ldo_vintcore_voltages),
  367. },
  368. /*
  369. * Fixed Voltage Regulators
  370. * name, fixed mV,
  371. * update bank, reg, mask, enable val
  372. */
  373. [AB8500_LDO_TVOUT] = {
  374. .desc = {
  375. .name = "LDO-TVOUT",
  376. .ops = &ab8500_regulator_fixed_ops,
  377. .type = REGULATOR_VOLTAGE,
  378. .id = AB8500_LDO_TVOUT,
  379. .owner = THIS_MODULE,
  380. .n_voltages = 1,
  381. },
  382. .fixed_uV = 2000000,
  383. .update_bank = 0x03,
  384. .update_reg = 0x80,
  385. .update_mask = 0x82,
  386. .update_val_enable = 0x02,
  387. },
  388. [AB8500_LDO_AUDIO] = {
  389. .desc = {
  390. .name = "LDO-AUDIO",
  391. .ops = &ab8500_regulator_fixed_ops,
  392. .type = REGULATOR_VOLTAGE,
  393. .id = AB8500_LDO_AUDIO,
  394. .owner = THIS_MODULE,
  395. .n_voltages = 1,
  396. },
  397. .fixed_uV = 2000000,
  398. .update_bank = 0x03,
  399. .update_reg = 0x83,
  400. .update_mask = 0x02,
  401. .update_val_enable = 0x02,
  402. },
  403. [AB8500_LDO_ANAMIC1] = {
  404. .desc = {
  405. .name = "LDO-ANAMIC1",
  406. .ops = &ab8500_regulator_fixed_ops,
  407. .type = REGULATOR_VOLTAGE,
  408. .id = AB8500_LDO_ANAMIC1,
  409. .owner = THIS_MODULE,
  410. .n_voltages = 1,
  411. },
  412. .fixed_uV = 2050000,
  413. .update_bank = 0x03,
  414. .update_reg = 0x83,
  415. .update_mask = 0x08,
  416. .update_val_enable = 0x08,
  417. },
  418. [AB8500_LDO_ANAMIC2] = {
  419. .desc = {
  420. .name = "LDO-ANAMIC2",
  421. .ops = &ab8500_regulator_fixed_ops,
  422. .type = REGULATOR_VOLTAGE,
  423. .id = AB8500_LDO_ANAMIC2,
  424. .owner = THIS_MODULE,
  425. .n_voltages = 1,
  426. },
  427. .fixed_uV = 2050000,
  428. .update_bank = 0x03,
  429. .update_reg = 0x83,
  430. .update_mask = 0x10,
  431. .update_val_enable = 0x10,
  432. },
  433. [AB8500_LDO_DMIC] = {
  434. .desc = {
  435. .name = "LDO-DMIC",
  436. .ops = &ab8500_regulator_fixed_ops,
  437. .type = REGULATOR_VOLTAGE,
  438. .id = AB8500_LDO_DMIC,
  439. .owner = THIS_MODULE,
  440. .n_voltages = 1,
  441. },
  442. .fixed_uV = 1800000,
  443. .update_bank = 0x03,
  444. .update_reg = 0x83,
  445. .update_mask = 0x04,
  446. .update_val_enable = 0x04,
  447. },
  448. [AB8500_LDO_ANA] = {
  449. .desc = {
  450. .name = "LDO-ANA",
  451. .ops = &ab8500_regulator_fixed_ops,
  452. .type = REGULATOR_VOLTAGE,
  453. .id = AB8500_LDO_ANA,
  454. .owner = THIS_MODULE,
  455. .n_voltages = 1,
  456. },
  457. .fixed_uV = 1200000,
  458. .update_bank = 0x04,
  459. .update_reg = 0x06,
  460. .update_mask = 0x0c,
  461. .update_val_enable = 0x04,
  462. },
  463. };
  464. static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
  465. {
  466. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  467. struct ab8500_platform_data *pdata;
  468. int i, err;
  469. if (!ab8500) {
  470. dev_err(&pdev->dev, "null mfd parent\n");
  471. return -EINVAL;
  472. }
  473. pdata = dev_get_platdata(ab8500->dev);
  474. if (!pdata) {
  475. dev_err(&pdev->dev, "null pdata\n");
  476. return -EINVAL;
  477. }
  478. /* make sure the platform data has the correct size */
  479. if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
  480. dev_err(&pdev->dev, "platform configuration error\n");
  481. return -EINVAL;
  482. }
  483. /* register all regulators */
  484. for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
  485. struct ab8500_regulator_info *info = NULL;
  486. /* assign per-regulator data */
  487. info = &ab8500_regulator_info[i];
  488. info->dev = &pdev->dev;
  489. /* fix for hardware before ab8500v2.0 */
  490. if (abx500_get_chip_id(info->dev) < 0x20) {
  491. if (info->desc.id == AB8500_LDO_AUX3) {
  492. info->desc.n_voltages =
  493. ARRAY_SIZE(ldo_vauxn_voltages);
  494. info->voltages = ldo_vauxn_voltages;
  495. info->voltages_len =
  496. ARRAY_SIZE(ldo_vauxn_voltages);
  497. info->voltage_mask = 0xf;
  498. }
  499. }
  500. /* register regulator with framework */
  501. info->regulator = regulator_register(&info->desc, &pdev->dev,
  502. &pdata->regulator[i], info);
  503. if (IS_ERR(info->regulator)) {
  504. err = PTR_ERR(info->regulator);
  505. dev_err(&pdev->dev, "failed to register regulator %s\n",
  506. info->desc.name);
  507. /* when we fail, un-register all earlier regulators */
  508. while (--i >= 0) {
  509. info = &ab8500_regulator_info[i];
  510. regulator_unregister(info->regulator);
  511. }
  512. return err;
  513. }
  514. dev_vdbg(rdev_get_dev(info->regulator),
  515. "%s-probed\n", info->desc.name);
  516. }
  517. return 0;
  518. }
  519. static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
  520. {
  521. int i;
  522. for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
  523. struct ab8500_regulator_info *info = NULL;
  524. info = &ab8500_regulator_info[i];
  525. dev_vdbg(rdev_get_dev(info->regulator),
  526. "%s-remove\n", info->desc.name);
  527. regulator_unregister(info->regulator);
  528. }
  529. return 0;
  530. }
  531. static struct platform_driver ab8500_regulator_driver = {
  532. .probe = ab8500_regulator_probe,
  533. .remove = __devexit_p(ab8500_regulator_remove),
  534. .driver = {
  535. .name = "ab8500-regulator",
  536. .owner = THIS_MODULE,
  537. },
  538. };
  539. static int __init ab8500_regulator_init(void)
  540. {
  541. int ret;
  542. ret = platform_driver_register(&ab8500_regulator_driver);
  543. if (ret != 0)
  544. pr_err("Failed to register ab8500 regulator: %d\n", ret);
  545. return ret;
  546. }
  547. subsys_initcall(ab8500_regulator_init);
  548. static void __exit ab8500_regulator_exit(void)
  549. {
  550. platform_driver_unregister(&ab8500_regulator_driver);
  551. }
  552. module_exit(ab8500_regulator_exit);
  553. MODULE_LICENSE("GPL v2");
  554. MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
  555. MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
  556. MODULE_ALIAS("platform:ab8500-regulator");