Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2//
  3// Driver for the Texas Instruments TAS2562 CODEC
  4// Copyright (C) 2019 Texas Instruments Inc.
  5
  6
  7#include <linux/module.h>
  8#include <linux/errno.h>
  9#include <linux/device.h>
 10#include <linux/i2c.h>
 11#include <linux/pm_runtime.h>
 12#include <linux/regmap.h>
 13#include <linux/slab.h>
 14#include <linux/gpio/consumer.h>
 15#include <linux/regulator/consumer.h>
 16#include <linux/delay.h>
 17
 18#include <sound/pcm.h>
 19#include <sound/pcm_params.h>
 20#include <sound/soc.h>
 21#include <sound/soc-dapm.h>
 22#include <sound/tlv.h>
 23
 24#include "tas2562.h"
 25
 26#define TAS2562_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
 27			 SNDRV_PCM_FORMAT_S32_LE)
 28
 29/* DVC equation involves floating point math
 30 * round(10^(volume in dB/20)*2^30)
 31 * so create a lookup table for 2dB step
 32 */
 33static const unsigned int float_vol_db_lookup[] = {
 340x00000d43, 0x000010b2, 0x00001505, 0x00001a67, 0x00002151,
 350x000029f1, 0x000034cd, 0x00004279, 0x000053af, 0x0000695b,
 360x0000695b, 0x0000a6fa, 0x0000d236, 0x000108a4, 0x00014d2a,
 370x0001a36e, 0x00021008, 0x000298c0, 0x000344df, 0x00041d8f,
 380x00052e5a, 0x000685c8, 0x00083621, 0x000a566d, 0x000d03a7,
 390x0010624d, 0x0014a050, 0x0019f786, 0x0020b0bc, 0x0029279d,
 400x0033cf8d, 0x004139d3, 0x00521d50, 0x00676044, 0x0082248a,
 410x00a3d70a, 0x00ce4328, 0x0103ab3d, 0x0146e75d, 0x019b8c27,
 420x02061b89, 0x028c423f, 0x03352529, 0x0409c2b0, 0x05156d68,
 430x080e9f96, 0x0a24b062, 0x0cc509ab, 0x10137987, 0x143d1362,
 440x197a967f, 0x2013739e, 0x28619ae9, 0x32d64617, 0x40000000
 45};
 46
 47struct tas2562_data {
 48	struct snd_soc_component *component;
 49	struct gpio_desc *sdz_gpio;
 50	struct regmap *regmap;
 51	struct device *dev;
 52	struct i2c_client *client;
 53	int v_sense_slot;
 54	int i_sense_slot;
 55	int volume_lvl;
 
 
 
 56};
 57
 58enum tas256x_model {
 59	TAS2562,
 60	TAS2563,
 
 61};
 62
 63static int tas2562_set_bias_level(struct snd_soc_component *component,
 64				 enum snd_soc_bias_level level)
 65{
 66	struct tas2562_data *tas2562 =
 67			snd_soc_component_get_drvdata(component);
 68
 69	switch (level) {
 70	case SND_SOC_BIAS_ON:
 71		snd_soc_component_update_bits(component,
 72			TAS2562_PWR_CTRL,
 73			TAS2562_MODE_MASK, TAS2562_ACTIVE);
 74		break;
 75	case SND_SOC_BIAS_STANDBY:
 76	case SND_SOC_BIAS_PREPARE:
 77		snd_soc_component_update_bits(component,
 78			TAS2562_PWR_CTRL,
 79			TAS2562_MODE_MASK, TAS2562_MUTE);
 80		break;
 81	case SND_SOC_BIAS_OFF:
 82		snd_soc_component_update_bits(component,
 83			TAS2562_PWR_CTRL,
 84			TAS2562_MODE_MASK, TAS2562_SHUTDOWN);
 85		break;
 86
 87	default:
 88		dev_err(tas2562->dev,
 89				"wrong power level setting %d\n", level);
 90		return -EINVAL;
 91	}
 92
 93	return 0;
 94}
 95
 96static int tas2562_set_samplerate(struct tas2562_data *tas2562, int samplerate)
 97{
 98	int samp_rate;
 99	int ramp_rate;
100
101	switch (samplerate) {
102	case 7350:
103		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
104		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
105		break;
106	case 8000:
107		ramp_rate = 0;
108		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
109		break;
110	case 14700:
111		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
112		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
113		break;
114	case 16000:
115		ramp_rate = 0;
116		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
117		break;
118	case 22050:
119		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
120		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
121		break;
122	case 24000:
123		ramp_rate = 0;
124		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
125		break;
126	case 29400:
127		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
128		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
129		break;
130	case 32000:
131		ramp_rate = 0;
132		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
133		break;
134	case 44100:
135		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
136		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
137		break;
138	case 48000:
139		ramp_rate = 0;
140		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
141		break;
142	case 88200:
143		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
144		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
145		break;
146	case 96000:
147		ramp_rate = 0;
148		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
149		break;
150	case 176400:
151		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
152		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
153		break;
154	case 192000:
155		ramp_rate = 0;
156		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
157		break;
158	default:
159		dev_info(tas2562->dev, "%s, unsupported sample rate, %d\n",
160			__func__, samplerate);
161		return -EINVAL;
162	}
163
164	snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
165		TAS2562_TDM_CFG0_RAMPRATE_MASK,	ramp_rate);
166	snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
167		TAS2562_TDM_CFG0_SAMPRATE_MASK,	samp_rate);
168
169	return 0;
170}
171
172static int tas2562_set_dai_tdm_slot(struct snd_soc_dai *dai,
173		unsigned int tx_mask, unsigned int rx_mask,
174		int slots, int slot_width)
175{
176	struct snd_soc_component *component = dai->component;
177	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
178	int left_slot, right_slot;
179	int slots_cfg;
180	int ret;
181
182	if (!tx_mask) {
183		dev_err(component->dev, "tx masks must not be 0\n");
184		return -EINVAL;
185	}
186
187	if (slots == 1) {
188		if (tx_mask != 1)
189			return -EINVAL;
190
191		left_slot = 0;
192		right_slot = 0;
193	} else {
194		left_slot = __ffs(tx_mask);
195		tx_mask &= ~(1 << left_slot);
196		if (tx_mask == 0) {
197			right_slot = left_slot;
198		} else {
199			right_slot = __ffs(tx_mask);
200			tx_mask &= ~(1 << right_slot);
201		}
202	}
203
204	slots_cfg = (right_slot << TAS2562_RIGHT_SLOT_SHIFT) | left_slot;
205
206	ret = snd_soc_component_write(component, TAS2562_TDM_CFG3, slots_cfg);
207	if (ret < 0)
208		return ret;
209
210	switch (slot_width) {
211	case 16:
212		ret = snd_soc_component_update_bits(component,
213						    TAS2562_TDM_CFG2,
214						    TAS2562_TDM_CFG2_RXLEN_MASK,
215						    TAS2562_TDM_CFG2_RXLEN_16B);
216		break;
217	case 24:
218		ret = snd_soc_component_update_bits(component,
219						    TAS2562_TDM_CFG2,
220						    TAS2562_TDM_CFG2_RXLEN_MASK,
221						    TAS2562_TDM_CFG2_RXLEN_24B);
222		break;
223	case 32:
224		ret = snd_soc_component_update_bits(component,
225						    TAS2562_TDM_CFG2,
226						    TAS2562_TDM_CFG2_RXLEN_MASK,
227						    TAS2562_TDM_CFG2_RXLEN_32B);
228		break;
229
230	case 0:
231		/* Do not change slot width */
232		break;
233	default:
234		dev_err(tas2562->dev, "slot width not supported");
235		ret = -EINVAL;
236	}
237
238	if (ret < 0)
239		return ret;
240
241	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG5,
242					    TAS2562_TDM_CFG5_VSNS_SLOT_MASK,
243					    tas2562->v_sense_slot);
244	if (ret < 0)
245		return ret;
246
247	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG6,
248					    TAS2562_TDM_CFG6_ISNS_SLOT_MASK,
249					    tas2562->i_sense_slot);
250	if (ret < 0)
251		return ret;
252
253	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG5,
254					    TAS2562_TDM_CFG5_VSNS_SLOT_MASK,
255					    tas2562->v_sense_slot);
256	if (ret < 0)
257		return ret;
258
259	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG6,
260					    TAS2562_TDM_CFG6_ISNS_SLOT_MASK,
261					    tas2562->i_sense_slot);
262	if (ret < 0)
263		return ret;
264
265	return 0;
266}
267
268static int tas2562_set_bitwidth(struct tas2562_data *tas2562, int bitwidth)
269{
270	int ret;
271	int val;
272	int sense_en;
273
274	switch (bitwidth) {
275	case SNDRV_PCM_FORMAT_S16_LE:
276		snd_soc_component_update_bits(tas2562->component,
277					      TAS2562_TDM_CFG2,
278					      TAS2562_TDM_CFG2_RXWLEN_MASK,
279					      TAS2562_TDM_CFG2_RXWLEN_16B);
280		break;
281	case SNDRV_PCM_FORMAT_S24_LE:
282		snd_soc_component_update_bits(tas2562->component,
283					      TAS2562_TDM_CFG2,
284					      TAS2562_TDM_CFG2_RXWLEN_MASK,
285					      TAS2562_TDM_CFG2_RXWLEN_24B);
286		break;
287	case SNDRV_PCM_FORMAT_S32_LE:
288		snd_soc_component_update_bits(tas2562->component,
289					      TAS2562_TDM_CFG2,
290					      TAS2562_TDM_CFG2_RXWLEN_MASK,
291					      TAS2562_TDM_CFG2_RXWLEN_32B);
292		break;
293
294	default:
295		dev_info(tas2562->dev, "Unsupported bitwidth format\n");
296		return -EINVAL;
297	}
298
299	val = snd_soc_component_read(tas2562->component, TAS2562_PWR_CTRL);
300	if (val < 0)
301		return val;
302
303	if (val & (1 << TAS2562_VSENSE_POWER_EN))
304		sense_en = 0;
305	else
306		sense_en = TAS2562_TDM_CFG5_VSNS_EN;
307
308	ret = snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG5,
309		TAS2562_TDM_CFG5_VSNS_EN, sense_en);
310	if (ret < 0)
311		return ret;
312
313	if (val & (1 << TAS2562_ISENSE_POWER_EN))
314		sense_en = 0;
315	else
316		sense_en = TAS2562_TDM_CFG6_ISNS_EN;
317
318	ret = snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG6,
319		TAS2562_TDM_CFG6_ISNS_EN, sense_en);
320	if (ret < 0)
321		return ret;
322
323	return 0;
324}
325
326static int tas2562_hw_params(struct snd_pcm_substream *substream,
327			     struct snd_pcm_hw_params *params,
328			     struct snd_soc_dai *dai)
329{
330	struct snd_soc_component *component = dai->component;
331	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
332	int ret;
333
334	ret = tas2562_set_bitwidth(tas2562, params_format(params));
335	if (ret) {
336		dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret);
337		return ret;
338	}
339
340	ret = tas2562_set_samplerate(tas2562, params_rate(params));
341	if (ret)
342		dev_err(tas2562->dev, "set sample rate failed, %d\n", ret);
343
344	return ret;
345}
346
347static int tas2562_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
348{
349	struct snd_soc_component *component = dai->component;
350	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
351	u8 asi_cfg_1 = 0;
352	u8 tdm_rx_start_slot = 0;
353	int ret;
354
355	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
356	case SND_SOC_DAIFMT_NB_NF:
357		asi_cfg_1 = 0;
358		break;
359	case SND_SOC_DAIFMT_IB_NF:
360		asi_cfg_1 |= TAS2562_TDM_CFG1_RX_FALLING;
361		break;
362	default:
363		dev_err(tas2562->dev, "ASI format Inverse is not found\n");
364		return -EINVAL;
365	}
366
367	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
368					    TAS2562_TDM_CFG1_RX_EDGE_MASK,
369					    asi_cfg_1);
370	if (ret < 0) {
371		dev_err(tas2562->dev, "Failed to set RX edge\n");
372		return ret;
373	}
374	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
375	case SND_SOC_DAIFMT_LEFT_J:
376	case SND_SOC_DAIFMT_DSP_B:
377		tdm_rx_start_slot = 0;
378		break;
379	case SND_SOC_DAIFMT_I2S:
380	case SND_SOC_DAIFMT_DSP_A:
381		tdm_rx_start_slot = 1;
382		break;
383	default:
384		dev_err(tas2562->dev,
385			"DAI Format is not found, fmt=0x%x\n", fmt);
386		return -EINVAL;
387	}
388
389	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
390				TAS2562_RX_OFF_MASK, (tdm_rx_start_slot << 1));
391	if (ret < 0)
392		return ret;
393
394	return 0;
395}
396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397static int tas2562_mute(struct snd_soc_dai *dai, int mute, int direction)
398{
399	struct snd_soc_component *component = dai->component;
400
401	return snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
402					     TAS2562_MODE_MASK,
403					     mute ? TAS2562_MUTE : 0);
404}
405
406static int tas2562_codec_probe(struct snd_soc_component *component)
407{
408	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
409	int ret;
410
411	tas2562->component = component;
412
413	if (tas2562->sdz_gpio)
414		gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
415
416	ret = snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
417					    TAS2562_MODE_MASK, TAS2562_MUTE);
418	if (ret < 0)
419		return ret;
420
421	return 0;
422}
423
424#ifdef CONFIG_PM
425static int tas2562_suspend(struct snd_soc_component *component)
426{
427	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
428
429	regcache_cache_only(tas2562->regmap, true);
430	regcache_mark_dirty(tas2562->regmap);
431
432	if (tas2562->sdz_gpio)
433		gpiod_set_value_cansleep(tas2562->sdz_gpio, 0);
434
435	return 0;
436}
437
438static int tas2562_resume(struct snd_soc_component *component)
439{
440	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
441
442	if (tas2562->sdz_gpio)
443		gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
444
445	regcache_cache_only(tas2562->regmap, false);
446
447	return regcache_sync(tas2562->regmap);
448}
449#else
450#define tas2562_suspend NULL
451#define tas2562_resume NULL
452#endif
453
454static const char * const tas2562_ASI1_src[] = {
455	"I2C offset", "Left", "Right", "LeftRightDiv2",
456};
457
458static SOC_ENUM_SINGLE_DECL(tas2562_ASI1_src_enum, TAS2562_TDM_CFG2, 4,
459			    tas2562_ASI1_src);
460
461static const struct snd_kcontrol_new tas2562_asi1_mux =
462	SOC_DAPM_ENUM("ASI1 Source", tas2562_ASI1_src_enum);
463
464static int tas2562_dac_event(struct snd_soc_dapm_widget *w,
465			     struct snd_kcontrol *kcontrol, int event)
466{
467	struct snd_soc_component *component =
468					snd_soc_dapm_to_component(w->dapm);
469	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
470	int ret;
471
472	switch (event) {
473	case SND_SOC_DAPM_POST_PMU:
474		ret = snd_soc_component_update_bits(component,
475			TAS2562_PWR_CTRL,
476			TAS2562_MODE_MASK,
477			TAS2562_MUTE);
478		if (ret)
479			goto end;
480		break;
481	case SND_SOC_DAPM_PRE_PMD:
482		ret = snd_soc_component_update_bits(component,
483			TAS2562_PWR_CTRL,
484			TAS2562_MODE_MASK,
485			TAS2562_SHUTDOWN);
486		if (ret)
487			goto end;
488		break;
489	default:
490		dev_err(tas2562->dev, "Not supported evevt\n");
491		return -EINVAL;
492	}
493
494end:
495	if (ret < 0)
496		return ret;
497
498	return 0;
499}
500
501static int tas2562_volume_control_get(struct snd_kcontrol *kcontrol,
502				      struct snd_ctl_elem_value *ucontrol)
503{
504	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
505	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
506
507	ucontrol->value.integer.value[0] = tas2562->volume_lvl;
508	return 0;
509}
510
511static int tas2562_volume_control_put(struct snd_kcontrol *kcontrol,
512				      struct snd_ctl_elem_value *ucontrol)
513{
514	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
515	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
516	int ret;
517	u32 reg_val;
518
519	reg_val = float_vol_db_lookup[ucontrol->value.integer.value[0]/2];
520	ret = snd_soc_component_write(component, TAS2562_DVC_CFG4,
521				      (reg_val & 0xff));
522	if (ret)
523		return ret;
524	ret = snd_soc_component_write(component, TAS2562_DVC_CFG3,
525				      ((reg_val >> 8) & 0xff));
526	if (ret)
527		return ret;
528	ret = snd_soc_component_write(component, TAS2562_DVC_CFG2,
529				      ((reg_val >> 16) & 0xff));
530	if (ret)
531		return ret;
532	ret = snd_soc_component_write(component, TAS2562_DVC_CFG1,
533				      ((reg_val >> 24) & 0xff));
534	if (ret)
535		return ret;
536
537	tas2562->volume_lvl = ucontrol->value.integer.value[0];
538
539	return ret;
540}
541
542/* Digital Volume Control. From 0 dB to -110 dB in 1 dB steps */
543static const DECLARE_TLV_DB_SCALE(dvc_tlv, -11000, 100, 0);
544
545static DECLARE_TLV_DB_SCALE(tas2562_dac_tlv, 850, 50, 0);
546
547static const struct snd_kcontrol_new isense_switch =
548	SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_ISENSE_POWER_EN,
549			1, 1);
550
551static const struct snd_kcontrol_new vsense_switch =
552	SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_VSENSE_POWER_EN,
553			1, 1);
554
555static const struct snd_kcontrol_new tas2562_snd_controls[] = {
556	SOC_SINGLE_TLV("Amp Gain Volume", TAS2562_PB_CFG1, 1, 0x1c, 0,
557		       tas2562_dac_tlv),
558	{
559		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
560		.name = "Digital Volume Control",
561		.index = 0,
562		.tlv.p = dvc_tlv,
563		.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | SNDRV_CTL_ELEM_ACCESS_READWRITE,
564		.info = snd_soc_info_volsw,
565		.get = tas2562_volume_control_get,
566		.put = tas2562_volume_control_put,
567		.private_value = SOC_SINGLE_VALUE(TAS2562_DVC_CFG1, 0, 110, 0, 0),
568	},
569};
570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
571static const struct snd_soc_dapm_widget tas2562_dapm_widgets[] = {
572	SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
573	SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux),
574	SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event,
575			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
576	SND_SOC_DAPM_SWITCH("ISENSE", TAS2562_PWR_CTRL, 3, 1, &isense_switch),
577	SND_SOC_DAPM_SWITCH("VSENSE", TAS2562_PWR_CTRL, 2, 1, &vsense_switch),
578	SND_SOC_DAPM_SIGGEN("VMON"),
579	SND_SOC_DAPM_SIGGEN("IMON"),
580	SND_SOC_DAPM_OUTPUT("OUT"),
581};
582
583static const struct snd_soc_dapm_route tas2562_audio_map[] = {
584	{"ASI1 Sel", "I2C offset", "ASI1"},
585	{"ASI1 Sel", "Left", "ASI1"},
586	{"ASI1 Sel", "Right", "ASI1"},
587	{"ASI1 Sel", "LeftRightDiv2", "ASI1"},
588	{ "DAC", NULL, "ASI1 Sel" },
589	{ "OUT", NULL, "DAC" },
590	{"ISENSE", "Switch", "IMON"},
591	{"VSENSE", "Switch", "VMON"},
592};
593
594static const struct snd_soc_component_driver soc_component_dev_tas2562 = {
595	.probe			= tas2562_codec_probe,
596	.suspend		= tas2562_suspend,
597	.resume			= tas2562_resume,
598	.set_bias_level		= tas2562_set_bias_level,
599	.controls		= tas2562_snd_controls,
600	.num_controls		= ARRAY_SIZE(tas2562_snd_controls),
601	.dapm_widgets		= tas2562_dapm_widgets,
602	.num_dapm_widgets	= ARRAY_SIZE(tas2562_dapm_widgets),
603	.dapm_routes		= tas2562_audio_map,
604	.num_dapm_routes	= ARRAY_SIZE(tas2562_audio_map),
605	.idle_bias_on		= 1,
606	.use_pmdown_time	= 1,
607	.endianness		= 1,
608	.non_legacy_dai_naming	= 1,
609};
610
611static const struct snd_soc_dai_ops tas2562_speaker_dai_ops = {
612	.hw_params	= tas2562_hw_params,
613	.set_fmt	= tas2562_set_dai_fmt,
614	.set_tdm_slot	= tas2562_set_dai_tdm_slot,
615	.mute_stream	= tas2562_mute,
616	.no_capture_mute = 1,
617};
618
619static struct snd_soc_dai_driver tas2562_dai[] = {
620	{
621		.name = "tas2562-amplifier",
622		.id = 0,
623		.playback = {
624			.stream_name    = "ASI1 Playback",
625			.channels_min   = 2,
626			.channels_max   = 2,
627			.rates      = SNDRV_PCM_RATE_8000_192000,
628			.formats    = TAS2562_FORMATS,
629		},
630		.capture = {
631			.stream_name    = "ASI1 Capture",
632			.channels_min   = 0,
633			.channels_max   = 2,
634			.rates		= SNDRV_PCM_RATE_8000_192000,
635			.formats	= TAS2562_FORMATS,
636		},
637		.ops = &tas2562_speaker_dai_ops,
638	},
639};
640
641static const struct regmap_range_cfg tas2562_ranges[] = {
642	{
643		.range_min = 0,
644		.range_max = 5 * 128,
645		.selector_reg = TAS2562_PAGE_CTRL,
646		.selector_mask = 0xff,
647		.selector_shift = 0,
648		.window_start = 0,
649		.window_len = 128,
650	},
651};
652
653static const struct reg_default tas2562_reg_defaults[] = {
654	{ TAS2562_PAGE_CTRL, 0x00 },
655	{ TAS2562_SW_RESET, 0x00 },
656	{ TAS2562_PWR_CTRL, 0x0e },
657	{ TAS2562_PB_CFG1, 0x20 },
658	{ TAS2562_TDM_CFG0, 0x09 },
659	{ TAS2562_TDM_CFG1, 0x02 },
660	{ TAS2562_DVC_CFG1, 0x40 },
661	{ TAS2562_DVC_CFG2, 0x40 },
662	{ TAS2562_DVC_CFG3, 0x00 },
663	{ TAS2562_DVC_CFG4, 0x00 },
664};
665
666static const struct regmap_config tas2562_regmap_config = {
667	.reg_bits = 8,
668	.val_bits = 8,
669
670	.max_register = 5 * 128,
671	.cache_type = REGCACHE_RBTREE,
672	.reg_defaults = tas2562_reg_defaults,
673	.num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),
674	.ranges = tas2562_ranges,
675	.num_ranges = ARRAY_SIZE(tas2562_ranges),
676};
677
678static int tas2562_parse_dt(struct tas2562_data *tas2562)
679{
680	struct device *dev = tas2562->dev;
681	int ret = 0;
682
683	tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
684	if (IS_ERR(tas2562->sdz_gpio)) {
685		if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER)
686			return -EPROBE_DEFER;
687
688		tas2562->sdz_gpio = NULL;
689	}
690
691	/*
692	 * The shut-down property is deprecated but needs to be checked for
693	 * backwards compatibility.
694	 */
695	if (tas2562->sdz_gpio == NULL) {
696		tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shut-down",
697							      GPIOD_OUT_HIGH);
698		if (IS_ERR(tas2562->sdz_gpio))
699			if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER)
700				return -EPROBE_DEFER;
701
702		tas2562->sdz_gpio = NULL;
703	}
704
 
 
 
705	ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
706			&tas2562->i_sense_slot);
707	if (ret) {
708		dev_err(dev, "Property %s is missing setting default slot\n",
709			"ti,imon-slot-no");
710		tas2562->i_sense_slot = 0;
711	}
712
713
714	ret = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
715			&tas2562->v_sense_slot);
716	if (ret) {
717		dev_info(dev, "Property %s is missing setting default slot\n",
718			"ti,vmon-slot-no");
719		tas2562->v_sense_slot = 2;
720	}
721
722	if (tas2562->v_sense_slot < tas2562->i_sense_slot) {
723		dev_err(dev, "Vsense slot must be greater than Isense slot\n");
724		return -EINVAL;
725	}
726
727	return ret;
728}
729
730static int tas2562_probe(struct i2c_client *client,
731			 const struct i2c_device_id *id)
 
 
 
 
 
 
 
732{
733	struct device *dev = &client->dev;
734	struct tas2562_data *data;
735	int ret;
 
736
737	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
738	if (!data)
739		return -ENOMEM;
740
 
741	data->client = client;
742	data->dev = &client->dev;
 
743
744	tas2562_parse_dt(data);
745
746	data->regmap = devm_regmap_init_i2c(client, &tas2562_regmap_config);
747	if (IS_ERR(data->regmap)) {
748		ret = PTR_ERR(data->regmap);
749		dev_err(dev, "failed to allocate register map: %d\n", ret);
750		return ret;
751	}
752
753	dev_set_drvdata(&client->dev, data);
754
 
 
 
 
 
 
755	return devm_snd_soc_register_component(dev, &soc_component_dev_tas2562,
756					       tas2562_dai,
757					       ARRAY_SIZE(tas2562_dai));
758
759}
760
761static const struct i2c_device_id tas2562_id[] = {
762	{ "tas2562", TAS2562 },
763	{ "tas2563", TAS2563 },
764	{ }
765};
766MODULE_DEVICE_TABLE(i2c, tas2562_id);
767
768static const struct of_device_id tas2562_of_match[] = {
769	{ .compatible = "ti,tas2562", },
770	{ .compatible = "ti,tas2563", },
 
771	{ },
772};
773MODULE_DEVICE_TABLE(of, tas2562_of_match);
 
774
775static struct i2c_driver tas2562_i2c_driver = {
776	.driver = {
777		.name = "tas2562",
778		.of_match_table = of_match_ptr(tas2562_of_match),
779	},
780	.probe = tas2562_probe,
781	.id_table = tas2562_id,
782};
783
784module_i2c_driver(tas2562_i2c_driver);
785
786MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
787MODULE_DESCRIPTION("TAS2562 Audio amplifier driver");
788MODULE_LICENSE("GPL");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2//
  3// Driver for the Texas Instruments TAS2562 CODEC
  4// Copyright (C) 2019 Texas Instruments Inc.
  5
  6
  7#include <linux/module.h>
  8#include <linux/errno.h>
  9#include <linux/device.h>
 10#include <linux/i2c.h>
 
 11#include <linux/regmap.h>
 12#include <linux/slab.h>
 13#include <linux/gpio/consumer.h>
 14#include <linux/regulator/consumer.h>
 15#include <linux/delay.h>
 16
 17#include <sound/pcm.h>
 18#include <sound/pcm_params.h>
 19#include <sound/soc.h>
 20#include <sound/soc-dapm.h>
 21#include <sound/tlv.h>
 22
 23#include "tas2562.h"
 24
 25#define TAS2562_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
 26			 SNDRV_PCM_FORMAT_S32_LE)
 27
 28/* DVC equation involves floating point math
 29 * round(10^(volume in dB/20)*2^30)
 30 * so create a lookup table for 2dB step
 31 */
 32static const unsigned int float_vol_db_lookup[] = {
 330x00000d43, 0x000010b2, 0x00001505, 0x00001a67, 0x00002151,
 340x000029f1, 0x000034cd, 0x00004279, 0x000053af, 0x0000695b,
 350x0000695b, 0x0000a6fa, 0x0000d236, 0x000108a4, 0x00014d2a,
 360x0001a36e, 0x00021008, 0x000298c0, 0x000344df, 0x00041d8f,
 370x00052e5a, 0x000685c8, 0x00083621, 0x000a566d, 0x000d03a7,
 380x0010624d, 0x0014a050, 0x0019f786, 0x0020b0bc, 0x0029279d,
 390x0033cf8d, 0x004139d3, 0x00521d50, 0x00676044, 0x0082248a,
 400x00a3d70a, 0x00ce4328, 0x0103ab3d, 0x0146e75d, 0x019b8c27,
 410x02061b89, 0x028c423f, 0x03352529, 0x0409c2b0, 0x05156d68,
 420x080e9f96, 0x0a24b062, 0x0cc509ab, 0x10137987, 0x143d1362,
 430x197a967f, 0x2013739e, 0x28619ae9, 0x32d64617, 0x40000000
 44};
 45
 46struct tas2562_data {
 47	struct snd_soc_component *component;
 48	struct gpio_desc *sdz_gpio;
 49	struct regmap *regmap;
 50	struct device *dev;
 51	struct i2c_client *client;
 52	int v_sense_slot;
 53	int i_sense_slot;
 54	int volume_lvl;
 55	int model_id;
 56	bool dac_powered;
 57	bool unmuted;
 58};
 59
 60enum tas256x_model {
 61	TAS2562,
 62	TAS2564,
 63	TAS2110,
 64};
 65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 66static int tas2562_set_samplerate(struct tas2562_data *tas2562, int samplerate)
 67{
 68	int samp_rate;
 69	int ramp_rate;
 70
 71	switch (samplerate) {
 72	case 7350:
 73		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
 74		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
 75		break;
 76	case 8000:
 77		ramp_rate = 0;
 78		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
 79		break;
 80	case 14700:
 81		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
 82		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
 83		break;
 84	case 16000:
 85		ramp_rate = 0;
 86		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
 87		break;
 88	case 22050:
 89		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
 90		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
 91		break;
 92	case 24000:
 93		ramp_rate = 0;
 94		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
 95		break;
 96	case 29400:
 97		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
 98		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
 99		break;
100	case 32000:
101		ramp_rate = 0;
102		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
103		break;
104	case 44100:
105		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
106		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
107		break;
108	case 48000:
109		ramp_rate = 0;
110		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
111		break;
112	case 88200:
113		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
114		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
115		break;
116	case 96000:
117		ramp_rate = 0;
118		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
119		break;
120	case 176400:
121		ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
122		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
123		break;
124	case 192000:
125		ramp_rate = 0;
126		samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
127		break;
128	default:
129		dev_info(tas2562->dev, "%s, unsupported sample rate, %d\n",
130			__func__, samplerate);
131		return -EINVAL;
132	}
133
134	snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
135		TAS2562_TDM_CFG0_RAMPRATE_MASK,	ramp_rate);
136	snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
137		TAS2562_TDM_CFG0_SAMPRATE_MASK,	samp_rate);
138
139	return 0;
140}
141
142static int tas2562_set_dai_tdm_slot(struct snd_soc_dai *dai,
143		unsigned int tx_mask, unsigned int rx_mask,
144		int slots, int slot_width)
145{
146	struct snd_soc_component *component = dai->component;
147	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
148	int left_slot, right_slot;
149	int slots_cfg;
150	int ret;
151
152	if (!tx_mask) {
153		dev_err(component->dev, "tx masks must not be 0\n");
154		return -EINVAL;
155	}
156
157	if (slots == 1) {
158		if (tx_mask != 1)
159			return -EINVAL;
160
161		left_slot = 0;
162		right_slot = 0;
163	} else {
164		left_slot = __ffs(tx_mask);
165		tx_mask &= ~(1 << left_slot);
166		if (tx_mask == 0) {
167			right_slot = left_slot;
168		} else {
169			right_slot = __ffs(tx_mask);
 
170		}
171	}
172
173	slots_cfg = (right_slot << TAS2562_RIGHT_SLOT_SHIFT) | left_slot;
174
175	ret = snd_soc_component_write(component, TAS2562_TDM_CFG3, slots_cfg);
176	if (ret < 0)
177		return ret;
178
179	switch (slot_width) {
180	case 16:
181		ret = snd_soc_component_update_bits(component,
182						    TAS2562_TDM_CFG2,
183						    TAS2562_TDM_CFG2_RXLEN_MASK,
184						    TAS2562_TDM_CFG2_RXLEN_16B);
185		break;
186	case 24:
187		ret = snd_soc_component_update_bits(component,
188						    TAS2562_TDM_CFG2,
189						    TAS2562_TDM_CFG2_RXLEN_MASK,
190						    TAS2562_TDM_CFG2_RXLEN_24B);
191		break;
192	case 32:
193		ret = snd_soc_component_update_bits(component,
194						    TAS2562_TDM_CFG2,
195						    TAS2562_TDM_CFG2_RXLEN_MASK,
196						    TAS2562_TDM_CFG2_RXLEN_32B);
197		break;
198
199	case 0:
200		/* Do not change slot width */
201		break;
202	default:
203		dev_err(tas2562->dev, "slot width not supported");
204		ret = -EINVAL;
205	}
206
207	if (ret < 0)
208		return ret;
209
210	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG5,
211					    TAS2562_TDM_CFG5_VSNS_SLOT_MASK,
212					    tas2562->v_sense_slot);
213	if (ret < 0)
214		return ret;
215
216	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG6,
217					    TAS2562_TDM_CFG6_ISNS_SLOT_MASK,
218					    tas2562->i_sense_slot);
219	if (ret < 0)
220		return ret;
221
 
 
 
 
 
 
 
 
 
 
 
 
222	return 0;
223}
224
225static int tas2562_set_bitwidth(struct tas2562_data *tas2562, int bitwidth)
226{
227	int ret;
228	int val;
229	int sense_en;
230
231	switch (bitwidth) {
232	case SNDRV_PCM_FORMAT_S16_LE:
233		snd_soc_component_update_bits(tas2562->component,
234					      TAS2562_TDM_CFG2,
235					      TAS2562_TDM_CFG2_RXWLEN_MASK,
236					      TAS2562_TDM_CFG2_RXWLEN_16B);
237		break;
238	case SNDRV_PCM_FORMAT_S24_LE:
239		snd_soc_component_update_bits(tas2562->component,
240					      TAS2562_TDM_CFG2,
241					      TAS2562_TDM_CFG2_RXWLEN_MASK,
242					      TAS2562_TDM_CFG2_RXWLEN_24B);
243		break;
244	case SNDRV_PCM_FORMAT_S32_LE:
245		snd_soc_component_update_bits(tas2562->component,
246					      TAS2562_TDM_CFG2,
247					      TAS2562_TDM_CFG2_RXWLEN_MASK,
248					      TAS2562_TDM_CFG2_RXWLEN_32B);
249		break;
250
251	default:
252		dev_info(tas2562->dev, "Unsupported bitwidth format\n");
253		return -EINVAL;
254	}
255
256	val = snd_soc_component_read(tas2562->component, TAS2562_PWR_CTRL);
257	if (val < 0)
258		return val;
259
260	if (val & (1 << TAS2562_VSENSE_POWER_EN))
261		sense_en = 0;
262	else
263		sense_en = TAS2562_TDM_CFG5_VSNS_EN;
264
265	ret = snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG5,
266		TAS2562_TDM_CFG5_VSNS_EN, sense_en);
267	if (ret < 0)
268		return ret;
269
270	if (val & (1 << TAS2562_ISENSE_POWER_EN))
271		sense_en = 0;
272	else
273		sense_en = TAS2562_TDM_CFG6_ISNS_EN;
274
275	ret = snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG6,
276		TAS2562_TDM_CFG6_ISNS_EN, sense_en);
277	if (ret < 0)
278		return ret;
279
280	return 0;
281}
282
283static int tas2562_hw_params(struct snd_pcm_substream *substream,
284			     struct snd_pcm_hw_params *params,
285			     struct snd_soc_dai *dai)
286{
287	struct snd_soc_component *component = dai->component;
288	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
289	int ret;
290
291	ret = tas2562_set_bitwidth(tas2562, params_format(params));
292	if (ret) {
293		dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret);
294		return ret;
295	}
296
297	ret = tas2562_set_samplerate(tas2562, params_rate(params));
298	if (ret)
299		dev_err(tas2562->dev, "set sample rate failed, %d\n", ret);
300
301	return ret;
302}
303
304static int tas2562_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
305{
306	struct snd_soc_component *component = dai->component;
307	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
308	u8 asi_cfg_1 = 0;
309	u8 tdm_rx_start_slot = 0;
310	int ret;
311
312	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
313	case SND_SOC_DAIFMT_NB_NF:
314		asi_cfg_1 = 0;
315		break;
316	case SND_SOC_DAIFMT_IB_NF:
317		asi_cfg_1 |= TAS2562_TDM_CFG1_RX_FALLING;
318		break;
319	default:
320		dev_err(tas2562->dev, "ASI format Inverse is not found\n");
321		return -EINVAL;
322	}
323
324	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
325					    TAS2562_TDM_CFG1_RX_EDGE_MASK,
326					    asi_cfg_1);
327	if (ret < 0) {
328		dev_err(tas2562->dev, "Failed to set RX edge\n");
329		return ret;
330	}
331	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
332	case SND_SOC_DAIFMT_LEFT_J:
333	case SND_SOC_DAIFMT_DSP_B:
334		tdm_rx_start_slot = 0;
335		break;
336	case SND_SOC_DAIFMT_I2S:
337	case SND_SOC_DAIFMT_DSP_A:
338		tdm_rx_start_slot = 1;
339		break;
340	default:
341		dev_err(tas2562->dev,
342			"DAI Format is not found, fmt=0x%x\n", fmt);
343		return -EINVAL;
344	}
345
346	ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
347				TAS2562_RX_OFF_MASK, (tdm_rx_start_slot << 1));
348	if (ret < 0)
349		return ret;
350
351	return 0;
352}
353
354static int tas2562_update_pwr_ctrl(struct tas2562_data *tas2562)
355{
356	struct snd_soc_component *component = tas2562->component;
357	unsigned int val;
358	int ret;
359
360	if (tas2562->dac_powered)
361		val = tas2562->unmuted ?
362			TAS2562_ACTIVE : TAS2562_MUTE;
363	else
364		val = TAS2562_SHUTDOWN;
365
366	ret = snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
367					    TAS2562_MODE_MASK, val);
368	if (ret < 0)
369		return ret;
370
371	return 0;
372}
373
374static int tas2562_mute(struct snd_soc_dai *dai, int mute, int direction)
375{
376	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(dai->component);
377
378	tas2562->unmuted = !mute;
379	return tas2562_update_pwr_ctrl(tas2562);
 
380}
381
382static int tas2562_codec_probe(struct snd_soc_component *component)
383{
384	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
 
385
386	tas2562->component = component;
387
388	if (tas2562->sdz_gpio)
389		gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
390
 
 
 
 
 
391	return 0;
392}
393
394#ifdef CONFIG_PM
395static int tas2562_suspend(struct snd_soc_component *component)
396{
397	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
398
399	regcache_cache_only(tas2562->regmap, true);
400	regcache_mark_dirty(tas2562->regmap);
401
402	if (tas2562->sdz_gpio)
403		gpiod_set_value_cansleep(tas2562->sdz_gpio, 0);
404
405	return 0;
406}
407
408static int tas2562_resume(struct snd_soc_component *component)
409{
410	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
411
412	if (tas2562->sdz_gpio)
413		gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
414
415	regcache_cache_only(tas2562->regmap, false);
416
417	return regcache_sync(tas2562->regmap);
418}
419#else
420#define tas2562_suspend NULL
421#define tas2562_resume NULL
422#endif
423
424static const char * const tas2562_ASI1_src[] = {
425	"I2C offset", "Left", "Right", "LeftRightDiv2",
426};
427
428static SOC_ENUM_SINGLE_DECL(tas2562_ASI1_src_enum, TAS2562_TDM_CFG2, 4,
429			    tas2562_ASI1_src);
430
431static const struct snd_kcontrol_new tas2562_asi1_mux =
432	SOC_DAPM_ENUM("ASI1 Source", tas2562_ASI1_src_enum);
433
434static int tas2562_dac_event(struct snd_soc_dapm_widget *w,
435			     struct snd_kcontrol *kcontrol, int event)
436{
437	struct snd_soc_component *component =
438					snd_soc_dapm_to_component(w->dapm);
439	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
440	int ret = 0;
441
442	switch (event) {
443	case SND_SOC_DAPM_POST_PMU:
444		tas2562->dac_powered = true;
445		ret = tas2562_update_pwr_ctrl(tas2562);
 
 
 
 
446		break;
447	case SND_SOC_DAPM_PRE_PMD:
448		tas2562->dac_powered = false;
449		ret = tas2562_update_pwr_ctrl(tas2562);
 
 
 
 
450		break;
451	default:
452		dev_err(tas2562->dev, "Not supported evevt\n");
453		return -EINVAL;
454	}
455
456	return ret;
 
 
 
 
457}
458
459static int tas2562_volume_control_get(struct snd_kcontrol *kcontrol,
460				      struct snd_ctl_elem_value *ucontrol)
461{
462	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
463	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
464
465	ucontrol->value.integer.value[0] = tas2562->volume_lvl;
466	return 0;
467}
468
469static int tas2562_volume_control_put(struct snd_kcontrol *kcontrol,
470				      struct snd_ctl_elem_value *ucontrol)
471{
472	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
473	struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
474	int ret;
475	u32 reg_val;
476
477	reg_val = float_vol_db_lookup[ucontrol->value.integer.value[0]/2];
478	ret = snd_soc_component_write(component, TAS2562_DVC_CFG4,
479				      (reg_val & 0xff));
480	if (ret)
481		return ret;
482	ret = snd_soc_component_write(component, TAS2562_DVC_CFG3,
483				      ((reg_val >> 8) & 0xff));
484	if (ret)
485		return ret;
486	ret = snd_soc_component_write(component, TAS2562_DVC_CFG2,
487				      ((reg_val >> 16) & 0xff));
488	if (ret)
489		return ret;
490	ret = snd_soc_component_write(component, TAS2562_DVC_CFG1,
491				      ((reg_val >> 24) & 0xff));
492	if (ret)
493		return ret;
494
495	tas2562->volume_lvl = ucontrol->value.integer.value[0];
496
497	return 0;
498}
499
500/* Digital Volume Control. From 0 dB to -110 dB in 1 dB steps */
501static const DECLARE_TLV_DB_SCALE(dvc_tlv, -11000, 100, 0);
502
503static DECLARE_TLV_DB_SCALE(tas2562_dac_tlv, 850, 50, 0);
504
505static const struct snd_kcontrol_new isense_switch =
506	SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_ISENSE_POWER_EN,
507			1, 1);
508
509static const struct snd_kcontrol_new vsense_switch =
510	SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_VSENSE_POWER_EN,
511			1, 1);
512
513static const struct snd_kcontrol_new tas2562_snd_controls[] = {
514	SOC_SINGLE_TLV("Amp Gain Volume", TAS2562_PB_CFG1, 1, 0x1c, 0,
515		       tas2562_dac_tlv),
516	{
517		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
518		.name = "Digital Volume Control",
519		.index = 0,
520		.tlv.p = dvc_tlv,
521		.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | SNDRV_CTL_ELEM_ACCESS_READWRITE,
522		.info = snd_soc_info_volsw,
523		.get = tas2562_volume_control_get,
524		.put = tas2562_volume_control_put,
525		.private_value = SOC_SINGLE_VALUE(TAS2562_DVC_CFG1, 0, 110, 0, 0),
526	},
527};
528
529static const struct snd_soc_dapm_widget tas2110_dapm_widgets[] = {
530	SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
531	SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux),
532	SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event,
533			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
534	SND_SOC_DAPM_OUTPUT("OUT"),
535};
536
537static const struct snd_soc_dapm_route tas2110_audio_map[] = {
538	{"ASI1 Sel", "I2C offset", "ASI1"},
539	{"ASI1 Sel", "Left", "ASI1"},
540	{"ASI1 Sel", "Right", "ASI1"},
541	{"ASI1 Sel", "LeftRightDiv2", "ASI1"},
542	{ "DAC", NULL, "ASI1 Sel" },
543	{ "OUT", NULL, "DAC" },
544};
545
546static const struct snd_soc_component_driver soc_component_dev_tas2110 = {
547	.probe			= tas2562_codec_probe,
548	.suspend		= tas2562_suspend,
549	.resume			= tas2562_resume,
550	.controls		= tas2562_snd_controls,
551	.num_controls		= ARRAY_SIZE(tas2562_snd_controls),
552	.dapm_widgets		= tas2110_dapm_widgets,
553	.num_dapm_widgets	= ARRAY_SIZE(tas2110_dapm_widgets),
554	.dapm_routes		= tas2110_audio_map,
555	.num_dapm_routes	= ARRAY_SIZE(tas2110_audio_map),
556	.idle_bias_on		= 1,
557	.use_pmdown_time	= 1,
558	.endianness		= 1,
559};
560
561static const struct snd_soc_dapm_widget tas2562_dapm_widgets[] = {
562	SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
563	SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux),
564	SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event,
565			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
566	SND_SOC_DAPM_SWITCH("ISENSE", TAS2562_PWR_CTRL, 3, 1, &isense_switch),
567	SND_SOC_DAPM_SWITCH("VSENSE", TAS2562_PWR_CTRL, 2, 1, &vsense_switch),
568	SND_SOC_DAPM_SIGGEN("VMON"),
569	SND_SOC_DAPM_SIGGEN("IMON"),
570	SND_SOC_DAPM_OUTPUT("OUT"),
571};
572
573static const struct snd_soc_dapm_route tas2562_audio_map[] = {
574	{"ASI1 Sel", "I2C offset", "ASI1"},
575	{"ASI1 Sel", "Left", "ASI1"},
576	{"ASI1 Sel", "Right", "ASI1"},
577	{"ASI1 Sel", "LeftRightDiv2", "ASI1"},
578	{ "DAC", NULL, "ASI1 Sel" },
579	{ "OUT", NULL, "DAC" },
580	{"ISENSE", "Switch", "IMON"},
581	{"VSENSE", "Switch", "VMON"},
582};
583
584static const struct snd_soc_component_driver soc_component_dev_tas2562 = {
585	.probe			= tas2562_codec_probe,
586	.suspend		= tas2562_suspend,
587	.resume			= tas2562_resume,
 
588	.controls		= tas2562_snd_controls,
589	.num_controls		= ARRAY_SIZE(tas2562_snd_controls),
590	.dapm_widgets		= tas2562_dapm_widgets,
591	.num_dapm_widgets	= ARRAY_SIZE(tas2562_dapm_widgets),
592	.dapm_routes		= tas2562_audio_map,
593	.num_dapm_routes	= ARRAY_SIZE(tas2562_audio_map),
594	.idle_bias_on		= 1,
595	.use_pmdown_time	= 1,
596	.endianness		= 1,
 
597};
598
599static const struct snd_soc_dai_ops tas2562_speaker_dai_ops = {
600	.hw_params	= tas2562_hw_params,
601	.set_fmt	= tas2562_set_dai_fmt,
602	.set_tdm_slot	= tas2562_set_dai_tdm_slot,
603	.mute_stream	= tas2562_mute,
604	.no_capture_mute = 1,
605};
606
607static struct snd_soc_dai_driver tas2562_dai[] = {
608	{
609		.name = "tas2562-amplifier",
610		.id = 0,
611		.playback = {
612			.stream_name    = "ASI1 Playback",
613			.channels_min   = 2,
614			.channels_max   = 2,
615			.rates      = SNDRV_PCM_RATE_8000_192000,
616			.formats    = TAS2562_FORMATS,
617		},
618		.capture = {
619			.stream_name    = "ASI1 Capture",
620			.channels_min   = 0,
621			.channels_max   = 2,
622			.rates		= SNDRV_PCM_RATE_8000_192000,
623			.formats	= TAS2562_FORMATS,
624		},
625		.ops = &tas2562_speaker_dai_ops,
626	},
627};
628
629static const struct regmap_range_cfg tas2562_ranges[] = {
630	{
631		.range_min = 0,
632		.range_max = 5 * 128,
633		.selector_reg = TAS2562_PAGE_CTRL,
634		.selector_mask = 0xff,
635		.selector_shift = 0,
636		.window_start = 0,
637		.window_len = 128,
638	},
639};
640
641static const struct reg_default tas2562_reg_defaults[] = {
642	{ TAS2562_PAGE_CTRL, 0x00 },
643	{ TAS2562_SW_RESET, 0x00 },
644	{ TAS2562_PWR_CTRL, 0x0e },
645	{ TAS2562_PB_CFG1, 0x20 },
646	{ TAS2562_TDM_CFG0, 0x09 },
647	{ TAS2562_TDM_CFG1, 0x02 },
648	{ TAS2562_DVC_CFG1, 0x40 },
649	{ TAS2562_DVC_CFG2, 0x40 },
650	{ TAS2562_DVC_CFG3, 0x00 },
651	{ TAS2562_DVC_CFG4, 0x00 },
652};
653
654static const struct regmap_config tas2562_regmap_config = {
655	.reg_bits = 8,
656	.val_bits = 8,
657
658	.max_register = 5 * 128,
659	.cache_type = REGCACHE_RBTREE,
660	.reg_defaults = tas2562_reg_defaults,
661	.num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),
662	.ranges = tas2562_ranges,
663	.num_ranges = ARRAY_SIZE(tas2562_ranges),
664};
665
666static int tas2562_parse_dt(struct tas2562_data *tas2562)
667{
668	struct device *dev = tas2562->dev;
669	int ret = 0;
670
671	tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
672	if (IS_ERR(tas2562->sdz_gpio)) {
673		if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER)
674			return -EPROBE_DEFER;
675
676		tas2562->sdz_gpio = NULL;
677	}
678
679	/*
680	 * The shut-down property is deprecated but needs to be checked for
681	 * backwards compatibility.
682	 */
683	if (tas2562->sdz_gpio == NULL) {
684		tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shut-down",
685							      GPIOD_OUT_HIGH);
686		if (IS_ERR(tas2562->sdz_gpio))
687			if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER)
688				return -EPROBE_DEFER;
689
690		tas2562->sdz_gpio = NULL;
691	}
692
693	if (tas2562->model_id == TAS2110)
694		return ret;
695
696	ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
697			&tas2562->i_sense_slot);
698	if (ret) {
699		dev_err(dev, "Property %s is missing setting default slot\n",
700			"ti,imon-slot-no");
701		tas2562->i_sense_slot = 0;
702	}
703
704
705	ret = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
706			&tas2562->v_sense_slot);
707	if (ret) {
708		dev_info(dev, "Property %s is missing setting default slot\n",
709			"ti,vmon-slot-no");
710		tas2562->v_sense_slot = 2;
711	}
712
713	if (tas2562->v_sense_slot < tas2562->i_sense_slot) {
714		dev_err(dev, "Vsense slot must be greater than Isense slot\n");
715		return -EINVAL;
716	}
717
718	return ret;
719}
720
721static const struct i2c_device_id tas2562_id[] = {
722	{ "tas2562", TAS2562 },
723	{ "tas2564", TAS2564 },
724	{ "tas2110", TAS2110 },
725	{ }
726};
727MODULE_DEVICE_TABLE(i2c, tas2562_id);
728
729static int tas2562_probe(struct i2c_client *client)
730{
731	struct device *dev = &client->dev;
732	struct tas2562_data *data;
733	int ret;
734	const struct i2c_device_id *id;
735
736	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
737	if (!data)
738		return -ENOMEM;
739
740	id = i2c_match_id(tas2562_id, client);
741	data->client = client;
742	data->dev = &client->dev;
743	data->model_id = id->driver_data;
744
745	tas2562_parse_dt(data);
746
747	data->regmap = devm_regmap_init_i2c(client, &tas2562_regmap_config);
748	if (IS_ERR(data->regmap)) {
749		ret = PTR_ERR(data->regmap);
750		dev_err(dev, "failed to allocate register map: %d\n", ret);
751		return ret;
752	}
753
754	dev_set_drvdata(&client->dev, data);
755
756	if (data->model_id == TAS2110)
757		return devm_snd_soc_register_component(dev,
758						       &soc_component_dev_tas2110,
759						       tas2562_dai,
760						       ARRAY_SIZE(tas2562_dai));
761
762	return devm_snd_soc_register_component(dev, &soc_component_dev_tas2562,
763					       tas2562_dai,
764					       ARRAY_SIZE(tas2562_dai));
765
766}
767
768#ifdef CONFIG_OF
 
 
 
 
 
 
769static const struct of_device_id tas2562_of_match[] = {
770	{ .compatible = "ti,tas2562", },
771	{ .compatible = "ti,tas2564", },
772	{ .compatible = "ti,tas2110", },
773	{ },
774};
775MODULE_DEVICE_TABLE(of, tas2562_of_match);
776#endif
777
778static struct i2c_driver tas2562_i2c_driver = {
779	.driver = {
780		.name = "tas2562",
781		.of_match_table = of_match_ptr(tas2562_of_match),
782	},
783	.probe = tas2562_probe,
784	.id_table = tas2562_id,
785};
786
787module_i2c_driver(tas2562_i2c_driver);
788
789MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
790MODULE_DESCRIPTION("TAS2562 Audio amplifier driver");
791MODULE_LICENSE("GPL");