18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Common functionality for the alsa driver code base for HD Audio.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef __SOUND_HDA_CONTROLLER_H
78c2ecf20Sopenharmony_ci#define __SOUND_HDA_CONTROLLER_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/timecounter.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <sound/core.h>
128c2ecf20Sopenharmony_ci#include <sound/pcm.h>
138c2ecf20Sopenharmony_ci#include <sound/initval.h>
148c2ecf20Sopenharmony_ci#include <sound/hda_codec.h>
158c2ecf20Sopenharmony_ci#include <sound/hda_register.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define AZX_MAX_CODECS		HDA_MAX_CODECS
188c2ecf20Sopenharmony_ci#define AZX_DEFAULT_CODECS	4
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* driver quirks (capabilities) */
218c2ecf20Sopenharmony_ci/* bits 0-7 are used for indicating driver type */
228c2ecf20Sopenharmony_ci#define AZX_DCAPS_NO_TCSEL	(1 << 8)	/* No Intel TCSEL bit */
238c2ecf20Sopenharmony_ci#define AZX_DCAPS_NO_MSI	(1 << 9)	/* No MSI support */
248c2ecf20Sopenharmony_ci#define AZX_DCAPS_SNOOP_MASK	(3 << 10)	/* snoop type mask */
258c2ecf20Sopenharmony_ci#define AZX_DCAPS_SNOOP_OFF	(1 << 12)	/* snoop default off */
268c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_HDA_I915
278c2ecf20Sopenharmony_ci#define AZX_DCAPS_I915_COMPONENT (1 << 13)	/* bind with i915 gfx */
288c2ecf20Sopenharmony_ci#else
298c2ecf20Sopenharmony_ci#define AZX_DCAPS_I915_COMPONENT 0		/* NOP */
308c2ecf20Sopenharmony_ci#endif
318c2ecf20Sopenharmony_ci/* 14 unused */
328c2ecf20Sopenharmony_ci#define AZX_DCAPS_CTX_WORKAROUND (1 << 15)	/* X-Fi workaround */
338c2ecf20Sopenharmony_ci#define AZX_DCAPS_POSFIX_LPIB	(1 << 16)	/* Use LPIB as default */
348c2ecf20Sopenharmony_ci#define AZX_DCAPS_AMD_WORKAROUND (1 << 17)	/* AMD-specific workaround */
358c2ecf20Sopenharmony_ci#define AZX_DCAPS_NO_64BIT	(1 << 18)	/* No 64bit address */
368c2ecf20Sopenharmony_ci/* 19 unused */
378c2ecf20Sopenharmony_ci#define AZX_DCAPS_OLD_SSYNC	(1 << 20)	/* Old SSYNC reg for ICH */
388c2ecf20Sopenharmony_ci#define AZX_DCAPS_NO_ALIGN_BUFSIZE (1 << 21)	/* no buffer size alignment */
398c2ecf20Sopenharmony_ci/* 22 unused */
408c2ecf20Sopenharmony_ci#define AZX_DCAPS_4K_BDLE_BOUNDARY (1 << 23)	/* BDLE in 4k boundary */
418c2ecf20Sopenharmony_ci/* 24 unused */
428c2ecf20Sopenharmony_ci#define AZX_DCAPS_COUNT_LPIB_DELAY  (1 << 25)	/* Take LPIB as delay */
438c2ecf20Sopenharmony_ci#define AZX_DCAPS_PM_RUNTIME	(1 << 26)	/* runtime PM support */
448c2ecf20Sopenharmony_ci#define AZX_DCAPS_RETRY_PROBE	(1 << 27)	/* retry probe if no codec is configured */
458c2ecf20Sopenharmony_ci#define AZX_DCAPS_CORBRP_SELF_CLEAR (1 << 28)	/* CORBRP clears itself after reset */
468c2ecf20Sopenharmony_ci#define AZX_DCAPS_NO_MSI64      (1 << 29)	/* Stick to 32-bit MSIs */
478c2ecf20Sopenharmony_ci#define AZX_DCAPS_SEPARATE_STREAM_TAG	(1 << 30) /* capture and playback use separate stream tag */
488c2ecf20Sopenharmony_ci#define AZX_DCAPS_LS2X_WORKAROUND (1 << 31)	/* Loongson-2H workaround */
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cienum {
518c2ecf20Sopenharmony_ci	AZX_SNOOP_TYPE_NONE,
528c2ecf20Sopenharmony_ci	AZX_SNOOP_TYPE_SCH,
538c2ecf20Sopenharmony_ci	AZX_SNOOP_TYPE_ATI,
548c2ecf20Sopenharmony_ci	AZX_SNOOP_TYPE_NVIDIA,
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct azx_dev {
588c2ecf20Sopenharmony_ci	struct hdac_stream core;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	unsigned int irq_pending:1;
618c2ecf20Sopenharmony_ci	/*
628c2ecf20Sopenharmony_ci	 * For VIA:
638c2ecf20Sopenharmony_ci	 *  A flag to ensure DMA position is 0
648c2ecf20Sopenharmony_ci	 *  when link position is not greater than FIFO size
658c2ecf20Sopenharmony_ci	 */
668c2ecf20Sopenharmony_ci	unsigned int insufficient:1;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	/* For Loongson */
698c2ecf20Sopenharmony_ci	unsigned int fix_prvpos;
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define azx_stream(dev)		(&(dev)->core)
738c2ecf20Sopenharmony_ci#define stream_to_azx_dev(s)	container_of(s, struct azx_dev, core)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistruct azx;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/* Functions to read/write to hda registers. */
788c2ecf20Sopenharmony_cistruct hda_controller_ops {
798c2ecf20Sopenharmony_ci	/* Disable msi if supported, PCI only */
808c2ecf20Sopenharmony_ci	int (*disable_msi_reset_irq)(struct azx *);
818c2ecf20Sopenharmony_ci	void (*pcm_mmap_prepare)(struct snd_pcm_substream *substream,
828c2ecf20Sopenharmony_ci				 struct vm_area_struct *area);
838c2ecf20Sopenharmony_ci	/* Check if current position is acceptable */
848c2ecf20Sopenharmony_ci	int (*position_check)(struct azx *chip, struct azx_dev *azx_dev);
858c2ecf20Sopenharmony_ci	/* enable/disable the link power */
868c2ecf20Sopenharmony_ci	int (*link_power)(struct azx *chip, bool enable);
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistruct azx_pcm {
908c2ecf20Sopenharmony_ci	struct azx *chip;
918c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
928c2ecf20Sopenharmony_ci	struct hda_codec *codec;
938c2ecf20Sopenharmony_ci	struct hda_pcm *info;
948c2ecf20Sopenharmony_ci	struct list_head list;
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_citypedef unsigned int (*azx_get_pos_callback_t)(struct azx *, struct azx_dev *);
988c2ecf20Sopenharmony_citypedef int (*azx_get_delay_callback_t)(struct azx *, struct azx_dev *, unsigned int pos);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistruct azx {
1018c2ecf20Sopenharmony_ci	struct hda_bus bus;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	struct snd_card *card;
1048c2ecf20Sopenharmony_ci	struct pci_dev *pci;
1058c2ecf20Sopenharmony_ci	int dev_index;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* chip type specific */
1088c2ecf20Sopenharmony_ci	int driver_type;
1098c2ecf20Sopenharmony_ci	unsigned int driver_caps;
1108c2ecf20Sopenharmony_ci	int playback_streams;
1118c2ecf20Sopenharmony_ci	int playback_index_offset;
1128c2ecf20Sopenharmony_ci	int capture_streams;
1138c2ecf20Sopenharmony_ci	int capture_index_offset;
1148c2ecf20Sopenharmony_ci	int num_streams;
1158c2ecf20Sopenharmony_ci	int jackpoll_interval; /* jack poll interval in jiffies */
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* Register interaction. */
1188c2ecf20Sopenharmony_ci	const struct hda_controller_ops *ops;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* position adjustment callbacks */
1218c2ecf20Sopenharmony_ci	azx_get_pos_callback_t get_position[2];
1228c2ecf20Sopenharmony_ci	azx_get_delay_callback_t get_delay[2];
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/* locks */
1258c2ecf20Sopenharmony_ci	struct mutex open_mutex; /* Prevents concurrent open/close operations */
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/* PCM */
1288c2ecf20Sopenharmony_ci	struct list_head pcm_list; /* azx_pcm list */
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/* HD codec */
1318c2ecf20Sopenharmony_ci	int  codec_probe_mask; /* copied from probe_mask option */
1328c2ecf20Sopenharmony_ci	unsigned int beep_mode;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_HDA_PATCH_LOADER
1358c2ecf20Sopenharmony_ci	const struct firmware *fw;
1368c2ecf20Sopenharmony_ci#endif
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	/* flags */
1398c2ecf20Sopenharmony_ci	int bdl_pos_adj;
1408c2ecf20Sopenharmony_ci	unsigned int running:1;
1418c2ecf20Sopenharmony_ci	unsigned int fallback_to_single_cmd:1;
1428c2ecf20Sopenharmony_ci	unsigned int single_cmd:1;
1438c2ecf20Sopenharmony_ci	unsigned int msi:1;
1448c2ecf20Sopenharmony_ci	unsigned int probing:1; /* codec probing phase */
1458c2ecf20Sopenharmony_ci	unsigned int snoop:1;
1468c2ecf20Sopenharmony_ci	unsigned int uc_buffer:1; /* non-cached pages for stream buffers */
1478c2ecf20Sopenharmony_ci	unsigned int align_buffer_size:1;
1488c2ecf20Sopenharmony_ci	unsigned int region_requested:1;
1498c2ecf20Sopenharmony_ci	unsigned int disabled:1; /* disabled by vga_switcheroo */
1508c2ecf20Sopenharmony_ci	unsigned int pm_prepared:1;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/* GTS present */
1538c2ecf20Sopenharmony_ci	unsigned int gts_present:1;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_HDA_DSP_LOADER
1568c2ecf20Sopenharmony_ci	struct azx_dev saved_azx_dev;
1578c2ecf20Sopenharmony_ci#endif
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define azx_bus(chip)	(&(chip)->bus.core)
1618c2ecf20Sopenharmony_ci#define bus_to_azx(_bus)	container_of(_bus, struct azx, bus.core)
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic inline bool azx_snoop(struct azx *chip)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	return !IS_ENABLED(CONFIG_X86) || chip->snoop;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * macros for easy use
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#define azx_writel(chip, reg, value) \
1738c2ecf20Sopenharmony_ci	snd_hdac_chip_writel(azx_bus(chip), reg, value)
1748c2ecf20Sopenharmony_ci#define azx_readl(chip, reg) \
1758c2ecf20Sopenharmony_ci	snd_hdac_chip_readl(azx_bus(chip), reg)
1768c2ecf20Sopenharmony_ci#define azx_writew(chip, reg, value) \
1778c2ecf20Sopenharmony_ci	snd_hdac_chip_writew(azx_bus(chip), reg, value)
1788c2ecf20Sopenharmony_ci#define azx_readw(chip, reg) \
1798c2ecf20Sopenharmony_ci	snd_hdac_chip_readw(azx_bus(chip), reg)
1808c2ecf20Sopenharmony_ci#define azx_writeb(chip, reg, value) \
1818c2ecf20Sopenharmony_ci	snd_hdac_chip_writeb(azx_bus(chip), reg, value)
1828c2ecf20Sopenharmony_ci#define azx_readb(chip, reg) \
1838c2ecf20Sopenharmony_ci	snd_hdac_chip_readb(azx_bus(chip), reg)
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci#define azx_has_pm_runtime(chip) \
1868c2ecf20Sopenharmony_ci	((chip)->driver_caps & AZX_DCAPS_PM_RUNTIME)
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/* PCM setup */
1898c2ecf20Sopenharmony_cistatic inline struct azx_dev *get_azx_dev(struct snd_pcm_substream *substream)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	return substream->runtime->private_data;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ciunsigned int azx_get_position(struct azx *chip, struct azx_dev *azx_dev);
1948c2ecf20Sopenharmony_ciunsigned int azx_get_pos_lpib(struct azx *chip, struct azx_dev *azx_dev);
1958c2ecf20Sopenharmony_ciunsigned int azx_get_pos_posbuf(struct azx *chip, struct azx_dev *azx_dev);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/* Stream control. */
1988c2ecf20Sopenharmony_civoid azx_stop_all_streams(struct azx *chip);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/* Allocation functions. */
2018c2ecf20Sopenharmony_ci#define azx_alloc_stream_pages(chip) \
2028c2ecf20Sopenharmony_ci	snd_hdac_bus_alloc_stream_pages(azx_bus(chip))
2038c2ecf20Sopenharmony_ci#define azx_free_stream_pages(chip) \
2048c2ecf20Sopenharmony_ci	snd_hdac_bus_free_stream_pages(azx_bus(chip))
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci/* Low level azx interface */
2078c2ecf20Sopenharmony_civoid azx_init_chip(struct azx *chip, bool full_reset);
2088c2ecf20Sopenharmony_civoid azx_stop_chip(struct azx *chip);
2098c2ecf20Sopenharmony_ci#define azx_enter_link_reset(chip) \
2108c2ecf20Sopenharmony_ci	snd_hdac_bus_enter_link_reset(azx_bus(chip))
2118c2ecf20Sopenharmony_ciirqreturn_t azx_interrupt(int irq, void *dev_id);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/* Codec interface */
2148c2ecf20Sopenharmony_ciint azx_bus_init(struct azx *chip, const char *model);
2158c2ecf20Sopenharmony_ciint azx_probe_codecs(struct azx *chip, unsigned int max_slots);
2168c2ecf20Sopenharmony_ciint azx_codec_configure(struct azx *chip);
2178c2ecf20Sopenharmony_ciint azx_init_streams(struct azx *chip);
2188c2ecf20Sopenharmony_civoid azx_free_streams(struct azx *chip);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci#endif /* __SOUND_HDA_CONTROLLER_H */
221