wm8731.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 char *intercon[][3] = {
  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. /* terminator */
  184. {NULL, NULL, NULL},
  185. };
  186. static int wm8731_add_widgets(struct snd_soc_codec *codec)
  187. {
  188. int i;
  189. for (i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++)
  190. snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
  191. /* set up audio path interconnects */
  192. for (i = 0; intercon[i][0] != NULL; i++)
  193. snd_soc_dapm_connect_input(codec, intercon[i][0],
  194. intercon[i][1], intercon[i][2]);
  195. snd_soc_dapm_new_widgets(codec);
  196. return 0;
  197. }
  198. struct _coeff_div {
  199. u32 mclk;
  200. u32 rate;
  201. u16 fs;
  202. u8 sr:4;
  203. u8 bosr:1;
  204. u8 usb:1;
  205. };
  206. /* codec mclk clock divider coefficients */
  207. static const struct _coeff_div coeff_div[] = {
  208. /* 48k */
  209. {12288000, 48000, 256, 0x0, 0x0, 0x0},
  210. {18432000, 48000, 384, 0x0, 0x1, 0x0},
  211. {12000000, 48000, 250, 0x0, 0x0, 0x1},
  212. /* 32k */
  213. {12288000, 32000, 384, 0x6, 0x0, 0x0},
  214. {18432000, 32000, 576, 0x6, 0x1, 0x0},
  215. {12000000, 32000, 375, 0x6, 0x0, 0x1},
  216. /* 8k */
  217. {12288000, 8000, 1536, 0x3, 0x0, 0x0},
  218. {18432000, 8000, 2304, 0x3, 0x1, 0x0},
  219. {11289600, 8000, 1408, 0xb, 0x0, 0x0},
  220. {16934400, 8000, 2112, 0xb, 0x1, 0x0},
  221. {12000000, 8000, 1500, 0x3, 0x0, 0x1},
  222. /* 96k */
  223. {12288000, 96000, 128, 0x7, 0x0, 0x0},
  224. {18432000, 96000, 192, 0x7, 0x1, 0x0},
  225. {12000000, 96000, 125, 0x7, 0x0, 0x1},
  226. /* 44.1k */
  227. {11289600, 44100, 256, 0x8, 0x0, 0x0},
  228. {16934400, 44100, 384, 0x8, 0x1, 0x0},
  229. {12000000, 44100, 272, 0x8, 0x1, 0x1},
  230. /* 88.2k */
  231. {11289600, 88200, 128, 0xf, 0x0, 0x0},
  232. {16934400, 88200, 192, 0xf, 0x1, 0x0},
  233. {12000000, 88200, 136, 0xf, 0x1, 0x1},
  234. };
  235. static inline int get_coeff(int mclk, int rate)
  236. {
  237. int i;
  238. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  239. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  240. return i;
  241. }
  242. return 0;
  243. }
  244. static int wm8731_hw_params(struct snd_pcm_substream *substream,
  245. struct snd_pcm_hw_params *params)
  246. {
  247. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  248. struct snd_soc_device *socdev = rtd->socdev;
  249. struct snd_soc_codec *codec = socdev->codec;
  250. struct wm8731_priv *wm8731 = codec->private_data;
  251. u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
  252. int i = get_coeff(wm8731->sysclk, params_rate(params));
  253. u16 srate = (coeff_div[i].sr << 2) |
  254. (coeff_div[i].bosr << 1) | coeff_div[i].usb;
  255. wm8731_write(codec, WM8731_SRATE, srate);
  256. /* bit size */
  257. switch (params_format(params)) {
  258. case SNDRV_PCM_FORMAT_S16_LE:
  259. break;
  260. case SNDRV_PCM_FORMAT_S20_3LE:
  261. iface |= 0x0004;
  262. break;
  263. case SNDRV_PCM_FORMAT_S24_LE:
  264. iface |= 0x0008;
  265. break;
  266. }
  267. wm8731_write(codec, WM8731_IFACE, iface);
  268. return 0;
  269. }
  270. static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
  271. {
  272. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  273. struct snd_soc_device *socdev = rtd->socdev;
  274. struct snd_soc_codec *codec = socdev->codec;
  275. /* set active */
  276. wm8731_write(codec, WM8731_ACTIVE, 0x0001);
  277. return 0;
  278. }
  279. static void wm8731_shutdown(struct snd_pcm_substream *substream)
  280. {
  281. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  282. struct snd_soc_device *socdev = rtd->socdev;
  283. struct snd_soc_codec *codec = socdev->codec;
  284. /* deactivate */
  285. if (!codec->active) {
  286. udelay(50);
  287. wm8731_write(codec, WM8731_ACTIVE, 0x0);
  288. }
  289. }
  290. static int wm8731_mute(struct snd_soc_codec_dai *dai, int mute)
  291. {
  292. struct snd_soc_codec *codec = dai->codec;
  293. u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
  294. if (mute)
  295. wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
  296. else
  297. wm8731_write(codec, WM8731_APDIGI, mute_reg);
  298. return 0;
  299. }
  300. static int wm8731_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
  301. int clk_id, unsigned int freq, int dir)
  302. {
  303. struct snd_soc_codec *codec = codec_dai->codec;
  304. struct wm8731_priv *wm8731 = codec->private_data;
  305. switch (freq) {
  306. case 11289600:
  307. case 12000000:
  308. case 12288000:
  309. case 16934400:
  310. case 18432000:
  311. wm8731->sysclk = freq;
  312. return 0;
  313. }
  314. return -EINVAL;
  315. }
  316. static int wm8731_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
  317. unsigned int fmt)
  318. {
  319. struct snd_soc_codec *codec = codec_dai->codec;
  320. u16 iface = 0;
  321. /* set master/slave audio interface */
  322. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  323. case SND_SOC_DAIFMT_CBM_CFM:
  324. iface |= 0x0040;
  325. break;
  326. case SND_SOC_DAIFMT_CBS_CFS:
  327. break;
  328. default:
  329. return -EINVAL;
  330. }
  331. /* interface format */
  332. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  333. case SND_SOC_DAIFMT_I2S:
  334. iface |= 0x0002;
  335. break;
  336. case SND_SOC_DAIFMT_RIGHT_J:
  337. break;
  338. case SND_SOC_DAIFMT_LEFT_J:
  339. iface |= 0x0001;
  340. break;
  341. case SND_SOC_DAIFMT_DSP_A:
  342. iface |= 0x0003;
  343. break;
  344. case SND_SOC_DAIFMT_DSP_B:
  345. iface |= 0x0013;
  346. break;
  347. default:
  348. return -EINVAL;
  349. }
  350. /* clock inversion */
  351. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  352. case SND_SOC_DAIFMT_NB_NF:
  353. break;
  354. case SND_SOC_DAIFMT_IB_IF:
  355. iface |= 0x0090;
  356. break;
  357. case SND_SOC_DAIFMT_IB_NF:
  358. iface |= 0x0080;
  359. break;
  360. case SND_SOC_DAIFMT_NB_IF:
  361. iface |= 0x0010;
  362. break;
  363. default:
  364. return -EINVAL;
  365. }
  366. /* set iface */
  367. wm8731_write(codec, WM8731_IFACE, iface);
  368. return 0;
  369. }
  370. static int wm8731_dapm_event(struct snd_soc_codec *codec, int event)
  371. {
  372. u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
  373. switch (event) {
  374. case SNDRV_CTL_POWER_D0: /* full On */
  375. /* vref/mid, osc on, dac unmute */
  376. wm8731_write(codec, WM8731_PWR, reg);
  377. break;
  378. case SNDRV_CTL_POWER_D1: /* partial On */
  379. case SNDRV_CTL_POWER_D2: /* partial On */
  380. break;
  381. case SNDRV_CTL_POWER_D3hot: /* Off, with power */
  382. /* everything off except vref/vmid, */
  383. wm8731_write(codec, WM8731_PWR, reg | 0x0040);
  384. break;
  385. case SNDRV_CTL_POWER_D3cold: /* Off, without power */
  386. /* everything off, dac mute, inactive */
  387. wm8731_write(codec, WM8731_ACTIVE, 0x0);
  388. wm8731_write(codec, WM8731_PWR, 0xffff);
  389. break;
  390. }
  391. codec->dapm_state = event;
  392. return 0;
  393. }
  394. #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  395. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  396. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  397. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
  398. SNDRV_PCM_RATE_96000)
  399. #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  400. SNDRV_PCM_FMTBIT_S24_LE)
  401. struct snd_soc_codec_dai wm8731_dai = {
  402. .name = "WM8731",
  403. .playback = {
  404. .stream_name = "Playback",
  405. .channels_min = 1,
  406. .channels_max = 2,
  407. .rates = WM8731_RATES,
  408. .formats = WM8731_FORMATS,},
  409. .capture = {
  410. .stream_name = "Capture",
  411. .channels_min = 1,
  412. .channels_max = 2,
  413. .rates = WM8731_RATES,
  414. .formats = WM8731_FORMATS,},
  415. .ops = {
  416. .prepare = wm8731_pcm_prepare,
  417. .hw_params = wm8731_hw_params,
  418. .shutdown = wm8731_shutdown,
  419. },
  420. .dai_ops = {
  421. .digital_mute = wm8731_mute,
  422. .set_sysclk = wm8731_set_dai_sysclk,
  423. .set_fmt = wm8731_set_dai_fmt,
  424. }
  425. };
  426. EXPORT_SYMBOL_GPL(wm8731_dai);
  427. static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
  428. {
  429. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  430. struct snd_soc_codec *codec = socdev->codec;
  431. wm8731_write(codec, WM8731_ACTIVE, 0x0);
  432. wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
  433. return 0;
  434. }
  435. static int wm8731_resume(struct platform_device *pdev)
  436. {
  437. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  438. struct snd_soc_codec *codec = socdev->codec;
  439. int i;
  440. u8 data[2];
  441. u16 *cache = codec->reg_cache;
  442. /* Sync reg_cache with the hardware */
  443. for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
  444. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  445. data[1] = cache[i] & 0x00ff;
  446. codec->hw_write(codec->control_data, data, 2);
  447. }
  448. wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
  449. wm8731_dapm_event(codec, codec->suspend_dapm_state);
  450. return 0;
  451. }
  452. /*
  453. * initialise the WM8731 driver
  454. * register the mixer and dsp interfaces with the kernel
  455. */
  456. static int wm8731_init(struct snd_soc_device *socdev)
  457. {
  458. struct snd_soc_codec *codec = socdev->codec;
  459. int reg, ret = 0;
  460. codec->name = "WM8731";
  461. codec->owner = THIS_MODULE;
  462. codec->read = wm8731_read_reg_cache;
  463. codec->write = wm8731_write;
  464. codec->dapm_event = wm8731_dapm_event;
  465. codec->dai = &wm8731_dai;
  466. codec->num_dai = 1;
  467. codec->reg_cache_size = sizeof(wm8731_reg);
  468. codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
  469. if (codec->reg_cache == NULL)
  470. return -ENOMEM;
  471. wm8731_reset(codec);
  472. /* register pcms */
  473. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  474. if (ret < 0) {
  475. printk(KERN_ERR "wm8731: failed to create pcms\n");
  476. goto pcm_err;
  477. }
  478. /* power on device */
  479. wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
  480. /* set the update bits */
  481. reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
  482. wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
  483. reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
  484. wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
  485. reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
  486. wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
  487. reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
  488. wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
  489. wm8731_add_controls(codec);
  490. wm8731_add_widgets(codec);
  491. ret = snd_soc_register_card(socdev);
  492. if (ret < 0) {
  493. printk(KERN_ERR "wm8731: failed to register card\n");
  494. goto card_err;
  495. }
  496. return ret;
  497. card_err:
  498. snd_soc_free_pcms(socdev);
  499. snd_soc_dapm_free(socdev);
  500. pcm_err:
  501. kfree(codec->reg_cache);
  502. return ret;
  503. }
  504. static struct snd_soc_device *wm8731_socdev;
  505. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  506. /*
  507. * WM8731 2 wire address is determined by GPIO5
  508. * state during powerup.
  509. * low = 0x1a
  510. * high = 0x1b
  511. */
  512. static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
  513. /* Magic definition of all other variables and things */
  514. I2C_CLIENT_INSMOD;
  515. static struct i2c_driver wm8731_i2c_driver;
  516. static struct i2c_client client_template;
  517. /* If the i2c layer weren't so broken, we could pass this kind of data
  518. around */
  519. static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind)
  520. {
  521. struct snd_soc_device *socdev = wm8731_socdev;
  522. struct wm8731_setup_data *setup = socdev->codec_data;
  523. struct snd_soc_codec *codec = socdev->codec;
  524. struct i2c_client *i2c;
  525. int ret;
  526. if (addr != setup->i2c_address)
  527. return -ENODEV;
  528. client_template.adapter = adap;
  529. client_template.addr = addr;
  530. i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
  531. if (i2c == NULL) {
  532. kfree(codec);
  533. return -ENOMEM;
  534. }
  535. i2c_set_clientdata(i2c, codec);
  536. codec->control_data = i2c;
  537. ret = i2c_attach_client(i2c);
  538. if (ret < 0) {
  539. err("failed to attach codec at addr %x\n", addr);
  540. goto err;
  541. }
  542. ret = wm8731_init(socdev);
  543. if (ret < 0) {
  544. err("failed to initialise WM8731\n");
  545. goto err;
  546. }
  547. return ret;
  548. err:
  549. kfree(codec);
  550. kfree(i2c);
  551. return ret;
  552. }
  553. static int wm8731_i2c_detach(struct i2c_client *client)
  554. {
  555. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  556. i2c_detach_client(client);
  557. kfree(codec->reg_cache);
  558. kfree(client);
  559. return 0;
  560. }
  561. static int wm8731_i2c_attach(struct i2c_adapter *adap)
  562. {
  563. return i2c_probe(adap, &addr_data, wm8731_codec_probe);
  564. }
  565. /* corgi i2c codec control layer */
  566. static struct i2c_driver wm8731_i2c_driver = {
  567. .driver = {
  568. .name = "WM8731 I2C Codec",
  569. .owner = THIS_MODULE,
  570. },
  571. .id = I2C_DRIVERID_WM8731,
  572. .attach_adapter = wm8731_i2c_attach,
  573. .detach_client = wm8731_i2c_detach,
  574. .command = NULL,
  575. };
  576. static struct i2c_client client_template = {
  577. .name = "WM8731",
  578. .driver = &wm8731_i2c_driver,
  579. };
  580. #endif
  581. static int wm8731_probe(struct platform_device *pdev)
  582. {
  583. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  584. struct wm8731_setup_data *setup;
  585. struct snd_soc_codec *codec;
  586. struct wm8731_priv *wm8731;
  587. int ret = 0;
  588. info("WM8731 Audio Codec %s", WM8731_VERSION);
  589. setup = socdev->codec_data;
  590. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  591. if (codec == NULL)
  592. return -ENOMEM;
  593. wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
  594. if (wm8731 == NULL) {
  595. kfree(codec);
  596. return -ENOMEM;
  597. }
  598. codec->private_data = wm8731;
  599. socdev->codec = codec;
  600. mutex_init(&codec->mutex);
  601. INIT_LIST_HEAD(&codec->dapm_widgets);
  602. INIT_LIST_HEAD(&codec->dapm_paths);
  603. wm8731_socdev = socdev;
  604. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  605. if (setup->i2c_address) {
  606. normal_i2c[0] = setup->i2c_address;
  607. codec->hw_write = (hw_write_t)i2c_master_send;
  608. ret = i2c_add_driver(&wm8731_i2c_driver);
  609. if (ret != 0)
  610. printk(KERN_ERR "can't add i2c driver");
  611. }
  612. #else
  613. /* Add other interfaces here */
  614. #endif
  615. return ret;
  616. }
  617. /* power down chip */
  618. static int wm8731_remove(struct platform_device *pdev)
  619. {
  620. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  621. struct snd_soc_codec *codec = socdev->codec;
  622. if (codec->control_data)
  623. wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
  624. snd_soc_free_pcms(socdev);
  625. snd_soc_dapm_free(socdev);
  626. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  627. i2c_del_driver(&wm8731_i2c_driver);
  628. #endif
  629. kfree(codec->private_data);
  630. kfree(codec);
  631. return 0;
  632. }
  633. struct snd_soc_codec_device soc_codec_dev_wm8731 = {
  634. .probe = wm8731_probe,
  635. .remove = wm8731_remove,
  636. .suspend = wm8731_suspend,
  637. .resume = wm8731_resume,
  638. };
  639. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
  640. MODULE_DESCRIPTION("ASoC WM8731 driver");
  641. MODULE_AUTHOR("Richard Purdie");
  642. MODULE_LICENSE("GPL");