2 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2008,2010 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
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:
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
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.
26 * Eric Anholt <eric@anholt.net>
27 * Chris Wilson <chris@chris-wilson.co.uk>
29 #include <linux/module.h>
30 #include <linux/i2c.h>
31 #include <linux/i2c-algo-bit.h>
33 #include "psb_intel_drv.h"
34 #include <drm/gma_drm.h>
36 #include "psb_intel_reg.h"
38 #define _wait_for(COND, MS, W) ({ \
39 unsigned long timeout__ = jiffies + msecs_to_jiffies(MS); \
42 if (time_after(jiffies, timeout__)) { \
46 if (W && !(in_atomic() || in_dbg_master())) msleep(W); \
51 #define wait_for(COND, MS) _wait_for(COND, MS, 1)
52 #define wait_for_atomic(COND, MS) _wait_for(COND, MS, 0)
54 #define GMBUS_REG_READ(reg) ioread32(dev_priv->gmbus_reg + (reg))
55 #define GMBUS_REG_WRITE(reg, val) iowrite32((val), dev_priv->gmbus_reg + (reg))
57 /* Intel GPIO access functions */
59 #define I2C_RISEFALL_TIME 20
61 static inline struct intel_gmbus
*
62 to_intel_gmbus(struct i2c_adapter
*i2c
)
64 return container_of(i2c
, struct intel_gmbus
, adapter
);
68 struct i2c_adapter adapter
;
69 struct i2c_algo_bit_data algo
;
70 struct drm_psb_private
*dev_priv
;
75 gma_intel_i2c_reset(struct drm_device
*dev
)
77 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
78 GMBUS_REG_WRITE(GMBUS0
, 0);
81 static void intel_i2c_quirk_set(struct drm_psb_private
*dev_priv
, bool enable
)
83 /* When using bit bashing for I2C, this bit needs to be set to 1 */
84 /* FIXME: We are never Pineview, right?
88 if (!IS_PINEVIEW(dev_priv->dev))
91 val = REG_READ(DSPCLK_GATE_D);
93 val |= DPCUNIT_CLOCK_GATE_DISABLE;
95 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
96 REG_WRITE(DSPCLK_GATE_D, val);
102 static u32
get_reserved(struct intel_gpio
*gpio
)
104 struct drm_psb_private
*dev_priv
= gpio
->dev_priv
;
107 /* On most chips, these bits must be preserved in software. */
108 reserved
= GMBUS_REG_READ(gpio
->reg
) &
109 (GPIO_DATA_PULLUP_DISABLE
|
110 GPIO_CLOCK_PULLUP_DISABLE
);
115 static int get_clock(void *data
)
117 struct intel_gpio
*gpio
= data
;
118 struct drm_psb_private
*dev_priv
= gpio
->dev_priv
;
119 u32 reserved
= get_reserved(gpio
);
120 GMBUS_REG_WRITE(gpio
->reg
, reserved
| GPIO_CLOCK_DIR_MASK
);
121 GMBUS_REG_WRITE(gpio
->reg
, reserved
);
122 return (GMBUS_REG_READ(gpio
->reg
) & GPIO_CLOCK_VAL_IN
) != 0;
125 static int get_data(void *data
)
127 struct intel_gpio
*gpio
= data
;
128 struct drm_psb_private
*dev_priv
= gpio
->dev_priv
;
129 u32 reserved
= get_reserved(gpio
);
130 GMBUS_REG_WRITE(gpio
->reg
, reserved
| GPIO_DATA_DIR_MASK
);
131 GMBUS_REG_WRITE(gpio
->reg
, reserved
);
132 return (GMBUS_REG_READ(gpio
->reg
) & GPIO_DATA_VAL_IN
) != 0;
135 static void set_clock(void *data
, int state_high
)
137 struct intel_gpio
*gpio
= data
;
138 struct drm_psb_private
*dev_priv
= gpio
->dev_priv
;
139 u32 reserved
= get_reserved(gpio
);
143 clock_bits
= GPIO_CLOCK_DIR_IN
| GPIO_CLOCK_DIR_MASK
;
145 clock_bits
= GPIO_CLOCK_DIR_OUT
| GPIO_CLOCK_DIR_MASK
|
148 GMBUS_REG_WRITE(gpio
->reg
, reserved
| clock_bits
);
149 GMBUS_REG_READ(gpio
->reg
); /* Posting */
152 static void set_data(void *data
, int state_high
)
154 struct intel_gpio
*gpio
= data
;
155 struct drm_psb_private
*dev_priv
= gpio
->dev_priv
;
156 u32 reserved
= get_reserved(gpio
);
160 data_bits
= GPIO_DATA_DIR_IN
| GPIO_DATA_DIR_MASK
;
162 data_bits
= GPIO_DATA_DIR_OUT
| GPIO_DATA_DIR_MASK
|
165 GMBUS_REG_WRITE(gpio
->reg
, reserved
| data_bits
);
166 GMBUS_REG_READ(gpio
->reg
);
169 static struct i2c_adapter
*
170 intel_gpio_create(struct drm_psb_private
*dev_priv
, u32 pin
)
172 static const int map_pin_to_reg
[] = {
182 struct intel_gpio
*gpio
;
184 if (pin
>= ARRAY_SIZE(map_pin_to_reg
) || !map_pin_to_reg
[pin
])
187 gpio
= kzalloc(sizeof(struct intel_gpio
), GFP_KERNEL
);
191 gpio
->reg
= map_pin_to_reg
[pin
];
192 gpio
->dev_priv
= dev_priv
;
194 snprintf(gpio
->adapter
.name
, sizeof(gpio
->adapter
.name
),
195 "gma500 GPIO%c", "?BACDE?F"[pin
]);
196 gpio
->adapter
.owner
= THIS_MODULE
;
197 gpio
->adapter
.algo_data
= &gpio
->algo
;
198 gpio
->adapter
.dev
.parent
= &dev_priv
->dev
->pdev
->dev
;
199 gpio
->algo
.setsda
= set_data
;
200 gpio
->algo
.setscl
= set_clock
;
201 gpio
->algo
.getsda
= get_data
;
202 gpio
->algo
.getscl
= get_clock
;
203 gpio
->algo
.udelay
= I2C_RISEFALL_TIME
;
204 gpio
->algo
.timeout
= usecs_to_jiffies(2200);
205 gpio
->algo
.data
= gpio
;
207 if (i2c_bit_add_bus(&gpio
->adapter
))
210 return &gpio
->adapter
;
218 intel_i2c_quirk_xfer(struct drm_psb_private
*dev_priv
,
219 struct i2c_adapter
*adapter
,
220 struct i2c_msg
*msgs
,
223 struct intel_gpio
*gpio
= container_of(adapter
,
228 gma_intel_i2c_reset(dev_priv
->dev
);
230 intel_i2c_quirk_set(dev_priv
, true);
233 udelay(I2C_RISEFALL_TIME
);
235 ret
= adapter
->algo
->master_xfer(adapter
, msgs
, num
);
239 intel_i2c_quirk_set(dev_priv
, false);
245 gmbus_xfer(struct i2c_adapter
*adapter
,
246 struct i2c_msg
*msgs
,
249 struct intel_gmbus
*bus
= container_of(adapter
,
252 struct drm_psb_private
*dev_priv
= adapter
->algo_data
;
256 return intel_i2c_quirk_xfer(dev_priv
,
257 bus
->force_bit
, msgs
, num
);
261 GMBUS_REG_WRITE(GMBUS0
+ reg_offset
, bus
->reg0
);
263 for (i
= 0; i
< num
; i
++) {
264 u16 len
= msgs
[i
].len
;
265 u8
*buf
= msgs
[i
].buf
;
267 if (msgs
[i
].flags
& I2C_M_RD
) {
268 GMBUS_REG_WRITE(GMBUS1
+ reg_offset
,
270 (i
+ 1 == num
? GMBUS_CYCLE_STOP
: 0) |
271 (len
<< GMBUS_BYTE_COUNT_SHIFT
) |
272 (msgs
[i
].addr
<< GMBUS_SLAVE_ADDR_SHIFT
) |
273 GMBUS_SLAVE_READ
| GMBUS_SW_RDY
);
274 GMBUS_REG_READ(GMBUS2
+reg_offset
);
278 if (wait_for(GMBUS_REG_READ(GMBUS2
+ reg_offset
) &
279 (GMBUS_SATOER
| GMBUS_HW_RDY
), 50))
281 if (GMBUS_REG_READ(GMBUS2
+ reg_offset
) & GMBUS_SATOER
)
284 val
= GMBUS_REG_READ(GMBUS3
+ reg_offset
);
288 } while (--len
&& ++loop
< 4);
295 val
|= *buf
++ << (8 * loop
);
296 } while (--len
&& ++loop
< 4);
298 GMBUS_REG_WRITE(GMBUS3
+ reg_offset
, val
);
299 GMBUS_REG_WRITE(GMBUS1
+ reg_offset
,
300 (i
+ 1 == num
? GMBUS_CYCLE_STOP
: GMBUS_CYCLE_WAIT
) |
301 (msgs
[i
].len
<< GMBUS_BYTE_COUNT_SHIFT
) |
302 (msgs
[i
].addr
<< GMBUS_SLAVE_ADDR_SHIFT
) |
303 GMBUS_SLAVE_WRITE
| GMBUS_SW_RDY
);
304 GMBUS_REG_READ(GMBUS2
+reg_offset
);
307 if (wait_for(GMBUS_REG_READ(GMBUS2
+ reg_offset
) &
308 (GMBUS_SATOER
| GMBUS_HW_RDY
), 50))
310 if (GMBUS_REG_READ(GMBUS2
+ reg_offset
) &
316 val
|= *buf
++ << (8 * loop
);
317 } while (--len
&& ++loop
< 4);
319 GMBUS_REG_WRITE(GMBUS3
+ reg_offset
, val
);
320 GMBUS_REG_READ(GMBUS2
+reg_offset
);
324 if (i
+ 1 < num
&& wait_for(GMBUS_REG_READ(GMBUS2
+ reg_offset
) & (GMBUS_SATOER
| GMBUS_HW_WAIT_PHASE
), 50))
326 if (GMBUS_REG_READ(GMBUS2
+ reg_offset
) & GMBUS_SATOER
)
333 /* Toggle the Software Clear Interrupt bit. This has the effect
334 * of resetting the GMBUS controller and so clearing the
335 * BUS_ERROR raised by the slave's NAK.
337 GMBUS_REG_WRITE(GMBUS1
+ reg_offset
, GMBUS_SW_CLR_INT
);
338 GMBUS_REG_WRITE(GMBUS1
+ reg_offset
, 0);
341 /* Mark the GMBUS interface as disabled. We will re-enable it at the
342 * start of the next xfer, till then let it sleep.
344 GMBUS_REG_WRITE(GMBUS0
+ reg_offset
, 0);
348 DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n",
349 bus
->reg0
& 0xff, bus
->adapter
.name
);
350 GMBUS_REG_WRITE(GMBUS0
+ reg_offset
, 0);
352 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
353 bus
->force_bit
= intel_gpio_create(dev_priv
, bus
->reg0
& 0xff);
357 return intel_i2c_quirk_xfer(dev_priv
, bus
->force_bit
, msgs
, num
);
360 static u32
gmbus_func(struct i2c_adapter
*adapter
)
362 struct intel_gmbus
*bus
= container_of(adapter
,
367 bus
->force_bit
->algo
->functionality(bus
->force_bit
);
369 return (I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
370 /* I2C_FUNC_10BIT_ADDR | */
371 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
372 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
);
375 static const struct i2c_algorithm gmbus_algorithm
= {
376 .master_xfer
= gmbus_xfer
,
377 .functionality
= gmbus_func
381 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
384 int gma_intel_setup_gmbus(struct drm_device
*dev
)
386 static const char *names
[GMBUS_NUM_PORTS
] = {
396 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
399 dev_priv
->gmbus
= kcalloc(GMBUS_NUM_PORTS
, sizeof(struct intel_gmbus
),
401 if (dev_priv
->gmbus
== NULL
)
405 dev_priv
->gmbus_reg
= dev_priv
->aux_reg
;
407 dev_priv
->gmbus_reg
= dev_priv
->vdc_reg
;
409 for (i
= 0; i
< GMBUS_NUM_PORTS
; i
++) {
410 struct intel_gmbus
*bus
= &dev_priv
->gmbus
[i
];
412 bus
->adapter
.owner
= THIS_MODULE
;
413 bus
->adapter
.class = I2C_CLASS_DDC
;
414 snprintf(bus
->adapter
.name
,
415 sizeof(bus
->adapter
.name
),
419 bus
->adapter
.dev
.parent
= &dev
->pdev
->dev
;
420 bus
->adapter
.algo_data
= dev_priv
;
422 bus
->adapter
.algo
= &gmbus_algorithm
;
423 ret
= i2c_add_adapter(&bus
->adapter
);
427 /* By default use a conservative clock rate */
428 bus
->reg0
= i
| GMBUS_RATE_100KHZ
;
430 /* XXX force bit banging until GMBUS is fully debugged */
431 bus
->force_bit
= intel_gpio_create(dev_priv
, i
);
434 gma_intel_i2c_reset(dev_priv
->dev
);
440 struct intel_gmbus
*bus
= &dev_priv
->gmbus
[i
];
441 i2c_del_adapter(&bus
->adapter
);
443 kfree(dev_priv
->gmbus
);
444 dev_priv
->gmbus
= NULL
;
448 void gma_intel_gmbus_set_speed(struct i2c_adapter
*adapter
, int speed
)
450 struct intel_gmbus
*bus
= to_intel_gmbus(adapter
);
458 bus
->reg0
= (bus
->reg0
& ~(0x3 << 8)) | (speed
<< 8);
461 void gma_intel_gmbus_force_bit(struct i2c_adapter
*adapter
, bool force_bit
)
463 struct intel_gmbus
*bus
= to_intel_gmbus(adapter
);
466 if (bus
->force_bit
== NULL
) {
467 struct drm_psb_private
*dev_priv
= adapter
->algo_data
;
468 bus
->force_bit
= intel_gpio_create(dev_priv
,
472 if (bus
->force_bit
) {
473 i2c_del_adapter(bus
->force_bit
);
474 kfree(bus
->force_bit
);
475 bus
->force_bit
= NULL
;
480 void gma_intel_teardown_gmbus(struct drm_device
*dev
)
482 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
485 if (dev_priv
->gmbus
== NULL
)
488 for (i
= 0; i
< GMBUS_NUM_PORTS
; i
++) {
489 struct intel_gmbus
*bus
= &dev_priv
->gmbus
[i
];
490 if (bus
->force_bit
) {
491 i2c_del_adapter(bus
->force_bit
);
492 kfree(bus
->force_bit
);
494 i2c_del_adapter(&bus
->adapter
);
497 dev_priv
->gmbus_reg
= NULL
; /* iounmap is done in driver_unload */
498 kfree(dev_priv
->gmbus
);
499 dev_priv
->gmbus
= NULL
;