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
pga_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * control,int event)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
speaker_gain_control_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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
speaker_gain_control_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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
speaker_mute_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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
speaker_mute_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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
max9759_probe(struct platform_device * pdev)140 static int max9759_probe(struct platform_device *pdev)
141 {
142 struct device *dev = &pdev->dev;
143 struct max9759 *priv;
144 int err;
145
146 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
147 if (!priv)
148 return -ENOMEM;
149
150 platform_set_drvdata(pdev, priv);
151
152 priv->gpiod_shutdown = devm_gpiod_get(dev, "shutdown", GPIOD_OUT_HIGH);
153 if (IS_ERR(priv->gpiod_shutdown)) {
154 err = PTR_ERR(priv->gpiod_shutdown);
155 if (err != -EPROBE_DEFER)
156 dev_err(dev, "Failed to get 'shutdown' gpio: %d", err);
157 return err;
158 }
159
160 priv->gpiod_mute = devm_gpiod_get(dev, "mute", GPIOD_OUT_HIGH);
161 if (IS_ERR(priv->gpiod_mute)) {
162 err = PTR_ERR(priv->gpiod_mute);
163 if (err != -EPROBE_DEFER)
164 dev_err(dev, "Failed to get 'mute' gpio: %d", err);
165 return err;
166 }
167 priv->is_mute = true;
168
169 priv->gpiod_gain = devm_gpiod_get_array(dev, "gain", GPIOD_OUT_HIGH);
170 if (IS_ERR(priv->gpiod_gain)) {
171 err = PTR_ERR(priv->gpiod_gain);
172 if (err != -EPROBE_DEFER)
173 dev_err(dev, "Failed to get 'gain' gpios: %d", err);
174 return err;
175 }
176 priv->gain = 0;
177
178 if (priv->gpiod_gain->ndescs != 2) {
179 dev_err(dev, "Invalid 'gain' gpios count: %d",
180 priv->gpiod_gain->ndescs);
181 return -EINVAL;
182 }
183
184 return devm_snd_soc_register_component(dev, &max9759_component_driver,
185 NULL, 0);
186 }
187
188 #ifdef CONFIG_OF
189 static const struct of_device_id max9759_ids[] = {
190 { .compatible = "maxim,max9759", },
191 { }
192 };
193 MODULE_DEVICE_TABLE(of, max9759_ids);
194 #endif
195
196 static struct platform_driver max9759_driver = {
197 .driver = {
198 .name = DRV_NAME,
199 .of_match_table = of_match_ptr(max9759_ids),
200 },
201 .probe = max9759_probe,
202 };
203
204 module_platform_driver(max9759_driver);
205
206 MODULE_DESCRIPTION("ASoC MAX9759 amplifier driver");
207 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
208 MODULE_LICENSE("GPL");
209