blob: f5e3dce2633a39a0a09a3ea3b5f2281a62db306d [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
Kenneth Westfield08d0a552015-02-17 00:53:11 -080015#include <linux/device.h>
16#include <linux/err.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080017#include <linux/gpio.h>
Vincent Stehlé5c8be982015-02-11 23:08:59 +010018#include <linux/gpio/consumer.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -080019#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>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080025#include <sound/soc.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -080026#include <sound/soc-dai.h>
27#include <sound/soc-dapm.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080028
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080029static 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
Anatol Pomozov51192222015-07-12 08:14:19 -070034 if (!sdmode)
35 return 0;
36
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080037 switch (cmd) {
38 case SNDRV_PCM_TRIGGER_START:
39 case SNDRV_PCM_TRIGGER_RESUME:
40 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
41 gpiod_set_value(sdmode, 1);
42 break;
43 case SNDRV_PCM_TRIGGER_STOP:
44 case SNDRV_PCM_TRIGGER_SUSPEND:
45 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
46 gpiod_set_value(sdmode, 0);
47 break;
48 }
49
50 return 0;
51}
52
53static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080054 SND_SOC_DAPM_OUTPUT("Speaker"),
55};
56
57static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
Anatol Pomozov508e6192015-07-12 08:14:20 -070058 {"Speaker", NULL, "HiFi Playback"},
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080059};
60
61static int max98357a_codec_probe(struct snd_soc_codec *codec)
62{
63 struct gpio_desc *sdmode;
64
Anatol Pomozov51192222015-07-12 08:14:19 -070065 sdmode = devm_gpiod_get_optional(codec->dev, "sdmode", GPIOD_OUT_LOW);
Anatol Pomozov4ab0c5912015-07-12 08:14:21 -070066 if (IS_ERR(sdmode))
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080067 return PTR_ERR(sdmode);
Anatol Pomozov4ab0c5912015-07-12 08:14:21 -070068
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080069 snd_soc_codec_set_drvdata(codec, sdmode);
70
71 return 0;
72}
73
74static 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
Axel Lin64793042015-07-15 15:38:14 +080082static const struct snd_soc_dai_ops max98357a_dai_ops = {
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080083 .trigger = max98357a_daiops_trigger,
84};
85
86static struct snd_soc_dai_driver max98357a_dai_driver = {
Kenneth Westfield92b2ad22015-02-24 22:39:04 -080087 .name = "HiFi",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080088 .playback = {
Kenneth Westfield92b2ad22015-02-24 22:39:04 -080089 .stream_name = "HiFi Playback",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080090 .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
105static int max98357a_platform_probe(struct platform_device *pdev)
106{
Anatol Pomozov4ab0c5912015-07-12 08:14:21 -0700107 return snd_soc_register_codec(&pdev->dev, &max98357a_codec_driver,
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800108 &max98357a_dai_driver, 1);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800109}
110
111static int max98357a_platform_remove(struct platform_device *pdev)
112{
113 snd_soc_unregister_codec(&pdev->dev);
114
115 return 0;
116}
117
118#ifdef CONFIG_OF
119static const struct of_device_id max98357a_device_id[] = {
Kenneth Westfield7ff5eab2015-02-17 00:53:12 -0800120 { .compatible = "maxim,max98357a" },
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800121 {}
122};
Mark Brown3efa1302015-02-09 14:36:47 +0800123MODULE_DEVICE_TABLE(of, max98357a_device_id);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800124#endif
125
126static struct platform_driver max98357a_platform_driver = {
127 .driver = {
Kenneth Westfield7ff5eab2015-02-17 00:53:12 -0800128 .name = "max98357a",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800129 .of_match_table = of_match_ptr(max98357a_device_id),
130 },
131 .probe = max98357a_platform_probe,
132 .remove = max98357a_platform_remove,
133};
134module_platform_driver(max98357a_platform_driver);
135
136MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
137MODULE_LICENSE("GPL v2");