-
Notifications
You must be signed in to change notification settings - Fork 1
/
Phase88.cpp
executable file
·276 lines (197 loc) · 5.79 KB
/
Phase88.cpp
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
#include "misc.h"
#include "DriverData.h"
#include "regs.h"
#include "ak_codec.h"
#include "Phase88.h"
#include <IOKit/IOLib.h>
#define SND_CS8404
static void Phase88_Start(struct CardData *card)
{
SaveGPIOStatus(card);
WriteCCI(card, CCI_GPIO_DIR, card->gpio_dir | PHASE88_RW); // prepare for write
WriteCCI(card, CCI_GPIO_MASK, ~PHASE88_RW); // prevent RW from switching
}
static void Phase88_Stop(struct CardData *card)
{
RestoreGPIOStatus(card);
}
// Set the direction of the CLK and SDA lines:
// For sending, use 1, 1
static void Phase88_SetDir_CLK_SDA(struct CardData *card, int clock, int data)
{
unsigned char mask = 0;
if (clock)
mask |= PHASE88_CLOCK; /* write SCL */
if (data)
mask |= PHASE88_DATA; /* write SDA */
WriteCCI(card, CCI_GPIO_DIR, (ReadCCI(card, CCI_GPIO_DIR) & ~(PHASE88_CLOCK | PHASE88_DATA)) | mask); // tbd: mag weg?
WriteCCI(card, CCI_GPIO_MASK, ~mask);
}
// Write data to the SDA line and clock to the SCL
static void Phase88_Write_CLK_SDA(struct CardData *card, int clk, int data)
{
unsigned char tmp = 0;
if (clk)
tmp |= PHASE88_CLOCK;
if (data)
tmp |= PHASE88_DATA;
WriteCCI(card, CCI_GPIO_DATA, tmp);
MicroDelay(5);
}
static int Phase88_GetClock(struct CardData *card)
{
unsigned char tmp;
tmp = ReadCCI(card, CCI_GPIO_DATA);
if (tmp & PHASE88_CLOCK)
return 1;
else
return 0;
}
static int Phase88_GetDataBit(struct CardData *card, int ack)
{
int bit;
//set RW to read mode
WriteCCI(card, CCI_GPIO_MASK, ~PHASE88_RW); // clear rw mask first
WriteCCI(card, CCI_GPIO_DATA, 0); // set to read
if (ack)
MicroDelay(5);
// get data bit from the GPIO pines
bit = ReadCCI(card, CCI_GPIO_DATA) & PHASE88_DATA ? 1 : 0;
/* set RW pin to high */
WriteCCI(card, CCI_GPIO_DATA, PHASE88_RW); // set to write
/* reset write mask */
WriteCCI(card, CCI_GPIO_MASK, ~PHASE88_CLOCK);
return bit;
}
// ---------------------------------------
/*
* AK4524 access
*/
/* AK4524 chip select; address 0x48 bit 0-3 */
static int Phase88_CS(struct CardData *card, unsigned char chip_mask)
{
unsigned char data, ndata;
if (chip_mask > 0x0f)
return 0;
if (ReadBytesI2C(card, card->i2c_out_addr, &data, 1) != 1)
goto __error;
ndata = (data & 0xf0) | chip_mask;
if (ndata != data)
if (WriteBytesI2C(card, card->i2c_out_addr, &ndata, 1) != 1)
goto __error;
return 0;
__error:
return -1;
}
/* start callback for PHASE88, needs to select a certain chip mask */
void Phase88_ak4524_lock(struct CardData *card, int chip)
{
unsigned char tmp;
if (Phase88_CS(card, ~(1 << chip) & 0x0f) < 0)
IOLog("Can't select chip\n");
SaveGPIOStatus(card);
tmp = PHASE88_DATA | PHASE88_CLOCK | PHASE88_RW;
WriteCCI(card, CCI_GPIO_DIR, card->gpio_dir | tmp);
WriteCCI(card, CCI_GPIO_MASK, ~tmp);
}
/* stop callback for PHASE88, needs to deselect chip mask */
void Phase88_ak4524_unlock(struct CardData *card, int chip)
{
RestoreGPIOStatus(card);
MicroDelay(1);
Phase88_CS(card, 0x0f);
}
static struct I2C_bit_ops Phase88_bit_ops = {
Phase88_Start,
Phase88_Stop,
Phase88_SetDir_CLK_SDA,
Phase88_Write_CLK_SDA,
Phase88_GetClock,
Phase88_GetDataBit,
};
void Phase88_akm4xxx_write(struct CardData *card, int chip, unsigned char addr, unsigned char data)
{
unsigned int tmp;
int idx;
unsigned int addrdata;
if (!(chip >= 0 && chip < 4))
return;
Phase88_ak4524_lock(card, chip);
tmp = ReadCCI(card, CCI_GPIO_DATA);
tmp |= PHASE88_RW;
/* build I2C address + data byte */
addrdata = (2 << 6) | 0x20 | (addr & 0x1f);
addrdata = (addrdata << 8) | data;
for (idx = 15; idx >= 0; idx--) {
/* drop clock */
tmp &= ~PHASE88_CLOCK;
WriteCCI(card, CCI_GPIO_DATA, tmp);
MicroDelay(1);
/* set data */
if (addrdata & (1 << idx))
tmp |= PHASE88_DATA;
else
tmp &= ~PHASE88_DATA;
WriteCCI(card, CCI_GPIO_DATA, tmp);
MicroDelay(1);
/* raise clock */
tmp |= PHASE88_CLOCK;
WriteCCI(card, CCI_GPIO_DATA, tmp);
MicroDelay(1);
}
/* assert a cs pulse to trigger */
WriteCCI(card, CCI_GPIO_DATA, tmp);
MicroDelay(1);
Phase88_ak4524_unlock(card, chip);
}
int Phase88_Init(struct CardData *card)
{
int err = 0;
/* create i2c devices */
card->i2c_cs8404 = AllocI2C(0x40 >> 1);
card->i2c_in_addr = AllocI2C(0x46 >> 1);
card->i2c_out_addr = AllocI2C(0x48 >> 1);
card->bit_ops = &Phase88_bit_ops;
/* Check if the front module is connected */
if ((err = Phase88_CS(card, 0x0f)) < 0) {
IOLog("Front module not connected?\n");
return err;
}
unsigned char *ptr, reg, data, *inits;
static unsigned char inits_phase88_ak4524[] = {
0x00, 0x07, /* 0: all power up */
0x01, 0x00, /* 1: ADC/DAC reset */
0x02, 0x60, /* 2: 24bit I2S */
0x03, 0x19, /* 3: deemphasis off */
0x01, 0x03, /* 1: ADC/DAC enable */
0x04, 0x80, /* 4: ADC IPGA gain 0dB */
0x05, 0x80, /* 5: ADC IPGA gain 0dB */
0x06, 0x7E, /* 6: DAC left muted */
0x07, 0x7E, /* 7: DAC right muted */
0xff, 0xff
};
inits = inits_phase88_ak4524;
for (int chip = 0; chip < 4; chip++)
{
ptr = inits;
while (*ptr != 0xff)
{
reg = *ptr++;
data = *ptr++;
Phase88_akm4xxx_write(card, chip, reg, data);
}
}
#if 0
/* set up SPDIF interface */
/* set up CS8404 */
ice->spdif.ops.open = ews88_open_spdif;
ice->spdif.ops.setup_rate = ews88_setup_spdif;
ice->spdif.ops.default_get = ews88_spdif_default_get;
ice->spdif.ops.default_put = ews88_spdif_default_put;
ice->spdif.ops.stream_get = ews88_spdif_stream_get;
ice->spdif.ops.stream_put = ews88_spdif_stream_put;
/* Set spdif defaults */
snd_ice1712_ews_cs8404_spdif_write(ice, ice->spdif.cs8403_bits);
#endif
return 0;
}