adau17x1.c 26 KB

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