wm831x-core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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 + 1] = {
  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 = 0x8310,
  90. WM8311 = 0x8311,
  91. WM8312 = 0x8312,
  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. /* Some engineering samples do not have the ID set, rely on
  1184. * the device being registered correctly.
  1185. */
  1186. if (ret == 0) {
  1187. dev_info(wm831x->dev, "Device is an engineering sample\n");
  1188. ret = id;
  1189. }
  1190. switch (ret) {
  1191. case WM8310:
  1192. parent = WM8310;
  1193. wm831x->num_gpio = 16;
  1194. dev_info(wm831x->dev, "WM8310 revision %c\n", 'A' + rev);
  1195. break;
  1196. case WM8311:
  1197. parent = WM8311;
  1198. wm831x->num_gpio = 16;
  1199. dev_info(wm831x->dev, "WM8311 revision %c\n", 'A' + rev);
  1200. break;
  1201. case WM8312:
  1202. parent = WM8312;
  1203. wm831x->num_gpio = 16;
  1204. dev_info(wm831x->dev, "WM8312 revision %c\n", 'A' + rev);
  1205. break;
  1206. default:
  1207. dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
  1208. ret = -EINVAL;
  1209. goto err;
  1210. }
  1211. /* This will need revisiting in future but is OK for all
  1212. * current parts.
  1213. */
  1214. if (parent != id)
  1215. dev_warn(wm831x->dev, "Device was registered as a WM%lx\n",
  1216. id);
  1217. /* Bootstrap the user key */
  1218. ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
  1219. if (ret < 0) {
  1220. dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
  1221. goto err;
  1222. }
  1223. if (ret != 0) {
  1224. dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
  1225. ret);
  1226. wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
  1227. }
  1228. wm831x->locked = 1;
  1229. if (pdata && pdata->pre_init) {
  1230. ret = pdata->pre_init(wm831x);
  1231. if (ret != 0) {
  1232. dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
  1233. goto err;
  1234. }
  1235. }
  1236. ret = wm831x_irq_init(wm831x, irq);
  1237. if (ret != 0)
  1238. goto err;
  1239. /* The core device is up, instantiate the subdevices. */
  1240. switch (parent) {
  1241. case WM8310:
  1242. ret = mfd_add_devices(wm831x->dev, -1,
  1243. wm8310_devs, ARRAY_SIZE(wm8310_devs),
  1244. NULL, 0);
  1245. break;
  1246. case WM8311:
  1247. ret = mfd_add_devices(wm831x->dev, -1,
  1248. wm8311_devs, ARRAY_SIZE(wm8311_devs),
  1249. NULL, 0);
  1250. break;
  1251. case WM8312:
  1252. ret = mfd_add_devices(wm831x->dev, -1,
  1253. wm8312_devs, ARRAY_SIZE(wm8312_devs),
  1254. NULL, 0);
  1255. break;
  1256. default:
  1257. /* If this happens the bus probe function is buggy */
  1258. BUG();
  1259. }
  1260. if (ret != 0) {
  1261. dev_err(wm831x->dev, "Failed to add children\n");
  1262. goto err_irq;
  1263. }
  1264. if (pdata && pdata->backlight) {
  1265. /* Treat errors as non-critical */
  1266. ret = mfd_add_devices(wm831x->dev, -1, backlight_devs,
  1267. ARRAY_SIZE(backlight_devs), NULL, 0);
  1268. if (ret < 0)
  1269. dev_err(wm831x->dev, "Failed to add backlight: %d\n",
  1270. ret);
  1271. }
  1272. wm831x_otp_init(wm831x);
  1273. if (pdata && pdata->post_init) {
  1274. ret = pdata->post_init(wm831x);
  1275. if (ret != 0) {
  1276. dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
  1277. goto err_irq;
  1278. }
  1279. }
  1280. return 0;
  1281. err_irq:
  1282. wm831x_irq_exit(wm831x);
  1283. err:
  1284. mfd_remove_devices(wm831x->dev);
  1285. kfree(wm831x);
  1286. return ret;
  1287. }
  1288. static void wm831x_device_exit(struct wm831x *wm831x)
  1289. {
  1290. wm831x_otp_exit(wm831x);
  1291. mfd_remove_devices(wm831x->dev);
  1292. wm831x_irq_exit(wm831x);
  1293. kfree(wm831x);
  1294. }
  1295. static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
  1296. int bytes, void *dest)
  1297. {
  1298. struct i2c_client *i2c = wm831x->control_data;
  1299. int ret;
  1300. u16 r = cpu_to_be16(reg);
  1301. ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
  1302. if (ret < 0)
  1303. return ret;
  1304. if (ret != 2)
  1305. return -EIO;
  1306. ret = i2c_master_recv(i2c, dest, bytes);
  1307. if (ret < 0)
  1308. return ret;
  1309. if (ret != bytes)
  1310. return -EIO;
  1311. return 0;
  1312. }
  1313. /* Currently we allocate the write buffer on the stack; this is OK for
  1314. * small writes - if we need to do large writes this will need to be
  1315. * revised.
  1316. */
  1317. static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
  1318. int bytes, void *src)
  1319. {
  1320. struct i2c_client *i2c = wm831x->control_data;
  1321. unsigned char msg[bytes + 2];
  1322. int ret;
  1323. reg = cpu_to_be16(reg);
  1324. memcpy(&msg[0], &reg, 2);
  1325. memcpy(&msg[2], src, bytes);
  1326. ret = i2c_master_send(i2c, msg, bytes + 2);
  1327. if (ret < 0)
  1328. return ret;
  1329. if (ret < bytes + 2)
  1330. return -EIO;
  1331. return 0;
  1332. }
  1333. static int wm831x_i2c_probe(struct i2c_client *i2c,
  1334. const struct i2c_device_id *id)
  1335. {
  1336. struct wm831x *wm831x;
  1337. wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
  1338. if (wm831x == NULL) {
  1339. kfree(i2c);
  1340. return -ENOMEM;
  1341. }
  1342. i2c_set_clientdata(i2c, wm831x);
  1343. wm831x->dev = &i2c->dev;
  1344. wm831x->control_data = i2c;
  1345. wm831x->read_dev = wm831x_i2c_read_device;
  1346. wm831x->write_dev = wm831x_i2c_write_device;
  1347. return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
  1348. }
  1349. static int wm831x_i2c_remove(struct i2c_client *i2c)
  1350. {
  1351. struct wm831x *wm831x = i2c_get_clientdata(i2c);
  1352. wm831x_device_exit(wm831x);
  1353. return 0;
  1354. }
  1355. static const struct i2c_device_id wm831x_i2c_id[] = {
  1356. { "wm8310", WM8310 },
  1357. { "wm8311", WM8311 },
  1358. { "wm8312", WM8312 },
  1359. { }
  1360. };
  1361. MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
  1362. static struct i2c_driver wm831x_i2c_driver = {
  1363. .driver = {
  1364. .name = "wm831x",
  1365. .owner = THIS_MODULE,
  1366. },
  1367. .probe = wm831x_i2c_probe,
  1368. .remove = wm831x_i2c_remove,
  1369. .id_table = wm831x_i2c_id,
  1370. };
  1371. static int __init wm831x_i2c_init(void)
  1372. {
  1373. int ret;
  1374. ret = i2c_add_driver(&wm831x_i2c_driver);
  1375. if (ret != 0)
  1376. pr_err("Failed to register wm831x I2C driver: %d\n", ret);
  1377. return ret;
  1378. }
  1379. subsys_initcall(wm831x_i2c_init);
  1380. static void __exit wm831x_i2c_exit(void)
  1381. {
  1382. i2c_del_driver(&wm831x_i2c_driver);
  1383. }
  1384. module_exit(wm831x_i2c_exit);
  1385. MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
  1386. MODULE_LICENSE("GPL");
  1387. MODULE_AUTHOR("Mark Brown");