2 * Copyright © 2006-2007 Intel Corporation
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 * Eric Anholt <eric@anholt.net>
21 #include <linux/i2c.h>
22 #include <linux/i2c-algo-bit.h>
25 #include "psb_intel_reg.h"
28 * Intel GPIO access functions
31 #define I2C_RISEFALL_TIME 20
33 static int get_clock(void *data
)
35 struct psb_intel_i2c_chan
*chan
= data
;
36 struct drm_device
*dev
= chan
->drm_dev
;
39 val
= REG_READ(chan
->reg
);
40 return (val
& GPIO_CLOCK_VAL_IN
) != 0;
43 static int get_data(void *data
)
45 struct psb_intel_i2c_chan
*chan
= data
;
46 struct drm_device
*dev
= chan
->drm_dev
;
49 val
= REG_READ(chan
->reg
);
50 return (val
& GPIO_DATA_VAL_IN
) != 0;
53 static void set_clock(void *data
, int state_high
)
55 struct psb_intel_i2c_chan
*chan
= data
;
56 struct drm_device
*dev
= chan
->drm_dev
;
57 u32 reserved
= 0, clock_bits
;
59 /* On most chips, these bits must be preserved in software. */
61 REG_READ(chan
->reg
) & (GPIO_DATA_PULLUP_DISABLE
|
62 GPIO_CLOCK_PULLUP_DISABLE
);
65 clock_bits
= GPIO_CLOCK_DIR_IN
| GPIO_CLOCK_DIR_MASK
;
67 clock_bits
= GPIO_CLOCK_DIR_OUT
| GPIO_CLOCK_DIR_MASK
|
69 REG_WRITE(chan
->reg
, reserved
| clock_bits
);
70 udelay(I2C_RISEFALL_TIME
); /* wait for the line to change state */
73 static void set_data(void *data
, int state_high
)
75 struct psb_intel_i2c_chan
*chan
= data
;
76 struct drm_device
*dev
= chan
->drm_dev
;
77 u32 reserved
= 0, data_bits
;
79 /* On most chips, these bits must be preserved in software. */
81 REG_READ(chan
->reg
) & (GPIO_DATA_PULLUP_DISABLE
|
82 GPIO_CLOCK_PULLUP_DISABLE
);
85 data_bits
= GPIO_DATA_DIR_IN
| GPIO_DATA_DIR_MASK
;
88 GPIO_DATA_DIR_OUT
| GPIO_DATA_DIR_MASK
|
91 REG_WRITE(chan
->reg
, reserved
| data_bits
);
92 udelay(I2C_RISEFALL_TIME
); /* wait for the line to change state */
96 * psb_intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
98 * @output: driver specific output device
99 * @reg: GPIO reg to use
100 * @name: name for this bus
102 * Creates and registers a new i2c bus with the Linux i2c layer, for use
103 * in output probing and control (e.g. DDC or SDVO control functions).
105 * Possible values for @reg include:
114 * see PRM for details on how these different busses are used.
116 struct psb_intel_i2c_chan
*psb_intel_i2c_create(struct drm_device
*dev
,
117 const u32 reg
, const char *name
)
119 struct psb_intel_i2c_chan
*chan
;
121 chan
= kzalloc(sizeof(struct psb_intel_i2c_chan
), GFP_KERNEL
);
127 snprintf(chan
->adapter
.name
, I2C_NAME_SIZE
, "intel drm %s", name
);
128 chan
->adapter
.owner
= THIS_MODULE
;
129 chan
->adapter
.algo_data
= &chan
->algo
;
130 chan
->adapter
.dev
.parent
= &dev
->pdev
->dev
;
131 chan
->algo
.setsda
= set_data
;
132 chan
->algo
.setscl
= set_clock
;
133 chan
->algo
.getsda
= get_data
;
134 chan
->algo
.getscl
= get_clock
;
135 chan
->algo
.udelay
= 20;
136 chan
->algo
.timeout
= usecs_to_jiffies(2200);
137 chan
->algo
.data
= chan
;
139 i2c_set_adapdata(&chan
->adapter
, chan
);
141 if (i2c_bit_add_bus(&chan
->adapter
))
144 /* JJJ: raise SCL and SDA? */
157 * psb_intel_i2c_destroy - unregister and free i2c bus resources
158 * @output: channel to free
160 * Unregister the adapter from the i2c layer, then free the structure.
162 void psb_intel_i2c_destroy(struct psb_intel_i2c_chan
*chan
)
167 i2c_del_adapter(&chan
->adapter
);