1254885Sdumbbell/* 2254885Sdumbbell * Copyright 2011 Advanced Micro Devices, Inc. 3254885Sdumbbell * 4254885Sdumbbell * Permission is hereby granted, free of charge, to any person obtaining a 5254885Sdumbbell * copy of this software and associated documentation files (the "Software"), 6254885Sdumbbell * to deal in the Software without restriction, including without limitation 7254885Sdumbbell * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8254885Sdumbbell * and/or sell copies of the Software, and to permit persons to whom the 9254885Sdumbbell * Software is furnished to do so, subject to the following conditions: 10254885Sdumbbell * 11254885Sdumbbell * The above copyright notice and this permission notice shall be included in 12254885Sdumbbell * all copies or substantial portions of the Software. 13254885Sdumbbell * 14254885Sdumbbell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15254885Sdumbbell * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16254885Sdumbbell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17254885Sdumbbell * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18254885Sdumbbell * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19254885Sdumbbell * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20254885Sdumbbell * OTHER DEALINGS IN THE SOFTWARE. 21254885Sdumbbell * 22254885Sdumbbell * Authors: Alex Deucher 23254885Sdumbbell * 24254885Sdumbbell */ 25254885Sdumbbell 26254885Sdumbbell#include <sys/cdefs.h> 27254885Sdumbbell__FBSDID("$FreeBSD$"); 28254885Sdumbbell 29254885Sdumbbell#include <dev/drm2/drmP.h> 30254885Sdumbbell#include <dev/drm2/radeon/radeon_drm.h> 31268564Srpaulo#include <dev/iicbus/iic.h> 32254885Sdumbbell#include <dev/iicbus/iiconf.h> 33254885Sdumbbell#include <dev/iicbus/iicbus.h> 34254885Sdumbbell#include "radeon.h" 35254885Sdumbbell#include "atom.h" 36254885Sdumbbell#include "iicbus_if.h" 37254885Sdumbbell#include "iicbb_if.h" 38254885Sdumbbell 39254885Sdumbbell#define TARGET_HW_I2C_CLOCK 50 40254885Sdumbbell 41254885Sdumbbell/* these are a limitation of ProcessI2cChannelTransaction not the hw */ 42254885Sdumbbell#define ATOM_MAX_HW_I2C_WRITE 2 43254885Sdumbbell#define ATOM_MAX_HW_I2C_READ 255 44254885Sdumbbell 45254885Sdumbbellstatic int radeon_process_i2c_ch(struct radeon_i2c_chan *chan, 46254885Sdumbbell u8 slave_addr, u8 flags, 47254885Sdumbbell u8 *buf, u8 num) 48254885Sdumbbell{ 49254885Sdumbbell struct drm_device *dev = chan->dev; 50254885Sdumbbell struct radeon_device *rdev = dev->dev_private; 51254885Sdumbbell PROCESS_I2C_CHANNEL_TRANSACTION_PS_ALLOCATION args; 52254885Sdumbbell int index = GetIndexIntoMasterTable(COMMAND, ProcessI2cChannelTransaction); 53254885Sdumbbell unsigned char *base; 54254885Sdumbbell u16 out; 55254885Sdumbbell 56254885Sdumbbell memset(&args, 0, sizeof(args)); 57254885Sdumbbell 58254885Sdumbbell base = (unsigned char *)rdev->mode_info.atom_context->scratch; 59254885Sdumbbell 60254885Sdumbbell if (flags & HW_I2C_WRITE) { 61254885Sdumbbell if (num > ATOM_MAX_HW_I2C_WRITE) { 62254885Sdumbbell DRM_ERROR("hw i2c: tried to write too many bytes (%d vs 2)\n", num); 63280183Sdumbbell return -EINVAL; 64254885Sdumbbell } 65254885Sdumbbell memcpy(&out, buf, num); 66254885Sdumbbell args.lpI2CDataOut = cpu_to_le16(out); 67280183Sdumbbell } else { 68280183Sdumbbell#ifdef FREEBSD_WIP /* FreeBSD: to please GCC 4.2. */ 69280183Sdumbbell if (num > ATOM_MAX_HW_I2C_READ) { 70280183Sdumbbell DRM_ERROR("hw i2c: tried to read too many bytes (%d vs 255)\n", num); 71280183Sdumbbell return -EINVAL; 72280183Sdumbbell } 73280183Sdumbbell#endif 74254885Sdumbbell } 75254885Sdumbbell 76254885Sdumbbell args.ucI2CSpeed = TARGET_HW_I2C_CLOCK; 77254885Sdumbbell args.ucRegIndex = 0; 78254885Sdumbbell args.ucTransBytes = num; 79254885Sdumbbell args.ucSlaveAddr = slave_addr << 1; 80254885Sdumbbell args.ucLineNumber = chan->rec.i2c_id; 81254885Sdumbbell 82254885Sdumbbell atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args); 83254885Sdumbbell 84254885Sdumbbell /* error */ 85254885Sdumbbell if (args.ucStatus != HW_ASSISTED_I2C_STATUS_SUCCESS) { 86254885Sdumbbell DRM_DEBUG_KMS("hw_i2c error\n"); 87280183Sdumbbell return -EIO; 88254885Sdumbbell } 89254885Sdumbbell 90254885Sdumbbell if (!(flags & HW_I2C_WRITE)) 91254885Sdumbbell memcpy(buf, base, num); 92254885Sdumbbell 93254885Sdumbbell return 0; 94254885Sdumbbell} 95254885Sdumbbell 96254885Sdumbbellstatic int 97254885Sdumbbellradeon_atom_hw_i2c_xfer(device_t dev, struct iic_msg *msgs, u_int num) 98254885Sdumbbell{ 99254885Sdumbbell struct radeon_i2c_chan *i2c = device_get_softc(dev); 100254885Sdumbbell struct iic_msg *p; 101254885Sdumbbell int i, remaining, current_count, buffer_offset, max_bytes, ret; 102254885Sdumbbell u8 buf = 0, flags; 103254885Sdumbbell 104254885Sdumbbell /* check for bus probe */ 105254885Sdumbbell p = &msgs[0]; 106254885Sdumbbell if ((num == 1) && (p->len == 0)) { 107254885Sdumbbell ret = radeon_process_i2c_ch(i2c, 108254885Sdumbbell p->slave, HW_I2C_WRITE, 109254885Sdumbbell &buf, 1); 110254885Sdumbbell if (ret) 111280183Sdumbbell return -ret; /* "ret" is returned on Linux. */ 112254885Sdumbbell else 113280183Sdumbbell return (0); /* "num" is returned on Linux. */ 114254885Sdumbbell } 115254885Sdumbbell 116254885Sdumbbell for (i = 0; i < num; i++) { 117254885Sdumbbell p = &msgs[i]; 118254885Sdumbbell remaining = p->len; 119254885Sdumbbell buffer_offset = 0; 120254885Sdumbbell /* max_bytes are a limitation of ProcessI2cChannelTransaction not the hw */ 121254885Sdumbbell if (p->flags & IIC_M_RD) { 122254885Sdumbbell max_bytes = ATOM_MAX_HW_I2C_READ; 123254885Sdumbbell flags = HW_I2C_READ; 124254885Sdumbbell } else { 125254885Sdumbbell max_bytes = ATOM_MAX_HW_I2C_WRITE; 126254885Sdumbbell flags = HW_I2C_WRITE; 127254885Sdumbbell } 128254885Sdumbbell while (remaining) { 129254885Sdumbbell if (remaining > max_bytes) 130254885Sdumbbell current_count = max_bytes; 131254885Sdumbbell else 132254885Sdumbbell current_count = remaining; 133254885Sdumbbell ret = radeon_process_i2c_ch(i2c, 134254885Sdumbbell p->slave, flags, 135254885Sdumbbell &p->buf[buffer_offset], current_count); 136254885Sdumbbell if (ret) 137280183Sdumbbell return -ret; /* "ret" is returned on Linux. */ 138254885Sdumbbell remaining -= current_count; 139254885Sdumbbell buffer_offset += current_count; 140254885Sdumbbell } 141254885Sdumbbell } 142254885Sdumbbell 143280183Sdumbbell return (0); /* "num" is returned on Linux. */ 144254885Sdumbbell} 145254885Sdumbbell 146254885Sdumbbellstatic int 147254885Sdumbbellradeon_atom_hw_i2c_probe(device_t dev) 148254885Sdumbbell{ 149254885Sdumbbell 150254885Sdumbbell return (BUS_PROBE_SPECIFIC); 151254885Sdumbbell} 152254885Sdumbbell 153254885Sdumbbellstatic int 154254885Sdumbbellradeon_atom_hw_i2c_attach(device_t dev) 155254885Sdumbbell{ 156254885Sdumbbell struct radeon_i2c_chan *i2c; 157254885Sdumbbell device_t iic_dev; 158254885Sdumbbell 159254885Sdumbbell i2c = device_get_softc(dev); 160254885Sdumbbell device_set_desc(dev, i2c->name); 161254885Sdumbbell 162254885Sdumbbell /* add generic bit-banging code */ 163254885Sdumbbell iic_dev = device_add_child(dev, "iicbus", -1); 164254885Sdumbbell if (iic_dev == NULL) 165254885Sdumbbell return (ENXIO); 166254885Sdumbbell device_quiet(iic_dev); 167254885Sdumbbell 168254885Sdumbbell /* attach and probe added child */ 169254885Sdumbbell bus_generic_attach(dev); 170254885Sdumbbell 171254885Sdumbbell return (0); 172254885Sdumbbell} 173254885Sdumbbell 174254885Sdumbbellstatic int 175254885Sdumbbellradeon_atom_hw_i2c_detach(device_t dev) 176254885Sdumbbell{ 177254885Sdumbbell /* detach bit-banding code. */ 178254885Sdumbbell bus_generic_detach(dev); 179254885Sdumbbell 180254885Sdumbbell /* delete bit-banding code. */ 181254885Sdumbbell device_delete_children(dev); 182254885Sdumbbell return (0); 183254885Sdumbbell} 184254885Sdumbbell 185254885Sdumbbellstatic int 186254885Sdumbbellradeon_atom_hw_i2c_reset(device_t dev, u_char speed, 187254885Sdumbbell u_char addr, u_char *oldaddr) 188254885Sdumbbell{ 189254885Sdumbbell 190254885Sdumbbell return (0); 191254885Sdumbbell} 192254885Sdumbbell 193254885Sdumbbellstatic device_method_t radeon_atom_hw_i2c_methods[] = { 194254885Sdumbbell DEVMETHOD(device_probe, radeon_atom_hw_i2c_probe), 195254885Sdumbbell DEVMETHOD(device_attach, radeon_atom_hw_i2c_attach), 196254885Sdumbbell DEVMETHOD(device_detach, radeon_atom_hw_i2c_detach), 197254885Sdumbbell DEVMETHOD(iicbus_reset, radeon_atom_hw_i2c_reset), 198254885Sdumbbell DEVMETHOD(iicbus_transfer, radeon_atom_hw_i2c_xfer), 199254885Sdumbbell DEVMETHOD_END 200254885Sdumbbell}; 201254885Sdumbbell 202254885Sdumbbellstatic driver_t radeon_atom_hw_i2c_driver = { 203254885Sdumbbell "radeon_atom_hw_i2c", 204254885Sdumbbell radeon_atom_hw_i2c_methods, 205254885Sdumbbell 0 206254885Sdumbbell}; 207254885Sdumbbell 208254885Sdumbbellstatic devclass_t radeon_atom_hw_i2c_devclass; 209254885SdumbbellDRIVER_MODULE_ORDERED(radeon_atom_hw_i2c, drmn, radeon_atom_hw_i2c_driver, 210254885Sdumbbell radeon_atom_hw_i2c_devclass, 0, 0, SI_ORDER_ANY); 211