nau8825.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667
  1. /*
  2. * Nuvoton NAU8825 audio codec driver
  3. *
  4. * Copyright 2015 Google Chromium project.
  5. * Author: Anatol Pomozov <anatol@chromium.org>
  6. * Copyright 2015 Nuvoton Technology Corp.
  7. * Co-author: Meng-Huang Kuo <mhkuo@nuvoton.com>
  8. *
  9. * Licensed under the GPL-2.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/i2c.h>
  15. #include <linux/regmap.h>
  16. #include <linux/slab.h>
  17. #include <linux/clk.h>
  18. #include <linux/acpi.h>
  19. #include <linux/math64.h>
  20. #include <linux/semaphore.h>
  21. #include <sound/initval.h>
  22. #include <sound/tlv.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/jack.h>
  28. #include "nau8825.h"
  29. #define NUVOTON_CODEC_DAI "nau8825-hifi"
  30. #define NAU_FREF_MAX 13500000
  31. #define NAU_FVCO_MAX 124000000
  32. #define NAU_FVCO_MIN 90000000
  33. /* cross talk suppression detection */
  34. #define LOG10_MAGIC 646456993
  35. #define GAIN_AUGMENT 22500
  36. #define SIDETONE_BASE 207000
  37. /* the maximum frequency of CLK_ADC and CLK_DAC */
  38. #define CLK_DA_AD_MAX 6144000
  39. static int nau8825_configure_sysclk(struct nau8825 *nau8825,
  40. int clk_id, unsigned int freq);
  41. struct nau8825_fll {
  42. int mclk_src;
  43. int ratio;
  44. int fll_frac;
  45. int fll_int;
  46. int clk_ref_div;
  47. };
  48. struct nau8825_fll_attr {
  49. unsigned int param;
  50. unsigned int val;
  51. };
  52. /* scaling for mclk from sysclk_src output */
  53. static const struct nau8825_fll_attr mclk_src_scaling[] = {
  54. { 1, 0x0 },
  55. { 2, 0x2 },
  56. { 4, 0x3 },
  57. { 8, 0x4 },
  58. { 16, 0x5 },
  59. { 32, 0x6 },
  60. { 3, 0x7 },
  61. { 6, 0xa },
  62. { 12, 0xb },
  63. { 24, 0xc },
  64. { 48, 0xd },
  65. { 96, 0xe },
  66. { 5, 0xf },
  67. };
  68. /* ratio for input clk freq */
  69. static const struct nau8825_fll_attr fll_ratio[] = {
  70. { 512000, 0x01 },
  71. { 256000, 0x02 },
  72. { 128000, 0x04 },
  73. { 64000, 0x08 },
  74. { 32000, 0x10 },
  75. { 8000, 0x20 },
  76. { 4000, 0x40 },
  77. };
  78. static const struct nau8825_fll_attr fll_pre_scalar[] = {
  79. { 1, 0x0 },
  80. { 2, 0x1 },
  81. { 4, 0x2 },
  82. { 8, 0x3 },
  83. };
  84. /* over sampling rate */
  85. struct nau8825_osr_attr {
  86. unsigned int osr;
  87. unsigned int clk_src;
  88. };
  89. static const struct nau8825_osr_attr osr_dac_sel[] = {
  90. { 64, 2 }, /* OSR 64, SRC 1/4 */
  91. { 256, 0 }, /* OSR 256, SRC 1 */
  92. { 128, 1 }, /* OSR 128, SRC 1/2 */
  93. { 0, 0 },
  94. { 32, 3 }, /* OSR 32, SRC 1/8 */
  95. };
  96. static const struct nau8825_osr_attr osr_adc_sel[] = {
  97. { 32, 3 }, /* OSR 32, SRC 1/8 */
  98. { 64, 2 }, /* OSR 64, SRC 1/4 */
  99. { 128, 1 }, /* OSR 128, SRC 1/2 */
  100. { 256, 0 }, /* OSR 256, SRC 1 */
  101. };
  102. static const struct reg_default nau8825_reg_defaults[] = {
  103. { NAU8825_REG_ENA_CTRL, 0x00ff },
  104. { NAU8825_REG_IIC_ADDR_SET, 0x0 },
  105. { NAU8825_REG_CLK_DIVIDER, 0x0050 },
  106. { NAU8825_REG_FLL1, 0x0 },
  107. { NAU8825_REG_FLL2, 0x3126 },
  108. { NAU8825_REG_FLL3, 0x0008 },
  109. { NAU8825_REG_FLL4, 0x0010 },
  110. { NAU8825_REG_FLL5, 0x0 },
  111. { NAU8825_REG_FLL6, 0x6000 },
  112. { NAU8825_REG_FLL_VCO_RSV, 0xf13c },
  113. { NAU8825_REG_HSD_CTRL, 0x000c },
  114. { NAU8825_REG_JACK_DET_CTRL, 0x0 },
  115. { NAU8825_REG_INTERRUPT_MASK, 0x0 },
  116. { NAU8825_REG_INTERRUPT_DIS_CTRL, 0xffff },
  117. { NAU8825_REG_SAR_CTRL, 0x0015 },
  118. { NAU8825_REG_KEYDET_CTRL, 0x0110 },
  119. { NAU8825_REG_VDET_THRESHOLD_1, 0x0 },
  120. { NAU8825_REG_VDET_THRESHOLD_2, 0x0 },
  121. { NAU8825_REG_VDET_THRESHOLD_3, 0x0 },
  122. { NAU8825_REG_VDET_THRESHOLD_4, 0x0 },
  123. { NAU8825_REG_GPIO34_CTRL, 0x0 },
  124. { NAU8825_REG_GPIO12_CTRL, 0x0 },
  125. { NAU8825_REG_TDM_CTRL, 0x0 },
  126. { NAU8825_REG_I2S_PCM_CTRL1, 0x000b },
  127. { NAU8825_REG_I2S_PCM_CTRL2, 0x8010 },
  128. { NAU8825_REG_LEFT_TIME_SLOT, 0x0 },
  129. { NAU8825_REG_RIGHT_TIME_SLOT, 0x0 },
  130. { NAU8825_REG_BIQ_CTRL, 0x0 },
  131. { NAU8825_REG_BIQ_COF1, 0x0 },
  132. { NAU8825_REG_BIQ_COF2, 0x0 },
  133. { NAU8825_REG_BIQ_COF3, 0x0 },
  134. { NAU8825_REG_BIQ_COF4, 0x0 },
  135. { NAU8825_REG_BIQ_COF5, 0x0 },
  136. { NAU8825_REG_BIQ_COF6, 0x0 },
  137. { NAU8825_REG_BIQ_COF7, 0x0 },
  138. { NAU8825_REG_BIQ_COF8, 0x0 },
  139. { NAU8825_REG_BIQ_COF9, 0x0 },
  140. { NAU8825_REG_BIQ_COF10, 0x0 },
  141. { NAU8825_REG_ADC_RATE, 0x0010 },
  142. { NAU8825_REG_DAC_CTRL1, 0x0001 },
  143. { NAU8825_REG_DAC_CTRL2, 0x0 },
  144. { NAU8825_REG_DAC_DGAIN_CTRL, 0x0 },
  145. { NAU8825_REG_ADC_DGAIN_CTRL, 0x00cf },
  146. { NAU8825_REG_MUTE_CTRL, 0x0 },
  147. { NAU8825_REG_HSVOL_CTRL, 0x0 },
  148. { NAU8825_REG_DACL_CTRL, 0x02cf },
  149. { NAU8825_REG_DACR_CTRL, 0x00cf },
  150. { NAU8825_REG_ADC_DRC_KNEE_IP12, 0x1486 },
  151. { NAU8825_REG_ADC_DRC_KNEE_IP34, 0x0f12 },
  152. { NAU8825_REG_ADC_DRC_SLOPES, 0x25ff },
  153. { NAU8825_REG_ADC_DRC_ATKDCY, 0x3457 },
  154. { NAU8825_REG_DAC_DRC_KNEE_IP12, 0x1486 },
  155. { NAU8825_REG_DAC_DRC_KNEE_IP34, 0x0f12 },
  156. { NAU8825_REG_DAC_DRC_SLOPES, 0x25f9 },
  157. { NAU8825_REG_DAC_DRC_ATKDCY, 0x3457 },
  158. { NAU8825_REG_IMM_MODE_CTRL, 0x0 },
  159. { NAU8825_REG_CLASSG_CTRL, 0x0 },
  160. { NAU8825_REG_OPT_EFUSE_CTRL, 0x0 },
  161. { NAU8825_REG_MISC_CTRL, 0x0 },
  162. { NAU8825_REG_BIAS_ADJ, 0x0 },
  163. { NAU8825_REG_TRIM_SETTINGS, 0x0 },
  164. { NAU8825_REG_ANALOG_CONTROL_1, 0x0 },
  165. { NAU8825_REG_ANALOG_CONTROL_2, 0x0 },
  166. { NAU8825_REG_ANALOG_ADC_1, 0x0011 },
  167. { NAU8825_REG_ANALOG_ADC_2, 0x0020 },
  168. { NAU8825_REG_RDAC, 0x0008 },
  169. { NAU8825_REG_MIC_BIAS, 0x0006 },
  170. { NAU8825_REG_BOOST, 0x0 },
  171. { NAU8825_REG_FEPGA, 0x0 },
  172. { NAU8825_REG_POWER_UP_CONTROL, 0x0 },
  173. { NAU8825_REG_CHARGE_PUMP, 0x0 },
  174. };
  175. /* register backup table when cross talk detection */
  176. static struct reg_default nau8825_xtalk_baktab[] = {
  177. { NAU8825_REG_ADC_DGAIN_CTRL, 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 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. * 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. * Computes log10 of a value; the result is round off to 3 decimal. This func-
  322. * tion takes reference to dvb-math. The source code locates as the following.
  323. * Linux/drivers/media/dvb-core/dvb_math.c
  324. *
  325. * return log10(value) * 1000
  326. */
  327. static u32 nau8825_intlog10_dec3(u32 value)
  328. {
  329. u32 msb, logentry, significand, interpolation, log10val;
  330. u64 log2val;
  331. /* first detect the msb (count begins at 0) */
  332. msb = fls(value) - 1;
  333. /**
  334. * now we use a logtable after the following method:
  335. *
  336. * log2(2^x * y) * 2^24 = x * 2^24 + log2(y) * 2^24
  337. * where x = msb and therefore 1 <= y < 2
  338. * first y is determined by shifting the value left
  339. * so that msb is bit 31
  340. * 0x00231f56 -> 0x8C7D5800
  341. * the result is y * 2^31 -> "significand"
  342. * then the highest 9 bits are used for a table lookup
  343. * the highest bit is discarded because it's always set
  344. * the highest nine bits in our example are 100011000
  345. * so we would use the entry 0x18
  346. */
  347. significand = value << (31 - msb);
  348. logentry = (significand >> 23) & 0xff;
  349. /**
  350. * last step we do is interpolation because of the
  351. * limitations of the log table the error is that part of
  352. * the significand which isn't used for lookup then we
  353. * compute the ratio between the error and the next table entry
  354. * and interpolate it between the log table entry used and the
  355. * next one the biggest error possible is 0x7fffff
  356. * (in our example it's 0x7D5800)
  357. * needed value for next table entry is 0x800000
  358. * so the interpolation is
  359. * (error / 0x800000) * (logtable_next - logtable_current)
  360. * in the implementation the division is moved to the end for
  361. * better accuracy there is also an overflow correction if
  362. * logtable_next is 256
  363. */
  364. interpolation = ((significand & 0x7fffff) *
  365. ((logtable[(logentry + 1) & 0xff] -
  366. logtable[logentry]) & 0xffff)) >> 15;
  367. log2val = ((msb << 24) + (logtable[logentry] << 8) + interpolation);
  368. /**
  369. * log10(x) = log2(x) * log10(2)
  370. */
  371. log10val = (log2val * LOG10_MAGIC) >> 31;
  372. /**
  373. * the result is round off to 3 decimal
  374. */
  375. return log10val / ((1 << 24) / 1000);
  376. }
  377. /**
  378. * computes cross talk suppression sidetone gain.
  379. *
  380. * @sig_org: orignal signal level
  381. * @sig_cros: cross talk signal level
  382. *
  383. * The orignal and cross talk signal vlues need to be characterized.
  384. * Once these values have been characterized, this sidetone value
  385. * can be converted to decibel with the equation below.
  386. * sidetone = 20 * log (original signal level / crosstalk signal level)
  387. *
  388. * return cross talk sidetone gain
  389. */
  390. static u32 nau8825_xtalk_sidetone(u32 sig_org, u32 sig_cros)
  391. {
  392. u32 gain, sidetone;
  393. if (unlikely(sig_org == 0) || unlikely(sig_cros == 0)) {
  394. WARN_ON(1);
  395. return 0;
  396. }
  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_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  851. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  852. switch (event) {
  853. case SND_SOC_DAPM_POST_PMU:
  854. regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
  855. NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC);
  856. break;
  857. case SND_SOC_DAPM_POST_PMD:
  858. if (!nau8825->irq)
  859. regmap_update_bits(nau8825->regmap,
  860. NAU8825_REG_ENA_CTRL, NAU8825_ENABLE_ADC, 0);
  861. break;
  862. default:
  863. return -EINVAL;
  864. }
  865. return 0;
  866. }
  867. static int nau8825_pump_event(struct snd_soc_dapm_widget *w,
  868. struct snd_kcontrol *kcontrol, int event)
  869. {
  870. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  871. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  872. switch (event) {
  873. case SND_SOC_DAPM_POST_PMU:
  874. /* Prevent startup click by letting charge pump to ramp up */
  875. msleep(10);
  876. regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
  877. NAU8825_JAMNODCLOW, NAU8825_JAMNODCLOW);
  878. break;
  879. case SND_SOC_DAPM_PRE_PMD:
  880. regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
  881. NAU8825_JAMNODCLOW, 0);
  882. break;
  883. default:
  884. return -EINVAL;
  885. }
  886. return 0;
  887. }
  888. static int nau8825_output_dac_event(struct snd_soc_dapm_widget *w,
  889. struct snd_kcontrol *kcontrol, int event)
  890. {
  891. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  892. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  893. switch (event) {
  894. case SND_SOC_DAPM_PRE_PMU:
  895. /* Disables the TESTDAC to let DAC signal pass through. */
  896. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  897. NAU8825_BIAS_TESTDAC_EN, 0);
  898. break;
  899. case SND_SOC_DAPM_POST_PMD:
  900. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  901. NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
  902. break;
  903. default:
  904. return -EINVAL;
  905. }
  906. return 0;
  907. }
  908. static int nau8825_biq_coeff_get(struct snd_kcontrol *kcontrol,
  909. struct snd_ctl_elem_value *ucontrol)
  910. {
  911. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  912. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  913. if (!component->regmap)
  914. return -EINVAL;
  915. regmap_raw_read(component->regmap, NAU8825_REG_BIQ_COF1,
  916. ucontrol->value.bytes.data, params->max);
  917. return 0;
  918. }
  919. static int nau8825_biq_coeff_put(struct snd_kcontrol *kcontrol,
  920. struct snd_ctl_elem_value *ucontrol)
  921. {
  922. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  923. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  924. void *data;
  925. if (!component->regmap)
  926. return -EINVAL;
  927. data = kmemdup(ucontrol->value.bytes.data,
  928. params->max, GFP_KERNEL | GFP_DMA);
  929. if (!data)
  930. return -ENOMEM;
  931. regmap_update_bits(component->regmap, NAU8825_REG_BIQ_CTRL,
  932. NAU8825_BIQ_WRT_EN, 0);
  933. regmap_raw_write(component->regmap, NAU8825_REG_BIQ_COF1,
  934. data, params->max);
  935. regmap_update_bits(component->regmap, NAU8825_REG_BIQ_CTRL,
  936. NAU8825_BIQ_WRT_EN, NAU8825_BIQ_WRT_EN);
  937. kfree(data);
  938. return 0;
  939. }
  940. static const char * const nau8825_biq_path[] = {
  941. "ADC", "DAC"
  942. };
  943. static const struct soc_enum nau8825_biq_path_enum =
  944. SOC_ENUM_SINGLE(NAU8825_REG_BIQ_CTRL, NAU8825_BIQ_PATH_SFT,
  945. ARRAY_SIZE(nau8825_biq_path), nau8825_biq_path);
  946. static const char * const nau8825_adc_decimation[] = {
  947. "32", "64", "128", "256"
  948. };
  949. static const struct soc_enum nau8825_adc_decimation_enum =
  950. SOC_ENUM_SINGLE(NAU8825_REG_ADC_RATE, NAU8825_ADC_SYNC_DOWN_SFT,
  951. ARRAY_SIZE(nau8825_adc_decimation), nau8825_adc_decimation);
  952. static const char * const nau8825_dac_oversampl[] = {
  953. "64", "256", "128", "", "32"
  954. };
  955. static const struct soc_enum nau8825_dac_oversampl_enum =
  956. SOC_ENUM_SINGLE(NAU8825_REG_DAC_CTRL1, NAU8825_DAC_OVERSAMPLE_SFT,
  957. ARRAY_SIZE(nau8825_dac_oversampl), nau8825_dac_oversampl);
  958. static const DECLARE_TLV_DB_MINMAX_MUTE(adc_vol_tlv, -10300, 2400);
  959. static const DECLARE_TLV_DB_MINMAX_MUTE(sidetone_vol_tlv, -4200, 0);
  960. static const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -5400, 0);
  961. static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600);
  962. static const DECLARE_TLV_DB_MINMAX_MUTE(crosstalk_vol_tlv, -9600, 2400);
  963. static const struct snd_kcontrol_new nau8825_controls[] = {
  964. SOC_SINGLE_TLV("Mic Volume", NAU8825_REG_ADC_DGAIN_CTRL,
  965. 0, 0xff, 0, adc_vol_tlv),
  966. SOC_DOUBLE_TLV("Headphone Bypass Volume", NAU8825_REG_ADC_DGAIN_CTRL,
  967. 12, 8, 0x0f, 0, sidetone_vol_tlv),
  968. SOC_DOUBLE_TLV("Headphone Volume", NAU8825_REG_HSVOL_CTRL,
  969. 6, 0, 0x3f, 1, dac_vol_tlv),
  970. SOC_SINGLE_TLV("Frontend PGA Volume", NAU8825_REG_POWER_UP_CONTROL,
  971. 8, 37, 0, fepga_gain_tlv),
  972. SOC_DOUBLE_TLV("Headphone Crosstalk Volume", NAU8825_REG_DAC_DGAIN_CTRL,
  973. 0, 8, 0xff, 0, crosstalk_vol_tlv),
  974. SOC_ENUM("ADC Decimation Rate", nau8825_adc_decimation_enum),
  975. SOC_ENUM("DAC Oversampling Rate", nau8825_dac_oversampl_enum),
  976. /* programmable biquad filter */
  977. SOC_ENUM("BIQ Path Select", nau8825_biq_path_enum),
  978. SND_SOC_BYTES_EXT("BIQ Coefficients", 20,
  979. nau8825_biq_coeff_get, nau8825_biq_coeff_put),
  980. };
  981. /* DAC Mux 0x33[9] and 0x34[9] */
  982. static const char * const nau8825_dac_src[] = {
  983. "DACL", "DACR",
  984. };
  985. static SOC_ENUM_SINGLE_DECL(
  986. nau8825_dacl_enum, NAU8825_REG_DACL_CTRL,
  987. NAU8825_DACL_CH_SEL_SFT, nau8825_dac_src);
  988. static SOC_ENUM_SINGLE_DECL(
  989. nau8825_dacr_enum, NAU8825_REG_DACR_CTRL,
  990. NAU8825_DACR_CH_SEL_SFT, nau8825_dac_src);
  991. static const struct snd_kcontrol_new nau8825_dacl_mux =
  992. SOC_DAPM_ENUM("DACL Source", nau8825_dacl_enum);
  993. static const struct snd_kcontrol_new nau8825_dacr_mux =
  994. SOC_DAPM_ENUM("DACR Source", nau8825_dacr_enum);
  995. static const struct snd_soc_dapm_widget nau8825_dapm_widgets[] = {
  996. SND_SOC_DAPM_AIF_OUT("AIFTX", "Capture", 0, NAU8825_REG_I2S_PCM_CTRL2,
  997. 15, 1),
  998. SND_SOC_DAPM_INPUT("MIC"),
  999. SND_SOC_DAPM_MICBIAS("MICBIAS", NAU8825_REG_MIC_BIAS, 8, 0),
  1000. SND_SOC_DAPM_PGA("Frontend PGA", NAU8825_REG_POWER_UP_CONTROL, 14, 0,
  1001. NULL, 0),
  1002. SND_SOC_DAPM_ADC_E("ADC", NULL, SND_SOC_NOPM, 0, 0,
  1003. nau8825_adc_event, SND_SOC_DAPM_POST_PMU |
  1004. SND_SOC_DAPM_POST_PMD),
  1005. SND_SOC_DAPM_SUPPLY("ADC Clock", NAU8825_REG_ENA_CTRL, 7, 0, NULL, 0),
  1006. SND_SOC_DAPM_SUPPLY("ADC Power", NAU8825_REG_ANALOG_ADC_2, 6, 0, NULL,
  1007. 0),
  1008. /* ADC for button press detection. A dapm supply widget is used to
  1009. * prevent dapm_power_widgets keeping the codec at SND_SOC_BIAS_ON
  1010. * during suspend.
  1011. */
  1012. SND_SOC_DAPM_SUPPLY("SAR", NAU8825_REG_SAR_CTRL,
  1013. NAU8825_SAR_ADC_EN_SFT, 0, NULL, 0),
  1014. SND_SOC_DAPM_PGA_S("ADACL", 2, NAU8825_REG_RDAC, 12, 0, NULL, 0),
  1015. SND_SOC_DAPM_PGA_S("ADACR", 2, NAU8825_REG_RDAC, 13, 0, NULL, 0),
  1016. SND_SOC_DAPM_PGA_S("ADACL Clock", 3, NAU8825_REG_RDAC, 8, 0, NULL, 0),
  1017. SND_SOC_DAPM_PGA_S("ADACR Clock", 3, NAU8825_REG_RDAC, 9, 0, NULL, 0),
  1018. SND_SOC_DAPM_DAC("DDACR", NULL, NAU8825_REG_ENA_CTRL,
  1019. NAU8825_ENABLE_DACR_SFT, 0),
  1020. SND_SOC_DAPM_DAC("DDACL", NULL, NAU8825_REG_ENA_CTRL,
  1021. NAU8825_ENABLE_DACL_SFT, 0),
  1022. SND_SOC_DAPM_SUPPLY("DDAC Clock", NAU8825_REG_ENA_CTRL, 6, 0, NULL, 0),
  1023. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacl_mux),
  1024. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacr_mux),
  1025. SND_SOC_DAPM_PGA_S("HP amp L", 0,
  1026. NAU8825_REG_CLASSG_CTRL, 1, 0, NULL, 0),
  1027. SND_SOC_DAPM_PGA_S("HP amp R", 0,
  1028. NAU8825_REG_CLASSG_CTRL, 2, 0, NULL, 0),
  1029. SND_SOC_DAPM_PGA_S("Charge Pump", 1, NAU8825_REG_CHARGE_PUMP, 5, 0,
  1030. nau8825_pump_event, SND_SOC_DAPM_POST_PMU |
  1031. SND_SOC_DAPM_PRE_PMD),
  1032. SND_SOC_DAPM_PGA_S("Output Driver R Stage 1", 4,
  1033. NAU8825_REG_POWER_UP_CONTROL, 5, 0, NULL, 0),
  1034. SND_SOC_DAPM_PGA_S("Output Driver L Stage 1", 4,
  1035. NAU8825_REG_POWER_UP_CONTROL, 4, 0, NULL, 0),
  1036. SND_SOC_DAPM_PGA_S("Output Driver R Stage 2", 5,
  1037. NAU8825_REG_POWER_UP_CONTROL, 3, 0, NULL, 0),
  1038. SND_SOC_DAPM_PGA_S("Output Driver L Stage 2", 5,
  1039. NAU8825_REG_POWER_UP_CONTROL, 2, 0, NULL, 0),
  1040. SND_SOC_DAPM_PGA_S("Output Driver R Stage 3", 6,
  1041. NAU8825_REG_POWER_UP_CONTROL, 1, 0, NULL, 0),
  1042. SND_SOC_DAPM_PGA_S("Output Driver L Stage 3", 6,
  1043. NAU8825_REG_POWER_UP_CONTROL, 0, 0, NULL, 0),
  1044. SND_SOC_DAPM_PGA_S("Output DACL", 7,
  1045. NAU8825_REG_CHARGE_PUMP, 8, 1, nau8825_output_dac_event,
  1046. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1047. SND_SOC_DAPM_PGA_S("Output DACR", 7,
  1048. NAU8825_REG_CHARGE_PUMP, 9, 1, nau8825_output_dac_event,
  1049. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1050. /* HPOL/R are ungrounded by disabling 16 Ohm pull-downs on playback */
  1051. SND_SOC_DAPM_PGA_S("HPOL Pulldown", 8,
  1052. NAU8825_REG_HSD_CTRL, 0, 1, NULL, 0),
  1053. SND_SOC_DAPM_PGA_S("HPOR Pulldown", 8,
  1054. NAU8825_REG_HSD_CTRL, 1, 1, NULL, 0),
  1055. /* High current HPOL/R boost driver */
  1056. SND_SOC_DAPM_PGA_S("HP Boost Driver", 9,
  1057. NAU8825_REG_BOOST, 9, 1, NULL, 0),
  1058. /* Class G operation control*/
  1059. SND_SOC_DAPM_PGA_S("Class G", 10,
  1060. NAU8825_REG_CLASSG_CTRL, 0, 0, NULL, 0),
  1061. SND_SOC_DAPM_OUTPUT("HPOL"),
  1062. SND_SOC_DAPM_OUTPUT("HPOR"),
  1063. };
  1064. static const struct snd_soc_dapm_route nau8825_dapm_routes[] = {
  1065. {"Frontend PGA", NULL, "MIC"},
  1066. {"ADC", NULL, "Frontend PGA"},
  1067. {"ADC", NULL, "ADC Clock"},
  1068. {"ADC", NULL, "ADC Power"},
  1069. {"AIFTX", NULL, "ADC"},
  1070. {"DDACL", NULL, "Playback"},
  1071. {"DDACR", NULL, "Playback"},
  1072. {"DDACL", NULL, "DDAC Clock"},
  1073. {"DDACR", NULL, "DDAC Clock"},
  1074. {"DACL Mux", "DACL", "DDACL"},
  1075. {"DACL Mux", "DACR", "DDACR"},
  1076. {"DACR Mux", "DACL", "DDACL"},
  1077. {"DACR Mux", "DACR", "DDACR"},
  1078. {"HP amp L", NULL, "DACL Mux"},
  1079. {"HP amp R", NULL, "DACR Mux"},
  1080. {"Charge Pump", NULL, "HP amp L"},
  1081. {"Charge Pump", NULL, "HP amp R"},
  1082. {"ADACL", NULL, "Charge Pump"},
  1083. {"ADACR", NULL, "Charge Pump"},
  1084. {"ADACL Clock", NULL, "ADACL"},
  1085. {"ADACR Clock", NULL, "ADACR"},
  1086. {"Output Driver L Stage 1", NULL, "ADACL Clock"},
  1087. {"Output Driver R Stage 1", NULL, "ADACR Clock"},
  1088. {"Output Driver L Stage 2", NULL, "Output Driver L Stage 1"},
  1089. {"Output Driver R Stage 2", NULL, "Output Driver R Stage 1"},
  1090. {"Output Driver L Stage 3", NULL, "Output Driver L Stage 2"},
  1091. {"Output Driver R Stage 3", NULL, "Output Driver R Stage 2"},
  1092. {"Output DACL", NULL, "Output Driver L Stage 3"},
  1093. {"Output DACR", NULL, "Output Driver R Stage 3"},
  1094. {"HPOL Pulldown", NULL, "Output DACL"},
  1095. {"HPOR Pulldown", NULL, "Output DACR"},
  1096. {"HP Boost Driver", NULL, "HPOL Pulldown"},
  1097. {"HP Boost Driver", NULL, "HPOR Pulldown"},
  1098. {"Class G", NULL, "HP Boost Driver"},
  1099. {"HPOL", NULL, "Class G"},
  1100. {"HPOR", NULL, "Class G"},
  1101. };
  1102. static int nau8825_clock_check(struct nau8825 *nau8825,
  1103. int stream, int rate, int osr)
  1104. {
  1105. int osrate;
  1106. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1107. if (osr >= ARRAY_SIZE(osr_dac_sel))
  1108. return -EINVAL;
  1109. osrate = osr_dac_sel[osr].osr;
  1110. } else {
  1111. if (osr >= ARRAY_SIZE(osr_adc_sel))
  1112. return -EINVAL;
  1113. osrate = osr_adc_sel[osr].osr;
  1114. }
  1115. if (!osrate || rate * osr > CLK_DA_AD_MAX) {
  1116. dev_err(nau8825->dev, "exceed the maximum frequency of CLK_ADC or CLK_DAC\n");
  1117. return -EINVAL;
  1118. }
  1119. return 0;
  1120. }
  1121. static int nau8825_hw_params(struct snd_pcm_substream *substream,
  1122. struct snd_pcm_hw_params *params,
  1123. struct snd_soc_dai *dai)
  1124. {
  1125. struct snd_soc_codec *codec = dai->codec;
  1126. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1127. unsigned int val_len = 0, osr, ctrl_val, bclk_fs, bclk_div;
  1128. nau8825_sema_acquire(nau8825, 3 * HZ);
  1129. /* CLK_DAC or CLK_ADC = OSR * FS
  1130. * DAC or ADC clock frequency is defined as Over Sampling Rate (OSR)
  1131. * multiplied by the audio sample rate (Fs). Note that the OSR and Fs
  1132. * values must be selected such that the maximum frequency is less
  1133. * than 6.144 MHz.
  1134. */
  1135. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1136. regmap_read(nau8825->regmap, NAU8825_REG_DAC_CTRL1, &osr);
  1137. osr &= NAU8825_DAC_OVERSAMPLE_MASK;
  1138. if (nau8825_clock_check(nau8825, substream->stream,
  1139. params_rate(params), osr)) {
  1140. nau8825_sema_release(nau8825);
  1141. return -EINVAL;
  1142. }
  1143. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  1144. NAU8825_CLK_DAC_SRC_MASK,
  1145. osr_dac_sel[osr].clk_src << NAU8825_CLK_DAC_SRC_SFT);
  1146. } else {
  1147. regmap_read(nau8825->regmap, NAU8825_REG_ADC_RATE, &osr);
  1148. osr &= NAU8825_ADC_SYNC_DOWN_MASK;
  1149. if (nau8825_clock_check(nau8825, substream->stream,
  1150. params_rate(params), osr)) {
  1151. nau8825_sema_release(nau8825);
  1152. return -EINVAL;
  1153. }
  1154. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  1155. NAU8825_CLK_ADC_SRC_MASK,
  1156. osr_adc_sel[osr].clk_src << NAU8825_CLK_ADC_SRC_SFT);
  1157. }
  1158. /* make BCLK and LRC divde configuration if the codec as master. */
  1159. regmap_read(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2, &ctrl_val);
  1160. if (ctrl_val & NAU8825_I2S_MS_MASTER) {
  1161. /* get the bclk and fs ratio */
  1162. bclk_fs = snd_soc_params_to_bclk(params) / params_rate(params);
  1163. if (bclk_fs <= 32)
  1164. bclk_div = 2;
  1165. else if (bclk_fs <= 64)
  1166. bclk_div = 1;
  1167. else if (bclk_fs <= 128)
  1168. bclk_div = 0;
  1169. else {
  1170. nau8825_sema_release(nau8825);
  1171. return -EINVAL;
  1172. }
  1173. regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
  1174. NAU8825_I2S_LRC_DIV_MASK | NAU8825_I2S_BLK_DIV_MASK,
  1175. ((bclk_div + 1) << NAU8825_I2S_LRC_DIV_SFT) | bclk_div);
  1176. }
  1177. switch (params_width(params)) {
  1178. case 16:
  1179. val_len |= NAU8825_I2S_DL_16;
  1180. break;
  1181. case 20:
  1182. val_len |= NAU8825_I2S_DL_20;
  1183. break;
  1184. case 24:
  1185. val_len |= NAU8825_I2S_DL_24;
  1186. break;
  1187. case 32:
  1188. val_len |= NAU8825_I2S_DL_32;
  1189. break;
  1190. default:
  1191. nau8825_sema_release(nau8825);
  1192. return -EINVAL;
  1193. }
  1194. regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1,
  1195. NAU8825_I2S_DL_MASK, val_len);
  1196. /* Release the semaphore. */
  1197. nau8825_sema_release(nau8825);
  1198. return 0;
  1199. }
  1200. static int nau8825_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  1201. {
  1202. struct snd_soc_codec *codec = codec_dai->codec;
  1203. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1204. unsigned int ctrl1_val = 0, ctrl2_val = 0;
  1205. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1206. case SND_SOC_DAIFMT_CBM_CFM:
  1207. ctrl2_val |= NAU8825_I2S_MS_MASTER;
  1208. break;
  1209. case SND_SOC_DAIFMT_CBS_CFS:
  1210. break;
  1211. default:
  1212. return -EINVAL;
  1213. }
  1214. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1215. case SND_SOC_DAIFMT_NB_NF:
  1216. break;
  1217. case SND_SOC_DAIFMT_IB_NF:
  1218. ctrl1_val |= NAU8825_I2S_BP_INV;
  1219. break;
  1220. default:
  1221. return -EINVAL;
  1222. }
  1223. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1224. case SND_SOC_DAIFMT_I2S:
  1225. ctrl1_val |= NAU8825_I2S_DF_I2S;
  1226. break;
  1227. case SND_SOC_DAIFMT_LEFT_J:
  1228. ctrl1_val |= NAU8825_I2S_DF_LEFT;
  1229. break;
  1230. case SND_SOC_DAIFMT_RIGHT_J:
  1231. ctrl1_val |= NAU8825_I2S_DF_RIGTH;
  1232. break;
  1233. case SND_SOC_DAIFMT_DSP_A:
  1234. ctrl1_val |= NAU8825_I2S_DF_PCM_AB;
  1235. break;
  1236. case SND_SOC_DAIFMT_DSP_B:
  1237. ctrl1_val |= NAU8825_I2S_DF_PCM_AB;
  1238. ctrl1_val |= NAU8825_I2S_PCMB_EN;
  1239. break;
  1240. default:
  1241. return -EINVAL;
  1242. }
  1243. nau8825_sema_acquire(nau8825, 3 * HZ);
  1244. regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1,
  1245. NAU8825_I2S_DL_MASK | NAU8825_I2S_DF_MASK |
  1246. NAU8825_I2S_BP_MASK | NAU8825_I2S_PCMB_MASK,
  1247. ctrl1_val);
  1248. regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
  1249. NAU8825_I2S_MS_MASK, ctrl2_val);
  1250. /* Release the semaphore. */
  1251. nau8825_sema_release(nau8825);
  1252. return 0;
  1253. }
  1254. static const struct snd_soc_dai_ops nau8825_dai_ops = {
  1255. .hw_params = nau8825_hw_params,
  1256. .set_fmt = nau8825_set_dai_fmt,
  1257. };
  1258. #define NAU8825_RATES SNDRV_PCM_RATE_8000_192000
  1259. #define NAU8825_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
  1260. | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  1261. static struct snd_soc_dai_driver nau8825_dai = {
  1262. .name = "nau8825-hifi",
  1263. .playback = {
  1264. .stream_name = "Playback",
  1265. .channels_min = 1,
  1266. .channels_max = 2,
  1267. .rates = NAU8825_RATES,
  1268. .formats = NAU8825_FORMATS,
  1269. },
  1270. .capture = {
  1271. .stream_name = "Capture",
  1272. .channels_min = 1,
  1273. .channels_max = 1,
  1274. .rates = NAU8825_RATES,
  1275. .formats = NAU8825_FORMATS,
  1276. },
  1277. .ops = &nau8825_dai_ops,
  1278. };
  1279. /**
  1280. * nau8825_enable_jack_detect - Specify a jack for event reporting
  1281. *
  1282. * @component: component to register the jack with
  1283. * @jack: jack to use to report headset and button events on
  1284. *
  1285. * After this function has been called the headset insert/remove and button
  1286. * events will be routed to the given jack. Jack can be null to stop
  1287. * reporting.
  1288. */
  1289. int nau8825_enable_jack_detect(struct snd_soc_codec *codec,
  1290. struct snd_soc_jack *jack)
  1291. {
  1292. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1293. struct regmap *regmap = nau8825->regmap;
  1294. nau8825->jack = jack;
  1295. /* Ground HP Outputs[1:0], needed for headset auto detection
  1296. * Enable Automatic Mic/Gnd switching reading on insert interrupt[6]
  1297. */
  1298. regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL,
  1299. NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L,
  1300. NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L);
  1301. return 0;
  1302. }
  1303. EXPORT_SYMBOL_GPL(nau8825_enable_jack_detect);
  1304. static bool nau8825_is_jack_inserted(struct regmap *regmap)
  1305. {
  1306. bool active_high, is_high;
  1307. int status, jkdet;
  1308. regmap_read(regmap, NAU8825_REG_JACK_DET_CTRL, &jkdet);
  1309. active_high = jkdet & NAU8825_JACK_POLARITY;
  1310. regmap_read(regmap, NAU8825_REG_I2C_DEVICE_ID, &status);
  1311. is_high = status & NAU8825_GPIO2JD1;
  1312. /* return jack connection status according to jack insertion logic
  1313. * active high or active low.
  1314. */
  1315. return active_high == is_high;
  1316. }
  1317. static void nau8825_restart_jack_detection(struct regmap *regmap)
  1318. {
  1319. /* this will restart the entire jack detection process including MIC/GND
  1320. * switching and create interrupts. We have to go from 0 to 1 and back
  1321. * to 0 to restart.
  1322. */
  1323. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1324. NAU8825_JACK_DET_RESTART, NAU8825_JACK_DET_RESTART);
  1325. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1326. NAU8825_JACK_DET_RESTART, 0);
  1327. }
  1328. static void nau8825_int_status_clear_all(struct regmap *regmap)
  1329. {
  1330. int active_irq, clear_irq, i;
  1331. /* Reset the intrruption status from rightmost bit if the corres-
  1332. * ponding irq event occurs.
  1333. */
  1334. regmap_read(regmap, NAU8825_REG_IRQ_STATUS, &active_irq);
  1335. for (i = 0; i < NAU8825_REG_DATA_LEN; i++) {
  1336. clear_irq = (0x1 << i);
  1337. if (active_irq & clear_irq)
  1338. regmap_write(regmap,
  1339. NAU8825_REG_INT_CLR_KEY_STATUS, clear_irq);
  1340. }
  1341. }
  1342. static void nau8825_eject_jack(struct nau8825 *nau8825)
  1343. {
  1344. struct snd_soc_dapm_context *dapm = nau8825->dapm;
  1345. struct regmap *regmap = nau8825->regmap;
  1346. /* Force to cancel the cross talk detection process */
  1347. nau8825_xtalk_cancel(nau8825);
  1348. snd_soc_dapm_disable_pin(dapm, "SAR");
  1349. snd_soc_dapm_disable_pin(dapm, "MICBIAS");
  1350. /* Detach 2kOhm Resistors from MICBIAS to MICGND1/2 */
  1351. regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
  1352. NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2, 0);
  1353. /* ground HPL/HPR, MICGRND1/2 */
  1354. regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 0xf, 0xf);
  1355. snd_soc_dapm_sync(dapm);
  1356. /* Clear all interruption status */
  1357. nau8825_int_status_clear_all(regmap);
  1358. /* Enable the insertion interruption, disable the ejection inter-
  1359. * ruption, and then bypass de-bounce circuit.
  1360. */
  1361. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_DIS_CTRL,
  1362. NAU8825_IRQ_EJECT_DIS | NAU8825_IRQ_INSERT_DIS,
  1363. NAU8825_IRQ_EJECT_DIS);
  1364. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  1365. NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_EJECT_EN |
  1366. NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_INSERT_EN,
  1367. NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_EJECT_EN |
  1368. NAU8825_IRQ_HEADSET_COMPLETE_EN);
  1369. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1370. NAU8825_JACK_DET_DB_BYPASS, NAU8825_JACK_DET_DB_BYPASS);
  1371. /* Disable ADC needed for interruptions at audo mode */
  1372. regmap_update_bits(regmap, NAU8825_REG_ENA_CTRL,
  1373. NAU8825_ENABLE_ADC, 0);
  1374. /* Close clock for jack type detection at manual mode */
  1375. nau8825_configure_sysclk(nau8825, NAU8825_CLK_DIS, 0);
  1376. }
  1377. /* Enable audo mode interruptions with internal clock. */
  1378. static void nau8825_setup_auto_irq(struct nau8825 *nau8825)
  1379. {
  1380. struct regmap *regmap = nau8825->regmap;
  1381. /* Enable headset jack type detection complete interruption and
  1382. * jack ejection interruption.
  1383. */
  1384. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  1385. NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_EJECT_EN, 0);
  1386. /* Enable internal VCO needed for interruptions */
  1387. nau8825_configure_sysclk(nau8825, NAU8825_CLK_INTERNAL, 0);
  1388. /* Enable ADC needed for interruptions */
  1389. regmap_update_bits(regmap, NAU8825_REG_ENA_CTRL,
  1390. NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC);
  1391. /* Chip needs one FSCLK cycle in order to generate interruptions,
  1392. * as we cannot guarantee one will be provided by the system. Turning
  1393. * master mode on then off enables us to generate that FSCLK cycle
  1394. * with a minimum of contention on the clock bus.
  1395. */
  1396. regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2,
  1397. NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_MASTER);
  1398. regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2,
  1399. NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_SLAVE);
  1400. /* Not bypass de-bounce circuit */
  1401. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1402. NAU8825_JACK_DET_DB_BYPASS, 0);
  1403. /* Unmask all interruptions */
  1404. regmap_write(regmap, NAU8825_REG_INTERRUPT_DIS_CTRL, 0);
  1405. /* Restart the jack detection process at auto mode */
  1406. nau8825_restart_jack_detection(regmap);
  1407. }
  1408. static int nau8825_button_decode(int value)
  1409. {
  1410. int buttons = 0;
  1411. /* The chip supports up to 8 buttons, but ALSA defines only 6 buttons */
  1412. if (value & BIT(0))
  1413. buttons |= SND_JACK_BTN_0;
  1414. if (value & BIT(1))
  1415. buttons |= SND_JACK_BTN_1;
  1416. if (value & BIT(2))
  1417. buttons |= SND_JACK_BTN_2;
  1418. if (value & BIT(3))
  1419. buttons |= SND_JACK_BTN_3;
  1420. if (value & BIT(4))
  1421. buttons |= SND_JACK_BTN_4;
  1422. if (value & BIT(5))
  1423. buttons |= SND_JACK_BTN_5;
  1424. return buttons;
  1425. }
  1426. static int nau8825_jack_insert(struct nau8825 *nau8825)
  1427. {
  1428. struct regmap *regmap = nau8825->regmap;
  1429. struct snd_soc_dapm_context *dapm = nau8825->dapm;
  1430. int jack_status_reg, mic_detected;
  1431. int type = 0;
  1432. regmap_read(regmap, NAU8825_REG_GENERAL_STATUS, &jack_status_reg);
  1433. mic_detected = (jack_status_reg >> 10) & 3;
  1434. /* The JKSLV and JKR2 all detected in high impedance headset */
  1435. if (mic_detected == 0x3)
  1436. nau8825->high_imped = true;
  1437. else
  1438. nau8825->high_imped = false;
  1439. switch (mic_detected) {
  1440. case 0:
  1441. /* no mic */
  1442. type = SND_JACK_HEADPHONE;
  1443. break;
  1444. case 1:
  1445. dev_dbg(nau8825->dev, "OMTP (micgnd1) mic connected\n");
  1446. type = SND_JACK_HEADSET;
  1447. /* Unground MICGND1 */
  1448. regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2,
  1449. 1 << 2);
  1450. /* Attach 2kOhm Resistor from MICBIAS to MICGND1 */
  1451. regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
  1452. NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2,
  1453. NAU8825_MICBIAS_JKR2);
  1454. /* Attach SARADC to MICGND1 */
  1455. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1456. NAU8825_SAR_INPUT_MASK,
  1457. NAU8825_SAR_INPUT_JKR2);
  1458. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
  1459. snd_soc_dapm_force_enable_pin(dapm, "SAR");
  1460. snd_soc_dapm_sync(dapm);
  1461. break;
  1462. case 2:
  1463. dev_dbg(nau8825->dev, "CTIA (micgnd2) mic connected\n");
  1464. type = SND_JACK_HEADSET;
  1465. /* Unground MICGND2 */
  1466. regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2,
  1467. 2 << 2);
  1468. /* Attach 2kOhm Resistor from MICBIAS to MICGND2 */
  1469. regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
  1470. NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2,
  1471. NAU8825_MICBIAS_JKSLV);
  1472. /* Attach SARADC to MICGND2 */
  1473. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1474. NAU8825_SAR_INPUT_MASK,
  1475. NAU8825_SAR_INPUT_JKSLV);
  1476. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
  1477. snd_soc_dapm_force_enable_pin(dapm, "SAR");
  1478. snd_soc_dapm_sync(dapm);
  1479. break;
  1480. case 3:
  1481. /* detect error case */
  1482. dev_err(nau8825->dev, "detection error; disable mic function\n");
  1483. type = SND_JACK_HEADPHONE;
  1484. break;
  1485. }
  1486. /* Leaving HPOL/R grounded after jack insert by default. They will be
  1487. * ungrounded as part of the widget power up sequence at the beginning
  1488. * of playback to reduce pop.
  1489. */
  1490. return type;
  1491. }
  1492. #define NAU8825_BUTTONS (SND_JACK_BTN_0 | SND_JACK_BTN_1 | \
  1493. SND_JACK_BTN_2 | SND_JACK_BTN_3)
  1494. static irqreturn_t nau8825_interrupt(int irq, void *data)
  1495. {
  1496. struct nau8825 *nau8825 = (struct nau8825 *)data;
  1497. struct regmap *regmap = nau8825->regmap;
  1498. int active_irq, clear_irq = 0, event = 0, event_mask = 0;
  1499. if (regmap_read(regmap, NAU8825_REG_IRQ_STATUS, &active_irq)) {
  1500. dev_err(nau8825->dev, "failed to read irq status\n");
  1501. return IRQ_NONE;
  1502. }
  1503. if ((active_irq & NAU8825_JACK_EJECTION_IRQ_MASK) ==
  1504. NAU8825_JACK_EJECTION_DETECTED) {
  1505. nau8825_eject_jack(nau8825);
  1506. event_mask |= SND_JACK_HEADSET;
  1507. clear_irq = NAU8825_JACK_EJECTION_IRQ_MASK;
  1508. } else if (active_irq & NAU8825_KEY_SHORT_PRESS_IRQ) {
  1509. int key_status;
  1510. regmap_read(regmap, NAU8825_REG_INT_CLR_KEY_STATUS,
  1511. &key_status);
  1512. /* upper 8 bits of the register are for short pressed keys,
  1513. * lower 8 bits - for long pressed buttons
  1514. */
  1515. nau8825->button_pressed = nau8825_button_decode(
  1516. key_status >> 8);
  1517. event |= nau8825->button_pressed;
  1518. event_mask |= NAU8825_BUTTONS;
  1519. clear_irq = NAU8825_KEY_SHORT_PRESS_IRQ;
  1520. } else if (active_irq & NAU8825_KEY_RELEASE_IRQ) {
  1521. event_mask = NAU8825_BUTTONS;
  1522. clear_irq = NAU8825_KEY_RELEASE_IRQ;
  1523. } else if (active_irq & NAU8825_HEADSET_COMPLETION_IRQ) {
  1524. if (nau8825_is_jack_inserted(regmap)) {
  1525. event |= nau8825_jack_insert(nau8825);
  1526. if (nau8825->xtalk_enable && !nau8825->high_imped) {
  1527. /* Apply the cross talk suppression in the
  1528. * headset without high impedance.
  1529. */
  1530. if (!nau8825->xtalk_protect) {
  1531. /* Raise protection for cross talk de-
  1532. * tection if no protection before.
  1533. * The driver has to cancel the pro-
  1534. * cess and restore changes if process
  1535. * is ongoing when ejection.
  1536. */
  1537. int ret;
  1538. nau8825->xtalk_protect = true;
  1539. ret = nau8825_sema_acquire(nau8825, 0);
  1540. if (ret)
  1541. nau8825->xtalk_protect = false;
  1542. }
  1543. /* Startup cross talk detection process */
  1544. if (nau8825->xtalk_protect) {
  1545. nau8825->xtalk_state =
  1546. NAU8825_XTALK_PREPARE;
  1547. schedule_work(&nau8825->xtalk_work);
  1548. }
  1549. } else {
  1550. /* The cross talk suppression shouldn't apply
  1551. * in the headset with high impedance. Thus,
  1552. * relieve the protection raised before.
  1553. */
  1554. if (nau8825->xtalk_protect) {
  1555. nau8825_sema_release(nau8825);
  1556. nau8825->xtalk_protect = false;
  1557. }
  1558. }
  1559. } else {
  1560. dev_warn(nau8825->dev, "Headset completion IRQ fired but no headset connected\n");
  1561. nau8825_eject_jack(nau8825);
  1562. }
  1563. event_mask |= SND_JACK_HEADSET;
  1564. clear_irq = NAU8825_HEADSET_COMPLETION_IRQ;
  1565. /* Record the interruption report event for driver to report
  1566. * the event later. The jack report will delay until cross
  1567. * talk detection process is done.
  1568. */
  1569. if (nau8825->xtalk_state == NAU8825_XTALK_PREPARE) {
  1570. nau8825->xtalk_event = event;
  1571. nau8825->xtalk_event_mask = event_mask;
  1572. }
  1573. } else if (active_irq & NAU8825_IMPEDANCE_MEAS_IRQ) {
  1574. /* crosstalk detection enable and process on going */
  1575. if (nau8825->xtalk_enable && nau8825->xtalk_protect)
  1576. schedule_work(&nau8825->xtalk_work);
  1577. clear_irq = NAU8825_IMPEDANCE_MEAS_IRQ;
  1578. } else if ((active_irq & NAU8825_JACK_INSERTION_IRQ_MASK) ==
  1579. NAU8825_JACK_INSERTION_DETECTED) {
  1580. /* One more step to check GPIO status directly. Thus, the
  1581. * driver can confirm the real insertion interruption because
  1582. * the intrruption at manual mode has bypassed debounce
  1583. * circuit which can get rid of unstable status.
  1584. */
  1585. if (nau8825_is_jack_inserted(regmap)) {
  1586. /* Turn off insertion interruption at manual mode */
  1587. regmap_update_bits(regmap,
  1588. NAU8825_REG_INTERRUPT_DIS_CTRL,
  1589. NAU8825_IRQ_INSERT_DIS,
  1590. NAU8825_IRQ_INSERT_DIS);
  1591. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  1592. NAU8825_IRQ_INSERT_EN, NAU8825_IRQ_INSERT_EN);
  1593. /* Enable interruption for jack type detection at audo
  1594. * mode which can detect microphone and jack type.
  1595. */
  1596. nau8825_setup_auto_irq(nau8825);
  1597. }
  1598. }
  1599. if (!clear_irq)
  1600. clear_irq = active_irq;
  1601. /* clears the rightmost interruption */
  1602. regmap_write(regmap, NAU8825_REG_INT_CLR_KEY_STATUS, clear_irq);
  1603. /* Delay jack report until cross talk detection is done. It can avoid
  1604. * application to do playback preparation when cross talk detection
  1605. * process is still working. Otherwise, the resource like clock and
  1606. * power will be issued by them at the same time and conflict happens.
  1607. */
  1608. if (event_mask && nau8825->xtalk_state == NAU8825_XTALK_DONE)
  1609. snd_soc_jack_report(nau8825->jack, event, event_mask);
  1610. return IRQ_HANDLED;
  1611. }
  1612. static void nau8825_setup_buttons(struct nau8825 *nau8825)
  1613. {
  1614. struct regmap *regmap = nau8825->regmap;
  1615. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1616. NAU8825_SAR_TRACKING_GAIN_MASK,
  1617. nau8825->sar_voltage << NAU8825_SAR_TRACKING_GAIN_SFT);
  1618. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1619. NAU8825_SAR_COMPARE_TIME_MASK,
  1620. nau8825->sar_compare_time << NAU8825_SAR_COMPARE_TIME_SFT);
  1621. regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
  1622. NAU8825_SAR_SAMPLING_TIME_MASK,
  1623. nau8825->sar_sampling_time << NAU8825_SAR_SAMPLING_TIME_SFT);
  1624. regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
  1625. NAU8825_KEYDET_LEVELS_NR_MASK,
  1626. (nau8825->sar_threshold_num - 1) << NAU8825_KEYDET_LEVELS_NR_SFT);
  1627. regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
  1628. NAU8825_KEYDET_HYSTERESIS_MASK,
  1629. nau8825->sar_hysteresis << NAU8825_KEYDET_HYSTERESIS_SFT);
  1630. regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
  1631. NAU8825_KEYDET_SHORTKEY_DEBOUNCE_MASK,
  1632. nau8825->key_debounce << NAU8825_KEYDET_SHORTKEY_DEBOUNCE_SFT);
  1633. regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_1,
  1634. (nau8825->sar_threshold[0] << 8) | nau8825->sar_threshold[1]);
  1635. regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_2,
  1636. (nau8825->sar_threshold[2] << 8) | nau8825->sar_threshold[3]);
  1637. regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_3,
  1638. (nau8825->sar_threshold[4] << 8) | nau8825->sar_threshold[5]);
  1639. regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_4,
  1640. (nau8825->sar_threshold[6] << 8) | nau8825->sar_threshold[7]);
  1641. /* Enable short press and release interruptions */
  1642. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  1643. NAU8825_IRQ_KEY_SHORT_PRESS_EN | NAU8825_IRQ_KEY_RELEASE_EN,
  1644. 0);
  1645. }
  1646. static void nau8825_init_regs(struct nau8825 *nau8825)
  1647. {
  1648. struct regmap *regmap = nau8825->regmap;
  1649. /* Latch IIC LSB value */
  1650. regmap_write(regmap, NAU8825_REG_IIC_ADDR_SET, 0x0001);
  1651. /* Enable Bias/Vmid */
  1652. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  1653. NAU8825_BIAS_VMID, NAU8825_BIAS_VMID);
  1654. regmap_update_bits(nau8825->regmap, NAU8825_REG_BOOST,
  1655. NAU8825_GLOBAL_BIAS_EN, NAU8825_GLOBAL_BIAS_EN);
  1656. /* VMID Tieoff */
  1657. regmap_update_bits(regmap, NAU8825_REG_BIAS_ADJ,
  1658. NAU8825_BIAS_VMID_SEL_MASK,
  1659. nau8825->vref_impedance << NAU8825_BIAS_VMID_SEL_SFT);
  1660. /* Disable Boost Driver, Automatic Short circuit protection enable */
  1661. regmap_update_bits(regmap, NAU8825_REG_BOOST,
  1662. NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS |
  1663. NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN,
  1664. NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS |
  1665. NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN);
  1666. regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
  1667. NAU8825_JKDET_OUTPUT_EN,
  1668. nau8825->jkdet_enable ? 0 : NAU8825_JKDET_OUTPUT_EN);
  1669. regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
  1670. NAU8825_JKDET_PULL_EN,
  1671. nau8825->jkdet_pull_enable ? 0 : NAU8825_JKDET_PULL_EN);
  1672. regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
  1673. NAU8825_JKDET_PULL_UP,
  1674. nau8825->jkdet_pull_up ? NAU8825_JKDET_PULL_UP : 0);
  1675. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1676. NAU8825_JACK_POLARITY,
  1677. /* jkdet_polarity - 1 is for active-low */
  1678. nau8825->jkdet_polarity ? 0 : NAU8825_JACK_POLARITY);
  1679. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1680. NAU8825_JACK_INSERT_DEBOUNCE_MASK,
  1681. nau8825->jack_insert_debounce << NAU8825_JACK_INSERT_DEBOUNCE_SFT);
  1682. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  1683. NAU8825_JACK_EJECT_DEBOUNCE_MASK,
  1684. nau8825->jack_eject_debounce << NAU8825_JACK_EJECT_DEBOUNCE_SFT);
  1685. /* Mask unneeded IRQs: 1 - disable, 0 - enable */
  1686. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK, 0x7ff, 0x7ff);
  1687. regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
  1688. NAU8825_MICBIAS_VOLTAGE_MASK, nau8825->micbias_voltage);
  1689. if (nau8825->sar_threshold_num)
  1690. nau8825_setup_buttons(nau8825);
  1691. /* Default oversampling/decimations settings are unusable
  1692. * (audible hiss). Set it to something better.
  1693. */
  1694. regmap_update_bits(regmap, NAU8825_REG_ADC_RATE,
  1695. NAU8825_ADC_SYNC_DOWN_MASK | NAU8825_ADC_SINC4_EN,
  1696. NAU8825_ADC_SYNC_DOWN_64);
  1697. regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1,
  1698. NAU8825_DAC_OVERSAMPLE_MASK, NAU8825_DAC_OVERSAMPLE_64);
  1699. /* Disable DACR/L power */
  1700. regmap_update_bits(regmap, NAU8825_REG_CHARGE_PUMP,
  1701. NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL,
  1702. NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL);
  1703. /* Enable TESTDAC. This sets the analog DAC inputs to a '0' input
  1704. * signal to avoid any glitches due to power up transients in both
  1705. * the analog and digital DAC circuit.
  1706. */
  1707. regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
  1708. NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
  1709. /* CICCLP off */
  1710. regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1,
  1711. NAU8825_DAC_CLIP_OFF, NAU8825_DAC_CLIP_OFF);
  1712. /* Class AB bias current to 2x, DAC Capacitor enable MSB/LSB */
  1713. regmap_update_bits(regmap, NAU8825_REG_ANALOG_CONTROL_2,
  1714. NAU8825_HP_NON_CLASSG_CURRENT_2xADJ |
  1715. NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB,
  1716. NAU8825_HP_NON_CLASSG_CURRENT_2xADJ |
  1717. NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB);
  1718. /* Class G timer 64ms */
  1719. regmap_update_bits(regmap, NAU8825_REG_CLASSG_CTRL,
  1720. NAU8825_CLASSG_TIMER_MASK,
  1721. 0x20 << NAU8825_CLASSG_TIMER_SFT);
  1722. /* DAC clock delay 2ns, VREF */
  1723. regmap_update_bits(regmap, NAU8825_REG_RDAC,
  1724. NAU8825_RDAC_CLK_DELAY_MASK | NAU8825_RDAC_VREF_MASK,
  1725. (0x2 << NAU8825_RDAC_CLK_DELAY_SFT) |
  1726. (0x3 << NAU8825_RDAC_VREF_SFT));
  1727. /* Config L/R channel */
  1728. regmap_update_bits(nau8825->regmap, NAU8825_REG_DACL_CTRL,
  1729. NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_L);
  1730. regmap_update_bits(nau8825->regmap, NAU8825_REG_DACR_CTRL,
  1731. NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_R);
  1732. /* Disable short Frame Sync detection logic */
  1733. regmap_update_bits(regmap, NAU8825_REG_LEFT_TIME_SLOT,
  1734. NAU8825_DIS_FS_SHORT_DET, NAU8825_DIS_FS_SHORT_DET);
  1735. }
  1736. static const struct regmap_config nau8825_regmap_config = {
  1737. .val_bits = NAU8825_REG_DATA_LEN,
  1738. .reg_bits = NAU8825_REG_ADDR_LEN,
  1739. .max_register = NAU8825_REG_MAX,
  1740. .readable_reg = nau8825_readable_reg,
  1741. .writeable_reg = nau8825_writeable_reg,
  1742. .volatile_reg = nau8825_volatile_reg,
  1743. .cache_type = REGCACHE_RBTREE,
  1744. .reg_defaults = nau8825_reg_defaults,
  1745. .num_reg_defaults = ARRAY_SIZE(nau8825_reg_defaults),
  1746. };
  1747. static int nau8825_codec_probe(struct snd_soc_codec *codec)
  1748. {
  1749. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1750. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  1751. nau8825->dapm = dapm;
  1752. return 0;
  1753. }
  1754. static int nau8825_codec_remove(struct snd_soc_codec *codec)
  1755. {
  1756. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1757. /* Cancel and reset cross tak suppresstion detection funciton */
  1758. nau8825_xtalk_cancel(nau8825);
  1759. return 0;
  1760. }
  1761. /**
  1762. * nau8825_calc_fll_param - Calculate FLL parameters.
  1763. * @fll_in: external clock provided to codec.
  1764. * @fs: sampling rate.
  1765. * @fll_param: Pointer to structure of FLL parameters.
  1766. *
  1767. * Calculate FLL parameters to configure codec.
  1768. *
  1769. * Returns 0 for success or negative error code.
  1770. */
  1771. static int nau8825_calc_fll_param(unsigned int fll_in, unsigned int fs,
  1772. struct nau8825_fll *fll_param)
  1773. {
  1774. u64 fvco, fvco_max;
  1775. unsigned int fref, i, fvco_sel;
  1776. /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by dividing
  1777. * freq_in by 1, 2, 4, or 8 using FLL pre-scalar.
  1778. * FREF = freq_in / NAU8825_FLL_REF_DIV_MASK
  1779. */
  1780. for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) {
  1781. fref = fll_in / fll_pre_scalar[i].param;
  1782. if (fref <= NAU_FREF_MAX)
  1783. break;
  1784. }
  1785. if (i == ARRAY_SIZE(fll_pre_scalar))
  1786. return -EINVAL;
  1787. fll_param->clk_ref_div = fll_pre_scalar[i].val;
  1788. /* Choose the FLL ratio based on FREF */
  1789. for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) {
  1790. if (fref >= fll_ratio[i].param)
  1791. break;
  1792. }
  1793. if (i == ARRAY_SIZE(fll_ratio))
  1794. return -EINVAL;
  1795. fll_param->ratio = fll_ratio[i].val;
  1796. /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs.
  1797. * FDCO must be within the 90MHz - 124MHz or the FFL cannot be
  1798. * guaranteed across the full range of operation.
  1799. * FDCO = freq_out * 2 * mclk_src_scaling
  1800. */
  1801. fvco_max = 0;
  1802. fvco_sel = ARRAY_SIZE(mclk_src_scaling);
  1803. for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) {
  1804. fvco = 256 * fs * 2 * mclk_src_scaling[i].param;
  1805. if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX &&
  1806. fvco_max < fvco) {
  1807. fvco_max = fvco;
  1808. fvco_sel = i;
  1809. }
  1810. }
  1811. if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel)
  1812. return -EINVAL;
  1813. fll_param->mclk_src = mclk_src_scaling[fvco_sel].val;
  1814. /* Calculate the FLL 10-bit integer input and the FLL 16-bit fractional
  1815. * input based on FDCO, FREF and FLL ratio.
  1816. */
  1817. fvco = div_u64(fvco_max << 16, fref * fll_param->ratio);
  1818. fll_param->fll_int = (fvco >> 16) & 0x3FF;
  1819. fll_param->fll_frac = fvco & 0xFFFF;
  1820. return 0;
  1821. }
  1822. static void nau8825_fll_apply(struct nau8825 *nau8825,
  1823. struct nau8825_fll *fll_param)
  1824. {
  1825. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  1826. NAU8825_CLK_SRC_MASK | NAU8825_CLK_MCLK_SRC_MASK,
  1827. NAU8825_CLK_SRC_MCLK | fll_param->mclk_src);
  1828. /* Make DSP operate at high speed for better performance. */
  1829. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL1,
  1830. NAU8825_FLL_RATIO_MASK | NAU8825_ICTRL_LATCH_MASK,
  1831. fll_param->ratio | (0x6 << NAU8825_ICTRL_LATCH_SFT));
  1832. /* FLL 16-bit fractional input */
  1833. regmap_write(nau8825->regmap, NAU8825_REG_FLL2, fll_param->fll_frac);
  1834. /* FLL 10-bit integer input */
  1835. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL3,
  1836. NAU8825_FLL_INTEGER_MASK, fll_param->fll_int);
  1837. /* FLL pre-scaler */
  1838. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL4,
  1839. NAU8825_FLL_REF_DIV_MASK,
  1840. fll_param->clk_ref_div << NAU8825_FLL_REF_DIV_SFT);
  1841. /* select divided VCO input */
  1842. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
  1843. NAU8825_FLL_CLK_SW_MASK, NAU8825_FLL_CLK_SW_REF);
  1844. /* Disable free-running mode */
  1845. regmap_update_bits(nau8825->regmap,
  1846. NAU8825_REG_FLL6, NAU8825_DCO_EN, 0);
  1847. if (fll_param->fll_frac) {
  1848. /* set FLL loop filter enable and cutoff frequency at 500Khz */
  1849. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
  1850. NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
  1851. NAU8825_FLL_FTR_SW_MASK,
  1852. NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
  1853. NAU8825_FLL_FTR_SW_FILTER);
  1854. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6,
  1855. NAU8825_SDM_EN | NAU8825_CUTOFF500,
  1856. NAU8825_SDM_EN | NAU8825_CUTOFF500);
  1857. } else {
  1858. /* disable FLL loop filter and cutoff frequency */
  1859. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
  1860. NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
  1861. NAU8825_FLL_FTR_SW_MASK, NAU8825_FLL_FTR_SW_ACCU);
  1862. regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6,
  1863. NAU8825_SDM_EN | NAU8825_CUTOFF500, 0);
  1864. }
  1865. }
  1866. /* freq_out must be 256*Fs in order to achieve the best performance */
  1867. static int nau8825_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  1868. unsigned int freq_in, unsigned int freq_out)
  1869. {
  1870. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  1871. struct nau8825_fll fll_param;
  1872. int ret, fs;
  1873. fs = freq_out / 256;
  1874. ret = nau8825_calc_fll_param(freq_in, fs, &fll_param);
  1875. if (ret < 0) {
  1876. dev_err(codec->dev, "Unsupported input clock %d\n", freq_in);
  1877. return ret;
  1878. }
  1879. dev_dbg(codec->dev, "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n",
  1880. fll_param.mclk_src, fll_param.ratio, fll_param.fll_frac,
  1881. fll_param.fll_int, fll_param.clk_ref_div);
  1882. nau8825_fll_apply(nau8825, &fll_param);
  1883. mdelay(2);
  1884. regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
  1885. NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
  1886. return 0;
  1887. }
  1888. static int nau8825_mclk_prepare(struct nau8825 *nau8825, unsigned int freq)
  1889. {
  1890. int ret = 0;
  1891. nau8825->mclk = devm_clk_get(nau8825->dev, "mclk");
  1892. if (IS_ERR(nau8825->mclk)) {
  1893. dev_info(nau8825->dev, "No 'mclk' clock found, assume MCLK is managed externally");
  1894. return 0;
  1895. }
  1896. if (!nau8825->mclk_freq) {
  1897. ret = clk_prepare_enable(nau8825->mclk);
  1898. if (ret) {
  1899. dev_err(nau8825->dev, "Unable to prepare codec mclk\n");
  1900. return ret;
  1901. }
  1902. }
  1903. if (nau8825->mclk_freq != freq) {
  1904. freq = clk_round_rate(nau8825->mclk, freq);
  1905. ret = clk_set_rate(nau8825->mclk, freq);
  1906. if (ret) {
  1907. dev_err(nau8825->dev, "Unable to set mclk rate\n");
  1908. return ret;
  1909. }
  1910. nau8825->mclk_freq = freq;
  1911. }
  1912. return 0;
  1913. }
  1914. static void nau8825_configure_mclk_as_sysclk(struct regmap *regmap)
  1915. {
  1916. regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
  1917. NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_MCLK);
  1918. regmap_update_bits(regmap, NAU8825_REG_FLL6,
  1919. NAU8825_DCO_EN, 0);
  1920. /* Make DSP operate as default setting for power saving. */
  1921. regmap_update_bits(regmap, NAU8825_REG_FLL1,
  1922. NAU8825_ICTRL_LATCH_MASK, 0);
  1923. }
  1924. static int nau8825_configure_sysclk(struct nau8825 *nau8825, int clk_id,
  1925. unsigned int freq)
  1926. {
  1927. struct regmap *regmap = nau8825->regmap;
  1928. int ret;
  1929. switch (clk_id) {
  1930. case NAU8825_CLK_DIS:
  1931. /* Clock provided externally and disable internal VCO clock */
  1932. nau8825_configure_mclk_as_sysclk(regmap);
  1933. if (nau8825->mclk_freq) {
  1934. clk_disable_unprepare(nau8825->mclk);
  1935. nau8825->mclk_freq = 0;
  1936. }
  1937. break;
  1938. case NAU8825_CLK_MCLK:
  1939. /* Acquire the semaphore to synchronize the playback and
  1940. * interrupt handler. In order to avoid the playback inter-
  1941. * fered by cross talk process, the driver make the playback
  1942. * preparation halted until cross talk process finish.
  1943. */
  1944. nau8825_sema_acquire(nau8825, 3 * HZ);
  1945. nau8825_configure_mclk_as_sysclk(regmap);
  1946. /* MCLK not changed by clock tree */
  1947. regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
  1948. NAU8825_CLK_MCLK_SRC_MASK, 0);
  1949. /* Release the semaphore. */
  1950. nau8825_sema_release(nau8825);
  1951. ret = nau8825_mclk_prepare(nau8825, freq);
  1952. if (ret)
  1953. return ret;
  1954. break;
  1955. case NAU8825_CLK_INTERNAL:
  1956. if (nau8825_is_jack_inserted(nau8825->regmap)) {
  1957. regmap_update_bits(regmap, NAU8825_REG_FLL6,
  1958. NAU8825_DCO_EN, NAU8825_DCO_EN);
  1959. regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
  1960. NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
  1961. /* Decrease the VCO frequency and make DSP operate
  1962. * as default setting for power saving.
  1963. */
  1964. regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
  1965. NAU8825_CLK_MCLK_SRC_MASK, 0xf);
  1966. regmap_update_bits(regmap, NAU8825_REG_FLL1,
  1967. NAU8825_ICTRL_LATCH_MASK |
  1968. NAU8825_FLL_RATIO_MASK, 0x10);
  1969. regmap_update_bits(regmap, NAU8825_REG_FLL6,
  1970. NAU8825_SDM_EN, NAU8825_SDM_EN);
  1971. } else {
  1972. /* The clock turns off intentionally for power saving
  1973. * when no headset connected.
  1974. */
  1975. nau8825_configure_mclk_as_sysclk(regmap);
  1976. dev_warn(nau8825->dev, "Disable clock for power saving when no headset connected\n");
  1977. }
  1978. if (nau8825->mclk_freq) {
  1979. clk_disable_unprepare(nau8825->mclk);
  1980. nau8825->mclk_freq = 0;
  1981. }
  1982. break;
  1983. case NAU8825_CLK_FLL_MCLK:
  1984. /* Acquire the semaphore to synchronize the playback and
  1985. * interrupt handler. In order to avoid the playback inter-
  1986. * fered by cross talk process, the driver make the playback
  1987. * preparation halted until cross talk process finish.
  1988. */
  1989. nau8825_sema_acquire(nau8825, 3 * HZ);
  1990. /* Higher FLL reference input frequency can only set lower
  1991. * gain error, such as 0000 for input reference from MCLK
  1992. * 12.288Mhz.
  1993. */
  1994. regmap_update_bits(regmap, NAU8825_REG_FLL3,
  1995. NAU8825_FLL_CLK_SRC_MASK | NAU8825_GAIN_ERR_MASK,
  1996. NAU8825_FLL_CLK_SRC_MCLK | 0);
  1997. /* Release the semaphore. */
  1998. nau8825_sema_release(nau8825);
  1999. ret = nau8825_mclk_prepare(nau8825, freq);
  2000. if (ret)
  2001. return ret;
  2002. break;
  2003. case NAU8825_CLK_FLL_BLK:
  2004. /* Acquire the semaphore to synchronize the playback and
  2005. * interrupt handler. In order to avoid the playback inter-
  2006. * fered by cross talk process, the driver make the playback
  2007. * preparation halted until cross talk process finish.
  2008. */
  2009. nau8825_sema_acquire(nau8825, 3 * HZ);
  2010. /* If FLL reference input is from low frequency source,
  2011. * higher error gain can apply such as 0xf which has
  2012. * the most sensitive gain error correction threshold,
  2013. * Therefore, FLL has the most accurate DCO to
  2014. * target frequency.
  2015. */
  2016. regmap_update_bits(regmap, NAU8825_REG_FLL3,
  2017. NAU8825_FLL_CLK_SRC_MASK | NAU8825_GAIN_ERR_MASK,
  2018. NAU8825_FLL_CLK_SRC_BLK |
  2019. (0xf << NAU8825_GAIN_ERR_SFT));
  2020. /* Release the semaphore. */
  2021. nau8825_sema_release(nau8825);
  2022. if (nau8825->mclk_freq) {
  2023. clk_disable_unprepare(nau8825->mclk);
  2024. nau8825->mclk_freq = 0;
  2025. }
  2026. break;
  2027. case NAU8825_CLK_FLL_FS:
  2028. /* Acquire the semaphore to synchronize the playback and
  2029. * interrupt handler. In order to avoid the playback inter-
  2030. * fered by cross talk process, the driver make the playback
  2031. * preparation halted until cross talk process finish.
  2032. */
  2033. nau8825_sema_acquire(nau8825, 3 * HZ);
  2034. /* If FLL reference input is from low frequency source,
  2035. * higher error gain can apply such as 0xf which has
  2036. * the most sensitive gain error correction threshold,
  2037. * Therefore, FLL has the most accurate DCO to
  2038. * target frequency.
  2039. */
  2040. regmap_update_bits(regmap, NAU8825_REG_FLL3,
  2041. NAU8825_FLL_CLK_SRC_MASK | NAU8825_GAIN_ERR_MASK,
  2042. NAU8825_FLL_CLK_SRC_FS |
  2043. (0xf << NAU8825_GAIN_ERR_SFT));
  2044. /* Release the semaphore. */
  2045. nau8825_sema_release(nau8825);
  2046. if (nau8825->mclk_freq) {
  2047. clk_disable_unprepare(nau8825->mclk);
  2048. nau8825->mclk_freq = 0;
  2049. }
  2050. break;
  2051. default:
  2052. dev_err(nau8825->dev, "Invalid clock id (%d)\n", clk_id);
  2053. return -EINVAL;
  2054. }
  2055. dev_dbg(nau8825->dev, "Sysclk is %dHz and clock id is %d\n", freq,
  2056. clk_id);
  2057. return 0;
  2058. }
  2059. static int nau8825_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  2060. int source, unsigned int freq, int dir)
  2061. {
  2062. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  2063. return nau8825_configure_sysclk(nau8825, clk_id, freq);
  2064. }
  2065. static int nau8825_resume_setup(struct nau8825 *nau8825)
  2066. {
  2067. struct regmap *regmap = nau8825->regmap;
  2068. /* Close clock when jack type detection at manual mode */
  2069. nau8825_configure_sysclk(nau8825, NAU8825_CLK_DIS, 0);
  2070. /* Clear all interruption status */
  2071. nau8825_int_status_clear_all(regmap);
  2072. /* Enable both insertion and ejection interruptions, and then
  2073. * bypass de-bounce circuit.
  2074. */
  2075. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
  2076. NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_HEADSET_COMPLETE_EN |
  2077. NAU8825_IRQ_EJECT_EN | NAU8825_IRQ_INSERT_EN,
  2078. NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_HEADSET_COMPLETE_EN);
  2079. regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
  2080. NAU8825_JACK_DET_DB_BYPASS, NAU8825_JACK_DET_DB_BYPASS);
  2081. regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_DIS_CTRL,
  2082. NAU8825_IRQ_INSERT_DIS | NAU8825_IRQ_EJECT_DIS, 0);
  2083. return 0;
  2084. }
  2085. static int nau8825_set_bias_level(struct snd_soc_codec *codec,
  2086. enum snd_soc_bias_level level)
  2087. {
  2088. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  2089. int ret;
  2090. switch (level) {
  2091. case SND_SOC_BIAS_ON:
  2092. break;
  2093. case SND_SOC_BIAS_PREPARE:
  2094. break;
  2095. case SND_SOC_BIAS_STANDBY:
  2096. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  2097. if (nau8825->mclk_freq) {
  2098. ret = clk_prepare_enable(nau8825->mclk);
  2099. if (ret) {
  2100. dev_err(nau8825->dev, "Unable to prepare codec mclk\n");
  2101. return ret;
  2102. }
  2103. }
  2104. /* Setup codec configuration after resume */
  2105. nau8825_resume_setup(nau8825);
  2106. }
  2107. break;
  2108. case SND_SOC_BIAS_OFF:
  2109. /* Reset the configuration of jack type for detection */
  2110. /* Detach 2kOhm Resistors from MICBIAS to MICGND1/2 */
  2111. regmap_update_bits(nau8825->regmap, NAU8825_REG_MIC_BIAS,
  2112. NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2, 0);
  2113. /* ground HPL/HPR, MICGRND1/2 */
  2114. regmap_update_bits(nau8825->regmap,
  2115. NAU8825_REG_HSD_CTRL, 0xf, 0xf);
  2116. /* Cancel and reset cross talk detection funciton */
  2117. nau8825_xtalk_cancel(nau8825);
  2118. /* Turn off all interruptions before system shutdown. Keep the
  2119. * interruption quiet before resume setup completes.
  2120. */
  2121. regmap_write(nau8825->regmap,
  2122. NAU8825_REG_INTERRUPT_DIS_CTRL, 0xffff);
  2123. /* Disable ADC needed for interruptions at audo mode */
  2124. regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
  2125. NAU8825_ENABLE_ADC, 0);
  2126. if (nau8825->mclk_freq)
  2127. clk_disable_unprepare(nau8825->mclk);
  2128. break;
  2129. }
  2130. return 0;
  2131. }
  2132. static int __maybe_unused nau8825_suspend(struct snd_soc_codec *codec)
  2133. {
  2134. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  2135. disable_irq(nau8825->irq);
  2136. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
  2137. /* Power down codec power; don't suppoet button wakeup */
  2138. snd_soc_dapm_disable_pin(nau8825->dapm, "SAR");
  2139. snd_soc_dapm_disable_pin(nau8825->dapm, "MICBIAS");
  2140. snd_soc_dapm_sync(nau8825->dapm);
  2141. regcache_cache_only(nau8825->regmap, true);
  2142. regcache_mark_dirty(nau8825->regmap);
  2143. return 0;
  2144. }
  2145. static int __maybe_unused nau8825_resume(struct snd_soc_codec *codec)
  2146. {
  2147. struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
  2148. int ret;
  2149. regcache_cache_only(nau8825->regmap, false);
  2150. regcache_sync(nau8825->regmap);
  2151. nau8825->xtalk_protect = true;
  2152. ret = nau8825_sema_acquire(nau8825, 0);
  2153. if (ret)
  2154. nau8825->xtalk_protect = false;
  2155. enable_irq(nau8825->irq);
  2156. return 0;
  2157. }
  2158. static const struct snd_soc_codec_driver nau8825_codec_driver = {
  2159. .probe = nau8825_codec_probe,
  2160. .remove = nau8825_codec_remove,
  2161. .set_sysclk = nau8825_set_sysclk,
  2162. .set_pll = nau8825_set_pll,
  2163. .set_bias_level = nau8825_set_bias_level,
  2164. .suspend_bias_off = true,
  2165. .suspend = nau8825_suspend,
  2166. .resume = nau8825_resume,
  2167. .component_driver = {
  2168. .controls = nau8825_controls,
  2169. .num_controls = ARRAY_SIZE(nau8825_controls),
  2170. .dapm_widgets = nau8825_dapm_widgets,
  2171. .num_dapm_widgets = ARRAY_SIZE(nau8825_dapm_widgets),
  2172. .dapm_routes = nau8825_dapm_routes,
  2173. .num_dapm_routes = ARRAY_SIZE(nau8825_dapm_routes),
  2174. },
  2175. };
  2176. static void nau8825_reset_chip(struct regmap *regmap)
  2177. {
  2178. regmap_write(regmap, NAU8825_REG_RESET, 0x00);
  2179. regmap_write(regmap, NAU8825_REG_RESET, 0x00);
  2180. }
  2181. static void nau8825_print_device_properties(struct nau8825 *nau8825)
  2182. {
  2183. int i;
  2184. struct device *dev = nau8825->dev;
  2185. dev_dbg(dev, "jkdet-enable: %d\n", nau8825->jkdet_enable);
  2186. dev_dbg(dev, "jkdet-pull-enable: %d\n", nau8825->jkdet_pull_enable);
  2187. dev_dbg(dev, "jkdet-pull-up: %d\n", nau8825->jkdet_pull_up);
  2188. dev_dbg(dev, "jkdet-polarity: %d\n", nau8825->jkdet_polarity);
  2189. dev_dbg(dev, "micbias-voltage: %d\n", nau8825->micbias_voltage);
  2190. dev_dbg(dev, "vref-impedance: %d\n", nau8825->vref_impedance);
  2191. dev_dbg(dev, "sar-threshold-num: %d\n", nau8825->sar_threshold_num);
  2192. for (i = 0; i < nau8825->sar_threshold_num; i++)
  2193. dev_dbg(dev, "sar-threshold[%d]=%d\n", i,
  2194. nau8825->sar_threshold[i]);
  2195. dev_dbg(dev, "sar-hysteresis: %d\n", nau8825->sar_hysteresis);
  2196. dev_dbg(dev, "sar-voltage: %d\n", nau8825->sar_voltage);
  2197. dev_dbg(dev, "sar-compare-time: %d\n", nau8825->sar_compare_time);
  2198. dev_dbg(dev, "sar-sampling-time: %d\n", nau8825->sar_sampling_time);
  2199. dev_dbg(dev, "short-key-debounce: %d\n", nau8825->key_debounce);
  2200. dev_dbg(dev, "jack-insert-debounce: %d\n",
  2201. nau8825->jack_insert_debounce);
  2202. dev_dbg(dev, "jack-eject-debounce: %d\n",
  2203. nau8825->jack_eject_debounce);
  2204. dev_dbg(dev, "crosstalk-enable: %d\n",
  2205. nau8825->xtalk_enable);
  2206. }
  2207. static int nau8825_read_device_properties(struct device *dev,
  2208. struct nau8825 *nau8825) {
  2209. int ret;
  2210. nau8825->jkdet_enable = device_property_read_bool(dev,
  2211. "nuvoton,jkdet-enable");
  2212. nau8825->jkdet_pull_enable = device_property_read_bool(dev,
  2213. "nuvoton,jkdet-pull-enable");
  2214. nau8825->jkdet_pull_up = device_property_read_bool(dev,
  2215. "nuvoton,jkdet-pull-up");
  2216. ret = device_property_read_u32(dev, "nuvoton,jkdet-polarity",
  2217. &nau8825->jkdet_polarity);
  2218. if (ret)
  2219. nau8825->jkdet_polarity = 1;
  2220. ret = device_property_read_u32(dev, "nuvoton,micbias-voltage",
  2221. &nau8825->micbias_voltage);
  2222. if (ret)
  2223. nau8825->micbias_voltage = 6;
  2224. ret = device_property_read_u32(dev, "nuvoton,vref-impedance",
  2225. &nau8825->vref_impedance);
  2226. if (ret)
  2227. nau8825->vref_impedance = 2;
  2228. ret = device_property_read_u32(dev, "nuvoton,sar-threshold-num",
  2229. &nau8825->sar_threshold_num);
  2230. if (ret)
  2231. nau8825->sar_threshold_num = 4;
  2232. ret = device_property_read_u32_array(dev, "nuvoton,sar-threshold",
  2233. nau8825->sar_threshold, nau8825->sar_threshold_num);
  2234. if (ret) {
  2235. nau8825->sar_threshold[0] = 0x08;
  2236. nau8825->sar_threshold[1] = 0x12;
  2237. nau8825->sar_threshold[2] = 0x26;
  2238. nau8825->sar_threshold[3] = 0x73;
  2239. }
  2240. ret = device_property_read_u32(dev, "nuvoton,sar-hysteresis",
  2241. &nau8825->sar_hysteresis);
  2242. if (ret)
  2243. nau8825->sar_hysteresis = 0;
  2244. ret = device_property_read_u32(dev, "nuvoton,sar-voltage",
  2245. &nau8825->sar_voltage);
  2246. if (ret)
  2247. nau8825->sar_voltage = 6;
  2248. ret = device_property_read_u32(dev, "nuvoton,sar-compare-time",
  2249. &nau8825->sar_compare_time);
  2250. if (ret)
  2251. nau8825->sar_compare_time = 1;
  2252. ret = device_property_read_u32(dev, "nuvoton,sar-sampling-time",
  2253. &nau8825->sar_sampling_time);
  2254. if (ret)
  2255. nau8825->sar_sampling_time = 1;
  2256. ret = device_property_read_u32(dev, "nuvoton,short-key-debounce",
  2257. &nau8825->key_debounce);
  2258. if (ret)
  2259. nau8825->key_debounce = 3;
  2260. ret = device_property_read_u32(dev, "nuvoton,jack-insert-debounce",
  2261. &nau8825->jack_insert_debounce);
  2262. if (ret)
  2263. nau8825->jack_insert_debounce = 7;
  2264. ret = device_property_read_u32(dev, "nuvoton,jack-eject-debounce",
  2265. &nau8825->jack_eject_debounce);
  2266. if (ret)
  2267. nau8825->jack_eject_debounce = 0;
  2268. nau8825->xtalk_enable = device_property_read_bool(dev,
  2269. "nuvoton,crosstalk-enable");
  2270. nau8825->mclk = devm_clk_get(dev, "mclk");
  2271. if (PTR_ERR(nau8825->mclk) == -EPROBE_DEFER) {
  2272. return -EPROBE_DEFER;
  2273. } else if (PTR_ERR(nau8825->mclk) == -ENOENT) {
  2274. /* The MCLK is managed externally or not used at all */
  2275. nau8825->mclk = NULL;
  2276. dev_info(dev, "No 'mclk' clock found, assume MCLK is managed externally");
  2277. } else if (IS_ERR(nau8825->mclk)) {
  2278. return -EINVAL;
  2279. }
  2280. return 0;
  2281. }
  2282. static int nau8825_setup_irq(struct nau8825 *nau8825)
  2283. {
  2284. int ret;
  2285. ret = devm_request_threaded_irq(nau8825->dev, nau8825->irq, NULL,
  2286. nau8825_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  2287. "nau8825", nau8825);
  2288. if (ret) {
  2289. dev_err(nau8825->dev, "Cannot request irq %d (%d)\n",
  2290. nau8825->irq, ret);
  2291. return ret;
  2292. }
  2293. return 0;
  2294. }
  2295. static int nau8825_i2c_probe(struct i2c_client *i2c,
  2296. const struct i2c_device_id *id)
  2297. {
  2298. struct device *dev = &i2c->dev;
  2299. struct nau8825 *nau8825 = dev_get_platdata(&i2c->dev);
  2300. int ret, value;
  2301. if (!nau8825) {
  2302. nau8825 = devm_kzalloc(dev, sizeof(*nau8825), GFP_KERNEL);
  2303. if (!nau8825)
  2304. return -ENOMEM;
  2305. ret = nau8825_read_device_properties(dev, nau8825);
  2306. if (ret)
  2307. return ret;
  2308. }
  2309. i2c_set_clientdata(i2c, nau8825);
  2310. nau8825->regmap = devm_regmap_init_i2c(i2c, &nau8825_regmap_config);
  2311. if (IS_ERR(nau8825->regmap))
  2312. return PTR_ERR(nau8825->regmap);
  2313. nau8825->dev = dev;
  2314. nau8825->irq = i2c->irq;
  2315. /* Initiate parameters, semaphore and work queue which are needed in
  2316. * cross talk suppression measurment function.
  2317. */
  2318. nau8825->xtalk_state = NAU8825_XTALK_DONE;
  2319. nau8825->xtalk_protect = false;
  2320. nau8825->xtalk_baktab_initialized = false;
  2321. sema_init(&nau8825->xtalk_sem, 1);
  2322. INIT_WORK(&nau8825->xtalk_work, nau8825_xtalk_work);
  2323. nau8825_print_device_properties(nau8825);
  2324. nau8825_reset_chip(nau8825->regmap);
  2325. ret = regmap_read(nau8825->regmap, NAU8825_REG_I2C_DEVICE_ID, &value);
  2326. if (ret < 0) {
  2327. dev_err(dev, "Failed to read device id from the NAU8825: %d\n",
  2328. ret);
  2329. return ret;
  2330. }
  2331. if ((value & NAU8825_SOFTWARE_ID_MASK) !=
  2332. NAU8825_SOFTWARE_ID_NAU8825) {
  2333. dev_err(dev, "Not a NAU8825 chip\n");
  2334. return -ENODEV;
  2335. }
  2336. nau8825_init_regs(nau8825);
  2337. if (i2c->irq)
  2338. nau8825_setup_irq(nau8825);
  2339. return snd_soc_register_codec(&i2c->dev, &nau8825_codec_driver,
  2340. &nau8825_dai, 1);
  2341. }
  2342. static int nau8825_i2c_remove(struct i2c_client *client)
  2343. {
  2344. snd_soc_unregister_codec(&client->dev);
  2345. return 0;
  2346. }
  2347. static const struct i2c_device_id nau8825_i2c_ids[] = {
  2348. { "nau8825", 0 },
  2349. { }
  2350. };
  2351. MODULE_DEVICE_TABLE(i2c, nau8825_i2c_ids);
  2352. #ifdef CONFIG_OF
  2353. static const struct of_device_id nau8825_of_ids[] = {
  2354. { .compatible = "nuvoton,nau8825", },
  2355. {}
  2356. };
  2357. MODULE_DEVICE_TABLE(of, nau8825_of_ids);
  2358. #endif
  2359. #ifdef CONFIG_ACPI
  2360. static const struct acpi_device_id nau8825_acpi_match[] = {
  2361. { "10508825", 0 },
  2362. {},
  2363. };
  2364. MODULE_DEVICE_TABLE(acpi, nau8825_acpi_match);
  2365. #endif
  2366. static struct i2c_driver nau8825_driver = {
  2367. .driver = {
  2368. .name = "nau8825",
  2369. .of_match_table = of_match_ptr(nau8825_of_ids),
  2370. .acpi_match_table = ACPI_PTR(nau8825_acpi_match),
  2371. },
  2372. .probe = nau8825_i2c_probe,
  2373. .remove = nau8825_i2c_remove,
  2374. .id_table = nau8825_i2c_ids,
  2375. };
  2376. module_i2c_driver(nau8825_driver);
  2377. MODULE_DESCRIPTION("ASoC nau8825 driver");
  2378. MODULE_AUTHOR("Anatol Pomozov <anatol@chromium.org>");
  2379. MODULE_LICENSE("GPL");