/*
 * tuner/tea5757h_if.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] =
  "$Id: tea5757h_if.c,v 1.11 2001/10/12 13:51:17 root Exp root $";

/*
 * $Log: tea5757h_if.c,v $
 * Revision 1.11  2001/10/12 13:51:17  root
 * #
 *
 * Revision 1.10  2001/01/30 06:16:30  root
 * #
 *
 * Revision 1.9  2000/12/01 04:42:19  root
 * #
 *
 * Revision 1.8  2000/12/01 04:13:51  root
 * #
 *
 * Revision 1.7  2000/11/23 23:45:28  root
 * #
 *
 * Revision 1.6  2000/10/07 18:30:09  root
 * #
 *
 * Revision 1.5  2000/10/06 19:52:44  root
 * #
 *
 * Revision 1.4  2000/09/24 01:52:21  root
 * #
 *
 * Revision 1.3  2000/09/24 01:48:15  root
 * #
 *
 */

#include "project.h"
#include <asm/io.h>

#ifdef ALPHA
#define inb_p(a) inb(a)
#define outb_p(a,b) outb(a,b)
#endif

extern int iopl (int);

typedef unsigned char byte;
static int base = 0xf84;

#define DATA 1
#define CLOCK 2
#define WREN 4

static int mutted;
static int stereo;
static int locked;

static int
get_data_line (void)
{
  return (inb_p (base + 1) & 1);
}

static int
get_status_line (void)
{
  int f = inb_p (base + 1);

  printf ("%02x ", f);
  f &= 0x8;
  return f;
}

static void
set_lines (int data, int clock, int wren)
{
  byte b = 0;

  if (clock)
    b |= CLOCK;
  if (data)
    b |= DATA;
  if (!wren)
    b |= WREN;

  outb_p (b, base);
}

DWord
tea5757h_read ()
{
  unsigned long ret = 0;

  unsigned long c = 1UL << 24;

  while (c)
    {
      if (get_data_line ())
        ret |= c;
      c >>= 1;

      if (c)
        {
          set_lines (1, 1, 0);
          stereo = get_status_line ()? 0 : 1;
          set_lines (1, 0, 0);
          locked = get_status_line ()? 0 : 1;
        }
    }

  printf ("\n");
  return ret;
}



void
tea5757h_write (DWord v)
{
  DWord c = 1UL << 24;

  set_lines (0, 0, 1);

  while (c)
    {
      int b = (c & v) ? 1 : 0;
      set_lines (b, 0, 1);
      set_lines (b, 1, 1);
      set_lines (b, 0, 1);
      c >>= 1;
    }

  mutted = 0;
  set_lines (0, 0, 0);
}

void
tea5757h_if_open (void)
{
  iopl (3);
}

int
tea5757h_ismutted (void)
{
  return (mutted);
}


void
tea5757h_mute (void)
{
  set_lines (0, 0, 1);
  mutted = 1;
}


int
tea5757h_isstereo (void)
{
  return stereo;
}

int
tea5757h_islocked (void)
{
  return locked;
}
