18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * osk5912.c  --  SoC audio for OSK 5912
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008 Mistral Solutions
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Contact: Arun KS  <arunks@mistralsolutions.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/clk.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <sound/core.h>
138c2ecf20Sopenharmony_ci#include <sound/pcm.h>
148c2ecf20Sopenharmony_ci#include <sound/soc.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <asm/mach-types.h>
178c2ecf20Sopenharmony_ci#include <linux/gpio.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_data/asoc-ti-mcbsp.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "omap-mcbsp.h"
228c2ecf20Sopenharmony_ci#include "../codecs/tlv320aic23.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define CODEC_CLOCK 	12000000
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic struct clk *tlv320aic23_mclk;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic int osk_startup(struct snd_pcm_substream *substream)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	return clk_enable(tlv320aic23_mclk);
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic void osk_shutdown(struct snd_pcm_substream *substream)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	clk_disable(tlv320aic23_mclk);
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int osk_hw_params(struct snd_pcm_substream *substream,
398c2ecf20Sopenharmony_ci			 struct snd_pcm_hw_params *params)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
428c2ecf20Sopenharmony_ci	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
438c2ecf20Sopenharmony_ci	int err;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* Set the codec system clock for DAC and ADC */
468c2ecf20Sopenharmony_ci	err =
478c2ecf20Sopenharmony_ci	    snd_soc_dai_set_sysclk(codec_dai, 0, CODEC_CLOCK, SND_SOC_CLOCK_IN);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (err < 0) {
508c2ecf20Sopenharmony_ci		printk(KERN_ERR "can't set codec system clock\n");
518c2ecf20Sopenharmony_ci		return err;
528c2ecf20Sopenharmony_ci	}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	return err;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic const struct snd_soc_ops osk_ops = {
588c2ecf20Sopenharmony_ci	.startup = osk_startup,
598c2ecf20Sopenharmony_ci	.hw_params = osk_hw_params,
608c2ecf20Sopenharmony_ci	.shutdown = osk_shutdown,
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
648c2ecf20Sopenharmony_ci	SND_SOC_DAPM_HP("Headphone Jack", NULL),
658c2ecf20Sopenharmony_ci	SND_SOC_DAPM_LINE("Line In", NULL),
668c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIC("Mic Jack", NULL),
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route audio_map[] = {
708c2ecf20Sopenharmony_ci	{"Headphone Jack", NULL, "LHPOUT"},
718c2ecf20Sopenharmony_ci	{"Headphone Jack", NULL, "RHPOUT"},
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	{"LLINEIN", NULL, "Line In"},
748c2ecf20Sopenharmony_ci	{"RLINEIN", NULL, "Line In"},
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	{"MICIN", NULL, "Mic Jack"},
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* Digital audio interface glue - connects codec <--> CPU */
808c2ecf20Sopenharmony_ciSND_SOC_DAILINK_DEFS(aic23,
818c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.1")),
828c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic23-codec",
838c2ecf20Sopenharmony_ci				      "tlv320aic23-hifi")),
848c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.1")));
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic struct snd_soc_dai_link osk_dai = {
878c2ecf20Sopenharmony_ci	.name = "TLV320AIC23",
888c2ecf20Sopenharmony_ci	.stream_name = "AIC23",
898c2ecf20Sopenharmony_ci	.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF |
908c2ecf20Sopenharmony_ci		   SND_SOC_DAIFMT_CBM_CFM,
918c2ecf20Sopenharmony_ci	.ops = &osk_ops,
928c2ecf20Sopenharmony_ci	SND_SOC_DAILINK_REG(aic23),
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/* Audio machine driver */
968c2ecf20Sopenharmony_cistatic struct snd_soc_card snd_soc_card_osk = {
978c2ecf20Sopenharmony_ci	.name = "OSK5912",
988c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
998c2ecf20Sopenharmony_ci	.dai_link = &osk_dai,
1008c2ecf20Sopenharmony_ci	.num_links = 1,
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	.dapm_widgets = tlv320aic23_dapm_widgets,
1038c2ecf20Sopenharmony_ci	.num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
1048c2ecf20Sopenharmony_ci	.dapm_routes = audio_map,
1058c2ecf20Sopenharmony_ci	.num_dapm_routes = ARRAY_SIZE(audio_map),
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic struct platform_device *osk_snd_device;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic int __init osk_soc_init(void)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	int err;
1138c2ecf20Sopenharmony_ci	u32 curRate;
1148c2ecf20Sopenharmony_ci	struct device *dev;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (!(machine_is_omap_osk()))
1178c2ecf20Sopenharmony_ci		return -ENODEV;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	osk_snd_device = platform_device_alloc("soc-audio", -1);
1208c2ecf20Sopenharmony_ci	if (!osk_snd_device)
1218c2ecf20Sopenharmony_ci		return -ENOMEM;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	platform_set_drvdata(osk_snd_device, &snd_soc_card_osk);
1248c2ecf20Sopenharmony_ci	err = platform_device_add(osk_snd_device);
1258c2ecf20Sopenharmony_ci	if (err)
1268c2ecf20Sopenharmony_ci		goto err1;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	dev = &osk_snd_device->dev;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	tlv320aic23_mclk = clk_get(dev, "mclk");
1318c2ecf20Sopenharmony_ci	if (IS_ERR(tlv320aic23_mclk)) {
1328c2ecf20Sopenharmony_ci		printk(KERN_ERR "Could not get mclk clock\n");
1338c2ecf20Sopenharmony_ci		err = PTR_ERR(tlv320aic23_mclk);
1348c2ecf20Sopenharmony_ci		goto err2;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	/*
1388c2ecf20Sopenharmony_ci	 * Configure 12 MHz output on MCLK.
1398c2ecf20Sopenharmony_ci	 */
1408c2ecf20Sopenharmony_ci	curRate = (uint) clk_get_rate(tlv320aic23_mclk);
1418c2ecf20Sopenharmony_ci	if (curRate != CODEC_CLOCK) {
1428c2ecf20Sopenharmony_ci		if (clk_set_rate(tlv320aic23_mclk, CODEC_CLOCK)) {
1438c2ecf20Sopenharmony_ci			printk(KERN_ERR "Cannot set MCLK for AIC23 CODEC\n");
1448c2ecf20Sopenharmony_ci			err = -ECANCELED;
1458c2ecf20Sopenharmony_ci			goto err3;
1468c2ecf20Sopenharmony_ci		}
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	printk(KERN_INFO "MCLK = %d [%d]\n",
1508c2ecf20Sopenharmony_ci	       (uint) clk_get_rate(tlv320aic23_mclk), CODEC_CLOCK);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	return 0;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cierr3:
1558c2ecf20Sopenharmony_ci	clk_put(tlv320aic23_mclk);
1568c2ecf20Sopenharmony_cierr2:
1578c2ecf20Sopenharmony_ci	platform_device_del(osk_snd_device);
1588c2ecf20Sopenharmony_cierr1:
1598c2ecf20Sopenharmony_ci	platform_device_put(osk_snd_device);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return err;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic void __exit osk_soc_exit(void)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	clk_put(tlv320aic23_mclk);
1688c2ecf20Sopenharmony_ci	platform_device_unregister(osk_snd_device);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cimodule_init(osk_soc_init);
1728c2ecf20Sopenharmony_cimodule_exit(osk_soc_exit);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ciMODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
1758c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ALSA SoC OSK 5912");
1768c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
177