Thomas Gleixner | 97fb5e8 | 2019-05-29 07:17:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved. |
| 3 | * |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 4 | * max98357a.c -- MAX98357A ALSA SoC Codec driver |
| 5 | */ |
| 6 | |
Rohit Ainapure | 5c27087 | 2015-12-11 11:29:06 -0800 | [diff] [blame] | 7 | #include <linux/acpi.h> |
Tzung-Bi Shih | 128f825a | 2020-02-12 13:55:16 +0800 | [diff] [blame] | 8 | #include <linux/delay.h> |
Kenneth Westfield | 08d0a55 | 2015-02-17 00:53:11 -0800 | [diff] [blame] | 9 | #include <linux/device.h> |
| 10 | #include <linux/err.h> |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 11 | #include <linux/gpio.h> |
Vincent Stehlé | 5c8be98 | 2015-02-11 23:08:59 +0100 | [diff] [blame] | 12 | #include <linux/gpio/consumer.h> |
Kenneth Westfield | 08d0a55 | 2015-02-17 00:53:11 -0800 | [diff] [blame] | 13 | #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 Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 19 | #include <sound/soc.h> |
Kenneth Westfield | 08d0a55 | 2015-02-17 00:53:11 -0800 | [diff] [blame] | 20 | #include <sound/soc-dai.h> |
| 21 | #include <sound/soc-dapm.h> |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 22 | |
Mac Chiang | cec5b01f | 2019-06-19 18:18:33 +0800 | [diff] [blame] | 23 | struct max98357a_priv { |
Mac Chiang | cec5b01f | 2019-06-19 18:18:33 +0800 | [diff] [blame] | 24 | struct gpio_desc *sdmode; |
| 25 | unsigned int sdmode_delay; |
Tzung-Bi Shih | 04a646f | 2020-07-21 19:42:32 +0800 | [diff] [blame] | 26 | int sdmode_switch; |
Mac Chiang | cec5b01f | 2019-06-19 18:18:33 +0800 | [diff] [blame] | 27 | }; |
| 28 | |
Tzung-Bi Shih | 04a646f | 2020-07-21 19:42:32 +0800 | [diff] [blame] | 29 | static 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 Shih | 128f825a | 2020-02-12 13:55:16 +0800 | [diff] [blame] | 60 | static int max98357a_sdmode_event(struct snd_soc_dapm_widget *w, |
| 61 | struct snd_kcontrol *kcontrol, int event) |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 62 | { |
Tzung-Bi Shih | 128f825a | 2020-02-12 13:55:16 +0800 | [diff] [blame] | 63 | 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 Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 67 | |
Tzung-Bi Shih | 04a646f | 2020-07-21 19:42:32 +0800 | [diff] [blame] | 68 | 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 Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = { |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 77 | SND_SOC_DAPM_OUTPUT("Speaker"), |
Tzung-Bi Shih | 128f825a | 2020-02-12 13:55:16 +0800 | [diff] [blame] | 78 | SND_SOC_DAPM_OUT_DRV_E("SD_MODE", SND_SOC_NOPM, 0, 0, NULL, 0, |
| 79 | max98357a_sdmode_event, |
Tzung-Bi Shih | 04a646f | 2020-07-21 19:42:32 +0800 | [diff] [blame] | 80 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static const struct snd_soc_dapm_route max98357a_dapm_routes[] = { |
Tzung-Bi Shih | 128f825a | 2020-02-12 13:55:16 +0800 | [diff] [blame] | 84 | {"SD_MODE", NULL, "HiFi Playback"}, |
| 85 | {"Speaker", NULL, "SD_MODE"}, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 86 | }; |
| 87 | |
Kuninori Morimoto | 4b51e9f | 2018-01-29 04:09:20 +0000 | [diff] [blame] | 88 | static const struct snd_soc_component_driver max98357a_component_driver = { |
Kuninori Morimoto | 4b51e9f | 2018-01-29 04:09:20 +0000 | [diff] [blame] | 89 | .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 Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Tzung-Bi Shih | 04a646f | 2020-07-21 19:42:32 +0800 | [diff] [blame] | 99 | static const struct snd_soc_dai_ops max98357a_dai_ops = { |
| 100 | .trigger = max98357a_daiops_trigger, |
| 101 | }; |
| 102 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 103 | static struct snd_soc_dai_driver max98357a_dai_driver = { |
Kenneth Westfield | 92b2ad2 | 2015-02-24 22:39:04 -0800 | [diff] [blame] | 104 | .name = "HiFi", |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 105 | .playback = { |
Kenneth Westfield | 92b2ad2 | 2015-02-24 22:39:04 -0800 | [diff] [blame] | 106 | .stream_name = "HiFi Playback", |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 107 | .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 Brunet | fdf3436 | 2019-04-04 13:50:15 +0200 | [diff] [blame] | 112 | SNDRV_PCM_RATE_32000 | |
| 113 | SNDRV_PCM_RATE_44100 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 114 | SNDRV_PCM_RATE_48000 | |
Jerome Brunet | fdf3436 | 2019-04-04 13:50:15 +0200 | [diff] [blame] | 115 | SNDRV_PCM_RATE_88200 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 116 | SNDRV_PCM_RATE_96000, |
| 117 | .rate_min = 8000, |
| 118 | .rate_max = 96000, |
| 119 | .channels_min = 1, |
| 120 | .channels_max = 2, |
| 121 | }, |
Tzung-Bi Shih | 04a646f | 2020-07-21 19:42:32 +0800 | [diff] [blame] | 122 | .ops = &max98357a_dai_ops, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static int max98357a_platform_probe(struct platform_device *pdev) |
| 126 | { |
Mac Chiang | cec5b01f | 2019-06-19 18:18:33 +0800 | [diff] [blame] | 127 | struct max98357a_priv *max98357a; |
| 128 | int ret; |
Tzung-Bi Shih | bcd9382 | 2019-05-09 11:04:54 +0800 | [diff] [blame] | 129 | |
Mac Chiang | cec5b01f | 2019-06-19 18:18:33 +0800 | [diff] [blame] | 130 | max98357a = devm_kzalloc(&pdev->dev, sizeof(*max98357a), GFP_KERNEL); |
Mac Chiang | cec5b01f | 2019-06-19 18:18:33 +0800 | [diff] [blame] | 131 | if (!max98357a) |
| 132 | return -ENOMEM; |
| 133 | |
| 134 | max98357a->sdmode = devm_gpiod_get_optional(&pdev->dev, |
Tzung-Bi Shih | bcd9382 | 2019-05-09 11:04:54 +0800 | [diff] [blame] | 135 | "sdmode", GPIOD_OUT_LOW); |
Mac Chiang | cec5b01f | 2019-06-19 18:18:33 +0800 | [diff] [blame] | 136 | 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 Chiang | cec5b01f | 2019-06-19 18:18:33 +0800 | [diff] [blame] | 141 | if (ret) { |
| 142 | max98357a->sdmode_delay = 0; |
| 143 | dev_dbg(&pdev->dev, |
Tzung-Bi Shih | 6cd249c | 2019-07-08 22:19:01 +0800 | [diff] [blame] | 144 | "no optional property 'sdmode-delay' found, " |
| 145 | "default: no delay\n"); |
Mac Chiang | cec5b01f | 2019-06-19 18:18:33 +0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | dev_set_drvdata(&pdev->dev, max98357a); |
| 149 | |
Kuninori Morimoto | 4b51e9f | 2018-01-29 04:09:20 +0000 | [diff] [blame] | 150 | return devm_snd_soc_register_component(&pdev->dev, |
| 151 | &max98357a_component_driver, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 152 | &max98357a_dai_driver, 1); |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 155 | #ifdef CONFIG_OF |
| 156 | static const struct of_device_id max98357a_device_id[] = { |
Kenneth Westfield | 7ff5eab | 2015-02-17 00:53:12 -0800 | [diff] [blame] | 157 | { .compatible = "maxim,max98357a" }, |
Tzung-Bi Shih | 3aad07b | 2020-06-05 11:49:30 +0800 | [diff] [blame] | 158 | { .compatible = "maxim,max98360a" }, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 159 | {} |
| 160 | }; |
Mark Brown | 3efa130 | 2015-02-09 14:36:47 +0800 | [diff] [blame] | 161 | MODULE_DEVICE_TABLE(of, max98357a_device_id); |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 162 | #endif |
| 163 | |
Rohit Ainapure | 5c27087 | 2015-12-11 11:29:06 -0800 | [diff] [blame] | 164 | #ifdef CONFIG_ACPI |
| 165 | static const struct acpi_device_id max98357a_acpi_match[] = { |
| 166 | { "MX98357A", 0 }, |
Yong Zhi | 1a0f243 | 2020-03-13 10:55:26 -0500 | [diff] [blame] | 167 | { "MX98360A", 0 }, |
Rohit Ainapure | 5c27087 | 2015-12-11 11:29:06 -0800 | [diff] [blame] | 168 | {}, |
| 169 | }; |
| 170 | MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match); |
| 171 | #endif |
| 172 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 173 | static struct platform_driver max98357a_platform_driver = { |
| 174 | .driver = { |
Kenneth Westfield | 7ff5eab | 2015-02-17 00:53:12 -0800 | [diff] [blame] | 175 | .name = "max98357a", |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 176 | .of_match_table = of_match_ptr(max98357a_device_id), |
Rohit Ainapure | 5c27087 | 2015-12-11 11:29:06 -0800 | [diff] [blame] | 177 | .acpi_match_table = ACPI_PTR(max98357a_acpi_match), |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 178 | }, |
| 179 | .probe = max98357a_platform_probe, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 180 | }; |
| 181 | module_platform_driver(max98357a_platform_driver); |
| 182 | |
| 183 | MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver"); |
| 184 | MODULE_LICENSE("GPL v2"); |