blob: c8d18dc4da2ad5467e954fc10590df64fb8fc507 [file] [log] [blame]
Dylan Reid3c320f32014-05-19 19:18:27 -07001/*
2 *
3 * Implementation of primary ALSA driver code base for NVIDIA Tegra HDA.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19#include <linux/clk.h>
20#include <linux/clocksource.h>
21#include <linux/completion.h>
22#include <linux/delay.h>
23#include <linux/dma-mapping.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/mutex.h>
31#include <linux/of_device.h>
Dylan Reid3c320f32014-05-19 19:18:27 -070032#include <linux/slab.h>
33#include <linux/time.h>
Sameer Pujarc94800a2018-11-29 09:28:52 +053034#include <linux/string.h>
Sameer Pujar3f7e94e2019-01-22 13:03:16 +053035#include <linux/pm_runtime.h>
Dylan Reid3c320f32014-05-19 19:18:27 -070036
37#include <sound/core.h>
38#include <sound/initval.h>
39
Pierre-Louis Bossartbe57bff2018-08-22 15:24:57 -050040#include <sound/hda_codec.h>
Dylan Reid3c320f32014-05-19 19:18:27 -070041#include "hda_controller.h"
Dylan Reid3c320f32014-05-19 19:18:27 -070042
43/* Defines for Nvidia Tegra HDA support */
44#define HDA_BAR0 0x8000
45
46#define HDA_CFG_CMD 0x1004
47#define HDA_CFG_BAR0 0x1010
48
49#define HDA_ENABLE_IO_SPACE (1 << 0)
50#define HDA_ENABLE_MEM_SPACE (1 << 1)
51#define HDA_ENABLE_BUS_MASTER (1 << 2)
52#define HDA_ENABLE_SERR (1 << 8)
53#define HDA_DISABLE_INTR (1 << 10)
54#define HDA_BAR0_INIT_PROGRAM 0xFFFFFFFF
55#define HDA_BAR0_FINAL_PROGRAM (1 << 14)
56
57/* IPFS */
58#define HDA_IPFS_CONFIG 0x180
59#define HDA_IPFS_EN_FPCI 0x1
60
61#define HDA_IPFS_FPCI_BAR0 0x80
62#define HDA_FPCI_BAR0_START 0x40
63
64#define HDA_IPFS_INTR_MASK 0x188
65#define HDA_IPFS_EN_INTR (1 << 16)
66
67/* max number of SDs */
68#define NUM_CAPTURE_SD 1
69#define NUM_PLAYBACK_SD 1
70
71struct hda_tegra {
72 struct azx chip;
73 struct device *dev;
74 struct clk *hda_clk;
75 struct clk *hda2codec_2x_clk;
76 struct clk *hda2hdmi_clk;
77 void __iomem *regs;
Takashi Iwai83510442015-09-24 11:00:18 +020078 struct work_struct probe_work;
Dylan Reid3c320f32014-05-19 19:18:27 -070079};
80
Arnd Bergmann16c23952014-05-26 21:15:20 +020081#ifdef CONFIG_PM
Dylan Reid3c320f32014-05-19 19:18:27 -070082static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
83module_param(power_save, bint, 0644);
84MODULE_PARM_DESC(power_save,
85 "Automatic power-saving timeout (in seconds, 0 = disable).");
Arnd Bergmann16c23952014-05-26 21:15:20 +020086#else
Takashi Iwaibb573922015-02-20 09:26:04 +010087#define power_save 0
Arnd Bergmann16c23952014-05-26 21:15:20 +020088#endif
Dylan Reid3c320f32014-05-19 19:18:27 -070089
90/*
91 * DMA page allocation ops.
92 */
Takashi Iwaia43ff5b2015-04-14 17:26:00 +020093static int dma_alloc_pages(struct hdac_bus *bus, int type, size_t size,
Dylan Reid3c320f32014-05-19 19:18:27 -070094 struct snd_dma_buffer *buf)
95{
Takashi Iwaia43ff5b2015-04-14 17:26:00 +020096 return snd_dma_alloc_pages(type, bus->dev, size, buf);
Dylan Reid3c320f32014-05-19 19:18:27 -070097}
98
Takashi Iwaia43ff5b2015-04-14 17:26:00 +020099static void dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
Dylan Reid3c320f32014-05-19 19:18:27 -0700100{
101 snd_dma_free_pages(buf);
102}
103
Dylan Reid3c320f32014-05-19 19:18:27 -0700104/*
105 * Register access ops. Tegra HDA register access is DWORD only.
106 */
Ben Dooksc9058d42016-06-21 19:18:32 +0100107static void hda_tegra_writel(u32 value, u32 __iomem *addr)
Dylan Reid3c320f32014-05-19 19:18:27 -0700108{
109 writel(value, addr);
110}
111
Ben Dooksc9058d42016-06-21 19:18:32 +0100112static u32 hda_tegra_readl(u32 __iomem *addr)
Dylan Reid3c320f32014-05-19 19:18:27 -0700113{
114 return readl(addr);
115}
116
Ben Dooksc9058d42016-06-21 19:18:32 +0100117static void hda_tegra_writew(u16 value, u16 __iomem *addr)
Dylan Reid3c320f32014-05-19 19:18:27 -0700118{
119 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
Ben Dooksc9058d42016-06-21 19:18:32 +0100120 void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
Dylan Reid3c320f32014-05-19 19:18:27 -0700121 u32 v;
122
123 v = readl(dword_addr);
124 v &= ~(0xffff << shift);
125 v |= value << shift;
126 writel(v, dword_addr);
127}
128
Ben Dooksc9058d42016-06-21 19:18:32 +0100129static u16 hda_tegra_readw(u16 __iomem *addr)
Dylan Reid3c320f32014-05-19 19:18:27 -0700130{
131 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
Ben Dooksc9058d42016-06-21 19:18:32 +0100132 void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
Dylan Reid3c320f32014-05-19 19:18:27 -0700133 u32 v;
134
135 v = readl(dword_addr);
136 return (v >> shift) & 0xffff;
137}
138
Ben Dooksc9058d42016-06-21 19:18:32 +0100139static void hda_tegra_writeb(u8 value, u8 __iomem *addr)
Dylan Reid3c320f32014-05-19 19:18:27 -0700140{
141 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
Ben Dooksc9058d42016-06-21 19:18:32 +0100142 void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
Dylan Reid3c320f32014-05-19 19:18:27 -0700143 u32 v;
144
145 v = readl(dword_addr);
146 v &= ~(0xff << shift);
147 v |= value << shift;
148 writel(v, dword_addr);
149}
150
Ben Dooksc9058d42016-06-21 19:18:32 +0100151static u8 hda_tegra_readb(u8 __iomem *addr)
Dylan Reid3c320f32014-05-19 19:18:27 -0700152{
153 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
Ben Dooksc9058d42016-06-21 19:18:32 +0100154 void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
Dylan Reid3c320f32014-05-19 19:18:27 -0700155 u32 v;
156
157 v = readl(dword_addr);
158 return (v >> shift) & 0xff;
159}
160
Takashi Iwaia43ff5b2015-04-14 17:26:00 +0200161static const struct hdac_io_ops hda_tegra_io_ops = {
Dylan Reid3c320f32014-05-19 19:18:27 -0700162 .reg_writel = hda_tegra_writel,
163 .reg_readl = hda_tegra_readl,
164 .reg_writew = hda_tegra_writew,
165 .reg_readw = hda_tegra_readw,
166 .reg_writeb = hda_tegra_writeb,
167 .reg_readb = hda_tegra_readb,
168 .dma_alloc_pages = dma_alloc_pages,
169 .dma_free_pages = dma_free_pages,
Takashi Iwaia43ff5b2015-04-14 17:26:00 +0200170};
171
Takashi Iwai193c7e12018-08-08 17:12:58 +0200172static const struct hda_controller_ops hda_tegra_ops; /* nothing special */
Dylan Reid3c320f32014-05-19 19:18:27 -0700173
174static void hda_tegra_init(struct hda_tegra *hda)
175{
176 u32 v;
177
178 /* Enable PCI access */
179 v = readl(hda->regs + HDA_IPFS_CONFIG);
180 v |= HDA_IPFS_EN_FPCI;
181 writel(v, hda->regs + HDA_IPFS_CONFIG);
182
183 /* Enable MEM/IO space and bus master */
184 v = readl(hda->regs + HDA_CFG_CMD);
185 v &= ~HDA_DISABLE_INTR;
186 v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE |
187 HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR;
188 writel(v, hda->regs + HDA_CFG_CMD);
189
190 writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0);
191 writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0);
192 writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0);
193
194 v = readl(hda->regs + HDA_IPFS_INTR_MASK);
195 v |= HDA_IPFS_EN_INTR;
196 writel(v, hda->regs + HDA_IPFS_INTR_MASK);
197}
198
199static int hda_tegra_enable_clocks(struct hda_tegra *data)
200{
201 int rc;
202
203 rc = clk_prepare_enable(data->hda_clk);
204 if (rc)
205 return rc;
206 rc = clk_prepare_enable(data->hda2codec_2x_clk);
207 if (rc)
208 goto disable_hda;
209 rc = clk_prepare_enable(data->hda2hdmi_clk);
210 if (rc)
211 goto disable_codec_2x;
212
213 return 0;
214
215disable_codec_2x:
216 clk_disable_unprepare(data->hda2codec_2x_clk);
217disable_hda:
218 clk_disable_unprepare(data->hda_clk);
219 return rc;
220}
221
Thierry Reding525549d2014-07-07 15:13:12 +0200222#ifdef CONFIG_PM_SLEEP
Dylan Reid3c320f32014-05-19 19:18:27 -0700223static void hda_tegra_disable_clocks(struct hda_tegra *data)
224{
225 clk_disable_unprepare(data->hda2hdmi_clk);
226 clk_disable_unprepare(data->hda2codec_2x_clk);
227 clk_disable_unprepare(data->hda_clk);
228}
229
Dylan Reid3c320f32014-05-19 19:18:27 -0700230/*
231 * power management
232 */
233static int hda_tegra_suspend(struct device *dev)
234{
235 struct snd_card *card = dev_get_drvdata(dev);
Sameer Pujar707e0752019-01-22 13:03:20 +0530236 int rc;
Dylan Reid3c320f32014-05-19 19:18:27 -0700237
Sameer Pujar707e0752019-01-22 13:03:20 +0530238 rc = pm_runtime_force_suspend(dev);
239 if (rc < 0)
240 return rc;
Dylan Reid3c320f32014-05-19 19:18:27 -0700241 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Dylan Reid3c320f32014-05-19 19:18:27 -0700242
Dylan Reid3c320f32014-05-19 19:18:27 -0700243 return 0;
244}
245
246static int hda_tegra_resume(struct device *dev)
247{
248 struct snd_card *card = dev_get_drvdata(dev);
Sameer Pujar707e0752019-01-22 13:03:20 +0530249 int rc;
Dylan Reid3c320f32014-05-19 19:18:27 -0700250
Sameer Pujar707e0752019-01-22 13:03:20 +0530251 rc = pm_runtime_force_resume(dev);
252 if (rc < 0)
253 return rc;
Dylan Reid3c320f32014-05-19 19:18:27 -0700254 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
255
256 return 0;
257}
258#endif /* CONFIG_PM_SLEEP */
259
Sameer Pujarf2974aa2019-01-22 13:03:18 +0530260#ifdef CONFIG_PM
261static int hda_tegra_runtime_suspend(struct device *dev)
262{
Sameer Pujar707e0752019-01-22 13:03:20 +0530263 struct snd_card *card = dev_get_drvdata(dev);
264 struct azx *chip = card->private_data;
265 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
266 struct hdac_bus *bus = azx_bus(chip);
267
268 if (chip && chip->running) {
269 azx_stop_chip(chip);
270 synchronize_irq(bus->irq);
271 azx_enter_link_reset(chip);
272 }
273 hda_tegra_disable_clocks(hda);
274
Sameer Pujarf2974aa2019-01-22 13:03:18 +0530275 return 0;
276}
277
278static int hda_tegra_runtime_resume(struct device *dev)
279{
Sameer Pujar707e0752019-01-22 13:03:20 +0530280 struct snd_card *card = dev_get_drvdata(dev);
281 struct azx *chip = card->private_data;
282 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
283 int rc;
284
285 rc = hda_tegra_enable_clocks(hda);
286 if (rc != 0)
287 return rc;
288 if (chip && chip->running) {
289 hda_tegra_init(hda);
290 azx_init_chip(chip, 1);
291 }
292
Sameer Pujarf2974aa2019-01-22 13:03:18 +0530293 return 0;
294}
295#endif /* CONFIG_PM */
296
Dylan Reid3c320f32014-05-19 19:18:27 -0700297static const struct dev_pm_ops hda_tegra_pm = {
298 SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
Sameer Pujarf2974aa2019-01-22 13:03:18 +0530299 SET_RUNTIME_PM_OPS(hda_tegra_runtime_suspend,
300 hda_tegra_runtime_resume,
301 NULL)
Dylan Reid3c320f32014-05-19 19:18:27 -0700302};
303
Takashi Iwaia41d1222015-04-14 22:13:18 +0200304static int hda_tegra_dev_disconnect(struct snd_device *device)
305{
306 struct azx *chip = device->device_data;
307
308 chip->bus.shutdown = 1;
309 return 0;
310}
311
Dylan Reid3c320f32014-05-19 19:18:27 -0700312/*
Dylan Reid3c320f32014-05-19 19:18:27 -0700313 * destructor
314 */
315static int hda_tegra_dev_free(struct snd_device *device)
316{
Dylan Reid3c320f32014-05-19 19:18:27 -0700317 struct azx *chip = device->device_data;
Takashi Iwai83510442015-09-24 11:00:18 +0200318 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
Dylan Reid3c320f32014-05-19 19:18:27 -0700319
Takashi Iwai83510442015-09-24 11:00:18 +0200320 cancel_work_sync(&hda->probe_work);
Takashi Iwaia41d1222015-04-14 22:13:18 +0200321 if (azx_bus(chip)->chip_init) {
Takashi Iwai7833c3f2015-04-14 18:13:13 +0200322 azx_stop_all_streams(chip);
Dylan Reid3c320f32014-05-19 19:18:27 -0700323 azx_stop_chip(chip);
324 }
325
326 azx_free_stream_pages(chip);
Takashi Iwaia41d1222015-04-14 22:13:18 +0200327 azx_free_streams(chip);
Takashi Iwai4cfe99c2015-04-16 12:02:30 +0200328 snd_hdac_bus_exit(azx_bus(chip));
Dylan Reid3c320f32014-05-19 19:18:27 -0700329
330 return 0;
331}
332
333static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
334{
335 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
Takashi Iwaia41d1222015-04-14 22:13:18 +0200336 struct hdac_bus *bus = azx_bus(chip);
Dylan Reid3c320f32014-05-19 19:18:27 -0700337 struct device *dev = hda->dev;
338 struct resource *res;
Dylan Reid3c320f32014-05-19 19:18:27 -0700339
Dylan Reid3c320f32014-05-19 19:18:27 -0700340 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
341 hda->regs = devm_ioremap_resource(dev, res);
Eliot Blennerhassett93ceaa32015-02-14 15:32:24 +1300342 if (IS_ERR(hda->regs))
343 return PTR_ERR(hda->regs);
Dylan Reid3c320f32014-05-19 19:18:27 -0700344
Takashi Iwaia41d1222015-04-14 22:13:18 +0200345 bus->remap_addr = hda->regs + HDA_BAR0;
346 bus->addr = res->start + HDA_BAR0;
Dylan Reid3c320f32014-05-19 19:18:27 -0700347
Dylan Reid3c320f32014-05-19 19:18:27 -0700348 hda_tegra_init(hda);
349
350 return 0;
351}
352
Sameer Pujar65af2122019-01-22 13:03:17 +0530353static int hda_tegra_init_clk(struct hda_tegra *hda)
354{
355 struct device *dev = hda->dev;
356
357 hda->hda_clk = devm_clk_get(dev, "hda");
358 if (IS_ERR(hda->hda_clk)) {
359 dev_err(dev, "failed to get hda clock\n");
360 return PTR_ERR(hda->hda_clk);
361 }
362 hda->hda2codec_2x_clk = devm_clk_get(dev, "hda2codec_2x");
363 if (IS_ERR(hda->hda2codec_2x_clk)) {
364 dev_err(dev, "failed to get hda2codec_2x clock\n");
365 return PTR_ERR(hda->hda2codec_2x_clk);
366 }
367 hda->hda2hdmi_clk = devm_clk_get(dev, "hda2hdmi");
368 if (IS_ERR(hda->hda2hdmi_clk)) {
369 dev_err(dev, "failed to get hda2hdmi clock\n");
370 return PTR_ERR(hda->hda2hdmi_clk);
371 }
372
373 return 0;
374}
375
Dylan Reid3c320f32014-05-19 19:18:27 -0700376static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
377{
Takashi Iwaia41d1222015-04-14 22:13:18 +0200378 struct hdac_bus *bus = azx_bus(chip);
Dylan Reid3c320f32014-05-19 19:18:27 -0700379 struct snd_card *card = chip->card;
380 int err;
381 unsigned short gcap;
382 int irq_id = platform_get_irq(pdev, 0);
Sameer Pujarc94800a2018-11-29 09:28:52 +0530383 const char *sname;
384 struct device_node *root;
Dylan Reid3c320f32014-05-19 19:18:27 -0700385
386 err = hda_tegra_init_chip(chip, pdev);
387 if (err)
388 return err;
389
390 err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
391 IRQF_SHARED, KBUILD_MODNAME, chip);
392 if (err) {
393 dev_err(chip->card->dev,
394 "unable to request IRQ %d, disabling device\n",
395 irq_id);
396 return err;
397 }
Takashi Iwaia41d1222015-04-14 22:13:18 +0200398 bus->irq = irq_id;
Dylan Reid3c320f32014-05-19 19:18:27 -0700399
Takashi Iwaia41d1222015-04-14 22:13:18 +0200400 synchronize_irq(bus->irq);
Dylan Reid3c320f32014-05-19 19:18:27 -0700401
402 gcap = azx_readw(chip, GCAP);
403 dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
404
405 /* read number of streams from GCAP register instead of using
406 * hardcoded value
407 */
408 chip->capture_streams = (gcap >> 8) & 0x0f;
409 chip->playback_streams = (gcap >> 12) & 0x0f;
410 if (!chip->playback_streams && !chip->capture_streams) {
411 /* gcap didn't give any info, switching to old method */
412 chip->playback_streams = NUM_PLAYBACK_SD;
413 chip->capture_streams = NUM_CAPTURE_SD;
414 }
415 chip->capture_index_offset = 0;
416 chip->playback_index_offset = chip->capture_streams;
417 chip->num_streams = chip->playback_streams + chip->capture_streams;
Dylan Reid3c320f32014-05-19 19:18:27 -0700418
Takashi Iwaia41d1222015-04-14 22:13:18 +0200419 /* initialize streams */
420 err = azx_init_streams(chip);
Thierry Reding6a464a42015-05-05 14:56:21 +0200421 if (err < 0) {
422 dev_err(card->dev, "failed to initialize streams: %d\n", err);
Dylan Reid3c320f32014-05-19 19:18:27 -0700423 return err;
Thierry Reding6a464a42015-05-05 14:56:21 +0200424 }
Dylan Reid3c320f32014-05-19 19:18:27 -0700425
Takashi Iwaia41d1222015-04-14 22:13:18 +0200426 err = azx_alloc_stream_pages(chip);
Thierry Reding6a464a42015-05-05 14:56:21 +0200427 if (err < 0) {
428 dev_err(card->dev, "failed to allocate stream pages: %d\n",
429 err);
Takashi Iwaia41d1222015-04-14 22:13:18 +0200430 return err;
Thierry Reding6a464a42015-05-05 14:56:21 +0200431 }
Dylan Reid3c320f32014-05-19 19:18:27 -0700432
433 /* initialize chip */
434 azx_init_chip(chip, 1);
435
436 /* codec detection */
Takashi Iwaia41d1222015-04-14 22:13:18 +0200437 if (!bus->codec_mask) {
Dylan Reid3c320f32014-05-19 19:18:27 -0700438 dev_err(card->dev, "no codecs found!\n");
439 return -ENODEV;
440 }
441
Sameer Pujarc94800a2018-11-29 09:28:52 +0530442 /* driver name */
Dylan Reid3c320f32014-05-19 19:18:27 -0700443 strcpy(card->driver, "tegra-hda");
Sameer Pujarc94800a2018-11-29 09:28:52 +0530444
445 root = of_find_node_by_path("/");
446 sname = of_get_property(root, "compatible", NULL);
447 of_node_put(root);
448 if (!sname) {
449 dev_err(card->dev,
450 "failed to get compatible property from root node\n");
451 return -ENODEV;
452 }
453 /* shortname for card */
454 if (strlen(sname) > sizeof(card->shortname))
455 dev_info(card->dev, "truncating shortname for card\n");
456 strncpy(card->shortname, sname, sizeof(card->shortname));
457
458 /* longname for card */
Dylan Reid3c320f32014-05-19 19:18:27 -0700459 snprintf(card->longname, sizeof(card->longname),
460 "%s at 0x%lx irq %i",
Takashi Iwaia41d1222015-04-14 22:13:18 +0200461 card->shortname, bus->addr, bus->irq);
Dylan Reid3c320f32014-05-19 19:18:27 -0700462
463 return 0;
464}
465
466/*
467 * constructor
468 */
Takashi Iwai83510442015-09-24 11:00:18 +0200469
470static void hda_tegra_probe_work(struct work_struct *work);
471
Dylan Reid3c320f32014-05-19 19:18:27 -0700472static int hda_tegra_create(struct snd_card *card,
473 unsigned int driver_caps,
Dylan Reid3c320f32014-05-19 19:18:27 -0700474 struct hda_tegra *hda)
475{
476 static struct snd_device_ops ops = {
Takashi Iwaia41d1222015-04-14 22:13:18 +0200477 .dev_disconnect = hda_tegra_dev_disconnect,
Dylan Reid3c320f32014-05-19 19:18:27 -0700478 .dev_free = hda_tegra_dev_free,
479 };
480 struct azx *chip;
481 int err;
482
483 chip = &hda->chip;
484
Dylan Reid3c320f32014-05-19 19:18:27 -0700485 mutex_init(&chip->open_mutex);
486 chip->card = card;
Takashi Iwaia43ff5b2015-04-14 17:26:00 +0200487 chip->ops = &hda_tegra_ops;
Dylan Reid3c320f32014-05-19 19:18:27 -0700488 chip->driver_caps = driver_caps;
489 chip->driver_type = driver_caps & 0xff;
490 chip->dev_index = 0;
491 INIT_LIST_HEAD(&chip->pcm_list);
Dylan Reid3c320f32014-05-19 19:18:27 -0700492
Dylan Reid3c320f32014-05-19 19:18:27 -0700493 chip->codec_probe_mask = -1;
494
495 chip->single_cmd = false;
496 chip->snoop = true;
497
Takashi Iwai83510442015-09-24 11:00:18 +0200498 INIT_WORK(&hda->probe_work, hda_tegra_probe_work);
499
Thierry Reding3b90f402015-05-05 14:45:57 +0200500 err = azx_bus_init(chip, NULL, &hda_tegra_io_ops);
501 if (err < 0)
502 return err;
503
Takashi Iwai7d9a1802015-12-17 08:23:39 +0100504 chip->bus.needs_damn_long_delay = 1;
505
Dylan Reid3c320f32014-05-19 19:18:27 -0700506 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
507 if (err < 0) {
508 dev_err(card->dev, "Error creating device\n");
509 return err;
510 }
511
512 return 0;
513}
514
515static const struct of_device_id hda_tegra_match[] = {
516 { .compatible = "nvidia,tegra30-hda" },
517 {},
518};
Dylan Reidf73387c2014-05-20 11:26:12 -0700519MODULE_DEVICE_TABLE(of, hda_tegra_match);
Dylan Reid3c320f32014-05-19 19:18:27 -0700520
521static int hda_tegra_probe(struct platform_device *pdev)
522{
Sameer Pujar9935d552019-01-22 13:03:21 +0530523 const unsigned int driver_flags = AZX_DCAPS_CORBRP_SELF_CLEAR |
524 AZX_DCAPS_PM_RUNTIME;
Dylan Reid3c320f32014-05-19 19:18:27 -0700525 struct snd_card *card;
526 struct azx *chip;
527 struct hda_tegra *hda;
528 int err;
Dylan Reid3c320f32014-05-19 19:18:27 -0700529
530 hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
531 if (!hda)
532 return -ENOMEM;
533 hda->dev = &pdev->dev;
534 chip = &hda->chip;
535
536 err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
537 THIS_MODULE, 0, &card);
538 if (err < 0) {
539 dev_err(&pdev->dev, "Error creating card!\n");
540 return err;
541 }
542
Sameer Pujar65af2122019-01-22 13:03:17 +0530543 err = hda_tegra_init_clk(hda);
544 if (err < 0)
545 goto out_free;
546
Takashi Iwaia43ff5b2015-04-14 17:26:00 +0200547 err = hda_tegra_create(card, driver_flags, hda);
Dylan Reid3c320f32014-05-19 19:18:27 -0700548 if (err < 0)
549 goto out_free;
550 card->private_data = chip;
551
552 dev_set_drvdata(&pdev->dev, card);
Sameer Pujar3f7e94e2019-01-22 13:03:16 +0530553
554 pm_runtime_enable(hda->dev);
555 if (!azx_has_pm_runtime(chip))
556 pm_runtime_forbid(hda->dev);
557
Takashi Iwai83510442015-09-24 11:00:18 +0200558 schedule_work(&hda->probe_work);
559
560 return 0;
561
562out_free:
563 snd_card_free(card);
564 return err;
565}
566
567static void hda_tegra_probe_work(struct work_struct *work)
568{
569 struct hda_tegra *hda = container_of(work, struct hda_tegra, probe_work);
570 struct azx *chip = &hda->chip;
571 struct platform_device *pdev = to_platform_device(hda->dev);
572 int err;
Dylan Reid3c320f32014-05-19 19:18:27 -0700573
Sameer Pujar3f7e94e2019-01-22 13:03:16 +0530574 pm_runtime_get_sync(hda->dev);
Dylan Reid3c320f32014-05-19 19:18:27 -0700575 err = hda_tegra_first_init(chip, pdev);
576 if (err < 0)
577 goto out_free;
578
579 /* create codec instances */
Thierry Reding350355e2018-12-03 16:53:16 +0100580 err = azx_probe_codecs(chip, 8);
Dylan Reid3c320f32014-05-19 19:18:27 -0700581 if (err < 0)
582 goto out_free;
583
584 err = azx_codec_configure(chip);
585 if (err < 0)
586 goto out_free;
587
Dylan Reid3c320f32014-05-19 19:18:27 -0700588 err = snd_card_register(chip->card);
589 if (err < 0)
590 goto out_free;
591
592 chip->running = 1;
Takashi Iwaia41d1222015-04-14 22:13:18 +0200593 snd_hda_set_power_save(&chip->bus, power_save * 1000);
Dylan Reid3c320f32014-05-19 19:18:27 -0700594
Takashi Iwai83510442015-09-24 11:00:18 +0200595 out_free:
Sameer Pujar3f7e94e2019-01-22 13:03:16 +0530596 pm_runtime_put(hda->dev);
Takashi Iwai83510442015-09-24 11:00:18 +0200597 return; /* no error return from async probe */
Dylan Reid3c320f32014-05-19 19:18:27 -0700598}
599
600static int hda_tegra_remove(struct platform_device *pdev)
601{
Sameer Pujar3f7e94e2019-01-22 13:03:16 +0530602 int ret;
603
604 ret = snd_card_free(dev_get_drvdata(&pdev->dev));
605 pm_runtime_disable(&pdev->dev);
606
607 return ret;
Dylan Reid3c320f32014-05-19 19:18:27 -0700608}
609
Takashi Iwaib2a0baf2015-03-05 17:21:32 +0100610static void hda_tegra_shutdown(struct platform_device *pdev)
611{
612 struct snd_card *card = dev_get_drvdata(&pdev->dev);
613 struct azx *chip;
614
615 if (!card)
616 return;
617 chip = card->private_data;
618 if (chip && chip->running)
619 azx_stop_chip(chip);
620}
621
Dylan Reid3c320f32014-05-19 19:18:27 -0700622static struct platform_driver tegra_platform_hda = {
623 .driver = {
624 .name = "tegra-hda",
625 .pm = &hda_tegra_pm,
626 .of_match_table = hda_tegra_match,
627 },
628 .probe = hda_tegra_probe,
629 .remove = hda_tegra_remove,
Takashi Iwaib2a0baf2015-03-05 17:21:32 +0100630 .shutdown = hda_tegra_shutdown,
Dylan Reid3c320f32014-05-19 19:18:27 -0700631};
632module_platform_driver(tegra_platform_hda);
633
634MODULE_DESCRIPTION("Tegra HDA bus driver");
635MODULE_LICENSE("GPL v2");