wm8731.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * wm8731.c -- WM8731 ALSA SoC Audio driver
  3. *
  4. * Copyright 2005 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <richard@openedhand.com>
  7. *
  8. * Based on wm8753.c by Liam Girdwood
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/spi/spi.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/soc-dapm.h>
  29. #include <sound/initval.h>
  30. #include <sound/tlv.h>
  31. #include "wm8731.h"
  32. #define WM8731_NUM_SUPPLIES 4
  33. static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
  34. "AVDD",
  35. "HPVDD",
  36. "DCVDD",
  37. "DBVDD",
  38. };
  39. /* codec private data */
  40. struct wm8731_priv {
  41. enum snd_soc_control_type control_type;
  42. struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
  43. u16 reg_cache[WM8731_CACHEREGNUM];
  44. unsigned int sysclk;
  45. int sysclk_type;
  46. };
  47. /*
  48. * wm8731 register cache
  49. * We can't read the WM8731 register space when we are
  50. * using 2 wire for device control, so we cache them instead.
  51. * There is no point in caching the reset register
  52. */
  53. static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
  54. 0x0097, 0x0097, 0x0079, 0x0079,
  55. 0x000a, 0x0008, 0x009f, 0x000a,
  56. 0x0000, 0x0000
  57. };
  58. #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
  59. static const char *wm8731_input_select[] = {"Line In", "Mic"};
  60. static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  61. static const struct soc_enum wm8731_enum[] = {
  62. SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
  63. SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
  64. };
  65. static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
  66. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
  67. static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
  68. static const struct snd_kcontrol_new wm8731_snd_controls[] = {
  69. SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
  70. 0, 127, 0, out_tlv),
  71. SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
  72. 7, 1, 0),
  73. SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
  74. in_tlv),
  75. SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
  76. SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
  77. SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
  78. SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
  79. sidetone_tlv),
  80. SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
  81. SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
  82. SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
  83. };
  84. /* Output Mixer */
  85. static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
  86. SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
  87. SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
  88. SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
  89. };
  90. /* Input mux */
  91. static const struct snd_kcontrol_new wm8731_input_mux_controls =
  92. SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
  93. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  94. SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
  95. SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
  96. &wm8731_output_mixer_controls[0],
  97. ARRAY_SIZE(wm8731_output_mixer_controls)),
  98. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
  99. SND_SOC_DAPM_OUTPUT("LOUT"),
  100. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  101. SND_SOC_DAPM_OUTPUT("ROUT"),
  102. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  103. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
  104. SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
  105. SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
  106. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
  107. SND_SOC_DAPM_INPUT("MICIN"),
  108. SND_SOC_DAPM_INPUT("RLINEIN"),
  109. SND_SOC_DAPM_INPUT("LLINEIN"),
  110. };
  111. static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
  112. struct snd_soc_dapm_widget *sink)
  113. {
  114. struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec);
  115. return wm8731->sysclk_type == WM8731_SYSCLK_MCLK;
  116. }
  117. static const struct snd_soc_dapm_route intercon[] = {
  118. {"DAC", NULL, "OSC", wm8731_check_osc},
  119. {"ADC", NULL, "OSC", wm8731_check_osc},
  120. /* output mixer */
  121. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  122. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  123. {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
  124. /* outputs */
  125. {"RHPOUT", NULL, "Output Mixer"},
  126. {"ROUT", NULL, "Output Mixer"},
  127. {"LHPOUT", NULL, "Output Mixer"},
  128. {"LOUT", NULL, "Output Mixer"},
  129. /* input mux */
  130. {"Input Mux", "Line In", "Line Input"},
  131. {"Input Mux", "Mic", "Mic Bias"},
  132. {"ADC", NULL, "Input Mux"},
  133. /* inputs */
  134. {"Line Input", NULL, "LLINEIN"},
  135. {"Line Input", NULL, "RLINEIN"},
  136. {"Mic Bias", NULL, "MICIN"},
  137. };
  138. static int wm8731_add_widgets(struct snd_soc_codec *codec)
  139. {
  140. struct snd_soc_dapm_context *dapm = &codec->dapm;
  141. snd_soc_dapm_new_controls(dapm, wm8731_dapm_widgets,
  142. ARRAY_SIZE(wm8731_dapm_widgets));
  143. snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
  144. return 0;
  145. }
  146. struct _coeff_div {
  147. u32 mclk;
  148. u32 rate;
  149. u16 fs;
  150. u8 sr:4;
  151. u8 bosr:1;
  152. u8 usb:1;
  153. };
  154. /* codec mclk clock divider coefficients */
  155. static const struct _coeff_div coeff_div[] = {
  156. /* 48k */
  157. {12288000, 48000, 256, 0x0, 0x0, 0x0},
  158. {18432000, 48000, 384, 0x0, 0x1, 0x0},
  159. {12000000, 48000, 250, 0x0, 0x0, 0x1},
  160. /* 32k */
  161. {12288000, 32000, 384, 0x6, 0x0, 0x0},
  162. {18432000, 32000, 576, 0x6, 0x1, 0x0},
  163. {12000000, 32000, 375, 0x6, 0x0, 0x1},
  164. /* 8k */
  165. {12288000, 8000, 1536, 0x3, 0x0, 0x0},
  166. {18432000, 8000, 2304, 0x3, 0x1, 0x0},
  167. {11289600, 8000, 1408, 0xb, 0x0, 0x0},
  168. {16934400, 8000, 2112, 0xb, 0x1, 0x0},
  169. {12000000, 8000, 1500, 0x3, 0x0, 0x1},
  170. /* 96k */
  171. {12288000, 96000, 128, 0x7, 0x0, 0x0},
  172. {18432000, 96000, 192, 0x7, 0x1, 0x0},
  173. {12000000, 96000, 125, 0x7, 0x0, 0x1},
  174. /* 44.1k */
  175. {11289600, 44100, 256, 0x8, 0x0, 0x0},
  176. {16934400, 44100, 384, 0x8, 0x1, 0x0},
  177. {12000000, 44100, 272, 0x8, 0x1, 0x1},
  178. /* 88.2k */
  179. {11289600, 88200, 128, 0xf, 0x0, 0x0},
  180. {16934400, 88200, 192, 0xf, 0x1, 0x0},
  181. {12000000, 88200, 136, 0xf, 0x1, 0x1},
  182. };
  183. static inline int get_coeff(int mclk, int rate)
  184. {
  185. int i;
  186. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  187. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  188. return i;
  189. }
  190. return 0;
  191. }
  192. static int wm8731_hw_params(struct snd_pcm_substream *substream,
  193. struct snd_pcm_hw_params *params,
  194. struct snd_soc_dai *dai)
  195. {
  196. struct snd_soc_codec *codec = dai->codec;
  197. struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
  198. u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
  199. int i = get_coeff(wm8731->sysclk, params_rate(params));
  200. u16 srate = (coeff_div[i].sr << 2) |
  201. (coeff_div[i].bosr << 1) | coeff_div[i].usb;
  202. snd_soc_write(codec, WM8731_SRATE, srate);
  203. /* bit size */
  204. switch (params_format(params)) {
  205. case SNDRV_PCM_FORMAT_S16_LE:
  206. break;
  207. case SNDRV_PCM_FORMAT_S20_3LE:
  208. iface |= 0x0004;
  209. break;
  210. case SNDRV_PCM_FORMAT_S24_LE:
  211. iface |= 0x0008;
  212. break;
  213. }
  214. snd_soc_write(codec, WM8731_IFACE, iface);
  215. return 0;
  216. }
  217. static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
  218. struct snd_soc_dai *dai)
  219. {
  220. struct snd_soc_codec *codec = dai->codec;
  221. /* set active */
  222. snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
  223. return 0;
  224. }
  225. static void wm8731_shutdown(struct snd_pcm_substream *substream,
  226. struct snd_soc_dai *dai)
  227. {
  228. struct snd_soc_codec *codec = dai->codec;
  229. /* deactivate */
  230. if (!codec->active) {
  231. udelay(50);
  232. snd_soc_write(codec, WM8731_ACTIVE, 0x0);
  233. }
  234. }
  235. static int wm8731_mute(struct snd_soc_dai *dai, int mute)
  236. {
  237. struct snd_soc_codec *codec = dai->codec;
  238. u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
  239. if (mute)
  240. snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
  241. else
  242. snd_soc_write(codec, WM8731_APDIGI, mute_reg);
  243. return 0;
  244. }
  245. static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  246. int clk_id, unsigned int freq, int dir)
  247. {
  248. struct snd_soc_codec *codec = codec_dai->codec;
  249. struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
  250. switch (clk_id) {
  251. case WM8731_SYSCLK_XTAL:
  252. case WM8731_SYSCLK_MCLK:
  253. wm8731->sysclk_type = clk_id;
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. switch (freq) {
  259. case 11289600:
  260. case 12000000:
  261. case 12288000:
  262. case 16934400:
  263. case 18432000:
  264. wm8731->sysclk = freq;
  265. break;
  266. default:
  267. return -EINVAL;
  268. }
  269. snd_soc_dapm_sync(&codec->dapm);
  270. return 0;
  271. }
  272. static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
  273. unsigned int fmt)
  274. {
  275. struct snd_soc_codec *codec = codec_dai->codec;
  276. u16 iface = 0;
  277. /* set master/slave audio interface */
  278. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  279. case SND_SOC_DAIFMT_CBM_CFM:
  280. iface |= 0x0040;
  281. break;
  282. case SND_SOC_DAIFMT_CBS_CFS:
  283. break;
  284. default:
  285. return -EINVAL;
  286. }
  287. /* interface format */
  288. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  289. case SND_SOC_DAIFMT_I2S:
  290. iface |= 0x0002;
  291. break;
  292. case SND_SOC_DAIFMT_RIGHT_J:
  293. break;
  294. case SND_SOC_DAIFMT_LEFT_J:
  295. iface |= 0x0001;
  296. break;
  297. case SND_SOC_DAIFMT_DSP_A:
  298. iface |= 0x0003;
  299. break;
  300. case SND_SOC_DAIFMT_DSP_B:
  301. iface |= 0x0013;
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. /* clock inversion */
  307. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  308. case SND_SOC_DAIFMT_NB_NF:
  309. break;
  310. case SND_SOC_DAIFMT_IB_IF:
  311. iface |= 0x0090;
  312. break;
  313. case SND_SOC_DAIFMT_IB_NF:
  314. iface |= 0x0080;
  315. break;
  316. case SND_SOC_DAIFMT_NB_IF:
  317. iface |= 0x0010;
  318. break;
  319. default:
  320. return -EINVAL;
  321. }
  322. /* set iface */
  323. snd_soc_write(codec, WM8731_IFACE, iface);
  324. return 0;
  325. }
  326. static int wm8731_set_bias_level(struct snd_soc_codec *codec,
  327. enum snd_soc_bias_level level)
  328. {
  329. struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
  330. int i, ret;
  331. u8 data[2];
  332. u16 *cache = codec->reg_cache;
  333. u16 reg;
  334. switch (level) {
  335. case SND_SOC_BIAS_ON:
  336. break;
  337. case SND_SOC_BIAS_PREPARE:
  338. break;
  339. case SND_SOC_BIAS_STANDBY:
  340. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  341. ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
  342. wm8731->supplies);
  343. if (ret != 0)
  344. return ret;
  345. /* Sync reg_cache with the hardware */
  346. for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
  347. if (cache[i] == wm8731_reg[i])
  348. continue;
  349. data[0] = (i << 1) | ((cache[i] >> 8)
  350. & 0x0001);
  351. data[1] = cache[i] & 0x00ff;
  352. codec->hw_write(codec->control_data, data, 2);
  353. }
  354. }
  355. /* Clear PWROFF, gate CLKOUT, everything else as-is */
  356. reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
  357. snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
  358. break;
  359. case SND_SOC_BIAS_OFF:
  360. snd_soc_write(codec, WM8731_ACTIVE, 0x0);
  361. snd_soc_write(codec, WM8731_PWR, 0xffff);
  362. regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
  363. wm8731->supplies);
  364. break;
  365. }
  366. codec->dapm.bias_level = level;
  367. return 0;
  368. }
  369. #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
  370. #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  371. SNDRV_PCM_FMTBIT_S24_LE)
  372. static struct snd_soc_dai_ops wm8731_dai_ops = {
  373. .prepare = wm8731_pcm_prepare,
  374. .hw_params = wm8731_hw_params,
  375. .shutdown = wm8731_shutdown,
  376. .digital_mute = wm8731_mute,
  377. .set_sysclk = wm8731_set_dai_sysclk,
  378. .set_fmt = wm8731_set_dai_fmt,
  379. };
  380. static struct snd_soc_dai_driver wm8731_dai = {
  381. .name = "wm8731-hifi",
  382. .playback = {
  383. .stream_name = "Playback",
  384. .channels_min = 1,
  385. .channels_max = 2,
  386. .rates = WM8731_RATES,
  387. .formats = WM8731_FORMATS,},
  388. .capture = {
  389. .stream_name = "Capture",
  390. .channels_min = 1,
  391. .channels_max = 2,
  392. .rates = WM8731_RATES,
  393. .formats = WM8731_FORMATS,},
  394. .ops = &wm8731_dai_ops,
  395. .symmetric_rates = 1,
  396. };
  397. #ifdef CONFIG_PM
  398. static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state)
  399. {
  400. wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
  401. return 0;
  402. }
  403. static int wm8731_resume(struct snd_soc_codec *codec)
  404. {
  405. wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  406. return 0;
  407. }
  408. #else
  409. #define wm8731_suspend NULL
  410. #define wm8731_resume NULL
  411. #endif
  412. static int wm8731_probe(struct snd_soc_codec *codec)
  413. {
  414. struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
  415. int ret = 0, i;
  416. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type);
  417. if (ret < 0) {
  418. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  419. return ret;
  420. }
  421. for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
  422. wm8731->supplies[i].supply = wm8731_supply_names[i];
  423. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
  424. wm8731->supplies);
  425. if (ret != 0) {
  426. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  427. return ret;
  428. }
  429. ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
  430. wm8731->supplies);
  431. if (ret != 0) {
  432. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  433. goto err_regulator_get;
  434. }
  435. ret = wm8731_reset(codec);
  436. if (ret < 0) {
  437. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  438. goto err_regulator_enable;
  439. }
  440. wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  441. /* Latch the update bits */
  442. snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
  443. snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
  444. snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
  445. snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
  446. /* Disable bypass path by default */
  447. snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
  448. snd_soc_add_controls(codec, wm8731_snd_controls,
  449. ARRAY_SIZE(wm8731_snd_controls));
  450. wm8731_add_widgets(codec);
  451. /* Regulators will have been enabled by bias management */
  452. regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
  453. return 0;
  454. err_regulator_enable:
  455. regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
  456. err_regulator_get:
  457. regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
  458. kfree(wm8731);
  459. return ret;
  460. }
  461. /* power down chip */
  462. static int wm8731_remove(struct snd_soc_codec *codec)
  463. {
  464. struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
  465. wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
  466. regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
  467. regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
  468. return 0;
  469. }
  470. static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
  471. .probe = wm8731_probe,
  472. .remove = wm8731_remove,
  473. .suspend = wm8731_suspend,
  474. .resume = wm8731_resume,
  475. .set_bias_level = wm8731_set_bias_level,
  476. .reg_cache_size = ARRAY_SIZE(wm8731_reg),
  477. .reg_word_size = sizeof(u16),
  478. .reg_cache_default = wm8731_reg,
  479. };
  480. #if defined(CONFIG_SPI_MASTER)
  481. static int __devinit wm8731_spi_probe(struct spi_device *spi)
  482. {
  483. struct wm8731_priv *wm8731;
  484. int ret;
  485. wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
  486. if (wm8731 == NULL)
  487. return -ENOMEM;
  488. wm8731->control_type = SND_SOC_SPI;
  489. spi_set_drvdata(spi, wm8731);
  490. ret = snd_soc_register_codec(&spi->dev,
  491. &soc_codec_dev_wm8731, &wm8731_dai, 1);
  492. if (ret < 0)
  493. kfree(wm8731);
  494. return ret;
  495. }
  496. static int __devexit wm8731_spi_remove(struct spi_device *spi)
  497. {
  498. snd_soc_unregister_codec(&spi->dev);
  499. kfree(spi_get_drvdata(spi));
  500. return 0;
  501. }
  502. static struct spi_driver wm8731_spi_driver = {
  503. .driver = {
  504. .name = "wm8731-codec",
  505. .owner = THIS_MODULE,
  506. },
  507. .probe = wm8731_spi_probe,
  508. .remove = __devexit_p(wm8731_spi_remove),
  509. };
  510. #endif /* CONFIG_SPI_MASTER */
  511. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  512. static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
  513. const struct i2c_device_id *id)
  514. {
  515. struct wm8731_priv *wm8731;
  516. int ret;
  517. wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
  518. if (wm8731 == NULL)
  519. return -ENOMEM;
  520. i2c_set_clientdata(i2c, wm8731);
  521. wm8731->control_type = SND_SOC_I2C;
  522. ret = snd_soc_register_codec(&i2c->dev,
  523. &soc_codec_dev_wm8731, &wm8731_dai, 1);
  524. if (ret < 0)
  525. kfree(wm8731);
  526. return ret;
  527. }
  528. static __devexit int wm8731_i2c_remove(struct i2c_client *client)
  529. {
  530. snd_soc_unregister_codec(&client->dev);
  531. kfree(i2c_get_clientdata(client));
  532. return 0;
  533. }
  534. static const struct i2c_device_id wm8731_i2c_id[] = {
  535. { "wm8731", 0 },
  536. { }
  537. };
  538. MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
  539. static struct i2c_driver wm8731_i2c_driver = {
  540. .driver = {
  541. .name = "wm8731-codec",
  542. .owner = THIS_MODULE,
  543. },
  544. .probe = wm8731_i2c_probe,
  545. .remove = __devexit_p(wm8731_i2c_remove),
  546. .id_table = wm8731_i2c_id,
  547. };
  548. #endif
  549. static int __init wm8731_modinit(void)
  550. {
  551. int ret = 0;
  552. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  553. ret = i2c_add_driver(&wm8731_i2c_driver);
  554. if (ret != 0) {
  555. printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
  556. ret);
  557. }
  558. #endif
  559. #if defined(CONFIG_SPI_MASTER)
  560. ret = spi_register_driver(&wm8731_spi_driver);
  561. if (ret != 0) {
  562. printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
  563. ret);
  564. }
  565. #endif
  566. return ret;
  567. }
  568. module_init(wm8731_modinit);
  569. static void __exit wm8731_exit(void)
  570. {
  571. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  572. i2c_del_driver(&wm8731_i2c_driver);
  573. #endif
  574. #if defined(CONFIG_SPI_MASTER)
  575. spi_unregister_driver(&wm8731_spi_driver);
  576. #endif
  577. }
  578. module_exit(wm8731_exit);
  579. MODULE_DESCRIPTION("ASoC WM8731 driver");
  580. MODULE_AUTHOR("Richard Purdie");
  581. MODULE_LICENSE("GPL");