wm8995.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. /*
  2. * wm8995.c -- WM8995 ALSA SoC Audio driver
  3. *
  4. * Copyright 2010 Wolfson Microelectronics plc
  5. *
  6. * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
  7. *
  8. * Based on wm8994.c and wm_hubs.c by Mark Brown
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/slab.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/soc-dapm.h>
  28. #include <sound/initval.h>
  29. #include <sound/tlv.h>
  30. #include "wm8995.h"
  31. #define WM8995_NUM_SUPPLIES 8
  32. static const char *wm8995_supply_names[WM8995_NUM_SUPPLIES] = {
  33. "DCVDD",
  34. "DBVDD1",
  35. "DBVDD2",
  36. "DBVDD3",
  37. "AVDD1",
  38. "AVDD2",
  39. "CPVDD",
  40. "MICVDD"
  41. };
  42. static const u16 wm8995_reg_defs[WM8995_MAX_REGISTER + 1] = {
  43. [0] = 0x8995, [5] = 0x0100, [16] = 0x000b, [17] = 0x000b,
  44. [24] = 0x02c0, [25] = 0x02c0, [26] = 0x02c0, [27] = 0x02c0,
  45. [28] = 0x000f, [32] = 0x0005, [33] = 0x0005, [40] = 0x0003,
  46. [41] = 0x0013, [48] = 0x0004, [56] = 0x09f8, [64] = 0x1f25,
  47. [69] = 0x0004, [82] = 0xaaaa, [84] = 0x2a2a, [146] = 0x0060,
  48. [256] = 0x0002, [257] = 0x8004, [520] = 0x0010, [528] = 0x0083,
  49. [529] = 0x0083, [548] = 0x0c80, [580] = 0x0c80, [768] = 0x4050,
  50. [769] = 0x4000, [771] = 0x0040, [772] = 0x0040, [773] = 0x0040,
  51. [774] = 0x0004, [775] = 0x0100, [784] = 0x4050, [785] = 0x4000,
  52. [787] = 0x0040, [788] = 0x0040, [789] = 0x0040, [1024] = 0x00c0,
  53. [1025] = 0x00c0, [1026] = 0x00c0, [1027] = 0x00c0, [1028] = 0x00c0,
  54. [1029] = 0x00c0, [1030] = 0x00c0, [1031] = 0x00c0, [1056] = 0x0200,
  55. [1057] = 0x0010, [1058] = 0x0200, [1059] = 0x0010, [1088] = 0x0098,
  56. [1089] = 0x0845, [1104] = 0x0098, [1105] = 0x0845, [1152] = 0x6318,
  57. [1153] = 0x6300, [1154] = 0x0fca, [1155] = 0x0400, [1156] = 0x00d8,
  58. [1157] = 0x1eb5, [1158] = 0xf145, [1159] = 0x0b75, [1160] = 0x01c5,
  59. [1161] = 0x1c58, [1162] = 0xf373, [1163] = 0x0a54, [1164] = 0x0558,
  60. [1165] = 0x168e, [1166] = 0xf829, [1167] = 0x07ad, [1168] = 0x1103,
  61. [1169] = 0x0564, [1170] = 0x0559, [1171] = 0x4000, [1184] = 0x6318,
  62. [1185] = 0x6300, [1186] = 0x0fca, [1187] = 0x0400, [1188] = 0x00d8,
  63. [1189] = 0x1eb5, [1190] = 0xf145, [1191] = 0x0b75, [1192] = 0x01c5,
  64. [1193] = 0x1c58, [1194] = 0xf373, [1195] = 0x0a54, [1196] = 0x0558,
  65. [1197] = 0x168e, [1198] = 0xf829, [1199] = 0x07ad, [1200] = 0x1103,
  66. [1201] = 0x0564, [1202] = 0x0559, [1203] = 0x4000, [1280] = 0x00c0,
  67. [1281] = 0x00c0, [1282] = 0x00c0, [1283] = 0x00c0, [1312] = 0x0200,
  68. [1313] = 0x0010, [1344] = 0x0098, [1345] = 0x0845, [1408] = 0x6318,
  69. [1409] = 0x6300, [1410] = 0x0fca, [1411] = 0x0400, [1412] = 0x00d8,
  70. [1413] = 0x1eb5, [1414] = 0xf145, [1415] = 0x0b75, [1416] = 0x01c5,
  71. [1417] = 0x1c58, [1418] = 0xf373, [1419] = 0x0a54, [1420] = 0x0558,
  72. [1421] = 0x168e, [1422] = 0xf829, [1423] = 0x07ad, [1424] = 0x1103,
  73. [1425] = 0x0564, [1426] = 0x0559, [1427] = 0x4000, [1568] = 0x0002,
  74. [1792] = 0xa100, [1793] = 0xa101, [1794] = 0xa101, [1795] = 0xa101,
  75. [1796] = 0xa101, [1797] = 0xa101, [1798] = 0xa101, [1799] = 0xa101,
  76. [1800] = 0xa101, [1801] = 0xa101, [1802] = 0xa101, [1803] = 0xa101,
  77. [1804] = 0xa101, [1805] = 0xa101, [1825] = 0x0055, [1848] = 0x3fff,
  78. [1849] = 0x1fff, [2049] = 0x0001, [2050] = 0x0069, [2056] = 0x0002,
  79. [2057] = 0x0003, [2058] = 0x0069, [12288] = 0x0001, [12289] = 0x0001,
  80. [12291] = 0x0006, [12292] = 0x0040, [12293] = 0x0001, [12294] = 0x000f,
  81. [12295] = 0x0006, [12296] = 0x0001, [12297] = 0x0003, [12298] = 0x0104,
  82. [12300] = 0x0060, [12301] = 0x0011, [12302] = 0x0401, [12304] = 0x0050,
  83. [12305] = 0x0003, [12306] = 0x0100, [12308] = 0x0051, [12309] = 0x0003,
  84. [12310] = 0x0104, [12311] = 0x000a, [12312] = 0x0060, [12313] = 0x003b,
  85. [12314] = 0x0502, [12315] = 0x0100, [12316] = 0x2fff, [12320] = 0x2fff,
  86. [12324] = 0x2fff, [12328] = 0x2fff, [12332] = 0x2fff, [12336] = 0x2fff,
  87. [12340] = 0x2fff, [12344] = 0x2fff, [12348] = 0x2fff, [12352] = 0x0001,
  88. [12353] = 0x0001, [12355] = 0x0006, [12356] = 0x0040, [12357] = 0x0001,
  89. [12358] = 0x000f, [12359] = 0x0006, [12360] = 0x0001, [12361] = 0x0003,
  90. [12362] = 0x0104, [12364] = 0x0060, [12365] = 0x0011, [12366] = 0x0401,
  91. [12368] = 0x0050, [12369] = 0x0003, [12370] = 0x0100, [12372] = 0x0060,
  92. [12373] = 0x003b, [12374] = 0x0502, [12375] = 0x0100, [12376] = 0x2fff,
  93. [12380] = 0x2fff, [12384] = 0x2fff, [12388] = 0x2fff, [12392] = 0x2fff,
  94. [12396] = 0x2fff, [12400] = 0x2fff, [12404] = 0x2fff, [12408] = 0x2fff,
  95. [12412] = 0x2fff, [12416] = 0x0001, [12417] = 0x0001, [12419] = 0x0006,
  96. [12420] = 0x0040, [12421] = 0x0001, [12422] = 0x000f, [12423] = 0x0006,
  97. [12424] = 0x0001, [12425] = 0x0003, [12426] = 0x0106, [12428] = 0x0061,
  98. [12429] = 0x0011, [12430] = 0x0401, [12432] = 0x0050, [12433] = 0x0003,
  99. [12434] = 0x0102, [12436] = 0x0051, [12437] = 0x0003, [12438] = 0x0106,
  100. [12439] = 0x000a, [12440] = 0x0061, [12441] = 0x003b, [12442] = 0x0502,
  101. [12443] = 0x0100, [12444] = 0x2fff, [12448] = 0x2fff, [12452] = 0x2fff,
  102. [12456] = 0x2fff, [12460] = 0x2fff, [12464] = 0x2fff, [12468] = 0x2fff,
  103. [12472] = 0x2fff, [12476] = 0x2fff, [12480] = 0x0001, [12481] = 0x0001,
  104. [12483] = 0x0006, [12484] = 0x0040, [12485] = 0x0001, [12486] = 0x000f,
  105. [12487] = 0x0006, [12488] = 0x0001, [12489] = 0x0003, [12490] = 0x0106,
  106. [12492] = 0x0061, [12493] = 0x0011, [12494] = 0x0401, [12496] = 0x0050,
  107. [12497] = 0x0003, [12498] = 0x0102, [12500] = 0x0061, [12501] = 0x003b,
  108. [12502] = 0x0502, [12503] = 0x0100, [12504] = 0x2fff, [12508] = 0x2fff,
  109. [12512] = 0x2fff, [12516] = 0x2fff, [12520] = 0x2fff, [12524] = 0x2fff,
  110. [12528] = 0x2fff, [12532] = 0x2fff, [12536] = 0x2fff, [12540] = 0x2fff,
  111. [12544] = 0x0060, [12546] = 0x0601, [12548] = 0x0050, [12550] = 0x0100,
  112. [12552] = 0x0001, [12554] = 0x0104, [12555] = 0x0100, [12556] = 0x2fff,
  113. [12560] = 0x2fff, [12564] = 0x2fff, [12568] = 0x2fff, [12572] = 0x2fff,
  114. [12576] = 0x2fff, [12580] = 0x2fff, [12584] = 0x2fff, [12588] = 0x2fff,
  115. [12592] = 0x2fff, [12596] = 0x2fff, [12600] = 0x2fff, [12604] = 0x2fff,
  116. [12608] = 0x0061, [12610] = 0x0601, [12612] = 0x0050, [12614] = 0x0102,
  117. [12616] = 0x0001, [12618] = 0x0106, [12619] = 0x0100, [12620] = 0x2fff,
  118. [12624] = 0x2fff, [12628] = 0x2fff, [12632] = 0x2fff, [12636] = 0x2fff,
  119. [12640] = 0x2fff, [12644] = 0x2fff, [12648] = 0x2fff, [12652] = 0x2fff,
  120. [12656] = 0x2fff, [12660] = 0x2fff, [12664] = 0x2fff, [12668] = 0x2fff,
  121. [12672] = 0x0060, [12674] = 0x0601, [12676] = 0x0061, [12678] = 0x0601,
  122. [12680] = 0x0050, [12682] = 0x0300, [12684] = 0x0001, [12686] = 0x0304,
  123. [12688] = 0x0040, [12690] = 0x000f, [12692] = 0x0001, [12695] = 0x0100
  124. };
  125. struct fll_config {
  126. int src;
  127. int in;
  128. int out;
  129. };
  130. struct wm8995_priv {
  131. enum snd_soc_control_type control_type;
  132. int sysclk[2];
  133. int mclk[2];
  134. int aifclk[2];
  135. struct fll_config fll[2], fll_suspend[2];
  136. struct regulator_bulk_data supplies[WM8995_NUM_SUPPLIES];
  137. struct notifier_block disable_nb[WM8995_NUM_SUPPLIES];
  138. struct snd_soc_codec *codec;
  139. };
  140. /*
  141. * We can't use the same notifier block for more than one supply and
  142. * there's no way I can see to get from a callback to the caller
  143. * except container_of().
  144. */
  145. #define WM8995_REGULATOR_EVENT(n) \
  146. static int wm8995_regulator_event_##n(struct notifier_block *nb, \
  147. unsigned long event, void *data) \
  148. { \
  149. struct wm8995_priv *wm8995 = container_of(nb, struct wm8995_priv, \
  150. disable_nb[n]); \
  151. if (event & REGULATOR_EVENT_DISABLE) { \
  152. wm8995->codec->cache_sync = 1; \
  153. } \
  154. return 0; \
  155. }
  156. WM8995_REGULATOR_EVENT(0)
  157. WM8995_REGULATOR_EVENT(1)
  158. WM8995_REGULATOR_EVENT(2)
  159. WM8995_REGULATOR_EVENT(3)
  160. WM8995_REGULATOR_EVENT(4)
  161. WM8995_REGULATOR_EVENT(5)
  162. WM8995_REGULATOR_EVENT(6)
  163. WM8995_REGULATOR_EVENT(7)
  164. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  165. static const DECLARE_TLV_DB_SCALE(in1lr_pga_tlv, -1650, 150, 0);
  166. static const DECLARE_TLV_DB_SCALE(in1l_boost_tlv, 0, 600, 0);
  167. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 150, 0);
  168. static const char *in1l_text[] = {
  169. "Differential", "Single-ended IN1LN", "Single-ended IN1LP"
  170. };
  171. static const SOC_ENUM_SINGLE_DECL(in1l_enum, WM8995_LEFT_LINE_INPUT_CONTROL,
  172. 2, in1l_text);
  173. static const char *in1r_text[] = {
  174. "Differential", "Single-ended IN1RN", "Single-ended IN1RP"
  175. };
  176. static const SOC_ENUM_SINGLE_DECL(in1r_enum, WM8995_LEFT_LINE_INPUT_CONTROL,
  177. 0, in1r_text);
  178. static const char *dmic_src_text[] = {
  179. "DMICDAT1", "DMICDAT2", "DMICDAT3"
  180. };
  181. static const SOC_ENUM_SINGLE_DECL(dmic_src1_enum, WM8995_POWER_MANAGEMENT_5,
  182. 8, dmic_src_text);
  183. static const SOC_ENUM_SINGLE_DECL(dmic_src2_enum, WM8995_POWER_MANAGEMENT_5,
  184. 6, dmic_src_text);
  185. static const struct snd_kcontrol_new wm8995_snd_controls[] = {
  186. SOC_DOUBLE_R_TLV("DAC1 Volume", WM8995_DAC1_LEFT_VOLUME,
  187. WM8995_DAC1_RIGHT_VOLUME, 0, 96, 0, digital_tlv),
  188. SOC_DOUBLE_R("DAC1 Switch", WM8995_DAC1_LEFT_VOLUME,
  189. WM8995_DAC1_RIGHT_VOLUME, 9, 1, 1),
  190. SOC_DOUBLE_R_TLV("DAC2 Volume", WM8995_DAC2_LEFT_VOLUME,
  191. WM8995_DAC2_RIGHT_VOLUME, 0, 96, 0, digital_tlv),
  192. SOC_DOUBLE_R("DAC2 Switch", WM8995_DAC2_LEFT_VOLUME,
  193. WM8995_DAC2_RIGHT_VOLUME, 9, 1, 1),
  194. SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8995_AIF1_DAC1_LEFT_VOLUME,
  195. WM8995_AIF1_DAC1_RIGHT_VOLUME, 0, 96, 0, digital_tlv),
  196. SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8995_AIF1_DAC2_LEFT_VOLUME,
  197. WM8995_AIF1_DAC2_RIGHT_VOLUME, 0, 96, 0, digital_tlv),
  198. SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8995_AIF2_DAC_LEFT_VOLUME,
  199. WM8995_AIF2_DAC_RIGHT_VOLUME, 0, 96, 0, digital_tlv),
  200. SOC_DOUBLE_R_TLV("IN1LR Volume", WM8995_LEFT_LINE_INPUT_1_VOLUME,
  201. WM8995_RIGHT_LINE_INPUT_1_VOLUME, 0, 31, 0, in1lr_pga_tlv),
  202. SOC_SINGLE_TLV("IN1L Boost", WM8995_LEFT_LINE_INPUT_CONTROL,
  203. 4, 3, 0, in1l_boost_tlv),
  204. SOC_ENUM("IN1L Mode", in1l_enum),
  205. SOC_ENUM("IN1R Mode", in1r_enum),
  206. SOC_ENUM("DMIC1 SRC", dmic_src1_enum),
  207. SOC_ENUM("DMIC2 SRC", dmic_src2_enum),
  208. SOC_DOUBLE_TLV("DAC1 Sidetone Volume", WM8995_DAC1_MIXER_VOLUMES, 0, 5,
  209. 24, 0, sidetone_tlv),
  210. SOC_DOUBLE_TLV("DAC2 Sidetone Volume", WM8995_DAC2_MIXER_VOLUMES, 0, 5,
  211. 24, 0, sidetone_tlv),
  212. SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8995_AIF1_ADC1_LEFT_VOLUME,
  213. WM8995_AIF1_ADC1_RIGHT_VOLUME, 0, 96, 0, digital_tlv),
  214. SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8995_AIF1_ADC2_LEFT_VOLUME,
  215. WM8995_AIF1_ADC2_RIGHT_VOLUME, 0, 96, 0, digital_tlv),
  216. SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8995_AIF2_ADC_LEFT_VOLUME,
  217. WM8995_AIF2_ADC_RIGHT_VOLUME, 0, 96, 0, digital_tlv)
  218. };
  219. static void wm8995_update_class_w(struct snd_soc_codec *codec)
  220. {
  221. int enable = 1;
  222. int source = 0; /* GCC flow analysis can't track enable */
  223. int reg, reg_r;
  224. /* We also need the same setting for L/R and only one path */
  225. reg = snd_soc_read(codec, WM8995_DAC1_LEFT_MIXER_ROUTING);
  226. switch (reg) {
  227. case WM8995_AIF2DACL_TO_DAC1L:
  228. dev_dbg(codec->dev, "Class W source AIF2DAC\n");
  229. source = 2 << WM8995_CP_DYN_SRC_SEL_SHIFT;
  230. break;
  231. case WM8995_AIF1DAC2L_TO_DAC1L:
  232. dev_dbg(codec->dev, "Class W source AIF1DAC2\n");
  233. source = 1 << WM8995_CP_DYN_SRC_SEL_SHIFT;
  234. break;
  235. case WM8995_AIF1DAC1L_TO_DAC1L:
  236. dev_dbg(codec->dev, "Class W source AIF1DAC1\n");
  237. source = 0 << WM8995_CP_DYN_SRC_SEL_SHIFT;
  238. break;
  239. default:
  240. dev_dbg(codec->dev, "DAC mixer setting: %x\n", reg);
  241. enable = 0;
  242. break;
  243. }
  244. reg_r = snd_soc_read(codec, WM8995_DAC1_RIGHT_MIXER_ROUTING);
  245. if (reg_r != reg) {
  246. dev_dbg(codec->dev, "Left and right DAC mixers different\n");
  247. enable = 0;
  248. }
  249. if (enable) {
  250. dev_dbg(codec->dev, "Class W enabled\n");
  251. snd_soc_update_bits(codec, WM8995_CLASS_W_1,
  252. WM8995_CP_DYN_PWR_MASK |
  253. WM8995_CP_DYN_SRC_SEL_MASK,
  254. source | WM8995_CP_DYN_PWR);
  255. } else {
  256. dev_dbg(codec->dev, "Class W disabled\n");
  257. snd_soc_update_bits(codec, WM8995_CLASS_W_1,
  258. WM8995_CP_DYN_PWR_MASK, 0);
  259. }
  260. }
  261. static int check_clk_sys(struct snd_soc_dapm_widget *source,
  262. struct snd_soc_dapm_widget *sink)
  263. {
  264. unsigned int reg;
  265. const char *clk;
  266. reg = snd_soc_read(source->codec, WM8995_CLOCKING_1);
  267. /* Check what we're currently using for CLK_SYS */
  268. if (reg & WM8995_SYSCLK_SRC)
  269. clk = "AIF2CLK";
  270. else
  271. clk = "AIF1CLK";
  272. return !strcmp(source->name, clk);
  273. }
  274. static int wm8995_put_class_w(struct snd_kcontrol *kcontrol,
  275. struct snd_ctl_elem_value *ucontrol)
  276. {
  277. struct snd_soc_dapm_widget *w;
  278. struct snd_soc_codec *codec;
  279. int ret;
  280. w = snd_kcontrol_chip(kcontrol);
  281. codec = w->codec;
  282. ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
  283. wm8995_update_class_w(codec);
  284. return ret;
  285. }
  286. static int hp_supply_event(struct snd_soc_dapm_widget *w,
  287. struct snd_kcontrol *kcontrol, int event)
  288. {
  289. struct snd_soc_codec *codec;
  290. struct wm8995_priv *wm8995;
  291. codec = w->codec;
  292. wm8995 = snd_soc_codec_get_drvdata(codec);
  293. switch (event) {
  294. case SND_SOC_DAPM_PRE_PMU:
  295. /* Enable the headphone amp */
  296. snd_soc_update_bits(codec, WM8995_POWER_MANAGEMENT_1,
  297. WM8995_HPOUT1L_ENA_MASK |
  298. WM8995_HPOUT1R_ENA_MASK,
  299. WM8995_HPOUT1L_ENA |
  300. WM8995_HPOUT1R_ENA);
  301. /* Enable the second stage */
  302. snd_soc_update_bits(codec, WM8995_ANALOGUE_HP_1,
  303. WM8995_HPOUT1L_DLY_MASK |
  304. WM8995_HPOUT1R_DLY_MASK,
  305. WM8995_HPOUT1L_DLY |
  306. WM8995_HPOUT1R_DLY);
  307. break;
  308. case SND_SOC_DAPM_PRE_PMD:
  309. snd_soc_update_bits(codec, WM8995_CHARGE_PUMP_1,
  310. WM8995_CP_ENA_MASK, 0);
  311. break;
  312. }
  313. return 0;
  314. }
  315. static void dc_servo_cmd(struct snd_soc_codec *codec,
  316. unsigned int reg, unsigned int val, unsigned int mask)
  317. {
  318. int timeout = 10;
  319. dev_dbg(codec->dev, "%s: reg = %#x, val = %#x, mask = %#x\n",
  320. __func__, reg, val, mask);
  321. snd_soc_write(codec, reg, val);
  322. while (timeout--) {
  323. msleep(10);
  324. val = snd_soc_read(codec, WM8995_DC_SERVO_READBACK_0);
  325. if ((val & mask) == mask)
  326. return;
  327. }
  328. dev_err(codec->dev, "Timed out waiting for DC Servo\n");
  329. }
  330. static int hp_event(struct snd_soc_dapm_widget *w,
  331. struct snd_kcontrol *kcontrol, int event)
  332. {
  333. struct snd_soc_codec *codec;
  334. unsigned int reg;
  335. codec = w->codec;
  336. reg = snd_soc_read(codec, WM8995_ANALOGUE_HP_1);
  337. switch (event) {
  338. case SND_SOC_DAPM_POST_PMU:
  339. snd_soc_update_bits(codec, WM8995_CHARGE_PUMP_1,
  340. WM8995_CP_ENA_MASK, WM8995_CP_ENA);
  341. msleep(5);
  342. snd_soc_update_bits(codec, WM8995_POWER_MANAGEMENT_1,
  343. WM8995_HPOUT1L_ENA_MASK |
  344. WM8995_HPOUT1R_ENA_MASK,
  345. WM8995_HPOUT1L_ENA | WM8995_HPOUT1R_ENA);
  346. udelay(20);
  347. reg |= WM8995_HPOUT1L_DLY | WM8995_HPOUT1R_DLY;
  348. snd_soc_write(codec, WM8995_ANALOGUE_HP_1, reg);
  349. snd_soc_write(codec, WM8995_DC_SERVO_1, WM8995_DCS_ENA_CHAN_0 |
  350. WM8995_DCS_ENA_CHAN_1);
  351. dc_servo_cmd(codec, WM8995_DC_SERVO_2,
  352. WM8995_DCS_TRIG_STARTUP_0 |
  353. WM8995_DCS_TRIG_STARTUP_1,
  354. WM8995_DCS_TRIG_DAC_WR_0 |
  355. WM8995_DCS_TRIG_DAC_WR_1);
  356. reg |= WM8995_HPOUT1R_OUTP | WM8995_HPOUT1R_RMV_SHORT |
  357. WM8995_HPOUT1L_OUTP | WM8995_HPOUT1L_RMV_SHORT;
  358. snd_soc_write(codec, WM8995_ANALOGUE_HP_1, reg);
  359. break;
  360. case SND_SOC_DAPM_PRE_PMD:
  361. snd_soc_update_bits(codec, WM8995_ANALOGUE_HP_1,
  362. WM8995_HPOUT1L_OUTP_MASK |
  363. WM8995_HPOUT1R_OUTP_MASK |
  364. WM8995_HPOUT1L_RMV_SHORT_MASK |
  365. WM8995_HPOUT1R_RMV_SHORT_MASK, 0);
  366. snd_soc_update_bits(codec, WM8995_ANALOGUE_HP_1,
  367. WM8995_HPOUT1L_DLY_MASK |
  368. WM8995_HPOUT1R_DLY_MASK, 0);
  369. snd_soc_write(codec, WM8995_DC_SERVO_1, 0);
  370. snd_soc_update_bits(codec, WM8995_POWER_MANAGEMENT_1,
  371. WM8995_HPOUT1L_ENA_MASK |
  372. WM8995_HPOUT1R_ENA_MASK,
  373. 0);
  374. break;
  375. }
  376. return 0;
  377. }
  378. static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
  379. {
  380. struct wm8995_priv *wm8995;
  381. int rate;
  382. int reg1 = 0;
  383. int offset;
  384. wm8995 = snd_soc_codec_get_drvdata(codec);
  385. if (aif)
  386. offset = 4;
  387. else
  388. offset = 0;
  389. switch (wm8995->sysclk[aif]) {
  390. case WM8995_SYSCLK_MCLK1:
  391. rate = wm8995->mclk[0];
  392. break;
  393. case WM8995_SYSCLK_MCLK2:
  394. reg1 |= 0x8;
  395. rate = wm8995->mclk[1];
  396. break;
  397. case WM8995_SYSCLK_FLL1:
  398. reg1 |= 0x10;
  399. rate = wm8995->fll[0].out;
  400. break;
  401. case WM8995_SYSCLK_FLL2:
  402. reg1 |= 0x18;
  403. rate = wm8995->fll[1].out;
  404. break;
  405. default:
  406. return -EINVAL;
  407. }
  408. if (rate >= 13500000) {
  409. rate /= 2;
  410. reg1 |= WM8995_AIF1CLK_DIV;
  411. dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
  412. aif + 1, rate);
  413. }
  414. wm8995->aifclk[aif] = rate;
  415. snd_soc_update_bits(codec, WM8995_AIF1_CLOCKING_1 + offset,
  416. WM8995_AIF1CLK_SRC_MASK | WM8995_AIF1CLK_DIV_MASK,
  417. reg1);
  418. return 0;
  419. }
  420. static int configure_clock(struct snd_soc_codec *codec)
  421. {
  422. struct wm8995_priv *wm8995;
  423. int old, new;
  424. wm8995 = snd_soc_codec_get_drvdata(codec);
  425. /* Bring up the AIF clocks first */
  426. configure_aif_clock(codec, 0);
  427. configure_aif_clock(codec, 1);
  428. /*
  429. * Then switch CLK_SYS over to the higher of them; a change
  430. * can only happen as a result of a clocking change which can
  431. * only be made outside of DAPM so we can safely redo the
  432. * clocking.
  433. */
  434. /* If they're equal it doesn't matter which is used */
  435. if (wm8995->aifclk[0] == wm8995->aifclk[1])
  436. return 0;
  437. if (wm8995->aifclk[0] < wm8995->aifclk[1])
  438. new = WM8995_SYSCLK_SRC;
  439. else
  440. new = 0;
  441. old = snd_soc_read(codec, WM8995_CLOCKING_1) & WM8995_SYSCLK_SRC;
  442. /* If there's no change then we're done. */
  443. if (old == new)
  444. return 0;
  445. snd_soc_update_bits(codec, WM8995_CLOCKING_1,
  446. WM8995_SYSCLK_SRC_MASK, new);
  447. snd_soc_dapm_sync(&codec->dapm);
  448. return 0;
  449. }
  450. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  451. struct snd_kcontrol *kcontrol, int event)
  452. {
  453. struct snd_soc_codec *codec;
  454. codec = w->codec;
  455. switch (event) {
  456. case SND_SOC_DAPM_PRE_PMU:
  457. return configure_clock(codec);
  458. case SND_SOC_DAPM_POST_PMD:
  459. configure_clock(codec);
  460. break;
  461. }
  462. return 0;
  463. }
  464. static const char *sidetone_text[] = {
  465. "ADC/DMIC1", "DMIC2",
  466. };
  467. static const struct soc_enum sidetone1_enum =
  468. SOC_ENUM_SINGLE(WM8995_SIDETONE, 0, 2, sidetone_text);
  469. static const struct snd_kcontrol_new sidetone1_mux =
  470. SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
  471. static const struct soc_enum sidetone2_enum =
  472. SOC_ENUM_SINGLE(WM8995_SIDETONE, 1, 2, sidetone_text);
  473. static const struct snd_kcontrol_new sidetone2_mux =
  474. SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
  475. static const struct snd_kcontrol_new aif1adc1l_mix[] = {
  476. SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8995_AIF1_ADC1_LEFT_MIXER_ROUTING,
  477. 1, 1, 0),
  478. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_AIF1_ADC1_LEFT_MIXER_ROUTING,
  479. 0, 1, 0),
  480. };
  481. static const struct snd_kcontrol_new aif1adc1r_mix[] = {
  482. SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8995_AIF1_ADC1_RIGHT_MIXER_ROUTING,
  483. 1, 1, 0),
  484. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_AIF1_ADC1_RIGHT_MIXER_ROUTING,
  485. 0, 1, 0),
  486. };
  487. static const struct snd_kcontrol_new aif1adc2l_mix[] = {
  488. SOC_DAPM_SINGLE("DMIC Switch", WM8995_AIF1_ADC2_LEFT_MIXER_ROUTING,
  489. 1, 1, 0),
  490. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_AIF1_ADC2_LEFT_MIXER_ROUTING,
  491. 0, 1, 0),
  492. };
  493. static const struct snd_kcontrol_new aif1adc2r_mix[] = {
  494. SOC_DAPM_SINGLE("DMIC Switch", WM8995_AIF1_ADC2_RIGHT_MIXER_ROUTING,
  495. 1, 1, 0),
  496. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_AIF1_ADC2_RIGHT_MIXER_ROUTING,
  497. 0, 1, 0),
  498. };
  499. static const struct snd_kcontrol_new dac1l_mix[] = {
  500. WM8995_CLASS_W_SWITCH("Right Sidetone Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  501. 5, 1, 0),
  502. WM8995_CLASS_W_SWITCH("Left Sidetone Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  503. 4, 1, 0),
  504. WM8995_CLASS_W_SWITCH("AIF2 Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  505. 2, 1, 0),
  506. WM8995_CLASS_W_SWITCH("AIF1.2 Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  507. 1, 1, 0),
  508. WM8995_CLASS_W_SWITCH("AIF1.1 Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  509. 0, 1, 0),
  510. };
  511. static const struct snd_kcontrol_new dac1r_mix[] = {
  512. WM8995_CLASS_W_SWITCH("Right Sidetone Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  513. 5, 1, 0),
  514. WM8995_CLASS_W_SWITCH("Left Sidetone Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  515. 4, 1, 0),
  516. WM8995_CLASS_W_SWITCH("AIF2 Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  517. 2, 1, 0),
  518. WM8995_CLASS_W_SWITCH("AIF1.2 Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  519. 1, 1, 0),
  520. WM8995_CLASS_W_SWITCH("AIF1.1 Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  521. 0, 1, 0),
  522. };
  523. static const struct snd_kcontrol_new aif2dac2l_mix[] = {
  524. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  525. 5, 1, 0),
  526. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  527. 4, 1, 0),
  528. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  529. 2, 1, 0),
  530. SOC_DAPM_SINGLE("AIF1.2 Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  531. 1, 1, 0),
  532. SOC_DAPM_SINGLE("AIF1.1 Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  533. 0, 1, 0),
  534. };
  535. static const struct snd_kcontrol_new aif2dac2r_mix[] = {
  536. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  537. 5, 1, 0),
  538. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  539. 4, 1, 0),
  540. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  541. 2, 1, 0),
  542. SOC_DAPM_SINGLE("AIF1.2 Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  543. 1, 1, 0),
  544. SOC_DAPM_SINGLE("AIF1.1 Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  545. 0, 1, 0),
  546. };
  547. static const struct snd_kcontrol_new in1l_pga =
  548. SOC_DAPM_SINGLE("IN1L Switch", WM8995_POWER_MANAGEMENT_2, 5, 1, 0);
  549. static const struct snd_kcontrol_new in1r_pga =
  550. SOC_DAPM_SINGLE("IN1R Switch", WM8995_POWER_MANAGEMENT_2, 4, 1, 0);
  551. static const char *adc_mux_text[] = {
  552. "ADC",
  553. "DMIC",
  554. };
  555. static const struct soc_enum adc_enum =
  556. SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
  557. static const struct snd_kcontrol_new adcl_mux =
  558. SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
  559. static const struct snd_kcontrol_new adcr_mux =
  560. SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
  561. static const char *spk_src_text[] = {
  562. "DAC1L", "DAC1R", "DAC2L", "DAC2R"
  563. };
  564. static const SOC_ENUM_SINGLE_DECL(spk1l_src_enum, WM8995_LEFT_PDM_SPEAKER_1,
  565. 0, spk_src_text);
  566. static const SOC_ENUM_SINGLE_DECL(spk1r_src_enum, WM8995_RIGHT_PDM_SPEAKER_1,
  567. 0, spk_src_text);
  568. static const SOC_ENUM_SINGLE_DECL(spk2l_src_enum, WM8995_LEFT_PDM_SPEAKER_2,
  569. 0, spk_src_text);
  570. static const SOC_ENUM_SINGLE_DECL(spk2r_src_enum, WM8995_RIGHT_PDM_SPEAKER_2,
  571. 0, spk_src_text);
  572. static const struct snd_kcontrol_new spk1l_mux =
  573. SOC_DAPM_ENUM("SPK1L SRC", spk1l_src_enum);
  574. static const struct snd_kcontrol_new spk1r_mux =
  575. SOC_DAPM_ENUM("SPK1R SRC", spk1r_src_enum);
  576. static const struct snd_kcontrol_new spk2l_mux =
  577. SOC_DAPM_ENUM("SPK2L SRC", spk2l_src_enum);
  578. static const struct snd_kcontrol_new spk2r_mux =
  579. SOC_DAPM_ENUM("SPK2R SRC", spk2r_src_enum);
  580. static const struct snd_soc_dapm_widget wm8995_dapm_widgets[] = {
  581. SND_SOC_DAPM_INPUT("DMIC1DAT"),
  582. SND_SOC_DAPM_INPUT("DMIC2DAT"),
  583. SND_SOC_DAPM_INPUT("IN1L"),
  584. SND_SOC_DAPM_INPUT("IN1R"),
  585. SND_SOC_DAPM_MIXER("IN1L PGA", SND_SOC_NOPM, 0, 0,
  586. &in1l_pga, 1),
  587. SND_SOC_DAPM_MIXER("IN1R PGA", SND_SOC_NOPM, 0, 0,
  588. &in1r_pga, 1),
  589. SND_SOC_DAPM_MICBIAS("MICBIAS1", WM8995_POWER_MANAGEMENT_1, 8, 0),
  590. SND_SOC_DAPM_MICBIAS("MICBIAS2", WM8995_POWER_MANAGEMENT_1, 9, 0),
  591. SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8995_AIF1_CLOCKING_1, 0, 0, NULL, 0),
  592. SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8995_AIF2_CLOCKING_1, 0, 0, NULL, 0),
  593. SND_SOC_DAPM_SUPPLY("DSP1CLK", WM8995_CLOCKING_1, 3, 0, NULL, 0),
  594. SND_SOC_DAPM_SUPPLY("DSP2CLK", WM8995_CLOCKING_1, 2, 0, NULL, 0),
  595. SND_SOC_DAPM_SUPPLY("SYSDSPCLK", WM8995_CLOCKING_1, 1, 0, NULL, 0),
  596. SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
  597. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  598. SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", "AIF1 Capture", 0,
  599. WM8995_POWER_MANAGEMENT_3, 9, 0),
  600. SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", "AIF1 Capture", 0,
  601. WM8995_POWER_MANAGEMENT_3, 8, 0),
  602. SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", "AIF1 Capture", 0,
  603. SND_SOC_NOPM, 0, 0),
  604. SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", "AIF1 Capture",
  605. 0, WM8995_POWER_MANAGEMENT_3, 11, 0),
  606. SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", "AIF1 Capture",
  607. 0, WM8995_POWER_MANAGEMENT_3, 10, 0),
  608. SND_SOC_DAPM_VIRT_MUX("ADCL Mux", SND_SOC_NOPM, 1, 0,
  609. &adcl_mux),
  610. SND_SOC_DAPM_VIRT_MUX("ADCR Mux", SND_SOC_NOPM, 0, 0,
  611. &adcr_mux),
  612. SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8995_POWER_MANAGEMENT_3, 5, 0),
  613. SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8995_POWER_MANAGEMENT_3, 4, 0),
  614. SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8995_POWER_MANAGEMENT_3, 3, 0),
  615. SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8995_POWER_MANAGEMENT_3, 2, 0),
  616. SND_SOC_DAPM_ADC("ADCL", NULL, WM8995_POWER_MANAGEMENT_3, 1, 0),
  617. SND_SOC_DAPM_ADC("ADCR", NULL, WM8995_POWER_MANAGEMENT_3, 0, 0),
  618. SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
  619. aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
  620. SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
  621. aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
  622. SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
  623. aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
  624. SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
  625. aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
  626. SND_SOC_DAPM_AIF_IN("AIF1DAC1L", NULL, 0, WM8995_POWER_MANAGEMENT_4,
  627. 9, 0),
  628. SND_SOC_DAPM_AIF_IN("AIF1DAC1R", NULL, 0, WM8995_POWER_MANAGEMENT_4,
  629. 8, 0),
  630. SND_SOC_DAPM_AIF_IN("AIF1DACDAT", "AIF1 Playback", 0, SND_SOC_NOPM,
  631. 0, 0),
  632. SND_SOC_DAPM_AIF_IN("AIF1DAC2L", NULL, 0, WM8995_POWER_MANAGEMENT_4,
  633. 11, 0),
  634. SND_SOC_DAPM_AIF_IN("AIF1DAC2R", NULL, 0, WM8995_POWER_MANAGEMENT_4,
  635. 10, 0),
  636. SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
  637. aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
  638. SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
  639. aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
  640. SND_SOC_DAPM_DAC("DAC2L", NULL, WM8995_POWER_MANAGEMENT_4, 3, 0),
  641. SND_SOC_DAPM_DAC("DAC2R", NULL, WM8995_POWER_MANAGEMENT_4, 2, 0),
  642. SND_SOC_DAPM_DAC("DAC1L", NULL, WM8995_POWER_MANAGEMENT_4, 1, 0),
  643. SND_SOC_DAPM_DAC("DAC1R", NULL, WM8995_POWER_MANAGEMENT_4, 0, 0),
  644. SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0, dac1l_mix,
  645. ARRAY_SIZE(dac1l_mix)),
  646. SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0, dac1r_mix,
  647. ARRAY_SIZE(dac1r_mix)),
  648. SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
  649. SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
  650. SND_SOC_DAPM_PGA_E("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
  651. hp_event, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  652. SND_SOC_DAPM_SUPPLY("Headphone Supply", SND_SOC_NOPM, 0, 0,
  653. hp_supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  654. SND_SOC_DAPM_MUX("SPK1L Driver", WM8995_LEFT_PDM_SPEAKER_1,
  655. 4, 0, &spk1l_mux),
  656. SND_SOC_DAPM_MUX("SPK1R Driver", WM8995_RIGHT_PDM_SPEAKER_1,
  657. 4, 0, &spk1r_mux),
  658. SND_SOC_DAPM_MUX("SPK2L Driver", WM8995_LEFT_PDM_SPEAKER_2,
  659. 4, 0, &spk2l_mux),
  660. SND_SOC_DAPM_MUX("SPK2R Driver", WM8995_RIGHT_PDM_SPEAKER_2,
  661. 4, 0, &spk2r_mux),
  662. SND_SOC_DAPM_SUPPLY("LDO2", WM8995_POWER_MANAGEMENT_2, 1, 0, NULL, 0),
  663. SND_SOC_DAPM_OUTPUT("HP1L"),
  664. SND_SOC_DAPM_OUTPUT("HP1R"),
  665. SND_SOC_DAPM_OUTPUT("SPK1L"),
  666. SND_SOC_DAPM_OUTPUT("SPK1R"),
  667. SND_SOC_DAPM_OUTPUT("SPK2L"),
  668. SND_SOC_DAPM_OUTPUT("SPK2R")
  669. };
  670. static const struct snd_soc_dapm_route wm8995_intercon[] = {
  671. { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
  672. { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
  673. { "DSP1CLK", NULL, "CLK_SYS" },
  674. { "DSP2CLK", NULL, "CLK_SYS" },
  675. { "SYSDSPCLK", NULL, "CLK_SYS" },
  676. { "AIF1ADC1L", NULL, "AIF1CLK" },
  677. { "AIF1ADC1L", NULL, "DSP1CLK" },
  678. { "AIF1ADC1R", NULL, "AIF1CLK" },
  679. { "AIF1ADC1R", NULL, "DSP1CLK" },
  680. { "AIF1ADC1R", NULL, "SYSDSPCLK" },
  681. { "AIF1ADC2L", NULL, "AIF1CLK" },
  682. { "AIF1ADC2L", NULL, "DSP1CLK" },
  683. { "AIF1ADC2R", NULL, "AIF1CLK" },
  684. { "AIF1ADC2R", NULL, "DSP1CLK" },
  685. { "AIF1ADC2R", NULL, "SYSDSPCLK" },
  686. { "DMIC1L", NULL, "DMIC1DAT" },
  687. { "DMIC1L", NULL, "CLK_SYS" },
  688. { "DMIC1R", NULL, "DMIC1DAT" },
  689. { "DMIC1R", NULL, "CLK_SYS" },
  690. { "DMIC2L", NULL, "DMIC2DAT" },
  691. { "DMIC2L", NULL, "CLK_SYS" },
  692. { "DMIC2R", NULL, "DMIC2DAT" },
  693. { "DMIC2R", NULL, "CLK_SYS" },
  694. { "ADCL", NULL, "AIF1CLK" },
  695. { "ADCL", NULL, "DSP1CLK" },
  696. { "ADCL", NULL, "SYSDSPCLK" },
  697. { "ADCR", NULL, "AIF1CLK" },
  698. { "ADCR", NULL, "DSP1CLK" },
  699. { "ADCR", NULL, "SYSDSPCLK" },
  700. { "IN1L PGA", "IN1L Switch", "IN1L" },
  701. { "IN1R PGA", "IN1R Switch", "IN1R" },
  702. { "IN1L PGA", NULL, "LDO2" },
  703. { "IN1R PGA", NULL, "LDO2" },
  704. { "ADCL", NULL, "IN1L PGA" },
  705. { "ADCR", NULL, "IN1R PGA" },
  706. { "ADCL Mux", "ADC", "ADCL" },
  707. { "ADCL Mux", "DMIC", "DMIC1L" },
  708. { "ADCR Mux", "ADC", "ADCR" },
  709. { "ADCR Mux", "DMIC", "DMIC1R" },
  710. /* AIF1 outputs */
  711. { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
  712. { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
  713. { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
  714. { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
  715. { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
  716. { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
  717. { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
  718. { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
  719. /* Sidetone */
  720. { "Left Sidetone", "ADC/DMIC1", "AIF1ADC1L" },
  721. { "Left Sidetone", "DMIC2", "AIF1ADC2L" },
  722. { "Right Sidetone", "ADC/DMIC1", "AIF1ADC1R" },
  723. { "Right Sidetone", "DMIC2", "AIF1ADC2R" },
  724. { "AIF1DAC1L", NULL, "AIF1CLK" },
  725. { "AIF1DAC1L", NULL, "DSP1CLK" },
  726. { "AIF1DAC1R", NULL, "AIF1CLK" },
  727. { "AIF1DAC1R", NULL, "DSP1CLK" },
  728. { "AIF1DAC1R", NULL, "SYSDSPCLK" },
  729. { "AIF1DAC2L", NULL, "AIF1CLK" },
  730. { "AIF1DAC2L", NULL, "DSP1CLK" },
  731. { "AIF1DAC2R", NULL, "AIF1CLK" },
  732. { "AIF1DAC2R", NULL, "DSP1CLK" },
  733. { "AIF1DAC2R", NULL, "SYSDSPCLK" },
  734. { "DAC1L", NULL, "AIF1CLK" },
  735. { "DAC1L", NULL, "DSP1CLK" },
  736. { "DAC1L", NULL, "SYSDSPCLK" },
  737. { "DAC1R", NULL, "AIF1CLK" },
  738. { "DAC1R", NULL, "DSP1CLK" },
  739. { "DAC1R", NULL, "SYSDSPCLK" },
  740. { "AIF1DAC1L", NULL, "AIF1DACDAT" },
  741. { "AIF1DAC1R", NULL, "AIF1DACDAT" },
  742. { "AIF1DAC2L", NULL, "AIF1DACDAT" },
  743. { "AIF1DAC2R", NULL, "AIF1DACDAT" },
  744. /* DAC1 inputs */
  745. { "DAC1L", NULL, "DAC1L Mixer" },
  746. { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
  747. { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
  748. { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
  749. { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
  750. { "DAC1R", NULL, "DAC1R Mixer" },
  751. { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
  752. { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
  753. { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
  754. { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
  755. /* DAC2/AIF2 outputs */
  756. { "DAC2L", NULL, "AIF2DAC2L Mixer" },
  757. { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
  758. { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
  759. { "DAC2R", NULL, "AIF2DAC2R Mixer" },
  760. { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
  761. { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
  762. /* Output stages */
  763. { "Headphone PGA", NULL, "DAC1L" },
  764. { "Headphone PGA", NULL, "DAC1R" },
  765. { "Headphone PGA", NULL, "DAC2L" },
  766. { "Headphone PGA", NULL, "DAC2R" },
  767. { "Headphone PGA", NULL, "Headphone Supply" },
  768. { "Headphone PGA", NULL, "CLK_SYS" },
  769. { "Headphone PGA", NULL, "LDO2" },
  770. { "HP1L", NULL, "Headphone PGA" },
  771. { "HP1R", NULL, "Headphone PGA" },
  772. { "SPK1L Driver", "DAC1L", "DAC1L" },
  773. { "SPK1L Driver", "DAC1R", "DAC1R" },
  774. { "SPK1L Driver", "DAC2L", "DAC2L" },
  775. { "SPK1L Driver", "DAC2R", "DAC2R" },
  776. { "SPK1L Driver", NULL, "CLK_SYS" },
  777. { "SPK1R Driver", "DAC1L", "DAC1L" },
  778. { "SPK1R Driver", "DAC1R", "DAC1R" },
  779. { "SPK1R Driver", "DAC2L", "DAC2L" },
  780. { "SPK1R Driver", "DAC2R", "DAC2R" },
  781. { "SPK1R Driver", NULL, "CLK_SYS" },
  782. { "SPK2L Driver", "DAC1L", "DAC1L" },
  783. { "SPK2L Driver", "DAC1R", "DAC1R" },
  784. { "SPK2L Driver", "DAC2L", "DAC2L" },
  785. { "SPK2L Driver", "DAC2R", "DAC2R" },
  786. { "SPK2L Driver", NULL, "CLK_SYS" },
  787. { "SPK2R Driver", "DAC1L", "DAC1L" },
  788. { "SPK2R Driver", "DAC1R", "DAC1R" },
  789. { "SPK2R Driver", "DAC2L", "DAC2L" },
  790. { "SPK2R Driver", "DAC2R", "DAC2R" },
  791. { "SPK2R Driver", NULL, "CLK_SYS" },
  792. { "SPK1L", NULL, "SPK1L Driver" },
  793. { "SPK1R", NULL, "SPK1R Driver" },
  794. { "SPK2L", NULL, "SPK2L Driver" },
  795. { "SPK2R", NULL, "SPK2R Driver" }
  796. };
  797. static int wm8995_volatile(struct snd_soc_codec *codec, unsigned int reg)
  798. {
  799. /* out of bounds registers are generally considered
  800. * volatile to support register banks that are partially
  801. * owned by something else for e.g. a DSP
  802. */
  803. if (reg > WM8995_MAX_CACHED_REGISTER)
  804. return 1;
  805. switch (reg) {
  806. case WM8995_SOFTWARE_RESET:
  807. case WM8995_DC_SERVO_READBACK_0:
  808. case WM8995_INTERRUPT_STATUS_1:
  809. case WM8995_INTERRUPT_STATUS_2:
  810. case WM8995_INTERRUPT_STATUS_1_MASK:
  811. case WM8995_INTERRUPT_STATUS_2_MASK:
  812. case WM8995_INTERRUPT_CONTROL:
  813. case WM8995_ACCESSORY_DETECT_MODE1:
  814. case WM8995_ACCESSORY_DETECT_MODE2:
  815. case WM8995_HEADPHONE_DETECT1:
  816. case WM8995_HEADPHONE_DETECT2:
  817. return 1;
  818. }
  819. return 0;
  820. }
  821. static int wm8995_aif_mute(struct snd_soc_dai *dai, int mute)
  822. {
  823. struct snd_soc_codec *codec = dai->codec;
  824. int mute_reg;
  825. switch (dai->id) {
  826. case 0:
  827. mute_reg = WM8995_AIF1_DAC1_FILTERS_1;
  828. break;
  829. case 1:
  830. mute_reg = WM8995_AIF2_DAC_FILTERS_1;
  831. break;
  832. default:
  833. return -EINVAL;
  834. }
  835. snd_soc_update_bits(codec, mute_reg, WM8995_AIF1DAC1_MUTE_MASK,
  836. !!mute << WM8995_AIF1DAC1_MUTE_SHIFT);
  837. return 0;
  838. }
  839. static int wm8995_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  840. {
  841. struct snd_soc_codec *codec;
  842. int master;
  843. int aif;
  844. codec = dai->codec;
  845. master = 0;
  846. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  847. case SND_SOC_DAIFMT_CBS_CFS:
  848. break;
  849. case SND_SOC_DAIFMT_CBM_CFM:
  850. master = WM8995_AIF1_MSTR;
  851. break;
  852. default:
  853. dev_err(dai->dev, "Unknown master/slave configuration\n");
  854. return -EINVAL;
  855. }
  856. aif = 0;
  857. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  858. case SND_SOC_DAIFMT_DSP_B:
  859. aif |= WM8995_AIF1_LRCLK_INV;
  860. case SND_SOC_DAIFMT_DSP_A:
  861. aif |= (0x3 << WM8995_AIF1_FMT_SHIFT);
  862. break;
  863. case SND_SOC_DAIFMT_I2S:
  864. aif |= (0x2 << WM8995_AIF1_FMT_SHIFT);
  865. break;
  866. case SND_SOC_DAIFMT_RIGHT_J:
  867. break;
  868. case SND_SOC_DAIFMT_LEFT_J:
  869. aif |= (0x1 << WM8995_AIF1_FMT_SHIFT);
  870. break;
  871. default:
  872. dev_err(dai->dev, "Unknown dai format\n");
  873. return -EINVAL;
  874. }
  875. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  876. case SND_SOC_DAIFMT_DSP_A:
  877. case SND_SOC_DAIFMT_DSP_B:
  878. /* frame inversion not valid for DSP modes */
  879. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  880. case SND_SOC_DAIFMT_NB_NF:
  881. break;
  882. case SND_SOC_DAIFMT_IB_NF:
  883. aif |= WM8995_AIF1_BCLK_INV;
  884. break;
  885. default:
  886. return -EINVAL;
  887. }
  888. break;
  889. case SND_SOC_DAIFMT_I2S:
  890. case SND_SOC_DAIFMT_RIGHT_J:
  891. case SND_SOC_DAIFMT_LEFT_J:
  892. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  893. case SND_SOC_DAIFMT_NB_NF:
  894. break;
  895. case SND_SOC_DAIFMT_IB_IF:
  896. aif |= WM8995_AIF1_BCLK_INV | WM8995_AIF1_LRCLK_INV;
  897. break;
  898. case SND_SOC_DAIFMT_IB_NF:
  899. aif |= WM8995_AIF1_BCLK_INV;
  900. break;
  901. case SND_SOC_DAIFMT_NB_IF:
  902. aif |= WM8995_AIF1_LRCLK_INV;
  903. break;
  904. default:
  905. return -EINVAL;
  906. }
  907. break;
  908. default:
  909. return -EINVAL;
  910. }
  911. snd_soc_update_bits(codec, WM8995_AIF1_CONTROL_1,
  912. WM8995_AIF1_BCLK_INV_MASK |
  913. WM8995_AIF1_LRCLK_INV_MASK |
  914. WM8995_AIF1_FMT_MASK, aif);
  915. snd_soc_update_bits(codec, WM8995_AIF1_MASTER_SLAVE,
  916. WM8995_AIF1_MSTR_MASK, master);
  917. return 0;
  918. }
  919. static const int srs[] = {
  920. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
  921. 48000, 88200, 96000
  922. };
  923. static const int fs_ratios[] = {
  924. -1 /* reserved */,
  925. 128, 192, 256, 384, 512, 768, 1024, 1408, 1536
  926. };
  927. static const int bclk_divs[] = {
  928. 10, 15, 20, 30, 40, 55, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480
  929. };
  930. static int wm8995_hw_params(struct snd_pcm_substream *substream,
  931. struct snd_pcm_hw_params *params,
  932. struct snd_soc_dai *dai)
  933. {
  934. struct snd_soc_codec *codec;
  935. struct wm8995_priv *wm8995;
  936. int aif1_reg;
  937. int bclk_reg;
  938. int lrclk_reg;
  939. int rate_reg;
  940. int bclk_rate;
  941. int aif1;
  942. int lrclk, bclk;
  943. int i, rate_val, best, best_val, cur_val;
  944. codec = dai->codec;
  945. wm8995 = snd_soc_codec_get_drvdata(codec);
  946. switch (dai->id) {
  947. case 0:
  948. aif1_reg = WM8995_AIF1_CONTROL_1;
  949. bclk_reg = WM8995_AIF1_BCLK;
  950. rate_reg = WM8995_AIF1_RATE;
  951. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK /* ||
  952. wm8995->lrclk_shared[0] */) {
  953. lrclk_reg = WM8995_AIF1DAC_LRCLK;
  954. } else {
  955. lrclk_reg = WM8995_AIF1ADC_LRCLK;
  956. dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
  957. }
  958. break;
  959. case 1:
  960. aif1_reg = WM8995_AIF2_CONTROL_1;
  961. bclk_reg = WM8995_AIF2_BCLK;
  962. rate_reg = WM8995_AIF2_RATE;
  963. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK /* ||
  964. wm8995->lrclk_shared[1] */) {
  965. lrclk_reg = WM8995_AIF2DAC_LRCLK;
  966. } else {
  967. lrclk_reg = WM8995_AIF2ADC_LRCLK;
  968. dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
  969. }
  970. break;
  971. default:
  972. return -EINVAL;
  973. }
  974. bclk_rate = snd_soc_params_to_bclk(params);
  975. if (bclk_rate < 0)
  976. return bclk_rate;
  977. aif1 = 0;
  978. switch (params_format(params)) {
  979. case SNDRV_PCM_FORMAT_S16_LE:
  980. break;
  981. case SNDRV_PCM_FORMAT_S20_3LE:
  982. aif1 |= (0x1 << WM8995_AIF1_WL_SHIFT);
  983. break;
  984. case SNDRV_PCM_FORMAT_S24_LE:
  985. aif1 |= (0x2 << WM8995_AIF1_WL_SHIFT);
  986. break;
  987. case SNDRV_PCM_FORMAT_S32_LE:
  988. aif1 |= (0x3 << WM8995_AIF1_WL_SHIFT);
  989. break;
  990. default:
  991. dev_err(dai->dev, "Unsupported word length %u\n",
  992. params_format(params));
  993. return -EINVAL;
  994. }
  995. /* try to find a suitable sample rate */
  996. for (i = 0; i < ARRAY_SIZE(srs); ++i)
  997. if (srs[i] == params_rate(params))
  998. break;
  999. if (i == ARRAY_SIZE(srs)) {
  1000. dev_err(dai->dev, "Sample rate %d is not supported\n",
  1001. params_rate(params));
  1002. return -EINVAL;
  1003. }
  1004. rate_val = i << WM8995_AIF1_SR_SHIFT;
  1005. dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i]);
  1006. dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
  1007. dai->id + 1, wm8995->aifclk[dai->id], bclk_rate);
  1008. /* AIFCLK/fs ratio; look for a close match in either direction */
  1009. best = 1;
  1010. best_val = abs((fs_ratios[1] * params_rate(params))
  1011. - wm8995->aifclk[dai->id]);
  1012. for (i = 2; i < ARRAY_SIZE(fs_ratios); i++) {
  1013. cur_val = abs((fs_ratios[i] * params_rate(params))
  1014. - wm8995->aifclk[dai->id]);
  1015. if (cur_val >= best_val)
  1016. continue;
  1017. best = i;
  1018. best_val = cur_val;
  1019. }
  1020. rate_val |= best;
  1021. dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
  1022. dai->id + 1, fs_ratios[best]);
  1023. /*
  1024. * We may not get quite the right frequency if using
  1025. * approximate clocks so look for the closest match that is
  1026. * higher than the target (we need to ensure that there enough
  1027. * BCLKs to clock out the samples).
  1028. */
  1029. best = 0;
  1030. bclk = 0;
  1031. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1032. cur_val = (wm8995->aifclk[dai->id] * 10 / bclk_divs[i]) - bclk_rate;
  1033. if (cur_val < 0) /* BCLK table is sorted */
  1034. break;
  1035. best = i;
  1036. }
  1037. bclk |= best << WM8995_AIF1_BCLK_DIV_SHIFT;
  1038. bclk_rate = wm8995->aifclk[dai->id] * 10 / bclk_divs[best];
  1039. dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
  1040. bclk_divs[best], bclk_rate);
  1041. lrclk = bclk_rate / params_rate(params);
  1042. dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
  1043. lrclk, bclk_rate / lrclk);
  1044. snd_soc_update_bits(codec, aif1_reg,
  1045. WM8995_AIF1_WL_MASK, aif1);
  1046. snd_soc_update_bits(codec, bclk_reg,
  1047. WM8995_AIF1_BCLK_DIV_MASK, bclk);
  1048. snd_soc_update_bits(codec, lrclk_reg,
  1049. WM8995_AIF1DAC_RATE_MASK, lrclk);
  1050. snd_soc_update_bits(codec, rate_reg,
  1051. WM8995_AIF1_SR_MASK |
  1052. WM8995_AIF1CLK_RATE_MASK, rate_val);
  1053. return 0;
  1054. }
  1055. static int wm8995_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
  1056. {
  1057. struct snd_soc_codec *codec = codec_dai->codec;
  1058. int reg, val, mask;
  1059. switch (codec_dai->id) {
  1060. case 0:
  1061. reg = WM8995_AIF1_MASTER_SLAVE;
  1062. mask = WM8995_AIF1_TRI;
  1063. break;
  1064. case 1:
  1065. reg = WM8995_AIF2_MASTER_SLAVE;
  1066. mask = WM8995_AIF2_TRI;
  1067. break;
  1068. case 2:
  1069. reg = WM8995_POWER_MANAGEMENT_5;
  1070. mask = WM8995_AIF3_TRI;
  1071. break;
  1072. default:
  1073. return -EINVAL;
  1074. }
  1075. if (tristate)
  1076. val = mask;
  1077. else
  1078. val = 0;
  1079. return snd_soc_update_bits(codec, reg, mask, val);
  1080. }
  1081. /* The size in bits of the FLL divide multiplied by 10
  1082. * to allow rounding later */
  1083. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  1084. struct fll_div {
  1085. u16 outdiv;
  1086. u16 n;
  1087. u16 k;
  1088. u16 clk_ref_div;
  1089. u16 fll_fratio;
  1090. };
  1091. static int wm8995_get_fll_config(struct fll_div *fll,
  1092. int freq_in, int freq_out)
  1093. {
  1094. u64 Kpart;
  1095. unsigned int K, Ndiv, Nmod;
  1096. pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
  1097. /* Scale the input frequency down to <= 13.5MHz */
  1098. fll->clk_ref_div = 0;
  1099. while (freq_in > 13500000) {
  1100. fll->clk_ref_div++;
  1101. freq_in /= 2;
  1102. if (fll->clk_ref_div > 3)
  1103. return -EINVAL;
  1104. }
  1105. pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
  1106. /* Scale the output to give 90MHz<=Fvco<=100MHz */
  1107. fll->outdiv = 3;
  1108. while (freq_out * (fll->outdiv + 1) < 90000000) {
  1109. fll->outdiv++;
  1110. if (fll->outdiv > 63)
  1111. return -EINVAL;
  1112. }
  1113. freq_out *= fll->outdiv + 1;
  1114. pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
  1115. if (freq_in > 1000000) {
  1116. fll->fll_fratio = 0;
  1117. } else if (freq_in > 256000) {
  1118. fll->fll_fratio = 1;
  1119. freq_in *= 2;
  1120. } else if (freq_in > 128000) {
  1121. fll->fll_fratio = 2;
  1122. freq_in *= 4;
  1123. } else if (freq_in > 64000) {
  1124. fll->fll_fratio = 3;
  1125. freq_in *= 8;
  1126. } else {
  1127. fll->fll_fratio = 4;
  1128. freq_in *= 16;
  1129. }
  1130. pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
  1131. /* Now, calculate N.K */
  1132. Ndiv = freq_out / freq_in;
  1133. fll->n = Ndiv;
  1134. Nmod = freq_out % freq_in;
  1135. pr_debug("Nmod=%d\n", Nmod);
  1136. /* Calculate fractional part - scale up so we can round. */
  1137. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  1138. do_div(Kpart, freq_in);
  1139. K = Kpart & 0xFFFFFFFF;
  1140. if ((K % 10) >= 5)
  1141. K += 5;
  1142. /* Move down to proper range now rounding is done */
  1143. fll->k = K / 10;
  1144. pr_debug("N=%x K=%x\n", fll->n, fll->k);
  1145. return 0;
  1146. }
  1147. static int wm8995_set_fll(struct snd_soc_dai *dai, int id,
  1148. int src, unsigned int freq_in,
  1149. unsigned int freq_out)
  1150. {
  1151. struct snd_soc_codec *codec;
  1152. struct wm8995_priv *wm8995;
  1153. int reg_offset, ret;
  1154. struct fll_div fll;
  1155. u16 reg, aif1, aif2;
  1156. codec = dai->codec;
  1157. wm8995 = snd_soc_codec_get_drvdata(codec);
  1158. aif1 = snd_soc_read(codec, WM8995_AIF1_CLOCKING_1)
  1159. & WM8995_AIF1CLK_ENA;
  1160. aif2 = snd_soc_read(codec, WM8995_AIF2_CLOCKING_1)
  1161. & WM8995_AIF2CLK_ENA;
  1162. switch (id) {
  1163. case WM8995_FLL1:
  1164. reg_offset = 0;
  1165. id = 0;
  1166. break;
  1167. case WM8995_FLL2:
  1168. reg_offset = 0x20;
  1169. id = 1;
  1170. break;
  1171. default:
  1172. return -EINVAL;
  1173. }
  1174. switch (src) {
  1175. case 0:
  1176. /* Allow no source specification when stopping */
  1177. if (freq_out)
  1178. return -EINVAL;
  1179. break;
  1180. case WM8995_FLL_SRC_MCLK1:
  1181. case WM8995_FLL_SRC_MCLK2:
  1182. case WM8995_FLL_SRC_LRCLK:
  1183. case WM8995_FLL_SRC_BCLK:
  1184. break;
  1185. default:
  1186. return -EINVAL;
  1187. }
  1188. /* Are we changing anything? */
  1189. if (wm8995->fll[id].src == src &&
  1190. wm8995->fll[id].in == freq_in && wm8995->fll[id].out == freq_out)
  1191. return 0;
  1192. /* If we're stopping the FLL redo the old config - no
  1193. * registers will actually be written but we avoid GCC flow
  1194. * analysis bugs spewing warnings.
  1195. */
  1196. if (freq_out)
  1197. ret = wm8995_get_fll_config(&fll, freq_in, freq_out);
  1198. else
  1199. ret = wm8995_get_fll_config(&fll, wm8995->fll[id].in,
  1200. wm8995->fll[id].out);
  1201. if (ret < 0)
  1202. return ret;
  1203. /* Gate the AIF clocks while we reclock */
  1204. snd_soc_update_bits(codec, WM8995_AIF1_CLOCKING_1,
  1205. WM8995_AIF1CLK_ENA_MASK, 0);
  1206. snd_soc_update_bits(codec, WM8995_AIF2_CLOCKING_1,
  1207. WM8995_AIF2CLK_ENA_MASK, 0);
  1208. /* We always need to disable the FLL while reconfiguring */
  1209. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_1 + reg_offset,
  1210. WM8995_FLL1_ENA_MASK, 0);
  1211. reg = (fll.outdiv << WM8995_FLL1_OUTDIV_SHIFT) |
  1212. (fll.fll_fratio << WM8995_FLL1_FRATIO_SHIFT);
  1213. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_2 + reg_offset,
  1214. WM8995_FLL1_OUTDIV_MASK |
  1215. WM8995_FLL1_FRATIO_MASK, reg);
  1216. snd_soc_write(codec, WM8995_FLL1_CONTROL_3 + reg_offset, fll.k);
  1217. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_4 + reg_offset,
  1218. WM8995_FLL1_N_MASK,
  1219. fll.n << WM8995_FLL1_N_SHIFT);
  1220. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_5 + reg_offset,
  1221. WM8995_FLL1_REFCLK_DIV_MASK |
  1222. WM8995_FLL1_REFCLK_SRC_MASK,
  1223. (fll.clk_ref_div << WM8995_FLL1_REFCLK_DIV_SHIFT) |
  1224. (src - 1));
  1225. if (freq_out)
  1226. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_1 + reg_offset,
  1227. WM8995_FLL1_ENA_MASK, WM8995_FLL1_ENA);
  1228. wm8995->fll[id].in = freq_in;
  1229. wm8995->fll[id].out = freq_out;
  1230. wm8995->fll[id].src = src;
  1231. /* Enable any gated AIF clocks */
  1232. snd_soc_update_bits(codec, WM8995_AIF1_CLOCKING_1,
  1233. WM8995_AIF1CLK_ENA_MASK, aif1);
  1234. snd_soc_update_bits(codec, WM8995_AIF2_CLOCKING_1,
  1235. WM8995_AIF2CLK_ENA_MASK, aif2);
  1236. configure_clock(codec);
  1237. return 0;
  1238. }
  1239. static int wm8995_set_dai_sysclk(struct snd_soc_dai *dai,
  1240. int clk_id, unsigned int freq, int dir)
  1241. {
  1242. struct snd_soc_codec *codec;
  1243. struct wm8995_priv *wm8995;
  1244. codec = dai->codec;
  1245. wm8995 = snd_soc_codec_get_drvdata(codec);
  1246. switch (dai->id) {
  1247. case 0:
  1248. case 1:
  1249. break;
  1250. default:
  1251. /* AIF3 shares clocking with AIF1/2 */
  1252. return -EINVAL;
  1253. }
  1254. switch (clk_id) {
  1255. case WM8995_SYSCLK_MCLK1:
  1256. wm8995->sysclk[dai->id] = WM8995_SYSCLK_MCLK1;
  1257. wm8995->mclk[0] = freq;
  1258. dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
  1259. dai->id + 1, freq);
  1260. break;
  1261. case WM8995_SYSCLK_MCLK2:
  1262. wm8995->sysclk[dai->id] = WM8995_SYSCLK_MCLK1;
  1263. wm8995->mclk[1] = freq;
  1264. dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
  1265. dai->id + 1, freq);
  1266. break;
  1267. case WM8995_SYSCLK_FLL1:
  1268. wm8995->sysclk[dai->id] = WM8995_SYSCLK_FLL1;
  1269. dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id + 1);
  1270. break;
  1271. case WM8995_SYSCLK_FLL2:
  1272. wm8995->sysclk[dai->id] = WM8995_SYSCLK_FLL2;
  1273. dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id + 1);
  1274. break;
  1275. case WM8995_SYSCLK_OPCLK:
  1276. default:
  1277. dev_err(dai->dev, "Unknown clock source %d\n", clk_id);
  1278. return -EINVAL;
  1279. }
  1280. configure_clock(codec);
  1281. return 0;
  1282. }
  1283. static int wm8995_set_bias_level(struct snd_soc_codec *codec,
  1284. enum snd_soc_bias_level level)
  1285. {
  1286. struct wm8995_priv *wm8995;
  1287. int ret;
  1288. wm8995 = snd_soc_codec_get_drvdata(codec);
  1289. switch (level) {
  1290. case SND_SOC_BIAS_ON:
  1291. case SND_SOC_BIAS_PREPARE:
  1292. break;
  1293. case SND_SOC_BIAS_STANDBY:
  1294. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  1295. ret = regulator_bulk_enable(ARRAY_SIZE(wm8995->supplies),
  1296. wm8995->supplies);
  1297. if (ret)
  1298. return ret;
  1299. ret = snd_soc_cache_sync(codec);
  1300. if (ret) {
  1301. dev_err(codec->dev,
  1302. "Failed to sync cache: %d\n", ret);
  1303. return ret;
  1304. }
  1305. snd_soc_update_bits(codec, WM8995_POWER_MANAGEMENT_1,
  1306. WM8995_BG_ENA_MASK, WM8995_BG_ENA);
  1307. }
  1308. break;
  1309. case SND_SOC_BIAS_OFF:
  1310. snd_soc_update_bits(codec, WM8995_POWER_MANAGEMENT_1,
  1311. WM8995_BG_ENA_MASK, 0);
  1312. regulator_bulk_disable(ARRAY_SIZE(wm8995->supplies),
  1313. wm8995->supplies);
  1314. break;
  1315. }
  1316. codec->dapm.bias_level = level;
  1317. return 0;
  1318. }
  1319. #ifdef CONFIG_PM
  1320. static int wm8995_suspend(struct snd_soc_codec *codec, pm_message_t state)
  1321. {
  1322. wm8995_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1323. return 0;
  1324. }
  1325. static int wm8995_resume(struct snd_soc_codec *codec)
  1326. {
  1327. wm8995_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1328. return 0;
  1329. }
  1330. #else
  1331. #define wm8995_suspend NULL
  1332. #define wm8995_resume NULL
  1333. #endif
  1334. static int wm8995_remove(struct snd_soc_codec *codec)
  1335. {
  1336. struct wm8995_priv *wm8995;
  1337. struct i2c_client *i2c;
  1338. i2c = container_of(codec->dev, struct i2c_client, dev);
  1339. wm8995 = snd_soc_codec_get_drvdata(codec);
  1340. wm8995_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1341. return 0;
  1342. }
  1343. static int wm8995_probe(struct snd_soc_codec *codec)
  1344. {
  1345. struct wm8995_priv *wm8995;
  1346. int i;
  1347. int ret;
  1348. codec->dapm.idle_bias_off = 1;
  1349. wm8995 = snd_soc_codec_get_drvdata(codec);
  1350. wm8995->codec = codec;
  1351. ret = snd_soc_codec_set_cache_io(codec, 16, 16, wm8995->control_type);
  1352. if (ret < 0) {
  1353. dev_err(codec->dev, "Failed to set cache i/o: %d\n", ret);
  1354. return ret;
  1355. }
  1356. for (i = 0; i < ARRAY_SIZE(wm8995->supplies); i++)
  1357. wm8995->supplies[i].supply = wm8995_supply_names[i];
  1358. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8995->supplies),
  1359. wm8995->supplies);
  1360. if (ret) {
  1361. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1362. return ret;
  1363. }
  1364. wm8995->disable_nb[0].notifier_call = wm8995_regulator_event_0;
  1365. wm8995->disable_nb[1].notifier_call = wm8995_regulator_event_1;
  1366. wm8995->disable_nb[2].notifier_call = wm8995_regulator_event_2;
  1367. wm8995->disable_nb[3].notifier_call = wm8995_regulator_event_3;
  1368. wm8995->disable_nb[4].notifier_call = wm8995_regulator_event_4;
  1369. wm8995->disable_nb[5].notifier_call = wm8995_regulator_event_5;
  1370. wm8995->disable_nb[6].notifier_call = wm8995_regulator_event_6;
  1371. wm8995->disable_nb[7].notifier_call = wm8995_regulator_event_7;
  1372. /* This should really be moved into the regulator core */
  1373. for (i = 0; i < ARRAY_SIZE(wm8995->supplies); i++) {
  1374. ret = regulator_register_notifier(wm8995->supplies[i].consumer,
  1375. &wm8995->disable_nb[i]);
  1376. if (ret) {
  1377. dev_err(codec->dev,
  1378. "Failed to register regulator notifier: %d\n",
  1379. ret);
  1380. }
  1381. }
  1382. ret = regulator_bulk_enable(ARRAY_SIZE(wm8995->supplies),
  1383. wm8995->supplies);
  1384. if (ret) {
  1385. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  1386. goto err_reg_get;
  1387. }
  1388. ret = snd_soc_read(codec, WM8995_SOFTWARE_RESET);
  1389. if (ret < 0) {
  1390. dev_err(codec->dev, "Failed to read device ID: %d\n", ret);
  1391. goto err_reg_enable;
  1392. }
  1393. if (ret != 0x8995) {
  1394. dev_err(codec->dev, "Invalid device ID: %#x\n", ret);
  1395. goto err_reg_enable;
  1396. }
  1397. ret = snd_soc_write(codec, WM8995_SOFTWARE_RESET, 0);
  1398. if (ret < 0) {
  1399. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  1400. goto err_reg_enable;
  1401. }
  1402. wm8995_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1403. /* Latch volume updates (right only; we always do left then right). */
  1404. snd_soc_update_bits(codec, WM8995_AIF1_DAC1_RIGHT_VOLUME,
  1405. WM8995_AIF1DAC1_VU_MASK, WM8995_AIF1DAC1_VU);
  1406. snd_soc_update_bits(codec, WM8995_AIF1_DAC2_RIGHT_VOLUME,
  1407. WM8995_AIF1DAC2_VU_MASK, WM8995_AIF1DAC2_VU);
  1408. snd_soc_update_bits(codec, WM8995_AIF2_DAC_RIGHT_VOLUME,
  1409. WM8995_AIF2DAC_VU_MASK, WM8995_AIF2DAC_VU);
  1410. snd_soc_update_bits(codec, WM8995_AIF1_ADC1_RIGHT_VOLUME,
  1411. WM8995_AIF1ADC1_VU_MASK, WM8995_AIF1ADC1_VU);
  1412. snd_soc_update_bits(codec, WM8995_AIF1_ADC2_RIGHT_VOLUME,
  1413. WM8995_AIF1ADC2_VU_MASK, WM8995_AIF1ADC2_VU);
  1414. snd_soc_update_bits(codec, WM8995_AIF2_ADC_RIGHT_VOLUME,
  1415. WM8995_AIF2ADC_VU_MASK, WM8995_AIF1ADC2_VU);
  1416. snd_soc_update_bits(codec, WM8995_DAC1_RIGHT_VOLUME,
  1417. WM8995_DAC1_VU_MASK, WM8995_DAC1_VU);
  1418. snd_soc_update_bits(codec, WM8995_DAC2_RIGHT_VOLUME,
  1419. WM8995_DAC2_VU_MASK, WM8995_DAC2_VU);
  1420. snd_soc_update_bits(codec, WM8995_RIGHT_LINE_INPUT_1_VOLUME,
  1421. WM8995_IN1_VU_MASK, WM8995_IN1_VU);
  1422. wm8995_update_class_w(codec);
  1423. snd_soc_add_controls(codec, wm8995_snd_controls,
  1424. ARRAY_SIZE(wm8995_snd_controls));
  1425. snd_soc_dapm_new_controls(&codec->dapm, wm8995_dapm_widgets,
  1426. ARRAY_SIZE(wm8995_dapm_widgets));
  1427. snd_soc_dapm_add_routes(&codec->dapm, wm8995_intercon,
  1428. ARRAY_SIZE(wm8995_intercon));
  1429. return 0;
  1430. err_reg_enable:
  1431. regulator_bulk_disable(ARRAY_SIZE(wm8995->supplies), wm8995->supplies);
  1432. err_reg_get:
  1433. regulator_bulk_free(ARRAY_SIZE(wm8995->supplies), wm8995->supplies);
  1434. return ret;
  1435. }
  1436. #define WM8995_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1437. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1438. static struct snd_soc_dai_ops wm8995_aif1_dai_ops = {
  1439. .set_sysclk = wm8995_set_dai_sysclk,
  1440. .set_fmt = wm8995_set_dai_fmt,
  1441. .hw_params = wm8995_hw_params,
  1442. .digital_mute = wm8995_aif_mute,
  1443. .set_pll = wm8995_set_fll,
  1444. .set_tristate = wm8995_set_tristate,
  1445. };
  1446. static struct snd_soc_dai_ops wm8995_aif2_dai_ops = {
  1447. .set_sysclk = wm8995_set_dai_sysclk,
  1448. .set_fmt = wm8995_set_dai_fmt,
  1449. .hw_params = wm8995_hw_params,
  1450. .digital_mute = wm8995_aif_mute,
  1451. .set_pll = wm8995_set_fll,
  1452. .set_tristate = wm8995_set_tristate,
  1453. };
  1454. static struct snd_soc_dai_ops wm8995_aif3_dai_ops = {
  1455. .set_tristate = wm8995_set_tristate,
  1456. };
  1457. static struct snd_soc_dai_driver wm8995_dai[] = {
  1458. {
  1459. .name = "wm8995-aif1",
  1460. .playback = {
  1461. .stream_name = "AIF1 Playback",
  1462. .channels_min = 2,
  1463. .channels_max = 2,
  1464. .rates = SNDRV_PCM_RATE_8000_96000,
  1465. .formats = WM8995_FORMATS
  1466. },
  1467. .capture = {
  1468. .stream_name = "AIF1 Capture",
  1469. .channels_min = 2,
  1470. .channels_max = 2,
  1471. .rates = SNDRV_PCM_RATE_8000_48000,
  1472. .formats = WM8995_FORMATS
  1473. },
  1474. .ops = &wm8995_aif1_dai_ops
  1475. },
  1476. {
  1477. .name = "wm8995-aif2",
  1478. .playback = {
  1479. .stream_name = "AIF2 Playback",
  1480. .channels_min = 2,
  1481. .channels_max = 2,
  1482. .rates = SNDRV_PCM_RATE_8000_96000,
  1483. .formats = WM8995_FORMATS
  1484. },
  1485. .capture = {
  1486. .stream_name = "AIF2 Capture",
  1487. .channels_min = 2,
  1488. .channels_max = 2,
  1489. .rates = SNDRV_PCM_RATE_8000_48000,
  1490. .formats = WM8995_FORMATS
  1491. },
  1492. .ops = &wm8995_aif2_dai_ops
  1493. },
  1494. {
  1495. .name = "wm8995-aif3",
  1496. .playback = {
  1497. .stream_name = "AIF3 Playback",
  1498. .channels_min = 2,
  1499. .channels_max = 2,
  1500. .rates = SNDRV_PCM_RATE_8000_96000,
  1501. .formats = WM8995_FORMATS
  1502. },
  1503. .capture = {
  1504. .stream_name = "AIF3 Capture",
  1505. .channels_min = 2,
  1506. .channels_max = 2,
  1507. .rates = SNDRV_PCM_RATE_8000_48000,
  1508. .formats = WM8995_FORMATS
  1509. },
  1510. .ops = &wm8995_aif3_dai_ops
  1511. }
  1512. };
  1513. static struct snd_soc_codec_driver soc_codec_dev_wm8995 = {
  1514. .probe = wm8995_probe,
  1515. .remove = wm8995_remove,
  1516. .suspend = wm8995_suspend,
  1517. .resume = wm8995_resume,
  1518. .set_bias_level = wm8995_set_bias_level,
  1519. .reg_cache_size = ARRAY_SIZE(wm8995_reg_defs),
  1520. .reg_word_size = sizeof(u16),
  1521. .reg_cache_default = wm8995_reg_defs,
  1522. .volatile_register = wm8995_volatile,
  1523. .compress_type = SND_SOC_RBTREE_COMPRESSION
  1524. };
  1525. #if defined(CONFIG_SPI_MASTER)
  1526. static int __devinit wm8995_spi_probe(struct spi_device *spi)
  1527. {
  1528. struct wm8995_priv *wm8995;
  1529. int ret;
  1530. wm8995 = kzalloc(sizeof *wm8995, GFP_KERNEL);
  1531. if (!wm8995)
  1532. return -ENOMEM;
  1533. wm8995->control_type = SND_SOC_SPI;
  1534. spi_set_drvdata(spi, wm8995);
  1535. ret = snd_soc_register_codec(&spi->dev,
  1536. &soc_codec_dev_wm8995, wm8995_dai,
  1537. ARRAY_SIZE(wm8995_dai));
  1538. if (ret < 0)
  1539. kfree(wm8995);
  1540. return ret;
  1541. }
  1542. static int __devexit wm8995_spi_remove(struct spi_device *spi)
  1543. {
  1544. snd_soc_unregister_codec(&spi->dev);
  1545. kfree(spi_get_drvdata(spi));
  1546. return 0;
  1547. }
  1548. static struct spi_driver wm8995_spi_driver = {
  1549. .driver = {
  1550. .name = "wm8995",
  1551. .owner = THIS_MODULE,
  1552. },
  1553. .probe = wm8995_spi_probe,
  1554. .remove = __devexit_p(wm8995_spi_remove)
  1555. };
  1556. #endif
  1557. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1558. static __devinit int wm8995_i2c_probe(struct i2c_client *i2c,
  1559. const struct i2c_device_id *id)
  1560. {
  1561. struct wm8995_priv *wm8995;
  1562. int ret;
  1563. wm8995 = kzalloc(sizeof *wm8995, GFP_KERNEL);
  1564. if (!wm8995)
  1565. return -ENOMEM;
  1566. wm8995->control_type = SND_SOC_I2C;
  1567. i2c_set_clientdata(i2c, wm8995);
  1568. ret = snd_soc_register_codec(&i2c->dev,
  1569. &soc_codec_dev_wm8995, wm8995_dai,
  1570. ARRAY_SIZE(wm8995_dai));
  1571. if (ret < 0)
  1572. kfree(wm8995);
  1573. return ret;
  1574. }
  1575. static __devexit int wm8995_i2c_remove(struct i2c_client *client)
  1576. {
  1577. snd_soc_unregister_codec(&client->dev);
  1578. kfree(i2c_get_clientdata(client));
  1579. return 0;
  1580. }
  1581. static const struct i2c_device_id wm8995_i2c_id[] = {
  1582. {"wm8995", 0},
  1583. {}
  1584. };
  1585. MODULE_DEVICE_TABLE(i2c, wm8995_i2c_id);
  1586. static struct i2c_driver wm8995_i2c_driver = {
  1587. .driver = {
  1588. .name = "wm8995",
  1589. .owner = THIS_MODULE,
  1590. },
  1591. .probe = wm8995_i2c_probe,
  1592. .remove = __devexit_p(wm8995_i2c_remove),
  1593. .id_table = wm8995_i2c_id
  1594. };
  1595. #endif
  1596. static int __init wm8995_modinit(void)
  1597. {
  1598. int ret = 0;
  1599. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1600. ret = i2c_add_driver(&wm8995_i2c_driver);
  1601. if (ret) {
  1602. printk(KERN_ERR "Failed to register wm8995 I2C driver: %d\n",
  1603. ret);
  1604. }
  1605. #endif
  1606. #if defined(CONFIG_SPI_MASTER)
  1607. ret = spi_register_driver(&wm8995_spi_driver);
  1608. if (ret) {
  1609. printk(KERN_ERR "Failed to register wm8995 SPI driver: %d\n",
  1610. ret);
  1611. }
  1612. #endif
  1613. return ret;
  1614. }
  1615. module_init(wm8995_modinit);
  1616. static void __exit wm8995_exit(void)
  1617. {
  1618. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1619. i2c_del_driver(&wm8995_i2c_driver);
  1620. #endif
  1621. #if defined(CONFIG_SPI_MASTER)
  1622. spi_unregister_driver(&wm8995_spi_driver);
  1623. #endif
  1624. }
  1625. module_exit(wm8995_exit);
  1626. MODULE_DESCRIPTION("ASoC WM8995 driver");
  1627. MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
  1628. MODULE_LICENSE("GPL");