wm8900.c 39 KB

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