wm8995.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  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_list *wlist = snd_kcontrol_chip(kcontrol);
  278. struct snd_soc_dapm_widget *w = wlist->widgets[0];
  279. struct snd_soc_codec *codec;
  280. int ret;
  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 change, 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. change = snd_soc_update_bits(codec, WM8995_CLOCKING_1,
  442. WM8995_SYSCLK_SRC_MASK, new);
  443. if (!change)
  444. return 0;
  445. snd_soc_dapm_sync(&codec->dapm);
  446. return 0;
  447. }
  448. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  449. struct snd_kcontrol *kcontrol, int event)
  450. {
  451. struct snd_soc_codec *codec;
  452. codec = w->codec;
  453. switch (event) {
  454. case SND_SOC_DAPM_PRE_PMU:
  455. return configure_clock(codec);
  456. case SND_SOC_DAPM_POST_PMD:
  457. configure_clock(codec);
  458. break;
  459. }
  460. return 0;
  461. }
  462. static const char *sidetone_text[] = {
  463. "ADC/DMIC1", "DMIC2",
  464. };
  465. static const struct soc_enum sidetone1_enum =
  466. SOC_ENUM_SINGLE(WM8995_SIDETONE, 0, 2, sidetone_text);
  467. static const struct snd_kcontrol_new sidetone1_mux =
  468. SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
  469. static const struct soc_enum sidetone2_enum =
  470. SOC_ENUM_SINGLE(WM8995_SIDETONE, 1, 2, sidetone_text);
  471. static const struct snd_kcontrol_new sidetone2_mux =
  472. SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
  473. static const struct snd_kcontrol_new aif1adc1l_mix[] = {
  474. SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8995_AIF1_ADC1_LEFT_MIXER_ROUTING,
  475. 1, 1, 0),
  476. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_AIF1_ADC1_LEFT_MIXER_ROUTING,
  477. 0, 1, 0),
  478. };
  479. static const struct snd_kcontrol_new aif1adc1r_mix[] = {
  480. SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8995_AIF1_ADC1_RIGHT_MIXER_ROUTING,
  481. 1, 1, 0),
  482. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_AIF1_ADC1_RIGHT_MIXER_ROUTING,
  483. 0, 1, 0),
  484. };
  485. static const struct snd_kcontrol_new aif1adc2l_mix[] = {
  486. SOC_DAPM_SINGLE("DMIC Switch", WM8995_AIF1_ADC2_LEFT_MIXER_ROUTING,
  487. 1, 1, 0),
  488. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_AIF1_ADC2_LEFT_MIXER_ROUTING,
  489. 0, 1, 0),
  490. };
  491. static const struct snd_kcontrol_new aif1adc2r_mix[] = {
  492. SOC_DAPM_SINGLE("DMIC Switch", WM8995_AIF1_ADC2_RIGHT_MIXER_ROUTING,
  493. 1, 1, 0),
  494. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_AIF1_ADC2_RIGHT_MIXER_ROUTING,
  495. 0, 1, 0),
  496. };
  497. static const struct snd_kcontrol_new dac1l_mix[] = {
  498. WM8995_CLASS_W_SWITCH("Right Sidetone Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  499. 5, 1, 0),
  500. WM8995_CLASS_W_SWITCH("Left Sidetone Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  501. 4, 1, 0),
  502. WM8995_CLASS_W_SWITCH("AIF2 Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  503. 2, 1, 0),
  504. WM8995_CLASS_W_SWITCH("AIF1.2 Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  505. 1, 1, 0),
  506. WM8995_CLASS_W_SWITCH("AIF1.1 Switch", WM8995_DAC1_LEFT_MIXER_ROUTING,
  507. 0, 1, 0),
  508. };
  509. static const struct snd_kcontrol_new dac1r_mix[] = {
  510. WM8995_CLASS_W_SWITCH("Right Sidetone Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  511. 5, 1, 0),
  512. WM8995_CLASS_W_SWITCH("Left Sidetone Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  513. 4, 1, 0),
  514. WM8995_CLASS_W_SWITCH("AIF2 Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  515. 2, 1, 0),
  516. WM8995_CLASS_W_SWITCH("AIF1.2 Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  517. 1, 1, 0),
  518. WM8995_CLASS_W_SWITCH("AIF1.1 Switch", WM8995_DAC1_RIGHT_MIXER_ROUTING,
  519. 0, 1, 0),
  520. };
  521. static const struct snd_kcontrol_new aif2dac2l_mix[] = {
  522. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  523. 5, 1, 0),
  524. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  525. 4, 1, 0),
  526. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  527. 2, 1, 0),
  528. SOC_DAPM_SINGLE("AIF1.2 Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  529. 1, 1, 0),
  530. SOC_DAPM_SINGLE("AIF1.1 Switch", WM8995_DAC2_LEFT_MIXER_ROUTING,
  531. 0, 1, 0),
  532. };
  533. static const struct snd_kcontrol_new aif2dac2r_mix[] = {
  534. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  535. 5, 1, 0),
  536. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  537. 4, 1, 0),
  538. SOC_DAPM_SINGLE("AIF2 Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  539. 2, 1, 0),
  540. SOC_DAPM_SINGLE("AIF1.2 Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  541. 1, 1, 0),
  542. SOC_DAPM_SINGLE("AIF1.1 Switch", WM8995_DAC2_RIGHT_MIXER_ROUTING,
  543. 0, 1, 0),
  544. };
  545. static const struct snd_kcontrol_new in1l_pga =
  546. SOC_DAPM_SINGLE("IN1L Switch", WM8995_POWER_MANAGEMENT_2, 5, 1, 0);
  547. static const struct snd_kcontrol_new in1r_pga =
  548. SOC_DAPM_SINGLE("IN1R Switch", WM8995_POWER_MANAGEMENT_2, 4, 1, 0);
  549. static const char *adc_mux_text[] = {
  550. "ADC",
  551. "DMIC",
  552. };
  553. static const struct soc_enum adc_enum =
  554. SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
  555. static const struct snd_kcontrol_new adcl_mux =
  556. SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
  557. static const struct snd_kcontrol_new adcr_mux =
  558. SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
  559. static const char *spk_src_text[] = {
  560. "DAC1L", "DAC1R", "DAC2L", "DAC2R"
  561. };
  562. static const SOC_ENUM_SINGLE_DECL(spk1l_src_enum, WM8995_LEFT_PDM_SPEAKER_1,
  563. 0, spk_src_text);
  564. static const SOC_ENUM_SINGLE_DECL(spk1r_src_enum, WM8995_RIGHT_PDM_SPEAKER_1,
  565. 0, spk_src_text);
  566. static const SOC_ENUM_SINGLE_DECL(spk2l_src_enum, WM8995_LEFT_PDM_SPEAKER_2,
  567. 0, spk_src_text);
  568. static const SOC_ENUM_SINGLE_DECL(spk2r_src_enum, WM8995_RIGHT_PDM_SPEAKER_2,
  569. 0, spk_src_text);
  570. static const struct snd_kcontrol_new spk1l_mux =
  571. SOC_DAPM_ENUM("SPK1L SRC", spk1l_src_enum);
  572. static const struct snd_kcontrol_new spk1r_mux =
  573. SOC_DAPM_ENUM("SPK1R SRC", spk1r_src_enum);
  574. static const struct snd_kcontrol_new spk2l_mux =
  575. SOC_DAPM_ENUM("SPK2L SRC", spk2l_src_enum);
  576. static const struct snd_kcontrol_new spk2r_mux =
  577. SOC_DAPM_ENUM("SPK2R SRC", spk2r_src_enum);
  578. static const struct snd_soc_dapm_widget wm8995_dapm_widgets[] = {
  579. SND_SOC_DAPM_INPUT("DMIC1DAT"),
  580. SND_SOC_DAPM_INPUT("DMIC2DAT"),
  581. SND_SOC_DAPM_INPUT("IN1L"),
  582. SND_SOC_DAPM_INPUT("IN1R"),
  583. SND_SOC_DAPM_MIXER("IN1L PGA", SND_SOC_NOPM, 0, 0,
  584. &in1l_pga, 1),
  585. SND_SOC_DAPM_MIXER("IN1R PGA", SND_SOC_NOPM, 0, 0,
  586. &in1r_pga, 1),
  587. SND_SOC_DAPM_MICBIAS("MICBIAS1", WM8995_POWER_MANAGEMENT_1, 8, 0),
  588. SND_SOC_DAPM_MICBIAS("MICBIAS2", WM8995_POWER_MANAGEMENT_1, 9, 0),
  589. SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8995_AIF1_CLOCKING_1, 0, 0, NULL, 0),
  590. SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8995_AIF2_CLOCKING_1, 0, 0, NULL, 0),
  591. SND_SOC_DAPM_SUPPLY("DSP1CLK", WM8995_CLOCKING_1, 3, 0, NULL, 0),
  592. SND_SOC_DAPM_SUPPLY("DSP2CLK", WM8995_CLOCKING_1, 2, 0, NULL, 0),
  593. SND_SOC_DAPM_SUPPLY("SYSDSPCLK", WM8995_CLOCKING_1, 1, 0, NULL, 0),
  594. SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
  595. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  596. SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", "AIF1 Capture", 0,
  597. WM8995_POWER_MANAGEMENT_3, 9, 0),
  598. SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", "AIF1 Capture", 0,
  599. WM8995_POWER_MANAGEMENT_3, 8, 0),
  600. SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", "AIF1 Capture", 0,
  601. SND_SOC_NOPM, 0, 0),
  602. SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", "AIF1 Capture",
  603. 0, WM8995_POWER_MANAGEMENT_3, 11, 0),
  604. SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", "AIF1 Capture",
  605. 0, WM8995_POWER_MANAGEMENT_3, 10, 0),
  606. SND_SOC_DAPM_VIRT_MUX("ADCL Mux", SND_SOC_NOPM, 1, 0,
  607. &adcl_mux),
  608. SND_SOC_DAPM_VIRT_MUX("ADCR Mux", SND_SOC_NOPM, 0, 0,
  609. &adcr_mux),
  610. SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8995_POWER_MANAGEMENT_3, 5, 0),
  611. SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8995_POWER_MANAGEMENT_3, 4, 0),
  612. SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8995_POWER_MANAGEMENT_3, 3, 0),
  613. SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8995_POWER_MANAGEMENT_3, 2, 0),
  614. SND_SOC_DAPM_ADC("ADCL", NULL, WM8995_POWER_MANAGEMENT_3, 1, 0),
  615. SND_SOC_DAPM_ADC("ADCR", NULL, WM8995_POWER_MANAGEMENT_3, 0, 0),
  616. SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
  617. aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
  618. SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
  619. aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
  620. SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
  621. aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
  622. SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
  623. aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
  624. SND_SOC_DAPM_AIF_IN("AIF1DAC1L", NULL, 0, WM8995_POWER_MANAGEMENT_4,
  625. 9, 0),
  626. SND_SOC_DAPM_AIF_IN("AIF1DAC1R", NULL, 0, WM8995_POWER_MANAGEMENT_4,
  627. 8, 0),
  628. SND_SOC_DAPM_AIF_IN("AIF1DACDAT", "AIF1 Playback", 0, SND_SOC_NOPM,
  629. 0, 0),
  630. SND_SOC_DAPM_AIF_IN("AIF1DAC2L", NULL, 0, WM8995_POWER_MANAGEMENT_4,
  631. 11, 0),
  632. SND_SOC_DAPM_AIF_IN("AIF1DAC2R", NULL, 0, WM8995_POWER_MANAGEMENT_4,
  633. 10, 0),
  634. SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
  635. aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
  636. SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
  637. aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
  638. SND_SOC_DAPM_DAC("DAC2L", NULL, WM8995_POWER_MANAGEMENT_4, 3, 0),
  639. SND_SOC_DAPM_DAC("DAC2R", NULL, WM8995_POWER_MANAGEMENT_4, 2, 0),
  640. SND_SOC_DAPM_DAC("DAC1L", NULL, WM8995_POWER_MANAGEMENT_4, 1, 0),
  641. SND_SOC_DAPM_DAC("DAC1R", NULL, WM8995_POWER_MANAGEMENT_4, 0, 0),
  642. SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0, dac1l_mix,
  643. ARRAY_SIZE(dac1l_mix)),
  644. SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0, dac1r_mix,
  645. ARRAY_SIZE(dac1r_mix)),
  646. SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
  647. SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
  648. SND_SOC_DAPM_PGA_E("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
  649. hp_event, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  650. SND_SOC_DAPM_SUPPLY("Headphone Supply", SND_SOC_NOPM, 0, 0,
  651. hp_supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  652. SND_SOC_DAPM_MUX("SPK1L Driver", WM8995_LEFT_PDM_SPEAKER_1,
  653. 4, 0, &spk1l_mux),
  654. SND_SOC_DAPM_MUX("SPK1R Driver", WM8995_RIGHT_PDM_SPEAKER_1,
  655. 4, 0, &spk1r_mux),
  656. SND_SOC_DAPM_MUX("SPK2L Driver", WM8995_LEFT_PDM_SPEAKER_2,
  657. 4, 0, &spk2l_mux),
  658. SND_SOC_DAPM_MUX("SPK2R Driver", WM8995_RIGHT_PDM_SPEAKER_2,
  659. 4, 0, &spk2r_mux),
  660. SND_SOC_DAPM_SUPPLY("LDO2", WM8995_POWER_MANAGEMENT_2, 1, 0, NULL, 0),
  661. SND_SOC_DAPM_OUTPUT("HP1L"),
  662. SND_SOC_DAPM_OUTPUT("HP1R"),
  663. SND_SOC_DAPM_OUTPUT("SPK1L"),
  664. SND_SOC_DAPM_OUTPUT("SPK1R"),
  665. SND_SOC_DAPM_OUTPUT("SPK2L"),
  666. SND_SOC_DAPM_OUTPUT("SPK2R")
  667. };
  668. static const struct snd_soc_dapm_route wm8995_intercon[] = {
  669. { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
  670. { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
  671. { "DSP1CLK", NULL, "CLK_SYS" },
  672. { "DSP2CLK", NULL, "CLK_SYS" },
  673. { "SYSDSPCLK", NULL, "CLK_SYS" },
  674. { "AIF1ADC1L", NULL, "AIF1CLK" },
  675. { "AIF1ADC1L", NULL, "DSP1CLK" },
  676. { "AIF1ADC1R", NULL, "AIF1CLK" },
  677. { "AIF1ADC1R", NULL, "DSP1CLK" },
  678. { "AIF1ADC1R", NULL, "SYSDSPCLK" },
  679. { "AIF1ADC2L", NULL, "AIF1CLK" },
  680. { "AIF1ADC2L", NULL, "DSP1CLK" },
  681. { "AIF1ADC2R", NULL, "AIF1CLK" },
  682. { "AIF1ADC2R", NULL, "DSP1CLK" },
  683. { "AIF1ADC2R", NULL, "SYSDSPCLK" },
  684. { "DMIC1L", NULL, "DMIC1DAT" },
  685. { "DMIC1L", NULL, "CLK_SYS" },
  686. { "DMIC1R", NULL, "DMIC1DAT" },
  687. { "DMIC1R", NULL, "CLK_SYS" },
  688. { "DMIC2L", NULL, "DMIC2DAT" },
  689. { "DMIC2L", NULL, "CLK_SYS" },
  690. { "DMIC2R", NULL, "DMIC2DAT" },
  691. { "DMIC2R", NULL, "CLK_SYS" },
  692. { "ADCL", NULL, "AIF1CLK" },
  693. { "ADCL", NULL, "DSP1CLK" },
  694. { "ADCL", NULL, "SYSDSPCLK" },
  695. { "ADCR", NULL, "AIF1CLK" },
  696. { "ADCR", NULL, "DSP1CLK" },
  697. { "ADCR", NULL, "SYSDSPCLK" },
  698. { "IN1L PGA", "IN1L Switch", "IN1L" },
  699. { "IN1R PGA", "IN1R Switch", "IN1R" },
  700. { "IN1L PGA", NULL, "LDO2" },
  701. { "IN1R PGA", NULL, "LDO2" },
  702. { "ADCL", NULL, "IN1L PGA" },
  703. { "ADCR", NULL, "IN1R PGA" },
  704. { "ADCL Mux", "ADC", "ADCL" },
  705. { "ADCL Mux", "DMIC", "DMIC1L" },
  706. { "ADCR Mux", "ADC", "ADCR" },
  707. { "ADCR Mux", "DMIC", "DMIC1R" },
  708. /* AIF1 outputs */
  709. { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
  710. { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
  711. { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
  712. { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
  713. { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
  714. { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
  715. { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
  716. { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
  717. /* Sidetone */
  718. { "Left Sidetone", "ADC/DMIC1", "AIF1ADC1L" },
  719. { "Left Sidetone", "DMIC2", "AIF1ADC2L" },
  720. { "Right Sidetone", "ADC/DMIC1", "AIF1ADC1R" },
  721. { "Right Sidetone", "DMIC2", "AIF1ADC2R" },
  722. { "AIF1DAC1L", NULL, "AIF1CLK" },
  723. { "AIF1DAC1L", NULL, "DSP1CLK" },
  724. { "AIF1DAC1R", NULL, "AIF1CLK" },
  725. { "AIF1DAC1R", NULL, "DSP1CLK" },
  726. { "AIF1DAC1R", NULL, "SYSDSPCLK" },
  727. { "AIF1DAC2L", NULL, "AIF1CLK" },
  728. { "AIF1DAC2L", NULL, "DSP1CLK" },
  729. { "AIF1DAC2R", NULL, "AIF1CLK" },
  730. { "AIF1DAC2R", NULL, "DSP1CLK" },
  731. { "AIF1DAC2R", NULL, "SYSDSPCLK" },
  732. { "DAC1L", NULL, "AIF1CLK" },
  733. { "DAC1L", NULL, "DSP1CLK" },
  734. { "DAC1L", NULL, "SYSDSPCLK" },
  735. { "DAC1R", NULL, "AIF1CLK" },
  736. { "DAC1R", NULL, "DSP1CLK" },
  737. { "DAC1R", NULL, "SYSDSPCLK" },
  738. { "AIF1DAC1L", NULL, "AIF1DACDAT" },
  739. { "AIF1DAC1R", NULL, "AIF1DACDAT" },
  740. { "AIF1DAC2L", NULL, "AIF1DACDAT" },
  741. { "AIF1DAC2R", NULL, "AIF1DACDAT" },
  742. /* DAC1 inputs */
  743. { "DAC1L", NULL, "DAC1L Mixer" },
  744. { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
  745. { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
  746. { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
  747. { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
  748. { "DAC1R", NULL, "DAC1R Mixer" },
  749. { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
  750. { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
  751. { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
  752. { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
  753. /* DAC2/AIF2 outputs */
  754. { "DAC2L", NULL, "AIF2DAC2L Mixer" },
  755. { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
  756. { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
  757. { "DAC2R", NULL, "AIF2DAC2R Mixer" },
  758. { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
  759. { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
  760. /* Output stages */
  761. { "Headphone PGA", NULL, "DAC1L" },
  762. { "Headphone PGA", NULL, "DAC1R" },
  763. { "Headphone PGA", NULL, "DAC2L" },
  764. { "Headphone PGA", NULL, "DAC2R" },
  765. { "Headphone PGA", NULL, "Headphone Supply" },
  766. { "Headphone PGA", NULL, "CLK_SYS" },
  767. { "Headphone PGA", NULL, "LDO2" },
  768. { "HP1L", NULL, "Headphone PGA" },
  769. { "HP1R", NULL, "Headphone PGA" },
  770. { "SPK1L Driver", "DAC1L", "DAC1L" },
  771. { "SPK1L Driver", "DAC1R", "DAC1R" },
  772. { "SPK1L Driver", "DAC2L", "DAC2L" },
  773. { "SPK1L Driver", "DAC2R", "DAC2R" },
  774. { "SPK1L Driver", NULL, "CLK_SYS" },
  775. { "SPK1R Driver", "DAC1L", "DAC1L" },
  776. { "SPK1R Driver", "DAC1R", "DAC1R" },
  777. { "SPK1R Driver", "DAC2L", "DAC2L" },
  778. { "SPK1R Driver", "DAC2R", "DAC2R" },
  779. { "SPK1R Driver", NULL, "CLK_SYS" },
  780. { "SPK2L Driver", "DAC1L", "DAC1L" },
  781. { "SPK2L Driver", "DAC1R", "DAC1R" },
  782. { "SPK2L Driver", "DAC2L", "DAC2L" },
  783. { "SPK2L Driver", "DAC2R", "DAC2R" },
  784. { "SPK2L Driver", NULL, "CLK_SYS" },
  785. { "SPK2R Driver", "DAC1L", "DAC1L" },
  786. { "SPK2R Driver", "DAC1R", "DAC1R" },
  787. { "SPK2R Driver", "DAC2L", "DAC2L" },
  788. { "SPK2R Driver", "DAC2R", "DAC2R" },
  789. { "SPK2R Driver", NULL, "CLK_SYS" },
  790. { "SPK1L", NULL, "SPK1L Driver" },
  791. { "SPK1R", NULL, "SPK1R Driver" },
  792. { "SPK2L", NULL, "SPK2L Driver" },
  793. { "SPK2R", NULL, "SPK2R Driver" }
  794. };
  795. static int wm8995_volatile(struct snd_soc_codec *codec, unsigned int reg)
  796. {
  797. /* out of bounds registers are generally considered
  798. * volatile to support register banks that are partially
  799. * owned by something else for e.g. a DSP
  800. */
  801. if (reg > WM8995_MAX_CACHED_REGISTER)
  802. return 1;
  803. switch (reg) {
  804. case WM8995_SOFTWARE_RESET:
  805. case WM8995_DC_SERVO_READBACK_0:
  806. case WM8995_INTERRUPT_STATUS_1:
  807. case WM8995_INTERRUPT_STATUS_2:
  808. case WM8995_INTERRUPT_STATUS_1_MASK:
  809. case WM8995_INTERRUPT_STATUS_2_MASK:
  810. case WM8995_INTERRUPT_CONTROL:
  811. case WM8995_ACCESSORY_DETECT_MODE1:
  812. case WM8995_ACCESSORY_DETECT_MODE2:
  813. case WM8995_HEADPHONE_DETECT1:
  814. case WM8995_HEADPHONE_DETECT2:
  815. return 1;
  816. }
  817. return 0;
  818. }
  819. static int wm8995_aif_mute(struct snd_soc_dai *dai, int mute)
  820. {
  821. struct snd_soc_codec *codec = dai->codec;
  822. int mute_reg;
  823. switch (dai->id) {
  824. case 0:
  825. mute_reg = WM8995_AIF1_DAC1_FILTERS_1;
  826. break;
  827. case 1:
  828. mute_reg = WM8995_AIF2_DAC_FILTERS_1;
  829. break;
  830. default:
  831. return -EINVAL;
  832. }
  833. snd_soc_update_bits(codec, mute_reg, WM8995_AIF1DAC1_MUTE_MASK,
  834. !!mute << WM8995_AIF1DAC1_MUTE_SHIFT);
  835. return 0;
  836. }
  837. static int wm8995_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  838. {
  839. struct snd_soc_codec *codec;
  840. int master;
  841. int aif;
  842. codec = dai->codec;
  843. master = 0;
  844. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  845. case SND_SOC_DAIFMT_CBS_CFS:
  846. break;
  847. case SND_SOC_DAIFMT_CBM_CFM:
  848. master = WM8995_AIF1_MSTR;
  849. break;
  850. default:
  851. dev_err(dai->dev, "Unknown master/slave configuration\n");
  852. return -EINVAL;
  853. }
  854. aif = 0;
  855. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  856. case SND_SOC_DAIFMT_DSP_B:
  857. aif |= WM8995_AIF1_LRCLK_INV;
  858. case SND_SOC_DAIFMT_DSP_A:
  859. aif |= (0x3 << WM8995_AIF1_FMT_SHIFT);
  860. break;
  861. case SND_SOC_DAIFMT_I2S:
  862. aif |= (0x2 << WM8995_AIF1_FMT_SHIFT);
  863. break;
  864. case SND_SOC_DAIFMT_RIGHT_J:
  865. break;
  866. case SND_SOC_DAIFMT_LEFT_J:
  867. aif |= (0x1 << WM8995_AIF1_FMT_SHIFT);
  868. break;
  869. default:
  870. dev_err(dai->dev, "Unknown dai format\n");
  871. return -EINVAL;
  872. }
  873. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  874. case SND_SOC_DAIFMT_DSP_A:
  875. case SND_SOC_DAIFMT_DSP_B:
  876. /* frame inversion not valid for DSP modes */
  877. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  878. case SND_SOC_DAIFMT_NB_NF:
  879. break;
  880. case SND_SOC_DAIFMT_IB_NF:
  881. aif |= WM8995_AIF1_BCLK_INV;
  882. break;
  883. default:
  884. return -EINVAL;
  885. }
  886. break;
  887. case SND_SOC_DAIFMT_I2S:
  888. case SND_SOC_DAIFMT_RIGHT_J:
  889. case SND_SOC_DAIFMT_LEFT_J:
  890. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  891. case SND_SOC_DAIFMT_NB_NF:
  892. break;
  893. case SND_SOC_DAIFMT_IB_IF:
  894. aif |= WM8995_AIF1_BCLK_INV | WM8995_AIF1_LRCLK_INV;
  895. break;
  896. case SND_SOC_DAIFMT_IB_NF:
  897. aif |= WM8995_AIF1_BCLK_INV;
  898. break;
  899. case SND_SOC_DAIFMT_NB_IF:
  900. aif |= WM8995_AIF1_LRCLK_INV;
  901. break;
  902. default:
  903. return -EINVAL;
  904. }
  905. break;
  906. default:
  907. return -EINVAL;
  908. }
  909. snd_soc_update_bits(codec, WM8995_AIF1_CONTROL_1,
  910. WM8995_AIF1_BCLK_INV_MASK |
  911. WM8995_AIF1_LRCLK_INV_MASK |
  912. WM8995_AIF1_FMT_MASK, aif);
  913. snd_soc_update_bits(codec, WM8995_AIF1_MASTER_SLAVE,
  914. WM8995_AIF1_MSTR_MASK, master);
  915. return 0;
  916. }
  917. static const int srs[] = {
  918. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
  919. 48000, 88200, 96000
  920. };
  921. static const int fs_ratios[] = {
  922. -1 /* reserved */,
  923. 128, 192, 256, 384, 512, 768, 1024, 1408, 1536
  924. };
  925. static const int bclk_divs[] = {
  926. 10, 15, 20, 30, 40, 55, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480
  927. };
  928. static int wm8995_hw_params(struct snd_pcm_substream *substream,
  929. struct snd_pcm_hw_params *params,
  930. struct snd_soc_dai *dai)
  931. {
  932. struct snd_soc_codec *codec;
  933. struct wm8995_priv *wm8995;
  934. int aif1_reg;
  935. int bclk_reg;
  936. int lrclk_reg;
  937. int rate_reg;
  938. int bclk_rate;
  939. int aif1;
  940. int lrclk, bclk;
  941. int i, rate_val, best, best_val, cur_val;
  942. codec = dai->codec;
  943. wm8995 = snd_soc_codec_get_drvdata(codec);
  944. switch (dai->id) {
  945. case 0:
  946. aif1_reg = WM8995_AIF1_CONTROL_1;
  947. bclk_reg = WM8995_AIF1_BCLK;
  948. rate_reg = WM8995_AIF1_RATE;
  949. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK /* ||
  950. wm8995->lrclk_shared[0] */) {
  951. lrclk_reg = WM8995_AIF1DAC_LRCLK;
  952. } else {
  953. lrclk_reg = WM8995_AIF1ADC_LRCLK;
  954. dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
  955. }
  956. break;
  957. case 1:
  958. aif1_reg = WM8995_AIF2_CONTROL_1;
  959. bclk_reg = WM8995_AIF2_BCLK;
  960. rate_reg = WM8995_AIF2_RATE;
  961. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK /* ||
  962. wm8995->lrclk_shared[1] */) {
  963. lrclk_reg = WM8995_AIF2DAC_LRCLK;
  964. } else {
  965. lrclk_reg = WM8995_AIF2ADC_LRCLK;
  966. dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
  967. }
  968. break;
  969. default:
  970. return -EINVAL;
  971. }
  972. bclk_rate = snd_soc_params_to_bclk(params);
  973. if (bclk_rate < 0)
  974. return bclk_rate;
  975. aif1 = 0;
  976. switch (params_format(params)) {
  977. case SNDRV_PCM_FORMAT_S16_LE:
  978. break;
  979. case SNDRV_PCM_FORMAT_S20_3LE:
  980. aif1 |= (0x1 << WM8995_AIF1_WL_SHIFT);
  981. break;
  982. case SNDRV_PCM_FORMAT_S24_LE:
  983. aif1 |= (0x2 << WM8995_AIF1_WL_SHIFT);
  984. break;
  985. case SNDRV_PCM_FORMAT_S32_LE:
  986. aif1 |= (0x3 << WM8995_AIF1_WL_SHIFT);
  987. break;
  988. default:
  989. dev_err(dai->dev, "Unsupported word length %u\n",
  990. params_format(params));
  991. return -EINVAL;
  992. }
  993. /* try to find a suitable sample rate */
  994. for (i = 0; i < ARRAY_SIZE(srs); ++i)
  995. if (srs[i] == params_rate(params))
  996. break;
  997. if (i == ARRAY_SIZE(srs)) {
  998. dev_err(dai->dev, "Sample rate %d is not supported\n",
  999. params_rate(params));
  1000. return -EINVAL;
  1001. }
  1002. rate_val = i << WM8995_AIF1_SR_SHIFT;
  1003. dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i]);
  1004. dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
  1005. dai->id + 1, wm8995->aifclk[dai->id], bclk_rate);
  1006. /* AIFCLK/fs ratio; look for a close match in either direction */
  1007. best = 1;
  1008. best_val = abs((fs_ratios[1] * params_rate(params))
  1009. - wm8995->aifclk[dai->id]);
  1010. for (i = 2; i < ARRAY_SIZE(fs_ratios); i++) {
  1011. cur_val = abs((fs_ratios[i] * params_rate(params))
  1012. - wm8995->aifclk[dai->id]);
  1013. if (cur_val >= best_val)
  1014. continue;
  1015. best = i;
  1016. best_val = cur_val;
  1017. }
  1018. rate_val |= best;
  1019. dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
  1020. dai->id + 1, fs_ratios[best]);
  1021. /*
  1022. * We may not get quite the right frequency if using
  1023. * approximate clocks so look for the closest match that is
  1024. * higher than the target (we need to ensure that there enough
  1025. * BCLKs to clock out the samples).
  1026. */
  1027. best = 0;
  1028. bclk = 0;
  1029. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1030. cur_val = (wm8995->aifclk[dai->id] * 10 / bclk_divs[i]) - bclk_rate;
  1031. if (cur_val < 0) /* BCLK table is sorted */
  1032. break;
  1033. best = i;
  1034. }
  1035. bclk |= best << WM8995_AIF1_BCLK_DIV_SHIFT;
  1036. bclk_rate = wm8995->aifclk[dai->id] * 10 / bclk_divs[best];
  1037. dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
  1038. bclk_divs[best], bclk_rate);
  1039. lrclk = bclk_rate / params_rate(params);
  1040. dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
  1041. lrclk, bclk_rate / lrclk);
  1042. snd_soc_update_bits(codec, aif1_reg,
  1043. WM8995_AIF1_WL_MASK, aif1);
  1044. snd_soc_update_bits(codec, bclk_reg,
  1045. WM8995_AIF1_BCLK_DIV_MASK, bclk);
  1046. snd_soc_update_bits(codec, lrclk_reg,
  1047. WM8995_AIF1DAC_RATE_MASK, lrclk);
  1048. snd_soc_update_bits(codec, rate_reg,
  1049. WM8995_AIF1_SR_MASK |
  1050. WM8995_AIF1CLK_RATE_MASK, rate_val);
  1051. return 0;
  1052. }
  1053. static int wm8995_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
  1054. {
  1055. struct snd_soc_codec *codec = codec_dai->codec;
  1056. int reg, val, mask;
  1057. switch (codec_dai->id) {
  1058. case 0:
  1059. reg = WM8995_AIF1_MASTER_SLAVE;
  1060. mask = WM8995_AIF1_TRI;
  1061. break;
  1062. case 1:
  1063. reg = WM8995_AIF2_MASTER_SLAVE;
  1064. mask = WM8995_AIF2_TRI;
  1065. break;
  1066. case 2:
  1067. reg = WM8995_POWER_MANAGEMENT_5;
  1068. mask = WM8995_AIF3_TRI;
  1069. break;
  1070. default:
  1071. return -EINVAL;
  1072. }
  1073. if (tristate)
  1074. val = mask;
  1075. else
  1076. val = 0;
  1077. return snd_soc_update_bits(codec, reg, mask, val);
  1078. }
  1079. /* The size in bits of the FLL divide multiplied by 10
  1080. * to allow rounding later */
  1081. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  1082. struct fll_div {
  1083. u16 outdiv;
  1084. u16 n;
  1085. u16 k;
  1086. u16 clk_ref_div;
  1087. u16 fll_fratio;
  1088. };
  1089. static int wm8995_get_fll_config(struct fll_div *fll,
  1090. int freq_in, int freq_out)
  1091. {
  1092. u64 Kpart;
  1093. unsigned int K, Ndiv, Nmod;
  1094. pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
  1095. /* Scale the input frequency down to <= 13.5MHz */
  1096. fll->clk_ref_div = 0;
  1097. while (freq_in > 13500000) {
  1098. fll->clk_ref_div++;
  1099. freq_in /= 2;
  1100. if (fll->clk_ref_div > 3)
  1101. return -EINVAL;
  1102. }
  1103. pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
  1104. /* Scale the output to give 90MHz<=Fvco<=100MHz */
  1105. fll->outdiv = 3;
  1106. while (freq_out * (fll->outdiv + 1) < 90000000) {
  1107. fll->outdiv++;
  1108. if (fll->outdiv > 63)
  1109. return -EINVAL;
  1110. }
  1111. freq_out *= fll->outdiv + 1;
  1112. pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
  1113. if (freq_in > 1000000) {
  1114. fll->fll_fratio = 0;
  1115. } else if (freq_in > 256000) {
  1116. fll->fll_fratio = 1;
  1117. freq_in *= 2;
  1118. } else if (freq_in > 128000) {
  1119. fll->fll_fratio = 2;
  1120. freq_in *= 4;
  1121. } else if (freq_in > 64000) {
  1122. fll->fll_fratio = 3;
  1123. freq_in *= 8;
  1124. } else {
  1125. fll->fll_fratio = 4;
  1126. freq_in *= 16;
  1127. }
  1128. pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
  1129. /* Now, calculate N.K */
  1130. Ndiv = freq_out / freq_in;
  1131. fll->n = Ndiv;
  1132. Nmod = freq_out % freq_in;
  1133. pr_debug("Nmod=%d\n", Nmod);
  1134. /* Calculate fractional part - scale up so we can round. */
  1135. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  1136. do_div(Kpart, freq_in);
  1137. K = Kpart & 0xFFFFFFFF;
  1138. if ((K % 10) >= 5)
  1139. K += 5;
  1140. /* Move down to proper range now rounding is done */
  1141. fll->k = K / 10;
  1142. pr_debug("N=%x K=%x\n", fll->n, fll->k);
  1143. return 0;
  1144. }
  1145. static int wm8995_set_fll(struct snd_soc_dai *dai, int id,
  1146. int src, unsigned int freq_in,
  1147. unsigned int freq_out)
  1148. {
  1149. struct snd_soc_codec *codec;
  1150. struct wm8995_priv *wm8995;
  1151. int reg_offset, ret;
  1152. struct fll_div fll;
  1153. u16 reg, aif1, aif2;
  1154. codec = dai->codec;
  1155. wm8995 = snd_soc_codec_get_drvdata(codec);
  1156. aif1 = snd_soc_read(codec, WM8995_AIF1_CLOCKING_1)
  1157. & WM8995_AIF1CLK_ENA;
  1158. aif2 = snd_soc_read(codec, WM8995_AIF2_CLOCKING_1)
  1159. & WM8995_AIF2CLK_ENA;
  1160. switch (id) {
  1161. case WM8995_FLL1:
  1162. reg_offset = 0;
  1163. id = 0;
  1164. break;
  1165. case WM8995_FLL2:
  1166. reg_offset = 0x20;
  1167. id = 1;
  1168. break;
  1169. default:
  1170. return -EINVAL;
  1171. }
  1172. switch (src) {
  1173. case 0:
  1174. /* Allow no source specification when stopping */
  1175. if (freq_out)
  1176. return -EINVAL;
  1177. break;
  1178. case WM8995_FLL_SRC_MCLK1:
  1179. case WM8995_FLL_SRC_MCLK2:
  1180. case WM8995_FLL_SRC_LRCLK:
  1181. case WM8995_FLL_SRC_BCLK:
  1182. break;
  1183. default:
  1184. return -EINVAL;
  1185. }
  1186. /* Are we changing anything? */
  1187. if (wm8995->fll[id].src == src &&
  1188. wm8995->fll[id].in == freq_in && wm8995->fll[id].out == freq_out)
  1189. return 0;
  1190. /* If we're stopping the FLL redo the old config - no
  1191. * registers will actually be written but we avoid GCC flow
  1192. * analysis bugs spewing warnings.
  1193. */
  1194. if (freq_out)
  1195. ret = wm8995_get_fll_config(&fll, freq_in, freq_out);
  1196. else
  1197. ret = wm8995_get_fll_config(&fll, wm8995->fll[id].in,
  1198. wm8995->fll[id].out);
  1199. if (ret < 0)
  1200. return ret;
  1201. /* Gate the AIF clocks while we reclock */
  1202. snd_soc_update_bits(codec, WM8995_AIF1_CLOCKING_1,
  1203. WM8995_AIF1CLK_ENA_MASK, 0);
  1204. snd_soc_update_bits(codec, WM8995_AIF2_CLOCKING_1,
  1205. WM8995_AIF2CLK_ENA_MASK, 0);
  1206. /* We always need to disable the FLL while reconfiguring */
  1207. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_1 + reg_offset,
  1208. WM8995_FLL1_ENA_MASK, 0);
  1209. reg = (fll.outdiv << WM8995_FLL1_OUTDIV_SHIFT) |
  1210. (fll.fll_fratio << WM8995_FLL1_FRATIO_SHIFT);
  1211. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_2 + reg_offset,
  1212. WM8995_FLL1_OUTDIV_MASK |
  1213. WM8995_FLL1_FRATIO_MASK, reg);
  1214. snd_soc_write(codec, WM8995_FLL1_CONTROL_3 + reg_offset, fll.k);
  1215. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_4 + reg_offset,
  1216. WM8995_FLL1_N_MASK,
  1217. fll.n << WM8995_FLL1_N_SHIFT);
  1218. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_5 + reg_offset,
  1219. WM8995_FLL1_REFCLK_DIV_MASK |
  1220. WM8995_FLL1_REFCLK_SRC_MASK,
  1221. (fll.clk_ref_div << WM8995_FLL1_REFCLK_DIV_SHIFT) |
  1222. (src - 1));
  1223. if (freq_out)
  1224. snd_soc_update_bits(codec, WM8995_FLL1_CONTROL_1 + reg_offset,
  1225. WM8995_FLL1_ENA_MASK, WM8995_FLL1_ENA);
  1226. wm8995->fll[id].in = freq_in;
  1227. wm8995->fll[id].out = freq_out;
  1228. wm8995->fll[id].src = src;
  1229. /* Enable any gated AIF clocks */
  1230. snd_soc_update_bits(codec, WM8995_AIF1_CLOCKING_1,
  1231. WM8995_AIF1CLK_ENA_MASK, aif1);
  1232. snd_soc_update_bits(codec, WM8995_AIF2_CLOCKING_1,
  1233. WM8995_AIF2CLK_ENA_MASK, aif2);
  1234. configure_clock(codec);
  1235. return 0;
  1236. }
  1237. static int wm8995_set_dai_sysclk(struct snd_soc_dai *dai,
  1238. int clk_id, unsigned int freq, int dir)
  1239. {
  1240. struct snd_soc_codec *codec;
  1241. struct wm8995_priv *wm8995;
  1242. codec = dai->codec;
  1243. wm8995 = snd_soc_codec_get_drvdata(codec);
  1244. switch (dai->id) {
  1245. case 0:
  1246. case 1:
  1247. break;
  1248. default:
  1249. /* AIF3 shares clocking with AIF1/2 */
  1250. return -EINVAL;
  1251. }
  1252. switch (clk_id) {
  1253. case WM8995_SYSCLK_MCLK1:
  1254. wm8995->sysclk[dai->id] = WM8995_SYSCLK_MCLK1;
  1255. wm8995->mclk[0] = freq;
  1256. dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
  1257. dai->id + 1, freq);
  1258. break;
  1259. case WM8995_SYSCLK_MCLK2:
  1260. wm8995->sysclk[dai->id] = WM8995_SYSCLK_MCLK1;
  1261. wm8995->mclk[1] = freq;
  1262. dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
  1263. dai->id + 1, freq);
  1264. break;
  1265. case WM8995_SYSCLK_FLL1:
  1266. wm8995->sysclk[dai->id] = WM8995_SYSCLK_FLL1;
  1267. dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id + 1);
  1268. break;
  1269. case WM8995_SYSCLK_FLL2:
  1270. wm8995->sysclk[dai->id] = WM8995_SYSCLK_FLL2;
  1271. dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id + 1);
  1272. break;
  1273. case WM8995_SYSCLK_OPCLK:
  1274. default:
  1275. dev_err(dai->dev, "Unknown clock source %d\n", clk_id);
  1276. return -EINVAL;
  1277. }
  1278. configure_clock(codec);
  1279. return 0;
  1280. }
  1281. static int wm8995_set_bias_level(struct snd_soc_codec *codec,
  1282. enum snd_soc_bias_level level)
  1283. {
  1284. struct wm8995_priv *wm8995;
  1285. int ret;
  1286. wm8995 = snd_soc_codec_get_drvdata(codec);
  1287. switch (level) {
  1288. case SND_SOC_BIAS_ON:
  1289. case SND_SOC_BIAS_PREPARE:
  1290. break;
  1291. case SND_SOC_BIAS_STANDBY:
  1292. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  1293. ret = regulator_bulk_enable(ARRAY_SIZE(wm8995->supplies),
  1294. wm8995->supplies);
  1295. if (ret)
  1296. return ret;
  1297. ret = snd_soc_cache_sync(codec);
  1298. if (ret) {
  1299. dev_err(codec->dev,
  1300. "Failed to sync cache: %d\n", ret);
  1301. return ret;
  1302. }
  1303. snd_soc_update_bits(codec, WM8995_POWER_MANAGEMENT_1,
  1304. WM8995_BG_ENA_MASK, WM8995_BG_ENA);
  1305. }
  1306. break;
  1307. case SND_SOC_BIAS_OFF:
  1308. snd_soc_update_bits(codec, WM8995_POWER_MANAGEMENT_1,
  1309. WM8995_BG_ENA_MASK, 0);
  1310. regulator_bulk_disable(ARRAY_SIZE(wm8995->supplies),
  1311. wm8995->supplies);
  1312. break;
  1313. }
  1314. codec->dapm.bias_level = level;
  1315. return 0;
  1316. }
  1317. #ifdef CONFIG_PM
  1318. static int wm8995_suspend(struct snd_soc_codec *codec, pm_message_t state)
  1319. {
  1320. wm8995_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1321. return 0;
  1322. }
  1323. static int wm8995_resume(struct snd_soc_codec *codec)
  1324. {
  1325. wm8995_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1326. return 0;
  1327. }
  1328. #else
  1329. #define wm8995_suspend NULL
  1330. #define wm8995_resume NULL
  1331. #endif
  1332. static int wm8995_remove(struct snd_soc_codec *codec)
  1333. {
  1334. struct wm8995_priv *wm8995;
  1335. int i;
  1336. wm8995 = snd_soc_codec_get_drvdata(codec);
  1337. wm8995_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1338. for (i = 0; i < ARRAY_SIZE(wm8995->supplies); ++i)
  1339. regulator_unregister_notifier(wm8995->supplies[i].consumer,
  1340. &wm8995->disable_nb[i]);
  1341. regulator_bulk_free(ARRAY_SIZE(wm8995->supplies), wm8995->supplies);
  1342. return 0;
  1343. }
  1344. static int wm8995_probe(struct snd_soc_codec *codec)
  1345. {
  1346. struct wm8995_priv *wm8995;
  1347. int i;
  1348. int ret;
  1349. codec->dapm.idle_bias_off = 1;
  1350. wm8995 = snd_soc_codec_get_drvdata(codec);
  1351. wm8995->codec = codec;
  1352. ret = snd_soc_codec_set_cache_io(codec, 16, 16, wm8995->control_type);
  1353. if (ret < 0) {
  1354. dev_err(codec->dev, "Failed to set cache i/o: %d\n", ret);
  1355. return ret;
  1356. }
  1357. for (i = 0; i < ARRAY_SIZE(wm8995->supplies); i++)
  1358. wm8995->supplies[i].supply = wm8995_supply_names[i];
  1359. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8995->supplies),
  1360. wm8995->supplies);
  1361. if (ret) {
  1362. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1363. return ret;
  1364. }
  1365. wm8995->disable_nb[0].notifier_call = wm8995_regulator_event_0;
  1366. wm8995->disable_nb[1].notifier_call = wm8995_regulator_event_1;
  1367. wm8995->disable_nb[2].notifier_call = wm8995_regulator_event_2;
  1368. wm8995->disable_nb[3].notifier_call = wm8995_regulator_event_3;
  1369. wm8995->disable_nb[4].notifier_call = wm8995_regulator_event_4;
  1370. wm8995->disable_nb[5].notifier_call = wm8995_regulator_event_5;
  1371. wm8995->disable_nb[6].notifier_call = wm8995_regulator_event_6;
  1372. wm8995->disable_nb[7].notifier_call = wm8995_regulator_event_7;
  1373. /* This should really be moved into the regulator core */
  1374. for (i = 0; i < ARRAY_SIZE(wm8995->supplies); i++) {
  1375. ret = regulator_register_notifier(wm8995->supplies[i].consumer,
  1376. &wm8995->disable_nb[i]);
  1377. if (ret) {
  1378. dev_err(codec->dev,
  1379. "Failed to register regulator notifier: %d\n",
  1380. ret);
  1381. }
  1382. }
  1383. ret = regulator_bulk_enable(ARRAY_SIZE(wm8995->supplies),
  1384. wm8995->supplies);
  1385. if (ret) {
  1386. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  1387. goto err_reg_get;
  1388. }
  1389. ret = snd_soc_read(codec, WM8995_SOFTWARE_RESET);
  1390. if (ret < 0) {
  1391. dev_err(codec->dev, "Failed to read device ID: %d\n", ret);
  1392. goto err_reg_enable;
  1393. }
  1394. if (ret != 0x8995) {
  1395. dev_err(codec->dev, "Invalid device ID: %#x\n", ret);
  1396. ret = -EINVAL;
  1397. goto err_reg_enable;
  1398. }
  1399. ret = snd_soc_write(codec, WM8995_SOFTWARE_RESET, 0);
  1400. if (ret < 0) {
  1401. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  1402. goto err_reg_enable;
  1403. }
  1404. wm8995_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1405. /* Latch volume updates (right only; we always do left then right). */
  1406. snd_soc_update_bits(codec, WM8995_AIF1_DAC1_RIGHT_VOLUME,
  1407. WM8995_AIF1DAC1_VU_MASK, WM8995_AIF1DAC1_VU);
  1408. snd_soc_update_bits(codec, WM8995_AIF1_DAC2_RIGHT_VOLUME,
  1409. WM8995_AIF1DAC2_VU_MASK, WM8995_AIF1DAC2_VU);
  1410. snd_soc_update_bits(codec, WM8995_AIF2_DAC_RIGHT_VOLUME,
  1411. WM8995_AIF2DAC_VU_MASK, WM8995_AIF2DAC_VU);
  1412. snd_soc_update_bits(codec, WM8995_AIF1_ADC1_RIGHT_VOLUME,
  1413. WM8995_AIF1ADC1_VU_MASK, WM8995_AIF1ADC1_VU);
  1414. snd_soc_update_bits(codec, WM8995_AIF1_ADC2_RIGHT_VOLUME,
  1415. WM8995_AIF1ADC2_VU_MASK, WM8995_AIF1ADC2_VU);
  1416. snd_soc_update_bits(codec, WM8995_AIF2_ADC_RIGHT_VOLUME,
  1417. WM8995_AIF2ADC_VU_MASK, WM8995_AIF1ADC2_VU);
  1418. snd_soc_update_bits(codec, WM8995_DAC1_RIGHT_VOLUME,
  1419. WM8995_DAC1_VU_MASK, WM8995_DAC1_VU);
  1420. snd_soc_update_bits(codec, WM8995_DAC2_RIGHT_VOLUME,
  1421. WM8995_DAC2_VU_MASK, WM8995_DAC2_VU);
  1422. snd_soc_update_bits(codec, WM8995_RIGHT_LINE_INPUT_1_VOLUME,
  1423. WM8995_IN1_VU_MASK, WM8995_IN1_VU);
  1424. wm8995_update_class_w(codec);
  1425. snd_soc_add_controls(codec, wm8995_snd_controls,
  1426. ARRAY_SIZE(wm8995_snd_controls));
  1427. snd_soc_dapm_new_controls(&codec->dapm, wm8995_dapm_widgets,
  1428. ARRAY_SIZE(wm8995_dapm_widgets));
  1429. snd_soc_dapm_add_routes(&codec->dapm, wm8995_intercon,
  1430. ARRAY_SIZE(wm8995_intercon));
  1431. return 0;
  1432. err_reg_enable:
  1433. regulator_bulk_disable(ARRAY_SIZE(wm8995->supplies), wm8995->supplies);
  1434. err_reg_get:
  1435. regulator_bulk_free(ARRAY_SIZE(wm8995->supplies), wm8995->supplies);
  1436. return ret;
  1437. }
  1438. #define WM8995_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1439. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1440. static struct snd_soc_dai_ops wm8995_aif1_dai_ops = {
  1441. .set_sysclk = wm8995_set_dai_sysclk,
  1442. .set_fmt = wm8995_set_dai_fmt,
  1443. .hw_params = wm8995_hw_params,
  1444. .digital_mute = wm8995_aif_mute,
  1445. .set_pll = wm8995_set_fll,
  1446. .set_tristate = wm8995_set_tristate,
  1447. };
  1448. static struct snd_soc_dai_ops wm8995_aif2_dai_ops = {
  1449. .set_sysclk = wm8995_set_dai_sysclk,
  1450. .set_fmt = wm8995_set_dai_fmt,
  1451. .hw_params = wm8995_hw_params,
  1452. .digital_mute = wm8995_aif_mute,
  1453. .set_pll = wm8995_set_fll,
  1454. .set_tristate = wm8995_set_tristate,
  1455. };
  1456. static struct snd_soc_dai_ops wm8995_aif3_dai_ops = {
  1457. .set_tristate = wm8995_set_tristate,
  1458. };
  1459. static struct snd_soc_dai_driver wm8995_dai[] = {
  1460. {
  1461. .name = "wm8995-aif1",
  1462. .playback = {
  1463. .stream_name = "AIF1 Playback",
  1464. .channels_min = 2,
  1465. .channels_max = 2,
  1466. .rates = SNDRV_PCM_RATE_8000_96000,
  1467. .formats = WM8995_FORMATS
  1468. },
  1469. .capture = {
  1470. .stream_name = "AIF1 Capture",
  1471. .channels_min = 2,
  1472. .channels_max = 2,
  1473. .rates = SNDRV_PCM_RATE_8000_48000,
  1474. .formats = WM8995_FORMATS
  1475. },
  1476. .ops = &wm8995_aif1_dai_ops
  1477. },
  1478. {
  1479. .name = "wm8995-aif2",
  1480. .playback = {
  1481. .stream_name = "AIF2 Playback",
  1482. .channels_min = 2,
  1483. .channels_max = 2,
  1484. .rates = SNDRV_PCM_RATE_8000_96000,
  1485. .formats = WM8995_FORMATS
  1486. },
  1487. .capture = {
  1488. .stream_name = "AIF2 Capture",
  1489. .channels_min = 2,
  1490. .channels_max = 2,
  1491. .rates = SNDRV_PCM_RATE_8000_48000,
  1492. .formats = WM8995_FORMATS
  1493. },
  1494. .ops = &wm8995_aif2_dai_ops
  1495. },
  1496. {
  1497. .name = "wm8995-aif3",
  1498. .playback = {
  1499. .stream_name = "AIF3 Playback",
  1500. .channels_min = 2,
  1501. .channels_max = 2,
  1502. .rates = SNDRV_PCM_RATE_8000_96000,
  1503. .formats = WM8995_FORMATS
  1504. },
  1505. .capture = {
  1506. .stream_name = "AIF3 Capture",
  1507. .channels_min = 2,
  1508. .channels_max = 2,
  1509. .rates = SNDRV_PCM_RATE_8000_48000,
  1510. .formats = WM8995_FORMATS
  1511. },
  1512. .ops = &wm8995_aif3_dai_ops
  1513. }
  1514. };
  1515. static struct snd_soc_codec_driver soc_codec_dev_wm8995 = {
  1516. .probe = wm8995_probe,
  1517. .remove = wm8995_remove,
  1518. .suspend = wm8995_suspend,
  1519. .resume = wm8995_resume,
  1520. .set_bias_level = wm8995_set_bias_level,
  1521. .reg_cache_size = ARRAY_SIZE(wm8995_reg_defs),
  1522. .reg_word_size = sizeof(u16),
  1523. .reg_cache_default = wm8995_reg_defs,
  1524. .volatile_register = wm8995_volatile,
  1525. .compress_type = SND_SOC_RBTREE_COMPRESSION
  1526. };
  1527. #if defined(CONFIG_SPI_MASTER)
  1528. static int __devinit wm8995_spi_probe(struct spi_device *spi)
  1529. {
  1530. struct wm8995_priv *wm8995;
  1531. int ret;
  1532. wm8995 = kzalloc(sizeof *wm8995, GFP_KERNEL);
  1533. if (!wm8995)
  1534. return -ENOMEM;
  1535. wm8995->control_type = SND_SOC_SPI;
  1536. spi_set_drvdata(spi, wm8995);
  1537. ret = snd_soc_register_codec(&spi->dev,
  1538. &soc_codec_dev_wm8995, wm8995_dai,
  1539. ARRAY_SIZE(wm8995_dai));
  1540. if (ret < 0)
  1541. kfree(wm8995);
  1542. return ret;
  1543. }
  1544. static int __devexit wm8995_spi_remove(struct spi_device *spi)
  1545. {
  1546. snd_soc_unregister_codec(&spi->dev);
  1547. kfree(spi_get_drvdata(spi));
  1548. return 0;
  1549. }
  1550. static struct spi_driver wm8995_spi_driver = {
  1551. .driver = {
  1552. .name = "wm8995",
  1553. .owner = THIS_MODULE,
  1554. },
  1555. .probe = wm8995_spi_probe,
  1556. .remove = __devexit_p(wm8995_spi_remove)
  1557. };
  1558. #endif
  1559. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1560. static __devinit int wm8995_i2c_probe(struct i2c_client *i2c,
  1561. const struct i2c_device_id *id)
  1562. {
  1563. struct wm8995_priv *wm8995;
  1564. int ret;
  1565. wm8995 = kzalloc(sizeof *wm8995, GFP_KERNEL);
  1566. if (!wm8995)
  1567. return -ENOMEM;
  1568. wm8995->control_type = SND_SOC_I2C;
  1569. i2c_set_clientdata(i2c, wm8995);
  1570. ret = snd_soc_register_codec(&i2c->dev,
  1571. &soc_codec_dev_wm8995, wm8995_dai,
  1572. ARRAY_SIZE(wm8995_dai));
  1573. if (ret < 0)
  1574. kfree(wm8995);
  1575. return ret;
  1576. }
  1577. static __devexit int wm8995_i2c_remove(struct i2c_client *client)
  1578. {
  1579. snd_soc_unregister_codec(&client->dev);
  1580. kfree(i2c_get_clientdata(client));
  1581. return 0;
  1582. }
  1583. static const struct i2c_device_id wm8995_i2c_id[] = {
  1584. {"wm8995", 0},
  1585. {}
  1586. };
  1587. MODULE_DEVICE_TABLE(i2c, wm8995_i2c_id);
  1588. static struct i2c_driver wm8995_i2c_driver = {
  1589. .driver = {
  1590. .name = "wm8995",
  1591. .owner = THIS_MODULE,
  1592. },
  1593. .probe = wm8995_i2c_probe,
  1594. .remove = __devexit_p(wm8995_i2c_remove),
  1595. .id_table = wm8995_i2c_id
  1596. };
  1597. #endif
  1598. static int __init wm8995_modinit(void)
  1599. {
  1600. int ret = 0;
  1601. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1602. ret = i2c_add_driver(&wm8995_i2c_driver);
  1603. if (ret) {
  1604. printk(KERN_ERR "Failed to register wm8995 I2C driver: %d\n",
  1605. ret);
  1606. }
  1607. #endif
  1608. #if defined(CONFIG_SPI_MASTER)
  1609. ret = spi_register_driver(&wm8995_spi_driver);
  1610. if (ret) {
  1611. printk(KERN_ERR "Failed to register wm8995 SPI driver: %d\n",
  1612. ret);
  1613. }
  1614. #endif
  1615. return ret;
  1616. }
  1617. module_init(wm8995_modinit);
  1618. static void __exit wm8995_exit(void)
  1619. {
  1620. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1621. i2c_del_driver(&wm8995_i2c_driver);
  1622. #endif
  1623. #if defined(CONFIG_SPI_MASTER)
  1624. spi_unregister_driver(&wm8995_spi_driver);
  1625. #endif
  1626. }
  1627. module_exit(wm8995_exit);
  1628. MODULE_DESCRIPTION("ASoC WM8995 driver");
  1629. MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
  1630. MODULE_LICENSE("GPL");