wm8350-regulator.c 33 KB

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