11b082ccfSAlan Cox /*
21b082ccfSAlan Cox * Copyright © 2010 Intel Corporation
31b082ccfSAlan Cox *
41b082ccfSAlan Cox * Permission is hereby granted, free of charge, to any person obtaining a
51b082ccfSAlan Cox * copy of this software and associated documentation files (the "Software"),
61b082ccfSAlan Cox * to deal in the Software without restriction, including without limitation
71b082ccfSAlan Cox * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81b082ccfSAlan Cox * and/or sell copies of the Software, and to permit persons to whom the
91b082ccfSAlan Cox * Software is furnished to do so, subject to the following conditions:
101b082ccfSAlan Cox *
111b082ccfSAlan Cox * The above copyright notice and this permission notice (including the next
121b082ccfSAlan Cox * paragraph) shall be included in all copies or substantial portions of the
131b082ccfSAlan Cox * Software.
141b082ccfSAlan Cox *
151b082ccfSAlan Cox * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161b082ccfSAlan Cox * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171b082ccfSAlan Cox * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
181b082ccfSAlan Cox * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191b082ccfSAlan Cox * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201b082ccfSAlan Cox * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211b082ccfSAlan Cox * DEALINGS IN THE SOFTWARE.
221b082ccfSAlan Cox *
231b082ccfSAlan Cox * Authors:
241b082ccfSAlan Cox * Li Peng <peng.li@intel.com>
251b082ccfSAlan Cox */
261b082ccfSAlan Cox
27af3a2cfbSDave Airlie #include <linux/export.h>
281b082ccfSAlan Cox #include <linux/mutex.h>
291b082ccfSAlan Cox #include <linux/pci.h>
301b082ccfSAlan Cox #include <linux/i2c.h>
311b082ccfSAlan Cox #include <linux/interrupt.h>
321b082ccfSAlan Cox #include <linux/delay.h>
331b082ccfSAlan Cox #include "psb_drv.h"
341b082ccfSAlan Cox
351b082ccfSAlan Cox #define HDMI_READ(reg) readl(hdmi_dev->regs + (reg))
361b082ccfSAlan Cox #define HDMI_WRITE(reg, val) writel(val, hdmi_dev->regs + (reg))
371b082ccfSAlan Cox
381b082ccfSAlan Cox #define HDMI_HCR 0x1000
391b082ccfSAlan Cox #define HCR_DETECT_HDP (1 << 6)
401b082ccfSAlan Cox #define HCR_ENABLE_HDCP (1 << 5)
411b082ccfSAlan Cox #define HCR_ENABLE_AUDIO (1 << 2)
421b082ccfSAlan Cox #define HCR_ENABLE_PIXEL (1 << 1)
431b082ccfSAlan Cox #define HCR_ENABLE_TMDS (1 << 0)
441b082ccfSAlan Cox #define HDMI_HICR 0x1004
451b082ccfSAlan Cox #define HDMI_INTR_I2C_ERROR (1 << 4)
461b082ccfSAlan Cox #define HDMI_INTR_I2C_FULL (1 << 3)
471b082ccfSAlan Cox #define HDMI_INTR_I2C_DONE (1 << 2)
481b082ccfSAlan Cox #define HDMI_INTR_HPD (1 << 0)
491b082ccfSAlan Cox #define HDMI_HSR 0x1008
501b082ccfSAlan Cox #define HDMI_HISR 0x100C
511b082ccfSAlan Cox #define HDMI_HI2CRDB0 0x1200
521b082ccfSAlan Cox #define HDMI_HI2CHCR 0x1240
531b082ccfSAlan Cox #define HI2C_HDCP_WRITE (0 << 2)
541b082ccfSAlan Cox #define HI2C_HDCP_RI_READ (1 << 2)
551b082ccfSAlan Cox #define HI2C_HDCP_READ (2 << 2)
561b082ccfSAlan Cox #define HI2C_EDID_READ (3 << 2)
571b082ccfSAlan Cox #define HI2C_READ_CONTINUE (1 << 1)
581b082ccfSAlan Cox #define HI2C_ENABLE_TRANSACTION (1 << 0)
591b082ccfSAlan Cox
601b082ccfSAlan Cox #define HDMI_ICRH 0x1100
611b082ccfSAlan Cox #define HDMI_HI2CTDR0 0x1244
621b082ccfSAlan Cox #define HDMI_HI2CTDR1 0x1248
631b082ccfSAlan Cox
641b082ccfSAlan Cox #define I2C_STAT_INIT 0
651b082ccfSAlan Cox #define I2C_READ_DONE 1
661b082ccfSAlan Cox #define I2C_TRANSACTION_DONE 2
671b082ccfSAlan Cox
681b082ccfSAlan Cox struct hdmi_i2c_dev {
691b082ccfSAlan Cox struct i2c_adapter *adap;
701b082ccfSAlan Cox struct mutex i2c_lock;
711b082ccfSAlan Cox struct completion complete;
721b082ccfSAlan Cox int status;
731b082ccfSAlan Cox struct i2c_msg *msg;
741b082ccfSAlan Cox int buf_offset;
751b082ccfSAlan Cox };
761b082ccfSAlan Cox
hdmi_i2c_irq_enable(struct oaktrail_hdmi_dev * hdmi_dev)771b082ccfSAlan Cox static void hdmi_i2c_irq_enable(struct oaktrail_hdmi_dev *hdmi_dev)
781b082ccfSAlan Cox {
791b082ccfSAlan Cox u32 temp;
801b082ccfSAlan Cox
811b082ccfSAlan Cox temp = HDMI_READ(HDMI_HICR);
821b082ccfSAlan Cox temp |= (HDMI_INTR_I2C_ERROR | HDMI_INTR_I2C_FULL | HDMI_INTR_I2C_DONE);
831b082ccfSAlan Cox HDMI_WRITE(HDMI_HICR, temp);
841b082ccfSAlan Cox HDMI_READ(HDMI_HICR);
851b082ccfSAlan Cox }
861b082ccfSAlan Cox
hdmi_i2c_irq_disable(struct oaktrail_hdmi_dev * hdmi_dev)871b082ccfSAlan Cox static void hdmi_i2c_irq_disable(struct oaktrail_hdmi_dev *hdmi_dev)
881b082ccfSAlan Cox {
891b082ccfSAlan Cox HDMI_WRITE(HDMI_HICR, 0x0);
901b082ccfSAlan Cox HDMI_READ(HDMI_HICR);
911b082ccfSAlan Cox }
921b082ccfSAlan Cox
xfer_read(struct i2c_adapter * adap,struct i2c_msg * pmsg)931b082ccfSAlan Cox static int xfer_read(struct i2c_adapter *adap, struct i2c_msg *pmsg)
941b082ccfSAlan Cox {
951b082ccfSAlan Cox struct oaktrail_hdmi_dev *hdmi_dev = i2c_get_adapdata(adap);
961b082ccfSAlan Cox struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
971b082ccfSAlan Cox u32 temp;
981b082ccfSAlan Cox
991b082ccfSAlan Cox i2c_dev->status = I2C_STAT_INIT;
1001b082ccfSAlan Cox i2c_dev->msg = pmsg;
1011b082ccfSAlan Cox i2c_dev->buf_offset = 0;
10216735d02SWolfram Sang reinit_completion(&i2c_dev->complete);
1031b082ccfSAlan Cox
1041b082ccfSAlan Cox /* Enable I2C transaction */
1051b082ccfSAlan Cox temp = ((pmsg->len) << 20) | HI2C_EDID_READ | HI2C_ENABLE_TRANSACTION;
1061b082ccfSAlan Cox HDMI_WRITE(HDMI_HI2CHCR, temp);
1071b082ccfSAlan Cox HDMI_READ(HDMI_HI2CHCR);
1081b082ccfSAlan Cox
1091b082ccfSAlan Cox while (i2c_dev->status != I2C_TRANSACTION_DONE)
1101b082ccfSAlan Cox wait_for_completion_interruptible_timeout(&i2c_dev->complete,
1111b082ccfSAlan Cox 10 * HZ);
1121b082ccfSAlan Cox
1131b082ccfSAlan Cox return 0;
1141b082ccfSAlan Cox }
1151b082ccfSAlan Cox
xfer_write(struct i2c_adapter * adap,struct i2c_msg * pmsg)1161b082ccfSAlan Cox static int xfer_write(struct i2c_adapter *adap, struct i2c_msg *pmsg)
1171b082ccfSAlan Cox {
1181b082ccfSAlan Cox /*
1191b082ccfSAlan Cox * XXX: i2c write seems isn't useful for EDID probe, don't do anything
1201b082ccfSAlan Cox */
1211b082ccfSAlan Cox return 0;
1221b082ccfSAlan Cox }
1231b082ccfSAlan Cox
oaktrail_hdmi_i2c_access(struct i2c_adapter * adap,struct i2c_msg * pmsg,int num)1241b082ccfSAlan Cox static int oaktrail_hdmi_i2c_access(struct i2c_adapter *adap,
1251b082ccfSAlan Cox struct i2c_msg *pmsg,
1261b082ccfSAlan Cox int num)
1271b082ccfSAlan Cox {
1281b082ccfSAlan Cox struct oaktrail_hdmi_dev *hdmi_dev = i2c_get_adapdata(adap);
1291b082ccfSAlan Cox struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
1301e30296aSKirill A. Shutemov int i;
1311b082ccfSAlan Cox
1321b082ccfSAlan Cox mutex_lock(&i2c_dev->i2c_lock);
1331b082ccfSAlan Cox
1341b082ccfSAlan Cox /* Enable i2c unit */
1351b082ccfSAlan Cox HDMI_WRITE(HDMI_ICRH, 0x00008760);
1361b082ccfSAlan Cox
1371b082ccfSAlan Cox /* Enable irq */
1381b082ccfSAlan Cox hdmi_i2c_irq_enable(hdmi_dev);
1391b082ccfSAlan Cox for (i = 0; i < num; i++) {
1401b082ccfSAlan Cox if (pmsg->len && pmsg->buf) {
1411b082ccfSAlan Cox if (pmsg->flags & I2C_M_RD)
1421e30296aSKirill A. Shutemov xfer_read(adap, pmsg);
1431b082ccfSAlan Cox else
1441e30296aSKirill A. Shutemov xfer_write(adap, pmsg);
1451b082ccfSAlan Cox }
1461b082ccfSAlan Cox pmsg++; /* next message */
1471b082ccfSAlan Cox }
1481b082ccfSAlan Cox
1491b082ccfSAlan Cox /* Disable irq */
1501b082ccfSAlan Cox hdmi_i2c_irq_disable(hdmi_dev);
1511b082ccfSAlan Cox
1521b082ccfSAlan Cox mutex_unlock(&i2c_dev->i2c_lock);
1531b082ccfSAlan Cox
1541b082ccfSAlan Cox return i;
1551b082ccfSAlan Cox }
1561b082ccfSAlan Cox
oaktrail_hdmi_i2c_func(struct i2c_adapter * adapter)1571b082ccfSAlan Cox static u32 oaktrail_hdmi_i2c_func(struct i2c_adapter *adapter)
1581b082ccfSAlan Cox {
1591b082ccfSAlan Cox return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
1601b082ccfSAlan Cox }
1611b082ccfSAlan Cox
1621b082ccfSAlan Cox static const struct i2c_algorithm oaktrail_hdmi_i2c_algorithm = {
1631b082ccfSAlan Cox .master_xfer = oaktrail_hdmi_i2c_access,
1641b082ccfSAlan Cox .functionality = oaktrail_hdmi_i2c_func,
1651b082ccfSAlan Cox };
1661b082ccfSAlan Cox
1671b082ccfSAlan Cox static struct i2c_adapter oaktrail_hdmi_i2c_adapter = {
1681b082ccfSAlan Cox .name = "oaktrail_hdmi_i2c",
1691b082ccfSAlan Cox .nr = 3,
1701b082ccfSAlan Cox .owner = THIS_MODULE,
1711b082ccfSAlan Cox .class = I2C_CLASS_DDC,
1721b082ccfSAlan Cox .algo = &oaktrail_hdmi_i2c_algorithm,
1731b082ccfSAlan Cox };
1741b082ccfSAlan Cox
hdmi_i2c_read(struct oaktrail_hdmi_dev * hdmi_dev)1751b082ccfSAlan Cox static void hdmi_i2c_read(struct oaktrail_hdmi_dev *hdmi_dev)
1761b082ccfSAlan Cox {
1771b082ccfSAlan Cox struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
1781b082ccfSAlan Cox struct i2c_msg *msg = i2c_dev->msg;
1791b082ccfSAlan Cox u8 *buf = msg->buf;
1801b082ccfSAlan Cox u32 temp;
1811b082ccfSAlan Cox int i, offset;
1821b082ccfSAlan Cox
1831b082ccfSAlan Cox offset = i2c_dev->buf_offset;
1841b082ccfSAlan Cox for (i = 0; i < 0x10; i++) {
1851b082ccfSAlan Cox temp = HDMI_READ(HDMI_HI2CRDB0 + (i * 4));
1861b082ccfSAlan Cox memcpy(buf + (offset + i * 4), &temp, 4);
1871b082ccfSAlan Cox }
1881b082ccfSAlan Cox i2c_dev->buf_offset += (0x10 * 4);
1891b082ccfSAlan Cox
1901b082ccfSAlan Cox /* clearing read buffer full intr */
1911b082ccfSAlan Cox temp = HDMI_READ(HDMI_HISR);
1921b082ccfSAlan Cox HDMI_WRITE(HDMI_HISR, temp | HDMI_INTR_I2C_FULL);
1931b082ccfSAlan Cox HDMI_READ(HDMI_HISR);
1941b082ccfSAlan Cox
1951b082ccfSAlan Cox /* continue read transaction */
1961b082ccfSAlan Cox temp = HDMI_READ(HDMI_HI2CHCR);
1971b082ccfSAlan Cox HDMI_WRITE(HDMI_HI2CHCR, temp | HI2C_READ_CONTINUE);
1981b082ccfSAlan Cox HDMI_READ(HDMI_HI2CHCR);
1991b082ccfSAlan Cox
2001b082ccfSAlan Cox i2c_dev->status = I2C_READ_DONE;
2011b082ccfSAlan Cox return;
2021b082ccfSAlan Cox }
2031b082ccfSAlan Cox
hdmi_i2c_transaction_done(struct oaktrail_hdmi_dev * hdmi_dev)2041b082ccfSAlan Cox static void hdmi_i2c_transaction_done(struct oaktrail_hdmi_dev *hdmi_dev)
2051b082ccfSAlan Cox {
2061b082ccfSAlan Cox struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
2071b082ccfSAlan Cox u32 temp;
2081b082ccfSAlan Cox
2091b082ccfSAlan Cox /* clear transaction done intr */
2101b082ccfSAlan Cox temp = HDMI_READ(HDMI_HISR);
2111b082ccfSAlan Cox HDMI_WRITE(HDMI_HISR, temp | HDMI_INTR_I2C_DONE);
2121b082ccfSAlan Cox HDMI_READ(HDMI_HISR);
2131b082ccfSAlan Cox
2141b082ccfSAlan Cox
2151b082ccfSAlan Cox temp = HDMI_READ(HDMI_HI2CHCR);
2161b082ccfSAlan Cox HDMI_WRITE(HDMI_HI2CHCR, temp & ~HI2C_ENABLE_TRANSACTION);
2171b082ccfSAlan Cox HDMI_READ(HDMI_HI2CHCR);
2181b082ccfSAlan Cox
2191b082ccfSAlan Cox i2c_dev->status = I2C_TRANSACTION_DONE;
2201b082ccfSAlan Cox return;
2211b082ccfSAlan Cox }
2221b082ccfSAlan Cox
oaktrail_hdmi_i2c_handler(int this_irq,void * dev)2231b082ccfSAlan Cox static irqreturn_t oaktrail_hdmi_i2c_handler(int this_irq, void *dev)
2241b082ccfSAlan Cox {
2251b082ccfSAlan Cox struct oaktrail_hdmi_dev *hdmi_dev = dev;
2261b082ccfSAlan Cox struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
2271b082ccfSAlan Cox u32 stat;
2281b082ccfSAlan Cox
2291b082ccfSAlan Cox stat = HDMI_READ(HDMI_HISR);
2301b082ccfSAlan Cox
2311b082ccfSAlan Cox if (stat & HDMI_INTR_HPD) {
2321b082ccfSAlan Cox HDMI_WRITE(HDMI_HISR, stat | HDMI_INTR_HPD);
2331b082ccfSAlan Cox HDMI_READ(HDMI_HISR);
2341b082ccfSAlan Cox }
2351b082ccfSAlan Cox
2361b082ccfSAlan Cox if (stat & HDMI_INTR_I2C_FULL)
2371b082ccfSAlan Cox hdmi_i2c_read(hdmi_dev);
2381b082ccfSAlan Cox
2391b082ccfSAlan Cox if (stat & HDMI_INTR_I2C_DONE)
2401b082ccfSAlan Cox hdmi_i2c_transaction_done(hdmi_dev);
2411b082ccfSAlan Cox
2421b082ccfSAlan Cox complete(&i2c_dev->complete);
2431b082ccfSAlan Cox
2441b082ccfSAlan Cox return IRQ_HANDLED;
2451b082ccfSAlan Cox }
2461b082ccfSAlan Cox
2471b082ccfSAlan Cox /*
2481b082ccfSAlan Cox * choose alternate function 2 of GPIO pin 52, 53,
2491b082ccfSAlan Cox * which is used by HDMI I2C logic
2501b082ccfSAlan Cox */
oaktrail_hdmi_i2c_gpio_fix(void)2511b082ccfSAlan Cox static void oaktrail_hdmi_i2c_gpio_fix(void)
2521b082ccfSAlan Cox {
2531f17fcd0SKirill A. Shutemov void __iomem *base;
2541b082ccfSAlan Cox unsigned int gpio_base = 0xff12c000;
2551b082ccfSAlan Cox int gpio_len = 0x1000;
2561b082ccfSAlan Cox u32 temp;
2571b082ccfSAlan Cox
2581b082ccfSAlan Cox base = ioremap((resource_size_t)gpio_base, gpio_len);
2591b082ccfSAlan Cox if (base == NULL) {
2601b082ccfSAlan Cox DRM_ERROR("gpio ioremap fail\n");
2611b082ccfSAlan Cox return;
2621b082ccfSAlan Cox }
2631b082ccfSAlan Cox
2641b082ccfSAlan Cox temp = readl(base + 0x44);
2651b082ccfSAlan Cox DRM_DEBUG_DRIVER("old gpio val %x\n", temp);
2661b082ccfSAlan Cox writel((temp | 0x00000a00), (base + 0x44));
2671b082ccfSAlan Cox temp = readl(base + 0x44);
2681b082ccfSAlan Cox DRM_DEBUG_DRIVER("new gpio val %x\n", temp);
2691b082ccfSAlan Cox
2701b082ccfSAlan Cox iounmap(base);
2711b082ccfSAlan Cox }
2721b082ccfSAlan Cox
oaktrail_hdmi_i2c_init(struct pci_dev * dev)2731b082ccfSAlan Cox int oaktrail_hdmi_i2c_init(struct pci_dev *dev)
2741b082ccfSAlan Cox {
2751b082ccfSAlan Cox struct oaktrail_hdmi_dev *hdmi_dev;
2761b082ccfSAlan Cox struct hdmi_i2c_dev *i2c_dev;
2771b082ccfSAlan Cox int ret;
2781b082ccfSAlan Cox
2791b082ccfSAlan Cox hdmi_dev = pci_get_drvdata(dev);
2801b082ccfSAlan Cox
2811b082ccfSAlan Cox i2c_dev = kzalloc(sizeof(struct hdmi_i2c_dev), GFP_KERNEL);
282*15ccc39bSDan Carpenter if (!i2c_dev)
283*15ccc39bSDan Carpenter return -ENOMEM;
2841b082ccfSAlan Cox
2851b082ccfSAlan Cox i2c_dev->adap = &oaktrail_hdmi_i2c_adapter;
2861b082ccfSAlan Cox i2c_dev->status = I2C_STAT_INIT;
2871b082ccfSAlan Cox init_completion(&i2c_dev->complete);
2881b082ccfSAlan Cox mutex_init(&i2c_dev->i2c_lock);
2891b082ccfSAlan Cox i2c_set_adapdata(&oaktrail_hdmi_i2c_adapter, hdmi_dev);
2901b082ccfSAlan Cox hdmi_dev->i2c_dev = i2c_dev;
2911b082ccfSAlan Cox
2921b082ccfSAlan Cox /* Enable HDMI I2C function on gpio */
2931b082ccfSAlan Cox oaktrail_hdmi_i2c_gpio_fix();
2941b082ccfSAlan Cox
2951b082ccfSAlan Cox /* request irq */
2961b082ccfSAlan Cox ret = request_irq(dev->irq, oaktrail_hdmi_i2c_handler, IRQF_SHARED,
2971b082ccfSAlan Cox oaktrail_hdmi_i2c_adapter.name, hdmi_dev);
2981b082ccfSAlan Cox if (ret) {
2991b082ccfSAlan Cox DRM_ERROR("Failed to request IRQ for I2C controller\n");
300*15ccc39bSDan Carpenter goto free_dev;
3011b082ccfSAlan Cox }
3021b082ccfSAlan Cox
3031b082ccfSAlan Cox /* Adapter registration */
3041b082ccfSAlan Cox ret = i2c_add_numbered_adapter(&oaktrail_hdmi_i2c_adapter);
305*15ccc39bSDan Carpenter if (ret) {
306*15ccc39bSDan Carpenter DRM_ERROR("Failed to add I2C adapter\n");
307*15ccc39bSDan Carpenter goto free_irq;
308*15ccc39bSDan Carpenter }
3091b082ccfSAlan Cox
310*15ccc39bSDan Carpenter return 0;
311*15ccc39bSDan Carpenter
312*15ccc39bSDan Carpenter free_irq:
313*15ccc39bSDan Carpenter free_irq(dev->irq, hdmi_dev);
314*15ccc39bSDan Carpenter free_dev:
3151b082ccfSAlan Cox kfree(i2c_dev);
316*15ccc39bSDan Carpenter
3171b082ccfSAlan Cox return ret;
3181b082ccfSAlan Cox }
3191b082ccfSAlan Cox
oaktrail_hdmi_i2c_exit(struct pci_dev * dev)3201b082ccfSAlan Cox void oaktrail_hdmi_i2c_exit(struct pci_dev *dev)
3211b082ccfSAlan Cox {
3221b082ccfSAlan Cox struct oaktrail_hdmi_dev *hdmi_dev;
3231b082ccfSAlan Cox struct hdmi_i2c_dev *i2c_dev;
3241b082ccfSAlan Cox
3251b082ccfSAlan Cox hdmi_dev = pci_get_drvdata(dev);
326bf51a8c5SLars-Peter Clausen i2c_del_adapter(&oaktrail_hdmi_i2c_adapter);
3271b082ccfSAlan Cox
3281b082ccfSAlan Cox i2c_dev = hdmi_dev->i2c_dev;
3291b082ccfSAlan Cox kfree(i2c_dev);
3301b082ccfSAlan Cox free_irq(dev->irq, hdmi_dev);
3311b082ccfSAlan Cox }
332