Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / hifiberry_dacplus.c
1 /*
2  * ASoC Driver for HiFiBerry DAC+ / DAC Pro / AMP100
3  *
4  * Author:      Daniel Matuschek, Stuart MacLean <stuart@hifiberry.com>
5  *              Copyright 2014-2015
6  *              based on code by Florian Meier <florian.meier@koalo.de>
7  *              Headphone/AMP100 Joerg Schambacher <joerg@hifiberry.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  */
18
19 #include <linux/module.h>
20 #include <linux/gpio/consumer.h>
21 #include <../drivers/gpio/gpiolib.h>
22 #include <linux/platform_device.h>
23 #include <linux/kernel.h>
24 #include <linux/clk.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/i2c.h>
31
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/jack.h>
37
38 #include "../codecs/pcm512x.h"
39
40 #define HIFIBERRY_DACPRO_NOCLOCK 0
41 #define HIFIBERRY_DACPRO_CLK44EN 1
42 #define HIFIBERRY_DACPRO_CLK48EN 2
43
44 struct pcm512x_priv {
45         struct regmap *regmap;
46         struct clk *sclk;
47 };
48
49 /* Clock rate of CLK44EN attached to GPIO6 pin */
50 #define CLK_44EN_RATE 22579200UL
51 /* Clock rate of CLK48EN attached to GPIO3 pin */
52 #define CLK_48EN_RATE 24576000UL
53
54 static bool slave;
55 static bool snd_rpi_hifiberry_is_dacpro;
56 static bool digital_gain_0db_limit = true;
57 static bool leds_off;
58 static bool auto_mute;
59 static int mute_ext_ctl;
60 static int mute_ext;
61 static struct gpio_desc *snd_mute_gpio;
62 static struct gpio_desc *snd_reset_gpio;
63 static struct snd_soc_card snd_rpi_hifiberry_dacplus;
64
65 static int snd_rpi_hifiberry_dacplus_mute_set(int mute)
66 {
67         gpiod_set_value_cansleep(snd_mute_gpio, mute);
68         return 1;
69 }
70
71 static int snd_rpi_hifiberry_dacplus_mute_get(struct snd_kcontrol *kcontrol,
72                                 struct snd_ctl_elem_value *ucontrol)
73 {
74         ucontrol->value.integer.value[0] = mute_ext;
75
76         return 0;
77 }
78
79 static int snd_rpi_hifiberry_dacplus_mute_put(struct snd_kcontrol *kcontrol,
80                                 struct snd_ctl_elem_value *ucontrol)
81 {
82         if (mute_ext == ucontrol->value.integer.value[0])
83                 return 0;
84
85         mute_ext = ucontrol->value.integer.value[0];
86
87         return snd_rpi_hifiberry_dacplus_mute_set(mute_ext);
88 }
89
90 static const char * const mute_text[] = {"Play", "Mute"};
91 static const struct soc_enum hb_dacplus_opt_mute_enum =
92         SOC_ENUM_SINGLE_EXT(2, mute_text);
93
94 static const struct snd_kcontrol_new hb_dacplus_opt_mute_controls[] = {
95         SOC_ENUM_EXT("Mute(ext)", hb_dacplus_opt_mute_enum,
96                               snd_rpi_hifiberry_dacplus_mute_get,
97                               snd_rpi_hifiberry_dacplus_mute_put),
98 };
99
100 static void snd_rpi_hifiberry_dacplus_select_clk(struct snd_soc_component *component,
101         int clk_id)
102 {
103         switch (clk_id) {
104         case HIFIBERRY_DACPRO_NOCLOCK:
105                 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x00);
106                 break;
107         case HIFIBERRY_DACPRO_CLK44EN:
108                 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x20);
109                 break;
110         case HIFIBERRY_DACPRO_CLK48EN:
111                 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x04);
112                 break;
113         }
114         usleep_range(3000, 4000);
115 }
116
117 static void snd_rpi_hifiberry_dacplus_clk_gpio(struct snd_soc_component *component)
118 {
119         snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x24, 0x24);
120         snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_3, 0x0f, 0x02);
121         snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_6, 0x0f, 0x02);
122 }
123
124 static bool snd_rpi_hifiberry_dacplus_is_sclk(struct snd_soc_component *component)
125 {
126         unsigned int sck;
127
128         sck = snd_soc_component_read(component, PCM512x_RATE_DET_4);
129         return (!(sck & 0x40));
130 }
131
132 static bool snd_rpi_hifiberry_dacplus_is_pro_card(struct snd_soc_component *component)
133 {
134         bool isClk44EN, isClk48En, isNoClk;
135
136         snd_rpi_hifiberry_dacplus_clk_gpio(component);
137
138         snd_rpi_hifiberry_dacplus_select_clk(component, HIFIBERRY_DACPRO_CLK44EN);
139         isClk44EN = snd_rpi_hifiberry_dacplus_is_sclk(component);
140
141         snd_rpi_hifiberry_dacplus_select_clk(component, HIFIBERRY_DACPRO_NOCLOCK);
142         isNoClk = snd_rpi_hifiberry_dacplus_is_sclk(component);
143
144         snd_rpi_hifiberry_dacplus_select_clk(component, HIFIBERRY_DACPRO_CLK48EN);
145         isClk48En = snd_rpi_hifiberry_dacplus_is_sclk(component);
146
147         return (isClk44EN && isClk48En && !isNoClk);
148 }
149
150 static int snd_rpi_hifiberry_dacplus_clk_for_rate(int sample_rate)
151 {
152         int type;
153
154         switch (sample_rate) {
155         case 11025:
156         case 22050:
157         case 44100:
158         case 88200:
159         case 176400:
160         case 352800:
161                 type = HIFIBERRY_DACPRO_CLK44EN;
162                 break;
163         default:
164                 type = HIFIBERRY_DACPRO_CLK48EN;
165                 break;
166         }
167         return type;
168 }
169
170 static void snd_rpi_hifiberry_dacplus_set_sclk(struct snd_soc_component *component,
171         int sample_rate)
172 {
173         struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
174
175         if (!IS_ERR(pcm512x->sclk)) {
176                 int ctype;
177
178                 ctype = snd_rpi_hifiberry_dacplus_clk_for_rate(sample_rate);
179                 clk_set_rate(pcm512x->sclk, (ctype == HIFIBERRY_DACPRO_CLK44EN)
180                         ? CLK_44EN_RATE : CLK_48EN_RATE);
181                 snd_rpi_hifiberry_dacplus_select_clk(component, ctype);
182         }
183 }
184
185 static int snd_rpi_hifiberry_dacplus_init(struct snd_soc_pcm_runtime *rtd)
186 {
187         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
188         struct pcm512x_priv *priv;
189         struct snd_soc_card *card = &snd_rpi_hifiberry_dacplus;
190
191         if (slave)
192                 snd_rpi_hifiberry_is_dacpro = false;
193         else
194                 snd_rpi_hifiberry_is_dacpro =
195                                 snd_rpi_hifiberry_dacplus_is_pro_card(component);
196
197         if (snd_rpi_hifiberry_is_dacpro) {
198                 struct snd_soc_dai_link *dai = rtd->dai_link;
199
200                 dai->name = "HiFiBerry DAC+ Pro";
201                 dai->stream_name = "HiFiBerry DAC+ Pro HiFi";
202                 dai->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
203                         | SND_SOC_DAIFMT_CBM_CFM;
204
205                 snd_soc_component_update_bits(component, PCM512x_BCLK_LRCLK_CFG, 0x31, 0x11);
206                 snd_soc_component_update_bits(component, PCM512x_MASTER_MODE, 0x03, 0x03);
207                 snd_soc_component_update_bits(component, PCM512x_MASTER_CLKDIV_2, 0x7f, 63);
208         } else {
209                 priv = snd_soc_component_get_drvdata(component);
210                 priv->sclk = ERR_PTR(-ENOENT);
211         }
212
213         snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x08, 0x08);
214         snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
215         if (leds_off)
216                 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
217         else
218                 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
219
220         if (digital_gain_0db_limit) {
221                 int ret;
222                 struct snd_soc_card *card = rtd->card;
223
224                 ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207);
225                 if (ret < 0)
226                         dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
227         }
228         if (snd_reset_gpio) {
229                 gpiod_set_value_cansleep(snd_reset_gpio, 0);
230                 msleep(1);
231                 gpiod_set_value_cansleep(snd_reset_gpio, 1);
232                 msleep(1);
233                 gpiod_set_value_cansleep(snd_reset_gpio, 0);
234         }
235
236         if (mute_ext_ctl)
237                 snd_soc_add_card_controls(card, hb_dacplus_opt_mute_controls,
238                                 ARRAY_SIZE(hb_dacplus_opt_mute_controls));
239
240         if (snd_mute_gpio)
241                 gpiod_set_value_cansleep(snd_mute_gpio, mute_ext);
242
243         return 0;
244 }
245
246 static int snd_rpi_hifiberry_dacplus_update_rate_den(
247         struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
248 {
249         struct snd_soc_pcm_runtime *rtd = substream->private_data;
250         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
251         struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
252         struct snd_ratnum *rats_no_pll;
253         unsigned int num = 0, den = 0;
254         int err;
255
256         rats_no_pll = devm_kzalloc(rtd->dev, sizeof(*rats_no_pll), GFP_KERNEL);
257         if (!rats_no_pll)
258                 return -ENOMEM;
259
260         rats_no_pll->num = clk_get_rate(pcm512x->sclk) / 64;
261         rats_no_pll->den_min = 1;
262         rats_no_pll->den_max = 128;
263         rats_no_pll->den_step = 1;
264
265         err = snd_interval_ratnum(hw_param_interval(params,
266                 SNDRV_PCM_HW_PARAM_RATE), 1, rats_no_pll, &num, &den);
267         if (err >= 0 && den) {
268                 params->rate_num = num;
269                 params->rate_den = den;
270         }
271
272         devm_kfree(rtd->dev, rats_no_pll);
273         return 0;
274 }
275
276 static int snd_rpi_hifiberry_dacplus_hw_params(
277         struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
278 {
279         int ret = 0;
280         struct snd_soc_pcm_runtime *rtd = substream->private_data;
281         int channels = params_channels(params);
282         int width = 32;
283
284         if (snd_rpi_hifiberry_is_dacpro) {
285                 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
286
287                 width = snd_pcm_format_physical_width(params_format(params));
288
289                 snd_rpi_hifiberry_dacplus_set_sclk(component,
290                         params_rate(params));
291
292                 ret = snd_rpi_hifiberry_dacplus_update_rate_den(
293                         substream, params);
294         }
295
296         ret = snd_soc_dai_set_bclk_ratio(asoc_rtd_to_cpu(rtd, 0), channels * width);
297         if (ret)
298                 return ret;
299         ret = snd_soc_dai_set_bclk_ratio(asoc_rtd_to_codec(rtd, 0), channels * width);
300         return ret;
301 }
302
303 static int snd_rpi_hifiberry_dacplus_startup(
304         struct snd_pcm_substream *substream)
305 {
306         struct snd_soc_pcm_runtime *rtd = substream->private_data;
307         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
308
309         if (auto_mute)
310                 gpiod_set_value_cansleep(snd_mute_gpio, 0);
311         if (leds_off)
312                 return 0;
313         snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
314         return 0;
315 }
316
317 static void snd_rpi_hifiberry_dacplus_shutdown(
318         struct snd_pcm_substream *substream)
319 {
320         struct snd_soc_pcm_runtime *rtd = substream->private_data;
321         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
322
323         snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
324         if (auto_mute)
325                 gpiod_set_value_cansleep(snd_mute_gpio, 1);
326 }
327
328 /* machine stream operations */
329 static struct snd_soc_ops snd_rpi_hifiberry_dacplus_ops = {
330         .hw_params = snd_rpi_hifiberry_dacplus_hw_params,
331         .startup = snd_rpi_hifiberry_dacplus_startup,
332         .shutdown = snd_rpi_hifiberry_dacplus_shutdown,
333 };
334
335 SND_SOC_DAILINK_DEFS(rpi_hifiberry_dacplus,
336         DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
337         DAILINK_COMP_ARRAY(COMP_CODEC("pcm512x.1-004d", "pcm512x-hifi")),
338         DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
339
340 static struct snd_soc_dai_link snd_rpi_hifiberry_dacplus_dai[] = {
341 {
342         .name           = "HiFiBerry DAC+",
343         .stream_name    = "HiFiBerry DAC+ HiFi",
344         .dai_fmt        = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
345                                 SND_SOC_DAIFMT_CBS_CFS,
346         .ops            = &snd_rpi_hifiberry_dacplus_ops,
347         .init           = snd_rpi_hifiberry_dacplus_init,
348         SND_SOC_DAILINK_REG(rpi_hifiberry_dacplus),
349 },
350 };
351
352 /* aux device for optional headphone amp */
353 static struct snd_soc_aux_dev hifiberry_dacplus_aux_devs[] = {
354         {
355                 .dlc = {
356                         .name = "tpa6130a2.1-0060",
357                 },
358         },
359 };
360
361 /* audio machine driver */
362 static struct snd_soc_card snd_rpi_hifiberry_dacplus = {
363         .name         = "snd_rpi_hifiberry_dacplus",
364         .driver_name  = "HifiberryDacp",
365         .owner        = THIS_MODULE,
366         .dai_link     = snd_rpi_hifiberry_dacplus_dai,
367         .num_links    = ARRAY_SIZE(snd_rpi_hifiberry_dacplus_dai),
368 };
369
370 static int hb_hp_detect(void)
371 {
372         struct i2c_adapter *adap = i2c_get_adapter(1);
373         int ret;
374         struct i2c_client tpa_i2c_client = {
375                 .addr = 0x60,
376                 .adapter = adap,
377         };
378
379         if (!adap)
380                 return -EPROBE_DEFER;   /* I2C module not yet available */
381
382         ret = i2c_smbus_read_byte(&tpa_i2c_client) >= 0;
383         i2c_put_adapter(adap);
384         return ret;
385 };
386
387 static struct property tpa_enable_prop = {
388                .name = "status",
389                .length = 4 + 1, /* length 'okay' + 1 */
390                .value = "okay",
391         };
392
393 static int snd_rpi_hifiberry_dacplus_probe(struct platform_device *pdev)
394 {
395         int ret = 0;
396         struct snd_soc_card *card = &snd_rpi_hifiberry_dacplus;
397         int len;
398         struct device_node *tpa_node;
399         struct property *tpa_prop;
400         struct of_changeset ocs;
401         struct property *pp;
402         int tmp;
403
404         /* probe for head phone amp */
405         ret = hb_hp_detect();
406         if (ret < 0)
407                 return ret;
408         if (ret) {
409                 card->aux_dev = hifiberry_dacplus_aux_devs;
410                 card->num_aux_devs =
411                                 ARRAY_SIZE(hifiberry_dacplus_aux_devs);
412                 tpa_node = of_find_compatible_node(NULL, NULL, "ti,tpa6130a2");
413                 tpa_prop = of_find_property(tpa_node, "status", &len);
414
415                 if (strcmp((char *)tpa_prop->value, "okay")) {
416                         /* and activate headphone using change_sets */
417                         dev_info(&pdev->dev, "activating headphone amplifier");
418                         of_changeset_init(&ocs);
419                         ret = of_changeset_update_property(&ocs, tpa_node,
420                                                         &tpa_enable_prop);
421                         if (ret) {
422                                 dev_err(&pdev->dev,
423                                 "cannot activate headphone amplifier\n");
424                                 return -ENODEV;
425                         }
426                         ret = of_changeset_apply(&ocs);
427                         if (ret) {
428                                 dev_err(&pdev->dev,
429                                 "cannot activate headphone amplifier\n");
430                                 return -ENODEV;
431                         }
432                 }
433         }
434
435         snd_rpi_hifiberry_dacplus.dev = &pdev->dev;
436         if (pdev->dev.of_node) {
437                 struct device_node *i2s_node;
438                 struct snd_soc_dai_link *dai;
439
440                 dai = &snd_rpi_hifiberry_dacplus_dai[0];
441                 i2s_node = of_parse_phandle(pdev->dev.of_node,
442                         "i2s-controller", 0);
443
444                 if (i2s_node) {
445                         dai->cpus->dai_name = NULL;
446                         dai->cpus->of_node = i2s_node;
447                         dai->platforms->name = NULL;
448                         dai->platforms->of_node = i2s_node;
449                 }
450
451                 digital_gain_0db_limit = !of_property_read_bool(
452                         pdev->dev.of_node, "hifiberry,24db_digital_gain");
453                 slave = of_property_read_bool(pdev->dev.of_node,
454                                                 "hifiberry-dacplus,slave");
455                 leds_off = of_property_read_bool(pdev->dev.of_node,
456                                                 "hifiberry-dacplus,leds_off");
457                 auto_mute = of_property_read_bool(pdev->dev.of_node,
458                                                 "hifiberry-dacplus,auto_mute");
459
460                 /*
461                  * check for HW MUTE as defined in DT-overlay
462                  * active high, therefore default to HIGH to MUTE
463                  */
464                 snd_mute_gpio = devm_gpiod_get_optional(&pdev->dev,
465                                                  "mute", GPIOD_OUT_HIGH);
466                 if (IS_ERR(snd_mute_gpio)) {
467                         dev_err(&pdev->dev, "Can't allocate GPIO (HW-MUTE)");
468                         return PTR_ERR(snd_mute_gpio);
469                 }
470
471                 /* add ALSA control if requested in DT-overlay (AMP100) */
472                 pp = of_find_property(pdev->dev.of_node,
473                                 "hifiberry-dacplus,mute_ext_ctl", &tmp);
474                 if (pp) {
475                         if (!of_property_read_u32(pdev->dev.of_node,
476                                 "hifiberry-dacplus,mute_ext_ctl", &mute_ext)) {
477                                 /* ALSA control will be used */
478                                 mute_ext_ctl = 1;
479                         }
480                 }
481
482                 /* check for HW RESET (AMP100) */
483                 snd_reset_gpio = devm_gpiod_get_optional(&pdev->dev,
484                                                 "reset", GPIOD_OUT_HIGH);
485                 if (IS_ERR(snd_reset_gpio)) {
486                         dev_err(&pdev->dev, "Can't allocate GPIO (HW-RESET)");
487                         return PTR_ERR(snd_reset_gpio);
488                 }
489
490         }
491
492         ret = devm_snd_soc_register_card(&pdev->dev,
493                         &snd_rpi_hifiberry_dacplus);
494         if (ret && ret != -EPROBE_DEFER)
495                 dev_err(&pdev->dev,
496                         "snd_soc_register_card() failed: %d\n", ret);
497         if (!ret) {
498                 if (snd_mute_gpio)
499                         dev_info(&pdev->dev, "GPIO%i for HW-MUTE selected",
500                                         gpio_chip_hwgpio(snd_mute_gpio));
501                 if (snd_reset_gpio)
502                         dev_info(&pdev->dev, "GPIO%i for HW-RESET selected",
503                                         gpio_chip_hwgpio(snd_reset_gpio));
504         }
505         return ret;
506 }
507
508 static const struct of_device_id snd_rpi_hifiberry_dacplus_of_match[] = {
509         { .compatible = "hifiberry,hifiberry-dacplus", },
510         {},
511 };
512 MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_dacplus_of_match);
513
514 static struct platform_driver snd_rpi_hifiberry_dacplus_driver = {
515         .driver = {
516                 .name   = "snd-rpi-hifiberry-dacplus",
517                 .owner  = THIS_MODULE,
518                 .of_match_table = snd_rpi_hifiberry_dacplus_of_match,
519         },
520         .probe          = snd_rpi_hifiberry_dacplus_probe,
521 };
522
523 module_platform_driver(snd_rpi_hifiberry_dacplus_driver);
524
525 MODULE_AUTHOR("Daniel Matuschek <daniel@hifiberry.com>");
526 MODULE_DESCRIPTION("ASoC Driver for HiFiBerry DAC+");
527 MODULE_LICENSE("GPL v2");