wm8900.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/soc.h>
  31. #include <sound/soc-dapm.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 0xfc00
  120. struct snd_soc_codec_device soc_codec_dev_wm8900;
  121. struct wm8900_priv {
  122. struct snd_soc_codec codec;
  123. u16 reg_cache[WM8900_MAXREG];
  124. u32 fll_in; /* FLL input frequency */
  125. u32 fll_out; /* FLL output frequency */
  126. };
  127. /*
  128. * wm8900 register cache. We can't read the entire register space and we
  129. * have slow control buses so we cache the registers.
  130. */
  131. static const u16 wm8900_reg_defaults[WM8900_MAXREG] = {
  132. 0x8900, 0x0000,
  133. 0xc000, 0x0000,
  134. 0x4050, 0x4000,
  135. 0x0008, 0x0000,
  136. 0x0040, 0x0040,
  137. 0x1004, 0x00c0,
  138. 0x00c0, 0x0000,
  139. 0x0100, 0x00c0,
  140. 0x00c0, 0x0000,
  141. 0xb001, 0x0000,
  142. 0x0000, 0x0044,
  143. 0x004c, 0x004c,
  144. 0x0044, 0x0044,
  145. 0x0000, 0x0044,
  146. 0x0000, 0x0000,
  147. 0x0002, 0x0000,
  148. 0x0000, 0x0000,
  149. 0x0000, 0x0000,
  150. 0x0008, 0x0000,
  151. 0x0000, 0x0008,
  152. 0x0097, 0x0100,
  153. 0x0000, 0x0000,
  154. 0x0050, 0x0050,
  155. 0x0055, 0x0055,
  156. 0x0055, 0x0000,
  157. 0x0000, 0x0079,
  158. 0x0079, 0x0079,
  159. 0x0079, 0x0000,
  160. /* Remaining registers all zero */
  161. };
  162. static int wm8900_volatile_register(unsigned int reg)
  163. {
  164. switch (reg) {
  165. case WM8900_REG_ID:
  166. case WM8900_REG_POWER1:
  167. return 1;
  168. default:
  169. return 0;
  170. }
  171. }
  172. static void wm8900_reset(struct snd_soc_codec *codec)
  173. {
  174. snd_soc_write(codec, WM8900_REG_RESET, 0);
  175. memcpy(codec->reg_cache, wm8900_reg_defaults,
  176. sizeof(wm8900_reg_defaults));
  177. }
  178. static int wm8900_hp_event(struct snd_soc_dapm_widget *w,
  179. struct snd_kcontrol *kcontrol, int event)
  180. {
  181. struct snd_soc_codec *codec = w->codec;
  182. u16 hpctl1 = snd_soc_read(codec, WM8900_REG_HPCTL1);
  183. switch (event) {
  184. case SND_SOC_DAPM_PRE_PMU:
  185. /* Clamp headphone outputs */
  186. hpctl1 = WM8900_REG_HPCTL1_HP_CLAMP_IP |
  187. WM8900_REG_HPCTL1_HP_CLAMP_OP;
  188. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  189. break;
  190. case SND_SOC_DAPM_POST_PMU:
  191. /* Enable the input stage */
  192. hpctl1 &= ~WM8900_REG_HPCTL1_HP_CLAMP_IP;
  193. hpctl1 |= WM8900_REG_HPCTL1_HP_SHORT |
  194. WM8900_REG_HPCTL1_HP_SHORT2 |
  195. WM8900_REG_HPCTL1_HP_IPSTAGE_ENA;
  196. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  197. msleep(400);
  198. /* Enable the output stage */
  199. hpctl1 &= ~WM8900_REG_HPCTL1_HP_CLAMP_OP;
  200. hpctl1 |= WM8900_REG_HPCTL1_HP_OPSTAGE_ENA;
  201. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  202. /* Remove the shorts */
  203. hpctl1 &= ~WM8900_REG_HPCTL1_HP_SHORT2;
  204. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  205. hpctl1 &= ~WM8900_REG_HPCTL1_HP_SHORT;
  206. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  207. break;
  208. case SND_SOC_DAPM_PRE_PMD:
  209. /* Short the output */
  210. hpctl1 |= WM8900_REG_HPCTL1_HP_SHORT;
  211. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  212. /* Disable the output stage */
  213. hpctl1 &= ~WM8900_REG_HPCTL1_HP_OPSTAGE_ENA;
  214. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  215. /* Clamp the outputs and power down input */
  216. hpctl1 |= WM8900_REG_HPCTL1_HP_CLAMP_IP |
  217. WM8900_REG_HPCTL1_HP_CLAMP_OP;
  218. hpctl1 &= ~WM8900_REG_HPCTL1_HP_IPSTAGE_ENA;
  219. snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
  220. break;
  221. case SND_SOC_DAPM_POST_PMD:
  222. /* Disable everything */
  223. snd_soc_write(codec, WM8900_REG_HPCTL1, 0);
  224. break;
  225. default:
  226. BUG();
  227. }
  228. return 0;
  229. }
  230. static const DECLARE_TLV_DB_SCALE(out_pga_tlv, -5700, 100, 0);
  231. static const DECLARE_TLV_DB_SCALE(out_mix_tlv, -1500, 300, 0);
  232. static const DECLARE_TLV_DB_SCALE(in_boost_tlv, -1200, 600, 0);
  233. static const DECLARE_TLV_DB_SCALE(in_pga_tlv, -1200, 100, 0);
  234. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  235. static const DECLARE_TLV_DB_SCALE(dac_tlv, -7200, 75, 1);
  236. static const DECLARE_TLV_DB_SCALE(adc_svol_tlv, -3600, 300, 0);
  237. static const DECLARE_TLV_DB_SCALE(adc_tlv, -7200, 75, 1);
  238. static const char *mic_bias_level_txt[] = { "0.9*AVDD", "0.65*AVDD" };
  239. static const struct soc_enum mic_bias_level =
  240. SOC_ENUM_SINGLE(WM8900_REG_INCTL, 8, 2, mic_bias_level_txt);
  241. static const char *dac_mute_rate_txt[] = { "Fast", "Slow" };
  242. static const struct soc_enum dac_mute_rate =
  243. SOC_ENUM_SINGLE(WM8900_REG_DACCTRL, 7, 2, dac_mute_rate_txt);
  244. static const char *dac_deemphasis_txt[] = {
  245. "Disabled", "32kHz", "44.1kHz", "48kHz"
  246. };
  247. static const struct soc_enum dac_deemphasis =
  248. SOC_ENUM_SINGLE(WM8900_REG_DACCTRL, 4, 4, dac_deemphasis_txt);
  249. static const char *adc_hpf_cut_txt[] = {
  250. "Hi-fi mode", "Voice mode 1", "Voice mode 2", "Voice mode 3"
  251. };
  252. static const struct soc_enum adc_hpf_cut =
  253. SOC_ENUM_SINGLE(WM8900_REG_ADCCTRL, 5, 4, adc_hpf_cut_txt);
  254. static const char *lr_txt[] = {
  255. "Left", "Right"
  256. };
  257. static const struct soc_enum aifl_src =
  258. SOC_ENUM_SINGLE(WM8900_REG_AUDIO1, 15, 2, lr_txt);
  259. static const struct soc_enum aifr_src =
  260. SOC_ENUM_SINGLE(WM8900_REG_AUDIO1, 14, 2, lr_txt);
  261. static const struct soc_enum dacl_src =
  262. SOC_ENUM_SINGLE(WM8900_REG_AUDIO2, 15, 2, lr_txt);
  263. static const struct soc_enum dacr_src =
  264. SOC_ENUM_SINGLE(WM8900_REG_AUDIO2, 14, 2, lr_txt);
  265. static const char *sidetone_txt[] = {
  266. "Disabled", "Left ADC", "Right ADC"
  267. };
  268. static const struct soc_enum dacl_sidetone =
  269. SOC_ENUM_SINGLE(WM8900_REG_SIDETONE, 2, 3, sidetone_txt);
  270. static const struct soc_enum dacr_sidetone =
  271. SOC_ENUM_SINGLE(WM8900_REG_SIDETONE, 0, 3, sidetone_txt);
  272. static const struct snd_kcontrol_new wm8900_snd_controls[] = {
  273. SOC_ENUM("Mic Bias Level", mic_bias_level),
  274. SOC_SINGLE_TLV("Left Input PGA Volume", WM8900_REG_LINVOL, 0, 31, 0,
  275. in_pga_tlv),
  276. SOC_SINGLE("Left Input PGA Switch", WM8900_REG_LINVOL, 6, 1, 1),
  277. SOC_SINGLE("Left Input PGA ZC Switch", WM8900_REG_LINVOL, 7, 1, 0),
  278. SOC_SINGLE_TLV("Right Input PGA Volume", WM8900_REG_RINVOL, 0, 31, 0,
  279. in_pga_tlv),
  280. SOC_SINGLE("Right Input PGA Switch", WM8900_REG_RINVOL, 6, 1, 1),
  281. SOC_SINGLE("Right Input PGA ZC Switch", WM8900_REG_RINVOL, 7, 1, 0),
  282. SOC_SINGLE("DAC Soft Mute Switch", WM8900_REG_DACCTRL, 6, 1, 1),
  283. SOC_ENUM("DAC Mute Rate", dac_mute_rate),
  284. SOC_SINGLE("DAC Mono Switch", WM8900_REG_DACCTRL, 9, 1, 0),
  285. SOC_ENUM("DAC Deemphasis", dac_deemphasis),
  286. SOC_SINGLE("DAC Sigma-Delta Modulator Clock Switch", WM8900_REG_DACCTRL,
  287. 12, 1, 0),
  288. SOC_SINGLE("ADC HPF Switch", WM8900_REG_ADCCTRL, 8, 1, 0),
  289. SOC_ENUM("ADC HPF Cut-Off", adc_hpf_cut),
  290. SOC_DOUBLE("ADC Invert Switch", WM8900_REG_ADCCTRL, 1, 0, 1, 0),
  291. SOC_SINGLE_TLV("Left ADC Sidetone Volume", WM8900_REG_SIDETONE, 9, 12, 0,
  292. adc_svol_tlv),
  293. SOC_SINGLE_TLV("Right ADC Sidetone Volume", WM8900_REG_SIDETONE, 5, 12, 0,
  294. adc_svol_tlv),
  295. SOC_ENUM("Left Digital Audio Source", aifl_src),
  296. SOC_ENUM("Right Digital Audio Source", aifr_src),
  297. SOC_SINGLE_TLV("DAC Input Boost Volume", WM8900_REG_AUDIO2, 10, 4, 0,
  298. dac_boost_tlv),
  299. SOC_ENUM("Left DAC Source", dacl_src),
  300. SOC_ENUM("Right DAC Source", dacr_src),
  301. SOC_ENUM("Left DAC Sidetone", dacl_sidetone),
  302. SOC_ENUM("Right DAC Sidetone", dacr_sidetone),
  303. SOC_DOUBLE("DAC Invert Switch", WM8900_REG_DACCTRL, 1, 0, 1, 0),
  304. SOC_DOUBLE_R_TLV("Digital Playback Volume",
  305. WM8900_REG_LDAC_DV, WM8900_REG_RDAC_DV,
  306. 1, 96, 0, dac_tlv),
  307. SOC_DOUBLE_R_TLV("Digital Capture Volume",
  308. WM8900_REG_LADC_DV, WM8900_REG_RADC_DV, 1, 119, 0, adc_tlv),
  309. SOC_SINGLE_TLV("LINPUT3 Bypass Volume", WM8900_REG_LOUTMIXCTL1, 4, 7, 0,
  310. out_mix_tlv),
  311. SOC_SINGLE_TLV("RINPUT3 Bypass Volume", WM8900_REG_ROUTMIXCTL1, 4, 7, 0,
  312. out_mix_tlv),
  313. SOC_SINGLE_TLV("Left AUX Bypass Volume", WM8900_REG_AUXOUT_CTL, 4, 7, 0,
  314. out_mix_tlv),
  315. SOC_SINGLE_TLV("Right AUX Bypass Volume", WM8900_REG_AUXOUT_CTL, 0, 7, 0,
  316. out_mix_tlv),
  317. SOC_SINGLE_TLV("LeftIn to RightOut Mixer Volume", WM8900_REG_BYPASS1, 0, 7, 0,
  318. out_mix_tlv),
  319. SOC_SINGLE_TLV("LeftIn to LeftOut Mixer Volume", WM8900_REG_BYPASS1, 4, 7, 0,
  320. out_mix_tlv),
  321. SOC_SINGLE_TLV("RightIn to LeftOut Mixer Volume", WM8900_REG_BYPASS2, 0, 7, 0,
  322. out_mix_tlv),
  323. SOC_SINGLE_TLV("RightIn to RightOut Mixer Volume", WM8900_REG_BYPASS2, 4, 7, 0,
  324. out_mix_tlv),
  325. SOC_SINGLE_TLV("IN2L Boost Volume", WM8900_REG_INBOOSTMIX1, 0, 3, 0,
  326. in_boost_tlv),
  327. SOC_SINGLE_TLV("IN3L Boost Volume", WM8900_REG_INBOOSTMIX1, 4, 3, 0,
  328. in_boost_tlv),
  329. SOC_SINGLE_TLV("IN2R Boost Volume", WM8900_REG_INBOOSTMIX2, 0, 3, 0,
  330. in_boost_tlv),
  331. SOC_SINGLE_TLV("IN3R Boost Volume", WM8900_REG_INBOOSTMIX2, 4, 3, 0,
  332. in_boost_tlv),
  333. SOC_SINGLE_TLV("Left AUX Boost Volume", WM8900_REG_AUXBOOST, 4, 3, 0,
  334. in_boost_tlv),
  335. SOC_SINGLE_TLV("Right AUX Boost Volume", WM8900_REG_AUXBOOST, 0, 3, 0,
  336. in_boost_tlv),
  337. SOC_DOUBLE_R_TLV("LINEOUT1 Volume", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
  338. 0, 63, 0, out_pga_tlv),
  339. SOC_DOUBLE_R("LINEOUT1 Switch", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
  340. 6, 1, 1),
  341. SOC_DOUBLE_R("LINEOUT1 ZC Switch", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
  342. 7, 1, 0),
  343. SOC_DOUBLE_R_TLV("LINEOUT2 Volume",
  344. WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL,
  345. 0, 63, 0, out_pga_tlv),
  346. SOC_DOUBLE_R("LINEOUT2 Switch",
  347. WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL, 6, 1, 1),
  348. SOC_DOUBLE_R("LINEOUT2 ZC Switch",
  349. WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL, 7, 1, 0),
  350. SOC_SINGLE("LINEOUT2 LP -12dB", WM8900_REG_LOUTMIXCTL1,
  351. 0, 1, 1),
  352. };
  353. static const struct snd_kcontrol_new wm8900_dapm_loutput2_control =
  354. SOC_DAPM_SINGLE("LINEOUT2L Switch", WM8900_REG_POWER3, 6, 1, 0);
  355. static const struct snd_kcontrol_new wm8900_dapm_routput2_control =
  356. SOC_DAPM_SINGLE("LINEOUT2R Switch", WM8900_REG_POWER3, 5, 1, 0);
  357. static const struct snd_kcontrol_new wm8900_loutmix_controls[] = {
  358. SOC_DAPM_SINGLE("LINPUT3 Bypass Switch", WM8900_REG_LOUTMIXCTL1, 7, 1, 0),
  359. SOC_DAPM_SINGLE("AUX Bypass Switch", WM8900_REG_AUXOUT_CTL, 7, 1, 0),
  360. SOC_DAPM_SINGLE("Left Input Mixer Switch", WM8900_REG_BYPASS1, 7, 1, 0),
  361. SOC_DAPM_SINGLE("Right Input Mixer Switch", WM8900_REG_BYPASS2, 3, 1, 0),
  362. SOC_DAPM_SINGLE("DACL Switch", WM8900_REG_LOUTMIXCTL1, 8, 1, 0),
  363. };
  364. static const struct snd_kcontrol_new wm8900_routmix_controls[] = {
  365. SOC_DAPM_SINGLE("RINPUT3 Bypass Switch", WM8900_REG_ROUTMIXCTL1, 7, 1, 0),
  366. SOC_DAPM_SINGLE("AUX Bypass Switch", WM8900_REG_AUXOUT_CTL, 3, 1, 0),
  367. SOC_DAPM_SINGLE("Left Input Mixer Switch", WM8900_REG_BYPASS1, 3, 1, 0),
  368. SOC_DAPM_SINGLE("Right Input Mixer Switch", WM8900_REG_BYPASS2, 7, 1, 0),
  369. SOC_DAPM_SINGLE("DACR Switch", WM8900_REG_ROUTMIXCTL1, 8, 1, 0),
  370. };
  371. static const struct snd_kcontrol_new wm8900_linmix_controls[] = {
  372. SOC_DAPM_SINGLE("LINPUT2 Switch", WM8900_REG_INBOOSTMIX1, 2, 1, 1),
  373. SOC_DAPM_SINGLE("LINPUT3 Switch", WM8900_REG_INBOOSTMIX1, 6, 1, 1),
  374. SOC_DAPM_SINGLE("AUX Switch", WM8900_REG_AUXBOOST, 6, 1, 1),
  375. SOC_DAPM_SINGLE("Input PGA Switch", WM8900_REG_ADCPATH, 6, 1, 0),
  376. };
  377. static const struct snd_kcontrol_new wm8900_rinmix_controls[] = {
  378. SOC_DAPM_SINGLE("RINPUT2 Switch", WM8900_REG_INBOOSTMIX2, 2, 1, 1),
  379. SOC_DAPM_SINGLE("RINPUT3 Switch", WM8900_REG_INBOOSTMIX2, 6, 1, 1),
  380. SOC_DAPM_SINGLE("AUX Switch", WM8900_REG_AUXBOOST, 2, 1, 1),
  381. SOC_DAPM_SINGLE("Input PGA Switch", WM8900_REG_ADCPATH, 2, 1, 0),
  382. };
  383. static const struct snd_kcontrol_new wm8900_linpga_controls[] = {
  384. SOC_DAPM_SINGLE("LINPUT1 Switch", WM8900_REG_INCTL, 6, 1, 0),
  385. SOC_DAPM_SINGLE("LINPUT2 Switch", WM8900_REG_INCTL, 5, 1, 0),
  386. SOC_DAPM_SINGLE("LINPUT3 Switch", WM8900_REG_INCTL, 4, 1, 0),
  387. };
  388. static const struct snd_kcontrol_new wm8900_rinpga_controls[] = {
  389. SOC_DAPM_SINGLE("RINPUT1 Switch", WM8900_REG_INCTL, 2, 1, 0),
  390. SOC_DAPM_SINGLE("RINPUT2 Switch", WM8900_REG_INCTL, 1, 1, 0),
  391. SOC_DAPM_SINGLE("RINPUT3 Switch", WM8900_REG_INCTL, 0, 1, 0),
  392. };
  393. static const char *wm9700_lp_mux[] = { "Disabled", "Enabled" };
  394. static const struct soc_enum wm8900_lineout2_lp_mux =
  395. SOC_ENUM_SINGLE(WM8900_REG_LOUTMIXCTL1, 1, 2, wm9700_lp_mux);
  396. static const struct snd_kcontrol_new wm8900_lineout2_lp =
  397. SOC_DAPM_ENUM("Route", wm8900_lineout2_lp_mux);
  398. static const struct snd_soc_dapm_widget wm8900_dapm_widgets[] = {
  399. /* Externally visible pins */
  400. SND_SOC_DAPM_OUTPUT("LINEOUT1L"),
  401. SND_SOC_DAPM_OUTPUT("LINEOUT1R"),
  402. SND_SOC_DAPM_OUTPUT("LINEOUT2L"),
  403. SND_SOC_DAPM_OUTPUT("LINEOUT2R"),
  404. SND_SOC_DAPM_OUTPUT("HP_L"),
  405. SND_SOC_DAPM_OUTPUT("HP_R"),
  406. SND_SOC_DAPM_INPUT("RINPUT1"),
  407. SND_SOC_DAPM_INPUT("LINPUT1"),
  408. SND_SOC_DAPM_INPUT("RINPUT2"),
  409. SND_SOC_DAPM_INPUT("LINPUT2"),
  410. SND_SOC_DAPM_INPUT("RINPUT3"),
  411. SND_SOC_DAPM_INPUT("LINPUT3"),
  412. SND_SOC_DAPM_INPUT("AUX"),
  413. SND_SOC_DAPM_VMID("VMID"),
  414. /* Input */
  415. SND_SOC_DAPM_MIXER("Left Input PGA", WM8900_REG_POWER2, 3, 0,
  416. wm8900_linpga_controls,
  417. ARRAY_SIZE(wm8900_linpga_controls)),
  418. SND_SOC_DAPM_MIXER("Right Input PGA", WM8900_REG_POWER2, 2, 0,
  419. wm8900_rinpga_controls,
  420. ARRAY_SIZE(wm8900_rinpga_controls)),
  421. SND_SOC_DAPM_MIXER("Left Input Mixer", WM8900_REG_POWER2, 5, 0,
  422. wm8900_linmix_controls,
  423. ARRAY_SIZE(wm8900_linmix_controls)),
  424. SND_SOC_DAPM_MIXER("Right Input Mixer", WM8900_REG_POWER2, 4, 0,
  425. wm8900_rinmix_controls,
  426. ARRAY_SIZE(wm8900_rinmix_controls)),
  427. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8900_REG_POWER1, 4, 0),
  428. SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8900_REG_POWER2, 1, 0),
  429. SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8900_REG_POWER2, 0, 0),
  430. /* Output */
  431. SND_SOC_DAPM_DAC("DACL", "Left HiFi Playback", WM8900_REG_POWER3, 1, 0),
  432. SND_SOC_DAPM_DAC("DACR", "Right HiFi Playback", WM8900_REG_POWER3, 0, 0),
  433. SND_SOC_DAPM_PGA_E("Headphone Amplifier", WM8900_REG_POWER3, 7, 0, NULL, 0,
  434. wm8900_hp_event,
  435. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  436. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  437. SND_SOC_DAPM_PGA("LINEOUT1L PGA", WM8900_REG_POWER2, 8, 0, NULL, 0),
  438. SND_SOC_DAPM_PGA("LINEOUT1R PGA", WM8900_REG_POWER2, 7, 0, NULL, 0),
  439. SND_SOC_DAPM_MUX("LINEOUT2 LP", SND_SOC_NOPM, 0, 0, &wm8900_lineout2_lp),
  440. SND_SOC_DAPM_PGA("LINEOUT2L PGA", WM8900_REG_POWER3, 6, 0, NULL, 0),
  441. SND_SOC_DAPM_PGA("LINEOUT2R PGA", WM8900_REG_POWER3, 5, 0, NULL, 0),
  442. SND_SOC_DAPM_MIXER("Left Output Mixer", WM8900_REG_POWER3, 3, 0,
  443. wm8900_loutmix_controls,
  444. ARRAY_SIZE(wm8900_loutmix_controls)),
  445. SND_SOC_DAPM_MIXER("Right Output Mixer", WM8900_REG_POWER3, 2, 0,
  446. wm8900_routmix_controls,
  447. ARRAY_SIZE(wm8900_routmix_controls)),
  448. };
  449. /* Target, Path, Source */
  450. static const struct snd_soc_dapm_route audio_map[] = {
  451. /* Inputs */
  452. {"Left Input PGA", "LINPUT1 Switch", "LINPUT1"},
  453. {"Left Input PGA", "LINPUT2 Switch", "LINPUT2"},
  454. {"Left Input PGA", "LINPUT3 Switch", "LINPUT3"},
  455. {"Right Input PGA", "RINPUT1 Switch", "RINPUT1"},
  456. {"Right Input PGA", "RINPUT2 Switch", "RINPUT2"},
  457. {"Right Input PGA", "RINPUT3 Switch", "RINPUT3"},
  458. {"Left Input Mixer", "LINPUT2 Switch", "LINPUT2"},
  459. {"Left Input Mixer", "LINPUT3 Switch", "LINPUT3"},
  460. {"Left Input Mixer", "AUX Switch", "AUX"},
  461. {"Left Input Mixer", "Input PGA Switch", "Left Input PGA"},
  462. {"Right Input Mixer", "RINPUT2 Switch", "RINPUT2"},
  463. {"Right Input Mixer", "RINPUT3 Switch", "RINPUT3"},
  464. {"Right Input Mixer", "AUX Switch", "AUX"},
  465. {"Right Input Mixer", "Input PGA Switch", "Right Input PGA"},
  466. {"ADCL", NULL, "Left Input Mixer"},
  467. {"ADCR", NULL, "Right Input Mixer"},
  468. /* Outputs */
  469. {"LINEOUT1L", NULL, "LINEOUT1L PGA"},
  470. {"LINEOUT1L PGA", NULL, "Left Output Mixer"},
  471. {"LINEOUT1R", NULL, "LINEOUT1R PGA"},
  472. {"LINEOUT1R PGA", NULL, "Right Output Mixer"},
  473. {"LINEOUT2L PGA", NULL, "Left Output Mixer"},
  474. {"LINEOUT2 LP", "Disabled", "LINEOUT2L PGA"},
  475. {"LINEOUT2 LP", "Enabled", "Left Output Mixer"},
  476. {"LINEOUT2L", NULL, "LINEOUT2 LP"},
  477. {"LINEOUT2R PGA", NULL, "Right Output Mixer"},
  478. {"LINEOUT2 LP", "Disabled", "LINEOUT2R PGA"},
  479. {"LINEOUT2 LP", "Enabled", "Right Output Mixer"},
  480. {"LINEOUT2R", NULL, "LINEOUT2 LP"},
  481. {"Left Output Mixer", "LINPUT3 Bypass Switch", "LINPUT3"},
  482. {"Left Output Mixer", "AUX Bypass Switch", "AUX"},
  483. {"Left Output Mixer", "Left Input Mixer Switch", "Left Input Mixer"},
  484. {"Left Output Mixer", "Right Input Mixer Switch", "Right Input Mixer"},
  485. {"Left Output Mixer", "DACL Switch", "DACL"},
  486. {"Right Output Mixer", "RINPUT3 Bypass Switch", "RINPUT3"},
  487. {"Right Output Mixer", "AUX Bypass Switch", "AUX"},
  488. {"Right Output Mixer", "Left Input Mixer Switch", "Left Input Mixer"},
  489. {"Right Output Mixer", "Right Input Mixer Switch", "Right Input Mixer"},
  490. {"Right Output Mixer", "DACR Switch", "DACR"},
  491. /* Note that the headphone output stage needs to be connected
  492. * externally to LINEOUT2 via DC blocking capacitors. Other
  493. * configurations are not supported.
  494. *
  495. * Note also that left and right headphone paths are treated as a
  496. * mono path.
  497. */
  498. {"Headphone Amplifier", NULL, "LINEOUT2 LP"},
  499. {"Headphone Amplifier", NULL, "LINEOUT2 LP"},
  500. {"HP_L", NULL, "Headphone Amplifier"},
  501. {"HP_R", NULL, "Headphone Amplifier"},
  502. };
  503. static int wm8900_add_widgets(struct snd_soc_codec *codec)
  504. {
  505. snd_soc_dapm_new_controls(codec, wm8900_dapm_widgets,
  506. ARRAY_SIZE(wm8900_dapm_widgets));
  507. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  508. return 0;
  509. }
  510. static int wm8900_hw_params(struct snd_pcm_substream *substream,
  511. struct snd_pcm_hw_params *params,
  512. struct snd_soc_dai *dai)
  513. {
  514. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  515. struct snd_soc_device *socdev = rtd->socdev;
  516. struct snd_soc_codec *codec = socdev->card->codec;
  517. u16 reg;
  518. reg = snd_soc_read(codec, WM8900_REG_AUDIO1) & ~0x60;
  519. switch (params_format(params)) {
  520. case SNDRV_PCM_FORMAT_S16_LE:
  521. break;
  522. case SNDRV_PCM_FORMAT_S20_3LE:
  523. reg |= 0x20;
  524. break;
  525. case SNDRV_PCM_FORMAT_S24_LE:
  526. reg |= 0x40;
  527. break;
  528. case SNDRV_PCM_FORMAT_S32_LE:
  529. reg |= 0x60;
  530. break;
  531. default:
  532. return -EINVAL;
  533. }
  534. snd_soc_write(codec, WM8900_REG_AUDIO1, reg);
  535. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  536. reg = snd_soc_read(codec, WM8900_REG_DACCTRL);
  537. if (params_rate(params) <= 24000)
  538. reg |= WM8900_REG_DACCTRL_DAC_SB_FILT;
  539. else
  540. reg &= ~WM8900_REG_DACCTRL_DAC_SB_FILT;
  541. snd_soc_write(codec, WM8900_REG_DACCTRL, reg);
  542. }
  543. return 0;
  544. }
  545. /* FLL divisors */
  546. struct _fll_div {
  547. u16 fll_ratio;
  548. u16 fllclk_div;
  549. u16 fll_slow_lock_ref;
  550. u16 n;
  551. u16 k;
  552. };
  553. /* The size in bits of the FLL divide multiplied by 10
  554. * to allow rounding later */
  555. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  556. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  557. unsigned int Fout)
  558. {
  559. u64 Kpart;
  560. unsigned int K, Ndiv, Nmod, target;
  561. unsigned int div;
  562. BUG_ON(!Fout);
  563. /* The FLL must run at 90-100MHz which is then scaled down to
  564. * the output value by FLLCLK_DIV. */
  565. target = Fout;
  566. div = 1;
  567. while (target < 90000000) {
  568. div *= 2;
  569. target *= 2;
  570. }
  571. if (target > 100000000)
  572. printk(KERN_WARNING "wm8900: FLL rate %u out of range, Fref=%u"
  573. " Fout=%u\n", target, Fref, Fout);
  574. if (div > 32) {
  575. printk(KERN_ERR "wm8900: Invalid FLL division rate %u, "
  576. "Fref=%u, Fout=%u, target=%u\n",
  577. div, Fref, Fout, target);
  578. return -EINVAL;
  579. }
  580. fll_div->fllclk_div = div >> 2;
  581. if (Fref < 48000)
  582. fll_div->fll_slow_lock_ref = 1;
  583. else
  584. fll_div->fll_slow_lock_ref = 0;
  585. Ndiv = target / Fref;
  586. if (Fref < 1000000)
  587. fll_div->fll_ratio = 8;
  588. else
  589. fll_div->fll_ratio = 1;
  590. fll_div->n = Ndiv / fll_div->fll_ratio;
  591. Nmod = (target / fll_div->fll_ratio) % Fref;
  592. /* Calculate fractional part - scale up so we can round. */
  593. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  594. do_div(Kpart, Fref);
  595. K = Kpart & 0xFFFFFFFF;
  596. if ((K % 10) >= 5)
  597. K += 5;
  598. /* Move down to proper range now rounding is done */
  599. fll_div->k = K / 10;
  600. BUG_ON(target != Fout * (fll_div->fllclk_div << 2));
  601. BUG_ON(!K && target != Fref * fll_div->fll_ratio * fll_div->n);
  602. return 0;
  603. }
  604. static int wm8900_set_fll(struct snd_soc_codec *codec,
  605. int fll_id, unsigned int freq_in, unsigned int freq_out)
  606. {
  607. struct wm8900_priv *wm8900 = codec->private_data;
  608. struct _fll_div fll_div;
  609. unsigned int reg;
  610. if (wm8900->fll_in == freq_in && wm8900->fll_out == freq_out)
  611. return 0;
  612. /* The digital side should be disabled during any change. */
  613. reg = snd_soc_read(codec, WM8900_REG_POWER1);
  614. snd_soc_write(codec, WM8900_REG_POWER1,
  615. reg & (~WM8900_REG_POWER1_FLL_ENA));
  616. /* Disable the FLL? */
  617. if (!freq_in || !freq_out) {
  618. reg = snd_soc_read(codec, WM8900_REG_CLOCKING1);
  619. snd_soc_write(codec, WM8900_REG_CLOCKING1,
  620. reg & (~WM8900_REG_CLOCKING1_MCLK_SRC));
  621. reg = snd_soc_read(codec, WM8900_REG_FLLCTL1);
  622. snd_soc_write(codec, WM8900_REG_FLLCTL1,
  623. reg & (~WM8900_REG_FLLCTL1_OSC_ENA));
  624. wm8900->fll_in = freq_in;
  625. wm8900->fll_out = freq_out;
  626. return 0;
  627. }
  628. if (fll_factors(&fll_div, freq_in, freq_out) != 0)
  629. goto reenable;
  630. wm8900->fll_in = freq_in;
  631. wm8900->fll_out = freq_out;
  632. /* The osclilator *MUST* be enabled before we enable the
  633. * digital circuit. */
  634. snd_soc_write(codec, WM8900_REG_FLLCTL1,
  635. fll_div.fll_ratio | WM8900_REG_FLLCTL1_OSC_ENA);
  636. snd_soc_write(codec, WM8900_REG_FLLCTL4, fll_div.n >> 5);
  637. snd_soc_write(codec, WM8900_REG_FLLCTL5,
  638. (fll_div.fllclk_div << 6) | (fll_div.n & 0x1f));
  639. if (fll_div.k) {
  640. snd_soc_write(codec, WM8900_REG_FLLCTL2,
  641. (fll_div.k >> 8) | 0x100);
  642. snd_soc_write(codec, WM8900_REG_FLLCTL3, fll_div.k & 0xff);
  643. } else
  644. snd_soc_write(codec, WM8900_REG_FLLCTL2, 0);
  645. if (fll_div.fll_slow_lock_ref)
  646. snd_soc_write(codec, WM8900_REG_FLLCTL6,
  647. WM8900_REG_FLLCTL6_FLL_SLOW_LOCK_REF);
  648. else
  649. snd_soc_write(codec, WM8900_REG_FLLCTL6, 0);
  650. reg = snd_soc_read(codec, WM8900_REG_POWER1);
  651. snd_soc_write(codec, WM8900_REG_POWER1,
  652. reg | WM8900_REG_POWER1_FLL_ENA);
  653. reenable:
  654. reg = snd_soc_read(codec, WM8900_REG_CLOCKING1);
  655. snd_soc_write(codec, WM8900_REG_CLOCKING1,
  656. reg | WM8900_REG_CLOCKING1_MCLK_SRC);
  657. return 0;
  658. }
  659. static int wm8900_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  660. int source, unsigned int freq_in, unsigned int freq_out)
  661. {
  662. return wm8900_set_fll(codec_dai->codec, pll_id, freq_in, freq_out);
  663. }
  664. static int wm8900_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  665. int div_id, int div)
  666. {
  667. struct snd_soc_codec *codec = codec_dai->codec;
  668. unsigned int reg;
  669. switch (div_id) {
  670. case WM8900_BCLK_DIV:
  671. reg = snd_soc_read(codec, WM8900_REG_CLOCKING1);
  672. snd_soc_write(codec, WM8900_REG_CLOCKING1,
  673. div | (reg & WM8900_REG_CLOCKING1_BCLK_MASK));
  674. break;
  675. case WM8900_OPCLK_DIV:
  676. reg = snd_soc_read(codec, WM8900_REG_CLOCKING1);
  677. snd_soc_write(codec, WM8900_REG_CLOCKING1,
  678. div | (reg & WM8900_REG_CLOCKING1_OPCLK_MASK));
  679. break;
  680. case WM8900_DAC_LRCLK:
  681. reg = snd_soc_read(codec, WM8900_REG_AUDIO4);
  682. snd_soc_write(codec, WM8900_REG_AUDIO4,
  683. div | (reg & WM8900_LRC_MASK));
  684. break;
  685. case WM8900_ADC_LRCLK:
  686. reg = snd_soc_read(codec, WM8900_REG_AUDIO3);
  687. snd_soc_write(codec, WM8900_REG_AUDIO3,
  688. div | (reg & WM8900_LRC_MASK));
  689. break;
  690. case WM8900_DAC_CLKDIV:
  691. reg = snd_soc_read(codec, WM8900_REG_CLOCKING2);
  692. snd_soc_write(codec, WM8900_REG_CLOCKING2,
  693. div | (reg & WM8900_REG_CLOCKING2_DAC_CLKDIV));
  694. break;
  695. case WM8900_ADC_CLKDIV:
  696. reg = snd_soc_read(codec, WM8900_REG_CLOCKING2);
  697. snd_soc_write(codec, WM8900_REG_CLOCKING2,
  698. div | (reg & WM8900_REG_CLOCKING2_ADC_CLKDIV));
  699. break;
  700. case WM8900_LRCLK_MODE:
  701. reg = snd_soc_read(codec, WM8900_REG_DACCTRL);
  702. snd_soc_write(codec, WM8900_REG_DACCTRL,
  703. div | (reg & WM8900_REG_DACCTRL_AIF_LRCLKRATE));
  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 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. struct snd_soc_dai 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. EXPORT_SYMBOL_GPL(wm8900_dai);
  860. static int wm8900_set_bias_level(struct snd_soc_codec *codec,
  861. enum snd_soc_bias_level level)
  862. {
  863. u16 reg;
  864. switch (level) {
  865. case SND_SOC_BIAS_ON:
  866. /* Enable thermal shutdown */
  867. reg = snd_soc_read(codec, WM8900_REG_GPIO);
  868. snd_soc_write(codec, WM8900_REG_GPIO,
  869. reg | WM8900_REG_GPIO_TEMP_ENA);
  870. reg = snd_soc_read(codec, WM8900_REG_ADDCTL);
  871. snd_soc_write(codec, WM8900_REG_ADDCTL,
  872. reg | WM8900_REG_ADDCTL_TEMP_SD);
  873. break;
  874. case SND_SOC_BIAS_PREPARE:
  875. break;
  876. case SND_SOC_BIAS_STANDBY:
  877. /* Charge capacitors if initial power up */
  878. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  879. /* STARTUP_BIAS_ENA on */
  880. snd_soc_write(codec, WM8900_REG_POWER1,
  881. WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  882. /* Startup bias mode */
  883. snd_soc_write(codec, WM8900_REG_ADDCTL,
  884. WM8900_REG_ADDCTL_BIAS_SRC |
  885. WM8900_REG_ADDCTL_VMID_SOFTST);
  886. /* VMID 2x50k */
  887. snd_soc_write(codec, WM8900_REG_POWER1,
  888. WM8900_REG_POWER1_STARTUP_BIAS_ENA | 0x1);
  889. /* Allow capacitors to charge */
  890. schedule_timeout_interruptible(msecs_to_jiffies(400));
  891. /* Enable bias */
  892. snd_soc_write(codec, WM8900_REG_POWER1,
  893. WM8900_REG_POWER1_STARTUP_BIAS_ENA |
  894. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  895. snd_soc_write(codec, WM8900_REG_ADDCTL, 0);
  896. snd_soc_write(codec, WM8900_REG_POWER1,
  897. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  898. }
  899. reg = snd_soc_read(codec, WM8900_REG_POWER1);
  900. snd_soc_write(codec, WM8900_REG_POWER1,
  901. (reg & WM8900_REG_POWER1_FLL_ENA) |
  902. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  903. snd_soc_write(codec, WM8900_REG_POWER2,
  904. WM8900_REG_POWER2_SYSCLK_ENA);
  905. snd_soc_write(codec, WM8900_REG_POWER3, 0);
  906. break;
  907. case SND_SOC_BIAS_OFF:
  908. /* Startup bias enable */
  909. reg = snd_soc_read(codec, WM8900_REG_POWER1);
  910. snd_soc_write(codec, WM8900_REG_POWER1,
  911. reg & WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  912. snd_soc_write(codec, WM8900_REG_ADDCTL,
  913. WM8900_REG_ADDCTL_BIAS_SRC |
  914. WM8900_REG_ADDCTL_VMID_SOFTST);
  915. /* Discharge caps */
  916. snd_soc_write(codec, WM8900_REG_POWER1,
  917. WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  918. schedule_timeout_interruptible(msecs_to_jiffies(500));
  919. /* Remove clamp */
  920. snd_soc_write(codec, WM8900_REG_HPCTL1, 0);
  921. /* Power down */
  922. snd_soc_write(codec, WM8900_REG_ADDCTL, 0);
  923. snd_soc_write(codec, WM8900_REG_POWER1, 0);
  924. snd_soc_write(codec, WM8900_REG_POWER2, 0);
  925. snd_soc_write(codec, WM8900_REG_POWER3, 0);
  926. /* Need to let things settle before stopping the clock
  927. * to ensure that restart works, see "Stopping the
  928. * master clock" in the datasheet. */
  929. schedule_timeout_interruptible(msecs_to_jiffies(1));
  930. snd_soc_write(codec, WM8900_REG_POWER2,
  931. WM8900_REG_POWER2_SYSCLK_ENA);
  932. break;
  933. }
  934. codec->bias_level = level;
  935. return 0;
  936. }
  937. static int wm8900_suspend(struct platform_device *pdev, pm_message_t state)
  938. {
  939. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  940. struct snd_soc_codec *codec = socdev->card->codec;
  941. struct wm8900_priv *wm8900 = codec->private_data;
  942. int fll_out = wm8900->fll_out;
  943. int fll_in = wm8900->fll_in;
  944. int ret;
  945. /* Stop the FLL in an orderly fashion */
  946. ret = wm8900_set_fll(codec, 0, 0, 0);
  947. if (ret != 0) {
  948. dev_err(&pdev->dev, "Failed to stop FLL\n");
  949. return ret;
  950. }
  951. wm8900->fll_out = fll_out;
  952. wm8900->fll_in = fll_in;
  953. wm8900_set_bias_level(codec, SND_SOC_BIAS_OFF);
  954. return 0;
  955. }
  956. static int wm8900_resume(struct platform_device *pdev)
  957. {
  958. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  959. struct snd_soc_codec *codec = socdev->card->codec;
  960. struct wm8900_priv *wm8900 = codec->private_data;
  961. u16 *cache;
  962. int i, ret;
  963. cache = kmemdup(codec->reg_cache, sizeof(wm8900_reg_defaults),
  964. GFP_KERNEL);
  965. wm8900_reset(codec);
  966. wm8900_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  967. /* Restart the FLL? */
  968. if (wm8900->fll_out) {
  969. int fll_out = wm8900->fll_out;
  970. int fll_in = wm8900->fll_in;
  971. wm8900->fll_in = 0;
  972. wm8900->fll_out = 0;
  973. ret = wm8900_set_fll(codec, 0, fll_in, fll_out);
  974. if (ret != 0) {
  975. dev_err(&pdev->dev, "Failed to restart FLL\n");
  976. return ret;
  977. }
  978. }
  979. if (cache) {
  980. for (i = 0; i < WM8900_MAXREG; i++)
  981. snd_soc_write(codec, i, cache[i]);
  982. kfree(cache);
  983. } else
  984. dev_err(&pdev->dev, "Unable to allocate register cache\n");
  985. return 0;
  986. }
  987. static struct snd_soc_codec *wm8900_codec;
  988. static __devinit int wm8900_i2c_probe(struct i2c_client *i2c,
  989. const struct i2c_device_id *id)
  990. {
  991. struct wm8900_priv *wm8900;
  992. struct snd_soc_codec *codec;
  993. unsigned int reg;
  994. int ret;
  995. wm8900 = kzalloc(sizeof(struct wm8900_priv), GFP_KERNEL);
  996. if (wm8900 == NULL)
  997. return -ENOMEM;
  998. codec = &wm8900->codec;
  999. codec->private_data = wm8900;
  1000. codec->reg_cache = &wm8900->reg_cache[0];
  1001. codec->reg_cache_size = WM8900_MAXREG;
  1002. mutex_init(&codec->mutex);
  1003. INIT_LIST_HEAD(&codec->dapm_widgets);
  1004. INIT_LIST_HEAD(&codec->dapm_paths);
  1005. codec->name = "WM8900";
  1006. codec->owner = THIS_MODULE;
  1007. codec->dai = &wm8900_dai;
  1008. codec->num_dai = 1;
  1009. codec->control_data = i2c;
  1010. codec->set_bias_level = wm8900_set_bias_level;
  1011. codec->volatile_register = wm8900_volatile_register;
  1012. codec->dev = &i2c->dev;
  1013. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
  1014. if (ret != 0) {
  1015. dev_err(&i2c->dev, "Failed to set cache I/O: %d\n", ret);
  1016. goto err;
  1017. }
  1018. reg = snd_soc_read(codec, WM8900_REG_ID);
  1019. if (reg != 0x8900) {
  1020. dev_err(&i2c->dev, "Device is not a WM8900 - ID %x\n", reg);
  1021. ret = -ENODEV;
  1022. goto err;
  1023. }
  1024. /* Read back from the chip */
  1025. reg = snd_soc_read(codec, WM8900_REG_POWER1);
  1026. reg = (reg >> 12) & 0xf;
  1027. dev_info(&i2c->dev, "WM8900 revision %d\n", reg);
  1028. wm8900_reset(codec);
  1029. /* Turn the chip on */
  1030. wm8900_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1031. /* Latch the volume update bits */
  1032. snd_soc_write(codec, WM8900_REG_LINVOL,
  1033. snd_soc_read(codec, WM8900_REG_LINVOL) | 0x100);
  1034. snd_soc_write(codec, WM8900_REG_RINVOL,
  1035. snd_soc_read(codec, WM8900_REG_RINVOL) | 0x100);
  1036. snd_soc_write(codec, WM8900_REG_LOUT1CTL,
  1037. snd_soc_read(codec, WM8900_REG_LOUT1CTL) | 0x100);
  1038. snd_soc_write(codec, WM8900_REG_ROUT1CTL,
  1039. snd_soc_read(codec, WM8900_REG_ROUT1CTL) | 0x100);
  1040. snd_soc_write(codec, WM8900_REG_LOUT2CTL,
  1041. snd_soc_read(codec, WM8900_REG_LOUT2CTL) | 0x100);
  1042. snd_soc_write(codec, WM8900_REG_ROUT2CTL,
  1043. snd_soc_read(codec, WM8900_REG_ROUT2CTL) | 0x100);
  1044. snd_soc_write(codec, WM8900_REG_LDAC_DV,
  1045. snd_soc_read(codec, WM8900_REG_LDAC_DV) | 0x100);
  1046. snd_soc_write(codec, WM8900_REG_RDAC_DV,
  1047. snd_soc_read(codec, WM8900_REG_RDAC_DV) | 0x100);
  1048. snd_soc_write(codec, WM8900_REG_LADC_DV,
  1049. snd_soc_read(codec, WM8900_REG_LADC_DV) | 0x100);
  1050. snd_soc_write(codec, WM8900_REG_RADC_DV,
  1051. snd_soc_read(codec, WM8900_REG_RADC_DV) | 0x100);
  1052. /* Set the DAC and mixer output bias */
  1053. snd_soc_write(codec, WM8900_REG_OUTBIASCTL, 0x81);
  1054. wm8900_dai.dev = &i2c->dev;
  1055. wm8900_codec = codec;
  1056. ret = snd_soc_register_codec(codec);
  1057. if (ret != 0) {
  1058. dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
  1059. goto err;
  1060. }
  1061. ret = snd_soc_register_dai(&wm8900_dai);
  1062. if (ret != 0) {
  1063. dev_err(&i2c->dev, "Failed to register DAI: %d\n", ret);
  1064. goto err_codec;
  1065. }
  1066. return ret;
  1067. err_codec:
  1068. snd_soc_unregister_codec(codec);
  1069. err:
  1070. kfree(wm8900);
  1071. wm8900_codec = NULL;
  1072. return ret;
  1073. }
  1074. static __devexit int wm8900_i2c_remove(struct i2c_client *client)
  1075. {
  1076. snd_soc_unregister_dai(&wm8900_dai);
  1077. snd_soc_unregister_codec(wm8900_codec);
  1078. wm8900_set_bias_level(wm8900_codec, SND_SOC_BIAS_OFF);
  1079. wm8900_dai.dev = NULL;
  1080. kfree(wm8900_codec->private_data);
  1081. wm8900_codec = NULL;
  1082. return 0;
  1083. }
  1084. static const struct i2c_device_id wm8900_i2c_id[] = {
  1085. { "wm8900", 0 },
  1086. { }
  1087. };
  1088. MODULE_DEVICE_TABLE(i2c, wm8900_i2c_id);
  1089. static struct i2c_driver wm8900_i2c_driver = {
  1090. .driver = {
  1091. .name = "WM8900",
  1092. .owner = THIS_MODULE,
  1093. },
  1094. .probe = wm8900_i2c_probe,
  1095. .remove = __devexit_p(wm8900_i2c_remove),
  1096. .id_table = wm8900_i2c_id,
  1097. };
  1098. static int wm8900_probe(struct platform_device *pdev)
  1099. {
  1100. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1101. struct snd_soc_codec *codec;
  1102. int ret = 0;
  1103. if (!wm8900_codec) {
  1104. dev_err(&pdev->dev, "I2C client not yet instantiated\n");
  1105. return -ENODEV;
  1106. }
  1107. codec = wm8900_codec;
  1108. socdev->card->codec = codec;
  1109. /* Register pcms */
  1110. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  1111. if (ret < 0) {
  1112. dev_err(&pdev->dev, "Failed to register new PCMs\n");
  1113. goto pcm_err;
  1114. }
  1115. snd_soc_add_controls(codec, wm8900_snd_controls,
  1116. ARRAY_SIZE(wm8900_snd_controls));
  1117. wm8900_add_widgets(codec);
  1118. pcm_err:
  1119. return ret;
  1120. }
  1121. /* power down chip */
  1122. static int wm8900_remove(struct platform_device *pdev)
  1123. {
  1124. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1125. snd_soc_free_pcms(socdev);
  1126. snd_soc_dapm_free(socdev);
  1127. return 0;
  1128. }
  1129. struct snd_soc_codec_device soc_codec_dev_wm8900 = {
  1130. .probe = wm8900_probe,
  1131. .remove = wm8900_remove,
  1132. .suspend = wm8900_suspend,
  1133. .resume = wm8900_resume,
  1134. };
  1135. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8900);
  1136. static int __init wm8900_modinit(void)
  1137. {
  1138. return i2c_add_driver(&wm8900_i2c_driver);
  1139. }
  1140. module_init(wm8900_modinit);
  1141. static void __exit wm8900_exit(void)
  1142. {
  1143. i2c_del_driver(&wm8900_i2c_driver);
  1144. }
  1145. module_exit(wm8900_exit);
  1146. MODULE_DESCRIPTION("ASoC WM8900 driver");
  1147. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
  1148. MODULE_LICENSE("GPL");