184a14ae8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a4e137abSRussell King /*
3a4e137abSRussell King * linux/drivers/mfd/mcp-core.c
4a4e137abSRussell King *
5a4e137abSRussell King * Copyright (C) 2001 Russell King
6a4e137abSRussell King *
7a4e137abSRussell King * Generic MCP (Multimedia Communications Port) layer. All MCP locking
8a4e137abSRussell King * is solely held within this file.
9a4e137abSRussell King */
10a4e137abSRussell King #include <linux/module.h>
11a4e137abSRussell King #include <linux/init.h>
12a4e137abSRussell King #include <linux/errno.h>
13a4e137abSRussell King #include <linux/smp.h>
14a4e137abSRussell King #include <linux/device.h>
158c65b4a6STim Schmielau #include <linux/slab.h>
168c65b4a6STim Schmielau #include <linux/string.h>
17c8602edfSThomas Kunze #include <linux/mfd/mcp.h>
18a4e137abSRussell King
19a4e137abSRussell King
20a4e137abSRussell King #define to_mcp(d) container_of(d, struct mcp, attached_device)
21a4e137abSRussell King #define to_mcp_driver(d) container_of(d, struct mcp_driver, drv)
22a4e137abSRussell King
mcp_bus_match(struct device * dev,const struct device_driver * drv)23*d69d8048SGreg Kroah-Hartman static int mcp_bus_match(struct device *dev, const struct device_driver *drv)
24a4e137abSRussell King {
2565f2e753SRussell King return 1;
26a4e137abSRussell King }
27a4e137abSRussell King
mcp_bus_probe(struct device * dev)28a4e137abSRussell King static int mcp_bus_probe(struct device *dev)
29a4e137abSRussell King {
30a4e137abSRussell King struct mcp *mcp = to_mcp(dev);
31a4e137abSRussell King struct mcp_driver *drv = to_mcp_driver(dev->driver);
32a4e137abSRussell King
33a4e137abSRussell King return drv->probe(mcp);
34a4e137abSRussell King }
35a4e137abSRussell King
mcp_bus_remove(struct device * dev)36fc7a6209SUwe Kleine-König static void mcp_bus_remove(struct device *dev)
37a4e137abSRussell King {
38a4e137abSRussell King struct mcp *mcp = to_mcp(dev);
39a4e137abSRussell King struct mcp_driver *drv = to_mcp_driver(dev->driver);
40a4e137abSRussell King
41a4e137abSRussell King drv->remove(mcp);
42a4e137abSRussell King }
43a4e137abSRussell King
446abc896cSRicardo B. Marliere static const struct bus_type mcp_bus_type = {
45a4e137abSRussell King .name = "mcp",
46a4e137abSRussell King .match = mcp_bus_match,
47413b486eSRussell King .probe = mcp_bus_probe,
48413b486eSRussell King .remove = mcp_bus_remove,
49a4e137abSRussell King };
50a4e137abSRussell King
51a4e137abSRussell King /**
52a4e137abSRussell King * mcp_set_telecom_divisor - set the telecom divisor
53a4e137abSRussell King * @mcp: MCP interface structure
54a4e137abSRussell King * @div: SIB clock divisor
55a4e137abSRussell King *
56a4e137abSRussell King * Set the telecom divisor on the MCP interface. The resulting
57a4e137abSRussell King * sample rate is SIBCLOCK/div.
58a4e137abSRussell King */
mcp_set_telecom_divisor(struct mcp * mcp,unsigned int div)59a4e137abSRussell King void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div)
60a4e137abSRussell King {
6198250221SRussell King unsigned long flags;
6298250221SRussell King
6398250221SRussell King spin_lock_irqsave(&mcp->lock, flags);
64a4e137abSRussell King mcp->ops->set_telecom_divisor(mcp, div);
6598250221SRussell King spin_unlock_irqrestore(&mcp->lock, flags);
66a4e137abSRussell King }
67a4e137abSRussell King EXPORT_SYMBOL(mcp_set_telecom_divisor);
68a4e137abSRussell King
69a4e137abSRussell King /**
70a4e137abSRussell King * mcp_set_audio_divisor - set the audio divisor
71a4e137abSRussell King * @mcp: MCP interface structure
72a4e137abSRussell King * @div: SIB clock divisor
73a4e137abSRussell King *
74a4e137abSRussell King * Set the audio divisor on the MCP interface.
75a4e137abSRussell King */
mcp_set_audio_divisor(struct mcp * mcp,unsigned int div)76a4e137abSRussell King void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div)
77a4e137abSRussell King {
7898250221SRussell King unsigned long flags;
7998250221SRussell King
8098250221SRussell King spin_lock_irqsave(&mcp->lock, flags);
81a4e137abSRussell King mcp->ops->set_audio_divisor(mcp, div);
8298250221SRussell King spin_unlock_irqrestore(&mcp->lock, flags);
83a4e137abSRussell King }
84a4e137abSRussell King EXPORT_SYMBOL(mcp_set_audio_divisor);
85a4e137abSRussell King
86a4e137abSRussell King /**
87a4e137abSRussell King * mcp_reg_write - write a device register
88a4e137abSRussell King * @mcp: MCP interface structure
89a4e137abSRussell King * @reg: 4-bit register index
90a4e137abSRussell King * @val: 16-bit data value
91a4e137abSRussell King *
92a4e137abSRussell King * Write a device register. The MCP interface must be enabled
93a4e137abSRussell King * to prevent this function hanging.
94a4e137abSRussell King */
mcp_reg_write(struct mcp * mcp,unsigned int reg,unsigned int val)95a4e137abSRussell King void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val)
96a4e137abSRussell King {
97a4e137abSRussell King unsigned long flags;
98a4e137abSRussell King
99a4e137abSRussell King spin_lock_irqsave(&mcp->lock, flags);
100a4e137abSRussell King mcp->ops->reg_write(mcp, reg, val);
101a4e137abSRussell King spin_unlock_irqrestore(&mcp->lock, flags);
102a4e137abSRussell King }
103a4e137abSRussell King EXPORT_SYMBOL(mcp_reg_write);
104a4e137abSRussell King
105a4e137abSRussell King /**
106a4e137abSRussell King * mcp_reg_read - read a device register
107a4e137abSRussell King * @mcp: MCP interface structure
108a4e137abSRussell King * @reg: 4-bit register index
109a4e137abSRussell King *
110a4e137abSRussell King * Read a device register and return its value. The MCP interface
111a4e137abSRussell King * must be enabled to prevent this function hanging.
112a4e137abSRussell King */
mcp_reg_read(struct mcp * mcp,unsigned int reg)113a4e137abSRussell King unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg)
114a4e137abSRussell King {
115a4e137abSRussell King unsigned long flags;
116a4e137abSRussell King unsigned int val;
117a4e137abSRussell King
118a4e137abSRussell King spin_lock_irqsave(&mcp->lock, flags);
119a4e137abSRussell King val = mcp->ops->reg_read(mcp, reg);
120a4e137abSRussell King spin_unlock_irqrestore(&mcp->lock, flags);
121a4e137abSRussell King
122a4e137abSRussell King return val;
123a4e137abSRussell King }
124a4e137abSRussell King EXPORT_SYMBOL(mcp_reg_read);
125a4e137abSRussell King
126a4e137abSRussell King /**
127a4e137abSRussell King * mcp_enable - enable the MCP interface
128a4e137abSRussell King * @mcp: MCP interface to enable
129a4e137abSRussell King *
130a4e137abSRussell King * Enable the MCP interface. Each call to mcp_enable will need
131a4e137abSRussell King * a corresponding call to mcp_disable to disable the interface.
132a4e137abSRussell King */
mcp_enable(struct mcp * mcp)133a4e137abSRussell King void mcp_enable(struct mcp *mcp)
134a4e137abSRussell King {
13598250221SRussell King unsigned long flags;
136b9ac2270SLee Jones
13798250221SRussell King spin_lock_irqsave(&mcp->lock, flags);
138a4e137abSRussell King if (mcp->use_count++ == 0)
139a4e137abSRussell King mcp->ops->enable(mcp);
14098250221SRussell King spin_unlock_irqrestore(&mcp->lock, flags);
141a4e137abSRussell King }
142a4e137abSRussell King EXPORT_SYMBOL(mcp_enable);
143a4e137abSRussell King
144a4e137abSRussell King /**
145a4e137abSRussell King * mcp_disable - disable the MCP interface
146a4e137abSRussell King * @mcp: MCP interface to disable
147a4e137abSRussell King *
148a4e137abSRussell King * Disable the MCP interface. The MCP interface will only be
149a4e137abSRussell King * disabled once the number of calls to mcp_enable matches the
150a4e137abSRussell King * number of calls to mcp_disable.
151a4e137abSRussell King */
mcp_disable(struct mcp * mcp)152a4e137abSRussell King void mcp_disable(struct mcp *mcp)
153a4e137abSRussell King {
154a4e137abSRussell King unsigned long flags;
155a4e137abSRussell King
156a4e137abSRussell King spin_lock_irqsave(&mcp->lock, flags);
157a4e137abSRussell King if (--mcp->use_count == 0)
158a4e137abSRussell King mcp->ops->disable(mcp);
159a4e137abSRussell King spin_unlock_irqrestore(&mcp->lock, flags);
160a4e137abSRussell King }
161a4e137abSRussell King EXPORT_SYMBOL(mcp_disable);
162a4e137abSRussell King
mcp_release(struct device * dev)163a4e137abSRussell King static void mcp_release(struct device *dev)
164a4e137abSRussell King {
165a4e137abSRussell King struct mcp *mcp = container_of(dev, struct mcp, attached_device);
166a4e137abSRussell King
167a4e137abSRussell King kfree(mcp);
168a4e137abSRussell King }
169a4e137abSRussell King
mcp_host_alloc(struct device * parent,size_t size)170a4e137abSRussell King struct mcp *mcp_host_alloc(struct device *parent, size_t size)
171a4e137abSRussell King {
172a4e137abSRussell King struct mcp *mcp;
173a4e137abSRussell King
174dd00cc48SYoann Padioleau mcp = kzalloc(sizeof(struct mcp) + size, GFP_KERNEL);
175a4e137abSRussell King if (mcp) {
176a4e137abSRussell King spin_lock_init(&mcp->lock);
17730816ac0SRussell King device_initialize(&mcp->attached_device);
178a4e137abSRussell King mcp->attached_device.parent = parent;
179a4e137abSRussell King mcp->attached_device.bus = &mcp_bus_type;
180a4e137abSRussell King mcp->attached_device.dma_mask = parent->dma_mask;
181a4e137abSRussell King mcp->attached_device.release = mcp_release;
182a4e137abSRussell King }
183a4e137abSRussell King return mcp;
184a4e137abSRussell King }
185a4e137abSRussell King EXPORT_SYMBOL(mcp_host_alloc);
186a4e137abSRussell King
mcp_host_add(struct mcp * mcp,void * pdata)187abe06082SRussell King int mcp_host_add(struct mcp *mcp, void *pdata)
188a4e137abSRussell King {
189abe06082SRussell King mcp->attached_device.platform_data = pdata;
190b2bf61f2SKay Sievers dev_set_name(&mcp->attached_device, "mcp0");
19130816ac0SRussell King return device_add(&mcp->attached_device);
192a4e137abSRussell King }
19330816ac0SRussell King EXPORT_SYMBOL(mcp_host_add);
194a4e137abSRussell King
mcp_host_del(struct mcp * mcp)19530816ac0SRussell King void mcp_host_del(struct mcp *mcp)
196a4e137abSRussell King {
19730816ac0SRussell King device_del(&mcp->attached_device);
198a4e137abSRussell King }
19930816ac0SRussell King EXPORT_SYMBOL(mcp_host_del);
20030816ac0SRussell King
mcp_host_free(struct mcp * mcp)20130816ac0SRussell King void mcp_host_free(struct mcp *mcp)
20230816ac0SRussell King {
20330816ac0SRussell King put_device(&mcp->attached_device);
20430816ac0SRussell King }
20530816ac0SRussell King EXPORT_SYMBOL(mcp_host_free);
206a4e137abSRussell King
mcp_driver_register(struct mcp_driver * mcpdrv)207a4e137abSRussell King int mcp_driver_register(struct mcp_driver *mcpdrv)
208a4e137abSRussell King {
209a4e137abSRussell King mcpdrv->drv.bus = &mcp_bus_type;
210a4e137abSRussell King return driver_register(&mcpdrv->drv);
211a4e137abSRussell King }
212a4e137abSRussell King EXPORT_SYMBOL(mcp_driver_register);
213a4e137abSRussell King
mcp_driver_unregister(struct mcp_driver * mcpdrv)214a4e137abSRussell King void mcp_driver_unregister(struct mcp_driver *mcpdrv)
215a4e137abSRussell King {
216a4e137abSRussell King driver_unregister(&mcpdrv->drv);
217a4e137abSRussell King }
218a4e137abSRussell King EXPORT_SYMBOL(mcp_driver_unregister);
219a4e137abSRussell King
mcp_init(void)220a4e137abSRussell King static int __init mcp_init(void)
221a4e137abSRussell King {
222a4e137abSRussell King return bus_register(&mcp_bus_type);
223a4e137abSRussell King }
224a4e137abSRussell King
mcp_exit(void)225a4e137abSRussell King static void __exit mcp_exit(void)
226a4e137abSRussell King {
227a4e137abSRussell King bus_unregister(&mcp_bus_type);
228a4e137abSRussell King }
229a4e137abSRussell King
230a4e137abSRussell King module_init(mcp_init);
231a4e137abSRussell King module_exit(mcp_exit);
232a4e137abSRussell King
233a4e137abSRussell King MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
234a4e137abSRussell King MODULE_DESCRIPTION("Core multimedia communications port driver");
235a4e137abSRussell King MODULE_LICENSE("GPL");
236