nau8825.c 80 KB

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