Thomas Gleixner | 8e8e69d | 2019-05-29 07:17:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 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 | * |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
Vinod Koul | 42f2bb1 | 2015-10-13 14:57:49 +0530 | [diff] [blame] | 14 | #include <linux/io.h> |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 15 | #include <sound/hdaudio_ext.h> |
| 16 | |
| 17 | MODULE_DESCRIPTION("HDA extended core"); |
| 18 | MODULE_LICENSE("GPL v2"); |
| 19 | |
| 20 | /** |
| 21 | * snd_hdac_ext_bus_init - initialize a HD-audio extended bus |
Keyon Jie | 6e57188f | 2020-01-13 14:56:38 -0600 | [diff] [blame] | 22 | * @bus: the pointer to HDAC bus object |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 23 | * @dev: device pointer |
| 24 | * @ops: bus verb operators |
Keyon Jie | 6e57188f | 2020-01-13 14:56:38 -0600 | [diff] [blame] | 25 | * @ext_ops: operators used for ASoC HDA codec drivers |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 26 | * |
| 27 | * Returns 0 if successful, or a negative error code. |
| 28 | */ |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 29 | int snd_hdac_ext_bus_init(struct hdac_bus *bus, struct device *dev, |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 30 | const struct hdac_bus_ops *ops, |
Rakesh Ughreja | cb04ba3 | 2018-06-01 22:53:58 -0500 | [diff] [blame] | 31 | const struct hdac_ext_bus_ops *ext_ops) |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 32 | { |
| 33 | int ret; |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 34 | |
Takashi Iwai | 19abfef | 2019-08-07 20:32:08 +0200 | [diff] [blame] | 35 | ret = snd_hdac_bus_init(bus, dev, ops); |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 36 | if (ret < 0) |
| 37 | return ret; |
| 38 | |
Rakesh Ughreja | cb04ba3 | 2018-06-01 22:53:58 -0500 | [diff] [blame] | 39 | bus->ext_ops = ext_ops; |
Amadeusz Sławiński | 8a5b017 | 2019-06-17 13:36:35 +0200 | [diff] [blame] | 40 | /* 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 Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 46 | bus->cmd_dma_state = true; |
Vinod Koul | 4446085d | 2016-05-12 08:58:53 +0530 | [diff] [blame] | 47 | |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init); |
| 51 | |
| 52 | /** |
| 53 | * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus |
Keyon Jie | 6e57188f | 2020-01-13 14:56:38 -0600 | [diff] [blame] | 54 | * @bus: the pointer to HDAC bus object |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 55 | */ |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 56 | void snd_hdac_ext_bus_exit(struct hdac_bus *bus) |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 57 | { |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 58 | snd_hdac_bus_exit(bus); |
| 59 | WARN_ON(!list_empty(&bus->hlink_list)); |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 60 | } |
| 61 | EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit); |
| 62 | |
| 63 | static void default_release(struct device *dev) |
| 64 | { |
Kai-Heng Feng | 50f0bf5 | 2020-05-05 11:03:52 +0800 | [diff] [blame] | 65 | snd_hdac_ext_bus_device_exit(dev_to_hdac_dev(dev)); |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Vinod Koul | a512f56 | 2015-08-21 15:47:41 +0530 | [diff] [blame] | 69 | * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device |
Keyon Jie | 6e57188f | 2020-01-13 14:56:38 -0600 | [diff] [blame] | 70 | * @bus: hdac bus to attach to |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 71 | * @addr: codec address |
Keyon Jie | 6e57188f | 2020-01-13 14:56:38 -0600 | [diff] [blame] | 72 | * @hdev: hdac device to init |
Kai Vehmanen | 163cd10 | 2020-09-21 13:08:41 +0300 | [diff] [blame] | 73 | * @type: codec type (HDAC_DEV_*) to use for this device |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 74 | * |
| 75 | * Returns zero for success or a negative error code. |
| 76 | */ |
Rakesh Ughreja | 6298542 | 2018-06-01 22:53:57 -0500 | [diff] [blame] | 77 | int snd_hdac_ext_bus_device_init(struct hdac_bus *bus, int addr, |
Kai Vehmanen | 163cd10 | 2020-09-21 13:08:41 +0300 | [diff] [blame] | 78 | struct hdac_device *hdev, int type) |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 79 | { |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 80 | char name[15]; |
| 81 | int ret; |
| 82 | |
Rakesh Ughreja | 3787a39 | 2018-06-01 22:53:49 -0500 | [diff] [blame] | 83 | hdev->bus = bus; |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 84 | |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 85 | snprintf(name, sizeof(name), "ehdaudio%dD%d", bus->idx, addr); |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 86 | |
| 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 Vehmanen | 163cd10 | 2020-09-21 13:08:41 +0300 | [diff] [blame] | 92 | hdev->type = type; |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 93 | 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 Koul | a512f56 | 2015-08-21 15:47:41 +0530 | [diff] [blame] | 101 | |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | EXPORT_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 | */ |
| 110 | void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev) |
| 111 | { |
| 112 | snd_hdac_device_exit(hdev); |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 113 | } |
| 114 | EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit); |
Vinod Koul | ee2d51b | 2015-08-21 15:47:40 +0530 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices |
| 118 | * |
Keyon Jie | 6e57188f | 2020-01-13 14:56:38 -0600 | [diff] [blame] | 119 | * @bus: the pointer to HDAC bus object |
Vinod Koul | ee2d51b | 2015-08-21 15:47:40 +0530 | [diff] [blame] | 120 | */ |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 121 | void snd_hdac_ext_bus_device_remove(struct hdac_bus *bus) |
Vinod Koul | ee2d51b | 2015-08-21 15:47:40 +0530 | [diff] [blame] | 122 | { |
| 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 Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 128 | list_for_each_entry_safe(codec, __codec, &bus->codec_list, list) { |
Vinod Koul | ee2d51b | 2015-08-21 15:47:40 +0530 | [diff] [blame] | 129 | snd_hdac_device_unregister(codec); |
| 130 | put_device(&codec->dev); |
| 131 | } |
| 132 | } |
| 133 | EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 134 | #define dev_to_hdac(dev) (container_of((dev), \ |
| 135 | struct hdac_device, dev)) |
| 136 | |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 137 | static inline struct hdac_driver *get_hdrv(struct device *dev) |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 138 | { |
| 139 | struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver); |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 140 | return hdrv; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 141 | } |
| 142 | |
Rakesh Ughreja | 3787a39 | 2018-06-01 22:53:49 -0500 | [diff] [blame] | 143 | static inline struct hdac_device *get_hdev(struct device *dev) |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 144 | { |
| 145 | struct hdac_device *hdev = dev_to_hdac_dev(dev); |
Rakesh Ughreja | 3787a39 | 2018-06-01 22:53:49 -0500 | [diff] [blame] | 146 | return hdev; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static int hda_ext_drv_probe(struct device *dev) |
| 150 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 151 | return (get_hdrv(dev))->probe(get_hdev(dev)); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static int hdac_ext_drv_remove(struct device *dev) |
| 155 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 156 | return (get_hdrv(dev))->remove(get_hdev(dev)); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void hdac_ext_drv_shutdown(struct device *dev) |
| 160 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 161 | return (get_hdrv(dev))->shutdown(get_hdev(dev)); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * snd_hda_ext_driver_register - register a driver for ext hda devices |
| 166 | * |
| 167 | * @drv: ext hda driver structure |
| 168 | */ |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 169 | int snd_hda_ext_driver_register(struct hdac_driver *drv) |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 170 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 171 | drv->type = HDA_DEV_ASOC; |
| 172 | drv->driver.bus = &snd_hda_bus_type; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 173 | /* we use default match */ |
| 174 | |
| 175 | if (drv->probe) |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 176 | drv->driver.probe = hda_ext_drv_probe; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 177 | if (drv->remove) |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 178 | drv->driver.remove = hdac_ext_drv_remove; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 179 | if (drv->shutdown) |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 180 | drv->driver.shutdown = hdac_ext_drv_shutdown; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 181 | |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 182 | return driver_register(&drv->driver); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 183 | } |
| 184 | EXPORT_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 Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 191 | void snd_hda_ext_driver_unregister(struct hdac_driver *drv) |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 192 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 193 | driver_unregister(&drv->driver); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 194 | } |
| 195 | EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister); |