blob: de03989d6df3e1ff04cd4bcc0b05f199e14f9fcd [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2008 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
5 *
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:
12 *
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
15 * Software.
16 *
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.
24 *
25 * Authors:
26 * Eric Anholt <eric@anholt.net>
27 */
28#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c-id.h>
31#include <linux/i2c-algo-bit.h>
32#include "drmP.h"
33#include "drm.h"
34#include "intel_drv.h"
35#include "i915_drm.h"
36#include "i915_drv.h"
37
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080038void intel_i2c_quirk_set(struct drm_device *dev, bool enable)
39{
40 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb222f262010-09-11 21:48:25 +010041 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080042
43 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -050044 if (!IS_PINEVIEW(dev))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080045 return;
Chris Wilsonb222f262010-09-11 21:48:25 +010046
47 val = I915_READ(DSPCLK_GATE_D);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080048 if (enable)
Chris Wilsonb222f262010-09-11 21:48:25 +010049 val |= DPCUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080050 else
Chris Wilsonb222f262010-09-11 21:48:25 +010051 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
52 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080053}
54
Jesse Barnes79e53942008-11-07 14:24:08 -080055/*
56 * Intel GPIO access functions
57 */
58
59#define I2C_RISEFALL_TIME 20
60
61static int get_clock(void *data)
62{
63 struct intel_i2c_chan *chan = data;
64 struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
Chris Wilsonb222f262010-09-11 21:48:25 +010065 return (I915_READ(chan->reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080066}
67
68static int get_data(void *data)
69{
70 struct intel_i2c_chan *chan = data;
71 struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
Chris Wilsonb222f262010-09-11 21:48:25 +010072 return (I915_READ(chan->reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080073}
74
75static void set_clock(void *data, int state_high)
76{
77 struct intel_i2c_chan *chan = data;
78 struct drm_device *dev = chan->drm_dev;
79 struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
80 u32 reserved = 0, clock_bits;
81
82 /* On most chips, these bits must be preserved in software. */
83 if (!IS_I830(dev) && !IS_845G(dev))
84 reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
85 GPIO_CLOCK_PULLUP_DISABLE);
86
87 if (state_high)
88 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
89 else
90 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
91 GPIO_CLOCK_VAL_MASK;
92 I915_WRITE(chan->reg, reserved | clock_bits);
Chris Wilsonb222f262010-09-11 21:48:25 +010093 POSTING_READ(chan->reg);
Jesse Barnes79e53942008-11-07 14:24:08 -080094}
95
96static void set_data(void *data, int state_high)
97{
98 struct intel_i2c_chan *chan = data;
99 struct drm_device *dev = chan->drm_dev;
100 struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
101 u32 reserved = 0, data_bits;
102
103 /* On most chips, these bits must be preserved in software. */
104 if (!IS_I830(dev) && !IS_845G(dev))
105 reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
106 GPIO_CLOCK_PULLUP_DISABLE);
107
108 if (state_high)
109 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
110 else
111 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
112 GPIO_DATA_VAL_MASK;
113
114 I915_WRITE(chan->reg, reserved | data_bits);
Chris Wilsonb222f262010-09-11 21:48:25 +0100115 POSTING_READ(chan->reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800116}
117
Eric Anholtf0217c42009-12-01 11:56:30 -0800118/* Clears the GMBUS setup. Our driver doesn't make use of the GMBUS I2C
119 * engine, but if the BIOS leaves it enabled, then that can break our use
120 * of the bit-banging I2C interfaces. This is notably the case with the
121 * Mac Mini in EFI mode.
122 */
123void
124intel_i2c_reset_gmbus(struct drm_device *dev)
125{
126 struct drm_i915_private *dev_priv = dev->dev_private;
127
Chris Wilsonb222f262010-09-11 21:48:25 +0100128 if (HAS_PCH_SPLIT(dev))
Eric Anholtf0217c42009-12-01 11:56:30 -0800129 I915_WRITE(PCH_GMBUS0, 0);
Chris Wilsonb222f262010-09-11 21:48:25 +0100130 else
Eric Anholtf0217c42009-12-01 11:56:30 -0800131 I915_WRITE(GMBUS0, 0);
Eric Anholtf0217c42009-12-01 11:56:30 -0800132}
133
Jesse Barnes79e53942008-11-07 14:24:08 -0800134/**
135 * intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
136 * @dev: DRM device
137 * @output: driver specific output device
138 * @reg: GPIO reg to use
139 * @name: name for this bus
Keith Packardf9c10a92009-05-30 12:16:25 -0700140 * @slave_addr: slave address (if fixed)
Jesse Barnes79e53942008-11-07 14:24:08 -0800141 *
142 * Creates and registers a new i2c bus with the Linux i2c layer, for use
143 * in output probing and control (e.g. DDC or SDVO control functions).
144 *
145 * Possible values for @reg include:
146 * %GPIOA
147 * %GPIOB
148 * %GPIOC
149 * %GPIOD
150 * %GPIOE
151 * %GPIOF
152 * %GPIOG
153 * %GPIOH
154 * see PRM for details on how these different busses are used.
155 */
Keith Packardf9c10a92009-05-30 12:16:25 -0700156struct i2c_adapter *intel_i2c_create(struct drm_device *dev, const u32 reg,
157 const char *name)
Jesse Barnes79e53942008-11-07 14:24:08 -0800158{
159 struct intel_i2c_chan *chan;
160
161 chan = kzalloc(sizeof(struct intel_i2c_chan), GFP_KERNEL);
162 if (!chan)
163 goto out_free;
164
165 chan->drm_dev = dev;
166 chan->reg = reg;
167 snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
168 chan->adapter.owner = THIS_MODULE;
Jesse Barnes79e53942008-11-07 14:24:08 -0800169 chan->adapter.algo_data = &chan->algo;
170 chan->adapter.dev.parent = &dev->pdev->dev;
171 chan->algo.setsda = set_data;
172 chan->algo.setscl = set_clock;
173 chan->algo.getsda = get_data;
174 chan->algo.getscl = get_clock;
Chris Wilsonb222f262010-09-11 21:48:25 +0100175 chan->algo.udelay = I2C_RISEFALL_TIME;
Jesse Barnes79e53942008-11-07 14:24:08 -0800176 chan->algo.timeout = usecs_to_jiffies(2200);
177 chan->algo.data = chan;
178
179 i2c_set_adapdata(&chan->adapter, chan);
180
181 if(i2c_bit_add_bus(&chan->adapter))
182 goto out_free;
183
Eric Anholtf0217c42009-12-01 11:56:30 -0800184 intel_i2c_reset_gmbus(dev);
185
Jesse Barnes79e53942008-11-07 14:24:08 -0800186 /* JJJ: raise SCL and SDA? */
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800187 intel_i2c_quirk_set(dev, true);
Jesse Barnes79e53942008-11-07 14:24:08 -0800188 set_data(chan, 1);
Chris Wilsonb222f262010-09-11 21:48:25 +0100189 udelay(I2C_RISEFALL_TIME);
Jesse Barnes79e53942008-11-07 14:24:08 -0800190 set_clock(chan, 1);
Chris Wilsonb222f262010-09-11 21:48:25 +0100191 udelay(I2C_RISEFALL_TIME);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800192 intel_i2c_quirk_set(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -0800193
Keith Packardf9c10a92009-05-30 12:16:25 -0700194 return &chan->adapter;
Jesse Barnes79e53942008-11-07 14:24:08 -0800195
196out_free:
197 kfree(chan);
198 return NULL;
199}
200
201/**
202 * intel_i2c_destroy - unregister and free i2c bus resources
203 * @output: channel to free
204 *
205 * Unregister the adapter from the i2c layer, then free the structure.
206 */
Keith Packardf9c10a92009-05-30 12:16:25 -0700207void intel_i2c_destroy(struct i2c_adapter *adapter)
Jesse Barnes79e53942008-11-07 14:24:08 -0800208{
Keith Packardf9c10a92009-05-30 12:16:25 -0700209 struct intel_i2c_chan *chan;
210
211 if (!adapter)
Jesse Barnes79e53942008-11-07 14:24:08 -0800212 return;
213
Keith Packardf9c10a92009-05-30 12:16:25 -0700214 chan = container_of(adapter,
215 struct intel_i2c_chan,
216 adapter);
Jesse Barnes79e53942008-11-07 14:24:08 -0800217 i2c_del_adapter(&chan->adapter);
218 kfree(chan);
219}