tps65910-regulator.c 23 KB

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