2 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2008 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Eric Anholt <eric@anholt.net>
28 #include <linux/i2c.h>
29 #include <linux/i2c-id.h>
30 #include <linux/i2c-algo-bit.h>
33 #include "intel_drv.h"
37 void intel_i2c_quirk_set(struct drm_device
*dev
, bool enable
)
39 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
41 /* When using bit bashing for I2C, this bit needs to be set to 1 */
46 I915_READ(CG_2D_DIS
) | DPCUNIT_CLOCK_GATE_DISABLE
);
49 I915_READ(CG_2D_DIS
) & (~DPCUNIT_CLOCK_GATE_DISABLE
));
53 * Intel GPIO access functions
56 #define I2C_RISEFALL_TIME 20
58 static int get_clock(void *data
)
60 struct intel_i2c_chan
*chan
= data
;
61 struct drm_i915_private
*dev_priv
= chan
->drm_dev
->dev_private
;
64 val
= I915_READ(chan
->reg
);
65 return ((val
& GPIO_CLOCK_VAL_IN
) != 0);
68 static int get_data(void *data
)
70 struct intel_i2c_chan
*chan
= data
;
71 struct drm_i915_private
*dev_priv
= chan
->drm_dev
->dev_private
;
74 val
= I915_READ(chan
->reg
);
75 return ((val
& GPIO_DATA_VAL_IN
) != 0);
78 static void set_clock(void *data
, int state_high
)
80 struct intel_i2c_chan
*chan
= data
;
81 struct drm_device
*dev
= chan
->drm_dev
;
82 struct drm_i915_private
*dev_priv
= chan
->drm_dev
->dev_private
;
83 u32 reserved
= 0, clock_bits
;
85 /* On most chips, these bits must be preserved in software. */
86 if (!IS_I830(dev
) && !IS_845G(dev
))
87 reserved
= I915_READ(chan
->reg
) & (GPIO_DATA_PULLUP_DISABLE
|
88 GPIO_CLOCK_PULLUP_DISABLE
);
91 clock_bits
= GPIO_CLOCK_DIR_IN
| GPIO_CLOCK_DIR_MASK
;
93 clock_bits
= GPIO_CLOCK_DIR_OUT
| GPIO_CLOCK_DIR_MASK
|
95 I915_WRITE(chan
->reg
, reserved
| clock_bits
);
96 udelay(I2C_RISEFALL_TIME
); /* wait for the line to change state */
99 static void set_data(void *data
, int state_high
)
101 struct intel_i2c_chan
*chan
= data
;
102 struct drm_device
*dev
= chan
->drm_dev
;
103 struct drm_i915_private
*dev_priv
= chan
->drm_dev
->dev_private
;
104 u32 reserved
= 0, data_bits
;
106 /* On most chips, these bits must be preserved in software. */
107 if (!IS_I830(dev
) && !IS_845G(dev
))
108 reserved
= I915_READ(chan
->reg
) & (GPIO_DATA_PULLUP_DISABLE
|
109 GPIO_CLOCK_PULLUP_DISABLE
);
112 data_bits
= GPIO_DATA_DIR_IN
| GPIO_DATA_DIR_MASK
;
114 data_bits
= GPIO_DATA_DIR_OUT
| GPIO_DATA_DIR_MASK
|
117 I915_WRITE(chan
->reg
, reserved
| data_bits
);
118 udelay(I2C_RISEFALL_TIME
); /* wait for the line to change state */
122 * intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
124 * @output: driver specific output device
125 * @reg: GPIO reg to use
126 * @name: name for this bus
128 * Creates and registers a new i2c bus with the Linux i2c layer, for use
129 * in output probing and control (e.g. DDC or SDVO control functions).
131 * Possible values for @reg include:
140 * see PRM for details on how these different busses are used.
142 struct intel_i2c_chan
*intel_i2c_create(struct drm_device
*dev
, const u32 reg
,
145 struct intel_i2c_chan
*chan
;
147 chan
= kzalloc(sizeof(struct intel_i2c_chan
), GFP_KERNEL
);
153 snprintf(chan
->adapter
.name
, I2C_NAME_SIZE
, "intel drm %s", name
);
154 chan
->adapter
.owner
= THIS_MODULE
;
155 chan
->adapter
.algo_data
= &chan
->algo
;
156 chan
->adapter
.dev
.parent
= &dev
->pdev
->dev
;
157 chan
->algo
.setsda
= set_data
;
158 chan
->algo
.setscl
= set_clock
;
159 chan
->algo
.getsda
= get_data
;
160 chan
->algo
.getscl
= get_clock
;
161 chan
->algo
.udelay
= 20;
162 chan
->algo
.timeout
= usecs_to_jiffies(2200);
163 chan
->algo
.data
= chan
;
165 i2c_set_adapdata(&chan
->adapter
, chan
);
167 if(i2c_bit_add_bus(&chan
->adapter
))
170 /* JJJ: raise SCL and SDA? */
171 intel_i2c_quirk_set(dev
, true);
174 intel_i2c_quirk_set(dev
, false);
185 * intel_i2c_destroy - unregister and free i2c bus resources
186 * @output: channel to free
188 * Unregister the adapter from the i2c layer, then free the structure.
190 void intel_i2c_destroy(struct intel_i2c_chan
*chan
)
195 i2c_del_adapter(&chan
->adapter
);