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
, const 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 void mcp_bus_remove(struct device
*dev
)
38 struct mcp
*mcp
= to_mcp(dev
);
39 struct mcp_driver
*drv
= to_mcp_driver(dev
->driver
);
44 static const struct bus_type mcp_bus_type
= {
46 .match
= mcp_bus_match
,
47 .probe
= mcp_bus_probe
,
48 .remove
= mcp_bus_remove
,
52 * mcp_set_telecom_divisor - set the telecom divisor
53 * @mcp: MCP interface structure
54 * @div: SIB clock divisor
56 * Set the telecom divisor on the MCP interface. The resulting
57 * sample rate is SIBCLOCK/div.
59 void mcp_set_telecom_divisor(struct mcp
*mcp
, unsigned int div
)
63 spin_lock_irqsave(&mcp
->lock
, flags
);
64 mcp
->ops
->set_telecom_divisor(mcp
, div
);
65 spin_unlock_irqrestore(&mcp
->lock
, flags
);
67 EXPORT_SYMBOL(mcp_set_telecom_divisor
);
70 * mcp_set_audio_divisor - set the audio divisor
71 * @mcp: MCP interface structure
72 * @div: SIB clock divisor
74 * Set the audio divisor on the MCP interface.
76 void mcp_set_audio_divisor(struct mcp
*mcp
, unsigned int div
)
80 spin_lock_irqsave(&mcp
->lock
, flags
);
81 mcp
->ops
->set_audio_divisor(mcp
, div
);
82 spin_unlock_irqrestore(&mcp
->lock
, flags
);
84 EXPORT_SYMBOL(mcp_set_audio_divisor
);
87 * mcp_reg_write - write a device register
88 * @mcp: MCP interface structure
89 * @reg: 4-bit register index
90 * @val: 16-bit data value
92 * Write a device register. The MCP interface must be enabled
93 * to prevent this function hanging.
95 void mcp_reg_write(struct mcp
*mcp
, unsigned int reg
, unsigned int val
)
99 spin_lock_irqsave(&mcp
->lock
, flags
);
100 mcp
->ops
->reg_write(mcp
, reg
, val
);
101 spin_unlock_irqrestore(&mcp
->lock
, flags
);
103 EXPORT_SYMBOL(mcp_reg_write
);
106 * mcp_reg_read - read a device register
107 * @mcp: MCP interface structure
108 * @reg: 4-bit register index
110 * Read a device register and return its value. The MCP interface
111 * must be enabled to prevent this function hanging.
113 unsigned int mcp_reg_read(struct mcp
*mcp
, unsigned int reg
)
118 spin_lock_irqsave(&mcp
->lock
, flags
);
119 val
= mcp
->ops
->reg_read(mcp
, reg
);
120 spin_unlock_irqrestore(&mcp
->lock
, flags
);
124 EXPORT_SYMBOL(mcp_reg_read
);
127 * mcp_enable - enable the MCP interface
128 * @mcp: MCP interface to enable
130 * Enable the MCP interface. Each call to mcp_enable will need
131 * a corresponding call to mcp_disable to disable the interface.
133 void mcp_enable(struct mcp
*mcp
)
137 spin_lock_irqsave(&mcp
->lock
, flags
);
138 if (mcp
->use_count
++ == 0)
139 mcp
->ops
->enable(mcp
);
140 spin_unlock_irqrestore(&mcp
->lock
, flags
);
142 EXPORT_SYMBOL(mcp_enable
);
145 * mcp_disable - disable the MCP interface
146 * @mcp: MCP interface to disable
148 * Disable the MCP interface. The MCP interface will only be
149 * disabled once the number of calls to mcp_enable matches the
150 * number of calls to mcp_disable.
152 void mcp_disable(struct mcp
*mcp
)
156 spin_lock_irqsave(&mcp
->lock
, flags
);
157 if (--mcp
->use_count
== 0)
158 mcp
->ops
->disable(mcp
);
159 spin_unlock_irqrestore(&mcp
->lock
, flags
);
161 EXPORT_SYMBOL(mcp_disable
);
163 static void mcp_release(struct device
*dev
)
165 struct mcp
*mcp
= container_of(dev
, struct mcp
, attached_device
);
170 struct mcp
*mcp_host_alloc(struct device
*parent
, size_t size
)
174 mcp
= kzalloc(sizeof(struct mcp
) + size
, GFP_KERNEL
);
176 spin_lock_init(&mcp
->lock
);
177 device_initialize(&mcp
->attached_device
);
178 mcp
->attached_device
.parent
= parent
;
179 mcp
->attached_device
.bus
= &mcp_bus_type
;
180 mcp
->attached_device
.dma_mask
= parent
->dma_mask
;
181 mcp
->attached_device
.release
= mcp_release
;
185 EXPORT_SYMBOL(mcp_host_alloc
);
187 int mcp_host_add(struct mcp
*mcp
, void *pdata
)
189 mcp
->attached_device
.platform_data
= pdata
;
190 dev_set_name(&mcp
->attached_device
, "mcp0");
191 return device_add(&mcp
->attached_device
);
193 EXPORT_SYMBOL(mcp_host_add
);
195 void mcp_host_del(struct mcp
*mcp
)
197 device_del(&mcp
->attached_device
);
199 EXPORT_SYMBOL(mcp_host_del
);
201 void mcp_host_free(struct mcp
*mcp
)
203 put_device(&mcp
->attached_device
);
205 EXPORT_SYMBOL(mcp_host_free
);
207 int mcp_driver_register(struct mcp_driver
*mcpdrv
)
209 mcpdrv
->drv
.bus
= &mcp_bus_type
;
210 return driver_register(&mcpdrv
->drv
);
212 EXPORT_SYMBOL(mcp_driver_register
);
214 void mcp_driver_unregister(struct mcp_driver
*mcpdrv
)
216 driver_unregister(&mcpdrv
->drv
);
218 EXPORT_SYMBOL(mcp_driver_unregister
);
220 static int __init
mcp_init(void)
222 return bus_register(&mcp_bus_type
);
225 static void __exit
mcp_exit(void)
227 bus_unregister(&mcp_bus_type
);
230 module_init(mcp_init
);
231 module_exit(mcp_exit
);
233 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
234 MODULE_DESCRIPTION("Core multimedia communications port driver");
235 MODULE_LICENSE("GPL");