summaryrefslogtreecommitdiff
path: root/kernel/drv/oss_envy24/envy24_ews88d.c
blob: c68be600d8d874c105b848463927ea98da603f01 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Purpose: Card specific routines for Terratec EWS88D.
 */
/*
 *
 * 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_config.h"
#include "ac97.h"
#include "envy24.h"

#if 0
# define PRT_STATUS(v) outb(v&0xff, 0x378)
#else
# define PRT_STATUS(v)
#endif

/* PCF8575 pin assignment  (default 0xffff) */
#define PCF_SPDIN_SELECT	0x0001	/* 0=optical 1=coax */
#define PCF_OPTOUT		0x0002	/* 0=SPDIF 1=ADAT */
#define PCF_CLOCKSRC		0x0004	/* 0=ADAT 1=SPDIF */
#define PCF_ADATEN		0x0008	/* 0=ADAT disabled 1=ADAT enabled */
#define PCF_ADATLOOP_		0x0010	/* 0=ADAT in->out loop, 1=no loop */
#define PCF_CS8414_ERF_		0x0020	/* Inverted CS8414 ERF */
#define PCF_SPDIF_ERR		0x0040	/* OR'ed from CS8414 E0..E2 */
#define PCF_ADAT_ERROR		0x0080	/* ADAT error input */

extern int envy24_gain_sliders;
extern int envy24_virtualout;
extern int envy24_zerolatency;	/* Testing in progress */
/*
 * Terratec stuff
 */

/*=============================================================================
  Function    : IIC_GetSDA
-------------------------------------------------------------------------------
  Description : 
  Returns     : unsigned char  -> 
  Parameters  : unsigned long dwPortAddr -> 
-------------------------------------------------------------------------------
  Notes       : 
=============================================================================*/
static __inline__ unsigned char
IIC_GetSDA (envy24_devc * devc, unsigned long dwPortAddr)
{

  unsigned char bReg, bSDA;

  /* FMB TEST: RW line */
  /* set write mask */
  bReg = ~(0x08);		/* writeable */
  envy24_write_cci (devc, 0x21, bReg);

  /* set RW line LOW */
  bReg = 0;			/* writeable */
  envy24_write_cci (devc, 0x20, bReg);

  /* Set direction: SDA to input and SCL to output */
  bReg = envy24_read_cci (devc, 0x22);
  bReg |= 0x20;			/* SCL output = 1 */
  bReg &= ~0x10;		/* SDA input  = 0 */
  envy24_write_cci (devc, 0x22, bReg);

  /* Get SDA line state */
  bSDA = envy24_read_cci (devc, 0x20);

  /* set RW line HIGH */
  bReg = 0x08;			/* writeable */
  envy24_write_cci (devc, 0x20, bReg);

  return 1;
  /* return ((bSDA & 0x10) == 0x10); */

}

/*=============================================================================
  Function    : IIC_SetIic
-------------------------------------------------------------------------------
  Description : 
  Returns     : void  -> 
  Parameters  : unsigned int dwPortAddr -> 
              : unsigned char fSDA -> 
              : unsigned char fSCL -> 
-------------------------------------------------------------------------------
  Notes       : 
=============================================================================*/
static __inline__ void
IIC_SetIic (envy24_devc * devc, unsigned int dwPortAddr, unsigned char fSDA,
	    unsigned char fSCL)
{
  unsigned char bReg;

  /* Set direction: SDA and SCL to output */
  bReg = envy24_read_cci (devc, 0x22);
  bReg |= (0x20 | 0x10);	/* 1 -> output */
  envy24_write_cci (devc, 0x22, bReg);

  /* set write mask */
  bReg = ~(0x20 | 0x10);	/* writeable */
  envy24_write_cci (devc, 0x21, bReg);

  /* Set line state */
  /* FMB TEST: RW line */
  bReg = 0x08;
/*	bReg = 0; */
  if (fSDA)
    bReg += 0x10;
  if (fSCL)
    bReg += 0x20;
  envy24_write_cci (devc, 0x20, bReg);

}

/*=============================================================================
  Function    : IIC_Start
-------------------------------------------------------------------------------
  Description : 
  Returns     : void  -> 
  Parameters  : unsigned int dwPortAddr -> 
-------------------------------------------------------------------------------
  Notes       : 
=============================================================================*/
static __inline__ void
IIC_Start (envy24_devc * devc, unsigned int dwPortAddr)
{
  /* falling edge of SDA while SCL is HIGH */
  IIC_SetIic (devc, dwPortAddr, 1, 1);
  IIC_SetIic (devc, dwPortAddr, 0, 1);
}

/*=============================================================================
  Function    : IIC_Stop  
-------------------------------------------------------------------------------
  Description : 
  Returns     : void -> 
  Parameters  : unsigned int dwPortAddr -> 
-------------------------------------------------------------------------------
  Notes       : 
=============================================================================*/
static __inline__ void
IIC_Stop (envy24_devc * devc, unsigned int dwPortAddr)
{
  /* rising edge of SDA while SCL is HIGH */
  IIC_SetIic (devc, dwPortAddr, 0, 1);
  IIC_SetIic (devc, dwPortAddr, 1, 1);
}


/*=============================================================================
  Function    : IIC_SendByte
-------------------------------------------------------------------------------
  Description : 
  Returns     : unsigned char -> 
  Parameters  : unsigned int dwPortAddr -> 
              : unsigned char bByte -> 
-------------------------------------------------------------------------------
  Notes       : 
=============================================================================*/
/*__inline  */
static unsigned char
IIC_SendByte (envy24_devc * devc, unsigned int dwPortAddr,
	      unsigned char bByte)
{
  unsigned char bDataBit, bAck;
  int i;

  for (i = 7; i >= 0; i--)	/* send byte (MSB first) */
    {
      bDataBit = (bByte >> i) & 0x01;

      IIC_SetIic (devc, dwPortAddr, bDataBit, 0);
      IIC_SetIic (devc, dwPortAddr, bDataBit, 1);
    }				/* end for i */

  IIC_SetIic (devc, dwPortAddr, 1, 0);

  /* Get acknowledge */
  IIC_SetIic (devc, dwPortAddr, 1, 1);
  bAck = IIC_GetSDA (devc, dwPortAddr);
  /* FMB this is a start condition but never mind */
  IIC_SetIic (devc, dwPortAddr, 0, 0);
  return 1;
  /* return (!bAck); *//* bAck = 0 --> success */
}

static unsigned char
IIC_WriteWord (envy24_devc * devc, int dwPortAddr, unsigned char bIicAddress,
	       unsigned short bByte)
{
  IIC_Start (devc, dwPortAddr);

  /* send IIC address and data byte */
  if (!IIC_SendByte (devc, dwPortAddr, bIicAddress))
    {
      cmn_err (CE_CONT, "IIC_SendByte 1 failed\n");
      goto FAILED;
    }
  if (!IIC_SendByte (devc, dwPortAddr, bByte & 0xff))
    {
      cmn_err (CE_CONT, "IIC_SendByte 3 failed\n");
      goto FAILED;
    }
  if (!IIC_SendByte (devc, dwPortAddr, (bByte >> 8) & 0xff))
    {
      cmn_err (CE_CONT, "IIC_SendByte 2 failed\n");
      goto FAILED;
    }

  IIC_Stop (devc, dwPortAddr);
  return 1;

FAILED:
  IIC_Stop (devc, dwPortAddr);
  return 0;
}

static void
ews88d_set_pcf8575 (envy24_devc * devc, unsigned short d)
{
  if (!IIC_WriteWord (devc, devc->ccs_base, 0x40, d))
    {
      cmn_err (CE_CONT, "IIC_WriteWord failed\n");
    }
  devc->gpio_tmp = d;
}

static void
ews88d_card_init (envy24_devc * devc)
{
  ews88d_set_pcf8575 (devc, 0xffff);
}

static void
set_ews88d_speed (envy24_devc * devc)
{
  int tmp;

  tmp = devc->speedbits;

  switch (devc->syncsource)
    {
    case SYNC_INTERNAL:
      OUTB (devc->osdev, tmp, devc->mt_base + 0x01);
      break;

    case SYNC_SPDIF:
      tmp |= 0x10;
      OUTB (devc->osdev, tmp, devc->mt_base + 0x01);
      devc->gpio_tmp &= ~PCF_CLOCKSRC;
      ews88d_set_pcf8575 (devc, devc->gpio_tmp);
      break;

    case SYNC_WCLOCK:
      tmp |= 0x10;
      OUTB (devc->osdev, tmp, devc->mt_base + 0x01);
      devc->gpio_tmp |= PCF_CLOCKSRC;
      ews88d_set_pcf8575 (devc, devc->gpio_tmp);
      break;
    }

  if (devc->speed > 48000)
    {
      devc->gpio_tmp &= ~PCF_ADATEN;
      ews88d_set_pcf8575 (devc, devc->gpio_tmp);
    }
  else
    {
      devc->gpio_tmp |= PCF_ADATEN;
      ews88d_set_pcf8575 (devc, devc->gpio_tmp);
    }
}

static int
ews88d_mixer_set (int dev, int ctrl, unsigned int cmd, int value)
{
  envy24_devc *devc = mixer_devs[dev]->devc;

  if (cmd == SNDCTL_MIX_READ)
    switch (ctrl)
      {
      case 1:
	return !!(devc->gpio_tmp & PCF_SPDIN_SELECT);
	break;

      case 2:
	return !!(devc->gpio_tmp & PCF_OPTOUT);
	break;

      case 3:
	return !(devc->gpio_tmp & PCF_ADATLOOP_);
	break;
      }

  if (cmd == SNDCTL_MIX_WRITE)
    switch (ctrl)
      {
      case 1:
	devc->gpio_tmp &= ~PCF_SPDIN_SELECT;
	if (value)
	  devc->gpio_tmp |= PCF_SPDIN_SELECT;
	ews88d_set_pcf8575 (devc, devc->gpio_tmp);
	return !!(devc->gpio_tmp & PCF_SPDIN_SELECT);
	break;

      case 2:
	devc->gpio_tmp &= ~PCF_OPTOUT;
	if (value)
	  devc->gpio_tmp |= PCF_OPTOUT;
	ews88d_set_pcf8575 (devc, devc->gpio_tmp);
	return !!(devc->gpio_tmp & PCF_OPTOUT);
	break;

      case 3:
	devc->gpio_tmp &= ~PCF_ADATLOOP_;
	if (!value)
	  devc->gpio_tmp |= PCF_ADATLOOP_;
	ews88d_set_pcf8575 (devc, devc->gpio_tmp);
	return !(devc->gpio_tmp & PCF_ADATLOOP_);
	break;
      }

  return OSS_EINVAL;
}

static int
ews88d_mix_init (envy24_devc * devc, int dev, int group)
{
  int err;

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

  if ((err = mixer_ext_create_control (dev, group,
				       1, ews88d_mixer_set,
				       MIXT_ENUM,
				       "EWS88D_SPDIN", 2,
				       MIXF_READABLE | MIXF_WRITEABLE)) < 0)
    return err;

  if ((err = mixer_ext_create_control (dev, group,
				       2, ews88d_mixer_set,
				       MIXT_ENUM,
				       "EWS88D_OPTOUT", 2,
				       MIXF_READABLE | MIXF_WRITEABLE)) < 0)
    return err;

  if ((err = mixer_ext_create_control (dev, group,
				       3, ews88d_mixer_set,
				       MIXT_ONOFF,
				       "EWS88D_ADATLOOP", 2,
				       MIXF_READABLE | MIXF_WRITEABLE)) < 0)
    return err;

  return 0;
}

envy24_auxdrv_t ews88d_auxdrv = {
  ews88d_card_init,
  ews88d_mix_init,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  set_ews88d_speed,
  NULL,
  cs8427_spdif_mixer_init
};