parisc: Fix pagefault crash in unaligned __get_user() call
[pandora-kernel.git] / drivers / staging / gma500 / intel_i2c.c
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
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.
7  *
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
11  * more details.
12  *
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.
16  *
17  * Authors:
18  *      Eric Anholt <eric@anholt.net>
19  */
20
21 #include <linux/i2c.h>
22 #include <linux/i2c-algo-bit.h>
23 #include <linux/export.h>
24
25 #include "psb_drv.h"
26 #include "psb_intel_reg.h"
27
28 /*
29  * Intel GPIO access functions
30  */
31
32 #define I2C_RISEFALL_TIME 20
33
34 static int get_clock(void *data)
35 {
36         struct psb_intel_i2c_chan *chan = data;
37         struct drm_device *dev = chan->drm_dev;
38         u32 val;
39
40         val = REG_READ(chan->reg);
41         return (val & GPIO_CLOCK_VAL_IN) != 0;
42 }
43
44 static int get_data(void *data)
45 {
46         struct psb_intel_i2c_chan *chan = data;
47         struct drm_device *dev = chan->drm_dev;
48         u32 val;
49
50         val = REG_READ(chan->reg);
51         return (val & GPIO_DATA_VAL_IN) != 0;
52 }
53
54 static void set_clock(void *data, int state_high)
55 {
56         struct psb_intel_i2c_chan *chan = data;
57         struct drm_device *dev = chan->drm_dev;
58         u32 reserved = 0, clock_bits;
59
60         /* On most chips, these bits must be preserved in software. */
61         reserved =
62                     REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
63                                            GPIO_CLOCK_PULLUP_DISABLE);
64
65         if (state_high)
66                 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
67         else
68                 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
69                     GPIO_CLOCK_VAL_MASK;
70         REG_WRITE(chan->reg, reserved | clock_bits);
71         udelay(I2C_RISEFALL_TIME);      /* wait for the line to change state */
72 }
73
74 static void set_data(void *data, int state_high)
75 {
76         struct psb_intel_i2c_chan *chan = data;
77         struct drm_device *dev = chan->drm_dev;
78         u32 reserved = 0, data_bits;
79
80         /* On most chips, these bits must be preserved in software. */
81         reserved =
82                     REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
83                                            GPIO_CLOCK_PULLUP_DISABLE);
84
85         if (state_high)
86                 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
87         else
88                 data_bits =
89                     GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
90                     GPIO_DATA_VAL_MASK;
91
92         REG_WRITE(chan->reg, reserved | data_bits);
93         udelay(I2C_RISEFALL_TIME);      /* wait for the line to change state */
94 }
95
96 /**
97  * psb_intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
98  * @dev: DRM device
99  * @output: driver specific output device
100  * @reg: GPIO reg to use
101  * @name: name for this bus
102  *
103  * Creates and registers a new i2c bus with the Linux i2c layer, for use
104  * in output probing and control (e.g. DDC or SDVO control functions).
105  *
106  * Possible values for @reg include:
107  *   %GPIOA
108  *   %GPIOB
109  *   %GPIOC
110  *   %GPIOD
111  *   %GPIOE
112  *   %GPIOF
113  *   %GPIOG
114  *   %GPIOH
115  * see PRM for details on how these different busses are used.
116  */
117 struct psb_intel_i2c_chan *psb_intel_i2c_create(struct drm_device *dev,
118                                         const u32 reg, const char *name)
119 {
120         struct psb_intel_i2c_chan *chan;
121
122         chan = kzalloc(sizeof(struct psb_intel_i2c_chan), GFP_KERNEL);
123         if (!chan)
124                 goto out_free;
125
126         chan->drm_dev = dev;
127         chan->reg = reg;
128         snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
129         chan->adapter.owner = THIS_MODULE;
130         chan->adapter.algo_data = &chan->algo;
131         chan->adapter.dev.parent = &dev->pdev->dev;
132         chan->algo.setsda = set_data;
133         chan->algo.setscl = set_clock;
134         chan->algo.getsda = get_data;
135         chan->algo.getscl = get_clock;
136         chan->algo.udelay = 20;
137         chan->algo.timeout = usecs_to_jiffies(2200);
138         chan->algo.data = chan;
139
140         i2c_set_adapdata(&chan->adapter, chan);
141
142         if (i2c_bit_add_bus(&chan->adapter))
143                 goto out_free;
144
145         /* JJJ:  raise SCL and SDA? */
146         set_data(chan, 1);
147         set_clock(chan, 1);
148         udelay(20);
149
150         return chan;
151
152 out_free:
153         kfree(chan);
154         return NULL;
155 }
156
157 /**
158  * psb_intel_i2c_destroy - unregister and free i2c bus resources
159  * @output: channel to free
160  *
161  * Unregister the adapter from the i2c layer, then free the structure.
162  */
163 void psb_intel_i2c_destroy(struct psb_intel_i2c_chan *chan)
164 {
165         if (!chan)
166                 return;
167
168         i2c_del_adapter(&chan->adapter);
169         kfree(chan);
170 }