xref: /openbmc/linux/sound/hda/ext/hdac_ext_bus.c (revision 2169e6da)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  hdac-ext-bus.c - HD-audio extended core bus functions.
4  *
5  *  Copyright (C) 2014-2015 Intel Corp
6  *  Author: Jeeja KP <jeeja.kp@intel.com>
7  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  */
11 
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/io.h>
15 #include <sound/hdaudio_ext.h>
16 
17 MODULE_DESCRIPTION("HDA extended core");
18 MODULE_LICENSE("GPL v2");
19 
20 static void hdac_ext_writel(u32 value, u32 __iomem *addr)
21 {
22 	writel(value, addr);
23 }
24 
25 static u32 hdac_ext_readl(u32 __iomem *addr)
26 {
27 	return readl(addr);
28 }
29 
30 static void hdac_ext_writew(u16 value, u16 __iomem *addr)
31 {
32 	writew(value, addr);
33 }
34 
35 static u16 hdac_ext_readw(u16 __iomem *addr)
36 {
37 	return readw(addr);
38 }
39 
40 static void hdac_ext_writeb(u8 value, u8 __iomem *addr)
41 {
42 	writeb(value, addr);
43 }
44 
45 static u8 hdac_ext_readb(u8 __iomem *addr)
46 {
47 	return readb(addr);
48 }
49 
50 static int hdac_ext_dma_alloc_pages(struct hdac_bus *bus, int type,
51 			   size_t size, struct snd_dma_buffer *buf)
52 {
53 	return snd_dma_alloc_pages(type, bus->dev, size, buf);
54 }
55 
56 static void hdac_ext_dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
57 {
58 	snd_dma_free_pages(buf);
59 }
60 
61 static const struct hdac_io_ops hdac_ext_default_io = {
62 	.reg_writel = hdac_ext_writel,
63 	.reg_readl = hdac_ext_readl,
64 	.reg_writew = hdac_ext_writew,
65 	.reg_readw = hdac_ext_readw,
66 	.reg_writeb = hdac_ext_writeb,
67 	.reg_readb = hdac_ext_readb,
68 	.dma_alloc_pages = hdac_ext_dma_alloc_pages,
69 	.dma_free_pages = hdac_ext_dma_free_pages,
70 };
71 
72 /**
73  * snd_hdac_ext_bus_init - initialize a HD-audio extended bus
74  * @ebus: the pointer to extended bus object
75  * @dev: device pointer
76  * @ops: bus verb operators
77  * @io_ops: lowlevel I/O operators, can be NULL. If NULL core will use
78  * default ops
79  *
80  * Returns 0 if successful, or a negative error code.
81  */
82 int snd_hdac_ext_bus_init(struct hdac_bus *bus, struct device *dev,
83 			const struct hdac_bus_ops *ops,
84 			const struct hdac_io_ops *io_ops,
85 			const struct hdac_ext_bus_ops *ext_ops)
86 {
87 	int ret;
88 
89 	/* check if io ops are provided, if not load the defaults */
90 	if (io_ops == NULL)
91 		io_ops = &hdac_ext_default_io;
92 
93 	ret = snd_hdac_bus_init(bus, dev, ops, io_ops);
94 	if (ret < 0)
95 		return ret;
96 
97 	bus->ext_ops = ext_ops;
98 	/* FIXME:
99 	 * Currently only one bus is supported, if there is device with more
100 	 * buses, bus->idx should be greater than 0, but there needs to be a
101 	 * reliable way to always assign same number.
102 	 */
103 	bus->idx = 0;
104 	bus->cmd_dma_state = true;
105 
106 	return 0;
107 }
108 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init);
109 
110 /**
111  * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus
112  * @ebus: the pointer to extended bus object
113  */
114 void snd_hdac_ext_bus_exit(struct hdac_bus *bus)
115 {
116 	snd_hdac_bus_exit(bus);
117 	WARN_ON(!list_empty(&bus->hlink_list));
118 }
119 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit);
120 
121 static void default_release(struct device *dev)
122 {
123 	snd_hdac_ext_bus_device_exit(container_of(dev, struct hdac_device, dev));
124 }
125 
126 /**
127  * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device
128  * @ebus: hdac extended bus to attach to
129  * @addr: codec address
130  *
131  * Returns zero for success or a negative error code.
132  */
133 int snd_hdac_ext_bus_device_init(struct hdac_bus *bus, int addr,
134 					struct hdac_device *hdev)
135 {
136 	char name[15];
137 	int ret;
138 
139 	hdev->bus = bus;
140 
141 	snprintf(name, sizeof(name), "ehdaudio%dD%d", bus->idx, addr);
142 
143 	ret  = snd_hdac_device_init(hdev, bus, name, addr);
144 	if (ret < 0) {
145 		dev_err(bus->dev, "device init failed for hdac device\n");
146 		return ret;
147 	}
148 	hdev->type = HDA_DEV_ASOC;
149 	hdev->dev.release = default_release;
150 
151 	ret = snd_hdac_device_register(hdev);
152 	if (ret) {
153 		dev_err(bus->dev, "failed to register hdac device\n");
154 		snd_hdac_ext_bus_device_exit(hdev);
155 		return ret;
156 	}
157 
158 	return 0;
159 }
160 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init);
161 
162 /**
163  * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device
164  * @hdev: hdac device to clean up
165  */
166 void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev)
167 {
168 	snd_hdac_device_exit(hdev);
169 }
170 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit);
171 
172 /**
173  * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices
174  *
175  * @ebus: HD-audio extended bus
176  */
177 void snd_hdac_ext_bus_device_remove(struct hdac_bus *bus)
178 {
179 	struct hdac_device *codec, *__codec;
180 	/*
181 	 * we need to remove all the codec devices objects created in the
182 	 * snd_hdac_ext_bus_device_init
183 	 */
184 	list_for_each_entry_safe(codec, __codec, &bus->codec_list, list) {
185 		snd_hdac_device_unregister(codec);
186 		put_device(&codec->dev);
187 	}
188 }
189 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove);
190 #define dev_to_hdac(dev) (container_of((dev), \
191 			struct hdac_device, dev))
192 
193 static inline struct hdac_driver *get_hdrv(struct device *dev)
194 {
195 	struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver);
196 	return hdrv;
197 }
198 
199 static inline struct hdac_device *get_hdev(struct device *dev)
200 {
201 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
202 	return hdev;
203 }
204 
205 static int hda_ext_drv_probe(struct device *dev)
206 {
207 	return (get_hdrv(dev))->probe(get_hdev(dev));
208 }
209 
210 static int hdac_ext_drv_remove(struct device *dev)
211 {
212 	return (get_hdrv(dev))->remove(get_hdev(dev));
213 }
214 
215 static void hdac_ext_drv_shutdown(struct device *dev)
216 {
217 	return (get_hdrv(dev))->shutdown(get_hdev(dev));
218 }
219 
220 /**
221  * snd_hda_ext_driver_register - register a driver for ext hda devices
222  *
223  * @drv: ext hda driver structure
224  */
225 int snd_hda_ext_driver_register(struct hdac_driver *drv)
226 {
227 	drv->type = HDA_DEV_ASOC;
228 	drv->driver.bus = &snd_hda_bus_type;
229 	/* we use default match */
230 
231 	if (drv->probe)
232 		drv->driver.probe = hda_ext_drv_probe;
233 	if (drv->remove)
234 		drv->driver.remove = hdac_ext_drv_remove;
235 	if (drv->shutdown)
236 		drv->driver.shutdown = hdac_ext_drv_shutdown;
237 
238 	return driver_register(&drv->driver);
239 }
240 EXPORT_SYMBOL_GPL(snd_hda_ext_driver_register);
241 
242 /**
243  * snd_hda_ext_driver_unregister - unregister a driver for ext hda devices
244  *
245  * @drv: ext hda driver structure
246  */
247 void snd_hda_ext_driver_unregister(struct hdac_driver *drv)
248 {
249 	driver_unregister(&drv->driver);
250 }
251 EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister);
252