18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// ak4642.c  --  AK4642/AK4643 ALSA Soc Audio driver
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright (C) 2009 Renesas Solutions Corp.
68c2ecf20Sopenharmony_ci// Kuninori Morimoto <morimoto.kuninori@renesas.com>
78c2ecf20Sopenharmony_ci//
88c2ecf20Sopenharmony_ci// Based on wm8731.c by Richard Purdie
98c2ecf20Sopenharmony_ci// Based on ak4535.c by Richard Purdie
108c2ecf20Sopenharmony_ci// Based on wm8753.c by Liam Girdwood
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/* ** CAUTION **
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * This is very simple driver.
158c2ecf20Sopenharmony_ci * It can use headphone output / stereo input only
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * AK4642 is tested.
188c2ecf20Sopenharmony_ci * AK4643 is tested.
198c2ecf20Sopenharmony_ci * AK4648 is tested.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/clk.h>
238c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
248c2ecf20Sopenharmony_ci#include <linux/delay.h>
258c2ecf20Sopenharmony_ci#include <linux/i2c.h>
268c2ecf20Sopenharmony_ci#include <linux/slab.h>
278c2ecf20Sopenharmony_ci#include <linux/of_device.h>
288c2ecf20Sopenharmony_ci#include <linux/module.h>
298c2ecf20Sopenharmony_ci#include <linux/regmap.h>
308c2ecf20Sopenharmony_ci#include <sound/soc.h>
318c2ecf20Sopenharmony_ci#include <sound/initval.h>
328c2ecf20Sopenharmony_ci#include <sound/tlv.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define PW_MGMT1	0x00
358c2ecf20Sopenharmony_ci#define PW_MGMT2	0x01
368c2ecf20Sopenharmony_ci#define SG_SL1		0x02
378c2ecf20Sopenharmony_ci#define SG_SL2		0x03
388c2ecf20Sopenharmony_ci#define MD_CTL1		0x04
398c2ecf20Sopenharmony_ci#define MD_CTL2		0x05
408c2ecf20Sopenharmony_ci#define TIMER		0x06
418c2ecf20Sopenharmony_ci#define ALC_CTL1	0x07
428c2ecf20Sopenharmony_ci#define ALC_CTL2	0x08
438c2ecf20Sopenharmony_ci#define L_IVC		0x09
448c2ecf20Sopenharmony_ci#define L_DVC		0x0a
458c2ecf20Sopenharmony_ci#define ALC_CTL3	0x0b
468c2ecf20Sopenharmony_ci#define R_IVC		0x0c
478c2ecf20Sopenharmony_ci#define R_DVC		0x0d
488c2ecf20Sopenharmony_ci#define MD_CTL3		0x0e
498c2ecf20Sopenharmony_ci#define MD_CTL4		0x0f
508c2ecf20Sopenharmony_ci#define PW_MGMT3	0x10
518c2ecf20Sopenharmony_ci#define DF_S		0x11
528c2ecf20Sopenharmony_ci#define FIL3_0		0x12
538c2ecf20Sopenharmony_ci#define FIL3_1		0x13
548c2ecf20Sopenharmony_ci#define FIL3_2		0x14
558c2ecf20Sopenharmony_ci#define FIL3_3		0x15
568c2ecf20Sopenharmony_ci#define EQ_0		0x16
578c2ecf20Sopenharmony_ci#define EQ_1		0x17
588c2ecf20Sopenharmony_ci#define EQ_2		0x18
598c2ecf20Sopenharmony_ci#define EQ_3		0x19
608c2ecf20Sopenharmony_ci#define EQ_4		0x1a
618c2ecf20Sopenharmony_ci#define EQ_5		0x1b
628c2ecf20Sopenharmony_ci#define FIL1_0		0x1c
638c2ecf20Sopenharmony_ci#define FIL1_1		0x1d
648c2ecf20Sopenharmony_ci#define FIL1_2		0x1e
658c2ecf20Sopenharmony_ci#define FIL1_3		0x1f	/* The maximum valid register for ak4642 */
668c2ecf20Sopenharmony_ci#define PW_MGMT4	0x20
678c2ecf20Sopenharmony_ci#define MD_CTL5		0x21
688c2ecf20Sopenharmony_ci#define LO_MS		0x22
698c2ecf20Sopenharmony_ci#define HP_MS		0x23
708c2ecf20Sopenharmony_ci#define SPK_MS		0x24	/* The maximum valid register for ak4643 */
718c2ecf20Sopenharmony_ci#define EQ_FBEQAB	0x25
728c2ecf20Sopenharmony_ci#define EQ_FBEQCD	0x26
738c2ecf20Sopenharmony_ci#define EQ_FBEQE	0x27	/* The maximum valid register for ak4648 */
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* PW_MGMT1*/
768c2ecf20Sopenharmony_ci#define PMVCM		(1 << 6) /* VCOM Power Management */
778c2ecf20Sopenharmony_ci#define PMMIN		(1 << 5) /* MIN Input Power Management */
788c2ecf20Sopenharmony_ci#define PMDAC		(1 << 2) /* DAC Power Management */
798c2ecf20Sopenharmony_ci#define PMADL		(1 << 0) /* MIC Amp Lch and ADC Lch Power Management */
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/* PW_MGMT2 */
828c2ecf20Sopenharmony_ci#define HPMTN		(1 << 6)
838c2ecf20Sopenharmony_ci#define PMHPL		(1 << 5)
848c2ecf20Sopenharmony_ci#define PMHPR		(1 << 4)
858c2ecf20Sopenharmony_ci#define MS		(1 << 3) /* master/slave select */
868c2ecf20Sopenharmony_ci#define MCKO		(1 << 1)
878c2ecf20Sopenharmony_ci#define PMPLL		(1 << 0)
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define PMHP_MASK	(PMHPL | PMHPR)
908c2ecf20Sopenharmony_ci#define PMHP		PMHP_MASK
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/* PW_MGMT3 */
938c2ecf20Sopenharmony_ci#define PMADR		(1 << 0) /* MIC L / ADC R Power Management */
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/* SG_SL1 */
968c2ecf20Sopenharmony_ci#define MINS		(1 << 6) /* Switch from MIN to Speaker */
978c2ecf20Sopenharmony_ci#define DACL		(1 << 4) /* Switch from DAC to Stereo or Receiver */
988c2ecf20Sopenharmony_ci#define PMMP		(1 << 2) /* MPWR pin Power Management */
998c2ecf20Sopenharmony_ci#define MGAIN0		(1 << 0) /* MIC amp gain*/
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* SG_SL2 */
1028c2ecf20Sopenharmony_ci#define LOPS		(1 << 6) /* Stero Line-out Power Save Mode */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/* TIMER */
1058c2ecf20Sopenharmony_ci#define ZTM(param)	((param & 0x3) << 4) /* ALC Zero Crossing TimeOut */
1068c2ecf20Sopenharmony_ci#define WTM(param)	(((param & 0x4) << 4) | ((param & 0x3) << 2))
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* ALC_CTL1 */
1098c2ecf20Sopenharmony_ci#define ALC		(1 << 5) /* ALC Enable */
1108c2ecf20Sopenharmony_ci#define LMTH0		(1 << 0) /* ALC Limiter / Recovery Level */
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* MD_CTL1 */
1138c2ecf20Sopenharmony_ci#define PLL3		(1 << 7)
1148c2ecf20Sopenharmony_ci#define PLL2		(1 << 6)
1158c2ecf20Sopenharmony_ci#define PLL1		(1 << 5)
1168c2ecf20Sopenharmony_ci#define PLL0		(1 << 4)
1178c2ecf20Sopenharmony_ci#define PLL_MASK	(PLL3 | PLL2 | PLL1 | PLL0)
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci#define BCKO_MASK	(1 << 3)
1208c2ecf20Sopenharmony_ci#define BCKO_64		BCKO_MASK
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define DIF_MASK	(3 << 0)
1238c2ecf20Sopenharmony_ci#define DSP		(0 << 0)
1248c2ecf20Sopenharmony_ci#define RIGHT_J		(1 << 0)
1258c2ecf20Sopenharmony_ci#define LEFT_J		(2 << 0)
1268c2ecf20Sopenharmony_ci#define I2S		(3 << 0)
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/* MD_CTL2 */
1298c2ecf20Sopenharmony_ci#define FSs(val)	(((val & 0x7) << 0) | ((val & 0x8) << 2))
1308c2ecf20Sopenharmony_ci#define PSs(val)	((val & 0x3) << 6)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* MD_CTL3 */
1338c2ecf20Sopenharmony_ci#define BST1		(1 << 3)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* MD_CTL4 */
1368c2ecf20Sopenharmony_ci#define DACH		(1 << 0)
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistruct ak4642_drvdata {
1398c2ecf20Sopenharmony_ci	const struct regmap_config *regmap_config;
1408c2ecf20Sopenharmony_ci	int extended_frequencies;
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistruct ak4642_priv {
1448c2ecf20Sopenharmony_ci	const struct ak4642_drvdata *drvdata;
1458c2ecf20Sopenharmony_ci	struct clk *mcko;
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/*
1498c2ecf20Sopenharmony_ci * Playback Volume (table 39)
1508c2ecf20Sopenharmony_ci *
1518c2ecf20Sopenharmony_ci * max : 0x00 : +12.0 dB
1528c2ecf20Sopenharmony_ci *       ( 0.5 dB step )
1538c2ecf20Sopenharmony_ci * min : 0xFE : -115.0 dB
1548c2ecf20Sopenharmony_ci * mute: 0xFF
1558c2ecf20Sopenharmony_ci */
1568c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(out_tlv, -11550, 50, 1);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new ak4642_snd_controls[] = {
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC,
1618c2ecf20Sopenharmony_ci			 0, 0xFF, 1, out_tlv),
1628c2ecf20Sopenharmony_ci	SOC_SINGLE("ALC Capture Switch", ALC_CTL1, 5, 1, 0),
1638c2ecf20Sopenharmony_ci	SOC_SINGLE("ALC Capture ZC Switch", ALC_CTL1, 4, 1, 1),
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new ak4642_headphone_control =
1678c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("Switch", PW_MGMT2, 6, 1, 0);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new ak4642_lout_mixer_controls[] = {
1708c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DACL", SG_SL1, 4, 1, 0),
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/* event handlers */
1748c2ecf20Sopenharmony_cistatic int ak4642_lout_event(struct snd_soc_dapm_widget *w,
1758c2ecf20Sopenharmony_ci			     struct snd_kcontrol *kcontrol, int event)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	switch (event) {
1808c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_PRE_PMD:
1818c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_PRE_PMU:
1828c2ecf20Sopenharmony_ci		/* Power save mode ON */
1838c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, SG_SL2, LOPS, LOPS);
1848c2ecf20Sopenharmony_ci		break;
1858c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_POST_PMU:
1868c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_POST_PMD:
1878c2ecf20Sopenharmony_ci		/* Power save mode OFF */
1888c2ecf20Sopenharmony_ci		msleep(300);
1898c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, SG_SL2, LOPS, 0);
1908c2ecf20Sopenharmony_ci		break;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return 0;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget ak4642_dapm_widgets[] = {
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/* Outputs */
1998c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("HPOUTL"),
2008c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("HPOUTR"),
2018c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("LINEOUT"),
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("HPL Out", PW_MGMT2, 5, 0, NULL, 0),
2048c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("HPR Out", PW_MGMT2, 4, 0, NULL, 0),
2058c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SWITCH("Headphone Enable", SND_SOC_NOPM, 0, 0,
2068c2ecf20Sopenharmony_ci			    &ak4642_headphone_control),
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("DACH", MD_CTL4, 0, 0, NULL, 0),
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER_E("LINEOUT Mixer", PW_MGMT1, 3, 0,
2118c2ecf20Sopenharmony_ci			   &ak4642_lout_mixer_controls[0],
2128c2ecf20Sopenharmony_ci			   ARRAY_SIZE(ak4642_lout_mixer_controls),
2138c2ecf20Sopenharmony_ci			   ak4642_lout_event,
2148c2ecf20Sopenharmony_ci			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
2158c2ecf20Sopenharmony_ci			   SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* DAC */
2188c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC", NULL, PW_MGMT1, 2, 0),
2198c2ecf20Sopenharmony_ci};
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route ak4642_intercon[] = {
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	/* Outputs */
2248c2ecf20Sopenharmony_ci	{"HPOUTL", NULL, "HPL Out"},
2258c2ecf20Sopenharmony_ci	{"HPOUTR", NULL, "HPR Out"},
2268c2ecf20Sopenharmony_ci	{"LINEOUT", NULL, "LINEOUT Mixer"},
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	{"HPL Out", NULL, "Headphone Enable"},
2298c2ecf20Sopenharmony_ci	{"HPR Out", NULL, "Headphone Enable"},
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	{"Headphone Enable", "Switch", "DACH"},
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	{"DACH", NULL, "DAC"},
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	{"LINEOUT Mixer", "DACL", "DAC"},
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	{ "DAC", NULL, "Playback" },
2388c2ecf20Sopenharmony_ci};
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci/*
2418c2ecf20Sopenharmony_ci * ak4642 register cache
2428c2ecf20Sopenharmony_ci */
2438c2ecf20Sopenharmony_cistatic const struct reg_default ak4643_reg[] = {
2448c2ecf20Sopenharmony_ci	{  0, 0x00 }, {  1, 0x00 }, {  2, 0x01 }, {  3, 0x00 },
2458c2ecf20Sopenharmony_ci	{  4, 0x02 }, {  5, 0x00 }, {  6, 0x00 }, {  7, 0x00 },
2468c2ecf20Sopenharmony_ci	{  8, 0xe1 }, {  9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
2478c2ecf20Sopenharmony_ci	{ 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0x08 },
2488c2ecf20Sopenharmony_ci	{ 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
2498c2ecf20Sopenharmony_ci	{ 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
2508c2ecf20Sopenharmony_ci	{ 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
2518c2ecf20Sopenharmony_ci	{ 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
2528c2ecf20Sopenharmony_ci	{ 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
2538c2ecf20Sopenharmony_ci	{ 36, 0x00 },
2548c2ecf20Sopenharmony_ci};
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/* The default settings for 0x0 ~ 0x1f registers are the same for ak4642
2578c2ecf20Sopenharmony_ci   and ak4643. So we reuse the ak4643 reg_default for ak4642.
2588c2ecf20Sopenharmony_ci   The valid registers for ak4642 are 0x0 ~ 0x1f which is a subset of ak4643,
2598c2ecf20Sopenharmony_ci   so define NUM_AK4642_REG_DEFAULTS for ak4642.
2608c2ecf20Sopenharmony_ci*/
2618c2ecf20Sopenharmony_ci#define ak4642_reg ak4643_reg
2628c2ecf20Sopenharmony_ci#define NUM_AK4642_REG_DEFAULTS	(FIL1_3 + 1)
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic const struct reg_default ak4648_reg[] = {
2658c2ecf20Sopenharmony_ci	{  0, 0x00 }, {  1, 0x00 }, {  2, 0x01 }, {  3, 0x00 },
2668c2ecf20Sopenharmony_ci	{  4, 0x02 }, {  5, 0x00 }, {  6, 0x00 }, {  7, 0x00 },
2678c2ecf20Sopenharmony_ci	{  8, 0xe1 }, {  9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
2688c2ecf20Sopenharmony_ci	{ 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0xb8 },
2698c2ecf20Sopenharmony_ci	{ 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
2708c2ecf20Sopenharmony_ci	{ 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
2718c2ecf20Sopenharmony_ci	{ 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
2728c2ecf20Sopenharmony_ci	{ 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
2738c2ecf20Sopenharmony_ci	{ 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
2748c2ecf20Sopenharmony_ci	{ 36, 0x00 }, { 37, 0x88 }, { 38, 0x88 }, { 39, 0x08 },
2758c2ecf20Sopenharmony_ci};
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic int ak4642_dai_startup(struct snd_pcm_substream *substream,
2788c2ecf20Sopenharmony_ci			      struct snd_soc_dai *dai)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
2818c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	if (is_play) {
2848c2ecf20Sopenharmony_ci		/*
2858c2ecf20Sopenharmony_ci		 * start headphone output
2868c2ecf20Sopenharmony_ci		 *
2878c2ecf20Sopenharmony_ci		 * PLL, Master Mode
2888c2ecf20Sopenharmony_ci		 * Audio I/F Format :MSB justified (ADC & DAC)
2898c2ecf20Sopenharmony_ci		 * Bass Boost Level : Middle
2908c2ecf20Sopenharmony_ci		 *
2918c2ecf20Sopenharmony_ci		 * This operation came from example code of
2928c2ecf20Sopenharmony_ci		 * "ASAHI KASEI AK4642" (japanese) manual p97.
2938c2ecf20Sopenharmony_ci		 */
2948c2ecf20Sopenharmony_ci		snd_soc_component_write(component, L_IVC, 0x91); /* volume */
2958c2ecf20Sopenharmony_ci		snd_soc_component_write(component, R_IVC, 0x91); /* volume */
2968c2ecf20Sopenharmony_ci	} else {
2978c2ecf20Sopenharmony_ci		/*
2988c2ecf20Sopenharmony_ci		 * start stereo input
2998c2ecf20Sopenharmony_ci		 *
3008c2ecf20Sopenharmony_ci		 * PLL Master Mode
3018c2ecf20Sopenharmony_ci		 * Audio I/F Format:MSB justified (ADC & DAC)
3028c2ecf20Sopenharmony_ci		 * Pre MIC AMP:+20dB
3038c2ecf20Sopenharmony_ci		 * MIC Power On
3048c2ecf20Sopenharmony_ci		 * ALC setting:Refer to Table 35
3058c2ecf20Sopenharmony_ci		 * ALC bit=“1”
3068c2ecf20Sopenharmony_ci		 *
3078c2ecf20Sopenharmony_ci		 * This operation came from example code of
3088c2ecf20Sopenharmony_ci		 * "ASAHI KASEI AK4642" (japanese) manual p94.
3098c2ecf20Sopenharmony_ci		 */
3108c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, SG_SL1, PMMP | MGAIN0, PMMP | MGAIN0);
3118c2ecf20Sopenharmony_ci		snd_soc_component_write(component, TIMER, ZTM(0x3) | WTM(0x3));
3128c2ecf20Sopenharmony_ci		snd_soc_component_write(component, ALC_CTL1, ALC | LMTH0);
3138c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, PW_MGMT1, PMADL, PMADL);
3148c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, PW_MGMT3, PMADR, PMADR);
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	return 0;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
3218c2ecf20Sopenharmony_ci			       struct snd_soc_dai *dai)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
3248c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	if (is_play) {
3278c2ecf20Sopenharmony_ci	} else {
3288c2ecf20Sopenharmony_ci		/* stop stereo input */
3298c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, PW_MGMT1, PMADL, 0);
3308c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, PW_MGMT3, PMADR, 0);
3318c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, ALC_CTL1, ALC, 0);
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
3368c2ecf20Sopenharmony_ci	int clk_id, unsigned int freq, int dir)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	struct snd_soc_component *component = codec_dai->component;
3398c2ecf20Sopenharmony_ci	struct ak4642_priv *priv = snd_soc_component_get_drvdata(component);
3408c2ecf20Sopenharmony_ci	u8 pll;
3418c2ecf20Sopenharmony_ci	int extended_freq = 0;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	switch (freq) {
3448c2ecf20Sopenharmony_ci	case 11289600:
3458c2ecf20Sopenharmony_ci		pll = PLL2;
3468c2ecf20Sopenharmony_ci		break;
3478c2ecf20Sopenharmony_ci	case 12288000:
3488c2ecf20Sopenharmony_ci		pll = PLL2 | PLL0;
3498c2ecf20Sopenharmony_ci		break;
3508c2ecf20Sopenharmony_ci	case 12000000:
3518c2ecf20Sopenharmony_ci		pll = PLL2 | PLL1;
3528c2ecf20Sopenharmony_ci		break;
3538c2ecf20Sopenharmony_ci	case 24000000:
3548c2ecf20Sopenharmony_ci		pll = PLL2 | PLL1 | PLL0;
3558c2ecf20Sopenharmony_ci		break;
3568c2ecf20Sopenharmony_ci	case 13500000:
3578c2ecf20Sopenharmony_ci		pll = PLL3 | PLL2;
3588c2ecf20Sopenharmony_ci		break;
3598c2ecf20Sopenharmony_ci	case 27000000:
3608c2ecf20Sopenharmony_ci		pll = PLL3 | PLL2 | PLL0;
3618c2ecf20Sopenharmony_ci		break;
3628c2ecf20Sopenharmony_ci	case 19200000:
3638c2ecf20Sopenharmony_ci		pll = PLL3;
3648c2ecf20Sopenharmony_ci		extended_freq = 1;
3658c2ecf20Sopenharmony_ci		break;
3668c2ecf20Sopenharmony_ci	case 13000000:
3678c2ecf20Sopenharmony_ci		pll = PLL3 | PLL2 | PLL1;
3688c2ecf20Sopenharmony_ci		extended_freq = 1;
3698c2ecf20Sopenharmony_ci		break;
3708c2ecf20Sopenharmony_ci	case 26000000:
3718c2ecf20Sopenharmony_ci		pll = PLL3 | PLL2 | PLL1 | PLL0;
3728c2ecf20Sopenharmony_ci		extended_freq = 1;
3738c2ecf20Sopenharmony_ci		break;
3748c2ecf20Sopenharmony_ci	default:
3758c2ecf20Sopenharmony_ci		return -EINVAL;
3768c2ecf20Sopenharmony_ci	}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	if (extended_freq && !priv->drvdata->extended_frequencies)
3798c2ecf20Sopenharmony_ci		return -EINVAL;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, MD_CTL1, PLL_MASK, pll);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	return 0;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
3898c2ecf20Sopenharmony_ci	u8 data;
3908c2ecf20Sopenharmony_ci	u8 bcko;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	data = MCKO | PMPLL; /* use MCKO */
3938c2ecf20Sopenharmony_ci	bcko = 0;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	/* set master/slave audio interface */
3968c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
3978c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
3988c2ecf20Sopenharmony_ci		data |= MS;
3998c2ecf20Sopenharmony_ci		bcko = BCKO_64;
4008c2ecf20Sopenharmony_ci		break;
4018c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
4028c2ecf20Sopenharmony_ci		break;
4038c2ecf20Sopenharmony_ci	default:
4048c2ecf20Sopenharmony_ci		return -EINVAL;
4058c2ecf20Sopenharmony_ci	}
4068c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, PW_MGMT2, MS | MCKO | PMPLL, data);
4078c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, MD_CTL1, BCKO_MASK, bcko);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	/* format type */
4108c2ecf20Sopenharmony_ci	data = 0;
4118c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
4128c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LEFT_J:
4138c2ecf20Sopenharmony_ci		data = LEFT_J;
4148c2ecf20Sopenharmony_ci		break;
4158c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
4168c2ecf20Sopenharmony_ci		data = I2S;
4178c2ecf20Sopenharmony_ci		break;
4188c2ecf20Sopenharmony_ci	/* FIXME
4198c2ecf20Sopenharmony_ci	 * Please add RIGHT_J / DSP support here
4208c2ecf20Sopenharmony_ci	 */
4218c2ecf20Sopenharmony_ci	default:
4228c2ecf20Sopenharmony_ci		return -EINVAL;
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, MD_CTL1, DIF_MASK, data);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	return 0;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic int ak4642_set_mcko(struct snd_soc_component *component,
4308c2ecf20Sopenharmony_ci			   u32 frequency)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	static const u32 fs_list[] = {
4338c2ecf20Sopenharmony_ci		[0] = 8000,
4348c2ecf20Sopenharmony_ci		[1] = 12000,
4358c2ecf20Sopenharmony_ci		[2] = 16000,
4368c2ecf20Sopenharmony_ci		[3] = 24000,
4378c2ecf20Sopenharmony_ci		[4] = 7350,
4388c2ecf20Sopenharmony_ci		[5] = 11025,
4398c2ecf20Sopenharmony_ci		[6] = 14700,
4408c2ecf20Sopenharmony_ci		[7] = 22050,
4418c2ecf20Sopenharmony_ci		[10] = 32000,
4428c2ecf20Sopenharmony_ci		[11] = 48000,
4438c2ecf20Sopenharmony_ci		[14] = 29400,
4448c2ecf20Sopenharmony_ci		[15] = 44100,
4458c2ecf20Sopenharmony_ci	};
4468c2ecf20Sopenharmony_ci	static const u32 ps_list[] = {
4478c2ecf20Sopenharmony_ci		[0] = 256,
4488c2ecf20Sopenharmony_ci		[1] = 128,
4498c2ecf20Sopenharmony_ci		[2] = 64,
4508c2ecf20Sopenharmony_ci		[3] = 32
4518c2ecf20Sopenharmony_ci	};
4528c2ecf20Sopenharmony_ci	int ps, fs;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	for (ps = 0; ps < ARRAY_SIZE(ps_list); ps++) {
4558c2ecf20Sopenharmony_ci		for (fs = 0; fs < ARRAY_SIZE(fs_list); fs++) {
4568c2ecf20Sopenharmony_ci			if (frequency == ps_list[ps] * fs_list[fs]) {
4578c2ecf20Sopenharmony_ci				snd_soc_component_write(component, MD_CTL2,
4588c2ecf20Sopenharmony_ci					      PSs(ps) | FSs(fs));
4598c2ecf20Sopenharmony_ci				return 0;
4608c2ecf20Sopenharmony_ci			}
4618c2ecf20Sopenharmony_ci		}
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	return 0;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
4688c2ecf20Sopenharmony_ci				struct snd_pcm_hw_params *params,
4698c2ecf20Sopenharmony_ci				struct snd_soc_dai *dai)
4708c2ecf20Sopenharmony_ci{
4718c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
4728c2ecf20Sopenharmony_ci	struct ak4642_priv *priv = snd_soc_component_get_drvdata(component);
4738c2ecf20Sopenharmony_ci	u32 rate = clk_get_rate(priv->mcko);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	if (!rate)
4768c2ecf20Sopenharmony_ci		rate = params_rate(params) * 256;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	return ak4642_set_mcko(component, rate);
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic int ak4642_set_bias_level(struct snd_soc_component *component,
4828c2ecf20Sopenharmony_ci				 enum snd_soc_bias_level level)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	switch (level) {
4858c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_OFF:
4868c2ecf20Sopenharmony_ci		snd_soc_component_write(component, PW_MGMT1, 0x00);
4878c2ecf20Sopenharmony_ci		break;
4888c2ecf20Sopenharmony_ci	default:
4898c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, PW_MGMT1, PMVCM, PMVCM);
4908c2ecf20Sopenharmony_ci		break;
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	return 0;
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops ak4642_dai_ops = {
4978c2ecf20Sopenharmony_ci	.startup	= ak4642_dai_startup,
4988c2ecf20Sopenharmony_ci	.shutdown	= ak4642_dai_shutdown,
4998c2ecf20Sopenharmony_ci	.set_sysclk	= ak4642_dai_set_sysclk,
5008c2ecf20Sopenharmony_ci	.set_fmt	= ak4642_dai_set_fmt,
5018c2ecf20Sopenharmony_ci	.hw_params	= ak4642_dai_hw_params,
5028c2ecf20Sopenharmony_ci};
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver ak4642_dai = {
5058c2ecf20Sopenharmony_ci	.name = "ak4642-hifi",
5068c2ecf20Sopenharmony_ci	.playback = {
5078c2ecf20Sopenharmony_ci		.stream_name = "Playback",
5088c2ecf20Sopenharmony_ci		.channels_min = 2,
5098c2ecf20Sopenharmony_ci		.channels_max = 2,
5108c2ecf20Sopenharmony_ci		.rates = SNDRV_PCM_RATE_8000_48000,
5118c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S16_LE },
5128c2ecf20Sopenharmony_ci	.capture = {
5138c2ecf20Sopenharmony_ci		.stream_name = "Capture",
5148c2ecf20Sopenharmony_ci		.channels_min = 2,
5158c2ecf20Sopenharmony_ci		.channels_max = 2,
5168c2ecf20Sopenharmony_ci		.rates = SNDRV_PCM_RATE_8000_48000,
5178c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S16_LE },
5188c2ecf20Sopenharmony_ci	.ops = &ak4642_dai_ops,
5198c2ecf20Sopenharmony_ci	.symmetric_rates = 1,
5208c2ecf20Sopenharmony_ci};
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic int ak4642_suspend(struct snd_soc_component *component)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	struct regmap *regmap = dev_get_regmap(component->dev, NULL);
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	regcache_cache_only(regmap, true);
5278c2ecf20Sopenharmony_ci	regcache_mark_dirty(regmap);
5288c2ecf20Sopenharmony_ci	return 0;
5298c2ecf20Sopenharmony_ci}
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_cistatic int ak4642_resume(struct snd_soc_component *component)
5328c2ecf20Sopenharmony_ci{
5338c2ecf20Sopenharmony_ci	struct regmap *regmap = dev_get_regmap(component->dev, NULL);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	regcache_cache_only(regmap, false);
5368c2ecf20Sopenharmony_ci	regcache_sync(regmap);
5378c2ecf20Sopenharmony_ci	return 0;
5388c2ecf20Sopenharmony_ci}
5398c2ecf20Sopenharmony_cistatic int ak4642_probe(struct snd_soc_component *component)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	struct ak4642_priv *priv = snd_soc_component_get_drvdata(component);
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	if (priv->mcko)
5448c2ecf20Sopenharmony_ci		ak4642_set_mcko(component, clk_get_rate(priv->mcko));
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	return 0;
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver soc_component_dev_ak4642 = {
5508c2ecf20Sopenharmony_ci	.probe			= ak4642_probe,
5518c2ecf20Sopenharmony_ci	.suspend		= ak4642_suspend,
5528c2ecf20Sopenharmony_ci	.resume			= ak4642_resume,
5538c2ecf20Sopenharmony_ci	.set_bias_level		= ak4642_set_bias_level,
5548c2ecf20Sopenharmony_ci	.controls		= ak4642_snd_controls,
5558c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(ak4642_snd_controls),
5568c2ecf20Sopenharmony_ci	.dapm_widgets		= ak4642_dapm_widgets,
5578c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(ak4642_dapm_widgets),
5588c2ecf20Sopenharmony_ci	.dapm_routes		= ak4642_intercon,
5598c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(ak4642_intercon),
5608c2ecf20Sopenharmony_ci	.idle_bias_on		= 1,
5618c2ecf20Sopenharmony_ci	.endianness		= 1,
5628c2ecf20Sopenharmony_ci	.non_legacy_dai_naming	= 1,
5638c2ecf20Sopenharmony_ci};
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic const struct regmap_config ak4642_regmap = {
5668c2ecf20Sopenharmony_ci	.reg_bits		= 8,
5678c2ecf20Sopenharmony_ci	.val_bits		= 8,
5688c2ecf20Sopenharmony_ci	.max_register		= FIL1_3,
5698c2ecf20Sopenharmony_ci	.reg_defaults		= ak4642_reg,
5708c2ecf20Sopenharmony_ci	.num_reg_defaults	= NUM_AK4642_REG_DEFAULTS,
5718c2ecf20Sopenharmony_ci	.cache_type		= REGCACHE_RBTREE,
5728c2ecf20Sopenharmony_ci};
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic const struct regmap_config ak4643_regmap = {
5758c2ecf20Sopenharmony_ci	.reg_bits		= 8,
5768c2ecf20Sopenharmony_ci	.val_bits		= 8,
5778c2ecf20Sopenharmony_ci	.max_register		= SPK_MS,
5788c2ecf20Sopenharmony_ci	.reg_defaults		= ak4643_reg,
5798c2ecf20Sopenharmony_ci	.num_reg_defaults	= ARRAY_SIZE(ak4643_reg),
5808c2ecf20Sopenharmony_ci	.cache_type		= REGCACHE_RBTREE,
5818c2ecf20Sopenharmony_ci};
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_cistatic const struct regmap_config ak4648_regmap = {
5848c2ecf20Sopenharmony_ci	.reg_bits		= 8,
5858c2ecf20Sopenharmony_ci	.val_bits		= 8,
5868c2ecf20Sopenharmony_ci	.max_register		= EQ_FBEQE,
5878c2ecf20Sopenharmony_ci	.reg_defaults		= ak4648_reg,
5888c2ecf20Sopenharmony_ci	.num_reg_defaults	= ARRAY_SIZE(ak4648_reg),
5898c2ecf20Sopenharmony_ci	.cache_type		= REGCACHE_RBTREE,
5908c2ecf20Sopenharmony_ci};
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic const struct ak4642_drvdata ak4642_drvdata = {
5938c2ecf20Sopenharmony_ci	.regmap_config = &ak4642_regmap,
5948c2ecf20Sopenharmony_ci};
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_cistatic const struct ak4642_drvdata ak4643_drvdata = {
5978c2ecf20Sopenharmony_ci	.regmap_config = &ak4643_regmap,
5988c2ecf20Sopenharmony_ci};
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic const struct ak4642_drvdata ak4648_drvdata = {
6018c2ecf20Sopenharmony_ci	.regmap_config = &ak4648_regmap,
6028c2ecf20Sopenharmony_ci	.extended_frequencies = 1,
6038c2ecf20Sopenharmony_ci};
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci#ifdef CONFIG_COMMON_CLK
6068c2ecf20Sopenharmony_cistatic struct clk *ak4642_of_parse_mcko(struct device *dev)
6078c2ecf20Sopenharmony_ci{
6088c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
6098c2ecf20Sopenharmony_ci	struct clk *clk;
6108c2ecf20Sopenharmony_ci	const char *clk_name = np->name;
6118c2ecf20Sopenharmony_ci	const char *parent_clk_name = NULL;
6128c2ecf20Sopenharmony_ci	u32 rate;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "clock-frequency", &rate))
6158c2ecf20Sopenharmony_ci		return NULL;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "clocks"))
6188c2ecf20Sopenharmony_ci		parent_clk_name = of_clk_get_parent_name(np, 0);
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	of_property_read_string(np, "clock-output-names", &clk_name);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	clk = clk_register_fixed_rate(dev, clk_name, parent_clk_name, 0, rate);
6238c2ecf20Sopenharmony_ci	if (!IS_ERR(clk))
6248c2ecf20Sopenharmony_ci		of_clk_add_provider(np, of_clk_src_simple_get, clk);
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	return clk;
6278c2ecf20Sopenharmony_ci}
6288c2ecf20Sopenharmony_ci#else
6298c2ecf20Sopenharmony_ci#define ak4642_of_parse_mcko(d) 0
6308c2ecf20Sopenharmony_ci#endif
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cistatic const struct of_device_id ak4642_of_match[];
6338c2ecf20Sopenharmony_cistatic int ak4642_i2c_probe(struct i2c_client *i2c,
6348c2ecf20Sopenharmony_ci			    const struct i2c_device_id *id)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	struct device *dev = &i2c->dev;
6378c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
6388c2ecf20Sopenharmony_ci	const struct ak4642_drvdata *drvdata = NULL;
6398c2ecf20Sopenharmony_ci	struct regmap *regmap;
6408c2ecf20Sopenharmony_ci	struct ak4642_priv *priv;
6418c2ecf20Sopenharmony_ci	struct clk *mcko = NULL;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	if (np) {
6448c2ecf20Sopenharmony_ci		const struct of_device_id *of_id;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci		mcko = ak4642_of_parse_mcko(dev);
6478c2ecf20Sopenharmony_ci		if (IS_ERR(mcko))
6488c2ecf20Sopenharmony_ci			mcko = NULL;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci		of_id = of_match_device(ak4642_of_match, dev);
6518c2ecf20Sopenharmony_ci		if (of_id)
6528c2ecf20Sopenharmony_ci			drvdata = of_id->data;
6538c2ecf20Sopenharmony_ci	} else {
6548c2ecf20Sopenharmony_ci		drvdata = (const struct ak4642_drvdata *)id->driver_data;
6558c2ecf20Sopenharmony_ci	}
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	if (!drvdata) {
6588c2ecf20Sopenharmony_ci		dev_err(dev, "Unknown device type\n");
6598c2ecf20Sopenharmony_ci		return -EINVAL;
6608c2ecf20Sopenharmony_ci	}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
6638c2ecf20Sopenharmony_ci	if (!priv)
6648c2ecf20Sopenharmony_ci		return -ENOMEM;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	priv->drvdata = drvdata;
6678c2ecf20Sopenharmony_ci	priv->mcko = mcko;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	i2c_set_clientdata(i2c, priv);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_i2c(i2c, drvdata->regmap_config);
6728c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
6738c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	return devm_snd_soc_register_component(dev,
6768c2ecf20Sopenharmony_ci				&soc_component_dev_ak4642, &ak4642_dai, 1);
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic const struct of_device_id ak4642_of_match[] = {
6808c2ecf20Sopenharmony_ci	{ .compatible = "asahi-kasei,ak4642",	.data = &ak4642_drvdata},
6818c2ecf20Sopenharmony_ci	{ .compatible = "asahi-kasei,ak4643",	.data = &ak4643_drvdata},
6828c2ecf20Sopenharmony_ci	{ .compatible = "asahi-kasei,ak4648",	.data = &ak4648_drvdata},
6838c2ecf20Sopenharmony_ci	{},
6848c2ecf20Sopenharmony_ci};
6858c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ak4642_of_match);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_cistatic const struct i2c_device_id ak4642_i2c_id[] = {
6888c2ecf20Sopenharmony_ci	{ "ak4642", (kernel_ulong_t)&ak4642_drvdata },
6898c2ecf20Sopenharmony_ci	{ "ak4643", (kernel_ulong_t)&ak4643_drvdata },
6908c2ecf20Sopenharmony_ci	{ "ak4648", (kernel_ulong_t)&ak4648_drvdata },
6918c2ecf20Sopenharmony_ci	{ }
6928c2ecf20Sopenharmony_ci};
6938c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_cistatic struct i2c_driver ak4642_i2c_driver = {
6968c2ecf20Sopenharmony_ci	.driver = {
6978c2ecf20Sopenharmony_ci		.name = "ak4642-codec",
6988c2ecf20Sopenharmony_ci		.of_match_table = ak4642_of_match,
6998c2ecf20Sopenharmony_ci	},
7008c2ecf20Sopenharmony_ci	.probe		= ak4642_i2c_probe,
7018c2ecf20Sopenharmony_ci	.id_table	= ak4642_i2c_id,
7028c2ecf20Sopenharmony_ci};
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cimodule_i2c_driver(ak4642_i2c_driver);
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Soc AK4642 driver");
7078c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
7088c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
709