ARM: pxa: ssp: use devm_ functions
[deliverable/linux.git] / arch / arm / plat-pxa / ssp.c
CommitLineData
1da177e4
LT
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>
1da177e4
LT
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>
00431707 27#include <linux/mutex.h>
88286450 28#include <linux/clk.h>
29#include <linux/err.h>
30#include <linux/platform_device.h>
8348c259 31#include <linux/spi/pxa2xx_spi.h>
fced80c7 32#include <linux/io.h>
a6e56c28
DM
33#include <linux/of.h>
34#include <linux/of_device.h>
88286450 35
1da177e4 36#include <asm/irq.h>
a09e64fb 37#include <mach/hardware.h>
1da177e4 38
88286450 39static DEFINE_MUTEX(ssp_lock);
40static LIST_HEAD(ssp_list);
41
baffe169 42struct ssp_device *pxa_ssp_request(int port, const char *label)
88286450 43{
44 struct ssp_device *ssp = NULL;
45
46 mutex_lock(&ssp_lock);
1da177e4 47
88286450 48 list_for_each_entry(ssp, &ssp_list, node) {
49 if (ssp->port_id == port && ssp->use_count == 0) {
50 ssp->use_count++;
51 ssp->label = label;
52 break;
53 }
1da177e4
LT
54 }
55
88286450 56 mutex_unlock(&ssp_lock);
57
a4aff223 58 if (&ssp->node == &ssp_list)
88286450 59 return NULL;
60
61 return ssp;
62}
baffe169 63EXPORT_SYMBOL(pxa_ssp_request);
88286450 64
baffe169 65void pxa_ssp_free(struct ssp_device *ssp)
88286450 66{
67 mutex_lock(&ssp_lock);
68 if (ssp->use_count) {
69 ssp->use_count--;
70 ssp->label = NULL;
71 } else
72 dev_err(&ssp->pdev->dev, "device already free\n");
73 mutex_unlock(&ssp_lock);
1da177e4 74}
baffe169 75EXPORT_SYMBOL(pxa_ssp_free);
88286450 76
a6e56c28
DM
77#ifdef CONFIG_OF
78static const struct of_device_id pxa_ssp_of_ids[] = {
79 { .compatible = "mrvl,pxa25x-ssp", .data = (void *) PXA25x_SSP },
80 { .compatible = "mvrl,pxa25x-nssp", .data = (void *) PXA25x_NSSP },
81 { .compatible = "mrvl,pxa27x-ssp", .data = (void *) PXA27x_SSP },
82 { .compatible = "mrvl,pxa3xx-ssp", .data = (void *) PXA3xx_SSP },
83 { .compatible = "mvrl,pxa168-ssp", .data = (void *) PXA168_SSP },
84 { .compatible = "mrvl,pxa910-ssp", .data = (void *) PXA910_SSP },
85 { .compatible = "mrvl,ce4100-ssp", .data = (void *) CE4100_SSP },
86 { .compatible = "mrvl,lpss-ssp", .data = (void *) LPSS_SSP },
87 { },
88};
89MODULE_DEVICE_TABLE(of, pxa_ssp_of_ids);
90#endif
91
351a102d 92static int pxa_ssp_probe(struct platform_device *pdev)
88286450 93{
94 struct resource *res;
95 struct ssp_device *ssp;
970d8a71 96 struct device *dev = &pdev->dev;
88286450 97
1c459de1 98 ssp = devm_kzalloc(dev, sizeof(struct ssp_device), GFP_KERNEL);
64be2814 99 if (ssp == NULL)
88286450 100 return -ENOMEM;
64be2814 101
919dcb21 102 ssp->pdev = pdev;
88286450 103
1c459de1
DM
104 ssp->clk = devm_clk_get(dev, NULL);
105 if (IS_ERR(ssp->clk))
106 return PTR_ERR(ssp->clk);
88286450 107
a6e56c28
DM
108 if (dev->of_node) {
109 struct of_phandle_args dma_spec;
110 struct device_node *np = dev->of_node;
111
112 /*
113 * FIXME: we should allocate the DMA channel from this
114 * context and pass the channel down to the ssp users.
115 * For now, we lookup the rx and tx indices manually
116 */
117
118 /* rx */
119 of_parse_phandle_with_args(np, "dmas", "#dma-cells",
120 0, &dma_spec);
121 ssp->drcmr_rx = dma_spec.args[0];
122 of_node_put(dma_spec.np);
123
124 /* tx */
125 of_parse_phandle_with_args(np, "dmas", "#dma-cells",
126 1, &dma_spec);
127 ssp->drcmr_tx = dma_spec.args[0];
128 of_node_put(dma_spec.np);
129 } else {
130 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
131 if (res == NULL) {
132 dev_err(dev, "no SSP RX DRCMR defined\n");
133 return -ENODEV;
134 }
135 ssp->drcmr_rx = res->start;
077de1ad 136
a6e56c28
DM
137 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
138 if (res == NULL) {
139 dev_err(dev, "no SSP TX DRCMR defined\n");
140 return -ENODEV;
141 }
142 ssp->drcmr_tx = res->start;
077de1ad 143 }
077de1ad 144
88286450 145 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
146 if (res == NULL) {
970d8a71 147 dev_err(dev, "no memory resource defined\n");
1c459de1 148 return -ENODEV;
88286450 149 }
150
1c459de1
DM
151 res = devm_request_mem_region(dev, res->start, resource_size(res),
152 pdev->name);
88286450 153 if (res == NULL) {
970d8a71 154 dev_err(dev, "failed to request memory resource\n");
1c459de1 155 return -EBUSY;
88286450 156 }
157
158 ssp->phys_base = res->start;
159
1c459de1 160 ssp->mmio_base = devm_ioremap(dev, res->start, resource_size(res));
88286450 161 if (ssp->mmio_base == NULL) {
970d8a71 162 dev_err(dev, "failed to ioremap() registers\n");
1c459de1 163 return -ENODEV;
88286450 164 }
165
166 ssp->irq = platform_get_irq(pdev, 0);
167 if (ssp->irq < 0) {
970d8a71 168 dev_err(dev, "no IRQ resource defined\n");
1c459de1 169 return -ENODEV;
88286450 170 }
171
a6e56c28
DM
172 if (dev->of_node) {
173 const struct of_device_id *id =
174 of_match_device(of_match_ptr(pxa_ssp_of_ids), dev);
175 ssp->type = (int) id->data;
176 } else {
177 const struct platform_device_id *id =
178 platform_get_device_id(pdev);
179 ssp->type = (int) id->driver_data;
180
181 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
182 * starts from 0, do a translation here
183 */
184 ssp->port_id = pdev->id + 1;
185 }
186
88286450 187 ssp->use_count = 0;
88286450 188
189 mutex_lock(&ssp_lock);
190 list_add(&ssp->node, &ssp_list);
191 mutex_unlock(&ssp_lock);
192
193 platform_set_drvdata(pdev, ssp);
88286450 194
1c459de1 195 return 0;
88286450 196}
197
351a102d 198static int pxa_ssp_remove(struct platform_device *pdev)
88286450 199{
200 struct resource *res;
201 struct ssp_device *ssp;
202
203 ssp = platform_get_drvdata(pdev);
204 if (ssp == NULL)
205 return -ENODEV;
206
207 iounmap(ssp->mmio_base);
208
209 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
c8ee5c69 210 release_mem_region(res->start, resource_size(res));
88286450 211
212 clk_put(ssp->clk);
1da177e4 213
88286450 214 mutex_lock(&ssp_lock);
215 list_del(&ssp->node);
216 mutex_unlock(&ssp_lock);
217
218 kfree(ssp);
219 return 0;
220}
221
6427d450
EM
222static const struct platform_device_id ssp_id_table[] = {
223 { "pxa25x-ssp", PXA25x_SSP },
224 { "pxa25x-nssp", PXA25x_NSSP },
225 { "pxa27x-ssp", PXA27x_SSP },
7e499228 226 { "pxa168-ssp", PXA168_SSP },
60172215 227 { "pxa910-ssp", PXA910_SSP },
6427d450 228 { },
88286450 229};
230
baffe169
HZ
231static struct platform_driver pxa_ssp_driver = {
232 .probe = pxa_ssp_probe,
351a102d 233 .remove = pxa_ssp_remove,
88286450 234 .driver = {
a6e56c28
DM
235 .owner = THIS_MODULE,
236 .name = "pxa2xx-ssp",
237 .of_match_table = of_match_ptr(pxa_ssp_of_ids),
88286450 238 },
6427d450 239 .id_table = ssp_id_table,
88286450 240};
241
242static int __init pxa_ssp_init(void)
243{
baffe169 244 return platform_driver_register(&pxa_ssp_driver);
88286450 245}
246
247static void __exit pxa_ssp_exit(void)
248{
baffe169 249 platform_driver_unregister(&pxa_ssp_driver);
1da177e4
LT
250}
251
cae05541 252arch_initcall(pxa_ssp_init);
88286450 253module_exit(pxa_ssp_exit);
1da177e4 254
1da177e4
LT
255MODULE_DESCRIPTION("PXA SSP driver");
256MODULE_AUTHOR("Liam Girdwood");
257MODULE_LICENSE("GPL");
This page took 0.657017 seconds and 5 git commands to generate.