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