radeonfb_i2c.c revision 1.1.12.3 1 1.1.12.2 yamt /*-
2 1.1.12.2 yamt * Copyright (c) 2006 Itronix Inc.
3 1.1.12.2 yamt * All rights reserved.
4 1.1.12.2 yamt *
5 1.1.12.2 yamt * Written by Garrett D'Amore for Itronix Inc.
6 1.1.12.2 yamt *
7 1.1.12.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.12.2 yamt * modification, are permitted provided that the following conditions
9 1.1.12.2 yamt * are met:
10 1.1.12.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.12.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.12.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.12.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1.12.2 yamt * documentation and/or other materials provided with the distribution.
15 1.1.12.2 yamt * 3. The name of Itronix Inc. may not be used to endorse
16 1.1.12.2 yamt * or promote products derived from this software without specific
17 1.1.12.2 yamt * prior written permission.
18 1.1.12.2 yamt *
19 1.1.12.2 yamt * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
20 1.1.12.2 yamt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1.12.2 yamt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1.12.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23 1.1.12.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1.12.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 1.1.12.2 yamt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.12.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 1.1.12.2 yamt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 1.1.12.2 yamt * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 1.1.12.2 yamt * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1.12.2 yamt */
31 1.1.12.2 yamt
32 1.1.12.2 yamt /*
33 1.1.12.2 yamt * ATI Technologies Inc. ("ATI") has not assisted in the creation of, and
34 1.1.12.2 yamt * does not endorse, this software. ATI will not be responsible or liable
35 1.1.12.2 yamt * for any actual or alleged damage or loss caused by or in connection with
36 1.1.12.2 yamt * the use of or reliance on this software.
37 1.1.12.2 yamt */
38 1.1.12.2 yamt
39 1.1.12.2 yamt #include <sys/cdefs.h>
40 1.1.12.3 yamt __KERNEL_RCSID(0, "$NetBSD: radeonfb_i2c.c,v 1.1.12.3 2007/10/27 11:33:29 yamt Exp $");
41 1.1.12.2 yamt
42 1.1.12.2 yamt #include <sys/param.h>
43 1.1.12.2 yamt #include <sys/systm.h>
44 1.1.12.2 yamt #include <sys/device.h>
45 1.1.12.2 yamt #include <sys/malloc.h>
46 1.1.12.2 yamt #include <sys/systm.h>
47 1.1.12.3 yamt #include <sys/bus.h>
48 1.1.12.2 yamt
49 1.1.12.2 yamt #include <dev/i2c/i2cvar.h>
50 1.1.12.2 yamt #include <dev/i2c/i2c_bitbang.h>
51 1.1.12.2 yamt #include <dev/i2c/ddcvar.h>
52 1.1.12.2 yamt
53 1.1.12.2 yamt #include <dev/pci/radeonfbreg.h>
54 1.1.12.2 yamt #include <dev/pci/radeonfbvar.h>
55 1.1.12.2 yamt
56 1.1.12.2 yamt /* i2c support */
57 1.1.12.2 yamt static int radeonfb_i2c_acquire_bus(void *, int);
58 1.1.12.2 yamt static void radeonfb_i2c_release_bus(void *, int);
59 1.1.12.2 yamt static int radeonfb_i2c_send_start(void *, int);
60 1.1.12.2 yamt static int radeonfb_i2c_send_stop(void *, int);
61 1.1.12.2 yamt static int radeonfb_i2c_initiate_xfer(void *, i2c_addr_t, int);
62 1.1.12.2 yamt static int radeonfb_i2c_read_byte(void *, uint8_t *, int);
63 1.1.12.2 yamt static int radeonfb_i2c_write_byte(void *, uint8_t, int);
64 1.1.12.2 yamt
65 1.1.12.2 yamt /* i2c bit-bang glue */
66 1.1.12.2 yamt static void radeonfb_i2cbb_set_bits(void *, uint32_t);
67 1.1.12.2 yamt static void radeonfb_i2cbb_set_dir(void *, uint32_t);
68 1.1.12.2 yamt static uint32_t radeonfb_i2cbb_read(void *);
69 1.1.12.2 yamt
70 1.1.12.2 yamt /*
71 1.1.12.2 yamt * I2C bit-bang operations
72 1.1.12.2 yamt */
73 1.1.12.2 yamt void
74 1.1.12.2 yamt radeonfb_i2cbb_set_bits(void *cookie, uint32_t bits)
75 1.1.12.2 yamt {
76 1.1.12.2 yamt struct radeonfb_i2c *ric = (struct radeonfb_i2c *)cookie;
77 1.1.12.2 yamt struct radeonfb_softc *sc = ric->ric_softc;
78 1.1.12.2 yamt
79 1.1.12.2 yamt PATCH32(sc, ric->ric_register, bits,
80 1.1.12.2 yamt ~(RADEON_GPIO_A_0 | RADEON_GPIO_A_1));
81 1.1.12.2 yamt }
82 1.1.12.2 yamt
83 1.1.12.2 yamt void
84 1.1.12.2 yamt radeonfb_i2cbb_set_dir(void *cookie, uint32_t bits)
85 1.1.12.2 yamt {
86 1.1.12.2 yamt struct radeonfb_i2c *ric = (struct radeonfb_i2c *)cookie;
87 1.1.12.2 yamt struct radeonfb_softc *sc = ric->ric_softc;
88 1.1.12.2 yamt
89 1.1.12.2 yamt PATCH32(sc, ric->ric_register, bits, ~(RADEON_GPIO_EN_0));
90 1.1.12.2 yamt }
91 1.1.12.2 yamt
92 1.1.12.2 yamt uint32_t
93 1.1.12.2 yamt radeonfb_i2cbb_read(void *cookie)
94 1.1.12.2 yamt {
95 1.1.12.2 yamt struct radeonfb_i2c *ric = (struct radeonfb_i2c *)cookie;
96 1.1.12.2 yamt struct radeonfb_softc *sc = ric->ric_softc;
97 1.1.12.2 yamt
98 1.1.12.2 yamt /* output bit is 0 shifted, input bit is shifted 8 */
99 1.1.12.2 yamt return (GET32(sc, ric->ric_register) >> RADEON_GPIO_Y_SHIFT_0);
100 1.1.12.2 yamt }
101 1.1.12.2 yamt
102 1.1.12.2 yamt static const struct i2c_bitbang_ops radeonfb_i2cbb_ops = {
103 1.1.12.2 yamt radeonfb_i2cbb_set_bits,
104 1.1.12.2 yamt radeonfb_i2cbb_set_dir,
105 1.1.12.2 yamt radeonfb_i2cbb_read,
106 1.1.12.2 yamt {
107 1.1.12.2 yamt RADEON_GPIO_A_0, /* SDA */
108 1.1.12.2 yamt RADEON_GPIO_A_1, /* SCL */
109 1.1.12.2 yamt RADEON_GPIO_EN_0, /* SDA output */
110 1.1.12.2 yamt 0, /* SDA input */
111 1.1.12.2 yamt }
112 1.1.12.2 yamt };
113 1.1.12.2 yamt
114 1.1.12.2 yamt /*
115 1.1.12.2 yamt * I2C support
116 1.1.12.2 yamt */
117 1.1.12.2 yamt int
118 1.1.12.2 yamt radeonfb_i2c_acquire_bus(void *cookie, int flags)
119 1.1.12.2 yamt {
120 1.1.12.2 yamt struct radeonfb_i2c *ric = (struct radeonfb_i2c *)cookie;
121 1.1.12.2 yamt struct radeonfb_softc *sc = ric->ric_softc;
122 1.1.12.2 yamt int i;
123 1.1.12.2 yamt
124 1.1.12.2 yamt /*
125 1.1.12.2 yamt * Some hardware seems to have hardware/software combined access
126 1.1.12.2 yamt * to the DVI I2C. We want to use software.
127 1.1.12.2 yamt */
128 1.1.12.2 yamt if (ric->ric_register == RADEON_GPIO_DVI_DDC) {
129 1.1.12.2 yamt
130 1.1.12.2 yamt /* ask for software access to I2C bus */
131 1.1.12.2 yamt SET32(sc, ric->ric_register, RADEON_GPIO_SW_USE);
132 1.1.12.2 yamt
133 1.1.12.2 yamt /*
134 1.1.12.2 yamt * wait for the chip to give up access. we don't make
135 1.1.12.2 yamt * this a hard timeout, because some hardware might
136 1.1.12.2 yamt * not implement this negotiation protocol
137 1.1.12.2 yamt */
138 1.1.12.2 yamt for (i = RADEON_TIMEOUT; i; i--) {
139 1.1.12.2 yamt if (GET32(sc, ric->ric_register) & RADEON_GPIO_SW_USE)
140 1.1.12.2 yamt break;
141 1.1.12.2 yamt }
142 1.1.12.2 yamt }
143 1.1.12.2 yamt
144 1.1.12.2 yamt /* enable the I2C clock */
145 1.1.12.2 yamt SET32(sc, ric->ric_register, RADEON_GPIO_EN_1);
146 1.1.12.2 yamt
147 1.1.12.2 yamt return 0;
148 1.1.12.2 yamt }
149 1.1.12.2 yamt
150 1.1.12.2 yamt void
151 1.1.12.2 yamt radeonfb_i2c_release_bus(void *cookie, int flags)
152 1.1.12.2 yamt {
153 1.1.12.2 yamt struct radeonfb_i2c *ric = (struct radeonfb_i2c *)cookie;
154 1.1.12.2 yamt struct radeonfb_softc *sc = ric->ric_softc;
155 1.1.12.2 yamt
156 1.1.12.2 yamt if (ric->ric_register == RADEON_GPIO_DVI_DDC) {
157 1.1.12.2 yamt /* we no longer "want" I2C, and we're "done" with it */
158 1.1.12.2 yamt CLR32(sc, ric->ric_register, RADEON_GPIO_SW_USE);
159 1.1.12.2 yamt SET32(sc, ric->ric_register, RADEON_GPIO_SW_DONE);
160 1.1.12.2 yamt }
161 1.1.12.2 yamt }
162 1.1.12.2 yamt
163 1.1.12.2 yamt int
164 1.1.12.2 yamt radeonfb_i2c_send_start(void *cookie, int flags)
165 1.1.12.2 yamt {
166 1.1.12.2 yamt
167 1.1.12.2 yamt return i2c_bitbang_send_start(cookie, flags, &radeonfb_i2cbb_ops);
168 1.1.12.2 yamt }
169 1.1.12.2 yamt
170 1.1.12.2 yamt int
171 1.1.12.2 yamt radeonfb_i2c_send_stop(void *cookie, int flags)
172 1.1.12.2 yamt {
173 1.1.12.2 yamt
174 1.1.12.2 yamt return i2c_bitbang_send_stop(cookie, flags, &radeonfb_i2cbb_ops);
175 1.1.12.2 yamt }
176 1.1.12.2 yamt
177 1.1.12.2 yamt int
178 1.1.12.2 yamt radeonfb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
179 1.1.12.2 yamt {
180 1.1.12.2 yamt
181 1.1.12.2 yamt return i2c_bitbang_initiate_xfer(cookie, addr, flags,
182 1.1.12.2 yamt &radeonfb_i2cbb_ops);
183 1.1.12.2 yamt }
184 1.1.12.2 yamt
185 1.1.12.2 yamt int
186 1.1.12.2 yamt radeonfb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
187 1.1.12.2 yamt {
188 1.1.12.2 yamt
189 1.1.12.2 yamt return i2c_bitbang_read_byte(cookie, valp, flags, &radeonfb_i2cbb_ops);
190 1.1.12.2 yamt }
191 1.1.12.2 yamt
192 1.1.12.2 yamt int
193 1.1.12.2 yamt radeonfb_i2c_write_byte(void *cookie, uint8_t val, int flags)
194 1.1.12.2 yamt {
195 1.1.12.2 yamt
196 1.1.12.2 yamt return i2c_bitbang_write_byte(cookie, val, flags, &radeonfb_i2cbb_ops);
197 1.1.12.2 yamt }
198 1.1.12.2 yamt
199 1.1.12.2 yamt void
200 1.1.12.2 yamt radeonfb_i2c_init(struct radeonfb_softc *sc)
201 1.1.12.2 yamt {
202 1.1.12.2 yamt int i;
203 1.1.12.2 yamt
204 1.1.12.2 yamt for (i = 0; i < 4; i++) {
205 1.1.12.2 yamt struct i2c_controller *icc = &sc->sc_i2c[i].ric_controller;
206 1.1.12.2 yamt
207 1.1.12.2 yamt sc->sc_i2c[i].ric_softc = sc;
208 1.1.12.2 yamt icc->ic_cookie = &sc->sc_i2c[i];
209 1.1.12.2 yamt icc->ic_acquire_bus = radeonfb_i2c_acquire_bus;
210 1.1.12.2 yamt icc->ic_release_bus = radeonfb_i2c_release_bus;
211 1.1.12.2 yamt icc->ic_send_start = radeonfb_i2c_send_start;
212 1.1.12.2 yamt icc->ic_send_stop = radeonfb_i2c_send_stop;
213 1.1.12.2 yamt icc->ic_initiate_xfer = radeonfb_i2c_initiate_xfer;
214 1.1.12.2 yamt icc->ic_read_byte = radeonfb_i2c_read_byte;
215 1.1.12.2 yamt icc->ic_write_byte = radeonfb_i2c_write_byte;
216 1.1.12.2 yamt }
217 1.1.12.2 yamt
218 1.1.12.2 yamt /* index == ddctype (RADEON_DDC_XX) - 1 */
219 1.1.12.2 yamt sc->sc_i2c[0].ric_register = RADEON_GPIO_MONID;
220 1.1.12.2 yamt sc->sc_i2c[1].ric_register = RADEON_GPIO_DVI_DDC;
221 1.1.12.2 yamt sc->sc_i2c[2].ric_register = RADEON_GPIO_VGA_DDC;
222 1.1.12.2 yamt sc->sc_i2c[3].ric_register = RADEON_GPIO_CRT2_DDC;
223 1.1.12.2 yamt }
224 1.1.12.2 yamt
225 1.1.12.2 yamt int
226 1.1.12.2 yamt radeonfb_i2c_read_edid(struct radeonfb_softc *sc, int ddctype, uint8_t *data)
227 1.1.12.2 yamt {
228 1.1.12.2 yamt
229 1.1.12.2 yamt if ((ddctype < 1) || (ddctype > 4))
230 1.1.12.2 yamt return EINVAL;
231 1.1.12.2 yamt
232 1.1.12.2 yamt ddctype--;
233 1.1.12.2 yamt return (ddc_read_edid(&sc->sc_i2c[ddctype].ric_controller, data, 128));
234 1.1.12.2 yamt }
235