18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2dfe66a18SJeeja KP /* 3dfe66a18SJeeja KP * hdac-ext-bus.c - HD-audio extended core bus functions. 4dfe66a18SJeeja KP * 5dfe66a18SJeeja KP * Copyright (C) 2014-2015 Intel Corp 6dfe66a18SJeeja KP * Author: Jeeja KP <jeeja.kp@intel.com> 7dfe66a18SJeeja KP * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8dfe66a18SJeeja KP * 9dfe66a18SJeeja KP * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10dfe66a18SJeeja KP */ 11dfe66a18SJeeja KP 12dfe66a18SJeeja KP #include <linux/module.h> 13dfe66a18SJeeja KP #include <linux/slab.h> 1442f2bb1cSVinod Koul #include <linux/io.h> 15dfe66a18SJeeja KP #include <sound/hdaudio_ext.h> 16dfe66a18SJeeja KP 17dfe66a18SJeeja KP MODULE_DESCRIPTION("HDA extended core"); 18dfe66a18SJeeja KP MODULE_LICENSE("GPL v2"); 19dfe66a18SJeeja KP 20dfe66a18SJeeja KP /** 21dfe66a18SJeeja KP * snd_hdac_ext_bus_init - initialize a HD-audio extended bus 22dfe66a18SJeeja KP * @ebus: the pointer to extended bus object 23dfe66a18SJeeja KP * @dev: device pointer 24dfe66a18SJeeja KP * @ops: bus verb operators 2599463b3aSVinod Koul * default ops 26dfe66a18SJeeja KP * 27dfe66a18SJeeja KP * Returns 0 if successful, or a negative error code. 28dfe66a18SJeeja KP */ 2976f56faeSRakesh Ughreja int snd_hdac_ext_bus_init(struct hdac_bus *bus, struct device *dev, 30dfe66a18SJeeja KP const struct hdac_bus_ops *ops, 31cb04ba33SRakesh Ughreja const struct hdac_ext_bus_ops *ext_ops) 32dfe66a18SJeeja KP { 33dfe66a18SJeeja KP int ret; 34dfe66a18SJeeja KP 35*19abfefdSTakashi Iwai ret = snd_hdac_bus_init(bus, dev, ops); 36dfe66a18SJeeja KP if (ret < 0) 37dfe66a18SJeeja KP return ret; 38dfe66a18SJeeja KP 39cb04ba33SRakesh Ughreja bus->ext_ops = ext_ops; 408a5b0177SAmadeusz Sławiński /* FIXME: 418a5b0177SAmadeusz Sławiński * Currently only one bus is supported, if there is device with more 428a5b0177SAmadeusz Sławiński * buses, bus->idx should be greater than 0, but there needs to be a 438a5b0177SAmadeusz Sławiński * reliable way to always assign same number. 448a5b0177SAmadeusz Sławiński */ 458a5b0177SAmadeusz Sławiński bus->idx = 0; 4676f56faeSRakesh Ughreja bus->cmd_dma_state = true; 474446085dSVinod Koul 48dfe66a18SJeeja KP return 0; 49dfe66a18SJeeja KP } 50dfe66a18SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init); 51dfe66a18SJeeja KP 52dfe66a18SJeeja KP /** 53dfe66a18SJeeja KP * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus 54dfe66a18SJeeja KP * @ebus: the pointer to extended bus object 55dfe66a18SJeeja KP */ 5676f56faeSRakesh Ughreja void snd_hdac_ext_bus_exit(struct hdac_bus *bus) 57dfe66a18SJeeja KP { 5876f56faeSRakesh Ughreja snd_hdac_bus_exit(bus); 5976f56faeSRakesh Ughreja WARN_ON(!list_empty(&bus->hlink_list)); 60dfe66a18SJeeja KP } 61dfe66a18SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit); 62dfe66a18SJeeja KP 63dfe66a18SJeeja KP static void default_release(struct device *dev) 64dfe66a18SJeeja KP { 65dfe66a18SJeeja KP snd_hdac_ext_bus_device_exit(container_of(dev, struct hdac_device, dev)); 66dfe66a18SJeeja KP } 67dfe66a18SJeeja KP 68dfe66a18SJeeja KP /** 69a512f561SVinod Koul * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device 70dfe66a18SJeeja KP * @ebus: hdac extended bus to attach to 71dfe66a18SJeeja KP * @addr: codec address 72dfe66a18SJeeja KP * 73dfe66a18SJeeja KP * Returns zero for success or a negative error code. 74dfe66a18SJeeja KP */ 756298542fSRakesh Ughreja int snd_hdac_ext_bus_device_init(struct hdac_bus *bus, int addr, 766298542fSRakesh Ughreja struct hdac_device *hdev) 77dfe66a18SJeeja KP { 78dfe66a18SJeeja KP char name[15]; 79dfe66a18SJeeja KP int ret; 80dfe66a18SJeeja KP 813787a398SRakesh Ughreja hdev->bus = bus; 82dfe66a18SJeeja KP 8376f56faeSRakesh Ughreja snprintf(name, sizeof(name), "ehdaudio%dD%d", bus->idx, addr); 84dfe66a18SJeeja KP 85dfe66a18SJeeja KP ret = snd_hdac_device_init(hdev, bus, name, addr); 86dfe66a18SJeeja KP if (ret < 0) { 87dfe66a18SJeeja KP dev_err(bus->dev, "device init failed for hdac device\n"); 88dfe66a18SJeeja KP return ret; 89dfe66a18SJeeja KP } 90dfe66a18SJeeja KP hdev->type = HDA_DEV_ASOC; 91dfe66a18SJeeja KP hdev->dev.release = default_release; 92dfe66a18SJeeja KP 93dfe66a18SJeeja KP ret = snd_hdac_device_register(hdev); 94dfe66a18SJeeja KP if (ret) { 95dfe66a18SJeeja KP dev_err(bus->dev, "failed to register hdac device\n"); 96dfe66a18SJeeja KP snd_hdac_ext_bus_device_exit(hdev); 97dfe66a18SJeeja KP return ret; 98dfe66a18SJeeja KP } 99a512f561SVinod Koul 100dfe66a18SJeeja KP return 0; 101dfe66a18SJeeja KP } 102dfe66a18SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init); 103dfe66a18SJeeja KP 104dfe66a18SJeeja KP /** 105dfe66a18SJeeja KP * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device 106dfe66a18SJeeja KP * @hdev: hdac device to clean up 107dfe66a18SJeeja KP */ 108dfe66a18SJeeja KP void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev) 109dfe66a18SJeeja KP { 110dfe66a18SJeeja KP snd_hdac_device_exit(hdev); 111dfe66a18SJeeja KP } 112dfe66a18SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit); 113ee2d51b3SVinod Koul 114ee2d51b3SVinod Koul /** 115ee2d51b3SVinod Koul * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices 116ee2d51b3SVinod Koul * 117ee2d51b3SVinod Koul * @ebus: HD-audio extended bus 118ee2d51b3SVinod Koul */ 11976f56faeSRakesh Ughreja void snd_hdac_ext_bus_device_remove(struct hdac_bus *bus) 120ee2d51b3SVinod Koul { 121ee2d51b3SVinod Koul struct hdac_device *codec, *__codec; 122ee2d51b3SVinod Koul /* 123ee2d51b3SVinod Koul * we need to remove all the codec devices objects created in the 124ee2d51b3SVinod Koul * snd_hdac_ext_bus_device_init 125ee2d51b3SVinod Koul */ 12676f56faeSRakesh Ughreja list_for_each_entry_safe(codec, __codec, &bus->codec_list, list) { 127ee2d51b3SVinod Koul snd_hdac_device_unregister(codec); 128ee2d51b3SVinod Koul put_device(&codec->dev); 129ee2d51b3SVinod Koul } 130ee2d51b3SVinod Koul } 131ee2d51b3SVinod Koul EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove); 132d51783c1SVinod Koul #define dev_to_hdac(dev) (container_of((dev), \ 133d51783c1SVinod Koul struct hdac_device, dev)) 134d51783c1SVinod Koul 135e1df9317SRakesh Ughreja static inline struct hdac_driver *get_hdrv(struct device *dev) 136d51783c1SVinod Koul { 137d51783c1SVinod Koul struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver); 138e1df9317SRakesh Ughreja return hdrv; 139d51783c1SVinod Koul } 140d51783c1SVinod Koul 1413787a398SRakesh Ughreja static inline struct hdac_device *get_hdev(struct device *dev) 142d51783c1SVinod Koul { 143d51783c1SVinod Koul struct hdac_device *hdev = dev_to_hdac_dev(dev); 1443787a398SRakesh Ughreja return hdev; 145d51783c1SVinod Koul } 146d51783c1SVinod Koul 147d51783c1SVinod Koul static int hda_ext_drv_probe(struct device *dev) 148d51783c1SVinod Koul { 149e1df9317SRakesh Ughreja return (get_hdrv(dev))->probe(get_hdev(dev)); 150d51783c1SVinod Koul } 151d51783c1SVinod Koul 152d51783c1SVinod Koul static int hdac_ext_drv_remove(struct device *dev) 153d51783c1SVinod Koul { 154e1df9317SRakesh Ughreja return (get_hdrv(dev))->remove(get_hdev(dev)); 155d51783c1SVinod Koul } 156d51783c1SVinod Koul 157d51783c1SVinod Koul static void hdac_ext_drv_shutdown(struct device *dev) 158d51783c1SVinod Koul { 159e1df9317SRakesh Ughreja return (get_hdrv(dev))->shutdown(get_hdev(dev)); 160d51783c1SVinod Koul } 161d51783c1SVinod Koul 162d51783c1SVinod Koul /** 163d51783c1SVinod Koul * snd_hda_ext_driver_register - register a driver for ext hda devices 164d51783c1SVinod Koul * 165d51783c1SVinod Koul * @drv: ext hda driver structure 166d51783c1SVinod Koul */ 167e1df9317SRakesh Ughreja int snd_hda_ext_driver_register(struct hdac_driver *drv) 168d51783c1SVinod Koul { 169e1df9317SRakesh Ughreja drv->type = HDA_DEV_ASOC; 170e1df9317SRakesh Ughreja drv->driver.bus = &snd_hda_bus_type; 171d51783c1SVinod Koul /* we use default match */ 172d51783c1SVinod Koul 173d51783c1SVinod Koul if (drv->probe) 174e1df9317SRakesh Ughreja drv->driver.probe = hda_ext_drv_probe; 175d51783c1SVinod Koul if (drv->remove) 176e1df9317SRakesh Ughreja drv->driver.remove = hdac_ext_drv_remove; 177d51783c1SVinod Koul if (drv->shutdown) 178e1df9317SRakesh Ughreja drv->driver.shutdown = hdac_ext_drv_shutdown; 179d51783c1SVinod Koul 180e1df9317SRakesh Ughreja return driver_register(&drv->driver); 181d51783c1SVinod Koul } 182d51783c1SVinod Koul EXPORT_SYMBOL_GPL(snd_hda_ext_driver_register); 183d51783c1SVinod Koul 184d51783c1SVinod Koul /** 185d51783c1SVinod Koul * snd_hda_ext_driver_unregister - unregister a driver for ext hda devices 186d51783c1SVinod Koul * 187d51783c1SVinod Koul * @drv: ext hda driver structure 188d51783c1SVinod Koul */ 189e1df9317SRakesh Ughreja void snd_hda_ext_driver_unregister(struct hdac_driver *drv) 190d51783c1SVinod Koul { 191e1df9317SRakesh Ughreja driver_unregister(&drv->driver); 192d51783c1SVinod Koul } 193d51783c1SVinod Koul EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister); 194