mcp-core.c 6.0 KB

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