blob: dabbf0aba2e9b5195625ffee4e8508bef3e087b4 [file] [log] [blame]
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -07001/*
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>
Kuninori Morimotocaf18c22014-06-18 01:59:44 -070025#include <linux/of_dma.h>
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -070026#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
47struct audmapp_chan {
48 struct shdma_chan shdma_chan;
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -070049 void __iomem *base;
Kuninori Morimoto016b10f2014-06-18 01:59:28 -070050 dma_addr_t slave_addr;
Kuninori Morimoto75bfa5f2014-06-18 01:59:35 -070051 u32 chcr;
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -070052};
53
54struct audmapp_device {
55 struct shdma_dev shdma_dev;
56 struct audmapp_pdata *pdata;
57 struct device *dev;
58 void __iomem *chan_reg;
59};
60
Kuninori Morimoto016b10f2014-06-18 01:59:28 -070061struct audmapp_desc {
62 struct shdma_desc shdma_desc;
63 dma_addr_t src;
64 dma_addr_t dst;
65};
66
Kuninori Morimotocaf18c22014-06-18 01:59:44 -070067#define to_shdma_chan(c) container_of(c, struct shdma_chan, dma_chan)
68
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -070069#define to_chan(chan) container_of(chan, struct audmapp_chan, shdma_chan)
Kuninori Morimoto016b10f2014-06-18 01:59:28 -070070#define to_desc(sdesc) container_of(sdesc, struct audmapp_desc, shdma_desc)
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -070071#define to_dev(chan) container_of(chan->shdma_chan.dma_chan.device, \
72 struct audmapp_device, shdma_dev.dma_dev)
73
74static 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
84static u32 audmapp_read(struct audmapp_chan *auchan, u32 reg)
85{
86 return ioread32(auchan->base + reg);
87}
88
89static 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
103static void audmapp_start_xfer(struct shdma_chan *schan,
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700104 struct shdma_desc *sdesc)
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700105{
106 struct audmapp_chan *auchan = to_chan(schan);
107 struct audmapp_device *audev = to_dev(auchan);
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700108 struct audmapp_desc *desc = to_desc(sdesc);
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700109 struct device *dev = audev->dev;
Kuninori Morimoto75bfa5f2014-06-18 01:59:35 -0700110 u32 chcr = auchan->chcr | PDMACHCR_DE;
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700111
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700112 dev_dbg(dev, "src/dst/chcr = %pad/%pad/%08x\n",
113 &desc->src, &desc->dst, chcr);
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700114
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700115 audmapp_write(auchan, desc->src, PDMASAR);
116 audmapp_write(auchan, desc->dst, PDMADAR);
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700117 audmapp_write(auchan, chcr, PDMACHCR);
118}
119
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700120static void audmapp_get_config(struct audmapp_chan *auchan, int slave_id,
121 u32 *chcr, dma_addr_t *dst)
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700122{
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
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700128 *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
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700139 if (slave_id >= AUDMAPP_SLAVE_NUMBER)
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700140 return;
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700141
142 for (i = 0, cfg = pdata->slave; i < pdata->slave_num; i++, cfg++)
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700143 if (cfg->slave_id == slave_id) {
144 *chcr = cfg->chcr;
145 *dst = cfg->dst;
146 break;
147 }
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700148}
149
150static 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);
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700154 u32 chcr;
155 dma_addr_t dst;
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700156
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700157 audmapp_get_config(auchan, slave_id, &chcr, &dst);
158
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700159 if (try)
160 return 0;
161
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700162 auchan->chcr = chcr;
163 auchan->slave_addr = slave_addr ? : dst;
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700164
165 return 0;
166}
167
168static int audmapp_desc_setup(struct shdma_chan *schan,
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700169 struct shdma_desc *sdesc,
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700170 dma_addr_t src, dma_addr_t dst, size_t *len)
171{
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700172 struct audmapp_desc *desc = to_desc(sdesc);
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700173
174 if (*len > (size_t)AUDMAPP_LEN_MAX)
175 *len = (size_t)AUDMAPP_LEN_MAX;
176
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700177 desc->src = src;
178 desc->dst = dst;
179
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700180 return 0;
181}
182
183static void audmapp_setup_xfer(struct shdma_chan *schan,
184 int slave_id)
185{
186}
187
188static dma_addr_t audmapp_slave_addr(struct shdma_chan *schan)
189{
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700190 struct audmapp_chan *auchan = to_chan(schan);
191
192 return auchan->slave_addr;
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700193}
194
195static 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
203static bool audmapp_desc_completed(struct shdma_chan *schan,
204 struct shdma_desc *sdesc)
205{
206 return true;
207}
208
209static struct shdma_desc *audmapp_embedded_desc(void *buf, int i)
210{
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700211 return &((struct audmapp_desc *)buf)[i].shdma_desc;
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700212}
213
214static 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
226static 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
249static 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
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700262static 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
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700282static int audmapp_probe(struct platform_device *pdev)
283{
284 struct audmapp_pdata *pdata = pdev->dev.platform_data;
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700285 struct device_node *np = pdev->dev.of_node;
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700286 struct audmapp_device *audev;
287 struct shdma_dev *sdev;
288 struct dma_device *dma_dev;
289 struct resource *res;
290 int err, i;
291
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700292 if (np)
293 of_dma_controller_register(np, audmapp_of_xlate, pdev);
294 else if (!pdata)
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700295 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;
Kuninori Morimoto016b10f2014-06-18 01:59:28 -0700311 sdev->desc_size = sizeof(struct audmapp_desc);
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700312
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
336chan_probe_err:
337 audmapp_chan_remove(audev);
338 shdma_cleanup(sdev);
339
340 return err;
341}
342
343static 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
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700356static const struct of_device_id audmapp_of_match[] = {
357 { .compatible = "renesas,rcar-audmapp", },
358 {},
359};
360
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700361static struct platform_driver audmapp_driver = {
362 .probe = audmapp_probe,
363 .remove = audmapp_remove,
364 .driver = {
365 .owner = THIS_MODULE,
366 .name = "rcar-audmapp-engine",
Kuninori Morimotocaf18c22014-06-18 01:59:44 -0700367 .of_match_table = audmapp_of_match,
Kuninori Morimotoe43a34e2014-03-10 18:11:50 -0700368 },
369};
370module_platform_driver(audmapp_driver);
371
372MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
373MODULE_DESCRIPTION("Renesas R-Car Audio DMAC peri-peri driver");
374MODULE_LICENSE("GPL");