wm8753.c 46 KB

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