blob: 9c37d9af3023f67bdba2ba4a3d207580cd1f70c5 [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>
Vinod Koul42f2bb12015-10-13 14:57:49 +053022#include <linux/io.h>
Jeeja KPdfe66a12015-06-11 14:11:47 +053023#include <sound/hdaudio_ext.h>
24
25MODULE_DESCRIPTION("HDA extended core");
26MODULE_LICENSE("GPL v2");
27
Vinod Koul99463b32015-06-17 11:20:18 +053028static void hdac_ext_writel(u32 value, u32 __iomem *addr)
29{
30 writel(value, addr);
31}
32
33static u32 hdac_ext_readl(u32 __iomem *addr)
34{
35 return readl(addr);
36}
37
38static void hdac_ext_writew(u16 value, u16 __iomem *addr)
39{
40 writew(value, addr);
41}
42
43static u16 hdac_ext_readw(u16 __iomem *addr)
44{
45 return readw(addr);
46}
47
48static void hdac_ext_writeb(u8 value, u8 __iomem *addr)
49{
50 writeb(value, addr);
51}
52
53static u8 hdac_ext_readb(u8 __iomem *addr)
54{
55 return readb(addr);
56}
57
58static int hdac_ext_dma_alloc_pages(struct hdac_bus *bus, int type,
59 size_t size, struct snd_dma_buffer *buf)
60{
61 return snd_dma_alloc_pages(type, bus->dev, size, buf);
62}
63
64static void hdac_ext_dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
65{
66 snd_dma_free_pages(buf);
67}
68
69static const struct hdac_io_ops hdac_ext_default_io = {
70 .reg_writel = hdac_ext_writel,
71 .reg_readl = hdac_ext_readl,
72 .reg_writew = hdac_ext_writew,
73 .reg_readw = hdac_ext_readw,
74 .reg_writeb = hdac_ext_writeb,
75 .reg_readb = hdac_ext_readb,
76 .dma_alloc_pages = hdac_ext_dma_alloc_pages,
77 .dma_free_pages = hdac_ext_dma_free_pages,
78};
79
Jeeja KPdfe66a12015-06-11 14:11:47 +053080/**
81 * snd_hdac_ext_bus_init - initialize a HD-audio extended bus
82 * @ebus: the pointer to extended bus object
83 * @dev: device pointer
84 * @ops: bus verb operators
Vinod Koul99463b32015-06-17 11:20:18 +053085 * @io_ops: lowlevel I/O operators, can be NULL. If NULL core will use
86 * default ops
Jeeja KPdfe66a12015-06-11 14:11:47 +053087 *
88 * Returns 0 if successful, or a negative error code.
89 */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -050090int snd_hdac_ext_bus_init(struct hdac_bus *bus, struct device *dev,
Jeeja KPdfe66a12015-06-11 14:11:47 +053091 const struct hdac_bus_ops *ops,
Rakesh Ughrejacb04ba32018-06-01 22:53:58 -050092 const struct hdac_io_ops *io_ops,
93 const struct hdac_ext_bus_ops *ext_ops)
Jeeja KPdfe66a12015-06-11 14:11:47 +053094{
95 int ret;
96 static int idx;
97
Vinod Koul99463b32015-06-17 11:20:18 +053098 /* check if io ops are provided, if not load the defaults */
99 if (io_ops == NULL)
100 io_ops = &hdac_ext_default_io;
101
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500102 ret = snd_hdac_bus_init(bus, dev, ops, io_ops);
Jeeja KPdfe66a12015-06-11 14:11:47 +0530103 if (ret < 0)
104 return ret;
105
Rakesh Ughrejacb04ba32018-06-01 22:53:58 -0500106 bus->ext_ops = ext_ops;
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500107 INIT_LIST_HEAD(&bus->hlink_list);
108 bus->idx = idx++;
Jeeja KPdfe66a12015-06-11 14:11:47 +0530109
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500110 mutex_init(&bus->lock);
111 bus->cmd_dma_state = true;
Vinod Koul4446085d2016-05-12 08:58:53 +0530112
Jeeja KPdfe66a12015-06-11 14:11:47 +0530113 return 0;
114}
115EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init);
116
117/**
118 * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus
119 * @ebus: the pointer to extended bus object
120 */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500121void snd_hdac_ext_bus_exit(struct hdac_bus *bus)
Jeeja KPdfe66a12015-06-11 14:11:47 +0530122{
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500123 snd_hdac_bus_exit(bus);
124 WARN_ON(!list_empty(&bus->hlink_list));
Jeeja KPdfe66a12015-06-11 14:11:47 +0530125}
126EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit);
127
128static void default_release(struct device *dev)
129{
130 snd_hdac_ext_bus_device_exit(container_of(dev, struct hdac_device, dev));
131}
132
133/**
Vinod Koula512f562015-08-21 15:47:41 +0530134 * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device
Jeeja KPdfe66a12015-06-11 14:11:47 +0530135 * @ebus: hdac extended bus to attach to
136 * @addr: codec address
137 *
138 * Returns zero for success or a negative error code.
139 */
Rakesh Ughreja62985422018-06-01 22:53:57 -0500140int snd_hdac_ext_bus_device_init(struct hdac_bus *bus, int addr,
141 struct hdac_device *hdev)
Jeeja KPdfe66a12015-06-11 14:11:47 +0530142{
Jeeja KPdfe66a12015-06-11 14:11:47 +0530143 char name[15];
144 int ret;
145
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500146 hdev->bus = bus;
Jeeja KPdfe66a12015-06-11 14:11:47 +0530147
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500148 snprintf(name, sizeof(name), "ehdaudio%dD%d", bus->idx, addr);
Jeeja KPdfe66a12015-06-11 14:11:47 +0530149
150 ret = snd_hdac_device_init(hdev, bus, name, addr);
151 if (ret < 0) {
152 dev_err(bus->dev, "device init failed for hdac device\n");
153 return ret;
154 }
155 hdev->type = HDA_DEV_ASOC;
156 hdev->dev.release = default_release;
157
158 ret = snd_hdac_device_register(hdev);
159 if (ret) {
160 dev_err(bus->dev, "failed to register hdac device\n");
161 snd_hdac_ext_bus_device_exit(hdev);
162 return ret;
163 }
Vinod Koula512f562015-08-21 15:47:41 +0530164
Jeeja KPdfe66a12015-06-11 14:11:47 +0530165 return 0;
166}
167EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init);
168
169/**
170 * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device
171 * @hdev: hdac device to clean up
172 */
173void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev)
174{
175 snd_hdac_device_exit(hdev);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500176 kfree(hdev);
Jeeja KPdfe66a12015-06-11 14:11:47 +0530177}
178EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit);
Vinod Koulee2d51b2015-08-21 15:47:40 +0530179
180/**
181 * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices
182 *
183 * @ebus: HD-audio extended bus
184 */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500185void snd_hdac_ext_bus_device_remove(struct hdac_bus *bus)
Vinod Koulee2d51b2015-08-21 15:47:40 +0530186{
187 struct hdac_device *codec, *__codec;
188 /*
189 * we need to remove all the codec devices objects created in the
190 * snd_hdac_ext_bus_device_init
191 */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500192 list_for_each_entry_safe(codec, __codec, &bus->codec_list, list) {
Vinod Koulee2d51b2015-08-21 15:47:40 +0530193 snd_hdac_device_unregister(codec);
194 put_device(&codec->dev);
195 }
196}
197EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove);
Vinod Kould51783c2015-08-21 15:47:42 +0530198#define dev_to_hdac(dev) (container_of((dev), \
199 struct hdac_device, dev))
200
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500201static inline struct hdac_driver *get_hdrv(struct device *dev)
Vinod Kould51783c2015-08-21 15:47:42 +0530202{
203 struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver);
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500204 return hdrv;
Vinod Kould51783c2015-08-21 15:47:42 +0530205}
206
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500207static inline struct hdac_device *get_hdev(struct device *dev)
Vinod Kould51783c2015-08-21 15:47:42 +0530208{
209 struct hdac_device *hdev = dev_to_hdac_dev(dev);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500210 return hdev;
Vinod Kould51783c2015-08-21 15:47:42 +0530211}
212
213static int hda_ext_drv_probe(struct device *dev)
214{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500215 return (get_hdrv(dev))->probe(get_hdev(dev));
Vinod Kould51783c2015-08-21 15:47:42 +0530216}
217
218static int hdac_ext_drv_remove(struct device *dev)
219{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500220 return (get_hdrv(dev))->remove(get_hdev(dev));
Vinod Kould51783c2015-08-21 15:47:42 +0530221}
222
223static void hdac_ext_drv_shutdown(struct device *dev)
224{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500225 return (get_hdrv(dev))->shutdown(get_hdev(dev));
Vinod Kould51783c2015-08-21 15:47:42 +0530226}
227
228/**
229 * snd_hda_ext_driver_register - register a driver for ext hda devices
230 *
231 * @drv: ext hda driver structure
232 */
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500233int snd_hda_ext_driver_register(struct hdac_driver *drv)
Vinod Kould51783c2015-08-21 15:47:42 +0530234{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500235 drv->type = HDA_DEV_ASOC;
236 drv->driver.bus = &snd_hda_bus_type;
Vinod Kould51783c2015-08-21 15:47:42 +0530237 /* we use default match */
238
239 if (drv->probe)
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500240 drv->driver.probe = hda_ext_drv_probe;
Vinod Kould51783c2015-08-21 15:47:42 +0530241 if (drv->remove)
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500242 drv->driver.remove = hdac_ext_drv_remove;
Vinod Kould51783c2015-08-21 15:47:42 +0530243 if (drv->shutdown)
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500244 drv->driver.shutdown = hdac_ext_drv_shutdown;
Vinod Kould51783c2015-08-21 15:47:42 +0530245
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500246 return driver_register(&drv->driver);
Vinod Kould51783c2015-08-21 15:47:42 +0530247}
248EXPORT_SYMBOL_GPL(snd_hda_ext_driver_register);
249
250/**
251 * snd_hda_ext_driver_unregister - unregister a driver for ext hda devices
252 *
253 * @drv: ext hda driver structure
254 */
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500255void snd_hda_ext_driver_unregister(struct hdac_driver *drv)
Vinod Kould51783c2015-08-21 15:47:42 +0530256{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500257 driver_unregister(&drv->driver);
Vinod Kould51783c2015-08-21 15:47:42 +0530258}
259EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister);