// SPDX-License-Identifier: GPL-2.0
//
// nau8822.c -- NAU8822 ALSA Soc Audio driver
//
// Copyright 2017 Nuvoton Technology Crop.
//
// Author: David Lin <ctlin0@nuvoton.com>
// Co-author: John Hsu <kchsu0@nuvoton.com>
// Co-author: Seven Li <wtli@nuvoton.com>
//
// Based on WM8974.c
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#include <asm/div64.h>
#include "nau8822.h"
#define NAU_PLL_FREQ_MAX 100000000
#define NAU_PLL_FREQ_MIN 90000000
#define NAU_PLL_REF_MAX 33000000
#define NAU_PLL_REF_MIN 8000000
#define NAU_PLL_OPTOP_MIN 6
static const int nau8822_mclk_scaler[] = { 10, 15, 20, 30, 40, 60, 80, 120 };
static const struct reg_default nau8822_reg_defaults[] = {
{ NAU8822_REG_POWER_MANAGEMENT_1, 0x0000 },
{ NAU8822_REG_POWER_MANAGEMENT_2, 0x0000 },
{ NAU8822_REG_POWER_MANAGEMENT_3, 0x0000 },
{ NAU8822_REG_AUDIO_INTERFACE, 0x0050 },
{ NAU8822_REG_COMPANDING_CONTROL, 0x0000 },
{ NAU8822_REG_CLOCKING, 0x0140 },
{ NAU8822_REG_ADDITIONAL_CONTROL, 0x0000 },
{ NAU8822_REG_GPIO_CONTROL, 0x0000 },
{ NAU8822_REG_JACK_DETECT_CONTROL_1, 0x0000 },
{ NAU8822_REG_DAC_CONTROL, 0x0000 },
{ NAU8822_REG_LEFT_DAC_DIGITAL_VOLUME, 0x00ff },
{ NAU8822_REG_RIGHT_DAC_DIGITAL_VOLUME, 0x00ff },
{ NAU8822_REG_JACK_DETECT_CONTROL_2, 0x0000 },
{ NAU8822_REG_ADC_CONTROL, 0x0100 },
{ NAU8822_REG_LEFT_ADC_DIGITAL_VOLUME, 0x00ff },
{ NAU8822_REG_RIGHT_ADC_DIGITAL_VOLUME, 0x00ff },
{ NAU8822_REG_EQ1, 0x012c },
{ NAU8822_REG_EQ2, 0x002c },
{ NAU8822_REG_EQ3, 0x002c },
{ NAU8822_REG_EQ4, 0x002c },
{ NAU8822_REG_EQ5, 0x002c },
{ NAU8822_REG_DAC_LIMITER_1, 0x0032 },
{ NAU8822_REG_DAC_LIMITER_2, 0x0000 },
{ NAU8822_REG_NOTCH_FILTER_1, 0x0000 },
{ NAU8822_REG_NOTCH_FILTER_2, 0x0000 },
{ NAU8822_REG_NOTCH_FILTER_3, 0x0000 },
{ NAU8822_REG_NOTCH_FILTER_4, 0x0000 },
{ NAU8822_REG_ALC_CONTROL_1, 0x0038 },
{ NAU8822_REG_ALC_CONTROL_2, 0x000b },
{ NAU8822_REG_ALC_CONTROL_3, 0x0032 },
{ NAU8822_REG_NOISE_GATE, 0x0010 },
{ NAU8822_REG_PLL_N, 0x0008 },
{ NAU8822_REG_PLL_K1, 0x000c },
{ NAU8822_REG_PLL_K2, 0x0093 },
{ NAU8822_REG_PLL_K3, 0x00e9 },
{ NAU8822_REG_3D_CONTROL, 0x0000 },
{ NAU8822_REG_RIGHT_SPEAKER_CONTROL, 0x0000 },
{ NAU8822_REG_INPUT_CONTROL, 0x0033 },
{ NAU8822_REG_LEFT_INP_PGA_CONTROL, 0x0010 },
{ NAU8822_REG_RIGHT_INP_PGA_CONTROL, 0x0010 },
{ NAU8822_REG_LEFT_ADC_BOOST_CONTROL, 0x0100 },
{ NAU8822_REG_RIGHT_ADC_BOOST_CONTROL, 0x0100 },
{ NAU8822_REG_OUTPUT_CONTROL, 0x0002 },
{ NAU8822_REG_LEFT_MIXER_CONTROL, 0x0001 },
{ NAU8822_REG_RIGHT_MIXER_CONTROL, 0x0001 },
{ NAU8822_REG_LHP_VOLUME, 0x0039 },
{ NAU8822_REG_RHP_VOLUME, 0x0039 },
{ NAU8822_REG_LSPKOUT_VOLUME, 0x0039 },
{ NAU8822_REG_RSPKOUT_VOLUME, 0x0039 },
{ NAU8822_REG_AUX2_MIXER, 0x0001 },
{ NAU8822_REG_AUX1_MIXER, 0x0001 },
{ NAU8822_REG_POWER_MANAGEMENT_4, 0x0000 },
{ NAU8822_REG_LEFT_TIME_SLOT, 0x0000 },
{ NAU8822_REG_MISC, 0x0020 },
{