1450N/A/*
1450N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
1450N/A */
1450N/A
1450N/A/*
1450N/A * Copyright (c) 2012 Intel Corporation. All rights reserved.
1450N/A */
1450N/A
1450N/A/*
1450N/A * Copyright © 2009 Keith Packard
1450N/A *
1450N/A * Permission to use, copy, modify, distribute, and sell this software and its
1450N/A * documentation for any purpose is hereby granted without fee, provided that
1450N/A * the above copyright notice appear in all copies and that both that copyright
1450N/A * notice and this permission notice appear in supporting documentation, and
1450N/A * that the name of the copyright holders not be used in advertising or
1450N/A * publicity pertaining to distribution of the software without specific,
1450N/A * written prior permission. The copyright holders make no representations
1450N/A * about the suitability of this software for any purpose. It is provided "as
1450N/A * is" without express or implied warranty.
1450N/A *
1450N/A * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1450N/A * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1450N/A * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1450N/A * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
1450N/A * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
1450N/A * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
1450N/A * OF THIS SOFTWARE.
1450N/A */
1450N/A
1450N/A#include "drm_dp_helper.h"
1450N/A#include "drmP.h"
1450N/A
1450N/A
1450N/A/* Run a single AUX_CH I2C transaction, writing/reading data as necessary */
1450N/Astatic int
1450N/Ai2c_algo_dp_aux_transaction(struct i2c_adapter *adapter, int mode,
1450N/A uint8_t write_byte, uint8_t *read_byte)
1450N/A{
1450N/A struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
1450N/A int ret;
1450N/A
1450N/A ret = (*algo_data->aux_ch)(adapter, mode,
1450N/A write_byte, read_byte);
1450N/A return ret;
1450N/A}
1450N/A
1450N/A/*
1450N/A * I2C over AUX CH
1450N/A */
1450N/A
1450N/A/*
1450N/A * Send the address. If the I2C link is running, this 'restarts'
1450N/A * the connection with the new address, this is used for doing
1450N/A * a write followed by a read (as needed for DDC)
1450N/A */
1450N/Astatic int
1450N/Ai2c_algo_dp_aux_address(struct i2c_adapter *adapter, u16 address, bool reading)
1450N/A{
1450N/A struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
1450N/A int mode = MODE_I2C_START;
1450N/A int ret;
1450N/A
1450N/A if (reading)
1450N/A mode |= MODE_I2C_READ;
1450N/A else
1450N/A mode |= MODE_I2C_WRITE;
1450N/A algo_data->address = address;
1450N/A algo_data->running = true;
1450N/A ret = i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
1450N/A return ret;
1450N/A}
1450N/A
1450N/A/*
1450N/A * Stop the I2C transaction. This closes out the link, sending
1450N/A * a bare address packet with the MOT bit turned off
1450N/A */
1450N/Astatic void
1450N/Ai2c_algo_dp_aux_stop(struct i2c_adapter *adapter, bool reading)
1450N/A{
1450N/A struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
1450N/A int mode = MODE_I2C_STOP;
1450N/A
1450N/A if (reading)
1450N/A mode |= MODE_I2C_READ;
1450N/A else
1450N/A mode |= MODE_I2C_WRITE;
1450N/A if (algo_data->running) {
1450N/A (void) i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
1450N/A algo_data->running = false;
1450N/A }
1450N/A}
1450N/A
1450N/A/*
1450N/A * Write a single byte to the current I2C address, the
1450N/A * the I2C link must be running or this returns -EIO
1450N/A */
1450N/Astatic int
1450N/Ai2c_algo_dp_aux_put_byte(struct i2c_adapter *adapter, u8 byte)
1450N/A{
1450N/A struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
1450N/A int ret;
1450N/A
1450N/A if (!algo_data->running)
1450N/A return -EIO;
1450N/A
1450N/A ret = i2c_algo_dp_aux_transaction(adapter, MODE_I2C_WRITE, byte, NULL);
1450N/A return ret;
1450N/A}
1450N/A
1450N/A/*
1450N/A * Read a single byte from the current I2C address, the
1450N/A * I2C link must be running or this returns -EIO
1450N/A */
1450N/Astatic int
1450N/Ai2c_algo_dp_aux_get_byte(struct i2c_adapter *adapter, u8 *byte_ret)
1450N/A{
1450N/A struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
1450N/A int ret;
1450N/A
1450N/A if (!algo_data->running)
1450N/A return -EIO;
1450N/A
1450N/A ret = i2c_algo_dp_aux_transaction(adapter, MODE_I2C_READ, 0, byte_ret);
1450N/A return ret;
1450N/A}
1450N/A
1450N/Astatic int
1450N/Ai2c_algo_dp_aux_xfer(struct i2c_adapter *adapter,
1450N/A struct i2c_msg *msgs,
1450N/A int num)
1450N/A{
1450N/A int ret = 0;
1450N/A bool reading = false;
1450N/A int m;
1450N/A int b;
1450N/A
1450N/A for (m = 0; m < num; m++) {
1450N/A u16 len = msgs[m].len;
1450N/A u8 *buf = msgs[m].buf;
1450N/A reading = (msgs[m].flags & I2C_M_RD) != 0;
1450N/A ret = i2c_algo_dp_aux_address(adapter, msgs[m].addr, reading);
1450N/A if (ret < 0)
1450N/A break;
1450N/A if (reading) {
1450N/A for (b = 0; b < len; b++) {
1450N/A ret = i2c_algo_dp_aux_get_byte(adapter, &buf[b]);
1450N/A if (ret < 0)
1450N/A break;
1450N/A }
1450N/A } else {
1450N/A for (b = 0; b < len; b++) {
1450N/A ret = i2c_algo_dp_aux_put_byte(adapter, buf[b]);
1450N/A if (ret < 0)
1450N/A break;
1450N/A }
1450N/A }
1450N/A if (ret < 0)
1450N/A break;
1450N/A }
1450N/A if (ret >= 0)
1450N/A ret = num;
1450N/A i2c_algo_dp_aux_stop(adapter, reading);
1450N/A DRM_DEBUG_KMS("dp_aux_xfer return %d\n", ret);
1450N/A return ret;
1450N/A}
1450N/A
1450N/Astatic u32
1450N/A/* LINTED E_FUNC_ARG_UNUSED */
1450N/Ai2c_algo_dp_aux_functionality(struct i2c_adapter *adapter)
1450N/A{
1450N/A return 0;
1450N/A}
1450N/A
1450N/Astatic struct i2c_algorithm i2c_dp_aux_algo = {
1450N/A .master_xfer = i2c_algo_dp_aux_xfer,
1450N/A .functionality = i2c_algo_dp_aux_functionality,
1450N/A};
1450N/A
1450N/Astatic void
1450N/Ai2c_dp_aux_reset_bus(struct i2c_adapter *adapter)
1450N/A{
1450N/A (void) i2c_algo_dp_aux_address(adapter, 0, false);
1450N/A (void) i2c_algo_dp_aux_stop(adapter, false);
1450N/A
1450N/A}
1450N/A
1450N/Astatic int
1450N/Ai2c_dp_aux_prepare_bus(struct i2c_adapter *adapter)
1450N/A{
1450N/A adapter->algo = &i2c_dp_aux_algo;
1450N/A adapter->retries = 3;
1450N/A i2c_dp_aux_reset_bus(adapter);
1450N/A return 0;
1450N/A}
1450N/A
1450N/Aint
1450N/Ai2c_dp_aux_add_bus(struct i2c_adapter *adapter)
1450N/A{
1450N/A int error;
1450N/A
1450N/A error = i2c_dp_aux_prepare_bus(adapter);
1450N/A if (error)
1450N/A return error;
1450N/A// error = i2c_add_adapter(adapter);
1450N/A return error;
1450N/A}