Merge branch 'parisc-3.17-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[cascardo/linux.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/of_dma.h>
26 #include <linux/platform_data/dma-rcar-audmapp.h>
27 #include <linux/platform_device.h>
28 #include <linux/shdma-base.h>
29
30 /*
31  * DMA register
32  */
33 #define PDMASAR         0x00
34 #define PDMADAR         0x04
35 #define PDMACHCR        0x0c
36
37 /* PDMACHCR */
38 #define PDMACHCR_DE             (1 << 0)
39
40 #define AUDMAPP_MAX_CHANNELS    29
41
42 /* Default MEMCPY transfer size = 2^2 = 4 bytes */
43 #define LOG2_DEFAULT_XFER_SIZE  2
44 #define AUDMAPP_SLAVE_NUMBER    256
45 #define AUDMAPP_LEN_MAX         (16 * 1024 * 1024)
46
47 struct audmapp_chan {
48         struct shdma_chan shdma_chan;
49         void __iomem *base;
50         dma_addr_t slave_addr;
51         u32 chcr;
52 };
53
54 struct audmapp_device {
55         struct shdma_dev shdma_dev;
56         struct audmapp_pdata *pdata;
57         struct device *dev;
58         void __iomem *chan_reg;
59 };
60
61 struct audmapp_desc {
62         struct shdma_desc shdma_desc;
63         dma_addr_t src;
64         dma_addr_t dst;
65 };
66
67 #define to_shdma_chan(c) container_of(c, struct shdma_chan, dma_chan)
68
69 #define to_chan(chan) container_of(chan, struct audmapp_chan, shdma_chan)
70 #define to_desc(sdesc) container_of(sdesc, struct audmapp_desc, shdma_desc)
71 #define to_dev(chan) container_of(chan->shdma_chan.dma_chan.device,     \
72                                   struct audmapp_device, shdma_dev.dma_dev)
73
74 static void audmapp_write(struct audmapp_chan *auchan, u32 data, u32 reg)
75 {
76         struct audmapp_device *audev = to_dev(auchan);
77         struct device *dev = audev->dev;
78
79         dev_dbg(dev, "w %p : %08x\n", auchan->base + reg, data);
80
81         iowrite32(data, auchan->base + reg);
82 }
83
84 static u32 audmapp_read(struct audmapp_chan *auchan, u32 reg)
85 {
86         return ioread32(auchan->base + reg);
87 }
88
89 static void audmapp_halt(struct shdma_chan *schan)
90 {
91         struct audmapp_chan *auchan = to_chan(schan);
92         int i;
93
94         audmapp_write(auchan, 0, PDMACHCR);
95
96         for (i = 0; i < 1024; i++) {
97                 if (0 == audmapp_read(auchan, PDMACHCR))
98                         return;
99                 udelay(1);
100         }
101 }
102
103 static void audmapp_start_xfer(struct shdma_chan *schan,
104                                struct shdma_desc *sdesc)
105 {
106         struct audmapp_chan *auchan = to_chan(schan);
107         struct audmapp_device *audev = to_dev(auchan);
108         struct audmapp_desc *desc = to_desc(sdesc);
109         struct device *dev = audev->dev;
110         u32 chcr = auchan->chcr | PDMACHCR_DE;
111
112         dev_dbg(dev, "src/dst/chcr = %pad/%pad/%08x\n",
113                 &desc->src, &desc->dst, chcr);
114
115         audmapp_write(auchan, desc->src,        PDMASAR);
116         audmapp_write(auchan, desc->dst,        PDMADAR);
117         audmapp_write(auchan, chcr,     PDMACHCR);
118 }
119
120 static void audmapp_get_config(struct audmapp_chan *auchan, int slave_id,
121                               u32 *chcr, dma_addr_t *dst)
122 {
123         struct audmapp_device *audev = to_dev(auchan);
124         struct audmapp_pdata *pdata = audev->pdata;
125         struct audmapp_slave_config *cfg;
126         int i;
127
128         *chcr   = 0;
129         *dst    = 0;
130
131         if (!pdata) { /* DT */
132                 *chcr = ((u32)slave_id) << 16;
133                 auchan->shdma_chan.slave_id = (slave_id) >> 8;
134                 return;
135         }
136
137         /* non-DT */
138
139         if (slave_id >= AUDMAPP_SLAVE_NUMBER)
140                 return;
141
142         for (i = 0, cfg = pdata->slave; i < pdata->slave_num; i++, cfg++)
143                 if (cfg->slave_id == slave_id) {
144                         *chcr   = cfg->chcr;
145                         *dst    = cfg->dst;
146                         break;
147                 }
148 }
149
150 static int audmapp_set_slave(struct shdma_chan *schan, int slave_id,
151                              dma_addr_t slave_addr, bool try)
152 {
153         struct audmapp_chan *auchan = to_chan(schan);
154         u32 chcr;
155         dma_addr_t dst;
156
157         audmapp_get_config(auchan, slave_id, &chcr, &dst);
158
159         if (try)
160                 return 0;
161
162         auchan->chcr            = chcr;
163         auchan->slave_addr      = slave_addr ? : dst;
164
165         return 0;
166 }
167
168 static int audmapp_desc_setup(struct shdma_chan *schan,
169                               struct shdma_desc *sdesc,
170                               dma_addr_t src, dma_addr_t dst, size_t *len)
171 {
172         struct audmapp_desc *desc = to_desc(sdesc);
173
174         if (*len > (size_t)AUDMAPP_LEN_MAX)
175                 *len = (size_t)AUDMAPP_LEN_MAX;
176
177         desc->src = src;
178         desc->dst = dst;
179
180         return 0;
181 }
182
183 static void audmapp_setup_xfer(struct shdma_chan *schan,
184                                int slave_id)
185 {
186 }
187
188 static dma_addr_t audmapp_slave_addr(struct shdma_chan *schan)
189 {
190         struct audmapp_chan *auchan = to_chan(schan);
191
192         return auchan->slave_addr;
193 }
194
195 static bool audmapp_channel_busy(struct shdma_chan *schan)
196 {
197         struct audmapp_chan *auchan = to_chan(schan);
198         u32 chcr = audmapp_read(auchan, PDMACHCR);
199
200         return chcr & ~PDMACHCR_DE;
201 }
202
203 static bool audmapp_desc_completed(struct shdma_chan *schan,
204                                    struct shdma_desc *sdesc)
205 {
206         return true;
207 }
208
209 static struct shdma_desc *audmapp_embedded_desc(void *buf, int i)
210 {
211         return &((struct audmapp_desc *)buf)[i].shdma_desc;
212 }
213
214 static const struct shdma_ops audmapp_shdma_ops = {
215         .halt_channel   = audmapp_halt,
216         .desc_setup     = audmapp_desc_setup,
217         .set_slave      = audmapp_set_slave,
218         .start_xfer     = audmapp_start_xfer,
219         .embedded_desc  = audmapp_embedded_desc,
220         .setup_xfer     = audmapp_setup_xfer,
221         .slave_addr     = audmapp_slave_addr,
222         .channel_busy   = audmapp_channel_busy,
223         .desc_completed = audmapp_desc_completed,
224 };
225
226 static int audmapp_chan_probe(struct platform_device *pdev,
227                               struct audmapp_device *audev, int id)
228 {
229         struct shdma_dev *sdev = &audev->shdma_dev;
230         struct audmapp_chan *auchan;
231         struct shdma_chan *schan;
232         struct device *dev = audev->dev;
233
234         auchan = devm_kzalloc(dev, sizeof(*auchan), GFP_KERNEL);
235         if (!auchan)
236                 return -ENOMEM;
237
238         schan = &auchan->shdma_chan;
239         schan->max_xfer_len = AUDMAPP_LEN_MAX;
240
241         shdma_chan_probe(sdev, schan, id);
242
243         auchan->base = audev->chan_reg + 0x20 + (0x10 * id);
244         dev_dbg(dev, "%02d : %p / %p", id, auchan->base, audev->chan_reg);
245
246         return 0;
247 }
248
249 static void audmapp_chan_remove(struct audmapp_device *audev)
250 {
251         struct dma_device *dma_dev = &audev->shdma_dev.dma_dev;
252         struct shdma_chan *schan;
253         int i;
254
255         shdma_for_each_chan(schan, &audev->shdma_dev, i) {
256                 BUG_ON(!schan);
257                 shdma_chan_remove(schan);
258         }
259         dma_dev->chancnt = 0;
260 }
261
262 static struct dma_chan *audmapp_of_xlate(struct of_phandle_args *dma_spec,
263                                          struct of_dma *ofdma)
264 {
265         dma_cap_mask_t mask;
266         struct dma_chan *chan;
267         u32 chcr = dma_spec->args[0];
268
269         if (dma_spec->args_count != 1)
270                 return NULL;
271
272         dma_cap_zero(mask);
273         dma_cap_set(DMA_SLAVE, mask);
274
275         chan = dma_request_channel(mask, shdma_chan_filter, NULL);
276         if (chan)
277                 to_shdma_chan(chan)->hw_req = chcr;
278
279         return chan;
280 }
281
282 static int audmapp_probe(struct platform_device *pdev)
283 {
284         struct audmapp_pdata *pdata = pdev->dev.platform_data;
285         struct device_node *np = pdev->dev.of_node;
286         struct audmapp_device *audev;
287         struct shdma_dev *sdev;
288         struct dma_device *dma_dev;
289         struct resource *res;
290         int err, i;
291
292         if (np)
293                 of_dma_controller_register(np, audmapp_of_xlate, pdev);
294         else if (!pdata)
295                 return -ENODEV;
296
297         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
298
299         audev = devm_kzalloc(&pdev->dev, sizeof(*audev), GFP_KERNEL);
300         if (!audev)
301                 return -ENOMEM;
302
303         audev->dev      = &pdev->dev;
304         audev->pdata    = pdata;
305         audev->chan_reg = devm_ioremap_resource(&pdev->dev, res);
306         if (IS_ERR(audev->chan_reg))
307                 return PTR_ERR(audev->chan_reg);
308
309         sdev            = &audev->shdma_dev;
310         sdev->ops       = &audmapp_shdma_ops;
311         sdev->desc_size = sizeof(struct audmapp_desc);
312
313         dma_dev                 = &sdev->dma_dev;
314         dma_dev->copy_align     = LOG2_DEFAULT_XFER_SIZE;
315         dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
316
317         err = shdma_init(&pdev->dev, sdev, AUDMAPP_MAX_CHANNELS);
318         if (err < 0)
319                 return err;
320
321         platform_set_drvdata(pdev, audev);
322
323         /* Create DMA Channel */
324         for (i = 0; i < AUDMAPP_MAX_CHANNELS; i++) {
325                 err = audmapp_chan_probe(pdev, audev, i);
326                 if (err)
327                         goto chan_probe_err;
328         }
329
330         err = dma_async_device_register(dma_dev);
331         if (err < 0)
332                 goto chan_probe_err;
333
334         return err;
335
336 chan_probe_err:
337         audmapp_chan_remove(audev);
338         shdma_cleanup(sdev);
339
340         return err;
341 }
342
343 static int audmapp_remove(struct platform_device *pdev)
344 {
345         struct audmapp_device *audev = platform_get_drvdata(pdev);
346         struct dma_device *dma_dev = &audev->shdma_dev.dma_dev;
347
348         dma_async_device_unregister(dma_dev);
349
350         audmapp_chan_remove(audev);
351         shdma_cleanup(&audev->shdma_dev);
352
353         return 0;
354 }
355
356 static const struct of_device_id audmapp_of_match[] = {
357         { .compatible = "renesas,rcar-audmapp", },
358         {},
359 };
360
361 static struct platform_driver audmapp_driver = {
362         .probe          = audmapp_probe,
363         .remove         = audmapp_remove,
364         .driver         = {
365                 .owner  = THIS_MODULE,
366                 .name   = "rcar-audmapp-engine",
367                 .of_match_table = audmapp_of_match,
368         },
369 };
370 module_platform_driver(audmapp_driver);
371
372 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
373 MODULE_DESCRIPTION("Renesas R-Car Audio DMAC peri-peri driver");
374 MODULE_LICENSE("GPL");