twl-regulator.c 29 KB

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