blob: 15543e9682489b97fd04922b23d78f5311f7e0b4 [file] [log] [blame]
Joseph Chan9f291632008-10-15 22:03:29 -07001/*
2 * Copyright 1998-2008 VIA Technologies, Inc. All Rights Reserved.
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
24static void via_i2c_setscl(void *data, int state)
25{
26 u8 val;
27 struct via_i2c_stuff *via_i2c_chan = (struct via_i2c_stuff *)data;
28
29 val = viafb_read_reg(VIASR, via_i2c_chan->i2c_port) & 0xF0;
30 if (state)
31 val |= 0x20;
32 else
33 val &= ~0x20;
34 switch (via_i2c_chan->i2c_port) {
35 case I2CPORTINDEX:
36 val |= 0x01;
37 break;
38 case GPIOPORTINDEX:
39 val |= 0x80;
40 break;
41 default:
42 DEBUG_MSG("via_i2c: specify wrong i2c port.\n");
43 }
44 viafb_write_reg(via_i2c_chan->i2c_port, VIASR, val);
45}
46
47static int via_i2c_getscl(void *data)
48{
49 struct via_i2c_stuff *via_i2c_chan = (struct via_i2c_stuff *)data;
50
51 if (viafb_read_reg(VIASR, via_i2c_chan->i2c_port) & 0x08)
52 return 1;
53 return 0;
54}
55
56static int via_i2c_getsda(void *data)
57{
58 struct via_i2c_stuff *via_i2c_chan = (struct via_i2c_stuff *)data;
59
60 if (viafb_read_reg(VIASR, via_i2c_chan->i2c_port) & 0x04)
61 return 1;
62 return 0;
63}
64
65static void via_i2c_setsda(void *data, int state)
66{
67 u8 val;
68 struct via_i2c_stuff *via_i2c_chan = (struct via_i2c_stuff *)data;
69
70 val = viafb_read_reg(VIASR, via_i2c_chan->i2c_port) & 0xF0;
71 if (state)
72 val |= 0x10;
73 else
74 val &= ~0x10;
75 switch (via_i2c_chan->i2c_port) {
76 case I2CPORTINDEX:
77 val |= 0x01;
78 break;
79 case GPIOPORTINDEX:
80 val |= 0x40;
81 break;
82 default:
83 DEBUG_MSG("via_i2c: specify wrong i2c port.\n");
84 }
85 viafb_write_reg(via_i2c_chan->i2c_port, VIASR, val);
86}
87
88int viafb_i2c_readbyte(u8 slave_addr, u8 index, u8 *pdata)
89{
90 u8 mm1[] = {0x00};
91 struct i2c_msg msgs[2];
92
93 *pdata = 0;
94 msgs[0].flags = 0;
95 msgs[1].flags = I2C_M_RD;
96 msgs[0].addr = msgs[1].addr = slave_addr / 2;
97 mm1[0] = index;
98 msgs[0].len = 1; msgs[1].len = 1;
99 msgs[0].buf = mm1; msgs[1].buf = pdata;
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700100 i2c_transfer(&viaparinfo->shared->i2c_stuff.adapter, msgs, 2);
Joseph Chan9f291632008-10-15 22:03:29 -0700101
102 return 0;
103}
104
105int viafb_i2c_writebyte(u8 slave_addr, u8 index, u8 data)
106{
107 u8 msg[2] = { index, data };
108 struct i2c_msg msgs;
109
110 msgs.flags = 0;
111 msgs.addr = slave_addr / 2;
112 msgs.len = 2;
113 msgs.buf = msg;
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700114 return i2c_transfer(&viaparinfo->shared->i2c_stuff.adapter, &msgs, 1);
Joseph Chan9f291632008-10-15 22:03:29 -0700115}
116
117int viafb_i2c_readbytes(u8 slave_addr, u8 index, u8 *buff, int buff_len)
118{
119 u8 mm1[] = {0x00};
120 struct i2c_msg msgs[2];
121
122 msgs[0].flags = 0;
123 msgs[1].flags = I2C_M_RD;
124 msgs[0].addr = msgs[1].addr = slave_addr / 2;
125 mm1[0] = index;
126 msgs[0].len = 1; msgs[1].len = buff_len;
127 msgs[0].buf = mm1; msgs[1].buf = buff;
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700128 i2c_transfer(&viaparinfo->shared->i2c_stuff.adapter, msgs, 2);
Joseph Chan9f291632008-10-15 22:03:29 -0700129 return 0;
130}
131
132int viafb_create_i2c_bus(void *viapar)
133{
134 int ret;
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700135 struct via_i2c_stuff *i2c_stuff =
136 &((struct viafb_par *)viapar)->shared->i2c_stuff;
Joseph Chan9f291632008-10-15 22:03:29 -0700137
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700138 strcpy(i2c_stuff->adapter.name, "via_i2c");
139 i2c_stuff->i2c_port = 0x0;
140 i2c_stuff->adapter.owner = THIS_MODULE;
141 i2c_stuff->adapter.id = 0x01FFFF;
142 i2c_stuff->adapter.class = 0;
143 i2c_stuff->adapter.algo_data = &i2c_stuff->algo;
144 i2c_stuff->adapter.dev.parent = NULL;
145 i2c_stuff->algo.setsda = via_i2c_setsda;
146 i2c_stuff->algo.setscl = via_i2c_setscl;
147 i2c_stuff->algo.getsda = via_i2c_getsda;
148 i2c_stuff->algo.getscl = via_i2c_getscl;
149 i2c_stuff->algo.udelay = 40;
150 i2c_stuff->algo.timeout = 20;
151 i2c_stuff->algo.data = i2c_stuff;
Joseph Chan9f291632008-10-15 22:03:29 -0700152
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700153 i2c_set_adapdata(&i2c_stuff->adapter, i2c_stuff);
Joseph Chan9f291632008-10-15 22:03:29 -0700154
155 /* Raise SCL and SDA */
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700156 i2c_stuff->i2c_port = I2CPORTINDEX;
157 via_i2c_setsda(i2c_stuff, 1);
158 via_i2c_setscl(i2c_stuff, 1);
Joseph Chan9f291632008-10-15 22:03:29 -0700159
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700160 i2c_stuff->i2c_port = GPIOPORTINDEX;
161 via_i2c_setsda(i2c_stuff, 1);
162 via_i2c_setscl(i2c_stuff, 1);
Joseph Chan9f291632008-10-15 22:03:29 -0700163 udelay(20);
164
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700165 ret = i2c_bit_add_bus(&i2c_stuff->adapter);
Joseph Chan9f291632008-10-15 22:03:29 -0700166 if (ret == 0)
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700167 DEBUG_MSG("I2C bus %s registered.\n", i2c_stuff->adapter.name);
Joseph Chan9f291632008-10-15 22:03:29 -0700168 else
169 DEBUG_MSG("Failed to register I2C bus %s.\n",
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700170 i2c_stuff->adapter.name);
Joseph Chan9f291632008-10-15 22:03:29 -0700171 return ret;
172}
173
174void viafb_delete_i2c_buss(void *par)
175{
Florian Tobias Schandinatc4df5482009-09-22 16:47:24 -0700176 i2c_del_adapter(&((struct viafb_par *)par)->shared->i2c_stuff.adapter);
Joseph Chan9f291632008-10-15 22:03:29 -0700177}