162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  Common functionality for the alsa driver code base for HD Audio.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef __SOUND_HDA_CONTROLLER_H
762306a36Sopenharmony_ci#define __SOUND_HDA_CONTROLLER_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/timecounter.h>
1062306a36Sopenharmony_ci#include <linux/interrupt.h>
1162306a36Sopenharmony_ci#include <sound/core.h>
1262306a36Sopenharmony_ci#include <sound/pcm.h>
1362306a36Sopenharmony_ci#include <sound/initval.h>
1462306a36Sopenharmony_ci#include <sound/hda_codec.h>
1562306a36Sopenharmony_ci#include <sound/hda_register.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define AZX_MAX_CODECS		HDA_MAX_CODECS
1862306a36Sopenharmony_ci#define AZX_DEFAULT_CODECS	4
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* driver quirks (capabilities) */
2162306a36Sopenharmony_ci/* bits 0-7 are used for indicating driver type */
2262306a36Sopenharmony_ci#define AZX_DCAPS_NO_TCSEL	(1 << 8)	/* No Intel TCSEL bit */
2362306a36Sopenharmony_ci#define AZX_DCAPS_NO_MSI	(1 << 9)	/* No MSI support */
2462306a36Sopenharmony_ci#define AZX_DCAPS_SNOOP_MASK	(3 << 10)	/* snoop type mask */
2562306a36Sopenharmony_ci#define AZX_DCAPS_SNOOP_OFF	(1 << 12)	/* snoop default off */
2662306a36Sopenharmony_ci#ifdef CONFIG_SND_HDA_I915
2762306a36Sopenharmony_ci#define AZX_DCAPS_I915_COMPONENT (1 << 13)	/* bind with i915 gfx */
2862306a36Sopenharmony_ci#else
2962306a36Sopenharmony_ci#define AZX_DCAPS_I915_COMPONENT 0		/* NOP */
3062306a36Sopenharmony_ci#endif
3162306a36Sopenharmony_ci/* 14 unused */
3262306a36Sopenharmony_ci#define AZX_DCAPS_CTX_WORKAROUND (1 << 15)	/* X-Fi workaround */
3362306a36Sopenharmony_ci#define AZX_DCAPS_POSFIX_LPIB	(1 << 16)	/* Use LPIB as default */
3462306a36Sopenharmony_ci#define AZX_DCAPS_AMD_WORKAROUND (1 << 17)	/* AMD-specific workaround */
3562306a36Sopenharmony_ci#define AZX_DCAPS_NO_64BIT	(1 << 18)	/* No 64bit address */
3662306a36Sopenharmony_ci/* 19 unused */
3762306a36Sopenharmony_ci#define AZX_DCAPS_OLD_SSYNC	(1 << 20)	/* Old SSYNC reg for ICH */
3862306a36Sopenharmony_ci#define AZX_DCAPS_NO_ALIGN_BUFSIZE (1 << 21)	/* no buffer size alignment */
3962306a36Sopenharmony_ci/* 22 unused */
4062306a36Sopenharmony_ci#define AZX_DCAPS_4K_BDLE_BOUNDARY (1 << 23)	/* BDLE in 4k boundary */
4162306a36Sopenharmony_ci/* 24 unused */
4262306a36Sopenharmony_ci#define AZX_DCAPS_COUNT_LPIB_DELAY  (1 << 25)	/* Take LPIB as delay */
4362306a36Sopenharmony_ci#define AZX_DCAPS_PM_RUNTIME	(1 << 26)	/* runtime PM support */
4462306a36Sopenharmony_ci#define AZX_DCAPS_RETRY_PROBE	(1 << 27)	/* retry probe if no codec is configured */
4562306a36Sopenharmony_ci#define AZX_DCAPS_CORBRP_SELF_CLEAR (1 << 28)	/* CORBRP clears itself after reset */
4662306a36Sopenharmony_ci#define AZX_DCAPS_NO_MSI64      (1 << 29)	/* Stick to 32-bit MSIs */
4762306a36Sopenharmony_ci#define AZX_DCAPS_SEPARATE_STREAM_TAG	(1 << 30) /* capture and playback use separate stream tag */
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cienum {
5062306a36Sopenharmony_ci	AZX_SNOOP_TYPE_NONE,
5162306a36Sopenharmony_ci	AZX_SNOOP_TYPE_SCH,
5262306a36Sopenharmony_ci	AZX_SNOOP_TYPE_ATI,
5362306a36Sopenharmony_ci	AZX_SNOOP_TYPE_NVIDIA,
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistruct azx_dev {
5762306a36Sopenharmony_ci	struct hdac_stream core;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	unsigned int irq_pending:1;
6062306a36Sopenharmony_ci	/*
6162306a36Sopenharmony_ci	 * For VIA:
6262306a36Sopenharmony_ci	 *  A flag to ensure DMA position is 0
6362306a36Sopenharmony_ci	 *  when link position is not greater than FIFO size
6462306a36Sopenharmony_ci	 */
6562306a36Sopenharmony_ci	unsigned int insufficient:1;
6662306a36Sopenharmony_ci};
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define azx_stream(dev)		(&(dev)->core)
6962306a36Sopenharmony_ci#define stream_to_azx_dev(s)	container_of(s, struct azx_dev, core)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistruct azx;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci/* Functions to read/write to hda registers. */
7462306a36Sopenharmony_cistruct hda_controller_ops {
7562306a36Sopenharmony_ci	/* Disable msi if supported, PCI only */
7662306a36Sopenharmony_ci	int (*disable_msi_reset_irq)(struct azx *);
7762306a36Sopenharmony_ci	/* Check if current position is acceptable */
7862306a36Sopenharmony_ci	int (*position_check)(struct azx *chip, struct azx_dev *azx_dev);
7962306a36Sopenharmony_ci	/* enable/disable the link power */
8062306a36Sopenharmony_ci	int (*link_power)(struct azx *chip, bool enable);
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistruct azx_pcm {
8462306a36Sopenharmony_ci	struct azx *chip;
8562306a36Sopenharmony_ci	struct snd_pcm *pcm;
8662306a36Sopenharmony_ci	struct hda_codec *codec;
8762306a36Sopenharmony_ci	struct hda_pcm *info;
8862306a36Sopenharmony_ci	struct list_head list;
8962306a36Sopenharmony_ci};
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_citypedef unsigned int (*azx_get_pos_callback_t)(struct azx *, struct azx_dev *);
9262306a36Sopenharmony_citypedef int (*azx_get_delay_callback_t)(struct azx *, struct azx_dev *, unsigned int pos);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistruct azx {
9562306a36Sopenharmony_ci	struct hda_bus bus;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	struct snd_card *card;
9862306a36Sopenharmony_ci	struct pci_dev *pci;
9962306a36Sopenharmony_ci	int dev_index;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/* chip type specific */
10262306a36Sopenharmony_ci	int driver_type;
10362306a36Sopenharmony_ci	unsigned int driver_caps;
10462306a36Sopenharmony_ci	int playback_streams;
10562306a36Sopenharmony_ci	int playback_index_offset;
10662306a36Sopenharmony_ci	int capture_streams;
10762306a36Sopenharmony_ci	int capture_index_offset;
10862306a36Sopenharmony_ci	int num_streams;
10962306a36Sopenharmony_ci	int jackpoll_interval; /* jack poll interval in jiffies */
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	/* Register interaction. */
11262306a36Sopenharmony_ci	const struct hda_controller_ops *ops;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	/* position adjustment callbacks */
11562306a36Sopenharmony_ci	azx_get_pos_callback_t get_position[2];
11662306a36Sopenharmony_ci	azx_get_delay_callback_t get_delay[2];
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	/* locks */
11962306a36Sopenharmony_ci	struct mutex open_mutex; /* Prevents concurrent open/close operations */
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/* PCM */
12262306a36Sopenharmony_ci	struct list_head pcm_list; /* azx_pcm list */
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	/* HD codec */
12562306a36Sopenharmony_ci	int  codec_probe_mask; /* copied from probe_mask option */
12662306a36Sopenharmony_ci	unsigned int beep_mode;
12762306a36Sopenharmony_ci	bool ctl_dev_id;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci#ifdef CONFIG_SND_HDA_PATCH_LOADER
13062306a36Sopenharmony_ci	const struct firmware *fw;
13162306a36Sopenharmony_ci#endif
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	/* flags */
13462306a36Sopenharmony_ci	int bdl_pos_adj;
13562306a36Sopenharmony_ci	unsigned int running:1;
13662306a36Sopenharmony_ci	unsigned int fallback_to_single_cmd:1;
13762306a36Sopenharmony_ci	unsigned int single_cmd:1;
13862306a36Sopenharmony_ci	unsigned int msi:1;
13962306a36Sopenharmony_ci	unsigned int probing:1; /* codec probing phase */
14062306a36Sopenharmony_ci	unsigned int snoop:1;
14162306a36Sopenharmony_ci	unsigned int uc_buffer:1; /* non-cached pages for stream buffers */
14262306a36Sopenharmony_ci	unsigned int align_buffer_size:1;
14362306a36Sopenharmony_ci	unsigned int disabled:1; /* disabled by vga_switcheroo */
14462306a36Sopenharmony_ci	unsigned int pm_prepared:1;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	/* GTS present */
14762306a36Sopenharmony_ci	unsigned int gts_present:1;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci#ifdef CONFIG_SND_HDA_DSP_LOADER
15062306a36Sopenharmony_ci	struct azx_dev saved_azx_dev;
15162306a36Sopenharmony_ci#endif
15262306a36Sopenharmony_ci};
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci#define azx_bus(chip)	(&(chip)->bus.core)
15562306a36Sopenharmony_ci#define bus_to_azx(_bus)	container_of(_bus, struct azx, bus.core)
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_cistatic inline bool azx_snoop(struct azx *chip)
15862306a36Sopenharmony_ci{
15962306a36Sopenharmony_ci	return !IS_ENABLED(CONFIG_X86) || chip->snoop;
16062306a36Sopenharmony_ci}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci/*
16362306a36Sopenharmony_ci * macros for easy use
16462306a36Sopenharmony_ci */
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci#define azx_writel(chip, reg, value) \
16762306a36Sopenharmony_ci	snd_hdac_chip_writel(azx_bus(chip), reg, value)
16862306a36Sopenharmony_ci#define azx_readl(chip, reg) \
16962306a36Sopenharmony_ci	snd_hdac_chip_readl(azx_bus(chip), reg)
17062306a36Sopenharmony_ci#define azx_writew(chip, reg, value) \
17162306a36Sopenharmony_ci	snd_hdac_chip_writew(azx_bus(chip), reg, value)
17262306a36Sopenharmony_ci#define azx_readw(chip, reg) \
17362306a36Sopenharmony_ci	snd_hdac_chip_readw(azx_bus(chip), reg)
17462306a36Sopenharmony_ci#define azx_writeb(chip, reg, value) \
17562306a36Sopenharmony_ci	snd_hdac_chip_writeb(azx_bus(chip), reg, value)
17662306a36Sopenharmony_ci#define azx_readb(chip, reg) \
17762306a36Sopenharmony_ci	snd_hdac_chip_readb(azx_bus(chip), reg)
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci#define azx_has_pm_runtime(chip) \
18062306a36Sopenharmony_ci	((chip)->driver_caps & AZX_DCAPS_PM_RUNTIME)
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci/* PCM setup */
18362306a36Sopenharmony_cistatic inline struct azx_dev *get_azx_dev(struct snd_pcm_substream *substream)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	return substream->runtime->private_data;
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ciunsigned int azx_get_position(struct azx *chip, struct azx_dev *azx_dev);
18862306a36Sopenharmony_ciunsigned int azx_get_pos_lpib(struct azx *chip, struct azx_dev *azx_dev);
18962306a36Sopenharmony_ciunsigned int azx_get_pos_posbuf(struct azx *chip, struct azx_dev *azx_dev);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci/* Stream control. */
19262306a36Sopenharmony_civoid azx_stop_all_streams(struct azx *chip);
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci/* Allocation functions. */
19562306a36Sopenharmony_ci#define azx_alloc_stream_pages(chip) \
19662306a36Sopenharmony_ci	snd_hdac_bus_alloc_stream_pages(azx_bus(chip))
19762306a36Sopenharmony_ci#define azx_free_stream_pages(chip) \
19862306a36Sopenharmony_ci	snd_hdac_bus_free_stream_pages(azx_bus(chip))
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci/* Low level azx interface */
20162306a36Sopenharmony_civoid azx_init_chip(struct azx *chip, bool full_reset);
20262306a36Sopenharmony_civoid azx_stop_chip(struct azx *chip);
20362306a36Sopenharmony_ci#define azx_enter_link_reset(chip) \
20462306a36Sopenharmony_ci	snd_hdac_bus_enter_link_reset(azx_bus(chip))
20562306a36Sopenharmony_ciirqreturn_t azx_interrupt(int irq, void *dev_id);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci/* Codec interface */
20862306a36Sopenharmony_ciint azx_bus_init(struct azx *chip, const char *model);
20962306a36Sopenharmony_ciint azx_probe_codecs(struct azx *chip, unsigned int max_slots);
21062306a36Sopenharmony_ciint azx_codec_configure(struct azx *chip);
21162306a36Sopenharmony_ciint azx_init_streams(struct azx *chip);
21262306a36Sopenharmony_civoid azx_free_streams(struct azx *chip);
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci#endif /* __SOUND_HDA_CONTROLLER_H */
215