ASoC: tas2552: Add DAPM calls for amp and PLL
[deliverable/linux.git] / sound / soc / codecs / tas2552.c
CommitLineData
5df7f71d
DM
1/*
2 * tas2552.c - ALSA SoC Texas Instruments TAS2552 Mono Audio Amplifier
3 *
4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Author: Dan Murphy <dmurphy@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/device.h>
21#include <linux/i2c.h>
22#include <linux/gpio.h>
23#include <linux/of_gpio.h>
24#include <linux/pm_runtime.h>
25#include <linux/regmap.h>
26#include <linux/slab.h>
27
28#include <linux/gpio/consumer.h>
29#include <linux/regulator/consumer.h>
30
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/soc.h>
34#include <sound/soc-dapm.h>
35#include <sound/tlv.h>
36#include <sound/tas2552-plat.h>
37
38#include "tas2552.h"
39
40static struct reg_default tas2552_reg_defs[] = {
41 {TAS2552_CFG_1, 0x22},
42 {TAS2552_CFG_3, 0x80},
43 {TAS2552_DOUT, 0x00},
44 {TAS2552_OUTPUT_DATA, 0xc0},
45 {TAS2552_PDM_CFG, 0x01},
46 {TAS2552_PGA_GAIN, 0x00},
47 {TAS2552_BOOST_PT_CTRL, 0x0f},
48 {TAS2552_RESERVED_0D, 0x00},
49 {TAS2552_LIMIT_RATE_HYS, 0x08},
50 {TAS2552_CFG_2, 0xef},
51 {TAS2552_SER_CTRL_1, 0x00},
52 {TAS2552_SER_CTRL_2, 0x00},
53 {TAS2552_PLL_CTRL_1, 0x10},
54 {TAS2552_PLL_CTRL_2, 0x00},
55 {TAS2552_PLL_CTRL_3, 0x00},
56 {TAS2552_BTIP, 0x8f},
57 {TAS2552_BTS_CTRL, 0x80},
58 {TAS2552_LIMIT_RELEASE, 0x04},
59 {TAS2552_LIMIT_INT_COUNT, 0x00},
60 {TAS2552_EDGE_RATE_CTRL, 0x40},
61 {TAS2552_VBAT_DATA, 0x00},
62};
63
64#define TAS2552_NUM_SUPPLIES 3
65static const char *tas2552_supply_names[TAS2552_NUM_SUPPLIES] = {
66 "vbat", /* vbat voltage */
67 "iovdd", /* I/O Voltage */
68 "avdd", /* Analog DAC Voltage */
69};
70
71struct tas2552_data {
72 struct snd_soc_codec *codec;
73 struct regmap *regmap;
74 struct i2c_client *tas2552_client;
75 struct regulator_bulk_data supplies[TAS2552_NUM_SUPPLIES];
76 struct gpio_desc *enable_gpio;
77 unsigned char regs[TAS2552_VBAT_DATA];
78 unsigned int mclk;
79};
80
a7a8e994
DM
81/* Input mux controls */
82static const char *tas2552_input_texts[] = {
83 "Digital", "Analog"
84};
85
86static SOC_ENUM_SINGLE_DECL(tas2552_input_mux_enum, TAS2552_CFG_3, 7,
87 tas2552_input_texts);
88
89static const struct snd_kcontrol_new tas2552_input_mux_control[] = {
90 SOC_DAPM_ENUM("Input selection", tas2552_input_mux_enum)
91};
92
93static const struct snd_soc_dapm_widget tas2552_dapm_widgets[] =
94{
95 SND_SOC_DAPM_INPUT("IN"),
96
97 /* MUX Controls */
98 SND_SOC_DAPM_MUX("Input selection", SND_SOC_NOPM, 0, 0,
99 tas2552_input_mux_control),
100
101 SND_SOC_DAPM_AIF_IN("DAC IN", "DAC Playback", 0, SND_SOC_NOPM, 0, 0),
102 SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
103 SND_SOC_DAPM_OUT_DRV("ClassD", TAS2552_CFG_2, 7, 0, NULL, 0),
104 SND_SOC_DAPM_SUPPLY("PLL", TAS2552_CFG_2, 3, 0, NULL, 0),
105
106 SND_SOC_DAPM_OUTPUT("OUT")
107};
108
109static const struct snd_soc_dapm_route tas2552_audio_map[] = {
110 {"DAC", NULL, "DAC IN"},
111 {"Input selection", "Digital", "DAC"},
112 {"Input selection", "Analog", "IN"},
113 {"ClassD", NULL, "Input selection"},
114 {"OUT", NULL, "ClassD"},
115 {"ClassD", NULL, "PLL"},
116};
117
5df7f71d
DM
118static void tas2552_sw_shutdown(struct tas2552_data *tas_data, int sw_shutdown)
119{
120 u8 cfg1_reg;
121
122 if (sw_shutdown)
123 cfg1_reg = 0;
124 else
125 cfg1_reg = TAS2552_SWS_MASK;
126
127 snd_soc_update_bits(tas_data->codec, TAS2552_CFG_1,
128 TAS2552_SWS_MASK, cfg1_reg);
129}
130
131static int tas2552_hw_params(struct snd_pcm_substream *substream,
132 struct snd_pcm_hw_params *params,
133 struct snd_soc_dai *dai)
134{
135 struct snd_soc_codec *codec = dai->codec;
136 struct tas2552_data *tas2552 = dev_get_drvdata(codec->dev);
137 int sample_rate, pll_clk;
138 int d;
139 u8 p, j;
140
5df7f71d
DM
141 if (!tas2552->mclk)
142 return -EINVAL;
143
144 snd_soc_update_bits(codec, TAS2552_CFG_2, TAS2552_PLL_ENABLE, 0);
145
146 if (tas2552->mclk == TAS2552_245MHZ_CLK ||
147 tas2552->mclk == TAS2552_225MHZ_CLK) {
148 /* By pass the PLL configuration */
149 snd_soc_update_bits(codec, TAS2552_PLL_CTRL_2,
150 TAS2552_PLL_BYPASS_MASK,
151 TAS2552_PLL_BYPASS);
152 } else {
153 /* Fill in the PLL control registers for J & D
154 * PLL_CLK = (.5 * freq * J.D) / 2^p
155 * Need to fill in J and D here based on incoming freq
156 */
157 p = snd_soc_read(codec, TAS2552_PLL_CTRL_1);
158 p = (p >> 7);
159 sample_rate = params_rate(params);
160
161 if (sample_rate == 48000)
162 pll_clk = TAS2552_245MHZ_CLK;
163 else if (sample_rate == 44100)
164 pll_clk = TAS2552_225MHZ_CLK;
165 else {
166 dev_vdbg(codec->dev, "Substream sample rate is not found %i\n",
167 params_rate(params));
168 return -EINVAL;
169 }
170
171 j = (pll_clk * 2 * (1 << p)) / tas2552->mclk;
172 d = (pll_clk * 2 * (1 << p)) % tas2552->mclk;
173
174 snd_soc_update_bits(codec, TAS2552_PLL_CTRL_1,
175 TAS2552_PLL_J_MASK, j);
176 snd_soc_write(codec, TAS2552_PLL_CTRL_2,
177 (d >> 7) & TAS2552_PLL_D_UPPER_MASK);
178 snd_soc_write(codec, TAS2552_PLL_CTRL_3,
179 d & TAS2552_PLL_D_LOWER_MASK);
180
181 }
182
5df7f71d
DM
183 return 0;
184}
185
186static int tas2552_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
187{
188 struct snd_soc_codec *codec = dai->codec;
189 u8 serial_format;
190 u8 serial_control_mask;
191
192 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
193 case SND_SOC_DAIFMT_CBS_CFS:
194 serial_format = 0x00;
195 break;
196 case SND_SOC_DAIFMT_CBS_CFM:
197 serial_format = TAS2552_WORD_CLK_MASK;
198 break;
199 case SND_SOC_DAIFMT_CBM_CFS:
200 serial_format = TAS2552_BIT_CLK_MASK;
201 break;
202 case SND_SOC_DAIFMT_CBM_CFM:
203 serial_format = (TAS2552_BIT_CLK_MASK | TAS2552_WORD_CLK_MASK);
204 break;
205 default:
206 dev_vdbg(codec->dev, "DAI Format master is not found\n");
207 return -EINVAL;
208 }
209
210 serial_control_mask = TAS2552_BIT_CLK_MASK | TAS2552_WORD_CLK_MASK;
211
212 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
213 case SND_SOC_DAIFMT_I2S:
214 serial_format &= TAS2552_DAIFMT_I2S_MASK;
215 break;
216 case SND_SOC_DAIFMT_DSP_A:
217 serial_format |= TAS2552_DAIFMT_DSP;
218 break;
219 case SND_SOC_DAIFMT_RIGHT_J:
220 serial_format |= TAS2552_DAIFMT_RIGHT_J;
221 break;
222 case SND_SOC_DAIFMT_LEFT_J:
223 serial_format |= TAS2552_DAIFMT_LEFT_J;
224 break;
225 default:
226 dev_vdbg(codec->dev, "DAI Format is not found\n");
227 return -EINVAL;
228 }
229
230 if (fmt & SND_SOC_DAIFMT_FORMAT_MASK)
231 serial_control_mask |= TAS2552_DATA_FORMAT_MASK;
232
233 snd_soc_update_bits(codec, TAS2552_SER_CTRL_1, serial_control_mask,
234 serial_format);
235
236 return 0;
237}
238
239static int tas2552_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
240 unsigned int freq, int dir)
241{
242 struct snd_soc_codec *codec = dai->codec;
243 struct tas2552_data *tas2552 = dev_get_drvdata(codec->dev);
244
245 tas2552->mclk = freq;
246
247 return 0;
248}
249
250static int tas2552_mute(struct snd_soc_dai *dai, int mute)
251{
252 u8 cfg1_reg;
253 struct snd_soc_codec *codec = dai->codec;
254
255 if (mute)
256 cfg1_reg = TAS2552_MUTE_MASK;
257 else
258 cfg1_reg = ~TAS2552_MUTE_MASK;
259
260 snd_soc_update_bits(codec, TAS2552_CFG_1, TAS2552_MUTE_MASK, cfg1_reg);
261
262 return 0;
263}
264
265#ifdef CONFIG_PM_RUNTIME
266static int tas2552_runtime_suspend(struct device *dev)
267{
268 struct tas2552_data *tas2552 = dev_get_drvdata(dev);
269
270 tas2552_sw_shutdown(tas2552, 0);
271
5df7f71d
DM
272 regcache_cache_only(tas2552->regmap, true);
273 regcache_mark_dirty(tas2552->regmap);
274
e295a4a4
DM
275 if (tas2552->enable_gpio)
276 gpiod_set_value(tas2552->enable_gpio, 0);
277
5df7f71d
DM
278 return 0;
279}
280
281static int tas2552_runtime_resume(struct device *dev)
282{
283 struct tas2552_data *tas2552 = dev_get_drvdata(dev);
284
285 if (tas2552->enable_gpio)
286 gpiod_set_value(tas2552->enable_gpio, 1);
287
288 tas2552_sw_shutdown(tas2552, 1);
289
290 regcache_cache_only(tas2552->regmap, false);
291 regcache_sync(tas2552->regmap);
292
293 return 0;
294}
295#endif
296
297static const struct dev_pm_ops tas2552_pm = {
298 SET_RUNTIME_PM_OPS(tas2552_runtime_suspend, tas2552_runtime_resume,
299 NULL)
300};
301
5df7f71d
DM
302static struct snd_soc_dai_ops tas2552_speaker_dai_ops = {
303 .hw_params = tas2552_hw_params,
304 .set_sysclk = tas2552_set_dai_sysclk,
305 .set_fmt = tas2552_set_dai_fmt,
5df7f71d
DM
306 .digital_mute = tas2552_mute,
307};
308
309/* Formats supported by TAS2552 driver. */
310#define TAS2552_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
311 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
312
313/* TAS2552 dai structure. */
314static struct snd_soc_dai_driver tas2552_dai[] = {
315 {
316 .name = "tas2552-amplifier",
317 .playback = {
a7a8e994 318 .stream_name = "Playback",
5df7f71d
DM
319 .channels_min = 2,
320 .channels_max = 2,
321 .rates = SNDRV_PCM_RATE_8000_192000,
322 .formats = TAS2552_FORMATS,
323 },
324 .ops = &tas2552_speaker_dai_ops,
325 },
326};
327
328/*
329 * DAC digital volumes. From -7 to 24 dB in 1 dB steps
330 */
331static DECLARE_TLV_DB_SCALE(dac_tlv, -7, 100, 24);
332
333static const struct snd_kcontrol_new tas2552_snd_controls[] = {
334 SOC_SINGLE_TLV("Speaker Driver Playback Volume",
335 TAS2552_PGA_GAIN, 0, 0x1f, 1, dac_tlv),
a7a8e994 336 SOC_DAPM_SINGLE("Playback AMP", SND_SOC_NOPM, 0, 1, 0),
5df7f71d
DM
337};
338
339static const struct reg_default tas2552_init_regs[] = {
340 { TAS2552_RESERVED_0D, 0xc0 },
341};
342
343static int tas2552_codec_probe(struct snd_soc_codec *codec)
344{
345 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
a7a8e994 346 struct snd_soc_dapm_context *dapm = &codec->dapm;
5df7f71d
DM
347 int ret;
348
349 tas2552->codec = codec;
350
351 ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies),
352 tas2552->supplies);
353
354 if (ret != 0) {
355 dev_err(codec->dev, "Failed to enable supplies: %d\n",
356 ret);
357 return ret;
358 }
359
360 if (tas2552->enable_gpio)
361 gpiod_set_value(tas2552->enable_gpio, 1);
362
363 ret = pm_runtime_get_sync(codec->dev);
364 if (ret < 0) {
365 dev_err(codec->dev, "Enabling device failed: %d\n",
366 ret);
367 goto probe_fail;
368 }
369
370 snd_soc_write(codec, TAS2552_CFG_1, TAS2552_MUTE_MASK |
371 TAS2552_PLL_SRC_BCLK);
372 snd_soc_write(codec, TAS2552_CFG_3, TAS2552_I2S_OUT_SEL |
373 TAS2552_DIN_SRC_SEL_AVG_L_R | TAS2552_88_96KHZ);
374 snd_soc_write(codec, TAS2552_DOUT, TAS2552_PDM_DATA_I);
375 snd_soc_write(codec, TAS2552_OUTPUT_DATA, TAS2552_PDM_DATA_V_I | 0x8);
376 snd_soc_write(codec, TAS2552_PDM_CFG, TAS2552_PDM_BCLK_SEL);
377 snd_soc_write(codec, TAS2552_BOOST_PT_CTRL, TAS2552_APT_DELAY_200 |
378 TAS2552_APT_THRESH_2_1_7);
379
380 ret = regmap_register_patch(tas2552->regmap, tas2552_init_regs,
381 ARRAY_SIZE(tas2552_init_regs));
382 if (ret != 0) {
383 dev_err(codec->dev, "Failed to write init registers: %d\n",
384 ret);
385 goto patch_fail;
386 }
387
a7a8e994
DM
388 snd_soc_write(codec, TAS2552_CFG_2, TAS2552_BOOST_EN |
389 TAS2552_APT_EN | TAS2552_LIM_EN);
390
391 snd_soc_dapm_new_controls(dapm, tas2552_dapm_widgets,
392 ARRAY_SIZE(tas2552_dapm_widgets));
393 snd_soc_dapm_add_routes(dapm, tas2552_audio_map,
394 ARRAY_SIZE(tas2552_audio_map));
395
5df7f71d
DM
396 return 0;
397
398patch_fail:
399 pm_runtime_put(codec->dev);
400probe_fail:
401 if (tas2552->enable_gpio)
402 gpiod_set_value(tas2552->enable_gpio, 0);
403
404 regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies),
405 tas2552->supplies);
406 return -EIO;
407}
408
409static int tas2552_codec_remove(struct snd_soc_codec *codec)
410{
411 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
412
e295a4a4
DM
413 pm_runtime_put(codec->dev);
414
5df7f71d
DM
415 if (tas2552->enable_gpio)
416 gpiod_set_value(tas2552->enable_gpio, 0);
417
418 return 0;
419};
420
421#ifdef CONFIG_PM
422static int tas2552_suspend(struct snd_soc_codec *codec)
423{
424 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
425 int ret;
426
427 ret = regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies),
428 tas2552->supplies);
429
430 if (ret != 0)
431 dev_err(codec->dev, "Failed to disable supplies: %d\n",
432 ret);
433 return 0;
434}
435
436static int tas2552_resume(struct snd_soc_codec *codec)
437{
438 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
439 int ret;
440
441 ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies),
442 tas2552->supplies);
443
444 if (ret != 0) {
445 dev_err(codec->dev, "Failed to enable supplies: %d\n",
446 ret);
447 }
448
449 return 0;
450}
451#else
452#define tas2552_suspend NULL
453#define tas2552_resume NULL
454#endif
455
456static struct snd_soc_codec_driver soc_codec_dev_tas2552 = {
457 .probe = tas2552_codec_probe,
458 .remove = tas2552_codec_remove,
459 .suspend = tas2552_suspend,
460 .resume = tas2552_resume,
461 .controls = tas2552_snd_controls,
462 .num_controls = ARRAY_SIZE(tas2552_snd_controls),
463};
464
465static const struct regmap_config tas2552_regmap_config = {
466 .reg_bits = 8,
467 .val_bits = 8,
468
469 .max_register = TAS2552_MAX_REG,
470 .reg_defaults = tas2552_reg_defs,
471 .num_reg_defaults = ARRAY_SIZE(tas2552_reg_defs),
472 .cache_type = REGCACHE_RBTREE,
473};
474
475static int tas2552_probe(struct i2c_client *client,
476 const struct i2c_device_id *id)
477{
478 struct device *dev;
479 struct tas2552_data *data;
480 int ret;
481 int i;
482
483 dev = &client->dev;
484 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
485 if (data == NULL)
486 return -ENOMEM;
487
488 data->enable_gpio = devm_gpiod_get(dev, "enable");
489 if (IS_ERR(data->enable_gpio)) {
490 ret = PTR_ERR(data->enable_gpio);
491 if (ret != -ENOENT && ret != -ENOSYS)
492 return ret;
493
494 data->enable_gpio = NULL;
495 } else {
496 gpiod_direction_output(data->enable_gpio, 0);
497 }
498
499 data->tas2552_client = client;
500 data->regmap = devm_regmap_init_i2c(client, &tas2552_regmap_config);
501 if (IS_ERR(data->regmap)) {
502 ret = PTR_ERR(data->regmap);
503 dev_err(&client->dev, "Failed to allocate register map: %d\n",
504 ret);
505 return ret;
506 }
507
508 for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
509 data->supplies[i].supply = tas2552_supply_names[i];
510
511 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
512 data->supplies);
c62f9d8f 513 if (ret != 0) {
5df7f71d 514 dev_err(dev, "Failed to request supplies: %d\n", ret);
c62f9d8f
AL
515 return ret;
516 }
5df7f71d
DM
517
518 pm_runtime_set_active(&client->dev);
519 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
520 pm_runtime_use_autosuspend(&client->dev);
521 pm_runtime_enable(&client->dev);
522 pm_runtime_mark_last_busy(&client->dev);
523 pm_runtime_put_sync_autosuspend(&client->dev);
524
525 dev_set_drvdata(&client->dev, data);
526
527 ret = snd_soc_register_codec(&client->dev,
528 &soc_codec_dev_tas2552,
529 tas2552_dai, ARRAY_SIZE(tas2552_dai));
530 if (ret < 0)
531 dev_err(&client->dev, "Failed to register codec: %d\n", ret);
532
c62f9d8f 533 return ret;
5df7f71d
DM
534}
535
536static int tas2552_i2c_remove(struct i2c_client *client)
537{
538 snd_soc_unregister_codec(&client->dev);
539 return 0;
540}
541
542static const struct i2c_device_id tas2552_id[] = {
543 { "tas2552", 0 },
544 { }
545};
546MODULE_DEVICE_TABLE(i2c, tas2552_id);
547
548#if IS_ENABLED(CONFIG_OF)
549static const struct of_device_id tas2552_of_match[] = {
550 { .compatible = "ti,tas2552", },
551 {},
552};
553MODULE_DEVICE_TABLE(of, tas2552_of_match);
554#endif
555
556static struct i2c_driver tas2552_i2c_driver = {
557 .driver = {
558 .name = "tas2552",
559 .owner = THIS_MODULE,
560 .of_match_table = of_match_ptr(tas2552_of_match),
561 .pm = &tas2552_pm,
562 },
563 .probe = tas2552_probe,
564 .remove = tas2552_i2c_remove,
565 .id_table = tas2552_id,
566};
567
568module_i2c_driver(tas2552_i2c_driver);
569
570MODULE_AUTHOR("Dan Muprhy <dmurphy@ti.com>");
571MODULE_DESCRIPTION("TAS2552 Audio amplifier driver");
572MODULE_LICENSE("GPL");
This page took 0.074905 seconds and 5 git commands to generate.