mcp-sa11x0.c 7.7 KB
Newer Older
R
Russell King 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  linux/drivers/mfd/mcp-sa11x0.c
 *
 *  Copyright (C) 2001-2005 Russell King
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 *
 *  SA11x0 MCP (Multimedia Communications Port) driver.
 *
 *  MCP read/write timeouts from Jordi Colomer, rehacked by rmk.
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
20
#include <linux/platform_device.h>
21
#include <linux/mfd/mcp.h>
22
#include <linux/io.h>
R
Russell King 已提交
23

24
#include <mach/dma.h>
25
#include <mach/hardware.h>
R
Russell King 已提交
26 27
#include <asm/mach-types.h>
#include <asm/system.h>
28
#include <mach/mcp.h>
R
Russell King 已提交
29

30 31 32 33 34 35 36
/* Register offsets */
#define MCCR0	0x00
#define MCDR0	0x08
#define MCDR1	0x0C
#define MCDR2	0x10
#define MCSR	0x18
#define MCCR1	0x00
R
Russell King 已提交
37 38

struct mcp_sa11x0 {
39 40 41 42
	u32		mccr0;
	u32		mccr1;
	unsigned char	*mccr0_base;
	unsigned char	*mccr1_base;
R
Russell King 已提交
43 44 45 46 47 48 49
};

#define priv(mcp)	((struct mcp_sa11x0 *)mcp_priv(mcp))

static void
mcp_sa11x0_set_telecom_divisor(struct mcp *mcp, unsigned int divisor)
{
50
	struct mcp_sa11x0 *priv = priv(mcp);
R
Russell King 已提交
51 52 53

	divisor /= 32;

54 55 56
	priv->mccr0 &= ~0x00007f00;
	priv->mccr0 |= divisor << 8;
	__raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
R
Russell King 已提交
57 58 59 60 61
}

static void
mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor)
{
62
	struct mcp_sa11x0 *priv = priv(mcp);
R
Russell King 已提交
63 64 65

	divisor /= 32;

66 67 68
	priv->mccr0 &= ~0x0000007f;
	priv->mccr0 |= divisor;
	__raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
R
Russell King 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
}

/*
 * Write data to the device.  The bit should be set after 3 subframe
 * times (each frame is 64 clocks).  We wait a maximum of 6 subframes.
 * We really should try doing something more productive while we
 * wait.
 */
static void
mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val)
{
	int ret = -ETIME;
	int i;
82 83
	u32 mcpreg;
	struct mcp_sa11x0 *priv = priv(mcp);
R
Russell King 已提交
84

85 86
	mcpreg = reg << 17 | MCDR2_Wr | (val & 0xffff);
	__raw_writel(mcpreg, priv->mccr0_base + MCDR2);
R
Russell King 已提交
87 88 89

	for (i = 0; i < 2; i++) {
		udelay(mcp->rw_timeout);
90 91
		mcpreg = __raw_readl(priv->mccr0_base + MCSR);
		if (mcpreg & MCSR_CWC) {
R
Russell King 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
			ret = 0;
			break;
		}
	}

	if (ret < 0)
		printk(KERN_WARNING "mcp: write timed out\n");
}

/*
 * Read data from the device.  The bit should be set after 3 subframe
 * times (each frame is 64 clocks).  We wait a maximum of 6 subframes.
 * We really should try doing something more productive while we
 * wait.
 */
static unsigned int
mcp_sa11x0_read(struct mcp *mcp, unsigned int reg)
{
	int ret = -ETIME;
	int i;
112 113
	u32 mcpreg;
	struct mcp_sa11x0 *priv = priv(mcp);
R
Russell King 已提交
114

115 116
	mcpreg = reg << 17 | MCDR2_Rd;
	__raw_writel(mcpreg, priv->mccr0_base + MCDR2);
R
Russell King 已提交
117 118 119

	for (i = 0; i < 2; i++) {
		udelay(mcp->rw_timeout);
120 121 122 123
		mcpreg = __raw_readl(priv->mccr0_base + MCSR);
		if (mcpreg & MCSR_CRC) {
			ret = __raw_readl(priv->mccr0_base + MCDR2)
				& 0xffff;
R
Russell King 已提交
124 125 126 127 128 129 130 131 132 133 134 135
			break;
		}
	}

	if (ret < 0)
		printk(KERN_WARNING "mcp: read timed out\n");

	return ret;
}

static void mcp_sa11x0_enable(struct mcp *mcp)
{
136 137 138 139 140
	struct mcp_sa11x0 *priv = priv(mcp);

	__raw_writel(-1, priv->mccr0_base + MCSR);
	priv->mccr0 |= MCCR0_MCE;
	__raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
R
Russell King 已提交
141 142 143 144
}

static void mcp_sa11x0_disable(struct mcp *mcp)
{
145 146 147 148
	struct mcp_sa11x0 *priv = priv(mcp);

	priv->mccr0 &= ~MCCR0_MCE;
	__raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
R
Russell King 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162
}

/*
 * Our methods.
 */
static struct mcp_ops mcp_sa11x0 = {
	.set_telecom_divisor	= mcp_sa11x0_set_telecom_divisor,
	.set_audio_divisor	= mcp_sa11x0_set_audio_divisor,
	.reg_write		= mcp_sa11x0_write,
	.reg_read		= mcp_sa11x0_read,
	.enable			= mcp_sa11x0_enable,
	.disable		= mcp_sa11x0_disable,
};

163
static int mcp_sa11x0_probe(struct platform_device *pdev)
R
Russell King 已提交
164
{
165
	struct mcp_plat_data *data = pdev->dev.platform_data;
R
Russell King 已提交
166 167
	struct mcp *mcp;
	int ret;
168 169 170
	struct mcp_sa11x0 *priv;
	struct resource *res_mem0, *res_mem1;
	u32 size0, size1;
R
Russell King 已提交
171

172
	if (!data)
R
Russell King 已提交
173 174
		return -ENODEV;

175 176 177
	if (!data->codec)
		return -ENODEV;

178 179 180 181 182 183 184 185 186 187 188
	res_mem0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res_mem0)
		return -ENODEV;
	size0 = res_mem0->end - res_mem0->start + 1;

	res_mem1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	if (!res_mem1)
		return -ENODEV;
	size1 = res_mem1->end - res_mem1->start + 1;

	if (!request_mem_region(res_mem0->start, size0, "sa11x0-mcp"))
R
Russell King 已提交
189 190
		return -EBUSY;

191 192 193 194 195
	if (!request_mem_region(res_mem1->start, size1, "sa11x0-mcp")) {
		ret = -EBUSY;
		goto release;
	}

R
Russell King 已提交
196 197 198
	mcp = mcp_host_alloc(&pdev->dev, sizeof(struct mcp_sa11x0));
	if (!mcp) {
		ret = -ENOMEM;
199
		goto release2;
R
Russell King 已提交
200 201
	}

202 203
	priv = priv(mcp);

R
Russell King 已提交
204 205
	mcp->owner		= THIS_MODULE;
	mcp->ops		= &mcp_sa11x0;
206
	mcp->sclk_rate		= data->sclk_rate;
207 208 209 210 211 212 213 214
	mcp->dma_audio_rd	= DDAR_DevAdd(res_mem0->start + MCDR0)
				+ DDAR_DevRd + DDAR_Brst4 + DDAR_8BitDev;
	mcp->dma_audio_wr	= DDAR_DevAdd(res_mem0->start + MCDR0)
				+ DDAR_DevWr + DDAR_Brst4 + DDAR_8BitDev;
	mcp->dma_telco_rd	= DDAR_DevAdd(res_mem0->start + MCDR1)
				+ DDAR_DevRd + DDAR_Brst4 + DDAR_8BitDev;
	mcp->dma_telco_wr	= DDAR_DevAdd(res_mem0->start + MCDR1)
				+ DDAR_DevWr + DDAR_Brst4 + DDAR_8BitDev;
215
	mcp->codec		= data->codec;
R
Russell King 已提交
216

217
	platform_set_drvdata(pdev, mcp);
R
Russell King 已提交
218

219 220 221 222
	/*
	 * Initialise device.  Note that we initially
	 * set the sampling rate to minimum.
	 */
223 224 225 226 227 228 229 230
	priv->mccr0_base = ioremap(res_mem0->start, size0);
	priv->mccr1_base = ioremap(res_mem1->start, size1);

	__raw_writel(-1, priv->mccr0_base + MCSR);
	priv->mccr1 = data->mccr1;
	priv->mccr0 = data->mccr0 | 0x7f7f;
	__raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
	__raw_writel(priv->mccr1, priv->mccr1_base + MCCR1);
R
Russell King 已提交
231 232 233 234 235 236 237 238 239

	/*
	 * Calculate the read/write timeout (us) from the bit clock
	 * rate.  This is the period for 3 64-bit frames.  Always
	 * round this time up.
	 */
	mcp->rw_timeout = (64 * 3 * 1000000 + mcp->sclk_rate - 1) /
			  mcp->sclk_rate;

240
	ret = mcp_host_register(mcp, data->codec_pdata);
R
Russell King 已提交
241 242 243
	if (ret == 0)
		goto out;

244 245
 release2:
	release_mem_region(res_mem1->start, size1);
R
Russell King 已提交
246
 release:
247
	release_mem_region(res_mem0->start, size0);
248
	platform_set_drvdata(pdev, NULL);
R
Russell King 已提交
249 250 251 252 253

 out:
	return ret;
}

254
static int mcp_sa11x0_remove(struct platform_device *pdev)
R
Russell King 已提交
255
{
256 257 258 259
	struct mcp *mcp = platform_get_drvdata(pdev);
	struct mcp_sa11x0 *priv = priv(mcp);
	struct resource *res_mem;
	u32 size;
R
Russell King 已提交
260

261
	platform_set_drvdata(pdev, NULL);
R
Russell King 已提交
262 263
	mcp_host_unregister(mcp);

264 265 266 267 268 269 270 271 272 273 274 275
	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res_mem) {
		size = res_mem->end - res_mem->start + 1;
		release_mem_region(res_mem->start, size);
	}
	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	if (res_mem) {
		size = res_mem->end - res_mem->start + 1;
		release_mem_region(res_mem->start, size);
	}
	iounmap(priv->mccr0_base);
	iounmap(priv->mccr1_base);
R
Russell King 已提交
276 277 278
	return 0;
}

279
static int mcp_sa11x0_suspend(struct platform_device *dev, pm_message_t state)
R
Russell King 已提交
280
{
281
	struct mcp *mcp = platform_get_drvdata(dev);
282 283
	struct mcp_sa11x0 *priv = priv(mcp);
	u32 mccr0;
R
Russell King 已提交
284

285 286
	mccr0 = priv->mccr0 & ~MCCR0_MCE;
	__raw_writel(mccr0, priv->mccr0_base + MCCR0);
287

R
Russell King 已提交
288 289 290
	return 0;
}

291
static int mcp_sa11x0_resume(struct platform_device *dev)
R
Russell King 已提交
292
{
293
	struct mcp *mcp = platform_get_drvdata(dev);
294
	struct mcp_sa11x0 *priv = priv(mcp);
R
Russell King 已提交
295

296 297
	__raw_writel(priv->mccr0, priv->mccr0_base + MCCR0);
	__raw_writel(priv->mccr1, priv->mccr1_base + MCCR1);
298

R
Russell King 已提交
299 300 301 302 303 304
	return 0;
}

/*
 * The driver for the SA11x0 MCP port.
 */
305 306
MODULE_ALIAS("platform:sa11x0-mcp");

307
static struct platform_driver mcp_sa11x0_driver = {
R
Russell King 已提交
308 309 310 311
	.probe		= mcp_sa11x0_probe,
	.remove		= mcp_sa11x0_remove,
	.suspend	= mcp_sa11x0_suspend,
	.resume		= mcp_sa11x0_resume,
312 313
	.driver		= {
		.name	= "sa11x0-mcp",
314
		.owner  = THIS_MODULE,
315
	},
R
Russell King 已提交
316 317 318 319 320
};

/*
 * This needs re-working
 */
321
module_platform_driver(mcp_sa11x0_driver);
R
Russell King 已提交
322 323 324 325

MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
MODULE_DESCRIPTION("SA11x0 multimedia communications port driver");
MODULE_LICENSE("GPL");