wm8900.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * wm8900.c -- WM8900 ALSA Soc Audio driver
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * TODO:
  13. * - Tristating.
  14. * - TDM.
  15. * - Jack detect.
  16. * - FLL source configuration, currently only MCLK is supported.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/pm.h>
  24. #include <linux/i2c.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/slab.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/soc.h>
  31. #include <sound/initval.h>
  32. #include <sound/tlv.h>
  33. #include "wm8900.h"
  34. /* WM8900 register space */
  35. #define WM8900_REG_RESET 0x0
  36. #define WM8900_REG_ID 0x0
  37. #define WM8900_REG_POWER1 0x1
  38. #define WM8900_REG_POWER2 0x2
  39. #define WM8900_REG_POWER3 0x3
  40. #define WM8900_REG_AUDIO1 0x4
  41. #define WM8900_REG_AUDIO2 0x5
  42. #define WM8900_REG_CLOCKING1 0x6
  43. #define WM8900_REG_CLOCKING2 0x7
  44. #define WM8900_REG_AUDIO3 0x8
  45. #define WM8900_REG_AUDIO4 0x9
  46. #define WM8900_REG_DACCTRL 0xa
  47. #define WM8900_REG_LDAC_DV 0xb
  48. #define WM8900_REG_RDAC_DV 0xc
  49. #define WM8900_REG_SIDETONE 0xd
  50. #define WM8900_REG_ADCCTRL 0xe
  51. #define WM8900_REG_LADC_DV 0xf
  52. #define WM8900_REG_RADC_DV 0x10
  53. #define WM8900_REG_GPIO 0x12
  54. #define WM8900_REG_INCTL 0x15
  55. #define WM8900_REG_LINVOL 0x16
  56. #define WM8900_REG_RINVOL 0x17
  57. #define WM8900_REG_INBOOSTMIX1 0x18
  58. #define WM8900_REG_INBOOSTMIX2 0x19
  59. #define WM8900_REG_ADCPATH 0x1a
  60. #define WM8900_REG_AUXBOOST 0x1b
  61. #define WM8900_REG_ADDCTL 0x1e
  62. #define WM8900_REG_FLLCTL1 0x24
  63. #define WM8900_REG_FLLCTL2 0x25
  64. #define WM8900_REG_FLLCTL3 0x26
  65. #define WM8900_REG_FLLCTL4 0x27
  66. #define WM8900_REG_FLLCTL5 0x28
  67. #define WM8900_REG_FLLCTL6 0x29
  68. #define WM8900_REG_LOUTMIXCTL1 0x2c
  69. #define WM8900_REG_ROUTMIXCTL1 0x2d
  70. #define WM8900_REG_BYPASS1 0x2e
  71. #define WM8900_REG_BYPASS2 0x2f
  72. #define WM8900_REG_AUXOUT_CTL 0x30
  73. #define WM8900_REG_LOUT1CTL 0x33
  74. #define WM8900_REG_ROUT1CTL 0x34
  75. #define WM8900_REG_LOUT2CTL 0x35
  76. #define WM8900_REG_ROUT2CTL 0x36
  77. #define WM8900_REG_HPCTL1 0x3a
  78. #define WM8900_REG_OUTBIASCTL 0x73
  79. #define WM8900_MAXREG 0x80
  80. #define WM8900_REG_ADDCTL_OUT1_DIS 0x80
  81. #define WM8900_REG_ADDCTL_OUT2_DIS 0x40
  82. #define WM8900_REG_ADDCTL_VMID_DIS 0x20
  83. #define WM8900_REG_ADDCTL_BIAS_SRC 0x10
  84. #define WM8900_REG_ADDCTL_VMID_SOFTST 0x04
  85. #define WM8900_REG_ADDCTL_TEMP_SD 0x02
  86. #define WM8900_REG_GPIO_TEMP_ENA 0x2
  87. #define WM8900_REG_POWER1_STARTUP_BIAS_ENA 0x0100
  88. #define WM8900_REG_POWER1_BIAS_ENA 0x0008
  89. #define WM8900_REG_POWER1_VMID_BUF_ENA 0x0004
  90. #define WM8900_REG_POWER1_FLL_ENA 0x0040
  91. #define WM8900_REG_POWER2_SYSCLK_ENA 0x8000
  92. #define WM8900_REG_POWER2_ADCL_ENA 0x0002
  93. #define WM8900_REG_POWER2_ADCR_ENA 0x0001
  94. #define WM8900_REG_POWER3_DACL_ENA 0x0002
  95. #define WM8900_REG_POWER3_DACR_ENA 0x0001
  96. #define WM8900_REG_AUDIO1_AIF_FMT_MASK 0x0018
  97. #define WM8900_REG_AUDIO1_LRCLK_INV 0x0080
  98. #define WM8900_REG_AUDIO1_BCLK_INV 0x0100
  99. #define WM8900_REG_CLOCKING1_BCLK_DIR 0x1
  100. #define WM8900_REG_CLOCKING1_MCLK_SRC 0x100
  101. #define WM8900_REG_CLOCKING1_BCLK_MASK 0x01e
  102. #define WM8900_REG_CLOCKING1_OPCLK_MASK 0x7000
  103. #define WM8900_REG_CLOCKING2_ADC_CLKDIV 0xe0
  104. #define WM8900_REG_CLOCKING2_DAC_CLKDIV 0x1c
  105. #define WM8900_REG_DACCTRL_MUTE 0x004
  106. #define WM8900_REG_DACCTRL_DAC_SB_FILT 0x100
  107. #define WM8900_REG_DACCTRL_AIF_LRCLKRATE 0x400
  108. #define WM8900_REG_AUDIO3_ADCLRC_DIR 0x0800
  109. #define WM8900_REG_AUDIO4_DACLRC_DIR 0x0800
  110. #define WM8900_REG_FLLCTL1_OSC_ENA 0x100
  111. #define WM8900_REG_FLLCTL6_FLL_SLOW_LOCK_REF 0x100
  112. #define WM8900_REG_HPCTL1_HP_IPSTAGE_ENA 0x80
  113. #define WM8900_REG_HPCTL1_HP_OPSTAGE_ENA 0x40
  114. #define WM8900_REG_HPCTL1_HP_CLAMP_IP 0x20
  115. #define WM8900_REG_HPCTL1_HP_CLAMP_OP 0x10
  116. #define WM8900_REG_HPCTL1_HP_SHORT 0x08
  117. #define WM8900_REG_HPCTL1_HP_SHORT2 0x04
  118. #define WM8900_LRC_MASK 0x03ff
  119. struct wm8900_priv {
  120. enum snd_soc_control_type control_type;
  121. u32 fll_in; /* FLL input frequency */
  122. u32 fll_out; /* FLL output frequency */
  123. };
  124. /*
  125. * wm8900 register cache. We can't read the entire register space and we
  126. * have slow control buses so we cache the registers.
  127. */
  128. static const u16 wm8900_reg_defaults[WM8900_MAXREG] = {
  129. 0x8900, 0x0000,
  130. 0xc000, 0x0000,
  131. 0x4050, 0x4000,
  132. 0x0008, 0x0000,
  133. 0x0040, 0x0040,
  134. 0x1004, 0x00c0,
  135. 0x00c0, 0x0000,
  136. 0x0100, 0x00c0,
  137. 0x00c0, 0x0000,
  138. 0xb001, 0x0000,
  139. 0x0000, 0x0044,
  140. 0x004c, 0x004c,
  141. 0x0044, 0x0044,
  142. 0x0000, 0x0044,
  143. 0x0000, 0x0000,
  144. 0x0002, 0x0000,
  145. 0x0000, 0x0000,
  146. 0x0000, 0x0000,
  147. 0x0008, 0x0000,
  148. 0x0000, 0x0008,
  149. 0x0097, 0x0100,
  150. 0x0000, 0x0000,
  151. 0x0050, 0x0050,
  152. 0x0055, 0x0055,
  153. 0x0055, 0x0000,
  154. 0x0000, 0x0079,
  155. 0x0079, 0x0079,
  156. 0x0079, 0x0000,
  157. /* Remaining registers all zero */
  158. };
  159. static int wm8900_volatile_register(struct snd_soc_codec *codec, unsigned int reg)
  160. {
  161. switch (reg) {
  162. case WM8900_REG_ID:
  163. return 1;
  164. default:
  165. return 0;
  166. }
  167. }
  168. static void wm8900_reset(struct snd_soc_codec *codec)
  169. {
  170. snd_soc_write(codec, WM8900_REG_RESET, 0);
  171. memcpy(codec->reg_cache, wm8900_reg_defaults,
  172. sizeof(wm8900_reg_defaults));
  173. }
  174. static int wm8900_hp_event(struct snd_soc_dapm_widget *w,
  175. struct snd_kcontrol *kcontrol, int event)
  176. {
  177. struct snd_soc_codec *codec = w->codec;
  178. u16 hpctl1 = snd_soc_read(codec, WM8900_REG_HPCTL1);
  179. switch (event) {
  180. case SND_SOC_DAPM_PRE_PMU:
  181. /* Clamp headphone outputs */
  182. hpctl1 = WM8900_REG_HPCTL1_HP_CLAMP_IP |
  183. WM8900_REG_HPCTL1_HP_CLAMP_OP;
  184. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  185. break;
  186. case SND_SOC_DAPM_POST_PMU:
  187. /* Enable the input stage */
  188. hpctl1 &= ~WM8900_REG_HPCTL1_HP_CLAMP_IP;
  189. hpctl1 |= WM8900_REG_HPCTL1_HP_SHORT |
  190. WM8900_REG_HPCTL1_HP_SHORT2 |
  191. WM8900_REG_HPCTL1_HP_IPSTAGE_ENA;
  192. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  193. msleep(400);
  194. /* Enable the output stage */
  195. hpctl1 &= ~WM8900_REG_HPCTL1_HP_CLAMP_OP;
  196. hpctl1 |= WM8900_REG_HPCTL1_HP_OPSTAGE_ENA;
  197. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  198. /* Remove the shorts */
  199. hpctl1 &= ~WM8900_REG_HPCTL1_HP_SHORT2;
  200. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  201. hpctl1 &= ~WM8900_REG_HPCTL1_HP_SHORT;
  202. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  203. break;
  204. case SND_SOC_DAPM_PRE_PMD:
  205. /* Short the output */
  206. hpctl1 |= WM8900_REG_HPCTL1_HP_SHORT;
  207. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  208. /* Disable the output stage */
  209. hpctl1 &= ~WM8900_REG_HPCTL1_HP_OPSTAGE_ENA;
  210. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  211. /* Clamp the outputs and power down input */
  212. hpctl1 |= WM8900_REG_HPCTL1_HP_CLAMP_IP |
  213. WM8900_REG_HPCTL1_HP_CLAMP_OP;
  214. hpctl1 &= ~WM8900_REG_HPCTL1_HP_IPSTAGE_ENA;
  215. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  216. break;
  217. case SND_SOC_DAPM_POST_PMD:
  218. /* Disable everything */
  219. snd_soc_write(codec, WM8900_REG_HPCTL1, 0);
  220. break;
  221. default:
  222. BUG();
  223. }
  224. return 0;
  225. }
  226. static const DECLARE_TLV_DB_SCALE(out_pga_tlv, -5700, 100, 0);
  227. static const DECLARE_TLV_DB_SCALE(out_mix_tlv, -1500, 300, 0);
  228. static const DECLARE_TLV_DB_SCALE(in_boost_tlv, -1200, 600, 0);
  229. static const DECLARE_TLV_DB_SCALE(in_pga_tlv, -1200, 100, 0);
  230. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  231. static const DECLARE_TLV_DB_SCALE(dac_tlv, -7200, 75, 1);
  232. static const DECLARE_TLV_DB_SCALE(adc_svol_tlv, -3600, 300, 0);
  233. static const DECLARE_TLV_DB_SCALE(adc_tlv, -7200, 75, 1);
  234. static const char *mic_bias_level_txt[] = { "0.9*AVDD", "0.65*AVDD" };
  235. static const struct soc_enum mic_bias_level =
  236. SOC_ENUM_SINGLE(WM8900_REG_INCTL, 8, 2, mic_bias_level_txt);
  237. static const char *dac_mute_rate_txt[] = { "Fast", "Slow" };
  238. static const struct soc_enum dac_mute_rate =
  239. SOC_ENUM_SINGLE(WM8900_REG_DACCTRL, 7, 2, dac_mute_rate_txt);
  240. static const char *dac_deemphasis_txt[] = {
  241. "Disabled", "32kHz", "44.1kHz", "48kHz"
  242. };
  243. static const struct soc_enum dac_deemphasis =
  244. SOC_ENUM_SINGLE(WM8900_REG_DACCTRL, 4, 4, dac_deemphasis_txt);
  245. static const char *adc_hpf_cut_txt[] = {
  246. "Hi-fi mode", "Voice mode 1", "Voice mode 2", "Voice mode 3"
  247. };
  248. static const struct soc_enum adc_hpf_cut =
  249. SOC_ENUM_SINGLE(WM8900_REG_ADCCTRL, 5, 4, adc_hpf_cut_txt);
  250. static const char *lr_txt[] = {
  251. "Left", "Right"
  252. };
  253. static const struct soc_enum aifl_src =
  254. SOC_ENUM_SINGLE(WM8900_REG_AUDIO1, 15, 2, lr_txt);
  255. static const struct soc_enum aifr_src =
  256. SOC_ENUM_SINGLE(WM8900_REG_AUDIO1, 14, 2, lr_txt);
  257. static const struct soc_enum dacl_src =
  258. SOC_ENUM_SINGLE(WM8900_REG_AUDIO2, 15, 2, lr_txt);
  259. static const struct soc_enum dacr_src =
  260. SOC_ENUM_SINGLE(WM8900_REG_AUDIO2, 14, 2, lr_txt);
  261. static const char *sidetone_txt[] = {
  262. "Disabled", "Left ADC", "Right ADC"
  263. };
  264. static const struct soc_enum dacl_sidetone =
  265. SOC_ENUM_SINGLE(WM8900_REG_SIDETONE, 2, 3, sidetone_txt);
  266. static const struct soc_enum dacr_sidetone =
  267. SOC_ENUM_SINGLE(WM8900_REG_SIDETONE, 0, 3, sidetone_txt);
  268. static const struct snd_kcontrol_new wm8900_snd_controls[] = {
  269. SOC_ENUM("Mic Bias Level", mic_bias_level),
  270. SOC_SINGLE_TLV("Left Input PGA Volume", WM8900_REG_LINVOL, 0, 31, 0,
  271. in_pga_tlv),
  272. SOC_SINGLE("Left Input PGA Switch", WM8900_REG_LINVOL, 6, 1, 1),
  273. SOC_SINGLE("Left Input PGA ZC Switch", WM8900_REG_LINVOL, 7, 1, 0),
  274. SOC_SINGLE_TLV("Right Input PGA Volume", WM8900_REG_RINVOL, 0, 31, 0,
  275. in_pga_tlv),
  276. SOC_SINGLE("Right Input PGA Switch", WM8900_REG_RINVOL, 6, 1, 1),
  277. SOC_SINGLE("Right Input PGA ZC Switch", WM8900_REG_RINVOL, 7, 1, 0),
  278. SOC_SINGLE("DAC Soft Mute Switch", WM8900_REG_DACCTRL, 6, 1, 1),
  279. SOC_ENUM("DAC Mute Rate", dac_mute_rate),
  280. SOC_SINGLE("DAC Mono Switch", WM8900_REG_DACCTRL, 9, 1, 0),
  281. SOC_ENUM("DAC Deemphasis", dac_deemphasis),
  282. SOC_SINGLE("DAC Sigma-Delta Modulator Clock Switch", WM8900_REG_DACCTRL,
  283. 12, 1, 0),
  284. SOC_SINGLE("ADC HPF Switch", WM8900_REG_ADCCTRL, 8, 1, 0),
  285. SOC_ENUM("ADC HPF Cut-Off", adc_hpf_cut),
  286. SOC_DOUBLE("ADC Invert Switch", WM8900_REG_ADCCTRL, 1, 0, 1, 0),
  287. SOC_SINGLE_TLV("Left ADC Sidetone Volume", WM8900_REG_SIDETONE, 9, 12, 0,
  288. adc_svol_tlv),
  289. SOC_SINGLE_TLV("Right ADC Sidetone Volume", WM8900_REG_SIDETONE, 5, 12, 0,
  290. adc_svol_tlv),
  291. SOC_ENUM("Left Digital Audio Source", aifl_src),
  292. SOC_ENUM("Right Digital Audio Source", aifr_src),
  293. SOC_SINGLE_TLV("DAC Input Boost Volume", WM8900_REG_AUDIO2, 10, 4, 0,
  294. dac_boost_tlv),
  295. SOC_ENUM("Left DAC Source", dacl_src),
  296. SOC_ENUM("Right DAC Source", dacr_src),
  297. SOC_ENUM("Left DAC Sidetone", dacl_sidetone),
  298. SOC_ENUM("Right DAC Sidetone", dacr_sidetone),
  299. SOC_DOUBLE("DAC Invert Switch", WM8900_REG_DACCTRL, 1, 0, 1, 0),
  300. SOC_DOUBLE_R_TLV("Digital Playback Volume",
  301. WM8900_REG_LDAC_DV, WM8900_REG_RDAC_DV,
  302. 1, 96, 0, dac_tlv),
  303. SOC_DOUBLE_R_TLV("Digital Capture Volume",
  304. WM8900_REG_LADC_DV, WM8900_REG_RADC_DV, 1, 119, 0, adc_tlv),
  305. SOC_SINGLE_TLV("LINPUT3 Bypass Volume", WM8900_REG_LOUTMIXCTL1, 4, 7, 0,
  306. out_mix_tlv),
  307. SOC_SINGLE_TLV("RINPUT3 Bypass Volume", WM8900_REG_ROUTMIXCTL1, 4, 7, 0,
  308. out_mix_tlv),
  309. SOC_SINGLE_TLV("Left AUX Bypass Volume", WM8900_REG_AUXOUT_CTL, 4, 7, 0,
  310. out_mix_tlv),
  311. SOC_SINGLE_TLV("Right AUX Bypass Volume", WM8900_REG_AUXOUT_CTL, 0, 7, 0,
  312. out_mix_tlv),
  313. SOC_SINGLE_TLV("LeftIn to RightOut Mixer Volume", WM8900_REG_BYPASS1, 0, 7, 0,
  314. out_mix_tlv),
  315. SOC_SINGLE_TLV("LeftIn to LeftOut Mixer Volume", WM8900_REG_BYPASS1, 4, 7, 0,
  316. out_mix_tlv),
  317. SOC_SINGLE_TLV("RightIn to LeftOut Mixer Volume", WM8900_REG_BYPASS2, 0, 7, 0,
  318. out_mix_tlv),
  319. SOC_SINGLE_TLV("RightIn to RightOut Mixer Volume", WM8900_REG_BYPASS2, 4, 7, 0,
  320. out_mix_tlv),
  321. SOC_SINGLE_TLV("IN2L Boost Volume", WM8900_REG_INBOOSTMIX1, 0, 3, 0,
  322. in_boost_tlv),
  323. SOC_SINGLE_TLV("IN3L Boost Volume", WM8900_REG_INBOOSTMIX1, 4, 3, 0,
  324. in_boost_tlv),
  325. SOC_SINGLE_TLV("IN2R Boost Volume", WM8900_REG_INBOOSTMIX2, 0, 3, 0,
  326. in_boost_tlv),
  327. SOC_SINGLE_TLV("IN3R Boost Volume", WM8900_REG_INBOOSTMIX2, 4, 3, 0,
  328. in_boost_tlv),
  329. SOC_SINGLE_TLV("Left AUX Boost Volume", WM8900_REG_AUXBOOST, 4, 3, 0,
  330. in_boost_tlv),
  331. SOC_SINGLE_TLV("Right AUX Boost Volume", WM8900_REG_AUXBOOST, 0, 3, 0,
  332. in_boost_tlv),
  333. SOC_DOUBLE_R_TLV("LINEOUT1 Volume", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
  334. 0, 63, 0, out_pga_tlv),
  335. SOC_DOUBLE_R("LINEOUT1 Switch", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
  336. 6, 1, 1),
  337. SOC_DOUBLE_R("LINEOUT1 ZC Switch", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
  338. 7, 1, 0),
  339. SOC_DOUBLE_R_TLV("LINEOUT2 Volume",
  340. WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL,
  341. 0, 63, 0, out_pga_tlv),
  342. SOC_DOUBLE_R("LINEOUT2 Switch",
  343. WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL, 6, 1, 1),
  344. SOC_DOUBLE_R("LINEOUT2 ZC Switch",
  345. WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL, 7, 1, 0),
  346. SOC_SINGLE("LINEOUT2 LP -12dB", WM8900_REG_LOUTMIXCTL1,
  347. 0, 1, 1),
  348. };
  349. static const struct snd_kcontrol_new wm8900_dapm_loutput2_control =
  350. SOC_DAPM_SINGLE("LINEOUT2L Switch", WM8900_REG_POWER3, 6, 1, 0);
  351. static const struct snd_kcontrol_new wm8900_dapm_routput2_control =
  352. SOC_DAPM_SINGLE("LINEOUT2R Switch", WM8900_REG_POWER3, 5, 1, 0);
  353. static const struct snd_kcontrol_new wm8900_loutmix_controls[] = {
  354. SOC_DAPM_SINGLE("LINPUT3 Bypass Switch", WM8900_REG_LOUTMIXCTL1, 7, 1, 0),
  355. SOC_DAPM_SINGLE("AUX Bypass Switch", WM8900_REG_AUXOUT_CTL, 7, 1, 0),
  356. SOC_DAPM_SINGLE("Left Input Mixer Switch", WM8900_REG_BYPASS1, 7, 1, 0),
  357. SOC_DAPM_SINGLE("Right Input Mixer Switch", WM8900_REG_BYPASS2, 3, 1, 0),
  358. SOC_DAPM_SINGLE("DACL Switch", WM8900_REG_LOUTMIXCTL1, 8, 1, 0),
  359. };
  360. static const struct snd_kcontrol_new wm8900_routmix_controls[] = {
  361. SOC_DAPM_SINGLE("RINPUT3 Bypass Switch", WM8900_REG_ROUTMIXCTL1, 7, 1, 0),
  362. SOC_DAPM_SINGLE("AUX Bypass Switch", WM8900_REG_AUXOUT_CTL, 3, 1, 0),
  363. SOC_DAPM_SINGLE("Left Input Mixer Switch", WM8900_REG_BYPASS1, 3, 1, 0),
  364. SOC_DAPM_SINGLE("Right Input Mixer Switch", WM8900_REG_BYPASS2, 7, 1, 0),
  365. SOC_DAPM_SINGLE("DACR Switch", WM8900_REG_ROUTMIXCTL1, 8, 1, 0),
  366. };
  367. static const struct snd_kcontrol_new wm8900_linmix_controls[] = {
  368. SOC_DAPM_SINGLE("LINPUT2 Switch", WM8900_REG_INBOOSTMIX1, 2, 1, 1),
  369. SOC_DAPM_SINGLE("LINPUT3 Switch", WM8900_REG_INBOOSTMIX1, 6, 1, 1),
  370. SOC_DAPM_SINGLE("AUX Switch", WM8900_REG_AUXBOOST, 6, 1, 1),
  371. SOC_DAPM_SINGLE("Input PGA Switch", WM8900_REG_ADCPATH, 6, 1, 0),
  372. };
  373. static const struct snd_kcontrol_new wm8900_rinmix_controls[] = {
  374. SOC_DAPM_SINGLE("RINPUT2 Switch", WM8900_REG_INBOOSTMIX2, 2, 1, 1),
  375. SOC_DAPM_SINGLE("RINPUT3 Switch", WM8900_REG_INBOOSTMIX2, 6, 1, 1),
  376. SOC_DAPM_SINGLE("AUX Switch", WM8900_REG_AUXBOOST, 2, 1, 1),
  377. SOC_DAPM_SINGLE("Input PGA Switch", WM8900_REG_ADCPATH, 2, 1, 0),
  378. };
  379. static const struct snd_kcontrol_new wm8900_linpga_controls[] = {
  380. SOC_DAPM_SINGLE("LINPUT1 Switch", WM8900_REG_INCTL, 6, 1, 0),
  381. SOC_DAPM_SINGLE("LINPUT2 Switch", WM8900_REG_INCTL, 5, 1, 0),
  382. SOC_DAPM_SINGLE("LINPUT3 Switch", WM8900_REG_INCTL, 4, 1, 0),
  383. };
  384. static const struct snd_kcontrol_new wm8900_rinpga_controls[] = {
  385. SOC_DAPM_SINGLE("RINPUT1 Switch", WM8900_REG_INCTL, 2, 1, 0),
  386. SOC_DAPM_SINGLE("RINPUT2 Switch", WM8900_REG_INCTL, 1, 1, 0),
  387. SOC_DAPM_SINGLE("RINPUT3 Switch", WM8900_REG_INCTL, 0, 1, 0),
  388. };
  389. static const char *wm9700_lp_mux[] = { "Disabled", "Enabled" };
  390. static const struct soc_enum wm8900_lineout2_lp_mux =
  391. SOC_ENUM_SINGLE(WM8900_REG_LOUTMIXCTL1, 1, 2, wm9700_lp_mux);
  392. static const struct snd_kcontrol_new wm8900_lineout2_lp =
  393. SOC_DAPM_ENUM("Route", wm8900_lineout2_lp_mux);
  394. static const struct snd_soc_dapm_widget wm8900_dapm_widgets[] = {
  395. /* Externally visible pins */
  396. SND_SOC_DAPM_OUTPUT("LINEOUT1L"),
  397. SND_SOC_DAPM_OUTPUT("LINEOUT1R"),
  398. SND_SOC_DAPM_OUTPUT("LINEOUT2L"),
  399. SND_SOC_DAPM_OUTPUT("LINEOUT2R"),
  400. SND_SOC_DAPM_OUTPUT("HP_L"),
  401. SND_SOC_DAPM_OUTPUT("HP_R"),
  402. SND_SOC_DAPM_INPUT("RINPUT1"),
  403. SND_SOC_DAPM_INPUT("LINPUT1"),
  404. SND_SOC_DAPM_INPUT("RINPUT2"),
  405. SND_SOC_DAPM_INPUT("LINPUT2"),
  406. SND_SOC_DAPM_INPUT("RINPUT3"),
  407. SND_SOC_DAPM_INPUT("LINPUT3"),
  408. SND_SOC_DAPM_INPUT("AUX"),
  409. SND_SOC_DAPM_VMID("VMID"),
  410. /* Input */
  411. SND_SOC_DAPM_MIXER("Left Input PGA", WM8900_REG_POWER2, 3, 0,
  412. wm8900_linpga_controls,
  413. ARRAY_SIZE(wm8900_linpga_controls)),
  414. SND_SOC_DAPM_MIXER("Right Input PGA", WM8900_REG_POWER2, 2, 0,
  415. wm8900_rinpga_controls,
  416. ARRAY_SIZE(wm8900_rinpga_controls)),
  417. SND_SOC_DAPM_MIXER("Left Input Mixer", WM8900_REG_POWER2, 5, 0,
  418. wm8900_linmix_controls,
  419. ARRAY_SIZE(wm8900_linmix_controls)),
  420. SND_SOC_DAPM_MIXER("Right Input Mixer", WM8900_REG_POWER2, 4, 0,
  421. wm8900_rinmix_controls,
  422. ARRAY_SIZE(wm8900_rinmix_controls)),
  423. SND_SOC_DAPM_SUPPLY("Mic Bias", WM8900_REG_POWER1, 4, 0, NULL, 0),
  424. SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8900_REG_POWER2, 1, 0),
  425. SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8900_REG_POWER2, 0, 0),
  426. /* Output */
  427. SND_SOC_DAPM_DAC("DACL", "Left HiFi Playback", WM8900_REG_POWER3, 1, 0),
  428. SND_SOC_DAPM_DAC("DACR", "Right HiFi Playback", WM8900_REG_POWER3, 0, 0),
  429. SND_SOC_DAPM_PGA_E("Headphone Amplifier", WM8900_REG_POWER3, 7, 0, NULL, 0,
  430. wm8900_hp_event,
  431. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  432. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  433. SND_SOC_DAPM_PGA("LINEOUT1L PGA", WM8900_REG_POWER2, 8, 0, NULL, 0),
  434. SND_SOC_DAPM_PGA("LINEOUT1R PGA", WM8900_REG_POWER2, 7, 0, NULL, 0),
  435. SND_SOC_DAPM_MUX("LINEOUT2 LP", SND_SOC_NOPM, 0, 0, &wm8900_lineout2_lp),
  436. SND_SOC_DAPM_PGA("LINEOUT2L PGA", WM8900_REG_POWER3, 6, 0, NULL, 0),
  437. SND_SOC_DAPM_PGA("LINEOUT2R PGA", WM8900_REG_POWER3, 5, 0, NULL, 0),
  438. SND_SOC_DAPM_MIXER("Left Output Mixer", WM8900_REG_POWER3, 3, 0,
  439. wm8900_loutmix_controls,
  440. ARRAY_SIZE(wm8900_loutmix_controls)),
  441. SND_SOC_DAPM_MIXER("Right Output Mixer", WM8900_REG_POWER3, 2, 0,
  442. wm8900_routmix_controls,
  443. ARRAY_SIZE(wm8900_routmix_controls)),
  444. };
  445. /* Target, Path, Source */
  446. static const struct snd_soc_dapm_route wm8900_dapm_routes[] = {
  447. /* Inputs */
  448. {"Left Input PGA", "LINPUT1 Switch", "LINPUT1"},
  449. {"Left Input PGA", "LINPUT2 Switch", "LINPUT2"},
  450. {"Left Input PGA", "LINPUT3 Switch", "LINPUT3"},
  451. {"Right Input PGA", "RINPUT1 Switch", "RINPUT1"},
  452. {"Right Input PGA", "RINPUT2 Switch", "RINPUT2"},
  453. {"Right Input PGA", "RINPUT3 Switch", "RINPUT3"},
  454. {"Left Input Mixer", "LINPUT2 Switch", "LINPUT2"},
  455. {"Left Input Mixer", "LINPUT3 Switch", "LINPUT3"},
  456. {"Left Input Mixer", "AUX Switch", "AUX"},
  457. {"Left Input Mixer", "Input PGA Switch", "Left Input PGA"},
  458. {"Right Input Mixer", "RINPUT2 Switch", "RINPUT2"},
  459. {"Right Input Mixer", "RINPUT3 Switch", "RINPUT3"},
  460. {"Right Input Mixer", "AUX Switch", "AUX"},
  461. {"Right Input Mixer", "Input PGA Switch", "Right Input PGA"},
  462. {"ADCL", NULL, "Left Input Mixer"},
  463. {"ADCR", NULL, "Right Input Mixer"},
  464. /* Outputs */
  465. {"LINEOUT1L", NULL, "LINEOUT1L PGA"},
  466. {"LINEOUT1L PGA", NULL, "Left Output Mixer"},
  467. {"LINEOUT1R", NULL, "LINEOUT1R PGA"},
  468. {"LINEOUT1R PGA", NULL, "Right Output Mixer"},
  469. {"LINEOUT2L PGA", NULL, "Left Output Mixer"},
  470. {"LINEOUT2 LP", "Disabled", "LINEOUT2L PGA"},
  471. {"LINEOUT2 LP", "Enabled", "Left Output Mixer"},
  472. {"LINEOUT2L", NULL, "LINEOUT2 LP"},
  473. {"LINEOUT2R PGA", NULL, "Right Output Mixer"},
  474. {"LINEOUT2 LP", "Disabled", "LINEOUT2R PGA"},
  475. {"LINEOUT2 LP", "Enabled", "Right Output Mixer"},
  476. {"LINEOUT2R", NULL, "LINEOUT2 LP"},
  477. {"Left Output Mixer", "LINPUT3 Bypass Switch", "LINPUT3"},
  478. {"Left Output Mixer", "AUX Bypass Switch", "AUX"},
  479. {"Left Output Mixer", "Left Input Mixer Switch", "Left Input Mixer"},
  480. {"Left Output Mixer", "Right Input Mixer Switch", "Right Input Mixer"},
  481. {"Left Output Mixer", "DACL Switch", "DACL"},
  482. {"Right Output Mixer", "RINPUT3 Bypass Switch", "RINPUT3"},
  483. {"Right Output Mixer", "AUX Bypass Switch", "AUX"},
  484. {"Right Output Mixer", "Left Input Mixer Switch", "Left Input Mixer"},
  485. {"Right Output Mixer", "Right Input Mixer Switch", "Right Input Mixer"},
  486. {"Right Output Mixer", "DACR Switch", "DACR"},
  487. /* Note that the headphone output stage needs to be connected
  488. * externally to LINEOUT2 via DC blocking capacitors. Other
  489. * configurations are not supported.
  490. *
  491. * Note also that left and right headphone paths are treated as a
  492. * mono path.
  493. */
  494. {"Headphone Amplifier", NULL, "LINEOUT2 LP"},
  495. {"Headphone Amplifier", NULL, "LINEOUT2 LP"},
  496. {"HP_L", NULL, "Headphone Amplifier"},
  497. {"HP_R", NULL, "Headphone Amplifier"},
  498. };
  499. static int wm8900_hw_params(struct snd_pcm_substream *substream,
  500. struct snd_pcm_hw_params *params,
  501. struct snd_soc_dai *dai)
  502. {
  503. struct snd_soc_codec *codec = dai->codec;
  504. u16 reg;
  505. reg = snd_soc_read(codec, WM8900_REG_AUDIO1) & ~0x60;
  506. switch (params_format(params)) {
  507. case SNDRV_PCM_FORMAT_S16_LE:
  508. break;
  509. case SNDRV_PCM_FORMAT_S20_3LE:
  510. reg |= 0x20;
  511. break;
  512. case SNDRV_PCM_FORMAT_S24_LE:
  513. reg |= 0x40;
  514. break;
  515. case SNDRV_PCM_FORMAT_S32_LE:
  516. reg |= 0x60;
  517. break;
  518. default:
  519. return -EINVAL;
  520. }
  521. snd_soc_write(codec, WM8900_REG_AUDIO1, reg);
  522. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  523. reg = snd_soc_read(codec, WM8900_REG_DACCTRL);
  524. if (params_rate(params) <= 24000)
  525. reg |= WM8900_REG_DACCTRL_DAC_SB_FILT;
  526. else
  527. reg &= ~WM8900_REG_DACCTRL_DAC_SB_FILT;
  528. snd_soc_write(codec, WM8900_REG_DACCTRL, reg);
  529. }
  530. return 0;
  531. }
  532. /* FLL divisors */
  533. struct _fll_div {
  534. u16 fll_ratio;
  535. u16 fllclk_div;
  536. u16 fll_slow_lock_ref;
  537. u16 n;
  538. u16 k;
  539. };
  540. /* The size in bits of the FLL divide multiplied by 10
  541. * to allow rounding later */
  542. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  543. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  544. unsigned int Fout)
  545. {
  546. u64 Kpart;
  547. unsigned int K, Ndiv, Nmod, target;
  548. unsigned int div;
  549. BUG_ON(!Fout);
  550. /* The FLL must run at 90-100MHz which is then scaled down to
  551. * the output value by FLLCLK_DIV. */
  552. target = Fout;
  553. div = 1;
  554. while (target < 90000000) {
  555. div *= 2;
  556. target *= 2;
  557. }
  558. if (target > 100000000)
  559. printk(KERN_WARNING "wm8900: FLL rate %u out of range, Fref=%u"
  560. " Fout=%u\n", target, Fref, Fout);
  561. if (div > 32) {
  562. printk(KERN_ERR "wm8900: Invalid FLL division rate %u, "
  563. "Fref=%u, Fout=%u, target=%u\n",
  564. div, Fref, Fout, target);
  565. return -EINVAL;
  566. }
  567. fll_div->fllclk_div = div >> 2;
  568. if (Fref < 48000)
  569. fll_div->fll_slow_lock_ref = 1;
  570. else
  571. fll_div->fll_slow_lock_ref = 0;
  572. Ndiv = target / Fref;
  573. if (Fref < 1000000)
  574. fll_div->fll_ratio = 8;
  575. else
  576. fll_div->fll_ratio = 1;
  577. fll_div->n = Ndiv / fll_div->fll_ratio;
  578. Nmod = (target / fll_div->fll_ratio) % Fref;
  579. /* Calculate fractional part - scale up so we can round. */
  580. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  581. do_div(Kpart, Fref);
  582. K = Kpart & 0xFFFFFFFF;
  583. if ((K % 10) >= 5)
  584. K += 5;
  585. /* Move down to proper range now rounding is done */
  586. fll_div->k = K / 10;
  587. BUG_ON(target != Fout * (fll_div->fllclk_div << 2));
  588. BUG_ON(!K && target != Fref * fll_div->fll_ratio * fll_div->n);
  589. return 0;
  590. }
  591. static int wm8900_set_fll(struct snd_soc_codec *codec,
  592. int fll_id, unsigned int freq_in, unsigned int freq_out)
  593. {
  594. struct wm8900_priv *wm8900 = snd_soc_codec_get_drvdata(codec);
  595. struct _fll_div fll_div;
  596. if (wm8900->fll_in == freq_in && wm8900->fll_out == freq_out)
  597. return 0;
  598. /* The digital side should be disabled during any change. */
  599. snd_soc_update_bits(codec, WM8900_REG_POWER1,
  600. WM8900_REG_POWER1_FLL_ENA, 0);
  601. /* Disable the FLL? */
  602. if (!freq_in || !freq_out) {
  603. snd_soc_update_bits(codec, WM8900_REG_CLOCKING1,
  604. WM8900_REG_CLOCKING1_MCLK_SRC, 0);
  605. snd_soc_update_bits(codec, WM8900_REG_FLLCTL1,
  606. WM8900_REG_FLLCTL1_OSC_ENA, 0);
  607. wm8900->fll_in = freq_in;
  608. wm8900->fll_out = freq_out;
  609. return 0;
  610. }
  611. if (fll_factors(&fll_div, freq_in, freq_out) != 0)
  612. goto reenable;
  613. wm8900->fll_in = freq_in;
  614. wm8900->fll_out = freq_out;
  615. /* The osclilator *MUST* be enabled before we enable the
  616. * digital circuit. */
  617. snd_soc_write(codec, WM8900_REG_FLLCTL1,
  618. fll_div.fll_ratio | WM8900_REG_FLLCTL1_OSC_ENA);
  619. snd_soc_write(codec, WM8900_REG_FLLCTL4, fll_div.n >> 5);
  620. snd_soc_write(codec, WM8900_REG_FLLCTL5,
  621. (fll_div.fllclk_div << 6) | (fll_div.n & 0x1f));
  622. if (fll_div.k) {
  623. snd_soc_write(codec, WM8900_REG_FLLCTL2,
  624. (fll_div.k >> 8) | 0x100);
  625. snd_soc_write(codec, WM8900_REG_FLLCTL3, fll_div.k & 0xff);
  626. } else
  627. snd_soc_write(codec, WM8900_REG_FLLCTL2, 0);
  628. if (fll_div.fll_slow_lock_ref)
  629. snd_soc_write(codec, WM8900_REG_FLLCTL6,
  630. WM8900_REG_FLLCTL6_FLL_SLOW_LOCK_REF);
  631. else
  632. snd_soc_write(codec, WM8900_REG_FLLCTL6, 0);
  633. snd_soc_update_bits(codec, WM8900_REG_POWER1,
  634. WM8900_REG_POWER1_FLL_ENA,
  635. WM8900_REG_POWER1_FLL_ENA);
  636. reenable:
  637. snd_soc_update_bits(codec, WM8900_REG_CLOCKING1,
  638. WM8900_REG_CLOCKING1_MCLK_SRC,
  639. WM8900_REG_CLOCKING1_MCLK_SRC);
  640. return 0;
  641. }
  642. static int wm8900_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  643. int source, unsigned int freq_in, unsigned int freq_out)
  644. {
  645. return wm8900_set_fll(codec_dai->codec, pll_id, freq_in, freq_out);
  646. }
  647. static int wm8900_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  648. int div_id, int div)
  649. {
  650. struct snd_soc_codec *codec = codec_dai->codec;
  651. switch (div_id) {
  652. case WM8900_BCLK_DIV:
  653. snd_soc_update_bits(codec, WM8900_REG_CLOCKING1,
  654. WM8900_REG_CLOCKING1_BCLK_MASK, div);
  655. break;
  656. case WM8900_OPCLK_DIV:
  657. snd_soc_update_bits(codec, WM8900_REG_CLOCKING1,
  658. WM8900_REG_CLOCKING1_OPCLK_MASK, div);
  659. break;
  660. case WM8900_DAC_LRCLK:
  661. snd_soc_update_bits(codec, WM8900_REG_AUDIO4,
  662. WM8900_LRC_MASK, div);
  663. break;
  664. case WM8900_ADC_LRCLK:
  665. snd_soc_update_bits(codec, WM8900_REG_AUDIO3,
  666. WM8900_LRC_MASK, div);
  667. break;
  668. case WM8900_DAC_CLKDIV:
  669. snd_soc_update_bits(codec, WM8900_REG_CLOCKING2,
  670. WM8900_REG_CLOCKING2_DAC_CLKDIV, div);
  671. break;
  672. case WM8900_ADC_CLKDIV:
  673. snd_soc_update_bits(codec, WM8900_REG_CLOCKING2,
  674. WM8900_REG_CLOCKING2_ADC_CLKDIV, div);
  675. break;
  676. case WM8900_LRCLK_MODE:
  677. snd_soc_update_bits(codec, WM8900_REG_DACCTRL,
  678. WM8900_REG_DACCTRL_AIF_LRCLKRATE, div);
  679. break;
  680. default:
  681. return -EINVAL;
  682. }
  683. return 0;
  684. }
  685. static int wm8900_set_dai_fmt(struct snd_soc_dai *codec_dai,
  686. unsigned int fmt)
  687. {
  688. struct snd_soc_codec *codec = codec_dai->codec;
  689. unsigned int clocking1, aif1, aif3, aif4;
  690. clocking1 = snd_soc_read(codec, WM8900_REG_CLOCKING1);
  691. aif1 = snd_soc_read(codec, WM8900_REG_AUDIO1);
  692. aif3 = snd_soc_read(codec, WM8900_REG_AUDIO3);
  693. aif4 = snd_soc_read(codec, WM8900_REG_AUDIO4);
  694. /* set master/slave audio interface */
  695. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  696. case SND_SOC_DAIFMT_CBS_CFS:
  697. clocking1 &= ~WM8900_REG_CLOCKING1_BCLK_DIR;
  698. aif3 &= ~WM8900_REG_AUDIO3_ADCLRC_DIR;
  699. aif4 &= ~WM8900_REG_AUDIO4_DACLRC_DIR;
  700. break;
  701. case SND_SOC_DAIFMT_CBS_CFM:
  702. clocking1 &= ~WM8900_REG_CLOCKING1_BCLK_DIR;
  703. aif3 |= WM8900_REG_AUDIO3_ADCLRC_DIR;
  704. aif4 |= WM8900_REG_AUDIO4_DACLRC_DIR;
  705. break;
  706. case SND_SOC_DAIFMT_CBM_CFM:
  707. clocking1 |= WM8900_REG_CLOCKING1_BCLK_DIR;
  708. aif3 |= WM8900_REG_AUDIO3_ADCLRC_DIR;
  709. aif4 |= WM8900_REG_AUDIO4_DACLRC_DIR;
  710. break;
  711. case SND_SOC_DAIFMT_CBM_CFS:
  712. clocking1 |= WM8900_REG_CLOCKING1_BCLK_DIR;
  713. aif3 &= ~WM8900_REG_AUDIO3_ADCLRC_DIR;
  714. aif4 &= ~WM8900_REG_AUDIO4_DACLRC_DIR;
  715. break;
  716. default:
  717. return -EINVAL;
  718. }
  719. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  720. case SND_SOC_DAIFMT_DSP_A:
  721. aif1 |= WM8900_REG_AUDIO1_AIF_FMT_MASK;
  722. aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
  723. break;
  724. case SND_SOC_DAIFMT_DSP_B:
  725. aif1 |= WM8900_REG_AUDIO1_AIF_FMT_MASK;
  726. aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
  727. break;
  728. case SND_SOC_DAIFMT_I2S:
  729. aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
  730. aif1 |= 0x10;
  731. break;
  732. case SND_SOC_DAIFMT_RIGHT_J:
  733. aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
  734. break;
  735. case SND_SOC_DAIFMT_LEFT_J:
  736. aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
  737. aif1 |= 0x8;
  738. break;
  739. default:
  740. return -EINVAL;
  741. }
  742. /* Clock inversion */
  743. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  744. case SND_SOC_DAIFMT_DSP_A:
  745. case SND_SOC_DAIFMT_DSP_B:
  746. /* frame inversion not valid for DSP modes */
  747. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  748. case SND_SOC_DAIFMT_NB_NF:
  749. aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
  750. break;
  751. case SND_SOC_DAIFMT_IB_NF:
  752. aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
  753. break;
  754. default:
  755. return -EINVAL;
  756. }
  757. break;
  758. case SND_SOC_DAIFMT_I2S:
  759. case SND_SOC_DAIFMT_RIGHT_J:
  760. case SND_SOC_DAIFMT_LEFT_J:
  761. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  762. case SND_SOC_DAIFMT_NB_NF:
  763. aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
  764. aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
  765. break;
  766. case SND_SOC_DAIFMT_IB_IF:
  767. aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
  768. aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
  769. break;
  770. case SND_SOC_DAIFMT_IB_NF:
  771. aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
  772. aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
  773. break;
  774. case SND_SOC_DAIFMT_NB_IF:
  775. aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
  776. aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
  777. break;
  778. default:
  779. return -EINVAL;
  780. }
  781. break;
  782. default:
  783. return -EINVAL;
  784. }
  785. snd_soc_write(codec, WM8900_REG_CLOCKING1, clocking1);
  786. snd_soc_write(codec, WM8900_REG_AUDIO1, aif1);
  787. snd_soc_write(codec, WM8900_REG_AUDIO3, aif3);
  788. snd_soc_write(codec, WM8900_REG_AUDIO4, aif4);
  789. return 0;
  790. }
  791. static int wm8900_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  792. {
  793. struct snd_soc_codec *codec = codec_dai->codec;
  794. u16 reg;
  795. reg = snd_soc_read(codec, WM8900_REG_DACCTRL);
  796. if (mute)
  797. reg |= WM8900_REG_DACCTRL_MUTE;
  798. else
  799. reg &= ~WM8900_REG_DACCTRL_MUTE;
  800. snd_soc_write(codec, WM8900_REG_DACCTRL, reg);
  801. return 0;
  802. }
  803. #define WM8900_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  804. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  805. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  806. #define WM8900_PCM_FORMATS \
  807. (SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
  808. SNDRV_PCM_FORMAT_S24_LE)
  809. static const struct snd_soc_dai_ops wm8900_dai_ops = {
  810. .hw_params = wm8900_hw_params,
  811. .set_clkdiv = wm8900_set_dai_clkdiv,
  812. .set_pll = wm8900_set_dai_pll,
  813. .set_fmt = wm8900_set_dai_fmt,
  814. .digital_mute = wm8900_digital_mute,
  815. };
  816. static struct snd_soc_dai_driver wm8900_dai = {
  817. .name = "wm8900-hifi",
  818. .playback = {
  819. .stream_name = "HiFi Playback",
  820. .channels_min = 1,
  821. .channels_max = 2,
  822. .rates = WM8900_RATES,
  823. .formats = WM8900_PCM_FORMATS,
  824. },
  825. .capture = {
  826. .stream_name = "HiFi Capture",
  827. .channels_min = 1,
  828. .channels_max = 2,
  829. .rates = WM8900_RATES,
  830. .formats = WM8900_PCM_FORMATS,
  831. },
  832. .ops = &wm8900_dai_ops,
  833. };
  834. static int wm8900_set_bias_level(struct snd_soc_codec *codec,
  835. enum snd_soc_bias_level level)
  836. {
  837. u16 reg;
  838. switch (level) {
  839. case SND_SOC_BIAS_ON:
  840. /* Enable thermal shutdown */
  841. snd_soc_update_bits(codec, WM8900_REG_GPIO,
  842. WM8900_REG_GPIO_TEMP_ENA,
  843. WM8900_REG_GPIO_TEMP_ENA);
  844. snd_soc_update_bits(codec, WM8900_REG_ADDCTL,
  845. WM8900_REG_ADDCTL_TEMP_SD,
  846. WM8900_REG_ADDCTL_TEMP_SD);
  847. break;
  848. case SND_SOC_BIAS_PREPARE:
  849. break;
  850. case SND_SOC_BIAS_STANDBY:
  851. /* Charge capacitors if initial power up */
  852. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  853. /* STARTUP_BIAS_ENA on */
  854. snd_soc_write(codec, WM8900_REG_POWER1,
  855. WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  856. /* Startup bias mode */
  857. snd_soc_write(codec, WM8900_REG_ADDCTL,
  858. WM8900_REG_ADDCTL_BIAS_SRC |
  859. WM8900_REG_ADDCTL_VMID_SOFTST);
  860. /* VMID 2x50k */
  861. snd_soc_write(codec, WM8900_REG_POWER1,
  862. WM8900_REG_POWER1_STARTUP_BIAS_ENA | 0x1);
  863. /* Allow capacitors to charge */
  864. schedule_timeout_interruptible(msecs_to_jiffies(400));
  865. /* Enable bias */
  866. snd_soc_write(codec, WM8900_REG_POWER1,
  867. WM8900_REG_POWER1_STARTUP_BIAS_ENA |
  868. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  869. snd_soc_write(codec, WM8900_REG_ADDCTL, 0);
  870. snd_soc_write(codec, WM8900_REG_POWER1,
  871. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  872. }
  873. reg = snd_soc_read(codec, WM8900_REG_POWER1);
  874. snd_soc_write(codec, WM8900_REG_POWER1,
  875. (reg & WM8900_REG_POWER1_FLL_ENA) |
  876. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  877. snd_soc_write(codec, WM8900_REG_POWER2,
  878. WM8900_REG_POWER2_SYSCLK_ENA);
  879. snd_soc_write(codec, WM8900_REG_POWER3, 0);
  880. break;
  881. case SND_SOC_BIAS_OFF:
  882. /* Startup bias enable */
  883. reg = snd_soc_read(codec, WM8900_REG_POWER1);
  884. snd_soc_write(codec, WM8900_REG_POWER1,
  885. reg & WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  886. snd_soc_write(codec, WM8900_REG_ADDCTL,
  887. WM8900_REG_ADDCTL_BIAS_SRC |
  888. WM8900_REG_ADDCTL_VMID_SOFTST);
  889. /* Discharge caps */
  890. snd_soc_write(codec, WM8900_REG_POWER1,
  891. WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  892. schedule_timeout_interruptible(msecs_to_jiffies(500));
  893. /* Remove clamp */
  894. snd_soc_write(codec, WM8900_REG_HPCTL1, 0);
  895. /* Power down */
  896. snd_soc_write(codec, WM8900_REG_ADDCTL, 0);
  897. snd_soc_write(codec, WM8900_REG_POWER1, 0);
  898. snd_soc_write(codec, WM8900_REG_POWER2, 0);
  899. snd_soc_write(codec, WM8900_REG_POWER3, 0);
  900. /* Need to let things settle before stopping the clock
  901. * to ensure that restart works, see "Stopping the
  902. * master clock" in the datasheet. */
  903. schedule_timeout_interruptible(msecs_to_jiffies(1));
  904. snd_soc_write(codec, WM8900_REG_POWER2,
  905. WM8900_REG_POWER2_SYSCLK_ENA);
  906. break;
  907. }
  908. codec->dapm.bias_level = level;
  909. return 0;
  910. }
  911. static int wm8900_suspend(struct snd_soc_codec *codec)
  912. {
  913. struct wm8900_priv *wm8900 = snd_soc_codec_get_drvdata(codec);
  914. int fll_out = wm8900->fll_out;
  915. int fll_in = wm8900->fll_in;
  916. int ret;
  917. /* Stop the FLL in an orderly fashion */
  918. ret = wm8900_set_fll(codec, 0, 0, 0);
  919. if (ret != 0) {
  920. dev_err(codec->dev, "Failed to stop FLL\n");
  921. return ret;
  922. }
  923. wm8900->fll_out = fll_out;
  924. wm8900->fll_in = fll_in;
  925. wm8900_set_bias_level(codec, SND_SOC_BIAS_OFF);
  926. return 0;
  927. }
  928. static int wm8900_resume(struct snd_soc_codec *codec)
  929. {
  930. struct wm8900_priv *wm8900 = snd_soc_codec_get_drvdata(codec);
  931. u16 *cache;
  932. int i, ret;
  933. cache = kmemdup(codec->reg_cache, sizeof(wm8900_reg_defaults),
  934. GFP_KERNEL);
  935. wm8900_reset(codec);
  936. wm8900_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  937. /* Restart the FLL? */
  938. if (wm8900->fll_out) {
  939. int fll_out = wm8900->fll_out;
  940. int fll_in = wm8900->fll_in;
  941. wm8900->fll_in = 0;
  942. wm8900->fll_out = 0;
  943. ret = wm8900_set_fll(codec, 0, fll_in, fll_out);
  944. if (ret != 0) {
  945. dev_err(codec->dev, "Failed to restart FLL\n");
  946. kfree(cache);
  947. return ret;
  948. }
  949. }
  950. if (cache) {
  951. for (i = 0; i < WM8900_MAXREG; i++)
  952. snd_soc_write(codec, i, cache[i]);
  953. kfree(cache);
  954. } else
  955. dev_err(codec->dev, "Unable to allocate register cache\n");
  956. return 0;
  957. }
  958. static int wm8900_probe(struct snd_soc_codec *codec)
  959. {
  960. struct wm8900_priv *wm8900 = snd_soc_codec_get_drvdata(codec);
  961. int ret = 0, reg;
  962. ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm8900->control_type);
  963. if (ret != 0) {
  964. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  965. return ret;
  966. }
  967. reg = snd_soc_read(codec, WM8900_REG_ID);
  968. if (reg != 0x8900) {
  969. dev_err(codec->dev, "Device is not a WM8900 - ID %x\n", reg);
  970. return -ENODEV;
  971. }
  972. wm8900_reset(codec);
  973. /* Turn the chip on */
  974. wm8900_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  975. /* Latch the volume update bits */
  976. snd_soc_update_bits(codec, WM8900_REG_LINVOL, 0x100, 0x100);
  977. snd_soc_update_bits(codec, WM8900_REG_RINVOL, 0x100, 0x100);
  978. snd_soc_update_bits(codec, WM8900_REG_LOUT1CTL, 0x100, 0x100);
  979. snd_soc_update_bits(codec, WM8900_REG_ROUT1CTL, 0x100, 0x100);
  980. snd_soc_update_bits(codec, WM8900_REG_LOUT2CTL, 0x100, 0x100);
  981. snd_soc_update_bits(codec, WM8900_REG_ROUT2CTL, 0x100, 0x100);
  982. snd_soc_update_bits(codec, WM8900_REG_LDAC_DV, 0x100, 0x100);
  983. snd_soc_update_bits(codec, WM8900_REG_RDAC_DV, 0x100, 0x100);
  984. snd_soc_update_bits(codec, WM8900_REG_LADC_DV, 0x100, 0x100);
  985. snd_soc_update_bits(codec, WM8900_REG_RADC_DV, 0x100, 0x100);
  986. /* Set the DAC and mixer output bias */
  987. snd_soc_write(codec, WM8900_REG_OUTBIASCTL, 0x81);
  988. return 0;
  989. }
  990. /* power down chip */
  991. static int wm8900_remove(struct snd_soc_codec *codec)
  992. {
  993. wm8900_set_bias_level(codec, SND_SOC_BIAS_OFF);
  994. return 0;
  995. }
  996. static struct snd_soc_codec_driver soc_codec_dev_wm8900 = {
  997. .probe = wm8900_probe,
  998. .remove = wm8900_remove,
  999. .suspend = wm8900_suspend,
  1000. .resume = wm8900_resume,
  1001. .set_bias_level = wm8900_set_bias_level,
  1002. .volatile_register = wm8900_volatile_register,
  1003. .reg_cache_size = ARRAY_SIZE(wm8900_reg_defaults),
  1004. .reg_word_size = sizeof(u16),
  1005. .reg_cache_default = wm8900_reg_defaults,
  1006. .controls = wm8900_snd_controls,
  1007. .num_controls = ARRAY_SIZE(wm8900_snd_controls),
  1008. .dapm_widgets = wm8900_dapm_widgets,
  1009. .num_dapm_widgets = ARRAY_SIZE(wm8900_dapm_widgets),
  1010. .dapm_routes = wm8900_dapm_routes,
  1011. .num_dapm_routes = ARRAY_SIZE(wm8900_dapm_routes),
  1012. };
  1013. #if defined(CONFIG_SPI_MASTER)
  1014. static int __devinit wm8900_spi_probe(struct spi_device *spi)
  1015. {
  1016. struct wm8900_priv *wm8900;
  1017. int ret;
  1018. wm8900 = kzalloc(sizeof(struct wm8900_priv), GFP_KERNEL);
  1019. if (wm8900 == NULL)
  1020. return -ENOMEM;
  1021. wm8900->control_type = SND_SOC_SPI;
  1022. spi_set_drvdata(spi, wm8900);
  1023. ret = snd_soc_register_codec(&spi->dev,
  1024. &soc_codec_dev_wm8900, &wm8900_dai, 1);
  1025. if (ret < 0)
  1026. kfree(wm8900);
  1027. return ret;
  1028. }
  1029. static int __devexit wm8900_spi_remove(struct spi_device *spi)
  1030. {
  1031. snd_soc_unregister_codec(&spi->dev);
  1032. kfree(spi_get_drvdata(spi));
  1033. return 0;
  1034. }
  1035. static struct spi_driver wm8900_spi_driver = {
  1036. .driver = {
  1037. .name = "wm8900",
  1038. .owner = THIS_MODULE,
  1039. },
  1040. .probe = wm8900_spi_probe,
  1041. .remove = __devexit_p(wm8900_spi_remove),
  1042. };
  1043. #endif /* CONFIG_SPI_MASTER */
  1044. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1045. static __devinit int wm8900_i2c_probe(struct i2c_client *i2c,
  1046. const struct i2c_device_id *id)
  1047. {
  1048. struct wm8900_priv *wm8900;
  1049. int ret;
  1050. wm8900 = kzalloc(sizeof(struct wm8900_priv), GFP_KERNEL);
  1051. if (wm8900 == NULL)
  1052. return -ENOMEM;
  1053. i2c_set_clientdata(i2c, wm8900);
  1054. wm8900->control_type = SND_SOC_I2C;
  1055. ret = snd_soc_register_codec(&i2c->dev,
  1056. &soc_codec_dev_wm8900, &wm8900_dai, 1);
  1057. if (ret < 0)
  1058. kfree(wm8900);
  1059. return ret;
  1060. }
  1061. static __devexit int wm8900_i2c_remove(struct i2c_client *client)
  1062. {
  1063. snd_soc_unregister_codec(&client->dev);
  1064. kfree(i2c_get_clientdata(client));
  1065. return 0;
  1066. }
  1067. static const struct i2c_device_id wm8900_i2c_id[] = {
  1068. { "wm8900", 0 },
  1069. { }
  1070. };
  1071. MODULE_DEVICE_TABLE(i2c, wm8900_i2c_id);
  1072. static struct i2c_driver wm8900_i2c_driver = {
  1073. .driver = {
  1074. .name = "wm8900",
  1075. .owner = THIS_MODULE,
  1076. },
  1077. .probe = wm8900_i2c_probe,
  1078. .remove = __devexit_p(wm8900_i2c_remove),
  1079. .id_table = wm8900_i2c_id,
  1080. };
  1081. #endif
  1082. static int __init wm8900_modinit(void)
  1083. {
  1084. int ret = 0;
  1085. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1086. ret = i2c_add_driver(&wm8900_i2c_driver);
  1087. if (ret != 0) {
  1088. printk(KERN_ERR "Failed to register wm8900 I2C driver: %d\n",
  1089. ret);
  1090. }
  1091. #endif
  1092. #if defined(CONFIG_SPI_MASTER)
  1093. ret = spi_register_driver(&wm8900_spi_driver);
  1094. if (ret != 0) {
  1095. printk(KERN_ERR "Failed to register wm8900 SPI driver: %d\n",
  1096. ret);
  1097. }
  1098. #endif
  1099. return ret;
  1100. }
  1101. module_init(wm8900_modinit);
  1102. static void __exit wm8900_exit(void)
  1103. {
  1104. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1105. i2c_del_driver(&wm8900_i2c_driver);
  1106. #endif
  1107. #if defined(CONFIG_SPI_MASTER)
  1108. spi_unregister_driver(&wm8900_spi_driver);
  1109. #endif
  1110. }
  1111. module_exit(wm8900_exit);
  1112. MODULE_DESCRIPTION("ASoC WM8900 driver");
  1113. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
  1114. MODULE_LICENSE("GPL");