Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Samsung Electronics Co., Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/clk.h> |
| 10 | #include <linux/of.h> |
| 11 | #include <linux/of_device.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <sound/soc.h> |
| 14 | #include <sound/pcm_params.h> |
| 15 | #include "i2s.h" |
| 16 | #include "i2s-regs.h" |
| 17 | |
| 18 | struct odroid_priv { |
| 19 | struct snd_soc_card card; |
| 20 | struct snd_soc_dai_link dai_link; |
| 21 | |
Sylwester Nawrocki | a8ad0c8 | 2017-08-04 12:58:17 +0200 | [diff] [blame] | 22 | struct clk *clk_i2s_bus; |
| 23 | struct clk *sclk_i2s; |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | static int odroid_card_startup(struct snd_pcm_substream *substream) |
| 27 | { |
| 28 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 29 | |
| 30 | snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static int odroid_card_hw_params(struct snd_pcm_substream *substream, |
| 35 | struct snd_pcm_hw_params *params) |
| 36 | { |
| 37 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 38 | struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
Sylwester Nawrocki | 1d22c33 | 2018-03-14 17:41:13 +0100 | [diff] [blame] | 39 | unsigned int pll_freq, rclk_freq, rfs; |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 40 | int ret; |
| 41 | |
| 42 | switch (params_rate(params)) { |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 43 | case 64000: |
Sylwester Nawrocki | 1d22c33 | 2018-03-14 17:41:13 +0100 | [diff] [blame] | 44 | pll_freq = 196608001U; |
| 45 | rfs = 384; |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 46 | break; |
| 47 | case 44100: |
| 48 | case 88200: |
Sylwester Nawrocki | 9d154e4 | 2017-07-21 18:29:20 +0200 | [diff] [blame] | 49 | pll_freq = 180633609U; |
Sylwester Nawrocki | 1d22c33 | 2018-03-14 17:41:13 +0100 | [diff] [blame] | 50 | rfs = 512; |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 51 | break; |
Sylwester Nawrocki | 1d22c33 | 2018-03-14 17:41:13 +0100 | [diff] [blame] | 52 | case 32000: |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 53 | case 48000: |
| 54 | case 96000: |
Sylwester Nawrocki | 9d154e4 | 2017-07-21 18:29:20 +0200 | [diff] [blame] | 55 | pll_freq = 196608001U; |
Sylwester Nawrocki | 1d22c33 | 2018-03-14 17:41:13 +0100 | [diff] [blame] | 56 | rfs = 512; |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 57 | break; |
| 58 | default: |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | |
Sylwester Nawrocki | a8ad0c8 | 2017-08-04 12:58:17 +0200 | [diff] [blame] | 62 | ret = clk_set_rate(priv->clk_i2s_bus, pll_freq / 2 + 1); |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 63 | if (ret < 0) |
| 64 | return ret; |
| 65 | |
Sylwester Nawrocki | a8ad0c8 | 2017-08-04 12:58:17 +0200 | [diff] [blame] | 66 | /* |
| 67 | * We add 1 to the rclk_freq value in order to avoid too low clock |
| 68 | * frequency values due to the EPLL output frequency not being exact |
| 69 | * multiple of the audio sampling rate. |
| 70 | */ |
Sylwester Nawrocki | 1d22c33 | 2018-03-14 17:41:13 +0100 | [diff] [blame] | 71 | rclk_freq = params_rate(params) * rfs + 1; |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 72 | |
Sylwester Nawrocki | a8ad0c8 | 2017-08-04 12:58:17 +0200 | [diff] [blame] | 73 | ret = clk_set_rate(priv->sclk_i2s, rclk_freq); |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 74 | if (ret < 0) |
| 75 | return ret; |
| 76 | |
| 77 | if (rtd->num_codecs > 1) { |
| 78 | struct snd_soc_dai *codec_dai = rtd->codec_dais[1]; |
| 79 | |
| 80 | ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk_freq, |
| 81 | SND_SOC_CLOCK_IN); |
| 82 | if (ret < 0) |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static const struct snd_soc_ops odroid_card_ops = { |
| 90 | .startup = odroid_card_startup, |
| 91 | .hw_params = odroid_card_hw_params, |
| 92 | }; |
| 93 | |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 94 | static int odroid_audio_probe(struct platform_device *pdev) |
| 95 | { |
| 96 | struct device *dev = &pdev->dev; |
| 97 | struct device_node *cpu, *codec; |
| 98 | struct odroid_priv *priv; |
| 99 | struct snd_soc_dai_link *link; |
| 100 | struct snd_soc_card *card; |
| 101 | int ret; |
| 102 | |
| 103 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 104 | if (!priv) |
| 105 | return -ENOMEM; |
| 106 | |
| 107 | card = &priv->card; |
| 108 | card->dev = dev; |
| 109 | |
| 110 | card->owner = THIS_MODULE; |
| 111 | card->fully_routed = true; |
| 112 | |
| 113 | snd_soc_card_set_drvdata(card, priv); |
| 114 | |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 115 | ret = snd_soc_of_parse_card_name(card, "model"); |
| 116 | if (ret < 0) |
| 117 | return ret; |
| 118 | |
| 119 | if (of_property_read_bool(dev->of_node, "samsung,audio-widgets")) { |
| 120 | ret = snd_soc_of_parse_audio_simple_widgets(card, |
| 121 | "samsung,audio-widgets"); |
| 122 | if (ret < 0) |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | if (of_property_read_bool(dev->of_node, "samsung,audio-routing")) { |
| 127 | ret = snd_soc_of_parse_audio_routing(card, |
| 128 | "samsung,audio-routing"); |
| 129 | if (ret < 0) |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | link = &priv->dai_link; |
| 134 | |
| 135 | link->ops = &odroid_card_ops; |
| 136 | link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 137 | SND_SOC_DAIFMT_CBS_CFS; |
| 138 | |
| 139 | card->dai_link = &priv->dai_link; |
| 140 | card->num_links = 1; |
| 141 | |
| 142 | cpu = of_get_child_by_name(dev->of_node, "cpu"); |
| 143 | codec = of_get_child_by_name(dev->of_node, "codec"); |
| 144 | |
| 145 | link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0); |
| 146 | if (!link->cpu_of_node) { |
| 147 | dev_err(dev, "Failed parsing cpu/sound-dai property\n"); |
| 148 | return -EINVAL; |
| 149 | } |
| 150 | |
| 151 | ret = snd_soc_of_get_dai_link_codecs(dev, codec, link); |
| 152 | if (ret < 0) |
| 153 | goto err_put_codec_n; |
| 154 | |
| 155 | link->platform_of_node = link->cpu_of_node; |
| 156 | |
| 157 | link->name = "Primary"; |
| 158 | link->stream_name = link->name; |
| 159 | |
Sylwester Nawrocki | a8ad0c8 | 2017-08-04 12:58:17 +0200 | [diff] [blame] | 160 | |
| 161 | priv->sclk_i2s = of_clk_get_by_name(link->cpu_of_node, "i2s_opclk1"); |
| 162 | if (IS_ERR(priv->sclk_i2s)) { |
| 163 | ret = PTR_ERR(priv->sclk_i2s); |
| 164 | goto err_put_i2s_n; |
| 165 | } |
| 166 | |
| 167 | priv->clk_i2s_bus = of_clk_get_by_name(link->cpu_of_node, "iis"); |
| 168 | if (IS_ERR(priv->clk_i2s_bus)) { |
| 169 | ret = PTR_ERR(priv->clk_i2s_bus); |
| 170 | goto err_put_sclk; |
| 171 | } |
| 172 | |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 173 | ret = devm_snd_soc_register_card(dev, card); |
| 174 | if (ret < 0) { |
| 175 | dev_err(dev, "snd_soc_register_card() failed: %d\n", ret); |
Sylwester Nawrocki | a8ad0c8 | 2017-08-04 12:58:17 +0200 | [diff] [blame] | 176 | goto err_put_clk_i2s; |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | |
Sylwester Nawrocki | a8ad0c8 | 2017-08-04 12:58:17 +0200 | [diff] [blame] | 181 | err_put_clk_i2s: |
| 182 | clk_put(priv->clk_i2s_bus); |
| 183 | err_put_sclk: |
| 184 | clk_put(priv->sclk_i2s); |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 185 | err_put_i2s_n: |
| 186 | of_node_put(link->cpu_of_node); |
| 187 | err_put_codec_n: |
Sylwester Nawrocki | 4718840 | 2018-03-14 17:41:15 +0100 | [diff] [blame] | 188 | snd_soc_of_put_dai_link_codecs(link); |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | static int odroid_audio_remove(struct platform_device *pdev) |
| 193 | { |
| 194 | struct odroid_priv *priv = platform_get_drvdata(pdev); |
| 195 | |
| 196 | of_node_put(priv->dai_link.cpu_of_node); |
Sylwester Nawrocki | 4718840 | 2018-03-14 17:41:15 +0100 | [diff] [blame] | 197 | snd_soc_of_put_dai_link_codecs(&priv->dai_link); |
Sylwester Nawrocki | a8ad0c8 | 2017-08-04 12:58:17 +0200 | [diff] [blame] | 198 | clk_put(priv->sclk_i2s); |
| 199 | clk_put(priv->clk_i2s_bus); |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static const struct of_device_id odroid_audio_of_match[] = { |
Sylwester Nawrocki | d9e5751 | 2018-03-07 18:46:25 +0100 | [diff] [blame] | 205 | { .compatible = "hardkernel,odroid-xu3-audio" }, |
| 206 | { .compatible = "hardkernel,odroid-xu4-audio" }, |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 207 | { .compatible = "samsung,odroid-xu3-audio" }, |
Sylwester Nawrocki | d9e5751 | 2018-03-07 18:46:25 +0100 | [diff] [blame] | 208 | { .compatible = "samsung,odroid-xu4-audio" }, |
Sylwester Nawrocki | aba611f | 2017-04-21 19:19:50 +0200 | [diff] [blame] | 209 | { }, |
| 210 | }; |
| 211 | MODULE_DEVICE_TABLE(of, odroid_audio_of_match); |
| 212 | |
| 213 | static struct platform_driver odroid_audio_driver = { |
| 214 | .driver = { |
| 215 | .name = "odroid-audio", |
| 216 | .of_match_table = odroid_audio_of_match, |
| 217 | .pm = &snd_soc_pm_ops, |
| 218 | }, |
| 219 | .probe = odroid_audio_probe, |
| 220 | .remove = odroid_audio_remove, |
| 221 | }; |
| 222 | module_platform_driver(odroid_audio_driver); |
| 223 | |
| 224 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); |
| 225 | MODULE_DESCRIPTION("Odroid XU3/XU4 audio support"); |
| 226 | MODULE_LICENSE("GPL v2"); |