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> |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/irq.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 35 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 37 | static DEFINE_MUTEX(ssp_lock); |
| 38 | static LIST_HEAD(ssp_list); |
| 39 | |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 40 | struct ssp_device *pxa_ssp_request(int port, const char *label) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 41 | { |
| 42 | struct ssp_device *ssp = NULL; |
| 43 | |
| 44 | mutex_lock(&ssp_lock); |
| 45 | |
| 46 | list_for_each_entry(ssp, &ssp_list, node) { |
| 47 | if (ssp->port_id == port && ssp->use_count == 0) { |
| 48 | ssp->use_count++; |
| 49 | ssp->label = label; |
| 50 | break; |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 54 | mutex_unlock(&ssp_lock); |
| 55 | |
Guennadi Liakhovetski | a4aff22 | 2008-06-05 10:43:14 +0100 | [diff] [blame] | 56 | if (&ssp->node == &ssp_list) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 57 | return NULL; |
| 58 | |
| 59 | return ssp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 61 | EXPORT_SYMBOL(pxa_ssp_request); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 62 | |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 63 | void pxa_ssp_free(struct ssp_device *ssp) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 64 | { |
| 65 | mutex_lock(&ssp_lock); |
| 66 | if (ssp->use_count) { |
| 67 | ssp->use_count--; |
| 68 | ssp->label = NULL; |
| 69 | } else |
| 70 | dev_err(&ssp->pdev->dev, "device already free\n"); |
| 71 | mutex_unlock(&ssp_lock); |
| 72 | } |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 73 | EXPORT_SYMBOL(pxa_ssp_free); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 74 | |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 75 | static int pxa_ssp_probe(struct platform_device *pdev) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 76 | { |
Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 77 | const struct platform_device_id *id = platform_get_device_id(pdev); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 78 | struct resource *res; |
| 79 | struct ssp_device *ssp; |
| 80 | int ret = 0; |
| 81 | |
| 82 | ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL); |
Daniel Mack | 64be281 | 2013-08-12 10:37:14 +0200 | [diff] [blame^] | 83 | if (ssp == NULL) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 84 | return -ENOMEM; |
Daniel Mack | 64be281 | 2013-08-12 10:37:14 +0200 | [diff] [blame^] | 85 | |
Mark Brown | 919dcb2 | 2008-06-19 02:55:52 +0100 | [diff] [blame] | 86 | ssp->pdev = pdev; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 87 | |
Russell King | e0d8b13 | 2008-11-11 17:52:32 +0000 | [diff] [blame] | 88 | ssp->clk = clk_get(&pdev->dev, NULL); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 89 | if (IS_ERR(ssp->clk)) { |
| 90 | ret = PTR_ERR(ssp->clk); |
| 91 | goto err_free; |
| 92 | } |
| 93 | |
Julia Lawall | 077de1a | 2010-03-22 16:11:55 +0800 | [diff] [blame] | 94 | res = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 95 | if (res == NULL) { |
| 96 | dev_err(&pdev->dev, "no SSP RX DRCMR defined\n"); |
| 97 | ret = -ENODEV; |
| 98 | goto err_free_clk; |
| 99 | } |
| 100 | ssp->drcmr_rx = res->start; |
| 101 | |
| 102 | res = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 103 | if (res == NULL) { |
| 104 | dev_err(&pdev->dev, "no SSP TX DRCMR defined\n"); |
| 105 | ret = -ENODEV; |
| 106 | goto err_free_clk; |
| 107 | } |
| 108 | ssp->drcmr_tx = res->start; |
| 109 | |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 110 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 111 | if (res == NULL) { |
| 112 | dev_err(&pdev->dev, "no memory resource defined\n"); |
| 113 | ret = -ENODEV; |
| 114 | goto err_free_clk; |
| 115 | } |
| 116 | |
Julia Lawall | c8ee5c6 | 2010-03-22 16:16:24 +0800 | [diff] [blame] | 117 | res = request_mem_region(res->start, resource_size(res), |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 118 | pdev->name); |
| 119 | if (res == NULL) { |
| 120 | dev_err(&pdev->dev, "failed to request memory resource\n"); |
| 121 | ret = -EBUSY; |
| 122 | goto err_free_clk; |
| 123 | } |
| 124 | |
| 125 | ssp->phys_base = res->start; |
| 126 | |
Julia Lawall | c8ee5c6 | 2010-03-22 16:16:24 +0800 | [diff] [blame] | 127 | ssp->mmio_base = ioremap(res->start, resource_size(res)); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 128 | if (ssp->mmio_base == NULL) { |
| 129 | dev_err(&pdev->dev, "failed to ioremap() registers\n"); |
| 130 | ret = -ENODEV; |
| 131 | goto err_free_mem; |
| 132 | } |
| 133 | |
| 134 | ssp->irq = platform_get_irq(pdev, 0); |
| 135 | if (ssp->irq < 0) { |
| 136 | dev_err(&pdev->dev, "no IRQ resource defined\n"); |
| 137 | ret = -ENODEV; |
| 138 | goto err_free_io; |
| 139 | } |
| 140 | |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 141 | /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id |
| 142 | * starts from 0, do a translation here |
| 143 | */ |
| 144 | ssp->port_id = pdev->id + 1; |
| 145 | ssp->use_count = 0; |
Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 146 | ssp->type = (int)id->driver_data; |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 147 | |
| 148 | mutex_lock(&ssp_lock); |
| 149 | list_add(&ssp->node, &ssp_list); |
| 150 | mutex_unlock(&ssp_lock); |
| 151 | |
| 152 | platform_set_drvdata(pdev, ssp); |
| 153 | return 0; |
| 154 | |
| 155 | err_free_io: |
| 156 | iounmap(ssp->mmio_base); |
| 157 | err_free_mem: |
Julia Lawall | c8ee5c6 | 2010-03-22 16:16:24 +0800 | [diff] [blame] | 158 | release_mem_region(res->start, resource_size(res)); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 159 | err_free_clk: |
| 160 | clk_put(ssp->clk); |
| 161 | err_free: |
| 162 | kfree(ssp); |
| 163 | return ret; |
| 164 | } |
| 165 | |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 166 | static int pxa_ssp_remove(struct platform_device *pdev) |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 167 | { |
| 168 | struct resource *res; |
| 169 | struct ssp_device *ssp; |
| 170 | |
| 171 | ssp = platform_get_drvdata(pdev); |
| 172 | if (ssp == NULL) |
| 173 | return -ENODEV; |
| 174 | |
| 175 | iounmap(ssp->mmio_base); |
| 176 | |
| 177 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Julia Lawall | c8ee5c6 | 2010-03-22 16:16:24 +0800 | [diff] [blame] | 178 | release_mem_region(res->start, resource_size(res)); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 179 | |
| 180 | clk_put(ssp->clk); |
| 181 | |
| 182 | mutex_lock(&ssp_lock); |
| 183 | list_del(&ssp->node); |
| 184 | mutex_unlock(&ssp_lock); |
| 185 | |
| 186 | kfree(ssp); |
| 187 | return 0; |
| 188 | } |
| 189 | |
Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 190 | static const struct platform_device_id ssp_id_table[] = { |
| 191 | { "pxa25x-ssp", PXA25x_SSP }, |
| 192 | { "pxa25x-nssp", PXA25x_NSSP }, |
| 193 | { "pxa27x-ssp", PXA27x_SSP }, |
Haojian Zhuang | 7e49922 | 2010-03-19 11:53:17 -0400 | [diff] [blame] | 194 | { "pxa168-ssp", PXA168_SSP }, |
Qiao Zhou | 6017221 | 2012-06-04 10:41:03 +0800 | [diff] [blame] | 195 | { "pxa910-ssp", PXA910_SSP }, |
Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 196 | { }, |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 197 | }; |
| 198 | |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 199 | static struct platform_driver pxa_ssp_driver = { |
| 200 | .probe = pxa_ssp_probe, |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 201 | .remove = pxa_ssp_remove, |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 202 | .driver = { |
Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 203 | .owner = THIS_MODULE, |
| 204 | .name = "pxa2xx-ssp", |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 205 | }, |
Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 206 | .id_table = ssp_id_table, |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | static int __init pxa_ssp_init(void) |
| 210 | { |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 211 | return platform_driver_register(&pxa_ssp_driver); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void __exit pxa_ssp_exit(void) |
| 215 | { |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 216 | platform_driver_unregister(&pxa_ssp_driver); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 217 | } |
| 218 | |
Russell King | cae0554 | 2007-12-10 15:35:54 +0000 | [diff] [blame] | 219 | arch_initcall(pxa_ssp_init); |
eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 220 | module_exit(pxa_ssp_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | MODULE_DESCRIPTION("PXA SSP driver"); |
| 223 | MODULE_AUTHOR("Liam Girdwood"); |
| 224 | MODULE_LICENSE("GPL"); |