staging: ks7010: separate dissimilar checks
[linux/fpc-iii.git] / drivers / mfd / mcp-core.c
blob7f5066e397527e27cf94df0baec9107f22e84246
1 /*
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>
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)
26 static int mcp_bus_match(struct device *dev, struct device_driver *drv)
28 return 1;
31 static int mcp_bus_probe(struct device *dev)
33 struct mcp *mcp = to_mcp(dev);
34 struct mcp_driver *drv = to_mcp_driver(dev->driver);
36 return drv->probe(mcp);
39 static int mcp_bus_remove(struct device *dev)
41 struct mcp *mcp = to_mcp(dev);
42 struct mcp_driver *drv = to_mcp_driver(dev->driver);
44 drv->remove(mcp);
45 return 0;
48 static struct bus_type mcp_bus_type = {
49 .name = "mcp",
50 .match = mcp_bus_match,
51 .probe = mcp_bus_probe,
52 .remove = mcp_bus_remove,
55 /**
56 * mcp_set_telecom_divisor - set the telecom divisor
57 * @mcp: MCP interface structure
58 * @div: SIB clock divisor
60 * Set the telecom divisor on the MCP interface. The resulting
61 * sample rate is SIBCLOCK/div.
63 void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div)
65 unsigned long flags;
67 spin_lock_irqsave(&mcp->lock, flags);
68 mcp->ops->set_telecom_divisor(mcp, div);
69 spin_unlock_irqrestore(&mcp->lock, flags);
71 EXPORT_SYMBOL(mcp_set_telecom_divisor);
73 /**
74 * mcp_set_audio_divisor - set the audio divisor
75 * @mcp: MCP interface structure
76 * @div: SIB clock divisor
78 * Set the audio divisor on the MCP interface.
80 void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div)
82 unsigned long flags;
84 spin_lock_irqsave(&mcp->lock, flags);
85 mcp->ops->set_audio_divisor(mcp, div);
86 spin_unlock_irqrestore(&mcp->lock, flags);
88 EXPORT_SYMBOL(mcp_set_audio_divisor);
90 /**
91 * mcp_reg_write - write a device register
92 * @mcp: MCP interface structure
93 * @reg: 4-bit register index
94 * @val: 16-bit data value
96 * Write a device register. The MCP interface must be enabled
97 * to prevent this function hanging.
99 void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val)
101 unsigned long flags;
103 spin_lock_irqsave(&mcp->lock, flags);
104 mcp->ops->reg_write(mcp, reg, val);
105 spin_unlock_irqrestore(&mcp->lock, flags);
107 EXPORT_SYMBOL(mcp_reg_write);
110 * mcp_reg_read - read a device register
111 * @mcp: MCP interface structure
112 * @reg: 4-bit register index
114 * Read a device register and return its value. The MCP interface
115 * must be enabled to prevent this function hanging.
117 unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg)
119 unsigned long flags;
120 unsigned int val;
122 spin_lock_irqsave(&mcp->lock, flags);
123 val = mcp->ops->reg_read(mcp, reg);
124 spin_unlock_irqrestore(&mcp->lock, flags);
126 return val;
128 EXPORT_SYMBOL(mcp_reg_read);
131 * mcp_enable - enable the MCP interface
132 * @mcp: MCP interface to enable
134 * Enable the MCP interface. Each call to mcp_enable will need
135 * a corresponding call to mcp_disable to disable the interface.
137 void mcp_enable(struct mcp *mcp)
139 unsigned long flags;
141 spin_lock_irqsave(&mcp->lock, flags);
142 if (mcp->use_count++ == 0)
143 mcp->ops->enable(mcp);
144 spin_unlock_irqrestore(&mcp->lock, flags);
146 EXPORT_SYMBOL(mcp_enable);
149 * mcp_disable - disable the MCP interface
150 * @mcp: MCP interface to disable
152 * Disable the MCP interface. The MCP interface will only be
153 * disabled once the number of calls to mcp_enable matches the
154 * number of calls to mcp_disable.
156 void mcp_disable(struct mcp *mcp)
158 unsigned long flags;
160 spin_lock_irqsave(&mcp->lock, flags);
161 if (--mcp->use_count == 0)
162 mcp->ops->disable(mcp);
163 spin_unlock_irqrestore(&mcp->lock, flags);
165 EXPORT_SYMBOL(mcp_disable);
167 static void mcp_release(struct device *dev)
169 struct mcp *mcp = container_of(dev, struct mcp, attached_device);
171 kfree(mcp);
174 struct mcp *mcp_host_alloc(struct device *parent, size_t size)
176 struct mcp *mcp;
178 mcp = kzalloc(sizeof(struct mcp) + size, GFP_KERNEL);
179 if (mcp) {
180 spin_lock_init(&mcp->lock);
181 device_initialize(&mcp->attached_device);
182 mcp->attached_device.parent = parent;
183 mcp->attached_device.bus = &mcp_bus_type;
184 mcp->attached_device.dma_mask = parent->dma_mask;
185 mcp->attached_device.release = mcp_release;
187 return mcp;
189 EXPORT_SYMBOL(mcp_host_alloc);
191 int mcp_host_add(struct mcp *mcp, void *pdata)
193 mcp->attached_device.platform_data = pdata;
194 dev_set_name(&mcp->attached_device, "mcp0");
195 return device_add(&mcp->attached_device);
197 EXPORT_SYMBOL(mcp_host_add);
199 void mcp_host_del(struct mcp *mcp)
201 device_del(&mcp->attached_device);
203 EXPORT_SYMBOL(mcp_host_del);
205 void mcp_host_free(struct mcp *mcp)
207 put_device(&mcp->attached_device);
209 EXPORT_SYMBOL(mcp_host_free);
211 int mcp_driver_register(struct mcp_driver *mcpdrv)
213 mcpdrv->drv.bus = &mcp_bus_type;
214 return driver_register(&mcpdrv->drv);
216 EXPORT_SYMBOL(mcp_driver_register);
218 void mcp_driver_unregister(struct mcp_driver *mcpdrv)
220 driver_unregister(&mcpdrv->drv);
222 EXPORT_SYMBOL(mcp_driver_unregister);
224 static int __init mcp_init(void)
226 return bus_register(&mcp_bus_type);
229 static void __exit mcp_exit(void)
231 bus_unregister(&mcp_bus_type);
234 module_init(mcp_init);
235 module_exit(mcp_exit);
237 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
238 MODULE_DESCRIPTION("Core multimedia communications port driver");
239 MODULE_LICENSE("GPL");