wm8731.c 19 KB

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