2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
23 * Authors: Dave Airlie
27 #include <linux/export.h>
28 #include <linux/pci.h>
30 #include <drm/drm_edid.h>
31 #include <drm/amdgpu_drm.h>
33 #include "amdgpu_i2c.h"
34 #include "amdgpu_atombios.h"
36 #include "atombios_dp.h"
37 #include "atombios_i2c.h"
40 static int amdgpu_i2c_pre_xfer(struct i2c_adapter
*i2c_adap
)
42 struct amdgpu_i2c_chan
*i2c
= i2c_get_adapdata(i2c_adap
);
43 struct amdgpu_device
*adev
= i2c
->dev
->dev_private
;
44 struct amdgpu_i2c_bus_rec
*rec
= &i2c
->rec
;
47 mutex_lock(&i2c
->mutex
);
49 /* switch the pads to ddc mode */
50 if (rec
->hw_capable
) {
51 temp
= RREG32(rec
->mask_clk_reg
);
53 WREG32(rec
->mask_clk_reg
, temp
);
56 /* clear the output pin values */
57 temp
= RREG32(rec
->a_clk_reg
) & ~rec
->a_clk_mask
;
58 WREG32(rec
->a_clk_reg
, temp
);
60 temp
= RREG32(rec
->a_data_reg
) & ~rec
->a_data_mask
;
61 WREG32(rec
->a_data_reg
, temp
);
63 /* set the pins to input */
64 temp
= RREG32(rec
->en_clk_reg
) & ~rec
->en_clk_mask
;
65 WREG32(rec
->en_clk_reg
, temp
);
67 temp
= RREG32(rec
->en_data_reg
) & ~rec
->en_data_mask
;
68 WREG32(rec
->en_data_reg
, temp
);
70 /* mask the gpio pins for software use */
71 temp
= RREG32(rec
->mask_clk_reg
) | rec
->mask_clk_mask
;
72 WREG32(rec
->mask_clk_reg
, temp
);
73 temp
= RREG32(rec
->mask_clk_reg
);
75 temp
= RREG32(rec
->mask_data_reg
) | rec
->mask_data_mask
;
76 WREG32(rec
->mask_data_reg
, temp
);
77 temp
= RREG32(rec
->mask_data_reg
);
82 static void amdgpu_i2c_post_xfer(struct i2c_adapter
*i2c_adap
)
84 struct amdgpu_i2c_chan
*i2c
= i2c_get_adapdata(i2c_adap
);
85 struct amdgpu_device
*adev
= i2c
->dev
->dev_private
;
86 struct amdgpu_i2c_bus_rec
*rec
= &i2c
->rec
;
89 /* unmask the gpio pins for software use */
90 temp
= RREG32(rec
->mask_clk_reg
) & ~rec
->mask_clk_mask
;
91 WREG32(rec
->mask_clk_reg
, temp
);
92 temp
= RREG32(rec
->mask_clk_reg
);
94 temp
= RREG32(rec
->mask_data_reg
) & ~rec
->mask_data_mask
;
95 WREG32(rec
->mask_data_reg
, temp
);
96 temp
= RREG32(rec
->mask_data_reg
);
98 mutex_unlock(&i2c
->mutex
);
101 static int amdgpu_i2c_get_clock(void *i2c_priv
)
103 struct amdgpu_i2c_chan
*i2c
= i2c_priv
;
104 struct amdgpu_device
*adev
= i2c
->dev
->dev_private
;
105 struct amdgpu_i2c_bus_rec
*rec
= &i2c
->rec
;
108 /* read the value off the pin */
109 val
= RREG32(rec
->y_clk_reg
);
110 val
&= rec
->y_clk_mask
;
116 static int amdgpu_i2c_get_data(void *i2c_priv
)
118 struct amdgpu_i2c_chan
*i2c
= i2c_priv
;
119 struct amdgpu_device
*adev
= i2c
->dev
->dev_private
;
120 struct amdgpu_i2c_bus_rec
*rec
= &i2c
->rec
;
123 /* read the value off the pin */
124 val
= RREG32(rec
->y_data_reg
);
125 val
&= rec
->y_data_mask
;
130 static void amdgpu_i2c_set_clock(void *i2c_priv
, int clock
)
132 struct amdgpu_i2c_chan
*i2c
= i2c_priv
;
133 struct amdgpu_device
*adev
= i2c
->dev
->dev_private
;
134 struct amdgpu_i2c_bus_rec
*rec
= &i2c
->rec
;
137 /* set pin direction */
138 val
= RREG32(rec
->en_clk_reg
) & ~rec
->en_clk_mask
;
139 val
|= clock
? 0 : rec
->en_clk_mask
;
140 WREG32(rec
->en_clk_reg
, val
);
143 static void amdgpu_i2c_set_data(void *i2c_priv
, int data
)
145 struct amdgpu_i2c_chan
*i2c
= i2c_priv
;
146 struct amdgpu_device
*adev
= i2c
->dev
->dev_private
;
147 struct amdgpu_i2c_bus_rec
*rec
= &i2c
->rec
;
150 /* set pin direction */
151 val
= RREG32(rec
->en_data_reg
) & ~rec
->en_data_mask
;
152 val
|= data
? 0 : rec
->en_data_mask
;
153 WREG32(rec
->en_data_reg
, val
);
156 static const struct i2c_algorithm amdgpu_atombios_i2c_algo
= {
157 .master_xfer
= amdgpu_atombios_i2c_xfer
,
158 .functionality
= amdgpu_atombios_i2c_func
,
161 struct amdgpu_i2c_chan
*amdgpu_i2c_create(struct drm_device
*dev
,
162 const struct amdgpu_i2c_bus_rec
*rec
,
165 struct amdgpu_i2c_chan
*i2c
;
168 /* don't add the mm_i2c bus unless hw_i2c is enabled */
169 if (rec
->mm_i2c
&& (amdgpu_hw_i2c
== 0))
172 i2c
= kzalloc(sizeof(struct amdgpu_i2c_chan
), GFP_KERNEL
);
177 i2c
->adapter
.owner
= THIS_MODULE
;
178 i2c
->adapter
.class = I2C_CLASS_DDC
;
179 i2c
->adapter
.dev
.parent
= &dev
->pdev
->dev
;
181 i2c_set_adapdata(&i2c
->adapter
, i2c
);
182 mutex_init(&i2c
->mutex
);
183 if (rec
->hw_capable
&&
185 /* hw i2c using atom */
186 snprintf(i2c
->adapter
.name
, sizeof(i2c
->adapter
.name
),
187 "AMDGPU i2c hw bus %s", name
);
188 i2c
->adapter
.algo
= &amdgpu_atombios_i2c_algo
;
189 ret
= i2c_add_adapter(&i2c
->adapter
);
193 /* set the amdgpu bit adapter */
194 snprintf(i2c
->adapter
.name
, sizeof(i2c
->adapter
.name
),
195 "AMDGPU i2c bit bus %s", name
);
196 i2c
->adapter
.algo_data
= &i2c
->bit
;
197 i2c
->bit
.pre_xfer
= amdgpu_i2c_pre_xfer
;
198 i2c
->bit
.post_xfer
= amdgpu_i2c_post_xfer
;
199 i2c
->bit
.setsda
= amdgpu_i2c_set_data
;
200 i2c
->bit
.setscl
= amdgpu_i2c_set_clock
;
201 i2c
->bit
.getsda
= amdgpu_i2c_get_data
;
202 i2c
->bit
.getscl
= amdgpu_i2c_get_clock
;
203 i2c
->bit
.udelay
= 10;
204 i2c
->bit
.timeout
= usecs_to_jiffies(2200); /* from VESA */
206 ret
= i2c_bit_add_bus(&i2c
->adapter
);
208 DRM_ERROR("Failed to register bit i2c %s\n", name
);
220 void amdgpu_i2c_destroy(struct amdgpu_i2c_chan
*i2c
)
224 WARN_ON(i2c
->has_aux
);
225 i2c_del_adapter(&i2c
->adapter
);
229 /* Add the default buses */
230 void amdgpu_i2c_init(struct amdgpu_device
*adev
)
233 DRM_INFO("hw_i2c forced on, you may experience display detection problems!\n");
235 amdgpu_atombios_i2c_init(adev
);
238 /* remove all the buses */
239 void amdgpu_i2c_fini(struct amdgpu_device
*adev
)
243 for (i
= 0; i
< AMDGPU_MAX_I2C_BUS
; i
++) {
244 if (adev
->i2c_bus
[i
]) {
245 amdgpu_i2c_destroy(adev
->i2c_bus
[i
]);
246 adev
->i2c_bus
[i
] = NULL
;
251 /* Add additional buses */
252 void amdgpu_i2c_add(struct amdgpu_device
*adev
,
253 const struct amdgpu_i2c_bus_rec
*rec
,
256 struct drm_device
*dev
= adev
->ddev
;
259 for (i
= 0; i
< AMDGPU_MAX_I2C_BUS
; i
++) {
260 if (!adev
->i2c_bus
[i
]) {
261 adev
->i2c_bus
[i
] = amdgpu_i2c_create(dev
, rec
, name
);
267 /* looks up bus based on id */
268 struct amdgpu_i2c_chan
*
269 amdgpu_i2c_lookup(struct amdgpu_device
*adev
,
270 const struct amdgpu_i2c_bus_rec
*i2c_bus
)
274 for (i
= 0; i
< AMDGPU_MAX_I2C_BUS
; i
++) {
275 if (adev
->i2c_bus
[i
] &&
276 (adev
->i2c_bus
[i
]->rec
.i2c_id
== i2c_bus
->i2c_id
)) {
277 return adev
->i2c_bus
[i
];
283 static void amdgpu_i2c_get_byte(struct amdgpu_i2c_chan
*i2c_bus
,
290 struct i2c_msg msgs
[] = {
308 if (i2c_transfer(&i2c_bus
->adapter
, msgs
, 2) == 2) {
310 DRM_DEBUG("val = 0x%02x\n", *val
);
312 DRM_DEBUG("i2c 0x%02x 0x%02x read failed\n",
317 static void amdgpu_i2c_put_byte(struct amdgpu_i2c_chan
*i2c_bus
,
323 struct i2c_msg msg
= {
333 if (i2c_transfer(&i2c_bus
->adapter
, &msg
, 1) != 1)
334 DRM_DEBUG("i2c 0x%02x 0x%02x write failed\n",
338 /* ddc router switching */
340 amdgpu_i2c_router_select_ddc_port(const struct amdgpu_connector
*amdgpu_connector
)
344 if (!amdgpu_connector
->router
.ddc_valid
)
347 if (!amdgpu_connector
->router_bus
)
350 amdgpu_i2c_get_byte(amdgpu_connector
->router_bus
,
351 amdgpu_connector
->router
.i2c_addr
,
353 val
&= ~amdgpu_connector
->router
.ddc_mux_control_pin
;
354 amdgpu_i2c_put_byte(amdgpu_connector
->router_bus
,
355 amdgpu_connector
->router
.i2c_addr
,
357 amdgpu_i2c_get_byte(amdgpu_connector
->router_bus
,
358 amdgpu_connector
->router
.i2c_addr
,
360 val
&= ~amdgpu_connector
->router
.ddc_mux_control_pin
;
361 val
|= amdgpu_connector
->router
.ddc_mux_state
;
362 amdgpu_i2c_put_byte(amdgpu_connector
->router_bus
,
363 amdgpu_connector
->router
.i2c_addr
,
367 /* clock/data router switching */
369 amdgpu_i2c_router_select_cd_port(const struct amdgpu_connector
*amdgpu_connector
)
373 if (!amdgpu_connector
->router
.cd_valid
)
376 if (!amdgpu_connector
->router_bus
)
379 amdgpu_i2c_get_byte(amdgpu_connector
->router_bus
,
380 amdgpu_connector
->router
.i2c_addr
,
382 val
&= ~amdgpu_connector
->router
.cd_mux_control_pin
;
383 amdgpu_i2c_put_byte(amdgpu_connector
->router_bus
,
384 amdgpu_connector
->router
.i2c_addr
,
386 amdgpu_i2c_get_byte(amdgpu_connector
->router_bus
,
387 amdgpu_connector
->router
.i2c_addr
,
389 val
&= ~amdgpu_connector
->router
.cd_mux_control_pin
;
390 val
|= amdgpu_connector
->router
.cd_mux_state
;
391 amdgpu_i2c_put_byte(amdgpu_connector
->router_bus
,
392 amdgpu_connector
->router
.i2c_addr
,