drm/radeon/kms: DP fixes and cleanup from the ddx
[linux-2.6-block.git] / drivers / gpu / drm / radeon / radeon_i2c.c
1 /*
2  * Copyright 2007-8 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: Dave Airlie
24  *          Alex Deucher
25  */
26 #include "drmP.h"
27 #include "radeon_drm.h"
28 #include "radeon.h"
29
30 /**
31  * radeon_ddc_probe
32  *
33  */
34 bool radeon_ddc_probe(struct radeon_connector *radeon_connector)
35 {
36         u8 out_buf[] = { 0x0, 0x0};
37         u8 buf[2];
38         int ret;
39         struct i2c_msg msgs[] = {
40                 {
41                         .addr = 0x50,
42                         .flags = 0,
43                         .len = 1,
44                         .buf = out_buf,
45                 },
46                 {
47                         .addr = 0x50,
48                         .flags = I2C_M_RD,
49                         .len = 1,
50                         .buf = buf,
51                 }
52         };
53
54         ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
55         if (ret == 2)
56                 return true;
57
58         return false;
59 }
60
61
62 void radeon_i2c_do_lock(struct radeon_i2c_chan *i2c, int lock_state)
63 {
64         struct radeon_device *rdev = i2c->dev->dev_private;
65         struct radeon_i2c_bus_rec *rec = &i2c->rec;
66         uint32_t temp;
67
68         /* RV410 appears to have a bug where the hw i2c in reset
69          * holds the i2c port in a bad state - switch hw i2c away before
70          * doing DDC - do this for all r200s/r300s/r400s for safety sake
71          */
72         if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
73                 if (rec->a_clk_reg == RADEON_GPIO_MONID) {
74                         WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
75                                                 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
76                 } else {
77                         WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
78                                                 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
79                 }
80         }
81
82         /* clear the output pin values */
83         temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
84         WREG32(rec->a_clk_reg, temp);
85
86         temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
87         WREG32(rec->a_data_reg, temp);
88
89
90         /* mask the gpio pins for software use */
91         temp = RREG32(rec->mask_clk_reg);
92         if (lock_state)
93                 temp |= rec->mask_clk_mask;
94         else
95                 temp &= ~rec->mask_clk_mask;
96         WREG32(rec->mask_clk_reg, temp);
97         temp = RREG32(rec->mask_clk_reg);
98
99         temp = RREG32(rec->mask_data_reg);
100         if (lock_state)
101                 temp |= rec->mask_data_mask;
102         else
103                 temp &= ~rec->mask_data_mask;
104         WREG32(rec->mask_data_reg, temp);
105         temp = RREG32(rec->mask_data_reg);
106 }
107
108 static int get_clock(void *i2c_priv)
109 {
110         struct radeon_i2c_chan *i2c = i2c_priv;
111         struct radeon_device *rdev = i2c->dev->dev_private;
112         struct radeon_i2c_bus_rec *rec = &i2c->rec;
113         uint32_t val;
114
115         /* read the value off the pin */
116         val = RREG32(rec->y_clk_reg);
117         val &= rec->y_clk_mask;
118
119         return (val != 0);
120 }
121
122
123 static int get_data(void *i2c_priv)
124 {
125         struct radeon_i2c_chan *i2c = i2c_priv;
126         struct radeon_device *rdev = i2c->dev->dev_private;
127         struct radeon_i2c_bus_rec *rec = &i2c->rec;
128         uint32_t val;
129
130         /* read the value off the pin */
131         val = RREG32(rec->y_data_reg);
132         val &= rec->y_data_mask;
133
134         return (val != 0);
135 }
136
137 static void set_clock(void *i2c_priv, int clock)
138 {
139         struct radeon_i2c_chan *i2c = i2c_priv;
140         struct radeon_device *rdev = i2c->dev->dev_private;
141         struct radeon_i2c_bus_rec *rec = &i2c->rec;
142         uint32_t val;
143
144         /* set pin direction */
145         val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
146         val |= clock ? 0 : rec->en_clk_mask;
147         WREG32(rec->en_clk_reg, val);
148 }
149
150 static void set_data(void *i2c_priv, int data)
151 {
152         struct radeon_i2c_chan *i2c = i2c_priv;
153         struct radeon_device *rdev = i2c->dev->dev_private;
154         struct radeon_i2c_bus_rec *rec = &i2c->rec;
155         uint32_t val;
156
157         /* set pin direction */
158         val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
159         val |= data ? 0 : rec->en_data_mask;
160         WREG32(rec->en_data_reg, val);
161 }
162
163 struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
164                                           struct radeon_i2c_bus_rec *rec,
165                                           const char *name)
166 {
167         struct radeon_i2c_chan *i2c;
168         int ret;
169
170         i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
171         if (i2c == NULL)
172                 return NULL;
173
174         i2c->adapter.owner = THIS_MODULE;
175         i2c->dev = dev;
176         i2c_set_adapdata(&i2c->adapter, i2c);
177         i2c->adapter.algo_data = &i2c->algo.bit;
178         i2c->algo.bit.setsda = set_data;
179         i2c->algo.bit.setscl = set_clock;
180         i2c->algo.bit.getsda = get_data;
181         i2c->algo.bit.getscl = get_clock;
182         i2c->algo.bit.udelay = 20;
183         /* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
184          * make this, 2 jiffies is a lot more reliable */
185         i2c->algo.bit.timeout = 2;
186         i2c->algo.bit.data = i2c;
187         i2c->rec = *rec;
188         ret = i2c_bit_add_bus(&i2c->adapter);
189         if (ret) {
190                 DRM_INFO("Failed to register i2c %s\n", name);
191                 goto out_free;
192         }
193
194         return i2c;
195 out_free:
196         kfree(i2c);
197         return NULL;
198
199 }
200
201 struct radeon_i2c_chan *radeon_i2c_create_dp(struct drm_device *dev,
202                                              const char *name, bool dp, u8 i2c_id)
203 {
204         struct radeon_i2c_chan *i2c;
205         int ret;
206
207         i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
208         if (i2c == NULL)
209                 return NULL;
210
211         i2c->i2c_id = i2c_id;
212         i2c->adapter.owner = THIS_MODULE;
213         i2c->dev = dev;
214         i2c_set_adapdata(&i2c->adapter, i2c);
215         i2c->adapter.algo_data = &i2c->algo.dp;
216         i2c->algo.dp.aux_ch = radeon_dp_i2c_aux_ch;
217         i2c->algo.dp.address = 0;
218         ret = i2c_dp_aux_add_bus(&i2c->adapter);
219         if (ret) {
220                 DRM_INFO("Failed to register i2c %s\n", name);
221                 goto out_free;
222         }
223
224         return i2c;
225 out_free:
226         kfree(i2c);
227         return NULL;
228
229 }
230
231
232 void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
233 {
234         if (!i2c)
235                 return;
236
237         i2c_del_adapter(&i2c->adapter);
238         kfree(i2c);
239 }
240
241 struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
242 {
243         return NULL;
244 }
245
246 void radeon_i2c_sw_get_byte(struct radeon_i2c_chan *i2c_bus,
247                             u8 slave_addr,
248                             u8 addr,
249                             u8 *val)
250 {
251         u8 out_buf[2];
252         u8 in_buf[2];
253         struct i2c_msg msgs[] = {
254                 {
255                         .addr = slave_addr,
256                         .flags = 0,
257                         .len = 1,
258                         .buf = out_buf,
259                 },
260                 {
261                         .addr = slave_addr,
262                         .flags = I2C_M_RD,
263                         .len = 1,
264                         .buf = in_buf,
265                 }
266         };
267
268         out_buf[0] = addr;
269         out_buf[1] = 0;
270
271         if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
272                 *val = in_buf[0];
273                 DRM_DEBUG("val = 0x%02x\n", *val);
274         } else {
275                 DRM_ERROR("i2c 0x%02x 0x%02x read failed\n",
276                           addr, *val);
277         }
278 }
279
280 void radeon_i2c_sw_put_byte(struct radeon_i2c_chan *i2c_bus,
281                             u8 slave_addr,
282                             u8 addr,
283                             u8 val)
284 {
285         uint8_t out_buf[2];
286         struct i2c_msg msg = {
287                 .addr = slave_addr,
288                 .flags = 0,
289                 .len = 2,
290                 .buf = out_buf,
291         };
292
293         out_buf[0] = addr;
294         out_buf[1] = val;
295
296         if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
297                 DRM_ERROR("i2c 0x%02x 0x%02x write failed\n",
298                           addr, val);
299 }
300