wm8350-regulator.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. /*
  2. * wm8350.c -- Voltage and current regulation for the Wolfson WM8350 PMIC
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood
  7. * linux@wolfsonmicro.com
  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. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/bitops.h>
  18. #include <linux/err.h>
  19. #include <linux/i2c.h>
  20. #include <linux/mfd/wm8350/core.h>
  21. #include <linux/mfd/wm8350/pmic.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regulator/driver.h>
  24. #include <linux/regulator/machine.h>
  25. /* Maximum value possible for VSEL */
  26. #define WM8350_DCDC_MAX_VSEL 0x66
  27. /* Microamps */
  28. static const int isink_cur[] = {
  29. 4,
  30. 5,
  31. 6,
  32. 7,
  33. 8,
  34. 10,
  35. 11,
  36. 14,
  37. 16,
  38. 19,
  39. 23,
  40. 27,
  41. 32,
  42. 39,
  43. 46,
  44. 54,
  45. 65,
  46. 77,
  47. 92,
  48. 109,
  49. 130,
  50. 154,
  51. 183,
  52. 218,
  53. 259,
  54. 308,
  55. 367,
  56. 436,
  57. 518,
  58. 616,
  59. 733,
  60. 872,
  61. 1037,
  62. 1233,
  63. 1466,
  64. 1744,
  65. 2073,
  66. 2466,
  67. 2933,
  68. 3487,
  69. 4147,
  70. 4932,
  71. 5865,
  72. 6975,
  73. 8294,
  74. 9864,
  75. 11730,
  76. 13949,
  77. 16589,
  78. 19728,
  79. 23460,
  80. 27899,
  81. 33178,
  82. 39455,
  83. 46920,
  84. 55798,
  85. 66355,
  86. 78910,
  87. 93840,
  88. 111596,
  89. 132710,
  90. 157820,
  91. 187681,
  92. 223191
  93. };
  94. static int get_isink_val(int min_uA, int max_uA, u16 *setting)
  95. {
  96. int i;
  97. for (i = ARRAY_SIZE(isink_cur) - 1; i >= 0; i--) {
  98. if (min_uA <= isink_cur[i] && max_uA >= isink_cur[i]) {
  99. *setting = i;
  100. return 0;
  101. }
  102. }
  103. return -EINVAL;
  104. }
  105. static inline int wm8350_ldo_val_to_mvolts(unsigned int val)
  106. {
  107. if (val < 16)
  108. return (val * 50) + 900;
  109. else
  110. return ((val - 16) * 100) + 1800;
  111. }
  112. static inline unsigned int wm8350_ldo_mvolts_to_val(int mV)
  113. {
  114. if (mV < 1800)
  115. return (mV - 900) / 50;
  116. else
  117. return ((mV - 1800) / 100) + 16;
  118. }
  119. static inline int wm8350_dcdc_val_to_mvolts(unsigned int val)
  120. {
  121. return (val * 25) + 850;
  122. }
  123. static inline unsigned int wm8350_dcdc_mvolts_to_val(int mV)
  124. {
  125. return (mV - 850) / 25;
  126. }
  127. static int wm8350_isink_set_current(struct regulator_dev *rdev, int min_uA,
  128. int max_uA)
  129. {
  130. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  131. int isink = rdev_get_id(rdev);
  132. u16 val, setting;
  133. int ret;
  134. ret = get_isink_val(min_uA, max_uA, &setting);
  135. if (ret != 0)
  136. return ret;
  137. switch (isink) {
  138. case WM8350_ISINK_A:
  139. val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_A) &
  140. ~WM8350_CS1_ISEL_MASK;
  141. wm8350_reg_write(wm8350, WM8350_CURRENT_SINK_DRIVER_A,
  142. val | setting);
  143. break;
  144. case WM8350_ISINK_B:
  145. val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_B) &
  146. ~WM8350_CS1_ISEL_MASK;
  147. wm8350_reg_write(wm8350, WM8350_CURRENT_SINK_DRIVER_B,
  148. val | setting);
  149. break;
  150. default:
  151. return -EINVAL;
  152. }
  153. return 0;
  154. }
  155. static int wm8350_isink_get_current(struct regulator_dev *rdev)
  156. {
  157. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  158. int isink = rdev_get_id(rdev);
  159. u16 val;
  160. switch (isink) {
  161. case WM8350_ISINK_A:
  162. val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_A) &
  163. WM8350_CS1_ISEL_MASK;
  164. break;
  165. case WM8350_ISINK_B:
  166. val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_B) &
  167. WM8350_CS1_ISEL_MASK;
  168. break;
  169. default:
  170. return 0;
  171. }
  172. return (isink_cur[val] + 50) / 100;
  173. }
  174. /* turn on ISINK followed by DCDC */
  175. static int wm8350_isink_enable(struct regulator_dev *rdev)
  176. {
  177. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  178. int isink = rdev_get_id(rdev);
  179. switch (isink) {
  180. case WM8350_ISINK_A:
  181. switch (wm8350->pmic.isink_A_dcdc) {
  182. case WM8350_DCDC_2:
  183. case WM8350_DCDC_5:
  184. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_7,
  185. WM8350_CS1_ENA);
  186. wm8350_set_bits(wm8350, WM8350_CSA_FLASH_CONTROL,
  187. WM8350_CS1_DRIVE);
  188. wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
  189. 1 << (wm8350->pmic.isink_A_dcdc -
  190. WM8350_DCDC_1));
  191. break;
  192. default:
  193. return -EINVAL;
  194. }
  195. break;
  196. case WM8350_ISINK_B:
  197. switch (wm8350->pmic.isink_B_dcdc) {
  198. case WM8350_DCDC_2:
  199. case WM8350_DCDC_5:
  200. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_7,
  201. WM8350_CS2_ENA);
  202. wm8350_set_bits(wm8350, WM8350_CSB_FLASH_CONTROL,
  203. WM8350_CS2_DRIVE);
  204. wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
  205. 1 << (wm8350->pmic.isink_B_dcdc -
  206. WM8350_DCDC_1));
  207. break;
  208. default:
  209. return -EINVAL;
  210. }
  211. break;
  212. default:
  213. return -EINVAL;
  214. }
  215. return 0;
  216. }
  217. static int wm8350_isink_disable(struct regulator_dev *rdev)
  218. {
  219. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  220. int isink = rdev_get_id(rdev);
  221. switch (isink) {
  222. case WM8350_ISINK_A:
  223. switch (wm8350->pmic.isink_A_dcdc) {
  224. case WM8350_DCDC_2:
  225. case WM8350_DCDC_5:
  226. wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
  227. 1 << (wm8350->pmic.isink_A_dcdc -
  228. WM8350_DCDC_1));
  229. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_7,
  230. WM8350_CS1_ENA);
  231. break;
  232. default:
  233. return -EINVAL;
  234. }
  235. break;
  236. case WM8350_ISINK_B:
  237. switch (wm8350->pmic.isink_B_dcdc) {
  238. case WM8350_DCDC_2:
  239. case WM8350_DCDC_5:
  240. wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
  241. 1 << (wm8350->pmic.isink_B_dcdc -
  242. WM8350_DCDC_1));
  243. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_7,
  244. WM8350_CS2_ENA);
  245. break;
  246. default:
  247. return -EINVAL;
  248. }
  249. break;
  250. default:
  251. return -EINVAL;
  252. }
  253. return 0;
  254. }
  255. static int wm8350_isink_is_enabled(struct regulator_dev *rdev)
  256. {
  257. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  258. int isink = rdev_get_id(rdev);
  259. switch (isink) {
  260. case WM8350_ISINK_A:
  261. return wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_A) &
  262. 0x8000;
  263. case WM8350_ISINK_B:
  264. return wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_B) &
  265. 0x8000;
  266. }
  267. return -EINVAL;
  268. }
  269. static int wm8350_isink_enable_time(struct regulator_dev *rdev)
  270. {
  271. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  272. int isink = rdev_get_id(rdev);
  273. int reg;
  274. switch (isink) {
  275. case WM8350_ISINK_A:
  276. reg = wm8350_reg_read(wm8350, WM8350_CSA_FLASH_CONTROL);
  277. break;
  278. case WM8350_ISINK_B:
  279. reg = wm8350_reg_read(wm8350, WM8350_CSB_FLASH_CONTROL);
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. if (reg & WM8350_CS1_FLASH_MODE) {
  285. switch (reg & WM8350_CS1_ON_RAMP_MASK) {
  286. case 0:
  287. return 0;
  288. case 1:
  289. return 1950;
  290. case 2:
  291. return 3910;
  292. case 3:
  293. return 7800;
  294. }
  295. } else {
  296. switch (reg & WM8350_CS1_ON_RAMP_MASK) {
  297. case 0:
  298. return 0;
  299. case 1:
  300. return 250000;
  301. case 2:
  302. return 500000;
  303. case 3:
  304. return 1000000;
  305. }
  306. }
  307. return -EINVAL;
  308. }
  309. int wm8350_isink_set_flash(struct wm8350 *wm8350, int isink, u16 mode,
  310. u16 trigger, u16 duration, u16 on_ramp, u16 off_ramp,
  311. u16 drive)
  312. {
  313. switch (isink) {
  314. case WM8350_ISINK_A:
  315. wm8350_reg_write(wm8350, WM8350_CSA_FLASH_CONTROL,
  316. (mode ? WM8350_CS1_FLASH_MODE : 0) |
  317. (trigger ? WM8350_CS1_TRIGSRC : 0) |
  318. duration | on_ramp | off_ramp | drive);
  319. break;
  320. case WM8350_ISINK_B:
  321. wm8350_reg_write(wm8350, WM8350_CSB_FLASH_CONTROL,
  322. (mode ? WM8350_CS2_FLASH_MODE : 0) |
  323. (trigger ? WM8350_CS2_TRIGSRC : 0) |
  324. duration | on_ramp | off_ramp | drive);
  325. break;
  326. default:
  327. return -EINVAL;
  328. }
  329. return 0;
  330. }
  331. EXPORT_SYMBOL_GPL(wm8350_isink_set_flash);
  332. static int wm8350_dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
  333. int max_uV, unsigned *selector)
  334. {
  335. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  336. int volt_reg, dcdc = rdev_get_id(rdev), mV,
  337. min_mV = min_uV / 1000, max_mV = max_uV / 1000;
  338. u16 val;
  339. if (min_mV < 850 || min_mV > 4025)
  340. return -EINVAL;
  341. if (max_mV < 850 || max_mV > 4025)
  342. return -EINVAL;
  343. /* step size is 25mV */
  344. mV = (min_mV - 826) / 25;
  345. if (wm8350_dcdc_val_to_mvolts(mV) > max_mV)
  346. return -EINVAL;
  347. BUG_ON(wm8350_dcdc_val_to_mvolts(mV) < min_mV);
  348. switch (dcdc) {
  349. case WM8350_DCDC_1:
  350. volt_reg = WM8350_DCDC1_CONTROL;
  351. break;
  352. case WM8350_DCDC_3:
  353. volt_reg = WM8350_DCDC3_CONTROL;
  354. break;
  355. case WM8350_DCDC_4:
  356. volt_reg = WM8350_DCDC4_CONTROL;
  357. break;
  358. case WM8350_DCDC_6:
  359. volt_reg = WM8350_DCDC6_CONTROL;
  360. break;
  361. case WM8350_DCDC_2:
  362. case WM8350_DCDC_5:
  363. default:
  364. return -EINVAL;
  365. }
  366. *selector = mV;
  367. /* all DCDCs have same mV bits */
  368. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_DC1_VSEL_MASK;
  369. wm8350_reg_write(wm8350, volt_reg, val | mV);
  370. return 0;
  371. }
  372. static int wm8350_dcdc_get_voltage_sel(struct regulator_dev *rdev)
  373. {
  374. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  375. int volt_reg, dcdc = rdev_get_id(rdev);
  376. switch (dcdc) {
  377. case WM8350_DCDC_1:
  378. volt_reg = WM8350_DCDC1_CONTROL;
  379. break;
  380. case WM8350_DCDC_3:
  381. volt_reg = WM8350_DCDC3_CONTROL;
  382. break;
  383. case WM8350_DCDC_4:
  384. volt_reg = WM8350_DCDC4_CONTROL;
  385. break;
  386. case WM8350_DCDC_6:
  387. volt_reg = WM8350_DCDC6_CONTROL;
  388. break;
  389. case WM8350_DCDC_2:
  390. case WM8350_DCDC_5:
  391. default:
  392. return -EINVAL;
  393. }
  394. /* all DCDCs have same mV bits */
  395. return wm8350_reg_read(wm8350, volt_reg) & WM8350_DC1_VSEL_MASK;
  396. }
  397. static int wm8350_dcdc_list_voltage(struct regulator_dev *rdev,
  398. unsigned selector)
  399. {
  400. if (selector > WM8350_DCDC_MAX_VSEL)
  401. return -EINVAL;
  402. return wm8350_dcdc_val_to_mvolts(selector) * 1000;
  403. }
  404. static int wm8350_dcdc_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  405. {
  406. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  407. int volt_reg, mV = uV / 1000, dcdc = rdev_get_id(rdev);
  408. u16 val;
  409. dev_dbg(wm8350->dev, "%s %d mV %d\n", __func__, dcdc, mV);
  410. if (mV && (mV < 850 || mV > 4025)) {
  411. dev_err(wm8350->dev,
  412. "DCDC%d suspend voltage %d mV out of range\n",
  413. dcdc, mV);
  414. return -EINVAL;
  415. }
  416. if (mV == 0)
  417. mV = 850;
  418. switch (dcdc) {
  419. case WM8350_DCDC_1:
  420. volt_reg = WM8350_DCDC1_LOW_POWER;
  421. break;
  422. case WM8350_DCDC_3:
  423. volt_reg = WM8350_DCDC3_LOW_POWER;
  424. break;
  425. case WM8350_DCDC_4:
  426. volt_reg = WM8350_DCDC4_LOW_POWER;
  427. break;
  428. case WM8350_DCDC_6:
  429. volt_reg = WM8350_DCDC6_LOW_POWER;
  430. break;
  431. case WM8350_DCDC_2:
  432. case WM8350_DCDC_5:
  433. default:
  434. return -EINVAL;
  435. }
  436. /* all DCDCs have same mV bits */
  437. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_DC1_VSEL_MASK;
  438. wm8350_reg_write(wm8350, volt_reg,
  439. val | wm8350_dcdc_mvolts_to_val(mV));
  440. return 0;
  441. }
  442. static int wm8350_dcdc_set_suspend_enable(struct regulator_dev *rdev)
  443. {
  444. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  445. int dcdc = rdev_get_id(rdev);
  446. u16 val;
  447. switch (dcdc) {
  448. case WM8350_DCDC_1:
  449. val = wm8350_reg_read(wm8350, WM8350_DCDC1_LOW_POWER)
  450. & ~WM8350_DCDC_HIB_MODE_MASK;
  451. wm8350_reg_write(wm8350, WM8350_DCDC1_LOW_POWER,
  452. wm8350->pmic.dcdc1_hib_mode);
  453. break;
  454. case WM8350_DCDC_3:
  455. val = wm8350_reg_read(wm8350, WM8350_DCDC3_LOW_POWER)
  456. & ~WM8350_DCDC_HIB_MODE_MASK;
  457. wm8350_reg_write(wm8350, WM8350_DCDC3_LOW_POWER,
  458. wm8350->pmic.dcdc3_hib_mode);
  459. break;
  460. case WM8350_DCDC_4:
  461. val = wm8350_reg_read(wm8350, WM8350_DCDC4_LOW_POWER)
  462. & ~WM8350_DCDC_HIB_MODE_MASK;
  463. wm8350_reg_write(wm8350, WM8350_DCDC4_LOW_POWER,
  464. wm8350->pmic.dcdc4_hib_mode);
  465. break;
  466. case WM8350_DCDC_6:
  467. val = wm8350_reg_read(wm8350, WM8350_DCDC6_LOW_POWER)
  468. & ~WM8350_DCDC_HIB_MODE_MASK;
  469. wm8350_reg_write(wm8350, WM8350_DCDC6_LOW_POWER,
  470. wm8350->pmic.dcdc6_hib_mode);
  471. break;
  472. case WM8350_DCDC_2:
  473. case WM8350_DCDC_5:
  474. default:
  475. return -EINVAL;
  476. }
  477. return 0;
  478. }
  479. static int wm8350_dcdc_set_suspend_disable(struct regulator_dev *rdev)
  480. {
  481. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  482. int dcdc = rdev_get_id(rdev);
  483. u16 val;
  484. switch (dcdc) {
  485. case WM8350_DCDC_1:
  486. val = wm8350_reg_read(wm8350, WM8350_DCDC1_LOW_POWER);
  487. wm8350->pmic.dcdc1_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  488. wm8350_reg_write(wm8350, WM8350_DCDC1_LOW_POWER,
  489. WM8350_DCDC_HIB_MODE_DIS);
  490. break;
  491. case WM8350_DCDC_3:
  492. val = wm8350_reg_read(wm8350, WM8350_DCDC3_LOW_POWER);
  493. wm8350->pmic.dcdc3_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  494. wm8350_reg_write(wm8350, WM8350_DCDC3_LOW_POWER,
  495. WM8350_DCDC_HIB_MODE_DIS);
  496. break;
  497. case WM8350_DCDC_4:
  498. val = wm8350_reg_read(wm8350, WM8350_DCDC4_LOW_POWER);
  499. wm8350->pmic.dcdc4_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  500. wm8350_reg_write(wm8350, WM8350_DCDC4_LOW_POWER,
  501. WM8350_DCDC_HIB_MODE_DIS);
  502. break;
  503. case WM8350_DCDC_6:
  504. val = wm8350_reg_read(wm8350, WM8350_DCDC6_LOW_POWER);
  505. wm8350->pmic.dcdc6_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  506. wm8350_reg_write(wm8350, WM8350_DCDC6_LOW_POWER,
  507. WM8350_DCDC_HIB_MODE_DIS);
  508. break;
  509. case WM8350_DCDC_2:
  510. case WM8350_DCDC_5:
  511. default:
  512. return -EINVAL;
  513. }
  514. return 0;
  515. }
  516. static int wm8350_dcdc25_set_suspend_enable(struct regulator_dev *rdev)
  517. {
  518. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  519. int dcdc = rdev_get_id(rdev);
  520. u16 val;
  521. switch (dcdc) {
  522. case WM8350_DCDC_2:
  523. val = wm8350_reg_read(wm8350, WM8350_DCDC2_CONTROL)
  524. & ~WM8350_DC2_HIB_MODE_MASK;
  525. wm8350_reg_write(wm8350, WM8350_DCDC2_CONTROL, val |
  526. WM8350_DC2_HIB_MODE_ACTIVE);
  527. break;
  528. case WM8350_DCDC_5:
  529. val = wm8350_reg_read(wm8350, WM8350_DCDC5_CONTROL)
  530. & ~WM8350_DC2_HIB_MODE_MASK;
  531. wm8350_reg_write(wm8350, WM8350_DCDC5_CONTROL, val |
  532. WM8350_DC5_HIB_MODE_ACTIVE);
  533. break;
  534. default:
  535. return -EINVAL;
  536. }
  537. return 0;
  538. }
  539. static int wm8350_dcdc25_set_suspend_disable(struct regulator_dev *rdev)
  540. {
  541. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  542. int dcdc = rdev_get_id(rdev);
  543. u16 val;
  544. switch (dcdc) {
  545. case WM8350_DCDC_2:
  546. val = wm8350_reg_read(wm8350, WM8350_DCDC2_CONTROL)
  547. & ~WM8350_DC2_HIB_MODE_MASK;
  548. wm8350_reg_write(wm8350, WM8350_DCDC2_CONTROL, val |
  549. WM8350_DC2_HIB_MODE_DISABLE);
  550. break;
  551. case WM8350_DCDC_5:
  552. val = wm8350_reg_read(wm8350, WM8350_DCDC5_CONTROL)
  553. & ~WM8350_DC2_HIB_MODE_MASK;
  554. wm8350_reg_write(wm8350, WM8350_DCDC5_CONTROL, val |
  555. WM8350_DC2_HIB_MODE_DISABLE);
  556. break;
  557. default:
  558. return -EINVAL;
  559. }
  560. return 0;
  561. }
  562. static int wm8350_dcdc_set_suspend_mode(struct regulator_dev *rdev,
  563. unsigned int mode)
  564. {
  565. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  566. int dcdc = rdev_get_id(rdev);
  567. u16 *hib_mode;
  568. switch (dcdc) {
  569. case WM8350_DCDC_1:
  570. hib_mode = &wm8350->pmic.dcdc1_hib_mode;
  571. break;
  572. case WM8350_DCDC_3:
  573. hib_mode = &wm8350->pmic.dcdc3_hib_mode;
  574. break;
  575. case WM8350_DCDC_4:
  576. hib_mode = &wm8350->pmic.dcdc4_hib_mode;
  577. break;
  578. case WM8350_DCDC_6:
  579. hib_mode = &wm8350->pmic.dcdc6_hib_mode;
  580. break;
  581. case WM8350_DCDC_2:
  582. case WM8350_DCDC_5:
  583. default:
  584. return -EINVAL;
  585. }
  586. switch (mode) {
  587. case REGULATOR_MODE_NORMAL:
  588. *hib_mode = WM8350_DCDC_HIB_MODE_IMAGE;
  589. break;
  590. case REGULATOR_MODE_IDLE:
  591. *hib_mode = WM8350_DCDC_HIB_MODE_STANDBY;
  592. break;
  593. case REGULATOR_MODE_STANDBY:
  594. *hib_mode = WM8350_DCDC_HIB_MODE_LDO_IM;
  595. break;
  596. default:
  597. return -EINVAL;
  598. }
  599. return 0;
  600. }
  601. static int wm8350_ldo_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  602. {
  603. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  604. int volt_reg, mV = uV / 1000, ldo = rdev_get_id(rdev);
  605. u16 val;
  606. dev_dbg(wm8350->dev, "%s %d mV %d\n", __func__, ldo, mV);
  607. if (mV < 900 || mV > 3300) {
  608. dev_err(wm8350->dev, "LDO%d voltage %d mV out of range\n",
  609. ldo, mV);
  610. return -EINVAL;
  611. }
  612. switch (ldo) {
  613. case WM8350_LDO_1:
  614. volt_reg = WM8350_LDO1_LOW_POWER;
  615. break;
  616. case WM8350_LDO_2:
  617. volt_reg = WM8350_LDO2_LOW_POWER;
  618. break;
  619. case WM8350_LDO_3:
  620. volt_reg = WM8350_LDO3_LOW_POWER;
  621. break;
  622. case WM8350_LDO_4:
  623. volt_reg = WM8350_LDO4_LOW_POWER;
  624. break;
  625. default:
  626. return -EINVAL;
  627. }
  628. /* all LDOs have same mV bits */
  629. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_VSEL_MASK;
  630. wm8350_reg_write(wm8350, volt_reg,
  631. val | wm8350_ldo_mvolts_to_val(mV));
  632. return 0;
  633. }
  634. static int wm8350_ldo_set_suspend_enable(struct regulator_dev *rdev)
  635. {
  636. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  637. int volt_reg, ldo = rdev_get_id(rdev);
  638. u16 val;
  639. switch (ldo) {
  640. case WM8350_LDO_1:
  641. volt_reg = WM8350_LDO1_LOW_POWER;
  642. break;
  643. case WM8350_LDO_2:
  644. volt_reg = WM8350_LDO2_LOW_POWER;
  645. break;
  646. case WM8350_LDO_3:
  647. volt_reg = WM8350_LDO3_LOW_POWER;
  648. break;
  649. case WM8350_LDO_4:
  650. volt_reg = WM8350_LDO4_LOW_POWER;
  651. break;
  652. default:
  653. return -EINVAL;
  654. }
  655. /* all LDOs have same mV bits */
  656. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_HIB_MODE_MASK;
  657. wm8350_reg_write(wm8350, volt_reg, val);
  658. return 0;
  659. }
  660. static int wm8350_ldo_set_suspend_disable(struct regulator_dev *rdev)
  661. {
  662. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  663. int volt_reg, ldo = rdev_get_id(rdev);
  664. u16 val;
  665. switch (ldo) {
  666. case WM8350_LDO_1:
  667. volt_reg = WM8350_LDO1_LOW_POWER;
  668. break;
  669. case WM8350_LDO_2:
  670. volt_reg = WM8350_LDO2_LOW_POWER;
  671. break;
  672. case WM8350_LDO_3:
  673. volt_reg = WM8350_LDO3_LOW_POWER;
  674. break;
  675. case WM8350_LDO_4:
  676. volt_reg = WM8350_LDO4_LOW_POWER;
  677. break;
  678. default:
  679. return -EINVAL;
  680. }
  681. /* all LDOs have same mV bits */
  682. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_HIB_MODE_MASK;
  683. wm8350_reg_write(wm8350, volt_reg, WM8350_LDO1_HIB_MODE_DIS);
  684. return 0;
  685. }
  686. static int wm8350_ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
  687. int max_uV, unsigned *selector)
  688. {
  689. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  690. int volt_reg, ldo = rdev_get_id(rdev), mV, min_mV = min_uV / 1000,
  691. max_mV = max_uV / 1000;
  692. u16 val;
  693. if (min_mV < 900 || min_mV > 3300)
  694. return -EINVAL;
  695. if (max_mV < 900 || max_mV > 3300)
  696. return -EINVAL;
  697. if (min_mV < 1800) {
  698. /* step size is 50mV < 1800mV */
  699. mV = (min_mV - 851) / 50;
  700. if (wm8350_ldo_val_to_mvolts(mV) > max_mV)
  701. return -EINVAL;
  702. BUG_ON(wm8350_ldo_val_to_mvolts(mV) < min_mV);
  703. } else {
  704. /* step size is 100mV > 1800mV */
  705. mV = ((min_mV - 1701) / 100) + 16;
  706. if (wm8350_ldo_val_to_mvolts(mV) > max_mV)
  707. return -EINVAL;
  708. BUG_ON(wm8350_ldo_val_to_mvolts(mV) < min_mV);
  709. }
  710. switch (ldo) {
  711. case WM8350_LDO_1:
  712. volt_reg = WM8350_LDO1_CONTROL;
  713. break;
  714. case WM8350_LDO_2:
  715. volt_reg = WM8350_LDO2_CONTROL;
  716. break;
  717. case WM8350_LDO_3:
  718. volt_reg = WM8350_LDO3_CONTROL;
  719. break;
  720. case WM8350_LDO_4:
  721. volt_reg = WM8350_LDO4_CONTROL;
  722. break;
  723. default:
  724. return -EINVAL;
  725. }
  726. *selector = mV;
  727. /* all LDOs have same mV bits */
  728. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_VSEL_MASK;
  729. wm8350_reg_write(wm8350, volt_reg, val | mV);
  730. return 0;
  731. }
  732. static int wm8350_ldo_get_voltage_sel(struct regulator_dev *rdev)
  733. {
  734. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  735. int volt_reg, ldo = rdev_get_id(rdev);
  736. switch (ldo) {
  737. case WM8350_LDO_1:
  738. volt_reg = WM8350_LDO1_CONTROL;
  739. break;
  740. case WM8350_LDO_2:
  741. volt_reg = WM8350_LDO2_CONTROL;
  742. break;
  743. case WM8350_LDO_3:
  744. volt_reg = WM8350_LDO3_CONTROL;
  745. break;
  746. case WM8350_LDO_4:
  747. volt_reg = WM8350_LDO4_CONTROL;
  748. break;
  749. default:
  750. return -EINVAL;
  751. }
  752. /* all LDOs have same mV bits */
  753. return wm8350_reg_read(wm8350, volt_reg) & WM8350_LDO1_VSEL_MASK;
  754. }
  755. static int wm8350_ldo_list_voltage(struct regulator_dev *rdev,
  756. unsigned selector)
  757. {
  758. if (selector > WM8350_LDO1_VSEL_MASK)
  759. return -EINVAL;
  760. return wm8350_ldo_val_to_mvolts(selector) * 1000;
  761. }
  762. int wm8350_dcdc_set_slot(struct wm8350 *wm8350, int dcdc, u16 start,
  763. u16 stop, u16 fault)
  764. {
  765. int slot_reg;
  766. u16 val;
  767. dev_dbg(wm8350->dev, "%s %d start %d stop %d\n",
  768. __func__, dcdc, start, stop);
  769. /* slot valid ? */
  770. if (start > 15 || stop > 15)
  771. return -EINVAL;
  772. switch (dcdc) {
  773. case WM8350_DCDC_1:
  774. slot_reg = WM8350_DCDC1_TIMEOUTS;
  775. break;
  776. case WM8350_DCDC_2:
  777. slot_reg = WM8350_DCDC2_TIMEOUTS;
  778. break;
  779. case WM8350_DCDC_3:
  780. slot_reg = WM8350_DCDC3_TIMEOUTS;
  781. break;
  782. case WM8350_DCDC_4:
  783. slot_reg = WM8350_DCDC4_TIMEOUTS;
  784. break;
  785. case WM8350_DCDC_5:
  786. slot_reg = WM8350_DCDC5_TIMEOUTS;
  787. break;
  788. case WM8350_DCDC_6:
  789. slot_reg = WM8350_DCDC6_TIMEOUTS;
  790. break;
  791. default:
  792. return -EINVAL;
  793. }
  794. val = wm8350_reg_read(wm8350, slot_reg) &
  795. ~(WM8350_DC1_ENSLOT_MASK | WM8350_DC1_SDSLOT_MASK |
  796. WM8350_DC1_ERRACT_MASK);
  797. wm8350_reg_write(wm8350, slot_reg,
  798. val | (start << WM8350_DC1_ENSLOT_SHIFT) |
  799. (stop << WM8350_DC1_SDSLOT_SHIFT) |
  800. (fault << WM8350_DC1_ERRACT_SHIFT));
  801. return 0;
  802. }
  803. EXPORT_SYMBOL_GPL(wm8350_dcdc_set_slot);
  804. int wm8350_ldo_set_slot(struct wm8350 *wm8350, int ldo, u16 start, u16 stop)
  805. {
  806. int slot_reg;
  807. u16 val;
  808. dev_dbg(wm8350->dev, "%s %d start %d stop %d\n",
  809. __func__, ldo, start, stop);
  810. /* slot valid ? */
  811. if (start > 15 || stop > 15)
  812. return -EINVAL;
  813. switch (ldo) {
  814. case WM8350_LDO_1:
  815. slot_reg = WM8350_LDO1_TIMEOUTS;
  816. break;
  817. case WM8350_LDO_2:
  818. slot_reg = WM8350_LDO2_TIMEOUTS;
  819. break;
  820. case WM8350_LDO_3:
  821. slot_reg = WM8350_LDO3_TIMEOUTS;
  822. break;
  823. case WM8350_LDO_4:
  824. slot_reg = WM8350_LDO4_TIMEOUTS;
  825. break;
  826. default:
  827. return -EINVAL;
  828. }
  829. val = wm8350_reg_read(wm8350, slot_reg) & ~WM8350_LDO1_SDSLOT_MASK;
  830. wm8350_reg_write(wm8350, slot_reg, val | ((start << 10) | (stop << 6)));
  831. return 0;
  832. }
  833. EXPORT_SYMBOL_GPL(wm8350_ldo_set_slot);
  834. int wm8350_dcdc25_set_mode(struct wm8350 *wm8350, int dcdc, u16 mode,
  835. u16 ilim, u16 ramp, u16 feedback)
  836. {
  837. u16 val;
  838. dev_dbg(wm8350->dev, "%s %d mode: %s %s\n", __func__, dcdc,
  839. mode ? "normal" : "boost", ilim ? "low" : "normal");
  840. switch (dcdc) {
  841. case WM8350_DCDC_2:
  842. val = wm8350_reg_read(wm8350, WM8350_DCDC2_CONTROL)
  843. & ~(WM8350_DC2_MODE_MASK | WM8350_DC2_ILIM_MASK |
  844. WM8350_DC2_RMP_MASK | WM8350_DC2_FBSRC_MASK);
  845. wm8350_reg_write(wm8350, WM8350_DCDC2_CONTROL, val |
  846. (mode << WM8350_DC2_MODE_SHIFT) |
  847. (ilim << WM8350_DC2_ILIM_SHIFT) |
  848. (ramp << WM8350_DC2_RMP_SHIFT) |
  849. (feedback << WM8350_DC2_FBSRC_SHIFT));
  850. break;
  851. case WM8350_DCDC_5:
  852. val = wm8350_reg_read(wm8350, WM8350_DCDC5_CONTROL)
  853. & ~(WM8350_DC5_MODE_MASK | WM8350_DC5_ILIM_MASK |
  854. WM8350_DC5_RMP_MASK | WM8350_DC5_FBSRC_MASK);
  855. wm8350_reg_write(wm8350, WM8350_DCDC5_CONTROL, val |
  856. (mode << WM8350_DC5_MODE_SHIFT) |
  857. (ilim << WM8350_DC5_ILIM_SHIFT) |
  858. (ramp << WM8350_DC5_RMP_SHIFT) |
  859. (feedback << WM8350_DC5_FBSRC_SHIFT));
  860. break;
  861. default:
  862. return -EINVAL;
  863. }
  864. return 0;
  865. }
  866. EXPORT_SYMBOL_GPL(wm8350_dcdc25_set_mode);
  867. static int wm8350_dcdc_enable(struct regulator_dev *rdev)
  868. {
  869. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  870. int dcdc = rdev_get_id(rdev);
  871. u16 shift;
  872. if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
  873. return -EINVAL;
  874. shift = dcdc - WM8350_DCDC_1;
  875. wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED, 1 << shift);
  876. return 0;
  877. }
  878. static int wm8350_dcdc_disable(struct regulator_dev *rdev)
  879. {
  880. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  881. int dcdc = rdev_get_id(rdev);
  882. u16 shift;
  883. if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
  884. return -EINVAL;
  885. shift = dcdc - WM8350_DCDC_1;
  886. wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED, 1 << shift);
  887. return 0;
  888. }
  889. static int wm8350_ldo_enable(struct regulator_dev *rdev)
  890. {
  891. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  892. int ldo = rdev_get_id(rdev);
  893. u16 shift;
  894. if (ldo < WM8350_LDO_1 || ldo > WM8350_LDO_4)
  895. return -EINVAL;
  896. shift = (ldo - WM8350_LDO_1) + 8;
  897. wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED, 1 << shift);
  898. return 0;
  899. }
  900. static int wm8350_ldo_disable(struct regulator_dev *rdev)
  901. {
  902. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  903. int ldo = rdev_get_id(rdev);
  904. u16 shift;
  905. if (ldo < WM8350_LDO_1 || ldo > WM8350_LDO_4)
  906. return -EINVAL;
  907. shift = (ldo - WM8350_LDO_1) + 8;
  908. wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED, 1 << shift);
  909. return 0;
  910. }
  911. static int force_continuous_enable(struct wm8350 *wm8350, int dcdc, int enable)
  912. {
  913. int reg = 0, ret;
  914. switch (dcdc) {
  915. case WM8350_DCDC_1:
  916. reg = WM8350_DCDC1_FORCE_PWM;
  917. break;
  918. case WM8350_DCDC_3:
  919. reg = WM8350_DCDC3_FORCE_PWM;
  920. break;
  921. case WM8350_DCDC_4:
  922. reg = WM8350_DCDC4_FORCE_PWM;
  923. break;
  924. case WM8350_DCDC_6:
  925. reg = WM8350_DCDC6_FORCE_PWM;
  926. break;
  927. default:
  928. return -EINVAL;
  929. }
  930. if (enable)
  931. ret = wm8350_set_bits(wm8350, reg,
  932. WM8350_DCDC1_FORCE_PWM_ENA);
  933. else
  934. ret = wm8350_clear_bits(wm8350, reg,
  935. WM8350_DCDC1_FORCE_PWM_ENA);
  936. return ret;
  937. }
  938. static int wm8350_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
  939. {
  940. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  941. int dcdc = rdev_get_id(rdev);
  942. u16 val;
  943. if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
  944. return -EINVAL;
  945. if (dcdc == WM8350_DCDC_2 || dcdc == WM8350_DCDC_5)
  946. return -EINVAL;
  947. val = 1 << (dcdc - WM8350_DCDC_1);
  948. switch (mode) {
  949. case REGULATOR_MODE_FAST:
  950. /* force continuous mode */
  951. wm8350_set_bits(wm8350, WM8350_DCDC_ACTIVE_OPTIONS, val);
  952. wm8350_clear_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
  953. force_continuous_enable(wm8350, dcdc, 1);
  954. break;
  955. case REGULATOR_MODE_NORMAL:
  956. /* active / pulse skipping */
  957. wm8350_set_bits(wm8350, WM8350_DCDC_ACTIVE_OPTIONS, val);
  958. wm8350_clear_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
  959. force_continuous_enable(wm8350, dcdc, 0);
  960. break;
  961. case REGULATOR_MODE_IDLE:
  962. /* standby mode */
  963. force_continuous_enable(wm8350, dcdc, 0);
  964. wm8350_clear_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
  965. wm8350_clear_bits(wm8350, WM8350_DCDC_ACTIVE_OPTIONS, val);
  966. break;
  967. case REGULATOR_MODE_STANDBY:
  968. /* LDO mode */
  969. force_continuous_enable(wm8350, dcdc, 0);
  970. wm8350_set_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
  971. break;
  972. }
  973. return 0;
  974. }
  975. static unsigned int wm8350_dcdc_get_mode(struct regulator_dev *rdev)
  976. {
  977. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  978. int dcdc = rdev_get_id(rdev);
  979. u16 mask, sleep, active, force;
  980. int mode = REGULATOR_MODE_NORMAL;
  981. int reg;
  982. switch (dcdc) {
  983. case WM8350_DCDC_1:
  984. reg = WM8350_DCDC1_FORCE_PWM;
  985. break;
  986. case WM8350_DCDC_3:
  987. reg = WM8350_DCDC3_FORCE_PWM;
  988. break;
  989. case WM8350_DCDC_4:
  990. reg = WM8350_DCDC4_FORCE_PWM;
  991. break;
  992. case WM8350_DCDC_6:
  993. reg = WM8350_DCDC6_FORCE_PWM;
  994. break;
  995. default:
  996. return -EINVAL;
  997. }
  998. mask = 1 << (dcdc - WM8350_DCDC_1);
  999. active = wm8350_reg_read(wm8350, WM8350_DCDC_ACTIVE_OPTIONS) & mask;
  1000. force = wm8350_reg_read(wm8350, reg) & WM8350_DCDC1_FORCE_PWM_ENA;
  1001. sleep = wm8350_reg_read(wm8350, WM8350_DCDC_SLEEP_OPTIONS) & mask;
  1002. dev_dbg(wm8350->dev, "mask %x active %x sleep %x force %x",
  1003. mask, active, sleep, force);
  1004. if (active && !sleep) {
  1005. if (force)
  1006. mode = REGULATOR_MODE_FAST;
  1007. else
  1008. mode = REGULATOR_MODE_NORMAL;
  1009. } else if (!active && !sleep)
  1010. mode = REGULATOR_MODE_IDLE;
  1011. else if (sleep)
  1012. mode = REGULATOR_MODE_STANDBY;
  1013. return mode;
  1014. }
  1015. static unsigned int wm8350_ldo_get_mode(struct regulator_dev *rdev)
  1016. {
  1017. return REGULATOR_MODE_NORMAL;
  1018. }
  1019. struct wm8350_dcdc_efficiency {
  1020. int uA_load_min;
  1021. int uA_load_max;
  1022. unsigned int mode;
  1023. };
  1024. static const struct wm8350_dcdc_efficiency dcdc1_6_efficiency[] = {
  1025. {0, 10000, REGULATOR_MODE_STANDBY}, /* 0 - 10mA - LDO */
  1026. {10000, 100000, REGULATOR_MODE_IDLE}, /* 10mA - 100mA - Standby */
  1027. {100000, 1000000, REGULATOR_MODE_NORMAL}, /* > 100mA - Active */
  1028. {-1, -1, REGULATOR_MODE_NORMAL},
  1029. };
  1030. static const struct wm8350_dcdc_efficiency dcdc3_4_efficiency[] = {
  1031. {0, 10000, REGULATOR_MODE_STANDBY}, /* 0 - 10mA - LDO */
  1032. {10000, 100000, REGULATOR_MODE_IDLE}, /* 10mA - 100mA - Standby */
  1033. {100000, 800000, REGULATOR_MODE_NORMAL}, /* > 100mA - Active */
  1034. {-1, -1, REGULATOR_MODE_NORMAL},
  1035. };
  1036. static unsigned int get_mode(int uA, const struct wm8350_dcdc_efficiency *eff)
  1037. {
  1038. int i = 0;
  1039. while (eff[i].uA_load_min != -1) {
  1040. if (uA >= eff[i].uA_load_min && uA <= eff[i].uA_load_max)
  1041. return eff[i].mode;
  1042. }
  1043. return REGULATOR_MODE_NORMAL;
  1044. }
  1045. /* Query the regulator for it's most efficient mode @ uV,uA
  1046. * WM8350 regulator efficiency is pretty similar over
  1047. * different input and output uV.
  1048. */
  1049. static unsigned int wm8350_dcdc_get_optimum_mode(struct regulator_dev *rdev,
  1050. int input_uV, int output_uV,
  1051. int output_uA)
  1052. {
  1053. int dcdc = rdev_get_id(rdev), mode;
  1054. switch (dcdc) {
  1055. case WM8350_DCDC_1:
  1056. case WM8350_DCDC_6:
  1057. mode = get_mode(output_uA, dcdc1_6_efficiency);
  1058. break;
  1059. case WM8350_DCDC_3:
  1060. case WM8350_DCDC_4:
  1061. mode = get_mode(output_uA, dcdc3_4_efficiency);
  1062. break;
  1063. default:
  1064. mode = REGULATOR_MODE_NORMAL;
  1065. break;
  1066. }
  1067. return mode;
  1068. }
  1069. static int wm8350_dcdc_is_enabled(struct regulator_dev *rdev)
  1070. {
  1071. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  1072. int dcdc = rdev_get_id(rdev), shift;
  1073. if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
  1074. return -EINVAL;
  1075. shift = dcdc - WM8350_DCDC_1;
  1076. return wm8350_reg_read(wm8350, WM8350_DCDC_LDO_REQUESTED)
  1077. & (1 << shift);
  1078. }
  1079. static int wm8350_ldo_is_enabled(struct regulator_dev *rdev)
  1080. {
  1081. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  1082. int ldo = rdev_get_id(rdev), shift;
  1083. if (ldo < WM8350_LDO_1 || ldo > WM8350_LDO_4)
  1084. return -EINVAL;
  1085. shift = (ldo - WM8350_LDO_1) + 8;
  1086. return wm8350_reg_read(wm8350, WM8350_DCDC_LDO_REQUESTED)
  1087. & (1 << shift);
  1088. }
  1089. static struct regulator_ops wm8350_dcdc_ops = {
  1090. .set_voltage = wm8350_dcdc_set_voltage,
  1091. .get_voltage_sel = wm8350_dcdc_get_voltage_sel,
  1092. .list_voltage = wm8350_dcdc_list_voltage,
  1093. .enable = wm8350_dcdc_enable,
  1094. .disable = wm8350_dcdc_disable,
  1095. .get_mode = wm8350_dcdc_get_mode,
  1096. .set_mode = wm8350_dcdc_set_mode,
  1097. .get_optimum_mode = wm8350_dcdc_get_optimum_mode,
  1098. .is_enabled = wm8350_dcdc_is_enabled,
  1099. .set_suspend_voltage = wm8350_dcdc_set_suspend_voltage,
  1100. .set_suspend_enable = wm8350_dcdc_set_suspend_enable,
  1101. .set_suspend_disable = wm8350_dcdc_set_suspend_disable,
  1102. .set_suspend_mode = wm8350_dcdc_set_suspend_mode,
  1103. };
  1104. static struct regulator_ops wm8350_dcdc2_5_ops = {
  1105. .enable = wm8350_dcdc_enable,
  1106. .disable = wm8350_dcdc_disable,
  1107. .is_enabled = wm8350_dcdc_is_enabled,
  1108. .set_suspend_enable = wm8350_dcdc25_set_suspend_enable,
  1109. .set_suspend_disable = wm8350_dcdc25_set_suspend_disable,
  1110. };
  1111. static struct regulator_ops wm8350_ldo_ops = {
  1112. .set_voltage = wm8350_ldo_set_voltage,
  1113. .get_voltage_sel = wm8350_ldo_get_voltage_sel,
  1114. .list_voltage = wm8350_ldo_list_voltage,
  1115. .enable = wm8350_ldo_enable,
  1116. .disable = wm8350_ldo_disable,
  1117. .is_enabled = wm8350_ldo_is_enabled,
  1118. .get_mode = wm8350_ldo_get_mode,
  1119. .set_suspend_voltage = wm8350_ldo_set_suspend_voltage,
  1120. .set_suspend_enable = wm8350_ldo_set_suspend_enable,
  1121. .set_suspend_disable = wm8350_ldo_set_suspend_disable,
  1122. };
  1123. static struct regulator_ops wm8350_isink_ops = {
  1124. .set_current_limit = wm8350_isink_set_current,
  1125. .get_current_limit = wm8350_isink_get_current,
  1126. .enable = wm8350_isink_enable,
  1127. .disable = wm8350_isink_disable,
  1128. .is_enabled = wm8350_isink_is_enabled,
  1129. .enable_time = wm8350_isink_enable_time,
  1130. };
  1131. static struct regulator_desc wm8350_reg[NUM_WM8350_REGULATORS] = {
  1132. {
  1133. .name = "DCDC1",
  1134. .id = WM8350_DCDC_1,
  1135. .ops = &wm8350_dcdc_ops,
  1136. .irq = WM8350_IRQ_UV_DC1,
  1137. .type = REGULATOR_VOLTAGE,
  1138. .n_voltages = WM8350_DCDC_MAX_VSEL + 1,
  1139. .owner = THIS_MODULE,
  1140. },
  1141. {
  1142. .name = "DCDC2",
  1143. .id = WM8350_DCDC_2,
  1144. .ops = &wm8350_dcdc2_5_ops,
  1145. .irq = WM8350_IRQ_UV_DC2,
  1146. .type = REGULATOR_VOLTAGE,
  1147. .owner = THIS_MODULE,
  1148. },
  1149. {
  1150. .name = "DCDC3",
  1151. .id = WM8350_DCDC_3,
  1152. .ops = &wm8350_dcdc_ops,
  1153. .irq = WM8350_IRQ_UV_DC3,
  1154. .type = REGULATOR_VOLTAGE,
  1155. .n_voltages = WM8350_DCDC_MAX_VSEL + 1,
  1156. .owner = THIS_MODULE,
  1157. },
  1158. {
  1159. .name = "DCDC4",
  1160. .id = WM8350_DCDC_4,
  1161. .ops = &wm8350_dcdc_ops,
  1162. .irq = WM8350_IRQ_UV_DC4,
  1163. .type = REGULATOR_VOLTAGE,
  1164. .n_voltages = WM8350_DCDC_MAX_VSEL + 1,
  1165. .owner = THIS_MODULE,
  1166. },
  1167. {
  1168. .name = "DCDC5",
  1169. .id = WM8350_DCDC_5,
  1170. .ops = &wm8350_dcdc2_5_ops,
  1171. .irq = WM8350_IRQ_UV_DC5,
  1172. .type = REGULATOR_VOLTAGE,
  1173. .owner = THIS_MODULE,
  1174. },
  1175. {
  1176. .name = "DCDC6",
  1177. .id = WM8350_DCDC_6,
  1178. .ops = &wm8350_dcdc_ops,
  1179. .irq = WM8350_IRQ_UV_DC6,
  1180. .type = REGULATOR_VOLTAGE,
  1181. .n_voltages = WM8350_DCDC_MAX_VSEL + 1,
  1182. .owner = THIS_MODULE,
  1183. },
  1184. {
  1185. .name = "LDO1",
  1186. .id = WM8350_LDO_1,
  1187. .ops = &wm8350_ldo_ops,
  1188. .irq = WM8350_IRQ_UV_LDO1,
  1189. .type = REGULATOR_VOLTAGE,
  1190. .n_voltages = WM8350_LDO1_VSEL_MASK + 1,
  1191. .owner = THIS_MODULE,
  1192. },
  1193. {
  1194. .name = "LDO2",
  1195. .id = WM8350_LDO_2,
  1196. .ops = &wm8350_ldo_ops,
  1197. .irq = WM8350_IRQ_UV_LDO2,
  1198. .type = REGULATOR_VOLTAGE,
  1199. .n_voltages = WM8350_LDO2_VSEL_MASK + 1,
  1200. .owner = THIS_MODULE,
  1201. },
  1202. {
  1203. .name = "LDO3",
  1204. .id = WM8350_LDO_3,
  1205. .ops = &wm8350_ldo_ops,
  1206. .irq = WM8350_IRQ_UV_LDO3,
  1207. .type = REGULATOR_VOLTAGE,
  1208. .n_voltages = WM8350_LDO3_VSEL_MASK + 1,
  1209. .owner = THIS_MODULE,
  1210. },
  1211. {
  1212. .name = "LDO4",
  1213. .id = WM8350_LDO_4,
  1214. .ops = &wm8350_ldo_ops,
  1215. .irq = WM8350_IRQ_UV_LDO4,
  1216. .type = REGULATOR_VOLTAGE,
  1217. .n_voltages = WM8350_LDO4_VSEL_MASK + 1,
  1218. .owner = THIS_MODULE,
  1219. },
  1220. {
  1221. .name = "ISINKA",
  1222. .id = WM8350_ISINK_A,
  1223. .ops = &wm8350_isink_ops,
  1224. .irq = WM8350_IRQ_CS1,
  1225. .type = REGULATOR_CURRENT,
  1226. .owner = THIS_MODULE,
  1227. },
  1228. {
  1229. .name = "ISINKB",
  1230. .id = WM8350_ISINK_B,
  1231. .ops = &wm8350_isink_ops,
  1232. .irq = WM8350_IRQ_CS2,
  1233. .type = REGULATOR_CURRENT,
  1234. .owner = THIS_MODULE,
  1235. },
  1236. };
  1237. static irqreturn_t pmic_uv_handler(int irq, void *data)
  1238. {
  1239. struct regulator_dev *rdev = (struct regulator_dev *)data;
  1240. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  1241. mutex_lock(&rdev->mutex);
  1242. if (irq == WM8350_IRQ_CS1 || irq == WM8350_IRQ_CS2)
  1243. regulator_notifier_call_chain(rdev,
  1244. REGULATOR_EVENT_REGULATION_OUT,
  1245. wm8350);
  1246. else
  1247. regulator_notifier_call_chain(rdev,
  1248. REGULATOR_EVENT_UNDER_VOLTAGE,
  1249. wm8350);
  1250. mutex_unlock(&rdev->mutex);
  1251. return IRQ_HANDLED;
  1252. }
  1253. static int wm8350_regulator_probe(struct platform_device *pdev)
  1254. {
  1255. struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev);
  1256. struct regulator_dev *rdev;
  1257. int ret;
  1258. u16 val;
  1259. if (pdev->id < WM8350_DCDC_1 || pdev->id > WM8350_ISINK_B)
  1260. return -ENODEV;
  1261. /* do any regulatior specific init */
  1262. switch (pdev->id) {
  1263. case WM8350_DCDC_1:
  1264. val = wm8350_reg_read(wm8350, WM8350_DCDC1_LOW_POWER);
  1265. wm8350->pmic.dcdc1_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  1266. break;
  1267. case WM8350_DCDC_3:
  1268. val = wm8350_reg_read(wm8350, WM8350_DCDC3_LOW_POWER);
  1269. wm8350->pmic.dcdc3_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  1270. break;
  1271. case WM8350_DCDC_4:
  1272. val = wm8350_reg_read(wm8350, WM8350_DCDC4_LOW_POWER);
  1273. wm8350->pmic.dcdc4_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  1274. break;
  1275. case WM8350_DCDC_6:
  1276. val = wm8350_reg_read(wm8350, WM8350_DCDC6_LOW_POWER);
  1277. wm8350->pmic.dcdc6_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  1278. break;
  1279. }
  1280. /* register regulator */
  1281. rdev = regulator_register(&wm8350_reg[pdev->id], &pdev->dev,
  1282. pdev->dev.platform_data,
  1283. dev_get_drvdata(&pdev->dev));
  1284. if (IS_ERR(rdev)) {
  1285. dev_err(&pdev->dev, "failed to register %s\n",
  1286. wm8350_reg[pdev->id].name);
  1287. return PTR_ERR(rdev);
  1288. }
  1289. /* register regulator IRQ */
  1290. ret = wm8350_register_irq(wm8350, wm8350_reg[pdev->id].irq,
  1291. pmic_uv_handler, 0, "UV", rdev);
  1292. if (ret < 0) {
  1293. regulator_unregister(rdev);
  1294. dev_err(&pdev->dev, "failed to register regulator %s IRQ\n",
  1295. wm8350_reg[pdev->id].name);
  1296. return ret;
  1297. }
  1298. return 0;
  1299. }
  1300. static int wm8350_regulator_remove(struct platform_device *pdev)
  1301. {
  1302. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  1303. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  1304. wm8350_free_irq(wm8350, wm8350_reg[pdev->id].irq, rdev);
  1305. regulator_unregister(rdev);
  1306. return 0;
  1307. }
  1308. int wm8350_register_regulator(struct wm8350 *wm8350, int reg,
  1309. struct regulator_init_data *initdata)
  1310. {
  1311. struct platform_device *pdev;
  1312. int ret;
  1313. if (reg < 0 || reg >= NUM_WM8350_REGULATORS)
  1314. return -EINVAL;
  1315. if (wm8350->pmic.pdev[reg])
  1316. return -EBUSY;
  1317. if (reg >= WM8350_DCDC_1 && reg <= WM8350_DCDC_6 &&
  1318. reg > wm8350->pmic.max_dcdc)
  1319. return -ENODEV;
  1320. if (reg >= WM8350_ISINK_A && reg <= WM8350_ISINK_B &&
  1321. reg > wm8350->pmic.max_isink)
  1322. return -ENODEV;
  1323. pdev = platform_device_alloc("wm8350-regulator", reg);
  1324. if (!pdev)
  1325. return -ENOMEM;
  1326. wm8350->pmic.pdev[reg] = pdev;
  1327. initdata->driver_data = wm8350;
  1328. pdev->dev.platform_data = initdata;
  1329. pdev->dev.parent = wm8350->dev;
  1330. platform_set_drvdata(pdev, wm8350);
  1331. ret = platform_device_add(pdev);
  1332. if (ret != 0) {
  1333. dev_err(wm8350->dev, "Failed to register regulator %d: %d\n",
  1334. reg, ret);
  1335. platform_device_put(pdev);
  1336. wm8350->pmic.pdev[reg] = NULL;
  1337. }
  1338. return ret;
  1339. }
  1340. EXPORT_SYMBOL_GPL(wm8350_register_regulator);
  1341. /**
  1342. * wm8350_register_led - Register a WM8350 LED output
  1343. *
  1344. * @param wm8350 The WM8350 device to configure.
  1345. * @param lednum LED device index to create.
  1346. * @param dcdc The DCDC to use for the LED.
  1347. * @param isink The ISINK to use for the LED.
  1348. * @param pdata Configuration for the LED.
  1349. *
  1350. * The WM8350 supports the use of an ISINK together with a DCDC to
  1351. * provide a power-efficient LED driver. This function registers the
  1352. * regulators and instantiates the platform device for a LED. The
  1353. * operating modes for the LED regulators must be configured using
  1354. * wm8350_isink_set_flash(), wm8350_dcdc25_set_mode() and
  1355. * wm8350_dcdc_set_slot() prior to calling this function.
  1356. */
  1357. int wm8350_register_led(struct wm8350 *wm8350, int lednum, int dcdc, int isink,
  1358. struct wm8350_led_platform_data *pdata)
  1359. {
  1360. struct wm8350_led *led;
  1361. struct platform_device *pdev;
  1362. int ret;
  1363. if (lednum >= ARRAY_SIZE(wm8350->pmic.led) || lednum < 0) {
  1364. dev_err(wm8350->dev, "Invalid LED index %d\n", lednum);
  1365. return -ENODEV;
  1366. }
  1367. led = &wm8350->pmic.led[lednum];
  1368. if (led->pdev) {
  1369. dev_err(wm8350->dev, "LED %d already allocated\n", lednum);
  1370. return -EINVAL;
  1371. }
  1372. pdev = platform_device_alloc("wm8350-led", lednum);
  1373. if (pdev == NULL) {
  1374. dev_err(wm8350->dev, "Failed to allocate LED %d\n", lednum);
  1375. return -ENOMEM;
  1376. }
  1377. led->isink_consumer.dev = &pdev->dev;
  1378. led->isink_consumer.supply = "led_isink";
  1379. led->isink_init.num_consumer_supplies = 1;
  1380. led->isink_init.consumer_supplies = &led->isink_consumer;
  1381. led->isink_init.constraints.min_uA = 0;
  1382. led->isink_init.constraints.max_uA = pdata->max_uA;
  1383. led->isink_init.constraints.valid_ops_mask
  1384. = REGULATOR_CHANGE_CURRENT | REGULATOR_CHANGE_STATUS;
  1385. led->isink_init.constraints.valid_modes_mask = REGULATOR_MODE_NORMAL;
  1386. ret = wm8350_register_regulator(wm8350, isink, &led->isink_init);
  1387. if (ret != 0) {
  1388. platform_device_put(pdev);
  1389. return ret;
  1390. }
  1391. led->dcdc_consumer.dev = &pdev->dev;
  1392. led->dcdc_consumer.supply = "led_vcc";
  1393. led->dcdc_init.num_consumer_supplies = 1;
  1394. led->dcdc_init.consumer_supplies = &led->dcdc_consumer;
  1395. led->dcdc_init.constraints.valid_modes_mask = REGULATOR_MODE_NORMAL;
  1396. led->dcdc_init.constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS;
  1397. ret = wm8350_register_regulator(wm8350, dcdc, &led->dcdc_init);
  1398. if (ret != 0) {
  1399. platform_device_put(pdev);
  1400. return ret;
  1401. }
  1402. switch (isink) {
  1403. case WM8350_ISINK_A:
  1404. wm8350->pmic.isink_A_dcdc = dcdc;
  1405. break;
  1406. case WM8350_ISINK_B:
  1407. wm8350->pmic.isink_B_dcdc = dcdc;
  1408. break;
  1409. }
  1410. pdev->dev.platform_data = pdata;
  1411. pdev->dev.parent = wm8350->dev;
  1412. ret = platform_device_add(pdev);
  1413. if (ret != 0) {
  1414. dev_err(wm8350->dev, "Failed to register LED %d: %d\n",
  1415. lednum, ret);
  1416. platform_device_put(pdev);
  1417. return ret;
  1418. }
  1419. led->pdev = pdev;
  1420. return 0;
  1421. }
  1422. EXPORT_SYMBOL_GPL(wm8350_register_led);
  1423. static struct platform_driver wm8350_regulator_driver = {
  1424. .probe = wm8350_regulator_probe,
  1425. .remove = wm8350_regulator_remove,
  1426. .driver = {
  1427. .name = "wm8350-regulator",
  1428. },
  1429. };
  1430. static int __init wm8350_regulator_init(void)
  1431. {
  1432. return platform_driver_register(&wm8350_regulator_driver);
  1433. }
  1434. subsys_initcall(wm8350_regulator_init);
  1435. static void __exit wm8350_regulator_exit(void)
  1436. {
  1437. platform_driver_unregister(&wm8350_regulator_driver);
  1438. }
  1439. module_exit(wm8350_regulator_exit);
  1440. /* Module information */
  1441. MODULE_AUTHOR("Liam Girdwood");
  1442. MODULE_DESCRIPTION("WM8350 voltage and current regulator driver");
  1443. MODULE_LICENSE("GPL");
  1444. MODULE_ALIAS("platform:wm8350-regulator");