1 /* 2 * Copyright © 2006-2007 Intel Corporation 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms and conditions of the GNU General Public License, 6 * version 2, as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * this program; if not, write to the Free Software Foundation, Inc., 15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 16 * 17 * Authors: 18 * Eric Anholt <eric@anholt.net> 19 */ 20 21 #include <linux/delay.h> 22 #include <linux/export.h> 23 #include <linux/i2c-algo-bit.h> 24 #include <linux/i2c.h> 25 26 #include "psb_drv.h" 27 #include "psb_intel_reg.h" 28 29 /* 30 * Intel GPIO access functions 31 */ 32 33 #define I2C_RISEFALL_TIME 20 34 35 static int get_clock(void *data) 36 { 37 struct psb_intel_i2c_chan *chan = data; 38 struct drm_device *dev = chan->drm_dev; 39 u32 val; 40 41 val = REG_READ(chan->reg); 42 return (val & GPIO_CLOCK_VAL_IN) != 0; 43 } 44 45 static int get_data(void *data) 46 { 47 struct psb_intel_i2c_chan *chan = data; 48 struct drm_device *dev = chan->drm_dev; 49 u32 val; 50 51 val = REG_READ(chan->reg); 52 return (val & GPIO_DATA_VAL_IN) != 0; 53 } 54 55 static void set_clock(void *data, int state_high) 56 { 57 struct psb_intel_i2c_chan *chan = data; 58 struct drm_device *dev = chan->drm_dev; 59 u32 reserved = 0, clock_bits; 60 61 /* On most chips, these bits must be preserved in software. */ 62 reserved = 63 REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE | 64 GPIO_CLOCK_PULLUP_DISABLE); 65 66 if (state_high) 67 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK; 68 else 69 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK | 70 GPIO_CLOCK_VAL_MASK; 71 REG_WRITE(chan->reg, reserved | clock_bits); 72 udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */ 73 } 74 75 static void set_data(void *data, int state_high) 76 { 77 struct psb_intel_i2c_chan *chan = data; 78 struct drm_device *dev = chan->drm_dev; 79 u32 reserved = 0, data_bits; 80 81 /* On most chips, these bits must be preserved in software. */ 82 reserved = 83 REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE | 84 GPIO_CLOCK_PULLUP_DISABLE); 85 86 if (state_high) 87 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK; 88 else 89 data_bits = 90 GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK | 91 GPIO_DATA_VAL_MASK; 92 93 REG_WRITE(chan->reg, reserved | data_bits); 94 udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */ 95 } 96 97 /** 98 * psb_intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg 99 * @dev: DRM device 100 * @output: driver specific output device 101 * @reg: GPIO reg to use 102 * @name: name for this bus 103 * 104 * Creates and registers a new i2c bus with the Linux i2c layer, for use 105 * in output probing and control (e.g. DDC or SDVO control functions). 106 * 107 * Possible values for @reg include: 108 * %GPIOA 109 * %GPIOB 110 * %GPIOC 111 * %GPIOD 112 * %GPIOE 113 * %GPIOF 114 * %GPIOG 115 * %GPIOH 116 * see PRM for details on how these different busses are used. 117 */ 118 struct psb_intel_i2c_chan *psb_intel_i2c_create(struct drm_device *dev, 119 const u32 reg, const char *name) 120 { 121 struct psb_intel_i2c_chan *chan; 122 123 chan = kzalloc(sizeof(struct psb_intel_i2c_chan), GFP_KERNEL); 124 if (!chan) 125 goto out_free; 126 127 chan->drm_dev = dev; 128 chan->reg = reg; 129 snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name); 130 chan->adapter.owner = THIS_MODULE; 131 chan->adapter.algo_data = &chan->algo; 132 chan->adapter.dev.parent = &dev->pdev->dev; 133 chan->algo.setsda = set_data; 134 chan->algo.setscl = set_clock; 135 chan->algo.getsda = get_data; 136 chan->algo.getscl = get_clock; 137 chan->algo.udelay = 20; 138 chan->algo.timeout = usecs_to_jiffies(2200); 139 chan->algo.data = chan; 140 141 i2c_set_adapdata(&chan->adapter, chan); 142 143 if (i2c_bit_add_bus(&chan->adapter)) 144 goto out_free; 145 146 /* JJJ: raise SCL and SDA? */ 147 set_data(chan, 1); 148 set_clock(chan, 1); 149 udelay(20); 150 151 return chan; 152 153 out_free: 154 kfree(chan); 155 return NULL; 156 } 157 158 /** 159 * psb_intel_i2c_destroy - unregister and free i2c bus resources 160 * @output: channel to free 161 * 162 * Unregister the adapter from the i2c layer, then free the structure. 163 */ 164 void psb_intel_i2c_destroy(struct psb_intel_i2c_chan *chan) 165 { 166 if (!chan) 167 return; 168 169 i2c_del_adapter(&chan->adapter); 170 kfree(chan); 171 } 172