]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/codecs/tas2552.c
ASoC: tas2552: Simplify and reverse the functionality of tas2552_sw_shutdown
[karo-tx-linux.git] / sound / soc / codecs / tas2552.c
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
40 static 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
65 static const char *tas2552_supply_names[TAS2552_NUM_SUPPLIES] = {
66         "vbat",         /* vbat voltage */
67         "iovdd",        /* I/O Voltage */
68         "avdd",         /* Analog DAC Voltage */
69 };
70
71 struct 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
81 /* Input mux controls */
82 static const char *tas2552_input_texts[] = {
83         "Digital", "Analog"
84 };
85
86 static SOC_ENUM_SINGLE_DECL(tas2552_input_mux_enum, TAS2552_CFG_3, 7,
87                             tas2552_input_texts);
88
89 static const struct snd_kcontrol_new tas2552_input_mux_control[] = {
90         SOC_DAPM_ENUM("Input selection", tas2552_input_mux_enum)
91 };
92
93 static 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
109 static 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
118 #ifdef CONFIG_PM
119 static void tas2552_sw_shutdown(struct tas2552_data *tas_data, int sw_shutdown)
120 {
121         u8 cfg1_reg = 0;
122
123         if (!tas_data->codec)
124                 return;
125
126         if (sw_shutdown)
127                 cfg1_reg = TAS2552_SWS;
128
129         snd_soc_update_bits(tas_data->codec, TAS2552_CFG_1, TAS2552_SWS,
130                             cfg1_reg);
131 }
132 #endif
133
134 static int tas2552_hw_params(struct snd_pcm_substream *substream,
135                              struct snd_pcm_hw_params *params,
136                              struct snd_soc_dai *dai)
137 {
138         struct snd_soc_codec *codec = dai->codec;
139         struct tas2552_data *tas2552 = dev_get_drvdata(codec->dev);
140         int sample_rate, pll_clk;
141         int d;
142         u8 p, j;
143
144         if (!tas2552->mclk)
145                 return -EINVAL;
146
147         snd_soc_update_bits(codec, TAS2552_CFG_2, TAS2552_PLL_ENABLE, 0);
148
149         if (tas2552->mclk == TAS2552_245MHZ_CLK ||
150                 tas2552->mclk == TAS2552_225MHZ_CLK) {
151                 /* By pass the PLL configuration */
152                 snd_soc_update_bits(codec, TAS2552_PLL_CTRL_2,
153                                     TAS2552_PLL_BYPASS_MASK,
154                                     TAS2552_PLL_BYPASS);
155         } else {
156                 /* Fill in the PLL control registers for J & D
157                  * PLL_CLK = (.5 * freq * J.D) / 2^p
158                  * Need to fill in J and D here based on incoming freq
159                  */
160                 p = snd_soc_read(codec, TAS2552_PLL_CTRL_1);
161                 p = (p >> 7);
162                 sample_rate = params_rate(params);
163
164                 if (sample_rate == 48000)
165                         pll_clk = TAS2552_245MHZ_CLK;
166                 else if (sample_rate == 44100)
167                         pll_clk = TAS2552_225MHZ_CLK;
168                 else {
169                         dev_vdbg(codec->dev, "Substream sample rate is not found %i\n",
170                                         params_rate(params));
171                         return -EINVAL;
172                 }
173
174                 j = (pll_clk * 2 * (1 << p)) / tas2552->mclk;
175                 d = (pll_clk * 2 * (1 << p)) % tas2552->mclk;
176
177                 snd_soc_update_bits(codec, TAS2552_PLL_CTRL_1,
178                                 TAS2552_PLL_J_MASK, j);
179                 snd_soc_write(codec, TAS2552_PLL_CTRL_2,
180                                         (d >> 7) & TAS2552_PLL_D_UPPER_MASK);
181                 snd_soc_write(codec, TAS2552_PLL_CTRL_3,
182                                 d & TAS2552_PLL_D_LOWER_MASK);
183
184         }
185
186         return 0;
187 }
188
189 static int tas2552_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
190 {
191         struct snd_soc_codec *codec = dai->codec;
192         u8 serial_format;
193         u8 serial_control_mask;
194
195         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
196         case SND_SOC_DAIFMT_CBS_CFS:
197                 serial_format = 0x00;
198                 break;
199         case SND_SOC_DAIFMT_CBS_CFM:
200                 serial_format = TAS2552_WORD_CLK_MASK;
201                 break;
202         case SND_SOC_DAIFMT_CBM_CFS:
203                 serial_format = TAS2552_BIT_CLK_MASK;
204                 break;
205         case SND_SOC_DAIFMT_CBM_CFM:
206                 serial_format = (TAS2552_BIT_CLK_MASK | TAS2552_WORD_CLK_MASK);
207                 break;
208         default:
209                 dev_vdbg(codec->dev, "DAI Format master is not found\n");
210                 return -EINVAL;
211         }
212
213         serial_control_mask = TAS2552_BIT_CLK_MASK | TAS2552_WORD_CLK_MASK;
214
215         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
216         case SND_SOC_DAIFMT_I2S:
217                 serial_format &= TAS2552_DAIFMT_I2S_MASK;
218                 break;
219         case SND_SOC_DAIFMT_DSP_A:
220                 serial_format |= TAS2552_DAIFMT_DSP;
221                 break;
222         case SND_SOC_DAIFMT_RIGHT_J:
223                 serial_format |= TAS2552_DAIFMT_RIGHT_J;
224                 break;
225         case SND_SOC_DAIFMT_LEFT_J:
226                 serial_format |= TAS2552_DAIFMT_LEFT_J;
227                 break;
228         default:
229                 dev_vdbg(codec->dev, "DAI Format is not found\n");
230                 return -EINVAL;
231         }
232
233         if (fmt & SND_SOC_DAIFMT_FORMAT_MASK)
234                 serial_control_mask |= TAS2552_DATA_FORMAT_MASK;
235
236         snd_soc_update_bits(codec, TAS2552_SER_CTRL_1, serial_control_mask,
237                                                 serial_format);
238
239         return 0;
240 }
241
242 static int tas2552_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
243                                   unsigned int freq, int dir)
244 {
245         struct snd_soc_codec *codec = dai->codec;
246         struct tas2552_data *tas2552 = dev_get_drvdata(codec->dev);
247
248         tas2552->mclk = freq;
249
250         return 0;
251 }
252
253 static int tas2552_mute(struct snd_soc_dai *dai, int mute)
254 {
255         u8 cfg1_reg = 0;
256         struct snd_soc_codec *codec = dai->codec;
257
258         if (mute)
259                 cfg1_reg |= TAS2552_MUTE;
260
261         snd_soc_update_bits(codec, TAS2552_CFG_1, TAS2552_MUTE, cfg1_reg);
262
263         return 0;
264 }
265
266 #ifdef CONFIG_PM
267 static int tas2552_runtime_suspend(struct device *dev)
268 {
269         struct tas2552_data *tas2552 = dev_get_drvdata(dev);
270
271         tas2552_sw_shutdown(tas2552, 1);
272
273         regcache_cache_only(tas2552->regmap, true);
274         regcache_mark_dirty(tas2552->regmap);
275
276         if (tas2552->enable_gpio)
277                 gpiod_set_value(tas2552->enable_gpio, 0);
278
279         return 0;
280 }
281
282 static int tas2552_runtime_resume(struct device *dev)
283 {
284         struct tas2552_data *tas2552 = dev_get_drvdata(dev);
285
286         if (tas2552->enable_gpio)
287                 gpiod_set_value(tas2552->enable_gpio, 1);
288
289         tas2552_sw_shutdown(tas2552, 0);
290
291         regcache_cache_only(tas2552->regmap, false);
292         regcache_sync(tas2552->regmap);
293
294         return 0;
295 }
296 #endif
297
298 static const struct dev_pm_ops tas2552_pm = {
299         SET_RUNTIME_PM_OPS(tas2552_runtime_suspend, tas2552_runtime_resume,
300                            NULL)
301 };
302
303 static struct snd_soc_dai_ops tas2552_speaker_dai_ops = {
304         .hw_params      = tas2552_hw_params,
305         .set_sysclk     = tas2552_set_dai_sysclk,
306         .set_fmt        = tas2552_set_dai_fmt,
307         .digital_mute = tas2552_mute,
308 };
309
310 /* Formats supported by TAS2552 driver. */
311 #define TAS2552_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
312                          SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
313
314 /* TAS2552 dai structure. */
315 static struct snd_soc_dai_driver tas2552_dai[] = {
316         {
317                 .name = "tas2552-amplifier",
318                 .playback = {
319                         .stream_name = "Playback",
320                         .channels_min = 2,
321                         .channels_max = 2,
322                         .rates = SNDRV_PCM_RATE_8000_192000,
323                         .formats = TAS2552_FORMATS,
324                 },
325                 .ops = &tas2552_speaker_dai_ops,
326         },
327 };
328
329 /*
330  * DAC digital volumes. From -7 to 24 dB in 1 dB steps
331  */
332 static DECLARE_TLV_DB_SCALE(dac_tlv, -7, 100, 24);
333
334 static const struct snd_kcontrol_new tas2552_snd_controls[] = {
335         SOC_SINGLE_TLV("Speaker Driver Playback Volume",
336                          TAS2552_PGA_GAIN, 0, 0x1f, 1, dac_tlv),
337 };
338
339 static const struct reg_default tas2552_init_regs[] = {
340         { TAS2552_RESERVED_0D, 0xc0 },
341 };
342
343 static int tas2552_codec_probe(struct snd_soc_codec *codec)
344 {
345         struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
346         int ret;
347
348         tas2552->codec = codec;
349
350         ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies),
351                                     tas2552->supplies);
352
353         if (ret != 0) {
354                 dev_err(codec->dev, "Failed to enable supplies: %d\n",
355                         ret);
356                 return ret;
357         }
358
359         if (tas2552->enable_gpio)
360                 gpiod_set_value(tas2552->enable_gpio, 1);
361
362         ret = pm_runtime_get_sync(codec->dev);
363         if (ret < 0) {
364                 dev_err(codec->dev, "Enabling device failed: %d\n",
365                         ret);
366                 goto probe_fail;
367         }
368
369         snd_soc_write(codec, TAS2552_CFG_1, TAS2552_MUTE |
370                                 TAS2552_PLL_SRC_BCLK);
371         snd_soc_write(codec, TAS2552_CFG_3, TAS2552_I2S_OUT_SEL |
372                                 TAS2552_DIN_SRC_SEL_AVG_L_R | TAS2552_88_96KHZ);
373         snd_soc_write(codec, TAS2552_DOUT, TAS2552_PDM_DATA_I);
374         snd_soc_write(codec, TAS2552_OUTPUT_DATA, TAS2552_PDM_DATA_V_I | 0x8);
375         snd_soc_write(codec, TAS2552_PDM_CFG, TAS2552_PDM_CLK_SEL_PLL);
376         snd_soc_write(codec, TAS2552_BOOST_PT_CTRL, TAS2552_APT_DELAY_200 |
377                                 TAS2552_APT_THRESH_2_1_7);
378
379         ret = regmap_register_patch(tas2552->regmap, tas2552_init_regs,
380                                             ARRAY_SIZE(tas2552_init_regs));
381         if (ret != 0) {
382                 dev_err(codec->dev, "Failed to write init registers: %d\n",
383                         ret);
384                 goto patch_fail;
385         }
386
387         snd_soc_write(codec, TAS2552_CFG_2, TAS2552_BOOST_EN |
388                                   TAS2552_APT_EN | TAS2552_LIM_EN);
389
390         return 0;
391
392 patch_fail:
393         pm_runtime_put(codec->dev);
394 probe_fail:
395         if (tas2552->enable_gpio)
396                 gpiod_set_value(tas2552->enable_gpio, 0);
397
398         regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies),
399                                         tas2552->supplies);
400         return -EIO;
401 }
402
403 static int tas2552_codec_remove(struct snd_soc_codec *codec)
404 {
405         struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
406
407         pm_runtime_put(codec->dev);
408
409         if (tas2552->enable_gpio)
410                 gpiod_set_value(tas2552->enable_gpio, 0);
411
412         return 0;
413 };
414
415 #ifdef CONFIG_PM
416 static int tas2552_suspend(struct snd_soc_codec *codec)
417 {
418         struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
419         int ret;
420
421         ret = regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies),
422                                         tas2552->supplies);
423
424         if (ret != 0)
425                 dev_err(codec->dev, "Failed to disable supplies: %d\n",
426                         ret);
427         return 0;
428 }
429
430 static int tas2552_resume(struct snd_soc_codec *codec)
431 {
432         struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
433         int ret;
434
435         ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies),
436                                     tas2552->supplies);
437
438         if (ret != 0) {
439                 dev_err(codec->dev, "Failed to enable supplies: %d\n",
440                         ret);
441         }
442
443         return 0;
444 }
445 #else
446 #define tas2552_suspend NULL
447 #define tas2552_resume NULL
448 #endif
449
450 static struct snd_soc_codec_driver soc_codec_dev_tas2552 = {
451         .probe = tas2552_codec_probe,
452         .remove = tas2552_codec_remove,
453         .suspend =      tas2552_suspend,
454         .resume = tas2552_resume,
455         .controls = tas2552_snd_controls,
456         .num_controls = ARRAY_SIZE(tas2552_snd_controls),
457         .dapm_widgets = tas2552_dapm_widgets,
458         .num_dapm_widgets = ARRAY_SIZE(tas2552_dapm_widgets),
459         .dapm_routes = tas2552_audio_map,
460         .num_dapm_routes = ARRAY_SIZE(tas2552_audio_map),
461 };
462
463 static const struct regmap_config tas2552_regmap_config = {
464         .reg_bits = 8,
465         .val_bits = 8,
466
467         .max_register = TAS2552_MAX_REG,
468         .reg_defaults = tas2552_reg_defs,
469         .num_reg_defaults = ARRAY_SIZE(tas2552_reg_defs),
470         .cache_type = REGCACHE_RBTREE,
471 };
472
473 static int tas2552_probe(struct i2c_client *client,
474                            const struct i2c_device_id *id)
475 {
476         struct device *dev;
477         struct tas2552_data *data;
478         int ret;
479         int i;
480
481         dev = &client->dev;
482         data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
483         if (data == NULL)
484                 return -ENOMEM;
485
486         data->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
487         if (IS_ERR(data->enable_gpio)) {
488                 if (PTR_ERR(data->enable_gpio) == -EPROBE_DEFER)
489                         return -EPROBE_DEFER;
490
491                 data->enable_gpio = NULL;;
492         }
493
494         data->tas2552_client = client;
495         data->regmap = devm_regmap_init_i2c(client, &tas2552_regmap_config);
496         if (IS_ERR(data->regmap)) {
497                 ret = PTR_ERR(data->regmap);
498                 dev_err(&client->dev, "Failed to allocate register map: %d\n",
499                         ret);
500                 return ret;
501         }
502
503         for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
504                 data->supplies[i].supply = tas2552_supply_names[i];
505
506         ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
507                                       data->supplies);
508         if (ret != 0) {
509                 dev_err(dev, "Failed to request supplies: %d\n", ret);
510                 return ret;
511         }
512
513         pm_runtime_set_active(&client->dev);
514         pm_runtime_set_autosuspend_delay(&client->dev, 1000);
515         pm_runtime_use_autosuspend(&client->dev);
516         pm_runtime_enable(&client->dev);
517         pm_runtime_mark_last_busy(&client->dev);
518         pm_runtime_put_sync_autosuspend(&client->dev);
519
520         dev_set_drvdata(&client->dev, data);
521
522         ret = snd_soc_register_codec(&client->dev,
523                                       &soc_codec_dev_tas2552,
524                                       tas2552_dai, ARRAY_SIZE(tas2552_dai));
525         if (ret < 0)
526                 dev_err(&client->dev, "Failed to register codec: %d\n", ret);
527
528         return ret;
529 }
530
531 static int tas2552_i2c_remove(struct i2c_client *client)
532 {
533         snd_soc_unregister_codec(&client->dev);
534         return 0;
535 }
536
537 static const struct i2c_device_id tas2552_id[] = {
538         { "tas2552", 0 },
539         { }
540 };
541 MODULE_DEVICE_TABLE(i2c, tas2552_id);
542
543 #if IS_ENABLED(CONFIG_OF)
544 static const struct of_device_id tas2552_of_match[] = {
545         { .compatible = "ti,tas2552", },
546         {},
547 };
548 MODULE_DEVICE_TABLE(of, tas2552_of_match);
549 #endif
550
551 static struct i2c_driver tas2552_i2c_driver = {
552         .driver = {
553                 .name = "tas2552",
554                 .owner = THIS_MODULE,
555                 .of_match_table = of_match_ptr(tas2552_of_match),
556                 .pm = &tas2552_pm,
557         },
558         .probe = tas2552_probe,
559         .remove = tas2552_i2c_remove,
560         .id_table = tas2552_id,
561 };
562
563 module_i2c_driver(tas2552_i2c_driver);
564
565 MODULE_AUTHOR("Dan Muprhy <dmurphy@ti.com>");
566 MODULE_DESCRIPTION("TAS2552 Audio amplifier driver");
567 MODULE_LICENSE("GPL");