blob: bcf2fe61a6ecda9b75760c158082a34f54d6bd45 [file] [log] [blame]
Joseph Chan9f291632008-10-15 22:03:29 -07001/*
Harald Welte277d32a2009-05-23 00:35:39 +08002 * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
Joseph Chan9f291632008-10-15 22:03:29 -07003 * 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
Jonathan Corbet24b4d822010-04-22 13:48:09 -060022#include "via-core.h"
23#include "via_i2c.h"
Joseph Chan9f291632008-10-15 22:03:29 -070024#include "global.h"
25
Jonathan Corbetf045f772009-12-01 20:29:39 -070026/*
27 * There can only be one set of these, so there's no point in having
28 * them be dynamically allocated...
29 */
30#define VIAFB_NUM_I2C 5
31static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
32
Joseph Chan9f291632008-10-15 22:03:29 -070033static void via_i2c_setscl(void *data, int state)
34{
35 u8 val;
Jonathan Corbetf045f772009-12-01 20:29:39 -070036 struct via_port_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070037
Harald Welte277d32a2009-05-23 00:35:39 +080038 val = viafb_read_reg(adap_data->io_port,
39 adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -070040 if (state)
41 val |= 0x20;
42 else
43 val &= ~0x20;
Harald Welte277d32a2009-05-23 00:35:39 +080044 switch (adap_data->type) {
Jonathan Corbetf045f772009-12-01 20:29:39 -070045 case VIA_PORT_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -070046 val |= 0x01;
47 break;
Jonathan Corbetf045f772009-12-01 20:29:39 -070048 case VIA_PORT_GPIO:
Joseph Chan9f291632008-10-15 22:03:29 -070049 val |= 0x80;
50 break;
51 default:
Harald Welte277d32a2009-05-23 00:35:39 +080052 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -070053 }
Harald Welte277d32a2009-05-23 00:35:39 +080054 viafb_write_reg(adap_data->ioport_index,
55 adap_data->io_port, val);
Joseph Chan9f291632008-10-15 22:03:29 -070056}
57
58static int via_i2c_getscl(void *data)
59{
Jonathan Corbetf045f772009-12-01 20:29:39 -070060 struct via_port_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070061
Harald Welte277d32a2009-05-23 00:35:39 +080062 if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
Joseph Chan9f291632008-10-15 22:03:29 -070063 return 1;
64 return 0;
65}
66
67static int via_i2c_getsda(void *data)
68{
Jonathan Corbetf045f772009-12-01 20:29:39 -070069 struct via_port_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070070
Harald Welte277d32a2009-05-23 00:35:39 +080071 if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
Joseph Chan9f291632008-10-15 22:03:29 -070072 return 1;
73 return 0;
74}
75
76static void via_i2c_setsda(void *data, int state)
77{
78 u8 val;
Jonathan Corbetf045f772009-12-01 20:29:39 -070079 struct via_port_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070080
Harald Welte277d32a2009-05-23 00:35:39 +080081 val = viafb_read_reg(adap_data->io_port,
82 adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -070083 if (state)
84 val |= 0x10;
85 else
86 val &= ~0x10;
Harald Welte277d32a2009-05-23 00:35:39 +080087 switch (adap_data->type) {
Jonathan Corbetf045f772009-12-01 20:29:39 -070088 case VIA_PORT_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -070089 val |= 0x01;
90 break;
Jonathan Corbetf045f772009-12-01 20:29:39 -070091 case VIA_PORT_GPIO:
Joseph Chan9f291632008-10-15 22:03:29 -070092 val |= 0x40;
93 break;
94 default:
Harald Welte277d32a2009-05-23 00:35:39 +080095 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -070096 }
Harald Welte277d32a2009-05-23 00:35:39 +080097 viafb_write_reg(adap_data->ioport_index,
98 adap_data->io_port, val);
Joseph Chan9f291632008-10-15 22:03:29 -070099}
100
Harald Welte277d32a2009-05-23 00:35:39 +0800101int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
Joseph Chan9f291632008-10-15 22:03:29 -0700102{
103 u8 mm1[] = {0x00};
104 struct i2c_msg msgs[2];
105
106 *pdata = 0;
107 msgs[0].flags = 0;
108 msgs[1].flags = I2C_M_RD;
109 msgs[0].addr = msgs[1].addr = slave_addr / 2;
110 mm1[0] = index;
111 msgs[0].len = 1; msgs[1].len = 1;
112 msgs[0].buf = mm1; msgs[1].buf = pdata;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700113 return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
Joseph Chan9f291632008-10-15 22:03:29 -0700114}
115
Harald Welte277d32a2009-05-23 00:35:39 +0800116int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
Joseph Chan9f291632008-10-15 22:03:29 -0700117{
118 u8 msg[2] = { index, data };
119 struct i2c_msg msgs;
120
121 msgs.flags = 0;
122 msgs.addr = slave_addr / 2;
123 msgs.len = 2;
124 msgs.buf = msg;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700125 return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
Joseph Chan9f291632008-10-15 22:03:29 -0700126}
127
Harald Welte277d32a2009-05-23 00:35:39 +0800128int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
Joseph Chan9f291632008-10-15 22:03:29 -0700129{
130 u8 mm1[] = {0x00};
131 struct i2c_msg msgs[2];
132
133 msgs[0].flags = 0;
134 msgs[1].flags = I2C_M_RD;
135 msgs[0].addr = msgs[1].addr = slave_addr / 2;
136 mm1[0] = index;
137 msgs[0].len = 1; msgs[1].len = buff_len;
138 msgs[0].buf = mm1; msgs[1].buf = buff;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700139 return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
Harald Welte277d32a2009-05-23 00:35:39 +0800140}
141
142static int create_i2c_bus(struct i2c_adapter *adapter,
143 struct i2c_algo_bit_data *algo,
Jonathan Corbetf045f772009-12-01 20:29:39 -0700144 struct via_port_cfg *adap_cfg,
Harald Welte277d32a2009-05-23 00:35:39 +0800145 struct pci_dev *pdev)
146{
Paul Foxc774c132009-10-07 11:13:01 -0400147 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 Welte277d32a2009-05-23 00:35:39 +0800148
149 algo->setsda = via_i2c_setsda;
150 algo->setscl = via_i2c_setscl;
151 algo->getsda = via_i2c_getsda;
152 algo->getscl = via_i2c_getscl;
153 algo->udelay = 40;
154 algo->timeout = 20;
155 algo->data = adap_cfg;
156
157 sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
158 adap_cfg->ioport_index);
159 adapter->owner = THIS_MODULE;
160 adapter->id = 0x01FFFF;
161 adapter->class = I2C_CLASS_DDC;
162 adapter->algo_data = algo;
163 if (pdev)
164 adapter->dev.parent = &pdev->dev;
165 else
166 adapter->dev.parent = NULL;
167 /* i2c_set_adapdata(adapter, adap_cfg); */
168
169 /* Raise SCL and SDA */
170 via_i2c_setsda(adap_cfg, 1);
171 via_i2c_setscl(adap_cfg, 1);
172 udelay(20);
173
174 return i2c_bit_add_bus(adapter);
175}
176
Jonathan Corbetf045f772009-12-01 20:29:39 -0700177int viafb_create_i2c_busses(struct via_port_cfg *configs)
Harald Welte277d32a2009-05-23 00:35:39 +0800178{
179 int i, ret;
180
Jonathan Corbetf045f772009-12-01 20:29:39 -0700181 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
182 struct via_port_cfg *adap_cfg = configs++;
183 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
Harald Welte277d32a2009-05-23 00:35:39 +0800184
Jonathan Corbetf045f772009-12-01 20:29:39 -0700185 if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
Jonathan Corbet4da62e62010-04-25 08:30:41 -0600186 continue;
Harald Welte277d32a2009-05-23 00:35:39 +0800187
188 ret = create_i2c_bus(&i2c_stuff->adapter,
189 &i2c_stuff->algo, adap_cfg,
190 NULL); /* FIXME: PCIDEV */
191 if (ret < 0) {
192 printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
193 i, ret);
194 /* FIXME: properly release previous busses */
195 return ret;
196 }
197 }
198
Joseph Chan9f291632008-10-15 22:03:29 -0700199 return 0;
200}
201
Jonathan Corbetf045f772009-12-01 20:29:39 -0700202void viafb_delete_i2c_busses(void)
Joseph Chan9f291632008-10-15 22:03:29 -0700203{
Harald Welte277d32a2009-05-23 00:35:39 +0800204 int i;
Joseph Chan9f291632008-10-15 22:03:29 -0700205
Jonathan Corbetf045f772009-12-01 20:29:39 -0700206 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
207 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
208 /*
209 * Only remove those entries in the array that we've
210 * actually used (and thus initialized algo_data)
211 */
Harald Welte277d32a2009-05-23 00:35:39 +0800212 if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
213 i2c_del_adapter(&i2c_stuff->adapter);
214 }
Joseph Chan9f291632008-10-15 22:03:29 -0700215}