| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * osk5912.c -- SoC audio for OSK 5912 |
| 4 | * |
| 5 | * Copyright (C) 2008 Mistral Solutions |
| 6 | * |
| 7 | * Contact: Arun KS <arunks@mistralsolutions.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <sound/core.h> |
| 13 | #include <sound/pcm.h> |
| 14 | #include <sound/soc.h> |
| 15 | |
| 16 | #include <asm/mach-types.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/platform_data/asoc-ti-mcbsp.h> |
| 19 | |
| 20 | #include "omap-mcbsp.h" |
| 21 | #include "../codecs/tlv320aic23.h" |
| 22 | |
| 23 | #define CODEC_CLOCK 12000000 |
| 24 | |
| 25 | static struct clk *tlv320aic23_mclk; |
| 26 | |
| 27 | static int osk_startup(struct snd_pcm_substream *substream) |
| 28 | { |
| 29 | return clk_prepare_enable(clk: tlv320aic23_mclk); |
| 30 | } |
| 31 | |
| 32 | static void osk_shutdown(struct snd_pcm_substream *substream) |
| 33 | { |
| 34 | clk_disable_unprepare(clk: tlv320aic23_mclk); |
| 35 | } |
| 36 | |
| 37 | static int osk_hw_params(struct snd_pcm_substream *substream, |
| 38 | struct snd_pcm_hw_params *params) |
| 39 | { |
| 40 | struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); |
| 41 | struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); |
| 42 | int err; |
| 43 | |
| 44 | /* Set the codec system clock for DAC and ADC */ |
| 45 | err = |
| 46 | snd_soc_dai_set_sysclk(dai: codec_dai, clk_id: 0, CODEC_CLOCK, SND_SOC_CLOCK_IN); |
| 47 | |
| 48 | if (err < 0) { |
| 49 | printk(KERN_ERR "can't set codec system clock\n" ); |
| 50 | return err; |
| 51 | } |
| 52 | |
| 53 | return err; |
| 54 | } |
| 55 | |
| 56 | static const struct snd_soc_ops osk_ops = { |
| 57 | .startup = osk_startup, |
| 58 | .hw_params = osk_hw_params, |
| 59 | .shutdown = osk_shutdown, |
| 60 | }; |
| 61 | |
| 62 | static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = { |
| 63 | SND_SOC_DAPM_HP("Headphone Jack" , NULL), |
| 64 | SND_SOC_DAPM_LINE("Line In" , NULL), |
| 65 | SND_SOC_DAPM_MIC("Mic Jack" , NULL), |
| 66 | }; |
| 67 | |
| 68 | static const struct snd_soc_dapm_route audio_map[] = { |
| 69 | {"Headphone Jack" , NULL, "LHPOUT" }, |
| 70 | {"Headphone Jack" , NULL, "RHPOUT" }, |
| 71 | |
| 72 | {"LLINEIN" , NULL, "Line In" }, |
| 73 | {"RLINEIN" , NULL, "Line In" }, |
| 74 | |
| 75 | {"MICIN" , NULL, "Mic Jack" }, |
| 76 | }; |
| 77 | |
| 78 | /* Digital audio interface glue - connects codec <--> CPU */ |
| 79 | SND_SOC_DAILINK_DEFS(aic23, |
| 80 | DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.1" )), |
| 81 | DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic23-codec" , |
| 82 | "tlv320aic23-hifi" )), |
| 83 | DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.1" ))); |
| 84 | |
| 85 | static struct snd_soc_dai_link osk_dai = { |
| 86 | .name = "TLV320AIC23" , |
| 87 | .stream_name = "AIC23" , |
| 88 | .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | |
| 89 | SND_SOC_DAIFMT_CBP_CFP, |
| 90 | .ops = &osk_ops, |
| 91 | SND_SOC_DAILINK_REG(aic23), |
| 92 | }; |
| 93 | |
| 94 | /* Audio machine driver */ |
| 95 | static struct snd_soc_card snd_soc_card_osk = { |
| 96 | .name = "OSK5912" , |
| 97 | .owner = THIS_MODULE, |
| 98 | .dai_link = &osk_dai, |
| 99 | .num_links = 1, |
| 100 | |
| 101 | .dapm_widgets = tlv320aic23_dapm_widgets, |
| 102 | .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets), |
| 103 | .dapm_routes = audio_map, |
| 104 | .num_dapm_routes = ARRAY_SIZE(audio_map), |
| 105 | }; |
| 106 | |
| 107 | static struct platform_device *osk_snd_device; |
| 108 | |
| 109 | static int __init osk_soc_init(void) |
| 110 | { |
| 111 | int err; |
| 112 | u32 curRate; |
| 113 | struct device *dev; |
| 114 | |
| 115 | if (!(machine_is_omap_osk())) |
| 116 | return -ENODEV; |
| 117 | |
| 118 | osk_snd_device = platform_device_alloc(name: "soc-audio" , id: -1); |
| 119 | if (!osk_snd_device) |
| 120 | return -ENOMEM; |
| 121 | |
| 122 | platform_set_drvdata(pdev: osk_snd_device, data: &snd_soc_card_osk); |
| 123 | err = platform_device_add(pdev: osk_snd_device); |
| 124 | if (err) |
| 125 | goto err1; |
| 126 | |
| 127 | dev = &osk_snd_device->dev; |
| 128 | |
| 129 | tlv320aic23_mclk = clk_get(dev, id: "mclk" ); |
| 130 | if (IS_ERR(ptr: tlv320aic23_mclk)) { |
| 131 | printk(KERN_ERR "Could not get mclk clock\n" ); |
| 132 | err = PTR_ERR(ptr: tlv320aic23_mclk); |
| 133 | goto err2; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Configure 12 MHz output on MCLK. |
| 138 | */ |
| 139 | curRate = (uint) clk_get_rate(clk: tlv320aic23_mclk); |
| 140 | if (curRate != CODEC_CLOCK) { |
| 141 | if (clk_set_rate(clk: tlv320aic23_mclk, CODEC_CLOCK)) { |
| 142 | printk(KERN_ERR "Cannot set MCLK for AIC23 CODEC\n" ); |
| 143 | err = -ECANCELED; |
| 144 | goto err3; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | printk(KERN_INFO "MCLK = %d [%d]\n" , |
| 149 | (uint) clk_get_rate(tlv320aic23_mclk), CODEC_CLOCK); |
| 150 | |
| 151 | return 0; |
| 152 | |
| 153 | err3: |
| 154 | clk_put(clk: tlv320aic23_mclk); |
| 155 | err2: |
| 156 | platform_device_del(pdev: osk_snd_device); |
| 157 | err1: |
| 158 | platform_device_put(pdev: osk_snd_device); |
| 159 | |
| 160 | return err; |
| 161 | |
| 162 | } |
| 163 | |
| 164 | static void __exit osk_soc_exit(void) |
| 165 | { |
| 166 | clk_put(clk: tlv320aic23_mclk); |
| 167 | platform_device_unregister(osk_snd_device); |
| 168 | } |
| 169 | |
| 170 | module_init(osk_soc_init); |
| 171 | module_exit(osk_soc_exit); |
| 172 | |
| 173 | MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>" ); |
| 174 | MODULE_DESCRIPTION("ALSA SoC OSK 5912" ); |
| 175 | MODULE_LICENSE("GPL" ); |
| 176 | |