es8328.c 21 KB

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