blob: e6875509bcd9cc53330a2e980d450b7e26a26e4e [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>
Sean Paul07e17a72018-01-08 14:55:41 -050033#include <drm/drm_hdcp.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "i915_drv.h"
37
Jani Nikula5ea6e5e2015-04-01 10:55:04 +030038struct gmbus_pin {
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080039 const char *name;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020040 i915_reg_t reg;
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080041};
42
Jani Nikula5ea6e5e2015-04-01 10:55:04 +030043/* Map gmbus pin pairs to names and registers. */
44static const struct gmbus_pin gmbus_pins[] = {
45 [GMBUS_PIN_SSC] = { "ssc", GPIOB },
46 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
47 [GMBUS_PIN_PANEL] = { "panel", GPIOC },
48 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
49 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
50 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080051};
52
Jani Nikulac1bad5b2015-05-06 15:33:43 +030053static const struct gmbus_pin gmbus_pins_bdw[] = {
54 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
55 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
56 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
57 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
58};
59
Jani Nikula6364e672015-05-06 15:33:44 +030060static const struct gmbus_pin gmbus_pins_skl[] = {
61 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
62 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
63 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
64};
65
Jani Nikula4c272832015-04-01 10:58:05 +030066static const struct gmbus_pin gmbus_pins_bxt[] = {
Ville Syrjäläb2e8c6c2015-11-04 23:20:00 +020067 [GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
68 [GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
69 [GMBUS_PIN_3_BXT] = { "misc", GPIOD },
Jani Nikula4c272832015-04-01 10:58:05 +030070};
71
Rodrigo Vivi3d023522017-06-02 13:06:43 -070072static const struct gmbus_pin gmbus_pins_cnp[] = {
73 [GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
74 [GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
75 [GMBUS_PIN_3_BXT] = { "misc", GPIOD },
76 [GMBUS_PIN_4_CNP] = { "dpd", GPIOE },
77};
78
Anusha Srivatsa5c749c52018-01-11 16:00:09 -020079static const struct gmbus_pin gmbus_pins_icp[] = {
80 [GMBUS_PIN_1_BXT] = { "dpa", GPIOA },
81 [GMBUS_PIN_2_BXT] = { "dpb", GPIOB },
82 [GMBUS_PIN_9_TC1_ICP] = { "tc1", GPIOC },
83 [GMBUS_PIN_10_TC2_ICP] = { "tc2", GPIOD },
84 [GMBUS_PIN_11_TC3_ICP] = { "tc3", GPIOE },
85 [GMBUS_PIN_12_TC4_ICP] = { "tc4", GPIOF },
86};
87
Jani Nikula4c272832015-04-01 10:58:05 +030088/* pin is expected to be valid */
89static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
90 unsigned int pin)
91{
Anusha Srivatsa5c749c52018-01-11 16:00:09 -020092 if (HAS_PCH_ICP(dev_priv))
93 return &gmbus_pins_icp[pin];
94 else if (HAS_PCH_CNP(dev_priv))
Rodrigo Vivi3d023522017-06-02 13:06:43 -070095 return &gmbus_pins_cnp[pin];
96 else if (IS_GEN9_LP(dev_priv))
Jani Nikula4c272832015-04-01 10:58:05 +030097 return &gmbus_pins_bxt[pin];
Rodrigo Vivib976dc52017-01-23 10:32:37 -080098 else if (IS_GEN9_BC(dev_priv))
Jani Nikula6364e672015-05-06 15:33:44 +030099 return &gmbus_pins_skl[pin];
Jani Nikulac1bad5b2015-05-06 15:33:43 +0300100 else if (IS_BROADWELL(dev_priv))
101 return &gmbus_pins_bdw[pin];
Jani Nikula4c272832015-04-01 10:58:05 +0300102 else
103 return &gmbus_pins[pin];
104}
105
Jani Nikula88ac7932015-03-27 00:20:22 +0200106bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
107 unsigned int pin)
108{
Jani Nikula4c272832015-04-01 10:58:05 +0300109 unsigned int size;
110
Anusha Srivatsa5c749c52018-01-11 16:00:09 -0200111 if (HAS_PCH_ICP(dev_priv))
112 size = ARRAY_SIZE(gmbus_pins_icp);
113 else if (HAS_PCH_CNP(dev_priv))
Rodrigo Vivi3d023522017-06-02 13:06:43 -0700114 size = ARRAY_SIZE(gmbus_pins_cnp);
115 else if (IS_GEN9_LP(dev_priv))
Jani Nikula4c272832015-04-01 10:58:05 +0300116 size = ARRAY_SIZE(gmbus_pins_bxt);
Rodrigo Vivib976dc52017-01-23 10:32:37 -0800117 else if (IS_GEN9_BC(dev_priv))
Jani Nikula6364e672015-05-06 15:33:44 +0300118 size = ARRAY_SIZE(gmbus_pins_skl);
Jani Nikulac1bad5b2015-05-06 15:33:43 +0300119 else if (IS_BROADWELL(dev_priv))
120 size = ARRAY_SIZE(gmbus_pins_bdw);
Jani Nikula4c272832015-04-01 10:58:05 +0300121 else
122 size = ARRAY_SIZE(gmbus_pins);
123
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200124 return pin < size &&
125 i915_mmio_reg_valid(get_gmbus_pin(dev_priv, pin)->reg);
Jani Nikula88ac7932015-03-27 00:20:22 +0200126}
127
Chris Wilsonf899fc62010-07-20 15:44:45 -0700128/* Intel GPIO access functions */
129
Jean Delvare1849ecb2012-01-28 11:07:09 +0100130#define I2C_RISEFALL_TIME 10
Chris Wilsonf899fc62010-07-20 15:44:45 -0700131
Chris Wilsone957d772010-09-24 12:52:03 +0100132static inline struct intel_gmbus *
133to_intel_gmbus(struct i2c_adapter *i2c)
134{
135 return container_of(i2c, struct intel_gmbus, adapter);
136}
137
Chris Wilsonf899fc62010-07-20 15:44:45 -0700138void
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +0000139intel_i2c_reset(struct drm_i915_private *dev_priv)
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800140{
Ville Syrjälä699fc402015-09-18 20:03:38 +0300141 I915_WRITE(GMBUS0, 0);
142 I915_WRITE(GMBUS4, 0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700143}
144
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200145static void pnv_gmbus_clock_gating(struct drm_i915_private *dev_priv,
146 bool enable)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700147{
Chris Wilsonb222f262010-09-11 21:48:25 +0100148 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800149
150 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Chris Wilsonb222f262010-09-11 21:48:25 +0100151 val = I915_READ(DSPCLK_GATE_D);
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200152 if (!enable)
153 val |= PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800154 else
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200155 val &= ~PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
Chris Wilsonb222f262010-09-11 21:48:25 +0100156 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800157}
158
Ville Syrjälä6481d5e2017-12-21 22:24:32 +0200159static void pch_gmbus_clock_gating(struct drm_i915_private *dev_priv,
160 bool enable)
161{
162 u32 val;
163
164 val = I915_READ(SOUTH_DSPCLK_GATE_D);
165 if (!enable)
166 val |= PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
167 else
168 val &= ~PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
169 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
170}
171
172static void bxt_gmbus_clock_gating(struct drm_i915_private *dev_priv,
173 bool enable)
174{
175 u32 val;
176
177 val = I915_READ(GEN9_CLKGATE_DIS_4);
178 if (!enable)
179 val |= BXT_GMBUS_GATING_DIS;
180 else
181 val &= ~BXT_GMBUS_GATING_DIS;
182 I915_WRITE(GEN9_CLKGATE_DIS_4, val);
183}
184
Daniel Vetter36c785f2012-02-14 22:37:22 +0100185static u32 get_reserved(struct intel_gmbus *bus)
Chris Wilsone957d772010-09-24 12:52:03 +0100186{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100187 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100188 u32 reserved = 0;
189
190 /* On most chips, these bits must be preserved in software. */
Jani Nikula2a307c22016-11-30 17:43:04 +0200191 if (!IS_I830(dev_priv) && !IS_I845G(dev_priv))
Daniel Vetter36c785f2012-02-14 22:37:22 +0100192 reserved = I915_READ_NOTRACE(bus->gpio_reg) &
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000193 (GPIO_DATA_PULLUP_DISABLE |
194 GPIO_CLOCK_PULLUP_DISABLE);
Chris Wilsone957d772010-09-24 12:52:03 +0100195
196 return reserved;
197}
198
Jesse Barnes79e53942008-11-07 14:24:08 -0800199static int get_clock(void *data)
200{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100201 struct intel_gmbus *bus = data;
202 struct drm_i915_private *dev_priv = bus->dev_priv;
203 u32 reserved = get_reserved(bus);
204 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
205 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
206 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800207}
208
209static int get_data(void *data)
210{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100211 struct intel_gmbus *bus = data;
212 struct drm_i915_private *dev_priv = bus->dev_priv;
213 u32 reserved = get_reserved(bus);
214 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
215 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
216 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800217}
218
219static void set_clock(void *data, int state_high)
220{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100221 struct intel_gmbus *bus = data;
222 struct drm_i915_private *dev_priv = bus->dev_priv;
223 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100224 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800225
226 if (state_high)
227 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
228 else
229 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
230 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700231
Daniel Vetter36c785f2012-02-14 22:37:22 +0100232 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
233 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800234}
235
236static void set_data(void *data, int state_high)
237{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100238 struct intel_gmbus *bus = data;
239 struct drm_i915_private *dev_priv = bus->dev_priv;
240 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100241 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800242
243 if (state_high)
244 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
245 else
246 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
247 GPIO_DATA_VAL_MASK;
248
Daniel Vetter36c785f2012-02-14 22:37:22 +0100249 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
250 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800251}
252
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800253static int
254intel_gpio_pre_xfer(struct i2c_adapter *adapter)
255{
256 struct intel_gmbus *bus = container_of(adapter,
257 struct intel_gmbus,
258 adapter);
259 struct drm_i915_private *dev_priv = bus->dev_priv;
260
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +0000261 intel_i2c_reset(dev_priv);
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200262
263 if (IS_PINEVIEW(dev_priv))
264 pnv_gmbus_clock_gating(dev_priv, false);
265
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800266 set_data(bus, 1);
267 set_clock(bus, 1);
268 udelay(I2C_RISEFALL_TIME);
269 return 0;
270}
271
272static void
273intel_gpio_post_xfer(struct i2c_adapter *adapter)
274{
275 struct intel_gmbus *bus = container_of(adapter,
276 struct intel_gmbus,
277 adapter);
278 struct drm_i915_private *dev_priv = bus->dev_priv;
279
280 set_data(bus, 1);
281 set_clock(bus, 1);
Ville Syrjäläad8059c2017-12-08 23:37:38 +0200282
283 if (IS_PINEVIEW(dev_priv))
284 pnv_gmbus_clock_gating(dev_priv, true);
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800285}
286
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800287static void
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300288intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800289{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100290 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100291 struct i2c_algo_bit_data *algo;
Eric Anholtf0217c42009-12-01 11:56:30 -0800292
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100293 algo = &bus->bit_algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100294
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200295 bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base +
296 i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg));
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100297 bus->adapter.algo_data = algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100298 algo->setsda = set_data;
299 algo->setscl = set_clock;
300 algo->getsda = get_data;
301 algo->getscl = get_clock;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800302 algo->pre_xfer = intel_gpio_pre_xfer;
303 algo->post_xfer = intel_gpio_post_xfer;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100304 algo->udelay = I2C_RISEFALL_TIME;
305 algo->timeout = usecs_to_jiffies(2200);
306 algo->data = bus;
Jesse Barnes79e53942008-11-07 14:24:08 -0800307}
308
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100309static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en)
Daniel Vetter61168c52012-12-01 13:53:43 +0100310{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100311 DEFINE_WAIT(wait);
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100312 u32 gmbus2;
313 int ret;
Jiri Kosinac12aba52013-03-19 09:56:57 +0100314
Daniel Vetter28c70f12012-12-01 13:53:45 +0100315 /* Important: The hw handles only the first bit, so set only one! Since
316 * we also need to check for NAKs besides the hw ready/idle signal, we
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100317 * need to wake up periodically and check that ourselves.
318 */
319 if (!HAS_GMBUS_IRQ(dev_priv))
320 irq_en = 0;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100321
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100322 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
323 I915_WRITE_FW(GMBUS4, irq_en);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100324
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100325 status |= GMBUS_SATOER;
326 ret = wait_for_us((gmbus2 = I915_READ_FW(GMBUS2)) & status, 2);
327 if (ret)
328 ret = wait_for((gmbus2 = I915_READ_FW(GMBUS2)) & status, 50);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100329
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100330 I915_WRITE_FW(GMBUS4, 0);
331 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
Daniel Vetter61168c52012-12-01 13:53:43 +0100332
333 if (gmbus2 & GMBUS_SATOER)
334 return -ENXIO;
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100335
336 return ret;
Daniel Vetter61168c52012-12-01 13:53:43 +0100337}
338
339static int
Daniel Vetter2c438c02012-12-01 13:53:46 +0100340gmbus_wait_idle(struct drm_i915_private *dev_priv)
341{
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100342 DEFINE_WAIT(wait);
343 u32 irq_enable;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100344 int ret;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100345
Daniel Vetter2c438c02012-12-01 13:53:46 +0100346 /* Important: The hw handles only the first bit, so set only one! */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100347 irq_enable = 0;
348 if (HAS_GMBUS_IRQ(dev_priv))
349 irq_enable = GMBUS_IDLE_EN;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100350
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100351 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
352 I915_WRITE_FW(GMBUS4, irq_enable);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100353
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100354 ret = intel_wait_for_register_fw(dev_priv,
355 GMBUS2, GMBUS_ACTIVE, 0,
356 10);
Daniel Vetter2c438c02012-12-01 13:53:46 +0100357
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100358 I915_WRITE_FW(GMBUS4, 0);
359 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
360
361 return ret;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100362}
363
364static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700365gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
366 unsigned short addr, u8 *buf, unsigned int len,
367 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800368{
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100369 I915_WRITE_FW(GMBUS1,
370 gmbus1_index |
371 GMBUS_CYCLE_WAIT |
372 (len << GMBUS_BYTE_COUNT_SHIFT) |
373 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
374 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800375 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800376 int ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800377 u32 val, loop = 0;
378
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100379 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800380 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100381 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800382
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100383 val = I915_READ_FW(GMBUS3);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800384 do {
385 *buf++ = val & 0xff;
386 val >>= 8;
387 } while (--len && ++loop < 4);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800388 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800389
390 return 0;
391}
392
393static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700394gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
395 u32 gmbus1_index)
396{
397 u8 *buf = msg->buf;
398 unsigned int rx_size = msg->len;
399 unsigned int len;
400 int ret;
401
402 do {
403 len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
404
405 ret = gmbus_xfer_read_chunk(dev_priv, msg->addr,
406 buf, len, gmbus1_index);
407 if (ret)
408 return ret;
409
410 rx_size -= len;
411 buf += len;
412 } while (rx_size != 0);
413
414 return 0;
415}
416
417static int
418gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
Sean Pauld02cf0a2018-01-08 14:55:40 -0500419 unsigned short addr, u8 *buf, unsigned int len,
420 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800421{
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700422 unsigned int chunk_size = len;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800423 u32 val, loop;
424
425 val = loop = 0;
Daniel Kurtz26883c32012-03-30 19:46:36 +0800426 while (len && loop < 4) {
427 val |= *buf++ << (8 * loop++);
428 len -= 1;
429 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800430
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100431 I915_WRITE_FW(GMBUS3, val);
432 I915_WRITE_FW(GMBUS1,
Sean Pauld02cf0a2018-01-08 14:55:40 -0500433 gmbus1_index | GMBUS_CYCLE_WAIT |
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100434 (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
435 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
436 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800437 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800438 int ret;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800439
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800440 val = loop = 0;
441 do {
442 val |= *buf++ << (8 * loop);
443 } while (--len && ++loop < 4);
444
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100445 I915_WRITE_FW(GMBUS3, val);
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800446
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100447 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800448 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100449 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800450 }
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700451
452 return 0;
453}
454
455static int
Sean Pauld02cf0a2018-01-08 14:55:40 -0500456gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
457 u32 gmbus1_index)
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700458{
459 u8 *buf = msg->buf;
460 unsigned int tx_size = msg->len;
461 unsigned int len;
462 int ret;
463
464 do {
465 len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
466
Sean Pauld02cf0a2018-01-08 14:55:40 -0500467 ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len,
468 gmbus1_index);
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700469 if (ret)
470 return ret;
471
472 buf += len;
473 tx_size -= len;
474 } while (tx_size != 0);
475
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800476 return 0;
477}
478
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800479/*
Sean Pauld02cf0a2018-01-08 14:55:40 -0500480 * The gmbus controller can combine a 1 or 2 byte write with another read/write
481 * that immediately follows it by using an "INDEX" cycle.
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800482 */
483static bool
Sean Pauld02cf0a2018-01-08 14:55:40 -0500484gmbus_is_index_xfer(struct i2c_msg *msgs, int i, int num)
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800485{
486 return (i + 1 < num &&
Ville Syrjäläc4deb622017-11-23 21:41:56 +0200487 msgs[i].addr == msgs[i + 1].addr &&
Ville Syrjäläbb9e0d42017-11-23 21:41:57 +0200488 !(msgs[i].flags & I2C_M_RD) &&
489 (msgs[i].len == 1 || msgs[i].len == 2) &&
Sean Pauld02cf0a2018-01-08 14:55:40 -0500490 msgs[i + 1].len > 0);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800491}
492
493static int
Sean Pauld02cf0a2018-01-08 14:55:40 -0500494gmbus_index_xfer(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800495{
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800496 u32 gmbus1_index = 0;
497 u32 gmbus5 = 0;
498 int ret;
499
500 if (msgs[0].len == 2)
501 gmbus5 = GMBUS_2BYTE_INDEX_EN |
502 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
503 if (msgs[0].len == 1)
504 gmbus1_index = GMBUS_CYCLE_INDEX |
505 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
506
507 /* GMBUS5 holds 16-bit index */
508 if (gmbus5)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100509 I915_WRITE_FW(GMBUS5, gmbus5);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800510
Sean Pauld02cf0a2018-01-08 14:55:40 -0500511 if (msgs[1].flags & I2C_M_RD)
512 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
513 else
514 ret = gmbus_xfer_write(dev_priv, &msgs[1], gmbus1_index);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800515
516 /* Clear GMBUS5 after each index transfer */
517 if (gmbus5)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100518 I915_WRITE_FW(GMBUS5, 0);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800519
520 return ret;
521}
522
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800523static int
Sean Paul07e17a72018-01-08 14:55:41 -0500524do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num,
525 u32 gmbus0_source)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700526{
527 struct intel_gmbus *bus = container_of(adapter,
528 struct intel_gmbus,
529 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100530 struct drm_i915_private *dev_priv = bus->dev_priv;
Ville Syrjälä699fc402015-09-18 20:03:38 +0300531 int i = 0, inc, try = 0;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800532 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700533
Ville Syrjälä6481d5e2017-12-21 22:24:32 +0200534 /* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
535 if (IS_GEN9_LP(dev_priv))
536 bxt_gmbus_clock_gating(dev_priv, false);
537 else if (HAS_PCH_SPT(dev_priv) ||
538 HAS_PCH_KBP(dev_priv) || HAS_PCH_CNP(dev_priv))
539 pch_gmbus_clock_gating(dev_priv, false);
540
Jani Nikula3f5f1552015-06-02 19:21:15 +0300541retry:
Sean Paul07e17a72018-01-08 14:55:41 -0500542 I915_WRITE_FW(GMBUS0, gmbus0_source | bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700543
Jani Nikula3f5f1552015-06-02 19:21:15 +0300544 for (; i < num; i += inc) {
545 inc = 1;
Sean Pauld02cf0a2018-01-08 14:55:40 -0500546 if (gmbus_is_index_xfer(msgs, i, num)) {
547 ret = gmbus_index_xfer(dev_priv, &msgs[i]);
548 inc = 2; /* an index transmission is two msgs */
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800549 } else if (msgs[i].flags & I2C_M_RD) {
550 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
551 } else {
Sean Pauld02cf0a2018-01-08 14:55:40 -0500552 ret = gmbus_xfer_write(dev_priv, &msgs[i], 0);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800553 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700554
Jani Nikula0aeb9042015-12-01 16:29:25 +0200555 if (!ret)
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100556 ret = gmbus_wait(dev_priv,
557 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800558 if (ret == -ETIMEDOUT)
559 goto timeout;
Jani Nikula0aeb9042015-12-01 16:29:25 +0200560 else if (ret)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800561 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700562 }
563
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800564 /* Generate a STOP condition on the bus. Note that gmbus can't generata
565 * a STOP on the very first cycle. To simplify the code we
566 * unconditionally generate the STOP condition with an additional gmbus
567 * cycle. */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100568 I915_WRITE_FW(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800569
Benson Leungcaae7452012-02-09 12:03:17 -0800570 /* Mark the GMBUS interface as disabled after waiting for idle.
571 * We will re-enable it at the start of the next xfer,
572 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100573 */
Daniel Vetter2c438c02012-12-01 13:53:46 +0100574 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800575 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800576 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800577 ret = -ETIMEDOUT;
578 }
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100579 I915_WRITE_FW(GMBUS0, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800580 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500581 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700582
Daniel Kurtze646d572012-03-30 19:46:38 +0800583clear_err:
584 /*
585 * Wait for bus to IDLE before clearing NAK.
586 * If we clear the NAK while bus is still active, then it will stay
587 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200588 *
589 * If no ACK is received during the address phase of a transaction, the
590 * adapter must report -ENXIO. It is not clear what to return if no ACK
591 * is received at other times. But we have to be careful to not return
592 * spurious -ENXIO because that will prevent i2c and drm edid functions
593 * from retrying. So return -ENXIO only when gmbus properly quiescents -
594 * timing out seems to happen when there _is_ a ddc chip present, but
595 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800596 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200597 ret = -ENXIO;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100598 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800599 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
600 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200601 ret = -ETIMEDOUT;
602 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800603
604 /* Toggle the Software Clear Interrupt bit. This has the effect
605 * of resetting the GMBUS controller and so clearing the
606 * BUS_ERROR raised by the slave's NAK.
607 */
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100608 I915_WRITE_FW(GMBUS1, GMBUS_SW_CLR_INT);
609 I915_WRITE_FW(GMBUS1, 0);
610 I915_WRITE_FW(GMBUS0, 0);
Daniel Kurtze646d572012-03-30 19:46:38 +0800611
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800612 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800613 adapter->name, msgs[i].addr,
614 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
615
Jani Nikula3f5f1552015-06-02 19:21:15 +0300616 /*
617 * Passive adapters sometimes NAK the first probe. Retry the first
618 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
619 * has retries internally. See also the retry loop in
620 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
621 */
622 if (ret == -ENXIO && i == 0 && try++ == 0) {
623 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
624 adapter->name);
625 goto retry;
626 }
627
Daniel Kurtze646d572012-03-30 19:46:38 +0800628 goto out;
629
Chris Wilsonf899fc62010-07-20 15:44:45 -0700630timeout:
Ville Syrjälä70677802016-03-07 17:57:00 +0200631 DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
632 bus->adapter.name, bus->reg0 & 0xff);
Chris Wilson4e6c2d52016-08-19 17:45:02 +0100633 I915_WRITE_FW(GMBUS0, 0);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100634
Jani Nikulabffce902015-12-01 16:29:26 +0200635 /*
636 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
637 * instead. Use EAGAIN to have i2c core retry.
638 */
Jani Nikulabffce902015-12-01 16:29:26 +0200639 ret = -EAGAIN;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800640
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500641out:
Ville Syrjälä6481d5e2017-12-21 22:24:32 +0200642 /* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
643 if (IS_GEN9_LP(dev_priv))
644 bxt_gmbus_clock_gating(dev_priv, true);
645 else if (HAS_PCH_SPT(dev_priv) ||
646 HAS_PCH_KBP(dev_priv) || HAS_PCH_CNP(dev_priv))
647 pch_gmbus_clock_gating(dev_priv, true);
648
Jani Nikulabffce902015-12-01 16:29:26 +0200649 return ret;
650}
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100651
Jani Nikulabffce902015-12-01 16:29:26 +0200652static int
653gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
654{
655 struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
656 adapter);
657 struct drm_i915_private *dev_priv = bus->dev_priv;
658 int ret;
659
660 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
Jani Nikulabffce902015-12-01 16:29:26 +0200661
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200662 if (bus->force_bit) {
Jani Nikulabffce902015-12-01 16:29:26 +0200663 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200664 if (ret < 0)
665 bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
666 } else {
Sean Paul07e17a72018-01-08 14:55:41 -0500667 ret = do_gmbus_xfer(adapter, msgs, num, 0);
Ville Syrjälä3e4d44e2016-03-07 17:56:59 +0200668 if (ret == -EAGAIN)
669 bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
670 }
Jani Nikulabffce902015-12-01 16:29:26 +0200671
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100672 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
673
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500674 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700675}
676
Sean Paul07e17a72018-01-08 14:55:41 -0500677int intel_gmbus_output_aksv(struct i2c_adapter *adapter)
678{
679 struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
680 adapter);
681 struct drm_i915_private *dev_priv = bus->dev_priv;
682 int ret;
683 u8 cmd = DRM_HDCP_DDC_AKSV;
684 u8 buf[DRM_HDCP_KSV_LEN] = { 0 };
685 struct i2c_msg msgs[] = {
686 {
687 .addr = DRM_HDCP_DDC_ADDR,
688 .flags = 0,
689 .len = sizeof(cmd),
690 .buf = &cmd,
691 },
692 {
693 .addr = DRM_HDCP_DDC_ADDR,
694 .flags = 0,
695 .len = sizeof(buf),
696 .buf = buf,
697 }
698 };
699
700 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
701 mutex_lock(&dev_priv->gmbus_mutex);
702
703 /*
704 * In order to output Aksv to the receiver, use an indexed write to
705 * pass the i2c command, and tell GMBUS to use the HW-provided value
706 * instead of sourcing GMBUS3 for the data.
707 */
708 ret = do_gmbus_xfer(adapter, msgs, ARRAY_SIZE(msgs), GMBUS_AKSV_SELECT);
709
710 mutex_unlock(&dev_priv->gmbus_mutex);
711 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
712
713 return ret;
714}
715
Chris Wilsonf899fc62010-07-20 15:44:45 -0700716static u32 gmbus_func(struct i2c_adapter *adapter)
717{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100718 return i2c_bit_algo.functionality(adapter) &
719 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700720 /* I2C_FUNC_10BIT_ADDR | */
721 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
722 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
723}
724
725static const struct i2c_algorithm gmbus_algorithm = {
726 .master_xfer = gmbus_xfer,
727 .functionality = gmbus_func
728};
729
Daniel Vettera85066842017-07-26 15:26:47 +0200730static void gmbus_lock_bus(struct i2c_adapter *adapter,
731 unsigned int flags)
732{
733 struct intel_gmbus *bus = to_intel_gmbus(adapter);
734 struct drm_i915_private *dev_priv = bus->dev_priv;
735
736 mutex_lock(&dev_priv->gmbus_mutex);
737}
738
739static int gmbus_trylock_bus(struct i2c_adapter *adapter,
740 unsigned int flags)
741{
742 struct intel_gmbus *bus = to_intel_gmbus(adapter);
743 struct drm_i915_private *dev_priv = bus->dev_priv;
744
745 return mutex_trylock(&dev_priv->gmbus_mutex);
746}
747
748static void gmbus_unlock_bus(struct i2c_adapter *adapter,
749 unsigned int flags)
750{
751 struct intel_gmbus *bus = to_intel_gmbus(adapter);
752 struct drm_i915_private *dev_priv = bus->dev_priv;
753
754 mutex_unlock(&dev_priv->gmbus_mutex);
755}
756
Ville Syrjälä0db1aa42017-09-01 17:31:22 +0300757static const struct i2c_lock_operations gmbus_lock_ops = {
Daniel Vettera85066842017-07-26 15:26:47 +0200758 .lock_bus = gmbus_lock_bus,
759 .trylock_bus = gmbus_trylock_bus,
760 .unlock_bus = gmbus_unlock_bus,
761};
762
Chris Wilsonf899fc62010-07-20 15:44:45 -0700763/**
764 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000765 * @dev_priv: i915 device private
Chris Wilsonf899fc62010-07-20 15:44:45 -0700766 */
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000767int intel_setup_gmbus(struct drm_i915_private *dev_priv)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700768{
David Weinehall52a05c32016-08-22 13:32:44 +0300769 struct pci_dev *pdev = dev_priv->drm.pdev;
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300770 struct intel_gmbus *bus;
771 unsigned int pin;
772 int ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700773
Tvrtko Ursulin6e266952016-10-13 11:02:53 +0100774 if (HAS_PCH_NOP(dev_priv))
Ben Widawskyab5c6082013-04-05 13:12:41 -0700775 return 0;
Ville Syrjäläb2e8c6c2015-11-04 23:20:00 +0200776
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100777 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläd8112152013-01-24 15:29:55 +0200778 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200779 else if (!HAS_GMCH_DISPLAY(dev_priv))
780 dev_priv->gpio_mmio_base =
781 i915_mmio_reg_offset(PCH_GPIOA) -
782 i915_mmio_reg_offset(GPIOA);
Daniel Vetter110447fc2012-03-23 23:43:36 +0100783
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500784 mutex_init(&dev_priv->gmbus_mutex);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100785 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500786
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300787 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200788 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300789 continue;
790
791 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700792
793 bus->adapter.owner = THIS_MODULE;
794 bus->adapter.class = I2C_CLASS_DDC;
795 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100796 sizeof(bus->adapter.name),
797 "i915 gmbus %s",
Jani Nikula4c272832015-04-01 10:58:05 +0300798 get_gmbus_pin(dev_priv, pin)->name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700799
David Weinehall52a05c32016-08-22 13:32:44 +0300800 bus->adapter.dev.parent = &pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100801 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700802
803 bus->adapter.algo = &gmbus_algorithm;
Daniel Vettera85066842017-07-26 15:26:47 +0200804 bus->adapter.lock_ops = &gmbus_lock_ops;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700805
Ville Syrjälä8b1f1652016-03-07 17:56:57 +0200806 /*
807 * We wish to retry with bit banging
808 * after a timed out GMBUS attempt.
809 */
810 bus->adapter.retries = 1;
811
Chris Wilsone957d772010-09-24 12:52:03 +0100812 /* By default use a conservative clock rate */
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300813 bus->reg0 = pin | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100814
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200815 /* gmbus seems to be broken on i830 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100816 if (IS_I830(dev_priv))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000817 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200818
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300819 intel_gpio_setup(bus, pin);
Jani Nikulacee25162012-08-13 17:33:02 +0300820
821 ret = i2c_add_adapter(&bus->adapter);
822 if (ret)
823 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700824 }
825
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +0000826 intel_i2c_reset(dev_priv);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700827
828 return 0;
829
830err:
Rasmus Villemoes2417c8c2016-02-09 21:11:13 +0100831 while (pin--) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200832 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300833 continue;
834
835 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700836 i2c_del_adapter(&bus->adapter);
837 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700838 return ret;
839}
840
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800841struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
Jani Nikula0184df42015-03-27 00:20:20 +0200842 unsigned int pin)
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800843{
Jani Nikula88ac7932015-03-27 00:20:22 +0200844 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300845 return NULL;
846
847 return &dev_priv->gmbus[pin].adapter;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800848}
849
Chris Wilsone957d772010-09-24 12:52:03 +0100850void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
851{
852 struct intel_gmbus *bus = to_intel_gmbus(adapter);
853
Adam Jacksond5090b92011-06-16 16:36:28 -0400854 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100855}
856
857void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
858{
859 struct intel_gmbus *bus = to_intel_gmbus(adapter);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200860 struct drm_i915_private *dev_priv = bus->dev_priv;
861
862 mutex_lock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100863
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000864 bus->force_bit += force_bit ? 1 : -1;
865 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
866 force_bit ? "en" : "dis", adapter->name,
867 bus->force_bit);
Ville Syrjäläade754e2016-03-07 17:56:58 +0200868
869 mutex_unlock(&dev_priv->gmbus_mutex);
Chris Wilsone957d772010-09-24 12:52:03 +0100870}
871
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000872void intel_teardown_gmbus(struct drm_i915_private *dev_priv)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700873{
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300874 struct intel_gmbus *bus;
875 unsigned int pin;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700876
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300877 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200878 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300879 continue;
880
881 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700882 i2c_del_adapter(&bus->adapter);
883 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800884}