blob: 918812763884861252d433f2094c59560f29df1c [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -08002/* Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
3 *
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -08004 * max98357a.c -- MAX98357A ALSA SoC Codec driver
5 */
6
Rohit Ainapure5c270872015-12-11 11:29:06 -08007#include <linux/acpi.h>
Tzung-Bi Shih128f825a2020-02-12 13:55:16 +08008#include <linux/delay.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -08009#include <linux/device.h>
10#include <linux/err.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080011#include <linux/gpio.h>
Vincent Stehlé5c8be982015-02-11 23:08:59 +010012#include <linux/gpio/consumer.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -080013#include <linux/kernel.h>
14#include <linux/mod_devicetable.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/platform_device.h>
18#include <sound/pcm.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080019#include <sound/soc.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -080020#include <sound/soc-dai.h>
21#include <sound/soc-dapm.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080022
Mac Chiangcec5b01f2019-06-19 18:18:33 +080023struct max98357a_priv {
Mac Chiangcec5b01f2019-06-19 18:18:33 +080024 struct gpio_desc *sdmode;
25 unsigned int sdmode_delay;
Tzung-Bi Shih04a646f2020-07-21 19:42:32 +080026 int sdmode_switch;
Mac Chiangcec5b01f2019-06-19 18:18:33 +080027};
28
Tzung-Bi Shih04a646f2020-07-21 19:42:32 +080029static int max98357a_daiops_trigger(struct snd_pcm_substream *substream,
30 int cmd, struct snd_soc_dai *dai)
31{
32 struct snd_soc_component *component = dai->component;
33 struct max98357a_priv *max98357a =
34 snd_soc_component_get_drvdata(component);
35
36 if (!max98357a->sdmode)
37 return 0;
38
39 switch (cmd) {
40 case SNDRV_PCM_TRIGGER_START:
41 case SNDRV_PCM_TRIGGER_RESUME:
42 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
43 mdelay(max98357a->sdmode_delay);
44 if (max98357a->sdmode_switch) {
45 gpiod_set_value(max98357a->sdmode, 1);
46 dev_dbg(component->dev, "set sdmode to 1");
47 }
48 break;
49 case SNDRV_PCM_TRIGGER_STOP:
50 case SNDRV_PCM_TRIGGER_SUSPEND:
51 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
52 gpiod_set_value(max98357a->sdmode, 0);
53 dev_dbg(component->dev, "set sdmode to 0");
54 break;
55 }
56
57 return 0;
58}
59
Tzung-Bi Shih128f825a2020-02-12 13:55:16 +080060static int max98357a_sdmode_event(struct snd_soc_dapm_widget *w,
61 struct snd_kcontrol *kcontrol, int event)
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080062{
Tzung-Bi Shih128f825a2020-02-12 13:55:16 +080063 struct snd_soc_component *component =
64 snd_soc_dapm_to_component(w->dapm);
65 struct max98357a_priv *max98357a =
66 snd_soc_component_get_drvdata(component);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080067
Tzung-Bi Shih04a646f2020-07-21 19:42:32 +080068 if (event & SND_SOC_DAPM_POST_PMU)
69 max98357a->sdmode_switch = 1;
70 else if (event & SND_SOC_DAPM_POST_PMD)
71 max98357a->sdmode_switch = 0;
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080072
73 return 0;
74}
75
76static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080077 SND_SOC_DAPM_OUTPUT("Speaker"),
Tzung-Bi Shih128f825a2020-02-12 13:55:16 +080078 SND_SOC_DAPM_OUT_DRV_E("SD_MODE", SND_SOC_NOPM, 0, 0, NULL, 0,
79 max98357a_sdmode_event,
Tzung-Bi Shih04a646f2020-07-21 19:42:32 +080080 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080081};
82
83static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
Tzung-Bi Shih128f825a2020-02-12 13:55:16 +080084 {"SD_MODE", NULL, "HiFi Playback"},
85 {"Speaker", NULL, "SD_MODE"},
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080086};
87
Kuninori Morimoto4b51e9f2018-01-29 04:09:20 +000088static const struct snd_soc_component_driver max98357a_component_driver = {
Kuninori Morimoto4b51e9f2018-01-29 04:09:20 +000089 .dapm_widgets = max98357a_dapm_widgets,
90 .num_dapm_widgets = ARRAY_SIZE(max98357a_dapm_widgets),
91 .dapm_routes = max98357a_dapm_routes,
92 .num_dapm_routes = ARRAY_SIZE(max98357a_dapm_routes),
93 .idle_bias_on = 1,
94 .use_pmdown_time = 1,
95 .endianness = 1,
96 .non_legacy_dai_naming = 1,
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080097};
98
Tzung-Bi Shih04a646f2020-07-21 19:42:32 +080099static const struct snd_soc_dai_ops max98357a_dai_ops = {
100 .trigger = max98357a_daiops_trigger,
101};
102
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800103static struct snd_soc_dai_driver max98357a_dai_driver = {
Kenneth Westfield92b2ad22015-02-24 22:39:04 -0800104 .name = "HiFi",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800105 .playback = {
Kenneth Westfield92b2ad22015-02-24 22:39:04 -0800106 .stream_name = "HiFi Playback",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800107 .formats = SNDRV_PCM_FMTBIT_S16 |
108 SNDRV_PCM_FMTBIT_S24 |
109 SNDRV_PCM_FMTBIT_S32,
110 .rates = SNDRV_PCM_RATE_8000 |
111 SNDRV_PCM_RATE_16000 |
Jerome Brunetfdf34362019-04-04 13:50:15 +0200112 SNDRV_PCM_RATE_32000 |
113 SNDRV_PCM_RATE_44100 |
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800114 SNDRV_PCM_RATE_48000 |
Jerome Brunetfdf34362019-04-04 13:50:15 +0200115 SNDRV_PCM_RATE_88200 |
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800116 SNDRV_PCM_RATE_96000,
117 .rate_min = 8000,
118 .rate_max = 96000,
119 .channels_min = 1,
120 .channels_max = 2,
121 },
Tzung-Bi Shih04a646f2020-07-21 19:42:32 +0800122 .ops = &max98357a_dai_ops,
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800123};
124
125static int max98357a_platform_probe(struct platform_device *pdev)
126{
Mac Chiangcec5b01f2019-06-19 18:18:33 +0800127 struct max98357a_priv *max98357a;
128 int ret;
Tzung-Bi Shihbcd93822019-05-09 11:04:54 +0800129
Mac Chiangcec5b01f2019-06-19 18:18:33 +0800130 max98357a = devm_kzalloc(&pdev->dev, sizeof(*max98357a), GFP_KERNEL);
Mac Chiangcec5b01f2019-06-19 18:18:33 +0800131 if (!max98357a)
132 return -ENOMEM;
133
134 max98357a->sdmode = devm_gpiod_get_optional(&pdev->dev,
Tzung-Bi Shihbcd93822019-05-09 11:04:54 +0800135 "sdmode", GPIOD_OUT_LOW);
Mac Chiangcec5b01f2019-06-19 18:18:33 +0800136 if (IS_ERR(max98357a->sdmode))
137 return PTR_ERR(max98357a->sdmode);
138
139 ret = device_property_read_u32(&pdev->dev, "sdmode-delay",
140 &max98357a->sdmode_delay);
Mac Chiangcec5b01f2019-06-19 18:18:33 +0800141 if (ret) {
142 max98357a->sdmode_delay = 0;
143 dev_dbg(&pdev->dev,
Tzung-Bi Shih6cd249c2019-07-08 22:19:01 +0800144 "no optional property 'sdmode-delay' found, "
145 "default: no delay\n");
Mac Chiangcec5b01f2019-06-19 18:18:33 +0800146 }
147
148 dev_set_drvdata(&pdev->dev, max98357a);
149
Kuninori Morimoto4b51e9f2018-01-29 04:09:20 +0000150 return devm_snd_soc_register_component(&pdev->dev,
151 &max98357a_component_driver,
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800152 &max98357a_dai_driver, 1);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800153}
154
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800155#ifdef CONFIG_OF
156static const struct of_device_id max98357a_device_id[] = {
Kenneth Westfield7ff5eab2015-02-17 00:53:12 -0800157 { .compatible = "maxim,max98357a" },
Tzung-Bi Shih3aad07b2020-06-05 11:49:30 +0800158 { .compatible = "maxim,max98360a" },
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800159 {}
160};
Mark Brown3efa1302015-02-09 14:36:47 +0800161MODULE_DEVICE_TABLE(of, max98357a_device_id);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800162#endif
163
Rohit Ainapure5c270872015-12-11 11:29:06 -0800164#ifdef CONFIG_ACPI
165static const struct acpi_device_id max98357a_acpi_match[] = {
166 { "MX98357A", 0 },
Yong Zhi1a0f2432020-03-13 10:55:26 -0500167 { "MX98360A", 0 },
Rohit Ainapure5c270872015-12-11 11:29:06 -0800168 {},
169};
170MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match);
171#endif
172
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800173static struct platform_driver max98357a_platform_driver = {
174 .driver = {
Kenneth Westfield7ff5eab2015-02-17 00:53:12 -0800175 .name = "max98357a",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800176 .of_match_table = of_match_ptr(max98357a_device_id),
Rohit Ainapure5c270872015-12-11 11:29:06 -0800177 .acpi_match_table = ACPI_PTR(max98357a_acpi_match),
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800178 },
179 .probe = max98357a_platform_probe,
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800180};
181module_platform_driver(max98357a_platform_driver);
182
183MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
184MODULE_LICENSE("GPL v2");