NV50: A minor change.
[platform/upstream/libdrm.git] / linux-core / intel_i2c.c
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Eric Anholt <eric@anholt.net>
25  */
26 /*
27  * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
28  *   Jesse Barnes <jesse.barnes@intel.com>
29  */
30
31 #include <linux/i2c.h>
32 #include <linux/i2c-id.h>
33 #include <linux/i2c-algo-bit.h>
34 #include "drmP.h"
35 #include "drm.h"
36 #include "intel_drv.h"
37 #include "i915_drm.h"
38 #include "i915_drv.h"
39
40 /*
41  * Intel GPIO access functions
42  */
43
44 #define I2C_RISEFALL_TIME 20
45
46 static int get_clock(void *data)
47 {
48         struct intel_i2c_chan *chan = data;
49         struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
50         u32 val;
51
52         val = I915_READ(chan->reg);
53         return ((val & GPIO_CLOCK_VAL_IN) != 0);
54 }
55
56 static int get_data(void *data)
57 {
58         struct intel_i2c_chan *chan = data;
59         struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
60         u32 val;
61
62         val = I915_READ(chan->reg);
63         return ((val & GPIO_DATA_VAL_IN) != 0);
64 }
65
66 static void set_clock(void *data, int state_high)
67 {
68         struct intel_i2c_chan *chan = data;
69         struct drm_device *dev = chan->drm_dev;
70         struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
71         u32 reserved = 0, clock_bits;
72
73         /* On most chips, these bits must be preserved in software. */
74         if (!IS_I830(dev) && !IS_845G(dev))
75                 reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
76                                                    GPIO_CLOCK_PULLUP_DISABLE);
77
78         if (state_high)
79                 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
80         else
81                 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
82                         GPIO_CLOCK_VAL_MASK;
83         I915_WRITE(chan->reg, reserved | clock_bits);
84         udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
85 }
86
87 static void set_data(void *data, int state_high)
88 {
89         struct intel_i2c_chan *chan = data;
90         struct drm_device *dev = chan->drm_dev;
91         struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
92         u32 reserved = 0, data_bits;
93
94         /* On most chips, these bits must be preserved in software. */
95         if (!IS_I830(dev) && !IS_845G(dev))
96                 reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
97                                                    GPIO_CLOCK_PULLUP_DISABLE);
98
99         if (state_high)
100                 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
101         else
102                 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
103                         GPIO_DATA_VAL_MASK;
104
105         I915_WRITE(chan->reg, reserved | data_bits);
106         udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
107 }
108
109 /**
110  * intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
111  * @dev: DRM device
112  * @output: driver specific output device
113  * @reg: GPIO reg to use
114  * @name: name for this bus
115  *
116  * Creates and registers a new i2c bus with the Linux i2c layer, for use
117  * in output probing and control (e.g. DDC or SDVO control functions).
118  *
119  * Possible values for @reg include:
120  *   %GPIOA
121  *   %GPIOB
122  *   %GPIOC
123  *   %GPIOD
124  *   %GPIOE
125  *   %GPIOF
126  *   %GPIOG
127  *   %GPIOH
128  * see PRM for details on how these different busses are used.
129  */
130 struct intel_i2c_chan *intel_i2c_create(struct drm_device *dev, const u32 reg,
131                                         const char *name)
132 {
133         struct intel_i2c_chan *chan;
134
135         chan = kzalloc(sizeof(struct intel_i2c_chan), GFP_KERNEL);
136         if (!chan)
137                 goto out_free;
138
139         chan->drm_dev = dev;
140         chan->reg = reg;
141         snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
142         chan->adapter.owner = THIS_MODULE;
143 #ifndef I2C_HW_B_INTELFB
144 #define I2C_HW_B_INTELFB I2C_HW_B_I810
145 #endif
146         chan->adapter.id = I2C_HW_B_INTELFB;
147         chan->adapter.algo_data = &chan->algo;
148         chan->adapter.dev.parent = &dev->pdev->dev;
149         chan->algo.setsda = set_data;
150         chan->algo.setscl = set_clock;
151         chan->algo.getsda = get_data;
152         chan->algo.getscl = get_clock;
153         chan->algo.udelay = 20;
154         chan->algo.timeout = usecs_to_jiffies(2200);
155         chan->algo.data = chan;
156
157         i2c_set_adapdata(&chan->adapter, chan);
158
159         if(i2c_bit_add_bus(&chan->adapter))
160                 goto out_free;
161
162         /* JJJ:  raise SCL and SDA? */
163         set_data(chan, 1);
164         set_clock(chan, 1);
165         udelay(20);
166
167         return chan;
168
169 out_free:
170         kfree(chan);
171         return NULL;
172 }
173
174 /**
175  * intel_i2c_destroy - unregister and free i2c bus resources
176  * @output: channel to free
177  *
178  * Unregister the adapter from the i2c layer, then free the structure.
179  */
180 void intel_i2c_destroy(struct intel_i2c_chan *chan)
181 {
182         if (!chan)
183                 return;
184
185         i2c_del_adapter(&chan->adapter);
186         kfree(chan);
187 }
188
189         
190