1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun // Copyright (c) 2020, Maxim Integrated
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include <linux/acpi.h>
5*4882a593Smuzhiyun #include <linux/delay.h>
6*4882a593Smuzhiyun #include <linux/module.h>
7*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
8*4882a593Smuzhiyun #include <linux/pm_runtime.h>
9*4882a593Smuzhiyun #include <linux/regmap.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <sound/pcm.h>
12*4882a593Smuzhiyun #include <sound/pcm_params.h>
13*4882a593Smuzhiyun #include <sound/soc.h>
14*4882a593Smuzhiyun #include <sound/tlv.h>
15*4882a593Smuzhiyun #include <linux/of.h>
16*4882a593Smuzhiyun #include <linux/soundwire/sdw.h>
17*4882a593Smuzhiyun #include <linux/soundwire/sdw_type.h>
18*4882a593Smuzhiyun #include <linux/soundwire/sdw_registers.h>
19*4882a593Smuzhiyun #include "max98373.h"
20*4882a593Smuzhiyun #include "max98373-sdw.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun struct sdw_stream_data {
23*4882a593Smuzhiyun struct sdw_stream_runtime *sdw_stream;
24*4882a593Smuzhiyun };
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun static struct reg_default max98373_reg[] = {
27*4882a593Smuzhiyun {MAX98373_R0040_SCP_INIT_STAT_1, 0x00},
28*4882a593Smuzhiyun {MAX98373_R0041_SCP_INIT_MASK_1, 0x00},
29*4882a593Smuzhiyun {MAX98373_R0042_SCP_INIT_STAT_2, 0x00},
30*4882a593Smuzhiyun {MAX98373_R0044_SCP_CTRL, 0x00},
31*4882a593Smuzhiyun {MAX98373_R0045_SCP_SYSTEM_CTRL, 0x00},
32*4882a593Smuzhiyun {MAX98373_R0046_SCP_DEV_NUMBER, 0x00},
33*4882a593Smuzhiyun {MAX98373_R0050_SCP_DEV_ID_0, 0x21},
34*4882a593Smuzhiyun {MAX98373_R0051_SCP_DEV_ID_1, 0x01},
35*4882a593Smuzhiyun {MAX98373_R0052_SCP_DEV_ID_2, 0x9F},
36*4882a593Smuzhiyun {MAX98373_R0053_SCP_DEV_ID_3, 0x87},
37*4882a593Smuzhiyun {MAX98373_R0054_SCP_DEV_ID_4, 0x08},
38*4882a593Smuzhiyun {MAX98373_R0055_SCP_DEV_ID_5, 0x00},
39*4882a593Smuzhiyun {MAX98373_R0060_SCP_FRAME_CTLR, 0x00},
40*4882a593Smuzhiyun {MAX98373_R0070_SCP_FRAME_CTLR, 0x00},
41*4882a593Smuzhiyun {MAX98373_R0100_DP1_INIT_STAT, 0x00},
42*4882a593Smuzhiyun {MAX98373_R0101_DP1_INIT_MASK, 0x00},
43*4882a593Smuzhiyun {MAX98373_R0102_DP1_PORT_CTRL, 0x00},
44*4882a593Smuzhiyun {MAX98373_R0103_DP1_BLOCK_CTRL_1, 0x00},
45*4882a593Smuzhiyun {MAX98373_R0104_DP1_PREPARE_STATUS, 0x00},
46*4882a593Smuzhiyun {MAX98373_R0105_DP1_PREPARE_CTRL, 0x00},
47*4882a593Smuzhiyun {MAX98373_R0120_DP1_CHANNEL_EN, 0x00},
48*4882a593Smuzhiyun {MAX98373_R0122_DP1_SAMPLE_CTRL1, 0x00},
49*4882a593Smuzhiyun {MAX98373_R0123_DP1_SAMPLE_CTRL2, 0x00},
50*4882a593Smuzhiyun {MAX98373_R0124_DP1_OFFSET_CTRL1, 0x00},
51*4882a593Smuzhiyun {MAX98373_R0125_DP1_OFFSET_CTRL2, 0x00},
52*4882a593Smuzhiyun {MAX98373_R0126_DP1_HCTRL, 0x00},
53*4882a593Smuzhiyun {MAX98373_R0127_DP1_BLOCK_CTRL3, 0x00},
54*4882a593Smuzhiyun {MAX98373_R0130_DP1_CHANNEL_EN, 0x00},
55*4882a593Smuzhiyun {MAX98373_R0132_DP1_SAMPLE_CTRL1, 0x00},
56*4882a593Smuzhiyun {MAX98373_R0133_DP1_SAMPLE_CTRL2, 0x00},
57*4882a593Smuzhiyun {MAX98373_R0134_DP1_OFFSET_CTRL1, 0x00},
58*4882a593Smuzhiyun {MAX98373_R0135_DP1_OFFSET_CTRL2, 0x00},
59*4882a593Smuzhiyun {MAX98373_R0136_DP1_HCTRL, 0x0136},
60*4882a593Smuzhiyun {MAX98373_R0137_DP1_BLOCK_CTRL3, 0x00},
61*4882a593Smuzhiyun {MAX98373_R0300_DP3_INIT_STAT, 0x00},
62*4882a593Smuzhiyun {MAX98373_R0301_DP3_INIT_MASK, 0x00},
63*4882a593Smuzhiyun {MAX98373_R0302_DP3_PORT_CTRL, 0x00},
64*4882a593Smuzhiyun {MAX98373_R0303_DP3_BLOCK_CTRL_1, 0x00},
65*4882a593Smuzhiyun {MAX98373_R0304_DP3_PREPARE_STATUS, 0x00},
66*4882a593Smuzhiyun {MAX98373_R0305_DP3_PREPARE_CTRL, 0x00},
67*4882a593Smuzhiyun {MAX98373_R0320_DP3_CHANNEL_EN, 0x00},
68*4882a593Smuzhiyun {MAX98373_R0322_DP3_SAMPLE_CTRL1, 0x00},
69*4882a593Smuzhiyun {MAX98373_R0323_DP3_SAMPLE_CTRL2, 0x00},
70*4882a593Smuzhiyun {MAX98373_R0324_DP3_OFFSET_CTRL1, 0x00},
71*4882a593Smuzhiyun {MAX98373_R0325_DP3_OFFSET_CTRL2, 0x00},
72*4882a593Smuzhiyun {MAX98373_R0326_DP3_HCTRL, 0x00},
73*4882a593Smuzhiyun {MAX98373_R0327_DP3_BLOCK_CTRL3, 0x00},
74*4882a593Smuzhiyun {MAX98373_R0330_DP3_CHANNEL_EN, 0x00},
75*4882a593Smuzhiyun {MAX98373_R0332_DP3_SAMPLE_CTRL1, 0x00},
76*4882a593Smuzhiyun {MAX98373_R0333_DP3_SAMPLE_CTRL2, 0x00},
77*4882a593Smuzhiyun {MAX98373_R0334_DP3_OFFSET_CTRL1, 0x00},
78*4882a593Smuzhiyun {MAX98373_R0335_DP3_OFFSET_CTRL2, 0x00},
79*4882a593Smuzhiyun {MAX98373_R0336_DP3_HCTRL, 0x00},
80*4882a593Smuzhiyun {MAX98373_R0337_DP3_BLOCK_CTRL3, 0x00},
81*4882a593Smuzhiyun {MAX98373_R2000_SW_RESET, 0x00},
82*4882a593Smuzhiyun {MAX98373_R2001_INT_RAW1, 0x00},
83*4882a593Smuzhiyun {MAX98373_R2002_INT_RAW2, 0x00},
84*4882a593Smuzhiyun {MAX98373_R2003_INT_RAW3, 0x00},
85*4882a593Smuzhiyun {MAX98373_R2004_INT_STATE1, 0x00},
86*4882a593Smuzhiyun {MAX98373_R2005_INT_STATE2, 0x00},
87*4882a593Smuzhiyun {MAX98373_R2006_INT_STATE3, 0x00},
88*4882a593Smuzhiyun {MAX98373_R2007_INT_FLAG1, 0x00},
89*4882a593Smuzhiyun {MAX98373_R2008_INT_FLAG2, 0x00},
90*4882a593Smuzhiyun {MAX98373_R2009_INT_FLAG3, 0x00},
91*4882a593Smuzhiyun {MAX98373_R200A_INT_EN1, 0x00},
92*4882a593Smuzhiyun {MAX98373_R200B_INT_EN2, 0x00},
93*4882a593Smuzhiyun {MAX98373_R200C_INT_EN3, 0x00},
94*4882a593Smuzhiyun {MAX98373_R200D_INT_FLAG_CLR1, 0x00},
95*4882a593Smuzhiyun {MAX98373_R200E_INT_FLAG_CLR2, 0x00},
96*4882a593Smuzhiyun {MAX98373_R200F_INT_FLAG_CLR3, 0x00},
97*4882a593Smuzhiyun {MAX98373_R2010_IRQ_CTRL, 0x00},
98*4882a593Smuzhiyun {MAX98373_R2014_THERM_WARN_THRESH, 0x10},
99*4882a593Smuzhiyun {MAX98373_R2015_THERM_SHDN_THRESH, 0x27},
100*4882a593Smuzhiyun {MAX98373_R2016_THERM_HYSTERESIS, 0x01},
101*4882a593Smuzhiyun {MAX98373_R2017_THERM_FOLDBACK_SET, 0xC0},
102*4882a593Smuzhiyun {MAX98373_R2018_THERM_FOLDBACK_EN, 0x00},
103*4882a593Smuzhiyun {MAX98373_R201E_PIN_DRIVE_STRENGTH, 0x55},
104*4882a593Smuzhiyun {MAX98373_R2020_PCM_TX_HIZ_EN_1, 0xFE},
105*4882a593Smuzhiyun {MAX98373_R2021_PCM_TX_HIZ_EN_2, 0xFF},
106*4882a593Smuzhiyun {MAX98373_R2022_PCM_TX_SRC_1, 0x00},
107*4882a593Smuzhiyun {MAX98373_R2023_PCM_TX_SRC_2, 0x00},
108*4882a593Smuzhiyun {MAX98373_R2024_PCM_DATA_FMT_CFG, 0xC0},
109*4882a593Smuzhiyun {MAX98373_R2025_AUDIO_IF_MODE, 0x00},
110*4882a593Smuzhiyun {MAX98373_R2026_PCM_CLOCK_RATIO, 0x04},
111*4882a593Smuzhiyun {MAX98373_R2027_PCM_SR_SETUP_1, 0x08},
112*4882a593Smuzhiyun {MAX98373_R2028_PCM_SR_SETUP_2, 0x88},
113*4882a593Smuzhiyun {MAX98373_R2029_PCM_TO_SPK_MONO_MIX_1, 0x00},
114*4882a593Smuzhiyun {MAX98373_R202A_PCM_TO_SPK_MONO_MIX_2, 0x00},
115*4882a593Smuzhiyun {MAX98373_R202B_PCM_RX_EN, 0x00},
116*4882a593Smuzhiyun {MAX98373_R202C_PCM_TX_EN, 0x00},
117*4882a593Smuzhiyun {MAX98373_R202E_ICC_RX_CH_EN_1, 0x00},
118*4882a593Smuzhiyun {MAX98373_R202F_ICC_RX_CH_EN_2, 0x00},
119*4882a593Smuzhiyun {MAX98373_R2030_ICC_TX_HIZ_EN_1, 0xFF},
120*4882a593Smuzhiyun {MAX98373_R2031_ICC_TX_HIZ_EN_2, 0xFF},
121*4882a593Smuzhiyun {MAX98373_R2032_ICC_LINK_EN_CFG, 0x30},
122*4882a593Smuzhiyun {MAX98373_R2034_ICC_TX_CNTL, 0x00},
123*4882a593Smuzhiyun {MAX98373_R2035_ICC_TX_EN, 0x00},
124*4882a593Smuzhiyun {MAX98373_R2036_SOUNDWIRE_CTRL, 0x05},
125*4882a593Smuzhiyun {MAX98373_R203D_AMP_DIG_VOL_CTRL, 0x00},
126*4882a593Smuzhiyun {MAX98373_R203E_AMP_PATH_GAIN, 0x08},
127*4882a593Smuzhiyun {MAX98373_R203F_AMP_DSP_CFG, 0x02},
128*4882a593Smuzhiyun {MAX98373_R2040_TONE_GEN_CFG, 0x00},
129*4882a593Smuzhiyun {MAX98373_R2041_AMP_CFG, 0x03},
130*4882a593Smuzhiyun {MAX98373_R2042_AMP_EDGE_RATE_CFG, 0x00},
131*4882a593Smuzhiyun {MAX98373_R2043_AMP_EN, 0x00},
132*4882a593Smuzhiyun {MAX98373_R2046_IV_SENSE_ADC_DSP_CFG, 0x04},
133*4882a593Smuzhiyun {MAX98373_R2047_IV_SENSE_ADC_EN, 0x00},
134*4882a593Smuzhiyun {MAX98373_R2051_MEAS_ADC_SAMPLING_RATE, 0x00},
135*4882a593Smuzhiyun {MAX98373_R2052_MEAS_ADC_PVDD_FLT_CFG, 0x00},
136*4882a593Smuzhiyun {MAX98373_R2053_MEAS_ADC_THERM_FLT_CFG, 0x00},
137*4882a593Smuzhiyun {MAX98373_R2054_MEAS_ADC_PVDD_CH_READBACK, 0x00},
138*4882a593Smuzhiyun {MAX98373_R2055_MEAS_ADC_THERM_CH_READBACK, 0x00},
139*4882a593Smuzhiyun {MAX98373_R2056_MEAS_ADC_PVDD_CH_EN, 0x00},
140*4882a593Smuzhiyun {MAX98373_R2090_BDE_LVL_HOLD, 0x00},
141*4882a593Smuzhiyun {MAX98373_R2091_BDE_GAIN_ATK_REL_RATE, 0x00},
142*4882a593Smuzhiyun {MAX98373_R2092_BDE_CLIPPER_MODE, 0x00},
143*4882a593Smuzhiyun {MAX98373_R2097_BDE_L1_THRESH, 0x00},
144*4882a593Smuzhiyun {MAX98373_R2098_BDE_L2_THRESH, 0x00},
145*4882a593Smuzhiyun {MAX98373_R2099_BDE_L3_THRESH, 0x00},
146*4882a593Smuzhiyun {MAX98373_R209A_BDE_L4_THRESH, 0x00},
147*4882a593Smuzhiyun {MAX98373_R209B_BDE_THRESH_HYST, 0x00},
148*4882a593Smuzhiyun {MAX98373_R20A8_BDE_L1_CFG_1, 0x00},
149*4882a593Smuzhiyun {MAX98373_R20A9_BDE_L1_CFG_2, 0x00},
150*4882a593Smuzhiyun {MAX98373_R20AA_BDE_L1_CFG_3, 0x00},
151*4882a593Smuzhiyun {MAX98373_R20AB_BDE_L2_CFG_1, 0x00},
152*4882a593Smuzhiyun {MAX98373_R20AC_BDE_L2_CFG_2, 0x00},
153*4882a593Smuzhiyun {MAX98373_R20AD_BDE_L2_CFG_3, 0x00},
154*4882a593Smuzhiyun {MAX98373_R20AE_BDE_L3_CFG_1, 0x00},
155*4882a593Smuzhiyun {MAX98373_R20AF_BDE_L3_CFG_2, 0x00},
156*4882a593Smuzhiyun {MAX98373_R20B0_BDE_L3_CFG_3, 0x00},
157*4882a593Smuzhiyun {MAX98373_R20B1_BDE_L4_CFG_1, 0x00},
158*4882a593Smuzhiyun {MAX98373_R20B2_BDE_L4_CFG_2, 0x00},
159*4882a593Smuzhiyun {MAX98373_R20B3_BDE_L4_CFG_3, 0x00},
160*4882a593Smuzhiyun {MAX98373_R20B4_BDE_INFINITE_HOLD_RELEASE, 0x00},
161*4882a593Smuzhiyun {MAX98373_R20B5_BDE_EN, 0x00},
162*4882a593Smuzhiyun {MAX98373_R20B6_BDE_CUR_STATE_READBACK, 0x00},
163*4882a593Smuzhiyun {MAX98373_R20D1_DHT_CFG, 0x01},
164*4882a593Smuzhiyun {MAX98373_R20D2_DHT_ATTACK_CFG, 0x02},
165*4882a593Smuzhiyun {MAX98373_R20D3_DHT_RELEASE_CFG, 0x03},
166*4882a593Smuzhiyun {MAX98373_R20D4_DHT_EN, 0x00},
167*4882a593Smuzhiyun {MAX98373_R20E0_LIMITER_THRESH_CFG, 0x00},
168*4882a593Smuzhiyun {MAX98373_R20E1_LIMITER_ATK_REL_RATES, 0x00},
169*4882a593Smuzhiyun {MAX98373_R20E2_LIMITER_EN, 0x00},
170*4882a593Smuzhiyun {MAX98373_R20FE_DEVICE_AUTO_RESTART_CFG, 0x00},
171*4882a593Smuzhiyun {MAX98373_R20FF_GLOBAL_SHDN, 0x00},
172*4882a593Smuzhiyun {MAX98373_R21FF_REV_ID, 0x42},
173*4882a593Smuzhiyun };
174*4882a593Smuzhiyun
max98373_readable_register(struct device * dev,unsigned int reg)175*4882a593Smuzhiyun static bool max98373_readable_register(struct device *dev, unsigned int reg)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun switch (reg) {
178*4882a593Smuzhiyun case MAX98373_R21FF_REV_ID:
179*4882a593Smuzhiyun case MAX98373_R2010_IRQ_CTRL:
180*4882a593Smuzhiyun /* SoundWire Control Port Registers */
181*4882a593Smuzhiyun case MAX98373_R0040_SCP_INIT_STAT_1 ... MAX98373_R0070_SCP_FRAME_CTLR:
182*4882a593Smuzhiyun /* Soundwire Data Port 1 Registers */
183*4882a593Smuzhiyun case MAX98373_R0100_DP1_INIT_STAT ... MAX98373_R0137_DP1_BLOCK_CTRL3:
184*4882a593Smuzhiyun /* Soundwire Data Port 3 Registers */
185*4882a593Smuzhiyun case MAX98373_R0300_DP3_INIT_STAT ... MAX98373_R0337_DP3_BLOCK_CTRL3:
186*4882a593Smuzhiyun case MAX98373_R2000_SW_RESET ... MAX98373_R200C_INT_EN3:
187*4882a593Smuzhiyun case MAX98373_R2014_THERM_WARN_THRESH
188*4882a593Smuzhiyun ... MAX98373_R2018_THERM_FOLDBACK_EN:
189*4882a593Smuzhiyun case MAX98373_R201E_PIN_DRIVE_STRENGTH
190*4882a593Smuzhiyun ... MAX98373_R2036_SOUNDWIRE_CTRL:
191*4882a593Smuzhiyun case MAX98373_R203D_AMP_DIG_VOL_CTRL ... MAX98373_R2043_AMP_EN:
192*4882a593Smuzhiyun case MAX98373_R2046_IV_SENSE_ADC_DSP_CFG
193*4882a593Smuzhiyun ... MAX98373_R2047_IV_SENSE_ADC_EN:
194*4882a593Smuzhiyun case MAX98373_R2051_MEAS_ADC_SAMPLING_RATE
195*4882a593Smuzhiyun ... MAX98373_R2056_MEAS_ADC_PVDD_CH_EN:
196*4882a593Smuzhiyun case MAX98373_R2090_BDE_LVL_HOLD ... MAX98373_R2092_BDE_CLIPPER_MODE:
197*4882a593Smuzhiyun case MAX98373_R2097_BDE_L1_THRESH
198*4882a593Smuzhiyun ... MAX98373_R209B_BDE_THRESH_HYST:
199*4882a593Smuzhiyun case MAX98373_R20A8_BDE_L1_CFG_1 ... MAX98373_R20B3_BDE_L4_CFG_3:
200*4882a593Smuzhiyun case MAX98373_R20B5_BDE_EN ... MAX98373_R20B6_BDE_CUR_STATE_READBACK:
201*4882a593Smuzhiyun case MAX98373_R20D1_DHT_CFG ... MAX98373_R20D4_DHT_EN:
202*4882a593Smuzhiyun case MAX98373_R20E0_LIMITER_THRESH_CFG ... MAX98373_R20E2_LIMITER_EN:
203*4882a593Smuzhiyun case MAX98373_R20FE_DEVICE_AUTO_RESTART_CFG
204*4882a593Smuzhiyun ... MAX98373_R20FF_GLOBAL_SHDN:
205*4882a593Smuzhiyun return true;
206*4882a593Smuzhiyun default:
207*4882a593Smuzhiyun return false;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun };
210*4882a593Smuzhiyun
max98373_volatile_reg(struct device * dev,unsigned int reg)211*4882a593Smuzhiyun static bool max98373_volatile_reg(struct device *dev, unsigned int reg)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun switch (reg) {
214*4882a593Smuzhiyun case MAX98373_R2054_MEAS_ADC_PVDD_CH_READBACK:
215*4882a593Smuzhiyun case MAX98373_R2055_MEAS_ADC_THERM_CH_READBACK:
216*4882a593Smuzhiyun case MAX98373_R20B6_BDE_CUR_STATE_READBACK:
217*4882a593Smuzhiyun case MAX98373_R20FF_GLOBAL_SHDN:
218*4882a593Smuzhiyun case MAX98373_R21FF_REV_ID:
219*4882a593Smuzhiyun /* SoundWire Control Port Registers */
220*4882a593Smuzhiyun case MAX98373_R0040_SCP_INIT_STAT_1 ... MAX98373_R0070_SCP_FRAME_CTLR:
221*4882a593Smuzhiyun /* Soundwire Data Port 1 Registers */
222*4882a593Smuzhiyun case MAX98373_R0100_DP1_INIT_STAT ... MAX98373_R0137_DP1_BLOCK_CTRL3:
223*4882a593Smuzhiyun /* Soundwire Data Port 3 Registers */
224*4882a593Smuzhiyun case MAX98373_R0300_DP3_INIT_STAT ... MAX98373_R0337_DP3_BLOCK_CTRL3:
225*4882a593Smuzhiyun case MAX98373_R2000_SW_RESET ... MAX98373_R2009_INT_FLAG3:
226*4882a593Smuzhiyun return true;
227*4882a593Smuzhiyun default:
228*4882a593Smuzhiyun return false;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun static const struct regmap_config max98373_sdw_regmap = {
233*4882a593Smuzhiyun .reg_bits = 32,
234*4882a593Smuzhiyun .val_bits = 8,
235*4882a593Smuzhiyun .max_register = MAX98373_R21FF_REV_ID,
236*4882a593Smuzhiyun .reg_defaults = max98373_reg,
237*4882a593Smuzhiyun .num_reg_defaults = ARRAY_SIZE(max98373_reg),
238*4882a593Smuzhiyun .readable_reg = max98373_readable_register,
239*4882a593Smuzhiyun .volatile_reg = max98373_volatile_reg,
240*4882a593Smuzhiyun .cache_type = REGCACHE_RBTREE,
241*4882a593Smuzhiyun .use_single_read = true,
242*4882a593Smuzhiyun .use_single_write = true,
243*4882a593Smuzhiyun };
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* Power management functions and structure */
max98373_suspend(struct device * dev)246*4882a593Smuzhiyun static __maybe_unused int max98373_suspend(struct device *dev)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun struct max98373_priv *max98373 = dev_get_drvdata(dev);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun regcache_cache_only(max98373->regmap, true);
251*4882a593Smuzhiyun regcache_mark_dirty(max98373->regmap);
252*4882a593Smuzhiyun return 0;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
max98373_resume(struct device * dev)255*4882a593Smuzhiyun static __maybe_unused int max98373_resume(struct device *dev)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun struct sdw_slave *slave = dev_to_sdw_dev(dev);
258*4882a593Smuzhiyun struct max98373_priv *max98373 = dev_get_drvdata(dev);
259*4882a593Smuzhiyun unsigned long time;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun if (!max98373->first_hw_init)
262*4882a593Smuzhiyun return 0;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun if (!slave->unattach_request)
265*4882a593Smuzhiyun goto regmap_sync;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun time = wait_for_completion_timeout(&slave->initialization_complete,
268*4882a593Smuzhiyun msecs_to_jiffies(2000));
269*4882a593Smuzhiyun if (!time) {
270*4882a593Smuzhiyun dev_err(dev, "Initialization not complete, timed out\n");
271*4882a593Smuzhiyun return -ETIMEDOUT;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun regmap_sync:
275*4882a593Smuzhiyun slave->unattach_request = 0;
276*4882a593Smuzhiyun regcache_cache_only(max98373->regmap, false);
277*4882a593Smuzhiyun regcache_sync(max98373->regmap);
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun return 0;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun static const struct dev_pm_ops max98373_pm = {
283*4882a593Smuzhiyun SET_SYSTEM_SLEEP_PM_OPS(max98373_suspend, max98373_resume)
284*4882a593Smuzhiyun SET_RUNTIME_PM_OPS(max98373_suspend, max98373_resume, NULL)
285*4882a593Smuzhiyun };
286*4882a593Smuzhiyun
max98373_read_prop(struct sdw_slave * slave)287*4882a593Smuzhiyun static int max98373_read_prop(struct sdw_slave *slave)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun struct sdw_slave_prop *prop = &slave->prop;
290*4882a593Smuzhiyun int nval, i;
291*4882a593Smuzhiyun u32 bit;
292*4882a593Smuzhiyun unsigned long addr;
293*4882a593Smuzhiyun struct sdw_dpn_prop *dpn;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun /* BITMAP: 00001000 Dataport 3 is active */
298*4882a593Smuzhiyun prop->source_ports = BIT(3);
299*4882a593Smuzhiyun /* BITMAP: 00000010 Dataport 1 is active */
300*4882a593Smuzhiyun prop->sink_ports = BIT(1);
301*4882a593Smuzhiyun prop->paging_support = true;
302*4882a593Smuzhiyun prop->clk_stop_timeout = 20;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun nval = hweight32(prop->source_ports);
305*4882a593Smuzhiyun prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
306*4882a593Smuzhiyun sizeof(*prop->src_dpn_prop),
307*4882a593Smuzhiyun GFP_KERNEL);
308*4882a593Smuzhiyun if (!prop->src_dpn_prop)
309*4882a593Smuzhiyun return -ENOMEM;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun i = 0;
312*4882a593Smuzhiyun dpn = prop->src_dpn_prop;
313*4882a593Smuzhiyun addr = prop->source_ports;
314*4882a593Smuzhiyun for_each_set_bit(bit, &addr, 32) {
315*4882a593Smuzhiyun dpn[i].num = bit;
316*4882a593Smuzhiyun dpn[i].type = SDW_DPN_FULL;
317*4882a593Smuzhiyun dpn[i].simple_ch_prep_sm = true;
318*4882a593Smuzhiyun dpn[i].ch_prep_timeout = 10;
319*4882a593Smuzhiyun i++;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /* do this again for sink now */
323*4882a593Smuzhiyun nval = hweight32(prop->sink_ports);
324*4882a593Smuzhiyun prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
325*4882a593Smuzhiyun sizeof(*prop->sink_dpn_prop),
326*4882a593Smuzhiyun GFP_KERNEL);
327*4882a593Smuzhiyun if (!prop->sink_dpn_prop)
328*4882a593Smuzhiyun return -ENOMEM;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun i = 0;
331*4882a593Smuzhiyun dpn = prop->sink_dpn_prop;
332*4882a593Smuzhiyun addr = prop->sink_ports;
333*4882a593Smuzhiyun for_each_set_bit(bit, &addr, 32) {
334*4882a593Smuzhiyun dpn[i].num = bit;
335*4882a593Smuzhiyun dpn[i].type = SDW_DPN_FULL;
336*4882a593Smuzhiyun dpn[i].simple_ch_prep_sm = true;
337*4882a593Smuzhiyun dpn[i].ch_prep_timeout = 10;
338*4882a593Smuzhiyun i++;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /* set the timeout values */
342*4882a593Smuzhiyun prop->clk_stop_timeout = 20;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun return 0;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
max98373_io_init(struct sdw_slave * slave)347*4882a593Smuzhiyun static int max98373_io_init(struct sdw_slave *slave)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun struct device *dev = &slave->dev;
350*4882a593Smuzhiyun struct max98373_priv *max98373 = dev_get_drvdata(dev);
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun if (max98373->first_hw_init) {
353*4882a593Smuzhiyun regcache_cache_only(max98373->regmap, false);
354*4882a593Smuzhiyun regcache_cache_bypass(max98373->regmap, true);
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /*
358*4882a593Smuzhiyun * PM runtime is only enabled when a Slave reports as Attached
359*4882a593Smuzhiyun */
360*4882a593Smuzhiyun if (!max98373->first_hw_init) {
361*4882a593Smuzhiyun /* set autosuspend parameters */
362*4882a593Smuzhiyun pm_runtime_set_autosuspend_delay(dev, 3000);
363*4882a593Smuzhiyun pm_runtime_use_autosuspend(dev);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /* update count of parent 'active' children */
366*4882a593Smuzhiyun pm_runtime_set_active(dev);
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun /* make sure the device does not suspend immediately */
369*4882a593Smuzhiyun pm_runtime_mark_last_busy(dev);
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun pm_runtime_enable(dev);
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun pm_runtime_get_noresume(dev);
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /* Software Reset */
377*4882a593Smuzhiyun max98373_reset(max98373, dev);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /* Set soundwire mode */
380*4882a593Smuzhiyun regmap_write(max98373->regmap, MAX98373_R2025_AUDIO_IF_MODE, 3);
381*4882a593Smuzhiyun /* Enable ADC */
382*4882a593Smuzhiyun regmap_write(max98373->regmap, MAX98373_R2047_IV_SENSE_ADC_EN, 3);
383*4882a593Smuzhiyun /* Set default Soundwire clock */
384*4882a593Smuzhiyun regmap_write(max98373->regmap, MAX98373_R2036_SOUNDWIRE_CTRL, 5);
385*4882a593Smuzhiyun /* Set default sampling rate for speaker and IVDAC */
386*4882a593Smuzhiyun regmap_write(max98373->regmap, MAX98373_R2028_PCM_SR_SETUP_2, 0x88);
387*4882a593Smuzhiyun /* IV default slot configuration */
388*4882a593Smuzhiyun regmap_write(max98373->regmap,
389*4882a593Smuzhiyun MAX98373_R2020_PCM_TX_HIZ_EN_1,
390*4882a593Smuzhiyun 0xFF);
391*4882a593Smuzhiyun regmap_write(max98373->regmap,
392*4882a593Smuzhiyun MAX98373_R2021_PCM_TX_HIZ_EN_2,
393*4882a593Smuzhiyun 0xFF);
394*4882a593Smuzhiyun /* L/R mix configuration */
395*4882a593Smuzhiyun regmap_write(max98373->regmap,
396*4882a593Smuzhiyun MAX98373_R2029_PCM_TO_SPK_MONO_MIX_1,
397*4882a593Smuzhiyun 0x80);
398*4882a593Smuzhiyun regmap_write(max98373->regmap,
399*4882a593Smuzhiyun MAX98373_R202A_PCM_TO_SPK_MONO_MIX_2,
400*4882a593Smuzhiyun 0x1);
401*4882a593Smuzhiyun /* Enable DC blocker */
402*4882a593Smuzhiyun regmap_write(max98373->regmap,
403*4882a593Smuzhiyun MAX98373_R203F_AMP_DSP_CFG,
404*4882a593Smuzhiyun 0x3);
405*4882a593Smuzhiyun /* Enable IMON VMON DC blocker */
406*4882a593Smuzhiyun regmap_write(max98373->regmap,
407*4882a593Smuzhiyun MAX98373_R2046_IV_SENSE_ADC_DSP_CFG,
408*4882a593Smuzhiyun 0x7);
409*4882a593Smuzhiyun /* voltage, current slot configuration */
410*4882a593Smuzhiyun regmap_write(max98373->regmap,
411*4882a593Smuzhiyun MAX98373_R2022_PCM_TX_SRC_1,
412*4882a593Smuzhiyun (max98373->i_slot << MAX98373_PCM_TX_CH_SRC_A_I_SHIFT |
413*4882a593Smuzhiyun max98373->v_slot) & 0xFF);
414*4882a593Smuzhiyun if (max98373->v_slot < 8)
415*4882a593Smuzhiyun regmap_update_bits(max98373->regmap,
416*4882a593Smuzhiyun MAX98373_R2020_PCM_TX_HIZ_EN_1,
417*4882a593Smuzhiyun 1 << max98373->v_slot, 0);
418*4882a593Smuzhiyun else
419*4882a593Smuzhiyun regmap_update_bits(max98373->regmap,
420*4882a593Smuzhiyun MAX98373_R2021_PCM_TX_HIZ_EN_2,
421*4882a593Smuzhiyun 1 << (max98373->v_slot - 8), 0);
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun if (max98373->i_slot < 8)
424*4882a593Smuzhiyun regmap_update_bits(max98373->regmap,
425*4882a593Smuzhiyun MAX98373_R2020_PCM_TX_HIZ_EN_1,
426*4882a593Smuzhiyun 1 << max98373->i_slot, 0);
427*4882a593Smuzhiyun else
428*4882a593Smuzhiyun regmap_update_bits(max98373->regmap,
429*4882a593Smuzhiyun MAX98373_R2021_PCM_TX_HIZ_EN_2,
430*4882a593Smuzhiyun 1 << (max98373->i_slot - 8), 0);
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun /* speaker feedback slot configuration */
433*4882a593Smuzhiyun regmap_write(max98373->regmap,
434*4882a593Smuzhiyun MAX98373_R2023_PCM_TX_SRC_2,
435*4882a593Smuzhiyun max98373->spkfb_slot & 0xFF);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /* Set interleave mode */
438*4882a593Smuzhiyun if (max98373->interleave_mode)
439*4882a593Smuzhiyun regmap_update_bits(max98373->regmap,
440*4882a593Smuzhiyun MAX98373_R2024_PCM_DATA_FMT_CFG,
441*4882a593Smuzhiyun MAX98373_PCM_TX_CH_INTERLEAVE_MASK,
442*4882a593Smuzhiyun MAX98373_PCM_TX_CH_INTERLEAVE_MASK);
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun /* Speaker enable */
445*4882a593Smuzhiyun regmap_update_bits(max98373->regmap,
446*4882a593Smuzhiyun MAX98373_R2043_AMP_EN,
447*4882a593Smuzhiyun MAX98373_SPK_EN_MASK, 1);
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun regmap_write(max98373->regmap, MAX98373_R20B5_BDE_EN, 1);
450*4882a593Smuzhiyun regmap_write(max98373->regmap, MAX98373_R20E2_LIMITER_EN, 1);
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun if (max98373->first_hw_init) {
453*4882a593Smuzhiyun regcache_cache_bypass(max98373->regmap, false);
454*4882a593Smuzhiyun regcache_mark_dirty(max98373->regmap);
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun max98373->first_hw_init = true;
458*4882a593Smuzhiyun max98373->hw_init = true;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun pm_runtime_mark_last_busy(dev);
461*4882a593Smuzhiyun pm_runtime_put_autosuspend(dev);
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun return 0;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun
max98373_clock_calculate(struct sdw_slave * slave,unsigned int clk_freq)466*4882a593Smuzhiyun static int max98373_clock_calculate(struct sdw_slave *slave,
467*4882a593Smuzhiyun unsigned int clk_freq)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun int x, y;
470*4882a593Smuzhiyun static const int max98373_clk_family[] = {
471*4882a593Smuzhiyun 7680000, 8400000, 9600000, 11289600,
472*4882a593Smuzhiyun 12000000, 12288000, 13000000
473*4882a593Smuzhiyun };
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun for (x = 0; x < 4; x++)
476*4882a593Smuzhiyun for (y = 0; y < ARRAY_SIZE(max98373_clk_family); y++)
477*4882a593Smuzhiyun if (clk_freq == (max98373_clk_family[y] >> x))
478*4882a593Smuzhiyun return (x << 3) + y;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /* Set default clock (12.288 Mhz) if the value is not in the list */
481*4882a593Smuzhiyun dev_err(&slave->dev, "Requested clock not found. (clk_freq = %d)\n",
482*4882a593Smuzhiyun clk_freq);
483*4882a593Smuzhiyun return 0x5;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun
max98373_clock_config(struct sdw_slave * slave,struct sdw_bus_params * params)486*4882a593Smuzhiyun static int max98373_clock_config(struct sdw_slave *slave,
487*4882a593Smuzhiyun struct sdw_bus_params *params)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun struct device *dev = &slave->dev;
490*4882a593Smuzhiyun struct max98373_priv *max98373 = dev_get_drvdata(dev);
491*4882a593Smuzhiyun unsigned int clk_freq, value;
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun clk_freq = (params->curr_dr_freq >> 1);
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun /*
496*4882a593Smuzhiyun * Select the proper value for the register based on the
497*4882a593Smuzhiyun * requested clock. If the value is not in the list,
498*4882a593Smuzhiyun * use reasonable default - 12.288 Mhz
499*4882a593Smuzhiyun */
500*4882a593Smuzhiyun value = max98373_clock_calculate(slave, clk_freq);
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun /* SWCLK */
503*4882a593Smuzhiyun regmap_write(max98373->regmap, MAX98373_R2036_SOUNDWIRE_CTRL, value);
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun /* The default Sampling Rate value for IV is 48KHz*/
506*4882a593Smuzhiyun regmap_write(max98373->regmap, MAX98373_R2028_PCM_SR_SETUP_2, 0x88);
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun return 0;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun #define MAX98373_RATES SNDRV_PCM_RATE_8000_96000
512*4882a593Smuzhiyun #define MAX98373_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
513*4882a593Smuzhiyun
max98373_sdw_dai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)514*4882a593Smuzhiyun static int max98373_sdw_dai_hw_params(struct snd_pcm_substream *substream,
515*4882a593Smuzhiyun struct snd_pcm_hw_params *params,
516*4882a593Smuzhiyun struct snd_soc_dai *dai)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun struct snd_soc_component *component = dai->component;
519*4882a593Smuzhiyun struct max98373_priv *max98373 =
520*4882a593Smuzhiyun snd_soc_component_get_drvdata(component);
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun struct sdw_stream_config stream_config;
523*4882a593Smuzhiyun struct sdw_port_config port_config;
524*4882a593Smuzhiyun enum sdw_data_direction direction;
525*4882a593Smuzhiyun struct sdw_stream_data *stream;
526*4882a593Smuzhiyun int ret, chan_sz, sampling_rate;
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun stream = snd_soc_dai_get_dma_data(dai, substream);
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun if (!stream)
531*4882a593Smuzhiyun return -EINVAL;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun if (!max98373->slave)
534*4882a593Smuzhiyun return -EINVAL;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
537*4882a593Smuzhiyun direction = SDW_DATA_DIR_RX;
538*4882a593Smuzhiyun port_config.num = 1;
539*4882a593Smuzhiyun } else {
540*4882a593Smuzhiyun direction = SDW_DATA_DIR_TX;
541*4882a593Smuzhiyun port_config.num = 3;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun stream_config.frame_rate = params_rate(params);
545*4882a593Smuzhiyun stream_config.bps = snd_pcm_format_width(params_format(params));
546*4882a593Smuzhiyun stream_config.direction = direction;
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun if (max98373->slot && direction == SDW_DATA_DIR_RX) {
549*4882a593Smuzhiyun stream_config.ch_count = max98373->slot;
550*4882a593Smuzhiyun port_config.ch_mask = max98373->rx_mask;
551*4882a593Smuzhiyun } else {
552*4882a593Smuzhiyun /* only IV are supported by capture */
553*4882a593Smuzhiyun if (direction == SDW_DATA_DIR_TX)
554*4882a593Smuzhiyun stream_config.ch_count = 2;
555*4882a593Smuzhiyun else
556*4882a593Smuzhiyun stream_config.ch_count = params_channels(params);
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun port_config.ch_mask = GENMASK((int)stream_config.ch_count - 1, 0);
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun ret = sdw_stream_add_slave(max98373->slave, &stream_config,
562*4882a593Smuzhiyun &port_config, 1, stream->sdw_stream);
563*4882a593Smuzhiyun if (ret) {
564*4882a593Smuzhiyun dev_err(dai->dev, "Unable to configure port\n");
565*4882a593Smuzhiyun return ret;
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun if (params_channels(params) > 16) {
569*4882a593Smuzhiyun dev_err(component->dev, "Unsupported channels %d\n",
570*4882a593Smuzhiyun params_channels(params));
571*4882a593Smuzhiyun return -EINVAL;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun /* Channel size configuration */
575*4882a593Smuzhiyun switch (snd_pcm_format_width(params_format(params))) {
576*4882a593Smuzhiyun case 16:
577*4882a593Smuzhiyun chan_sz = MAX98373_PCM_MODE_CFG_CHANSZ_16;
578*4882a593Smuzhiyun break;
579*4882a593Smuzhiyun case 24:
580*4882a593Smuzhiyun chan_sz = MAX98373_PCM_MODE_CFG_CHANSZ_24;
581*4882a593Smuzhiyun break;
582*4882a593Smuzhiyun case 32:
583*4882a593Smuzhiyun chan_sz = MAX98373_PCM_MODE_CFG_CHANSZ_32;
584*4882a593Smuzhiyun break;
585*4882a593Smuzhiyun default:
586*4882a593Smuzhiyun dev_err(component->dev, "Channel size unsupported %d\n",
587*4882a593Smuzhiyun params_format(params));
588*4882a593Smuzhiyun return -EINVAL;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun max98373->ch_size = snd_pcm_format_width(params_format(params));
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun regmap_update_bits(max98373->regmap,
594*4882a593Smuzhiyun MAX98373_R2024_PCM_DATA_FMT_CFG,
595*4882a593Smuzhiyun MAX98373_PCM_MODE_CFG_CHANSZ_MASK, chan_sz);
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun dev_dbg(component->dev, "Format supported %d", params_format(params));
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun /* Sampling rate configuration */
600*4882a593Smuzhiyun switch (params_rate(params)) {
601*4882a593Smuzhiyun case 8000:
602*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_8000;
603*4882a593Smuzhiyun break;
604*4882a593Smuzhiyun case 11025:
605*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_11025;
606*4882a593Smuzhiyun break;
607*4882a593Smuzhiyun case 12000:
608*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_12000;
609*4882a593Smuzhiyun break;
610*4882a593Smuzhiyun case 16000:
611*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_16000;
612*4882a593Smuzhiyun break;
613*4882a593Smuzhiyun case 22050:
614*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_22050;
615*4882a593Smuzhiyun break;
616*4882a593Smuzhiyun case 24000:
617*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_24000;
618*4882a593Smuzhiyun break;
619*4882a593Smuzhiyun case 32000:
620*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_32000;
621*4882a593Smuzhiyun break;
622*4882a593Smuzhiyun case 44100:
623*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_44100;
624*4882a593Smuzhiyun break;
625*4882a593Smuzhiyun case 48000:
626*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_48000;
627*4882a593Smuzhiyun break;
628*4882a593Smuzhiyun case 88200:
629*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_88200;
630*4882a593Smuzhiyun break;
631*4882a593Smuzhiyun case 96000:
632*4882a593Smuzhiyun sampling_rate = MAX98373_PCM_SR_SET1_SR_96000;
633*4882a593Smuzhiyun break;
634*4882a593Smuzhiyun default:
635*4882a593Smuzhiyun dev_err(component->dev, "Rate %d is not supported\n",
636*4882a593Smuzhiyun params_rate(params));
637*4882a593Smuzhiyun return -EINVAL;
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun /* set correct sampling frequency */
641*4882a593Smuzhiyun regmap_update_bits(max98373->regmap,
642*4882a593Smuzhiyun MAX98373_R2028_PCM_SR_SETUP_2,
643*4882a593Smuzhiyun MAX98373_PCM_SR_SET2_SR_MASK,
644*4882a593Smuzhiyun sampling_rate << MAX98373_PCM_SR_SET2_SR_SHIFT);
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun /* set sampling rate of IV */
647*4882a593Smuzhiyun regmap_update_bits(max98373->regmap,
648*4882a593Smuzhiyun MAX98373_R2028_PCM_SR_SETUP_2,
649*4882a593Smuzhiyun MAX98373_PCM_SR_SET2_IVADC_SR_MASK,
650*4882a593Smuzhiyun sampling_rate);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun return 0;
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun
max98373_pcm_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)655*4882a593Smuzhiyun static int max98373_pcm_hw_free(struct snd_pcm_substream *substream,
656*4882a593Smuzhiyun struct snd_soc_dai *dai)
657*4882a593Smuzhiyun {
658*4882a593Smuzhiyun struct snd_soc_component *component = dai->component;
659*4882a593Smuzhiyun struct max98373_priv *max98373 =
660*4882a593Smuzhiyun snd_soc_component_get_drvdata(component);
661*4882a593Smuzhiyun struct sdw_stream_data *stream =
662*4882a593Smuzhiyun snd_soc_dai_get_dma_data(dai, substream);
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun if (!max98373->slave)
665*4882a593Smuzhiyun return -EINVAL;
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun sdw_stream_remove_slave(max98373->slave, stream->sdw_stream);
668*4882a593Smuzhiyun return 0;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun
max98373_set_sdw_stream(struct snd_soc_dai * dai,void * sdw_stream,int direction)671*4882a593Smuzhiyun static int max98373_set_sdw_stream(struct snd_soc_dai *dai,
672*4882a593Smuzhiyun void *sdw_stream, int direction)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun struct sdw_stream_data *stream;
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun if (!sdw_stream)
677*4882a593Smuzhiyun return 0;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun stream = kzalloc(sizeof(*stream), GFP_KERNEL);
680*4882a593Smuzhiyun if (!stream)
681*4882a593Smuzhiyun return -ENOMEM;
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun stream->sdw_stream = sdw_stream;
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
686*4882a593Smuzhiyun if (direction == SNDRV_PCM_STREAM_PLAYBACK)
687*4882a593Smuzhiyun dai->playback_dma_data = stream;
688*4882a593Smuzhiyun else
689*4882a593Smuzhiyun dai->capture_dma_data = stream;
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun return 0;
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun
max98373_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)694*4882a593Smuzhiyun static void max98373_shutdown(struct snd_pcm_substream *substream,
695*4882a593Smuzhiyun struct snd_soc_dai *dai)
696*4882a593Smuzhiyun {
697*4882a593Smuzhiyun struct sdw_stream_data *stream;
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun stream = snd_soc_dai_get_dma_data(dai, substream);
700*4882a593Smuzhiyun snd_soc_dai_set_dma_data(dai, substream, NULL);
701*4882a593Smuzhiyun kfree(stream);
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun
max98373_sdw_set_tdm_slot(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)704*4882a593Smuzhiyun static int max98373_sdw_set_tdm_slot(struct snd_soc_dai *dai,
705*4882a593Smuzhiyun unsigned int tx_mask,
706*4882a593Smuzhiyun unsigned int rx_mask,
707*4882a593Smuzhiyun int slots, int slot_width)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun struct snd_soc_component *component = dai->component;
710*4882a593Smuzhiyun struct max98373_priv *max98373 =
711*4882a593Smuzhiyun snd_soc_component_get_drvdata(component);
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun /* tx_mask is unused since it's irrelevant for I/V feedback */
714*4882a593Smuzhiyun if (tx_mask)
715*4882a593Smuzhiyun return -EINVAL;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun if (!rx_mask && !slots && !slot_width)
718*4882a593Smuzhiyun max98373->tdm_mode = false;
719*4882a593Smuzhiyun else
720*4882a593Smuzhiyun max98373->tdm_mode = true;
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun max98373->rx_mask = rx_mask;
723*4882a593Smuzhiyun max98373->slot = slots;
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun return 0;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun static const struct snd_soc_dai_ops max98373_dai_sdw_ops = {
729*4882a593Smuzhiyun .hw_params = max98373_sdw_dai_hw_params,
730*4882a593Smuzhiyun .hw_free = max98373_pcm_hw_free,
731*4882a593Smuzhiyun .set_sdw_stream = max98373_set_sdw_stream,
732*4882a593Smuzhiyun .shutdown = max98373_shutdown,
733*4882a593Smuzhiyun .set_tdm_slot = max98373_sdw_set_tdm_slot,
734*4882a593Smuzhiyun };
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun static struct snd_soc_dai_driver max98373_sdw_dai[] = {
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun .name = "max98373-aif1",
739*4882a593Smuzhiyun .playback = {
740*4882a593Smuzhiyun .stream_name = "HiFi Playback",
741*4882a593Smuzhiyun .channels_min = 1,
742*4882a593Smuzhiyun .channels_max = 2,
743*4882a593Smuzhiyun .rates = MAX98373_RATES,
744*4882a593Smuzhiyun .formats = MAX98373_FORMATS,
745*4882a593Smuzhiyun },
746*4882a593Smuzhiyun .capture = {
747*4882a593Smuzhiyun .stream_name = "HiFi Capture",
748*4882a593Smuzhiyun .channels_min = 1,
749*4882a593Smuzhiyun .channels_max = 2,
750*4882a593Smuzhiyun .rates = MAX98373_RATES,
751*4882a593Smuzhiyun .formats = MAX98373_FORMATS,
752*4882a593Smuzhiyun },
753*4882a593Smuzhiyun .ops = &max98373_dai_sdw_ops,
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun };
756*4882a593Smuzhiyun
max98373_init(struct sdw_slave * slave,struct regmap * regmap)757*4882a593Smuzhiyun static int max98373_init(struct sdw_slave *slave, struct regmap *regmap)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun struct max98373_priv *max98373;
760*4882a593Smuzhiyun int ret;
761*4882a593Smuzhiyun struct device *dev = &slave->dev;
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun /* Allocate and assign private driver data structure */
764*4882a593Smuzhiyun max98373 = devm_kzalloc(dev, sizeof(*max98373), GFP_KERNEL);
765*4882a593Smuzhiyun if (!max98373)
766*4882a593Smuzhiyun return -ENOMEM;
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun dev_set_drvdata(dev, max98373);
769*4882a593Smuzhiyun max98373->regmap = regmap;
770*4882a593Smuzhiyun max98373->slave = slave;
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun /* Read voltage and slot configuration */
773*4882a593Smuzhiyun max98373_slot_config(dev, max98373);
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun max98373->hw_init = false;
776*4882a593Smuzhiyun max98373->first_hw_init = false;
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun /* codec registration */
779*4882a593Smuzhiyun ret = devm_snd_soc_register_component(dev, &soc_codec_dev_max98373_sdw,
780*4882a593Smuzhiyun max98373_sdw_dai,
781*4882a593Smuzhiyun ARRAY_SIZE(max98373_sdw_dai));
782*4882a593Smuzhiyun if (ret < 0)
783*4882a593Smuzhiyun dev_err(dev, "Failed to register codec: %d\n", ret);
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun return ret;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun
max98373_update_status(struct sdw_slave * slave,enum sdw_slave_status status)788*4882a593Smuzhiyun static int max98373_update_status(struct sdw_slave *slave,
789*4882a593Smuzhiyun enum sdw_slave_status status)
790*4882a593Smuzhiyun {
791*4882a593Smuzhiyun struct max98373_priv *max98373 = dev_get_drvdata(&slave->dev);
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun if (status == SDW_SLAVE_UNATTACHED)
794*4882a593Smuzhiyun max98373->hw_init = false;
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /*
797*4882a593Smuzhiyun * Perform initialization only if slave status is SDW_SLAVE_ATTACHED
798*4882a593Smuzhiyun */
799*4882a593Smuzhiyun if (max98373->hw_init || status != SDW_SLAVE_ATTACHED)
800*4882a593Smuzhiyun return 0;
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun /* perform I/O transfers required for Slave initialization */
803*4882a593Smuzhiyun return max98373_io_init(slave);
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun
max98373_bus_config(struct sdw_slave * slave,struct sdw_bus_params * params)806*4882a593Smuzhiyun static int max98373_bus_config(struct sdw_slave *slave,
807*4882a593Smuzhiyun struct sdw_bus_params *params)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun int ret;
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun ret = max98373_clock_config(slave, params);
812*4882a593Smuzhiyun if (ret < 0)
813*4882a593Smuzhiyun dev_err(&slave->dev, "Invalid clk config");
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun return ret;
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun /*
819*4882a593Smuzhiyun * slave_ops: callbacks for get_clock_stop_mode, clock_stop and
820*4882a593Smuzhiyun * port_prep are not defined for now
821*4882a593Smuzhiyun */
822*4882a593Smuzhiyun static struct sdw_slave_ops max98373_slave_ops = {
823*4882a593Smuzhiyun .read_prop = max98373_read_prop,
824*4882a593Smuzhiyun .update_status = max98373_update_status,
825*4882a593Smuzhiyun .bus_config = max98373_bus_config,
826*4882a593Smuzhiyun };
827*4882a593Smuzhiyun
max98373_sdw_probe(struct sdw_slave * slave,const struct sdw_device_id * id)828*4882a593Smuzhiyun static int max98373_sdw_probe(struct sdw_slave *slave,
829*4882a593Smuzhiyun const struct sdw_device_id *id)
830*4882a593Smuzhiyun {
831*4882a593Smuzhiyun struct regmap *regmap;
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /* Regmap Initialization */
834*4882a593Smuzhiyun regmap = devm_regmap_init_sdw(slave, &max98373_sdw_regmap);
835*4882a593Smuzhiyun if (IS_ERR(regmap))
836*4882a593Smuzhiyun return PTR_ERR(regmap);
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun return max98373_init(slave, regmap);
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun #if defined(CONFIG_OF)
842*4882a593Smuzhiyun static const struct of_device_id max98373_of_match[] = {
843*4882a593Smuzhiyun { .compatible = "maxim,max98373", },
844*4882a593Smuzhiyun {},
845*4882a593Smuzhiyun };
846*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, max98373_of_match);
847*4882a593Smuzhiyun #endif
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun #ifdef CONFIG_ACPI
850*4882a593Smuzhiyun static const struct acpi_device_id max98373_acpi_match[] = {
851*4882a593Smuzhiyun { "MX98373", 0 },
852*4882a593Smuzhiyun {},
853*4882a593Smuzhiyun };
854*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, max98373_acpi_match);
855*4882a593Smuzhiyun #endif
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun static const struct sdw_device_id max98373_id[] = {
858*4882a593Smuzhiyun SDW_SLAVE_ENTRY(0x019F, 0x8373, 0),
859*4882a593Smuzhiyun {},
860*4882a593Smuzhiyun };
861*4882a593Smuzhiyun MODULE_DEVICE_TABLE(sdw, max98373_id);
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun static struct sdw_driver max98373_sdw_driver = {
864*4882a593Smuzhiyun .driver = {
865*4882a593Smuzhiyun .name = "max98373",
866*4882a593Smuzhiyun .owner = THIS_MODULE,
867*4882a593Smuzhiyun .of_match_table = of_match_ptr(max98373_of_match),
868*4882a593Smuzhiyun .acpi_match_table = ACPI_PTR(max98373_acpi_match),
869*4882a593Smuzhiyun .pm = &max98373_pm,
870*4882a593Smuzhiyun },
871*4882a593Smuzhiyun .probe = max98373_sdw_probe,
872*4882a593Smuzhiyun .remove = NULL,
873*4882a593Smuzhiyun .ops = &max98373_slave_ops,
874*4882a593Smuzhiyun .id_table = max98373_id,
875*4882a593Smuzhiyun };
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun module_sdw_driver(max98373_sdw_driver);
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun MODULE_DESCRIPTION("ASoC MAX98373 driver SDW");
880*4882a593Smuzhiyun MODULE_AUTHOR("Oleg Sherbakov <oleg.sherbakov@maximintegrated.com>");
881*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
882