xref: /OK3568_Linux_fs/kernel/sound/soc/codecs/sta32x.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Codec driver for ST STA32x 2.1-channel high-efficiency digital audio system
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright: 2011 Raumfeld GmbH
6*4882a593Smuzhiyun  * Author: Johannes Stezenbach <js@sig21.net>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * based on code from:
9*4882a593Smuzhiyun  *	Wolfson Microelectronics PLC.
10*4882a593Smuzhiyun  *	  Mark Brown <broonie@opensource.wolfsonmicro.com>
11*4882a593Smuzhiyun  *	Freescale Semiconductor, Inc.
12*4882a593Smuzhiyun  *	  Timur Tabi <timur@freescale.com>
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ":%s:%d: " fmt, __func__, __LINE__
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun #include <linux/moduleparam.h>
19*4882a593Smuzhiyun #include <linux/init.h>
20*4882a593Smuzhiyun #include <linux/clk.h>
21*4882a593Smuzhiyun #include <linux/delay.h>
22*4882a593Smuzhiyun #include <linux/pm.h>
23*4882a593Smuzhiyun #include <linux/i2c.h>
24*4882a593Smuzhiyun #include <linux/of_device.h>
25*4882a593Smuzhiyun #include <linux/of_gpio.h>
26*4882a593Smuzhiyun #include <linux/regmap.h>
27*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
28*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
29*4882a593Smuzhiyun #include <linux/slab.h>
30*4882a593Smuzhiyun #include <linux/workqueue.h>
31*4882a593Smuzhiyun #include <sound/core.h>
32*4882a593Smuzhiyun #include <sound/pcm.h>
33*4882a593Smuzhiyun #include <sound/pcm_params.h>
34*4882a593Smuzhiyun #include <sound/soc.h>
35*4882a593Smuzhiyun #include <sound/soc-dapm.h>
36*4882a593Smuzhiyun #include <sound/initval.h>
37*4882a593Smuzhiyun #include <sound/tlv.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include <sound/sta32x.h>
40*4882a593Smuzhiyun #include "sta32x.h"
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define STA32X_RATES (SNDRV_PCM_RATE_32000 | \
43*4882a593Smuzhiyun 		      SNDRV_PCM_RATE_44100 | \
44*4882a593Smuzhiyun 		      SNDRV_PCM_RATE_48000 | \
45*4882a593Smuzhiyun 		      SNDRV_PCM_RATE_88200 | \
46*4882a593Smuzhiyun 		      SNDRV_PCM_RATE_96000 | \
47*4882a593Smuzhiyun 		      SNDRV_PCM_RATE_176400 | \
48*4882a593Smuzhiyun 		      SNDRV_PCM_RATE_192000)
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #define STA32X_FORMATS \
51*4882a593Smuzhiyun 	(SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
52*4882a593Smuzhiyun 	 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
53*4882a593Smuzhiyun 	 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
54*4882a593Smuzhiyun 	 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
55*4882a593Smuzhiyun 	 SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE  | \
56*4882a593Smuzhiyun 	 SNDRV_PCM_FMTBIT_S32_LE  | SNDRV_PCM_FMTBIT_S32_BE)
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* Power-up register defaults */
59*4882a593Smuzhiyun static const struct reg_default sta32x_regs[] = {
60*4882a593Smuzhiyun 	{  0x0, 0x63 },
61*4882a593Smuzhiyun 	{  0x1, 0x80 },
62*4882a593Smuzhiyun 	{  0x2, 0xc2 },
63*4882a593Smuzhiyun 	{  0x3, 0x40 },
64*4882a593Smuzhiyun 	{  0x4, 0xc2 },
65*4882a593Smuzhiyun 	{  0x5, 0x5c },
66*4882a593Smuzhiyun 	{  0x6, 0x10 },
67*4882a593Smuzhiyun 	{  0x7, 0xff },
68*4882a593Smuzhiyun 	{  0x8, 0x60 },
69*4882a593Smuzhiyun 	{  0x9, 0x60 },
70*4882a593Smuzhiyun 	{  0xa, 0x60 },
71*4882a593Smuzhiyun 	{  0xb, 0x80 },
72*4882a593Smuzhiyun 	{  0xc, 0x00 },
73*4882a593Smuzhiyun 	{  0xd, 0x00 },
74*4882a593Smuzhiyun 	{  0xe, 0x00 },
75*4882a593Smuzhiyun 	{  0xf, 0x40 },
76*4882a593Smuzhiyun 	{ 0x10, 0x80 },
77*4882a593Smuzhiyun 	{ 0x11, 0x77 },
78*4882a593Smuzhiyun 	{ 0x12, 0x6a },
79*4882a593Smuzhiyun 	{ 0x13, 0x69 },
80*4882a593Smuzhiyun 	{ 0x14, 0x6a },
81*4882a593Smuzhiyun 	{ 0x15, 0x69 },
82*4882a593Smuzhiyun 	{ 0x16, 0x00 },
83*4882a593Smuzhiyun 	{ 0x17, 0x00 },
84*4882a593Smuzhiyun 	{ 0x18, 0x00 },
85*4882a593Smuzhiyun 	{ 0x19, 0x00 },
86*4882a593Smuzhiyun 	{ 0x1a, 0x00 },
87*4882a593Smuzhiyun 	{ 0x1b, 0x00 },
88*4882a593Smuzhiyun 	{ 0x1c, 0x00 },
89*4882a593Smuzhiyun 	{ 0x1d, 0x00 },
90*4882a593Smuzhiyun 	{ 0x1e, 0x00 },
91*4882a593Smuzhiyun 	{ 0x1f, 0x00 },
92*4882a593Smuzhiyun 	{ 0x20, 0x00 },
93*4882a593Smuzhiyun 	{ 0x21, 0x00 },
94*4882a593Smuzhiyun 	{ 0x22, 0x00 },
95*4882a593Smuzhiyun 	{ 0x23, 0x00 },
96*4882a593Smuzhiyun 	{ 0x24, 0x00 },
97*4882a593Smuzhiyun 	{ 0x25, 0x00 },
98*4882a593Smuzhiyun 	{ 0x26, 0x00 },
99*4882a593Smuzhiyun 	{ 0x27, 0x2d },
100*4882a593Smuzhiyun 	{ 0x28, 0xc0 },
101*4882a593Smuzhiyun 	{ 0x2b, 0x00 },
102*4882a593Smuzhiyun 	{ 0x2c, 0x0c },
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun static const struct regmap_range sta32x_write_regs_range[] = {
106*4882a593Smuzhiyun 	regmap_reg_range(STA32X_CONFA,  STA32X_FDRC2),
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun static const struct regmap_range sta32x_read_regs_range[] = {
110*4882a593Smuzhiyun 	regmap_reg_range(STA32X_CONFA,  STA32X_FDRC2),
111*4882a593Smuzhiyun };
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun static const struct regmap_range sta32x_volatile_regs_range[] = {
114*4882a593Smuzhiyun 	regmap_reg_range(STA32X_CFADDR2, STA32X_CFUD),
115*4882a593Smuzhiyun };
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun static const struct regmap_access_table sta32x_write_regs = {
118*4882a593Smuzhiyun 	.yes_ranges =	sta32x_write_regs_range,
119*4882a593Smuzhiyun 	.n_yes_ranges =	ARRAY_SIZE(sta32x_write_regs_range),
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun static const struct regmap_access_table sta32x_read_regs = {
123*4882a593Smuzhiyun 	.yes_ranges =	sta32x_read_regs_range,
124*4882a593Smuzhiyun 	.n_yes_ranges =	ARRAY_SIZE(sta32x_read_regs_range),
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun static const struct regmap_access_table sta32x_volatile_regs = {
128*4882a593Smuzhiyun 	.yes_ranges =	sta32x_volatile_regs_range,
129*4882a593Smuzhiyun 	.n_yes_ranges =	ARRAY_SIZE(sta32x_volatile_regs_range),
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /* regulator power supply names */
133*4882a593Smuzhiyun static const char *sta32x_supply_names[] = {
134*4882a593Smuzhiyun 	"Vdda",	/* analog supply, 3.3VV */
135*4882a593Smuzhiyun 	"Vdd3",	/* digital supply, 3.3V */
136*4882a593Smuzhiyun 	"Vcc"	/* power amp spply, 10V - 36V */
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* codec private data */
140*4882a593Smuzhiyun struct sta32x_priv {
141*4882a593Smuzhiyun 	struct regmap *regmap;
142*4882a593Smuzhiyun 	struct clk *xti_clk;
143*4882a593Smuzhiyun 	struct regulator_bulk_data supplies[ARRAY_SIZE(sta32x_supply_names)];
144*4882a593Smuzhiyun 	struct snd_soc_component *component;
145*4882a593Smuzhiyun 	struct sta32x_platform_data *pdata;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	unsigned int mclk;
148*4882a593Smuzhiyun 	unsigned int format;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	u32 coef_shadow[STA32X_COEF_COUNT];
151*4882a593Smuzhiyun 	struct delayed_work watchdog_work;
152*4882a593Smuzhiyun 	int shutdown;
153*4882a593Smuzhiyun 	struct gpio_desc *gpiod_nreset;
154*4882a593Smuzhiyun 	struct mutex coeff_lock;
155*4882a593Smuzhiyun };
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun static const DECLARE_TLV_DB_SCALE(mvol_tlv, -12700, 50, 1);
158*4882a593Smuzhiyun static const DECLARE_TLV_DB_SCALE(chvol_tlv, -7950, 50, 1);
159*4882a593Smuzhiyun static const DECLARE_TLV_DB_SCALE(tone_tlv, -120, 200, 0);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun static const char *sta32x_drc_ac[] = {
162*4882a593Smuzhiyun 	"Anti-Clipping", "Dynamic Range Compression" };
163*4882a593Smuzhiyun static const char *sta32x_auto_eq_mode[] = {
164*4882a593Smuzhiyun 	"User", "Preset", "Loudness" };
165*4882a593Smuzhiyun static const char *sta32x_auto_gc_mode[] = {
166*4882a593Smuzhiyun 	"User", "AC no clipping", "AC limited clipping (10%)",
167*4882a593Smuzhiyun 	"DRC nighttime listening mode" };
168*4882a593Smuzhiyun static const char *sta32x_auto_xo_mode[] = {
169*4882a593Smuzhiyun 	"User", "80Hz", "100Hz", "120Hz", "140Hz", "160Hz", "180Hz", "200Hz",
170*4882a593Smuzhiyun 	"220Hz", "240Hz", "260Hz", "280Hz", "300Hz", "320Hz", "340Hz", "360Hz" };
171*4882a593Smuzhiyun static const char *sta32x_preset_eq_mode[] = {
172*4882a593Smuzhiyun 	"Flat", "Rock", "Soft Rock", "Jazz", "Classical", "Dance", "Pop", "Soft",
173*4882a593Smuzhiyun 	"Hard", "Party", "Vocal", "Hip-Hop", "Dialog", "Bass-boost #1",
174*4882a593Smuzhiyun 	"Bass-boost #2", "Bass-boost #3", "Loudness 1", "Loudness 2",
175*4882a593Smuzhiyun 	"Loudness 3", "Loudness 4", "Loudness 5", "Loudness 6", "Loudness 7",
176*4882a593Smuzhiyun 	"Loudness 8", "Loudness 9", "Loudness 10", "Loudness 11", "Loudness 12",
177*4882a593Smuzhiyun 	"Loudness 13", "Loudness 14", "Loudness 15", "Loudness 16" };
178*4882a593Smuzhiyun static const char *sta32x_limiter_select[] = {
179*4882a593Smuzhiyun 	"Limiter Disabled", "Limiter #1", "Limiter #2" };
180*4882a593Smuzhiyun static const char *sta32x_limiter_attack_rate[] = {
181*4882a593Smuzhiyun 	"3.1584", "2.7072", "2.2560", "1.8048", "1.3536", "0.9024",
182*4882a593Smuzhiyun 	"0.4512", "0.2256", "0.1504", "0.1123", "0.0902", "0.0752",
183*4882a593Smuzhiyun 	"0.0645", "0.0564", "0.0501", "0.0451" };
184*4882a593Smuzhiyun static const char *sta32x_limiter_release_rate[] = {
185*4882a593Smuzhiyun 	"0.5116", "0.1370", "0.0744", "0.0499", "0.0360", "0.0299",
186*4882a593Smuzhiyun 	"0.0264", "0.0208", "0.0198", "0.0172", "0.0147", "0.0137",
187*4882a593Smuzhiyun 	"0.0134", "0.0117", "0.0110", "0.0104" };
188*4882a593Smuzhiyun static DECLARE_TLV_DB_RANGE(sta32x_limiter_ac_attack_tlv,
189*4882a593Smuzhiyun 	0, 7, TLV_DB_SCALE_ITEM(-1200, 200, 0),
190*4882a593Smuzhiyun 	8, 16, TLV_DB_SCALE_ITEM(300, 100, 0),
191*4882a593Smuzhiyun );
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun static DECLARE_TLV_DB_RANGE(sta32x_limiter_ac_release_tlv,
194*4882a593Smuzhiyun 	0, 0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
195*4882a593Smuzhiyun 	1, 1, TLV_DB_SCALE_ITEM(-2900, 0, 0),
196*4882a593Smuzhiyun 	2, 2, TLV_DB_SCALE_ITEM(-2000, 0, 0),
197*4882a593Smuzhiyun 	3, 8, TLV_DB_SCALE_ITEM(-1400, 200, 0),
198*4882a593Smuzhiyun 	8, 16, TLV_DB_SCALE_ITEM(-700, 100, 0),
199*4882a593Smuzhiyun );
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun static DECLARE_TLV_DB_RANGE(sta32x_limiter_drc_attack_tlv,
202*4882a593Smuzhiyun 	0, 7, TLV_DB_SCALE_ITEM(-3100, 200, 0),
203*4882a593Smuzhiyun 	8, 13, TLV_DB_SCALE_ITEM(-1600, 100, 0),
204*4882a593Smuzhiyun 	14, 16, TLV_DB_SCALE_ITEM(-1000, 300, 0),
205*4882a593Smuzhiyun );
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun static DECLARE_TLV_DB_RANGE(sta32x_limiter_drc_release_tlv,
208*4882a593Smuzhiyun 	0, 0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
209*4882a593Smuzhiyun 	1, 2, TLV_DB_SCALE_ITEM(-3800, 200, 0),
210*4882a593Smuzhiyun 	3, 4, TLV_DB_SCALE_ITEM(-3300, 200, 0),
211*4882a593Smuzhiyun 	5, 12, TLV_DB_SCALE_ITEM(-3000, 200, 0),
212*4882a593Smuzhiyun 	13, 16, TLV_DB_SCALE_ITEM(-1500, 300, 0),
213*4882a593Smuzhiyun );
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_drc_ac_enum,
216*4882a593Smuzhiyun 			    STA32X_CONFD, STA32X_CONFD_DRC_SHIFT,
217*4882a593Smuzhiyun 			    sta32x_drc_ac);
218*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_auto_eq_enum,
219*4882a593Smuzhiyun 			    STA32X_AUTO1, STA32X_AUTO1_AMEQ_SHIFT,
220*4882a593Smuzhiyun 			    sta32x_auto_eq_mode);
221*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_auto_gc_enum,
222*4882a593Smuzhiyun 			    STA32X_AUTO1, STA32X_AUTO1_AMGC_SHIFT,
223*4882a593Smuzhiyun 			    sta32x_auto_gc_mode);
224*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_auto_xo_enum,
225*4882a593Smuzhiyun 			    STA32X_AUTO2, STA32X_AUTO2_XO_SHIFT,
226*4882a593Smuzhiyun 			    sta32x_auto_xo_mode);
227*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_preset_eq_enum,
228*4882a593Smuzhiyun 			    STA32X_AUTO3, STA32X_AUTO3_PEQ_SHIFT,
229*4882a593Smuzhiyun 			    sta32x_preset_eq_mode);
230*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_limiter_ch1_enum,
231*4882a593Smuzhiyun 			    STA32X_C1CFG, STA32X_CxCFG_LS_SHIFT,
232*4882a593Smuzhiyun 			    sta32x_limiter_select);
233*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_limiter_ch2_enum,
234*4882a593Smuzhiyun 			    STA32X_C2CFG, STA32X_CxCFG_LS_SHIFT,
235*4882a593Smuzhiyun 			    sta32x_limiter_select);
236*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_limiter_ch3_enum,
237*4882a593Smuzhiyun 			    STA32X_C3CFG, STA32X_CxCFG_LS_SHIFT,
238*4882a593Smuzhiyun 			    sta32x_limiter_select);
239*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_limiter1_attack_rate_enum,
240*4882a593Smuzhiyun 			    STA32X_L1AR, STA32X_LxA_SHIFT,
241*4882a593Smuzhiyun 			    sta32x_limiter_attack_rate);
242*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_limiter2_attack_rate_enum,
243*4882a593Smuzhiyun 			    STA32X_L2AR, STA32X_LxA_SHIFT,
244*4882a593Smuzhiyun 			    sta32x_limiter_attack_rate);
245*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_limiter1_release_rate_enum,
246*4882a593Smuzhiyun 			    STA32X_L1AR, STA32X_LxR_SHIFT,
247*4882a593Smuzhiyun 			    sta32x_limiter_release_rate);
248*4882a593Smuzhiyun static SOC_ENUM_SINGLE_DECL(sta32x_limiter2_release_rate_enum,
249*4882a593Smuzhiyun 			    STA32X_L2AR, STA32X_LxR_SHIFT,
250*4882a593Smuzhiyun 			    sta32x_limiter_release_rate);
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun /* byte array controls for setting biquad, mixer, scaling coefficients;
253*4882a593Smuzhiyun  * for biquads all five coefficients need to be set in one go,
254*4882a593Smuzhiyun  * mixer and pre/postscale coefs can be set individually;
255*4882a593Smuzhiyun  * each coef is 24bit, the bytes are ordered in the same way
256*4882a593Smuzhiyun  * as given in the STA32x data sheet (big endian; b1, b2, a1, a2, b0)
257*4882a593Smuzhiyun  */
258*4882a593Smuzhiyun 
sta32x_coefficient_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)259*4882a593Smuzhiyun static int sta32x_coefficient_info(struct snd_kcontrol *kcontrol,
260*4882a593Smuzhiyun 				   struct snd_ctl_elem_info *uinfo)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	int numcoef = kcontrol->private_value >> 16;
263*4882a593Smuzhiyun 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
264*4882a593Smuzhiyun 	uinfo->count = 3 * numcoef;
265*4882a593Smuzhiyun 	return 0;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
sta32x_coefficient_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)268*4882a593Smuzhiyun static int sta32x_coefficient_get(struct snd_kcontrol *kcontrol,
269*4882a593Smuzhiyun 				  struct snd_ctl_elem_value *ucontrol)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
272*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
273*4882a593Smuzhiyun 	int numcoef = kcontrol->private_value >> 16;
274*4882a593Smuzhiyun 	int index = kcontrol->private_value & 0xffff;
275*4882a593Smuzhiyun 	unsigned int cfud, val;
276*4882a593Smuzhiyun 	int i, ret = 0;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	mutex_lock(&sta32x->coeff_lock);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	/* preserve reserved bits in STA32X_CFUD */
281*4882a593Smuzhiyun 	regmap_read(sta32x->regmap, STA32X_CFUD, &cfud);
282*4882a593Smuzhiyun 	cfud &= 0xf0;
283*4882a593Smuzhiyun 	/*
284*4882a593Smuzhiyun 	 * chip documentation does not say if the bits are self clearing,
285*4882a593Smuzhiyun 	 * so do it explicitly
286*4882a593Smuzhiyun 	 */
287*4882a593Smuzhiyun 	regmap_write(sta32x->regmap, STA32X_CFUD, cfud);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	regmap_write(sta32x->regmap, STA32X_CFADDR2, index);
290*4882a593Smuzhiyun 	if (numcoef == 1) {
291*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_CFUD, cfud | 0x04);
292*4882a593Smuzhiyun 	} else if (numcoef == 5) {
293*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_CFUD, cfud | 0x08);
294*4882a593Smuzhiyun 	} else {
295*4882a593Smuzhiyun 		ret = -EINVAL;
296*4882a593Smuzhiyun 		goto exit_unlock;
297*4882a593Smuzhiyun 	}
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	for (i = 0; i < 3 * numcoef; i++) {
300*4882a593Smuzhiyun 		regmap_read(sta32x->regmap, STA32X_B1CF1 + i, &val);
301*4882a593Smuzhiyun 		ucontrol->value.bytes.data[i] = val;
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun exit_unlock:
305*4882a593Smuzhiyun 	mutex_unlock(&sta32x->coeff_lock);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	return ret;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun 
sta32x_coefficient_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)310*4882a593Smuzhiyun static int sta32x_coefficient_put(struct snd_kcontrol *kcontrol,
311*4882a593Smuzhiyun 				  struct snd_ctl_elem_value *ucontrol)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
314*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
315*4882a593Smuzhiyun 	int numcoef = kcontrol->private_value >> 16;
316*4882a593Smuzhiyun 	int index = kcontrol->private_value & 0xffff;
317*4882a593Smuzhiyun 	unsigned int cfud;
318*4882a593Smuzhiyun 	int i;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	/* preserve reserved bits in STA32X_CFUD */
321*4882a593Smuzhiyun 	regmap_read(sta32x->regmap, STA32X_CFUD, &cfud);
322*4882a593Smuzhiyun 	cfud &= 0xf0;
323*4882a593Smuzhiyun 	/*
324*4882a593Smuzhiyun 	 * chip documentation does not say if the bits are self clearing,
325*4882a593Smuzhiyun 	 * so do it explicitly
326*4882a593Smuzhiyun 	 */
327*4882a593Smuzhiyun 	regmap_write(sta32x->regmap, STA32X_CFUD, cfud);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	regmap_write(sta32x->regmap, STA32X_CFADDR2, index);
330*4882a593Smuzhiyun 	for (i = 0; i < numcoef && (index + i < STA32X_COEF_COUNT); i++)
331*4882a593Smuzhiyun 		sta32x->coef_shadow[index + i] =
332*4882a593Smuzhiyun 			  (ucontrol->value.bytes.data[3 * i] << 16)
333*4882a593Smuzhiyun 			| (ucontrol->value.bytes.data[3 * i + 1] << 8)
334*4882a593Smuzhiyun 			| (ucontrol->value.bytes.data[3 * i + 2]);
335*4882a593Smuzhiyun 	for (i = 0; i < 3 * numcoef; i++)
336*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_B1CF1 + i,
337*4882a593Smuzhiyun 			     ucontrol->value.bytes.data[i]);
338*4882a593Smuzhiyun 	if (numcoef == 1)
339*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_CFUD, cfud | 0x01);
340*4882a593Smuzhiyun 	else if (numcoef == 5)
341*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_CFUD, cfud | 0x02);
342*4882a593Smuzhiyun 	else
343*4882a593Smuzhiyun 		return -EINVAL;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	return 0;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun 
sta32x_sync_coef_shadow(struct snd_soc_component * component)348*4882a593Smuzhiyun static int sta32x_sync_coef_shadow(struct snd_soc_component *component)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
351*4882a593Smuzhiyun 	unsigned int cfud;
352*4882a593Smuzhiyun 	int i;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/* preserve reserved bits in STA32X_CFUD */
355*4882a593Smuzhiyun 	regmap_read(sta32x->regmap, STA32X_CFUD, &cfud);
356*4882a593Smuzhiyun 	cfud &= 0xf0;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	for (i = 0; i < STA32X_COEF_COUNT; i++) {
359*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_CFADDR2, i);
360*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_B1CF1,
361*4882a593Smuzhiyun 			     (sta32x->coef_shadow[i] >> 16) & 0xff);
362*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_B1CF2,
363*4882a593Smuzhiyun 			     (sta32x->coef_shadow[i] >> 8) & 0xff);
364*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_B1CF3,
365*4882a593Smuzhiyun 			     (sta32x->coef_shadow[i]) & 0xff);
366*4882a593Smuzhiyun 		/*
367*4882a593Smuzhiyun 		 * chip documentation does not say if the bits are
368*4882a593Smuzhiyun 		 * self-clearing, so do it explicitly
369*4882a593Smuzhiyun 		 */
370*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_CFUD, cfud);
371*4882a593Smuzhiyun 		regmap_write(sta32x->regmap, STA32X_CFUD, cfud | 0x01);
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 	return 0;
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun 
sta32x_cache_sync(struct snd_soc_component * component)376*4882a593Smuzhiyun static int sta32x_cache_sync(struct snd_soc_component *component)
377*4882a593Smuzhiyun {
378*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
379*4882a593Smuzhiyun 	unsigned int mute;
380*4882a593Smuzhiyun 	int rc;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	/* mute during register sync */
383*4882a593Smuzhiyun 	regmap_read(sta32x->regmap, STA32X_MMUTE, &mute);
384*4882a593Smuzhiyun 	regmap_write(sta32x->regmap, STA32X_MMUTE, mute | STA32X_MMUTE_MMUTE);
385*4882a593Smuzhiyun 	sta32x_sync_coef_shadow(component);
386*4882a593Smuzhiyun 	rc = regcache_sync(sta32x->regmap);
387*4882a593Smuzhiyun 	regmap_write(sta32x->regmap, STA32X_MMUTE, mute);
388*4882a593Smuzhiyun 	return rc;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun /* work around ESD issue where sta32x resets and loses all configuration */
sta32x_watchdog(struct work_struct * work)392*4882a593Smuzhiyun static void sta32x_watchdog(struct work_struct *work)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = container_of(work, struct sta32x_priv,
395*4882a593Smuzhiyun 						  watchdog_work.work);
396*4882a593Smuzhiyun 	struct snd_soc_component *component = sta32x->component;
397*4882a593Smuzhiyun 	unsigned int confa, confa_cached;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/* check if sta32x has reset itself */
400*4882a593Smuzhiyun 	confa_cached = snd_soc_component_read(component, STA32X_CONFA);
401*4882a593Smuzhiyun 	regcache_cache_bypass(sta32x->regmap, true);
402*4882a593Smuzhiyun 	confa = snd_soc_component_read(component, STA32X_CONFA);
403*4882a593Smuzhiyun 	regcache_cache_bypass(sta32x->regmap, false);
404*4882a593Smuzhiyun 	if (confa != confa_cached) {
405*4882a593Smuzhiyun 		regcache_mark_dirty(sta32x->regmap);
406*4882a593Smuzhiyun 		sta32x_cache_sync(component);
407*4882a593Smuzhiyun 	}
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	if (!sta32x->shutdown)
410*4882a593Smuzhiyun 		queue_delayed_work(system_power_efficient_wq,
411*4882a593Smuzhiyun 				   &sta32x->watchdog_work,
412*4882a593Smuzhiyun 				   round_jiffies_relative(HZ));
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
sta32x_watchdog_start(struct sta32x_priv * sta32x)415*4882a593Smuzhiyun static void sta32x_watchdog_start(struct sta32x_priv *sta32x)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	if (sta32x->pdata->needs_esd_watchdog) {
418*4882a593Smuzhiyun 		sta32x->shutdown = 0;
419*4882a593Smuzhiyun 		queue_delayed_work(system_power_efficient_wq,
420*4882a593Smuzhiyun 				   &sta32x->watchdog_work,
421*4882a593Smuzhiyun 				   round_jiffies_relative(HZ));
422*4882a593Smuzhiyun 	}
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
sta32x_watchdog_stop(struct sta32x_priv * sta32x)425*4882a593Smuzhiyun static void sta32x_watchdog_stop(struct sta32x_priv *sta32x)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	if (sta32x->pdata->needs_esd_watchdog) {
428*4882a593Smuzhiyun 		sta32x->shutdown = 1;
429*4882a593Smuzhiyun 		cancel_delayed_work_sync(&sta32x->watchdog_work);
430*4882a593Smuzhiyun 	}
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun #define SINGLE_COEF(xname, index) \
434*4882a593Smuzhiyun {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
435*4882a593Smuzhiyun 	.info = sta32x_coefficient_info, \
436*4882a593Smuzhiyun 	.get = sta32x_coefficient_get,\
437*4882a593Smuzhiyun 	.put = sta32x_coefficient_put, \
438*4882a593Smuzhiyun 	.private_value = index | (1 << 16) }
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun #define BIQUAD_COEFS(xname, index) \
441*4882a593Smuzhiyun {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
442*4882a593Smuzhiyun 	.info = sta32x_coefficient_info, \
443*4882a593Smuzhiyun 	.get = sta32x_coefficient_get,\
444*4882a593Smuzhiyun 	.put = sta32x_coefficient_put, \
445*4882a593Smuzhiyun 	.private_value = index | (5 << 16) }
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun static const struct snd_kcontrol_new sta32x_snd_controls[] = {
448*4882a593Smuzhiyun SOC_SINGLE_TLV("Master Volume", STA32X_MVOL, 0, 0xff, 1, mvol_tlv),
449*4882a593Smuzhiyun SOC_SINGLE("Master Switch", STA32X_MMUTE, 0, 1, 1),
450*4882a593Smuzhiyun SOC_SINGLE("Ch1 Switch", STA32X_MMUTE, 1, 1, 1),
451*4882a593Smuzhiyun SOC_SINGLE("Ch2 Switch", STA32X_MMUTE, 2, 1, 1),
452*4882a593Smuzhiyun SOC_SINGLE("Ch3 Switch", STA32X_MMUTE, 3, 1, 1),
453*4882a593Smuzhiyun SOC_SINGLE_TLV("Ch1 Volume", STA32X_C1VOL, 0, 0xff, 1, chvol_tlv),
454*4882a593Smuzhiyun SOC_SINGLE_TLV("Ch2 Volume", STA32X_C2VOL, 0, 0xff, 1, chvol_tlv),
455*4882a593Smuzhiyun SOC_SINGLE_TLV("Ch3 Volume", STA32X_C3VOL, 0, 0xff, 1, chvol_tlv),
456*4882a593Smuzhiyun SOC_SINGLE("De-emphasis Filter Switch", STA32X_CONFD, STA32X_CONFD_DEMP_SHIFT, 1, 0),
457*4882a593Smuzhiyun SOC_ENUM("Compressor/Limiter Switch", sta32x_drc_ac_enum),
458*4882a593Smuzhiyun SOC_SINGLE("Miami Mode Switch", STA32X_CONFD, STA32X_CONFD_MME_SHIFT, 1, 0),
459*4882a593Smuzhiyun SOC_SINGLE("Zero Cross Switch", STA32X_CONFE, STA32X_CONFE_ZCE_SHIFT, 1, 0),
460*4882a593Smuzhiyun SOC_SINGLE("Soft Ramp Switch", STA32X_CONFE, STA32X_CONFE_SVE_SHIFT, 1, 0),
461*4882a593Smuzhiyun SOC_SINGLE("Auto-Mute Switch", STA32X_CONFF, STA32X_CONFF_IDE_SHIFT, 1, 0),
462*4882a593Smuzhiyun SOC_ENUM("Automode EQ", sta32x_auto_eq_enum),
463*4882a593Smuzhiyun SOC_ENUM("Automode GC", sta32x_auto_gc_enum),
464*4882a593Smuzhiyun SOC_ENUM("Automode XO", sta32x_auto_xo_enum),
465*4882a593Smuzhiyun SOC_ENUM("Preset EQ", sta32x_preset_eq_enum),
466*4882a593Smuzhiyun SOC_SINGLE("Ch1 Tone Control Bypass Switch", STA32X_C1CFG, STA32X_CxCFG_TCB_SHIFT, 1, 0),
467*4882a593Smuzhiyun SOC_SINGLE("Ch2 Tone Control Bypass Switch", STA32X_C2CFG, STA32X_CxCFG_TCB_SHIFT, 1, 0),
468*4882a593Smuzhiyun SOC_SINGLE("Ch1 EQ Bypass Switch", STA32X_C1CFG, STA32X_CxCFG_EQBP_SHIFT, 1, 0),
469*4882a593Smuzhiyun SOC_SINGLE("Ch2 EQ Bypass Switch", STA32X_C2CFG, STA32X_CxCFG_EQBP_SHIFT, 1, 0),
470*4882a593Smuzhiyun SOC_SINGLE("Ch1 Master Volume Bypass Switch", STA32X_C1CFG, STA32X_CxCFG_VBP_SHIFT, 1, 0),
471*4882a593Smuzhiyun SOC_SINGLE("Ch2 Master Volume Bypass Switch", STA32X_C1CFG, STA32X_CxCFG_VBP_SHIFT, 1, 0),
472*4882a593Smuzhiyun SOC_SINGLE("Ch3 Master Volume Bypass Switch", STA32X_C1CFG, STA32X_CxCFG_VBP_SHIFT, 1, 0),
473*4882a593Smuzhiyun SOC_ENUM("Ch1 Limiter Select", sta32x_limiter_ch1_enum),
474*4882a593Smuzhiyun SOC_ENUM("Ch2 Limiter Select", sta32x_limiter_ch2_enum),
475*4882a593Smuzhiyun SOC_ENUM("Ch3 Limiter Select", sta32x_limiter_ch3_enum),
476*4882a593Smuzhiyun SOC_SINGLE_TLV("Bass Tone Control", STA32X_TONE, STA32X_TONE_BTC_SHIFT, 15, 0, tone_tlv),
477*4882a593Smuzhiyun SOC_SINGLE_TLV("Treble Tone Control", STA32X_TONE, STA32X_TONE_TTC_SHIFT, 15, 0, tone_tlv),
478*4882a593Smuzhiyun SOC_ENUM("Limiter1 Attack Rate (dB/ms)", sta32x_limiter1_attack_rate_enum),
479*4882a593Smuzhiyun SOC_ENUM("Limiter2 Attack Rate (dB/ms)", sta32x_limiter2_attack_rate_enum),
480*4882a593Smuzhiyun SOC_ENUM("Limiter1 Release Rate (dB/ms)", sta32x_limiter1_release_rate_enum),
481*4882a593Smuzhiyun SOC_ENUM("Limiter2 Release Rate (dB/ms)", sta32x_limiter2_release_rate_enum),
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun /* depending on mode, the attack/release thresholds have
484*4882a593Smuzhiyun  * two different enum definitions; provide both
485*4882a593Smuzhiyun  */
486*4882a593Smuzhiyun SOC_SINGLE_TLV("Limiter1 Attack Threshold (AC Mode)", STA32X_L1ATRT, STA32X_LxA_SHIFT,
487*4882a593Smuzhiyun 	       16, 0, sta32x_limiter_ac_attack_tlv),
488*4882a593Smuzhiyun SOC_SINGLE_TLV("Limiter2 Attack Threshold (AC Mode)", STA32X_L2ATRT, STA32X_LxA_SHIFT,
489*4882a593Smuzhiyun 	       16, 0, sta32x_limiter_ac_attack_tlv),
490*4882a593Smuzhiyun SOC_SINGLE_TLV("Limiter1 Release Threshold (AC Mode)", STA32X_L1ATRT, STA32X_LxR_SHIFT,
491*4882a593Smuzhiyun 	       16, 0, sta32x_limiter_ac_release_tlv),
492*4882a593Smuzhiyun SOC_SINGLE_TLV("Limiter2 Release Threshold (AC Mode)", STA32X_L2ATRT, STA32X_LxR_SHIFT,
493*4882a593Smuzhiyun 	       16, 0, sta32x_limiter_ac_release_tlv),
494*4882a593Smuzhiyun SOC_SINGLE_TLV("Limiter1 Attack Threshold (DRC Mode)", STA32X_L1ATRT, STA32X_LxA_SHIFT,
495*4882a593Smuzhiyun 	       16, 0, sta32x_limiter_drc_attack_tlv),
496*4882a593Smuzhiyun SOC_SINGLE_TLV("Limiter2 Attack Threshold (DRC Mode)", STA32X_L2ATRT, STA32X_LxA_SHIFT,
497*4882a593Smuzhiyun 	       16, 0, sta32x_limiter_drc_attack_tlv),
498*4882a593Smuzhiyun SOC_SINGLE_TLV("Limiter1 Release Threshold (DRC Mode)", STA32X_L1ATRT, STA32X_LxR_SHIFT,
499*4882a593Smuzhiyun 	       16, 0, sta32x_limiter_drc_release_tlv),
500*4882a593Smuzhiyun SOC_SINGLE_TLV("Limiter2 Release Threshold (DRC Mode)", STA32X_L2ATRT, STA32X_LxR_SHIFT,
501*4882a593Smuzhiyun 	       16, 0, sta32x_limiter_drc_release_tlv),
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun BIQUAD_COEFS("Ch1 - Biquad 1", 0),
504*4882a593Smuzhiyun BIQUAD_COEFS("Ch1 - Biquad 2", 5),
505*4882a593Smuzhiyun BIQUAD_COEFS("Ch1 - Biquad 3", 10),
506*4882a593Smuzhiyun BIQUAD_COEFS("Ch1 - Biquad 4", 15),
507*4882a593Smuzhiyun BIQUAD_COEFS("Ch2 - Biquad 1", 20),
508*4882a593Smuzhiyun BIQUAD_COEFS("Ch2 - Biquad 2", 25),
509*4882a593Smuzhiyun BIQUAD_COEFS("Ch2 - Biquad 3", 30),
510*4882a593Smuzhiyun BIQUAD_COEFS("Ch2 - Biquad 4", 35),
511*4882a593Smuzhiyun BIQUAD_COEFS("High-pass", 40),
512*4882a593Smuzhiyun BIQUAD_COEFS("Low-pass", 45),
513*4882a593Smuzhiyun SINGLE_COEF("Ch1 - Prescale", 50),
514*4882a593Smuzhiyun SINGLE_COEF("Ch2 - Prescale", 51),
515*4882a593Smuzhiyun SINGLE_COEF("Ch1 - Postscale", 52),
516*4882a593Smuzhiyun SINGLE_COEF("Ch2 - Postscale", 53),
517*4882a593Smuzhiyun SINGLE_COEF("Ch3 - Postscale", 54),
518*4882a593Smuzhiyun SINGLE_COEF("Thermal warning - Postscale", 55),
519*4882a593Smuzhiyun SINGLE_COEF("Ch1 - Mix 1", 56),
520*4882a593Smuzhiyun SINGLE_COEF("Ch1 - Mix 2", 57),
521*4882a593Smuzhiyun SINGLE_COEF("Ch2 - Mix 1", 58),
522*4882a593Smuzhiyun SINGLE_COEF("Ch2 - Mix 2", 59),
523*4882a593Smuzhiyun SINGLE_COEF("Ch3 - Mix 1", 60),
524*4882a593Smuzhiyun SINGLE_COEF("Ch3 - Mix 2", 61),
525*4882a593Smuzhiyun };
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun static const struct snd_soc_dapm_widget sta32x_dapm_widgets[] = {
528*4882a593Smuzhiyun SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
529*4882a593Smuzhiyun SND_SOC_DAPM_OUTPUT("LEFT"),
530*4882a593Smuzhiyun SND_SOC_DAPM_OUTPUT("RIGHT"),
531*4882a593Smuzhiyun SND_SOC_DAPM_OUTPUT("SUB"),
532*4882a593Smuzhiyun };
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun static const struct snd_soc_dapm_route sta32x_dapm_routes[] = {
535*4882a593Smuzhiyun 	{ "LEFT", NULL, "DAC" },
536*4882a593Smuzhiyun 	{ "RIGHT", NULL, "DAC" },
537*4882a593Smuzhiyun 	{ "SUB", NULL, "DAC" },
538*4882a593Smuzhiyun };
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun /* MCLK interpolation ratio per fs */
541*4882a593Smuzhiyun static struct {
542*4882a593Smuzhiyun 	int fs;
543*4882a593Smuzhiyun 	int ir;
544*4882a593Smuzhiyun } interpolation_ratios[] = {
545*4882a593Smuzhiyun 	{ 32000, 0 },
546*4882a593Smuzhiyun 	{ 44100, 0 },
547*4882a593Smuzhiyun 	{ 48000, 0 },
548*4882a593Smuzhiyun 	{ 88200, 1 },
549*4882a593Smuzhiyun 	{ 96000, 1 },
550*4882a593Smuzhiyun 	{ 176400, 2 },
551*4882a593Smuzhiyun 	{ 192000, 2 },
552*4882a593Smuzhiyun };
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun /* MCLK to fs clock ratios */
555*4882a593Smuzhiyun static int mcs_ratio_table[3][7] = {
556*4882a593Smuzhiyun 	{ 768, 512, 384, 256, 128, 576, 0 },
557*4882a593Smuzhiyun 	{ 384, 256, 192, 128,  64,   0 },
558*4882a593Smuzhiyun 	{ 384, 256, 192, 128,  64,   0 },
559*4882a593Smuzhiyun };
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun /**
562*4882a593Smuzhiyun  * sta32x_set_dai_sysclk - configure MCLK
563*4882a593Smuzhiyun  * @codec_dai: the codec DAI
564*4882a593Smuzhiyun  * @clk_id: the clock ID (ignored)
565*4882a593Smuzhiyun  * @freq: the MCLK input frequency
566*4882a593Smuzhiyun  * @dir: the clock direction (ignored)
567*4882a593Smuzhiyun  *
568*4882a593Smuzhiyun  * The value of MCLK is used to determine which sample rates are supported
569*4882a593Smuzhiyun  * by the STA32X, based on the mclk_ratios table.
570*4882a593Smuzhiyun  *
571*4882a593Smuzhiyun  * This function must be called by the machine driver's 'startup' function,
572*4882a593Smuzhiyun  * otherwise the list of supported sample rates will not be available in
573*4882a593Smuzhiyun  * time for ALSA.
574*4882a593Smuzhiyun  *
575*4882a593Smuzhiyun  * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
576*4882a593Smuzhiyun  * theoretically possible sample rates to be enabled. Call it again with a
577*4882a593Smuzhiyun  * proper value set one the external clock is set (most probably you would do
578*4882a593Smuzhiyun  * that from a machine's driver 'hw_param' hook.
579*4882a593Smuzhiyun  */
sta32x_set_dai_sysclk(struct snd_soc_dai * codec_dai,int clk_id,unsigned int freq,int dir)580*4882a593Smuzhiyun static int sta32x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
581*4882a593Smuzhiyun 		int clk_id, unsigned int freq, int dir)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	struct snd_soc_component *component = codec_dai->component;
584*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	dev_dbg(component->dev, "mclk=%u\n", freq);
587*4882a593Smuzhiyun 	sta32x->mclk = freq;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	return 0;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun /**
593*4882a593Smuzhiyun  * sta32x_set_dai_fmt - configure the codec for the selected audio format
594*4882a593Smuzhiyun  * @codec_dai: the codec DAI
595*4882a593Smuzhiyun  * @fmt: a SND_SOC_DAIFMT_x value indicating the data format
596*4882a593Smuzhiyun  *
597*4882a593Smuzhiyun  * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
598*4882a593Smuzhiyun  * codec accordingly.
599*4882a593Smuzhiyun  */
sta32x_set_dai_fmt(struct snd_soc_dai * codec_dai,unsigned int fmt)600*4882a593Smuzhiyun static int sta32x_set_dai_fmt(struct snd_soc_dai *codec_dai,
601*4882a593Smuzhiyun 			      unsigned int fmt)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun 	struct snd_soc_component *component = codec_dai->component;
604*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
605*4882a593Smuzhiyun 	u8 confb = 0;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
608*4882a593Smuzhiyun 	case SND_SOC_DAIFMT_CBS_CFS:
609*4882a593Smuzhiyun 		break;
610*4882a593Smuzhiyun 	default:
611*4882a593Smuzhiyun 		return -EINVAL;
612*4882a593Smuzhiyun 	}
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
615*4882a593Smuzhiyun 	case SND_SOC_DAIFMT_I2S:
616*4882a593Smuzhiyun 	case SND_SOC_DAIFMT_RIGHT_J:
617*4882a593Smuzhiyun 	case SND_SOC_DAIFMT_LEFT_J:
618*4882a593Smuzhiyun 		sta32x->format = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
619*4882a593Smuzhiyun 		break;
620*4882a593Smuzhiyun 	default:
621*4882a593Smuzhiyun 		return -EINVAL;
622*4882a593Smuzhiyun 	}
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
625*4882a593Smuzhiyun 	case SND_SOC_DAIFMT_NB_NF:
626*4882a593Smuzhiyun 		confb |= STA32X_CONFB_C2IM;
627*4882a593Smuzhiyun 		break;
628*4882a593Smuzhiyun 	case SND_SOC_DAIFMT_NB_IF:
629*4882a593Smuzhiyun 		confb |= STA32X_CONFB_C1IM;
630*4882a593Smuzhiyun 		break;
631*4882a593Smuzhiyun 	default:
632*4882a593Smuzhiyun 		return -EINVAL;
633*4882a593Smuzhiyun 	}
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	return regmap_update_bits(sta32x->regmap, STA32X_CONFB,
636*4882a593Smuzhiyun 				  STA32X_CONFB_C1IM | STA32X_CONFB_C2IM, confb);
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun /**
640*4882a593Smuzhiyun  * sta32x_hw_params - program the STA32X with the given hardware parameters.
641*4882a593Smuzhiyun  * @substream: the audio stream
642*4882a593Smuzhiyun  * @params: the hardware parameters to set
643*4882a593Smuzhiyun  * @dai: the SOC DAI (ignored)
644*4882a593Smuzhiyun  *
645*4882a593Smuzhiyun  * This function programs the hardware with the values provided.
646*4882a593Smuzhiyun  * Specifically, the sample rate and the data format.
647*4882a593Smuzhiyun  */
sta32x_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)648*4882a593Smuzhiyun static int sta32x_hw_params(struct snd_pcm_substream *substream,
649*4882a593Smuzhiyun 			    struct snd_pcm_hw_params *params,
650*4882a593Smuzhiyun 			    struct snd_soc_dai *dai)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun 	struct snd_soc_component *component = dai->component;
653*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
654*4882a593Smuzhiyun 	int i, mcs = -EINVAL, ir = -EINVAL;
655*4882a593Smuzhiyun 	unsigned int confa, confb;
656*4882a593Smuzhiyun 	unsigned int rate, ratio;
657*4882a593Smuzhiyun 	int ret;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	if (!sta32x->mclk) {
660*4882a593Smuzhiyun 		dev_err(component->dev,
661*4882a593Smuzhiyun 			"sta32x->mclk is unset. Unable to determine ratio\n");
662*4882a593Smuzhiyun 		return -EIO;
663*4882a593Smuzhiyun 	}
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	rate = params_rate(params);
666*4882a593Smuzhiyun 	ratio = sta32x->mclk / rate;
667*4882a593Smuzhiyun 	dev_dbg(component->dev, "rate: %u, ratio: %u\n", rate, ratio);
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(interpolation_ratios); i++) {
670*4882a593Smuzhiyun 		if (interpolation_ratios[i].fs == rate) {
671*4882a593Smuzhiyun 			ir = interpolation_ratios[i].ir;
672*4882a593Smuzhiyun 			break;
673*4882a593Smuzhiyun 		}
674*4882a593Smuzhiyun 	}
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	if (ir < 0) {
677*4882a593Smuzhiyun 		dev_err(component->dev, "Unsupported samplerate: %u\n", rate);
678*4882a593Smuzhiyun 		return -EINVAL;
679*4882a593Smuzhiyun 	}
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	for (i = 0; i < 6; i++) {
682*4882a593Smuzhiyun 		if (mcs_ratio_table[ir][i] == ratio) {
683*4882a593Smuzhiyun 			mcs = i;
684*4882a593Smuzhiyun 			break;
685*4882a593Smuzhiyun 		}
686*4882a593Smuzhiyun 	}
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	if (mcs < 0) {
689*4882a593Smuzhiyun 		dev_err(component->dev, "Unresolvable ratio: %u\n", ratio);
690*4882a593Smuzhiyun 		return -EINVAL;
691*4882a593Smuzhiyun 	}
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	confa = (ir << STA32X_CONFA_IR_SHIFT) |
694*4882a593Smuzhiyun 		(mcs << STA32X_CONFA_MCS_SHIFT);
695*4882a593Smuzhiyun 	confb = 0;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	switch (params_width(params)) {
698*4882a593Smuzhiyun 	case 24:
699*4882a593Smuzhiyun 		dev_dbg(component->dev, "24bit\n");
700*4882a593Smuzhiyun 		fallthrough;
701*4882a593Smuzhiyun 	case 32:
702*4882a593Smuzhiyun 		dev_dbg(component->dev, "24bit or 32bit\n");
703*4882a593Smuzhiyun 		switch (sta32x->format) {
704*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_I2S:
705*4882a593Smuzhiyun 			confb |= 0x0;
706*4882a593Smuzhiyun 			break;
707*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_LEFT_J:
708*4882a593Smuzhiyun 			confb |= 0x1;
709*4882a593Smuzhiyun 			break;
710*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_RIGHT_J:
711*4882a593Smuzhiyun 			confb |= 0x2;
712*4882a593Smuzhiyun 			break;
713*4882a593Smuzhiyun 		}
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 		break;
716*4882a593Smuzhiyun 	case 20:
717*4882a593Smuzhiyun 		dev_dbg(component->dev, "20bit\n");
718*4882a593Smuzhiyun 		switch (sta32x->format) {
719*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_I2S:
720*4882a593Smuzhiyun 			confb |= 0x4;
721*4882a593Smuzhiyun 			break;
722*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_LEFT_J:
723*4882a593Smuzhiyun 			confb |= 0x5;
724*4882a593Smuzhiyun 			break;
725*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_RIGHT_J:
726*4882a593Smuzhiyun 			confb |= 0x6;
727*4882a593Smuzhiyun 			break;
728*4882a593Smuzhiyun 		}
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 		break;
731*4882a593Smuzhiyun 	case 18:
732*4882a593Smuzhiyun 		dev_dbg(component->dev, "18bit\n");
733*4882a593Smuzhiyun 		switch (sta32x->format) {
734*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_I2S:
735*4882a593Smuzhiyun 			confb |= 0x8;
736*4882a593Smuzhiyun 			break;
737*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_LEFT_J:
738*4882a593Smuzhiyun 			confb |= 0x9;
739*4882a593Smuzhiyun 			break;
740*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_RIGHT_J:
741*4882a593Smuzhiyun 			confb |= 0xa;
742*4882a593Smuzhiyun 			break;
743*4882a593Smuzhiyun 		}
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 		break;
746*4882a593Smuzhiyun 	case 16:
747*4882a593Smuzhiyun 		dev_dbg(component->dev, "16bit\n");
748*4882a593Smuzhiyun 		switch (sta32x->format) {
749*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_I2S:
750*4882a593Smuzhiyun 			confb |= 0x0;
751*4882a593Smuzhiyun 			break;
752*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_LEFT_J:
753*4882a593Smuzhiyun 			confb |= 0xd;
754*4882a593Smuzhiyun 			break;
755*4882a593Smuzhiyun 		case SND_SOC_DAIFMT_RIGHT_J:
756*4882a593Smuzhiyun 			confb |= 0xe;
757*4882a593Smuzhiyun 			break;
758*4882a593Smuzhiyun 		}
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 		break;
761*4882a593Smuzhiyun 	default:
762*4882a593Smuzhiyun 		return -EINVAL;
763*4882a593Smuzhiyun 	}
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	ret = regmap_update_bits(sta32x->regmap, STA32X_CONFA,
766*4882a593Smuzhiyun 				 STA32X_CONFA_MCS_MASK | STA32X_CONFA_IR_MASK,
767*4882a593Smuzhiyun 				 confa);
768*4882a593Smuzhiyun 	if (ret < 0)
769*4882a593Smuzhiyun 		return ret;
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	ret = regmap_update_bits(sta32x->regmap, STA32X_CONFB,
772*4882a593Smuzhiyun 				 STA32X_CONFB_SAI_MASK | STA32X_CONFB_SAIFB,
773*4882a593Smuzhiyun 				 confb);
774*4882a593Smuzhiyun 	if (ret < 0)
775*4882a593Smuzhiyun 		return ret;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	return 0;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun 
sta32x_startup_sequence(struct sta32x_priv * sta32x)780*4882a593Smuzhiyun static int sta32x_startup_sequence(struct sta32x_priv *sta32x)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun 	if (sta32x->gpiod_nreset) {
783*4882a593Smuzhiyun 		gpiod_set_value(sta32x->gpiod_nreset, 0);
784*4882a593Smuzhiyun 		mdelay(1);
785*4882a593Smuzhiyun 		gpiod_set_value(sta32x->gpiod_nreset, 1);
786*4882a593Smuzhiyun 		mdelay(1);
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	return 0;
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun /**
793*4882a593Smuzhiyun  * sta32x_set_bias_level - DAPM callback
794*4882a593Smuzhiyun  * @component: the component device
795*4882a593Smuzhiyun  * @level: DAPM power level
796*4882a593Smuzhiyun  *
797*4882a593Smuzhiyun  * This is called by ALSA to put the component into low power mode
798*4882a593Smuzhiyun  * or to wake it up.  If the component is powered off completely
799*4882a593Smuzhiyun  * all registers must be restored after power on.
800*4882a593Smuzhiyun  */
sta32x_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)801*4882a593Smuzhiyun static int sta32x_set_bias_level(struct snd_soc_component *component,
802*4882a593Smuzhiyun 				 enum snd_soc_bias_level level)
803*4882a593Smuzhiyun {
804*4882a593Smuzhiyun 	int ret;
805*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	dev_dbg(component->dev, "level = %d\n", level);
808*4882a593Smuzhiyun 	switch (level) {
809*4882a593Smuzhiyun 	case SND_SOC_BIAS_ON:
810*4882a593Smuzhiyun 		break;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	case SND_SOC_BIAS_PREPARE:
813*4882a593Smuzhiyun 		/* Full power on */
814*4882a593Smuzhiyun 		regmap_update_bits(sta32x->regmap, STA32X_CONFF,
815*4882a593Smuzhiyun 				    STA32X_CONFF_PWDN | STA32X_CONFF_EAPD,
816*4882a593Smuzhiyun 				    STA32X_CONFF_PWDN | STA32X_CONFF_EAPD);
817*4882a593Smuzhiyun 		break;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	case SND_SOC_BIAS_STANDBY:
820*4882a593Smuzhiyun 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
821*4882a593Smuzhiyun 			ret = regulator_bulk_enable(ARRAY_SIZE(sta32x->supplies),
822*4882a593Smuzhiyun 						    sta32x->supplies);
823*4882a593Smuzhiyun 			if (ret != 0) {
824*4882a593Smuzhiyun 				dev_err(component->dev,
825*4882a593Smuzhiyun 					"Failed to enable supplies: %d\n", ret);
826*4882a593Smuzhiyun 				return ret;
827*4882a593Smuzhiyun 			}
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 			sta32x_startup_sequence(sta32x);
830*4882a593Smuzhiyun 			sta32x_cache_sync(component);
831*4882a593Smuzhiyun 			sta32x_watchdog_start(sta32x);
832*4882a593Smuzhiyun 		}
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 		/* Power down */
835*4882a593Smuzhiyun 		regmap_update_bits(sta32x->regmap, STA32X_CONFF,
836*4882a593Smuzhiyun 				   STA32X_CONFF_PWDN | STA32X_CONFF_EAPD,
837*4882a593Smuzhiyun 				   0);
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 		break;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	case SND_SOC_BIAS_OFF:
842*4882a593Smuzhiyun 		/* The chip runs through the power down sequence for us. */
843*4882a593Smuzhiyun 		regmap_update_bits(sta32x->regmap, STA32X_CONFF,
844*4882a593Smuzhiyun 				   STA32X_CONFF_PWDN | STA32X_CONFF_EAPD, 0);
845*4882a593Smuzhiyun 		msleep(300);
846*4882a593Smuzhiyun 		sta32x_watchdog_stop(sta32x);
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 		gpiod_set_value(sta32x->gpiod_nreset, 0);
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 		regulator_bulk_disable(ARRAY_SIZE(sta32x->supplies),
851*4882a593Smuzhiyun 				       sta32x->supplies);
852*4882a593Smuzhiyun 		break;
853*4882a593Smuzhiyun 	}
854*4882a593Smuzhiyun 	return 0;
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun static const struct snd_soc_dai_ops sta32x_dai_ops = {
858*4882a593Smuzhiyun 	.hw_params	= sta32x_hw_params,
859*4882a593Smuzhiyun 	.set_sysclk	= sta32x_set_dai_sysclk,
860*4882a593Smuzhiyun 	.set_fmt	= sta32x_set_dai_fmt,
861*4882a593Smuzhiyun };
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun static struct snd_soc_dai_driver sta32x_dai = {
864*4882a593Smuzhiyun 	.name = "sta32x-hifi",
865*4882a593Smuzhiyun 	.playback = {
866*4882a593Smuzhiyun 		.stream_name = "Playback",
867*4882a593Smuzhiyun 		.channels_min = 2,
868*4882a593Smuzhiyun 		.channels_max = 2,
869*4882a593Smuzhiyun 		.rates = STA32X_RATES,
870*4882a593Smuzhiyun 		.formats = STA32X_FORMATS,
871*4882a593Smuzhiyun 	},
872*4882a593Smuzhiyun 	.ops = &sta32x_dai_ops,
873*4882a593Smuzhiyun };
874*4882a593Smuzhiyun 
sta32x_probe(struct snd_soc_component * component)875*4882a593Smuzhiyun static int sta32x_probe(struct snd_soc_component *component)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
878*4882a593Smuzhiyun 	struct sta32x_platform_data *pdata = sta32x->pdata;
879*4882a593Smuzhiyun 	int i, ret = 0, thermal = 0;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	sta32x->component = component;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	if (sta32x->xti_clk) {
884*4882a593Smuzhiyun 		ret = clk_prepare_enable(sta32x->xti_clk);
885*4882a593Smuzhiyun 		if (ret != 0) {
886*4882a593Smuzhiyun 			dev_err(component->dev,
887*4882a593Smuzhiyun 				"Failed to enable clock: %d\n", ret);
888*4882a593Smuzhiyun 			return ret;
889*4882a593Smuzhiyun 		}
890*4882a593Smuzhiyun 	}
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	ret = regulator_bulk_enable(ARRAY_SIZE(sta32x->supplies),
893*4882a593Smuzhiyun 				    sta32x->supplies);
894*4882a593Smuzhiyun 	if (ret != 0) {
895*4882a593Smuzhiyun 		dev_err(component->dev, "Failed to enable supplies: %d\n", ret);
896*4882a593Smuzhiyun 		goto err_clk_disable_unprepare;
897*4882a593Smuzhiyun 	}
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	ret = sta32x_startup_sequence(sta32x);
900*4882a593Smuzhiyun 	if (ret < 0) {
901*4882a593Smuzhiyun 		dev_err(component->dev, "Failed to startup device\n");
902*4882a593Smuzhiyun 		goto err_regulator_bulk_disable;
903*4882a593Smuzhiyun 	}
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	/* CONFA */
906*4882a593Smuzhiyun 	if (!pdata->thermal_warning_recovery)
907*4882a593Smuzhiyun 		thermal |= STA32X_CONFA_TWAB;
908*4882a593Smuzhiyun 	if (!pdata->thermal_warning_adjustment)
909*4882a593Smuzhiyun 		thermal |= STA32X_CONFA_TWRB;
910*4882a593Smuzhiyun 	if (!pdata->fault_detect_recovery)
911*4882a593Smuzhiyun 		thermal |= STA32X_CONFA_FDRB;
912*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_CONFA,
913*4882a593Smuzhiyun 			   STA32X_CONFA_TWAB | STA32X_CONFA_TWRB |
914*4882a593Smuzhiyun 			   STA32X_CONFA_FDRB,
915*4882a593Smuzhiyun 			   thermal);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	/* CONFC */
918*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_CONFC,
919*4882a593Smuzhiyun 			   STA32X_CONFC_CSZ_MASK,
920*4882a593Smuzhiyun 			   pdata->drop_compensation_ns
921*4882a593Smuzhiyun 				<< STA32X_CONFC_CSZ_SHIFT);
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	/* CONFE */
924*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_CONFE,
925*4882a593Smuzhiyun 			   STA32X_CONFE_MPCV,
926*4882a593Smuzhiyun 			   pdata->max_power_use_mpcc ?
927*4882a593Smuzhiyun 				STA32X_CONFE_MPCV : 0);
928*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_CONFE,
929*4882a593Smuzhiyun 			   STA32X_CONFE_MPC,
930*4882a593Smuzhiyun 			   pdata->max_power_correction ?
931*4882a593Smuzhiyun 				STA32X_CONFE_MPC : 0);
932*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_CONFE,
933*4882a593Smuzhiyun 			   STA32X_CONFE_AME,
934*4882a593Smuzhiyun 			   pdata->am_reduction_mode ?
935*4882a593Smuzhiyun 				STA32X_CONFE_AME : 0);
936*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_CONFE,
937*4882a593Smuzhiyun 			   STA32X_CONFE_PWMS,
938*4882a593Smuzhiyun 			   pdata->odd_pwm_speed_mode ?
939*4882a593Smuzhiyun 				STA32X_CONFE_PWMS : 0);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	/*  CONFF */
942*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_CONFF,
943*4882a593Smuzhiyun 			   STA32X_CONFF_IDE,
944*4882a593Smuzhiyun 			   pdata->invalid_input_detect_mute ?
945*4882a593Smuzhiyun 				STA32X_CONFF_IDE : 0);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	/* select output configuration  */
948*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_CONFF,
949*4882a593Smuzhiyun 			   STA32X_CONFF_OCFG_MASK,
950*4882a593Smuzhiyun 			   pdata->output_conf
951*4882a593Smuzhiyun 				<< STA32X_CONFF_OCFG_SHIFT);
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	/* channel to output mapping */
954*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_C1CFG,
955*4882a593Smuzhiyun 			   STA32X_CxCFG_OM_MASK,
956*4882a593Smuzhiyun 			   pdata->ch1_output_mapping
957*4882a593Smuzhiyun 				<< STA32X_CxCFG_OM_SHIFT);
958*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_C2CFG,
959*4882a593Smuzhiyun 			   STA32X_CxCFG_OM_MASK,
960*4882a593Smuzhiyun 			   pdata->ch2_output_mapping
961*4882a593Smuzhiyun 				<< STA32X_CxCFG_OM_SHIFT);
962*4882a593Smuzhiyun 	regmap_update_bits(sta32x->regmap, STA32X_C3CFG,
963*4882a593Smuzhiyun 			   STA32X_CxCFG_OM_MASK,
964*4882a593Smuzhiyun 			   pdata->ch3_output_mapping
965*4882a593Smuzhiyun 				<< STA32X_CxCFG_OM_SHIFT);
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	/* initialize coefficient shadow RAM with reset values */
968*4882a593Smuzhiyun 	for (i = 4; i <= 49; i += 5)
969*4882a593Smuzhiyun 		sta32x->coef_shadow[i] = 0x400000;
970*4882a593Smuzhiyun 	for (i = 50; i <= 54; i++)
971*4882a593Smuzhiyun 		sta32x->coef_shadow[i] = 0x7fffff;
972*4882a593Smuzhiyun 	sta32x->coef_shadow[55] = 0x5a9df7;
973*4882a593Smuzhiyun 	sta32x->coef_shadow[56] = 0x7fffff;
974*4882a593Smuzhiyun 	sta32x->coef_shadow[59] = 0x7fffff;
975*4882a593Smuzhiyun 	sta32x->coef_shadow[60] = 0x400000;
976*4882a593Smuzhiyun 	sta32x->coef_shadow[61] = 0x400000;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	if (sta32x->pdata->needs_esd_watchdog)
979*4882a593Smuzhiyun 		INIT_DELAYED_WORK(&sta32x->watchdog_work, sta32x_watchdog);
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_STANDBY);
982*4882a593Smuzhiyun 	/* Bias level configuration will have done an extra enable */
983*4882a593Smuzhiyun 	regulator_bulk_disable(ARRAY_SIZE(sta32x->supplies), sta32x->supplies);
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	return 0;
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun err_regulator_bulk_disable:
988*4882a593Smuzhiyun 	regulator_bulk_disable(ARRAY_SIZE(sta32x->supplies), sta32x->supplies);
989*4882a593Smuzhiyun err_clk_disable_unprepare:
990*4882a593Smuzhiyun 	if (sta32x->xti_clk)
991*4882a593Smuzhiyun 		clk_disable_unprepare(sta32x->xti_clk);
992*4882a593Smuzhiyun 	return ret;
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun 
sta32x_remove(struct snd_soc_component * component)995*4882a593Smuzhiyun static void sta32x_remove(struct snd_soc_component *component)
996*4882a593Smuzhiyun {
997*4882a593Smuzhiyun 	struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component);
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	sta32x_watchdog_stop(sta32x);
1000*4882a593Smuzhiyun 	regulator_bulk_disable(ARRAY_SIZE(sta32x->supplies), sta32x->supplies);
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	if (sta32x->xti_clk)
1003*4882a593Smuzhiyun 		clk_disable_unprepare(sta32x->xti_clk);
1004*4882a593Smuzhiyun }
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun static const struct snd_soc_component_driver sta32x_component = {
1007*4882a593Smuzhiyun 	.probe			= sta32x_probe,
1008*4882a593Smuzhiyun 	.remove			= sta32x_remove,
1009*4882a593Smuzhiyun 	.set_bias_level		= sta32x_set_bias_level,
1010*4882a593Smuzhiyun 	.controls		= sta32x_snd_controls,
1011*4882a593Smuzhiyun 	.num_controls		= ARRAY_SIZE(sta32x_snd_controls),
1012*4882a593Smuzhiyun 	.dapm_widgets		= sta32x_dapm_widgets,
1013*4882a593Smuzhiyun 	.num_dapm_widgets	= ARRAY_SIZE(sta32x_dapm_widgets),
1014*4882a593Smuzhiyun 	.dapm_routes		= sta32x_dapm_routes,
1015*4882a593Smuzhiyun 	.num_dapm_routes	= ARRAY_SIZE(sta32x_dapm_routes),
1016*4882a593Smuzhiyun 	.suspend_bias_off	= 1,
1017*4882a593Smuzhiyun 	.idle_bias_on		= 1,
1018*4882a593Smuzhiyun 	.use_pmdown_time	= 1,
1019*4882a593Smuzhiyun 	.endianness		= 1,
1020*4882a593Smuzhiyun 	.non_legacy_dai_naming	= 1,
1021*4882a593Smuzhiyun };
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun static const struct regmap_config sta32x_regmap = {
1024*4882a593Smuzhiyun 	.reg_bits =		8,
1025*4882a593Smuzhiyun 	.val_bits =		8,
1026*4882a593Smuzhiyun 	.max_register =		STA32X_FDRC2,
1027*4882a593Smuzhiyun 	.reg_defaults =		sta32x_regs,
1028*4882a593Smuzhiyun 	.num_reg_defaults =	ARRAY_SIZE(sta32x_regs),
1029*4882a593Smuzhiyun 	.cache_type =		REGCACHE_RBTREE,
1030*4882a593Smuzhiyun 	.wr_table =		&sta32x_write_regs,
1031*4882a593Smuzhiyun 	.rd_table =		&sta32x_read_regs,
1032*4882a593Smuzhiyun 	.volatile_table =	&sta32x_volatile_regs,
1033*4882a593Smuzhiyun };
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun #ifdef CONFIG_OF
1036*4882a593Smuzhiyun static const struct of_device_id st32x_dt_ids[] = {
1037*4882a593Smuzhiyun 	{ .compatible = "st,sta32x", },
1038*4882a593Smuzhiyun 	{ }
1039*4882a593Smuzhiyun };
1040*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, st32x_dt_ids);
1041*4882a593Smuzhiyun 
sta32x_probe_dt(struct device * dev,struct sta32x_priv * sta32x)1042*4882a593Smuzhiyun static int sta32x_probe_dt(struct device *dev, struct sta32x_priv *sta32x)
1043*4882a593Smuzhiyun {
1044*4882a593Smuzhiyun 	struct device_node *np = dev->of_node;
1045*4882a593Smuzhiyun 	struct sta32x_platform_data *pdata;
1046*4882a593Smuzhiyun 	u16 tmp;
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1049*4882a593Smuzhiyun 	if (!pdata)
1050*4882a593Smuzhiyun 		return -ENOMEM;
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	of_property_read_u8(np, "st,output-conf",
1053*4882a593Smuzhiyun 			    &pdata->output_conf);
1054*4882a593Smuzhiyun 	of_property_read_u8(np, "st,ch1-output-mapping",
1055*4882a593Smuzhiyun 			    &pdata->ch1_output_mapping);
1056*4882a593Smuzhiyun 	of_property_read_u8(np, "st,ch2-output-mapping",
1057*4882a593Smuzhiyun 			    &pdata->ch2_output_mapping);
1058*4882a593Smuzhiyun 	of_property_read_u8(np, "st,ch3-output-mapping",
1059*4882a593Smuzhiyun 			    &pdata->ch3_output_mapping);
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	if (of_get_property(np, "st,fault-detect-recovery", NULL))
1062*4882a593Smuzhiyun 		pdata->fault_detect_recovery = 1;
1063*4882a593Smuzhiyun 	if (of_get_property(np, "st,thermal-warning-recovery", NULL))
1064*4882a593Smuzhiyun 		pdata->thermal_warning_recovery = 1;
1065*4882a593Smuzhiyun 	if (of_get_property(np, "st,thermal-warning-adjustment", NULL))
1066*4882a593Smuzhiyun 		pdata->thermal_warning_adjustment = 1;
1067*4882a593Smuzhiyun 	if (of_get_property(np, "st,needs_esd_watchdog", NULL))
1068*4882a593Smuzhiyun 		pdata->needs_esd_watchdog = 1;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	tmp = 140;
1071*4882a593Smuzhiyun 	of_property_read_u16(np, "st,drop-compensation-ns", &tmp);
1072*4882a593Smuzhiyun 	pdata->drop_compensation_ns = clamp_t(u16, tmp, 0, 300) / 20;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 	/* CONFE */
1075*4882a593Smuzhiyun 	if (of_get_property(np, "st,max-power-use-mpcc", NULL))
1076*4882a593Smuzhiyun 		pdata->max_power_use_mpcc = 1;
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun 	if (of_get_property(np, "st,max-power-correction", NULL))
1079*4882a593Smuzhiyun 		pdata->max_power_correction = 1;
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	if (of_get_property(np, "st,am-reduction-mode", NULL))
1082*4882a593Smuzhiyun 		pdata->am_reduction_mode = 1;
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	if (of_get_property(np, "st,odd-pwm-speed-mode", NULL))
1085*4882a593Smuzhiyun 		pdata->odd_pwm_speed_mode = 1;
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	/* CONFF */
1088*4882a593Smuzhiyun 	if (of_get_property(np, "st,invalid-input-detect-mute", NULL))
1089*4882a593Smuzhiyun 		pdata->invalid_input_detect_mute = 1;
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	sta32x->pdata = pdata;
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	return 0;
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun #endif
1096*4882a593Smuzhiyun 
sta32x_i2c_probe(struct i2c_client * i2c,const struct i2c_device_id * id)1097*4882a593Smuzhiyun static int sta32x_i2c_probe(struct i2c_client *i2c,
1098*4882a593Smuzhiyun 			    const struct i2c_device_id *id)
1099*4882a593Smuzhiyun {
1100*4882a593Smuzhiyun 	struct device *dev = &i2c->dev;
1101*4882a593Smuzhiyun 	struct sta32x_priv *sta32x;
1102*4882a593Smuzhiyun 	int ret, i;
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	sta32x = devm_kzalloc(&i2c->dev, sizeof(struct sta32x_priv),
1105*4882a593Smuzhiyun 			      GFP_KERNEL);
1106*4882a593Smuzhiyun 	if (!sta32x)
1107*4882a593Smuzhiyun 		return -ENOMEM;
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	mutex_init(&sta32x->coeff_lock);
1110*4882a593Smuzhiyun 	sta32x->pdata = dev_get_platdata(dev);
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun #ifdef CONFIG_OF
1113*4882a593Smuzhiyun 	if (dev->of_node) {
1114*4882a593Smuzhiyun 		ret = sta32x_probe_dt(dev, sta32x);
1115*4882a593Smuzhiyun 		if (ret < 0)
1116*4882a593Smuzhiyun 			return ret;
1117*4882a593Smuzhiyun 	}
1118*4882a593Smuzhiyun #endif
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	/* Clock */
1121*4882a593Smuzhiyun 	sta32x->xti_clk = devm_clk_get(dev, "xti");
1122*4882a593Smuzhiyun 	if (IS_ERR(sta32x->xti_clk)) {
1123*4882a593Smuzhiyun 		ret = PTR_ERR(sta32x->xti_clk);
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 		if (ret == -EPROBE_DEFER)
1126*4882a593Smuzhiyun 			return ret;
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 		sta32x->xti_clk = NULL;
1129*4882a593Smuzhiyun 	}
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	/* GPIOs */
1132*4882a593Smuzhiyun 	sta32x->gpiod_nreset = devm_gpiod_get_optional(dev, "reset",
1133*4882a593Smuzhiyun 						       GPIOD_OUT_LOW);
1134*4882a593Smuzhiyun 	if (IS_ERR(sta32x->gpiod_nreset))
1135*4882a593Smuzhiyun 		return PTR_ERR(sta32x->gpiod_nreset);
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	/* regulators */
1138*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(sta32x->supplies); i++)
1139*4882a593Smuzhiyun 		sta32x->supplies[i].supply = sta32x_supply_names[i];
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(sta32x->supplies),
1142*4882a593Smuzhiyun 				      sta32x->supplies);
1143*4882a593Smuzhiyun 	if (ret != 0) {
1144*4882a593Smuzhiyun 		dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
1145*4882a593Smuzhiyun 		return ret;
1146*4882a593Smuzhiyun 	}
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	sta32x->regmap = devm_regmap_init_i2c(i2c, &sta32x_regmap);
1149*4882a593Smuzhiyun 	if (IS_ERR(sta32x->regmap)) {
1150*4882a593Smuzhiyun 		ret = PTR_ERR(sta32x->regmap);
1151*4882a593Smuzhiyun 		dev_err(dev, "Failed to init regmap: %d\n", ret);
1152*4882a593Smuzhiyun 		return ret;
1153*4882a593Smuzhiyun 	}
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	i2c_set_clientdata(i2c, sta32x);
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	ret = devm_snd_soc_register_component(dev, &sta32x_component,
1158*4882a593Smuzhiyun 					      &sta32x_dai, 1);
1159*4882a593Smuzhiyun 	if (ret < 0)
1160*4882a593Smuzhiyun 		dev_err(dev, "Failed to register component (%d)\n", ret);
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	return ret;
1163*4882a593Smuzhiyun }
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun static const struct i2c_device_id sta32x_i2c_id[] = {
1166*4882a593Smuzhiyun 	{ "sta326", 0 },
1167*4882a593Smuzhiyun 	{ "sta328", 0 },
1168*4882a593Smuzhiyun 	{ "sta329", 0 },
1169*4882a593Smuzhiyun 	{ }
1170*4882a593Smuzhiyun };
1171*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, sta32x_i2c_id);
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun static struct i2c_driver sta32x_i2c_driver = {
1174*4882a593Smuzhiyun 	.driver = {
1175*4882a593Smuzhiyun 		.name = "sta32x",
1176*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(st32x_dt_ids),
1177*4882a593Smuzhiyun 	},
1178*4882a593Smuzhiyun 	.probe =    sta32x_i2c_probe,
1179*4882a593Smuzhiyun 	.id_table = sta32x_i2c_id,
1180*4882a593Smuzhiyun };
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun module_i2c_driver(sta32x_i2c_driver);
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun MODULE_DESCRIPTION("ASoC STA32X driver");
1185*4882a593Smuzhiyun MODULE_AUTHOR("Johannes Stezenbach <js@sig21.net>");
1186*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1187