18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Handles the Mitac mioa701 SoC system
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008 Robert Jarzmik
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This is a little schema of the sound interconnections :
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *    Sagem X200                 Wolfson WM9713
108c2ecf20Sopenharmony_ci *    +--------+             +-------------------+      Rear Speaker
118c2ecf20Sopenharmony_ci *    |        |             |                   |           /-+
128c2ecf20Sopenharmony_ci *    |        +--->----->---+MONOIN         SPKL+--->----+-+  |
138c2ecf20Sopenharmony_ci *    |  GSM   |             |                   |        | |  |
148c2ecf20Sopenharmony_ci *    |        +--->----->---+PCBEEP         SPKR+--->----+-+  |
158c2ecf20Sopenharmony_ci *    |  CHIP  |             |                   |           \-+
168c2ecf20Sopenharmony_ci *    |        +---<-----<---+MONO               |
178c2ecf20Sopenharmony_ci *    |        |             |                   |      Front Speaker
188c2ecf20Sopenharmony_ci *    +--------+             |                   |           /-+
198c2ecf20Sopenharmony_ci *                           |                HPL+--->----+-+  |
208c2ecf20Sopenharmony_ci *                           |                   |        | |  |
218c2ecf20Sopenharmony_ci *                           |               OUT3+--->----+-+  |
228c2ecf20Sopenharmony_ci *                           |                   |           \-+
238c2ecf20Sopenharmony_ci *                           |                   |
248c2ecf20Sopenharmony_ci *                           |                   |     Front Micro
258c2ecf20Sopenharmony_ci *                           |                   |         +
268c2ecf20Sopenharmony_ci *                           |               MIC1+-----<--+o+
278c2ecf20Sopenharmony_ci *                           |                   |         +
288c2ecf20Sopenharmony_ci *                           +-------------------+        ---
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/module.h>
328c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
338c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <asm/mach-types.h>
368c2ecf20Sopenharmony_ci#include <mach/audio.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include <sound/core.h>
398c2ecf20Sopenharmony_ci#include <sound/pcm.h>
408c2ecf20Sopenharmony_ci#include <sound/soc.h>
418c2ecf20Sopenharmony_ci#include <sound/initval.h>
428c2ecf20Sopenharmony_ci#include <sound/ac97_codec.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include "../codecs/wm9713.h"
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define AC97_GPIO_PULL		0x58
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* Use GPIO8 for rear speaker amplifier */
498c2ecf20Sopenharmony_cistatic int rear_amp_power(struct snd_soc_component *component, int power)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	unsigned short reg;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (power) {
548c2ecf20Sopenharmony_ci		reg = snd_soc_component_read(component, AC97_GPIO_CFG);
558c2ecf20Sopenharmony_ci		snd_soc_component_write(component, AC97_GPIO_CFG, reg | 0x0100);
568c2ecf20Sopenharmony_ci		reg = snd_soc_component_read(component, AC97_GPIO_PULL);
578c2ecf20Sopenharmony_ci		snd_soc_component_write(component, AC97_GPIO_PULL, reg | (1<<15));
588c2ecf20Sopenharmony_ci	} else {
598c2ecf20Sopenharmony_ci		reg = snd_soc_component_read(component, AC97_GPIO_CFG);
608c2ecf20Sopenharmony_ci		snd_soc_component_write(component, AC97_GPIO_CFG, reg & ~0x0100);
618c2ecf20Sopenharmony_ci		reg = snd_soc_component_read(component, AC97_GPIO_PULL);
628c2ecf20Sopenharmony_ci		snd_soc_component_write(component, AC97_GPIO_PULL, reg & ~(1<<15));
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return 0;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int rear_amp_event(struct snd_soc_dapm_widget *widget,
698c2ecf20Sopenharmony_ci			  struct snd_kcontrol *kctl, int event)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	struct snd_soc_card *card = widget->dapm->card;
728c2ecf20Sopenharmony_ci	struct snd_soc_pcm_runtime *rtd;
738c2ecf20Sopenharmony_ci	struct snd_soc_component *component;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
768c2ecf20Sopenharmony_ci	component = asoc_rtd_to_codec(rtd, 0)->component;
778c2ecf20Sopenharmony_ci	return rear_amp_power(component, SND_SOC_DAPM_EVENT_ON(event));
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* mioa701 machine dapm widgets */
818c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget mioa701_dapm_widgets[] = {
828c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SPK("Front Speaker", NULL),
838c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SPK("Rear Speaker", rear_amp_event),
848c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIC("Headset", NULL),
858c2ecf20Sopenharmony_ci	SND_SOC_DAPM_LINE("GSM Line Out", NULL),
868c2ecf20Sopenharmony_ci	SND_SOC_DAPM_LINE("GSM Line In", NULL),
878c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIC("Headset Mic", NULL),
888c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIC("Front Mic", NULL),
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route audio_map[] = {
928c2ecf20Sopenharmony_ci	/* Call Mic */
938c2ecf20Sopenharmony_ci	{"Mic Bias", NULL, "Front Mic"},
948c2ecf20Sopenharmony_ci	{"MIC1", NULL, "Mic Bias"},
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* Headset Mic */
978c2ecf20Sopenharmony_ci	{"LINEL", NULL, "Headset Mic"},
988c2ecf20Sopenharmony_ci	{"LINER", NULL, "Headset Mic"},
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/* GSM Module */
1018c2ecf20Sopenharmony_ci	{"MONOIN", NULL, "GSM Line Out"},
1028c2ecf20Sopenharmony_ci	{"PCBEEP", NULL, "GSM Line Out"},
1038c2ecf20Sopenharmony_ci	{"GSM Line In", NULL, "MONO"},
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* headphone connected to HPL, HPR */
1068c2ecf20Sopenharmony_ci	{"Headset", NULL, "HPL"},
1078c2ecf20Sopenharmony_ci	{"Headset", NULL, "HPR"},
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* front speaker connected to HPL, OUT3 */
1108c2ecf20Sopenharmony_ci	{"Front Speaker", NULL, "HPL"},
1118c2ecf20Sopenharmony_ci	{"Front Speaker", NULL, "OUT3"},
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* rear speaker connected to SPKL, SPKR */
1148c2ecf20Sopenharmony_ci	{"Rear Speaker", NULL, "SPKL"},
1158c2ecf20Sopenharmony_ci	{"Rear Speaker", NULL, "SPKR"},
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic int mioa701_wm9713_init(struct snd_soc_pcm_runtime *rtd)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* Prepare GPIO8 for rear speaker amplifier */
1238c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, AC97_GPIO_CFG, 0x100, 0x100);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/* Prepare MIC input */
1268c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, AC97_3D_CONTROL, 0xc000, 0xc000);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	return 0;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic struct snd_soc_ops mioa701_ops;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciSND_SOC_DAILINK_DEFS(ac97,
1348c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-ac97")),
1358c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CODEC("wm9713-codec", "wm9713-hifi")),
1368c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ciSND_SOC_DAILINK_DEFS(ac97_aux,
1398c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-ac97-aux")),
1408c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CODEC("wm9713-codec", "wm9713-aux")),
1418c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic struct snd_soc_dai_link mioa701_dai[] = {
1448c2ecf20Sopenharmony_ci	{
1458c2ecf20Sopenharmony_ci		.name = "AC97",
1468c2ecf20Sopenharmony_ci		.stream_name = "AC97 HiFi",
1478c2ecf20Sopenharmony_ci		.init = mioa701_wm9713_init,
1488c2ecf20Sopenharmony_ci		.ops = &mioa701_ops,
1498c2ecf20Sopenharmony_ci		SND_SOC_DAILINK_REG(ac97),
1508c2ecf20Sopenharmony_ci	},
1518c2ecf20Sopenharmony_ci	{
1528c2ecf20Sopenharmony_ci		.name = "AC97 Aux",
1538c2ecf20Sopenharmony_ci		.stream_name = "AC97 Aux",
1548c2ecf20Sopenharmony_ci		.ops = &mioa701_ops,
1558c2ecf20Sopenharmony_ci		SND_SOC_DAILINK_REG(ac97_aux),
1568c2ecf20Sopenharmony_ci	},
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic struct snd_soc_card mioa701 = {
1608c2ecf20Sopenharmony_ci	.name = "MioA701",
1618c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
1628c2ecf20Sopenharmony_ci	.dai_link = mioa701_dai,
1638c2ecf20Sopenharmony_ci	.num_links = ARRAY_SIZE(mioa701_dai),
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	.dapm_widgets = mioa701_dapm_widgets,
1668c2ecf20Sopenharmony_ci	.num_dapm_widgets = ARRAY_SIZE(mioa701_dapm_widgets),
1678c2ecf20Sopenharmony_ci	.dapm_routes = audio_map,
1688c2ecf20Sopenharmony_ci	.num_dapm_routes = ARRAY_SIZE(audio_map),
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic int mioa701_wm9713_probe(struct platform_device *pdev)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	int rc;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	if (!machine_is_mioa701())
1768c2ecf20Sopenharmony_ci		return -ENODEV;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	mioa701.dev = &pdev->dev;
1798c2ecf20Sopenharmony_ci	rc = devm_snd_soc_register_card(&pdev->dev, &mioa701);
1808c2ecf20Sopenharmony_ci	if (!rc)
1818c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "Be warned that incorrect mixers/muxes setup will "
1828c2ecf20Sopenharmony_ci			 "lead to overheating and possible destruction of your device."
1838c2ecf20Sopenharmony_ci			 " Do not use without a good knowledge of mio's board design!\n");
1848c2ecf20Sopenharmony_ci	return rc;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic struct platform_driver mioa701_wm9713_driver = {
1888c2ecf20Sopenharmony_ci	.probe		= mioa701_wm9713_probe,
1898c2ecf20Sopenharmony_ci	.driver		= {
1908c2ecf20Sopenharmony_ci		.name		= "mioa701-wm9713",
1918c2ecf20Sopenharmony_ci		.pm     = &snd_soc_pm_ops,
1928c2ecf20Sopenharmony_ci	},
1938c2ecf20Sopenharmony_ci};
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cimodule_platform_driver(mioa701_wm9713_driver);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/* Module information */
1988c2ecf20Sopenharmony_ciMODULE_AUTHOR("Robert Jarzmik (rjarzmik@free.fr)");
1998c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ALSA SoC WM9713 MIO A701");
2008c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2018c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:mioa701-wm9713");
202