blob: c83f27b6bdda0eac2c566d40b4d99e5428be650b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
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 Ven00431702006-01-12 18:42:23 +000027#include <linux/mutex.h>
eric miao88286452007-12-06 17:56:42 +080028#include <linux/clk.h>
29#include <linux/err.h>
30#include <linux/platform_device.h>
Sebastian Andrzej Siewior8348c252010-11-22 17:12:15 -080031#include <linux/spi/pxa2xx_spi.h>
Russell Kingfced80c2008-09-06 12:10:45 +010032#include <linux/io.h>
Daniel Macka6e56c22013-08-12 10:37:16 +020033#include <linux/of.h>
34#include <linux/of_device.h>
eric miao88286452007-12-06 17:56:42 +080035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
eric miao88286452007-12-06 17:56:42 +080039static DEFINE_MUTEX(ssp_lock);
40static LIST_HEAD(ssp_list);
41
Haojian Zhuangbaffe162010-05-05 10:11:15 -040042struct ssp_device *pxa_ssp_request(int port, const char *label)
eric miao88286452007-12-06 17:56:42 +080043{
44 struct ssp_device *ssp = NULL;
45
46 mutex_lock(&ssp_lock);
47
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
55
eric miao88286452007-12-06 17:56:42 +080056 mutex_unlock(&ssp_lock);
57
Guennadi Liakhovetskia4aff222008-06-05 10:43:14 +010058 if (&ssp->node == &ssp_list)
eric miao88286452007-12-06 17:56:42 +080059 return NULL;
60
61 return ssp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
Haojian Zhuangbaffe162010-05-05 10:11:15 -040063EXPORT_SYMBOL(pxa_ssp_request);
eric miao88286452007-12-06 17:56:42 +080064
Daniel Mack64462212013-08-12 10:37:18 +020065struct ssp_device *pxa_ssp_request_of(const struct device_node *of_node,
66 const char *label)
67{
68 struct ssp_device *ssp = NULL;
69
70 mutex_lock(&ssp_lock);
71
72 list_for_each_entry(ssp, &ssp_list, node) {
73 if (ssp->of_node == of_node && ssp->use_count == 0) {
74 ssp->use_count++;
75 ssp->label = label;
76 break;
77 }
78 }
79
80 mutex_unlock(&ssp_lock);
81
82 if (&ssp->node == &ssp_list)
83 return NULL;
84
85 return ssp;
86}
87EXPORT_SYMBOL(pxa_ssp_request_of);
88
Haojian Zhuangbaffe162010-05-05 10:11:15 -040089void pxa_ssp_free(struct ssp_device *ssp)
eric miao88286452007-12-06 17:56:42 +080090{
91 mutex_lock(&ssp_lock);
92 if (ssp->use_count) {
93 ssp->use_count--;
94 ssp->label = NULL;
95 } else
96 dev_err(&ssp->pdev->dev, "device already free\n");
97 mutex_unlock(&ssp_lock);
98}
Haojian Zhuangbaffe162010-05-05 10:11:15 -040099EXPORT_SYMBOL(pxa_ssp_free);
eric miao88286452007-12-06 17:56:42 +0800100
Daniel Macka6e56c22013-08-12 10:37:16 +0200101#ifdef CONFIG_OF
102static const struct of_device_id pxa_ssp_of_ids[] = {
103 { .compatible = "mrvl,pxa25x-ssp", .data = (void *) PXA25x_SSP },
104 { .compatible = "mvrl,pxa25x-nssp", .data = (void *) PXA25x_NSSP },
105 { .compatible = "mrvl,pxa27x-ssp", .data = (void *) PXA27x_SSP },
106 { .compatible = "mrvl,pxa3xx-ssp", .data = (void *) PXA3xx_SSP },
107 { .compatible = "mvrl,pxa168-ssp", .data = (void *) PXA168_SSP },
108 { .compatible = "mrvl,pxa910-ssp", .data = (void *) PXA910_SSP },
109 { .compatible = "mrvl,ce4100-ssp", .data = (void *) CE4100_SSP },
110 { .compatible = "mrvl,lpss-ssp", .data = (void *) LPSS_SSP },
111 { },
112};
113MODULE_DEVICE_TABLE(of, pxa_ssp_of_ids);
114#endif
115
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800116static int pxa_ssp_probe(struct platform_device *pdev)
eric miao88286452007-12-06 17:56:42 +0800117{
118 struct resource *res;
119 struct ssp_device *ssp;
Daniel Mack970d8a72013-08-12 10:37:15 +0200120 struct device *dev = &pdev->dev;
eric miao88286452007-12-06 17:56:42 +0800121
Daniel Mack1c459de2013-08-12 10:37:17 +0200122 ssp = devm_kzalloc(dev, sizeof(struct ssp_device), GFP_KERNEL);
Daniel Mack64be2812013-08-12 10:37:14 +0200123 if (ssp == NULL)
eric miao88286452007-12-06 17:56:42 +0800124 return -ENOMEM;
Daniel Mack64be2812013-08-12 10:37:14 +0200125
Mark Brown919dcb22008-06-19 02:55:52 +0100126 ssp->pdev = pdev;
eric miao88286452007-12-06 17:56:42 +0800127
Daniel Mack1c459de2013-08-12 10:37:17 +0200128 ssp->clk = devm_clk_get(dev, NULL);
129 if (IS_ERR(ssp->clk))
130 return PTR_ERR(ssp->clk);
eric miao88286452007-12-06 17:56:42 +0800131
Daniel Macka6e56c22013-08-12 10:37:16 +0200132 if (dev->of_node) {
133 struct of_phandle_args dma_spec;
134 struct device_node *np = dev->of_node;
Julia Lawall077de1a2010-03-22 16:11:55 +0800135
Daniel Macka6e56c22013-08-12 10:37:16 +0200136 /*
137 * FIXME: we should allocate the DMA channel from this
138 * context and pass the channel down to the ssp users.
139 * For now, we lookup the rx and tx indices manually
140 */
141
142 /* rx */
143 of_parse_phandle_with_args(np, "dmas", "#dma-cells",
144 0, &dma_spec);
145 ssp->drcmr_rx = dma_spec.args[0];
146 of_node_put(dma_spec.np);
147
148 /* tx */
149 of_parse_phandle_with_args(np, "dmas", "#dma-cells",
150 1, &dma_spec);
151 ssp->drcmr_tx = dma_spec.args[0];
152 of_node_put(dma_spec.np);
153 } else {
154 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
155 if (res == NULL) {
156 dev_err(dev, "no SSP RX DRCMR defined\n");
157 return -ENODEV;
158 }
159 ssp->drcmr_rx = res->start;
160
161 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
162 if (res == NULL) {
163 dev_err(dev, "no SSP TX DRCMR defined\n");
164 return -ENODEV;
165 }
166 ssp->drcmr_tx = res->start;
Julia Lawall077de1a2010-03-22 16:11:55 +0800167 }
Julia Lawall077de1a2010-03-22 16:11:55 +0800168
eric miao88286452007-12-06 17:56:42 +0800169 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
170 if (res == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200171 dev_err(dev, "no memory resource defined\n");
Daniel Mack1c459de2013-08-12 10:37:17 +0200172 return -ENODEV;
eric miao88286452007-12-06 17:56:42 +0800173 }
174
Daniel Mack1c459de2013-08-12 10:37:17 +0200175 res = devm_request_mem_region(dev, res->start, resource_size(res),
176 pdev->name);
eric miao88286452007-12-06 17:56:42 +0800177 if (res == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200178 dev_err(dev, "failed to request memory resource\n");
Daniel Mack1c459de2013-08-12 10:37:17 +0200179 return -EBUSY;
eric miao88286452007-12-06 17:56:42 +0800180 }
181
182 ssp->phys_base = res->start;
183
Daniel Mack1c459de2013-08-12 10:37:17 +0200184 ssp->mmio_base = devm_ioremap(dev, res->start, resource_size(res));
eric miao88286452007-12-06 17:56:42 +0800185 if (ssp->mmio_base == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200186 dev_err(dev, "failed to ioremap() registers\n");
Daniel Mack1c459de2013-08-12 10:37:17 +0200187 return -ENODEV;
eric miao88286452007-12-06 17:56:42 +0800188 }
189
190 ssp->irq = platform_get_irq(pdev, 0);
191 if (ssp->irq < 0) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200192 dev_err(dev, "no IRQ resource defined\n");
Daniel Mack1c459de2013-08-12 10:37:17 +0200193 return -ENODEV;
eric miao88286452007-12-06 17:56:42 +0800194 }
195
Daniel Macka6e56c22013-08-12 10:37:16 +0200196 if (dev->of_node) {
197 const struct of_device_id *id =
198 of_match_device(of_match_ptr(pxa_ssp_of_ids), dev);
199 ssp->type = (int) id->data;
200 } else {
201 const struct platform_device_id *id =
202 platform_get_device_id(pdev);
203 ssp->type = (int) id->driver_data;
204
205 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
206 * starts from 0, do a translation here
207 */
208 ssp->port_id = pdev->id + 1;
209 }
210
eric miao88286452007-12-06 17:56:42 +0800211 ssp->use_count = 0;
Daniel Mack64462212013-08-12 10:37:18 +0200212 ssp->of_node = dev->of_node;
eric miao88286452007-12-06 17:56:42 +0800213
214 mutex_lock(&ssp_lock);
215 list_add(&ssp->node, &ssp_list);
216 mutex_unlock(&ssp_lock);
217
218 platform_set_drvdata(pdev, ssp);
eric miao88286452007-12-06 17:56:42 +0800219
Daniel Mack1c459de2013-08-12 10:37:17 +0200220 return 0;
eric miao88286452007-12-06 17:56:42 +0800221}
222
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800223static int pxa_ssp_remove(struct platform_device *pdev)
eric miao88286452007-12-06 17:56:42 +0800224{
225 struct resource *res;
226 struct ssp_device *ssp;
227
228 ssp = platform_get_drvdata(pdev);
229 if (ssp == NULL)
230 return -ENODEV;
231
232 iounmap(ssp->mmio_base);
233
234 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawallc8ee5c62010-03-22 16:16:24 +0800235 release_mem_region(res->start, resource_size(res));
eric miao88286452007-12-06 17:56:42 +0800236
237 clk_put(ssp->clk);
238
239 mutex_lock(&ssp_lock);
240 list_del(&ssp->node);
241 mutex_unlock(&ssp_lock);
242
243 kfree(ssp);
244 return 0;
245}
246
Eric Miao6427d452009-10-23 00:09:47 +0800247static const struct platform_device_id ssp_id_table[] = {
248 { "pxa25x-ssp", PXA25x_SSP },
249 { "pxa25x-nssp", PXA25x_NSSP },
250 { "pxa27x-ssp", PXA27x_SSP },
Haojian Zhuang7e499222010-03-19 11:53:17 -0400251 { "pxa168-ssp", PXA168_SSP },
Qiao Zhou60172212012-06-04 10:41:03 +0800252 { "pxa910-ssp", PXA910_SSP },
Eric Miao6427d452009-10-23 00:09:47 +0800253 { },
eric miao88286452007-12-06 17:56:42 +0800254};
255
Haojian Zhuangbaffe162010-05-05 10:11:15 -0400256static struct platform_driver pxa_ssp_driver = {
257 .probe = pxa_ssp_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800258 .remove = pxa_ssp_remove,
eric miao88286452007-12-06 17:56:42 +0800259 .driver = {
Daniel Macka6e56c22013-08-12 10:37:16 +0200260 .owner = THIS_MODULE,
261 .name = "pxa2xx-ssp",
262 .of_match_table = of_match_ptr(pxa_ssp_of_ids),
eric miao88286452007-12-06 17:56:42 +0800263 },
Eric Miao6427d452009-10-23 00:09:47 +0800264 .id_table = ssp_id_table,
eric miao88286452007-12-06 17:56:42 +0800265};
266
267static int __init pxa_ssp_init(void)
268{
Haojian Zhuangbaffe162010-05-05 10:11:15 -0400269 return platform_driver_register(&pxa_ssp_driver);
eric miao88286452007-12-06 17:56:42 +0800270}
271
272static void __exit pxa_ssp_exit(void)
273{
Haojian Zhuangbaffe162010-05-05 10:11:15 -0400274 platform_driver_unregister(&pxa_ssp_driver);
eric miao88286452007-12-06 17:56:42 +0800275}
276
Russell Kingcae05542007-12-10 15:35:54 +0000277arch_initcall(pxa_ssp_init);
eric miao88286452007-12-06 17:56:42 +0800278module_exit(pxa_ssp_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280MODULE_DESCRIPTION("PXA SSP driver");
281MODULE_AUTHOR("Liam Girdwood");
282MODULE_LICENSE("GPL");