wm831x-core.c 31 KB

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