wm8753.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * wm8753.c -- WM8753 ALSA Soc Audio driver
  4. *
  5. * Copyright 2003-11 Wolfson Microelectronics PLC.
  6. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  7. *
  8. * Notes:
  9. * The WM8753 is a low power, high quality stereo codec with integrated PCM
  10. * codec designed for portable digital telephony applications.
  11. *
  12. * Dual DAI:-
  13. *
  14. * This driver support 2 DAI PCM's. This makes the default PCM available for
  15. * HiFi audio (e.g. MP3, ogg) playback/capture and the other PCM available for
  16. * voice.
  17. *
  18. * Please note that the voice PCM can be connected directly to a Bluetooth
  19. * codec or GSM modem and thus cannot be read or written to, although it is
  20. * available to be configured with snd_hw_params(), etc and kcontrols in the
  21. * normal alsa manner.
  22. *
  23. * Fast DAI switching:-
  24. *
  25. * The driver can now fast switch between the DAI configurations via a
  26. * an alsa kcontrol. This allows the PCM to remain open.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/pm.h>
  33. #include <linux/i2c.h>
  34. #include <linux/of_device.h>
  35. #include <linux/regmap.h>
  36. #include <linux/spi/spi.h>
  37. #include <linux/slab.h>
  38. #include <sound/core.h>
  39. #include <sound/pcm.h>
  40. #include <sound/pcm_params.h>
  41. #include <sound/soc.h>
  42. #include <sound/initval.h>
  43. #include <sound/tlv.h>
  44. #include <asm/div64.h>
  45. #include "wm8753.h"
  46. static int caps_charge = 2000;
  47. module_param(caps_charge, int, 0);
  48. MODULE_PARM_DESC(caps_charge, "WM8753 cap charge time (msecs)");
  49. static int wm8753_hifi_write_dai_fmt(struct snd_soc_component *component,
  50. unsigned int fmt);
  51. static int wm8753_voice_write_dai_fmt(struct snd_soc_component *component,
  52. unsigned int fmt);
  53. /*
  54. * wm8753 register cache
  55. * We can't read the WM8753 register space when we
  56. * are using 2 wire for device control, so we cache them instead.
  57. */
  58. static const struct reg_default wm8753_reg_defaults[] = {
  59. { 0x00, 0x0000 },
  60. { 0x01, 0x0008 },
  61. { 0x02, 0x0000 },
  62. { 0x03, 0x000a },
  63. { 0x04, 0x000a },
  64. { 0x05, 0x0033 },
  65. { 0x06, 0x0000 },
  66. { 0x07, 0x0007 },
  67. { 0x08, 0x00ff },
  68. { 0x09, 0x00ff },
  69. { 0x0a, 0x000f },
  70. { 0x0b, 0x000f },
  71. { 0x0c, 0x007b },
  72. { 0x0d, 0x0000 },
  73. { 0x0e, 0x0032 },
  74. { 0x0f, 0x0000 },
  75. { 0x10, 0x00c3 },
  76. { 0x11, 0x00c3 },
  77. { 0x12, 0x00c0 },
  78. { 0x13, 0x0000 },
  79. { 0x14, 0x0000 },
  80. { 0x15, 0x0000 },
  81. { 0x16, 0x0000 },
  82. { 0x17, 0x0000 },
  83. { 0x18, 0x0000 },
  84. { 0x19, 0x0000 },
  85. { 0x1a, 0x0000 },
  86. { 0x1b, 0x0000 },
  87. { 0x1c, 0x0000 },
  88. { 0x1d, 0x0000 },
  89. { 0x1e, 0x0000 },
  90. { 0x1f, 0x0000 },
  91. { 0x20, 0x0055 },
  92. { 0x21, 0x0005 },
  93. { 0x22, 0x0050 },
  94. { 0x23, 0x0055 },
  95. { 0x24, 0x0050 },
  96. { 0x25, 0x0055 },
  97. { 0x26, 0x0050 },
  98. { 0x27, 0x0055 },
  99. { 0x28, 0x0079 },
  100. { 0x29, 0x0079 },
  101. { 0x2a, 0x0079 },
  102. { 0x2b, 0x0079 },
  103. { 0x2c, 0x0079 },
  104. { 0x2d, 0x0000 },
  105. { 0x2e, 0x0000 },
  106. { 0x2f, 0x0000 },
  107. { 0x30, 0x0000 },
  108. { 0x31, 0x0097 },
  109. { 0x32, 0x0097 },
  110. { 0x33, 0x0000 },
  111. { 0x34, 0x0004 },
  112. { 0x35, 0x0000 },
  113. { 0x36, 0x0083 },
  114. { 0x37, 0x0024 },
  115. { 0x38, 0x01ba },
  116. { 0x39, 0x0000 },
  117. { 0x3a, 0x0083 },
  118. { 0x3b, 0x0024 },
  119. { 0x3c, 0x01ba },
  120. { 0x3d, 0x0000 },
  121. { 0x3e, 0x0000 },
  122. { 0x3f, 0x0000 },
  123. };
  124. static bool wm8753_volatile(struct device *dev, unsigned int reg)
  125. {
  126. return reg == WM8753_RESET;
  127. }
  128. /* codec private data */
  129. struct wm8753_priv {
  130. struct regmap *regmap;
  131. unsigned int sysclk;
  132. unsigned int pcmclk;
  133. unsigned int voice_fmt;
  134. unsigned int hifi_fmt;
  135. int dai_func;
  136. struct delayed_work charge_work;
  137. };
  138. #define wm8753_reset(c) snd_soc_component_write(c, WM8753_RESET, 0)
  139. /*
  140. * WM8753 Controls
  141. */
  142. static const char *wm8753_base[] = {"Linear Control", "Adaptive Boost"};
  143. static const char *wm8753_base_filter[] =
  144. {"130Hz @ 48kHz", "200Hz @ 48kHz", "100Hz @ 16kHz", "400Hz @ 48kHz",
  145. "100Hz @ 8kHz", "200Hz @ 8kHz"};
  146. static const char *wm8753_treble[] = {"8kHz", "4kHz"};
  147. static const char *wm8753_alc_func[] = {"Off", "Right", "Left", "Stereo"};
  148. static const char *wm8753_ng_type[] = {"Constant PGA Gain", "Mute ADC Output"};
  149. static const char *wm8753_3d_func[] = {"Capture", "Playback"};
  150. static const char *wm8753_3d_uc[] = {"2.2kHz", "1.5kHz"};
  151. static const char *wm8753_3d_lc[] = {"200Hz", "500Hz"};
  152. static const char *wm8753_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz"};
  153. static const char *wm8753_mono_mix[] = {"Stereo", "Left", "Right", "Mono"};
  154. static const char *wm8753_dac_phase[] = {"Non Inverted", "Inverted"};
  155. static const char *wm8753_line_mix[] = {"Line 1 + 2", "Line 1 - 2",
  156. "Line 1", "Line 2"};
  157. static const char *wm8753_mono_mux[] = {"Line Mix", "Rx Mix"};
  158. static const char *wm8753_right_mux[] = {"Line 2", "Rx Mix"};
  159. static const char *wm8753_left_mux[] = {"Line 1", "Rx Mix"};
  160. static const char *wm8753_rxmsel[] = {"RXP - RXN", "RXP + RXN", "RXP", "RXN"};
  161. static const char *wm8753_sidetone_mux[] = {"Left PGA", "Mic 1", "Mic 2",
  162. "Right PGA"};
  163. static const char *wm8753_mono2_src[] = {"Inverted Mono 1", "Left", "Right",
  164. "Left + Right"};
  165. static const char *wm8753_out3[] = {"VREF", "ROUT2", "Left + Right"};
  166. static const char *wm8753_out4[] = {"VREF", "Capture ST", "LOUT2"};
  167. static const char *wm8753_radcsel[] = {"PGA", "Line or RXP-RXN", "Sidetone"};
  168. static const char *wm8753_ladcsel[] = {"PGA", "Line or RXP-RXN", "Line"};
  169. static const char *wm8753_mono_adc[] = {"Stereo", "Analogue Mix Left",
  170. "Analogue Mix Right", "Digital Mono Mix"};
  171. static const char *wm8753_adc_hp[] = {"3.4Hz @ 48kHz", "82Hz @ 16k",
  172. "82Hz @ 8kHz", "170Hz @ 8kHz"};
  173. static const char *wm8753_adc_filter[] = {"HiFi", "Voice"};
  174. static const char *wm8753_mic_sel[] = {"Mic 1", "Mic 2", "Mic 3"};
  175. static const char *wm8753_dai_mode[] = {"DAI 0", "DAI 1", "DAI 2", "DAI 3"};
  176. static const char *wm8753_dat_sel[] = {"Stereo", "Left ADC", "Right ADC",
  177. "Channel Swap"};
  178. static const char *wm8753_rout2_phase[] = {"Non Inverted", "Inverted"};
  179. static const struct soc_enum wm8753_enum[] = {
  180. SOC_ENUM_SINGLE(WM8753_BASS, 7, 2, wm8753_base),
  181. SOC_ENUM_SINGLE(WM8753_BASS, 4, 6, wm8753_base_filter),
  182. SOC_ENUM_SINGLE(WM8753_TREBLE, 6, 2, wm8753_treble),
  183. SOC_ENUM_SINGLE(WM8753_ALC1, 7, 4, wm8753_alc_func),
  184. SOC_ENUM_SINGLE(WM8753_NGATE, 1, 2, wm8753_ng_type),
  185. SOC_ENUM_SINGLE(WM8753_3D, 7, 2, wm8753_3d_func),
  186. SOC_ENUM_SINGLE(WM8753_3D, 6, 2, wm8753_3d_uc),
  187. SOC_ENUM_SINGLE(WM8753_3D, 5, 2, wm8753_3d_lc),
  188. SOC_ENUM_SINGLE(WM8753_DAC, 1, 4, wm8753_deemp),
  189. SOC_ENUM_SINGLE(WM8753_DAC, 4, 4, wm8753_mono_mix),
  190. SOC_ENUM_SINGLE(WM8753_DAC, 6, 2, wm8753_dac_phase),
  191. SOC_ENUM_SINGLE(WM8753_INCTL1, 3, 4, wm8753_line_mix),
  192. SOC_ENUM_SINGLE(WM8753_INCTL1, 2, 2, wm8753_mono_mux),
  193. SOC_ENUM_SINGLE(WM8753_INCTL1, 1, 2, wm8753_right_mux),
  194. SOC_ENUM_SINGLE(WM8753_INCTL1, 0, 2, wm8753_left_mux),
  195. SOC_ENUM_SINGLE(WM8753_INCTL2, 6, 4, wm8753_rxmsel),
  196. SOC_ENUM_SINGLE(WM8753_INCTL2, 4, 4, wm8753_sidetone_mux),
  197. SOC_ENUM_SINGLE(WM8753_OUTCTL, 7, 4, wm8753_mono2_src),
  198. SOC_ENUM_SINGLE(WM8753_OUTCTL, 0, 3, wm8753_out3),
  199. SOC_ENUM_SINGLE(WM8753_ADCTL2, 7, 3, wm8753_out4),
  200. SOC_ENUM_SINGLE(WM8753_ADCIN, 2, 3, wm8753_radcsel),
  201. SOC_ENUM_SINGLE(WM8753_ADCIN, 0, 3, wm8753_ladcsel),
  202. SOC_ENUM_SINGLE(WM8753_ADCIN, 4, 4, wm8753_mono_adc),
  203. SOC_ENUM_SINGLE(WM8753_ADC, 2, 4, wm8753_adc_hp),
  204. SOC_ENUM_SINGLE(WM8753_ADC, 4, 2, wm8753_adc_filter),
  205. SOC_ENUM_SINGLE(WM8753_MICBIAS, 6, 3, wm8753_mic_sel),
  206. SOC_ENUM_SINGLE(WM8753_IOCTL, 2, 4, wm8753_dai_mode),
  207. SOC_ENUM_SINGLE(WM8753_ADC, 7, 4, wm8753_dat_sel),
  208. SOC_ENUM_SINGLE(WM8753_OUTCTL, 2, 2, wm8753_rout2_phase),
  209. };
  210. static int wm8753_get_dai(struct snd_kcontrol *kcontrol,
  211. struct snd_ctl_elem_value *ucontrol)
  212. {
  213. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  214. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  215. ucontrol->value.enumerated.item[0] = wm8753->dai_func;
  216. return 0;
  217. }
  218. static int wm8753_set_dai(struct snd_kcontrol *kcontrol,
  219. struct snd_ctl_elem_value *ucontrol)
  220. {
  221. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  222. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  223. u16 ioctl;
  224. if (wm8753->dai_func == ucontrol->value.enumerated.item[0])
  225. return 0;
  226. if (snd_soc_component_active(component))
  227. return -EBUSY;
  228. ioctl = snd_soc_component_read(component, WM8753_IOCTL);
  229. wm8753->dai_func = ucontrol->value.enumerated.item[0];
  230. if (((ioctl >> 2) & 0x3) == wm8753->dai_func)
  231. return 1;
  232. ioctl = (ioctl & 0x1f3) | (wm8753->dai_func << 2);
  233. snd_soc_component_write(component, WM8753_IOCTL, ioctl);
  234. wm8753_hifi_write_dai_fmt(component, wm8753->hifi_fmt);
  235. wm8753_voice_write_dai_fmt(component, wm8753->voice_fmt);
  236. return 1;
  237. }
  238. static const DECLARE_TLV_DB_SCALE(rec_mix_tlv, -1500, 300, 0);
  239. static const DECLARE_TLV_DB_SCALE(mic_preamp_tlv, 1200, 600, 0);
  240. static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
  241. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
  242. static const DECLARE_TLV_DB_RANGE(out_tlv,
  243. /* 0000000 - 0101111 = "Analogue mute" */
  244. 0, 48, TLV_DB_SCALE_ITEM(-25500, 0, 0),
  245. 48, 127, TLV_DB_SCALE_ITEM(-7300, 100, 0)
  246. );
  247. static const DECLARE_TLV_DB_SCALE(mix_tlv, -1500, 300, 0);
  248. static const DECLARE_TLV_DB_SCALE(voice_mix_tlv, -1200, 300, 0);
  249. static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
  250. static const struct snd_kcontrol_new wm8753_snd_controls[] = {
  251. SOC_SINGLE("Hi-Fi DAC Left/Right channel Swap", WM8753_HIFI, 5, 1, 0),
  252. SOC_DOUBLE_R_TLV("PCM Volume", WM8753_LDAC, WM8753_RDAC, 0, 255, 0, dac_tlv),
  253. SOC_DOUBLE_R_TLV("ADC Capture Volume", WM8753_LADC, WM8753_RADC, 0, 255, 0,
  254. adc_tlv),
  255. SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8753_LOUT1V, WM8753_ROUT1V,
  256. 0, 127, 0, out_tlv),
  257. SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8753_LOUT2V, WM8753_ROUT2V, 0,
  258. 127, 0, out_tlv),
  259. SOC_SINGLE_TLV("Mono Playback Volume", WM8753_MOUTV, 0, 127, 0, out_tlv),
  260. SOC_DOUBLE_R_TLV("Bypass Playback Volume", WM8753_LOUTM1, WM8753_ROUTM1, 4, 7,
  261. 1, mix_tlv),
  262. SOC_DOUBLE_R_TLV("Sidetone Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 4,
  263. 7, 1, mix_tlv),
  264. SOC_DOUBLE_R_TLV("Voice Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 0, 7,
  265. 1, voice_mix_tlv),
  266. SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8753_LOUT1V, WM8753_ROUT1V, 7,
  267. 1, 0),
  268. SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8753_LOUT2V, WM8753_ROUT2V, 7,
  269. 1, 0),
  270. SOC_SINGLE_TLV("Mono Bypass Playback Volume", WM8753_MOUTM1, 4, 7, 1, mix_tlv),
  271. SOC_SINGLE_TLV("Mono Sidetone Playback Volume", WM8753_MOUTM2, 4, 7, 1,
  272. mix_tlv),
  273. SOC_SINGLE_TLV("Mono Voice Playback Volume", WM8753_MOUTM2, 0, 7, 1,
  274. voice_mix_tlv),
  275. SOC_SINGLE("Mono Playback ZC Switch", WM8753_MOUTV, 7, 1, 0),
  276. SOC_ENUM("Bass Boost", wm8753_enum[0]),
  277. SOC_ENUM("Bass Filter", wm8753_enum[1]),
  278. SOC_SINGLE("Bass Volume", WM8753_BASS, 0, 15, 1),
  279. SOC_SINGLE("Treble Volume", WM8753_TREBLE, 0, 15, 1),
  280. SOC_ENUM("Treble Cut-off", wm8753_enum[2]),
  281. SOC_DOUBLE_TLV("Sidetone Capture Volume", WM8753_RECMIX1, 0, 4, 7, 1,
  282. rec_mix_tlv),
  283. SOC_SINGLE_TLV("Voice Sidetone Capture Volume", WM8753_RECMIX2, 0, 7, 1,
  284. rec_mix_tlv),
  285. SOC_DOUBLE_R_TLV("Capture Volume", WM8753_LINVOL, WM8753_RINVOL, 0, 63, 0,
  286. pga_tlv),
  287. SOC_DOUBLE_R("Capture ZC Switch", WM8753_LINVOL, WM8753_RINVOL, 6, 1, 0),
  288. SOC_DOUBLE_R("Capture Switch", WM8753_LINVOL, WM8753_RINVOL, 7, 1, 1),
  289. SOC_ENUM("Capture Filter Select", wm8753_enum[23]),
  290. SOC_ENUM("Capture Filter Cut-off", wm8753_enum[24]),
  291. SOC_SINGLE("Capture Filter Switch", WM8753_ADC, 0, 1, 1),
  292. SOC_SINGLE("ALC Capture Target Volume", WM8753_ALC1, 0, 7, 0),
  293. SOC_SINGLE("ALC Capture Max Volume", WM8753_ALC1, 4, 7, 0),
  294. SOC_ENUM("ALC Capture Function", wm8753_enum[3]),
  295. SOC_SINGLE("ALC Capture ZC Switch", WM8753_ALC2, 8, 1, 0),
  296. SOC_SINGLE("ALC Capture Hold Time", WM8753_ALC2, 0, 15, 1),
  297. SOC_SINGLE("ALC Capture Decay Time", WM8753_ALC3, 4, 15, 1),
  298. SOC_SINGLE("ALC Capture Attack Time", WM8753_ALC3, 0, 15, 0),
  299. SOC_SINGLE("ALC Capture NG Threshold", WM8753_NGATE, 3, 31, 0),
  300. SOC_ENUM("ALC Capture NG Type", wm8753_enum[4]),
  301. SOC_SINGLE("ALC Capture NG Switch", WM8753_NGATE, 0, 1, 0),
  302. SOC_ENUM("3D Function", wm8753_enum[5]),
  303. SOC_ENUM("3D Upper Cut-off", wm8753_enum[6]),
  304. SOC_ENUM("3D Lower Cut-off", wm8753_enum[7]),
  305. SOC_SINGLE("3D Volume", WM8753_3D, 1, 15, 0),
  306. SOC_SINGLE("3D Switch", WM8753_3D, 0, 1, 0),
  307. SOC_SINGLE("Capture 6dB Attenuate", WM8753_ADCTL1, 2, 1, 0),
  308. SOC_SINGLE("Playback 6dB Attenuate", WM8753_ADCTL1, 1, 1, 0),
  309. SOC_ENUM("De-emphasis", wm8753_enum[8]),
  310. SOC_ENUM("Playback Mono Mix", wm8753_enum[9]),
  311. SOC_ENUM("Playback Phase", wm8753_enum[10]),
  312. SOC_SINGLE_TLV("Mic2 Capture Volume", WM8753_INCTL1, 7, 3, 0, mic_preamp_tlv),
  313. SOC_SINGLE_TLV("Mic1 Capture Volume", WM8753_INCTL1, 5, 3, 0, mic_preamp_tlv),
  314. SOC_ENUM_EXT("DAI Mode", wm8753_enum[26], wm8753_get_dai, wm8753_set_dai),
  315. SOC_ENUM("ADC Data Select", wm8753_enum[27]),
  316. SOC_ENUM("ROUT2 Phase", wm8753_enum[28]),
  317. };
  318. /*
  319. * _DAPM_ Controls
  320. */
  321. /* Left Mixer */
  322. static const struct snd_kcontrol_new wm8753_left_mixer_controls[] = {
  323. SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_LOUTM2, 8, 1, 0),
  324. SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_LOUTM2, 7, 1, 0),
  325. SOC_DAPM_SINGLE("Left Playback Switch", WM8753_LOUTM1, 8, 1, 0),
  326. SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_LOUTM1, 7, 1, 0),
  327. };
  328. /* Right mixer */
  329. static const struct snd_kcontrol_new wm8753_right_mixer_controls[] = {
  330. SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_ROUTM2, 8, 1, 0),
  331. SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_ROUTM2, 7, 1, 0),
  332. SOC_DAPM_SINGLE("Right Playback Switch", WM8753_ROUTM1, 8, 1, 0),
  333. SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_ROUTM1, 7, 1, 0),
  334. };
  335. /* Mono mixer */
  336. static const struct snd_kcontrol_new wm8753_mono_mixer_controls[] = {
  337. SOC_DAPM_SINGLE("Left Playback Switch", WM8753_MOUTM1, 8, 1, 0),
  338. SOC_DAPM_SINGLE("Right Playback Switch", WM8753_MOUTM2, 8, 1, 0),
  339. SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_MOUTM2, 3, 1, 0),
  340. SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_MOUTM2, 7, 1, 0),
  341. SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_MOUTM1, 7, 1, 0),
  342. };
  343. /* Mono 2 Mux */
  344. static const struct snd_kcontrol_new wm8753_mono2_controls =
  345. SOC_DAPM_ENUM("Route", wm8753_enum[17]);
  346. /* Out 3 Mux */
  347. static const struct snd_kcontrol_new wm8753_out3_controls =
  348. SOC_DAPM_ENUM("Route", wm8753_enum[18]);
  349. /* Out 4 Mux */
  350. static const struct snd_kcontrol_new wm8753_out4_controls =
  351. SOC_DAPM_ENUM("Route", wm8753_enum[19]);
  352. /* ADC Mono Mix */
  353. static const struct snd_kcontrol_new wm8753_adc_mono_controls =
  354. SOC_DAPM_ENUM("Route", wm8753_enum[22]);
  355. /* Record mixer */
  356. static const struct snd_kcontrol_new wm8753_record_mixer_controls[] = {
  357. SOC_DAPM_SINGLE("Voice Capture Switch", WM8753_RECMIX2, 3, 1, 0),
  358. SOC_DAPM_SINGLE("Left Capture Switch", WM8753_RECMIX1, 3, 1, 0),
  359. SOC_DAPM_SINGLE("Right Capture Switch", WM8753_RECMIX1, 7, 1, 0),
  360. };
  361. /* Left ADC mux */
  362. static const struct snd_kcontrol_new wm8753_adc_left_controls =
  363. SOC_DAPM_ENUM("Route", wm8753_enum[21]);
  364. /* Right ADC mux */
  365. static const struct snd_kcontrol_new wm8753_adc_right_controls =
  366. SOC_DAPM_ENUM("Route", wm8753_enum[20]);
  367. /* MIC mux */
  368. static const struct snd_kcontrol_new wm8753_mic_mux_controls =
  369. SOC_DAPM_ENUM("Route", wm8753_enum[16]);
  370. /* ALC mixer */
  371. static const struct snd_kcontrol_new wm8753_alc_mixer_controls[] = {
  372. SOC_DAPM_SINGLE("Line Capture Switch", WM8753_INCTL2, 3, 1, 0),
  373. SOC_DAPM_SINGLE("Mic2 Capture Switch", WM8753_INCTL2, 2, 1, 0),
  374. SOC_DAPM_SINGLE("Mic1 Capture Switch", WM8753_INCTL2, 1, 1, 0),
  375. SOC_DAPM_SINGLE("Rx Capture Switch", WM8753_INCTL2, 0, 1, 0),
  376. };
  377. /* Left Line mux */
  378. static const struct snd_kcontrol_new wm8753_line_left_controls =
  379. SOC_DAPM_ENUM("Route", wm8753_enum[14]);
  380. /* Right Line mux */
  381. static const struct snd_kcontrol_new wm8753_line_right_controls =
  382. SOC_DAPM_ENUM("Route", wm8753_enum[13]);
  383. /* Mono Line mux */
  384. static const struct snd_kcontrol_new wm8753_line_mono_controls =
  385. SOC_DAPM_ENUM("Route", wm8753_enum[12]);
  386. /* Line mux and mixer */
  387. static const struct snd_kcontrol_new wm8753_line_mux_mix_controls =
  388. SOC_DAPM_ENUM("Route", wm8753_enum[11]);
  389. /* Rx mux and mixer */
  390. static const struct snd_kcontrol_new wm8753_rx_mux_mix_controls =
  391. SOC_DAPM_ENUM("Route", wm8753_enum[15]);
  392. /* Mic Selector Mux */
  393. static const struct snd_kcontrol_new wm8753_mic_sel_mux_controls =
  394. SOC_DAPM_ENUM("Route", wm8753_enum[25]);
  395. static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
  396. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8753_PWR1, 5, 0),
  397. SND_SOC_DAPM_MIXER("Left Mixer", WM8753_PWR4, 0, 0,
  398. &wm8753_left_mixer_controls[0], ARRAY_SIZE(wm8753_left_mixer_controls)),
  399. SND_SOC_DAPM_PGA("Left Out 1", WM8753_PWR3, 8, 0, NULL, 0),
  400. SND_SOC_DAPM_PGA("Left Out 2", WM8753_PWR3, 6, 0, NULL, 0),
  401. SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", WM8753_PWR1, 3, 0),
  402. SND_SOC_DAPM_OUTPUT("LOUT1"),
  403. SND_SOC_DAPM_OUTPUT("LOUT2"),
  404. SND_SOC_DAPM_MIXER("Right Mixer", WM8753_PWR4, 1, 0,
  405. &wm8753_right_mixer_controls[0], ARRAY_SIZE(wm8753_right_mixer_controls)),
  406. SND_SOC_DAPM_PGA("Right Out 1", WM8753_PWR3, 7, 0, NULL, 0),
  407. SND_SOC_DAPM_PGA("Right Out 2", WM8753_PWR3, 5, 0, NULL, 0),
  408. SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", WM8753_PWR1, 2, 0),
  409. SND_SOC_DAPM_OUTPUT("ROUT1"),
  410. SND_SOC_DAPM_OUTPUT("ROUT2"),
  411. SND_SOC_DAPM_MIXER("Mono Mixer", WM8753_PWR4, 2, 0,
  412. &wm8753_mono_mixer_controls[0], ARRAY_SIZE(wm8753_mono_mixer_controls)),
  413. SND_SOC_DAPM_PGA("Mono Out 1", WM8753_PWR3, 2, 0, NULL, 0),
  414. SND_SOC_DAPM_PGA("Mono Out 2", WM8753_PWR3, 1, 0, NULL, 0),
  415. SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", WM8753_PWR1, 4, 0),
  416. SND_SOC_DAPM_OUTPUT("MONO1"),
  417. SND_SOC_DAPM_MUX("Mono 2 Mux", SND_SOC_NOPM, 0, 0, &wm8753_mono2_controls),
  418. SND_SOC_DAPM_OUTPUT("MONO2"),
  419. SND_SOC_DAPM_MIXER("Out3 Left + Right", SND_SOC_NOPM, 0, 0, NULL, 0),
  420. SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out3_controls),
  421. SND_SOC_DAPM_PGA("Out 3", WM8753_PWR3, 4, 0, NULL, 0),
  422. SND_SOC_DAPM_OUTPUT("OUT3"),
  423. SND_SOC_DAPM_MUX("Out4 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out4_controls),
  424. SND_SOC_DAPM_PGA("Out 4", WM8753_PWR3, 3, 0, NULL, 0),
  425. SND_SOC_DAPM_OUTPUT("OUT4"),
  426. SND_SOC_DAPM_MIXER("Playback Mixer", WM8753_PWR4, 3, 0,
  427. &wm8753_record_mixer_controls[0],
  428. ARRAY_SIZE(wm8753_record_mixer_controls)),
  429. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8753_PWR2, 3, 0),
  430. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8753_PWR2, 2, 0),
  431. SND_SOC_DAPM_MUX("Capture Left Mixer", SND_SOC_NOPM, 0, 0,
  432. &wm8753_adc_mono_controls),
  433. SND_SOC_DAPM_MUX("Capture Right Mixer", SND_SOC_NOPM, 0, 0,
  434. &wm8753_adc_mono_controls),
  435. SND_SOC_DAPM_MUX("Capture Left Mux", SND_SOC_NOPM, 0, 0,
  436. &wm8753_adc_left_controls),
  437. SND_SOC_DAPM_MUX("Capture Right Mux", SND_SOC_NOPM, 0, 0,
  438. &wm8753_adc_right_controls),
  439. SND_SOC_DAPM_MUX("Mic Sidetone Mux", SND_SOC_NOPM, 0, 0,
  440. &wm8753_mic_mux_controls),
  441. SND_SOC_DAPM_PGA("Left Capture Volume", WM8753_PWR2, 5, 0, NULL, 0),
  442. SND_SOC_DAPM_PGA("Right Capture Volume", WM8753_PWR2, 4, 0, NULL, 0),
  443. SND_SOC_DAPM_MIXER("ALC Mixer", WM8753_PWR2, 6, 0,
  444. &wm8753_alc_mixer_controls[0], ARRAY_SIZE(wm8753_alc_mixer_controls)),
  445. SND_SOC_DAPM_MUX("Line Left Mux", SND_SOC_NOPM, 0, 0,
  446. &wm8753_line_left_controls),
  447. SND_SOC_DAPM_MUX("Line Right Mux", SND_SOC_NOPM, 0, 0,
  448. &wm8753_line_right_controls),
  449. SND_SOC_DAPM_MUX("Line Mono Mux", SND_SOC_NOPM, 0, 0,
  450. &wm8753_line_mono_controls),
  451. SND_SOC_DAPM_MUX("Line Mixer", WM8753_PWR2, 0, 0,
  452. &wm8753_line_mux_mix_controls),
  453. SND_SOC_DAPM_MUX("Rx Mixer", WM8753_PWR2, 1, 0,
  454. &wm8753_rx_mux_mix_controls),
  455. SND_SOC_DAPM_PGA("Mic 1 Volume", WM8753_PWR2, 8, 0, NULL, 0),
  456. SND_SOC_DAPM_PGA("Mic 2 Volume", WM8753_PWR2, 7, 0, NULL, 0),
  457. SND_SOC_DAPM_MUX("Mic Selection Mux", SND_SOC_NOPM, 0, 0,
  458. &wm8753_mic_sel_mux_controls),
  459. SND_SOC_DAPM_INPUT("LINE1"),
  460. SND_SOC_DAPM_INPUT("LINE2"),
  461. SND_SOC_DAPM_INPUT("RXP"),
  462. SND_SOC_DAPM_INPUT("RXN"),
  463. SND_SOC_DAPM_INPUT("ACIN"),
  464. SND_SOC_DAPM_OUTPUT("ACOP"),
  465. SND_SOC_DAPM_INPUT("MIC1N"),
  466. SND_SOC_DAPM_INPUT("MIC1"),
  467. SND_SOC_DAPM_INPUT("MIC2N"),
  468. SND_SOC_DAPM_INPUT("MIC2"),
  469. SND_SOC_DAPM_VMID("VREF"),
  470. };
  471. static const struct snd_soc_dapm_route wm8753_dapm_routes[] = {
  472. /* left mixer */
  473. {"Left Mixer", "Left Playback Switch", "Left DAC"},
  474. {"Left Mixer", "Voice Playback Switch", "Voice DAC"},
  475. {"Left Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
  476. {"Left Mixer", "Bypass Playback Switch", "Line Left Mux"},
  477. /* right mixer */
  478. {"Right Mixer", "Right Playback Switch", "Right DAC"},
  479. {"Right Mixer", "Voice Playback Switch", "Voice DAC"},
  480. {"Right Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
  481. {"Right Mixer", "Bypass Playback Switch", "Line Right Mux"},
  482. /* mono mixer */
  483. {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
  484. {"Mono Mixer", "Left Playback Switch", "Left DAC"},
  485. {"Mono Mixer", "Right Playback Switch", "Right DAC"},
  486. {"Mono Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
  487. {"Mono Mixer", "Bypass Playback Switch", "Line Mono Mux"},
  488. /* left out */
  489. {"Left Out 1", NULL, "Left Mixer"},
  490. {"Left Out 2", NULL, "Left Mixer"},
  491. {"LOUT1", NULL, "Left Out 1"},
  492. {"LOUT2", NULL, "Left Out 2"},
  493. /* right out */
  494. {"Right Out 1", NULL, "Right Mixer"},
  495. {"Right Out 2", NULL, "Right Mixer"},
  496. {"ROUT1", NULL, "Right Out 1"},
  497. {"ROUT2", NULL, "Right Out 2"},
  498. /* mono 1 out */
  499. {"Mono Out 1", NULL, "Mono Mixer"},
  500. {"MONO1", NULL, "Mono Out 1"},
  501. /* mono 2 out */
  502. {"Mono 2 Mux", "Left + Right", "Out3 Left + Right"},
  503. {"Mono 2 Mux", "Inverted Mono 1", "MONO1"},
  504. {"Mono 2 Mux", "Left", "Left Mixer"},
  505. {"Mono 2 Mux", "Right", "Right Mixer"},
  506. {"Mono Out 2", NULL, "Mono 2 Mux"},
  507. {"MONO2", NULL, "Mono Out 2"},
  508. /* out 3 */
  509. {"Out3 Left + Right", NULL, "Left Mixer"},
  510. {"Out3 Left + Right", NULL, "Right Mixer"},
  511. {"Out3 Mux", "VREF", "VREF"},
  512. {"Out3 Mux", "Left + Right", "Out3 Left + Right"},
  513. {"Out3 Mux", "ROUT2", "ROUT2"},
  514. {"Out 3", NULL, "Out3 Mux"},
  515. {"OUT3", NULL, "Out 3"},
  516. /* out 4 */
  517. {"Out4 Mux", "VREF", "VREF"},
  518. {"Out4 Mux", "Capture ST", "Playback Mixer"},
  519. {"Out4 Mux", "LOUT2", "LOUT2"},
  520. {"Out 4", NULL, "Out4 Mux"},
  521. {"OUT4", NULL, "Out 4"},
  522. /* record mixer */
  523. {"Playback Mixer", "Left Capture Switch", "Left Mixer"},
  524. {"Playback Mixer", "Voice Capture Switch", "Mono Mixer"},
  525. {"Playback Mixer", "Right Capture Switch", "Right Mixer"},
  526. /* Mic/SideTone Mux */
  527. {"Mic Sidetone Mux", "Left PGA", "Left Capture Volume"},
  528. {"Mic Sidetone Mux", "Right PGA", "Right Capture Volume"},
  529. {"Mic Sidetone Mux", "Mic 1", "Mic 1 Volume"},
  530. {"Mic Sidetone Mux", "Mic 2", "Mic 2 Volume"},
  531. /* Capture Left Mux */
  532. {"Capture Left Mux", "PGA", "Left Capture Volume"},
  533. {"Capture Left Mux", "Line or RXP-RXN", "Line Left Mux"},
  534. {"Capture Left Mux", "Line", "LINE1"},
  535. /* Capture Right Mux */
  536. {"Capture Right Mux", "PGA", "Right Capture Volume"},
  537. {"Capture Right Mux", "Line or RXP-RXN", "Line Right Mux"},
  538. {"Capture Right Mux", "Sidetone", "Playback Mixer"},
  539. /* Mono Capture mixer-mux */
  540. {"Capture Right Mixer", "Stereo", "Capture Right Mux"},
  541. {"Capture Left Mixer", "Stereo", "Capture Left Mux"},
  542. {"Capture Left Mixer", "Analogue Mix Left", "Capture Left Mux"},
  543. {"Capture Left Mixer", "Analogue Mix Left", "Capture Right Mux"},
  544. {"Capture Right Mixer", "Analogue Mix Right", "Capture Left Mux"},
  545. {"Capture Right Mixer", "Analogue Mix Right", "Capture Right Mux"},
  546. {"Capture Left Mixer", "Digital Mono Mix", "Capture Left Mux"},
  547. {"Capture Left Mixer", "Digital Mono Mix", "Capture Right Mux"},
  548. {"Capture Right Mixer", "Digital Mono Mix", "Capture Left Mux"},
  549. {"Capture Right Mixer", "Digital Mono Mix", "Capture Right Mux"},
  550. /* ADC */
  551. {"Left ADC", NULL, "Capture Left Mixer"},
  552. {"Right ADC", NULL, "Capture Right Mixer"},
  553. /* Left Capture Volume */
  554. {"Left Capture Volume", NULL, "ACIN"},
  555. /* Right Capture Volume */
  556. {"Right Capture Volume", NULL, "Mic 2 Volume"},
  557. /* ALC Mixer */
  558. {"ALC Mixer", "Line Capture Switch", "Line Mixer"},
  559. {"ALC Mixer", "Mic2 Capture Switch", "Mic 2 Volume"},
  560. {"ALC Mixer", "Mic1 Capture Switch", "Mic 1 Volume"},
  561. {"ALC Mixer", "Rx Capture Switch", "Rx Mixer"},
  562. /* Line Left Mux */
  563. {"Line Left Mux", "Line 1", "LINE1"},
  564. {"Line Left Mux", "Rx Mix", "Rx Mixer"},
  565. /* Line Right Mux */
  566. {"Line Right Mux", "Line 2", "LINE2"},
  567. {"Line Right Mux", "Rx Mix", "Rx Mixer"},
  568. /* Line Mono Mux */
  569. {"Line Mono Mux", "Line Mix", "Line Mixer"},
  570. {"Line Mono Mux", "Rx Mix", "Rx Mixer"},
  571. /* Line Mixer/Mux */
  572. {"Line Mixer", "Line 1 + 2", "LINE1"},
  573. {"Line Mixer", "Line 1 - 2", "LINE1"},
  574. {"Line Mixer", "Line 1 + 2", "LINE2"},
  575. {"Line Mixer", "Line 1 - 2", "LINE2"},
  576. {"Line Mixer", "Line 1", "LINE1"},
  577. {"Line Mixer", "Line 2", "LINE2"},
  578. /* Rx Mixer/Mux */
  579. {"Rx Mixer", "RXP - RXN", "RXP"},
  580. {"Rx Mixer", "RXP + RXN", "RXP"},
  581. {"Rx Mixer", "RXP - RXN", "RXN"},
  582. {"Rx Mixer", "RXP + RXN", "RXN"},
  583. {"Rx Mixer", "RXP", "RXP"},
  584. {"Rx Mixer", "RXN", "RXN"},
  585. /* Mic 1 Volume */
  586. {"Mic 1 Volume", NULL, "MIC1N"},
  587. {"Mic 1 Volume", NULL, "Mic Selection Mux"},
  588. /* Mic 2 Volume */
  589. {"Mic 2 Volume", NULL, "MIC2N"},
  590. {"Mic 2 Volume", NULL, "MIC2"},
  591. /* Mic Selector Mux */
  592. {"Mic Selection Mux", "Mic 1", "MIC1"},
  593. {"Mic Selection Mux", "Mic 2", "MIC2N"},
  594. {"Mic Selection Mux", "Mic 3", "MIC2"},
  595. /* ACOP */
  596. {"ACOP", NULL, "ALC Mixer"},
  597. };
  598. /* PLL divisors */
  599. struct _pll_div {
  600. u32 div2:1;
  601. u32 n:4;
  602. u32 k:24;
  603. };
  604. /* The size in bits of the pll divide multiplied by 10
  605. * to allow rounding later */
  606. #define FIXED_PLL_SIZE ((1 << 22) * 10)
  607. static void pll_factors(struct _pll_div *pll_div, unsigned int target,
  608. unsigned int source)
  609. {
  610. u64 Kpart;
  611. unsigned int K, Ndiv, Nmod;
  612. Ndiv = target / source;
  613. if (Ndiv < 6) {
  614. source >>= 1;
  615. pll_div->div2 = 1;
  616. Ndiv = target / source;
  617. } else
  618. pll_div->div2 = 0;
  619. if ((Ndiv < 6) || (Ndiv > 12))
  620. printk(KERN_WARNING
  621. "wm8753: unsupported N = %u\n", Ndiv);
  622. pll_div->n = Ndiv;
  623. Nmod = target % source;
  624. Kpart = FIXED_PLL_SIZE * (long long)Nmod;
  625. do_div(Kpart, source);
  626. K = Kpart & 0xFFFFFFFF;
  627. /* Check if we need to round */
  628. if ((K % 10) >= 5)
  629. K += 5;
  630. /* Move down to proper range now rounding is done */
  631. K /= 10;
  632. pll_div->k = K;
  633. }
  634. static int wm8753_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  635. int source, unsigned int freq_in, unsigned int freq_out)
  636. {
  637. u16 reg, enable;
  638. int offset;
  639. struct snd_soc_component *component = codec_dai->component;
  640. if (pll_id < WM8753_PLL1 || pll_id > WM8753_PLL2)
  641. return -ENODEV;
  642. if (pll_id == WM8753_PLL1) {
  643. offset = 0;
  644. enable = 0x10;
  645. reg = snd_soc_component_read(component, WM8753_CLOCK) & 0xffef;
  646. } else {
  647. offset = 4;
  648. enable = 0x8;
  649. reg = snd_soc_component_read(component, WM8753_CLOCK) & 0xfff7;
  650. }
  651. if (!freq_in || !freq_out) {
  652. /* disable PLL */
  653. snd_soc_component_write(component, WM8753_PLL1CTL1 + offset, 0x0026);
  654. snd_soc_component_write(component, WM8753_CLOCK, reg);
  655. return 0;
  656. } else {
  657. u16 value = 0;
  658. struct _pll_div pll_div;
  659. pll_factors(&pll_div, freq_out * 8, freq_in);
  660. /* set up N and K PLL divisor ratios */
  661. /* bits 8:5 = PLL_N, bits 3:0 = PLL_K[21:18] */
  662. value = (pll_div.n << 5) + ((pll_div.k & 0x3c0000) >> 18);
  663. snd_soc_component_write(component, WM8753_PLL1CTL2 + offset, value);
  664. /* bits 8:0 = PLL_K[17:9] */
  665. value = (pll_div.k & 0x03fe00) >> 9;
  666. snd_soc_component_write(component, WM8753_PLL1CTL3 + offset, value);
  667. /* bits 8:0 = PLL_K[8:0] */
  668. value = pll_div.k & 0x0001ff;
  669. snd_soc_component_write(component, WM8753_PLL1CTL4 + offset, value);
  670. /* set PLL as input and enable */
  671. snd_soc_component_write(component, WM8753_PLL1CTL1 + offset, 0x0027 |
  672. (pll_div.div2 << 3));
  673. snd_soc_component_write(component, WM8753_CLOCK, reg | enable);
  674. }
  675. return 0;
  676. }
  677. struct _coeff_div {
  678. u32 mclk;
  679. u32 rate;
  680. u8 sr:5;
  681. u8 usb:1;
  682. };
  683. /* codec hifi mclk (after PLL) clock divider coefficients */
  684. static const struct _coeff_div coeff_div[] = {
  685. /* 8k */
  686. {12288000, 8000, 0x6, 0x0},
  687. {11289600, 8000, 0x16, 0x0},
  688. {18432000, 8000, 0x7, 0x0},
  689. {16934400, 8000, 0x17, 0x0},
  690. {12000000, 8000, 0x6, 0x1},
  691. /* 11.025k */
  692. {11289600, 11025, 0x18, 0x0},
  693. {16934400, 11025, 0x19, 0x0},
  694. {12000000, 11025, 0x19, 0x1},
  695. /* 16k */
  696. {12288000, 16000, 0xa, 0x0},
  697. {18432000, 16000, 0xb, 0x0},
  698. {12000000, 16000, 0xa, 0x1},
  699. /* 22.05k */
  700. {11289600, 22050, 0x1a, 0x0},
  701. {16934400, 22050, 0x1b, 0x0},
  702. {12000000, 22050, 0x1b, 0x1},
  703. /* 32k */
  704. {12288000, 32000, 0xc, 0x0},
  705. {18432000, 32000, 0xd, 0x0},
  706. {12000000, 32000, 0xa, 0x1},
  707. /* 44.1k */
  708. {11289600, 44100, 0x10, 0x0},
  709. {16934400, 44100, 0x11, 0x0},
  710. {12000000, 44100, 0x11, 0x1},
  711. /* 48k */
  712. {12288000, 48000, 0x0, 0x0},
  713. {18432000, 48000, 0x1, 0x0},
  714. {12000000, 48000, 0x0, 0x1},
  715. /* 88.2k */
  716. {11289600, 88200, 0x1e, 0x0},
  717. {16934400, 88200, 0x1f, 0x0},
  718. {12000000, 88200, 0x1f, 0x1},
  719. /* 96k */
  720. {12288000, 96000, 0xe, 0x0},
  721. {18432000, 96000, 0xf, 0x0},
  722. {12000000, 96000, 0xe, 0x1},
  723. };
  724. static int get_coeff(int mclk, int rate)
  725. {
  726. int i;
  727. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  728. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  729. return i;
  730. }
  731. return -EINVAL;
  732. }
  733. /*
  734. * Clock after PLL and dividers
  735. */
  736. static int wm8753_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  737. int clk_id, unsigned int freq, int dir)
  738. {
  739. struct snd_soc_component *component = codec_dai->component;
  740. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  741. switch (freq) {
  742. case 11289600:
  743. case 12000000:
  744. case 12288000:
  745. case 16934400:
  746. case 18432000:
  747. if (clk_id == WM8753_MCLK) {
  748. wm8753->sysclk = freq;
  749. return 0;
  750. } else if (clk_id == WM8753_PCMCLK) {
  751. wm8753->pcmclk = freq;
  752. return 0;
  753. }
  754. break;
  755. }
  756. return -EINVAL;
  757. }
  758. /*
  759. * Set's ADC and Voice DAC format.
  760. */
  761. static int wm8753_vdac_adc_set_dai_fmt(struct snd_soc_component *component,
  762. unsigned int fmt)
  763. {
  764. u16 voice = snd_soc_component_read(component, WM8753_PCM) & 0x01ec;
  765. /* interface format */
  766. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  767. case SND_SOC_DAIFMT_I2S:
  768. voice |= 0x0002;
  769. break;
  770. case SND_SOC_DAIFMT_RIGHT_J:
  771. break;
  772. case SND_SOC_DAIFMT_LEFT_J:
  773. voice |= 0x0001;
  774. break;
  775. case SND_SOC_DAIFMT_DSP_A:
  776. voice |= 0x0003;
  777. break;
  778. case SND_SOC_DAIFMT_DSP_B:
  779. voice |= 0x0013;
  780. break;
  781. default:
  782. return -EINVAL;
  783. }
  784. snd_soc_component_write(component, WM8753_PCM, voice);
  785. return 0;
  786. }
  787. /*
  788. * Set PCM DAI bit size and sample rate.
  789. */
  790. static int wm8753_pcm_hw_params(struct snd_pcm_substream *substream,
  791. struct snd_pcm_hw_params *params,
  792. struct snd_soc_dai *dai)
  793. {
  794. struct snd_soc_component *component = dai->component;
  795. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  796. u16 voice = snd_soc_component_read(component, WM8753_PCM) & 0x01f3;
  797. u16 srate = snd_soc_component_read(component, WM8753_SRATE1) & 0x017f;
  798. /* bit size */
  799. switch (params_width(params)) {
  800. case 16:
  801. break;
  802. case 20:
  803. voice |= 0x0004;
  804. break;
  805. case 24:
  806. voice |= 0x0008;
  807. break;
  808. case 32:
  809. voice |= 0x000c;
  810. break;
  811. }
  812. /* sample rate */
  813. if (params_rate(params) * 384 == wm8753->pcmclk)
  814. srate |= 0x80;
  815. snd_soc_component_write(component, WM8753_SRATE1, srate);
  816. snd_soc_component_write(component, WM8753_PCM, voice);
  817. return 0;
  818. }
  819. /*
  820. * Set's PCM dai fmt and BCLK.
  821. */
  822. static int wm8753_pcm_set_dai_fmt(struct snd_soc_component *component,
  823. unsigned int fmt)
  824. {
  825. u16 voice, ioctl;
  826. voice = snd_soc_component_read(component, WM8753_PCM) & 0x011f;
  827. ioctl = snd_soc_component_read(component, WM8753_IOCTL) & 0x015d;
  828. /* set master/slave audio interface */
  829. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  830. case SND_SOC_DAIFMT_CBS_CFS:
  831. break;
  832. case SND_SOC_DAIFMT_CBM_CFM:
  833. ioctl |= 0x2;
  834. fallthrough;
  835. case SND_SOC_DAIFMT_CBM_CFS:
  836. voice |= 0x0040;
  837. break;
  838. default:
  839. return -EINVAL;
  840. }
  841. /* clock inversion */
  842. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  843. case SND_SOC_DAIFMT_DSP_A:
  844. case SND_SOC_DAIFMT_DSP_B:
  845. /* frame inversion not valid for DSP modes */
  846. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  847. case SND_SOC_DAIFMT_NB_NF:
  848. break;
  849. case SND_SOC_DAIFMT_IB_NF:
  850. voice |= 0x0080;
  851. break;
  852. default:
  853. return -EINVAL;
  854. }
  855. break;
  856. case SND_SOC_DAIFMT_I2S:
  857. case SND_SOC_DAIFMT_RIGHT_J:
  858. case SND_SOC_DAIFMT_LEFT_J:
  859. voice &= ~0x0010;
  860. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  861. case SND_SOC_DAIFMT_NB_NF:
  862. break;
  863. case SND_SOC_DAIFMT_IB_IF:
  864. voice |= 0x0090;
  865. break;
  866. case SND_SOC_DAIFMT_IB_NF:
  867. voice |= 0x0080;
  868. break;
  869. case SND_SOC_DAIFMT_NB_IF:
  870. voice |= 0x0010;
  871. break;
  872. default:
  873. return -EINVAL;
  874. }
  875. break;
  876. default:
  877. return -EINVAL;
  878. }
  879. snd_soc_component_write(component, WM8753_PCM, voice);
  880. snd_soc_component_write(component, WM8753_IOCTL, ioctl);
  881. return 0;
  882. }
  883. static int wm8753_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  884. int div_id, int div)
  885. {
  886. struct snd_soc_component *component = codec_dai->component;
  887. u16 reg;
  888. switch (div_id) {
  889. case WM8753_PCMDIV:
  890. reg = snd_soc_component_read(component, WM8753_CLOCK) & 0x003f;
  891. snd_soc_component_write(component, WM8753_CLOCK, reg | div);
  892. break;
  893. case WM8753_BCLKDIV:
  894. reg = snd_soc_component_read(component, WM8753_SRATE2) & 0x01c7;
  895. snd_soc_component_write(component, WM8753_SRATE2, reg | div);
  896. break;
  897. case WM8753_VXCLKDIV:
  898. reg = snd_soc_component_read(component, WM8753_SRATE2) & 0x003f;
  899. snd_soc_component_write(component, WM8753_SRATE2, reg | div);
  900. break;
  901. default:
  902. return -EINVAL;
  903. }
  904. return 0;
  905. }
  906. /*
  907. * Set's HiFi DAC format.
  908. */
  909. static int wm8753_hdac_set_dai_fmt(struct snd_soc_component *component,
  910. unsigned int fmt)
  911. {
  912. u16 hifi = snd_soc_component_read(component, WM8753_HIFI) & 0x01e0;
  913. /* interface format */
  914. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  915. case SND_SOC_DAIFMT_I2S:
  916. hifi |= 0x0002;
  917. break;
  918. case SND_SOC_DAIFMT_RIGHT_J:
  919. break;
  920. case SND_SOC_DAIFMT_LEFT_J:
  921. hifi |= 0x0001;
  922. break;
  923. case SND_SOC_DAIFMT_DSP_A:
  924. hifi |= 0x0003;
  925. break;
  926. case SND_SOC_DAIFMT_DSP_B:
  927. hifi |= 0x0013;
  928. break;
  929. default:
  930. return -EINVAL;
  931. }
  932. snd_soc_component_write(component, WM8753_HIFI, hifi);
  933. return 0;
  934. }
  935. /*
  936. * Set's I2S DAI format.
  937. */
  938. static int wm8753_i2s_set_dai_fmt(struct snd_soc_component *component,
  939. unsigned int fmt)
  940. {
  941. u16 ioctl, hifi;
  942. hifi = snd_soc_component_read(component, WM8753_HIFI) & 0x013f;
  943. ioctl = snd_soc_component_read(component, WM8753_IOCTL) & 0x00ae;
  944. /* set master/slave audio interface */
  945. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  946. case SND_SOC_DAIFMT_CBS_CFS:
  947. break;
  948. case SND_SOC_DAIFMT_CBM_CFM:
  949. ioctl |= 0x1;
  950. fallthrough;
  951. case SND_SOC_DAIFMT_CBM_CFS:
  952. hifi |= 0x0040;
  953. break;
  954. default:
  955. return -EINVAL;
  956. }
  957. /* clock inversion */
  958. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  959. case SND_SOC_DAIFMT_DSP_A:
  960. case SND_SOC_DAIFMT_DSP_B:
  961. /* frame inversion not valid for DSP modes */
  962. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  963. case SND_SOC_DAIFMT_NB_NF:
  964. break;
  965. case SND_SOC_DAIFMT_IB_NF:
  966. hifi |= 0x0080;
  967. break;
  968. default:
  969. return -EINVAL;
  970. }
  971. break;
  972. case SND_SOC_DAIFMT_I2S:
  973. case SND_SOC_DAIFMT_RIGHT_J:
  974. case SND_SOC_DAIFMT_LEFT_J:
  975. hifi &= ~0x0010;
  976. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  977. case SND_SOC_DAIFMT_NB_NF:
  978. break;
  979. case SND_SOC_DAIFMT_IB_IF:
  980. hifi |= 0x0090;
  981. break;
  982. case SND_SOC_DAIFMT_IB_NF:
  983. hifi |= 0x0080;
  984. break;
  985. case SND_SOC_DAIFMT_NB_IF:
  986. hifi |= 0x0010;
  987. break;
  988. default:
  989. return -EINVAL;
  990. }
  991. break;
  992. default:
  993. return -EINVAL;
  994. }
  995. snd_soc_component_write(component, WM8753_HIFI, hifi);
  996. snd_soc_component_write(component, WM8753_IOCTL, ioctl);
  997. return 0;
  998. }
  999. /*
  1000. * Set PCM DAI bit size and sample rate.
  1001. */
  1002. static int wm8753_i2s_hw_params(struct snd_pcm_substream *substream,
  1003. struct snd_pcm_hw_params *params,
  1004. struct snd_soc_dai *dai)
  1005. {
  1006. struct snd_soc_component *component = dai->component;
  1007. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  1008. u16 srate = snd_soc_component_read(component, WM8753_SRATE1) & 0x01c0;
  1009. u16 hifi = snd_soc_component_read(component, WM8753_HIFI) & 0x01f3;
  1010. int coeff;
  1011. /* is digital filter coefficient valid ? */
  1012. coeff = get_coeff(wm8753->sysclk, params_rate(params));
  1013. if (coeff < 0) {
  1014. printk(KERN_ERR "wm8753 invalid MCLK or rate\n");
  1015. return coeff;
  1016. }
  1017. snd_soc_component_write(component, WM8753_SRATE1, srate | (coeff_div[coeff].sr << 1) |
  1018. coeff_div[coeff].usb);
  1019. /* bit size */
  1020. switch (params_width(params)) {
  1021. case 16:
  1022. break;
  1023. case 20:
  1024. hifi |= 0x0004;
  1025. break;
  1026. case 24:
  1027. hifi |= 0x0008;
  1028. break;
  1029. case 32:
  1030. hifi |= 0x000c;
  1031. break;
  1032. }
  1033. snd_soc_component_write(component, WM8753_HIFI, hifi);
  1034. return 0;
  1035. }
  1036. static int wm8753_mode1v_set_dai_fmt(struct snd_soc_component *component,
  1037. unsigned int fmt)
  1038. {
  1039. u16 clock;
  1040. /* set clk source as pcmclk */
  1041. clock = snd_soc_component_read(component, WM8753_CLOCK) & 0xfffb;
  1042. snd_soc_component_write(component, WM8753_CLOCK, clock);
  1043. return wm8753_vdac_adc_set_dai_fmt(component, fmt);
  1044. }
  1045. static int wm8753_mode1h_set_dai_fmt(struct snd_soc_component *component,
  1046. unsigned int fmt)
  1047. {
  1048. return wm8753_hdac_set_dai_fmt(component, fmt);
  1049. }
  1050. static int wm8753_mode2_set_dai_fmt(struct snd_soc_component *component,
  1051. unsigned int fmt)
  1052. {
  1053. u16 clock;
  1054. /* set clk source as pcmclk */
  1055. clock = snd_soc_component_read(component, WM8753_CLOCK) & 0xfffb;
  1056. snd_soc_component_write(component, WM8753_CLOCK, clock);
  1057. return wm8753_vdac_adc_set_dai_fmt(component, fmt);
  1058. }
  1059. static int wm8753_mode3_4_set_dai_fmt(struct snd_soc_component *component,
  1060. unsigned int fmt)
  1061. {
  1062. u16 clock;
  1063. /* set clk source as mclk */
  1064. clock = snd_soc_component_read(component, WM8753_CLOCK) & 0xfffb;
  1065. snd_soc_component_write(component, WM8753_CLOCK, clock | 0x4);
  1066. if (wm8753_hdac_set_dai_fmt(component, fmt) < 0)
  1067. return -EINVAL;
  1068. return wm8753_vdac_adc_set_dai_fmt(component, fmt);
  1069. }
  1070. static int wm8753_hifi_write_dai_fmt(struct snd_soc_component *component,
  1071. unsigned int fmt)
  1072. {
  1073. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  1074. int ret = 0;
  1075. switch (wm8753->dai_func) {
  1076. case 0:
  1077. ret = wm8753_mode1h_set_dai_fmt(component, fmt);
  1078. break;
  1079. case 1:
  1080. ret = wm8753_mode2_set_dai_fmt(component, fmt);
  1081. break;
  1082. case 2:
  1083. case 3:
  1084. ret = wm8753_mode3_4_set_dai_fmt(component, fmt);
  1085. break;
  1086. default:
  1087. break;
  1088. }
  1089. if (ret)
  1090. return ret;
  1091. return wm8753_i2s_set_dai_fmt(component, fmt);
  1092. }
  1093. static int wm8753_hifi_set_dai_fmt(struct snd_soc_dai *codec_dai,
  1094. unsigned int fmt)
  1095. {
  1096. struct snd_soc_component *component = codec_dai->component;
  1097. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  1098. wm8753->hifi_fmt = fmt;
  1099. return wm8753_hifi_write_dai_fmt(component, fmt);
  1100. };
  1101. static int wm8753_voice_write_dai_fmt(struct snd_soc_component *component,
  1102. unsigned int fmt)
  1103. {
  1104. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  1105. int ret = 0;
  1106. if (wm8753->dai_func != 0)
  1107. return 0;
  1108. ret = wm8753_mode1v_set_dai_fmt(component, fmt);
  1109. if (ret)
  1110. return ret;
  1111. ret = wm8753_pcm_set_dai_fmt(component, fmt);
  1112. if (ret)
  1113. return ret;
  1114. return 0;
  1115. };
  1116. static int wm8753_voice_set_dai_fmt(struct snd_soc_dai *codec_dai,
  1117. unsigned int fmt)
  1118. {
  1119. struct snd_soc_component *component = codec_dai->component;
  1120. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  1121. wm8753->voice_fmt = fmt;
  1122. return wm8753_voice_write_dai_fmt(component, fmt);
  1123. };
  1124. static int wm8753_mute(struct snd_soc_dai *dai, int mute, int direction)
  1125. {
  1126. struct snd_soc_component *component = dai->component;
  1127. u16 mute_reg = snd_soc_component_read(component, WM8753_DAC) & 0xfff7;
  1128. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  1129. /* the digital mute covers the HiFi and Voice DAC's on the WM8753.
  1130. * make sure we check if they are not both active when we mute */
  1131. if (mute && wm8753->dai_func == 1) {
  1132. if (!snd_soc_component_active(component))
  1133. snd_soc_component_write(component, WM8753_DAC, mute_reg | 0x8);
  1134. } else {
  1135. if (mute)
  1136. snd_soc_component_write(component, WM8753_DAC, mute_reg | 0x8);
  1137. else
  1138. snd_soc_component_write(component, WM8753_DAC, mute_reg);
  1139. }
  1140. return 0;
  1141. }
  1142. static void wm8753_charge_work(struct work_struct *work)
  1143. {
  1144. struct wm8753_priv *wm8753 =
  1145. container_of(work, struct wm8753_priv, charge_work.work);
  1146. /* Set to 500k */
  1147. regmap_update_bits(wm8753->regmap, WM8753_PWR1, 0x0180, 0x0100);
  1148. }
  1149. static int wm8753_set_bias_level(struct snd_soc_component *component,
  1150. enum snd_soc_bias_level level)
  1151. {
  1152. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  1153. u16 pwr_reg = snd_soc_component_read(component, WM8753_PWR1) & 0xfe3e;
  1154. switch (level) {
  1155. case SND_SOC_BIAS_ON:
  1156. /* set vmid to 50k and unmute dac */
  1157. snd_soc_component_write(component, WM8753_PWR1, pwr_reg | 0x00c0);
  1158. break;
  1159. case SND_SOC_BIAS_PREPARE:
  1160. /* Wait until fully charged */
  1161. flush_delayed_work(&wm8753->charge_work);
  1162. break;
  1163. case SND_SOC_BIAS_STANDBY:
  1164. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
  1165. /* set vmid to 5k for quick power up */
  1166. snd_soc_component_write(component, WM8753_PWR1, pwr_reg | 0x01c1);
  1167. schedule_delayed_work(&wm8753->charge_work,
  1168. msecs_to_jiffies(caps_charge));
  1169. } else {
  1170. /* mute dac and set vmid to 500k, enable VREF */
  1171. snd_soc_component_write(component, WM8753_PWR1, pwr_reg | 0x0141);
  1172. }
  1173. break;
  1174. case SND_SOC_BIAS_OFF:
  1175. cancel_delayed_work_sync(&wm8753->charge_work);
  1176. snd_soc_component_write(component, WM8753_PWR1, 0x0001);
  1177. break;
  1178. }
  1179. return 0;
  1180. }
  1181. #define WM8753_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  1182. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  1183. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  1184. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  1185. #define WM8753_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1186. SNDRV_PCM_FMTBIT_S24_LE)
  1187. /*
  1188. * The WM8753 supports up to 4 different and mutually exclusive DAI
  1189. * configurations. This gives 2 PCM's available for use, hifi and voice.
  1190. * NOTE: The Voice PCM cannot play or capture audio to the CPU as it's DAI
  1191. * is connected between the wm8753 and a BT codec or GSM modem.
  1192. *
  1193. * 1. Voice over PCM DAI - HIFI DAC over HIFI DAI
  1194. * 2. Voice over HIFI DAI - HIFI disabled
  1195. * 3. Voice disabled - HIFI over HIFI
  1196. * 4. Voice disabled - HIFI over HIFI, uses voice DAI LRC for capture
  1197. */
  1198. static const struct snd_soc_dai_ops wm8753_dai_ops_hifi_mode = {
  1199. .hw_params = wm8753_i2s_hw_params,
  1200. .mute_stream = wm8753_mute,
  1201. .set_fmt = wm8753_hifi_set_dai_fmt,
  1202. .set_clkdiv = wm8753_set_dai_clkdiv,
  1203. .set_pll = wm8753_set_dai_pll,
  1204. .set_sysclk = wm8753_set_dai_sysclk,
  1205. .no_capture_mute = 1,
  1206. };
  1207. static const struct snd_soc_dai_ops wm8753_dai_ops_voice_mode = {
  1208. .hw_params = wm8753_pcm_hw_params,
  1209. .mute_stream = wm8753_mute,
  1210. .set_fmt = wm8753_voice_set_dai_fmt,
  1211. .set_clkdiv = wm8753_set_dai_clkdiv,
  1212. .set_pll = wm8753_set_dai_pll,
  1213. .set_sysclk = wm8753_set_dai_sysclk,
  1214. .no_capture_mute = 1,
  1215. };
  1216. static struct snd_soc_dai_driver wm8753_dai[] = {
  1217. /* DAI HiFi mode 1 */
  1218. { .name = "wm8753-hifi",
  1219. .playback = {
  1220. .stream_name = "HiFi Playback",
  1221. .channels_min = 1,
  1222. .channels_max = 2,
  1223. .rates = WM8753_RATES,
  1224. .formats = WM8753_FORMATS
  1225. },
  1226. .capture = { /* dummy for fast DAI switching */
  1227. .stream_name = "Capture",
  1228. .channels_min = 1,
  1229. .channels_max = 2,
  1230. .rates = WM8753_RATES,
  1231. .formats = WM8753_FORMATS
  1232. },
  1233. .ops = &wm8753_dai_ops_hifi_mode,
  1234. },
  1235. /* DAI Voice mode 1 */
  1236. { .name = "wm8753-voice",
  1237. .playback = {
  1238. .stream_name = "Voice Playback",
  1239. .channels_min = 1,
  1240. .channels_max = 1,
  1241. .rates = WM8753_RATES,
  1242. .formats = WM8753_FORMATS,
  1243. },
  1244. .capture = {
  1245. .stream_name = "Capture",
  1246. .channels_min = 1,
  1247. .channels_max = 2,
  1248. .rates = WM8753_RATES,
  1249. .formats = WM8753_FORMATS,
  1250. },
  1251. .ops = &wm8753_dai_ops_voice_mode,
  1252. },
  1253. };
  1254. static int wm8753_resume(struct snd_soc_component *component)
  1255. {
  1256. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  1257. regcache_sync(wm8753->regmap);
  1258. return 0;
  1259. }
  1260. static int wm8753_probe(struct snd_soc_component *component)
  1261. {
  1262. struct wm8753_priv *wm8753 = snd_soc_component_get_drvdata(component);
  1263. int ret;
  1264. INIT_DELAYED_WORK(&wm8753->charge_work, wm8753_charge_work);
  1265. ret = wm8753_reset(component);
  1266. if (ret < 0) {
  1267. dev_err(component->dev, "Failed to issue reset: %d\n", ret);
  1268. return ret;
  1269. }
  1270. wm8753->dai_func = 0;
  1271. /* set the update bits */
  1272. snd_soc_component_update_bits(component, WM8753_LDAC, 0x0100, 0x0100);
  1273. snd_soc_component_update_bits(component, WM8753_RDAC, 0x0100, 0x0100);
  1274. snd_soc_component_update_bits(component, WM8753_LADC, 0x0100, 0x0100);
  1275. snd_soc_component_update_bits(component, WM8753_RADC, 0x0100, 0x0100);
  1276. snd_soc_component_update_bits(component, WM8753_LOUT1V, 0x0100, 0x0100);
  1277. snd_soc_component_update_bits(component, WM8753_ROUT1V, 0x0100, 0x0100);
  1278. snd_soc_component_update_bits(component, WM8753_LOUT2V, 0x0100, 0x0100);
  1279. snd_soc_component_update_bits(component, WM8753_ROUT2V, 0x0100, 0x0100);
  1280. snd_soc_component_update_bits(component, WM8753_LINVOL, 0x0100, 0x0100);
  1281. snd_soc_component_update_bits(component, WM8753_RINVOL, 0x0100, 0x0100);
  1282. return 0;
  1283. }
  1284. static const struct snd_soc_component_driver soc_component_dev_wm8753 = {
  1285. .probe = wm8753_probe,
  1286. .resume = wm8753_resume,
  1287. .set_bias_level = wm8753_set_bias_level,
  1288. .controls = wm8753_snd_controls,
  1289. .num_controls = ARRAY_SIZE(wm8753_snd_controls),
  1290. .dapm_widgets = wm8753_dapm_widgets,
  1291. .num_dapm_widgets = ARRAY_SIZE(wm8753_dapm_widgets),
  1292. .dapm_routes = wm8753_dapm_routes,
  1293. .num_dapm_routes = ARRAY_SIZE(wm8753_dapm_routes),
  1294. .suspend_bias_off = 1,
  1295. .idle_bias_on = 1,
  1296. .use_pmdown_time = 1,
  1297. .endianness = 1,
  1298. .non_legacy_dai_naming = 1,
  1299. };
  1300. static const struct of_device_id wm8753_of_match[] = {
  1301. { .compatible = "wlf,wm8753", },
  1302. { }
  1303. };
  1304. MODULE_DEVICE_TABLE(of, wm8753_of_match);
  1305. static const struct regmap_config wm8753_regmap = {
  1306. .reg_bits = 7,
  1307. .val_bits = 9,
  1308. .max_register = WM8753_ADCTL2,
  1309. .volatile_reg = wm8753_volatile,
  1310. .cache_type = REGCACHE_RBTREE,
  1311. .reg_defaults = wm8753_reg_defaults,
  1312. .num_reg_defaults = ARRAY_SIZE(wm8753_reg_defaults),
  1313. };
  1314. #if defined(CONFIG_SPI_MASTER)
  1315. static int wm8753_spi_probe(struct spi_device *spi)
  1316. {
  1317. struct wm8753_priv *wm8753;
  1318. int ret;
  1319. wm8753 = devm_kzalloc(&spi->dev, sizeof(struct wm8753_priv),
  1320. GFP_KERNEL);
  1321. if (wm8753 == NULL)
  1322. return -ENOMEM;
  1323. spi_set_drvdata(spi, wm8753);
  1324. wm8753->regmap = devm_regmap_init_spi(spi, &wm8753_regmap);
  1325. if (IS_ERR(wm8753->regmap)) {
  1326. ret = PTR_ERR(wm8753->regmap);
  1327. dev_err(&spi->dev, "Failed to allocate register map: %d\n",
  1328. ret);
  1329. return ret;
  1330. }
  1331. ret = devm_snd_soc_register_component(&spi->dev, &soc_component_dev_wm8753,
  1332. wm8753_dai, ARRAY_SIZE(wm8753_dai));
  1333. if (ret != 0)
  1334. dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret);
  1335. return ret;
  1336. }
  1337. static struct spi_driver wm8753_spi_driver = {
  1338. .driver = {
  1339. .name = "wm8753",
  1340. .of_match_table = wm8753_of_match,
  1341. },
  1342. .probe = wm8753_spi_probe,
  1343. };
  1344. #endif /* CONFIG_SPI_MASTER */
  1345. #if IS_ENABLED(CONFIG_I2C)
  1346. static int wm8753_i2c_probe(struct i2c_client *i2c,
  1347. const struct i2c_device_id *id)
  1348. {
  1349. struct wm8753_priv *wm8753;
  1350. int ret;
  1351. wm8753 = devm_kzalloc(&i2c->dev, sizeof(struct wm8753_priv),
  1352. GFP_KERNEL);
  1353. if (wm8753 == NULL)
  1354. return -ENOMEM;
  1355. i2c_set_clientdata(i2c, wm8753);
  1356. wm8753->regmap = devm_regmap_init_i2c(i2c, &wm8753_regmap);
  1357. if (IS_ERR(wm8753->regmap)) {
  1358. ret = PTR_ERR(wm8753->regmap);
  1359. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  1360. ret);
  1361. return ret;
  1362. }
  1363. ret = devm_snd_soc_register_component(&i2c->dev, &soc_component_dev_wm8753,
  1364. wm8753_dai, ARRAY_SIZE(wm8753_dai));
  1365. if (ret != 0)
  1366. dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
  1367. return ret;
  1368. }
  1369. static const struct i2c_device_id wm8753_i2c_id[] = {
  1370. { "wm8753", 0 },
  1371. { }
  1372. };
  1373. MODULE_DEVICE_TABLE(i2c, wm8753_i2c_id);
  1374. static struct i2c_driver wm8753_i2c_driver = {
  1375. .driver = {
  1376. .name = "wm8753",
  1377. .of_match_table = wm8753_of_match,
  1378. },
  1379. .probe = wm8753_i2c_probe,
  1380. .id_table = wm8753_i2c_id,
  1381. };
  1382. #endif
  1383. static int __init wm8753_modinit(void)
  1384. {
  1385. int ret = 0;
  1386. #if IS_ENABLED(CONFIG_I2C)
  1387. ret = i2c_add_driver(&wm8753_i2c_driver);
  1388. if (ret != 0) {
  1389. printk(KERN_ERR "Failed to register wm8753 I2C driver: %d\n",
  1390. ret);
  1391. }
  1392. #endif
  1393. #if defined(CONFIG_SPI_MASTER)
  1394. ret = spi_register_driver(&wm8753_spi_driver);
  1395. if (ret != 0) {
  1396. printk(KERN_ERR "Failed to register wm8753 SPI driver: %d\n",
  1397. ret);
  1398. }
  1399. #endif
  1400. return ret;
  1401. }
  1402. module_init(wm8753_modinit);
  1403. static void __exit wm8753_exit(void)
  1404. {
  1405. #if IS_ENABLED(CONFIG_I2C)
  1406. i2c_del_driver(&wm8753_i2c_driver);
  1407. #endif
  1408. #if defined(CONFIG_SPI_MASTER)
  1409. spi_unregister_driver(&wm8753_spi_driver);
  1410. #endif
  1411. }
  1412. module_exit(wm8753_exit);
  1413. MODULE_DESCRIPTION("ASoC WM8753 driver");
  1414. MODULE_AUTHOR("Liam Girdwood");
  1415. MODULE_LICENSE("GPL");