blob: 5e9b76edd81c94190116d4a184a5b0a2493ac932 [file] [log] [blame]
Ezequiel García9cb21732012-08-11 14:32:57 -03001/*
2 * STK1160 driver
3 *
4 * Copyright (C) 2012 Ezequiel Garcia
5 * <elezegarcia--a.t--gmail.com>
6 *
Marcel Haslere36e6b52016-12-15 20:17:26 -02007 * Copyright (C) 2016 Marcel Hasler
8 * <mahasler--a.t--gmail.com>
9 *
Ezequiel García9cb21732012-08-11 14:32:57 -030010 * Based on Easycap driver by R.M. Thomas
11 * Copyright (C) 2010 R.M. Thomas
12 * <rmthomas--a.t--sciolus.org>
13 *
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.
18 *
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.
23 *
24 */
25
Ezequiel García9cb21732012-08-11 14:32:57 -030026#include "stk1160.h"
27#include "stk1160-reg.h"
28
Marcel Haslere36e6b52016-12-15 20:17:26 -020029static void stk1160_write_ac97(struct stk1160 *dev, u16 reg, u16 value)
Ezequiel García9cb21732012-08-11 14:32:57 -030030{
Ezequiel García9cb21732012-08-11 14:32:57 -030031 /* Set codec register address */
32 stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
33
34 /* Set codec command */
35 stk1160_write_reg(dev, STK1160_AC97_CMD, value & 0xff);
36 stk1160_write_reg(dev, STK1160_AC97_CMD + 1, (value & 0xff00) >> 8);
37
38 /*
39 * Set command write bit to initiate write operation.
40 * The bit will be cleared when transfer is done.
41 */
42 stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8c);
43}
44
Marcel Haslere36e6b52016-12-15 20:17:26 -020045#ifdef DEBUG
46static u16 stk1160_read_ac97(struct stk1160 *dev, u16 reg)
Ezequiel García9cb21732012-08-11 14:32:57 -030047{
Ezequiel García9cb21732012-08-11 14:32:57 -030048 u8 vall = 0;
49 u8 valh = 0;
50
51 /* Set codec register address */
52 stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
53
54 /*
55 * Set command read bit to initiate read operation.
56 * The bit will be cleared when transfer is done.
57 */
58 stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8b);
59
60 /* Retrieve register value */
61 stk1160_read_reg(dev, STK1160_AC97_CMD, &vall);
62 stk1160_read_reg(dev, STK1160_AC97_CMD + 1, &valh);
63
64 return (valh << 8) | vall;
65}
66
Marcel Haslere36e6b52016-12-15 20:17:26 -020067void stk1160_ac97_dump_regs(struct stk1160 *dev)
Ezequiel García9cb21732012-08-11 14:32:57 -030068{
Marcel Haslere36e6b52016-12-15 20:17:26 -020069 u16 value;
70
71 value = stk1160_read_ac97(dev, 0x12); /* CD volume */
72 stk1160_dbg("0x12 == 0x%04x", value);
73
74 value = stk1160_read_ac97(dev, 0x10); /* Line-in volume */
75 stk1160_dbg("0x10 == 0x%04x", value);
76
77 value = stk1160_read_ac97(dev, 0x0e); /* MIC volume (mono) */
78 stk1160_dbg("0x0e == 0x%04x", value);
79
80 value = stk1160_read_ac97(dev, 0x16); /* Aux volume */
81 stk1160_dbg("0x16 == 0x%04x", value);
82
83 value = stk1160_read_ac97(dev, 0x1a); /* Record select */
84 stk1160_dbg("0x1a == 0x%04x", value);
85
86 value = stk1160_read_ac97(dev, 0x02); /* Master volume */
87 stk1160_dbg("0x02 == 0x%04x", value);
88
89 value = stk1160_read_ac97(dev, 0x1c); /* Record gain */
90 stk1160_dbg("0x1c == 0x%04x", value);
91}
92#endif
93
Marcel Hasler1dc7df42016-12-15 20:13:34 -020094int stk1160_has_audio(struct stk1160 *dev)
95{
96 u8 value;
97
98 stk1160_read_reg(dev, STK1160_POSV_L, &value);
99 return !(value & STK1160_POSV_L_ACDOUT);
100}
101
102int stk1160_has_ac97(struct stk1160 *dev)
103{
104 u8 value;
105
106 stk1160_read_reg(dev, STK1160_POSV_L, &value);
107 return !(value & STK1160_POSV_L_ACSYNC);
108}
109
Marcel Haslere36e6b52016-12-15 20:17:26 -0200110void stk1160_ac97_setup(struct stk1160 *dev)
111{
Marcel Hasler1dc7df42016-12-15 20:13:34 -0200112 if (!stk1160_has_audio(dev)) {
113 stk1160_info("Device doesn't support audio, skipping AC97 setup.");
114 return;
115 }
116
117 if (!stk1160_has_ac97(dev)) {
118 stk1160_info("Device uses internal 8-bit ADC, skipping AC97 setup.");
119 return;
120 }
121
Ezequiel García9cb21732012-08-11 14:32:57 -0300122 /* Two-step reset AC97 interface and hardware codec */
123 stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x94);
Marcel Haslere36e6b52016-12-15 20:17:26 -0200124 stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8c);
Ezequiel García9cb21732012-08-11 14:32:57 -0300125
126 /* Set 16-bit audio data and choose L&R channel*/
127 stk1160_write_reg(dev, STK1160_AC97CTL_1 + 2, 0x01);
Marcel Haslere36e6b52016-12-15 20:17:26 -0200128 stk1160_write_reg(dev, STK1160_AC97CTL_1 + 3, 0x00);
Ezequiel García9cb21732012-08-11 14:32:57 -0300129
Marcel Haslere36e6b52016-12-15 20:17:26 -0200130 /* Setup channels */
131 stk1160_write_ac97(dev, 0x12, 0x8808); /* CD volume */
132 stk1160_write_ac97(dev, 0x10, 0x0808); /* Line-in volume */
133 stk1160_write_ac97(dev, 0x0e, 0x0008); /* MIC volume (mono) */
134 stk1160_write_ac97(dev, 0x16, 0x0808); /* Aux volume */
135 stk1160_write_ac97(dev, 0x1a, 0x0404); /* Record select */
136 stk1160_write_ac97(dev, 0x02, 0x0000); /* Master volume */
137 stk1160_write_ac97(dev, 0x1c, 0x0808); /* Record gain */
Ezequiel García9cb21732012-08-11 14:32:57 -0300138
Marcel Haslere36e6b52016-12-15 20:17:26 -0200139#ifdef DEBUG
140 stk1160_ac97_dump_regs(dev);
141#endif
Ezequiel García9cb21732012-08-11 14:32:57 -0300142}