162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * cs42l83-i2c.c -- CS42L83 ALSA SoC audio driver for I2C
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Based on cs42l42-i2c.c:
662306a36Sopenharmony_ci *   Copyright 2016, 2022 Cirrus Logic, Inc.
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/i2c.h>
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include <linux/regmap.h>
1262306a36Sopenharmony_ci#include <linux/slab.h>
1362306a36Sopenharmony_ci#include <linux/types.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include "cs42l42.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistatic const struct reg_default cs42l83_reg_defaults[] = {
1862306a36Sopenharmony_ci	{ CS42L42_FRZ_CTL,			0x00 },
1962306a36Sopenharmony_ci	{ CS42L42_SRC_CTL,			0x10 },
2062306a36Sopenharmony_ci	{ CS42L42_MCLK_CTL,			0x00 }, /* <- only deviation from CS42L42 */
2162306a36Sopenharmony_ci	{ CS42L42_SFTRAMP_RATE,			0xA4 },
2262306a36Sopenharmony_ci	{ CS42L42_SLOW_START_ENABLE,		0x70 },
2362306a36Sopenharmony_ci	{ CS42L42_I2C_DEBOUNCE,			0x88 },
2462306a36Sopenharmony_ci	{ CS42L42_I2C_STRETCH,			0x03 },
2562306a36Sopenharmony_ci	{ CS42L42_I2C_TIMEOUT,			0xB7 },
2662306a36Sopenharmony_ci	{ CS42L42_PWR_CTL1,			0xFF },
2762306a36Sopenharmony_ci	{ CS42L42_PWR_CTL2,			0x84 },
2862306a36Sopenharmony_ci	{ CS42L42_PWR_CTL3,			0x20 },
2962306a36Sopenharmony_ci	{ CS42L42_RSENSE_CTL1,			0x40 },
3062306a36Sopenharmony_ci	{ CS42L42_RSENSE_CTL2,			0x00 },
3162306a36Sopenharmony_ci	{ CS42L42_OSC_SWITCH,			0x00 },
3262306a36Sopenharmony_ci	{ CS42L42_RSENSE_CTL3,			0x1B },
3362306a36Sopenharmony_ci	{ CS42L42_TSENSE_CTL,			0x1B },
3462306a36Sopenharmony_ci	{ CS42L42_TSRS_INT_DISABLE,		0x00 },
3562306a36Sopenharmony_ci	{ CS42L42_HSDET_CTL1,			0x77 },
3662306a36Sopenharmony_ci	{ CS42L42_HSDET_CTL2,			0x00 },
3762306a36Sopenharmony_ci	{ CS42L42_HS_SWITCH_CTL,		0xF3 },
3862306a36Sopenharmony_ci	{ CS42L42_HS_CLAMP_DISABLE,		0x00 },
3962306a36Sopenharmony_ci	{ CS42L42_MCLK_SRC_SEL,			0x00 },
4062306a36Sopenharmony_ci	{ CS42L42_SPDIF_CLK_CFG,		0x00 },
4162306a36Sopenharmony_ci	{ CS42L42_FSYNC_PW_LOWER,		0x00 },
4262306a36Sopenharmony_ci	{ CS42L42_FSYNC_PW_UPPER,		0x00 },
4362306a36Sopenharmony_ci	{ CS42L42_FSYNC_P_LOWER,		0xF9 },
4462306a36Sopenharmony_ci	{ CS42L42_FSYNC_P_UPPER,		0x00 },
4562306a36Sopenharmony_ci	{ CS42L42_ASP_CLK_CFG,			0x00 },
4662306a36Sopenharmony_ci	{ CS42L42_ASP_FRM_CFG,			0x10 },
4762306a36Sopenharmony_ci	{ CS42L42_FS_RATE_EN,			0x00 },
4862306a36Sopenharmony_ci	{ CS42L42_IN_ASRC_CLK,			0x00 },
4962306a36Sopenharmony_ci	{ CS42L42_OUT_ASRC_CLK,			0x00 },
5062306a36Sopenharmony_ci	{ CS42L42_PLL_DIV_CFG1,			0x00 },
5162306a36Sopenharmony_ci	{ CS42L42_ADC_OVFL_INT_MASK,		0x01 },
5262306a36Sopenharmony_ci	{ CS42L42_MIXER_INT_MASK,		0x0F },
5362306a36Sopenharmony_ci	{ CS42L42_SRC_INT_MASK,			0x0F },
5462306a36Sopenharmony_ci	{ CS42L42_ASP_RX_INT_MASK,		0x1F },
5562306a36Sopenharmony_ci	{ CS42L42_ASP_TX_INT_MASK,		0x0F },
5662306a36Sopenharmony_ci	{ CS42L42_CODEC_INT_MASK,		0x03 },
5762306a36Sopenharmony_ci	{ CS42L42_SRCPL_INT_MASK,		0x7F },
5862306a36Sopenharmony_ci	{ CS42L42_VPMON_INT_MASK,		0x01 },
5962306a36Sopenharmony_ci	{ CS42L42_PLL_LOCK_INT_MASK,		0x01 },
6062306a36Sopenharmony_ci	{ CS42L42_TSRS_PLUG_INT_MASK,		0x0F },
6162306a36Sopenharmony_ci	{ CS42L42_PLL_CTL1,			0x00 },
6262306a36Sopenharmony_ci	{ CS42L42_PLL_DIV_FRAC0,		0x00 },
6362306a36Sopenharmony_ci	{ CS42L42_PLL_DIV_FRAC1,		0x00 },
6462306a36Sopenharmony_ci	{ CS42L42_PLL_DIV_FRAC2,		0x00 },
6562306a36Sopenharmony_ci	{ CS42L42_PLL_DIV_INT,			0x40 },
6662306a36Sopenharmony_ci	{ CS42L42_PLL_CTL3,			0x10 },
6762306a36Sopenharmony_ci	{ CS42L42_PLL_CAL_RATIO,		0x80 },
6862306a36Sopenharmony_ci	{ CS42L42_PLL_CTL4,			0x03 },
6962306a36Sopenharmony_ci	{ CS42L42_LOAD_DET_EN,			0x00 },
7062306a36Sopenharmony_ci	{ CS42L42_HSBIAS_SC_AUTOCTL,		0x03 },
7162306a36Sopenharmony_ci	{ CS42L42_WAKE_CTL,			0xC0 },
7262306a36Sopenharmony_ci	{ CS42L42_ADC_DISABLE_MUTE,		0x00 },
7362306a36Sopenharmony_ci	{ CS42L42_TIPSENSE_CTL,			0x02 },
7462306a36Sopenharmony_ci	{ CS42L42_MISC_DET_CTL,			0x03 },
7562306a36Sopenharmony_ci	{ CS42L42_MIC_DET_CTL1,			0x1F },
7662306a36Sopenharmony_ci	{ CS42L42_MIC_DET_CTL2,			0x2F },
7762306a36Sopenharmony_ci	{ CS42L42_DET_INT1_MASK,		0xE0 },
7862306a36Sopenharmony_ci	{ CS42L42_DET_INT2_MASK,		0xFF },
7962306a36Sopenharmony_ci	{ CS42L42_HS_BIAS_CTL,			0xC2 },
8062306a36Sopenharmony_ci	{ CS42L42_ADC_CTL,			0x00 },
8162306a36Sopenharmony_ci	{ CS42L42_ADC_VOLUME,			0x00 },
8262306a36Sopenharmony_ci	{ CS42L42_ADC_WNF_HPF_CTL,		0x71 },
8362306a36Sopenharmony_ci	{ CS42L42_DAC_CTL1,			0x00 },
8462306a36Sopenharmony_ci	{ CS42L42_DAC_CTL2,			0x02 },
8562306a36Sopenharmony_ci	{ CS42L42_HP_CTL,			0x0D },
8662306a36Sopenharmony_ci	{ CS42L42_CLASSH_CTL,			0x07 },
8762306a36Sopenharmony_ci	{ CS42L42_MIXER_CHA_VOL,		0x3F },
8862306a36Sopenharmony_ci	{ CS42L42_MIXER_ADC_VOL,		0x3F },
8962306a36Sopenharmony_ci	{ CS42L42_MIXER_CHB_VOL,		0x3F },
9062306a36Sopenharmony_ci	{ CS42L42_EQ_COEF_IN0,			0x00 },
9162306a36Sopenharmony_ci	{ CS42L42_EQ_COEF_IN1,			0x00 },
9262306a36Sopenharmony_ci	{ CS42L42_EQ_COEF_IN2,			0x00 },
9362306a36Sopenharmony_ci	{ CS42L42_EQ_COEF_IN3,			0x00 },
9462306a36Sopenharmony_ci	{ CS42L42_EQ_COEF_RW,			0x00 },
9562306a36Sopenharmony_ci	{ CS42L42_EQ_COEF_OUT0,			0x00 },
9662306a36Sopenharmony_ci	{ CS42L42_EQ_COEF_OUT1,			0x00 },
9762306a36Sopenharmony_ci	{ CS42L42_EQ_COEF_OUT2,			0x00 },
9862306a36Sopenharmony_ci	{ CS42L42_EQ_COEF_OUT3,			0x00 },
9962306a36Sopenharmony_ci	{ CS42L42_EQ_INIT_STAT,			0x00 },
10062306a36Sopenharmony_ci	{ CS42L42_EQ_START_FILT,		0x00 },
10162306a36Sopenharmony_ci	{ CS42L42_EQ_MUTE_CTL,			0x00 },
10262306a36Sopenharmony_ci	{ CS42L42_SP_RX_CH_SEL,			0x04 },
10362306a36Sopenharmony_ci	{ CS42L42_SP_RX_ISOC_CTL,		0x04 },
10462306a36Sopenharmony_ci	{ CS42L42_SP_RX_FS,			0x8C },
10562306a36Sopenharmony_ci	{ CS42l42_SPDIF_CH_SEL,			0x0E },
10662306a36Sopenharmony_ci	{ CS42L42_SP_TX_ISOC_CTL,		0x04 },
10762306a36Sopenharmony_ci	{ CS42L42_SP_TX_FS,			0xCC },
10862306a36Sopenharmony_ci	{ CS42L42_SPDIF_SW_CTL1,		0x3F },
10962306a36Sopenharmony_ci	{ CS42L42_SRC_SDIN_FS,			0x40 },
11062306a36Sopenharmony_ci	{ CS42L42_SRC_SDOUT_FS,			0x40 },
11162306a36Sopenharmony_ci	{ CS42L42_SPDIF_CTL1,			0x01 },
11262306a36Sopenharmony_ci	{ CS42L42_SPDIF_CTL2,			0x00 },
11362306a36Sopenharmony_ci	{ CS42L42_SPDIF_CTL3,			0x00 },
11462306a36Sopenharmony_ci	{ CS42L42_SPDIF_CTL4,			0x42 },
11562306a36Sopenharmony_ci	{ CS42L42_ASP_TX_SZ_EN,			0x00 },
11662306a36Sopenharmony_ci	{ CS42L42_ASP_TX_CH_EN,			0x00 },
11762306a36Sopenharmony_ci	{ CS42L42_ASP_TX_CH_AP_RES,		0x0F },
11862306a36Sopenharmony_ci	{ CS42L42_ASP_TX_CH1_BIT_MSB,		0x00 },
11962306a36Sopenharmony_ci	{ CS42L42_ASP_TX_CH1_BIT_LSB,		0x00 },
12062306a36Sopenharmony_ci	{ CS42L42_ASP_TX_HIZ_DLY_CFG,		0x00 },
12162306a36Sopenharmony_ci	{ CS42L42_ASP_TX_CH2_BIT_MSB,		0x00 },
12262306a36Sopenharmony_ci	{ CS42L42_ASP_TX_CH2_BIT_LSB,		0x00 },
12362306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_EN,		0x00 },
12462306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH1_AP_RES,	0x03 },
12562306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH1_BIT_MSB,	0x00 },
12662306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH1_BIT_LSB,	0x00 },
12762306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH2_AP_RES,	0x03 },
12862306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH2_BIT_MSB,	0x00 },
12962306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH2_BIT_LSB,	0x00 },
13062306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH3_AP_RES,	0x03 },
13162306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH3_BIT_MSB,	0x00 },
13262306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH3_BIT_LSB,	0x00 },
13362306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH4_AP_RES,	0x03 },
13462306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH4_BIT_MSB,	0x00 },
13562306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI0_CH4_BIT_LSB,	0x00 },
13662306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI1_CH1_AP_RES,	0x03 },
13762306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI1_CH1_BIT_MSB,	0x00 },
13862306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI1_CH1_BIT_LSB,	0x00 },
13962306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI1_CH2_AP_RES,	0x03 },
14062306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI1_CH2_BIT_MSB,	0x00 },
14162306a36Sopenharmony_ci	{ CS42L42_ASP_RX_DAI1_CH2_BIT_LSB,	0x00 },
14262306a36Sopenharmony_ci};
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/*
14562306a36Sopenharmony_ci * This is all the same as for CS42L42 but we
14662306a36Sopenharmony_ci * replace the on-reset register defaults.
14762306a36Sopenharmony_ci */
14862306a36Sopenharmony_cistatic const struct regmap_config cs42l83_regmap = {
14962306a36Sopenharmony_ci	.reg_bits = 8,
15062306a36Sopenharmony_ci	.val_bits = 8,
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	.readable_reg = cs42l42_readable_register,
15362306a36Sopenharmony_ci	.volatile_reg = cs42l42_volatile_register,
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	.ranges = &cs42l42_page_range,
15662306a36Sopenharmony_ci	.num_ranges = 1,
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	.max_register = CS42L42_MAX_REGISTER,
15962306a36Sopenharmony_ci	.reg_defaults = cs42l83_reg_defaults,
16062306a36Sopenharmony_ci	.num_reg_defaults = ARRAY_SIZE(cs42l83_reg_defaults),
16162306a36Sopenharmony_ci	.cache_type = REGCACHE_MAPLE,
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	.use_single_read = true,
16462306a36Sopenharmony_ci	.use_single_write = true,
16562306a36Sopenharmony_ci};
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cistatic int cs42l83_i2c_probe(struct i2c_client *i2c_client)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	struct device *dev = &i2c_client->dev;
17062306a36Sopenharmony_ci	struct cs42l42_private *cs42l83;
17162306a36Sopenharmony_ci	struct regmap *regmap;
17262306a36Sopenharmony_ci	int ret;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	cs42l83 = devm_kzalloc(dev, sizeof(*cs42l83), GFP_KERNEL);
17562306a36Sopenharmony_ci	if (!cs42l83)
17662306a36Sopenharmony_ci		return -ENOMEM;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	regmap = devm_regmap_init_i2c(i2c_client, &cs42l83_regmap);
17962306a36Sopenharmony_ci	if (IS_ERR(regmap))
18062306a36Sopenharmony_ci		return dev_err_probe(&i2c_client->dev, PTR_ERR(regmap),
18162306a36Sopenharmony_ci				     "regmap_init() failed\n");
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	cs42l83->devid = CS42L83_CHIP_ID;
18462306a36Sopenharmony_ci	cs42l83->dev = dev;
18562306a36Sopenharmony_ci	cs42l83->regmap = regmap;
18662306a36Sopenharmony_ci	cs42l83->irq = i2c_client->irq;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	ret = cs42l42_common_probe(cs42l83, &cs42l42_soc_component, &cs42l42_dai);
18962306a36Sopenharmony_ci	if (ret)
19062306a36Sopenharmony_ci		return ret;
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	return cs42l42_init(cs42l83);
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic void cs42l83_i2c_remove(struct i2c_client *i2c_client)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	struct cs42l42_private *cs42l83 = dev_get_drvdata(&i2c_client->dev);
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	cs42l42_common_remove(cs42l83);
20062306a36Sopenharmony_ci}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_cistatic int __maybe_unused cs42l83_i2c_resume(struct device *dev)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	int ret;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	ret = cs42l42_resume(dev);
20762306a36Sopenharmony_ci	if (ret)
20862306a36Sopenharmony_ci		return ret;
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	cs42l42_resume_restore(dev);
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	return 0;
21362306a36Sopenharmony_ci}
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_cistatic const struct dev_pm_ops cs42l83_i2c_pm_ops = {
21662306a36Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(cs42l42_suspend, cs42l83_i2c_resume)
21762306a36Sopenharmony_ci};
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_cistatic const struct of_device_id __maybe_unused cs42l83_of_match[] = {
22062306a36Sopenharmony_ci	{ .compatible = "cirrus,cs42l83", },
22162306a36Sopenharmony_ci	{}
22262306a36Sopenharmony_ci};
22362306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, cs42l83_of_match);
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cistatic struct i2c_driver cs42l83_i2c_driver = {
22662306a36Sopenharmony_ci	.driver = {
22762306a36Sopenharmony_ci		.name = "cs42l83",
22862306a36Sopenharmony_ci		.pm = &cs42l83_i2c_pm_ops,
22962306a36Sopenharmony_ci		.of_match_table = of_match_ptr(cs42l83_of_match),
23062306a36Sopenharmony_ci		},
23162306a36Sopenharmony_ci	.probe = cs42l83_i2c_probe,
23262306a36Sopenharmony_ci	.remove = cs42l83_i2c_remove,
23362306a36Sopenharmony_ci};
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_cimodule_i2c_driver(cs42l83_i2c_driver);
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ciMODULE_DESCRIPTION("ASoC CS42L83 I2C driver");
23862306a36Sopenharmony_ciMODULE_AUTHOR("Martin Povišer <povik+lin@cutebit.org>");
23962306a36Sopenharmony_ciMODULE_LICENSE("GPL");
24062306a36Sopenharmony_ciMODULE_IMPORT_NS(SND_SOC_CS42L42_CORE);
241