Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[linux-flexiantxendom0-3.2.10.git] / drivers / mfd / mcp-core.c
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
22 #include <asm/system.h>
23
24
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)
27
28 static int mcp_bus_match(struct device *dev, struct device_driver *drv)
29 {
30         return 1;
31 }
32
33 static int mcp_bus_probe(struct device *dev)
34 {
35         struct mcp *mcp = to_mcp(dev);
36         struct mcp_driver *drv = to_mcp_driver(dev->driver);
37
38         return drv->probe(mcp);
39 }
40
41 static int mcp_bus_remove(struct device *dev)
42 {
43         struct mcp *mcp = to_mcp(dev);
44         struct mcp_driver *drv = to_mcp_driver(dev->driver);
45
46         drv->remove(mcp);
47         return 0;
48 }
49
50 static struct bus_type mcp_bus_type = {
51         .name           = "mcp",
52         .match          = mcp_bus_match,
53         .probe          = mcp_bus_probe,
54         .remove         = mcp_bus_remove,
55 };
56
57 /**
58  *      mcp_set_telecom_divisor - set the telecom divisor
59  *      @mcp: MCP interface structure
60  *      @div: SIB clock divisor
61  *
62  *      Set the telecom divisor on the MCP interface.  The resulting
63  *      sample rate is SIBCLOCK/div.
64  */
65 void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div)
66 {
67         unsigned long flags;
68
69         spin_lock_irqsave(&mcp->lock, flags);
70         mcp->ops->set_telecom_divisor(mcp, div);
71         spin_unlock_irqrestore(&mcp->lock, flags);
72 }
73 EXPORT_SYMBOL(mcp_set_telecom_divisor);
74
75 /**
76  *      mcp_set_audio_divisor - set the audio divisor
77  *      @mcp: MCP interface structure
78  *      @div: SIB clock divisor
79  *
80  *      Set the audio divisor on the MCP interface.
81  */
82 void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div)
83 {
84         unsigned long flags;
85
86         spin_lock_irqsave(&mcp->lock, flags);
87         mcp->ops->set_audio_divisor(mcp, div);
88         spin_unlock_irqrestore(&mcp->lock, flags);
89 }
90 EXPORT_SYMBOL(mcp_set_audio_divisor);
91
92 /**
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
97  *
98  *      Write a device register.  The MCP interface must be enabled
99  *      to prevent this function hanging.
100  */
101 void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val)
102 {
103         unsigned long flags;
104
105         spin_lock_irqsave(&mcp->lock, flags);
106         mcp->ops->reg_write(mcp, reg, val);
107         spin_unlock_irqrestore(&mcp->lock, flags);
108 }
109 EXPORT_SYMBOL(mcp_reg_write);
110
111 /**
112  *      mcp_reg_read - read a device register
113  *      @mcp: MCP interface structure
114  *      @reg: 4-bit register index
115  *
116  *      Read a device register and return its value.  The MCP interface
117  *      must be enabled to prevent this function hanging.
118  */
119 unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg)
120 {
121         unsigned long flags;
122         unsigned int val;
123
124         spin_lock_irqsave(&mcp->lock, flags);
125         val = mcp->ops->reg_read(mcp, reg);
126         spin_unlock_irqrestore(&mcp->lock, flags);
127
128         return val;
129 }
130 EXPORT_SYMBOL(mcp_reg_read);
131
132 /**
133  *      mcp_enable - enable the MCP interface
134  *      @mcp: MCP interface to enable
135  *
136  *      Enable the MCP interface.  Each call to mcp_enable will need
137  *      a corresponding call to mcp_disable to disable the interface.
138  */
139 void mcp_enable(struct mcp *mcp)
140 {
141         unsigned long flags;
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);
146 }
147 EXPORT_SYMBOL(mcp_enable);
148
149 /**
150  *      mcp_disable - disable the MCP interface
151  *      @mcp: MCP interface to disable
152  *
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.
156  */
157 void mcp_disable(struct mcp *mcp)
158 {
159         unsigned long flags;
160
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);
165 }
166 EXPORT_SYMBOL(mcp_disable);
167
168 static void mcp_release(struct device *dev)
169 {
170         struct mcp *mcp = container_of(dev, struct mcp, attached_device);
171
172         kfree(mcp);
173 }
174
175 struct mcp *mcp_host_alloc(struct device *parent, size_t size)
176 {
177         struct mcp *mcp;
178
179         mcp = kzalloc(sizeof(struct mcp) + size, GFP_KERNEL);
180         if (mcp) {
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;
187         }
188         return mcp;
189 }
190 EXPORT_SYMBOL(mcp_host_alloc);
191
192 int mcp_host_add(struct mcp *mcp, void *pdata)
193 {
194         mcp->attached_device.platform_data = pdata;
195         dev_set_name(&mcp->attached_device, "mcp0");
196         return device_add(&mcp->attached_device);
197 }
198 EXPORT_SYMBOL(mcp_host_add);
199
200 void mcp_host_del(struct mcp *mcp)
201 {
202         device_del(&mcp->attached_device);
203 }
204 EXPORT_SYMBOL(mcp_host_del);
205
206 void mcp_host_free(struct mcp *mcp)
207 {
208         put_device(&mcp->attached_device);
209 }
210 EXPORT_SYMBOL(mcp_host_free);
211
212 int mcp_driver_register(struct mcp_driver *mcpdrv)
213 {
214         mcpdrv->drv.bus = &mcp_bus_type;
215         return driver_register(&mcpdrv->drv);
216 }
217 EXPORT_SYMBOL(mcp_driver_register);
218
219 void mcp_driver_unregister(struct mcp_driver *mcpdrv)
220 {
221         driver_unregister(&mcpdrv->drv);
222 }
223 EXPORT_SYMBOL(mcp_driver_unregister);
224
225 static int __init mcp_init(void)
226 {
227         return bus_register(&mcp_bus_type);
228 }
229
230 static void __exit mcp_exit(void)
231 {
232         bus_unregister(&mcp_bus_type);
233 }
234
235 module_init(mcp_init);
236 module_exit(mcp_exit);
237
238 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
239 MODULE_DESCRIPTION("Core multimedia communications port driver");
240 MODULE_LICENSE("GPL");