Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * bf6xx_sport.c Analog Devices BF6XX SPORT driver |
| 3 | * |
| 4 | * Copyright (c) 2012 Analog Devices Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/dma-mapping.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
| 27 | #include <asm/blackfin.h> |
| 28 | #include <asm/dma.h> |
| 29 | #include <asm/portmux.h> |
| 30 | |
| 31 | #include "bf6xx-sport.h" |
| 32 | |
| 33 | int sport_set_tx_params(struct sport_device *sport, |
| 34 | struct sport_params *params) |
| 35 | { |
| 36 | if (sport->tx_regs->spctl & SPORT_CTL_SPENPRI) |
| 37 | return -EBUSY; |
| 38 | sport->tx_regs->spctl = params->spctl | SPORT_CTL_SPTRAN; |
| 39 | sport->tx_regs->div = params->div; |
| 40 | SSYNC(); |
| 41 | return 0; |
| 42 | } |
| 43 | EXPORT_SYMBOL(sport_set_tx_params); |
| 44 | |
| 45 | int sport_set_rx_params(struct sport_device *sport, |
| 46 | struct sport_params *params) |
| 47 | { |
| 48 | if (sport->rx_regs->spctl & SPORT_CTL_SPENPRI) |
| 49 | return -EBUSY; |
| 50 | sport->rx_regs->spctl = params->spctl & ~SPORT_CTL_SPTRAN; |
| 51 | sport->rx_regs->div = params->div; |
| 52 | SSYNC(); |
| 53 | return 0; |
| 54 | } |
| 55 | EXPORT_SYMBOL(sport_set_rx_params); |
| 56 | |
| 57 | static int compute_wdsize(size_t wdsize) |
| 58 | { |
| 59 | switch (wdsize) { |
| 60 | case 1: |
| 61 | return WDSIZE_8 | PSIZE_8; |
| 62 | case 2: |
| 63 | return WDSIZE_16 | PSIZE_16; |
| 64 | default: |
| 65 | return WDSIZE_32 | PSIZE_32; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void sport_tx_start(struct sport_device *sport) |
| 70 | { |
| 71 | set_dma_next_desc_addr(sport->tx_dma_chan, sport->tx_desc); |
| 72 | set_dma_config(sport->tx_dma_chan, DMAFLOW_LIST | DI_EN |
| 73 | | compute_wdsize(sport->wdsize) | NDSIZE_6); |
| 74 | enable_dma(sport->tx_dma_chan); |
| 75 | sport->tx_regs->spctl |= SPORT_CTL_SPENPRI; |
| 76 | SSYNC(); |
| 77 | } |
| 78 | EXPORT_SYMBOL(sport_tx_start); |
| 79 | |
| 80 | void sport_rx_start(struct sport_device *sport) |
| 81 | { |
| 82 | set_dma_next_desc_addr(sport->rx_dma_chan, sport->rx_desc); |
| 83 | set_dma_config(sport->rx_dma_chan, DMAFLOW_LIST | DI_EN | WNR |
| 84 | | compute_wdsize(sport->wdsize) | NDSIZE_6); |
| 85 | enable_dma(sport->rx_dma_chan); |
| 86 | sport->rx_regs->spctl |= SPORT_CTL_SPENPRI; |
| 87 | SSYNC(); |
| 88 | } |
| 89 | EXPORT_SYMBOL(sport_rx_start); |
| 90 | |
| 91 | void sport_tx_stop(struct sport_device *sport) |
| 92 | { |
| 93 | sport->tx_regs->spctl &= ~SPORT_CTL_SPENPRI; |
| 94 | SSYNC(); |
| 95 | disable_dma(sport->tx_dma_chan); |
| 96 | } |
| 97 | EXPORT_SYMBOL(sport_tx_stop); |
| 98 | |
| 99 | void sport_rx_stop(struct sport_device *sport) |
| 100 | { |
| 101 | sport->rx_regs->spctl &= ~SPORT_CTL_SPENPRI; |
| 102 | SSYNC(); |
| 103 | disable_dma(sport->rx_dma_chan); |
| 104 | } |
| 105 | EXPORT_SYMBOL(sport_rx_stop); |
| 106 | |
| 107 | void sport_set_tx_callback(struct sport_device *sport, |
| 108 | void (*tx_callback)(void *), void *tx_data) |
| 109 | { |
| 110 | sport->tx_callback = tx_callback; |
| 111 | sport->tx_data = tx_data; |
| 112 | } |
| 113 | EXPORT_SYMBOL(sport_set_tx_callback); |
| 114 | |
| 115 | void sport_set_rx_callback(struct sport_device *sport, |
| 116 | void (*rx_callback)(void *), void *rx_data) |
| 117 | { |
| 118 | sport->rx_callback = rx_callback; |
| 119 | sport->rx_data = rx_data; |
| 120 | } |
| 121 | EXPORT_SYMBOL(sport_set_rx_callback); |
| 122 | |
| 123 | static void setup_desc(struct dmasg *desc, void *buf, int fragcount, |
| 124 | size_t fragsize, unsigned int cfg, |
| 125 | unsigned int count, size_t wdsize) |
| 126 | { |
| 127 | |
| 128 | int i; |
| 129 | |
| 130 | for (i = 0; i < fragcount; ++i) { |
| 131 | desc[i].next_desc_addr = &(desc[i + 1]); |
Markus Elfring | 11f25bb | 2017-08-11 22:20:07 +0200 | [diff] [blame] | 132 | desc[i].start_addr = (unsigned long)buf + i * fragsize; |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 133 | desc[i].cfg = cfg; |
| 134 | desc[i].x_count = count; |
| 135 | desc[i].x_modify = wdsize; |
| 136 | desc[i].y_count = 0; |
| 137 | desc[i].y_modify = 0; |
| 138 | } |
| 139 | |
| 140 | /* make circular */ |
Markus Elfring | 11f25bb | 2017-08-11 22:20:07 +0200 | [diff] [blame] | 141 | desc[fragcount - 1].next_desc_addr = desc; |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int sport_config_tx_dma(struct sport_device *sport, void *buf, |
| 145 | int fragcount, size_t fragsize) |
| 146 | { |
| 147 | unsigned int count; |
| 148 | unsigned int cfg; |
| 149 | dma_addr_t addr; |
| 150 | |
Markus Elfring | 11f25bb | 2017-08-11 22:20:07 +0200 | [diff] [blame] | 151 | count = fragsize / sport->wdsize; |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 152 | |
| 153 | if (sport->tx_desc) |
| 154 | dma_free_coherent(NULL, sport->tx_desc_size, |
| 155 | sport->tx_desc, 0); |
| 156 | |
| 157 | sport->tx_desc = dma_alloc_coherent(NULL, |
| 158 | fragcount * sizeof(struct dmasg), &addr, 0); |
| 159 | sport->tx_desc_size = fragcount * sizeof(struct dmasg); |
| 160 | if (!sport->tx_desc) |
| 161 | return -ENOMEM; |
| 162 | |
| 163 | sport->tx_buf = buf; |
| 164 | sport->tx_fragsize = fragsize; |
| 165 | sport->tx_frags = fragcount; |
| 166 | cfg = DMAFLOW_LIST | DI_EN | compute_wdsize(sport->wdsize) | NDSIZE_6; |
| 167 | |
| 168 | setup_desc(sport->tx_desc, buf, fragcount, fragsize, |
Markus Elfring | 11f25bb | 2017-08-11 22:20:07 +0200 | [diff] [blame] | 169 | cfg | DMAEN, count, sport->wdsize); |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | EXPORT_SYMBOL(sport_config_tx_dma); |
| 173 | |
| 174 | int sport_config_rx_dma(struct sport_device *sport, void *buf, |
| 175 | int fragcount, size_t fragsize) |
| 176 | { |
| 177 | unsigned int count; |
| 178 | unsigned int cfg; |
| 179 | dma_addr_t addr; |
| 180 | |
Markus Elfring | 11f25bb | 2017-08-11 22:20:07 +0200 | [diff] [blame] | 181 | count = fragsize / sport->wdsize; |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 182 | |
| 183 | if (sport->rx_desc) |
| 184 | dma_free_coherent(NULL, sport->rx_desc_size, |
| 185 | sport->rx_desc, 0); |
| 186 | |
| 187 | sport->rx_desc = dma_alloc_coherent(NULL, |
| 188 | fragcount * sizeof(struct dmasg), &addr, 0); |
| 189 | sport->rx_desc_size = fragcount * sizeof(struct dmasg); |
| 190 | if (!sport->rx_desc) |
| 191 | return -ENOMEM; |
| 192 | |
| 193 | sport->rx_buf = buf; |
| 194 | sport->rx_fragsize = fragsize; |
| 195 | sport->rx_frags = fragcount; |
| 196 | cfg = DMAFLOW_LIST | DI_EN | compute_wdsize(sport->wdsize) |
| 197 | | WNR | NDSIZE_6; |
| 198 | |
| 199 | setup_desc(sport->rx_desc, buf, fragcount, fragsize, |
Markus Elfring | 11f25bb | 2017-08-11 22:20:07 +0200 | [diff] [blame] | 200 | cfg | DMAEN, count, sport->wdsize); |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | EXPORT_SYMBOL(sport_config_rx_dma); |
| 204 | |
| 205 | unsigned long sport_curr_offset_tx(struct sport_device *sport) |
| 206 | { |
| 207 | unsigned long curr = get_dma_curr_addr(sport->tx_dma_chan); |
| 208 | |
| 209 | return (unsigned char *)curr - sport->tx_buf; |
| 210 | } |
| 211 | EXPORT_SYMBOL(sport_curr_offset_tx); |
| 212 | |
| 213 | unsigned long sport_curr_offset_rx(struct sport_device *sport) |
| 214 | { |
| 215 | unsigned long curr = get_dma_curr_addr(sport->rx_dma_chan); |
| 216 | |
| 217 | return (unsigned char *)curr - sport->rx_buf; |
| 218 | } |
| 219 | EXPORT_SYMBOL(sport_curr_offset_rx); |
| 220 | |
| 221 | static irqreturn_t sport_tx_irq(int irq, void *dev_id) |
| 222 | { |
| 223 | struct sport_device *sport = dev_id; |
| 224 | static unsigned long status; |
| 225 | |
| 226 | status = get_dma_curr_irqstat(sport->tx_dma_chan); |
Markus Elfring | 11f25bb | 2017-08-11 22:20:07 +0200 | [diff] [blame] | 227 | if (status & (DMA_DONE | DMA_ERR)) { |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 228 | clear_dma_irqstat(sport->tx_dma_chan); |
| 229 | SSYNC(); |
| 230 | } |
| 231 | if (sport->tx_callback) |
| 232 | sport->tx_callback(sport->tx_data); |
| 233 | return IRQ_HANDLED; |
| 234 | } |
| 235 | |
| 236 | static irqreturn_t sport_rx_irq(int irq, void *dev_id) |
| 237 | { |
| 238 | struct sport_device *sport = dev_id; |
| 239 | unsigned long status; |
| 240 | |
| 241 | status = get_dma_curr_irqstat(sport->rx_dma_chan); |
Markus Elfring | 11f25bb | 2017-08-11 22:20:07 +0200 | [diff] [blame] | 242 | if (status & (DMA_DONE | DMA_ERR)) { |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 243 | clear_dma_irqstat(sport->rx_dma_chan); |
| 244 | SSYNC(); |
| 245 | } |
| 246 | if (sport->rx_callback) |
| 247 | sport->rx_callback(sport->rx_data); |
| 248 | return IRQ_HANDLED; |
| 249 | } |
| 250 | |
| 251 | static irqreturn_t sport_err_irq(int irq, void *dev_id) |
| 252 | { |
| 253 | struct sport_device *sport = dev_id; |
| 254 | struct device *dev = &sport->pdev->dev; |
| 255 | |
| 256 | if (sport->tx_regs->spctl & SPORT_CTL_DERRPRI) |
Scott Jiang | f4a6348 | 2012-06-21 13:51:58 -0400 | [diff] [blame] | 257 | dev_err(dev, "sport error: TUVF\n"); |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 258 | if (sport->rx_regs->spctl & SPORT_CTL_DERRPRI) |
Scott Jiang | f4a6348 | 2012-06-21 13:51:58 -0400 | [diff] [blame] | 259 | dev_err(dev, "sport error: ROVF\n"); |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 260 | |
| 261 | return IRQ_HANDLED; |
| 262 | } |
| 263 | |
| 264 | static int sport_get_resource(struct sport_device *sport) |
| 265 | { |
| 266 | struct platform_device *pdev = sport->pdev; |
| 267 | struct device *dev = &pdev->dev; |
| 268 | struct bfin_snd_platform_data *pdata = dev->platform_data; |
| 269 | struct resource *res; |
| 270 | |
| 271 | if (!pdata) { |
| 272 | dev_err(dev, "No platform data\n"); |
| 273 | return -ENODEV; |
| 274 | } |
| 275 | sport->pin_req = pdata->pin_req; |
| 276 | |
| 277 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 278 | if (!res) { |
| 279 | dev_err(dev, "No tx MEM resource\n"); |
| 280 | return -ENODEV; |
| 281 | } |
| 282 | sport->tx_regs = (struct sport_register *)res->start; |
| 283 | |
| 284 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 285 | if (!res) { |
| 286 | dev_err(dev, "No rx MEM resource\n"); |
| 287 | return -ENODEV; |
| 288 | } |
| 289 | sport->rx_regs = (struct sport_register *)res->start; |
| 290 | |
| 291 | res = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 292 | if (!res) { |
| 293 | dev_err(dev, "No tx DMA resource\n"); |
| 294 | return -ENODEV; |
| 295 | } |
| 296 | sport->tx_dma_chan = res->start; |
| 297 | |
| 298 | res = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 299 | if (!res) { |
| 300 | dev_err(dev, "No rx DMA resource\n"); |
| 301 | return -ENODEV; |
| 302 | } |
| 303 | sport->rx_dma_chan = res->start; |
| 304 | |
| 305 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 306 | if (!res) { |
| 307 | dev_err(dev, "No tx error irq resource\n"); |
| 308 | return -ENODEV; |
| 309 | } |
| 310 | sport->tx_err_irq = res->start; |
| 311 | |
| 312 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 1); |
| 313 | if (!res) { |
| 314 | dev_err(dev, "No rx error irq resource\n"); |
| 315 | return -ENODEV; |
| 316 | } |
| 317 | sport->rx_err_irq = res->start; |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int sport_request_resource(struct sport_device *sport) |
| 323 | { |
| 324 | struct device *dev = &sport->pdev->dev; |
| 325 | int ret; |
| 326 | |
| 327 | ret = peripheral_request_list(sport->pin_req, "soc-audio"); |
| 328 | if (ret) { |
| 329 | dev_err(dev, "Unable to request sport pin\n"); |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | ret = request_dma(sport->tx_dma_chan, "SPORT TX Data"); |
| 334 | if (ret) { |
| 335 | dev_err(dev, "Unable to allocate DMA channel for sport tx\n"); |
| 336 | goto err_tx_dma; |
| 337 | } |
| 338 | set_dma_callback(sport->tx_dma_chan, sport_tx_irq, sport); |
| 339 | |
| 340 | ret = request_dma(sport->rx_dma_chan, "SPORT RX Data"); |
| 341 | if (ret) { |
| 342 | dev_err(dev, "Unable to allocate DMA channel for sport rx\n"); |
| 343 | goto err_rx_dma; |
| 344 | } |
| 345 | set_dma_callback(sport->rx_dma_chan, sport_rx_irq, sport); |
| 346 | |
| 347 | ret = request_irq(sport->tx_err_irq, sport_err_irq, |
| 348 | 0, "SPORT TX ERROR", sport); |
| 349 | if (ret) { |
| 350 | dev_err(dev, "Unable to allocate tx error IRQ for sport\n"); |
| 351 | goto err_tx_irq; |
| 352 | } |
| 353 | |
| 354 | ret = request_irq(sport->rx_err_irq, sport_err_irq, |
| 355 | 0, "SPORT RX ERROR", sport); |
| 356 | if (ret) { |
| 357 | dev_err(dev, "Unable to allocate rx error IRQ for sport\n"); |
| 358 | goto err_rx_irq; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | err_rx_irq: |
| 363 | free_irq(sport->tx_err_irq, sport); |
| 364 | err_tx_irq: |
| 365 | free_dma(sport->rx_dma_chan); |
| 366 | err_rx_dma: |
| 367 | free_dma(sport->tx_dma_chan); |
| 368 | err_tx_dma: |
| 369 | peripheral_free_list(sport->pin_req); |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | static void sport_free_resource(struct sport_device *sport) |
| 374 | { |
| 375 | free_irq(sport->rx_err_irq, sport); |
| 376 | free_irq(sport->tx_err_irq, sport); |
| 377 | free_dma(sport->rx_dma_chan); |
| 378 | free_dma(sport->tx_dma_chan); |
| 379 | peripheral_free_list(sport->pin_req); |
| 380 | } |
| 381 | |
| 382 | struct sport_device *sport_create(struct platform_device *pdev) |
| 383 | { |
| 384 | struct device *dev = &pdev->dev; |
| 385 | struct sport_device *sport; |
| 386 | int ret; |
| 387 | |
| 388 | sport = kzalloc(sizeof(*sport), GFP_KERNEL); |
Markus Elfring | 60ea039 | 2017-08-11 21:45:37 +0200 | [diff] [blame] | 389 | if (!sport) |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 390 | return NULL; |
Markus Elfring | 60ea039 | 2017-08-11 21:45:37 +0200 | [diff] [blame] | 391 | |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 392 | sport->pdev = pdev; |
| 393 | |
| 394 | ret = sport_get_resource(sport); |
Markus Elfring | 11fca34 | 2017-08-11 22:02:47 +0200 | [diff] [blame] | 395 | if (ret) |
| 396 | goto free_data; |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 397 | |
| 398 | ret = sport_request_resource(sport); |
Markus Elfring | 11fca34 | 2017-08-11 22:02:47 +0200 | [diff] [blame] | 399 | if (ret) |
| 400 | goto free_data; |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 401 | |
| 402 | dev_dbg(dev, "SPORT create success\n"); |
| 403 | return sport; |
Markus Elfring | 11fca34 | 2017-08-11 22:02:47 +0200 | [diff] [blame] | 404 | free_data: |
| 405 | kfree(sport); |
| 406 | return NULL; |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 407 | } |
| 408 | EXPORT_SYMBOL(sport_create); |
| 409 | |
| 410 | void sport_delete(struct sport_device *sport) |
| 411 | { |
Scott Jiang | 8b5eae1 | 2012-08-09 18:08:40 -0400 | [diff] [blame] | 412 | if (sport->tx_desc) |
| 413 | dma_free_coherent(NULL, sport->tx_desc_size, |
| 414 | sport->tx_desc, 0); |
| 415 | if (sport->rx_desc) |
| 416 | dma_free_coherent(NULL, sport->rx_desc_size, |
| 417 | sport->rx_desc, 0); |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 418 | sport_free_resource(sport); |
Scott Jiang | 8b5eae1 | 2012-08-09 18:08:40 -0400 | [diff] [blame] | 419 | kfree(sport); |
Scott Jiang | d2aae47 | 2012-06-20 17:00:30 -0400 | [diff] [blame] | 420 | } |
| 421 | EXPORT_SYMBOL(sport_delete); |
| 422 | |
| 423 | MODULE_DESCRIPTION("Analog Devices BF6XX SPORT driver"); |
| 424 | MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>"); |
| 425 | MODULE_LICENSE("GPL v2"); |