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 delayed_work enable_sdmode_work; 24 struct gpio_desc *sdmode; 25 unsigned int sdmode_delay; 26 }; 27 28 static void max98357a_enable_sdmode_work(struct work_struct *work) 29 { 30 struct max98357a_priv *max98357a = 31 container_of(work, struct max98357a_priv, 32 enable_sdmode_work.work); 33 34 gpiod_set_value(max98357a->sdmode, 1); 35 } 36 37 static int max98357a_daiops_trigger(struct snd_pcm_substream *substream, 38 int cmd, struct snd_soc_dai *dai) 39 { 40 struct max98357a_priv *max98357a = snd_soc_dai_get_drvdata(dai); 41 42 if (!max98357a->sdmode) 43 return 0; 44 45 switch (cmd) { 46 case SNDRV_PCM_TRIGGER_START: 47 case SNDRV_PCM_TRIGGER_RESUME: 48 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 49 queue_delayed_work(system_power_efficient_wq, 50 &max98357a->enable_sdmode_work, 51 msecs_to_jiffies(max98357a->sdmode_delay)); 52 break; 53 case SNDRV_PCM_TRIGGER_STOP: 54 case SNDRV_PCM_TRIGGER_SUSPEND: 55 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 56 cancel_delayed_work_sync(&max98357a->enable_sdmode_work); 57 gpiod_set_value(max98357a->sdmode, 0); 58 break; 59 } 60 61 return 0; 62 } 63 64 static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = { 65 SND_SOC_DAPM_OUTPUT("Speaker"), 66 }; 67 68 static const struct snd_soc_dapm_route max98357a_dapm_routes[] = { 69 {"Speaker", NULL, "HiFi Playback"}, 70 }; 71 72 static const struct snd_soc_component_driver max98357a_component_driver = { 73 .dapm_widgets = max98357a_dapm_widgets, 74 .num_dapm_widgets = ARRAY_SIZE(max98357a_dapm_widgets), 75 .dapm_routes = max98357a_dapm_routes, 76 .num_dapm_routes = ARRAY_SIZE(max98357a_dapm_routes), 77 .idle_bias_on = 1, 78 .use_pmdown_time = 1, 79 .endianness = 1, 80 .non_legacy_dai_naming = 1, 81 }; 82 83 static const struct snd_soc_dai_ops max98357a_dai_ops = { 84 .trigger = max98357a_daiops_trigger, 85 }; 86 87 static struct snd_soc_dai_driver max98357a_dai_driver = { 88 .name = "HiFi", 89 .playback = { 90 .stream_name = "HiFi Playback", 91 .formats = SNDRV_PCM_FMTBIT_S16 | 92 SNDRV_PCM_FMTBIT_S24 | 93 SNDRV_PCM_FMTBIT_S32, 94 .rates = SNDRV_PCM_RATE_8000 | 95 SNDRV_PCM_RATE_16000 | 96 SNDRV_PCM_RATE_32000 | 97 SNDRV_PCM_RATE_44100 | 98 SNDRV_PCM_RATE_48000 | 99 SNDRV_PCM_RATE_88200 | 100 SNDRV_PCM_RATE_96000, 101 .rate_min = 8000, 102 .rate_max = 96000, 103 .channels_min = 1, 104 .channels_max = 2, 105 }, 106 .ops = &max98357a_dai_ops, 107 }; 108 109 static int max98357a_platform_probe(struct platform_device *pdev) 110 { 111 struct max98357a_priv *max98357a; 112 int ret; 113 114 max98357a = devm_kzalloc(&pdev->dev, sizeof(*max98357a), GFP_KERNEL); 115 116 if (!max98357a) 117 return -ENOMEM; 118 119 max98357a->sdmode = devm_gpiod_get_optional(&pdev->dev, 120 "sdmode", GPIOD_OUT_LOW); 121 122 if (IS_ERR(max98357a->sdmode)) 123 return PTR_ERR(max98357a->sdmode); 124 125 ret = device_property_read_u32(&pdev->dev, "sdmode-delay", 126 &max98357a->sdmode_delay); 127 128 if (ret) { 129 max98357a->sdmode_delay = 0; 130 dev_dbg(&pdev->dev, 131 "no optional property 'sdmode-delay' found, default: no delay\n"); 132 } 133 134 dev_set_drvdata(&pdev->dev, max98357a); 135 136 INIT_DELAYED_WORK(&max98357a->enable_sdmode_work, 137 max98357a_enable_sdmode_work); 138 139 return devm_snd_soc_register_component(&pdev->dev, 140 &max98357a_component_driver, 141 &max98357a_dai_driver, 1); 142 } 143 144 #ifdef CONFIG_OF 145 static const struct of_device_id max98357a_device_id[] = { 146 { .compatible = "maxim,max98357a" }, 147 {} 148 }; 149 MODULE_DEVICE_TABLE(of, max98357a_device_id); 150 #endif 151 152 #ifdef CONFIG_ACPI 153 static const struct acpi_device_id max98357a_acpi_match[] = { 154 { "MX98357A", 0 }, 155 {}, 156 }; 157 MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match); 158 #endif 159 160 static struct platform_driver max98357a_platform_driver = { 161 .driver = { 162 .name = "max98357a", 163 .of_match_table = of_match_ptr(max98357a_device_id), 164 .acpi_match_table = ACPI_PTR(max98357a_acpi_match), 165 }, 166 .probe = max98357a_platform_probe, 167 }; 168 module_platform_driver(max98357a_platform_driver); 169 170 MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver"); 171 MODULE_LICENSE("GPL v2"); 172