wm8753.c 48 KB

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