xref: /linux/drivers/mfd/mcp-sa11x0.c (revision abe06082d07fcb0673cb93338c1d6f037fdc375b)
15e742ad6SRussell King /*
25e742ad6SRussell King  *  linux/drivers/mfd/mcp-sa11x0.c
35e742ad6SRussell King  *
45e742ad6SRussell King  *  Copyright (C) 2001-2005 Russell King
55e742ad6SRussell King  *
65e742ad6SRussell King  * This program is free software; you can redistribute it and/or modify
75e742ad6SRussell King  * it under the terms of the GNU General Public License as published by
85e742ad6SRussell King  * the Free Software Foundation; either version 2 of the License.
95e742ad6SRussell King  *
105e742ad6SRussell King  *  SA11x0 MCP (Multimedia Communications Port) driver.
115e742ad6SRussell King  *
125e742ad6SRussell King  *  MCP read/write timeouts from Jordi Colomer, rehacked by rmk.
135e742ad6SRussell King  */
145e742ad6SRussell King #include <linux/module.h>
155e742ad6SRussell King #include <linux/init.h>
1645c7f75fSRussell King #include <linux/io.h>
175e742ad6SRussell King #include <linux/errno.h>
185e742ad6SRussell King #include <linux/kernel.h>
195e742ad6SRussell King #include <linux/delay.h>
205e742ad6SRussell King #include <linux/spinlock.h>
21d052d1beSRussell King #include <linux/platform_device.h>
222796e397SRussell King #include <linux/pm.h>
23c8602edfSThomas Kunze #include <linux/mfd/mcp.h>
245e742ad6SRussell King 
25a09e64fbSRussell King #include <mach/hardware.h>
265e742ad6SRussell King #include <asm/mach-types.h>
275e742ad6SRussell King #include <asm/system.h>
28a09e64fbSRussell King #include <mach/mcp.h>
295e742ad6SRussell King 
30216f63c4SRussell King #include <mach/assabet.h>
31216f63c4SRussell King 
32c4592ce4SRussell King #define DRIVER_NAME "sa11x0-mcp"
335e742ad6SRussell King 
345e742ad6SRussell King struct mcp_sa11x0 {
3545c7f75fSRussell King 	void __iomem	*base0;
3645c7f75fSRussell King 	void __iomem	*base1;
375e742ad6SRussell King 	u32		mccr0;
385e742ad6SRussell King 	u32		mccr1;
395e742ad6SRussell King };
405e742ad6SRussell King 
4145c7f75fSRussell King /* Register offsets */
4245c7f75fSRussell King #define MCCR0(m)	((m)->base0 + 0x00)
4345c7f75fSRussell King #define MCDR0(m)	((m)->base0 + 0x08)
4445c7f75fSRussell King #define MCDR1(m)	((m)->base0 + 0x0c)
4545c7f75fSRussell King #define MCDR2(m)	((m)->base0 + 0x10)
4645c7f75fSRussell King #define MCSR(m)		((m)->base0 + 0x18)
4745c7f75fSRussell King #define MCCR1(m)	((m)->base1 + 0x00)
4845c7f75fSRussell King 
495e742ad6SRussell King #define priv(mcp)	((struct mcp_sa11x0 *)mcp_priv(mcp))
505e742ad6SRussell King 
515e742ad6SRussell King static void
525e742ad6SRussell King mcp_sa11x0_set_telecom_divisor(struct mcp *mcp, unsigned int divisor)
535e742ad6SRussell King {
5445c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
555e742ad6SRussell King 
565e742ad6SRussell King 	divisor /= 32;
575e742ad6SRussell King 
5845c7f75fSRussell King 	m->mccr0 &= ~0x00007f00;
5945c7f75fSRussell King 	m->mccr0 |= divisor << 8;
6045c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
615e742ad6SRussell King }
625e742ad6SRussell King 
635e742ad6SRussell King static void
645e742ad6SRussell King mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor)
655e742ad6SRussell King {
6645c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
675e742ad6SRussell King 
685e742ad6SRussell King 	divisor /= 32;
695e742ad6SRussell King 
7045c7f75fSRussell King 	m->mccr0 &= ~0x0000007f;
7145c7f75fSRussell King 	m->mccr0 |= divisor;
7245c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
735e742ad6SRussell King }
745e742ad6SRussell King 
755e742ad6SRussell King /*
765e742ad6SRussell King  * Write data to the device.  The bit should be set after 3 subframe
775e742ad6SRussell King  * times (each frame is 64 clocks).  We wait a maximum of 6 subframes.
785e742ad6SRussell King  * We really should try doing something more productive while we
795e742ad6SRussell King  * wait.
805e742ad6SRussell King  */
815e742ad6SRussell King static void
825e742ad6SRussell King mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val)
835e742ad6SRussell King {
8445c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
855e742ad6SRussell King 	int ret = -ETIME;
865e742ad6SRussell King 	int i;
875e742ad6SRussell King 
8845c7f75fSRussell King 	writel_relaxed(reg << 17 | MCDR2_Wr | (val & 0xffff), MCDR2(m));
895e742ad6SRussell King 
905e742ad6SRussell King 	for (i = 0; i < 2; i++) {
915e742ad6SRussell King 		udelay(mcp->rw_timeout);
9245c7f75fSRussell King 		if (readl_relaxed(MCSR(m)) & MCSR_CWC) {
935e742ad6SRussell King 			ret = 0;
945e742ad6SRussell King 			break;
955e742ad6SRussell King 		}
965e742ad6SRussell King 	}
975e742ad6SRussell King 
985e742ad6SRussell King 	if (ret < 0)
995e742ad6SRussell King 		printk(KERN_WARNING "mcp: write timed out\n");
1005e742ad6SRussell King }
1015e742ad6SRussell King 
1025e742ad6SRussell King /*
1035e742ad6SRussell King  * Read data from the device.  The bit should be set after 3 subframe
1045e742ad6SRussell King  * times (each frame is 64 clocks).  We wait a maximum of 6 subframes.
1055e742ad6SRussell King  * We really should try doing something more productive while we
1065e742ad6SRussell King  * wait.
1075e742ad6SRussell King  */
1085e742ad6SRussell King static unsigned int
1095e742ad6SRussell King mcp_sa11x0_read(struct mcp *mcp, unsigned int reg)
1105e742ad6SRussell King {
11145c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
1125e742ad6SRussell King 	int ret = -ETIME;
1135e742ad6SRussell King 	int i;
1145e742ad6SRussell King 
11545c7f75fSRussell King 	writel_relaxed(reg << 17 | MCDR2_Rd, MCDR2(m));
1165e742ad6SRussell King 
1175e742ad6SRussell King 	for (i = 0; i < 2; i++) {
1185e742ad6SRussell King 		udelay(mcp->rw_timeout);
11945c7f75fSRussell King 		if (readl_relaxed(MCSR(m)) & MCSR_CRC) {
12045c7f75fSRussell King 			ret = readl_relaxed(MCDR2(m)) & 0xffff;
1215e742ad6SRussell King 			break;
1225e742ad6SRussell King 		}
1235e742ad6SRussell King 	}
1245e742ad6SRussell King 
1255e742ad6SRussell King 	if (ret < 0)
1265e742ad6SRussell King 		printk(KERN_WARNING "mcp: read timed out\n");
1275e742ad6SRussell King 
1285e742ad6SRussell King 	return ret;
1295e742ad6SRussell King }
1305e742ad6SRussell King 
1315e742ad6SRussell King static void mcp_sa11x0_enable(struct mcp *mcp)
1325e742ad6SRussell King {
13345c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
13445c7f75fSRussell King 
13545c7f75fSRussell King 	writel(-1, MCSR(m));
13645c7f75fSRussell King 	m->mccr0 |= MCCR0_MCE;
13745c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
1385e742ad6SRussell King }
1395e742ad6SRussell King 
1405e742ad6SRussell King static void mcp_sa11x0_disable(struct mcp *mcp)
1415e742ad6SRussell King {
14245c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
14345c7f75fSRussell King 
14445c7f75fSRussell King 	m->mccr0 &= ~MCCR0_MCE;
14545c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
1465e742ad6SRussell King }
1475e742ad6SRussell King 
1485e742ad6SRussell King /*
1495e742ad6SRussell King  * Our methods.
1505e742ad6SRussell King  */
1515e742ad6SRussell King static struct mcp_ops mcp_sa11x0 = {
1525e742ad6SRussell King 	.set_telecom_divisor	= mcp_sa11x0_set_telecom_divisor,
1535e742ad6SRussell King 	.set_audio_divisor	= mcp_sa11x0_set_audio_divisor,
1545e742ad6SRussell King 	.reg_write		= mcp_sa11x0_write,
1555e742ad6SRussell King 	.reg_read		= mcp_sa11x0_read,
1565e742ad6SRussell King 	.enable			= mcp_sa11x0_enable,
1575e742ad6SRussell King 	.disable		= mcp_sa11x0_disable,
1585e742ad6SRussell King };
1595e742ad6SRussell King 
16045c7f75fSRussell King static int mcp_sa11x0_probe(struct platform_device *dev)
1615e742ad6SRussell King {
16245c7f75fSRussell King 	struct mcp_plat_data *data = dev->dev.platform_data;
16345c7f75fSRussell King 	struct resource *mem0, *mem1;
16445c7f75fSRussell King 	struct mcp_sa11x0 *m;
1655e742ad6SRussell King 	struct mcp *mcp;
1665e742ad6SRussell King 	int ret;
1675e742ad6SRussell King 
168323cdfc1SRussell King 	if (!data)
1695e742ad6SRussell King 		return -ENODEV;
1705e742ad6SRussell King 
17145c7f75fSRussell King 	mem0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
17245c7f75fSRussell King 	mem1 = platform_get_resource(dev, IORESOURCE_MEM, 1);
17345c7f75fSRussell King 	if (!mem0 || !mem1)
17445c7f75fSRussell King 		return -ENXIO;
1755e742ad6SRussell King 
17645c7f75fSRussell King 	if (!request_mem_region(mem0->start, resource_size(mem0),
17745c7f75fSRussell King 				DRIVER_NAME)) {
17845c7f75fSRussell King 		ret = -EBUSY;
17945c7f75fSRussell King 		goto err_mem0;
18045c7f75fSRussell King 	}
18145c7f75fSRussell King 
18245c7f75fSRussell King 	if (!request_mem_region(mem1->start, resource_size(mem1),
18345c7f75fSRussell King 				DRIVER_NAME)) {
18445c7f75fSRussell King 		ret = -EBUSY;
18545c7f75fSRussell King 		goto err_mem1;
18645c7f75fSRussell King 	}
18745c7f75fSRussell King 
18845c7f75fSRussell King 	mcp = mcp_host_alloc(&dev->dev, sizeof(struct mcp_sa11x0));
1895e742ad6SRussell King 	if (!mcp) {
1905e742ad6SRussell King 		ret = -ENOMEM;
19145c7f75fSRussell King 		goto err_alloc;
1925e742ad6SRussell King 	}
1935e742ad6SRussell King 
1945e742ad6SRussell King 	mcp->owner		= THIS_MODULE;
1955e742ad6SRussell King 	mcp->ops		= &mcp_sa11x0;
196323cdfc1SRussell King 	mcp->sclk_rate		= data->sclk_rate;
1975e742ad6SRussell King 
19845c7f75fSRussell King 	m = priv(mcp);
19945c7f75fSRussell King 	m->mccr0 = data->mccr0 | 0x7f7f;
20045c7f75fSRussell King 	m->mccr1 = data->mccr1;
20145c7f75fSRussell King 
20245c7f75fSRussell King 	m->base0 = ioremap(mem0->start, resource_size(mem0));
20345c7f75fSRussell King 	m->base1 = ioremap(mem1->start, resource_size(mem1));
20445c7f75fSRussell King 	if (!m->base0 || !m->base1) {
20545c7f75fSRussell King 		ret = -ENOMEM;
20645c7f75fSRussell King 		goto err_ioremap;
20745c7f75fSRussell King 	}
20845c7f75fSRussell King 
20945c7f75fSRussell King 	platform_set_drvdata(dev, mcp);
2105e742ad6SRussell King 
211216f63c4SRussell King 	if (machine_is_assabet()) {
212216f63c4SRussell King 		ASSABET_BCR_set(ASSABET_BCR_CODEC_RST);
213216f63c4SRussell King 	}
214216f63c4SRussell King 
215216f63c4SRussell King 	/*
216323cdfc1SRussell King 	 * Initialise device.  Note that we initially
217323cdfc1SRussell King 	 * set the sampling rate to minimum.
218323cdfc1SRussell King 	 */
21945c7f75fSRussell King 	writel_relaxed(-1, MCSR(m));
22045c7f75fSRussell King 	writel_relaxed(m->mccr1, MCCR1(m));
22145c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
2225e742ad6SRussell King 
2235e742ad6SRussell King 	/*
2245e742ad6SRussell King 	 * Calculate the read/write timeout (us) from the bit clock
2255e742ad6SRussell King 	 * rate.  This is the period for 3 64-bit frames.  Always
2265e742ad6SRussell King 	 * round this time up.
2275e742ad6SRussell King 	 */
2285e742ad6SRussell King 	mcp->rw_timeout = (64 * 3 * 1000000 + mcp->sclk_rate - 1) /
2295e742ad6SRussell King 			  mcp->sclk_rate;
2305e742ad6SRussell King 
231*abe06082SRussell King 	ret = mcp_host_add(mcp, data->codec_pdata);
2325e742ad6SRussell King 	if (ret == 0)
23345c7f75fSRussell King 		return 0;
2345e742ad6SRussell King 
23545c7f75fSRussell King 	platform_set_drvdata(dev, NULL);
23645c7f75fSRussell King 
23745c7f75fSRussell King  err_ioremap:
23845c7f75fSRussell King 	iounmap(m->base1);
23945c7f75fSRussell King 	iounmap(m->base0);
24030816ac0SRussell King 	mcp_host_free(mcp);
24145c7f75fSRussell King  err_alloc:
24245c7f75fSRussell King 	release_mem_region(mem1->start, resource_size(mem1));
24345c7f75fSRussell King  err_mem1:
24445c7f75fSRussell King 	release_mem_region(mem0->start, resource_size(mem0));
24545c7f75fSRussell King  err_mem0:
2465e742ad6SRussell King 	return ret;
2475e742ad6SRussell King }
2485e742ad6SRussell King 
249216f63c4SRussell King static int mcp_sa11x0_remove(struct platform_device *dev)
2505e742ad6SRussell King {
251216f63c4SRussell King 	struct mcp *mcp = platform_get_drvdata(dev);
25245c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
25345c7f75fSRussell King 	struct resource *mem0, *mem1;
25445c7f75fSRussell King 
25545c7f75fSRussell King 	mem0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
25645c7f75fSRussell King 	mem1 = platform_get_resource(dev, IORESOURCE_MEM, 1);
2575e742ad6SRussell King 
258216f63c4SRussell King 	platform_set_drvdata(dev, NULL);
25930816ac0SRussell King 	mcp_host_del(mcp);
26045c7f75fSRussell King 	iounmap(m->base1);
26145c7f75fSRussell King 	iounmap(m->base0);
26230816ac0SRussell King 	mcp_host_free(mcp);
26345c7f75fSRussell King 	release_mem_region(mem1->start, resource_size(mem1));
26445c7f75fSRussell King 	release_mem_region(mem0->start, resource_size(mem0));
2655e742ad6SRussell King 
2665e742ad6SRussell King 	return 0;
2675e742ad6SRussell King }
2685e742ad6SRussell King 
2692796e397SRussell King #ifdef CONFIG_PM_SLEEP
2702796e397SRussell King static int mcp_sa11x0_suspend(struct device *dev)
2715e742ad6SRussell King {
2722796e397SRussell King 	struct mcp_sa11x0 *m = priv(dev_get_drvdata(dev));
2735e742ad6SRussell King 
27445c7f75fSRussell King 	writel(m->mccr0 & ~MCCR0_MCE, MCCR0(m));
2759480e307SRussell King 
2765e742ad6SRussell King 	return 0;
2775e742ad6SRussell King }
2785e742ad6SRussell King 
2792796e397SRussell King static int mcp_sa11x0_resume(struct device *dev)
2805e742ad6SRussell King {
2812796e397SRussell King 	struct mcp_sa11x0 *m = priv(dev_get_drvdata(dev));
2825e742ad6SRussell King 
28345c7f75fSRussell King 	writel_relaxed(m->mccr1, MCCR1(m));
28445c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
2859480e307SRussell King 
2865e742ad6SRussell King 	return 0;
2875e742ad6SRussell King }
2882796e397SRussell King #endif
2892796e397SRussell King 
2902796e397SRussell King static const struct dev_pm_ops mcp_sa11x0_pm_ops = {
291a6aecae2SRussell King #ifdef CONFIG_PM_SLEEP
292a6aecae2SRussell King 	.suspend = mcp_sa11x0_suspend,
293a6aecae2SRussell King 	.freeze = mcp_sa11x0_suspend,
294a6aecae2SRussell King 	.poweroff = mcp_sa11x0_suspend,
295a6aecae2SRussell King 	.resume_noirq = mcp_sa11x0_resume,
296a6aecae2SRussell King 	.thaw_noirq = mcp_sa11x0_resume,
297a6aecae2SRussell King 	.restore_noirq = mcp_sa11x0_resume,
298a6aecae2SRussell King #endif
2992796e397SRussell King };
3005e742ad6SRussell King 
3013ae5eaecSRussell King static struct platform_driver mcp_sa11x0_driver = {
3025e742ad6SRussell King 	.probe		= mcp_sa11x0_probe,
3035e742ad6SRussell King 	.remove		= mcp_sa11x0_remove,
3043ae5eaecSRussell King 	.driver		= {
305c4592ce4SRussell King 		.name	= DRIVER_NAME,
306c4592ce4SRussell King 		.owner	= THIS_MODULE,
3072796e397SRussell King 		.pm	= &mcp_sa11x0_pm_ops,
3083ae5eaecSRussell King 	},
3095e742ad6SRussell King };
3105e742ad6SRussell King 
3115e742ad6SRussell King /*
3125e742ad6SRussell King  * This needs re-working
3135e742ad6SRussell King  */
31465349d60SMark Brown module_platform_driver(mcp_sa11x0_driver);
3155e742ad6SRussell King 
316c4592ce4SRussell King MODULE_ALIAS("platform:" DRIVER_NAME);
3175e742ad6SRussell King MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
3185e742ad6SRussell King MODULE_DESCRIPTION("SA11x0 multimedia communications port driver");
3195e742ad6SRussell King MODULE_LICENSE("GPL");
320