1/*
2 * ES1370 Haiku Driver for ES1370 audio
3 *
4 * Copyright 2002-2007, Haiku, Inc.
5 * Distributed under the terms of the MIT License.
6 *
7 * Authors:
8 *		Marcus Overhagen, marcus@overhagen.de
9 *		Jerome Duval, jerome.duval@free.fr
10 */
11#include <KernelExport.h>
12#include <OS.h>
13#include "io.h"
14#include "es1370reg.h"
15#include "debug.h"
16#include <PCI.h>
17
18extern pci_module_info  *pci;
19
20uint8
21es1370_reg_read_8(device_config *config, int regno)
22{
23	ASSERT(regno >= 0);
24	return pci->read_io_8(config->base + regno);
25}
26
27uint16
28es1370_reg_read_16(device_config *config, int regno)
29{
30	ASSERT(regno >= 0);
31	return pci->read_io_16(config->base + regno);
32}
33
34uint32
35es1370_reg_read_32(device_config *config, int regno)
36{
37	ASSERT(regno >= 0);
38	return pci->read_io_32(config->base + regno);
39}
40
41void
42es1370_reg_write_8(device_config *config, int regno, uint8 value)
43{
44	ASSERT(regno >= 0);
45	pci->write_io_8(config->base + regno, value);
46}
47
48void
49es1370_reg_write_16(device_config *config, int regno, uint16 value)
50{
51	ASSERT(regno >= 0);
52	pci->write_io_16(config->base + regno, value);
53}
54
55void
56es1370_reg_write_32(device_config *config, int regno, uint32 value)
57{
58	ASSERT(regno >= 0);
59	pci->write_io_32(config->base + regno, value);
60}
61
62/* codec */
63static int
64es1370_codec_wait(device_config *config)
65{
66	int i;
67	for (i = 0; i < 1100; i++) {
68		if ((es1370_reg_read_32(config, ES1370_REG_STATUS) & STAT_CWRIP) == 0)
69			return B_OK;
70		if (i > 100)
71			spin(1);
72	}
73	return B_TIMED_OUT;
74}
75
76uint16
77es1370_codec_read(device_config *config, int regno)
78{
79	ASSERT(regno >= 0);
80	if(es1370_codec_wait(config)!=B_OK) {
81		PRINT(("codec busy (2)\n"));
82		return -1;
83	}
84
85	return pci->read_io_32(config->base + ES1370_REG_CODEC);
86}
87
88void
89es1370_codec_write(device_config *config, int regno, uint16 value)
90{
91	ASSERT(regno >= 0);
92	if(es1370_codec_wait(config)!=B_OK) {
93		PRINT(("codec busy (4)\n"));
94		return;
95	}
96	pci->write_io_32(config->base + ES1370_REG_CODEC, (regno << 8) | value);
97}
98