wm8900.c 38 KB

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