Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Dave Airlie <airlied@linux.ie> |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 3 | * Copyright © 2006-2008,2010 Intel Corporation |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4 | * 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 Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 27 | * Chris Wilson <chris@chris-wilson.co.uk> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 28 | */ |
| 29 | #include <linux/i2c.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 30 | #include <linux/i2c-algo-bit.h> |
Paul Gortmaker | 2d1a8a4 | 2011-08-30 18:16:33 -0400 | [diff] [blame] | 31 | #include <linux/export.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 32 | #include <drm/drmP.h> |
Sean Paul | 07e17a7 | 2018-01-08 14:55:41 -0500 | [diff] [blame] | 33 | #include <drm/drm_hdcp.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 34 | #include "intel_drv.h" |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 35 | #include <drm/i915_drm.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 36 | #include "i915_drv.h" |
| 37 | |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 38 | struct gmbus_pin { |
Daniel Kurtz | 2ed06c9 | 2012-03-28 02:36:15 +0800 | [diff] [blame] | 39 | const char *name; |
Ville Syrjälä | f0f59a0 | 2015-11-18 15:33:26 +0200 | [diff] [blame] | 40 | i915_reg_t reg; |
Daniel Kurtz | 2ed06c9 | 2012-03-28 02:36:15 +0800 | [diff] [blame] | 41 | }; |
| 42 | |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 43 | /* Map gmbus pin pairs to names and registers. */ |
| 44 | static 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 Kurtz | 2ed06c9 | 2012-03-28 02:36:15 +0800 | [diff] [blame] | 51 | }; |
| 52 | |
Jani Nikula | c1bad5b | 2015-05-06 15:33:43 +0300 | [diff] [blame] | 53 | static 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 Nikula | 6364e67 | 2015-05-06 15:33:44 +0300 | [diff] [blame] | 60 | static 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 Nikula | 4c27283 | 2015-04-01 10:58:05 +0300 | [diff] [blame] | 66 | static const struct gmbus_pin gmbus_pins_bxt[] = { |
Ville Syrjälä | b2e8c6c | 2015-11-04 23:20:00 +0200 | [diff] [blame] | 67 | [GMBUS_PIN_1_BXT] = { "dpb", GPIOB }, |
| 68 | [GMBUS_PIN_2_BXT] = { "dpc", GPIOC }, |
| 69 | [GMBUS_PIN_3_BXT] = { "misc", GPIOD }, |
Jani Nikula | 4c27283 | 2015-04-01 10:58:05 +0300 | [diff] [blame] | 70 | }; |
| 71 | |
Rodrigo Vivi | 3d02352 | 2017-06-02 13:06:43 -0700 | [diff] [blame] | 72 | static 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 Srivatsa | 5c749c5 | 2018-01-11 16:00:09 -0200 | [diff] [blame] | 79 | static 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 Nikula | 4c27283 | 2015-04-01 10:58:05 +0300 | [diff] [blame] | 88 | /* pin is expected to be valid */ |
| 89 | static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv, |
| 90 | unsigned int pin) |
| 91 | { |
Anusha Srivatsa | 5c749c5 | 2018-01-11 16:00:09 -0200 | [diff] [blame] | 92 | if (HAS_PCH_ICP(dev_priv)) |
| 93 | return &gmbus_pins_icp[pin]; |
| 94 | else if (HAS_PCH_CNP(dev_priv)) |
Rodrigo Vivi | 3d02352 | 2017-06-02 13:06:43 -0700 | [diff] [blame] | 95 | return &gmbus_pins_cnp[pin]; |
| 96 | else if (IS_GEN9_LP(dev_priv)) |
Jani Nikula | 4c27283 | 2015-04-01 10:58:05 +0300 | [diff] [blame] | 97 | return &gmbus_pins_bxt[pin]; |
Rodrigo Vivi | b976dc5 | 2017-01-23 10:32:37 -0800 | [diff] [blame] | 98 | else if (IS_GEN9_BC(dev_priv)) |
Jani Nikula | 6364e67 | 2015-05-06 15:33:44 +0300 | [diff] [blame] | 99 | return &gmbus_pins_skl[pin]; |
Jani Nikula | c1bad5b | 2015-05-06 15:33:43 +0300 | [diff] [blame] | 100 | else if (IS_BROADWELL(dev_priv)) |
| 101 | return &gmbus_pins_bdw[pin]; |
Jani Nikula | 4c27283 | 2015-04-01 10:58:05 +0300 | [diff] [blame] | 102 | else |
| 103 | return &gmbus_pins[pin]; |
| 104 | } |
| 105 | |
Jani Nikula | 88ac793 | 2015-03-27 00:20:22 +0200 | [diff] [blame] | 106 | bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv, |
| 107 | unsigned int pin) |
| 108 | { |
Jani Nikula | 4c27283 | 2015-04-01 10:58:05 +0300 | [diff] [blame] | 109 | unsigned int size; |
| 110 | |
Anusha Srivatsa | 5c749c5 | 2018-01-11 16:00:09 -0200 | [diff] [blame] | 111 | if (HAS_PCH_ICP(dev_priv)) |
| 112 | size = ARRAY_SIZE(gmbus_pins_icp); |
| 113 | else if (HAS_PCH_CNP(dev_priv)) |
Rodrigo Vivi | 3d02352 | 2017-06-02 13:06:43 -0700 | [diff] [blame] | 114 | size = ARRAY_SIZE(gmbus_pins_cnp); |
| 115 | else if (IS_GEN9_LP(dev_priv)) |
Jani Nikula | 4c27283 | 2015-04-01 10:58:05 +0300 | [diff] [blame] | 116 | size = ARRAY_SIZE(gmbus_pins_bxt); |
Rodrigo Vivi | b976dc5 | 2017-01-23 10:32:37 -0800 | [diff] [blame] | 117 | else if (IS_GEN9_BC(dev_priv)) |
Jani Nikula | 6364e67 | 2015-05-06 15:33:44 +0300 | [diff] [blame] | 118 | size = ARRAY_SIZE(gmbus_pins_skl); |
Jani Nikula | c1bad5b | 2015-05-06 15:33:43 +0300 | [diff] [blame] | 119 | else if (IS_BROADWELL(dev_priv)) |
| 120 | size = ARRAY_SIZE(gmbus_pins_bdw); |
Jani Nikula | 4c27283 | 2015-04-01 10:58:05 +0300 | [diff] [blame] | 121 | else |
| 122 | size = ARRAY_SIZE(gmbus_pins); |
| 123 | |
Ville Syrjälä | f0f59a0 | 2015-11-18 15:33:26 +0200 | [diff] [blame] | 124 | return pin < size && |
| 125 | i915_mmio_reg_valid(get_gmbus_pin(dev_priv, pin)->reg); |
Jani Nikula | 88ac793 | 2015-03-27 00:20:22 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 128 | /* Intel GPIO access functions */ |
| 129 | |
Jean Delvare | 1849ecb | 2012-01-28 11:07:09 +0100 | [diff] [blame] | 130 | #define I2C_RISEFALL_TIME 10 |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 131 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 132 | static inline struct intel_gmbus * |
| 133 | to_intel_gmbus(struct i2c_adapter *i2c) |
| 134 | { |
| 135 | return container_of(i2c, struct intel_gmbus, adapter); |
| 136 | } |
| 137 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 138 | void |
Tvrtko Ursulin | af6dc74 | 2016-12-01 14:16:44 +0000 | [diff] [blame] | 139 | intel_i2c_reset(struct drm_i915_private *dev_priv) |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 140 | { |
Ville Syrjälä | 699fc40 | 2015-09-18 20:03:38 +0300 | [diff] [blame] | 141 | I915_WRITE(GMBUS0, 0); |
| 142 | I915_WRITE(GMBUS4, 0); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Ville Syrjälä | ad8059c | 2017-12-08 23:37:38 +0200 | [diff] [blame] | 145 | static void pnv_gmbus_clock_gating(struct drm_i915_private *dev_priv, |
| 146 | bool enable) |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 147 | { |
Chris Wilson | b222f26 | 2010-09-11 21:48:25 +0100 | [diff] [blame] | 148 | u32 val; |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 149 | |
| 150 | /* When using bit bashing for I2C, this bit needs to be set to 1 */ |
Chris Wilson | b222f26 | 2010-09-11 21:48:25 +0100 | [diff] [blame] | 151 | val = I915_READ(DSPCLK_GATE_D); |
Ville Syrjälä | ad8059c | 2017-12-08 23:37:38 +0200 | [diff] [blame] | 152 | if (!enable) |
| 153 | val |= PNV_GMBUSUNIT_CLOCK_GATE_DISABLE; |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 154 | else |
Ville Syrjälä | ad8059c | 2017-12-08 23:37:38 +0200 | [diff] [blame] | 155 | val &= ~PNV_GMBUSUNIT_CLOCK_GATE_DISABLE; |
Chris Wilson | b222f26 | 2010-09-11 21:48:25 +0100 | [diff] [blame] | 156 | I915_WRITE(DSPCLK_GATE_D, val); |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Ville Syrjälä | 6481d5e | 2017-12-21 22:24:32 +0200 | [diff] [blame] | 159 | static 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 | |
| 172 | static 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 Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 185 | static u32 get_reserved(struct intel_gmbus *bus) |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 186 | { |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 187 | struct drm_i915_private *dev_priv = bus->dev_priv; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 188 | u32 reserved = 0; |
| 189 | |
| 190 | /* On most chips, these bits must be preserved in software. */ |
Jani Nikula | 2a307c2 | 2016-11-30 17:43:04 +0200 | [diff] [blame] | 191 | if (!IS_I830(dev_priv) && !IS_I845G(dev_priv)) |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 192 | reserved = I915_READ_NOTRACE(bus->gpio_reg) & |
Yuanhan Liu | db5e417 | 2010-11-08 09:58:16 +0000 | [diff] [blame] | 193 | (GPIO_DATA_PULLUP_DISABLE | |
| 194 | GPIO_CLOCK_PULLUP_DISABLE); |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 195 | |
| 196 | return reserved; |
| 197 | } |
| 198 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 199 | static int get_clock(void *data) |
| 200 | { |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 201 | 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 Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static int get_data(void *data) |
| 210 | { |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 211 | 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 Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void set_clock(void *data, int state_high) |
| 220 | { |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 221 | struct intel_gmbus *bus = data; |
| 222 | struct drm_i915_private *dev_priv = bus->dev_priv; |
| 223 | u32 reserved = get_reserved(bus); |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 224 | u32 clock_bits; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 225 | |
| 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 Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 231 | |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 232 | I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits); |
| 233 | POSTING_READ(bus->gpio_reg); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void set_data(void *data, int state_high) |
| 237 | { |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 238 | struct intel_gmbus *bus = data; |
| 239 | struct drm_i915_private *dev_priv = bus->dev_priv; |
| 240 | u32 reserved = get_reserved(bus); |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 241 | u32 data_bits; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 242 | |
| 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 Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 249 | I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits); |
| 250 | POSTING_READ(bus->gpio_reg); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Daniel Kurtz | 489fbc1 | 2012-03-28 02:36:13 +0800 | [diff] [blame] | 253 | static int |
| 254 | intel_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 Ursulin | af6dc74 | 2016-12-01 14:16:44 +0000 | [diff] [blame] | 261 | intel_i2c_reset(dev_priv); |
Ville Syrjälä | ad8059c | 2017-12-08 23:37:38 +0200 | [diff] [blame] | 262 | |
| 263 | if (IS_PINEVIEW(dev_priv)) |
| 264 | pnv_gmbus_clock_gating(dev_priv, false); |
| 265 | |
Daniel Kurtz | 489fbc1 | 2012-03-28 02:36:13 +0800 | [diff] [blame] | 266 | set_data(bus, 1); |
| 267 | set_clock(bus, 1); |
| 268 | udelay(I2C_RISEFALL_TIME); |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static void |
| 273 | intel_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ä | ad8059c | 2017-12-08 23:37:38 +0200 | [diff] [blame] | 282 | |
| 283 | if (IS_PINEVIEW(dev_priv)) |
| 284 | pnv_gmbus_clock_gating(dev_priv, true); |
Daniel Kurtz | 489fbc1 | 2012-03-28 02:36:13 +0800 | [diff] [blame] | 285 | } |
| 286 | |
Daniel Kurtz | 2ed06c9 | 2012-03-28 02:36:15 +0800 | [diff] [blame] | 287 | static void |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 288 | intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin) |
Eric Anholt | f0217c4 | 2009-12-01 11:56:30 -0800 | [diff] [blame] | 289 | { |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 290 | struct drm_i915_private *dev_priv = bus->dev_priv; |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 291 | struct i2c_algo_bit_data *algo; |
Eric Anholt | f0217c4 | 2009-12-01 11:56:30 -0800 | [diff] [blame] | 292 | |
Daniel Vetter | c167a6f | 2012-02-28 00:43:09 +0100 | [diff] [blame] | 293 | algo = &bus->bit_algo; |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 294 | |
Ville Syrjälä | f0f59a0 | 2015-11-18 15:33:26 +0200 | [diff] [blame] | 295 | bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base + |
| 296 | i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg)); |
Daniel Vetter | c167a6f | 2012-02-28 00:43:09 +0100 | [diff] [blame] | 297 | bus->adapter.algo_data = algo; |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 298 | algo->setsda = set_data; |
| 299 | algo->setscl = set_clock; |
| 300 | algo->getsda = get_data; |
| 301 | algo->getscl = get_clock; |
Daniel Kurtz | 489fbc1 | 2012-03-28 02:36:13 +0800 | [diff] [blame] | 302 | algo->pre_xfer = intel_gpio_pre_xfer; |
| 303 | algo->post_xfer = intel_gpio_post_xfer; |
Daniel Vetter | 36c785f | 2012-02-14 22:37:22 +0100 | [diff] [blame] | 304 | algo->udelay = I2C_RISEFALL_TIME; |
| 305 | algo->timeout = usecs_to_jiffies(2200); |
| 306 | algo->data = bus; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 309 | static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en) |
Daniel Vetter | 61168c5 | 2012-12-01 13:53:43 +0100 | [diff] [blame] | 310 | { |
Daniel Vetter | 28c70f1 | 2012-12-01 13:53:45 +0100 | [diff] [blame] | 311 | DEFINE_WAIT(wait); |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 312 | u32 gmbus2; |
| 313 | int ret; |
Jiri Kosina | c12aba5 | 2013-03-19 09:56:57 +0100 | [diff] [blame] | 314 | |
Daniel Vetter | 28c70f1 | 2012-12-01 13:53:45 +0100 | [diff] [blame] | 315 | /* 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 Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 317 | * need to wake up periodically and check that ourselves. |
| 318 | */ |
| 319 | if (!HAS_GMBUS_IRQ(dev_priv)) |
| 320 | irq_en = 0; |
Daniel Vetter | 28c70f1 | 2012-12-01 13:53:45 +0100 | [diff] [blame] | 321 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 322 | add_wait_queue(&dev_priv->gmbus_wait_queue, &wait); |
| 323 | I915_WRITE_FW(GMBUS4, irq_en); |
Daniel Vetter | 28c70f1 | 2012-12-01 13:53:45 +0100 | [diff] [blame] | 324 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 325 | 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 Vetter | 28c70f1 | 2012-12-01 13:53:45 +0100 | [diff] [blame] | 329 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 330 | I915_WRITE_FW(GMBUS4, 0); |
| 331 | remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait); |
Daniel Vetter | 61168c5 | 2012-12-01 13:53:43 +0100 | [diff] [blame] | 332 | |
| 333 | if (gmbus2 & GMBUS_SATOER) |
| 334 | return -ENXIO; |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 335 | |
| 336 | return ret; |
Daniel Vetter | 61168c5 | 2012-12-01 13:53:43 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static int |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 340 | gmbus_wait_idle(struct drm_i915_private *dev_priv) |
| 341 | { |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 342 | DEFINE_WAIT(wait); |
| 343 | u32 irq_enable; |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 344 | int ret; |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 345 | |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 346 | /* Important: The hw handles only the first bit, so set only one! */ |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 347 | irq_enable = 0; |
| 348 | if (HAS_GMBUS_IRQ(dev_priv)) |
| 349 | irq_enable = GMBUS_IDLE_EN; |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 350 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 351 | add_wait_queue(&dev_priv->gmbus_wait_queue, &wait); |
| 352 | I915_WRITE_FW(GMBUS4, irq_enable); |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 353 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 354 | ret = intel_wait_for_register_fw(dev_priv, |
| 355 | GMBUS2, GMBUS_ACTIVE, 0, |
| 356 | 10); |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 357 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 358 | I915_WRITE_FW(GMBUS4, 0); |
| 359 | remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait); |
| 360 | |
| 361 | return ret; |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static int |
Dmitry Torokhov | 9535c47 | 2015-04-21 09:49:11 -0700 | [diff] [blame] | 365 | gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv, |
| 366 | unsigned short addr, u8 *buf, unsigned int len, |
| 367 | u32 gmbus1_index) |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 368 | { |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 369 | 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 Kurtz | 79985ee | 2012-04-13 19:47:53 +0800 | [diff] [blame] | 375 | while (len) { |
Daniel Kurtz | 90e6b26 | 2012-03-30 19:46:41 +0800 | [diff] [blame] | 376 | int ret; |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 377 | u32 val, loop = 0; |
| 378 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 379 | ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN); |
Daniel Kurtz | 90e6b26 | 2012-03-30 19:46:41 +0800 | [diff] [blame] | 380 | if (ret) |
Daniel Vetter | 61168c5 | 2012-12-01 13:53:43 +0100 | [diff] [blame] | 381 | return ret; |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 382 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 383 | val = I915_READ_FW(GMBUS3); |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 384 | do { |
| 385 | *buf++ = val & 0xff; |
| 386 | val >>= 8; |
| 387 | } while (--len && ++loop < 4); |
Daniel Kurtz | 79985ee | 2012-04-13 19:47:53 +0800 | [diff] [blame] | 388 | } |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int |
Dmitry Torokhov | 9535c47 | 2015-04-21 09:49:11 -0700 | [diff] [blame] | 394 | gmbus_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 | |
| 417 | static int |
| 418 | gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv, |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 419 | unsigned short addr, u8 *buf, unsigned int len, |
| 420 | u32 gmbus1_index) |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 421 | { |
Dmitry Torokhov | 9535c47 | 2015-04-21 09:49:11 -0700 | [diff] [blame] | 422 | unsigned int chunk_size = len; |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 423 | u32 val, loop; |
| 424 | |
| 425 | val = loop = 0; |
Daniel Kurtz | 26883c3 | 2012-03-30 19:46:36 +0800 | [diff] [blame] | 426 | while (len && loop < 4) { |
| 427 | val |= *buf++ << (8 * loop++); |
| 428 | len -= 1; |
| 429 | } |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 430 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 431 | I915_WRITE_FW(GMBUS3, val); |
| 432 | I915_WRITE_FW(GMBUS1, |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 433 | gmbus1_index | GMBUS_CYCLE_WAIT | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 434 | (chunk_size << GMBUS_BYTE_COUNT_SHIFT) | |
| 435 | (addr << GMBUS_SLAVE_ADDR_SHIFT) | |
| 436 | GMBUS_SLAVE_WRITE | GMBUS_SW_RDY); |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 437 | while (len) { |
Daniel Kurtz | 90e6b26 | 2012-03-30 19:46:41 +0800 | [diff] [blame] | 438 | int ret; |
Daniel Kurtz | 90e6b26 | 2012-03-30 19:46:41 +0800 | [diff] [blame] | 439 | |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 440 | val = loop = 0; |
| 441 | do { |
| 442 | val |= *buf++ << (8 * loop); |
| 443 | } while (--len && ++loop < 4); |
| 444 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 445 | I915_WRITE_FW(GMBUS3, val); |
Daniel Kurtz | 7a39a9d | 2012-03-30 19:46:37 +0800 | [diff] [blame] | 446 | |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 447 | ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN); |
Daniel Kurtz | 90e6b26 | 2012-03-30 19:46:41 +0800 | [diff] [blame] | 448 | if (ret) |
Daniel Vetter | 61168c5 | 2012-12-01 13:53:43 +0100 | [diff] [blame] | 449 | return ret; |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 450 | } |
Dmitry Torokhov | 9535c47 | 2015-04-21 09:49:11 -0700 | [diff] [blame] | 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 456 | gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg, |
| 457 | u32 gmbus1_index) |
Dmitry Torokhov | 9535c47 | 2015-04-21 09:49:11 -0700 | [diff] [blame] | 458 | { |
| 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 Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 467 | ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len, |
| 468 | gmbus1_index); |
Dmitry Torokhov | 9535c47 | 2015-04-21 09:49:11 -0700 | [diff] [blame] | 469 | if (ret) |
| 470 | return ret; |
| 471 | |
| 472 | buf += len; |
| 473 | tx_size -= len; |
| 474 | } while (tx_size != 0); |
| 475 | |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
Daniel Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 479 | /* |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 480 | * 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 Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 482 | */ |
| 483 | static bool |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 484 | gmbus_is_index_xfer(struct i2c_msg *msgs, int i, int num) |
Daniel Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 485 | { |
| 486 | return (i + 1 < num && |
Ville Syrjälä | c4deb62 | 2017-11-23 21:41:56 +0200 | [diff] [blame] | 487 | msgs[i].addr == msgs[i + 1].addr && |
Ville Syrjälä | bb9e0d4 | 2017-11-23 21:41:57 +0200 | [diff] [blame] | 488 | !(msgs[i].flags & I2C_M_RD) && |
| 489 | (msgs[i].len == 1 || msgs[i].len == 2) && |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 490 | msgs[i + 1].len > 0); |
Daniel Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static int |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 494 | gmbus_index_xfer(struct drm_i915_private *dev_priv, struct i2c_msg *msgs) |
Daniel Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 495 | { |
Daniel Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 496 | 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 Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 509 | I915_WRITE_FW(GMBUS5, gmbus5); |
Daniel Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 510 | |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 511 | 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 Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 515 | |
| 516 | /* Clear GMBUS5 after each index transfer */ |
| 517 | if (gmbus5) |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 518 | I915_WRITE_FW(GMBUS5, 0); |
Daniel Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 519 | |
| 520 | return ret; |
| 521 | } |
| 522 | |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 523 | static int |
Sean Paul | 07e17a7 | 2018-01-08 14:55:41 -0500 | [diff] [blame] | 524 | do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num, |
| 525 | u32 gmbus0_source) |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 526 | { |
| 527 | struct intel_gmbus *bus = container_of(adapter, |
| 528 | struct intel_gmbus, |
| 529 | adapter); |
Daniel Vetter | c2b9152 | 2012-02-14 22:37:19 +0100 | [diff] [blame] | 530 | struct drm_i915_private *dev_priv = bus->dev_priv; |
Ville Syrjälä | 699fc40 | 2015-09-18 20:03:38 +0300 | [diff] [blame] | 531 | int i = 0, inc, try = 0; |
Daniel Kurtz | 72d66af | 2012-03-30 19:46:39 +0800 | [diff] [blame] | 532 | int ret = 0; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 533 | |
Ville Syrjälä | 6481d5e | 2017-12-21 22:24:32 +0200 | [diff] [blame] | 534 | /* 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 Nikula | 3f5f155 | 2015-06-02 19:21:15 +0300 | [diff] [blame] | 541 | retry: |
Sean Paul | 07e17a7 | 2018-01-08 14:55:41 -0500 | [diff] [blame] | 542 | I915_WRITE_FW(GMBUS0, gmbus0_source | bus->reg0); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 543 | |
Jani Nikula | 3f5f155 | 2015-06-02 19:21:15 +0300 | [diff] [blame] | 544 | for (; i < num; i += inc) { |
| 545 | inc = 1; |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 546 | 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 Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 549 | } else if (msgs[i].flags & I2C_M_RD) { |
| 550 | ret = gmbus_xfer_read(dev_priv, &msgs[i], 0); |
| 551 | } else { |
Sean Paul | d02cf0a | 2018-01-08 14:55:40 -0500 | [diff] [blame] | 552 | ret = gmbus_xfer_write(dev_priv, &msgs[i], 0); |
Daniel Kurtz | 56f9eac | 2012-03-30 19:46:40 +0800 | [diff] [blame] | 553 | } |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 554 | |
Jani Nikula | 0aeb904 | 2015-12-01 16:29:25 +0200 | [diff] [blame] | 555 | if (!ret) |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 556 | ret = gmbus_wait(dev_priv, |
| 557 | GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN); |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 558 | if (ret == -ETIMEDOUT) |
| 559 | goto timeout; |
Jani Nikula | 0aeb904 | 2015-12-01 16:29:25 +0200 | [diff] [blame] | 560 | else if (ret) |
Daniel Kurtz | 924a93e | 2012-03-28 02:36:10 +0800 | [diff] [blame] | 561 | goto clear_err; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Daniel Kurtz | 72d66af | 2012-03-30 19:46:39 +0800 | [diff] [blame] | 564 | /* 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 Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 568 | I915_WRITE_FW(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY); |
Daniel Kurtz | 72d66af | 2012-03-30 19:46:39 +0800 | [diff] [blame] | 569 | |
Benson Leung | caae745 | 2012-02-09 12:03:17 -0800 | [diff] [blame] | 570 | /* 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 Wilson | 7f58aab | 2011-03-30 16:20:43 +0100 | [diff] [blame] | 573 | */ |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 574 | if (gmbus_wait_idle(dev_priv)) { |
Daniel Kurtz | 56fa6d6 | 2012-04-13 19:47:54 +0800 | [diff] [blame] | 575 | DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n", |
Daniel Kurtz | e646d57 | 2012-03-30 19:46:38 +0800 | [diff] [blame] | 576 | adapter->name); |
Daniel Kurtz | 72d66af | 2012-03-30 19:46:39 +0800 | [diff] [blame] | 577 | ret = -ETIMEDOUT; |
| 578 | } |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 579 | I915_WRITE_FW(GMBUS0, 0); |
Daniel Kurtz | 72d66af | 2012-03-30 19:46:39 +0800 | [diff] [blame] | 580 | ret = ret ?: i; |
Yufeng Shen | 8a8ed1f | 2012-02-13 17:36:54 -0500 | [diff] [blame] | 581 | goto out; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 582 | |
Daniel Kurtz | e646d57 | 2012-03-30 19:46:38 +0800 | [diff] [blame] | 583 | clear_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 Vetter | 65e8186 | 2012-05-21 20:19:48 +0200 | [diff] [blame] | 588 | * |
| 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 Kurtz | e646d57 | 2012-03-30 19:46:38 +0800 | [diff] [blame] | 596 | */ |
Daniel Vetter | 65e8186 | 2012-05-21 20:19:48 +0200 | [diff] [blame] | 597 | ret = -ENXIO; |
Daniel Vetter | 2c438c0 | 2012-12-01 13:53:46 +0100 | [diff] [blame] | 598 | if (gmbus_wait_idle(dev_priv)) { |
Daniel Kurtz | 56fa6d6 | 2012-04-13 19:47:54 +0800 | [diff] [blame] | 599 | DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n", |
| 600 | adapter->name); |
Daniel Vetter | 65e8186 | 2012-05-21 20:19:48 +0200 | [diff] [blame] | 601 | ret = -ETIMEDOUT; |
| 602 | } |
Daniel Kurtz | e646d57 | 2012-03-30 19:46:38 +0800 | [diff] [blame] | 603 | |
| 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 Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 608 | I915_WRITE_FW(GMBUS1, GMBUS_SW_CLR_INT); |
| 609 | I915_WRITE_FW(GMBUS1, 0); |
| 610 | I915_WRITE_FW(GMBUS0, 0); |
Daniel Kurtz | e646d57 | 2012-03-30 19:46:38 +0800 | [diff] [blame] | 611 | |
Daniel Kurtz | 56fa6d6 | 2012-04-13 19:47:54 +0800 | [diff] [blame] | 612 | DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n", |
Daniel Kurtz | e646d57 | 2012-03-30 19:46:38 +0800 | [diff] [blame] | 613 | adapter->name, msgs[i].addr, |
| 614 | (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len); |
| 615 | |
Jani Nikula | 3f5f155 | 2015-06-02 19:21:15 +0300 | [diff] [blame] | 616 | /* |
| 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 Kurtz | e646d57 | 2012-03-30 19:46:38 +0800 | [diff] [blame] | 628 | goto out; |
| 629 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 630 | timeout: |
Ville Syrjälä | 7067780 | 2016-03-07 17:57:00 +0200 | [diff] [blame] | 631 | DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n", |
| 632 | bus->adapter.name, bus->reg0 & 0xff); |
Chris Wilson | 4e6c2d5 | 2016-08-19 17:45:02 +0100 | [diff] [blame] | 633 | I915_WRITE_FW(GMBUS0, 0); |
Chris Wilson | 7f58aab | 2011-03-30 16:20:43 +0100 | [diff] [blame] | 634 | |
Jani Nikula | bffce90 | 2015-12-01 16:29:26 +0200 | [diff] [blame] | 635 | /* |
| 636 | * Hardware may not support GMBUS over these pins? Try GPIO bitbanging |
| 637 | * instead. Use EAGAIN to have i2c core retry. |
| 638 | */ |
Jani Nikula | bffce90 | 2015-12-01 16:29:26 +0200 | [diff] [blame] | 639 | ret = -EAGAIN; |
Daniel Kurtz | 489fbc1 | 2012-03-28 02:36:13 +0800 | [diff] [blame] | 640 | |
Yufeng Shen | 8a8ed1f | 2012-02-13 17:36:54 -0500 | [diff] [blame] | 641 | out: |
Ville Syrjälä | 6481d5e | 2017-12-21 22:24:32 +0200 | [diff] [blame] | 642 | /* 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 Nikula | bffce90 | 2015-12-01 16:29:26 +0200 | [diff] [blame] | 649 | return ret; |
| 650 | } |
Ville Syrjälä | f0ab43e | 2015-11-09 16:48:19 +0100 | [diff] [blame] | 651 | |
Jani Nikula | bffce90 | 2015-12-01 16:29:26 +0200 | [diff] [blame] | 652 | static int |
| 653 | gmbus_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 Nikula | bffce90 | 2015-12-01 16:29:26 +0200 | [diff] [blame] | 661 | |
Ville Syrjälä | 3e4d44e | 2016-03-07 17:56:59 +0200 | [diff] [blame] | 662 | if (bus->force_bit) { |
Jani Nikula | bffce90 | 2015-12-01 16:29:26 +0200 | [diff] [blame] | 663 | ret = i2c_bit_algo.master_xfer(adapter, msgs, num); |
Ville Syrjälä | 3e4d44e | 2016-03-07 17:56:59 +0200 | [diff] [blame] | 664 | if (ret < 0) |
| 665 | bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY; |
| 666 | } else { |
Sean Paul | 07e17a7 | 2018-01-08 14:55:41 -0500 | [diff] [blame] | 667 | ret = do_gmbus_xfer(adapter, msgs, num, 0); |
Ville Syrjälä | 3e4d44e | 2016-03-07 17:56:59 +0200 | [diff] [blame] | 668 | if (ret == -EAGAIN) |
| 669 | bus->force_bit |= GMBUS_FORCE_BIT_RETRY; |
| 670 | } |
Jani Nikula | bffce90 | 2015-12-01 16:29:26 +0200 | [diff] [blame] | 671 | |
Ville Syrjälä | f0ab43e | 2015-11-09 16:48:19 +0100 | [diff] [blame] | 672 | intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS); |
| 673 | |
Yufeng Shen | 8a8ed1f | 2012-02-13 17:36:54 -0500 | [diff] [blame] | 674 | return ret; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Sean Paul | 07e17a7 | 2018-01-08 14:55:41 -0500 | [diff] [blame] | 677 | int 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 Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 716 | static u32 gmbus_func(struct i2c_adapter *adapter) |
| 717 | { |
Daniel Vetter | f6f808c | 2012-02-14 18:58:49 +0100 | [diff] [blame] | 718 | return i2c_bit_algo.functionality(adapter) & |
| 719 | (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 720 | /* I2C_FUNC_10BIT_ADDR | */ |
| 721 | I2C_FUNC_SMBUS_READ_BLOCK_DATA | |
| 722 | I2C_FUNC_SMBUS_BLOCK_PROC_CALL); |
| 723 | } |
| 724 | |
| 725 | static const struct i2c_algorithm gmbus_algorithm = { |
| 726 | .master_xfer = gmbus_xfer, |
| 727 | .functionality = gmbus_func |
| 728 | }; |
| 729 | |
Daniel Vetter | a8506684 | 2017-07-26 15:26:47 +0200 | [diff] [blame] | 730 | static 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 | |
| 739 | static 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 | |
| 748 | static 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ä | 0db1aa4 | 2017-09-01 17:31:22 +0300 | [diff] [blame] | 757 | static const struct i2c_lock_operations gmbus_lock_ops = { |
Daniel Vetter | a8506684 | 2017-07-26 15:26:47 +0200 | [diff] [blame] | 758 | .lock_bus = gmbus_lock_bus, |
| 759 | .trylock_bus = gmbus_trylock_bus, |
| 760 | .unlock_bus = gmbus_unlock_bus, |
| 761 | }; |
| 762 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 763 | /** |
| 764 | * intel_gmbus_setup - instantiate all Intel i2c GMBuses |
Tvrtko Ursulin | 4019644 | 2016-12-01 14:16:42 +0000 | [diff] [blame] | 765 | * @dev_priv: i915 device private |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 766 | */ |
Tvrtko Ursulin | 4019644 | 2016-12-01 14:16:42 +0000 | [diff] [blame] | 767 | int intel_setup_gmbus(struct drm_i915_private *dev_priv) |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 768 | { |
David Weinehall | 52a05c3 | 2016-08-22 13:32:44 +0300 | [diff] [blame] | 769 | struct pci_dev *pdev = dev_priv->drm.pdev; |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 770 | struct intel_gmbus *bus; |
| 771 | unsigned int pin; |
| 772 | int ret; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 773 | |
Tvrtko Ursulin | 6e26695 | 2016-10-13 11:02:53 +0100 | [diff] [blame] | 774 | if (HAS_PCH_NOP(dev_priv)) |
Ben Widawsky | ab5c608 | 2013-04-05 13:12:41 -0700 | [diff] [blame] | 775 | return 0; |
Ville Syrjälä | b2e8c6c | 2015-11-04 23:20:00 +0200 | [diff] [blame] | 776 | |
Tvrtko Ursulin | 920a14b | 2016-10-14 10:13:44 +0100 | [diff] [blame] | 777 | if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) |
Ville Syrjälä | d811215 | 2013-01-24 15:29:55 +0200 | [diff] [blame] | 778 | dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE; |
Ville Syrjälä | f0f59a0 | 2015-11-18 15:33:26 +0200 | [diff] [blame] | 779 | 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 Vetter | 110447fc | 2012-03-23 23:43:36 +0100 | [diff] [blame] | 783 | |
Yufeng Shen | 8a8ed1f | 2012-02-13 17:36:54 -0500 | [diff] [blame] | 784 | mutex_init(&dev_priv->gmbus_mutex); |
Daniel Vetter | 28c70f1 | 2012-12-01 13:53:45 +0100 | [diff] [blame] | 785 | init_waitqueue_head(&dev_priv->gmbus_wait_queue); |
Yufeng Shen | 8a8ed1f | 2012-02-13 17:36:54 -0500 | [diff] [blame] | 786 | |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 787 | for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) { |
Jani Nikula | 88ac793 | 2015-03-27 00:20:22 +0200 | [diff] [blame] | 788 | if (!intel_gmbus_is_valid_pin(dev_priv, pin)) |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 789 | continue; |
| 790 | |
| 791 | bus = &dev_priv->gmbus[pin]; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 792 | |
| 793 | bus->adapter.owner = THIS_MODULE; |
| 794 | bus->adapter.class = I2C_CLASS_DDC; |
| 795 | snprintf(bus->adapter.name, |
Jean Delvare | 6966945 | 2010-11-05 18:51:34 +0100 | [diff] [blame] | 796 | sizeof(bus->adapter.name), |
| 797 | "i915 gmbus %s", |
Jani Nikula | 4c27283 | 2015-04-01 10:58:05 +0300 | [diff] [blame] | 798 | get_gmbus_pin(dev_priv, pin)->name); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 799 | |
David Weinehall | 52a05c3 | 2016-08-22 13:32:44 +0300 | [diff] [blame] | 800 | bus->adapter.dev.parent = &pdev->dev; |
Daniel Vetter | c2b9152 | 2012-02-14 22:37:19 +0100 | [diff] [blame] | 801 | bus->dev_priv = dev_priv; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 802 | |
| 803 | bus->adapter.algo = &gmbus_algorithm; |
Daniel Vetter | a8506684 | 2017-07-26 15:26:47 +0200 | [diff] [blame] | 804 | bus->adapter.lock_ops = &gmbus_lock_ops; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 805 | |
Ville Syrjälä | 8b1f165 | 2016-03-07 17:56:57 +0200 | [diff] [blame] | 806 | /* |
| 807 | * We wish to retry with bit banging |
| 808 | * after a timed out GMBUS attempt. |
| 809 | */ |
| 810 | bus->adapter.retries = 1; |
| 811 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 812 | /* By default use a conservative clock rate */ |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 813 | bus->reg0 = pin | GMBUS_RATE_100KHZ; |
Chris Wilson | cb8ea75 | 2010-09-28 13:35:47 +0100 | [diff] [blame] | 814 | |
Daniel Vetter | 83ee9e6 | 2012-05-13 14:44:20 +0200 | [diff] [blame] | 815 | /* gmbus seems to be broken on i830 */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 816 | if (IS_I830(dev_priv)) |
Chris Wilson | f2ce9fa | 2012-11-10 15:58:21 +0000 | [diff] [blame] | 817 | bus->force_bit = 1; |
Daniel Vetter | 83ee9e6 | 2012-05-13 14:44:20 +0200 | [diff] [blame] | 818 | |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 819 | intel_gpio_setup(bus, pin); |
Jani Nikula | cee2516 | 2012-08-13 17:33:02 +0300 | [diff] [blame] | 820 | |
| 821 | ret = i2c_add_adapter(&bus->adapter); |
| 822 | if (ret) |
| 823 | goto err; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Tvrtko Ursulin | af6dc74 | 2016-12-01 14:16:44 +0000 | [diff] [blame] | 826 | intel_i2c_reset(dev_priv); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 827 | |
| 828 | return 0; |
| 829 | |
| 830 | err: |
Rasmus Villemoes | 2417c8c | 2016-02-09 21:11:13 +0100 | [diff] [blame] | 831 | while (pin--) { |
Jani Nikula | 88ac793 | 2015-03-27 00:20:22 +0200 | [diff] [blame] | 832 | if (!intel_gmbus_is_valid_pin(dev_priv, pin)) |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 833 | continue; |
| 834 | |
| 835 | bus = &dev_priv->gmbus[pin]; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 836 | i2c_del_adapter(&bus->adapter); |
| 837 | } |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 838 | return ret; |
| 839 | } |
| 840 | |
Daniel Kurtz | 3bd7d90 | 2012-03-28 02:36:14 +0800 | [diff] [blame] | 841 | struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv, |
Jani Nikula | 0184df4 | 2015-03-27 00:20:20 +0200 | [diff] [blame] | 842 | unsigned int pin) |
Daniel Kurtz | 3bd7d90 | 2012-03-28 02:36:14 +0800 | [diff] [blame] | 843 | { |
Jani Nikula | 88ac793 | 2015-03-27 00:20:22 +0200 | [diff] [blame] | 844 | if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin))) |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 845 | return NULL; |
| 846 | |
| 847 | return &dev_priv->gmbus[pin].adapter; |
Daniel Kurtz | 3bd7d90 | 2012-03-28 02:36:14 +0800 | [diff] [blame] | 848 | } |
| 849 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 850 | void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed) |
| 851 | { |
| 852 | struct intel_gmbus *bus = to_intel_gmbus(adapter); |
| 853 | |
Adam Jackson | d5090b9 | 2011-06-16 16:36:28 -0400 | [diff] [blame] | 854 | bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit) |
| 858 | { |
| 859 | struct intel_gmbus *bus = to_intel_gmbus(adapter); |
Ville Syrjälä | ade754e | 2016-03-07 17:56:58 +0200 | [diff] [blame] | 860 | struct drm_i915_private *dev_priv = bus->dev_priv; |
| 861 | |
| 862 | mutex_lock(&dev_priv->gmbus_mutex); |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 863 | |
Chris Wilson | f2ce9fa | 2012-11-10 15:58:21 +0000 | [diff] [blame] | 864 | 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ä | ade754e | 2016-03-07 17:56:58 +0200 | [diff] [blame] | 868 | |
| 869 | mutex_unlock(&dev_priv->gmbus_mutex); |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Tvrtko Ursulin | 4019644 | 2016-12-01 14:16:42 +0000 | [diff] [blame] | 872 | void intel_teardown_gmbus(struct drm_i915_private *dev_priv) |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 873 | { |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 874 | struct intel_gmbus *bus; |
| 875 | unsigned int pin; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 876 | |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 877 | for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) { |
Jani Nikula | 88ac793 | 2015-03-27 00:20:22 +0200 | [diff] [blame] | 878 | if (!intel_gmbus_is_valid_pin(dev_priv, pin)) |
Jani Nikula | 5ea6e5e | 2015-04-01 10:55:04 +0300 | [diff] [blame] | 879 | continue; |
| 880 | |
| 881 | bus = &dev_priv->gmbus[pin]; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 882 | i2c_del_adapter(&bus->adapter); |
| 883 | } |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 884 | } |