Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 1 | /* 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 Ainapure | 5c27087 | 2015-12-11 11:29:06 -0800 | [diff] [blame] | 15 | #include <linux/acpi.h> |
Kenneth Westfield | 08d0a55 | 2015-02-17 00:53:11 -0800 | [diff] [blame] | 16 | #include <linux/device.h> |
| 17 | #include <linux/err.h> |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 18 | #include <linux/gpio.h> |
Vincent Stehlé | 5c8be98 | 2015-02-11 23:08:59 +0100 | [diff] [blame] | 19 | #include <linux/gpio/consumer.h> |
Kenneth Westfield | 08d0a55 | 2015-02-17 00:53:11 -0800 | [diff] [blame] | 20 | #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 Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 26 | #include <sound/soc.h> |
Kenneth Westfield | 08d0a55 | 2015-02-17 00:53:11 -0800 | [diff] [blame] | 27 | #include <sound/soc-dai.h> |
| 28 | #include <sound/soc-dapm.h> |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 29 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 30 | static 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 Pomozov | 5119222 | 2015-07-12 08:14:19 -0700 | [diff] [blame] | 35 | if (!sdmode) |
| 36 | return 0; |
| 37 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 38 | 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 | |
| 54 | static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = { |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 55 | SND_SOC_DAPM_OUTPUT("Speaker"), |
| 56 | }; |
| 57 | |
| 58 | static const struct snd_soc_dapm_route max98357a_dapm_routes[] = { |
Anatol Pomozov | 508e619 | 2015-07-12 08:14:20 -0700 | [diff] [blame] | 59 | {"Speaker", NULL, "HiFi Playback"}, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Kuninori Morimoto | 4b51e9f | 2018-01-29 04:09:20 +0000 | [diff] [blame] | 62 | static int max98357a_component_probe(struct snd_soc_component *component) |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 63 | { |
| 64 | struct gpio_desc *sdmode; |
| 65 | |
Kuninori Morimoto | 4b51e9f | 2018-01-29 04:09:20 +0000 | [diff] [blame] | 66 | sdmode = devm_gpiod_get_optional(component->dev, "sdmode", GPIOD_OUT_LOW); |
Anatol Pomozov | 4ab0c591 | 2015-07-12 08:14:21 -0700 | [diff] [blame] | 67 | if (IS_ERR(sdmode)) |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 68 | return PTR_ERR(sdmode); |
Anatol Pomozov | 4ab0c591 | 2015-07-12 08:14:21 -0700 | [diff] [blame] | 69 | |
Kuninori Morimoto | 4b51e9f | 2018-01-29 04:09:20 +0000 | [diff] [blame] | 70 | snd_soc_component_set_drvdata(component, sdmode); |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Kuninori Morimoto | 4b51e9f | 2018-01-29 04:09:20 +0000 | [diff] [blame] | 75 | static const struct snd_soc_component_driver max98357a_component_driver = { |
| 76 | .probe = max98357a_component_probe, |
| 77 | .dapm_widgets = max98357a_dapm_widgets, |
| 78 | .num_dapm_widgets = ARRAY_SIZE(max98357a_dapm_widgets), |
| 79 | .dapm_routes = max98357a_dapm_routes, |
| 80 | .num_dapm_routes = ARRAY_SIZE(max98357a_dapm_routes), |
| 81 | .idle_bias_on = 1, |
| 82 | .use_pmdown_time = 1, |
| 83 | .endianness = 1, |
| 84 | .non_legacy_dai_naming = 1, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Axel Lin | 6479304 | 2015-07-15 15:38:14 +0800 | [diff] [blame] | 87 | static const struct snd_soc_dai_ops max98357a_dai_ops = { |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 88 | .trigger = max98357a_daiops_trigger, |
| 89 | }; |
| 90 | |
| 91 | static struct snd_soc_dai_driver max98357a_dai_driver = { |
Kenneth Westfield | 92b2ad2 | 2015-02-24 22:39:04 -0800 | [diff] [blame] | 92 | .name = "HiFi", |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 93 | .playback = { |
Kenneth Westfield | 92b2ad2 | 2015-02-24 22:39:04 -0800 | [diff] [blame] | 94 | .stream_name = "HiFi Playback", |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 95 | .formats = SNDRV_PCM_FMTBIT_S16 | |
| 96 | SNDRV_PCM_FMTBIT_S24 | |
| 97 | SNDRV_PCM_FMTBIT_S32, |
| 98 | .rates = SNDRV_PCM_RATE_8000 | |
| 99 | SNDRV_PCM_RATE_16000 | |
Jerome Brunet | fdf3436 | 2019-04-04 13:50:15 +0200 | [diff] [blame] | 100 | SNDRV_PCM_RATE_32000 | |
| 101 | SNDRV_PCM_RATE_44100 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 102 | SNDRV_PCM_RATE_48000 | |
Jerome Brunet | fdf3436 | 2019-04-04 13:50:15 +0200 | [diff] [blame] | 103 | SNDRV_PCM_RATE_88200 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 104 | SNDRV_PCM_RATE_96000, |
| 105 | .rate_min = 8000, |
| 106 | .rate_max = 96000, |
| 107 | .channels_min = 1, |
| 108 | .channels_max = 2, |
| 109 | }, |
| 110 | .ops = &max98357a_dai_ops, |
| 111 | }; |
| 112 | |
| 113 | static int max98357a_platform_probe(struct platform_device *pdev) |
| 114 | { |
Kuninori Morimoto | 4b51e9f | 2018-01-29 04:09:20 +0000 | [diff] [blame] | 115 | return devm_snd_soc_register_component(&pdev->dev, |
| 116 | &max98357a_component_driver, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 117 | &max98357a_dai_driver, 1); |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int max98357a_platform_remove(struct platform_device *pdev) |
| 121 | { |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | #ifdef CONFIG_OF |
| 126 | static const struct of_device_id max98357a_device_id[] = { |
Kenneth Westfield | 7ff5eab | 2015-02-17 00:53:12 -0800 | [diff] [blame] | 127 | { .compatible = "maxim,max98357a" }, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 128 | {} |
| 129 | }; |
Mark Brown | 3efa130 | 2015-02-09 14:36:47 +0800 | [diff] [blame] | 130 | MODULE_DEVICE_TABLE(of, max98357a_device_id); |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 131 | #endif |
| 132 | |
Rohit Ainapure | 5c27087 | 2015-12-11 11:29:06 -0800 | [diff] [blame] | 133 | #ifdef CONFIG_ACPI |
| 134 | static const struct acpi_device_id max98357a_acpi_match[] = { |
| 135 | { "MX98357A", 0 }, |
| 136 | {}, |
| 137 | }; |
| 138 | MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match); |
| 139 | #endif |
| 140 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 141 | static struct platform_driver max98357a_platform_driver = { |
| 142 | .driver = { |
Kenneth Westfield | 7ff5eab | 2015-02-17 00:53:12 -0800 | [diff] [blame] | 143 | .name = "max98357a", |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 144 | .of_match_table = of_match_ptr(max98357a_device_id), |
Rohit Ainapure | 5c27087 | 2015-12-11 11:29:06 -0800 | [diff] [blame] | 145 | .acpi_match_table = ACPI_PTR(max98357a_acpi_match), |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 146 | }, |
| 147 | .probe = max98357a_platform_probe, |
| 148 | .remove = max98357a_platform_remove, |
| 149 | }; |
| 150 | module_platform_driver(max98357a_platform_driver); |
| 151 | |
| 152 | MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver"); |
| 153 | MODULE_LICENSE("GPL v2"); |