18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2006 Dave Airlie <airlied@linux.ie> 38c2ecf20Sopenharmony_ci * Copyright © 2006-2008,2010 Intel Corporation 48c2ecf20Sopenharmony_ci * Jesse Barnes <jesse.barnes@intel.com> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 88c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation 98c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 108c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 118c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next 148c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 158c2ecf20Sopenharmony_ci * Software. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 188c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 198c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 208c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 218c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 228c2ecf20Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 238c2ecf20Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * Authors: 268c2ecf20Sopenharmony_ci * Eric Anholt <eric@anholt.net> 278c2ecf20Sopenharmony_ci * Chris Wilson <chris@chris-wilson.co.uk> 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#include <linux/delay.h> 318c2ecf20Sopenharmony_ci#include <linux/i2c-algo-bit.h> 328c2ecf20Sopenharmony_ci#include <linux/i2c.h> 338c2ecf20Sopenharmony_ci#include <linux/module.h> 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#include "psb_drv.h" 368c2ecf20Sopenharmony_ci#include "psb_intel_drv.h" 378c2ecf20Sopenharmony_ci#include "psb_intel_reg.h" 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define _wait_for(COND, MS, W) ({ \ 408c2ecf20Sopenharmony_ci unsigned long timeout__ = jiffies + msecs_to_jiffies(MS); \ 418c2ecf20Sopenharmony_ci int ret__ = 0; \ 428c2ecf20Sopenharmony_ci while (! (COND)) { \ 438c2ecf20Sopenharmony_ci if (time_after(jiffies, timeout__)) { \ 448c2ecf20Sopenharmony_ci ret__ = -ETIMEDOUT; \ 458c2ecf20Sopenharmony_ci break; \ 468c2ecf20Sopenharmony_ci } \ 478c2ecf20Sopenharmony_ci if (W && !(in_atomic() || in_dbg_master())) msleep(W); \ 488c2ecf20Sopenharmony_ci } \ 498c2ecf20Sopenharmony_ci ret__; \ 508c2ecf20Sopenharmony_ci}) 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define wait_for(COND, MS) _wait_for(COND, MS, 1) 538c2ecf20Sopenharmony_ci#define wait_for_atomic(COND, MS) _wait_for(COND, MS, 0) 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define GMBUS_REG_READ(reg) ioread32(dev_priv->gmbus_reg + (reg)) 568c2ecf20Sopenharmony_ci#define GMBUS_REG_WRITE(reg, val) iowrite32((val), dev_priv->gmbus_reg + (reg)) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/* Intel GPIO access functions */ 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#define I2C_RISEFALL_TIME 20 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_cistatic inline struct intel_gmbus * 638c2ecf20Sopenharmony_cito_intel_gmbus(struct i2c_adapter *i2c) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci return container_of(i2c, struct intel_gmbus, adapter); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistruct intel_gpio { 698c2ecf20Sopenharmony_ci struct i2c_adapter adapter; 708c2ecf20Sopenharmony_ci struct i2c_algo_bit_data algo; 718c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv; 728c2ecf20Sopenharmony_ci u32 reg; 738c2ecf20Sopenharmony_ci}; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_civoid 768c2ecf20Sopenharmony_cigma_intel_i2c_reset(struct drm_device *dev) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 798c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS0, 0); 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic void intel_i2c_quirk_set(struct drm_psb_private *dev_priv, bool enable) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci /* When using bit bashing for I2C, this bit needs to be set to 1 */ 858c2ecf20Sopenharmony_ci /* FIXME: We are never Pineview, right? 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci u32 val; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci if (!IS_PINEVIEW(dev_priv->dev)) 908c2ecf20Sopenharmony_ci return; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci val = REG_READ(DSPCLK_GATE_D); 938c2ecf20Sopenharmony_ci if (enable) 948c2ecf20Sopenharmony_ci val |= DPCUNIT_CLOCK_GATE_DISABLE; 958c2ecf20Sopenharmony_ci else 968c2ecf20Sopenharmony_ci val &= ~DPCUNIT_CLOCK_GATE_DISABLE; 978c2ecf20Sopenharmony_ci REG_WRITE(DSPCLK_GATE_D, val); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci return; 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic u32 get_reserved(struct intel_gpio *gpio) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = gpio->dev_priv; 1068c2ecf20Sopenharmony_ci u32 reserved = 0; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci /* On most chips, these bits must be preserved in software. */ 1098c2ecf20Sopenharmony_ci reserved = GMBUS_REG_READ(gpio->reg) & 1108c2ecf20Sopenharmony_ci (GPIO_DATA_PULLUP_DISABLE | 1118c2ecf20Sopenharmony_ci GPIO_CLOCK_PULLUP_DISABLE); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci return reserved; 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic int get_clock(void *data) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci struct intel_gpio *gpio = data; 1198c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = gpio->dev_priv; 1208c2ecf20Sopenharmony_ci u32 reserved = get_reserved(gpio); 1218c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(gpio->reg, reserved | GPIO_CLOCK_DIR_MASK); 1228c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(gpio->reg, reserved); 1238c2ecf20Sopenharmony_ci return (GMBUS_REG_READ(gpio->reg) & GPIO_CLOCK_VAL_IN) != 0; 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cistatic int get_data(void *data) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci struct intel_gpio *gpio = data; 1298c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = gpio->dev_priv; 1308c2ecf20Sopenharmony_ci u32 reserved = get_reserved(gpio); 1318c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(gpio->reg, reserved | GPIO_DATA_DIR_MASK); 1328c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(gpio->reg, reserved); 1338c2ecf20Sopenharmony_ci return (GMBUS_REG_READ(gpio->reg) & GPIO_DATA_VAL_IN) != 0; 1348c2ecf20Sopenharmony_ci} 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic void set_clock(void *data, int state_high) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci struct intel_gpio *gpio = data; 1398c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = gpio->dev_priv; 1408c2ecf20Sopenharmony_ci u32 reserved = get_reserved(gpio); 1418c2ecf20Sopenharmony_ci u32 clock_bits; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci if (state_high) 1448c2ecf20Sopenharmony_ci clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK; 1458c2ecf20Sopenharmony_ci else 1468c2ecf20Sopenharmony_ci clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK | 1478c2ecf20Sopenharmony_ci GPIO_CLOCK_VAL_MASK; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(gpio->reg, reserved | clock_bits); 1508c2ecf20Sopenharmony_ci GMBUS_REG_READ(gpio->reg); /* Posting */ 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic void set_data(void *data, int state_high) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci struct intel_gpio *gpio = data; 1568c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = gpio->dev_priv; 1578c2ecf20Sopenharmony_ci u32 reserved = get_reserved(gpio); 1588c2ecf20Sopenharmony_ci u32 data_bits; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (state_high) 1618c2ecf20Sopenharmony_ci data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK; 1628c2ecf20Sopenharmony_ci else 1638c2ecf20Sopenharmony_ci data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK | 1648c2ecf20Sopenharmony_ci GPIO_DATA_VAL_MASK; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(gpio->reg, reserved | data_bits); 1678c2ecf20Sopenharmony_ci GMBUS_REG_READ(gpio->reg); 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_cistatic struct i2c_adapter * 1718c2ecf20Sopenharmony_ciintel_gpio_create(struct drm_psb_private *dev_priv, u32 pin) 1728c2ecf20Sopenharmony_ci{ 1738c2ecf20Sopenharmony_ci static const int map_pin_to_reg[] = { 1748c2ecf20Sopenharmony_ci 0, 1758c2ecf20Sopenharmony_ci GPIOB, 1768c2ecf20Sopenharmony_ci GPIOA, 1778c2ecf20Sopenharmony_ci GPIOC, 1788c2ecf20Sopenharmony_ci GPIOD, 1798c2ecf20Sopenharmony_ci GPIOE, 1808c2ecf20Sopenharmony_ci 0, 1818c2ecf20Sopenharmony_ci GPIOF, 1828c2ecf20Sopenharmony_ci }; 1838c2ecf20Sopenharmony_ci struct intel_gpio *gpio; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci if (pin >= ARRAY_SIZE(map_pin_to_reg) || !map_pin_to_reg[pin]) 1868c2ecf20Sopenharmony_ci return NULL; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci gpio = kzalloc(sizeof(struct intel_gpio), GFP_KERNEL); 1898c2ecf20Sopenharmony_ci if (gpio == NULL) 1908c2ecf20Sopenharmony_ci return NULL; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci gpio->reg = map_pin_to_reg[pin]; 1938c2ecf20Sopenharmony_ci gpio->dev_priv = dev_priv; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci snprintf(gpio->adapter.name, sizeof(gpio->adapter.name), 1968c2ecf20Sopenharmony_ci "gma500 GPIO%c", "?BACDE?F"[pin]); 1978c2ecf20Sopenharmony_ci gpio->adapter.owner = THIS_MODULE; 1988c2ecf20Sopenharmony_ci gpio->adapter.algo_data = &gpio->algo; 1998c2ecf20Sopenharmony_ci gpio->adapter.dev.parent = &dev_priv->dev->pdev->dev; 2008c2ecf20Sopenharmony_ci gpio->algo.setsda = set_data; 2018c2ecf20Sopenharmony_ci gpio->algo.setscl = set_clock; 2028c2ecf20Sopenharmony_ci gpio->algo.getsda = get_data; 2038c2ecf20Sopenharmony_ci gpio->algo.getscl = get_clock; 2048c2ecf20Sopenharmony_ci gpio->algo.udelay = I2C_RISEFALL_TIME; 2058c2ecf20Sopenharmony_ci gpio->algo.timeout = usecs_to_jiffies(2200); 2068c2ecf20Sopenharmony_ci gpio->algo.data = gpio; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci if (i2c_bit_add_bus(&gpio->adapter)) 2098c2ecf20Sopenharmony_ci goto out_free; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci return &gpio->adapter; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ciout_free: 2148c2ecf20Sopenharmony_ci kfree(gpio); 2158c2ecf20Sopenharmony_ci return NULL; 2168c2ecf20Sopenharmony_ci} 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cistatic int 2198c2ecf20Sopenharmony_ciintel_i2c_quirk_xfer(struct drm_psb_private *dev_priv, 2208c2ecf20Sopenharmony_ci struct i2c_adapter *adapter, 2218c2ecf20Sopenharmony_ci struct i2c_msg *msgs, 2228c2ecf20Sopenharmony_ci int num) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci struct intel_gpio *gpio = container_of(adapter, 2258c2ecf20Sopenharmony_ci struct intel_gpio, 2268c2ecf20Sopenharmony_ci adapter); 2278c2ecf20Sopenharmony_ci int ret; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci gma_intel_i2c_reset(dev_priv->dev); 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci intel_i2c_quirk_set(dev_priv, true); 2328c2ecf20Sopenharmony_ci set_data(gpio, 1); 2338c2ecf20Sopenharmony_ci set_clock(gpio, 1); 2348c2ecf20Sopenharmony_ci udelay(I2C_RISEFALL_TIME); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci ret = adapter->algo->master_xfer(adapter, msgs, num); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci set_data(gpio, 1); 2398c2ecf20Sopenharmony_ci set_clock(gpio, 1); 2408c2ecf20Sopenharmony_ci intel_i2c_quirk_set(dev_priv, false); 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci return ret; 2438c2ecf20Sopenharmony_ci} 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_cistatic int 2468c2ecf20Sopenharmony_cigmbus_xfer(struct i2c_adapter *adapter, 2478c2ecf20Sopenharmony_ci struct i2c_msg *msgs, 2488c2ecf20Sopenharmony_ci int num) 2498c2ecf20Sopenharmony_ci{ 2508c2ecf20Sopenharmony_ci struct intel_gmbus *bus = container_of(adapter, 2518c2ecf20Sopenharmony_ci struct intel_gmbus, 2528c2ecf20Sopenharmony_ci adapter); 2538c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = adapter->algo_data; 2548c2ecf20Sopenharmony_ci int i, reg_offset; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci if (bus->force_bit) 2578c2ecf20Sopenharmony_ci return intel_i2c_quirk_xfer(dev_priv, 2588c2ecf20Sopenharmony_ci bus->force_bit, msgs, num); 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci reg_offset = 0; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS0 + reg_offset, bus->reg0); 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci for (i = 0; i < num; i++) { 2658c2ecf20Sopenharmony_ci u16 len = msgs[i].len; 2668c2ecf20Sopenharmony_ci u8 *buf = msgs[i].buf; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci if (msgs[i].flags & I2C_M_RD) { 2698c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS1 + reg_offset, 2708c2ecf20Sopenharmony_ci GMBUS_CYCLE_WAIT | 2718c2ecf20Sopenharmony_ci (i + 1 == num ? GMBUS_CYCLE_STOP : 0) | 2728c2ecf20Sopenharmony_ci (len << GMBUS_BYTE_COUNT_SHIFT) | 2738c2ecf20Sopenharmony_ci (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) | 2748c2ecf20Sopenharmony_ci GMBUS_SLAVE_READ | GMBUS_SW_RDY); 2758c2ecf20Sopenharmony_ci GMBUS_REG_READ(GMBUS2+reg_offset); 2768c2ecf20Sopenharmony_ci do { 2778c2ecf20Sopenharmony_ci u32 val, loop = 0; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci if (wait_for(GMBUS_REG_READ(GMBUS2 + reg_offset) & 2808c2ecf20Sopenharmony_ci (GMBUS_SATOER | GMBUS_HW_RDY), 50)) 2818c2ecf20Sopenharmony_ci goto timeout; 2828c2ecf20Sopenharmony_ci if (GMBUS_REG_READ(GMBUS2 + reg_offset) & GMBUS_SATOER) 2838c2ecf20Sopenharmony_ci goto clear_err; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci val = GMBUS_REG_READ(GMBUS3 + reg_offset); 2868c2ecf20Sopenharmony_ci do { 2878c2ecf20Sopenharmony_ci *buf++ = val & 0xff; 2888c2ecf20Sopenharmony_ci val >>= 8; 2898c2ecf20Sopenharmony_ci } while (--len && ++loop < 4); 2908c2ecf20Sopenharmony_ci } while (len); 2918c2ecf20Sopenharmony_ci } else { 2928c2ecf20Sopenharmony_ci u32 val, loop; 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci val = loop = 0; 2958c2ecf20Sopenharmony_ci do { 2968c2ecf20Sopenharmony_ci val |= *buf++ << (8 * loop); 2978c2ecf20Sopenharmony_ci } while (--len && ++loop < 4); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS3 + reg_offset, val); 3008c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS1 + reg_offset, 3018c2ecf20Sopenharmony_ci (i + 1 == num ? GMBUS_CYCLE_STOP : GMBUS_CYCLE_WAIT) | 3028c2ecf20Sopenharmony_ci (msgs[i].len << GMBUS_BYTE_COUNT_SHIFT) | 3038c2ecf20Sopenharmony_ci (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) | 3048c2ecf20Sopenharmony_ci GMBUS_SLAVE_WRITE | GMBUS_SW_RDY); 3058c2ecf20Sopenharmony_ci GMBUS_REG_READ(GMBUS2+reg_offset); 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci while (len) { 3088c2ecf20Sopenharmony_ci if (wait_for(GMBUS_REG_READ(GMBUS2 + reg_offset) & 3098c2ecf20Sopenharmony_ci (GMBUS_SATOER | GMBUS_HW_RDY), 50)) 3108c2ecf20Sopenharmony_ci goto timeout; 3118c2ecf20Sopenharmony_ci if (GMBUS_REG_READ(GMBUS2 + reg_offset) & 3128c2ecf20Sopenharmony_ci GMBUS_SATOER) 3138c2ecf20Sopenharmony_ci goto clear_err; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci val = loop = 0; 3168c2ecf20Sopenharmony_ci do { 3178c2ecf20Sopenharmony_ci val |= *buf++ << (8 * loop); 3188c2ecf20Sopenharmony_ci } while (--len && ++loop < 4); 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS3 + reg_offset, val); 3218c2ecf20Sopenharmony_ci GMBUS_REG_READ(GMBUS2+reg_offset); 3228c2ecf20Sopenharmony_ci } 3238c2ecf20Sopenharmony_ci } 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci if (i + 1 < num && wait_for(GMBUS_REG_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_WAIT_PHASE), 50)) 3268c2ecf20Sopenharmony_ci goto timeout; 3278c2ecf20Sopenharmony_ci if (GMBUS_REG_READ(GMBUS2 + reg_offset) & GMBUS_SATOER) 3288c2ecf20Sopenharmony_ci goto clear_err; 3298c2ecf20Sopenharmony_ci } 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci goto done; 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ciclear_err: 3348c2ecf20Sopenharmony_ci /* Toggle the Software Clear Interrupt bit. This has the effect 3358c2ecf20Sopenharmony_ci * of resetting the GMBUS controller and so clearing the 3368c2ecf20Sopenharmony_ci * BUS_ERROR raised by the slave's NAK. 3378c2ecf20Sopenharmony_ci */ 3388c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT); 3398c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS1 + reg_offset, 0); 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_cidone: 3428c2ecf20Sopenharmony_ci /* Mark the GMBUS interface as disabled. We will re-enable it at the 3438c2ecf20Sopenharmony_ci * start of the next xfer, till then let it sleep. 3448c2ecf20Sopenharmony_ci */ 3458c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS0 + reg_offset, 0); 3468c2ecf20Sopenharmony_ci return i; 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_citimeout: 3498c2ecf20Sopenharmony_ci DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n", 3508c2ecf20Sopenharmony_ci bus->reg0 & 0xff, bus->adapter.name); 3518c2ecf20Sopenharmony_ci GMBUS_REG_WRITE(GMBUS0 + reg_offset, 0); 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */ 3548c2ecf20Sopenharmony_ci bus->force_bit = intel_gpio_create(dev_priv, bus->reg0 & 0xff); 3558c2ecf20Sopenharmony_ci if (!bus->force_bit) 3568c2ecf20Sopenharmony_ci return -ENOMEM; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci return intel_i2c_quirk_xfer(dev_priv, bus->force_bit, msgs, num); 3598c2ecf20Sopenharmony_ci} 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_cistatic u32 gmbus_func(struct i2c_adapter *adapter) 3628c2ecf20Sopenharmony_ci{ 3638c2ecf20Sopenharmony_ci struct intel_gmbus *bus = container_of(adapter, 3648c2ecf20Sopenharmony_ci struct intel_gmbus, 3658c2ecf20Sopenharmony_ci adapter); 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci if (bus->force_bit) 3688c2ecf20Sopenharmony_ci bus->force_bit->algo->functionality(bus->force_bit); 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | 3718c2ecf20Sopenharmony_ci /* I2C_FUNC_10BIT_ADDR | */ 3728c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_READ_BLOCK_DATA | 3738c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_BLOCK_PROC_CALL); 3748c2ecf20Sopenharmony_ci} 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_cistatic const struct i2c_algorithm gmbus_algorithm = { 3778c2ecf20Sopenharmony_ci .master_xfer = gmbus_xfer, 3788c2ecf20Sopenharmony_ci .functionality = gmbus_func 3798c2ecf20Sopenharmony_ci}; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci/** 3828c2ecf20Sopenharmony_ci * intel_gmbus_setup - instantiate all Intel i2c GMBuses 3838c2ecf20Sopenharmony_ci * @dev: DRM device 3848c2ecf20Sopenharmony_ci */ 3858c2ecf20Sopenharmony_ciint gma_intel_setup_gmbus(struct drm_device *dev) 3868c2ecf20Sopenharmony_ci{ 3878c2ecf20Sopenharmony_ci static const char *names[GMBUS_NUM_PORTS] = { 3888c2ecf20Sopenharmony_ci "disabled", 3898c2ecf20Sopenharmony_ci "ssc", 3908c2ecf20Sopenharmony_ci "vga", 3918c2ecf20Sopenharmony_ci "panel", 3928c2ecf20Sopenharmony_ci "dpc", 3938c2ecf20Sopenharmony_ci "dpb", 3948c2ecf20Sopenharmony_ci "reserved", 3958c2ecf20Sopenharmony_ci "dpd", 3968c2ecf20Sopenharmony_ci }; 3978c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 3988c2ecf20Sopenharmony_ci int ret, i; 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci dev_priv->gmbus = kcalloc(GMBUS_NUM_PORTS, sizeof(struct intel_gmbus), 4018c2ecf20Sopenharmony_ci GFP_KERNEL); 4028c2ecf20Sopenharmony_ci if (dev_priv->gmbus == NULL) 4038c2ecf20Sopenharmony_ci return -ENOMEM; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci if (IS_MRST(dev)) 4068c2ecf20Sopenharmony_ci dev_priv->gmbus_reg = dev_priv->aux_reg; 4078c2ecf20Sopenharmony_ci else 4088c2ecf20Sopenharmony_ci dev_priv->gmbus_reg = dev_priv->vdc_reg; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci for (i = 0; i < GMBUS_NUM_PORTS; i++) { 4118c2ecf20Sopenharmony_ci struct intel_gmbus *bus = &dev_priv->gmbus[i]; 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci bus->adapter.owner = THIS_MODULE; 4148c2ecf20Sopenharmony_ci bus->adapter.class = I2C_CLASS_DDC; 4158c2ecf20Sopenharmony_ci snprintf(bus->adapter.name, 4168c2ecf20Sopenharmony_ci sizeof(bus->adapter.name), 4178c2ecf20Sopenharmony_ci "gma500 gmbus %s", 4188c2ecf20Sopenharmony_ci names[i]); 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci bus->adapter.dev.parent = &dev->pdev->dev; 4218c2ecf20Sopenharmony_ci bus->adapter.algo_data = dev_priv; 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci bus->adapter.algo = &gmbus_algorithm; 4248c2ecf20Sopenharmony_ci ret = i2c_add_adapter(&bus->adapter); 4258c2ecf20Sopenharmony_ci if (ret) 4268c2ecf20Sopenharmony_ci goto err; 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci /* By default use a conservative clock rate */ 4298c2ecf20Sopenharmony_ci bus->reg0 = i | GMBUS_RATE_100KHZ; 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci /* XXX force bit banging until GMBUS is fully debugged */ 4328c2ecf20Sopenharmony_ci bus->force_bit = intel_gpio_create(dev_priv, i); 4338c2ecf20Sopenharmony_ci } 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci gma_intel_i2c_reset(dev_priv->dev); 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci return 0; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_cierr: 4408c2ecf20Sopenharmony_ci while (i--) { 4418c2ecf20Sopenharmony_ci struct intel_gmbus *bus = &dev_priv->gmbus[i]; 4428c2ecf20Sopenharmony_ci i2c_del_adapter(&bus->adapter); 4438c2ecf20Sopenharmony_ci } 4448c2ecf20Sopenharmony_ci kfree(dev_priv->gmbus); 4458c2ecf20Sopenharmony_ci dev_priv->gmbus = NULL; 4468c2ecf20Sopenharmony_ci return ret; 4478c2ecf20Sopenharmony_ci} 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_civoid gma_intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed) 4508c2ecf20Sopenharmony_ci{ 4518c2ecf20Sopenharmony_ci struct intel_gmbus *bus = to_intel_gmbus(adapter); 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci /* speed: 4548c2ecf20Sopenharmony_ci * 0x0 = 100 KHz 4558c2ecf20Sopenharmony_ci * 0x1 = 50 KHz 4568c2ecf20Sopenharmony_ci * 0x2 = 400 KHz 4578c2ecf20Sopenharmony_ci * 0x3 = 1000 Khz 4588c2ecf20Sopenharmony_ci */ 4598c2ecf20Sopenharmony_ci bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | (speed << 8); 4608c2ecf20Sopenharmony_ci} 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_civoid gma_intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit) 4638c2ecf20Sopenharmony_ci{ 4648c2ecf20Sopenharmony_ci struct intel_gmbus *bus = to_intel_gmbus(adapter); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci if (force_bit) { 4678c2ecf20Sopenharmony_ci if (bus->force_bit == NULL) { 4688c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = adapter->algo_data; 4698c2ecf20Sopenharmony_ci bus->force_bit = intel_gpio_create(dev_priv, 4708c2ecf20Sopenharmony_ci bus->reg0 & 0xff); 4718c2ecf20Sopenharmony_ci } 4728c2ecf20Sopenharmony_ci } else { 4738c2ecf20Sopenharmony_ci if (bus->force_bit) { 4748c2ecf20Sopenharmony_ci i2c_del_adapter(bus->force_bit); 4758c2ecf20Sopenharmony_ci kfree(bus->force_bit); 4768c2ecf20Sopenharmony_ci bus->force_bit = NULL; 4778c2ecf20Sopenharmony_ci } 4788c2ecf20Sopenharmony_ci } 4798c2ecf20Sopenharmony_ci} 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_civoid gma_intel_teardown_gmbus(struct drm_device *dev) 4828c2ecf20Sopenharmony_ci{ 4838c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 4848c2ecf20Sopenharmony_ci int i; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci if (dev_priv->gmbus == NULL) 4878c2ecf20Sopenharmony_ci return; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci for (i = 0; i < GMBUS_NUM_PORTS; i++) { 4908c2ecf20Sopenharmony_ci struct intel_gmbus *bus = &dev_priv->gmbus[i]; 4918c2ecf20Sopenharmony_ci if (bus->force_bit) { 4928c2ecf20Sopenharmony_ci i2c_del_adapter(bus->force_bit); 4938c2ecf20Sopenharmony_ci kfree(bus->force_bit); 4948c2ecf20Sopenharmony_ci } 4958c2ecf20Sopenharmony_ci i2c_del_adapter(&bus->adapter); 4968c2ecf20Sopenharmony_ci } 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci dev_priv->gmbus_reg = NULL; /* iounmap is done in driver_unload */ 4998c2ecf20Sopenharmony_ci kfree(dev_priv->gmbus); 5008c2ecf20Sopenharmony_ci dev_priv->gmbus = NULL; 5018c2ecf20Sopenharmony_ci} 502