Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mach-pxa/ssp.c |
| 3 | * |
| 4 | * based on linux/arch/arm/mach-sa1100/ssp.c by Russell King |
| 5 | * |
| 6 | * Copyright (C) 2003 Russell King. |
| 7 | * Copyright (C) 2003 Wolfson Microelectronics PLC |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * PXA2xx SSP driver. This provides the generic core for simple |
| 14 | * IO-based SSP applications and allows easy port setup for DMA access. |
| 15 | * |
| 16 | * Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/ioport.h> |
| 26 | #include <linux/init.h> |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 27 | #include <linux/mutex.h> |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 28 | #include <linux/clk.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/platform_device.h> |
Sebastian Andrzej Siewior | 8348c25 | 2010-11-22 17:12:15 -0800 | [diff] [blame] | 31 | #include <linux/spi/pxa2xx_spi.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 32 | #include <linux/io.h> |
Daniel Mack | a6e56c2 | 2013-08-12 10:37:16 +0200 | [diff] [blame] | 33 | #include <linux/of.h> |
| 34 | #include <linux/of_device.h> |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 38 | static DEFINE_MUTEX(ssp_lock); |
| 39 | static LIST_HEAD(ssp_list); |
| 40 | |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 41 | struct ssp_device *pxa_ssp_request(int port, const char *label) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 42 | { |
| 43 | struct ssp_device *ssp = NULL; |
| 44 | |
| 45 | mutex_lock(&ssp_lock); |
| 46 | |
| 47 | list_for_each_entry(ssp, &ssp_list, node) { |
| 48 | if (ssp->port_id == port && ssp->use_count == 0) { |
| 49 | ssp->use_count++; |
| 50 | ssp->label = label; |
| 51 | break; |
| 52 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 55 | mutex_unlock(&ssp_lock); |
| 56 | |
Guennadi Liakhovetski | a4aff22 | 2008-06-05 10:43:14 +0100 | [diff] [blame] | 57 | if (&ssp->node == &ssp_list) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 58 | return NULL; |
| 59 | |
| 60 | return ssp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 62 | EXPORT_SYMBOL(pxa_ssp_request); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 63 | |
Daniel Mack | 6446221 | 2013-08-12 10:37:18 +0200 | [diff] [blame] | 64 | struct ssp_device *pxa_ssp_request_of(const struct device_node *of_node, |
| 65 | const char *label) |
| 66 | { |
| 67 | struct ssp_device *ssp = NULL; |
| 68 | |
| 69 | mutex_lock(&ssp_lock); |
| 70 | |
| 71 | list_for_each_entry(ssp, &ssp_list, node) { |
| 72 | if (ssp->of_node == of_node && ssp->use_count == 0) { |
| 73 | ssp->use_count++; |
| 74 | ssp->label = label; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | mutex_unlock(&ssp_lock); |
| 80 | |
| 81 | if (&ssp->node == &ssp_list) |
| 82 | return NULL; |
| 83 | |
| 84 | return ssp; |
| 85 | } |
| 86 | EXPORT_SYMBOL(pxa_ssp_request_of); |
| 87 | |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 88 | void pxa_ssp_free(struct ssp_device *ssp) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 89 | { |
| 90 | mutex_lock(&ssp_lock); |
| 91 | if (ssp->use_count) { |
| 92 | ssp->use_count--; |
| 93 | ssp->label = NULL; |
| 94 | } else |
| 95 | dev_err(&ssp->pdev->dev, "device already free\n"); |
| 96 | mutex_unlock(&ssp_lock); |
| 97 | } |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 98 | EXPORT_SYMBOL(pxa_ssp_free); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 99 | |
Daniel Mack | a6e56c2 | 2013-08-12 10:37:16 +0200 | [diff] [blame] | 100 | #ifdef CONFIG_OF |
| 101 | static const struct of_device_id pxa_ssp_of_ids[] = { |
| 102 | { .compatible = "mrvl,pxa25x-ssp", .data = (void *) PXA25x_SSP }, |
| 103 | { .compatible = "mvrl,pxa25x-nssp", .data = (void *) PXA25x_NSSP }, |
| 104 | { .compatible = "mrvl,pxa27x-ssp", .data = (void *) PXA27x_SSP }, |
| 105 | { .compatible = "mrvl,pxa3xx-ssp", .data = (void *) PXA3xx_SSP }, |
| 106 | { .compatible = "mvrl,pxa168-ssp", .data = (void *) PXA168_SSP }, |
| 107 | { .compatible = "mrvl,pxa910-ssp", .data = (void *) PXA910_SSP }, |
| 108 | { .compatible = "mrvl,ce4100-ssp", .data = (void *) CE4100_SSP }, |
Daniel Mack | a6e56c2 | 2013-08-12 10:37:16 +0200 | [diff] [blame] | 109 | { }, |
| 110 | }; |
| 111 | MODULE_DEVICE_TABLE(of, pxa_ssp_of_ids); |
| 112 | #endif |
| 113 | |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 114 | static int pxa_ssp_probe(struct platform_device *pdev) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 115 | { |
| 116 | struct resource *res; |
| 117 | struct ssp_device *ssp; |
Daniel Mack | 970d8a7 | 2013-08-12 10:37:15 +0200 | [diff] [blame] | 118 | struct device *dev = &pdev->dev; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 119 | |
Daniel Mack | 1c459de | 2013-08-12 10:37:17 +0200 | [diff] [blame] | 120 | ssp = devm_kzalloc(dev, sizeof(struct ssp_device), GFP_KERNEL); |
Daniel Mack | 64be281 | 2013-08-12 10:37:14 +0200 | [diff] [blame] | 121 | if (ssp == NULL) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 122 | return -ENOMEM; |
Daniel Mack | 64be281 | 2013-08-12 10:37:14 +0200 | [diff] [blame] | 123 | |
Mark Brown | 919dcb2 | 2008-06-19 02:55:52 +0100 | [diff] [blame] | 124 | ssp->pdev = pdev; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 125 | |
Daniel Mack | 1c459de | 2013-08-12 10:37:17 +0200 | [diff] [blame] | 126 | ssp->clk = devm_clk_get(dev, NULL); |
| 127 | if (IS_ERR(ssp->clk)) |
| 128 | return PTR_ERR(ssp->clk); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 129 | |
Daniel Mack | a6e56c2 | 2013-08-12 10:37:16 +0200 | [diff] [blame] | 130 | if (dev->of_node) { |
| 131 | struct of_phandle_args dma_spec; |
| 132 | struct device_node *np = dev->of_node; |
Olof Johansson | d5ece93 | 2013-09-05 12:30:52 -0700 | [diff] [blame] | 133 | int ret; |
Julia Lawall | 077de1a | 2010-03-22 16:11:55 +0800 | [diff] [blame] | 134 | |
Daniel Mack | a6e56c2 | 2013-08-12 10:37:16 +0200 | [diff] [blame] | 135 | /* |
| 136 | * FIXME: we should allocate the DMA channel from this |
| 137 | * context and pass the channel down to the ssp users. |
| 138 | * For now, we lookup the rx and tx indices manually |
| 139 | */ |
| 140 | |
| 141 | /* rx */ |
Olof Johansson | d5ece93 | 2013-09-05 12:30:52 -0700 | [diff] [blame] | 142 | ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", |
| 143 | 0, &dma_spec); |
| 144 | |
| 145 | if (ret) { |
| 146 | dev_err(dev, "Can't parse dmas property\n"); |
| 147 | return -ENODEV; |
| 148 | } |
Daniel Mack | a6e56c2 | 2013-08-12 10:37:16 +0200 | [diff] [blame] | 149 | ssp->drcmr_rx = dma_spec.args[0]; |
| 150 | of_node_put(dma_spec.np); |
| 151 | |
| 152 | /* tx */ |
Olof Johansson | d5ece93 | 2013-09-05 12:30:52 -0700 | [diff] [blame] | 153 | ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", |
| 154 | 1, &dma_spec); |
| 155 | if (ret) { |
| 156 | dev_err(dev, "Can't parse dmas property\n"); |
| 157 | return -ENODEV; |
| 158 | } |
Daniel Mack | a6e56c2 | 2013-08-12 10:37:16 +0200 | [diff] [blame] | 159 | ssp->drcmr_tx = dma_spec.args[0]; |
| 160 | of_node_put(dma_spec.np); |
| 161 | } else { |
| 162 | res = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 163 | if (res == NULL) { |
| 164 | dev_err(dev, "no SSP RX DRCMR defined\n"); |
| 165 | return -ENODEV; |
| 166 | } |
| 167 | ssp->drcmr_rx = res->start; |
| 168 | |
| 169 | res = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 170 | if (res == NULL) { |
| 171 | dev_err(dev, "no SSP TX DRCMR defined\n"); |
| 172 | return -ENODEV; |
| 173 | } |
| 174 | ssp->drcmr_tx = res->start; |
Julia Lawall | 077de1a | 2010-03-22 16:11:55 +0800 | [diff] [blame] | 175 | } |
Julia Lawall | 077de1a | 2010-03-22 16:11:55 +0800 | [diff] [blame] | 176 | |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 177 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 178 | if (res == NULL) { |
Daniel Mack | 970d8a7 | 2013-08-12 10:37:15 +0200 | [diff] [blame] | 179 | dev_err(dev, "no memory resource defined\n"); |
Daniel Mack | 1c459de | 2013-08-12 10:37:17 +0200 | [diff] [blame] | 180 | return -ENODEV; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 181 | } |
| 182 | |
Daniel Mack | 1c459de | 2013-08-12 10:37:17 +0200 | [diff] [blame] | 183 | res = devm_request_mem_region(dev, res->start, resource_size(res), |
| 184 | pdev->name); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 185 | if (res == NULL) { |
Daniel Mack | 970d8a7 | 2013-08-12 10:37:15 +0200 | [diff] [blame] | 186 | dev_err(dev, "failed to request memory resource\n"); |
Daniel Mack | 1c459de | 2013-08-12 10:37:17 +0200 | [diff] [blame] | 187 | return -EBUSY; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | ssp->phys_base = res->start; |
| 191 | |
Daniel Mack | 1c459de | 2013-08-12 10:37:17 +0200 | [diff] [blame] | 192 | ssp->mmio_base = devm_ioremap(dev, res->start, resource_size(res)); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 193 | if (ssp->mmio_base == NULL) { |
Daniel Mack | 970d8a7 | 2013-08-12 10:37:15 +0200 | [diff] [blame] | 194 | dev_err(dev, "failed to ioremap() registers\n"); |
Daniel Mack | 1c459de | 2013-08-12 10:37:17 +0200 | [diff] [blame] | 195 | return -ENODEV; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | ssp->irq = platform_get_irq(pdev, 0); |
| 199 | if (ssp->irq < 0) { |
Daniel Mack | 970d8a7 | 2013-08-12 10:37:15 +0200 | [diff] [blame] | 200 | dev_err(dev, "no IRQ resource defined\n"); |
Daniel Mack | 1c459de | 2013-08-12 10:37:17 +0200 | [diff] [blame] | 201 | return -ENODEV; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Daniel Mack | a6e56c2 | 2013-08-12 10:37:16 +0200 | [diff] [blame] | 204 | if (dev->of_node) { |
| 205 | const struct of_device_id *id = |
| 206 | of_match_device(of_match_ptr(pxa_ssp_of_ids), dev); |
| 207 | ssp->type = (int) id->data; |
| 208 | } else { |
| 209 | const struct platform_device_id *id = |
| 210 | platform_get_device_id(pdev); |
| 211 | ssp->type = (int) id->driver_data; |
| 212 | |
| 213 | /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id |
| 214 | * starts from 0, do a translation here |
| 215 | */ |
| 216 | ssp->port_id = pdev->id + 1; |
| 217 | } |
| 218 | |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 219 | ssp->use_count = 0; |
Daniel Mack | 6446221 | 2013-08-12 10:37:18 +0200 | [diff] [blame] | 220 | ssp->of_node = dev->of_node; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 221 | |
| 222 | mutex_lock(&ssp_lock); |
| 223 | list_add(&ssp->node, &ssp_list); |
| 224 | mutex_unlock(&ssp_lock); |
| 225 | |
| 226 | platform_set_drvdata(pdev, ssp); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 227 | |
Daniel Mack | 1c459de | 2013-08-12 10:37:17 +0200 | [diff] [blame] | 228 | return 0; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 231 | static int pxa_ssp_remove(struct platform_device *pdev) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 232 | { |
| 233 | struct resource *res; |
| 234 | struct ssp_device *ssp; |
| 235 | |
| 236 | ssp = platform_get_drvdata(pdev); |
| 237 | if (ssp == NULL) |
| 238 | return -ENODEV; |
| 239 | |
| 240 | iounmap(ssp->mmio_base); |
| 241 | |
| 242 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Julia Lawall | c8ee5c6 | 2010-03-22 16:16:24 +0800 | [diff] [blame] | 243 | release_mem_region(res->start, resource_size(res)); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 244 | |
| 245 | clk_put(ssp->clk); |
| 246 | |
| 247 | mutex_lock(&ssp_lock); |
| 248 | list_del(&ssp->node); |
| 249 | mutex_unlock(&ssp_lock); |
| 250 | |
| 251 | kfree(ssp); |
| 252 | return 0; |
| 253 | } |
| 254 | |
Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 255 | static const struct platform_device_id ssp_id_table[] = { |
| 256 | { "pxa25x-ssp", PXA25x_SSP }, |
| 257 | { "pxa25x-nssp", PXA25x_NSSP }, |
| 258 | { "pxa27x-ssp", PXA27x_SSP }, |
Daniel Mack | 6f0243a | 2014-08-13 21:59:18 +0200 | [diff] [blame] | 259 | { "pxa3xx-ssp", PXA3xx_SSP }, |
Haojian Zhuang | 7e49922 | 2010-03-19 11:53:17 -0400 | [diff] [blame] | 260 | { "pxa168-ssp", PXA168_SSP }, |
Qiao Zhou | 6017221 | 2012-06-04 10:41:03 +0800 | [diff] [blame] | 261 | { "pxa910-ssp", PXA910_SSP }, |
Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 262 | { }, |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 263 | }; |
| 264 | |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 265 | static struct platform_driver pxa_ssp_driver = { |
| 266 | .probe = pxa_ssp_probe, |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 267 | .remove = pxa_ssp_remove, |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 268 | .driver = { |
Daniel Mack | a6e56c2 | 2013-08-12 10:37:16 +0200 | [diff] [blame] | 269 | .name = "pxa2xx-ssp", |
| 270 | .of_match_table = of_match_ptr(pxa_ssp_of_ids), |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 271 | }, |
Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 272 | .id_table = ssp_id_table, |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | static int __init pxa_ssp_init(void) |
| 276 | { |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 277 | return platform_driver_register(&pxa_ssp_driver); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void __exit pxa_ssp_exit(void) |
| 281 | { |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 282 | platform_driver_unregister(&pxa_ssp_driver); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 283 | } |
| 284 | |
Russell King | cae0554 | 2007-12-10 15:35:54 +0000 | [diff] [blame] | 285 | arch_initcall(pxa_ssp_init); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 286 | module_exit(pxa_ssp_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | MODULE_DESCRIPTION("PXA SSP driver"); |
| 289 | MODULE_AUTHOR("Liam Girdwood"); |
| 290 | MODULE_LICENSE("GPL"); |