Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / dma / sh / rcar-audmapp.c
1 /*
2  * This is for Renesas R-Car Audio-DMAC-peri-peri.
3  *
4  * Copyright (C) 2014 Renesas Electronics Corporation
5  * Copyright (C) 2014 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  *
7  * based on the drivers/dma/sh/shdma.c
8  *
9  * Copyright (C) 2011-2012 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
10  * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
11  * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
12  * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
13  *
14  * This is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  */
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/dmaengine.h>
25 #include <linux/platform_data/dma-rcar-audmapp.h>
26 #include <linux/platform_device.h>
27 #include <linux/shdma-base.h>
28
29 /*
30  * DMA register
31  */
32 #define PDMASAR         0x00
33 #define PDMADAR         0x04
34 #define PDMACHCR        0x0c
35
36 /* PDMACHCR */
37 #define PDMACHCR_DE             (1 << 0)
38
39 #define AUDMAPP_MAX_CHANNELS    29
40
41 /* Default MEMCPY transfer size = 2^2 = 4 bytes */
42 #define LOG2_DEFAULT_XFER_SIZE  2
43 #define AUDMAPP_SLAVE_NUMBER    256
44 #define AUDMAPP_LEN_MAX         (16 * 1024 * 1024)
45
46 struct audmapp_chan {
47         struct shdma_chan shdma_chan;
48         struct audmapp_slave_config *config;
49         void __iomem *base;
50 };
51
52 struct audmapp_device {
53         struct shdma_dev shdma_dev;
54         struct audmapp_pdata *pdata;
55         struct device *dev;
56         void __iomem *chan_reg;
57 };
58
59 #define to_chan(chan) container_of(chan, struct audmapp_chan, shdma_chan)
60 #define to_dev(chan) container_of(chan->shdma_chan.dma_chan.device,     \
61                                   struct audmapp_device, shdma_dev.dma_dev)
62
63 static void audmapp_write(struct audmapp_chan *auchan, u32 data, u32 reg)
64 {
65         struct audmapp_device *audev = to_dev(auchan);
66         struct device *dev = audev->dev;
67
68         dev_dbg(dev, "w %p : %08x\n", auchan->base + reg, data);
69
70         iowrite32(data, auchan->base + reg);
71 }
72
73 static u32 audmapp_read(struct audmapp_chan *auchan, u32 reg)
74 {
75         return ioread32(auchan->base + reg);
76 }
77
78 static void audmapp_halt(struct shdma_chan *schan)
79 {
80         struct audmapp_chan *auchan = to_chan(schan);
81         int i;
82
83         audmapp_write(auchan, 0, PDMACHCR);
84
85         for (i = 0; i < 1024; i++) {
86                 if (0 == audmapp_read(auchan, PDMACHCR))
87                         return;
88                 udelay(1);
89         }
90 }
91
92 static void audmapp_start_xfer(struct shdma_chan *schan,
93                                struct shdma_desc *sdecs)
94 {
95         struct audmapp_chan *auchan = to_chan(schan);
96         struct audmapp_device *audev = to_dev(auchan);
97         struct audmapp_slave_config *cfg = auchan->config;
98         struct device *dev = audev->dev;
99         u32 chcr = cfg->chcr | PDMACHCR_DE;
100
101         dev_dbg(dev, "src/dst/chcr = %pad/%pad/%x\n",
102                 &cfg->src, &cfg->dst, cfg->chcr);
103
104         audmapp_write(auchan, cfg->src, PDMASAR);
105         audmapp_write(auchan, cfg->dst, PDMADAR);
106         audmapp_write(auchan, chcr,     PDMACHCR);
107 }
108
109 static struct audmapp_slave_config *
110 audmapp_find_slave(struct audmapp_chan *auchan, int slave_id)
111 {
112         struct audmapp_device *audev = to_dev(auchan);
113         struct audmapp_pdata *pdata = audev->pdata;
114         struct audmapp_slave_config *cfg;
115         int i;
116
117         if (slave_id >= AUDMAPP_SLAVE_NUMBER)
118                 return NULL;
119
120         for (i = 0, cfg = pdata->slave; i < pdata->slave_num; i++, cfg++)
121                 if (cfg->slave_id == slave_id)
122                         return cfg;
123
124         return NULL;
125 }
126
127 static int audmapp_set_slave(struct shdma_chan *schan, int slave_id,
128                              dma_addr_t slave_addr, bool try)
129 {
130         struct audmapp_chan *auchan = to_chan(schan);
131         struct audmapp_slave_config *cfg =
132                 audmapp_find_slave(auchan, slave_id);
133
134         if (!cfg)
135                 return -ENODEV;
136         if (try)
137                 return 0;
138
139         auchan->config  = cfg;
140
141         return 0;
142 }
143
144 static int audmapp_desc_setup(struct shdma_chan *schan,
145                               struct shdma_desc *sdecs,
146                               dma_addr_t src, dma_addr_t dst, size_t *len)
147 {
148         struct audmapp_chan *auchan = to_chan(schan);
149         struct audmapp_slave_config *cfg = auchan->config;
150
151         if (!cfg)
152                 return -ENODEV;
153
154         if (*len > (size_t)AUDMAPP_LEN_MAX)
155                 *len = (size_t)AUDMAPP_LEN_MAX;
156
157         return 0;
158 }
159
160 static void audmapp_setup_xfer(struct shdma_chan *schan,
161                                int slave_id)
162 {
163 }
164
165 static dma_addr_t audmapp_slave_addr(struct shdma_chan *schan)
166 {
167         return 0; /* always fixed address */
168 }
169
170 static bool audmapp_channel_busy(struct shdma_chan *schan)
171 {
172         struct audmapp_chan *auchan = to_chan(schan);
173         u32 chcr = audmapp_read(auchan, PDMACHCR);
174
175         return chcr & ~PDMACHCR_DE;
176 }
177
178 static bool audmapp_desc_completed(struct shdma_chan *schan,
179                                    struct shdma_desc *sdesc)
180 {
181         return true;
182 }
183
184 static struct shdma_desc *audmapp_embedded_desc(void *buf, int i)
185 {
186         return &((struct shdma_desc *)buf)[i];
187 }
188
189 static const struct shdma_ops audmapp_shdma_ops = {
190         .halt_channel   = audmapp_halt,
191         .desc_setup     = audmapp_desc_setup,
192         .set_slave      = audmapp_set_slave,
193         .start_xfer     = audmapp_start_xfer,
194         .embedded_desc  = audmapp_embedded_desc,
195         .setup_xfer     = audmapp_setup_xfer,
196         .slave_addr     = audmapp_slave_addr,
197         .channel_busy   = audmapp_channel_busy,
198         .desc_completed = audmapp_desc_completed,
199 };
200
201 static int audmapp_chan_probe(struct platform_device *pdev,
202                               struct audmapp_device *audev, int id)
203 {
204         struct shdma_dev *sdev = &audev->shdma_dev;
205         struct audmapp_chan *auchan;
206         struct shdma_chan *schan;
207         struct device *dev = audev->dev;
208
209         auchan = devm_kzalloc(dev, sizeof(*auchan), GFP_KERNEL);
210         if (!auchan)
211                 return -ENOMEM;
212
213         schan = &auchan->shdma_chan;
214         schan->max_xfer_len = AUDMAPP_LEN_MAX;
215
216         shdma_chan_probe(sdev, schan, id);
217
218         auchan->base = audev->chan_reg + 0x20 + (0x10 * id);
219         dev_dbg(dev, "%02d : %p / %p", id, auchan->base, audev->chan_reg);
220
221         return 0;
222 }
223
224 static void audmapp_chan_remove(struct audmapp_device *audev)
225 {
226         struct dma_device *dma_dev = &audev->shdma_dev.dma_dev;
227         struct shdma_chan *schan;
228         int i;
229
230         shdma_for_each_chan(schan, &audev->shdma_dev, i) {
231                 BUG_ON(!schan);
232                 shdma_chan_remove(schan);
233         }
234         dma_dev->chancnt = 0;
235 }
236
237 static int audmapp_probe(struct platform_device *pdev)
238 {
239         struct audmapp_pdata *pdata = pdev->dev.platform_data;
240         struct audmapp_device *audev;
241         struct shdma_dev *sdev;
242         struct dma_device *dma_dev;
243         struct resource *res;
244         int err, i;
245
246         if (!pdata)
247                 return -ENODEV;
248
249         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
250
251         audev = devm_kzalloc(&pdev->dev, sizeof(*audev), GFP_KERNEL);
252         if (!audev)
253                 return -ENOMEM;
254
255         audev->dev      = &pdev->dev;
256         audev->pdata    = pdata;
257         audev->chan_reg = devm_ioremap_resource(&pdev->dev, res);
258         if (IS_ERR(audev->chan_reg))
259                 return PTR_ERR(audev->chan_reg);
260
261         sdev            = &audev->shdma_dev;
262         sdev->ops       = &audmapp_shdma_ops;
263         sdev->desc_size = sizeof(struct shdma_desc);
264
265         dma_dev                 = &sdev->dma_dev;
266         dma_dev->copy_align     = LOG2_DEFAULT_XFER_SIZE;
267         dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
268
269         err = shdma_init(&pdev->dev, sdev, AUDMAPP_MAX_CHANNELS);
270         if (err < 0)
271                 return err;
272
273         platform_set_drvdata(pdev, audev);
274
275         /* Create DMA Channel */
276         for (i = 0; i < AUDMAPP_MAX_CHANNELS; i++) {
277                 err = audmapp_chan_probe(pdev, audev, i);
278                 if (err)
279                         goto chan_probe_err;
280         }
281
282         err = dma_async_device_register(dma_dev);
283         if (err < 0)
284                 goto chan_probe_err;
285
286         return err;
287
288 chan_probe_err:
289         audmapp_chan_remove(audev);
290         shdma_cleanup(sdev);
291
292         return err;
293 }
294
295 static int audmapp_remove(struct platform_device *pdev)
296 {
297         struct audmapp_device *audev = platform_get_drvdata(pdev);
298         struct dma_device *dma_dev = &audev->shdma_dev.dma_dev;
299
300         dma_async_device_unregister(dma_dev);
301
302         audmapp_chan_remove(audev);
303         shdma_cleanup(&audev->shdma_dev);
304
305         return 0;
306 }
307
308 static struct platform_driver audmapp_driver = {
309         .probe          = audmapp_probe,
310         .remove         = audmapp_remove,
311         .driver         = {
312                 .owner  = THIS_MODULE,
313                 .name   = "rcar-audmapp-engine",
314         },
315 };
316 module_platform_driver(audmapp_driver);
317
318 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
319 MODULE_DESCRIPTION("Renesas R-Car Audio DMAC peri-peri driver");
320 MODULE_LICENSE("GPL");