mcp-core.c 5.6 KB

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