4 * Copyright (C) 2012 Ezequiel Garcia
5 * <elezegarcia--a.t--gmail.com>
7 * Copyright (C) 2016 Marcel Hasler
8 * <mahasler--a.t--gmail.com>
10 * Based on Easycap driver by R.M. Thomas
11 * Copyright (C) 2010 R.M. Thomas
12 * <rmthomas--a.t--sciolus.org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
26 #include <linux/delay.h>
29 #include "stk1160-reg.h"
31 static int stk1160_ac97_wait_transfer_complete(struct stk1160
*dev
)
33 unsigned long timeout
= jiffies
+ msecs_to_jiffies(STK1160_AC97_TIMEOUT
);
36 /* Wait for AC97 transfer to complete */
37 while (time_is_after_jiffies(timeout
)) {
38 stk1160_read_reg(dev
, STK1160_AC97CTL_0
, &value
);
40 if (!(value
& (STK1160_AC97CTL_0_CR
| STK1160_AC97CTL_0_CW
)))
43 usleep_range(50, 100);
46 stk1160_err("AC97 transfer took too long, this should never happen!");
50 static void stk1160_write_ac97(struct stk1160
*dev
, u16 reg
, u16 value
)
52 /* Set codec register address */
53 stk1160_write_reg(dev
, STK1160_AC97_ADDR
, reg
);
55 /* Set codec command */
56 stk1160_write_reg(dev
, STK1160_AC97_CMD
, value
& 0xff);
57 stk1160_write_reg(dev
, STK1160_AC97_CMD
+ 1, (value
& 0xff00) >> 8);
59 /* Set command write bit to initiate write operation */
60 stk1160_write_reg(dev
, STK1160_AC97CTL_0
, 0x8c);
62 /* Wait for command write bit to be cleared */
63 stk1160_ac97_wait_transfer_complete(dev
);
67 static u16
stk1160_read_ac97(struct stk1160
*dev
, u16 reg
)
72 /* Set codec register address */
73 stk1160_write_reg(dev
, STK1160_AC97_ADDR
, reg
);
75 /* Set command read bit to initiate read operation */
76 stk1160_write_reg(dev
, STK1160_AC97CTL_0
, 0x8b);
78 /* Wait for command read bit to be cleared */
79 if (stk1160_ac97_wait_transfer_complete(dev
) < 0)
83 /* Retrieve register value */
84 stk1160_read_reg(dev
, STK1160_AC97_CMD
, &vall
);
85 stk1160_read_reg(dev
, STK1160_AC97_CMD
+ 1, &valh
);
87 return (valh
<< 8) | vall
;
90 void stk1160_ac97_dump_regs(struct stk1160
*dev
)
94 value
= stk1160_read_ac97(dev
, 0x12); /* CD volume */
95 stk1160_dbg("0x12 == 0x%04x", value
);
97 value
= stk1160_read_ac97(dev
, 0x10); /* Line-in volume */
98 stk1160_dbg("0x10 == 0x%04x", value
);
100 value
= stk1160_read_ac97(dev
, 0x0e); /* MIC volume (mono) */
101 stk1160_dbg("0x0e == 0x%04x", value
);
103 value
= stk1160_read_ac97(dev
, 0x16); /* Aux volume */
104 stk1160_dbg("0x16 == 0x%04x", value
);
106 value
= stk1160_read_ac97(dev
, 0x1a); /* Record select */
107 stk1160_dbg("0x1a == 0x%04x", value
);
109 value
= stk1160_read_ac97(dev
, 0x02); /* Master volume */
110 stk1160_dbg("0x02 == 0x%04x", value
);
112 value
= stk1160_read_ac97(dev
, 0x1c); /* Record gain */
113 stk1160_dbg("0x1c == 0x%04x", value
);
117 static int stk1160_has_audio(struct stk1160
*dev
)
121 stk1160_read_reg(dev
, STK1160_POSV_L
, &value
);
122 return !(value
& STK1160_POSV_L_ACDOUT
);
125 static int stk1160_has_ac97(struct stk1160
*dev
)
129 stk1160_read_reg(dev
, STK1160_POSV_L
, &value
);
130 return !(value
& STK1160_POSV_L_ACSYNC
);
133 void stk1160_ac97_setup(struct stk1160
*dev
)
135 if (!stk1160_has_audio(dev
)) {
136 stk1160_info("Device doesn't support audio, skipping AC97 setup.");
140 if (!stk1160_has_ac97(dev
)) {
141 stk1160_info("Device uses internal 8-bit ADC, skipping AC97 setup.");
145 /* Two-step reset AC97 interface and hardware codec */
146 stk1160_write_reg(dev
, STK1160_AC97CTL_0
, 0x94);
147 stk1160_write_reg(dev
, STK1160_AC97CTL_0
, 0x8c);
149 /* Set 16-bit audio data and choose L&R channel*/
150 stk1160_write_reg(dev
, STK1160_AC97CTL_1
+ 2, 0x01);
151 stk1160_write_reg(dev
, STK1160_AC97CTL_1
+ 3, 0x00);
154 stk1160_write_ac97(dev
, 0x12, 0x8808); /* CD volume */
155 stk1160_write_ac97(dev
, 0x10, 0x0808); /* Line-in volume */
156 stk1160_write_ac97(dev
, 0x0e, 0x0008); /* MIC volume (mono) */
157 stk1160_write_ac97(dev
, 0x16, 0x0808); /* Aux volume */
158 stk1160_write_ac97(dev
, 0x1a, 0x0404); /* Record select */
159 stk1160_write_ac97(dev
, 0x02, 0x0000); /* Master volume */
160 stk1160_write_ac97(dev
, 0x1c, 0x0808); /* Record gain */
163 stk1160_ac97_dump_regs(dev
);