blob: 65ba28a0e3c6c6d966ac741935f85b8d2466d404 [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>
eric miao88286452007-12-06 17:56:42 +080033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
eric miao88286452007-12-06 17:56:42 +080037static DEFINE_MUTEX(ssp_lock);
38static LIST_HEAD(ssp_list);
39
Haojian Zhuangbaffe162010-05-05 10:11:15 -040040struct ssp_device *pxa_ssp_request(int port, const char *label)
eric miao88286452007-12-06 17:56:42 +080041{
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 Torvalds1da177e2005-04-16 15:20:36 -070052 }
53
eric miao88286452007-12-06 17:56:42 +080054 mutex_unlock(&ssp_lock);
55
Guennadi Liakhovetskia4aff222008-06-05 10:43:14 +010056 if (&ssp->node == &ssp_list)
eric miao88286452007-12-06 17:56:42 +080057 return NULL;
58
59 return ssp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
Haojian Zhuangbaffe162010-05-05 10:11:15 -040061EXPORT_SYMBOL(pxa_ssp_request);
eric miao88286452007-12-06 17:56:42 +080062
Haojian Zhuangbaffe162010-05-05 10:11:15 -040063void pxa_ssp_free(struct ssp_device *ssp)
eric miao88286452007-12-06 17:56:42 +080064{
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 Zhuangbaffe162010-05-05 10:11:15 -040073EXPORT_SYMBOL(pxa_ssp_free);
eric miao88286452007-12-06 17:56:42 +080074
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -080075static int pxa_ssp_probe(struct platform_device *pdev)
eric miao88286452007-12-06 17:56:42 +080076{
Eric Miao6427d452009-10-23 00:09:47 +080077 const struct platform_device_id *id = platform_get_device_id(pdev);
eric miao88286452007-12-06 17:56:42 +080078 struct resource *res;
79 struct ssp_device *ssp;
Daniel Mack970d8a72013-08-12 10:37:15 +020080 struct device *dev = &pdev->dev;
eric miao88286452007-12-06 17:56:42 +080081 int ret = 0;
82
83 ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
Daniel Mack64be2812013-08-12 10:37:14 +020084 if (ssp == NULL)
eric miao88286452007-12-06 17:56:42 +080085 return -ENOMEM;
Daniel Mack64be2812013-08-12 10:37:14 +020086
Mark Brown919dcb22008-06-19 02:55:52 +010087 ssp->pdev = pdev;
eric miao88286452007-12-06 17:56:42 +080088
Daniel Mack970d8a72013-08-12 10:37:15 +020089 ssp->clk = clk_get(dev, NULL);
eric miao88286452007-12-06 17:56:42 +080090 if (IS_ERR(ssp->clk)) {
91 ret = PTR_ERR(ssp->clk);
92 goto err_free;
93 }
94
Julia Lawall077de1a2010-03-22 16:11:55 +080095 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
96 if (res == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +020097 dev_err(dev, "no SSP RX DRCMR defined\n");
Julia Lawall077de1a2010-03-22 16:11:55 +080098 ret = -ENODEV;
99 goto err_free_clk;
100 }
101 ssp->drcmr_rx = res->start;
102
103 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
104 if (res == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200105 dev_err(dev, "no SSP TX DRCMR defined\n");
Julia Lawall077de1a2010-03-22 16:11:55 +0800106 ret = -ENODEV;
107 goto err_free_clk;
108 }
109 ssp->drcmr_tx = res->start;
110
eric miao88286452007-12-06 17:56:42 +0800111 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
112 if (res == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200113 dev_err(dev, "no memory resource defined\n");
eric miao88286452007-12-06 17:56:42 +0800114 ret = -ENODEV;
115 goto err_free_clk;
116 }
117
Julia Lawallc8ee5c62010-03-22 16:16:24 +0800118 res = request_mem_region(res->start, resource_size(res),
eric miao88286452007-12-06 17:56:42 +0800119 pdev->name);
120 if (res == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200121 dev_err(dev, "failed to request memory resource\n");
eric miao88286452007-12-06 17:56:42 +0800122 ret = -EBUSY;
123 goto err_free_clk;
124 }
125
126 ssp->phys_base = res->start;
127
Julia Lawallc8ee5c62010-03-22 16:16:24 +0800128 ssp->mmio_base = ioremap(res->start, resource_size(res));
eric miao88286452007-12-06 17:56:42 +0800129 if (ssp->mmio_base == NULL) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200130 dev_err(dev, "failed to ioremap() registers\n");
eric miao88286452007-12-06 17:56:42 +0800131 ret = -ENODEV;
132 goto err_free_mem;
133 }
134
135 ssp->irq = platform_get_irq(pdev, 0);
136 if (ssp->irq < 0) {
Daniel Mack970d8a72013-08-12 10:37:15 +0200137 dev_err(dev, "no IRQ resource defined\n");
eric miao88286452007-12-06 17:56:42 +0800138 ret = -ENODEV;
139 goto err_free_io;
140 }
141
eric miao88286452007-12-06 17:56:42 +0800142 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
143 * starts from 0, do a translation here
144 */
145 ssp->port_id = pdev->id + 1;
146 ssp->use_count = 0;
Eric Miao6427d452009-10-23 00:09:47 +0800147 ssp->type = (int)id->driver_data;
eric miao88286452007-12-06 17:56:42 +0800148
149 mutex_lock(&ssp_lock);
150 list_add(&ssp->node, &ssp_list);
151 mutex_unlock(&ssp_lock);
152
153 platform_set_drvdata(pdev, ssp);
154 return 0;
155
156err_free_io:
157 iounmap(ssp->mmio_base);
158err_free_mem:
Julia Lawallc8ee5c62010-03-22 16:16:24 +0800159 release_mem_region(res->start, resource_size(res));
eric miao88286452007-12-06 17:56:42 +0800160err_free_clk:
161 clk_put(ssp->clk);
162err_free:
163 kfree(ssp);
164 return ret;
165}
166
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800167static int pxa_ssp_remove(struct platform_device *pdev)
eric miao88286452007-12-06 17:56:42 +0800168{
169 struct resource *res;
170 struct ssp_device *ssp;
171
172 ssp = platform_get_drvdata(pdev);
173 if (ssp == NULL)
174 return -ENODEV;
175
176 iounmap(ssp->mmio_base);
177
178 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawallc8ee5c62010-03-22 16:16:24 +0800179 release_mem_region(res->start, resource_size(res));
eric miao88286452007-12-06 17:56:42 +0800180
181 clk_put(ssp->clk);
182
183 mutex_lock(&ssp_lock);
184 list_del(&ssp->node);
185 mutex_unlock(&ssp_lock);
186
187 kfree(ssp);
188 return 0;
189}
190
Eric Miao6427d452009-10-23 00:09:47 +0800191static const struct platform_device_id ssp_id_table[] = {
192 { "pxa25x-ssp", PXA25x_SSP },
193 { "pxa25x-nssp", PXA25x_NSSP },
194 { "pxa27x-ssp", PXA27x_SSP },
Haojian Zhuang7e499222010-03-19 11:53:17 -0400195 { "pxa168-ssp", PXA168_SSP },
Qiao Zhou60172212012-06-04 10:41:03 +0800196 { "pxa910-ssp", PXA910_SSP },
Eric Miao6427d452009-10-23 00:09:47 +0800197 { },
eric miao88286452007-12-06 17:56:42 +0800198};
199
Haojian Zhuangbaffe162010-05-05 10:11:15 -0400200static struct platform_driver pxa_ssp_driver = {
201 .probe = pxa_ssp_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800202 .remove = pxa_ssp_remove,
eric miao88286452007-12-06 17:56:42 +0800203 .driver = {
Eric Miao6427d452009-10-23 00:09:47 +0800204 .owner = THIS_MODULE,
205 .name = "pxa2xx-ssp",
eric miao88286452007-12-06 17:56:42 +0800206 },
Eric Miao6427d452009-10-23 00:09:47 +0800207 .id_table = ssp_id_table,
eric miao88286452007-12-06 17:56:42 +0800208};
209
210static int __init pxa_ssp_init(void)
211{
Haojian Zhuangbaffe162010-05-05 10:11:15 -0400212 return platform_driver_register(&pxa_ssp_driver);
eric miao88286452007-12-06 17:56:42 +0800213}
214
215static void __exit pxa_ssp_exit(void)
216{
Haojian Zhuangbaffe162010-05-05 10:11:15 -0400217 platform_driver_unregister(&pxa_ssp_driver);
eric miao88286452007-12-06 17:56:42 +0800218}
219
Russell Kingcae05542007-12-10 15:35:54 +0000220arch_initcall(pxa_ssp_init);
eric miao88286452007-12-06 17:56:42 +0800221module_exit(pxa_ssp_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223MODULE_DESCRIPTION("PXA SSP driver");
224MODULE_AUTHOR("Liam Girdwood");
225MODULE_LICENSE("GPL");