xref: /openbmc/linux/include/sound/hdaudio.h (revision 18d43c9b)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2e3d280fcSTakashi Iwai /*
3e3d280fcSTakashi Iwai  * HD-audio core stuff
4e3d280fcSTakashi Iwai  */
5e3d280fcSTakashi Iwai 
6e3d280fcSTakashi Iwai #ifndef __SOUND_HDAUDIO_H
7e3d280fcSTakashi Iwai #define __SOUND_HDAUDIO_H
8e3d280fcSTakashi Iwai 
9e3d280fcSTakashi Iwai #include <linux/device.h>
1014752412STakashi Iwai #include <linux/interrupt.h>
11feb20faeSTakashi Iwai #include <linux/pm_runtime.h>
1214752412STakashi Iwai #include <linux/timecounter.h>
1314752412STakashi Iwai #include <sound/core.h>
14a6ea5fe9STakashi Iwai #include <sound/pcm.h>
1514752412STakashi Iwai #include <sound/memalloc.h>
16d068ebc2STakashi Iwai #include <sound/hda_verbs.h>
1798d8fc6cSMengdong Lin #include <drm/i915_component.h>
18d068ebc2STakashi Iwai 
197639a06cSTakashi Iwai /* codec node id */
207639a06cSTakashi Iwai typedef u16 hda_nid_t;
217639a06cSTakashi Iwai 
22d068ebc2STakashi Iwai struct hdac_bus;
2314752412STakashi Iwai struct hdac_stream;
24d068ebc2STakashi Iwai struct hdac_device;
25d068ebc2STakashi Iwai struct hdac_driver;
263256be65STakashi Iwai struct hdac_widget_tree;
27da23ac1eSSubhransu S. Prusty struct hda_device_id;
28e3d280fcSTakashi Iwai 
29e3d280fcSTakashi Iwai /*
30e3d280fcSTakashi Iwai  * exported bus type
31e3d280fcSTakashi Iwai  */
32e3d280fcSTakashi Iwai extern struct bus_type snd_hda_bus_type;
33e3d280fcSTakashi Iwai 
34e3d280fcSTakashi Iwai /*
3571fc4c7eSTakashi Iwai  * generic arrays
3671fc4c7eSTakashi Iwai  */
3771fc4c7eSTakashi Iwai struct snd_array {
3871fc4c7eSTakashi Iwai 	unsigned int used;
3971fc4c7eSTakashi Iwai 	unsigned int alloced;
4071fc4c7eSTakashi Iwai 	unsigned int elem_size;
4171fc4c7eSTakashi Iwai 	unsigned int alloc_align;
4271fc4c7eSTakashi Iwai 	void *list;
4371fc4c7eSTakashi Iwai };
4471fc4c7eSTakashi Iwai 
4571fc4c7eSTakashi Iwai /*
46e3d280fcSTakashi Iwai  * HD-audio codec base device
47e3d280fcSTakashi Iwai  */
48e3d280fcSTakashi Iwai struct hdac_device {
49e3d280fcSTakashi Iwai 	struct device dev;
50e3d280fcSTakashi Iwai 	int type;
51d068ebc2STakashi Iwai 	struct hdac_bus *bus;
52d068ebc2STakashi Iwai 	unsigned int addr;		/* codec address */
53d068ebc2STakashi Iwai 	struct list_head list;		/* list point for bus codec_list */
547639a06cSTakashi Iwai 
557639a06cSTakashi Iwai 	hda_nid_t afg;			/* AFG node id */
567639a06cSTakashi Iwai 	hda_nid_t mfg;			/* MFG node id */
577639a06cSTakashi Iwai 
587639a06cSTakashi Iwai 	/* ids */
597639a06cSTakashi Iwai 	unsigned int vendor_id;
607639a06cSTakashi Iwai 	unsigned int subsystem_id;
617639a06cSTakashi Iwai 	unsigned int revision_id;
627639a06cSTakashi Iwai 	unsigned int afg_function_id;
637639a06cSTakashi Iwai 	unsigned int mfg_function_id;
647639a06cSTakashi Iwai 	unsigned int afg_unsol:1;
657639a06cSTakashi Iwai 	unsigned int mfg_unsol:1;
667639a06cSTakashi Iwai 
677639a06cSTakashi Iwai 	unsigned int power_caps;	/* FG power caps */
687639a06cSTakashi Iwai 
697639a06cSTakashi Iwai 	const char *vendor_name;	/* codec vendor name */
707639a06cSTakashi Iwai 	const char *chip_name;		/* codec chip name */
717639a06cSTakashi Iwai 
7205852448STakashi Iwai 	/* verb exec op override */
7305852448STakashi Iwai 	int (*exec_verb)(struct hdac_device *dev, unsigned int cmd,
7405852448STakashi Iwai 			 unsigned int flags, unsigned int *res);
7505852448STakashi Iwai 
767639a06cSTakashi Iwai 	/* widgets */
777639a06cSTakashi Iwai 	unsigned int num_nodes;
787639a06cSTakashi Iwai 	hda_nid_t start_nid, end_nid;
797639a06cSTakashi Iwai 
807639a06cSTakashi Iwai 	/* misc flags */
817639a06cSTakashi Iwai 	atomic_t in_pm;		/* suspend/resume being performed */
823256be65STakashi Iwai 
833256be65STakashi Iwai 	/* sysfs */
843256be65STakashi Iwai 	struct hdac_widget_tree *widgets;
854d75faa0STakashi Iwai 
864d75faa0STakashi Iwai 	/* regmap */
874d75faa0STakashi Iwai 	struct regmap *regmap;
885e56bceaSTakashi Iwai 	struct snd_array vendor_verbs;
894d75faa0STakashi Iwai 	bool lazy_cache:1;	/* don't wake up for writes */
90faa75f8aSTakashi Iwai 	bool caps_overwriting:1; /* caps overwrite being in process */
9140ba66a7STakashi Iwai 	bool cache_coef:1;	/* cache COEF read/write too */
92e3d280fcSTakashi Iwai };
93e3d280fcSTakashi Iwai 
94e3d280fcSTakashi Iwai /* device/driver type used for matching */
95e3d280fcSTakashi Iwai enum {
96e3d280fcSTakashi Iwai 	HDA_DEV_CORE,
97e3d280fcSTakashi Iwai 	HDA_DEV_LEGACY,
98c1cc18b1SRamesh Babu 	HDA_DEV_ASOC,
99e3d280fcSTakashi Iwai };
100e3d280fcSTakashi Iwai 
1017639a06cSTakashi Iwai /* direction */
1027639a06cSTakashi Iwai enum {
1037639a06cSTakashi Iwai 	HDA_INPUT, HDA_OUTPUT
1047639a06cSTakashi Iwai };
1057639a06cSTakashi Iwai 
106e3d280fcSTakashi Iwai #define dev_to_hdac_dev(_dev)	container_of(_dev, struct hdac_device, dev)
107e3d280fcSTakashi Iwai 
1087639a06cSTakashi Iwai int snd_hdac_device_init(struct hdac_device *dev, struct hdac_bus *bus,
1097639a06cSTakashi Iwai 			 const char *name, unsigned int addr);
1107639a06cSTakashi Iwai void snd_hdac_device_exit(struct hdac_device *dev);
1113256be65STakashi Iwai int snd_hdac_device_register(struct hdac_device *codec);
1123256be65STakashi Iwai void snd_hdac_device_unregister(struct hdac_device *codec);
113ded255beSTakashi Iwai int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name);
1144f9e0c38STakashi Iwai int snd_hdac_codec_modalias(struct hdac_device *hdac, char *buf, size_t size);
1157639a06cSTakashi Iwai 
1169780ded3STakashi Iwai int snd_hdac_refresh_widgets(struct hdac_device *codec, bool sysfs);
1177639a06cSTakashi Iwai 
1187639a06cSTakashi Iwai unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
1197639a06cSTakashi Iwai 			       unsigned int verb, unsigned int parm);
12005852448STakashi Iwai int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
12105852448STakashi Iwai 		       unsigned int flags, unsigned int *res);
1227639a06cSTakashi Iwai int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
1237639a06cSTakashi Iwai 		  unsigned int verb, unsigned int parm, unsigned int *res);
12401ed3c06STakashi Iwai int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
12501ed3c06STakashi Iwai 			unsigned int *res);
1269ba17b4dSTakashi Iwai int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
1279ba17b4dSTakashi Iwai 				int parm);
128faa75f8aSTakashi Iwai int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
129faa75f8aSTakashi Iwai 			   unsigned int parm, unsigned int val);
1307639a06cSTakashi Iwai int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
1317639a06cSTakashi Iwai 			     hda_nid_t *conn_list, int max_conns);
1327639a06cSTakashi Iwai int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
1337639a06cSTakashi Iwai 			   hda_nid_t *start_id);
134b7d023e1STakashi Iwai unsigned int snd_hdac_calc_stream_format(unsigned int rate,
135b7d023e1STakashi Iwai 					 unsigned int channels,
136a6ea5fe9STakashi Iwai 					 snd_pcm_format_t format,
137b7d023e1STakashi Iwai 					 unsigned int maxbps,
138b7d023e1STakashi Iwai 					 unsigned short spdif_ctls);
139b7d023e1STakashi Iwai int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
140b7d023e1STakashi Iwai 				u32 *ratesp, u64 *formatsp, unsigned int *bpsp);
141b7d023e1STakashi Iwai bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
142b7d023e1STakashi Iwai 				  unsigned int format);
1437639a06cSTakashi Iwai 
1441b5e6167SSubhransu S. Prusty int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1451b5e6167SSubhransu S. Prusty 			int flags, unsigned int verb, unsigned int parm);
1461b5e6167SSubhransu S. Prusty int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1471b5e6167SSubhransu S. Prusty 			int flags, unsigned int verb, unsigned int parm);
1481b5e6167SSubhransu S. Prusty bool snd_hdac_check_power_state(struct hdac_device *hdac,
1491b5e6167SSubhransu S. Prusty 		hda_nid_t nid, unsigned int target_state);
15009787492SAbhijeet Kumar unsigned int snd_hdac_sync_power_state(struct hdac_device *hdac,
15109787492SAbhijeet Kumar 		      hda_nid_t nid, unsigned int target_state);
15201ed3c06STakashi Iwai /**
15301ed3c06STakashi Iwai  * snd_hdac_read_parm - read a codec parameter
15401ed3c06STakashi Iwai  * @codec: the codec object
15501ed3c06STakashi Iwai  * @nid: NID to read a parameter
15601ed3c06STakashi Iwai  * @parm: parameter to read
15701ed3c06STakashi Iwai  *
15801ed3c06STakashi Iwai  * Returns -1 for error.  If you need to distinguish the error more
15901ed3c06STakashi Iwai  * strictly, use _snd_hdac_read_parm() directly.
16001ed3c06STakashi Iwai  */
16101ed3c06STakashi Iwai static inline int snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid,
16201ed3c06STakashi Iwai 				     int parm)
16301ed3c06STakashi Iwai {
16401ed3c06STakashi Iwai 	unsigned int val;
16501ed3c06STakashi Iwai 
16601ed3c06STakashi Iwai 	return _snd_hdac_read_parm(codec, nid, parm, &val) < 0 ? -1 : val;
16701ed3c06STakashi Iwai }
16801ed3c06STakashi Iwai 
1697639a06cSTakashi Iwai #ifdef CONFIG_PM
170fbce23a0STakashi Iwai int snd_hdac_power_up(struct hdac_device *codec);
171fbce23a0STakashi Iwai int snd_hdac_power_down(struct hdac_device *codec);
172fbce23a0STakashi Iwai int snd_hdac_power_up_pm(struct hdac_device *codec);
173fbce23a0STakashi Iwai int snd_hdac_power_down_pm(struct hdac_device *codec);
174fc4f000bSTakashi Iwai int snd_hdac_keep_power_up(struct hdac_device *codec);
175feb20faeSTakashi Iwai 
176feb20faeSTakashi Iwai /* call this at entering into suspend/resume callbacks in codec driver */
177feb20faeSTakashi Iwai static inline void snd_hdac_enter_pm(struct hdac_device *codec)
178feb20faeSTakashi Iwai {
179feb20faeSTakashi Iwai 	atomic_inc(&codec->in_pm);
180feb20faeSTakashi Iwai }
181feb20faeSTakashi Iwai 
182feb20faeSTakashi Iwai /* call this at leaving from suspend/resume callbacks in codec driver */
183feb20faeSTakashi Iwai static inline void snd_hdac_leave_pm(struct hdac_device *codec)
184feb20faeSTakashi Iwai {
185feb20faeSTakashi Iwai 	atomic_dec(&codec->in_pm);
186feb20faeSTakashi Iwai }
187feb20faeSTakashi Iwai 
188feb20faeSTakashi Iwai static inline bool snd_hdac_is_in_pm(struct hdac_device *codec)
189feb20faeSTakashi Iwai {
190feb20faeSTakashi Iwai 	return atomic_read(&codec->in_pm);
191feb20faeSTakashi Iwai }
192feb20faeSTakashi Iwai 
193feb20faeSTakashi Iwai static inline bool snd_hdac_is_power_on(struct hdac_device *codec)
194feb20faeSTakashi Iwai {
195feb20faeSTakashi Iwai 	return !pm_runtime_suspended(&codec->dev);
196feb20faeSTakashi Iwai }
1977639a06cSTakashi Iwai #else
198fbce23a0STakashi Iwai static inline int snd_hdac_power_up(struct hdac_device *codec) { return 0; }
199fbce23a0STakashi Iwai static inline int snd_hdac_power_down(struct hdac_device *codec) { return 0; }
200fbce23a0STakashi Iwai static inline int snd_hdac_power_up_pm(struct hdac_device *codec) { return 0; }
201fbce23a0STakashi Iwai static inline int snd_hdac_power_down_pm(struct hdac_device *codec) { return 0; }
202fc4f000bSTakashi Iwai static inline int snd_hdac_keep_power_up(struct hdac_device *codec) { return 0; }
203feb20faeSTakashi Iwai static inline void snd_hdac_enter_pm(struct hdac_device *codec) {}
204feb20faeSTakashi Iwai static inline void snd_hdac_leave_pm(struct hdac_device *codec) {}
205feb20faeSTakashi Iwai static inline bool snd_hdac_is_in_pm(struct hdac_device *codec) { return 0; }
206feb20faeSTakashi Iwai static inline bool snd_hdac_is_power_on(struct hdac_device *codec) { return 1; }
2077639a06cSTakashi Iwai #endif
2087639a06cSTakashi Iwai 
209e3d280fcSTakashi Iwai /*
210e3d280fcSTakashi Iwai  * HD-audio codec base driver
211e3d280fcSTakashi Iwai  */
212e3d280fcSTakashi Iwai struct hdac_driver {
213e3d280fcSTakashi Iwai 	struct device_driver driver;
214e3d280fcSTakashi Iwai 	int type;
215ec71efc9SVinod Koul 	const struct hda_device_id *id_table;
216e3d280fcSTakashi Iwai 	int (*match)(struct hdac_device *dev, struct hdac_driver *drv);
217d068ebc2STakashi Iwai 	void (*unsol_event)(struct hdac_device *dev, unsigned int event);
218e1df9317SRakesh Ughreja 
219e1df9317SRakesh Ughreja 	/* fields used by ext bus APIs */
220e1df9317SRakesh Ughreja 	int (*probe)(struct hdac_device *dev);
221e1df9317SRakesh Ughreja 	int (*remove)(struct hdac_device *dev);
222e1df9317SRakesh Ughreja 	void (*shutdown)(struct hdac_device *dev);
223e3d280fcSTakashi Iwai };
224e3d280fcSTakashi Iwai 
225e3d280fcSTakashi Iwai #define drv_to_hdac_driver(_drv) container_of(_drv, struct hdac_driver, driver)
226e3d280fcSTakashi Iwai 
227ec71efc9SVinod Koul const struct hda_device_id *
228ec71efc9SVinod Koul hdac_get_device_id(struct hdac_device *hdev, struct hdac_driver *drv);
229ec71efc9SVinod Koul 
230d068ebc2STakashi Iwai /*
23114752412STakashi Iwai  * Bus verb operators
232d068ebc2STakashi Iwai  */
233d068ebc2STakashi Iwai struct hdac_bus_ops {
234d068ebc2STakashi Iwai 	/* send a single command */
235d068ebc2STakashi Iwai 	int (*command)(struct hdac_bus *bus, unsigned int cmd);
236d068ebc2STakashi Iwai 	/* get a response from the last command */
237d068ebc2STakashi Iwai 	int (*get_response)(struct hdac_bus *bus, unsigned int addr,
238d068ebc2STakashi Iwai 			    unsigned int *res);
239d068ebc2STakashi Iwai };
240d068ebc2STakashi Iwai 
24114752412STakashi Iwai /*
242cb04ba33SRakesh Ughreja  * ops used for ASoC HDA codec drivers
243cb04ba33SRakesh Ughreja  */
244cb04ba33SRakesh Ughreja struct hdac_ext_bus_ops {
245cb04ba33SRakesh Ughreja 	int (*hdev_attach)(struct hdac_device *hdev);
246cb04ba33SRakesh Ughreja 	int (*hdev_detach)(struct hdac_device *hdev);
247cb04ba33SRakesh Ughreja };
248cb04ba33SRakesh Ughreja 
249cb04ba33SRakesh Ughreja /*
25014752412STakashi Iwai  * Lowlevel I/O operators
25114752412STakashi Iwai  */
25214752412STakashi Iwai struct hdac_io_ops {
25314752412STakashi Iwai 	/* mapped register accesses */
25414752412STakashi Iwai 	void (*reg_writel)(u32 value, u32 __iomem *addr);
25514752412STakashi Iwai 	u32 (*reg_readl)(u32 __iomem *addr);
25614752412STakashi Iwai 	void (*reg_writew)(u16 value, u16 __iomem *addr);
25714752412STakashi Iwai 	u16 (*reg_readw)(u16 __iomem *addr);
25814752412STakashi Iwai 	void (*reg_writeb)(u8 value, u8 __iomem *addr);
25914752412STakashi Iwai 	u8 (*reg_readb)(u8 __iomem *addr);
2608f3f600bSTakashi Iwai 	/* Allocation ops */
2618f3f600bSTakashi Iwai 	int (*dma_alloc_pages)(struct hdac_bus *bus, int type, size_t size,
2628f3f600bSTakashi Iwai 			       struct snd_dma_buffer *buf);
2638f3f600bSTakashi Iwai 	void (*dma_free_pages)(struct hdac_bus *bus,
2648f3f600bSTakashi Iwai 			       struct snd_dma_buffer *buf);
26514752412STakashi Iwai };
266d068ebc2STakashi Iwai 
26714752412STakashi Iwai #define HDA_UNSOL_QUEUE_SIZE	64
26814752412STakashi Iwai #define HDA_MAX_CODECS		8	/* limit by controller side */
26914752412STakashi Iwai 
27014752412STakashi Iwai /*
27114752412STakashi Iwai  * CORB/RIRB
27214752412STakashi Iwai  *
27314752412STakashi Iwai  * Each CORB entry is 4byte, RIRB is 8byte
27414752412STakashi Iwai  */
27514752412STakashi Iwai struct hdac_rb {
27614752412STakashi Iwai 	__le32 *buf;		/* virtual address of CORB/RIRB buffer */
27714752412STakashi Iwai 	dma_addr_t addr;	/* physical address of CORB/RIRB buffer */
27814752412STakashi Iwai 	unsigned short rp, wp;	/* RIRB read/write pointers */
27914752412STakashi Iwai 	int cmds[HDA_MAX_CODECS];	/* number of pending requests */
28014752412STakashi Iwai 	u32 res[HDA_MAX_CODECS];	/* last read value */
28114752412STakashi Iwai };
28214752412STakashi Iwai 
28314752412STakashi Iwai /*
28414752412STakashi Iwai  * HD-audio bus base driver
2856720b384SVinod Koul  *
2866720b384SVinod Koul  * @ppcap: pp capabilities pointer
2876720b384SVinod Koul  * @spbcap: SPIB capabilities pointer
2886720b384SVinod Koul  * @mlcap: MultiLink capabilities pointer
2896720b384SVinod Koul  * @gtscap: gts capabilities pointer
2906720b384SVinod Koul  * @drsmcap: dma resume capabilities pointer
29176f56faeSRakesh Ughreja  * @num_streams: streams supported
29276f56faeSRakesh Ughreja  * @idx: HDA link index
29376f56faeSRakesh Ughreja  * @hlink_list: link list of HDA links
29476f56faeSRakesh Ughreja  * @lock: lock for link mgmt
29576f56faeSRakesh Ughreja  * @cmd_dma_state: state of cmd DMAs: CORB and RIRB
29614752412STakashi Iwai  */
297d068ebc2STakashi Iwai struct hdac_bus {
298d068ebc2STakashi Iwai 	struct device *dev;
299d068ebc2STakashi Iwai 	const struct hdac_bus_ops *ops;
30014752412STakashi Iwai 	const struct hdac_io_ops *io_ops;
301cb04ba33SRakesh Ughreja 	const struct hdac_ext_bus_ops *ext_ops;
30214752412STakashi Iwai 
30314752412STakashi Iwai 	/* h/w resources */
30414752412STakashi Iwai 	unsigned long addr;
30514752412STakashi Iwai 	void __iomem *remap_addr;
30614752412STakashi Iwai 	int irq;
307d068ebc2STakashi Iwai 
3086720b384SVinod Koul 	void __iomem *ppcap;
3096720b384SVinod Koul 	void __iomem *spbcap;
3106720b384SVinod Koul 	void __iomem *mlcap;
3116720b384SVinod Koul 	void __iomem *gtscap;
3126720b384SVinod Koul 	void __iomem *drsmcap;
3136720b384SVinod Koul 
314d068ebc2STakashi Iwai 	/* codec linked list */
315d068ebc2STakashi Iwai 	struct list_head codec_list;
316d068ebc2STakashi Iwai 	unsigned int num_codecs;
317d068ebc2STakashi Iwai 
318d068ebc2STakashi Iwai 	/* link caddr -> codec */
319d068ebc2STakashi Iwai 	struct hdac_device *caddr_tbl[HDA_MAX_CODEC_ADDRESS + 1];
320d068ebc2STakashi Iwai 
321d068ebc2STakashi Iwai 	/* unsolicited event queue */
322d068ebc2STakashi Iwai 	u32 unsol_queue[HDA_UNSOL_QUEUE_SIZE * 2]; /* ring buffer */
323d068ebc2STakashi Iwai 	unsigned int unsol_rp, unsol_wp;
324d068ebc2STakashi Iwai 	struct work_struct unsol_work;
325d068ebc2STakashi Iwai 
32614752412STakashi Iwai 	/* bit flags of detected codecs */
32714752412STakashi Iwai 	unsigned long codec_mask;
32814752412STakashi Iwai 
329d068ebc2STakashi Iwai 	/* bit flags of powered codecs */
330d068ebc2STakashi Iwai 	unsigned long codec_powered;
331d068ebc2STakashi Iwai 
33214752412STakashi Iwai 	/* CORB/RIRB */
33314752412STakashi Iwai 	struct hdac_rb corb;
33414752412STakashi Iwai 	struct hdac_rb rirb;
33514752412STakashi Iwai 	unsigned int last_cmd[HDA_MAX_CODECS];	/* last sent command */
33614752412STakashi Iwai 
33714752412STakashi Iwai 	/* CORB/RIRB and position buffers */
33814752412STakashi Iwai 	struct snd_dma_buffer rb;
33914752412STakashi Iwai 	struct snd_dma_buffer posbuf;
34014752412STakashi Iwai 
34114752412STakashi Iwai 	/* hdac_stream linked list */
34214752412STakashi Iwai 	struct list_head stream_list;
34314752412STakashi Iwai 
34414752412STakashi Iwai 	/* operation state */
34514752412STakashi Iwai 	bool chip_init:1;		/* h/w initialized */
34614752412STakashi Iwai 
34714752412STakashi Iwai 	/* behavior flags */
348d068ebc2STakashi Iwai 	bool sync_write:1;		/* sync after verb write */
34914752412STakashi Iwai 	bool use_posbuf:1;		/* use position buffer */
35014752412STakashi Iwai 	bool snoop:1;			/* enable snooping */
35114752412STakashi Iwai 	bool align_bdle_4k:1;		/* BDLE align 4K boundary */
35214752412STakashi Iwai 	bool reverse_assign:1;		/* assign devices in reverse order */
35314752412STakashi Iwai 	bool corbrp_self_clear:1;	/* CORBRP clears itself after reset */
35414752412STakashi Iwai 
35514752412STakashi Iwai 	int bdl_pos_adj;		/* BDL position adjustment */
356d068ebc2STakashi Iwai 
357d068ebc2STakashi Iwai 	/* locks */
35814752412STakashi Iwai 	spinlock_t reg_lock;
359d068ebc2STakashi Iwai 	struct mutex cmd_mutex;
36098d8fc6cSMengdong Lin 
361ae891abeSTakashi Iwai 	/* DRM component interface */
362ae891abeSTakashi Iwai 	struct drm_audio_component *audio_component;
363029d92c2STakashi Iwai 	long display_power_status;
364029d92c2STakashi Iwai 	bool display_power_active;
36576f56faeSRakesh Ughreja 
36676f56faeSRakesh Ughreja 	/* parameters required for enhanced capabilities */
36776f56faeSRakesh Ughreja 	int num_streams;
36876f56faeSRakesh Ughreja 	int idx;
36976f56faeSRakesh Ughreja 
37076f56faeSRakesh Ughreja 	struct list_head hlink_list;
37176f56faeSRakesh Ughreja 
37276f56faeSRakesh Ughreja 	struct mutex lock;
37376f56faeSRakesh Ughreja 	bool cmd_dma_state;
37476f56faeSRakesh Ughreja 
375d068ebc2STakashi Iwai };
376d068ebc2STakashi Iwai 
377d068ebc2STakashi Iwai int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
37814752412STakashi Iwai 		      const struct hdac_bus_ops *ops,
37914752412STakashi Iwai 		      const struct hdac_io_ops *io_ops);
380d068ebc2STakashi Iwai void snd_hdac_bus_exit(struct hdac_bus *bus);
381d068ebc2STakashi Iwai int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
382d068ebc2STakashi Iwai 			   unsigned int cmd, unsigned int *res);
383d068ebc2STakashi Iwai int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
384d068ebc2STakashi Iwai 				    unsigned int cmd, unsigned int *res);
385d068ebc2STakashi Iwai void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex);
386d068ebc2STakashi Iwai 
387d068ebc2STakashi Iwai int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec);
388d068ebc2STakashi Iwai void snd_hdac_bus_remove_device(struct hdac_bus *bus,
389d068ebc2STakashi Iwai 				struct hdac_device *codec);
39018d43c9bSKeyon Jie void snd_hdac_bus_process_unsol_events(struct work_struct *work);
391d068ebc2STakashi Iwai 
3927639a06cSTakashi Iwai static inline void snd_hdac_codec_link_up(struct hdac_device *codec)
3937639a06cSTakashi Iwai {
3947639a06cSTakashi Iwai 	set_bit(codec->addr, &codec->bus->codec_powered);
3957639a06cSTakashi Iwai }
3967639a06cSTakashi Iwai 
3977639a06cSTakashi Iwai static inline void snd_hdac_codec_link_down(struct hdac_device *codec)
3987639a06cSTakashi Iwai {
3997639a06cSTakashi Iwai 	clear_bit(codec->addr, &codec->bus->codec_powered);
4007639a06cSTakashi Iwai }
4017639a06cSTakashi Iwai 
40214752412STakashi Iwai int snd_hdac_bus_send_cmd(struct hdac_bus *bus, unsigned int val);
40314752412STakashi Iwai int snd_hdac_bus_get_response(struct hdac_bus *bus, unsigned int addr,
40414752412STakashi Iwai 			      unsigned int *res);
4056720b384SVinod Koul int snd_hdac_bus_parse_capabilities(struct hdac_bus *bus);
40614752412STakashi Iwai 
40714752412STakashi Iwai bool snd_hdac_bus_init_chip(struct hdac_bus *bus, bool full_reset);
40814752412STakashi Iwai void snd_hdac_bus_stop_chip(struct hdac_bus *bus);
40914752412STakashi Iwai void snd_hdac_bus_init_cmd_io(struct hdac_bus *bus);
41014752412STakashi Iwai void snd_hdac_bus_stop_cmd_io(struct hdac_bus *bus);
41114752412STakashi Iwai void snd_hdac_bus_enter_link_reset(struct hdac_bus *bus);
41214752412STakashi Iwai void snd_hdac_bus_exit_link_reset(struct hdac_bus *bus);
41375383f8dSYu Zhao int snd_hdac_bus_reset_link(struct hdac_bus *bus, bool full_reset);
41414752412STakashi Iwai 
41514752412STakashi Iwai void snd_hdac_bus_update_rirb(struct hdac_bus *bus);
416473f4145STakashi Iwai int snd_hdac_bus_handle_stream_irq(struct hdac_bus *bus, unsigned int status,
41714752412STakashi Iwai 				    void (*ack)(struct hdac_bus *,
41814752412STakashi Iwai 						struct hdac_stream *));
41914752412STakashi Iwai 
420304dad30SJeeja KP int snd_hdac_bus_alloc_stream_pages(struct hdac_bus *bus);
421304dad30SJeeja KP void snd_hdac_bus_free_stream_pages(struct hdac_bus *bus);
422304dad30SJeeja KP 
42314752412STakashi Iwai /*
42414752412STakashi Iwai  * macros for easy use
42514752412STakashi Iwai  */
4262c1f8138STakashi Iwai #define _snd_hdac_chip_writeb(chip, reg, value) \
4272c1f8138STakashi Iwai 	((chip)->io_ops->reg_writeb(value, (chip)->remap_addr + (reg)))
4282c1f8138STakashi Iwai #define _snd_hdac_chip_readb(chip, reg) \
4292c1f8138STakashi Iwai 	((chip)->io_ops->reg_readb((chip)->remap_addr + (reg)))
4302c1f8138STakashi Iwai #define _snd_hdac_chip_writew(chip, reg, value) \
4312c1f8138STakashi Iwai 	((chip)->io_ops->reg_writew(value, (chip)->remap_addr + (reg)))
4322c1f8138STakashi Iwai #define _snd_hdac_chip_readw(chip, reg) \
4332c1f8138STakashi Iwai 	((chip)->io_ops->reg_readw((chip)->remap_addr + (reg)))
4342c1f8138STakashi Iwai #define _snd_hdac_chip_writel(chip, reg, value) \
4352c1f8138STakashi Iwai 	((chip)->io_ops->reg_writel(value, (chip)->remap_addr + (reg)))
4362c1f8138STakashi Iwai #define _snd_hdac_chip_readl(chip, reg) \
4372c1f8138STakashi Iwai 	((chip)->io_ops->reg_readl((chip)->remap_addr + (reg)))
43814752412STakashi Iwai 
43914752412STakashi Iwai /* read/write a register, pass without AZX_REG_ prefix */
44014752412STakashi Iwai #define snd_hdac_chip_writel(chip, reg, value) \
4412c1f8138STakashi Iwai 	_snd_hdac_chip_writel(chip, AZX_REG_ ## reg, value)
44214752412STakashi Iwai #define snd_hdac_chip_writew(chip, reg, value) \
4432c1f8138STakashi Iwai 	_snd_hdac_chip_writew(chip, AZX_REG_ ## reg, value)
44414752412STakashi Iwai #define snd_hdac_chip_writeb(chip, reg, value) \
4452c1f8138STakashi Iwai 	_snd_hdac_chip_writeb(chip, AZX_REG_ ## reg, value)
44614752412STakashi Iwai #define snd_hdac_chip_readl(chip, reg) \
4472c1f8138STakashi Iwai 	_snd_hdac_chip_readl(chip, AZX_REG_ ## reg)
44814752412STakashi Iwai #define snd_hdac_chip_readw(chip, reg) \
4492c1f8138STakashi Iwai 	_snd_hdac_chip_readw(chip, AZX_REG_ ## reg)
45014752412STakashi Iwai #define snd_hdac_chip_readb(chip, reg) \
4512c1f8138STakashi Iwai 	_snd_hdac_chip_readb(chip, AZX_REG_ ## reg)
45214752412STakashi Iwai 
45314752412STakashi Iwai /* update a register, pass without AZX_REG_ prefix */
45414752412STakashi Iwai #define snd_hdac_chip_updatel(chip, reg, mask, val) \
45514752412STakashi Iwai 	snd_hdac_chip_writel(chip, reg, \
45614752412STakashi Iwai 			     (snd_hdac_chip_readl(chip, reg) & ~(mask)) | (val))
45714752412STakashi Iwai #define snd_hdac_chip_updatew(chip, reg, mask, val) \
45814752412STakashi Iwai 	snd_hdac_chip_writew(chip, reg, \
45914752412STakashi Iwai 			     (snd_hdac_chip_readw(chip, reg) & ~(mask)) | (val))
46014752412STakashi Iwai #define snd_hdac_chip_updateb(chip, reg, mask, val) \
46114752412STakashi Iwai 	snd_hdac_chip_writeb(chip, reg, \
46214752412STakashi Iwai 			     (snd_hdac_chip_readb(chip, reg) & ~(mask)) | (val))
46314752412STakashi Iwai 
46414752412STakashi Iwai /*
46514752412STakashi Iwai  * HD-audio stream
46614752412STakashi Iwai  */
46714752412STakashi Iwai struct hdac_stream {
46814752412STakashi Iwai 	struct hdac_bus *bus;
46914752412STakashi Iwai 	struct snd_dma_buffer bdl; /* BDL buffer */
47014752412STakashi Iwai 	__le32 *posbuf;		/* position buffer pointer */
47114752412STakashi Iwai 	int direction;		/* playback / capture (SNDRV_PCM_STREAM_*) */
47214752412STakashi Iwai 
47314752412STakashi Iwai 	unsigned int bufsize;	/* size of the play buffer in bytes */
47414752412STakashi Iwai 	unsigned int period_bytes; /* size of the period in bytes */
47514752412STakashi Iwai 	unsigned int frags;	/* number for period in the play buffer */
47614752412STakashi Iwai 	unsigned int fifo_size;	/* FIFO size */
47714752412STakashi Iwai 
47814752412STakashi Iwai 	void __iomem *sd_addr;	/* stream descriptor pointer */
47914752412STakashi Iwai 
48014752412STakashi Iwai 	u32 sd_int_sta_mask;	/* stream int status mask */
48114752412STakashi Iwai 
48214752412STakashi Iwai 	/* pcm support */
48314752412STakashi Iwai 	struct snd_pcm_substream *substream;	/* assigned substream,
48414752412STakashi Iwai 						 * set in PCM open
48514752412STakashi Iwai 						 */
48614752412STakashi Iwai 	unsigned int format_val;	/* format value to be set in the
48714752412STakashi Iwai 					 * controller and the codec
48814752412STakashi Iwai 					 */
48914752412STakashi Iwai 	unsigned char stream_tag;	/* assigned stream */
49014752412STakashi Iwai 	unsigned char index;		/* stream index */
49114752412STakashi Iwai 	int assigned_key;		/* last device# key assigned to */
49214752412STakashi Iwai 
49314752412STakashi Iwai 	bool opened:1;
49414752412STakashi Iwai 	bool running:1;
4956d23c8f5STakashi Iwai 	bool prepared:1;
49614752412STakashi Iwai 	bool no_period_wakeup:1;
4978f3f600bSTakashi Iwai 	bool locked:1;
49814752412STakashi Iwai 
49914752412STakashi Iwai 	/* timestamp */
50014752412STakashi Iwai 	unsigned long start_wallclk;	/* start + minimum wallclk */
50114752412STakashi Iwai 	unsigned long period_wallclk;	/* wallclk for period */
50214752412STakashi Iwai 	struct timecounter  tc;
50314752412STakashi Iwai 	struct cyclecounter cc;
50414752412STakashi Iwai 	int delay_negative_threshold;
50514752412STakashi Iwai 
50614752412STakashi Iwai 	struct list_head list;
5078f3f600bSTakashi Iwai #ifdef CONFIG_SND_HDA_DSP_LOADER
5088f3f600bSTakashi Iwai 	/* DSP access mutex */
5098f3f600bSTakashi Iwai 	struct mutex dsp_mutex;
5108f3f600bSTakashi Iwai #endif
51114752412STakashi Iwai };
51214752412STakashi Iwai 
51314752412STakashi Iwai void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
51414752412STakashi Iwai 			  int idx, int direction, int tag);
51514752412STakashi Iwai struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
51614752412STakashi Iwai 					   struct snd_pcm_substream *substream);
51714752412STakashi Iwai void snd_hdac_stream_release(struct hdac_stream *azx_dev);
5184308c9b0SJeeja KP struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus,
5194308c9b0SJeeja KP 					int dir, int stream_tag);
52014752412STakashi Iwai 
52114752412STakashi Iwai int snd_hdac_stream_setup(struct hdac_stream *azx_dev);
52214752412STakashi Iwai void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev);
52314752412STakashi Iwai int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev);
52486f6501bSJeeja KP int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
52586f6501bSJeeja KP 				unsigned int format_val);
52614752412STakashi Iwai void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start);
52714752412STakashi Iwai void snd_hdac_stream_clear(struct hdac_stream *azx_dev);
52814752412STakashi Iwai void snd_hdac_stream_stop(struct hdac_stream *azx_dev);
52914752412STakashi Iwai void snd_hdac_stream_reset(struct hdac_stream *azx_dev);
53014752412STakashi Iwai void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
53114752412STakashi Iwai 				  unsigned int streams, unsigned int reg);
53214752412STakashi Iwai void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
53314752412STakashi Iwai 			  unsigned int streams);
53414752412STakashi Iwai void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
53514752412STakashi Iwai 				      unsigned int streams);
53614752412STakashi Iwai /*
53714752412STakashi Iwai  * macros for easy use
53814752412STakashi Iwai  */
53914752412STakashi Iwai #define _snd_hdac_stream_write(type, dev, reg, value)			\
54014752412STakashi Iwai 	((dev)->bus->io_ops->reg_write ## type(value, (dev)->sd_addr + (reg)))
54114752412STakashi Iwai #define _snd_hdac_stream_read(type, dev, reg)				\
54214752412STakashi Iwai 	((dev)->bus->io_ops->reg_read ## type((dev)->sd_addr + (reg)))
54314752412STakashi Iwai 
54414752412STakashi Iwai /* read/write a register, pass without AZX_REG_ prefix */
54514752412STakashi Iwai #define snd_hdac_stream_writel(dev, reg, value) \
54614752412STakashi Iwai 	_snd_hdac_stream_write(l, dev, AZX_REG_ ## reg, value)
54714752412STakashi Iwai #define snd_hdac_stream_writew(dev, reg, value) \
54814752412STakashi Iwai 	_snd_hdac_stream_write(w, dev, AZX_REG_ ## reg, value)
54914752412STakashi Iwai #define snd_hdac_stream_writeb(dev, reg, value) \
55014752412STakashi Iwai 	_snd_hdac_stream_write(b, dev, AZX_REG_ ## reg, value)
55114752412STakashi Iwai #define snd_hdac_stream_readl(dev, reg) \
55214752412STakashi Iwai 	_snd_hdac_stream_read(l, dev, AZX_REG_ ## reg)
55314752412STakashi Iwai #define snd_hdac_stream_readw(dev, reg) \
55414752412STakashi Iwai 	_snd_hdac_stream_read(w, dev, AZX_REG_ ## reg)
55514752412STakashi Iwai #define snd_hdac_stream_readb(dev, reg) \
55614752412STakashi Iwai 	_snd_hdac_stream_read(b, dev, AZX_REG_ ## reg)
55714752412STakashi Iwai 
55814752412STakashi Iwai /* update a register, pass without AZX_REG_ prefix */
55914752412STakashi Iwai #define snd_hdac_stream_updatel(dev, reg, mask, val) \
56014752412STakashi Iwai 	snd_hdac_stream_writel(dev, reg, \
56114752412STakashi Iwai 			       (snd_hdac_stream_readl(dev, reg) & \
56214752412STakashi Iwai 				~(mask)) | (val))
56314752412STakashi Iwai #define snd_hdac_stream_updatew(dev, reg, mask, val) \
56414752412STakashi Iwai 	snd_hdac_stream_writew(dev, reg, \
56514752412STakashi Iwai 			       (snd_hdac_stream_readw(dev, reg) & \
56614752412STakashi Iwai 				~(mask)) | (val))
56714752412STakashi Iwai #define snd_hdac_stream_updateb(dev, reg, mask, val) \
56814752412STakashi Iwai 	snd_hdac_stream_writeb(dev, reg, \
56914752412STakashi Iwai 			       (snd_hdac_stream_readb(dev, reg) & \
57014752412STakashi Iwai 				~(mask)) | (val))
57114752412STakashi Iwai 
5728f3f600bSTakashi Iwai #ifdef CONFIG_SND_HDA_DSP_LOADER
5738f3f600bSTakashi Iwai /* DSP lock helpers */
5748f3f600bSTakashi Iwai #define snd_hdac_dsp_lock_init(dev)	mutex_init(&(dev)->dsp_mutex)
5758f3f600bSTakashi Iwai #define snd_hdac_dsp_lock(dev)		mutex_lock(&(dev)->dsp_mutex)
5768f3f600bSTakashi Iwai #define snd_hdac_dsp_unlock(dev)	mutex_unlock(&(dev)->dsp_mutex)
5778f3f600bSTakashi Iwai #define snd_hdac_stream_is_locked(dev)	((dev)->locked)
5788f3f600bSTakashi Iwai /* DSP loader helpers */
5798f3f600bSTakashi Iwai int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
5808f3f600bSTakashi Iwai 			 unsigned int byte_size, struct snd_dma_buffer *bufp);
5818f3f600bSTakashi Iwai void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start);
5828f3f600bSTakashi Iwai void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
5838f3f600bSTakashi Iwai 			  struct snd_dma_buffer *dmab);
5848f3f600bSTakashi Iwai #else /* CONFIG_SND_HDA_DSP_LOADER */
5858f3f600bSTakashi Iwai #define snd_hdac_dsp_lock_init(dev)	do {} while (0)
5868f3f600bSTakashi Iwai #define snd_hdac_dsp_lock(dev)		do {} while (0)
5878f3f600bSTakashi Iwai #define snd_hdac_dsp_unlock(dev)	do {} while (0)
5888f3f600bSTakashi Iwai #define snd_hdac_stream_is_locked(dev)	0
5898f3f600bSTakashi Iwai 
5908f3f600bSTakashi Iwai static inline int
5918f3f600bSTakashi Iwai snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
5928f3f600bSTakashi Iwai 		     unsigned int byte_size, struct snd_dma_buffer *bufp)
5938f3f600bSTakashi Iwai {
5948f3f600bSTakashi Iwai 	return 0;
5958f3f600bSTakashi Iwai }
5968f3f600bSTakashi Iwai 
5978f3f600bSTakashi Iwai static inline void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
5988f3f600bSTakashi Iwai {
5998f3f600bSTakashi Iwai }
6008f3f600bSTakashi Iwai 
6018f3f600bSTakashi Iwai static inline void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
6028f3f600bSTakashi Iwai 					struct snd_dma_buffer *dmab)
6038f3f600bSTakashi Iwai {
6048f3f600bSTakashi Iwai }
6058f3f600bSTakashi Iwai #endif /* CONFIG_SND_HDA_DSP_LOADER */
6068f3f600bSTakashi Iwai 
6078f3f600bSTakashi Iwai 
60871fc4c7eSTakashi Iwai /*
60971fc4c7eSTakashi Iwai  * generic array helpers
61071fc4c7eSTakashi Iwai  */
61171fc4c7eSTakashi Iwai void *snd_array_new(struct snd_array *array);
61271fc4c7eSTakashi Iwai void snd_array_free(struct snd_array *array);
61371fc4c7eSTakashi Iwai static inline void snd_array_init(struct snd_array *array, unsigned int size,
61471fc4c7eSTakashi Iwai 				  unsigned int align)
61571fc4c7eSTakashi Iwai {
61671fc4c7eSTakashi Iwai 	array->elem_size = size;
61771fc4c7eSTakashi Iwai 	array->alloc_align = align;
61871fc4c7eSTakashi Iwai }
61971fc4c7eSTakashi Iwai 
62071fc4c7eSTakashi Iwai static inline void *snd_array_elem(struct snd_array *array, unsigned int idx)
62171fc4c7eSTakashi Iwai {
62271fc4c7eSTakashi Iwai 	return array->list + idx * array->elem_size;
62371fc4c7eSTakashi Iwai }
62471fc4c7eSTakashi Iwai 
62571fc4c7eSTakashi Iwai static inline unsigned int snd_array_index(struct snd_array *array, void *ptr)
62671fc4c7eSTakashi Iwai {
62771fc4c7eSTakashi Iwai 	return (unsigned long)(ptr - array->list) / array->elem_size;
62871fc4c7eSTakashi Iwai }
62971fc4c7eSTakashi Iwai 
630a9c2dfc8STakashi Iwai /* a helper macro to iterate for each snd_array element */
631a9c2dfc8STakashi Iwai #define snd_array_for_each(array, idx, ptr) \
632a9c2dfc8STakashi Iwai 	for ((idx) = 0, (ptr) = (array)->list; (idx) < (array)->used; \
633a9c2dfc8STakashi Iwai 	     (ptr) = snd_array_elem(array, ++(idx)))
634a9c2dfc8STakashi Iwai 
635e3d280fcSTakashi Iwai #endif /* __SOUND_HDAUDIO_H */
636