18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// MAX9867 ALSA SoC codec driver
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright 2013-2015 Maxim Integrated Products
68c2ecf20Sopenharmony_ci// Copyright 2018 Ladislav Michl <ladis@linux-mips.org>
78c2ecf20Sopenharmony_ci//
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/i2c.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/regmap.h>
138c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
148c2ecf20Sopenharmony_ci#include <sound/soc.h>
158c2ecf20Sopenharmony_ci#include <sound/tlv.h>
168c2ecf20Sopenharmony_ci#include "max9867.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistruct max9867_priv {
198c2ecf20Sopenharmony_ci	struct regmap *regmap;
208c2ecf20Sopenharmony_ci	const struct snd_pcm_hw_constraint_list *constraints;
218c2ecf20Sopenharmony_ci	unsigned int sysclk, pclk;
228c2ecf20Sopenharmony_ci	bool master, dsp_a;
238c2ecf20Sopenharmony_ci	unsigned int adc_dac_active;
248c2ecf20Sopenharmony_ci};
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic const char *const max9867_spmode[] = {
278c2ecf20Sopenharmony_ci	"Stereo Diff", "Mono Diff",
288c2ecf20Sopenharmony_ci	"Stereo Cap", "Mono Cap",
298c2ecf20Sopenharmony_ci	"Stereo Single", "Mono Single",
308c2ecf20Sopenharmony_ci	"Stereo Single Fast", "Mono Single Fast"
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_cistatic const char *const max9867_filter_text[] = {"IIR", "FIR"};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic const char *const max9867_adc_dac_filter_text[] = {
358c2ecf20Sopenharmony_ci	"Disabled",
368c2ecf20Sopenharmony_ci	"Elliptical/16/256",
378c2ecf20Sopenharmony_ci	"Butterworth/16/500",
388c2ecf20Sopenharmony_ci	"Elliptical/8/256",
398c2ecf20Sopenharmony_ci	"Butterworth/8/500",
408c2ecf20Sopenharmony_ci	"Butterworth/8-24"
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cienum max9867_adc_dac {
448c2ecf20Sopenharmony_ci	MAX9867_ADC_LEFT,
458c2ecf20Sopenharmony_ci	MAX9867_ADC_RIGHT,
468c2ecf20Sopenharmony_ci	MAX9867_DAC_LEFT,
478c2ecf20Sopenharmony_ci	MAX9867_DAC_RIGHT,
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int max9867_adc_dac_event(struct snd_soc_dapm_widget *w,
518c2ecf20Sopenharmony_ci	struct snd_kcontrol *kcontrol, int event)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
548c2ecf20Sopenharmony_ci	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
558c2ecf20Sopenharmony_ci	enum max9867_adc_dac adc_dac;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (!strcmp(w->name, "ADCL"))
588c2ecf20Sopenharmony_ci		adc_dac = MAX9867_ADC_LEFT;
598c2ecf20Sopenharmony_ci	else if (!strcmp(w->name, "ADCR"))
608c2ecf20Sopenharmony_ci		adc_dac = MAX9867_ADC_RIGHT;
618c2ecf20Sopenharmony_ci	else if (!strcmp(w->name, "DACL"))
628c2ecf20Sopenharmony_ci		adc_dac = MAX9867_DAC_LEFT;
638c2ecf20Sopenharmony_ci	else if (!strcmp(w->name, "DACR"))
648c2ecf20Sopenharmony_ci		adc_dac = MAX9867_DAC_RIGHT;
658c2ecf20Sopenharmony_ci	else
668c2ecf20Sopenharmony_ci		return 0;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (SND_SOC_DAPM_EVENT_ON(event))
698c2ecf20Sopenharmony_ci		max9867->adc_dac_active |= BIT(adc_dac);
708c2ecf20Sopenharmony_ci	else if (SND_SOC_DAPM_EVENT_OFF(event))
718c2ecf20Sopenharmony_ci		max9867->adc_dac_active &= ~BIT(adc_dac);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return 0;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic int max9867_filter_get(struct snd_kcontrol *kcontrol,
778c2ecf20Sopenharmony_ci			      struct snd_ctl_elem_value *ucontrol)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
808c2ecf20Sopenharmony_ci	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
818c2ecf20Sopenharmony_ci	unsigned int reg;
828c2ecf20Sopenharmony_ci	int ret;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	ret = regmap_read(max9867->regmap, MAX9867_CODECFLTR, &reg);
858c2ecf20Sopenharmony_ci	if (ret)
868c2ecf20Sopenharmony_ci		return -EINVAL;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (reg & MAX9867_CODECFLTR_MODE)
898c2ecf20Sopenharmony_ci		ucontrol->value.enumerated.item[0] = 1;
908c2ecf20Sopenharmony_ci	else
918c2ecf20Sopenharmony_ci		ucontrol->value.enumerated.item[0] = 0;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	return 0;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic int max9867_filter_set(struct snd_kcontrol *kcontrol,
978c2ecf20Sopenharmony_ci			      struct snd_ctl_elem_value *ucontrol)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1008c2ecf20Sopenharmony_ci	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
1018c2ecf20Sopenharmony_ci	unsigned int reg, mode = ucontrol->value.enumerated.item[0];
1028c2ecf20Sopenharmony_ci	int ret;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (mode > 1)
1058c2ecf20Sopenharmony_ci		return -EINVAL;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* don't allow change if ADC/DAC active */
1088c2ecf20Sopenharmony_ci	if (max9867->adc_dac_active)
1098c2ecf20Sopenharmony_ci		return -EBUSY;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/* read current filter mode */
1128c2ecf20Sopenharmony_ci	ret = regmap_read(max9867->regmap, MAX9867_CODECFLTR, &reg);
1138c2ecf20Sopenharmony_ci	if (ret)
1148c2ecf20Sopenharmony_ci		return -EINVAL;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (mode)
1178c2ecf20Sopenharmony_ci		mode = MAX9867_CODECFLTR_MODE;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/* check if change is needed */
1208c2ecf20Sopenharmony_ci	if ((reg & MAX9867_CODECFLTR_MODE) == mode)
1218c2ecf20Sopenharmony_ci		return 0;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* shutdown codec before switching filter mode */
1248c2ecf20Sopenharmony_ci	regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
1258c2ecf20Sopenharmony_ci		MAX9867_PWRMAN_SHDN, 0);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/* switch filter mode */
1288c2ecf20Sopenharmony_ci	regmap_update_bits(max9867->regmap, MAX9867_CODECFLTR,
1298c2ecf20Sopenharmony_ci		MAX9867_CODECFLTR_MODE, mode);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	/* out of shutdown now */
1328c2ecf20Sopenharmony_ci	regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
1338c2ecf20Sopenharmony_ci		MAX9867_PWRMAN_SHDN, MAX9867_PWRMAN_SHDN);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return 0;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_EXT_DECL(max9867_filter, max9867_filter_text);
1398c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(max9867_dac_filter, MAX9867_CODECFLTR, 0,
1408c2ecf20Sopenharmony_ci	max9867_adc_dac_filter_text);
1418c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(max9867_adc_filter, MAX9867_CODECFLTR, 4,
1428c2ecf20Sopenharmony_ci	max9867_adc_dac_filter_text);
1438c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(max9867_spkmode, MAX9867_MODECONFIG, 0,
1448c2ecf20Sopenharmony_ci	max9867_spmode);
1458c2ecf20Sopenharmony_cistatic const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(max9867_master_tlv,
1468c2ecf20Sopenharmony_ci	 0,  2, TLV_DB_SCALE_ITEM(-8600, 200, 1),
1478c2ecf20Sopenharmony_ci	 3, 17, TLV_DB_SCALE_ITEM(-7800, 400, 0),
1488c2ecf20Sopenharmony_ci	18, 25, TLV_DB_SCALE_ITEM(-2000, 200, 0),
1498c2ecf20Sopenharmony_ci	26, 34, TLV_DB_SCALE_ITEM( -500, 100, 0),
1508c2ecf20Sopenharmony_ci	35, 40, TLV_DB_SCALE_ITEM(  350,  50, 0),
1518c2ecf20Sopenharmony_ci);
1528c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_SCALE(max9867_mic_tlv, 0, 100, 0);
1538c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_SCALE(max9867_line_tlv, -600, 200, 0);
1548c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_SCALE(max9867_adc_tlv, -1200, 100, 0);
1558c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_SCALE(max9867_dac_tlv, -1500, 100, 0);
1568c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_SCALE(max9867_dacboost_tlv, 0, 600, 0);
1578c2ecf20Sopenharmony_cistatic const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(max9867_micboost_tlv,
1588c2ecf20Sopenharmony_ci	0, 2, TLV_DB_SCALE_ITEM(-2000, 2000, 1),
1598c2ecf20Sopenharmony_ci	3, 3, TLV_DB_SCALE_ITEM(3000, 0, 0),
1608c2ecf20Sopenharmony_ci);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max9867_snd_controls[] = {
1638c2ecf20Sopenharmony_ci	SOC_DOUBLE_R_TLV("Master Playback Volume", MAX9867_LEFTVOL,
1648c2ecf20Sopenharmony_ci			MAX9867_RIGHTVOL, 0, 40, 1, max9867_master_tlv),
1658c2ecf20Sopenharmony_ci	SOC_DOUBLE_R_TLV("Line Capture Volume", MAX9867_LEFTLINELVL,
1668c2ecf20Sopenharmony_ci			MAX9867_RIGHTLINELVL, 0, 15, 1, max9867_line_tlv),
1678c2ecf20Sopenharmony_ci	SOC_DOUBLE_R_TLV("Mic Capture Volume", MAX9867_LEFTMICGAIN,
1688c2ecf20Sopenharmony_ci			MAX9867_RIGHTMICGAIN, 0, 20, 1, max9867_mic_tlv),
1698c2ecf20Sopenharmony_ci	SOC_DOUBLE_R_TLV("Mic Boost Capture Volume", MAX9867_LEFTMICGAIN,
1708c2ecf20Sopenharmony_ci			MAX9867_RIGHTMICGAIN, 5, 3, 0, max9867_micboost_tlv),
1718c2ecf20Sopenharmony_ci	SOC_SINGLE("Digital Sidetone Volume", MAX9867_SIDETONE, 0, 31, 1),
1728c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("Digital Playback Volume", MAX9867_DACLEVEL, 0, 15, 1,
1738c2ecf20Sopenharmony_ci			max9867_dac_tlv),
1748c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("Digital Boost Playback Volume", MAX9867_DACLEVEL, 4, 3, 0,
1758c2ecf20Sopenharmony_ci			max9867_dacboost_tlv),
1768c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("Digital Capture Volume", MAX9867_ADCLEVEL, 4, 0, 15, 1,
1778c2ecf20Sopenharmony_ci			max9867_adc_tlv),
1788c2ecf20Sopenharmony_ci	SOC_ENUM("Speaker Mode", max9867_spkmode),
1798c2ecf20Sopenharmony_ci	SOC_SINGLE("Volume Smoothing Switch", MAX9867_MODECONFIG, 6, 1, 0),
1808c2ecf20Sopenharmony_ci	SOC_SINGLE("Line ZC Switch", MAX9867_MODECONFIG, 5, 1, 0),
1818c2ecf20Sopenharmony_ci	SOC_ENUM_EXT("DSP Filter", max9867_filter, max9867_filter_get, max9867_filter_set),
1828c2ecf20Sopenharmony_ci	SOC_ENUM("ADC Filter", max9867_adc_filter),
1838c2ecf20Sopenharmony_ci	SOC_ENUM("DAC Filter", max9867_dac_filter),
1848c2ecf20Sopenharmony_ci	SOC_SINGLE("Mono Playback Switch", MAX9867_IFC1B, 3, 1, 0),
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/* Input mixer */
1888c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max9867_input_mixer_controls[] = {
1898c2ecf20Sopenharmony_ci	SOC_DAPM_DOUBLE("Line Capture Switch", MAX9867_INPUTCONFIG, 7, 5, 1, 0),
1908c2ecf20Sopenharmony_ci	SOC_DAPM_DOUBLE("Mic Capture Switch", MAX9867_INPUTCONFIG, 6, 4, 1, 0),
1918c2ecf20Sopenharmony_ci};
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/* Output mixer */
1948c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max9867_output_mixer_controls[] = {
1958c2ecf20Sopenharmony_ci	SOC_DAPM_DOUBLE_R("Line Bypass Switch",
1968c2ecf20Sopenharmony_ci			  MAX9867_LEFTLINELVL, MAX9867_RIGHTLINELVL, 6, 1, 1),
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/* Sidetone mixer */
2008c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max9867_sidetone_mixer_controls[] = {
2018c2ecf20Sopenharmony_ci	SOC_DAPM_DOUBLE("Sidetone Switch", MAX9867_SIDETONE, 6, 7, 1, 0),
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* Line out switch */
2058c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max9867_line_out_control =
2068c2ecf20Sopenharmony_ci	SOC_DAPM_DOUBLE_R("Switch",
2078c2ecf20Sopenharmony_ci			  MAX9867_LEFTVOL, MAX9867_RIGHTVOL, 6, 1, 1);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci/* DMIC mux */
2108c2ecf20Sopenharmony_cistatic const char *const dmic_mux_text[] = {
2118c2ecf20Sopenharmony_ci	"ADC", "DMIC"
2128c2ecf20Sopenharmony_ci};
2138c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(left_dmic_mux_enum,
2148c2ecf20Sopenharmony_ci			    MAX9867_MICCONFIG, 5, dmic_mux_text);
2158c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(right_dmic_mux_enum,
2168c2ecf20Sopenharmony_ci			    MAX9867_MICCONFIG, 4, dmic_mux_text);
2178c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max9867_left_dmic_mux =
2188c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("DMICL Mux", left_dmic_mux_enum);
2198c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max9867_right_dmic_mux =
2208c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("DMICR Mux", right_dmic_mux_enum);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget max9867_dapm_widgets[] = {
2238c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("MICL"),
2248c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("MICR"),
2258c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("DMICL"),
2268c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("DMICR"),
2278c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("LINL"),
2288c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("LINR"),
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("Left Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
2318c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("Right Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
2328c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER_NAMED_CTL("Input Mixer", SND_SOC_NOPM, 0, 0,
2338c2ecf20Sopenharmony_ci				     max9867_input_mixer_controls,
2348c2ecf20Sopenharmony_ci				     ARRAY_SIZE(max9867_input_mixer_controls)),
2358c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DMICL Mux", SND_SOC_NOPM, 0, 0,
2368c2ecf20Sopenharmony_ci			 &max9867_left_dmic_mux),
2378c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DMICR Mux", SND_SOC_NOPM, 0, 0,
2388c2ecf20Sopenharmony_ci			 &max9867_right_dmic_mux),
2398c2ecf20Sopenharmony_ci	SND_SOC_DAPM_ADC_E("ADCL", "HiFi Capture", SND_SOC_NOPM, 0, 0,
2408c2ecf20Sopenharmony_ci			   max9867_adc_dac_event,
2418c2ecf20Sopenharmony_ci			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2428c2ecf20Sopenharmony_ci	SND_SOC_DAPM_ADC_E("ADCR", "HiFi Capture", SND_SOC_NOPM, 0, 0,
2438c2ecf20Sopenharmony_ci			   max9867_adc_dac_event,
2448c2ecf20Sopenharmony_ci			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Digital", SND_SOC_NOPM, 0, 0,
2478c2ecf20Sopenharmony_ci			   max9867_sidetone_mixer_controls,
2488c2ecf20Sopenharmony_ci			   ARRAY_SIZE(max9867_sidetone_mixer_controls)),
2498c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", SND_SOC_NOPM, 0, 0,
2508c2ecf20Sopenharmony_ci				     max9867_output_mixer_controls,
2518c2ecf20Sopenharmony_ci				     ARRAY_SIZE(max9867_output_mixer_controls)),
2528c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC_E("DACL", "HiFi Playback", SND_SOC_NOPM, 0, 0,
2538c2ecf20Sopenharmony_ci			   max9867_adc_dac_event,
2548c2ecf20Sopenharmony_ci			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2558c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC_E("DACR", "HiFi Playback", SND_SOC_NOPM, 0, 0,
2568c2ecf20Sopenharmony_ci			   max9867_adc_dac_event,
2578c2ecf20Sopenharmony_ci			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2588c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SWITCH("Master Playback", SND_SOC_NOPM, 0, 0,
2598c2ecf20Sopenharmony_ci			    &max9867_line_out_control),
2608c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("LOUT"),
2618c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("ROUT"),
2628c2ecf20Sopenharmony_ci};
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route max9867_audio_map[] = {
2658c2ecf20Sopenharmony_ci	{"Left Line Input", NULL, "LINL"},
2668c2ecf20Sopenharmony_ci	{"Right Line Input", NULL, "LINR"},
2678c2ecf20Sopenharmony_ci	{"Input Mixer", "Mic Capture Switch", "MICL"},
2688c2ecf20Sopenharmony_ci	{"Input Mixer", "Mic Capture Switch", "MICR"},
2698c2ecf20Sopenharmony_ci	{"Input Mixer", "Line Capture Switch", "Left Line Input"},
2708c2ecf20Sopenharmony_ci	{"Input Mixer", "Line Capture Switch", "Right Line Input"},
2718c2ecf20Sopenharmony_ci	{"DMICL Mux", "DMIC", "DMICL"},
2728c2ecf20Sopenharmony_ci	{"DMICR Mux", "DMIC", "DMICR"},
2738c2ecf20Sopenharmony_ci	{"DMICL Mux", "ADC", "Input Mixer"},
2748c2ecf20Sopenharmony_ci	{"DMICR Mux", "ADC", "Input Mixer"},
2758c2ecf20Sopenharmony_ci	{"ADCL", NULL, "DMICL Mux"},
2768c2ecf20Sopenharmony_ci	{"ADCR", NULL, "DMICR Mux"},
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	{"Digital", "Sidetone Switch", "ADCL"},
2798c2ecf20Sopenharmony_ci	{"Digital", "Sidetone Switch", "ADCR"},
2808c2ecf20Sopenharmony_ci	{"DACL", NULL, "Digital"},
2818c2ecf20Sopenharmony_ci	{"DACR", NULL, "Digital"},
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	{"Output Mixer", "Line Bypass Switch", "Left Line Input"},
2848c2ecf20Sopenharmony_ci	{"Output Mixer", "Line Bypass Switch", "Right Line Input"},
2858c2ecf20Sopenharmony_ci	{"Output Mixer", NULL, "DACL"},
2868c2ecf20Sopenharmony_ci	{"Output Mixer", NULL, "DACR"},
2878c2ecf20Sopenharmony_ci	{"Master Playback", "Switch", "Output Mixer"},
2888c2ecf20Sopenharmony_ci	{"LOUT", NULL, "Master Playback"},
2898c2ecf20Sopenharmony_ci	{"ROUT", NULL, "Master Playback"},
2908c2ecf20Sopenharmony_ci};
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic const unsigned int max9867_rates_44k1[] = {
2938c2ecf20Sopenharmony_ci	11025, 22050, 44100,
2948c2ecf20Sopenharmony_ci};
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic const struct snd_pcm_hw_constraint_list max9867_constraints_44k1 = {
2978c2ecf20Sopenharmony_ci	.list = max9867_rates_44k1,
2988c2ecf20Sopenharmony_ci	.count = ARRAY_SIZE(max9867_rates_44k1),
2998c2ecf20Sopenharmony_ci};
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic const unsigned int max9867_rates_48k[] = {
3028c2ecf20Sopenharmony_ci	8000, 16000, 32000, 48000,
3038c2ecf20Sopenharmony_ci};
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic const struct snd_pcm_hw_constraint_list max9867_constraints_48k = {
3068c2ecf20Sopenharmony_ci	.list = max9867_rates_48k,
3078c2ecf20Sopenharmony_ci	.count = ARRAY_SIZE(max9867_rates_48k),
3088c2ecf20Sopenharmony_ci};
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic int max9867_startup(struct snd_pcm_substream *substream,
3118c2ecf20Sopenharmony_ci			   struct snd_soc_dai *dai)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci        struct max9867_priv *max9867 =
3148c2ecf20Sopenharmony_ci		snd_soc_component_get_drvdata(dai->component);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (max9867->constraints)
3178c2ecf20Sopenharmony_ci		snd_pcm_hw_constraint_list(substream->runtime, 0,
3188c2ecf20Sopenharmony_ci			SNDRV_PCM_HW_PARAM_RATE, max9867->constraints);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return 0;
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic int max9867_dai_hw_params(struct snd_pcm_substream *substream,
3248c2ecf20Sopenharmony_ci		struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	int value;
3278c2ecf20Sopenharmony_ci	unsigned long int rate, ratio;
3288c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
3298c2ecf20Sopenharmony_ci	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
3308c2ecf20Sopenharmony_ci	unsigned int ni = DIV_ROUND_CLOSEST_ULL(96ULL * 0x10000 * params_rate(params),
3318c2ecf20Sopenharmony_ci						max9867->pclk);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/* set up the ni value */
3348c2ecf20Sopenharmony_ci	regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH,
3358c2ecf20Sopenharmony_ci		MAX9867_NI_HIGH_MASK, (0xFF00 & ni) >> 8);
3368c2ecf20Sopenharmony_ci	regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW,
3378c2ecf20Sopenharmony_ci		MAX9867_NI_LOW_MASK, 0x00FF & ni);
3388c2ecf20Sopenharmony_ci	if (max9867->master) {
3398c2ecf20Sopenharmony_ci		if (max9867->dsp_a) {
3408c2ecf20Sopenharmony_ci			value = MAX9867_IFC1B_48X;
3418c2ecf20Sopenharmony_ci		} else {
3428c2ecf20Sopenharmony_ci			rate = params_rate(params) * 2 * params_width(params);
3438c2ecf20Sopenharmony_ci			ratio = max9867->pclk / rate;
3448c2ecf20Sopenharmony_ci			switch (params_width(params)) {
3458c2ecf20Sopenharmony_ci			case 8:
3468c2ecf20Sopenharmony_ci			case 16:
3478c2ecf20Sopenharmony_ci				switch (ratio) {
3488c2ecf20Sopenharmony_ci				case 2:
3498c2ecf20Sopenharmony_ci					value = MAX9867_IFC1B_PCLK_2;
3508c2ecf20Sopenharmony_ci					break;
3518c2ecf20Sopenharmony_ci				case 4:
3528c2ecf20Sopenharmony_ci					value = MAX9867_IFC1B_PCLK_4;
3538c2ecf20Sopenharmony_ci					break;
3548c2ecf20Sopenharmony_ci				case 8:
3558c2ecf20Sopenharmony_ci					value = MAX9867_IFC1B_PCLK_8;
3568c2ecf20Sopenharmony_ci					break;
3578c2ecf20Sopenharmony_ci				case 16:
3588c2ecf20Sopenharmony_ci					value = MAX9867_IFC1B_PCLK_16;
3598c2ecf20Sopenharmony_ci					break;
3608c2ecf20Sopenharmony_ci				default:
3618c2ecf20Sopenharmony_ci					return -EINVAL;
3628c2ecf20Sopenharmony_ci				}
3638c2ecf20Sopenharmony_ci				break;
3648c2ecf20Sopenharmony_ci			case 24:
3658c2ecf20Sopenharmony_ci				value = MAX9867_IFC1B_48X;
3668c2ecf20Sopenharmony_ci				break;
3678c2ecf20Sopenharmony_ci			case 32:
3688c2ecf20Sopenharmony_ci				value = MAX9867_IFC1B_64X;
3698c2ecf20Sopenharmony_ci				break;
3708c2ecf20Sopenharmony_ci			default:
3718c2ecf20Sopenharmony_ci				return -EINVAL;
3728c2ecf20Sopenharmony_ci			}
3738c2ecf20Sopenharmony_ci		}
3748c2ecf20Sopenharmony_ci		regmap_update_bits(max9867->regmap, MAX9867_IFC1B,
3758c2ecf20Sopenharmony_ci			MAX9867_IFC1B_BCLK_MASK, value);
3768c2ecf20Sopenharmony_ci	} else {
3778c2ecf20Sopenharmony_ci		/*
3788c2ecf20Sopenharmony_ci		 * digital pll locks on to any externally supplied LRCLK signal
3798c2ecf20Sopenharmony_ci		 * and also enable rapid lock mode.
3808c2ecf20Sopenharmony_ci		 */
3818c2ecf20Sopenharmony_ci		regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW,
3828c2ecf20Sopenharmony_ci			MAX9867_RAPID_LOCK, MAX9867_RAPID_LOCK);
3838c2ecf20Sopenharmony_ci		regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH,
3848c2ecf20Sopenharmony_ci			MAX9867_PLL, MAX9867_PLL);
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci	return 0;
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic int max9867_mute(struct snd_soc_dai *dai, int mute, int direction)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
3928c2ecf20Sopenharmony_ci	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	return regmap_update_bits(max9867->regmap, MAX9867_DACLEVEL,
3958c2ecf20Sopenharmony_ci				  1 << 6, !!mute << 6);
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic int max9867_set_dai_sysclk(struct snd_soc_dai *codec_dai,
3998c2ecf20Sopenharmony_ci		int clk_id, unsigned int freq, int dir)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	struct snd_soc_component *component = codec_dai->component;
4028c2ecf20Sopenharmony_ci	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
4038c2ecf20Sopenharmony_ci	int value = 0;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	/* Set the prescaler based on the master clock frequency*/
4068c2ecf20Sopenharmony_ci	if (freq >= 10000000 && freq <= 20000000) {
4078c2ecf20Sopenharmony_ci		value |= MAX9867_PSCLK_10_20;
4088c2ecf20Sopenharmony_ci		max9867->pclk = freq;
4098c2ecf20Sopenharmony_ci	} else if (freq >= 20000000 && freq <= 40000000) {
4108c2ecf20Sopenharmony_ci		value |= MAX9867_PSCLK_20_40;
4118c2ecf20Sopenharmony_ci		max9867->pclk = freq / 2;
4128c2ecf20Sopenharmony_ci	} else if (freq >= 40000000 && freq <= 60000000) {
4138c2ecf20Sopenharmony_ci		value |= MAX9867_PSCLK_40_60;
4148c2ecf20Sopenharmony_ci		max9867->pclk = freq / 4;
4158c2ecf20Sopenharmony_ci	} else {
4168c2ecf20Sopenharmony_ci		dev_err(component->dev,
4178c2ecf20Sopenharmony_ci			"Invalid clock frequency %uHz (required 10-60MHz)\n",
4188c2ecf20Sopenharmony_ci			freq);
4198c2ecf20Sopenharmony_ci		return -EINVAL;
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci	if (freq % 48000 == 0)
4228c2ecf20Sopenharmony_ci		max9867->constraints = &max9867_constraints_48k;
4238c2ecf20Sopenharmony_ci	else if (freq % 44100 == 0)
4248c2ecf20Sopenharmony_ci		max9867->constraints = &max9867_constraints_44k1;
4258c2ecf20Sopenharmony_ci	else
4268c2ecf20Sopenharmony_ci		dev_warn(component->dev,
4278c2ecf20Sopenharmony_ci			 "Unable to set exact rate with %uHz clock frequency\n",
4288c2ecf20Sopenharmony_ci			 freq);
4298c2ecf20Sopenharmony_ci	max9867->sysclk = freq;
4308c2ecf20Sopenharmony_ci	value = value << MAX9867_PSCLK_SHIFT;
4318c2ecf20Sopenharmony_ci	/* exact integer mode is not supported */
4328c2ecf20Sopenharmony_ci	value &= ~MAX9867_FREQ_MASK;
4338c2ecf20Sopenharmony_ci	regmap_update_bits(max9867->regmap, MAX9867_SYSCLK,
4348c2ecf20Sopenharmony_ci			MAX9867_PSCLK_MASK, value);
4358c2ecf20Sopenharmony_ci	return 0;
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_cistatic int max9867_dai_set_fmt(struct snd_soc_dai *codec_dai,
4398c2ecf20Sopenharmony_ci		unsigned int fmt)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	struct snd_soc_component *component = codec_dai->component;
4428c2ecf20Sopenharmony_ci	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
4438c2ecf20Sopenharmony_ci	u8 iface1A, iface1B;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
4468c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
4478c2ecf20Sopenharmony_ci		max9867->master = true;
4488c2ecf20Sopenharmony_ci		iface1A = MAX9867_MASTER;
4498c2ecf20Sopenharmony_ci		iface1B = MAX9867_IFC1B_48X;
4508c2ecf20Sopenharmony_ci		break;
4518c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
4528c2ecf20Sopenharmony_ci		max9867->master = false;
4538c2ecf20Sopenharmony_ci		iface1A = iface1B = 0;
4548c2ecf20Sopenharmony_ci		break;
4558c2ecf20Sopenharmony_ci	default:
4568c2ecf20Sopenharmony_ci		return -EINVAL;
4578c2ecf20Sopenharmony_ci	}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
4608c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
4618c2ecf20Sopenharmony_ci		max9867->dsp_a = false;
4628c2ecf20Sopenharmony_ci		iface1A |= MAX9867_I2S_DLY;
4638c2ecf20Sopenharmony_ci		break;
4648c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_A:
4658c2ecf20Sopenharmony_ci		max9867->dsp_a = true;
4668c2ecf20Sopenharmony_ci		iface1A |= MAX9867_TDM_MODE | MAX9867_SDOUT_HIZ;
4678c2ecf20Sopenharmony_ci		break;
4688c2ecf20Sopenharmony_ci	default:
4698c2ecf20Sopenharmony_ci		return -EINVAL;
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* Clock inversion bits, BCI and WCI */
4738c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
4748c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_NF:
4758c2ecf20Sopenharmony_ci		break;
4768c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_IF:
4778c2ecf20Sopenharmony_ci		iface1A |= MAX9867_WCI_MODE | MAX9867_BCI_MODE;
4788c2ecf20Sopenharmony_ci		break;
4798c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_NF:
4808c2ecf20Sopenharmony_ci		iface1A |= MAX9867_BCI_MODE;
4818c2ecf20Sopenharmony_ci		break;
4828c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_IF:
4838c2ecf20Sopenharmony_ci		iface1A |= MAX9867_WCI_MODE;
4848c2ecf20Sopenharmony_ci		break;
4858c2ecf20Sopenharmony_ci	default:
4868c2ecf20Sopenharmony_ci		return -EINVAL;
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	regmap_write(max9867->regmap, MAX9867_IFC1A, iface1A);
4908c2ecf20Sopenharmony_ci	regmap_update_bits(max9867->regmap, MAX9867_IFC1B,
4918c2ecf20Sopenharmony_ci			   MAX9867_IFC1B_BCLK_MASK, iface1B);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	return 0;
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops max9867_dai_ops = {
4978c2ecf20Sopenharmony_ci	.set_sysclk	= max9867_set_dai_sysclk,
4988c2ecf20Sopenharmony_ci	.set_fmt	= max9867_dai_set_fmt,
4998c2ecf20Sopenharmony_ci	.mute_stream	= max9867_mute,
5008c2ecf20Sopenharmony_ci	.startup	= max9867_startup,
5018c2ecf20Sopenharmony_ci	.hw_params	= max9867_dai_hw_params,
5028c2ecf20Sopenharmony_ci	.no_capture_mute = 1,
5038c2ecf20Sopenharmony_ci};
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver max9867_dai[] = {
5068c2ecf20Sopenharmony_ci	{
5078c2ecf20Sopenharmony_ci	.name = "max9867-aif1",
5088c2ecf20Sopenharmony_ci	.playback = {
5098c2ecf20Sopenharmony_ci		.stream_name = "HiFi Playback",
5108c2ecf20Sopenharmony_ci		.channels_min = 2,
5118c2ecf20Sopenharmony_ci		.channels_max = 2,
5128c2ecf20Sopenharmony_ci		.rates = SNDRV_PCM_RATE_8000_48000,
5138c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S16_LE,
5148c2ecf20Sopenharmony_ci	},
5158c2ecf20Sopenharmony_ci	.capture = {
5168c2ecf20Sopenharmony_ci		.stream_name = "HiFi Capture",
5178c2ecf20Sopenharmony_ci		.channels_min = 2,
5188c2ecf20Sopenharmony_ci		.channels_max = 2,
5198c2ecf20Sopenharmony_ci		.rates = SNDRV_PCM_RATE_8000_48000,
5208c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S16_LE,
5218c2ecf20Sopenharmony_ci	},
5228c2ecf20Sopenharmony_ci	.ops = &max9867_dai_ops,
5238c2ecf20Sopenharmony_ci	.symmetric_rates = 1,
5248c2ecf20Sopenharmony_ci	}
5258c2ecf20Sopenharmony_ci};
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
5288c2ecf20Sopenharmony_cistatic int max9867_suspend(struct snd_soc_component *component)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	return 0;
5338c2ecf20Sopenharmony_ci}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_cistatic int max9867_resume(struct snd_soc_component *component)
5368c2ecf20Sopenharmony_ci{
5378c2ecf20Sopenharmony_ci	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_STANDBY);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	return 0;
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci#else
5428c2ecf20Sopenharmony_ci#define max9867_suspend	NULL
5438c2ecf20Sopenharmony_ci#define max9867_resume	NULL
5448c2ecf20Sopenharmony_ci#endif
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_cistatic int max9867_set_bias_level(struct snd_soc_component *component,
5478c2ecf20Sopenharmony_ci				  enum snd_soc_bias_level level)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	int err;
5508c2ecf20Sopenharmony_ci	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	switch (level) {
5538c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_STANDBY:
5548c2ecf20Sopenharmony_ci		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
5558c2ecf20Sopenharmony_ci			err = regcache_sync(max9867->regmap);
5568c2ecf20Sopenharmony_ci			if (err)
5578c2ecf20Sopenharmony_ci				return err;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci			err = regmap_write(max9867->regmap,
5608c2ecf20Sopenharmony_ci					   MAX9867_PWRMAN, 0xff);
5618c2ecf20Sopenharmony_ci			if (err)
5628c2ecf20Sopenharmony_ci				return err;
5638c2ecf20Sopenharmony_ci		}
5648c2ecf20Sopenharmony_ci		break;
5658c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_OFF:
5668c2ecf20Sopenharmony_ci		err = regmap_write(max9867->regmap, MAX9867_PWRMAN, 0);
5678c2ecf20Sopenharmony_ci		if (err)
5688c2ecf20Sopenharmony_ci			return err;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci		regcache_mark_dirty(max9867->regmap);
5718c2ecf20Sopenharmony_ci		break;
5728c2ecf20Sopenharmony_ci	default:
5738c2ecf20Sopenharmony_ci		break;
5748c2ecf20Sopenharmony_ci	}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	return 0;
5778c2ecf20Sopenharmony_ci}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver max9867_component = {
5808c2ecf20Sopenharmony_ci	.controls		= max9867_snd_controls,
5818c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(max9867_snd_controls),
5828c2ecf20Sopenharmony_ci	.dapm_routes		= max9867_audio_map,
5838c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(max9867_audio_map),
5848c2ecf20Sopenharmony_ci	.dapm_widgets		= max9867_dapm_widgets,
5858c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(max9867_dapm_widgets),
5868c2ecf20Sopenharmony_ci	.suspend		= max9867_suspend,
5878c2ecf20Sopenharmony_ci	.resume			= max9867_resume,
5888c2ecf20Sopenharmony_ci	.set_bias_level		= max9867_set_bias_level,
5898c2ecf20Sopenharmony_ci	.idle_bias_on		= 1,
5908c2ecf20Sopenharmony_ci	.use_pmdown_time	= 1,
5918c2ecf20Sopenharmony_ci	.endianness		= 1,
5928c2ecf20Sopenharmony_ci	.non_legacy_dai_naming	= 1,
5938c2ecf20Sopenharmony_ci};
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_cistatic bool max9867_volatile_register(struct device *dev, unsigned int reg)
5968c2ecf20Sopenharmony_ci{
5978c2ecf20Sopenharmony_ci	switch (reg) {
5988c2ecf20Sopenharmony_ci	case MAX9867_STATUS:
5998c2ecf20Sopenharmony_ci	case MAX9867_JACKSTATUS:
6008c2ecf20Sopenharmony_ci	case MAX9867_AUXHIGH:
6018c2ecf20Sopenharmony_ci	case MAX9867_AUXLOW:
6028c2ecf20Sopenharmony_ci		return true;
6038c2ecf20Sopenharmony_ci	default:
6048c2ecf20Sopenharmony_ci		return false;
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistatic const struct regmap_config max9867_regmap = {
6098c2ecf20Sopenharmony_ci	.reg_bits	= 8,
6108c2ecf20Sopenharmony_ci	.val_bits	= 8,
6118c2ecf20Sopenharmony_ci	.max_register	= MAX9867_REVISION,
6128c2ecf20Sopenharmony_ci	.volatile_reg	= max9867_volatile_register,
6138c2ecf20Sopenharmony_ci	.cache_type	= REGCACHE_RBTREE,
6148c2ecf20Sopenharmony_ci};
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_cistatic int max9867_i2c_probe(struct i2c_client *i2c,
6178c2ecf20Sopenharmony_ci		const struct i2c_device_id *id)
6188c2ecf20Sopenharmony_ci{
6198c2ecf20Sopenharmony_ci	struct max9867_priv *max9867;
6208c2ecf20Sopenharmony_ci	int ret, reg;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	max9867 = devm_kzalloc(&i2c->dev, sizeof(*max9867), GFP_KERNEL);
6238c2ecf20Sopenharmony_ci	if (!max9867)
6248c2ecf20Sopenharmony_ci		return -ENOMEM;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	i2c_set_clientdata(i2c, max9867);
6278c2ecf20Sopenharmony_ci	max9867->regmap = devm_regmap_init_i2c(i2c, &max9867_regmap);
6288c2ecf20Sopenharmony_ci	if (IS_ERR(max9867->regmap)) {
6298c2ecf20Sopenharmony_ci		ret = PTR_ERR(max9867->regmap);
6308c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
6318c2ecf20Sopenharmony_ci		return ret;
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci	ret = regmap_read(max9867->regmap, MAX9867_REVISION, &reg);
6348c2ecf20Sopenharmony_ci	if (ret < 0) {
6358c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Failed to read: %d\n", ret);
6368c2ecf20Sopenharmony_ci		return ret;
6378c2ecf20Sopenharmony_ci	}
6388c2ecf20Sopenharmony_ci	dev_info(&i2c->dev, "device revision: %x\n", reg);
6398c2ecf20Sopenharmony_ci	ret = devm_snd_soc_register_component(&i2c->dev, &max9867_component,
6408c2ecf20Sopenharmony_ci			max9867_dai, ARRAY_SIZE(max9867_dai));
6418c2ecf20Sopenharmony_ci	if (ret < 0)
6428c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Failed to register component: %d\n", ret);
6438c2ecf20Sopenharmony_ci	return ret;
6448c2ecf20Sopenharmony_ci}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_cistatic const struct i2c_device_id max9867_i2c_id[] = {
6478c2ecf20Sopenharmony_ci	{ "max9867", 0 },
6488c2ecf20Sopenharmony_ci	{ }
6498c2ecf20Sopenharmony_ci};
6508c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max9867_i2c_id);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistatic const struct of_device_id max9867_of_match[] = {
6538c2ecf20Sopenharmony_ci	{ .compatible = "maxim,max9867", },
6548c2ecf20Sopenharmony_ci	{ }
6558c2ecf20Sopenharmony_ci};
6568c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, max9867_of_match);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_cistatic struct i2c_driver max9867_i2c_driver = {
6598c2ecf20Sopenharmony_ci	.driver = {
6608c2ecf20Sopenharmony_ci		.name = "max9867",
6618c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(max9867_of_match),
6628c2ecf20Sopenharmony_ci	},
6638c2ecf20Sopenharmony_ci	.probe  = max9867_i2c_probe,
6648c2ecf20Sopenharmony_ci	.id_table = max9867_i2c_id,
6658c2ecf20Sopenharmony_ci};
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_cimodule_i2c_driver(max9867_i2c_driver);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ladislav Michl <ladis@linux-mips.org>");
6708c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ASoC MAX9867 driver");
6718c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
672