blob: 8544e4fb1cd3be3cf231ddf998ce38b686a25034 [file] [log] [blame]
Jeeja KPdfe66a12015-06-11 14:11:47 +05301/*
2 * hdac-ext-bus.c - HD-audio extended core bus functions.
3 *
4 * Copyright (C) 2014-2015 Intel Corp
5 * Author: Jeeja KP <jeeja.kp@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 */
19
20#include <linux/module.h>
21#include <linux/slab.h>
22#include <sound/hdaudio_ext.h>
23
24MODULE_DESCRIPTION("HDA extended core");
25MODULE_LICENSE("GPL v2");
26
Vinod Koul99463b32015-06-17 11:20:18 +053027static void hdac_ext_writel(u32 value, u32 __iomem *addr)
28{
29 writel(value, addr);
30}
31
32static u32 hdac_ext_readl(u32 __iomem *addr)
33{
34 return readl(addr);
35}
36
37static void hdac_ext_writew(u16 value, u16 __iomem *addr)
38{
39 writew(value, addr);
40}
41
42static u16 hdac_ext_readw(u16 __iomem *addr)
43{
44 return readw(addr);
45}
46
47static void hdac_ext_writeb(u8 value, u8 __iomem *addr)
48{
49 writeb(value, addr);
50}
51
52static u8 hdac_ext_readb(u8 __iomem *addr)
53{
54 return readb(addr);
55}
56
57static int hdac_ext_dma_alloc_pages(struct hdac_bus *bus, int type,
58 size_t size, struct snd_dma_buffer *buf)
59{
60 return snd_dma_alloc_pages(type, bus->dev, size, buf);
61}
62
63static void hdac_ext_dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
64{
65 snd_dma_free_pages(buf);
66}
67
68static const struct hdac_io_ops hdac_ext_default_io = {
69 .reg_writel = hdac_ext_writel,
70 .reg_readl = hdac_ext_readl,
71 .reg_writew = hdac_ext_writew,
72 .reg_readw = hdac_ext_readw,
73 .reg_writeb = hdac_ext_writeb,
74 .reg_readb = hdac_ext_readb,
75 .dma_alloc_pages = hdac_ext_dma_alloc_pages,
76 .dma_free_pages = hdac_ext_dma_free_pages,
77};
78
Jeeja KPdfe66a12015-06-11 14:11:47 +053079/**
80 * snd_hdac_ext_bus_init - initialize a HD-audio extended bus
81 * @ebus: the pointer to extended bus object
82 * @dev: device pointer
83 * @ops: bus verb operators
Vinod Koul99463b32015-06-17 11:20:18 +053084 * @io_ops: lowlevel I/O operators, can be NULL. If NULL core will use
85 * default ops
Jeeja KPdfe66a12015-06-11 14:11:47 +053086 *
87 * Returns 0 if successful, or a negative error code.
88 */
89int snd_hdac_ext_bus_init(struct hdac_ext_bus *ebus, struct device *dev,
90 const struct hdac_bus_ops *ops,
91 const struct hdac_io_ops *io_ops)
92{
93 int ret;
94 static int idx;
95
Vinod Koul99463b32015-06-17 11:20:18 +053096 /* check if io ops are provided, if not load the defaults */
97 if (io_ops == NULL)
98 io_ops = &hdac_ext_default_io;
99
Jeeja KPdfe66a12015-06-11 14:11:47 +0530100 ret = snd_hdac_bus_init(&ebus->bus, dev, ops, io_ops);
101 if (ret < 0)
102 return ret;
103
104 INIT_LIST_HEAD(&ebus->hlink_list);
105 ebus->idx = idx++;
106
107 return 0;
108}
109EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init);
110
111/**
112 * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus
113 * @ebus: the pointer to extended bus object
114 */
115void snd_hdac_ext_bus_exit(struct hdac_ext_bus *ebus)
116{
117 snd_hdac_bus_exit(&ebus->bus);
118 WARN_ON(!list_empty(&ebus->hlink_list));
119}
120EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit);
121
122static void default_release(struct device *dev)
123{
124 snd_hdac_ext_bus_device_exit(container_of(dev, struct hdac_device, dev));
125}
126
127/**
Vinod Koula512f562015-08-21 15:47:41 +0530128 * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device
Jeeja KPdfe66a12015-06-11 14:11:47 +0530129 * @ebus: hdac extended bus to attach to
130 * @addr: codec address
131 *
132 * Returns zero for success or a negative error code.
133 */
134int snd_hdac_ext_bus_device_init(struct hdac_ext_bus *ebus, int addr)
135{
Vinod Koula512f562015-08-21 15:47:41 +0530136 struct hdac_ext_device *edev;
Jeeja KPdfe66a12015-06-11 14:11:47 +0530137 struct hdac_device *hdev = NULL;
138 struct hdac_bus *bus = ebus_to_hbus(ebus);
139 char name[15];
140 int ret;
141
Vinod Koula512f562015-08-21 15:47:41 +0530142 edev = kzalloc(sizeof(*hdev), GFP_KERNEL);
143 if (!edev)
Jeeja KPdfe66a12015-06-11 14:11:47 +0530144 return -ENOMEM;
Vinod Koula512f562015-08-21 15:47:41 +0530145 hdev = &edev->hdac;
Jeeja KPdfe66a12015-06-11 14:11:47 +0530146
147 snprintf(name, sizeof(name), "ehdaudio%dD%d", ebus->idx, addr);
148
149 ret = snd_hdac_device_init(hdev, bus, name, addr);
150 if (ret < 0) {
151 dev_err(bus->dev, "device init failed for hdac device\n");
152 return ret;
153 }
154 hdev->type = HDA_DEV_ASOC;
155 hdev->dev.release = default_release;
156
157 ret = snd_hdac_device_register(hdev);
158 if (ret) {
159 dev_err(bus->dev, "failed to register hdac device\n");
160 snd_hdac_ext_bus_device_exit(hdev);
161 return ret;
162 }
Vinod Koula512f562015-08-21 15:47:41 +0530163
Jeeja KPdfe66a12015-06-11 14:11:47 +0530164 return 0;
165}
166EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init);
167
168/**
169 * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device
170 * @hdev: hdac device to clean up
171 */
172void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev)
173{
Vinod Koula512f562015-08-21 15:47:41 +0530174 struct hdac_ext_device *edev = to_ehdac_device(hdev);
175
Jeeja KPdfe66a12015-06-11 14:11:47 +0530176 snd_hdac_device_exit(hdev);
Vinod Koula512f562015-08-21 15:47:41 +0530177 kfree(edev);
Jeeja KPdfe66a12015-06-11 14:11:47 +0530178}
179EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit);
Vinod Koulee2d51b2015-08-21 15:47:40 +0530180
181/**
182 * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices
183 *
184 * @ebus: HD-audio extended bus
185 */
186void snd_hdac_ext_bus_device_remove(struct hdac_ext_bus *ebus)
187{
188 struct hdac_device *codec, *__codec;
189 /*
190 * we need to remove all the codec devices objects created in the
191 * snd_hdac_ext_bus_device_init
192 */
193 list_for_each_entry_safe(codec, __codec, &ebus->bus.codec_list, list) {
194 snd_hdac_device_unregister(codec);
195 put_device(&codec->dev);
196 }
197}
198EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove);
Vinod Kould51783c2015-08-21 15:47:42 +0530199#define dev_to_hdac(dev) (container_of((dev), \
200 struct hdac_device, dev))
201
202static inline struct hdac_ext_driver *get_edrv(struct device *dev)
203{
204 struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver);
205 struct hdac_ext_driver *edrv = to_ehdac_driver(hdrv);
206
207 return edrv;
208}
209
210static inline struct hdac_ext_device *get_edev(struct device *dev)
211{
212 struct hdac_device *hdev = dev_to_hdac_dev(dev);
213 struct hdac_ext_device *edev = to_ehdac_device(hdev);
214
215 return edev;
216}
217
218static int hda_ext_drv_probe(struct device *dev)
219{
220 return (get_edrv(dev))->probe(get_edev(dev));
221}
222
223static int hdac_ext_drv_remove(struct device *dev)
224{
225 return (get_edrv(dev))->remove(get_edev(dev));
226}
227
228static void hdac_ext_drv_shutdown(struct device *dev)
229{
230 return (get_edrv(dev))->shutdown(get_edev(dev));
231}
232
233/**
234 * snd_hda_ext_driver_register - register a driver for ext hda devices
235 *
236 * @drv: ext hda driver structure
237 */
238int snd_hda_ext_driver_register(struct hdac_ext_driver *drv)
239{
240 drv->hdac.type = HDA_DEV_ASOC;
241 drv->hdac.driver.bus = &snd_hda_bus_type;
242 /* we use default match */
243
244 if (drv->probe)
245 drv->hdac.driver.probe = hda_ext_drv_probe;
246 if (drv->remove)
247 drv->hdac.driver.remove = hdac_ext_drv_remove;
248 if (drv->shutdown)
249 drv->hdac.driver.shutdown = hdac_ext_drv_shutdown;
250
251 return driver_register(&drv->hdac.driver);
252}
253EXPORT_SYMBOL_GPL(snd_hda_ext_driver_register);
254
255/**
256 * snd_hda_ext_driver_unregister - unregister a driver for ext hda devices
257 *
258 * @drv: ext hda driver structure
259 */
260void snd_hda_ext_driver_unregister(struct hdac_ext_driver *drv)
261{
262 driver_unregister(&drv->hdac.driver);
263}
264EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister);