blob: 0f3ed4eb236d44f4e9e9d6e38a949919980d7821 [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;
100 i2c_transfer(&viaparinfo->i2c_stuff.adapter, msgs, 2);
101
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;
114 return i2c_transfer(&viaparinfo->i2c_stuff.adapter, &msgs, 1);
115}
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;
128 i2c_transfer(&viaparinfo->i2c_stuff.adapter, msgs, 2);
129 return 0;
130}
131
132int viafb_create_i2c_bus(void *viapar)
133{
134 int ret;
135 struct viafb_par *par = (struct viafb_par *)viapar;
136
137 strcpy(par->i2c_stuff.adapter.name, "via_i2c");
138 par->i2c_stuff.i2c_port = 0x0;
139 par->i2c_stuff.adapter.owner = THIS_MODULE;
140 par->i2c_stuff.adapter.id = 0x01FFFF;
141 par->i2c_stuff.adapter.class = 0;
142 par->i2c_stuff.adapter.algo_data = &par->i2c_stuff.algo;
143 par->i2c_stuff.adapter.dev.parent = NULL;
144 par->i2c_stuff.algo.setsda = via_i2c_setsda;
145 par->i2c_stuff.algo.setscl = via_i2c_setscl;
146 par->i2c_stuff.algo.getsda = via_i2c_getsda;
147 par->i2c_stuff.algo.getscl = via_i2c_getscl;
148 par->i2c_stuff.algo.udelay = 40;
149 par->i2c_stuff.algo.timeout = 20;
150 par->i2c_stuff.algo.data = &par->i2c_stuff;
151
152 i2c_set_adapdata(&par->i2c_stuff.adapter, &par->i2c_stuff);
153
154 /* Raise SCL and SDA */
155 par->i2c_stuff.i2c_port = I2CPORTINDEX;
156 via_i2c_setsda(&par->i2c_stuff, 1);
157 via_i2c_setscl(&par->i2c_stuff, 1);
158
159 par->i2c_stuff.i2c_port = GPIOPORTINDEX;
160 via_i2c_setsda(&par->i2c_stuff, 1);
161 via_i2c_setscl(&par->i2c_stuff, 1);
162 udelay(20);
163
164 ret = i2c_bit_add_bus(&par->i2c_stuff.adapter);
165 if (ret == 0)
166 DEBUG_MSG("I2C bus %s registered.\n",
167 par->i2c_stuff.adapter.name);
168 else
169 DEBUG_MSG("Failed to register I2C bus %s.\n",
170 par->i2c_stuff.adapter.name);
171 return ret;
172}
173
174void viafb_delete_i2c_buss(void *par)
175{
176 i2c_del_adapter(&((struct viafb_par *)par)->i2c_stuff.adapter);
177}