max98095.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  1. /*
  2. * max98095.c -- MAX98095 ALSA SoC Audio driver
  3. *
  4. * Copyright 2011 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/clk.h>
  18. #include <linux/mutex.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #include <sound/initval.h>
  24. #include <sound/tlv.h>
  25. #include <linux/slab.h>
  26. #include <asm/div64.h>
  27. #include <sound/max98095.h>
  28. #include <sound/jack.h>
  29. #include "max98095.h"
  30. enum max98095_type {
  31. MAX98095,
  32. };
  33. struct max98095_cdata {
  34. unsigned int rate;
  35. unsigned int fmt;
  36. int eq_sel;
  37. int bq_sel;
  38. };
  39. struct max98095_priv {
  40. struct regmap *regmap;
  41. enum max98095_type devtype;
  42. struct max98095_pdata *pdata;
  43. struct clk *mclk;
  44. unsigned int sysclk;
  45. struct max98095_cdata dai[3];
  46. const char **eq_texts;
  47. const char **bq_texts;
  48. struct soc_enum eq_enum;
  49. struct soc_enum bq_enum;
  50. int eq_textcnt;
  51. int bq_textcnt;
  52. u8 lin_state;
  53. unsigned int mic1pre;
  54. unsigned int mic2pre;
  55. struct snd_soc_jack *headphone_jack;
  56. struct snd_soc_jack *mic_jack;
  57. struct mutex lock;
  58. };
  59. static const struct reg_default max98095_reg_def[] = {
  60. { 0xf, 0x00 }, /* 0F */
  61. { 0x10, 0x00 }, /* 10 */
  62. { 0x11, 0x00 }, /* 11 */
  63. { 0x12, 0x00 }, /* 12 */
  64. { 0x13, 0x00 }, /* 13 */
  65. { 0x14, 0x00 }, /* 14 */
  66. { 0x15, 0x00 }, /* 15 */
  67. { 0x16, 0x00 }, /* 16 */
  68. { 0x17, 0x00 }, /* 17 */
  69. { 0x18, 0x00 }, /* 18 */
  70. { 0x19, 0x00 }, /* 19 */
  71. { 0x1a, 0x00 }, /* 1A */
  72. { 0x1b, 0x00 }, /* 1B */
  73. { 0x1c, 0x00 }, /* 1C */
  74. { 0x1d, 0x00 }, /* 1D */
  75. { 0x1e, 0x00 }, /* 1E */
  76. { 0x1f, 0x00 }, /* 1F */
  77. { 0x20, 0x00 }, /* 20 */
  78. { 0x21, 0x00 }, /* 21 */
  79. { 0x22, 0x00 }, /* 22 */
  80. { 0x23, 0x00 }, /* 23 */
  81. { 0x24, 0x00 }, /* 24 */
  82. { 0x25, 0x00 }, /* 25 */
  83. { 0x26, 0x00 }, /* 26 */
  84. { 0x27, 0x00 }, /* 27 */
  85. { 0x28, 0x00 }, /* 28 */
  86. { 0x29, 0x00 }, /* 29 */
  87. { 0x2a, 0x00 }, /* 2A */
  88. { 0x2b, 0x00 }, /* 2B */
  89. { 0x2c, 0x00 }, /* 2C */
  90. { 0x2d, 0x00 }, /* 2D */
  91. { 0x2e, 0x00 }, /* 2E */
  92. { 0x2f, 0x00 }, /* 2F */
  93. { 0x30, 0x00 }, /* 30 */
  94. { 0x31, 0x00 }, /* 31 */
  95. { 0x32, 0x00 }, /* 32 */
  96. { 0x33, 0x00 }, /* 33 */
  97. { 0x34, 0x00 }, /* 34 */
  98. { 0x35, 0x00 }, /* 35 */
  99. { 0x36, 0x00 }, /* 36 */
  100. { 0x37, 0x00 }, /* 37 */
  101. { 0x38, 0x00 }, /* 38 */
  102. { 0x39, 0x00 }, /* 39 */
  103. { 0x3a, 0x00 }, /* 3A */
  104. { 0x3b, 0x00 }, /* 3B */
  105. { 0x3c, 0x00 }, /* 3C */
  106. { 0x3d, 0x00 }, /* 3D */
  107. { 0x3e, 0x00 }, /* 3E */
  108. { 0x3f, 0x00 }, /* 3F */
  109. { 0x40, 0x00 }, /* 40 */
  110. { 0x41, 0x00 }, /* 41 */
  111. { 0x42, 0x00 }, /* 42 */
  112. { 0x43, 0x00 }, /* 43 */
  113. { 0x44, 0x00 }, /* 44 */
  114. { 0x45, 0x00 }, /* 45 */
  115. { 0x46, 0x00 }, /* 46 */
  116. { 0x47, 0x00 }, /* 47 */
  117. { 0x48, 0x00 }, /* 48 */
  118. { 0x49, 0x00 }, /* 49 */
  119. { 0x4a, 0x00 }, /* 4A */
  120. { 0x4b, 0x00 }, /* 4B */
  121. { 0x4c, 0x00 }, /* 4C */
  122. { 0x4d, 0x00 }, /* 4D */
  123. { 0x4e, 0x00 }, /* 4E */
  124. { 0x4f, 0x00 }, /* 4F */
  125. { 0x50, 0x00 }, /* 50 */
  126. { 0x51, 0x00 }, /* 51 */
  127. { 0x52, 0x00 }, /* 52 */
  128. { 0x53, 0x00 }, /* 53 */
  129. { 0x54, 0x00 }, /* 54 */
  130. { 0x55, 0x00 }, /* 55 */
  131. { 0x56, 0x00 }, /* 56 */
  132. { 0x57, 0x00 }, /* 57 */
  133. { 0x58, 0x00 }, /* 58 */
  134. { 0x59, 0x00 }, /* 59 */
  135. { 0x5a, 0x00 }, /* 5A */
  136. { 0x5b, 0x00 }, /* 5B */
  137. { 0x5c, 0x00 }, /* 5C */
  138. { 0x5d, 0x00 }, /* 5D */
  139. { 0x5e, 0x00 }, /* 5E */
  140. { 0x5f, 0x00 }, /* 5F */
  141. { 0x60, 0x00 }, /* 60 */
  142. { 0x61, 0x00 }, /* 61 */
  143. { 0x62, 0x00 }, /* 62 */
  144. { 0x63, 0x00 }, /* 63 */
  145. { 0x64, 0x00 }, /* 64 */
  146. { 0x65, 0x00 }, /* 65 */
  147. { 0x66, 0x00 }, /* 66 */
  148. { 0x67, 0x00 }, /* 67 */
  149. { 0x68, 0x00 }, /* 68 */
  150. { 0x69, 0x00 }, /* 69 */
  151. { 0x6a, 0x00 }, /* 6A */
  152. { 0x6b, 0x00 }, /* 6B */
  153. { 0x6c, 0x00 }, /* 6C */
  154. { 0x6d, 0x00 }, /* 6D */
  155. { 0x6e, 0x00 }, /* 6E */
  156. { 0x6f, 0x00 }, /* 6F */
  157. { 0x70, 0x00 }, /* 70 */
  158. { 0x71, 0x00 }, /* 71 */
  159. { 0x72, 0x00 }, /* 72 */
  160. { 0x73, 0x00 }, /* 73 */
  161. { 0x74, 0x00 }, /* 74 */
  162. { 0x75, 0x00 }, /* 75 */
  163. { 0x76, 0x00 }, /* 76 */
  164. { 0x77, 0x00 }, /* 77 */
  165. { 0x78, 0x00 }, /* 78 */
  166. { 0x79, 0x00 }, /* 79 */
  167. { 0x7a, 0x00 }, /* 7A */
  168. { 0x7b, 0x00 }, /* 7B */
  169. { 0x7c, 0x00 }, /* 7C */
  170. { 0x7d, 0x00 }, /* 7D */
  171. { 0x7e, 0x00 }, /* 7E */
  172. { 0x7f, 0x00 }, /* 7F */
  173. { 0x80, 0x00 }, /* 80 */
  174. { 0x81, 0x00 }, /* 81 */
  175. { 0x82, 0x00 }, /* 82 */
  176. { 0x83, 0x00 }, /* 83 */
  177. { 0x84, 0x00 }, /* 84 */
  178. { 0x85, 0x00 }, /* 85 */
  179. { 0x86, 0x00 }, /* 86 */
  180. { 0x87, 0x00 }, /* 87 */
  181. { 0x88, 0x00 }, /* 88 */
  182. { 0x89, 0x00 }, /* 89 */
  183. { 0x8a, 0x00 }, /* 8A */
  184. { 0x8b, 0x00 }, /* 8B */
  185. { 0x8c, 0x00 }, /* 8C */
  186. { 0x8d, 0x00 }, /* 8D */
  187. { 0x8e, 0x00 }, /* 8E */
  188. { 0x8f, 0x00 }, /* 8F */
  189. { 0x90, 0x00 }, /* 90 */
  190. { 0x91, 0x00 }, /* 91 */
  191. { 0x92, 0x30 }, /* 92 */
  192. { 0x93, 0xF0 }, /* 93 */
  193. { 0x94, 0x00 }, /* 94 */
  194. { 0x95, 0x00 }, /* 95 */
  195. { 0x96, 0x3F }, /* 96 */
  196. { 0x97, 0x00 }, /* 97 */
  197. { 0xff, 0x00 }, /* FF */
  198. };
  199. static bool max98095_readable(struct device *dev, unsigned int reg)
  200. {
  201. switch (reg) {
  202. case M98095_001_HOST_INT_STS ... M98095_097_PWR_SYS:
  203. case M98095_0FF_REV_ID:
  204. return true;
  205. default:
  206. return false;
  207. }
  208. }
  209. static bool max98095_writeable(struct device *dev, unsigned int reg)
  210. {
  211. switch (reg) {
  212. case M98095_00F_HOST_CFG ... M98095_097_PWR_SYS:
  213. return true;
  214. default:
  215. return false;
  216. }
  217. }
  218. static bool max98095_volatile(struct device *dev, unsigned int reg)
  219. {
  220. switch (reg) {
  221. case M98095_000_HOST_DATA ... M98095_00E_TEMP_SENSOR_STS:
  222. case M98095_REG_MAX_CACHED + 1 ... M98095_0FF_REV_ID:
  223. return true;
  224. default:
  225. return false;
  226. }
  227. }
  228. static const struct regmap_config max98095_regmap = {
  229. .reg_bits = 8,
  230. .val_bits = 8,
  231. .reg_defaults = max98095_reg_def,
  232. .num_reg_defaults = ARRAY_SIZE(max98095_reg_def),
  233. .max_register = M98095_0FF_REV_ID,
  234. .cache_type = REGCACHE_RBTREE,
  235. .readable_reg = max98095_readable,
  236. .writeable_reg = max98095_writeable,
  237. .volatile_reg = max98095_volatile,
  238. };
  239. /*
  240. * Load equalizer DSP coefficient configurations registers
  241. */
  242. static void m98095_eq_band(struct snd_soc_codec *codec, unsigned int dai,
  243. unsigned int band, u16 *coefs)
  244. {
  245. unsigned int eq_reg;
  246. unsigned int i;
  247. if (WARN_ON(band > 4) ||
  248. WARN_ON(dai > 1))
  249. return;
  250. /* Load the base register address */
  251. eq_reg = dai ? M98095_142_DAI2_EQ_BASE : M98095_110_DAI1_EQ_BASE;
  252. /* Add the band address offset, note adjustment for word address */
  253. eq_reg += band * (M98095_COEFS_PER_BAND << 1);
  254. /* Step through the registers and coefs */
  255. for (i = 0; i < M98095_COEFS_PER_BAND; i++) {
  256. snd_soc_write(codec, eq_reg++, M98095_BYTE1(coefs[i]));
  257. snd_soc_write(codec, eq_reg++, M98095_BYTE0(coefs[i]));
  258. }
  259. }
  260. /*
  261. * Load biquad filter coefficient configurations registers
  262. */
  263. static void m98095_biquad_band(struct snd_soc_codec *codec, unsigned int dai,
  264. unsigned int band, u16 *coefs)
  265. {
  266. unsigned int bq_reg;
  267. unsigned int i;
  268. if (WARN_ON(band > 1) ||
  269. WARN_ON(dai > 1))
  270. return;
  271. /* Load the base register address */
  272. bq_reg = dai ? M98095_17E_DAI2_BQ_BASE : M98095_174_DAI1_BQ_BASE;
  273. /* Add the band address offset, note adjustment for word address */
  274. bq_reg += band * (M98095_COEFS_PER_BAND << 1);
  275. /* Step through the registers and coefs */
  276. for (i = 0; i < M98095_COEFS_PER_BAND; i++) {
  277. snd_soc_write(codec, bq_reg++, M98095_BYTE1(coefs[i]));
  278. snd_soc_write(codec, bq_reg++, M98095_BYTE0(coefs[i]));
  279. }
  280. }
  281. static const char * const max98095_fltr_mode[] = { "Voice", "Music" };
  282. static SOC_ENUM_SINGLE_DECL(max98095_dai1_filter_mode_enum,
  283. M98095_02E_DAI1_FILTERS, 7,
  284. max98095_fltr_mode);
  285. static SOC_ENUM_SINGLE_DECL(max98095_dai2_filter_mode_enum,
  286. M98095_038_DAI2_FILTERS, 7,
  287. max98095_fltr_mode);
  288. static const char * const max98095_extmic_text[] = { "None", "MIC1", "MIC2" };
  289. static SOC_ENUM_SINGLE_DECL(max98095_extmic_enum,
  290. M98095_087_CFG_MIC, 0,
  291. max98095_extmic_text);
  292. static const struct snd_kcontrol_new max98095_extmic_mux =
  293. SOC_DAPM_ENUM("External MIC Mux", max98095_extmic_enum);
  294. static const char * const max98095_linein_text[] = { "INA", "INB" };
  295. static SOC_ENUM_SINGLE_DECL(max98095_linein_enum,
  296. M98095_086_CFG_LINE, 6,
  297. max98095_linein_text);
  298. static const struct snd_kcontrol_new max98095_linein_mux =
  299. SOC_DAPM_ENUM("Linein Input Mux", max98095_linein_enum);
  300. static const char * const max98095_line_mode_text[] = {
  301. "Stereo", "Differential"};
  302. static SOC_ENUM_SINGLE_DECL(max98095_linein_mode_enum,
  303. M98095_086_CFG_LINE, 7,
  304. max98095_line_mode_text);
  305. static SOC_ENUM_SINGLE_DECL(max98095_lineout_mode_enum,
  306. M98095_086_CFG_LINE, 4,
  307. max98095_line_mode_text);
  308. static const char * const max98095_dai_fltr[] = {
  309. "Off", "Elliptical-HPF-16k", "Butterworth-HPF-16k",
  310. "Elliptical-HPF-8k", "Butterworth-HPF-8k", "Butterworth-HPF-Fs/240"};
  311. static SOC_ENUM_SINGLE_DECL(max98095_dai1_dac_filter_enum,
  312. M98095_02E_DAI1_FILTERS, 0,
  313. max98095_dai_fltr);
  314. static SOC_ENUM_SINGLE_DECL(max98095_dai2_dac_filter_enum,
  315. M98095_038_DAI2_FILTERS, 0,
  316. max98095_dai_fltr);
  317. static SOC_ENUM_SINGLE_DECL(max98095_dai3_dac_filter_enum,
  318. M98095_042_DAI3_FILTERS, 0,
  319. max98095_dai_fltr);
  320. static int max98095_mic1pre_set(struct snd_kcontrol *kcontrol,
  321. struct snd_ctl_elem_value *ucontrol)
  322. {
  323. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  324. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  325. unsigned int sel = ucontrol->value.integer.value[0];
  326. max98095->mic1pre = sel;
  327. snd_soc_update_bits(codec, M98095_05F_LVL_MIC1, M98095_MICPRE_MASK,
  328. (1+sel)<<M98095_MICPRE_SHIFT);
  329. return 0;
  330. }
  331. static int max98095_mic1pre_get(struct snd_kcontrol *kcontrol,
  332. struct snd_ctl_elem_value *ucontrol)
  333. {
  334. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  335. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  336. ucontrol->value.integer.value[0] = max98095->mic1pre;
  337. return 0;
  338. }
  339. static int max98095_mic2pre_set(struct snd_kcontrol *kcontrol,
  340. struct snd_ctl_elem_value *ucontrol)
  341. {
  342. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  343. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  344. unsigned int sel = ucontrol->value.integer.value[0];
  345. max98095->mic2pre = sel;
  346. snd_soc_update_bits(codec, M98095_060_LVL_MIC2, M98095_MICPRE_MASK,
  347. (1+sel)<<M98095_MICPRE_SHIFT);
  348. return 0;
  349. }
  350. static int max98095_mic2pre_get(struct snd_kcontrol *kcontrol,
  351. struct snd_ctl_elem_value *ucontrol)
  352. {
  353. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  354. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  355. ucontrol->value.integer.value[0] = max98095->mic2pre;
  356. return 0;
  357. }
  358. static const DECLARE_TLV_DB_RANGE(max98095_micboost_tlv,
  359. 0, 1, TLV_DB_SCALE_ITEM(0, 2000, 0),
  360. 2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0)
  361. );
  362. static const DECLARE_TLV_DB_SCALE(max98095_mic_tlv, 0, 100, 0);
  363. static const DECLARE_TLV_DB_SCALE(max98095_adc_tlv, -1200, 100, 0);
  364. static const DECLARE_TLV_DB_SCALE(max98095_adcboost_tlv, 0, 600, 0);
  365. static const DECLARE_TLV_DB_RANGE(max98095_hp_tlv,
  366. 0, 6, TLV_DB_SCALE_ITEM(-6700, 400, 0),
  367. 7, 14, TLV_DB_SCALE_ITEM(-4000, 300, 0),
  368. 15, 21, TLV_DB_SCALE_ITEM(-1700, 200, 0),
  369. 22, 27, TLV_DB_SCALE_ITEM(-400, 100, 0),
  370. 28, 31, TLV_DB_SCALE_ITEM(150, 50, 0)
  371. );
  372. static const DECLARE_TLV_DB_RANGE(max98095_spk_tlv,
  373. 0, 10, TLV_DB_SCALE_ITEM(-5900, 400, 0),
  374. 11, 18, TLV_DB_SCALE_ITEM(-1700, 200, 0),
  375. 19, 27, TLV_DB_SCALE_ITEM(-200, 100, 0),
  376. 28, 39, TLV_DB_SCALE_ITEM(650, 50, 0)
  377. );
  378. static const DECLARE_TLV_DB_RANGE(max98095_rcv_lout_tlv,
  379. 0, 6, TLV_DB_SCALE_ITEM(-6200, 400, 0),
  380. 7, 14, TLV_DB_SCALE_ITEM(-3500, 300, 0),
  381. 15, 21, TLV_DB_SCALE_ITEM(-1200, 200, 0),
  382. 22, 27, TLV_DB_SCALE_ITEM(100, 100, 0),
  383. 28, 31, TLV_DB_SCALE_ITEM(650, 50, 0)
  384. );
  385. static const DECLARE_TLV_DB_RANGE(max98095_lin_tlv,
  386. 0, 2, TLV_DB_SCALE_ITEM(-600, 300, 0),
  387. 3, 3, TLV_DB_SCALE_ITEM(300, 1100, 0),
  388. 4, 5, TLV_DB_SCALE_ITEM(1400, 600, 0)
  389. );
  390. static const struct snd_kcontrol_new max98095_snd_controls[] = {
  391. SOC_DOUBLE_R_TLV("Headphone Volume", M98095_064_LVL_HP_L,
  392. M98095_065_LVL_HP_R, 0, 31, 0, max98095_hp_tlv),
  393. SOC_DOUBLE_R_TLV("Speaker Volume", M98095_067_LVL_SPK_L,
  394. M98095_068_LVL_SPK_R, 0, 39, 0, max98095_spk_tlv),
  395. SOC_SINGLE_TLV("Receiver Volume", M98095_066_LVL_RCV,
  396. 0, 31, 0, max98095_rcv_lout_tlv),
  397. SOC_DOUBLE_R_TLV("Lineout Volume", M98095_062_LVL_LINEOUT1,
  398. M98095_063_LVL_LINEOUT2, 0, 31, 0, max98095_rcv_lout_tlv),
  399. SOC_DOUBLE_R("Headphone Switch", M98095_064_LVL_HP_L,
  400. M98095_065_LVL_HP_R, 7, 1, 1),
  401. SOC_DOUBLE_R("Speaker Switch", M98095_067_LVL_SPK_L,
  402. M98095_068_LVL_SPK_R, 7, 1, 1),
  403. SOC_SINGLE("Receiver Switch", M98095_066_LVL_RCV, 7, 1, 1),
  404. SOC_DOUBLE_R("Lineout Switch", M98095_062_LVL_LINEOUT1,
  405. M98095_063_LVL_LINEOUT2, 7, 1, 1),
  406. SOC_SINGLE_TLV("MIC1 Volume", M98095_05F_LVL_MIC1, 0, 20, 1,
  407. max98095_mic_tlv),
  408. SOC_SINGLE_TLV("MIC2 Volume", M98095_060_LVL_MIC2, 0, 20, 1,
  409. max98095_mic_tlv),
  410. SOC_SINGLE_EXT_TLV("MIC1 Boost Volume",
  411. M98095_05F_LVL_MIC1, 5, 2, 0,
  412. max98095_mic1pre_get, max98095_mic1pre_set,
  413. max98095_micboost_tlv),
  414. SOC_SINGLE_EXT_TLV("MIC2 Boost Volume",
  415. M98095_060_LVL_MIC2, 5, 2, 0,
  416. max98095_mic2pre_get, max98095_mic2pre_set,
  417. max98095_micboost_tlv),
  418. SOC_SINGLE_TLV("Linein Volume", M98095_061_LVL_LINEIN, 0, 5, 1,
  419. max98095_lin_tlv),
  420. SOC_SINGLE_TLV("ADCL Volume", M98095_05D_LVL_ADC_L, 0, 15, 1,
  421. max98095_adc_tlv),
  422. SOC_SINGLE_TLV("ADCR Volume", M98095_05E_LVL_ADC_R, 0, 15, 1,
  423. max98095_adc_tlv),
  424. SOC_SINGLE_TLV("ADCL Boost Volume", M98095_05D_LVL_ADC_L, 4, 3, 0,
  425. max98095_adcboost_tlv),
  426. SOC_SINGLE_TLV("ADCR Boost Volume", M98095_05E_LVL_ADC_R, 4, 3, 0,
  427. max98095_adcboost_tlv),
  428. SOC_SINGLE("EQ1 Switch", M98095_088_CFG_LEVEL, 0, 1, 0),
  429. SOC_SINGLE("EQ2 Switch", M98095_088_CFG_LEVEL, 1, 1, 0),
  430. SOC_SINGLE("Biquad1 Switch", M98095_088_CFG_LEVEL, 2, 1, 0),
  431. SOC_SINGLE("Biquad2 Switch", M98095_088_CFG_LEVEL, 3, 1, 0),
  432. SOC_ENUM("DAI1 Filter Mode", max98095_dai1_filter_mode_enum),
  433. SOC_ENUM("DAI2 Filter Mode", max98095_dai2_filter_mode_enum),
  434. SOC_ENUM("DAI1 DAC Filter", max98095_dai1_dac_filter_enum),
  435. SOC_ENUM("DAI2 DAC Filter", max98095_dai2_dac_filter_enum),
  436. SOC_ENUM("DAI3 DAC Filter", max98095_dai3_dac_filter_enum),
  437. SOC_ENUM("Linein Mode", max98095_linein_mode_enum),
  438. SOC_ENUM("Lineout Mode", max98095_lineout_mode_enum),
  439. };
  440. /* Left speaker mixer switch */
  441. static const struct snd_kcontrol_new max98095_left_speaker_mixer_controls[] = {
  442. SOC_DAPM_SINGLE("Left DAC1 Switch", M98095_050_MIX_SPK_LEFT, 0, 1, 0),
  443. SOC_DAPM_SINGLE("Right DAC1 Switch", M98095_050_MIX_SPK_LEFT, 6, 1, 0),
  444. SOC_DAPM_SINGLE("Mono DAC2 Switch", M98095_050_MIX_SPK_LEFT, 3, 1, 0),
  445. SOC_DAPM_SINGLE("Mono DAC3 Switch", M98095_050_MIX_SPK_LEFT, 3, 1, 0),
  446. SOC_DAPM_SINGLE("MIC1 Switch", M98095_050_MIX_SPK_LEFT, 4, 1, 0),
  447. SOC_DAPM_SINGLE("MIC2 Switch", M98095_050_MIX_SPK_LEFT, 5, 1, 0),
  448. SOC_DAPM_SINGLE("IN1 Switch", M98095_050_MIX_SPK_LEFT, 1, 1, 0),
  449. SOC_DAPM_SINGLE("IN2 Switch", M98095_050_MIX_SPK_LEFT, 2, 1, 0),
  450. };
  451. /* Right speaker mixer switch */
  452. static const struct snd_kcontrol_new max98095_right_speaker_mixer_controls[] = {
  453. SOC_DAPM_SINGLE("Left DAC1 Switch", M98095_051_MIX_SPK_RIGHT, 6, 1, 0),
  454. SOC_DAPM_SINGLE("Right DAC1 Switch", M98095_051_MIX_SPK_RIGHT, 0, 1, 0),
  455. SOC_DAPM_SINGLE("Mono DAC2 Switch", M98095_051_MIX_SPK_RIGHT, 3, 1, 0),
  456. SOC_DAPM_SINGLE("Mono DAC3 Switch", M98095_051_MIX_SPK_RIGHT, 3, 1, 0),
  457. SOC_DAPM_SINGLE("MIC1 Switch", M98095_051_MIX_SPK_RIGHT, 5, 1, 0),
  458. SOC_DAPM_SINGLE("MIC2 Switch", M98095_051_MIX_SPK_RIGHT, 4, 1, 0),
  459. SOC_DAPM_SINGLE("IN1 Switch", M98095_051_MIX_SPK_RIGHT, 1, 1, 0),
  460. SOC_DAPM_SINGLE("IN2 Switch", M98095_051_MIX_SPK_RIGHT, 2, 1, 0),
  461. };
  462. /* Left headphone mixer switch */
  463. static const struct snd_kcontrol_new max98095_left_hp_mixer_controls[] = {
  464. SOC_DAPM_SINGLE("Left DAC1 Switch", M98095_04C_MIX_HP_LEFT, 0, 1, 0),
  465. SOC_DAPM_SINGLE("Right DAC1 Switch", M98095_04C_MIX_HP_LEFT, 5, 1, 0),
  466. SOC_DAPM_SINGLE("MIC1 Switch", M98095_04C_MIX_HP_LEFT, 3, 1, 0),
  467. SOC_DAPM_SINGLE("MIC2 Switch", M98095_04C_MIX_HP_LEFT, 4, 1, 0),
  468. SOC_DAPM_SINGLE("IN1 Switch", M98095_04C_MIX_HP_LEFT, 1, 1, 0),
  469. SOC_DAPM_SINGLE("IN2 Switch", M98095_04C_MIX_HP_LEFT, 2, 1, 0),
  470. };
  471. /* Right headphone mixer switch */
  472. static const struct snd_kcontrol_new max98095_right_hp_mixer_controls[] = {
  473. SOC_DAPM_SINGLE("Left DAC1 Switch", M98095_04D_MIX_HP_RIGHT, 5, 1, 0),
  474. SOC_DAPM_SINGLE("Right DAC1 Switch", M98095_04D_MIX_HP_RIGHT, 0, 1, 0),
  475. SOC_DAPM_SINGLE("MIC1 Switch", M98095_04D_MIX_HP_RIGHT, 3, 1, 0),
  476. SOC_DAPM_SINGLE("MIC2 Switch", M98095_04D_MIX_HP_RIGHT, 4, 1, 0),
  477. SOC_DAPM_SINGLE("IN1 Switch", M98095_04D_MIX_HP_RIGHT, 1, 1, 0),
  478. SOC_DAPM_SINGLE("IN2 Switch", M98095_04D_MIX_HP_RIGHT, 2, 1, 0),
  479. };
  480. /* Receiver earpiece mixer switch */
  481. static const struct snd_kcontrol_new max98095_mono_rcv_mixer_controls[] = {
  482. SOC_DAPM_SINGLE("Left DAC1 Switch", M98095_04F_MIX_RCV, 0, 1, 0),
  483. SOC_DAPM_SINGLE("Right DAC1 Switch", M98095_04F_MIX_RCV, 5, 1, 0),
  484. SOC_DAPM_SINGLE("MIC1 Switch", M98095_04F_MIX_RCV, 3, 1, 0),
  485. SOC_DAPM_SINGLE("MIC2 Switch", M98095_04F_MIX_RCV, 4, 1, 0),
  486. SOC_DAPM_SINGLE("IN1 Switch", M98095_04F_MIX_RCV, 1, 1, 0),
  487. SOC_DAPM_SINGLE("IN2 Switch", M98095_04F_MIX_RCV, 2, 1, 0),
  488. };
  489. /* Left lineout mixer switch */
  490. static const struct snd_kcontrol_new max98095_left_lineout_mixer_controls[] = {
  491. SOC_DAPM_SINGLE("Left DAC1 Switch", M98095_053_MIX_LINEOUT1, 5, 1, 0),
  492. SOC_DAPM_SINGLE("Right DAC1 Switch", M98095_053_MIX_LINEOUT1, 0, 1, 0),
  493. SOC_DAPM_SINGLE("MIC1 Switch", M98095_053_MIX_LINEOUT1, 3, 1, 0),
  494. SOC_DAPM_SINGLE("MIC2 Switch", M98095_053_MIX_LINEOUT1, 4, 1, 0),
  495. SOC_DAPM_SINGLE("IN1 Switch", M98095_053_MIX_LINEOUT1, 1, 1, 0),
  496. SOC_DAPM_SINGLE("IN2 Switch", M98095_053_MIX_LINEOUT1, 2, 1, 0),
  497. };
  498. /* Right lineout mixer switch */
  499. static const struct snd_kcontrol_new max98095_right_lineout_mixer_controls[] = {
  500. SOC_DAPM_SINGLE("Left DAC1 Switch", M98095_054_MIX_LINEOUT2, 0, 1, 0),
  501. SOC_DAPM_SINGLE("Right DAC1 Switch", M98095_054_MIX_LINEOUT2, 5, 1, 0),
  502. SOC_DAPM_SINGLE("MIC1 Switch", M98095_054_MIX_LINEOUT2, 3, 1, 0),
  503. SOC_DAPM_SINGLE("MIC2 Switch", M98095_054_MIX_LINEOUT2, 4, 1, 0),
  504. SOC_DAPM_SINGLE("IN1 Switch", M98095_054_MIX_LINEOUT2, 1, 1, 0),
  505. SOC_DAPM_SINGLE("IN2 Switch", M98095_054_MIX_LINEOUT2, 2, 1, 0),
  506. };
  507. /* Left ADC mixer switch */
  508. static const struct snd_kcontrol_new max98095_left_ADC_mixer_controls[] = {
  509. SOC_DAPM_SINGLE("MIC1 Switch", M98095_04A_MIX_ADC_LEFT, 7, 1, 0),
  510. SOC_DAPM_SINGLE("MIC2 Switch", M98095_04A_MIX_ADC_LEFT, 6, 1, 0),
  511. SOC_DAPM_SINGLE("IN1 Switch", M98095_04A_MIX_ADC_LEFT, 3, 1, 0),
  512. SOC_DAPM_SINGLE("IN2 Switch", M98095_04A_MIX_ADC_LEFT, 2, 1, 0),
  513. };
  514. /* Right ADC mixer switch */
  515. static const struct snd_kcontrol_new max98095_right_ADC_mixer_controls[] = {
  516. SOC_DAPM_SINGLE("MIC1 Switch", M98095_04B_MIX_ADC_RIGHT, 7, 1, 0),
  517. SOC_DAPM_SINGLE("MIC2 Switch", M98095_04B_MIX_ADC_RIGHT, 6, 1, 0),
  518. SOC_DAPM_SINGLE("IN1 Switch", M98095_04B_MIX_ADC_RIGHT, 3, 1, 0),
  519. SOC_DAPM_SINGLE("IN2 Switch", M98095_04B_MIX_ADC_RIGHT, 2, 1, 0),
  520. };
  521. static int max98095_mic_event(struct snd_soc_dapm_widget *w,
  522. struct snd_kcontrol *kcontrol, int event)
  523. {
  524. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  525. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  526. switch (event) {
  527. case SND_SOC_DAPM_POST_PMU:
  528. if (w->reg == M98095_05F_LVL_MIC1) {
  529. snd_soc_update_bits(codec, w->reg, M98095_MICPRE_MASK,
  530. (1+max98095->mic1pre)<<M98095_MICPRE_SHIFT);
  531. } else {
  532. snd_soc_update_bits(codec, w->reg, M98095_MICPRE_MASK,
  533. (1+max98095->mic2pre)<<M98095_MICPRE_SHIFT);
  534. }
  535. break;
  536. case SND_SOC_DAPM_POST_PMD:
  537. snd_soc_update_bits(codec, w->reg, M98095_MICPRE_MASK, 0);
  538. break;
  539. default:
  540. return -EINVAL;
  541. }
  542. return 0;
  543. }
  544. /*
  545. * The line inputs are stereo inputs with the left and right
  546. * channels sharing a common PGA power control signal.
  547. */
  548. static int max98095_line_pga(struct snd_soc_dapm_widget *w,
  549. int event, u8 channel)
  550. {
  551. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  552. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  553. u8 *state;
  554. if (WARN_ON(!(channel == 1 || channel == 2)))
  555. return -EINVAL;
  556. state = &max98095->lin_state;
  557. switch (event) {
  558. case SND_SOC_DAPM_POST_PMU:
  559. *state |= channel;
  560. snd_soc_update_bits(codec, w->reg,
  561. (1 << w->shift), (1 << w->shift));
  562. break;
  563. case SND_SOC_DAPM_POST_PMD:
  564. *state &= ~channel;
  565. if (*state == 0) {
  566. snd_soc_update_bits(codec, w->reg,
  567. (1 << w->shift), 0);
  568. }
  569. break;
  570. default:
  571. return -EINVAL;
  572. }
  573. return 0;
  574. }
  575. static int max98095_pga_in1_event(struct snd_soc_dapm_widget *w,
  576. struct snd_kcontrol *k, int event)
  577. {
  578. return max98095_line_pga(w, event, 1);
  579. }
  580. static int max98095_pga_in2_event(struct snd_soc_dapm_widget *w,
  581. struct snd_kcontrol *k, int event)
  582. {
  583. return max98095_line_pga(w, event, 2);
  584. }
  585. /*
  586. * The stereo line out mixer outputs to two stereo line outs.
  587. * The 2nd pair has a separate set of enables.
  588. */
  589. static int max98095_lineout_event(struct snd_soc_dapm_widget *w,
  590. struct snd_kcontrol *kcontrol, int event)
  591. {
  592. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  593. switch (event) {
  594. case SND_SOC_DAPM_POST_PMU:
  595. snd_soc_update_bits(codec, w->reg,
  596. (1 << (w->shift+2)), (1 << (w->shift+2)));
  597. break;
  598. case SND_SOC_DAPM_POST_PMD:
  599. snd_soc_update_bits(codec, w->reg,
  600. (1 << (w->shift+2)), 0);
  601. break;
  602. default:
  603. return -EINVAL;
  604. }
  605. return 0;
  606. }
  607. static const struct snd_soc_dapm_widget max98095_dapm_widgets[] = {
  608. SND_SOC_DAPM_ADC("ADCL", "HiFi Capture", M98095_090_PWR_EN_IN, 0, 0),
  609. SND_SOC_DAPM_ADC("ADCR", "HiFi Capture", M98095_090_PWR_EN_IN, 1, 0),
  610. SND_SOC_DAPM_DAC("DACL1", "HiFi Playback",
  611. M98095_091_PWR_EN_OUT, 0, 0),
  612. SND_SOC_DAPM_DAC("DACR1", "HiFi Playback",
  613. M98095_091_PWR_EN_OUT, 1, 0),
  614. SND_SOC_DAPM_DAC("DACM2", "Aux Playback",
  615. M98095_091_PWR_EN_OUT, 2, 0),
  616. SND_SOC_DAPM_DAC("DACM3", "Voice Playback",
  617. M98095_091_PWR_EN_OUT, 2, 0),
  618. SND_SOC_DAPM_PGA("HP Left Out", M98095_091_PWR_EN_OUT,
  619. 6, 0, NULL, 0),
  620. SND_SOC_DAPM_PGA("HP Right Out", M98095_091_PWR_EN_OUT,
  621. 7, 0, NULL, 0),
  622. SND_SOC_DAPM_PGA("SPK Left Out", M98095_091_PWR_EN_OUT,
  623. 4, 0, NULL, 0),
  624. SND_SOC_DAPM_PGA("SPK Right Out", M98095_091_PWR_EN_OUT,
  625. 5, 0, NULL, 0),
  626. SND_SOC_DAPM_PGA("RCV Mono Out", M98095_091_PWR_EN_OUT,
  627. 3, 0, NULL, 0),
  628. SND_SOC_DAPM_PGA_E("LINE Left Out", M98095_092_PWR_EN_OUT,
  629. 0, 0, NULL, 0, max98095_lineout_event, SND_SOC_DAPM_PRE_PMD),
  630. SND_SOC_DAPM_PGA_E("LINE Right Out", M98095_092_PWR_EN_OUT,
  631. 1, 0, NULL, 0, max98095_lineout_event, SND_SOC_DAPM_PRE_PMD),
  632. SND_SOC_DAPM_MUX("External MIC", SND_SOC_NOPM, 0, 0,
  633. &max98095_extmic_mux),
  634. SND_SOC_DAPM_MUX("Linein Mux", SND_SOC_NOPM, 0, 0,
  635. &max98095_linein_mux),
  636. SND_SOC_DAPM_MIXER("Left Headphone Mixer", SND_SOC_NOPM, 0, 0,
  637. &max98095_left_hp_mixer_controls[0],
  638. ARRAY_SIZE(max98095_left_hp_mixer_controls)),
  639. SND_SOC_DAPM_MIXER("Right Headphone Mixer", SND_SOC_NOPM, 0, 0,
  640. &max98095_right_hp_mixer_controls[0],
  641. ARRAY_SIZE(max98095_right_hp_mixer_controls)),
  642. SND_SOC_DAPM_MIXER("Left Speaker Mixer", SND_SOC_NOPM, 0, 0,
  643. &max98095_left_speaker_mixer_controls[0],
  644. ARRAY_SIZE(max98095_left_speaker_mixer_controls)),
  645. SND_SOC_DAPM_MIXER("Right Speaker Mixer", SND_SOC_NOPM, 0, 0,
  646. &max98095_right_speaker_mixer_controls[0],
  647. ARRAY_SIZE(max98095_right_speaker_mixer_controls)),
  648. SND_SOC_DAPM_MIXER("Receiver Mixer", SND_SOC_NOPM, 0, 0,
  649. &max98095_mono_rcv_mixer_controls[0],
  650. ARRAY_SIZE(max98095_mono_rcv_mixer_controls)),
  651. SND_SOC_DAPM_MIXER("Left Lineout Mixer", SND_SOC_NOPM, 0, 0,
  652. &max98095_left_lineout_mixer_controls[0],
  653. ARRAY_SIZE(max98095_left_lineout_mixer_controls)),
  654. SND_SOC_DAPM_MIXER("Right Lineout Mixer", SND_SOC_NOPM, 0, 0,
  655. &max98095_right_lineout_mixer_controls[0],
  656. ARRAY_SIZE(max98095_right_lineout_mixer_controls)),
  657. SND_SOC_DAPM_MIXER("Left ADC Mixer", SND_SOC_NOPM, 0, 0,
  658. &max98095_left_ADC_mixer_controls[0],
  659. ARRAY_SIZE(max98095_left_ADC_mixer_controls)),
  660. SND_SOC_DAPM_MIXER("Right ADC Mixer", SND_SOC_NOPM, 0, 0,
  661. &max98095_right_ADC_mixer_controls[0],
  662. ARRAY_SIZE(max98095_right_ADC_mixer_controls)),
  663. SND_SOC_DAPM_PGA_E("MIC1 Input", M98095_05F_LVL_MIC1,
  664. 5, 0, NULL, 0, max98095_mic_event,
  665. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  666. SND_SOC_DAPM_PGA_E("MIC2 Input", M98095_060_LVL_MIC2,
  667. 5, 0, NULL, 0, max98095_mic_event,
  668. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  669. SND_SOC_DAPM_PGA_E("IN1 Input", M98095_090_PWR_EN_IN,
  670. 7, 0, NULL, 0, max98095_pga_in1_event,
  671. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  672. SND_SOC_DAPM_PGA_E("IN2 Input", M98095_090_PWR_EN_IN,
  673. 7, 0, NULL, 0, max98095_pga_in2_event,
  674. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  675. SND_SOC_DAPM_MICBIAS("MICBIAS1", M98095_090_PWR_EN_IN, 2, 0),
  676. SND_SOC_DAPM_MICBIAS("MICBIAS2", M98095_090_PWR_EN_IN, 3, 0),
  677. SND_SOC_DAPM_OUTPUT("HPL"),
  678. SND_SOC_DAPM_OUTPUT("HPR"),
  679. SND_SOC_DAPM_OUTPUT("SPKL"),
  680. SND_SOC_DAPM_OUTPUT("SPKR"),
  681. SND_SOC_DAPM_OUTPUT("RCV"),
  682. SND_SOC_DAPM_OUTPUT("OUT1"),
  683. SND_SOC_DAPM_OUTPUT("OUT2"),
  684. SND_SOC_DAPM_OUTPUT("OUT3"),
  685. SND_SOC_DAPM_OUTPUT("OUT4"),
  686. SND_SOC_DAPM_INPUT("MIC1"),
  687. SND_SOC_DAPM_INPUT("MIC2"),
  688. SND_SOC_DAPM_INPUT("INA1"),
  689. SND_SOC_DAPM_INPUT("INA2"),
  690. SND_SOC_DAPM_INPUT("INB1"),
  691. SND_SOC_DAPM_INPUT("INB2"),
  692. };
  693. static const struct snd_soc_dapm_route max98095_audio_map[] = {
  694. /* Left headphone output mixer */
  695. {"Left Headphone Mixer", "Left DAC1 Switch", "DACL1"},
  696. {"Left Headphone Mixer", "Right DAC1 Switch", "DACR1"},
  697. {"Left Headphone Mixer", "MIC1 Switch", "MIC1 Input"},
  698. {"Left Headphone Mixer", "MIC2 Switch", "MIC2 Input"},
  699. {"Left Headphone Mixer", "IN1 Switch", "IN1 Input"},
  700. {"Left Headphone Mixer", "IN2 Switch", "IN2 Input"},
  701. /* Right headphone output mixer */
  702. {"Right Headphone Mixer", "Left DAC1 Switch", "DACL1"},
  703. {"Right Headphone Mixer", "Right DAC1 Switch", "DACR1"},
  704. {"Right Headphone Mixer", "MIC1 Switch", "MIC1 Input"},
  705. {"Right Headphone Mixer", "MIC2 Switch", "MIC2 Input"},
  706. {"Right Headphone Mixer", "IN1 Switch", "IN1 Input"},
  707. {"Right Headphone Mixer", "IN2 Switch", "IN2 Input"},
  708. /* Left speaker output mixer */
  709. {"Left Speaker Mixer", "Left DAC1 Switch", "DACL1"},
  710. {"Left Speaker Mixer", "Right DAC1 Switch", "DACR1"},
  711. {"Left Speaker Mixer", "Mono DAC2 Switch", "DACM2"},
  712. {"Left Speaker Mixer", "Mono DAC3 Switch", "DACM3"},
  713. {"Left Speaker Mixer", "MIC1 Switch", "MIC1 Input"},
  714. {"Left Speaker Mixer", "MIC2 Switch", "MIC2 Input"},
  715. {"Left Speaker Mixer", "IN1 Switch", "IN1 Input"},
  716. {"Left Speaker Mixer", "IN2 Switch", "IN2 Input"},
  717. /* Right speaker output mixer */
  718. {"Right Speaker Mixer", "Left DAC1 Switch", "DACL1"},
  719. {"Right Speaker Mixer", "Right DAC1 Switch", "DACR1"},
  720. {"Right Speaker Mixer", "Mono DAC2 Switch", "DACM2"},
  721. {"Right Speaker Mixer", "Mono DAC3 Switch", "DACM3"},
  722. {"Right Speaker Mixer", "MIC1 Switch", "MIC1 Input"},
  723. {"Right Speaker Mixer", "MIC2 Switch", "MIC2 Input"},
  724. {"Right Speaker Mixer", "IN1 Switch", "IN1 Input"},
  725. {"Right Speaker Mixer", "IN2 Switch", "IN2 Input"},
  726. /* Earpiece/Receiver output mixer */
  727. {"Receiver Mixer", "Left DAC1 Switch", "DACL1"},
  728. {"Receiver Mixer", "Right DAC1 Switch", "DACR1"},
  729. {"Receiver Mixer", "MIC1 Switch", "MIC1 Input"},
  730. {"Receiver Mixer", "MIC2 Switch", "MIC2 Input"},
  731. {"Receiver Mixer", "IN1 Switch", "IN1 Input"},
  732. {"Receiver Mixer", "IN2 Switch", "IN2 Input"},
  733. /* Left Lineout output mixer */
  734. {"Left Lineout Mixer", "Left DAC1 Switch", "DACL1"},
  735. {"Left Lineout Mixer", "Right DAC1 Switch", "DACR1"},
  736. {"Left Lineout Mixer", "MIC1 Switch", "MIC1 Input"},
  737. {"Left Lineout Mixer", "MIC2 Switch", "MIC2 Input"},
  738. {"Left Lineout Mixer", "IN1 Switch", "IN1 Input"},
  739. {"Left Lineout Mixer", "IN2 Switch", "IN2 Input"},
  740. /* Right lineout output mixer */
  741. {"Right Lineout Mixer", "Left DAC1 Switch", "DACL1"},
  742. {"Right Lineout Mixer", "Right DAC1 Switch", "DACR1"},
  743. {"Right Lineout Mixer", "MIC1 Switch", "MIC1 Input"},
  744. {"Right Lineout Mixer", "MIC2 Switch", "MIC2 Input"},
  745. {"Right Lineout Mixer", "IN1 Switch", "IN1 Input"},
  746. {"Right Lineout Mixer", "IN2 Switch", "IN2 Input"},
  747. {"HP Left Out", NULL, "Left Headphone Mixer"},
  748. {"HP Right Out", NULL, "Right Headphone Mixer"},
  749. {"SPK Left Out", NULL, "Left Speaker Mixer"},
  750. {"SPK Right Out", NULL, "Right Speaker Mixer"},
  751. {"RCV Mono Out", NULL, "Receiver Mixer"},
  752. {"LINE Left Out", NULL, "Left Lineout Mixer"},
  753. {"LINE Right Out", NULL, "Right Lineout Mixer"},
  754. {"HPL", NULL, "HP Left Out"},
  755. {"HPR", NULL, "HP Right Out"},
  756. {"SPKL", NULL, "SPK Left Out"},
  757. {"SPKR", NULL, "SPK Right Out"},
  758. {"RCV", NULL, "RCV Mono Out"},
  759. {"OUT1", NULL, "LINE Left Out"},
  760. {"OUT2", NULL, "LINE Right Out"},
  761. {"OUT3", NULL, "LINE Left Out"},
  762. {"OUT4", NULL, "LINE Right Out"},
  763. /* Left ADC input mixer */
  764. {"Left ADC Mixer", "MIC1 Switch", "MIC1 Input"},
  765. {"Left ADC Mixer", "MIC2 Switch", "MIC2 Input"},
  766. {"Left ADC Mixer", "IN1 Switch", "IN1 Input"},
  767. {"Left ADC Mixer", "IN2 Switch", "IN2 Input"},
  768. /* Right ADC input mixer */
  769. {"Right ADC Mixer", "MIC1 Switch", "MIC1 Input"},
  770. {"Right ADC Mixer", "MIC2 Switch", "MIC2 Input"},
  771. {"Right ADC Mixer", "IN1 Switch", "IN1 Input"},
  772. {"Right ADC Mixer", "IN2 Switch", "IN2 Input"},
  773. /* Inputs */
  774. {"ADCL", NULL, "Left ADC Mixer"},
  775. {"ADCR", NULL, "Right ADC Mixer"},
  776. {"IN1 Input", NULL, "INA1"},
  777. {"IN2 Input", NULL, "INA2"},
  778. {"MIC1 Input", NULL, "MIC1"},
  779. {"MIC2 Input", NULL, "MIC2"},
  780. };
  781. /* codec mclk clock divider coefficients */
  782. static const struct {
  783. u32 rate;
  784. u8 sr;
  785. } rate_table[] = {
  786. {8000, 0x01},
  787. {11025, 0x02},
  788. {16000, 0x03},
  789. {22050, 0x04},
  790. {24000, 0x05},
  791. {32000, 0x06},
  792. {44100, 0x07},
  793. {48000, 0x08},
  794. {88200, 0x09},
  795. {96000, 0x0A},
  796. };
  797. static int rate_value(int rate, u8 *value)
  798. {
  799. int i;
  800. for (i = 0; i < ARRAY_SIZE(rate_table); i++) {
  801. if (rate_table[i].rate >= rate) {
  802. *value = rate_table[i].sr;
  803. return 0;
  804. }
  805. }
  806. *value = rate_table[0].sr;
  807. return -EINVAL;
  808. }
  809. static int max98095_dai1_hw_params(struct snd_pcm_substream *substream,
  810. struct snd_pcm_hw_params *params,
  811. struct snd_soc_dai *dai)
  812. {
  813. struct snd_soc_codec *codec = dai->codec;
  814. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  815. struct max98095_cdata *cdata;
  816. unsigned long long ni;
  817. unsigned int rate;
  818. u8 regval;
  819. cdata = &max98095->dai[0];
  820. rate = params_rate(params);
  821. switch (params_width(params)) {
  822. case 16:
  823. snd_soc_update_bits(codec, M98095_02A_DAI1_FORMAT,
  824. M98095_DAI_WS, 0);
  825. break;
  826. case 24:
  827. snd_soc_update_bits(codec, M98095_02A_DAI1_FORMAT,
  828. M98095_DAI_WS, M98095_DAI_WS);
  829. break;
  830. default:
  831. return -EINVAL;
  832. }
  833. if (rate_value(rate, &regval))
  834. return -EINVAL;
  835. snd_soc_update_bits(codec, M98095_027_DAI1_CLKMODE,
  836. M98095_CLKMODE_MASK, regval);
  837. cdata->rate = rate;
  838. /* Configure NI when operating as master */
  839. if (snd_soc_read(codec, M98095_02A_DAI1_FORMAT) & M98095_DAI_MAS) {
  840. if (max98095->sysclk == 0) {
  841. dev_err(codec->dev, "Invalid system clock frequency\n");
  842. return -EINVAL;
  843. }
  844. ni = 65536ULL * (rate < 50000 ? 96ULL : 48ULL)
  845. * (unsigned long long int)rate;
  846. do_div(ni, (unsigned long long int)max98095->sysclk);
  847. snd_soc_write(codec, M98095_028_DAI1_CLKCFG_HI,
  848. (ni >> 8) & 0x7F);
  849. snd_soc_write(codec, M98095_029_DAI1_CLKCFG_LO,
  850. ni & 0xFF);
  851. }
  852. /* Update sample rate mode */
  853. if (rate < 50000)
  854. snd_soc_update_bits(codec, M98095_02E_DAI1_FILTERS,
  855. M98095_DAI_DHF, 0);
  856. else
  857. snd_soc_update_bits(codec, M98095_02E_DAI1_FILTERS,
  858. M98095_DAI_DHF, M98095_DAI_DHF);
  859. return 0;
  860. }
  861. static int max98095_dai2_hw_params(struct snd_pcm_substream *substream,
  862. struct snd_pcm_hw_params *params,
  863. struct snd_soc_dai *dai)
  864. {
  865. struct snd_soc_codec *codec = dai->codec;
  866. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  867. struct max98095_cdata *cdata;
  868. unsigned long long ni;
  869. unsigned int rate;
  870. u8 regval;
  871. cdata = &max98095->dai[1];
  872. rate = params_rate(params);
  873. switch (params_width(params)) {
  874. case 16:
  875. snd_soc_update_bits(codec, M98095_034_DAI2_FORMAT,
  876. M98095_DAI_WS, 0);
  877. break;
  878. case 24:
  879. snd_soc_update_bits(codec, M98095_034_DAI2_FORMAT,
  880. M98095_DAI_WS, M98095_DAI_WS);
  881. break;
  882. default:
  883. return -EINVAL;
  884. }
  885. if (rate_value(rate, &regval))
  886. return -EINVAL;
  887. snd_soc_update_bits(codec, M98095_031_DAI2_CLKMODE,
  888. M98095_CLKMODE_MASK, regval);
  889. cdata->rate = rate;
  890. /* Configure NI when operating as master */
  891. if (snd_soc_read(codec, M98095_034_DAI2_FORMAT) & M98095_DAI_MAS) {
  892. if (max98095->sysclk == 0) {
  893. dev_err(codec->dev, "Invalid system clock frequency\n");
  894. return -EINVAL;
  895. }
  896. ni = 65536ULL * (rate < 50000 ? 96ULL : 48ULL)
  897. * (unsigned long long int)rate;
  898. do_div(ni, (unsigned long long int)max98095->sysclk);
  899. snd_soc_write(codec, M98095_032_DAI2_CLKCFG_HI,
  900. (ni >> 8) & 0x7F);
  901. snd_soc_write(codec, M98095_033_DAI2_CLKCFG_LO,
  902. ni & 0xFF);
  903. }
  904. /* Update sample rate mode */
  905. if (rate < 50000)
  906. snd_soc_update_bits(codec, M98095_038_DAI2_FILTERS,
  907. M98095_DAI_DHF, 0);
  908. else
  909. snd_soc_update_bits(codec, M98095_038_DAI2_FILTERS,
  910. M98095_DAI_DHF, M98095_DAI_DHF);
  911. return 0;
  912. }
  913. static int max98095_dai3_hw_params(struct snd_pcm_substream *substream,
  914. struct snd_pcm_hw_params *params,
  915. struct snd_soc_dai *dai)
  916. {
  917. struct snd_soc_codec *codec = dai->codec;
  918. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  919. struct max98095_cdata *cdata;
  920. unsigned long long ni;
  921. unsigned int rate;
  922. u8 regval;
  923. cdata = &max98095->dai[2];
  924. rate = params_rate(params);
  925. switch (params_width(params)) {
  926. case 16:
  927. snd_soc_update_bits(codec, M98095_03E_DAI3_FORMAT,
  928. M98095_DAI_WS, 0);
  929. break;
  930. case 24:
  931. snd_soc_update_bits(codec, M98095_03E_DAI3_FORMAT,
  932. M98095_DAI_WS, M98095_DAI_WS);
  933. break;
  934. default:
  935. return -EINVAL;
  936. }
  937. if (rate_value(rate, &regval))
  938. return -EINVAL;
  939. snd_soc_update_bits(codec, M98095_03B_DAI3_CLKMODE,
  940. M98095_CLKMODE_MASK, regval);
  941. cdata->rate = rate;
  942. /* Configure NI when operating as master */
  943. if (snd_soc_read(codec, M98095_03E_DAI3_FORMAT) & M98095_DAI_MAS) {
  944. if (max98095->sysclk == 0) {
  945. dev_err(codec->dev, "Invalid system clock frequency\n");
  946. return -EINVAL;
  947. }
  948. ni = 65536ULL * (rate < 50000 ? 96ULL : 48ULL)
  949. * (unsigned long long int)rate;
  950. do_div(ni, (unsigned long long int)max98095->sysclk);
  951. snd_soc_write(codec, M98095_03C_DAI3_CLKCFG_HI,
  952. (ni >> 8) & 0x7F);
  953. snd_soc_write(codec, M98095_03D_DAI3_CLKCFG_LO,
  954. ni & 0xFF);
  955. }
  956. /* Update sample rate mode */
  957. if (rate < 50000)
  958. snd_soc_update_bits(codec, M98095_042_DAI3_FILTERS,
  959. M98095_DAI_DHF, 0);
  960. else
  961. snd_soc_update_bits(codec, M98095_042_DAI3_FILTERS,
  962. M98095_DAI_DHF, M98095_DAI_DHF);
  963. return 0;
  964. }
  965. static int max98095_dai_set_sysclk(struct snd_soc_dai *dai,
  966. int clk_id, unsigned int freq, int dir)
  967. {
  968. struct snd_soc_codec *codec = dai->codec;
  969. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  970. /* Requested clock frequency is already setup */
  971. if (freq == max98095->sysclk)
  972. return 0;
  973. if (!IS_ERR(max98095->mclk)) {
  974. freq = clk_round_rate(max98095->mclk, freq);
  975. clk_set_rate(max98095->mclk, freq);
  976. }
  977. /* Setup clocks for slave mode, and using the PLL
  978. * PSCLK = 0x01 (when master clk is 10MHz to 20MHz)
  979. * 0x02 (when master clk is 20MHz to 40MHz)..
  980. * 0x03 (when master clk is 40MHz to 60MHz)..
  981. */
  982. if ((freq >= 10000000) && (freq < 20000000)) {
  983. snd_soc_write(codec, M98095_026_SYS_CLK, 0x10);
  984. } else if ((freq >= 20000000) && (freq < 40000000)) {
  985. snd_soc_write(codec, M98095_026_SYS_CLK, 0x20);
  986. } else if ((freq >= 40000000) && (freq < 60000000)) {
  987. snd_soc_write(codec, M98095_026_SYS_CLK, 0x30);
  988. } else {
  989. dev_err(codec->dev, "Invalid master clock frequency\n");
  990. return -EINVAL;
  991. }
  992. dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
  993. max98095->sysclk = freq;
  994. return 0;
  995. }
  996. static int max98095_dai1_set_fmt(struct snd_soc_dai *codec_dai,
  997. unsigned int fmt)
  998. {
  999. struct snd_soc_codec *codec = codec_dai->codec;
  1000. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1001. struct max98095_cdata *cdata;
  1002. u8 regval = 0;
  1003. cdata = &max98095->dai[0];
  1004. if (fmt != cdata->fmt) {
  1005. cdata->fmt = fmt;
  1006. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1007. case SND_SOC_DAIFMT_CBS_CFS:
  1008. /* Slave mode PLL */
  1009. snd_soc_write(codec, M98095_028_DAI1_CLKCFG_HI,
  1010. 0x80);
  1011. snd_soc_write(codec, M98095_029_DAI1_CLKCFG_LO,
  1012. 0x00);
  1013. break;
  1014. case SND_SOC_DAIFMT_CBM_CFM:
  1015. /* Set to master mode */
  1016. regval |= M98095_DAI_MAS;
  1017. break;
  1018. case SND_SOC_DAIFMT_CBS_CFM:
  1019. case SND_SOC_DAIFMT_CBM_CFS:
  1020. default:
  1021. dev_err(codec->dev, "Clock mode unsupported");
  1022. return -EINVAL;
  1023. }
  1024. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1025. case SND_SOC_DAIFMT_I2S:
  1026. regval |= M98095_DAI_DLY;
  1027. break;
  1028. case SND_SOC_DAIFMT_LEFT_J:
  1029. break;
  1030. default:
  1031. return -EINVAL;
  1032. }
  1033. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1034. case SND_SOC_DAIFMT_NB_NF:
  1035. break;
  1036. case SND_SOC_DAIFMT_NB_IF:
  1037. regval |= M98095_DAI_WCI;
  1038. break;
  1039. case SND_SOC_DAIFMT_IB_NF:
  1040. regval |= M98095_DAI_BCI;
  1041. break;
  1042. case SND_SOC_DAIFMT_IB_IF:
  1043. regval |= M98095_DAI_BCI|M98095_DAI_WCI;
  1044. break;
  1045. default:
  1046. return -EINVAL;
  1047. }
  1048. snd_soc_update_bits(codec, M98095_02A_DAI1_FORMAT,
  1049. M98095_DAI_MAS | M98095_DAI_DLY | M98095_DAI_BCI |
  1050. M98095_DAI_WCI, regval);
  1051. snd_soc_write(codec, M98095_02B_DAI1_CLOCK, M98095_DAI_BSEL64);
  1052. }
  1053. return 0;
  1054. }
  1055. static int max98095_dai2_set_fmt(struct snd_soc_dai *codec_dai,
  1056. unsigned int fmt)
  1057. {
  1058. struct snd_soc_codec *codec = codec_dai->codec;
  1059. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1060. struct max98095_cdata *cdata;
  1061. u8 regval = 0;
  1062. cdata = &max98095->dai[1];
  1063. if (fmt != cdata->fmt) {
  1064. cdata->fmt = fmt;
  1065. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1066. case SND_SOC_DAIFMT_CBS_CFS:
  1067. /* Slave mode PLL */
  1068. snd_soc_write(codec, M98095_032_DAI2_CLKCFG_HI,
  1069. 0x80);
  1070. snd_soc_write(codec, M98095_033_DAI2_CLKCFG_LO,
  1071. 0x00);
  1072. break;
  1073. case SND_SOC_DAIFMT_CBM_CFM:
  1074. /* Set to master mode */
  1075. regval |= M98095_DAI_MAS;
  1076. break;
  1077. case SND_SOC_DAIFMT_CBS_CFM:
  1078. case SND_SOC_DAIFMT_CBM_CFS:
  1079. default:
  1080. dev_err(codec->dev, "Clock mode unsupported");
  1081. return -EINVAL;
  1082. }
  1083. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1084. case SND_SOC_DAIFMT_I2S:
  1085. regval |= M98095_DAI_DLY;
  1086. break;
  1087. case SND_SOC_DAIFMT_LEFT_J:
  1088. break;
  1089. default:
  1090. return -EINVAL;
  1091. }
  1092. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1093. case SND_SOC_DAIFMT_NB_NF:
  1094. break;
  1095. case SND_SOC_DAIFMT_NB_IF:
  1096. regval |= M98095_DAI_WCI;
  1097. break;
  1098. case SND_SOC_DAIFMT_IB_NF:
  1099. regval |= M98095_DAI_BCI;
  1100. break;
  1101. case SND_SOC_DAIFMT_IB_IF:
  1102. regval |= M98095_DAI_BCI|M98095_DAI_WCI;
  1103. break;
  1104. default:
  1105. return -EINVAL;
  1106. }
  1107. snd_soc_update_bits(codec, M98095_034_DAI2_FORMAT,
  1108. M98095_DAI_MAS | M98095_DAI_DLY | M98095_DAI_BCI |
  1109. M98095_DAI_WCI, regval);
  1110. snd_soc_write(codec, M98095_035_DAI2_CLOCK,
  1111. M98095_DAI_BSEL64);
  1112. }
  1113. return 0;
  1114. }
  1115. static int max98095_dai3_set_fmt(struct snd_soc_dai *codec_dai,
  1116. unsigned int fmt)
  1117. {
  1118. struct snd_soc_codec *codec = codec_dai->codec;
  1119. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1120. struct max98095_cdata *cdata;
  1121. u8 regval = 0;
  1122. cdata = &max98095->dai[2];
  1123. if (fmt != cdata->fmt) {
  1124. cdata->fmt = fmt;
  1125. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1126. case SND_SOC_DAIFMT_CBS_CFS:
  1127. /* Slave mode PLL */
  1128. snd_soc_write(codec, M98095_03C_DAI3_CLKCFG_HI,
  1129. 0x80);
  1130. snd_soc_write(codec, M98095_03D_DAI3_CLKCFG_LO,
  1131. 0x00);
  1132. break;
  1133. case SND_SOC_DAIFMT_CBM_CFM:
  1134. /* Set to master mode */
  1135. regval |= M98095_DAI_MAS;
  1136. break;
  1137. case SND_SOC_DAIFMT_CBS_CFM:
  1138. case SND_SOC_DAIFMT_CBM_CFS:
  1139. default:
  1140. dev_err(codec->dev, "Clock mode unsupported");
  1141. return -EINVAL;
  1142. }
  1143. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1144. case SND_SOC_DAIFMT_I2S:
  1145. regval |= M98095_DAI_DLY;
  1146. break;
  1147. case SND_SOC_DAIFMT_LEFT_J:
  1148. break;
  1149. default:
  1150. return -EINVAL;
  1151. }
  1152. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1153. case SND_SOC_DAIFMT_NB_NF:
  1154. break;
  1155. case SND_SOC_DAIFMT_NB_IF:
  1156. regval |= M98095_DAI_WCI;
  1157. break;
  1158. case SND_SOC_DAIFMT_IB_NF:
  1159. regval |= M98095_DAI_BCI;
  1160. break;
  1161. case SND_SOC_DAIFMT_IB_IF:
  1162. regval |= M98095_DAI_BCI|M98095_DAI_WCI;
  1163. break;
  1164. default:
  1165. return -EINVAL;
  1166. }
  1167. snd_soc_update_bits(codec, M98095_03E_DAI3_FORMAT,
  1168. M98095_DAI_MAS | M98095_DAI_DLY | M98095_DAI_BCI |
  1169. M98095_DAI_WCI, regval);
  1170. snd_soc_write(codec, M98095_03F_DAI3_CLOCK,
  1171. M98095_DAI_BSEL64);
  1172. }
  1173. return 0;
  1174. }
  1175. static int max98095_set_bias_level(struct snd_soc_codec *codec,
  1176. enum snd_soc_bias_level level)
  1177. {
  1178. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1179. int ret;
  1180. switch (level) {
  1181. case SND_SOC_BIAS_ON:
  1182. break;
  1183. case SND_SOC_BIAS_PREPARE:
  1184. /*
  1185. * SND_SOC_BIAS_PREPARE is called while preparing for a
  1186. * transition to ON or away from ON. If current bias_level
  1187. * is SND_SOC_BIAS_ON, then it is preparing for a transition
  1188. * away from ON. Disable the clock in that case, otherwise
  1189. * enable it.
  1190. */
  1191. if (IS_ERR(max98095->mclk))
  1192. break;
  1193. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_ON) {
  1194. clk_disable_unprepare(max98095->mclk);
  1195. } else {
  1196. ret = clk_prepare_enable(max98095->mclk);
  1197. if (ret)
  1198. return ret;
  1199. }
  1200. break;
  1201. case SND_SOC_BIAS_STANDBY:
  1202. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  1203. ret = regcache_sync(max98095->regmap);
  1204. if (ret != 0) {
  1205. dev_err(codec->dev, "Failed to sync cache: %d\n", ret);
  1206. return ret;
  1207. }
  1208. }
  1209. snd_soc_update_bits(codec, M98095_090_PWR_EN_IN,
  1210. M98095_MBEN, M98095_MBEN);
  1211. break;
  1212. case SND_SOC_BIAS_OFF:
  1213. snd_soc_update_bits(codec, M98095_090_PWR_EN_IN,
  1214. M98095_MBEN, 0);
  1215. regcache_mark_dirty(max98095->regmap);
  1216. break;
  1217. }
  1218. return 0;
  1219. }
  1220. #define MAX98095_RATES SNDRV_PCM_RATE_8000_96000
  1221. #define MAX98095_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
  1222. static const struct snd_soc_dai_ops max98095_dai1_ops = {
  1223. .set_sysclk = max98095_dai_set_sysclk,
  1224. .set_fmt = max98095_dai1_set_fmt,
  1225. .hw_params = max98095_dai1_hw_params,
  1226. };
  1227. static const struct snd_soc_dai_ops max98095_dai2_ops = {
  1228. .set_sysclk = max98095_dai_set_sysclk,
  1229. .set_fmt = max98095_dai2_set_fmt,
  1230. .hw_params = max98095_dai2_hw_params,
  1231. };
  1232. static const struct snd_soc_dai_ops max98095_dai3_ops = {
  1233. .set_sysclk = max98095_dai_set_sysclk,
  1234. .set_fmt = max98095_dai3_set_fmt,
  1235. .hw_params = max98095_dai3_hw_params,
  1236. };
  1237. static struct snd_soc_dai_driver max98095_dai[] = {
  1238. {
  1239. .name = "HiFi",
  1240. .playback = {
  1241. .stream_name = "HiFi Playback",
  1242. .channels_min = 1,
  1243. .channels_max = 2,
  1244. .rates = MAX98095_RATES,
  1245. .formats = MAX98095_FORMATS,
  1246. },
  1247. .capture = {
  1248. .stream_name = "HiFi Capture",
  1249. .channels_min = 1,
  1250. .channels_max = 2,
  1251. .rates = MAX98095_RATES,
  1252. .formats = MAX98095_FORMATS,
  1253. },
  1254. .ops = &max98095_dai1_ops,
  1255. },
  1256. {
  1257. .name = "Aux",
  1258. .playback = {
  1259. .stream_name = "Aux Playback",
  1260. .channels_min = 1,
  1261. .channels_max = 1,
  1262. .rates = MAX98095_RATES,
  1263. .formats = MAX98095_FORMATS,
  1264. },
  1265. .ops = &max98095_dai2_ops,
  1266. },
  1267. {
  1268. .name = "Voice",
  1269. .playback = {
  1270. .stream_name = "Voice Playback",
  1271. .channels_min = 1,
  1272. .channels_max = 1,
  1273. .rates = MAX98095_RATES,
  1274. .formats = MAX98095_FORMATS,
  1275. },
  1276. .ops = &max98095_dai3_ops,
  1277. }
  1278. };
  1279. static int max98095_get_eq_channel(const char *name)
  1280. {
  1281. if (strcmp(name, "EQ1 Mode") == 0)
  1282. return 0;
  1283. if (strcmp(name, "EQ2 Mode") == 0)
  1284. return 1;
  1285. return -EINVAL;
  1286. }
  1287. static int max98095_put_eq_enum(struct snd_kcontrol *kcontrol,
  1288. struct snd_ctl_elem_value *ucontrol)
  1289. {
  1290. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1291. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1292. struct max98095_pdata *pdata = max98095->pdata;
  1293. int channel = max98095_get_eq_channel(kcontrol->id.name);
  1294. struct max98095_cdata *cdata;
  1295. unsigned int sel = ucontrol->value.enumerated.item[0];
  1296. struct max98095_eq_cfg *coef_set;
  1297. int fs, best, best_val, i;
  1298. int regmask, regsave;
  1299. if (WARN_ON(channel > 1))
  1300. return -EINVAL;
  1301. if (!pdata || !max98095->eq_textcnt)
  1302. return 0;
  1303. if (sel >= pdata->eq_cfgcnt)
  1304. return -EINVAL;
  1305. cdata = &max98095->dai[channel];
  1306. cdata->eq_sel = sel;
  1307. fs = cdata->rate;
  1308. /* Find the selected configuration with nearest sample rate */
  1309. best = 0;
  1310. best_val = INT_MAX;
  1311. for (i = 0; i < pdata->eq_cfgcnt; i++) {
  1312. if (strcmp(pdata->eq_cfg[i].name, max98095->eq_texts[sel]) == 0 &&
  1313. abs(pdata->eq_cfg[i].rate - fs) < best_val) {
  1314. best = i;
  1315. best_val = abs(pdata->eq_cfg[i].rate - fs);
  1316. }
  1317. }
  1318. dev_dbg(codec->dev, "Selected %s/%dHz for %dHz sample rate\n",
  1319. pdata->eq_cfg[best].name,
  1320. pdata->eq_cfg[best].rate, fs);
  1321. coef_set = &pdata->eq_cfg[best];
  1322. regmask = (channel == 0) ? M98095_EQ1EN : M98095_EQ2EN;
  1323. /* Disable filter while configuring, and save current on/off state */
  1324. regsave = snd_soc_read(codec, M98095_088_CFG_LEVEL);
  1325. snd_soc_update_bits(codec, M98095_088_CFG_LEVEL, regmask, 0);
  1326. mutex_lock(&max98095->lock);
  1327. snd_soc_update_bits(codec, M98095_00F_HOST_CFG, M98095_SEG, M98095_SEG);
  1328. m98095_eq_band(codec, channel, 0, coef_set->band1);
  1329. m98095_eq_band(codec, channel, 1, coef_set->band2);
  1330. m98095_eq_band(codec, channel, 2, coef_set->band3);
  1331. m98095_eq_band(codec, channel, 3, coef_set->band4);
  1332. m98095_eq_band(codec, channel, 4, coef_set->band5);
  1333. snd_soc_update_bits(codec, M98095_00F_HOST_CFG, M98095_SEG, 0);
  1334. mutex_unlock(&max98095->lock);
  1335. /* Restore the original on/off state */
  1336. snd_soc_update_bits(codec, M98095_088_CFG_LEVEL, regmask, regsave);
  1337. return 0;
  1338. }
  1339. static int max98095_get_eq_enum(struct snd_kcontrol *kcontrol,
  1340. struct snd_ctl_elem_value *ucontrol)
  1341. {
  1342. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1343. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1344. int channel = max98095_get_eq_channel(kcontrol->id.name);
  1345. struct max98095_cdata *cdata;
  1346. cdata = &max98095->dai[channel];
  1347. ucontrol->value.enumerated.item[0] = cdata->eq_sel;
  1348. return 0;
  1349. }
  1350. static void max98095_handle_eq_pdata(struct snd_soc_codec *codec)
  1351. {
  1352. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1353. struct max98095_pdata *pdata = max98095->pdata;
  1354. struct max98095_eq_cfg *cfg;
  1355. unsigned int cfgcnt;
  1356. int i, j;
  1357. const char **t;
  1358. int ret;
  1359. struct snd_kcontrol_new controls[] = {
  1360. SOC_ENUM_EXT("EQ1 Mode",
  1361. max98095->eq_enum,
  1362. max98095_get_eq_enum,
  1363. max98095_put_eq_enum),
  1364. SOC_ENUM_EXT("EQ2 Mode",
  1365. max98095->eq_enum,
  1366. max98095_get_eq_enum,
  1367. max98095_put_eq_enum),
  1368. };
  1369. cfg = pdata->eq_cfg;
  1370. cfgcnt = pdata->eq_cfgcnt;
  1371. /* Setup an array of texts for the equalizer enum.
  1372. * This is based on Mark Brown's equalizer driver code.
  1373. */
  1374. max98095->eq_textcnt = 0;
  1375. max98095->eq_texts = NULL;
  1376. for (i = 0; i < cfgcnt; i++) {
  1377. for (j = 0; j < max98095->eq_textcnt; j++) {
  1378. if (strcmp(cfg[i].name, max98095->eq_texts[j]) == 0)
  1379. break;
  1380. }
  1381. if (j != max98095->eq_textcnt)
  1382. continue;
  1383. /* Expand the array */
  1384. t = krealloc(max98095->eq_texts,
  1385. sizeof(char *) * (max98095->eq_textcnt + 1),
  1386. GFP_KERNEL);
  1387. if (t == NULL)
  1388. continue;
  1389. /* Store the new entry */
  1390. t[max98095->eq_textcnt] = cfg[i].name;
  1391. max98095->eq_textcnt++;
  1392. max98095->eq_texts = t;
  1393. }
  1394. /* Now point the soc_enum to .texts array items */
  1395. max98095->eq_enum.texts = max98095->eq_texts;
  1396. max98095->eq_enum.items = max98095->eq_textcnt;
  1397. ret = snd_soc_add_codec_controls(codec, controls, ARRAY_SIZE(controls));
  1398. if (ret != 0)
  1399. dev_err(codec->dev, "Failed to add EQ control: %d\n", ret);
  1400. }
  1401. static const char *bq_mode_name[] = {"Biquad1 Mode", "Biquad2 Mode"};
  1402. static int max98095_get_bq_channel(struct snd_soc_codec *codec,
  1403. const char *name)
  1404. {
  1405. int i;
  1406. for (i = 0; i < ARRAY_SIZE(bq_mode_name); i++)
  1407. if (strcmp(name, bq_mode_name[i]) == 0)
  1408. return i;
  1409. /* Shouldn't happen */
  1410. dev_err(codec->dev, "Bad biquad channel name '%s'\n", name);
  1411. return -EINVAL;
  1412. }
  1413. static int max98095_put_bq_enum(struct snd_kcontrol *kcontrol,
  1414. struct snd_ctl_elem_value *ucontrol)
  1415. {
  1416. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1417. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1418. struct max98095_pdata *pdata = max98095->pdata;
  1419. int channel = max98095_get_bq_channel(codec, kcontrol->id.name);
  1420. struct max98095_cdata *cdata;
  1421. unsigned int sel = ucontrol->value.enumerated.item[0];
  1422. struct max98095_biquad_cfg *coef_set;
  1423. int fs, best, best_val, i;
  1424. int regmask, regsave;
  1425. if (channel < 0)
  1426. return channel;
  1427. if (!pdata || !max98095->bq_textcnt)
  1428. return 0;
  1429. if (sel >= pdata->bq_cfgcnt)
  1430. return -EINVAL;
  1431. cdata = &max98095->dai[channel];
  1432. cdata->bq_sel = sel;
  1433. fs = cdata->rate;
  1434. /* Find the selected configuration with nearest sample rate */
  1435. best = 0;
  1436. best_val = INT_MAX;
  1437. for (i = 0; i < pdata->bq_cfgcnt; i++) {
  1438. if (strcmp(pdata->bq_cfg[i].name, max98095->bq_texts[sel]) == 0 &&
  1439. abs(pdata->bq_cfg[i].rate - fs) < best_val) {
  1440. best = i;
  1441. best_val = abs(pdata->bq_cfg[i].rate - fs);
  1442. }
  1443. }
  1444. dev_dbg(codec->dev, "Selected %s/%dHz for %dHz sample rate\n",
  1445. pdata->bq_cfg[best].name,
  1446. pdata->bq_cfg[best].rate, fs);
  1447. coef_set = &pdata->bq_cfg[best];
  1448. regmask = (channel == 0) ? M98095_BQ1EN : M98095_BQ2EN;
  1449. /* Disable filter while configuring, and save current on/off state */
  1450. regsave = snd_soc_read(codec, M98095_088_CFG_LEVEL);
  1451. snd_soc_update_bits(codec, M98095_088_CFG_LEVEL, regmask, 0);
  1452. mutex_lock(&max98095->lock);
  1453. snd_soc_update_bits(codec, M98095_00F_HOST_CFG, M98095_SEG, M98095_SEG);
  1454. m98095_biquad_band(codec, channel, 0, coef_set->band1);
  1455. m98095_biquad_band(codec, channel, 1, coef_set->band2);
  1456. snd_soc_update_bits(codec, M98095_00F_HOST_CFG, M98095_SEG, 0);
  1457. mutex_unlock(&max98095->lock);
  1458. /* Restore the original on/off state */
  1459. snd_soc_update_bits(codec, M98095_088_CFG_LEVEL, regmask, regsave);
  1460. return 0;
  1461. }
  1462. static int max98095_get_bq_enum(struct snd_kcontrol *kcontrol,
  1463. struct snd_ctl_elem_value *ucontrol)
  1464. {
  1465. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1466. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1467. int channel = max98095_get_bq_channel(codec, kcontrol->id.name);
  1468. struct max98095_cdata *cdata;
  1469. if (channel < 0)
  1470. return channel;
  1471. cdata = &max98095->dai[channel];
  1472. ucontrol->value.enumerated.item[0] = cdata->bq_sel;
  1473. return 0;
  1474. }
  1475. static void max98095_handle_bq_pdata(struct snd_soc_codec *codec)
  1476. {
  1477. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1478. struct max98095_pdata *pdata = max98095->pdata;
  1479. struct max98095_biquad_cfg *cfg;
  1480. unsigned int cfgcnt;
  1481. int i, j;
  1482. const char **t;
  1483. int ret;
  1484. struct snd_kcontrol_new controls[] = {
  1485. SOC_ENUM_EXT((char *)bq_mode_name[0],
  1486. max98095->bq_enum,
  1487. max98095_get_bq_enum,
  1488. max98095_put_bq_enum),
  1489. SOC_ENUM_EXT((char *)bq_mode_name[1],
  1490. max98095->bq_enum,
  1491. max98095_get_bq_enum,
  1492. max98095_put_bq_enum),
  1493. };
  1494. BUILD_BUG_ON(ARRAY_SIZE(controls) != ARRAY_SIZE(bq_mode_name));
  1495. cfg = pdata->bq_cfg;
  1496. cfgcnt = pdata->bq_cfgcnt;
  1497. /* Setup an array of texts for the biquad enum.
  1498. * This is based on Mark Brown's equalizer driver code.
  1499. */
  1500. max98095->bq_textcnt = 0;
  1501. max98095->bq_texts = NULL;
  1502. for (i = 0; i < cfgcnt; i++) {
  1503. for (j = 0; j < max98095->bq_textcnt; j++) {
  1504. if (strcmp(cfg[i].name, max98095->bq_texts[j]) == 0)
  1505. break;
  1506. }
  1507. if (j != max98095->bq_textcnt)
  1508. continue;
  1509. /* Expand the array */
  1510. t = krealloc(max98095->bq_texts,
  1511. sizeof(char *) * (max98095->bq_textcnt + 1),
  1512. GFP_KERNEL);
  1513. if (t == NULL)
  1514. continue;
  1515. /* Store the new entry */
  1516. t[max98095->bq_textcnt] = cfg[i].name;
  1517. max98095->bq_textcnt++;
  1518. max98095->bq_texts = t;
  1519. }
  1520. /* Now point the soc_enum to .texts array items */
  1521. max98095->bq_enum.texts = max98095->bq_texts;
  1522. max98095->bq_enum.items = max98095->bq_textcnt;
  1523. ret = snd_soc_add_codec_controls(codec, controls, ARRAY_SIZE(controls));
  1524. if (ret != 0)
  1525. dev_err(codec->dev, "Failed to add Biquad control: %d\n", ret);
  1526. }
  1527. static void max98095_handle_pdata(struct snd_soc_codec *codec)
  1528. {
  1529. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1530. struct max98095_pdata *pdata = max98095->pdata;
  1531. u8 regval = 0;
  1532. if (!pdata) {
  1533. dev_dbg(codec->dev, "No platform data\n");
  1534. return;
  1535. }
  1536. /* Configure mic for analog/digital mic mode */
  1537. if (pdata->digmic_left_mode)
  1538. regval |= M98095_DIGMIC_L;
  1539. if (pdata->digmic_right_mode)
  1540. regval |= M98095_DIGMIC_R;
  1541. snd_soc_write(codec, M98095_087_CFG_MIC, regval);
  1542. /* Configure equalizers */
  1543. if (pdata->eq_cfgcnt)
  1544. max98095_handle_eq_pdata(codec);
  1545. /* Configure bi-quad filters */
  1546. if (pdata->bq_cfgcnt)
  1547. max98095_handle_bq_pdata(codec);
  1548. }
  1549. static irqreturn_t max98095_report_jack(int irq, void *data)
  1550. {
  1551. struct snd_soc_codec *codec = data;
  1552. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1553. unsigned int value;
  1554. int hp_report = 0;
  1555. int mic_report = 0;
  1556. /* Read the Jack Status Register */
  1557. value = snd_soc_read(codec, M98095_007_JACK_AUTO_STS);
  1558. /* If ddone is not set, then detection isn't finished yet */
  1559. if ((value & M98095_DDONE) == 0)
  1560. return IRQ_NONE;
  1561. /* if hp, check its bit, and if set, clear it */
  1562. if ((value & M98095_HP_IN || value & M98095_LO_IN) &&
  1563. max98095->headphone_jack)
  1564. hp_report |= SND_JACK_HEADPHONE;
  1565. /* if mic, check its bit, and if set, clear it */
  1566. if ((value & M98095_MIC_IN) && max98095->mic_jack)
  1567. mic_report |= SND_JACK_MICROPHONE;
  1568. if (max98095->headphone_jack == max98095->mic_jack) {
  1569. snd_soc_jack_report(max98095->headphone_jack,
  1570. hp_report | mic_report,
  1571. SND_JACK_HEADSET);
  1572. } else {
  1573. if (max98095->headphone_jack)
  1574. snd_soc_jack_report(max98095->headphone_jack,
  1575. hp_report, SND_JACK_HEADPHONE);
  1576. if (max98095->mic_jack)
  1577. snd_soc_jack_report(max98095->mic_jack,
  1578. mic_report, SND_JACK_MICROPHONE);
  1579. }
  1580. return IRQ_HANDLED;
  1581. }
  1582. static int max98095_jack_detect_enable(struct snd_soc_codec *codec)
  1583. {
  1584. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1585. int ret = 0;
  1586. int detect_enable = M98095_JDEN;
  1587. unsigned int slew = M98095_DEFAULT_SLEW_DELAY;
  1588. if (max98095->pdata->jack_detect_pin5en)
  1589. detect_enable |= M98095_PIN5EN;
  1590. if (max98095->pdata->jack_detect_delay)
  1591. slew = max98095->pdata->jack_detect_delay;
  1592. ret = snd_soc_write(codec, M98095_08E_JACK_DC_SLEW, slew);
  1593. if (ret < 0) {
  1594. dev_err(codec->dev, "Failed to cfg auto detect %d\n", ret);
  1595. return ret;
  1596. }
  1597. /* configure auto detection to be enabled */
  1598. ret = snd_soc_write(codec, M98095_089_JACK_DET_AUTO, detect_enable);
  1599. if (ret < 0) {
  1600. dev_err(codec->dev, "Failed to cfg auto detect %d\n", ret);
  1601. return ret;
  1602. }
  1603. return ret;
  1604. }
  1605. static int max98095_jack_detect_disable(struct snd_soc_codec *codec)
  1606. {
  1607. int ret = 0;
  1608. /* configure auto detection to be disabled */
  1609. ret = snd_soc_write(codec, M98095_089_JACK_DET_AUTO, 0x0);
  1610. if (ret < 0) {
  1611. dev_err(codec->dev, "Failed to cfg auto detect %d\n", ret);
  1612. return ret;
  1613. }
  1614. return ret;
  1615. }
  1616. int max98095_jack_detect(struct snd_soc_codec *codec,
  1617. struct snd_soc_jack *hp_jack, struct snd_soc_jack *mic_jack)
  1618. {
  1619. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1620. struct i2c_client *client = to_i2c_client(codec->dev);
  1621. int ret = 0;
  1622. max98095->headphone_jack = hp_jack;
  1623. max98095->mic_jack = mic_jack;
  1624. /* only progress if we have at least 1 jack pointer */
  1625. if (!hp_jack && !mic_jack)
  1626. return -EINVAL;
  1627. max98095_jack_detect_enable(codec);
  1628. /* enable interrupts for headphone jack detection */
  1629. ret = snd_soc_update_bits(codec, M98095_013_JACK_INT_EN,
  1630. M98095_IDDONE, M98095_IDDONE);
  1631. if (ret < 0) {
  1632. dev_err(codec->dev, "Failed to cfg jack irqs %d\n", ret);
  1633. return ret;
  1634. }
  1635. max98095_report_jack(client->irq, codec);
  1636. return 0;
  1637. }
  1638. EXPORT_SYMBOL_GPL(max98095_jack_detect);
  1639. #ifdef CONFIG_PM
  1640. static int max98095_suspend(struct snd_soc_codec *codec)
  1641. {
  1642. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1643. if (max98095->headphone_jack || max98095->mic_jack)
  1644. max98095_jack_detect_disable(codec);
  1645. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
  1646. return 0;
  1647. }
  1648. static int max98095_resume(struct snd_soc_codec *codec)
  1649. {
  1650. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1651. struct i2c_client *client = to_i2c_client(codec->dev);
  1652. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1653. if (max98095->headphone_jack || max98095->mic_jack) {
  1654. max98095_jack_detect_enable(codec);
  1655. max98095_report_jack(client->irq, codec);
  1656. }
  1657. return 0;
  1658. }
  1659. #else
  1660. #define max98095_suspend NULL
  1661. #define max98095_resume NULL
  1662. #endif
  1663. static int max98095_reset(struct snd_soc_codec *codec)
  1664. {
  1665. int i, ret;
  1666. /* Gracefully reset the DSP core and the codec hardware
  1667. * in a proper sequence */
  1668. ret = snd_soc_write(codec, M98095_00F_HOST_CFG, 0);
  1669. if (ret < 0) {
  1670. dev_err(codec->dev, "Failed to reset DSP: %d\n", ret);
  1671. return ret;
  1672. }
  1673. ret = snd_soc_write(codec, M98095_097_PWR_SYS, 0);
  1674. if (ret < 0) {
  1675. dev_err(codec->dev, "Failed to reset codec: %d\n", ret);
  1676. return ret;
  1677. }
  1678. /* Reset to hardware default for registers, as there is not
  1679. * a soft reset hardware control register */
  1680. for (i = M98095_010_HOST_INT_CFG; i < M98095_REG_MAX_CACHED; i++) {
  1681. ret = snd_soc_write(codec, i, snd_soc_read(codec, i));
  1682. if (ret < 0) {
  1683. dev_err(codec->dev, "Failed to reset: %d\n", ret);
  1684. return ret;
  1685. }
  1686. }
  1687. return ret;
  1688. }
  1689. static int max98095_probe(struct snd_soc_codec *codec)
  1690. {
  1691. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1692. struct max98095_cdata *cdata;
  1693. struct i2c_client *client;
  1694. int ret = 0;
  1695. max98095->mclk = devm_clk_get(codec->dev, "mclk");
  1696. if (PTR_ERR(max98095->mclk) == -EPROBE_DEFER)
  1697. return -EPROBE_DEFER;
  1698. /* reset the codec, the DSP core, and disable all interrupts */
  1699. max98095_reset(codec);
  1700. client = to_i2c_client(codec->dev);
  1701. /* initialize private data */
  1702. max98095->sysclk = (unsigned)-1;
  1703. max98095->eq_textcnt = 0;
  1704. max98095->bq_textcnt = 0;
  1705. cdata = &max98095->dai[0];
  1706. cdata->rate = (unsigned)-1;
  1707. cdata->fmt = (unsigned)-1;
  1708. cdata->eq_sel = 0;
  1709. cdata->bq_sel = 0;
  1710. cdata = &max98095->dai[1];
  1711. cdata->rate = (unsigned)-1;
  1712. cdata->fmt = (unsigned)-1;
  1713. cdata->eq_sel = 0;
  1714. cdata->bq_sel = 0;
  1715. cdata = &max98095->dai[2];
  1716. cdata->rate = (unsigned)-1;
  1717. cdata->fmt = (unsigned)-1;
  1718. cdata->eq_sel = 0;
  1719. cdata->bq_sel = 0;
  1720. max98095->lin_state = 0;
  1721. max98095->mic1pre = 0;
  1722. max98095->mic2pre = 0;
  1723. if (client->irq) {
  1724. /* register an audio interrupt */
  1725. ret = request_threaded_irq(client->irq, NULL,
  1726. max98095_report_jack,
  1727. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
  1728. IRQF_ONESHOT, "max98095", codec);
  1729. if (ret) {
  1730. dev_err(codec->dev, "Failed to request IRQ: %d\n", ret);
  1731. goto err_access;
  1732. }
  1733. }
  1734. ret = snd_soc_read(codec, M98095_0FF_REV_ID);
  1735. if (ret < 0) {
  1736. dev_err(codec->dev, "Failure reading hardware revision: %d\n",
  1737. ret);
  1738. goto err_irq;
  1739. }
  1740. dev_info(codec->dev, "Hardware revision: %c\n", ret - 0x40 + 'A');
  1741. snd_soc_write(codec, M98095_097_PWR_SYS, M98095_PWRSV);
  1742. snd_soc_write(codec, M98095_048_MIX_DAC_LR,
  1743. M98095_DAI1L_TO_DACL|M98095_DAI1R_TO_DACR);
  1744. snd_soc_write(codec, M98095_049_MIX_DAC_M,
  1745. M98095_DAI2M_TO_DACM|M98095_DAI3M_TO_DACM);
  1746. snd_soc_write(codec, M98095_092_PWR_EN_OUT, M98095_SPK_SPREADSPECTRUM);
  1747. snd_soc_write(codec, M98095_045_CFG_DSP, M98095_DSPNORMAL);
  1748. snd_soc_write(codec, M98095_04E_CFG_HP, M98095_HPNORMAL);
  1749. snd_soc_write(codec, M98095_02C_DAI1_IOCFG,
  1750. M98095_S1NORMAL|M98095_SDATA);
  1751. snd_soc_write(codec, M98095_036_DAI2_IOCFG,
  1752. M98095_S2NORMAL|M98095_SDATA);
  1753. snd_soc_write(codec, M98095_040_DAI3_IOCFG,
  1754. M98095_S3NORMAL|M98095_SDATA);
  1755. max98095_handle_pdata(codec);
  1756. /* take the codec out of the shut down */
  1757. snd_soc_update_bits(codec, M98095_097_PWR_SYS, M98095_SHDNRUN,
  1758. M98095_SHDNRUN);
  1759. return 0;
  1760. err_irq:
  1761. if (client->irq)
  1762. free_irq(client->irq, codec);
  1763. err_access:
  1764. return ret;
  1765. }
  1766. static int max98095_remove(struct snd_soc_codec *codec)
  1767. {
  1768. struct max98095_priv *max98095 = snd_soc_codec_get_drvdata(codec);
  1769. struct i2c_client *client = to_i2c_client(codec->dev);
  1770. if (max98095->headphone_jack || max98095->mic_jack)
  1771. max98095_jack_detect_disable(codec);
  1772. if (client->irq)
  1773. free_irq(client->irq, codec);
  1774. return 0;
  1775. }
  1776. static struct snd_soc_codec_driver soc_codec_dev_max98095 = {
  1777. .probe = max98095_probe,
  1778. .remove = max98095_remove,
  1779. .suspend = max98095_suspend,
  1780. .resume = max98095_resume,
  1781. .set_bias_level = max98095_set_bias_level,
  1782. .component_driver = {
  1783. .controls = max98095_snd_controls,
  1784. .num_controls = ARRAY_SIZE(max98095_snd_controls),
  1785. .dapm_widgets = max98095_dapm_widgets,
  1786. .num_dapm_widgets = ARRAY_SIZE(max98095_dapm_widgets),
  1787. .dapm_routes = max98095_audio_map,
  1788. .num_dapm_routes = ARRAY_SIZE(max98095_audio_map),
  1789. },
  1790. };
  1791. static int max98095_i2c_probe(struct i2c_client *i2c,
  1792. const struct i2c_device_id *id)
  1793. {
  1794. struct max98095_priv *max98095;
  1795. int ret;
  1796. max98095 = devm_kzalloc(&i2c->dev, sizeof(struct max98095_priv),
  1797. GFP_KERNEL);
  1798. if (max98095 == NULL)
  1799. return -ENOMEM;
  1800. mutex_init(&max98095->lock);
  1801. max98095->regmap = devm_regmap_init_i2c(i2c, &max98095_regmap);
  1802. if (IS_ERR(max98095->regmap)) {
  1803. ret = PTR_ERR(max98095->regmap);
  1804. dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
  1805. return ret;
  1806. }
  1807. max98095->devtype = id->driver_data;
  1808. i2c_set_clientdata(i2c, max98095);
  1809. max98095->pdata = i2c->dev.platform_data;
  1810. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_max98095,
  1811. max98095_dai, ARRAY_SIZE(max98095_dai));
  1812. return ret;
  1813. }
  1814. static int max98095_i2c_remove(struct i2c_client *client)
  1815. {
  1816. snd_soc_unregister_codec(&client->dev);
  1817. return 0;
  1818. }
  1819. static const struct i2c_device_id max98095_i2c_id[] = {
  1820. { "max98095", MAX98095 },
  1821. { }
  1822. };
  1823. MODULE_DEVICE_TABLE(i2c, max98095_i2c_id);
  1824. static const struct of_device_id max98095_of_match[] = {
  1825. { .compatible = "maxim,max98095", },
  1826. { }
  1827. };
  1828. MODULE_DEVICE_TABLE(of, max98095_of_match);
  1829. static struct i2c_driver max98095_i2c_driver = {
  1830. .driver = {
  1831. .name = "max98095",
  1832. .of_match_table = of_match_ptr(max98095_of_match),
  1833. },
  1834. .probe = max98095_i2c_probe,
  1835. .remove = max98095_i2c_remove,
  1836. .id_table = max98095_i2c_id,
  1837. };
  1838. module_i2c_driver(max98095_i2c_driver);
  1839. MODULE_DESCRIPTION("ALSA SoC MAX98095 driver");
  1840. MODULE_AUTHOR("Peter Hsiang");
  1841. MODULE_LICENSE("GPL");