blob: f519199741837664df922fc60e31c42de8eab145 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
eric miao88286452007-12-06 17:56:42 +080038static DEFINE_MUTEX(ssp_lock);
39static LIST_HEAD(ssp_list);
40
Haojian Zhuangbaffe162010-05-05 10:11:15 -040041struct ssp_device *pxa_ssp_request(int port, const char *label)
eric miao88286452007-12-06 17:56:42 +080042{
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 Torvalds1da177e2005-04-16 15:20:36 -070053 }
54
eric miao88286452007-12-06 17:56:42 +080055 mutex_unlock(&ssp_lock);
56
Guennadi Liakhovetskia4aff222008-06-05 10:43:14 +010057 if (&ssp->node == &ssp_list)
eric miao88286452007-12-06 17:56:42 +080058 return NULL;
59
60 return ssp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
Haojian Zhuangbaffe162010-05-05 10:11:15 -040062EXPORT_SYMBOL(pxa_ssp_request);
eric miao88286452007-12-06 17:56:42 +080063
Daniel Mack64462212013-08-12 10:37:18 +020064struct 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}
86EXPORT_SYMBOL(pxa_ssp_request_of);
87
Haojian Zhuangbaffe162010-05-05 10:11:15 -040088void pxa_ssp_free(struct ssp_device *ssp)
eric miao88286452007-12-06 17:56:42 +080089{
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 Zhuangbaffe162010-05-05 10:11:15 -040098EXPORT_SYMBOL(pxa_ssp_free);
eric miao88286452007-12-06 17:56:42 +080099
Daniel Macka6e56c22013-08-12 10:37:16 +0200100#ifdef CONFIG_OF
101static 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 Macka6e56c22013-08-12 10:37:16 +0200109 { },
110};
111MODULE_DEVICE_TABLE(of, pxa_ssp_of_ids);
112#endif
113
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800114static int pxa_ssp_probe(struct platform_device *pdev)
eric miao88286452007-12-06 17:56:42 +0800115{
116 struct resource *res;
117 struct ssp_device *ssp;
Daniel Mack970d8a72013-08-12 10:37:15 +0200118 struct device *dev = &pdev->dev;
eric miao88286452007-12-06 17:56:42 +0800119
Daniel Mack1c459de2013-08-12 10:37:17 +0200120 ssp = devm_kzalloc(dev, sizeof(struct ssp_device), GFP_KERNEL);
Daniel Mack64be2812013-08-12 10:37:14 +0200121 if (ssp == NULL)
eric miao88286452007-12-06 17:56:42 +0800122 return -ENOMEM;
Daniel Mack64be2812013-08-12 10:37:14 +0200123
Mark Brown919dcb22008-06-19 02:55:52 +0100124 ssp->pdev = pdev;
eric miao88286452007-12-06 17:56:42 +0800125
Daniel Mack1c459de2013-08-12 10:37:17 +0200126 ssp->clk = devm_clk_get(dev, NULL);
127 if (IS_ERR(ssp->clk))
128 return PTR_ERR(ssp->clk);
eric miao88286452007-12-06 17:56:42 +0800129
130 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131 if (res == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200132 dev_err(dev, "no memory resource defined\n");
Daniel Mack1c459de2013-08-12 10:37:17 +0200133 return -ENODEV;
eric miao88286452007-12-06 17:56:42 +0800134 }
135
Daniel Mack1c459de2013-08-12 10:37:17 +0200136 res = devm_request_mem_region(dev, res->start, resource_size(res),
137 pdev->name);
eric miao88286452007-12-06 17:56:42 +0800138 if (res == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200139 dev_err(dev, "failed to request memory resource\n");
Daniel Mack1c459de2013-08-12 10:37:17 +0200140 return -EBUSY;
eric miao88286452007-12-06 17:56:42 +0800141 }
142
143 ssp->phys_base = res->start;
144
Daniel Mack1c459de2013-08-12 10:37:17 +0200145 ssp->mmio_base = devm_ioremap(dev, res->start, resource_size(res));
eric miao88286452007-12-06 17:56:42 +0800146 if (ssp->mmio_base == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200147 dev_err(dev, "failed to ioremap() registers\n");
Daniel Mack1c459de2013-08-12 10:37:17 +0200148 return -ENODEV;
eric miao88286452007-12-06 17:56:42 +0800149 }
150
151 ssp->irq = platform_get_irq(pdev, 0);
152 if (ssp->irq < 0) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200153 dev_err(dev, "no IRQ resource defined\n");
Daniel Mack1c459de2013-08-12 10:37:17 +0200154 return -ENODEV;
eric miao88286452007-12-06 17:56:42 +0800155 }
156
Daniel Macka6e56c22013-08-12 10:37:16 +0200157 if (dev->of_node) {
158 const struct of_device_id *id =
159 of_match_device(of_match_ptr(pxa_ssp_of_ids), dev);
160 ssp->type = (int) id->data;
161 } else {
162 const struct platform_device_id *id =
163 platform_get_device_id(pdev);
164 ssp->type = (int) id->driver_data;
165
166 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
167 * starts from 0, do a translation here
168 */
169 ssp->port_id = pdev->id + 1;
170 }
171
eric miao88286452007-12-06 17:56:42 +0800172 ssp->use_count = 0;
Daniel Mack64462212013-08-12 10:37:18 +0200173 ssp->of_node = dev->of_node;
eric miao88286452007-12-06 17:56:42 +0800174
175 mutex_lock(&ssp_lock);
176 list_add(&ssp->node, &ssp_list);
177 mutex_unlock(&ssp_lock);
178
179 platform_set_drvdata(pdev, ssp);
eric miao88286452007-12-06 17:56:42 +0800180
Daniel Mack1c459de2013-08-12 10:37:17 +0200181 return 0;
eric miao88286452007-12-06 17:56:42 +0800182}
183
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800184static int pxa_ssp_remove(struct platform_device *pdev)
eric miao88286452007-12-06 17:56:42 +0800185{
186 struct resource *res;
187 struct ssp_device *ssp;
188
189 ssp = platform_get_drvdata(pdev);
190 if (ssp == NULL)
191 return -ENODEV;
192
eric miao88286452007-12-06 17:56:42 +0800193 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawallc8ee5c62010-03-22 16:16:24 +0800194 release_mem_region(res->start, resource_size(res));
eric miao88286452007-12-06 17:56:42 +0800195
196 clk_put(ssp->clk);
197
198 mutex_lock(&ssp_lock);
199 list_del(&ssp->node);
200 mutex_unlock(&ssp_lock);
201
eric miao88286452007-12-06 17:56:42 +0800202 return 0;
203}
204
Eric Miao6427d452009-10-23 00:09:47 +0800205static const struct platform_device_id ssp_id_table[] = {
206 { "pxa25x-ssp", PXA25x_SSP },
207 { "pxa25x-nssp", PXA25x_NSSP },
208 { "pxa27x-ssp", PXA27x_SSP },
Daniel Mack6f0243a2014-08-13 21:59:18 +0200209 { "pxa3xx-ssp", PXA3xx_SSP },
Haojian Zhuang7e499222010-03-19 11:53:17 -0400210 { "pxa168-ssp", PXA168_SSP },
Qiao Zhou60172212012-06-04 10:41:03 +0800211 { "pxa910-ssp", PXA910_SSP },
Eric Miao6427d452009-10-23 00:09:47 +0800212 { },
eric miao88286452007-12-06 17:56:42 +0800213};
214
Haojian Zhuangbaffe162010-05-05 10:11:15 -0400215static struct platform_driver pxa_ssp_driver = {
216 .probe = pxa_ssp_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800217 .remove = pxa_ssp_remove,
eric miao88286452007-12-06 17:56:42 +0800218 .driver = {
Daniel Macka6e56c22013-08-12 10:37:16 +0200219 .name = "pxa2xx-ssp",
220 .of_match_table = of_match_ptr(pxa_ssp_of_ids),
eric miao88286452007-12-06 17:56:42 +0800221 },
Eric Miao6427d452009-10-23 00:09:47 +0800222 .id_table = ssp_id_table,
eric miao88286452007-12-06 17:56:42 +0800223};
224
225static int __init pxa_ssp_init(void)
226{
Haojian Zhuangbaffe162010-05-05 10:11:15 -0400227 return platform_driver_register(&pxa_ssp_driver);
eric miao88286452007-12-06 17:56:42 +0800228}
229
230static void __exit pxa_ssp_exit(void)
231{
Haojian Zhuangbaffe162010-05-05 10:11:15 -0400232 platform_driver_unregister(&pxa_ssp_driver);
eric miao88286452007-12-06 17:56:42 +0800233}
234
Russell Kingcae05542007-12-10 15:35:54 +0000235arch_initcall(pxa_ssp_init);
eric miao88286452007-12-06 17:56:42 +0800236module_exit(pxa_ssp_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238MODULE_DESCRIPTION("PXA SSP driver");
239MODULE_AUTHOR("Liam Girdwood");
240MODULE_LICENSE("GPL");