xref: /linux/drivers/mfd/mcp-sa11x0.c (revision 334a41ce9b753ec615e8c6c50ee07d6197190610)
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>
27a1fd844cSArnd Bergmann #include <linux/platform_data/mfd-mcp-sa11x0.h>
285e742ad6SRussell King 
29c4592ce4SRussell King #define DRIVER_NAME "sa11x0-mcp"
305e742ad6SRussell King 
315e742ad6SRussell King struct mcp_sa11x0 {
3245c7f75fSRussell King 	void __iomem	*base0;
3345c7f75fSRussell King 	void __iomem	*base1;
345e742ad6SRussell King 	u32		mccr0;
355e742ad6SRussell King 	u32		mccr1;
365e742ad6SRussell King };
375e742ad6SRussell King 
3845c7f75fSRussell King /* Register offsets */
3945c7f75fSRussell King #define MCCR0(m)	((m)->base0 + 0x00)
4045c7f75fSRussell King #define MCDR0(m)	((m)->base0 + 0x08)
4145c7f75fSRussell King #define MCDR1(m)	((m)->base0 + 0x0c)
4245c7f75fSRussell King #define MCDR2(m)	((m)->base0 + 0x10)
4345c7f75fSRussell King #define MCSR(m)		((m)->base0 + 0x18)
4445c7f75fSRussell King #define MCCR1(m)	((m)->base1 + 0x00)
4545c7f75fSRussell King 
465e742ad6SRussell King #define priv(mcp)	((struct mcp_sa11x0 *)mcp_priv(mcp))
475e742ad6SRussell King 
485e742ad6SRussell King static void
495e742ad6SRussell King mcp_sa11x0_set_telecom_divisor(struct mcp *mcp, unsigned int divisor)
505e742ad6SRussell King {
5145c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
525e742ad6SRussell King 
535e742ad6SRussell King 	divisor /= 32;
545e742ad6SRussell King 
5545c7f75fSRussell King 	m->mccr0 &= ~0x00007f00;
5645c7f75fSRussell King 	m->mccr0 |= divisor << 8;
5745c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
585e742ad6SRussell King }
595e742ad6SRussell King 
605e742ad6SRussell King static void
615e742ad6SRussell King mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor)
625e742ad6SRussell King {
6345c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
645e742ad6SRussell King 
655e742ad6SRussell King 	divisor /= 32;
665e742ad6SRussell King 
6745c7f75fSRussell King 	m->mccr0 &= ~0x0000007f;
6845c7f75fSRussell King 	m->mccr0 |= divisor;
6945c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
705e742ad6SRussell King }
715e742ad6SRussell King 
725e742ad6SRussell King /*
735e742ad6SRussell King  * Write data to the device.  The bit should be set after 3 subframe
745e742ad6SRussell King  * times (each frame is 64 clocks).  We wait a maximum of 6 subframes.
755e742ad6SRussell King  * We really should try doing something more productive while we
765e742ad6SRussell King  * wait.
775e742ad6SRussell King  */
785e742ad6SRussell King static void
795e742ad6SRussell King mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val)
805e742ad6SRussell King {
8145c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
825e742ad6SRussell King 	int ret = -ETIME;
835e742ad6SRussell King 	int i;
845e742ad6SRussell King 
8545c7f75fSRussell King 	writel_relaxed(reg << 17 | MCDR2_Wr | (val & 0xffff), MCDR2(m));
865e742ad6SRussell King 
875e742ad6SRussell King 	for (i = 0; i < 2; i++) {
885e742ad6SRussell King 		udelay(mcp->rw_timeout);
8945c7f75fSRussell King 		if (readl_relaxed(MCSR(m)) & MCSR_CWC) {
905e742ad6SRussell King 			ret = 0;
915e742ad6SRussell King 			break;
925e742ad6SRussell King 		}
935e742ad6SRussell King 	}
945e742ad6SRussell King 
955e742ad6SRussell King 	if (ret < 0)
965e742ad6SRussell King 		printk(KERN_WARNING "mcp: write timed out\n");
975e742ad6SRussell King }
985e742ad6SRussell King 
995e742ad6SRussell King /*
1005e742ad6SRussell King  * Read data from the device.  The bit should be set after 3 subframe
1015e742ad6SRussell King  * times (each frame is 64 clocks).  We wait a maximum of 6 subframes.
1025e742ad6SRussell King  * We really should try doing something more productive while we
1035e742ad6SRussell King  * wait.
1045e742ad6SRussell King  */
1055e742ad6SRussell King static unsigned int
1065e742ad6SRussell King mcp_sa11x0_read(struct mcp *mcp, unsigned int reg)
1075e742ad6SRussell King {
10845c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
1095e742ad6SRussell King 	int ret = -ETIME;
1105e742ad6SRussell King 	int i;
1115e742ad6SRussell King 
11245c7f75fSRussell King 	writel_relaxed(reg << 17 | MCDR2_Rd, MCDR2(m));
1135e742ad6SRussell King 
1145e742ad6SRussell King 	for (i = 0; i < 2; i++) {
1155e742ad6SRussell King 		udelay(mcp->rw_timeout);
11645c7f75fSRussell King 		if (readl_relaxed(MCSR(m)) & MCSR_CRC) {
11745c7f75fSRussell King 			ret = readl_relaxed(MCDR2(m)) & 0xffff;
1185e742ad6SRussell King 			break;
1195e742ad6SRussell King 		}
1205e742ad6SRussell King 	}
1215e742ad6SRussell King 
1225e742ad6SRussell King 	if (ret < 0)
1235e742ad6SRussell King 		printk(KERN_WARNING "mcp: read timed out\n");
1245e742ad6SRussell King 
1255e742ad6SRussell King 	return ret;
1265e742ad6SRussell King }
1275e742ad6SRussell King 
1285e742ad6SRussell King static void mcp_sa11x0_enable(struct mcp *mcp)
1295e742ad6SRussell King {
13045c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
13145c7f75fSRussell King 
13245c7f75fSRussell King 	writel(-1, MCSR(m));
13345c7f75fSRussell King 	m->mccr0 |= MCCR0_MCE;
13445c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
1355e742ad6SRussell King }
1365e742ad6SRussell King 
1375e742ad6SRussell King static void mcp_sa11x0_disable(struct mcp *mcp)
1385e742ad6SRussell King {
13945c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
14045c7f75fSRussell King 
14145c7f75fSRussell King 	m->mccr0 &= ~MCCR0_MCE;
14245c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
1435e742ad6SRussell King }
1445e742ad6SRussell King 
1455e742ad6SRussell King /*
1465e742ad6SRussell King  * Our methods.
1475e742ad6SRussell King  */
1485e742ad6SRussell King static struct mcp_ops mcp_sa11x0 = {
1495e742ad6SRussell King 	.set_telecom_divisor	= mcp_sa11x0_set_telecom_divisor,
1505e742ad6SRussell King 	.set_audio_divisor	= mcp_sa11x0_set_audio_divisor,
1515e742ad6SRussell King 	.reg_write		= mcp_sa11x0_write,
1525e742ad6SRussell King 	.reg_read		= mcp_sa11x0_read,
1535e742ad6SRussell King 	.enable			= mcp_sa11x0_enable,
1545e742ad6SRussell King 	.disable		= mcp_sa11x0_disable,
1555e742ad6SRussell King };
1565e742ad6SRussell King 
15745c7f75fSRussell King static int mcp_sa11x0_probe(struct platform_device *dev)
1585e742ad6SRussell King {
159*334a41ceSJingoo Han 	struct mcp_plat_data *data = dev_get_platdata(&dev->dev);
16045c7f75fSRussell King 	struct resource *mem0, *mem1;
16145c7f75fSRussell King 	struct mcp_sa11x0 *m;
1625e742ad6SRussell King 	struct mcp *mcp;
1635e742ad6SRussell King 	int ret;
1645e742ad6SRussell King 
165323cdfc1SRussell King 	if (!data)
1665e742ad6SRussell King 		return -ENODEV;
1675e742ad6SRussell King 
16845c7f75fSRussell King 	mem0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
16945c7f75fSRussell King 	mem1 = platform_get_resource(dev, IORESOURCE_MEM, 1);
17045c7f75fSRussell King 	if (!mem0 || !mem1)
17145c7f75fSRussell King 		return -ENXIO;
1725e742ad6SRussell King 
17345c7f75fSRussell King 	if (!request_mem_region(mem0->start, resource_size(mem0),
17445c7f75fSRussell King 				DRIVER_NAME)) {
17545c7f75fSRussell King 		ret = -EBUSY;
17645c7f75fSRussell King 		goto err_mem0;
17745c7f75fSRussell King 	}
17845c7f75fSRussell King 
17945c7f75fSRussell King 	if (!request_mem_region(mem1->start, resource_size(mem1),
18045c7f75fSRussell King 				DRIVER_NAME)) {
18145c7f75fSRussell King 		ret = -EBUSY;
18245c7f75fSRussell King 		goto err_mem1;
18345c7f75fSRussell King 	}
18445c7f75fSRussell King 
18545c7f75fSRussell King 	mcp = mcp_host_alloc(&dev->dev, sizeof(struct mcp_sa11x0));
1865e742ad6SRussell King 	if (!mcp) {
1875e742ad6SRussell King 		ret = -ENOMEM;
18845c7f75fSRussell King 		goto err_alloc;
1895e742ad6SRussell King 	}
1905e742ad6SRussell King 
1915e742ad6SRussell King 	mcp->owner		= THIS_MODULE;
1925e742ad6SRussell King 	mcp->ops		= &mcp_sa11x0;
193323cdfc1SRussell King 	mcp->sclk_rate		= data->sclk_rate;
1945e742ad6SRussell King 
19545c7f75fSRussell King 	m = priv(mcp);
19645c7f75fSRussell King 	m->mccr0 = data->mccr0 | 0x7f7f;
19745c7f75fSRussell King 	m->mccr1 = data->mccr1;
19845c7f75fSRussell King 
19945c7f75fSRussell King 	m->base0 = ioremap(mem0->start, resource_size(mem0));
20045c7f75fSRussell King 	m->base1 = ioremap(mem1->start, resource_size(mem1));
20145c7f75fSRussell King 	if (!m->base0 || !m->base1) {
20245c7f75fSRussell King 		ret = -ENOMEM;
20345c7f75fSRussell King 		goto err_ioremap;
20445c7f75fSRussell King 	}
20545c7f75fSRussell King 
20645c7f75fSRussell King 	platform_set_drvdata(dev, mcp);
2075e742ad6SRussell King 
208216f63c4SRussell King 	/*
209323cdfc1SRussell King 	 * Initialise device.  Note that we initially
210323cdfc1SRussell King 	 * set the sampling rate to minimum.
211323cdfc1SRussell King 	 */
21245c7f75fSRussell King 	writel_relaxed(-1, MCSR(m));
21345c7f75fSRussell King 	writel_relaxed(m->mccr1, MCCR1(m));
21445c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
2155e742ad6SRussell King 
2165e742ad6SRussell King 	/*
2175e742ad6SRussell King 	 * Calculate the read/write timeout (us) from the bit clock
2185e742ad6SRussell King 	 * rate.  This is the period for 3 64-bit frames.  Always
2195e742ad6SRussell King 	 * round this time up.
2205e742ad6SRussell King 	 */
2215e742ad6SRussell King 	mcp->rw_timeout = (64 * 3 * 1000000 + mcp->sclk_rate - 1) /
2225e742ad6SRussell King 			  mcp->sclk_rate;
2235e742ad6SRussell King 
224abe06082SRussell King 	ret = mcp_host_add(mcp, data->codec_pdata);
2255e742ad6SRussell King 	if (ret == 0)
22645c7f75fSRussell King 		return 0;
2275e742ad6SRussell King 
22845c7f75fSRussell King  err_ioremap:
22945c7f75fSRussell King 	iounmap(m->base1);
23045c7f75fSRussell King 	iounmap(m->base0);
23130816ac0SRussell King 	mcp_host_free(mcp);
23245c7f75fSRussell King  err_alloc:
23345c7f75fSRussell King 	release_mem_region(mem1->start, resource_size(mem1));
23445c7f75fSRussell King  err_mem1:
23545c7f75fSRussell King 	release_mem_region(mem0->start, resource_size(mem0));
23645c7f75fSRussell King  err_mem0:
2375e742ad6SRussell King 	return ret;
2385e742ad6SRussell King }
2395e742ad6SRussell King 
240216f63c4SRussell King static int mcp_sa11x0_remove(struct platform_device *dev)
2415e742ad6SRussell King {
242216f63c4SRussell King 	struct mcp *mcp = platform_get_drvdata(dev);
24345c7f75fSRussell King 	struct mcp_sa11x0 *m = priv(mcp);
24445c7f75fSRussell King 	struct resource *mem0, *mem1;
24545c7f75fSRussell King 
246a4b54acfSRussell King 	if (m->mccr0 & MCCR0_MCE)
247a4b54acfSRussell King 		dev_warn(&dev->dev,
248a4b54acfSRussell King 			 "device left active (missing disable call?)\n");
249a4b54acfSRussell King 
25045c7f75fSRussell King 	mem0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
25145c7f75fSRussell King 	mem1 = platform_get_resource(dev, IORESOURCE_MEM, 1);
2525e742ad6SRussell King 
25330816ac0SRussell King 	mcp_host_del(mcp);
25445c7f75fSRussell King 	iounmap(m->base1);
25545c7f75fSRussell King 	iounmap(m->base0);
25630816ac0SRussell King 	mcp_host_free(mcp);
25745c7f75fSRussell King 	release_mem_region(mem1->start, resource_size(mem1));
25845c7f75fSRussell King 	release_mem_region(mem0->start, resource_size(mem0));
2595e742ad6SRussell King 
2605e742ad6SRussell King 	return 0;
2615e742ad6SRussell King }
2625e742ad6SRussell King 
2632796e397SRussell King #ifdef CONFIG_PM_SLEEP
2642796e397SRussell King static int mcp_sa11x0_suspend(struct device *dev)
2655e742ad6SRussell King {
2662796e397SRussell King 	struct mcp_sa11x0 *m = priv(dev_get_drvdata(dev));
2675e742ad6SRussell King 
268a4b54acfSRussell King 	if (m->mccr0 & MCCR0_MCE)
269a4b54acfSRussell King 		dev_warn(dev, "device left active (missing disable call?)\n");
270a4b54acfSRussell King 
27145c7f75fSRussell King 	writel(m->mccr0 & ~MCCR0_MCE, MCCR0(m));
2729480e307SRussell King 
2735e742ad6SRussell King 	return 0;
2745e742ad6SRussell King }
2755e742ad6SRussell King 
2762796e397SRussell King static int mcp_sa11x0_resume(struct device *dev)
2775e742ad6SRussell King {
2782796e397SRussell King 	struct mcp_sa11x0 *m = priv(dev_get_drvdata(dev));
2795e742ad6SRussell King 
28045c7f75fSRussell King 	writel_relaxed(m->mccr1, MCCR1(m));
28145c7f75fSRussell King 	writel_relaxed(m->mccr0, MCCR0(m));
2829480e307SRussell King 
2835e742ad6SRussell King 	return 0;
2845e742ad6SRussell King }
2852796e397SRussell King #endif
2862796e397SRussell King 
2872796e397SRussell King static const struct dev_pm_ops mcp_sa11x0_pm_ops = {
288a6aecae2SRussell King #ifdef CONFIG_PM_SLEEP
289a6aecae2SRussell King 	.suspend = mcp_sa11x0_suspend,
290a6aecae2SRussell King 	.freeze = mcp_sa11x0_suspend,
291a6aecae2SRussell King 	.poweroff = mcp_sa11x0_suspend,
292a6aecae2SRussell King 	.resume_noirq = mcp_sa11x0_resume,
293a6aecae2SRussell King 	.thaw_noirq = mcp_sa11x0_resume,
294a6aecae2SRussell King 	.restore_noirq = mcp_sa11x0_resume,
295a6aecae2SRussell King #endif
2962796e397SRussell King };
2975e742ad6SRussell King 
2983ae5eaecSRussell King static struct platform_driver mcp_sa11x0_driver = {
2995e742ad6SRussell King 	.probe		= mcp_sa11x0_probe,
3005e742ad6SRussell King 	.remove		= mcp_sa11x0_remove,
3013ae5eaecSRussell King 	.driver		= {
302c4592ce4SRussell King 		.name	= DRIVER_NAME,
303c4592ce4SRussell King 		.owner	= THIS_MODULE,
3042796e397SRussell King 		.pm	= &mcp_sa11x0_pm_ops,
3053ae5eaecSRussell King 	},
3065e742ad6SRussell King };
3075e742ad6SRussell King 
3085e742ad6SRussell King /*
3095e742ad6SRussell King  * This needs re-working
3105e742ad6SRussell King  */
31165349d60SMark Brown module_platform_driver(mcp_sa11x0_driver);
3125e742ad6SRussell King 
313c4592ce4SRussell King MODULE_ALIAS("platform:" DRIVER_NAME);
3145e742ad6SRussell King MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
3155e742ad6SRussell King MODULE_DESCRIPTION("SA11x0 multimedia communications port driver");
3165e742ad6SRussell King MODULE_LICENSE("GPL");
317