blob: d9e6993fa718e77bac7489fadba04f8377d1dff1 [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
Rodrigo Vivi3d023522017-06-02 13:06:43 -070071static const struct gmbus_pin gmbus_pins_cnp[] = {
72 [GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
73 [GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
74 [GMBUS_PIN_3_BXT] = { "misc", GPIOD },
75 [GMBUS_PIN_4_CNP] = { "dpd", GPIOE },
76};
77
Jani Nikula4c272832015-04-01 10:58:05 +030078/* pin is expected to be valid */
79static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
80 unsigned int pin)
81{
Rodrigo Vivi3d023522017-06-02 13:06:43 -070082 if (HAS_PCH_CNP(dev_priv))
83 return &gmbus_pins_cnp[pin];
84 else if (IS_GEN9_LP(dev_priv))
Jani Nikula4c272832015-04-01 10:58:05 +030085 return &gmbus_pins_bxt[pin];
Rodrigo Vivib976dc52017-01-23 10:32:37 -080086 else if (IS_GEN9_BC(dev_priv))
Jani Nikula6364e672015-05-06 15:33:44 +030087 return &gmbus_pins_skl[pin];
Jani Nikulac1bad5b2015-05-06 15:33:43 +030088 else if (IS_BROADWELL(dev_priv))
89 return &gmbus_pins_bdw[pin];
Jani Nikula4c272832015-04-01 10:58:05 +030090 else
91 return &gmbus_pins[pin];
92}
93
Jani Nikula88ac7932015-03-27 00:20:22 +020094bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
95 unsigned int pin)
96{
Jani Nikula4c272832015-04-01 10:58:05 +030097 unsigned int size;
98
Rodrigo Vivi3d023522017-06-02 13:06:43 -070099 if (HAS_PCH_CNP(dev_priv))
100 size = ARRAY_SIZE(gmbus_pins_cnp);
101 else if (IS_GEN9_LP(dev_priv))
Jani Nikula4c272832015-04-01 10:58:05 +0300102 size = ARRAY_SIZE(gmbus_pins_bxt);
Rodrigo Vivib976dc52017-01-23 10:32:37 -0800103 else if (IS_GEN9_BC(dev_priv))
Jani Nikula6364e672015-05-06 15:33:44 +0300104 size = ARRAY_SIZE(gmbus_pins_skl);
Jani Nikulac1bad5b2015-05-06 15:33:43 +0300105 else if (IS_BROADWELL(dev_priv))
106 size = ARRAY_SIZE(gmbus_pins_bdw);
Jani Nikula4c272832015-04-01 10:58:05 +0300107 else
108 size = ARRAY_SIZE(gmbus_pins);
109
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200110 return pin < size &&
111 i915_mmio_reg_valid(get_gmbus_pin(dev_priv, pin)->reg);
Jani Nikula88ac7932015-03-27 00:20:22 +0200112}
113
Chris Wilsonf899fc62010-07-20 15:44:45 -0700114/* Intel GPIO access functions */
115
Jean Delvare1849ecb2012-01-28 11:07:09 +0100116#define I2C_RISEFALL_TIME 10
Chris Wilsonf899fc62010-07-20 15:44:45 -0700117
Chris Wilsone957d772010-09-24 12:52:03 +0100118static inline struct intel_gmbus *
119to_intel_gmbus(struct i2c_adapter *i2c)
120{
121 return container_of(i2c, struct intel_gmbus, adapter);
122}
123
Chris Wilsonf899fc62010-07-20 15:44:45 -0700124void
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +0000125intel_i2c_reset(struct drm_i915_private *dev_priv)
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800126{
Ville Syrjälä699fc402015-09-18 20:03:38 +0300127 I915_WRITE(GMBUS0, 0);
128 I915_WRITE(GMBUS4, 0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700129}
130
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200131static void pnv_gmbus_clock_gating(struct drm_i915_private *dev_priv,
132 bool enable)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700133{
Chris Wilsonb222f262010-09-11 21:48:25 +0100134 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800135
136 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Chris Wilsonb222f262010-09-11 21:48:25 +0100137 val = I915_READ(DSPCLK_GATE_D);
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200138 if (!enable)
139 val |= PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800140 else
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200141 val &= ~PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
Chris Wilsonb222f262010-09-11 21:48:25 +0100142 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800143}
144
Ville Syrjälä6481d5e2017-12-21 22:24:32 +0200145static void pch_gmbus_clock_gating(struct drm_i915_private *dev_priv,
146 bool enable)
147{
148 u32 val;
149
150 val = I915_READ(SOUTH_DSPCLK_GATE_D);
151 if (!enable)
152 val |= PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
153 else
154 val &= ~PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
155 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
156}
157
158static void bxt_gmbus_clock_gating(struct drm_i915_private *dev_priv,
159 bool enable)
160{
161 u32 val;
162
163 val = I915_READ(GEN9_CLKGATE_DIS_4);
164 if (!enable)
165 val |= BXT_GMBUS_GATING_DIS;
166 else
167 val &= ~BXT_GMBUS_GATING_DIS;
168 I915_WRITE(GEN9_CLKGATE_DIS_4, val);
169}
170
Daniel Vetter36c785f2012-02-14 22:37:22 +0100171static u32 get_reserved(struct intel_gmbus *bus)
Chris Wilsone957d772010-09-24 12:52:03 +0100172{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100173 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100174 u32 reserved = 0;
175
176 /* On most chips, these bits must be preserved in software. */
Jani Nikula2a307c22016-11-30 17:43:04 +0200177 if (!IS_I830(dev_priv) && !IS_I845G(dev_priv))
Daniel Vetter36c785f2012-02-14 22:37:22 +0100178 reserved = I915_READ_NOTRACE(bus->gpio_reg) &
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000179 (GPIO_DATA_PULLUP_DISABLE |
180 GPIO_CLOCK_PULLUP_DISABLE);
Chris Wilsone957d772010-09-24 12:52:03 +0100181
182 return reserved;
183}
184
Jesse Barnes79e53942008-11-07 14:24:08 -0800185static int get_clock(void *data)
186{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100187 struct intel_gmbus *bus = data;
188 struct drm_i915_private *dev_priv = bus->dev_priv;
189 u32 reserved = get_reserved(bus);
190 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
191 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
192 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800193}
194
195static int get_data(void *data)
196{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100197 struct intel_gmbus *bus = data;
198 struct drm_i915_private *dev_priv = bus->dev_priv;
199 u32 reserved = get_reserved(bus);
200 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
201 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
202 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800203}
204
205static void set_clock(void *data, int state_high)
206{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100207 struct intel_gmbus *bus = data;
208 struct drm_i915_private *dev_priv = bus->dev_priv;
209 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100210 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800211
212 if (state_high)
213 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
214 else
215 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
216 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700217
Daniel Vetter36c785f2012-02-14 22:37:22 +0100218 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
219 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800220}
221
222static void set_data(void *data, int state_high)
223{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100224 struct intel_gmbus *bus = data;
225 struct drm_i915_private *dev_priv = bus->dev_priv;
226 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100227 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800228
229 if (state_high)
230 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
231 else
232 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
233 GPIO_DATA_VAL_MASK;
234
Daniel Vetter36c785f2012-02-14 22:37:22 +0100235 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
236 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800237}
238
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800239static int
240intel_gpio_pre_xfer(struct i2c_adapter *adapter)
241{
242 struct intel_gmbus *bus = container_of(adapter,
243 struct intel_gmbus,
244 adapter);
245 struct drm_i915_private *dev_priv = bus->dev_priv;
246
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +0000247 intel_i2c_reset(dev_priv);
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200248
249 if (IS_PINEVIEW(dev_priv))
250 pnv_gmbus_clock_gating(dev_priv, false);
251
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800252 set_data(bus, 1);
253 set_clock(bus, 1);
254 udelay(I2C_RISEFALL_TIME);
255 return 0;
256}
257
258static void
259intel_gpio_post_xfer(struct i2c_adapter *adapter)
260{
261 struct intel_gmbus *bus = container_of(adapter,
262 struct intel_gmbus,
263 adapter);
264 struct drm_i915_private *dev_priv = bus->dev_priv;
265
266 set_data(bus, 1);
267 set_clock(bus, 1);
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200268
269 if (IS_PINEVIEW(dev_priv))
270 pnv_gmbus_clock_gating(dev_priv, true);
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800271}
272
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800273static void
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300274intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800275{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100276 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100277 struct i2c_algo_bit_data *algo;
Eric Anholtf0217c42009-12-01 11:56:30 -0800278
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100279 algo = &bus->bit_algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100280
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200281 bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base +
282 i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg));
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100283 bus->adapter.algo_data = algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100284 algo->setsda = set_data;
285 algo->setscl = set_clock;
286 algo->getsda = get_data;
287 algo->getscl = get_clock;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800288 algo->pre_xfer = intel_gpio_pre_xfer;
289 algo->post_xfer = intel_gpio_post_xfer;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100290 algo->udelay = I2C_RISEFALL_TIME;
291 algo->timeout = usecs_to_jiffies(2200);
292 algo->data = bus;
Jesse Barnes79e53942008-11-07 14:24:08 -0800293}
294
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100295static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en)
Daniel Vetter61168c52012-12-01 13:53:43 +0100296{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100297 DEFINE_WAIT(wait);
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100298 u32 gmbus2;
299 int ret;
Jiri Kosinac12aba52013-03-19 09:56:57 +0100300
Daniel Vetter28c70f12012-12-01 13:53:45 +0100301 /* Important: The hw handles only the first bit, so set only one! Since
302 * we also need to check for NAKs besides the hw ready/idle signal, we
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100303 * need to wake up periodically and check that ourselves.
304 */
305 if (!HAS_GMBUS_IRQ(dev_priv))
306 irq_en = 0;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100307
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100308 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
309 I915_WRITE_FW(GMBUS4, irq_en);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100310
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100311 status |= GMBUS_SATOER;
312 ret = wait_for_us((gmbus2 = I915_READ_FW(GMBUS2)) & status, 2);
313 if (ret)
314 ret = wait_for((gmbus2 = I915_READ_FW(GMBUS2)) & status, 50);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100315
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100316 I915_WRITE_FW(GMBUS4, 0);
317 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
Daniel Vetter61168c52012-12-01 13:53:43 +0100318
319 if (gmbus2 & GMBUS_SATOER)
320 return -ENXIO;
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100321
322 return ret;
Daniel Vetter61168c52012-12-01 13:53:43 +0100323}
324
325static int
Daniel Vetter2c438c02012-12-01 13:53:46 +0100326gmbus_wait_idle(struct drm_i915_private *dev_priv)
327{
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100328 DEFINE_WAIT(wait);
329 u32 irq_enable;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100330 int ret;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100331
Daniel Vetter2c438c02012-12-01 13:53:46 +0100332 /* Important: The hw handles only the first bit, so set only one! */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100333 irq_enable = 0;
334 if (HAS_GMBUS_IRQ(dev_priv))
335 irq_enable = GMBUS_IDLE_EN;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100336
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100337 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
338 I915_WRITE_FW(GMBUS4, irq_enable);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100339
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100340 ret = intel_wait_for_register_fw(dev_priv,
341 GMBUS2, GMBUS_ACTIVE, 0,
342 10);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100343
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100344 I915_WRITE_FW(GMBUS4, 0);
345 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
346
347 return ret;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100348}
349
350static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700351gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
352 unsigned short addr, u8 *buf, unsigned int len,
353 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800354{
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100355 I915_WRITE_FW(GMBUS1,
356 gmbus1_index |
357 GMBUS_CYCLE_WAIT |
358 (len << GMBUS_BYTE_COUNT_SHIFT) |
359 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
360 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800361 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800362 int ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800363 u32 val, loop = 0;
364
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100365 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800366 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100367 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800368
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100369 val = I915_READ_FW(GMBUS3);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800370 do {
371 *buf++ = val & 0xff;
372 val >>= 8;
373 } while (--len && ++loop < 4);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800374 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800375
376 return 0;
377}
378
379static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700380gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
381 u32 gmbus1_index)
382{
383 u8 *buf = msg->buf;
384 unsigned int rx_size = msg->len;
385 unsigned int len;
386 int ret;
387
388 do {
389 len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
390
391 ret = gmbus_xfer_read_chunk(dev_priv, msg->addr,
392 buf, len, gmbus1_index);
393 if (ret)
394 return ret;
395
396 rx_size -= len;
397 buf += len;
398 } while (rx_size != 0);
399
400 return 0;
401}
402
403static int
404gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
Sean Pauld02cf0a2018-01-08 14:55:40 -0500405 unsigned short addr, u8 *buf, unsigned int len,
406 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800407{
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700408 unsigned int chunk_size = len;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800409 u32 val, loop;
410
411 val = loop = 0;
Daniel Kurtz26883c32012-03-30 19:46:36 +0800412 while (len && loop < 4) {
413 val |= *buf++ << (8 * loop++);
414 len -= 1;
415 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800416
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100417 I915_WRITE_FW(GMBUS3, val);
418 I915_WRITE_FW(GMBUS1,
Sean Pauld02cf0a2018-01-08 14:55:40 -0500419 gmbus1_index | GMBUS_CYCLE_WAIT |
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100420 (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
421 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
422 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800423 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800424 int ret;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800425
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800426 val = loop = 0;
427 do {
428 val |= *buf++ << (8 * loop);
429 } while (--len && ++loop < 4);
430
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100431 I915_WRITE_FW(GMBUS3, val);
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800432
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100433 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800434 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100435 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800436 }
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700437
438 return 0;
439}
440
441static int
Sean Pauld02cf0a2018-01-08 14:55:40 -0500442gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
443 u32 gmbus1_index)
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700444{
445 u8 *buf = msg->buf;
446 unsigned int tx_size = msg->len;
447 unsigned int len;
448 int ret;
449
450 do {
451 len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
452
Sean Pauld02cf0a2018-01-08 14:55:40 -0500453 ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len,
454 gmbus1_index);
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700455 if (ret)
456 return ret;
457
458 buf += len;
459 tx_size -= len;
460 } while (tx_size != 0);
461
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800462 return 0;
463}
464
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800465/*
Sean Pauld02cf0a2018-01-08 14:55:40 -0500466 * The gmbus controller can combine a 1 or 2 byte write with another read/write
467 * that immediately follows it by using an "INDEX" cycle.
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800468 */
469static bool
Sean Pauld02cf0a2018-01-08 14:55:40 -0500470gmbus_is_index_xfer(struct i2c_msg *msgs, int i, int num)
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800471{
472 return (i + 1 < num &&
Ville Syrjäläc4deb622017-11-23 21:41:56 +0200473 msgs[i].addr == msgs[i + 1].addr &&
Ville Syrjäläbb9e0d42017-11-23 21:41:57 +0200474 !(msgs[i].flags & I2C_M_RD) &&
475 (msgs[i].len == 1 || msgs[i].len == 2) &&
Sean Pauld02cf0a2018-01-08 14:55:40 -0500476 msgs[i + 1].len > 0);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800477}
478
479static int
Sean Pauld02cf0a2018-01-08 14:55:40 -0500480gmbus_index_xfer(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800481{
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800482 u32 gmbus1_index = 0;
483 u32 gmbus5 = 0;
484 int ret;
485
486 if (msgs[0].len == 2)
487 gmbus5 = GMBUS_2BYTE_INDEX_EN |
488 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
489 if (msgs[0].len == 1)
490 gmbus1_index = GMBUS_CYCLE_INDEX |
491 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
492
493 /* GMBUS5 holds 16-bit index */
494 if (gmbus5)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100495 I915_WRITE_FW(GMBUS5, gmbus5);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800496
Sean Pauld02cf0a2018-01-08 14:55:40 -0500497 if (msgs[1].flags & I2C_M_RD)
498 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
499 else
500 ret = gmbus_xfer_write(dev_priv, &msgs[1], gmbus1_index);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800501
502 /* Clear GMBUS5 after each index transfer */
503 if (gmbus5)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100504 I915_WRITE_FW(GMBUS5, 0);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800505
506 return ret;
507}
508
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800509static int
Jani Nikulabffce902015-12-01 16:29:26 +0200510do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700511{
512 struct intel_gmbus *bus = container_of(adapter,
513 struct intel_gmbus,
514 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100515 struct drm_i915_private *dev_priv = bus->dev_priv;
Ville Syrjälä699fc402015-09-18 20:03:38 +0300516 int i = 0, inc, try = 0;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800517 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700518
Ville Syrjälä6481d5e2017-12-21 22:24:32 +0200519 /* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
520 if (IS_GEN9_LP(dev_priv))
521 bxt_gmbus_clock_gating(dev_priv, false);
522 else if (HAS_PCH_SPT(dev_priv) ||
523 HAS_PCH_KBP(dev_priv) || HAS_PCH_CNP(dev_priv))
524 pch_gmbus_clock_gating(dev_priv, false);
525
Jani Nikula3f5f1552015-06-02 19:21:15 +0300526retry:
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100527 I915_WRITE_FW(GMBUS0, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700528
Jani Nikula3f5f1552015-06-02 19:21:15 +0300529 for (; i < num; i += inc) {
530 inc = 1;
Sean Pauld02cf0a2018-01-08 14:55:40 -0500531 if (gmbus_is_index_xfer(msgs, i, num)) {
532 ret = gmbus_index_xfer(dev_priv, &msgs[i]);
533 inc = 2; /* an index transmission is two msgs */
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800534 } else if (msgs[i].flags & I2C_M_RD) {
535 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
536 } else {
Sean Pauld02cf0a2018-01-08 14:55:40 -0500537 ret = gmbus_xfer_write(dev_priv, &msgs[i], 0);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800538 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700539
Jani Nikula0aeb9042015-12-01 16:29:25 +0200540 if (!ret)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100541 ret = gmbus_wait(dev_priv,
542 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800543 if (ret == -ETIMEDOUT)
544 goto timeout;
Jani Nikula0aeb9042015-12-01 16:29:25 +0200545 else if (ret)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800546 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700547 }
548
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800549 /* Generate a STOP condition on the bus. Note that gmbus can't generata
550 * a STOP on the very first cycle. To simplify the code we
551 * unconditionally generate the STOP condition with an additional gmbus
552 * cycle. */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100553 I915_WRITE_FW(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800554
Benson Leungcaae7452012-02-09 12:03:17 -0800555 /* Mark the GMBUS interface as disabled after waiting for idle.
556 * We will re-enable it at the start of the next xfer,
557 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100558 */
Daniel Vetter2c438c02012-12-01 13:53:46 +0100559 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800560 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800561 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800562 ret = -ETIMEDOUT;
563 }
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100564 I915_WRITE_FW(GMBUS0, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800565 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500566 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700567
Daniel Kurtze646d572012-03-30 19:46:38 +0800568clear_err:
569 /*
570 * Wait for bus to IDLE before clearing NAK.
571 * If we clear the NAK while bus is still active, then it will stay
572 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200573 *
574 * If no ACK is received during the address phase of a transaction, the
575 * adapter must report -ENXIO. It is not clear what to return if no ACK
576 * is received at other times. But we have to be careful to not return
577 * spurious -ENXIO because that will prevent i2c and drm edid functions
578 * from retrying. So return -ENXIO only when gmbus properly quiescents -
579 * timing out seems to happen when there _is_ a ddc chip present, but
580 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800581 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200582 ret = -ENXIO;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100583 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800584 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
585 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200586 ret = -ETIMEDOUT;
587 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800588
589 /* Toggle the Software Clear Interrupt bit. This has the effect
590 * of resetting the GMBUS controller and so clearing the
591 * BUS_ERROR raised by the slave's NAK.
592 */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100593 I915_WRITE_FW(GMBUS1, GMBUS_SW_CLR_INT);
594 I915_WRITE_FW(GMBUS1, 0);
595 I915_WRITE_FW(GMBUS0, 0);
Daniel Kurtze646d572012-03-30 19:46:38 +0800596
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800597 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800598 adapter->name, msgs[i].addr,
599 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
600
Jani Nikula3f5f1552015-06-02 19:21:15 +0300601 /*
602 * Passive adapters sometimes NAK the first probe. Retry the first
603 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
604 * has retries internally. See also the retry loop in
605 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
606 */
607 if (ret == -ENXIO && i == 0 && try++ == 0) {
608 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
609 adapter->name);
610 goto retry;
611 }
612
Daniel Kurtze646d572012-03-30 19:46:38 +0800613 goto out;
614
Chris Wilsonf899fc62010-07-20 15:44:45 -0700615timeout:
Ville Syrjälä70677802016-03-07 17:57:00 +0200616 DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
617 bus->adapter.name, bus->reg0 & 0xff);
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100618 I915_WRITE_FW(GMBUS0, 0);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100619
Jani Nikulabffce902015-12-01 16:29:26 +0200620 /*
621 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
622 * instead. Use EAGAIN to have i2c core retry.
623 */
Jani Nikulabffce902015-12-01 16:29:26 +0200624 ret = -EAGAIN;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800625
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500626out:
Ville Syrjälä6481d5e2017-12-21 22:24:32 +0200627 /* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
628 if (IS_GEN9_LP(dev_priv))
629 bxt_gmbus_clock_gating(dev_priv, true);
630 else if (HAS_PCH_SPT(dev_priv) ||
631 HAS_PCH_KBP(dev_priv) || HAS_PCH_CNP(dev_priv))
632 pch_gmbus_clock_gating(dev_priv, true);
633
Jani Nikulabffce902015-12-01 16:29:26 +0200634 return ret;
635}
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100636
Jani Nikulabffce902015-12-01 16:29:26 +0200637static int
638gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
639{
640 struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
641 adapter);
642 struct drm_i915_private *dev_priv = bus->dev_priv;
643 int ret;
644
645 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
Jani Nikulabffce902015-12-01 16:29:26 +0200646
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200647 if (bus->force_bit) {
Jani Nikulabffce902015-12-01 16:29:26 +0200648 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200649 if (ret < 0)
650 bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
651 } else {
Jani Nikulabffce902015-12-01 16:29:26 +0200652 ret = do_gmbus_xfer(adapter, msgs, num);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200653 if (ret == -EAGAIN)
654 bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
655 }
Jani Nikulabffce902015-12-01 16:29:26 +0200656
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100657 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
658
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500659 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700660}
661
662static u32 gmbus_func(struct i2c_adapter *adapter)
663{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100664 return i2c_bit_algo.functionality(adapter) &
665 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700666 /* I2C_FUNC_10BIT_ADDR | */
667 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
668 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
669}
670
671static const struct i2c_algorithm gmbus_algorithm = {
672 .master_xfer = gmbus_xfer,
673 .functionality = gmbus_func
674};
675
Daniel Vettera85066842017-07-26 15:26:47 +0200676static void gmbus_lock_bus(struct i2c_adapter *adapter,
677 unsigned int flags)
678{
679 struct intel_gmbus *bus = to_intel_gmbus(adapter);
680 struct drm_i915_private *dev_priv = bus->dev_priv;
681
682 mutex_lock(&dev_priv->gmbus_mutex);
683}
684
685static int gmbus_trylock_bus(struct i2c_adapter *adapter,
686 unsigned int flags)
687{
688 struct intel_gmbus *bus = to_intel_gmbus(adapter);
689 struct drm_i915_private *dev_priv = bus->dev_priv;
690
691 return mutex_trylock(&dev_priv->gmbus_mutex);
692}
693
694static void gmbus_unlock_bus(struct i2c_adapter *adapter,
695 unsigned int flags)
696{
697 struct intel_gmbus *bus = to_intel_gmbus(adapter);
698 struct drm_i915_private *dev_priv = bus->dev_priv;
699
700 mutex_unlock(&dev_priv->gmbus_mutex);
701}
702
Ville Syrjälä0db1aa42017-09-01 17:31:22 +0300703static const struct i2c_lock_operations gmbus_lock_ops = {
Daniel Vettera85066842017-07-26 15:26:47 +0200704 .lock_bus = gmbus_lock_bus,
705 .trylock_bus = gmbus_trylock_bus,
706 .unlock_bus = gmbus_unlock_bus,
707};
708
Chris Wilsonf899fc62010-07-20 15:44:45 -0700709/**
710 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000711 * @dev_priv: i915 device private
Chris Wilsonf899fc62010-07-20 15:44:45 -0700712 */
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000713int intel_setup_gmbus(struct drm_i915_private *dev_priv)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700714{
David Weinehall52a05c32016-08-22 13:32:44 +0300715 struct pci_dev *pdev = dev_priv->drm.pdev;
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300716 struct intel_gmbus *bus;
717 unsigned int pin;
718 int ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700719
Tvrtko Ursulin6e266952016-10-13 11:02:53 +0100720 if (HAS_PCH_NOP(dev_priv))
Ben Widawskyab5c6082013-04-05 13:12:41 -0700721 return 0;
Ville Syrjäläb2e8c6c2015-11-04 23:20:00 +0200722
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100723 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläd8112152013-01-24 15:29:55 +0200724 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200725 else if (!HAS_GMCH_DISPLAY(dev_priv))
726 dev_priv->gpio_mmio_base =
727 i915_mmio_reg_offset(PCH_GPIOA) -
728 i915_mmio_reg_offset(GPIOA);
Daniel Vetter110447fc2012-03-23 23:43:36 +0100729
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500730 mutex_init(&dev_priv->gmbus_mutex);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100731 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500732
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300733 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200734 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300735 continue;
736
737 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700738
739 bus->adapter.owner = THIS_MODULE;
740 bus->adapter.class = I2C_CLASS_DDC;
741 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100742 sizeof(bus->adapter.name),
743 "i915 gmbus %s",
Jani Nikula4c272832015-04-01 10:58:05 +0300744 get_gmbus_pin(dev_priv, pin)->name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700745
David Weinehall52a05c32016-08-22 13:32:44 +0300746 bus->adapter.dev.parent = &pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100747 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700748
749 bus->adapter.algo = &gmbus_algorithm;
Daniel Vettera85066842017-07-26 15:26:47 +0200750 bus->adapter.lock_ops = &gmbus_lock_ops;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700751
Ville Syrjälä8b1f1652016-03-07 17:56:57 +0200752 /*
753 * We wish to retry with bit banging
754 * after a timed out GMBUS attempt.
755 */
756 bus->adapter.retries = 1;
757
Chris Wilsone957d772010-09-24 12:52:03 +0100758 /* By default use a conservative clock rate */
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300759 bus->reg0 = pin | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100760
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200761 /* gmbus seems to be broken on i830 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100762 if (IS_I830(dev_priv))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000763 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200764
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300765 intel_gpio_setup(bus, pin);
Jani Nikulacee25162012-08-13 17:33:02 +0300766
767 ret = i2c_add_adapter(&bus->adapter);
768 if (ret)
769 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700770 }
771
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +0000772 intel_i2c_reset(dev_priv);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700773
774 return 0;
775
776err:
Rasmus Villemoes2417c8c2016-02-09 21:11:13 +0100777 while (pin--) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200778 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300779 continue;
780
781 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700782 i2c_del_adapter(&bus->adapter);
783 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700784 return ret;
785}
786
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800787struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
Jani Nikula0184df42015-03-27 00:20:20 +0200788 unsigned int pin)
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800789{
Jani Nikula88ac7932015-03-27 00:20:22 +0200790 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300791 return NULL;
792
793 return &dev_priv->gmbus[pin].adapter;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800794}
795
Chris Wilsone957d772010-09-24 12:52:03 +0100796void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
797{
798 struct intel_gmbus *bus = to_intel_gmbus(adapter);
799
Adam Jacksond5090b92011-06-16 16:36:28 -0400800 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100801}
802
803void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
804{
805 struct intel_gmbus *bus = to_intel_gmbus(adapter);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200806 struct drm_i915_private *dev_priv = bus->dev_priv;
807
808 mutex_lock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100809
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000810 bus->force_bit += force_bit ? 1 : -1;
811 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
812 force_bit ? "en" : "dis", adapter->name,
813 bus->force_bit);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200814
815 mutex_unlock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100816}
817
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000818void intel_teardown_gmbus(struct drm_i915_private *dev_priv)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700819{
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300820 struct intel_gmbus *bus;
821 unsigned int pin;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700822
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300823 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200824 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300825 continue;
826
827 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700828 i2c_del_adapter(&bus->adapter);
829 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800830}