1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* Copyright © 2009 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/kobj.h>
#include <sys/bus.h>
#include <dev/iicbus/iic.h>
#include "iicbus_if.h"
#include <dev/iicbus/iiconf.h>
#include <dev/drm2/drmP.h>
#include <dev/drm2/drm_dp_helper.h>
static int
iic_dp_aux_transaction(device_t idev, int mode, uint8_t write_byte,
uint8_t *read_byte)
{
struct iic_dp_aux_data *aux_data;
int ret;
aux_data = device_get_softc(idev);
ret = (*aux_data->aux_ch)(idev, mode, write_byte, read_byte);
if (ret < 0)
return (ret);
return (0);
}
/*
* I2C over AUX CH
*/
/*
* Send the address. If the I2C link is running, this 'restarts'
* the connection with the new address, this is used for doing
* a write followed by a read (as needed for DDC)
*/
static int
iic_dp_aux_address(device_t idev, u16 address, bool reading)
{
struct iic_dp_aux_data *aux_data;
int mode, ret;
aux_data = device_get_softc(idev);
mode = MODE_I2C_START;
if (reading)
mode |= MODE_I2C_READ;
else
mode |= MODE_I2C_WRITE;
aux_data->address = address;
aux_data->running = true;
ret = iic_dp_aux_transaction(idev, mode, 0, NULL);
return (ret);
}
/*
* Stop the I2C transaction. This closes out the link, sending
* a bare address packet with the MOT bit turned off
*/
static void
iic_dp_aux_stop(device_t idev, bool reading)
{
struct iic_dp_aux_data *aux_data;
int mode;
aux_data = device_get_softc(idev);
mode = MODE_I2C_STOP;
if (reading)
mode |= MODE_I2C_READ;
else
mode |= MODE_I2C_WRITE;
if (aux_data->running) {
(void)iic_dp_aux_transaction(idev, mode, 0, NULL);
aux_data->running = false;
}
}
/*
* Write a single byte to the current I2C address, the
* the I2C link must be running or this returns -EIO
*/
static int
iic_dp_aux_put_byte(device_t idev, u8 byte)
{
struct iic_dp_aux_data *aux_data;
int ret;
aux_data = device_get_softc(idev);
if (!aux_data->running)
return (-EIO);
ret = iic_dp_aux_transaction(idev, MODE_I2C_WRITE, byte, NULL);
return (ret);
}
/*
* Read a single byte from the current I2C address, the
* I2C link must be running or this returns -EIO
*/
static int
iic_dp_aux_get_byte(device_t idev, u8 *byte_ret)
{
struct iic_dp_aux_data *aux_data;
int ret;
aux_data = device_get_softc(idev);
if (!aux_data->running)
return (-EIO);
ret = iic_dp_aux_transaction(idev, MODE_I2C_READ, 0, byte_ret);
return (ret);
}
static int
iic_dp_aux_xfer(device_t idev, struct iic_msg *msgs, uint32_t num)
{
u8 *buf;
int b, m, ret;
u16 len;
bool reading;
ret = 0;
reading = false;
for (m = 0; m < num; m++) {
len = msgs[m].len;
buf = msgs[m].buf;
reading = (msgs[m].flags & IIC_M_RD) != 0;
ret = iic_dp_aux_address(idev, msgs[m].slave >> 1, reading);
if (ret < 0)
break;
if (reading) {
for (b = 0; b < len; b++) {
ret = iic_dp_aux_get_byte(idev, &buf[b]);
if (ret != 0)
break;
}
} else {
for (b = 0; b < len; b++) {
ret = iic_dp_aux_put_byte(idev, buf[b]);
if (ret < 0)
break;
}
}
if (ret != 0)
break;
}
iic_dp_aux_stop(idev, reading);
DRM_DEBUG_KMS("dp_aux_xfer return %d\n", ret);
return (-ret);
}
static void
iic_dp_aux_reset_bus(device_t idev)
{
(void)iic_dp_aux_address(idev, 0, false);
(void)iic_dp_aux_stop(idev, false);
}
static int
iic_dp_aux_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr)
{
iic_dp_aux_reset_bus(idev);
return (0);
}
static int
iic_dp_aux_prepare_bus(device_t idev)
{
/* adapter->retries = 3; */
iic_dp_aux_reset_bus(idev);
return (0);
}
static int
iic_dp_aux_probe(device_t idev)
{
return (BUS_PROBE_DEFAULT);
}
static int
iic_dp_aux_attach(device_t idev)
{
struct iic_dp_aux_data *aux_data;
aux_data = device_get_softc(idev);
aux_data->port = device_add_child(idev, "iicbus", -1);
if (aux_data->port == NULL)
return (ENXIO);
device_quiet(aux_data->port);
bus_generic_attach(idev);
return (0);
}
int
iic_dp_aux_add_bus(device_t dev, const char *name,
int (*ch)(device_t idev, int mode, uint8_t write_byte, uint8_t *read_byte),
void *priv, device_t *bus, device_t *adapter)
{
device_t ibus;
struct iic_dp_aux_data *data;
int idx, error;
static int dp_bus_counter;
mtx_lock(&Giant);
idx = atomic_fetchadd_int(&dp_bus_counter, 1);
ibus = device_add_child(dev, "drm_iic_dp_aux", idx);
if (ibus == NULL) {
mtx_unlock(&Giant);
DRM_ERROR("drm_iic_dp_aux bus %d creation error\n", idx);
return (-ENXIO);
}
device_quiet(ibus);
error = device_probe_and_attach(ibus);
if (error != 0) {
device_delete_child(dev, ibus);
mtx_unlock(&Giant);
DRM_ERROR("drm_iic_dp_aux bus %d attach failed, %d\n",
idx, error);
return (-error);
}
data = device_get_softc(ibus);
data->running = false;
data->address = 0;
data->aux_ch = ch;
data->priv = priv;
error = iic_dp_aux_prepare_bus(ibus);
if (error == 0) {
*bus = ibus;
*adapter = data->port;
}
mtx_unlock(&Giant);
return (-error);
}
static device_method_t drm_iic_dp_aux_methods[] = {
DEVMETHOD(device_probe, iic_dp_aux_probe),
DEVMETHOD(device_attach, iic_dp_aux_attach),
DEVMETHOD(device_detach, bus_generic_detach),
DEVMETHOD(iicbus_reset, iic_dp_aux_reset),
DEVMETHOD(iicbus_transfer, iic_dp_aux_xfer),
DEVMETHOD_END
};
static driver_t drm_iic_dp_aux_driver = {
"drm_iic_dp_aux",
drm_iic_dp_aux_methods,
sizeof(struct iic_dp_aux_data)
};
static devclass_t drm_iic_dp_aux_devclass;
DRIVER_MODULE_ORDERED(drm_iic_dp_aux, drmn, drm_iic_dp_aux_driver,
drm_iic_dp_aux_devclass, 0, 0, SI_ORDER_SECOND);
|