nau8825.c 92 KB

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