2 * linux/drivers/mfd/mcp-core.c
4 * Copyright (C) 2001 Russell King
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.
10 * Generic MCP (Multimedia Communications Port) layer. All MCP locking
11 * is solely held within this file.
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>
20 #include <asm/system.h>
24 #define to_mcp(d) container_of(d, struct mcp, attached_device)
25 #define to_mcp_driver(d) container_of(d, struct mcp_driver, drv)
27 static int mcp_bus_match(struct device *dev, struct device_driver *drv)
32 static int mcp_bus_probe(struct device *dev)
34 struct mcp *mcp = to_mcp(dev);
35 struct mcp_driver *drv = to_mcp_driver(dev->driver);
37 return drv->probe(mcp);
40 static int mcp_bus_remove(struct device *dev)
42 struct mcp *mcp = to_mcp(dev);
43 struct mcp_driver *drv = to_mcp_driver(dev->driver);
49 static int mcp_bus_suspend(struct device *dev, pm_message_t state)
51 struct mcp *mcp = to_mcp(dev);
55 struct mcp_driver *drv = to_mcp_driver(dev->driver);
57 ret = drv->suspend(mcp, state);
62 static int mcp_bus_resume(struct device *dev)
64 struct mcp *mcp = to_mcp(dev);
68 struct mcp_driver *drv = to_mcp_driver(dev->driver);
70 ret = drv->resume(mcp);
75 static struct bus_type mcp_bus_type = {
77 .match = mcp_bus_match,
78 .suspend = mcp_bus_suspend,
79 .resume = mcp_bus_resume,
83 * mcp_set_telecom_divisor - set the telecom divisor
84 * @mcp: MCP interface structure
85 * @div: SIB clock divisor
87 * Set the telecom divisor on the MCP interface. The resulting
88 * sample rate is SIBCLOCK/div.
90 void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div)
92 spin_lock_irq(&mcp->lock);
93 mcp->ops->set_telecom_divisor(mcp, div);
94 spin_unlock_irq(&mcp->lock);
96 EXPORT_SYMBOL(mcp_set_telecom_divisor);
99 * mcp_set_audio_divisor - set the audio divisor
100 * @mcp: MCP interface structure
101 * @div: SIB clock divisor
103 * Set the audio divisor on the MCP interface.
105 void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div)
107 spin_lock_irq(&mcp->lock);
108 mcp->ops->set_audio_divisor(mcp, div);
109 spin_unlock_irq(&mcp->lock);
111 EXPORT_SYMBOL(mcp_set_audio_divisor);
114 * mcp_reg_write - write a device register
115 * @mcp: MCP interface structure
116 * @reg: 4-bit register index
117 * @val: 16-bit data value
119 * Write a device register. The MCP interface must be enabled
120 * to prevent this function hanging.
122 void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val)
126 spin_lock_irqsave(&mcp->lock, flags);
127 mcp->ops->reg_write(mcp, reg, val);
128 spin_unlock_irqrestore(&mcp->lock, flags);
130 EXPORT_SYMBOL(mcp_reg_write);
133 * mcp_reg_read - read a device register
134 * @mcp: MCP interface structure
135 * @reg: 4-bit register index
137 * Read a device register and return its value. The MCP interface
138 * must be enabled to prevent this function hanging.
140 unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg)
145 spin_lock_irqsave(&mcp->lock, flags);
146 val = mcp->ops->reg_read(mcp, reg);
147 spin_unlock_irqrestore(&mcp->lock, flags);
151 EXPORT_SYMBOL(mcp_reg_read);
154 * mcp_enable - enable the MCP interface
155 * @mcp: MCP interface to enable
157 * Enable the MCP interface. Each call to mcp_enable will need
158 * a corresponding call to mcp_disable to disable the interface.
160 void mcp_enable(struct mcp *mcp)
162 spin_lock_irq(&mcp->lock);
163 if (mcp->use_count++ == 0)
164 mcp->ops->enable(mcp);
165 spin_unlock_irq(&mcp->lock);
167 EXPORT_SYMBOL(mcp_enable);
170 * mcp_disable - disable the MCP interface
171 * @mcp: MCP interface to disable
173 * Disable the MCP interface. The MCP interface will only be
174 * disabled once the number of calls to mcp_enable matches the
175 * number of calls to mcp_disable.
177 void mcp_disable(struct mcp *mcp)
181 spin_lock_irqsave(&mcp->lock, flags);
182 if (--mcp->use_count == 0)
183 mcp->ops->disable(mcp);
184 spin_unlock_irqrestore(&mcp->lock, flags);
186 EXPORT_SYMBOL(mcp_disable);
188 static void mcp_release(struct device *dev)
190 struct mcp *mcp = container_of(dev, struct mcp, attached_device);
195 struct mcp *mcp_host_alloc(struct device *parent, size_t size)
199 mcp = kmalloc(sizeof(struct mcp) + size, GFP_KERNEL);
201 memset(mcp, 0, sizeof(struct mcp) + size);
202 spin_lock_init(&mcp->lock);
203 mcp->attached_device.parent = parent;
204 mcp->attached_device.bus = &mcp_bus_type;
205 mcp->attached_device.dma_mask = parent->dma_mask;
206 mcp->attached_device.release = mcp_release;
210 EXPORT_SYMBOL(mcp_host_alloc);
212 int mcp_host_register(struct mcp *mcp)
214 strcpy(mcp->attached_device.bus_id, "mcp0");
215 return device_register(&mcp->attached_device);
217 EXPORT_SYMBOL(mcp_host_register);
219 void mcp_host_unregister(struct mcp *mcp)
221 device_unregister(&mcp->attached_device);
223 EXPORT_SYMBOL(mcp_host_unregister);
225 int mcp_driver_register(struct mcp_driver *mcpdrv)
227 mcpdrv->drv.bus = &mcp_bus_type;
228 mcpdrv->drv.probe = mcp_bus_probe;
229 mcpdrv->drv.remove = mcp_bus_remove;
230 return driver_register(&mcpdrv->drv);
232 EXPORT_SYMBOL(mcp_driver_register);
234 void mcp_driver_unregister(struct mcp_driver *mcpdrv)
236 driver_unregister(&mcpdrv->drv);
238 EXPORT_SYMBOL(mcp_driver_unregister);
240 static int __init mcp_init(void)
242 return bus_register(&mcp_bus_type);
245 static void __exit mcp_exit(void)
247 bus_unregister(&mcp_bus_type);
250 module_init(mcp_init);
251 module_exit(mcp_exit);
253 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
254 MODULE_DESCRIPTION("Core multimedia communications port driver");
255 MODULE_LICENSE("GPL");