twl-regulator.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /*
  2. * twl-regulator.c -- support regulators in twl4030/twl6030 family chips
  3. *
  4. * Copyright (C) 2008 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/delay.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regulator/machine.h>
  18. #include <linux/i2c/twl.h>
  19. /*
  20. * The TWL4030/TW5030/TPS659x0/TWL6030 family chips include power management, a
  21. * USB OTG transceiver, an RTC, ADC, PWM, and lots more. Some versions
  22. * include an audio codec, battery charger, and more voltage regulators.
  23. * These chips are often used in OMAP-based systems.
  24. *
  25. * This driver implements software-based resource control for various
  26. * voltage regulators. This is usually augmented with state machine
  27. * based control.
  28. */
  29. struct twlreg_info {
  30. /* start of regulator's PM_RECEIVER control register bank */
  31. u8 base;
  32. /* twl resource ID, for resource control state machine */
  33. u8 id;
  34. /* voltage in mV = table[VSEL]; table_len must be a power-of-two */
  35. u8 table_len;
  36. const u16 *table;
  37. /* regulator specific turn-on delay */
  38. u16 delay;
  39. /* State REMAP default configuration */
  40. u8 remap;
  41. /* chip constraints on regulator behavior */
  42. u16 min_mV;
  43. u16 max_mV;
  44. u8 flags;
  45. /* used by regulator core */
  46. struct regulator_desc desc;
  47. /* chip specific features */
  48. unsigned long features;
  49. };
  50. /* LDO control registers ... offset is from the base of its register bank.
  51. * The first three registers of all power resource banks help hardware to
  52. * manage the various resource groups.
  53. */
  54. /* Common offset in TWL4030/6030 */
  55. #define VREG_GRP 0
  56. /* TWL4030 register offsets */
  57. #define VREG_TYPE 1
  58. #define VREG_REMAP 2
  59. #define VREG_DEDICATED 3 /* LDO control */
  60. /* TWL6030 register offsets */
  61. #define VREG_TRANS 1
  62. #define VREG_STATE 2
  63. #define VREG_VOLTAGE 3
  64. #define VREG_VOLTAGE_SMPS 4
  65. /* TWL6030 Misc register offsets */
  66. #define VREG_BC_ALL 1
  67. #define VREG_BC_REF 2
  68. #define VREG_BC_PROC 3
  69. #define VREG_BC_CLK_RST 4
  70. /* TWL6030 LDO register values for CFG_STATE */
  71. #define TWL6030_CFG_STATE_OFF 0x00
  72. #define TWL6030_CFG_STATE_ON 0x01
  73. #define TWL6030_CFG_STATE_OFF2 0x02
  74. #define TWL6030_CFG_STATE_SLEEP 0x03
  75. #define TWL6030_CFG_STATE_GRP_SHIFT 5
  76. #define TWL6030_CFG_STATE_APP_SHIFT 2
  77. #define TWL6030_CFG_STATE_APP_MASK (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
  78. #define TWL6030_CFG_STATE_APP(v) (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
  79. TWL6030_CFG_STATE_APP_SHIFT)
  80. /* Flags for SMPS Voltage reading */
  81. #define SMPS_OFFSET_EN BIT(0)
  82. #define SMPS_EXTENDED_EN BIT(1)
  83. /* twl6025 SMPS EPROM values */
  84. #define TWL6030_SMPS_OFFSET 0xB0
  85. #define TWL6030_SMPS_MULT 0xB3
  86. #define SMPS_MULTOFFSET_SMPS4 BIT(0)
  87. #define SMPS_MULTOFFSET_VIO BIT(1)
  88. #define SMPS_MULTOFFSET_SMPS3 BIT(6)
  89. static inline int
  90. twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
  91. {
  92. u8 value;
  93. int status;
  94. status = twl_i2c_read_u8(slave_subgp,
  95. &value, info->base + offset);
  96. return (status < 0) ? status : value;
  97. }
  98. static inline int
  99. twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
  100. u8 value)
  101. {
  102. return twl_i2c_write_u8(slave_subgp,
  103. value, info->base + offset);
  104. }
  105. /*----------------------------------------------------------------------*/
  106. /* generic power resource operations, which work on all regulators */
  107. static int twlreg_grp(struct regulator_dev *rdev)
  108. {
  109. return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
  110. VREG_GRP);
  111. }
  112. /*
  113. * Enable/disable regulators by joining/leaving the P1 (processor) group.
  114. * We assume nobody else is updating the DEV_GRP registers.
  115. */
  116. /* definition for 4030 family */
  117. #define P3_GRP_4030 BIT(7) /* "peripherals" */
  118. #define P2_GRP_4030 BIT(6) /* secondary processor, modem, etc */
  119. #define P1_GRP_4030 BIT(5) /* CPU/Linux */
  120. /* definition for 6030 family */
  121. #define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
  122. #define P2_GRP_6030 BIT(1) /* "peripherals" */
  123. #define P1_GRP_6030 BIT(0) /* CPU/Linux */
  124. static int twl4030reg_is_enabled(struct regulator_dev *rdev)
  125. {
  126. int state = twlreg_grp(rdev);
  127. if (state < 0)
  128. return state;
  129. return state & P1_GRP_4030;
  130. }
  131. static int twl6030reg_is_enabled(struct regulator_dev *rdev)
  132. {
  133. struct twlreg_info *info = rdev_get_drvdata(rdev);
  134. int grp = 0, val;
  135. if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
  136. grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
  137. if (grp < 0)
  138. return grp;
  139. if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
  140. grp &= P1_GRP_6030;
  141. else
  142. grp = 1;
  143. val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
  144. val = TWL6030_CFG_STATE_APP(val);
  145. return grp && (val == TWL6030_CFG_STATE_ON);
  146. }
  147. static int twl4030reg_enable(struct regulator_dev *rdev)
  148. {
  149. struct twlreg_info *info = rdev_get_drvdata(rdev);
  150. int grp;
  151. int ret;
  152. grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
  153. if (grp < 0)
  154. return grp;
  155. grp |= P1_GRP_4030;
  156. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
  157. udelay(info->delay);
  158. return ret;
  159. }
  160. static int twl6030reg_enable(struct regulator_dev *rdev)
  161. {
  162. struct twlreg_info *info = rdev_get_drvdata(rdev);
  163. int grp = 0;
  164. int ret;
  165. if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
  166. grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
  167. if (grp < 0)
  168. return grp;
  169. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
  170. grp << TWL6030_CFG_STATE_GRP_SHIFT |
  171. TWL6030_CFG_STATE_ON);
  172. udelay(info->delay);
  173. return ret;
  174. }
  175. static int twl4030reg_disable(struct regulator_dev *rdev)
  176. {
  177. struct twlreg_info *info = rdev_get_drvdata(rdev);
  178. int grp;
  179. int ret;
  180. grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
  181. if (grp < 0)
  182. return grp;
  183. grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
  184. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
  185. return ret;
  186. }
  187. static int twl6030reg_disable(struct regulator_dev *rdev)
  188. {
  189. struct twlreg_info *info = rdev_get_drvdata(rdev);
  190. int grp = 0;
  191. int ret;
  192. if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
  193. grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
  194. /* For 6030, set the off state for all grps enabled */
  195. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
  196. (grp) << TWL6030_CFG_STATE_GRP_SHIFT |
  197. TWL6030_CFG_STATE_OFF);
  198. return ret;
  199. }
  200. static int twl4030reg_get_status(struct regulator_dev *rdev)
  201. {
  202. int state = twlreg_grp(rdev);
  203. if (state < 0)
  204. return state;
  205. state &= 0x0f;
  206. /* assume state != WARM_RESET; we'd not be running... */
  207. if (!state)
  208. return REGULATOR_STATUS_OFF;
  209. return (state & BIT(3))
  210. ? REGULATOR_STATUS_NORMAL
  211. : REGULATOR_STATUS_STANDBY;
  212. }
  213. static int twl6030reg_get_status(struct regulator_dev *rdev)
  214. {
  215. struct twlreg_info *info = rdev_get_drvdata(rdev);
  216. int val;
  217. val = twlreg_grp(rdev);
  218. if (val < 0)
  219. return val;
  220. val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
  221. switch (TWL6030_CFG_STATE_APP(val)) {
  222. case TWL6030_CFG_STATE_ON:
  223. return REGULATOR_STATUS_NORMAL;
  224. case TWL6030_CFG_STATE_SLEEP:
  225. return REGULATOR_STATUS_STANDBY;
  226. case TWL6030_CFG_STATE_OFF:
  227. case TWL6030_CFG_STATE_OFF2:
  228. default:
  229. break;
  230. }
  231. return REGULATOR_STATUS_OFF;
  232. }
  233. static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
  234. {
  235. struct twlreg_info *info = rdev_get_drvdata(rdev);
  236. unsigned message;
  237. int status;
  238. /* We can only set the mode through state machine commands... */
  239. switch (mode) {
  240. case REGULATOR_MODE_NORMAL:
  241. message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
  242. break;
  243. case REGULATOR_MODE_STANDBY:
  244. message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
  245. break;
  246. default:
  247. return -EINVAL;
  248. }
  249. /* Ensure the resource is associated with some group */
  250. status = twlreg_grp(rdev);
  251. if (status < 0)
  252. return status;
  253. if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030)))
  254. return -EACCES;
  255. status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
  256. message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB);
  257. if (status < 0)
  258. return status;
  259. return twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
  260. message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB);
  261. }
  262. static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
  263. {
  264. struct twlreg_info *info = rdev_get_drvdata(rdev);
  265. int grp = 0;
  266. int val;
  267. if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
  268. grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
  269. if (grp < 0)
  270. return grp;
  271. /* Compose the state register settings */
  272. val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
  273. /* We can only set the mode through state machine commands... */
  274. switch (mode) {
  275. case REGULATOR_MODE_NORMAL:
  276. val |= TWL6030_CFG_STATE_ON;
  277. break;
  278. case REGULATOR_MODE_STANDBY:
  279. val |= TWL6030_CFG_STATE_SLEEP;
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
  285. }
  286. /*----------------------------------------------------------------------*/
  287. /*
  288. * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
  289. * select field in its control register. We use tables indexed by VSEL
  290. * to record voltages in milliVolts. (Accuracy is about three percent.)
  291. *
  292. * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
  293. * currently handled by listing two slightly different VAUX2 regulators,
  294. * only one of which will be configured.
  295. *
  296. * VSEL values documented as "TI cannot support these values" are flagged
  297. * in these tables as UNSUP() values; we normally won't assign them.
  298. *
  299. * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
  300. * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
  301. */
  302. #ifdef CONFIG_TWL4030_ALLOW_UNSUPPORTED
  303. #define UNSUP_MASK 0x0000
  304. #else
  305. #define UNSUP_MASK 0x8000
  306. #endif
  307. #define UNSUP(x) (UNSUP_MASK | (x))
  308. #define IS_UNSUP(x) (UNSUP_MASK & (x))
  309. #define LDO_MV(x) (~UNSUP_MASK & (x))
  310. static const u16 VAUX1_VSEL_table[] = {
  311. UNSUP(1500), UNSUP(1800), 2500, 2800,
  312. 3000, 3000, 3000, 3000,
  313. };
  314. static const u16 VAUX2_4030_VSEL_table[] = {
  315. UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
  316. 1500, 1800, UNSUP(1850), 2500,
  317. UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
  318. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  319. };
  320. static const u16 VAUX2_VSEL_table[] = {
  321. 1700, 1700, 1900, 1300,
  322. 1500, 1800, 2000, 2500,
  323. 2100, 2800, 2200, 2300,
  324. 2400, 2400, 2400, 2400,
  325. };
  326. static const u16 VAUX3_VSEL_table[] = {
  327. 1500, 1800, 2500, 2800,
  328. 3000, 3000, 3000, 3000,
  329. };
  330. static const u16 VAUX4_VSEL_table[] = {
  331. 700, 1000, 1200, UNSUP(1300),
  332. 1500, 1800, UNSUP(1850), 2500,
  333. UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
  334. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  335. };
  336. static const u16 VMMC1_VSEL_table[] = {
  337. 1850, 2850, 3000, 3150,
  338. };
  339. static const u16 VMMC2_VSEL_table[] = {
  340. UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
  341. UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
  342. 2600, 2800, 2850, 3000,
  343. 3150, 3150, 3150, 3150,
  344. };
  345. static const u16 VPLL1_VSEL_table[] = {
  346. 1000, 1200, 1300, 1800,
  347. UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
  348. };
  349. static const u16 VPLL2_VSEL_table[] = {
  350. 700, 1000, 1200, 1300,
  351. UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
  352. UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
  353. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  354. };
  355. static const u16 VSIM_VSEL_table[] = {
  356. UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
  357. 2800, 3000, 3000, 3000,
  358. };
  359. static const u16 VDAC_VSEL_table[] = {
  360. 1200, 1300, 1800, 1800,
  361. };
  362. static const u16 VDD1_VSEL_table[] = {
  363. 800, 1450,
  364. };
  365. static const u16 VDD2_VSEL_table[] = {
  366. 800, 1450, 1500,
  367. };
  368. static const u16 VIO_VSEL_table[] = {
  369. 1800, 1850,
  370. };
  371. static const u16 VINTANA2_VSEL_table[] = {
  372. 2500, 2750,
  373. };
  374. static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
  375. {
  376. struct twlreg_info *info = rdev_get_drvdata(rdev);
  377. int mV = info->table[index];
  378. return IS_UNSUP(mV) ? 0 : (LDO_MV(mV) * 1000);
  379. }
  380. static int
  381. twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
  382. unsigned *selector)
  383. {
  384. struct twlreg_info *info = rdev_get_drvdata(rdev);
  385. int vsel;
  386. for (vsel = 0; vsel < info->table_len; vsel++) {
  387. int mV = info->table[vsel];
  388. int uV;
  389. if (IS_UNSUP(mV))
  390. continue;
  391. uV = LDO_MV(mV) * 1000;
  392. /* REVISIT for VAUX2, first match may not be best/lowest */
  393. /* use the first in-range value */
  394. if (min_uV <= uV && uV <= max_uV) {
  395. *selector = vsel;
  396. return twlreg_write(info, TWL_MODULE_PM_RECEIVER,
  397. VREG_VOLTAGE, vsel);
  398. }
  399. }
  400. return -EDOM;
  401. }
  402. static int twl4030ldo_get_voltage(struct regulator_dev *rdev)
  403. {
  404. struct twlreg_info *info = rdev_get_drvdata(rdev);
  405. int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
  406. VREG_VOLTAGE);
  407. if (vsel < 0)
  408. return vsel;
  409. vsel &= info->table_len - 1;
  410. return LDO_MV(info->table[vsel]) * 1000;
  411. }
  412. static struct regulator_ops twl4030ldo_ops = {
  413. .list_voltage = twl4030ldo_list_voltage,
  414. .set_voltage = twl4030ldo_set_voltage,
  415. .get_voltage = twl4030ldo_get_voltage,
  416. .enable = twl4030reg_enable,
  417. .disable = twl4030reg_disable,
  418. .is_enabled = twl4030reg_is_enabled,
  419. .set_mode = twl4030reg_set_mode,
  420. .get_status = twl4030reg_get_status,
  421. };
  422. static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
  423. {
  424. struct twlreg_info *info = rdev_get_drvdata(rdev);
  425. return ((info->min_mV + (index * 100)) * 1000);
  426. }
  427. static int
  428. twl6030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
  429. unsigned *selector)
  430. {
  431. struct twlreg_info *info = rdev_get_drvdata(rdev);
  432. int vsel;
  433. if ((min_uV/1000 < info->min_mV) || (max_uV/1000 > info->max_mV))
  434. return -EDOM;
  435. /*
  436. * Use the below formula to calculate vsel
  437. * mV = 1000mv + 100mv * (vsel - 1)
  438. */
  439. vsel = (min_uV/1000 - 1000)/100 + 1;
  440. *selector = vsel;
  441. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE, vsel);
  442. }
  443. static int twl6030ldo_get_voltage(struct regulator_dev *rdev)
  444. {
  445. struct twlreg_info *info = rdev_get_drvdata(rdev);
  446. int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
  447. VREG_VOLTAGE);
  448. if (vsel < 0)
  449. return vsel;
  450. /*
  451. * Use the below formula to calculate vsel
  452. * mV = 1000mv + 100mv * (vsel - 1)
  453. */
  454. return (1000 + (100 * (vsel - 1))) * 1000;
  455. }
  456. static struct regulator_ops twl6030ldo_ops = {
  457. .list_voltage = twl6030ldo_list_voltage,
  458. .set_voltage = twl6030ldo_set_voltage,
  459. .get_voltage = twl6030ldo_get_voltage,
  460. .enable = twl6030reg_enable,
  461. .disable = twl6030reg_disable,
  462. .is_enabled = twl6030reg_is_enabled,
  463. .set_mode = twl6030reg_set_mode,
  464. .get_status = twl6030reg_get_status,
  465. };
  466. /*----------------------------------------------------------------------*/
  467. /*
  468. * Fixed voltage LDOs don't have a VSEL field to update.
  469. */
  470. static int twlfixed_list_voltage(struct regulator_dev *rdev, unsigned index)
  471. {
  472. struct twlreg_info *info = rdev_get_drvdata(rdev);
  473. return info->min_mV * 1000;
  474. }
  475. static int twlfixed_get_voltage(struct regulator_dev *rdev)
  476. {
  477. struct twlreg_info *info = rdev_get_drvdata(rdev);
  478. return info->min_mV * 1000;
  479. }
  480. static struct regulator_ops twl4030fixed_ops = {
  481. .list_voltage = twlfixed_list_voltage,
  482. .get_voltage = twlfixed_get_voltage,
  483. .enable = twl4030reg_enable,
  484. .disable = twl4030reg_disable,
  485. .is_enabled = twl4030reg_is_enabled,
  486. .set_mode = twl4030reg_set_mode,
  487. .get_status = twl4030reg_get_status,
  488. };
  489. static struct regulator_ops twl6030fixed_ops = {
  490. .list_voltage = twlfixed_list_voltage,
  491. .get_voltage = twlfixed_get_voltage,
  492. .enable = twl6030reg_enable,
  493. .disable = twl6030reg_disable,
  494. .is_enabled = twl6030reg_is_enabled,
  495. .set_mode = twl6030reg_set_mode,
  496. .get_status = twl6030reg_get_status,
  497. };
  498. static struct regulator_ops twl6030_fixed_resource = {
  499. .enable = twl6030reg_enable,
  500. .disable = twl6030reg_disable,
  501. .is_enabled = twl6030reg_is_enabled,
  502. .get_status = twl6030reg_get_status,
  503. };
  504. /*
  505. * SMPS status and control
  506. */
  507. static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
  508. {
  509. struct twlreg_info *info = rdev_get_drvdata(rdev);
  510. int voltage = 0;
  511. switch (info->flags) {
  512. case SMPS_OFFSET_EN:
  513. voltage = 100000;
  514. /* fall through */
  515. case 0:
  516. switch (index) {
  517. case 0:
  518. voltage = 0;
  519. break;
  520. case 58:
  521. voltage = 1350 * 1000;
  522. break;
  523. case 59:
  524. voltage = 1500 * 1000;
  525. break;
  526. case 60:
  527. voltage = 1800 * 1000;
  528. break;
  529. case 61:
  530. voltage = 1900 * 1000;
  531. break;
  532. case 62:
  533. voltage = 2100 * 1000;
  534. break;
  535. default:
  536. voltage += (600000 + (12500 * (index - 1)));
  537. }
  538. break;
  539. case SMPS_EXTENDED_EN:
  540. switch (index) {
  541. case 0:
  542. voltage = 0;
  543. break;
  544. case 58:
  545. voltage = 2084 * 1000;
  546. break;
  547. case 59:
  548. voltage = 2315 * 1000;
  549. break;
  550. case 60:
  551. voltage = 2778 * 1000;
  552. break;
  553. case 61:
  554. voltage = 2932 * 1000;
  555. break;
  556. case 62:
  557. voltage = 3241 * 1000;
  558. break;
  559. default:
  560. voltage = (1852000 + (38600 * (index - 1)));
  561. }
  562. break;
  563. case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
  564. switch (index) {
  565. case 0:
  566. voltage = 0;
  567. break;
  568. case 58:
  569. voltage = 4167 * 1000;
  570. break;
  571. case 59:
  572. voltage = 2315 * 1000;
  573. break;
  574. case 60:
  575. voltage = 2778 * 1000;
  576. break;
  577. case 61:
  578. voltage = 2932 * 1000;
  579. break;
  580. case 62:
  581. voltage = 3241 * 1000;
  582. break;
  583. default:
  584. voltage = (2161000 + (38600 * (index - 1)));
  585. }
  586. break;
  587. }
  588. return voltage;
  589. }
  590. static int
  591. twl6030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
  592. unsigned int *selector)
  593. {
  594. struct twlreg_info *info = rdev_get_drvdata(rdev);
  595. int vsel = 0;
  596. switch (info->flags) {
  597. case 0:
  598. if (min_uV == 0)
  599. vsel = 0;
  600. else if ((min_uV >= 600000) && (max_uV <= 1300000)) {
  601. vsel = (min_uV - 600000) / 125;
  602. if (vsel % 100)
  603. vsel += 100;
  604. vsel /= 100;
  605. vsel++;
  606. }
  607. /* Values 1..57 for vsel are linear and can be calculated
  608. * values 58..62 are non linear.
  609. */
  610. else if ((min_uV > 1900000) && (max_uV >= 2100000))
  611. vsel = 62;
  612. else if ((min_uV > 1800000) && (max_uV >= 1900000))
  613. vsel = 61;
  614. else if ((min_uV > 1500000) && (max_uV >= 1800000))
  615. vsel = 60;
  616. else if ((min_uV > 1350000) && (max_uV >= 1500000))
  617. vsel = 59;
  618. else if ((min_uV > 1300000) && (max_uV >= 1350000))
  619. vsel = 58;
  620. else
  621. return -EINVAL;
  622. break;
  623. case SMPS_OFFSET_EN:
  624. if (min_uV == 0)
  625. vsel = 0;
  626. else if ((min_uV >= 700000) && (max_uV <= 1420000)) {
  627. vsel = (min_uV - 700000) / 125;
  628. if (vsel % 100)
  629. vsel += 100;
  630. vsel /= 100;
  631. vsel++;
  632. }
  633. /* Values 1..57 for vsel are linear and can be calculated
  634. * values 58..62 are non linear.
  635. */
  636. else if ((min_uV > 1900000) && (max_uV >= 2100000))
  637. vsel = 62;
  638. else if ((min_uV > 1800000) && (max_uV >= 1900000))
  639. vsel = 61;
  640. else if ((min_uV > 1350000) && (max_uV >= 1800000))
  641. vsel = 60;
  642. else if ((min_uV > 1350000) && (max_uV >= 1500000))
  643. vsel = 59;
  644. else if ((min_uV > 1300000) && (max_uV >= 1350000))
  645. vsel = 58;
  646. else
  647. return -EINVAL;
  648. break;
  649. case SMPS_EXTENDED_EN:
  650. if (min_uV == 0)
  651. vsel = 0;
  652. else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
  653. vsel = (min_uV - 1852000) / 386;
  654. if (vsel % 100)
  655. vsel += 100;
  656. vsel /= 100;
  657. vsel++;
  658. }
  659. break;
  660. case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
  661. if (min_uV == 0)
  662. vsel = 0;
  663. else if ((min_uV >= 2161000) && (max_uV <= 4321000)) {
  664. vsel = (min_uV - 1852000) / 386;
  665. if (vsel % 100)
  666. vsel += 100;
  667. vsel /= 100;
  668. vsel++;
  669. }
  670. break;
  671. }
  672. *selector = vsel;
  673. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
  674. vsel);
  675. }
  676. static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
  677. {
  678. struct twlreg_info *info = rdev_get_drvdata(rdev);
  679. return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
  680. }
  681. static struct regulator_ops twlsmps_ops = {
  682. .list_voltage = twl6030smps_list_voltage,
  683. .set_voltage = twl6030smps_set_voltage,
  684. .get_voltage_sel = twl6030smps_get_voltage_sel,
  685. .enable = twl6030reg_enable,
  686. .disable = twl6030reg_disable,
  687. .is_enabled = twl6030reg_is_enabled,
  688. .set_mode = twl6030reg_set_mode,
  689. .get_status = twl6030reg_get_status,
  690. };
  691. /*----------------------------------------------------------------------*/
  692. #define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
  693. remap_conf) \
  694. TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
  695. remap_conf, TWL4030, twl4030fixed_ops)
  696. #define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
  697. TWL_FIXED_LDO(label, offset, mVolts, 0x0, turnon_delay, \
  698. 0x0, TWL6030, twl6030fixed_ops)
  699. #define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) { \
  700. .base = offset, \
  701. .id = num, \
  702. .table_len = ARRAY_SIZE(label##_VSEL_table), \
  703. .table = label##_VSEL_table, \
  704. .delay = turnon_delay, \
  705. .remap = remap_conf, \
  706. .desc = { \
  707. .name = #label, \
  708. .id = TWL4030_REG_##label, \
  709. .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
  710. .ops = &twl4030ldo_ops, \
  711. .type = REGULATOR_VOLTAGE, \
  712. .owner = THIS_MODULE, \
  713. }, \
  714. }
  715. #define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
  716. .base = offset, \
  717. .min_mV = min_mVolts, \
  718. .max_mV = max_mVolts, \
  719. .desc = { \
  720. .name = #label, \
  721. .id = TWL6030_REG_##label, \
  722. .n_voltages = (max_mVolts - min_mVolts)/100 + 1, \
  723. .ops = &twl6030ldo_ops, \
  724. .type = REGULATOR_VOLTAGE, \
  725. .owner = THIS_MODULE, \
  726. }, \
  727. }
  728. #define TWL6025_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
  729. .base = offset, \
  730. .min_mV = min_mVolts, \
  731. .max_mV = max_mVolts, \
  732. .desc = { \
  733. .name = #label, \
  734. .id = TWL6025_REG_##label, \
  735. .n_voltages = ((max_mVolts - min_mVolts)/100) + 1, \
  736. .ops = &twl6030ldo_ops, \
  737. .type = REGULATOR_VOLTAGE, \
  738. .owner = THIS_MODULE, \
  739. }, \
  740. }
  741. #define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
  742. family, operations) { \
  743. .base = offset, \
  744. .id = num, \
  745. .min_mV = mVolts, \
  746. .delay = turnon_delay, \
  747. .remap = remap_conf, \
  748. .desc = { \
  749. .name = #label, \
  750. .id = family##_REG_##label, \
  751. .n_voltages = 1, \
  752. .ops = &operations, \
  753. .type = REGULATOR_VOLTAGE, \
  754. .owner = THIS_MODULE, \
  755. }, \
  756. }
  757. #define TWL6030_FIXED_RESOURCE(label, offset, turnon_delay) { \
  758. .base = offset, \
  759. .delay = turnon_delay, \
  760. .desc = { \
  761. .name = #label, \
  762. .id = TWL6030_REG_##label, \
  763. .ops = &twl6030_fixed_resource, \
  764. .type = REGULATOR_VOLTAGE, \
  765. .owner = THIS_MODULE, \
  766. }, \
  767. }
  768. #define TWL6025_ADJUSTABLE_SMPS(label, offset) { \
  769. .base = offset, \
  770. .min_mV = 600, \
  771. .max_mV = 2100, \
  772. .desc = { \
  773. .name = #label, \
  774. .id = TWL6025_REG_##label, \
  775. .n_voltages = 63, \
  776. .ops = &twlsmps_ops, \
  777. .type = REGULATOR_VOLTAGE, \
  778. .owner = THIS_MODULE, \
  779. }, \
  780. }
  781. /*
  782. * We list regulators here if systems need some level of
  783. * software control over them after boot.
  784. */
  785. static struct twlreg_info twl_regs[] = {
  786. TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08),
  787. TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08),
  788. TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08),
  789. TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08),
  790. TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08),
  791. TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08),
  792. TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08),
  793. TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00),
  794. TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08),
  795. TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00),
  796. TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08),
  797. TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08),
  798. TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08),
  799. TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08),
  800. TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08),
  801. TWL4030_ADJUSTABLE_LDO(VDD1, 0x55, 15, 1000, 0x08),
  802. TWL4030_ADJUSTABLE_LDO(VDD2, 0x63, 16, 1000, 0x08),
  803. TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08),
  804. TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08),
  805. TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08),
  806. /* VUSBCP is managed *only* by the USB subchip */
  807. /* 6030 REG with base as PMC Slave Misc : 0x0030 */
  808. /* Turnon-delay and remap configuration values for 6030 are not
  809. verified since the specification is not public */
  810. TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300),
  811. TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300),
  812. TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300),
  813. TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300),
  814. TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300),
  815. TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300),
  816. TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0),
  817. TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0),
  818. TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0),
  819. TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0),
  820. TWL6030_FIXED_RESOURCE(CLK32KG, 0x8C, 0),
  821. /* 6025 are renamed compared to 6030 versions */
  822. TWL6025_ADJUSTABLE_LDO(LDO2, 0x54, 1000, 3300),
  823. TWL6025_ADJUSTABLE_LDO(LDO4, 0x58, 1000, 3300),
  824. TWL6025_ADJUSTABLE_LDO(LDO3, 0x5c, 1000, 3300),
  825. TWL6025_ADJUSTABLE_LDO(LDO5, 0x68, 1000, 3300),
  826. TWL6025_ADJUSTABLE_LDO(LDO1, 0x6c, 1000, 3300),
  827. TWL6025_ADJUSTABLE_LDO(LDO7, 0x74, 1000, 3300),
  828. TWL6025_ADJUSTABLE_LDO(LDO6, 0x60, 1000, 3300),
  829. TWL6025_ADJUSTABLE_LDO(LDOLN, 0x64, 1000, 3300),
  830. TWL6025_ADJUSTABLE_LDO(LDOUSB, 0x70, 1000, 3300),
  831. TWL6025_ADJUSTABLE_SMPS(SMPS3, 0x34),
  832. TWL6025_ADJUSTABLE_SMPS(SMPS4, 0x10),
  833. TWL6025_ADJUSTABLE_SMPS(VIO, 0x16),
  834. };
  835. static u8 twl_get_smps_offset(void)
  836. {
  837. u8 value;
  838. twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
  839. TWL6030_SMPS_OFFSET);
  840. return value;
  841. }
  842. static u8 twl_get_smps_mult(void)
  843. {
  844. u8 value;
  845. twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
  846. TWL6030_SMPS_MULT);
  847. return value;
  848. }
  849. static int __devinit twlreg_probe(struct platform_device *pdev)
  850. {
  851. int i;
  852. struct twlreg_info *info;
  853. struct regulator_init_data *initdata;
  854. struct regulation_constraints *c;
  855. struct regulator_dev *rdev;
  856. for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
  857. if (twl_regs[i].desc.id != pdev->id)
  858. continue;
  859. info = twl_regs + i;
  860. break;
  861. }
  862. if (!info)
  863. return -ENODEV;
  864. initdata = pdev->dev.platform_data;
  865. if (!initdata)
  866. return -EINVAL;
  867. /* copy the features into regulator data */
  868. info->features = (unsigned long)initdata->driver_data;
  869. /* Constrain board-specific capabilities according to what
  870. * this driver and the chip itself can actually do.
  871. */
  872. c = &initdata->constraints;
  873. c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
  874. c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
  875. | REGULATOR_CHANGE_MODE
  876. | REGULATOR_CHANGE_STATUS;
  877. switch (pdev->id) {
  878. case TWL4030_REG_VIO:
  879. case TWL4030_REG_VDD1:
  880. case TWL4030_REG_VDD2:
  881. case TWL4030_REG_VPLL1:
  882. case TWL4030_REG_VINTANA1:
  883. case TWL4030_REG_VINTANA2:
  884. case TWL4030_REG_VINTDIG:
  885. c->always_on = true;
  886. break;
  887. default:
  888. break;
  889. }
  890. switch (pdev->id) {
  891. case TWL6025_REG_SMPS3:
  892. if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
  893. info->flags |= SMPS_EXTENDED_EN;
  894. if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
  895. info->flags |= SMPS_OFFSET_EN;
  896. break;
  897. case TWL6025_REG_SMPS4:
  898. if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
  899. info->flags |= SMPS_EXTENDED_EN;
  900. if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
  901. info->flags |= SMPS_OFFSET_EN;
  902. break;
  903. case TWL6025_REG_VIO:
  904. if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
  905. info->flags |= SMPS_EXTENDED_EN;
  906. if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
  907. info->flags |= SMPS_OFFSET_EN;
  908. break;
  909. }
  910. rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
  911. if (IS_ERR(rdev)) {
  912. dev_err(&pdev->dev, "can't register %s, %ld\n",
  913. info->desc.name, PTR_ERR(rdev));
  914. return PTR_ERR(rdev);
  915. }
  916. platform_set_drvdata(pdev, rdev);
  917. if (twl_class_is_4030())
  918. twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
  919. info->remap);
  920. /* NOTE: many regulators support short-circuit IRQs (presentable
  921. * as REGULATOR_OVER_CURRENT notifications?) configured via:
  922. * - SC_CONFIG
  923. * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
  924. * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
  925. * - IT_CONFIG
  926. */
  927. return 0;
  928. }
  929. static int __devexit twlreg_remove(struct platform_device *pdev)
  930. {
  931. regulator_unregister(platform_get_drvdata(pdev));
  932. return 0;
  933. }
  934. MODULE_ALIAS("platform:twl_reg");
  935. static struct platform_driver twlreg_driver = {
  936. .probe = twlreg_probe,
  937. .remove = __devexit_p(twlreg_remove),
  938. /* NOTE: short name, to work around driver model truncation of
  939. * "twl_regulator.12" (and friends) to "twl_regulator.1".
  940. */
  941. .driver.name = "twl_reg",
  942. .driver.owner = THIS_MODULE,
  943. };
  944. static int __init twlreg_init(void)
  945. {
  946. return platform_driver_register(&twlreg_driver);
  947. }
  948. subsys_initcall(twlreg_init);
  949. static void __exit twlreg_exit(void)
  950. {
  951. platform_driver_unregister(&twlreg_driver);
  952. }
  953. module_exit(twlreg_exit)
  954. MODULE_DESCRIPTION("TWL regulator driver");
  955. MODULE_LICENSE("GPL");