wm9705.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * wm9705.c -- ALSA Soc WM9705 codec support
  4. *
  5. * Copyright 2008 Ian Molton <[email protected]>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/mfd/wm97xx.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/device.h>
  13. #include <linux/regmap.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm.h>
  16. #include <sound/ac97_codec.h>
  17. #include <sound/ac97/codec.h>
  18. #include <sound/ac97/compat.h>
  19. #include <sound/initval.h>
  20. #include <sound/soc.h>
  21. #define WM9705_VENDOR_ID 0x574d4c05
  22. #define WM9705_VENDOR_ID_MASK 0xffffffff
  23. struct wm9705_priv {
  24. struct snd_ac97 *ac97;
  25. struct wm97xx_platform_data *mfd_pdata;
  26. };
  27. static const struct reg_default wm9705_reg_defaults[] = {
  28. { 0x02, 0x8000 },
  29. { 0x04, 0x8000 },
  30. { 0x06, 0x8000 },
  31. { 0x0a, 0x8000 },
  32. { 0x0c, 0x8008 },
  33. { 0x0e, 0x8008 },
  34. { 0x10, 0x8808 },
  35. { 0x12, 0x8808 },
  36. { 0x14, 0x8808 },
  37. { 0x16, 0x8808 },
  38. { 0x18, 0x8808 },
  39. { 0x1a, 0x0000 },
  40. { 0x1c, 0x8000 },
  41. { 0x20, 0x0000 },
  42. { 0x22, 0x0000 },
  43. { 0x26, 0x000f },
  44. { 0x28, 0x0605 },
  45. { 0x2a, 0x0000 },
  46. { 0x2c, 0xbb80 },
  47. { 0x32, 0xbb80 },
  48. { 0x34, 0x2000 },
  49. { 0x5a, 0x0000 },
  50. { 0x5c, 0x0000 },
  51. { 0x72, 0x0808 },
  52. { 0x74, 0x0000 },
  53. { 0x76, 0x0006 },
  54. { 0x78, 0x0000 },
  55. { 0x7a, 0x0000 },
  56. };
  57. static const struct regmap_config wm9705_regmap_config = {
  58. .reg_bits = 16,
  59. .reg_stride = 2,
  60. .val_bits = 16,
  61. .max_register = 0x7e,
  62. .cache_type = REGCACHE_RBTREE,
  63. .volatile_reg = regmap_ac97_default_volatile,
  64. .reg_defaults = wm9705_reg_defaults,
  65. .num_reg_defaults = ARRAY_SIZE(wm9705_reg_defaults),
  66. };
  67. static const struct snd_kcontrol_new wm9705_snd_ac97_controls[] = {
  68. SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
  69. SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
  70. SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
  71. SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
  72. SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
  73. SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
  74. SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
  75. SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
  76. SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP, 1, 15, 1),
  77. SOC_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 31, 1),
  78. SOC_DOUBLE("Line Playback Volume", AC97_LINE, 8, 0, 31, 1),
  79. SOC_DOUBLE("CD Playback Volume", AC97_CD, 8, 0, 31, 1),
  80. SOC_SINGLE("Mic Playback Volume", AC97_MIC, 0, 31, 1),
  81. SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 6, 1, 0),
  82. SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0),
  83. SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1),
  84. };
  85. static const char *wm9705_mic[] = {"Mic 1", "Mic 2"};
  86. static const char *wm9705_rec_sel[] = {"Mic", "CD", "NC", "NC",
  87. "Line", "Stereo Mix", "Mono Mix", "Phone"};
  88. static SOC_ENUM_SINGLE_DECL(wm9705_enum_mic,
  89. AC97_GENERAL_PURPOSE, 8, wm9705_mic);
  90. static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_l,
  91. AC97_REC_SEL, 8, wm9705_rec_sel);
  92. static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_r,
  93. AC97_REC_SEL, 0, wm9705_rec_sel);
  94. /* Headphone Mixer */
  95. static const struct snd_kcontrol_new wm9705_hp_mixer_controls[] = {
  96. SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP, 15, 1, 1),
  97. SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD, 15, 1, 1),
  98. SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1),
  99. SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1),
  100. SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE, 15, 1, 1),
  101. };
  102. /* Mic source */
  103. static const struct snd_kcontrol_new wm9705_mic_src_controls =
  104. SOC_DAPM_ENUM("Route", wm9705_enum_mic);
  105. /* Capture source */
  106. static const struct snd_kcontrol_new wm9705_capture_selectl_controls =
  107. SOC_DAPM_ENUM("Route", wm9705_enum_rec_l);
  108. static const struct snd_kcontrol_new wm9705_capture_selectr_controls =
  109. SOC_DAPM_ENUM("Route", wm9705_enum_rec_r);
  110. /* DAPM widgets */
  111. static const struct snd_soc_dapm_widget wm9705_dapm_widgets[] = {
  112. SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM, 0, 0,
  113. &wm9705_mic_src_controls),
  114. SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
  115. &wm9705_capture_selectl_controls),
  116. SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
  117. &wm9705_capture_selectr_controls),
  118. SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
  119. SND_SOC_NOPM, 0, 0),
  120. SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
  121. SND_SOC_NOPM, 0, 0),
  122. SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM, 0, 0,
  123. &wm9705_hp_mixer_controls[0],
  124. ARRAY_SIZE(wm9705_hp_mixer_controls)),
  125. SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  126. SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
  127. SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
  128. SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  129. SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  130. SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  131. SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  132. SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  133. SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  134. SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  135. SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  136. SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  137. SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  138. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  139. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  140. SND_SOC_DAPM_OUTPUT("LOUT"),
  141. SND_SOC_DAPM_OUTPUT("ROUT"),
  142. SND_SOC_DAPM_OUTPUT("MONOOUT"),
  143. SND_SOC_DAPM_INPUT("PHONE"),
  144. SND_SOC_DAPM_INPUT("LINEINL"),
  145. SND_SOC_DAPM_INPUT("LINEINR"),
  146. SND_SOC_DAPM_INPUT("CDINL"),
  147. SND_SOC_DAPM_INPUT("CDINR"),
  148. SND_SOC_DAPM_INPUT("PCBEEP"),
  149. SND_SOC_DAPM_INPUT("MIC1"),
  150. SND_SOC_DAPM_INPUT("MIC2"),
  151. };
  152. /* Audio map
  153. * WM9705 has no switches to disable the route from the inputs to the HP mixer
  154. * so in order to prevent active inputs from forcing the audio outputs to be
  155. * constantly enabled, we use the mutes on those inputs to simulate such
  156. * controls.
  157. */
  158. static const struct snd_soc_dapm_route wm9705_audio_map[] = {
  159. /* HP mixer */
  160. {"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"},
  161. {"HP Mixer", "CD Playback Switch", "CD PGA"},
  162. {"HP Mixer", "Mic Playback Switch", "Mic PGA"},
  163. {"HP Mixer", "Phone Playback Switch", "Phone PGA"},
  164. {"HP Mixer", "Line Playback Switch", "Line PGA"},
  165. {"HP Mixer", NULL, "Left DAC"},
  166. {"HP Mixer", NULL, "Right DAC"},
  167. /* mono mixer */
  168. {"Mono Mixer", NULL, "HP Mixer"},
  169. /* outputs */
  170. {"Headphone PGA", NULL, "HP Mixer"},
  171. {"HPOUTL", NULL, "Headphone PGA"},
  172. {"HPOUTR", NULL, "Headphone PGA"},
  173. {"Line out PGA", NULL, "HP Mixer"},
  174. {"LOUT", NULL, "Line out PGA"},
  175. {"ROUT", NULL, "Line out PGA"},
  176. {"Mono PGA", NULL, "Mono Mixer"},
  177. {"MONOOUT", NULL, "Mono PGA"},
  178. /* inputs */
  179. {"CD PGA", NULL, "CDINL"},
  180. {"CD PGA", NULL, "CDINR"},
  181. {"Line PGA", NULL, "LINEINL"},
  182. {"Line PGA", NULL, "LINEINR"},
  183. {"Phone PGA", NULL, "PHONE"},
  184. {"Mic Source", "Mic 1", "MIC1"},
  185. {"Mic Source", "Mic 2", "MIC2"},
  186. {"Mic PGA", NULL, "Mic Source"},
  187. {"PCBEEP PGA", NULL, "PCBEEP"},
  188. /* Left capture selector */
  189. {"Left Capture Source", "Mic", "Mic Source"},
  190. {"Left Capture Source", "CD", "CDINL"},
  191. {"Left Capture Source", "Line", "LINEINL"},
  192. {"Left Capture Source", "Stereo Mix", "HP Mixer"},
  193. {"Left Capture Source", "Mono Mix", "HP Mixer"},
  194. {"Left Capture Source", "Phone", "PHONE"},
  195. /* Right capture source */
  196. {"Right Capture Source", "Mic", "Mic Source"},
  197. {"Right Capture Source", "CD", "CDINR"},
  198. {"Right Capture Source", "Line", "LINEINR"},
  199. {"Right Capture Source", "Stereo Mix", "HP Mixer"},
  200. {"Right Capture Source", "Mono Mix", "HP Mixer"},
  201. {"Right Capture Source", "Phone", "PHONE"},
  202. {"ADC PGA", NULL, "Left Capture Source"},
  203. {"ADC PGA", NULL, "Right Capture Source"},
  204. /* ADC's */
  205. {"Left ADC", NULL, "ADC PGA"},
  206. {"Right ADC", NULL, "ADC PGA"},
  207. };
  208. static int ac97_prepare(struct snd_pcm_substream *substream,
  209. struct snd_soc_dai *dai)
  210. {
  211. struct snd_soc_component *component = dai->component;
  212. int reg;
  213. snd_soc_component_update_bits(component, AC97_EXTENDED_STATUS, 0x1, 0x1);
  214. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  215. reg = AC97_PCM_FRONT_DAC_RATE;
  216. else
  217. reg = AC97_PCM_LR_ADC_RATE;
  218. return snd_soc_component_write(component, reg, substream->runtime->rate);
  219. }
  220. #define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
  221. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  222. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  223. SNDRV_PCM_RATE_48000)
  224. static const struct snd_soc_dai_ops wm9705_dai_ops = {
  225. .prepare = ac97_prepare,
  226. };
  227. static struct snd_soc_dai_driver wm9705_dai[] = {
  228. {
  229. .name = "wm9705-hifi",
  230. .playback = {
  231. .stream_name = "HiFi Playback",
  232. .channels_min = 1,
  233. .channels_max = 2,
  234. .rates = WM9705_AC97_RATES,
  235. .formats = SND_SOC_STD_AC97_FMTS,
  236. },
  237. .capture = {
  238. .stream_name = "HiFi Capture",
  239. .channels_min = 1,
  240. .channels_max = 2,
  241. .rates = WM9705_AC97_RATES,
  242. .formats = SND_SOC_STD_AC97_FMTS,
  243. },
  244. .ops = &wm9705_dai_ops,
  245. },
  246. {
  247. .name = "wm9705-aux",
  248. .playback = {
  249. .stream_name = "Aux Playback",
  250. .channels_min = 1,
  251. .channels_max = 1,
  252. .rates = WM9705_AC97_RATES,
  253. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  254. },
  255. }
  256. };
  257. #ifdef CONFIG_PM
  258. static int wm9705_soc_suspend(struct snd_soc_component *component)
  259. {
  260. regcache_cache_bypass(component->regmap, true);
  261. snd_soc_component_write(component, AC97_POWERDOWN, 0xffff);
  262. regcache_cache_bypass(component->regmap, false);
  263. return 0;
  264. }
  265. static int wm9705_soc_resume(struct snd_soc_component *component)
  266. {
  267. struct wm9705_priv *wm9705 = snd_soc_component_get_drvdata(component);
  268. int ret;
  269. ret = snd_ac97_reset(wm9705->ac97, true, WM9705_VENDOR_ID,
  270. WM9705_VENDOR_ID_MASK);
  271. if (ret < 0)
  272. return ret;
  273. snd_soc_component_cache_sync(component);
  274. return 0;
  275. }
  276. #else
  277. #define wm9705_soc_suspend NULL
  278. #define wm9705_soc_resume NULL
  279. #endif
  280. static int wm9705_soc_probe(struct snd_soc_component *component)
  281. {
  282. struct wm9705_priv *wm9705 = snd_soc_component_get_drvdata(component);
  283. struct regmap *regmap;
  284. if (wm9705->mfd_pdata) {
  285. wm9705->ac97 = wm9705->mfd_pdata->ac97;
  286. regmap = wm9705->mfd_pdata->regmap;
  287. } else if (IS_ENABLED(CONFIG_SND_SOC_AC97_BUS)) {
  288. wm9705->ac97 = snd_soc_new_ac97_component(component, WM9705_VENDOR_ID,
  289. WM9705_VENDOR_ID_MASK);
  290. if (IS_ERR(wm9705->ac97)) {
  291. dev_err(component->dev, "Failed to register AC97 codec\n");
  292. return PTR_ERR(wm9705->ac97);
  293. }
  294. regmap = regmap_init_ac97(wm9705->ac97, &wm9705_regmap_config);
  295. if (IS_ERR(regmap)) {
  296. snd_soc_free_ac97_component(wm9705->ac97);
  297. return PTR_ERR(regmap);
  298. }
  299. } else {
  300. return -ENXIO;
  301. }
  302. snd_soc_component_set_drvdata(component, wm9705->ac97);
  303. snd_soc_component_init_regmap(component, regmap);
  304. return 0;
  305. }
  306. static void wm9705_soc_remove(struct snd_soc_component *component)
  307. {
  308. struct wm9705_priv *wm9705 = snd_soc_component_get_drvdata(component);
  309. if (IS_ENABLED(CONFIG_SND_SOC_AC97_BUS) && !wm9705->mfd_pdata) {
  310. snd_soc_component_exit_regmap(component);
  311. snd_soc_free_ac97_component(wm9705->ac97);
  312. }
  313. }
  314. static const struct snd_soc_component_driver soc_component_dev_wm9705 = {
  315. .probe = wm9705_soc_probe,
  316. .remove = wm9705_soc_remove,
  317. .suspend = wm9705_soc_suspend,
  318. .resume = wm9705_soc_resume,
  319. .controls = wm9705_snd_ac97_controls,
  320. .num_controls = ARRAY_SIZE(wm9705_snd_ac97_controls),
  321. .dapm_widgets = wm9705_dapm_widgets,
  322. .num_dapm_widgets = ARRAY_SIZE(wm9705_dapm_widgets),
  323. .dapm_routes = wm9705_audio_map,
  324. .num_dapm_routes = ARRAY_SIZE(wm9705_audio_map),
  325. .idle_bias_on = 1,
  326. .use_pmdown_time = 1,
  327. .endianness = 1,
  328. };
  329. static int wm9705_probe(struct platform_device *pdev)
  330. {
  331. struct wm9705_priv *wm9705;
  332. wm9705 = devm_kzalloc(&pdev->dev, sizeof(*wm9705), GFP_KERNEL);
  333. if (wm9705 == NULL)
  334. return -ENOMEM;
  335. wm9705->mfd_pdata = dev_get_platdata(&pdev->dev);
  336. platform_set_drvdata(pdev, wm9705);
  337. return devm_snd_soc_register_component(&pdev->dev,
  338. &soc_component_dev_wm9705, wm9705_dai, ARRAY_SIZE(wm9705_dai));
  339. }
  340. static struct platform_driver wm9705_codec_driver = {
  341. .driver = {
  342. .name = "wm9705-codec",
  343. },
  344. .probe = wm9705_probe,
  345. };
  346. module_platform_driver(wm9705_codec_driver);
  347. MODULE_DESCRIPTION("ASoC WM9705 driver");
  348. MODULE_AUTHOR("Ian Molton");
  349. MODULE_LICENSE("GPL v2");