nau8825.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573
  1. /*
  2. * Nuvoton NAU8825 audio codec driver
  3. *
  4. * Copyright 2015 Google Chromium project.
  5. * Author: Anatol Pomozov <anatol@chromium.org>
  6. * Copyright 2015 Nuvoton Technology Corp.
  7. * Co-author: Meng-Huang Kuo <mhkuo@nuvoton.com>
  8. *
  9. * Licensed under the GPL-2.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/i2c.h>
  15. #include <linux/regmap.h>
  16. #include <linux/slab.h>
  17. #include <linux/clk.h>
  18. #include <linux/acpi.h>
  19. #include <linux/math64.h>
  20. #include <linux/semaphore.h>
  21. #include <sound/initval.h>
  22. #include <sound/tlv.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/jack.h>
  28. #include "nau8825.h"
  29. #define NUVOTON_CODEC_DAI "nau8825-hifi"
  30. #define NAU_FREF_MAX 13500000
  31. #define NAU_FVCO_MAX 124000000
  32. #define NAU_FVCO_MIN 90000000
  33. /* cross talk suppression detection */
  34. #define LOG10_MAGIC 646456993
  35. #define GAIN_AUGMENT 22500
  36. #define SIDETONE_BASE 207000
  37. /* the maximum frequency of CLK_ADC and CLK_DAC */
  38. #define CLK_DA_AD_MAX 6144000
  39. static int nau8825_configure_sysclk(struct nau8825 *nau8825,
  40. int clk_id, unsigned int freq);
  41. struct nau8825_fll {
  42. int mclk_src;
  43. int ratio;
  44. int fll_frac;
  45. int fll_int;
  46. int clk_ref_div;
  47. };
  48. struct nau8825_fll_attr {
  49. unsigned int param;
  50. unsigned int val;
  51. };
  52. /* scaling for mclk from sysclk_src output */
  53. static const struct nau8825_fll_attr mclk_src_scaling[] = {
  54. { 1, 0x0 },
  55. { 2, 0x2 },
  56. { 4, 0x3 },
  57. { 8, 0x4 },
  58. { 16, 0x5 },
  59. { 32, 0x6 },
  60. { 3, 0x7 },
  61. { 6, 0xa },
  62. { 12, 0xb },
  63. { 24, 0xc },
  64. { 48, 0xd },
  65. { 96, 0xe },
  66. { 5, 0xf },
  67. };
  68. /* ratio for input clk freq */
  69. static const struct nau8825_fll_attr fll_ratio[] = {
  70. { 512000, 0x01 },
  71. { 256000, 0x02 },
  72. { 128000, 0x04 },
  73. { 64000, 0x08 },
  74. { 32000, 0x10 },
  75. { 8000, 0x20 },
  76. { 4000, 0x40 },
  77. };
  78. static const struct nau8825_fll_attr fll_pre_scalar[] = {
  79. { 1, 0x0 },
  80. { 2, 0x1 },
  81. { 4, 0x2 },
  82. { 8, 0x3 },
  83. };
  84. /* over sampling rate */
  85. struct nau8825_osr_attr {
  86. unsigned int osr;
  87. unsigned int clk_src;
  88. };
  89. static const struct nau8825_osr_attr osr_dac_sel[] = {
  90. { 64, 2 }, /* OSR 64, SRC 1/4 */
  91. { 256, 0 }, /* OSR 256, SRC 1 */
  92. { 128, 1 }, /* OSR 128, SRC 1/2 */
  93. { 0, 0 },
  94. { 32, 3 }, /* OSR 32, SRC 1/8 */
  95. };
  96. static const struct nau8825_osr_attr osr_adc_sel[] = {
  97. { 32, 3 }, /* OSR 32, SRC 1/8 */
  98. { 64, 2 }, /* OSR 64, SRC 1/4 */
  99. { 128, 1 }, /* OSR 128, SRC 1/2 */
  100. { 256, 0 }, /* OSR 256, SRC 1 */
  101. };
  102. static const struct reg_default nau8825_reg_defaults[] = {
  103. { NAU8825_REG_ENA_CTRL, 0x00ff },
  104. { NAU8825_REG_IIC_ADDR_SET, 0x0 },
  105. { NAU8825_REG_CLK_DIVIDER, 0x0050 },
  106. { NAU8825_REG_FLL1, 0x0 },
  107. { NAU8825_REG_FLL2, 0x3126 },
  108. { NAU8825_REG_FLL3, 0x0008 },
  109. { NAU8825_REG_FLL4, 0x0010 },
  110. { NAU8825_REG_FLL5, 0x0 },
  111. { NAU8825_REG_FLL6, 0x6000 },
  112. { NAU8825_REG_FLL_VCO_RSV, 0xf13c },
  113. { NAU8825_REG_HSD_CTRL, 0x000c },
  114. { NAU8825_REG_JACK_DET_CTRL, 0x0 },
  115. { NAU8825_REG_INTERRUPT_MASK, 0x0 },
  116. { NAU8825_REG_INTERRUPT_DIS_CTRL, 0xffff },
  117. { NAU8825_REG_SAR_CTRL, 0x0015 },
  118. { NAU8825_REG_KEYDET_CTRL, 0x0110 },
  119. { NAU8825_REG_VDET_THRESHOLD_1, 0x0 },
  120. { NAU8825_REG_VDET_THRESHOLD_2, 0x0 },
  121. { NAU8825_REG_VDET_THRESHOLD_3, 0x0 },
  122. { NAU8825_REG_VDET_THRESHOLD_4, 0x0 },
  123. { NAU8825_REG_GPIO34_CTRL, 0x0 },
  124. { NAU8825_REG_GPIO12_CTRL, 0x0 },
  125. { NAU8825_REG_TDM_CTRL, 0x0 },
  126. { NAU8825_REG_I2S_PCM_CTRL1, 0x000b },
  127. { NAU8825_REG_I2S_PCM_CTRL2, 0x8010 },
  128. { NAU8825_REG_LEFT_TIME_SLOT, 0x0 },
  129. { NAU8825_REG_RIGHT_TIME_SLOT, 0x0 },
  130. { NAU8825_REG_BIQ_CTRL, 0x0 },
  131. { NAU8825_REG_BIQ_COF1, 0x0 },
  132. { NAU8825_REG_BIQ_COF2, 0x0 },
  133. { NAU8825_REG_BIQ_COF3, 0x0 },
  134. { NAU8825_REG_BIQ_COF4, 0x0 },
  135. { NAU8825_REG_BIQ_COF5, 0x0 },
  136. { NAU8825_REG_BIQ_COF6, 0x0 },
  137. { NAU8825_REG_BIQ_COF7, 0x0 },
  138. { NAU8825_REG_BIQ_COF8, 0x0 },
  139. { NAU8825_REG_BIQ_COF9, 0x0 },
  140. { NAU8825_REG_BIQ_COF10, 0x0 },
  141. { NAU8825_REG_ADC_RATE, 0x0010 },
  142. { NAU8825_REG_DAC_CTRL1, 0x0001 },
  143. { NAU8825_REG_DAC_CTRL2, 0x0 },
  144. { NAU8825_REG_DAC_DGAIN_CTRL, 0x0 },
  145. { NAU8825_REG_ADC_DGAIN_CTRL, 0x00cf },
  146. { NAU8825_REG_MUTE_CTRL, 0x0 },
  147. { NAU8825_REG_HSVOL_CTRL, 0x0 },
  148. { NAU8825_REG_DACL_CTRL, 0x02cf },
  149. { NAU8825_REG_DACR_CTRL, 0x00cf },
  150. { NAU8825_REG_ADC_DRC_KNEE_IP12, 0x1486 },
  151. { NAU8825_REG_ADC_DRC_KNEE_IP34, 0x0f12 },
  152. { NAU8825_REG_ADC_DRC_SLOPES, 0x25ff },
  153. { NAU8825_REG_ADC_DRC_ATKDCY, 0x3457 },
  154. { NAU8825_REG_DAC_DRC_KNEE_IP12, 0x1486 },
  155. { NAU8825_REG_DAC_DRC_KNEE_IP34, 0x0f12 },
  156. { NAU8825_REG_DAC_DRC_SLOPES, 0x25f9 },
  157. { NAU8825_REG_DAC_DRC_ATKDCY, 0x3457 },
  158. { NAU8825_REG_IMM_MODE_CTRL, 0x0 },
  159. { NAU8825_REG_CLASSG_CTRL, 0x0 },
  160. { NAU8825_REG_OPT_EFUSE_CTRL, 0x0 },
  161. { NAU8825_REG_MISC_CTRL, 0x0 },
  162. { NAU8825_REG_BIAS_ADJ, 0x0 },
  163. { NAU8825_REG_TRIM_SETTINGS, 0x0 },
  164. { NAU8825_REG_ANALOG_CONTROL_1, 0x0 },
  165. { NAU8825_REG_ANALOG_CONTROL_2, 0x0 },
  166. { NAU8825_REG_ANALOG_ADC_1, 0x0011 },
  167. { NAU8825_REG_ANALOG_ADC_2, 0x0020 },
  168. { NAU8825_REG_RDAC, 0x0008 },
  169. { NAU8825_REG_MIC_BIAS, 0x0006 },
  170. { NAU8825_REG_BOOST, 0x0 },
  171. { NAU8825_REG_FEPGA, 0x0 },
  172. { NAU8825_REG_POWER_UP_CONTROL, 0x0 },
  173. { NAU8825_REG_CHARGE_PUMP, 0x0 },
  174. };
  175. /* register backup table when cross talk detection */
  176. static struct reg_default nau8825_xtalk_baktab[] = {
  177. { NAU8825_REG_ADC_DGAIN_CTRL, 0 },
  178. { NAU8825_REG_HSVOL_CTRL, 0 },
  179. { NAU8825_REG_DACL_CTRL, 0 },
  180. { NAU8825_REG_DACR_CTRL, 0 },
  181. };
  182. static const unsigned short logtable[256] = {
  183. 0x0000, 0x0171, 0x02e0, 0x044e, 0x05ba, 0x0725, 0x088e, 0x09f7,
  184. 0x0b5d, 0x0cc3, 0x0e27, 0x0f8a, 0x10eb, 0x124b, 0x13aa, 0x1508,
  185. 0x1664, 0x17bf, 0x1919, 0x1a71, 0x1bc8, 0x1d1e, 0x1e73, 0x1fc6,
  186. 0x2119, 0x226a, 0x23ba, 0x2508, 0x2656, 0x27a2, 0x28ed, 0x2a37,
  187. 0x2b80, 0x2cc8, 0x2e0f, 0x2f54, 0x3098, 0x31dc, 0x331e, 0x345f,
  188. 0x359f, 0x36de, 0x381b, 0x3958, 0x3a94, 0x3bce, 0x3d08, 0x3e41,
  189. 0x3f78, 0x40af, 0x41e4, 0x4319, 0x444c, 0x457f, 0x46b0, 0x47e1,
  190. 0x4910, 0x4a3f, 0x4b6c, 0x4c99, 0x4dc5, 0x4eef, 0x5019, 0x5142,
  191. 0x526a, 0x5391, 0x54b7, 0x55dc, 0x5700, 0x5824, 0x5946, 0x5a68,
  192. 0x5b89, 0x5ca8, 0x5dc7, 0x5ee5, 0x6003, 0x611f, 0x623a, 0x6355,
  193. 0x646f, 0x6588, 0x66a0, 0x67b7, 0x68ce, 0x69e4, 0x6af8, 0x6c0c,
  194. 0x6d20, 0x6e32, 0x6f44, 0x7055, 0x7165, 0x7274, 0x7383, 0x7490,
  195. 0x759d, 0x76aa, 0x77b5, 0x78c0, 0x79ca, 0x7ad3, 0x7bdb, 0x7ce3,
  196. 0x7dea, 0x7ef0, 0x7ff6, 0x80fb, 0x81ff, 0x8302, 0x8405, 0x8507,
  197. 0x8608, 0x8709, 0x8809, 0x8908, 0x8a06, 0x8b04, 0x8c01, 0x8cfe,
  198. 0x8dfa, 0x8ef5, 0x8fef, 0x90e9, 0x91e2, 0x92db, 0x93d2, 0x94ca,
  199. 0x95c0, 0x96b6, 0x97ab, 0x98a0, 0x9994, 0x9a87, 0x9b7a, 0x9c6c,
  200. 0x9d5e, 0x9e4f, 0x9f3f, 0xa02e, 0xa11e, 0xa20c, 0xa2fa, 0xa3e7,
  201. 0xa4d4, 0xa5c0, 0xa6ab, 0xa796, 0xa881, 0xa96a, 0xaa53, 0xab3c,
  202. 0xac24, 0xad0c, 0xadf2, 0xaed9, 0xafbe, 0xb0a4, 0xb188, 0xb26c,
  203. 0xb350, 0xb433, 0xb515, 0xb5f7, 0xb6d9, 0xb7ba, 0xb89a, 0xb97a,
  204. 0xba59, 0xbb38, 0xbc16, 0xbcf4, 0xbdd1, 0xbead, 0xbf8a, 0xc065,
  205. 0xc140, 0xc21b, 0xc2f5, 0xc3cf, 0xc4a8, 0xc580, 0xc658, 0xc730,
  206. 0xc807, 0xc8de, 0xc9b4, 0xca8a, 0xcb5f, 0xcc34, 0xcd08, 0xcddc,
  207. 0xceaf, 0xcf82, 0xd054, 0xd126, 0xd1f7, 0xd2c8, 0xd399, 0xd469,
  208. 0xd538, 0xd607, 0xd6d6, 0xd7a4, 0xd872, 0xd93f, 0xda0c, 0xdad9,
  209. 0xdba5, 0xdc70, 0xdd3b, 0xde06, 0xded0, 0xdf9a, 0xe063, 0xe12c,
  210. 0xe1f5, 0xe2bd, 0xe385, 0xe44c, 0xe513, 0xe5d9, 0xe69f, 0xe765,
  211. 0xe82a, 0xe8ef, 0xe9b3, 0xea77, 0xeb3b, 0xebfe, 0xecc1, 0xed83,
  212. 0xee45, 0xef06, 0xefc8, 0xf088, 0xf149, 0xf209, 0xf2c8, 0xf387,
  213. 0xf446, 0xf505, 0xf5c3, 0xf680, 0xf73e, 0xf7fb, 0xf8b7, 0xf973,
  214. 0xfa2f, 0xfaea, 0xfba5, 0xfc60, 0xfd1a, 0xfdd4, 0xfe8e, 0xff47
  215. };
  216. /**
  217. * nau8825_sema_acquire - acquire the semaphore of nau88l25
  218. * @nau8825: component to register the codec private data with
  219. * @timeout: how long in jiffies to wait before failure or zero to wait
  220. * until release
  221. *
  222. * Attempts to acquire the semaphore with number of jiffies. If no more
  223. * tasks are allowed to acquire the semaphore, calling this function will
  224. * put the task to sleep. If the semaphore is not released within the
  225. * specified number of jiffies, this function returns.
  226. * Acquires the semaphore without jiffies. If no more tasks are allowed
  227. * to acquire the semaphore, calling this function will put the task to
  228. * sleep until the semaphore is released.
  229. * If the semaphore is not released within the specified number of jiffies,
  230. * this function returns -ETIME.
  231. * If the sleep is interrupted by a signal, this function will return -EINTR.
  232. * It returns 0 if the semaphore was acquired successfully.
  233. */
  234. static int nau8825_sema_acquire(struct nau8825 *nau8825, long timeout)
  235. {
  236. int ret;
  237. if (timeout) {
  238. ret = down_timeout(&nau8825->xtalk_sem, timeout);
  239. if (ret < 0)
  240. dev_warn(nau8825->dev, "Acquire semaphone timeout\n");
  241. } else {
  242. ret = down_interruptible(&nau8825->xtalk_sem);
  243. if (ret < 0)
  244. dev_warn(nau8825->dev, "Acquire semaphone fail\n");
  245. }
  246. return ret;
  247. }
  248. /**
  249. * nau8825_sema_release - release the semaphore of nau88l25
  250. * @nau8825: component to register the codec private data with
  251. *
  252. * Release the semaphore which may be called from any context and
  253. * even by tasks which have never called down().
  254. */
  255. static inline void nau8825_sema_release(struct nau8825 *nau8825)
  256. {
  257. up(&nau8825->xtalk_sem);
  258. }
  259. /**
  260. * nau8825_sema_reset - reset the semaphore for nau88l25
  261. * @nau8825: component to register the codec private data with
  262. *
  263. * Reset the counter of the semaphore. Call this function to restart
  264. * a new round task management.
  265. */
  266. static inline void nau8825_sema_reset(struct nau8825 *nau8825)
  267. {
  268. nau8825->xtalk_sem.count = 1;
  269. }
  270. /**
  271. * Ramp up the headphone volume change gradually to target level.
  272. *
  273. * @nau8825: component to register the codec private data with
  274. * @vol_from: the volume to start up
  275. * @vol_to: the target volume
  276. * @step: the volume span to move on
  277. *
  278. * The headphone volume is from 0dB to minimum -54dB and -1dB per step.
  279. * If the volume changes sharp, there is a pop noise heard in headphone. We
  280. * provide the function to ramp up the volume up or down by delaying 10ms
  281. * per step.
  282. */
  283. static void nau8825_hpvol_ramp(struct nau8825 *nau8825,
  284. unsigned int vol_from, unsigned int vol_to, unsigned int step)
  285. {
  286. unsigned int value, volume, ramp_up, from, to;
  287. if (vol_from == vol_to || step == 0) {
  288. return;
  289. } else if (vol_from < vol_to) {
  290. ramp_up = true;
  291. from = vol_from;
  292. to = vol_to;
  293. } else {
  294. ramp_up = false;
  295. from = vol_to;
  296. to = vol_from;
  297. }
  298. /* only handle volume from 0dB to minimum -54dB */
  299. if (to > NAU8825_HP_VOL_MIN)
  300. to = NAU8825_HP_VOL_MIN;
  301. for (volume = from; volume < to; volume += step) {
  302. if (ramp_up)
  303. value = volume;
  304. else
  305. value = to - volume + from;
  306. regmap_update_bits(nau8825->regmap, NAU8825_REG_HSVOL_CTRL,
  307. NAU8825_HPL_VOL_MASK | NAU8825_HPR_VOL_MASK,
  308. (value << NAU8825_HPL_VOL_SFT) | value);
  309. usleep_range(10000, 10500);
  310. }
  311. if (ramp_up)
  312. value = to;
  313. else
  314. value = from;
  315. regmap_update_bits(nau8825->regmap, NAU8825_REG_HSVOL_CTRL,
  316. NAU8825_HPL_VOL_MASK | NAU8825_HPR_VOL_MASK,
  317. (value << NAU8825_HPL_VOL_SFT) | value);
  318. }
  319. /**
  320. * Computes log10 of a value; the result is round off to 3 decimal. This func-
  321. * tion takes reference to dvb-math. The source code locates as the following.
  322. * Linux/drivers/media/dvb-core/dvb_math.c
  323. *
  324. * return log10(value) * 1000
  325. */
  326. static u32 nau8825_intlog10_dec3(u32 value)
  327. {
  328. u32 msb, logentry, significand, interpolation, log10val;
  329. u64 log2val;
  330. /* first detect the msb (count begins at 0) */
  331. msb = fls(value) - 1;
  332. /**
  333. * now we use a logtable after the following method:
  334. *
  335. * log2(2^x * y) * 2^24 = x * 2^24 + log2(y) * 2^24
  336. * where x = msb and therefore 1 <= y < 2
  337. * first y is determined by shifting the value left
  338. * so that msb is bit 31
  339. * 0x00231f56 -> 0x8C7D5800
  340. * the result is y * 2^31 -> "significand"
  341. * then the highest 9 bits are used for a table lookup
  342. * the highest bit is discarded because it's always set
  343. * the highest nine bits in our example are 100011000
  344. * so we would use the entry 0x18
  345. */
  346. significand = value << (31 - msb);
  347. logentry = (significand >> 23) & 0xff;
  348. /**
  349. * last step we do is interpolation because of the
  350. * limitations of the log table the error is that part of
  351. * the significand which isn't used for lookup then we
  352. * compute the ratio between the error and the next table entry
  353. * and interpolate it between the log table entry used and the
  354. * next one the biggest error possible is 0x7fffff
  355. * (in our example it's 0x7D5800)
  356. * needed value for next table entry is 0x800000
  357. * so the interpolation is
  358. * (error / 0x800000) * (logtable_next - logtable_current)
  359. * in the implementation the division is moved to the end for
  360. * better accuracy there is also an overflow correction if
  361. * logtable_next is 256
  362. */
  363. interpolation = ((significand & 0x7fffff) *
  364. ((logtable[(logentry + 1) & 0xff] -
  365. logtable[logentry]) & 0xffff)) >> 15;
  366. log2val = ((msb << 24) + (logtable[logentry] << 8) + interpolation);
  367. /**
  368. * log10(x) = log2(x) * log10(2)
  369. */
  370. log10val = (log2val * LOG10_MAGIC) >> 31;
  371. /**
  372. * the result is round off to 3 decimal
  373. */
  374. return log10val / ((1 << 24) / 1000);
  375. }
  376. /**
  377. * computes cross talk suppression sidetone gain.
  378. *
  379. * @sig_org: orignal signal level
  380. * @sig_cros: cross talk signal level
  381. *
  382. * The orignal and cross talk signal vlues need to be characterized.
  383. * Once these values have been characterized, this sidetone value
  384. * can be converted to decibel with the equation below.
  385. * sidetone = 20 * log (original signal level / crosstalk signal level)
  386. *
  387. * return cross talk sidetone gain
  388. */
  389. static u32 nau8825_xtalk_sidetone(u32 sig_org, u32 sig_cros)
  390. {
  391. u32 gain, sidetone;
  392. if (unlikely(sig_org == 0) || unlikely(sig_cros == 0)) {
  393. WARN_ON(1);
  394. return 0;
  395. }
  396. sig_org = nau8825_intlog10_dec3(sig_org);
  397. sig_cros = nau8825_intlog10_dec3(sig_cros);
  398. if (sig_org >= sig_cros)
  399. gain = (sig_org - sig_cros) * 20 + GAIN_AUGMENT;
  400. else
  401. gain = (sig_cros - sig_org) * 20 + GAIN_AUGMENT;
  402. sidetone = SIDETONE_BASE - gain * 2;
  403. sidetone /= 1000;
  404. return sidetone;
  405. }
  406. static int nau8825_xtalk_baktab_index_by_reg(unsigned int reg)
  407. {
  408. int index;
  409. for (index = 0; index < ARRAY_SIZE(nau8825_xtalk_baktab); index++)
  410. if (nau8825_xtalk_baktab[index].reg == reg)
  411. return index;
  412. return -EINVAL;
  413. }
  414. static void nau8825_xtalk_backup(struct nau8825 *nau8825)
  415. {
  416. int i;
  417. /* Backup some register values to backup table */
  418. for (i = 0; i < ARRAY_SIZE(nau8825_xtalk_baktab); i++)
  419. regmap_read(nau8825->regmap, nau8825_xtalk_baktab[i].reg,
  420. &nau8825_xtalk_baktab[i].def);
  421. }
  422. static void nau8825_xtalk_restore(struct nau8825 *nau8825)
  423. {
  424. int i, volume;
  425. /* Restore register values from backup table; When the driver restores
  426. * the headphone volumem, it needs recover to original level gradually
  427. * with 3dB per step for less pop noise.
  428. */
  429. for (i = 0; i < ARRAY_SIZE(nau8825_xtalk_baktab); i++) {
  430. if (nau8825_xtalk_baktab[i].reg == NAU8825_REG_HSVOL_CTRL) {
  431. /* Ramping up the volume change to reduce pop noise */
  432. volume = nau8825_xtalk_baktab[i].def &
  433. NAU8825_HPR_VOL_MASK;
  434. nau8825_hpvol_ramp(nau8825, 0, volume, 3);
  435. continue;
  436. }
  437. regmap_write(nau8825->regmap, nau8825_xtalk_baktab[i].reg,
  438. nau8825_xtalk_baktab[i].def);
  439. }
  440. }
  441. static void nau8825_xtalk_prepare_dac(struct nau8825 *nau8825)
  442. {
  443. /* Enable power of DAC path */
  444. regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
  445. NAU8825_ENABLE_DACR | NAU8825_ENABLE_DACL |
  446. NAU8825_ENABLE_ADC | NAU8825_ENABLE_ADC_CLK |
  447. NAU8825_ENABLE_DAC_CLK, NAU8825_ENABLE_DACR |
  448. NAU8825_ENABLE_DACL | NAU8825_ENABLE_ADC |
  449. NAU8825_ENABLE_ADC_CLK | NAU8825_ENABLE_DAC_CLK);
  450. /* Prevent startup click by letting charge pump to ramp up and
  451. * change bump enable
  452. */
  453. regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
  454. NAU8825_JAMNODCLOW | NAU8825_CHANRGE_PUMP_EN,
  455. NAU8825_JAMNODCLOW | NAU8825_CHANRGE_PUMP_EN);
  456. /* Enable clock sync of DAC and DAC clock */
  457. regmap_update_bits(nau8825->regmap, NAU8825_REG_RDAC,
  458. NAU8825_RDAC_EN | NAU8825_RDAC_CLK_EN |
  459. NAU8825_RDAC_FS_BCLK_ENB,
  460. NAU8825_RDAC_EN | NAU8825_RDAC_CLK_EN);
  461. /* Power up output driver with 2 stage */
  462. regmap_update_bits(nau8825->regmap, NAU8825_REG_POWER_UP_CONTROL,
  463. NAU8825_POWERUP_INTEGR_R | NAU8825_POWERUP_INTEGR_L |
  464. NAU8825_POWERUP_DRV_IN_R | NAU8825_POWERUP_DRV_IN_L,
  465. NAU8825_POWERUP_INTEGR_R | NAU8825_POWERUP_INTEGR_L |
  466. NAU8825_POWERUP_DRV_IN_R | NAU8825_POWERUP_DRV_IN_L);
  467. regmap_update_bits(nau8825->regmap, NAU8825_REG_POWER_UP_CONTROL,
  468. NAU8825_POWERUP_HP_DRV_R | NAU8825_POWERUP_HP_DRV_L,
  469. NAU8825_POWERUP_HP_DRV_R | NAU8825_POWERUP_HP_DRV_L);
  470. /* HP outputs not shouted to ground */
  471. regmap_update_bits(nau8825->regmap, NAU8825_REG_HSD_CTRL,
  472. NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L, 0);
  473. /* Enable HP boost driver */
  474. regmap_update_bits(nau8825->regmap, NAU8825_REG_BOOST,
  475. NAU8825_HP_BOOST_DIS, NAU8825_HP_BOOST_DIS);
  476. /* Enable class G compare path to supply 1.8V or 0.9V. */
  477. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLASSG_CTRL,
  478. NAU8825_CLASSG_LDAC_EN | NAU8825_CLASSG_RDAC_EN,
  479. NAU8825_CLASSG_LDAC_EN | NAU8825_CLASSG_RDAC_EN);
  480. }
  481. static void nau8825_xtalk_prepare_adc(struct nau8825 *nau8825)
  482. {
  483. /* Power up left ADC and raise 5dB than Vmid for Vref */
  484. regmap_update_bits(nau8825->regmap, NAU8825_REG_ANALOG_ADC_2,
  485. NAU8825_POWERUP_ADCL | NAU8825_ADC_VREFSEL_MASK,
  486. NAU8825_POWERUP_ADCL | NAU8825_ADC_VREFSEL_VMID_PLUS_0_5DB);
  487. }
  488. static void nau8825_xtalk_clock(struct nau8825 *nau8825)
  489. {
  490. /* Recover FLL default value */
  491. regmap_write(nau8825->regmap, NAU8825_REG_FLL1, 0x0);
  492. regmap_write(nau8825->regmap, NAU8825_REG_FLL2, 0x3126);
  493. regmap_write(nau8825->regmap, NAU8825_REG_FLL3, 0x0008);
  494. regmap_write(nau8825->regmap, NAU8825_REG_FLL4, 0x0010);
  495. regmap_write(nau8825->regmap, NAU8825_REG_FLL5, 0x0);
  496. regmap_write(nau8825->regmap, NAU8825_REG_FLL6, 0x6000);
  497. /* Enable internal VCO clock for detection signal generated */
  498. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  499. NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
  500. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6, NAU8825_DCO_EN,
  501. NAU8825_DCO_EN);
  502. /* Given specific clock frequency of internal clock to
  503. * generate signal.
  504. */
  505. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  506. NAU8825_CLK_MCLK_SRC_MASK, 0xf);
  507. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL1,
  508. NAU8825_FLL_RATIO_MASK, 0x10);
  509. }
  510. static void nau8825_xtalk_prepare(struct nau8825 *nau8825)
  511. {
  512. int volume, index;
  513. /* Backup those registers changed by cross talk detection */
  514. nau8825_xtalk_backup(nau8825);
  515. /* Config IIS as master to output signal by codec */
  516. regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
  517. NAU8825_I2S_MS_MASK | NAU8825_I2S_DRV_MASK |
  518. NAU8825_I2S_BLK_DIV_MASK, NAU8825_I2S_MS_MASTER |
  519. (0x2 << NAU8825_I2S_DRV_SFT) | 0x1);
  520. /* Ramp up headphone volume to 0dB to get better performance and
  521. * avoid pop noise in headphone.
  522. */
  523. index = nau8825_xtalk_baktab_index_by_reg(NAU8825_REG_HSVOL_CTRL);
  524. if (index != -EINVAL) {
  525. volume = nau8825_xtalk_baktab[index].def &
  526. NAU8825_HPR_VOL_MASK;
  527. nau8825_hpvol_ramp(nau8825, volume, 0, 3);
  528. }
  529. nau8825_xtalk_clock(nau8825);
  530. nau8825_xtalk_prepare_dac(nau8825);
  531. nau8825_xtalk_prepare_adc(nau8825);
  532. /* Config channel path and digital gain */
  533. regmap_update_bits(nau8825->regmap, NAU8825_REG_DACL_CTRL,
  534. NAU8825_DACL_CH_SEL_MASK | NAU8825_DACL_CH_VOL_MASK,
  535. NAU8825_DACL_CH_SEL_L | 0xab);
  536. regmap_update_bits(nau8825->regmap, NAU8825_REG_DACR_CTRL,
  537. NAU8825_DACR_CH_SEL_MASK | NAU8825_DACR_CH_VOL_MASK,
  538. NAU8825_DACR_CH_SEL_R | 0xab);
  539. /* Config cross talk parameters and generate the 23Hz sine wave with
  540. * 1/16 full scale of signal level for impedance measurement.
  541. */
  542. regmap_update_bits(nau8825->regmap, NAU8825_REG_IMM_MODE_CTRL,
  543. NAU8825_IMM_THD_MASK | NAU8825_IMM_GEN_VOL_MASK |
  544. NAU8825_IMM_CYC_MASK | NAU8825_IMM_DAC_SRC_MASK,
  545. (0x9 << NAU8825_IMM_THD_SFT) | NAU8825_IMM_GEN_VOL_1_16th |
  546. NAU8825_IMM_CYC_8192 | NAU8825_IMM_DAC_SRC_SIN);
  547. /* RMS intrruption enable */
  548. regmap_update_bits(nau8825->regmap,
  549. NAU8825_REG_INTERRUPT_MASK, NAU8825_IRQ_RMS_EN, 0);
  550. /* Power up left and right DAC */
  551. regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
  552. NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL, 0);
  553. }
  554. static void nau8825_xtalk_clean_dac(struct nau8825 *nau8825)
  555. {
  556. /* Disable HP boost driver */
  557. regmap_update_bits(nau8825->regmap, NAU8825_REG_BOOST,
  558. NAU8825_HP_BOOST_DIS, 0);
  559. /* HP outputs shouted to ground */
  560. regmap_update_bits(nau8825->regmap, NAU8825_REG_HSD_CTRL,
  561. NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L,
  562. NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L);
  563. /* Power down left and right DAC */
  564. regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
  565. NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL,
  566. NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL);
  567. /* Enable the TESTDAC and disable L/R HP impedance */
  568. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  569. NAU8825_BIAS_HPR_IMP | NAU8825_BIAS_HPL_IMP |
  570. NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
  571. /* Power down output driver with 2 stage */
  572. regmap_update_bits(nau8825->regmap, NAU8825_REG_POWER_UP_CONTROL,
  573. NAU8825_POWERUP_HP_DRV_R | NAU8825_POWERUP_HP_DRV_L, 0);
  574. regmap_update_bits(nau8825->regmap, NAU8825_REG_POWER_UP_CONTROL,
  575. NAU8825_POWERUP_INTEGR_R | NAU8825_POWERUP_INTEGR_L |
  576. NAU8825_POWERUP_DRV_IN_R | NAU8825_POWERUP_DRV_IN_L, 0);
  577. /* Disable clock sync of DAC and DAC clock */
  578. regmap_update_bits(nau8825->regmap, NAU8825_REG_RDAC,
  579. NAU8825_RDAC_EN | NAU8825_RDAC_CLK_EN, 0);
  580. /* Disable charge pump ramp up function and change bump */
  581. regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
  582. NAU8825_JAMNODCLOW | NAU8825_CHANRGE_PUMP_EN, 0);
  583. /* Disable power of DAC path */
  584. regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
  585. NAU8825_ENABLE_DACR | NAU8825_ENABLE_DACL |
  586. NAU8825_ENABLE_ADC_CLK | NAU8825_ENABLE_DAC_CLK, 0);
  587. if (!nau8825->irq)
  588. regmap_update_bits(nau8825->regmap,
  589. NAU8825_REG_ENA_CTRL, NAU8825_ENABLE_ADC, 0);
  590. }
  591. static void nau8825_xtalk_clean_adc(struct nau8825 *nau8825)
  592. {
  593. /* Power down left ADC and restore voltage to Vmid */
  594. regmap_update_bits(nau8825->regmap, NAU8825_REG_ANALOG_ADC_2,
  595. NAU8825_POWERUP_ADCL | NAU8825_ADC_VREFSEL_MASK, 0);
  596. }
  597. static void nau8825_xtalk_clean(struct nau8825 *nau8825)
  598. {
  599. /* Enable internal VCO needed for interruptions */
  600. nau8825_configure_sysclk(nau8825, NAU8825_CLK_INTERNAL, 0);
  601. nau8825_xtalk_clean_dac(nau8825);
  602. nau8825_xtalk_clean_adc(nau8825);
  603. /* Clear cross talk parameters and disable */
  604. regmap_write(nau8825->regmap, NAU8825_REG_IMM_MODE_CTRL, 0);
  605. /* RMS intrruption disable */
  606. regmap_update_bits(nau8825->regmap, NAU8825_REG_INTERRUPT_MASK,
  607. NAU8825_IRQ_RMS_EN, NAU8825_IRQ_RMS_EN);
  608. /* Recover default value for IIS */
  609. regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
  610. NAU8825_I2S_MS_MASK | NAU8825_I2S_DRV_MASK |
  611. NAU8825_I2S_BLK_DIV_MASK, NAU8825_I2S_MS_SLAVE);
  612. /* Restore value of specific register for cross talk */
  613. nau8825_xtalk_restore(nau8825);
  614. }
  615. static void nau8825_xtalk_imm_start(struct nau8825 *nau8825, int vol)
  616. {
  617. /* Apply ADC volume for better cross talk performance */
  618. regmap_update_bits(nau8825->regmap, NAU8825_REG_ADC_DGAIN_CTRL,
  619. NAU8825_ADC_DIG_VOL_MASK, vol);
  620. /* Disables JKTIP(HPL) DAC channel for right to left measurement.
  621. * Do it before sending signal in order to erase pop noise.
  622. */
  623. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  624. NAU8825_BIAS_TESTDACR_EN | NAU8825_BIAS_TESTDACL_EN,
  625. NAU8825_BIAS_TESTDACL_EN);
  626. switch (nau8825->xtalk_state) {
  627. case NAU8825_XTALK_HPR_R2L:
  628. /* Enable right headphone impedance */
  629. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  630. NAU8825_BIAS_HPR_IMP | NAU8825_BIAS_HPL_IMP,
  631. NAU8825_BIAS_HPR_IMP);
  632. break;
  633. case NAU8825_XTALK_HPL_R2L:
  634. /* Enable left headphone impedance */
  635. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  636. NAU8825_BIAS_HPR_IMP | NAU8825_BIAS_HPL_IMP,
  637. NAU8825_BIAS_HPL_IMP);
  638. break;
  639. default:
  640. break;
  641. }
  642. msleep(100);
  643. /* Impedance measurement mode enable */
  644. regmap_update_bits(nau8825->regmap, NAU8825_REG_IMM_MODE_CTRL,
  645. NAU8825_IMM_EN, NAU8825_IMM_EN);
  646. }
  647. static void nau8825_xtalk_imm_stop(struct nau8825 *nau8825)
  648. {
  649. /* Impedance measurement mode disable */
  650. regmap_update_bits(nau8825->regmap,
  651. NAU8825_REG_IMM_MODE_CTRL, NAU8825_IMM_EN, 0);
  652. }
  653. /* The cross talk measurement function can reduce cross talk across the
  654. * JKTIP(HPL) and JKR1(HPR) outputs which measures the cross talk signal
  655. * level to determine what cross talk reduction gain is. This system works by
  656. * sending a 23Hz -24dBV sine wave into the headset output DAC and through
  657. * the PGA. The output of the PGA is then connected to an internal current
  658. * sense which measures the attenuated 23Hz signal and passing the output to
  659. * an ADC which converts the measurement to a binary code. With two separated
  660. * measurement, one for JKR1(HPR) and the other JKTIP(HPL), measurement data
  661. * can be separated read in IMM_RMS_L for HSR and HSL after each measurement.
  662. * Thus, the measurement function has four states to complete whole sequence.
  663. * 1. Prepare state : Prepare the resource for detection and transfer to HPR
  664. * IMM stat to make JKR1(HPR) impedance measure.
  665. * 2. HPR IMM state : Read out orignal signal level of JKR1(HPR) and transfer
  666. * to HPL IMM state to make JKTIP(HPL) impedance measure.
  667. * 3. HPL IMM state : Read out cross talk signal level of JKTIP(HPL) and
  668. * transfer to IMM state to determine suppression sidetone gain.
  669. * 4. IMM state : Computes cross talk suppression sidetone gain with orignal
  670. * and cross talk signal level. Apply this gain and then restore codec
  671. * configuration. Then transfer to Done state for ending.
  672. */
  673. static void nau8825_xtalk_measure(struct nau8825 *nau8825)
  674. {
  675. u32 sidetone;
  676. switch (nau8825->xtalk_state) {
  677. case NAU8825_XTALK_PREPARE:
  678. /* In prepare state, set up clock, intrruption, DAC path, ADC
  679. * path and cross talk detection parameters for preparation.
  680. */
  681. nau8825_xtalk_prepare(nau8825);
  682. msleep(280);
  683. /* Trigger right headphone impedance detection */
  684. nau8825->xtalk_state = NAU8825_XTALK_HPR_R2L;
  685. nau8825_xtalk_imm_start(nau8825, 0x00d2);
  686. break;
  687. case NAU8825_XTALK_HPR_R2L:
  688. /* In right headphone IMM state, read out right headphone
  689. * impedance measure result, and then start up left side.
  690. */
  691. regmap_read(nau8825->regmap, NAU8825_REG_IMM_RMS_L,
  692. &nau8825->imp_rms[NAU8825_XTALK_HPR_R2L]);
  693. dev_dbg(nau8825->dev, "HPR_R2L imm: %x\n",
  694. nau8825->imp_rms[NAU8825_XTALK_HPR_R2L]);
  695. /* Disable then re-enable IMM mode to update */
  696. nau8825_xtalk_imm_stop(nau8825);
  697. /* Trigger left headphone impedance detection */
  698. nau8825->xtalk_state = NAU8825_XTALK_HPL_R2L;
  699. nau8825_xtalk_imm_start(nau8825, 0x00ff);
  700. break;
  701. case NAU8825_XTALK_HPL_R2L:
  702. /* In left headphone IMM state, read out left headphone
  703. * impedance measure result, and delay some time to wait
  704. * detection sine wave output finish. Then, we can calculate
  705. * the cross talk suppresstion side tone according to the L/R
  706. * headphone imedance.
  707. */
  708. regmap_read(nau8825->regmap, NAU8825_REG_IMM_RMS_L,
  709. &nau8825->imp_rms[NAU8825_XTALK_HPL_R2L]);
  710. dev_dbg(nau8825->dev, "HPL_R2L imm: %x\n",
  711. nau8825->imp_rms[NAU8825_XTALK_HPL_R2L]);
  712. nau8825_xtalk_imm_stop(nau8825);
  713. msleep(150);
  714. nau8825->xtalk_state = NAU8825_XTALK_IMM;
  715. break;
  716. case NAU8825_XTALK_IMM:
  717. /* In impedance measure state, the orignal and cross talk
  718. * signal level vlues are ready. The side tone gain is deter-
  719. * mined with these signal level. After all, restore codec
  720. * configuration.
  721. */
  722. sidetone = nau8825_xtalk_sidetone(
  723. nau8825->imp_rms[NAU8825_XTALK_HPR_R2L],
  724. nau8825->imp_rms[NAU8825_XTALK_HPL_R2L]);
  725. dev_dbg(nau8825->dev, "cross talk sidetone: %x\n", sidetone);
  726. regmap_write(nau8825->regmap, NAU8825_REG_DAC_DGAIN_CTRL,
  727. (sidetone << 8) | sidetone);
  728. nau8825_xtalk_clean(nau8825);
  729. nau8825->xtalk_state = NAU8825_XTALK_DONE;
  730. break;
  731. default:
  732. break;
  733. }
  734. }
  735. static void nau8825_xtalk_work(struct work_struct *work)
  736. {
  737. struct nau8825 *nau8825 = container_of(
  738. work, struct nau8825, xtalk_work);
  739. nau8825_xtalk_measure(nau8825);
  740. /* To determine the cross talk side tone gain when reach
  741. * the impedance measure state.
  742. */
  743. if (nau8825->xtalk_state == NAU8825_XTALK_IMM)
  744. nau8825_xtalk_measure(nau8825);
  745. /* Delay jack report until cross talk detection process
  746. * completed. It can avoid application to do playback
  747. * preparation before cross talk detection is still working.
  748. * Meanwhile, the protection of the cross talk detection
  749. * is released.
  750. */
  751. if (nau8825->xtalk_state == NAU8825_XTALK_DONE) {
  752. snd_soc_jack_report(nau8825->jack, nau8825->xtalk_event,
  753. nau8825->xtalk_event_mask);
  754. nau8825_sema_release(nau8825);
  755. nau8825->xtalk_protect = false;
  756. }
  757. }
  758. static void nau8825_xtalk_cancel(struct nau8825 *nau8825)
  759. {
  760. /* If the xtalk_protect is true, that means the process is still
  761. * on going. The driver forces to cancel the cross talk task and
  762. * restores the configuration to original status.
  763. */
  764. if (nau8825->xtalk_protect) {
  765. cancel_work_sync(&nau8825->xtalk_work);
  766. nau8825_xtalk_clean(nau8825);
  767. }
  768. /* Reset parameters for cross talk suppression function */
  769. nau8825_sema_reset(nau8825);
  770. nau8825->xtalk_state = NAU8825_XTALK_DONE;
  771. nau8825->xtalk_protect = false;
  772. }
  773. static bool nau8825_readable_reg(struct device *dev, unsigned int reg)
  774. {
  775. switch (reg) {
  776. case NAU8825_REG_ENA_CTRL ... NAU8825_REG_FLL_VCO_RSV:
  777. case NAU8825_REG_HSD_CTRL ... NAU8825_REG_JACK_DET_CTRL:
  778. case NAU8825_REG_INTERRUPT_MASK ... NAU8825_REG_KEYDET_CTRL:
  779. case NAU8825_REG_VDET_THRESHOLD_1 ... NAU8825_REG_DACR_CTRL:
  780. case NAU8825_REG_ADC_DRC_KNEE_IP12 ... NAU8825_REG_ADC_DRC_ATKDCY:
  781. case NAU8825_REG_DAC_DRC_KNEE_IP12 ... NAU8825_REG_DAC_DRC_ATKDCY:
  782. case NAU8825_REG_IMM_MODE_CTRL ... NAU8825_REG_IMM_RMS_R:
  783. case NAU8825_REG_CLASSG_CTRL ... NAU8825_REG_OPT_EFUSE_CTRL:
  784. case NAU8825_REG_MISC_CTRL:
  785. case NAU8825_REG_I2C_DEVICE_ID ... NAU8825_REG_SARDOUT_RAM_STATUS:
  786. case NAU8825_REG_BIAS_ADJ:
  787. case NAU8825_REG_TRIM_SETTINGS ... NAU8825_REG_ANALOG_CONTROL_2:
  788. case NAU8825_REG_ANALOG_ADC_1 ... NAU8825_REG_MIC_BIAS:
  789. case NAU8825_REG_BOOST ... NAU8825_REG_FEPGA:
  790. case NAU8825_REG_POWER_UP_CONTROL ... NAU8825_REG_GENERAL_STATUS:
  791. return true;
  792. default:
  793. return false;
  794. }
  795. }
  796. static bool nau8825_writeable_reg(struct device *dev, unsigned int reg)
  797. {
  798. switch (reg) {
  799. case NAU8825_REG_RESET ... NAU8825_REG_FLL_VCO_RSV:
  800. case NAU8825_REG_HSD_CTRL ... NAU8825_REG_JACK_DET_CTRL:
  801. case NAU8825_REG_INTERRUPT_MASK:
  802. case NAU8825_REG_INT_CLR_KEY_STATUS ... NAU8825_REG_KEYDET_CTRL:
  803. case NAU8825_REG_VDET_THRESHOLD_1 ... NAU8825_REG_DACR_CTRL:
  804. case NAU8825_REG_ADC_DRC_KNEE_IP12 ... NAU8825_REG_ADC_DRC_ATKDCY:
  805. case NAU8825_REG_DAC_DRC_KNEE_IP12 ... NAU8825_REG_DAC_DRC_ATKDCY:
  806. case NAU8825_REG_IMM_MODE_CTRL:
  807. case NAU8825_REG_CLASSG_CTRL ... NAU8825_REG_OPT_EFUSE_CTRL:
  808. case NAU8825_REG_MISC_CTRL:
  809. case NAU8825_REG_BIAS_ADJ:
  810. case NAU8825_REG_TRIM_SETTINGS ... NAU8825_REG_ANALOG_CONTROL_2:
  811. case NAU8825_REG_ANALOG_ADC_1 ... NAU8825_REG_MIC_BIAS:
  812. case NAU8825_REG_BOOST ... NAU8825_REG_FEPGA:
  813. case NAU8825_REG_POWER_UP_CONTROL ... NAU8825_REG_CHARGE_PUMP:
  814. return true;
  815. default:
  816. return false;
  817. }
  818. }
  819. static bool nau8825_volatile_reg(struct device *dev, unsigned int reg)
  820. {
  821. switch (reg) {
  822. case NAU8825_REG_RESET:
  823. case NAU8825_REG_IRQ_STATUS:
  824. case NAU8825_REG_INT_CLR_KEY_STATUS:
  825. case NAU8825_REG_IMM_RMS_L:
  826. case NAU8825_REG_IMM_RMS_R:
  827. case NAU8825_REG_I2C_DEVICE_ID:
  828. case NAU8825_REG_SARDOUT_RAM_STATUS:
  829. case NAU8825_REG_CHARGE_PUMP_INPUT_READ:
  830. case NAU8825_REG_GENERAL_STATUS:
  831. case NAU8825_REG_BIQ_CTRL ... NAU8825_REG_BIQ_COF10:
  832. return true;
  833. default:
  834. return false;
  835. }
  836. }
  837. static int nau8825_adc_event(struct snd_soc_dapm_widget *w,
  838. struct snd_kcontrol *kcontrol, int event)
  839. {
  840. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  841. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  842. switch (event) {
  843. case SND_SOC_DAPM_POST_PMU:
  844. regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
  845. NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC);
  846. break;
  847. case SND_SOC_DAPM_POST_PMD:
  848. if (!nau8825->irq)
  849. regmap_update_bits(nau8825->regmap,
  850. NAU8825_REG_ENA_CTRL, NAU8825_ENABLE_ADC, 0);
  851. break;
  852. default:
  853. return -EINVAL;
  854. }
  855. return 0;
  856. }
  857. static int nau8825_pump_event(struct snd_soc_dapm_widget *w,
  858. struct snd_kcontrol *kcontrol, int event)
  859. {
  860. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  861. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  862. switch (event) {
  863. case SND_SOC_DAPM_POST_PMU:
  864. /* Prevent startup click by letting charge pump to ramp up */
  865. msleep(10);
  866. regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
  867. NAU8825_JAMNODCLOW, NAU8825_JAMNODCLOW);
  868. break;
  869. case SND_SOC_DAPM_PRE_PMD:
  870. regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
  871. NAU8825_JAMNODCLOW, 0);
  872. break;
  873. default:
  874. return -EINVAL;
  875. }
  876. return 0;
  877. }
  878. static int nau8825_output_dac_event(struct snd_soc_dapm_widget *w,
  879. struct snd_kcontrol *kcontrol, int event)
  880. {
  881. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  882. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  883. switch (event) {
  884. case SND_SOC_DAPM_PRE_PMU:
  885. /* Disables the TESTDAC to let DAC signal pass through. */
  886. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  887. NAU8825_BIAS_TESTDAC_EN, 0);
  888. break;
  889. case SND_SOC_DAPM_POST_PMD:
  890. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  891. NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
  892. break;
  893. default:
  894. return -EINVAL;
  895. }
  896. return 0;
  897. }
  898. static int nau8825_biq_coeff_get(struct snd_kcontrol *kcontrol,
  899. struct snd_ctl_elem_value *ucontrol)
  900. {
  901. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  902. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  903. if (!component->regmap)
  904. return -EINVAL;
  905. regmap_raw_read(component->regmap, NAU8825_REG_BIQ_COF1,
  906. ucontrol->value.bytes.data, params->max);
  907. return 0;
  908. }
  909. static int nau8825_biq_coeff_put(struct snd_kcontrol *kcontrol,
  910. struct snd_ctl_elem_value *ucontrol)
  911. {
  912. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  913. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  914. void *data;
  915. if (!component->regmap)
  916. return -EINVAL;
  917. data = kmemdup(ucontrol->value.bytes.data,
  918. params->max, GFP_KERNEL | GFP_DMA);
  919. if (!data)
  920. return -ENOMEM;
  921. regmap_update_bits(component->regmap, NAU8825_REG_BIQ_CTRL,
  922. NAU8825_BIQ_WRT_EN, 0);
  923. regmap_raw_write(component->regmap, NAU8825_REG_BIQ_COF1,
  924. data, params->max);
  925. regmap_update_bits(component->regmap, NAU8825_REG_BIQ_CTRL,
  926. NAU8825_BIQ_WRT_EN, NAU8825_BIQ_WRT_EN);
  927. kfree(data);
  928. return 0;
  929. }
  930. static const char * const nau8825_biq_path[] = {
  931. "ADC", "DAC"
  932. };
  933. static const struct soc_enum nau8825_biq_path_enum =
  934. SOC_ENUM_SINGLE(NAU8825_REG_BIQ_CTRL, NAU8825_BIQ_PATH_SFT,
  935. ARRAY_SIZE(nau8825_biq_path), nau8825_biq_path);
  936. static const char * const nau8825_adc_decimation[] = {
  937. "32", "64", "128", "256"
  938. };
  939. static const struct soc_enum nau8825_adc_decimation_enum =
  940. SOC_ENUM_SINGLE(NAU8825_REG_ADC_RATE, NAU8825_ADC_SYNC_DOWN_SFT,
  941. ARRAY_SIZE(nau8825_adc_decimation), nau8825_adc_decimation);
  942. static const char * const nau8825_dac_oversampl[] = {
  943. "64", "256", "128", "", "32"
  944. };
  945. static const struct soc_enum nau8825_dac_oversampl_enum =
  946. SOC_ENUM_SINGLE(NAU8825_REG_DAC_CTRL1, NAU8825_DAC_OVERSAMPLE_SFT,
  947. ARRAY_SIZE(nau8825_dac_oversampl), nau8825_dac_oversampl);
  948. static const DECLARE_TLV_DB_MINMAX_MUTE(adc_vol_tlv, -10300, 2400);
  949. static const DECLARE_TLV_DB_MINMAX_MUTE(sidetone_vol_tlv, -4200, 0);
  950. static const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -5400, 0);
  951. static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600);
  952. static const DECLARE_TLV_DB_MINMAX_MUTE(crosstalk_vol_tlv, -9600, 2400);
  953. static const struct snd_kcontrol_new nau8825_controls[] = {
  954. SOC_SINGLE_TLV("Mic Volume", NAU8825_REG_ADC_DGAIN_CTRL,
  955. 0, 0xff, 0, adc_vol_tlv),
  956. SOC_DOUBLE_TLV("Headphone Bypass Volume", NAU8825_REG_ADC_DGAIN_CTRL,
  957. 12, 8, 0x0f, 0, sidetone_vol_tlv),
  958. SOC_DOUBLE_TLV("Headphone Volume", NAU8825_REG_HSVOL_CTRL,
  959. 6, 0, 0x3f, 1, dac_vol_tlv),
  960. SOC_SINGLE_TLV("Frontend PGA Volume", NAU8825_REG_POWER_UP_CONTROL,
  961. 8, 37, 0, fepga_gain_tlv),
  962. SOC_DOUBLE_TLV("Headphone Crosstalk Volume", NAU8825_REG_DAC_DGAIN_CTRL,
  963. 0, 8, 0xff, 0, crosstalk_vol_tlv),
  964. SOC_ENUM("ADC Decimation Rate", nau8825_adc_decimation_enum),
  965. SOC_ENUM("DAC Oversampling Rate", nau8825_dac_oversampl_enum),
  966. /* programmable biquad filter */
  967. SOC_ENUM("BIQ Path Select", nau8825_biq_path_enum),
  968. SND_SOC_BYTES_EXT("BIQ Coefficients", 20,
  969. nau8825_biq_coeff_get, nau8825_biq_coeff_put),
  970. };
  971. /* DAC Mux 0x33[9] and 0x34[9] */
  972. static const char * const nau8825_dac_src[] = {
  973. "DACL", "DACR",
  974. };
  975. static SOC_ENUM_SINGLE_DECL(
  976. nau8825_dacl_enum, NAU8825_REG_DACL_CTRL,
  977. NAU8825_DACL_CH_SEL_SFT, nau8825_dac_src);
  978. static SOC_ENUM_SINGLE_DECL(
  979. nau8825_dacr_enum, NAU8825_REG_DACR_CTRL,
  980. NAU8825_DACR_CH_SEL_SFT, nau8825_dac_src);
  981. static const struct snd_kcontrol_new nau8825_dacl_mux =
  982. SOC_DAPM_ENUM("DACL Source", nau8825_dacl_enum);
  983. static const struct snd_kcontrol_new nau8825_dacr_mux =
  984. SOC_DAPM_ENUM("DACR Source", nau8825_dacr_enum);
  985. static const struct snd_soc_dapm_widget nau8825_dapm_widgets[] = {
  986. SND_SOC_DAPM_AIF_OUT("AIFTX", "Capture", 0, NAU8825_REG_I2S_PCM_CTRL2,
  987. 15, 1),
  988. SND_SOC_DAPM_INPUT("MIC"),
  989. SND_SOC_DAPM_MICBIAS("MICBIAS", NAU8825_REG_MIC_BIAS, 8, 0),
  990. SND_SOC_DAPM_PGA("Frontend PGA", NAU8825_REG_POWER_UP_CONTROL, 14, 0,
  991. NULL, 0),
  992. SND_SOC_DAPM_ADC_E("ADC", NULL, SND_SOC_NOPM, 0, 0,
  993. nau8825_adc_event, SND_SOC_DAPM_POST_PMU |
  994. SND_SOC_DAPM_POST_PMD),
  995. SND_SOC_DAPM_SUPPLY("ADC Clock", NAU8825_REG_ENA_CTRL, 7, 0, NULL, 0),
  996. SND_SOC_DAPM_SUPPLY("ADC Power", NAU8825_REG_ANALOG_ADC_2, 6, 0, NULL,
  997. 0),
  998. /* ADC for button press detection. A dapm supply widget is used to
  999. * prevent dapm_power_widgets keeping the codec at SND_SOC_BIAS_ON
  1000. * during suspend.
  1001. */
  1002. SND_SOC_DAPM_SUPPLY("SAR", NAU8825_REG_SAR_CTRL,
  1003. NAU8825_SAR_ADC_EN_SFT, 0, NULL, 0),
  1004. SND_SOC_DAPM_PGA_S("ADACL", 2, NAU8825_REG_RDAC, 12, 0, NULL, 0),
  1005. SND_SOC_DAPM_PGA_S("ADACR", 2, NAU8825_REG_RDAC, 13, 0, NULL, 0),
  1006. SND_SOC_DAPM_PGA_S("ADACL Clock", 3, NAU8825_REG_RDAC, 8, 0, NULL, 0),
  1007. SND_SOC_DAPM_PGA_S("ADACR Clock", 3, NAU8825_REG_RDAC, 9, 0, NULL, 0),
  1008. SND_SOC_DAPM_DAC("DDACR", NULL, NAU8825_REG_ENA_CTRL,
  1009. NAU8825_ENABLE_DACR_SFT, 0),
  1010. SND_SOC_DAPM_DAC("DDACL", NULL, NAU8825_REG_ENA_CTRL,
  1011. NAU8825_ENABLE_DACL_SFT, 0),
  1012. SND_SOC_DAPM_SUPPLY("DDAC Clock", NAU8825_REG_ENA_CTRL, 6, 0, NULL, 0),
  1013. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacl_mux),
  1014. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacr_mux),
  1015. SND_SOC_DAPM_PGA_S("HP amp L", 0,
  1016. NAU8825_REG_CLASSG_CTRL, 1, 0, NULL, 0),
  1017. SND_SOC_DAPM_PGA_S("HP amp R", 0,
  1018. NAU8825_REG_CLASSG_CTRL, 2, 0, NULL, 0),
  1019. SND_SOC_DAPM_PGA_S("Charge Pump", 1, NAU8825_REG_CHARGE_PUMP, 5, 0,
  1020. nau8825_pump_event, SND_SOC_DAPM_POST_PMU |
  1021. SND_SOC_DAPM_PRE_PMD),
  1022. SND_SOC_DAPM_PGA_S("Output Driver R Stage 1", 4,
  1023. NAU8825_REG_POWER_UP_CONTROL, 5, 0, NULL, 0),
  1024. SND_SOC_DAPM_PGA_S("Output Driver L Stage 1", 4,
  1025. NAU8825_REG_POWER_UP_CONTROL, 4, 0, NULL, 0),
  1026. SND_SOC_DAPM_PGA_S("Output Driver R Stage 2", 5,
  1027. NAU8825_REG_POWER_UP_CONTROL, 3, 0, NULL, 0),
  1028. SND_SOC_DAPM_PGA_S("Output Driver L Stage 2", 5,
  1029. NAU8825_REG_POWER_UP_CONTROL, 2, 0, NULL, 0),
  1030. SND_SOC_DAPM_PGA_S("Output Driver R Stage 3", 6,
  1031. NAU8825_REG_POWER_UP_CONTROL, 1, 0, NULL, 0),
  1032. SND_SOC_DAPM_PGA_S("Output Driver L Stage 3", 6,
  1033. NAU8825_REG_POWER_UP_CONTROL, 0, 0, NULL, 0),
  1034. SND_SOC_DAPM_PGA_S("Output DACL", 7,
  1035. NAU8825_REG_CHARGE_PUMP, 8, 1, nau8825_output_dac_event,
  1036. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1037. SND_SOC_DAPM_PGA_S("Output DACR", 7,
  1038. NAU8825_REG_CHARGE_PUMP, 9, 1, nau8825_output_dac_event,
  1039. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1040. /* HPOL/R are ungrounded by disabling 16 Ohm pull-downs on playback */
  1041. SND_SOC_DAPM_PGA_S("HPOL Pulldown", 8,
  1042. NAU8825_REG_HSD_CTRL, 0, 1, NULL, 0),
  1043. SND_SOC_DAPM_PGA_S("HPOR Pulldown", 8,
  1044. NAU8825_REG_HSD_CTRL, 1, 1, NULL, 0),
  1045. /* High current HPOL/R boost driver */
  1046. SND_SOC_DAPM_PGA_S("HP Boost Driver", 9,
  1047. NAU8825_REG_BOOST, 9, 1, NULL, 0),
  1048. /* Class G operation control*/
  1049. SND_SOC_DAPM_PGA_S("Class G", 10,
  1050. NAU8825_REG_CLASSG_CTRL, 0, 0, NULL, 0),
  1051. SND_SOC_DAPM_OUTPUT("HPOL"),
  1052. SND_SOC_DAPM_OUTPUT("HPOR"),
  1053. };
  1054. static const struct snd_soc_dapm_route nau8825_dapm_routes[] = {
  1055. {"Frontend PGA", NULL, "MIC"},
  1056. {"ADC", NULL, "Frontend PGA"},
  1057. {"ADC", NULL, "ADC Clock"},
  1058. {"ADC", NULL, "ADC Power"},
  1059. {"AIFTX", NULL, "ADC"},
  1060. {"DDACL", NULL, "Playback"},
  1061. {"DDACR", NULL, "Playback"},
  1062. {"DDACL", NULL, "DDAC Clock"},
  1063. {"DDACR", NULL, "DDAC Clock"},
  1064. {"DACL Mux", "DACL", "DDACL"},
  1065. {"DACL Mux", "DACR", "DDACR"},
  1066. {"DACR Mux", "DACL", "DDACL"},
  1067. {"DACR Mux", "DACR", "DDACR"},
  1068. {"HP amp L", NULL, "DACL Mux"},
  1069. {"HP amp R", NULL, "DACR Mux"},
  1070. {"Charge Pump", NULL, "HP amp L"},
  1071. {"Charge Pump", NULL, "HP amp R"},
  1072. {"ADACL", NULL, "Charge Pump"},
  1073. {"ADACR", NULL, "Charge Pump"},
  1074. {"ADACL Clock", NULL, "ADACL"},
  1075. {"ADACR Clock", NULL, "ADACR"},
  1076. {"Output Driver L Stage 1", NULL, "ADACL Clock"},
  1077. {"Output Driver R Stage 1", NULL, "ADACR Clock"},
  1078. {"Output Driver L Stage 2", NULL, "Output Driver L Stage 1"},
  1079. {"Output Driver R Stage 2", NULL, "Output Driver R Stage 1"},
  1080. {"Output Driver L Stage 3", NULL, "Output Driver L Stage 2"},
  1081. {"Output Driver R Stage 3", NULL, "Output Driver R Stage 2"},
  1082. {"Output DACL", NULL, "Output Driver L Stage 3"},
  1083. {"Output DACR", NULL, "Output Driver R Stage 3"},
  1084. {"HPOL Pulldown", NULL, "Output DACL"},
  1085. {"HPOR Pulldown", NULL, "Output DACR"},
  1086. {"HP Boost Driver", NULL, "HPOL Pulldown"},
  1087. {"HP Boost Driver", NULL, "HPOR Pulldown"},
  1088. {"Class G", NULL, "HP Boost Driver"},
  1089. {"HPOL", NULL, "Class G"},
  1090. {"HPOR", NULL, "Class G"},
  1091. };
  1092. static int nau8825_clock_check(struct nau8825 *nau8825,
  1093. int stream, int rate, int osr)
  1094. {
  1095. int osrate;
  1096. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1097. if (osr >= ARRAY_SIZE(osr_dac_sel))
  1098. return -EINVAL;
  1099. osrate = osr_dac_sel[osr].osr;
  1100. } else {
  1101. if (osr >= ARRAY_SIZE(osr_adc_sel))
  1102. return -EINVAL;
  1103. osrate = osr_adc_sel[osr].osr;
  1104. }
  1105. if (!osrate || rate * osr > CLK_DA_AD_MAX) {
  1106. dev_err(nau8825->dev, "exceed the maximum frequency of CLK_ADC or CLK_DAC\n");
  1107. return -EINVAL;
  1108. }
  1109. return 0;
  1110. }
  1111. static int nau8825_hw_params(struct snd_pcm_substream *substream,
  1112. struct snd_pcm_hw_params *params,
  1113. struct snd_soc_dai *dai)
  1114. {
  1115. struct snd_soc_codec *codec = dai->codec;
  1116. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1117. unsigned int val_len = 0, osr;
  1118. nau8825_sema_acquire(nau8825, 3 * HZ);
  1119. /* CLK_DAC or CLK_ADC = OSR * FS
  1120. * DAC or ADC clock frequency is defined as Over Sampling Rate (OSR)
  1121. * multiplied by the audio sample rate (Fs). Note that the OSR and Fs
  1122. * values must be selected such that the maximum frequency is less
  1123. * than 6.144 MHz.
  1124. */
  1125. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1126. regmap_read(nau8825->regmap, NAU8825_REG_DAC_CTRL1, &osr);
  1127. osr &= NAU8825_DAC_OVERSAMPLE_MASK;
  1128. if (nau8825_clock_check(nau8825, substream->stream,
  1129. params_rate(params), osr))
  1130. return -EINVAL;
  1131. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  1132. NAU8825_CLK_DAC_SRC_MASK,
  1133. osr_dac_sel[osr].clk_src << NAU8825_CLK_DAC_SRC_SFT);
  1134. } else {
  1135. regmap_read(nau8825->regmap, NAU8825_REG_ADC_RATE, &osr);
  1136. osr &= NAU8825_ADC_SYNC_DOWN_MASK;
  1137. if (nau8825_clock_check(nau8825, substream->stream,
  1138. params_rate(params), osr))
  1139. return -EINVAL;
  1140. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  1141. NAU8825_CLK_ADC_SRC_MASK,
  1142. osr_adc_sel[osr].clk_src << NAU8825_CLK_ADC_SRC_SFT);
  1143. }
  1144. switch (params_width(params)) {
  1145. case 16:
  1146. val_len |= NAU8825_I2S_DL_16;
  1147. break;
  1148. case 20:
  1149. val_len |= NAU8825_I2S_DL_20;
  1150. break;
  1151. case 24:
  1152. val_len |= NAU8825_I2S_DL_24;
  1153. break;
  1154. case 32:
  1155. val_len |= NAU8825_I2S_DL_32;
  1156. break;
  1157. default:
  1158. return -EINVAL;
  1159. }
  1160. regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1,
  1161. NAU8825_I2S_DL_MASK, val_len);
  1162. /* Release the semaphone. */
  1163. nau8825_sema_release(nau8825);
  1164. return 0;
  1165. }
  1166. static int nau8825_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  1167. {
  1168. struct snd_soc_codec *codec = codec_dai->codec;
  1169. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1170. unsigned int ctrl1_val = 0, ctrl2_val = 0;
  1171. nau8825_sema_acquire(nau8825, 3 * HZ);
  1172. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1173. case SND_SOC_DAIFMT_CBM_CFM:
  1174. ctrl2_val |= NAU8825_I2S_MS_MASTER;
  1175. break;
  1176. case SND_SOC_DAIFMT_CBS_CFS:
  1177. break;
  1178. default:
  1179. return -EINVAL;
  1180. }
  1181. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1182. case SND_SOC_DAIFMT_NB_NF:
  1183. break;
  1184. case SND_SOC_DAIFMT_IB_NF:
  1185. ctrl1_val |= NAU8825_I2S_BP_INV;
  1186. break;
  1187. default:
  1188. return -EINVAL;
  1189. }
  1190. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1191. case SND_SOC_DAIFMT_I2S:
  1192. ctrl1_val |= NAU8825_I2S_DF_I2S;
  1193. break;
  1194. case SND_SOC_DAIFMT_LEFT_J:
  1195. ctrl1_val |= NAU8825_I2S_DF_LEFT;
  1196. break;
  1197. case SND_SOC_DAIFMT_RIGHT_J:
  1198. ctrl1_val |= NAU8825_I2S_DF_RIGTH;
  1199. break;
  1200. case SND_SOC_DAIFMT_DSP_A:
  1201. ctrl1_val |= NAU8825_I2S_DF_PCM_AB;
  1202. break;
  1203. case SND_SOC_DAIFMT_DSP_B:
  1204. ctrl1_val |= NAU8825_I2S_DF_PCM_AB;
  1205. ctrl1_val |= NAU8825_I2S_PCMB_EN;
  1206. break;
  1207. default:
  1208. return -EINVAL;
  1209. }
  1210. regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1,
  1211. NAU8825_I2S_DL_MASK | NAU8825_I2S_DF_MASK |
  1212. NAU8825_I2S_BP_MASK | NAU8825_I2S_PCMB_MASK,
  1213. ctrl1_val);
  1214. regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
  1215. NAU8825_I2S_MS_MASK, ctrl2_val);
  1216. /* Release the semaphone. */
  1217. nau8825_sema_release(nau8825);
  1218. return 0;
  1219. }
  1220. static const struct snd_soc_dai_ops nau8825_dai_ops = {
  1221. .hw_params = nau8825_hw_params,
  1222. .set_fmt = nau8825_set_dai_fmt,
  1223. };
  1224. #define NAU8825_RATES SNDRV_PCM_RATE_8000_192000
  1225. #define NAU8825_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
  1226. | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  1227. static struct snd_soc_dai_driver nau8825_dai = {
  1228. .name = "nau8825-hifi",
  1229. .playback = {
  1230. .stream_name = "Playback",
  1231. .channels_min = 1,
  1232. .channels_max = 2,
  1233. .rates = NAU8825_RATES,
  1234. .formats = NAU8825_FORMATS,
  1235. },
  1236. .capture = {
  1237. .stream_name = "Capture",
  1238. .channels_min = 1,
  1239. .channels_max = 1,
  1240. .rates = NAU8825_RATES,
  1241. .formats = NAU8825_FORMATS,
  1242. },
  1243. .ops = &nau8825_dai_ops,
  1244. };
  1245. /**
  1246. * nau8825_enable_jack_detect - Specify a jack for event reporting
  1247. *
  1248. * @component: component to register the jack with
  1249. * @jack: jack to use to report headset and button events on
  1250. *
  1251. * After this function has been called the headset insert/remove and button
  1252. * events will be routed to the given jack. Jack can be null to stop
  1253. * reporting.
  1254. */
  1255. int nau8825_enable_jack_detect(struct snd_soc_codec *codec,
  1256. struct snd_soc_jack *jack)
  1257. {
  1258. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1259. struct regmap *regmap = nau8825->regmap;
  1260. nau8825->jack = jack;
  1261. /* Ground HP Outputs[1:0], needed for headset auto detection
  1262. * Enable Automatic Mic/Gnd switching reading on insert interrupt[6]
  1263. */
  1264. regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL,
  1265. NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L,
  1266. NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L);
  1267. return 0;
  1268. }
  1269. EXPORT_SYMBOL_GPL(nau8825_enable_jack_detect);
  1270. static bool nau8825_is_jack_inserted(struct regmap *regmap)
  1271. {
  1272. bool active_high, is_high;
  1273. int status, jkdet;
  1274. regmap_read(regmap, NAU8825_REG_JACK_DET_CTRL, &jkdet);
  1275. active_high = jkdet & NAU8825_JACK_POLARITY;
  1276. regmap_read(regmap, NAU8825_REG_I2C_DEVICE_ID, &status);
  1277. is_high = status & NAU8825_GPIO2JD1;
  1278. /* return jack connection status according to jack insertion logic
  1279. * active high or active low.
  1280. */
  1281. return active_high == is_high;
  1282. }
  1283. static void nau8825_restart_jack_detection(struct regmap *regmap)
  1284. {
  1285. /* this will restart the entire jack detection process including MIC/GND
  1286. * switching and create interrupts. We have to go from 0 to 1 and back
  1287. * to 0 to restart.
  1288. */
  1289. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1290. NAU8825_JACK_DET_RESTART, NAU8825_JACK_DET_RESTART);
  1291. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1292. NAU8825_JACK_DET_RESTART, 0);
  1293. }
  1294. static void nau8825_int_status_clear_all(struct regmap *regmap)
  1295. {
  1296. int active_irq, clear_irq, i;
  1297. /* Reset the intrruption status from rightmost bit if the corres-
  1298. * ponding irq event occurs.
  1299. */
  1300. regmap_read(regmap, NAU8825_REG_IRQ_STATUS, &active_irq);
  1301. for (i = 0; i < NAU8825_REG_DATA_LEN; i++) {
  1302. clear_irq = (0x1 << i);
  1303. if (active_irq & clear_irq)
  1304. regmap_write(regmap,
  1305. NAU8825_REG_INT_CLR_KEY_STATUS, clear_irq);
  1306. }
  1307. }
  1308. static void nau8825_eject_jack(struct nau8825 *nau8825)
  1309. {
  1310. struct snd_soc_dapm_context *dapm = nau8825->dapm;
  1311. struct regmap *regmap = nau8825->regmap;
  1312. /* Force to cancel the cross talk detection process */
  1313. nau8825_xtalk_cancel(nau8825);
  1314. snd_soc_dapm_disable_pin(dapm, "SAR");
  1315. snd_soc_dapm_disable_pin(dapm, "MICBIAS");
  1316. /* Detach 2kOhm Resistors from MICBIAS to MICGND1/2 */
  1317. regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
  1318. NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2, 0);
  1319. /* ground HPL/HPR, MICGRND1/2 */
  1320. regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 0xf, 0xf);
  1321. snd_soc_dapm_sync(dapm);
  1322. /* Clear all interruption status */
  1323. nau8825_int_status_clear_all(regmap);
  1324. /* Enable the insertion interruption, disable the ejection inter-
  1325. * ruption, and then bypass de-bounce circuit.
  1326. */
  1327. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_DIS_CTRL,
  1328. NAU8825_IRQ_EJECT_DIS | NAU8825_IRQ_INSERT_DIS,
  1329. NAU8825_IRQ_EJECT_DIS);
  1330. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  1331. NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_EJECT_EN |
  1332. NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_INSERT_EN,
  1333. NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_EJECT_EN |
  1334. NAU8825_IRQ_HEADSET_COMPLETE_EN);
  1335. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1336. NAU8825_JACK_DET_DB_BYPASS, NAU8825_JACK_DET_DB_BYPASS);
  1337. /* Disable ADC needed for interruptions at audo mode */
  1338. regmap_update_bits(regmap, NAU8825_REG_ENA_CTRL,
  1339. NAU8825_ENABLE_ADC, 0);
  1340. /* Close clock for jack type detection at manual mode */
  1341. nau8825_configure_sysclk(nau8825, NAU8825_CLK_DIS, 0);
  1342. }
  1343. /* Enable audo mode interruptions with internal clock. */
  1344. static void nau8825_setup_auto_irq(struct nau8825 *nau8825)
  1345. {
  1346. struct regmap *regmap = nau8825->regmap;
  1347. /* Enable headset jack type detection complete interruption and
  1348. * jack ejection interruption.
  1349. */
  1350. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  1351. NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_EJECT_EN, 0);
  1352. /* Enable internal VCO needed for interruptions */
  1353. nau8825_configure_sysclk(nau8825, NAU8825_CLK_INTERNAL, 0);
  1354. /* Enable ADC needed for interruptions */
  1355. regmap_update_bits(regmap, NAU8825_REG_ENA_CTRL,
  1356. NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC);
  1357. /* Chip needs one FSCLK cycle in order to generate interruptions,
  1358. * as we cannot guarantee one will be provided by the system. Turning
  1359. * master mode on then off enables us to generate that FSCLK cycle
  1360. * with a minimum of contention on the clock bus.
  1361. */
  1362. regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2,
  1363. NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_MASTER);
  1364. regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2,
  1365. NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_SLAVE);
  1366. /* Not bypass de-bounce circuit */
  1367. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1368. NAU8825_JACK_DET_DB_BYPASS, 0);
  1369. /* Unmask all interruptions */
  1370. regmap_write(regmap, NAU8825_REG_INTERRUPT_DIS_CTRL, 0);
  1371. /* Restart the jack detection process at auto mode */
  1372. nau8825_restart_jack_detection(regmap);
  1373. }
  1374. static int nau8825_button_decode(int value)
  1375. {
  1376. int buttons = 0;
  1377. /* The chip supports up to 8 buttons, but ALSA defines only 6 buttons */
  1378. if (value & BIT(0))
  1379. buttons |= SND_JACK_BTN_0;
  1380. if (value & BIT(1))
  1381. buttons |= SND_JACK_BTN_1;
  1382. if (value & BIT(2))
  1383. buttons |= SND_JACK_BTN_2;
  1384. if (value & BIT(3))
  1385. buttons |= SND_JACK_BTN_3;
  1386. if (value & BIT(4))
  1387. buttons |= SND_JACK_BTN_4;
  1388. if (value & BIT(5))
  1389. buttons |= SND_JACK_BTN_5;
  1390. return buttons;
  1391. }
  1392. static int nau8825_jack_insert(struct nau8825 *nau8825)
  1393. {
  1394. struct regmap *regmap = nau8825->regmap;
  1395. struct snd_soc_dapm_context *dapm = nau8825->dapm;
  1396. int jack_status_reg, mic_detected;
  1397. int type = 0;
  1398. regmap_read(regmap, NAU8825_REG_GENERAL_STATUS, &jack_status_reg);
  1399. mic_detected = (jack_status_reg >> 10) & 3;
  1400. /* The JKSLV and JKR2 all detected in high impedance headset */
  1401. if (mic_detected == 0x3)
  1402. nau8825->high_imped = true;
  1403. else
  1404. nau8825->high_imped = false;
  1405. switch (mic_detected) {
  1406. case 0:
  1407. /* no mic */
  1408. type = SND_JACK_HEADPHONE;
  1409. break;
  1410. case 1:
  1411. dev_dbg(nau8825->dev, "OMTP (micgnd1) mic connected\n");
  1412. type = SND_JACK_HEADSET;
  1413. /* Unground MICGND1 */
  1414. regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2,
  1415. 1 << 2);
  1416. /* Attach 2kOhm Resistor from MICBIAS to MICGND1 */
  1417. regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
  1418. NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2,
  1419. NAU8825_MICBIAS_JKR2);
  1420. /* Attach SARADC to MICGND1 */
  1421. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1422. NAU8825_SAR_INPUT_MASK,
  1423. NAU8825_SAR_INPUT_JKR2);
  1424. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
  1425. snd_soc_dapm_force_enable_pin(dapm, "SAR");
  1426. snd_soc_dapm_sync(dapm);
  1427. break;
  1428. case 2:
  1429. case 3:
  1430. dev_dbg(nau8825->dev, "CTIA (micgnd2) mic connected\n");
  1431. type = SND_JACK_HEADSET;
  1432. /* Unground MICGND2 */
  1433. regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2,
  1434. 2 << 2);
  1435. /* Attach 2kOhm Resistor from MICBIAS to MICGND2 */
  1436. regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
  1437. NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2,
  1438. NAU8825_MICBIAS_JKSLV);
  1439. /* Attach SARADC to MICGND2 */
  1440. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1441. NAU8825_SAR_INPUT_MASK,
  1442. NAU8825_SAR_INPUT_JKSLV);
  1443. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
  1444. snd_soc_dapm_force_enable_pin(dapm, "SAR");
  1445. snd_soc_dapm_sync(dapm);
  1446. break;
  1447. }
  1448. /* Leaving HPOL/R grounded after jack insert by default. They will be
  1449. * ungrounded as part of the widget power up sequence at the beginning
  1450. * of playback to reduce pop.
  1451. */
  1452. return type;
  1453. }
  1454. #define NAU8825_BUTTONS (SND_JACK_BTN_0 | SND_JACK_BTN_1 | \
  1455. SND_JACK_BTN_2 | SND_JACK_BTN_3)
  1456. static irqreturn_t nau8825_interrupt(int irq, void *data)
  1457. {
  1458. struct nau8825 *nau8825 = (struct nau8825 *)data;
  1459. struct regmap *regmap = nau8825->regmap;
  1460. int active_irq, clear_irq = 0, event = 0, event_mask = 0;
  1461. if (regmap_read(regmap, NAU8825_REG_IRQ_STATUS, &active_irq)) {
  1462. dev_err(nau8825->dev, "failed to read irq status\n");
  1463. return IRQ_NONE;
  1464. }
  1465. if ((active_irq & NAU8825_JACK_EJECTION_IRQ_MASK) ==
  1466. NAU8825_JACK_EJECTION_DETECTED) {
  1467. nau8825_eject_jack(nau8825);
  1468. event_mask |= SND_JACK_HEADSET;
  1469. clear_irq = NAU8825_JACK_EJECTION_IRQ_MASK;
  1470. } else if (active_irq & NAU8825_KEY_SHORT_PRESS_IRQ) {
  1471. int key_status;
  1472. regmap_read(regmap, NAU8825_REG_INT_CLR_KEY_STATUS,
  1473. &key_status);
  1474. /* upper 8 bits of the register are for short pressed keys,
  1475. * lower 8 bits - for long pressed buttons
  1476. */
  1477. nau8825->button_pressed = nau8825_button_decode(
  1478. key_status >> 8);
  1479. event |= nau8825->button_pressed;
  1480. event_mask |= NAU8825_BUTTONS;
  1481. clear_irq = NAU8825_KEY_SHORT_PRESS_IRQ;
  1482. } else if (active_irq & NAU8825_KEY_RELEASE_IRQ) {
  1483. event_mask = NAU8825_BUTTONS;
  1484. clear_irq = NAU8825_KEY_RELEASE_IRQ;
  1485. } else if (active_irq & NAU8825_HEADSET_COMPLETION_IRQ) {
  1486. if (nau8825_is_jack_inserted(regmap)) {
  1487. event |= nau8825_jack_insert(nau8825);
  1488. if (!nau8825->high_imped) {
  1489. /* Apply the cross talk suppression in the
  1490. * headset without high impedance.
  1491. */
  1492. if (!nau8825->xtalk_protect) {
  1493. /* Raise protection for cross talk de-
  1494. * tection if no protection before.
  1495. * The driver has to cancel the pro-
  1496. * cess and restore changes if process
  1497. * is ongoing when ejection.
  1498. */
  1499. int ret;
  1500. nau8825->xtalk_protect = true;
  1501. ret = nau8825_sema_acquire(nau8825, 0);
  1502. if (ret < 0)
  1503. nau8825->xtalk_protect = false;
  1504. }
  1505. /* Startup cross talk detection process */
  1506. nau8825->xtalk_state = NAU8825_XTALK_PREPARE;
  1507. schedule_work(&nau8825->xtalk_work);
  1508. } else {
  1509. /* The cross talk suppression shouldn't apply
  1510. * in the headset with high impedance. Thus,
  1511. * relieve the protection raised before.
  1512. */
  1513. if (nau8825->xtalk_protect) {
  1514. nau8825_sema_release(nau8825);
  1515. nau8825->xtalk_protect = false;
  1516. }
  1517. }
  1518. } else {
  1519. dev_warn(nau8825->dev, "Headset completion IRQ fired but no headset connected\n");
  1520. nau8825_eject_jack(nau8825);
  1521. }
  1522. event_mask |= SND_JACK_HEADSET;
  1523. clear_irq = NAU8825_HEADSET_COMPLETION_IRQ;
  1524. /* Record the interruption report event for driver to report
  1525. * the event later. The jack report will delay until cross
  1526. * talk detection process is done.
  1527. */
  1528. if (nau8825->xtalk_state == NAU8825_XTALK_PREPARE) {
  1529. nau8825->xtalk_event = event;
  1530. nau8825->xtalk_event_mask = event_mask;
  1531. }
  1532. } else if (active_irq & NAU8825_IMPEDANCE_MEAS_IRQ) {
  1533. schedule_work(&nau8825->xtalk_work);
  1534. clear_irq = NAU8825_IMPEDANCE_MEAS_IRQ;
  1535. } else if ((active_irq & NAU8825_JACK_INSERTION_IRQ_MASK) ==
  1536. NAU8825_JACK_INSERTION_DETECTED) {
  1537. /* One more step to check GPIO status directly. Thus, the
  1538. * driver can confirm the real insertion interruption because
  1539. * the intrruption at manual mode has bypassed debounce
  1540. * circuit which can get rid of unstable status.
  1541. */
  1542. if (nau8825_is_jack_inserted(regmap)) {
  1543. /* Turn off insertion interruption at manual mode */
  1544. regmap_update_bits(regmap,
  1545. NAU8825_REG_INTERRUPT_DIS_CTRL,
  1546. NAU8825_IRQ_INSERT_DIS,
  1547. NAU8825_IRQ_INSERT_DIS);
  1548. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  1549. NAU8825_IRQ_INSERT_EN, NAU8825_IRQ_INSERT_EN);
  1550. /* Enable interruption for jack type detection at audo
  1551. * mode which can detect microphone and jack type.
  1552. */
  1553. nau8825_setup_auto_irq(nau8825);
  1554. }
  1555. }
  1556. if (!clear_irq)
  1557. clear_irq = active_irq;
  1558. /* clears the rightmost interruption */
  1559. regmap_write(regmap, NAU8825_REG_INT_CLR_KEY_STATUS, clear_irq);
  1560. /* Delay jack report until cross talk detection is done. It can avoid
  1561. * application to do playback preparation when cross talk detection
  1562. * process is still working. Otherwise, the resource like clock and
  1563. * power will be issued by them at the same time and conflict happens.
  1564. */
  1565. if (event_mask && nau8825->xtalk_state == NAU8825_XTALK_DONE)
  1566. snd_soc_jack_report(nau8825->jack, event, event_mask);
  1567. return IRQ_HANDLED;
  1568. }
  1569. static void nau8825_setup_buttons(struct nau8825 *nau8825)
  1570. {
  1571. struct regmap *regmap = nau8825->regmap;
  1572. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1573. NAU8825_SAR_TRACKING_GAIN_MASK,
  1574. nau8825->sar_voltage << NAU8825_SAR_TRACKING_GAIN_SFT);
  1575. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1576. NAU8825_SAR_COMPARE_TIME_MASK,
  1577. nau8825->sar_compare_time << NAU8825_SAR_COMPARE_TIME_SFT);
  1578. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1579. NAU8825_SAR_SAMPLING_TIME_MASK,
  1580. nau8825->sar_sampling_time << NAU8825_SAR_SAMPLING_TIME_SFT);
  1581. regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
  1582. NAU8825_KEYDET_LEVELS_NR_MASK,
  1583. (nau8825->sar_threshold_num - 1) << NAU8825_KEYDET_LEVELS_NR_SFT);
  1584. regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
  1585. NAU8825_KEYDET_HYSTERESIS_MASK,
  1586. nau8825->sar_hysteresis << NAU8825_KEYDET_HYSTERESIS_SFT);
  1587. regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
  1588. NAU8825_KEYDET_SHORTKEY_DEBOUNCE_MASK,
  1589. nau8825->key_debounce << NAU8825_KEYDET_SHORTKEY_DEBOUNCE_SFT);
  1590. regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_1,
  1591. (nau8825->sar_threshold[0] << 8) | nau8825->sar_threshold[1]);
  1592. regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_2,
  1593. (nau8825->sar_threshold[2] << 8) | nau8825->sar_threshold[3]);
  1594. regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_3,
  1595. (nau8825->sar_threshold[4] << 8) | nau8825->sar_threshold[5]);
  1596. regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_4,
  1597. (nau8825->sar_threshold[6] << 8) | nau8825->sar_threshold[7]);
  1598. /* Enable short press and release interruptions */
  1599. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  1600. NAU8825_IRQ_KEY_SHORT_PRESS_EN | NAU8825_IRQ_KEY_RELEASE_EN,
  1601. 0);
  1602. }
  1603. static void nau8825_init_regs(struct nau8825 *nau8825)
  1604. {
  1605. struct regmap *regmap = nau8825->regmap;
  1606. /* Latch IIC LSB value */
  1607. regmap_write(regmap, NAU8825_REG_IIC_ADDR_SET, 0x0001);
  1608. /* Enable Bias/Vmid */
  1609. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  1610. NAU8825_BIAS_VMID, NAU8825_BIAS_VMID);
  1611. regmap_update_bits(nau8825->regmap, NAU8825_REG_BOOST,
  1612. NAU8825_GLOBAL_BIAS_EN, NAU8825_GLOBAL_BIAS_EN);
  1613. /* VMID Tieoff */
  1614. regmap_update_bits(regmap, NAU8825_REG_BIAS_ADJ,
  1615. NAU8825_BIAS_VMID_SEL_MASK,
  1616. nau8825->vref_impedance << NAU8825_BIAS_VMID_SEL_SFT);
  1617. /* Disable Boost Driver, Automatic Short circuit protection enable */
  1618. regmap_update_bits(regmap, NAU8825_REG_BOOST,
  1619. NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS |
  1620. NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN,
  1621. NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS |
  1622. NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN);
  1623. regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
  1624. NAU8825_JKDET_OUTPUT_EN,
  1625. nau8825->jkdet_enable ? 0 : NAU8825_JKDET_OUTPUT_EN);
  1626. regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
  1627. NAU8825_JKDET_PULL_EN,
  1628. nau8825->jkdet_pull_enable ? 0 : NAU8825_JKDET_PULL_EN);
  1629. regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
  1630. NAU8825_JKDET_PULL_UP,
  1631. nau8825->jkdet_pull_up ? NAU8825_JKDET_PULL_UP : 0);
  1632. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1633. NAU8825_JACK_POLARITY,
  1634. /* jkdet_polarity - 1 is for active-low */
  1635. nau8825->jkdet_polarity ? 0 : NAU8825_JACK_POLARITY);
  1636. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1637. NAU8825_JACK_INSERT_DEBOUNCE_MASK,
  1638. nau8825->jack_insert_debounce << NAU8825_JACK_INSERT_DEBOUNCE_SFT);
  1639. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1640. NAU8825_JACK_EJECT_DEBOUNCE_MASK,
  1641. nau8825->jack_eject_debounce << NAU8825_JACK_EJECT_DEBOUNCE_SFT);
  1642. /* Mask unneeded IRQs: 1 - disable, 0 - enable */
  1643. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK, 0x7ff, 0x7ff);
  1644. regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
  1645. NAU8825_MICBIAS_VOLTAGE_MASK, nau8825->micbias_voltage);
  1646. if (nau8825->sar_threshold_num)
  1647. nau8825_setup_buttons(nau8825);
  1648. /* Default oversampling/decimations settings are unusable
  1649. * (audible hiss). Set it to something better.
  1650. */
  1651. regmap_update_bits(regmap, NAU8825_REG_ADC_RATE,
  1652. NAU8825_ADC_SYNC_DOWN_MASK | NAU8825_ADC_SINC4_EN,
  1653. NAU8825_ADC_SYNC_DOWN_64);
  1654. regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1,
  1655. NAU8825_DAC_OVERSAMPLE_MASK, NAU8825_DAC_OVERSAMPLE_64);
  1656. /* Disable DACR/L power */
  1657. regmap_update_bits(regmap, NAU8825_REG_CHARGE_PUMP,
  1658. NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL,
  1659. NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL);
  1660. /* Enable TESTDAC. This sets the analog DAC inputs to a '0' input
  1661. * signal to avoid any glitches due to power up transients in both
  1662. * the analog and digital DAC circuit.
  1663. */
  1664. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  1665. NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
  1666. /* CICCLP off */
  1667. regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1,
  1668. NAU8825_DAC_CLIP_OFF, NAU8825_DAC_CLIP_OFF);
  1669. /* Class AB bias current to 2x, DAC Capacitor enable MSB/LSB */
  1670. regmap_update_bits(regmap, NAU8825_REG_ANALOG_CONTROL_2,
  1671. NAU8825_HP_NON_CLASSG_CURRENT_2xADJ |
  1672. NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB,
  1673. NAU8825_HP_NON_CLASSG_CURRENT_2xADJ |
  1674. NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB);
  1675. /* Class G timer 64ms */
  1676. regmap_update_bits(regmap, NAU8825_REG_CLASSG_CTRL,
  1677. NAU8825_CLASSG_TIMER_MASK,
  1678. 0x20 << NAU8825_CLASSG_TIMER_SFT);
  1679. /* DAC clock delay 2ns, VREF */
  1680. regmap_update_bits(regmap, NAU8825_REG_RDAC,
  1681. NAU8825_RDAC_CLK_DELAY_MASK | NAU8825_RDAC_VREF_MASK,
  1682. (0x2 << NAU8825_RDAC_CLK_DELAY_SFT) |
  1683. (0x3 << NAU8825_RDAC_VREF_SFT));
  1684. /* Config L/R channel */
  1685. regmap_update_bits(nau8825->regmap, NAU8825_REG_DACL_CTRL,
  1686. NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_L);
  1687. regmap_update_bits(nau8825->regmap, NAU8825_REG_DACR_CTRL,
  1688. NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_R);
  1689. /* Disable short Frame Sync detection logic */
  1690. regmap_update_bits(regmap, NAU8825_REG_LEFT_TIME_SLOT,
  1691. NAU8825_DIS_FS_SHORT_DET, NAU8825_DIS_FS_SHORT_DET);
  1692. }
  1693. static const struct regmap_config nau8825_regmap_config = {
  1694. .val_bits = NAU8825_REG_DATA_LEN,
  1695. .reg_bits = NAU8825_REG_ADDR_LEN,
  1696. .max_register = NAU8825_REG_MAX,
  1697. .readable_reg = nau8825_readable_reg,
  1698. .writeable_reg = nau8825_writeable_reg,
  1699. .volatile_reg = nau8825_volatile_reg,
  1700. .cache_type = REGCACHE_RBTREE,
  1701. .reg_defaults = nau8825_reg_defaults,
  1702. .num_reg_defaults = ARRAY_SIZE(nau8825_reg_defaults),
  1703. };
  1704. static int nau8825_codec_probe(struct snd_soc_codec *codec)
  1705. {
  1706. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1707. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  1708. nau8825->dapm = dapm;
  1709. return 0;
  1710. }
  1711. static int nau8825_codec_remove(struct snd_soc_codec *codec)
  1712. {
  1713. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1714. /* Cancel and reset cross tak suppresstion detection funciton */
  1715. nau8825_xtalk_cancel(nau8825);
  1716. return 0;
  1717. }
  1718. /**
  1719. * nau8825_calc_fll_param - Calculate FLL parameters.
  1720. * @fll_in: external clock provided to codec.
  1721. * @fs: sampling rate.
  1722. * @fll_param: Pointer to structure of FLL parameters.
  1723. *
  1724. * Calculate FLL parameters to configure codec.
  1725. *
  1726. * Returns 0 for success or negative error code.
  1727. */
  1728. static int nau8825_calc_fll_param(unsigned int fll_in, unsigned int fs,
  1729. struct nau8825_fll *fll_param)
  1730. {
  1731. u64 fvco, fvco_max;
  1732. unsigned int fref, i, fvco_sel;
  1733. /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by dividing
  1734. * freq_in by 1, 2, 4, or 8 using FLL pre-scalar.
  1735. * FREF = freq_in / NAU8825_FLL_REF_DIV_MASK
  1736. */
  1737. for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) {
  1738. fref = fll_in / fll_pre_scalar[i].param;
  1739. if (fref <= NAU_FREF_MAX)
  1740. break;
  1741. }
  1742. if (i == ARRAY_SIZE(fll_pre_scalar))
  1743. return -EINVAL;
  1744. fll_param->clk_ref_div = fll_pre_scalar[i].val;
  1745. /* Choose the FLL ratio based on FREF */
  1746. for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) {
  1747. if (fref >= fll_ratio[i].param)
  1748. break;
  1749. }
  1750. if (i == ARRAY_SIZE(fll_ratio))
  1751. return -EINVAL;
  1752. fll_param->ratio = fll_ratio[i].val;
  1753. /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs.
  1754. * FDCO must be within the 90MHz - 124MHz or the FFL cannot be
  1755. * guaranteed across the full range of operation.
  1756. * FDCO = freq_out * 2 * mclk_src_scaling
  1757. */
  1758. fvco_max = 0;
  1759. fvco_sel = ARRAY_SIZE(mclk_src_scaling);
  1760. for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) {
  1761. fvco = 256 * fs * 2 * mclk_src_scaling[i].param;
  1762. if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX &&
  1763. fvco_max < fvco) {
  1764. fvco_max = fvco;
  1765. fvco_sel = i;
  1766. }
  1767. }
  1768. if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel)
  1769. return -EINVAL;
  1770. fll_param->mclk_src = mclk_src_scaling[fvco_sel].val;
  1771. /* Calculate the FLL 10-bit integer input and the FLL 16-bit fractional
  1772. * input based on FDCO, FREF and FLL ratio.
  1773. */
  1774. fvco = div_u64(fvco_max << 16, fref * fll_param->ratio);
  1775. fll_param->fll_int = (fvco >> 16) & 0x3FF;
  1776. fll_param->fll_frac = fvco & 0xFFFF;
  1777. return 0;
  1778. }
  1779. static void nau8825_fll_apply(struct nau8825 *nau8825,
  1780. struct nau8825_fll *fll_param)
  1781. {
  1782. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  1783. NAU8825_CLK_SRC_MASK | NAU8825_CLK_MCLK_SRC_MASK,
  1784. NAU8825_CLK_SRC_MCLK | fll_param->mclk_src);
  1785. /* Make DSP operate at high speed for better performance. */
  1786. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL1,
  1787. NAU8825_FLL_RATIO_MASK | NAU8825_ICTRL_LATCH_MASK,
  1788. fll_param->ratio | (0x6 << NAU8825_ICTRL_LATCH_SFT));
  1789. /* FLL 16-bit fractional input */
  1790. regmap_write(nau8825->regmap, NAU8825_REG_FLL2, fll_param->fll_frac);
  1791. /* FLL 10-bit integer input */
  1792. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL3,
  1793. NAU8825_FLL_INTEGER_MASK, fll_param->fll_int);
  1794. /* FLL pre-scaler */
  1795. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL4,
  1796. NAU8825_FLL_REF_DIV_MASK, fll_param->clk_ref_div);
  1797. /* select divided VCO input */
  1798. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
  1799. NAU8825_FLL_CLK_SW_MASK, NAU8825_FLL_CLK_SW_REF);
  1800. /* Disable free-running mode */
  1801. regmap_update_bits(nau8825->regmap,
  1802. NAU8825_REG_FLL6, NAU8825_DCO_EN, 0);
  1803. if (fll_param->fll_frac) {
  1804. /* set FLL loop filter enable and cutoff frequency at 500Khz */
  1805. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
  1806. NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
  1807. NAU8825_FLL_FTR_SW_MASK,
  1808. NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
  1809. NAU8825_FLL_FTR_SW_FILTER);
  1810. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6,
  1811. NAU8825_SDM_EN | NAU8825_CUTOFF500,
  1812. NAU8825_SDM_EN | NAU8825_CUTOFF500);
  1813. } else {
  1814. /* disable FLL loop filter and cutoff frequency */
  1815. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
  1816. NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
  1817. NAU8825_FLL_FTR_SW_MASK, NAU8825_FLL_FTR_SW_ACCU);
  1818. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6,
  1819. NAU8825_SDM_EN | NAU8825_CUTOFF500, 0);
  1820. }
  1821. }
  1822. /* freq_out must be 256*Fs in order to achieve the best performance */
  1823. static int nau8825_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  1824. unsigned int freq_in, unsigned int freq_out)
  1825. {
  1826. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1827. struct nau8825_fll fll_param;
  1828. int ret, fs;
  1829. fs = freq_out / 256;
  1830. ret = nau8825_calc_fll_param(freq_in, fs, &fll_param);
  1831. if (ret < 0) {
  1832. dev_err(codec->dev, "Unsupported input clock %d\n", freq_in);
  1833. return ret;
  1834. }
  1835. dev_dbg(codec->dev, "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n",
  1836. fll_param.mclk_src, fll_param.ratio, fll_param.fll_frac,
  1837. fll_param.fll_int, fll_param.clk_ref_div);
  1838. nau8825_fll_apply(nau8825, &fll_param);
  1839. mdelay(2);
  1840. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  1841. NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
  1842. return 0;
  1843. }
  1844. static int nau8825_mclk_prepare(struct nau8825 *nau8825, unsigned int freq)
  1845. {
  1846. int ret = 0;
  1847. nau8825->mclk = devm_clk_get(nau8825->dev, "mclk");
  1848. if (IS_ERR(nau8825->mclk)) {
  1849. dev_info(nau8825->dev, "No 'mclk' clock found, assume MCLK is managed externally");
  1850. return 0;
  1851. }
  1852. if (!nau8825->mclk_freq) {
  1853. ret = clk_prepare_enable(nau8825->mclk);
  1854. if (ret) {
  1855. dev_err(nau8825->dev, "Unable to prepare codec mclk\n");
  1856. return ret;
  1857. }
  1858. }
  1859. if (nau8825->mclk_freq != freq) {
  1860. freq = clk_round_rate(nau8825->mclk, freq);
  1861. ret = clk_set_rate(nau8825->mclk, freq);
  1862. if (ret) {
  1863. dev_err(nau8825->dev, "Unable to set mclk rate\n");
  1864. return ret;
  1865. }
  1866. nau8825->mclk_freq = freq;
  1867. }
  1868. return 0;
  1869. }
  1870. static void nau8825_configure_mclk_as_sysclk(struct regmap *regmap)
  1871. {
  1872. regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
  1873. NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_MCLK);
  1874. regmap_update_bits(regmap, NAU8825_REG_FLL6,
  1875. NAU8825_DCO_EN, 0);
  1876. /* Make DSP operate as default setting for power saving. */
  1877. regmap_update_bits(regmap, NAU8825_REG_FLL1,
  1878. NAU8825_ICTRL_LATCH_MASK, 0);
  1879. }
  1880. static int nau8825_configure_sysclk(struct nau8825 *nau8825, int clk_id,
  1881. unsigned int freq)
  1882. {
  1883. struct regmap *regmap = nau8825->regmap;
  1884. int ret;
  1885. switch (clk_id) {
  1886. case NAU8825_CLK_DIS:
  1887. /* Clock provided externally and disable internal VCO clock */
  1888. nau8825_configure_mclk_as_sysclk(regmap);
  1889. if (nau8825->mclk_freq) {
  1890. clk_disable_unprepare(nau8825->mclk);
  1891. nau8825->mclk_freq = 0;
  1892. }
  1893. break;
  1894. case NAU8825_CLK_MCLK:
  1895. /* Acquire the semaphone to synchronize the playback and
  1896. * interrupt handler. In order to avoid the playback inter-
  1897. * fered by cross talk process, the driver make the playback
  1898. * preparation halted until cross talk process finish.
  1899. */
  1900. nau8825_sema_acquire(nau8825, 3 * HZ);
  1901. nau8825_configure_mclk_as_sysclk(regmap);
  1902. /* MCLK not changed by clock tree */
  1903. regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
  1904. NAU8825_CLK_MCLK_SRC_MASK, 0);
  1905. /* Release the semaphone. */
  1906. nau8825_sema_release(nau8825);
  1907. ret = nau8825_mclk_prepare(nau8825, freq);
  1908. if (ret)
  1909. return ret;
  1910. break;
  1911. case NAU8825_CLK_INTERNAL:
  1912. if (nau8825_is_jack_inserted(nau8825->regmap)) {
  1913. regmap_update_bits(regmap, NAU8825_REG_FLL6,
  1914. NAU8825_DCO_EN, NAU8825_DCO_EN);
  1915. regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
  1916. NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
  1917. /* Decrease the VCO frequency and make DSP operate
  1918. * as default setting for power saving.
  1919. */
  1920. regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
  1921. NAU8825_CLK_MCLK_SRC_MASK, 0xf);
  1922. regmap_update_bits(regmap, NAU8825_REG_FLL1,
  1923. NAU8825_ICTRL_LATCH_MASK |
  1924. NAU8825_FLL_RATIO_MASK, 0x10);
  1925. regmap_update_bits(regmap, NAU8825_REG_FLL6,
  1926. NAU8825_SDM_EN, NAU8825_SDM_EN);
  1927. } else {
  1928. /* The clock turns off intentionally for power saving
  1929. * when no headset connected.
  1930. */
  1931. nau8825_configure_mclk_as_sysclk(regmap);
  1932. dev_warn(nau8825->dev, "Disable clock for power saving when no headset connected\n");
  1933. }
  1934. if (nau8825->mclk_freq) {
  1935. clk_disable_unprepare(nau8825->mclk);
  1936. nau8825->mclk_freq = 0;
  1937. }
  1938. break;
  1939. case NAU8825_CLK_FLL_MCLK:
  1940. /* Acquire the semaphone to synchronize the playback and
  1941. * interrupt handler. In order to avoid the playback inter-
  1942. * fered by cross talk process, the driver make the playback
  1943. * preparation halted until cross talk process finish.
  1944. */
  1945. nau8825_sema_acquire(nau8825, 3 * HZ);
  1946. /* Higher FLL reference input frequency can only set lower
  1947. * gain error, such as 0000 for input reference from MCLK
  1948. * 12.288Mhz.
  1949. */
  1950. regmap_update_bits(regmap, NAU8825_REG_FLL3,
  1951. NAU8825_FLL_CLK_SRC_MASK | NAU8825_GAIN_ERR_MASK,
  1952. NAU8825_FLL_CLK_SRC_MCLK | 0);
  1953. /* Release the semaphone. */
  1954. nau8825_sema_release(nau8825);
  1955. ret = nau8825_mclk_prepare(nau8825, freq);
  1956. if (ret)
  1957. return ret;
  1958. break;
  1959. case NAU8825_CLK_FLL_BLK:
  1960. /* Acquire the semaphone to synchronize the playback and
  1961. * interrupt handler. In order to avoid the playback inter-
  1962. * fered by cross talk process, the driver make the playback
  1963. * preparation halted until cross talk process finish.
  1964. */
  1965. nau8825_sema_acquire(nau8825, 3 * HZ);
  1966. /* If FLL reference input is from low frequency source,
  1967. * higher error gain can apply such as 0xf which has
  1968. * the most sensitive gain error correction threshold,
  1969. * Therefore, FLL has the most accurate DCO to
  1970. * target frequency.
  1971. */
  1972. regmap_update_bits(regmap, NAU8825_REG_FLL3,
  1973. NAU8825_FLL_CLK_SRC_MASK | NAU8825_GAIN_ERR_MASK,
  1974. NAU8825_FLL_CLK_SRC_BLK |
  1975. (0xf << NAU8825_GAIN_ERR_SFT));
  1976. /* Release the semaphone. */
  1977. nau8825_sema_release(nau8825);
  1978. if (nau8825->mclk_freq) {
  1979. clk_disable_unprepare(nau8825->mclk);
  1980. nau8825->mclk_freq = 0;
  1981. }
  1982. break;
  1983. case NAU8825_CLK_FLL_FS:
  1984. /* Acquire the semaphone to synchronize the playback and
  1985. * interrupt handler. In order to avoid the playback inter-
  1986. * fered by cross talk process, the driver make the playback
  1987. * preparation halted until cross talk process finish.
  1988. */
  1989. nau8825_sema_acquire(nau8825, 3 * HZ);
  1990. /* If FLL reference input is from low frequency source,
  1991. * higher error gain can apply such as 0xf which has
  1992. * the most sensitive gain error correction threshold,
  1993. * Therefore, FLL has the most accurate DCO to
  1994. * target frequency.
  1995. */
  1996. regmap_update_bits(regmap, NAU8825_REG_FLL3,
  1997. NAU8825_FLL_CLK_SRC_MASK | NAU8825_GAIN_ERR_MASK,
  1998. NAU8825_FLL_CLK_SRC_FS |
  1999. (0xf << NAU8825_GAIN_ERR_SFT));
  2000. /* Release the semaphone. */
  2001. nau8825_sema_release(nau8825);
  2002. if (nau8825->mclk_freq) {
  2003. clk_disable_unprepare(nau8825->mclk);
  2004. nau8825->mclk_freq = 0;
  2005. }
  2006. break;
  2007. default:
  2008. dev_err(nau8825->dev, "Invalid clock id (%d)\n", clk_id);
  2009. return -EINVAL;
  2010. }
  2011. dev_dbg(nau8825->dev, "Sysclk is %dHz and clock id is %d\n", freq,
  2012. clk_id);
  2013. return 0;
  2014. }
  2015. static int nau8825_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  2016. int source, unsigned int freq, int dir)
  2017. {
  2018. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  2019. return nau8825_configure_sysclk(nau8825, clk_id, freq);
  2020. }
  2021. static int nau8825_resume_setup(struct nau8825 *nau8825)
  2022. {
  2023. struct regmap *regmap = nau8825->regmap;
  2024. /* Close clock when jack type detection at manual mode */
  2025. nau8825_configure_sysclk(nau8825, NAU8825_CLK_DIS, 0);
  2026. /* Clear all interruption status */
  2027. nau8825_int_status_clear_all(regmap);
  2028. /* Enable both insertion and ejection interruptions, and then
  2029. * bypass de-bounce circuit.
  2030. */
  2031. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  2032. NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_HEADSET_COMPLETE_EN |
  2033. NAU8825_IRQ_EJECT_EN | NAU8825_IRQ_INSERT_EN,
  2034. NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_HEADSET_COMPLETE_EN);
  2035. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  2036. NAU8825_JACK_DET_DB_BYPASS, NAU8825_JACK_DET_DB_BYPASS);
  2037. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_DIS_CTRL,
  2038. NAU8825_IRQ_INSERT_DIS | NAU8825_IRQ_EJECT_DIS, 0);
  2039. return 0;
  2040. }
  2041. static int nau8825_set_bias_level(struct snd_soc_codec *codec,
  2042. enum snd_soc_bias_level level)
  2043. {
  2044. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  2045. int ret;
  2046. switch (level) {
  2047. case SND_SOC_BIAS_ON:
  2048. break;
  2049. case SND_SOC_BIAS_PREPARE:
  2050. break;
  2051. case SND_SOC_BIAS_STANDBY:
  2052. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  2053. if (nau8825->mclk_freq) {
  2054. ret = clk_prepare_enable(nau8825->mclk);
  2055. if (ret) {
  2056. dev_err(nau8825->dev, "Unable to prepare codec mclk\n");
  2057. return ret;
  2058. }
  2059. }
  2060. /* Setup codec configuration after resume */
  2061. nau8825_resume_setup(nau8825);
  2062. }
  2063. break;
  2064. case SND_SOC_BIAS_OFF:
  2065. /* Cancel and reset cross talk detection funciton */
  2066. nau8825_xtalk_cancel(nau8825);
  2067. /* Turn off all interruptions before system shutdown. Keep the
  2068. * interruption quiet before resume setup completes.
  2069. */
  2070. regmap_write(nau8825->regmap,
  2071. NAU8825_REG_INTERRUPT_DIS_CTRL, 0xffff);
  2072. /* Disable ADC needed for interruptions at audo mode */
  2073. regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
  2074. NAU8825_ENABLE_ADC, 0);
  2075. if (nau8825->mclk_freq)
  2076. clk_disable_unprepare(nau8825->mclk);
  2077. break;
  2078. }
  2079. return 0;
  2080. }
  2081. static int __maybe_unused nau8825_suspend(struct snd_soc_codec *codec)
  2082. {
  2083. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  2084. disable_irq(nau8825->irq);
  2085. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
  2086. regcache_cache_only(nau8825->regmap, true);
  2087. regcache_mark_dirty(nau8825->regmap);
  2088. return 0;
  2089. }
  2090. static int __maybe_unused nau8825_resume(struct snd_soc_codec *codec)
  2091. {
  2092. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  2093. int ret;
  2094. regcache_cache_only(nau8825->regmap, false);
  2095. regcache_sync(nau8825->regmap);
  2096. nau8825->xtalk_protect = true;
  2097. ret = nau8825_sema_acquire(nau8825, 0);
  2098. if (ret < 0)
  2099. nau8825->xtalk_protect = false;
  2100. enable_irq(nau8825->irq);
  2101. return 0;
  2102. }
  2103. static struct snd_soc_codec_driver nau8825_codec_driver = {
  2104. .probe = nau8825_codec_probe,
  2105. .remove = nau8825_codec_remove,
  2106. .set_sysclk = nau8825_set_sysclk,
  2107. .set_pll = nau8825_set_pll,
  2108. .set_bias_level = nau8825_set_bias_level,
  2109. .suspend_bias_off = true,
  2110. .suspend = nau8825_suspend,
  2111. .resume = nau8825_resume,
  2112. .component_driver = {
  2113. .controls = nau8825_controls,
  2114. .num_controls = ARRAY_SIZE(nau8825_controls),
  2115. .dapm_widgets = nau8825_dapm_widgets,
  2116. .num_dapm_widgets = ARRAY_SIZE(nau8825_dapm_widgets),
  2117. .dapm_routes = nau8825_dapm_routes,
  2118. .num_dapm_routes = ARRAY_SIZE(nau8825_dapm_routes),
  2119. },
  2120. };
  2121. static void nau8825_reset_chip(struct regmap *regmap)
  2122. {
  2123. regmap_write(regmap, NAU8825_REG_RESET, 0x00);
  2124. regmap_write(regmap, NAU8825_REG_RESET, 0x00);
  2125. }
  2126. static void nau8825_print_device_properties(struct nau8825 *nau8825)
  2127. {
  2128. int i;
  2129. struct device *dev = nau8825->dev;
  2130. dev_dbg(dev, "jkdet-enable: %d\n", nau8825->jkdet_enable);
  2131. dev_dbg(dev, "jkdet-pull-enable: %d\n", nau8825->jkdet_pull_enable);
  2132. dev_dbg(dev, "jkdet-pull-up: %d\n", nau8825->jkdet_pull_up);
  2133. dev_dbg(dev, "jkdet-polarity: %d\n", nau8825->jkdet_polarity);
  2134. dev_dbg(dev, "micbias-voltage: %d\n", nau8825->micbias_voltage);
  2135. dev_dbg(dev, "vref-impedance: %d\n", nau8825->vref_impedance);
  2136. dev_dbg(dev, "sar-threshold-num: %d\n", nau8825->sar_threshold_num);
  2137. for (i = 0; i < nau8825->sar_threshold_num; i++)
  2138. dev_dbg(dev, "sar-threshold[%d]=%d\n", i,
  2139. nau8825->sar_threshold[i]);
  2140. dev_dbg(dev, "sar-hysteresis: %d\n", nau8825->sar_hysteresis);
  2141. dev_dbg(dev, "sar-voltage: %d\n", nau8825->sar_voltage);
  2142. dev_dbg(dev, "sar-compare-time: %d\n", nau8825->sar_compare_time);
  2143. dev_dbg(dev, "sar-sampling-time: %d\n", nau8825->sar_sampling_time);
  2144. dev_dbg(dev, "short-key-debounce: %d\n", nau8825->key_debounce);
  2145. dev_dbg(dev, "jack-insert-debounce: %d\n",
  2146. nau8825->jack_insert_debounce);
  2147. dev_dbg(dev, "jack-eject-debounce: %d\n",
  2148. nau8825->jack_eject_debounce);
  2149. }
  2150. static int nau8825_read_device_properties(struct device *dev,
  2151. struct nau8825 *nau8825) {
  2152. nau8825->jkdet_enable = device_property_read_bool(dev,
  2153. "nuvoton,jkdet-enable");
  2154. nau8825->jkdet_pull_enable = device_property_read_bool(dev,
  2155. "nuvoton,jkdet-pull-enable");
  2156. nau8825->jkdet_pull_up = device_property_read_bool(dev,
  2157. "nuvoton,jkdet-pull-up");
  2158. device_property_read_u32(dev, "nuvoton,jkdet-polarity",
  2159. &nau8825->jkdet_polarity);
  2160. device_property_read_u32(dev, "nuvoton,micbias-voltage",
  2161. &nau8825->micbias_voltage);
  2162. device_property_read_u32(dev, "nuvoton,vref-impedance",
  2163. &nau8825->vref_impedance);
  2164. device_property_read_u32(dev, "nuvoton,sar-threshold-num",
  2165. &nau8825->sar_threshold_num);
  2166. device_property_read_u32_array(dev, "nuvoton,sar-threshold",
  2167. nau8825->sar_threshold, nau8825->sar_threshold_num);
  2168. device_property_read_u32(dev, "nuvoton,sar-hysteresis",
  2169. &nau8825->sar_hysteresis);
  2170. device_property_read_u32(dev, "nuvoton,sar-voltage",
  2171. &nau8825->sar_voltage);
  2172. device_property_read_u32(dev, "nuvoton,sar-compare-time",
  2173. &nau8825->sar_compare_time);
  2174. device_property_read_u32(dev, "nuvoton,sar-sampling-time",
  2175. &nau8825->sar_sampling_time);
  2176. device_property_read_u32(dev, "nuvoton,short-key-debounce",
  2177. &nau8825->key_debounce);
  2178. device_property_read_u32(dev, "nuvoton,jack-insert-debounce",
  2179. &nau8825->jack_insert_debounce);
  2180. device_property_read_u32(dev, "nuvoton,jack-eject-debounce",
  2181. &nau8825->jack_eject_debounce);
  2182. nau8825->mclk = devm_clk_get(dev, "mclk");
  2183. if (PTR_ERR(nau8825->mclk) == -EPROBE_DEFER) {
  2184. return -EPROBE_DEFER;
  2185. } else if (PTR_ERR(nau8825->mclk) == -ENOENT) {
  2186. /* The MCLK is managed externally or not used at all */
  2187. nau8825->mclk = NULL;
  2188. dev_info(dev, "No 'mclk' clock found, assume MCLK is managed externally");
  2189. } else if (IS_ERR(nau8825->mclk)) {
  2190. return -EINVAL;
  2191. }
  2192. return 0;
  2193. }
  2194. static int nau8825_setup_irq(struct nau8825 *nau8825)
  2195. {
  2196. int ret;
  2197. ret = devm_request_threaded_irq(nau8825->dev, nau8825->irq, NULL,
  2198. nau8825_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  2199. "nau8825", nau8825);
  2200. if (ret) {
  2201. dev_err(nau8825->dev, "Cannot request irq %d (%d)\n",
  2202. nau8825->irq, ret);
  2203. return ret;
  2204. }
  2205. return 0;
  2206. }
  2207. static int nau8825_i2c_probe(struct i2c_client *i2c,
  2208. const struct i2c_device_id *id)
  2209. {
  2210. struct device *dev = &i2c->dev;
  2211. struct nau8825 *nau8825 = dev_get_platdata(&i2c->dev);
  2212. int ret, value;
  2213. if (!nau8825) {
  2214. nau8825 = devm_kzalloc(dev, sizeof(*nau8825), GFP_KERNEL);
  2215. if (!nau8825)
  2216. return -ENOMEM;
  2217. ret = nau8825_read_device_properties(dev, nau8825);
  2218. if (ret)
  2219. return ret;
  2220. }
  2221. i2c_set_clientdata(i2c, nau8825);
  2222. nau8825->regmap = devm_regmap_init_i2c(i2c, &nau8825_regmap_config);
  2223. if (IS_ERR(nau8825->regmap))
  2224. return PTR_ERR(nau8825->regmap);
  2225. nau8825->dev = dev;
  2226. nau8825->irq = i2c->irq;
  2227. /* Initiate parameters, semaphone and work queue which are needed in
  2228. * cross talk suppression measurment function.
  2229. */
  2230. nau8825->xtalk_state = NAU8825_XTALK_DONE;
  2231. nau8825->xtalk_protect = false;
  2232. sema_init(&nau8825->xtalk_sem, 1);
  2233. INIT_WORK(&nau8825->xtalk_work, nau8825_xtalk_work);
  2234. nau8825_print_device_properties(nau8825);
  2235. nau8825_reset_chip(nau8825->regmap);
  2236. ret = regmap_read(nau8825->regmap, NAU8825_REG_I2C_DEVICE_ID, &value);
  2237. if (ret < 0) {
  2238. dev_err(dev, "Failed to read device id from the NAU8825: %d\n",
  2239. ret);
  2240. return ret;
  2241. }
  2242. if ((value & NAU8825_SOFTWARE_ID_MASK) !=
  2243. NAU8825_SOFTWARE_ID_NAU8825) {
  2244. dev_err(dev, "Not a NAU8825 chip\n");
  2245. return -ENODEV;
  2246. }
  2247. nau8825_init_regs(nau8825);
  2248. if (i2c->irq)
  2249. nau8825_setup_irq(nau8825);
  2250. return snd_soc_register_codec(&i2c->dev, &nau8825_codec_driver,
  2251. &nau8825_dai, 1);
  2252. }
  2253. static int nau8825_i2c_remove(struct i2c_client *client)
  2254. {
  2255. snd_soc_unregister_codec(&client->dev);
  2256. return 0;
  2257. }
  2258. static const struct i2c_device_id nau8825_i2c_ids[] = {
  2259. { "nau8825", 0 },
  2260. { }
  2261. };
  2262. MODULE_DEVICE_TABLE(i2c, nau8825_i2c_ids);
  2263. #ifdef CONFIG_OF
  2264. static const struct of_device_id nau8825_of_ids[] = {
  2265. { .compatible = "nuvoton,nau8825", },
  2266. {}
  2267. };
  2268. MODULE_DEVICE_TABLE(of, nau8825_of_ids);
  2269. #endif
  2270. #ifdef CONFIG_ACPI
  2271. static const struct acpi_device_id nau8825_acpi_match[] = {
  2272. { "10508825", 0 },
  2273. {},
  2274. };
  2275. MODULE_DEVICE_TABLE(acpi, nau8825_acpi_match);
  2276. #endif
  2277. static struct i2c_driver nau8825_driver = {
  2278. .driver = {
  2279. .name = "nau8825",
  2280. .of_match_table = of_match_ptr(nau8825_of_ids),
  2281. .acpi_match_table = ACPI_PTR(nau8825_acpi_match),
  2282. },
  2283. .probe = nau8825_i2c_probe,
  2284. .remove = nau8825_i2c_remove,
  2285. .id_table = nau8825_i2c_ids,
  2286. };
  2287. module_i2c_driver(nau8825_driver);
  2288. MODULE_DESCRIPTION("ASoC nau8825 driver");
  2289. MODULE_AUTHOR("Anatol Pomozov <anatol@chromium.org>");
  2290. MODULE_LICENSE("GPL");