Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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> |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 25 | #include <linux/of_dma.h> |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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; |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 49 | void __iomem *base; |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 50 | dma_addr_t slave_addr; |
Kuninori Morimoto | 75bfa5f | 2014-06-18 01:59:35 -0700 | [diff] [blame] | 51 | u32 chcr; |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 61 | struct audmapp_desc { |
| 62 | struct shdma_desc shdma_desc; |
| 63 | dma_addr_t src; |
| 64 | dma_addr_t dst; |
| 65 | }; |
| 66 | |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 67 | #define to_shdma_chan(c) container_of(c, struct shdma_chan, dma_chan) |
| 68 | |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 69 | #define to_chan(chan) container_of(chan, struct audmapp_chan, shdma_chan) |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 70 | #define to_desc(sdesc) container_of(sdesc, struct audmapp_desc, shdma_desc) |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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, |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 104 | struct shdma_desc *sdesc) |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 105 | { |
| 106 | struct audmapp_chan *auchan = to_chan(schan); |
| 107 | struct audmapp_device *audev = to_dev(auchan); |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 108 | struct audmapp_desc *desc = to_desc(sdesc); |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 109 | struct device *dev = audev->dev; |
Kuninori Morimoto | 75bfa5f | 2014-06-18 01:59:35 -0700 | [diff] [blame] | 110 | u32 chcr = auchan->chcr | PDMACHCR_DE; |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 111 | |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 112 | dev_dbg(dev, "src/dst/chcr = %pad/%pad/%08x\n", |
| 113 | &desc->src, &desc->dst, chcr); |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 114 | |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 115 | audmapp_write(auchan, desc->src, PDMASAR); |
| 116 | audmapp_write(auchan, desc->dst, PDMADAR); |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 117 | audmapp_write(auchan, chcr, PDMACHCR); |
| 118 | } |
| 119 | |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 120 | static void audmapp_get_config(struct audmapp_chan *auchan, int slave_id, |
| 121 | u32 *chcr, dma_addr_t *dst) |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 139 | if (slave_id >= AUDMAPP_SLAVE_NUMBER) |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 140 | return; |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 141 | |
| 142 | for (i = 0, cfg = pdata->slave; i < pdata->slave_num; i++, cfg++) |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 143 | if (cfg->slave_id == slave_id) { |
| 144 | *chcr = cfg->chcr; |
| 145 | *dst = cfg->dst; |
| 146 | break; |
| 147 | } |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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); |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 154 | u32 chcr; |
| 155 | dma_addr_t dst; |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 156 | |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 157 | audmapp_get_config(auchan, slave_id, &chcr, &dst); |
| 158 | |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 159 | if (try) |
| 160 | return 0; |
| 161 | |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 162 | auchan->chcr = chcr; |
| 163 | auchan->slave_addr = slave_addr ? : dst; |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int audmapp_desc_setup(struct shdma_chan *schan, |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 169 | struct shdma_desc *sdesc, |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 170 | dma_addr_t src, dma_addr_t dst, size_t *len) |
| 171 | { |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 172 | struct audmapp_desc *desc = to_desc(sdesc); |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 173 | |
| 174 | if (*len > (size_t)AUDMAPP_LEN_MAX) |
| 175 | *len = (size_t)AUDMAPP_LEN_MAX; |
| 176 | |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 177 | desc->src = src; |
| 178 | desc->dst = dst; |
| 179 | |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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 | { |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 190 | struct audmapp_chan *auchan = to_chan(schan); |
| 191 | |
| 192 | return auchan->slave_addr; |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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 | { |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 211 | return &((struct audmapp_desc *)buf)[i].shdma_desc; |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 282 | static int audmapp_probe(struct platform_device *pdev) |
| 283 | { |
| 284 | struct audmapp_pdata *pdata = pdev->dev.platform_data; |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 285 | struct device_node *np = pdev->dev.of_node; |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 292 | if (np) |
| 293 | of_dma_controller_register(np, audmapp_of_xlate, pdev); |
| 294 | else if (!pdata) |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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; |
Kuninori Morimoto | 016b10f | 2014-06-18 01:59:28 -0700 | [diff] [blame] | 311 | sdev->desc_size = sizeof(struct audmapp_desc); |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 356 | static const struct of_device_id audmapp_of_match[] = { |
| 357 | { .compatible = "renesas,rcar-audmapp", }, |
| 358 | {}, |
| 359 | }; |
| 360 | |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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", |
Kuninori Morimoto | caf18c2 | 2014-06-18 01:59:44 -0700 | [diff] [blame] | 367 | .of_match_table = audmapp_of_match, |
Kuninori Morimoto | e43a34e | 2014-03-10 18:11:50 -0700 | [diff] [blame] | 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"); |