wm8900.c 39 KB

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