Path: blob/master/drivers/gpu/drm/i915/intel_i2c.c
15113 views
/*1* Copyright (c) 2006 Dave Airlie <[email protected]>2* Copyright © 2006-2008,2010 Intel Corporation3* Jesse Barnes <[email protected]>4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice (including the next13* paragraph) shall be included in all copies or substantial portions of the14* Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING21* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER22* DEALINGS IN THE SOFTWARE.23*24* Authors:25* Eric Anholt <[email protected]>26* Chris Wilson <[email protected]>27*/28#include <linux/i2c.h>29#include <linux/i2c-algo-bit.h>30#include "drmP.h"31#include "drm.h"32#include "intel_drv.h"33#include "i915_drm.h"34#include "i915_drv.h"3536/* Intel GPIO access functions */3738#define I2C_RISEFALL_TIME 203940static inline struct intel_gmbus *41to_intel_gmbus(struct i2c_adapter *i2c)42{43return container_of(i2c, struct intel_gmbus, adapter);44}4546struct intel_gpio {47struct i2c_adapter adapter;48struct i2c_algo_bit_data algo;49struct drm_i915_private *dev_priv;50u32 reg;51};5253void54intel_i2c_reset(struct drm_device *dev)55{56struct drm_i915_private *dev_priv = dev->dev_private;57if (HAS_PCH_SPLIT(dev))58I915_WRITE(PCH_GMBUS0, 0);59else60I915_WRITE(GMBUS0, 0);61}6263static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)64{65u32 val;6667/* When using bit bashing for I2C, this bit needs to be set to 1 */68if (!IS_PINEVIEW(dev_priv->dev))69return;7071val = I915_READ(DSPCLK_GATE_D);72if (enable)73val |= DPCUNIT_CLOCK_GATE_DISABLE;74else75val &= ~DPCUNIT_CLOCK_GATE_DISABLE;76I915_WRITE(DSPCLK_GATE_D, val);77}7879static u32 get_reserved(struct intel_gpio *gpio)80{81struct drm_i915_private *dev_priv = gpio->dev_priv;82struct drm_device *dev = dev_priv->dev;83u32 reserved = 0;8485/* On most chips, these bits must be preserved in software. */86if (!IS_I830(dev) && !IS_845G(dev))87reserved = I915_READ_NOTRACE(gpio->reg) &88(GPIO_DATA_PULLUP_DISABLE |89GPIO_CLOCK_PULLUP_DISABLE);9091return reserved;92}9394static int get_clock(void *data)95{96struct intel_gpio *gpio = data;97struct drm_i915_private *dev_priv = gpio->dev_priv;98u32 reserved = get_reserved(gpio);99I915_WRITE_NOTRACE(gpio->reg, reserved | GPIO_CLOCK_DIR_MASK);100I915_WRITE_NOTRACE(gpio->reg, reserved);101return (I915_READ_NOTRACE(gpio->reg) & GPIO_CLOCK_VAL_IN) != 0;102}103104static int get_data(void *data)105{106struct intel_gpio *gpio = data;107struct drm_i915_private *dev_priv = gpio->dev_priv;108u32 reserved = get_reserved(gpio);109I915_WRITE_NOTRACE(gpio->reg, reserved | GPIO_DATA_DIR_MASK);110I915_WRITE_NOTRACE(gpio->reg, reserved);111return (I915_READ_NOTRACE(gpio->reg) & GPIO_DATA_VAL_IN) != 0;112}113114static void set_clock(void *data, int state_high)115{116struct intel_gpio *gpio = data;117struct drm_i915_private *dev_priv = gpio->dev_priv;118u32 reserved = get_reserved(gpio);119u32 clock_bits;120121if (state_high)122clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;123else124clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |125GPIO_CLOCK_VAL_MASK;126127I915_WRITE_NOTRACE(gpio->reg, reserved | clock_bits);128POSTING_READ(gpio->reg);129}130131static void set_data(void *data, int state_high)132{133struct intel_gpio *gpio = data;134struct drm_i915_private *dev_priv = gpio->dev_priv;135u32 reserved = get_reserved(gpio);136u32 data_bits;137138if (state_high)139data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;140else141data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |142GPIO_DATA_VAL_MASK;143144I915_WRITE_NOTRACE(gpio->reg, reserved | data_bits);145POSTING_READ(gpio->reg);146}147148static struct i2c_adapter *149intel_gpio_create(struct drm_i915_private *dev_priv, u32 pin)150{151static const int map_pin_to_reg[] = {1520,153GPIOB,154GPIOA,155GPIOC,156GPIOD,157GPIOE,1580,159GPIOF,160};161struct intel_gpio *gpio;162163if (pin >= ARRAY_SIZE(map_pin_to_reg) || !map_pin_to_reg[pin])164return NULL;165166gpio = kzalloc(sizeof(struct intel_gpio), GFP_KERNEL);167if (gpio == NULL)168return NULL;169170gpio->reg = map_pin_to_reg[pin];171if (HAS_PCH_SPLIT(dev_priv->dev))172gpio->reg += PCH_GPIOA - GPIOA;173gpio->dev_priv = dev_priv;174175snprintf(gpio->adapter.name, sizeof(gpio->adapter.name),176"i915 GPIO%c", "?BACDE?F"[pin]);177gpio->adapter.owner = THIS_MODULE;178gpio->adapter.algo_data = &gpio->algo;179gpio->adapter.dev.parent = &dev_priv->dev->pdev->dev;180gpio->algo.setsda = set_data;181gpio->algo.setscl = set_clock;182gpio->algo.getsda = get_data;183gpio->algo.getscl = get_clock;184gpio->algo.udelay = I2C_RISEFALL_TIME;185gpio->algo.timeout = usecs_to_jiffies(2200);186gpio->algo.data = gpio;187188if (i2c_bit_add_bus(&gpio->adapter))189goto out_free;190191return &gpio->adapter;192193out_free:194kfree(gpio);195return NULL;196}197198static int199intel_i2c_quirk_xfer(struct drm_i915_private *dev_priv,200struct i2c_adapter *adapter,201struct i2c_msg *msgs,202int num)203{204struct intel_gpio *gpio = container_of(adapter,205struct intel_gpio,206adapter);207int ret;208209intel_i2c_reset(dev_priv->dev);210211intel_i2c_quirk_set(dev_priv, true);212set_data(gpio, 1);213set_clock(gpio, 1);214udelay(I2C_RISEFALL_TIME);215216ret = adapter->algo->master_xfer(adapter, msgs, num);217218set_data(gpio, 1);219set_clock(gpio, 1);220intel_i2c_quirk_set(dev_priv, false);221222return ret;223}224225static int226gmbus_xfer(struct i2c_adapter *adapter,227struct i2c_msg *msgs,228int num)229{230struct intel_gmbus *bus = container_of(adapter,231struct intel_gmbus,232adapter);233struct drm_i915_private *dev_priv = adapter->algo_data;234int i, reg_offset;235236if (bus->force_bit)237return intel_i2c_quirk_xfer(dev_priv,238bus->force_bit, msgs, num);239240reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;241242I915_WRITE(GMBUS0 + reg_offset, bus->reg0);243244for (i = 0; i < num; i++) {245u16 len = msgs[i].len;246u8 *buf = msgs[i].buf;247248if (msgs[i].flags & I2C_M_RD) {249I915_WRITE(GMBUS1 + reg_offset,250GMBUS_CYCLE_WAIT | (i + 1 == num ? GMBUS_CYCLE_STOP : 0) |251(len << GMBUS_BYTE_COUNT_SHIFT) |252(msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |253GMBUS_SLAVE_READ | GMBUS_SW_RDY);254POSTING_READ(GMBUS2+reg_offset);255do {256u32 val, loop = 0;257258if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))259goto timeout;260if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)261goto clear_err;262263val = I915_READ(GMBUS3 + reg_offset);264do {265*buf++ = val & 0xff;266val >>= 8;267} while (--len && ++loop < 4);268} while (len);269} else {270u32 val, loop;271272val = loop = 0;273do {274val |= *buf++ << (8 * loop);275} while (--len && ++loop < 4);276277I915_WRITE(GMBUS3 + reg_offset, val);278I915_WRITE(GMBUS1 + reg_offset,279(i + 1 == num ? GMBUS_CYCLE_STOP : GMBUS_CYCLE_WAIT) |280(msgs[i].len << GMBUS_BYTE_COUNT_SHIFT) |281(msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |282GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);283POSTING_READ(GMBUS2+reg_offset);284285while (len) {286if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))287goto timeout;288if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)289goto clear_err;290291val = loop = 0;292do {293val |= *buf++ << (8 * loop);294} while (--len && ++loop < 4);295296I915_WRITE(GMBUS3 + reg_offset, val);297POSTING_READ(GMBUS2+reg_offset);298}299}300301if (i + 1 < num && wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_WAIT_PHASE), 50))302goto timeout;303if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)304goto clear_err;305}306307goto done;308309clear_err:310/* Toggle the Software Clear Interrupt bit. This has the effect311* of resetting the GMBUS controller and so clearing the312* BUS_ERROR raised by the slave's NAK.313*/314I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);315I915_WRITE(GMBUS1 + reg_offset, 0);316317done:318/* Mark the GMBUS interface as disabled. We will re-enable it at the319* start of the next xfer, till then let it sleep.320*/321I915_WRITE(GMBUS0 + reg_offset, 0);322return i;323324timeout:325DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n",326bus->reg0 & 0xff, bus->adapter.name);327I915_WRITE(GMBUS0 + reg_offset, 0);328329/* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */330bus->force_bit = intel_gpio_create(dev_priv, bus->reg0 & 0xff);331if (!bus->force_bit)332return -ENOMEM;333334return intel_i2c_quirk_xfer(dev_priv, bus->force_bit, msgs, num);335}336337static u32 gmbus_func(struct i2c_adapter *adapter)338{339struct intel_gmbus *bus = container_of(adapter,340struct intel_gmbus,341adapter);342343if (bus->force_bit)344bus->force_bit->algo->functionality(bus->force_bit);345346return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |347/* I2C_FUNC_10BIT_ADDR | */348I2C_FUNC_SMBUS_READ_BLOCK_DATA |349I2C_FUNC_SMBUS_BLOCK_PROC_CALL);350}351352static const struct i2c_algorithm gmbus_algorithm = {353.master_xfer = gmbus_xfer,354.functionality = gmbus_func355};356357/**358* intel_gmbus_setup - instantiate all Intel i2c GMBuses359* @dev: DRM device360*/361int intel_setup_gmbus(struct drm_device *dev)362{363static const char *names[GMBUS_NUM_PORTS] = {364"disabled",365"ssc",366"vga",367"panel",368"dpc",369"dpb",370"reserved",371"dpd",372};373struct drm_i915_private *dev_priv = dev->dev_private;374int ret, i;375376dev_priv->gmbus = kcalloc(sizeof(struct intel_gmbus), GMBUS_NUM_PORTS,377GFP_KERNEL);378if (dev_priv->gmbus == NULL)379return -ENOMEM;380381for (i = 0; i < GMBUS_NUM_PORTS; i++) {382struct intel_gmbus *bus = &dev_priv->gmbus[i];383384bus->adapter.owner = THIS_MODULE;385bus->adapter.class = I2C_CLASS_DDC;386snprintf(bus->adapter.name,387sizeof(bus->adapter.name),388"i915 gmbus %s",389names[i]);390391bus->adapter.dev.parent = &dev->pdev->dev;392bus->adapter.algo_data = dev_priv;393394bus->adapter.algo = &gmbus_algorithm;395ret = i2c_add_adapter(&bus->adapter);396if (ret)397goto err;398399/* By default use a conservative clock rate */400bus->reg0 = i | GMBUS_RATE_100KHZ;401402/* XXX force bit banging until GMBUS is fully debugged */403bus->force_bit = intel_gpio_create(dev_priv, i);404}405406intel_i2c_reset(dev_priv->dev);407408return 0;409410err:411while (--i) {412struct intel_gmbus *bus = &dev_priv->gmbus[i];413i2c_del_adapter(&bus->adapter);414}415kfree(dev_priv->gmbus);416dev_priv->gmbus = NULL;417return ret;418}419420void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)421{422struct intel_gmbus *bus = to_intel_gmbus(adapter);423424/* speed:425* 0x0 = 100 KHz426* 0x1 = 50 KHz427* 0x2 = 400 KHz428* 0x3 = 1000 Khz429*/430bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | (speed << 8);431}432433void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)434{435struct intel_gmbus *bus = to_intel_gmbus(adapter);436437if (force_bit) {438if (bus->force_bit == NULL) {439struct drm_i915_private *dev_priv = adapter->algo_data;440bus->force_bit = intel_gpio_create(dev_priv,441bus->reg0 & 0xff);442}443} else {444if (bus->force_bit) {445i2c_del_adapter(bus->force_bit);446kfree(bus->force_bit);447bus->force_bit = NULL;448}449}450}451452void intel_teardown_gmbus(struct drm_device *dev)453{454struct drm_i915_private *dev_priv = dev->dev_private;455int i;456457if (dev_priv->gmbus == NULL)458return;459460for (i = 0; i < GMBUS_NUM_PORTS; i++) {461struct intel_gmbus *bus = &dev_priv->gmbus[i];462if (bus->force_bit) {463i2c_del_adapter(bus->force_bit);464kfree(bus->force_bit);465}466i2c_del_adapter(&bus->adapter);467}468469kfree(dev_priv->gmbus);470dev_priv->gmbus = NULL;471}472473474