blob: 6a6b68a4cb527fc389b61d6e70524f13f11b3b5a [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
Rohit Ainapure5c270872015-12-11 11:29:06 -080015#include <linux/acpi.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -080016#include <linux/device.h>
17#include <linux/err.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080018#include <linux/gpio.h>
Vincent Stehlé5c8be982015-02-11 23:08:59 +010019#include <linux/gpio/consumer.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -080020#include <linux/kernel.h>
21#include <linux/mod_devicetable.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/platform_device.h>
25#include <sound/pcm.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080026#include <sound/soc.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -080027#include <sound/soc-dai.h>
28#include <sound/soc-dapm.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080029
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080030static int max98357a_daiops_trigger(struct snd_pcm_substream *substream,
31 int cmd, struct snd_soc_dai *dai)
32{
33 struct gpio_desc *sdmode = snd_soc_dai_get_drvdata(dai);
34
Anatol Pomozov51192222015-07-12 08:14:19 -070035 if (!sdmode)
36 return 0;
37
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080038 switch (cmd) {
39 case SNDRV_PCM_TRIGGER_START:
40 case SNDRV_PCM_TRIGGER_RESUME:
41 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
42 gpiod_set_value(sdmode, 1);
43 break;
44 case SNDRV_PCM_TRIGGER_STOP:
45 case SNDRV_PCM_TRIGGER_SUSPEND:
46 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
47 gpiod_set_value(sdmode, 0);
48 break;
49 }
50
51 return 0;
52}
53
54static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080055 SND_SOC_DAPM_OUTPUT("Speaker"),
56};
57
58static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
Anatol Pomozov508e6192015-07-12 08:14:20 -070059 {"Speaker", NULL, "HiFi Playback"},
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080060};
61
62static int max98357a_codec_probe(struct snd_soc_codec *codec)
63{
64 struct gpio_desc *sdmode;
65
Anatol Pomozov51192222015-07-12 08:14:19 -070066 sdmode = devm_gpiod_get_optional(codec->dev, "sdmode", GPIOD_OUT_LOW);
Anatol Pomozov4ab0c5912015-07-12 08:14:21 -070067 if (IS_ERR(sdmode))
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080068 return PTR_ERR(sdmode);
Anatol Pomozov4ab0c5912015-07-12 08:14:21 -070069
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080070 snd_soc_codec_set_drvdata(codec, sdmode);
71
72 return 0;
73}
74
75static struct snd_soc_codec_driver max98357a_codec_driver = {
76 .probe = max98357a_codec_probe,
Kuninori Morimotob071a0bc2016-08-08 09:18:31 +000077 .component_driver = {
78 .dapm_widgets = max98357a_dapm_widgets,
79 .num_dapm_widgets = ARRAY_SIZE(max98357a_dapm_widgets),
80 .dapm_routes = max98357a_dapm_routes,
81 .num_dapm_routes = ARRAY_SIZE(max98357a_dapm_routes),
82 },
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080083};
84
Axel Lin64793042015-07-15 15:38:14 +080085static const struct snd_soc_dai_ops max98357a_dai_ops = {
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080086 .trigger = max98357a_daiops_trigger,
87};
88
89static struct snd_soc_dai_driver max98357a_dai_driver = {
Kenneth Westfield92b2ad22015-02-24 22:39:04 -080090 .name = "HiFi",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080091 .playback = {
Kenneth Westfield92b2ad22015-02-24 22:39:04 -080092 .stream_name = "HiFi Playback",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080093 .formats = SNDRV_PCM_FMTBIT_S16 |
94 SNDRV_PCM_FMTBIT_S24 |
95 SNDRV_PCM_FMTBIT_S32,
96 .rates = SNDRV_PCM_RATE_8000 |
97 SNDRV_PCM_RATE_16000 |
98 SNDRV_PCM_RATE_48000 |
99 SNDRV_PCM_RATE_96000,
100 .rate_min = 8000,
101 .rate_max = 96000,
102 .channels_min = 1,
103 .channels_max = 2,
104 },
105 .ops = &max98357a_dai_ops,
106};
107
108static int max98357a_platform_probe(struct platform_device *pdev)
109{
Anatol Pomozov4ab0c5912015-07-12 08:14:21 -0700110 return snd_soc_register_codec(&pdev->dev, &max98357a_codec_driver,
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800111 &max98357a_dai_driver, 1);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800112}
113
114static int max98357a_platform_remove(struct platform_device *pdev)
115{
116 snd_soc_unregister_codec(&pdev->dev);
117
118 return 0;
119}
120
121#ifdef CONFIG_OF
122static const struct of_device_id max98357a_device_id[] = {
Kenneth Westfield7ff5eab2015-02-17 00:53:12 -0800123 { .compatible = "maxim,max98357a" },
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800124 {}
125};
Mark Brown3efa1302015-02-09 14:36:47 +0800126MODULE_DEVICE_TABLE(of, max98357a_device_id);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800127#endif
128
Rohit Ainapure5c270872015-12-11 11:29:06 -0800129#ifdef CONFIG_ACPI
130static const struct acpi_device_id max98357a_acpi_match[] = {
131 { "MX98357A", 0 },
132 {},
133};
134MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match);
135#endif
136
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800137static struct platform_driver max98357a_platform_driver = {
138 .driver = {
Kenneth Westfield7ff5eab2015-02-17 00:53:12 -0800139 .name = "max98357a",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800140 .of_match_table = of_match_ptr(max98357a_device_id),
Rohit Ainapure5c270872015-12-11 11:29:06 -0800141 .acpi_match_table = ACPI_PTR(max98357a_acpi_match),
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800142 },
143 .probe = max98357a_platform_probe,
144 .remove = max98357a_platform_remove,
145};
146module_platform_driver(max98357a_platform_driver);
147
148MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
149MODULE_LICENSE("GPL v2");