wm8350-core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * wm8350-core.c -- Device access for Wolfson WM8350
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood, Mark Brown
  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/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/bug.h>
  19. #include <linux/device.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/mfd/wm8350/core.h>
  24. #include <linux/mfd/wm8350/audio.h>
  25. #include <linux/mfd/wm8350/comparator.h>
  26. #include <linux/mfd/wm8350/gpio.h>
  27. #include <linux/mfd/wm8350/pmic.h>
  28. #include <linux/mfd/wm8350/rtc.h>
  29. #include <linux/mfd/wm8350/supply.h>
  30. #include <linux/mfd/wm8350/wdt.h>
  31. #define WM8350_UNLOCK_KEY 0x0013
  32. #define WM8350_LOCK_KEY 0x0000
  33. #define WM8350_CLOCK_CONTROL_1 0x28
  34. #define WM8350_AIF_TEST 0x74
  35. /* debug */
  36. #define WM8350_BUS_DEBUG 0
  37. #if WM8350_BUS_DEBUG
  38. #define dump(regs, src) do { \
  39. int i_; \
  40. u16 *src_ = src; \
  41. printk(KERN_DEBUG); \
  42. for (i_ = 0; i_ < regs; i_++) \
  43. printk(" 0x%4.4x", *src_++); \
  44. printk("\n"); \
  45. } while (0);
  46. #else
  47. #define dump(bytes, src)
  48. #endif
  49. #define WM8350_LOCK_DEBUG 0
  50. #if WM8350_LOCK_DEBUG
  51. #define ldbg(format, arg...) printk(format, ## arg)
  52. #else
  53. #define ldbg(format, arg...)
  54. #endif
  55. /*
  56. * WM8350 Device IO
  57. */
  58. static DEFINE_MUTEX(io_mutex);
  59. static DEFINE_MUTEX(reg_lock_mutex);
  60. /* Perform a physical read from the device.
  61. */
  62. static int wm8350_phys_read(struct wm8350 *wm8350, u8 reg, int num_regs,
  63. u16 *dest)
  64. {
  65. int i, ret;
  66. int bytes = num_regs * 2;
  67. dev_dbg(wm8350->dev, "volatile read\n");
  68. ret = wm8350->read_dev(wm8350, reg, bytes, (char *)dest);
  69. for (i = reg; i < reg + num_regs; i++) {
  70. /* Cache is CPU endian */
  71. dest[i - reg] = be16_to_cpu(dest[i - reg]);
  72. /* Mask out non-readable bits */
  73. dest[i - reg] &= wm8350_reg_io_map[i].readable;
  74. }
  75. dump(num_regs, dest);
  76. return ret;
  77. }
  78. static int wm8350_read(struct wm8350 *wm8350, u8 reg, int num_regs, u16 *dest)
  79. {
  80. int i;
  81. int end = reg + num_regs;
  82. int ret = 0;
  83. int bytes = num_regs * 2;
  84. if (wm8350->read_dev == NULL)
  85. return -ENODEV;
  86. if ((reg + num_regs - 1) > WM8350_MAX_REGISTER) {
  87. dev_err(wm8350->dev, "invalid reg %x\n",
  88. reg + num_regs - 1);
  89. return -EINVAL;
  90. }
  91. dev_dbg(wm8350->dev,
  92. "%s R%d(0x%2.2x) %d regs\n", __func__, reg, reg, num_regs);
  93. #if WM8350_BUS_DEBUG
  94. /* we can _safely_ read any register, but warn if read not supported */
  95. for (i = reg; i < end; i++) {
  96. if (!wm8350_reg_io_map[i].readable)
  97. dev_warn(wm8350->dev,
  98. "reg R%d is not readable\n", i);
  99. }
  100. #endif
  101. /* if any volatile registers are required, then read back all */
  102. for (i = reg; i < end; i++)
  103. if (wm8350_reg_io_map[i].vol)
  104. return wm8350_phys_read(wm8350, reg, num_regs, dest);
  105. /* no volatiles, then cache is good */
  106. dev_dbg(wm8350->dev, "cache read\n");
  107. memcpy(dest, &wm8350->reg_cache[reg], bytes);
  108. dump(num_regs, dest);
  109. return ret;
  110. }
  111. static inline int is_reg_locked(struct wm8350 *wm8350, u8 reg)
  112. {
  113. if (reg == WM8350_SECURITY ||
  114. wm8350->reg_cache[WM8350_SECURITY] == WM8350_UNLOCK_KEY)
  115. return 0;
  116. if ((reg >= WM8350_GPIO_FUNCTION_SELECT_1 &&
  117. reg <= WM8350_GPIO_FUNCTION_SELECT_4) ||
  118. (reg >= WM8350_BATTERY_CHARGER_CONTROL_1 &&
  119. reg <= WM8350_BATTERY_CHARGER_CONTROL_3))
  120. return 1;
  121. return 0;
  122. }
  123. static int wm8350_write(struct wm8350 *wm8350, u8 reg, int num_regs, u16 *src)
  124. {
  125. int i;
  126. int end = reg + num_regs;
  127. int bytes = num_regs * 2;
  128. if (wm8350->write_dev == NULL)
  129. return -ENODEV;
  130. if ((reg + num_regs - 1) > WM8350_MAX_REGISTER) {
  131. dev_err(wm8350->dev, "invalid reg %x\n",
  132. reg + num_regs - 1);
  133. return -EINVAL;
  134. }
  135. /* it's generally not a good idea to write to RO or locked registers */
  136. for (i = reg; i < end; i++) {
  137. if (!wm8350_reg_io_map[i].writable) {
  138. dev_err(wm8350->dev,
  139. "attempted write to read only reg R%d\n", i);
  140. return -EINVAL;
  141. }
  142. if (is_reg_locked(wm8350, i)) {
  143. dev_err(wm8350->dev,
  144. "attempted write to locked reg R%d\n", i);
  145. return -EINVAL;
  146. }
  147. src[i - reg] &= wm8350_reg_io_map[i].writable;
  148. wm8350->reg_cache[i] =
  149. (wm8350->reg_cache[i] & ~wm8350_reg_io_map[i].writable)
  150. | src[i - reg];
  151. src[i - reg] = cpu_to_be16(src[i - reg]);
  152. }
  153. /* Actually write it out */
  154. return wm8350->write_dev(wm8350, reg, bytes, (char *)src);
  155. }
  156. /*
  157. * Safe read, modify, write methods
  158. */
  159. int wm8350_clear_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
  160. {
  161. u16 data;
  162. int err;
  163. mutex_lock(&io_mutex);
  164. err = wm8350_read(wm8350, reg, 1, &data);
  165. if (err) {
  166. dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
  167. goto out;
  168. }
  169. data &= ~mask;
  170. err = wm8350_write(wm8350, reg, 1, &data);
  171. if (err)
  172. dev_err(wm8350->dev, "write to reg R%d failed\n", reg);
  173. out:
  174. mutex_unlock(&io_mutex);
  175. return err;
  176. }
  177. EXPORT_SYMBOL_GPL(wm8350_clear_bits);
  178. int wm8350_set_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
  179. {
  180. u16 data;
  181. int err;
  182. mutex_lock(&io_mutex);
  183. err = wm8350_read(wm8350, reg, 1, &data);
  184. if (err) {
  185. dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
  186. goto out;
  187. }
  188. data |= mask;
  189. err = wm8350_write(wm8350, reg, 1, &data);
  190. if (err)
  191. dev_err(wm8350->dev, "write to reg R%d failed\n", reg);
  192. out:
  193. mutex_unlock(&io_mutex);
  194. return err;
  195. }
  196. EXPORT_SYMBOL_GPL(wm8350_set_bits);
  197. u16 wm8350_reg_read(struct wm8350 *wm8350, int reg)
  198. {
  199. u16 data;
  200. int err;
  201. mutex_lock(&io_mutex);
  202. err = wm8350_read(wm8350, reg, 1, &data);
  203. if (err)
  204. dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
  205. mutex_unlock(&io_mutex);
  206. return data;
  207. }
  208. EXPORT_SYMBOL_GPL(wm8350_reg_read);
  209. int wm8350_reg_write(struct wm8350 *wm8350, int reg, u16 val)
  210. {
  211. int ret;
  212. u16 data = val;
  213. mutex_lock(&io_mutex);
  214. ret = wm8350_write(wm8350, reg, 1, &data);
  215. if (ret)
  216. dev_err(wm8350->dev, "write to reg R%d failed\n", reg);
  217. mutex_unlock(&io_mutex);
  218. return ret;
  219. }
  220. EXPORT_SYMBOL_GPL(wm8350_reg_write);
  221. int wm8350_block_read(struct wm8350 *wm8350, int start_reg, int regs,
  222. u16 *dest)
  223. {
  224. int err = 0;
  225. mutex_lock(&io_mutex);
  226. err = wm8350_read(wm8350, start_reg, regs, dest);
  227. if (err)
  228. dev_err(wm8350->dev, "block read starting from R%d failed\n",
  229. start_reg);
  230. mutex_unlock(&io_mutex);
  231. return err;
  232. }
  233. EXPORT_SYMBOL_GPL(wm8350_block_read);
  234. int wm8350_block_write(struct wm8350 *wm8350, int start_reg, int regs,
  235. u16 *src)
  236. {
  237. int ret = 0;
  238. mutex_lock(&io_mutex);
  239. ret = wm8350_write(wm8350, start_reg, regs, src);
  240. if (ret)
  241. dev_err(wm8350->dev, "block write starting at R%d failed\n",
  242. start_reg);
  243. mutex_unlock(&io_mutex);
  244. return ret;
  245. }
  246. EXPORT_SYMBOL_GPL(wm8350_block_write);
  247. /**
  248. * wm8350_reg_lock()
  249. *
  250. * The WM8350 has a hardware lock which can be used to prevent writes to
  251. * some registers (generally those which can cause particularly serious
  252. * problems if misused). This function enables that lock.
  253. */
  254. int wm8350_reg_lock(struct wm8350 *wm8350)
  255. {
  256. u16 key = WM8350_LOCK_KEY;
  257. int ret;
  258. ldbg(__func__);
  259. mutex_lock(&io_mutex);
  260. ret = wm8350_write(wm8350, WM8350_SECURITY, 1, &key);
  261. if (ret)
  262. dev_err(wm8350->dev, "lock failed\n");
  263. mutex_unlock(&io_mutex);
  264. return ret;
  265. }
  266. EXPORT_SYMBOL_GPL(wm8350_reg_lock);
  267. /**
  268. * wm8350_reg_unlock()
  269. *
  270. * The WM8350 has a hardware lock which can be used to prevent writes to
  271. * some registers (generally those which can cause particularly serious
  272. * problems if misused). This function disables that lock so updates
  273. * can be performed. For maximum safety this should be done only when
  274. * required.
  275. */
  276. int wm8350_reg_unlock(struct wm8350 *wm8350)
  277. {
  278. u16 key = WM8350_UNLOCK_KEY;
  279. int ret;
  280. ldbg(__func__);
  281. mutex_lock(&io_mutex);
  282. ret = wm8350_write(wm8350, WM8350_SECURITY, 1, &key);
  283. if (ret)
  284. dev_err(wm8350->dev, "unlock failed\n");
  285. mutex_unlock(&io_mutex);
  286. return ret;
  287. }
  288. EXPORT_SYMBOL_GPL(wm8350_reg_unlock);
  289. int wm8350_read_auxadc(struct wm8350 *wm8350, int channel, int scale, int vref)
  290. {
  291. u16 reg, result = 0;
  292. if (channel < WM8350_AUXADC_AUX1 || channel > WM8350_AUXADC_TEMP)
  293. return -EINVAL;
  294. if (channel >= WM8350_AUXADC_USB && channel <= WM8350_AUXADC_TEMP
  295. && (scale != 0 || vref != 0))
  296. return -EINVAL;
  297. mutex_lock(&wm8350->auxadc_mutex);
  298. /* Turn on the ADC */
  299. reg = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
  300. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_5, reg | WM8350_AUXADC_ENA);
  301. if (scale || vref) {
  302. reg = scale << 13;
  303. reg |= vref << 12;
  304. wm8350_reg_write(wm8350, WM8350_AUX1_READBACK + channel, reg);
  305. }
  306. reg = wm8350_reg_read(wm8350, WM8350_DIGITISER_CONTROL_1);
  307. reg |= 1 << channel | WM8350_AUXADC_POLL;
  308. wm8350_reg_write(wm8350, WM8350_DIGITISER_CONTROL_1, reg);
  309. /* If a late IRQ left the completion signalled then consume
  310. * the completion. */
  311. try_wait_for_completion(&wm8350->auxadc_done);
  312. /* We ignore the result of the completion and just check for a
  313. * conversion result, allowing us to soldier on if the IRQ
  314. * infrastructure is not set up for the chip. */
  315. wait_for_completion_timeout(&wm8350->auxadc_done, msecs_to_jiffies(5));
  316. reg = wm8350_reg_read(wm8350, WM8350_DIGITISER_CONTROL_1);
  317. if (reg & WM8350_AUXADC_POLL)
  318. dev_err(wm8350->dev, "adc chn %d read timeout\n", channel);
  319. else
  320. result = wm8350_reg_read(wm8350,
  321. WM8350_AUX1_READBACK + channel);
  322. /* Turn off the ADC */
  323. reg = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
  324. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_5,
  325. reg & ~WM8350_AUXADC_ENA);
  326. mutex_unlock(&wm8350->auxadc_mutex);
  327. return result & WM8350_AUXADC_DATA1_MASK;
  328. }
  329. EXPORT_SYMBOL_GPL(wm8350_read_auxadc);
  330. static irqreturn_t wm8350_auxadc_irq(int irq, void *irq_data)
  331. {
  332. struct wm8350 *wm8350 = irq_data;
  333. complete(&wm8350->auxadc_done);
  334. return IRQ_HANDLED;
  335. }
  336. /*
  337. * Cache is always host endian.
  338. */
  339. static int wm8350_create_cache(struct wm8350 *wm8350, int type, int mode)
  340. {
  341. int i, ret = 0;
  342. u16 value;
  343. const u16 *reg_map;
  344. switch (type) {
  345. case 0:
  346. switch (mode) {
  347. #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_0
  348. case 0:
  349. reg_map = wm8350_mode0_defaults;
  350. break;
  351. #endif
  352. #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_1
  353. case 1:
  354. reg_map = wm8350_mode1_defaults;
  355. break;
  356. #endif
  357. #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_2
  358. case 2:
  359. reg_map = wm8350_mode2_defaults;
  360. break;
  361. #endif
  362. #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_3
  363. case 3:
  364. reg_map = wm8350_mode3_defaults;
  365. break;
  366. #endif
  367. default:
  368. dev_err(wm8350->dev,
  369. "WM8350 configuration mode %d not supported\n",
  370. mode);
  371. return -EINVAL;
  372. }
  373. break;
  374. case 1:
  375. switch (mode) {
  376. #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_0
  377. case 0:
  378. reg_map = wm8351_mode0_defaults;
  379. break;
  380. #endif
  381. #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_1
  382. case 1:
  383. reg_map = wm8351_mode1_defaults;
  384. break;
  385. #endif
  386. #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_2
  387. case 2:
  388. reg_map = wm8351_mode2_defaults;
  389. break;
  390. #endif
  391. #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_3
  392. case 3:
  393. reg_map = wm8351_mode3_defaults;
  394. break;
  395. #endif
  396. default:
  397. dev_err(wm8350->dev,
  398. "WM8351 configuration mode %d not supported\n",
  399. mode);
  400. return -EINVAL;
  401. }
  402. break;
  403. case 2:
  404. switch (mode) {
  405. #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_0
  406. case 0:
  407. reg_map = wm8352_mode0_defaults;
  408. break;
  409. #endif
  410. #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_1
  411. case 1:
  412. reg_map = wm8352_mode1_defaults;
  413. break;
  414. #endif
  415. #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_2
  416. case 2:
  417. reg_map = wm8352_mode2_defaults;
  418. break;
  419. #endif
  420. #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_3
  421. case 3:
  422. reg_map = wm8352_mode3_defaults;
  423. break;
  424. #endif
  425. default:
  426. dev_err(wm8350->dev,
  427. "WM8352 configuration mode %d not supported\n",
  428. mode);
  429. return -EINVAL;
  430. }
  431. break;
  432. default:
  433. dev_err(wm8350->dev,
  434. "WM835x configuration mode %d not supported\n",
  435. mode);
  436. return -EINVAL;
  437. }
  438. wm8350->reg_cache =
  439. kmalloc(sizeof(u16) * (WM8350_MAX_REGISTER + 1), GFP_KERNEL);
  440. if (wm8350->reg_cache == NULL)
  441. return -ENOMEM;
  442. /* Read the initial cache state back from the device - this is
  443. * a PMIC so the device many not be in a virgin state and we
  444. * can't rely on the silicon values.
  445. */
  446. ret = wm8350->read_dev(wm8350, 0,
  447. sizeof(u16) * (WM8350_MAX_REGISTER + 1),
  448. wm8350->reg_cache);
  449. if (ret < 0) {
  450. dev_err(wm8350->dev,
  451. "failed to read initial cache values\n");
  452. goto out;
  453. }
  454. /* Mask out uncacheable/unreadable bits and the audio. */
  455. for (i = 0; i < WM8350_MAX_REGISTER; i++) {
  456. if (wm8350_reg_io_map[i].readable &&
  457. (i < WM8350_CLOCK_CONTROL_1 || i > WM8350_AIF_TEST)) {
  458. value = be16_to_cpu(wm8350->reg_cache[i]);
  459. value &= wm8350_reg_io_map[i].readable;
  460. wm8350->reg_cache[i] = value;
  461. } else
  462. wm8350->reg_cache[i] = reg_map[i];
  463. }
  464. out:
  465. kfree(wm8350->reg_cache);
  466. return ret;
  467. }
  468. /*
  469. * Register a client device. This is non-fatal since there is no need to
  470. * fail the entire device init due to a single platform device failing.
  471. */
  472. static void wm8350_client_dev_register(struct wm8350 *wm8350,
  473. const char *name,
  474. struct platform_device **pdev)
  475. {
  476. int ret;
  477. *pdev = platform_device_alloc(name, -1);
  478. if (*pdev == NULL) {
  479. dev_err(wm8350->dev, "Failed to allocate %s\n", name);
  480. return;
  481. }
  482. (*pdev)->dev.parent = wm8350->dev;
  483. platform_set_drvdata(*pdev, wm8350);
  484. ret = platform_device_add(*pdev);
  485. if (ret != 0) {
  486. dev_err(wm8350->dev, "Failed to register %s: %d\n", name, ret);
  487. platform_device_put(*pdev);
  488. *pdev = NULL;
  489. }
  490. }
  491. int wm8350_device_init(struct wm8350 *wm8350, int irq,
  492. struct wm8350_platform_data *pdata)
  493. {
  494. int ret;
  495. u16 id1, id2, mask_rev;
  496. u16 cust_id, mode, chip_rev;
  497. dev_set_drvdata(wm8350->dev, wm8350);
  498. /* get WM8350 revision and config mode */
  499. ret = wm8350->read_dev(wm8350, WM8350_RESET_ID, sizeof(id1), &id1);
  500. if (ret != 0) {
  501. dev_err(wm8350->dev, "Failed to read ID: %d\n", ret);
  502. goto err;
  503. }
  504. ret = wm8350->read_dev(wm8350, WM8350_ID, sizeof(id2), &id2);
  505. if (ret != 0) {
  506. dev_err(wm8350->dev, "Failed to read ID: %d\n", ret);
  507. goto err;
  508. }
  509. ret = wm8350->read_dev(wm8350, WM8350_REVISION, sizeof(mask_rev),
  510. &mask_rev);
  511. if (ret != 0) {
  512. dev_err(wm8350->dev, "Failed to read revision: %d\n", ret);
  513. goto err;
  514. }
  515. id1 = be16_to_cpu(id1);
  516. id2 = be16_to_cpu(id2);
  517. mask_rev = be16_to_cpu(mask_rev);
  518. if (id1 != 0x6143) {
  519. dev_err(wm8350->dev,
  520. "Device with ID %x is not a WM8350\n", id1);
  521. ret = -ENODEV;
  522. goto err;
  523. }
  524. mode = id2 & WM8350_CONF_STS_MASK >> 10;
  525. cust_id = id2 & WM8350_CUST_ID_MASK;
  526. chip_rev = (id2 & WM8350_CHIP_REV_MASK) >> 12;
  527. dev_info(wm8350->dev,
  528. "CONF_STS %d, CUST_ID %d, MASK_REV %d, CHIP_REV %d\n",
  529. mode, cust_id, mask_rev, chip_rev);
  530. if (cust_id != 0) {
  531. dev_err(wm8350->dev, "Unsupported CUST_ID\n");
  532. ret = -ENODEV;
  533. goto err;
  534. }
  535. switch (mask_rev) {
  536. case 0:
  537. wm8350->pmic.max_dcdc = WM8350_DCDC_6;
  538. wm8350->pmic.max_isink = WM8350_ISINK_B;
  539. switch (chip_rev) {
  540. case WM8350_REV_E:
  541. dev_info(wm8350->dev, "WM8350 Rev E\n");
  542. break;
  543. case WM8350_REV_F:
  544. dev_info(wm8350->dev, "WM8350 Rev F\n");
  545. break;
  546. case WM8350_REV_G:
  547. dev_info(wm8350->dev, "WM8350 Rev G\n");
  548. wm8350->power.rev_g_coeff = 1;
  549. break;
  550. case WM8350_REV_H:
  551. dev_info(wm8350->dev, "WM8350 Rev H\n");
  552. wm8350->power.rev_g_coeff = 1;
  553. break;
  554. default:
  555. /* For safety we refuse to run on unknown hardware */
  556. dev_err(wm8350->dev, "Unknown WM8350 CHIP_REV\n");
  557. ret = -ENODEV;
  558. goto err;
  559. }
  560. break;
  561. case 1:
  562. wm8350->pmic.max_dcdc = WM8350_DCDC_4;
  563. wm8350->pmic.max_isink = WM8350_ISINK_A;
  564. switch (chip_rev) {
  565. case 0:
  566. dev_info(wm8350->dev, "WM8351 Rev A\n");
  567. wm8350->power.rev_g_coeff = 1;
  568. break;
  569. case 1:
  570. dev_info(wm8350->dev, "WM8351 Rev B\n");
  571. wm8350->power.rev_g_coeff = 1;
  572. break;
  573. default:
  574. dev_err(wm8350->dev, "Unknown WM8351 CHIP_REV\n");
  575. ret = -ENODEV;
  576. goto err;
  577. }
  578. break;
  579. case 2:
  580. wm8350->pmic.max_dcdc = WM8350_DCDC_6;
  581. wm8350->pmic.max_isink = WM8350_ISINK_B;
  582. switch (chip_rev) {
  583. case 0:
  584. dev_info(wm8350->dev, "WM8352 Rev A\n");
  585. wm8350->power.rev_g_coeff = 1;
  586. break;
  587. default:
  588. dev_err(wm8350->dev, "Unknown WM8352 CHIP_REV\n");
  589. ret = -ENODEV;
  590. goto err;
  591. }
  592. break;
  593. default:
  594. dev_err(wm8350->dev, "Unknown MASK_REV\n");
  595. ret = -ENODEV;
  596. goto err;
  597. }
  598. ret = wm8350_create_cache(wm8350, mask_rev, mode);
  599. if (ret < 0) {
  600. dev_err(wm8350->dev, "Failed to create register cache\n");
  601. return ret;
  602. }
  603. mutex_init(&wm8350->auxadc_mutex);
  604. init_completion(&wm8350->auxadc_done);
  605. ret = wm8350_irq_init(wm8350, irq, pdata);
  606. if (ret < 0)
  607. goto err_free;
  608. if (wm8350->irq_base) {
  609. ret = request_threaded_irq(wm8350->irq_base +
  610. WM8350_IRQ_AUXADC_DATARDY,
  611. NULL, wm8350_auxadc_irq, 0,
  612. "auxadc", wm8350);
  613. if (ret < 0)
  614. dev_warn(wm8350->dev,
  615. "Failed to request AUXADC IRQ: %d\n", ret);
  616. }
  617. if (pdata && pdata->init) {
  618. ret = pdata->init(wm8350);
  619. if (ret != 0) {
  620. dev_err(wm8350->dev, "Platform init() failed: %d\n",
  621. ret);
  622. goto err_irq;
  623. }
  624. }
  625. wm8350_reg_write(wm8350, WM8350_SYSTEM_INTERRUPTS_MASK, 0x0);
  626. wm8350_client_dev_register(wm8350, "wm8350-codec",
  627. &(wm8350->codec.pdev));
  628. wm8350_client_dev_register(wm8350, "wm8350-gpio",
  629. &(wm8350->gpio.pdev));
  630. wm8350_client_dev_register(wm8350, "wm8350-hwmon",
  631. &(wm8350->hwmon.pdev));
  632. wm8350_client_dev_register(wm8350, "wm8350-power",
  633. &(wm8350->power.pdev));
  634. wm8350_client_dev_register(wm8350, "wm8350-rtc", &(wm8350->rtc.pdev));
  635. wm8350_client_dev_register(wm8350, "wm8350-wdt", &(wm8350->wdt.pdev));
  636. return 0;
  637. err_irq:
  638. wm8350_irq_exit(wm8350);
  639. err_free:
  640. kfree(wm8350->reg_cache);
  641. err:
  642. return ret;
  643. }
  644. EXPORT_SYMBOL_GPL(wm8350_device_init);
  645. void wm8350_device_exit(struct wm8350 *wm8350)
  646. {
  647. int i;
  648. for (i = 0; i < ARRAY_SIZE(wm8350->pmic.led); i++)
  649. platform_device_unregister(wm8350->pmic.led[i].pdev);
  650. for (i = 0; i < ARRAY_SIZE(wm8350->pmic.pdev); i++)
  651. platform_device_unregister(wm8350->pmic.pdev[i]);
  652. platform_device_unregister(wm8350->wdt.pdev);
  653. platform_device_unregister(wm8350->rtc.pdev);
  654. platform_device_unregister(wm8350->power.pdev);
  655. platform_device_unregister(wm8350->hwmon.pdev);
  656. platform_device_unregister(wm8350->gpio.pdev);
  657. platform_device_unregister(wm8350->codec.pdev);
  658. if (wm8350->irq_base)
  659. free_irq(wm8350->irq_base + WM8350_IRQ_AUXADC_DATARDY, wm8350);
  660. wm8350_irq_exit(wm8350);
  661. kfree(wm8350->reg_cache);
  662. }
  663. EXPORT_SYMBOL_GPL(wm8350_device_exit);
  664. MODULE_DESCRIPTION("WM8350 AudioPlus PMIC core driver");
  665. MODULE_LICENSE("GPL");