da7219.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. /*
  2. * da7219.c - DA7219 ALSA SoC Codec Driver
  3. *
  4. * Copyright (c) 2015 Dialog Semiconductor
  5. *
  6. * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/acpi.h>
  14. #include <linux/clk.h>
  15. #include <linux/i2c.h>
  16. #include <linux/of_device.h>
  17. #include <linux/property.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/pm.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/soc-dapm.h>
  28. #include <sound/initval.h>
  29. #include <sound/tlv.h>
  30. #include <asm/div64.h>
  31. #include <sound/da7219.h>
  32. #include "da7219.h"
  33. #include "da7219-aad.h"
  34. /*
  35. * TLVs and Enums
  36. */
  37. /* Input TLVs */
  38. static const DECLARE_TLV_DB_SCALE(da7219_mic_gain_tlv, -600, 600, 0);
  39. static const DECLARE_TLV_DB_SCALE(da7219_mixin_gain_tlv, -450, 150, 0);
  40. static const DECLARE_TLV_DB_SCALE(da7219_adc_dig_gain_tlv, -8325, 75, 0);
  41. static const DECLARE_TLV_DB_SCALE(da7219_alc_threshold_tlv, -9450, 150, 0);
  42. static const DECLARE_TLV_DB_SCALE(da7219_alc_gain_tlv, 0, 600, 0);
  43. static const DECLARE_TLV_DB_SCALE(da7219_alc_ana_gain_tlv, 0, 600, 0);
  44. static const DECLARE_TLV_DB_SCALE(da7219_sidetone_gain_tlv, -4200, 300, 0);
  45. static const DECLARE_TLV_DB_SCALE(da7219_tonegen_gain_tlv, -4500, 300, 0);
  46. /* Output TLVs */
  47. static const DECLARE_TLV_DB_SCALE(da7219_dac_eq_band_tlv, -1050, 150, 0);
  48. static const DECLARE_TLV_DB_RANGE(da7219_dac_dig_gain_tlv,
  49. 0x0, 0x07, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  50. /* -77.25dB to 12dB */
  51. 0x08, 0x7f, TLV_DB_SCALE_ITEM(-7725, 75, 0)
  52. );
  53. static const DECLARE_TLV_DB_SCALE(da7219_dac_ng_threshold_tlv, -10200, 600, 0);
  54. static const DECLARE_TLV_DB_SCALE(da7219_hp_gain_tlv, -5700, 100, 0);
  55. /* Input Enums */
  56. static const char * const da7219_alc_attack_rate_txt[] = {
  57. "7.33/fs", "14.66/fs", "29.32/fs", "58.64/fs", "117.3/fs", "234.6/fs",
  58. "469.1/fs", "938.2/fs", "1876/fs", "3753/fs", "7506/fs", "15012/fs",
  59. "30024/fs"
  60. };
  61. static const struct soc_enum da7219_alc_attack_rate =
  62. SOC_ENUM_SINGLE(DA7219_ALC_CTRL2, DA7219_ALC_ATTACK_SHIFT,
  63. DA7219_ALC_ATTACK_MAX, da7219_alc_attack_rate_txt);
  64. static const char * const da7219_alc_release_rate_txt[] = {
  65. "28.66/fs", "57.33/fs", "114.6/fs", "229.3/fs", "458.6/fs", "917.1/fs",
  66. "1834/fs", "3668/fs", "7337/fs", "14674/fs", "29348/fs"
  67. };
  68. static const struct soc_enum da7219_alc_release_rate =
  69. SOC_ENUM_SINGLE(DA7219_ALC_CTRL2, DA7219_ALC_RELEASE_SHIFT,
  70. DA7219_ALC_RELEASE_MAX, da7219_alc_release_rate_txt);
  71. static const char * const da7219_alc_hold_time_txt[] = {
  72. "62/fs", "124/fs", "248/fs", "496/fs", "992/fs", "1984/fs", "3968/fs",
  73. "7936/fs", "15872/fs", "31744/fs", "63488/fs", "126976/fs",
  74. "253952/fs", "507904/fs", "1015808/fs", "2031616/fs"
  75. };
  76. static const struct soc_enum da7219_alc_hold_time =
  77. SOC_ENUM_SINGLE(DA7219_ALC_CTRL3, DA7219_ALC_HOLD_SHIFT,
  78. DA7219_ALC_HOLD_MAX, da7219_alc_hold_time_txt);
  79. static const char * const da7219_alc_env_rate_txt[] = {
  80. "1/4", "1/16", "1/256", "1/65536"
  81. };
  82. static const struct soc_enum da7219_alc_env_attack_rate =
  83. SOC_ENUM_SINGLE(DA7219_ALC_CTRL3, DA7219_ALC_INTEG_ATTACK_SHIFT,
  84. DA7219_ALC_INTEG_MAX, da7219_alc_env_rate_txt);
  85. static const struct soc_enum da7219_alc_env_release_rate =
  86. SOC_ENUM_SINGLE(DA7219_ALC_CTRL3, DA7219_ALC_INTEG_RELEASE_SHIFT,
  87. DA7219_ALC_INTEG_MAX, da7219_alc_env_rate_txt);
  88. static const char * const da7219_alc_anticlip_step_txt[] = {
  89. "0.034dB/fs", "0.068dB/fs", "0.136dB/fs", "0.272dB/fs"
  90. };
  91. static const struct soc_enum da7219_alc_anticlip_step =
  92. SOC_ENUM_SINGLE(DA7219_ALC_ANTICLIP_CTRL,
  93. DA7219_ALC_ANTICLIP_STEP_SHIFT,
  94. DA7219_ALC_ANTICLIP_STEP_MAX,
  95. da7219_alc_anticlip_step_txt);
  96. /* Input/Output Enums */
  97. static const char * const da7219_gain_ramp_rate_txt[] = {
  98. "Nominal Rate * 8", "Nominal Rate", "Nominal Rate / 8",
  99. "Nominal Rate / 16"
  100. };
  101. static const struct soc_enum da7219_gain_ramp_rate =
  102. SOC_ENUM_SINGLE(DA7219_GAIN_RAMP_CTRL, DA7219_GAIN_RAMP_RATE_SHIFT,
  103. DA7219_GAIN_RAMP_RATE_MAX, da7219_gain_ramp_rate_txt);
  104. static const char * const da7219_hpf_mode_txt[] = {
  105. "Disabled", "Audio", "Voice"
  106. };
  107. static const unsigned int da7219_hpf_mode_val[] = {
  108. DA7219_HPF_DISABLED, DA7219_HPF_AUDIO_EN, DA7219_HPF_VOICE_EN,
  109. };
  110. static const struct soc_enum da7219_adc_hpf_mode =
  111. SOC_VALUE_ENUM_SINGLE(DA7219_ADC_FILTERS1, DA7219_HPF_MODE_SHIFT,
  112. DA7219_HPF_MODE_MASK, DA7219_HPF_MODE_MAX,
  113. da7219_hpf_mode_txt, da7219_hpf_mode_val);
  114. static const struct soc_enum da7219_dac_hpf_mode =
  115. SOC_VALUE_ENUM_SINGLE(DA7219_DAC_FILTERS1, DA7219_HPF_MODE_SHIFT,
  116. DA7219_HPF_MODE_MASK, DA7219_HPF_MODE_MAX,
  117. da7219_hpf_mode_txt, da7219_hpf_mode_val);
  118. static const char * const da7219_audio_hpf_corner_txt[] = {
  119. "2Hz", "4Hz", "8Hz", "16Hz"
  120. };
  121. static const struct soc_enum da7219_adc_audio_hpf_corner =
  122. SOC_ENUM_SINGLE(DA7219_ADC_FILTERS1,
  123. DA7219_ADC_AUDIO_HPF_CORNER_SHIFT,
  124. DA7219_AUDIO_HPF_CORNER_MAX,
  125. da7219_audio_hpf_corner_txt);
  126. static const struct soc_enum da7219_dac_audio_hpf_corner =
  127. SOC_ENUM_SINGLE(DA7219_DAC_FILTERS1,
  128. DA7219_DAC_AUDIO_HPF_CORNER_SHIFT,
  129. DA7219_AUDIO_HPF_CORNER_MAX,
  130. da7219_audio_hpf_corner_txt);
  131. static const char * const da7219_voice_hpf_corner_txt[] = {
  132. "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
  133. };
  134. static const struct soc_enum da7219_adc_voice_hpf_corner =
  135. SOC_ENUM_SINGLE(DA7219_ADC_FILTERS1,
  136. DA7219_ADC_VOICE_HPF_CORNER_SHIFT,
  137. DA7219_VOICE_HPF_CORNER_MAX,
  138. da7219_voice_hpf_corner_txt);
  139. static const struct soc_enum da7219_dac_voice_hpf_corner =
  140. SOC_ENUM_SINGLE(DA7219_DAC_FILTERS1,
  141. DA7219_DAC_VOICE_HPF_CORNER_SHIFT,
  142. DA7219_VOICE_HPF_CORNER_MAX,
  143. da7219_voice_hpf_corner_txt);
  144. static const char * const da7219_tonegen_dtmf_key_txt[] = {
  145. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
  146. "*", "#"
  147. };
  148. static const struct soc_enum da7219_tonegen_dtmf_key =
  149. SOC_ENUM_SINGLE(DA7219_TONE_GEN_CFG1, DA7219_DTMF_REG_SHIFT,
  150. DA7219_DTMF_REG_MAX, da7219_tonegen_dtmf_key_txt);
  151. static const char * const da7219_tonegen_swg_sel_txt[] = {
  152. "Sum", "SWG1", "SWG2", "SWG1_1-Cos"
  153. };
  154. static const struct soc_enum da7219_tonegen_swg_sel =
  155. SOC_ENUM_SINGLE(DA7219_TONE_GEN_CFG2, DA7219_SWG_SEL_SHIFT,
  156. DA7219_SWG_SEL_MAX, da7219_tonegen_swg_sel_txt);
  157. /* Output Enums */
  158. static const char * const da7219_dac_softmute_rate_txt[] = {
  159. "1 Sample", "2 Samples", "4 Samples", "8 Samples", "16 Samples",
  160. "32 Samples", "64 Samples"
  161. };
  162. static const struct soc_enum da7219_dac_softmute_rate =
  163. SOC_ENUM_SINGLE(DA7219_DAC_FILTERS5, DA7219_DAC_SOFTMUTE_RATE_SHIFT,
  164. DA7219_DAC_SOFTMUTE_RATE_MAX,
  165. da7219_dac_softmute_rate_txt);
  166. static const char * const da7219_dac_ng_setup_time_txt[] = {
  167. "256 Samples", "512 Samples", "1024 Samples", "2048 Samples"
  168. };
  169. static const struct soc_enum da7219_dac_ng_setup_time =
  170. SOC_ENUM_SINGLE(DA7219_DAC_NG_SETUP_TIME,
  171. DA7219_DAC_NG_SETUP_TIME_SHIFT,
  172. DA7219_DAC_NG_SETUP_TIME_MAX,
  173. da7219_dac_ng_setup_time_txt);
  174. static const char * const da7219_dac_ng_rampup_txt[] = {
  175. "0.22ms/dB", "0.0138ms/dB"
  176. };
  177. static const struct soc_enum da7219_dac_ng_rampup_rate =
  178. SOC_ENUM_SINGLE(DA7219_DAC_NG_SETUP_TIME,
  179. DA7219_DAC_NG_RAMPUP_RATE_SHIFT,
  180. DA7219_DAC_NG_RAMP_RATE_MAX,
  181. da7219_dac_ng_rampup_txt);
  182. static const char * const da7219_dac_ng_rampdown_txt[] = {
  183. "0.88ms/dB", "14.08ms/dB"
  184. };
  185. static const struct soc_enum da7219_dac_ng_rampdown_rate =
  186. SOC_ENUM_SINGLE(DA7219_DAC_NG_SETUP_TIME,
  187. DA7219_DAC_NG_RAMPDN_RATE_SHIFT,
  188. DA7219_DAC_NG_RAMP_RATE_MAX,
  189. da7219_dac_ng_rampdown_txt);
  190. static const char * const da7219_cp_track_mode_txt[] = {
  191. "Largest Volume", "DAC Volume", "Signal Magnitude"
  192. };
  193. static const unsigned int da7219_cp_track_mode_val[] = {
  194. DA7219_CP_MCHANGE_LARGEST_VOL, DA7219_CP_MCHANGE_DAC_VOL,
  195. DA7219_CP_MCHANGE_SIG_MAG
  196. };
  197. static const struct soc_enum da7219_cp_track_mode =
  198. SOC_VALUE_ENUM_SINGLE(DA7219_CP_CTRL, DA7219_CP_MCHANGE_SHIFT,
  199. DA7219_CP_MCHANGE_REL_MASK, DA7219_CP_MCHANGE_MAX,
  200. da7219_cp_track_mode_txt,
  201. da7219_cp_track_mode_val);
  202. /*
  203. * Control Functions
  204. */
  205. /* Locked Kcontrol calls */
  206. static int da7219_volsw_locked_get(struct snd_kcontrol *kcontrol,
  207. struct snd_ctl_elem_value *ucontrol)
  208. {
  209. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  210. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  211. int ret;
  212. mutex_lock(&da7219->lock);
  213. ret = snd_soc_get_volsw(kcontrol, ucontrol);
  214. mutex_unlock(&da7219->lock);
  215. return ret;
  216. }
  217. static int da7219_volsw_locked_put(struct snd_kcontrol *kcontrol,
  218. struct snd_ctl_elem_value *ucontrol)
  219. {
  220. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  221. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  222. int ret;
  223. mutex_lock(&da7219->lock);
  224. ret = snd_soc_put_volsw(kcontrol, ucontrol);
  225. mutex_unlock(&da7219->lock);
  226. return ret;
  227. }
  228. static int da7219_enum_locked_get(struct snd_kcontrol *kcontrol,
  229. struct snd_ctl_elem_value *ucontrol)
  230. {
  231. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  232. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  233. int ret;
  234. mutex_lock(&da7219->lock);
  235. ret = snd_soc_get_enum_double(kcontrol, ucontrol);
  236. mutex_unlock(&da7219->lock);
  237. return ret;
  238. }
  239. static int da7219_enum_locked_put(struct snd_kcontrol *kcontrol,
  240. struct snd_ctl_elem_value *ucontrol)
  241. {
  242. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  243. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  244. int ret;
  245. mutex_lock(&da7219->lock);
  246. ret = snd_soc_put_enum_double(kcontrol, ucontrol);
  247. mutex_unlock(&da7219->lock);
  248. return ret;
  249. }
  250. /* ALC */
  251. static void da7219_alc_calib(struct snd_soc_codec *codec)
  252. {
  253. u8 mic_ctrl, mixin_ctrl, adc_ctrl, calib_ctrl;
  254. /* Save current state of mic control register */
  255. mic_ctrl = snd_soc_read(codec, DA7219_MIC_1_CTRL);
  256. /* Save current state of input mixer control register */
  257. mixin_ctrl = snd_soc_read(codec, DA7219_MIXIN_L_CTRL);
  258. /* Save current state of input ADC control register */
  259. adc_ctrl = snd_soc_read(codec, DA7219_ADC_L_CTRL);
  260. /* Enable then Mute MIC PGAs */
  261. snd_soc_update_bits(codec, DA7219_MIC_1_CTRL, DA7219_MIC_1_AMP_EN_MASK,
  262. DA7219_MIC_1_AMP_EN_MASK);
  263. snd_soc_update_bits(codec, DA7219_MIC_1_CTRL,
  264. DA7219_MIC_1_AMP_MUTE_EN_MASK,
  265. DA7219_MIC_1_AMP_MUTE_EN_MASK);
  266. /* Enable input mixers unmuted */
  267. snd_soc_update_bits(codec, DA7219_MIXIN_L_CTRL,
  268. DA7219_MIXIN_L_AMP_EN_MASK |
  269. DA7219_MIXIN_L_AMP_MUTE_EN_MASK,
  270. DA7219_MIXIN_L_AMP_EN_MASK);
  271. /* Enable input filters unmuted */
  272. snd_soc_update_bits(codec, DA7219_ADC_L_CTRL,
  273. DA7219_ADC_L_MUTE_EN_MASK | DA7219_ADC_L_EN_MASK,
  274. DA7219_ADC_L_EN_MASK);
  275. /* Perform auto calibration */
  276. snd_soc_update_bits(codec, DA7219_ALC_CTRL1,
  277. DA7219_ALC_AUTO_CALIB_EN_MASK,
  278. DA7219_ALC_AUTO_CALIB_EN_MASK);
  279. do {
  280. calib_ctrl = snd_soc_read(codec, DA7219_ALC_CTRL1);
  281. } while (calib_ctrl & DA7219_ALC_AUTO_CALIB_EN_MASK);
  282. /* If auto calibration fails, disable DC offset, hybrid ALC */
  283. if (calib_ctrl & DA7219_ALC_CALIB_OVERFLOW_MASK) {
  284. dev_warn(codec->dev,
  285. "ALC auto calibration failed with overflow\n");
  286. snd_soc_update_bits(codec, DA7219_ALC_CTRL1,
  287. DA7219_ALC_OFFSET_EN_MASK |
  288. DA7219_ALC_SYNC_MODE_MASK, 0);
  289. } else {
  290. /* Enable DC offset cancellation, hybrid mode */
  291. snd_soc_update_bits(codec, DA7219_ALC_CTRL1,
  292. DA7219_ALC_OFFSET_EN_MASK |
  293. DA7219_ALC_SYNC_MODE_MASK,
  294. DA7219_ALC_OFFSET_EN_MASK |
  295. DA7219_ALC_SYNC_MODE_MASK);
  296. }
  297. /* Restore input filter control register to original state */
  298. snd_soc_write(codec, DA7219_ADC_L_CTRL, adc_ctrl);
  299. /* Restore input mixer control registers to original state */
  300. snd_soc_write(codec, DA7219_MIXIN_L_CTRL, mixin_ctrl);
  301. /* Restore MIC control registers to original states */
  302. snd_soc_write(codec, DA7219_MIC_1_CTRL, mic_ctrl);
  303. }
  304. static int da7219_mixin_gain_put(struct snd_kcontrol *kcontrol,
  305. struct snd_ctl_elem_value *ucontrol)
  306. {
  307. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  308. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  309. int ret;
  310. ret = snd_soc_put_volsw(kcontrol, ucontrol);
  311. /*
  312. * If ALC in operation and value of control has been updated,
  313. * make sure calibrated offsets are updated.
  314. */
  315. if ((ret == 1) && (da7219->alc_en))
  316. da7219_alc_calib(codec);
  317. return ret;
  318. }
  319. static int da7219_alc_sw_put(struct snd_kcontrol *kcontrol,
  320. struct snd_ctl_elem_value *ucontrol)
  321. {
  322. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  323. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  324. /* Force ALC offset calibration if enabling ALC */
  325. if ((ucontrol->value.integer.value[0]) && (!da7219->alc_en)) {
  326. da7219_alc_calib(codec);
  327. da7219->alc_en = true;
  328. } else {
  329. da7219->alc_en = false;
  330. }
  331. return snd_soc_put_volsw(kcontrol, ucontrol);
  332. }
  333. /* ToneGen */
  334. static int da7219_tonegen_freq_get(struct snd_kcontrol *kcontrol,
  335. struct snd_ctl_elem_value *ucontrol)
  336. {
  337. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  338. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  339. struct soc_mixer_control *mixer_ctrl =
  340. (struct soc_mixer_control *) kcontrol->private_value;
  341. unsigned int reg = mixer_ctrl->reg;
  342. u16 val;
  343. int ret;
  344. mutex_lock(&da7219->lock);
  345. ret = regmap_raw_read(da7219->regmap, reg, &val, sizeof(val));
  346. mutex_unlock(&da7219->lock);
  347. if (ret)
  348. return ret;
  349. /*
  350. * Frequency value spans two 8-bit registers, lower then upper byte.
  351. * Therefore we need to convert to host endianness here.
  352. */
  353. ucontrol->value.integer.value[0] = le16_to_cpu(val);
  354. return 0;
  355. }
  356. static int da7219_tonegen_freq_put(struct snd_kcontrol *kcontrol,
  357. struct snd_ctl_elem_value *ucontrol)
  358. {
  359. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  360. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  361. struct soc_mixer_control *mixer_ctrl =
  362. (struct soc_mixer_control *) kcontrol->private_value;
  363. unsigned int reg = mixer_ctrl->reg;
  364. u16 val;
  365. int ret;
  366. /*
  367. * Frequency value spans two 8-bit registers, lower then upper byte.
  368. * Therefore we need to convert to little endian here to align with
  369. * HW registers.
  370. */
  371. val = cpu_to_le16(ucontrol->value.integer.value[0]);
  372. mutex_lock(&da7219->lock);
  373. ret = regmap_raw_write(da7219->regmap, reg, &val, sizeof(val));
  374. mutex_unlock(&da7219->lock);
  375. return ret;
  376. }
  377. /*
  378. * KControls
  379. */
  380. static const struct snd_kcontrol_new da7219_snd_controls[] = {
  381. /* Mics */
  382. SOC_SINGLE_TLV("Mic Volume", DA7219_MIC_1_GAIN,
  383. DA7219_MIC_1_AMP_GAIN_SHIFT, DA7219_MIC_1_AMP_GAIN_MAX,
  384. DA7219_NO_INVERT, da7219_mic_gain_tlv),
  385. SOC_SINGLE("Mic Switch", DA7219_MIC_1_CTRL,
  386. DA7219_MIC_1_AMP_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  387. DA7219_INVERT),
  388. /* Mixer Input */
  389. SOC_SINGLE_EXT_TLV("Mixin Volume", DA7219_MIXIN_L_GAIN,
  390. DA7219_MIXIN_L_AMP_GAIN_SHIFT,
  391. DA7219_MIXIN_L_AMP_GAIN_MAX, DA7219_NO_INVERT,
  392. snd_soc_get_volsw, da7219_mixin_gain_put,
  393. da7219_mixin_gain_tlv),
  394. SOC_SINGLE("Mixin Switch", DA7219_MIXIN_L_CTRL,
  395. DA7219_MIXIN_L_AMP_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  396. DA7219_INVERT),
  397. SOC_SINGLE("Mixin Gain Ramp Switch", DA7219_MIXIN_L_CTRL,
  398. DA7219_MIXIN_L_AMP_RAMP_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  399. DA7219_NO_INVERT),
  400. SOC_SINGLE("Mixin ZC Gain Switch", DA7219_MIXIN_L_CTRL,
  401. DA7219_MIXIN_L_AMP_ZC_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  402. DA7219_NO_INVERT),
  403. /* ADC */
  404. SOC_SINGLE_TLV("Capture Digital Volume", DA7219_ADC_L_GAIN,
  405. DA7219_ADC_L_DIGITAL_GAIN_SHIFT,
  406. DA7219_ADC_L_DIGITAL_GAIN_MAX, DA7219_NO_INVERT,
  407. da7219_adc_dig_gain_tlv),
  408. SOC_SINGLE("Capture Digital Switch", DA7219_ADC_L_CTRL,
  409. DA7219_ADC_L_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  410. DA7219_INVERT),
  411. SOC_SINGLE("Capture Digital Gain Ramp Switch", DA7219_ADC_L_CTRL,
  412. DA7219_ADC_L_RAMP_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  413. DA7219_NO_INVERT),
  414. /* ALC */
  415. SOC_ENUM("ALC Attack Rate", da7219_alc_attack_rate),
  416. SOC_ENUM("ALC Release Rate", da7219_alc_release_rate),
  417. SOC_ENUM("ALC Hold Time", da7219_alc_hold_time),
  418. SOC_ENUM("ALC Envelope Attack Rate", da7219_alc_env_attack_rate),
  419. SOC_ENUM("ALC Envelope Release Rate", da7219_alc_env_release_rate),
  420. SOC_SINGLE_TLV("ALC Noise Threshold", DA7219_ALC_NOISE,
  421. DA7219_ALC_NOISE_SHIFT, DA7219_ALC_THRESHOLD_MAX,
  422. DA7219_INVERT, da7219_alc_threshold_tlv),
  423. SOC_SINGLE_TLV("ALC Min Threshold", DA7219_ALC_TARGET_MIN,
  424. DA7219_ALC_THRESHOLD_MIN_SHIFT, DA7219_ALC_THRESHOLD_MAX,
  425. DA7219_INVERT, da7219_alc_threshold_tlv),
  426. SOC_SINGLE_TLV("ALC Max Threshold", DA7219_ALC_TARGET_MAX,
  427. DA7219_ALC_THRESHOLD_MAX_SHIFT, DA7219_ALC_THRESHOLD_MAX,
  428. DA7219_INVERT, da7219_alc_threshold_tlv),
  429. SOC_SINGLE_TLV("ALC Max Attenuation", DA7219_ALC_GAIN_LIMITS,
  430. DA7219_ALC_ATTEN_MAX_SHIFT, DA7219_ALC_ATTEN_GAIN_MAX,
  431. DA7219_NO_INVERT, da7219_alc_gain_tlv),
  432. SOC_SINGLE_TLV("ALC Max Volume", DA7219_ALC_GAIN_LIMITS,
  433. DA7219_ALC_GAIN_MAX_SHIFT, DA7219_ALC_ATTEN_GAIN_MAX,
  434. DA7219_NO_INVERT, da7219_alc_gain_tlv),
  435. SOC_SINGLE_RANGE_TLV("ALC Min Analog Volume", DA7219_ALC_ANA_GAIN_LIMITS,
  436. DA7219_ALC_ANA_GAIN_MIN_SHIFT,
  437. DA7219_ALC_ANA_GAIN_MIN, DA7219_ALC_ANA_GAIN_MAX,
  438. DA7219_NO_INVERT, da7219_alc_ana_gain_tlv),
  439. SOC_SINGLE_RANGE_TLV("ALC Max Analog Volume", DA7219_ALC_ANA_GAIN_LIMITS,
  440. DA7219_ALC_ANA_GAIN_MAX_SHIFT,
  441. DA7219_ALC_ANA_GAIN_MIN, DA7219_ALC_ANA_GAIN_MAX,
  442. DA7219_NO_INVERT, da7219_alc_ana_gain_tlv),
  443. SOC_ENUM("ALC Anticlip Step", da7219_alc_anticlip_step),
  444. SOC_SINGLE("ALC Anticlip Switch", DA7219_ALC_ANTICLIP_CTRL,
  445. DA7219_ALC_ANTIPCLIP_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  446. DA7219_NO_INVERT),
  447. SOC_SINGLE_EXT("ALC Switch", DA7219_ALC_CTRL1, DA7219_ALC_EN_SHIFT,
  448. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT,
  449. snd_soc_get_volsw, da7219_alc_sw_put),
  450. /* Input High-Pass Filters */
  451. SOC_ENUM("ADC HPF Mode", da7219_adc_hpf_mode),
  452. SOC_ENUM("ADC HPF Corner Audio", da7219_adc_audio_hpf_corner),
  453. SOC_ENUM("ADC HPF Corner Voice", da7219_adc_voice_hpf_corner),
  454. /* Sidetone Filter */
  455. SOC_SINGLE_TLV("Sidetone Volume", DA7219_SIDETONE_GAIN,
  456. DA7219_SIDETONE_GAIN_SHIFT, DA7219_SIDETONE_GAIN_MAX,
  457. DA7219_NO_INVERT, da7219_sidetone_gain_tlv),
  458. SOC_SINGLE("Sidetone Switch", DA7219_SIDETONE_CTRL,
  459. DA7219_SIDETONE_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  460. DA7219_INVERT),
  461. /* Tone Generator */
  462. SOC_SINGLE_EXT_TLV("ToneGen Volume", DA7219_TONE_GEN_CFG2,
  463. DA7219_TONE_GEN_GAIN_SHIFT, DA7219_TONE_GEN_GAIN_MAX,
  464. DA7219_NO_INVERT, da7219_volsw_locked_get,
  465. da7219_volsw_locked_put, da7219_tonegen_gain_tlv),
  466. SOC_ENUM_EXT("ToneGen DTMF Key", da7219_tonegen_dtmf_key,
  467. da7219_enum_locked_get, da7219_enum_locked_put),
  468. SOC_SINGLE_EXT("ToneGen DTMF Switch", DA7219_TONE_GEN_CFG1,
  469. DA7219_DTMF_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  470. DA7219_NO_INVERT, da7219_volsw_locked_get,
  471. da7219_volsw_locked_put),
  472. SOC_ENUM_EXT("ToneGen Sinewave Gen Type", da7219_tonegen_swg_sel,
  473. da7219_enum_locked_get, da7219_enum_locked_put),
  474. SOC_SINGLE_EXT("ToneGen Sinewave1 Freq", DA7219_TONE_GEN_FREQ1_L,
  475. DA7219_FREQ1_L_SHIFT, DA7219_FREQ_MAX, DA7219_NO_INVERT,
  476. da7219_tonegen_freq_get, da7219_tonegen_freq_put),
  477. SOC_SINGLE_EXT("ToneGen Sinewave2 Freq", DA7219_TONE_GEN_FREQ2_L,
  478. DA7219_FREQ2_L_SHIFT, DA7219_FREQ_MAX, DA7219_NO_INVERT,
  479. da7219_tonegen_freq_get, da7219_tonegen_freq_put),
  480. SOC_SINGLE_EXT("ToneGen On Time", DA7219_TONE_GEN_ON_PER,
  481. DA7219_BEEP_ON_PER_SHIFT, DA7219_BEEP_ON_OFF_MAX,
  482. DA7219_NO_INVERT, da7219_volsw_locked_get,
  483. da7219_volsw_locked_put),
  484. SOC_SINGLE("ToneGen Off Time", DA7219_TONE_GEN_OFF_PER,
  485. DA7219_BEEP_OFF_PER_SHIFT, DA7219_BEEP_ON_OFF_MAX,
  486. DA7219_NO_INVERT),
  487. /* Gain ramping */
  488. SOC_ENUM("Gain Ramp Rate", da7219_gain_ramp_rate),
  489. /* DAC High-Pass Filter */
  490. SOC_ENUM_EXT("DAC HPF Mode", da7219_dac_hpf_mode,
  491. da7219_enum_locked_get, da7219_enum_locked_put),
  492. SOC_ENUM("DAC HPF Corner Audio", da7219_dac_audio_hpf_corner),
  493. SOC_ENUM("DAC HPF Corner Voice", da7219_dac_voice_hpf_corner),
  494. /* DAC 5-Band Equaliser */
  495. SOC_SINGLE_TLV("DAC EQ Band1 Volume", DA7219_DAC_FILTERS2,
  496. DA7219_DAC_EQ_BAND1_SHIFT, DA7219_DAC_EQ_BAND_MAX,
  497. DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
  498. SOC_SINGLE_TLV("DAC EQ Band2 Volume", DA7219_DAC_FILTERS2,
  499. DA7219_DAC_EQ_BAND2_SHIFT, DA7219_DAC_EQ_BAND_MAX,
  500. DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
  501. SOC_SINGLE_TLV("DAC EQ Band3 Volume", DA7219_DAC_FILTERS3,
  502. DA7219_DAC_EQ_BAND3_SHIFT, DA7219_DAC_EQ_BAND_MAX,
  503. DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
  504. SOC_SINGLE_TLV("DAC EQ Band4 Volume", DA7219_DAC_FILTERS3,
  505. DA7219_DAC_EQ_BAND4_SHIFT, DA7219_DAC_EQ_BAND_MAX,
  506. DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
  507. SOC_SINGLE_TLV("DAC EQ Band5 Volume", DA7219_DAC_FILTERS4,
  508. DA7219_DAC_EQ_BAND5_SHIFT, DA7219_DAC_EQ_BAND_MAX,
  509. DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
  510. SOC_SINGLE_EXT("DAC EQ Switch", DA7219_DAC_FILTERS4,
  511. DA7219_DAC_EQ_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  512. DA7219_NO_INVERT, da7219_volsw_locked_get,
  513. da7219_volsw_locked_put),
  514. /* DAC Softmute */
  515. SOC_ENUM("DAC Soft Mute Rate", da7219_dac_softmute_rate),
  516. SOC_SINGLE_EXT("DAC Soft Mute Switch", DA7219_DAC_FILTERS5,
  517. DA7219_DAC_SOFTMUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  518. DA7219_NO_INVERT, da7219_volsw_locked_get,
  519. da7219_volsw_locked_put),
  520. /* DAC Noise Gate */
  521. SOC_ENUM("DAC NG Setup Time", da7219_dac_ng_setup_time),
  522. SOC_ENUM("DAC NG Rampup Rate", da7219_dac_ng_rampup_rate),
  523. SOC_ENUM("DAC NG Rampdown Rate", da7219_dac_ng_rampdown_rate),
  524. SOC_SINGLE_TLV("DAC NG Off Threshold", DA7219_DAC_NG_OFF_THRESH,
  525. DA7219_DAC_NG_OFF_THRESHOLD_SHIFT,
  526. DA7219_DAC_NG_THRESHOLD_MAX, DA7219_NO_INVERT,
  527. da7219_dac_ng_threshold_tlv),
  528. SOC_SINGLE_TLV("DAC NG On Threshold", DA7219_DAC_NG_ON_THRESH,
  529. DA7219_DAC_NG_ON_THRESHOLD_SHIFT,
  530. DA7219_DAC_NG_THRESHOLD_MAX, DA7219_NO_INVERT,
  531. da7219_dac_ng_threshold_tlv),
  532. SOC_SINGLE("DAC NG Switch", DA7219_DAC_NG_CTRL, DA7219_DAC_NG_EN_SHIFT,
  533. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
  534. /* DACs */
  535. SOC_DOUBLE_R_EXT_TLV("Playback Digital Volume", DA7219_DAC_L_GAIN,
  536. DA7219_DAC_R_GAIN, DA7219_DAC_L_DIGITAL_GAIN_SHIFT,
  537. DA7219_DAC_DIGITAL_GAIN_MAX, DA7219_NO_INVERT,
  538. da7219_volsw_locked_get, da7219_volsw_locked_put,
  539. da7219_dac_dig_gain_tlv),
  540. SOC_DOUBLE_R_EXT("Playback Digital Switch", DA7219_DAC_L_CTRL,
  541. DA7219_DAC_R_CTRL, DA7219_DAC_L_MUTE_EN_SHIFT,
  542. DA7219_SWITCH_EN_MAX, DA7219_INVERT,
  543. da7219_volsw_locked_get, da7219_volsw_locked_put),
  544. SOC_DOUBLE_R("Playback Digital Gain Ramp Switch", DA7219_DAC_L_CTRL,
  545. DA7219_DAC_R_CTRL, DA7219_DAC_L_RAMP_EN_SHIFT,
  546. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
  547. /* CP */
  548. SOC_ENUM("Charge Pump Track Mode", da7219_cp_track_mode),
  549. SOC_SINGLE("Charge Pump Threshold", DA7219_CP_VOL_THRESHOLD1,
  550. DA7219_CP_THRESH_VDD2_SHIFT, DA7219_CP_THRESH_VDD2_MAX,
  551. DA7219_NO_INVERT),
  552. /* Headphones */
  553. SOC_DOUBLE_R_EXT_TLV("Headphone Volume", DA7219_HP_L_GAIN,
  554. DA7219_HP_R_GAIN, DA7219_HP_L_AMP_GAIN_SHIFT,
  555. DA7219_HP_AMP_GAIN_MAX, DA7219_NO_INVERT,
  556. da7219_volsw_locked_get, da7219_volsw_locked_put,
  557. da7219_hp_gain_tlv),
  558. SOC_DOUBLE_R_EXT("Headphone Switch", DA7219_HP_L_CTRL, DA7219_HP_R_CTRL,
  559. DA7219_HP_L_AMP_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
  560. DA7219_INVERT, da7219_volsw_locked_get,
  561. da7219_volsw_locked_put),
  562. SOC_DOUBLE_R("Headphone Gain Ramp Switch", DA7219_HP_L_CTRL,
  563. DA7219_HP_R_CTRL, DA7219_HP_L_AMP_RAMP_EN_SHIFT,
  564. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
  565. SOC_DOUBLE_R("Headphone ZC Gain Switch", DA7219_HP_L_CTRL,
  566. DA7219_HP_R_CTRL, DA7219_HP_L_AMP_ZC_EN_SHIFT,
  567. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
  568. };
  569. /*
  570. * DAPM Mux Controls
  571. */
  572. static const char * const da7219_out_sel_txt[] = {
  573. "ADC", "Tone Generator", "DAIL", "DAIR"
  574. };
  575. static const struct soc_enum da7219_out_dail_sel =
  576. SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAI,
  577. DA7219_DAI_L_SRC_SHIFT,
  578. DA7219_OUT_SRC_MAX,
  579. da7219_out_sel_txt);
  580. static const struct snd_kcontrol_new da7219_out_dail_sel_mux =
  581. SOC_DAPM_ENUM("Out DAIL Mux", da7219_out_dail_sel);
  582. static const struct soc_enum da7219_out_dair_sel =
  583. SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAI,
  584. DA7219_DAI_R_SRC_SHIFT,
  585. DA7219_OUT_SRC_MAX,
  586. da7219_out_sel_txt);
  587. static const struct snd_kcontrol_new da7219_out_dair_sel_mux =
  588. SOC_DAPM_ENUM("Out DAIR Mux", da7219_out_dair_sel);
  589. static const struct soc_enum da7219_out_dacl_sel =
  590. SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAC,
  591. DA7219_DAC_L_SRC_SHIFT,
  592. DA7219_OUT_SRC_MAX,
  593. da7219_out_sel_txt);
  594. static const struct snd_kcontrol_new da7219_out_dacl_sel_mux =
  595. SOC_DAPM_ENUM("Out DACL Mux", da7219_out_dacl_sel);
  596. static const struct soc_enum da7219_out_dacr_sel =
  597. SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAC,
  598. DA7219_DAC_R_SRC_SHIFT,
  599. DA7219_OUT_SRC_MAX,
  600. da7219_out_sel_txt);
  601. static const struct snd_kcontrol_new da7219_out_dacr_sel_mux =
  602. SOC_DAPM_ENUM("Out DACR Mux", da7219_out_dacr_sel);
  603. /*
  604. * DAPM Mixer Controls
  605. */
  606. static const struct snd_kcontrol_new da7219_mixin_controls[] = {
  607. SOC_DAPM_SINGLE("Mic Switch", DA7219_MIXIN_L_SELECT,
  608. DA7219_MIXIN_L_MIX_SELECT_SHIFT,
  609. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
  610. };
  611. static const struct snd_kcontrol_new da7219_mixout_l_controls[] = {
  612. SOC_DAPM_SINGLE("DACL Switch", DA7219_MIXOUT_L_SELECT,
  613. DA7219_MIXOUT_L_MIX_SELECT_SHIFT,
  614. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
  615. };
  616. static const struct snd_kcontrol_new da7219_mixout_r_controls[] = {
  617. SOC_DAPM_SINGLE("DACR Switch", DA7219_MIXOUT_R_SELECT,
  618. DA7219_MIXOUT_R_MIX_SELECT_SHIFT,
  619. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
  620. };
  621. #define DA7219_DMIX_ST_CTRLS(reg) \
  622. SOC_DAPM_SINGLE("Out FilterL Switch", reg, \
  623. DA7219_DMIX_ST_SRC_OUTFILT1L_SHIFT, \
  624. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), \
  625. SOC_DAPM_SINGLE("Out FilterR Switch", reg, \
  626. DA7219_DMIX_ST_SRC_OUTFILT1R_SHIFT, \
  627. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), \
  628. SOC_DAPM_SINGLE("Sidetone Switch", reg, \
  629. DA7219_DMIX_ST_SRC_SIDETONE_SHIFT, \
  630. DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT) \
  631. static const struct snd_kcontrol_new da7219_st_out_filtl_mix_controls[] = {
  632. DA7219_DMIX_ST_CTRLS(DA7219_DROUTING_ST_OUTFILT_1L),
  633. };
  634. static const struct snd_kcontrol_new da7219_st_out_filtr_mix_controls[] = {
  635. DA7219_DMIX_ST_CTRLS(DA7219_DROUTING_ST_OUTFILT_1R),
  636. };
  637. /*
  638. * DAPM Events
  639. */
  640. static int da7219_dai_event(struct snd_soc_dapm_widget *w,
  641. struct snd_kcontrol *kcontrol, int event)
  642. {
  643. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  644. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  645. u8 pll_ctrl, pll_status;
  646. int i = 0;
  647. bool srm_lock = false;
  648. switch (event) {
  649. case SND_SOC_DAPM_PRE_PMU:
  650. if (da7219->master)
  651. /* Enable DAI clks for master mode */
  652. snd_soc_update_bits(codec, DA7219_DAI_CLK_MODE,
  653. DA7219_DAI_CLK_EN_MASK,
  654. DA7219_DAI_CLK_EN_MASK);
  655. /* PC synchronised to DAI */
  656. snd_soc_update_bits(codec, DA7219_PC_COUNT,
  657. DA7219_PC_FREERUN_MASK, 0);
  658. /* Slave mode, if SRM not enabled no need for status checks */
  659. pll_ctrl = snd_soc_read(codec, DA7219_PLL_CTRL);
  660. if ((pll_ctrl & DA7219_PLL_MODE_MASK) != DA7219_PLL_MODE_SRM)
  661. return 0;
  662. /* Check SRM has locked */
  663. do {
  664. pll_status = snd_soc_read(codec, DA7219_PLL_SRM_STS);
  665. if (pll_status & DA7219_PLL_SRM_STS_SRM_LOCK) {
  666. srm_lock = true;
  667. } else {
  668. ++i;
  669. msleep(50);
  670. }
  671. } while ((i < DA7219_SRM_CHECK_RETRIES) && (!srm_lock));
  672. if (!srm_lock)
  673. dev_warn(codec->dev, "SRM failed to lock\n");
  674. return 0;
  675. case SND_SOC_DAPM_POST_PMD:
  676. /* PC free-running */
  677. snd_soc_update_bits(codec, DA7219_PC_COUNT,
  678. DA7219_PC_FREERUN_MASK,
  679. DA7219_PC_FREERUN_MASK);
  680. /* Disable DAI clks if in master mode */
  681. if (da7219->master)
  682. snd_soc_update_bits(codec, DA7219_DAI_CLK_MODE,
  683. DA7219_DAI_CLK_EN_MASK, 0);
  684. return 0;
  685. default:
  686. return -EINVAL;
  687. }
  688. }
  689. /*
  690. * DAPM Widgets
  691. */
  692. static const struct snd_soc_dapm_widget da7219_dapm_widgets[] = {
  693. /* Input Supplies */
  694. SND_SOC_DAPM_SUPPLY("Mic Bias", DA7219_MICBIAS_CTRL,
  695. DA7219_MICBIAS1_EN_SHIFT, DA7219_NO_INVERT,
  696. NULL, 0),
  697. /* Inputs */
  698. SND_SOC_DAPM_INPUT("MIC"),
  699. /* Input PGAs */
  700. SND_SOC_DAPM_PGA("Mic PGA", DA7219_MIC_1_CTRL,
  701. DA7219_MIC_1_AMP_EN_SHIFT, DA7219_NO_INVERT,
  702. NULL, 0),
  703. SND_SOC_DAPM_PGA("Mixin PGA", DA7219_MIXIN_L_CTRL,
  704. DA7219_MIXIN_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
  705. NULL, 0),
  706. /* Input Filters */
  707. SND_SOC_DAPM_ADC("ADC", NULL, DA7219_ADC_L_CTRL, DA7219_ADC_L_EN_SHIFT,
  708. DA7219_NO_INVERT),
  709. /* Tone Generator */
  710. SND_SOC_DAPM_SIGGEN("TONE"),
  711. SND_SOC_DAPM_PGA("Tone Generator", DA7219_TONE_GEN_CFG1,
  712. DA7219_START_STOPN_SHIFT, DA7219_NO_INVERT, NULL, 0),
  713. /* Sidetone Input */
  714. SND_SOC_DAPM_ADC("Sidetone Filter", NULL, DA7219_SIDETONE_CTRL,
  715. DA7219_SIDETONE_EN_SHIFT, DA7219_NO_INVERT),
  716. /* Input Mixer Supply */
  717. SND_SOC_DAPM_SUPPLY("Mixer In Supply", DA7219_MIXIN_L_CTRL,
  718. DA7219_MIXIN_L_MIX_EN_SHIFT, DA7219_NO_INVERT,
  719. NULL, 0),
  720. /* Input Mixer */
  721. SND_SOC_DAPM_MIXER("Mixer In", SND_SOC_NOPM, 0, 0,
  722. da7219_mixin_controls,
  723. ARRAY_SIZE(da7219_mixin_controls)),
  724. /* Input Muxes */
  725. SND_SOC_DAPM_MUX("Out DAIL Mux", SND_SOC_NOPM, 0, 0,
  726. &da7219_out_dail_sel_mux),
  727. SND_SOC_DAPM_MUX("Out DAIR Mux", SND_SOC_NOPM, 0, 0,
  728. &da7219_out_dair_sel_mux),
  729. /* DAI Supply */
  730. SND_SOC_DAPM_SUPPLY("DAI", DA7219_DAI_CTRL, DA7219_DAI_EN_SHIFT,
  731. DA7219_NO_INVERT, da7219_dai_event,
  732. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  733. /* DAI */
  734. SND_SOC_DAPM_AIF_OUT("DAIOUT", "Capture", 0, DA7219_DAI_TDM_CTRL,
  735. DA7219_DAI_OE_SHIFT, DA7219_NO_INVERT),
  736. SND_SOC_DAPM_AIF_IN("DAIIN", "Playback", 0, SND_SOC_NOPM, 0, 0),
  737. /* Output Muxes */
  738. SND_SOC_DAPM_MUX("Out DACL Mux", SND_SOC_NOPM, 0, 0,
  739. &da7219_out_dacl_sel_mux),
  740. SND_SOC_DAPM_MUX("Out DACR Mux", SND_SOC_NOPM, 0, 0,
  741. &da7219_out_dacr_sel_mux),
  742. /* Output Mixers */
  743. SND_SOC_DAPM_MIXER("Mixer Out FilterL", SND_SOC_NOPM, 0, 0,
  744. da7219_mixout_l_controls,
  745. ARRAY_SIZE(da7219_mixout_l_controls)),
  746. SND_SOC_DAPM_MIXER("Mixer Out FilterR", SND_SOC_NOPM, 0, 0,
  747. da7219_mixout_r_controls,
  748. ARRAY_SIZE(da7219_mixout_r_controls)),
  749. /* Sidetone Mixers */
  750. SND_SOC_DAPM_MIXER("ST Mixer Out FilterL", SND_SOC_NOPM, 0, 0,
  751. da7219_st_out_filtl_mix_controls,
  752. ARRAY_SIZE(da7219_st_out_filtl_mix_controls)),
  753. SND_SOC_DAPM_MIXER("ST Mixer Out FilterR", SND_SOC_NOPM, 0,
  754. 0, da7219_st_out_filtr_mix_controls,
  755. ARRAY_SIZE(da7219_st_out_filtr_mix_controls)),
  756. /* DACs */
  757. SND_SOC_DAPM_DAC("DACL", NULL, DA7219_DAC_L_CTRL, DA7219_DAC_L_EN_SHIFT,
  758. DA7219_NO_INVERT),
  759. SND_SOC_DAPM_DAC("DACR", NULL, DA7219_DAC_R_CTRL, DA7219_DAC_R_EN_SHIFT,
  760. DA7219_NO_INVERT),
  761. /* Output PGAs */
  762. SND_SOC_DAPM_PGA("Mixout Left PGA", DA7219_MIXOUT_L_CTRL,
  763. DA7219_MIXOUT_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
  764. NULL, 0),
  765. SND_SOC_DAPM_PGA("Mixout Right PGA", DA7219_MIXOUT_R_CTRL,
  766. DA7219_MIXOUT_R_AMP_EN_SHIFT, DA7219_NO_INVERT,
  767. NULL, 0),
  768. SND_SOC_DAPM_PGA("Headphone Left PGA", DA7219_HP_L_CTRL,
  769. DA7219_HP_L_AMP_EN_SHIFT, DA7219_NO_INVERT, NULL, 0),
  770. SND_SOC_DAPM_PGA("Headphone Right PGA", DA7219_HP_R_CTRL,
  771. DA7219_HP_R_AMP_EN_SHIFT, DA7219_NO_INVERT, NULL, 0),
  772. /* Output Supplies */
  773. SND_SOC_DAPM_SUPPLY("Charge Pump", DA7219_CP_CTRL, DA7219_CP_EN_SHIFT,
  774. DA7219_NO_INVERT, NULL, 0),
  775. /* Outputs */
  776. SND_SOC_DAPM_OUTPUT("HPL"),
  777. SND_SOC_DAPM_OUTPUT("HPR"),
  778. };
  779. /*
  780. * DAPM Mux Routes
  781. */
  782. #define DA7219_OUT_DAI_MUX_ROUTES(name) \
  783. {name, "ADC", "Mixer In"}, \
  784. {name, "Tone Generator", "Tone Generator"}, \
  785. {name, "DAIL", "DAIOUT"}, \
  786. {name, "DAIR", "DAIOUT"}
  787. #define DA7219_OUT_DAC_MUX_ROUTES(name) \
  788. {name, "ADC", "Mixer In"}, \
  789. {name, "Tone Generator", "Tone Generator"}, \
  790. {name, "DAIL", "DAIIN"}, \
  791. {name, "DAIR", "DAIIN"}
  792. /*
  793. * DAPM Mixer Routes
  794. */
  795. #define DA7219_DMIX_ST_ROUTES(name) \
  796. {name, "Out FilterL Switch", "Mixer Out FilterL"}, \
  797. {name, "Out FilterR Switch", "Mixer Out FilterR"}, \
  798. {name, "Sidetone Switch", "Sidetone Filter"}
  799. /*
  800. * DAPM audio route definition
  801. */
  802. static const struct snd_soc_dapm_route da7219_audio_map[] = {
  803. /* Input paths */
  804. {"MIC", NULL, "Mic Bias"},
  805. {"Mic PGA", NULL, "MIC"},
  806. {"Mixin PGA", NULL, "Mic PGA"},
  807. {"ADC", NULL, "Mixin PGA"},
  808. {"Mixer In", NULL, "Mixer In Supply"},
  809. {"Mixer In", "Mic Switch", "ADC"},
  810. {"Sidetone Filter", NULL, "Mixer In"},
  811. {"Tone Generator", NULL, "TONE"},
  812. DA7219_OUT_DAI_MUX_ROUTES("Out DAIL Mux"),
  813. DA7219_OUT_DAI_MUX_ROUTES("Out DAIR Mux"),
  814. {"DAIOUT", NULL, "Out DAIL Mux"},
  815. {"DAIOUT", NULL, "Out DAIR Mux"},
  816. {"DAIOUT", NULL, "DAI"},
  817. /* Output paths */
  818. {"DAIIN", NULL, "DAI"},
  819. DA7219_OUT_DAC_MUX_ROUTES("Out DACL Mux"),
  820. DA7219_OUT_DAC_MUX_ROUTES("Out DACR Mux"),
  821. {"Mixer Out FilterL", "DACL Switch", "Out DACL Mux"},
  822. {"Mixer Out FilterR", "DACR Switch", "Out DACR Mux"},
  823. DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterL"),
  824. DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterR"),
  825. {"DACL", NULL, "ST Mixer Out FilterL"},
  826. {"DACR", NULL, "ST Mixer Out FilterR"},
  827. {"Mixout Left PGA", NULL, "DACL"},
  828. {"Mixout Right PGA", NULL, "DACR"},
  829. {"Headphone Left PGA", NULL, "Mixout Left PGA"},
  830. {"Headphone Right PGA", NULL, "Mixout Right PGA"},
  831. {"HPL", NULL, "Headphone Left PGA"},
  832. {"HPR", NULL, "Headphone Right PGA"},
  833. {"HPL", NULL, "Charge Pump"},
  834. {"HPR", NULL, "Charge Pump"},
  835. };
  836. /*
  837. * DAI operations
  838. */
  839. static int da7219_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  840. int clk_id, unsigned int freq, int dir)
  841. {
  842. struct snd_soc_codec *codec = codec_dai->codec;
  843. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  844. int ret = 0;
  845. if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq))
  846. return 0;
  847. if ((freq < 2000000) || (freq > 54000000)) {
  848. dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
  849. freq);
  850. return -EINVAL;
  851. }
  852. switch (clk_id) {
  853. case DA7219_CLKSRC_MCLK_SQR:
  854. snd_soc_update_bits(codec, DA7219_PLL_CTRL,
  855. DA7219_PLL_MCLK_SQR_EN_MASK,
  856. DA7219_PLL_MCLK_SQR_EN_MASK);
  857. break;
  858. case DA7219_CLKSRC_MCLK:
  859. snd_soc_update_bits(codec, DA7219_PLL_CTRL,
  860. DA7219_PLL_MCLK_SQR_EN_MASK, 0);
  861. break;
  862. default:
  863. dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
  864. return -EINVAL;
  865. }
  866. da7219->clk_src = clk_id;
  867. if (da7219->mclk) {
  868. freq = clk_round_rate(da7219->mclk, freq);
  869. ret = clk_set_rate(da7219->mclk, freq);
  870. if (ret) {
  871. dev_err(codec_dai->dev, "Failed to set clock rate %d\n",
  872. freq);
  873. return ret;
  874. }
  875. }
  876. da7219->mclk_rate = freq;
  877. return 0;
  878. }
  879. static int da7219_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  880. int source, unsigned int fref, unsigned int fout)
  881. {
  882. struct snd_soc_codec *codec = codec_dai->codec;
  883. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  884. u8 pll_ctrl, indiv_bits, indiv;
  885. u8 pll_frac_top, pll_frac_bot, pll_integer;
  886. u32 freq_ref;
  887. u64 frac_div;
  888. /* Verify 2MHz - 54MHz MCLK provided, and set input divider */
  889. if (da7219->mclk_rate < 2000000) {
  890. dev_err(codec->dev, "PLL input clock %d below valid range\n",
  891. da7219->mclk_rate);
  892. return -EINVAL;
  893. } else if (da7219->mclk_rate <= 4500000) {
  894. indiv_bits = DA7219_PLL_INDIV_2_TO_4_5_MHZ;
  895. indiv = DA7219_PLL_INDIV_2_TO_4_5_MHZ_VAL;
  896. } else if (da7219->mclk_rate <= 9000000) {
  897. indiv_bits = DA7219_PLL_INDIV_4_5_TO_9_MHZ;
  898. indiv = DA7219_PLL_INDIV_4_5_TO_9_MHZ_VAL;
  899. } else if (da7219->mclk_rate <= 18000000) {
  900. indiv_bits = DA7219_PLL_INDIV_9_TO_18_MHZ;
  901. indiv = DA7219_PLL_INDIV_9_TO_18_MHZ_VAL;
  902. } else if (da7219->mclk_rate <= 36000000) {
  903. indiv_bits = DA7219_PLL_INDIV_18_TO_36_MHZ;
  904. indiv = DA7219_PLL_INDIV_18_TO_36_MHZ_VAL;
  905. } else if (da7219->mclk_rate <= 54000000) {
  906. indiv_bits = DA7219_PLL_INDIV_36_TO_54_MHZ;
  907. indiv = DA7219_PLL_INDIV_36_TO_54_MHZ_VAL;
  908. } else {
  909. dev_err(codec->dev, "PLL input clock %d above valid range\n",
  910. da7219->mclk_rate);
  911. return -EINVAL;
  912. }
  913. freq_ref = (da7219->mclk_rate / indiv);
  914. pll_ctrl = indiv_bits;
  915. /* Configure PLL */
  916. switch (source) {
  917. case DA7219_SYSCLK_MCLK:
  918. pll_ctrl |= DA7219_PLL_MODE_BYPASS;
  919. snd_soc_update_bits(codec, DA7219_PLL_CTRL,
  920. DA7219_PLL_INDIV_MASK |
  921. DA7219_PLL_MODE_MASK, pll_ctrl);
  922. return 0;
  923. case DA7219_SYSCLK_PLL:
  924. pll_ctrl |= DA7219_PLL_MODE_NORMAL;
  925. break;
  926. case DA7219_SYSCLK_PLL_SRM:
  927. pll_ctrl |= DA7219_PLL_MODE_SRM;
  928. break;
  929. default:
  930. dev_err(codec->dev, "Invalid PLL config\n");
  931. return -EINVAL;
  932. }
  933. /* Calculate dividers for PLL */
  934. pll_integer = fout / freq_ref;
  935. frac_div = (u64)(fout % freq_ref) * 8192ULL;
  936. do_div(frac_div, freq_ref);
  937. pll_frac_top = (frac_div >> DA7219_BYTE_SHIFT) & DA7219_BYTE_MASK;
  938. pll_frac_bot = (frac_div) & DA7219_BYTE_MASK;
  939. /* Write PLL config & dividers */
  940. snd_soc_write(codec, DA7219_PLL_FRAC_TOP, pll_frac_top);
  941. snd_soc_write(codec, DA7219_PLL_FRAC_BOT, pll_frac_bot);
  942. snd_soc_write(codec, DA7219_PLL_INTEGER, pll_integer);
  943. snd_soc_update_bits(codec, DA7219_PLL_CTRL,
  944. DA7219_PLL_INDIV_MASK | DA7219_PLL_MODE_MASK,
  945. pll_ctrl);
  946. return 0;
  947. }
  948. static int da7219_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  949. {
  950. struct snd_soc_codec *codec = codec_dai->codec;
  951. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  952. u8 dai_clk_mode = 0, dai_ctrl = 0;
  953. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  954. case SND_SOC_DAIFMT_CBM_CFM:
  955. da7219->master = true;
  956. break;
  957. case SND_SOC_DAIFMT_CBS_CFS:
  958. da7219->master = false;
  959. break;
  960. default:
  961. return -EINVAL;
  962. }
  963. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  964. case SND_SOC_DAIFMT_I2S:
  965. case SND_SOC_DAIFMT_LEFT_J:
  966. case SND_SOC_DAIFMT_RIGHT_J:
  967. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  968. case SND_SOC_DAIFMT_NB_NF:
  969. break;
  970. case SND_SOC_DAIFMT_NB_IF:
  971. dai_clk_mode |= DA7219_DAI_WCLK_POL_INV;
  972. break;
  973. case SND_SOC_DAIFMT_IB_NF:
  974. dai_clk_mode |= DA7219_DAI_CLK_POL_INV;
  975. break;
  976. case SND_SOC_DAIFMT_IB_IF:
  977. dai_clk_mode |= DA7219_DAI_WCLK_POL_INV |
  978. DA7219_DAI_CLK_POL_INV;
  979. break;
  980. default:
  981. return -EINVAL;
  982. }
  983. break;
  984. case SND_SOC_DAIFMT_DSP_B:
  985. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  986. case SND_SOC_DAIFMT_NB_NF:
  987. dai_clk_mode |= DA7219_DAI_CLK_POL_INV;
  988. break;
  989. case SND_SOC_DAIFMT_NB_IF:
  990. dai_clk_mode |= DA7219_DAI_WCLK_POL_INV |
  991. DA7219_DAI_CLK_POL_INV;
  992. break;
  993. case SND_SOC_DAIFMT_IB_NF:
  994. break;
  995. case SND_SOC_DAIFMT_IB_IF:
  996. dai_clk_mode |= DA7219_DAI_WCLK_POL_INV;
  997. break;
  998. default:
  999. return -EINVAL;
  1000. }
  1001. break;
  1002. default:
  1003. return -EINVAL;
  1004. }
  1005. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1006. case SND_SOC_DAIFMT_I2S:
  1007. dai_ctrl |= DA7219_DAI_FORMAT_I2S;
  1008. break;
  1009. case SND_SOC_DAIFMT_LEFT_J:
  1010. dai_ctrl |= DA7219_DAI_FORMAT_LEFT_J;
  1011. break;
  1012. case SND_SOC_DAIFMT_RIGHT_J:
  1013. dai_ctrl |= DA7219_DAI_FORMAT_RIGHT_J;
  1014. break;
  1015. case SND_SOC_DAIFMT_DSP_B:
  1016. dai_ctrl |= DA7219_DAI_FORMAT_DSP;
  1017. break;
  1018. default:
  1019. return -EINVAL;
  1020. }
  1021. /* By default 64 BCLKs per WCLK is supported */
  1022. dai_clk_mode |= DA7219_DAI_BCLKS_PER_WCLK_64;
  1023. snd_soc_update_bits(codec, DA7219_DAI_CLK_MODE,
  1024. DA7219_DAI_BCLKS_PER_WCLK_MASK |
  1025. DA7219_DAI_CLK_POL_MASK | DA7219_DAI_WCLK_POL_MASK,
  1026. dai_clk_mode);
  1027. snd_soc_update_bits(codec, DA7219_DAI_CTRL, DA7219_DAI_FORMAT_MASK,
  1028. dai_ctrl);
  1029. return 0;
  1030. }
  1031. static int da7219_set_dai_tdm_slot(struct snd_soc_dai *dai,
  1032. unsigned int tx_mask, unsigned int rx_mask,
  1033. int slots, int slot_width)
  1034. {
  1035. struct snd_soc_codec *codec = dai->codec;
  1036. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  1037. u8 dai_bclks_per_wclk;
  1038. u16 offset;
  1039. u32 frame_size;
  1040. /* No channels enabled so disable TDM, revert to 64-bit frames */
  1041. if (!tx_mask) {
  1042. snd_soc_update_bits(codec, DA7219_DAI_TDM_CTRL,
  1043. DA7219_DAI_TDM_CH_EN_MASK |
  1044. DA7219_DAI_TDM_MODE_EN_MASK, 0);
  1045. snd_soc_update_bits(codec, DA7219_DAI_CLK_MODE,
  1046. DA7219_DAI_BCLKS_PER_WCLK_MASK,
  1047. DA7219_DAI_BCLKS_PER_WCLK_64);
  1048. return 0;
  1049. }
  1050. /* Check we have valid slots */
  1051. if (fls(tx_mask) > DA7219_DAI_TDM_MAX_SLOTS) {
  1052. dev_err(codec->dev, "Invalid number of slots, max = %d\n",
  1053. DA7219_DAI_TDM_MAX_SLOTS);
  1054. return -EINVAL;
  1055. }
  1056. /* Check we have a valid offset given */
  1057. if (rx_mask > DA7219_DAI_OFFSET_MAX) {
  1058. dev_err(codec->dev, "Invalid slot offset, max = %d\n",
  1059. DA7219_DAI_OFFSET_MAX);
  1060. return -EINVAL;
  1061. }
  1062. /* Calculate & validate frame size based on slot info provided. */
  1063. frame_size = slots * slot_width;
  1064. switch (frame_size) {
  1065. case 32:
  1066. dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_32;
  1067. break;
  1068. case 64:
  1069. dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_64;
  1070. break;
  1071. case 128:
  1072. dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_128;
  1073. break;
  1074. case 256:
  1075. dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_256;
  1076. break;
  1077. default:
  1078. dev_err(codec->dev, "Invalid frame size %d\n", frame_size);
  1079. return -EINVAL;
  1080. }
  1081. snd_soc_update_bits(codec, DA7219_DAI_CLK_MODE,
  1082. DA7219_DAI_BCLKS_PER_WCLK_MASK,
  1083. dai_bclks_per_wclk);
  1084. offset = cpu_to_le16(rx_mask);
  1085. regmap_bulk_write(da7219->regmap, DA7219_DAI_OFFSET_LOWER,
  1086. &offset, sizeof(offset));
  1087. snd_soc_update_bits(codec, DA7219_DAI_TDM_CTRL,
  1088. DA7219_DAI_TDM_CH_EN_MASK |
  1089. DA7219_DAI_TDM_MODE_EN_MASK,
  1090. (tx_mask << DA7219_DAI_TDM_CH_EN_SHIFT) |
  1091. DA7219_DAI_TDM_MODE_EN_MASK);
  1092. return 0;
  1093. }
  1094. static int da7219_hw_params(struct snd_pcm_substream *substream,
  1095. struct snd_pcm_hw_params *params,
  1096. struct snd_soc_dai *dai)
  1097. {
  1098. struct snd_soc_codec *codec = dai->codec;
  1099. u8 dai_ctrl = 0, fs;
  1100. unsigned int channels;
  1101. switch (params_width(params)) {
  1102. case 16:
  1103. dai_ctrl |= DA7219_DAI_WORD_LENGTH_S16_LE;
  1104. break;
  1105. case 20:
  1106. dai_ctrl |= DA7219_DAI_WORD_LENGTH_S20_LE;
  1107. break;
  1108. case 24:
  1109. dai_ctrl |= DA7219_DAI_WORD_LENGTH_S24_LE;
  1110. break;
  1111. case 32:
  1112. dai_ctrl |= DA7219_DAI_WORD_LENGTH_S32_LE;
  1113. break;
  1114. default:
  1115. return -EINVAL;
  1116. }
  1117. channels = params_channels(params);
  1118. if ((channels < 1) || (channels > DA7219_DAI_CH_NUM_MAX)) {
  1119. dev_err(codec->dev,
  1120. "Invalid number of channels, only 1 to %d supported\n",
  1121. DA7219_DAI_CH_NUM_MAX);
  1122. return -EINVAL;
  1123. }
  1124. dai_ctrl |= channels << DA7219_DAI_CH_NUM_SHIFT;
  1125. switch (params_rate(params)) {
  1126. case 8000:
  1127. fs = DA7219_SR_8000;
  1128. break;
  1129. case 11025:
  1130. fs = DA7219_SR_11025;
  1131. break;
  1132. case 12000:
  1133. fs = DA7219_SR_12000;
  1134. break;
  1135. case 16000:
  1136. fs = DA7219_SR_16000;
  1137. break;
  1138. case 22050:
  1139. fs = DA7219_SR_22050;
  1140. break;
  1141. case 24000:
  1142. fs = DA7219_SR_24000;
  1143. break;
  1144. case 32000:
  1145. fs = DA7219_SR_32000;
  1146. break;
  1147. case 44100:
  1148. fs = DA7219_SR_44100;
  1149. break;
  1150. case 48000:
  1151. fs = DA7219_SR_48000;
  1152. break;
  1153. case 88200:
  1154. fs = DA7219_SR_88200;
  1155. break;
  1156. case 96000:
  1157. fs = DA7219_SR_96000;
  1158. break;
  1159. default:
  1160. return -EINVAL;
  1161. }
  1162. snd_soc_update_bits(codec, DA7219_DAI_CTRL,
  1163. DA7219_DAI_WORD_LENGTH_MASK |
  1164. DA7219_DAI_CH_NUM_MASK,
  1165. dai_ctrl);
  1166. snd_soc_write(codec, DA7219_SR, fs);
  1167. return 0;
  1168. }
  1169. static const struct snd_soc_dai_ops da7219_dai_ops = {
  1170. .hw_params = da7219_hw_params,
  1171. .set_sysclk = da7219_set_dai_sysclk,
  1172. .set_pll = da7219_set_dai_pll,
  1173. .set_fmt = da7219_set_dai_fmt,
  1174. .set_tdm_slot = da7219_set_dai_tdm_slot,
  1175. };
  1176. #define DA7219_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1177. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1178. static struct snd_soc_dai_driver da7219_dai = {
  1179. .name = "da7219-hifi",
  1180. .playback = {
  1181. .stream_name = "Playback",
  1182. .channels_min = 1,
  1183. .channels_max = DA7219_DAI_CH_NUM_MAX,
  1184. .rates = SNDRV_PCM_RATE_8000_96000,
  1185. .formats = DA7219_FORMATS,
  1186. },
  1187. .capture = {
  1188. .stream_name = "Capture",
  1189. .channels_min = 1,
  1190. .channels_max = DA7219_DAI_CH_NUM_MAX,
  1191. .rates = SNDRV_PCM_RATE_8000_96000,
  1192. .formats = DA7219_FORMATS,
  1193. },
  1194. .ops = &da7219_dai_ops,
  1195. .symmetric_rates = 1,
  1196. .symmetric_channels = 1,
  1197. .symmetric_samplebits = 1,
  1198. };
  1199. /*
  1200. * DT/ACPI
  1201. */
  1202. static const struct of_device_id da7219_of_match[] = {
  1203. { .compatible = "dlg,da7219", },
  1204. { }
  1205. };
  1206. MODULE_DEVICE_TABLE(of, da7219_of_match);
  1207. static const struct acpi_device_id da7219_acpi_match[] = {
  1208. { .id = "DLGS7219", },
  1209. { }
  1210. };
  1211. MODULE_DEVICE_TABLE(acpi, da7219_acpi_match);
  1212. static enum da7219_micbias_voltage
  1213. da7219_fw_micbias_lvl(struct device *dev, u32 val)
  1214. {
  1215. switch (val) {
  1216. case 1600:
  1217. return DA7219_MICBIAS_1_6V;
  1218. case 1800:
  1219. return DA7219_MICBIAS_1_8V;
  1220. case 2000:
  1221. return DA7219_MICBIAS_2_0V;
  1222. case 2200:
  1223. return DA7219_MICBIAS_2_2V;
  1224. case 2400:
  1225. return DA7219_MICBIAS_2_4V;
  1226. case 2600:
  1227. return DA7219_MICBIAS_2_6V;
  1228. default:
  1229. dev_warn(dev, "Invalid micbias level");
  1230. return DA7219_MICBIAS_2_2V;
  1231. }
  1232. }
  1233. static enum da7219_mic_amp_in_sel
  1234. da7219_fw_mic_amp_in_sel(struct device *dev, const char *str)
  1235. {
  1236. if (!strcmp(str, "diff")) {
  1237. return DA7219_MIC_AMP_IN_SEL_DIFF;
  1238. } else if (!strcmp(str, "se_p")) {
  1239. return DA7219_MIC_AMP_IN_SEL_SE_P;
  1240. } else if (!strcmp(str, "se_n")) {
  1241. return DA7219_MIC_AMP_IN_SEL_SE_N;
  1242. } else {
  1243. dev_warn(dev, "Invalid mic input type selection");
  1244. return DA7219_MIC_AMP_IN_SEL_DIFF;
  1245. }
  1246. }
  1247. static struct da7219_pdata *da7219_fw_to_pdata(struct snd_soc_codec *codec)
  1248. {
  1249. struct device *dev = codec->dev;
  1250. struct da7219_pdata *pdata;
  1251. const char *of_str;
  1252. u32 of_val32;
  1253. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1254. if (!pdata)
  1255. return NULL;
  1256. pdata->wakeup_source = device_property_read_bool(dev, "wakeup-source");
  1257. if (device_property_read_u32(dev, "dlg,micbias-lvl", &of_val32) >= 0)
  1258. pdata->micbias_lvl = da7219_fw_micbias_lvl(dev, of_val32);
  1259. else
  1260. pdata->micbias_lvl = DA7219_MICBIAS_2_2V;
  1261. if (!device_property_read_string(dev, "dlg,mic-amp-in-sel", &of_str))
  1262. pdata->mic_amp_in_sel = da7219_fw_mic_amp_in_sel(dev, of_str);
  1263. else
  1264. pdata->mic_amp_in_sel = DA7219_MIC_AMP_IN_SEL_DIFF;
  1265. return pdata;
  1266. }
  1267. /*
  1268. * Codec driver functions
  1269. */
  1270. static int da7219_set_bias_level(struct snd_soc_codec *codec,
  1271. enum snd_soc_bias_level level)
  1272. {
  1273. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  1274. int ret;
  1275. switch (level) {
  1276. case SND_SOC_BIAS_ON:
  1277. break;
  1278. case SND_SOC_BIAS_PREPARE:
  1279. /* Enable MCLK for transition to ON state */
  1280. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_STANDBY) {
  1281. if (da7219->mclk) {
  1282. ret = clk_prepare_enable(da7219->mclk);
  1283. if (ret) {
  1284. dev_err(codec->dev,
  1285. "Failed to enable mclk\n");
  1286. return ret;
  1287. }
  1288. }
  1289. }
  1290. break;
  1291. case SND_SOC_BIAS_STANDBY:
  1292. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF)
  1293. /* Master bias */
  1294. snd_soc_update_bits(codec, DA7219_REFERENCES,
  1295. DA7219_BIAS_EN_MASK,
  1296. DA7219_BIAS_EN_MASK);
  1297. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_PREPARE) {
  1298. /* Remove MCLK */
  1299. if (da7219->mclk)
  1300. clk_disable_unprepare(da7219->mclk);
  1301. }
  1302. break;
  1303. case SND_SOC_BIAS_OFF:
  1304. /* Only disable master bias if we're not a wake-up source */
  1305. if (!da7219->wakeup_source)
  1306. snd_soc_update_bits(codec, DA7219_REFERENCES,
  1307. DA7219_BIAS_EN_MASK, 0);
  1308. break;
  1309. }
  1310. return 0;
  1311. }
  1312. static const char *da7219_supply_names[DA7219_NUM_SUPPLIES] = {
  1313. [DA7219_SUPPLY_VDD] = "VDD",
  1314. [DA7219_SUPPLY_VDDMIC] = "VDDMIC",
  1315. [DA7219_SUPPLY_VDDIO] = "VDDIO",
  1316. };
  1317. static int da7219_handle_supplies(struct snd_soc_codec *codec)
  1318. {
  1319. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  1320. struct regulator *vddio;
  1321. u8 io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_2_5V_3_6V;
  1322. int i, ret;
  1323. /* Get required supplies */
  1324. for (i = 0; i < DA7219_NUM_SUPPLIES; ++i)
  1325. da7219->supplies[i].supply = da7219_supply_names[i];
  1326. ret = devm_regulator_bulk_get(codec->dev, DA7219_NUM_SUPPLIES,
  1327. da7219->supplies);
  1328. if (ret) {
  1329. dev_err(codec->dev, "Failed to get supplies");
  1330. return ret;
  1331. }
  1332. /* Determine VDDIO voltage provided */
  1333. vddio = da7219->supplies[DA7219_SUPPLY_VDDIO].consumer;
  1334. ret = regulator_get_voltage(vddio);
  1335. if (ret < 1200000)
  1336. dev_warn(codec->dev, "Invalid VDDIO voltage\n");
  1337. else if (ret < 2800000)
  1338. io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_1_2V_2_8V;
  1339. /* Enable main supplies */
  1340. ret = regulator_bulk_enable(DA7219_NUM_SUPPLIES, da7219->supplies);
  1341. if (ret) {
  1342. dev_err(codec->dev, "Failed to enable supplies");
  1343. return ret;
  1344. }
  1345. /* Ensure device in active mode */
  1346. snd_soc_write(codec, DA7219_SYSTEM_ACTIVE, DA7219_SYSTEM_ACTIVE_MASK);
  1347. /* Update IO voltage level range */
  1348. snd_soc_write(codec, DA7219_IO_CTRL, io_voltage_lvl);
  1349. return 0;
  1350. }
  1351. static void da7219_handle_pdata(struct snd_soc_codec *codec)
  1352. {
  1353. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  1354. struct da7219_pdata *pdata = da7219->pdata;
  1355. if (pdata) {
  1356. u8 micbias_lvl = 0;
  1357. da7219->wakeup_source = pdata->wakeup_source;
  1358. /* Mic Bias voltages */
  1359. switch (pdata->micbias_lvl) {
  1360. case DA7219_MICBIAS_1_6V:
  1361. case DA7219_MICBIAS_1_8V:
  1362. case DA7219_MICBIAS_2_0V:
  1363. case DA7219_MICBIAS_2_2V:
  1364. case DA7219_MICBIAS_2_4V:
  1365. case DA7219_MICBIAS_2_6V:
  1366. micbias_lvl |= (pdata->micbias_lvl <<
  1367. DA7219_MICBIAS1_LEVEL_SHIFT);
  1368. break;
  1369. }
  1370. snd_soc_write(codec, DA7219_MICBIAS_CTRL, micbias_lvl);
  1371. /* Mic */
  1372. switch (pdata->mic_amp_in_sel) {
  1373. case DA7219_MIC_AMP_IN_SEL_DIFF:
  1374. case DA7219_MIC_AMP_IN_SEL_SE_P:
  1375. case DA7219_MIC_AMP_IN_SEL_SE_N:
  1376. snd_soc_write(codec, DA7219_MIC_1_SELECT,
  1377. pdata->mic_amp_in_sel);
  1378. break;
  1379. }
  1380. }
  1381. }
  1382. static struct reg_sequence da7219_rev_aa_patch[] = {
  1383. { DA7219_REFERENCES, 0x08 },
  1384. };
  1385. static int da7219_probe(struct snd_soc_codec *codec)
  1386. {
  1387. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  1388. unsigned int rev;
  1389. int ret;
  1390. mutex_init(&da7219->lock);
  1391. /* Regulator configuration */
  1392. ret = da7219_handle_supplies(codec);
  1393. if (ret)
  1394. return ret;
  1395. ret = regmap_read(da7219->regmap, DA7219_CHIP_REVISION, &rev);
  1396. if (ret) {
  1397. dev_err(codec->dev, "Failed to read chip revision: %d\n", ret);
  1398. goto err_disable_reg;
  1399. }
  1400. switch (rev & DA7219_CHIP_MINOR_MASK) {
  1401. case 0:
  1402. ret = regmap_register_patch(da7219->regmap, da7219_rev_aa_patch,
  1403. ARRAY_SIZE(da7219_rev_aa_patch));
  1404. if (ret) {
  1405. dev_err(codec->dev, "Failed to register AA patch: %d\n",
  1406. ret);
  1407. goto err_disable_reg;
  1408. }
  1409. break;
  1410. default:
  1411. break;
  1412. }
  1413. /* Handle DT/ACPI/Platform data */
  1414. da7219->pdata = dev_get_platdata(codec->dev);
  1415. if (!da7219->pdata)
  1416. da7219->pdata = da7219_fw_to_pdata(codec);
  1417. da7219_handle_pdata(codec);
  1418. /* Check if MCLK provided */
  1419. da7219->mclk = devm_clk_get(codec->dev, "mclk");
  1420. if (IS_ERR(da7219->mclk)) {
  1421. if (PTR_ERR(da7219->mclk) != -ENOENT) {
  1422. ret = PTR_ERR(da7219->mclk);
  1423. goto err_disable_reg;
  1424. } else {
  1425. da7219->mclk = NULL;
  1426. }
  1427. }
  1428. /* Default PC counter to free-running */
  1429. snd_soc_update_bits(codec, DA7219_PC_COUNT, DA7219_PC_FREERUN_MASK,
  1430. DA7219_PC_FREERUN_MASK);
  1431. /* Default gain ramping */
  1432. snd_soc_update_bits(codec, DA7219_MIXIN_L_CTRL,
  1433. DA7219_MIXIN_L_AMP_RAMP_EN_MASK,
  1434. DA7219_MIXIN_L_AMP_RAMP_EN_MASK);
  1435. snd_soc_update_bits(codec, DA7219_ADC_L_CTRL, DA7219_ADC_L_RAMP_EN_MASK,
  1436. DA7219_ADC_L_RAMP_EN_MASK);
  1437. snd_soc_update_bits(codec, DA7219_DAC_L_CTRL, DA7219_DAC_L_RAMP_EN_MASK,
  1438. DA7219_DAC_L_RAMP_EN_MASK);
  1439. snd_soc_update_bits(codec, DA7219_DAC_R_CTRL, DA7219_DAC_R_RAMP_EN_MASK,
  1440. DA7219_DAC_R_RAMP_EN_MASK);
  1441. snd_soc_update_bits(codec, DA7219_HP_L_CTRL,
  1442. DA7219_HP_L_AMP_RAMP_EN_MASK,
  1443. DA7219_HP_L_AMP_RAMP_EN_MASK);
  1444. snd_soc_update_bits(codec, DA7219_HP_R_CTRL,
  1445. DA7219_HP_R_AMP_RAMP_EN_MASK,
  1446. DA7219_HP_R_AMP_RAMP_EN_MASK);
  1447. /* Default infinite tone gen, start/stop by Kcontrol */
  1448. snd_soc_write(codec, DA7219_TONE_GEN_CYCLES, DA7219_BEEP_CYCLES_MASK);
  1449. /* Initialise AAD block */
  1450. ret = da7219_aad_init(codec);
  1451. if (ret)
  1452. goto err_disable_reg;
  1453. return 0;
  1454. err_disable_reg:
  1455. regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
  1456. return ret;
  1457. }
  1458. static int da7219_remove(struct snd_soc_codec *codec)
  1459. {
  1460. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  1461. da7219_aad_exit(codec);
  1462. /* Supplies */
  1463. return regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
  1464. }
  1465. #ifdef CONFIG_PM
  1466. static int da7219_suspend(struct snd_soc_codec *codec)
  1467. {
  1468. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  1469. /* Suspend AAD if we're not a wake-up source */
  1470. if (!da7219->wakeup_source)
  1471. da7219_aad_suspend(codec);
  1472. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
  1473. return 0;
  1474. }
  1475. static int da7219_resume(struct snd_soc_codec *codec)
  1476. {
  1477. struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
  1478. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1479. /* Resume AAD if previously suspended */
  1480. if (!da7219->wakeup_source)
  1481. da7219_aad_resume(codec);
  1482. return 0;
  1483. }
  1484. #else
  1485. #define da7219_suspend NULL
  1486. #define da7219_resume NULL
  1487. #endif
  1488. static struct snd_soc_codec_driver soc_codec_dev_da7219 = {
  1489. .probe = da7219_probe,
  1490. .remove = da7219_remove,
  1491. .suspend = da7219_suspend,
  1492. .resume = da7219_resume,
  1493. .set_bias_level = da7219_set_bias_level,
  1494. .component_driver = {
  1495. .controls = da7219_snd_controls,
  1496. .num_controls = ARRAY_SIZE(da7219_snd_controls),
  1497. .dapm_widgets = da7219_dapm_widgets,
  1498. .num_dapm_widgets = ARRAY_SIZE(da7219_dapm_widgets),
  1499. .dapm_routes = da7219_audio_map,
  1500. .num_dapm_routes = ARRAY_SIZE(da7219_audio_map),
  1501. },
  1502. };
  1503. /*
  1504. * Regmap configs
  1505. */
  1506. static struct reg_default da7219_reg_defaults[] = {
  1507. { DA7219_MIC_1_SELECT, 0x00 },
  1508. { DA7219_CIF_TIMEOUT_CTRL, 0x01 },
  1509. { DA7219_SR_24_48, 0x00 },
  1510. { DA7219_SR, 0x0A },
  1511. { DA7219_CIF_I2C_ADDR_CFG, 0x02 },
  1512. { DA7219_PLL_CTRL, 0x10 },
  1513. { DA7219_PLL_FRAC_TOP, 0x00 },
  1514. { DA7219_PLL_FRAC_BOT, 0x00 },
  1515. { DA7219_PLL_INTEGER, 0x20 },
  1516. { DA7219_DIG_ROUTING_DAI, 0x10 },
  1517. { DA7219_DAI_CLK_MODE, 0x01 },
  1518. { DA7219_DAI_CTRL, 0x28 },
  1519. { DA7219_DAI_TDM_CTRL, 0x40 },
  1520. { DA7219_DIG_ROUTING_DAC, 0x32 },
  1521. { DA7219_DAI_OFFSET_LOWER, 0x00 },
  1522. { DA7219_DAI_OFFSET_UPPER, 0x00 },
  1523. { DA7219_REFERENCES, 0x08 },
  1524. { DA7219_MIXIN_L_SELECT, 0x00 },
  1525. { DA7219_MIXIN_L_GAIN, 0x03 },
  1526. { DA7219_ADC_L_GAIN, 0x6F },
  1527. { DA7219_ADC_FILTERS1, 0x80 },
  1528. { DA7219_MIC_1_GAIN, 0x01 },
  1529. { DA7219_SIDETONE_CTRL, 0x40 },
  1530. { DA7219_SIDETONE_GAIN, 0x0E },
  1531. { DA7219_DROUTING_ST_OUTFILT_1L, 0x01 },
  1532. { DA7219_DROUTING_ST_OUTFILT_1R, 0x02 },
  1533. { DA7219_DAC_FILTERS5, 0x00 },
  1534. { DA7219_DAC_FILTERS2, 0x88 },
  1535. { DA7219_DAC_FILTERS3, 0x88 },
  1536. { DA7219_DAC_FILTERS4, 0x08 },
  1537. { DA7219_DAC_FILTERS1, 0x80 },
  1538. { DA7219_DAC_L_GAIN, 0x6F },
  1539. { DA7219_DAC_R_GAIN, 0x6F },
  1540. { DA7219_CP_CTRL, 0x20 },
  1541. { DA7219_HP_L_GAIN, 0x39 },
  1542. { DA7219_HP_R_GAIN, 0x39 },
  1543. { DA7219_MIXOUT_L_SELECT, 0x00 },
  1544. { DA7219_MIXOUT_R_SELECT, 0x00 },
  1545. { DA7219_MICBIAS_CTRL, 0x03 },
  1546. { DA7219_MIC_1_CTRL, 0x40 },
  1547. { DA7219_MIXIN_L_CTRL, 0x40 },
  1548. { DA7219_ADC_L_CTRL, 0x40 },
  1549. { DA7219_DAC_L_CTRL, 0x40 },
  1550. { DA7219_DAC_R_CTRL, 0x40 },
  1551. { DA7219_HP_L_CTRL, 0x40 },
  1552. { DA7219_HP_R_CTRL, 0x40 },
  1553. { DA7219_MIXOUT_L_CTRL, 0x10 },
  1554. { DA7219_MIXOUT_R_CTRL, 0x10 },
  1555. { DA7219_CHIP_ID1, 0x23 },
  1556. { DA7219_CHIP_ID2, 0x93 },
  1557. { DA7219_IO_CTRL, 0x00 },
  1558. { DA7219_GAIN_RAMP_CTRL, 0x00 },
  1559. { DA7219_PC_COUNT, 0x02 },
  1560. { DA7219_CP_VOL_THRESHOLD1, 0x0E },
  1561. { DA7219_DIG_CTRL, 0x00 },
  1562. { DA7219_ALC_CTRL2, 0x00 },
  1563. { DA7219_ALC_CTRL3, 0x00 },
  1564. { DA7219_ALC_NOISE, 0x3F },
  1565. { DA7219_ALC_TARGET_MIN, 0x3F },
  1566. { DA7219_ALC_TARGET_MAX, 0x00 },
  1567. { DA7219_ALC_GAIN_LIMITS, 0xFF },
  1568. { DA7219_ALC_ANA_GAIN_LIMITS, 0x71 },
  1569. { DA7219_ALC_ANTICLIP_CTRL, 0x00 },
  1570. { DA7219_ALC_ANTICLIP_LEVEL, 0x00 },
  1571. { DA7219_DAC_NG_SETUP_TIME, 0x00 },
  1572. { DA7219_DAC_NG_OFF_THRESH, 0x00 },
  1573. { DA7219_DAC_NG_ON_THRESH, 0x00 },
  1574. { DA7219_DAC_NG_CTRL, 0x00 },
  1575. { DA7219_TONE_GEN_CFG1, 0x00 },
  1576. { DA7219_TONE_GEN_CFG2, 0x00 },
  1577. { DA7219_TONE_GEN_CYCLES, 0x00 },
  1578. { DA7219_TONE_GEN_FREQ1_L, 0x55 },
  1579. { DA7219_TONE_GEN_FREQ1_U, 0x15 },
  1580. { DA7219_TONE_GEN_FREQ2_L, 0x00 },
  1581. { DA7219_TONE_GEN_FREQ2_U, 0x40 },
  1582. { DA7219_TONE_GEN_ON_PER, 0x02 },
  1583. { DA7219_TONE_GEN_OFF_PER, 0x01 },
  1584. { DA7219_ACCDET_IRQ_MASK_A, 0x00 },
  1585. { DA7219_ACCDET_IRQ_MASK_B, 0x00 },
  1586. { DA7219_ACCDET_CONFIG_1, 0xD6 },
  1587. { DA7219_ACCDET_CONFIG_2, 0x34 },
  1588. { DA7219_ACCDET_CONFIG_3, 0x0A },
  1589. { DA7219_ACCDET_CONFIG_4, 0x16 },
  1590. { DA7219_ACCDET_CONFIG_5, 0x21 },
  1591. { DA7219_ACCDET_CONFIG_6, 0x3E },
  1592. { DA7219_ACCDET_CONFIG_7, 0x01 },
  1593. { DA7219_SYSTEM_ACTIVE, 0x00 },
  1594. };
  1595. static bool da7219_volatile_register(struct device *dev, unsigned int reg)
  1596. {
  1597. switch (reg) {
  1598. case DA7219_MIC_1_GAIN_STATUS:
  1599. case DA7219_MIXIN_L_GAIN_STATUS:
  1600. case DA7219_ADC_L_GAIN_STATUS:
  1601. case DA7219_DAC_L_GAIN_STATUS:
  1602. case DA7219_DAC_R_GAIN_STATUS:
  1603. case DA7219_HP_L_GAIN_STATUS:
  1604. case DA7219_HP_R_GAIN_STATUS:
  1605. case DA7219_CIF_CTRL:
  1606. case DA7219_PLL_SRM_STS:
  1607. case DA7219_ALC_CTRL1:
  1608. case DA7219_SYSTEM_MODES_INPUT:
  1609. case DA7219_SYSTEM_MODES_OUTPUT:
  1610. case DA7219_ALC_OFFSET_AUTO_M_L:
  1611. case DA7219_ALC_OFFSET_AUTO_U_L:
  1612. case DA7219_TONE_GEN_CFG1:
  1613. case DA7219_ACCDET_STATUS_A:
  1614. case DA7219_ACCDET_STATUS_B:
  1615. case DA7219_ACCDET_IRQ_EVENT_A:
  1616. case DA7219_ACCDET_IRQ_EVENT_B:
  1617. case DA7219_ACCDET_CONFIG_8:
  1618. case DA7219_SYSTEM_STATUS:
  1619. return 1;
  1620. default:
  1621. return 0;
  1622. }
  1623. }
  1624. static const struct regmap_config da7219_regmap_config = {
  1625. .reg_bits = 8,
  1626. .val_bits = 8,
  1627. .max_register = DA7219_SYSTEM_ACTIVE,
  1628. .reg_defaults = da7219_reg_defaults,
  1629. .num_reg_defaults = ARRAY_SIZE(da7219_reg_defaults),
  1630. .volatile_reg = da7219_volatile_register,
  1631. .cache_type = REGCACHE_RBTREE,
  1632. };
  1633. /*
  1634. * I2C layer
  1635. */
  1636. static int da7219_i2c_probe(struct i2c_client *i2c,
  1637. const struct i2c_device_id *id)
  1638. {
  1639. struct da7219_priv *da7219;
  1640. unsigned int system_active, system_status;
  1641. int i, ret;
  1642. da7219 = devm_kzalloc(&i2c->dev, sizeof(struct da7219_priv),
  1643. GFP_KERNEL);
  1644. if (!da7219)
  1645. return -ENOMEM;
  1646. i2c_set_clientdata(i2c, da7219);
  1647. da7219->regmap = devm_regmap_init_i2c(i2c, &da7219_regmap_config);
  1648. if (IS_ERR(da7219->regmap)) {
  1649. ret = PTR_ERR(da7219->regmap);
  1650. dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
  1651. return ret;
  1652. }
  1653. regcache_cache_bypass(da7219->regmap, true);
  1654. /* Disable audio paths if still active from previous start */
  1655. regmap_read(da7219->regmap, DA7219_SYSTEM_ACTIVE, &system_active);
  1656. if (system_active) {
  1657. regmap_write(da7219->regmap, DA7219_GAIN_RAMP_CTRL,
  1658. DA7219_GAIN_RAMP_RATE_NOMINAL);
  1659. regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_INPUT, 0x00);
  1660. regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_OUTPUT, 0x01);
  1661. for (i = 0; i < DA7219_SYS_STAT_CHECK_RETRIES; ++i) {
  1662. regmap_read(da7219->regmap, DA7219_SYSTEM_STATUS,
  1663. &system_status);
  1664. if (!system_status)
  1665. break;
  1666. msleep(DA7219_SYS_STAT_CHECK_DELAY);
  1667. }
  1668. }
  1669. /* Soft reset codec */
  1670. regmap_write_bits(da7219->regmap, DA7219_ACCDET_CONFIG_1,
  1671. DA7219_ACCDET_EN_MASK, 0);
  1672. regmap_write_bits(da7219->regmap, DA7219_CIF_CTRL,
  1673. DA7219_CIF_REG_SOFT_RESET_MASK,
  1674. DA7219_CIF_REG_SOFT_RESET_MASK);
  1675. regmap_write_bits(da7219->regmap, DA7219_SYSTEM_ACTIVE,
  1676. DA7219_SYSTEM_ACTIVE_MASK, 0);
  1677. regcache_cache_bypass(da7219->regmap, false);
  1678. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_da7219,
  1679. &da7219_dai, 1);
  1680. if (ret < 0) {
  1681. dev_err(&i2c->dev, "Failed to register da7219 codec: %d\n",
  1682. ret);
  1683. }
  1684. return ret;
  1685. }
  1686. static int da7219_i2c_remove(struct i2c_client *client)
  1687. {
  1688. snd_soc_unregister_codec(&client->dev);
  1689. return 0;
  1690. }
  1691. static const struct i2c_device_id da7219_i2c_id[] = {
  1692. { "da7219", },
  1693. { }
  1694. };
  1695. MODULE_DEVICE_TABLE(i2c, da7219_i2c_id);
  1696. static struct i2c_driver da7219_i2c_driver = {
  1697. .driver = {
  1698. .name = "da7219",
  1699. .of_match_table = of_match_ptr(da7219_of_match),
  1700. .acpi_match_table = ACPI_PTR(da7219_acpi_match),
  1701. },
  1702. .probe = da7219_i2c_probe,
  1703. .remove = da7219_i2c_remove,
  1704. .id_table = da7219_i2c_id,
  1705. };
  1706. module_i2c_driver(da7219_i2c_driver);
  1707. MODULE_DESCRIPTION("ASoC DA7219 Codec Driver");
  1708. MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
  1709. MODULE_LICENSE("GPL");