Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / sound / soc / intel / boards / byt-max98090.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel Baytrail SST MAX98090 machine driver
4  * Copyright (c) 2014, Intel Corporation.
5  */
6
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/acpi.h>
11 #include <linux/device.h>
12 #include <linux/gpio.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/slab.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include <sound/jack.h>
19 #include "../../codecs/max98090.h"
20
21 struct byt_max98090_private {
22         struct snd_soc_jack jack;
23 };
24
25 static const struct snd_soc_dapm_widget byt_max98090_widgets[] = {
26         SND_SOC_DAPM_HP("Headphone", NULL),
27         SND_SOC_DAPM_MIC("Headset Mic", NULL),
28         SND_SOC_DAPM_MIC("Int Mic", NULL),
29         SND_SOC_DAPM_SPK("Ext Spk", NULL),
30 };
31
32 static const struct snd_soc_dapm_route byt_max98090_audio_map[] = {
33         {"IN34", NULL, "Headset Mic"},
34         {"Headset Mic", NULL, "MICBIAS"},
35         {"DMICL", NULL, "Int Mic"},
36         {"Headphone", NULL, "HPL"},
37         {"Headphone", NULL, "HPR"},
38         {"Ext Spk", NULL, "SPKL"},
39         {"Ext Spk", NULL, "SPKR"},
40 };
41
42 static const struct snd_kcontrol_new byt_max98090_controls[] = {
43         SOC_DAPM_PIN_SWITCH("Headphone"),
44         SOC_DAPM_PIN_SWITCH("Headset Mic"),
45         SOC_DAPM_PIN_SWITCH("Int Mic"),
46         SOC_DAPM_PIN_SWITCH("Ext Spk"),
47 };
48
49 static struct snd_soc_jack_pin hs_jack_pins[] = {
50         {
51                 .pin    = "Headphone",
52                 .mask   = SND_JACK_HEADPHONE,
53         },
54         {
55                 .pin    = "Headset Mic",
56                 .mask   = SND_JACK_MICROPHONE,
57         },
58 };
59
60 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
61         {
62                 .name           = "hp",
63                 .report         = SND_JACK_HEADPHONE | SND_JACK_LINEOUT,
64                 .debounce_time  = 200,
65         },
66         {
67                 .name           = "mic",
68                 .invert         = 1,
69                 .report         = SND_JACK_MICROPHONE,
70                 .debounce_time  = 200,
71         },
72 };
73
74 static const struct acpi_gpio_params hp_gpios = { 0, 0, false };
75 static const struct acpi_gpio_params mic_gpios = { 1, 0, false };
76
77 static const struct acpi_gpio_mapping acpi_byt_max98090_gpios[] = {
78         { "hp-gpios", &hp_gpios, 1 },
79         { "mic-gpios", &mic_gpios, 1 },
80         {},
81 };
82
83 static int byt_max98090_init(struct snd_soc_pcm_runtime *runtime)
84 {
85         int ret;
86         struct snd_soc_card *card = runtime->card;
87         struct byt_max98090_private *drv = snd_soc_card_get_drvdata(card);
88         struct snd_soc_jack *jack = &drv->jack;
89
90         card->dapm.idle_bias_off = true;
91
92         ret = snd_soc_dai_set_sysclk(runtime->codec_dai,
93                                      M98090_REG_SYSTEM_CLOCK,
94                                      25000000, SND_SOC_CLOCK_IN);
95         if (ret < 0) {
96                 dev_err(card->dev, "Can't set codec clock %d\n", ret);
97                 return ret;
98         }
99
100         /* Enable jack detection */
101         ret = snd_soc_card_jack_new(runtime->card, "Headset",
102                                     SND_JACK_LINEOUT | SND_JACK_HEADSET, jack,
103                                     hs_jack_pins, ARRAY_SIZE(hs_jack_pins));
104         if (ret)
105                 return ret;
106
107         return snd_soc_jack_add_gpiods(card->dev->parent, jack,
108                                        ARRAY_SIZE(hs_jack_gpios),
109                                        hs_jack_gpios);
110 }
111
112 SND_SOC_DAILINK_DEFS(baytrail,
113         DAILINK_COMP_ARRAY(COMP_CPU("baytrail-pcm-audio")),
114         DAILINK_COMP_ARRAY(COMP_CODEC("i2c-193C9890:00", "HiFi")),
115         DAILINK_COMP_ARRAY(COMP_PLATFORM("baytrail-pcm-audio")));
116
117 static struct snd_soc_dai_link byt_max98090_dais[] = {
118         {
119                 .name = "Baytrail Audio",
120                 .stream_name = "Audio",
121                 .init = byt_max98090_init,
122                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
123                            SND_SOC_DAIFMT_CBS_CFS,
124                 SND_SOC_DAILINK_REG(baytrail),
125         },
126 };
127
128 static struct snd_soc_card byt_max98090_card = {
129         .name = "byt-max98090",
130         .owner = THIS_MODULE,
131         .dai_link = byt_max98090_dais,
132         .num_links = ARRAY_SIZE(byt_max98090_dais),
133         .dapm_widgets = byt_max98090_widgets,
134         .num_dapm_widgets = ARRAY_SIZE(byt_max98090_widgets),
135         .dapm_routes = byt_max98090_audio_map,
136         .num_dapm_routes = ARRAY_SIZE(byt_max98090_audio_map),
137         .controls = byt_max98090_controls,
138         .num_controls = ARRAY_SIZE(byt_max98090_controls),
139         .fully_routed = true,
140 };
141
142 static int byt_max98090_probe(struct platform_device *pdev)
143 {
144         struct device *dev = &pdev->dev;
145         struct byt_max98090_private *priv;
146         int ret_val;
147
148         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
149         if (!priv) {
150                 dev_err(&pdev->dev, "allocation failed\n");
151                 return -ENOMEM;
152         }
153
154         ret_val = devm_acpi_dev_add_driver_gpios(dev->parent, acpi_byt_max98090_gpios);
155         if (ret_val)
156                 dev_dbg(dev, "Unable to add GPIO mapping table\n");
157
158         byt_max98090_card.dev = &pdev->dev;
159         snd_soc_card_set_drvdata(&byt_max98090_card, priv);
160         ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_max98090_card);
161         if (ret_val) {
162                 dev_err(&pdev->dev,
163                         "snd_soc_register_card failed %d\n", ret_val);
164                 return ret_val;
165         }
166
167         return 0;
168 }
169
170 static struct platform_driver byt_max98090_driver = {
171         .probe = byt_max98090_probe,
172         .driver = {
173                 .name = "byt-max98090",
174                 .pm = &snd_soc_pm_ops,
175         },
176 };
177 module_platform_driver(byt_max98090_driver)
178
179 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail Machine driver");
180 MODULE_AUTHOR("Omair Md Abdullah, Jarkko Nikula");
181 MODULE_LICENSE("GPL v2");
182 MODULE_ALIAS("platform:byt-max98090");