162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef OXYGEN_H_INCLUDED
362306a36Sopenharmony_ci#define OXYGEN_H_INCLUDED
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <linux/mutex.h>
662306a36Sopenharmony_ci#include <linux/spinlock.h>
762306a36Sopenharmony_ci#include <linux/wait.h>
862306a36Sopenharmony_ci#include <linux/workqueue.h>
962306a36Sopenharmony_ci#include "oxygen_regs.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/* 1 << PCM_x == OXYGEN_CHANNEL_x */
1262306a36Sopenharmony_ci#define PCM_A		0
1362306a36Sopenharmony_ci#define PCM_B		1
1462306a36Sopenharmony_ci#define PCM_C		2
1562306a36Sopenharmony_ci#define PCM_SPDIF	3
1662306a36Sopenharmony_ci#define PCM_MULTICH	4
1762306a36Sopenharmony_ci#define PCM_AC97	5
1862306a36Sopenharmony_ci#define PCM_COUNT	6
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define OXYGEN_MCLKS(f_single, f_double, f_quad) ((MCLK_##f_single << 0) | \
2162306a36Sopenharmony_ci						  (MCLK_##f_double << 2) | \
2262306a36Sopenharmony_ci						  (MCLK_##f_quad   << 4))
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define OXYGEN_IO_SIZE	0x100
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define OXYGEN_EEPROM_ID	0x434d	/* "CM" */
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/* model-specific configuration of outputs/inputs */
2962306a36Sopenharmony_ci#define PLAYBACK_0_TO_I2S	0x0001
3062306a36Sopenharmony_ci     /* PLAYBACK_0_TO_AC97_0		not implemented */
3162306a36Sopenharmony_ci#define PLAYBACK_1_TO_SPDIF	0x0004
3262306a36Sopenharmony_ci#define PLAYBACK_2_TO_AC97_1	0x0008
3362306a36Sopenharmony_ci#define CAPTURE_0_FROM_I2S_1	0x0010
3462306a36Sopenharmony_ci#define CAPTURE_0_FROM_I2S_2	0x0020
3562306a36Sopenharmony_ci     /* CAPTURE_0_FROM_AC97_0		not implemented */
3662306a36Sopenharmony_ci#define CAPTURE_1_FROM_SPDIF	0x0080
3762306a36Sopenharmony_ci#define CAPTURE_2_FROM_I2S_2	0x0100
3862306a36Sopenharmony_ci#define CAPTURE_2_FROM_AC97_1	0x0200
3962306a36Sopenharmony_ci#define CAPTURE_3_FROM_I2S_3	0x0400
4062306a36Sopenharmony_ci#define MIDI_OUTPUT		0x0800
4162306a36Sopenharmony_ci#define MIDI_INPUT		0x1000
4262306a36Sopenharmony_ci#define AC97_CD_INPUT		0x2000
4362306a36Sopenharmony_ci#define AC97_FMIC_SWITCH	0x4000
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cienum {
4662306a36Sopenharmony_ci	CONTROL_SPDIF_PCM,
4762306a36Sopenharmony_ci	CONTROL_SPDIF_INPUT_BITS,
4862306a36Sopenharmony_ci	CONTROL_MIC_CAPTURE_SWITCH,
4962306a36Sopenharmony_ci	CONTROL_LINE_CAPTURE_SWITCH,
5062306a36Sopenharmony_ci	CONTROL_CD_CAPTURE_SWITCH,
5162306a36Sopenharmony_ci	CONTROL_AUX_CAPTURE_SWITCH,
5262306a36Sopenharmony_ci	CONTROL_COUNT
5362306a36Sopenharmony_ci};
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define OXYGEN_PCI_SUBID(sv, sd) \
5662306a36Sopenharmony_ci	.vendor = PCI_VENDOR_ID_CMEDIA, \
5762306a36Sopenharmony_ci	.device = 0x8788, \
5862306a36Sopenharmony_ci	.subvendor = sv, \
5962306a36Sopenharmony_ci	.subdevice = sd
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define BROKEN_EEPROM_DRIVER_DATA ((unsigned long)-1)
6262306a36Sopenharmony_ci#define OXYGEN_PCI_SUBID_BROKEN_EEPROM \
6362306a36Sopenharmony_ci	OXYGEN_PCI_SUBID(PCI_VENDOR_ID_CMEDIA, 0x8788), \
6462306a36Sopenharmony_ci	.driver_data = BROKEN_EEPROM_DRIVER_DATA
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistruct pci_dev;
6762306a36Sopenharmony_cistruct pci_device_id;
6862306a36Sopenharmony_cistruct snd_card;
6962306a36Sopenharmony_cistruct snd_pcm_substream;
7062306a36Sopenharmony_cistruct snd_pcm_hardware;
7162306a36Sopenharmony_cistruct snd_pcm_hw_params;
7262306a36Sopenharmony_cistruct snd_kcontrol_new;
7362306a36Sopenharmony_cistruct snd_rawmidi;
7462306a36Sopenharmony_cistruct snd_info_buffer;
7562306a36Sopenharmony_cistruct oxygen;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_cistruct oxygen_model {
7862306a36Sopenharmony_ci	const char *shortname;
7962306a36Sopenharmony_ci	const char *longname;
8062306a36Sopenharmony_ci	const char *chip;
8162306a36Sopenharmony_ci	void (*init)(struct oxygen *chip);
8262306a36Sopenharmony_ci	int (*control_filter)(struct snd_kcontrol_new *template);
8362306a36Sopenharmony_ci	int (*mixer_init)(struct oxygen *chip);
8462306a36Sopenharmony_ci	void (*cleanup)(struct oxygen *chip);
8562306a36Sopenharmony_ci	void (*suspend)(struct oxygen *chip);
8662306a36Sopenharmony_ci	void (*resume)(struct oxygen *chip);
8762306a36Sopenharmony_ci	void (*pcm_hardware_filter)(unsigned int channel,
8862306a36Sopenharmony_ci				    struct snd_pcm_hardware *hardware);
8962306a36Sopenharmony_ci	void (*set_dac_params)(struct oxygen *chip,
9062306a36Sopenharmony_ci			       struct snd_pcm_hw_params *params);
9162306a36Sopenharmony_ci	void (*set_adc_params)(struct oxygen *chip,
9262306a36Sopenharmony_ci			       struct snd_pcm_hw_params *params);
9362306a36Sopenharmony_ci	void (*update_dac_volume)(struct oxygen *chip);
9462306a36Sopenharmony_ci	void (*update_dac_mute)(struct oxygen *chip);
9562306a36Sopenharmony_ci	void (*update_center_lfe_mix)(struct oxygen *chip, bool mixed);
9662306a36Sopenharmony_ci	unsigned int (*adjust_dac_routing)(struct oxygen *chip,
9762306a36Sopenharmony_ci					   unsigned int play_routing);
9862306a36Sopenharmony_ci	void (*gpio_changed)(struct oxygen *chip);
9962306a36Sopenharmony_ci	void (*uart_input)(struct oxygen *chip);
10062306a36Sopenharmony_ci	void (*ac97_switch)(struct oxygen *chip,
10162306a36Sopenharmony_ci			    unsigned int reg, unsigned int mute);
10262306a36Sopenharmony_ci	void (*dump_registers)(struct oxygen *chip,
10362306a36Sopenharmony_ci			       struct snd_info_buffer *buffer);
10462306a36Sopenharmony_ci	const unsigned int *dac_tlv;
10562306a36Sopenharmony_ci	size_t model_data_size;
10662306a36Sopenharmony_ci	unsigned int device_config;
10762306a36Sopenharmony_ci	u8 dac_channels_pcm;
10862306a36Sopenharmony_ci	u8 dac_channels_mixer;
10962306a36Sopenharmony_ci	u8 dac_volume_min;
11062306a36Sopenharmony_ci	u8 dac_volume_max;
11162306a36Sopenharmony_ci	u8 misc_flags;
11262306a36Sopenharmony_ci	u8 function_flags;
11362306a36Sopenharmony_ci	u8 dac_mclks;
11462306a36Sopenharmony_ci	u8 adc_mclks;
11562306a36Sopenharmony_ci	u16 dac_i2s_format;
11662306a36Sopenharmony_ci	u16 adc_i2s_format;
11762306a36Sopenharmony_ci};
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistruct oxygen {
12062306a36Sopenharmony_ci	unsigned long addr;
12162306a36Sopenharmony_ci	spinlock_t reg_lock;
12262306a36Sopenharmony_ci	struct mutex mutex;
12362306a36Sopenharmony_ci	struct snd_card *card;
12462306a36Sopenharmony_ci	struct pci_dev *pci;
12562306a36Sopenharmony_ci	struct snd_rawmidi *midi;
12662306a36Sopenharmony_ci	int irq;
12762306a36Sopenharmony_ci	void *model_data;
12862306a36Sopenharmony_ci	unsigned int interrupt_mask;
12962306a36Sopenharmony_ci	u8 dac_volume[8];
13062306a36Sopenharmony_ci	u8 dac_mute;
13162306a36Sopenharmony_ci	u8 pcm_active;
13262306a36Sopenharmony_ci	u8 pcm_running;
13362306a36Sopenharmony_ci	u8 dac_routing;
13462306a36Sopenharmony_ci	u8 spdif_playback_enable;
13562306a36Sopenharmony_ci	u8 has_ac97_0;
13662306a36Sopenharmony_ci	u8 has_ac97_1;
13762306a36Sopenharmony_ci	u32 spdif_bits;
13862306a36Sopenharmony_ci	u32 spdif_pcm_bits;
13962306a36Sopenharmony_ci	struct snd_pcm_substream *streams[PCM_COUNT];
14062306a36Sopenharmony_ci	struct snd_kcontrol *controls[CONTROL_COUNT];
14162306a36Sopenharmony_ci	struct work_struct spdif_input_bits_work;
14262306a36Sopenharmony_ci	struct work_struct gpio_work;
14362306a36Sopenharmony_ci	wait_queue_head_t ac97_waitqueue;
14462306a36Sopenharmony_ci	union {
14562306a36Sopenharmony_ci		u8 _8[OXYGEN_IO_SIZE];
14662306a36Sopenharmony_ci		__le16 _16[OXYGEN_IO_SIZE / 2];
14762306a36Sopenharmony_ci		__le32 _32[OXYGEN_IO_SIZE / 4];
14862306a36Sopenharmony_ci	} saved_registers;
14962306a36Sopenharmony_ci	u16 saved_ac97_registers[2][0x40];
15062306a36Sopenharmony_ci	unsigned int uart_input_count;
15162306a36Sopenharmony_ci	u8 uart_input[32];
15262306a36Sopenharmony_ci	struct oxygen_model model;
15362306a36Sopenharmony_ci};
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci/* oxygen_lib.c */
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ciint oxygen_pci_probe(struct pci_dev *pci, int index, char *id,
15862306a36Sopenharmony_ci		     struct module *owner,
15962306a36Sopenharmony_ci		     const struct pci_device_id *ids,
16062306a36Sopenharmony_ci		     int (*get_model)(struct oxygen *chip,
16162306a36Sopenharmony_ci				      const struct pci_device_id *id
16262306a36Sopenharmony_ci				     )
16362306a36Sopenharmony_ci		    );
16462306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
16562306a36Sopenharmony_ciextern const struct dev_pm_ops oxygen_pci_pm;
16662306a36Sopenharmony_ci#endif
16762306a36Sopenharmony_civoid oxygen_pci_shutdown(struct pci_dev *pci);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci/* oxygen_mixer.c */
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ciint oxygen_mixer_init(struct oxygen *chip);
17262306a36Sopenharmony_civoid oxygen_update_dac_routing(struct oxygen *chip);
17362306a36Sopenharmony_civoid oxygen_update_spdif_source(struct oxygen *chip);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci/* oxygen_pcm.c */
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ciint oxygen_pcm_init(struct oxygen *chip);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci/* oxygen_io.c */
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ciu8 oxygen_read8(struct oxygen *chip, unsigned int reg);
18262306a36Sopenharmony_ciu16 oxygen_read16(struct oxygen *chip, unsigned int reg);
18362306a36Sopenharmony_ciu32 oxygen_read32(struct oxygen *chip, unsigned int reg);
18462306a36Sopenharmony_civoid oxygen_write8(struct oxygen *chip, unsigned int reg, u8 value);
18562306a36Sopenharmony_civoid oxygen_write16(struct oxygen *chip, unsigned int reg, u16 value);
18662306a36Sopenharmony_civoid oxygen_write32(struct oxygen *chip, unsigned int reg, u32 value);
18762306a36Sopenharmony_civoid oxygen_write8_masked(struct oxygen *chip, unsigned int reg,
18862306a36Sopenharmony_ci			  u8 value, u8 mask);
18962306a36Sopenharmony_civoid oxygen_write16_masked(struct oxygen *chip, unsigned int reg,
19062306a36Sopenharmony_ci			   u16 value, u16 mask);
19162306a36Sopenharmony_civoid oxygen_write32_masked(struct oxygen *chip, unsigned int reg,
19262306a36Sopenharmony_ci			   u32 value, u32 mask);
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ciu16 oxygen_read_ac97(struct oxygen *chip, unsigned int codec,
19562306a36Sopenharmony_ci		     unsigned int index);
19662306a36Sopenharmony_civoid oxygen_write_ac97(struct oxygen *chip, unsigned int codec,
19762306a36Sopenharmony_ci		       unsigned int index, u16 data);
19862306a36Sopenharmony_civoid oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec,
19962306a36Sopenharmony_ci			      unsigned int index, u16 data, u16 mask);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ciint oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data);
20262306a36Sopenharmony_civoid oxygen_write_i2c(struct oxygen *chip, u8 device, u8 map, u8 data);
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_civoid oxygen_reset_uart(struct oxygen *chip);
20562306a36Sopenharmony_civoid oxygen_write_uart(struct oxygen *chip, u8 data);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ciu16 oxygen_read_eeprom(struct oxygen *chip, unsigned int index);
20862306a36Sopenharmony_civoid oxygen_write_eeprom(struct oxygen *chip, unsigned int index, u16 value);
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_cistatic inline void oxygen_set_bits8(struct oxygen *chip,
21162306a36Sopenharmony_ci				    unsigned int reg, u8 value)
21262306a36Sopenharmony_ci{
21362306a36Sopenharmony_ci	oxygen_write8_masked(chip, reg, value, value);
21462306a36Sopenharmony_ci}
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_cistatic inline void oxygen_set_bits16(struct oxygen *chip,
21762306a36Sopenharmony_ci				     unsigned int reg, u16 value)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	oxygen_write16_masked(chip, reg, value, value);
22062306a36Sopenharmony_ci}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_cistatic inline void oxygen_set_bits32(struct oxygen *chip,
22362306a36Sopenharmony_ci				     unsigned int reg, u32 value)
22462306a36Sopenharmony_ci{
22562306a36Sopenharmony_ci	oxygen_write32_masked(chip, reg, value, value);
22662306a36Sopenharmony_ci}
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_cistatic inline void oxygen_clear_bits8(struct oxygen *chip,
22962306a36Sopenharmony_ci				      unsigned int reg, u8 value)
23062306a36Sopenharmony_ci{
23162306a36Sopenharmony_ci	oxygen_write8_masked(chip, reg, 0, value);
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_cistatic inline void oxygen_clear_bits16(struct oxygen *chip,
23562306a36Sopenharmony_ci				       unsigned int reg, u16 value)
23662306a36Sopenharmony_ci{
23762306a36Sopenharmony_ci	oxygen_write16_masked(chip, reg, 0, value);
23862306a36Sopenharmony_ci}
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_cistatic inline void oxygen_clear_bits32(struct oxygen *chip,
24162306a36Sopenharmony_ci				       unsigned int reg, u32 value)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	oxygen_write32_masked(chip, reg, 0, value);
24462306a36Sopenharmony_ci}
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_cistatic inline void oxygen_ac97_set_bits(struct oxygen *chip, unsigned int codec,
24762306a36Sopenharmony_ci					unsigned int index, u16 value)
24862306a36Sopenharmony_ci{
24962306a36Sopenharmony_ci	oxygen_write_ac97_masked(chip, codec, index, value, value);
25062306a36Sopenharmony_ci}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_cistatic inline void oxygen_ac97_clear_bits(struct oxygen *chip,
25362306a36Sopenharmony_ci					  unsigned int codec,
25462306a36Sopenharmony_ci					  unsigned int index, u16 value)
25562306a36Sopenharmony_ci{
25662306a36Sopenharmony_ci	oxygen_write_ac97_masked(chip, codec, index, 0, value);
25762306a36Sopenharmony_ci}
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci#endif
260