intel_i2c.c 6.2 KB
Newer Older
J
Jesse Barnes 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
 * Copyright © 2006-2008 Intel Corporation
 *   Jesse Barnes <jesse.barnes@intel.com>
 *
 * 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 (including the next
 * paragraph) 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 AUTHORS OR COPYRIGHT HOLDERS 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:
 *	Eric Anholt <eric@anholt.net>
 */
#include <linux/i2c.h>
29
#include <linux/slab.h>
J
Jesse Barnes 已提交
30 31 32 33 34 35 36 37
#include <linux/i2c-id.h>
#include <linux/i2c-algo-bit.h>
#include "drmP.h"
#include "drm.h"
#include "intel_drv.h"
#include "i915_drm.h"
#include "i915_drv.h"

38 39 40
void intel_i2c_quirk_set(struct drm_device *dev, bool enable)
{
	struct drm_i915_private *dev_priv = dev->dev_private;
41
	u32 val;
42 43

	/* When using bit bashing for I2C, this bit needs to be set to 1 */
44
	if (!IS_PINEVIEW(dev))
45
		return;
46 47

	val = I915_READ(DSPCLK_GATE_D);
48
	if (enable)
49
		val |= DPCUNIT_CLOCK_GATE_DISABLE;
50
	else
51 52
		val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
	I915_WRITE(DSPCLK_GATE_D, val);
53 54
}

J
Jesse Barnes 已提交
55 56 57 58 59 60
/*
 * Intel GPIO access functions
 */

#define I2C_RISEFALL_TIME 20

61 62 63 64 65 66
static inline struct drm_i915_private *
get_dev_priv(struct intel_i2c_chan *chan)
{
	return chan->encoder->base.dev->dev_private;
}

J
Jesse Barnes 已提交
67 68 69
static int get_clock(void *data)
{
	struct intel_i2c_chan *chan = data;
70
	struct drm_i915_private *dev_priv = get_dev_priv(chan);
71
	return (I915_READ(chan->reg) & GPIO_CLOCK_VAL_IN) != 0;
J
Jesse Barnes 已提交
72 73 74 75 76
}

static int get_data(void *data)
{
	struct intel_i2c_chan *chan = data;
77
	struct drm_i915_private *dev_priv = get_dev_priv(chan);
78
	return (I915_READ(chan->reg) & GPIO_DATA_VAL_IN) != 0;
J
Jesse Barnes 已提交
79 80 81 82 83
}

static void set_clock(void *data, int state_high)
{
	struct intel_i2c_chan *chan = data;
84 85
	struct drm_i915_private *dev_priv = get_dev_priv(chan);
	struct drm_device *dev = dev_priv->dev;
J
Jesse Barnes 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
	u32 reserved = 0, clock_bits;

	/* On most chips, these bits must be preserved in software. */
	if (!IS_I830(dev) && !IS_845G(dev))
		reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
						   GPIO_CLOCK_PULLUP_DISABLE);

	if (state_high)
		clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
	else
		clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
			GPIO_CLOCK_VAL_MASK;
	I915_WRITE(chan->reg, reserved | clock_bits);
99
	POSTING_READ(chan->reg);
J
Jesse Barnes 已提交
100 101 102 103 104
}

static void set_data(void *data, int state_high)
{
	struct intel_i2c_chan *chan = data;
105 106
	struct drm_i915_private *dev_priv = get_dev_priv(chan);
	struct drm_device *dev = dev_priv->dev;
J
Jesse Barnes 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120
	u32 reserved = 0, data_bits;

	/* On most chips, these bits must be preserved in software. */
	if (!IS_I830(dev) && !IS_845G(dev))
		reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
						   GPIO_CLOCK_PULLUP_DISABLE);

	if (state_high)
		data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
	else
		data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
			GPIO_DATA_VAL_MASK;

	I915_WRITE(chan->reg, reserved | data_bits);
121
	POSTING_READ(chan->reg);
J
Jesse Barnes 已提交
122 123
}

124 125 126 127 128 129 130 131 132 133
/* Clears the GMBUS setup.  Our driver doesn't make use of the GMBUS I2C
 * engine, but if the BIOS leaves it enabled, then that can break our use
 * of the bit-banging I2C interfaces.  This is notably the case with the
 * Mac Mini in EFI mode.
 */
void
intel_i2c_reset_gmbus(struct drm_device *dev)
{
	struct drm_i915_private *dev_priv = dev->dev_private;

134
	if (HAS_PCH_SPLIT(dev))
135
		I915_WRITE(PCH_GMBUS0, 0);
136
	else
137 138 139
		I915_WRITE(GMBUS0, 0);
}

J
Jesse Barnes 已提交
140 141 142 143 144 145
/**
 * intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
 * @dev: DRM device
 * @output: driver specific output device
 * @reg: GPIO reg to use
 * @name: name for this bus
146
 * @slave_addr: slave address (if fixed)
J
Jesse Barnes 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
 *
 * Creates and registers a new i2c bus with the Linux i2c layer, for use
 * in output probing and control (e.g. DDC or SDVO control functions).
 *
 * Possible values for @reg include:
 *   %GPIOA
 *   %GPIOB
 *   %GPIOC
 *   %GPIOD
 *   %GPIOE
 *   %GPIOF
 *   %GPIOG
 *   %GPIOH
 * see PRM for details on how these different busses are used.
 */
162 163
struct i2c_adapter *intel_i2c_create(struct intel_encoder *encoder,
				     const u32 reg,
164
				     const char *name)
J
Jesse Barnes 已提交
165 166
{
	struct intel_i2c_chan *chan;
167
	struct drm_device *dev = encoder->base.dev;
J
Jesse Barnes 已提交
168 169 170 171 172

	chan = kzalloc(sizeof(struct intel_i2c_chan), GFP_KERNEL);
	if (!chan)
		goto out_free;

173
	chan->encoder = encoder;
J
Jesse Barnes 已提交
174 175 176 177 178 179 180 181 182
	chan->reg = reg;
	snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
	chan->adapter.owner = THIS_MODULE;
	chan->adapter.algo_data	= &chan->algo;
	chan->adapter.dev.parent = &dev->pdev->dev;
	chan->algo.setsda = set_data;
	chan->algo.setscl = set_clock;
	chan->algo.getsda = get_data;
	chan->algo.getscl = get_clock;
183
	chan->algo.udelay = I2C_RISEFALL_TIME;
J
Jesse Barnes 已提交
184 185 186 187 188
	chan->algo.timeout = usecs_to_jiffies(2200);
	chan->algo.data = chan;

	i2c_set_adapdata(&chan->adapter, chan);

189
	if (i2c_bit_add_bus(&chan->adapter))
J
Jesse Barnes 已提交
190 191
		goto out_free;

192 193
	intel_i2c_reset_gmbus(dev);

J
Jesse Barnes 已提交
194
	/* JJJ:  raise SCL and SDA? */
195
	intel_i2c_quirk_set(dev, true);
J
Jesse Barnes 已提交
196
	set_data(chan, 1);
197
	udelay(I2C_RISEFALL_TIME);
J
Jesse Barnes 已提交
198
	set_clock(chan, 1);
199
	udelay(I2C_RISEFALL_TIME);
200
	intel_i2c_quirk_set(dev, false);
J
Jesse Barnes 已提交
201

202
	return &chan->adapter;
J
Jesse Barnes 已提交
203 204 205 206 207 208 209 210 211 212 213 214

out_free:
	kfree(chan);
	return NULL;
}

/**
 * intel_i2c_destroy - unregister and free i2c bus resources
 * @output: channel to free
 *
 * Unregister the adapter from the i2c layer, then free the structure.
 */
215
void intel_i2c_destroy(struct i2c_adapter *adapter)
J
Jesse Barnes 已提交
216
{
217 218 219
	struct intel_i2c_chan *chan;

	if (!adapter)
J
Jesse Barnes 已提交
220 221
		return;

222 223 224
	chan = container_of(adapter,
			    struct intel_i2c_chan,
			    adapter);
J
Jesse Barnes 已提交
225 226 227
	i2c_del_adapter(&chan->adapter);
	kfree(chan);
}