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 | |
Vinod Koul | 99463b3 | 2015-06-17 11:20:18 +0530 | [diff] [blame] | 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 | |
Vinod Koul | 99463b3 | 2015-06-17 11:20:18 +0530 | [diff] [blame] | 50 | static const struct hdac_io_ops hdac_ext_default_io = { |
| 51 | .reg_writel = hdac_ext_writel, |
| 52 | .reg_readl = hdac_ext_readl, |
| 53 | .reg_writew = hdac_ext_writew, |
| 54 | .reg_readw = hdac_ext_readw, |
| 55 | .reg_writeb = hdac_ext_writeb, |
| 56 | .reg_readb = hdac_ext_readb, |
Vinod Koul | 99463b3 | 2015-06-17 11:20:18 +0530 | [diff] [blame] | 57 | }; |
| 58 | |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 59 | /** |
| 60 | * snd_hdac_ext_bus_init - initialize a HD-audio extended bus |
| 61 | * @ebus: the pointer to extended bus object |
| 62 | * @dev: device pointer |
| 63 | * @ops: bus verb operators |
Vinod Koul | 99463b3 | 2015-06-17 11:20:18 +0530 | [diff] [blame] | 64 | * @io_ops: lowlevel I/O operators, can be NULL. If NULL core will use |
| 65 | * default ops |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 66 | * |
| 67 | * Returns 0 if successful, or a negative error code. |
| 68 | */ |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 69 | 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] | 70 | const struct hdac_bus_ops *ops, |
Rakesh Ughreja | cb04ba3 | 2018-06-01 22:53:58 -0500 | [diff] [blame] | 71 | const struct hdac_io_ops *io_ops, |
| 72 | const struct hdac_ext_bus_ops *ext_ops) |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 73 | { |
| 74 | int ret; |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 75 | |
Vinod Koul | 99463b3 | 2015-06-17 11:20:18 +0530 | [diff] [blame] | 76 | /* check if io ops are provided, if not load the defaults */ |
| 77 | if (io_ops == NULL) |
| 78 | io_ops = &hdac_ext_default_io; |
| 79 | |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 80 | ret = snd_hdac_bus_init(bus, dev, ops, io_ops); |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 81 | if (ret < 0) |
| 82 | return ret; |
| 83 | |
Rakesh Ughreja | cb04ba3 | 2018-06-01 22:53:58 -0500 | [diff] [blame] | 84 | bus->ext_ops = ext_ops; |
Amadeusz Sławiński | 8a5b017 | 2019-06-17 13:36:35 +0200 | [diff] [blame] | 85 | /* FIXME: |
| 86 | * Currently only one bus is supported, if there is device with more |
| 87 | * buses, bus->idx should be greater than 0, but there needs to be a |
| 88 | * reliable way to always assign same number. |
| 89 | */ |
| 90 | bus->idx = 0; |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 91 | bus->cmd_dma_state = true; |
Vinod Koul | 4446085d | 2016-05-12 08:58:53 +0530 | [diff] [blame] | 92 | |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init); |
| 96 | |
| 97 | /** |
| 98 | * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus |
| 99 | * @ebus: the pointer to extended bus object |
| 100 | */ |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 101 | void snd_hdac_ext_bus_exit(struct hdac_bus *bus) |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 102 | { |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 103 | snd_hdac_bus_exit(bus); |
| 104 | WARN_ON(!list_empty(&bus->hlink_list)); |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 105 | } |
| 106 | EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit); |
| 107 | |
| 108 | static void default_release(struct device *dev) |
| 109 | { |
| 110 | snd_hdac_ext_bus_device_exit(container_of(dev, struct hdac_device, dev)); |
| 111 | } |
| 112 | |
| 113 | /** |
Vinod Koul | a512f56 | 2015-08-21 15:47:41 +0530 | [diff] [blame] | 114 | * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 115 | * @ebus: hdac extended bus to attach to |
| 116 | * @addr: codec address |
| 117 | * |
| 118 | * Returns zero for success or a negative error code. |
| 119 | */ |
Rakesh Ughreja | 6298542 | 2018-06-01 22:53:57 -0500 | [diff] [blame] | 120 | int snd_hdac_ext_bus_device_init(struct hdac_bus *bus, int addr, |
| 121 | struct hdac_device *hdev) |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 122 | { |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 123 | char name[15]; |
| 124 | int ret; |
| 125 | |
Rakesh Ughreja | 3787a39 | 2018-06-01 22:53:49 -0500 | [diff] [blame] | 126 | hdev->bus = bus; |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 127 | |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 128 | snprintf(name, sizeof(name), "ehdaudio%dD%d", bus->idx, addr); |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 129 | |
| 130 | ret = snd_hdac_device_init(hdev, bus, name, addr); |
| 131 | if (ret < 0) { |
| 132 | dev_err(bus->dev, "device init failed for hdac device\n"); |
| 133 | return ret; |
| 134 | } |
| 135 | hdev->type = HDA_DEV_ASOC; |
| 136 | hdev->dev.release = default_release; |
| 137 | |
| 138 | ret = snd_hdac_device_register(hdev); |
| 139 | if (ret) { |
| 140 | dev_err(bus->dev, "failed to register hdac device\n"); |
| 141 | snd_hdac_ext_bus_device_exit(hdev); |
| 142 | return ret; |
| 143 | } |
Vinod Koul | a512f56 | 2015-08-21 15:47:41 +0530 | [diff] [blame] | 144 | |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init); |
| 148 | |
| 149 | /** |
| 150 | * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device |
| 151 | * @hdev: hdac device to clean up |
| 152 | */ |
| 153 | void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev) |
| 154 | { |
| 155 | snd_hdac_device_exit(hdev); |
Jeeja KP | dfe66a1 | 2015-06-11 14:11:47 +0530 | [diff] [blame] | 156 | } |
| 157 | EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit); |
Vinod Koul | ee2d51b | 2015-08-21 15:47:40 +0530 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices |
| 161 | * |
| 162 | * @ebus: HD-audio extended bus |
| 163 | */ |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 164 | void snd_hdac_ext_bus_device_remove(struct hdac_bus *bus) |
Vinod Koul | ee2d51b | 2015-08-21 15:47:40 +0530 | [diff] [blame] | 165 | { |
| 166 | struct hdac_device *codec, *__codec; |
| 167 | /* |
| 168 | * we need to remove all the codec devices objects created in the |
| 169 | * snd_hdac_ext_bus_device_init |
| 170 | */ |
Rakesh Ughreja | 76f56fa | 2018-06-01 22:53:50 -0500 | [diff] [blame] | 171 | list_for_each_entry_safe(codec, __codec, &bus->codec_list, list) { |
Vinod Koul | ee2d51b | 2015-08-21 15:47:40 +0530 | [diff] [blame] | 172 | snd_hdac_device_unregister(codec); |
| 173 | put_device(&codec->dev); |
| 174 | } |
| 175 | } |
| 176 | EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 177 | #define dev_to_hdac(dev) (container_of((dev), \ |
| 178 | struct hdac_device, dev)) |
| 179 | |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 180 | static inline struct hdac_driver *get_hdrv(struct device *dev) |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 181 | { |
| 182 | struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver); |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 183 | return hdrv; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 184 | } |
| 185 | |
Rakesh Ughreja | 3787a39 | 2018-06-01 22:53:49 -0500 | [diff] [blame] | 186 | static inline struct hdac_device *get_hdev(struct device *dev) |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 187 | { |
| 188 | struct hdac_device *hdev = dev_to_hdac_dev(dev); |
Rakesh Ughreja | 3787a39 | 2018-06-01 22:53:49 -0500 | [diff] [blame] | 189 | return hdev; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int hda_ext_drv_probe(struct device *dev) |
| 193 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 194 | return (get_hdrv(dev))->probe(get_hdev(dev)); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static int hdac_ext_drv_remove(struct device *dev) |
| 198 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 199 | return (get_hdrv(dev))->remove(get_hdev(dev)); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void hdac_ext_drv_shutdown(struct device *dev) |
| 203 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 204 | return (get_hdrv(dev))->shutdown(get_hdev(dev)); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
| 208 | * snd_hda_ext_driver_register - register a driver for ext hda devices |
| 209 | * |
| 210 | * @drv: ext hda driver structure |
| 211 | */ |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 212 | int snd_hda_ext_driver_register(struct hdac_driver *drv) |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 213 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 214 | drv->type = HDA_DEV_ASOC; |
| 215 | drv->driver.bus = &snd_hda_bus_type; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 216 | /* we use default match */ |
| 217 | |
| 218 | if (drv->probe) |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 219 | drv->driver.probe = hda_ext_drv_probe; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 220 | if (drv->remove) |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 221 | drv->driver.remove = hdac_ext_drv_remove; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 222 | if (drv->shutdown) |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 223 | drv->driver.shutdown = hdac_ext_drv_shutdown; |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 224 | |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 225 | return driver_register(&drv->driver); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 226 | } |
| 227 | EXPORT_SYMBOL_GPL(snd_hda_ext_driver_register); |
| 228 | |
| 229 | /** |
| 230 | * snd_hda_ext_driver_unregister - unregister a driver for ext hda devices |
| 231 | * |
| 232 | * @drv: ext hda driver structure |
| 233 | */ |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 234 | void snd_hda_ext_driver_unregister(struct hdac_driver *drv) |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 235 | { |
Rakesh Ughreja | e1df931 | 2018-06-01 22:53:51 -0500 | [diff] [blame] | 236 | driver_unregister(&drv->driver); |
Vinod Koul | d51783c | 2015-08-21 15:47:42 +0530 | [diff] [blame] | 237 | } |
| 238 | EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister); |