tps65910-regulator.c 22 KB

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