fsl_asrc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * Freescale ASRC ALSA SoC Digital Audio Interface (DAI) driver
  3. *
  4. * Copyright (C) 2014 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Nicolin Chen <nicoleotsuka@gmail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/module.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_data/dma-imx.h>
  18. #include <linux/pm_runtime.h>
  19. #include <sound/dmaengine_pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include "fsl_asrc.h"
  22. #define IDEAL_RATIO_DECIMAL_DEPTH 26
  23. #define pair_err(fmt, ...) \
  24. dev_err(&asrc_priv->pdev->dev, "Pair %c: " fmt, 'A' + index, ##__VA_ARGS__)
  25. #define pair_dbg(fmt, ...) \
  26. dev_dbg(&asrc_priv->pdev->dev, "Pair %c: " fmt, 'A' + index, ##__VA_ARGS__)
  27. /* Sample rates are aligned with that defined in pcm.h file */
  28. static const u8 process_option[][8][2] = {
  29. /* 32kHz 44.1kHz 48kHz 64kHz 88.2kHz 96kHz 176kHz 192kHz */
  30. {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},}, /* 5512Hz */
  31. {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},}, /* 8kHz */
  32. {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},}, /* 11025Hz */
  33. {{0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},}, /* 16kHz */
  34. {{0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},}, /* 22050Hz */
  35. {{0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0},}, /* 32kHz */
  36. {{0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0},}, /* 44.1kHz */
  37. {{0, 2}, {0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0},}, /* 48kHz */
  38. {{1, 2}, {0, 2}, {0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 0},}, /* 64kHz */
  39. {{1, 2}, {1, 2}, {1, 2}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1},}, /* 88.2kHz */
  40. {{1, 2}, {1, 2}, {1, 2}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1},}, /* 96kHz */
  41. {{2, 2}, {2, 2}, {2, 2}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1},}, /* 176kHz */
  42. {{2, 2}, {2, 2}, {2, 2}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1},}, /* 192kHz */
  43. };
  44. /* Corresponding to process_option */
  45. static int supported_input_rate[] = {
  46. 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, 88200,
  47. 96000, 176400, 192000,
  48. };
  49. static int supported_asrc_rate[] = {
  50. 32000, 44100, 48000, 64000, 88200, 96000, 176400, 192000,
  51. };
  52. /**
  53. * The following tables map the relationship between asrc_inclk/asrc_outclk in
  54. * fsl_asrc.h and the registers of ASRCSR
  55. */
  56. static unsigned char input_clk_map_imx35[] = {
  57. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
  58. };
  59. static unsigned char output_clk_map_imx35[] = {
  60. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
  61. };
  62. /* i.MX53 uses the same map for input and output */
  63. static unsigned char input_clk_map_imx53[] = {
  64. /* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf */
  65. 0x0, 0x1, 0x2, 0x7, 0x4, 0x5, 0x6, 0x3, 0x8, 0x9, 0xa, 0xb, 0xc, 0xf, 0xe, 0xd,
  66. };
  67. static unsigned char output_clk_map_imx53[] = {
  68. /* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf */
  69. 0x8, 0x9, 0xa, 0x7, 0xc, 0x5, 0x6, 0xb, 0x0, 0x1, 0x2, 0x3, 0x4, 0xf, 0xe, 0xd,
  70. };
  71. static unsigned char *clk_map[2];
  72. /**
  73. * Request ASRC pair
  74. *
  75. * It assigns pair by the order of A->C->B because allocation of pair B,
  76. * within range [ANCA, ANCA+ANCB-1], depends on the channels of pair A
  77. * while pair A and pair C are comparatively independent.
  78. */
  79. static int fsl_asrc_request_pair(int channels, struct fsl_asrc_pair *pair)
  80. {
  81. enum asrc_pair_index index = ASRC_INVALID_PAIR;
  82. struct fsl_asrc *asrc_priv = pair->asrc_priv;
  83. struct device *dev = &asrc_priv->pdev->dev;
  84. unsigned long lock_flags;
  85. int i, ret = 0;
  86. spin_lock_irqsave(&asrc_priv->lock, lock_flags);
  87. for (i = ASRC_PAIR_A; i < ASRC_PAIR_MAX_NUM; i++) {
  88. if (asrc_priv->pair[i] != NULL)
  89. continue;
  90. index = i;
  91. if (i != ASRC_PAIR_B)
  92. break;
  93. }
  94. if (index == ASRC_INVALID_PAIR) {
  95. dev_err(dev, "all pairs are busy now\n");
  96. ret = -EBUSY;
  97. } else if (asrc_priv->channel_avail < channels) {
  98. dev_err(dev, "can't afford required channels: %d\n", channels);
  99. ret = -EINVAL;
  100. } else {
  101. asrc_priv->channel_avail -= channels;
  102. asrc_priv->pair[index] = pair;
  103. pair->channels = channels;
  104. pair->index = index;
  105. }
  106. spin_unlock_irqrestore(&asrc_priv->lock, lock_flags);
  107. return ret;
  108. }
  109. /**
  110. * Release ASRC pair
  111. *
  112. * It clears the resource from asrc_priv and releases the occupied channels.
  113. */
  114. static void fsl_asrc_release_pair(struct fsl_asrc_pair *pair)
  115. {
  116. struct fsl_asrc *asrc_priv = pair->asrc_priv;
  117. enum asrc_pair_index index = pair->index;
  118. unsigned long lock_flags;
  119. /* Make sure the pair is disabled */
  120. regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
  121. ASRCTR_ASRCEi_MASK(index), 0);
  122. spin_lock_irqsave(&asrc_priv->lock, lock_flags);
  123. asrc_priv->channel_avail += pair->channels;
  124. asrc_priv->pair[index] = NULL;
  125. pair->error = 0;
  126. spin_unlock_irqrestore(&asrc_priv->lock, lock_flags);
  127. }
  128. /**
  129. * Configure input and output thresholds
  130. */
  131. static void fsl_asrc_set_watermarks(struct fsl_asrc_pair *pair, u32 in, u32 out)
  132. {
  133. struct fsl_asrc *asrc_priv = pair->asrc_priv;
  134. enum asrc_pair_index index = pair->index;
  135. regmap_update_bits(asrc_priv->regmap, REG_ASRMCR(index),
  136. ASRMCRi_EXTTHRSHi_MASK |
  137. ASRMCRi_INFIFO_THRESHOLD_MASK |
  138. ASRMCRi_OUTFIFO_THRESHOLD_MASK,
  139. ASRMCRi_EXTTHRSHi |
  140. ASRMCRi_INFIFO_THRESHOLD(in) |
  141. ASRMCRi_OUTFIFO_THRESHOLD(out));
  142. }
  143. /**
  144. * Calculate the total divisor between asrck clock rate and sample rate
  145. *
  146. * It follows the formula clk_rate = samplerate * (2 ^ prescaler) * divider
  147. */
  148. static u32 fsl_asrc_cal_asrck_divisor(struct fsl_asrc_pair *pair, u32 div)
  149. {
  150. u32 ps;
  151. /* Calculate the divisors: prescaler [2^0, 2^7], divder [1, 8] */
  152. for (ps = 0; div > 8; ps++)
  153. div >>= 1;
  154. return ((div - 1) << ASRCDRi_AxCPi_WIDTH) | ps;
  155. }
  156. /**
  157. * Calculate and set the ratio for Ideal Ratio mode only
  158. *
  159. * The ratio is a 32-bit fixed point value with 26 fractional bits.
  160. */
  161. static int fsl_asrc_set_ideal_ratio(struct fsl_asrc_pair *pair,
  162. int inrate, int outrate)
  163. {
  164. struct fsl_asrc *asrc_priv = pair->asrc_priv;
  165. enum asrc_pair_index index = pair->index;
  166. unsigned long ratio;
  167. int i;
  168. if (!outrate) {
  169. pair_err("output rate should not be zero\n");
  170. return -EINVAL;
  171. }
  172. /* Calculate the intergal part of the ratio */
  173. ratio = (inrate / outrate) << IDEAL_RATIO_DECIMAL_DEPTH;
  174. /* ... and then the 26 depth decimal part */
  175. inrate %= outrate;
  176. for (i = 1; i <= IDEAL_RATIO_DECIMAL_DEPTH; i++) {
  177. inrate <<= 1;
  178. if (inrate < outrate)
  179. continue;
  180. ratio |= 1 << (IDEAL_RATIO_DECIMAL_DEPTH - i);
  181. inrate -= outrate;
  182. if (!inrate)
  183. break;
  184. }
  185. regmap_write(asrc_priv->regmap, REG_ASRIDRL(index), ratio);
  186. regmap_write(asrc_priv->regmap, REG_ASRIDRH(index), ratio >> 24);
  187. return 0;
  188. }
  189. /**
  190. * Configure the assigned ASRC pair
  191. *
  192. * It configures those ASRC registers according to a configuration instance
  193. * of struct asrc_config which includes in/output sample rate, width, channel
  194. * and clock settings.
  195. */
  196. static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair)
  197. {
  198. struct asrc_config *config = pair->config;
  199. struct fsl_asrc *asrc_priv = pair->asrc_priv;
  200. enum asrc_pair_index index = pair->index;
  201. u32 inrate, outrate, indiv, outdiv;
  202. u32 clk_index[2], div[2];
  203. int in, out, channels;
  204. struct clk *clk;
  205. bool ideal;
  206. if (!config) {
  207. pair_err("invalid pair config\n");
  208. return -EINVAL;
  209. }
  210. /* Validate channels */
  211. if (config->channel_num < 1 || config->channel_num > 10) {
  212. pair_err("does not support %d channels\n", config->channel_num);
  213. return -EINVAL;
  214. }
  215. /* Validate output width */
  216. if (config->output_word_width == ASRC_WIDTH_8_BIT) {
  217. pair_err("does not support 8bit width output\n");
  218. return -EINVAL;
  219. }
  220. inrate = config->input_sample_rate;
  221. outrate = config->output_sample_rate;
  222. ideal = config->inclk == INCLK_NONE;
  223. /* Validate input and output sample rates */
  224. for (in = 0; in < ARRAY_SIZE(supported_input_rate); in++)
  225. if (inrate == supported_input_rate[in])
  226. break;
  227. if (in == ARRAY_SIZE(supported_input_rate)) {
  228. pair_err("unsupported input sample rate: %dHz\n", inrate);
  229. return -EINVAL;
  230. }
  231. for (out = 0; out < ARRAY_SIZE(supported_asrc_rate); out++)
  232. if (outrate == supported_asrc_rate[out])
  233. break;
  234. if (out == ARRAY_SIZE(supported_asrc_rate)) {
  235. pair_err("unsupported output sample rate: %dHz\n", outrate);
  236. return -EINVAL;
  237. }
  238. /* Validate input and output clock sources */
  239. clk_index[IN] = clk_map[IN][config->inclk];
  240. clk_index[OUT] = clk_map[OUT][config->outclk];
  241. /* We only have output clock for ideal ratio mode */
  242. clk = asrc_priv->asrck_clk[clk_index[ideal ? OUT : IN]];
  243. div[IN] = clk_get_rate(clk) / inrate;
  244. if (div[IN] == 0) {
  245. pair_err("failed to support input sample rate %dHz by asrck_%x\n",
  246. inrate, clk_index[ideal ? OUT : IN]);
  247. return -EINVAL;
  248. }
  249. clk = asrc_priv->asrck_clk[clk_index[OUT]];
  250. /* Use fixed output rate for Ideal Ratio mode (INCLK_NONE) */
  251. if (ideal)
  252. div[OUT] = clk_get_rate(clk) / IDEAL_RATIO_RATE;
  253. else
  254. div[OUT] = clk_get_rate(clk) / outrate;
  255. if (div[OUT] == 0) {
  256. pair_err("failed to support output sample rate %dHz by asrck_%x\n",
  257. outrate, clk_index[OUT]);
  258. return -EINVAL;
  259. }
  260. /* Set the channel number */
  261. channels = config->channel_num;
  262. if (asrc_priv->channel_bits < 4)
  263. channels /= 2;
  264. /* Update channels for current pair */
  265. regmap_update_bits(asrc_priv->regmap, REG_ASRCNCR,
  266. ASRCNCR_ANCi_MASK(index, asrc_priv->channel_bits),
  267. ASRCNCR_ANCi(index, channels, asrc_priv->channel_bits));
  268. /* Default setting: Automatic selection for processing mode */
  269. regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
  270. ASRCTR_ATSi_MASK(index), ASRCTR_ATS(index));
  271. regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
  272. ASRCTR_USRi_MASK(index), 0);
  273. /* Set the input and output clock sources */
  274. regmap_update_bits(asrc_priv->regmap, REG_ASRCSR,
  275. ASRCSR_AICSi_MASK(index) | ASRCSR_AOCSi_MASK(index),
  276. ASRCSR_AICS(index, clk_index[IN]) |
  277. ASRCSR_AOCS(index, clk_index[OUT]));
  278. /* Calculate the input clock divisors */
  279. indiv = fsl_asrc_cal_asrck_divisor(pair, div[IN]);
  280. outdiv = fsl_asrc_cal_asrck_divisor(pair, div[OUT]);
  281. /* Suppose indiv and outdiv includes prescaler, so add its MASK too */
  282. regmap_update_bits(asrc_priv->regmap, REG_ASRCDR(index),
  283. ASRCDRi_AOCPi_MASK(index) | ASRCDRi_AICPi_MASK(index) |
  284. ASRCDRi_AOCDi_MASK(index) | ASRCDRi_AICDi_MASK(index),
  285. ASRCDRi_AOCP(index, outdiv) | ASRCDRi_AICP(index, indiv));
  286. /* Implement word_width configurations */
  287. regmap_update_bits(asrc_priv->regmap, REG_ASRMCR1(index),
  288. ASRMCR1i_OW16_MASK | ASRMCR1i_IWD_MASK,
  289. ASRMCR1i_OW16(config->output_word_width) |
  290. ASRMCR1i_IWD(config->input_word_width));
  291. /* Enable BUFFER STALL */
  292. regmap_update_bits(asrc_priv->regmap, REG_ASRMCR(index),
  293. ASRMCRi_BUFSTALLi_MASK, ASRMCRi_BUFSTALLi);
  294. /* Set default thresholds for input and output FIFO */
  295. fsl_asrc_set_watermarks(pair, ASRC_INPUTFIFO_THRESHOLD,
  296. ASRC_INPUTFIFO_THRESHOLD);
  297. /* Configure the followings only for Ideal Ratio mode */
  298. if (!ideal)
  299. return 0;
  300. /* Clear ASTSx bit to use Ideal Ratio mode */
  301. regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
  302. ASRCTR_ATSi_MASK(index), 0);
  303. /* Enable Ideal Ratio mode */
  304. regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
  305. ASRCTR_IDRi_MASK(index) | ASRCTR_USRi_MASK(index),
  306. ASRCTR_IDR(index) | ASRCTR_USR(index));
  307. /* Apply configurations for pre- and post-processing */
  308. regmap_update_bits(asrc_priv->regmap, REG_ASRCFG,
  309. ASRCFG_PREMODi_MASK(index) | ASRCFG_POSTMODi_MASK(index),
  310. ASRCFG_PREMOD(index, process_option[in][out][0]) |
  311. ASRCFG_POSTMOD(index, process_option[in][out][1]));
  312. return fsl_asrc_set_ideal_ratio(pair, inrate, outrate);
  313. }
  314. /**
  315. * Start the assigned ASRC pair
  316. *
  317. * It enables the assigned pair and makes it stopped at the stall level.
  318. */
  319. static void fsl_asrc_start_pair(struct fsl_asrc_pair *pair)
  320. {
  321. struct fsl_asrc *asrc_priv = pair->asrc_priv;
  322. enum asrc_pair_index index = pair->index;
  323. int reg, retry = 10, i;
  324. /* Enable the current pair */
  325. regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
  326. ASRCTR_ASRCEi_MASK(index), ASRCTR_ASRCE(index));
  327. /* Wait for status of initialization */
  328. do {
  329. udelay(5);
  330. regmap_read(asrc_priv->regmap, REG_ASRCFG, &reg);
  331. reg &= ASRCFG_INIRQi_MASK(index);
  332. } while (!reg && --retry);
  333. /* Make the input fifo to ASRC STALL level */
  334. regmap_read(asrc_priv->regmap, REG_ASRCNCR, &reg);
  335. for (i = 0; i < pair->channels * 4; i++)
  336. regmap_write(asrc_priv->regmap, REG_ASRDI(index), 0);
  337. /* Enable overload interrupt */
  338. regmap_write(asrc_priv->regmap, REG_ASRIER, ASRIER_AOLIE);
  339. }
  340. /**
  341. * Stop the assigned ASRC pair
  342. */
  343. static void fsl_asrc_stop_pair(struct fsl_asrc_pair *pair)
  344. {
  345. struct fsl_asrc *asrc_priv = pair->asrc_priv;
  346. enum asrc_pair_index index = pair->index;
  347. /* Stop the current pair */
  348. regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
  349. ASRCTR_ASRCEi_MASK(index), 0);
  350. }
  351. /**
  352. * Get DMA channel according to the pair and direction.
  353. */
  354. struct dma_chan *fsl_asrc_get_dma_channel(struct fsl_asrc_pair *pair, bool dir)
  355. {
  356. struct fsl_asrc *asrc_priv = pair->asrc_priv;
  357. enum asrc_pair_index index = pair->index;
  358. char name[4];
  359. sprintf(name, "%cx%c", dir == IN ? 'r' : 't', index + 'a');
  360. return dma_request_slave_channel(&asrc_priv->pdev->dev, name);
  361. }
  362. EXPORT_SYMBOL_GPL(fsl_asrc_get_dma_channel);
  363. static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream,
  364. struct snd_pcm_hw_params *params,
  365. struct snd_soc_dai *dai)
  366. {
  367. struct fsl_asrc *asrc_priv = snd_soc_dai_get_drvdata(dai);
  368. int width = snd_pcm_format_width(params_format(params));
  369. struct snd_pcm_runtime *runtime = substream->runtime;
  370. struct fsl_asrc_pair *pair = runtime->private_data;
  371. unsigned int channels = params_channels(params);
  372. unsigned int rate = params_rate(params);
  373. struct asrc_config config;
  374. int word_width, ret;
  375. ret = fsl_asrc_request_pair(channels, pair);
  376. if (ret) {
  377. dev_err(dai->dev, "fail to request asrc pair\n");
  378. return ret;
  379. }
  380. pair->config = &config;
  381. if (width == 16)
  382. width = ASRC_WIDTH_16_BIT;
  383. else
  384. width = ASRC_WIDTH_24_BIT;
  385. if (asrc_priv->asrc_width == 16)
  386. word_width = ASRC_WIDTH_16_BIT;
  387. else
  388. word_width = ASRC_WIDTH_24_BIT;
  389. config.pair = pair->index;
  390. config.channel_num = channels;
  391. config.inclk = INCLK_NONE;
  392. config.outclk = OUTCLK_ASRCK1_CLK;
  393. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  394. config.input_word_width = width;
  395. config.output_word_width = word_width;
  396. config.input_sample_rate = rate;
  397. config.output_sample_rate = asrc_priv->asrc_rate;
  398. } else {
  399. config.input_word_width = word_width;
  400. config.output_word_width = width;
  401. config.input_sample_rate = asrc_priv->asrc_rate;
  402. config.output_sample_rate = rate;
  403. }
  404. ret = fsl_asrc_config_pair(pair);
  405. if (ret) {
  406. dev_err(dai->dev, "fail to config asrc pair\n");
  407. return ret;
  408. }
  409. return 0;
  410. }
  411. static int fsl_asrc_dai_hw_free(struct snd_pcm_substream *substream,
  412. struct snd_soc_dai *dai)
  413. {
  414. struct snd_pcm_runtime *runtime = substream->runtime;
  415. struct fsl_asrc_pair *pair = runtime->private_data;
  416. if (pair)
  417. fsl_asrc_release_pair(pair);
  418. return 0;
  419. }
  420. static int fsl_asrc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  421. struct snd_soc_dai *dai)
  422. {
  423. struct snd_pcm_runtime *runtime = substream->runtime;
  424. struct fsl_asrc_pair *pair = runtime->private_data;
  425. switch (cmd) {
  426. case SNDRV_PCM_TRIGGER_START:
  427. case SNDRV_PCM_TRIGGER_RESUME:
  428. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  429. fsl_asrc_start_pair(pair);
  430. break;
  431. case SNDRV_PCM_TRIGGER_STOP:
  432. case SNDRV_PCM_TRIGGER_SUSPEND:
  433. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  434. fsl_asrc_stop_pair(pair);
  435. break;
  436. default:
  437. return -EINVAL;
  438. }
  439. return 0;
  440. }
  441. static struct snd_soc_dai_ops fsl_asrc_dai_ops = {
  442. .hw_params = fsl_asrc_dai_hw_params,
  443. .hw_free = fsl_asrc_dai_hw_free,
  444. .trigger = fsl_asrc_dai_trigger,
  445. };
  446. static int fsl_asrc_dai_probe(struct snd_soc_dai *dai)
  447. {
  448. struct fsl_asrc *asrc_priv = snd_soc_dai_get_drvdata(dai);
  449. snd_soc_dai_init_dma_data(dai, &asrc_priv->dma_params_tx,
  450. &asrc_priv->dma_params_rx);
  451. return 0;
  452. }
  453. #define FSL_ASRC_RATES SNDRV_PCM_RATE_8000_192000
  454. #define FSL_ASRC_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | \
  455. SNDRV_PCM_FMTBIT_S16_LE | \
  456. SNDRV_PCM_FMTBIT_S20_3LE)
  457. static struct snd_soc_dai_driver fsl_asrc_dai = {
  458. .probe = fsl_asrc_dai_probe,
  459. .playback = {
  460. .stream_name = "ASRC-Playback",
  461. .channels_min = 1,
  462. .channels_max = 10,
  463. .rates = FSL_ASRC_RATES,
  464. .formats = FSL_ASRC_FORMATS,
  465. },
  466. .capture = {
  467. .stream_name = "ASRC-Capture",
  468. .channels_min = 1,
  469. .channels_max = 10,
  470. .rates = FSL_ASRC_RATES,
  471. .formats = FSL_ASRC_FORMATS,
  472. },
  473. .ops = &fsl_asrc_dai_ops,
  474. };
  475. static const struct snd_soc_component_driver fsl_asrc_component = {
  476. .name = "fsl-asrc-dai",
  477. };
  478. static bool fsl_asrc_readable_reg(struct device *dev, unsigned int reg)
  479. {
  480. switch (reg) {
  481. case REG_ASRCTR:
  482. case REG_ASRIER:
  483. case REG_ASRCNCR:
  484. case REG_ASRCFG:
  485. case REG_ASRCSR:
  486. case REG_ASRCDR1:
  487. case REG_ASRCDR2:
  488. case REG_ASRSTR:
  489. case REG_ASRPM1:
  490. case REG_ASRPM2:
  491. case REG_ASRPM3:
  492. case REG_ASRPM4:
  493. case REG_ASRPM5:
  494. case REG_ASRTFR1:
  495. case REG_ASRCCR:
  496. case REG_ASRDOA:
  497. case REG_ASRDOB:
  498. case REG_ASRDOC:
  499. case REG_ASRIDRHA:
  500. case REG_ASRIDRLA:
  501. case REG_ASRIDRHB:
  502. case REG_ASRIDRLB:
  503. case REG_ASRIDRHC:
  504. case REG_ASRIDRLC:
  505. case REG_ASR76K:
  506. case REG_ASR56K:
  507. case REG_ASRMCRA:
  508. case REG_ASRFSTA:
  509. case REG_ASRMCRB:
  510. case REG_ASRFSTB:
  511. case REG_ASRMCRC:
  512. case REG_ASRFSTC:
  513. case REG_ASRMCR1A:
  514. case REG_ASRMCR1B:
  515. case REG_ASRMCR1C:
  516. return true;
  517. default:
  518. return false;
  519. }
  520. }
  521. static bool fsl_asrc_volatile_reg(struct device *dev, unsigned int reg)
  522. {
  523. switch (reg) {
  524. case REG_ASRSTR:
  525. case REG_ASRDIA:
  526. case REG_ASRDIB:
  527. case REG_ASRDIC:
  528. case REG_ASRDOA:
  529. case REG_ASRDOB:
  530. case REG_ASRDOC:
  531. case REG_ASRFSTA:
  532. case REG_ASRFSTB:
  533. case REG_ASRFSTC:
  534. case REG_ASRCFG:
  535. return true;
  536. default:
  537. return false;
  538. }
  539. }
  540. static bool fsl_asrc_writeable_reg(struct device *dev, unsigned int reg)
  541. {
  542. switch (reg) {
  543. case REG_ASRCTR:
  544. case REG_ASRIER:
  545. case REG_ASRCNCR:
  546. case REG_ASRCFG:
  547. case REG_ASRCSR:
  548. case REG_ASRCDR1:
  549. case REG_ASRCDR2:
  550. case REG_ASRSTR:
  551. case REG_ASRPM1:
  552. case REG_ASRPM2:
  553. case REG_ASRPM3:
  554. case REG_ASRPM4:
  555. case REG_ASRPM5:
  556. case REG_ASRTFR1:
  557. case REG_ASRCCR:
  558. case REG_ASRDIA:
  559. case REG_ASRDIB:
  560. case REG_ASRDIC:
  561. case REG_ASRIDRHA:
  562. case REG_ASRIDRLA:
  563. case REG_ASRIDRHB:
  564. case REG_ASRIDRLB:
  565. case REG_ASRIDRHC:
  566. case REG_ASRIDRLC:
  567. case REG_ASR76K:
  568. case REG_ASR56K:
  569. case REG_ASRMCRA:
  570. case REG_ASRMCRB:
  571. case REG_ASRMCRC:
  572. case REG_ASRMCR1A:
  573. case REG_ASRMCR1B:
  574. case REG_ASRMCR1C:
  575. return true;
  576. default:
  577. return false;
  578. }
  579. }
  580. static struct reg_default fsl_asrc_reg[] = {
  581. { REG_ASRCTR, 0x0000 }, { REG_ASRIER, 0x0000 },
  582. { REG_ASRCNCR, 0x0000 }, { REG_ASRCFG, 0x0000 },
  583. { REG_ASRCSR, 0x0000 }, { REG_ASRCDR1, 0x0000 },
  584. { REG_ASRCDR2, 0x0000 }, { REG_ASRSTR, 0x0000 },
  585. { REG_ASRRA, 0x0000 }, { REG_ASRRB, 0x0000 },
  586. { REG_ASRRC, 0x0000 }, { REG_ASRPM1, 0x0000 },
  587. { REG_ASRPM2, 0x0000 }, { REG_ASRPM3, 0x0000 },
  588. { REG_ASRPM4, 0x0000 }, { REG_ASRPM5, 0x0000 },
  589. { REG_ASRTFR1, 0x0000 }, { REG_ASRCCR, 0x0000 },
  590. { REG_ASRDIA, 0x0000 }, { REG_ASRDOA, 0x0000 },
  591. { REG_ASRDIB, 0x0000 }, { REG_ASRDOB, 0x0000 },
  592. { REG_ASRDIC, 0x0000 }, { REG_ASRDOC, 0x0000 },
  593. { REG_ASRIDRHA, 0x0000 }, { REG_ASRIDRLA, 0x0000 },
  594. { REG_ASRIDRHB, 0x0000 }, { REG_ASRIDRLB, 0x0000 },
  595. { REG_ASRIDRHC, 0x0000 }, { REG_ASRIDRLC, 0x0000 },
  596. { REG_ASR76K, 0x0A47 }, { REG_ASR56K, 0x0DF3 },
  597. { REG_ASRMCRA, 0x0000 }, { REG_ASRFSTA, 0x0000 },
  598. { REG_ASRMCRB, 0x0000 }, { REG_ASRFSTB, 0x0000 },
  599. { REG_ASRMCRC, 0x0000 }, { REG_ASRFSTC, 0x0000 },
  600. { REG_ASRMCR1A, 0x0000 }, { REG_ASRMCR1B, 0x0000 },
  601. { REG_ASRMCR1C, 0x0000 },
  602. };
  603. static const struct regmap_config fsl_asrc_regmap_config = {
  604. .reg_bits = 32,
  605. .reg_stride = 4,
  606. .val_bits = 32,
  607. .max_register = REG_ASRMCR1C,
  608. .reg_defaults = fsl_asrc_reg,
  609. .num_reg_defaults = ARRAY_SIZE(fsl_asrc_reg),
  610. .readable_reg = fsl_asrc_readable_reg,
  611. .volatile_reg = fsl_asrc_volatile_reg,
  612. .writeable_reg = fsl_asrc_writeable_reg,
  613. .cache_type = REGCACHE_RBTREE,
  614. };
  615. /**
  616. * Initialize ASRC registers with a default configurations
  617. */
  618. static int fsl_asrc_init(struct fsl_asrc *asrc_priv)
  619. {
  620. /* Halt ASRC internal FP when input FIFO needs data for pair A, B, C */
  621. regmap_write(asrc_priv->regmap, REG_ASRCTR, ASRCTR_ASRCEN);
  622. /* Disable interrupt by default */
  623. regmap_write(asrc_priv->regmap, REG_ASRIER, 0x0);
  624. /* Apply recommended settings for parameters from Reference Manual */
  625. regmap_write(asrc_priv->regmap, REG_ASRPM1, 0x7fffff);
  626. regmap_write(asrc_priv->regmap, REG_ASRPM2, 0x255555);
  627. regmap_write(asrc_priv->regmap, REG_ASRPM3, 0xff7280);
  628. regmap_write(asrc_priv->regmap, REG_ASRPM4, 0xff7280);
  629. regmap_write(asrc_priv->regmap, REG_ASRPM5, 0xff7280);
  630. /* Base address for task queue FIFO. Set to 0x7C */
  631. regmap_update_bits(asrc_priv->regmap, REG_ASRTFR1,
  632. ASRTFR1_TF_BASE_MASK, ASRTFR1_TF_BASE(0xfc));
  633. /* Set the processing clock for 76KHz to 133M */
  634. regmap_write(asrc_priv->regmap, REG_ASR76K, 0x06D6);
  635. /* Set the processing clock for 56KHz to 133M */
  636. return regmap_write(asrc_priv->regmap, REG_ASR56K, 0x0947);
  637. }
  638. /**
  639. * Interrupt handler for ASRC
  640. */
  641. static irqreturn_t fsl_asrc_isr(int irq, void *dev_id)
  642. {
  643. struct fsl_asrc *asrc_priv = (struct fsl_asrc *)dev_id;
  644. struct device *dev = &asrc_priv->pdev->dev;
  645. enum asrc_pair_index index;
  646. u32 status;
  647. regmap_read(asrc_priv->regmap, REG_ASRSTR, &status);
  648. /* Clean overload error */
  649. regmap_write(asrc_priv->regmap, REG_ASRSTR, ASRSTR_AOLE);
  650. /*
  651. * We here use dev_dbg() for all exceptions because ASRC itself does
  652. * not care if FIFO overflowed or underrun while a warning in the
  653. * interrupt would result a ridged conversion.
  654. */
  655. for (index = ASRC_PAIR_A; index < ASRC_PAIR_MAX_NUM; index++) {
  656. if (!asrc_priv->pair[index])
  657. continue;
  658. if (status & ASRSTR_ATQOL) {
  659. asrc_priv->pair[index]->error |= ASRC_TASK_Q_OVERLOAD;
  660. dev_dbg(dev, "ASRC Task Queue FIFO overload\n");
  661. }
  662. if (status & ASRSTR_AOOL(index)) {
  663. asrc_priv->pair[index]->error |= ASRC_OUTPUT_TASK_OVERLOAD;
  664. pair_dbg("Output Task Overload\n");
  665. }
  666. if (status & ASRSTR_AIOL(index)) {
  667. asrc_priv->pair[index]->error |= ASRC_INPUT_TASK_OVERLOAD;
  668. pair_dbg("Input Task Overload\n");
  669. }
  670. if (status & ASRSTR_AODO(index)) {
  671. asrc_priv->pair[index]->error |= ASRC_OUTPUT_BUFFER_OVERFLOW;
  672. pair_dbg("Output Data Buffer has overflowed\n");
  673. }
  674. if (status & ASRSTR_AIDU(index)) {
  675. asrc_priv->pair[index]->error |= ASRC_INPUT_BUFFER_UNDERRUN;
  676. pair_dbg("Input Data Buffer has underflowed\n");
  677. }
  678. }
  679. return IRQ_HANDLED;
  680. }
  681. static int fsl_asrc_probe(struct platform_device *pdev)
  682. {
  683. struct device_node *np = pdev->dev.of_node;
  684. struct fsl_asrc *asrc_priv;
  685. struct resource *res;
  686. void __iomem *regs;
  687. int irq, ret, i;
  688. char tmp[16];
  689. asrc_priv = devm_kzalloc(&pdev->dev, sizeof(*asrc_priv), GFP_KERNEL);
  690. if (!asrc_priv)
  691. return -ENOMEM;
  692. asrc_priv->pdev = pdev;
  693. /* Get the addresses and IRQ */
  694. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  695. regs = devm_ioremap_resource(&pdev->dev, res);
  696. if (IS_ERR(regs))
  697. return PTR_ERR(regs);
  698. asrc_priv->paddr = res->start;
  699. asrc_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "mem", regs,
  700. &fsl_asrc_regmap_config);
  701. if (IS_ERR(asrc_priv->regmap)) {
  702. dev_err(&pdev->dev, "failed to init regmap\n");
  703. return PTR_ERR(asrc_priv->regmap);
  704. }
  705. irq = platform_get_irq(pdev, 0);
  706. if (irq < 0) {
  707. dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
  708. return irq;
  709. }
  710. ret = devm_request_irq(&pdev->dev, irq, fsl_asrc_isr, 0,
  711. dev_name(&pdev->dev), asrc_priv);
  712. if (ret) {
  713. dev_err(&pdev->dev, "failed to claim irq %u: %d\n", irq, ret);
  714. return ret;
  715. }
  716. asrc_priv->mem_clk = devm_clk_get(&pdev->dev, "mem");
  717. if (IS_ERR(asrc_priv->mem_clk)) {
  718. dev_err(&pdev->dev, "failed to get mem clock\n");
  719. return PTR_ERR(asrc_priv->mem_clk);
  720. }
  721. asrc_priv->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
  722. if (IS_ERR(asrc_priv->ipg_clk)) {
  723. dev_err(&pdev->dev, "failed to get ipg clock\n");
  724. return PTR_ERR(asrc_priv->ipg_clk);
  725. }
  726. for (i = 0; i < ASRC_CLK_MAX_NUM; i++) {
  727. sprintf(tmp, "asrck_%x", i);
  728. asrc_priv->asrck_clk[i] = devm_clk_get(&pdev->dev, tmp);
  729. if (IS_ERR(asrc_priv->asrck_clk[i])) {
  730. dev_err(&pdev->dev, "failed to get %s clock\n", tmp);
  731. return PTR_ERR(asrc_priv->asrck_clk[i]);
  732. }
  733. }
  734. if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx35-asrc")) {
  735. asrc_priv->channel_bits = 3;
  736. clk_map[IN] = input_clk_map_imx35;
  737. clk_map[OUT] = output_clk_map_imx35;
  738. } else {
  739. asrc_priv->channel_bits = 4;
  740. clk_map[IN] = input_clk_map_imx53;
  741. clk_map[OUT] = output_clk_map_imx53;
  742. }
  743. ret = fsl_asrc_init(asrc_priv);
  744. if (ret) {
  745. dev_err(&pdev->dev, "failed to init asrc %d\n", ret);
  746. return -EINVAL;
  747. }
  748. asrc_priv->channel_avail = 10;
  749. ret = of_property_read_u32(np, "fsl,asrc-rate",
  750. &asrc_priv->asrc_rate);
  751. if (ret) {
  752. dev_err(&pdev->dev, "failed to get output rate\n");
  753. return -EINVAL;
  754. }
  755. ret = of_property_read_u32(np, "fsl,asrc-width",
  756. &asrc_priv->asrc_width);
  757. if (ret) {
  758. dev_err(&pdev->dev, "failed to get output width\n");
  759. return -EINVAL;
  760. }
  761. if (asrc_priv->asrc_width != 16 && asrc_priv->asrc_width != 24) {
  762. dev_warn(&pdev->dev, "unsupported width, switching to 24bit\n");
  763. asrc_priv->asrc_width = 24;
  764. }
  765. platform_set_drvdata(pdev, asrc_priv);
  766. pm_runtime_enable(&pdev->dev);
  767. spin_lock_init(&asrc_priv->lock);
  768. ret = devm_snd_soc_register_component(&pdev->dev, &fsl_asrc_component,
  769. &fsl_asrc_dai, 1);
  770. if (ret) {
  771. dev_err(&pdev->dev, "failed to register ASoC DAI\n");
  772. return ret;
  773. }
  774. ret = devm_snd_soc_register_platform(&pdev->dev, &fsl_asrc_platform);
  775. if (ret) {
  776. dev_err(&pdev->dev, "failed to register ASoC platform\n");
  777. return ret;
  778. }
  779. dev_info(&pdev->dev, "driver registered\n");
  780. return 0;
  781. }
  782. #ifdef CONFIG_PM
  783. static int fsl_asrc_runtime_resume(struct device *dev)
  784. {
  785. struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
  786. int i, ret;
  787. ret = clk_prepare_enable(asrc_priv->mem_clk);
  788. if (ret)
  789. return ret;
  790. ret = clk_prepare_enable(asrc_priv->ipg_clk);
  791. if (ret)
  792. goto disable_mem_clk;
  793. for (i = 0; i < ASRC_CLK_MAX_NUM; i++) {
  794. ret = clk_prepare_enable(asrc_priv->asrck_clk[i]);
  795. if (ret)
  796. goto disable_asrck_clk;
  797. }
  798. return 0;
  799. disable_asrck_clk:
  800. for (i--; i >= 0; i--)
  801. clk_disable_unprepare(asrc_priv->asrck_clk[i]);
  802. clk_disable_unprepare(asrc_priv->ipg_clk);
  803. disable_mem_clk:
  804. clk_disable_unprepare(asrc_priv->mem_clk);
  805. return ret;
  806. }
  807. static int fsl_asrc_runtime_suspend(struct device *dev)
  808. {
  809. struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
  810. int i;
  811. for (i = 0; i < ASRC_CLK_MAX_NUM; i++)
  812. clk_disable_unprepare(asrc_priv->asrck_clk[i]);
  813. clk_disable_unprepare(asrc_priv->ipg_clk);
  814. clk_disable_unprepare(asrc_priv->mem_clk);
  815. return 0;
  816. }
  817. #endif /* CONFIG_PM */
  818. #ifdef CONFIG_PM_SLEEP
  819. static int fsl_asrc_suspend(struct device *dev)
  820. {
  821. struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
  822. regcache_cache_only(asrc_priv->regmap, true);
  823. regcache_mark_dirty(asrc_priv->regmap);
  824. return 0;
  825. }
  826. static int fsl_asrc_resume(struct device *dev)
  827. {
  828. struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
  829. u32 asrctr;
  830. /* Stop all pairs provisionally */
  831. regmap_read(asrc_priv->regmap, REG_ASRCTR, &asrctr);
  832. regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
  833. ASRCTR_ASRCEi_ALL_MASK, 0);
  834. /* Restore all registers */
  835. regcache_cache_only(asrc_priv->regmap, false);
  836. regcache_sync(asrc_priv->regmap);
  837. /* Restart enabled pairs */
  838. regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
  839. ASRCTR_ASRCEi_ALL_MASK, asrctr);
  840. return 0;
  841. }
  842. #endif /* CONFIG_PM_SLEEP */
  843. static const struct dev_pm_ops fsl_asrc_pm = {
  844. SET_RUNTIME_PM_OPS(fsl_asrc_runtime_suspend, fsl_asrc_runtime_resume, NULL)
  845. SET_SYSTEM_SLEEP_PM_OPS(fsl_asrc_suspend, fsl_asrc_resume)
  846. };
  847. static const struct of_device_id fsl_asrc_ids[] = {
  848. { .compatible = "fsl,imx35-asrc", },
  849. { .compatible = "fsl,imx53-asrc", },
  850. {}
  851. };
  852. MODULE_DEVICE_TABLE(of, fsl_asrc_ids);
  853. static struct platform_driver fsl_asrc_driver = {
  854. .probe = fsl_asrc_probe,
  855. .driver = {
  856. .name = "fsl-asrc",
  857. .of_match_table = fsl_asrc_ids,
  858. .pm = &fsl_asrc_pm,
  859. },
  860. };
  861. module_platform_driver(fsl_asrc_driver);
  862. MODULE_DESCRIPTION("Freescale ASRC ASoC driver");
  863. MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
  864. MODULE_ALIAS("platform:fsl-asrc");
  865. MODULE_LICENSE("GPL v2");