mcp-sa11x0.c 7.0 KB

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