max98088.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  1. /*
  2. * max98088.c -- MAX98088 ALSA SoC Audio driver
  3. *
  4. * Copyright 2010 Maxim Integrated Products
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <linux/pm.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <sound/initval.h>
  23. #include <sound/tlv.h>
  24. #include <linux/slab.h>
  25. #include <asm/div64.h>
  26. #include <sound/max98088.h>
  27. #include "max98088.h"
  28. enum max98088_type {
  29. MAX98088,
  30. MAX98089,
  31. };
  32. struct max98088_cdata {
  33. unsigned int rate;
  34. unsigned int fmt;
  35. int eq_sel;
  36. };
  37. struct max98088_priv {
  38. struct regmap *regmap;
  39. enum max98088_type devtype;
  40. struct max98088_pdata *pdata;
  41. unsigned int sysclk;
  42. struct max98088_cdata dai[2];
  43. int eq_textcnt;
  44. const char **eq_texts;
  45. struct soc_enum eq_enum;
  46. u8 ina_state;
  47. u8 inb_state;
  48. unsigned int ex_mode;
  49. unsigned int digmic;
  50. unsigned int mic1pre;
  51. unsigned int mic2pre;
  52. unsigned int extmic_mode;
  53. };
  54. static const struct reg_default max98088_reg[] = {
  55. { 0xf, 0x00 }, /* 0F interrupt enable */
  56. { 0x10, 0x00 }, /* 10 master clock */
  57. { 0x11, 0x00 }, /* 11 DAI1 clock mode */
  58. { 0x12, 0x00 }, /* 12 DAI1 clock control */
  59. { 0x13, 0x00 }, /* 13 DAI1 clock control */
  60. { 0x14, 0x00 }, /* 14 DAI1 format */
  61. { 0x15, 0x00 }, /* 15 DAI1 clock */
  62. { 0x16, 0x00 }, /* 16 DAI1 config */
  63. { 0x17, 0x00 }, /* 17 DAI1 TDM */
  64. { 0x18, 0x00 }, /* 18 DAI1 filters */
  65. { 0x19, 0x00 }, /* 19 DAI2 clock mode */
  66. { 0x1a, 0x00 }, /* 1A DAI2 clock control */
  67. { 0x1b, 0x00 }, /* 1B DAI2 clock control */
  68. { 0x1c, 0x00 }, /* 1C DAI2 format */
  69. { 0x1d, 0x00 }, /* 1D DAI2 clock */
  70. { 0x1e, 0x00 }, /* 1E DAI2 config */
  71. { 0x1f, 0x00 }, /* 1F DAI2 TDM */
  72. { 0x20, 0x00 }, /* 20 DAI2 filters */
  73. { 0x21, 0x00 }, /* 21 data config */
  74. { 0x22, 0x00 }, /* 22 DAC mixer */
  75. { 0x23, 0x00 }, /* 23 left ADC mixer */
  76. { 0x24, 0x00 }, /* 24 right ADC mixer */
  77. { 0x25, 0x00 }, /* 25 left HP mixer */
  78. { 0x26, 0x00 }, /* 26 right HP mixer */
  79. { 0x27, 0x00 }, /* 27 HP control */
  80. { 0x28, 0x00 }, /* 28 left REC mixer */
  81. { 0x29, 0x00 }, /* 29 right REC mixer */
  82. { 0x2a, 0x00 }, /* 2A REC control */
  83. { 0x2b, 0x00 }, /* 2B left SPK mixer */
  84. { 0x2c, 0x00 }, /* 2C right SPK mixer */
  85. { 0x2d, 0x00 }, /* 2D SPK control */
  86. { 0x2e, 0x00 }, /* 2E sidetone */
  87. { 0x2f, 0x00 }, /* 2F DAI1 playback level */
  88. { 0x30, 0x00 }, /* 30 DAI1 playback level */
  89. { 0x31, 0x00 }, /* 31 DAI2 playback level */
  90. { 0x32, 0x00 }, /* 32 DAI2 playbakc level */
  91. { 0x33, 0x00 }, /* 33 left ADC level */
  92. { 0x34, 0x00 }, /* 34 right ADC level */
  93. { 0x35, 0x00 }, /* 35 MIC1 level */
  94. { 0x36, 0x00 }, /* 36 MIC2 level */
  95. { 0x37, 0x00 }, /* 37 INA level */
  96. { 0x38, 0x00 }, /* 38 INB level */
  97. { 0x39, 0x00 }, /* 39 left HP volume */
  98. { 0x3a, 0x00 }, /* 3A right HP volume */
  99. { 0x3b, 0x00 }, /* 3B left REC volume */
  100. { 0x3c, 0x00 }, /* 3C right REC volume */
  101. { 0x3d, 0x00 }, /* 3D left SPK volume */
  102. { 0x3e, 0x00 }, /* 3E right SPK volume */
  103. { 0x3f, 0x00 }, /* 3F MIC config */
  104. { 0x40, 0x00 }, /* 40 MIC threshold */
  105. { 0x41, 0x00 }, /* 41 excursion limiter filter */
  106. { 0x42, 0x00 }, /* 42 excursion limiter threshold */
  107. { 0x43, 0x00 }, /* 43 ALC */
  108. { 0x44, 0x00 }, /* 44 power limiter threshold */
  109. { 0x45, 0x00 }, /* 45 power limiter config */
  110. { 0x46, 0x00 }, /* 46 distortion limiter config */
  111. { 0x47, 0x00 }, /* 47 audio input */
  112. { 0x48, 0x00 }, /* 48 microphone */
  113. { 0x49, 0x00 }, /* 49 level control */
  114. { 0x4a, 0x00 }, /* 4A bypass switches */
  115. { 0x4b, 0x00 }, /* 4B jack detect */
  116. { 0x4c, 0x00 }, /* 4C input enable */
  117. { 0x4d, 0x00 }, /* 4D output enable */
  118. { 0x4e, 0xF0 }, /* 4E bias control */
  119. { 0x4f, 0x00 }, /* 4F DAC power */
  120. { 0x50, 0x0F }, /* 50 DAC power */
  121. { 0x51, 0x00 }, /* 51 system */
  122. { 0x52, 0x00 }, /* 52 DAI1 EQ1 */
  123. { 0x53, 0x00 }, /* 53 DAI1 EQ1 */
  124. { 0x54, 0x00 }, /* 54 DAI1 EQ1 */
  125. { 0x55, 0x00 }, /* 55 DAI1 EQ1 */
  126. { 0x56, 0x00 }, /* 56 DAI1 EQ1 */
  127. { 0x57, 0x00 }, /* 57 DAI1 EQ1 */
  128. { 0x58, 0x00 }, /* 58 DAI1 EQ1 */
  129. { 0x59, 0x00 }, /* 59 DAI1 EQ1 */
  130. { 0x5a, 0x00 }, /* 5A DAI1 EQ1 */
  131. { 0x5b, 0x00 }, /* 5B DAI1 EQ1 */
  132. { 0x5c, 0x00 }, /* 5C DAI1 EQ2 */
  133. { 0x5d, 0x00 }, /* 5D DAI1 EQ2 */
  134. { 0x5e, 0x00 }, /* 5E DAI1 EQ2 */
  135. { 0x5f, 0x00 }, /* 5F DAI1 EQ2 */
  136. { 0x60, 0x00 }, /* 60 DAI1 EQ2 */
  137. { 0x61, 0x00 }, /* 61 DAI1 EQ2 */
  138. { 0x62, 0x00 }, /* 62 DAI1 EQ2 */
  139. { 0x63, 0x00 }, /* 63 DAI1 EQ2 */
  140. { 0x64, 0x00 }, /* 64 DAI1 EQ2 */
  141. { 0x65, 0x00 }, /* 65 DAI1 EQ2 */
  142. { 0x66, 0x00 }, /* 66 DAI1 EQ3 */
  143. { 0x67, 0x00 }, /* 67 DAI1 EQ3 */
  144. { 0x68, 0x00 }, /* 68 DAI1 EQ3 */
  145. { 0x69, 0x00 }, /* 69 DAI1 EQ3 */
  146. { 0x6a, 0x00 }, /* 6A DAI1 EQ3 */
  147. { 0x6b, 0x00 }, /* 6B DAI1 EQ3 */
  148. { 0x6c, 0x00 }, /* 6C DAI1 EQ3 */
  149. { 0x6d, 0x00 }, /* 6D DAI1 EQ3 */
  150. { 0x6e, 0x00 }, /* 6E DAI1 EQ3 */
  151. { 0x6f, 0x00 }, /* 6F DAI1 EQ3 */
  152. { 0x70, 0x00 }, /* 70 DAI1 EQ4 */
  153. { 0x71, 0x00 }, /* 71 DAI1 EQ4 */
  154. { 0x72, 0x00 }, /* 72 DAI1 EQ4 */
  155. { 0x73, 0x00 }, /* 73 DAI1 EQ4 */
  156. { 0x74, 0x00 }, /* 74 DAI1 EQ4 */
  157. { 0x75, 0x00 }, /* 75 DAI1 EQ4 */
  158. { 0x76, 0x00 }, /* 76 DAI1 EQ4 */
  159. { 0x77, 0x00 }, /* 77 DAI1 EQ4 */
  160. { 0x78, 0x00 }, /* 78 DAI1 EQ4 */
  161. { 0x79, 0x00 }, /* 79 DAI1 EQ4 */
  162. { 0x7a, 0x00 }, /* 7A DAI1 EQ5 */
  163. { 0x7b, 0x00 }, /* 7B DAI1 EQ5 */
  164. { 0x7c, 0x00 }, /* 7C DAI1 EQ5 */
  165. { 0x7d, 0x00 }, /* 7D DAI1 EQ5 */
  166. { 0x7e, 0x00 }, /* 7E DAI1 EQ5 */
  167. { 0x7f, 0x00 }, /* 7F DAI1 EQ5 */
  168. { 0x80, 0x00 }, /* 80 DAI1 EQ5 */
  169. { 0x81, 0x00 }, /* 81 DAI1 EQ5 */
  170. { 0x82, 0x00 }, /* 82 DAI1 EQ5 */
  171. { 0x83, 0x00 }, /* 83 DAI1 EQ5 */
  172. { 0x84, 0x00 }, /* 84 DAI2 EQ1 */
  173. { 0x85, 0x00 }, /* 85 DAI2 EQ1 */
  174. { 0x86, 0x00 }, /* 86 DAI2 EQ1 */
  175. { 0x87, 0x00 }, /* 87 DAI2 EQ1 */
  176. { 0x88, 0x00 }, /* 88 DAI2 EQ1 */
  177. { 0x89, 0x00 }, /* 89 DAI2 EQ1 */
  178. { 0x8a, 0x00 }, /* 8A DAI2 EQ1 */
  179. { 0x8b, 0x00 }, /* 8B DAI2 EQ1 */
  180. { 0x8c, 0x00 }, /* 8C DAI2 EQ1 */
  181. { 0x8d, 0x00 }, /* 8D DAI2 EQ1 */
  182. { 0x8e, 0x00 }, /* 8E DAI2 EQ2 */
  183. { 0x8f, 0x00 }, /* 8F DAI2 EQ2 */
  184. { 0x90, 0x00 }, /* 90 DAI2 EQ2 */
  185. { 0x91, 0x00 }, /* 91 DAI2 EQ2 */
  186. { 0x92, 0x00 }, /* 92 DAI2 EQ2 */
  187. { 0x93, 0x00 }, /* 93 DAI2 EQ2 */
  188. { 0x94, 0x00 }, /* 94 DAI2 EQ2 */
  189. { 0x95, 0x00 }, /* 95 DAI2 EQ2 */
  190. { 0x96, 0x00 }, /* 96 DAI2 EQ2 */
  191. { 0x97, 0x00 }, /* 97 DAI2 EQ2 */
  192. { 0x98, 0x00 }, /* 98 DAI2 EQ3 */
  193. { 0x99, 0x00 }, /* 99 DAI2 EQ3 */
  194. { 0x9a, 0x00 }, /* 9A DAI2 EQ3 */
  195. { 0x9b, 0x00 }, /* 9B DAI2 EQ3 */
  196. { 0x9c, 0x00 }, /* 9C DAI2 EQ3 */
  197. { 0x9d, 0x00 }, /* 9D DAI2 EQ3 */
  198. { 0x9e, 0x00 }, /* 9E DAI2 EQ3 */
  199. { 0x9f, 0x00 }, /* 9F DAI2 EQ3 */
  200. { 0xa0, 0x00 }, /* A0 DAI2 EQ3 */
  201. { 0xa1, 0x00 }, /* A1 DAI2 EQ3 */
  202. { 0xa2, 0x00 }, /* A2 DAI2 EQ4 */
  203. { 0xa3, 0x00 }, /* A3 DAI2 EQ4 */
  204. { 0xa4, 0x00 }, /* A4 DAI2 EQ4 */
  205. { 0xa5, 0x00 }, /* A5 DAI2 EQ4 */
  206. { 0xa6, 0x00 }, /* A6 DAI2 EQ4 */
  207. { 0xa7, 0x00 }, /* A7 DAI2 EQ4 */
  208. { 0xa8, 0x00 }, /* A8 DAI2 EQ4 */
  209. { 0xa9, 0x00 }, /* A9 DAI2 EQ4 */
  210. { 0xaa, 0x00 }, /* AA DAI2 EQ4 */
  211. { 0xab, 0x00 }, /* AB DAI2 EQ4 */
  212. { 0xac, 0x00 }, /* AC DAI2 EQ5 */
  213. { 0xad, 0x00 }, /* AD DAI2 EQ5 */
  214. { 0xae, 0x00 }, /* AE DAI2 EQ5 */
  215. { 0xaf, 0x00 }, /* AF DAI2 EQ5 */
  216. { 0xb0, 0x00 }, /* B0 DAI2 EQ5 */
  217. { 0xb1, 0x00 }, /* B1 DAI2 EQ5 */
  218. { 0xb2, 0x00 }, /* B2 DAI2 EQ5 */
  219. { 0xb3, 0x00 }, /* B3 DAI2 EQ5 */
  220. { 0xb4, 0x00 }, /* B4 DAI2 EQ5 */
  221. { 0xb5, 0x00 }, /* B5 DAI2 EQ5 */
  222. { 0xb6, 0x00 }, /* B6 DAI1 biquad */
  223. { 0xb7, 0x00 }, /* B7 DAI1 biquad */
  224. { 0xb8 ,0x00 }, /* B8 DAI1 biquad */
  225. { 0xb9, 0x00 }, /* B9 DAI1 biquad */
  226. { 0xba, 0x00 }, /* BA DAI1 biquad */
  227. { 0xbb, 0x00 }, /* BB DAI1 biquad */
  228. { 0xbc, 0x00 }, /* BC DAI1 biquad */
  229. { 0xbd, 0x00 }, /* BD DAI1 biquad */
  230. { 0xbe, 0x00 }, /* BE DAI1 biquad */
  231. { 0xbf, 0x00 }, /* BF DAI1 biquad */
  232. { 0xc0, 0x00 }, /* C0 DAI2 biquad */
  233. { 0xc1, 0x00 }, /* C1 DAI2 biquad */
  234. { 0xc2, 0x00 }, /* C2 DAI2 biquad */
  235. { 0xc3, 0x00 }, /* C3 DAI2 biquad */
  236. { 0xc4, 0x00 }, /* C4 DAI2 biquad */
  237. { 0xc5, 0x00 }, /* C5 DAI2 biquad */
  238. { 0xc6, 0x00 }, /* C6 DAI2 biquad */
  239. { 0xc7, 0x00 }, /* C7 DAI2 biquad */
  240. { 0xc8, 0x00 }, /* C8 DAI2 biquad */
  241. { 0xc9, 0x00 }, /* C9 DAI2 biquad */
  242. };
  243. static bool max98088_readable_register(struct device *dev, unsigned int reg)
  244. {
  245. switch (reg) {
  246. case M98088_REG_00_IRQ_STATUS ... 0xC9:
  247. case M98088_REG_FF_REV_ID:
  248. return true;
  249. default:
  250. return false;
  251. }
  252. }
  253. static bool max98088_writeable_register(struct device *dev, unsigned int reg)
  254. {
  255. switch (reg) {
  256. case M98088_REG_03_BATTERY_VOLTAGE ... 0xC9:
  257. return true;
  258. default:
  259. return false;
  260. }
  261. }
  262. static bool max98088_volatile_register(struct device *dev, unsigned int reg)
  263. {
  264. switch (reg) {
  265. case M98088_REG_00_IRQ_STATUS ... M98088_REG_03_BATTERY_VOLTAGE:
  266. case M98088_REG_FF_REV_ID:
  267. return true;
  268. default:
  269. return false;
  270. }
  271. }
  272. static const struct regmap_config max98088_regmap = {
  273. .reg_bits = 8,
  274. .val_bits = 8,
  275. .readable_reg = max98088_readable_register,
  276. .writeable_reg = max98088_writeable_register,
  277. .volatile_reg = max98088_volatile_register,
  278. .max_register = 0xff,
  279. .reg_defaults = max98088_reg,
  280. .num_reg_defaults = ARRAY_SIZE(max98088_reg),
  281. .cache_type = REGCACHE_RBTREE,
  282. };
  283. /*
  284. * Load equalizer DSP coefficient configurations registers
  285. */
  286. static void m98088_eq_band(struct snd_soc_codec *codec, unsigned int dai,
  287. unsigned int band, u16 *coefs)
  288. {
  289. unsigned int eq_reg;
  290. unsigned int i;
  291. if (WARN_ON(band > 4) ||
  292. WARN_ON(dai > 1))
  293. return;
  294. /* Load the base register address */
  295. eq_reg = dai ? M98088_REG_84_DAI2_EQ_BASE : M98088_REG_52_DAI1_EQ_BASE;
  296. /* Add the band address offset, note adjustment for word address */
  297. eq_reg += band * (M98088_COEFS_PER_BAND << 1);
  298. /* Step through the registers and coefs */
  299. for (i = 0; i < M98088_COEFS_PER_BAND; i++) {
  300. snd_soc_write(codec, eq_reg++, M98088_BYTE1(coefs[i]));
  301. snd_soc_write(codec, eq_reg++, M98088_BYTE0(coefs[i]));
  302. }
  303. }
  304. /*
  305. * Excursion limiter modes
  306. */
  307. static const char *max98088_exmode_texts[] = {
  308. "Off", "100Hz", "400Hz", "600Hz", "800Hz", "1000Hz", "200-400Hz",
  309. "400-600Hz", "400-800Hz",
  310. };
  311. static const unsigned int max98088_exmode_values[] = {
  312. 0x00, 0x43, 0x10, 0x20, 0x30, 0x40, 0x11, 0x22, 0x32
  313. };
  314. static SOC_VALUE_ENUM_SINGLE_DECL(max98088_exmode_enum,
  315. M98088_REG_41_SPKDHP, 0, 127,
  316. max98088_exmode_texts,
  317. max98088_exmode_values);
  318. static const char *max98088_ex_thresh[] = { /* volts PP */
  319. "0.6", "1.2", "1.8", "2.4", "3.0", "3.6", "4.2", "4.8"};
  320. static SOC_ENUM_SINGLE_DECL(max98088_ex_thresh_enum,
  321. M98088_REG_42_SPKDHP_THRESH, 0,
  322. max98088_ex_thresh);
  323. static const char *max98088_fltr_mode[] = {"Voice", "Music" };
  324. static SOC_ENUM_SINGLE_DECL(max98088_filter_mode_enum,
  325. M98088_REG_18_DAI1_FILTERS, 7,
  326. max98088_fltr_mode);
  327. static const char *max98088_extmic_text[] = { "None", "MIC1", "MIC2" };
  328. static SOC_ENUM_SINGLE_DECL(max98088_extmic_enum,
  329. M98088_REG_48_CFG_MIC, 0,
  330. max98088_extmic_text);
  331. static const struct snd_kcontrol_new max98088_extmic_mux =
  332. SOC_DAPM_ENUM("External MIC Mux", max98088_extmic_enum);
  333. static const char *max98088_dai1_fltr[] = {
  334. "Off", "fc=258/fs=16k", "fc=500/fs=16k",
  335. "fc=258/fs=8k", "fc=500/fs=8k", "fc=200"};
  336. static SOC_ENUM_SINGLE_DECL(max98088_dai1_dac_filter_enum,
  337. M98088_REG_18_DAI1_FILTERS, 0,
  338. max98088_dai1_fltr);
  339. static SOC_ENUM_SINGLE_DECL(max98088_dai1_adc_filter_enum,
  340. M98088_REG_18_DAI1_FILTERS, 4,
  341. max98088_dai1_fltr);
  342. static int max98088_mic1pre_set(struct snd_kcontrol *kcontrol,
  343. struct snd_ctl_elem_value *ucontrol)
  344. {
  345. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  346. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  347. unsigned int sel = ucontrol->value.integer.value[0];
  348. max98088->mic1pre = sel;
  349. snd_soc_update_bits(codec, M98088_REG_35_LVL_MIC1, M98088_MICPRE_MASK,
  350. (1+sel)<<M98088_MICPRE_SHIFT);
  351. return 0;
  352. }
  353. static int max98088_mic1pre_get(struct snd_kcontrol *kcontrol,
  354. struct snd_ctl_elem_value *ucontrol)
  355. {
  356. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  357. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  358. ucontrol->value.integer.value[0] = max98088->mic1pre;
  359. return 0;
  360. }
  361. static int max98088_mic2pre_set(struct snd_kcontrol *kcontrol,
  362. struct snd_ctl_elem_value *ucontrol)
  363. {
  364. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  365. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  366. unsigned int sel = ucontrol->value.integer.value[0];
  367. max98088->mic2pre = sel;
  368. snd_soc_update_bits(codec, M98088_REG_36_LVL_MIC2, M98088_MICPRE_MASK,
  369. (1+sel)<<M98088_MICPRE_SHIFT);
  370. return 0;
  371. }
  372. static int max98088_mic2pre_get(struct snd_kcontrol *kcontrol,
  373. struct snd_ctl_elem_value *ucontrol)
  374. {
  375. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  376. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  377. ucontrol->value.integer.value[0] = max98088->mic2pre;
  378. return 0;
  379. }
  380. static const DECLARE_TLV_DB_RANGE(max98088_micboost_tlv,
  381. 0, 1, TLV_DB_SCALE_ITEM(0, 2000, 0),
  382. 2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0)
  383. );
  384. static const DECLARE_TLV_DB_RANGE(max98088_hp_tlv,
  385. 0, 6, TLV_DB_SCALE_ITEM(-6700, 400, 0),
  386. 7, 14, TLV_DB_SCALE_ITEM(-4000, 300, 0),
  387. 15, 21, TLV_DB_SCALE_ITEM(-1700, 200, 0),
  388. 22, 27, TLV_DB_SCALE_ITEM(-400, 100, 0),
  389. 28, 31, TLV_DB_SCALE_ITEM(150, 50, 0)
  390. );
  391. static const DECLARE_TLV_DB_RANGE(max98088_spk_tlv,
  392. 0, 6, TLV_DB_SCALE_ITEM(-6200, 400, 0),
  393. 7, 14, TLV_DB_SCALE_ITEM(-3500, 300, 0),
  394. 15, 21, TLV_DB_SCALE_ITEM(-1200, 200, 0),
  395. 22, 27, TLV_DB_SCALE_ITEM(100, 100, 0),
  396. 28, 31, TLV_DB_SCALE_ITEM(650, 50, 0)
  397. );
  398. static const struct snd_kcontrol_new max98088_snd_controls[] = {
  399. SOC_DOUBLE_R_TLV("Headphone Volume", M98088_REG_39_LVL_HP_L,
  400. M98088_REG_3A_LVL_HP_R, 0, 31, 0, max98088_hp_tlv),
  401. SOC_DOUBLE_R_TLV("Speaker Volume", M98088_REG_3D_LVL_SPK_L,
  402. M98088_REG_3E_LVL_SPK_R, 0, 31, 0, max98088_spk_tlv),
  403. SOC_DOUBLE_R_TLV("Receiver Volume", M98088_REG_3B_LVL_REC_L,
  404. M98088_REG_3C_LVL_REC_R, 0, 31, 0, max98088_spk_tlv),
  405. SOC_DOUBLE_R("Headphone Switch", M98088_REG_39_LVL_HP_L,
  406. M98088_REG_3A_LVL_HP_R, 7, 1, 1),
  407. SOC_DOUBLE_R("Speaker Switch", M98088_REG_3D_LVL_SPK_L,
  408. M98088_REG_3E_LVL_SPK_R, 7, 1, 1),
  409. SOC_DOUBLE_R("Receiver Switch", M98088_REG_3B_LVL_REC_L,
  410. M98088_REG_3C_LVL_REC_R, 7, 1, 1),
  411. SOC_SINGLE("MIC1 Volume", M98088_REG_35_LVL_MIC1, 0, 31, 1),
  412. SOC_SINGLE("MIC2 Volume", M98088_REG_36_LVL_MIC2, 0, 31, 1),
  413. SOC_SINGLE_EXT_TLV("MIC1 Boost Volume",
  414. M98088_REG_35_LVL_MIC1, 5, 2, 0,
  415. max98088_mic1pre_get, max98088_mic1pre_set,
  416. max98088_micboost_tlv),
  417. SOC_SINGLE_EXT_TLV("MIC2 Boost Volume",
  418. M98088_REG_36_LVL_MIC2, 5, 2, 0,
  419. max98088_mic2pre_get, max98088_mic2pre_set,
  420. max98088_micboost_tlv),
  421. SOC_SINGLE("INA Volume", M98088_REG_37_LVL_INA, 0, 7, 1),
  422. SOC_SINGLE("INB Volume", M98088_REG_38_LVL_INB, 0, 7, 1),
  423. SOC_SINGLE("ADCL Volume", M98088_REG_33_LVL_ADC_L, 0, 15, 0),
  424. SOC_SINGLE("ADCR Volume", M98088_REG_34_LVL_ADC_R, 0, 15, 0),
  425. SOC_SINGLE("ADCL Boost Volume", M98088_REG_33_LVL_ADC_L, 4, 3, 0),
  426. SOC_SINGLE("ADCR Boost Volume", M98088_REG_34_LVL_ADC_R, 4, 3, 0),
  427. SOC_SINGLE("EQ1 Switch", M98088_REG_49_CFG_LEVEL, 0, 1, 0),
  428. SOC_SINGLE("EQ2 Switch", M98088_REG_49_CFG_LEVEL, 1, 1, 0),
  429. SOC_ENUM("EX Limiter Mode", max98088_exmode_enum),
  430. SOC_ENUM("EX Limiter Threshold", max98088_ex_thresh_enum),
  431. SOC_ENUM("DAI1 Filter Mode", max98088_filter_mode_enum),
  432. SOC_ENUM("DAI1 DAC Filter", max98088_dai1_dac_filter_enum),
  433. SOC_ENUM("DAI1 ADC Filter", max98088_dai1_adc_filter_enum),
  434. SOC_SINGLE("DAI2 DC Block Switch", M98088_REG_20_DAI2_FILTERS,
  435. 0, 1, 0),
  436. SOC_SINGLE("ALC Switch", M98088_REG_43_SPKALC_COMP, 7, 1, 0),
  437. SOC_SINGLE("ALC Threshold", M98088_REG_43_SPKALC_COMP, 0, 7, 0),
  438. SOC_SINGLE("ALC Multiband", M98088_REG_43_SPKALC_COMP, 3, 1, 0),
  439. SOC_SINGLE("ALC Release Time", M98088_REG_43_SPKALC_COMP, 4, 7, 0),
  440. SOC_SINGLE("PWR Limiter Threshold", M98088_REG_44_PWRLMT_CFG,
  441. 4, 15, 0),
  442. SOC_SINGLE("PWR Limiter Weight", M98088_REG_44_PWRLMT_CFG, 0, 7, 0),
  443. SOC_SINGLE("PWR Limiter Time1", M98088_REG_45_PWRLMT_TIME, 0, 15, 0),
  444. SOC_SINGLE("PWR Limiter Time2", M98088_REG_45_PWRLMT_TIME, 4, 15, 0),
  445. SOC_SINGLE("THD Limiter Threshold", M98088_REG_46_THDLMT_CFG, 4, 15, 0),
  446. SOC_SINGLE("THD Limiter Time", M98088_REG_46_THDLMT_CFG, 0, 7, 0),
  447. };
  448. /* Left speaker mixer switch */
  449. static const struct snd_kcontrol_new max98088_left_speaker_mixer_controls[] = {
  450. SOC_DAPM_SINGLE("Left DAC1 Switch", M98088_REG_2B_MIX_SPK_LEFT, 0, 1, 0),
  451. SOC_DAPM_SINGLE("Right DAC1 Switch", M98088_REG_2B_MIX_SPK_LEFT, 7, 1, 0),
  452. SOC_DAPM_SINGLE("Left DAC2 Switch", M98088_REG_2B_MIX_SPK_LEFT, 0, 1, 0),
  453. SOC_DAPM_SINGLE("Right DAC2 Switch", M98088_REG_2B_MIX_SPK_LEFT, 7, 1, 0),
  454. SOC_DAPM_SINGLE("MIC1 Switch", M98088_REG_2B_MIX_SPK_LEFT, 5, 1, 0),
  455. SOC_DAPM_SINGLE("MIC2 Switch", M98088_REG_2B_MIX_SPK_LEFT, 6, 1, 0),
  456. SOC_DAPM_SINGLE("INA1 Switch", M98088_REG_2B_MIX_SPK_LEFT, 1, 1, 0),
  457. SOC_DAPM_SINGLE("INA2 Switch", M98088_REG_2B_MIX_SPK_LEFT, 2, 1, 0),
  458. SOC_DAPM_SINGLE("INB1 Switch", M98088_REG_2B_MIX_SPK_LEFT, 3, 1, 0),
  459. SOC_DAPM_SINGLE("INB2 Switch", M98088_REG_2B_MIX_SPK_LEFT, 4, 1, 0),
  460. };
  461. /* Right speaker mixer switch */
  462. static const struct snd_kcontrol_new max98088_right_speaker_mixer_controls[] = {
  463. SOC_DAPM_SINGLE("Left DAC1 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 7, 1, 0),
  464. SOC_DAPM_SINGLE("Right DAC1 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 0, 1, 0),
  465. SOC_DAPM_SINGLE("Left DAC2 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 7, 1, 0),
  466. SOC_DAPM_SINGLE("Right DAC2 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 0, 1, 0),
  467. SOC_DAPM_SINGLE("MIC1 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 5, 1, 0),
  468. SOC_DAPM_SINGLE("MIC2 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 6, 1, 0),
  469. SOC_DAPM_SINGLE("INA1 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 1, 1, 0),
  470. SOC_DAPM_SINGLE("INA2 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 2, 1, 0),
  471. SOC_DAPM_SINGLE("INB1 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 3, 1, 0),
  472. SOC_DAPM_SINGLE("INB2 Switch", M98088_REG_2C_MIX_SPK_RIGHT, 4, 1, 0),
  473. };
  474. /* Left headphone mixer switch */
  475. static const struct snd_kcontrol_new max98088_left_hp_mixer_controls[] = {
  476. SOC_DAPM_SINGLE("Left DAC1 Switch", M98088_REG_25_MIX_HP_LEFT, 0, 1, 0),
  477. SOC_DAPM_SINGLE("Right DAC1 Switch", M98088_REG_25_MIX_HP_LEFT, 7, 1, 0),
  478. SOC_DAPM_SINGLE("Left DAC2 Switch", M98088_REG_25_MIX_HP_LEFT, 0, 1, 0),
  479. SOC_DAPM_SINGLE("Right DAC2 Switch", M98088_REG_25_MIX_HP_LEFT, 7, 1, 0),
  480. SOC_DAPM_SINGLE("MIC1 Switch", M98088_REG_25_MIX_HP_LEFT, 5, 1, 0),
  481. SOC_DAPM_SINGLE("MIC2 Switch", M98088_REG_25_MIX_HP_LEFT, 6, 1, 0),
  482. SOC_DAPM_SINGLE("INA1 Switch", M98088_REG_25_MIX_HP_LEFT, 1, 1, 0),
  483. SOC_DAPM_SINGLE("INA2 Switch", M98088_REG_25_MIX_HP_LEFT, 2, 1, 0),
  484. SOC_DAPM_SINGLE("INB1 Switch", M98088_REG_25_MIX_HP_LEFT, 3, 1, 0),
  485. SOC_DAPM_SINGLE("INB2 Switch", M98088_REG_25_MIX_HP_LEFT, 4, 1, 0),
  486. };
  487. /* Right headphone mixer switch */
  488. static const struct snd_kcontrol_new max98088_right_hp_mixer_controls[] = {
  489. SOC_DAPM_SINGLE("Left DAC1 Switch", M98088_REG_26_MIX_HP_RIGHT, 7, 1, 0),
  490. SOC_DAPM_SINGLE("Right DAC1 Switch", M98088_REG_26_MIX_HP_RIGHT, 0, 1, 0),
  491. SOC_DAPM_SINGLE("Left DAC2 Switch", M98088_REG_26_MIX_HP_RIGHT, 7, 1, 0),
  492. SOC_DAPM_SINGLE("Right DAC2 Switch", M98088_REG_26_MIX_HP_RIGHT, 0, 1, 0),
  493. SOC_DAPM_SINGLE("MIC1 Switch", M98088_REG_26_MIX_HP_RIGHT, 5, 1, 0),
  494. SOC_DAPM_SINGLE("MIC2 Switch", M98088_REG_26_MIX_HP_RIGHT, 6, 1, 0),
  495. SOC_DAPM_SINGLE("INA1 Switch", M98088_REG_26_MIX_HP_RIGHT, 1, 1, 0),
  496. SOC_DAPM_SINGLE("INA2 Switch", M98088_REG_26_MIX_HP_RIGHT, 2, 1, 0),
  497. SOC_DAPM_SINGLE("INB1 Switch", M98088_REG_26_MIX_HP_RIGHT, 3, 1, 0),
  498. SOC_DAPM_SINGLE("INB2 Switch", M98088_REG_26_MIX_HP_RIGHT, 4, 1, 0),
  499. };
  500. /* Left earpiece/receiver mixer switch */
  501. static const struct snd_kcontrol_new max98088_left_rec_mixer_controls[] = {
  502. SOC_DAPM_SINGLE("Left DAC1 Switch", M98088_REG_28_MIX_REC_LEFT, 0, 1, 0),
  503. SOC_DAPM_SINGLE("Right DAC1 Switch", M98088_REG_28_MIX_REC_LEFT, 7, 1, 0),
  504. SOC_DAPM_SINGLE("Left DAC2 Switch", M98088_REG_28_MIX_REC_LEFT, 0, 1, 0),
  505. SOC_DAPM_SINGLE("Right DAC2 Switch", M98088_REG_28_MIX_REC_LEFT, 7, 1, 0),
  506. SOC_DAPM_SINGLE("MIC1 Switch", M98088_REG_28_MIX_REC_LEFT, 5, 1, 0),
  507. SOC_DAPM_SINGLE("MIC2 Switch", M98088_REG_28_MIX_REC_LEFT, 6, 1, 0),
  508. SOC_DAPM_SINGLE("INA1 Switch", M98088_REG_28_MIX_REC_LEFT, 1, 1, 0),
  509. SOC_DAPM_SINGLE("INA2 Switch", M98088_REG_28_MIX_REC_LEFT, 2, 1, 0),
  510. SOC_DAPM_SINGLE("INB1 Switch", M98088_REG_28_MIX_REC_LEFT, 3, 1, 0),
  511. SOC_DAPM_SINGLE("INB2 Switch", M98088_REG_28_MIX_REC_LEFT, 4, 1, 0),
  512. };
  513. /* Right earpiece/receiver mixer switch */
  514. static const struct snd_kcontrol_new max98088_right_rec_mixer_controls[] = {
  515. SOC_DAPM_SINGLE("Left DAC1 Switch", M98088_REG_29_MIX_REC_RIGHT, 7, 1, 0),
  516. SOC_DAPM_SINGLE("Right DAC1 Switch", M98088_REG_29_MIX_REC_RIGHT, 0, 1, 0),
  517. SOC_DAPM_SINGLE("Left DAC2 Switch", M98088_REG_29_MIX_REC_RIGHT, 7, 1, 0),
  518. SOC_DAPM_SINGLE("Right DAC2 Switch", M98088_REG_29_MIX_REC_RIGHT, 0, 1, 0),
  519. SOC_DAPM_SINGLE("MIC1 Switch", M98088_REG_29_MIX_REC_RIGHT, 5, 1, 0),
  520. SOC_DAPM_SINGLE("MIC2 Switch", M98088_REG_29_MIX_REC_RIGHT, 6, 1, 0),
  521. SOC_DAPM_SINGLE("INA1 Switch", M98088_REG_29_MIX_REC_RIGHT, 1, 1, 0),
  522. SOC_DAPM_SINGLE("INA2 Switch", M98088_REG_29_MIX_REC_RIGHT, 2, 1, 0),
  523. SOC_DAPM_SINGLE("INB1 Switch", M98088_REG_29_MIX_REC_RIGHT, 3, 1, 0),
  524. SOC_DAPM_SINGLE("INB2 Switch", M98088_REG_29_MIX_REC_RIGHT, 4, 1, 0),
  525. };
  526. /* Left ADC mixer switch */
  527. static const struct snd_kcontrol_new max98088_left_ADC_mixer_controls[] = {
  528. SOC_DAPM_SINGLE("MIC1 Switch", M98088_REG_23_MIX_ADC_LEFT, 7, 1, 0),
  529. SOC_DAPM_SINGLE("MIC2 Switch", M98088_REG_23_MIX_ADC_LEFT, 6, 1, 0),
  530. SOC_DAPM_SINGLE("INA1 Switch", M98088_REG_23_MIX_ADC_LEFT, 3, 1, 0),
  531. SOC_DAPM_SINGLE("INA2 Switch", M98088_REG_23_MIX_ADC_LEFT, 2, 1, 0),
  532. SOC_DAPM_SINGLE("INB1 Switch", M98088_REG_23_MIX_ADC_LEFT, 1, 1, 0),
  533. SOC_DAPM_SINGLE("INB2 Switch", M98088_REG_23_MIX_ADC_LEFT, 0, 1, 0),
  534. };
  535. /* Right ADC mixer switch */
  536. static const struct snd_kcontrol_new max98088_right_ADC_mixer_controls[] = {
  537. SOC_DAPM_SINGLE("MIC1 Switch", M98088_REG_24_MIX_ADC_RIGHT, 7, 1, 0),
  538. SOC_DAPM_SINGLE("MIC2 Switch", M98088_REG_24_MIX_ADC_RIGHT, 6, 1, 0),
  539. SOC_DAPM_SINGLE("INA1 Switch", M98088_REG_24_MIX_ADC_RIGHT, 3, 1, 0),
  540. SOC_DAPM_SINGLE("INA2 Switch", M98088_REG_24_MIX_ADC_RIGHT, 2, 1, 0),
  541. SOC_DAPM_SINGLE("INB1 Switch", M98088_REG_24_MIX_ADC_RIGHT, 1, 1, 0),
  542. SOC_DAPM_SINGLE("INB2 Switch", M98088_REG_24_MIX_ADC_RIGHT, 0, 1, 0),
  543. };
  544. static int max98088_mic_event(struct snd_soc_dapm_widget *w,
  545. struct snd_kcontrol *kcontrol, int event)
  546. {
  547. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  548. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  549. switch (event) {
  550. case SND_SOC_DAPM_POST_PMU:
  551. if (w->reg == M98088_REG_35_LVL_MIC1) {
  552. snd_soc_update_bits(codec, w->reg, M98088_MICPRE_MASK,
  553. (1+max98088->mic1pre)<<M98088_MICPRE_SHIFT);
  554. } else {
  555. snd_soc_update_bits(codec, w->reg, M98088_MICPRE_MASK,
  556. (1+max98088->mic2pre)<<M98088_MICPRE_SHIFT);
  557. }
  558. break;
  559. case SND_SOC_DAPM_POST_PMD:
  560. snd_soc_update_bits(codec, w->reg, M98088_MICPRE_MASK, 0);
  561. break;
  562. default:
  563. return -EINVAL;
  564. }
  565. return 0;
  566. }
  567. /*
  568. * The line inputs are 2-channel stereo inputs with the left
  569. * and right channels sharing a common PGA power control signal.
  570. */
  571. static int max98088_line_pga(struct snd_soc_dapm_widget *w,
  572. int event, int line, u8 channel)
  573. {
  574. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  575. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  576. u8 *state;
  577. if (WARN_ON(!(channel == 1 || channel == 2)))
  578. return -EINVAL;
  579. switch (line) {
  580. case LINE_INA:
  581. state = &max98088->ina_state;
  582. break;
  583. case LINE_INB:
  584. state = &max98088->inb_state;
  585. break;
  586. default:
  587. return -EINVAL;
  588. }
  589. switch (event) {
  590. case SND_SOC_DAPM_POST_PMU:
  591. *state |= channel;
  592. snd_soc_update_bits(codec, w->reg,
  593. (1 << w->shift), (1 << w->shift));
  594. break;
  595. case SND_SOC_DAPM_POST_PMD:
  596. *state &= ~channel;
  597. if (*state == 0) {
  598. snd_soc_update_bits(codec, w->reg,
  599. (1 << w->shift), 0);
  600. }
  601. break;
  602. default:
  603. return -EINVAL;
  604. }
  605. return 0;
  606. }
  607. static int max98088_pga_ina1_event(struct snd_soc_dapm_widget *w,
  608. struct snd_kcontrol *k, int event)
  609. {
  610. return max98088_line_pga(w, event, LINE_INA, 1);
  611. }
  612. static int max98088_pga_ina2_event(struct snd_soc_dapm_widget *w,
  613. struct snd_kcontrol *k, int event)
  614. {
  615. return max98088_line_pga(w, event, LINE_INA, 2);
  616. }
  617. static int max98088_pga_inb1_event(struct snd_soc_dapm_widget *w,
  618. struct snd_kcontrol *k, int event)
  619. {
  620. return max98088_line_pga(w, event, LINE_INB, 1);
  621. }
  622. static int max98088_pga_inb2_event(struct snd_soc_dapm_widget *w,
  623. struct snd_kcontrol *k, int event)
  624. {
  625. return max98088_line_pga(w, event, LINE_INB, 2);
  626. }
  627. static const struct snd_soc_dapm_widget max98088_dapm_widgets[] = {
  628. SND_SOC_DAPM_ADC("ADCL", "HiFi Capture", M98088_REG_4C_PWR_EN_IN, 1, 0),
  629. SND_SOC_DAPM_ADC("ADCR", "HiFi Capture", M98088_REG_4C_PWR_EN_IN, 0, 0),
  630. SND_SOC_DAPM_DAC("DACL1", "HiFi Playback",
  631. M98088_REG_4D_PWR_EN_OUT, 1, 0),
  632. SND_SOC_DAPM_DAC("DACR1", "HiFi Playback",
  633. M98088_REG_4D_PWR_EN_OUT, 0, 0),
  634. SND_SOC_DAPM_DAC("DACL2", "Aux Playback",
  635. M98088_REG_4D_PWR_EN_OUT, 1, 0),
  636. SND_SOC_DAPM_DAC("DACR2", "Aux Playback",
  637. M98088_REG_4D_PWR_EN_OUT, 0, 0),
  638. SND_SOC_DAPM_PGA("HP Left Out", M98088_REG_4D_PWR_EN_OUT,
  639. 7, 0, NULL, 0),
  640. SND_SOC_DAPM_PGA("HP Right Out", M98088_REG_4D_PWR_EN_OUT,
  641. 6, 0, NULL, 0),
  642. SND_SOC_DAPM_PGA("SPK Left Out", M98088_REG_4D_PWR_EN_OUT,
  643. 5, 0, NULL, 0),
  644. SND_SOC_DAPM_PGA("SPK Right Out", M98088_REG_4D_PWR_EN_OUT,
  645. 4, 0, NULL, 0),
  646. SND_SOC_DAPM_PGA("REC Left Out", M98088_REG_4D_PWR_EN_OUT,
  647. 3, 0, NULL, 0),
  648. SND_SOC_DAPM_PGA("REC Right Out", M98088_REG_4D_PWR_EN_OUT,
  649. 2, 0, NULL, 0),
  650. SND_SOC_DAPM_MUX("External MIC", SND_SOC_NOPM, 0, 0,
  651. &max98088_extmic_mux),
  652. SND_SOC_DAPM_MIXER("Left HP Mixer", SND_SOC_NOPM, 0, 0,
  653. &max98088_left_hp_mixer_controls[0],
  654. ARRAY_SIZE(max98088_left_hp_mixer_controls)),
  655. SND_SOC_DAPM_MIXER("Right HP Mixer", SND_SOC_NOPM, 0, 0,
  656. &max98088_right_hp_mixer_controls[0],
  657. ARRAY_SIZE(max98088_right_hp_mixer_controls)),
  658. SND_SOC_DAPM_MIXER("Left SPK Mixer", SND_SOC_NOPM, 0, 0,
  659. &max98088_left_speaker_mixer_controls[0],
  660. ARRAY_SIZE(max98088_left_speaker_mixer_controls)),
  661. SND_SOC_DAPM_MIXER("Right SPK Mixer", SND_SOC_NOPM, 0, 0,
  662. &max98088_right_speaker_mixer_controls[0],
  663. ARRAY_SIZE(max98088_right_speaker_mixer_controls)),
  664. SND_SOC_DAPM_MIXER("Left REC Mixer", SND_SOC_NOPM, 0, 0,
  665. &max98088_left_rec_mixer_controls[0],
  666. ARRAY_SIZE(max98088_left_rec_mixer_controls)),
  667. SND_SOC_DAPM_MIXER("Right REC Mixer", SND_SOC_NOPM, 0, 0,
  668. &max98088_right_rec_mixer_controls[0],
  669. ARRAY_SIZE(max98088_right_rec_mixer_controls)),
  670. SND_SOC_DAPM_MIXER("Left ADC Mixer", SND_SOC_NOPM, 0, 0,
  671. &max98088_left_ADC_mixer_controls[0],
  672. ARRAY_SIZE(max98088_left_ADC_mixer_controls)),
  673. SND_SOC_DAPM_MIXER("Right ADC Mixer", SND_SOC_NOPM, 0, 0,
  674. &max98088_right_ADC_mixer_controls[0],
  675. ARRAY_SIZE(max98088_right_ADC_mixer_controls)),
  676. SND_SOC_DAPM_PGA_E("MIC1 Input", M98088_REG_35_LVL_MIC1,
  677. 5, 0, NULL, 0, max98088_mic_event,
  678. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  679. SND_SOC_DAPM_PGA_E("MIC2 Input", M98088_REG_36_LVL_MIC2,
  680. 5, 0, NULL, 0, max98088_mic_event,
  681. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  682. SND_SOC_DAPM_PGA_E("INA1 Input", M98088_REG_4C_PWR_EN_IN,
  683. 7, 0, NULL, 0, max98088_pga_ina1_event,
  684. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  685. SND_SOC_DAPM_PGA_E("INA2 Input", M98088_REG_4C_PWR_EN_IN,
  686. 7, 0, NULL, 0, max98088_pga_ina2_event,
  687. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  688. SND_SOC_DAPM_PGA_E("INB1 Input", M98088_REG_4C_PWR_EN_IN,
  689. 6, 0, NULL, 0, max98088_pga_inb1_event,
  690. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  691. SND_SOC_DAPM_PGA_E("INB2 Input", M98088_REG_4C_PWR_EN_IN,
  692. 6, 0, NULL, 0, max98088_pga_inb2_event,
  693. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  694. SND_SOC_DAPM_MICBIAS("MICBIAS", M98088_REG_4C_PWR_EN_IN, 3, 0),
  695. SND_SOC_DAPM_OUTPUT("HPL"),
  696. SND_SOC_DAPM_OUTPUT("HPR"),
  697. SND_SOC_DAPM_OUTPUT("SPKL"),
  698. SND_SOC_DAPM_OUTPUT("SPKR"),
  699. SND_SOC_DAPM_OUTPUT("RECL"),
  700. SND_SOC_DAPM_OUTPUT("RECR"),
  701. SND_SOC_DAPM_INPUT("MIC1"),
  702. SND_SOC_DAPM_INPUT("MIC2"),
  703. SND_SOC_DAPM_INPUT("INA1"),
  704. SND_SOC_DAPM_INPUT("INA2"),
  705. SND_SOC_DAPM_INPUT("INB1"),
  706. SND_SOC_DAPM_INPUT("INB2"),
  707. };
  708. static const struct snd_soc_dapm_route max98088_audio_map[] = {
  709. /* Left headphone output mixer */
  710. {"Left HP Mixer", "Left DAC1 Switch", "DACL1"},
  711. {"Left HP Mixer", "Left DAC2 Switch", "DACL2"},
  712. {"Left HP Mixer", "Right DAC1 Switch", "DACR1"},
  713. {"Left HP Mixer", "Right DAC2 Switch", "DACR2"},
  714. {"Left HP Mixer", "MIC1 Switch", "MIC1 Input"},
  715. {"Left HP Mixer", "MIC2 Switch", "MIC2 Input"},
  716. {"Left HP Mixer", "INA1 Switch", "INA1 Input"},
  717. {"Left HP Mixer", "INA2 Switch", "INA2 Input"},
  718. {"Left HP Mixer", "INB1 Switch", "INB1 Input"},
  719. {"Left HP Mixer", "INB2 Switch", "INB2 Input"},
  720. /* Right headphone output mixer */
  721. {"Right HP Mixer", "Left DAC1 Switch", "DACL1"},
  722. {"Right HP Mixer", "Left DAC2 Switch", "DACL2" },
  723. {"Right HP Mixer", "Right DAC1 Switch", "DACR1"},
  724. {"Right HP Mixer", "Right DAC2 Switch", "DACR2"},
  725. {"Right HP Mixer", "MIC1 Switch", "MIC1 Input"},
  726. {"Right HP Mixer", "MIC2 Switch", "MIC2 Input"},
  727. {"Right HP Mixer", "INA1 Switch", "INA1 Input"},
  728. {"Right HP Mixer", "INA2 Switch", "INA2 Input"},
  729. {"Right HP Mixer", "INB1 Switch", "INB1 Input"},
  730. {"Right HP Mixer", "INB2 Switch", "INB2 Input"},
  731. /* Left speaker output mixer */
  732. {"Left SPK Mixer", "Left DAC1 Switch", "DACL1"},
  733. {"Left SPK Mixer", "Left DAC2 Switch", "DACL2"},
  734. {"Left SPK Mixer", "Right DAC1 Switch", "DACR1"},
  735. {"Left SPK Mixer", "Right DAC2 Switch", "DACR2"},
  736. {"Left SPK Mixer", "MIC1 Switch", "MIC1 Input"},
  737. {"Left SPK Mixer", "MIC2 Switch", "MIC2 Input"},
  738. {"Left SPK Mixer", "INA1 Switch", "INA1 Input"},
  739. {"Left SPK Mixer", "INA2 Switch", "INA2 Input"},
  740. {"Left SPK Mixer", "INB1 Switch", "INB1 Input"},
  741. {"Left SPK Mixer", "INB2 Switch", "INB2 Input"},
  742. /* Right speaker output mixer */
  743. {"Right SPK Mixer", "Left DAC1 Switch", "DACL1"},
  744. {"Right SPK Mixer", "Left DAC2 Switch", "DACL2"},
  745. {"Right SPK Mixer", "Right DAC1 Switch", "DACR1"},
  746. {"Right SPK Mixer", "Right DAC2 Switch", "DACR2"},
  747. {"Right SPK Mixer", "MIC1 Switch", "MIC1 Input"},
  748. {"Right SPK Mixer", "MIC2 Switch", "MIC2 Input"},
  749. {"Right SPK Mixer", "INA1 Switch", "INA1 Input"},
  750. {"Right SPK Mixer", "INA2 Switch", "INA2 Input"},
  751. {"Right SPK Mixer", "INB1 Switch", "INB1 Input"},
  752. {"Right SPK Mixer", "INB2 Switch", "INB2 Input"},
  753. /* Earpiece/Receiver output mixer */
  754. {"Left REC Mixer", "Left DAC1 Switch", "DACL1"},
  755. {"Left REC Mixer", "Left DAC2 Switch", "DACL2"},
  756. {"Left REC Mixer", "Right DAC1 Switch", "DACR1"},
  757. {"Left REC Mixer", "Right DAC2 Switch", "DACR2"},
  758. {"Left REC Mixer", "MIC1 Switch", "MIC1 Input"},
  759. {"Left REC Mixer", "MIC2 Switch", "MIC2 Input"},
  760. {"Left REC Mixer", "INA1 Switch", "INA1 Input"},
  761. {"Left REC Mixer", "INA2 Switch", "INA2 Input"},
  762. {"Left REC Mixer", "INB1 Switch", "INB1 Input"},
  763. {"Left REC Mixer", "INB2 Switch", "INB2 Input"},
  764. /* Earpiece/Receiver output mixer */
  765. {"Right REC Mixer", "Left DAC1 Switch", "DACL1"},
  766. {"Right REC Mixer", "Left DAC2 Switch", "DACL2"},
  767. {"Right REC Mixer", "Right DAC1 Switch", "DACR1"},
  768. {"Right REC Mixer", "Right DAC2 Switch", "DACR2"},
  769. {"Right REC Mixer", "MIC1 Switch", "MIC1 Input"},
  770. {"Right REC Mixer", "MIC2 Switch", "MIC2 Input"},
  771. {"Right REC Mixer", "INA1 Switch", "INA1 Input"},
  772. {"Right REC Mixer", "INA2 Switch", "INA2 Input"},
  773. {"Right REC Mixer", "INB1 Switch", "INB1 Input"},
  774. {"Right REC Mixer", "INB2 Switch", "INB2 Input"},
  775. {"HP Left Out", NULL, "Left HP Mixer"},
  776. {"HP Right Out", NULL, "Right HP Mixer"},
  777. {"SPK Left Out", NULL, "Left SPK Mixer"},
  778. {"SPK Right Out", NULL, "Right SPK Mixer"},
  779. {"REC Left Out", NULL, "Left REC Mixer"},
  780. {"REC Right Out", NULL, "Right REC Mixer"},
  781. {"HPL", NULL, "HP Left Out"},
  782. {"HPR", NULL, "HP Right Out"},
  783. {"SPKL", NULL, "SPK Left Out"},
  784. {"SPKR", NULL, "SPK Right Out"},
  785. {"RECL", NULL, "REC Left Out"},
  786. {"RECR", NULL, "REC Right Out"},
  787. /* Left ADC input mixer */
  788. {"Left ADC Mixer", "MIC1 Switch", "MIC1 Input"},
  789. {"Left ADC Mixer", "MIC2 Switch", "MIC2 Input"},
  790. {"Left ADC Mixer", "INA1 Switch", "INA1 Input"},
  791. {"Left ADC Mixer", "INA2 Switch", "INA2 Input"},
  792. {"Left ADC Mixer", "INB1 Switch", "INB1 Input"},
  793. {"Left ADC Mixer", "INB2 Switch", "INB2 Input"},
  794. /* Right ADC input mixer */
  795. {"Right ADC Mixer", "MIC1 Switch", "MIC1 Input"},
  796. {"Right ADC Mixer", "MIC2 Switch", "MIC2 Input"},
  797. {"Right ADC Mixer", "INA1 Switch", "INA1 Input"},
  798. {"Right ADC Mixer", "INA2 Switch", "INA2 Input"},
  799. {"Right ADC Mixer", "INB1 Switch", "INB1 Input"},
  800. {"Right ADC Mixer", "INB2 Switch", "INB2 Input"},
  801. /* Inputs */
  802. {"ADCL", NULL, "Left ADC Mixer"},
  803. {"ADCR", NULL, "Right ADC Mixer"},
  804. {"INA1 Input", NULL, "INA1"},
  805. {"INA2 Input", NULL, "INA2"},
  806. {"INB1 Input", NULL, "INB1"},
  807. {"INB2 Input", NULL, "INB2"},
  808. {"MIC1 Input", NULL, "MIC1"},
  809. {"MIC2 Input", NULL, "MIC2"},
  810. };
  811. /* codec mclk clock divider coefficients */
  812. static const struct {
  813. u32 rate;
  814. u8 sr;
  815. } rate_table[] = {
  816. {8000, 0x10},
  817. {11025, 0x20},
  818. {16000, 0x30},
  819. {22050, 0x40},
  820. {24000, 0x50},
  821. {32000, 0x60},
  822. {44100, 0x70},
  823. {48000, 0x80},
  824. {88200, 0x90},
  825. {96000, 0xA0},
  826. };
  827. static inline int rate_value(int rate, u8 *value)
  828. {
  829. int i;
  830. for (i = 0; i < ARRAY_SIZE(rate_table); i++) {
  831. if (rate_table[i].rate >= rate) {
  832. *value = rate_table[i].sr;
  833. return 0;
  834. }
  835. }
  836. *value = rate_table[0].sr;
  837. return -EINVAL;
  838. }
  839. static int max98088_dai1_hw_params(struct snd_pcm_substream *substream,
  840. struct snd_pcm_hw_params *params,
  841. struct snd_soc_dai *dai)
  842. {
  843. struct snd_soc_codec *codec = dai->codec;
  844. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  845. struct max98088_cdata *cdata;
  846. unsigned long long ni;
  847. unsigned int rate;
  848. u8 regval;
  849. cdata = &max98088->dai[0];
  850. rate = params_rate(params);
  851. switch (params_width(params)) {
  852. case 16:
  853. snd_soc_update_bits(codec, M98088_REG_14_DAI1_FORMAT,
  854. M98088_DAI_WS, 0);
  855. break;
  856. case 24:
  857. snd_soc_update_bits(codec, M98088_REG_14_DAI1_FORMAT,
  858. M98088_DAI_WS, M98088_DAI_WS);
  859. break;
  860. default:
  861. return -EINVAL;
  862. }
  863. snd_soc_update_bits(codec, M98088_REG_51_PWR_SYS, M98088_SHDNRUN, 0);
  864. if (rate_value(rate, &regval))
  865. return -EINVAL;
  866. snd_soc_update_bits(codec, M98088_REG_11_DAI1_CLKMODE,
  867. M98088_CLKMODE_MASK, regval);
  868. cdata->rate = rate;
  869. /* Configure NI when operating as master */
  870. if (snd_soc_read(codec, M98088_REG_14_DAI1_FORMAT)
  871. & M98088_DAI_MAS) {
  872. if (max98088->sysclk == 0) {
  873. dev_err(codec->dev, "Invalid system clock frequency\n");
  874. return -EINVAL;
  875. }
  876. ni = 65536ULL * (rate < 50000 ? 96ULL : 48ULL)
  877. * (unsigned long long int)rate;
  878. do_div(ni, (unsigned long long int)max98088->sysclk);
  879. snd_soc_write(codec, M98088_REG_12_DAI1_CLKCFG_HI,
  880. (ni >> 8) & 0x7F);
  881. snd_soc_write(codec, M98088_REG_13_DAI1_CLKCFG_LO,
  882. ni & 0xFF);
  883. }
  884. /* Update sample rate mode */
  885. if (rate < 50000)
  886. snd_soc_update_bits(codec, M98088_REG_18_DAI1_FILTERS,
  887. M98088_DAI_DHF, 0);
  888. else
  889. snd_soc_update_bits(codec, M98088_REG_18_DAI1_FILTERS,
  890. M98088_DAI_DHF, M98088_DAI_DHF);
  891. snd_soc_update_bits(codec, M98088_REG_51_PWR_SYS, M98088_SHDNRUN,
  892. M98088_SHDNRUN);
  893. return 0;
  894. }
  895. static int max98088_dai2_hw_params(struct snd_pcm_substream *substream,
  896. struct snd_pcm_hw_params *params,
  897. struct snd_soc_dai *dai)
  898. {
  899. struct snd_soc_codec *codec = dai->codec;
  900. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  901. struct max98088_cdata *cdata;
  902. unsigned long long ni;
  903. unsigned int rate;
  904. u8 regval;
  905. cdata = &max98088->dai[1];
  906. rate = params_rate(params);
  907. switch (params_width(params)) {
  908. case 16:
  909. snd_soc_update_bits(codec, M98088_REG_1C_DAI2_FORMAT,
  910. M98088_DAI_WS, 0);
  911. break;
  912. case 24:
  913. snd_soc_update_bits(codec, M98088_REG_1C_DAI2_FORMAT,
  914. M98088_DAI_WS, M98088_DAI_WS);
  915. break;
  916. default:
  917. return -EINVAL;
  918. }
  919. snd_soc_update_bits(codec, M98088_REG_51_PWR_SYS, M98088_SHDNRUN, 0);
  920. if (rate_value(rate, &regval))
  921. return -EINVAL;
  922. snd_soc_update_bits(codec, M98088_REG_19_DAI2_CLKMODE,
  923. M98088_CLKMODE_MASK, regval);
  924. cdata->rate = rate;
  925. /* Configure NI when operating as master */
  926. if (snd_soc_read(codec, M98088_REG_1C_DAI2_FORMAT)
  927. & M98088_DAI_MAS) {
  928. if (max98088->sysclk == 0) {
  929. dev_err(codec->dev, "Invalid system clock frequency\n");
  930. return -EINVAL;
  931. }
  932. ni = 65536ULL * (rate < 50000 ? 96ULL : 48ULL)
  933. * (unsigned long long int)rate;
  934. do_div(ni, (unsigned long long int)max98088->sysclk);
  935. snd_soc_write(codec, M98088_REG_1A_DAI2_CLKCFG_HI,
  936. (ni >> 8) & 0x7F);
  937. snd_soc_write(codec, M98088_REG_1B_DAI2_CLKCFG_LO,
  938. ni & 0xFF);
  939. }
  940. /* Update sample rate mode */
  941. if (rate < 50000)
  942. snd_soc_update_bits(codec, M98088_REG_20_DAI2_FILTERS,
  943. M98088_DAI_DHF, 0);
  944. else
  945. snd_soc_update_bits(codec, M98088_REG_20_DAI2_FILTERS,
  946. M98088_DAI_DHF, M98088_DAI_DHF);
  947. snd_soc_update_bits(codec, M98088_REG_51_PWR_SYS, M98088_SHDNRUN,
  948. M98088_SHDNRUN);
  949. return 0;
  950. }
  951. static int max98088_dai_set_sysclk(struct snd_soc_dai *dai,
  952. int clk_id, unsigned int freq, int dir)
  953. {
  954. struct snd_soc_codec *codec = dai->codec;
  955. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  956. /* Requested clock frequency is already setup */
  957. if (freq == max98088->sysclk)
  958. return 0;
  959. /* Setup clocks for slave mode, and using the PLL
  960. * PSCLK = 0x01 (when master clk is 10MHz to 20MHz)
  961. * 0x02 (when master clk is 20MHz to 30MHz)..
  962. */
  963. if ((freq >= 10000000) && (freq < 20000000)) {
  964. snd_soc_write(codec, M98088_REG_10_SYS_CLK, 0x10);
  965. } else if ((freq >= 20000000) && (freq < 30000000)) {
  966. snd_soc_write(codec, M98088_REG_10_SYS_CLK, 0x20);
  967. } else {
  968. dev_err(codec->dev, "Invalid master clock frequency\n");
  969. return -EINVAL;
  970. }
  971. if (snd_soc_read(codec, M98088_REG_51_PWR_SYS) & M98088_SHDNRUN) {
  972. snd_soc_update_bits(codec, M98088_REG_51_PWR_SYS,
  973. M98088_SHDNRUN, 0);
  974. snd_soc_update_bits(codec, M98088_REG_51_PWR_SYS,
  975. M98088_SHDNRUN, M98088_SHDNRUN);
  976. }
  977. dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
  978. max98088->sysclk = freq;
  979. return 0;
  980. }
  981. static int max98088_dai1_set_fmt(struct snd_soc_dai *codec_dai,
  982. unsigned int fmt)
  983. {
  984. struct snd_soc_codec *codec = codec_dai->codec;
  985. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  986. struct max98088_cdata *cdata;
  987. u8 reg15val;
  988. u8 reg14val = 0;
  989. cdata = &max98088->dai[0];
  990. if (fmt != cdata->fmt) {
  991. cdata->fmt = fmt;
  992. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  993. case SND_SOC_DAIFMT_CBS_CFS:
  994. /* Slave mode PLL */
  995. snd_soc_write(codec, M98088_REG_12_DAI1_CLKCFG_HI,
  996. 0x80);
  997. snd_soc_write(codec, M98088_REG_13_DAI1_CLKCFG_LO,
  998. 0x00);
  999. break;
  1000. case SND_SOC_DAIFMT_CBM_CFM:
  1001. /* Set to master mode */
  1002. reg14val |= M98088_DAI_MAS;
  1003. break;
  1004. case SND_SOC_DAIFMT_CBS_CFM:
  1005. case SND_SOC_DAIFMT_CBM_CFS:
  1006. default:
  1007. dev_err(codec->dev, "Clock mode unsupported");
  1008. return -EINVAL;
  1009. }
  1010. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1011. case SND_SOC_DAIFMT_I2S:
  1012. reg14val |= M98088_DAI_DLY;
  1013. break;
  1014. case SND_SOC_DAIFMT_LEFT_J:
  1015. break;
  1016. default:
  1017. return -EINVAL;
  1018. }
  1019. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1020. case SND_SOC_DAIFMT_NB_NF:
  1021. break;
  1022. case SND_SOC_DAIFMT_NB_IF:
  1023. reg14val |= M98088_DAI_WCI;
  1024. break;
  1025. case SND_SOC_DAIFMT_IB_NF:
  1026. reg14val |= M98088_DAI_BCI;
  1027. break;
  1028. case SND_SOC_DAIFMT_IB_IF:
  1029. reg14val |= M98088_DAI_BCI|M98088_DAI_WCI;
  1030. break;
  1031. default:
  1032. return -EINVAL;
  1033. }
  1034. snd_soc_update_bits(codec, M98088_REG_14_DAI1_FORMAT,
  1035. M98088_DAI_MAS | M98088_DAI_DLY | M98088_DAI_BCI |
  1036. M98088_DAI_WCI, reg14val);
  1037. reg15val = M98088_DAI_BSEL64;
  1038. if (max98088->digmic)
  1039. reg15val |= M98088_DAI_OSR64;
  1040. snd_soc_write(codec, M98088_REG_15_DAI1_CLOCK, reg15val);
  1041. }
  1042. return 0;
  1043. }
  1044. static int max98088_dai2_set_fmt(struct snd_soc_dai *codec_dai,
  1045. unsigned int fmt)
  1046. {
  1047. struct snd_soc_codec *codec = codec_dai->codec;
  1048. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1049. struct max98088_cdata *cdata;
  1050. u8 reg1Cval = 0;
  1051. cdata = &max98088->dai[1];
  1052. if (fmt != cdata->fmt) {
  1053. cdata->fmt = fmt;
  1054. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1055. case SND_SOC_DAIFMT_CBS_CFS:
  1056. /* Slave mode PLL */
  1057. snd_soc_write(codec, M98088_REG_1A_DAI2_CLKCFG_HI,
  1058. 0x80);
  1059. snd_soc_write(codec, M98088_REG_1B_DAI2_CLKCFG_LO,
  1060. 0x00);
  1061. break;
  1062. case SND_SOC_DAIFMT_CBM_CFM:
  1063. /* Set to master mode */
  1064. reg1Cval |= M98088_DAI_MAS;
  1065. break;
  1066. case SND_SOC_DAIFMT_CBS_CFM:
  1067. case SND_SOC_DAIFMT_CBM_CFS:
  1068. default:
  1069. dev_err(codec->dev, "Clock mode unsupported");
  1070. return -EINVAL;
  1071. }
  1072. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1073. case SND_SOC_DAIFMT_I2S:
  1074. reg1Cval |= M98088_DAI_DLY;
  1075. break;
  1076. case SND_SOC_DAIFMT_LEFT_J:
  1077. break;
  1078. default:
  1079. return -EINVAL;
  1080. }
  1081. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1082. case SND_SOC_DAIFMT_NB_NF:
  1083. break;
  1084. case SND_SOC_DAIFMT_NB_IF:
  1085. reg1Cval |= M98088_DAI_WCI;
  1086. break;
  1087. case SND_SOC_DAIFMT_IB_NF:
  1088. reg1Cval |= M98088_DAI_BCI;
  1089. break;
  1090. case SND_SOC_DAIFMT_IB_IF:
  1091. reg1Cval |= M98088_DAI_BCI|M98088_DAI_WCI;
  1092. break;
  1093. default:
  1094. return -EINVAL;
  1095. }
  1096. snd_soc_update_bits(codec, M98088_REG_1C_DAI2_FORMAT,
  1097. M98088_DAI_MAS | M98088_DAI_DLY | M98088_DAI_BCI |
  1098. M98088_DAI_WCI, reg1Cval);
  1099. snd_soc_write(codec, M98088_REG_1D_DAI2_CLOCK,
  1100. M98088_DAI_BSEL64);
  1101. }
  1102. return 0;
  1103. }
  1104. static int max98088_dai1_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1105. {
  1106. struct snd_soc_codec *codec = codec_dai->codec;
  1107. int reg;
  1108. if (mute)
  1109. reg = M98088_DAI_MUTE;
  1110. else
  1111. reg = 0;
  1112. snd_soc_update_bits(codec, M98088_REG_2F_LVL_DAI1_PLAY,
  1113. M98088_DAI_MUTE_MASK, reg);
  1114. return 0;
  1115. }
  1116. static int max98088_dai2_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1117. {
  1118. struct snd_soc_codec *codec = codec_dai->codec;
  1119. int reg;
  1120. if (mute)
  1121. reg = M98088_DAI_MUTE;
  1122. else
  1123. reg = 0;
  1124. snd_soc_update_bits(codec, M98088_REG_31_LVL_DAI2_PLAY,
  1125. M98088_DAI_MUTE_MASK, reg);
  1126. return 0;
  1127. }
  1128. static int max98088_set_bias_level(struct snd_soc_codec *codec,
  1129. enum snd_soc_bias_level level)
  1130. {
  1131. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1132. switch (level) {
  1133. case SND_SOC_BIAS_ON:
  1134. break;
  1135. case SND_SOC_BIAS_PREPARE:
  1136. break;
  1137. case SND_SOC_BIAS_STANDBY:
  1138. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF)
  1139. regcache_sync(max98088->regmap);
  1140. snd_soc_update_bits(codec, M98088_REG_4C_PWR_EN_IN,
  1141. M98088_MBEN, M98088_MBEN);
  1142. break;
  1143. case SND_SOC_BIAS_OFF:
  1144. snd_soc_update_bits(codec, M98088_REG_4C_PWR_EN_IN,
  1145. M98088_MBEN, 0);
  1146. regcache_mark_dirty(max98088->regmap);
  1147. break;
  1148. }
  1149. return 0;
  1150. }
  1151. #define MAX98088_RATES SNDRV_PCM_RATE_8000_96000
  1152. #define MAX98088_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
  1153. static const struct snd_soc_dai_ops max98088_dai1_ops = {
  1154. .set_sysclk = max98088_dai_set_sysclk,
  1155. .set_fmt = max98088_dai1_set_fmt,
  1156. .hw_params = max98088_dai1_hw_params,
  1157. .digital_mute = max98088_dai1_digital_mute,
  1158. };
  1159. static const struct snd_soc_dai_ops max98088_dai2_ops = {
  1160. .set_sysclk = max98088_dai_set_sysclk,
  1161. .set_fmt = max98088_dai2_set_fmt,
  1162. .hw_params = max98088_dai2_hw_params,
  1163. .digital_mute = max98088_dai2_digital_mute,
  1164. };
  1165. static struct snd_soc_dai_driver max98088_dai[] = {
  1166. {
  1167. .name = "HiFi",
  1168. .playback = {
  1169. .stream_name = "HiFi Playback",
  1170. .channels_min = 1,
  1171. .channels_max = 2,
  1172. .rates = MAX98088_RATES,
  1173. .formats = MAX98088_FORMATS,
  1174. },
  1175. .capture = {
  1176. .stream_name = "HiFi Capture",
  1177. .channels_min = 1,
  1178. .channels_max = 2,
  1179. .rates = MAX98088_RATES,
  1180. .formats = MAX98088_FORMATS,
  1181. },
  1182. .ops = &max98088_dai1_ops,
  1183. },
  1184. {
  1185. .name = "Aux",
  1186. .playback = {
  1187. .stream_name = "Aux Playback",
  1188. .channels_min = 1,
  1189. .channels_max = 2,
  1190. .rates = MAX98088_RATES,
  1191. .formats = MAX98088_FORMATS,
  1192. },
  1193. .ops = &max98088_dai2_ops,
  1194. }
  1195. };
  1196. static const char *eq_mode_name[] = {"EQ1 Mode", "EQ2 Mode"};
  1197. static int max98088_get_channel(struct snd_soc_codec *codec, const char *name)
  1198. {
  1199. int i;
  1200. for (i = 0; i < ARRAY_SIZE(eq_mode_name); i++)
  1201. if (strcmp(name, eq_mode_name[i]) == 0)
  1202. return i;
  1203. /* Shouldn't happen */
  1204. dev_err(codec->dev, "Bad EQ channel name '%s'\n", name);
  1205. return -EINVAL;
  1206. }
  1207. static void max98088_setup_eq1(struct snd_soc_codec *codec)
  1208. {
  1209. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1210. struct max98088_pdata *pdata = max98088->pdata;
  1211. struct max98088_eq_cfg *coef_set;
  1212. int best, best_val, save, i, sel, fs;
  1213. struct max98088_cdata *cdata;
  1214. cdata = &max98088->dai[0];
  1215. if (!pdata || !max98088->eq_textcnt)
  1216. return;
  1217. /* Find the selected configuration with nearest sample rate */
  1218. fs = cdata->rate;
  1219. sel = cdata->eq_sel;
  1220. best = 0;
  1221. best_val = INT_MAX;
  1222. for (i = 0; i < pdata->eq_cfgcnt; i++) {
  1223. if (strcmp(pdata->eq_cfg[i].name, max98088->eq_texts[sel]) == 0 &&
  1224. abs(pdata->eq_cfg[i].rate - fs) < best_val) {
  1225. best = i;
  1226. best_val = abs(pdata->eq_cfg[i].rate - fs);
  1227. }
  1228. }
  1229. dev_dbg(codec->dev, "Selected %s/%dHz for %dHz sample rate\n",
  1230. pdata->eq_cfg[best].name,
  1231. pdata->eq_cfg[best].rate, fs);
  1232. /* Disable EQ while configuring, and save current on/off state */
  1233. save = snd_soc_read(codec, M98088_REG_49_CFG_LEVEL);
  1234. snd_soc_update_bits(codec, M98088_REG_49_CFG_LEVEL, M98088_EQ1EN, 0);
  1235. coef_set = &pdata->eq_cfg[sel];
  1236. m98088_eq_band(codec, 0, 0, coef_set->band1);
  1237. m98088_eq_band(codec, 0, 1, coef_set->band2);
  1238. m98088_eq_band(codec, 0, 2, coef_set->band3);
  1239. m98088_eq_band(codec, 0, 3, coef_set->band4);
  1240. m98088_eq_band(codec, 0, 4, coef_set->band5);
  1241. /* Restore the original on/off state */
  1242. snd_soc_update_bits(codec, M98088_REG_49_CFG_LEVEL, M98088_EQ1EN, save);
  1243. }
  1244. static void max98088_setup_eq2(struct snd_soc_codec *codec)
  1245. {
  1246. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1247. struct max98088_pdata *pdata = max98088->pdata;
  1248. struct max98088_eq_cfg *coef_set;
  1249. int best, best_val, save, i, sel, fs;
  1250. struct max98088_cdata *cdata;
  1251. cdata = &max98088->dai[1];
  1252. if (!pdata || !max98088->eq_textcnt)
  1253. return;
  1254. /* Find the selected configuration with nearest sample rate */
  1255. fs = cdata->rate;
  1256. sel = cdata->eq_sel;
  1257. best = 0;
  1258. best_val = INT_MAX;
  1259. for (i = 0; i < pdata->eq_cfgcnt; i++) {
  1260. if (strcmp(pdata->eq_cfg[i].name, max98088->eq_texts[sel]) == 0 &&
  1261. abs(pdata->eq_cfg[i].rate - fs) < best_val) {
  1262. best = i;
  1263. best_val = abs(pdata->eq_cfg[i].rate - fs);
  1264. }
  1265. }
  1266. dev_dbg(codec->dev, "Selected %s/%dHz for %dHz sample rate\n",
  1267. pdata->eq_cfg[best].name,
  1268. pdata->eq_cfg[best].rate, fs);
  1269. /* Disable EQ while configuring, and save current on/off state */
  1270. save = snd_soc_read(codec, M98088_REG_49_CFG_LEVEL);
  1271. snd_soc_update_bits(codec, M98088_REG_49_CFG_LEVEL, M98088_EQ2EN, 0);
  1272. coef_set = &pdata->eq_cfg[sel];
  1273. m98088_eq_band(codec, 1, 0, coef_set->band1);
  1274. m98088_eq_band(codec, 1, 1, coef_set->band2);
  1275. m98088_eq_band(codec, 1, 2, coef_set->band3);
  1276. m98088_eq_band(codec, 1, 3, coef_set->band4);
  1277. m98088_eq_band(codec, 1, 4, coef_set->band5);
  1278. /* Restore the original on/off state */
  1279. snd_soc_update_bits(codec, M98088_REG_49_CFG_LEVEL, M98088_EQ2EN,
  1280. save);
  1281. }
  1282. static int max98088_put_eq_enum(struct snd_kcontrol *kcontrol,
  1283. struct snd_ctl_elem_value *ucontrol)
  1284. {
  1285. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1286. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1287. struct max98088_pdata *pdata = max98088->pdata;
  1288. int channel = max98088_get_channel(codec, kcontrol->id.name);
  1289. struct max98088_cdata *cdata;
  1290. int sel = ucontrol->value.enumerated.item[0];
  1291. if (channel < 0)
  1292. return channel;
  1293. cdata = &max98088->dai[channel];
  1294. if (sel >= pdata->eq_cfgcnt)
  1295. return -EINVAL;
  1296. cdata->eq_sel = sel;
  1297. switch (channel) {
  1298. case 0:
  1299. max98088_setup_eq1(codec);
  1300. break;
  1301. case 1:
  1302. max98088_setup_eq2(codec);
  1303. break;
  1304. }
  1305. return 0;
  1306. }
  1307. static int max98088_get_eq_enum(struct snd_kcontrol *kcontrol,
  1308. struct snd_ctl_elem_value *ucontrol)
  1309. {
  1310. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1311. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1312. int channel = max98088_get_channel(codec, kcontrol->id.name);
  1313. struct max98088_cdata *cdata;
  1314. if (channel < 0)
  1315. return channel;
  1316. cdata = &max98088->dai[channel];
  1317. ucontrol->value.enumerated.item[0] = cdata->eq_sel;
  1318. return 0;
  1319. }
  1320. static void max98088_handle_eq_pdata(struct snd_soc_codec *codec)
  1321. {
  1322. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1323. struct max98088_pdata *pdata = max98088->pdata;
  1324. struct max98088_eq_cfg *cfg;
  1325. unsigned int cfgcnt;
  1326. int i, j;
  1327. const char **t;
  1328. int ret;
  1329. struct snd_kcontrol_new controls[] = {
  1330. SOC_ENUM_EXT((char *)eq_mode_name[0],
  1331. max98088->eq_enum,
  1332. max98088_get_eq_enum,
  1333. max98088_put_eq_enum),
  1334. SOC_ENUM_EXT((char *)eq_mode_name[1],
  1335. max98088->eq_enum,
  1336. max98088_get_eq_enum,
  1337. max98088_put_eq_enum),
  1338. };
  1339. BUILD_BUG_ON(ARRAY_SIZE(controls) != ARRAY_SIZE(eq_mode_name));
  1340. cfg = pdata->eq_cfg;
  1341. cfgcnt = pdata->eq_cfgcnt;
  1342. /* Setup an array of texts for the equalizer enum.
  1343. * This is based on Mark Brown's equalizer driver code.
  1344. */
  1345. max98088->eq_textcnt = 0;
  1346. max98088->eq_texts = NULL;
  1347. for (i = 0; i < cfgcnt; i++) {
  1348. for (j = 0; j < max98088->eq_textcnt; j++) {
  1349. if (strcmp(cfg[i].name, max98088->eq_texts[j]) == 0)
  1350. break;
  1351. }
  1352. if (j != max98088->eq_textcnt)
  1353. continue;
  1354. /* Expand the array */
  1355. t = krealloc(max98088->eq_texts,
  1356. sizeof(char *) * (max98088->eq_textcnt + 1),
  1357. GFP_KERNEL);
  1358. if (t == NULL)
  1359. continue;
  1360. /* Store the new entry */
  1361. t[max98088->eq_textcnt] = cfg[i].name;
  1362. max98088->eq_textcnt++;
  1363. max98088->eq_texts = t;
  1364. }
  1365. /* Now point the soc_enum to .texts array items */
  1366. max98088->eq_enum.texts = max98088->eq_texts;
  1367. max98088->eq_enum.items = max98088->eq_textcnt;
  1368. ret = snd_soc_add_codec_controls(codec, controls, ARRAY_SIZE(controls));
  1369. if (ret != 0)
  1370. dev_err(codec->dev, "Failed to add EQ control: %d\n", ret);
  1371. }
  1372. static void max98088_handle_pdata(struct snd_soc_codec *codec)
  1373. {
  1374. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1375. struct max98088_pdata *pdata = max98088->pdata;
  1376. u8 regval = 0;
  1377. if (!pdata) {
  1378. dev_dbg(codec->dev, "No platform data\n");
  1379. return;
  1380. }
  1381. /* Configure mic for analog/digital mic mode */
  1382. if (pdata->digmic_left_mode)
  1383. regval |= M98088_DIGMIC_L;
  1384. if (pdata->digmic_right_mode)
  1385. regval |= M98088_DIGMIC_R;
  1386. max98088->digmic = (regval ? 1 : 0);
  1387. snd_soc_write(codec, M98088_REG_48_CFG_MIC, regval);
  1388. /* Configure receiver output */
  1389. regval = ((pdata->receiver_mode) ? M98088_REC_LINEMODE : 0);
  1390. snd_soc_update_bits(codec, M98088_REG_2A_MIC_REC_CNTL,
  1391. M98088_REC_LINEMODE_MASK, regval);
  1392. /* Configure equalizers */
  1393. if (pdata->eq_cfgcnt)
  1394. max98088_handle_eq_pdata(codec);
  1395. }
  1396. static int max98088_probe(struct snd_soc_codec *codec)
  1397. {
  1398. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1399. struct max98088_cdata *cdata;
  1400. int ret = 0;
  1401. regcache_mark_dirty(max98088->regmap);
  1402. /* initialize private data */
  1403. max98088->sysclk = (unsigned)-1;
  1404. max98088->eq_textcnt = 0;
  1405. cdata = &max98088->dai[0];
  1406. cdata->rate = (unsigned)-1;
  1407. cdata->fmt = (unsigned)-1;
  1408. cdata->eq_sel = 0;
  1409. cdata = &max98088->dai[1];
  1410. cdata->rate = (unsigned)-1;
  1411. cdata->fmt = (unsigned)-1;
  1412. cdata->eq_sel = 0;
  1413. max98088->ina_state = 0;
  1414. max98088->inb_state = 0;
  1415. max98088->ex_mode = 0;
  1416. max98088->digmic = 0;
  1417. max98088->mic1pre = 0;
  1418. max98088->mic2pre = 0;
  1419. ret = snd_soc_read(codec, M98088_REG_FF_REV_ID);
  1420. if (ret < 0) {
  1421. dev_err(codec->dev, "Failed to read device revision: %d\n",
  1422. ret);
  1423. goto err_access;
  1424. }
  1425. dev_info(codec->dev, "revision %c\n", ret - 0x40 + 'A');
  1426. snd_soc_write(codec, M98088_REG_51_PWR_SYS, M98088_PWRSV);
  1427. snd_soc_write(codec, M98088_REG_0F_IRQ_ENABLE, 0x00);
  1428. snd_soc_write(codec, M98088_REG_22_MIX_DAC,
  1429. M98088_DAI1L_TO_DACL|M98088_DAI2L_TO_DACL|
  1430. M98088_DAI1R_TO_DACR|M98088_DAI2R_TO_DACR);
  1431. snd_soc_write(codec, M98088_REG_4E_BIAS_CNTL, 0xF0);
  1432. snd_soc_write(codec, M98088_REG_50_DAC_BIAS2, 0x0F);
  1433. snd_soc_write(codec, M98088_REG_16_DAI1_IOCFG,
  1434. M98088_S1NORMAL|M98088_SDATA);
  1435. snd_soc_write(codec, M98088_REG_1E_DAI2_IOCFG,
  1436. M98088_S2NORMAL|M98088_SDATA);
  1437. max98088_handle_pdata(codec);
  1438. err_access:
  1439. return ret;
  1440. }
  1441. static int max98088_remove(struct snd_soc_codec *codec)
  1442. {
  1443. struct max98088_priv *max98088 = snd_soc_codec_get_drvdata(codec);
  1444. kfree(max98088->eq_texts);
  1445. return 0;
  1446. }
  1447. static struct snd_soc_codec_driver soc_codec_dev_max98088 = {
  1448. .probe = max98088_probe,
  1449. .remove = max98088_remove,
  1450. .set_bias_level = max98088_set_bias_level,
  1451. .suspend_bias_off = true,
  1452. .component_driver = {
  1453. .controls = max98088_snd_controls,
  1454. .num_controls = ARRAY_SIZE(max98088_snd_controls),
  1455. .dapm_widgets = max98088_dapm_widgets,
  1456. .num_dapm_widgets = ARRAY_SIZE(max98088_dapm_widgets),
  1457. .dapm_routes = max98088_audio_map,
  1458. .num_dapm_routes = ARRAY_SIZE(max98088_audio_map),
  1459. },
  1460. };
  1461. static int max98088_i2c_probe(struct i2c_client *i2c,
  1462. const struct i2c_device_id *id)
  1463. {
  1464. struct max98088_priv *max98088;
  1465. int ret;
  1466. max98088 = devm_kzalloc(&i2c->dev, sizeof(struct max98088_priv),
  1467. GFP_KERNEL);
  1468. if (max98088 == NULL)
  1469. return -ENOMEM;
  1470. max98088->regmap = devm_regmap_init_i2c(i2c, &max98088_regmap);
  1471. if (IS_ERR(max98088->regmap))
  1472. return PTR_ERR(max98088->regmap);
  1473. max98088->devtype = id->driver_data;
  1474. i2c_set_clientdata(i2c, max98088);
  1475. max98088->pdata = i2c->dev.platform_data;
  1476. ret = snd_soc_register_codec(&i2c->dev,
  1477. &soc_codec_dev_max98088, &max98088_dai[0], 2);
  1478. return ret;
  1479. }
  1480. static int max98088_i2c_remove(struct i2c_client *client)
  1481. {
  1482. snd_soc_unregister_codec(&client->dev);
  1483. return 0;
  1484. }
  1485. static const struct i2c_device_id max98088_i2c_id[] = {
  1486. { "max98088", MAX98088 },
  1487. { "max98089", MAX98089 },
  1488. { }
  1489. };
  1490. MODULE_DEVICE_TABLE(i2c, max98088_i2c_id);
  1491. static struct i2c_driver max98088_i2c_driver = {
  1492. .driver = {
  1493. .name = "max98088",
  1494. },
  1495. .probe = max98088_i2c_probe,
  1496. .remove = max98088_i2c_remove,
  1497. .id_table = max98088_i2c_id,
  1498. };
  1499. module_i2c_driver(max98088_i2c_driver);
  1500. MODULE_DESCRIPTION("ALSA SoC MAX98088 driver");
  1501. MODULE_AUTHOR("Peter Hsiang, Jesse Marroquin");
  1502. MODULE_LICENSE("GPL");