wm8350-regulator.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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 = 0; i < ARRAY_SIZE(isink_cur); 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 int wm8350_isink_set_current(struct regulator_dev *rdev, int min_uA,
  106. int max_uA)
  107. {
  108. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  109. int isink = rdev_get_id(rdev);
  110. u16 val, setting;
  111. int ret;
  112. ret = get_isink_val(min_uA, max_uA, &setting);
  113. if (ret != 0)
  114. return ret;
  115. switch (isink) {
  116. case WM8350_ISINK_A:
  117. val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_A) &
  118. ~WM8350_CS1_ISEL_MASK;
  119. wm8350_reg_write(wm8350, WM8350_CURRENT_SINK_DRIVER_A,
  120. val | setting);
  121. break;
  122. case WM8350_ISINK_B:
  123. val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_B) &
  124. ~WM8350_CS1_ISEL_MASK;
  125. wm8350_reg_write(wm8350, WM8350_CURRENT_SINK_DRIVER_B,
  126. val | setting);
  127. break;
  128. default:
  129. return -EINVAL;
  130. }
  131. return 0;
  132. }
  133. static int wm8350_isink_get_current(struct regulator_dev *rdev)
  134. {
  135. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  136. int isink = rdev_get_id(rdev);
  137. u16 val;
  138. switch (isink) {
  139. case WM8350_ISINK_A:
  140. val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_A) &
  141. WM8350_CS1_ISEL_MASK;
  142. break;
  143. case WM8350_ISINK_B:
  144. val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_B) &
  145. WM8350_CS1_ISEL_MASK;
  146. break;
  147. default:
  148. return 0;
  149. }
  150. return isink_cur[val];
  151. }
  152. /* turn on ISINK followed by DCDC */
  153. static int wm8350_isink_enable(struct regulator_dev *rdev)
  154. {
  155. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  156. int isink = rdev_get_id(rdev);
  157. switch (isink) {
  158. case WM8350_ISINK_A:
  159. switch (wm8350->pmic.isink_A_dcdc) {
  160. case WM8350_DCDC_2:
  161. case WM8350_DCDC_5:
  162. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_7,
  163. WM8350_CS1_ENA);
  164. wm8350_set_bits(wm8350, WM8350_CSA_FLASH_CONTROL,
  165. WM8350_CS1_DRIVE);
  166. wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
  167. 1 << (wm8350->pmic.isink_A_dcdc -
  168. WM8350_DCDC_1));
  169. break;
  170. default:
  171. return -EINVAL;
  172. }
  173. break;
  174. case WM8350_ISINK_B:
  175. switch (wm8350->pmic.isink_B_dcdc) {
  176. case WM8350_DCDC_2:
  177. case WM8350_DCDC_5:
  178. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_7,
  179. WM8350_CS2_ENA);
  180. wm8350_set_bits(wm8350, WM8350_CSB_FLASH_CONTROL,
  181. WM8350_CS2_DRIVE);
  182. wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
  183. 1 << (wm8350->pmic.isink_B_dcdc -
  184. WM8350_DCDC_1));
  185. break;
  186. default:
  187. return -EINVAL;
  188. }
  189. break;
  190. default:
  191. return -EINVAL;
  192. }
  193. return 0;
  194. }
  195. static int wm8350_isink_disable(struct regulator_dev *rdev)
  196. {
  197. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  198. int isink = rdev_get_id(rdev);
  199. switch (isink) {
  200. case WM8350_ISINK_A:
  201. switch (wm8350->pmic.isink_A_dcdc) {
  202. case WM8350_DCDC_2:
  203. case WM8350_DCDC_5:
  204. wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
  205. 1 << (wm8350->pmic.isink_A_dcdc -
  206. WM8350_DCDC_1));
  207. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_7,
  208. WM8350_CS1_ENA);
  209. break;
  210. default:
  211. return -EINVAL;
  212. }
  213. break;
  214. case WM8350_ISINK_B:
  215. switch (wm8350->pmic.isink_B_dcdc) {
  216. case WM8350_DCDC_2:
  217. case WM8350_DCDC_5:
  218. wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
  219. 1 << (wm8350->pmic.isink_B_dcdc -
  220. WM8350_DCDC_1));
  221. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_7,
  222. WM8350_CS2_ENA);
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. break;
  228. default:
  229. return -EINVAL;
  230. }
  231. return 0;
  232. }
  233. static int wm8350_isink_is_enabled(struct regulator_dev *rdev)
  234. {
  235. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  236. int isink = rdev_get_id(rdev);
  237. switch (isink) {
  238. case WM8350_ISINK_A:
  239. return wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_A) &
  240. 0x8000;
  241. case WM8350_ISINK_B:
  242. return wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_B) &
  243. 0x8000;
  244. }
  245. return -EINVAL;
  246. }
  247. static int wm8350_isink_enable_time(struct regulator_dev *rdev)
  248. {
  249. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  250. int isink = rdev_get_id(rdev);
  251. int reg;
  252. switch (isink) {
  253. case WM8350_ISINK_A:
  254. reg = wm8350_reg_read(wm8350, WM8350_CSA_FLASH_CONTROL);
  255. break;
  256. case WM8350_ISINK_B:
  257. reg = wm8350_reg_read(wm8350, WM8350_CSB_FLASH_CONTROL);
  258. break;
  259. default:
  260. return -EINVAL;
  261. }
  262. if (reg & WM8350_CS1_FLASH_MODE) {
  263. switch (reg & WM8350_CS1_ON_RAMP_MASK) {
  264. case 0:
  265. return 0;
  266. case 1:
  267. return 1950;
  268. case 2:
  269. return 3910;
  270. case 3:
  271. return 7800;
  272. }
  273. } else {
  274. switch (reg & WM8350_CS1_ON_RAMP_MASK) {
  275. case 0:
  276. return 0;
  277. case 1:
  278. return 250000;
  279. case 2:
  280. return 500000;
  281. case 3:
  282. return 1000000;
  283. }
  284. }
  285. return -EINVAL;
  286. }
  287. int wm8350_isink_set_flash(struct wm8350 *wm8350, int isink, u16 mode,
  288. u16 trigger, u16 duration, u16 on_ramp, u16 off_ramp,
  289. u16 drive)
  290. {
  291. switch (isink) {
  292. case WM8350_ISINK_A:
  293. wm8350_reg_write(wm8350, WM8350_CSA_FLASH_CONTROL,
  294. (mode ? WM8350_CS1_FLASH_MODE : 0) |
  295. (trigger ? WM8350_CS1_TRIGSRC : 0) |
  296. duration | on_ramp | off_ramp | drive);
  297. break;
  298. case WM8350_ISINK_B:
  299. wm8350_reg_write(wm8350, WM8350_CSB_FLASH_CONTROL,
  300. (mode ? WM8350_CS2_FLASH_MODE : 0) |
  301. (trigger ? WM8350_CS2_TRIGSRC : 0) |
  302. duration | on_ramp | off_ramp | drive);
  303. break;
  304. default:
  305. return -EINVAL;
  306. }
  307. return 0;
  308. }
  309. EXPORT_SYMBOL_GPL(wm8350_isink_set_flash);
  310. static int wm8350_dcdc_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  311. {
  312. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  313. int sel, volt_reg, dcdc = rdev_get_id(rdev);
  314. u16 val;
  315. dev_dbg(wm8350->dev, "%s %d mV %d\n", __func__, dcdc, uV / 1000);
  316. switch (dcdc) {
  317. case WM8350_DCDC_1:
  318. volt_reg = WM8350_DCDC1_LOW_POWER;
  319. break;
  320. case WM8350_DCDC_3:
  321. volt_reg = WM8350_DCDC3_LOW_POWER;
  322. break;
  323. case WM8350_DCDC_4:
  324. volt_reg = WM8350_DCDC4_LOW_POWER;
  325. break;
  326. case WM8350_DCDC_6:
  327. volt_reg = WM8350_DCDC6_LOW_POWER;
  328. break;
  329. case WM8350_DCDC_2:
  330. case WM8350_DCDC_5:
  331. default:
  332. return -EINVAL;
  333. }
  334. sel = regulator_map_voltage_linear(rdev, uV, uV);
  335. if (sel < 0)
  336. return sel;
  337. /* all DCDCs have same mV bits */
  338. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_DC1_VSEL_MASK;
  339. wm8350_reg_write(wm8350, volt_reg, val | sel);
  340. return 0;
  341. }
  342. static int wm8350_dcdc_set_suspend_enable(struct regulator_dev *rdev)
  343. {
  344. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  345. int dcdc = rdev_get_id(rdev);
  346. u16 val;
  347. switch (dcdc) {
  348. case WM8350_DCDC_1:
  349. val = wm8350_reg_read(wm8350, WM8350_DCDC1_LOW_POWER)
  350. & ~WM8350_DCDC_HIB_MODE_MASK;
  351. wm8350_reg_write(wm8350, WM8350_DCDC1_LOW_POWER,
  352. val | wm8350->pmic.dcdc1_hib_mode);
  353. break;
  354. case WM8350_DCDC_3:
  355. val = wm8350_reg_read(wm8350, WM8350_DCDC3_LOW_POWER)
  356. & ~WM8350_DCDC_HIB_MODE_MASK;
  357. wm8350_reg_write(wm8350, WM8350_DCDC3_LOW_POWER,
  358. val | wm8350->pmic.dcdc3_hib_mode);
  359. break;
  360. case WM8350_DCDC_4:
  361. val = wm8350_reg_read(wm8350, WM8350_DCDC4_LOW_POWER)
  362. & ~WM8350_DCDC_HIB_MODE_MASK;
  363. wm8350_reg_write(wm8350, WM8350_DCDC4_LOW_POWER,
  364. val | wm8350->pmic.dcdc4_hib_mode);
  365. break;
  366. case WM8350_DCDC_6:
  367. val = wm8350_reg_read(wm8350, WM8350_DCDC6_LOW_POWER)
  368. & ~WM8350_DCDC_HIB_MODE_MASK;
  369. wm8350_reg_write(wm8350, WM8350_DCDC6_LOW_POWER,
  370. val | wm8350->pmic.dcdc6_hib_mode);
  371. break;
  372. case WM8350_DCDC_2:
  373. case WM8350_DCDC_5:
  374. default:
  375. return -EINVAL;
  376. }
  377. return 0;
  378. }
  379. static int wm8350_dcdc_set_suspend_disable(struct regulator_dev *rdev)
  380. {
  381. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  382. int dcdc = rdev_get_id(rdev);
  383. u16 val;
  384. switch (dcdc) {
  385. case WM8350_DCDC_1:
  386. val = wm8350_reg_read(wm8350, WM8350_DCDC1_LOW_POWER);
  387. wm8350->pmic.dcdc1_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  388. wm8350_reg_write(wm8350, WM8350_DCDC1_LOW_POWER,
  389. val | WM8350_DCDC_HIB_MODE_DIS);
  390. break;
  391. case WM8350_DCDC_3:
  392. val = wm8350_reg_read(wm8350, WM8350_DCDC3_LOW_POWER);
  393. wm8350->pmic.dcdc3_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  394. wm8350_reg_write(wm8350, WM8350_DCDC3_LOW_POWER,
  395. val | WM8350_DCDC_HIB_MODE_DIS);
  396. break;
  397. case WM8350_DCDC_4:
  398. val = wm8350_reg_read(wm8350, WM8350_DCDC4_LOW_POWER);
  399. wm8350->pmic.dcdc4_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  400. wm8350_reg_write(wm8350, WM8350_DCDC4_LOW_POWER,
  401. val | WM8350_DCDC_HIB_MODE_DIS);
  402. break;
  403. case WM8350_DCDC_6:
  404. val = wm8350_reg_read(wm8350, WM8350_DCDC6_LOW_POWER);
  405. wm8350->pmic.dcdc6_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  406. wm8350_reg_write(wm8350, WM8350_DCDC6_LOW_POWER,
  407. val | WM8350_DCDC_HIB_MODE_DIS);
  408. break;
  409. case WM8350_DCDC_2:
  410. case WM8350_DCDC_5:
  411. default:
  412. return -EINVAL;
  413. }
  414. return 0;
  415. }
  416. static int wm8350_dcdc25_set_suspend_enable(struct regulator_dev *rdev)
  417. {
  418. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  419. int dcdc = rdev_get_id(rdev);
  420. u16 val;
  421. switch (dcdc) {
  422. case WM8350_DCDC_2:
  423. val = wm8350_reg_read(wm8350, WM8350_DCDC2_CONTROL)
  424. & ~WM8350_DC2_HIB_MODE_MASK;
  425. wm8350_reg_write(wm8350, WM8350_DCDC2_CONTROL, val |
  426. (WM8350_DC2_HIB_MODE_ACTIVE << WM8350_DC2_HIB_MODE_SHIFT));
  427. break;
  428. case WM8350_DCDC_5:
  429. val = wm8350_reg_read(wm8350, WM8350_DCDC5_CONTROL)
  430. & ~WM8350_DC5_HIB_MODE_MASK;
  431. wm8350_reg_write(wm8350, WM8350_DCDC5_CONTROL, val |
  432. (WM8350_DC5_HIB_MODE_ACTIVE << WM8350_DC5_HIB_MODE_SHIFT));
  433. break;
  434. default:
  435. return -EINVAL;
  436. }
  437. return 0;
  438. }
  439. static int wm8350_dcdc25_set_suspend_disable(struct regulator_dev *rdev)
  440. {
  441. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  442. int dcdc = rdev_get_id(rdev);
  443. u16 val;
  444. switch (dcdc) {
  445. case WM8350_DCDC_2:
  446. val = wm8350_reg_read(wm8350, WM8350_DCDC2_CONTROL)
  447. & ~WM8350_DC2_HIB_MODE_MASK;
  448. wm8350_reg_write(wm8350, WM8350_DCDC2_CONTROL, val |
  449. (WM8350_DC2_HIB_MODE_DISABLE << WM8350_DC2_HIB_MODE_SHIFT));
  450. break;
  451. case WM8350_DCDC_5:
  452. val = wm8350_reg_read(wm8350, WM8350_DCDC5_CONTROL)
  453. & ~WM8350_DC5_HIB_MODE_MASK;
  454. wm8350_reg_write(wm8350, WM8350_DCDC5_CONTROL, val |
  455. (WM8350_DC5_HIB_MODE_DISABLE << WM8350_DC5_HIB_MODE_SHIFT));
  456. break;
  457. default:
  458. return -EINVAL;
  459. }
  460. return 0;
  461. }
  462. static int wm8350_dcdc_set_suspend_mode(struct regulator_dev *rdev,
  463. unsigned int mode)
  464. {
  465. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  466. int dcdc = rdev_get_id(rdev);
  467. u16 *hib_mode;
  468. switch (dcdc) {
  469. case WM8350_DCDC_1:
  470. hib_mode = &wm8350->pmic.dcdc1_hib_mode;
  471. break;
  472. case WM8350_DCDC_3:
  473. hib_mode = &wm8350->pmic.dcdc3_hib_mode;
  474. break;
  475. case WM8350_DCDC_4:
  476. hib_mode = &wm8350->pmic.dcdc4_hib_mode;
  477. break;
  478. case WM8350_DCDC_6:
  479. hib_mode = &wm8350->pmic.dcdc6_hib_mode;
  480. break;
  481. case WM8350_DCDC_2:
  482. case WM8350_DCDC_5:
  483. default:
  484. return -EINVAL;
  485. }
  486. switch (mode) {
  487. case REGULATOR_MODE_NORMAL:
  488. *hib_mode = WM8350_DCDC_HIB_MODE_IMAGE;
  489. break;
  490. case REGULATOR_MODE_IDLE:
  491. *hib_mode = WM8350_DCDC_HIB_MODE_STANDBY;
  492. break;
  493. case REGULATOR_MODE_STANDBY:
  494. *hib_mode = WM8350_DCDC_HIB_MODE_LDO_IM;
  495. break;
  496. default:
  497. return -EINVAL;
  498. }
  499. return 0;
  500. }
  501. static const struct regulator_linear_range wm8350_ldo_ranges[] = {
  502. REGULATOR_LINEAR_RANGE(900000, 0, 15, 50000),
  503. REGULATOR_LINEAR_RANGE(1800000, 16, 31, 100000),
  504. };
  505. static int wm8350_ldo_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  506. {
  507. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  508. int sel, volt_reg, ldo = rdev_get_id(rdev);
  509. u16 val;
  510. dev_dbg(wm8350->dev, "%s %d mV %d\n", __func__, ldo, uV / 1000);
  511. switch (ldo) {
  512. case WM8350_LDO_1:
  513. volt_reg = WM8350_LDO1_LOW_POWER;
  514. break;
  515. case WM8350_LDO_2:
  516. volt_reg = WM8350_LDO2_LOW_POWER;
  517. break;
  518. case WM8350_LDO_3:
  519. volt_reg = WM8350_LDO3_LOW_POWER;
  520. break;
  521. case WM8350_LDO_4:
  522. volt_reg = WM8350_LDO4_LOW_POWER;
  523. break;
  524. default:
  525. return -EINVAL;
  526. }
  527. sel = regulator_map_voltage_linear_range(rdev, uV, uV);
  528. if (sel < 0)
  529. return sel;
  530. /* all LDOs have same mV bits */
  531. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_VSEL_MASK;
  532. wm8350_reg_write(wm8350, volt_reg, val | sel);
  533. return 0;
  534. }
  535. static int wm8350_ldo_set_suspend_enable(struct regulator_dev *rdev)
  536. {
  537. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  538. int volt_reg, ldo = rdev_get_id(rdev);
  539. u16 val;
  540. switch (ldo) {
  541. case WM8350_LDO_1:
  542. volt_reg = WM8350_LDO1_LOW_POWER;
  543. break;
  544. case WM8350_LDO_2:
  545. volt_reg = WM8350_LDO2_LOW_POWER;
  546. break;
  547. case WM8350_LDO_3:
  548. volt_reg = WM8350_LDO3_LOW_POWER;
  549. break;
  550. case WM8350_LDO_4:
  551. volt_reg = WM8350_LDO4_LOW_POWER;
  552. break;
  553. default:
  554. return -EINVAL;
  555. }
  556. /* all LDOs have same mV bits */
  557. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_HIB_MODE_MASK;
  558. wm8350_reg_write(wm8350, volt_reg, val);
  559. return 0;
  560. }
  561. static int wm8350_ldo_set_suspend_disable(struct regulator_dev *rdev)
  562. {
  563. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  564. int volt_reg, ldo = rdev_get_id(rdev);
  565. u16 val;
  566. switch (ldo) {
  567. case WM8350_LDO_1:
  568. volt_reg = WM8350_LDO1_LOW_POWER;
  569. break;
  570. case WM8350_LDO_2:
  571. volt_reg = WM8350_LDO2_LOW_POWER;
  572. break;
  573. case WM8350_LDO_3:
  574. volt_reg = WM8350_LDO3_LOW_POWER;
  575. break;
  576. case WM8350_LDO_4:
  577. volt_reg = WM8350_LDO4_LOW_POWER;
  578. break;
  579. default:
  580. return -EINVAL;
  581. }
  582. /* all LDOs have same mV bits */
  583. val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_HIB_MODE_MASK;
  584. wm8350_reg_write(wm8350, volt_reg, val | WM8350_LDO1_HIB_MODE_DIS);
  585. return 0;
  586. }
  587. int wm8350_dcdc_set_slot(struct wm8350 *wm8350, int dcdc, u16 start,
  588. u16 stop, u16 fault)
  589. {
  590. int slot_reg;
  591. u16 val;
  592. dev_dbg(wm8350->dev, "%s %d start %d stop %d\n",
  593. __func__, dcdc, start, stop);
  594. /* slot valid ? */
  595. if (start > 15 || stop > 15)
  596. return -EINVAL;
  597. switch (dcdc) {
  598. case WM8350_DCDC_1:
  599. slot_reg = WM8350_DCDC1_TIMEOUTS;
  600. break;
  601. case WM8350_DCDC_2:
  602. slot_reg = WM8350_DCDC2_TIMEOUTS;
  603. break;
  604. case WM8350_DCDC_3:
  605. slot_reg = WM8350_DCDC3_TIMEOUTS;
  606. break;
  607. case WM8350_DCDC_4:
  608. slot_reg = WM8350_DCDC4_TIMEOUTS;
  609. break;
  610. case WM8350_DCDC_5:
  611. slot_reg = WM8350_DCDC5_TIMEOUTS;
  612. break;
  613. case WM8350_DCDC_6:
  614. slot_reg = WM8350_DCDC6_TIMEOUTS;
  615. break;
  616. default:
  617. return -EINVAL;
  618. }
  619. val = wm8350_reg_read(wm8350, slot_reg) &
  620. ~(WM8350_DC1_ENSLOT_MASK | WM8350_DC1_SDSLOT_MASK |
  621. WM8350_DC1_ERRACT_MASK);
  622. wm8350_reg_write(wm8350, slot_reg,
  623. val | (start << WM8350_DC1_ENSLOT_SHIFT) |
  624. (stop << WM8350_DC1_SDSLOT_SHIFT) |
  625. (fault << WM8350_DC1_ERRACT_SHIFT));
  626. return 0;
  627. }
  628. EXPORT_SYMBOL_GPL(wm8350_dcdc_set_slot);
  629. int wm8350_ldo_set_slot(struct wm8350 *wm8350, int ldo, u16 start, u16 stop)
  630. {
  631. int slot_reg;
  632. u16 val;
  633. dev_dbg(wm8350->dev, "%s %d start %d stop %d\n",
  634. __func__, ldo, start, stop);
  635. /* slot valid ? */
  636. if (start > 15 || stop > 15)
  637. return -EINVAL;
  638. switch (ldo) {
  639. case WM8350_LDO_1:
  640. slot_reg = WM8350_LDO1_TIMEOUTS;
  641. break;
  642. case WM8350_LDO_2:
  643. slot_reg = WM8350_LDO2_TIMEOUTS;
  644. break;
  645. case WM8350_LDO_3:
  646. slot_reg = WM8350_LDO3_TIMEOUTS;
  647. break;
  648. case WM8350_LDO_4:
  649. slot_reg = WM8350_LDO4_TIMEOUTS;
  650. break;
  651. default:
  652. return -EINVAL;
  653. }
  654. val = wm8350_reg_read(wm8350, slot_reg) & ~WM8350_LDO1_SDSLOT_MASK;
  655. wm8350_reg_write(wm8350, slot_reg, val | ((start << 10) | (stop << 6)));
  656. return 0;
  657. }
  658. EXPORT_SYMBOL_GPL(wm8350_ldo_set_slot);
  659. int wm8350_dcdc25_set_mode(struct wm8350 *wm8350, int dcdc, u16 mode,
  660. u16 ilim, u16 ramp, u16 feedback)
  661. {
  662. u16 val;
  663. dev_dbg(wm8350->dev, "%s %d mode: %s %s\n", __func__, dcdc,
  664. mode ? "normal" : "boost", ilim ? "low" : "normal");
  665. switch (dcdc) {
  666. case WM8350_DCDC_2:
  667. val = wm8350_reg_read(wm8350, WM8350_DCDC2_CONTROL)
  668. & ~(WM8350_DC2_MODE_MASK | WM8350_DC2_ILIM_MASK |
  669. WM8350_DC2_RMP_MASK | WM8350_DC2_FBSRC_MASK);
  670. wm8350_reg_write(wm8350, WM8350_DCDC2_CONTROL, val |
  671. (mode << WM8350_DC2_MODE_SHIFT) |
  672. (ilim << WM8350_DC2_ILIM_SHIFT) |
  673. (ramp << WM8350_DC2_RMP_SHIFT) |
  674. (feedback << WM8350_DC2_FBSRC_SHIFT));
  675. break;
  676. case WM8350_DCDC_5:
  677. val = wm8350_reg_read(wm8350, WM8350_DCDC5_CONTROL)
  678. & ~(WM8350_DC5_MODE_MASK | WM8350_DC5_ILIM_MASK |
  679. WM8350_DC5_RMP_MASK | WM8350_DC5_FBSRC_MASK);
  680. wm8350_reg_write(wm8350, WM8350_DCDC5_CONTROL, val |
  681. (mode << WM8350_DC5_MODE_SHIFT) |
  682. (ilim << WM8350_DC5_ILIM_SHIFT) |
  683. (ramp << WM8350_DC5_RMP_SHIFT) |
  684. (feedback << WM8350_DC5_FBSRC_SHIFT));
  685. break;
  686. default:
  687. return -EINVAL;
  688. }
  689. return 0;
  690. }
  691. EXPORT_SYMBOL_GPL(wm8350_dcdc25_set_mode);
  692. static int force_continuous_enable(struct wm8350 *wm8350, int dcdc, int enable)
  693. {
  694. int reg = 0, ret;
  695. switch (dcdc) {
  696. case WM8350_DCDC_1:
  697. reg = WM8350_DCDC1_FORCE_PWM;
  698. break;
  699. case WM8350_DCDC_3:
  700. reg = WM8350_DCDC3_FORCE_PWM;
  701. break;
  702. case WM8350_DCDC_4:
  703. reg = WM8350_DCDC4_FORCE_PWM;
  704. break;
  705. case WM8350_DCDC_6:
  706. reg = WM8350_DCDC6_FORCE_PWM;
  707. break;
  708. default:
  709. return -EINVAL;
  710. }
  711. if (enable)
  712. ret = wm8350_set_bits(wm8350, reg,
  713. WM8350_DCDC1_FORCE_PWM_ENA);
  714. else
  715. ret = wm8350_clear_bits(wm8350, reg,
  716. WM8350_DCDC1_FORCE_PWM_ENA);
  717. return ret;
  718. }
  719. static int wm8350_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
  720. {
  721. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  722. int dcdc = rdev_get_id(rdev);
  723. u16 val;
  724. if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
  725. return -EINVAL;
  726. if (dcdc == WM8350_DCDC_2 || dcdc == WM8350_DCDC_5)
  727. return -EINVAL;
  728. val = 1 << (dcdc - WM8350_DCDC_1);
  729. switch (mode) {
  730. case REGULATOR_MODE_FAST:
  731. /* force continuous mode */
  732. wm8350_set_bits(wm8350, WM8350_DCDC_ACTIVE_OPTIONS, val);
  733. wm8350_clear_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
  734. force_continuous_enable(wm8350, dcdc, 1);
  735. break;
  736. case REGULATOR_MODE_NORMAL:
  737. /* active / pulse skipping */
  738. wm8350_set_bits(wm8350, WM8350_DCDC_ACTIVE_OPTIONS, val);
  739. wm8350_clear_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
  740. force_continuous_enable(wm8350, dcdc, 0);
  741. break;
  742. case REGULATOR_MODE_IDLE:
  743. /* standby mode */
  744. force_continuous_enable(wm8350, dcdc, 0);
  745. wm8350_clear_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
  746. wm8350_clear_bits(wm8350, WM8350_DCDC_ACTIVE_OPTIONS, val);
  747. break;
  748. case REGULATOR_MODE_STANDBY:
  749. /* LDO mode */
  750. force_continuous_enable(wm8350, dcdc, 0);
  751. wm8350_set_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
  752. break;
  753. }
  754. return 0;
  755. }
  756. static unsigned int wm8350_dcdc_get_mode(struct regulator_dev *rdev)
  757. {
  758. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  759. int dcdc = rdev_get_id(rdev);
  760. u16 mask, sleep, active, force;
  761. int mode = REGULATOR_MODE_NORMAL;
  762. int reg;
  763. switch (dcdc) {
  764. case WM8350_DCDC_1:
  765. reg = WM8350_DCDC1_FORCE_PWM;
  766. break;
  767. case WM8350_DCDC_3:
  768. reg = WM8350_DCDC3_FORCE_PWM;
  769. break;
  770. case WM8350_DCDC_4:
  771. reg = WM8350_DCDC4_FORCE_PWM;
  772. break;
  773. case WM8350_DCDC_6:
  774. reg = WM8350_DCDC6_FORCE_PWM;
  775. break;
  776. default:
  777. return -EINVAL;
  778. }
  779. mask = 1 << (dcdc - WM8350_DCDC_1);
  780. active = wm8350_reg_read(wm8350, WM8350_DCDC_ACTIVE_OPTIONS) & mask;
  781. force = wm8350_reg_read(wm8350, reg) & WM8350_DCDC1_FORCE_PWM_ENA;
  782. sleep = wm8350_reg_read(wm8350, WM8350_DCDC_SLEEP_OPTIONS) & mask;
  783. dev_dbg(wm8350->dev, "mask %x active %x sleep %x force %x",
  784. mask, active, sleep, force);
  785. if (active && !sleep) {
  786. if (force)
  787. mode = REGULATOR_MODE_FAST;
  788. else
  789. mode = REGULATOR_MODE_NORMAL;
  790. } else if (!active && !sleep)
  791. mode = REGULATOR_MODE_IDLE;
  792. else if (sleep)
  793. mode = REGULATOR_MODE_STANDBY;
  794. return mode;
  795. }
  796. static unsigned int wm8350_ldo_get_mode(struct regulator_dev *rdev)
  797. {
  798. return REGULATOR_MODE_NORMAL;
  799. }
  800. struct wm8350_dcdc_efficiency {
  801. int uA_load_min;
  802. int uA_load_max;
  803. unsigned int mode;
  804. };
  805. static const struct wm8350_dcdc_efficiency dcdc1_6_efficiency[] = {
  806. {0, 10000, REGULATOR_MODE_STANDBY}, /* 0 - 10mA - LDO */
  807. {10000, 100000, REGULATOR_MODE_IDLE}, /* 10mA - 100mA - Standby */
  808. {100000, 1000000, REGULATOR_MODE_NORMAL}, /* > 100mA - Active */
  809. {-1, -1, REGULATOR_MODE_NORMAL},
  810. };
  811. static const struct wm8350_dcdc_efficiency dcdc3_4_efficiency[] = {
  812. {0, 10000, REGULATOR_MODE_STANDBY}, /* 0 - 10mA - LDO */
  813. {10000, 100000, REGULATOR_MODE_IDLE}, /* 10mA - 100mA - Standby */
  814. {100000, 800000, REGULATOR_MODE_NORMAL}, /* > 100mA - Active */
  815. {-1, -1, REGULATOR_MODE_NORMAL},
  816. };
  817. static unsigned int get_mode(int uA, const struct wm8350_dcdc_efficiency *eff)
  818. {
  819. int i = 0;
  820. while (eff[i].uA_load_min != -1) {
  821. if (uA >= eff[i].uA_load_min && uA <= eff[i].uA_load_max)
  822. return eff[i].mode;
  823. }
  824. return REGULATOR_MODE_NORMAL;
  825. }
  826. /* Query the regulator for it's most efficient mode @ uV,uA
  827. * WM8350 regulator efficiency is pretty similar over
  828. * different input and output uV.
  829. */
  830. static unsigned int wm8350_dcdc_get_optimum_mode(struct regulator_dev *rdev,
  831. int input_uV, int output_uV,
  832. int output_uA)
  833. {
  834. int dcdc = rdev_get_id(rdev), mode;
  835. switch (dcdc) {
  836. case WM8350_DCDC_1:
  837. case WM8350_DCDC_6:
  838. mode = get_mode(output_uA, dcdc1_6_efficiency);
  839. break;
  840. case WM8350_DCDC_3:
  841. case WM8350_DCDC_4:
  842. mode = get_mode(output_uA, dcdc3_4_efficiency);
  843. break;
  844. default:
  845. mode = REGULATOR_MODE_NORMAL;
  846. break;
  847. }
  848. return mode;
  849. }
  850. static const struct regulator_ops wm8350_dcdc_ops = {
  851. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  852. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  853. .list_voltage = regulator_list_voltage_linear,
  854. .map_voltage = regulator_map_voltage_linear,
  855. .enable = regulator_enable_regmap,
  856. .disable = regulator_disable_regmap,
  857. .is_enabled = regulator_is_enabled_regmap,
  858. .get_mode = wm8350_dcdc_get_mode,
  859. .set_mode = wm8350_dcdc_set_mode,
  860. .get_optimum_mode = wm8350_dcdc_get_optimum_mode,
  861. .set_suspend_voltage = wm8350_dcdc_set_suspend_voltage,
  862. .set_suspend_enable = wm8350_dcdc_set_suspend_enable,
  863. .set_suspend_disable = wm8350_dcdc_set_suspend_disable,
  864. .set_suspend_mode = wm8350_dcdc_set_suspend_mode,
  865. };
  866. static const struct regulator_ops wm8350_dcdc2_5_ops = {
  867. .enable = regulator_enable_regmap,
  868. .disable = regulator_disable_regmap,
  869. .is_enabled = regulator_is_enabled_regmap,
  870. .set_suspend_enable = wm8350_dcdc25_set_suspend_enable,
  871. .set_suspend_disable = wm8350_dcdc25_set_suspend_disable,
  872. };
  873. static const struct regulator_ops wm8350_ldo_ops = {
  874. .map_voltage = regulator_map_voltage_linear_range,
  875. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  876. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  877. .list_voltage = regulator_list_voltage_linear_range,
  878. .enable = regulator_enable_regmap,
  879. .disable = regulator_disable_regmap,
  880. .is_enabled = regulator_is_enabled_regmap,
  881. .get_mode = wm8350_ldo_get_mode,
  882. .set_suspend_voltage = wm8350_ldo_set_suspend_voltage,
  883. .set_suspend_enable = wm8350_ldo_set_suspend_enable,
  884. .set_suspend_disable = wm8350_ldo_set_suspend_disable,
  885. };
  886. static const struct regulator_ops wm8350_isink_ops = {
  887. .set_current_limit = wm8350_isink_set_current,
  888. .get_current_limit = wm8350_isink_get_current,
  889. .enable = wm8350_isink_enable,
  890. .disable = wm8350_isink_disable,
  891. .is_enabled = wm8350_isink_is_enabled,
  892. .enable_time = wm8350_isink_enable_time,
  893. };
  894. static const struct regulator_desc wm8350_reg[NUM_WM8350_REGULATORS] = {
  895. {
  896. .name = "DCDC1",
  897. .id = WM8350_DCDC_1,
  898. .ops = &wm8350_dcdc_ops,
  899. .irq = WM8350_IRQ_UV_DC1,
  900. .type = REGULATOR_VOLTAGE,
  901. .n_voltages = WM8350_DCDC_MAX_VSEL + 1,
  902. .min_uV = 850000,
  903. .uV_step = 25000,
  904. .vsel_reg = WM8350_DCDC1_CONTROL,
  905. .vsel_mask = WM8350_DC1_VSEL_MASK,
  906. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  907. .enable_mask = WM8350_DC1_ENA,
  908. .owner = THIS_MODULE,
  909. },
  910. {
  911. .name = "DCDC2",
  912. .id = WM8350_DCDC_2,
  913. .ops = &wm8350_dcdc2_5_ops,
  914. .irq = WM8350_IRQ_UV_DC2,
  915. .type = REGULATOR_VOLTAGE,
  916. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  917. .enable_mask = WM8350_DC2_ENA,
  918. .owner = THIS_MODULE,
  919. },
  920. {
  921. .name = "DCDC3",
  922. .id = WM8350_DCDC_3,
  923. .ops = &wm8350_dcdc_ops,
  924. .irq = WM8350_IRQ_UV_DC3,
  925. .type = REGULATOR_VOLTAGE,
  926. .n_voltages = WM8350_DCDC_MAX_VSEL + 1,
  927. .min_uV = 850000,
  928. .uV_step = 25000,
  929. .vsel_reg = WM8350_DCDC3_CONTROL,
  930. .vsel_mask = WM8350_DC3_VSEL_MASK,
  931. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  932. .enable_mask = WM8350_DC3_ENA,
  933. .owner = THIS_MODULE,
  934. },
  935. {
  936. .name = "DCDC4",
  937. .id = WM8350_DCDC_4,
  938. .ops = &wm8350_dcdc_ops,
  939. .irq = WM8350_IRQ_UV_DC4,
  940. .type = REGULATOR_VOLTAGE,
  941. .n_voltages = WM8350_DCDC_MAX_VSEL + 1,
  942. .min_uV = 850000,
  943. .uV_step = 25000,
  944. .vsel_reg = WM8350_DCDC4_CONTROL,
  945. .vsel_mask = WM8350_DC4_VSEL_MASK,
  946. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  947. .enable_mask = WM8350_DC4_ENA,
  948. .owner = THIS_MODULE,
  949. },
  950. {
  951. .name = "DCDC5",
  952. .id = WM8350_DCDC_5,
  953. .ops = &wm8350_dcdc2_5_ops,
  954. .irq = WM8350_IRQ_UV_DC5,
  955. .type = REGULATOR_VOLTAGE,
  956. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  957. .enable_mask = WM8350_DC5_ENA,
  958. .owner = THIS_MODULE,
  959. },
  960. {
  961. .name = "DCDC6",
  962. .id = WM8350_DCDC_6,
  963. .ops = &wm8350_dcdc_ops,
  964. .irq = WM8350_IRQ_UV_DC6,
  965. .type = REGULATOR_VOLTAGE,
  966. .n_voltages = WM8350_DCDC_MAX_VSEL + 1,
  967. .min_uV = 850000,
  968. .uV_step = 25000,
  969. .vsel_reg = WM8350_DCDC6_CONTROL,
  970. .vsel_mask = WM8350_DC6_VSEL_MASK,
  971. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  972. .enable_mask = WM8350_DC6_ENA,
  973. .owner = THIS_MODULE,
  974. },
  975. {
  976. .name = "LDO1",
  977. .id = WM8350_LDO_1,
  978. .ops = &wm8350_ldo_ops,
  979. .irq = WM8350_IRQ_UV_LDO1,
  980. .type = REGULATOR_VOLTAGE,
  981. .n_voltages = WM8350_LDO1_VSEL_MASK + 1,
  982. .linear_ranges = wm8350_ldo_ranges,
  983. .n_linear_ranges = ARRAY_SIZE(wm8350_ldo_ranges),
  984. .vsel_reg = WM8350_LDO1_CONTROL,
  985. .vsel_mask = WM8350_LDO1_VSEL_MASK,
  986. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  987. .enable_mask = WM8350_LDO1_ENA,
  988. .owner = THIS_MODULE,
  989. },
  990. {
  991. .name = "LDO2",
  992. .id = WM8350_LDO_2,
  993. .ops = &wm8350_ldo_ops,
  994. .irq = WM8350_IRQ_UV_LDO2,
  995. .type = REGULATOR_VOLTAGE,
  996. .n_voltages = WM8350_LDO2_VSEL_MASK + 1,
  997. .linear_ranges = wm8350_ldo_ranges,
  998. .n_linear_ranges = ARRAY_SIZE(wm8350_ldo_ranges),
  999. .vsel_reg = WM8350_LDO2_CONTROL,
  1000. .vsel_mask = WM8350_LDO2_VSEL_MASK,
  1001. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  1002. .enable_mask = WM8350_LDO2_ENA,
  1003. .owner = THIS_MODULE,
  1004. },
  1005. {
  1006. .name = "LDO3",
  1007. .id = WM8350_LDO_3,
  1008. .ops = &wm8350_ldo_ops,
  1009. .irq = WM8350_IRQ_UV_LDO3,
  1010. .type = REGULATOR_VOLTAGE,
  1011. .n_voltages = WM8350_LDO3_VSEL_MASK + 1,
  1012. .linear_ranges = wm8350_ldo_ranges,
  1013. .n_linear_ranges = ARRAY_SIZE(wm8350_ldo_ranges),
  1014. .vsel_reg = WM8350_LDO3_CONTROL,
  1015. .vsel_mask = WM8350_LDO3_VSEL_MASK,
  1016. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  1017. .enable_mask = WM8350_LDO3_ENA,
  1018. .owner = THIS_MODULE,
  1019. },
  1020. {
  1021. .name = "LDO4",
  1022. .id = WM8350_LDO_4,
  1023. .ops = &wm8350_ldo_ops,
  1024. .irq = WM8350_IRQ_UV_LDO4,
  1025. .type = REGULATOR_VOLTAGE,
  1026. .n_voltages = WM8350_LDO4_VSEL_MASK + 1,
  1027. .linear_ranges = wm8350_ldo_ranges,
  1028. .n_linear_ranges = ARRAY_SIZE(wm8350_ldo_ranges),
  1029. .vsel_reg = WM8350_LDO4_CONTROL,
  1030. .vsel_mask = WM8350_LDO4_VSEL_MASK,
  1031. .enable_reg = WM8350_DCDC_LDO_REQUESTED,
  1032. .enable_mask = WM8350_LDO4_ENA,
  1033. .owner = THIS_MODULE,
  1034. },
  1035. {
  1036. .name = "ISINKA",
  1037. .id = WM8350_ISINK_A,
  1038. .ops = &wm8350_isink_ops,
  1039. .irq = WM8350_IRQ_CS1,
  1040. .type = REGULATOR_CURRENT,
  1041. .owner = THIS_MODULE,
  1042. },
  1043. {
  1044. .name = "ISINKB",
  1045. .id = WM8350_ISINK_B,
  1046. .ops = &wm8350_isink_ops,
  1047. .irq = WM8350_IRQ_CS2,
  1048. .type = REGULATOR_CURRENT,
  1049. .owner = THIS_MODULE,
  1050. },
  1051. };
  1052. static irqreturn_t pmic_uv_handler(int irq, void *data)
  1053. {
  1054. struct regulator_dev *rdev = (struct regulator_dev *)data;
  1055. mutex_lock(&rdev->mutex);
  1056. if (irq == WM8350_IRQ_CS1 || irq == WM8350_IRQ_CS2)
  1057. regulator_notifier_call_chain(rdev,
  1058. REGULATOR_EVENT_REGULATION_OUT,
  1059. NULL);
  1060. else
  1061. regulator_notifier_call_chain(rdev,
  1062. REGULATOR_EVENT_UNDER_VOLTAGE,
  1063. NULL);
  1064. mutex_unlock(&rdev->mutex);
  1065. return IRQ_HANDLED;
  1066. }
  1067. static int wm8350_regulator_probe(struct platform_device *pdev)
  1068. {
  1069. struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev);
  1070. struct regulator_config config = { };
  1071. struct regulator_dev *rdev;
  1072. int ret;
  1073. u16 val;
  1074. if (pdev->id < WM8350_DCDC_1 || pdev->id > WM8350_ISINK_B)
  1075. return -ENODEV;
  1076. /* do any regulatior specific init */
  1077. switch (pdev->id) {
  1078. case WM8350_DCDC_1:
  1079. val = wm8350_reg_read(wm8350, WM8350_DCDC1_LOW_POWER);
  1080. wm8350->pmic.dcdc1_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  1081. break;
  1082. case WM8350_DCDC_3:
  1083. val = wm8350_reg_read(wm8350, WM8350_DCDC3_LOW_POWER);
  1084. wm8350->pmic.dcdc3_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  1085. break;
  1086. case WM8350_DCDC_4:
  1087. val = wm8350_reg_read(wm8350, WM8350_DCDC4_LOW_POWER);
  1088. wm8350->pmic.dcdc4_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  1089. break;
  1090. case WM8350_DCDC_6:
  1091. val = wm8350_reg_read(wm8350, WM8350_DCDC6_LOW_POWER);
  1092. wm8350->pmic.dcdc6_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
  1093. break;
  1094. }
  1095. config.dev = &pdev->dev;
  1096. config.init_data = dev_get_platdata(&pdev->dev);
  1097. config.driver_data = dev_get_drvdata(&pdev->dev);
  1098. config.regmap = wm8350->regmap;
  1099. /* register regulator */
  1100. rdev = devm_regulator_register(&pdev->dev, &wm8350_reg[pdev->id],
  1101. &config);
  1102. if (IS_ERR(rdev)) {
  1103. dev_err(&pdev->dev, "failed to register %s\n",
  1104. wm8350_reg[pdev->id].name);
  1105. return PTR_ERR(rdev);
  1106. }
  1107. /* register regulator IRQ */
  1108. ret = wm8350_register_irq(wm8350, wm8350_reg[pdev->id].irq,
  1109. pmic_uv_handler, 0, "UV", rdev);
  1110. if (ret < 0) {
  1111. dev_err(&pdev->dev, "failed to register regulator %s IRQ\n",
  1112. wm8350_reg[pdev->id].name);
  1113. return ret;
  1114. }
  1115. return 0;
  1116. }
  1117. static int wm8350_regulator_remove(struct platform_device *pdev)
  1118. {
  1119. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  1120. struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
  1121. wm8350_free_irq(wm8350, wm8350_reg[pdev->id].irq, rdev);
  1122. return 0;
  1123. }
  1124. int wm8350_register_regulator(struct wm8350 *wm8350, int reg,
  1125. struct regulator_init_data *initdata)
  1126. {
  1127. struct platform_device *pdev;
  1128. int ret;
  1129. if (reg < 0 || reg >= NUM_WM8350_REGULATORS)
  1130. return -EINVAL;
  1131. if (wm8350->pmic.pdev[reg])
  1132. return -EBUSY;
  1133. if (reg >= WM8350_DCDC_1 && reg <= WM8350_DCDC_6 &&
  1134. reg > wm8350->pmic.max_dcdc)
  1135. return -ENODEV;
  1136. if (reg >= WM8350_ISINK_A && reg <= WM8350_ISINK_B &&
  1137. reg > wm8350->pmic.max_isink)
  1138. return -ENODEV;
  1139. pdev = platform_device_alloc("wm8350-regulator", reg);
  1140. if (!pdev)
  1141. return -ENOMEM;
  1142. wm8350->pmic.pdev[reg] = pdev;
  1143. initdata->driver_data = wm8350;
  1144. pdev->dev.platform_data = initdata;
  1145. pdev->dev.parent = wm8350->dev;
  1146. platform_set_drvdata(pdev, wm8350);
  1147. ret = platform_device_add(pdev);
  1148. if (ret != 0) {
  1149. dev_err(wm8350->dev, "Failed to register regulator %d: %d\n",
  1150. reg, ret);
  1151. platform_device_put(pdev);
  1152. wm8350->pmic.pdev[reg] = NULL;
  1153. }
  1154. return ret;
  1155. }
  1156. EXPORT_SYMBOL_GPL(wm8350_register_regulator);
  1157. /**
  1158. * wm8350_register_led - Register a WM8350 LED output
  1159. *
  1160. * @param wm8350 The WM8350 device to configure.
  1161. * @param lednum LED device index to create.
  1162. * @param dcdc The DCDC to use for the LED.
  1163. * @param isink The ISINK to use for the LED.
  1164. * @param pdata Configuration for the LED.
  1165. *
  1166. * The WM8350 supports the use of an ISINK together with a DCDC to
  1167. * provide a power-efficient LED driver. This function registers the
  1168. * regulators and instantiates the platform device for a LED. The
  1169. * operating modes for the LED regulators must be configured using
  1170. * wm8350_isink_set_flash(), wm8350_dcdc25_set_mode() and
  1171. * wm8350_dcdc_set_slot() prior to calling this function.
  1172. */
  1173. int wm8350_register_led(struct wm8350 *wm8350, int lednum, int dcdc, int isink,
  1174. struct wm8350_led_platform_data *pdata)
  1175. {
  1176. struct wm8350_led *led;
  1177. struct platform_device *pdev;
  1178. int ret;
  1179. if (lednum >= ARRAY_SIZE(wm8350->pmic.led) || lednum < 0) {
  1180. dev_err(wm8350->dev, "Invalid LED index %d\n", lednum);
  1181. return -ENODEV;
  1182. }
  1183. led = &wm8350->pmic.led[lednum];
  1184. if (led->pdev) {
  1185. dev_err(wm8350->dev, "LED %d already allocated\n", lednum);
  1186. return -EINVAL;
  1187. }
  1188. pdev = platform_device_alloc("wm8350-led", lednum);
  1189. if (pdev == NULL) {
  1190. dev_err(wm8350->dev, "Failed to allocate LED %d\n", lednum);
  1191. return -ENOMEM;
  1192. }
  1193. led->isink_consumer.dev_name = dev_name(&pdev->dev);
  1194. led->isink_consumer.supply = "led_isink";
  1195. led->isink_init.num_consumer_supplies = 1;
  1196. led->isink_init.consumer_supplies = &led->isink_consumer;
  1197. led->isink_init.constraints.min_uA = 0;
  1198. led->isink_init.constraints.max_uA = pdata->max_uA;
  1199. led->isink_init.constraints.valid_ops_mask
  1200. = REGULATOR_CHANGE_CURRENT | REGULATOR_CHANGE_STATUS;
  1201. led->isink_init.constraints.valid_modes_mask = REGULATOR_MODE_NORMAL;
  1202. ret = wm8350_register_regulator(wm8350, isink, &led->isink_init);
  1203. if (ret != 0) {
  1204. platform_device_put(pdev);
  1205. return ret;
  1206. }
  1207. led->dcdc_consumer.dev_name = dev_name(&pdev->dev);
  1208. led->dcdc_consumer.supply = "led_vcc";
  1209. led->dcdc_init.num_consumer_supplies = 1;
  1210. led->dcdc_init.consumer_supplies = &led->dcdc_consumer;
  1211. led->dcdc_init.constraints.valid_modes_mask = REGULATOR_MODE_NORMAL;
  1212. led->dcdc_init.constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS;
  1213. ret = wm8350_register_regulator(wm8350, dcdc, &led->dcdc_init);
  1214. if (ret != 0) {
  1215. platform_device_put(pdev);
  1216. return ret;
  1217. }
  1218. switch (isink) {
  1219. case WM8350_ISINK_A:
  1220. wm8350->pmic.isink_A_dcdc = dcdc;
  1221. break;
  1222. case WM8350_ISINK_B:
  1223. wm8350->pmic.isink_B_dcdc = dcdc;
  1224. break;
  1225. }
  1226. pdev->dev.platform_data = pdata;
  1227. pdev->dev.parent = wm8350->dev;
  1228. ret = platform_device_add(pdev);
  1229. if (ret != 0) {
  1230. dev_err(wm8350->dev, "Failed to register LED %d: %d\n",
  1231. lednum, ret);
  1232. platform_device_put(pdev);
  1233. return ret;
  1234. }
  1235. led->pdev = pdev;
  1236. return 0;
  1237. }
  1238. EXPORT_SYMBOL_GPL(wm8350_register_led);
  1239. static struct platform_driver wm8350_regulator_driver = {
  1240. .probe = wm8350_regulator_probe,
  1241. .remove = wm8350_regulator_remove,
  1242. .driver = {
  1243. .name = "wm8350-regulator",
  1244. },
  1245. };
  1246. static int __init wm8350_regulator_init(void)
  1247. {
  1248. return platform_driver_register(&wm8350_regulator_driver);
  1249. }
  1250. subsys_initcall(wm8350_regulator_init);
  1251. static void __exit wm8350_regulator_exit(void)
  1252. {
  1253. platform_driver_unregister(&wm8350_regulator_driver);
  1254. }
  1255. module_exit(wm8350_regulator_exit);
  1256. /* Module information */
  1257. MODULE_AUTHOR("Liam Girdwood");
  1258. MODULE_DESCRIPTION("WM8350 voltage and current regulator driver");
  1259. MODULE_LICENSE("GPL");
  1260. MODULE_ALIAS("platform:wm8350-regulator");