es8328.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * es8328.c -- ES8328 ALSA SoC Audio driver
  3. *
  4. * Copyright 2014 Sutajio Ko-Usagi PTE LTD
  5. *
  6. * Author: Sean Cross <xobs@kosagi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/of_device.h>
  15. #include <linux/module.h>
  16. #include <linux/pm.h>
  17. #include <linux/regmap.h>
  18. #include <linux/slab.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <sound/core.h>
  21. #include <sound/initval.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/tlv.h>
  26. #include "es8328.h"
  27. static const unsigned int rates_12288[] = {
  28. 8000, 12000, 16000, 24000, 32000, 48000, 96000,
  29. };
  30. static const int ratios_12288[] = {
  31. 10, 7, 6, 4, 3, 2, 0,
  32. };
  33. static const struct snd_pcm_hw_constraint_list constraints_12288 = {
  34. .count = ARRAY_SIZE(rates_12288),
  35. .list = rates_12288,
  36. };
  37. static const unsigned int rates_11289[] = {
  38. 8018, 11025, 22050, 44100, 88200,
  39. };
  40. static const int ratios_11289[] = {
  41. 9, 7, 4, 2, 0,
  42. };
  43. static const struct snd_pcm_hw_constraint_list constraints_11289 = {
  44. .count = ARRAY_SIZE(rates_11289),
  45. .list = rates_11289,
  46. };
  47. /* regulator supplies for sgtl5000, VDDD is an optional external supply */
  48. enum sgtl5000_regulator_supplies {
  49. DVDD,
  50. AVDD,
  51. PVDD,
  52. HPVDD,
  53. ES8328_SUPPLY_NUM
  54. };
  55. /* vddd is optional supply */
  56. static const char * const supply_names[ES8328_SUPPLY_NUM] = {
  57. "DVDD",
  58. "AVDD",
  59. "PVDD",
  60. "HPVDD",
  61. };
  62. #define ES8328_RATES (SNDRV_PCM_RATE_96000 | \
  63. SNDRV_PCM_RATE_48000 | \
  64. SNDRV_PCM_RATE_44100 | \
  65. SNDRV_PCM_RATE_32000 | \
  66. SNDRV_PCM_RATE_22050 | \
  67. SNDRV_PCM_RATE_16000 | \
  68. SNDRV_PCM_RATE_11025 | \
  69. SNDRV_PCM_RATE_8000)
  70. #define ES8328_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  71. SNDRV_PCM_FMTBIT_S18_3LE | \
  72. SNDRV_PCM_FMTBIT_S20_3LE | \
  73. SNDRV_PCM_FMTBIT_S24_LE | \
  74. SNDRV_PCM_FMTBIT_S32_LE)
  75. struct es8328_priv {
  76. struct regmap *regmap;
  77. struct clk *clk;
  78. int playback_fs;
  79. bool deemph;
  80. int mclkdiv2;
  81. const struct snd_pcm_hw_constraint_list *sysclk_constraints;
  82. const int *mclk_ratios;
  83. struct regulator_bulk_data supplies[ES8328_SUPPLY_NUM];
  84. };
  85. /*
  86. * ES8328 Controls
  87. */
  88. static const char * const adcpol_txt[] = {"Normal", "L Invert", "R Invert",
  89. "L + R Invert"};
  90. static SOC_ENUM_SINGLE_DECL(adcpol,
  91. ES8328_ADCCONTROL6, 6, adcpol_txt);
  92. static const DECLARE_TLV_DB_SCALE(play_tlv, -3000, 100, 0);
  93. static const DECLARE_TLV_DB_SCALE(dac_adc_tlv, -9600, 50, 0);
  94. static const DECLARE_TLV_DB_SCALE(pga_tlv, 0, 300, 0);
  95. static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
  96. static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 300, 0);
  97. static const struct {
  98. int rate;
  99. unsigned int val;
  100. } deemph_settings[] = {
  101. { 0, ES8328_DACCONTROL6_DEEMPH_OFF },
  102. { 32000, ES8328_DACCONTROL6_DEEMPH_32k },
  103. { 44100, ES8328_DACCONTROL6_DEEMPH_44_1k },
  104. { 48000, ES8328_DACCONTROL6_DEEMPH_48k },
  105. };
  106. static int es8328_set_deemph(struct snd_soc_codec *codec)
  107. {
  108. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  109. int val, i, best;
  110. /*
  111. * If we're using deemphasis select the nearest available sample
  112. * rate.
  113. */
  114. if (es8328->deemph) {
  115. best = 0;
  116. for (i = 1; i < ARRAY_SIZE(deemph_settings); i++) {
  117. if (abs(deemph_settings[i].rate - es8328->playback_fs) <
  118. abs(deemph_settings[best].rate - es8328->playback_fs))
  119. best = i;
  120. }
  121. val = deemph_settings[best].val;
  122. } else {
  123. val = ES8328_DACCONTROL6_DEEMPH_OFF;
  124. }
  125. dev_dbg(codec->dev, "Set deemphasis %d\n", val);
  126. return snd_soc_update_bits(codec, ES8328_DACCONTROL6,
  127. ES8328_DACCONTROL6_DEEMPH_MASK, val);
  128. }
  129. static int es8328_get_deemph(struct snd_kcontrol *kcontrol,
  130. struct snd_ctl_elem_value *ucontrol)
  131. {
  132. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  133. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  134. ucontrol->value.integer.value[0] = es8328->deemph;
  135. return 0;
  136. }
  137. static int es8328_put_deemph(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol)
  139. {
  140. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  141. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  142. unsigned int deemph = ucontrol->value.integer.value[0];
  143. int ret;
  144. if (deemph > 1)
  145. return -EINVAL;
  146. ret = es8328_set_deemph(codec);
  147. if (ret < 0)
  148. return ret;
  149. es8328->deemph = deemph;
  150. return 0;
  151. }
  152. static const struct snd_kcontrol_new es8328_snd_controls[] = {
  153. SOC_DOUBLE_R_TLV("Capture Digital Volume",
  154. ES8328_ADCCONTROL8, ES8328_ADCCONTROL9,
  155. 0, 0xc0, 1, dac_adc_tlv),
  156. SOC_SINGLE("Capture ZC Switch", ES8328_ADCCONTROL7, 6, 1, 0),
  157. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  158. es8328_get_deemph, es8328_put_deemph),
  159. SOC_ENUM("Capture Polarity", adcpol),
  160. SOC_SINGLE_TLV("Left Mixer Left Bypass Volume",
  161. ES8328_DACCONTROL17, 3, 7, 1, bypass_tlv),
  162. SOC_SINGLE_TLV("Left Mixer Right Bypass Volume",
  163. ES8328_DACCONTROL19, 3, 7, 1, bypass_tlv),
  164. SOC_SINGLE_TLV("Right Mixer Left Bypass Volume",
  165. ES8328_DACCONTROL18, 3, 7, 1, bypass_tlv),
  166. SOC_SINGLE_TLV("Right Mixer Right Bypass Volume",
  167. ES8328_DACCONTROL20, 3, 7, 1, bypass_tlv),
  168. SOC_DOUBLE_R_TLV("PCM Volume",
  169. ES8328_LDACVOL, ES8328_RDACVOL,
  170. 0, ES8328_DACVOL_MAX, 1, dac_adc_tlv),
  171. SOC_DOUBLE_R_TLV("Output 1 Playback Volume",
  172. ES8328_LOUT1VOL, ES8328_ROUT1VOL,
  173. 0, ES8328_OUT1VOL_MAX, 0, play_tlv),
  174. SOC_DOUBLE_R_TLV("Output 2 Playback Volume",
  175. ES8328_LOUT2VOL, ES8328_ROUT2VOL,
  176. 0, ES8328_OUT2VOL_MAX, 0, play_tlv),
  177. SOC_DOUBLE_TLV("Mic PGA Volume", ES8328_ADCCONTROL1,
  178. 4, 0, 8, 0, mic_tlv),
  179. };
  180. /*
  181. * DAPM Controls
  182. */
  183. static const char * const es8328_line_texts[] = {
  184. "Line 1", "Line 2", "PGA", "Differential"};
  185. static const struct soc_enum es8328_lline_enum =
  186. SOC_ENUM_SINGLE(ES8328_DACCONTROL16, 3,
  187. ARRAY_SIZE(es8328_line_texts),
  188. es8328_line_texts);
  189. static const struct snd_kcontrol_new es8328_left_line_controls =
  190. SOC_DAPM_ENUM("Route", es8328_lline_enum);
  191. static const struct soc_enum es8328_rline_enum =
  192. SOC_ENUM_SINGLE(ES8328_DACCONTROL16, 0,
  193. ARRAY_SIZE(es8328_line_texts),
  194. es8328_line_texts);
  195. static const struct snd_kcontrol_new es8328_right_line_controls =
  196. SOC_DAPM_ENUM("Route", es8328_lline_enum);
  197. /* Left Mixer */
  198. static const struct snd_kcontrol_new es8328_left_mixer_controls[] = {
  199. SOC_DAPM_SINGLE("Playback Switch", ES8328_DACCONTROL17, 7, 1, 0),
  200. SOC_DAPM_SINGLE("Left Bypass Switch", ES8328_DACCONTROL17, 6, 1, 0),
  201. SOC_DAPM_SINGLE("Right Playback Switch", ES8328_DACCONTROL18, 7, 1, 0),
  202. SOC_DAPM_SINGLE("Right Bypass Switch", ES8328_DACCONTROL18, 6, 1, 0),
  203. };
  204. /* Right Mixer */
  205. static const struct snd_kcontrol_new es8328_right_mixer_controls[] = {
  206. SOC_DAPM_SINGLE("Left Playback Switch", ES8328_DACCONTROL19, 7, 1, 0),
  207. SOC_DAPM_SINGLE("Left Bypass Switch", ES8328_DACCONTROL19, 6, 1, 0),
  208. SOC_DAPM_SINGLE("Playback Switch", ES8328_DACCONTROL20, 7, 1, 0),
  209. SOC_DAPM_SINGLE("Right Bypass Switch", ES8328_DACCONTROL20, 6, 1, 0),
  210. };
  211. static const char * const es8328_pga_sel[] = {
  212. "Line 1", "Line 2", "Line 3", "Differential"};
  213. /* Left PGA Mux */
  214. static const struct soc_enum es8328_lpga_enum =
  215. SOC_ENUM_SINGLE(ES8328_ADCCONTROL2, 6,
  216. ARRAY_SIZE(es8328_pga_sel),
  217. es8328_pga_sel);
  218. static const struct snd_kcontrol_new es8328_left_pga_controls =
  219. SOC_DAPM_ENUM("Route", es8328_lpga_enum);
  220. /* Right PGA Mux */
  221. static const struct soc_enum es8328_rpga_enum =
  222. SOC_ENUM_SINGLE(ES8328_ADCCONTROL2, 4,
  223. ARRAY_SIZE(es8328_pga_sel),
  224. es8328_pga_sel);
  225. static const struct snd_kcontrol_new es8328_right_pga_controls =
  226. SOC_DAPM_ENUM("Route", es8328_rpga_enum);
  227. /* Differential Mux */
  228. static const char * const es8328_diff_sel[] = {"Line 1", "Line 2"};
  229. static SOC_ENUM_SINGLE_DECL(diffmux,
  230. ES8328_ADCCONTROL3, 7, es8328_diff_sel);
  231. static const struct snd_kcontrol_new es8328_diffmux_controls =
  232. SOC_DAPM_ENUM("Route", diffmux);
  233. /* Mono ADC Mux */
  234. static const char * const es8328_mono_mux[] = {"Stereo", "Mono (Left)",
  235. "Mono (Right)", "Digital Mono"};
  236. static SOC_ENUM_SINGLE_DECL(monomux,
  237. ES8328_ADCCONTROL3, 3, es8328_mono_mux);
  238. static const struct snd_kcontrol_new es8328_monomux_controls =
  239. SOC_DAPM_ENUM("Route", monomux);
  240. static const struct snd_soc_dapm_widget es8328_dapm_widgets[] = {
  241. SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
  242. &es8328_diffmux_controls),
  243. SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
  244. &es8328_monomux_controls),
  245. SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
  246. &es8328_monomux_controls),
  247. SND_SOC_DAPM_MUX("Left PGA Mux", ES8328_ADCPOWER,
  248. ES8328_ADCPOWER_AINL_OFF, 1,
  249. &es8328_left_pga_controls),
  250. SND_SOC_DAPM_MUX("Right PGA Mux", ES8328_ADCPOWER,
  251. ES8328_ADCPOWER_AINR_OFF, 1,
  252. &es8328_right_pga_controls),
  253. SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
  254. &es8328_left_line_controls),
  255. SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
  256. &es8328_right_line_controls),
  257. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", ES8328_ADCPOWER,
  258. ES8328_ADCPOWER_ADCR_OFF, 1),
  259. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", ES8328_ADCPOWER,
  260. ES8328_ADCPOWER_ADCL_OFF, 1),
  261. SND_SOC_DAPM_SUPPLY("Mic Bias", ES8328_ADCPOWER,
  262. ES8328_ADCPOWER_MIC_BIAS_OFF, 1, NULL, 0),
  263. SND_SOC_DAPM_SUPPLY("Mic Bias Gen", ES8328_ADCPOWER,
  264. ES8328_ADCPOWER_ADC_BIAS_GEN_OFF, 1, NULL, 0),
  265. SND_SOC_DAPM_SUPPLY("DAC STM", ES8328_CHIPPOWER,
  266. ES8328_CHIPPOWER_DACSTM_RESET, 1, NULL, 0),
  267. SND_SOC_DAPM_SUPPLY("ADC STM", ES8328_CHIPPOWER,
  268. ES8328_CHIPPOWER_ADCSTM_RESET, 1, NULL, 0),
  269. SND_SOC_DAPM_SUPPLY("DAC DIG", ES8328_CHIPPOWER,
  270. ES8328_CHIPPOWER_DACDIG_OFF, 1, NULL, 0),
  271. SND_SOC_DAPM_SUPPLY("ADC DIG", ES8328_CHIPPOWER,
  272. ES8328_CHIPPOWER_ADCDIG_OFF, 1, NULL, 0),
  273. SND_SOC_DAPM_SUPPLY("DAC DLL", ES8328_CHIPPOWER,
  274. ES8328_CHIPPOWER_DACDLL_OFF, 1, NULL, 0),
  275. SND_SOC_DAPM_SUPPLY("ADC DLL", ES8328_CHIPPOWER,
  276. ES8328_CHIPPOWER_ADCDLL_OFF, 1, NULL, 0),
  277. SND_SOC_DAPM_SUPPLY("ADC Vref", ES8328_CHIPPOWER,
  278. ES8328_CHIPPOWER_ADCVREF_OFF, 1, NULL, 0),
  279. SND_SOC_DAPM_SUPPLY("DAC Vref", ES8328_CHIPPOWER,
  280. ES8328_CHIPPOWER_DACVREF_OFF, 1, NULL, 0),
  281. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", ES8328_DACPOWER,
  282. ES8328_DACPOWER_RDAC_OFF, 1),
  283. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", ES8328_DACPOWER,
  284. ES8328_DACPOWER_LDAC_OFF, 1),
  285. SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
  286. &es8328_left_mixer_controls[0],
  287. ARRAY_SIZE(es8328_left_mixer_controls)),
  288. SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
  289. &es8328_right_mixer_controls[0],
  290. ARRAY_SIZE(es8328_right_mixer_controls)),
  291. SND_SOC_DAPM_PGA("Right Out 2", ES8328_DACPOWER,
  292. ES8328_DACPOWER_ROUT2_ON, 0, NULL, 0),
  293. SND_SOC_DAPM_PGA("Left Out 2", ES8328_DACPOWER,
  294. ES8328_DACPOWER_LOUT2_ON, 0, NULL, 0),
  295. SND_SOC_DAPM_PGA("Right Out 1", ES8328_DACPOWER,
  296. ES8328_DACPOWER_ROUT1_ON, 0, NULL, 0),
  297. SND_SOC_DAPM_PGA("Left Out 1", ES8328_DACPOWER,
  298. ES8328_DACPOWER_LOUT1_ON, 0, NULL, 0),
  299. SND_SOC_DAPM_OUTPUT("LOUT1"),
  300. SND_SOC_DAPM_OUTPUT("ROUT1"),
  301. SND_SOC_DAPM_OUTPUT("LOUT2"),
  302. SND_SOC_DAPM_OUTPUT("ROUT2"),
  303. SND_SOC_DAPM_INPUT("LINPUT1"),
  304. SND_SOC_DAPM_INPUT("LINPUT2"),
  305. SND_SOC_DAPM_INPUT("RINPUT1"),
  306. SND_SOC_DAPM_INPUT("RINPUT2"),
  307. };
  308. static const struct snd_soc_dapm_route es8328_dapm_routes[] = {
  309. { "Left Line Mux", "Line 1", "LINPUT1" },
  310. { "Left Line Mux", "Line 2", "LINPUT2" },
  311. { "Left Line Mux", "PGA", "Left PGA Mux" },
  312. { "Left Line Mux", "Differential", "Differential Mux" },
  313. { "Right Line Mux", "Line 1", "RINPUT1" },
  314. { "Right Line Mux", "Line 2", "RINPUT2" },
  315. { "Right Line Mux", "PGA", "Right PGA Mux" },
  316. { "Right Line Mux", "Differential", "Differential Mux" },
  317. { "Left PGA Mux", "Line 1", "LINPUT1" },
  318. { "Left PGA Mux", "Line 2", "LINPUT2" },
  319. { "Left PGA Mux", "Differential", "Differential Mux" },
  320. { "Right PGA Mux", "Line 1", "RINPUT1" },
  321. { "Right PGA Mux", "Line 2", "RINPUT2" },
  322. { "Right PGA Mux", "Differential", "Differential Mux" },
  323. { "Differential Mux", "Line 1", "LINPUT1" },
  324. { "Differential Mux", "Line 1", "RINPUT1" },
  325. { "Differential Mux", "Line 2", "LINPUT2" },
  326. { "Differential Mux", "Line 2", "RINPUT2" },
  327. { "Left ADC Mux", "Stereo", "Left PGA Mux" },
  328. { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
  329. { "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
  330. { "Right ADC Mux", "Stereo", "Right PGA Mux" },
  331. { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
  332. { "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
  333. { "Left ADC", NULL, "Left ADC Mux" },
  334. { "Right ADC", NULL, "Right ADC Mux" },
  335. { "ADC DIG", NULL, "ADC STM" },
  336. { "ADC DIG", NULL, "ADC Vref" },
  337. { "ADC DIG", NULL, "ADC DLL" },
  338. { "Left ADC", NULL, "ADC DIG" },
  339. { "Right ADC", NULL, "ADC DIG" },
  340. { "Mic Bias", NULL, "Mic Bias Gen" },
  341. { "Left Line Mux", "Line 1", "LINPUT1" },
  342. { "Left Line Mux", "Line 2", "LINPUT2" },
  343. { "Left Line Mux", "PGA", "Left PGA Mux" },
  344. { "Left Line Mux", "Differential", "Differential Mux" },
  345. { "Right Line Mux", "Line 1", "RINPUT1" },
  346. { "Right Line Mux", "Line 2", "RINPUT2" },
  347. { "Right Line Mux", "PGA", "Right PGA Mux" },
  348. { "Right Line Mux", "Differential", "Differential Mux" },
  349. { "Left Out 1", NULL, "Left DAC" },
  350. { "Right Out 1", NULL, "Right DAC" },
  351. { "Left Out 2", NULL, "Left DAC" },
  352. { "Right Out 2", NULL, "Right DAC" },
  353. { "Left Mixer", "Playback Switch", "Left DAC" },
  354. { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
  355. { "Left Mixer", "Right Playback Switch", "Right DAC" },
  356. { "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
  357. { "Right Mixer", "Left Playback Switch", "Left DAC" },
  358. { "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
  359. { "Right Mixer", "Playback Switch", "Right DAC" },
  360. { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
  361. { "DAC DIG", NULL, "DAC STM" },
  362. { "DAC DIG", NULL, "DAC Vref" },
  363. { "DAC DIG", NULL, "DAC DLL" },
  364. { "Left DAC", NULL, "DAC DIG" },
  365. { "Right DAC", NULL, "DAC DIG" },
  366. { "Left Out 1", NULL, "Left Mixer" },
  367. { "LOUT1", NULL, "Left Out 1" },
  368. { "Right Out 1", NULL, "Right Mixer" },
  369. { "ROUT1", NULL, "Right Out 1" },
  370. { "Left Out 2", NULL, "Left Mixer" },
  371. { "LOUT2", NULL, "Left Out 2" },
  372. { "Right Out 2", NULL, "Right Mixer" },
  373. { "ROUT2", NULL, "Right Out 2" },
  374. };
  375. static int es8328_mute(struct snd_soc_dai *dai, int mute)
  376. {
  377. return snd_soc_update_bits(dai->codec, ES8328_DACCONTROL3,
  378. ES8328_DACCONTROL3_DACMUTE,
  379. mute ? ES8328_DACCONTROL3_DACMUTE : 0);
  380. }
  381. static int es8328_startup(struct snd_pcm_substream *substream,
  382. struct snd_soc_dai *dai)
  383. {
  384. struct snd_soc_codec *codec = dai->codec;
  385. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  386. if (es8328->sysclk_constraints)
  387. snd_pcm_hw_constraint_list(substream->runtime, 0,
  388. SNDRV_PCM_HW_PARAM_RATE,
  389. es8328->sysclk_constraints);
  390. return 0;
  391. }
  392. static int es8328_hw_params(struct snd_pcm_substream *substream,
  393. struct snd_pcm_hw_params *params,
  394. struct snd_soc_dai *dai)
  395. {
  396. struct snd_soc_codec *codec = dai->codec;
  397. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  398. int i;
  399. int reg;
  400. int wl;
  401. int ratio;
  402. if (!es8328->sysclk_constraints) {
  403. dev_err(codec->dev, "No MCLK configured\n");
  404. return -EINVAL;
  405. }
  406. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  407. reg = ES8328_DACCONTROL2;
  408. else
  409. reg = ES8328_ADCCONTROL5;
  410. for (i = 0; i < es8328->sysclk_constraints->count; i++)
  411. if (es8328->sysclk_constraints->list[i] == params_rate(params))
  412. break;
  413. if (i == es8328->sysclk_constraints->count) {
  414. dev_err(codec->dev, "LRCLK %d unsupported with current clock\n",
  415. params_rate(params));
  416. return -EINVAL;
  417. }
  418. ratio = es8328->mclk_ratios[i];
  419. snd_soc_update_bits(codec, ES8328_MASTERMODE,
  420. ES8328_MASTERMODE_MCLKDIV2,
  421. es8328->mclkdiv2 ? ES8328_MASTERMODE_MCLKDIV2 : 0);
  422. switch (params_width(params)) {
  423. case 16:
  424. wl = 3;
  425. break;
  426. case 18:
  427. wl = 2;
  428. break;
  429. case 20:
  430. wl = 1;
  431. break;
  432. case 24:
  433. wl = 0;
  434. break;
  435. case 32:
  436. wl = 4;
  437. break;
  438. default:
  439. return -EINVAL;
  440. }
  441. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  442. snd_soc_update_bits(codec, ES8328_DACCONTROL1,
  443. ES8328_DACCONTROL1_DACWL_MASK,
  444. wl << ES8328_DACCONTROL1_DACWL_SHIFT);
  445. es8328->playback_fs = params_rate(params);
  446. es8328_set_deemph(codec);
  447. } else
  448. snd_soc_update_bits(codec, ES8328_ADCCONTROL4,
  449. ES8328_ADCCONTROL4_ADCWL_MASK,
  450. wl << ES8328_ADCCONTROL4_ADCWL_SHIFT);
  451. return snd_soc_update_bits(codec, reg, ES8328_RATEMASK, ratio);
  452. }
  453. static int es8328_set_sysclk(struct snd_soc_dai *codec_dai,
  454. int clk_id, unsigned int freq, int dir)
  455. {
  456. struct snd_soc_codec *codec = codec_dai->codec;
  457. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  458. int mclkdiv2 = 0;
  459. switch (freq) {
  460. case 0:
  461. es8328->sysclk_constraints = NULL;
  462. es8328->mclk_ratios = NULL;
  463. break;
  464. case 22579200:
  465. mclkdiv2 = 1;
  466. /* fallthru */
  467. case 11289600:
  468. es8328->sysclk_constraints = &constraints_11289;
  469. es8328->mclk_ratios = ratios_11289;
  470. break;
  471. case 24576000:
  472. mclkdiv2 = 1;
  473. /* fallthru */
  474. case 12288000:
  475. es8328->sysclk_constraints = &constraints_12288;
  476. es8328->mclk_ratios = ratios_12288;
  477. break;
  478. default:
  479. return -EINVAL;
  480. }
  481. es8328->mclkdiv2 = mclkdiv2;
  482. return 0;
  483. }
  484. static int es8328_set_dai_fmt(struct snd_soc_dai *codec_dai,
  485. unsigned int fmt)
  486. {
  487. struct snd_soc_codec *codec = codec_dai->codec;
  488. u8 dac_mode = 0;
  489. u8 adc_mode = 0;
  490. /* set master/slave audio interface */
  491. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBM_CFM)
  492. return -EINVAL;
  493. /* interface format */
  494. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  495. case SND_SOC_DAIFMT_I2S:
  496. dac_mode |= ES8328_DACCONTROL1_DACFORMAT_I2S;
  497. adc_mode |= ES8328_ADCCONTROL4_ADCFORMAT_I2S;
  498. break;
  499. case SND_SOC_DAIFMT_RIGHT_J:
  500. dac_mode |= ES8328_DACCONTROL1_DACFORMAT_RJUST;
  501. adc_mode |= ES8328_ADCCONTROL4_ADCFORMAT_RJUST;
  502. break;
  503. case SND_SOC_DAIFMT_LEFT_J:
  504. dac_mode |= ES8328_DACCONTROL1_DACFORMAT_LJUST;
  505. adc_mode |= ES8328_ADCCONTROL4_ADCFORMAT_LJUST;
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. /* clock inversion */
  511. if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF)
  512. return -EINVAL;
  513. snd_soc_update_bits(codec, ES8328_DACCONTROL1,
  514. ES8328_DACCONTROL1_DACFORMAT_MASK, dac_mode);
  515. snd_soc_update_bits(codec, ES8328_ADCCONTROL4,
  516. ES8328_ADCCONTROL4_ADCFORMAT_MASK, adc_mode);
  517. /* Master serial port mode, with BCLK generated automatically */
  518. snd_soc_update_bits(codec, ES8328_MASTERMODE,
  519. ES8328_MASTERMODE_MSC, ES8328_MASTERMODE_MSC);
  520. return 0;
  521. }
  522. static int es8328_set_bias_level(struct snd_soc_codec *codec,
  523. enum snd_soc_bias_level level)
  524. {
  525. switch (level) {
  526. case SND_SOC_BIAS_ON:
  527. break;
  528. case SND_SOC_BIAS_PREPARE:
  529. /* VREF, VMID=2x50k, digital enabled */
  530. snd_soc_write(codec, ES8328_CHIPPOWER, 0);
  531. snd_soc_update_bits(codec, ES8328_CONTROL1,
  532. ES8328_CONTROL1_VMIDSEL_MASK |
  533. ES8328_CONTROL1_ENREF,
  534. ES8328_CONTROL1_VMIDSEL_50k |
  535. ES8328_CONTROL1_ENREF);
  536. break;
  537. case SND_SOC_BIAS_STANDBY:
  538. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  539. snd_soc_update_bits(codec, ES8328_CONTROL1,
  540. ES8328_CONTROL1_VMIDSEL_MASK |
  541. ES8328_CONTROL1_ENREF,
  542. ES8328_CONTROL1_VMIDSEL_5k |
  543. ES8328_CONTROL1_ENREF);
  544. /* Charge caps */
  545. msleep(100);
  546. }
  547. snd_soc_write(codec, ES8328_CONTROL2,
  548. ES8328_CONTROL2_OVERCURRENT_ON |
  549. ES8328_CONTROL2_THERMAL_SHUTDOWN_ON);
  550. /* VREF, VMID=2*500k, digital stopped */
  551. snd_soc_update_bits(codec, ES8328_CONTROL1,
  552. ES8328_CONTROL1_VMIDSEL_MASK |
  553. ES8328_CONTROL1_ENREF,
  554. ES8328_CONTROL1_VMIDSEL_500k |
  555. ES8328_CONTROL1_ENREF);
  556. break;
  557. case SND_SOC_BIAS_OFF:
  558. snd_soc_update_bits(codec, ES8328_CONTROL1,
  559. ES8328_CONTROL1_VMIDSEL_MASK |
  560. ES8328_CONTROL1_ENREF,
  561. 0);
  562. break;
  563. }
  564. return 0;
  565. }
  566. static const struct snd_soc_dai_ops es8328_dai_ops = {
  567. .startup = es8328_startup,
  568. .hw_params = es8328_hw_params,
  569. .digital_mute = es8328_mute,
  570. .set_sysclk = es8328_set_sysclk,
  571. .set_fmt = es8328_set_dai_fmt,
  572. };
  573. static struct snd_soc_dai_driver es8328_dai = {
  574. .name = "es8328-hifi-analog",
  575. .playback = {
  576. .stream_name = "Playback",
  577. .channels_min = 2,
  578. .channels_max = 2,
  579. .rates = ES8328_RATES,
  580. .formats = ES8328_FORMATS,
  581. },
  582. .capture = {
  583. .stream_name = "Capture",
  584. .channels_min = 2,
  585. .channels_max = 2,
  586. .rates = ES8328_RATES,
  587. .formats = ES8328_FORMATS,
  588. },
  589. .ops = &es8328_dai_ops,
  590. .symmetric_rates = 1,
  591. };
  592. static int es8328_suspend(struct snd_soc_codec *codec)
  593. {
  594. struct es8328_priv *es8328;
  595. int ret;
  596. es8328 = snd_soc_codec_get_drvdata(codec);
  597. clk_disable_unprepare(es8328->clk);
  598. ret = regulator_bulk_disable(ARRAY_SIZE(es8328->supplies),
  599. es8328->supplies);
  600. if (ret) {
  601. dev_err(codec->dev, "unable to disable regulators\n");
  602. return ret;
  603. }
  604. return 0;
  605. }
  606. static int es8328_resume(struct snd_soc_codec *codec)
  607. {
  608. struct regmap *regmap = dev_get_regmap(codec->dev, NULL);
  609. struct es8328_priv *es8328;
  610. int ret;
  611. es8328 = snd_soc_codec_get_drvdata(codec);
  612. ret = clk_prepare_enable(es8328->clk);
  613. if (ret) {
  614. dev_err(codec->dev, "unable to enable clock\n");
  615. return ret;
  616. }
  617. ret = regulator_bulk_enable(ARRAY_SIZE(es8328->supplies),
  618. es8328->supplies);
  619. if (ret) {
  620. dev_err(codec->dev, "unable to enable regulators\n");
  621. return ret;
  622. }
  623. regcache_mark_dirty(regmap);
  624. ret = regcache_sync(regmap);
  625. if (ret) {
  626. dev_err(codec->dev, "unable to sync regcache\n");
  627. return ret;
  628. }
  629. return 0;
  630. }
  631. static int es8328_codec_probe(struct snd_soc_codec *codec)
  632. {
  633. struct es8328_priv *es8328;
  634. int ret;
  635. es8328 = snd_soc_codec_get_drvdata(codec);
  636. ret = regulator_bulk_enable(ARRAY_SIZE(es8328->supplies),
  637. es8328->supplies);
  638. if (ret) {
  639. dev_err(codec->dev, "unable to enable regulators\n");
  640. return ret;
  641. }
  642. /* Setup clocks */
  643. es8328->clk = devm_clk_get(codec->dev, NULL);
  644. if (IS_ERR(es8328->clk)) {
  645. dev_err(codec->dev, "codec clock missing or invalid\n");
  646. ret = PTR_ERR(es8328->clk);
  647. goto clk_fail;
  648. }
  649. ret = clk_prepare_enable(es8328->clk);
  650. if (ret) {
  651. dev_err(codec->dev, "unable to prepare codec clk\n");
  652. goto clk_fail;
  653. }
  654. return 0;
  655. clk_fail:
  656. regulator_bulk_disable(ARRAY_SIZE(es8328->supplies),
  657. es8328->supplies);
  658. return ret;
  659. }
  660. static int es8328_remove(struct snd_soc_codec *codec)
  661. {
  662. struct es8328_priv *es8328;
  663. es8328 = snd_soc_codec_get_drvdata(codec);
  664. if (es8328->clk)
  665. clk_disable_unprepare(es8328->clk);
  666. regulator_bulk_disable(ARRAY_SIZE(es8328->supplies),
  667. es8328->supplies);
  668. return 0;
  669. }
  670. const struct regmap_config es8328_regmap_config = {
  671. .reg_bits = 8,
  672. .val_bits = 8,
  673. .max_register = ES8328_REG_MAX,
  674. .cache_type = REGCACHE_RBTREE,
  675. .use_single_rw = true,
  676. };
  677. EXPORT_SYMBOL_GPL(es8328_regmap_config);
  678. static struct snd_soc_codec_driver es8328_codec_driver = {
  679. .probe = es8328_codec_probe,
  680. .suspend = es8328_suspend,
  681. .resume = es8328_resume,
  682. .remove = es8328_remove,
  683. .set_bias_level = es8328_set_bias_level,
  684. .suspend_bias_off = true,
  685. .component_driver = {
  686. .controls = es8328_snd_controls,
  687. .num_controls = ARRAY_SIZE(es8328_snd_controls),
  688. .dapm_widgets = es8328_dapm_widgets,
  689. .num_dapm_widgets = ARRAY_SIZE(es8328_dapm_widgets),
  690. .dapm_routes = es8328_dapm_routes,
  691. .num_dapm_routes = ARRAY_SIZE(es8328_dapm_routes),
  692. },
  693. };
  694. int es8328_probe(struct device *dev, struct regmap *regmap)
  695. {
  696. struct es8328_priv *es8328;
  697. int ret;
  698. int i;
  699. if (IS_ERR(regmap))
  700. return PTR_ERR(regmap);
  701. es8328 = devm_kzalloc(dev, sizeof(*es8328), GFP_KERNEL);
  702. if (es8328 == NULL)
  703. return -ENOMEM;
  704. es8328->regmap = regmap;
  705. for (i = 0; i < ARRAY_SIZE(es8328->supplies); i++)
  706. es8328->supplies[i].supply = supply_names[i];
  707. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(es8328->supplies),
  708. es8328->supplies);
  709. if (ret) {
  710. dev_err(dev, "unable to get regulators\n");
  711. return ret;
  712. }
  713. dev_set_drvdata(dev, es8328);
  714. return snd_soc_register_codec(dev,
  715. &es8328_codec_driver, &es8328_dai, 1);
  716. }
  717. EXPORT_SYMBOL_GPL(es8328_probe);
  718. MODULE_DESCRIPTION("ASoC ES8328 driver");
  719. MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
  720. MODULE_LICENSE("GPL");