1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/export.h>
21#include <linux/i2c.h>
22#include <linux/i2c-algo-bit.h>
23
24#include "psb_drv.h"
25#include "psb_intel_reg.h"
26
27
28
29
30
31#define I2C_RISEFALL_TIME 20
32
33static int get_clock(void *data)
34{
35 struct psb_intel_i2c_chan *chan = data;
36 struct drm_device *dev = chan->drm_dev;
37 u32 val;
38
39 val = REG_READ(chan->reg);
40 return (val & GPIO_CLOCK_VAL_IN) != 0;
41}
42
43static int get_data(void *data)
44{
45 struct psb_intel_i2c_chan *chan = data;
46 struct drm_device *dev = chan->drm_dev;
47 u32 val;
48
49 val = REG_READ(chan->reg);
50 return (val & GPIO_DATA_VAL_IN) != 0;
51}
52
53static void set_clock(void *data, int state_high)
54{
55 struct psb_intel_i2c_chan *chan = data;
56 struct drm_device *dev = chan->drm_dev;
57 u32 reserved = 0, clock_bits;
58
59
60 reserved =
61 REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
62 GPIO_CLOCK_PULLUP_DISABLE);
63
64 if (state_high)
65 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
66 else
67 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
68 GPIO_CLOCK_VAL_MASK;
69 REG_WRITE(chan->reg, reserved | clock_bits);
70 udelay(I2C_RISEFALL_TIME);
71}
72
73static void set_data(void *data, int state_high)
74{
75 struct psb_intel_i2c_chan *chan = data;
76 struct drm_device *dev = chan->drm_dev;
77 u32 reserved = 0, data_bits;
78
79
80 reserved =
81 REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
82 GPIO_CLOCK_PULLUP_DISABLE);
83
84 if (state_high)
85 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
86 else
87 data_bits =
88 GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
89 GPIO_DATA_VAL_MASK;
90
91 REG_WRITE(chan->reg, reserved | data_bits);
92 udelay(I2C_RISEFALL_TIME);
93}
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116struct psb_intel_i2c_chan *psb_intel_i2c_create(struct drm_device *dev,
117 const u32 reg, const char *name)
118{
119 struct psb_intel_i2c_chan *chan;
120
121 chan = kzalloc(sizeof(struct psb_intel_i2c_chan), GFP_KERNEL);
122 if (!chan)
123 goto out_free;
124
125 chan->drm_dev = dev;
126 chan->reg = reg;
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;
138
139 i2c_set_adapdata(&chan->adapter, chan);
140
141 if (i2c_bit_add_bus(&chan->adapter))
142 goto out_free;
143
144
145 set_data(chan, 1);
146 set_clock(chan, 1);
147 udelay(20);
148
149 return chan;
150
151out_free:
152 kfree(chan);
153 return NULL;
154}
155
156
157
158
159
160
161
162void psb_intel_i2c_destroy(struct psb_intel_i2c_chan *chan)
163{
164 if (!chan)
165 return;
166
167 i2c_del_adapter(&chan->adapter);
168 kfree(chan);
169}
170