summaryrefslogtreecommitdiff
path: root/kernel/drv/oss_envy24ht/envy24ht_via.c.save
blob: 49d41998c0ffc2d4ed3e29ad94c4f95032c7c1c4 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Purpose: Low level routines for VIA's Envy24HT reference board (AKM codec)
 *
 * Notice:
 *
 * This driver is currently disabled because no known real world device
 * is based on this design. To enable this driver againa just rename it to
 * envy24ht_via.c and edit the models[] table in envy24ht.c.
 */
/*
 *
 * This file is part of Open Sound System.
 *
 * Copyright (C) 4Front Technologies 1996-2008.
 *
 * This this source file is released under GPL v2 license (no other versions).
 * See the COPYING file included in the main directory of this source
 * distribution for the license terms and conditions.
 *
 */

#include "oss_envy24ht_cfg.h"

#include "spdif.h"
#include "envy24ht.h"

#define AKM_ADDRESS 0x10

static unsigned char
i2c_read (envy24ht_devc * devc, unsigned char addr, unsigned char pos)
{
  int i;
  unsigned char data;
  oss_native_word flags;

  MUTEX_ENTER_IRQDISABLE (devc->low_mutex, flags);
  OUTB (devc->osdev, pos, devc->ccs_base + 0x11);	/* Offset */
  OUTB (devc->osdev, addr << 1, devc->ccs_base + 0x10);	/* Read address  */

  for (i = 0; i < 2000; i++)
    {
      unsigned char status = INB (devc->osdev, devc->ccs_base + 0x13);
      if (!(status & 1))
	break;

    }

  oss_udelay (1);
  data = INB (devc->osdev, devc->ccs_base + 0x12);
  MUTEX_EXIT_IRQRESTORE (devc->low_mutex, flags);

  return data;
}

static void
i2c_write (envy24ht_devc * devc, unsigned char addr, unsigned char pos,
	   unsigned char data)
{
  int i;
  oss_native_word flags;

  MUTEX_ENTER_IRQDISABLE (devc->low_mutex, flags);
  OUTB (devc->osdev, pos, devc->ccs_base + 0x11);	/* Offset */
  OUTB (devc->osdev, data, devc->ccs_base + 0x12);	/* Data */
  OUTB (devc->osdev, (addr << 1) | 1, devc->ccs_base + 0x10);	/* Write address  */

  for (i = 0; i < 2000; i++)
    {
      unsigned char status = INB (devc->osdev, devc->ccs_base + 0x13);
      if (!(status & 1))
	break;

    }
  MUTEX_EXIT_IRQRESTORE (devc->low_mutex, flags);
}

static void
viaref_card_init (envy24ht_devc * devc)
{
  int addr = AKM_ADDRESS;

  if (devc->codec_type != CODEC_I2S)
    return;

  OUTW (devc->osdev, 0x000f, devc->ccs_base + 0x14);	/* GPIO */

  oss_udelay (1000);

  i2c_write (devc, addr, 0, 0x00);
  i2c_write (devc, addr, 0, 0x0f);
  i2c_write (devc, addr, 1, 0x00);
  i2c_write (devc, addr, 2, 0x01);
  i2c_write (devc, addr, 5, 0x07);
  i2c_write (devc, addr, 6, 0x00);
  i2c_write (devc, addr, 7, 0x00);

  i2c_write (devc, addr, 6, 0x00);
  i2c_write (devc, addr, 7, 0x00);
  i2c_write (devc, addr, 8, 0x00);
  i2c_write (devc, addr, 9, 0x00);
  i2c_write (devc, addr, 10, 0x00);
  i2c_write (devc, addr, 11, 0x00);

  devc->recsrc = 0;

#if 0
  cmn_err(CE_CONT, "Regs=");
  for (i = 0; i < 0x18; i++)
    {
      cmn_err(CE_CONT, "%02x ", i2c_read (devc, addr, i));
    }
  cmn_err(CE_CONT, "\n");
#endif
}

static int
viaref_set_akm (int dev, int ctrl, unsigned int cmd, int value)
{
  envy24ht_devc *devc = mixer_devs[dev]->hw_devc;
  int left, right, val;
  unsigned char tmp, tmp2, old, old2;

  if (cmd == SNDCTL_MIX_READ)
    {
      switch (ctrl)
	{
	  /* Output gain controls */
	case 0:
	case 1:
	case 2:
	case 3:
	  return devc->gains[ctrl];

	case 10:		/* Loopback enable */
	  return !!(i2c_read (devc, AKM_ADDRESS, 0x01) & 0x30);

	case 11:		/* codec.recsrc (ANALOG OPTICAL COAX CD AUX) */
	  return devc->recsrc;
	  break;

	default:
	  return OSS_EINVAL;
	}
    }

  if (cmd == SNDCTL_MIX_WRITE)
    {
      oss_native_word flags;
      left = value & 0xff;
      right = (value >> 8) & 0xff;
      val = left | (right << 8);

      switch (ctrl)
	{
	case 0:
	  left = 0xff - left;
	  right = 0xff - right;
	  i2c_write (devc, AKM_ADDRESS, 0x06, left);
	  i2c_write (devc, AKM_ADDRESS, 0x07, right);
	  return devc->gains[ctrl] = val;

	case 1:
	  left = 0xff - left;
	  right = 0xff - right;
	  i2c_write (devc, AKM_ADDRESS, 0x08, left);
	  i2c_write (devc, AKM_ADDRESS, 0x09, right);
	  return devc->gains[ctrl] = val;

	case 2:
	  val = left | (left << 8);
	  left = 0xff - left;
	  i2c_write (devc, AKM_ADDRESS, 0x0a, left);
	  return devc->gains[ctrl] = val;

	case 3:
	  val = left | (left << 8);
	  left = 0xff - left;
	  i2c_write (devc, AKM_ADDRESS, 0x0b, left);
	  return devc->gains[ctrl] = val;

	case 10:		/* Loopback enable */
	  MUTEX_ENTER_IRQDISABLE (devc->mutex, flags);
	  old = tmp = i2c_read (devc, AKM_ADDRESS, 0x01);
	  value = !!value;
	  if (value)
	    tmp |= 0x10;
	  else
	    tmp &= ~0x30;

	  if (tmp != old)
	    {
	      i2c_write (devc, AKM_ADDRESS, 0x01, tmp);
	    }
	  MUTEX_EXIT_IRQRESTORE (devc->mutex, flags);
	  return value;

	case 11:		/* codec.recsrc (ANALOG OPTICAL COAX CD AUX) */
	  if (value < 0 || value > 4)
	    return devc->recsrc;
	  MUTEX_ENTER_IRQDISABLE (devc->mutex, flags);
	  old = tmp = i2c_read (devc, AKM_ADDRESS, 0x01);
	  old2 = tmp2 = i2c_read (devc, AKM_ADDRESS, 0x02);

	  tmp &= ~0x0c;
	  tmp2 &= ~0x03;

	  switch (value)
	    {
	    case 0:		/* Analog */
	      tmp2 |= 0x01;
	      break;

	    case 1:		/* Optical (RX2) */
	      tmp |= 0x04;
	      tmp2 |= 0x00;
	      break;

	    case 2:		/* Coax (RX1) */
	      tmp |= 0x00;
	      tmp2 |= 0x00;
	      break;

	    case 3:		/* CD digital input (RX3) */
	      tmp |= 0x08;
	      tmp2 |= 0x00;
	      break;

	    case 4:		/* Unused digital input (RX4) */
	      tmp |= 0x08;
	      tmp2 |= 0x00;
	      break;
	    }

	  if (tmp != old)
	    i2c_write (devc, AKM_ADDRESS, 0x01, tmp);
	  if (tmp2 != old2)
	    i2c_write (devc, AKM_ADDRESS, 0x02, tmp2);

	  devc->recsrc = value;
	  MUTEX_EXIT_IRQRESTORE (devc->mutex, flags);
	  return devc->recsrc;
	  break;

	}
      return OSS_EIO;
    }

  return OSS_EIO;
}

static int
viaref_mixer_init (envy24ht_devc * devc, int dev, int group)
{
  int i, err;
  if (devc->codec_type != CODEC_I2S)
    return 0;

  for (i = 0; i < 4; i++)
    devc->gains[i] = 0xffff;

  if ((group = mixer_ext_create_group (dev, 0, "ENVY24HT_GAIN")) < 0)
    return group;

  if ((err = mixer_ext_create_control (dev, group,
				       0, viaref_set_akm,
				       MIXT_STEREOSLIDER,
				       "GAIN_FRONT", 255,
				       MIXF_READABLE | MIXF_WRITEABLE)) < 0)
    return err;

  if ((err = mixer_ext_create_control (dev, group,
				       1, viaref_set_akm,
				       MIXT_STEREOSLIDER,
				       "GAIN_REAR", 255,
				       MIXF_READABLE | MIXF_WRITEABLE)) < 0)
    return err;

  if ((err = mixer_ext_create_control (dev, group,
				       2, viaref_set_akm,
				       MIXT_MONOSLIDER,
				       "GAIN_CENTER", 255,
				       MIXF_READABLE | MIXF_WRITEABLE)) < 0)
    return err;

  if ((err = mixer_ext_create_control (dev, group,
				       3, viaref_set_akm,
				       MIXT_MONOSLIDER,
				       "GAIN_LFE", 255,
				       MIXF_READABLE | MIXF_WRITEABLE)) < 0)
    return err;

  if ((group = mixer_ext_create_group (dev, 0, "ENVY24HT_CODEC")) < 0)
    return group;

  if ((err = mixer_ext_create_control (dev, group,
				       11, viaref_set_akm,
				       MIXT_ENUM,
				       "CODEC_RECSRC", 5,
				       MIXF_READABLE | MIXF_WRITEABLE)) < 0)
    return err;

  if ((err = mixer_ext_create_control (dev, group,
				       10, viaref_set_akm,
				       MIXT_ONOFF,
				       "CODEC_LOOPBACK", 2,
				       MIXF_READABLE | MIXF_WRITEABLE)) < 0)
    return err;

  return 0;
}

#if 0
static int
viaref_private1 (envy24ht_devc * devc, int value)
{
  i2c_write (devc, AKM_ADDRESS, (value >> 8) & 0xff, value & 0xff);
  return 0;
}
#endif

envy24ht_auxdrv_t envy24ht_viaref_auxdrv = {
  viaref_card_init,
  viaref_mixer_init
};