2 * TAS571x amplifier audio driver
4 * Copyright (C) 2015 Google, Inc.
5 * Copyright (c) 2013 Daniel Mack <zonque@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/stddef.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/tlv.h>
31 #define TAS571X_MAX_SUPPLIES 6
34 const char *const *supply_names
;
36 const struct snd_kcontrol_new
*controls
;
38 const struct regmap_config
*regmap_config
;
42 struct tas571x_private
{
43 const struct tas571x_chip
*chip
;
44 struct regmap
*regmap
;
45 struct regulator_bulk_data supplies
[TAS571X_MAX_SUPPLIES
];
48 struct gpio_desc
*reset_gpio
;
49 struct gpio_desc
*pdn_gpio
;
50 struct snd_soc_codec_driver codec_driver
;
53 static int tas571x_register_size(struct tas571x_private
*priv
, unsigned int reg
)
56 case TAS571X_MVOL_REG
:
57 case TAS571X_CH1_VOL_REG
:
58 case TAS571X_CH2_VOL_REG
:
59 return priv
->chip
->vol_reg_size
;
65 static int tas571x_reg_write(void *context
, unsigned int reg
,
68 struct i2c_client
*client
= context
;
69 struct tas571x_private
*priv
= i2c_get_clientdata(client
);
74 size
= tas571x_register_size(priv
, reg
);
77 for (i
= size
; i
>= 1; --i
) {
82 ret
= i2c_master_send(client
, buf
, size
+ 1);
91 static int tas571x_reg_read(void *context
, unsigned int reg
,
94 struct i2c_client
*client
= context
;
95 struct tas571x_private
*priv
= i2c_get_clientdata(client
);
96 uint8_t send_buf
, recv_buf
[4];
97 struct i2c_msg msgs
[2];
102 size
= tas571x_register_size(priv
, reg
);
105 msgs
[0].addr
= client
->addr
;
106 msgs
[0].len
= sizeof(send_buf
);
107 msgs
[0].buf
= &send_buf
;
110 msgs
[1].addr
= client
->addr
;
112 msgs
[1].buf
= recv_buf
;
113 msgs
[1].flags
= I2C_M_RD
;
115 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
118 else if (ret
!= ARRAY_SIZE(msgs
))
123 for (i
= 0; i
< size
; i
++) {
125 *value
|= recv_buf
[i
];
131 static int tas571x_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int format
)
133 struct tas571x_private
*priv
= snd_soc_codec_get_drvdata(dai
->codec
);
135 priv
->format
= format
;
140 static int tas571x_hw_params(struct snd_pcm_substream
*substream
,
141 struct snd_pcm_hw_params
*params
,
142 struct snd_soc_dai
*dai
)
144 struct tas571x_private
*priv
= snd_soc_codec_get_drvdata(dai
->codec
);
147 switch (priv
->format
& SND_SOC_DAIFMT_FORMAT_MASK
) {
148 case SND_SOC_DAIFMT_RIGHT_J
:
151 case SND_SOC_DAIFMT_I2S
:
154 case SND_SOC_DAIFMT_LEFT_J
:
161 if (params_width(params
) >= 24)
163 else if (params_width(params
) >= 20)
166 return regmap_update_bits(priv
->regmap
, TAS571X_SDI_REG
,
167 TAS571X_SDI_FMT_MASK
, val
);
170 static int tas571x_set_bias_level(struct snd_soc_codec
*codec
,
171 enum snd_soc_bias_level level
)
173 struct tas571x_private
*priv
= snd_soc_codec_get_drvdata(codec
);
177 case SND_SOC_BIAS_ON
:
179 case SND_SOC_BIAS_PREPARE
:
181 case SND_SOC_BIAS_STANDBY
:
182 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
183 if (!IS_ERR(priv
->mclk
)) {
184 ret
= clk_prepare_enable(priv
->mclk
);
187 "Failed to enable master clock: %d\n",
193 gpiod_set_value(priv
->pdn_gpio
, 0);
194 usleep_range(5000, 6000);
196 regcache_cache_only(priv
->regmap
, false);
197 ret
= regcache_sync(priv
->regmap
);
202 case SND_SOC_BIAS_OFF
:
203 regcache_cache_only(priv
->regmap
, true);
204 gpiod_set_value(priv
->pdn_gpio
, 1);
206 if (!IS_ERR(priv
->mclk
))
207 clk_disable_unprepare(priv
->mclk
);
211 codec
->dapm
.bias_level
= level
;
215 static const struct snd_soc_dai_ops tas571x_dai_ops
= {
216 .set_fmt
= tas571x_set_dai_fmt
,
217 .hw_params
= tas571x_hw_params
,
220 static const char *const tas5711_supply_names
[] = {
229 static const DECLARE_TLV_DB_SCALE(tas5711_volume_tlv
, -10350, 50, 1);
231 static const struct snd_kcontrol_new tas5711_controls
[] = {
232 SOC_SINGLE_TLV("Master Volume",
234 0, 0xff, 1, tas5711_volume_tlv
),
235 SOC_DOUBLE_R_TLV("Speaker Volume",
238 0, 0xff, 1, tas5711_volume_tlv
),
239 SOC_DOUBLE("Speaker Switch",
240 TAS571X_SOFT_MUTE_REG
,
241 TAS571X_SOFT_MUTE_CH1_SHIFT
, TAS571X_SOFT_MUTE_CH2_SHIFT
,
245 static const struct reg_default tas5711_reg_defaults
[] = {
255 static const struct regmap_config tas5711_regmap_config
= {
258 .max_register
= 0xff,
259 .reg_read
= tas571x_reg_read
,
260 .reg_write
= tas571x_reg_write
,
261 .reg_defaults
= tas5711_reg_defaults
,
262 .num_reg_defaults
= ARRAY_SIZE(tas5711_reg_defaults
),
263 .cache_type
= REGCACHE_RBTREE
,
266 static const struct tas571x_chip tas5711_chip
= {
267 .supply_names
= tas5711_supply_names
,
268 .num_supply_names
= ARRAY_SIZE(tas5711_supply_names
),
269 .controls
= tas5711_controls
,
270 .num_controls
= ARRAY_SIZE(tas5711_controls
),
271 .regmap_config
= &tas5711_regmap_config
,
275 static const char *const tas5717_supply_names
[] = {
283 static const DECLARE_TLV_DB_SCALE(tas5717_volume_tlv
, -10375, 25, 0);
285 static const struct snd_kcontrol_new tas5717_controls
[] = {
286 /* MVOL LSB is ignored - see comments in tas571x_i2c_probe() */
287 SOC_SINGLE_TLV("Master Volume",
288 TAS571X_MVOL_REG
, 1, 0x1ff, 1,
290 SOC_DOUBLE_R_TLV("Speaker Volume",
291 TAS571X_CH1_VOL_REG
, TAS571X_CH2_VOL_REG
,
292 1, 0x1ff, 1, tas5717_volume_tlv
),
293 SOC_DOUBLE("Speaker Switch",
294 TAS571X_SOFT_MUTE_REG
,
295 TAS571X_SOFT_MUTE_CH1_SHIFT
, TAS571X_SOFT_MUTE_CH2_SHIFT
,
299 static const struct reg_default tas5717_reg_defaults
[] = {
309 static const struct regmap_config tas5717_regmap_config
= {
312 .max_register
= 0xff,
313 .reg_read
= tas571x_reg_read
,
314 .reg_write
= tas571x_reg_write
,
315 .reg_defaults
= tas5717_reg_defaults
,
316 .num_reg_defaults
= ARRAY_SIZE(tas5717_reg_defaults
),
317 .cache_type
= REGCACHE_RBTREE
,
320 /* This entry is reused for tas5719 as the software interface is identical. */
321 static const struct tas571x_chip tas5717_chip
= {
322 .supply_names
= tas5717_supply_names
,
323 .num_supply_names
= ARRAY_SIZE(tas5717_supply_names
),
324 .controls
= tas5717_controls
,
325 .num_controls
= ARRAY_SIZE(tas5717_controls
),
326 .regmap_config
= &tas5717_regmap_config
,
330 static const struct snd_soc_dapm_widget tas571x_dapm_widgets
[] = {
331 SND_SOC_DAPM_DAC("DACL", NULL
, SND_SOC_NOPM
, 0, 0),
332 SND_SOC_DAPM_DAC("DACR", NULL
, SND_SOC_NOPM
, 0, 0),
334 SND_SOC_DAPM_OUTPUT("OUT_A"),
335 SND_SOC_DAPM_OUTPUT("OUT_B"),
336 SND_SOC_DAPM_OUTPUT("OUT_C"),
337 SND_SOC_DAPM_OUTPUT("OUT_D"),
340 static const struct snd_soc_dapm_route tas571x_dapm_routes
[] = {
341 { "DACL", NULL
, "Playback" },
342 { "DACR", NULL
, "Playback" },
344 { "OUT_A", NULL
, "DACL" },
345 { "OUT_B", NULL
, "DACL" },
346 { "OUT_C", NULL
, "DACR" },
347 { "OUT_D", NULL
, "DACR" },
350 static const struct snd_soc_codec_driver tas571x_codec
= {
351 .set_bias_level
= tas571x_set_bias_level
,
352 .idle_bias_off
= true,
354 .dapm_widgets
= tas571x_dapm_widgets
,
355 .num_dapm_widgets
= ARRAY_SIZE(tas571x_dapm_widgets
),
356 .dapm_routes
= tas571x_dapm_routes
,
357 .num_dapm_routes
= ARRAY_SIZE(tas571x_dapm_routes
),
360 static struct snd_soc_dai_driver tas571x_dai
= {
361 .name
= "tas571x-hifi",
363 .stream_name
= "Playback",
366 .rates
= SNDRV_PCM_RATE_8000_48000
,
367 .formats
= SNDRV_PCM_FMTBIT_S32_LE
|
368 SNDRV_PCM_FMTBIT_S24_LE
|
369 SNDRV_PCM_FMTBIT_S16_LE
,
371 .ops
= &tas571x_dai_ops
,
374 static const struct of_device_id tas571x_of_match
[];
376 static int tas571x_i2c_probe(struct i2c_client
*client
,
377 const struct i2c_device_id
*id
)
379 struct tas571x_private
*priv
;
380 struct device
*dev
= &client
->dev
;
383 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
386 i2c_set_clientdata(client
, priv
);
389 const struct of_device_id
*of_id
;
391 of_id
= of_match_device(tas571x_of_match
, dev
);
393 priv
->chip
= of_id
->data
;
397 dev_err(dev
, "Unknown device type\n");
401 priv
->mclk
= devm_clk_get(dev
, "mclk");
402 if (IS_ERR(priv
->mclk
) && PTR_ERR(priv
->mclk
) != -ENOENT
) {
403 dev_err(dev
, "Failed to request mclk: %ld\n",
404 PTR_ERR(priv
->mclk
));
405 return PTR_ERR(priv
->mclk
);
408 BUG_ON(priv
->chip
->num_supply_names
> TAS571X_MAX_SUPPLIES
);
409 for (i
= 0; i
< priv
->chip
->num_supply_names
; i
++)
410 priv
->supplies
[i
].supply
= priv
->chip
->supply_names
[i
];
412 ret
= devm_regulator_bulk_get(dev
, priv
->chip
->num_supply_names
,
415 dev_err(dev
, "Failed to get supplies: %d\n", ret
);
418 ret
= regulator_bulk_enable(priv
->chip
->num_supply_names
,
421 dev_err(dev
, "Failed to enable supplies: %d\n", ret
);
425 priv
->regmap
= devm_regmap_init(dev
, NULL
, client
,
426 priv
->chip
->regmap_config
);
427 if (IS_ERR(priv
->regmap
))
428 return PTR_ERR(priv
->regmap
);
430 priv
->pdn_gpio
= devm_gpiod_get_optional(dev
, "pdn", GPIOD_OUT_LOW
);
431 if (IS_ERR(priv
->pdn_gpio
)) {
432 dev_err(dev
, "error requesting pdn_gpio: %ld\n",
433 PTR_ERR(priv
->pdn_gpio
));
434 return PTR_ERR(priv
->pdn_gpio
);
437 priv
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
439 if (IS_ERR(priv
->reset_gpio
)) {
440 dev_err(dev
, "error requesting reset_gpio: %ld\n",
441 PTR_ERR(priv
->reset_gpio
));
442 return PTR_ERR(priv
->reset_gpio
);
443 } else if (priv
->reset_gpio
) {
444 /* pulse the active low reset line for ~100us */
445 usleep_range(100, 200);
446 gpiod_set_value(priv
->reset_gpio
, 0);
447 usleep_range(12000, 20000);
450 ret
= regmap_write(priv
->regmap
, TAS571X_OSC_TRIM_REG
, 0);
454 ret
= regmap_update_bits(priv
->regmap
, TAS571X_SYS_CTRL_2_REG
,
455 TAS571X_SYS_CTRL_2_SDN_MASK
, 0);
459 memcpy(&priv
->codec_driver
, &tas571x_codec
, sizeof(priv
->codec_driver
));
460 priv
->codec_driver
.controls
= priv
->chip
->controls
;
461 priv
->codec_driver
.num_controls
= priv
->chip
->num_controls
;
463 if (priv
->chip
->vol_reg_size
== 2) {
465 * The master volume defaults to 0x3ff (mute), but we ignore
466 * (zero) the LSB because the hardware step size is 0.125 dB
467 * and TLV_DB_SCALE_ITEM has a resolution of 0.01 dB.
469 ret
= regmap_update_bits(priv
->regmap
, TAS571X_MVOL_REG
, 1, 0);
474 regcache_cache_only(priv
->regmap
, true);
475 gpiod_set_value(priv
->pdn_gpio
, 1);
477 return snd_soc_register_codec(&client
->dev
, &priv
->codec_driver
,
481 static int tas571x_i2c_remove(struct i2c_client
*client
)
483 struct tas571x_private
*priv
= i2c_get_clientdata(client
);
485 snd_soc_unregister_codec(&client
->dev
);
486 regulator_bulk_disable(priv
->chip
->num_supply_names
, priv
->supplies
);
491 static const struct of_device_id tas571x_of_match
[] = {
492 { .compatible
= "ti,tas5711", .data
= &tas5711_chip
, },
493 { .compatible
= "ti,tas5717", .data
= &tas5717_chip
, },
494 { .compatible
= "ti,tas5719", .data
= &tas5717_chip
, },
497 MODULE_DEVICE_TABLE(of
, tas571x_of_match
);
499 static const struct i2c_device_id tas571x_i2c_id
[] = {
505 MODULE_DEVICE_TABLE(i2c
, tas571x_i2c_id
);
507 static struct i2c_driver tas571x_i2c_driver
= {
510 .of_match_table
= of_match_ptr(tas571x_of_match
),
512 .probe
= tas571x_i2c_probe
,
513 .remove
= tas571x_i2c_remove
,
514 .id_table
= tas571x_i2c_id
,
516 module_i2c_driver(tas571x_i2c_driver
);
518 MODULE_DESCRIPTION("ASoC TAS571x driver");
519 MODULE_AUTHOR("Kevin Cernekee <cernekee@chromium.org>");
520 MODULE_LICENSE("GPL");