18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci    ad1816a.c - lowlevel code for Analog Devices AD1816A chip.
48c2ecf20Sopenharmony_ci    Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci*/
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/delay.h>
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/ioport.h>
138c2ecf20Sopenharmony_ci#include <linux/io.h>
148c2ecf20Sopenharmony_ci#include <sound/core.h>
158c2ecf20Sopenharmony_ci#include <sound/tlv.h>
168c2ecf20Sopenharmony_ci#include <sound/ad1816a.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <asm/dma.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic inline int snd_ad1816a_busy_wait(struct snd_ad1816a *chip)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	int timeout;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	for (timeout = 1000; timeout-- > 0; udelay(10))
258c2ecf20Sopenharmony_ci		if (inb(AD1816A_REG(AD1816A_CHIP_STATUS)) & AD1816A_READY)
268c2ecf20Sopenharmony_ci			return 0;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	snd_printk(KERN_WARNING "chip busy.\n");
298c2ecf20Sopenharmony_ci	return -EBUSY;
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic inline unsigned char snd_ad1816a_in(struct snd_ad1816a *chip, unsigned char reg)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	snd_ad1816a_busy_wait(chip);
358c2ecf20Sopenharmony_ci	return inb(AD1816A_REG(reg));
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic inline void snd_ad1816a_out(struct snd_ad1816a *chip, unsigned char reg,
398c2ecf20Sopenharmony_ci			    unsigned char value)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	snd_ad1816a_busy_wait(chip);
428c2ecf20Sopenharmony_ci	outb(value, AD1816A_REG(reg));
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic inline void snd_ad1816a_out_mask(struct snd_ad1816a *chip, unsigned char reg,
468c2ecf20Sopenharmony_ci				 unsigned char mask, unsigned char value)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	snd_ad1816a_out(chip, reg,
498c2ecf20Sopenharmony_ci		(value & mask) | (snd_ad1816a_in(chip, reg) & ~mask));
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic unsigned short snd_ad1816a_read(struct snd_ad1816a *chip, unsigned char reg)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	snd_ad1816a_out(chip, AD1816A_INDIR_ADDR, reg & 0x3f);
558c2ecf20Sopenharmony_ci	return snd_ad1816a_in(chip, AD1816A_INDIR_DATA_LOW) |
568c2ecf20Sopenharmony_ci		(snd_ad1816a_in(chip, AD1816A_INDIR_DATA_HIGH) << 8);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic void snd_ad1816a_write(struct snd_ad1816a *chip, unsigned char reg,
608c2ecf20Sopenharmony_ci			      unsigned short value)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	snd_ad1816a_out(chip, AD1816A_INDIR_ADDR, reg & 0x3f);
638c2ecf20Sopenharmony_ci	snd_ad1816a_out(chip, AD1816A_INDIR_DATA_LOW, value & 0xff);
648c2ecf20Sopenharmony_ci	snd_ad1816a_out(chip, AD1816A_INDIR_DATA_HIGH, (value >> 8) & 0xff);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic void snd_ad1816a_write_mask(struct snd_ad1816a *chip, unsigned char reg,
688c2ecf20Sopenharmony_ci				   unsigned short mask, unsigned short value)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, reg,
718c2ecf20Sopenharmony_ci		(value & mask) | (snd_ad1816a_read(chip, reg) & ~mask));
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic unsigned char snd_ad1816a_get_format(struct snd_ad1816a *chip,
768c2ecf20Sopenharmony_ci					    snd_pcm_format_t format,
778c2ecf20Sopenharmony_ci					    int channels)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	unsigned char retval = AD1816A_FMT_LINEAR_8;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	switch (format) {
828c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_MU_LAW:
838c2ecf20Sopenharmony_ci		retval = AD1816A_FMT_ULAW_8;
848c2ecf20Sopenharmony_ci		break;
858c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_A_LAW:
868c2ecf20Sopenharmony_ci		retval = AD1816A_FMT_ALAW_8;
878c2ecf20Sopenharmony_ci		break;
888c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_S16_LE:
898c2ecf20Sopenharmony_ci		retval = AD1816A_FMT_LINEAR_16_LIT;
908c2ecf20Sopenharmony_ci		break;
918c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_S16_BE:
928c2ecf20Sopenharmony_ci		retval = AD1816A_FMT_LINEAR_16_BIG;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci	return (channels > 1) ? (retval | AD1816A_FMT_STEREO) : retval;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int snd_ad1816a_open(struct snd_ad1816a *chip, unsigned int mode)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	unsigned long flags;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	if (chip->mode & mode) {
1048c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->lock, flags);
1058c2ecf20Sopenharmony_ci		return -EAGAIN;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	switch ((mode &= AD1816A_MODE_OPEN)) {
1098c2ecf20Sopenharmony_ci	case AD1816A_MODE_PLAYBACK:
1108c2ecf20Sopenharmony_ci		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
1118c2ecf20Sopenharmony_ci			AD1816A_PLAYBACK_IRQ_PENDING, 0x00);
1128c2ecf20Sopenharmony_ci		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
1138c2ecf20Sopenharmony_ci			AD1816A_PLAYBACK_IRQ_ENABLE, 0xffff);
1148c2ecf20Sopenharmony_ci		break;
1158c2ecf20Sopenharmony_ci	case AD1816A_MODE_CAPTURE:
1168c2ecf20Sopenharmony_ci		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
1178c2ecf20Sopenharmony_ci			AD1816A_CAPTURE_IRQ_PENDING, 0x00);
1188c2ecf20Sopenharmony_ci		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
1198c2ecf20Sopenharmony_ci			AD1816A_CAPTURE_IRQ_ENABLE, 0xffff);
1208c2ecf20Sopenharmony_ci		break;
1218c2ecf20Sopenharmony_ci	case AD1816A_MODE_TIMER:
1228c2ecf20Sopenharmony_ci		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
1238c2ecf20Sopenharmony_ci			AD1816A_TIMER_IRQ_PENDING, 0x00);
1248c2ecf20Sopenharmony_ci		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
1258c2ecf20Sopenharmony_ci			AD1816A_TIMER_IRQ_ENABLE, 0xffff);
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci	chip->mode |= mode;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
1308c2ecf20Sopenharmony_ci	return 0;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic void snd_ad1816a_close(struct snd_ad1816a *chip, unsigned int mode)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	unsigned long flags;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	switch ((mode &= AD1816A_MODE_OPEN)) {
1408c2ecf20Sopenharmony_ci	case AD1816A_MODE_PLAYBACK:
1418c2ecf20Sopenharmony_ci		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
1428c2ecf20Sopenharmony_ci			AD1816A_PLAYBACK_IRQ_PENDING, 0x00);
1438c2ecf20Sopenharmony_ci		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
1448c2ecf20Sopenharmony_ci			AD1816A_PLAYBACK_IRQ_ENABLE, 0x0000);
1458c2ecf20Sopenharmony_ci		break;
1468c2ecf20Sopenharmony_ci	case AD1816A_MODE_CAPTURE:
1478c2ecf20Sopenharmony_ci		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
1488c2ecf20Sopenharmony_ci			AD1816A_CAPTURE_IRQ_PENDING, 0x00);
1498c2ecf20Sopenharmony_ci		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
1508c2ecf20Sopenharmony_ci			AD1816A_CAPTURE_IRQ_ENABLE, 0x0000);
1518c2ecf20Sopenharmony_ci		break;
1528c2ecf20Sopenharmony_ci	case AD1816A_MODE_TIMER:
1538c2ecf20Sopenharmony_ci		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
1548c2ecf20Sopenharmony_ci			AD1816A_TIMER_IRQ_PENDING, 0x00);
1558c2ecf20Sopenharmony_ci		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
1568c2ecf20Sopenharmony_ci			AD1816A_TIMER_IRQ_ENABLE, 0x0000);
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci	if (!((chip->mode &= ~mode) & AD1816A_MODE_OPEN))
1598c2ecf20Sopenharmony_ci		chip->mode = 0;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic int snd_ad1816a_trigger(struct snd_ad1816a *chip, unsigned char what,
1668c2ecf20Sopenharmony_ci			       int channel, int cmd, int iscapture)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	int error = 0;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	switch (cmd) {
1718c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
1728c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
1738c2ecf20Sopenharmony_ci		spin_lock(&chip->lock);
1748c2ecf20Sopenharmony_ci		cmd = (cmd == SNDRV_PCM_TRIGGER_START) ? 0xff: 0x00;
1758c2ecf20Sopenharmony_ci		/* if (what & AD1816A_PLAYBACK_ENABLE) */
1768c2ecf20Sopenharmony_ci		/* That is not valid, because playback and capture enable
1778c2ecf20Sopenharmony_ci		 * are the same bit pattern, just to different addresses
1788c2ecf20Sopenharmony_ci		 */
1798c2ecf20Sopenharmony_ci		if (! iscapture)
1808c2ecf20Sopenharmony_ci			snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
1818c2ecf20Sopenharmony_ci				AD1816A_PLAYBACK_ENABLE, cmd);
1828c2ecf20Sopenharmony_ci		else
1838c2ecf20Sopenharmony_ci			snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
1848c2ecf20Sopenharmony_ci				AD1816A_CAPTURE_ENABLE, cmd);
1858c2ecf20Sopenharmony_ci		spin_unlock(&chip->lock);
1868c2ecf20Sopenharmony_ci		break;
1878c2ecf20Sopenharmony_ci	default:
1888c2ecf20Sopenharmony_ci		snd_printk(KERN_WARNING "invalid trigger mode 0x%x.\n", what);
1898c2ecf20Sopenharmony_ci		error = -EINVAL;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	return error;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic int snd_ad1816a_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
1988c2ecf20Sopenharmony_ci	return snd_ad1816a_trigger(chip, AD1816A_PLAYBACK_ENABLE,
1998c2ecf20Sopenharmony_ci				   SNDRV_PCM_STREAM_PLAYBACK, cmd, 0);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic int snd_ad1816a_capture_trigger(struct snd_pcm_substream *substream, int cmd)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
2058c2ecf20Sopenharmony_ci	return snd_ad1816a_trigger(chip, AD1816A_CAPTURE_ENABLE,
2068c2ecf20Sopenharmony_ci				   SNDRV_PCM_STREAM_CAPTURE, cmd, 1);
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic int snd_ad1816a_playback_prepare(struct snd_pcm_substream *substream)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
2128c2ecf20Sopenharmony_ci	unsigned long flags;
2138c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
2148c2ecf20Sopenharmony_ci	unsigned int size, rate;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	chip->p_dma_size = size = snd_pcm_lib_buffer_bytes(substream);
2198c2ecf20Sopenharmony_ci	snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
2208c2ecf20Sopenharmony_ci		AD1816A_PLAYBACK_ENABLE | AD1816A_PLAYBACK_PIO, 0x00);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	snd_dma_program(chip->dma1, runtime->dma_addr, size,
2238c2ecf20Sopenharmony_ci			DMA_MODE_WRITE | DMA_AUTOINIT);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	rate = runtime->rate;
2268c2ecf20Sopenharmony_ci	if (chip->clock_freq)
2278c2ecf20Sopenharmony_ci		rate = (rate * 33000) / chip->clock_freq;
2288c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, AD1816A_PLAYBACK_SAMPLE_RATE, rate);
2298c2ecf20Sopenharmony_ci	snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
2308c2ecf20Sopenharmony_ci		AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
2318c2ecf20Sopenharmony_ci		snd_ad1816a_get_format(chip, runtime->format,
2328c2ecf20Sopenharmony_ci			runtime->channels));
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, AD1816A_PLAYBACK_BASE_COUNT,
2358c2ecf20Sopenharmony_ci		snd_pcm_lib_period_bytes(substream) / 4 - 1);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
2388c2ecf20Sopenharmony_ci	return 0;
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic int snd_ad1816a_capture_prepare(struct snd_pcm_substream *substream)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
2448c2ecf20Sopenharmony_ci	unsigned long flags;
2458c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
2468c2ecf20Sopenharmony_ci	unsigned int size, rate;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	chip->c_dma_size = size = snd_pcm_lib_buffer_bytes(substream);
2518c2ecf20Sopenharmony_ci	snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
2528c2ecf20Sopenharmony_ci		AD1816A_CAPTURE_ENABLE | AD1816A_CAPTURE_PIO, 0x00);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	snd_dma_program(chip->dma2, runtime->dma_addr, size,
2558c2ecf20Sopenharmony_ci			DMA_MODE_READ | DMA_AUTOINIT);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	rate = runtime->rate;
2588c2ecf20Sopenharmony_ci	if (chip->clock_freq)
2598c2ecf20Sopenharmony_ci		rate = (rate * 33000) / chip->clock_freq;
2608c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, AD1816A_CAPTURE_SAMPLE_RATE, rate);
2618c2ecf20Sopenharmony_ci	snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
2628c2ecf20Sopenharmony_ci		AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
2638c2ecf20Sopenharmony_ci		snd_ad1816a_get_format(chip, runtime->format,
2648c2ecf20Sopenharmony_ci			runtime->channels));
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, AD1816A_CAPTURE_BASE_COUNT,
2678c2ecf20Sopenharmony_ci		snd_pcm_lib_period_bytes(substream) / 4 - 1);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
2708c2ecf20Sopenharmony_ci	return 0;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_ad1816a_playback_pointer(struct snd_pcm_substream *substream)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
2778c2ecf20Sopenharmony_ci	size_t ptr;
2788c2ecf20Sopenharmony_ci	if (!(chip->mode & AD1816A_MODE_PLAYBACK))
2798c2ecf20Sopenharmony_ci		return 0;
2808c2ecf20Sopenharmony_ci	ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
2818c2ecf20Sopenharmony_ci	return bytes_to_frames(substream->runtime, ptr);
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_ad1816a_capture_pointer(struct snd_pcm_substream *substream)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
2878c2ecf20Sopenharmony_ci	size_t ptr;
2888c2ecf20Sopenharmony_ci	if (!(chip->mode & AD1816A_MODE_CAPTURE))
2898c2ecf20Sopenharmony_ci		return 0;
2908c2ecf20Sopenharmony_ci	ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
2918c2ecf20Sopenharmony_ci	return bytes_to_frames(substream->runtime, ptr);
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic irqreturn_t snd_ad1816a_interrupt(int irq, void *dev_id)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = dev_id;
2988c2ecf20Sopenharmony_ci	unsigned char status;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	spin_lock(&chip->lock);
3018c2ecf20Sopenharmony_ci	status = snd_ad1816a_in(chip, AD1816A_INTERRUPT_STATUS);
3028c2ecf20Sopenharmony_ci	spin_unlock(&chip->lock);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	if ((status & AD1816A_PLAYBACK_IRQ_PENDING) && chip->playback_substream)
3058c2ecf20Sopenharmony_ci		snd_pcm_period_elapsed(chip->playback_substream);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	if ((status & AD1816A_CAPTURE_IRQ_PENDING) && chip->capture_substream)
3088c2ecf20Sopenharmony_ci		snd_pcm_period_elapsed(chip->capture_substream);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if ((status & AD1816A_TIMER_IRQ_PENDING) && chip->timer)
3118c2ecf20Sopenharmony_ci		snd_timer_interrupt(chip->timer, chip->timer->sticks);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	spin_lock(&chip->lock);
3148c2ecf20Sopenharmony_ci	snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00);
3158c2ecf20Sopenharmony_ci	spin_unlock(&chip->lock);
3168c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_ad1816a_playback = {
3218c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
3228c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID),
3238c2ecf20Sopenharmony_ci	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
3248c2ecf20Sopenharmony_ci				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
3258c2ecf20Sopenharmony_ci				 SNDRV_PCM_FMTBIT_S16_BE),
3268c2ecf20Sopenharmony_ci	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
3278c2ecf20Sopenharmony_ci	.rate_min =		4000,
3288c2ecf20Sopenharmony_ci	.rate_max =		55200,
3298c2ecf20Sopenharmony_ci	.channels_min =		1,
3308c2ecf20Sopenharmony_ci	.channels_max =		2,
3318c2ecf20Sopenharmony_ci	.buffer_bytes_max =	(128*1024),
3328c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
3338c2ecf20Sopenharmony_ci	.period_bytes_max =	(128*1024),
3348c2ecf20Sopenharmony_ci	.periods_min =		1,
3358c2ecf20Sopenharmony_ci	.periods_max =		1024,
3368c2ecf20Sopenharmony_ci	.fifo_size =		0,
3378c2ecf20Sopenharmony_ci};
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_ad1816a_capture = {
3408c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
3418c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID),
3428c2ecf20Sopenharmony_ci	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
3438c2ecf20Sopenharmony_ci				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
3448c2ecf20Sopenharmony_ci				 SNDRV_PCM_FMTBIT_S16_BE),
3458c2ecf20Sopenharmony_ci	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
3468c2ecf20Sopenharmony_ci	.rate_min =		4000,
3478c2ecf20Sopenharmony_ci	.rate_max =		55200,
3488c2ecf20Sopenharmony_ci	.channels_min =		1,
3498c2ecf20Sopenharmony_ci	.channels_max =		2,
3508c2ecf20Sopenharmony_ci	.buffer_bytes_max =	(128*1024),
3518c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
3528c2ecf20Sopenharmony_ci	.period_bytes_max =	(128*1024),
3538c2ecf20Sopenharmony_ci	.periods_min =		1,
3548c2ecf20Sopenharmony_ci	.periods_max =		1024,
3558c2ecf20Sopenharmony_ci	.fifo_size =		0,
3568c2ecf20Sopenharmony_ci};
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_cistatic int snd_ad1816a_timer_close(struct snd_timer *timer)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_timer_chip(timer);
3618c2ecf20Sopenharmony_ci	snd_ad1816a_close(chip, AD1816A_MODE_TIMER);
3628c2ecf20Sopenharmony_ci	return 0;
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic int snd_ad1816a_timer_open(struct snd_timer *timer)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_timer_chip(timer);
3688c2ecf20Sopenharmony_ci	snd_ad1816a_open(chip, AD1816A_MODE_TIMER);
3698c2ecf20Sopenharmony_ci	return 0;
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic unsigned long snd_ad1816a_timer_resolution(struct snd_timer *timer)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!timer))
3758c2ecf20Sopenharmony_ci		return 0;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	return 10000;
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic int snd_ad1816a_timer_start(struct snd_timer *timer)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	unsigned short bits;
3838c2ecf20Sopenharmony_ci	unsigned long flags;
3848c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_timer_chip(timer);
3858c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
3868c2ecf20Sopenharmony_ci	bits = snd_ad1816a_read(chip, AD1816A_INTERRUPT_ENABLE);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	if (!(bits & AD1816A_TIMER_ENABLE)) {
3898c2ecf20Sopenharmony_ci		snd_ad1816a_write(chip, AD1816A_TIMER_BASE_COUNT,
3908c2ecf20Sopenharmony_ci			timer->sticks & 0xffff);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
3938c2ecf20Sopenharmony_ci			AD1816A_TIMER_ENABLE, 0xffff);
3948c2ecf20Sopenharmony_ci	}
3958c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
3968c2ecf20Sopenharmony_ci	return 0;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic int snd_ad1816a_timer_stop(struct snd_timer *timer)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	unsigned long flags;
4028c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_timer_chip(timer);
4038c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
4068c2ecf20Sopenharmony_ci		AD1816A_TIMER_ENABLE, 0x0000);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
4098c2ecf20Sopenharmony_ci	return 0;
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic const struct snd_timer_hardware snd_ad1816a_timer_table = {
4138c2ecf20Sopenharmony_ci	.flags =	SNDRV_TIMER_HW_AUTO,
4148c2ecf20Sopenharmony_ci	.resolution =	10000,
4158c2ecf20Sopenharmony_ci	.ticks =	65535,
4168c2ecf20Sopenharmony_ci	.open =		snd_ad1816a_timer_open,
4178c2ecf20Sopenharmony_ci	.close =	snd_ad1816a_timer_close,
4188c2ecf20Sopenharmony_ci	.c_resolution =	snd_ad1816a_timer_resolution,
4198c2ecf20Sopenharmony_ci	.start =	snd_ad1816a_timer_start,
4208c2ecf20Sopenharmony_ci	.stop =		snd_ad1816a_timer_stop,
4218c2ecf20Sopenharmony_ci};
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cistatic int snd_ad1816a_playback_open(struct snd_pcm_substream *substream)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
4268c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
4278c2ecf20Sopenharmony_ci	int error;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	if ((error = snd_ad1816a_open(chip, AD1816A_MODE_PLAYBACK)) < 0)
4308c2ecf20Sopenharmony_ci		return error;
4318c2ecf20Sopenharmony_ci	runtime->hw = snd_ad1816a_playback;
4328c2ecf20Sopenharmony_ci	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
4338c2ecf20Sopenharmony_ci	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
4348c2ecf20Sopenharmony_ci	chip->playback_substream = substream;
4358c2ecf20Sopenharmony_ci	return 0;
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_cistatic int snd_ad1816a_capture_open(struct snd_pcm_substream *substream)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
4418c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
4428c2ecf20Sopenharmony_ci	int error;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	if ((error = snd_ad1816a_open(chip, AD1816A_MODE_CAPTURE)) < 0)
4458c2ecf20Sopenharmony_ci		return error;
4468c2ecf20Sopenharmony_ci	runtime->hw = snd_ad1816a_capture;
4478c2ecf20Sopenharmony_ci	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
4488c2ecf20Sopenharmony_ci	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
4498c2ecf20Sopenharmony_ci	chip->capture_substream = substream;
4508c2ecf20Sopenharmony_ci	return 0;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic int snd_ad1816a_playback_close(struct snd_pcm_substream *substream)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	chip->playback_substream = NULL;
4588c2ecf20Sopenharmony_ci	snd_ad1816a_close(chip, AD1816A_MODE_PLAYBACK);
4598c2ecf20Sopenharmony_ci	return 0;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic int snd_ad1816a_capture_close(struct snd_pcm_substream *substream)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	chip->capture_substream = NULL;
4678c2ecf20Sopenharmony_ci	snd_ad1816a_close(chip, AD1816A_MODE_CAPTURE);
4688c2ecf20Sopenharmony_ci	return 0;
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_cistatic void snd_ad1816a_init(struct snd_ad1816a *chip)
4738c2ecf20Sopenharmony_ci{
4748c2ecf20Sopenharmony_ci	unsigned long flags;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00);
4798c2ecf20Sopenharmony_ci	snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
4808c2ecf20Sopenharmony_ci		AD1816A_PLAYBACK_ENABLE | AD1816A_PLAYBACK_PIO, 0x00);
4818c2ecf20Sopenharmony_ci	snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
4828c2ecf20Sopenharmony_ci		AD1816A_CAPTURE_ENABLE | AD1816A_CAPTURE_PIO, 0x00);
4838c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, AD1816A_INTERRUPT_ENABLE, 0x0000);
4848c2ecf20Sopenharmony_ci	snd_ad1816a_write_mask(chip, AD1816A_CHIP_CONFIG,
4858c2ecf20Sopenharmony_ci		AD1816A_CAPTURE_NOT_EQUAL | AD1816A_WSS_ENABLE, 0xffff);
4868c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, AD1816A_DSP_CONFIG, 0x0000);
4878c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, AD1816A_POWERDOWN_CTRL, 0x0000);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
4938c2ecf20Sopenharmony_civoid snd_ad1816a_suspend(struct snd_ad1816a *chip)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	int reg;
4968c2ecf20Sopenharmony_ci	unsigned long flags;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
4998c2ecf20Sopenharmony_ci	for (reg = 0; reg < 48; reg++)
5008c2ecf20Sopenharmony_ci		chip->image[reg] = snd_ad1816a_read(chip, reg);
5018c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_civoid snd_ad1816a_resume(struct snd_ad1816a *chip)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	int reg;
5078c2ecf20Sopenharmony_ci	unsigned long flags;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	snd_ad1816a_init(chip);
5108c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
5118c2ecf20Sopenharmony_ci	for (reg = 0; reg < 48; reg++)
5128c2ecf20Sopenharmony_ci		snd_ad1816a_write(chip, reg, chip->image[reg]);
5138c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
5148c2ecf20Sopenharmony_ci}
5158c2ecf20Sopenharmony_ci#endif
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_cistatic int snd_ad1816a_probe(struct snd_ad1816a *chip)
5188c2ecf20Sopenharmony_ci{
5198c2ecf20Sopenharmony_ci	unsigned long flags;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	switch (chip->version = snd_ad1816a_read(chip, AD1816A_VERSION_ID)) {
5248c2ecf20Sopenharmony_ci	case 0:
5258c2ecf20Sopenharmony_ci		chip->hardware = AD1816A_HW_AD1815;
5268c2ecf20Sopenharmony_ci		break;
5278c2ecf20Sopenharmony_ci	case 1:
5288c2ecf20Sopenharmony_ci		chip->hardware = AD1816A_HW_AD18MAX10;
5298c2ecf20Sopenharmony_ci		break;
5308c2ecf20Sopenharmony_ci	case 3:
5318c2ecf20Sopenharmony_ci		chip->hardware = AD1816A_HW_AD1816A;
5328c2ecf20Sopenharmony_ci		break;
5338c2ecf20Sopenharmony_ci	default:
5348c2ecf20Sopenharmony_ci		chip->hardware = AD1816A_HW_AUTO;
5358c2ecf20Sopenharmony_ci	}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
5388c2ecf20Sopenharmony_ci	return 0;
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic int snd_ad1816a_free(struct snd_ad1816a *chip)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	release_and_free_resource(chip->res_port);
5448c2ecf20Sopenharmony_ci	if (chip->irq >= 0)
5458c2ecf20Sopenharmony_ci		free_irq(chip->irq, (void *) chip);
5468c2ecf20Sopenharmony_ci	if (chip->dma1 >= 0) {
5478c2ecf20Sopenharmony_ci		snd_dma_disable(chip->dma1);
5488c2ecf20Sopenharmony_ci		free_dma(chip->dma1);
5498c2ecf20Sopenharmony_ci	}
5508c2ecf20Sopenharmony_ci	if (chip->dma2 >= 0) {
5518c2ecf20Sopenharmony_ci		snd_dma_disable(chip->dma2);
5528c2ecf20Sopenharmony_ci		free_dma(chip->dma2);
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci	return 0;
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_cistatic int snd_ad1816a_dev_free(struct snd_device *device)
5588c2ecf20Sopenharmony_ci{
5598c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = device->device_data;
5608c2ecf20Sopenharmony_ci	return snd_ad1816a_free(chip);
5618c2ecf20Sopenharmony_ci}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_cistatic const char *snd_ad1816a_chip_id(struct snd_ad1816a *chip)
5648c2ecf20Sopenharmony_ci{
5658c2ecf20Sopenharmony_ci	switch (chip->hardware) {
5668c2ecf20Sopenharmony_ci	case AD1816A_HW_AD1816A: return "AD1816A";
5678c2ecf20Sopenharmony_ci	case AD1816A_HW_AD1815:	return "AD1815";
5688c2ecf20Sopenharmony_ci	case AD1816A_HW_AD18MAX10: return "AD18max10";
5698c2ecf20Sopenharmony_ci	default:
5708c2ecf20Sopenharmony_ci		snd_printk(KERN_WARNING "Unknown chip version %d:%d.\n",
5718c2ecf20Sopenharmony_ci			chip->version, chip->hardware);
5728c2ecf20Sopenharmony_ci		return "AD1816A - unknown";
5738c2ecf20Sopenharmony_ci	}
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ciint snd_ad1816a_create(struct snd_card *card,
5778c2ecf20Sopenharmony_ci		       unsigned long port, int irq, int dma1, int dma2,
5788c2ecf20Sopenharmony_ci		       struct snd_ad1816a *chip)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
5818c2ecf20Sopenharmony_ci		.dev_free =	snd_ad1816a_dev_free,
5828c2ecf20Sopenharmony_ci	};
5838c2ecf20Sopenharmony_ci	int error;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	chip->irq = -1;
5868c2ecf20Sopenharmony_ci	chip->dma1 = -1;
5878c2ecf20Sopenharmony_ci	chip->dma2 = -1;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	if ((chip->res_port = request_region(port, 16, "AD1816A")) == NULL) {
5908c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "ad1816a: can't grab port 0x%lx\n", port);
5918c2ecf20Sopenharmony_ci		snd_ad1816a_free(chip);
5928c2ecf20Sopenharmony_ci		return -EBUSY;
5938c2ecf20Sopenharmony_ci	}
5948c2ecf20Sopenharmony_ci	if (request_irq(irq, snd_ad1816a_interrupt, 0, "AD1816A", (void *) chip)) {
5958c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "ad1816a: can't grab IRQ %d\n", irq);
5968c2ecf20Sopenharmony_ci		snd_ad1816a_free(chip);
5978c2ecf20Sopenharmony_ci		return -EBUSY;
5988c2ecf20Sopenharmony_ci	}
5998c2ecf20Sopenharmony_ci	chip->irq = irq;
6008c2ecf20Sopenharmony_ci	card->sync_irq = chip->irq;
6018c2ecf20Sopenharmony_ci	if (request_dma(dma1, "AD1816A - 1")) {
6028c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "ad1816a: can't grab DMA1 %d\n", dma1);
6038c2ecf20Sopenharmony_ci		snd_ad1816a_free(chip);
6048c2ecf20Sopenharmony_ci		return -EBUSY;
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci	chip->dma1 = dma1;
6078c2ecf20Sopenharmony_ci	if (request_dma(dma2, "AD1816A - 2")) {
6088c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "ad1816a: can't grab DMA2 %d\n", dma2);
6098c2ecf20Sopenharmony_ci		snd_ad1816a_free(chip);
6108c2ecf20Sopenharmony_ci		return -EBUSY;
6118c2ecf20Sopenharmony_ci	}
6128c2ecf20Sopenharmony_ci	chip->dma2 = dma2;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	chip->card = card;
6158c2ecf20Sopenharmony_ci	chip->port = port;
6168c2ecf20Sopenharmony_ci	spin_lock_init(&chip->lock);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	if ((error = snd_ad1816a_probe(chip))) {
6198c2ecf20Sopenharmony_ci		snd_ad1816a_free(chip);
6208c2ecf20Sopenharmony_ci		return error;
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	snd_ad1816a_init(chip);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	/* Register device */
6268c2ecf20Sopenharmony_ci	if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
6278c2ecf20Sopenharmony_ci		snd_ad1816a_free(chip);
6288c2ecf20Sopenharmony_ci		return error;
6298c2ecf20Sopenharmony_ci	}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	return 0;
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ad1816a_playback_ops = {
6358c2ecf20Sopenharmony_ci	.open =		snd_ad1816a_playback_open,
6368c2ecf20Sopenharmony_ci	.close =	snd_ad1816a_playback_close,
6378c2ecf20Sopenharmony_ci	.prepare =	snd_ad1816a_playback_prepare,
6388c2ecf20Sopenharmony_ci	.trigger =	snd_ad1816a_playback_trigger,
6398c2ecf20Sopenharmony_ci	.pointer =	snd_ad1816a_playback_pointer,
6408c2ecf20Sopenharmony_ci};
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ad1816a_capture_ops = {
6438c2ecf20Sopenharmony_ci	.open =		snd_ad1816a_capture_open,
6448c2ecf20Sopenharmony_ci	.close =	snd_ad1816a_capture_close,
6458c2ecf20Sopenharmony_ci	.prepare =	snd_ad1816a_capture_prepare,
6468c2ecf20Sopenharmony_ci	.trigger =	snd_ad1816a_capture_trigger,
6478c2ecf20Sopenharmony_ci	.pointer =	snd_ad1816a_capture_pointer,
6488c2ecf20Sopenharmony_ci};
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ciint snd_ad1816a_pcm(struct snd_ad1816a *chip, int device)
6518c2ecf20Sopenharmony_ci{
6528c2ecf20Sopenharmony_ci	int error;
6538c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	if ((error = snd_pcm_new(chip->card, "AD1816A", device, 1, 1, &pcm)))
6568c2ecf20Sopenharmony_ci		return error;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1816a_playback_ops);
6598c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1816a_capture_ops);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	pcm->private_data = chip;
6628c2ecf20Sopenharmony_ci	pcm->info_flags = (chip->dma1 == chip->dma2 ) ? SNDRV_PCM_INFO_JOINT_DUPLEX : 0;
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	strcpy(pcm->name, snd_ad1816a_chip_id(chip));
6658c2ecf20Sopenharmony_ci	snd_ad1816a_init(chip);
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, chip->card->dev,
6688c2ecf20Sopenharmony_ci				       64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	chip->pcm = pcm;
6718c2ecf20Sopenharmony_ci	return 0;
6728c2ecf20Sopenharmony_ci}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ciint snd_ad1816a_timer(struct snd_ad1816a *chip, int device)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	struct snd_timer *timer;
6778c2ecf20Sopenharmony_ci	struct snd_timer_id tid;
6788c2ecf20Sopenharmony_ci	int error;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
6818c2ecf20Sopenharmony_ci	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
6828c2ecf20Sopenharmony_ci	tid.card = chip->card->number;
6838c2ecf20Sopenharmony_ci	tid.device = device;
6848c2ecf20Sopenharmony_ci	tid.subdevice = 0;
6858c2ecf20Sopenharmony_ci	if ((error = snd_timer_new(chip->card, "AD1816A", &tid, &timer)) < 0)
6868c2ecf20Sopenharmony_ci		return error;
6878c2ecf20Sopenharmony_ci	strcpy(timer->name, snd_ad1816a_chip_id(chip));
6888c2ecf20Sopenharmony_ci	timer->private_data = chip;
6898c2ecf20Sopenharmony_ci	chip->timer = timer;
6908c2ecf20Sopenharmony_ci	timer->hw = snd_ad1816a_timer_table;
6918c2ecf20Sopenharmony_ci	return 0;
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci/*
6958c2ecf20Sopenharmony_ci *
6968c2ecf20Sopenharmony_ci */
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_cistatic int snd_ad1816a_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
6998c2ecf20Sopenharmony_ci{
7008c2ecf20Sopenharmony_ci	static const char * const texts[8] = {
7018c2ecf20Sopenharmony_ci		"Line", "Mix", "CD", "Synth", "Video",
7028c2ecf20Sopenharmony_ci		"Mic", "Phone",
7038c2ecf20Sopenharmony_ci	};
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	return snd_ctl_enum_info(uinfo, 2, 7, texts);
7068c2ecf20Sopenharmony_ci}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_cistatic int snd_ad1816a_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
7098c2ecf20Sopenharmony_ci{
7108c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
7118c2ecf20Sopenharmony_ci	unsigned long flags;
7128c2ecf20Sopenharmony_ci	unsigned short val;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
7158c2ecf20Sopenharmony_ci	val = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL);
7168c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
7178c2ecf20Sopenharmony_ci	ucontrol->value.enumerated.item[0] = (val >> 12) & 7;
7188c2ecf20Sopenharmony_ci	ucontrol->value.enumerated.item[1] = (val >> 4) & 7;
7198c2ecf20Sopenharmony_ci	return 0;
7208c2ecf20Sopenharmony_ci}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cistatic int snd_ad1816a_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
7258c2ecf20Sopenharmony_ci	unsigned long flags;
7268c2ecf20Sopenharmony_ci	unsigned short val;
7278c2ecf20Sopenharmony_ci	int change;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	if (ucontrol->value.enumerated.item[0] > 6 ||
7308c2ecf20Sopenharmony_ci	    ucontrol->value.enumerated.item[1] > 6)
7318c2ecf20Sopenharmony_ci		return -EINVAL;
7328c2ecf20Sopenharmony_ci	val = (ucontrol->value.enumerated.item[0] << 12) |
7338c2ecf20Sopenharmony_ci	      (ucontrol->value.enumerated.item[1] << 4);
7348c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
7358c2ecf20Sopenharmony_ci	change = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL) != val;
7368c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, AD1816A_ADC_SOURCE_SEL, val);
7378c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
7388c2ecf20Sopenharmony_ci	return change;
7398c2ecf20Sopenharmony_ci}
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci#define AD1816A_SINGLE_TLV(xname, reg, shift, mask, invert, xtlv)	\
7428c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
7438c2ecf20Sopenharmony_ci  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
7448c2ecf20Sopenharmony_ci  .name = xname, .info = snd_ad1816a_info_single, \
7458c2ecf20Sopenharmony_ci  .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
7468c2ecf20Sopenharmony_ci  .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
7478c2ecf20Sopenharmony_ci  .tlv = { .p = (xtlv) } }
7488c2ecf20Sopenharmony_ci#define AD1816A_SINGLE(xname, reg, shift, mask, invert) \
7498c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_single, \
7508c2ecf20Sopenharmony_ci  .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
7518c2ecf20Sopenharmony_ci  .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_cistatic int snd_ad1816a_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
7548c2ecf20Sopenharmony_ci{
7558c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 16) & 0xff;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
7588c2ecf20Sopenharmony_ci	uinfo->count = 1;
7598c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
7608c2ecf20Sopenharmony_ci	uinfo->value.integer.max = mask;
7618c2ecf20Sopenharmony_ci	return 0;
7628c2ecf20Sopenharmony_ci}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_cistatic int snd_ad1816a_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
7658c2ecf20Sopenharmony_ci{
7668c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
7678c2ecf20Sopenharmony_ci	unsigned long flags;
7688c2ecf20Sopenharmony_ci	int reg = kcontrol->private_value & 0xff;
7698c2ecf20Sopenharmony_ci	int shift = (kcontrol->private_value >> 8) & 0xff;
7708c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 16) & 0xff;
7718c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 24) & 0xff;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
7748c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = (snd_ad1816a_read(chip, reg) >> shift) & mask;
7758c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
7768c2ecf20Sopenharmony_ci	if (invert)
7778c2ecf20Sopenharmony_ci		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
7788c2ecf20Sopenharmony_ci	return 0;
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_cistatic int snd_ad1816a_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
7828c2ecf20Sopenharmony_ci{
7838c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
7848c2ecf20Sopenharmony_ci	unsigned long flags;
7858c2ecf20Sopenharmony_ci	int reg = kcontrol->private_value & 0xff;
7868c2ecf20Sopenharmony_ci	int shift = (kcontrol->private_value >> 8) & 0xff;
7878c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 16) & 0xff;
7888c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 24) & 0xff;
7898c2ecf20Sopenharmony_ci	int change;
7908c2ecf20Sopenharmony_ci	unsigned short old_val, val;
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	val = (ucontrol->value.integer.value[0] & mask);
7938c2ecf20Sopenharmony_ci	if (invert)
7948c2ecf20Sopenharmony_ci		val = mask - val;
7958c2ecf20Sopenharmony_ci	val <<= shift;
7968c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
7978c2ecf20Sopenharmony_ci	old_val = snd_ad1816a_read(chip, reg);
7988c2ecf20Sopenharmony_ci	val = (old_val & ~(mask << shift)) | val;
7998c2ecf20Sopenharmony_ci	change = val != old_val;
8008c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, reg, val);
8018c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
8028c2ecf20Sopenharmony_ci	return change;
8038c2ecf20Sopenharmony_ci}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci#define AD1816A_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
8068c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
8078c2ecf20Sopenharmony_ci  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
8088c2ecf20Sopenharmony_ci  .name = xname, .info = snd_ad1816a_info_double,		\
8098c2ecf20Sopenharmony_ci  .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
8108c2ecf20Sopenharmony_ci  .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
8118c2ecf20Sopenharmony_ci  .tlv = { .p = (xtlv) } }
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci#define AD1816A_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
8148c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_double, \
8158c2ecf20Sopenharmony_ci  .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
8168c2ecf20Sopenharmony_ci  .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_cistatic int snd_ad1816a_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
8198c2ecf20Sopenharmony_ci{
8208c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 16) & 0xff;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
8238c2ecf20Sopenharmony_ci	uinfo->count = 2;
8248c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
8258c2ecf20Sopenharmony_ci	uinfo->value.integer.max = mask;
8268c2ecf20Sopenharmony_ci	return 0;
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_cistatic int snd_ad1816a_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
8308c2ecf20Sopenharmony_ci{
8318c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
8328c2ecf20Sopenharmony_ci	unsigned long flags;
8338c2ecf20Sopenharmony_ci	int reg = kcontrol->private_value & 0xff;
8348c2ecf20Sopenharmony_ci	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
8358c2ecf20Sopenharmony_ci	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
8368c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 16) & 0xff;
8378c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 24) & 0xff;
8388c2ecf20Sopenharmony_ci	unsigned short val;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
8418c2ecf20Sopenharmony_ci	val = snd_ad1816a_read(chip, reg);
8428c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
8438c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
8448c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
8458c2ecf20Sopenharmony_ci	if (invert) {
8468c2ecf20Sopenharmony_ci		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
8478c2ecf20Sopenharmony_ci		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
8488c2ecf20Sopenharmony_ci	}
8498c2ecf20Sopenharmony_ci	return 0;
8508c2ecf20Sopenharmony_ci}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_cistatic int snd_ad1816a_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
8558c2ecf20Sopenharmony_ci	unsigned long flags;
8568c2ecf20Sopenharmony_ci	int reg = kcontrol->private_value & 0xff;
8578c2ecf20Sopenharmony_ci	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
8588c2ecf20Sopenharmony_ci	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
8598c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 16) & 0xff;
8608c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 24) & 0xff;
8618c2ecf20Sopenharmony_ci	int change;
8628c2ecf20Sopenharmony_ci	unsigned short old_val, val1, val2;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	val1 = ucontrol->value.integer.value[0] & mask;
8658c2ecf20Sopenharmony_ci	val2 = ucontrol->value.integer.value[1] & mask;
8668c2ecf20Sopenharmony_ci	if (invert) {
8678c2ecf20Sopenharmony_ci		val1 = mask - val1;
8688c2ecf20Sopenharmony_ci		val2 = mask - val2;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci	val1 <<= shift_left;
8718c2ecf20Sopenharmony_ci	val2 <<= shift_right;
8728c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->lock, flags);
8738c2ecf20Sopenharmony_ci	old_val = snd_ad1816a_read(chip, reg);
8748c2ecf20Sopenharmony_ci	val1 = (old_val & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
8758c2ecf20Sopenharmony_ci	change = val1 != old_val;
8768c2ecf20Sopenharmony_ci	snd_ad1816a_write(chip, reg, val1);
8778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->lock, flags);
8788c2ecf20Sopenharmony_ci	return change;
8798c2ecf20Sopenharmony_ci}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
8828c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
8838c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
8848c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
8858c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_ad1816a_controls[] = {
8888c2ecf20Sopenharmony_ciAD1816A_DOUBLE("Master Playback Switch", AD1816A_MASTER_ATT, 15, 7, 1, 1),
8898c2ecf20Sopenharmony_ciAD1816A_DOUBLE_TLV("Master Playback Volume", AD1816A_MASTER_ATT, 8, 0, 31, 1,
8908c2ecf20Sopenharmony_ci		   db_scale_5bit),
8918c2ecf20Sopenharmony_ciAD1816A_DOUBLE("PCM Playback Switch", AD1816A_VOICE_ATT, 15, 7, 1, 1),
8928c2ecf20Sopenharmony_ciAD1816A_DOUBLE_TLV("PCM Playback Volume", AD1816A_VOICE_ATT, 8, 0, 63, 1,
8938c2ecf20Sopenharmony_ci		   db_scale_6bit),
8948c2ecf20Sopenharmony_ciAD1816A_DOUBLE("Line Playback Switch", AD1816A_LINE_GAIN_ATT, 15, 7, 1, 1),
8958c2ecf20Sopenharmony_ciAD1816A_DOUBLE_TLV("Line Playback Volume", AD1816A_LINE_GAIN_ATT, 8, 0, 31, 1,
8968c2ecf20Sopenharmony_ci		   db_scale_5bit_12db_max),
8978c2ecf20Sopenharmony_ciAD1816A_DOUBLE("CD Playback Switch", AD1816A_CD_GAIN_ATT, 15, 7, 1, 1),
8988c2ecf20Sopenharmony_ciAD1816A_DOUBLE_TLV("CD Playback Volume", AD1816A_CD_GAIN_ATT, 8, 0, 31, 1,
8998c2ecf20Sopenharmony_ci		   db_scale_5bit_12db_max),
9008c2ecf20Sopenharmony_ciAD1816A_DOUBLE("Synth Playback Switch", AD1816A_SYNTH_GAIN_ATT, 15, 7, 1, 1),
9018c2ecf20Sopenharmony_ciAD1816A_DOUBLE_TLV("Synth Playback Volume", AD1816A_SYNTH_GAIN_ATT, 8, 0, 31, 1,
9028c2ecf20Sopenharmony_ci		   db_scale_5bit_12db_max),
9038c2ecf20Sopenharmony_ciAD1816A_DOUBLE("FM Playback Switch", AD1816A_FM_ATT, 15, 7, 1, 1),
9048c2ecf20Sopenharmony_ciAD1816A_DOUBLE_TLV("FM Playback Volume", AD1816A_FM_ATT, 8, 0, 63, 1,
9058c2ecf20Sopenharmony_ci		   db_scale_6bit),
9068c2ecf20Sopenharmony_ciAD1816A_SINGLE("Mic Playback Switch", AD1816A_MIC_GAIN_ATT, 15, 1, 1),
9078c2ecf20Sopenharmony_ciAD1816A_SINGLE_TLV("Mic Playback Volume", AD1816A_MIC_GAIN_ATT, 8, 31, 1,
9088c2ecf20Sopenharmony_ci		   db_scale_5bit_12db_max),
9098c2ecf20Sopenharmony_ciAD1816A_SINGLE("Mic Boost", AD1816A_MIC_GAIN_ATT, 14, 1, 0),
9108c2ecf20Sopenharmony_ciAD1816A_DOUBLE("Video Playback Switch", AD1816A_VID_GAIN_ATT, 15, 7, 1, 1),
9118c2ecf20Sopenharmony_ciAD1816A_DOUBLE_TLV("Video Playback Volume", AD1816A_VID_GAIN_ATT, 8, 0, 31, 1,
9128c2ecf20Sopenharmony_ci		   db_scale_5bit_12db_max),
9138c2ecf20Sopenharmony_ciAD1816A_SINGLE("Phone Capture Switch", AD1816A_PHONE_IN_GAIN_ATT, 15, 1, 1),
9148c2ecf20Sopenharmony_ciAD1816A_SINGLE_TLV("Phone Capture Volume", AD1816A_PHONE_IN_GAIN_ATT, 0, 15, 1,
9158c2ecf20Sopenharmony_ci		   db_scale_4bit),
9168c2ecf20Sopenharmony_ciAD1816A_SINGLE("Phone Playback Switch", AD1816A_PHONE_OUT_ATT, 7, 1, 1),
9178c2ecf20Sopenharmony_ciAD1816A_SINGLE_TLV("Phone Playback Volume", AD1816A_PHONE_OUT_ATT, 0, 31, 1,
9188c2ecf20Sopenharmony_ci		   db_scale_5bit),
9198c2ecf20Sopenharmony_ci{
9208c2ecf20Sopenharmony_ci	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
9218c2ecf20Sopenharmony_ci	.name = "Capture Source",
9228c2ecf20Sopenharmony_ci	.info = snd_ad1816a_info_mux,
9238c2ecf20Sopenharmony_ci	.get = snd_ad1816a_get_mux,
9248c2ecf20Sopenharmony_ci	.put = snd_ad1816a_put_mux,
9258c2ecf20Sopenharmony_ci},
9268c2ecf20Sopenharmony_ciAD1816A_DOUBLE("Capture Switch", AD1816A_ADC_PGA, 15, 7, 1, 1),
9278c2ecf20Sopenharmony_ciAD1816A_DOUBLE_TLV("Capture Volume", AD1816A_ADC_PGA, 8, 0, 15, 0,
9288c2ecf20Sopenharmony_ci		   db_scale_rec_gain),
9298c2ecf20Sopenharmony_ciAD1816A_SINGLE("3D Control - Switch", AD1816A_3D_PHAT_CTRL, 15, 1, 1),
9308c2ecf20Sopenharmony_ciAD1816A_SINGLE("3D Control - Level", AD1816A_3D_PHAT_CTRL, 0, 15, 0),
9318c2ecf20Sopenharmony_ci};
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ciint snd_ad1816a_mixer(struct snd_ad1816a *chip)
9348c2ecf20Sopenharmony_ci{
9358c2ecf20Sopenharmony_ci	struct snd_card *card;
9368c2ecf20Sopenharmony_ci	unsigned int idx;
9378c2ecf20Sopenharmony_ci	int err;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!chip || !chip->card))
9408c2ecf20Sopenharmony_ci		return -EINVAL;
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	card = chip->card;
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	strcpy(card->mixername, snd_ad1816a_chip_id(chip));
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	for (idx = 0; idx < ARRAY_SIZE(snd_ad1816a_controls); idx++) {
9478c2ecf20Sopenharmony_ci		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ad1816a_controls[idx], chip))) < 0)
9488c2ecf20Sopenharmony_ci			return err;
9498c2ecf20Sopenharmony_ci	}
9508c2ecf20Sopenharmony_ci	return 0;
9518c2ecf20Sopenharmony_ci}
952