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