wm8900.c 40 KB

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