blob: f493fb6fd4ea764d617b3d7dd5f3b1ebe1e0fe20 [file] [log] [blame]
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -08001/* 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/module.h>
16#include <linux/gpio.h>
Vincent Stehlé5c8be982015-02-11 23:08:59 +010017#include <linux/gpio/consumer.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080018#include <sound/soc.h>
19
20#define DRV_NAME "max98357a"
21
22static int max98357a_daiops_trigger(struct snd_pcm_substream *substream,
23 int cmd, struct snd_soc_dai *dai)
24{
25 struct gpio_desc *sdmode = snd_soc_dai_get_drvdata(dai);
26
27 switch (cmd) {
28 case SNDRV_PCM_TRIGGER_START:
29 case SNDRV_PCM_TRIGGER_RESUME:
30 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
31 gpiod_set_value(sdmode, 1);
32 break;
33 case SNDRV_PCM_TRIGGER_STOP:
34 case SNDRV_PCM_TRIGGER_SUSPEND:
35 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
36 gpiod_set_value(sdmode, 0);
37 break;
38 }
39
40 return 0;
41}
42
43static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
44 SND_SOC_DAPM_DAC("SDMode", NULL, SND_SOC_NOPM, 0, 0),
45 SND_SOC_DAPM_OUTPUT("Speaker"),
46};
47
48static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
49 {"Speaker", NULL, "SDMode"},
50};
51
52static int max98357a_codec_probe(struct snd_soc_codec *codec)
53{
54 struct gpio_desc *sdmode;
55
56 sdmode = devm_gpiod_get(codec->dev, "sdmode");
57 if (IS_ERR(sdmode)) {
58 dev_err(codec->dev, "%s() unable to get sdmode GPIO: %ld\n",
59 __func__, PTR_ERR(sdmode));
60 return PTR_ERR(sdmode);
61 }
62 gpiod_direction_output(sdmode, 0);
63 snd_soc_codec_set_drvdata(codec, sdmode);
64
65 return 0;
66}
67
68static struct snd_soc_codec_driver max98357a_codec_driver = {
69 .probe = max98357a_codec_probe,
70 .dapm_widgets = max98357a_dapm_widgets,
71 .num_dapm_widgets = ARRAY_SIZE(max98357a_dapm_widgets),
72 .dapm_routes = max98357a_dapm_routes,
73 .num_dapm_routes = ARRAY_SIZE(max98357a_dapm_routes),
74};
75
76static struct snd_soc_dai_ops max98357a_dai_ops = {
77 .trigger = max98357a_daiops_trigger,
78};
79
80static struct snd_soc_dai_driver max98357a_dai_driver = {
81 .name = DRV_NAME,
82 .playback = {
83 .stream_name = DRV_NAME "-playback",
84 .formats = SNDRV_PCM_FMTBIT_S16 |
85 SNDRV_PCM_FMTBIT_S24 |
86 SNDRV_PCM_FMTBIT_S32,
87 .rates = SNDRV_PCM_RATE_8000 |
88 SNDRV_PCM_RATE_16000 |
89 SNDRV_PCM_RATE_48000 |
90 SNDRV_PCM_RATE_96000,
91 .rate_min = 8000,
92 .rate_max = 96000,
93 .channels_min = 1,
94 .channels_max = 2,
95 },
96 .ops = &max98357a_dai_ops,
97};
98
99static int max98357a_platform_probe(struct platform_device *pdev)
100{
101 int ret;
102
103 ret = snd_soc_register_codec(&pdev->dev, &max98357a_codec_driver,
104 &max98357a_dai_driver, 1);
105 if (ret)
106 dev_err(&pdev->dev, "%s() error registering codec driver: %d\n",
107 __func__, ret);
108
109 return ret;
110}
111
112static int max98357a_platform_remove(struct platform_device *pdev)
113{
114 snd_soc_unregister_codec(&pdev->dev);
115
116 return 0;
117}
118
119#ifdef CONFIG_OF
120static const struct of_device_id max98357a_device_id[] = {
121 { .compatible = "maxim," DRV_NAME, },
122 {}
123};
Mark Brown3efa1302015-02-09 14:36:47 +0800124MODULE_DEVICE_TABLE(of, max98357a_device_id);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800125#endif
126
127static struct platform_driver max98357a_platform_driver = {
128 .driver = {
129 .name = DRV_NAME,
130 .of_match_table = of_match_ptr(max98357a_device_id),
131 },
132 .probe = max98357a_platform_probe,
133 .remove = max98357a_platform_remove,
134};
135module_platform_driver(max98357a_platform_driver);
136
137MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
138MODULE_LICENSE("GPL v2");
139MODULE_ALIAS("platform:" DRV_NAME);