blob: 8324654037b65577fd4bdc65484576e9ceaf432a [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
Chris Wilsonf899fc62010-07-20 15:44:45 -07003 * Copyright © 2006-2008,2010 Intel Corporation
Jesse Barnes79e53942008-11-07 14:24:08 -08004 * 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>
Chris Wilsonf899fc62010-07-20 15:44:45 -070027 * Chris Wilson <chris@chris-wilson.co.uk>
Jesse Barnes79e53942008-11-07 14:24:08 -080028 */
29#include <linux/i2c.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c-algo-bit.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040031#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "i915_drv.h"
36
Jani Nikula5ea6e5e2015-04-01 10:55:04 +030037struct gmbus_pin {
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080038 const char *name;
39 int reg;
40};
41
Jani Nikula5ea6e5e2015-04-01 10:55:04 +030042/* Map gmbus pin pairs to names and registers. */
43static const struct gmbus_pin gmbus_pins[] = {
44 [GMBUS_PIN_SSC] = { "ssc", GPIOB },
45 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
46 [GMBUS_PIN_PANEL] = { "panel", GPIOC },
47 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
48 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
49 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080050};
51
Jani Nikulac1bad5b2015-05-06 15:33:43 +030052static const struct gmbus_pin gmbus_pins_bdw[] = {
53 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
54 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
55 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
56 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
57};
58
Jani Nikula6364e672015-05-06 15:33:44 +030059static const struct gmbus_pin gmbus_pins_skl[] = {
60 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
61 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
62 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
63};
64
Jani Nikula4c272832015-04-01 10:58:05 +030065static const struct gmbus_pin gmbus_pins_bxt[] = {
66 [GMBUS_PIN_1_BXT] = { "dpb", PCH_GPIOB },
67 [GMBUS_PIN_2_BXT] = { "dpc", PCH_GPIOC },
68 [GMBUS_PIN_3_BXT] = { "misc", PCH_GPIOD },
69};
70
71/* pin is expected to be valid */
72static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
73 unsigned int pin)
74{
75 if (IS_BROXTON(dev_priv))
76 return &gmbus_pins_bxt[pin];
Jani Nikula6364e672015-05-06 15:33:44 +030077 else if (IS_SKYLAKE(dev_priv))
78 return &gmbus_pins_skl[pin];
Jani Nikulac1bad5b2015-05-06 15:33:43 +030079 else if (IS_BROADWELL(dev_priv))
80 return &gmbus_pins_bdw[pin];
Jani Nikula4c272832015-04-01 10:58:05 +030081 else
82 return &gmbus_pins[pin];
83}
84
Jani Nikula88ac7932015-03-27 00:20:22 +020085bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
86 unsigned int pin)
87{
Jani Nikula4c272832015-04-01 10:58:05 +030088 unsigned int size;
89
90 if (IS_BROXTON(dev_priv))
91 size = ARRAY_SIZE(gmbus_pins_bxt);
Jani Nikula6364e672015-05-06 15:33:44 +030092 else if (IS_SKYLAKE(dev_priv))
93 size = ARRAY_SIZE(gmbus_pins_skl);
Jani Nikulac1bad5b2015-05-06 15:33:43 +030094 else if (IS_BROADWELL(dev_priv))
95 size = ARRAY_SIZE(gmbus_pins_bdw);
Jani Nikula4c272832015-04-01 10:58:05 +030096 else
97 size = ARRAY_SIZE(gmbus_pins);
98
99 return pin < size && get_gmbus_pin(dev_priv, pin)->reg;
Jani Nikula88ac7932015-03-27 00:20:22 +0200100}
101
Chris Wilsonf899fc62010-07-20 15:44:45 -0700102/* Intel GPIO access functions */
103
Jean Delvare1849ecb2012-01-28 11:07:09 +0100104#define I2C_RISEFALL_TIME 10
Chris Wilsonf899fc62010-07-20 15:44:45 -0700105
Chris Wilsone957d772010-09-24 12:52:03 +0100106static inline struct intel_gmbus *
107to_intel_gmbus(struct i2c_adapter *i2c)
108{
109 return container_of(i2c, struct intel_gmbus, adapter);
110}
111
Chris Wilsonf899fc62010-07-20 15:44:45 -0700112void
113intel_i2c_reset(struct drm_device *dev)
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800114{
115 struct drm_i915_private *dev_priv = dev->dev_private;
Chon Ming Lee24eb2d52013-09-27 15:31:00 +0800116
Ville Syrjälä699fc402015-09-18 20:03:38 +0300117 I915_WRITE(GMBUS0, 0);
118 I915_WRITE(GMBUS4, 0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700119}
120
121static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
122{
Chris Wilsonb222f262010-09-11 21:48:25 +0100123 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800124
125 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Chris Wilsonf899fc62010-07-20 15:44:45 -0700126 if (!IS_PINEVIEW(dev_priv->dev))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800127 return;
Chris Wilsonb222f262010-09-11 21:48:25 +0100128
129 val = I915_READ(DSPCLK_GATE_D);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800130 if (enable)
Chris Wilsonb222f262010-09-11 21:48:25 +0100131 val |= DPCUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800132 else
Chris Wilsonb222f262010-09-11 21:48:25 +0100133 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
134 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800135}
136
Daniel Vetter36c785f2012-02-14 22:37:22 +0100137static u32 get_reserved(struct intel_gmbus *bus)
Chris Wilsone957d772010-09-24 12:52:03 +0100138{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100139 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100140 struct drm_device *dev = dev_priv->dev;
141 u32 reserved = 0;
142
143 /* On most chips, these bits must be preserved in software. */
144 if (!IS_I830(dev) && !IS_845G(dev))
Daniel Vetter36c785f2012-02-14 22:37:22 +0100145 reserved = I915_READ_NOTRACE(bus->gpio_reg) &
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000146 (GPIO_DATA_PULLUP_DISABLE |
147 GPIO_CLOCK_PULLUP_DISABLE);
Chris Wilsone957d772010-09-24 12:52:03 +0100148
149 return reserved;
150}
151
Jesse Barnes79e53942008-11-07 14:24:08 -0800152static int get_clock(void *data)
153{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100154 struct intel_gmbus *bus = data;
155 struct drm_i915_private *dev_priv = bus->dev_priv;
156 u32 reserved = get_reserved(bus);
157 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
158 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
159 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800160}
161
162static int get_data(void *data)
163{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100164 struct intel_gmbus *bus = data;
165 struct drm_i915_private *dev_priv = bus->dev_priv;
166 u32 reserved = get_reserved(bus);
167 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
168 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
169 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800170}
171
172static void set_clock(void *data, int state_high)
173{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100174 struct intel_gmbus *bus = data;
175 struct drm_i915_private *dev_priv = bus->dev_priv;
176 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100177 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800178
179 if (state_high)
180 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
181 else
182 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
183 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700184
Daniel Vetter36c785f2012-02-14 22:37:22 +0100185 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
186 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800187}
188
189static void set_data(void *data, int state_high)
190{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100191 struct intel_gmbus *bus = data;
192 struct drm_i915_private *dev_priv = bus->dev_priv;
193 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100194 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800195
196 if (state_high)
197 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
198 else
199 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
200 GPIO_DATA_VAL_MASK;
201
Daniel Vetter36c785f2012-02-14 22:37:22 +0100202 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
203 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800204}
205
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800206static int
207intel_gpio_pre_xfer(struct i2c_adapter *adapter)
208{
209 struct intel_gmbus *bus = container_of(adapter,
210 struct intel_gmbus,
211 adapter);
212 struct drm_i915_private *dev_priv = bus->dev_priv;
213
214 intel_i2c_reset(dev_priv->dev);
215 intel_i2c_quirk_set(dev_priv, true);
216 set_data(bus, 1);
217 set_clock(bus, 1);
218 udelay(I2C_RISEFALL_TIME);
219 return 0;
220}
221
222static void
223intel_gpio_post_xfer(struct i2c_adapter *adapter)
224{
225 struct intel_gmbus *bus = container_of(adapter,
226 struct intel_gmbus,
227 adapter);
228 struct drm_i915_private *dev_priv = bus->dev_priv;
229
230 set_data(bus, 1);
231 set_clock(bus, 1);
232 intel_i2c_quirk_set(dev_priv, false);
233}
234
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800235static void
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300236intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800237{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100238 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100239 struct i2c_algo_bit_data *algo;
Eric Anholtf0217c42009-12-01 11:56:30 -0800240
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100241 algo = &bus->bit_algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100242
Jani Nikula4c272832015-04-01 10:58:05 +0300243 bus->gpio_reg = dev_priv->gpio_mmio_base +
244 get_gmbus_pin(dev_priv, pin)->reg;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700245
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100246 bus->adapter.algo_data = algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100247 algo->setsda = set_data;
248 algo->setscl = set_clock;
249 algo->getsda = get_data;
250 algo->getscl = get_clock;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800251 algo->pre_xfer = intel_gpio_pre_xfer;
252 algo->post_xfer = intel_gpio_post_xfer;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100253 algo->udelay = I2C_RISEFALL_TIME;
254 algo->timeout = usecs_to_jiffies(2200);
255 algo->data = bus;
Jesse Barnes79e53942008-11-07 14:24:08 -0800256}
257
Chris Wilsonf899fc62010-07-20 15:44:45 -0700258static int
Daniel Vetter61168c52012-12-01 13:53:43 +0100259gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
Daniel Vetter28c70f12012-12-01 13:53:45 +0100260 u32 gmbus2_status,
261 u32 gmbus4_irq_en)
Daniel Vetter61168c52012-12-01 13:53:43 +0100262{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100263 int i;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100264 u32 gmbus2 = 0;
265 DEFINE_WAIT(wait);
Daniel Vetter61168c52012-12-01 13:53:43 +0100266
Jiri Kosinac12aba52013-03-19 09:56:57 +0100267 if (!HAS_GMBUS_IRQ(dev_priv->dev))
268 gmbus4_irq_en = 0;
269
Daniel Vetter28c70f12012-12-01 13:53:45 +0100270 /* Important: The hw handles only the first bit, so set only one! Since
271 * we also need to check for NAKs besides the hw ready/idle signal, we
272 * need to wake up periodically and check that ourselves. */
Ville Syrjälä699fc402015-09-18 20:03:38 +0300273 I915_WRITE(GMBUS4, gmbus4_irq_en);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100274
Imre Deak2554fc12013-05-21 20:03:18 +0300275 for (i = 0; i < msecs_to_jiffies_timeout(50); i++) {
Daniel Vetter28c70f12012-12-01 13:53:45 +0100276 prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait,
277 TASK_UNINTERRUPTIBLE);
278
Ville Syrjälä699fc402015-09-18 20:03:38 +0300279 gmbus2 = I915_READ_NOTRACE(GMBUS2);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100280 if (gmbus2 & (GMBUS_SATOER | gmbus2_status))
281 break;
282
283 schedule_timeout(1);
284 }
285 finish_wait(&dev_priv->gmbus_wait_queue, &wait);
286
Ville Syrjälä699fc402015-09-18 20:03:38 +0300287 I915_WRITE(GMBUS4, 0);
Daniel Vetter61168c52012-12-01 13:53:43 +0100288
289 if (gmbus2 & GMBUS_SATOER)
290 return -ENXIO;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100291 if (gmbus2 & gmbus2_status)
292 return 0;
293 return -ETIMEDOUT;
Daniel Vetter61168c52012-12-01 13:53:43 +0100294}
295
296static int
Daniel Vetter2c438c02012-12-01 13:53:46 +0100297gmbus_wait_idle(struct drm_i915_private *dev_priv)
298{
299 int ret;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100300
Ville Syrjälä699fc402015-09-18 20:03:38 +0300301#define C ((I915_READ_NOTRACE(GMBUS2) & GMBUS_ACTIVE) == 0)
Daniel Vetter2c438c02012-12-01 13:53:46 +0100302
303 if (!HAS_GMBUS_IRQ(dev_priv->dev))
304 return wait_for(C, 10);
305
306 /* Important: The hw handles only the first bit, so set only one! */
Ville Syrjälä699fc402015-09-18 20:03:38 +0300307 I915_WRITE(GMBUS4, GMBUS_IDLE_EN);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100308
Imre Deak35987062013-05-21 20:03:20 +0300309 ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
310 msecs_to_jiffies_timeout(10));
Daniel Vetter2c438c02012-12-01 13:53:46 +0100311
Ville Syrjälä699fc402015-09-18 20:03:38 +0300312 I915_WRITE(GMBUS4, 0);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100313
314 if (ret)
315 return 0;
316 else
317 return -ETIMEDOUT;
318#undef C
319}
320
321static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700322gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
323 unsigned short addr, u8 *buf, unsigned int len,
324 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800325{
Ville Syrjälä699fc402015-09-18 20:03:38 +0300326 I915_WRITE(GMBUS1,
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800327 gmbus1_index |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800328 GMBUS_CYCLE_WAIT |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800329 (len << GMBUS_BYTE_COUNT_SHIFT) |
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700330 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800331 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800332 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800333 int ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800334 u32 val, loop = 0;
335
Daniel Vetter28c70f12012-12-01 13:53:45 +0100336 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
337 GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800338 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100339 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800340
Ville Syrjälä699fc402015-09-18 20:03:38 +0300341 val = I915_READ(GMBUS3);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800342 do {
343 *buf++ = val & 0xff;
344 val >>= 8;
345 } while (--len && ++loop < 4);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800346 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800347
348 return 0;
349}
350
351static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700352gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
353 u32 gmbus1_index)
354{
355 u8 *buf = msg->buf;
356 unsigned int rx_size = msg->len;
357 unsigned int len;
358 int ret;
359
360 do {
361 len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
362
363 ret = gmbus_xfer_read_chunk(dev_priv, msg->addr,
364 buf, len, gmbus1_index);
365 if (ret)
366 return ret;
367
368 rx_size -= len;
369 buf += len;
370 } while (rx_size != 0);
371
372 return 0;
373}
374
375static int
376gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
377 unsigned short addr, u8 *buf, unsigned int len)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800378{
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700379 unsigned int chunk_size = len;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800380 u32 val, loop;
381
382 val = loop = 0;
Daniel Kurtz26883c32012-03-30 19:46:36 +0800383 while (len && loop < 4) {
384 val |= *buf++ << (8 * loop++);
385 len -= 1;
386 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800387
Ville Syrjälä699fc402015-09-18 20:03:38 +0300388 I915_WRITE(GMBUS3, val);
389 I915_WRITE(GMBUS1,
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800390 GMBUS_CYCLE_WAIT |
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700391 (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
392 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800393 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800394 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800395 int ret;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800396
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800397 val = loop = 0;
398 do {
399 val |= *buf++ << (8 * loop);
400 } while (--len && ++loop < 4);
401
Ville Syrjälä699fc402015-09-18 20:03:38 +0300402 I915_WRITE(GMBUS3, val);
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800403
Daniel Vetter28c70f12012-12-01 13:53:45 +0100404 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
405 GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800406 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100407 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800408 }
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700409
410 return 0;
411}
412
413static int
414gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
415{
416 u8 *buf = msg->buf;
417 unsigned int tx_size = msg->len;
418 unsigned int len;
419 int ret;
420
421 do {
422 len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
423
424 ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len);
425 if (ret)
426 return ret;
427
428 buf += len;
429 tx_size -= len;
430 } while (tx_size != 0);
431
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800432 return 0;
433}
434
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800435/*
436 * The gmbus controller can combine a 1 or 2 byte write with a read that
437 * immediately follows it by using an "INDEX" cycle.
438 */
439static bool
440gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
441{
442 return (i + 1 < num &&
443 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
444 (msgs[i + 1].flags & I2C_M_RD));
445}
446
447static int
448gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
449{
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800450 u32 gmbus1_index = 0;
451 u32 gmbus5 = 0;
452 int ret;
453
454 if (msgs[0].len == 2)
455 gmbus5 = GMBUS_2BYTE_INDEX_EN |
456 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
457 if (msgs[0].len == 1)
458 gmbus1_index = GMBUS_CYCLE_INDEX |
459 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
460
461 /* GMBUS5 holds 16-bit index */
462 if (gmbus5)
Ville Syrjälä699fc402015-09-18 20:03:38 +0300463 I915_WRITE(GMBUS5, gmbus5);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800464
465 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
466
467 /* Clear GMBUS5 after each index transfer */
468 if (gmbus5)
Ville Syrjälä699fc402015-09-18 20:03:38 +0300469 I915_WRITE(GMBUS5, 0);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800470
471 return ret;
472}
473
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800474static int
Chris Wilsonf899fc62010-07-20 15:44:45 -0700475gmbus_xfer(struct i2c_adapter *adapter,
476 struct i2c_msg *msgs,
477 int num)
478{
479 struct intel_gmbus *bus = container_of(adapter,
480 struct intel_gmbus,
481 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100482 struct drm_i915_private *dev_priv = bus->dev_priv;
Ville Syrjälä699fc402015-09-18 20:03:38 +0300483 int i = 0, inc, try = 0;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800484 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700485
Ville Syrjäläac9b8232015-11-27 18:55:26 +0200486 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500487 mutex_lock(&dev_priv->gmbus_mutex);
488
489 if (bus->force_bit) {
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800490 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500491 goto out;
492 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700493
Jani Nikula3f5f1552015-06-02 19:21:15 +0300494retry:
Ville Syrjälä699fc402015-09-18 20:03:38 +0300495 I915_WRITE(GMBUS0, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700496
Jani Nikula3f5f1552015-06-02 19:21:15 +0300497 for (; i < num; i += inc) {
498 inc = 1;
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800499 if (gmbus_is_index_read(msgs, i, num)) {
500 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
Jani Nikula3f5f1552015-06-02 19:21:15 +0300501 inc = 2; /* an index read is two msgs */
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800502 } else if (msgs[i].flags & I2C_M_RD) {
503 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
504 } else {
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800505 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800506 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700507
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800508 if (ret == -ETIMEDOUT)
509 goto timeout;
510 if (ret == -ENXIO)
511 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700512
Daniel Vetter28c70f12012-12-01 13:53:45 +0100513 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
514 GMBUS_HW_WAIT_EN);
Daniel Vetter61168c52012-12-01 13:53:43 +0100515 if (ret == -ENXIO)
516 goto clear_err;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800517 if (ret)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700518 goto timeout;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700519 }
520
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800521 /* Generate a STOP condition on the bus. Note that gmbus can't generata
522 * a STOP on the very first cycle. To simplify the code we
523 * unconditionally generate the STOP condition with an additional gmbus
524 * cycle. */
Ville Syrjälä699fc402015-09-18 20:03:38 +0300525 I915_WRITE(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800526
Benson Leungcaae7452012-02-09 12:03:17 -0800527 /* Mark the GMBUS interface as disabled after waiting for idle.
528 * We will re-enable it at the start of the next xfer,
529 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100530 */
Daniel Vetter2c438c02012-12-01 13:53:46 +0100531 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800532 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800533 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800534 ret = -ETIMEDOUT;
535 }
Ville Syrjälä699fc402015-09-18 20:03:38 +0300536 I915_WRITE(GMBUS0, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800537 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500538 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700539
Daniel Kurtze646d572012-03-30 19:46:38 +0800540clear_err:
541 /*
542 * Wait for bus to IDLE before clearing NAK.
543 * If we clear the NAK while bus is still active, then it will stay
544 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200545 *
546 * If no ACK is received during the address phase of a transaction, the
547 * adapter must report -ENXIO. It is not clear what to return if no ACK
548 * is received at other times. But we have to be careful to not return
549 * spurious -ENXIO because that will prevent i2c and drm edid functions
550 * from retrying. So return -ENXIO only when gmbus properly quiescents -
551 * timing out seems to happen when there _is_ a ddc chip present, but
552 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800553 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200554 ret = -ENXIO;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100555 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800556 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
557 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200558 ret = -ETIMEDOUT;
559 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800560
561 /* Toggle the Software Clear Interrupt bit. This has the effect
562 * of resetting the GMBUS controller and so clearing the
563 * BUS_ERROR raised by the slave's NAK.
564 */
Ville Syrjälä699fc402015-09-18 20:03:38 +0300565 I915_WRITE(GMBUS1, GMBUS_SW_CLR_INT);
566 I915_WRITE(GMBUS1, 0);
567 I915_WRITE(GMBUS0, 0);
Daniel Kurtze646d572012-03-30 19:46:38 +0800568
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800569 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800570 adapter->name, msgs[i].addr,
571 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
572
Jani Nikula3f5f1552015-06-02 19:21:15 +0300573 /*
574 * Passive adapters sometimes NAK the first probe. Retry the first
575 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
576 * has retries internally. See also the retry loop in
577 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
578 */
579 if (ret == -ENXIO && i == 0 && try++ == 0) {
580 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
581 adapter->name);
582 goto retry;
583 }
584
Daniel Kurtze646d572012-03-30 19:46:38 +0800585 goto out;
586
Chris Wilsonf899fc62010-07-20 15:44:45 -0700587timeout:
Daniel Kurtz874e3cc2012-03-28 02:36:11 +0800588 DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
589 bus->adapter.name, bus->reg0 & 0xff);
Ville Syrjälä699fc402015-09-18 20:03:38 +0300590 I915_WRITE(GMBUS0, 0);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100591
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800592 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000593 bus->force_bit = 1;
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800594 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800595
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500596out:
597 mutex_unlock(&dev_priv->gmbus_mutex);
Ville Syrjäläac9b8232015-11-27 18:55:26 +0200598
599 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
600
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500601 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700602}
603
604static u32 gmbus_func(struct i2c_adapter *adapter)
605{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100606 return i2c_bit_algo.functionality(adapter) &
607 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700608 /* I2C_FUNC_10BIT_ADDR | */
609 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
610 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
611}
612
613static const struct i2c_algorithm gmbus_algorithm = {
614 .master_xfer = gmbus_xfer,
615 .functionality = gmbus_func
616};
617
618/**
619 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
620 * @dev: DRM device
621 */
622int intel_setup_gmbus(struct drm_device *dev)
623{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700624 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300625 struct intel_gmbus *bus;
626 unsigned int pin;
627 int ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700628
Ben Widawskyab5c6082013-04-05 13:12:41 -0700629 if (HAS_PCH_NOP(dev))
630 return 0;
631 else if (HAS_PCH_SPLIT(dev))
Daniel Vetter110447fc2012-03-23 23:43:36 +0100632 dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
Ville Syrjäläd8112152013-01-24 15:29:55 +0200633 else if (IS_VALLEYVIEW(dev))
634 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
Daniel Vetter110447fc2012-03-23 23:43:36 +0100635 else
636 dev_priv->gpio_mmio_base = 0;
637
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500638 mutex_init(&dev_priv->gmbus_mutex);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100639 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500640
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300641 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200642 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300643 continue;
644
645 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700646
647 bus->adapter.owner = THIS_MODULE;
648 bus->adapter.class = I2C_CLASS_DDC;
649 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100650 sizeof(bus->adapter.name),
651 "i915 gmbus %s",
Jani Nikula4c272832015-04-01 10:58:05 +0300652 get_gmbus_pin(dev_priv, pin)->name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700653
654 bus->adapter.dev.parent = &dev->pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100655 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700656
657 bus->adapter.algo = &gmbus_algorithm;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700658
Chris Wilsone957d772010-09-24 12:52:03 +0100659 /* By default use a conservative clock rate */
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300660 bus->reg0 = pin | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100661
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200662 /* gmbus seems to be broken on i830 */
663 if (IS_I830(dev))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000664 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200665
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300666 intel_gpio_setup(bus, pin);
Jani Nikulacee25162012-08-13 17:33:02 +0300667
668 ret = i2c_add_adapter(&bus->adapter);
669 if (ret)
670 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700671 }
672
673 intel_i2c_reset(dev_priv->dev);
674
675 return 0;
676
677err:
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300678 while (--pin) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200679 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300680 continue;
681
682 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700683 i2c_del_adapter(&bus->adapter);
684 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700685 return ret;
686}
687
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800688struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
Jani Nikula0184df42015-03-27 00:20:20 +0200689 unsigned int pin)
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800690{
Jani Nikula88ac7932015-03-27 00:20:22 +0200691 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300692 return NULL;
693
694 return &dev_priv->gmbus[pin].adapter;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800695}
696
Chris Wilsone957d772010-09-24 12:52:03 +0100697void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
698{
699 struct intel_gmbus *bus = to_intel_gmbus(adapter);
700
Adam Jacksond5090b92011-06-16 16:36:28 -0400701 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100702}
703
704void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
705{
706 struct intel_gmbus *bus = to_intel_gmbus(adapter);
707
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000708 bus->force_bit += force_bit ? 1 : -1;
709 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
710 force_bit ? "en" : "dis", adapter->name,
711 bus->force_bit);
Chris Wilsone957d772010-09-24 12:52:03 +0100712}
713
Chris Wilsonf899fc62010-07-20 15:44:45 -0700714void intel_teardown_gmbus(struct drm_device *dev)
715{
716 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300717 struct intel_gmbus *bus;
718 unsigned int pin;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700719
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300720 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200721 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300722 continue;
723
724 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700725 i2c_del_adapter(&bus->adapter);
726 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800727}