130388c6eSAlex Deucher /* 230388c6eSAlex Deucher * Copyright 2011 Advanced Micro Devices, Inc. 330388c6eSAlex Deucher * 430388c6eSAlex Deucher * Permission is hereby granted, free of charge, to any person obtaining a 530388c6eSAlex Deucher * copy of this software and associated documentation files (the "Software"), 630388c6eSAlex Deucher * to deal in the Software without restriction, including without limitation 730388c6eSAlex Deucher * the rights to use, copy, modify, merge, publish, distribute, sublicense, 830388c6eSAlex Deucher * and/or sell copies of the Software, and to permit persons to whom the 930388c6eSAlex Deucher * Software is furnished to do so, subject to the following conditions: 1030388c6eSAlex Deucher * 1130388c6eSAlex Deucher * The above copyright notice and this permission notice shall be included in 1230388c6eSAlex Deucher * all copies or substantial portions of the Software. 1330388c6eSAlex Deucher * 1430388c6eSAlex Deucher * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1530388c6eSAlex Deucher * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1630388c6eSAlex Deucher * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1730388c6eSAlex Deucher * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 1830388c6eSAlex Deucher * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 1930388c6eSAlex Deucher * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 2030388c6eSAlex Deucher * OTHER DEALINGS IN THE SOFTWARE. 2130388c6eSAlex Deucher * 2230388c6eSAlex Deucher * Authors: Alex Deucher 2330388c6eSAlex Deucher * 2430388c6eSAlex Deucher */ 25760285e7SDavid Howells #include <drm/drmP.h> 26760285e7SDavid Howells #include <drm/radeon_drm.h> 2730388c6eSAlex Deucher #include "radeon.h" 2830388c6eSAlex Deucher #include "atom.h" 2930388c6eSAlex Deucher 3030388c6eSAlex Deucher #define TARGET_HW_I2C_CLOCK 50 3130388c6eSAlex Deucher 3230388c6eSAlex Deucher /* these are a limitation of ProcessI2cChannelTransaction not the hw */ 33d1e3b556SAlex Deucher #define ATOM_MAX_HW_I2C_WRITE 3 3430388c6eSAlex Deucher #define ATOM_MAX_HW_I2C_READ 255 3530388c6eSAlex Deucher 3630388c6eSAlex Deucher static int radeon_process_i2c_ch(struct radeon_i2c_chan *chan, 3730388c6eSAlex Deucher u8 slave_addr, u8 flags, 38*1f81fbc4SMathieu Malaterre u8 *buf, int num) 3930388c6eSAlex Deucher { 4030388c6eSAlex Deucher struct drm_device *dev = chan->dev; 4130388c6eSAlex Deucher struct radeon_device *rdev = dev->dev_private; 4230388c6eSAlex Deucher PROCESS_I2C_CHANNEL_TRANSACTION_PS_ALLOCATION args; 4330388c6eSAlex Deucher int index = GetIndexIntoMasterTable(COMMAND, ProcessI2cChannelTransaction); 4430388c6eSAlex Deucher unsigned char *base; 45ffd3d336SAlex Deucher u16 out = cpu_to_le16(0); 46831719d6SAlex Deucher int r = 0; 4730388c6eSAlex Deucher 4830388c6eSAlex Deucher memset(&args, 0, sizeof(args)); 4930388c6eSAlex Deucher 50831719d6SAlex Deucher mutex_lock(&chan->mutex); 511c949842SDave Airlie mutex_lock(&rdev->mode_info.atom_context->scratch_mutex); 52831719d6SAlex Deucher 5330388c6eSAlex Deucher base = (unsigned char *)rdev->mode_info.atom_context->scratch; 5430388c6eSAlex Deucher 5530388c6eSAlex Deucher if (flags & HW_I2C_WRITE) { 5630388c6eSAlex Deucher if (num > ATOM_MAX_HW_I2C_WRITE) { 57d1e3b556SAlex Deucher DRM_ERROR("hw i2c: tried to write too many bytes (%d vs 3)\n", num); 58831719d6SAlex Deucher r = -EINVAL; 59831719d6SAlex Deucher goto done; 6030388c6eSAlex Deucher } 61ffd3d336SAlex Deucher if (buf == NULL) 62ffd3d336SAlex Deucher args.ucRegIndex = 0; 63ffd3d336SAlex Deucher else 64d1e3b556SAlex Deucher args.ucRegIndex = buf[0]; 65ffd3d336SAlex Deucher if (num) 66fae009d1SJerome Glisse num--; 67ffd3d336SAlex Deucher if (num) 68fae009d1SJerome Glisse memcpy(&out, &buf[1], num); 6930388c6eSAlex Deucher args.lpI2CDataOut = cpu_to_le16(out); 7030388c6eSAlex Deucher } else { 7130388c6eSAlex Deucher if (num > ATOM_MAX_HW_I2C_READ) { 7230388c6eSAlex Deucher DRM_ERROR("hw i2c: tried to read too many bytes (%d vs 255)\n", num); 73831719d6SAlex Deucher r = -EINVAL; 74831719d6SAlex Deucher goto done; 7530388c6eSAlex Deucher } 76d1e3b556SAlex Deucher args.ucRegIndex = 0; 77d1e3b556SAlex Deucher args.lpI2CDataOut = 0; 7830388c6eSAlex Deucher } 7930388c6eSAlex Deucher 80d1e3b556SAlex Deucher args.ucFlag = flags; 8130388c6eSAlex Deucher args.ucI2CSpeed = TARGET_HW_I2C_CLOCK; 8230388c6eSAlex Deucher args.ucTransBytes = num; 8330388c6eSAlex Deucher args.ucSlaveAddr = slave_addr << 1; 8430388c6eSAlex Deucher args.ucLineNumber = chan->rec.i2c_id; 8530388c6eSAlex Deucher 861c949842SDave Airlie atom_execute_table_scratch_unlocked(rdev->mode_info.atom_context, index, (uint32_t *)&args); 8730388c6eSAlex Deucher 8830388c6eSAlex Deucher /* error */ 8930388c6eSAlex Deucher if (args.ucStatus != HW_ASSISTED_I2C_STATUS_SUCCESS) { 9030388c6eSAlex Deucher DRM_DEBUG_KMS("hw_i2c error\n"); 91831719d6SAlex Deucher r = -EIO; 92831719d6SAlex Deucher goto done; 9330388c6eSAlex Deucher } 9430388c6eSAlex Deucher 9530388c6eSAlex Deucher if (!(flags & HW_I2C_WRITE)) 964543eda5SAlex Deucher radeon_atom_copy_swap(buf, base, num, false); 9730388c6eSAlex Deucher 98831719d6SAlex Deucher done: 991c949842SDave Airlie mutex_unlock(&rdev->mode_info.atom_context->scratch_mutex); 100831719d6SAlex Deucher mutex_unlock(&chan->mutex); 101831719d6SAlex Deucher 102831719d6SAlex Deucher return r; 10330388c6eSAlex Deucher } 10430388c6eSAlex Deucher 10530388c6eSAlex Deucher int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap, 10630388c6eSAlex Deucher struct i2c_msg *msgs, int num) 10730388c6eSAlex Deucher { 10830388c6eSAlex Deucher struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap); 10930388c6eSAlex Deucher struct i2c_msg *p; 11030388c6eSAlex Deucher int i, remaining, current_count, buffer_offset, max_bytes, ret; 111ffd3d336SAlex Deucher u8 flags; 11230388c6eSAlex Deucher 11330388c6eSAlex Deucher /* check for bus probe */ 11430388c6eSAlex Deucher p = &msgs[0]; 11530388c6eSAlex Deucher if ((num == 1) && (p->len == 0)) { 11630388c6eSAlex Deucher ret = radeon_process_i2c_ch(i2c, 11730388c6eSAlex Deucher p->addr, HW_I2C_WRITE, 118ffd3d336SAlex Deucher NULL, 0); 11930388c6eSAlex Deucher if (ret) 12030388c6eSAlex Deucher return ret; 12130388c6eSAlex Deucher else 12230388c6eSAlex Deucher return num; 12330388c6eSAlex Deucher } 12430388c6eSAlex Deucher 12530388c6eSAlex Deucher for (i = 0; i < num; i++) { 12630388c6eSAlex Deucher p = &msgs[i]; 12730388c6eSAlex Deucher remaining = p->len; 12830388c6eSAlex Deucher buffer_offset = 0; 12930388c6eSAlex Deucher /* max_bytes are a limitation of ProcessI2cChannelTransaction not the hw */ 13030388c6eSAlex Deucher if (p->flags & I2C_M_RD) { 13130388c6eSAlex Deucher max_bytes = ATOM_MAX_HW_I2C_READ; 13230388c6eSAlex Deucher flags = HW_I2C_READ; 13330388c6eSAlex Deucher } else { 13430388c6eSAlex Deucher max_bytes = ATOM_MAX_HW_I2C_WRITE; 13530388c6eSAlex Deucher flags = HW_I2C_WRITE; 13630388c6eSAlex Deucher } 13730388c6eSAlex Deucher while (remaining) { 13830388c6eSAlex Deucher if (remaining > max_bytes) 13930388c6eSAlex Deucher current_count = max_bytes; 14030388c6eSAlex Deucher else 14130388c6eSAlex Deucher current_count = remaining; 14230388c6eSAlex Deucher ret = radeon_process_i2c_ch(i2c, 14330388c6eSAlex Deucher p->addr, flags, 14430388c6eSAlex Deucher &p->buf[buffer_offset], current_count); 14530388c6eSAlex Deucher if (ret) 14630388c6eSAlex Deucher return ret; 14730388c6eSAlex Deucher remaining -= current_count; 14830388c6eSAlex Deucher buffer_offset += current_count; 14930388c6eSAlex Deucher } 15030388c6eSAlex Deucher } 15130388c6eSAlex Deucher 15230388c6eSAlex Deucher return num; 15330388c6eSAlex Deucher } 15430388c6eSAlex Deucher 15530388c6eSAlex Deucher u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap) 15630388c6eSAlex Deucher { 15730388c6eSAlex Deucher return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 15830388c6eSAlex Deucher } 15930388c6eSAlex Deucher 160