blob: 63be60bc3455396e764fa57661f497d5670b04a8 [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
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +010029static const struct mcp_device_id *mcp_match_id(const struct mcp_device_id *id,
30 const char *codec)
31{
32 while (id->name[0]) {
33 if (strcmp(codec, id->name) == 0)
34 return id;
35 id++;
36 }
37 return NULL;
38}
39
40const struct mcp_device_id *mcp_get_device_id(const struct mcp *mcp)
41{
42 const struct mcp_driver *driver =
43 to_mcp_driver(mcp->attached_device.driver);
44
45 return mcp_match_id(driver->id_table, mcp->codec);
46}
47EXPORT_SYMBOL(mcp_get_device_id);
48
Russell Kinga4e137a2005-08-18 10:06:59 +010049static int mcp_bus_match(struct device *dev, struct device_driver *drv)
50{
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +010051 const struct mcp *mcp = to_mcp(dev);
52 const struct mcp_driver *driver = to_mcp_driver(drv);
53
54 if (driver->id_table)
55 return !!mcp_match_id(driver->id_table, mcp->codec);
56
57 return 0;
Russell Kinga4e137a2005-08-18 10:06:59 +010058}
59
60static int mcp_bus_probe(struct device *dev)
61{
62 struct mcp *mcp = to_mcp(dev);
63 struct mcp_driver *drv = to_mcp_driver(dev->driver);
64
65 return drv->probe(mcp);
66}
67
68static int mcp_bus_remove(struct device *dev)
69{
70 struct mcp *mcp = to_mcp(dev);
71 struct mcp_driver *drv = to_mcp_driver(dev->driver);
72
73 drv->remove(mcp);
74 return 0;
75}
76
77static int mcp_bus_suspend(struct device *dev, pm_message_t state)
78{
79 struct mcp *mcp = to_mcp(dev);
80 int ret = 0;
81
82 if (dev->driver) {
83 struct mcp_driver *drv = to_mcp_driver(dev->driver);
84
85 ret = drv->suspend(mcp, state);
86 }
87 return ret;
88}
89
90static int mcp_bus_resume(struct device *dev)
91{
92 struct mcp *mcp = to_mcp(dev);
93 int ret = 0;
94
95 if (dev->driver) {
96 struct mcp_driver *drv = to_mcp_driver(dev->driver);
97
98 ret = drv->resume(mcp);
99 }
100 return ret;
101}
102
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +0100103static int mcp_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
104{
105 struct mcp *mcp = to_mcp(dev);
106
107 add_uevent_var(env, "MODALIAS=%s%s", MCP_MODULE_PREFIX, mcp->codec);
108 return 0;
109}
110
Russell Kinga4e137a2005-08-18 10:06:59 +0100111static struct bus_type mcp_bus_type = {
112 .name = "mcp",
113 .match = mcp_bus_match,
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +0100114 .uevent = mcp_bus_uevent,
Russell King413b4862006-01-05 14:39:56 +0000115 .probe = mcp_bus_probe,
116 .remove = mcp_bus_remove,
Russell Kinga4e137a2005-08-18 10:06:59 +0100117 .suspend = mcp_bus_suspend,
118 .resume = mcp_bus_resume,
119};
120
121/**
122 * mcp_set_telecom_divisor - set the telecom divisor
123 * @mcp: MCP interface structure
124 * @div: SIB clock divisor
125 *
126 * Set the telecom divisor on the MCP interface. The resulting
127 * sample rate is SIBCLOCK/div.
128 */
129void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div)
130{
131 spin_lock_irq(&mcp->lock);
132 mcp->ops->set_telecom_divisor(mcp, div);
133 spin_unlock_irq(&mcp->lock);
134}
135EXPORT_SYMBOL(mcp_set_telecom_divisor);
136
137/**
138 * mcp_set_audio_divisor - set the audio divisor
139 * @mcp: MCP interface structure
140 * @div: SIB clock divisor
141 *
142 * Set the audio divisor on the MCP interface.
143 */
144void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div)
145{
146 spin_lock_irq(&mcp->lock);
147 mcp->ops->set_audio_divisor(mcp, div);
148 spin_unlock_irq(&mcp->lock);
149}
150EXPORT_SYMBOL(mcp_set_audio_divisor);
151
152/**
153 * mcp_reg_write - write a device register
154 * @mcp: MCP interface structure
155 * @reg: 4-bit register index
156 * @val: 16-bit data value
157 *
158 * Write a device register. The MCP interface must be enabled
159 * to prevent this function hanging.
160 */
161void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val)
162{
163 unsigned long flags;
164
165 spin_lock_irqsave(&mcp->lock, flags);
166 mcp->ops->reg_write(mcp, reg, val);
167 spin_unlock_irqrestore(&mcp->lock, flags);
168}
169EXPORT_SYMBOL(mcp_reg_write);
170
171/**
172 * mcp_reg_read - read a device register
173 * @mcp: MCP interface structure
174 * @reg: 4-bit register index
175 *
176 * Read a device register and return its value. The MCP interface
177 * must be enabled to prevent this function hanging.
178 */
179unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg)
180{
181 unsigned long flags;
182 unsigned int val;
183
184 spin_lock_irqsave(&mcp->lock, flags);
185 val = mcp->ops->reg_read(mcp, reg);
186 spin_unlock_irqrestore(&mcp->lock, flags);
187
188 return val;
189}
190EXPORT_SYMBOL(mcp_reg_read);
191
192/**
193 * mcp_enable - enable the MCP interface
194 * @mcp: MCP interface to enable
195 *
196 * Enable the MCP interface. Each call to mcp_enable will need
197 * a corresponding call to mcp_disable to disable the interface.
198 */
199void mcp_enable(struct mcp *mcp)
200{
201 spin_lock_irq(&mcp->lock);
202 if (mcp->use_count++ == 0)
203 mcp->ops->enable(mcp);
204 spin_unlock_irq(&mcp->lock);
205}
206EXPORT_SYMBOL(mcp_enable);
207
208/**
209 * mcp_disable - disable the MCP interface
210 * @mcp: MCP interface to disable
211 *
212 * Disable the MCP interface. The MCP interface will only be
213 * disabled once the number of calls to mcp_enable matches the
214 * number of calls to mcp_disable.
215 */
216void mcp_disable(struct mcp *mcp)
217{
218 unsigned long flags;
219
220 spin_lock_irqsave(&mcp->lock, flags);
221 if (--mcp->use_count == 0)
222 mcp->ops->disable(mcp);
223 spin_unlock_irqrestore(&mcp->lock, flags);
224}
225EXPORT_SYMBOL(mcp_disable);
226
227static void mcp_release(struct device *dev)
228{
229 struct mcp *mcp = container_of(dev, struct mcp, attached_device);
230
231 kfree(mcp);
232}
233
234struct mcp *mcp_host_alloc(struct device *parent, size_t size)
235{
236 struct mcp *mcp;
237
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700238 mcp = kzalloc(sizeof(struct mcp) + size, GFP_KERNEL);
Russell Kinga4e137a2005-08-18 10:06:59 +0100239 if (mcp) {
Russell Kinga4e137a2005-08-18 10:06:59 +0100240 spin_lock_init(&mcp->lock);
241 mcp->attached_device.parent = parent;
242 mcp->attached_device.bus = &mcp_bus_type;
243 mcp->attached_device.dma_mask = parent->dma_mask;
244 mcp->attached_device.release = mcp_release;
245 }
246 return mcp;
247}
248EXPORT_SYMBOL(mcp_host_alloc);
249
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +0100250int mcp_host_register(struct mcp *mcp, void *pdata)
Russell Kinga4e137a2005-08-18 10:06:59 +0100251{
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +0100252 if (!mcp->codec)
253 return -EINVAL;
254
255 mcp->attached_device.platform_data = pdata;
Kay Sieversb2bf61f2009-03-24 16:38:23 -0700256 dev_set_name(&mcp->attached_device, "mcp0");
Jochen Friedrich5dd7bf52011-11-27 22:00:54 +0100257 request_module("%s%s", MCP_MODULE_PREFIX, mcp->codec);
Russell Kinga4e137a2005-08-18 10:06:59 +0100258 return device_register(&mcp->attached_device);
259}
260EXPORT_SYMBOL(mcp_host_register);
261
262void mcp_host_unregister(struct mcp *mcp)
263{
264 device_unregister(&mcp->attached_device);
265}
266EXPORT_SYMBOL(mcp_host_unregister);
267
268int mcp_driver_register(struct mcp_driver *mcpdrv)
269{
270 mcpdrv->drv.bus = &mcp_bus_type;
Russell Kinga4e137a2005-08-18 10:06:59 +0100271 return driver_register(&mcpdrv->drv);
272}
273EXPORT_SYMBOL(mcp_driver_register);
274
275void mcp_driver_unregister(struct mcp_driver *mcpdrv)
276{
277 driver_unregister(&mcpdrv->drv);
278}
279EXPORT_SYMBOL(mcp_driver_unregister);
280
281static int __init mcp_init(void)
282{
283 return bus_register(&mcp_bus_type);
284}
285
286static void __exit mcp_exit(void)
287{
288 bus_unregister(&mcp_bus_type);
289}
290
291module_init(mcp_init);
292module_exit(mcp_exit);
293
294MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
295MODULE_DESCRIPTION("Core multimedia communications port driver");
296MODULE_LICENSE("GPL");