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> |
| 17 | #include <linux/sched.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/pci.h> |
| 20 | #include <linux/fb.h> |
| 21 | #include <linux/jiffies.h> |
| 22 | |
| 23 | #include <asm/io.h> |
| 24 | |
| 25 | #include "rivafb.h" |
| 26 | #include "../edid.h" |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static void riva_gpio_setscl(void* data, int state) |
| 29 | { |
Antonino A. Daplas | f4a4183 | 2006-01-09 20:53:04 -0800 | [diff] [blame] | 30 | struct riva_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | struct riva_par *par = chan->par; |
| 32 | u32 val; |
| 33 | |
| 34 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1); |
| 35 | val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0; |
| 36 | |
| 37 | if (state) |
| 38 | val |= 0x20; |
| 39 | else |
| 40 | val &= ~0x20; |
| 41 | |
| 42 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1); |
| 43 | VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1); |
| 44 | } |
| 45 | |
| 46 | static void riva_gpio_setsda(void* data, int state) |
| 47 | { |
Antonino A. Daplas | f4a4183 | 2006-01-09 20:53:04 -0800 | [diff] [blame] | 48 | struct riva_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | struct riva_par *par = chan->par; |
| 50 | u32 val; |
| 51 | |
| 52 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1); |
| 53 | val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0; |
| 54 | |
| 55 | if (state) |
| 56 | val |= 0x10; |
| 57 | else |
| 58 | val &= ~0x10; |
| 59 | |
| 60 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1); |
| 61 | VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1); |
| 62 | } |
| 63 | |
| 64 | static int riva_gpio_getscl(void* data) |
| 65 | { |
Antonino A. Daplas | f4a4183 | 2006-01-09 20:53:04 -0800 | [diff] [blame] | 66 | struct riva_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | struct riva_par *par = chan->par; |
| 68 | u32 val = 0; |
| 69 | |
| 70 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base); |
| 71 | if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x04) |
| 72 | val = 1; |
| 73 | |
| 74 | val = VGA_RD08(par->riva.PCIO, 0x3d5); |
| 75 | |
| 76 | return val; |
| 77 | } |
| 78 | |
| 79 | static int riva_gpio_getsda(void* data) |
| 80 | { |
Antonino A. Daplas | f4a4183 | 2006-01-09 20:53:04 -0800 | [diff] [blame] | 81 | struct riva_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | struct riva_par *par = chan->par; |
| 83 | u32 val = 0; |
| 84 | |
| 85 | VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base); |
| 86 | if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08) |
| 87 | val = 1; |
| 88 | |
| 89 | return val; |
| 90 | } |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | static int riva_setup_i2c_bus(struct riva_i2c_chan *chan, const char *name) |
| 93 | { |
| 94 | int rc; |
| 95 | |
| 96 | strcpy(chan->adapter.name, name); |
| 97 | chan->adapter.owner = THIS_MODULE; |
Jean Delvare | 1684a984 | 2005-08-11 23:51:10 +0200 | [diff] [blame] | 98 | chan->adapter.id = I2C_HW_B_RIVA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | chan->adapter.algo_data = &chan->algo; |
| 100 | chan->adapter.dev.parent = &chan->par->pdev->dev; |
| 101 | chan->algo.setsda = riva_gpio_setsda; |
| 102 | chan->algo.setscl = riva_gpio_setscl; |
| 103 | chan->algo.getsda = riva_gpio_getsda; |
| 104 | chan->algo.getscl = riva_gpio_getscl; |
| 105 | chan->algo.udelay = 40; |
| 106 | chan->algo.timeout = msecs_to_jiffies(2); |
| 107 | chan->algo.data = chan; |
| 108 | |
| 109 | i2c_set_adapdata(&chan->adapter, chan); |
| 110 | |
| 111 | /* Raise SCL and SDA */ |
| 112 | riva_gpio_setsda(chan, 1); |
| 113 | riva_gpio_setscl(chan, 1); |
| 114 | udelay(20); |
| 115 | |
| 116 | rc = i2c_bit_add_bus(&chan->adapter); |
| 117 | if (rc == 0) |
| 118 | dev_dbg(&chan->par->pdev->dev, "I2C bus %s registered.\n", name); |
| 119 | else { |
| 120 | dev_warn(&chan->par->pdev->dev, |
| 121 | "Failed to register I2C bus %s.\n", name); |
| 122 | chan->par = NULL; |
| 123 | } |
| 124 | |
| 125 | return rc; |
| 126 | } |
| 127 | |
| 128 | void riva_create_i2c_busses(struct riva_par *par) |
| 129 | { |
| 130 | par->bus = 3; |
| 131 | |
| 132 | par->chan[0].par = par; |
| 133 | par->chan[1].par = par; |
| 134 | par->chan[2].par = par; |
| 135 | |
| 136 | par->chan[0].ddc_base = 0x3e; |
| 137 | par->chan[1].ddc_base = 0x36; |
| 138 | par->chan[2].ddc_base = 0x50; |
| 139 | riva_setup_i2c_bus(&par->chan[0], "BUS1"); |
| 140 | riva_setup_i2c_bus(&par->chan[1], "BUS2"); |
| 141 | riva_setup_i2c_bus(&par->chan[2], "BUS3"); |
| 142 | } |
| 143 | |
| 144 | void riva_delete_i2c_busses(struct riva_par *par) |
| 145 | { |
| 146 | if (par->chan[0].par) |
| 147 | i2c_bit_del_bus(&par->chan[0].adapter); |
| 148 | par->chan[0].par = NULL; |
| 149 | |
| 150 | if (par->chan[1].par) |
| 151 | i2c_bit_del_bus(&par->chan[1].adapter); |
| 152 | par->chan[1].par = NULL; |
| 153 | |
| 154 | if (par->chan[2].par) |
| 155 | i2c_bit_del_bus(&par->chan[2].adapter); |
| 156 | par->chan[2].par = NULL; |
| 157 | } |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | int riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid) |
| 160 | { |
| 161 | u8 *edid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Antonino A. Daplas | bf5df0a | 2006-10-03 01:14:44 -0700 | [diff] [blame^] | 163 | edid = fb_ddc_read(&par->chan[conn-1].adapter); |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | if (out_edid) |
| 166 | *out_edid = edid; |
| 167 | if (!edid) |
| 168 | return 1; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |