mcp-core.c 6.8 KB

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