gma500: Fix DPU build
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / gma500 / mrst_hdmi_i2c.c
blob351b9d897b9fd45bd31793c3fb2bd1a1e0e34486
1 /*
2 * Copyright © 2010 Intel Corporation
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:
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.
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.
23 * Authors:
24 * Li Peng <peng.li@intel.com>
27 #include <linux/mutex.h>
28 #include <linux/pci.h>
29 #include <linux/i2c.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include "psb_drv.h"
34 #define HDMI_READ(reg) readl(hdmi_dev->regs + (reg))
35 #define HDMI_WRITE(reg, val) writel(val, hdmi_dev->regs + (reg))
37 #define HDMI_HCR 0x1000
38 #define HCR_DETECT_HDP (1 << 6)
39 #define HCR_ENABLE_HDCP (1 << 5)
40 #define HCR_ENABLE_AUDIO (1 << 2)
41 #define HCR_ENABLE_PIXEL (1 << 1)
42 #define HCR_ENABLE_TMDS (1 << 0)
43 #define HDMI_HICR 0x1004
44 #define HDMI_INTR_I2C_ERROR (1 << 4)
45 #define HDMI_INTR_I2C_FULL (1 << 3)
46 #define HDMI_INTR_I2C_DONE (1 << 2)
47 #define HDMI_INTR_HPD (1 << 0)
48 #define HDMI_HSR 0x1008
49 #define HDMI_HISR 0x100C
50 #define HDMI_HI2CRDB0 0x1200
51 #define HDMI_HI2CHCR 0x1240
52 #define HI2C_HDCP_WRITE (0 << 2)
53 #define HI2C_HDCP_RI_READ (1 << 2)
54 #define HI2C_HDCP_READ (2 << 2)
55 #define HI2C_EDID_READ (3 << 2)
56 #define HI2C_READ_CONTINUE (1 << 1)
57 #define HI2C_ENABLE_TRANSACTION (1 << 0)
59 #define HDMI_ICRH 0x1100
60 #define HDMI_HI2CTDR0 0x1244
61 #define HDMI_HI2CTDR1 0x1248
63 #define I2C_STAT_INIT 0
64 #define I2C_READ_DONE 1
65 #define I2C_TRANSACTION_DONE 2
67 struct hdmi_i2c_dev {
68 struct i2c_adapter *adap;
69 struct mutex i2c_lock;
70 struct completion complete;
71 int status;
72 struct i2c_msg *msg;
73 int buf_offset;
76 static void hdmi_i2c_irq_enable(struct mrst_hdmi_dev *hdmi_dev)
78 u32 temp;
80 temp = HDMI_READ(HDMI_HICR);
81 temp |= (HDMI_INTR_I2C_ERROR | HDMI_INTR_I2C_FULL | HDMI_INTR_I2C_DONE);
82 HDMI_WRITE(HDMI_HICR, temp);
83 HDMI_READ(HDMI_HICR);
86 static void hdmi_i2c_irq_disable(struct mrst_hdmi_dev *hdmi_dev)
88 HDMI_WRITE(HDMI_HICR, 0x0);
89 HDMI_READ(HDMI_HICR);
92 static int xfer_read(struct i2c_adapter *adap, struct i2c_msg *pmsg)
94 struct mrst_hdmi_dev *hdmi_dev = i2c_get_adapdata(adap);
95 struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
96 u32 temp;
98 i2c_dev->status = I2C_STAT_INIT;
99 i2c_dev->msg = pmsg;
100 i2c_dev->buf_offset = 0;
101 INIT_COMPLETION(i2c_dev->complete);
103 /* Enable I2C transaction */
104 temp = ((pmsg->len) << 20) | HI2C_EDID_READ | HI2C_ENABLE_TRANSACTION;
105 HDMI_WRITE(HDMI_HI2CHCR, temp);
106 HDMI_READ(HDMI_HI2CHCR);
108 while (i2c_dev->status != I2C_TRANSACTION_DONE)
109 wait_for_completion_interruptible_timeout(&i2c_dev->complete,
110 10 * HZ);
112 return 0;
115 static int xfer_write(struct i2c_adapter *adap, struct i2c_msg *pmsg)
118 * XXX: i2c write seems isn't useful for EDID probe, don't do anything
120 return 0;
123 static int mrst_hdmi_i2c_access(struct i2c_adapter *adap,
124 struct i2c_msg *pmsg,
125 int num)
127 struct mrst_hdmi_dev *hdmi_dev = i2c_get_adapdata(adap);
128 struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
129 int i, err = 0;
131 mutex_lock(&i2c_dev->i2c_lock);
133 /* Enable i2c unit */
134 HDMI_WRITE(HDMI_ICRH, 0x00008760);
136 /* Enable irq */
137 hdmi_i2c_irq_enable(hdmi_dev);
138 for (i = 0; i < num; i++) {
139 if (pmsg->len && pmsg->buf) {
140 if (pmsg->flags & I2C_M_RD)
141 err = xfer_read(adap, pmsg);
142 else
143 err = xfer_write(adap, pmsg);
145 pmsg++; /* next message */
148 /* Disable irq */
149 hdmi_i2c_irq_disable(hdmi_dev);
151 mutex_unlock(&i2c_dev->i2c_lock);
153 return i;
156 static u32 mrst_hdmi_i2c_func(struct i2c_adapter *adapter)
158 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
161 static const struct i2c_algorithm mrst_hdmi_i2c_algorithm = {
162 .master_xfer = mrst_hdmi_i2c_access,
163 .functionality = mrst_hdmi_i2c_func,
166 static struct i2c_adapter mrst_hdmi_i2c_adapter = {
167 .name = "mrst_hdmi_i2c",
168 .nr = 3,
169 .owner = THIS_MODULE,
170 .class = I2C_CLASS_DDC,
171 .algo = &mrst_hdmi_i2c_algorithm,
174 static void hdmi_i2c_read(struct mrst_hdmi_dev *hdmi_dev)
176 struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
177 struct i2c_msg *msg = i2c_dev->msg;
178 u8 *buf = msg->buf;
179 u32 temp;
180 int i, offset;
182 offset = i2c_dev->buf_offset;
183 for (i = 0; i < 0x10; i++) {
184 temp = HDMI_READ(HDMI_HI2CRDB0 + (i * 4));
185 memcpy(buf + (offset + i * 4), &temp, 4);
187 i2c_dev->buf_offset += (0x10 * 4);
189 /* clearing read buffer full intr */
190 temp = HDMI_READ(HDMI_HISR);
191 HDMI_WRITE(HDMI_HISR, temp | HDMI_INTR_I2C_FULL);
192 HDMI_READ(HDMI_HISR);
194 /* continue read transaction */
195 temp = HDMI_READ(HDMI_HI2CHCR);
196 HDMI_WRITE(HDMI_HI2CHCR, temp | HI2C_READ_CONTINUE);
197 HDMI_READ(HDMI_HI2CHCR);
199 i2c_dev->status = I2C_READ_DONE;
200 return;
203 static void hdmi_i2c_transaction_done(struct mrst_hdmi_dev *hdmi_dev)
205 struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
206 u32 temp;
208 /* clear transaction done intr */
209 temp = HDMI_READ(HDMI_HISR);
210 HDMI_WRITE(HDMI_HISR, temp | HDMI_INTR_I2C_DONE);
211 HDMI_READ(HDMI_HISR);
214 temp = HDMI_READ(HDMI_HI2CHCR);
215 HDMI_WRITE(HDMI_HI2CHCR, temp & ~HI2C_ENABLE_TRANSACTION);
216 HDMI_READ(HDMI_HI2CHCR);
218 i2c_dev->status = I2C_TRANSACTION_DONE;
219 return;
222 static irqreturn_t mrst_hdmi_i2c_handler(int this_irq, void *dev)
224 struct mrst_hdmi_dev *hdmi_dev = dev;
225 struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
226 u32 stat;
228 stat = HDMI_READ(HDMI_HISR);
230 if (stat & HDMI_INTR_HPD) {
231 HDMI_WRITE(HDMI_HISR, stat | HDMI_INTR_HPD);
232 HDMI_READ(HDMI_HISR);
235 if (stat & HDMI_INTR_I2C_FULL)
236 hdmi_i2c_read(hdmi_dev);
238 if (stat & HDMI_INTR_I2C_DONE)
239 hdmi_i2c_transaction_done(hdmi_dev);
241 complete(&i2c_dev->complete);
243 return IRQ_HANDLED;
247 * choose alternate function 2 of GPIO pin 52, 53,
248 * which is used by HDMI I2C logic
250 static void mrst_hdmi_i2c_gpio_fix(void)
252 void *base;
253 unsigned int gpio_base = 0xff12c000;
254 int gpio_len = 0x1000;
255 u32 temp;
257 base = ioremap((resource_size_t)gpio_base, gpio_len);
258 if (base == NULL) {
259 DRM_ERROR("gpio ioremap fail\n");
260 return;
263 temp = readl(base + 0x44);
264 DRM_DEBUG_DRIVER("old gpio val %x\n", temp);
265 writel((temp | 0x00000a00), (base + 0x44));
266 temp = readl(base + 0x44);
267 DRM_DEBUG_DRIVER("new gpio val %x\n", temp);
269 iounmap(base);
272 int mrst_hdmi_i2c_init(struct pci_dev *dev)
274 struct mrst_hdmi_dev *hdmi_dev;
275 struct hdmi_i2c_dev *i2c_dev;
276 int ret;
278 hdmi_dev = pci_get_drvdata(dev);
280 i2c_dev = kzalloc(sizeof(struct hdmi_i2c_dev), GFP_KERNEL);
281 if (i2c_dev == NULL) {
282 DRM_ERROR("Can't allocate interface\n");
283 ret = -ENOMEM;
284 goto exit;
287 i2c_dev->adap = &mrst_hdmi_i2c_adapter;
288 i2c_dev->status = I2C_STAT_INIT;
289 init_completion(&i2c_dev->complete);
290 mutex_init(&i2c_dev->i2c_lock);
291 i2c_set_adapdata(&mrst_hdmi_i2c_adapter, hdmi_dev);
292 hdmi_dev->i2c_dev = i2c_dev;
294 /* Enable HDMI I2C function on gpio */
295 mrst_hdmi_i2c_gpio_fix();
297 /* request irq */
298 ret = request_irq(dev->irq, mrst_hdmi_i2c_handler, IRQF_SHARED,
299 mrst_hdmi_i2c_adapter.name, hdmi_dev);
300 if (ret) {
301 DRM_ERROR("Failed to request IRQ for I2C controller\n");
302 goto err;
305 /* Adapter registration */
306 ret = i2c_add_numbered_adapter(&mrst_hdmi_i2c_adapter);
307 return ret;
309 err:
310 kfree(i2c_dev);
311 exit:
312 return ret;
315 void mrst_hdmi_i2c_exit(struct pci_dev *dev)
317 struct mrst_hdmi_dev *hdmi_dev;
318 struct hdmi_i2c_dev *i2c_dev;
320 hdmi_dev = pci_get_drvdata(dev);
321 if (i2c_del_adapter(&mrst_hdmi_i2c_adapter))
322 DRM_DEBUG_DRIVER("Failed to delete hdmi-i2c adapter\n");
324 i2c_dev = hdmi_dev->i2c_dev;
325 kfree(i2c_dev);
326 free_irq(dev->irq, hdmi_dev);