fsl_asrc.c 29 KB

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