mcp-sa11x0.c 5.5 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>
R
Russell King 已提交
22

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

29 30
#include <mach/assabet.h>

R
Russell King 已提交
31 32

struct mcp_sa11x0 {
33 34
	u32	mccr0;
	u32	mccr1;
R
Russell King 已提交
35 36 37 38 39 40 41
};

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

static void
mcp_sa11x0_set_telecom_divisor(struct mcp *mcp, unsigned int divisor)
{
42
	unsigned int mccr0;
R
Russell King 已提交
43 44 45

	divisor /= 32;

46 47 48
	mccr0 = Ser4MCCR0 & ~0x00007f00;
	mccr0 |= divisor << 8;
	Ser4MCCR0 = mccr0;
R
Russell King 已提交
49 50 51 52 53
}

static void
mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor)
{
54
	unsigned int mccr0;
R
Russell King 已提交
55 56 57

	divisor /= 32;

58 59 60
	mccr0 = Ser4MCCR0 & ~0x0000007f;
	mccr0 |= divisor;
	Ser4MCCR0 = mccr0;
R
Russell King 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
}

/*
 * 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;

75
	Ser4MCDR2 = reg << 17 | MCDR2_Wr | (val & 0xffff);
R
Russell King 已提交
76 77 78

	for (i = 0; i < 2; i++) {
		udelay(mcp->rw_timeout);
79
		if (Ser4MCSR & MCSR_CWC) {
R
Russell King 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
			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;

101
	Ser4MCDR2 = reg << 17 | MCDR2_Rd;
R
Russell King 已提交
102 103 104

	for (i = 0; i < 2; i++) {
		udelay(mcp->rw_timeout);
105 106
		if (Ser4MCSR & MCSR_CRC) {
			ret = Ser4MCDR2 & 0xffff;
R
Russell King 已提交
107 108 109 110 111 112 113 114 115 116 117 118
			break;
		}
	}

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

	return ret;
}

static void mcp_sa11x0_enable(struct mcp *mcp)
{
119 120
	Ser4MCSR = -1;
	Ser4MCCR0 |= MCCR0_MCE;
R
Russell King 已提交
121 122 123 124
}

static void mcp_sa11x0_disable(struct mcp *mcp)
{
125
	Ser4MCCR0 &= ~MCCR0_MCE;
R
Russell King 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

/*
 * 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,
};

140
static int mcp_sa11x0_probe(struct platform_device *pdev)
R
Russell King 已提交
141
{
142
	struct mcp_plat_data *data = pdev->dev.platform_data;
R
Russell King 已提交
143 144 145
	struct mcp *mcp;
	int ret;

146
	if (!data)
R
Russell King 已提交
147 148
		return -ENODEV;

149
	if (!request_mem_region(0x80060000, 0x60, "sa11x0-mcp"))
R
Russell King 已提交
150 151 152 153 154
		return -EBUSY;

	mcp = mcp_host_alloc(&pdev->dev, sizeof(struct mcp_sa11x0));
	if (!mcp) {
		ret = -ENOMEM;
155
		goto release;
R
Russell King 已提交
156 157 158 159
	}

	mcp->owner		= THIS_MODULE;
	mcp->ops		= &mcp_sa11x0;
160
	mcp->sclk_rate		= data->sclk_rate;
161 162 163 164
	mcp->dma_audio_rd	= DMA_Ser4MCP0Rd;
	mcp->dma_audio_wr	= DMA_Ser4MCP0Wr;
	mcp->dma_telco_rd	= DMA_Ser4MCP1Rd;
	mcp->dma_telco_wr	= DMA_Ser4MCP1Wr;
165
	mcp->gpio_base		= data->gpio_base;
R
Russell King 已提交
166

167
	platform_set_drvdata(pdev, mcp);
R
Russell King 已提交
168

169 170 171 172 173 174 175 176 177 178 179 180 181
	if (machine_is_assabet()) {
		ASSABET_BCR_set(ASSABET_BCR_CODEC_RST);
	}

	/*
	 * Setup the PPC unit correctly.
	 */
	PPDR &= ~PPC_RXD4;
	PPDR |= PPC_TXD4 | PPC_SCLK | PPC_SFRM;
	PSDR |= PPC_RXD4;
	PSDR &= ~(PPC_TXD4 | PPC_SCLK | PPC_SFRM);
	PPSR &= ~(PPC_TXD4 | PPC_SCLK | PPC_SFRM);

182 183 184 185
	/*
	 * Initialise device.  Note that we initially
	 * set the sampling rate to minimum.
	 */
186 187 188
	Ser4MCSR = -1;
	Ser4MCCR1 = data->mccr1;
	Ser4MCCR0 = data->mccr0 | 0x7f7f;
R
Russell King 已提交
189 190 191 192 193 194 195 196 197

	/*
	 * 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;

198
	ret = mcp_host_register(mcp);
R
Russell King 已提交
199 200 201 202
	if (ret == 0)
		goto out;

 release:
203
	release_mem_region(0x80060000, 0x60);
204
	platform_set_drvdata(pdev, NULL);
R
Russell King 已提交
205 206 207 208 209

 out:
	return ret;
}

210
static int mcp_sa11x0_remove(struct platform_device *dev)
R
Russell King 已提交
211
{
212
	struct mcp *mcp = platform_get_drvdata(dev);
R
Russell King 已提交
213

214
	platform_set_drvdata(dev, NULL);
R
Russell King 已提交
215
	mcp_host_unregister(mcp);
216
	release_mem_region(0x80060000, 0x60);
R
Russell King 已提交
217 218 219 220

	return 0;
}

221
static int mcp_sa11x0_suspend(struct platform_device *dev, pm_message_t state)
R
Russell King 已提交
222
{
223
	struct mcp *mcp = platform_get_drvdata(dev);
R
Russell King 已提交
224

225 226 227
	priv(mcp)->mccr0 = Ser4MCCR0;
	priv(mcp)->mccr1 = Ser4MCCR1;
	Ser4MCCR0 &= ~MCCR0_MCE;
228

R
Russell King 已提交
229 230 231
	return 0;
}

232
static int mcp_sa11x0_resume(struct platform_device *dev)
R
Russell King 已提交
233
{
234
	struct mcp *mcp = platform_get_drvdata(dev);
R
Russell King 已提交
235

236 237
	Ser4MCCR1 = priv(mcp)->mccr1;
	Ser4MCCR0 = priv(mcp)->mccr0;
238

R
Russell King 已提交
239 240 241 242 243 244
	return 0;
}

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

247
static struct platform_driver mcp_sa11x0_driver = {
R
Russell King 已提交
248 249 250 251
	.probe		= mcp_sa11x0_probe,
	.remove		= mcp_sa11x0_remove,
	.suspend	= mcp_sa11x0_suspend,
	.resume		= mcp_sa11x0_resume,
252 253 254
	.driver		= {
		.name	= "sa11x0-mcp",
	},
R
Russell King 已提交
255 256 257 258 259
};

/*
 * This needs re-working
 */
260
module_platform_driver(mcp_sa11x0_driver);
R
Russell King 已提交
261 262 263 264

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