summaryrefslogtreecommitdiffstats
path: root/drivers/media/usb/stk1160/stk1160-ac97.c
blob: 2dd308f9541f469b719adf7d53f01901f4525b3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * STK1160 driver
 *
 * Copyright (C) 2012 Ezequiel Garcia
 * <elezegarcia--a.t--gmail.com>
 *
 * Based on Easycap driver by R.M. Thomas
 *	Copyright (C) 2010 R.M. Thomas
 *	<rmthomas--a.t--sciolus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/module.h>
#include <sound/core.h>
#include <sound/initval.h>
#include <sound/ac97_codec.h>

#include "stk1160.h"
#include "stk1160-reg.h"

static struct snd_ac97 *stk1160_ac97;

static void stk1160_write_ac97(struct snd_ac97 *ac97, u16 reg, u16 value)
{
	struct stk1160 *dev = ac97->private_data;

	/* Set codec register address */
	stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);

	/* Set codec command */
	stk1160_write_reg(dev, STK1160_AC97_CMD, value & 0xff);
	stk1160_write_reg(dev, STK1160_AC97_CMD + 1, (value & 0xff00) >> 8);

	/*
	 * Set command write bit to initiate write operation.
	 * The bit will be cleared when transfer is done.
	 */
	stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8c);
}

static u16 stk1160_read_ac97(struct snd_ac97 *ac97, u16 reg)
{
	struct stk1160 *dev = ac97->private_data;
	u8 vall = 0;
	u8 valh = 0;

	/* Set codec register address */
	stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);

	/*
	 * Set command read bit to initiate read operation.
	 * The bit will be cleared when transfer is done.
	 */
	stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8b);

	/* Retrieve register value */
	stk1160_read_reg(dev, STK1160_AC97_CMD, &vall);
	stk1160_read_reg(dev, STK1160_AC97_CMD + 1, &valh);

	return (valh << 8) | vall;
}

static void stk1160_reset_ac97(struct snd_ac97 *ac97)
{
	struct stk1160 *dev = ac97->private_data;
	/* Two-step reset AC97 interface and hardware codec */
	stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x94);
	stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x88);

	/* Set 16-bit audio data and choose L&R channel*/
	stk1160_write_reg(dev, STK1160_AC97CTL_1 + 2, 0x01);
}

static struct snd_ac97_bus_ops stk1160_ac97_ops = {
	.read	= stk1160_read_ac97,
	.write	= stk1160_write_ac97,
	.reset	= stk1160_reset_ac97,
};

int stk1160_ac97_register(struct stk1160 *dev)
{
	struct snd_card *card = NULL;
	struct snd_ac97_bus *ac97_bus;
	struct snd_ac97_template ac97_template;
	int rc;

	/*
	 * Just want a card to access ac96 controls,
	 * the actual capture interface will be handled by snd-usb-audio
	 */
	rc = snd_card_new(dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
			  THIS_MODULE, 0, &card);
	if (rc < 0)
		return rc;

	/* TODO: I'm not sure where should I get these names :-( */
	snprintf(card->shortname, sizeof(card->shortname),
		 "stk1160-mixer");
	snprintf(card->longname, sizeof(card->longname),
		 "stk1160 ac97 codec mixer control");
	strlcpy(card->driver, dev->dev->driver->name, sizeof(card->driver));

	rc = snd_ac97_bus(card, 0, &stk1160_ac97_ops, NULL, &ac97_bus);
	if (rc)
		goto err;

	/* We must set private_data before calling snd_ac97_mixer */
	memset(&ac97_template, 0, sizeof(ac97_template));
	ac97_template.private_data = dev;
	ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
	rc = snd_ac97_mixer(ac97_bus, &ac97_template, &stk1160_ac97);
	if (rc)
		goto err;

	dev->snd_card = card;
	rc = snd_card_register(card);
	if (rc)
		goto err;

	return 0;

err:
	dev->snd_card = NULL;
	snd_card_free(card);
	return rc;
}

int stk1160_ac97_unregister(struct stk1160 *dev)
{
	struct snd_card *card = dev->snd_card;

	/*
	 * We need to check usb_device,
	 * because ac97 release attempts to communicate with codec
	 */
	if (card && dev->udev)
		snd_card_free(card);

	return 0;
}
OpenPOWER on IntegriCloud