xtfpga-i2s.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 = READ_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. WRITE_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 = READ_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_component_driver xtfpga_i2s_component = {
  410. .name = DRV_NAME,
  411. .pcm_new = xtfpga_pcm_new,
  412. .ops = &xtfpga_pcm_ops,
  413. };
  414. static const struct snd_soc_dai_ops xtfpga_i2s_dai_ops = {
  415. .startup = xtfpga_i2s_startup,
  416. .hw_params = xtfpga_i2s_hw_params,
  417. .set_fmt = xtfpga_i2s_set_fmt,
  418. };
  419. static struct snd_soc_dai_driver xtfpga_i2s_dai[] = {
  420. {
  421. .name = "xtfpga-i2s",
  422. .id = 0,
  423. .playback = {
  424. .channels_min = 1,
  425. .channels_max = 2,
  426. .rates = SNDRV_PCM_RATE_8000_96000,
  427. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  428. SNDRV_PCM_FMTBIT_S32_LE,
  429. },
  430. .ops = &xtfpga_i2s_dai_ops,
  431. },
  432. };
  433. static int xtfpga_i2s_runtime_suspend(struct device *dev)
  434. {
  435. struct xtfpga_i2s *i2s = dev_get_drvdata(dev);
  436. clk_disable_unprepare(i2s->clk);
  437. return 0;
  438. }
  439. static int xtfpga_i2s_runtime_resume(struct device *dev)
  440. {
  441. struct xtfpga_i2s *i2s = dev_get_drvdata(dev);
  442. int ret;
  443. ret = clk_prepare_enable(i2s->clk);
  444. if (ret) {
  445. dev_err(dev, "clk_prepare_enable failed: %d\n", ret);
  446. return ret;
  447. }
  448. return 0;
  449. }
  450. static int xtfpga_i2s_probe(struct platform_device *pdev)
  451. {
  452. struct xtfpga_i2s *i2s;
  453. struct resource *mem;
  454. int err, irq;
  455. i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
  456. if (!i2s) {
  457. err = -ENOMEM;
  458. goto err;
  459. }
  460. platform_set_drvdata(pdev, i2s);
  461. i2s->dev = &pdev->dev;
  462. dev_dbg(&pdev->dev, "dev: %p, i2s: %p\n", &pdev->dev, i2s);
  463. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  464. i2s->regs = devm_ioremap_resource(&pdev->dev, mem);
  465. if (IS_ERR(i2s->regs)) {
  466. err = PTR_ERR(i2s->regs);
  467. goto err;
  468. }
  469. i2s->regmap = devm_regmap_init_mmio(&pdev->dev, i2s->regs,
  470. &xtfpga_i2s_regmap_config);
  471. if (IS_ERR(i2s->regmap)) {
  472. dev_err(&pdev->dev, "regmap init failed\n");
  473. err = PTR_ERR(i2s->regmap);
  474. goto err;
  475. }
  476. i2s->clk = devm_clk_get(&pdev->dev, NULL);
  477. if (IS_ERR(i2s->clk)) {
  478. dev_err(&pdev->dev, "couldn't get clock\n");
  479. err = PTR_ERR(i2s->clk);
  480. goto err;
  481. }
  482. regmap_write(i2s->regmap, XTFPGA_I2S_CONFIG,
  483. (0x1 << XTFPGA_I2S_CONFIG_CHANNEL_BASE));
  484. regmap_write(i2s->regmap, XTFPGA_I2S_INT_STATUS, XTFPGA_I2S_INT_VALID);
  485. regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK, XTFPGA_I2S_INT_UNDERRUN);
  486. irq = platform_get_irq(pdev, 0);
  487. if (irq < 0) {
  488. dev_err(&pdev->dev, "No IRQ resource\n");
  489. err = irq;
  490. goto err;
  491. }
  492. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  493. xtfpga_i2s_threaded_irq_handler,
  494. IRQF_SHARED | IRQF_ONESHOT,
  495. pdev->name, i2s);
  496. if (err < 0) {
  497. dev_err(&pdev->dev, "request_irq failed\n");
  498. goto err;
  499. }
  500. err = devm_snd_soc_register_component(&pdev->dev,
  501. &xtfpga_i2s_component,
  502. xtfpga_i2s_dai,
  503. ARRAY_SIZE(xtfpga_i2s_dai));
  504. if (err < 0) {
  505. dev_err(&pdev->dev, "couldn't register component\n");
  506. goto err;
  507. }
  508. pm_runtime_enable(&pdev->dev);
  509. if (!pm_runtime_enabled(&pdev->dev)) {
  510. err = xtfpga_i2s_runtime_resume(&pdev->dev);
  511. if (err)
  512. goto err_pm_disable;
  513. }
  514. return 0;
  515. err_pm_disable:
  516. pm_runtime_disable(&pdev->dev);
  517. err:
  518. dev_err(&pdev->dev, "%s: err = %d\n", __func__, err);
  519. return err;
  520. }
  521. static int xtfpga_i2s_remove(struct platform_device *pdev)
  522. {
  523. struct xtfpga_i2s *i2s = dev_get_drvdata(&pdev->dev);
  524. if (i2s->regmap && !IS_ERR(i2s->regmap)) {
  525. regmap_write(i2s->regmap, XTFPGA_I2S_CONFIG, 0);
  526. regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK, 0);
  527. regmap_write(i2s->regmap, XTFPGA_I2S_INT_STATUS,
  528. XTFPGA_I2S_INT_VALID);
  529. }
  530. pm_runtime_disable(&pdev->dev);
  531. if (!pm_runtime_status_suspended(&pdev->dev))
  532. xtfpga_i2s_runtime_suspend(&pdev->dev);
  533. return 0;
  534. }
  535. #ifdef CONFIG_OF
  536. static const struct of_device_id xtfpga_i2s_of_match[] = {
  537. { .compatible = "cdns,xtfpga-i2s", },
  538. {},
  539. };
  540. MODULE_DEVICE_TABLE(of, xtfpga_i2s_of_match);
  541. #endif
  542. static const struct dev_pm_ops xtfpga_i2s_pm_ops = {
  543. SET_RUNTIME_PM_OPS(xtfpga_i2s_runtime_suspend,
  544. xtfpga_i2s_runtime_resume, NULL)
  545. };
  546. static struct platform_driver xtfpga_i2s_driver = {
  547. .probe = xtfpga_i2s_probe,
  548. .remove = xtfpga_i2s_remove,
  549. .driver = {
  550. .name = "xtfpga-i2s",
  551. .of_match_table = of_match_ptr(xtfpga_i2s_of_match),
  552. .pm = &xtfpga_i2s_pm_ops,
  553. },
  554. };
  555. module_platform_driver(xtfpga_i2s_driver);
  556. MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
  557. MODULE_DESCRIPTION("xtfpga I2S controller driver");
  558. MODULE_LICENSE("GPL v2");