Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * sam9x5_wm8731 -- SoC audio for AT91SAM9X5-based boards |
| 3 | * that are using WM8731 as codec. |
| 4 | * |
| 5 | * Copyright (C) 2011 Atmel, |
| 6 | * Nicolas Ferre <nicolas.ferre@atmel.com> |
| 7 | * |
| 8 | * Copyright (C) 2013 Paratronic, |
| 9 | * Richard Genoud <richard.genoud@gmail.com> |
| 10 | * |
| 11 | * Based on sam9g20_wm8731.c by: |
| 12 | * Sedji Gaouaou <sedji.gaouaou@atmel.com> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify it |
| 15 | * under the terms of the GNU General Public License as published by the |
| 16 | * Free Software Foundation; either version 2 of the License, or (at your |
| 17 | * option) any later version. |
| 18 | * |
| 19 | */ |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/export.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/mod_devicetable.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/device.h> |
| 26 | |
| 27 | #include <sound/soc.h> |
| 28 | #include <sound/soc-dai.h> |
| 29 | #include <sound/soc-dapm.h> |
| 30 | |
| 31 | #include "../codecs/wm8731.h" |
| 32 | #include "atmel_ssc_dai.h" |
| 33 | |
| 34 | |
| 35 | #define MCLK_RATE 12288000 |
| 36 | |
| 37 | #define DRV_NAME "sam9x5-snd-wm8731" |
| 38 | |
| 39 | struct sam9x5_drvdata { |
| 40 | int ssc_id; |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * Logic for a wm8731 as connected on a at91sam9x5ek based board. |
| 45 | */ |
| 46 | static int sam9x5_wm8731_init(struct snd_soc_pcm_runtime *rtd) |
| 47 | { |
| 48 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 49 | struct device *dev = rtd->dev; |
| 50 | int ret; |
| 51 | |
Ladislav Michl | 2efb8a8 | 2018-01-15 08:53:21 +0100 | [diff] [blame] | 52 | dev_dbg(dev, "%s called\n", __func__); |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 53 | |
| 54 | /* set the codec system clock for DAC and ADC */ |
| 55 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, |
| 56 | MCLK_RATE, SND_SOC_CLOCK_IN); |
| 57 | if (ret < 0) { |
Ladislav Michl | 2efb8a8 | 2018-01-15 08:53:21 +0100 | [diff] [blame] | 58 | dev_err(dev, "Failed to set WM8731 SYSCLK: %d\n", ret); |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Audio paths on at91sam9x5ek board: |
| 67 | * |
| 68 | * |A| ------------> | | ---R----> Headphone Jack |
| 69 | * |T| <----\ | WM | ---L--/ |
| 70 | * |9| ---> CLK <--> | 8731 | <--R----- Line In Jack |
| 71 | * |1| <------------ | | <--L--/ |
| 72 | */ |
| 73 | static const struct snd_soc_dapm_widget sam9x5_dapm_widgets[] = { |
| 74 | SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 75 | SND_SOC_DAPM_LINE("Line In Jack", NULL), |
| 76 | }; |
| 77 | |
| 78 | static int sam9x5_wm8731_driver_probe(struct platform_device *pdev) |
| 79 | { |
| 80 | struct device_node *np = pdev->dev.of_node; |
| 81 | struct device_node *codec_np, *cpu_np; |
| 82 | struct snd_soc_card *card; |
| 83 | struct snd_soc_dai_link *dai; |
| 84 | struct sam9x5_drvdata *priv; |
Kuninori Morimoto | 35617d8 | 2019-06-06 13:13:42 +0900 | [diff] [blame^] | 85 | struct snd_soc_dai_link_component *comp; |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 86 | int ret; |
| 87 | |
| 88 | if (!np) { |
| 89 | dev_err(&pdev->dev, "No device node supplied\n"); |
| 90 | return -EINVAL; |
| 91 | } |
| 92 | |
| 93 | card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL); |
| 94 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 95 | dai = devm_kzalloc(&pdev->dev, sizeof(*dai), GFP_KERNEL); |
Kuninori Morimoto | 35617d8 | 2019-06-06 13:13:42 +0900 | [diff] [blame^] | 96 | comp = devm_kzalloc(&pdev->dev, 3 * sizeof(*comp), GFP_KERNEL); |
| 97 | if (!dai || !card || !priv || !comp) { |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 98 | ret = -ENOMEM; |
| 99 | goto out; |
| 100 | } |
| 101 | |
Bo Shen | 46bec25 | 2013-11-27 18:05:09 +0800 | [diff] [blame] | 102 | snd_soc_card_set_drvdata(card, priv); |
| 103 | |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 104 | card->dev = &pdev->dev; |
| 105 | card->owner = THIS_MODULE; |
| 106 | card->dai_link = dai; |
| 107 | card->num_links = 1; |
| 108 | card->dapm_widgets = sam9x5_dapm_widgets; |
| 109 | card->num_dapm_widgets = ARRAY_SIZE(sam9x5_dapm_widgets); |
Kuninori Morimoto | 35617d8 | 2019-06-06 13:13:42 +0900 | [diff] [blame^] | 110 | |
| 111 | dai->cpus = &comp[0]; |
| 112 | dai->num_cpus = 1; |
| 113 | dai->codecs = &comp[1]; |
| 114 | dai->num_codecs = 1; |
| 115 | dai->platforms = &comp[2]; |
| 116 | dai->num_platforms = 1; |
| 117 | |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 118 | dai->name = "WM8731"; |
| 119 | dai->stream_name = "WM8731 PCM"; |
Kuninori Morimoto | 35617d8 | 2019-06-06 13:13:42 +0900 | [diff] [blame^] | 120 | dai->codecs->dai_name = "wm8731-hifi"; |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 121 | dai->init = sam9x5_wm8731_init; |
Bo Shen | bc567a9 | 2013-12-04 10:37:04 +0800 | [diff] [blame] | 122 | dai->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 123 | | SND_SOC_DAIFMT_CBM_CFM; |
| 124 | |
| 125 | ret = snd_soc_of_parse_card_name(card, "atmel,model"); |
| 126 | if (ret) { |
| 127 | dev_err(&pdev->dev, "atmel,model node missing\n"); |
| 128 | goto out; |
| 129 | } |
| 130 | |
| 131 | ret = snd_soc_of_parse_audio_routing(card, "atmel,audio-routing"); |
| 132 | if (ret) { |
| 133 | dev_err(&pdev->dev, "atmel,audio-routing node missing\n"); |
| 134 | goto out; |
| 135 | } |
| 136 | |
| 137 | codec_np = of_parse_phandle(np, "atmel,audio-codec", 0); |
| 138 | if (!codec_np) { |
| 139 | dev_err(&pdev->dev, "atmel,audio-codec node missing\n"); |
| 140 | ret = -EINVAL; |
| 141 | goto out; |
| 142 | } |
| 143 | |
Kuninori Morimoto | 35617d8 | 2019-06-06 13:13:42 +0900 | [diff] [blame^] | 144 | dai->codecs->of_node = codec_np; |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 145 | |
| 146 | cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0); |
| 147 | if (!cpu_np) { |
| 148 | dev_err(&pdev->dev, "atmel,ssc-controller node missing\n"); |
| 149 | ret = -EINVAL; |
| 150 | goto out; |
| 151 | } |
Kuninori Morimoto | 35617d8 | 2019-06-06 13:13:42 +0900 | [diff] [blame^] | 152 | dai->cpus->of_node = cpu_np; |
| 153 | dai->platforms->of_node = cpu_np; |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 154 | |
| 155 | priv->ssc_id = of_alias_get_id(cpu_np, "ssc"); |
| 156 | |
| 157 | ret = atmel_ssc_set_audio(priv->ssc_id); |
| 158 | if (ret != 0) { |
Ladislav Michl | 2efb8a8 | 2018-01-15 08:53:21 +0100 | [diff] [blame] | 159 | dev_err(&pdev->dev, "Failed to set SSC %d for audio: %d\n", |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 160 | ret, priv->ssc_id); |
| 161 | goto out; |
| 162 | } |
| 163 | |
| 164 | of_node_put(codec_np); |
| 165 | of_node_put(cpu_np); |
| 166 | |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 167 | ret = snd_soc_register_card(card); |
| 168 | if (ret) { |
Ladislav Michl | 2efb8a8 | 2018-01-15 08:53:21 +0100 | [diff] [blame] | 169 | dev_err(&pdev->dev, "Platform device allocation failed\n"); |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 170 | goto out_put_audio; |
| 171 | } |
| 172 | |
Ladislav Michl | 2efb8a8 | 2018-01-15 08:53:21 +0100 | [diff] [blame] | 173 | dev_dbg(&pdev->dev, "%s ok\n", __func__); |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 174 | |
| 175 | return ret; |
| 176 | |
| 177 | out_put_audio: |
| 178 | atmel_ssc_put_audio(priv->ssc_id); |
| 179 | out: |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static int sam9x5_wm8731_driver_remove(struct platform_device *pdev) |
| 184 | { |
| 185 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 186 | struct sam9x5_drvdata *priv = card->drvdata; |
| 187 | |
| 188 | snd_soc_unregister_card(card); |
| 189 | atmel_ssc_put_audio(priv->ssc_id); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static const struct of_device_id sam9x5_wm8731_of_match[] = { |
| 195 | { .compatible = "atmel,sam9x5-wm8731-audio", }, |
| 196 | {}, |
| 197 | }; |
| 198 | MODULE_DEVICE_TABLE(of, sam9x5_wm8731_of_match); |
| 199 | |
| 200 | static struct platform_driver sam9x5_wm8731_driver = { |
| 201 | .driver = { |
| 202 | .name = DRV_NAME, |
Nicolas Ferre | fdbcb3c | 2013-07-30 12:32:03 +0200 | [diff] [blame] | 203 | .of_match_table = of_match_ptr(sam9x5_wm8731_of_match), |
| 204 | }, |
| 205 | .probe = sam9x5_wm8731_driver_probe, |
| 206 | .remove = sam9x5_wm8731_driver_remove, |
| 207 | }; |
| 208 | module_platform_driver(sam9x5_wm8731_driver); |
| 209 | |
| 210 | /* Module information */ |
| 211 | MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>"); |
| 212 | MODULE_AUTHOR("Richard Genoud <richard.genoud@gmail.com>"); |
| 213 | MODULE_DESCRIPTION("ALSA SoC machine driver for AT91SAM9x5 - WM8731"); |
| 214 | MODULE_LICENSE("GPL"); |
| 215 | MODULE_ALIAS("platform:" DRV_NAME); |