1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved. 3 * 4 * max98357a.c -- MAX98357A ALSA SoC Codec driver 5 */ 6 7 #include <linux/acpi.h> 8 #include <linux/device.h> 9 #include <linux/err.h> 10 #include <linux/gpio.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/kernel.h> 13 #include <linux/mod_devicetable.h> 14 #include <linux/module.h> 15 #include <linux/of.h> 16 #include <linux/platform_device.h> 17 #include <sound/pcm.h> 18 #include <sound/soc.h> 19 #include <sound/soc-dai.h> 20 #include <sound/soc-dapm.h> 21 22 struct max98357a_priv { 23 struct gpio_desc *sdmode; 24 unsigned int sdmode_delay; 25 }; 26 27 static int max98357a_daiops_trigger(struct snd_pcm_substream *substream, 28 int cmd, struct snd_soc_dai *dai) 29 { 30 struct max98357a_priv *max98357a = snd_soc_dai_get_drvdata(dai); 31 32 if (!max98357a->sdmode) 33 return 0; 34 35 switch (cmd) { 36 case SNDRV_PCM_TRIGGER_START: 37 case SNDRV_PCM_TRIGGER_RESUME: 38 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 39 mdelay(max98357a->sdmode_delay); 40 gpiod_set_value(max98357a->sdmode, 1); 41 break; 42 case SNDRV_PCM_TRIGGER_STOP: 43 case SNDRV_PCM_TRIGGER_SUSPEND: 44 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 45 gpiod_set_value(max98357a->sdmode, 0); 46 break; 47 } 48 49 return 0; 50 } 51 52 static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = { 53 SND_SOC_DAPM_OUTPUT("Speaker"), 54 }; 55 56 static const struct snd_soc_dapm_route max98357a_dapm_routes[] = { 57 {"Speaker", NULL, "HiFi Playback"}, 58 }; 59 60 static const struct snd_soc_component_driver max98357a_component_driver = { 61 .dapm_widgets = max98357a_dapm_widgets, 62 .num_dapm_widgets = ARRAY_SIZE(max98357a_dapm_widgets), 63 .dapm_routes = max98357a_dapm_routes, 64 .num_dapm_routes = ARRAY_SIZE(max98357a_dapm_routes), 65 .idle_bias_on = 1, 66 .use_pmdown_time = 1, 67 .endianness = 1, 68 .non_legacy_dai_naming = 1, 69 }; 70 71 static const struct snd_soc_dai_ops max98357a_dai_ops = { 72 .trigger = max98357a_daiops_trigger, 73 }; 74 75 static struct snd_soc_dai_driver max98357a_dai_driver = { 76 .name = "HiFi", 77 .playback = { 78 .stream_name = "HiFi Playback", 79 .formats = SNDRV_PCM_FMTBIT_S16 | 80 SNDRV_PCM_FMTBIT_S24 | 81 SNDRV_PCM_FMTBIT_S32, 82 .rates = SNDRV_PCM_RATE_8000 | 83 SNDRV_PCM_RATE_16000 | 84 SNDRV_PCM_RATE_32000 | 85 SNDRV_PCM_RATE_44100 | 86 SNDRV_PCM_RATE_48000 | 87 SNDRV_PCM_RATE_88200 | 88 SNDRV_PCM_RATE_96000, 89 .rate_min = 8000, 90 .rate_max = 96000, 91 .channels_min = 1, 92 .channels_max = 2, 93 }, 94 .ops = &max98357a_dai_ops, 95 }; 96 97 static int max98357a_platform_probe(struct platform_device *pdev) 98 { 99 struct max98357a_priv *max98357a; 100 int ret; 101 102 max98357a = devm_kzalloc(&pdev->dev, sizeof(*max98357a), GFP_KERNEL); 103 if (!max98357a) 104 return -ENOMEM; 105 106 max98357a->sdmode = devm_gpiod_get_optional(&pdev->dev, 107 "sdmode", GPIOD_OUT_LOW); 108 if (IS_ERR(max98357a->sdmode)) 109 return PTR_ERR(max98357a->sdmode); 110 111 ret = device_property_read_u32(&pdev->dev, "sdmode-delay", 112 &max98357a->sdmode_delay); 113 if (ret) { 114 max98357a->sdmode_delay = 0; 115 dev_dbg(&pdev->dev, 116 "no optional property 'sdmode-delay' found, " 117 "default: no delay\n"); 118 } 119 120 dev_set_drvdata(&pdev->dev, max98357a); 121 122 return devm_snd_soc_register_component(&pdev->dev, 123 &max98357a_component_driver, 124 &max98357a_dai_driver, 1); 125 } 126 127 #ifdef CONFIG_OF 128 static const struct of_device_id max98357a_device_id[] = { 129 { .compatible = "maxim,max98357a" }, 130 {} 131 }; 132 MODULE_DEVICE_TABLE(of, max98357a_device_id); 133 #endif 134 135 #ifdef CONFIG_ACPI 136 static const struct acpi_device_id max98357a_acpi_match[] = { 137 { "MX98357A", 0 }, 138 {}, 139 }; 140 MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match); 141 #endif 142 143 static struct platform_driver max98357a_platform_driver = { 144 .driver = { 145 .name = "max98357a", 146 .of_match_table = of_match_ptr(max98357a_device_id), 147 .acpi_match_table = ACPI_PTR(max98357a_acpi_match), 148 }, 149 .probe = max98357a_platform_probe, 150 }; 151 module_platform_driver(max98357a_platform_driver); 152 153 MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver"); 154 MODULE_LICENSE("GPL v2"); 155