Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 1 | /* |
| 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 King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Thomas Kunze | c8602ed | 2009-02-10 14:54:57 +0100 | [diff] [blame] | 21 | #include <linux/mfd/mcp.h> |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 22 | |
Russell King | dcea83a | 2008-11-29 11:40:28 +0000 | [diff] [blame] | 23 | #include <mach/dma.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 24 | #include <mach/hardware.h> |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 25 | #include <asm/mach-types.h> |
| 26 | #include <asm/system.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 27 | #include <mach/mcp.h> |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 28 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 29 | #include <mach/assabet.h> |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 30 | |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 31 | |
| 32 | struct mcp_sa11x0 { |
| 33 | u32 mccr0; |
| 34 | u32 mccr1; |
| 35 | }; |
| 36 | |
| 37 | #define priv(mcp) ((struct mcp_sa11x0 *)mcp_priv(mcp)) |
| 38 | |
| 39 | static void |
| 40 | mcp_sa11x0_set_telecom_divisor(struct mcp *mcp, unsigned int divisor) |
| 41 | { |
| 42 | unsigned int mccr0; |
| 43 | |
| 44 | divisor /= 32; |
| 45 | |
| 46 | mccr0 = Ser4MCCR0 & ~0x00007f00; |
| 47 | mccr0 |= divisor << 8; |
| 48 | Ser4MCCR0 = mccr0; |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor) |
| 53 | { |
| 54 | unsigned int mccr0; |
| 55 | |
| 56 | divisor /= 32; |
| 57 | |
| 58 | mccr0 = Ser4MCCR0 & ~0x0000007f; |
| 59 | mccr0 |= divisor; |
| 60 | Ser4MCCR0 = mccr0; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Write data to the device. The bit should be set after 3 subframe |
| 65 | * times (each frame is 64 clocks). We wait a maximum of 6 subframes. |
| 66 | * We really should try doing something more productive while we |
| 67 | * wait. |
| 68 | */ |
| 69 | static void |
| 70 | mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val) |
| 71 | { |
| 72 | int ret = -ETIME; |
| 73 | int i; |
| 74 | |
| 75 | Ser4MCDR2 = reg << 17 | MCDR2_Wr | (val & 0xffff); |
| 76 | |
| 77 | for (i = 0; i < 2; i++) { |
| 78 | udelay(mcp->rw_timeout); |
| 79 | if (Ser4MCSR & MCSR_CWC) { |
| 80 | ret = 0; |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (ret < 0) |
| 86 | printk(KERN_WARNING "mcp: write timed out\n"); |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Read data from the device. The bit should be set after 3 subframe |
| 91 | * times (each frame is 64 clocks). We wait a maximum of 6 subframes. |
| 92 | * We really should try doing something more productive while we |
| 93 | * wait. |
| 94 | */ |
| 95 | static unsigned int |
| 96 | mcp_sa11x0_read(struct mcp *mcp, unsigned int reg) |
| 97 | { |
| 98 | int ret = -ETIME; |
| 99 | int i; |
| 100 | |
| 101 | Ser4MCDR2 = reg << 17 | MCDR2_Rd; |
| 102 | |
| 103 | for (i = 0; i < 2; i++) { |
| 104 | udelay(mcp->rw_timeout); |
| 105 | if (Ser4MCSR & MCSR_CRC) { |
| 106 | ret = Ser4MCDR2 & 0xffff; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (ret < 0) |
| 112 | printk(KERN_WARNING "mcp: read timed out\n"); |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static void mcp_sa11x0_enable(struct mcp *mcp) |
| 118 | { |
| 119 | Ser4MCSR = -1; |
| 120 | Ser4MCCR0 |= MCCR0_MCE; |
| 121 | } |
| 122 | |
| 123 | static void mcp_sa11x0_disable(struct mcp *mcp) |
| 124 | { |
| 125 | Ser4MCCR0 &= ~MCCR0_MCE; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Our methods. |
| 130 | */ |
| 131 | static struct mcp_ops mcp_sa11x0 = { |
| 132 | .set_telecom_divisor = mcp_sa11x0_set_telecom_divisor, |
| 133 | .set_audio_divisor = mcp_sa11x0_set_audio_divisor, |
| 134 | .reg_write = mcp_sa11x0_write, |
| 135 | .reg_read = mcp_sa11x0_read, |
| 136 | .enable = mcp_sa11x0_enable, |
| 137 | .disable = mcp_sa11x0_disable, |
| 138 | }; |
| 139 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 140 | static int mcp_sa11x0_probe(struct platform_device *pdev) |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 141 | { |
Russell King | 323cdfc | 2005-08-18 10:10:46 +0100 | [diff] [blame] | 142 | struct mcp_plat_data *data = pdev->dev.platform_data; |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 143 | struct mcp *mcp; |
| 144 | int ret; |
| 145 | |
Russell King | 323cdfc | 2005-08-18 10:10:46 +0100 | [diff] [blame] | 146 | if (!data) |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 147 | return -ENODEV; |
| 148 | |
Jochen Friedrich | 5dd7bf5 | 2011-11-27 22:00:54 +0100 | [diff] [blame^] | 149 | if (!data->codec) |
| 150 | return -ENODEV; |
| 151 | |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 152 | if (!request_mem_region(0x80060000, 0x60, "sa11x0-mcp")) |
| 153 | return -EBUSY; |
| 154 | |
| 155 | mcp = mcp_host_alloc(&pdev->dev, sizeof(struct mcp_sa11x0)); |
| 156 | if (!mcp) { |
| 157 | ret = -ENOMEM; |
| 158 | goto release; |
| 159 | } |
| 160 | |
| 161 | mcp->owner = THIS_MODULE; |
| 162 | mcp->ops = &mcp_sa11x0; |
Russell King | 323cdfc | 2005-08-18 10:10:46 +0100 | [diff] [blame] | 163 | mcp->sclk_rate = data->sclk_rate; |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 164 | mcp->dma_audio_rd = DMA_Ser4MCP0Rd; |
| 165 | mcp->dma_audio_wr = DMA_Ser4MCP0Wr; |
| 166 | mcp->dma_telco_rd = DMA_Ser4MCP1Rd; |
| 167 | mcp->dma_telco_wr = DMA_Ser4MCP1Wr; |
Jochen Friedrich | 5dd7bf5 | 2011-11-27 22:00:54 +0100 | [diff] [blame^] | 168 | mcp->codec = data->codec; |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 169 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 170 | platform_set_drvdata(pdev, mcp); |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 171 | |
| 172 | if (machine_is_assabet()) { |
| 173 | ASSABET_BCR_set(ASSABET_BCR_CODEC_RST); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Setup the PPC unit correctly. |
| 178 | */ |
| 179 | PPDR &= ~PPC_RXD4; |
| 180 | PPDR |= PPC_TXD4 | PPC_SCLK | PPC_SFRM; |
| 181 | PSDR |= PPC_RXD4; |
| 182 | PSDR &= ~(PPC_TXD4 | PPC_SCLK | PPC_SFRM); |
| 183 | PPSR &= ~(PPC_TXD4 | PPC_SCLK | PPC_SFRM); |
| 184 | |
Russell King | 323cdfc | 2005-08-18 10:10:46 +0100 | [diff] [blame] | 185 | /* |
| 186 | * Initialise device. Note that we initially |
| 187 | * set the sampling rate to minimum. |
| 188 | */ |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 189 | Ser4MCSR = -1; |
Russell King | 323cdfc | 2005-08-18 10:10:46 +0100 | [diff] [blame] | 190 | Ser4MCCR1 = data->mccr1; |
| 191 | Ser4MCCR0 = data->mccr0 | 0x7f7f; |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * Calculate the read/write timeout (us) from the bit clock |
| 195 | * rate. This is the period for 3 64-bit frames. Always |
| 196 | * round this time up. |
| 197 | */ |
| 198 | mcp->rw_timeout = (64 * 3 * 1000000 + mcp->sclk_rate - 1) / |
| 199 | mcp->sclk_rate; |
| 200 | |
Jochen Friedrich | 5dd7bf5 | 2011-11-27 22:00:54 +0100 | [diff] [blame^] | 201 | ret = mcp_host_register(mcp, data->codec_pdata); |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 202 | if (ret == 0) |
| 203 | goto out; |
| 204 | |
| 205 | release: |
| 206 | release_mem_region(0x80060000, 0x60); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 207 | platform_set_drvdata(pdev, NULL); |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 208 | |
| 209 | out: |
| 210 | return ret; |
| 211 | } |
| 212 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 213 | static int mcp_sa11x0_remove(struct platform_device *dev) |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 214 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 215 | struct mcp *mcp = platform_get_drvdata(dev); |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 216 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 217 | platform_set_drvdata(dev, NULL); |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 218 | mcp_host_unregister(mcp); |
| 219 | release_mem_region(0x80060000, 0x60); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 224 | static int mcp_sa11x0_suspend(struct platform_device *dev, pm_message_t state) |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 225 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 226 | struct mcp *mcp = platform_get_drvdata(dev); |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 227 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 228 | priv(mcp)->mccr0 = Ser4MCCR0; |
| 229 | priv(mcp)->mccr1 = Ser4MCCR1; |
| 230 | Ser4MCCR0 &= ~MCCR0_MCE; |
| 231 | |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 235 | static int mcp_sa11x0_resume(struct platform_device *dev) |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 236 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 237 | struct mcp *mcp = platform_get_drvdata(dev); |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 238 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 239 | Ser4MCCR1 = priv(mcp)->mccr1; |
| 240 | Ser4MCCR0 = priv(mcp)->mccr0; |
| 241 | |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * The driver for the SA11x0 MCP port. |
| 247 | */ |
Kay Sievers | 4f46d6e | 2008-07-25 01:45:47 -0700 | [diff] [blame] | 248 | MODULE_ALIAS("platform:sa11x0-mcp"); |
| 249 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 250 | static struct platform_driver mcp_sa11x0_driver = { |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 251 | .probe = mcp_sa11x0_probe, |
| 252 | .remove = mcp_sa11x0_remove, |
| 253 | .suspend = mcp_sa11x0_suspend, |
| 254 | .resume = mcp_sa11x0_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 255 | .driver = { |
| 256 | .name = "sa11x0-mcp", |
| 257 | }, |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | /* |
| 261 | * This needs re-working |
| 262 | */ |
Mark Brown | 65349d6 | 2011-11-23 22:58:34 +0000 | [diff] [blame] | 263 | module_platform_driver(mcp_sa11x0_driver); |
Russell King | 5e742ad | 2005-08-18 10:08:15 +0100 | [diff] [blame] | 264 | |
| 265 | MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); |
| 266 | MODULE_DESCRIPTION("SA11x0 multimedia communications port driver"); |
| 267 | MODULE_LICENSE("GPL"); |