xref: /openbmc/linux/sound/hda/ext/hdac_ext_bus.c (revision 4446085d21e75dd6c0c45577f12db0bd7c7bf35f)
1dfe66a18SJeeja KP /*
2dfe66a18SJeeja KP  *  hdac-ext-bus.c - HD-audio extended core bus functions.
3dfe66a18SJeeja KP  *
4dfe66a18SJeeja KP  *  Copyright (C) 2014-2015 Intel Corp
5dfe66a18SJeeja KP  *  Author: Jeeja KP <jeeja.kp@intel.com>
6dfe66a18SJeeja KP  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7dfe66a18SJeeja KP  *
8dfe66a18SJeeja KP  *  This program is free software; you can redistribute it and/or modify
9dfe66a18SJeeja KP  *  it under the terms of the GNU General Public License as published by
10dfe66a18SJeeja KP  *  the Free Software Foundation; version 2 of the License.
11dfe66a18SJeeja KP  *
12dfe66a18SJeeja KP  *  This program is distributed in the hope that it will be useful, but
13dfe66a18SJeeja KP  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14dfe66a18SJeeja KP  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15dfe66a18SJeeja KP  *  General Public License for more details.
16dfe66a18SJeeja KP  *
17dfe66a18SJeeja KP  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18dfe66a18SJeeja KP  */
19dfe66a18SJeeja KP 
20dfe66a18SJeeja KP #include <linux/module.h>
21dfe66a18SJeeja KP #include <linux/slab.h>
2242f2bb1cSVinod Koul #include <linux/io.h>
23dfe66a18SJeeja KP #include <sound/hdaudio_ext.h>
24dfe66a18SJeeja KP 
25dfe66a18SJeeja KP MODULE_DESCRIPTION("HDA extended core");
26dfe66a18SJeeja KP MODULE_LICENSE("GPL v2");
27dfe66a18SJeeja KP 
2899463b3aSVinod Koul static void hdac_ext_writel(u32 value, u32 __iomem *addr)
2999463b3aSVinod Koul {
3099463b3aSVinod Koul 	writel(value, addr);
3199463b3aSVinod Koul }
3299463b3aSVinod Koul 
3399463b3aSVinod Koul static u32 hdac_ext_readl(u32 __iomem *addr)
3499463b3aSVinod Koul {
3599463b3aSVinod Koul 	return readl(addr);
3699463b3aSVinod Koul }
3799463b3aSVinod Koul 
3899463b3aSVinod Koul static void hdac_ext_writew(u16 value, u16 __iomem *addr)
3999463b3aSVinod Koul {
4099463b3aSVinod Koul 	writew(value, addr);
4199463b3aSVinod Koul }
4299463b3aSVinod Koul 
4399463b3aSVinod Koul static u16 hdac_ext_readw(u16 __iomem *addr)
4499463b3aSVinod Koul {
4599463b3aSVinod Koul 	return readw(addr);
4699463b3aSVinod Koul }
4799463b3aSVinod Koul 
4899463b3aSVinod Koul static void hdac_ext_writeb(u8 value, u8 __iomem *addr)
4999463b3aSVinod Koul {
5099463b3aSVinod Koul 	writeb(value, addr);
5199463b3aSVinod Koul }
5299463b3aSVinod Koul 
5399463b3aSVinod Koul static u8 hdac_ext_readb(u8 __iomem *addr)
5499463b3aSVinod Koul {
5599463b3aSVinod Koul 	return readb(addr);
5699463b3aSVinod Koul }
5799463b3aSVinod Koul 
5899463b3aSVinod Koul static int hdac_ext_dma_alloc_pages(struct hdac_bus *bus, int type,
5999463b3aSVinod Koul 			   size_t size, struct snd_dma_buffer *buf)
6099463b3aSVinod Koul {
6199463b3aSVinod Koul 	return snd_dma_alloc_pages(type, bus->dev, size, buf);
6299463b3aSVinod Koul }
6399463b3aSVinod Koul 
6499463b3aSVinod Koul static void hdac_ext_dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
6599463b3aSVinod Koul {
6699463b3aSVinod Koul 	snd_dma_free_pages(buf);
6799463b3aSVinod Koul }
6899463b3aSVinod Koul 
6999463b3aSVinod Koul static const struct hdac_io_ops hdac_ext_default_io = {
7099463b3aSVinod Koul 	.reg_writel = hdac_ext_writel,
7199463b3aSVinod Koul 	.reg_readl = hdac_ext_readl,
7299463b3aSVinod Koul 	.reg_writew = hdac_ext_writew,
7399463b3aSVinod Koul 	.reg_readw = hdac_ext_readw,
7499463b3aSVinod Koul 	.reg_writeb = hdac_ext_writeb,
7599463b3aSVinod Koul 	.reg_readb = hdac_ext_readb,
7699463b3aSVinod Koul 	.dma_alloc_pages = hdac_ext_dma_alloc_pages,
7799463b3aSVinod Koul 	.dma_free_pages = hdac_ext_dma_free_pages,
7899463b3aSVinod Koul };
7999463b3aSVinod Koul 
80dfe66a18SJeeja KP /**
81dfe66a18SJeeja KP  * snd_hdac_ext_bus_init - initialize a HD-audio extended bus
82dfe66a18SJeeja KP  * @ebus: the pointer to extended bus object
83dfe66a18SJeeja KP  * @dev: device pointer
84dfe66a18SJeeja KP  * @ops: bus verb operators
8599463b3aSVinod Koul  * @io_ops: lowlevel I/O operators, can be NULL. If NULL core will use
8699463b3aSVinod Koul  * default ops
87dfe66a18SJeeja KP  *
88dfe66a18SJeeja KP  * Returns 0 if successful, or a negative error code.
89dfe66a18SJeeja KP  */
90dfe66a18SJeeja KP int snd_hdac_ext_bus_init(struct hdac_ext_bus *ebus, struct device *dev,
91dfe66a18SJeeja KP 			const struct hdac_bus_ops *ops,
92dfe66a18SJeeja KP 			const struct hdac_io_ops *io_ops)
93dfe66a18SJeeja KP {
94dfe66a18SJeeja KP 	int ret;
95dfe66a18SJeeja KP 	static int idx;
96dfe66a18SJeeja KP 
9799463b3aSVinod Koul 	/* check if io ops are provided, if not load the defaults */
9899463b3aSVinod Koul 	if (io_ops == NULL)
9999463b3aSVinod Koul 		io_ops = &hdac_ext_default_io;
10099463b3aSVinod Koul 
101dfe66a18SJeeja KP 	ret = snd_hdac_bus_init(&ebus->bus, dev, ops, io_ops);
102dfe66a18SJeeja KP 	if (ret < 0)
103dfe66a18SJeeja KP 		return ret;
104dfe66a18SJeeja KP 
105dfe66a18SJeeja KP 	INIT_LIST_HEAD(&ebus->hlink_list);
106dfe66a18SJeeja KP 	ebus->idx = idx++;
107dfe66a18SJeeja KP 
108*4446085dSVinod Koul 	mutex_init(&ebus->lock);
109*4446085dSVinod Koul 	ebus->cmd_dma_state = true;
110*4446085dSVinod Koul 
111dfe66a18SJeeja KP 	return 0;
112dfe66a18SJeeja KP }
113dfe66a18SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init);
114dfe66a18SJeeja KP 
115dfe66a18SJeeja KP /**
116dfe66a18SJeeja KP  * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus
117dfe66a18SJeeja KP  * @ebus: the pointer to extended bus object
118dfe66a18SJeeja KP  */
119dfe66a18SJeeja KP void snd_hdac_ext_bus_exit(struct hdac_ext_bus *ebus)
120dfe66a18SJeeja KP {
121dfe66a18SJeeja KP 	snd_hdac_bus_exit(&ebus->bus);
122dfe66a18SJeeja KP 	WARN_ON(!list_empty(&ebus->hlink_list));
123dfe66a18SJeeja KP }
124dfe66a18SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit);
125dfe66a18SJeeja KP 
126dfe66a18SJeeja KP static void default_release(struct device *dev)
127dfe66a18SJeeja KP {
128dfe66a18SJeeja KP 	snd_hdac_ext_bus_device_exit(container_of(dev, struct hdac_device, dev));
129dfe66a18SJeeja KP }
130dfe66a18SJeeja KP 
131dfe66a18SJeeja KP /**
132a512f561SVinod Koul  * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device
133dfe66a18SJeeja KP  * @ebus: hdac extended bus to attach to
134dfe66a18SJeeja KP  * @addr: codec address
135dfe66a18SJeeja KP  *
136dfe66a18SJeeja KP  * Returns zero for success or a negative error code.
137dfe66a18SJeeja KP  */
138dfe66a18SJeeja KP int snd_hdac_ext_bus_device_init(struct hdac_ext_bus *ebus, int addr)
139dfe66a18SJeeja KP {
140a512f561SVinod Koul 	struct hdac_ext_device *edev;
141dfe66a18SJeeja KP 	struct hdac_device *hdev = NULL;
142dfe66a18SJeeja KP 	struct hdac_bus *bus = ebus_to_hbus(ebus);
143dfe66a18SJeeja KP 	char name[15];
144dfe66a18SJeeja KP 	int ret;
145dfe66a18SJeeja KP 
146e57690b1SVinod Koul 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
147a512f561SVinod Koul 	if (!edev)
148dfe66a18SJeeja KP 		return -ENOMEM;
149a512f561SVinod Koul 	hdev = &edev->hdac;
150dfe66a18SJeeja KP 
151dfe66a18SJeeja KP 	snprintf(name, sizeof(name), "ehdaudio%dD%d", ebus->idx, addr);
152dfe66a18SJeeja KP 
153dfe66a18SJeeja KP 	ret  = snd_hdac_device_init(hdev, bus, name, addr);
154dfe66a18SJeeja KP 	if (ret < 0) {
155dfe66a18SJeeja KP 		dev_err(bus->dev, "device init failed for hdac device\n");
156dfe66a18SJeeja KP 		return ret;
157dfe66a18SJeeja KP 	}
158dfe66a18SJeeja KP 	hdev->type = HDA_DEV_ASOC;
159dfe66a18SJeeja KP 	hdev->dev.release = default_release;
160dfe66a18SJeeja KP 
161dfe66a18SJeeja KP 	ret = snd_hdac_device_register(hdev);
162dfe66a18SJeeja KP 	if (ret) {
163dfe66a18SJeeja KP 		dev_err(bus->dev, "failed to register hdac device\n");
164dfe66a18SJeeja KP 		snd_hdac_ext_bus_device_exit(hdev);
165dfe66a18SJeeja KP 		return ret;
166dfe66a18SJeeja KP 	}
167a512f561SVinod Koul 
168dfe66a18SJeeja KP 	return 0;
169dfe66a18SJeeja KP }
170dfe66a18SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init);
171dfe66a18SJeeja KP 
172dfe66a18SJeeja KP /**
173dfe66a18SJeeja KP  * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device
174dfe66a18SJeeja KP  * @hdev: hdac device to clean up
175dfe66a18SJeeja KP  */
176dfe66a18SJeeja KP void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev)
177dfe66a18SJeeja KP {
178a512f561SVinod Koul 	struct hdac_ext_device *edev = to_ehdac_device(hdev);
179a512f561SVinod Koul 
180dfe66a18SJeeja KP 	snd_hdac_device_exit(hdev);
181a512f561SVinod Koul 	kfree(edev);
182dfe66a18SJeeja KP }
183dfe66a18SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit);
184ee2d51b3SVinod Koul 
185ee2d51b3SVinod Koul /**
186ee2d51b3SVinod Koul  * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices
187ee2d51b3SVinod Koul  *
188ee2d51b3SVinod Koul  * @ebus: HD-audio extended bus
189ee2d51b3SVinod Koul  */
190ee2d51b3SVinod Koul void snd_hdac_ext_bus_device_remove(struct hdac_ext_bus *ebus)
191ee2d51b3SVinod Koul {
192ee2d51b3SVinod Koul 	struct hdac_device *codec, *__codec;
193ee2d51b3SVinod Koul 	/*
194ee2d51b3SVinod Koul 	 * we need to remove all the codec devices objects created in the
195ee2d51b3SVinod Koul 	 * snd_hdac_ext_bus_device_init
196ee2d51b3SVinod Koul 	 */
197ee2d51b3SVinod Koul 	list_for_each_entry_safe(codec, __codec, &ebus->bus.codec_list, list) {
198ee2d51b3SVinod Koul 		snd_hdac_device_unregister(codec);
199ee2d51b3SVinod Koul 		put_device(&codec->dev);
200ee2d51b3SVinod Koul 	}
201ee2d51b3SVinod Koul }
202ee2d51b3SVinod Koul EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove);
203d51783c1SVinod Koul #define dev_to_hdac(dev) (container_of((dev), \
204d51783c1SVinod Koul 			struct hdac_device, dev))
205d51783c1SVinod Koul 
206d51783c1SVinod Koul static inline struct hdac_ext_driver *get_edrv(struct device *dev)
207d51783c1SVinod Koul {
208d51783c1SVinod Koul 	struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver);
209d51783c1SVinod Koul 	struct hdac_ext_driver *edrv = to_ehdac_driver(hdrv);
210d51783c1SVinod Koul 
211d51783c1SVinod Koul 	return edrv;
212d51783c1SVinod Koul }
213d51783c1SVinod Koul 
214d51783c1SVinod Koul static inline struct hdac_ext_device *get_edev(struct device *dev)
215d51783c1SVinod Koul {
216d51783c1SVinod Koul 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
217d51783c1SVinod Koul 	struct hdac_ext_device *edev = to_ehdac_device(hdev);
218d51783c1SVinod Koul 
219d51783c1SVinod Koul 	return edev;
220d51783c1SVinod Koul }
221d51783c1SVinod Koul 
222d51783c1SVinod Koul static int hda_ext_drv_probe(struct device *dev)
223d51783c1SVinod Koul {
224d51783c1SVinod Koul 	return (get_edrv(dev))->probe(get_edev(dev));
225d51783c1SVinod Koul }
226d51783c1SVinod Koul 
227d51783c1SVinod Koul static int hdac_ext_drv_remove(struct device *dev)
228d51783c1SVinod Koul {
229d51783c1SVinod Koul 	return (get_edrv(dev))->remove(get_edev(dev));
230d51783c1SVinod Koul }
231d51783c1SVinod Koul 
232d51783c1SVinod Koul static void hdac_ext_drv_shutdown(struct device *dev)
233d51783c1SVinod Koul {
234d51783c1SVinod Koul 	return (get_edrv(dev))->shutdown(get_edev(dev));
235d51783c1SVinod Koul }
236d51783c1SVinod Koul 
237d51783c1SVinod Koul /**
238d51783c1SVinod Koul  * snd_hda_ext_driver_register - register a driver for ext hda devices
239d51783c1SVinod Koul  *
240d51783c1SVinod Koul  * @drv: ext hda driver structure
241d51783c1SVinod Koul  */
242d51783c1SVinod Koul int snd_hda_ext_driver_register(struct hdac_ext_driver *drv)
243d51783c1SVinod Koul {
244d51783c1SVinod Koul 	drv->hdac.type = HDA_DEV_ASOC;
245d51783c1SVinod Koul 	drv->hdac.driver.bus = &snd_hda_bus_type;
246d51783c1SVinod Koul 	/* we use default match */
247d51783c1SVinod Koul 
248d51783c1SVinod Koul 	if (drv->probe)
249d51783c1SVinod Koul 		drv->hdac.driver.probe = hda_ext_drv_probe;
250d51783c1SVinod Koul 	if (drv->remove)
251d51783c1SVinod Koul 		drv->hdac.driver.remove = hdac_ext_drv_remove;
252d51783c1SVinod Koul 	if (drv->shutdown)
253d51783c1SVinod Koul 		drv->hdac.driver.shutdown = hdac_ext_drv_shutdown;
254d51783c1SVinod Koul 
255d51783c1SVinod Koul 	return driver_register(&drv->hdac.driver);
256d51783c1SVinod Koul }
257d51783c1SVinod Koul EXPORT_SYMBOL_GPL(snd_hda_ext_driver_register);
258d51783c1SVinod Koul 
259d51783c1SVinod Koul /**
260d51783c1SVinod Koul  * snd_hda_ext_driver_unregister - unregister a driver for ext hda devices
261d51783c1SVinod Koul  *
262d51783c1SVinod Koul  * @drv: ext hda driver structure
263d51783c1SVinod Koul  */
264d51783c1SVinod Koul void snd_hda_ext_driver_unregister(struct hdac_ext_driver *drv)
265d51783c1SVinod Koul {
266d51783c1SVinod Koul 	driver_unregister(&drv->hdac.driver);
267d51783c1SVinod Koul }
268d51783c1SVinod Koul EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister);
269