Merge tag 'asoc-fix-v5.17-rc2' of https://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / sound / soc / codecs / max9759.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * MAX9759 Amplifier Driver
4  *
5  * Copyright (c) 2017 BayLibre, SAS.
6  * Author: Neil Armstrong <narmstrong@baylibre.com>
7  */
8
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <sound/soc.h>
12 #include <sound/soc-dapm.h>
13 #include <sound/tlv.h>
14
15 #define DRV_NAME "max9759"
16
17 struct max9759 {
18         struct gpio_desc *gpiod_shutdown;
19         struct gpio_desc *gpiod_mute;
20         struct gpio_descs *gpiod_gain;
21         bool is_mute;
22         unsigned int gain;
23 };
24
25 static int pga_event(struct snd_soc_dapm_widget *w,
26                      struct snd_kcontrol *control, int event)
27 {
28         struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
29         struct max9759 *priv = snd_soc_component_get_drvdata(c);
30
31         if (SND_SOC_DAPM_EVENT_ON(event))
32                 gpiod_set_value_cansleep(priv->gpiod_shutdown, 0);
33         else
34                 gpiod_set_value_cansleep(priv->gpiod_shutdown, 1);
35
36         return 0;
37 }
38
39 /* From 6dB to 24dB in steps of 6dB */
40 static const DECLARE_TLV_DB_SCALE(speaker_gain_tlv, 600, 600, 0);
41
42 static int speaker_gain_control_get(struct snd_kcontrol *kcontrol,
43                                     struct snd_ctl_elem_value *ucontrol)
44 {
45         struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
46         struct max9759 *priv = snd_soc_component_get_drvdata(c);
47
48         ucontrol->value.integer.value[0] = priv->gain;
49
50         return 0;
51 }
52
53 static const bool speaker_gain_table[4][2] = {
54         /* G1, G2 */
55         {true, true},   /* +6dB */
56         {false, true},  /* +12dB */
57         {true, false},  /* +18dB */
58         {false, false}, /* +24dB */
59 };
60
61 static int speaker_gain_control_put(struct snd_kcontrol *kcontrol,
62                                     struct snd_ctl_elem_value *ucontrol)
63 {
64         struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
65         struct max9759 *priv = snd_soc_component_get_drvdata(c);
66
67         if (ucontrol->value.integer.value[0] < 0 ||
68             ucontrol->value.integer.value[0] > 3)
69                 return -EINVAL;
70
71         priv->gain = ucontrol->value.integer.value[0];
72
73         /* G1 */
74         gpiod_set_value_cansleep(priv->gpiod_gain->desc[0],
75                                  speaker_gain_table[priv->gain][0]);
76         /* G2 */
77         gpiod_set_value_cansleep(priv->gpiod_gain->desc[1],
78                                  speaker_gain_table[priv->gain][1]);
79
80         return 1;
81 }
82
83 static int speaker_mute_get(struct snd_kcontrol *kcontrol,
84                             struct snd_ctl_elem_value *ucontrol)
85 {
86         struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
87         struct max9759 *priv = snd_soc_component_get_drvdata(c);
88
89         ucontrol->value.integer.value[0] = !priv->is_mute;
90
91         return 0;
92 }
93
94 static int speaker_mute_put(struct snd_kcontrol *kcontrol,
95                             struct snd_ctl_elem_value *ucontrol)
96 {
97         struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
98         struct max9759 *priv = snd_soc_component_get_drvdata(c);
99
100         priv->is_mute = !ucontrol->value.integer.value[0];
101
102         gpiod_set_value_cansleep(priv->gpiod_mute, priv->is_mute);
103
104         return 1;
105 }
106
107 static const struct snd_kcontrol_new max9759_dapm_controls[] = {
108         SOC_SINGLE_EXT_TLV("Speaker Gain Volume", 0, 0, 3, 0,
109                            speaker_gain_control_get, speaker_gain_control_put,
110                            speaker_gain_tlv),
111         SOC_SINGLE_BOOL_EXT("Playback Switch", 0,
112                             speaker_mute_get, speaker_mute_put),
113 };
114
115 static const struct snd_soc_dapm_widget max9759_dapm_widgets[] = {
116         SND_SOC_DAPM_INPUT("INL"),
117         SND_SOC_DAPM_INPUT("INR"),
118         SND_SOC_DAPM_PGA_E("PGA", SND_SOC_NOPM, 0, 0, NULL, 0, pga_event,
119                            (SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD)),
120         SND_SOC_DAPM_OUTPUT("OUTL"),
121         SND_SOC_DAPM_OUTPUT("OUTR"),
122 };
123
124 static const struct snd_soc_dapm_route max9759_dapm_routes[] = {
125         { "PGA", NULL, "INL" },
126         { "PGA", NULL, "INR" },
127         { "OUTL", NULL, "PGA" },
128         { "OUTR", NULL, "PGA" },
129 };
130
131 static const struct snd_soc_component_driver max9759_component_driver = {
132         .controls               = max9759_dapm_controls,
133         .num_controls           = ARRAY_SIZE(max9759_dapm_controls),
134         .dapm_widgets           = max9759_dapm_widgets,
135         .num_dapm_widgets       = ARRAY_SIZE(max9759_dapm_widgets),
136         .dapm_routes            = max9759_dapm_routes,
137         .num_dapm_routes        = ARRAY_SIZE(max9759_dapm_routes),
138 };
139
140 static int max9759_probe(struct platform_device *pdev)
141 {
142         struct device *dev = &pdev->dev;
143         struct max9759 *priv;
144
145         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
146         if (!priv)
147                 return -ENOMEM;
148
149         platform_set_drvdata(pdev, priv);
150
151         priv->gpiod_shutdown = devm_gpiod_get(dev, "shutdown", GPIOD_OUT_HIGH);
152         if (IS_ERR(priv->gpiod_shutdown))
153                 return dev_err_probe(dev, PTR_ERR(priv->gpiod_shutdown),
154                                      "Failed to get 'shutdown' gpio");
155
156         priv->gpiod_mute = devm_gpiod_get(dev, "mute", GPIOD_OUT_HIGH);
157         if (IS_ERR(priv->gpiod_mute))
158                 return dev_err_probe(dev, PTR_ERR(priv->gpiod_mute),
159                                      "Failed to get 'mute' gpio");
160         priv->is_mute = true;
161
162         priv->gpiod_gain = devm_gpiod_get_array(dev, "gain", GPIOD_OUT_HIGH);
163         if (IS_ERR(priv->gpiod_gain))
164                 return dev_err_probe(dev, PTR_ERR(priv->gpiod_gain),
165                                      "Failed to get 'gain' gpios");
166         priv->gain = 0;
167
168         if (priv->gpiod_gain->ndescs != 2) {
169                 dev_err(dev, "Invalid 'gain' gpios count: %d",
170                         priv->gpiod_gain->ndescs);
171                 return -EINVAL;
172         }
173
174         return devm_snd_soc_register_component(dev, &max9759_component_driver,
175                                                NULL, 0);
176 }
177
178 #ifdef CONFIG_OF
179 static const struct of_device_id max9759_ids[] = {
180         { .compatible = "maxim,max9759", },
181         { }
182 };
183 MODULE_DEVICE_TABLE(of, max9759_ids);
184 #endif
185
186 static struct platform_driver max9759_driver = {
187         .driver = {
188                 .name = DRV_NAME,
189                 .of_match_table = of_match_ptr(max9759_ids),
190         },
191         .probe = max9759_probe,
192 };
193
194 module_platform_driver(max9759_driver);
195
196 MODULE_DESCRIPTION("ASoC MAX9759 amplifier driver");
197 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
198 MODULE_LICENSE("GPL");