2 * Copyright © 2009 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
26 #include <sys/types.h>
29 #include <dev/iicbus/iic.h>
30 #include "iicbus_if.h"
31 #include <dev/iicbus/iiconf.h>
32 #include <dev/drm2/drmP.h>
33 #include <dev/drm2/drm_dp_helper.h>
36 iic_dp_aux_transaction(device_t idev, int mode, uint8_t write_byte,
39 struct iic_dp_aux_data *aux_data;
42 aux_data = device_get_softc(idev);
43 ret = (*aux_data->aux_ch)(idev, mode, write_byte, read_byte);
52 * Send the address. If the I2C link is running, this 'restarts'
53 * the connection with the new address, this is used for doing
54 * a write followed by a read (as needed for DDC)
57 iic_dp_aux_address(device_t idev, u16 address, bool reading)
59 struct iic_dp_aux_data *aux_data;
62 aux_data = device_get_softc(idev);
63 mode = MODE_I2C_START;
65 mode |= MODE_I2C_READ;
67 mode |= MODE_I2C_WRITE;
68 aux_data->address = address;
69 aux_data->running = true;
70 ret = iic_dp_aux_transaction(idev, mode, 0, NULL);
75 * Stop the I2C transaction. This closes out the link, sending
76 * a bare address packet with the MOT bit turned off
79 iic_dp_aux_stop(device_t idev, bool reading)
81 struct iic_dp_aux_data *aux_data;
84 aux_data = device_get_softc(idev);
87 mode |= MODE_I2C_READ;
89 mode |= MODE_I2C_WRITE;
90 if (aux_data->running) {
91 (void)iic_dp_aux_transaction(idev, mode, 0, NULL);
92 aux_data->running = false;
97 * Write a single byte to the current I2C address, the
98 * the I2C link must be running or this returns -EIO
101 iic_dp_aux_put_byte(device_t idev, u8 byte)
103 struct iic_dp_aux_data *aux_data;
106 aux_data = device_get_softc(idev);
108 if (!aux_data->running)
111 ret = iic_dp_aux_transaction(idev, MODE_I2C_WRITE, byte, NULL);
116 * Read a single byte from the current I2C address, the
117 * I2C link must be running or this returns -EIO
120 iic_dp_aux_get_byte(device_t idev, u8 *byte_ret)
122 struct iic_dp_aux_data *aux_data;
125 aux_data = device_get_softc(idev);
127 if (!aux_data->running)
130 ret = iic_dp_aux_transaction(idev, MODE_I2C_READ, 0, byte_ret);
135 iic_dp_aux_xfer(device_t idev, struct iic_msg *msgs, uint32_t num)
145 for (m = 0; m < num; m++) {
148 reading = (msgs[m].flags & IIC_M_RD) != 0;
149 ret = iic_dp_aux_address(idev, msgs[m].slave >> 1, reading);
153 for (b = 0; b < len; b++) {
154 ret = iic_dp_aux_get_byte(idev, &buf[b]);
159 for (b = 0; b < len; b++) {
160 ret = iic_dp_aux_put_byte(idev, buf[b]);
168 iic_dp_aux_stop(idev, reading);
169 DRM_DEBUG_KMS("dp_aux_xfer return %d\n", ret);
174 iic_dp_aux_reset_bus(device_t idev)
177 (void)iic_dp_aux_address(idev, 0, false);
178 (void)iic_dp_aux_stop(idev, false);
182 iic_dp_aux_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr)
185 iic_dp_aux_reset_bus(idev);
190 iic_dp_aux_prepare_bus(device_t idev)
193 /* adapter->retries = 3; */
194 iic_dp_aux_reset_bus(idev);
199 iic_dp_aux_probe(device_t idev)
202 return (BUS_PROBE_DEFAULT);
206 iic_dp_aux_attach(device_t idev)
208 struct iic_dp_aux_data *aux_data;
210 aux_data = device_get_softc(idev);
211 aux_data->port = device_add_child(idev, "iicbus", -1);
212 if (aux_data->port == NULL)
214 device_quiet(aux_data->port);
215 bus_generic_attach(idev);
220 iic_dp_aux_add_bus(device_t dev, const char *name,
221 int (*ch)(device_t idev, int mode, uint8_t write_byte, uint8_t *read_byte),
222 void *priv, device_t *bus, device_t *adapter)
225 struct iic_dp_aux_data *data;
227 static int dp_bus_counter;
231 idx = atomic_fetchadd_int(&dp_bus_counter, 1);
232 ibus = device_add_child(dev, "drm_iic_dp_aux", idx);
235 DRM_ERROR("drm_iic_dp_aux bus %d creation error\n", idx);
239 error = device_probe_and_attach(ibus);
241 device_delete_child(dev, ibus);
243 DRM_ERROR("drm_iic_dp_aux bus %d attach failed, %d\n",
247 data = device_get_softc(ibus);
248 data->running = false;
252 error = iic_dp_aux_prepare_bus(ibus);
255 *adapter = data->port;
261 static device_method_t drm_iic_dp_aux_methods[] = {
262 DEVMETHOD(device_probe, iic_dp_aux_probe),
263 DEVMETHOD(device_attach, iic_dp_aux_attach),
264 DEVMETHOD(device_detach, bus_generic_detach),
265 DEVMETHOD(iicbus_reset, iic_dp_aux_reset),
266 DEVMETHOD(iicbus_transfer, iic_dp_aux_xfer),
269 static driver_t drm_iic_dp_aux_driver = {
271 drm_iic_dp_aux_methods,
272 sizeof(struct iic_dp_aux_data)
274 static devclass_t drm_iic_dp_aux_devclass;
275 DRIVER_MODULE_ORDERED(drm_iic_dp_aux, drmn, drm_iic_dp_aux_driver,
276 drm_iic_dp_aux_devclass, 0, 0, SI_ORDER_SECOND);