nau8825.c 80 KB

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