blob: 86cc3f7841cdfdb2714368b1459a5fd54ac6491a [file] [log] [blame]
Russell Kinga4e137a2005-08-18 10:06:59 +01001/*
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>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080018#include <linux/slab.h>
19#include <linux/string.h>
Thomas Kunzec8602ed2009-02-10 14:54:57 +010020#include <linux/mfd/mcp.h>
Russell Kinga4e137a2005-08-18 10:06:59 +010021
Russell Kingdcea83a2008-11-29 11:40:28 +000022#include <mach/dma.h>
Russell Kinga4e137a2005-08-18 10:06:59 +010023#include <asm/system.h>
24
Russell Kinga4e137a2005-08-18 10:06:59 +010025
26#define to_mcp(d) container_of(d, struct mcp, attached_device)
27#define to_mcp_driver(d) container_of(d, struct mcp_driver, drv)
28
29static int mcp_bus_match(struct device *dev, struct device_driver *drv)
30{
Russell King65f2e752012-01-20 17:38:58 +000031 return 1;
Russell Kinga4e137a2005-08-18 10:06:59 +010032}
33
34static int mcp_bus_probe(struct device *dev)
35{
36 struct mcp *mcp = to_mcp(dev);
37 struct mcp_driver *drv = to_mcp_driver(dev->driver);
38
39 return drv->probe(mcp);
40}
41
42static int mcp_bus_remove(struct device *dev)
43{
44 struct mcp *mcp = to_mcp(dev);
45 struct mcp_driver *drv = to_mcp_driver(dev->driver);
46
47 drv->remove(mcp);
48 return 0;
49}
50
51static int mcp_bus_suspend(struct device *dev, pm_message_t state)
52{
53 struct mcp *mcp = to_mcp(dev);
54 int ret = 0;
55
56 if (dev->driver) {
57 struct mcp_driver *drv = to_mcp_driver(dev->driver);
58
59 ret = drv->suspend(mcp, state);
60 }
61 return ret;
62}
63
64static int mcp_bus_resume(struct device *dev)
65{
66 struct mcp *mcp = to_mcp(dev);
67 int ret = 0;
68
69 if (dev->driver) {
70 struct mcp_driver *drv = to_mcp_driver(dev->driver);
71
72 ret = drv->resume(mcp);
73 }
74 return ret;
75}
76
77static struct bus_type mcp_bus_type = {
78 .name = "mcp",
79 .match = mcp_bus_match,
Russell King413b4862006-01-05 14:39:56 +000080 .probe = mcp_bus_probe,
81 .remove = mcp_bus_remove,
Russell Kinga4e137a2005-08-18 10:06:59 +010082 .suspend = mcp_bus_suspend,
83 .resume = mcp_bus_resume,
84};
85
86/**
87 * mcp_set_telecom_divisor - set the telecom divisor
88 * @mcp: MCP interface structure
89 * @div: SIB clock divisor
90 *
91 * Set the telecom divisor on the MCP interface. The resulting
92 * sample rate is SIBCLOCK/div.
93 */
94void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div)
95{
Russell King98250222012-01-14 08:49:46 +000096 unsigned long flags;
97
98 spin_lock_irqsave(&mcp->lock, flags);
Russell Kinga4e137a2005-08-18 10:06:59 +010099 mcp->ops->set_telecom_divisor(mcp, div);
Russell King98250222012-01-14 08:49:46 +0000100 spin_unlock_irqrestore(&mcp->lock, flags);
Russell Kinga4e137a2005-08-18 10:06:59 +0100101}
102EXPORT_SYMBOL(mcp_set_telecom_divisor);
103
104/**
105 * mcp_set_audio_divisor - set the audio divisor
106 * @mcp: MCP interface structure
107 * @div: SIB clock divisor
108 *
109 * Set the audio divisor on the MCP interface.
110 */
111void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div)
112{
Russell King98250222012-01-14 08:49:46 +0000113 unsigned long flags;
114
115 spin_lock_irqsave(&mcp->lock, flags);
Russell Kinga4e137a2005-08-18 10:06:59 +0100116 mcp->ops->set_audio_divisor(mcp, div);
Russell King98250222012-01-14 08:49:46 +0000117 spin_unlock_irqrestore(&mcp->lock, flags);
Russell Kinga4e137a2005-08-18 10:06:59 +0100118}
119EXPORT_SYMBOL(mcp_set_audio_divisor);
120
121/**
122 * mcp_reg_write - write a device register
123 * @mcp: MCP interface structure
124 * @reg: 4-bit register index
125 * @val: 16-bit data value
126 *
127 * Write a device register. The MCP interface must be enabled
128 * to prevent this function hanging.
129 */
130void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val)
131{
132 unsigned long flags;
133
134 spin_lock_irqsave(&mcp->lock, flags);
135 mcp->ops->reg_write(mcp, reg, val);
136 spin_unlock_irqrestore(&mcp->lock, flags);
137}
138EXPORT_SYMBOL(mcp_reg_write);
139
140/**
141 * mcp_reg_read - read a device register
142 * @mcp: MCP interface structure
143 * @reg: 4-bit register index
144 *
145 * Read a device register and return its value. The MCP interface
146 * must be enabled to prevent this function hanging.
147 */
148unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg)
149{
150 unsigned long flags;
151 unsigned int val;
152
153 spin_lock_irqsave(&mcp->lock, flags);
154 val = mcp->ops->reg_read(mcp, reg);
155 spin_unlock_irqrestore(&mcp->lock, flags);
156
157 return val;
158}
159EXPORT_SYMBOL(mcp_reg_read);
160
161/**
162 * mcp_enable - enable the MCP interface
163 * @mcp: MCP interface to enable
164 *
165 * Enable the MCP interface. Each call to mcp_enable will need
166 * a corresponding call to mcp_disable to disable the interface.
167 */
168void mcp_enable(struct mcp *mcp)
169{
Russell King98250222012-01-14 08:49:46 +0000170 unsigned long flags;
171 spin_lock_irqsave(&mcp->lock, flags);
Russell Kinga4e137a2005-08-18 10:06:59 +0100172 if (mcp->use_count++ == 0)
173 mcp->ops->enable(mcp);
Russell King98250222012-01-14 08:49:46 +0000174 spin_unlock_irqrestore(&mcp->lock, flags);
Russell Kinga4e137a2005-08-18 10:06:59 +0100175}
176EXPORT_SYMBOL(mcp_enable);
177
178/**
179 * mcp_disable - disable the MCP interface
180 * @mcp: MCP interface to disable
181 *
182 * Disable the MCP interface. The MCP interface will only be
183 * disabled once the number of calls to mcp_enable matches the
184 * number of calls to mcp_disable.
185 */
186void mcp_disable(struct mcp *mcp)
187{
188 unsigned long flags;
189
190 spin_lock_irqsave(&mcp->lock, flags);
191 if (--mcp->use_count == 0)
192 mcp->ops->disable(mcp);
193 spin_unlock_irqrestore(&mcp->lock, flags);
194}
195EXPORT_SYMBOL(mcp_disable);
196
197static void mcp_release(struct device *dev)
198{
199 struct mcp *mcp = container_of(dev, struct mcp, attached_device);
200
201 kfree(mcp);
202}
203
204struct mcp *mcp_host_alloc(struct device *parent, size_t size)
205{
206 struct mcp *mcp;
207
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700208 mcp = kzalloc(sizeof(struct mcp) + size, GFP_KERNEL);
Russell Kinga4e137a2005-08-18 10:06:59 +0100209 if (mcp) {
Russell Kinga4e137a2005-08-18 10:06:59 +0100210 spin_lock_init(&mcp->lock);
211 mcp->attached_device.parent = parent;
212 mcp->attached_device.bus = &mcp_bus_type;
213 mcp->attached_device.dma_mask = parent->dma_mask;
214 mcp->attached_device.release = mcp_release;
215 }
216 return mcp;
217}
218EXPORT_SYMBOL(mcp_host_alloc);
219
Russell King65f2e752012-01-20 17:38:58 +0000220int mcp_host_register(struct mcp *mcp)
Russell Kinga4e137a2005-08-18 10:06:59 +0100221{
Kay Sieversb2bf61f2009-03-24 16:38:23 -0700222 dev_set_name(&mcp->attached_device, "mcp0");
Russell Kinga4e137a2005-08-18 10:06:59 +0100223 return device_register(&mcp->attached_device);
224}
225EXPORT_SYMBOL(mcp_host_register);
226
227void mcp_host_unregister(struct mcp *mcp)
228{
229 device_unregister(&mcp->attached_device);
230}
231EXPORT_SYMBOL(mcp_host_unregister);
232
233int mcp_driver_register(struct mcp_driver *mcpdrv)
234{
235 mcpdrv->drv.bus = &mcp_bus_type;
Russell Kinga4e137a2005-08-18 10:06:59 +0100236 return driver_register(&mcpdrv->drv);
237}
238EXPORT_SYMBOL(mcp_driver_register);
239
240void mcp_driver_unregister(struct mcp_driver *mcpdrv)
241{
242 driver_unregister(&mcpdrv->drv);
243}
244EXPORT_SYMBOL(mcp_driver_unregister);
245
246static int __init mcp_init(void)
247{
248 return bus_register(&mcp_bus_type);
249}
250
251static void __exit mcp_exit(void)
252{
253 bus_unregister(&mcp_bus_type);
254}
255
256module_init(mcp_init);
257module_exit(mcp_exit);
258
259MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
260MODULE_DESCRIPTION("Core multimedia communications port driver");
261MODULE_LICENSE("GPL");