mcp-sa11x0.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/io.h>
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/delay.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm.h>
  23. #include <linux/mfd/mcp.h>
  24. #include <mach/hardware.h>
  25. #include <asm/mach-types.h>
  26. #include <mach/mcp.h>
  27. #define DRIVER_NAME "sa11x0-mcp"
  28. struct mcp_sa11x0 {
  29. void __iomem *base0;
  30. void __iomem *base1;
  31. u32 mccr0;
  32. u32 mccr1;
  33. };
  34. /* Register offsets */
  35. #define MCCR0(m) ((m)->base0 + 0x00)
  36. #define MCDR0(m) ((m)->base0 + 0x08)
  37. #define MCDR1(m) ((m)->base0 + 0x0c)
  38. #define MCDR2(m) ((m)->base0 + 0x10)
  39. #define MCSR(m) ((m)->base0 + 0x18)
  40. #define MCCR1(m) ((m)->base1 + 0x00)
  41. #define priv(mcp) ((struct mcp_sa11x0 *)mcp_priv(mcp))
  42. static void
  43. mcp_sa11x0_set_telecom_divisor(struct mcp *mcp, unsigned int divisor)
  44. {
  45. struct mcp_sa11x0 *m = priv(mcp);
  46. divisor /= 32;
  47. m->mccr0 &= ~0x00007f00;
  48. m->mccr0 |= divisor << 8;
  49. writel_relaxed(m->mccr0, MCCR0(m));
  50. }
  51. static void
  52. mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor)
  53. {
  54. struct mcp_sa11x0 *m = priv(mcp);
  55. divisor /= 32;
  56. m->mccr0 &= ~0x0000007f;
  57. m->mccr0 |= divisor;
  58. writel_relaxed(m->mccr0, MCCR0(m));
  59. }
  60. /*
  61. * Write data to the device. The bit should be set after 3 subframe
  62. * times (each frame is 64 clocks). We wait a maximum of 6 subframes.
  63. * We really should try doing something more productive while we
  64. * wait.
  65. */
  66. static void
  67. mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val)
  68. {
  69. struct mcp_sa11x0 *m = priv(mcp);
  70. int ret = -ETIME;
  71. int i;
  72. writel_relaxed(reg << 17 | MCDR2_Wr | (val & 0xffff), MCDR2(m));
  73. for (i = 0; i < 2; i++) {
  74. udelay(mcp->rw_timeout);
  75. if (readl_relaxed(MCSR(m)) & MCSR_CWC) {
  76. ret = 0;
  77. break;
  78. }
  79. }
  80. if (ret < 0)
  81. printk(KERN_WARNING "mcp: write timed out\n");
  82. }
  83. /*
  84. * Read data from the device. The bit should be set after 3 subframe
  85. * times (each frame is 64 clocks). We wait a maximum of 6 subframes.
  86. * We really should try doing something more productive while we
  87. * wait.
  88. */
  89. static unsigned int
  90. mcp_sa11x0_read(struct mcp *mcp, unsigned int reg)
  91. {
  92. struct mcp_sa11x0 *m = priv(mcp);
  93. int ret = -ETIME;
  94. int i;
  95. writel_relaxed(reg << 17 | MCDR2_Rd, MCDR2(m));
  96. for (i = 0; i < 2; i++) {
  97. udelay(mcp->rw_timeout);
  98. if (readl_relaxed(MCSR(m)) & MCSR_CRC) {
  99. ret = readl_relaxed(MCDR2(m)) & 0xffff;
  100. break;
  101. }
  102. }
  103. if (ret < 0)
  104. printk(KERN_WARNING "mcp: read timed out\n");
  105. return ret;
  106. }
  107. static void mcp_sa11x0_enable(struct mcp *mcp)
  108. {
  109. struct mcp_sa11x0 *m = priv(mcp);
  110. writel(-1, MCSR(m));
  111. m->mccr0 |= MCCR0_MCE;
  112. writel_relaxed(m->mccr0, MCCR0(m));
  113. }
  114. static void mcp_sa11x0_disable(struct mcp *mcp)
  115. {
  116. struct mcp_sa11x0 *m = priv(mcp);
  117. m->mccr0 &= ~MCCR0_MCE;
  118. writel_relaxed(m->mccr0, MCCR0(m));
  119. }
  120. /*
  121. * Our methods.
  122. */
  123. static struct mcp_ops mcp_sa11x0 = {
  124. .set_telecom_divisor = mcp_sa11x0_set_telecom_divisor,
  125. .set_audio_divisor = mcp_sa11x0_set_audio_divisor,
  126. .reg_write = mcp_sa11x0_write,
  127. .reg_read = mcp_sa11x0_read,
  128. .enable = mcp_sa11x0_enable,
  129. .disable = mcp_sa11x0_disable,
  130. };
  131. static int mcp_sa11x0_probe(struct platform_device *dev)
  132. {
  133. struct mcp_plat_data *data = dev->dev.platform_data;
  134. struct resource *mem0, *mem1;
  135. struct mcp_sa11x0 *m;
  136. struct mcp *mcp;
  137. int ret;
  138. if (!data)
  139. return -ENODEV;
  140. mem0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
  141. mem1 = platform_get_resource(dev, IORESOURCE_MEM, 1);
  142. if (!mem0 || !mem1)
  143. return -ENXIO;
  144. if (!request_mem_region(mem0->start, resource_size(mem0),
  145. DRIVER_NAME)) {
  146. ret = -EBUSY;
  147. goto err_mem0;
  148. }
  149. if (!request_mem_region(mem1->start, resource_size(mem1),
  150. DRIVER_NAME)) {
  151. ret = -EBUSY;
  152. goto err_mem1;
  153. }
  154. mcp = mcp_host_alloc(&dev->dev, sizeof(struct mcp_sa11x0));
  155. if (!mcp) {
  156. ret = -ENOMEM;
  157. goto err_alloc;
  158. }
  159. mcp->owner = THIS_MODULE;
  160. mcp->ops = &mcp_sa11x0;
  161. mcp->sclk_rate = data->sclk_rate;
  162. m = priv(mcp);
  163. m->mccr0 = data->mccr0 | 0x7f7f;
  164. m->mccr1 = data->mccr1;
  165. m->base0 = ioremap(mem0->start, resource_size(mem0));
  166. m->base1 = ioremap(mem1->start, resource_size(mem1));
  167. if (!m->base0 || !m->base1) {
  168. ret = -ENOMEM;
  169. goto err_ioremap;
  170. }
  171. platform_set_drvdata(dev, mcp);
  172. /*
  173. * Initialise device. Note that we initially
  174. * set the sampling rate to minimum.
  175. */
  176. writel_relaxed(-1, MCSR(m));
  177. writel_relaxed(m->mccr1, MCCR1(m));
  178. writel_relaxed(m->mccr0, MCCR0(m));
  179. /*
  180. * Calculate the read/write timeout (us) from the bit clock
  181. * rate. This is the period for 3 64-bit frames. Always
  182. * round this time up.
  183. */
  184. mcp->rw_timeout = (64 * 3 * 1000000 + mcp->sclk_rate - 1) /
  185. mcp->sclk_rate;
  186. ret = mcp_host_add(mcp, data->codec_pdata);
  187. if (ret == 0)
  188. return 0;
  189. platform_set_drvdata(dev, NULL);
  190. err_ioremap:
  191. iounmap(m->base1);
  192. iounmap(m->base0);
  193. mcp_host_free(mcp);
  194. err_alloc:
  195. release_mem_region(mem1->start, resource_size(mem1));
  196. err_mem1:
  197. release_mem_region(mem0->start, resource_size(mem0));
  198. err_mem0:
  199. return ret;
  200. }
  201. static int mcp_sa11x0_remove(struct platform_device *dev)
  202. {
  203. struct mcp *mcp = platform_get_drvdata(dev);
  204. struct mcp_sa11x0 *m = priv(mcp);
  205. struct resource *mem0, *mem1;
  206. if (m->mccr0 & MCCR0_MCE)
  207. dev_warn(&dev->dev,
  208. "device left active (missing disable call?)\n");
  209. mem0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
  210. mem1 = platform_get_resource(dev, IORESOURCE_MEM, 1);
  211. platform_set_drvdata(dev, NULL);
  212. mcp_host_del(mcp);
  213. iounmap(m->base1);
  214. iounmap(m->base0);
  215. mcp_host_free(mcp);
  216. release_mem_region(mem1->start, resource_size(mem1));
  217. release_mem_region(mem0->start, resource_size(mem0));
  218. return 0;
  219. }
  220. #ifdef CONFIG_PM_SLEEP
  221. static int mcp_sa11x0_suspend(struct device *dev)
  222. {
  223. struct mcp_sa11x0 *m = priv(dev_get_drvdata(dev));
  224. if (m->mccr0 & MCCR0_MCE)
  225. dev_warn(dev, "device left active (missing disable call?)\n");
  226. writel(m->mccr0 & ~MCCR0_MCE, MCCR0(m));
  227. return 0;
  228. }
  229. static int mcp_sa11x0_resume(struct device *dev)
  230. {
  231. struct mcp_sa11x0 *m = priv(dev_get_drvdata(dev));
  232. writel_relaxed(m->mccr1, MCCR1(m));
  233. writel_relaxed(m->mccr0, MCCR0(m));
  234. return 0;
  235. }
  236. #endif
  237. static const struct dev_pm_ops mcp_sa11x0_pm_ops = {
  238. #ifdef CONFIG_PM_SLEEP
  239. .suspend = mcp_sa11x0_suspend,
  240. .freeze = mcp_sa11x0_suspend,
  241. .poweroff = mcp_sa11x0_suspend,
  242. .resume_noirq = mcp_sa11x0_resume,
  243. .thaw_noirq = mcp_sa11x0_resume,
  244. .restore_noirq = mcp_sa11x0_resume,
  245. #endif
  246. };
  247. static struct platform_driver mcp_sa11x0_driver = {
  248. .probe = mcp_sa11x0_probe,
  249. .remove = mcp_sa11x0_remove,
  250. .driver = {
  251. .name = DRIVER_NAME,
  252. .owner = THIS_MODULE,
  253. .pm = &mcp_sa11x0_pm_ops,
  254. },
  255. };
  256. /*
  257. * This needs re-working
  258. */
  259. module_platform_driver(mcp_sa11x0_driver);
  260. MODULE_ALIAS("platform:" DRIVER_NAME);
  261. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  262. MODULE_DESCRIPTION("SA11x0 multimedia communications port driver");
  263. MODULE_LICENSE("GPL");