Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "radeonfb.h"
0003 
0004 #include <linux/module.h>
0005 #include <linux/kernel.h>
0006 #include <linux/delay.h>
0007 #include <linux/fb.h>
0008 
0009 
0010 #include <linux/i2c.h>
0011 #include <linux/i2c-algo-bit.h>
0012 
0013 #include <asm/io.h>
0014 
0015 #include <video/radeon.h>
0016 #include "../edid.h"
0017 
0018 static void radeon_gpio_setscl(void* data, int state)
0019 {
0020     struct radeon_i2c_chan  *chan = data;
0021     struct radeonfb_info    *rinfo = chan->rinfo;
0022     u32         val;
0023     
0024     val = INREG(chan->ddc_reg) & ~(VGA_DDC_CLK_OUT_EN);
0025     if (!state)
0026         val |= VGA_DDC_CLK_OUT_EN;
0027 
0028     OUTREG(chan->ddc_reg, val);
0029     (void)INREG(chan->ddc_reg);
0030 }
0031 
0032 static void radeon_gpio_setsda(void* data, int state)
0033 {
0034     struct radeon_i2c_chan  *chan = data;
0035     struct radeonfb_info    *rinfo = chan->rinfo;
0036     u32         val;
0037     
0038     val = INREG(chan->ddc_reg) & ~(VGA_DDC_DATA_OUT_EN);
0039     if (!state)
0040         val |= VGA_DDC_DATA_OUT_EN;
0041 
0042     OUTREG(chan->ddc_reg, val);
0043     (void)INREG(chan->ddc_reg);
0044 }
0045 
0046 static int radeon_gpio_getscl(void* data)
0047 {
0048     struct radeon_i2c_chan  *chan = data;
0049     struct radeonfb_info    *rinfo = chan->rinfo;
0050     u32         val;
0051     
0052     val = INREG(chan->ddc_reg);
0053 
0054     return (val & VGA_DDC_CLK_INPUT) ? 1 : 0;
0055 }
0056 
0057 static int radeon_gpio_getsda(void* data)
0058 {
0059     struct radeon_i2c_chan  *chan = data;
0060     struct radeonfb_info    *rinfo = chan->rinfo;
0061     u32         val;
0062     
0063     val = INREG(chan->ddc_reg);
0064 
0065     return (val & VGA_DDC_DATA_INPUT) ? 1 : 0;
0066 }
0067 
0068 static int radeon_setup_i2c_bus(struct radeon_i2c_chan *chan, const char *name)
0069 {
0070     int rc;
0071 
0072     snprintf(chan->adapter.name, sizeof(chan->adapter.name),
0073          "radeonfb %s", name);
0074     chan->adapter.owner     = THIS_MODULE;
0075     chan->adapter.algo_data     = &chan->algo;
0076     chan->adapter.dev.parent    = &chan->rinfo->pdev->dev;
0077     chan->algo.setsda       = radeon_gpio_setsda;
0078     chan->algo.setscl       = radeon_gpio_setscl;
0079     chan->algo.getsda       = radeon_gpio_getsda;
0080     chan->algo.getscl       = radeon_gpio_getscl;
0081     chan->algo.udelay       = 10;
0082     chan->algo.timeout      = 20;
0083     chan->algo.data         = chan; 
0084     
0085     i2c_set_adapdata(&chan->adapter, chan);
0086     
0087     /* Raise SCL and SDA */
0088     radeon_gpio_setsda(chan, 1);
0089     radeon_gpio_setscl(chan, 1);
0090     udelay(20);
0091 
0092     rc = i2c_bit_add_bus(&chan->adapter);
0093     if (rc == 0)
0094         dev_dbg(&chan->rinfo->pdev->dev, "I2C bus %s registered.\n", name);
0095     else
0096         dev_warn(&chan->rinfo->pdev->dev, "Failed to register I2C bus %s.\n", name);
0097     return rc;
0098 }
0099 
0100 void radeon_create_i2c_busses(struct radeonfb_info *rinfo)
0101 {
0102     rinfo->i2c[0].rinfo = rinfo;
0103     rinfo->i2c[0].ddc_reg   = GPIO_MONID;
0104 #ifndef CONFIG_PPC
0105     rinfo->i2c[0].adapter.class = I2C_CLASS_HWMON;
0106 #endif
0107     radeon_setup_i2c_bus(&rinfo->i2c[0], "monid");
0108 
0109     rinfo->i2c[1].rinfo = rinfo;
0110     rinfo->i2c[1].ddc_reg   = GPIO_DVI_DDC;
0111     radeon_setup_i2c_bus(&rinfo->i2c[1], "dvi");
0112 
0113     rinfo->i2c[2].rinfo = rinfo;
0114     rinfo->i2c[2].ddc_reg   = GPIO_VGA_DDC;
0115     radeon_setup_i2c_bus(&rinfo->i2c[2], "vga");
0116 
0117     rinfo->i2c[3].rinfo = rinfo;
0118     rinfo->i2c[3].ddc_reg   = GPIO_CRT2_DDC;
0119     radeon_setup_i2c_bus(&rinfo->i2c[3], "crt2");
0120 }
0121 
0122 void radeon_delete_i2c_busses(struct radeonfb_info *rinfo)
0123 {
0124     if (rinfo->i2c[0].rinfo)
0125         i2c_del_adapter(&rinfo->i2c[0].adapter);
0126     rinfo->i2c[0].rinfo = NULL;
0127 
0128     if (rinfo->i2c[1].rinfo)
0129         i2c_del_adapter(&rinfo->i2c[1].adapter);
0130     rinfo->i2c[1].rinfo = NULL;
0131 
0132     if (rinfo->i2c[2].rinfo)
0133         i2c_del_adapter(&rinfo->i2c[2].adapter);
0134     rinfo->i2c[2].rinfo = NULL;
0135 
0136     if (rinfo->i2c[3].rinfo)
0137         i2c_del_adapter(&rinfo->i2c[3].adapter);
0138     rinfo->i2c[3].rinfo = NULL;
0139 }
0140 
0141 int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn,
0142                    u8 **out_edid)
0143 {
0144     u8 *edid;
0145 
0146     edid = fb_ddc_read(&rinfo->i2c[conn-1].adapter);
0147 
0148     if (out_edid)
0149         *out_edid = edid;
0150     if (!edid) {
0151         pr_debug("radeonfb: I2C (port %d) ... not found\n", conn);
0152         return MT_NONE;
0153     }
0154     if (edid[0x14] & 0x80) {
0155         /* Fix detection using BIOS tables */
0156         if (rinfo->is_mobility /*&& conn == ddc_dvi*/ &&
0157             (INREG(LVDS_GEN_CNTL) & LVDS_ON)) {
0158             pr_debug("radeonfb: I2C (port %d) ... found LVDS panel\n", conn);
0159             return MT_LCD;
0160         } else {
0161             pr_debug("radeonfb: I2C (port %d) ... found TMDS panel\n", conn);
0162             return MT_DFP;
0163         }
0164     }
0165     pr_debug("radeonfb: I2C (port %d) ... found CRT display\n", conn);
0166     return MT_CRT;
0167 }
0168