tps65910-regulator.c 23 KB

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