Skip to content

Instantly share code, notes, and snippets.

@hitech95
Last active June 22, 2018 15:29
Show Gist options
  • Save hitech95/2e77d08ed96847ca59e8a3362f4e2857 to your computer and use it in GitHub Desktop.
Save hitech95/2e77d08ed96847ca59e8a3362f4e2857 to your computer and use it in GitHub Desktop.
Simple amp with GPIO mute/shutdown.
/*
* ALSA SoC simple amplifier driver
*
* Copyright 2018 <nicveronese@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/gpio/consumer.h>
#include <linux/regulator/consumer.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
/* This struct is used to save the context */
struct simple_amp_data {
struct device *dev;
struct regmap *regmap;
struct gpio_desc *power_gpio;
};
static void simple_amp_power_gpio(struct simple_amp_data *data, int enable)
{
/* Shutdown GPIO */
if (data->power_gpio)
gpiod_set_value_cansleep(data->power_gpio, enable);
}
static int simple_amp_power_event(struct snd_soc_dapm_widget *widget,
struct snd_kcontrol *kctrl, int event)
{
struct snd_soc_component *c = snd_soc_dapm_to_component(widget->dapm);
struct simple_amp_data *data = snd_soc_component_get_drvdata(c);
simple_amp_power_gpio(data, SND_SOC_DAPM_EVENT_ON(event));
return 0;
}
static const struct snd_soc_dapm_widget simple_amp_dapm_widgets[] = {
SND_SOC_DAPM_INPUT("LEFT IN"),
SND_SOC_DAPM_INPUT("RIGHT IN"),
SND_SOC_DAPM_OUTPUT("SPK LEFT"),
SND_SOC_DAPM_OUTPUT("SPK RIGHT"),
SND_SOC_DAPM_REGULATOR_SUPPLY("VCC", 20, 0),
SND_SOC_DAPM_PGA_E("MUTE", SND_SOC_NOPM, 0, 0, NULL, 0, simple_amp_power_event, 0),
};
static const struct snd_soc_dapm_route simple_amp_dapm_routes[] = {
{ "SPK LEFT", NULL, "LEFT IN" },
{ "SPK RIGHT", NULL, "RIGHT IN" },
{ "SPK LEFT", NULL, "VCC" },
{ "SPK RIGHT", NULL, "VCC" },
{ "SPK LEFT", NULL, "MUTE" },
{ "SPK RIGHT", NULL, "MUTE" },
};
static const struct snd_soc_component_driver simple_amp_component_driver = {
.name = "simple-amp",
.dapm_widgets = simple_amp_dapm_widgets,
.num_dapm_widgets = ARRAY_SIZE(simple_amp_dapm_widgets),
.dapm_routes = simple_amp_dapm_routes,
.num_dapm_routes = ARRAY_SIZE(simple_amp_dapm_routes),
};
static int asoc_simple_amp_probe(struct platform_device *pdev)
{
struct simple_amp_data *data;
struct device *dev = &pdev->dev;
int ret;
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
data->dev = &pdev->dev;
if(of_property_read_bool(pdev->dev.of_node,"shutdown-gpio")) {
data->power_gpio = devm_gpiod_get_optional(&pdev->dev, "shutdown", GPIOD_OUT_LOW);
if (IS_ERR(data->power_gpio)) {
ret = PTR_ERR(data->power_gpio);
dev_err(&pdev->dev, "Failed to get shutdown gpio: %d\n", ret);
return ret;
}
}
return devm_snd_soc_register_component(dev,
&simple_amp_component_driver, NULL, 0);
}
static const struct of_device_id asoc_simple_of_match[] = {
{ .compatible = "simple-audio-amp", },
{},
};
MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
static struct platform_driver asoc_simple_amp = {
.driver = {
.name = "asoc-simple-amp",
.of_match_table = of_match_ptr(asoc_simple_of_match),
},
.probe = asoc_simple_amp_probe,
};
module_platform_driver(asoc_simple_amp);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("ASoC Simple Amp");
MODULE_AUTHOR("Nicolò Veronese <nicveronese@gmail.com>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment