xref: /openbmc/linux/sound/soc/codecs/max98357a.c (revision c0e297dc)
1 /* Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * max98357a.c -- MAX98357A ALSA SoC Codec driver
13  */
14 
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/kernel.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/platform_device.h>
24 #include <sound/pcm.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dai.h>
27 #include <sound/soc-dapm.h>
28 
29 static int max98357a_daiops_trigger(struct snd_pcm_substream *substream,
30 		int cmd, struct snd_soc_dai *dai)
31 {
32 	struct gpio_desc *sdmode = snd_soc_dai_get_drvdata(dai);
33 
34 	switch (cmd) {
35 	case SNDRV_PCM_TRIGGER_START:
36 	case SNDRV_PCM_TRIGGER_RESUME:
37 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
38 		gpiod_set_value(sdmode, 1);
39 		break;
40 	case SNDRV_PCM_TRIGGER_STOP:
41 	case SNDRV_PCM_TRIGGER_SUSPEND:
42 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
43 		gpiod_set_value(sdmode, 0);
44 		break;
45 	}
46 
47 	return 0;
48 }
49 
50 static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
51 	SND_SOC_DAPM_DAC("SDMode", NULL, SND_SOC_NOPM, 0, 0),
52 	SND_SOC_DAPM_OUTPUT("Speaker"),
53 };
54 
55 static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
56 	{"Speaker", NULL, "SDMode"},
57 };
58 
59 static int max98357a_codec_probe(struct snd_soc_codec *codec)
60 {
61 	struct gpio_desc *sdmode;
62 
63 	sdmode = devm_gpiod_get(codec->dev, "sdmode", GPIOD_OUT_LOW);
64 	if (IS_ERR(sdmode)) {
65 		dev_err(codec->dev, "%s() unable to get sdmode GPIO: %ld\n",
66 				__func__, PTR_ERR(sdmode));
67 		return PTR_ERR(sdmode);
68 	}
69 	snd_soc_codec_set_drvdata(codec, sdmode);
70 
71 	return 0;
72 }
73 
74 static struct snd_soc_codec_driver max98357a_codec_driver = {
75 	.probe			= max98357a_codec_probe,
76 	.dapm_widgets		= max98357a_dapm_widgets,
77 	.num_dapm_widgets	= ARRAY_SIZE(max98357a_dapm_widgets),
78 	.dapm_routes		= max98357a_dapm_routes,
79 	.num_dapm_routes	= ARRAY_SIZE(max98357a_dapm_routes),
80 };
81 
82 static struct snd_soc_dai_ops max98357a_dai_ops = {
83 	.trigger	= max98357a_daiops_trigger,
84 };
85 
86 static struct snd_soc_dai_driver max98357a_dai_driver = {
87 	.name = "HiFi",
88 	.playback = {
89 		.stream_name	= "HiFi Playback",
90 		.formats	= SNDRV_PCM_FMTBIT_S16 |
91 					SNDRV_PCM_FMTBIT_S24 |
92 					SNDRV_PCM_FMTBIT_S32,
93 		.rates		= SNDRV_PCM_RATE_8000 |
94 					SNDRV_PCM_RATE_16000 |
95 					SNDRV_PCM_RATE_48000 |
96 					SNDRV_PCM_RATE_96000,
97 		.rate_min	= 8000,
98 		.rate_max	= 96000,
99 		.channels_min	= 1,
100 		.channels_max	= 2,
101 	},
102 	.ops    = &max98357a_dai_ops,
103 };
104 
105 static int max98357a_platform_probe(struct platform_device *pdev)
106 {
107 	int ret;
108 
109 	ret = snd_soc_register_codec(&pdev->dev, &max98357a_codec_driver,
110 			&max98357a_dai_driver, 1);
111 	if (ret)
112 		dev_err(&pdev->dev, "%s() error registering codec driver: %d\n",
113 				__func__, ret);
114 
115 	return ret;
116 }
117 
118 static int max98357a_platform_remove(struct platform_device *pdev)
119 {
120 	snd_soc_unregister_codec(&pdev->dev);
121 
122 	return 0;
123 }
124 
125 #ifdef CONFIG_OF
126 static const struct of_device_id max98357a_device_id[] = {
127 	{ .compatible = "maxim,max98357a" },
128 	{}
129 };
130 MODULE_DEVICE_TABLE(of, max98357a_device_id);
131 #endif
132 
133 static struct platform_driver max98357a_platform_driver = {
134 	.driver = {
135 		.name = "max98357a",
136 		.of_match_table = of_match_ptr(max98357a_device_id),
137 	},
138 	.probe	= max98357a_platform_probe,
139 	.remove = max98357a_platform_remove,
140 };
141 module_platform_driver(max98357a_platform_driver);
142 
143 MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
144 MODULE_LICENSE("GPL v2");
145