blob: 9adc2eb6949252031b3964320d3e6e1dfa4d4ed0 [file] [log] [blame]
Russell King5e742ad2005-08-18 10:08:15 +01001/*
2 * linux/drivers/mfd/mcp-sa11x0.c
3 *
4 * Copyright (C) 2001-2005 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License.
9 *
10 * SA11x0 MCP (Multimedia Communications Port) driver.
11 *
12 * MCP read/write timeouts from Jordi Colomer, rehacked by rmk.
13 */
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/errno.h>
17#include <linux/kernel.h>
18#include <linux/delay.h>
19#include <linux/spinlock.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010020#include <linux/platform_device.h>
Thomas Kunzec8602ed2009-02-10 14:54:57 +010021#include <linux/mfd/mcp.h>
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010022#include <linux/io.h>
Russell King5e742ad2005-08-18 10:08:15 +010023
Russell Kingdcea83a2008-11-29 11:40:28 +000024#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Russell King5e742ad2005-08-18 10:08:15 +010026#include <asm/mach-types.h>
27#include <asm/system.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/mcp.h>
Russell King5e742ad2005-08-18 10:08:15 +010029
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010030/* Register offsets */
31#define MCCR0 0x00
32#define MCDR0 0x08
33#define MCDR1 0x0C
34#define MCDR2 0x10
35#define MCSR 0x18
36#define MCCR1 0x00
Russell King5e742ad2005-08-18 10:08:15 +010037
38struct mcp_sa11x0 {
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010039 u32 mccr0;
40 u32 mccr1;
41 unsigned char *mccr0_base;
42 unsigned char *mccr1_base;
Russell King5e742ad2005-08-18 10:08:15 +010043};
44
45#define priv(mcp) ((struct mcp_sa11x0 *)mcp_priv(mcp))
46
47static void
48mcp_sa11x0_set_telecom_divisor(struct mcp *mcp, unsigned int divisor)
49{
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010050 struct mcp_sa11x0 *priv = priv(mcp);
Russell King5e742ad2005-08-18 10:08:15 +010051
52 divisor /= 32;
53
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010054 priv->mccr0 &= ~0x00007f00;
55 priv->mccr0 |= divisor << 8;
56 __raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
Russell King5e742ad2005-08-18 10:08:15 +010057}
58
59static void
60mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor)
61{
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010062 struct mcp_sa11x0 *priv = priv(mcp);
Russell King5e742ad2005-08-18 10:08:15 +010063
64 divisor /= 32;
65
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010066 priv->mccr0 &= ~0x0000007f;
67 priv->mccr0 |= divisor;
68 __raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
Russell King5e742ad2005-08-18 10:08:15 +010069}
70
71/*
72 * Write data to the device. The bit should be set after 3 subframe
73 * times (each frame is 64 clocks). We wait a maximum of 6 subframes.
74 * We really should try doing something more productive while we
75 * wait.
76 */
77static void
78mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val)
79{
80 int ret = -ETIME;
81 int i;
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010082 u32 mcpreg;
83 struct mcp_sa11x0 *priv = priv(mcp);
Russell King5e742ad2005-08-18 10:08:15 +010084
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010085 mcpreg = reg << 17 | MCDR2_Wr | (val & 0xffff);
86 __raw_writel(mcpreg, priv->mccr0_base + MCDR2);
Russell King5e742ad2005-08-18 10:08:15 +010087
88 for (i = 0; i < 2; i++) {
89 udelay(mcp->rw_timeout);
Jochen Friedrichaf9081a2011-11-27 22:00:55 +010090 mcpreg = __raw_readl(priv->mccr0_base + MCSR);
91 if (mcpreg & MCSR_CWC) {
Russell King5e742ad2005-08-18 10:08:15 +010092 ret = 0;
93 break;
94 }
95 }
96
97 if (ret < 0)
98 printk(KERN_WARNING "mcp: write timed out\n");
99}
100
101/*
102 * Read data from the device. The bit should be set after 3 subframe
103 * times (each frame is 64 clocks). We wait a maximum of 6 subframes.
104 * We really should try doing something more productive while we
105 * wait.
106 */
107static unsigned int
108mcp_sa11x0_read(struct mcp *mcp, unsigned int reg)
109{
110 int ret = -ETIME;
111 int i;
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100112 u32 mcpreg;
113 struct mcp_sa11x0 *priv = priv(mcp);
Russell King5e742ad2005-08-18 10:08:15 +0100114
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100115 mcpreg = reg << 17 | MCDR2_Rd;
116 __raw_writel(mcpreg, priv->mccr0_base + MCDR2);
Russell King5e742ad2005-08-18 10:08:15 +0100117
118 for (i = 0; i < 2; i++) {
119 udelay(mcp->rw_timeout);
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100120 mcpreg = __raw_readl(priv->mccr0_base + MCSR);
121 if (mcpreg & MCSR_CRC) {
122 ret = __raw_readl(priv->mccr0_base + MCDR2)
123 & 0xffff;
Russell King5e742ad2005-08-18 10:08:15 +0100124 break;
125 }
126 }
127
128 if (ret < 0)
129 printk(KERN_WARNING "mcp: read timed out\n");
130
131 return ret;
132}
133
134static void mcp_sa11x0_enable(struct mcp *mcp)
135{
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100136 struct mcp_sa11x0 *priv = priv(mcp);
137
138 __raw_writel(-1, priv->mccr0_base + MCSR);
139 priv->mccr0 |= MCCR0_MCE;
140 __raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
Russell King5e742ad2005-08-18 10:08:15 +0100141}
142
143static void mcp_sa11x0_disable(struct mcp *mcp)
144{
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100145 struct mcp_sa11x0 *priv = priv(mcp);
146
147 priv->mccr0 &= ~MCCR0_MCE;
148 __raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
Russell King5e742ad2005-08-18 10:08:15 +0100149}
150
151/*
152 * Our methods.
153 */
154static struct mcp_ops mcp_sa11x0 = {
155 .set_telecom_divisor = mcp_sa11x0_set_telecom_divisor,
156 .set_audio_divisor = mcp_sa11x0_set_audio_divisor,
157 .reg_write = mcp_sa11x0_write,
158 .reg_read = mcp_sa11x0_read,
159 .enable = mcp_sa11x0_enable,
160 .disable = mcp_sa11x0_disable,
161};
162
Russell King3ae5eae2005-11-09 22:32:44 +0000163static int mcp_sa11x0_probe(struct platform_device *pdev)
Russell King5e742ad2005-08-18 10:08:15 +0100164{
Russell King323cdfc2005-08-18 10:10:46 +0100165 struct mcp_plat_data *data = pdev->dev.platform_data;
Russell King5e742ad2005-08-18 10:08:15 +0100166 struct mcp *mcp;
167 int ret;
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100168 struct mcp_sa11x0 *priv;
169 struct resource *res_mem0, *res_mem1;
170 u32 size0, size1;
Russell King5e742ad2005-08-18 10:08:15 +0100171
Russell King323cdfc2005-08-18 10:10:46 +0100172 if (!data)
Russell King5e742ad2005-08-18 10:08:15 +0100173 return -ENODEV;
174
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +0100175 if (!data->codec)
176 return -ENODEV;
177
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100178 res_mem0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
179 if (!res_mem0)
180 return -ENODEV;
181 size0 = res_mem0->end - res_mem0->start + 1;
182
183 res_mem1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
184 if (!res_mem1)
185 return -ENODEV;
186 size1 = res_mem1->end - res_mem1->start + 1;
187
188 if (!request_mem_region(res_mem0->start, size0, "sa11x0-mcp"))
Russell King5e742ad2005-08-18 10:08:15 +0100189 return -EBUSY;
190
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100191 if (!request_mem_region(res_mem1->start, size1, "sa11x0-mcp")) {
192 ret = -EBUSY;
193 goto release;
194 }
195
Russell King5e742ad2005-08-18 10:08:15 +0100196 mcp = mcp_host_alloc(&pdev->dev, sizeof(struct mcp_sa11x0));
197 if (!mcp) {
198 ret = -ENOMEM;
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100199 goto release2;
Russell King5e742ad2005-08-18 10:08:15 +0100200 }
201
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100202 priv = priv(mcp);
203
Russell King5e742ad2005-08-18 10:08:15 +0100204 mcp->owner = THIS_MODULE;
205 mcp->ops = &mcp_sa11x0;
Russell King323cdfc2005-08-18 10:10:46 +0100206 mcp->sclk_rate = data->sclk_rate;
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100207 mcp->dma_audio_rd = DDAR_DevAdd(res_mem0->start + MCDR0)
208 + DDAR_DevRd + DDAR_Brst4 + DDAR_8BitDev;
209 mcp->dma_audio_wr = DDAR_DevAdd(res_mem0->start + MCDR0)
210 + DDAR_DevWr + DDAR_Brst4 + DDAR_8BitDev;
211 mcp->dma_telco_rd = DDAR_DevAdd(res_mem0->start + MCDR1)
212 + DDAR_DevRd + DDAR_Brst4 + DDAR_8BitDev;
213 mcp->dma_telco_wr = DDAR_DevAdd(res_mem0->start + MCDR1)
214 + DDAR_DevWr + DDAR_Brst4 + DDAR_8BitDev;
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +0100215 mcp->codec = data->codec;
Russell King5e742ad2005-08-18 10:08:15 +0100216
Russell King3ae5eae2005-11-09 22:32:44 +0000217 platform_set_drvdata(pdev, mcp);
Russell King5e742ad2005-08-18 10:08:15 +0100218
Russell King323cdfc2005-08-18 10:10:46 +0100219 /*
220 * Initialise device. Note that we initially
221 * set the sampling rate to minimum.
222 */
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100223 priv->mccr0_base = ioremap(res_mem0->start, size0);
224 priv->mccr1_base = ioremap(res_mem1->start, size1);
225
226 __raw_writel(-1, priv->mccr0_base + MCSR);
227 priv->mccr1 = data->mccr1;
228 priv->mccr0 = data->mccr0 | 0x7f7f;
229 __raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
230 __raw_writel(priv->mccr1, priv->mccr1_base + MCCR1);
Russell King5e742ad2005-08-18 10:08:15 +0100231
232 /*
233 * Calculate the read/write timeout (us) from the bit clock
234 * rate. This is the period for 3 64-bit frames. Always
235 * round this time up.
236 */
237 mcp->rw_timeout = (64 * 3 * 1000000 + mcp->sclk_rate - 1) /
238 mcp->sclk_rate;
239
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +0100240 ret = mcp_host_register(mcp, data->codec_pdata);
Russell King5e742ad2005-08-18 10:08:15 +0100241 if (ret == 0)
242 goto out;
243
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100244 release2:
245 release_mem_region(res_mem1->start, size1);
Russell King5e742ad2005-08-18 10:08:15 +0100246 release:
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100247 release_mem_region(res_mem0->start, size0);
Russell King3ae5eae2005-11-09 22:32:44 +0000248 platform_set_drvdata(pdev, NULL);
Russell King5e742ad2005-08-18 10:08:15 +0100249
250 out:
251 return ret;
252}
253
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100254static int mcp_sa11x0_remove(struct platform_device *pdev)
Russell King5e742ad2005-08-18 10:08:15 +0100255{
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100256 struct mcp *mcp = platform_get_drvdata(pdev);
257 struct mcp_sa11x0 *priv = priv(mcp);
258 struct resource *res_mem;
259 u32 size;
Russell King5e742ad2005-08-18 10:08:15 +0100260
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100261 platform_set_drvdata(pdev, NULL);
Russell King5e742ad2005-08-18 10:08:15 +0100262 mcp_host_unregister(mcp);
Russell King5e742ad2005-08-18 10:08:15 +0100263
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100264 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
265 if (res_mem) {
266 size = res_mem->end - res_mem->start + 1;
267 release_mem_region(res_mem->start, size);
268 }
269 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
270 if (res_mem) {
271 size = res_mem->end - res_mem->start + 1;
272 release_mem_region(res_mem->start, size);
273 }
274 iounmap(priv->mccr0_base);
275 iounmap(priv->mccr1_base);
Russell King5e742ad2005-08-18 10:08:15 +0100276 return 0;
277}
278
Russell King3ae5eae2005-11-09 22:32:44 +0000279static int mcp_sa11x0_suspend(struct platform_device *dev, pm_message_t state)
Russell King5e742ad2005-08-18 10:08:15 +0100280{
Russell King3ae5eae2005-11-09 22:32:44 +0000281 struct mcp *mcp = platform_get_drvdata(dev);
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100282 struct mcp_sa11x0 *priv = priv(mcp);
283 u32 mccr0;
Russell King5e742ad2005-08-18 10:08:15 +0100284
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100285 mccr0 = priv->mccr0 & ~MCCR0_MCE;
286 __raw_writel(mccr0, priv->mccr0_base + MCCR0);
Russell King9480e302005-10-28 09:52:56 -0700287
Russell King5e742ad2005-08-18 10:08:15 +0100288 return 0;
289}
290
Russell King3ae5eae2005-11-09 22:32:44 +0000291static int mcp_sa11x0_resume(struct platform_device *dev)
Russell King5e742ad2005-08-18 10:08:15 +0100292{
Russell King3ae5eae2005-11-09 22:32:44 +0000293 struct mcp *mcp = platform_get_drvdata(dev);
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100294 struct mcp_sa11x0 *priv = priv(mcp);
Russell King5e742ad2005-08-18 10:08:15 +0100295
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100296 __raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
297 __raw_writel(priv->mccr1, priv->mccr1_base + MCCR1);
Russell King9480e302005-10-28 09:52:56 -0700298
Russell King5e742ad2005-08-18 10:08:15 +0100299 return 0;
300}
301
302/*
303 * The driver for the SA11x0 MCP port.
304 */
Kay Sievers4f46d6e2008-07-25 01:45:47 -0700305MODULE_ALIAS("platform:sa11x0-mcp");
306
Russell King3ae5eae2005-11-09 22:32:44 +0000307static struct platform_driver mcp_sa11x0_driver = {
Russell King5e742ad2005-08-18 10:08:15 +0100308 .probe = mcp_sa11x0_probe,
309 .remove = mcp_sa11x0_remove,
310 .suspend = mcp_sa11x0_suspend,
311 .resume = mcp_sa11x0_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000312 .driver = {
313 .name = "sa11x0-mcp",
Jochen Friedrichaf9081a2011-11-27 22:00:55 +0100314 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +0000315 },
Russell King5e742ad2005-08-18 10:08:15 +0100316};
317
318/*
319 * This needs re-working
320 */
Mark Brown65349d62011-11-23 22:58:34 +0000321module_platform_driver(mcp_sa11x0_driver);
Russell King5e742ad2005-08-18 10:08:15 +0100322
323MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
324MODULE_DESCRIPTION("SA11x0 multimedia communications port driver");
325MODULE_LICENSE("GPL");