tps65910-regulator.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * tps65910.c -- TI tps65910
  3. *
  4. * Copyright 2010 Texas Instruments Inc.
  5. *
  6. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  7. * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/gpio.h>
  25. #include <linux/mfd/tps65910.h>
  26. #define TPS65910_SUPPLY_STATE_ENABLED 0x1
  27. /* supported VIO voltages in milivolts */
  28. static const u16 VIO_VSEL_table[] = {
  29. 1500, 1800, 2500, 3300,
  30. };
  31. /* VSEL tables for TPS65910 specific LDOs and dcdc's */
  32. /* supported VDD3 voltages in milivolts */
  33. static const u16 VDD3_VSEL_table[] = {
  34. 5000,
  35. };
  36. /* supported VDIG1 voltages in milivolts */
  37. static const u16 VDIG1_VSEL_table[] = {
  38. 1200, 1500, 1800, 2700,
  39. };
  40. /* supported VDIG2 voltages in milivolts */
  41. static const u16 VDIG2_VSEL_table[] = {
  42. 1000, 1100, 1200, 1800,
  43. };
  44. /* supported VPLL voltages in milivolts */
  45. static const u16 VPLL_VSEL_table[] = {
  46. 1000, 1100, 1800, 2500,
  47. };
  48. /* supported VDAC voltages in milivolts */
  49. static const u16 VDAC_VSEL_table[] = {
  50. 1800, 2600, 2800, 2850,
  51. };
  52. /* supported VAUX1 voltages in milivolts */
  53. static const u16 VAUX1_VSEL_table[] = {
  54. 1800, 2500, 2800, 2850,
  55. };
  56. /* supported VAUX2 voltages in milivolts */
  57. static const u16 VAUX2_VSEL_table[] = {
  58. 1800, 2800, 2900, 3300,
  59. };
  60. /* supported VAUX33 voltages in milivolts */
  61. static const u16 VAUX33_VSEL_table[] = {
  62. 1800, 2000, 2800, 3300,
  63. };
  64. /* supported VMMC voltages in milivolts */
  65. static const u16 VMMC_VSEL_table[] = {
  66. 1800, 2800, 3000, 3300,
  67. };
  68. struct tps_info {
  69. const char *name;
  70. unsigned min_uV;
  71. unsigned max_uV;
  72. u8 table_len;
  73. const u16 *table;
  74. };
  75. static struct tps_info tps65910_regs[] = {
  76. {
  77. .name = "VRTC",
  78. },
  79. {
  80. .name = "VIO",
  81. .min_uV = 1500000,
  82. .max_uV = 3300000,
  83. .table_len = ARRAY_SIZE(VIO_VSEL_table),
  84. .table = VIO_VSEL_table,
  85. },
  86. {
  87. .name = "VDD1",
  88. .min_uV = 600000,
  89. .max_uV = 4500000,
  90. },
  91. {
  92. .name = "VDD2",
  93. .min_uV = 600000,
  94. .max_uV = 4500000,
  95. },
  96. {
  97. .name = "VDD3",
  98. .min_uV = 5000000,
  99. .max_uV = 5000000,
  100. .table_len = ARRAY_SIZE(VDD3_VSEL_table),
  101. .table = VDD3_VSEL_table,
  102. },
  103. {
  104. .name = "VDIG1",
  105. .min_uV = 1200000,
  106. .max_uV = 2700000,
  107. .table_len = ARRAY_SIZE(VDIG1_VSEL_table),
  108. .table = VDIG1_VSEL_table,
  109. },
  110. {
  111. .name = "VDIG2",
  112. .min_uV = 1000000,
  113. .max_uV = 1800000,
  114. .table_len = ARRAY_SIZE(VDIG2_VSEL_table),
  115. .table = VDIG2_VSEL_table,
  116. },
  117. {
  118. .name = "VPLL",
  119. .min_uV = 1000000,
  120. .max_uV = 2500000,
  121. .table_len = ARRAY_SIZE(VPLL_VSEL_table),
  122. .table = VPLL_VSEL_table,
  123. },
  124. {
  125. .name = "VDAC",
  126. .min_uV = 1800000,
  127. .max_uV = 2850000,
  128. .table_len = ARRAY_SIZE(VDAC_VSEL_table),
  129. .table = VDAC_VSEL_table,
  130. },
  131. {
  132. .name = "VAUX1",
  133. .min_uV = 1800000,
  134. .max_uV = 2850000,
  135. .table_len = ARRAY_SIZE(VAUX1_VSEL_table),
  136. .table = VAUX1_VSEL_table,
  137. },
  138. {
  139. .name = "VAUX2",
  140. .min_uV = 1800000,
  141. .max_uV = 3300000,
  142. .table_len = ARRAY_SIZE(VAUX2_VSEL_table),
  143. .table = VAUX2_VSEL_table,
  144. },
  145. {
  146. .name = "VAUX33",
  147. .min_uV = 1800000,
  148. .max_uV = 3300000,
  149. .table_len = ARRAY_SIZE(VAUX33_VSEL_table),
  150. .table = VAUX33_VSEL_table,
  151. },
  152. {
  153. .name = "VMMC",
  154. .min_uV = 1800000,
  155. .max_uV = 3300000,
  156. .table_len = ARRAY_SIZE(VMMC_VSEL_table),
  157. .table = VMMC_VSEL_table,
  158. },
  159. };
  160. static struct tps_info tps65911_regs[] = {
  161. {
  162. .name = "VRTC",
  163. },
  164. {
  165. .name = "VIO",
  166. .min_uV = 1500000,
  167. .max_uV = 3300000,
  168. .table_len = ARRAY_SIZE(VIO_VSEL_table),
  169. .table = VIO_VSEL_table,
  170. },
  171. {
  172. .name = "VDD1",
  173. .min_uV = 600000,
  174. .max_uV = 4500000,
  175. },
  176. {
  177. .name = "VDD2",
  178. .min_uV = 600000,
  179. .max_uV = 4500000,
  180. },
  181. {
  182. .name = "VDDCTRL",
  183. .min_uV = 600000,
  184. .max_uV = 1400000,
  185. },
  186. {
  187. .name = "LDO1",
  188. .min_uV = 1000000,
  189. .max_uV = 3300000,
  190. },
  191. {
  192. .name = "LDO2",
  193. .min_uV = 1000000,
  194. .max_uV = 3300000,
  195. },
  196. {
  197. .name = "LDO3",
  198. .min_uV = 1000000,
  199. .max_uV = 3300000,
  200. },
  201. {
  202. .name = "LDO4",
  203. .min_uV = 1000000,
  204. .max_uV = 3300000,
  205. },
  206. {
  207. .name = "LDO5",
  208. .min_uV = 1000000,
  209. .max_uV = 3300000,
  210. },
  211. {
  212. .name = "LDO6",
  213. .min_uV = 1000000,
  214. .max_uV = 3300000,
  215. },
  216. {
  217. .name = "LDO7",
  218. .min_uV = 1000000,
  219. .max_uV = 3300000,
  220. },
  221. {
  222. .name = "LDO8",
  223. .min_uV = 1000000,
  224. .max_uV = 3300000,
  225. },
  226. };
  227. struct tps65910_reg {
  228. struct regulator_desc *desc;
  229. struct tps65910 *mfd;
  230. struct regulator_dev **rdev;
  231. struct tps_info **info;
  232. struct mutex mutex;
  233. int num_regulators;
  234. int mode;
  235. int (*get_ctrl_reg)(int);
  236. };
  237. static inline int tps65910_read(struct tps65910_reg *pmic, u8 reg)
  238. {
  239. u8 val;
  240. int err;
  241. err = pmic->mfd->read(pmic->mfd, reg, 1, &val);
  242. if (err)
  243. return err;
  244. return val;
  245. }
  246. static inline int tps65910_write(struct tps65910_reg *pmic, u8 reg, u8 val)
  247. {
  248. return pmic->mfd->write(pmic->mfd, reg, 1, &val);
  249. }
  250. static int tps65910_modify_bits(struct tps65910_reg *pmic, u8 reg,
  251. u8 set_mask, u8 clear_mask)
  252. {
  253. int err, data;
  254. mutex_lock(&pmic->mutex);
  255. data = tps65910_read(pmic, reg);
  256. if (data < 0) {
  257. dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
  258. err = data;
  259. goto out;
  260. }
  261. data &= ~clear_mask;
  262. data |= set_mask;
  263. err = tps65910_write(pmic, reg, data);
  264. if (err)
  265. dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
  266. out:
  267. mutex_unlock(&pmic->mutex);
  268. return err;
  269. }
  270. static int tps65910_reg_read(struct tps65910_reg *pmic, u8 reg)
  271. {
  272. int data;
  273. mutex_lock(&pmic->mutex);
  274. data = tps65910_read(pmic, reg);
  275. if (data < 0)
  276. dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
  277. mutex_unlock(&pmic->mutex);
  278. return data;
  279. }
  280. static int tps65910_reg_write(struct tps65910_reg *pmic, u8 reg, u8 val)
  281. {
  282. int err;
  283. mutex_lock(&pmic->mutex);
  284. err = tps65910_write(pmic, reg, val);
  285. if (err < 0)
  286. dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
  287. mutex_unlock(&pmic->mutex);
  288. return err;
  289. }
  290. static int tps65910_get_ctrl_register(int id)
  291. {
  292. switch (id) {
  293. case TPS65910_REG_VRTC:
  294. return TPS65910_VRTC;
  295. case TPS65910_REG_VIO:
  296. return TPS65910_VIO;
  297. case TPS65910_REG_VDD1:
  298. return TPS65910_VDD1;
  299. case TPS65910_REG_VDD2:
  300. return TPS65910_VDD2;
  301. case TPS65910_REG_VDD3:
  302. return TPS65910_VDD3;
  303. case TPS65910_REG_VDIG1:
  304. return TPS65910_VDIG1;
  305. case TPS65910_REG_VDIG2:
  306. return TPS65910_VDIG2;
  307. case TPS65910_REG_VPLL:
  308. return TPS65910_VPLL;
  309. case TPS65910_REG_VDAC:
  310. return TPS65910_VDAC;
  311. case TPS65910_REG_VAUX1:
  312. return TPS65910_VAUX1;
  313. case TPS65910_REG_VAUX2:
  314. return TPS65910_VAUX2;
  315. case TPS65910_REG_VAUX33:
  316. return TPS65910_VAUX33;
  317. case TPS65910_REG_VMMC:
  318. return TPS65910_VMMC;
  319. default:
  320. return -EINVAL;
  321. }
  322. }
  323. static int tps65911_get_ctrl_register(int id)
  324. {
  325. switch (id) {
  326. case TPS65910_REG_VRTC:
  327. return TPS65910_VRTC;
  328. case TPS65910_REG_VIO:
  329. return TPS65910_VIO;
  330. case TPS65910_REG_VDD1:
  331. return TPS65910_VDD1;
  332. case TPS65910_REG_VDD2:
  333. return TPS65910_VDD2;
  334. case TPS65911_REG_VDDCTRL:
  335. return TPS65911_VDDCTRL;
  336. case TPS65911_REG_LDO1:
  337. return TPS65911_LDO1;
  338. case TPS65911_REG_LDO2:
  339. return TPS65911_LDO2;
  340. case TPS65911_REG_LDO3:
  341. return TPS65911_LDO3;
  342. case TPS65911_REG_LDO4:
  343. return TPS65911_LDO4;
  344. case TPS65911_REG_LDO5:
  345. return TPS65911_LDO5;
  346. case TPS65911_REG_LDO6:
  347. return TPS65911_LDO6;
  348. case TPS65911_REG_LDO7:
  349. return TPS65911_LDO7;
  350. case TPS65911_REG_LDO8:
  351. return TPS65911_LDO8;
  352. default:
  353. return -EINVAL;
  354. }
  355. }
  356. static int tps65910_is_enabled(struct regulator_dev *dev)
  357. {
  358. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  359. int reg, value, id = rdev_get_id(dev);
  360. reg = pmic->get_ctrl_reg(id);
  361. if (reg < 0)
  362. return reg;
  363. value = tps65910_reg_read(pmic, reg);
  364. if (value < 0)
  365. return value;
  366. return value & TPS65910_SUPPLY_STATE_ENABLED;
  367. }
  368. static int tps65910_enable(struct regulator_dev *dev)
  369. {
  370. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  371. struct tps65910 *mfd = pmic->mfd;
  372. int reg, id = rdev_get_id(dev);
  373. reg = pmic->get_ctrl_reg(id);
  374. if (reg < 0)
  375. return reg;
  376. return tps65910_set_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
  377. }
  378. static int tps65910_disable(struct regulator_dev *dev)
  379. {
  380. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  381. struct tps65910 *mfd = pmic->mfd;
  382. int reg, id = rdev_get_id(dev);
  383. reg = pmic->get_ctrl_reg(id);
  384. if (reg < 0)
  385. return reg;
  386. return tps65910_clear_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
  387. }
  388. static int tps65910_set_mode(struct regulator_dev *dev, unsigned int mode)
  389. {
  390. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  391. struct tps65910 *mfd = pmic->mfd;
  392. int reg, value, id = rdev_get_id(dev);
  393. reg = pmic->get_ctrl_reg(id);
  394. if (reg < 0)
  395. return reg;
  396. switch (mode) {
  397. case REGULATOR_MODE_NORMAL:
  398. return tps65910_modify_bits(pmic, reg, LDO_ST_ON_BIT,
  399. LDO_ST_MODE_BIT);
  400. case REGULATOR_MODE_IDLE:
  401. value = LDO_ST_ON_BIT | LDO_ST_MODE_BIT;
  402. return tps65910_set_bits(mfd, reg, value);
  403. case REGULATOR_MODE_STANDBY:
  404. return tps65910_clear_bits(mfd, reg, LDO_ST_ON_BIT);
  405. }
  406. return -EINVAL;
  407. }
  408. static unsigned int tps65910_get_mode(struct regulator_dev *dev)
  409. {
  410. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  411. int reg, value, id = rdev_get_id(dev);
  412. reg = pmic->get_ctrl_reg(id);
  413. if (reg < 0)
  414. return reg;
  415. value = tps65910_reg_read(pmic, reg);
  416. if (value < 0)
  417. return value;
  418. if (value & LDO_ST_ON_BIT)
  419. return REGULATOR_MODE_STANDBY;
  420. else if (value & LDO_ST_MODE_BIT)
  421. return REGULATOR_MODE_IDLE;
  422. else
  423. return REGULATOR_MODE_NORMAL;
  424. }
  425. static int tps65910_get_voltage_dcdc(struct regulator_dev *dev)
  426. {
  427. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  428. int id = rdev_get_id(dev), voltage = 0;
  429. int opvsel = 0, srvsel = 0, vselmax = 0, mult = 0, sr = 0;
  430. switch (id) {
  431. case TPS65910_REG_VDD1:
  432. opvsel = tps65910_reg_read(pmic, TPS65910_VDD1_OP);
  433. mult = tps65910_reg_read(pmic, TPS65910_VDD1);
  434. mult = (mult & VDD1_VGAIN_SEL_MASK) >> VDD1_VGAIN_SEL_SHIFT;
  435. srvsel = tps65910_reg_read(pmic, TPS65910_VDD1_SR);
  436. sr = opvsel & VDD1_OP_CMD_MASK;
  437. opvsel &= VDD1_OP_SEL_MASK;
  438. srvsel &= VDD1_SR_SEL_MASK;
  439. vselmax = 75;
  440. break;
  441. case TPS65910_REG_VDD2:
  442. opvsel = tps65910_reg_read(pmic, TPS65910_VDD2_OP);
  443. mult = tps65910_reg_read(pmic, TPS65910_VDD2);
  444. mult = (mult & VDD2_VGAIN_SEL_MASK) >> VDD2_VGAIN_SEL_SHIFT;
  445. srvsel = tps65910_reg_read(pmic, TPS65910_VDD2_SR);
  446. sr = opvsel & VDD2_OP_CMD_MASK;
  447. opvsel &= VDD2_OP_SEL_MASK;
  448. srvsel &= VDD2_SR_SEL_MASK;
  449. vselmax = 75;
  450. break;
  451. case TPS65911_REG_VDDCTRL:
  452. opvsel = tps65910_reg_read(pmic, TPS65911_VDDCTRL_OP);
  453. srvsel = tps65910_reg_read(pmic, TPS65911_VDDCTRL_SR);
  454. sr = opvsel & VDDCTRL_OP_CMD_MASK;
  455. opvsel &= VDDCTRL_OP_SEL_MASK;
  456. srvsel &= VDDCTRL_SR_SEL_MASK;
  457. vselmax = 64;
  458. break;
  459. }
  460. /* multiplier 0 == 1 but 2,3 normal */
  461. if (!mult)
  462. mult=1;
  463. if (sr) {
  464. /* normalise to valid range */
  465. if (srvsel < 3)
  466. srvsel = 3;
  467. if (srvsel > vselmax)
  468. srvsel = vselmax;
  469. srvsel -= 3;
  470. voltage = (srvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
  471. } else {
  472. /* normalise to valid range*/
  473. if (opvsel < 3)
  474. opvsel = 3;
  475. if (opvsel > vselmax)
  476. opvsel = vselmax;
  477. opvsel -= 3;
  478. voltage = (opvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
  479. }
  480. voltage *= mult;
  481. return voltage;
  482. }
  483. static int tps65910_get_voltage(struct regulator_dev *dev)
  484. {
  485. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  486. int reg, value, id = rdev_get_id(dev), voltage = 0;
  487. reg = pmic->get_ctrl_reg(id);
  488. if (reg < 0)
  489. return reg;
  490. value = tps65910_reg_read(pmic, reg);
  491. if (value < 0)
  492. return value;
  493. switch (id) {
  494. case TPS65910_REG_VIO:
  495. case TPS65910_REG_VDIG1:
  496. case TPS65910_REG_VDIG2:
  497. case TPS65910_REG_VPLL:
  498. case TPS65910_REG_VDAC:
  499. case TPS65910_REG_VAUX1:
  500. case TPS65910_REG_VAUX2:
  501. case TPS65910_REG_VAUX33:
  502. case TPS65910_REG_VMMC:
  503. value &= LDO_SEL_MASK;
  504. value >>= LDO_SEL_SHIFT;
  505. break;
  506. default:
  507. return -EINVAL;
  508. }
  509. voltage = pmic->info[id]->table[value] * 1000;
  510. return voltage;
  511. }
  512. static int tps65910_get_voltage_vdd3(struct regulator_dev *dev)
  513. {
  514. return 5 * 1000 * 1000;
  515. }
  516. static int tps65911_get_voltage(struct regulator_dev *dev)
  517. {
  518. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  519. int step_mv, id = rdev_get_id(dev);
  520. u8 value, reg;
  521. reg = pmic->get_ctrl_reg(id);
  522. value = tps65910_reg_read(pmic, reg);
  523. switch (id) {
  524. case TPS65911_REG_LDO1:
  525. case TPS65911_REG_LDO2:
  526. case TPS65911_REG_LDO4:
  527. value &= LDO1_SEL_MASK;
  528. value >>= LDO_SEL_SHIFT;
  529. /* The first 5 values of the selector correspond to 1V */
  530. if (value < 5)
  531. value = 0;
  532. else
  533. value -= 4;
  534. step_mv = 50;
  535. break;
  536. case TPS65911_REG_LDO3:
  537. case TPS65911_REG_LDO5:
  538. case TPS65911_REG_LDO6:
  539. case TPS65911_REG_LDO7:
  540. case TPS65911_REG_LDO8:
  541. value &= LDO3_SEL_MASK;
  542. value >>= LDO_SEL_SHIFT;
  543. /* The first 3 values of the selector correspond to 1V */
  544. if (value < 3)
  545. value = 0;
  546. else
  547. value -= 2;
  548. step_mv = 100;
  549. break;
  550. case TPS65910_REG_VIO:
  551. return pmic->info[id]->table[value] * 1000;
  552. break;
  553. default:
  554. return -EINVAL;
  555. }
  556. return (LDO_MIN_VOLT + value * step_mv) * 1000;
  557. }
  558. static int tps65910_set_voltage_dcdc(struct regulator_dev *dev,
  559. unsigned selector)
  560. {
  561. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  562. int id = rdev_get_id(dev), vsel;
  563. int dcdc_mult = 0;
  564. switch (id) {
  565. case TPS65910_REG_VDD1:
  566. dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
  567. if (dcdc_mult == 1)
  568. dcdc_mult--;
  569. vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
  570. tps65910_modify_bits(pmic, TPS65910_VDD1,
  571. (dcdc_mult << VDD1_VGAIN_SEL_SHIFT),
  572. VDD1_VGAIN_SEL_MASK);
  573. tps65910_reg_write(pmic, TPS65910_VDD1_OP, vsel);
  574. break;
  575. case TPS65910_REG_VDD2:
  576. dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
  577. if (dcdc_mult == 1)
  578. dcdc_mult--;
  579. vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
  580. tps65910_modify_bits(pmic, TPS65910_VDD2,
  581. (dcdc_mult << VDD2_VGAIN_SEL_SHIFT),
  582. VDD1_VGAIN_SEL_MASK);
  583. tps65910_reg_write(pmic, TPS65910_VDD2_OP, vsel);
  584. break;
  585. case TPS65911_REG_VDDCTRL:
  586. vsel = selector;
  587. tps65910_reg_write(pmic, TPS65911_VDDCTRL_OP, vsel);
  588. }
  589. return 0;
  590. }
  591. static int tps65910_set_voltage(struct regulator_dev *dev, unsigned selector)
  592. {
  593. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  594. int reg, id = rdev_get_id(dev);
  595. reg = pmic->get_ctrl_reg(id);
  596. if (reg < 0)
  597. return reg;
  598. switch (id) {
  599. case TPS65910_REG_VIO:
  600. case TPS65910_REG_VDIG1:
  601. case TPS65910_REG_VDIG2:
  602. case TPS65910_REG_VPLL:
  603. case TPS65910_REG_VDAC:
  604. case TPS65910_REG_VAUX1:
  605. case TPS65910_REG_VAUX2:
  606. case TPS65910_REG_VAUX33:
  607. case TPS65910_REG_VMMC:
  608. return tps65910_modify_bits(pmic, reg,
  609. (selector << LDO_SEL_SHIFT), LDO_SEL_MASK);
  610. }
  611. return -EINVAL;
  612. }
  613. static int tps65911_set_voltage(struct regulator_dev *dev, unsigned selector)
  614. {
  615. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  616. int reg, id = rdev_get_id(dev);
  617. reg = pmic->get_ctrl_reg(id);
  618. if (reg < 0)
  619. return reg;
  620. switch (id) {
  621. case TPS65911_REG_LDO1:
  622. case TPS65911_REG_LDO2:
  623. case TPS65911_REG_LDO4:
  624. return tps65910_modify_bits(pmic, reg,
  625. (selector << LDO_SEL_SHIFT), LDO1_SEL_MASK);
  626. case TPS65911_REG_LDO3:
  627. case TPS65911_REG_LDO5:
  628. case TPS65911_REG_LDO6:
  629. case TPS65911_REG_LDO7:
  630. case TPS65911_REG_LDO8:
  631. case TPS65910_REG_VIO:
  632. return tps65910_modify_bits(pmic, reg,
  633. (selector << LDO_SEL_SHIFT), LDO3_SEL_MASK);
  634. }
  635. return -EINVAL;
  636. }
  637. static int tps65910_list_voltage_dcdc(struct regulator_dev *dev,
  638. unsigned selector)
  639. {
  640. int volt, mult = 1, id = rdev_get_id(dev);
  641. switch (id) {
  642. case TPS65910_REG_VDD1:
  643. case TPS65910_REG_VDD2:
  644. mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
  645. volt = VDD1_2_MIN_VOLT +
  646. (selector % VDD1_2_NUM_VOLT_FINE) * VDD1_2_OFFSET;
  647. break;
  648. case TPS65911_REG_VDDCTRL:
  649. volt = VDDCTRL_MIN_VOLT + (selector * VDDCTRL_OFFSET);
  650. break;
  651. default:
  652. BUG();
  653. return -EINVAL;
  654. }
  655. return volt * 100 * mult;
  656. }
  657. static int tps65910_list_voltage(struct regulator_dev *dev,
  658. unsigned selector)
  659. {
  660. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  661. int id = rdev_get_id(dev), voltage;
  662. if (id < TPS65910_REG_VIO || id > TPS65910_REG_VMMC)
  663. return -EINVAL;
  664. if (selector >= pmic->info[id]->table_len)
  665. return -EINVAL;
  666. else
  667. voltage = pmic->info[id]->table[selector] * 1000;
  668. return voltage;
  669. }
  670. static int tps65911_list_voltage(struct regulator_dev *dev, unsigned selector)
  671. {
  672. struct tps65910_reg *pmic = rdev_get_drvdata(dev);
  673. int step_mv = 0, id = rdev_get_id(dev);
  674. switch(id) {
  675. case TPS65911_REG_LDO1:
  676. case TPS65911_REG_LDO2:
  677. case TPS65911_REG_LDO4:
  678. /* The first 5 values of the selector correspond to 1V */
  679. if (selector < 5)
  680. selector = 0;
  681. else
  682. selector -= 4;
  683. step_mv = 50;
  684. break;
  685. case TPS65911_REG_LDO3:
  686. case TPS65911_REG_LDO5:
  687. case TPS65911_REG_LDO6:
  688. case TPS65911_REG_LDO7:
  689. case TPS65911_REG_LDO8:
  690. /* The first 3 values of the selector correspond to 1V */
  691. if (selector < 3)
  692. selector = 0;
  693. else
  694. selector -= 2;
  695. step_mv = 100;
  696. break;
  697. case TPS65910_REG_VIO:
  698. return pmic->info[id]->table[selector] * 1000;
  699. default:
  700. return -EINVAL;
  701. }
  702. return (LDO_MIN_VOLT + selector * step_mv) * 1000;
  703. }
  704. /* Regulator ops (except VRTC) */
  705. static struct regulator_ops tps65910_ops_dcdc = {
  706. .is_enabled = tps65910_is_enabled,
  707. .enable = tps65910_enable,
  708. .disable = tps65910_disable,
  709. .set_mode = tps65910_set_mode,
  710. .get_mode = tps65910_get_mode,
  711. .get_voltage = tps65910_get_voltage_dcdc,
  712. .set_voltage_sel = tps65910_set_voltage_dcdc,
  713. .list_voltage = tps65910_list_voltage_dcdc,
  714. };
  715. static struct regulator_ops tps65910_ops_vdd3 = {
  716. .is_enabled = tps65910_is_enabled,
  717. .enable = tps65910_enable,
  718. .disable = tps65910_disable,
  719. .set_mode = tps65910_set_mode,
  720. .get_mode = tps65910_get_mode,
  721. .get_voltage = tps65910_get_voltage_vdd3,
  722. .list_voltage = tps65910_list_voltage,
  723. };
  724. static struct regulator_ops tps65910_ops = {
  725. .is_enabled = tps65910_is_enabled,
  726. .enable = tps65910_enable,
  727. .disable = tps65910_disable,
  728. .set_mode = tps65910_set_mode,
  729. .get_mode = tps65910_get_mode,
  730. .get_voltage = tps65910_get_voltage,
  731. .set_voltage_sel = tps65910_set_voltage,
  732. .list_voltage = tps65910_list_voltage,
  733. };
  734. static struct regulator_ops tps65911_ops = {
  735. .is_enabled = tps65910_is_enabled,
  736. .enable = tps65910_enable,
  737. .disable = tps65910_disable,
  738. .set_mode = tps65910_set_mode,
  739. .get_mode = tps65910_get_mode,
  740. .get_voltage = tps65911_get_voltage,
  741. .set_voltage_sel = tps65911_set_voltage,
  742. .list_voltage = tps65911_list_voltage,
  743. };
  744. static __devinit int tps65910_probe(struct platform_device *pdev)
  745. {
  746. struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
  747. struct tps_info *info;
  748. struct regulator_init_data *reg_data;
  749. struct regulator_dev *rdev;
  750. struct tps65910_reg *pmic;
  751. struct tps65910_board *pmic_plat_data;
  752. int i, err;
  753. pmic_plat_data = dev_get_platdata(tps65910->dev);
  754. if (!pmic_plat_data)
  755. return -EINVAL;
  756. pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
  757. if (!pmic)
  758. return -ENOMEM;
  759. mutex_init(&pmic->mutex);
  760. pmic->mfd = tps65910;
  761. platform_set_drvdata(pdev, pmic);
  762. /* Give control of all register to control port */
  763. tps65910_set_bits(pmic->mfd, TPS65910_DEVCTRL,
  764. DEVCTRL_SR_CTL_I2C_SEL_MASK);
  765. switch(tps65910_chip_id(tps65910)) {
  766. case TPS65910:
  767. pmic->get_ctrl_reg = &tps65910_get_ctrl_register;
  768. pmic->num_regulators = ARRAY_SIZE(tps65910_regs);
  769. info = tps65910_regs;
  770. break;
  771. case TPS65911:
  772. pmic->get_ctrl_reg = &tps65911_get_ctrl_register;
  773. pmic->num_regulators = ARRAY_SIZE(tps65911_regs);
  774. info = tps65911_regs;
  775. break;
  776. default:
  777. pr_err("Invalid tps chip version\n");
  778. kfree(pmic);
  779. return -ENODEV;
  780. }
  781. pmic->desc = kcalloc(pmic->num_regulators,
  782. sizeof(struct regulator_desc), GFP_KERNEL);
  783. if (!pmic->desc) {
  784. err = -ENOMEM;
  785. goto err_free_pmic;
  786. }
  787. pmic->info = kcalloc(pmic->num_regulators,
  788. sizeof(struct tps_info *), GFP_KERNEL);
  789. if (!pmic->info) {
  790. err = -ENOMEM;
  791. goto err_free_desc;
  792. }
  793. pmic->rdev = kcalloc(pmic->num_regulators,
  794. sizeof(struct regulator_dev *), GFP_KERNEL);
  795. if (!pmic->rdev) {
  796. err = -ENOMEM;
  797. goto err_free_info;
  798. }
  799. for (i = 0; i < pmic->num_regulators && i < TPS65910_NUM_REGS;
  800. i++, info++) {
  801. reg_data = pmic_plat_data->tps65910_pmic_init_data[i];
  802. /* Regulator API handles empty constraints but not NULL
  803. * constraints */
  804. if (!reg_data)
  805. continue;
  806. /* Register the regulators */
  807. pmic->info[i] = info;
  808. pmic->desc[i].name = info->name;
  809. pmic->desc[i].id = i;
  810. pmic->desc[i].n_voltages = info->table_len;
  811. if (i == TPS65910_REG_VDD1 || i == TPS65910_REG_VDD2) {
  812. pmic->desc[i].ops = &tps65910_ops_dcdc;
  813. pmic->desc[i].n_voltages = VDD1_2_NUM_VOLT_FINE *
  814. VDD1_2_NUM_VOLT_COARSE;
  815. } else if (i == TPS65910_REG_VDD3) {
  816. if (tps65910_chip_id(tps65910) == TPS65910)
  817. pmic->desc[i].ops = &tps65910_ops_vdd3;
  818. else
  819. pmic->desc[i].ops = &tps65910_ops_dcdc;
  820. } else {
  821. if (tps65910_chip_id(tps65910) == TPS65910)
  822. pmic->desc[i].ops = &tps65910_ops;
  823. else
  824. pmic->desc[i].ops = &tps65911_ops;
  825. }
  826. pmic->desc[i].type = REGULATOR_VOLTAGE;
  827. pmic->desc[i].owner = THIS_MODULE;
  828. rdev = regulator_register(&pmic->desc[i],
  829. tps65910->dev, reg_data, pmic, NULL);
  830. if (IS_ERR(rdev)) {
  831. dev_err(tps65910->dev,
  832. "failed to register %s regulator\n",
  833. pdev->name);
  834. err = PTR_ERR(rdev);
  835. goto err_unregister_regulator;
  836. }
  837. /* Save regulator for cleanup */
  838. pmic->rdev[i] = rdev;
  839. }
  840. return 0;
  841. err_unregister_regulator:
  842. while (--i >= 0)
  843. regulator_unregister(pmic->rdev[i]);
  844. kfree(pmic->rdev);
  845. err_free_info:
  846. kfree(pmic->info);
  847. err_free_desc:
  848. kfree(pmic->desc);
  849. err_free_pmic:
  850. kfree(pmic);
  851. return err;
  852. }
  853. static int __devexit tps65910_remove(struct platform_device *pdev)
  854. {
  855. struct tps65910_reg *pmic = platform_get_drvdata(pdev);
  856. int i;
  857. for (i = 0; i < pmic->num_regulators; i++)
  858. regulator_unregister(pmic->rdev[i]);
  859. kfree(pmic->rdev);
  860. kfree(pmic->info);
  861. kfree(pmic->desc);
  862. kfree(pmic);
  863. return 0;
  864. }
  865. static struct platform_driver tps65910_driver = {
  866. .driver = {
  867. .name = "tps65910-pmic",
  868. .owner = THIS_MODULE,
  869. },
  870. .probe = tps65910_probe,
  871. .remove = __devexit_p(tps65910_remove),
  872. };
  873. static int __init tps65910_init(void)
  874. {
  875. return platform_driver_register(&tps65910_driver);
  876. }
  877. subsys_initcall(tps65910_init);
  878. static void __exit tps65910_cleanup(void)
  879. {
  880. platform_driver_unregister(&tps65910_driver);
  881. }
  882. module_exit(tps65910_cleanup);
  883. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  884. MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
  885. MODULE_LICENSE("GPL v2");
  886. MODULE_ALIAS("platform:tps65910-pmic");