OSDN Git Service

initial import
[android-x86/device-viliv-s5.git] / psb-kernel-source-4.41.1 / psb_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 "psb_drv.h"
38
39 #define I2C_HW_B_INTELFB 0x010021; /* intel framebuffer driver */
40
41 /*
42  * Intel GPIO access functions
43  */
44
45 #define I2C_RISEFALL_TIME 20
46
47 static int get_clock(void *data)
48 {
49         struct intel_i2c_chan *chan = data;
50         struct drm_psb_private *dev_priv = chan->drm_dev->dev_private;
51         uint32_t val;
52
53         val = PSB_RVDC32(chan->reg);
54         return ((val & GPIO_CLOCK_VAL_IN) != 0);
55 }
56
57 static int get_data(void *data)
58 {
59         struct intel_i2c_chan *chan = data;
60         struct drm_psb_private *dev_priv = chan->drm_dev->dev_private;
61         uint32_t val;
62
63         val = PSB_RVDC32(chan->reg);
64         return ((val & GPIO_DATA_VAL_IN) != 0);
65 }
66
67 static void set_clock(void *data, int state_high)
68 {
69         struct intel_i2c_chan *chan = data;
70         struct drm_psb_private *dev_priv = chan->drm_dev->dev_private;
71         uint32_t reserved = 0, clock_bits;
72
73         /* On most chips, these bits must be preserved in software. */
74         reserved = PSB_RVDC32(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
75                                             GPIO_CLOCK_PULLUP_DISABLE);
76
77         if (state_high)
78                 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
79         else
80                 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
81                     GPIO_CLOCK_VAL_MASK;
82         PSB_WVDC32(reserved | clock_bits, chan->reg);
83         udelay(I2C_RISEFALL_TIME);      /* wait for the line to change state */
84 }
85
86 static void set_data(void *data, int state_high)
87 {
88         struct intel_i2c_chan *chan = data;
89         struct drm_psb_private *dev_priv = chan->drm_dev->dev_private;
90         uint32_t reserved = 0, data_bits;
91
92         /* On most chips, these bits must be preserved in software. */
93         reserved = PSB_RVDC32(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
94                                             GPIO_CLOCK_PULLUP_DISABLE);
95
96         if (state_high)
97                 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
98         else
99                 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
100                     GPIO_DATA_VAL_MASK;
101
102         PSB_WVDC32(data_bits, chan->reg);
103         udelay(I2C_RISEFALL_TIME);      /* wait for the line to change state */
104 }
105
106 /**
107  * intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
108  * @dev: DRM device
109  * @output: driver specific output device
110  * @reg: GPIO reg to use
111  * @name: name for this bus
112  *
113  * Creates and registers a new i2c bus with the Linux i2c layer, for use
114  * in output probing and control (e.g. DDC or SDVO control functions).
115  *
116  * Possible values for @reg include:
117  *   %GPIOA
118  *   %GPIOB
119  *   %GPIOC
120  *   %GPIOD
121  *   %GPIOE
122  *   %GPIOF
123  *   %GPIOG
124  *   %GPIOH
125  * see PRM for details on how these different busses are used.
126  */
127 struct intel_i2c_chan *intel_i2c_create(struct drm_device *dev,
128                                         const uint32_t reg, const char *name)
129 {
130         struct intel_i2c_chan *chan;
131
132         chan = kzalloc(sizeof(struct intel_i2c_chan), GFP_KERNEL);
133         if (!chan)
134                 goto out_free;
135
136         chan->drm_dev = dev;
137         chan->reg = reg;
138         snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
139         chan->adapter.owner = THIS_MODULE;
140         chan->adapter.id = I2C_HW_B_INTELFB;
141         chan->adapter.algo_data = &chan->algo;
142         chan->adapter.dev.parent = &dev->pdev->dev;
143         chan->algo.setsda = set_data;
144         chan->algo.setscl = set_clock;
145         chan->algo.getsda = get_data;
146         chan->algo.getscl = get_clock;
147         chan->algo.udelay = 20;
148         chan->algo.timeout = usecs_to_jiffies(2200);
149         chan->algo.data = chan;
150
151         i2c_set_adapdata(&chan->adapter, chan);
152
153         if (i2c_bit_add_bus(&chan->adapter))
154                 goto out_free;
155
156         /* JJJ:  raise SCL and SDA? */
157         set_data(chan, 1);
158         set_clock(chan, 1);
159         udelay(20);
160
161         return chan;
162
163       out_free:
164         kfree(chan);
165         return NULL;
166 }
167
168 /**
169  * intel_i2c_destroy - unregister and free i2c bus resources
170  * @output: channel to free
171  *
172  * Unregister the adapter from the i2c layer, then free the structure.
173  */
174 void intel_i2c_destroy(struct intel_i2c_chan *chan)
175 {
176         if (!chan)
177                 return;
178
179         i2c_del_adapter(&chan->adapter);
180         kfree(chan);
181 }