blob: 765c40a6ccbadabc4ba5b1227f0ea1e901b3a75f [file] [log] [blame]
Thomas Gleixner8e8e69d2019-05-29 07:17:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Jeeja KPdfe66a12015-06-11 14:11:47 +05302/*
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 *
Jeeja KPdfe66a12015-06-11 14:11:47 +05309 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 */
11
12#include <linux/module.h>
13#include <linux/slab.h>
Vinod Koul42f2bb12015-10-13 14:57:49 +053014#include <linux/io.h>
Jeeja KPdfe66a12015-06-11 14:11:47 +053015#include <sound/hdaudio_ext.h>
16
17MODULE_DESCRIPTION("HDA extended core");
18MODULE_LICENSE("GPL v2");
19
20/**
21 * snd_hdac_ext_bus_init - initialize a HD-audio extended bus
Keyon Jie6e57188f2020-01-13 14:56:38 -060022 * @bus: the pointer to HDAC bus object
Jeeja KPdfe66a12015-06-11 14:11:47 +053023 * @dev: device pointer
24 * @ops: bus verb operators
Keyon Jie6e57188f2020-01-13 14:56:38 -060025 * @ext_ops: operators used for ASoC HDA codec drivers
Jeeja KPdfe66a12015-06-11 14:11:47 +053026 *
27 * Returns 0 if successful, or a negative error code.
28 */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -050029int snd_hdac_ext_bus_init(struct hdac_bus *bus, struct device *dev,
Jeeja KPdfe66a12015-06-11 14:11:47 +053030 const struct hdac_bus_ops *ops,
Rakesh Ughrejacb04ba32018-06-01 22:53:58 -050031 const struct hdac_ext_bus_ops *ext_ops)
Jeeja KPdfe66a12015-06-11 14:11:47 +053032{
33 int ret;
Jeeja KPdfe66a12015-06-11 14:11:47 +053034
Takashi Iwai19abfef2019-08-07 20:32:08 +020035 ret = snd_hdac_bus_init(bus, dev, ops);
Jeeja KPdfe66a12015-06-11 14:11:47 +053036 if (ret < 0)
37 return ret;
38
Rakesh Ughrejacb04ba32018-06-01 22:53:58 -050039 bus->ext_ops = ext_ops;
Amadeusz Sławiński8a5b0172019-06-17 13:36:35 +020040 /* FIXME:
41 * Currently only one bus is supported, if there is device with more
42 * buses, bus->idx should be greater than 0, but there needs to be a
43 * reliable way to always assign same number.
44 */
45 bus->idx = 0;
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -050046 bus->cmd_dma_state = true;
Vinod Koul4446085d2016-05-12 08:58:53 +053047
Jeeja KPdfe66a12015-06-11 14:11:47 +053048 return 0;
49}
50EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init);
51
52/**
53 * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus
Keyon Jie6e57188f2020-01-13 14:56:38 -060054 * @bus: the pointer to HDAC bus object
Jeeja KPdfe66a12015-06-11 14:11:47 +053055 */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -050056void snd_hdac_ext_bus_exit(struct hdac_bus *bus)
Jeeja KPdfe66a12015-06-11 14:11:47 +053057{
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -050058 snd_hdac_bus_exit(bus);
59 WARN_ON(!list_empty(&bus->hlink_list));
Jeeja KPdfe66a12015-06-11 14:11:47 +053060}
61EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit);
62
63static void default_release(struct device *dev)
64{
Kai-Heng Feng50f0bf52020-05-05 11:03:52 +080065 snd_hdac_ext_bus_device_exit(dev_to_hdac_dev(dev));
Jeeja KPdfe66a12015-06-11 14:11:47 +053066}
67
68/**
Vinod Koula512f562015-08-21 15:47:41 +053069 * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device
Keyon Jie6e57188f2020-01-13 14:56:38 -060070 * @bus: hdac bus to attach to
Jeeja KPdfe66a12015-06-11 14:11:47 +053071 * @addr: codec address
Keyon Jie6e57188f2020-01-13 14:56:38 -060072 * @hdev: hdac device to init
Kai Vehmanen163cd102020-09-21 13:08:41 +030073 * @type: codec type (HDAC_DEV_*) to use for this device
Jeeja KPdfe66a12015-06-11 14:11:47 +053074 *
75 * Returns zero for success or a negative error code.
76 */
Rakesh Ughreja62985422018-06-01 22:53:57 -050077int snd_hdac_ext_bus_device_init(struct hdac_bus *bus, int addr,
Kai Vehmanen163cd102020-09-21 13:08:41 +030078 struct hdac_device *hdev, int type)
Jeeja KPdfe66a12015-06-11 14:11:47 +053079{
Jeeja KPdfe66a12015-06-11 14:11:47 +053080 char name[15];
81 int ret;
82
Rakesh Ughreja3787a392018-06-01 22:53:49 -050083 hdev->bus = bus;
Jeeja KPdfe66a12015-06-11 14:11:47 +053084
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -050085 snprintf(name, sizeof(name), "ehdaudio%dD%d", bus->idx, addr);
Jeeja KPdfe66a12015-06-11 14:11:47 +053086
87 ret = snd_hdac_device_init(hdev, bus, name, addr);
88 if (ret < 0) {
89 dev_err(bus->dev, "device init failed for hdac device\n");
90 return ret;
91 }
Kai Vehmanen163cd102020-09-21 13:08:41 +030092 hdev->type = type;
Jeeja KPdfe66a12015-06-11 14:11:47 +053093 hdev->dev.release = default_release;
94
95 ret = snd_hdac_device_register(hdev);
96 if (ret) {
97 dev_err(bus->dev, "failed to register hdac device\n");
98 snd_hdac_ext_bus_device_exit(hdev);
99 return ret;
100 }
Vinod Koula512f562015-08-21 15:47:41 +0530101
Jeeja KPdfe66a12015-06-11 14:11:47 +0530102 return 0;
103}
104EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init);
105
106/**
107 * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device
108 * @hdev: hdac device to clean up
109 */
110void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev)
111{
112 snd_hdac_device_exit(hdev);
Jeeja KPdfe66a12015-06-11 14:11:47 +0530113}
114EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit);
Vinod Koulee2d51b2015-08-21 15:47:40 +0530115
116/**
117 * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices
118 *
Keyon Jie6e57188f2020-01-13 14:56:38 -0600119 * @bus: the pointer to HDAC bus object
Vinod Koulee2d51b2015-08-21 15:47:40 +0530120 */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500121void snd_hdac_ext_bus_device_remove(struct hdac_bus *bus)
Vinod Koulee2d51b2015-08-21 15:47:40 +0530122{
123 struct hdac_device *codec, *__codec;
124 /*
125 * we need to remove all the codec devices objects created in the
126 * snd_hdac_ext_bus_device_init
127 */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -0500128 list_for_each_entry_safe(codec, __codec, &bus->codec_list, list) {
Vinod Koulee2d51b2015-08-21 15:47:40 +0530129 snd_hdac_device_unregister(codec);
130 put_device(&codec->dev);
131 }
132}
133EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove);
Vinod Kould51783c2015-08-21 15:47:42 +0530134#define dev_to_hdac(dev) (container_of((dev), \
135 struct hdac_device, dev))
136
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500137static inline struct hdac_driver *get_hdrv(struct device *dev)
Vinod Kould51783c2015-08-21 15:47:42 +0530138{
139 struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver);
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500140 return hdrv;
Vinod Kould51783c2015-08-21 15:47:42 +0530141}
142
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500143static inline struct hdac_device *get_hdev(struct device *dev)
Vinod Kould51783c2015-08-21 15:47:42 +0530144{
145 struct hdac_device *hdev = dev_to_hdac_dev(dev);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500146 return hdev;
Vinod Kould51783c2015-08-21 15:47:42 +0530147}
148
149static int hda_ext_drv_probe(struct device *dev)
150{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500151 return (get_hdrv(dev))->probe(get_hdev(dev));
Vinod Kould51783c2015-08-21 15:47:42 +0530152}
153
154static int hdac_ext_drv_remove(struct device *dev)
155{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500156 return (get_hdrv(dev))->remove(get_hdev(dev));
Vinod Kould51783c2015-08-21 15:47:42 +0530157}
158
159static void hdac_ext_drv_shutdown(struct device *dev)
160{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500161 return (get_hdrv(dev))->shutdown(get_hdev(dev));
Vinod Kould51783c2015-08-21 15:47:42 +0530162}
163
164/**
165 * snd_hda_ext_driver_register - register a driver for ext hda devices
166 *
167 * @drv: ext hda driver structure
168 */
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500169int snd_hda_ext_driver_register(struct hdac_driver *drv)
Vinod Kould51783c2015-08-21 15:47:42 +0530170{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500171 drv->type = HDA_DEV_ASOC;
172 drv->driver.bus = &snd_hda_bus_type;
Vinod Kould51783c2015-08-21 15:47:42 +0530173 /* we use default match */
174
175 if (drv->probe)
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500176 drv->driver.probe = hda_ext_drv_probe;
Vinod Kould51783c2015-08-21 15:47:42 +0530177 if (drv->remove)
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500178 drv->driver.remove = hdac_ext_drv_remove;
Vinod Kould51783c2015-08-21 15:47:42 +0530179 if (drv->shutdown)
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500180 drv->driver.shutdown = hdac_ext_drv_shutdown;
Vinod Kould51783c2015-08-21 15:47:42 +0530181
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500182 return driver_register(&drv->driver);
Vinod Kould51783c2015-08-21 15:47:42 +0530183}
184EXPORT_SYMBOL_GPL(snd_hda_ext_driver_register);
185
186/**
187 * snd_hda_ext_driver_unregister - unregister a driver for ext hda devices
188 *
189 * @drv: ext hda driver structure
190 */
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500191void snd_hda_ext_driver_unregister(struct hdac_driver *drv)
Vinod Kould51783c2015-08-21 15:47:42 +0530192{
Rakesh Ughrejae1df9312018-06-01 22:53:51 -0500193 driver_unregister(&drv->driver);
Vinod Kould51783c2015-08-21 15:47:42 +0530194}
195EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister);