blob: 8affd47b98b814cfdcedbb511ebe47d5d90c9fbe [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
131static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
132{
Chris Wilsonb222f262010-09-11 21:48:25 +0100133 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800134
135 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +0300136 if (!IS_PINEVIEW(dev_priv))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800137 return;
Chris Wilsonb222f262010-09-11 21:48:25 +0100138
139 val = I915_READ(DSPCLK_GATE_D);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800140 if (enable)
Chris Wilsonb222f262010-09-11 21:48:25 +0100141 val |= DPCUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800142 else
Chris Wilsonb222f262010-09-11 21:48:25 +0100143 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
144 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800145}
146
Daniel Vetter36c785f2012-02-14 22:37:22 +0100147static u32 get_reserved(struct intel_gmbus *bus)
Chris Wilsone957d772010-09-24 12:52:03 +0100148{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100149 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100150 u32 reserved = 0;
151
152 /* On most chips, these bits must be preserved in software. */
Jani Nikula2a307c22016-11-30 17:43:04 +0200153 if (!IS_I830(dev_priv) && !IS_I845G(dev_priv))
Daniel Vetter36c785f2012-02-14 22:37:22 +0100154 reserved = I915_READ_NOTRACE(bus->gpio_reg) &
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000155 (GPIO_DATA_PULLUP_DISABLE |
156 GPIO_CLOCK_PULLUP_DISABLE);
Chris Wilsone957d772010-09-24 12:52:03 +0100157
158 return reserved;
159}
160
Jesse Barnes79e53942008-11-07 14:24:08 -0800161static int get_clock(void *data)
162{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100163 struct intel_gmbus *bus = data;
164 struct drm_i915_private *dev_priv = bus->dev_priv;
165 u32 reserved = get_reserved(bus);
166 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
167 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
168 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800169}
170
171static int get_data(void *data)
172{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100173 struct intel_gmbus *bus = data;
174 struct drm_i915_private *dev_priv = bus->dev_priv;
175 u32 reserved = get_reserved(bus);
176 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
177 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
178 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800179}
180
181static void set_clock(void *data, int state_high)
182{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100183 struct intel_gmbus *bus = data;
184 struct drm_i915_private *dev_priv = bus->dev_priv;
185 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100186 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800187
188 if (state_high)
189 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
190 else
191 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
192 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700193
Daniel Vetter36c785f2012-02-14 22:37:22 +0100194 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
195 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800196}
197
198static void set_data(void *data, int state_high)
199{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100200 struct intel_gmbus *bus = data;
201 struct drm_i915_private *dev_priv = bus->dev_priv;
202 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100203 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800204
205 if (state_high)
206 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
207 else
208 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
209 GPIO_DATA_VAL_MASK;
210
Daniel Vetter36c785f2012-02-14 22:37:22 +0100211 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
212 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800213}
214
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800215static int
216intel_gpio_pre_xfer(struct i2c_adapter *adapter)
217{
218 struct intel_gmbus *bus = container_of(adapter,
219 struct intel_gmbus,
220 adapter);
221 struct drm_i915_private *dev_priv = bus->dev_priv;
222
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +0000223 intel_i2c_reset(dev_priv);
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800224 intel_i2c_quirk_set(dev_priv, true);
225 set_data(bus, 1);
226 set_clock(bus, 1);
227 udelay(I2C_RISEFALL_TIME);
228 return 0;
229}
230
231static void
232intel_gpio_post_xfer(struct i2c_adapter *adapter)
233{
234 struct intel_gmbus *bus = container_of(adapter,
235 struct intel_gmbus,
236 adapter);
237 struct drm_i915_private *dev_priv = bus->dev_priv;
238
239 set_data(bus, 1);
240 set_clock(bus, 1);
241 intel_i2c_quirk_set(dev_priv, false);
242}
243
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800244static void
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300245intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800246{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100247 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100248 struct i2c_algo_bit_data *algo;
Eric Anholtf0217c42009-12-01 11:56:30 -0800249
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100250 algo = &bus->bit_algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100251
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200252 bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base +
253 i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg));
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100254 bus->adapter.algo_data = algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100255 algo->setsda = set_data;
256 algo->setscl = set_clock;
257 algo->getsda = get_data;
258 algo->getscl = get_clock;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800259 algo->pre_xfer = intel_gpio_pre_xfer;
260 algo->post_xfer = intel_gpio_post_xfer;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100261 algo->udelay = I2C_RISEFALL_TIME;
262 algo->timeout = usecs_to_jiffies(2200);
263 algo->data = bus;
Jesse Barnes79e53942008-11-07 14:24:08 -0800264}
265
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100266static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en)
Daniel Vetter61168c52012-12-01 13:53:43 +0100267{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100268 DEFINE_WAIT(wait);
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100269 u32 gmbus2;
270 int ret;
Jiri Kosinac12aba52013-03-19 09:56:57 +0100271
Daniel Vetter28c70f12012-12-01 13:53:45 +0100272 /* Important: The hw handles only the first bit, so set only one! Since
273 * we also need to check for NAKs besides the hw ready/idle signal, we
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100274 * need to wake up periodically and check that ourselves.
275 */
276 if (!HAS_GMBUS_IRQ(dev_priv))
277 irq_en = 0;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100278
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100279 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
280 I915_WRITE_FW(GMBUS4, irq_en);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100281
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100282 status |= GMBUS_SATOER;
283 ret = wait_for_us((gmbus2 = I915_READ_FW(GMBUS2)) & status, 2);
284 if (ret)
285 ret = wait_for((gmbus2 = I915_READ_FW(GMBUS2)) & status, 50);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100286
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100287 I915_WRITE_FW(GMBUS4, 0);
288 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
Daniel Vetter61168c52012-12-01 13:53:43 +0100289
290 if (gmbus2 & GMBUS_SATOER)
291 return -ENXIO;
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100292
293 return ret;
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{
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100299 DEFINE_WAIT(wait);
300 u32 irq_enable;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100301 int ret;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100302
Daniel Vetter2c438c02012-12-01 13:53:46 +0100303 /* Important: The hw handles only the first bit, so set only one! */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100304 irq_enable = 0;
305 if (HAS_GMBUS_IRQ(dev_priv))
306 irq_enable = GMBUS_IDLE_EN;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100307
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100308 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
309 I915_WRITE_FW(GMBUS4, irq_enable);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100310
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100311 ret = intel_wait_for_register_fw(dev_priv,
312 GMBUS2, GMBUS_ACTIVE, 0,
313 10);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100314
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100315 I915_WRITE_FW(GMBUS4, 0);
316 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
317
318 return ret;
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{
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100326 I915_WRITE_FW(GMBUS1,
327 gmbus1_index |
328 GMBUS_CYCLE_WAIT |
329 (len << GMBUS_BYTE_COUNT_SHIFT) |
330 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
331 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
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100336 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800337 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100338 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800339
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100340 val = I915_READ_FW(GMBUS3);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800341 do {
342 *buf++ = val & 0xff;
343 val >>= 8;
344 } while (--len && ++loop < 4);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800345 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800346
347 return 0;
348}
349
350static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700351gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
352 u32 gmbus1_index)
353{
354 u8 *buf = msg->buf;
355 unsigned int rx_size = msg->len;
356 unsigned int len;
357 int ret;
358
359 do {
360 len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
361
362 ret = gmbus_xfer_read_chunk(dev_priv, msg->addr,
363 buf, len, gmbus1_index);
364 if (ret)
365 return ret;
366
367 rx_size -= len;
368 buf += len;
369 } while (rx_size != 0);
370
371 return 0;
372}
373
374static int
375gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
376 unsigned short addr, u8 *buf, unsigned int len)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800377{
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700378 unsigned int chunk_size = len;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800379 u32 val, loop;
380
381 val = loop = 0;
Daniel Kurtz26883c32012-03-30 19:46:36 +0800382 while (len && loop < 4) {
383 val |= *buf++ << (8 * loop++);
384 len -= 1;
385 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800386
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100387 I915_WRITE_FW(GMBUS3, val);
388 I915_WRITE_FW(GMBUS1,
389 GMBUS_CYCLE_WAIT |
390 (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
391 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
392 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800393 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800394 int ret;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800395
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800396 val = loop = 0;
397 do {
398 val |= *buf++ << (8 * loop);
399 } while (--len && ++loop < 4);
400
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100401 I915_WRITE_FW(GMBUS3, val);
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800402
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100403 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800404 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100405 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800406 }
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700407
408 return 0;
409}
410
411static int
412gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
413{
414 u8 *buf = msg->buf;
415 unsigned int tx_size = msg->len;
416 unsigned int len;
417 int ret;
418
419 do {
420 len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
421
422 ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len);
423 if (ret)
424 return ret;
425
426 buf += len;
427 tx_size -= len;
428 } while (tx_size != 0);
429
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800430 return 0;
431}
432
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800433/*
434 * The gmbus controller can combine a 1 or 2 byte write with a read that
435 * immediately follows it by using an "INDEX" cycle.
436 */
437static bool
438gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
439{
440 return (i + 1 < num &&
Ville Syrjäläc4deb622017-11-23 21:41:56 +0200441 msgs[i].addr == msgs[i + 1].addr &&
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800442 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
443 (msgs[i + 1].flags & I2C_M_RD));
444}
445
446static int
447gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
448{
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800449 u32 gmbus1_index = 0;
450 u32 gmbus5 = 0;
451 int ret;
452
453 if (msgs[0].len == 2)
454 gmbus5 = GMBUS_2BYTE_INDEX_EN |
455 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
456 if (msgs[0].len == 1)
457 gmbus1_index = GMBUS_CYCLE_INDEX |
458 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
459
460 /* GMBUS5 holds 16-bit index */
461 if (gmbus5)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100462 I915_WRITE_FW(GMBUS5, gmbus5);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800463
464 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
465
466 /* Clear GMBUS5 after each index transfer */
467 if (gmbus5)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100468 I915_WRITE_FW(GMBUS5, 0);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800469
470 return ret;
471}
472
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800473static int
Jani Nikulabffce902015-12-01 16:29:26 +0200474do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700475{
476 struct intel_gmbus *bus = container_of(adapter,
477 struct intel_gmbus,
478 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100479 struct drm_i915_private *dev_priv = bus->dev_priv;
Ville Syrjälä699fc402015-09-18 20:03:38 +0300480 int i = 0, inc, try = 0;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800481 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700482
Jani Nikula3f5f1552015-06-02 19:21:15 +0300483retry:
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100484 I915_WRITE_FW(GMBUS0, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700485
Jani Nikula3f5f1552015-06-02 19:21:15 +0300486 for (; i < num; i += inc) {
487 inc = 1;
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800488 if (gmbus_is_index_read(msgs, i, num)) {
489 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
Jani Nikula3f5f1552015-06-02 19:21:15 +0300490 inc = 2; /* an index read is two msgs */
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800491 } else if (msgs[i].flags & I2C_M_RD) {
492 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
493 } else {
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800494 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800495 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700496
Jani Nikula0aeb9042015-12-01 16:29:25 +0200497 if (!ret)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100498 ret = gmbus_wait(dev_priv,
499 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800500 if (ret == -ETIMEDOUT)
501 goto timeout;
Jani Nikula0aeb9042015-12-01 16:29:25 +0200502 else if (ret)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800503 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700504 }
505
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800506 /* Generate a STOP condition on the bus. Note that gmbus can't generata
507 * a STOP on the very first cycle. To simplify the code we
508 * unconditionally generate the STOP condition with an additional gmbus
509 * cycle. */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100510 I915_WRITE_FW(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800511
Benson Leungcaae7452012-02-09 12:03:17 -0800512 /* Mark the GMBUS interface as disabled after waiting for idle.
513 * We will re-enable it at the start of the next xfer,
514 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100515 */
Daniel Vetter2c438c02012-12-01 13:53:46 +0100516 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800517 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800518 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800519 ret = -ETIMEDOUT;
520 }
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100521 I915_WRITE_FW(GMBUS0, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800522 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500523 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700524
Daniel Kurtze646d572012-03-30 19:46:38 +0800525clear_err:
526 /*
527 * Wait for bus to IDLE before clearing NAK.
528 * If we clear the NAK while bus is still active, then it will stay
529 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200530 *
531 * If no ACK is received during the address phase of a transaction, the
532 * adapter must report -ENXIO. It is not clear what to return if no ACK
533 * is received at other times. But we have to be careful to not return
534 * spurious -ENXIO because that will prevent i2c and drm edid functions
535 * from retrying. So return -ENXIO only when gmbus properly quiescents -
536 * timing out seems to happen when there _is_ a ddc chip present, but
537 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800538 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200539 ret = -ENXIO;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100540 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800541 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
542 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200543 ret = -ETIMEDOUT;
544 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800545
546 /* Toggle the Software Clear Interrupt bit. This has the effect
547 * of resetting the GMBUS controller and so clearing the
548 * BUS_ERROR raised by the slave's NAK.
549 */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100550 I915_WRITE_FW(GMBUS1, GMBUS_SW_CLR_INT);
551 I915_WRITE_FW(GMBUS1, 0);
552 I915_WRITE_FW(GMBUS0, 0);
Daniel Kurtze646d572012-03-30 19:46:38 +0800553
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800554 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800555 adapter->name, msgs[i].addr,
556 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
557
Jani Nikula3f5f1552015-06-02 19:21:15 +0300558 /*
559 * Passive adapters sometimes NAK the first probe. Retry the first
560 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
561 * has retries internally. See also the retry loop in
562 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
563 */
564 if (ret == -ENXIO && i == 0 && try++ == 0) {
565 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
566 adapter->name);
567 goto retry;
568 }
569
Daniel Kurtze646d572012-03-30 19:46:38 +0800570 goto out;
571
Chris Wilsonf899fc62010-07-20 15:44:45 -0700572timeout:
Ville Syrjälä70677802016-03-07 17:57:00 +0200573 DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
574 bus->adapter.name, bus->reg0 & 0xff);
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100575 I915_WRITE_FW(GMBUS0, 0);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100576
Jani Nikulabffce902015-12-01 16:29:26 +0200577 /*
578 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
579 * instead. Use EAGAIN to have i2c core retry.
580 */
Jani Nikulabffce902015-12-01 16:29:26 +0200581 ret = -EAGAIN;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800582
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500583out:
Jani Nikulabffce902015-12-01 16:29:26 +0200584 return ret;
585}
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100586
Jani Nikulabffce902015-12-01 16:29:26 +0200587static int
588gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
589{
590 struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
591 adapter);
592 struct drm_i915_private *dev_priv = bus->dev_priv;
593 int ret;
594
595 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
Jani Nikulabffce902015-12-01 16:29:26 +0200596
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200597 if (bus->force_bit) {
Jani Nikulabffce902015-12-01 16:29:26 +0200598 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200599 if (ret < 0)
600 bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
601 } else {
Jani Nikulabffce902015-12-01 16:29:26 +0200602 ret = do_gmbus_xfer(adapter, msgs, num);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200603 if (ret == -EAGAIN)
604 bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
605 }
Jani Nikulabffce902015-12-01 16:29:26 +0200606
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100607 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
608
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500609 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700610}
611
612static u32 gmbus_func(struct i2c_adapter *adapter)
613{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100614 return i2c_bit_algo.functionality(adapter) &
615 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700616 /* I2C_FUNC_10BIT_ADDR | */
617 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
618 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
619}
620
621static const struct i2c_algorithm gmbus_algorithm = {
622 .master_xfer = gmbus_xfer,
623 .functionality = gmbus_func
624};
625
Daniel Vettera85066842017-07-26 15:26:47 +0200626static void gmbus_lock_bus(struct i2c_adapter *adapter,
627 unsigned int flags)
628{
629 struct intel_gmbus *bus = to_intel_gmbus(adapter);
630 struct drm_i915_private *dev_priv = bus->dev_priv;
631
632 mutex_lock(&dev_priv->gmbus_mutex);
633}
634
635static int gmbus_trylock_bus(struct i2c_adapter *adapter,
636 unsigned int flags)
637{
638 struct intel_gmbus *bus = to_intel_gmbus(adapter);
639 struct drm_i915_private *dev_priv = bus->dev_priv;
640
641 return mutex_trylock(&dev_priv->gmbus_mutex);
642}
643
644static void gmbus_unlock_bus(struct i2c_adapter *adapter,
645 unsigned int flags)
646{
647 struct intel_gmbus *bus = to_intel_gmbus(adapter);
648 struct drm_i915_private *dev_priv = bus->dev_priv;
649
650 mutex_unlock(&dev_priv->gmbus_mutex);
651}
652
Ville Syrjälä0db1aa42017-09-01 17:31:22 +0300653static const struct i2c_lock_operations gmbus_lock_ops = {
Daniel Vettera85066842017-07-26 15:26:47 +0200654 .lock_bus = gmbus_lock_bus,
655 .trylock_bus = gmbus_trylock_bus,
656 .unlock_bus = gmbus_unlock_bus,
657};
658
Chris Wilsonf899fc62010-07-20 15:44:45 -0700659/**
660 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000661 * @dev_priv: i915 device private
Chris Wilsonf899fc62010-07-20 15:44:45 -0700662 */
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000663int intel_setup_gmbus(struct drm_i915_private *dev_priv)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700664{
David Weinehall52a05c32016-08-22 13:32:44 +0300665 struct pci_dev *pdev = dev_priv->drm.pdev;
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300666 struct intel_gmbus *bus;
667 unsigned int pin;
668 int ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700669
Tvrtko Ursulin6e266952016-10-13 11:02:53 +0100670 if (HAS_PCH_NOP(dev_priv))
Ben Widawskyab5c6082013-04-05 13:12:41 -0700671 return 0;
Ville Syrjäläb2e8c6c2015-11-04 23:20:00 +0200672
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100673 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläd8112152013-01-24 15:29:55 +0200674 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200675 else if (!HAS_GMCH_DISPLAY(dev_priv))
676 dev_priv->gpio_mmio_base =
677 i915_mmio_reg_offset(PCH_GPIOA) -
678 i915_mmio_reg_offset(GPIOA);
Daniel Vetter110447fc2012-03-23 23:43:36 +0100679
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500680 mutex_init(&dev_priv->gmbus_mutex);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100681 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500682
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300683 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200684 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300685 continue;
686
687 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700688
689 bus->adapter.owner = THIS_MODULE;
690 bus->adapter.class = I2C_CLASS_DDC;
691 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100692 sizeof(bus->adapter.name),
693 "i915 gmbus %s",
Jani Nikula4c272832015-04-01 10:58:05 +0300694 get_gmbus_pin(dev_priv, pin)->name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700695
David Weinehall52a05c32016-08-22 13:32:44 +0300696 bus->adapter.dev.parent = &pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100697 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700698
699 bus->adapter.algo = &gmbus_algorithm;
Daniel Vettera85066842017-07-26 15:26:47 +0200700 bus->adapter.lock_ops = &gmbus_lock_ops;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700701
Ville Syrjälä8b1f1652016-03-07 17:56:57 +0200702 /*
703 * We wish to retry with bit banging
704 * after a timed out GMBUS attempt.
705 */
706 bus->adapter.retries = 1;
707
Chris Wilsone957d772010-09-24 12:52:03 +0100708 /* By default use a conservative clock rate */
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300709 bus->reg0 = pin | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100710
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200711 /* gmbus seems to be broken on i830 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100712 if (IS_I830(dev_priv))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000713 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200714
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300715 intel_gpio_setup(bus, pin);
Jani Nikulacee25162012-08-13 17:33:02 +0300716
717 ret = i2c_add_adapter(&bus->adapter);
718 if (ret)
719 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700720 }
721
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +0000722 intel_i2c_reset(dev_priv);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700723
724 return 0;
725
726err:
Rasmus Villemoes2417c8c2016-02-09 21:11:13 +0100727 while (pin--) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200728 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300729 continue;
730
731 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700732 i2c_del_adapter(&bus->adapter);
733 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700734 return ret;
735}
736
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800737struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
Jani Nikula0184df42015-03-27 00:20:20 +0200738 unsigned int pin)
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800739{
Jani Nikula88ac7932015-03-27 00:20:22 +0200740 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300741 return NULL;
742
743 return &dev_priv->gmbus[pin].adapter;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800744}
745
Chris Wilsone957d772010-09-24 12:52:03 +0100746void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
747{
748 struct intel_gmbus *bus = to_intel_gmbus(adapter);
749
Adam Jacksond5090b92011-06-16 16:36:28 -0400750 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100751}
752
753void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
754{
755 struct intel_gmbus *bus = to_intel_gmbus(adapter);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200756 struct drm_i915_private *dev_priv = bus->dev_priv;
757
758 mutex_lock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100759
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000760 bus->force_bit += force_bit ? 1 : -1;
761 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
762 force_bit ? "en" : "dis", adapter->name,
763 bus->force_bit);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200764
765 mutex_unlock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100766}
767
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000768void intel_teardown_gmbus(struct drm_i915_private *dev_priv)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700769{
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300770 struct intel_gmbus *bus;
771 unsigned int pin;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700772
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300773 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200774 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300775 continue;
776
777 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700778 i2c_del_adapter(&bus->adapter);
779 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800780}