Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Implementation of primary ALSA driver code base for NVIDIA Tegra HDA. |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/clk.h> |
| 8 | #include <linux/clocksource.h> |
| 9 | #include <linux/completion.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/dma-mapping.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/of_device.h> |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/time.h> |
Sameer Pujar | c94800a | 2018-11-29 09:28:52 +0530 | [diff] [blame] | 22 | #include <linux/string.h> |
Sameer Pujar | 3f7e94e | 2019-01-22 13:03:16 +0530 | [diff] [blame] | 23 | #include <linux/pm_runtime.h> |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 24 | |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/initval.h> |
| 27 | |
Pierre-Louis Bossart | be57bff | 2018-08-22 15:24:57 -0500 | [diff] [blame] | 28 | #include <sound/hda_codec.h> |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 29 | #include "hda_controller.h" |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 30 | |
| 31 | /* Defines for Nvidia Tegra HDA support */ |
| 32 | #define HDA_BAR0 0x8000 |
| 33 | |
| 34 | #define HDA_CFG_CMD 0x1004 |
| 35 | #define HDA_CFG_BAR0 0x1010 |
| 36 | |
| 37 | #define HDA_ENABLE_IO_SPACE (1 << 0) |
| 38 | #define HDA_ENABLE_MEM_SPACE (1 << 1) |
| 39 | #define HDA_ENABLE_BUS_MASTER (1 << 2) |
| 40 | #define HDA_ENABLE_SERR (1 << 8) |
| 41 | #define HDA_DISABLE_INTR (1 << 10) |
| 42 | #define HDA_BAR0_INIT_PROGRAM 0xFFFFFFFF |
| 43 | #define HDA_BAR0_FINAL_PROGRAM (1 << 14) |
| 44 | |
| 45 | /* IPFS */ |
| 46 | #define HDA_IPFS_CONFIG 0x180 |
| 47 | #define HDA_IPFS_EN_FPCI 0x1 |
| 48 | |
| 49 | #define HDA_IPFS_FPCI_BAR0 0x80 |
| 50 | #define HDA_FPCI_BAR0_START 0x40 |
| 51 | |
| 52 | #define HDA_IPFS_INTR_MASK 0x188 |
| 53 | #define HDA_IPFS_EN_INTR (1 << 16) |
| 54 | |
Sameer Pujar | bb9b02a | 2020-05-04 13:46:14 +0530 | [diff] [blame] | 55 | /* FPCI */ |
| 56 | #define FPCI_DBG_CFG_2 0x10F4 |
| 57 | #define FPCI_GCAP_NSDO_SHIFT 18 |
| 58 | #define FPCI_GCAP_NSDO_MASK (0x3 << FPCI_GCAP_NSDO_SHIFT) |
| 59 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 60 | /* max number of SDs */ |
| 61 | #define NUM_CAPTURE_SD 1 |
| 62 | #define NUM_PLAYBACK_SD 1 |
| 63 | |
Sameer Pujar | bb9b02a | 2020-05-04 13:46:14 +0530 | [diff] [blame] | 64 | /* |
| 65 | * Tegra194 does not reflect correct number of SDO lines. Below macro |
| 66 | * is used to update the GCAP register to workaround the issue. |
| 67 | */ |
| 68 | #define TEGRA194_NUM_SDO_LINES 4 |
| 69 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 70 | struct hda_tegra { |
| 71 | struct azx chip; |
| 72 | struct device *dev; |
| 73 | struct clk *hda_clk; |
| 74 | struct clk *hda2codec_2x_clk; |
| 75 | struct clk *hda2hdmi_clk; |
| 76 | void __iomem *regs; |
Takashi Iwai | 8351044 | 2015-09-24 11:00:18 +0200 | [diff] [blame] | 77 | struct work_struct probe_work; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Arnd Bergmann | 16c2395 | 2014-05-26 21:15:20 +0200 | [diff] [blame] | 80 | #ifdef CONFIG_PM |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 81 | static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT; |
| 82 | module_param(power_save, bint, 0644); |
| 83 | MODULE_PARM_DESC(power_save, |
| 84 | "Automatic power-saving timeout (in seconds, 0 = disable)."); |
Arnd Bergmann | 16c2395 | 2014-05-26 21:15:20 +0200 | [diff] [blame] | 85 | #else |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 86 | #define power_save 0 |
Arnd Bergmann | 16c2395 | 2014-05-26 21:15:20 +0200 | [diff] [blame] | 87 | #endif |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 88 | |
Takashi Iwai | 193c7e1 | 2018-08-08 17:12:58 +0200 | [diff] [blame] | 89 | static const struct hda_controller_ops hda_tegra_ops; /* nothing special */ |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 90 | |
| 91 | static void hda_tegra_init(struct hda_tegra *hda) |
| 92 | { |
| 93 | u32 v; |
| 94 | |
| 95 | /* Enable PCI access */ |
| 96 | v = readl(hda->regs + HDA_IPFS_CONFIG); |
| 97 | v |= HDA_IPFS_EN_FPCI; |
| 98 | writel(v, hda->regs + HDA_IPFS_CONFIG); |
| 99 | |
| 100 | /* Enable MEM/IO space and bus master */ |
| 101 | v = readl(hda->regs + HDA_CFG_CMD); |
| 102 | v &= ~HDA_DISABLE_INTR; |
| 103 | v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE | |
| 104 | HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR; |
| 105 | writel(v, hda->regs + HDA_CFG_CMD); |
| 106 | |
| 107 | writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0); |
| 108 | writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0); |
| 109 | writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0); |
| 110 | |
| 111 | v = readl(hda->regs + HDA_IPFS_INTR_MASK); |
| 112 | v |= HDA_IPFS_EN_INTR; |
| 113 | writel(v, hda->regs + HDA_IPFS_INTR_MASK); |
| 114 | } |
| 115 | |
| 116 | static int hda_tegra_enable_clocks(struct hda_tegra *data) |
| 117 | { |
| 118 | int rc; |
| 119 | |
| 120 | rc = clk_prepare_enable(data->hda_clk); |
| 121 | if (rc) |
| 122 | return rc; |
| 123 | rc = clk_prepare_enable(data->hda2codec_2x_clk); |
| 124 | if (rc) |
| 125 | goto disable_hda; |
| 126 | rc = clk_prepare_enable(data->hda2hdmi_clk); |
| 127 | if (rc) |
| 128 | goto disable_codec_2x; |
| 129 | |
| 130 | return 0; |
| 131 | |
| 132 | disable_codec_2x: |
| 133 | clk_disable_unprepare(data->hda2codec_2x_clk); |
| 134 | disable_hda: |
| 135 | clk_disable_unprepare(data->hda_clk); |
| 136 | return rc; |
| 137 | } |
| 138 | |
| 139 | static void hda_tegra_disable_clocks(struct hda_tegra *data) |
| 140 | { |
| 141 | clk_disable_unprepare(data->hda2hdmi_clk); |
| 142 | clk_disable_unprepare(data->hda2codec_2x_clk); |
| 143 | clk_disable_unprepare(data->hda_clk); |
| 144 | } |
| 145 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 146 | /* |
| 147 | * power management |
| 148 | */ |
Arnd Bergmann | 7472946 | 2019-03-04 21:33:25 +0100 | [diff] [blame] | 149 | static int __maybe_unused hda_tegra_suspend(struct device *dev) |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 150 | { |
| 151 | struct snd_card *card = dev_get_drvdata(dev); |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 152 | int rc; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 153 | |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 154 | rc = pm_runtime_force_suspend(dev); |
| 155 | if (rc < 0) |
| 156 | return rc; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 157 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 158 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
Arnd Bergmann | 7472946 | 2019-03-04 21:33:25 +0100 | [diff] [blame] | 162 | static int __maybe_unused hda_tegra_resume(struct device *dev) |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 163 | { |
| 164 | struct snd_card *card = dev_get_drvdata(dev); |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 165 | int rc; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 166 | |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 167 | rc = pm_runtime_force_resume(dev); |
| 168 | if (rc < 0) |
| 169 | return rc; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 170 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); |
| 171 | |
| 172 | return 0; |
| 173 | } |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 174 | |
Arnd Bergmann | 7472946 | 2019-03-04 21:33:25 +0100 | [diff] [blame] | 175 | static int __maybe_unused hda_tegra_runtime_suspend(struct device *dev) |
Sameer Pujar | f2974aa | 2019-01-22 13:03:18 +0530 | [diff] [blame] | 176 | { |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 177 | struct snd_card *card = dev_get_drvdata(dev); |
| 178 | struct azx *chip = card->private_data; |
| 179 | struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip); |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 180 | |
| 181 | if (chip && chip->running) { |
Mohan Kumar | 23d63a3 | 2020-08-25 10:54:15 +0530 | [diff] [blame] | 182 | /* enable controller wake up event */ |
| 183 | azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) | |
| 184 | STATESTS_INT_MASK); |
| 185 | |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 186 | azx_stop_chip(chip); |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 187 | azx_enter_link_reset(chip); |
| 188 | } |
| 189 | hda_tegra_disable_clocks(hda); |
| 190 | |
Sameer Pujar | f2974aa | 2019-01-22 13:03:18 +0530 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
Arnd Bergmann | 7472946 | 2019-03-04 21:33:25 +0100 | [diff] [blame] | 194 | static int __maybe_unused hda_tegra_runtime_resume(struct device *dev) |
Sameer Pujar | f2974aa | 2019-01-22 13:03:18 +0530 | [diff] [blame] | 195 | { |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 196 | struct snd_card *card = dev_get_drvdata(dev); |
| 197 | struct azx *chip = card->private_data; |
| 198 | struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip); |
| 199 | int rc; |
| 200 | |
| 201 | rc = hda_tegra_enable_clocks(hda); |
| 202 | if (rc != 0) |
| 203 | return rc; |
| 204 | if (chip && chip->running) { |
| 205 | hda_tegra_init(hda); |
| 206 | azx_init_chip(chip, 1); |
Mohan Kumar | 23d63a3 | 2020-08-25 10:54:15 +0530 | [diff] [blame] | 207 | /* disable controller wake up event*/ |
| 208 | azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) & |
| 209 | ~STATESTS_INT_MASK); |
Sameer Pujar | 707e075 | 2019-01-22 13:03:20 +0530 | [diff] [blame] | 210 | } |
| 211 | |
Sameer Pujar | f2974aa | 2019-01-22 13:03:18 +0530 | [diff] [blame] | 212 | return 0; |
| 213 | } |
Sameer Pujar | f2974aa | 2019-01-22 13:03:18 +0530 | [diff] [blame] | 214 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 215 | static const struct dev_pm_ops hda_tegra_pm = { |
| 216 | SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume) |
Sameer Pujar | f2974aa | 2019-01-22 13:03:18 +0530 | [diff] [blame] | 217 | SET_RUNTIME_PM_OPS(hda_tegra_runtime_suspend, |
| 218 | hda_tegra_runtime_resume, |
| 219 | NULL) |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 222 | static int hda_tegra_dev_disconnect(struct snd_device *device) |
| 223 | { |
| 224 | struct azx *chip = device->device_data; |
| 225 | |
| 226 | chip->bus.shutdown = 1; |
| 227 | return 0; |
| 228 | } |
| 229 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 230 | /* |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 231 | * destructor |
| 232 | */ |
| 233 | static int hda_tegra_dev_free(struct snd_device *device) |
| 234 | { |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 235 | struct azx *chip = device->device_data; |
Takashi Iwai | 8351044 | 2015-09-24 11:00:18 +0200 | [diff] [blame] | 236 | struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 237 | |
Takashi Iwai | 8351044 | 2015-09-24 11:00:18 +0200 | [diff] [blame] | 238 | cancel_work_sync(&hda->probe_work); |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 239 | if (azx_bus(chip)->chip_init) { |
Takashi Iwai | 7833c3f | 2015-04-14 18:13:13 +0200 | [diff] [blame] | 240 | azx_stop_all_streams(chip); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 241 | azx_stop_chip(chip); |
| 242 | } |
| 243 | |
| 244 | azx_free_stream_pages(chip); |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 245 | azx_free_streams(chip); |
Takashi Iwai | 4cfe99c | 2015-04-16 12:02:30 +0200 | [diff] [blame] | 246 | snd_hdac_bus_exit(azx_bus(chip)); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev) |
| 252 | { |
| 253 | struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip); |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 254 | struct hdac_bus *bus = azx_bus(chip); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 255 | struct device *dev = hda->dev; |
| 256 | struct resource *res; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 257 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 258 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 259 | hda->regs = devm_ioremap_resource(dev, res); |
Eliot Blennerhassett | 93ceaa3 | 2015-02-14 15:32:24 +1300 | [diff] [blame] | 260 | if (IS_ERR(hda->regs)) |
| 261 | return PTR_ERR(hda->regs); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 262 | |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 263 | bus->remap_addr = hda->regs + HDA_BAR0; |
| 264 | bus->addr = res->start + HDA_BAR0; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 265 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 266 | hda_tegra_init(hda); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Sameer Pujar | 65af212 | 2019-01-22 13:03:17 +0530 | [diff] [blame] | 271 | static int hda_tegra_init_clk(struct hda_tegra *hda) |
| 272 | { |
| 273 | struct device *dev = hda->dev; |
| 274 | |
| 275 | hda->hda_clk = devm_clk_get(dev, "hda"); |
| 276 | if (IS_ERR(hda->hda_clk)) { |
| 277 | dev_err(dev, "failed to get hda clock\n"); |
| 278 | return PTR_ERR(hda->hda_clk); |
| 279 | } |
| 280 | hda->hda2codec_2x_clk = devm_clk_get(dev, "hda2codec_2x"); |
| 281 | if (IS_ERR(hda->hda2codec_2x_clk)) { |
| 282 | dev_err(dev, "failed to get hda2codec_2x clock\n"); |
| 283 | return PTR_ERR(hda->hda2codec_2x_clk); |
| 284 | } |
| 285 | hda->hda2hdmi_clk = devm_clk_get(dev, "hda2hdmi"); |
| 286 | if (IS_ERR(hda->hda2hdmi_clk)) { |
| 287 | dev_err(dev, "failed to get hda2hdmi clock\n"); |
| 288 | return PTR_ERR(hda->hda2hdmi_clk); |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 294 | static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev) |
| 295 | { |
Sameer Pujar | bb9b02a | 2020-05-04 13:46:14 +0530 | [diff] [blame] | 296 | struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip); |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 297 | struct hdac_bus *bus = azx_bus(chip); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 298 | struct snd_card *card = chip->card; |
| 299 | int err; |
| 300 | unsigned short gcap; |
| 301 | int irq_id = platform_get_irq(pdev, 0); |
Sameer Pujar | c0bde00 | 2019-02-20 20:43:24 +0530 | [diff] [blame] | 302 | const char *sname, *drv_name = "tegra-hda"; |
| 303 | struct device_node *np = pdev->dev.of_node; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 304 | |
| 305 | err = hda_tegra_init_chip(chip, pdev); |
| 306 | if (err) |
| 307 | return err; |
| 308 | |
| 309 | err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt, |
| 310 | IRQF_SHARED, KBUILD_MODNAME, chip); |
| 311 | if (err) { |
| 312 | dev_err(chip->card->dev, |
| 313 | "unable to request IRQ %d, disabling device\n", |
| 314 | irq_id); |
| 315 | return err; |
| 316 | } |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 317 | bus->irq = irq_id; |
Mohan Kumar | ed4d0a4a | 2020-08-05 15:22:21 +0530 | [diff] [blame] | 318 | bus->dma_stop_delay = 100; |
Takashi Iwai | f36da94 | 2019-12-10 07:34:20 +0100 | [diff] [blame] | 319 | card->sync_irq = bus->irq; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 320 | |
Sameer Pujar | bb9b02a | 2020-05-04 13:46:14 +0530 | [diff] [blame] | 321 | /* |
| 322 | * Tegra194 has 4 SDO lines and the STRIPE can be used to |
| 323 | * indicate how many of the SDO lines the stream should be |
| 324 | * striped. But GCAP register does not reflect the true |
| 325 | * capability of HW. Below workaround helps to fix this. |
| 326 | * |
| 327 | * GCAP_NSDO is bits 19:18 in T_AZA_DBG_CFG_2, |
| 328 | * 0 for 1 SDO, 1 for 2 SDO, 2 for 4 SDO lines. |
| 329 | */ |
| 330 | if (of_device_is_compatible(np, "nvidia,tegra194-hda")) { |
| 331 | u32 val; |
| 332 | |
| 333 | dev_info(card->dev, "Override SDO lines to %u\n", |
| 334 | TEGRA194_NUM_SDO_LINES); |
| 335 | |
| 336 | val = readl(hda->regs + FPCI_DBG_CFG_2) & ~FPCI_GCAP_NSDO_MASK; |
| 337 | val |= (TEGRA194_NUM_SDO_LINES >> 1) << FPCI_GCAP_NSDO_SHIFT; |
| 338 | writel(val, hda->regs + FPCI_DBG_CFG_2); |
| 339 | } |
| 340 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 341 | gcap = azx_readw(chip, GCAP); |
| 342 | dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap); |
| 343 | |
Mohan Kumar | 6c17e9d | 2020-08-05 15:22:19 +0530 | [diff] [blame] | 344 | chip->align_buffer_size = 1; |
| 345 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 346 | /* read number of streams from GCAP register instead of using |
| 347 | * hardcoded value |
| 348 | */ |
| 349 | chip->capture_streams = (gcap >> 8) & 0x0f; |
| 350 | chip->playback_streams = (gcap >> 12) & 0x0f; |
| 351 | if (!chip->playback_streams && !chip->capture_streams) { |
| 352 | /* gcap didn't give any info, switching to old method */ |
| 353 | chip->playback_streams = NUM_PLAYBACK_SD; |
| 354 | chip->capture_streams = NUM_CAPTURE_SD; |
| 355 | } |
| 356 | chip->capture_index_offset = 0; |
| 357 | chip->playback_index_offset = chip->capture_streams; |
| 358 | chip->num_streams = chip->playback_streams + chip->capture_streams; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 359 | |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 360 | /* initialize streams */ |
| 361 | err = azx_init_streams(chip); |
Thierry Reding | 6a464a4 | 2015-05-05 14:56:21 +0200 | [diff] [blame] | 362 | if (err < 0) { |
| 363 | dev_err(card->dev, "failed to initialize streams: %d\n", err); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 364 | return err; |
Thierry Reding | 6a464a4 | 2015-05-05 14:56:21 +0200 | [diff] [blame] | 365 | } |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 366 | |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 367 | err = azx_alloc_stream_pages(chip); |
Thierry Reding | 6a464a4 | 2015-05-05 14:56:21 +0200 | [diff] [blame] | 368 | if (err < 0) { |
| 369 | dev_err(card->dev, "failed to allocate stream pages: %d\n", |
| 370 | err); |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 371 | return err; |
Thierry Reding | 6a464a4 | 2015-05-05 14:56:21 +0200 | [diff] [blame] | 372 | } |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 373 | |
| 374 | /* initialize chip */ |
| 375 | azx_init_chip(chip, 1); |
| 376 | |
Sameer Pujar | 60019d8 | 2020-05-04 13:46:16 +0530 | [diff] [blame] | 377 | /* |
| 378 | * Playback (for 44.1K/48K, 2-channel, 16-bps) fails with |
| 379 | * 4 SDO lines due to legacy design limitation. Following |
| 380 | * is, from HD Audio Specification (Revision 1.0a), used to |
| 381 | * control striping of the stream across multiple SDO lines |
| 382 | * for sample rates <= 48K. |
| 383 | * |
| 384 | * { ((num_channels * bits_per_sample) / number of SDOs) >= 8 } |
| 385 | * |
| 386 | * Due to legacy design issue it is recommended that above |
| 387 | * ratio must be greater than 8. Since number of SDO lines is |
| 388 | * in powers of 2, next available ratio is 16 which can be |
| 389 | * used as a limiting factor here. |
| 390 | */ |
| 391 | if (of_device_is_compatible(np, "nvidia,tegra194-hda")) |
| 392 | chip->bus.core.sdo_limit = 16; |
| 393 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 394 | /* codec detection */ |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 395 | if (!bus->codec_mask) { |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 396 | dev_err(card->dev, "no codecs found!\n"); |
| 397 | return -ENODEV; |
| 398 | } |
| 399 | |
Sameer Pujar | c94800a | 2018-11-29 09:28:52 +0530 | [diff] [blame] | 400 | /* driver name */ |
Sameer Pujar | c0bde00 | 2019-02-20 20:43:24 +0530 | [diff] [blame] | 401 | strncpy(card->driver, drv_name, sizeof(card->driver)); |
Sameer Pujar | c94800a | 2018-11-29 09:28:52 +0530 | [diff] [blame] | 402 | /* shortname for card */ |
Sameer Pujar | c0bde00 | 2019-02-20 20:43:24 +0530 | [diff] [blame] | 403 | sname = of_get_property(np, "nvidia,model", NULL); |
| 404 | if (!sname) |
| 405 | sname = drv_name; |
Sameer Pujar | c94800a | 2018-11-29 09:28:52 +0530 | [diff] [blame] | 406 | if (strlen(sname) > sizeof(card->shortname)) |
| 407 | dev_info(card->dev, "truncating shortname for card\n"); |
| 408 | strncpy(card->shortname, sname, sizeof(card->shortname)); |
| 409 | |
| 410 | /* longname for card */ |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 411 | snprintf(card->longname, sizeof(card->longname), |
| 412 | "%s at 0x%lx irq %i", |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 413 | card->shortname, bus->addr, bus->irq); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * constructor |
| 420 | */ |
Takashi Iwai | 8351044 | 2015-09-24 11:00:18 +0200 | [diff] [blame] | 421 | |
| 422 | static void hda_tegra_probe_work(struct work_struct *work); |
| 423 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 424 | static int hda_tegra_create(struct snd_card *card, |
| 425 | unsigned int driver_caps, |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 426 | struct hda_tegra *hda) |
| 427 | { |
Takashi Iwai | 41f394a | 2020-01-03 09:16:24 +0100 | [diff] [blame] | 428 | static const struct snd_device_ops ops = { |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 429 | .dev_disconnect = hda_tegra_dev_disconnect, |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 430 | .dev_free = hda_tegra_dev_free, |
| 431 | }; |
| 432 | struct azx *chip; |
| 433 | int err; |
| 434 | |
| 435 | chip = &hda->chip; |
| 436 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 437 | mutex_init(&chip->open_mutex); |
| 438 | chip->card = card; |
Takashi Iwai | a43ff5b | 2015-04-14 17:26:00 +0200 | [diff] [blame] | 439 | chip->ops = &hda_tegra_ops; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 440 | chip->driver_caps = driver_caps; |
| 441 | chip->driver_type = driver_caps & 0xff; |
| 442 | chip->dev_index = 0; |
| 443 | INIT_LIST_HEAD(&chip->pcm_list); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 444 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 445 | chip->codec_probe_mask = -1; |
| 446 | |
| 447 | chip->single_cmd = false; |
| 448 | chip->snoop = true; |
| 449 | |
Takashi Iwai | 8351044 | 2015-09-24 11:00:18 +0200 | [diff] [blame] | 450 | INIT_WORK(&hda->probe_work, hda_tegra_probe_work); |
| 451 | |
Takashi Iwai | 19abfef | 2019-08-07 20:32:08 +0200 | [diff] [blame] | 452 | err = azx_bus_init(chip, NULL); |
Thierry Reding | 3b90f40 | 2015-05-05 14:45:57 +0200 | [diff] [blame] | 453 | if (err < 0) |
| 454 | return err; |
| 455 | |
Jon Hunter | ee85a36 | 2020-07-14 17:08:41 +0100 | [diff] [blame] | 456 | chip->bus.core.sync_write = 0; |
Takashi Iwai | 5f2cb36 | 2019-12-12 20:11:01 +0100 | [diff] [blame] | 457 | chip->bus.core.needs_damn_long_delay = 1; |
Takashi Iwai | 4d024fe | 2020-01-20 11:41:27 +0100 | [diff] [blame] | 458 | chip->bus.core.aligned_mmio = 1; |
Takashi Iwai | 7d9a180 | 2015-12-17 08:23:39 +0100 | [diff] [blame] | 459 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 460 | err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); |
| 461 | if (err < 0) { |
| 462 | dev_err(card->dev, "Error creating device\n"); |
| 463 | return err; |
| 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static const struct of_device_id hda_tegra_match[] = { |
| 470 | { .compatible = "nvidia,tegra30-hda" }, |
Sameer Pujar | bb9b02a | 2020-05-04 13:46:14 +0530 | [diff] [blame] | 471 | { .compatible = "nvidia,tegra194-hda" }, |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 472 | {}, |
| 473 | }; |
Dylan Reid | f73387c | 2014-05-20 11:26:12 -0700 | [diff] [blame] | 474 | MODULE_DEVICE_TABLE(of, hda_tegra_match); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 475 | |
| 476 | static int hda_tegra_probe(struct platform_device *pdev) |
| 477 | { |
Sameer Pujar | 9935d55 | 2019-01-22 13:03:21 +0530 | [diff] [blame] | 478 | const unsigned int driver_flags = AZX_DCAPS_CORBRP_SELF_CLEAR | |
| 479 | AZX_DCAPS_PM_RUNTIME; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 480 | struct snd_card *card; |
| 481 | struct azx *chip; |
| 482 | struct hda_tegra *hda; |
| 483 | int err; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 484 | |
| 485 | hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL); |
| 486 | if (!hda) |
| 487 | return -ENOMEM; |
| 488 | hda->dev = &pdev->dev; |
| 489 | chip = &hda->chip; |
| 490 | |
| 491 | err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, |
| 492 | THIS_MODULE, 0, &card); |
| 493 | if (err < 0) { |
| 494 | dev_err(&pdev->dev, "Error creating card!\n"); |
| 495 | return err; |
| 496 | } |
| 497 | |
Sameer Pujar | 65af212 | 2019-01-22 13:03:17 +0530 | [diff] [blame] | 498 | err = hda_tegra_init_clk(hda); |
| 499 | if (err < 0) |
| 500 | goto out_free; |
| 501 | |
Takashi Iwai | a43ff5b | 2015-04-14 17:26:00 +0200 | [diff] [blame] | 502 | err = hda_tegra_create(card, driver_flags, hda); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 503 | if (err < 0) |
| 504 | goto out_free; |
| 505 | card->private_data = chip; |
| 506 | |
| 507 | dev_set_drvdata(&pdev->dev, card); |
Sameer Pujar | 3f7e94e | 2019-01-22 13:03:16 +0530 | [diff] [blame] | 508 | |
| 509 | pm_runtime_enable(hda->dev); |
| 510 | if (!azx_has_pm_runtime(chip)) |
| 511 | pm_runtime_forbid(hda->dev); |
| 512 | |
Takashi Iwai | 8351044 | 2015-09-24 11:00:18 +0200 | [diff] [blame] | 513 | schedule_work(&hda->probe_work); |
| 514 | |
| 515 | return 0; |
| 516 | |
| 517 | out_free: |
| 518 | snd_card_free(card); |
| 519 | return err; |
| 520 | } |
| 521 | |
| 522 | static void hda_tegra_probe_work(struct work_struct *work) |
| 523 | { |
| 524 | struct hda_tegra *hda = container_of(work, struct hda_tegra, probe_work); |
| 525 | struct azx *chip = &hda->chip; |
| 526 | struct platform_device *pdev = to_platform_device(hda->dev); |
| 527 | int err; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 528 | |
Sameer Pujar | 3f7e94e | 2019-01-22 13:03:16 +0530 | [diff] [blame] | 529 | pm_runtime_get_sync(hda->dev); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 530 | err = hda_tegra_first_init(chip, pdev); |
| 531 | if (err < 0) |
| 532 | goto out_free; |
| 533 | |
| 534 | /* create codec instances */ |
Thierry Reding | 350355e | 2018-12-03 16:53:16 +0100 | [diff] [blame] | 535 | err = azx_probe_codecs(chip, 8); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 536 | if (err < 0) |
| 537 | goto out_free; |
| 538 | |
| 539 | err = azx_codec_configure(chip); |
| 540 | if (err < 0) |
| 541 | goto out_free; |
| 542 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 543 | err = snd_card_register(chip->card); |
| 544 | if (err < 0) |
| 545 | goto out_free; |
| 546 | |
| 547 | chip->running = 1; |
Takashi Iwai | a41d122 | 2015-04-14 22:13:18 +0200 | [diff] [blame] | 548 | snd_hda_set_power_save(&chip->bus, power_save * 1000); |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 549 | |
Takashi Iwai | 8351044 | 2015-09-24 11:00:18 +0200 | [diff] [blame] | 550 | out_free: |
Sameer Pujar | 3f7e94e | 2019-01-22 13:03:16 +0530 | [diff] [blame] | 551 | pm_runtime_put(hda->dev); |
Takashi Iwai | 8351044 | 2015-09-24 11:00:18 +0200 | [diff] [blame] | 552 | return; /* no error return from async probe */ |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static int hda_tegra_remove(struct platform_device *pdev) |
| 556 | { |
Sameer Pujar | 3f7e94e | 2019-01-22 13:03:16 +0530 | [diff] [blame] | 557 | int ret; |
| 558 | |
| 559 | ret = snd_card_free(dev_get_drvdata(&pdev->dev)); |
| 560 | pm_runtime_disable(&pdev->dev); |
| 561 | |
| 562 | return ret; |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Takashi Iwai | b2a0baf | 2015-03-05 17:21:32 +0100 | [diff] [blame] | 565 | static void hda_tegra_shutdown(struct platform_device *pdev) |
| 566 | { |
| 567 | struct snd_card *card = dev_get_drvdata(&pdev->dev); |
| 568 | struct azx *chip; |
| 569 | |
| 570 | if (!card) |
| 571 | return; |
| 572 | chip = card->private_data; |
| 573 | if (chip && chip->running) |
| 574 | azx_stop_chip(chip); |
| 575 | } |
| 576 | |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 577 | static struct platform_driver tegra_platform_hda = { |
| 578 | .driver = { |
| 579 | .name = "tegra-hda", |
| 580 | .pm = &hda_tegra_pm, |
| 581 | .of_match_table = hda_tegra_match, |
| 582 | }, |
| 583 | .probe = hda_tegra_probe, |
| 584 | .remove = hda_tegra_remove, |
Takashi Iwai | b2a0baf | 2015-03-05 17:21:32 +0100 | [diff] [blame] | 585 | .shutdown = hda_tegra_shutdown, |
Dylan Reid | 3c320f3 | 2014-05-19 19:18:27 -0700 | [diff] [blame] | 586 | }; |
| 587 | module_platform_driver(tegra_platform_hda); |
| 588 | |
| 589 | MODULE_DESCRIPTION("Tegra HDA bus driver"); |
| 590 | MODULE_LICENSE("GPL v2"); |