1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * SoC audio for EDB93xx 4 * 5 * Copyright (c) 2010 Alexander Sverdlin <subaparts@yandex.ru> 6 * 7 * This driver support CS4271 codec being master or slave, working 8 * in control port mode, connected either via SPI or I2C. 9 * The data format accepted is I2S or left-justified. 10 * DAPM support not implemented. 11 */ 12 13 #include <linux/platform_device.h> 14 #include <linux/gpio.h> 15 #include <linux/module.h> 16 #include <linux/soc/cirrus/ep93xx.h> 17 #include <sound/core.h> 18 #include <sound/pcm.h> 19 #include <sound/soc.h> 20 #include <asm/mach-types.h> 21 22 static int edb93xx_hw_params(struct snd_pcm_substream *substream, 23 struct snd_pcm_hw_params *params) 24 { 25 struct snd_soc_pcm_runtime *rtd = substream->private_data; 26 struct snd_soc_dai *codec_dai = rtd->codec_dai; 27 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 28 int err; 29 unsigned int mclk_rate; 30 unsigned int rate = params_rate(params); 31 32 /* 33 * According to CS4271 datasheet we use MCLK/LRCK=256 for 34 * rates below 50kHz and 128 for higher sample rates 35 */ 36 if (rate < 50000) 37 mclk_rate = rate * 64 * 4; 38 else 39 mclk_rate = rate * 64 * 2; 40 41 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, 42 SND_SOC_CLOCK_IN); 43 if (err) 44 return err; 45 46 return snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, 47 SND_SOC_CLOCK_OUT); 48 } 49 50 static const struct snd_soc_ops edb93xx_ops = { 51 .hw_params = edb93xx_hw_params, 52 }; 53 54 static struct snd_soc_dai_link edb93xx_dai = { 55 .name = "CS4271", 56 .stream_name = "CS4271 HiFi", 57 .platform_name = "ep93xx-i2s", 58 .cpu_dai_name = "ep93xx-i2s", 59 .codec_name = "spi0.0", 60 .codec_dai_name = "cs4271-hifi", 61 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 62 SND_SOC_DAIFMT_CBS_CFS, 63 .ops = &edb93xx_ops, 64 }; 65 66 static struct snd_soc_card snd_soc_edb93xx = { 67 .name = "EDB93XX", 68 .owner = THIS_MODULE, 69 .dai_link = &edb93xx_dai, 70 .num_links = 1, 71 }; 72 73 static int edb93xx_probe(struct platform_device *pdev) 74 { 75 struct snd_soc_card *card = &snd_soc_edb93xx; 76 int ret; 77 78 ret = ep93xx_i2s_acquire(); 79 if (ret) 80 return ret; 81 82 card->dev = &pdev->dev; 83 84 ret = snd_soc_register_card(card); 85 if (ret) { 86 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", 87 ret); 88 ep93xx_i2s_release(); 89 } 90 91 return ret; 92 } 93 94 static int edb93xx_remove(struct platform_device *pdev) 95 { 96 struct snd_soc_card *card = platform_get_drvdata(pdev); 97 98 snd_soc_unregister_card(card); 99 ep93xx_i2s_release(); 100 101 return 0; 102 } 103 104 static struct platform_driver edb93xx_driver = { 105 .driver = { 106 .name = "edb93xx-audio", 107 }, 108 .probe = edb93xx_probe, 109 .remove = edb93xx_remove, 110 }; 111 112 module_platform_driver(edb93xx_driver); 113 114 MODULE_AUTHOR("Alexander Sverdlin <subaparts@yandex.ru>"); 115 MODULE_DESCRIPTION("ALSA SoC EDB93xx"); 116 MODULE_LICENSE("GPL"); 117 MODULE_ALIAS("platform:edb93xx-audio"); 118