adau17x1.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Common code for ADAU1X61 and ADAU1X81 codecs
  3. *
  4. * Copyright 2011-2014 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/slab.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include <sound/tlv.h>
  19. #include <linux/gcd.h>
  20. #include <linux/i2c.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/regmap.h>
  23. #include "sigmadsp.h"
  24. #include "adau17x1.h"
  25. #include "adau-utils.h"
  26. static const char * const adau17x1_capture_mixer_boost_text[] = {
  27. "Normal operation", "Boost Level 1", "Boost Level 2", "Boost Level 3",
  28. };
  29. static SOC_ENUM_SINGLE_DECL(adau17x1_capture_boost_enum,
  30. ADAU17X1_REC_POWER_MGMT, 5, adau17x1_capture_mixer_boost_text);
  31. static const char * const adau17x1_mic_bias_mode_text[] = {
  32. "Normal operation", "High performance",
  33. };
  34. static SOC_ENUM_SINGLE_DECL(adau17x1_mic_bias_mode_enum,
  35. ADAU17X1_MICBIAS, 3, adau17x1_mic_bias_mode_text);
  36. static const DECLARE_TLV_DB_MINMAX(adau17x1_digital_tlv, -9563, 0);
  37. static const struct snd_kcontrol_new adau17x1_controls[] = {
  38. SOC_DOUBLE_R_TLV("Digital Capture Volume",
  39. ADAU17X1_LEFT_INPUT_DIGITAL_VOL,
  40. ADAU17X1_RIGHT_INPUT_DIGITAL_VOL,
  41. 0, 0xff, 1, adau17x1_digital_tlv),
  42. SOC_DOUBLE_R_TLV("Digital Playback Volume", ADAU17X1_DAC_CONTROL1,
  43. ADAU17X1_DAC_CONTROL2, 0, 0xff, 1, adau17x1_digital_tlv),
  44. SOC_SINGLE("ADC High Pass Filter Switch", ADAU17X1_ADC_CONTROL,
  45. 5, 1, 0),
  46. SOC_SINGLE("Playback De-emphasis Switch", ADAU17X1_DAC_CONTROL0,
  47. 2, 1, 0),
  48. SOC_ENUM("Capture Boost", adau17x1_capture_boost_enum),
  49. SOC_ENUM("Mic Bias Mode", adau17x1_mic_bias_mode_enum),
  50. };
  51. static int adau17x1_pll_event(struct snd_soc_dapm_widget *w,
  52. struct snd_kcontrol *kcontrol, int event)
  53. {
  54. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  55. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  56. int ret;
  57. if (SND_SOC_DAPM_EVENT_ON(event)) {
  58. adau->pll_regs[5] = 1;
  59. } else {
  60. adau->pll_regs[5] = 0;
  61. /* Bypass the PLL when disabled, otherwise registers will become
  62. * inaccessible. */
  63. regmap_update_bits(adau->regmap, ADAU17X1_CLOCK_CONTROL,
  64. ADAU17X1_CLOCK_CONTROL_CORECLK_SRC_PLL, 0);
  65. }
  66. /* The PLL register is 6 bytes long and can only be written at once. */
  67. ret = regmap_raw_write(adau->regmap, ADAU17X1_PLL_CONTROL,
  68. adau->pll_regs, ARRAY_SIZE(adau->pll_regs));
  69. if (SND_SOC_DAPM_EVENT_ON(event)) {
  70. mdelay(5);
  71. regmap_update_bits(adau->regmap, ADAU17X1_CLOCK_CONTROL,
  72. ADAU17X1_CLOCK_CONTROL_CORECLK_SRC_PLL,
  73. ADAU17X1_CLOCK_CONTROL_CORECLK_SRC_PLL);
  74. }
  75. return 0;
  76. }
  77. static int adau17x1_adc_fixup(struct snd_soc_dapm_widget *w,
  78. struct snd_kcontrol *kcontrol, int event)
  79. {
  80. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  81. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  82. /*
  83. * If we are capturing, toggle the ADOSR bit in Converter Control 0 to
  84. * avoid losing SNR (workaround from ADI). This must be done after
  85. * the ADC(s) have been enabled. According to the data sheet, it is
  86. * normally illegal to set this bit when the sampling rate is 96 kHz,
  87. * but according to ADI it is acceptable for this workaround.
  88. */
  89. regmap_update_bits(adau->regmap, ADAU17X1_CONVERTER0,
  90. ADAU17X1_CONVERTER0_ADOSR, ADAU17X1_CONVERTER0_ADOSR);
  91. regmap_update_bits(adau->regmap, ADAU17X1_CONVERTER0,
  92. ADAU17X1_CONVERTER0_ADOSR, 0);
  93. return 0;
  94. }
  95. static const char * const adau17x1_mono_stereo_text[] = {
  96. "Stereo",
  97. "Mono Left Channel (L+R)",
  98. "Mono Right Channel (L+R)",
  99. "Mono (L+R)",
  100. };
  101. static SOC_ENUM_SINGLE_DECL(adau17x1_dac_mode_enum,
  102. ADAU17X1_DAC_CONTROL0, 6, adau17x1_mono_stereo_text);
  103. static const struct snd_kcontrol_new adau17x1_dac_mode_mux =
  104. SOC_DAPM_ENUM("DAC Mono-Stereo-Mode", adau17x1_dac_mode_enum);
  105. static const struct snd_soc_dapm_widget adau17x1_dapm_widgets[] = {
  106. SND_SOC_DAPM_SUPPLY_S("PLL", 3, SND_SOC_NOPM, 0, 0, adau17x1_pll_event,
  107. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  108. SND_SOC_DAPM_SUPPLY("AIFCLK", SND_SOC_NOPM, 0, 0, NULL, 0),
  109. SND_SOC_DAPM_SUPPLY("MICBIAS", ADAU17X1_MICBIAS, 0, 0, NULL, 0),
  110. SND_SOC_DAPM_SUPPLY("Left Playback Enable", ADAU17X1_PLAY_POWER_MGMT,
  111. 0, 0, NULL, 0),
  112. SND_SOC_DAPM_SUPPLY("Right Playback Enable", ADAU17X1_PLAY_POWER_MGMT,
  113. 1, 0, NULL, 0),
  114. SND_SOC_DAPM_MUX("Left DAC Mode Mux", SND_SOC_NOPM, 0, 0,
  115. &adau17x1_dac_mode_mux),
  116. SND_SOC_DAPM_MUX("Right DAC Mode Mux", SND_SOC_NOPM, 0, 0,
  117. &adau17x1_dac_mode_mux),
  118. SND_SOC_DAPM_ADC_E("Left Decimator", NULL, ADAU17X1_ADC_CONTROL, 0, 0,
  119. adau17x1_adc_fixup, SND_SOC_DAPM_POST_PMU),
  120. SND_SOC_DAPM_ADC("Right Decimator", NULL, ADAU17X1_ADC_CONTROL, 1, 0),
  121. SND_SOC_DAPM_DAC("Left DAC", NULL, ADAU17X1_DAC_CONTROL0, 0, 0),
  122. SND_SOC_DAPM_DAC("Right DAC", NULL, ADAU17X1_DAC_CONTROL0, 1, 0),
  123. };
  124. static const struct snd_soc_dapm_route adau17x1_dapm_routes[] = {
  125. { "Left Decimator", NULL, "SYSCLK" },
  126. { "Right Decimator", NULL, "SYSCLK" },
  127. { "Left DAC", NULL, "SYSCLK" },
  128. { "Right DAC", NULL, "SYSCLK" },
  129. { "Capture", NULL, "SYSCLK" },
  130. { "Playback", NULL, "SYSCLK" },
  131. { "Left DAC", NULL, "Left DAC Mode Mux" },
  132. { "Right DAC", NULL, "Right DAC Mode Mux" },
  133. { "Capture", NULL, "AIFCLK" },
  134. { "Playback", NULL, "AIFCLK" },
  135. };
  136. static const struct snd_soc_dapm_route adau17x1_dapm_pll_route = {
  137. "SYSCLK", NULL, "PLL",
  138. };
  139. /*
  140. * The MUX register for the Capture and Playback MUXs selects either DSP as
  141. * source/destination or one of the TDM slots. The TDM slot is selected via
  142. * snd_soc_dai_set_tdm_slot(), so we only expose whether to go to the DSP or
  143. * directly to the DAI interface with this control.
  144. */
  145. static int adau17x1_dsp_mux_enum_put(struct snd_kcontrol *kcontrol,
  146. struct snd_ctl_elem_value *ucontrol)
  147. {
  148. struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
  149. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  150. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  151. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  152. struct snd_soc_dapm_update update;
  153. unsigned int stream = e->shift_l;
  154. unsigned int val, change;
  155. int reg;
  156. if (ucontrol->value.enumerated.item[0] >= e->items)
  157. return -EINVAL;
  158. switch (ucontrol->value.enumerated.item[0]) {
  159. case 0:
  160. val = 0;
  161. adau->dsp_bypass[stream] = false;
  162. break;
  163. default:
  164. val = (adau->tdm_slot[stream] * 2) + 1;
  165. adau->dsp_bypass[stream] = true;
  166. break;
  167. }
  168. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  169. reg = ADAU17X1_SERIAL_INPUT_ROUTE;
  170. else
  171. reg = ADAU17X1_SERIAL_OUTPUT_ROUTE;
  172. change = snd_soc_test_bits(codec, reg, 0xff, val);
  173. if (change) {
  174. update.kcontrol = kcontrol;
  175. update.mask = 0xff;
  176. update.reg = reg;
  177. update.val = val;
  178. snd_soc_dapm_mux_update_power(dapm, kcontrol,
  179. ucontrol->value.enumerated.item[0], e, &update);
  180. }
  181. return change;
  182. }
  183. static int adau17x1_dsp_mux_enum_get(struct snd_kcontrol *kcontrol,
  184. struct snd_ctl_elem_value *ucontrol)
  185. {
  186. struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
  187. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  188. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  189. unsigned int stream = e->shift_l;
  190. unsigned int reg, val;
  191. int ret;
  192. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  193. reg = ADAU17X1_SERIAL_INPUT_ROUTE;
  194. else
  195. reg = ADAU17X1_SERIAL_OUTPUT_ROUTE;
  196. ret = regmap_read(adau->regmap, reg, &val);
  197. if (ret)
  198. return ret;
  199. if (val != 0)
  200. val = 1;
  201. ucontrol->value.enumerated.item[0] = val;
  202. return 0;
  203. }
  204. #define DECLARE_ADAU17X1_DSP_MUX_CTRL(_name, _label, _stream, _text) \
  205. const struct snd_kcontrol_new _name = \
  206. SOC_DAPM_ENUM_EXT(_label, (const struct soc_enum)\
  207. SOC_ENUM_SINGLE(SND_SOC_NOPM, _stream, \
  208. ARRAY_SIZE(_text), _text), \
  209. adau17x1_dsp_mux_enum_get, adau17x1_dsp_mux_enum_put)
  210. static const char * const adau17x1_dac_mux_text[] = {
  211. "DSP",
  212. "AIFIN",
  213. };
  214. static const char * const adau17x1_capture_mux_text[] = {
  215. "DSP",
  216. "Decimator",
  217. };
  218. static DECLARE_ADAU17X1_DSP_MUX_CTRL(adau17x1_dac_mux, "DAC Playback Mux",
  219. SNDRV_PCM_STREAM_PLAYBACK, adau17x1_dac_mux_text);
  220. static DECLARE_ADAU17X1_DSP_MUX_CTRL(adau17x1_capture_mux, "Capture Mux",
  221. SNDRV_PCM_STREAM_CAPTURE, adau17x1_capture_mux_text);
  222. static const struct snd_soc_dapm_widget adau17x1_dsp_dapm_widgets[] = {
  223. SND_SOC_DAPM_PGA("DSP", ADAU17X1_DSP_RUN, 0, 0, NULL, 0),
  224. SND_SOC_DAPM_SIGGEN("DSP Siggen"),
  225. SND_SOC_DAPM_MUX("DAC Playback Mux", SND_SOC_NOPM, 0, 0,
  226. &adau17x1_dac_mux),
  227. SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0,
  228. &adau17x1_capture_mux),
  229. };
  230. static const struct snd_soc_dapm_route adau17x1_dsp_dapm_routes[] = {
  231. { "DAC Playback Mux", "DSP", "DSP" },
  232. { "DAC Playback Mux", "AIFIN", "Playback" },
  233. { "Left DAC Mode Mux", "Stereo", "DAC Playback Mux" },
  234. { "Left DAC Mode Mux", "Mono (L+R)", "DAC Playback Mux" },
  235. { "Left DAC Mode Mux", "Mono Left Channel (L+R)", "DAC Playback Mux" },
  236. { "Right DAC Mode Mux", "Stereo", "DAC Playback Mux" },
  237. { "Right DAC Mode Mux", "Mono (L+R)", "DAC Playback Mux" },
  238. { "Right DAC Mode Mux", "Mono Right Channel (L+R)", "DAC Playback Mux" },
  239. { "Capture Mux", "DSP", "DSP" },
  240. { "Capture Mux", "Decimator", "Left Decimator" },
  241. { "Capture Mux", "Decimator", "Right Decimator" },
  242. { "Capture", NULL, "Capture Mux" },
  243. { "DSP", NULL, "DSP Siggen" },
  244. { "DSP", NULL, "Left Decimator" },
  245. { "DSP", NULL, "Right Decimator" },
  246. };
  247. static const struct snd_soc_dapm_route adau17x1_no_dsp_dapm_routes[] = {
  248. { "Left DAC Mode Mux", "Stereo", "Playback" },
  249. { "Left DAC Mode Mux", "Mono (L+R)", "Playback" },
  250. { "Left DAC Mode Mux", "Mono Left Channel (L+R)", "Playback" },
  251. { "Right DAC Mode Mux", "Stereo", "Playback" },
  252. { "Right DAC Mode Mux", "Mono (L+R)", "Playback" },
  253. { "Right DAC Mode Mux", "Mono Right Channel (L+R)", "Playback" },
  254. { "Capture", NULL, "Left Decimator" },
  255. { "Capture", NULL, "Right Decimator" },
  256. };
  257. bool adau17x1_has_dsp(struct adau *adau)
  258. {
  259. switch (adau->type) {
  260. case ADAU1761:
  261. case ADAU1381:
  262. case ADAU1781:
  263. return true;
  264. default:
  265. return false;
  266. }
  267. }
  268. EXPORT_SYMBOL_GPL(adau17x1_has_dsp);
  269. static int adau17x1_set_dai_pll(struct snd_soc_dai *dai, int pll_id,
  270. int source, unsigned int freq_in, unsigned int freq_out)
  271. {
  272. struct snd_soc_codec *codec = dai->codec;
  273. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  274. int ret;
  275. if (freq_in < 8000000 || freq_in > 27000000)
  276. return -EINVAL;
  277. ret = adau_calc_pll_cfg(freq_in, freq_out, adau->pll_regs);
  278. if (ret < 0)
  279. return ret;
  280. /* The PLL register is 6 bytes long and can only be written at once. */
  281. ret = regmap_raw_write(adau->regmap, ADAU17X1_PLL_CONTROL,
  282. adau->pll_regs, ARRAY_SIZE(adau->pll_regs));
  283. if (ret)
  284. return ret;
  285. adau->pll_freq = freq_out;
  286. return 0;
  287. }
  288. static int adau17x1_set_dai_sysclk(struct snd_soc_dai *dai,
  289. int clk_id, unsigned int freq, int dir)
  290. {
  291. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(dai->codec);
  292. struct adau *adau = snd_soc_codec_get_drvdata(dai->codec);
  293. bool is_pll;
  294. bool was_pll;
  295. switch (clk_id) {
  296. case ADAU17X1_CLK_SRC_MCLK:
  297. is_pll = false;
  298. break;
  299. case ADAU17X1_CLK_SRC_PLL_AUTO:
  300. if (!adau->mclk)
  301. return -EINVAL;
  302. /* Fall-through */
  303. case ADAU17X1_CLK_SRC_PLL:
  304. is_pll = true;
  305. break;
  306. default:
  307. return -EINVAL;
  308. }
  309. switch (adau->clk_src) {
  310. case ADAU17X1_CLK_SRC_MCLK:
  311. was_pll = false;
  312. break;
  313. case ADAU17X1_CLK_SRC_PLL:
  314. case ADAU17X1_CLK_SRC_PLL_AUTO:
  315. was_pll = true;
  316. break;
  317. default:
  318. return -EINVAL;
  319. }
  320. adau->sysclk = freq;
  321. if (is_pll != was_pll) {
  322. if (is_pll) {
  323. snd_soc_dapm_add_routes(dapm,
  324. &adau17x1_dapm_pll_route, 1);
  325. } else {
  326. snd_soc_dapm_del_routes(dapm,
  327. &adau17x1_dapm_pll_route, 1);
  328. }
  329. }
  330. adau->clk_src = clk_id;
  331. return 0;
  332. }
  333. static int adau17x1_auto_pll(struct snd_soc_dai *dai,
  334. struct snd_pcm_hw_params *params)
  335. {
  336. struct adau *adau = snd_soc_dai_get_drvdata(dai);
  337. unsigned int pll_rate;
  338. switch (params_rate(params)) {
  339. case 48000:
  340. case 8000:
  341. case 12000:
  342. case 16000:
  343. case 24000:
  344. case 32000:
  345. case 96000:
  346. pll_rate = 48000 * 1024;
  347. break;
  348. case 44100:
  349. case 7350:
  350. case 11025:
  351. case 14700:
  352. case 22050:
  353. case 29400:
  354. case 88200:
  355. pll_rate = 44100 * 1024;
  356. break;
  357. default:
  358. return -EINVAL;
  359. }
  360. return adau17x1_set_dai_pll(dai, ADAU17X1_PLL, ADAU17X1_PLL_SRC_MCLK,
  361. clk_get_rate(adau->mclk), pll_rate);
  362. }
  363. static int adau17x1_hw_params(struct snd_pcm_substream *substream,
  364. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  365. {
  366. struct snd_soc_codec *codec = dai->codec;
  367. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  368. unsigned int val, div, dsp_div;
  369. unsigned int freq;
  370. int ret;
  371. switch (adau->clk_src) {
  372. case ADAU17X1_CLK_SRC_PLL_AUTO:
  373. ret = adau17x1_auto_pll(dai, params);
  374. if (ret)
  375. return ret;
  376. /* Fall-through */
  377. case ADAU17X1_CLK_SRC_PLL:
  378. freq = adau->pll_freq;
  379. break;
  380. default:
  381. freq = adau->sysclk;
  382. break;
  383. }
  384. if (freq % params_rate(params) != 0)
  385. return -EINVAL;
  386. switch (freq / params_rate(params)) {
  387. case 1024: /* fs */
  388. div = 0;
  389. dsp_div = 1;
  390. break;
  391. case 6144: /* fs / 6 */
  392. div = 1;
  393. dsp_div = 6;
  394. break;
  395. case 4096: /* fs / 4 */
  396. div = 2;
  397. dsp_div = 5;
  398. break;
  399. case 3072: /* fs / 3 */
  400. div = 3;
  401. dsp_div = 4;
  402. break;
  403. case 2048: /* fs / 2 */
  404. div = 4;
  405. dsp_div = 3;
  406. break;
  407. case 1536: /* fs / 1.5 */
  408. div = 5;
  409. dsp_div = 2;
  410. break;
  411. case 512: /* fs / 0.5 */
  412. div = 6;
  413. dsp_div = 0;
  414. break;
  415. default:
  416. return -EINVAL;
  417. }
  418. regmap_update_bits(adau->regmap, ADAU17X1_CONVERTER0,
  419. ADAU17X1_CONVERTER0_CONVSR_MASK, div);
  420. if (adau17x1_has_dsp(adau)) {
  421. regmap_write(adau->regmap, ADAU17X1_SERIAL_SAMPLING_RATE, div);
  422. regmap_write(adau->regmap, ADAU17X1_DSP_SAMPLING_RATE, dsp_div);
  423. }
  424. if (adau->sigmadsp) {
  425. ret = adau17x1_setup_firmware(adau, params_rate(params));
  426. if (ret < 0)
  427. return ret;
  428. }
  429. if (adau->dai_fmt != SND_SOC_DAIFMT_RIGHT_J)
  430. return 0;
  431. switch (params_width(params)) {
  432. case 16:
  433. val = ADAU17X1_SERIAL_PORT1_DELAY16;
  434. break;
  435. case 24:
  436. val = ADAU17X1_SERIAL_PORT1_DELAY8;
  437. break;
  438. case 32:
  439. val = ADAU17X1_SERIAL_PORT1_DELAY0;
  440. break;
  441. default:
  442. return -EINVAL;
  443. }
  444. return regmap_update_bits(adau->regmap, ADAU17X1_SERIAL_PORT1,
  445. ADAU17X1_SERIAL_PORT1_DELAY_MASK, val);
  446. }
  447. static int adau17x1_set_dai_fmt(struct snd_soc_dai *dai,
  448. unsigned int fmt)
  449. {
  450. struct adau *adau = snd_soc_codec_get_drvdata(dai->codec);
  451. unsigned int ctrl0, ctrl1;
  452. int lrclk_pol;
  453. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  454. case SND_SOC_DAIFMT_CBM_CFM:
  455. ctrl0 = ADAU17X1_SERIAL_PORT0_MASTER;
  456. adau->master = true;
  457. break;
  458. case SND_SOC_DAIFMT_CBS_CFS:
  459. ctrl0 = 0;
  460. adau->master = false;
  461. break;
  462. default:
  463. return -EINVAL;
  464. }
  465. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  466. case SND_SOC_DAIFMT_I2S:
  467. lrclk_pol = 0;
  468. ctrl1 = ADAU17X1_SERIAL_PORT1_DELAY1;
  469. break;
  470. case SND_SOC_DAIFMT_LEFT_J:
  471. case SND_SOC_DAIFMT_RIGHT_J:
  472. lrclk_pol = 1;
  473. ctrl1 = ADAU17X1_SERIAL_PORT1_DELAY0;
  474. break;
  475. case SND_SOC_DAIFMT_DSP_A:
  476. lrclk_pol = 1;
  477. ctrl0 |= ADAU17X1_SERIAL_PORT0_PULSE_MODE;
  478. ctrl1 = ADAU17X1_SERIAL_PORT1_DELAY1;
  479. break;
  480. case SND_SOC_DAIFMT_DSP_B:
  481. lrclk_pol = 1;
  482. ctrl0 |= ADAU17X1_SERIAL_PORT0_PULSE_MODE;
  483. ctrl1 = ADAU17X1_SERIAL_PORT1_DELAY0;
  484. break;
  485. default:
  486. return -EINVAL;
  487. }
  488. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  489. case SND_SOC_DAIFMT_NB_NF:
  490. break;
  491. case SND_SOC_DAIFMT_IB_NF:
  492. ctrl0 |= ADAU17X1_SERIAL_PORT0_BCLK_POL;
  493. break;
  494. case SND_SOC_DAIFMT_NB_IF:
  495. lrclk_pol = !lrclk_pol;
  496. break;
  497. case SND_SOC_DAIFMT_IB_IF:
  498. ctrl0 |= ADAU17X1_SERIAL_PORT0_BCLK_POL;
  499. lrclk_pol = !lrclk_pol;
  500. break;
  501. default:
  502. return -EINVAL;
  503. }
  504. if (lrclk_pol)
  505. ctrl0 |= ADAU17X1_SERIAL_PORT0_LRCLK_POL;
  506. regmap_write(adau->regmap, ADAU17X1_SERIAL_PORT0, ctrl0);
  507. regmap_write(adau->regmap, ADAU17X1_SERIAL_PORT1, ctrl1);
  508. adau->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  509. return 0;
  510. }
  511. static int adau17x1_set_dai_tdm_slot(struct snd_soc_dai *dai,
  512. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  513. {
  514. struct adau *adau = snd_soc_codec_get_drvdata(dai->codec);
  515. unsigned int ser_ctrl0, ser_ctrl1;
  516. unsigned int conv_ctrl0, conv_ctrl1;
  517. /* I2S mode */
  518. if (slots == 0) {
  519. slots = 2;
  520. rx_mask = 3;
  521. tx_mask = 3;
  522. slot_width = 32;
  523. }
  524. switch (slots) {
  525. case 2:
  526. ser_ctrl0 = ADAU17X1_SERIAL_PORT0_STEREO;
  527. break;
  528. case 4:
  529. ser_ctrl0 = ADAU17X1_SERIAL_PORT0_TDM4;
  530. break;
  531. case 8:
  532. if (adau->type == ADAU1361)
  533. return -EINVAL;
  534. ser_ctrl0 = ADAU17X1_SERIAL_PORT0_TDM8;
  535. break;
  536. default:
  537. return -EINVAL;
  538. }
  539. switch (slot_width * slots) {
  540. case 32:
  541. if (adau->type == ADAU1761)
  542. return -EINVAL;
  543. ser_ctrl1 = ADAU17X1_SERIAL_PORT1_BCLK32;
  544. break;
  545. case 64:
  546. ser_ctrl1 = ADAU17X1_SERIAL_PORT1_BCLK64;
  547. break;
  548. case 48:
  549. ser_ctrl1 = ADAU17X1_SERIAL_PORT1_BCLK48;
  550. break;
  551. case 128:
  552. ser_ctrl1 = ADAU17X1_SERIAL_PORT1_BCLK128;
  553. break;
  554. case 256:
  555. if (adau->type == ADAU1361)
  556. return -EINVAL;
  557. ser_ctrl1 = ADAU17X1_SERIAL_PORT1_BCLK256;
  558. break;
  559. default:
  560. return -EINVAL;
  561. }
  562. switch (rx_mask) {
  563. case 0x03:
  564. conv_ctrl1 = ADAU17X1_CONVERTER1_ADC_PAIR(1);
  565. adau->tdm_slot[SNDRV_PCM_STREAM_CAPTURE] = 0;
  566. break;
  567. case 0x0c:
  568. conv_ctrl1 = ADAU17X1_CONVERTER1_ADC_PAIR(2);
  569. adau->tdm_slot[SNDRV_PCM_STREAM_CAPTURE] = 1;
  570. break;
  571. case 0x30:
  572. conv_ctrl1 = ADAU17X1_CONVERTER1_ADC_PAIR(3);
  573. adau->tdm_slot[SNDRV_PCM_STREAM_CAPTURE] = 2;
  574. break;
  575. case 0xc0:
  576. conv_ctrl1 = ADAU17X1_CONVERTER1_ADC_PAIR(4);
  577. adau->tdm_slot[SNDRV_PCM_STREAM_CAPTURE] = 3;
  578. break;
  579. default:
  580. return -EINVAL;
  581. }
  582. switch (tx_mask) {
  583. case 0x03:
  584. conv_ctrl0 = ADAU17X1_CONVERTER0_DAC_PAIR(1);
  585. adau->tdm_slot[SNDRV_PCM_STREAM_PLAYBACK] = 0;
  586. break;
  587. case 0x0c:
  588. conv_ctrl0 = ADAU17X1_CONVERTER0_DAC_PAIR(2);
  589. adau->tdm_slot[SNDRV_PCM_STREAM_PLAYBACK] = 1;
  590. break;
  591. case 0x30:
  592. conv_ctrl0 = ADAU17X1_CONVERTER0_DAC_PAIR(3);
  593. adau->tdm_slot[SNDRV_PCM_STREAM_PLAYBACK] = 2;
  594. break;
  595. case 0xc0:
  596. conv_ctrl0 = ADAU17X1_CONVERTER0_DAC_PAIR(4);
  597. adau->tdm_slot[SNDRV_PCM_STREAM_PLAYBACK] = 3;
  598. break;
  599. default:
  600. return -EINVAL;
  601. }
  602. regmap_update_bits(adau->regmap, ADAU17X1_CONVERTER0,
  603. ADAU17X1_CONVERTER0_DAC_PAIR_MASK, conv_ctrl0);
  604. regmap_update_bits(adau->regmap, ADAU17X1_CONVERTER1,
  605. ADAU17X1_CONVERTER1_ADC_PAIR_MASK, conv_ctrl1);
  606. regmap_update_bits(adau->regmap, ADAU17X1_SERIAL_PORT0,
  607. ADAU17X1_SERIAL_PORT0_TDM_MASK, ser_ctrl0);
  608. regmap_update_bits(adau->regmap, ADAU17X1_SERIAL_PORT1,
  609. ADAU17X1_SERIAL_PORT1_BCLK_MASK, ser_ctrl1);
  610. if (!adau17x1_has_dsp(adau))
  611. return 0;
  612. if (adau->dsp_bypass[SNDRV_PCM_STREAM_PLAYBACK]) {
  613. regmap_write(adau->regmap, ADAU17X1_SERIAL_INPUT_ROUTE,
  614. (adau->tdm_slot[SNDRV_PCM_STREAM_PLAYBACK] * 2) + 1);
  615. }
  616. if (adau->dsp_bypass[SNDRV_PCM_STREAM_CAPTURE]) {
  617. regmap_write(adau->regmap, ADAU17X1_SERIAL_OUTPUT_ROUTE,
  618. (adau->tdm_slot[SNDRV_PCM_STREAM_CAPTURE] * 2) + 1);
  619. }
  620. return 0;
  621. }
  622. static int adau17x1_startup(struct snd_pcm_substream *substream,
  623. struct snd_soc_dai *dai)
  624. {
  625. struct adau *adau = snd_soc_codec_get_drvdata(dai->codec);
  626. if (adau->sigmadsp)
  627. return sigmadsp_restrict_params(adau->sigmadsp, substream);
  628. return 0;
  629. }
  630. const struct snd_soc_dai_ops adau17x1_dai_ops = {
  631. .hw_params = adau17x1_hw_params,
  632. .set_sysclk = adau17x1_set_dai_sysclk,
  633. .set_fmt = adau17x1_set_dai_fmt,
  634. .set_pll = adau17x1_set_dai_pll,
  635. .set_tdm_slot = adau17x1_set_dai_tdm_slot,
  636. .startup = adau17x1_startup,
  637. };
  638. EXPORT_SYMBOL_GPL(adau17x1_dai_ops);
  639. int adau17x1_set_micbias_voltage(struct snd_soc_codec *codec,
  640. enum adau17x1_micbias_voltage micbias)
  641. {
  642. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  643. switch (micbias) {
  644. case ADAU17X1_MICBIAS_0_90_AVDD:
  645. case ADAU17X1_MICBIAS_0_65_AVDD:
  646. break;
  647. default:
  648. return -EINVAL;
  649. }
  650. return regmap_write(adau->regmap, ADAU17X1_MICBIAS, micbias << 2);
  651. }
  652. EXPORT_SYMBOL_GPL(adau17x1_set_micbias_voltage);
  653. bool adau17x1_precious_register(struct device *dev, unsigned int reg)
  654. {
  655. /* SigmaDSP parameter memory */
  656. if (reg < 0x400)
  657. return true;
  658. return false;
  659. }
  660. EXPORT_SYMBOL_GPL(adau17x1_precious_register);
  661. bool adau17x1_readable_register(struct device *dev, unsigned int reg)
  662. {
  663. /* SigmaDSP parameter memory */
  664. if (reg < 0x400)
  665. return true;
  666. switch (reg) {
  667. case ADAU17X1_CLOCK_CONTROL:
  668. case ADAU17X1_PLL_CONTROL:
  669. case ADAU17X1_REC_POWER_MGMT:
  670. case ADAU17X1_MICBIAS:
  671. case ADAU17X1_SERIAL_PORT0:
  672. case ADAU17X1_SERIAL_PORT1:
  673. case ADAU17X1_CONVERTER0:
  674. case ADAU17X1_CONVERTER1:
  675. case ADAU17X1_LEFT_INPUT_DIGITAL_VOL:
  676. case ADAU17X1_RIGHT_INPUT_DIGITAL_VOL:
  677. case ADAU17X1_ADC_CONTROL:
  678. case ADAU17X1_PLAY_POWER_MGMT:
  679. case ADAU17X1_DAC_CONTROL0:
  680. case ADAU17X1_DAC_CONTROL1:
  681. case ADAU17X1_DAC_CONTROL2:
  682. case ADAU17X1_SERIAL_PORT_PAD:
  683. case ADAU17X1_CONTROL_PORT_PAD0:
  684. case ADAU17X1_CONTROL_PORT_PAD1:
  685. case ADAU17X1_DSP_SAMPLING_RATE:
  686. case ADAU17X1_SERIAL_INPUT_ROUTE:
  687. case ADAU17X1_SERIAL_OUTPUT_ROUTE:
  688. case ADAU17X1_DSP_ENABLE:
  689. case ADAU17X1_DSP_RUN:
  690. case ADAU17X1_SERIAL_SAMPLING_RATE:
  691. return true;
  692. default:
  693. break;
  694. }
  695. return false;
  696. }
  697. EXPORT_SYMBOL_GPL(adau17x1_readable_register);
  698. bool adau17x1_volatile_register(struct device *dev, unsigned int reg)
  699. {
  700. /* SigmaDSP parameter and program memory */
  701. if (reg < 0x4000)
  702. return true;
  703. switch (reg) {
  704. /* The PLL register is 6 bytes long */
  705. case ADAU17X1_PLL_CONTROL:
  706. case ADAU17X1_PLL_CONTROL + 1:
  707. case ADAU17X1_PLL_CONTROL + 2:
  708. case ADAU17X1_PLL_CONTROL + 3:
  709. case ADAU17X1_PLL_CONTROL + 4:
  710. case ADAU17X1_PLL_CONTROL + 5:
  711. return true;
  712. default:
  713. break;
  714. }
  715. return false;
  716. }
  717. EXPORT_SYMBOL_GPL(adau17x1_volatile_register);
  718. int adau17x1_setup_firmware(struct adau *adau, unsigned int rate)
  719. {
  720. int ret;
  721. int dspsr;
  722. ret = regmap_read(adau->regmap, ADAU17X1_DSP_SAMPLING_RATE, &dspsr);
  723. if (ret)
  724. return ret;
  725. regmap_write(adau->regmap, ADAU17X1_DSP_ENABLE, 1);
  726. regmap_write(adau->regmap, ADAU17X1_DSP_SAMPLING_RATE, 0xf);
  727. ret = sigmadsp_setup(adau->sigmadsp, rate);
  728. if (ret) {
  729. regmap_write(adau->regmap, ADAU17X1_DSP_ENABLE, 0);
  730. return ret;
  731. }
  732. regmap_write(adau->regmap, ADAU17X1_DSP_SAMPLING_RATE, dspsr);
  733. return 0;
  734. }
  735. EXPORT_SYMBOL_GPL(adau17x1_setup_firmware);
  736. int adau17x1_add_widgets(struct snd_soc_codec *codec)
  737. {
  738. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  739. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  740. int ret;
  741. ret = snd_soc_add_codec_controls(codec, adau17x1_controls,
  742. ARRAY_SIZE(adau17x1_controls));
  743. if (ret)
  744. return ret;
  745. ret = snd_soc_dapm_new_controls(dapm, adau17x1_dapm_widgets,
  746. ARRAY_SIZE(adau17x1_dapm_widgets));
  747. if (ret)
  748. return ret;
  749. if (adau17x1_has_dsp(adau)) {
  750. ret = snd_soc_dapm_new_controls(dapm, adau17x1_dsp_dapm_widgets,
  751. ARRAY_SIZE(adau17x1_dsp_dapm_widgets));
  752. if (ret)
  753. return ret;
  754. if (!adau->sigmadsp)
  755. return 0;
  756. ret = sigmadsp_attach(adau->sigmadsp, &codec->component);
  757. if (ret) {
  758. dev_err(codec->dev, "Failed to attach firmware: %d\n",
  759. ret);
  760. return ret;
  761. }
  762. }
  763. return 0;
  764. }
  765. EXPORT_SYMBOL_GPL(adau17x1_add_widgets);
  766. int adau17x1_add_routes(struct snd_soc_codec *codec)
  767. {
  768. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  769. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  770. int ret;
  771. ret = snd_soc_dapm_add_routes(dapm, adau17x1_dapm_routes,
  772. ARRAY_SIZE(adau17x1_dapm_routes));
  773. if (ret)
  774. return ret;
  775. if (adau17x1_has_dsp(adau)) {
  776. ret = snd_soc_dapm_add_routes(dapm, adau17x1_dsp_dapm_routes,
  777. ARRAY_SIZE(adau17x1_dsp_dapm_routes));
  778. } else {
  779. ret = snd_soc_dapm_add_routes(dapm, adau17x1_no_dsp_dapm_routes,
  780. ARRAY_SIZE(adau17x1_no_dsp_dapm_routes));
  781. }
  782. if (adau->clk_src != ADAU17X1_CLK_SRC_MCLK)
  783. snd_soc_dapm_add_routes(dapm, &adau17x1_dapm_pll_route, 1);
  784. return ret;
  785. }
  786. EXPORT_SYMBOL_GPL(adau17x1_add_routes);
  787. int adau17x1_resume(struct snd_soc_codec *codec)
  788. {
  789. struct adau *adau = snd_soc_codec_get_drvdata(codec);
  790. if (adau->switch_mode)
  791. adau->switch_mode(codec->dev);
  792. regcache_sync(adau->regmap);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL_GPL(adau17x1_resume);
  796. int adau17x1_probe(struct device *dev, struct regmap *regmap,
  797. enum adau17x1_type type, void (*switch_mode)(struct device *dev),
  798. const char *firmware_name)
  799. {
  800. struct adau *adau;
  801. int ret;
  802. if (IS_ERR(regmap))
  803. return PTR_ERR(regmap);
  804. adau = devm_kzalloc(dev, sizeof(*adau), GFP_KERNEL);
  805. if (!adau)
  806. return -ENOMEM;
  807. adau->mclk = devm_clk_get(dev, "mclk");
  808. if (IS_ERR(adau->mclk)) {
  809. if (PTR_ERR(adau->mclk) != -ENOENT)
  810. return PTR_ERR(adau->mclk);
  811. /* Clock is optional (for the driver) */
  812. adau->mclk = NULL;
  813. } else if (adau->mclk) {
  814. adau->clk_src = ADAU17X1_CLK_SRC_PLL_AUTO;
  815. /*
  816. * Any valid PLL output rate will work at this point, use one
  817. * that is likely to be chosen later as well. The register will
  818. * be written when the PLL is powered up for the first time.
  819. */
  820. ret = adau_calc_pll_cfg(clk_get_rate(adau->mclk), 48000 * 1024,
  821. adau->pll_regs);
  822. if (ret < 0)
  823. return ret;
  824. ret = clk_prepare_enable(adau->mclk);
  825. if (ret)
  826. return ret;
  827. }
  828. adau->regmap = regmap;
  829. adau->switch_mode = switch_mode;
  830. adau->type = type;
  831. dev_set_drvdata(dev, adau);
  832. if (firmware_name) {
  833. adau->sigmadsp = devm_sigmadsp_init_regmap(dev, regmap, NULL,
  834. firmware_name);
  835. if (IS_ERR(adau->sigmadsp)) {
  836. dev_warn(dev, "Could not find firmware file: %ld\n",
  837. PTR_ERR(adau->sigmadsp));
  838. adau->sigmadsp = NULL;
  839. }
  840. }
  841. if (switch_mode)
  842. switch_mode(dev);
  843. return 0;
  844. }
  845. EXPORT_SYMBOL_GPL(adau17x1_probe);
  846. void adau17x1_remove(struct device *dev)
  847. {
  848. struct adau *adau = dev_get_drvdata(dev);
  849. snd_soc_unregister_codec(dev);
  850. if (adau->mclk)
  851. clk_disable_unprepare(adau->mclk);
  852. }
  853. EXPORT_SYMBOL_GPL(adau17x1_remove);
  854. MODULE_DESCRIPTION("ASoC ADAU1X61/ADAU1X81 common code");
  855. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  856. MODULE_LICENSE("GPL");