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 | |
Kenneth Westfield | 08d0a55 | 2015-02-17 00:53:11 -0800 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/err.h> |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 17 | #include <linux/gpio.h> |
Vincent Stehlé | 5c8be98 | 2015-02-11 23:08:59 +0100 | [diff] [blame] | 18 | #include <linux/gpio/consumer.h> |
Kenneth Westfield | 08d0a55 | 2015-02-17 00:53:11 -0800 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | #include <linux/mod_devicetable.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <sound/pcm.h> |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 25 | #include <sound/soc.h> |
Kenneth Westfield | 08d0a55 | 2015-02-17 00:53:11 -0800 | [diff] [blame] | 26 | #include <sound/soc-dai.h> |
| 27 | #include <sound/soc-dapm.h> |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 28 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -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 gpio_desc *sdmode = snd_soc_dai_get_drvdata(dai); |
| 33 | |
Anatol Pomozov | 5119222 | 2015-07-12 08:14:19 -0700 | [diff] [blame] | 34 | if (!sdmode) |
| 35 | return 0; |
| 36 | |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 37 | switch (cmd) { |
| 38 | case SNDRV_PCM_TRIGGER_START: |
| 39 | case SNDRV_PCM_TRIGGER_RESUME: |
| 40 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 41 | gpiod_set_value(sdmode, 1); |
| 42 | break; |
| 43 | case SNDRV_PCM_TRIGGER_STOP: |
| 44 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 45 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 46 | gpiod_set_value(sdmode, 0); |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = { |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 54 | SND_SOC_DAPM_OUTPUT("Speaker"), |
| 55 | }; |
| 56 | |
| 57 | static const struct snd_soc_dapm_route max98357a_dapm_routes[] = { |
Anatol Pomozov | 508e619 | 2015-07-12 08:14:20 -0700 | [diff] [blame^] | 58 | {"Speaker", NULL, "HiFi Playback"}, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static int max98357a_codec_probe(struct snd_soc_codec *codec) |
| 62 | { |
| 63 | struct gpio_desc *sdmode; |
| 64 | |
Anatol Pomozov | 5119222 | 2015-07-12 08:14:19 -0700 | [diff] [blame] | 65 | sdmode = devm_gpiod_get_optional(codec->dev, "sdmode", GPIOD_OUT_LOW); |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 66 | if (IS_ERR(sdmode)) { |
| 67 | dev_err(codec->dev, "%s() unable to get sdmode GPIO: %ld\n", |
| 68 | __func__, PTR_ERR(sdmode)); |
| 69 | return PTR_ERR(sdmode); |
| 70 | } |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 71 | snd_soc_codec_set_drvdata(codec, sdmode); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static struct snd_soc_codec_driver max98357a_codec_driver = { |
| 77 | .probe = max98357a_codec_probe, |
| 78 | .dapm_widgets = max98357a_dapm_widgets, |
| 79 | .num_dapm_widgets = ARRAY_SIZE(max98357a_dapm_widgets), |
| 80 | .dapm_routes = max98357a_dapm_routes, |
| 81 | .num_dapm_routes = ARRAY_SIZE(max98357a_dapm_routes), |
| 82 | }; |
| 83 | |
| 84 | static struct snd_soc_dai_ops max98357a_dai_ops = { |
| 85 | .trigger = max98357a_daiops_trigger, |
| 86 | }; |
| 87 | |
| 88 | static struct snd_soc_dai_driver max98357a_dai_driver = { |
Kenneth Westfield | 92b2ad2 | 2015-02-24 22:39:04 -0800 | [diff] [blame] | 89 | .name = "HiFi", |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 90 | .playback = { |
Kenneth Westfield | 92b2ad2 | 2015-02-24 22:39:04 -0800 | [diff] [blame] | 91 | .stream_name = "HiFi Playback", |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 92 | .formats = SNDRV_PCM_FMTBIT_S16 | |
| 93 | SNDRV_PCM_FMTBIT_S24 | |
| 94 | SNDRV_PCM_FMTBIT_S32, |
| 95 | .rates = SNDRV_PCM_RATE_8000 | |
| 96 | SNDRV_PCM_RATE_16000 | |
| 97 | SNDRV_PCM_RATE_48000 | |
| 98 | SNDRV_PCM_RATE_96000, |
| 99 | .rate_min = 8000, |
| 100 | .rate_max = 96000, |
| 101 | .channels_min = 1, |
| 102 | .channels_max = 2, |
| 103 | }, |
| 104 | .ops = &max98357a_dai_ops, |
| 105 | }; |
| 106 | |
| 107 | static int max98357a_platform_probe(struct platform_device *pdev) |
| 108 | { |
| 109 | int ret; |
| 110 | |
| 111 | ret = snd_soc_register_codec(&pdev->dev, &max98357a_codec_driver, |
| 112 | &max98357a_dai_driver, 1); |
| 113 | if (ret) |
| 114 | dev_err(&pdev->dev, "%s() error registering codec driver: %d\n", |
| 115 | __func__, ret); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static int max98357a_platform_remove(struct platform_device *pdev) |
| 121 | { |
| 122 | snd_soc_unregister_codec(&pdev->dev); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | #ifdef CONFIG_OF |
| 128 | static const struct of_device_id max98357a_device_id[] = { |
Kenneth Westfield | 7ff5eab | 2015-02-17 00:53:12 -0800 | [diff] [blame] | 129 | { .compatible = "maxim,max98357a" }, |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 130 | {} |
| 131 | }; |
Mark Brown | 3efa130 | 2015-02-09 14:36:47 +0800 | [diff] [blame] | 132 | MODULE_DEVICE_TABLE(of, max98357a_device_id); |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 133 | #endif |
| 134 | |
| 135 | static struct platform_driver max98357a_platform_driver = { |
| 136 | .driver = { |
Kenneth Westfield | 7ff5eab | 2015-02-17 00:53:12 -0800 | [diff] [blame] | 137 | .name = "max98357a", |
Kenneth Westfield | af5adf1 | 2015-02-05 12:53:40 -0800 | [diff] [blame] | 138 | .of_match_table = of_match_ptr(max98357a_device_id), |
| 139 | }, |
| 140 | .probe = max98357a_platform_probe, |
| 141 | .remove = max98357a_platform_remove, |
| 142 | }; |
| 143 | module_platform_driver(max98357a_platform_driver); |
| 144 | |
| 145 | MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver"); |
| 146 | MODULE_LICENSE("GPL v2"); |