ASoC: amd: fix memory leak of i2s_data on error return
[sfrench/cifs-2.6.git] / sound / soc / amd / raven / acp3x-pcm-dma.c
1 /*
2  * AMD ALSA SoC PCM Driver
3  *
4  * Copyright 2016 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  */
15
16 #include <linux/platform_device.h>
17 #include <linux/module.h>
18 #include <linux/err.h>
19 #include <linux/io.h>
20 #include <linux/pm_runtime.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <sound/soc-dai.h>
25
26 #include "acp3x.h"
27
28 #define DRV_NAME "acp3x-i2s-audio"
29
30 struct i2s_dev_data {
31         bool tdm_mode;
32         unsigned int i2s_irq;
33         u32 tdm_fmt;
34         void __iomem *acp3x_base;
35         struct snd_pcm_substream *play_stream;
36         struct snd_pcm_substream *capture_stream;
37 };
38
39 struct i2s_stream_instance {
40         u16 num_pages;
41         u16 channels;
42         u32 xfer_resolution;
43         struct page *pg;
44         void __iomem *acp3x_base;
45 };
46
47 static const struct snd_pcm_hardware acp3x_pcm_hardware_playback = {
48         .info = SNDRV_PCM_INFO_INTERLEAVED |
49                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
50                 SNDRV_PCM_INFO_BATCH |
51                 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
52         .formats = SNDRV_PCM_FMTBIT_S16_LE |  SNDRV_PCM_FMTBIT_S8 |
53                    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE |
54                    SNDRV_PCM_FMTBIT_S32_LE,
55         .channels_min = 2,
56         .channels_max = 8,
57         .rates = SNDRV_PCM_RATE_8000_96000,
58         .rate_min = 8000,
59         .rate_max = 96000,
60         .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
61         .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
62         .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
63         .periods_min = PLAYBACK_MIN_NUM_PERIODS,
64         .periods_max = PLAYBACK_MAX_NUM_PERIODS,
65 };
66
67 static const struct snd_pcm_hardware acp3x_pcm_hardware_capture = {
68         .info = SNDRV_PCM_INFO_INTERLEAVED |
69                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
70                 SNDRV_PCM_INFO_BATCH |
71             SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
72         .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
73                    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE |
74                    SNDRV_PCM_FMTBIT_S32_LE,
75         .channels_min = 2,
76         .channels_max = 2,
77         .rates = SNDRV_PCM_RATE_8000_48000,
78         .rate_min = 8000,
79         .rate_max = 48000,
80         .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
81         .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
82         .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
83         .periods_min = CAPTURE_MIN_NUM_PERIODS,
84         .periods_max = CAPTURE_MAX_NUM_PERIODS,
85 };
86
87 static int acp3x_power_on(void __iomem *acp3x_base, bool on)
88 {
89         u16 val, mask;
90         u32 timeout;
91
92         if (on == true) {
93                 val = 1;
94                 mask = ACP3x_POWER_ON;
95         } else {
96                 val = 0;
97                 mask = ACP3x_POWER_OFF;
98         }
99
100         rv_writel(val, acp3x_base + mmACP_PGFSM_CONTROL);
101         timeout = 0;
102         while (true) {
103                 val = rv_readl(acp3x_base + mmACP_PGFSM_STATUS);
104                 if ((val & ACP3x_POWER_OFF_IN_PROGRESS) == mask)
105                         break;
106                 if (timeout > 100) {
107                         pr_err("ACP3x power state change failure\n");
108                         return -ENODEV;
109                 }
110                 timeout++;
111                 cpu_relax();
112         }
113         return 0;
114 }
115
116 static int acp3x_reset(void __iomem *acp3x_base)
117 {
118         u32 val, timeout;
119
120         rv_writel(1, acp3x_base + mmACP_SOFT_RESET);
121         timeout = 0;
122         while (true) {
123                 val = rv_readl(acp3x_base + mmACP_SOFT_RESET);
124                 if ((val & ACP3x_SOFT_RESET__SoftResetAudDone_MASK) ||
125                      timeout > 100) {
126                         if (val & ACP3x_SOFT_RESET__SoftResetAudDone_MASK)
127                                 break;
128                         return -ENODEV;
129                 }
130                 timeout++;
131                 cpu_relax();
132         }
133
134         rv_writel(0, acp3x_base + mmACP_SOFT_RESET);
135         timeout = 0;
136         while (true) {
137                 val = rv_readl(acp3x_base + mmACP_SOFT_RESET);
138                 if (!val || timeout > 100) {
139                         if (!val)
140                                 break;
141                         return -ENODEV;
142                 }
143                 timeout++;
144                 cpu_relax();
145         }
146         return 0;
147 }
148
149 static int acp3x_init(void __iomem *acp3x_base)
150 {
151         int ret;
152
153         /* power on */
154         ret = acp3x_power_on(acp3x_base, true);
155         if (ret) {
156                 pr_err("ACP3x power on failed\n");
157                 return ret;
158         }
159         /* Reset */
160         ret = acp3x_reset(acp3x_base);
161         if (ret) {
162                 pr_err("ACP3x reset failed\n");
163                 return ret;
164         }
165         return 0;
166 }
167
168 static int acp3x_deinit(void __iomem *acp3x_base)
169 {
170         int ret;
171
172         /* Reset */
173         ret = acp3x_reset(acp3x_base);
174         if (ret) {
175                 pr_err("ACP3x reset failed\n");
176                 return ret;
177         }
178         /* power off */
179         ret = acp3x_power_on(acp3x_base, false);
180         if (ret) {
181                 pr_err("ACP3x power off failed\n");
182                 return ret;
183         }
184         return 0;
185 }
186
187 static irqreturn_t i2s_irq_handler(int irq, void *dev_id)
188 {
189         u16 play_flag, cap_flag;
190         u32 val;
191         struct i2s_dev_data *rv_i2s_data = dev_id;
192
193         if (!rv_i2s_data)
194                 return IRQ_NONE;
195
196         play_flag = 0;
197         cap_flag = 0;
198         val = rv_readl(rv_i2s_data->acp3x_base + mmACP_EXTERNAL_INTR_STAT);
199         if ((val & BIT(BT_TX_THRESHOLD)) && rv_i2s_data->play_stream) {
200                 rv_writel(BIT(BT_TX_THRESHOLD), rv_i2s_data->acp3x_base +
201                           mmACP_EXTERNAL_INTR_STAT);
202                 snd_pcm_period_elapsed(rv_i2s_data->play_stream);
203                 play_flag = 1;
204         }
205
206         if ((val & BIT(BT_RX_THRESHOLD)) && rv_i2s_data->capture_stream) {
207                 rv_writel(BIT(BT_RX_THRESHOLD), rv_i2s_data->acp3x_base +
208                           mmACP_EXTERNAL_INTR_STAT);
209                 snd_pcm_period_elapsed(rv_i2s_data->capture_stream);
210                 cap_flag = 1;
211         }
212
213         if (play_flag | cap_flag)
214                 return IRQ_HANDLED;
215         else
216                 return IRQ_NONE;
217 }
218
219 static void config_acp3x_dma(struct i2s_stream_instance *rtd, int direction)
220 {
221         u16 page_idx;
222         u64 addr;
223         u32 low, high, val, acp_fifo_addr;
224         struct page *pg = rtd->pg;
225
226         /* 8 scratch registers used to map one 64 bit address */
227         if (direction == SNDRV_PCM_STREAM_PLAYBACK)
228                 val = 0;
229         else
230                 val = rtd->num_pages * 8;
231
232         /* Group Enable */
233         rv_writel(ACP_SRAM_PTE_OFFSET | BIT(31), rtd->acp3x_base +
234                   mmACPAXI2AXI_ATU_BASE_ADDR_GRP_1);
235         rv_writel(PAGE_SIZE_4K_ENABLE, rtd->acp3x_base +
236                   mmACPAXI2AXI_ATU_PAGE_SIZE_GRP_1);
237
238         for (page_idx = 0; page_idx < rtd->num_pages; page_idx++) {
239                 /* Load the low address of page int ACP SRAM through SRBM */
240                 addr = page_to_phys(pg);
241                 low = lower_32_bits(addr);
242                 high = upper_32_bits(addr);
243
244                 rv_writel(low, rtd->acp3x_base + mmACP_SCRATCH_REG_0 + val);
245                 high |= BIT(31);
246                 rv_writel(high, rtd->acp3x_base + mmACP_SCRATCH_REG_0 + val
247                                 + 4);
248                 /* Move to next physically contiguos page */
249                 val += 8;
250                 pg++;
251         }
252
253         if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
254                 /* Config ringbuffer */
255                 rv_writel(MEM_WINDOW_START, rtd->acp3x_base +
256                           mmACP_BT_TX_RINGBUFADDR);
257                 rv_writel(MAX_BUFFER, rtd->acp3x_base +
258                           mmACP_BT_TX_RINGBUFSIZE);
259                 rv_writel(DMA_SIZE, rtd->acp3x_base + mmACP_BT_TX_DMA_SIZE);
260
261                 /* Config audio fifo */
262                 acp_fifo_addr = ACP_SRAM_PTE_OFFSET + (rtd->num_pages * 8)
263                                 + PLAYBACK_FIFO_ADDR_OFFSET;
264                 rv_writel(acp_fifo_addr, rtd->acp3x_base +
265                           mmACP_BT_TX_FIFOADDR);
266                 rv_writel(FIFO_SIZE, rtd->acp3x_base + mmACP_BT_TX_FIFOSIZE);
267         } else {
268                 /* Config ringbuffer */
269                 rv_writel(MEM_WINDOW_START + MAX_BUFFER, rtd->acp3x_base +
270                           mmACP_BT_RX_RINGBUFADDR);
271                 rv_writel(MAX_BUFFER, rtd->acp3x_base +
272                           mmACP_BT_RX_RINGBUFSIZE);
273                 rv_writel(DMA_SIZE, rtd->acp3x_base + mmACP_BT_RX_DMA_SIZE);
274
275                 /* Config audio fifo */
276                 acp_fifo_addr = ACP_SRAM_PTE_OFFSET +
277                                 (rtd->num_pages * 8) + CAPTURE_FIFO_ADDR_OFFSET;
278                 rv_writel(acp_fifo_addr, rtd->acp3x_base +
279                           mmACP_BT_RX_FIFOADDR);
280                 rv_writel(FIFO_SIZE, rtd->acp3x_base + mmACP_BT_RX_FIFOSIZE);
281         }
282
283         /* Enable  watermark/period interrupt to host */
284         rv_writel(BIT(BT_TX_THRESHOLD) | BIT(BT_RX_THRESHOLD),
285                   rtd->acp3x_base + mmACP_EXTERNAL_INTR_CNTL);
286 }
287
288 static int acp3x_dma_open(struct snd_pcm_substream *substream)
289 {
290         int ret = 0;
291
292         struct snd_pcm_runtime *runtime = substream->runtime;
293         struct snd_soc_pcm_runtime *prtd = substream->private_data;
294         struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
295                                                                     DRV_NAME);
296         struct i2s_dev_data *adata = dev_get_drvdata(component->dev);
297
298         struct i2s_stream_instance *i2s_data = kzalloc(sizeof(struct i2s_stream_instance),
299                                                        GFP_KERNEL);
300         if (!i2s_data)
301                 return -EINVAL;
302
303         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
304                 runtime->hw = acp3x_pcm_hardware_playback;
305         else
306                 runtime->hw = acp3x_pcm_hardware_capture;
307
308         ret = snd_pcm_hw_constraint_integer(runtime,
309                                             SNDRV_PCM_HW_PARAM_PERIODS);
310         if (ret < 0) {
311                 dev_err(component->dev, "set integer constraint failed\n");
312                 kfree(i2s_data);
313                 return ret;
314         }
315
316         if (!adata->play_stream && !adata->capture_stream)
317                 rv_writel(1, adata->acp3x_base + mmACP_EXTERNAL_INTR_ENB);
318
319         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
320                 adata->play_stream = substream;
321         else
322                 adata->capture_stream = substream;
323
324         i2s_data->acp3x_base = adata->acp3x_base;
325         runtime->private_data = i2s_data;
326         return 0;
327 }
328
329 static int acp3x_dma_hw_params(struct snd_pcm_substream *substream,
330                                struct snd_pcm_hw_params *params)
331 {
332         int status;
333         u64 size;
334         struct snd_dma_buffer *dma_buffer;
335         struct page *pg;
336         struct snd_pcm_runtime *runtime = substream->runtime;
337         struct i2s_stream_instance *rtd = runtime->private_data;
338
339         if (!rtd)
340                 return -EINVAL;
341
342         dma_buffer = &substream->dma_buffer;
343         size = params_buffer_bytes(params);
344         status = snd_pcm_lib_malloc_pages(substream, size);
345         if (status < 0)
346                 return status;
347
348         memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
349         pg = virt_to_page(substream->dma_buffer.area);
350         if (pg) {
351                 rtd->pg = pg;
352                 rtd->num_pages = (PAGE_ALIGN(size) >> PAGE_SHIFT);
353                 config_acp3x_dma(rtd, substream->stream);
354                 status = 0;
355         } else {
356                 status = -ENOMEM;
357         }
358         return status;
359 }
360
361 static snd_pcm_uframes_t acp3x_dma_pointer(struct snd_pcm_substream *substream)
362 {
363         u32 pos = 0;
364         struct i2s_stream_instance *rtd = substream->runtime->private_data;
365
366         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
367                 pos = rv_readl(rtd->acp3x_base +
368                                mmACP_BT_TX_LINKPOSITIONCNTR);
369         else
370                 pos = rv_readl(rtd->acp3x_base +
371                                mmACP_BT_RX_LINKPOSITIONCNTR);
372
373         if (pos >= MAX_BUFFER)
374                 pos = 0;
375
376         return bytes_to_frames(substream->runtime, pos);
377 }
378
379 static int acp3x_dma_new(struct snd_soc_pcm_runtime *rtd)
380 {
381         return snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
382                                                      SNDRV_DMA_TYPE_DEV,
383                                                      NULL, MIN_BUFFER,
384                                                      MAX_BUFFER);
385 }
386
387 static int acp3x_dma_hw_free(struct snd_pcm_substream *substream)
388 {
389         return snd_pcm_lib_free_pages(substream);
390 }
391
392 static int acp3x_dma_mmap(struct snd_pcm_substream *substream,
393                           struct vm_area_struct *vma)
394 {
395         return snd_pcm_lib_default_mmap(substream, vma);
396 }
397
398 static int acp3x_dma_close(struct snd_pcm_substream *substream)
399 {
400         struct snd_soc_pcm_runtime *prtd = substream->private_data;
401         struct i2s_stream_instance *rtd = substream->runtime->private_data;
402         struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
403                                                                     DRV_NAME);
404         struct i2s_dev_data *adata = dev_get_drvdata(component->dev);
405
406         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
407                 adata->play_stream = NULL;
408         else
409                 adata->capture_stream = NULL;
410
411         /* Disable ACP irq, when the current stream is being closed and
412          * another stream is also not active.
413          */
414         if (!adata->play_stream && !adata->capture_stream)
415                 rv_writel(0, adata->acp3x_base + mmACP_EXTERNAL_INTR_ENB);
416         kfree(rtd);
417         return 0;
418 }
419
420 static struct snd_pcm_ops acp3x_dma_ops = {
421         .open = acp3x_dma_open,
422         .close = acp3x_dma_close,
423         .ioctl = snd_pcm_lib_ioctl,
424         .hw_params = acp3x_dma_hw_params,
425         .hw_free = acp3x_dma_hw_free,
426         .pointer = acp3x_dma_pointer,
427         .mmap = acp3x_dma_mmap,
428 };
429
430
431 static int acp3x_dai_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
432 {
433
434         struct i2s_dev_data *adata = snd_soc_dai_get_drvdata(cpu_dai);
435
436         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
437         case SND_SOC_DAIFMT_I2S:
438                 adata->tdm_mode = false;
439                 break;
440         case SND_SOC_DAIFMT_DSP_A:
441                 adata->tdm_mode = true;
442                 break;
443         default:
444                 return -EINVAL;
445         }
446
447         return 0;
448 }
449
450 static int acp3x_dai_set_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
451                                   u32 rx_mask, int slots, int slot_width)
452 {
453         u32 val = 0;
454         u16 slot_len;
455
456         struct i2s_dev_data *adata = snd_soc_dai_get_drvdata(cpu_dai);
457
458         switch (slot_width) {
459         case SLOT_WIDTH_8:
460                 slot_len = 8;
461                 break;
462         case SLOT_WIDTH_16:
463                 slot_len = 16;
464                 break;
465         case SLOT_WIDTH_24:
466                 slot_len = 24;
467                 break;
468         case SLOT_WIDTH_32:
469                 slot_len = 0;
470                 break;
471         default:
472                 return -EINVAL;
473         }
474
475         val = rv_readl(adata->acp3x_base + mmACP_BTTDM_ITER);
476         rv_writel((val | 0x2), adata->acp3x_base + mmACP_BTTDM_ITER);
477         val = rv_readl(adata->acp3x_base + mmACP_BTTDM_IRER);
478         rv_writel((val | 0x2), adata->acp3x_base + mmACP_BTTDM_IRER);
479
480         val = (FRM_LEN | (slots << 15) | (slot_len << 18));
481         rv_writel(val, adata->acp3x_base + mmACP_BTTDM_TXFRMT);
482         rv_writel(val, adata->acp3x_base + mmACP_BTTDM_RXFRMT);
483
484         adata->tdm_fmt = val;
485         return 0;
486 }
487
488 static int acp3x_dai_i2s_hwparams(struct snd_pcm_substream *substream,
489                                   struct snd_pcm_hw_params *params,
490                                   struct snd_soc_dai *dai)
491 {
492         u32 val = 0;
493         struct i2s_stream_instance *rtd = substream->runtime->private_data;
494
495         switch (params_format(params)) {
496         case SNDRV_PCM_FORMAT_U8:
497         case SNDRV_PCM_FORMAT_S8:
498                 rtd->xfer_resolution = 0x0;
499                 break;
500         case SNDRV_PCM_FORMAT_S16_LE:
501                 rtd->xfer_resolution = 0x02;
502                 break;
503         case SNDRV_PCM_FORMAT_S24_LE:
504                 rtd->xfer_resolution = 0x04;
505                 break;
506         case SNDRV_PCM_FORMAT_S32_LE:
507                 rtd->xfer_resolution = 0x05;
508                 break;
509         default:
510                 return -EINVAL;
511         }
512         val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_ITER);
513         val = val | (rtd->xfer_resolution  << 3);
514         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
515                 rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_ITER);
516         else
517                 rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_IRER);
518
519         return 0;
520 }
521
522 static int acp3x_dai_i2s_trigger(struct snd_pcm_substream *substream,
523                                  int cmd, struct snd_soc_dai *dai)
524 {
525         int ret = 0;
526         struct i2s_stream_instance *rtd = substream->runtime->private_data;
527         u32 val, period_bytes;
528
529         period_bytes = frames_to_bytes(substream->runtime,
530                                        substream->runtime->period_size);
531         switch (cmd) {
532         case SNDRV_PCM_TRIGGER_START:
533         case SNDRV_PCM_TRIGGER_RESUME:
534         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
535                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
536                         rv_writel(period_bytes, rtd->acp3x_base +
537                                   mmACP_BT_TX_INTR_WATERMARK_SIZE);
538                         val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_ITER);
539                         val = val | BIT(0);
540                         rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_ITER);
541                 } else {
542                         rv_writel(period_bytes, rtd->acp3x_base +
543                                   mmACP_BT_RX_INTR_WATERMARK_SIZE);
544                         val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_IRER);
545                         val = val | BIT(0);
546                         rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_IRER);
547                 }
548                 rv_writel(1, rtd->acp3x_base + mmACP_BTTDM_IER);
549                 break;
550         case SNDRV_PCM_TRIGGER_STOP:
551         case SNDRV_PCM_TRIGGER_SUSPEND:
552         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
553                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
554                         val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_ITER);
555                         val = val & ~BIT(0);
556                         rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_ITER);
557                 } else {
558                         val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_IRER);
559                         val = val & ~BIT(0);
560                         rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_IRER);
561                 }
562                 rv_writel(0, rtd->acp3x_base + mmACP_BTTDM_IER);
563                 break;
564         default:
565                 ret = -EINVAL;
566                 break;
567         }
568
569         return ret;
570 }
571
572 struct snd_soc_dai_ops acp3x_dai_i2s_ops = {
573         .hw_params = acp3x_dai_i2s_hwparams,
574         .trigger   = acp3x_dai_i2s_trigger,
575         .set_fmt = acp3x_dai_i2s_set_fmt,
576         .set_tdm_slot = acp3x_dai_set_tdm_slot,
577 };
578
579 static struct snd_soc_dai_driver acp3x_i2s_dai_driver = {
580         .playback = {
581                 .rates = SNDRV_PCM_RATE_8000_96000,
582                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
583                                         SNDRV_PCM_FMTBIT_U8 |
584                                         SNDRV_PCM_FMTBIT_S24_LE |
585                                         SNDRV_PCM_FMTBIT_S32_LE,
586                 .channels_min = 2,
587                 .channels_max = 8,
588
589                 .rate_min = 8000,
590                 .rate_max = 96000,
591         },
592         .capture = {
593                 .rates = SNDRV_PCM_RATE_8000_48000,
594                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
595                                         SNDRV_PCM_FMTBIT_U8 |
596                                         SNDRV_PCM_FMTBIT_S24_LE |
597                                         SNDRV_PCM_FMTBIT_S32_LE,
598                 .channels_min = 2,
599                 .channels_max = 2,
600                 .rate_min = 8000,
601                 .rate_max = 48000,
602         },
603         .ops = &acp3x_dai_i2s_ops,
604 };
605
606 static const struct snd_soc_component_driver acp3x_i2s_component = {
607         .name           = DRV_NAME,
608         .ops            = &acp3x_dma_ops,
609         .pcm_new        = acp3x_dma_new,
610 };
611
612 static int acp3x_audio_probe(struct platform_device *pdev)
613 {
614         int status;
615         struct resource *res;
616         struct i2s_dev_data *adata;
617         unsigned int irqflags;
618
619         if (!pdev->dev.platform_data) {
620                 dev_err(&pdev->dev, "platform_data not retrieved\n");
621                 return -ENODEV;
622         }
623         irqflags = *((unsigned int *)(pdev->dev.platform_data));
624
625         adata = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dev_data),
626                              GFP_KERNEL);
627         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
628         if (!res) {
629                 dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
630                         return -ENODEV;
631         }
632
633         adata->acp3x_base = devm_ioremap(&pdev->dev, res->start,
634                                          resource_size(res));
635
636         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
637         if (!res) {
638                 dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
639                 return -ENODEV;
640         }
641
642         adata->i2s_irq = res->start;
643         adata->play_stream = NULL;
644         adata->capture_stream = NULL;
645
646         dev_set_drvdata(&pdev->dev, adata);
647         /* Initialize ACP */
648         status = acp3x_init(adata->acp3x_base);
649         if (status)
650                 return -ENODEV;
651         status = devm_snd_soc_register_component(&pdev->dev,
652                                                  &acp3x_i2s_component,
653                                                  &acp3x_i2s_dai_driver, 1);
654         if (status) {
655                 dev_err(&pdev->dev, "Fail to register acp i2s dai\n");
656                 goto dev_err;
657         }
658         status = devm_request_irq(&pdev->dev, adata->i2s_irq, i2s_irq_handler,
659                                   irqflags, "ACP3x_I2S_IRQ", adata);
660         if (status) {
661                 dev_err(&pdev->dev, "ACP3x I2S IRQ request failed\n");
662                 goto dev_err;
663         }
664
665         pm_runtime_set_autosuspend_delay(&pdev->dev, 10000);
666         pm_runtime_use_autosuspend(&pdev->dev);
667         pm_runtime_enable(&pdev->dev);
668         return 0;
669 dev_err:
670         status = acp3x_deinit(adata->acp3x_base);
671         if (status)
672                 dev_err(&pdev->dev, "ACP de-init failed\n");
673         else
674                 dev_info(&pdev->dev, "ACP de-initialized\n");
675         /*ignore device status and return driver probe error*/
676         return -ENODEV;
677 }
678
679 static int acp3x_audio_remove(struct platform_device *pdev)
680 {
681         int ret;
682         struct i2s_dev_data *adata = dev_get_drvdata(&pdev->dev);
683
684         ret = acp3x_deinit(adata->acp3x_base);
685         if (ret)
686                 dev_err(&pdev->dev, "ACP de-init failed\n");
687         else
688                 dev_info(&pdev->dev, "ACP de-initialized\n");
689
690         pm_runtime_disable(&pdev->dev);
691         return 0;
692 }
693
694 static int acp3x_resume(struct device *dev)
695 {
696         int status;
697         u32 val;
698         struct i2s_dev_data *adata = dev_get_drvdata(dev);
699
700         status = acp3x_init(adata->acp3x_base);
701         if (status)
702                 return -ENODEV;
703
704         if (adata->play_stream && adata->play_stream->runtime) {
705                 struct i2s_stream_instance *rtd =
706                         adata->play_stream->runtime->private_data;
707                 config_acp3x_dma(rtd, SNDRV_PCM_STREAM_PLAYBACK);
708                 rv_writel((rtd->xfer_resolution  << 3),
709                           rtd->acp3x_base + mmACP_BTTDM_ITER);
710                 if (adata->tdm_mode == true) {
711                         rv_writel(adata->tdm_fmt, adata->acp3x_base +
712                                   mmACP_BTTDM_TXFRMT);
713                         val = rv_readl(adata->acp3x_base + mmACP_BTTDM_ITER);
714                         rv_writel((val | 0x2), adata->acp3x_base +
715                                   mmACP_BTTDM_ITER);
716                 }
717         }
718
719         if (adata->capture_stream && adata->capture_stream->runtime) {
720                 struct i2s_stream_instance *rtd =
721                         adata->capture_stream->runtime->private_data;
722                 config_acp3x_dma(rtd, SNDRV_PCM_STREAM_CAPTURE);
723                 rv_writel((rtd->xfer_resolution  << 3),
724                           rtd->acp3x_base + mmACP_BTTDM_IRER);
725                 if (adata->tdm_mode == true) {
726                         rv_writel(adata->tdm_fmt, adata->acp3x_base +
727                                   mmACP_BTTDM_RXFRMT);
728                         val = rv_readl(adata->acp3x_base + mmACP_BTTDM_IRER);
729                         rv_writel((val | 0x2), adata->acp3x_base +
730                                   mmACP_BTTDM_IRER);
731                 }
732         }
733
734         rv_writel(1, adata->acp3x_base + mmACP_EXTERNAL_INTR_ENB);
735         return 0;
736 }
737
738
739 static int acp3x_pcm_runtime_suspend(struct device *dev)
740 {
741         int status;
742         struct i2s_dev_data *adata = dev_get_drvdata(dev);
743
744         status = acp3x_deinit(adata->acp3x_base);
745         if (status)
746                 dev_err(dev, "ACP de-init failed\n");
747         else
748                 dev_info(dev, "ACP de-initialized\n");
749
750         rv_writel(0, adata->acp3x_base + mmACP_EXTERNAL_INTR_ENB);
751
752         return 0;
753 }
754
755 static int acp3x_pcm_runtime_resume(struct device *dev)
756 {
757         int status;
758         struct i2s_dev_data *adata = dev_get_drvdata(dev);
759
760         status = acp3x_init(adata->acp3x_base);
761         if (status)
762                 return -ENODEV;
763         rv_writel(1, adata->acp3x_base + mmACP_EXTERNAL_INTR_ENB);
764         return 0;
765 }
766
767 static const struct dev_pm_ops acp3x_pm_ops = {
768         .runtime_suspend = acp3x_pcm_runtime_suspend,
769         .runtime_resume = acp3x_pcm_runtime_resume,
770         .resume = acp3x_resume,
771 };
772
773 static struct platform_driver acp3x_dma_driver = {
774         .probe = acp3x_audio_probe,
775         .remove = acp3x_audio_remove,
776         .driver = {
777                 .name = "acp3x_rv_i2s",
778                 .pm = &acp3x_pm_ops,
779         },
780 };
781
782 module_platform_driver(acp3x_dma_driver);
783
784 MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
785 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
786 MODULE_DESCRIPTION("AMD ACP 3.x PCM Driver");
787 MODULE_LICENSE("GPL v2");
788 MODULE_ALIAS("platform:" DRV_NAME);