summaryrefslogtreecommitdiffstats
path: root/sys/dev/sound/pci/ak452x.c
blob: feb550e9a36fa857ae2e8d0b2a6bc802923a1d55 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (c) 2001 Katsurajima Naoto <raven@katsurajima.seya.yokohama.jp>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <dev/sound/pcm/sound.h>

#include <dev/sound/pci/ak452x.h>

MALLOC_DEFINE(M_AK452X, "ak452x", "ak452x codec");

#define AK452X_NAMELEN	16
struct ak452x_info {
	device_t dev;
	ak452x_ctrl ctrl;
	void *devinfo;
	int num; /* number of this device */
	unsigned int type;   /* codec type */
	unsigned int cif;    /* Controll data Interface Format (0/1) */
	unsigned int format; /* data format and master clock frequency */
	unsigned int dvc;    /* De-emphasis and Volume Control */
	unsigned int left, right;
	char name[AK452X_NAMELEN];
	void *lock;
};

static void
ak452x_wrbit(struct ak452x_info *codec, int bit)
{
	unsigned int cs, cdti;
	if (codec->cif)
		cs = 1;
	else
		cs = 0;
	if (bit)
		cdti = 1;
	else
		cdti = 0;
	codec->ctrl(codec->devinfo, cs, 0, cdti);
	DELAY(1);
	codec->ctrl(codec->devinfo, cs, 1, cdti);
	DELAY(1);

	return;
}

static void
ak452x_wrcd(struct ak452x_info *codec, int reg, u_int8_t val)
{
	int mask;

#if(0)
	device_printf(codec->dev, "ak452x_wrcd(codec, 0x%02x, 0x%02x)\n", reg, val);
#endif
	/* start */
	if (codec->cif)
		codec->ctrl(codec->devinfo, 1, 1, 0);
	else
		codec->ctrl(codec->devinfo, 0, 1, 0);
	DELAY(1);
	/* chip address */
	ak452x_wrbit(codec, 1);
	ak452x_wrbit(codec, 0);
	/* write */
	ak452x_wrbit(codec, 1);
	/* register address */
	for (mask = 0x10; mask != 0; mask >>= 1)
		ak452x_wrbit(codec, reg & mask);
	/* data */
	for (mask = 0x80; mask != 0; mask >>= 1)
		ak452x_wrbit(codec, val & mask);
	/* stop */
	DELAY(1);
	if (codec->cif) {
		codec->ctrl(codec->devinfo, 0, 1, 0);
		DELAY(1);
		codec->ctrl(codec->devinfo, 1, 1, 0);
	}
	else {
		codec->ctrl(codec->devinfo, 1, 1, 0);
	}

	return;
}

struct ak452x_info *
ak452x_create(device_t dev, void *devinfo, int num, ak452x_ctrl ctrl)
{
	struct ak452x_info *codec;

#if(0)
	device_printf(dev, "ak452x_create(dev, devinfo, %d, ctrl)\n", num);
#endif
	codec = (struct ak452x_info *)malloc(sizeof *codec, M_AK452X, M_NOWAIT);
	if (codec == NULL)
		return NULL;

	snprintf(codec->name, AK452X_NAMELEN, "%s:ak452x%d", device_get_nameunit(dev), num);
	codec->lock = snd_mtxcreate(codec->name, codec->name);
	codec->dev = dev;
	codec->ctrl = ctrl;
	codec->devinfo = devinfo;
	codec->num = num;
	codec->type = AK452X_TYPE_4524;
	codec->cif = 0;
	codec->format = AK452X_FORMAT_I2S | AK452X_FORMAT_256FSN | AK452X_FORMAT_1X;
	codec->dvc = AK452X_DVC_DEMOFF | AK452X_DVC_ZTM1024 | AK452X_DVC_ZCE;

	return codec;
}

void
ak452x_destroy(struct ak452x_info *codec)
{
	snd_mtxfree(codec->lock);
	free(codec, M_AK452X);
}

void
ak452x_settype(struct ak452x_info *codec, unsigned int type)
{
	snd_mtxlock(codec->lock);
	codec->type = type;
	snd_mtxunlock(codec->lock);
}

void
ak452x_setcif(struct ak452x_info *codec, unsigned int cif)
{
	snd_mtxlock(codec->lock);
	codec->cif = cif;
	snd_mtxunlock(codec->lock);
}

void
ak452x_setformat(struct ak452x_info *codec, unsigned int format)
{
	snd_mtxlock(codec->lock);
	codec->format = format;
	snd_mtxunlock(codec->lock);
}

void
ak452x_setdvc(struct ak452x_info *codec, unsigned int dvc)
{
	snd_mtxlock(codec->lock);
	codec->dvc = dvc;
	snd_mtxunlock(codec->lock);
}

void
ak452x_init(struct ak452x_info *codec)
{
#if(0)
	device_printf(codec->dev, "ak452x_init(codec)\n");
#endif
	snd_mtxlock(codec->lock);
	/* power off */
	ak452x_wrcd(codec, AK4524_POWER, 0);
	/* set parameter */
	ak452x_wrcd(codec, AK4524_FORMAT, codec->format);
	ak452x_wrcd(codec, AK4524_DVC, codec->dvc);
	/* power on */
	ak452x_wrcd(codec, AK4524_POWER, AK452X_POWER_PWDA | AK452X_POWER_PWAD | AK452X_POWER_PWVR);
	/* free reset register */
	ak452x_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
	snd_mtxunlock(codec->lock);
}

void
ak452x_reinit(struct ak452x_info *codec)
{
	snd_mtxlock(codec->lock);
	/* reset */
	ak452x_wrcd(codec, AK4524_RESET, 0);
	/* set parameter */
	ak452x_wrcd(codec, AK4524_FORMAT, codec->format);
	ak452x_wrcd(codec, AK4524_DVC, codec->dvc);
	/* free reset register */
	ak452x_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
	snd_mtxunlock(codec->lock);
}

void
ak452x_set(struct ak452x_info *codec, int dir, unsigned int left, unsigned int right)
{
#if(0)
	device_printf(codec->dev, "ak452x_set(codec, %d, %d, %d)\n", dir, left, right);
#endif
	snd_mtxlock(codec->lock);
	if (left >= 100)
		left  = 127;
	else
		left = left * 127 / 100;
	if (right >= 100)
		right  = 127;
	else
		right = right * 127 / 100;
	if (dir == PCMDIR_REC && codec->type == AK452X_TYPE_4524) {
#if(0)
		device_printf(codec->dev, "ak452x_set(): AK4524(REC) %d/%d\n", left, right);
#endif
		ak452x_wrcd(codec, AK4524_LIPGA, left);
		ak452x_wrcd(codec, AK4524_RIPGA, right);
	}
	if (dir == PCMDIR_PLAY && codec->type == AK452X_TYPE_4524) {
#if(0)
		device_printf(codec->dev, "ak452x_set(): AK4524(PLAY) %d/%d\n", left, right);
#endif
		ak452x_wrcd(codec, AK4524_LOATT, left);
		ak452x_wrcd(codec, AK4524_ROATT, right);
	}
	if (dir == PCMDIR_PLAY && codec->type == AK452X_TYPE_4528) {
#if(0)
		device_printf(codec->dev, "ak452x_set(): AK4528(PLAY) %d/%d\n", left, right);
#endif
		ak452x_wrcd(codec, AK4528_LOATT, left);
		ak452x_wrcd(codec, AK4528_ROATT, right);
	}
	snd_mtxunlock(codec->lock);
}

MODULE_DEPEND(snd_ak452x, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
MODULE_VERSION(snd_ak452x, 1);
OpenPOWER on IntegriCloud