wm831x-core.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /*
  2. * wm831x-core.c -- Device access for Wolfson WM831x PMICs
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/bcd.h>
  18. #include <linux/delay.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/mfd/wm831x/core.h>
  21. #include <linux/mfd/wm831x/pdata.h>
  22. #include <linux/mfd/wm831x/irq.h>
  23. #include <linux/mfd/wm831x/auxadc.h>
  24. enum wm831x_parent {
  25. WM8310 = 0,
  26. WM8311 = 1,
  27. WM8312 = 2,
  28. };
  29. static int wm831x_reg_locked(struct wm831x *wm831x, unsigned short reg)
  30. {
  31. if (!wm831x->locked)
  32. return 0;
  33. switch (reg) {
  34. case WM831X_WATCHDOG:
  35. case WM831X_DC4_CONTROL:
  36. case WM831X_ON_PIN_CONTROL:
  37. case WM831X_BACKUP_CHARGER_CONTROL:
  38. case WM831X_CHARGER_CONTROL_1:
  39. case WM831X_CHARGER_CONTROL_2:
  40. return 1;
  41. default:
  42. return 0;
  43. }
  44. }
  45. /**
  46. * wm831x_reg_unlock: Unlock user keyed registers
  47. *
  48. * The WM831x has a user key preventing writes to particularly
  49. * critical registers. This function locks those registers,
  50. * allowing writes to them.
  51. */
  52. void wm831x_reg_lock(struct wm831x *wm831x)
  53. {
  54. int ret;
  55. ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
  56. if (ret == 0) {
  57. dev_vdbg(wm831x->dev, "Registers locked\n");
  58. mutex_lock(&wm831x->io_lock);
  59. WARN_ON(wm831x->locked);
  60. wm831x->locked = 1;
  61. mutex_unlock(&wm831x->io_lock);
  62. } else {
  63. dev_err(wm831x->dev, "Failed to lock registers: %d\n", ret);
  64. }
  65. }
  66. EXPORT_SYMBOL_GPL(wm831x_reg_lock);
  67. /**
  68. * wm831x_reg_unlock: Unlock user keyed registers
  69. *
  70. * The WM831x has a user key preventing writes to particularly
  71. * critical registers. This function locks those registers,
  72. * preventing spurious writes.
  73. */
  74. int wm831x_reg_unlock(struct wm831x *wm831x)
  75. {
  76. int ret;
  77. /* 0x9716 is the value required to unlock the registers */
  78. ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716);
  79. if (ret == 0) {
  80. dev_vdbg(wm831x->dev, "Registers unlocked\n");
  81. mutex_lock(&wm831x->io_lock);
  82. WARN_ON(!wm831x->locked);
  83. wm831x->locked = 0;
  84. mutex_unlock(&wm831x->io_lock);
  85. }
  86. return ret;
  87. }
  88. EXPORT_SYMBOL_GPL(wm831x_reg_unlock);
  89. static int wm831x_read(struct wm831x *wm831x, unsigned short reg,
  90. int bytes, void *dest)
  91. {
  92. int ret, i;
  93. u16 *buf = dest;
  94. BUG_ON(bytes % 2);
  95. BUG_ON(bytes <= 0);
  96. ret = wm831x->read_dev(wm831x, reg, bytes, dest);
  97. if (ret < 0)
  98. return ret;
  99. for (i = 0; i < bytes / 2; i++) {
  100. buf[i] = be16_to_cpu(buf[i]);
  101. dev_vdbg(wm831x->dev, "Read %04x from R%d(0x%x)\n",
  102. buf[i], reg + i, reg + i);
  103. }
  104. return 0;
  105. }
  106. /**
  107. * wm831x_reg_read: Read a single WM831x register.
  108. *
  109. * @wm831x: Device to read from.
  110. * @reg: Register to read.
  111. */
  112. int wm831x_reg_read(struct wm831x *wm831x, unsigned short reg)
  113. {
  114. unsigned short val;
  115. int ret;
  116. mutex_lock(&wm831x->io_lock);
  117. ret = wm831x_read(wm831x, reg, 2, &val);
  118. mutex_unlock(&wm831x->io_lock);
  119. if (ret < 0)
  120. return ret;
  121. else
  122. return val;
  123. }
  124. EXPORT_SYMBOL_GPL(wm831x_reg_read);
  125. /**
  126. * wm831x_bulk_read: Read multiple WM831x registers
  127. *
  128. * @wm831x: Device to read from
  129. * @reg: First register
  130. * @count: Number of registers
  131. * @buf: Buffer to fill.
  132. */
  133. int wm831x_bulk_read(struct wm831x *wm831x, unsigned short reg,
  134. int count, u16 *buf)
  135. {
  136. int ret;
  137. mutex_lock(&wm831x->io_lock);
  138. ret = wm831x_read(wm831x, reg, count * 2, buf);
  139. mutex_unlock(&wm831x->io_lock);
  140. return ret;
  141. }
  142. EXPORT_SYMBOL_GPL(wm831x_bulk_read);
  143. static int wm831x_write(struct wm831x *wm831x, unsigned short reg,
  144. int bytes, void *src)
  145. {
  146. u16 *buf = src;
  147. int i;
  148. BUG_ON(bytes % 2);
  149. BUG_ON(bytes <= 0);
  150. for (i = 0; i < bytes / 2; i++) {
  151. if (wm831x_reg_locked(wm831x, reg))
  152. return -EPERM;
  153. dev_vdbg(wm831x->dev, "Write %04x to R%d(0x%x)\n",
  154. buf[i], reg + i, reg + i);
  155. buf[i] = cpu_to_be16(buf[i]);
  156. }
  157. return wm831x->write_dev(wm831x, reg, bytes, src);
  158. }
  159. /**
  160. * wm831x_reg_write: Write a single WM831x register.
  161. *
  162. * @wm831x: Device to write to.
  163. * @reg: Register to write to.
  164. * @val: Value to write.
  165. */
  166. int wm831x_reg_write(struct wm831x *wm831x, unsigned short reg,
  167. unsigned short val)
  168. {
  169. int ret;
  170. mutex_lock(&wm831x->io_lock);
  171. ret = wm831x_write(wm831x, reg, 2, &val);
  172. mutex_unlock(&wm831x->io_lock);
  173. return ret;
  174. }
  175. EXPORT_SYMBOL_GPL(wm831x_reg_write);
  176. /**
  177. * wm831x_set_bits: Set the value of a bitfield in a WM831x register
  178. *
  179. * @wm831x: Device to write to.
  180. * @reg: Register to write to.
  181. * @mask: Mask of bits to set.
  182. * @val: Value to set (unshifted)
  183. */
  184. int wm831x_set_bits(struct wm831x *wm831x, unsigned short reg,
  185. unsigned short mask, unsigned short val)
  186. {
  187. int ret;
  188. u16 r;
  189. mutex_lock(&wm831x->io_lock);
  190. ret = wm831x_read(wm831x, reg, 2, &r);
  191. if (ret < 0)
  192. goto out;
  193. r &= ~mask;
  194. r |= val;
  195. ret = wm831x_write(wm831x, reg, 2, &r);
  196. out:
  197. mutex_unlock(&wm831x->io_lock);
  198. return ret;
  199. }
  200. EXPORT_SYMBOL_GPL(wm831x_set_bits);
  201. /**
  202. * wm831x_auxadc_read: Read a value from the WM831x AUXADC
  203. *
  204. * @wm831x: Device to read from.
  205. * @input: AUXADC input to read.
  206. */
  207. int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input)
  208. {
  209. int tries = 10;
  210. int ret, src;
  211. mutex_lock(&wm831x->auxadc_lock);
  212. ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
  213. WM831X_AUX_ENA, WM831X_AUX_ENA);
  214. if (ret < 0) {
  215. dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n", ret);
  216. goto out;
  217. }
  218. /* We force a single source at present */
  219. src = input;
  220. ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE,
  221. 1 << src);
  222. if (ret < 0) {
  223. dev_err(wm831x->dev, "Failed to set AUXADC source: %d\n", ret);
  224. goto out;
  225. }
  226. ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
  227. WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA);
  228. if (ret < 0) {
  229. dev_err(wm831x->dev, "Failed to start AUXADC: %d\n", ret);
  230. goto disable;
  231. }
  232. do {
  233. msleep(1);
  234. ret = wm831x_reg_read(wm831x, WM831X_AUXADC_CONTROL);
  235. if (ret < 0)
  236. ret = WM831X_AUX_CVT_ENA;
  237. } while ((ret & WM831X_AUX_CVT_ENA) && --tries);
  238. if (ret & WM831X_AUX_CVT_ENA) {
  239. dev_err(wm831x->dev, "Timed out reading AUXADC\n");
  240. ret = -EBUSY;
  241. goto disable;
  242. }
  243. ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
  244. if (ret < 0) {
  245. dev_err(wm831x->dev, "Failed to read AUXADC data: %d\n", ret);
  246. } else {
  247. src = ((ret & WM831X_AUX_DATA_SRC_MASK)
  248. >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
  249. if (src == 14)
  250. src = WM831X_AUX_CAL;
  251. if (src != input) {
  252. dev_err(wm831x->dev, "Data from source %d not %d\n",
  253. src, input);
  254. ret = -EINVAL;
  255. } else {
  256. ret &= WM831X_AUX_DATA_MASK;
  257. }
  258. }
  259. disable:
  260. wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
  261. out:
  262. mutex_unlock(&wm831x->auxadc_lock);
  263. return ret;
  264. }
  265. EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
  266. /**
  267. * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
  268. *
  269. * @wm831x: Device to read from.
  270. * @input: AUXADC input to read.
  271. */
  272. int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
  273. {
  274. int ret;
  275. ret = wm831x_auxadc_read(wm831x, input);
  276. if (ret < 0)
  277. return ret;
  278. ret *= 1465;
  279. return ret;
  280. }
  281. EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
  282. static struct resource wm831x_dcdc1_resources[] = {
  283. {
  284. .start = WM831X_DC1_CONTROL_1,
  285. .end = WM831X_DC1_DVS_CONTROL,
  286. .flags = IORESOURCE_IO,
  287. },
  288. {
  289. .name = "UV",
  290. .start = WM831X_IRQ_UV_DC1,
  291. .end = WM831X_IRQ_UV_DC1,
  292. .flags = IORESOURCE_IRQ,
  293. },
  294. {
  295. .name = "HC",
  296. .start = WM831X_IRQ_HC_DC1,
  297. .end = WM831X_IRQ_HC_DC1,
  298. .flags = IORESOURCE_IRQ,
  299. },
  300. };
  301. static struct resource wm831x_dcdc2_resources[] = {
  302. {
  303. .start = WM831X_DC2_CONTROL_1,
  304. .end = WM831X_DC2_DVS_CONTROL,
  305. .flags = IORESOURCE_IO,
  306. },
  307. {
  308. .name = "UV",
  309. .start = WM831X_IRQ_UV_DC2,
  310. .end = WM831X_IRQ_UV_DC2,
  311. .flags = IORESOURCE_IRQ,
  312. },
  313. {
  314. .name = "HC",
  315. .start = WM831X_IRQ_HC_DC2,
  316. .end = WM831X_IRQ_HC_DC2,
  317. .flags = IORESOURCE_IRQ,
  318. },
  319. };
  320. static struct resource wm831x_dcdc3_resources[] = {
  321. {
  322. .start = WM831X_DC3_CONTROL_1,
  323. .end = WM831X_DC3_SLEEP_CONTROL,
  324. .flags = IORESOURCE_IO,
  325. },
  326. {
  327. .name = "UV",
  328. .start = WM831X_IRQ_UV_DC3,
  329. .end = WM831X_IRQ_UV_DC3,
  330. .flags = IORESOURCE_IRQ,
  331. },
  332. };
  333. static struct resource wm831x_dcdc4_resources[] = {
  334. {
  335. .start = WM831X_DC4_CONTROL,
  336. .end = WM831X_DC4_SLEEP_CONTROL,
  337. .flags = IORESOURCE_IO,
  338. },
  339. {
  340. .name = "UV",
  341. .start = WM831X_IRQ_UV_DC4,
  342. .end = WM831X_IRQ_UV_DC4,
  343. .flags = IORESOURCE_IRQ,
  344. },
  345. };
  346. static struct resource wm831x_gpio_resources[] = {
  347. {
  348. .start = WM831X_IRQ_GPIO_1,
  349. .end = WM831X_IRQ_GPIO_16,
  350. .flags = IORESOURCE_IRQ,
  351. },
  352. };
  353. static struct resource wm831x_isink1_resources[] = {
  354. {
  355. .start = WM831X_CURRENT_SINK_1,
  356. .end = WM831X_CURRENT_SINK_1,
  357. .flags = IORESOURCE_IO,
  358. },
  359. {
  360. .start = WM831X_IRQ_CS1,
  361. .end = WM831X_IRQ_CS1,
  362. .flags = IORESOURCE_IRQ,
  363. },
  364. };
  365. static struct resource wm831x_isink2_resources[] = {
  366. {
  367. .start = WM831X_CURRENT_SINK_2,
  368. .end = WM831X_CURRENT_SINK_2,
  369. .flags = IORESOURCE_IO,
  370. },
  371. {
  372. .start = WM831X_IRQ_CS2,
  373. .end = WM831X_IRQ_CS2,
  374. .flags = IORESOURCE_IRQ,
  375. },
  376. };
  377. static struct resource wm831x_ldo1_resources[] = {
  378. {
  379. .start = WM831X_LDO1_CONTROL,
  380. .end = WM831X_LDO1_SLEEP_CONTROL,
  381. .flags = IORESOURCE_IO,
  382. },
  383. {
  384. .name = "UV",
  385. .start = WM831X_IRQ_UV_LDO1,
  386. .end = WM831X_IRQ_UV_LDO1,
  387. .flags = IORESOURCE_IRQ,
  388. },
  389. };
  390. static struct resource wm831x_ldo2_resources[] = {
  391. {
  392. .start = WM831X_LDO2_CONTROL,
  393. .end = WM831X_LDO2_SLEEP_CONTROL,
  394. .flags = IORESOURCE_IO,
  395. },
  396. {
  397. .name = "UV",
  398. .start = WM831X_IRQ_UV_LDO2,
  399. .end = WM831X_IRQ_UV_LDO2,
  400. .flags = IORESOURCE_IRQ,
  401. },
  402. };
  403. static struct resource wm831x_ldo3_resources[] = {
  404. {
  405. .start = WM831X_LDO3_CONTROL,
  406. .end = WM831X_LDO3_SLEEP_CONTROL,
  407. .flags = IORESOURCE_IO,
  408. },
  409. {
  410. .name = "UV",
  411. .start = WM831X_IRQ_UV_LDO3,
  412. .end = WM831X_IRQ_UV_LDO3,
  413. .flags = IORESOURCE_IRQ,
  414. },
  415. };
  416. static struct resource wm831x_ldo4_resources[] = {
  417. {
  418. .start = WM831X_LDO4_CONTROL,
  419. .end = WM831X_LDO4_SLEEP_CONTROL,
  420. .flags = IORESOURCE_IO,
  421. },
  422. {
  423. .name = "UV",
  424. .start = WM831X_IRQ_UV_LDO4,
  425. .end = WM831X_IRQ_UV_LDO4,
  426. .flags = IORESOURCE_IRQ,
  427. },
  428. };
  429. static struct resource wm831x_ldo5_resources[] = {
  430. {
  431. .start = WM831X_LDO5_CONTROL,
  432. .end = WM831X_LDO5_SLEEP_CONTROL,
  433. .flags = IORESOURCE_IO,
  434. },
  435. {
  436. .name = "UV",
  437. .start = WM831X_IRQ_UV_LDO5,
  438. .end = WM831X_IRQ_UV_LDO5,
  439. .flags = IORESOURCE_IRQ,
  440. },
  441. };
  442. static struct resource wm831x_ldo6_resources[] = {
  443. {
  444. .start = WM831X_LDO6_CONTROL,
  445. .end = WM831X_LDO6_SLEEP_CONTROL,
  446. .flags = IORESOURCE_IO,
  447. },
  448. {
  449. .name = "UV",
  450. .start = WM831X_IRQ_UV_LDO6,
  451. .end = WM831X_IRQ_UV_LDO6,
  452. .flags = IORESOURCE_IRQ,
  453. },
  454. };
  455. static struct resource wm831x_ldo7_resources[] = {
  456. {
  457. .start = WM831X_LDO7_CONTROL,
  458. .end = WM831X_LDO7_SLEEP_CONTROL,
  459. .flags = IORESOURCE_IO,
  460. },
  461. {
  462. .name = "UV",
  463. .start = WM831X_IRQ_UV_LDO7,
  464. .end = WM831X_IRQ_UV_LDO7,
  465. .flags = IORESOURCE_IRQ,
  466. },
  467. };
  468. static struct resource wm831x_ldo8_resources[] = {
  469. {
  470. .start = WM831X_LDO8_CONTROL,
  471. .end = WM831X_LDO8_SLEEP_CONTROL,
  472. .flags = IORESOURCE_IO,
  473. },
  474. {
  475. .name = "UV",
  476. .start = WM831X_IRQ_UV_LDO8,
  477. .end = WM831X_IRQ_UV_LDO8,
  478. .flags = IORESOURCE_IRQ,
  479. },
  480. };
  481. static struct resource wm831x_ldo9_resources[] = {
  482. {
  483. .start = WM831X_LDO9_CONTROL,
  484. .end = WM831X_LDO9_SLEEP_CONTROL,
  485. .flags = IORESOURCE_IO,
  486. },
  487. {
  488. .name = "UV",
  489. .start = WM831X_IRQ_UV_LDO9,
  490. .end = WM831X_IRQ_UV_LDO9,
  491. .flags = IORESOURCE_IRQ,
  492. },
  493. };
  494. static struct resource wm831x_ldo10_resources[] = {
  495. {
  496. .start = WM831X_LDO10_CONTROL,
  497. .end = WM831X_LDO10_SLEEP_CONTROL,
  498. .flags = IORESOURCE_IO,
  499. },
  500. {
  501. .name = "UV",
  502. .start = WM831X_IRQ_UV_LDO10,
  503. .end = WM831X_IRQ_UV_LDO10,
  504. .flags = IORESOURCE_IRQ,
  505. },
  506. };
  507. static struct resource wm831x_ldo11_resources[] = {
  508. {
  509. .start = WM831X_LDO11_ON_CONTROL,
  510. .end = WM831X_LDO11_SLEEP_CONTROL,
  511. .flags = IORESOURCE_IO,
  512. },
  513. };
  514. static struct resource wm831x_on_resources[] = {
  515. {
  516. .start = WM831X_IRQ_ON,
  517. .end = WM831X_IRQ_ON,
  518. .flags = IORESOURCE_IRQ,
  519. },
  520. };
  521. static struct resource wm831x_power_resources[] = {
  522. {
  523. .name = "SYSLO",
  524. .start = WM831X_IRQ_PPM_SYSLO,
  525. .end = WM831X_IRQ_PPM_SYSLO,
  526. .flags = IORESOURCE_IRQ,
  527. },
  528. {
  529. .name = "PWR SRC",
  530. .start = WM831X_IRQ_PPM_PWR_SRC,
  531. .end = WM831X_IRQ_PPM_PWR_SRC,
  532. .flags = IORESOURCE_IRQ,
  533. },
  534. {
  535. .name = "USB CURR",
  536. .start = WM831X_IRQ_PPM_USB_CURR,
  537. .end = WM831X_IRQ_PPM_USB_CURR,
  538. .flags = IORESOURCE_IRQ,
  539. },
  540. {
  541. .name = "BATT HOT",
  542. .start = WM831X_IRQ_CHG_BATT_HOT,
  543. .end = WM831X_IRQ_CHG_BATT_HOT,
  544. .flags = IORESOURCE_IRQ,
  545. },
  546. {
  547. .name = "BATT COLD",
  548. .start = WM831X_IRQ_CHG_BATT_COLD,
  549. .end = WM831X_IRQ_CHG_BATT_COLD,
  550. .flags = IORESOURCE_IRQ,
  551. },
  552. {
  553. .name = "BATT FAIL",
  554. .start = WM831X_IRQ_CHG_BATT_FAIL,
  555. .end = WM831X_IRQ_CHG_BATT_FAIL,
  556. .flags = IORESOURCE_IRQ,
  557. },
  558. {
  559. .name = "OV",
  560. .start = WM831X_IRQ_CHG_OV,
  561. .end = WM831X_IRQ_CHG_OV,
  562. .flags = IORESOURCE_IRQ,
  563. },
  564. {
  565. .name = "END",
  566. .start = WM831X_IRQ_CHG_END,
  567. .end = WM831X_IRQ_CHG_END,
  568. .flags = IORESOURCE_IRQ,
  569. },
  570. {
  571. .name = "TO",
  572. .start = WM831X_IRQ_CHG_TO,
  573. .end = WM831X_IRQ_CHG_TO,
  574. .flags = IORESOURCE_IRQ,
  575. },
  576. {
  577. .name = "MODE",
  578. .start = WM831X_IRQ_CHG_MODE,
  579. .end = WM831X_IRQ_CHG_MODE,
  580. .flags = IORESOURCE_IRQ,
  581. },
  582. {
  583. .name = "START",
  584. .start = WM831X_IRQ_CHG_START,
  585. .end = WM831X_IRQ_CHG_START,
  586. .flags = IORESOURCE_IRQ,
  587. },
  588. };
  589. static struct resource wm831x_rtc_resources[] = {
  590. {
  591. .name = "PER",
  592. .start = WM831X_IRQ_RTC_PER,
  593. .end = WM831X_IRQ_RTC_PER,
  594. .flags = IORESOURCE_IRQ,
  595. },
  596. {
  597. .name = "ALM",
  598. .start = WM831X_IRQ_RTC_ALM,
  599. .end = WM831X_IRQ_RTC_ALM,
  600. .flags = IORESOURCE_IRQ,
  601. },
  602. };
  603. static struct resource wm831x_status1_resources[] = {
  604. {
  605. .start = WM831X_STATUS_LED_1,
  606. .end = WM831X_STATUS_LED_1,
  607. .flags = IORESOURCE_IO,
  608. },
  609. };
  610. static struct resource wm831x_status2_resources[] = {
  611. {
  612. .start = WM831X_STATUS_LED_2,
  613. .end = WM831X_STATUS_LED_2,
  614. .flags = IORESOURCE_IO,
  615. },
  616. };
  617. static struct resource wm831x_touch_resources[] = {
  618. {
  619. .name = "TCHPD",
  620. .start = WM831X_IRQ_TCHPD,
  621. .end = WM831X_IRQ_TCHPD,
  622. .flags = IORESOURCE_IRQ,
  623. },
  624. {
  625. .name = "TCHDATA",
  626. .start = WM831X_IRQ_TCHDATA,
  627. .end = WM831X_IRQ_TCHDATA,
  628. .flags = IORESOURCE_IRQ,
  629. },
  630. };
  631. static struct resource wm831x_wdt_resources[] = {
  632. {
  633. .start = WM831X_IRQ_WDOG_TO,
  634. .end = WM831X_IRQ_WDOG_TO,
  635. .flags = IORESOURCE_IRQ,
  636. },
  637. };
  638. static struct mfd_cell wm8310_devs[] = {
  639. {
  640. .name = "wm831x-buckv",
  641. .id = 1,
  642. .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
  643. .resources = wm831x_dcdc1_resources,
  644. },
  645. {
  646. .name = "wm831x-buckv",
  647. .id = 2,
  648. .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
  649. .resources = wm831x_dcdc2_resources,
  650. },
  651. {
  652. .name = "wm831x-buckp",
  653. .id = 3,
  654. .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
  655. .resources = wm831x_dcdc3_resources,
  656. },
  657. {
  658. .name = "wm831x-boostp",
  659. .id = 4,
  660. .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
  661. .resources = wm831x_dcdc4_resources,
  662. },
  663. {
  664. .name = "wm831x-epe",
  665. .id = 1,
  666. },
  667. {
  668. .name = "wm831x-epe",
  669. .id = 2,
  670. },
  671. {
  672. .name = "wm831x-gpio",
  673. .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
  674. .resources = wm831x_gpio_resources,
  675. },
  676. {
  677. .name = "wm831x-hwmon",
  678. },
  679. {
  680. .name = "wm831x-isink",
  681. .id = 1,
  682. .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
  683. .resources = wm831x_isink1_resources,
  684. },
  685. {
  686. .name = "wm831x-isink",
  687. .id = 2,
  688. .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
  689. .resources = wm831x_isink2_resources,
  690. },
  691. {
  692. .name = "wm831x-ldo",
  693. .id = 1,
  694. .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
  695. .resources = wm831x_ldo1_resources,
  696. },
  697. {
  698. .name = "wm831x-ldo",
  699. .id = 2,
  700. .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
  701. .resources = wm831x_ldo2_resources,
  702. },
  703. {
  704. .name = "wm831x-ldo",
  705. .id = 3,
  706. .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
  707. .resources = wm831x_ldo3_resources,
  708. },
  709. {
  710. .name = "wm831x-ldo",
  711. .id = 4,
  712. .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
  713. .resources = wm831x_ldo4_resources,
  714. },
  715. {
  716. .name = "wm831x-ldo",
  717. .id = 5,
  718. .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
  719. .resources = wm831x_ldo5_resources,
  720. },
  721. {
  722. .name = "wm831x-ldo",
  723. .id = 6,
  724. .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
  725. .resources = wm831x_ldo6_resources,
  726. },
  727. {
  728. .name = "wm831x-aldo",
  729. .id = 7,
  730. .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
  731. .resources = wm831x_ldo7_resources,
  732. },
  733. {
  734. .name = "wm831x-aldo",
  735. .id = 8,
  736. .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
  737. .resources = wm831x_ldo8_resources,
  738. },
  739. {
  740. .name = "wm831x-aldo",
  741. .id = 9,
  742. .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
  743. .resources = wm831x_ldo9_resources,
  744. },
  745. {
  746. .name = "wm831x-aldo",
  747. .id = 10,
  748. .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
  749. .resources = wm831x_ldo10_resources,
  750. },
  751. {
  752. .name = "wm831x-alive-ldo",
  753. .id = 11,
  754. .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
  755. .resources = wm831x_ldo11_resources,
  756. },
  757. {
  758. .name = "wm831x-on",
  759. .num_resources = ARRAY_SIZE(wm831x_on_resources),
  760. .resources = wm831x_on_resources,
  761. },
  762. {
  763. .name = "wm831x-power",
  764. .num_resources = ARRAY_SIZE(wm831x_power_resources),
  765. .resources = wm831x_power_resources,
  766. },
  767. {
  768. .name = "wm831x-rtc",
  769. .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
  770. .resources = wm831x_rtc_resources,
  771. },
  772. {
  773. .name = "wm831x-status",
  774. .id = 1,
  775. .num_resources = ARRAY_SIZE(wm831x_status1_resources),
  776. .resources = wm831x_status1_resources,
  777. },
  778. {
  779. .name = "wm831x-status",
  780. .id = 2,
  781. .num_resources = ARRAY_SIZE(wm831x_status2_resources),
  782. .resources = wm831x_status2_resources,
  783. },
  784. {
  785. .name = "wm831x-watchdog",
  786. .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
  787. .resources = wm831x_wdt_resources,
  788. },
  789. };
  790. static struct mfd_cell wm8311_devs[] = {
  791. {
  792. .name = "wm831x-buckv",
  793. .id = 1,
  794. .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
  795. .resources = wm831x_dcdc1_resources,
  796. },
  797. {
  798. .name = "wm831x-buckv",
  799. .id = 2,
  800. .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
  801. .resources = wm831x_dcdc2_resources,
  802. },
  803. {
  804. .name = "wm831x-buckp",
  805. .id = 3,
  806. .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
  807. .resources = wm831x_dcdc3_resources,
  808. },
  809. {
  810. .name = "wm831x-boostp",
  811. .id = 4,
  812. .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
  813. .resources = wm831x_dcdc4_resources,
  814. },
  815. {
  816. .name = "wm831x-epe",
  817. .id = 1,
  818. },
  819. {
  820. .name = "wm831x-epe",
  821. .id = 2,
  822. },
  823. {
  824. .name = "wm831x-gpio",
  825. .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
  826. .resources = wm831x_gpio_resources,
  827. },
  828. {
  829. .name = "wm831x-hwmon",
  830. },
  831. {
  832. .name = "wm831x-isink",
  833. .id = 1,
  834. .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
  835. .resources = wm831x_isink1_resources,
  836. },
  837. {
  838. .name = "wm831x-isink",
  839. .id = 2,
  840. .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
  841. .resources = wm831x_isink2_resources,
  842. },
  843. {
  844. .name = "wm831x-ldo",
  845. .id = 1,
  846. .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
  847. .resources = wm831x_ldo1_resources,
  848. },
  849. {
  850. .name = "wm831x-ldo",
  851. .id = 2,
  852. .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
  853. .resources = wm831x_ldo2_resources,
  854. },
  855. {
  856. .name = "wm831x-ldo",
  857. .id = 3,
  858. .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
  859. .resources = wm831x_ldo3_resources,
  860. },
  861. {
  862. .name = "wm831x-ldo",
  863. .id = 4,
  864. .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
  865. .resources = wm831x_ldo4_resources,
  866. },
  867. {
  868. .name = "wm831x-ldo",
  869. .id = 5,
  870. .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
  871. .resources = wm831x_ldo5_resources,
  872. },
  873. {
  874. .name = "wm831x-aldo",
  875. .id = 7,
  876. .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
  877. .resources = wm831x_ldo7_resources,
  878. },
  879. {
  880. .name = "wm831x-alive-ldo",
  881. .id = 11,
  882. .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
  883. .resources = wm831x_ldo11_resources,
  884. },
  885. {
  886. .name = "wm831x-on",
  887. .num_resources = ARRAY_SIZE(wm831x_on_resources),
  888. .resources = wm831x_on_resources,
  889. },
  890. {
  891. .name = "wm831x-power",
  892. .num_resources = ARRAY_SIZE(wm831x_power_resources),
  893. .resources = wm831x_power_resources,
  894. },
  895. {
  896. .name = "wm831x-rtc",
  897. .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
  898. .resources = wm831x_rtc_resources,
  899. },
  900. {
  901. .name = "wm831x-status",
  902. .id = 1,
  903. .num_resources = ARRAY_SIZE(wm831x_status1_resources),
  904. .resources = wm831x_status1_resources,
  905. },
  906. {
  907. .name = "wm831x-status",
  908. .id = 2,
  909. .num_resources = ARRAY_SIZE(wm831x_status2_resources),
  910. .resources = wm831x_status2_resources,
  911. },
  912. {
  913. .name = "wm831x-touch",
  914. .num_resources = ARRAY_SIZE(wm831x_touch_resources),
  915. .resources = wm831x_touch_resources,
  916. },
  917. {
  918. .name = "wm831x-watchdog",
  919. .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
  920. .resources = wm831x_wdt_resources,
  921. },
  922. };
  923. static struct mfd_cell wm8312_devs[] = {
  924. {
  925. .name = "wm831x-buckv",
  926. .id = 1,
  927. .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
  928. .resources = wm831x_dcdc1_resources,
  929. },
  930. {
  931. .name = "wm831x-buckv",
  932. .id = 2,
  933. .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
  934. .resources = wm831x_dcdc2_resources,
  935. },
  936. {
  937. .name = "wm831x-buckp",
  938. .id = 3,
  939. .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
  940. .resources = wm831x_dcdc3_resources,
  941. },
  942. {
  943. .name = "wm831x-boostp",
  944. .id = 4,
  945. .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
  946. .resources = wm831x_dcdc4_resources,
  947. },
  948. {
  949. .name = "wm831x-epe",
  950. .id = 1,
  951. },
  952. {
  953. .name = "wm831x-epe",
  954. .id = 2,
  955. },
  956. {
  957. .name = "wm831x-gpio",
  958. .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
  959. .resources = wm831x_gpio_resources,
  960. },
  961. {
  962. .name = "wm831x-hwmon",
  963. },
  964. {
  965. .name = "wm831x-isink",
  966. .id = 1,
  967. .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
  968. .resources = wm831x_isink1_resources,
  969. },
  970. {
  971. .name = "wm831x-isink",
  972. .id = 2,
  973. .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
  974. .resources = wm831x_isink2_resources,
  975. },
  976. {
  977. .name = "wm831x-ldo",
  978. .id = 1,
  979. .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
  980. .resources = wm831x_ldo1_resources,
  981. },
  982. {
  983. .name = "wm831x-ldo",
  984. .id = 2,
  985. .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
  986. .resources = wm831x_ldo2_resources,
  987. },
  988. {
  989. .name = "wm831x-ldo",
  990. .id = 3,
  991. .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
  992. .resources = wm831x_ldo3_resources,
  993. },
  994. {
  995. .name = "wm831x-ldo",
  996. .id = 4,
  997. .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
  998. .resources = wm831x_ldo4_resources,
  999. },
  1000. {
  1001. .name = "wm831x-ldo",
  1002. .id = 5,
  1003. .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
  1004. .resources = wm831x_ldo5_resources,
  1005. },
  1006. {
  1007. .name = "wm831x-ldo",
  1008. .id = 6,
  1009. .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
  1010. .resources = wm831x_ldo6_resources,
  1011. },
  1012. {
  1013. .name = "wm831x-aldo",
  1014. .id = 7,
  1015. .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
  1016. .resources = wm831x_ldo7_resources,
  1017. },
  1018. {
  1019. .name = "wm831x-aldo",
  1020. .id = 8,
  1021. .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
  1022. .resources = wm831x_ldo8_resources,
  1023. },
  1024. {
  1025. .name = "wm831x-aldo",
  1026. .id = 9,
  1027. .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
  1028. .resources = wm831x_ldo9_resources,
  1029. },
  1030. {
  1031. .name = "wm831x-aldo",
  1032. .id = 10,
  1033. .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
  1034. .resources = wm831x_ldo10_resources,
  1035. },
  1036. {
  1037. .name = "wm831x-alive-ldo",
  1038. .id = 11,
  1039. .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
  1040. .resources = wm831x_ldo11_resources,
  1041. },
  1042. {
  1043. .name = "wm831x-on",
  1044. .num_resources = ARRAY_SIZE(wm831x_on_resources),
  1045. .resources = wm831x_on_resources,
  1046. },
  1047. {
  1048. .name = "wm831x-power",
  1049. .num_resources = ARRAY_SIZE(wm831x_power_resources),
  1050. .resources = wm831x_power_resources,
  1051. },
  1052. {
  1053. .name = "wm831x-rtc",
  1054. .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
  1055. .resources = wm831x_rtc_resources,
  1056. },
  1057. {
  1058. .name = "wm831x-status",
  1059. .id = 1,
  1060. .num_resources = ARRAY_SIZE(wm831x_status1_resources),
  1061. .resources = wm831x_status1_resources,
  1062. },
  1063. {
  1064. .name = "wm831x-status",
  1065. .id = 2,
  1066. .num_resources = ARRAY_SIZE(wm831x_status2_resources),
  1067. .resources = wm831x_status2_resources,
  1068. },
  1069. {
  1070. .name = "wm831x-touch",
  1071. .num_resources = ARRAY_SIZE(wm831x_touch_resources),
  1072. .resources = wm831x_touch_resources,
  1073. },
  1074. {
  1075. .name = "wm831x-watchdog",
  1076. .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
  1077. .resources = wm831x_wdt_resources,
  1078. },
  1079. };
  1080. /*
  1081. * Instantiate the generic non-control parts of the device.
  1082. */
  1083. static int wm831x_device_init(struct wm831x *wm831x, unsigned long id, int irq)
  1084. {
  1085. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  1086. int rev;
  1087. enum wm831x_parent parent;
  1088. int ret;
  1089. mutex_init(&wm831x->io_lock);
  1090. mutex_init(&wm831x->key_lock);
  1091. mutex_init(&wm831x->auxadc_lock);
  1092. dev_set_drvdata(wm831x->dev, wm831x);
  1093. ret = wm831x_reg_read(wm831x, WM831X_PARENT_ID);
  1094. if (ret < 0) {
  1095. dev_err(wm831x->dev, "Failed to read parent ID: %d\n", ret);
  1096. goto err;
  1097. }
  1098. if (ret != 0x6204) {
  1099. dev_err(wm831x->dev, "Device is not a WM831x: ID %x\n", ret);
  1100. ret = -EINVAL;
  1101. goto err;
  1102. }
  1103. ret = wm831x_reg_read(wm831x, WM831X_REVISION);
  1104. if (ret < 0) {
  1105. dev_err(wm831x->dev, "Failed to read revision: %d\n", ret);
  1106. goto err;
  1107. }
  1108. rev = (ret & WM831X_PARENT_REV_MASK) >> WM831X_PARENT_REV_SHIFT;
  1109. ret = wm831x_reg_read(wm831x, WM831X_RESET_ID);
  1110. if (ret < 0) {
  1111. dev_err(wm831x->dev, "Failed to read device ID: %d\n", ret);
  1112. goto err;
  1113. }
  1114. switch (ret) {
  1115. case 0x8310:
  1116. parent = WM8310;
  1117. switch (rev) {
  1118. case 0:
  1119. dev_info(wm831x->dev, "WM8310 revision %c\n",
  1120. 'A' + rev);
  1121. break;
  1122. }
  1123. break;
  1124. case 0x8311:
  1125. parent = WM8311;
  1126. switch (rev) {
  1127. case 0:
  1128. dev_info(wm831x->dev, "WM8311 revision %c\n",
  1129. 'A' + rev);
  1130. break;
  1131. }
  1132. break;
  1133. case 0x8312:
  1134. parent = WM8312;
  1135. switch (rev) {
  1136. case 0:
  1137. dev_info(wm831x->dev, "WM8312 revision %c\n",
  1138. 'A' + rev);
  1139. break;
  1140. }
  1141. break;
  1142. case 0:
  1143. /* Some engineering samples do not have the ID set,
  1144. * rely on the device being registered correctly.
  1145. * This will need revisiting for future devices with
  1146. * multiple dies.
  1147. */
  1148. parent = id;
  1149. switch (rev) {
  1150. case 0:
  1151. dev_info(wm831x->dev, "WM831%d ES revision %c\n",
  1152. parent, 'A' + rev);
  1153. break;
  1154. }
  1155. break;
  1156. default:
  1157. dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
  1158. ret = -EINVAL;
  1159. goto err;
  1160. }
  1161. /* This will need revisiting in future but is OK for all
  1162. * current parts.
  1163. */
  1164. if (parent != id)
  1165. dev_warn(wm831x->dev, "Device was registered as a WM831%lu\n",
  1166. id);
  1167. /* Bootstrap the user key */
  1168. ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
  1169. if (ret < 0) {
  1170. dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
  1171. goto err;
  1172. }
  1173. if (ret != 0) {
  1174. dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
  1175. ret);
  1176. wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
  1177. }
  1178. wm831x->locked = 1;
  1179. if (pdata && pdata->pre_init) {
  1180. ret = pdata->pre_init(wm831x);
  1181. if (ret != 0) {
  1182. dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
  1183. goto err;
  1184. }
  1185. }
  1186. ret = wm831x_irq_init(wm831x, irq);
  1187. if (ret != 0)
  1188. goto err;
  1189. /* The core device is up, instantiate the subdevices. */
  1190. switch (parent) {
  1191. case WM8310:
  1192. ret = mfd_add_devices(wm831x->dev, -1,
  1193. wm8310_devs, ARRAY_SIZE(wm8310_devs),
  1194. NULL, 0);
  1195. break;
  1196. case WM8311:
  1197. ret = mfd_add_devices(wm831x->dev, -1,
  1198. wm8311_devs, ARRAY_SIZE(wm8311_devs),
  1199. NULL, 0);
  1200. break;
  1201. case WM8312:
  1202. ret = mfd_add_devices(wm831x->dev, -1,
  1203. wm8312_devs, ARRAY_SIZE(wm8312_devs),
  1204. NULL, 0);
  1205. break;
  1206. default:
  1207. /* If this happens the bus probe function is buggy */
  1208. BUG();
  1209. }
  1210. if (ret != 0) {
  1211. dev_err(wm831x->dev, "Failed to add children\n");
  1212. goto err_irq;
  1213. }
  1214. if (pdata && pdata->post_init) {
  1215. ret = pdata->post_init(wm831x);
  1216. if (ret != 0) {
  1217. dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
  1218. goto err_irq;
  1219. }
  1220. }
  1221. return 0;
  1222. err_irq:
  1223. wm831x_irq_exit(wm831x);
  1224. err:
  1225. mfd_remove_devices(wm831x->dev);
  1226. kfree(wm831x);
  1227. return ret;
  1228. }
  1229. static void wm831x_device_exit(struct wm831x *wm831x)
  1230. {
  1231. mfd_remove_devices(wm831x->dev);
  1232. wm831x_irq_exit(wm831x);
  1233. kfree(wm831x);
  1234. }
  1235. static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
  1236. int bytes, void *dest)
  1237. {
  1238. struct i2c_client *i2c = wm831x->control_data;
  1239. int ret;
  1240. u16 r = cpu_to_be16(reg);
  1241. ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
  1242. if (ret < 0)
  1243. return ret;
  1244. if (ret != 2)
  1245. return -EIO;
  1246. ret = i2c_master_recv(i2c, dest, bytes);
  1247. if (ret < 0)
  1248. return ret;
  1249. if (ret != bytes)
  1250. return -EIO;
  1251. return 0;
  1252. }
  1253. /* Currently we allocate the write buffer on the stack; this is OK for
  1254. * small writes - if we need to do large writes this will need to be
  1255. * revised.
  1256. */
  1257. static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
  1258. int bytes, void *src)
  1259. {
  1260. struct i2c_client *i2c = wm831x->control_data;
  1261. unsigned char msg[bytes + 2];
  1262. int ret;
  1263. reg = cpu_to_be16(reg);
  1264. memcpy(&msg[0], &reg, 2);
  1265. memcpy(&msg[2], src, bytes);
  1266. ret = i2c_master_send(i2c, msg, bytes + 2);
  1267. if (ret < 0)
  1268. return ret;
  1269. if (ret < bytes + 2)
  1270. return -EIO;
  1271. return 0;
  1272. }
  1273. static int wm831x_i2c_probe(struct i2c_client *i2c,
  1274. const struct i2c_device_id *id)
  1275. {
  1276. struct wm831x *wm831x;
  1277. wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
  1278. if (wm831x == NULL) {
  1279. kfree(i2c);
  1280. return -ENOMEM;
  1281. }
  1282. i2c_set_clientdata(i2c, wm831x);
  1283. wm831x->dev = &i2c->dev;
  1284. wm831x->control_data = i2c;
  1285. wm831x->read_dev = wm831x_i2c_read_device;
  1286. wm831x->write_dev = wm831x_i2c_write_device;
  1287. return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
  1288. }
  1289. static int wm831x_i2c_remove(struct i2c_client *i2c)
  1290. {
  1291. struct wm831x *wm831x = i2c_get_clientdata(i2c);
  1292. wm831x_device_exit(wm831x);
  1293. return 0;
  1294. }
  1295. static const struct i2c_device_id wm831x_i2c_id[] = {
  1296. { "wm8310", WM8310 },
  1297. { "wm8311", WM8311 },
  1298. { "wm8312", WM8312 },
  1299. { }
  1300. };
  1301. MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
  1302. static struct i2c_driver wm831x_i2c_driver = {
  1303. .driver = {
  1304. .name = "wm831x",
  1305. .owner = THIS_MODULE,
  1306. },
  1307. .probe = wm831x_i2c_probe,
  1308. .remove = wm831x_i2c_remove,
  1309. .id_table = wm831x_i2c_id,
  1310. };
  1311. static int __init wm831x_i2c_init(void)
  1312. {
  1313. int ret;
  1314. ret = i2c_add_driver(&wm831x_i2c_driver);
  1315. if (ret != 0)
  1316. pr_err("Failed to register wm831x I2C driver: %d\n", ret);
  1317. return ret;
  1318. }
  1319. subsys_initcall(wm831x_i2c_init);
  1320. static void __exit wm831x_i2c_exit(void)
  1321. {
  1322. i2c_del_driver(&wm831x_i2c_driver);
  1323. }
  1324. module_exit(wm831x_i2c_exit);
  1325. MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
  1326. MODULE_LICENSE("GPL");
  1327. MODULE_AUTHOR("Mark Brown");