ATI i2c bus driver
Hi all,
Any reason why we don't have a i2c bus driver for ATI video adapters? Looks like all other brands are supported (Voodoo and S3 in lm_sensors2 CVS, Matrox in kernel and Nvidia by the rivatv project) but not ATI. I do own such an adapter (actually my laptop's video chip) and plan to buy another one for my desktop system soon. I'd enjoy playing with their I2C busses if they do have one.
Where should I start if I want to write a new bus driver for ATI Radeon chips?
Thanks.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 3 lines
Katos, Gatos, ATI.2. I think all are hosted at sourceforge, you should find XFree86 I2C/DDC interface code there. Many ATI datasheets were not publicly available the last time I looked.
I'll give a try to the gatos-devel mailing-list.
There must be some code in XFree as well, but that's so complex that I did not understand anything (probably because it's mixed with so many other parts). I'd hope that data sheets, if they exist and are somewhat available, would be more helpful.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
OK, I got some code from the Gatos project. I took i2c-savage4 and edited it in accordance with what I found there, but I can't get it to work. If anyone could take a look and tell me where I'm wrong...
Registers are here:
http://cvs.sourceforge.net/viewcvs.py/gatos/ati.2/radeon_reg.h?rev=1.14&view=auto
I use these:
#define RADEON_GPIO_DVI_DDC 0x0064 #define RADEON_GPIO_VGA_DDC 0x0060 # define RADEON_GPIO_Y_0 (1 << 8) # define RADEON_GPIO_Y_1 (1 << 9) # define RADEON_GPIO_EN_0 (1 << 16) # define RADEON_GPIO_EN_1 (1 << 17)
Also tried these but didn't help:
#define RADEON_GPIO_MONID 0x0068
#define RADEON_GPIO_CRT2_DDC 0x006c
Code is here:
http://cvs.sourceforge.net/viewcvs.py/gatos/ati.2/radeon_driver.c?rev=1.17&view=auto
Functions I took my code from are RADEONI2CGetBits, RADEONI2CPutBits and RADEONI2cInit. One thing I noticed is that set{scl,sda} seem to be inverted.
Attached is my driver. It doesn't work. Whatever base register I try (DVI, VGA, CRT2...), both SDA and SCL lines are stuck to low (so says i2c-algo-bit with bit_test=1). So I'm obviously missing something, maybe because I never worked on this kind of hardware before. Any help appreciated.
Thanks.
Signature · 218 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
-------------- next part --------------
/*
i2c-radeon.c - Part of lm_sensors, Linux kernel modules for hardware
monitoring
Copyright (C) 1998-2004 The LM Sensors Team
Based on i2c-savage4.c.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>
#include <linux/init.h>
#include <asm/io.h>
#include <asm/param.h> /* for HZ */
#include "version.h"
/* Radeon defines */
#define RADEON_GPIO_VGA_DDC 0x0060
#define RADEON_GPIO_DVI_DDC 0x0064
/* bit locations in the registers */
#define I2C_SDA_IN (1 << 8)
#define I2C_SCL_IN (1 << 9)
#define I2C_SCL_OUT (1 << 17) /* inverted */
#define I2C_SDA_OUT (1 << 16) /* inverted */
/* delays */
#define CYCLE_DELAY 10
#define TIMEOUT (HZ / 2)
static void config_radeon(struct pci_dev *dev);
static unsigned long ioaddr;
static void radeon_dvi_setscl(void *data, int val)
{
unsigned int r;
r = readl(ioaddr + RADEON_GPIO_DVI_DDC);
if(val)
r &= ~I2C_SCL_OUT;
else
r |= I2C_SCL_OUT;
writel(r, ioaddr + RADEON_GPIO_DVI_DDC);
readl(ioaddr + RADEON_GPIO_DVI_DDC); /* flush posted write */
}
static void radeon_dvi_setsda(void *data, int val)
{
unsigned int r;
r = readl(ioaddr + RADEON_GPIO_DVI_DDC);
if(val)
r &= ~I2C_SDA_OUT;
else
r |= I2C_SDA_OUT;
writel(r, ioaddr + RADEON_GPIO_DVI_DDC);
readl(ioaddr + RADEON_GPIO_DVI_DDC); /* flush posted write */
}
static int radeon_dvi_getscl(void *data)
{
return (0 != (readl(ioaddr + RADEON_GPIO_DVI_DDC) & I2C_SCL_IN));
}
static int radeon_dvi_getsda(void *data)
{
return (0 != (readl(ioaddr + RADEON_GPIO_DVI_DDC) & I2C_SDA_IN));
}
static void radeon_vga_setscl(void *data, int val)
{
unsigned int r;
r = readl(ioaddr + RADEON_GPIO_VGA_DDC);
if(val)
r &= ~I2C_SCL_OUT;
else
r |= I2C_SCL_OUT;
writel(r, ioaddr + RADEON_GPIO_VGA_DDC);
readl(ioaddr + RADEON_GPIO_VGA_DDC); /* flush posted write */
}
static void radeon_vga_setsda(void *data, int val)
{
unsigned int r;
r = readl(ioaddr + RADEON_GPIO_VGA_DDC);
if(val)
r &= ~I2C_SDA_OUT;
else
r |= I2C_SDA_OUT;
writel(r, ioaddr + RADEON_GPIO_VGA_DDC);
readl(ioaddr + RADEON_GPIO_VGA_DDC); /* flush posted write */
}
static int radeon_vga_getscl(void *data)
{
return (0 != (readl(ioaddr + RADEON_GPIO_VGA_DDC) & I2C_SCL_IN));
}
static int radeon_vga_getsda(void *data)
{
return (0 != (readl(ioaddr + RADEON_GPIO_VGA_DDC) & I2C_SDA_IN));
}
/* Configures the chip */
void config_radeon(struct pci_dev *dev)
{
unsigned int cadr;
/* map memory */
cadr = dev->resource[0].start;
cadr &= PCI_BASE_ADDRESS_MEM_MASK;
ioaddr = (unsigned long)ioremap_nocache(cadr, 0x0001000);
if(ioaddr) {
// writel(0x8160, ioaddr + RADEON_GPIO_DVI_DDC2);
// writel(0x00000020, ioaddr + RADEON_GPIO_DVI_DDC);
radeon_dvi_setscl(NULL, 1);
radeon_dvi_setsda(NULL, 1);
radeon_vga_setscl(NULL, 1);
radeon_vga_setsda(NULL, 1);
printk(KERN_INFO "i2c-radeon.o: Using Radeon at 0x%lx\n", ioaddr);
}
}
static struct i2c_algo_bit_data radeon_dvi_bit_data = {
.setsda = radeon_dvi_setsda,
.setscl = radeon_dvi_setscl,
.getsda = radeon_dvi_getsda,
.getscl = radeon_dvi_getscl,
.udelay = CYCLE_DELAY,
.mdelay = CYCLE_DELAY,
.timeout = TIMEOUT
};
static struct i2c_algo_bit_data radeon_vga_bit_data = {
.setsda = radeon_vga_setsda,
.setscl = radeon_vga_setscl,
.getsda = radeon_vga_getsda,
.getscl = radeon_vga_getscl,
.udelay = CYCLE_DELAY,
.mdelay = CYCLE_DELAY,
.timeout = TIMEOUT
};
static struct i2c_adapter radeon_i2c_adapter = {
.owner = THIS_MODULE,
.name = "ATI Radeon",
.id = I2C_HW_B_SAVG, /* FIXME */
.algo_data = &radeon_vga_bit_data,
};
static struct pci_device_id radeon_ids[] __devinitdata = {
{
.vendor = 0x1002,
.device = 0x4C59,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
},
{ 0 }
};
static int __devinit radeon_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
config_radeon(dev);
return i2c_bit_add_bus(&radeon_i2c_adapter);
}
static void __devexit radeon_remove(struct pci_dev *dev)
{
i2c_bit_del_bus(&radeon_i2c_adapter);
}
static int __init i2c_radeon_init(void)
{
struct pci_dev *dev;
const struct pci_device_id *id;
printk(KERN_DEBUG "i2c-radeon.o init\n");
pci_for_each_dev(dev) {
id = pci_match_device(radeon_ids, dev);
if(id && radeon_probe(dev, id) >= 0)
return 0;
}
return -ENODEV;
}
static void __exit i2c_radeon_exit(void)
{
radeon_remove(NULL);
iounmap((void *)ioaddr);
}
MODULE_AUTHOR("Jean Delvare <[email protected]>");
MODULE_DESCRIPTION("ATI Radeon I2C bus driver");
MODULE_LICENSE("GPL");
module_init(i2c_radeon_init);
module_exit(i2c_radeon_exit);start by finding a datasheet.
after that it's easy :)
Quoted 14 lines · Jean Delvare
Hi all,
Any reason why we don't have a i2c bus driver for ATI video adapters? Looks like all other brands are supported (Voodoo and S3 in
lm_sensors2CVS, Matrox in kernel and Nvidia by the rivatv project) but not ATI. I do own such an adapter (actually my laptop's video chip) and plan to buy another one for my desktop system soon. I'd enjoy playing with their I2C busses if they do have one.Where should I start if I want to write a new bus driver for ATI Radeon chips?
Thanks.
Quoted 2 lines · Mark Studebaker
start by finding a datasheet.
after that it's easy :)
Katos, Gatos, ATI.2. I think all are hosted at sourceforge, you should find XFree86 I2C/DDC interface code there. Many ATI datasheets were not publicly available the last time I looked.
Signature · 1 line
Ky?sti M?lkki <[email protected]>
Quoted 3 lines · Jean Delvare
OK, I got some code from the Gatos project. I took i2c-savage4 and edited it in accordance with what I found there, but I can't get it to work. If anyone could take a look and tell me where I'm wrong...
The GPIO pins could be bidirectional, but one way at a time. Meaning, the read value _GPIO_Y_0 is not valid when _GPIO_EN_0 is set.
Also, I did not find _GPIO_A_0 (nor _1) initialised to 0 in your code. I believe this is the value ( 0 = pull low, 1 = hi-Z ) when _GPIO_EN_0 is set. Makes sense with the inverted setscl&sda.
Did you try without bit_test? You might need some <1 us delay between read and write to see the change, the bus has some capacitance even without monitor cable.
Signature · 1 line
Ky?sti M?lkki <[email protected]>
Quoted 2 lines
The GPIO pins could be bidirectional, but one way at a time. Meaning, the read value
_GPIO_Y_0is not valid when_GPIO_EN_0is set.
I did not see anything similar in Gatos' code.
Quoted 3 lines
Also, I did not find
_GPIO_A_0(nor _1) initialised to 0 in your code. I believe this is the value ( 0 = pull low, 1 = hi-Z ) when_GPIO_EN_0is set. Makes sense with the inverted setscl&sda.
I did that, it did not help. I believe that both bits were 0 anyway -- the whole register value is 0.
Quoted 3 lines
Did you try without
bit_test? You might need some <1 us delay between read and write to see the change, the bus has some capacitance even without monitor cable.
Yes, I tried without bit_test=1. I2cdetect hangs using 100% CPU, which makes me think my code is somehow broken, although I don't see where. I've copied what I read from the gatos code, using i2c-savage4 as a base, everything seems very simple, but it doesn't work. Would you have any concrete suggestion of something I could try? Else I think I'll just give up.
Thanks.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 4 lines · Jean Delvare
Quoted 2 lines
The GPIO pins could be bidirectional, but one way at a time. Meaning, the read value
_GPIO_Y_0is not valid when_GPIO_EN_0is set.I did not see anything similar in Gatos' code.
Change getscl/sda to test the _GPIO_EN_x bit. If set (pull low), return zero, else if unset (hi-Z) read and return _GPIO_Y_x.
I wondered why there was I2C_ALGO_ATI in i2c-id.h : Katos. It claims to have working i2c kernel code, and does not use i2c-algo-bit. The code is from -99, so don't expect it to compile out-of-the box either.
http://www.core.binghamton.edu/~insomnia/gatos/katos/src/katos-0.5.tar.gz
Quoted 10 lines
Quoted 3 lines
Did you try without
bit_test? You might need some <1 us delay between read and write to see the change, the bus has some capacitance even without monitor cable.Yes, I tried without bit_test=1. I2cdetect hangs using 100% CPU, which makes me think my code is somehow broken, although I don't see where. I've copied what I read from the gatos code, using i2c-savage4 as a base, everything seems very simple, but it doesn't work. Would you have any concrete suggestion of something I could try? Else I think I'll just give up.
I guess i2cdetect sleeps in sclhi() twice for each address to scan. Run with strace, does it return from syscalls or is it simply slow? Also have i2c-algo-bit debugging enabled, plz.
At some point I got fed up tracing the failure paths and return codes in i2c-algo-bit. You could have better debugging experience using the i2c-algo-biths, just uncomment one COMPATIBILITY #define in the source.
Signature · 1 line
Ky?sti M?lkki <[email protected]>
Quoted 2 lines
Change getscl/sda to test the
_GPIO_EN_xbit. If set (pull low), return zero, else if unset (hi-Z) read and return_GPIO_Y_x.
Didn't work.
But it happens that Benjamin Herrenschmidt has a working i2c bus driver for Radeon boards in Linux 2.6, as part of its new radeonfb driver. The i2c part is much similar to what I had tried, but there is much more initialization stuff in the video part. This is probably what was missing to me.
There is no point in writing a separate i2c driver now, so I'm canceling my driver completely.
Thanks for the help anyway.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/