1/*
2 *
3 *  hda_loongson.c - Implementation of primary alsa driver code base
4 *                for Intel HD Audio.
5 *
6 *  Copyright (c) 2004 Intel Corporation. All rights reserved.
7 *
8 *  Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
9 *                     PeiSen Hou <pshou@realtek.com.tw>
10 *
11 *  Copyright (c) 2014 Huacai Chen <chenhc@lemote.com>
12 *
13 *  This program is free software; you can redistribute it and/or modify it
14 *  under the terms of the GNU General Public License as published by the Free
15 *  Software Foundation; either version 2 of the License, or (at your option)
16 *  any later version.
17 *
18 *  This program is distributed in the hope that it will be useful, but WITHOUT
19 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
21 *  more details.
22 *
23 *  You should have received a copy of the GNU General Public License along with
24 *  this program; if not, write to the Free Software Foundation, Inc., 59
25 *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 *
27 *  CONTACTS:
28 *
29 *  Matt Jared		matt.jared@intel.com
30 *  Andy Kopp		andy.kopp@intel.com
31 *  Dan Kogan		dan.d.kogan@intel.com
32 *
33 *  CHANGES:
34 *
35 *  2004.12.01	Major rewrite by tiwai, merged the work of pshou
36 *
37 */
38
39#include <linux/delay.h>
40#include <linux/interrupt.h>
41#include <linux/kernel.h>
42#include <linux/module.h>
43#include <linux/dma-mapping.h>
44#include <linux/moduleparam.h>
45#include <linux/init.h>
46#include <linux/slab.h>
47#include <linux/platform_device.h>
48#include <linux/mutex.h>
49#include <linux/reboot.h>
50#include <linux/io.h>
51#include <linux/pm_runtime.h>
52#include <linux/clocksource.h>
53#include <linux/time.h>
54#include <linux/completion.h>
55#include <linux/firmware.h>
56
57#include <sound/core.h>
58#include <sound/initval.h>
59#include <sound/hda_codec.h>
60#include <loongson.h>
61#include "hda_controller.h"
62
63#define LS7A_NUM_CAPTURE	4
64#define LS7A_NUM_PLAYBACK	4
65
66static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
67static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
68static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
69static char *model[SNDRV_CARDS];
70static int bdl_pos_adj[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
71static int probe_only[SNDRV_CARDS];
72static int jackpoll_ms[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1200};
73static bool single_cmd;
74static int enable_msi = -1;
75#ifdef CONFIG_SND_HDA_INPUT_BEEP
76static bool beep_mode[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] =
77					CONFIG_SND_HDA_INPUT_BEEP_MODE};
78#endif
79
80module_param_array(index, int, NULL, 0444);
81MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
82module_param_array(id, charp, NULL, 0444);
83MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
84module_param_array(enable, bool, NULL, 0444);
85MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
86module_param_array(model, charp, NULL, 0444);
87MODULE_PARM_DESC(model, "Use the given board model.");
88module_param_array(bdl_pos_adj, int, NULL, 0644);
89MODULE_PARM_DESC(bdl_pos_adj, "BDL position adjustment offset.");
90module_param_array(probe_only, int, NULL, 0444);
91MODULE_PARM_DESC(probe_only, "Only probing and no codec initialization.");
92module_param_array(jackpoll_ms, int, NULL, 0444);
93MODULE_PARM_DESC(jackpoll_ms, "Ms between polling for jack events (default = 0, using unsol events only)");
94module_param(single_cmd, bool, 0444);
95MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
96		 "(for debugging only).");
97module_param(enable_msi, bint, 0444);
98MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
99#ifdef CONFIG_SND_HDA_INPUT_BEEP
100module_param_array(beep_mode, bool, NULL, 0444);
101MODULE_PARM_DESC(beep_mode, "Select HDA Beep registration mode "
102			    "(0=off, 1=on) (default=1).");
103#endif
104
105#ifdef CONFIG_PM
106static int param_set_xint(const char *val, const struct kernel_param *kp);
107static struct kernel_param_ops param_ops_xint = {
108	.set = param_set_xint,
109	.get = param_get_int,
110};
111#define param_check_xint param_check_int
112
113static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
114module_param(power_save, xint, 0644);
115MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
116		 "(in second, 0 = disable).");
117
118/* reset the HD-audio controller in power save mode.
119 * this may give more power-saving, but will take longer time to
120 * wake up.
121 */
122static bool power_save_controller = 1;
123module_param(power_save_controller, bool, 0644);
124MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
125#else
126#define power_save	0
127#endif /* CONFIG_PM */
128
129static int align_buffer_size = -1;
130module_param(align_buffer_size, bint, 0644);
131MODULE_PARM_DESC(align_buffer_size,
132		"Force buffer and period sizes to be multiple of 128 bytes.");
133
134MODULE_LICENSE("GPL");
135MODULE_SUPPORTED_DEVICE("{Loongson, LS7A}");
136MODULE_DESCRIPTION("Loongson LS7A HDA driver");
137
138/* driver types */
139enum {
140	AZX_DRIVER_LS7A,
141	AZX_DRIVER_HDMI,
142	AZX_NUM_DRIVERS, /* keep this as last entry */
143};
144
145static char *driver_short_names[] = {
146	[AZX_DRIVER_LS7A] = "HD-Audio Loongson",
147	[AZX_DRIVER_HDMI] = "HD-Audio Loongson HDMI",
148};
149
150struct hda_loongson {
151	struct azx chip;
152
153	/* for pending irqs */
154	struct work_struct irq_pending_work;
155
156	/* sync probing */
157	struct completion probe_wait;
158	struct work_struct probe_work;
159
160	/* card list (for power_save trigger) */
161	struct list_head list;
162
163	/* extra flags */
164	unsigned int irq_pending_warned:1;
165	unsigned int probe_continued:1;
166};
167
168static int azx_acquire_irq(struct azx *chip, int do_disconnect);
169
170static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
171
172/* called from IRQ */
173static int azx_position_check(struct azx *chip, struct azx_dev *azx_dev)
174{
175	struct hda_loongson *hda = container_of(chip, struct hda_loongson, chip);
176	int ok;
177
178	ok = azx_position_ok(chip, azx_dev);
179	if (ok == 1) {
180		azx_dev->irq_pending = 0;
181		return ok;
182	} else if (ok == 0) {
183		/* bogus IRQ, process it later */
184		azx_dev->irq_pending = 1;
185		schedule_work(&hda->irq_pending_work);
186	}
187	return 0;
188}
189
190/*
191 * Check whether the current DMA position is acceptable for updating
192 * periods.  Returns non-zero if it's OK.
193 *
194 * Many HD-audio controllers appear pretty inaccurate about
195 * the update-IRQ timing.  The IRQ is issued before actually the
196 * data is processed.  So, we need to process it afterwords in a
197 * workqueue.
198 */
199static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
200{
201	return 1; /* OK, it's fine */
202}
203
204/*
205 * The work for pending PCM period updates.
206 */
207static void azx_irq_pending_work(struct work_struct *work)
208{
209	struct hda_loongson *hda = container_of(work, struct hda_loongson, irq_pending_work);
210	struct azx *chip = &hda->chip;
211	struct hdac_bus *bus = azx_bus(chip);
212	struct hdac_stream *s;
213	int pending, ok;
214
215	if (!hda->irq_pending_warned) {
216		dev_info(chip->card->dev,
217			 "IRQ timing workaround is activated for card #%d. Suggest a bigger bdl_pos_adj.\n",
218			 chip->card->number);
219		hda->irq_pending_warned = 1;
220	}
221
222	for (;;) {
223		pending = 0;
224		spin_lock_irq(&bus->reg_lock);
225		list_for_each_entry(s, &bus->stream_list, list) {
226			struct azx_dev *azx_dev = stream_to_azx_dev(s);
227			if (!azx_dev->irq_pending ||
228			    !s->substream ||
229			    !s->running)
230				continue;
231			ok = azx_position_ok(chip, azx_dev);
232			if (ok > 0) {
233				azx_dev->irq_pending = 0;
234				spin_unlock(&bus->reg_lock);
235				snd_pcm_period_elapsed(s->substream);
236				spin_lock(&bus->reg_lock);
237			} else if (ok < 0) {
238				pending = 0;	/* too early */
239			} else
240				pending++;
241		}
242		spin_unlock_irq(&bus->reg_lock);
243		if (!pending)
244			return;
245		msleep(1);
246	}
247}
248
249/* clear irq_pending flags and assure no on-going workq */
250static void azx_clear_irq_pending(struct azx *chip)
251{
252	struct hdac_bus *bus = azx_bus(chip);
253	struct hdac_stream *s;
254
255	spin_lock_irq(&bus->reg_lock);
256	list_for_each_entry(s, &bus->stream_list, list) {
257		struct azx_dev *azx_dev = stream_to_azx_dev(s);
258		azx_dev->irq_pending = 0;
259	}
260	spin_unlock_irq(&bus->reg_lock);
261}
262
263static int azx_acquire_irq(struct azx *chip, int do_disconnect)
264{
265	struct hdac_bus *bus = azx_bus(chip);
266	int irq;
267
268	if (chip->pci)
269		irq = chip->pci->irq;
270	else
271		irq = platform_get_irq(to_platform_device(chip->card->dev), 0);
272
273	if (request_irq(irq, azx_interrupt, chip->msi ? 0 : IRQF_SHARED,
274			KBUILD_MODNAME, chip)) {
275		dev_err(chip->card->dev,
276			"unable to grab IRQ %d, disabling device\n", irq);
277		if (do_disconnect)
278			snd_card_disconnect(chip->card);
279		return -1;
280	}
281	bus->irq = irq;
282	return 0;
283}
284
285#ifdef CONFIG_PM
286static DEFINE_MUTEX(card_list_lock);
287static LIST_HEAD(card_list);
288
289static void azx_add_card_list(struct azx *chip)
290{
291	struct hda_loongson *hda = container_of(chip, struct hda_loongson, chip);
292	mutex_lock(&card_list_lock);
293	list_add(&hda->list, &card_list);
294	mutex_unlock(&card_list_lock);
295}
296
297static void azx_del_card_list(struct azx *chip)
298{
299	struct hda_loongson *hda = container_of(chip, struct hda_loongson, chip);
300	mutex_lock(&card_list_lock);
301	list_del_init(&hda->list);
302	mutex_unlock(&card_list_lock);
303}
304
305/* trigger power-save check at writing parameter */
306static int param_set_xint(const char *val, const struct kernel_param *kp)
307{
308	struct hda_loongson *hda;
309	struct azx *chip;
310	int prev = power_save;
311	int ret = param_set_int(val, kp);
312
313	if (ret || prev == power_save)
314		return ret;
315
316	mutex_lock(&card_list_lock);
317	list_for_each_entry(hda, &card_list, list) {
318		chip = &hda->chip;
319		if (!hda->probe_continued || chip->disabled)
320			continue;
321		snd_hda_set_power_save(&chip->bus, power_save * 1000);
322	}
323	mutex_unlock(&card_list_lock);
324	return 0;
325}
326#else
327#define azx_add_card_list(chip) /* NOP */
328#define azx_del_card_list(chip) /* NOP */
329#endif /* CONFIG_PM */
330
331#if defined(CONFIG_PM_SLEEP)
332/*
333 * power management
334 */
335static int azx_suspend(struct device *dev)
336{
337	struct snd_card *card = dev_get_drvdata(dev);
338	struct azx *chip = card->private_data;
339	struct hdac_bus *bus;
340
341	if (chip->disabled)
342		return 0;
343
344	bus = azx_bus(chip);
345	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
346	azx_clear_irq_pending(chip);
347	azx_stop_chip(chip);
348	azx_enter_link_reset(chip);
349	if (bus->irq >= 0) {
350		free_irq(bus->irq, chip);
351		bus->irq = -1;
352	}
353	return 0;
354}
355
356static int azx_resume(struct device *dev)
357{
358	struct snd_card *card = dev_get_drvdata(dev);
359	struct azx *chip = card->private_data;
360
361	if (chip->disabled)
362		return 0;
363
364	chip->msi = 0;
365	if (azx_acquire_irq(chip, 1) < 0)
366		return -EIO;
367
368	azx_init_chip(chip, true);
369
370	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
371	return 0;
372}
373#endif /* CONFIG_PM_SLEEP */
374
375#ifdef CONFIG_PM
376static int azx_runtime_suspend(struct device *dev)
377{
378	struct snd_card *card = dev_get_drvdata(dev);
379	struct azx *chip = card->private_data;
380
381	if (chip->disabled)
382		return 0;
383
384	if (!azx_has_pm_runtime(chip))
385		return 0;
386
387	/* enable controller wake up event */
388	azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) |
389		  STATESTS_INT_MASK);
390
391	azx_stop_chip(chip);
392	azx_enter_link_reset(chip);
393	azx_clear_irq_pending(chip);
394	return 0;
395}
396
397static int azx_runtime_resume(struct device *dev)
398{
399	struct snd_card *card = dev_get_drvdata(dev);
400	struct azx *chip = card->private_data;
401	struct hda_codec *codec;
402	int status;
403
404	if (chip->disabled)
405		return 0;
406
407	if (!azx_has_pm_runtime(chip))
408		return 0;
409
410	/* Read STATESTS before controller reset */
411	status = azx_readw(chip, STATESTS);
412
413	azx_init_chip(chip, true);
414
415	if (status) {
416		list_for_each_codec(codec, &chip->bus)
417			if (status & (1 << codec->addr))
418				schedule_delayed_work(&codec->jackpoll_work,
419						      codec->jackpoll_interval);
420	}
421
422	/* disable controller Wake Up event*/
423	azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) &
424			~STATESTS_INT_MASK);
425
426	return 0;
427}
428
429static int azx_runtime_idle(struct device *dev)
430{
431	struct snd_card *card = dev_get_drvdata(dev);
432	struct azx *chip = card->private_data;
433
434	if (chip->disabled)
435		return 0;
436
437	if (!power_save_controller || !azx_has_pm_runtime(chip) ||
438	    azx_bus(chip)->codec_powered)
439		return -EBUSY;
440
441	return 0;
442}
443
444#endif /* CONFIG_PM */
445
446#ifdef CONFIG_PM
447static const struct dev_pm_ops azx_pm = {
448	SET_SYSTEM_SLEEP_PM_OPS(azx_suspend, azx_resume)
449	SET_RUNTIME_PM_OPS(azx_runtime_suspend, azx_runtime_resume, azx_runtime_idle)
450};
451
452#define AZX_PM_OPS	&azx_pm
453#else
454#define AZX_PM_OPS	NULL
455#endif /* CONFIG_PM */
456
457static int azx_probe_continue(struct azx *chip);
458
459/*
460 * destructor
461 */
462static int azx_free(struct azx *chip)
463{
464	struct device *snddev = chip->card->dev;
465	struct hda_loongson *hda = container_of(chip, struct hda_loongson, chip);
466	struct hdac_bus *bus = azx_bus(chip);
467
468	if (azx_has_pm_runtime(chip) && chip->running)
469		pm_runtime_get_noresume(snddev);
470
471	azx_del_card_list(chip);
472
473	complete_all(&hda->probe_wait);
474
475	if (bus->chip_init) {
476		azx_clear_irq_pending(chip);
477		azx_stop_all_streams(chip);
478		azx_stop_chip(chip);
479	}
480
481	if (bus->irq >= 0)
482		free_irq(bus->irq, (void*)chip);
483	if (bus->remap_addr)
484		iounmap(bus->remap_addr);
485
486	azx_free_stream_pages(chip);
487	azx_free_streams(chip);
488	snd_hdac_bus_exit(bus);
489	kfree(hda);
490
491	return 0;
492}
493
494static int azx_dev_disconnect(struct snd_device *device)
495{
496	struct azx *chip = device->device_data;
497
498	chip->bus.shutdown = 1;
499	return 0;
500}
501
502static int azx_dev_free(struct snd_device *device)
503{
504	return azx_free(device->device_data);
505}
506
507/*
508 * constructor
509 */
510static const struct hda_controller_ops loongson_hda_ops;
511
512static int azx_create(struct snd_card *card, struct pci_dev *pcidev,
513		      struct platform_device *platdev, int dev,
514		      unsigned int driver_caps, struct azx **rchip)
515{
516	static struct snd_device_ops ops = {
517		.dev_disconnect = azx_dev_disconnect,
518		.dev_free = azx_dev_free,
519	};
520	struct hda_loongson *hda;
521	struct azx *chip;
522	int err;
523
524	*rchip = NULL;
525
526	hda = kzalloc(sizeof(*hda), GFP_KERNEL);
527	if (!hda) {
528		dev_err(card->dev, "Cannot allocate hda\n");
529		return -ENOMEM;
530	}
531
532	chip = &hda->chip;
533	mutex_init(&chip->open_mutex);
534	chip->card = card;
535	chip->pci = pcidev;
536	chip->ops = &loongson_hda_ops;
537	chip->driver_caps = driver_caps;
538	chip->driver_type = driver_caps & 0xff;
539	chip->dev_index = dev;
540	if (jackpoll_ms[dev] >= 50 && jackpoll_ms[dev] <= 60000)
541		chip->jackpoll_interval = msecs_to_jiffies(jackpoll_ms[dev]);
542	INIT_LIST_HEAD(&chip->pcm_list);
543	INIT_WORK(&hda->irq_pending_work, azx_irq_pending_work);
544	INIT_LIST_HEAD(&hda->list);
545	init_completion(&hda->probe_wait);
546
547	chip->get_position[0] = chip->get_position[1] = azx_get_pos_lpib;
548
549	chip->snoop = false;
550	chip->single_cmd = single_cmd;
551	azx_bus(chip)->codec_mask = chip->codec_probe_mask = 0xf;
552
553	if (bdl_pos_adj[dev] < 0) {
554		switch (chip->driver_type) {
555		case AZX_DRIVER_LS7A:
556			bdl_pos_adj[dev] = 1;
557			break;
558		default:
559			bdl_pos_adj[dev] = 32;
560			break;
561		}
562	}
563	chip->bdl_pos_adj = bdl_pos_adj[dev];
564
565	err = azx_bus_init(chip, model[dev]);
566	if (err < 0) {
567		kfree(hda);
568		return err;
569	}
570
571	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
572	if (err < 0) {
573		dev_err(card->dev, "Error creating device [card]!\n");
574		azx_free(chip);
575		return err;
576	}
577
578	*rchip = chip;
579
580	return 0;
581}
582
583static int azx_first_init(struct azx *chip)
584{
585	int dev = chip->dev_index;
586	struct snd_card *card = chip->card;
587	struct hdac_bus *bus = azx_bus(chip);
588	int err;
589	unsigned short gcap;
590
591	bus->addr = pci_resource_start(chip->pci, 0);
592	bus->remap_addr = ioremap(bus->addr,
593			   pci_resource_end(chip->pci, 0) - pci_resource_start(chip->pci, 0) + 1);
594	if (bus->remap_addr == NULL) {
595		dev_err(card->dev, "ioremap error\n");
596		return -ENXIO;
597	}
598
599	chip->msi = 0;
600
601	if (azx_acquire_irq(chip, 0) < 0)
602		return -EBUSY;
603
604	synchronize_irq(bus->irq);
605
606	gcap = azx_readw(chip, GCAP);
607	dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
608
609	/* disable 64bit DMA address on some devices */
610	if (chip->driver_caps & AZX_DCAPS_NO_64BIT) {
611		dev_dbg(card->dev, "Disabling 64bit DMA\n");
612		gcap &= ~AZX_GCAP_64OK;
613	}
614
615	/* disable buffer size rounding to 128-byte multiples if supported */
616	if (align_buffer_size >= 0)
617		chip->align_buffer_size = !!align_buffer_size;
618	else {
619		if (chip->driver_caps & AZX_DCAPS_NO_ALIGN_BUFSIZE)
620			chip->align_buffer_size = 0;
621		else
622			chip->align_buffer_size = 1;
623	}
624
625	/* allow 64bit DMA address if supported by H/W */
626	if ((gcap & AZX_GCAP_64OK) && !dma_set_mask(chip->card->dev, DMA_BIT_MASK(64)))
627		dma_set_coherent_mask(chip->card->dev, DMA_BIT_MASK(64));
628	else {
629		dma_set_mask(chip->card->dev, DMA_BIT_MASK(32));
630		dma_set_coherent_mask(chip->card->dev, DMA_BIT_MASK(32));
631	}
632
633	/* read number of streams from GCAP register instead of using
634	 * hardcoded value
635	 */
636	chip->capture_streams = (gcap >> 8) & 0x0f;
637	chip->playback_streams = (gcap >> 12) & 0x0f;
638	if (!chip->playback_streams && !chip->capture_streams) {
639		/* gcap didn't give any info, switching to old method */
640		chip->capture_streams = LS7A_NUM_CAPTURE;
641		chip->playback_streams = LS7A_NUM_PLAYBACK;
642	}
643	chip->capture_index_offset = 0;
644	chip->playback_index_offset = chip->capture_streams;
645	chip->num_streams = chip->playback_streams + chip->capture_streams;
646
647	/* initialize streams */
648	err = azx_init_streams(chip);
649	if (err < 0)
650		return err;
651	chip->playback_streams = chip->capture_streams = 1; /* Loongson */
652
653	err = azx_alloc_stream_pages(chip);
654	if (err < 0)
655		return err;
656
657	/* initialize chip */
658	azx_init_chip(chip, (probe_only[dev] & 2) == 0);
659
660	/* codec detection */
661	if (!azx_bus(chip)->codec_mask) {
662		dev_err(card->dev, "no codecs found!\n");
663		return -ENODEV;
664	}
665
666	strcpy(card->driver, "HDA-Loongson");
667	strlcpy(card->shortname, driver_short_names[chip->driver_type],
668		sizeof(card->shortname));
669	snprintf(card->longname, sizeof(card->longname),
670		 "%s at 0x%lx irq %i",
671		 card->shortname, bus->addr, bus->irq);
672
673	return 0;
674}
675
676static const struct hda_controller_ops loongson_hda_ops = {
677	.position_check = azx_position_check,
678};
679
680/* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
681static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] = {};
682
683static int azx_probe_continue(struct azx *chip)
684{
685	struct hda_loongson *hda = container_of(chip, struct hda_loongson, chip);
686	int dev = chip->dev_index;
687	int err;
688	struct device *snddev = chip->card->dev;
689
690	hda->probe_continued = 1;
691
692	err = azx_first_init(chip);
693	if (err < 0)
694		goto out_free;
695
696#ifdef CONFIG_SND_HDA_INPUT_BEEP
697	chip->beep_mode = beep_mode[dev];
698#endif
699
700	/* create codec instances */
701	err = azx_probe_codecs(chip, azx_max_codecs[chip->driver_type]);
702	if (err < 0)
703		goto out_free;
704
705	if ((probe_only[dev] & 1) == 0) {
706		err = azx_codec_configure(chip);
707		if (err < 0)
708			goto out_free;
709	}
710
711	err = snd_card_register(chip->card);
712	if (err < 0)
713		goto out_free;
714
715	chip->running = 1;
716	azx_add_card_list(chip);
717#ifdef CONFIG_PM
718	pm_runtime_forbid(snddev);
719	pm_runtime_set_active(snddev);
720#endif
721	snd_hda_set_power_save(&chip->bus, power_save * 1000);
722	if (azx_has_pm_runtime(chip))
723		pm_runtime_put_noidle(snddev);
724
725out_free:
726	complete_all(&hda->probe_wait);
727	return err;
728}
729
730static const struct pci_device_id azx_ids[] = {
731	{PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, PCI_DEVICE_ID_LOONGSON_HDA),
732	.driver_data = AZX_DRIVER_LS7A | AZX_DCAPS_LS2X_WORKAROUND},
733	{PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, PCI_DEVICE_ID_LOONGSON_HDMI),
734	.driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_LS2X_WORKAROUND},
735	{}
736};
737
738MODULE_DEVICE_TABLE(pci, azx_ids);
739
740static int azx_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
741{
742	int ret;
743	bool probe_now;
744	static int dev;
745	struct snd_card *card;
746	struct azx *chip;
747	struct hda_loongson *hda;
748
749	/* Enable device in PCI config */
750	ret = pci_enable_device(pdev);
751	if (ret < 0) {
752		printk(KERN_ERR "Loongson HDA (%s): Cannot enable PCI device\n",
753		       pci_name(pdev));
754		goto out;
755	}
756
757	/* request the mem regions */
758	ret = pci_request_region(pdev, 0, "Loongson HDA");
759	if (ret < 0) {
760		printk( KERN_ERR "Loongson HDA (%s): cannot request region 0.\n",
761			pci_name(pdev));
762		goto out;
763	}
764
765	if (dev >= SNDRV_CARDS)
766		return -ENODEV;
767	if (!enable[dev]) {
768		dev++;
769		return -ENOENT;
770	}
771
772	ret = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
773			   0, &card);
774	if (ret < 0) {
775		dev_err(&pdev->dev, "Error creating card!\n");
776		return ret;
777	}
778
779	ret = azx_create(card, pdev, NULL, dev, pid->driver_data, &chip);
780	if (ret < 0)
781		goto out_free;
782	card->private_data = chip;
783	hda = container_of(chip, struct hda_loongson, chip);
784
785	dev_set_drvdata(&pdev->dev, card);
786
787	probe_now = !chip->disabled;
788
789	if (probe_now) {
790		ret = azx_probe_continue(chip);
791		if (ret < 0)
792			goto out_free;
793	}
794
795	dev++;
796	if (chip->disabled)
797		complete_all(&hda->probe_wait);
798	return 0;
799
800out_free:
801	snd_card_free(card);
802out:
803	return ret;
804}
805
806static void azx_pci_remove(struct pci_dev *pdev)
807{
808	snd_card_free(dev_get_drvdata(&pdev->dev));
809}
810
811static void azx_pci_shutdown(struct pci_dev *pdev)
812{
813	struct snd_card *card = dev_get_drvdata(&pdev->dev);
814	struct azx *chip;
815
816	if (!card)
817		return;
818	chip = card->private_data;
819	if (chip && chip->running)
820		azx_stop_chip(chip);
821}
822
823/* pci_driver definition */
824static struct pci_driver azx_pci_driver = {
825	.name = "loongson-audio",
826	.id_table = azx_ids,
827	.probe = azx_pci_probe,
828	.remove = azx_pci_remove,
829	.shutdown = azx_pci_shutdown,
830	.driver.pm = AZX_PM_OPS,
831};
832
833static int __init alsa_card_azx_init(void)
834{
835	int ret;
836
837	ret = pci_register_driver(&azx_pci_driver);
838	if (ret)
839		pr_err("hda azx pci driver register\n");
840
841	return ret;
842}
843
844static void __exit alsa_card_azx_exit(void)
845{
846	pci_unregister_driver(&azx_pci_driver);
847}
848
849module_init(alsa_card_azx_init)
850module_exit(alsa_card_azx_exit)
851