Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 1 | /* |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 2 | * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved. |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 3 | * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved. |
| 4 | |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public |
| 7 | * License as published by the Free Software Foundation; |
| 8 | * either version 2, or (at your option) any later version. |
| 9 | |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTIES OR REPRESENTATIONS; without even |
| 12 | * the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 13 | * A PARTICULAR PURPOSE.See the GNU General Public License |
| 14 | * for more details. |
| 15 | |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "global.h" |
| 23 | |
| 24 | static void via_i2c_setscl(void *data, int state) |
| 25 | { |
| 26 | u8 val; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 27 | struct via_i2c_adap_cfg *adap_data = data; |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 28 | |
Paul Fox | c774c13 | 2009-10-07 11:13:01 -0400 | [diff] [blame] | 29 | DEBUG_MSG(KERN_DEBUG "reading index 0x%02x from IO 0x%x\n", |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 30 | adap_data->ioport_index, adap_data->io_port); |
| 31 | val = viafb_read_reg(adap_data->io_port, |
| 32 | adap_data->ioport_index) & 0xF0; |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 33 | if (state) |
| 34 | val |= 0x20; |
| 35 | else |
| 36 | val &= ~0x20; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 37 | switch (adap_data->type) { |
| 38 | case VIA_I2C_I2C: |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 39 | val |= 0x01; |
| 40 | break; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 41 | case VIA_I2C_GPIO: |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 42 | val |= 0x80; |
| 43 | break; |
| 44 | default: |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 45 | DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n"); |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 46 | } |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 47 | viafb_write_reg(adap_data->ioport_index, |
| 48 | adap_data->io_port, val); |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static int via_i2c_getscl(void *data) |
| 52 | { |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 53 | struct via_i2c_adap_cfg *adap_data = data; |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 54 | |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 55 | if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08) |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 56 | return 1; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int via_i2c_getsda(void *data) |
| 61 | { |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 62 | struct via_i2c_adap_cfg *adap_data = data; |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 63 | |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 64 | if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04) |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 65 | return 1; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static void via_i2c_setsda(void *data, int state) |
| 70 | { |
| 71 | u8 val; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 72 | struct via_i2c_adap_cfg *adap_data = data; |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 73 | |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 74 | val = viafb_read_reg(adap_data->io_port, |
| 75 | adap_data->ioport_index) & 0xF0; |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 76 | if (state) |
| 77 | val |= 0x10; |
| 78 | else |
| 79 | val &= ~0x10; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 80 | switch (adap_data->type) { |
| 81 | case VIA_I2C_I2C: |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 82 | val |= 0x01; |
| 83 | break; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 84 | case VIA_I2C_GPIO: |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 85 | val |= 0x40; |
| 86 | break; |
| 87 | default: |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 88 | DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n"); |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 89 | } |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 90 | viafb_write_reg(adap_data->ioport_index, |
| 91 | adap_data->io_port, val); |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 94 | int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata) |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 95 | { |
| 96 | u8 mm1[] = {0x00}; |
| 97 | struct i2c_msg msgs[2]; |
| 98 | |
| 99 | *pdata = 0; |
| 100 | msgs[0].flags = 0; |
| 101 | msgs[1].flags = I2C_M_RD; |
| 102 | msgs[0].addr = msgs[1].addr = slave_addr / 2; |
| 103 | mm1[0] = index; |
| 104 | msgs[0].len = 1; msgs[1].len = 1; |
| 105 | msgs[0].buf = mm1; msgs[1].buf = pdata; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 106 | return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter, |
| 107 | msgs, 2); |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 110 | int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data) |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 111 | { |
| 112 | u8 msg[2] = { index, data }; |
| 113 | struct i2c_msg msgs; |
| 114 | |
| 115 | msgs.flags = 0; |
| 116 | msgs.addr = slave_addr / 2; |
| 117 | msgs.len = 2; |
| 118 | msgs.buf = msg; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 119 | return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter, |
| 120 | &msgs, 1); |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 123 | int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len) |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 124 | { |
| 125 | u8 mm1[] = {0x00}; |
| 126 | struct i2c_msg msgs[2]; |
| 127 | |
| 128 | msgs[0].flags = 0; |
| 129 | msgs[1].flags = I2C_M_RD; |
| 130 | msgs[0].addr = msgs[1].addr = slave_addr / 2; |
| 131 | mm1[0] = index; |
| 132 | msgs[0].len = 1; msgs[1].len = buff_len; |
| 133 | msgs[0].buf = mm1; msgs[1].buf = buff; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 134 | return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter, |
| 135 | msgs, 2); |
| 136 | } |
| 137 | |
| 138 | static int create_i2c_bus(struct i2c_adapter *adapter, |
| 139 | struct i2c_algo_bit_data *algo, |
| 140 | struct via_i2c_adap_cfg *adap_cfg, |
| 141 | struct pci_dev *pdev) |
| 142 | { |
Paul Fox | c774c13 | 2009-10-07 11:13:01 -0400 | [diff] [blame] | 143 | DEBUG_MSG(KERN_DEBUG "viafb: creating bus adap=0x%p, algo_bit_data=0x%p, adap_cfg=0x%p\n", adapter, algo, adap_cfg); |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 144 | |
| 145 | algo->setsda = via_i2c_setsda; |
| 146 | algo->setscl = via_i2c_setscl; |
| 147 | algo->getsda = via_i2c_getsda; |
| 148 | algo->getscl = via_i2c_getscl; |
| 149 | algo->udelay = 40; |
| 150 | algo->timeout = 20; |
| 151 | algo->data = adap_cfg; |
| 152 | |
| 153 | sprintf(adapter->name, "viafb i2c io_port idx 0x%02x", |
| 154 | adap_cfg->ioport_index); |
| 155 | adapter->owner = THIS_MODULE; |
| 156 | adapter->id = 0x01FFFF; |
| 157 | adapter->class = I2C_CLASS_DDC; |
| 158 | adapter->algo_data = algo; |
| 159 | if (pdev) |
| 160 | adapter->dev.parent = &pdev->dev; |
| 161 | else |
| 162 | adapter->dev.parent = NULL; |
| 163 | /* i2c_set_adapdata(adapter, adap_cfg); */ |
| 164 | |
| 165 | /* Raise SCL and SDA */ |
| 166 | via_i2c_setsda(adap_cfg, 1); |
| 167 | via_i2c_setscl(adap_cfg, 1); |
| 168 | udelay(20); |
| 169 | |
| 170 | return i2c_bit_add_bus(adapter); |
| 171 | } |
| 172 | |
Jonathan Corbet | 4da62e6 | 2010-04-25 08:30:41 -0600 | [diff] [blame^] | 173 | /* |
| 174 | * By default, we only activate busses on ports 2c and 31 to avoid |
| 175 | * conflicts with other possible users; that default can be changed |
| 176 | * below. |
| 177 | */ |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 178 | static struct via_i2c_adap_cfg adap_configs[] = { |
Jonathan Corbet | 4da62e6 | 2010-04-25 08:30:41 -0600 | [diff] [blame^] | 179 | [VIA_I2C_ADAP_26] = { VIA_I2C_I2C, VIASR, 0x26, 0 }, |
| 180 | [VIA_I2C_ADAP_31] = { VIA_I2C_I2C, VIASR, 0x31, 1 }, |
| 181 | [VIA_I2C_ADAP_25] = { VIA_I2C_GPIO, VIASR, 0x25, 0 }, |
| 182 | [VIA_I2C_ADAP_2C] = { VIA_I2C_GPIO, VIASR, 0x2c, 1 }, |
| 183 | [VIA_I2C_ADAP_3D] = { VIA_I2C_GPIO, VIASR, 0x3d, 0 }, |
| 184 | { 0, 0, 0, 0 } |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | int viafb_create_i2c_busses(struct viafb_par *viapar) |
| 188 | { |
| 189 | int i, ret; |
| 190 | |
| 191 | for (i = 0; i < VIAFB_NUM_I2C; i++) { |
| 192 | struct via_i2c_adap_cfg *adap_cfg = &adap_configs[i]; |
| 193 | struct via_i2c_stuff *i2c_stuff = &viapar->shared->i2c_stuff[i]; |
| 194 | |
| 195 | if (adap_cfg->type == 0) |
| 196 | break; |
Jonathan Corbet | 4da62e6 | 2010-04-25 08:30:41 -0600 | [diff] [blame^] | 197 | if (!adap_cfg->is_active) |
| 198 | continue; |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 199 | |
| 200 | ret = create_i2c_bus(&i2c_stuff->adapter, |
| 201 | &i2c_stuff->algo, adap_cfg, |
| 202 | NULL); /* FIXME: PCIDEV */ |
| 203 | if (ret < 0) { |
| 204 | printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n", |
| 205 | i, ret); |
| 206 | /* FIXME: properly release previous busses */ |
| 207 | return ret; |
| 208 | } |
| 209 | } |
| 210 | |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 214 | void viafb_delete_i2c_busses(struct viafb_par *par) |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 215 | { |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 216 | int i; |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 217 | |
Harald Welte | 277d32a | 2009-05-23 00:35:39 +0800 | [diff] [blame] | 218 | for (i = 0; i < ARRAY_SIZE(par->shared->i2c_stuff); i++) { |
| 219 | struct via_i2c_stuff *i2c_stuff = &par->shared->i2c_stuff[i]; |
| 220 | /* only remove those entries in the array that we've |
| 221 | * actually used (and thus initialized algo_data) */ |
| 222 | if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo) |
| 223 | i2c_del_adapter(&i2c_stuff->adapter); |
| 224 | } |
Joseph Chan | 9f29163 | 2008-10-15 22:03:29 -0700 | [diff] [blame] | 225 | } |