Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/video/riva/fbdev-i2c.c - nVidia i2c |
| 3 | * |
| 4 | * Maintained by Ani Joshi <ajoshi@shell.unixbox.com> |
| 5 | * |
| 6 | * Copyright 2004 Antonino A. Daplas <adaplas @pol.net> |
| 7 | * |
| 8 | * Based on radeonfb-i2c.c |
| 9 | * |
| 10 | * This file is subject to the terms and conditions of the GNU General Public |
| 11 | * License. See the file COPYING in the main directory of this archive |
| 12 | * for more details. |
| 13 | */ |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/delay.h> |
| 18 | #include <linux/pci.h> |
| 19 | #include <linux/fb.h> |
| 20 | #include <linux/jiffies.h> |
| 21 | |
| 22 | #include <asm/io.h> |
| 23 | |
| 24 | #include "rivafb.h" |
| 25 | #include "../edid.h" |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | static void riva_gpio_setscl(void* data, int state) |
| 28 | { |
Antonino A. Daplas | f4a4183 | 2006-01-09 20:53:04 -0800 | [diff] [blame] | 29 | struct riva_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | struct riva_par *par = chan->par; |
| 31 | u32 val; |
| 32 | |
| 33 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1); |
| 34 | val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0; |
| 35 | |
| 36 | if (state) |
| 37 | val |= 0x20; |
| 38 | else |
| 39 | val &= ~0x20; |
| 40 | |
| 41 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1); |
| 42 | VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1); |
| 43 | } |
| 44 | |
| 45 | static void riva_gpio_setsda(void* data, int state) |
| 46 | { |
Antonino A. Daplas | f4a4183 | 2006-01-09 20:53:04 -0800 | [diff] [blame] | 47 | struct riva_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct riva_par *par = chan->par; |
| 49 | u32 val; |
| 50 | |
| 51 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1); |
| 52 | val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0; |
| 53 | |
| 54 | if (state) |
| 55 | val |= 0x10; |
| 56 | else |
| 57 | val &= ~0x10; |
| 58 | |
| 59 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1); |
| 60 | VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1); |
| 61 | } |
| 62 | |
| 63 | static int riva_gpio_getscl(void* data) |
| 64 | { |
Antonino A. Daplas | f4a4183 | 2006-01-09 20:53:04 -0800 | [diff] [blame] | 65 | struct riva_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | struct riva_par *par = chan->par; |
| 67 | u32 val = 0; |
| 68 | |
| 69 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base); |
| 70 | if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x04) |
| 71 | val = 1; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | return val; |
| 74 | } |
| 75 | |
| 76 | static int riva_gpio_getsda(void* data) |
| 77 | { |
Antonino A. Daplas | f4a4183 | 2006-01-09 20:53:04 -0800 | [diff] [blame] | 78 | struct riva_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct riva_par *par = chan->par; |
| 80 | u32 val = 0; |
| 81 | |
| 82 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base); |
| 83 | if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08) |
| 84 | val = 1; |
| 85 | |
| 86 | return val; |
| 87 | } |
| 88 | |
Jean Delvare | a65ff76 | 2007-05-08 00:38:20 -0700 | [diff] [blame] | 89 | static int __devinit riva_setup_i2c_bus(struct riva_i2c_chan *chan, |
| 90 | const char *name, |
| 91 | unsigned int i2c_class) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
| 93 | int rc; |
| 94 | |
| 95 | strcpy(chan->adapter.name, name); |
| 96 | chan->adapter.owner = THIS_MODULE; |
Jean Delvare | 1e73db2 | 2007-05-08 00:38:15 -0700 | [diff] [blame] | 97 | chan->adapter.class = i2c_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | chan->adapter.algo_data = &chan->algo; |
| 99 | chan->adapter.dev.parent = &chan->par->pdev->dev; |
| 100 | chan->algo.setsda = riva_gpio_setsda; |
| 101 | chan->algo.setscl = riva_gpio_setscl; |
| 102 | chan->algo.getsda = riva_gpio_getsda; |
| 103 | chan->algo.getscl = riva_gpio_getscl; |
| 104 | chan->algo.udelay = 40; |
| 105 | chan->algo.timeout = msecs_to_jiffies(2); |
| 106 | chan->algo.data = chan; |
| 107 | |
| 108 | i2c_set_adapdata(&chan->adapter, chan); |
| 109 | |
| 110 | /* Raise SCL and SDA */ |
| 111 | riva_gpio_setsda(chan, 1); |
| 112 | riva_gpio_setscl(chan, 1); |
| 113 | udelay(20); |
| 114 | |
| 115 | rc = i2c_bit_add_bus(&chan->adapter); |
| 116 | if (rc == 0) |
| 117 | dev_dbg(&chan->par->pdev->dev, "I2C bus %s registered.\n", name); |
| 118 | else { |
| 119 | dev_warn(&chan->par->pdev->dev, |
| 120 | "Failed to register I2C bus %s.\n", name); |
| 121 | chan->par = NULL; |
| 122 | } |
| 123 | |
| 124 | return rc; |
| 125 | } |
| 126 | |
Jean Delvare | a65ff76 | 2007-05-08 00:38:20 -0700 | [diff] [blame] | 127 | void __devinit riva_create_i2c_busses(struct riva_par *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | par->chan[0].par = par; |
| 130 | par->chan[1].par = par; |
| 131 | par->chan[2].par = par; |
| 132 | |
Antonino A. Daplas | 0404384 | 2007-05-08 00:38:22 -0700 | [diff] [blame] | 133 | par->chan[0].ddc_base = 0x36; |
| 134 | par->chan[1].ddc_base = 0x3e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | par->chan[2].ddc_base = 0x50; |
Antonino A. Daplas | 0404384 | 2007-05-08 00:38:22 -0700 | [diff] [blame] | 136 | riva_setup_i2c_bus(&par->chan[0], "BUS1", I2C_CLASS_HWMON); |
| 137 | riva_setup_i2c_bus(&par->chan[1], "BUS2", 0); |
Jean Delvare | 1e73db2 | 2007-05-08 00:38:15 -0700 | [diff] [blame] | 138 | riva_setup_i2c_bus(&par->chan[2], "BUS3", 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void riva_delete_i2c_busses(struct riva_par *par) |
| 142 | { |
Jean Delvare | a65ff76 | 2007-05-08 00:38:20 -0700 | [diff] [blame] | 143 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Jean Delvare | a65ff76 | 2007-05-08 00:38:20 -0700 | [diff] [blame] | 145 | for (i = 0; i < 3; i++) { |
| 146 | if (!par->chan[i].par) |
| 147 | continue; |
| 148 | i2c_del_adapter(&par->chan[i].adapter); |
| 149 | par->chan[i].par = NULL; |
| 150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Jean Delvare | a65ff76 | 2007-05-08 00:38:20 -0700 | [diff] [blame] | 153 | int __devinit riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | u8 *edid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Jean Delvare | a65ff76 | 2007-05-08 00:38:20 -0700 | [diff] [blame] | 157 | if (par->chan[conn].par) |
| 158 | edid = fb_ddc_read(&par->chan[conn].adapter); |
Antonino A. Daplas | bf5df0a | 2006-10-03 01:14:44 -0700 | [diff] [blame] | 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | if (out_edid) |
| 161 | *out_edid = edid; |
| 162 | if (!edid) |
| 163 | return 1; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |