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>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/mfd/mcp.h>
22 #include <asm/system.h>
25 #define to_mcp(d) container_of(d, struct mcp, attached_device)
26 #define to_mcp_driver(d) container_of(d, struct mcp_driver, drv)
28 static int mcp_bus_match(struct device *dev, struct device_driver *drv)
33 static int mcp_bus_probe(struct device *dev)
35 struct mcp *mcp = to_mcp(dev);
36 struct mcp_driver *drv = to_mcp_driver(dev->driver);
38 return drv->probe(mcp);
41 static int mcp_bus_remove(struct device *dev)
43 struct mcp *mcp = to_mcp(dev);
44 struct mcp_driver *drv = to_mcp_driver(dev->driver);
50 static struct bus_type mcp_bus_type = {
52 .match = mcp_bus_match,
53 .probe = mcp_bus_probe,
54 .remove = mcp_bus_remove,
58 * mcp_set_telecom_divisor - set the telecom divisor
59 * @mcp: MCP interface structure
60 * @div: SIB clock divisor
62 * Set the telecom divisor on the MCP interface. The resulting
63 * sample rate is SIBCLOCK/div.
65 void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div)
69 spin_lock_irqsave(&mcp->lock, flags);
70 mcp->ops->set_telecom_divisor(mcp, div);
71 spin_unlock_irqrestore(&mcp->lock, flags);
73 EXPORT_SYMBOL(mcp_set_telecom_divisor);
76 * mcp_set_audio_divisor - set the audio divisor
77 * @mcp: MCP interface structure
78 * @div: SIB clock divisor
80 * Set the audio divisor on the MCP interface.
82 void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div)
86 spin_lock_irqsave(&mcp->lock, flags);
87 mcp->ops->set_audio_divisor(mcp, div);
88 spin_unlock_irqrestore(&mcp->lock, flags);
90 EXPORT_SYMBOL(mcp_set_audio_divisor);
93 * mcp_reg_write - write a device register
94 * @mcp: MCP interface structure
95 * @reg: 4-bit register index
96 * @val: 16-bit data value
98 * Write a device register. The MCP interface must be enabled
99 * to prevent this function hanging.
101 void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val)
105 spin_lock_irqsave(&mcp->lock, flags);
106 mcp->ops->reg_write(mcp, reg, val);
107 spin_unlock_irqrestore(&mcp->lock, flags);
109 EXPORT_SYMBOL(mcp_reg_write);
112 * mcp_reg_read - read a device register
113 * @mcp: MCP interface structure
114 * @reg: 4-bit register index
116 * Read a device register and return its value. The MCP interface
117 * must be enabled to prevent this function hanging.
119 unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg)
124 spin_lock_irqsave(&mcp->lock, flags);
125 val = mcp->ops->reg_read(mcp, reg);
126 spin_unlock_irqrestore(&mcp->lock, flags);
130 EXPORT_SYMBOL(mcp_reg_read);
133 * mcp_enable - enable the MCP interface
134 * @mcp: MCP interface to enable
136 * Enable the MCP interface. Each call to mcp_enable will need
137 * a corresponding call to mcp_disable to disable the interface.
139 void mcp_enable(struct mcp *mcp)
142 spin_lock_irqsave(&mcp->lock, flags);
143 if (mcp->use_count++ == 0)
144 mcp->ops->enable(mcp);
145 spin_unlock_irqrestore(&mcp->lock, flags);
147 EXPORT_SYMBOL(mcp_enable);
150 * mcp_disable - disable the MCP interface
151 * @mcp: MCP interface to disable
153 * Disable the MCP interface. The MCP interface will only be
154 * disabled once the number of calls to mcp_enable matches the
155 * number of calls to mcp_disable.
157 void mcp_disable(struct mcp *mcp)
161 spin_lock_irqsave(&mcp->lock, flags);
162 if (--mcp->use_count == 0)
163 mcp->ops->disable(mcp);
164 spin_unlock_irqrestore(&mcp->lock, flags);
166 EXPORT_SYMBOL(mcp_disable);
168 static void mcp_release(struct device *dev)
170 struct mcp *mcp = container_of(dev, struct mcp, attached_device);
175 struct mcp *mcp_host_alloc(struct device *parent, size_t size)
179 mcp = kzalloc(sizeof(struct mcp) + size, GFP_KERNEL);
181 spin_lock_init(&mcp->lock);
182 device_initialize(&mcp->attached_device);
183 mcp->attached_device.parent = parent;
184 mcp->attached_device.bus = &mcp_bus_type;
185 mcp->attached_device.dma_mask = parent->dma_mask;
186 mcp->attached_device.release = mcp_release;
190 EXPORT_SYMBOL(mcp_host_alloc);
192 int mcp_host_add(struct mcp *mcp, void *pdata)
194 mcp->attached_device.platform_data = pdata;
195 dev_set_name(&mcp->attached_device, "mcp0");
196 return device_add(&mcp->attached_device);
198 EXPORT_SYMBOL(mcp_host_add);
200 void mcp_host_del(struct mcp *mcp)
202 device_del(&mcp->attached_device);
204 EXPORT_SYMBOL(mcp_host_del);
206 void mcp_host_free(struct mcp *mcp)
208 put_device(&mcp->attached_device);
210 EXPORT_SYMBOL(mcp_host_free);
212 int mcp_driver_register(struct mcp_driver *mcpdrv)
214 mcpdrv->drv.bus = &mcp_bus_type;
215 return driver_register(&mcpdrv->drv);
217 EXPORT_SYMBOL(mcp_driver_register);
219 void mcp_driver_unregister(struct mcp_driver *mcpdrv)
221 driver_unregister(&mcpdrv->drv);
223 EXPORT_SYMBOL(mcp_driver_unregister);
225 static int __init mcp_init(void)
227 return bus_register(&mcp_bus_type);
230 static void __exit mcp_exit(void)
232 bus_unregister(&mcp_bus_type);
235 module_init(mcp_init);
236 module_exit(mcp_exit);
238 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
239 MODULE_DESCRIPTION("Core multimedia communications port driver");
240 MODULE_LICENSE("GPL");