es8328.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. #define ES8328_SYSCLK_RATE_1X 11289600
  28. #define ES8328_SYSCLK_RATE_2X 22579200
  29. /* Run the codec at 22.5792 or 11.2896 MHz to support these rates */
  30. static struct {
  31. int rate;
  32. u8 ratio;
  33. } mclk_ratios[] = {
  34. { 8000, 9 },
  35. {11025, 7 },
  36. {22050, 4 },
  37. {44100, 2 },
  38. };
  39. /* regulator supplies for sgtl5000, VDDD is an optional external supply */
  40. enum sgtl5000_regulator_supplies {
  41. DVDD,
  42. AVDD,
  43. PVDD,
  44. HPVDD,
  45. ES8328_SUPPLY_NUM
  46. };
  47. /* vddd is optional supply */
  48. static const char * const supply_names[ES8328_SUPPLY_NUM] = {
  49. "DVDD",
  50. "AVDD",
  51. "PVDD",
  52. "HPVDD",
  53. };
  54. #define ES8328_RATES (SNDRV_PCM_RATE_44100 | \
  55. SNDRV_PCM_RATE_22050 | \
  56. SNDRV_PCM_RATE_11025)
  57. #define ES8328_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
  58. struct es8328_priv {
  59. struct regmap *regmap;
  60. struct clk *clk;
  61. int playback_fs;
  62. bool deemph;
  63. struct regulator_bulk_data supplies[ES8328_SUPPLY_NUM];
  64. };
  65. /*
  66. * ES8328 Controls
  67. */
  68. static const char * const adcpol_txt[] = {"Normal", "L Invert", "R Invert",
  69. "L + R Invert"};
  70. static SOC_ENUM_SINGLE_DECL(adcpol,
  71. ES8328_ADCCONTROL6, 6, adcpol_txt);
  72. static const DECLARE_TLV_DB_SCALE(play_tlv, -3000, 100, 0);
  73. static const DECLARE_TLV_DB_SCALE(dac_adc_tlv, -9600, 50, 0);
  74. static const DECLARE_TLV_DB_SCALE(pga_tlv, 0, 300, 0);
  75. static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
  76. static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 300, 0);
  77. static const int deemph_settings[] = { 0, 32000, 44100, 48000 };
  78. static int es8328_set_deemph(struct snd_soc_codec *codec)
  79. {
  80. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  81. int val, i, best;
  82. /*
  83. * If we're using deemphasis select the nearest available sample
  84. * rate.
  85. */
  86. if (es8328->deemph) {
  87. best = 1;
  88. for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
  89. if (abs(deemph_settings[i] - es8328->playback_fs) <
  90. abs(deemph_settings[best] - es8328->playback_fs))
  91. best = i;
  92. }
  93. val = best << 1;
  94. } else {
  95. val = 0;
  96. }
  97. dev_dbg(codec->dev, "Set deemphasis %d\n", val);
  98. return snd_soc_update_bits(codec, ES8328_DACCONTROL6, 0x6, val);
  99. }
  100. static int es8328_get_deemph(struct snd_kcontrol *kcontrol,
  101. struct snd_ctl_elem_value *ucontrol)
  102. {
  103. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  104. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  105. ucontrol->value.integer.value[0] = es8328->deemph;
  106. return 0;
  107. }
  108. static int es8328_put_deemph(struct snd_kcontrol *kcontrol,
  109. struct snd_ctl_elem_value *ucontrol)
  110. {
  111. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  112. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  113. int deemph = ucontrol->value.integer.value[0];
  114. int ret;
  115. if (deemph > 1)
  116. return -EINVAL;
  117. ret = es8328_set_deemph(codec);
  118. if (ret < 0)
  119. return ret;
  120. es8328->deemph = deemph;
  121. return 0;
  122. }
  123. static const struct snd_kcontrol_new es8328_snd_controls[] = {
  124. SOC_DOUBLE_R_TLV("Capture Digital Volume",
  125. ES8328_ADCCONTROL8, ES8328_ADCCONTROL9,
  126. 0, 0xc0, 1, dac_adc_tlv),
  127. SOC_SINGLE("Capture ZC Switch", ES8328_ADCCONTROL7, 6, 1, 0),
  128. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  129. es8328_get_deemph, es8328_put_deemph),
  130. SOC_ENUM("Capture Polarity", adcpol),
  131. SOC_SINGLE_TLV("Left Mixer Left Bypass Volume",
  132. ES8328_DACCONTROL17, 3, 7, 1, bypass_tlv),
  133. SOC_SINGLE_TLV("Left Mixer Right Bypass Volume",
  134. ES8328_DACCONTROL19, 3, 7, 1, bypass_tlv),
  135. SOC_SINGLE_TLV("Right Mixer Left Bypass Volume",
  136. ES8328_DACCONTROL18, 3, 7, 1, bypass_tlv),
  137. SOC_SINGLE_TLV("Right Mixer Right Bypass Volume",
  138. ES8328_DACCONTROL20, 3, 7, 1, bypass_tlv),
  139. SOC_DOUBLE_R_TLV("PCM Volume",
  140. ES8328_LDACVOL, ES8328_RDACVOL,
  141. 0, ES8328_DACVOL_MAX, 1, dac_adc_tlv),
  142. SOC_DOUBLE_R_TLV("Output 1 Playback Volume",
  143. ES8328_LOUT1VOL, ES8328_ROUT1VOL,
  144. 0, ES8328_OUT1VOL_MAX, 0, play_tlv),
  145. SOC_DOUBLE_R_TLV("Output 2 Playback Volume",
  146. ES8328_LOUT2VOL, ES8328_ROUT2VOL,
  147. 0, ES8328_OUT2VOL_MAX, 0, play_tlv),
  148. SOC_DOUBLE_TLV("Mic PGA Volume", ES8328_ADCCONTROL1,
  149. 4, 0, 8, 0, mic_tlv),
  150. };
  151. /*
  152. * DAPM Controls
  153. */
  154. static const char * const es8328_line_texts[] = {
  155. "Line 1", "Line 2", "PGA", "Differential"};
  156. static const struct soc_enum es8328_lline_enum =
  157. SOC_ENUM_SINGLE(ES8328_DACCONTROL16, 3,
  158. ARRAY_SIZE(es8328_line_texts),
  159. es8328_line_texts);
  160. static const struct snd_kcontrol_new es8328_left_line_controls =
  161. SOC_DAPM_ENUM("Route", es8328_lline_enum);
  162. static const struct soc_enum es8328_rline_enum =
  163. SOC_ENUM_SINGLE(ES8328_DACCONTROL16, 0,
  164. ARRAY_SIZE(es8328_line_texts),
  165. es8328_line_texts);
  166. static const struct snd_kcontrol_new es8328_right_line_controls =
  167. SOC_DAPM_ENUM("Route", es8328_lline_enum);
  168. /* Left Mixer */
  169. static const struct snd_kcontrol_new es8328_left_mixer_controls[] = {
  170. SOC_DAPM_SINGLE("Playback Switch", ES8328_DACCONTROL17, 8, 1, 0),
  171. SOC_DAPM_SINGLE("Left Bypass Switch", ES8328_DACCONTROL17, 7, 1, 0),
  172. SOC_DAPM_SINGLE("Right Playback Switch", ES8328_DACCONTROL18, 8, 1, 0),
  173. SOC_DAPM_SINGLE("Right Bypass Switch", ES8328_DACCONTROL18, 7, 1, 0),
  174. };
  175. /* Right Mixer */
  176. static const struct snd_kcontrol_new es8328_right_mixer_controls[] = {
  177. SOC_DAPM_SINGLE("Left Playback Switch", ES8328_DACCONTROL19, 8, 1, 0),
  178. SOC_DAPM_SINGLE("Left Bypass Switch", ES8328_DACCONTROL19, 7, 1, 0),
  179. SOC_DAPM_SINGLE("Playback Switch", ES8328_DACCONTROL20, 8, 1, 0),
  180. SOC_DAPM_SINGLE("Right Bypass Switch", ES8328_DACCONTROL20, 7, 1, 0),
  181. };
  182. static const char * const es8328_pga_sel[] = {
  183. "Line 1", "Line 2", "Line 3", "Differential"};
  184. /* Left PGA Mux */
  185. static const struct soc_enum es8328_lpga_enum =
  186. SOC_ENUM_SINGLE(ES8328_ADCCONTROL2, 6,
  187. ARRAY_SIZE(es8328_pga_sel),
  188. es8328_pga_sel);
  189. static const struct snd_kcontrol_new es8328_left_pga_controls =
  190. SOC_DAPM_ENUM("Route", es8328_lpga_enum);
  191. /* Right PGA Mux */
  192. static const struct soc_enum es8328_rpga_enum =
  193. SOC_ENUM_SINGLE(ES8328_ADCCONTROL2, 4,
  194. ARRAY_SIZE(es8328_pga_sel),
  195. es8328_pga_sel);
  196. static const struct snd_kcontrol_new es8328_right_pga_controls =
  197. SOC_DAPM_ENUM("Route", es8328_rpga_enum);
  198. /* Differential Mux */
  199. static const char * const es8328_diff_sel[] = {"Line 1", "Line 2"};
  200. static SOC_ENUM_SINGLE_DECL(diffmux,
  201. ES8328_ADCCONTROL3, 7, es8328_diff_sel);
  202. static const struct snd_kcontrol_new es8328_diffmux_controls =
  203. SOC_DAPM_ENUM("Route", diffmux);
  204. /* Mono ADC Mux */
  205. static const char * const es8328_mono_mux[] = {"Stereo", "Mono (Left)",
  206. "Mono (Right)", "Digital Mono"};
  207. static SOC_ENUM_SINGLE_DECL(monomux,
  208. ES8328_ADCCONTROL3, 3, es8328_mono_mux);
  209. static const struct snd_kcontrol_new es8328_monomux_controls =
  210. SOC_DAPM_ENUM("Route", monomux);
  211. static const struct snd_soc_dapm_widget es8328_dapm_widgets[] = {
  212. SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
  213. &es8328_diffmux_controls),
  214. SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
  215. &es8328_monomux_controls),
  216. SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
  217. &es8328_monomux_controls),
  218. SND_SOC_DAPM_MUX("Left PGA Mux", ES8328_ADCPOWER,
  219. ES8328_ADCPOWER_AINL_OFF, 1,
  220. &es8328_left_pga_controls),
  221. SND_SOC_DAPM_MUX("Right PGA Mux", ES8328_ADCPOWER,
  222. ES8328_ADCPOWER_AINR_OFF, 1,
  223. &es8328_right_pga_controls),
  224. SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
  225. &es8328_left_line_controls),
  226. SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
  227. &es8328_right_line_controls),
  228. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", ES8328_ADCPOWER,
  229. ES8328_ADCPOWER_ADCR_OFF, 1),
  230. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", ES8328_ADCPOWER,
  231. ES8328_ADCPOWER_ADCL_OFF, 1),
  232. SND_SOC_DAPM_SUPPLY("Mic Bias", ES8328_ADCPOWER,
  233. ES8328_ADCPOWER_MIC_BIAS_OFF, 1, NULL, 0),
  234. SND_SOC_DAPM_SUPPLY("Mic Bias Gen", ES8328_ADCPOWER,
  235. ES8328_ADCPOWER_ADC_BIAS_GEN_OFF, 1, NULL, 0),
  236. SND_SOC_DAPM_SUPPLY("DAC STM", ES8328_CHIPPOWER,
  237. ES8328_CHIPPOWER_DACSTM_RESET, 1, NULL, 0),
  238. SND_SOC_DAPM_SUPPLY("ADC STM", ES8328_CHIPPOWER,
  239. ES8328_CHIPPOWER_ADCSTM_RESET, 1, NULL, 0),
  240. SND_SOC_DAPM_SUPPLY("DAC DIG", ES8328_CHIPPOWER,
  241. ES8328_CHIPPOWER_DACDIG_OFF, 1, NULL, 0),
  242. SND_SOC_DAPM_SUPPLY("ADC DIG", ES8328_CHIPPOWER,
  243. ES8328_CHIPPOWER_ADCDIG_OFF, 1, NULL, 0),
  244. SND_SOC_DAPM_SUPPLY("DAC DLL", ES8328_CHIPPOWER,
  245. ES8328_CHIPPOWER_DACDLL_OFF, 1, NULL, 0),
  246. SND_SOC_DAPM_SUPPLY("ADC DLL", ES8328_CHIPPOWER,
  247. ES8328_CHIPPOWER_ADCDLL_OFF, 1, NULL, 0),
  248. SND_SOC_DAPM_SUPPLY("ADC Vref", ES8328_CHIPPOWER,
  249. ES8328_CHIPPOWER_ADCVREF_OFF, 1, NULL, 0),
  250. SND_SOC_DAPM_SUPPLY("DAC Vref", ES8328_CHIPPOWER,
  251. ES8328_CHIPPOWER_DACVREF_OFF, 1, NULL, 0),
  252. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", ES8328_DACPOWER,
  253. ES8328_DACPOWER_RDAC_OFF, 1),
  254. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", ES8328_DACPOWER,
  255. ES8328_DACPOWER_LDAC_OFF, 1),
  256. SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
  257. &es8328_left_mixer_controls[0],
  258. ARRAY_SIZE(es8328_left_mixer_controls)),
  259. SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
  260. &es8328_right_mixer_controls[0],
  261. ARRAY_SIZE(es8328_right_mixer_controls)),
  262. SND_SOC_DAPM_PGA("Right Out 2", ES8328_DACPOWER,
  263. ES8328_DACPOWER_ROUT2_ON, 0, NULL, 0),
  264. SND_SOC_DAPM_PGA("Left Out 2", ES8328_DACPOWER,
  265. ES8328_DACPOWER_LOUT2_ON, 0, NULL, 0),
  266. SND_SOC_DAPM_PGA("Right Out 1", ES8328_DACPOWER,
  267. ES8328_DACPOWER_ROUT1_ON, 0, NULL, 0),
  268. SND_SOC_DAPM_PGA("Left Out 1", ES8328_DACPOWER,
  269. ES8328_DACPOWER_LOUT1_ON, 0, NULL, 0),
  270. SND_SOC_DAPM_OUTPUT("LOUT1"),
  271. SND_SOC_DAPM_OUTPUT("ROUT1"),
  272. SND_SOC_DAPM_OUTPUT("LOUT2"),
  273. SND_SOC_DAPM_OUTPUT("ROUT2"),
  274. SND_SOC_DAPM_INPUT("LINPUT1"),
  275. SND_SOC_DAPM_INPUT("LINPUT2"),
  276. SND_SOC_DAPM_INPUT("RINPUT1"),
  277. SND_SOC_DAPM_INPUT("RINPUT2"),
  278. };
  279. static const struct snd_soc_dapm_route es8328_dapm_routes[] = {
  280. { "Left Line Mux", "Line 1", "LINPUT1" },
  281. { "Left Line Mux", "Line 2", "LINPUT2" },
  282. { "Left Line Mux", "PGA", "Left PGA Mux" },
  283. { "Left Line Mux", "Differential", "Differential Mux" },
  284. { "Right Line Mux", "Line 1", "RINPUT1" },
  285. { "Right Line Mux", "Line 2", "RINPUT2" },
  286. { "Right Line Mux", "PGA", "Right PGA Mux" },
  287. { "Right Line Mux", "Differential", "Differential Mux" },
  288. { "Left PGA Mux", "Line 1", "LINPUT1" },
  289. { "Left PGA Mux", "Line 2", "LINPUT2" },
  290. { "Left PGA Mux", "Differential", "Differential Mux" },
  291. { "Right PGA Mux", "Line 1", "RINPUT1" },
  292. { "Right PGA Mux", "Line 2", "RINPUT2" },
  293. { "Right PGA Mux", "Differential", "Differential Mux" },
  294. { "Differential Mux", "Line 1", "LINPUT1" },
  295. { "Differential Mux", "Line 1", "RINPUT1" },
  296. { "Differential Mux", "Line 2", "LINPUT2" },
  297. { "Differential Mux", "Line 2", "RINPUT2" },
  298. { "Left ADC Mux", "Stereo", "Left PGA Mux" },
  299. { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
  300. { "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
  301. { "Right ADC Mux", "Stereo", "Right PGA Mux" },
  302. { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
  303. { "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
  304. { "Left ADC", NULL, "Left ADC Mux" },
  305. { "Right ADC", NULL, "Right ADC Mux" },
  306. { "ADC DIG", NULL, "ADC STM" },
  307. { "ADC DIG", NULL, "ADC Vref" },
  308. { "ADC DIG", NULL, "ADC DLL" },
  309. { "Left ADC", NULL, "ADC DIG" },
  310. { "Right ADC", NULL, "ADC DIG" },
  311. { "Mic Bias", NULL, "Mic Bias Gen" },
  312. { "Left Line Mux", "Line 1", "LINPUT1" },
  313. { "Left Line Mux", "Line 2", "LINPUT2" },
  314. { "Left Line Mux", "PGA", "Left PGA Mux" },
  315. { "Left Line Mux", "Differential", "Differential Mux" },
  316. { "Right Line Mux", "Line 1", "RINPUT1" },
  317. { "Right Line Mux", "Line 2", "RINPUT2" },
  318. { "Right Line Mux", "PGA", "Right PGA Mux" },
  319. { "Right Line Mux", "Differential", "Differential Mux" },
  320. { "Left Out 1", NULL, "Left DAC" },
  321. { "Right Out 1", NULL, "Right DAC" },
  322. { "Left Out 2", NULL, "Left DAC" },
  323. { "Right Out 2", NULL, "Right DAC" },
  324. { "Left Mixer", "Playback Switch", "Left DAC" },
  325. { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
  326. { "Left Mixer", "Right Playback Switch", "Right DAC" },
  327. { "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
  328. { "Right Mixer", "Left Playback Switch", "Left DAC" },
  329. { "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
  330. { "Right Mixer", "Playback Switch", "Right DAC" },
  331. { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
  332. { "DAC DIG", NULL, "DAC STM" },
  333. { "DAC DIG", NULL, "DAC Vref" },
  334. { "DAC DIG", NULL, "DAC DLL" },
  335. { "Left DAC", NULL, "DAC DIG" },
  336. { "Right DAC", NULL, "DAC DIG" },
  337. { "Left Out 1", NULL, "Left Mixer" },
  338. { "LOUT1", NULL, "Left Out 1" },
  339. { "Right Out 1", NULL, "Right Mixer" },
  340. { "ROUT1", NULL, "Right Out 1" },
  341. { "Left Out 2", NULL, "Left Mixer" },
  342. { "LOUT2", NULL, "Left Out 2" },
  343. { "Right Out 2", NULL, "Right Mixer" },
  344. { "ROUT2", NULL, "Right Out 2" },
  345. };
  346. static int es8328_mute(struct snd_soc_dai *dai, int mute)
  347. {
  348. return snd_soc_update_bits(dai->codec, ES8328_DACCONTROL3,
  349. ES8328_DACCONTROL3_DACMUTE,
  350. mute ? ES8328_DACCONTROL3_DACMUTE : 0);
  351. }
  352. static int es8328_hw_params(struct snd_pcm_substream *substream,
  353. struct snd_pcm_hw_params *params,
  354. struct snd_soc_dai *dai)
  355. {
  356. struct snd_soc_codec *codec = dai->codec;
  357. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  358. int clk_rate;
  359. int i;
  360. int reg;
  361. u8 ratio;
  362. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  363. reg = ES8328_DACCONTROL2;
  364. else
  365. reg = ES8328_ADCCONTROL5;
  366. clk_rate = clk_get_rate(es8328->clk);
  367. if ((clk_rate != ES8328_SYSCLK_RATE_1X) &&
  368. (clk_rate != ES8328_SYSCLK_RATE_2X)) {
  369. dev_err(codec->dev,
  370. "%s: clock is running at %d Hz, not %d or %d Hz\n",
  371. __func__, clk_rate,
  372. ES8328_SYSCLK_RATE_1X, ES8328_SYSCLK_RATE_2X);
  373. return -EINVAL;
  374. }
  375. /* find master mode MCLK to sampling frequency ratio */
  376. ratio = mclk_ratios[0].rate;
  377. for (i = 1; i < ARRAY_SIZE(mclk_ratios); i++)
  378. if (params_rate(params) <= mclk_ratios[i].rate)
  379. ratio = mclk_ratios[i].ratio;
  380. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  381. es8328->playback_fs = params_rate(params);
  382. es8328_set_deemph(codec);
  383. }
  384. return snd_soc_update_bits(codec, reg, ES8328_RATEMASK, ratio);
  385. }
  386. static int es8328_set_dai_fmt(struct snd_soc_dai *codec_dai,
  387. unsigned int fmt)
  388. {
  389. struct snd_soc_codec *codec = codec_dai->codec;
  390. struct es8328_priv *es8328 = snd_soc_codec_get_drvdata(codec);
  391. int clk_rate;
  392. u8 mode = ES8328_DACCONTROL1_DACWL_16;
  393. /* set master/slave audio interface */
  394. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBM_CFM)
  395. return -EINVAL;
  396. /* interface format */
  397. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  398. case SND_SOC_DAIFMT_I2S:
  399. mode |= ES8328_DACCONTROL1_DACFORMAT_I2S;
  400. break;
  401. case SND_SOC_DAIFMT_RIGHT_J:
  402. mode |= ES8328_DACCONTROL1_DACFORMAT_RJUST;
  403. break;
  404. case SND_SOC_DAIFMT_LEFT_J:
  405. mode |= ES8328_DACCONTROL1_DACFORMAT_LJUST;
  406. break;
  407. default:
  408. return -EINVAL;
  409. }
  410. /* clock inversion */
  411. if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF)
  412. return -EINVAL;
  413. snd_soc_write(codec, ES8328_DACCONTROL1, mode);
  414. snd_soc_write(codec, ES8328_ADCCONTROL4, mode);
  415. /* Master serial port mode, with BCLK generated automatically */
  416. clk_rate = clk_get_rate(es8328->clk);
  417. if (clk_rate == ES8328_SYSCLK_RATE_1X)
  418. snd_soc_write(codec, ES8328_MASTERMODE,
  419. ES8328_MASTERMODE_MSC);
  420. else
  421. snd_soc_write(codec, ES8328_MASTERMODE,
  422. ES8328_MASTERMODE_MCLKDIV2 |
  423. ES8328_MASTERMODE_MSC);
  424. return 0;
  425. }
  426. static int es8328_set_bias_level(struct snd_soc_codec *codec,
  427. enum snd_soc_bias_level level)
  428. {
  429. switch (level) {
  430. case SND_SOC_BIAS_ON:
  431. break;
  432. case SND_SOC_BIAS_PREPARE:
  433. /* VREF, VMID=2x50k, digital enabled */
  434. snd_soc_write(codec, ES8328_CHIPPOWER, 0);
  435. snd_soc_update_bits(codec, ES8328_CONTROL1,
  436. ES8328_CONTROL1_VMIDSEL_MASK |
  437. ES8328_CONTROL1_ENREF,
  438. ES8328_CONTROL1_VMIDSEL_50k |
  439. ES8328_CONTROL1_ENREF);
  440. break;
  441. case SND_SOC_BIAS_STANDBY:
  442. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  443. snd_soc_update_bits(codec, ES8328_CONTROL1,
  444. ES8328_CONTROL1_VMIDSEL_MASK |
  445. ES8328_CONTROL1_ENREF,
  446. ES8328_CONTROL1_VMIDSEL_5k |
  447. ES8328_CONTROL1_ENREF);
  448. /* Charge caps */
  449. msleep(100);
  450. }
  451. snd_soc_write(codec, ES8328_CONTROL2,
  452. ES8328_CONTROL2_OVERCURRENT_ON |
  453. ES8328_CONTROL2_THERMAL_SHUTDOWN_ON);
  454. /* VREF, VMID=2*500k, digital stopped */
  455. snd_soc_update_bits(codec, ES8328_CONTROL1,
  456. ES8328_CONTROL1_VMIDSEL_MASK |
  457. ES8328_CONTROL1_ENREF,
  458. ES8328_CONTROL1_VMIDSEL_500k |
  459. ES8328_CONTROL1_ENREF);
  460. break;
  461. case SND_SOC_BIAS_OFF:
  462. snd_soc_update_bits(codec, ES8328_CONTROL1,
  463. ES8328_CONTROL1_VMIDSEL_MASK |
  464. ES8328_CONTROL1_ENREF,
  465. 0);
  466. break;
  467. }
  468. codec->dapm.bias_level = level;
  469. return 0;
  470. }
  471. static const struct snd_soc_dai_ops es8328_dai_ops = {
  472. .hw_params = es8328_hw_params,
  473. .digital_mute = es8328_mute,
  474. .set_fmt = es8328_set_dai_fmt,
  475. };
  476. static struct snd_soc_dai_driver es8328_dai = {
  477. .name = "es8328-hifi-analog",
  478. .playback = {
  479. .stream_name = "Playback",
  480. .channels_min = 2,
  481. .channels_max = 2,
  482. .rates = ES8328_RATES,
  483. .formats = ES8328_FORMATS,
  484. },
  485. .capture = {
  486. .stream_name = "Capture",
  487. .channels_min = 2,
  488. .channels_max = 2,
  489. .rates = ES8328_RATES,
  490. .formats = ES8328_FORMATS,
  491. },
  492. .ops = &es8328_dai_ops,
  493. };
  494. static int es8328_suspend(struct snd_soc_codec *codec)
  495. {
  496. struct es8328_priv *es8328;
  497. int ret;
  498. es8328 = snd_soc_codec_get_drvdata(codec);
  499. clk_disable_unprepare(es8328->clk);
  500. ret = regulator_bulk_disable(ARRAY_SIZE(es8328->supplies),
  501. es8328->supplies);
  502. if (ret) {
  503. dev_err(codec->dev, "unable to disable regulators\n");
  504. return ret;
  505. }
  506. return 0;
  507. }
  508. static int es8328_resume(struct snd_soc_codec *codec)
  509. {
  510. struct regmap *regmap = dev_get_regmap(codec->dev, NULL);
  511. struct es8328_priv *es8328;
  512. int ret;
  513. es8328 = snd_soc_codec_get_drvdata(codec);
  514. ret = clk_prepare_enable(es8328->clk);
  515. if (ret) {
  516. dev_err(codec->dev, "unable to enable clock\n");
  517. return ret;
  518. }
  519. ret = regulator_bulk_enable(ARRAY_SIZE(es8328->supplies),
  520. es8328->supplies);
  521. if (ret) {
  522. dev_err(codec->dev, "unable to enable regulators\n");
  523. return ret;
  524. }
  525. regcache_mark_dirty(regmap);
  526. ret = regcache_sync(regmap);
  527. if (ret) {
  528. dev_err(codec->dev, "unable to sync regcache\n");
  529. return ret;
  530. }
  531. return 0;
  532. }
  533. static int es8328_codec_probe(struct snd_soc_codec *codec)
  534. {
  535. struct es8328_priv *es8328;
  536. int ret;
  537. es8328 = snd_soc_codec_get_drvdata(codec);
  538. ret = regulator_bulk_enable(ARRAY_SIZE(es8328->supplies),
  539. es8328->supplies);
  540. if (ret) {
  541. dev_err(codec->dev, "unable to enable regulators\n");
  542. return ret;
  543. }
  544. /* Setup clocks */
  545. es8328->clk = devm_clk_get(codec->dev, NULL);
  546. if (IS_ERR(es8328->clk)) {
  547. dev_err(codec->dev, "codec clock missing or invalid\n");
  548. ret = PTR_ERR(es8328->clk);
  549. goto clk_fail;
  550. }
  551. ret = clk_prepare_enable(es8328->clk);
  552. if (ret) {
  553. dev_err(codec->dev, "unable to prepare codec clk\n");
  554. goto clk_fail;
  555. }
  556. return 0;
  557. clk_fail:
  558. regulator_bulk_disable(ARRAY_SIZE(es8328->supplies),
  559. es8328->supplies);
  560. return ret;
  561. }
  562. static int es8328_remove(struct snd_soc_codec *codec)
  563. {
  564. struct es8328_priv *es8328;
  565. es8328 = snd_soc_codec_get_drvdata(codec);
  566. if (es8328->clk)
  567. clk_disable_unprepare(es8328->clk);
  568. regulator_bulk_disable(ARRAY_SIZE(es8328->supplies),
  569. es8328->supplies);
  570. return 0;
  571. }
  572. const struct regmap_config es8328_regmap_config = {
  573. .reg_bits = 8,
  574. .val_bits = 8,
  575. .max_register = ES8328_REG_MAX,
  576. .cache_type = REGCACHE_RBTREE,
  577. };
  578. EXPORT_SYMBOL_GPL(es8328_regmap_config);
  579. static struct snd_soc_codec_driver es8328_codec_driver = {
  580. .probe = es8328_codec_probe,
  581. .suspend = es8328_suspend,
  582. .resume = es8328_resume,
  583. .remove = es8328_remove,
  584. .set_bias_level = es8328_set_bias_level,
  585. .suspend_bias_off = true,
  586. .controls = es8328_snd_controls,
  587. .num_controls = ARRAY_SIZE(es8328_snd_controls),
  588. .dapm_widgets = es8328_dapm_widgets,
  589. .num_dapm_widgets = ARRAY_SIZE(es8328_dapm_widgets),
  590. .dapm_routes = es8328_dapm_routes,
  591. .num_dapm_routes = ARRAY_SIZE(es8328_dapm_routes),
  592. };
  593. int es8328_probe(struct device *dev, struct regmap *regmap)
  594. {
  595. struct es8328_priv *es8328;
  596. int ret;
  597. int i;
  598. if (IS_ERR(regmap))
  599. return PTR_ERR(regmap);
  600. es8328 = devm_kzalloc(dev, sizeof(*es8328), GFP_KERNEL);
  601. if (es8328 == NULL)
  602. return -ENOMEM;
  603. es8328->regmap = regmap;
  604. for (i = 0; i < ARRAY_SIZE(es8328->supplies); i++)
  605. es8328->supplies[i].supply = supply_names[i];
  606. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(es8328->supplies),
  607. es8328->supplies);
  608. if (ret) {
  609. dev_err(dev, "unable to get regulators\n");
  610. return ret;
  611. }
  612. dev_set_drvdata(dev, es8328);
  613. return snd_soc_register_codec(dev,
  614. &es8328_codec_driver, &es8328_dai, 1);
  615. }
  616. EXPORT_SYMBOL_GPL(es8328_probe);
  617. MODULE_DESCRIPTION("ASoC ES8328 driver");
  618. MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
  619. MODULE_LICENSE("GPL");