mcp-core.c 6.0 KB

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