blob: 79aab9ad6faa826416abd8004f35ce78d9f62c2c [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;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020039 i915_reg_t reg;
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080040};
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[] = {
Ville Syrjäläb2e8c6c2015-11-04 23:20:00 +020066 [GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
67 [GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
68 [GMBUS_PIN_3_BXT] = { "misc", GPIOD },
Jani Nikula4c272832015-04-01 10:58:05 +030069};
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];
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070077 else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Jani Nikula6364e672015-05-06 15:33:44 +030078 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);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070092 else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Jani Nikula6364e672015-05-06 15:33:44 +030093 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
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020099 return pin < size &&
100 i915_mmio_reg_valid(get_gmbus_pin(dev_priv, pin)->reg);
Jani Nikula88ac7932015-03-27 00:20:22 +0200101}
102
Chris Wilsonf899fc62010-07-20 15:44:45 -0700103/* Intel GPIO access functions */
104
Jean Delvare1849ecb2012-01-28 11:07:09 +0100105#define I2C_RISEFALL_TIME 10
Chris Wilsonf899fc62010-07-20 15:44:45 -0700106
Chris Wilsone957d772010-09-24 12:52:03 +0100107static inline struct intel_gmbus *
108to_intel_gmbus(struct i2c_adapter *i2c)
109{
110 return container_of(i2c, struct intel_gmbus, adapter);
111}
112
Chris Wilsonf899fc62010-07-20 15:44:45 -0700113void
114intel_i2c_reset(struct drm_device *dev)
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800115{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100116 struct drm_i915_private *dev_priv = to_i915(dev);
Chon Ming Lee24eb2d52013-09-27 15:31:00 +0800117
Ville Syrjälä699fc402015-09-18 20:03:38 +0300118 I915_WRITE(GMBUS0, 0);
119 I915_WRITE(GMBUS4, 0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700120}
121
122static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
123{
Chris Wilsonb222f262010-09-11 21:48:25 +0100124 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800125
126 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +0300127 if (!IS_PINEVIEW(dev_priv))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800128 return;
Chris Wilsonb222f262010-09-11 21:48:25 +0100129
130 val = I915_READ(DSPCLK_GATE_D);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800131 if (enable)
Chris Wilsonb222f262010-09-11 21:48:25 +0100132 val |= DPCUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800133 else
Chris Wilsonb222f262010-09-11 21:48:25 +0100134 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
135 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800136}
137
Daniel Vetter36c785f2012-02-14 22:37:22 +0100138static u32 get_reserved(struct intel_gmbus *bus)
Chris Wilsone957d772010-09-24 12:52:03 +0100139{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100140 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilson91c8a322016-07-05 10:40:23 +0100141 struct drm_device *dev = &dev_priv->drm;
Chris Wilsone957d772010-09-24 12:52:03 +0100142 u32 reserved = 0;
143
144 /* On most chips, these bits must be preserved in software. */
145 if (!IS_I830(dev) && !IS_845G(dev))
Daniel Vetter36c785f2012-02-14 22:37:22 +0100146 reserved = I915_READ_NOTRACE(bus->gpio_reg) &
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000147 (GPIO_DATA_PULLUP_DISABLE |
148 GPIO_CLOCK_PULLUP_DISABLE);
Chris Wilsone957d772010-09-24 12:52:03 +0100149
150 return reserved;
151}
152
Jesse Barnes79e53942008-11-07 14:24:08 -0800153static int get_clock(void *data)
154{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100155 struct intel_gmbus *bus = data;
156 struct drm_i915_private *dev_priv = bus->dev_priv;
157 u32 reserved = get_reserved(bus);
158 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
159 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
160 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800161}
162
163static int get_data(void *data)
164{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100165 struct intel_gmbus *bus = data;
166 struct drm_i915_private *dev_priv = bus->dev_priv;
167 u32 reserved = get_reserved(bus);
168 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
169 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
170 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800171}
172
173static void set_clock(void *data, int state_high)
174{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100175 struct intel_gmbus *bus = data;
176 struct drm_i915_private *dev_priv = bus->dev_priv;
177 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100178 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800179
180 if (state_high)
181 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
182 else
183 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
184 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700185
Daniel Vetter36c785f2012-02-14 22:37:22 +0100186 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
187 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800188}
189
190static void set_data(void *data, int state_high)
191{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100192 struct intel_gmbus *bus = data;
193 struct drm_i915_private *dev_priv = bus->dev_priv;
194 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100195 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800196
197 if (state_high)
198 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
199 else
200 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
201 GPIO_DATA_VAL_MASK;
202
Daniel Vetter36c785f2012-02-14 22:37:22 +0100203 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
204 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800205}
206
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800207static int
208intel_gpio_pre_xfer(struct i2c_adapter *adapter)
209{
210 struct intel_gmbus *bus = container_of(adapter,
211 struct intel_gmbus,
212 adapter);
213 struct drm_i915_private *dev_priv = bus->dev_priv;
214
Chris Wilson91c8a322016-07-05 10:40:23 +0100215 intel_i2c_reset(&dev_priv->drm);
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800216 intel_i2c_quirk_set(dev_priv, true);
217 set_data(bus, 1);
218 set_clock(bus, 1);
219 udelay(I2C_RISEFALL_TIME);
220 return 0;
221}
222
223static void
224intel_gpio_post_xfer(struct i2c_adapter *adapter)
225{
226 struct intel_gmbus *bus = container_of(adapter,
227 struct intel_gmbus,
228 adapter);
229 struct drm_i915_private *dev_priv = bus->dev_priv;
230
231 set_data(bus, 1);
232 set_clock(bus, 1);
233 intel_i2c_quirk_set(dev_priv, false);
234}
235
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800236static void
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300237intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800238{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100239 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100240 struct i2c_algo_bit_data *algo;
Eric Anholtf0217c42009-12-01 11:56:30 -0800241
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100242 algo = &bus->bit_algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100243
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200244 bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base +
245 i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg));
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 Wilson4e6c2d52016-08-19 17:45:02 +0100258static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en)
Daniel Vetter61168c52012-12-01 13:53:43 +0100259{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100260 DEFINE_WAIT(wait);
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100261 u32 gmbus2;
262 int ret;
Jiri Kosinac12aba52013-03-19 09:56:57 +0100263
Daniel Vetter28c70f12012-12-01 13:53:45 +0100264 /* Important: The hw handles only the first bit, so set only one! Since
265 * we also need to check for NAKs besides the hw ready/idle signal, we
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100266 * need to wake up periodically and check that ourselves.
267 */
268 if (!HAS_GMBUS_IRQ(dev_priv))
269 irq_en = 0;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100270
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100271 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
272 I915_WRITE_FW(GMBUS4, irq_en);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100273
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100274 status |= GMBUS_SATOER;
275 ret = wait_for_us((gmbus2 = I915_READ_FW(GMBUS2)) & status, 2);
276 if (ret)
277 ret = wait_for((gmbus2 = I915_READ_FW(GMBUS2)) & status, 50);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100278
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100279 I915_WRITE_FW(GMBUS4, 0);
280 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
Daniel Vetter61168c52012-12-01 13:53:43 +0100281
282 if (gmbus2 & GMBUS_SATOER)
283 return -ENXIO;
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100284
285 return ret;
Daniel Vetter61168c52012-12-01 13:53:43 +0100286}
287
288static int
Daniel Vetter2c438c02012-12-01 13:53:46 +0100289gmbus_wait_idle(struct drm_i915_private *dev_priv)
290{
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100291 DEFINE_WAIT(wait);
292 u32 irq_enable;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100293 int ret;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100294
Daniel Vetter2c438c02012-12-01 13:53:46 +0100295 /* Important: The hw handles only the first bit, so set only one! */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100296 irq_enable = 0;
297 if (HAS_GMBUS_IRQ(dev_priv))
298 irq_enable = GMBUS_IDLE_EN;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100299
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100300 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
301 I915_WRITE_FW(GMBUS4, irq_enable);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100302
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100303 ret = intel_wait_for_register_fw(dev_priv,
304 GMBUS2, GMBUS_ACTIVE, 0,
305 10);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100306
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100307 I915_WRITE_FW(GMBUS4, 0);
308 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
309
310 return ret;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100311}
312
313static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700314gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
315 unsigned short addr, u8 *buf, unsigned int len,
316 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800317{
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100318 I915_WRITE_FW(GMBUS1,
319 gmbus1_index |
320 GMBUS_CYCLE_WAIT |
321 (len << GMBUS_BYTE_COUNT_SHIFT) |
322 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
323 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800324 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800325 int ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800326 u32 val, loop = 0;
327
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100328 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800329 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100330 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800331
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100332 val = I915_READ_FW(GMBUS3);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800333 do {
334 *buf++ = val & 0xff;
335 val >>= 8;
336 } while (--len && ++loop < 4);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800337 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800338
339 return 0;
340}
341
342static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700343gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
344 u32 gmbus1_index)
345{
346 u8 *buf = msg->buf;
347 unsigned int rx_size = msg->len;
348 unsigned int len;
349 int ret;
350
351 do {
352 len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
353
354 ret = gmbus_xfer_read_chunk(dev_priv, msg->addr,
355 buf, len, gmbus1_index);
356 if (ret)
357 return ret;
358
359 rx_size -= len;
360 buf += len;
361 } while (rx_size != 0);
362
363 return 0;
364}
365
366static int
367gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
368 unsigned short addr, u8 *buf, unsigned int len)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800369{
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700370 unsigned int chunk_size = len;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800371 u32 val, loop;
372
373 val = loop = 0;
Daniel Kurtz26883c32012-03-30 19:46:36 +0800374 while (len && loop < 4) {
375 val |= *buf++ << (8 * loop++);
376 len -= 1;
377 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800378
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100379 I915_WRITE_FW(GMBUS3, val);
380 I915_WRITE_FW(GMBUS1,
381 GMBUS_CYCLE_WAIT |
382 (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
383 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
384 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800385 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800386 int ret;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800387
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800388 val = loop = 0;
389 do {
390 val |= *buf++ << (8 * loop);
391 } while (--len && ++loop < 4);
392
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100393 I915_WRITE_FW(GMBUS3, val);
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800394
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100395 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800396 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100397 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800398 }
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700399
400 return 0;
401}
402
403static int
404gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
405{
406 u8 *buf = msg->buf;
407 unsigned int tx_size = msg->len;
408 unsigned int len;
409 int ret;
410
411 do {
412 len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
413
414 ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len);
415 if (ret)
416 return ret;
417
418 buf += len;
419 tx_size -= len;
420 } while (tx_size != 0);
421
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800422 return 0;
423}
424
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800425/*
426 * The gmbus controller can combine a 1 or 2 byte write with a read that
427 * immediately follows it by using an "INDEX" cycle.
428 */
429static bool
430gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
431{
432 return (i + 1 < num &&
433 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
434 (msgs[i + 1].flags & I2C_M_RD));
435}
436
437static int
438gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
439{
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800440 u32 gmbus1_index = 0;
441 u32 gmbus5 = 0;
442 int ret;
443
444 if (msgs[0].len == 2)
445 gmbus5 = GMBUS_2BYTE_INDEX_EN |
446 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
447 if (msgs[0].len == 1)
448 gmbus1_index = GMBUS_CYCLE_INDEX |
449 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
450
451 /* GMBUS5 holds 16-bit index */
452 if (gmbus5)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100453 I915_WRITE_FW(GMBUS5, gmbus5);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800454
455 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
456
457 /* Clear GMBUS5 after each index transfer */
458 if (gmbus5)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100459 I915_WRITE_FW(GMBUS5, 0);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800460
461 return ret;
462}
463
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800464static int
Jani Nikulabffce902015-12-01 16:29:26 +0200465do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700466{
467 struct intel_gmbus *bus = container_of(adapter,
468 struct intel_gmbus,
469 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100470 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100471 const unsigned int fw =
472 intel_uncore_forcewake_for_reg(dev_priv, GMBUS0,
473 FW_REG_READ | FW_REG_WRITE);
Ville Syrjälä699fc402015-09-18 20:03:38 +0300474 int i = 0, inc, try = 0;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800475 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700476
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100477 intel_uncore_forcewake_get(dev_priv, fw);
Jani Nikula3f5f1552015-06-02 19:21:15 +0300478retry:
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100479 I915_WRITE_FW(GMBUS0, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700480
Jani Nikula3f5f1552015-06-02 19:21:15 +0300481 for (; i < num; i += inc) {
482 inc = 1;
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800483 if (gmbus_is_index_read(msgs, i, num)) {
484 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
Jani Nikula3f5f1552015-06-02 19:21:15 +0300485 inc = 2; /* an index read is two msgs */
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800486 } else if (msgs[i].flags & I2C_M_RD) {
487 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
488 } else {
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800489 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800490 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700491
Jani Nikula0aeb9042015-12-01 16:29:25 +0200492 if (!ret)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100493 ret = gmbus_wait(dev_priv,
494 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800495 if (ret == -ETIMEDOUT)
496 goto timeout;
Jani Nikula0aeb9042015-12-01 16:29:25 +0200497 else if (ret)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800498 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700499 }
500
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800501 /* Generate a STOP condition on the bus. Note that gmbus can't generata
502 * a STOP on the very first cycle. To simplify the code we
503 * unconditionally generate the STOP condition with an additional gmbus
504 * cycle. */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100505 I915_WRITE_FW(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800506
Benson Leungcaae7452012-02-09 12:03:17 -0800507 /* Mark the GMBUS interface as disabled after waiting for idle.
508 * We will re-enable it at the start of the next xfer,
509 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100510 */
Daniel Vetter2c438c02012-12-01 13:53:46 +0100511 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800512 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800513 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800514 ret = -ETIMEDOUT;
515 }
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100516 I915_WRITE_FW(GMBUS0, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800517 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500518 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700519
Daniel Kurtze646d572012-03-30 19:46:38 +0800520clear_err:
521 /*
522 * Wait for bus to IDLE before clearing NAK.
523 * If we clear the NAK while bus is still active, then it will stay
524 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200525 *
526 * If no ACK is received during the address phase of a transaction, the
527 * adapter must report -ENXIO. It is not clear what to return if no ACK
528 * is received at other times. But we have to be careful to not return
529 * spurious -ENXIO because that will prevent i2c and drm edid functions
530 * from retrying. So return -ENXIO only when gmbus properly quiescents -
531 * timing out seems to happen when there _is_ a ddc chip present, but
532 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800533 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200534 ret = -ENXIO;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100535 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800536 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
537 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200538 ret = -ETIMEDOUT;
539 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800540
541 /* Toggle the Software Clear Interrupt bit. This has the effect
542 * of resetting the GMBUS controller and so clearing the
543 * BUS_ERROR raised by the slave's NAK.
544 */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100545 I915_WRITE_FW(GMBUS1, GMBUS_SW_CLR_INT);
546 I915_WRITE_FW(GMBUS1, 0);
547 I915_WRITE_FW(GMBUS0, 0);
Daniel Kurtze646d572012-03-30 19:46:38 +0800548
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800549 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800550 adapter->name, msgs[i].addr,
551 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
552
Jani Nikula3f5f1552015-06-02 19:21:15 +0300553 /*
554 * Passive adapters sometimes NAK the first probe. Retry the first
555 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
556 * has retries internally. See also the retry loop in
557 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
558 */
559 if (ret == -ENXIO && i == 0 && try++ == 0) {
560 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
561 adapter->name);
562 goto retry;
563 }
564
Daniel Kurtze646d572012-03-30 19:46:38 +0800565 goto out;
566
Chris Wilsonf899fc62010-07-20 15:44:45 -0700567timeout:
Ville Syrjälä70677802016-03-07 17:57:00 +0200568 DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
569 bus->adapter.name, bus->reg0 & 0xff);
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100570 I915_WRITE_FW(GMBUS0, 0);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100571
Jani Nikulabffce902015-12-01 16:29:26 +0200572 /*
573 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
574 * instead. Use EAGAIN to have i2c core retry.
575 */
Jani Nikulabffce902015-12-01 16:29:26 +0200576 ret = -EAGAIN;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800577
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500578out:
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100579 intel_uncore_forcewake_put(dev_priv, fw);
Jani Nikulabffce902015-12-01 16:29:26 +0200580 return ret;
581}
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100582
Jani Nikulabffce902015-12-01 16:29:26 +0200583static int
584gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
585{
586 struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
587 adapter);
588 struct drm_i915_private *dev_priv = bus->dev_priv;
589 int ret;
590
591 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
592 mutex_lock(&dev_priv->gmbus_mutex);
593
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200594 if (bus->force_bit) {
Jani Nikulabffce902015-12-01 16:29:26 +0200595 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200596 if (ret < 0)
597 bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
598 } else {
Jani Nikulabffce902015-12-01 16:29:26 +0200599 ret = do_gmbus_xfer(adapter, msgs, num);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200600 if (ret == -EAGAIN)
601 bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
602 }
Jani Nikulabffce902015-12-01 16:29:26 +0200603
604 mutex_unlock(&dev_priv->gmbus_mutex);
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100605 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
606
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500607 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700608}
609
610static u32 gmbus_func(struct i2c_adapter *adapter)
611{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100612 return i2c_bit_algo.functionality(adapter) &
613 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700614 /* I2C_FUNC_10BIT_ADDR | */
615 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
616 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
617}
618
619static const struct i2c_algorithm gmbus_algorithm = {
620 .master_xfer = gmbus_xfer,
621 .functionality = gmbus_func
622};
623
624/**
625 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
626 * @dev: DRM device
627 */
628int intel_setup_gmbus(struct drm_device *dev)
629{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100630 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +0300631 struct pci_dev *pdev = dev_priv->drm.pdev;
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300632 struct intel_gmbus *bus;
633 unsigned int pin;
634 int ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700635
Ben Widawskyab5c6082013-04-05 13:12:41 -0700636 if (HAS_PCH_NOP(dev))
637 return 0;
Ville Syrjäläb2e8c6c2015-11-04 23:20:00 +0200638
Wayne Boyer666a4532015-12-09 12:29:35 -0800639 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjäläd8112152013-01-24 15:29:55 +0200640 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200641 else if (!HAS_GMCH_DISPLAY(dev_priv))
642 dev_priv->gpio_mmio_base =
643 i915_mmio_reg_offset(PCH_GPIOA) -
644 i915_mmio_reg_offset(GPIOA);
Daniel Vetter110447fc2012-03-23 23:43:36 +0100645
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500646 mutex_init(&dev_priv->gmbus_mutex);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100647 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500648
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300649 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200650 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300651 continue;
652
653 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700654
655 bus->adapter.owner = THIS_MODULE;
656 bus->adapter.class = I2C_CLASS_DDC;
657 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100658 sizeof(bus->adapter.name),
659 "i915 gmbus %s",
Jani Nikula4c272832015-04-01 10:58:05 +0300660 get_gmbus_pin(dev_priv, pin)->name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700661
David Weinehall52a05c32016-08-22 13:32:44 +0300662 bus->adapter.dev.parent = &pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100663 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700664
665 bus->adapter.algo = &gmbus_algorithm;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700666
Ville Syrjälä8b1f1652016-03-07 17:56:57 +0200667 /*
668 * We wish to retry with bit banging
669 * after a timed out GMBUS attempt.
670 */
671 bus->adapter.retries = 1;
672
Chris Wilsone957d772010-09-24 12:52:03 +0100673 /* By default use a conservative clock rate */
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300674 bus->reg0 = pin | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100675
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200676 /* gmbus seems to be broken on i830 */
677 if (IS_I830(dev))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000678 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200679
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300680 intel_gpio_setup(bus, pin);
Jani Nikulacee25162012-08-13 17:33:02 +0300681
682 ret = i2c_add_adapter(&bus->adapter);
683 if (ret)
684 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700685 }
686
Chris Wilson91c8a322016-07-05 10:40:23 +0100687 intel_i2c_reset(&dev_priv->drm);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700688
689 return 0;
690
691err:
Rasmus Villemoes2417c8c2016-02-09 21:11:13 +0100692 while (pin--) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200693 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300694 continue;
695
696 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700697 i2c_del_adapter(&bus->adapter);
698 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700699 return ret;
700}
701
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800702struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
Jani Nikula0184df42015-03-27 00:20:20 +0200703 unsigned int pin)
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800704{
Jani Nikula88ac7932015-03-27 00:20:22 +0200705 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300706 return NULL;
707
708 return &dev_priv->gmbus[pin].adapter;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800709}
710
Chris Wilsone957d772010-09-24 12:52:03 +0100711void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
712{
713 struct intel_gmbus *bus = to_intel_gmbus(adapter);
714
Adam Jacksond5090b92011-06-16 16:36:28 -0400715 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100716}
717
718void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
719{
720 struct intel_gmbus *bus = to_intel_gmbus(adapter);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200721 struct drm_i915_private *dev_priv = bus->dev_priv;
722
723 mutex_lock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100724
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000725 bus->force_bit += force_bit ? 1 : -1;
726 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
727 force_bit ? "en" : "dis", adapter->name,
728 bus->force_bit);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200729
730 mutex_unlock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100731}
732
Chris Wilsonf899fc62010-07-20 15:44:45 -0700733void intel_teardown_gmbus(struct drm_device *dev)
734{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100735 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300736 struct intel_gmbus *bus;
737 unsigned int pin;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700738
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300739 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200740 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300741 continue;
742
743 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700744 i2c_del_adapter(&bus->adapter);
745 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800746}