wm8731.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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/platform_device.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/initval.h>
  27. #include "wm8731.h"
  28. #define AUDIO_NAME "wm8731"
  29. #define WM8731_VERSION "0.13"
  30. /*
  31. * Debug
  32. */
  33. #define WM8731_DEBUG 0
  34. #ifdef WM8731_DEBUG
  35. #define dbg(format, arg...) \
  36. printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg)
  37. #else
  38. #define dbg(format, arg...) do {} while (0)
  39. #endif
  40. #define err(format, arg...) \
  41. printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
  42. #define info(format, arg...) \
  43. printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
  44. #define warn(format, arg...) \
  45. printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)
  46. struct snd_soc_codec_device soc_codec_dev_wm8731;
  47. /* codec private data */
  48. struct wm8731_priv {
  49. unsigned int sysclk;
  50. };
  51. /*
  52. * wm8731 register cache
  53. * We can't read the WM8731 register space when we are
  54. * using 2 wire for device control, so we cache them instead.
  55. * There is no point in caching the reset register
  56. */
  57. static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
  58. 0x0097, 0x0097, 0x0079, 0x0079,
  59. 0x000a, 0x0008, 0x009f, 0x000a,
  60. 0x0000, 0x0000
  61. };
  62. /*
  63. * read wm8731 register cache
  64. */
  65. static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
  66. unsigned int reg)
  67. {
  68. u16 *cache = codec->reg_cache;
  69. if (reg == WM8731_RESET)
  70. return 0;
  71. if (reg >= WM8731_CACHEREGNUM)
  72. return -1;
  73. return cache[reg];
  74. }
  75. /*
  76. * write wm8731 register cache
  77. */
  78. static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
  79. u16 reg, unsigned int value)
  80. {
  81. u16 *cache = codec->reg_cache;
  82. if (reg >= WM8731_CACHEREGNUM)
  83. return;
  84. cache[reg] = value;
  85. }
  86. /*
  87. * write to the WM8731 register space
  88. */
  89. static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
  90. unsigned int value)
  91. {
  92. u8 data[2];
  93. /* data is
  94. * D15..D9 WM8731 register offset
  95. * D8...D0 register data
  96. */
  97. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  98. data[1] = value & 0x00ff;
  99. wm8731_write_reg_cache(codec, reg, value);
  100. if (codec->hw_write(codec->control_data, data, 2) == 2)
  101. return 0;
  102. else
  103. return -EIO;
  104. }
  105. #define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
  106. static const char *wm8731_input_select[] = {"Line In", "Mic"};
  107. static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  108. static const struct soc_enum wm8731_enum[] = {
  109. SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
  110. SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
  111. };
  112. static const struct snd_kcontrol_new wm8731_snd_controls[] = {
  113. SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
  114. 0, 127, 0),
  115. SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
  116. 7, 1, 0),
  117. SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
  118. SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
  119. SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
  120. SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
  121. SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
  122. SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
  123. SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
  124. SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
  125. };
  126. /* add non dapm controls */
  127. static int wm8731_add_controls(struct snd_soc_codec *codec)
  128. {
  129. int err, i;
  130. for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) {
  131. err = snd_ctl_add(codec->card,
  132. snd_soc_cnew(&wm8731_snd_controls[i],
  133. codec, NULL));
  134. if (err < 0)
  135. return err;
  136. }
  137. return 0;
  138. }
  139. /* Output Mixer */
  140. static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
  141. SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
  142. SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
  143. SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
  144. };
  145. /* Input mux */
  146. static const struct snd_kcontrol_new wm8731_input_mux_controls =
  147. SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
  148. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  149. SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
  150. &wm8731_output_mixer_controls[0],
  151. ARRAY_SIZE(wm8731_output_mixer_controls)),
  152. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
  153. SND_SOC_DAPM_OUTPUT("LOUT"),
  154. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  155. SND_SOC_DAPM_OUTPUT("ROUT"),
  156. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  157. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
  158. SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
  159. SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
  160. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
  161. SND_SOC_DAPM_INPUT("MICIN"),
  162. SND_SOC_DAPM_INPUT("RLINEIN"),
  163. SND_SOC_DAPM_INPUT("LLINEIN"),
  164. };
  165. static const struct snd_soc_dapm_route intercon[] = {
  166. /* output mixer */
  167. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  168. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  169. {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
  170. /* outputs */
  171. {"RHPOUT", NULL, "Output Mixer"},
  172. {"ROUT", NULL, "Output Mixer"},
  173. {"LHPOUT", NULL, "Output Mixer"},
  174. {"LOUT", NULL, "Output Mixer"},
  175. /* input mux */
  176. {"Input Mux", "Line In", "Line Input"},
  177. {"Input Mux", "Mic", "Mic Bias"},
  178. {"ADC", NULL, "Input Mux"},
  179. /* inputs */
  180. {"Line Input", NULL, "LLINEIN"},
  181. {"Line Input", NULL, "RLINEIN"},
  182. {"Mic Bias", NULL, "MICIN"},
  183. };
  184. static int wm8731_add_widgets(struct snd_soc_codec *codec)
  185. {
  186. snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
  187. ARRAY_SIZE(wm8731_dapm_widgets));
  188. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  189. snd_soc_dapm_new_widgets(codec);
  190. return 0;
  191. }
  192. struct _coeff_div {
  193. u32 mclk;
  194. u32 rate;
  195. u16 fs;
  196. u8 sr:4;
  197. u8 bosr:1;
  198. u8 usb:1;
  199. };
  200. /* codec mclk clock divider coefficients */
  201. static const struct _coeff_div coeff_div[] = {
  202. /* 48k */
  203. {12288000, 48000, 256, 0x0, 0x0, 0x0},
  204. {18432000, 48000, 384, 0x0, 0x1, 0x0},
  205. {12000000, 48000, 250, 0x0, 0x0, 0x1},
  206. /* 32k */
  207. {12288000, 32000, 384, 0x6, 0x0, 0x0},
  208. {18432000, 32000, 576, 0x6, 0x1, 0x0},
  209. {12000000, 32000, 375, 0x6, 0x0, 0x1},
  210. /* 8k */
  211. {12288000, 8000, 1536, 0x3, 0x0, 0x0},
  212. {18432000, 8000, 2304, 0x3, 0x1, 0x0},
  213. {11289600, 8000, 1408, 0xb, 0x0, 0x0},
  214. {16934400, 8000, 2112, 0xb, 0x1, 0x0},
  215. {12000000, 8000, 1500, 0x3, 0x0, 0x1},
  216. /* 96k */
  217. {12288000, 96000, 128, 0x7, 0x0, 0x0},
  218. {18432000, 96000, 192, 0x7, 0x1, 0x0},
  219. {12000000, 96000, 125, 0x7, 0x0, 0x1},
  220. /* 44.1k */
  221. {11289600, 44100, 256, 0x8, 0x0, 0x0},
  222. {16934400, 44100, 384, 0x8, 0x1, 0x0},
  223. {12000000, 44100, 272, 0x8, 0x1, 0x1},
  224. /* 88.2k */
  225. {11289600, 88200, 128, 0xf, 0x0, 0x0},
  226. {16934400, 88200, 192, 0xf, 0x1, 0x0},
  227. {12000000, 88200, 136, 0xf, 0x1, 0x1},
  228. };
  229. static inline int get_coeff(int mclk, int rate)
  230. {
  231. int i;
  232. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  233. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  234. return i;
  235. }
  236. return 0;
  237. }
  238. static int wm8731_hw_params(struct snd_pcm_substream *substream,
  239. struct snd_pcm_hw_params *params)
  240. {
  241. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  242. struct snd_soc_device *socdev = rtd->socdev;
  243. struct snd_soc_codec *codec = socdev->codec;
  244. struct wm8731_priv *wm8731 = codec->private_data;
  245. u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
  246. int i = get_coeff(wm8731->sysclk, params_rate(params));
  247. u16 srate = (coeff_div[i].sr << 2) |
  248. (coeff_div[i].bosr << 1) | coeff_div[i].usb;
  249. wm8731_write(codec, WM8731_SRATE, srate);
  250. /* bit size */
  251. switch (params_format(params)) {
  252. case SNDRV_PCM_FORMAT_S16_LE:
  253. break;
  254. case SNDRV_PCM_FORMAT_S20_3LE:
  255. iface |= 0x0004;
  256. break;
  257. case SNDRV_PCM_FORMAT_S24_LE:
  258. iface |= 0x0008;
  259. break;
  260. }
  261. wm8731_write(codec, WM8731_IFACE, iface);
  262. return 0;
  263. }
  264. static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
  265. {
  266. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  267. struct snd_soc_device *socdev = rtd->socdev;
  268. struct snd_soc_codec *codec = socdev->codec;
  269. /* set active */
  270. wm8731_write(codec, WM8731_ACTIVE, 0x0001);
  271. return 0;
  272. }
  273. static void wm8731_shutdown(struct snd_pcm_substream *substream)
  274. {
  275. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  276. struct snd_soc_device *socdev = rtd->socdev;
  277. struct snd_soc_codec *codec = socdev->codec;
  278. /* deactivate */
  279. if (!codec->active) {
  280. udelay(50);
  281. wm8731_write(codec, WM8731_ACTIVE, 0x0);
  282. }
  283. }
  284. static int wm8731_mute(struct snd_soc_codec_dai *dai, int mute)
  285. {
  286. struct snd_soc_codec *codec = dai->codec;
  287. u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
  288. if (mute)
  289. wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
  290. else
  291. wm8731_write(codec, WM8731_APDIGI, mute_reg);
  292. return 0;
  293. }
  294. static int wm8731_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
  295. int clk_id, unsigned int freq, int dir)
  296. {
  297. struct snd_soc_codec *codec = codec_dai->codec;
  298. struct wm8731_priv *wm8731 = codec->private_data;
  299. switch (freq) {
  300. case 11289600:
  301. case 12000000:
  302. case 12288000:
  303. case 16934400:
  304. case 18432000:
  305. wm8731->sysclk = freq;
  306. return 0;
  307. }
  308. return -EINVAL;
  309. }
  310. static int wm8731_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
  311. unsigned int fmt)
  312. {
  313. struct snd_soc_codec *codec = codec_dai->codec;
  314. u16 iface = 0;
  315. /* set master/slave audio interface */
  316. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  317. case SND_SOC_DAIFMT_CBM_CFM:
  318. iface |= 0x0040;
  319. break;
  320. case SND_SOC_DAIFMT_CBS_CFS:
  321. break;
  322. default:
  323. return -EINVAL;
  324. }
  325. /* interface format */
  326. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  327. case SND_SOC_DAIFMT_I2S:
  328. iface |= 0x0002;
  329. break;
  330. case SND_SOC_DAIFMT_RIGHT_J:
  331. break;
  332. case SND_SOC_DAIFMT_LEFT_J:
  333. iface |= 0x0001;
  334. break;
  335. case SND_SOC_DAIFMT_DSP_A:
  336. iface |= 0x0003;
  337. break;
  338. case SND_SOC_DAIFMT_DSP_B:
  339. iface |= 0x0013;
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. /* clock inversion */
  345. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  346. case SND_SOC_DAIFMT_NB_NF:
  347. break;
  348. case SND_SOC_DAIFMT_IB_IF:
  349. iface |= 0x0090;
  350. break;
  351. case SND_SOC_DAIFMT_IB_NF:
  352. iface |= 0x0080;
  353. break;
  354. case SND_SOC_DAIFMT_NB_IF:
  355. iface |= 0x0010;
  356. break;
  357. default:
  358. return -EINVAL;
  359. }
  360. /* set iface */
  361. wm8731_write(codec, WM8731_IFACE, iface);
  362. return 0;
  363. }
  364. static int wm8731_set_bias_level(struct snd_soc_codec *codec,
  365. enum snd_soc_bias_level level)
  366. {
  367. u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
  368. switch (level) {
  369. case SND_SOC_BIAS_ON:
  370. /* vref/mid, osc on, dac unmute */
  371. wm8731_write(codec, WM8731_PWR, reg);
  372. break;
  373. case SND_SOC_BIAS_PREPARE:
  374. break;
  375. case SND_SOC_BIAS_STANDBY:
  376. /* everything off except vref/vmid, */
  377. wm8731_write(codec, WM8731_PWR, reg | 0x0040);
  378. break;
  379. case SND_SOC_BIAS_OFF:
  380. /* everything off, dac mute, inactive */
  381. wm8731_write(codec, WM8731_ACTIVE, 0x0);
  382. wm8731_write(codec, WM8731_PWR, 0xffff);
  383. break;
  384. }
  385. codec->bias_level = level;
  386. return 0;
  387. }
  388. #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  389. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  390. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  391. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
  392. SNDRV_PCM_RATE_96000)
  393. #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  394. SNDRV_PCM_FMTBIT_S24_LE)
  395. struct snd_soc_codec_dai wm8731_dai = {
  396. .name = "WM8731",
  397. .playback = {
  398. .stream_name = "Playback",
  399. .channels_min = 1,
  400. .channels_max = 2,
  401. .rates = WM8731_RATES,
  402. .formats = WM8731_FORMATS,},
  403. .capture = {
  404. .stream_name = "Capture",
  405. .channels_min = 1,
  406. .channels_max = 2,
  407. .rates = WM8731_RATES,
  408. .formats = WM8731_FORMATS,},
  409. .ops = {
  410. .prepare = wm8731_pcm_prepare,
  411. .hw_params = wm8731_hw_params,
  412. .shutdown = wm8731_shutdown,
  413. },
  414. .dai_ops = {
  415. .digital_mute = wm8731_mute,
  416. .set_sysclk = wm8731_set_dai_sysclk,
  417. .set_fmt = wm8731_set_dai_fmt,
  418. }
  419. };
  420. EXPORT_SYMBOL_GPL(wm8731_dai);
  421. static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
  422. {
  423. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  424. struct snd_soc_codec *codec = socdev->codec;
  425. wm8731_write(codec, WM8731_ACTIVE, 0x0);
  426. wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
  427. return 0;
  428. }
  429. static int wm8731_resume(struct platform_device *pdev)
  430. {
  431. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  432. struct snd_soc_codec *codec = socdev->codec;
  433. int i;
  434. u8 data[2];
  435. u16 *cache = codec->reg_cache;
  436. /* Sync reg_cache with the hardware */
  437. for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
  438. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  439. data[1] = cache[i] & 0x00ff;
  440. codec->hw_write(codec->control_data, data, 2);
  441. }
  442. wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  443. wm8731_set_bias_level(codec, codec->suspend_bias_level);
  444. return 0;
  445. }
  446. /*
  447. * initialise the WM8731 driver
  448. * register the mixer and dsp interfaces with the kernel
  449. */
  450. static int wm8731_init(struct snd_soc_device *socdev)
  451. {
  452. struct snd_soc_codec *codec = socdev->codec;
  453. int reg, ret = 0;
  454. codec->name = "WM8731";
  455. codec->owner = THIS_MODULE;
  456. codec->read = wm8731_read_reg_cache;
  457. codec->write = wm8731_write;
  458. codec->set_bias_level = wm8731_set_bias_level;
  459. codec->dai = &wm8731_dai;
  460. codec->num_dai = 1;
  461. codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
  462. codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
  463. if (codec->reg_cache == NULL)
  464. return -ENOMEM;
  465. wm8731_reset(codec);
  466. /* register pcms */
  467. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  468. if (ret < 0) {
  469. printk(KERN_ERR "wm8731: failed to create pcms\n");
  470. goto pcm_err;
  471. }
  472. /* power on device */
  473. wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  474. /* set the update bits */
  475. reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
  476. wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
  477. reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
  478. wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
  479. reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
  480. wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
  481. reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
  482. wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
  483. wm8731_add_controls(codec);
  484. wm8731_add_widgets(codec);
  485. ret = snd_soc_register_card(socdev);
  486. if (ret < 0) {
  487. printk(KERN_ERR "wm8731: failed to register card\n");
  488. goto card_err;
  489. }
  490. return ret;
  491. card_err:
  492. snd_soc_free_pcms(socdev);
  493. snd_soc_dapm_free(socdev);
  494. pcm_err:
  495. kfree(codec->reg_cache);
  496. return ret;
  497. }
  498. static struct snd_soc_device *wm8731_socdev;
  499. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  500. /*
  501. * WM8731 2 wire address is determined by GPIO5
  502. * state during powerup.
  503. * low = 0x1a
  504. * high = 0x1b
  505. */
  506. static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
  507. /* Magic definition of all other variables and things */
  508. I2C_CLIENT_INSMOD;
  509. static struct i2c_driver wm8731_i2c_driver;
  510. static struct i2c_client client_template;
  511. /* If the i2c layer weren't so broken, we could pass this kind of data
  512. around */
  513. static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind)
  514. {
  515. struct snd_soc_device *socdev = wm8731_socdev;
  516. struct wm8731_setup_data *setup = socdev->codec_data;
  517. struct snd_soc_codec *codec = socdev->codec;
  518. struct i2c_client *i2c;
  519. int ret;
  520. if (addr != setup->i2c_address)
  521. return -ENODEV;
  522. client_template.adapter = adap;
  523. client_template.addr = addr;
  524. i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
  525. if (i2c == NULL) {
  526. kfree(codec);
  527. return -ENOMEM;
  528. }
  529. i2c_set_clientdata(i2c, codec);
  530. codec->control_data = i2c;
  531. ret = i2c_attach_client(i2c);
  532. if (ret < 0) {
  533. err("failed to attach codec at addr %x\n", addr);
  534. goto err;
  535. }
  536. ret = wm8731_init(socdev);
  537. if (ret < 0) {
  538. err("failed to initialise WM8731\n");
  539. goto err;
  540. }
  541. return ret;
  542. err:
  543. kfree(codec);
  544. kfree(i2c);
  545. return ret;
  546. }
  547. static int wm8731_i2c_detach(struct i2c_client *client)
  548. {
  549. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  550. i2c_detach_client(client);
  551. kfree(codec->reg_cache);
  552. kfree(client);
  553. return 0;
  554. }
  555. static int wm8731_i2c_attach(struct i2c_adapter *adap)
  556. {
  557. return i2c_probe(adap, &addr_data, wm8731_codec_probe);
  558. }
  559. /* corgi i2c codec control layer */
  560. static struct i2c_driver wm8731_i2c_driver = {
  561. .driver = {
  562. .name = "WM8731 I2C Codec",
  563. .owner = THIS_MODULE,
  564. },
  565. .id = I2C_DRIVERID_WM8731,
  566. .attach_adapter = wm8731_i2c_attach,
  567. .detach_client = wm8731_i2c_detach,
  568. .command = NULL,
  569. };
  570. static struct i2c_client client_template = {
  571. .name = "WM8731",
  572. .driver = &wm8731_i2c_driver,
  573. };
  574. #endif
  575. static int wm8731_probe(struct platform_device *pdev)
  576. {
  577. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  578. struct wm8731_setup_data *setup;
  579. struct snd_soc_codec *codec;
  580. struct wm8731_priv *wm8731;
  581. int ret = 0;
  582. info("WM8731 Audio Codec %s", WM8731_VERSION);
  583. setup = socdev->codec_data;
  584. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  585. if (codec == NULL)
  586. return -ENOMEM;
  587. wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
  588. if (wm8731 == NULL) {
  589. kfree(codec);
  590. return -ENOMEM;
  591. }
  592. codec->private_data = wm8731;
  593. socdev->codec = codec;
  594. mutex_init(&codec->mutex);
  595. INIT_LIST_HEAD(&codec->dapm_widgets);
  596. INIT_LIST_HEAD(&codec->dapm_paths);
  597. wm8731_socdev = socdev;
  598. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  599. if (setup->i2c_address) {
  600. normal_i2c[0] = setup->i2c_address;
  601. codec->hw_write = (hw_write_t)i2c_master_send;
  602. ret = i2c_add_driver(&wm8731_i2c_driver);
  603. if (ret != 0)
  604. printk(KERN_ERR "can't add i2c driver");
  605. }
  606. #else
  607. /* Add other interfaces here */
  608. #endif
  609. return ret;
  610. }
  611. /* power down chip */
  612. static int wm8731_remove(struct platform_device *pdev)
  613. {
  614. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  615. struct snd_soc_codec *codec = socdev->codec;
  616. if (codec->control_data)
  617. wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
  618. snd_soc_free_pcms(socdev);
  619. snd_soc_dapm_free(socdev);
  620. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  621. i2c_del_driver(&wm8731_i2c_driver);
  622. #endif
  623. kfree(codec->private_data);
  624. kfree(codec);
  625. return 0;
  626. }
  627. struct snd_soc_codec_device soc_codec_dev_wm8731 = {
  628. .probe = wm8731_probe,
  629. .remove = wm8731_remove,
  630. .suspend = wm8731_suspend,
  631. .resume = wm8731_resume,
  632. };
  633. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
  634. MODULE_DESCRIPTION("ASoC WM8731 driver");
  635. MODULE_AUTHOR("Richard Purdie");
  636. MODULE_LICENSE("GPL");