xref: /openbmc/linux/sound/soc/codecs/ac97.c (revision 6627a653)
1dbc6b6adSRichard Purdie /*
2dbc6b6adSRichard Purdie  * ac97.c  --  ALSA Soc AC97 codec support
3dbc6b6adSRichard Purdie  *
4dbc6b6adSRichard Purdie  * Copyright 2005 Wolfson Microelectronics PLC.
5d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
6dbc6b6adSRichard Purdie  *
7dbc6b6adSRichard Purdie  *  This program is free software; you can redistribute  it and/or modify it
8dbc6b6adSRichard Purdie  *  under  the terms of  the GNU General  Public License as published by the
9dbc6b6adSRichard Purdie  *  Free Software Foundation;  either version 2 of the  License, or (at your
10dbc6b6adSRichard Purdie  *  option) any later version.
11dbc6b6adSRichard Purdie  *
12dbc6b6adSRichard Purdie  * Generic AC97 support.
13dbc6b6adSRichard Purdie  */
14dbc6b6adSRichard Purdie 
15dbc6b6adSRichard Purdie #include <linux/init.h>
16dbc6b6adSRichard Purdie #include <linux/kernel.h>
17dbc6b6adSRichard Purdie #include <linux/device.h>
18dbc6b6adSRichard Purdie #include <sound/core.h>
19dbc6b6adSRichard Purdie #include <sound/pcm.h>
20dbc6b6adSRichard Purdie #include <sound/ac97_codec.h>
21dbc6b6adSRichard Purdie #include <sound/initval.h>
22dbc6b6adSRichard Purdie #include <sound/soc.h>
23dbac7cb8SMark Brown #include "ac97.h"
24dbc6b6adSRichard Purdie 
255a8ec343SLiam Girdwood #define AC97_VERSION "0.6"
26dbc6b6adSRichard Purdie 
27dee89c4dSMark Brown static int ac97_prepare(struct snd_pcm_substream *substream,
28dee89c4dSMark Brown 			struct snd_soc_dai *dai)
29dbc6b6adSRichard Purdie {
30dbc6b6adSRichard Purdie 	struct snd_pcm_runtime *runtime = substream->runtime;
31dbc6b6adSRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
32dbc6b6adSRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
336627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
34dbc6b6adSRichard Purdie 
35dbc6b6adSRichard Purdie 	int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
36dbc6b6adSRichard Purdie 		  AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
37dbc6b6adSRichard Purdie 	return snd_ac97_set_rate(codec->ac97, reg, runtime->rate);
38dbc6b6adSRichard Purdie }
39dbc6b6adSRichard Purdie 
405a8ec343SLiam Girdwood #define STD_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
4124c053e7SMark Brown 		SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 |\
4224c053e7SMark Brown 		SNDRV_PCM_RATE_48000)
435a8ec343SLiam Girdwood 
44e550e17fSLiam Girdwood struct snd_soc_dai ac97_dai = {
45dbc6b6adSRichard Purdie 	.name = "AC97 HiFi",
463ba9e10aSMark Brown 	.ac97_control = 1,
47dbc6b6adSRichard Purdie 	.playback = {
48dbc6b6adSRichard Purdie 		.stream_name = "AC97 Playback",
49dbc6b6adSRichard Purdie 		.channels_min = 1,
505a8ec343SLiam Girdwood 		.channels_max = 2,
515a8ec343SLiam Girdwood 		.rates = STD_AC97_RATES,
525a8ec343SLiam Girdwood 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
53dbc6b6adSRichard Purdie 	.capture = {
54dbc6b6adSRichard Purdie 		.stream_name = "AC97 Capture",
55dbc6b6adSRichard Purdie 		.channels_min = 1,
565a8ec343SLiam Girdwood 		.channels_max = 2,
575a8ec343SLiam Girdwood 		.rates = STD_AC97_RATES,
585a8ec343SLiam Girdwood 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
59dbc6b6adSRichard Purdie 	.ops = {
60dbc6b6adSRichard Purdie 		.prepare = ac97_prepare,},
61dbc6b6adSRichard Purdie };
625d0cedeeSGraeme Gregory EXPORT_SYMBOL_GPL(ac97_dai);
63dbc6b6adSRichard Purdie 
64dbc6b6adSRichard Purdie static unsigned int ac97_read(struct snd_soc_codec *codec,
65dbc6b6adSRichard Purdie 	unsigned int reg)
66dbc6b6adSRichard Purdie {
67dbc6b6adSRichard Purdie 	return soc_ac97_ops.read(codec->ac97, reg);
68dbc6b6adSRichard Purdie }
69dbc6b6adSRichard Purdie 
70dbc6b6adSRichard Purdie static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
71dbc6b6adSRichard Purdie 	unsigned int val)
72dbc6b6adSRichard Purdie {
73dbc6b6adSRichard Purdie 	soc_ac97_ops.write(codec->ac97, reg, val);
74dbc6b6adSRichard Purdie 	return 0;
75dbc6b6adSRichard Purdie }
76dbc6b6adSRichard Purdie 
77dbc6b6adSRichard Purdie static int ac97_soc_probe(struct platform_device *pdev)
78dbc6b6adSRichard Purdie {
79dbc6b6adSRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
80dbc6b6adSRichard Purdie 	struct snd_soc_codec *codec;
81dbc6b6adSRichard Purdie 	struct snd_ac97_bus *ac97_bus;
82dbc6b6adSRichard Purdie 	struct snd_ac97_template ac97_template;
83dbc6b6adSRichard Purdie 	int ret = 0;
84dbc6b6adSRichard Purdie 
85dbc6b6adSRichard Purdie 	printk(KERN_INFO "AC97 SoC Audio Codec %s\n", AC97_VERSION);
86dbc6b6adSRichard Purdie 
876627a653SMark Brown 	socdev->card->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
886627a653SMark Brown 	if (!socdev->card->codec)
89dbc6b6adSRichard Purdie 		return -ENOMEM;
906627a653SMark Brown 	codec = socdev->card->codec;
91dbc6b6adSRichard Purdie 	mutex_init(&codec->mutex);
92dbc6b6adSRichard Purdie 
93dbc6b6adSRichard Purdie 	codec->name = "AC97";
94dbc6b6adSRichard Purdie 	codec->owner = THIS_MODULE;
95dbc6b6adSRichard Purdie 	codec->dai = &ac97_dai;
96dbc6b6adSRichard Purdie 	codec->num_dai = 1;
97dbc6b6adSRichard Purdie 	codec->write = ac97_write;
98dbc6b6adSRichard Purdie 	codec->read = ac97_read;
99dbc6b6adSRichard Purdie 	INIT_LIST_HEAD(&codec->dapm_widgets);
100dbc6b6adSRichard Purdie 	INIT_LIST_HEAD(&codec->dapm_paths);
101dbc6b6adSRichard Purdie 
102dbc6b6adSRichard Purdie 	/* register pcms */
103dbc6b6adSRichard Purdie 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
104dbc6b6adSRichard Purdie 	if (ret < 0)
105dbc6b6adSRichard Purdie 		goto err;
106dbc6b6adSRichard Purdie 
107dbc6b6adSRichard Purdie 	/* add codec as bus device for standard ac97 */
108dbc6b6adSRichard Purdie 	ret = snd_ac97_bus(codec->card, 0, &soc_ac97_ops, NULL, &ac97_bus);
109dbc6b6adSRichard Purdie 	if (ret < 0)
110dbc6b6adSRichard Purdie 		goto bus_err;
111dbc6b6adSRichard Purdie 
112dbc6b6adSRichard Purdie 	memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
113dbc6b6adSRichard Purdie 	ret = snd_ac97_mixer(ac97_bus, &ac97_template, &codec->ac97);
114dbc6b6adSRichard Purdie 	if (ret < 0)
115dbc6b6adSRichard Purdie 		goto bus_err;
116dbc6b6adSRichard Purdie 
117968a6025SMark Brown 	ret = snd_soc_init_card(socdev);
118dbc6b6adSRichard Purdie 	if (ret < 0)
119dbc6b6adSRichard Purdie 		goto bus_err;
120dbc6b6adSRichard Purdie 	return 0;
121dbc6b6adSRichard Purdie 
122dbc6b6adSRichard Purdie bus_err:
123dbc6b6adSRichard Purdie 	snd_soc_free_pcms(socdev);
124dbc6b6adSRichard Purdie 
125dbc6b6adSRichard Purdie err:
1266627a653SMark Brown 	kfree(socdev->card->codec);
1276627a653SMark Brown 	socdev->card->codec = NULL;
128dbc6b6adSRichard Purdie 	return ret;
129dbc6b6adSRichard Purdie }
130dbc6b6adSRichard Purdie 
131dbc6b6adSRichard Purdie static int ac97_soc_remove(struct platform_device *pdev)
132dbc6b6adSRichard Purdie {
133dbc6b6adSRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1346627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
135dbc6b6adSRichard Purdie 
13624c053e7SMark Brown 	if (!codec)
137dbc6b6adSRichard Purdie 		return 0;
138dbc6b6adSRichard Purdie 
139dbc6b6adSRichard Purdie 	snd_soc_free_pcms(socdev);
1406627a653SMark Brown 	kfree(socdev->card->codec);
141dbc6b6adSRichard Purdie 
142dbc6b6adSRichard Purdie 	return 0;
143dbc6b6adSRichard Purdie }
144dbc6b6adSRichard Purdie 
1453f775987SManuel Lauss #ifdef CONFIG_PM
1463f775987SManuel Lauss static int ac97_soc_suspend(struct platform_device *pdev, pm_message_t msg)
1473f775987SManuel Lauss {
1483f775987SManuel Lauss 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1493f775987SManuel Lauss 
1506627a653SMark Brown 	snd_ac97_suspend(socdev->card->codec->ac97);
1513f775987SManuel Lauss 
1523f775987SManuel Lauss 	return 0;
1533f775987SManuel Lauss }
1543f775987SManuel Lauss 
1553f775987SManuel Lauss static int ac97_soc_resume(struct platform_device *pdev)
1563f775987SManuel Lauss {
1573f775987SManuel Lauss 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1583f775987SManuel Lauss 
1596627a653SMark Brown 	snd_ac97_resume(socdev->card->codec->ac97);
1603f775987SManuel Lauss 
1613f775987SManuel Lauss 	return 0;
1623f775987SManuel Lauss }
1633f775987SManuel Lauss #else
1643f775987SManuel Lauss #define ac97_soc_suspend NULL
1653f775987SManuel Lauss #define ac97_soc_resume NULL
1663f775987SManuel Lauss #endif
1673f775987SManuel Lauss 
168dbc6b6adSRichard Purdie struct snd_soc_codec_device soc_codec_dev_ac97 = {
169dbc6b6adSRichard Purdie 	.probe = 	ac97_soc_probe,
170dbc6b6adSRichard Purdie 	.remove = 	ac97_soc_remove,
1713f775987SManuel Lauss 	.suspend =	ac97_soc_suspend,
1723f775987SManuel Lauss 	.resume =	ac97_soc_resume,
173dbc6b6adSRichard Purdie };
174dbc6b6adSRichard Purdie EXPORT_SYMBOL_GPL(soc_codec_dev_ac97);
175dbc6b6adSRichard Purdie 
176dbc6b6adSRichard Purdie MODULE_DESCRIPTION("Soc Generic AC97 driver");
177dbc6b6adSRichard Purdie MODULE_AUTHOR("Liam Girdwood");
178dbc6b6adSRichard Purdie MODULE_LICENSE("GPL");
179