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 22 struct clk *clk_i2s_bus; 23 struct clk *sclk_i2s; 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); 39 unsigned int pll_freq, rclk_freq, rfs; 40 int ret; 41 42 switch (params_rate(params)) { 43 case 64000: 44 pll_freq = 196608001U; 45 rfs = 384; 46 break; 47 case 44100: 48 case 88200: 49 case 176400: 50 pll_freq = 180633609U; 51 rfs = 512; 52 break; 53 case 32000: 54 case 48000: 55 case 96000: 56 case 192000: 57 pll_freq = 196608001U; 58 rfs = 512; 59 break; 60 default: 61 return -EINVAL; 62 } 63 64 ret = clk_set_rate(priv->clk_i2s_bus, pll_freq / 2 + 1); 65 if (ret < 0) 66 return ret; 67 68 /* 69 * We add 1 to the rclk_freq value in order to avoid too low clock 70 * frequency values due to the EPLL output frequency not being exact 71 * multiple of the audio sampling rate. 72 */ 73 rclk_freq = params_rate(params) * rfs + 1; 74 75 ret = clk_set_rate(priv->sclk_i2s, rclk_freq); 76 if (ret < 0) 77 return ret; 78 79 if (rtd->num_codecs > 1) { 80 struct snd_soc_dai *codec_dai = rtd->codec_dais[1]; 81 82 ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk_freq, 83 SND_SOC_CLOCK_IN); 84 if (ret < 0) 85 return ret; 86 } 87 88 return 0; 89 } 90 91 static const struct snd_soc_ops odroid_card_ops = { 92 .startup = odroid_card_startup, 93 .hw_params = odroid_card_hw_params, 94 }; 95 96 static void odroid_put_codec_of_nodes(struct snd_soc_dai_link *link) 97 { 98 struct snd_soc_dai_link_component *component = link->codecs; 99 int i; 100 101 for (i = 0; i < link->num_codecs; i++, component++) { 102 if (!component->of_node) 103 break; 104 of_node_put(component->of_node); 105 } 106 } 107 108 static int odroid_audio_probe(struct platform_device *pdev) 109 { 110 struct device *dev = &pdev->dev; 111 struct device_node *cpu, *codec; 112 struct odroid_priv *priv; 113 struct snd_soc_dai_link *link; 114 struct snd_soc_card *card; 115 int ret; 116 117 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 118 if (!priv) 119 return -ENOMEM; 120 121 card = &priv->card; 122 card->dev = dev; 123 124 card->owner = THIS_MODULE; 125 card->fully_routed = true; 126 127 snd_soc_card_set_drvdata(card, priv); 128 129 ret = snd_soc_of_parse_card_name(card, "model"); 130 if (ret < 0) 131 return ret; 132 133 if (of_property_read_bool(dev->of_node, "samsung,audio-widgets")) { 134 ret = snd_soc_of_parse_audio_simple_widgets(card, 135 "samsung,audio-widgets"); 136 if (ret < 0) 137 return ret; 138 } 139 140 if (of_property_read_bool(dev->of_node, "samsung,audio-routing")) { 141 ret = snd_soc_of_parse_audio_routing(card, 142 "samsung,audio-routing"); 143 if (ret < 0) 144 return ret; 145 } 146 147 link = &priv->dai_link; 148 149 link->ops = &odroid_card_ops; 150 link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 151 SND_SOC_DAIFMT_CBS_CFS; 152 153 card->dai_link = &priv->dai_link; 154 card->num_links = 1; 155 156 cpu = of_get_child_by_name(dev->of_node, "cpu"); 157 codec = of_get_child_by_name(dev->of_node, "codec"); 158 159 link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0); 160 if (!link->cpu_of_node) { 161 dev_err(dev, "Failed parsing cpu/sound-dai property\n"); 162 return -EINVAL; 163 } 164 165 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link); 166 if (ret < 0) 167 goto err_put_codec_n; 168 169 link->platform_of_node = link->cpu_of_node; 170 171 link->name = "Primary"; 172 link->stream_name = link->name; 173 174 175 priv->sclk_i2s = of_clk_get_by_name(link->cpu_of_node, "i2s_opclk1"); 176 if (IS_ERR(priv->sclk_i2s)) { 177 ret = PTR_ERR(priv->sclk_i2s); 178 goto err_put_i2s_n; 179 } 180 181 priv->clk_i2s_bus = of_clk_get_by_name(link->cpu_of_node, "iis"); 182 if (IS_ERR(priv->clk_i2s_bus)) { 183 ret = PTR_ERR(priv->clk_i2s_bus); 184 goto err_put_sclk; 185 } 186 187 ret = devm_snd_soc_register_card(dev, card); 188 if (ret < 0) { 189 dev_err(dev, "snd_soc_register_card() failed: %d\n", ret); 190 goto err_put_clk_i2s; 191 } 192 193 return 0; 194 195 err_put_clk_i2s: 196 clk_put(priv->clk_i2s_bus); 197 err_put_sclk: 198 clk_put(priv->sclk_i2s); 199 err_put_i2s_n: 200 of_node_put(link->cpu_of_node); 201 err_put_codec_n: 202 odroid_put_codec_of_nodes(link); 203 return ret; 204 } 205 206 static int odroid_audio_remove(struct platform_device *pdev) 207 { 208 struct odroid_priv *priv = platform_get_drvdata(pdev); 209 210 of_node_put(priv->dai_link.cpu_of_node); 211 odroid_put_codec_of_nodes(&priv->dai_link); 212 clk_put(priv->sclk_i2s); 213 clk_put(priv->clk_i2s_bus); 214 215 return 0; 216 } 217 218 static const struct of_device_id odroid_audio_of_match[] = { 219 { .compatible = "hardkernel,odroid-xu3-audio" }, 220 { .compatible = "hardkernel,odroid-xu4-audio" }, 221 { .compatible = "samsung,odroid-xu3-audio" }, 222 { .compatible = "samsung,odroid-xu4-audio" }, 223 { }, 224 }; 225 MODULE_DEVICE_TABLE(of, odroid_audio_of_match); 226 227 static struct platform_driver odroid_audio_driver = { 228 .driver = { 229 .name = "odroid-audio", 230 .of_match_table = odroid_audio_of_match, 231 .pm = &snd_soc_pm_ops, 232 }, 233 .probe = odroid_audio_probe, 234 .remove = odroid_audio_remove, 235 }; 236 module_platform_driver(odroid_audio_driver); 237 238 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); 239 MODULE_DESCRIPTION("Odroid XU3/XU4 audio support"); 240 MODULE_LICENSE("GPL v2"); 241