blob: 3c6f338866da309a35a3f04a0fd0235b33739470 [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 Wilsone957d772010-09-24 12:52:03 +0100141 struct drm_device *dev = dev_priv->dev;
142 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
215 intel_i2c_reset(dev_priv->dev);
216 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 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
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +0300267 if (!HAS_GMBUS_IRQ(dev_priv))
Jiri Kosinac12aba52013-03-19 09:56:57 +0100268 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
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +0300301 if (!HAS_GMBUS_IRQ(dev_priv))
Chris Wilson4c802752016-06-30 15:33:22 +0100302 return intel_wait_for_register(dev_priv,
303 GMBUS2, GMBUS_ACTIVE, 0,
304 10);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100305
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
Chris Wilson4c802752016-06-30 15:33:22 +0100309 ret = wait_event_timeout(dev_priv->gmbus_wait_queue,
310 (I915_READ_NOTRACE(GMBUS2) & GMBUS_ACTIVE) == 0,
Imre Deak35987062013-05-21 20:03:20 +0300311 msecs_to_jiffies_timeout(10));
Daniel Vetter2c438c02012-12-01 13:53:46 +0100312
Ville Syrjälä699fc402015-09-18 20:03:38 +0300313 I915_WRITE(GMBUS4, 0);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100314
315 if (ret)
316 return 0;
317 else
318 return -ETIMEDOUT;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100319}
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
Jani Nikulabffce902015-12-01 16:29:26 +0200475do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700476{
477 struct intel_gmbus *bus = container_of(adapter,
478 struct intel_gmbus,
479 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100480 struct drm_i915_private *dev_priv = bus->dev_priv;
Ville Syrjälä699fc402015-09-18 20:03:38 +0300481 int i = 0, inc, try = 0;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800482 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700483
Jani Nikula3f5f1552015-06-02 19:21:15 +0300484retry:
Ville Syrjälä699fc402015-09-18 20:03:38 +0300485 I915_WRITE(GMBUS0, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700486
Jani Nikula3f5f1552015-06-02 19:21:15 +0300487 for (; i < num; i += inc) {
488 inc = 1;
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800489 if (gmbus_is_index_read(msgs, i, num)) {
490 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
Jani Nikula3f5f1552015-06-02 19:21:15 +0300491 inc = 2; /* an index read is two msgs */
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800492 } else if (msgs[i].flags & I2C_M_RD) {
493 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
494 } else {
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800495 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800496 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700497
Jani Nikula0aeb9042015-12-01 16:29:25 +0200498 if (!ret)
499 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
500 GMBUS_HW_WAIT_EN);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800501 if (ret == -ETIMEDOUT)
502 goto timeout;
Jani Nikula0aeb9042015-12-01 16:29:25 +0200503 else if (ret)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800504 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700505 }
506
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800507 /* Generate a STOP condition on the bus. Note that gmbus can't generata
508 * a STOP on the very first cycle. To simplify the code we
509 * unconditionally generate the STOP condition with an additional gmbus
510 * cycle. */
Ville Syrjälä699fc402015-09-18 20:03:38 +0300511 I915_WRITE(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800512
Benson Leungcaae7452012-02-09 12:03:17 -0800513 /* Mark the GMBUS interface as disabled after waiting for idle.
514 * We will re-enable it at the start of the next xfer,
515 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100516 */
Daniel Vetter2c438c02012-12-01 13:53:46 +0100517 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800518 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800519 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800520 ret = -ETIMEDOUT;
521 }
Ville Syrjälä699fc402015-09-18 20:03:38 +0300522 I915_WRITE(GMBUS0, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800523 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500524 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700525
Daniel Kurtze646d572012-03-30 19:46:38 +0800526clear_err:
527 /*
528 * Wait for bus to IDLE before clearing NAK.
529 * If we clear the NAK while bus is still active, then it will stay
530 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200531 *
532 * If no ACK is received during the address phase of a transaction, the
533 * adapter must report -ENXIO. It is not clear what to return if no ACK
534 * is received at other times. But we have to be careful to not return
535 * spurious -ENXIO because that will prevent i2c and drm edid functions
536 * from retrying. So return -ENXIO only when gmbus properly quiescents -
537 * timing out seems to happen when there _is_ a ddc chip present, but
538 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800539 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200540 ret = -ENXIO;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100541 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800542 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
543 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200544 ret = -ETIMEDOUT;
545 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800546
547 /* Toggle the Software Clear Interrupt bit. This has the effect
548 * of resetting the GMBUS controller and so clearing the
549 * BUS_ERROR raised by the slave's NAK.
550 */
Ville Syrjälä699fc402015-09-18 20:03:38 +0300551 I915_WRITE(GMBUS1, GMBUS_SW_CLR_INT);
552 I915_WRITE(GMBUS1, 0);
553 I915_WRITE(GMBUS0, 0);
Daniel Kurtze646d572012-03-30 19:46:38 +0800554
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800555 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800556 adapter->name, msgs[i].addr,
557 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
558
Jani Nikula3f5f1552015-06-02 19:21:15 +0300559 /*
560 * Passive adapters sometimes NAK the first probe. Retry the first
561 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
562 * has retries internally. See also the retry loop in
563 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
564 */
565 if (ret == -ENXIO && i == 0 && try++ == 0) {
566 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
567 adapter->name);
568 goto retry;
569 }
570
Daniel Kurtze646d572012-03-30 19:46:38 +0800571 goto out;
572
Chris Wilsonf899fc62010-07-20 15:44:45 -0700573timeout:
Ville Syrjälä70677802016-03-07 17:57:00 +0200574 DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
575 bus->adapter.name, bus->reg0 & 0xff);
Ville Syrjälä699fc402015-09-18 20:03:38 +0300576 I915_WRITE(GMBUS0, 0);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100577
Jani Nikulabffce902015-12-01 16:29:26 +0200578 /*
579 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
580 * instead. Use EAGAIN to have i2c core retry.
581 */
Jani Nikulabffce902015-12-01 16:29:26 +0200582 ret = -EAGAIN;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800583
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500584out:
Jani Nikulabffce902015-12-01 16:29:26 +0200585 return ret;
586}
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100587
Jani Nikulabffce902015-12-01 16:29:26 +0200588static int
589gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
590{
591 struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
592 adapter);
593 struct drm_i915_private *dev_priv = bus->dev_priv;
594 int ret;
595
596 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
597 mutex_lock(&dev_priv->gmbus_mutex);
598
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200599 if (bus->force_bit) {
Jani Nikulabffce902015-12-01 16:29:26 +0200600 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200601 if (ret < 0)
602 bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
603 } else {
Jani Nikulabffce902015-12-01 16:29:26 +0200604 ret = do_gmbus_xfer(adapter, msgs, num);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200605 if (ret == -EAGAIN)
606 bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
607 }
Jani Nikulabffce902015-12-01 16:29:26 +0200608
609 mutex_unlock(&dev_priv->gmbus_mutex);
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100610 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
611
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500612 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700613}
614
615static u32 gmbus_func(struct i2c_adapter *adapter)
616{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100617 return i2c_bit_algo.functionality(adapter) &
618 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700619 /* I2C_FUNC_10BIT_ADDR | */
620 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
621 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
622}
623
624static const struct i2c_algorithm gmbus_algorithm = {
625 .master_xfer = gmbus_xfer,
626 .functionality = gmbus_func
627};
628
629/**
630 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
631 * @dev: DRM device
632 */
633int intel_setup_gmbus(struct drm_device *dev)
634{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100635 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300636 struct intel_gmbus *bus;
637 unsigned int pin;
638 int ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700639
Ben Widawskyab5c6082013-04-05 13:12:41 -0700640 if (HAS_PCH_NOP(dev))
641 return 0;
Ville Syrjäläb2e8c6c2015-11-04 23:20:00 +0200642
Wayne Boyer666a4532015-12-09 12:29:35 -0800643 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjäläd8112152013-01-24 15:29:55 +0200644 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200645 else if (!HAS_GMCH_DISPLAY(dev_priv))
646 dev_priv->gpio_mmio_base =
647 i915_mmio_reg_offset(PCH_GPIOA) -
648 i915_mmio_reg_offset(GPIOA);
Daniel Vetter110447fc2012-03-23 23:43:36 +0100649
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500650 mutex_init(&dev_priv->gmbus_mutex);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100651 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500652
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300653 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200654 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300655 continue;
656
657 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700658
659 bus->adapter.owner = THIS_MODULE;
660 bus->adapter.class = I2C_CLASS_DDC;
661 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100662 sizeof(bus->adapter.name),
663 "i915 gmbus %s",
Jani Nikula4c272832015-04-01 10:58:05 +0300664 get_gmbus_pin(dev_priv, pin)->name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700665
666 bus->adapter.dev.parent = &dev->pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100667 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700668
669 bus->adapter.algo = &gmbus_algorithm;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700670
Ville Syrjälä8b1f1652016-03-07 17:56:57 +0200671 /*
672 * We wish to retry with bit banging
673 * after a timed out GMBUS attempt.
674 */
675 bus->adapter.retries = 1;
676
Chris Wilsone957d772010-09-24 12:52:03 +0100677 /* By default use a conservative clock rate */
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300678 bus->reg0 = pin | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100679
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200680 /* gmbus seems to be broken on i830 */
681 if (IS_I830(dev))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000682 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200683
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300684 intel_gpio_setup(bus, pin);
Jani Nikulacee25162012-08-13 17:33:02 +0300685
686 ret = i2c_add_adapter(&bus->adapter);
687 if (ret)
688 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700689 }
690
691 intel_i2c_reset(dev_priv->dev);
692
693 return 0;
694
695err:
Rasmus Villemoes2417c8c2016-02-09 21:11:13 +0100696 while (pin--) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200697 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300698 continue;
699
700 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700701 i2c_del_adapter(&bus->adapter);
702 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700703 return ret;
704}
705
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800706struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
Jani Nikula0184df42015-03-27 00:20:20 +0200707 unsigned int pin)
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800708{
Jani Nikula88ac7932015-03-27 00:20:22 +0200709 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300710 return NULL;
711
712 return &dev_priv->gmbus[pin].adapter;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800713}
714
Chris Wilsone957d772010-09-24 12:52:03 +0100715void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
716{
717 struct intel_gmbus *bus = to_intel_gmbus(adapter);
718
Adam Jacksond5090b92011-06-16 16:36:28 -0400719 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100720}
721
722void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
723{
724 struct intel_gmbus *bus = to_intel_gmbus(adapter);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200725 struct drm_i915_private *dev_priv = bus->dev_priv;
726
727 mutex_lock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100728
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000729 bus->force_bit += force_bit ? 1 : -1;
730 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
731 force_bit ? "en" : "dis", adapter->name,
732 bus->force_bit);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200733
734 mutex_unlock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100735}
736
Chris Wilsonf899fc62010-07-20 15:44:45 -0700737void intel_teardown_gmbus(struct drm_device *dev)
738{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100739 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300740 struct intel_gmbus *bus;
741 unsigned int pin;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700742
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300743 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200744 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300745 continue;
746
747 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700748 i2c_del_adapter(&bus->adapter);
749 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800750}