drm/radeon/kms/dce3+: add support for hw i2c using atom
authorAlex Deucher <alexander.deucher@amd.com>
Fri, 20 Jan 2012 19:50:18 +0000 (14:50 -0500)
committerDave Airlie <airlied@redhat.com>
Fri, 3 Feb 2012 09:38:05 +0000 (09:38 +0000)
Starting with DCE3 hardware, atom contains a general purpose
ProcessI2cChannelTransaction similar to ProcessAuxChannelTransaction.

Add an implementation using the atom tables for DCE3+ hardware.

This should be a little less CPU intensive than bit banging and
may work better in certain cases.

Enable it by setting the radeon hw_i2c module parameter to 1.  E.g.,
radeon.hw_i2c=1
on the kernel command line in grub.

Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
drivers/gpu/drm/radeon/Makefile
drivers/gpu/drm/radeon/atombios_i2c.c [new file with mode: 0644]
drivers/gpu/drm/radeon/radeon_i2c.c

index 2139fe8..8410415 100644 (file)
@@ -71,7 +71,7 @@ radeon-y += radeon_device.o radeon_asic.o radeon_kms.o \
        r600_blit_kms.o radeon_pm.o atombios_dp.o r600_audio.o r600_hdmi.o \
        evergreen.o evergreen_cs.o evergreen_blit_shaders.o evergreen_blit_kms.o \
        radeon_trace_points.o ni.o cayman_blit_shaders.o atombios_encoders.o \
-       radeon_semaphore.o radeon_sa.o
+       radeon_semaphore.o radeon_sa.o atombios_i2c.o
 
 radeon-$(CONFIG_COMPAT) += radeon_ioc32.o
 radeon-$(CONFIG_VGA_SWITCHEROO) += radeon_atpx_handler.o
diff --git a/drivers/gpu/drm/radeon/atombios_i2c.c b/drivers/gpu/drm/radeon/atombios_i2c.c
new file mode 100644 (file)
index 0000000..44d87b6
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2011 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Alex Deucher
+ *
+ */
+#include "drmP.h"
+#include "radeon_drm.h"
+#include "radeon.h"
+#include "atom.h"
+
+#define TARGET_HW_I2C_CLOCK 50
+
+/* these are a limitation of ProcessI2cChannelTransaction not the hw */
+#define ATOM_MAX_HW_I2C_WRITE 2
+#define ATOM_MAX_HW_I2C_READ  255
+
+static int radeon_process_i2c_ch(struct radeon_i2c_chan *chan,
+                                u8 slave_addr, u8 flags,
+                                u8 *buf, u8 num)
+{
+       struct drm_device *dev = chan->dev;
+       struct radeon_device *rdev = dev->dev_private;
+       PROCESS_I2C_CHANNEL_TRANSACTION_PS_ALLOCATION args;
+       int index = GetIndexIntoMasterTable(COMMAND, ProcessI2cChannelTransaction);
+       unsigned char *base;
+       u16 out;
+
+       memset(&args, 0, sizeof(args));
+
+       base = (unsigned char *)rdev->mode_info.atom_context->scratch;
+
+       if (flags & HW_I2C_WRITE) {
+               if (num > ATOM_MAX_HW_I2C_WRITE) {
+                       DRM_ERROR("hw i2c: tried to write too many bytes (%d vs 2)\n", num);
+                       return -EINVAL;
+               }
+               memcpy(&out, buf, num);
+               args.lpI2CDataOut = cpu_to_le16(out);
+       } else {
+               if (num > ATOM_MAX_HW_I2C_READ) {
+                       DRM_ERROR("hw i2c: tried to read too many bytes (%d vs 255)\n", num);
+                       return -EINVAL;
+               }
+       }
+
+       args.ucI2CSpeed = TARGET_HW_I2C_CLOCK;
+       args.ucRegIndex = 0;
+       args.ucTransBytes = num;
+       args.ucSlaveAddr = slave_addr << 1;
+       args.ucLineNumber = chan->rec.i2c_id;
+
+       atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
+
+       /* error */
+       if (args.ucStatus != HW_ASSISTED_I2C_STATUS_SUCCESS) {
+               DRM_DEBUG_KMS("hw_i2c error\n");
+               return -EIO;
+       }
+
+       if (!(flags & HW_I2C_WRITE))
+               memcpy(buf, base, num);
+
+       return 0;
+}
+
+int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
+                           struct i2c_msg *msgs, int num)
+{
+       struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
+       struct i2c_msg *p;
+       int i, remaining, current_count, buffer_offset, max_bytes, ret;
+       u8 buf = 0, flags;
+
+       /* check for bus probe */
+       p = &msgs[0];
+       if ((num == 1) && (p->len == 0)) {
+               ret = radeon_process_i2c_ch(i2c,
+                                           p->addr, HW_I2C_WRITE,
+                                           &buf, 1);
+               if (ret)
+                       return ret;
+               else
+                       return num;
+       }
+
+       for (i = 0; i < num; i++) {
+               p = &msgs[i];
+               remaining = p->len;
+               buffer_offset = 0;
+               /* max_bytes are a limitation of ProcessI2cChannelTransaction not the hw */
+               if (p->flags & I2C_M_RD) {
+                       max_bytes = ATOM_MAX_HW_I2C_READ;
+                       flags = HW_I2C_READ;
+               } else {
+                       max_bytes = ATOM_MAX_HW_I2C_WRITE;
+                       flags = HW_I2C_WRITE;
+               }
+               while (remaining) {
+                       if (remaining > max_bytes)
+                               current_count = max_bytes;
+                       else
+                               current_count = remaining;
+                       ret = radeon_process_i2c_ch(i2c,
+                                                   p->addr, flags,
+                                                   &p->buf[buffer_offset], current_count);
+                       if (ret)
+                               return ret;
+                       remaining -= current_count;
+                       buffer_offset += current_count;
+               }
+       }
+
+       return num;
+}
+
+u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap)
+{
+       return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
+}
+
index c47f222..3265a7a 100644 (file)
 #include "radeon.h"
 #include "atom.h"
 
+extern int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
+                                  struct i2c_msg *msgs, int num);
+extern u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap);
+
 /**
  * radeon_ddc_probe
  *
@@ -882,6 +886,11 @@ static const struct i2c_algorithm radeon_i2c_algo = {
        .functionality = radeon_hw_i2c_func,
 };
 
+static const struct i2c_algorithm radeon_atom_i2c_algo = {
+       .master_xfer = radeon_atom_hw_i2c_xfer,
+       .functionality = radeon_atom_hw_i2c_func,
+};
+
 struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
                                          struct radeon_i2c_bus_rec *rec,
                                          const char *name)
@@ -914,6 +923,18 @@ struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
                        DRM_ERROR("Failed to register hw i2c %s\n", name);
                        goto out_free;
                }
+       } else if (rec->hw_capable &&
+                  radeon_hw_i2c &&
+                  ASIC_IS_DCE3(rdev)) {
+               /* hw i2c using atom */
+               snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
+                        "Radeon i2c hw bus %s", name);
+               i2c->adapter.algo = &radeon_atom_i2c_algo;
+               ret = i2c_add_adapter(&i2c->adapter);
+               if (ret) {
+                       DRM_ERROR("Failed to register hw i2c %s\n", name);
+                       goto out_free;
+               }
        } else {
                /* set the radeon bit adapter */
                snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),