xref: /linux/sound/soc/codecs/sta529.c (revision aec2f682d47c54ef434b2d440992626d80b1ebdc)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ASoC codec driver for spear platform
4  *
5  * sound/soc/codecs/sta529.c -- spear ALSA Soc codec driver
6  *
7  * Copyright (C) 2012 ST Microelectronics
8  * Rajeev Kumar <rajeevkumar.linux@gmail.com>
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/init.h>
13 #include <linux/i2c.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/pm.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 
21 #include <sound/core.h>
22 #include <sound/initval.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/tlv.h>
28 
29 /* STA529 Register offsets */
30 #define	 STA529_FFXCFG0		0x00
31 #define	 STA529_FFXCFG1		0x01
32 #define	 STA529_MVOL		0x02
33 #define	 STA529_LVOL		0x03
34 #define	 STA529_RVOL		0x04
35 #define	 STA529_TTF0		0x05
36 #define	 STA529_TTF1		0x06
37 #define	 STA529_TTP0		0x07
38 #define	 STA529_TTP1		0x08
39 #define	 STA529_S2PCFG0		0x0A
40 #define	 STA529_S2PCFG1		0x0B
41 #define	 STA529_P2SCFG0		0x0C
42 #define	 STA529_P2SCFG1		0x0D
43 #define	 STA529_PLLCFG0		0x14
44 #define	 STA529_PLLCFG1		0x15
45 #define	 STA529_PLLCFG2		0x16
46 #define	 STA529_PLLCFG3		0x17
47 #define	 STA529_PLLPFE		0x18
48 #define	 STA529_PLLST		0x19
49 #define	 STA529_ADCCFG		0x1E /*mic_select*/
50 #define	 STA529_CKOCFG		0x1F
51 #define	 STA529_MISC		0x20
52 #define	 STA529_PADST0		0x21
53 #define	 STA529_PADST1		0x22
54 #define	 STA529_FFXST		0x23
55 #define	 STA529_PWMIN1		0x2D
56 #define	 STA529_PWMIN2		0x2E
57 #define	 STA529_POWST		0x32
58 
59 #define STA529_MAX_REGISTER	0x32
60 
61 #define STA529_RATES		(SNDRV_PCM_RATE_8000 | \
62 				SNDRV_PCM_RATE_11025 | \
63 				SNDRV_PCM_RATE_16000 | \
64 				SNDRV_PCM_RATE_22050 | \
65 				SNDRV_PCM_RATE_32000 | \
66 				SNDRV_PCM_RATE_44100 | \
67 				SNDRV_PCM_RATE_48000)
68 
69 #define STA529_FORMAT		(SNDRV_PCM_FMTBIT_S16_LE | \
70 				SNDRV_PCM_FMTBIT_S24_LE | \
71 				SNDRV_PCM_FMTBIT_S32_LE)
72 #define	S2PC_VALUE		0x98
73 #define CLOCK_OUT		0x60
74 #define DATA_FORMAT_MSK		0x0E
75 #define LEFT_J_DATA_FORMAT	0x00
76 #define I2S_DATA_FORMAT		0x02
77 #define RIGHT_J_DATA_FORMAT	0x04
78 #define CODEC_MUTE_VAL		0x80
79 
80 #define POWER_CNTLMSAK		0x40
81 #define POWER_STDBY		0x40
82 #define FFX_MASK		0x80
83 #define FFX_OFF			0x80
84 #define POWER_UP		0x00
85 #define FFX_CLK_ENB		0x01
86 #define FFX_CLK_DIS		0x00
87 #define FFX_CLK_MSK		0x01
88 #define PLAY_FREQ_RANGE_MSK	0x70
89 #define CAP_FREQ_RANGE_MSK	0x0C
90 #define PDATA_LEN_MSK		0xC0
91 #define BCLK_TO_FS_MSK		0x30
92 #define AUDIO_MUTE_MSK		0x80
93 
94 static const struct reg_default sta529_reg_defaults[] = {
95 	{ 0,  0x35 },     /* R0   - FFX Configuration reg 0 */
96 	{ 1,  0xc8 },     /* R1   - FFX Configuration reg 1 */
97 	{ 2,  0x50 },     /* R2   - Master Volume */
98 	{ 3,  0x00 },     /* R3   - Left Volume */
99 	{ 4,  0x00 },     /* R4  -  Right Volume */
100 	{ 10, 0xb2 },     /* R10  - S2P Config Reg 0 */
101 	{ 11, 0x41 },     /* R11  - S2P Config Reg 1 */
102 	{ 12, 0x92 },     /* R12  - P2S Config Reg 0 */
103 	{ 13, 0x41 },     /* R13  - P2S Config Reg 1 */
104 	{ 30, 0xd2 },     /* R30  - ADC Config Reg */
105 	{ 31, 0x40 },     /* R31  - clock Out Reg */
106 	{ 32, 0x21 },     /* R32  - Misc Register */
107 };
108 
109 struct sta529 {
110 	struct regmap *regmap;
111 };
112 
113 static bool sta529_readable(struct device *dev, unsigned int reg)
114 {
115 	switch (reg) {
116 
117 	case STA529_FFXCFG0:
118 	case STA529_FFXCFG1:
119 	case STA529_MVOL:
120 	case STA529_LVOL:
121 	case STA529_RVOL:
122 	case STA529_S2PCFG0:
123 	case STA529_S2PCFG1:
124 	case STA529_P2SCFG0:
125 	case STA529_P2SCFG1:
126 	case STA529_ADCCFG:
127 	case STA529_CKOCFG:
128 	case STA529_MISC:
129 		return true;
130 	default:
131 		return false;
132 	}
133 }
134 
135 
136 static const char *pwm_mode_text[] = { "Binary", "Headphone", "Ternary",
137 	"Phase-shift"};
138 
139 static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -9150, 50, 0);
140 static const DECLARE_TLV_DB_SCALE(master_vol_tlv, -12750, 50, 0);
141 static SOC_ENUM_SINGLE_DECL(pwm_src, STA529_FFXCFG1, 4, pwm_mode_text);
142 
143 static const struct snd_kcontrol_new sta529_snd_controls[] = {
144 	SOC_DOUBLE_R_TLV("Digital Playback Volume", STA529_LVOL, STA529_RVOL, 0,
145 			127, 0, out_gain_tlv),
146 	SOC_SINGLE_TLV("Master Playback Volume", STA529_MVOL, 0, 127, 1,
147 			master_vol_tlv),
148 	SOC_ENUM("PWM Select", pwm_src),
149 };
150 
151 static int sta529_set_bias_level(struct snd_soc_component *component, enum
152 		snd_soc_bias_level level)
153 {
154 	struct sta529 *sta529 = snd_soc_component_get_drvdata(component);
155 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
156 
157 	switch (level) {
158 	case SND_SOC_BIAS_ON:
159 	case SND_SOC_BIAS_PREPARE:
160 		snd_soc_component_update_bits(component, STA529_FFXCFG0, POWER_CNTLMSAK,
161 				POWER_UP);
162 		snd_soc_component_update_bits(component, STA529_MISC,	FFX_CLK_MSK,
163 				FFX_CLK_ENB);
164 		break;
165 	case SND_SOC_BIAS_STANDBY:
166 		if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_OFF)
167 			regcache_sync(sta529->regmap);
168 		snd_soc_component_update_bits(component, STA529_FFXCFG0,
169 					POWER_CNTLMSAK, POWER_STDBY);
170 		/* Making FFX output to zero */
171 		snd_soc_component_update_bits(component, STA529_FFXCFG0, FFX_MASK,
172 				FFX_OFF);
173 		snd_soc_component_update_bits(component, STA529_MISC, FFX_CLK_MSK,
174 				FFX_CLK_DIS);
175 		break;
176 	case SND_SOC_BIAS_OFF:
177 		break;
178 	}
179 
180 	return 0;
181 
182 }
183 
184 static int sta529_hw_params(struct snd_pcm_substream *substream,
185 		struct snd_pcm_hw_params *params,
186 		struct snd_soc_dai *dai)
187 {
188 	struct snd_soc_component *component = dai->component;
189 	int pdata, play_freq_val, record_freq_val;
190 	int bclk_to_fs_ratio;
191 
192 	switch (params_width(params)) {
193 	case 16:
194 		pdata = 1;
195 		bclk_to_fs_ratio = 0;
196 		break;
197 	case 24:
198 		pdata = 2;
199 		bclk_to_fs_ratio = 1;
200 		break;
201 	case 32:
202 		pdata = 3;
203 		bclk_to_fs_ratio = 2;
204 		break;
205 	default:
206 		dev_err(component->dev, "Unsupported format\n");
207 		return -EINVAL;
208 	}
209 
210 	switch (params_rate(params)) {
211 	case 8000:
212 	case 11025:
213 		play_freq_val = 0;
214 		record_freq_val = 2;
215 		break;
216 	case 16000:
217 	case 22050:
218 		play_freq_val = 1;
219 		record_freq_val = 0;
220 		break;
221 
222 	case 32000:
223 	case 44100:
224 	case 48000:
225 		play_freq_val = 2;
226 		record_freq_val = 0;
227 		break;
228 	default:
229 		dev_err(component->dev, "Unsupported rate\n");
230 		return -EINVAL;
231 	}
232 
233 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
234 		snd_soc_component_update_bits(component, STA529_S2PCFG1, PDATA_LEN_MSK,
235 				pdata << 6);
236 		snd_soc_component_update_bits(component, STA529_S2PCFG1, BCLK_TO_FS_MSK,
237 				bclk_to_fs_ratio << 4);
238 		snd_soc_component_update_bits(component, STA529_MISC, PLAY_FREQ_RANGE_MSK,
239 				play_freq_val << 4);
240 	} else {
241 		snd_soc_component_update_bits(component, STA529_P2SCFG1, PDATA_LEN_MSK,
242 				pdata << 6);
243 		snd_soc_component_update_bits(component, STA529_P2SCFG1, BCLK_TO_FS_MSK,
244 				bclk_to_fs_ratio << 4);
245 		snd_soc_component_update_bits(component, STA529_MISC, CAP_FREQ_RANGE_MSK,
246 				record_freq_val << 2);
247 	}
248 
249 	return 0;
250 }
251 
252 static int sta529_mute(struct snd_soc_dai *dai, int mute, int direction)
253 {
254 	u8 val = 0;
255 
256 	if (mute)
257 		val |= CODEC_MUTE_VAL;
258 
259 	snd_soc_component_update_bits(dai->component, STA529_FFXCFG0, AUDIO_MUTE_MSK, val);
260 
261 	return 0;
262 }
263 
264 static int sta529_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
265 {
266 	struct snd_soc_component *component = codec_dai->component;
267 	u8 mode = 0;
268 
269 	/* interface format */
270 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
271 	case SND_SOC_DAIFMT_LEFT_J:
272 		mode = LEFT_J_DATA_FORMAT;
273 		break;
274 	case SND_SOC_DAIFMT_I2S:
275 		mode = I2S_DATA_FORMAT;
276 		break;
277 	case SND_SOC_DAIFMT_RIGHT_J:
278 		mode = RIGHT_J_DATA_FORMAT;
279 		break;
280 	default:
281 		return -EINVAL;
282 	}
283 
284 	snd_soc_component_update_bits(component, STA529_S2PCFG0, DATA_FORMAT_MSK, mode);
285 
286 	return 0;
287 }
288 
289 static const struct snd_soc_dai_ops sta529_dai_ops = {
290 	.hw_params	=	sta529_hw_params,
291 	.set_fmt	=	sta529_set_dai_fmt,
292 	.mute_stream	=	sta529_mute,
293 	.no_capture_mute = 1,
294 };
295 
296 static struct snd_soc_dai_driver sta529_dai = {
297 	.name = "sta529-audio",
298 	.playback = {
299 		.stream_name = "Playback",
300 		.channels_min = 2,
301 		.channels_max = 2,
302 		.rates = STA529_RATES,
303 		.formats = STA529_FORMAT,
304 	},
305 	.capture = {
306 		.stream_name = "Capture",
307 		.channels_min = 2,
308 		.channels_max = 2,
309 		.rates = STA529_RATES,
310 		.formats = STA529_FORMAT,
311 	},
312 	.ops	= &sta529_dai_ops,
313 };
314 
315 static const struct snd_soc_component_driver sta529_component_driver = {
316 	.set_bias_level		= sta529_set_bias_level,
317 	.controls		= sta529_snd_controls,
318 	.num_controls		= ARRAY_SIZE(sta529_snd_controls),
319 	.suspend_bias_off	= 1,
320 	.idle_bias_on		= 1,
321 	.use_pmdown_time	= 1,
322 	.endianness		= 1,
323 };
324 
325 static const struct regmap_config sta529_regmap = {
326 	.reg_bits = 8,
327 	.val_bits = 8,
328 
329 	.max_register = STA529_MAX_REGISTER,
330 	.readable_reg = sta529_readable,
331 
332 	.cache_type = REGCACHE_MAPLE,
333 	.reg_defaults = sta529_reg_defaults,
334 	.num_reg_defaults = ARRAY_SIZE(sta529_reg_defaults),
335 };
336 
337 static int sta529_i2c_probe(struct i2c_client *i2c)
338 {
339 	struct sta529 *sta529;
340 	int ret;
341 
342 	sta529 = devm_kzalloc(&i2c->dev, sizeof(struct sta529), GFP_KERNEL);
343 	if (!sta529)
344 		return -ENOMEM;
345 
346 	sta529->regmap = devm_regmap_init_i2c(i2c, &sta529_regmap);
347 	if (IS_ERR(sta529->regmap)) {
348 		ret = PTR_ERR(sta529->regmap);
349 		dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
350 		return ret;
351 	}
352 
353 	i2c_set_clientdata(i2c, sta529);
354 
355 	ret = devm_snd_soc_register_component(&i2c->dev,
356 			&sta529_component_driver, &sta529_dai, 1);
357 	if (ret != 0)
358 		dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
359 
360 	return ret;
361 }
362 
363 static const struct i2c_device_id sta529_i2c_id[] = {
364 	{ "sta529" },
365 	{ }
366 };
367 MODULE_DEVICE_TABLE(i2c, sta529_i2c_id);
368 
369 static const struct of_device_id sta529_of_match[] = {
370 	{ .compatible = "st,sta529", },
371 	{ }
372 };
373 MODULE_DEVICE_TABLE(of, sta529_of_match);
374 
375 static struct i2c_driver sta529_i2c_driver = {
376 	.driver = {
377 		.name = "sta529",
378 		.of_match_table = sta529_of_match,
379 	},
380 	.probe		= sta529_i2c_probe,
381 	.id_table	= sta529_i2c_id,
382 };
383 
384 module_i2c_driver(sta529_i2c_driver);
385 
386 MODULE_DESCRIPTION("ASoC STA529 codec driver");
387 MODULE_AUTHOR("Rajeev Kumar <rajeevkumar.linux@gmail.com>");
388 MODULE_LICENSE("GPL");
389