es8316.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * es8316.c -- es8316 ALSA SoC audio driver
  4. * Copyright Everest Semiconductor Co.,Ltd
  5. *
  6. * Authors: David Yang <[email protected]>,
  7. * Daniel Drake <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/acpi.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/i2c.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/mutex.h>
  16. #include <linux/regmap.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/soc.h>
  20. #include <sound/soc-dapm.h>
  21. #include <sound/tlv.h>
  22. #include <sound/jack.h>
  23. #include "es8316.h"
  24. /* In slave mode at single speed, the codec is documented as accepting 5
  25. * MCLK/LRCK ratios, but we also add ratio 400, which is commonly used on
  26. * Intel Cherry Trail platforms (19.2MHz MCLK, 48kHz LRCK).
  27. */
  28. #define NR_SUPPORTED_MCLK_LRCK_RATIOS 6
  29. static const unsigned int supported_mclk_lrck_ratios[] = {
  30. 256, 384, 400, 512, 768, 1024
  31. };
  32. struct es8316_priv {
  33. struct mutex lock;
  34. struct clk *mclk;
  35. struct regmap *regmap;
  36. struct snd_soc_component *component;
  37. struct snd_soc_jack *jack;
  38. int irq;
  39. unsigned int sysclk;
  40. unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS];
  41. struct snd_pcm_hw_constraint_list sysclk_constraints;
  42. bool jd_inverted;
  43. };
  44. /*
  45. * ES8316 controls
  46. */
  47. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
  48. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
  49. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0);
  50. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0);
  51. static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(alc_target_tlv,
  52. 0, 10, TLV_DB_SCALE_ITEM(-1650, 150, 0),
  53. 11, 11, TLV_DB_SCALE_ITEM(-150, 0, 0),
  54. );
  55. static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv,
  56. 0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0),
  57. 8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0),
  58. );
  59. static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
  60. 0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
  61. 1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
  62. 2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
  63. 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
  64. 4, 7, TLV_DB_SCALE_ITEM(700, 300, 0),
  65. 8, 10, TLV_DB_SCALE_ITEM(1800, 300, 0),
  66. );
  67. static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv,
  68. 0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0),
  69. 1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0),
  70. );
  71. static const char * const ng_type_txt[] =
  72. { "Constant PGA Gain", "Mute ADC Output" };
  73. static const struct soc_enum ng_type =
  74. SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt);
  75. static const char * const adcpol_txt[] = { "Normal", "Invert" };
  76. static const struct soc_enum adcpol =
  77. SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt);
  78. static const char *const dacpol_txt[] =
  79. { "Normal", "R Invert", "L Invert", "L + R Invert" };
  80. static const struct soc_enum dacpol =
  81. SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt);
  82. static const struct snd_kcontrol_new es8316_snd_controls[] = {
  83. SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
  84. 4, 0, 3, 1, hpout_vol_tlv),
  85. SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
  86. 4, 0, 11, 0, hpmixer_gain_tlv),
  87. SOC_ENUM("Playback Polarity", dacpol),
  88. SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
  89. ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv),
  90. SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1),
  91. SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0),
  92. SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
  93. SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
  94. SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
  95. SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0),
  96. SOC_ENUM("Capture Polarity", adcpol),
  97. SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
  98. SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME,
  99. 0, 0xc0, 1, adc_vol_tlv),
  100. SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN,
  101. 4, 10, 0, adc_pga_gain_tlv),
  102. SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0),
  103. SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0),
  104. SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0),
  105. SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0,
  106. alc_max_gain_tlv),
  107. SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
  108. alc_min_gain_tlv),
  109. SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 11, 0,
  110. alc_target_tlv),
  111. SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0),
  112. SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0),
  113. SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0),
  114. SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG,
  115. 5, 1, 0),
  116. SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
  117. 0, 31, 0),
  118. SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
  119. };
  120. /* Analog Input Mux */
  121. static const char * const es8316_analog_in_txt[] = {
  122. "lin1-rin1",
  123. "lin2-rin2",
  124. "lin1-rin1 with 20db Boost",
  125. "lin2-rin2 with 20db Boost"
  126. };
  127. static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 };
  128. static const struct soc_enum es8316_analog_input_enum =
  129. SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3,
  130. ARRAY_SIZE(es8316_analog_in_txt),
  131. es8316_analog_in_txt,
  132. es8316_analog_in_values);
  133. static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
  134. SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
  135. static const char * const es8316_dmic_txt[] = {
  136. "dmic disable",
  137. "dmic data at high level",
  138. "dmic data at low level",
  139. };
  140. static const unsigned int es8316_dmic_values[] = { 0, 2, 3 };
  141. static const struct soc_enum es8316_dmic_src_enum =
  142. SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3,
  143. ARRAY_SIZE(es8316_dmic_txt),
  144. es8316_dmic_txt,
  145. es8316_dmic_values);
  146. static const struct snd_kcontrol_new es8316_dmic_src_controls =
  147. SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
  148. /* hp mixer mux */
  149. static const char * const es8316_hpmux_texts[] = {
  150. "lin1-rin1",
  151. "lin2-rin2",
  152. "lin-rin with Boost",
  153. "lin-rin with Boost and PGA"
  154. };
  155. static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
  156. 4, es8316_hpmux_texts);
  157. static const struct snd_kcontrol_new es8316_left_hpmux_controls =
  158. SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
  159. static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
  160. 0, es8316_hpmux_texts);
  161. static const struct snd_kcontrol_new es8316_right_hpmux_controls =
  162. SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
  163. /* headphone Output Mixer */
  164. static const struct snd_kcontrol_new es8316_out_left_mix[] = {
  165. SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0),
  166. SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0),
  167. };
  168. static const struct snd_kcontrol_new es8316_out_right_mix[] = {
  169. SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0),
  170. SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0),
  171. };
  172. /* DAC data source mux */
  173. static const char * const es8316_dacsrc_texts[] = {
  174. "LDATA TO LDAC, RDATA TO RDAC",
  175. "LDATA TO LDAC, LDATA TO RDAC",
  176. "RDATA TO LDAC, RDATA TO RDAC",
  177. "RDATA TO LDAC, LDATA TO RDAC",
  178. };
  179. static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
  180. 6, es8316_dacsrc_texts);
  181. static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
  182. SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
  183. static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
  184. SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0),
  185. SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0),
  186. SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0),
  187. SND_SOC_DAPM_INPUT("DMIC"),
  188. SND_SOC_DAPM_INPUT("MIC1"),
  189. SND_SOC_DAPM_INPUT("MIC2"),
  190. /* Input Mux */
  191. SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
  192. &es8316_analog_in_mux_controls),
  193. SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0),
  194. SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0),
  195. SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0),
  196. SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL,
  197. 7, 1, NULL, 0),
  198. SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1),
  199. SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
  200. &es8316_dmic_src_controls),
  201. /* Digital Interface */
  202. SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 1,
  203. ES8316_SERDATA_ADC, 6, 1),
  204. SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
  205. SND_SOC_NOPM, 0, 0),
  206. SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
  207. &es8316_dacsrc_mux_controls),
  208. SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0),
  209. SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0),
  210. SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1),
  211. SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1),
  212. /* Headphone Output Side */
  213. SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
  214. &es8316_left_hpmux_controls),
  215. SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
  216. &es8316_right_hpmux_controls),
  217. SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN,
  218. 5, 1, &es8316_out_left_mix[0],
  219. ARRAY_SIZE(es8316_out_left_mix)),
  220. SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN,
  221. 1, 1, &es8316_out_right_mix[0],
  222. ARRAY_SIZE(es8316_out_right_mix)),
  223. SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN,
  224. 4, 1, NULL, 0),
  225. SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
  226. 0, 1, NULL, 0),
  227. SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
  228. 6, 0, NULL, 0),
  229. SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
  230. 2, 0, NULL, 0),
  231. SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
  232. 5, 1, NULL, 0),
  233. SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
  234. 4, 0, NULL, 0),
  235. SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
  236. 5, 0, NULL, 0),
  237. SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
  238. 1, 0, NULL, 0),
  239. SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
  240. /* pdn_Lical and pdn_Rical bits are documented as Reserved, but must
  241. * be explicitly unset in order to enable HP output
  242. */
  243. SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
  244. 7, 1, NULL, 0),
  245. SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
  246. 3, 1, NULL, 0),
  247. SND_SOC_DAPM_OUTPUT("HPOL"),
  248. SND_SOC_DAPM_OUTPUT("HPOR"),
  249. };
  250. static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
  251. /* Recording */
  252. {"MIC1", NULL, "Mic Bias"},
  253. {"MIC2", NULL, "Mic Bias"},
  254. {"MIC1", NULL, "Bias"},
  255. {"MIC2", NULL, "Bias"},
  256. {"MIC1", NULL, "Analog power"},
  257. {"MIC2", NULL, "Analog power"},
  258. {"Differential Mux", "lin1-rin1", "MIC1"},
  259. {"Differential Mux", "lin2-rin2", "MIC2"},
  260. {"Line input PGA", NULL, "Differential Mux"},
  261. {"Mono ADC", NULL, "ADC Clock"},
  262. {"Mono ADC", NULL, "ADC Vref"},
  263. {"Mono ADC", NULL, "ADC bias"},
  264. {"Mono ADC", NULL, "Line input PGA"},
  265. /* It's not clear why, but to avoid recording only silence,
  266. * the DAC clock must be running for the ADC to work.
  267. */
  268. {"Mono ADC", NULL, "DAC Clock"},
  269. {"Digital Mic Mux", "dmic disable", "Mono ADC"},
  270. {"I2S OUT", NULL, "Digital Mic Mux"},
  271. /* Playback */
  272. {"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
  273. {"Left DAC", NULL, "DAC Clock"},
  274. {"Right DAC", NULL, "DAC Clock"},
  275. {"Left DAC", NULL, "DAC Vref"},
  276. {"Right DAC", NULL, "DAC Vref"},
  277. {"Left DAC", NULL, "DAC Source Mux"},
  278. {"Right DAC", NULL, "DAC Source Mux"},
  279. {"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
  280. {"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
  281. {"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
  282. {"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
  283. {"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
  284. {"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
  285. {"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
  286. {"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
  287. {"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
  288. {"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
  289. {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
  290. {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
  291. {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
  292. {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
  293. {"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
  294. {"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
  295. {"HPOL", NULL, "Left Headphone Driver"},
  296. {"HPOR", NULL, "Right Headphone Driver"},
  297. {"HPOL", NULL, "Left Headphone ical"},
  298. {"HPOR", NULL, "Right Headphone ical"},
  299. {"Headphone Out", NULL, "Bias"},
  300. {"Headphone Out", NULL, "Analog power"},
  301. {"HPOL", NULL, "Headphone Out"},
  302. {"HPOR", NULL, "Headphone Out"},
  303. };
  304. static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  305. int clk_id, unsigned int freq, int dir)
  306. {
  307. struct snd_soc_component *component = codec_dai->component;
  308. struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
  309. int i, ret;
  310. int count = 0;
  311. es8316->sysclk = freq;
  312. es8316->sysclk_constraints.list = NULL;
  313. es8316->sysclk_constraints.count = 0;
  314. if (freq == 0)
  315. return 0;
  316. ret = clk_set_rate(es8316->mclk, freq);
  317. if (ret)
  318. return ret;
  319. /* Limit supported sample rates to ones that can be autodetected
  320. * by the codec running in slave mode.
  321. */
  322. for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
  323. const unsigned int ratio = supported_mclk_lrck_ratios[i];
  324. if (freq % ratio == 0)
  325. es8316->allowed_rates[count++] = freq / ratio;
  326. }
  327. if (count) {
  328. es8316->sysclk_constraints.list = es8316->allowed_rates;
  329. es8316->sysclk_constraints.count = count;
  330. }
  331. return 0;
  332. }
  333. static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
  334. unsigned int fmt)
  335. {
  336. struct snd_soc_component *component = codec_dai->component;
  337. u8 serdata1 = 0;
  338. u8 serdata2 = 0;
  339. u8 clksw;
  340. u8 mask;
  341. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBP_CFP)
  342. serdata1 |= ES8316_SERDATA1_MASTER;
  343. if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
  344. dev_err(component->dev, "Codec driver only supports I2S format\n");
  345. return -EINVAL;
  346. }
  347. /* Clock inversion */
  348. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  349. case SND_SOC_DAIFMT_NB_NF:
  350. break;
  351. case SND_SOC_DAIFMT_IB_IF:
  352. serdata1 |= ES8316_SERDATA1_BCLK_INV;
  353. serdata2 |= ES8316_SERDATA2_ADCLRP;
  354. break;
  355. case SND_SOC_DAIFMT_IB_NF:
  356. serdata1 |= ES8316_SERDATA1_BCLK_INV;
  357. break;
  358. case SND_SOC_DAIFMT_NB_IF:
  359. serdata2 |= ES8316_SERDATA2_ADCLRP;
  360. break;
  361. default:
  362. return -EINVAL;
  363. }
  364. mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
  365. snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
  366. mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
  367. snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
  368. snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
  369. /* Enable BCLK and MCLK inputs in slave mode */
  370. clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
  371. snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
  372. return 0;
  373. }
  374. static int es8316_pcm_startup(struct snd_pcm_substream *substream,
  375. struct snd_soc_dai *dai)
  376. {
  377. struct snd_soc_component *component = dai->component;
  378. struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
  379. if (es8316->sysclk_constraints.list)
  380. snd_pcm_hw_constraint_list(substream->runtime, 0,
  381. SNDRV_PCM_HW_PARAM_RATE,
  382. &es8316->sysclk_constraints);
  383. return 0;
  384. }
  385. static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
  386. struct snd_pcm_hw_params *params,
  387. struct snd_soc_dai *dai)
  388. {
  389. struct snd_soc_component *component = dai->component;
  390. struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
  391. u8 wordlen = 0;
  392. u8 bclk_divider;
  393. u16 lrck_divider;
  394. int i;
  395. /* Validate supported sample rates that are autodetected from MCLK */
  396. for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
  397. const unsigned int ratio = supported_mclk_lrck_ratios[i];
  398. if (es8316->sysclk % ratio != 0)
  399. continue;
  400. if (es8316->sysclk / ratio == params_rate(params))
  401. break;
  402. }
  403. if (i == NR_SUPPORTED_MCLK_LRCK_RATIOS)
  404. return -EINVAL;
  405. lrck_divider = es8316->sysclk / params_rate(params);
  406. bclk_divider = lrck_divider / 4;
  407. switch (params_format(params)) {
  408. case SNDRV_PCM_FORMAT_S16_LE:
  409. wordlen = ES8316_SERDATA2_LEN_16;
  410. bclk_divider /= 16;
  411. break;
  412. case SNDRV_PCM_FORMAT_S20_3LE:
  413. wordlen = ES8316_SERDATA2_LEN_20;
  414. bclk_divider /= 20;
  415. break;
  416. case SNDRV_PCM_FORMAT_S24_LE:
  417. wordlen = ES8316_SERDATA2_LEN_24;
  418. bclk_divider /= 24;
  419. break;
  420. case SNDRV_PCM_FORMAT_S32_LE:
  421. wordlen = ES8316_SERDATA2_LEN_32;
  422. bclk_divider /= 32;
  423. break;
  424. default:
  425. return -EINVAL;
  426. }
  427. snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
  428. ES8316_SERDATA2_LEN_MASK, wordlen);
  429. snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
  430. ES8316_SERDATA2_LEN_MASK, wordlen);
  431. snd_soc_component_update_bits(component, ES8316_SERDATA1, 0x1f, bclk_divider);
  432. snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV1, 0x0f, lrck_divider >> 8);
  433. snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV2, 0xff, lrck_divider & 0xff);
  434. snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV1, 0x0f, lrck_divider >> 8);
  435. snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV2, 0xff, lrck_divider & 0xff);
  436. return 0;
  437. }
  438. static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction)
  439. {
  440. snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
  441. mute ? 0x20 : 0);
  442. return 0;
  443. }
  444. #define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  445. SNDRV_PCM_FMTBIT_S24_LE)
  446. static const struct snd_soc_dai_ops es8316_ops = {
  447. .startup = es8316_pcm_startup,
  448. .hw_params = es8316_pcm_hw_params,
  449. .set_fmt = es8316_set_dai_fmt,
  450. .set_sysclk = es8316_set_dai_sysclk,
  451. .mute_stream = es8316_mute,
  452. .no_capture_mute = 1,
  453. };
  454. static struct snd_soc_dai_driver es8316_dai = {
  455. .name = "ES8316 HiFi",
  456. .playback = {
  457. .stream_name = "Playback",
  458. .channels_min = 1,
  459. .channels_max = 2,
  460. .rates = SNDRV_PCM_RATE_8000_48000,
  461. .formats = ES8316_FORMATS,
  462. },
  463. .capture = {
  464. .stream_name = "Capture",
  465. .channels_min = 1,
  466. .channels_max = 2,
  467. .rates = SNDRV_PCM_RATE_8000_48000,
  468. .formats = ES8316_FORMATS,
  469. },
  470. .ops = &es8316_ops,
  471. .symmetric_rate = 1,
  472. };
  473. static void es8316_enable_micbias_for_mic_gnd_short_detect(
  474. struct snd_soc_component *component)
  475. {
  476. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  477. snd_soc_dapm_mutex_lock(dapm);
  478. snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias");
  479. snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power");
  480. snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias");
  481. snd_soc_dapm_sync_unlocked(dapm);
  482. snd_soc_dapm_mutex_unlock(dapm);
  483. msleep(20);
  484. }
  485. static void es8316_disable_micbias_for_mic_gnd_short_detect(
  486. struct snd_soc_component *component)
  487. {
  488. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  489. snd_soc_dapm_mutex_lock(dapm);
  490. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias");
  491. snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power");
  492. snd_soc_dapm_disable_pin_unlocked(dapm, "Bias");
  493. snd_soc_dapm_sync_unlocked(dapm);
  494. snd_soc_dapm_mutex_unlock(dapm);
  495. }
  496. static irqreturn_t es8316_irq(int irq, void *data)
  497. {
  498. struct es8316_priv *es8316 = data;
  499. struct snd_soc_component *comp = es8316->component;
  500. unsigned int flags;
  501. mutex_lock(&es8316->lock);
  502. regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
  503. if (flags == 0x00)
  504. goto out; /* Powered-down / reset */
  505. /* Catch spurious IRQ before set_jack is called */
  506. if (!es8316->jack)
  507. goto out;
  508. if (es8316->jd_inverted)
  509. flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
  510. dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
  511. if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
  512. /* Jack removed, or spurious IRQ? */
  513. if (es8316->jack->status & SND_JACK_MICROPHONE)
  514. es8316_disable_micbias_for_mic_gnd_short_detect(comp);
  515. if (es8316->jack->status & SND_JACK_HEADPHONE) {
  516. snd_soc_jack_report(es8316->jack, 0,
  517. SND_JACK_HEADSET | SND_JACK_BTN_0);
  518. dev_dbg(comp->dev, "jack unplugged\n");
  519. }
  520. } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) {
  521. /* Jack inserted, determine type */
  522. es8316_enable_micbias_for_mic_gnd_short_detect(comp);
  523. regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
  524. if (es8316->jd_inverted)
  525. flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
  526. dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
  527. if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
  528. /* Jack unplugged underneath us */
  529. es8316_disable_micbias_for_mic_gnd_short_detect(comp);
  530. } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
  531. /* Open, headset */
  532. snd_soc_jack_report(es8316->jack,
  533. SND_JACK_HEADSET,
  534. SND_JACK_HEADSET);
  535. /* Keep mic-gnd-short detection on for button press */
  536. } else {
  537. /* Shorted, headphones */
  538. snd_soc_jack_report(es8316->jack,
  539. SND_JACK_HEADPHONE,
  540. SND_JACK_HEADSET);
  541. /* No longer need mic-gnd-short detection */
  542. es8316_disable_micbias_for_mic_gnd_short_detect(comp);
  543. }
  544. } else if (es8316->jack->status & SND_JACK_MICROPHONE) {
  545. /* Interrupt while jack inserted, report button state */
  546. if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
  547. /* Open, button release */
  548. snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
  549. } else {
  550. /* Short, button press */
  551. snd_soc_jack_report(es8316->jack,
  552. SND_JACK_BTN_0,
  553. SND_JACK_BTN_0);
  554. }
  555. }
  556. out:
  557. mutex_unlock(&es8316->lock);
  558. return IRQ_HANDLED;
  559. }
  560. static void es8316_enable_jack_detect(struct snd_soc_component *component,
  561. struct snd_soc_jack *jack)
  562. {
  563. struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
  564. /*
  565. * Init es8316->jd_inverted here and not in the probe, as we cannot
  566. * guarantee that the bytchr-es8316 driver, which might set this
  567. * property, will probe before us.
  568. */
  569. es8316->jd_inverted = device_property_read_bool(component->dev,
  570. "everest,jack-detect-inverted");
  571. mutex_lock(&es8316->lock);
  572. es8316->jack = jack;
  573. if (es8316->jack->status & SND_JACK_MICROPHONE)
  574. es8316_enable_micbias_for_mic_gnd_short_detect(component);
  575. snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
  576. ES8316_GPIO_ENABLE_INTERRUPT,
  577. ES8316_GPIO_ENABLE_INTERRUPT);
  578. mutex_unlock(&es8316->lock);
  579. /* Enable irq and sync initial jack state */
  580. enable_irq(es8316->irq);
  581. es8316_irq(es8316->irq, es8316);
  582. }
  583. static void es8316_disable_jack_detect(struct snd_soc_component *component)
  584. {
  585. struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
  586. if (!es8316->jack)
  587. return; /* Already disabled (or never enabled) */
  588. disable_irq(es8316->irq);
  589. mutex_lock(&es8316->lock);
  590. snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
  591. ES8316_GPIO_ENABLE_INTERRUPT, 0);
  592. if (es8316->jack->status & SND_JACK_MICROPHONE) {
  593. es8316_disable_micbias_for_mic_gnd_short_detect(component);
  594. snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
  595. }
  596. es8316->jack = NULL;
  597. mutex_unlock(&es8316->lock);
  598. }
  599. static int es8316_set_jack(struct snd_soc_component *component,
  600. struct snd_soc_jack *jack, void *data)
  601. {
  602. if (jack)
  603. es8316_enable_jack_detect(component, jack);
  604. else
  605. es8316_disable_jack_detect(component);
  606. return 0;
  607. }
  608. static int es8316_probe(struct snd_soc_component *component)
  609. {
  610. struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
  611. int ret;
  612. es8316->component = component;
  613. es8316->mclk = devm_clk_get_optional(component->dev, "mclk");
  614. if (IS_ERR(es8316->mclk)) {
  615. dev_err(component->dev, "unable to get mclk\n");
  616. return PTR_ERR(es8316->mclk);
  617. }
  618. if (!es8316->mclk)
  619. dev_warn(component->dev, "assuming static mclk\n");
  620. ret = clk_prepare_enable(es8316->mclk);
  621. if (ret) {
  622. dev_err(component->dev, "unable to enable mclk\n");
  623. return ret;
  624. }
  625. /* Reset codec and enable current state machine */
  626. snd_soc_component_write(component, ES8316_RESET, 0x3f);
  627. usleep_range(5000, 5500);
  628. snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
  629. msleep(30);
  630. /*
  631. * Documentation is unclear, but this value from the vendor driver is
  632. * needed otherwise audio output is silent.
  633. */
  634. snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
  635. /*
  636. * Documentation for this register is unclear and incomplete,
  637. * but here is a vendor-provided value that improves volume
  638. * and quality for Intel CHT platforms.
  639. */
  640. snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
  641. return 0;
  642. }
  643. static void es8316_remove(struct snd_soc_component *component)
  644. {
  645. struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
  646. clk_disable_unprepare(es8316->mclk);
  647. }
  648. static int es8316_resume(struct snd_soc_component *component)
  649. {
  650. struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
  651. regcache_cache_only(es8316->regmap, false);
  652. regcache_sync(es8316->regmap);
  653. return 0;
  654. }
  655. static int es8316_suspend(struct snd_soc_component *component)
  656. {
  657. struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
  658. regcache_cache_only(es8316->regmap, true);
  659. regcache_mark_dirty(es8316->regmap);
  660. return 0;
  661. }
  662. static const struct snd_soc_component_driver soc_component_dev_es8316 = {
  663. .probe = es8316_probe,
  664. .remove = es8316_remove,
  665. .resume = es8316_resume,
  666. .suspend = es8316_suspend,
  667. .set_jack = es8316_set_jack,
  668. .controls = es8316_snd_controls,
  669. .num_controls = ARRAY_SIZE(es8316_snd_controls),
  670. .dapm_widgets = es8316_dapm_widgets,
  671. .num_dapm_widgets = ARRAY_SIZE(es8316_dapm_widgets),
  672. .dapm_routes = es8316_dapm_routes,
  673. .num_dapm_routes = ARRAY_SIZE(es8316_dapm_routes),
  674. .use_pmdown_time = 1,
  675. .endianness = 1,
  676. };
  677. static const struct regmap_range es8316_volatile_ranges[] = {
  678. regmap_reg_range(ES8316_GPIO_FLAG, ES8316_GPIO_FLAG),
  679. };
  680. static const struct regmap_access_table es8316_volatile_table = {
  681. .yes_ranges = es8316_volatile_ranges,
  682. .n_yes_ranges = ARRAY_SIZE(es8316_volatile_ranges),
  683. };
  684. static const struct regmap_config es8316_regmap = {
  685. .reg_bits = 8,
  686. .val_bits = 8,
  687. .use_single_read = true,
  688. .use_single_write = true,
  689. .max_register = 0x53,
  690. .volatile_table = &es8316_volatile_table,
  691. .cache_type = REGCACHE_RBTREE,
  692. };
  693. static int es8316_i2c_probe(struct i2c_client *i2c_client)
  694. {
  695. struct device *dev = &i2c_client->dev;
  696. struct es8316_priv *es8316;
  697. int ret;
  698. es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
  699. GFP_KERNEL);
  700. if (es8316 == NULL)
  701. return -ENOMEM;
  702. i2c_set_clientdata(i2c_client, es8316);
  703. es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
  704. if (IS_ERR(es8316->regmap))
  705. return PTR_ERR(es8316->regmap);
  706. es8316->irq = i2c_client->irq;
  707. mutex_init(&es8316->lock);
  708. if (es8316->irq > 0) {
  709. ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq,
  710. IRQF_TRIGGER_HIGH | IRQF_ONESHOT | IRQF_NO_AUTOEN,
  711. "es8316", es8316);
  712. if (ret) {
  713. dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret);
  714. es8316->irq = -ENXIO;
  715. }
  716. }
  717. return devm_snd_soc_register_component(&i2c_client->dev,
  718. &soc_component_dev_es8316,
  719. &es8316_dai, 1);
  720. }
  721. static const struct i2c_device_id es8316_i2c_id[] = {
  722. {"es8316", 0 },
  723. {}
  724. };
  725. MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
  726. #ifdef CONFIG_OF
  727. static const struct of_device_id es8316_of_match[] = {
  728. { .compatible = "everest,es8316", },
  729. {},
  730. };
  731. MODULE_DEVICE_TABLE(of, es8316_of_match);
  732. #endif
  733. #ifdef CONFIG_ACPI
  734. static const struct acpi_device_id es8316_acpi_match[] = {
  735. {"ESSX8316", 0},
  736. {"ESSX8336", 0},
  737. {},
  738. };
  739. MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
  740. #endif
  741. static struct i2c_driver es8316_i2c_driver = {
  742. .driver = {
  743. .name = "es8316",
  744. .acpi_match_table = ACPI_PTR(es8316_acpi_match),
  745. .of_match_table = of_match_ptr(es8316_of_match),
  746. },
  747. .probe_new = es8316_i2c_probe,
  748. .id_table = es8316_i2c_id,
  749. };
  750. module_i2c_driver(es8316_i2c_driver);
  751. MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
  752. MODULE_AUTHOR("David Yang <[email protected]>");
  753. MODULE_LICENSE("GPL v2");