mcp-core.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * linux/drivers/mfd/mcp-core.c
  3. *
  4. * Copyright (C) 2001 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. * Generic MCP (Multimedia Communications Port) layer. All MCP locking
  11. * is solely held within this file.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/smp.h>
  17. #include <linux/device.h>
  18. #include <asm/dma.h>
  19. #include <asm/system.h>
  20. #include "mcp.h"
  21. #define to_mcp(d) container_of(d, struct mcp, attached_device)
  22. #define to_mcp_driver(d) container_of(d, struct mcp_driver, drv)
  23. static int mcp_bus_match(struct device *dev, struct device_driver *drv)
  24. {
  25. return 1;
  26. }
  27. static int mcp_bus_probe(struct device *dev)
  28. {
  29. struct mcp *mcp = to_mcp(dev);
  30. struct mcp_driver *drv = to_mcp_driver(dev->driver);
  31. return drv->probe(mcp);
  32. }
  33. static int mcp_bus_remove(struct device *dev)
  34. {
  35. struct mcp *mcp = to_mcp(dev);
  36. struct mcp_driver *drv = to_mcp_driver(dev->driver);
  37. drv->remove(mcp);
  38. return 0;
  39. }
  40. static int mcp_bus_suspend(struct device *dev, pm_message_t state)
  41. {
  42. struct mcp *mcp = to_mcp(dev);
  43. int ret = 0;
  44. if (dev->driver) {
  45. struct mcp_driver *drv = to_mcp_driver(dev->driver);
  46. ret = drv->suspend(mcp, state);
  47. }
  48. return ret;
  49. }
  50. static int mcp_bus_resume(struct device *dev)
  51. {
  52. struct mcp *mcp = to_mcp(dev);
  53. int ret = 0;
  54. if (dev->driver) {
  55. struct mcp_driver *drv = to_mcp_driver(dev->driver);
  56. ret = drv->resume(mcp);
  57. }
  58. return ret;
  59. }
  60. static struct bus_type mcp_bus_type = {
  61. .name = "mcp",
  62. .match = mcp_bus_match,
  63. .suspend = mcp_bus_suspend,
  64. .resume = mcp_bus_resume,
  65. };
  66. /**
  67. * mcp_set_telecom_divisor - set the telecom divisor
  68. * @mcp: MCP interface structure
  69. * @div: SIB clock divisor
  70. *
  71. * Set the telecom divisor on the MCP interface. The resulting
  72. * sample rate is SIBCLOCK/div.
  73. */
  74. void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div)
  75. {
  76. spin_lock_irq(&mcp->lock);
  77. mcp->ops->set_telecom_divisor(mcp, div);
  78. spin_unlock_irq(&mcp->lock);
  79. }
  80. EXPORT_SYMBOL(mcp_set_telecom_divisor);
  81. /**
  82. * mcp_set_audio_divisor - set the audio divisor
  83. * @mcp: MCP interface structure
  84. * @div: SIB clock divisor
  85. *
  86. * Set the audio divisor on the MCP interface.
  87. */
  88. void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div)
  89. {
  90. spin_lock_irq(&mcp->lock);
  91. mcp->ops->set_audio_divisor(mcp, div);
  92. spin_unlock_irq(&mcp->lock);
  93. }
  94. EXPORT_SYMBOL(mcp_set_audio_divisor);
  95. /**
  96. * mcp_reg_write - write a device register
  97. * @mcp: MCP interface structure
  98. * @reg: 4-bit register index
  99. * @val: 16-bit data value
  100. *
  101. * Write a device register. The MCP interface must be enabled
  102. * to prevent this function hanging.
  103. */
  104. void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val)
  105. {
  106. unsigned long flags;
  107. spin_lock_irqsave(&mcp->lock, flags);
  108. mcp->ops->reg_write(mcp, reg, val);
  109. spin_unlock_irqrestore(&mcp->lock, flags);
  110. }
  111. EXPORT_SYMBOL(mcp_reg_write);
  112. /**
  113. * mcp_reg_read - read a device register
  114. * @mcp: MCP interface structure
  115. * @reg: 4-bit register index
  116. *
  117. * Read a device register and return its value. The MCP interface
  118. * must be enabled to prevent this function hanging.
  119. */
  120. unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg)
  121. {
  122. unsigned long flags;
  123. unsigned int val;
  124. spin_lock_irqsave(&mcp->lock, flags);
  125. val = mcp->ops->reg_read(mcp, reg);
  126. spin_unlock_irqrestore(&mcp->lock, flags);
  127. return val;
  128. }
  129. EXPORT_SYMBOL(mcp_reg_read);
  130. /**
  131. * mcp_enable - enable the MCP interface
  132. * @mcp: MCP interface to enable
  133. *
  134. * Enable the MCP interface. Each call to mcp_enable will need
  135. * a corresponding call to mcp_disable to disable the interface.
  136. */
  137. void mcp_enable(struct mcp *mcp)
  138. {
  139. spin_lock_irq(&mcp->lock);
  140. if (mcp->use_count++ == 0)
  141. mcp->ops->enable(mcp);
  142. spin_unlock_irq(&mcp->lock);
  143. }
  144. EXPORT_SYMBOL(mcp_enable);
  145. /**
  146. * mcp_disable - disable the MCP interface
  147. * @mcp: MCP interface to disable
  148. *
  149. * Disable the MCP interface. The MCP interface will only be
  150. * disabled once the number of calls to mcp_enable matches the
  151. * number of calls to mcp_disable.
  152. */
  153. void mcp_disable(struct mcp *mcp)
  154. {
  155. unsigned long flags;
  156. spin_lock_irqsave(&mcp->lock, flags);
  157. if (--mcp->use_count == 0)
  158. mcp->ops->disable(mcp);
  159. spin_unlock_irqrestore(&mcp->lock, flags);
  160. }
  161. EXPORT_SYMBOL(mcp_disable);
  162. static void mcp_release(struct device *dev)
  163. {
  164. struct mcp *mcp = container_of(dev, struct mcp, attached_device);
  165. kfree(mcp);
  166. }
  167. struct mcp *mcp_host_alloc(struct device *parent, size_t size)
  168. {
  169. struct mcp *mcp;
  170. mcp = kmalloc(sizeof(struct mcp) + size, GFP_KERNEL);
  171. if (mcp) {
  172. memset(mcp, 0, sizeof(struct mcp) + size);
  173. spin_lock_init(&mcp->lock);
  174. mcp->attached_device.parent = parent;
  175. mcp->attached_device.bus = &mcp_bus_type;
  176. mcp->attached_device.dma_mask = parent->dma_mask;
  177. mcp->attached_device.release = mcp_release;
  178. }
  179. return mcp;
  180. }
  181. EXPORT_SYMBOL(mcp_host_alloc);
  182. int mcp_host_register(struct mcp *mcp)
  183. {
  184. strcpy(mcp->attached_device.bus_id, "mcp0");
  185. return device_register(&mcp->attached_device);
  186. }
  187. EXPORT_SYMBOL(mcp_host_register);
  188. void mcp_host_unregister(struct mcp *mcp)
  189. {
  190. device_unregister(&mcp->attached_device);
  191. }
  192. EXPORT_SYMBOL(mcp_host_unregister);
  193. int mcp_driver_register(struct mcp_driver *mcpdrv)
  194. {
  195. mcpdrv->drv.bus = &mcp_bus_type;
  196. mcpdrv->drv.probe = mcp_bus_probe;
  197. mcpdrv->drv.remove = mcp_bus_remove;
  198. return driver_register(&mcpdrv->drv);
  199. }
  200. EXPORT_SYMBOL(mcp_driver_register);
  201. void mcp_driver_unregister(struct mcp_driver *mcpdrv)
  202. {
  203. driver_unregister(&mcpdrv->drv);
  204. }
  205. EXPORT_SYMBOL(mcp_driver_unregister);
  206. static int __init mcp_init(void)
  207. {
  208. return bus_register(&mcp_bus_type);
  209. }
  210. static void __exit mcp_exit(void)
  211. {
  212. bus_unregister(&mcp_bus_type);
  213. }
  214. module_init(mcp_init);
  215. module_exit(mcp_exit);
  216. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  217. MODULE_DESCRIPTION("Core multimedia communications port driver");
  218. MODULE_LICENSE("GPL");