1 // SPDX-License-Identifier: GPL-2.0
3 * MAX9759 Amplifier Driver
5 * Copyright (c) 2017 BayLibre, SAS.
6 * Author: Neil Armstrong <narmstrong@baylibre.com>
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>
15 #define DRV_NAME "max9759"
18 struct gpio_desc
*gpiod_shutdown
;
19 struct gpio_desc
*gpiod_mute
;
20 struct gpio_descs
*gpiod_gain
;
25 static int pga_event(struct snd_soc_dapm_widget
*w
,
26 struct snd_kcontrol
*control
, int event
)
28 struct snd_soc_component
*c
= snd_soc_dapm_to_component(w
->dapm
);
29 struct max9759
*priv
= snd_soc_component_get_drvdata(c
);
31 if (SND_SOC_DAPM_EVENT_ON(event
))
32 gpiod_set_value_cansleep(priv
->gpiod_shutdown
, 0);
34 gpiod_set_value_cansleep(priv
->gpiod_shutdown
, 1);
39 /* From 6dB to 24dB in steps of 6dB */
40 static const DECLARE_TLV_DB_SCALE(speaker_gain_tlv
, 600, 600, 0);
42 static int speaker_gain_control_get(struct snd_kcontrol
*kcontrol
,
43 struct snd_ctl_elem_value
*ucontrol
)
45 struct snd_soc_component
*c
= snd_soc_kcontrol_component(kcontrol
);
46 struct max9759
*priv
= snd_soc_component_get_drvdata(c
);
48 ucontrol
->value
.integer
.value
[0] = priv
->gain
;
53 static const bool speaker_gain_table
[4][2] = {
55 {true, true}, /* +6dB */
56 {false, true}, /* +12dB */
57 {true, false}, /* +18dB */
58 {false, false}, /* +24dB */
61 static int speaker_gain_control_put(struct snd_kcontrol
*kcontrol
,
62 struct snd_ctl_elem_value
*ucontrol
)
64 struct snd_soc_component
*c
= snd_soc_kcontrol_component(kcontrol
);
65 struct max9759
*priv
= snd_soc_component_get_drvdata(c
);
67 if (ucontrol
->value
.integer
.value
[0] > 3)
70 priv
->gain
= ucontrol
->value
.integer
.value
[0];
73 gpiod_set_value_cansleep(priv
->gpiod_gain
->desc
[0],
74 speaker_gain_table
[priv
->gain
][0]);
76 gpiod_set_value_cansleep(priv
->gpiod_gain
->desc
[1],
77 speaker_gain_table
[priv
->gain
][1]);
82 static int speaker_mute_get(struct snd_kcontrol
*kcontrol
,
83 struct snd_ctl_elem_value
*ucontrol
)
85 struct snd_soc_component
*c
= snd_soc_kcontrol_component(kcontrol
);
86 struct max9759
*priv
= snd_soc_component_get_drvdata(c
);
88 ucontrol
->value
.integer
.value
[0] = !priv
->is_mute
;
93 static int speaker_mute_put(struct snd_kcontrol
*kcontrol
,
94 struct snd_ctl_elem_value
*ucontrol
)
96 struct snd_soc_component
*c
= snd_soc_kcontrol_component(kcontrol
);
97 struct max9759
*priv
= snd_soc_component_get_drvdata(c
);
99 priv
->is_mute
= !ucontrol
->value
.integer
.value
[0];
101 gpiod_set_value_cansleep(priv
->gpiod_mute
, priv
->is_mute
);
106 static const struct snd_kcontrol_new max9759_dapm_controls
[] = {
107 SOC_SINGLE_EXT_TLV("Speaker Gain Volume", 0, 0, 3, 0,
108 speaker_gain_control_get
, speaker_gain_control_put
,
110 SOC_SINGLE_BOOL_EXT("Playback Switch", 0,
111 speaker_mute_get
, speaker_mute_put
),
114 static const struct snd_soc_dapm_widget max9759_dapm_widgets
[] = {
115 SND_SOC_DAPM_INPUT("INL"),
116 SND_SOC_DAPM_INPUT("INR"),
117 SND_SOC_DAPM_PGA_E("PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0, pga_event
,
118 (SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
)),
119 SND_SOC_DAPM_OUTPUT("OUTL"),
120 SND_SOC_DAPM_OUTPUT("OUTR"),
123 static const struct snd_soc_dapm_route max9759_dapm_routes
[] = {
124 { "PGA", NULL
, "INL" },
125 { "PGA", NULL
, "INR" },
126 { "OUTL", NULL
, "PGA" },
127 { "OUTR", NULL
, "PGA" },
130 static const struct snd_soc_component_driver max9759_component_driver
= {
131 .controls
= max9759_dapm_controls
,
132 .num_controls
= ARRAY_SIZE(max9759_dapm_controls
),
133 .dapm_widgets
= max9759_dapm_widgets
,
134 .num_dapm_widgets
= ARRAY_SIZE(max9759_dapm_widgets
),
135 .dapm_routes
= max9759_dapm_routes
,
136 .num_dapm_routes
= ARRAY_SIZE(max9759_dapm_routes
),
139 static int max9759_probe(struct platform_device
*pdev
)
141 struct device
*dev
= &pdev
->dev
;
142 struct max9759
*priv
;
145 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
149 platform_set_drvdata(pdev
, priv
);
151 priv
->gpiod_shutdown
= devm_gpiod_get(dev
, "shutdown", GPIOD_OUT_HIGH
);
152 if (IS_ERR(priv
->gpiod_shutdown
)) {
153 err
= PTR_ERR(priv
->gpiod_shutdown
);
154 if (err
!= -EPROBE_DEFER
)
155 dev_err(dev
, "Failed to get 'shutdown' gpio: %d", err
);
159 priv
->gpiod_mute
= devm_gpiod_get(dev
, "mute", GPIOD_OUT_HIGH
);
160 if (IS_ERR(priv
->gpiod_mute
)) {
161 err
= PTR_ERR(priv
->gpiod_mute
);
162 if (err
!= -EPROBE_DEFER
)
163 dev_err(dev
, "Failed to get 'mute' gpio: %d", err
);
166 priv
->is_mute
= true;
168 priv
->gpiod_gain
= devm_gpiod_get_array(dev
, "gain", GPIOD_OUT_HIGH
);
169 if (IS_ERR(priv
->gpiod_gain
)) {
170 err
= PTR_ERR(priv
->gpiod_gain
);
171 if (err
!= -EPROBE_DEFER
)
172 dev_err(dev
, "Failed to get 'gain' gpios: %d", err
);
177 if (priv
->gpiod_gain
->ndescs
!= 2) {
178 dev_err(dev
, "Invalid 'gain' gpios count: %d",
179 priv
->gpiod_gain
->ndescs
);
183 return devm_snd_soc_register_component(dev
, &max9759_component_driver
,
188 static const struct of_device_id max9759_ids
[] = {
189 { .compatible
= "maxim,max9759", },
192 MODULE_DEVICE_TABLE(of
, max9759_ids
);
195 static struct platform_driver max9759_driver
= {
198 .of_match_table
= of_match_ptr(max9759_ids
),
200 .probe
= max9759_probe
,
203 module_platform_driver(max9759_driver
);
205 MODULE_DESCRIPTION("ASoC MAX9759 amplifier driver");
206 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
207 MODULE_LICENSE("GPL");