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 #include <asm/system.h>
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)
29 static const struct mcp_device_id
*mcp_match_id(const struct mcp_device_id
*id
,
33 if (strcmp(codec
, id
->name
) == 0)
40 const struct mcp_device_id
*mcp_get_device_id(const struct mcp
*mcp
)
42 const struct mcp_driver
*driver
=
43 to_mcp_driver(mcp
->attached_device
.driver
);
45 return mcp_match_id(driver
->id_table
, mcp
->codec
);
47 EXPORT_SYMBOL(mcp_get_device_id
);
49 static int mcp_bus_match(struct device
*dev
, struct device_driver
*drv
)
51 const struct mcp
*mcp
= to_mcp(dev
);
52 const struct mcp_driver
*driver
= to_mcp_driver(drv
);
55 return !!mcp_match_id(driver
->id_table
, mcp
->codec
);
60 static int mcp_bus_probe(struct device
*dev
)
62 struct mcp
*mcp
= to_mcp(dev
);
63 struct mcp_driver
*drv
= to_mcp_driver(dev
->driver
);
65 return drv
->probe(mcp
);
68 static int mcp_bus_remove(struct device
*dev
)
70 struct mcp
*mcp
= to_mcp(dev
);
71 struct mcp_driver
*drv
= to_mcp_driver(dev
->driver
);
77 static int mcp_bus_suspend(struct device
*dev
, pm_message_t state
)
79 struct mcp
*mcp
= to_mcp(dev
);
83 struct mcp_driver
*drv
= to_mcp_driver(dev
->driver
);
85 ret
= drv
->suspend(mcp
, state
);
90 static int mcp_bus_resume(struct device
*dev
)
92 struct mcp
*mcp
= to_mcp(dev
);
96 struct mcp_driver
*drv
= to_mcp_driver(dev
->driver
);
98 ret
= drv
->resume(mcp
);
103 static int mcp_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
105 struct mcp
*mcp
= to_mcp(dev
);
107 add_uevent_var(env
, "MODALIAS=%s%s", MCP_MODULE_PREFIX
, mcp
->codec
);
111 static struct bus_type mcp_bus_type
= {
113 .match
= mcp_bus_match
,
114 .uevent
= mcp_bus_uevent
,
115 .probe
= mcp_bus_probe
,
116 .remove
= mcp_bus_remove
,
117 .suspend
= mcp_bus_suspend
,
118 .resume
= mcp_bus_resume
,
122 * mcp_set_telecom_divisor - set the telecom divisor
123 * @mcp: MCP interface structure
124 * @div: SIB clock divisor
126 * Set the telecom divisor on the MCP interface. The resulting
127 * sample rate is SIBCLOCK/div.
129 void mcp_set_telecom_divisor(struct mcp
*mcp
, unsigned int div
)
131 spin_lock_irq(&mcp
->lock
);
132 mcp
->ops
->set_telecom_divisor(mcp
, div
);
133 spin_unlock_irq(&mcp
->lock
);
135 EXPORT_SYMBOL(mcp_set_telecom_divisor
);
138 * mcp_set_audio_divisor - set the audio divisor
139 * @mcp: MCP interface structure
140 * @div: SIB clock divisor
142 * Set the audio divisor on the MCP interface.
144 void mcp_set_audio_divisor(struct mcp
*mcp
, unsigned int div
)
146 spin_lock_irq(&mcp
->lock
);
147 mcp
->ops
->set_audio_divisor(mcp
, div
);
148 spin_unlock_irq(&mcp
->lock
);
150 EXPORT_SYMBOL(mcp_set_audio_divisor
);
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
158 * Write a device register. The MCP interface must be enabled
159 * to prevent this function hanging.
161 void mcp_reg_write(struct mcp
*mcp
, unsigned int reg
, unsigned int val
)
165 spin_lock_irqsave(&mcp
->lock
, flags
);
166 mcp
->ops
->reg_write(mcp
, reg
, val
);
167 spin_unlock_irqrestore(&mcp
->lock
, flags
);
169 EXPORT_SYMBOL(mcp_reg_write
);
172 * mcp_reg_read - read a device register
173 * @mcp: MCP interface structure
174 * @reg: 4-bit register index
176 * Read a device register and return its value. The MCP interface
177 * must be enabled to prevent this function hanging.
179 unsigned int mcp_reg_read(struct mcp
*mcp
, unsigned int reg
)
184 spin_lock_irqsave(&mcp
->lock
, flags
);
185 val
= mcp
->ops
->reg_read(mcp
, reg
);
186 spin_unlock_irqrestore(&mcp
->lock
, flags
);
190 EXPORT_SYMBOL(mcp_reg_read
);
193 * mcp_enable - enable the MCP interface
194 * @mcp: MCP interface to enable
196 * Enable the MCP interface. Each call to mcp_enable will need
197 * a corresponding call to mcp_disable to disable the interface.
199 void mcp_enable(struct mcp
*mcp
)
201 spin_lock_irq(&mcp
->lock
);
202 if (mcp
->use_count
++ == 0)
203 mcp
->ops
->enable(mcp
);
204 spin_unlock_irq(&mcp
->lock
);
206 EXPORT_SYMBOL(mcp_enable
);
209 * mcp_disable - disable the MCP interface
210 * @mcp: MCP interface to disable
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.
216 void mcp_disable(struct mcp
*mcp
)
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
);
225 EXPORT_SYMBOL(mcp_disable
);
227 static void mcp_release(struct device
*dev
)
229 struct mcp
*mcp
= container_of(dev
, struct mcp
, attached_device
);
234 struct mcp
*mcp_host_alloc(struct device
*parent
, size_t size
)
238 mcp
= kzalloc(sizeof(struct mcp
) + size
, GFP_KERNEL
);
240 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
;
248 EXPORT_SYMBOL(mcp_host_alloc
);
250 int mcp_host_register(struct mcp
*mcp
, void *pdata
)
255 mcp
->attached_device
.platform_data
= pdata
;
256 dev_set_name(&mcp
->attached_device
, "mcp0");
257 request_module("%s%s", MCP_MODULE_PREFIX
, mcp
->codec
);
258 return device_register(&mcp
->attached_device
);
260 EXPORT_SYMBOL(mcp_host_register
);
262 void mcp_host_unregister(struct mcp
*mcp
)
264 device_unregister(&mcp
->attached_device
);
266 EXPORT_SYMBOL(mcp_host_unregister
);
268 int mcp_driver_register(struct mcp_driver
*mcpdrv
)
270 mcpdrv
->drv
.bus
= &mcp_bus_type
;
271 return driver_register(&mcpdrv
->drv
);
273 EXPORT_SYMBOL(mcp_driver_register
);
275 void mcp_driver_unregister(struct mcp_driver
*mcpdrv
)
277 driver_unregister(&mcpdrv
->drv
);
279 EXPORT_SYMBOL(mcp_driver_unregister
);
281 static int __init
mcp_init(void)
283 return bus_register(&mcp_bus_type
);
286 static void __exit
mcp_exit(void)
288 bus_unregister(&mcp_bus_type
);
291 module_init(mcp_init
);
292 module_exit(mcp_exit
);
294 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
295 MODULE_DESCRIPTION("Core multimedia communications port driver");
296 MODULE_LICENSE("GPL");