1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/mfd/mcp-core.c
5 * Copyright (C) 2001 Russell King
7 * Generic MCP (Multimedia Communications Port) layer. All MCP locking
8 * is solely held within this file.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/smp.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/mfd/mcp.h>
20 #define to_mcp(d) container_of(d, struct mcp, attached_device)
21 #define to_mcp_driver(d) container_of(d, struct mcp_driver, drv)
23 static int mcp_bus_match(struct device *dev, struct device_driver *drv)
28 static int mcp_bus_probe(struct device *dev)
30 struct mcp *mcp = to_mcp(dev);
31 struct mcp_driver *drv = to_mcp_driver(dev->driver);
33 return drv->probe(mcp);
36 static int mcp_bus_remove(struct device *dev)
38 struct mcp *mcp = to_mcp(dev);
39 struct mcp_driver *drv = to_mcp_driver(dev->driver);
45 static struct bus_type mcp_bus_type = {
47 .match = mcp_bus_match,
48 .probe = mcp_bus_probe,
49 .remove = mcp_bus_remove,
53 * mcp_set_telecom_divisor - set the telecom divisor
54 * @mcp: MCP interface structure
55 * @div: SIB clock divisor
57 * Set the telecom divisor on the MCP interface. The resulting
58 * sample rate is SIBCLOCK/div.
60 void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div)
64 spin_lock_irqsave(&mcp->lock, flags);
65 mcp->ops->set_telecom_divisor(mcp, div);
66 spin_unlock_irqrestore(&mcp->lock, flags);
68 EXPORT_SYMBOL(mcp_set_telecom_divisor);
71 * mcp_set_audio_divisor - set the audio divisor
72 * @mcp: MCP interface structure
73 * @div: SIB clock divisor
75 * Set the audio divisor on the MCP interface.
77 void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div)
81 spin_lock_irqsave(&mcp->lock, flags);
82 mcp->ops->set_audio_divisor(mcp, div);
83 spin_unlock_irqrestore(&mcp->lock, flags);
85 EXPORT_SYMBOL(mcp_set_audio_divisor);
88 * mcp_reg_write - write a device register
89 * @mcp: MCP interface structure
90 * @reg: 4-bit register index
91 * @val: 16-bit data value
93 * Write a device register. The MCP interface must be enabled
94 * to prevent this function hanging.
96 void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val)
100 spin_lock_irqsave(&mcp->lock, flags);
101 mcp->ops->reg_write(mcp, reg, val);
102 spin_unlock_irqrestore(&mcp->lock, flags);
104 EXPORT_SYMBOL(mcp_reg_write);
107 * mcp_reg_read - read a device register
108 * @mcp: MCP interface structure
109 * @reg: 4-bit register index
111 * Read a device register and return its value. The MCP interface
112 * must be enabled to prevent this function hanging.
114 unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg)
119 spin_lock_irqsave(&mcp->lock, flags);
120 val = mcp->ops->reg_read(mcp, reg);
121 spin_unlock_irqrestore(&mcp->lock, flags);
125 EXPORT_SYMBOL(mcp_reg_read);
128 * mcp_enable - enable the MCP interface
129 * @mcp: MCP interface to enable
131 * Enable the MCP interface. Each call to mcp_enable will need
132 * a corresponding call to mcp_disable to disable the interface.
134 void mcp_enable(struct mcp *mcp)
138 spin_lock_irqsave(&mcp->lock, flags);
139 if (mcp->use_count++ == 0)
140 mcp->ops->enable(mcp);
141 spin_unlock_irqrestore(&mcp->lock, flags);
143 EXPORT_SYMBOL(mcp_enable);
146 * mcp_disable - disable the MCP interface
147 * @mcp: MCP interface to disable
149 * Disable the MCP interface. The MCP interface will only be
150 * disabled once the number of calls to mcp_enable matches the
151 * number of calls to mcp_disable.
153 void mcp_disable(struct mcp *mcp)
157 spin_lock_irqsave(&mcp->lock, flags);
158 if (--mcp->use_count == 0)
159 mcp->ops->disable(mcp);
160 spin_unlock_irqrestore(&mcp->lock, flags);
162 EXPORT_SYMBOL(mcp_disable);
164 static void mcp_release(struct device *dev)
166 struct mcp *mcp = container_of(dev, struct mcp, attached_device);
171 struct mcp *mcp_host_alloc(struct device *parent, size_t size)
175 mcp = kzalloc(sizeof(struct mcp) + size, GFP_KERNEL);
177 spin_lock_init(&mcp->lock);
178 device_initialize(&mcp->attached_device);
179 mcp->attached_device.parent = parent;
180 mcp->attached_device.bus = &mcp_bus_type;
181 mcp->attached_device.dma_mask = parent->dma_mask;
182 mcp->attached_device.release = mcp_release;
186 EXPORT_SYMBOL(mcp_host_alloc);
188 int mcp_host_add(struct mcp *mcp, void *pdata)
190 mcp->attached_device.platform_data = pdata;
191 dev_set_name(&mcp->attached_device, "mcp0");
192 return device_add(&mcp->attached_device);
194 EXPORT_SYMBOL(mcp_host_add);
196 void mcp_host_del(struct mcp *mcp)
198 device_del(&mcp->attached_device);
200 EXPORT_SYMBOL(mcp_host_del);
202 void mcp_host_free(struct mcp *mcp)
204 put_device(&mcp->attached_device);
206 EXPORT_SYMBOL(mcp_host_free);
208 int mcp_driver_register(struct mcp_driver *mcpdrv)
210 mcpdrv->drv.bus = &mcp_bus_type;
211 return driver_register(&mcpdrv->drv);
213 EXPORT_SYMBOL(mcp_driver_register);
215 void mcp_driver_unregister(struct mcp_driver *mcpdrv)
217 driver_unregister(&mcpdrv->drv);
219 EXPORT_SYMBOL(mcp_driver_unregister);
221 static int __init mcp_init(void)
223 return bus_register(&mcp_bus_type);
226 static void __exit mcp_exit(void)
228 bus_unregister(&mcp_bus_type);
231 module_init(mcp_init);
232 module_exit(mcp_exit);
234 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
235 MODULE_DESCRIPTION("Core multimedia communications port driver");
236 MODULE_LICENSE("GPL");