xtfpga-i2s.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * Xtfpga I2S controller driver
  3. *
  4. * Copyright (c) 2014 Cadence Design Systems Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #define DRV_NAME "xtfpga-i2s"
  19. #define XTFPGA_I2S_VERSION 0x00
  20. #define XTFPGA_I2S_CONFIG 0x04
  21. #define XTFPGA_I2S_INT_MASK 0x08
  22. #define XTFPGA_I2S_INT_STATUS 0x0c
  23. #define XTFPGA_I2S_CHAN0_DATA 0x10
  24. #define XTFPGA_I2S_CHAN1_DATA 0x14
  25. #define XTFPGA_I2S_CHAN2_DATA 0x18
  26. #define XTFPGA_I2S_CHAN3_DATA 0x1c
  27. #define XTFPGA_I2S_CONFIG_TX_ENABLE 0x1
  28. #define XTFPGA_I2S_CONFIG_INT_ENABLE 0x2
  29. #define XTFPGA_I2S_CONFIG_LEFT 0x4
  30. #define XTFPGA_I2S_CONFIG_RATIO_BASE 8
  31. #define XTFPGA_I2S_CONFIG_RATIO_MASK 0x0000ff00
  32. #define XTFPGA_I2S_CONFIG_RES_BASE 16
  33. #define XTFPGA_I2S_CONFIG_RES_MASK 0x003f0000
  34. #define XTFPGA_I2S_CONFIG_LEVEL_BASE 24
  35. #define XTFPGA_I2S_CONFIG_LEVEL_MASK 0x0f000000
  36. #define XTFPGA_I2S_CONFIG_CHANNEL_BASE 28
  37. #define XTFPGA_I2S_INT_UNDERRUN 0x1
  38. #define XTFPGA_I2S_INT_LEVEL 0x2
  39. #define XTFPGA_I2S_INT_VALID 0x3
  40. #define XTFPGA_I2S_FIFO_SIZE 8192
  41. /*
  42. * I2S controller operation:
  43. *
  44. * Enabling TX: output 1 period of zeros (starting with left channel)
  45. * and then queued data.
  46. *
  47. * Level status and interrupt: whenever FIFO level is below FIFO trigger,
  48. * level status is 1 and an IRQ is asserted (if enabled).
  49. *
  50. * Underrun status and interrupt: whenever FIFO is empty, underrun status
  51. * is 1 and an IRQ is asserted (if enabled).
  52. */
  53. struct xtfpga_i2s {
  54. struct device *dev;
  55. struct clk *clk;
  56. struct regmap *regmap;
  57. void __iomem *regs;
  58. /* current playback substream. NULL if not playing.
  59. *
  60. * Access to that field is synchronized between the interrupt handler
  61. * and userspace through RCU.
  62. *
  63. * Interrupt handler (threaded part) does PIO on substream data in RCU
  64. * read-side critical section. Trigger callback sets and clears the
  65. * pointer when the playback is started and stopped with
  66. * rcu_assign_pointer. When userspace is about to free the playback
  67. * stream in the pcm_close callback it synchronizes with the interrupt
  68. * handler by means of synchronize_rcu call.
  69. */
  70. struct snd_pcm_substream __rcu *tx_substream;
  71. unsigned (*tx_fn)(struct xtfpga_i2s *i2s,
  72. struct snd_pcm_runtime *runtime,
  73. unsigned tx_ptr);
  74. unsigned tx_ptr; /* next frame index in the sample buffer */
  75. /* current fifo level estimate.
  76. * Doesn't have to be perfectly accurate, but must be not less than
  77. * the actual FIFO level in order to avoid stall on push attempt.
  78. */
  79. unsigned tx_fifo_level;
  80. /* FIFO level at which level interrupt occurs */
  81. unsigned tx_fifo_low;
  82. /* maximal FIFO level */
  83. unsigned tx_fifo_high;
  84. };
  85. static bool xtfpga_i2s_wr_reg(struct device *dev, unsigned int reg)
  86. {
  87. return reg >= XTFPGA_I2S_CONFIG;
  88. }
  89. static bool xtfpga_i2s_rd_reg(struct device *dev, unsigned int reg)
  90. {
  91. return reg < XTFPGA_I2S_CHAN0_DATA;
  92. }
  93. static bool xtfpga_i2s_volatile_reg(struct device *dev, unsigned int reg)
  94. {
  95. return reg == XTFPGA_I2S_INT_STATUS;
  96. }
  97. static const struct regmap_config xtfpga_i2s_regmap_config = {
  98. .reg_bits = 32,
  99. .reg_stride = 4,
  100. .val_bits = 32,
  101. .max_register = XTFPGA_I2S_CHAN3_DATA,
  102. .writeable_reg = xtfpga_i2s_wr_reg,
  103. .readable_reg = xtfpga_i2s_rd_reg,
  104. .volatile_reg = xtfpga_i2s_volatile_reg,
  105. .cache_type = REGCACHE_FLAT,
  106. };
  107. /* Generate functions that do PIO from TX DMA area to FIFO for all supported
  108. * stream formats.
  109. * Functions will be called xtfpga_pcm_tx_<channels>x<sample bits>, e.g.
  110. * xtfpga_pcm_tx_2x16 for 16-bit stereo.
  111. *
  112. * FIFO consists of 32-bit words, one word per channel, always 2 channels.
  113. * If I2S interface is configured with smaller sample resolution, only
  114. * the LSB of each word is used.
  115. */
  116. #define xtfpga_pcm_tx_fn(channels, sample_bits) \
  117. static unsigned xtfpga_pcm_tx_##channels##x##sample_bits( \
  118. struct xtfpga_i2s *i2s, struct snd_pcm_runtime *runtime, \
  119. unsigned tx_ptr) \
  120. { \
  121. const u##sample_bits (*p)[channels] = \
  122. (void *)runtime->dma_area; \
  123. \
  124. for (; i2s->tx_fifo_level < i2s->tx_fifo_high; \
  125. i2s->tx_fifo_level += 2) { \
  126. iowrite32(p[tx_ptr][0], \
  127. i2s->regs + XTFPGA_I2S_CHAN0_DATA); \
  128. iowrite32(p[tx_ptr][channels - 1], \
  129. i2s->regs + XTFPGA_I2S_CHAN0_DATA); \
  130. if (++tx_ptr >= runtime->buffer_size) \
  131. tx_ptr = 0; \
  132. } \
  133. return tx_ptr; \
  134. }
  135. xtfpga_pcm_tx_fn(1, 16)
  136. xtfpga_pcm_tx_fn(2, 16)
  137. xtfpga_pcm_tx_fn(1, 32)
  138. xtfpga_pcm_tx_fn(2, 32)
  139. #undef xtfpga_pcm_tx_fn
  140. static bool xtfpga_pcm_push_tx(struct xtfpga_i2s *i2s)
  141. {
  142. struct snd_pcm_substream *tx_substream;
  143. bool tx_active;
  144. rcu_read_lock();
  145. tx_substream = rcu_dereference(i2s->tx_substream);
  146. tx_active = tx_substream && snd_pcm_running(tx_substream);
  147. if (tx_active) {
  148. unsigned tx_ptr = ACCESS_ONCE(i2s->tx_ptr);
  149. unsigned new_tx_ptr = i2s->tx_fn(i2s, tx_substream->runtime,
  150. tx_ptr);
  151. cmpxchg(&i2s->tx_ptr, tx_ptr, new_tx_ptr);
  152. }
  153. rcu_read_unlock();
  154. return tx_active;
  155. }
  156. static void xtfpga_pcm_refill_fifo(struct xtfpga_i2s *i2s)
  157. {
  158. unsigned int_status;
  159. unsigned i;
  160. regmap_read(i2s->regmap, XTFPGA_I2S_INT_STATUS,
  161. &int_status);
  162. for (i = 0; i < 2; ++i) {
  163. bool tx_active = xtfpga_pcm_push_tx(i2s);
  164. regmap_write(i2s->regmap, XTFPGA_I2S_INT_STATUS,
  165. XTFPGA_I2S_INT_VALID);
  166. if (tx_active)
  167. regmap_read(i2s->regmap, XTFPGA_I2S_INT_STATUS,
  168. &int_status);
  169. if (!tx_active ||
  170. !(int_status & XTFPGA_I2S_INT_LEVEL))
  171. break;
  172. /* After the push the level IRQ is still asserted,
  173. * means FIFO level is below tx_fifo_low. Estimate
  174. * it as tx_fifo_low.
  175. */
  176. i2s->tx_fifo_level = i2s->tx_fifo_low;
  177. }
  178. if (!(int_status & XTFPGA_I2S_INT_LEVEL))
  179. regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK,
  180. XTFPGA_I2S_INT_VALID);
  181. else if (!(int_status & XTFPGA_I2S_INT_UNDERRUN))
  182. regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK,
  183. XTFPGA_I2S_INT_UNDERRUN);
  184. if (!(int_status & XTFPGA_I2S_INT_UNDERRUN))
  185. regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG,
  186. XTFPGA_I2S_CONFIG_INT_ENABLE |
  187. XTFPGA_I2S_CONFIG_TX_ENABLE,
  188. XTFPGA_I2S_CONFIG_INT_ENABLE |
  189. XTFPGA_I2S_CONFIG_TX_ENABLE);
  190. else
  191. regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG,
  192. XTFPGA_I2S_CONFIG_INT_ENABLE |
  193. XTFPGA_I2S_CONFIG_TX_ENABLE, 0);
  194. }
  195. static irqreturn_t xtfpga_i2s_threaded_irq_handler(int irq, void *dev_id)
  196. {
  197. struct xtfpga_i2s *i2s = dev_id;
  198. struct snd_pcm_substream *tx_substream;
  199. unsigned config, int_status, int_mask;
  200. regmap_read(i2s->regmap, XTFPGA_I2S_CONFIG, &config);
  201. regmap_read(i2s->regmap, XTFPGA_I2S_INT_MASK, &int_mask);
  202. regmap_read(i2s->regmap, XTFPGA_I2S_INT_STATUS, &int_status);
  203. if (!(config & XTFPGA_I2S_CONFIG_INT_ENABLE) ||
  204. !(int_status & int_mask & XTFPGA_I2S_INT_VALID))
  205. return IRQ_NONE;
  206. /* Update FIFO level estimate in accordance with interrupt status
  207. * register.
  208. */
  209. if (int_status & XTFPGA_I2S_INT_UNDERRUN) {
  210. i2s->tx_fifo_level = 0;
  211. regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG,
  212. XTFPGA_I2S_CONFIG_TX_ENABLE, 0);
  213. } else {
  214. /* The FIFO isn't empty, but is below tx_fifo_low. Estimate
  215. * it as tx_fifo_low.
  216. */
  217. i2s->tx_fifo_level = i2s->tx_fifo_low;
  218. }
  219. rcu_read_lock();
  220. tx_substream = rcu_dereference(i2s->tx_substream);
  221. if (tx_substream && snd_pcm_running(tx_substream)) {
  222. snd_pcm_period_elapsed(tx_substream);
  223. if (int_status & XTFPGA_I2S_INT_UNDERRUN)
  224. dev_dbg_ratelimited(i2s->dev, "%s: underrun\n",
  225. __func__);
  226. }
  227. rcu_read_unlock();
  228. /* Refill FIFO, update allowed IRQ reasons, enable IRQ if FIFO is
  229. * not empty.
  230. */
  231. xtfpga_pcm_refill_fifo(i2s);
  232. return IRQ_HANDLED;
  233. }
  234. static int xtfpga_i2s_startup(struct snd_pcm_substream *substream,
  235. struct snd_soc_dai *dai)
  236. {
  237. struct xtfpga_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  238. snd_soc_dai_set_dma_data(dai, substream, i2s);
  239. return 0;
  240. }
  241. static int xtfpga_i2s_hw_params(struct snd_pcm_substream *substream,
  242. struct snd_pcm_hw_params *params,
  243. struct snd_soc_dai *dai)
  244. {
  245. struct xtfpga_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  246. unsigned srate = params_rate(params);
  247. unsigned channels = params_channels(params);
  248. unsigned period_size = params_period_size(params);
  249. unsigned sample_size = snd_pcm_format_width(params_format(params));
  250. unsigned freq, ratio, level;
  251. int err;
  252. regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG,
  253. XTFPGA_I2S_CONFIG_RES_MASK,
  254. sample_size << XTFPGA_I2S_CONFIG_RES_BASE);
  255. freq = 256 * srate;
  256. err = clk_set_rate(i2s->clk, freq);
  257. if (err < 0)
  258. return err;
  259. /* ratio field of the config register controls MCLK->I2S clock
  260. * derivation: I2S clock = MCLK / (2 * (ratio + 2)).
  261. *
  262. * So with MCLK = 256 * sample rate ratio is 0 for 32 bit stereo
  263. * and 2 for 16 bit stereo.
  264. */
  265. ratio = (freq - (srate * sample_size * 8)) /
  266. (srate * sample_size * 4);
  267. regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG,
  268. XTFPGA_I2S_CONFIG_RATIO_MASK,
  269. ratio << XTFPGA_I2S_CONFIG_RATIO_BASE);
  270. i2s->tx_fifo_low = XTFPGA_I2S_FIFO_SIZE / 2;
  271. /* period_size * 2: FIFO always gets 2 samples per frame */
  272. for (level = 1;
  273. i2s->tx_fifo_low / 2 >= period_size * 2 &&
  274. level < (XTFPGA_I2S_CONFIG_LEVEL_MASK >>
  275. XTFPGA_I2S_CONFIG_LEVEL_BASE); ++level)
  276. i2s->tx_fifo_low /= 2;
  277. i2s->tx_fifo_high = 2 * i2s->tx_fifo_low;
  278. regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG,
  279. XTFPGA_I2S_CONFIG_LEVEL_MASK,
  280. level << XTFPGA_I2S_CONFIG_LEVEL_BASE);
  281. dev_dbg(i2s->dev,
  282. "%s srate: %u, channels: %u, sample_size: %u, period_size: %u\n",
  283. __func__, srate, channels, sample_size, period_size);
  284. dev_dbg(i2s->dev, "%s freq: %u, ratio: %u, level: %u\n",
  285. __func__, freq, ratio, level);
  286. return 0;
  287. }
  288. static int xtfpga_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
  289. unsigned int fmt)
  290. {
  291. if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF)
  292. return -EINVAL;
  293. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  294. return -EINVAL;
  295. if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S)
  296. return -EINVAL;
  297. return 0;
  298. }
  299. /* PCM */
  300. static const struct snd_pcm_hardware xtfpga_pcm_hardware = {
  301. .info = SNDRV_PCM_INFO_INTERLEAVED |
  302. SNDRV_PCM_INFO_MMAP_VALID |
  303. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  304. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  305. SNDRV_PCM_FMTBIT_S32_LE,
  306. .channels_min = 1,
  307. .channels_max = 2,
  308. .period_bytes_min = 2,
  309. .period_bytes_max = XTFPGA_I2S_FIFO_SIZE / 2 * 8,
  310. .periods_min = 2,
  311. .periods_max = XTFPGA_I2S_FIFO_SIZE * 8 / 2,
  312. .buffer_bytes_max = XTFPGA_I2S_FIFO_SIZE * 8,
  313. .fifo_size = 16,
  314. };
  315. static int xtfpga_pcm_open(struct snd_pcm_substream *substream)
  316. {
  317. struct snd_pcm_runtime *runtime = substream->runtime;
  318. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  319. void *p;
  320. snd_soc_set_runtime_hwparams(substream, &xtfpga_pcm_hardware);
  321. p = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  322. runtime->private_data = p;
  323. return 0;
  324. }
  325. static int xtfpga_pcm_close(struct snd_pcm_substream *substream)
  326. {
  327. synchronize_rcu();
  328. return 0;
  329. }
  330. static int xtfpga_pcm_hw_params(struct snd_pcm_substream *substream,
  331. struct snd_pcm_hw_params *hw_params)
  332. {
  333. int ret;
  334. struct snd_pcm_runtime *runtime = substream->runtime;
  335. struct xtfpga_i2s *i2s = runtime->private_data;
  336. unsigned channels = params_channels(hw_params);
  337. switch (channels) {
  338. case 1:
  339. case 2:
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. switch (params_format(hw_params)) {
  345. case SNDRV_PCM_FORMAT_S16_LE:
  346. i2s->tx_fn = (channels == 1) ?
  347. xtfpga_pcm_tx_1x16 :
  348. xtfpga_pcm_tx_2x16;
  349. break;
  350. case SNDRV_PCM_FORMAT_S32_LE:
  351. i2s->tx_fn = (channels == 1) ?
  352. xtfpga_pcm_tx_1x32 :
  353. xtfpga_pcm_tx_2x32;
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. ret = snd_pcm_lib_malloc_pages(substream,
  359. params_buffer_bytes(hw_params));
  360. return ret;
  361. }
  362. static int xtfpga_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  363. {
  364. int ret = 0;
  365. struct snd_pcm_runtime *runtime = substream->runtime;
  366. struct xtfpga_i2s *i2s = runtime->private_data;
  367. switch (cmd) {
  368. case SNDRV_PCM_TRIGGER_START:
  369. case SNDRV_PCM_TRIGGER_RESUME:
  370. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  371. ACCESS_ONCE(i2s->tx_ptr) = 0;
  372. rcu_assign_pointer(i2s->tx_substream, substream);
  373. xtfpga_pcm_refill_fifo(i2s);
  374. break;
  375. case SNDRV_PCM_TRIGGER_STOP:
  376. case SNDRV_PCM_TRIGGER_SUSPEND:
  377. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  378. rcu_assign_pointer(i2s->tx_substream, NULL);
  379. break;
  380. default:
  381. ret = -EINVAL;
  382. break;
  383. }
  384. return ret;
  385. }
  386. static snd_pcm_uframes_t xtfpga_pcm_pointer(struct snd_pcm_substream *substream)
  387. {
  388. struct snd_pcm_runtime *runtime = substream->runtime;
  389. struct xtfpga_i2s *i2s = runtime->private_data;
  390. snd_pcm_uframes_t pos = ACCESS_ONCE(i2s->tx_ptr);
  391. return pos < runtime->buffer_size ? pos : 0;
  392. }
  393. static int xtfpga_pcm_new(struct snd_soc_pcm_runtime *rtd)
  394. {
  395. struct snd_card *card = rtd->card->snd_card;
  396. size_t size = xtfpga_pcm_hardware.buffer_bytes_max;
  397. return snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
  398. SNDRV_DMA_TYPE_DEV,
  399. card->dev, size, size);
  400. }
  401. static const struct snd_pcm_ops xtfpga_pcm_ops = {
  402. .open = xtfpga_pcm_open,
  403. .close = xtfpga_pcm_close,
  404. .ioctl = snd_pcm_lib_ioctl,
  405. .hw_params = xtfpga_pcm_hw_params,
  406. .trigger = xtfpga_pcm_trigger,
  407. .pointer = xtfpga_pcm_pointer,
  408. };
  409. static const struct snd_soc_platform_driver xtfpga_soc_platform = {
  410. .pcm_new = xtfpga_pcm_new,
  411. .ops = &xtfpga_pcm_ops,
  412. };
  413. static const struct snd_soc_component_driver xtfpga_i2s_component = {
  414. .name = DRV_NAME,
  415. };
  416. static const struct snd_soc_dai_ops xtfpga_i2s_dai_ops = {
  417. .startup = xtfpga_i2s_startup,
  418. .hw_params = xtfpga_i2s_hw_params,
  419. .set_fmt = xtfpga_i2s_set_fmt,
  420. };
  421. static struct snd_soc_dai_driver xtfpga_i2s_dai[] = {
  422. {
  423. .name = "xtfpga-i2s",
  424. .id = 0,
  425. .playback = {
  426. .channels_min = 1,
  427. .channels_max = 2,
  428. .rates = SNDRV_PCM_RATE_8000_96000,
  429. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  430. SNDRV_PCM_FMTBIT_S32_LE,
  431. },
  432. .ops = &xtfpga_i2s_dai_ops,
  433. },
  434. };
  435. static int xtfpga_i2s_runtime_suspend(struct device *dev)
  436. {
  437. struct xtfpga_i2s *i2s = dev_get_drvdata(dev);
  438. clk_disable_unprepare(i2s->clk);
  439. return 0;
  440. }
  441. static int xtfpga_i2s_runtime_resume(struct device *dev)
  442. {
  443. struct xtfpga_i2s *i2s = dev_get_drvdata(dev);
  444. int ret;
  445. ret = clk_prepare_enable(i2s->clk);
  446. if (ret) {
  447. dev_err(dev, "clk_prepare_enable failed: %d\n", ret);
  448. return ret;
  449. }
  450. return 0;
  451. }
  452. static int xtfpga_i2s_probe(struct platform_device *pdev)
  453. {
  454. struct xtfpga_i2s *i2s;
  455. struct resource *mem;
  456. int err, irq;
  457. i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
  458. if (!i2s) {
  459. err = -ENOMEM;
  460. goto err;
  461. }
  462. platform_set_drvdata(pdev, i2s);
  463. i2s->dev = &pdev->dev;
  464. dev_dbg(&pdev->dev, "dev: %p, i2s: %p\n", &pdev->dev, i2s);
  465. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  466. i2s->regs = devm_ioremap_resource(&pdev->dev, mem);
  467. if (IS_ERR(i2s->regs)) {
  468. err = PTR_ERR(i2s->regs);
  469. goto err;
  470. }
  471. i2s->regmap = devm_regmap_init_mmio(&pdev->dev, i2s->regs,
  472. &xtfpga_i2s_regmap_config);
  473. if (IS_ERR(i2s->regmap)) {
  474. dev_err(&pdev->dev, "regmap init failed\n");
  475. err = PTR_ERR(i2s->regmap);
  476. goto err;
  477. }
  478. i2s->clk = devm_clk_get(&pdev->dev, NULL);
  479. if (IS_ERR(i2s->clk)) {
  480. dev_err(&pdev->dev, "couldn't get clock\n");
  481. err = PTR_ERR(i2s->clk);
  482. goto err;
  483. }
  484. regmap_write(i2s->regmap, XTFPGA_I2S_CONFIG,
  485. (0x1 << XTFPGA_I2S_CONFIG_CHANNEL_BASE));
  486. regmap_write(i2s->regmap, XTFPGA_I2S_INT_STATUS, XTFPGA_I2S_INT_VALID);
  487. regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK, XTFPGA_I2S_INT_UNDERRUN);
  488. irq = platform_get_irq(pdev, 0);
  489. if (irq < 0) {
  490. dev_err(&pdev->dev, "No IRQ resource\n");
  491. err = irq;
  492. goto err;
  493. }
  494. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  495. xtfpga_i2s_threaded_irq_handler,
  496. IRQF_SHARED | IRQF_ONESHOT,
  497. pdev->name, i2s);
  498. if (err < 0) {
  499. dev_err(&pdev->dev, "request_irq failed\n");
  500. goto err;
  501. }
  502. err = snd_soc_register_platform(&pdev->dev, &xtfpga_soc_platform);
  503. if (err < 0) {
  504. dev_err(&pdev->dev, "couldn't register platform\n");
  505. goto err;
  506. }
  507. err = devm_snd_soc_register_component(&pdev->dev,
  508. &xtfpga_i2s_component,
  509. xtfpga_i2s_dai,
  510. ARRAY_SIZE(xtfpga_i2s_dai));
  511. if (err < 0) {
  512. dev_err(&pdev->dev, "couldn't register component\n");
  513. goto err_unregister_platform;
  514. }
  515. pm_runtime_enable(&pdev->dev);
  516. if (!pm_runtime_enabled(&pdev->dev)) {
  517. err = xtfpga_i2s_runtime_resume(&pdev->dev);
  518. if (err)
  519. goto err_pm_disable;
  520. }
  521. return 0;
  522. err_pm_disable:
  523. pm_runtime_disable(&pdev->dev);
  524. err_unregister_platform:
  525. snd_soc_unregister_platform(&pdev->dev);
  526. err:
  527. dev_err(&pdev->dev, "%s: err = %d\n", __func__, err);
  528. return err;
  529. }
  530. static int xtfpga_i2s_remove(struct platform_device *pdev)
  531. {
  532. struct xtfpga_i2s *i2s = dev_get_drvdata(&pdev->dev);
  533. snd_soc_unregister_platform(&pdev->dev);
  534. if (i2s->regmap && !IS_ERR(i2s->regmap)) {
  535. regmap_write(i2s->regmap, XTFPGA_I2S_CONFIG, 0);
  536. regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK, 0);
  537. regmap_write(i2s->regmap, XTFPGA_I2S_INT_STATUS,
  538. XTFPGA_I2S_INT_VALID);
  539. }
  540. pm_runtime_disable(&pdev->dev);
  541. if (!pm_runtime_status_suspended(&pdev->dev))
  542. xtfpga_i2s_runtime_suspend(&pdev->dev);
  543. return 0;
  544. }
  545. #ifdef CONFIG_OF
  546. static const struct of_device_id xtfpga_i2s_of_match[] = {
  547. { .compatible = "cdns,xtfpga-i2s", },
  548. {},
  549. };
  550. MODULE_DEVICE_TABLE(of, xtfpga_i2s_of_match);
  551. #endif
  552. static const struct dev_pm_ops xtfpga_i2s_pm_ops = {
  553. SET_RUNTIME_PM_OPS(xtfpga_i2s_runtime_suspend,
  554. xtfpga_i2s_runtime_resume, NULL)
  555. };
  556. static struct platform_driver xtfpga_i2s_driver = {
  557. .probe = xtfpga_i2s_probe,
  558. .remove = xtfpga_i2s_remove,
  559. .driver = {
  560. .name = "xtfpga-i2s",
  561. .of_match_table = of_match_ptr(xtfpga_i2s_of_match),
  562. .pm = &xtfpga_i2s_pm_ops,
  563. },
  564. };
  565. module_platform_driver(xtfpga_i2s_driver);
  566. MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
  567. MODULE_DESCRIPTION("xtfpga I2S controller driver");
  568. MODULE_LICENSE("GPL v2");