2 * TAS571x amplifier audio driver
4 * Copyright (C) 2015 Google, Inc.
5 * Copyright (c) 2013 Daniel Mack <zonque@gmail.com>
8 * Copyright (C) 2016 Petr Kulhavy, Barix AG <petr@barix.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/stddef.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/tlv.h>
34 #define TAS571X_MAX_SUPPLIES 6
37 const char *const *supply_names
;
39 const struct snd_kcontrol_new
*controls
;
41 const struct regmap_config
*regmap_config
;
45 struct tas571x_private
{
46 const struct tas571x_chip
*chip
;
47 struct regmap
*regmap
;
48 struct regulator_bulk_data supplies
[TAS571X_MAX_SUPPLIES
];
51 struct gpio_desc
*reset_gpio
;
52 struct gpio_desc
*pdn_gpio
;
53 struct snd_soc_codec_driver codec_driver
;
56 static int tas571x_register_size(struct tas571x_private
*priv
, unsigned int reg
)
59 case TAS571X_MVOL_REG
:
60 case TAS571X_CH1_VOL_REG
:
61 case TAS571X_CH2_VOL_REG
:
62 return priv
->chip
->vol_reg_size
;
63 case TAS571X_INPUT_MUX_REG
:
64 case TAS571X_CH4_SRC_SELECT_REG
:
65 case TAS571X_PWM_MUX_REG
:
72 static int tas571x_reg_write(void *context
, unsigned int reg
,
75 struct i2c_client
*client
= context
;
76 struct tas571x_private
*priv
= i2c_get_clientdata(client
);
81 size
= tas571x_register_size(priv
, reg
);
84 for (i
= size
; i
>= 1; --i
) {
89 ret
= i2c_master_send(client
, buf
, size
+ 1);
98 static int tas571x_reg_read(void *context
, unsigned int reg
,
101 struct i2c_client
*client
= context
;
102 struct tas571x_private
*priv
= i2c_get_clientdata(client
);
103 uint8_t send_buf
, recv_buf
[4];
104 struct i2c_msg msgs
[2];
109 size
= tas571x_register_size(priv
, reg
);
112 msgs
[0].addr
= client
->addr
;
113 msgs
[0].len
= sizeof(send_buf
);
114 msgs
[0].buf
= &send_buf
;
117 msgs
[1].addr
= client
->addr
;
119 msgs
[1].buf
= recv_buf
;
120 msgs
[1].flags
= I2C_M_RD
;
122 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
125 else if (ret
!= ARRAY_SIZE(msgs
))
130 for (i
= 0; i
< size
; i
++) {
132 *value
|= recv_buf
[i
];
138 static int tas571x_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int format
)
140 struct tas571x_private
*priv
= snd_soc_codec_get_drvdata(dai
->codec
);
142 priv
->format
= format
;
147 static int tas571x_hw_params(struct snd_pcm_substream
*substream
,
148 struct snd_pcm_hw_params
*params
,
149 struct snd_soc_dai
*dai
)
151 struct tas571x_private
*priv
= snd_soc_codec_get_drvdata(dai
->codec
);
154 switch (priv
->format
& SND_SOC_DAIFMT_FORMAT_MASK
) {
155 case SND_SOC_DAIFMT_RIGHT_J
:
158 case SND_SOC_DAIFMT_I2S
:
161 case SND_SOC_DAIFMT_LEFT_J
:
168 if (params_width(params
) >= 24)
170 else if (params_width(params
) >= 20)
173 return regmap_update_bits(priv
->regmap
, TAS571X_SDI_REG
,
174 TAS571X_SDI_FMT_MASK
, val
);
177 static int tas571x_mute(struct snd_soc_dai
*dai
, int mute
)
179 struct snd_soc_codec
*codec
= dai
->codec
;
183 sysctl2
= mute
? TAS571X_SYS_CTRL_2_SDN_MASK
: 0;
185 ret
= snd_soc_update_bits(codec
,
186 TAS571X_SYS_CTRL_2_REG
,
187 TAS571X_SYS_CTRL_2_SDN_MASK
,
189 usleep_range(1000, 2000);
194 static int tas571x_set_bias_level(struct snd_soc_codec
*codec
,
195 enum snd_soc_bias_level level
)
197 struct tas571x_private
*priv
= snd_soc_codec_get_drvdata(codec
);
201 case SND_SOC_BIAS_ON
:
203 case SND_SOC_BIAS_PREPARE
:
205 case SND_SOC_BIAS_STANDBY
:
206 if (snd_soc_codec_get_bias_level(codec
) == SND_SOC_BIAS_OFF
) {
207 if (!IS_ERR(priv
->mclk
)) {
208 ret
= clk_prepare_enable(priv
->mclk
);
211 "Failed to enable master clock: %d\n",
217 gpiod_set_value(priv
->pdn_gpio
, 0);
218 usleep_range(5000, 6000);
220 regcache_cache_only(priv
->regmap
, false);
221 ret
= regcache_sync(priv
->regmap
);
226 case SND_SOC_BIAS_OFF
:
227 regcache_cache_only(priv
->regmap
, true);
228 gpiod_set_value(priv
->pdn_gpio
, 1);
230 if (!IS_ERR(priv
->mclk
))
231 clk_disable_unprepare(priv
->mclk
);
238 static const struct snd_soc_dai_ops tas571x_dai_ops
= {
239 .set_fmt
= tas571x_set_dai_fmt
,
240 .hw_params
= tas571x_hw_params
,
241 .digital_mute
= tas571x_mute
,
244 static const char *const tas5711_supply_names
[] = {
253 static const DECLARE_TLV_DB_SCALE(tas5711_volume_tlv
, -10350, 50, 1);
255 static const struct snd_kcontrol_new tas5711_controls
[] = {
256 SOC_SINGLE_TLV("Master Volume",
258 0, 0xff, 1, tas5711_volume_tlv
),
259 SOC_DOUBLE_R_TLV("Speaker Volume",
262 0, 0xff, 1, tas5711_volume_tlv
),
263 SOC_DOUBLE("Speaker Switch",
264 TAS571X_SOFT_MUTE_REG
,
265 TAS571X_SOFT_MUTE_CH1_SHIFT
, TAS571X_SOFT_MUTE_CH2_SHIFT
,
269 static const struct regmap_range tas571x_readonly_regs_range
[] = {
270 regmap_reg_range(TAS571X_CLK_CTRL_REG
, TAS571X_DEV_ID_REG
),
273 static const struct regmap_range tas571x_volatile_regs_range
[] = {
274 regmap_reg_range(TAS571X_CLK_CTRL_REG
, TAS571X_ERR_STATUS_REG
),
275 regmap_reg_range(TAS571X_OSC_TRIM_REG
, TAS571X_OSC_TRIM_REG
),
278 static const struct regmap_access_table tas571x_write_regs
= {
279 .no_ranges
= tas571x_readonly_regs_range
,
280 .n_no_ranges
= ARRAY_SIZE(tas571x_readonly_regs_range
),
283 static const struct regmap_access_table tas571x_volatile_regs
= {
284 .yes_ranges
= tas571x_volatile_regs_range
,
285 .n_yes_ranges
= ARRAY_SIZE(tas571x_volatile_regs_range
),
289 static const struct reg_default tas5711_reg_defaults
[] = {
299 static const struct regmap_config tas5711_regmap_config
= {
302 .max_register
= 0xff,
303 .reg_read
= tas571x_reg_read
,
304 .reg_write
= tas571x_reg_write
,
305 .reg_defaults
= tas5711_reg_defaults
,
306 .num_reg_defaults
= ARRAY_SIZE(tas5711_reg_defaults
),
307 .cache_type
= REGCACHE_RBTREE
,
308 .wr_table
= &tas571x_write_regs
,
309 .volatile_table
= &tas571x_volatile_regs
,
312 static const struct tas571x_chip tas5711_chip
= {
313 .supply_names
= tas5711_supply_names
,
314 .num_supply_names
= ARRAY_SIZE(tas5711_supply_names
),
315 .controls
= tas5711_controls
,
316 .num_controls
= ARRAY_SIZE(tas5711_controls
),
317 .regmap_config
= &tas5711_regmap_config
,
321 static const char *const tas5717_supply_names
[] = {
329 static const DECLARE_TLV_DB_SCALE(tas5717_volume_tlv
, -10375, 25, 0);
331 static const struct snd_kcontrol_new tas5717_controls
[] = {
332 /* MVOL LSB is ignored - see comments in tas571x_i2c_probe() */
333 SOC_SINGLE_TLV("Master Volume",
334 TAS571X_MVOL_REG
, 1, 0x1ff, 1,
336 SOC_DOUBLE_R_TLV("Speaker Volume",
337 TAS571X_CH1_VOL_REG
, TAS571X_CH2_VOL_REG
,
338 1, 0x1ff, 1, tas5717_volume_tlv
),
339 SOC_DOUBLE("Speaker Switch",
340 TAS571X_SOFT_MUTE_REG
,
341 TAS571X_SOFT_MUTE_CH1_SHIFT
, TAS571X_SOFT_MUTE_CH2_SHIFT
,
345 static const struct reg_default tas5717_reg_defaults
[] = {
355 static const struct regmap_config tas5717_regmap_config
= {
358 .max_register
= 0xff,
359 .reg_read
= tas571x_reg_read
,
360 .reg_write
= tas571x_reg_write
,
361 .reg_defaults
= tas5717_reg_defaults
,
362 .num_reg_defaults
= ARRAY_SIZE(tas5717_reg_defaults
),
363 .cache_type
= REGCACHE_RBTREE
,
364 .wr_table
= &tas571x_write_regs
,
365 .volatile_table
= &tas571x_volatile_regs
,
368 /* This entry is reused for tas5719 as the software interface is identical. */
369 static const struct tas571x_chip tas5717_chip
= {
370 .supply_names
= tas5717_supply_names
,
371 .num_supply_names
= ARRAY_SIZE(tas5717_supply_names
),
372 .controls
= tas5717_controls
,
373 .num_controls
= ARRAY_SIZE(tas5717_controls
),
374 .regmap_config
= &tas5717_regmap_config
,
378 static const char *const tas5721_supply_names
[] = {
385 static const struct snd_kcontrol_new tas5721_controls
[] = {
386 SOC_SINGLE_TLV("Master Volume",
388 0, 0xff, 1, tas5711_volume_tlv
),
389 SOC_DOUBLE_R_TLV("Speaker Volume",
392 0, 0xff, 1, tas5711_volume_tlv
),
393 SOC_DOUBLE("Speaker Switch",
394 TAS571X_SOFT_MUTE_REG
,
395 TAS571X_SOFT_MUTE_CH1_SHIFT
, TAS571X_SOFT_MUTE_CH2_SHIFT
,
399 static const struct reg_default tas5721_reg_defaults
[] = {
400 {TAS571X_CLK_CTRL_REG
, 0x6c},
401 {TAS571X_DEV_ID_REG
, 0x00},
402 {TAS571X_ERR_STATUS_REG
, 0x00},
403 {TAS571X_SYS_CTRL_1_REG
, 0xa0},
404 {TAS571X_SDI_REG
, 0x05},
405 {TAS571X_SYS_CTRL_2_REG
, 0x40},
406 {TAS571X_SOFT_MUTE_REG
, 0x00},
407 {TAS571X_MVOL_REG
, 0xff},
408 {TAS571X_CH1_VOL_REG
, 0x30},
409 {TAS571X_CH2_VOL_REG
, 0x30},
410 {TAS571X_CH3_VOL_REG
, 0x30},
411 {TAS571X_VOL_CFG_REG
, 0x91},
412 {TAS571X_MODULATION_LIMIT_REG
, 0x02},
413 {TAS571X_IC_DELAY_CH1_REG
, 0xac},
414 {TAS571X_IC_DELAY_CH2_REG
, 0x54},
415 {TAS571X_IC_DELAY_CH3_REG
, 0xac},
416 {TAS571X_IC_DELAY_CH4_REG
, 0x54},
417 {TAS571X_PWM_CH_SDN_GROUP_REG
, 0x30},
418 {TAS571X_START_STOP_PERIOD_REG
, 0x0f},
419 {TAS571X_OSC_TRIM_REG
, 0x82},
420 {TAS571X_BKND_ERR_REG
, 0x02},
421 {TAS571X_INPUT_MUX_REG
, 0x17772},
422 {TAS571X_CH4_SRC_SELECT_REG
, 0x4303},
423 {TAS571X_PWM_MUX_REG
, 0x1021345},
426 static const struct regmap_config tas5721_regmap_config
= {
429 .max_register
= 0xff,
430 .reg_read
= tas571x_reg_read
,
431 .reg_write
= tas571x_reg_write
,
432 .reg_defaults
= tas5721_reg_defaults
,
433 .num_reg_defaults
= ARRAY_SIZE(tas5721_reg_defaults
),
434 .cache_type
= REGCACHE_RBTREE
,
435 .wr_table
= &tas571x_write_regs
,
436 .volatile_table
= &tas571x_volatile_regs
,
440 static const struct tas571x_chip tas5721_chip
= {
441 .supply_names
= tas5721_supply_names
,
442 .num_supply_names
= ARRAY_SIZE(tas5721_supply_names
),
443 .controls
= tas5711_controls
,
444 .num_controls
= ARRAY_SIZE(tas5711_controls
),
445 .regmap_config
= &tas5721_regmap_config
,
449 static const struct snd_soc_dapm_widget tas571x_dapm_widgets
[] = {
450 SND_SOC_DAPM_DAC("DACL", NULL
, SND_SOC_NOPM
, 0, 0),
451 SND_SOC_DAPM_DAC("DACR", NULL
, SND_SOC_NOPM
, 0, 0),
453 SND_SOC_DAPM_OUTPUT("OUT_A"),
454 SND_SOC_DAPM_OUTPUT("OUT_B"),
455 SND_SOC_DAPM_OUTPUT("OUT_C"),
456 SND_SOC_DAPM_OUTPUT("OUT_D"),
459 static const struct snd_soc_dapm_route tas571x_dapm_routes
[] = {
460 { "DACL", NULL
, "Playback" },
461 { "DACR", NULL
, "Playback" },
463 { "OUT_A", NULL
, "DACL" },
464 { "OUT_B", NULL
, "DACL" },
465 { "OUT_C", NULL
, "DACR" },
466 { "OUT_D", NULL
, "DACR" },
469 static const struct snd_soc_codec_driver tas571x_codec
= {
470 .set_bias_level
= tas571x_set_bias_level
,
471 .idle_bias_off
= true,
473 .dapm_widgets
= tas571x_dapm_widgets
,
474 .num_dapm_widgets
= ARRAY_SIZE(tas571x_dapm_widgets
),
475 .dapm_routes
= tas571x_dapm_routes
,
476 .num_dapm_routes
= ARRAY_SIZE(tas571x_dapm_routes
),
479 static struct snd_soc_dai_driver tas571x_dai
= {
480 .name
= "tas571x-hifi",
482 .stream_name
= "Playback",
485 .rates
= SNDRV_PCM_RATE_8000_48000
,
486 .formats
= SNDRV_PCM_FMTBIT_S32_LE
|
487 SNDRV_PCM_FMTBIT_S24_LE
|
488 SNDRV_PCM_FMTBIT_S16_LE
,
490 .ops
= &tas571x_dai_ops
,
493 static const struct of_device_id tas571x_of_match
[];
495 static int tas571x_i2c_probe(struct i2c_client
*client
,
496 const struct i2c_device_id
*id
)
498 struct tas571x_private
*priv
;
499 struct device
*dev
= &client
->dev
;
500 const struct of_device_id
*of_id
;
503 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
506 i2c_set_clientdata(client
, priv
);
508 of_id
= of_match_device(tas571x_of_match
, dev
);
510 priv
->chip
= of_id
->data
;
512 priv
->chip
= (void *) id
->driver_data
;
514 priv
->mclk
= devm_clk_get(dev
, "mclk");
515 if (IS_ERR(priv
->mclk
) && PTR_ERR(priv
->mclk
) != -ENOENT
) {
516 dev_err(dev
, "Failed to request mclk: %ld\n",
517 PTR_ERR(priv
->mclk
));
518 return PTR_ERR(priv
->mclk
);
521 BUG_ON(priv
->chip
->num_supply_names
> TAS571X_MAX_SUPPLIES
);
522 for (i
= 0; i
< priv
->chip
->num_supply_names
; i
++)
523 priv
->supplies
[i
].supply
= priv
->chip
->supply_names
[i
];
525 ret
= devm_regulator_bulk_get(dev
, priv
->chip
->num_supply_names
,
528 dev_err(dev
, "Failed to get supplies: %d\n", ret
);
531 ret
= regulator_bulk_enable(priv
->chip
->num_supply_names
,
534 dev_err(dev
, "Failed to enable supplies: %d\n", ret
);
538 priv
->regmap
= devm_regmap_init(dev
, NULL
, client
,
539 priv
->chip
->regmap_config
);
540 if (IS_ERR(priv
->regmap
))
541 return PTR_ERR(priv
->regmap
);
543 priv
->pdn_gpio
= devm_gpiod_get_optional(dev
, "pdn", GPIOD_OUT_LOW
);
544 if (IS_ERR(priv
->pdn_gpio
)) {
545 dev_err(dev
, "error requesting pdn_gpio: %ld\n",
546 PTR_ERR(priv
->pdn_gpio
));
547 return PTR_ERR(priv
->pdn_gpio
);
550 priv
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
552 if (IS_ERR(priv
->reset_gpio
)) {
553 dev_err(dev
, "error requesting reset_gpio: %ld\n",
554 PTR_ERR(priv
->reset_gpio
));
555 return PTR_ERR(priv
->reset_gpio
);
556 } else if (priv
->reset_gpio
) {
557 /* pulse the active low reset line for ~100us */
558 usleep_range(100, 200);
559 gpiod_set_value(priv
->reset_gpio
, 0);
560 usleep_range(12000, 20000);
563 ret
= regmap_write(priv
->regmap
, TAS571X_OSC_TRIM_REG
, 0);
568 memcpy(&priv
->codec_driver
, &tas571x_codec
, sizeof(priv
->codec_driver
));
569 priv
->codec_driver
.controls
= priv
->chip
->controls
;
570 priv
->codec_driver
.num_controls
= priv
->chip
->num_controls
;
572 if (priv
->chip
->vol_reg_size
== 2) {
574 * The master volume defaults to 0x3ff (mute), but we ignore
575 * (zero) the LSB because the hardware step size is 0.125 dB
576 * and TLV_DB_SCALE_ITEM has a resolution of 0.01 dB.
578 ret
= regmap_update_bits(priv
->regmap
, TAS571X_MVOL_REG
, 1, 0);
583 regcache_cache_only(priv
->regmap
, true);
584 gpiod_set_value(priv
->pdn_gpio
, 1);
586 return snd_soc_register_codec(&client
->dev
, &priv
->codec_driver
,
590 static int tas571x_i2c_remove(struct i2c_client
*client
)
592 struct tas571x_private
*priv
= i2c_get_clientdata(client
);
594 snd_soc_unregister_codec(&client
->dev
);
595 regulator_bulk_disable(priv
->chip
->num_supply_names
, priv
->supplies
);
600 static const struct of_device_id tas571x_of_match
[] = {
601 { .compatible
= "ti,tas5711", .data
= &tas5711_chip
, },
602 { .compatible
= "ti,tas5717", .data
= &tas5717_chip
, },
603 { .compatible
= "ti,tas5719", .data
= &tas5717_chip
, },
604 { .compatible
= "ti,tas5721", .data
= &tas5721_chip
, },
607 MODULE_DEVICE_TABLE(of
, tas571x_of_match
);
609 static const struct i2c_device_id tas571x_i2c_id
[] = {
610 { "tas5711", (kernel_ulong_t
) &tas5711_chip
},
611 { "tas5717", (kernel_ulong_t
) &tas5717_chip
},
612 { "tas5719", (kernel_ulong_t
) &tas5717_chip
},
613 { "tas5721", (kernel_ulong_t
) &tas5721_chip
},
616 MODULE_DEVICE_TABLE(i2c
, tas571x_i2c_id
);
618 static struct i2c_driver tas571x_i2c_driver
= {
621 .of_match_table
= of_match_ptr(tas571x_of_match
),
623 .probe
= tas571x_i2c_probe
,
624 .remove
= tas571x_i2c_remove
,
625 .id_table
= tas571x_i2c_id
,
627 module_i2c_driver(tas571x_i2c_driver
);
629 MODULE_DESCRIPTION("ASoC TAS571x driver");
630 MODULE_AUTHOR("Kevin Cernekee <cernekee@chromium.org>");
631 MODULE_LICENSE("GPL");