atombios_i2c.c revision 1.10
1272343Sngie/* 2272343Sngie * Copyright 2011 Advanced Micro Devices, Inc. 3272343Sngie * 4272343Sngie * Permission is hereby granted, free of charge, to any person obtaining a 5272343Sngie * copy of this software and associated documentation files (the "Software"), 6272343Sngie * to deal in the Software without restriction, including without limitation 7272343Sngie * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8272343Sngie * and/or sell copies of the Software, and to permit persons to whom the 9272343Sngie * Software is furnished to do so, subject to the following conditions: 10272343Sngie * 11272343Sngie * The above copyright notice and this permission notice shall be included in 12272343Sngie * all copies or substantial portions of the Software. 13272343Sngie * 14272343Sngie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15272343Sngie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16272343Sngie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17272343Sngie * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18272343Sngie * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19272343Sngie * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20272343Sngie * OTHER DEALINGS IN THE SOFTWARE. 21272343Sngie * 22272343Sngie * Authors: Alex Deucher 23272343Sngie * 24272343Sngie */ 25272343Sngie#include <dev/pci/drm/drmP.h> 26272343Sngie#include <dev/pci/drm/radeon_drm.h> 27272343Sngie#include "radeon.h" 28272343Sngie#include "atom.h" 29272343Sngie 30272343Sngie#define TARGET_HW_I2C_CLOCK 50 31272343Sngie 32272343Sngie/* these are a limitation of ProcessI2cChannelTransaction not the hw */ 33272343Sngie#define ATOM_MAX_HW_I2C_WRITE 3 34272343Sngie#define ATOM_MAX_HW_I2C_READ 255 35272343Sngie 36272343Sngiestatic int radeon_process_i2c_ch(struct radeon_i2c_chan *chan, 37272343Sngie u8 slave_addr, u8 flags, 38272343Sngie u8 *buf, u8 num) 39272343Sngie{ 40272343Sngie struct drm_device *dev = chan->dev; 41272343Sngie struct radeon_device *rdev = dev->dev_private; 42272343Sngie PROCESS_I2C_CHANNEL_TRANSACTION_PS_ALLOCATION args; 43272343Sngie int index = GetIndexIntoMasterTable(COMMAND, ProcessI2cChannelTransaction); 44272343Sngie unsigned char *base; 45272343Sngie u16 out = cpu_to_le16(0); 46272343Sngie int r = 0; 47272343Sngie 48272343Sngie memset(&args, 0, sizeof(args)); 49272343Sngie 50272343Sngie mutex_lock(&chan->mutex); 51272343Sngie mutex_lock(&rdev->mode_info.atom_context->scratch_mutex); 52272343Sngie 53272343Sngie base = (unsigned char *)rdev->mode_info.atom_context->scratch; 54272343Sngie 55272343Sngie if (flags & HW_I2C_WRITE) { 56272343Sngie if (num > ATOM_MAX_HW_I2C_WRITE) { 57272343Sngie DRM_ERROR("hw i2c: tried to write too many bytes (%d vs 3)\n", num); 58272343Sngie r = -EINVAL; 59272343Sngie goto done; 60272343Sngie } 61272343Sngie if (buf == NULL) 62272343Sngie args.ucRegIndex = 0; 63272343Sngie else 64272343Sngie args.ucRegIndex = buf[0]; 65272343Sngie if (num) 66272343Sngie num--; 67272343Sngie if (num) 68272343Sngie memcpy(&out, &buf[1], num); 69272343Sngie args.lpI2CDataOut = cpu_to_le16(out); 70272343Sngie } else { 71272343Sngie#if 0 72272343Sngie /* 73272343Sngie * gcc 4.2 gives 'warning: comparison is always false 74272343Sngie * due to limited range of data type' 75272343Sngie */ 76272343Sngie if (num > ATOM_MAX_HW_I2C_READ) { 77272343Sngie DRM_ERROR("hw i2c: tried to read too many bytes (%d vs 255)\n", num); 78272343Sngie r = -EINVAL; 79272343Sngie goto done; 80272343Sngie } 81272343Sngie#endif 82272343Sngie args.ucRegIndex = 0; 83272343Sngie args.lpI2CDataOut = 0; 84272343Sngie } 85272343Sngie 86272343Sngie args.ucFlag = flags; 87272343Sngie args.ucI2CSpeed = TARGET_HW_I2C_CLOCK; 88272343Sngie args.ucTransBytes = num; 89272343Sngie args.ucSlaveAddr = slave_addr << 1; 90272343Sngie args.ucLineNumber = chan->rec.i2c_id; 91272343Sngie 92272343Sngie atom_execute_table_scratch_unlocked(rdev->mode_info.atom_context, index, (uint32_t *)&args); 93272343Sngie 94272343Sngie /* error */ 95272343Sngie if (args.ucStatus != HW_ASSISTED_I2C_STATUS_SUCCESS) { 96272343Sngie DRM_DEBUG_KMS("hw_i2c error\n"); 97 r = -EIO; 98 goto done; 99 } 100 101 if (!(flags & HW_I2C_WRITE)) 102 radeon_atom_copy_swap(buf, base, num, false); 103 104done: 105 mutex_unlock(&rdev->mode_info.atom_context->scratch_mutex); 106 mutex_unlock(&chan->mutex); 107 108 return r; 109} 110 111int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap, 112 struct i2c_msg *msgs, int num) 113{ 114 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap); 115 struct i2c_msg *p; 116 int i, remaining, current_count, buffer_offset, max_bytes, ret; 117 u8 flags; 118 119 /* check for bus probe */ 120 p = &msgs[0]; 121 if ((num == 1) && (p->len == 0)) { 122 ret = radeon_process_i2c_ch(i2c, 123 p->addr, HW_I2C_WRITE, 124 NULL, 0); 125 if (ret) 126 return ret; 127 else 128 return num; 129 } 130 131 for (i = 0; i < num; i++) { 132 p = &msgs[i]; 133 remaining = p->len; 134 buffer_offset = 0; 135 /* max_bytes are a limitation of ProcessI2cChannelTransaction not the hw */ 136 if (p->flags & I2C_M_RD) { 137 max_bytes = ATOM_MAX_HW_I2C_READ; 138 flags = HW_I2C_READ; 139 } else { 140 max_bytes = ATOM_MAX_HW_I2C_WRITE; 141 flags = HW_I2C_WRITE; 142 } 143 while (remaining) { 144 if (remaining > max_bytes) 145 current_count = max_bytes; 146 else 147 current_count = remaining; 148 ret = radeon_process_i2c_ch(i2c, 149 p->addr, flags, 150 &p->buf[buffer_offset], current_count); 151 if (ret) 152 return ret; 153 remaining -= current_count; 154 buffer_offset += current_count; 155 } 156 } 157 158 return num; 159} 160 161u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap) 162{ 163 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 164} 165 166