suppress verbose debug messages: change printk() to DEBUG_MSG()
[safe/jmp/linux-2.6] / drivers / video / via / via_i2c.c
1 /*
2  * Copyright 1998-2009 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
24 static void via_i2c_setscl(void *data, int state)
25 {
26         u8 val;
27         struct via_i2c_adap_cfg *adap_data = data;
28
29         DEBUG_MSG(KERN_DEBUG "reading index 0x%02x from IO 0x%x\n",
30                 adap_data->ioport_index, adap_data->io_port);
31         val = viafb_read_reg(adap_data->io_port,
32                              adap_data->ioport_index) & 0xF0;
33         if (state)
34                 val |= 0x20;
35         else
36                 val &= ~0x20;
37         switch (adap_data->type) {
38         case VIA_I2C_I2C:
39                 val |= 0x01;
40                 break;
41         case VIA_I2C_GPIO:
42                 val |= 0x80;
43                 break;
44         default:
45                 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
46         }
47         viafb_write_reg(adap_data->ioport_index,
48                         adap_data->io_port, val);
49 }
50
51 static int via_i2c_getscl(void *data)
52 {
53         struct via_i2c_adap_cfg *adap_data = data;
54
55         if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
56                 return 1;
57         return 0;
58 }
59
60 static int via_i2c_getsda(void *data)
61 {
62         struct via_i2c_adap_cfg *adap_data = data;
63
64         if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
65                 return 1;
66         return 0;
67 }
68
69 static void via_i2c_setsda(void *data, int state)
70 {
71         u8 val;
72         struct via_i2c_adap_cfg *adap_data = data;
73
74         val = viafb_read_reg(adap_data->io_port,
75                              adap_data->ioport_index) & 0xF0;
76         if (state)
77                 val |= 0x10;
78         else
79                 val &= ~0x10;
80         switch (adap_data->type) {
81         case VIA_I2C_I2C:
82                 val |= 0x01;
83                 break;
84         case VIA_I2C_GPIO:
85                 val |= 0x40;
86                 break;
87         default:
88                 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
89         }
90         viafb_write_reg(adap_data->ioport_index,
91                         adap_data->io_port, val);
92 }
93
94 int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
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;
106         return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter,
107                         msgs, 2);
108 }
109
110 int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
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;
119         return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter,
120                         &msgs, 1);
121 }
122
123 int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
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;
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 {
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);
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
173 static struct via_i2c_adap_cfg adap_configs[] = {
174         [VIA_I2C_ADAP_26]       = { VIA_I2C_I2C,  VIASR, 0x26 },
175         [VIA_I2C_ADAP_31]       = { VIA_I2C_I2C,  VIASR, 0x31 },
176         [VIA_I2C_ADAP_25]       = { VIA_I2C_GPIO, VIASR, 0x25 },
177         [VIA_I2C_ADAP_2C]       = { VIA_I2C_GPIO, VIASR, 0x2c },
178         [VIA_I2C_ADAP_3D]       = { VIA_I2C_GPIO, VIASR, 0x3d },
179         { 0, 0, 0 }
180 };
181
182 int viafb_create_i2c_busses(struct viafb_par *viapar)
183 {
184         int i, ret;
185
186         for (i = 0; i < VIAFB_NUM_I2C; i++) {
187                 struct via_i2c_adap_cfg *adap_cfg = &adap_configs[i];
188                 struct via_i2c_stuff *i2c_stuff = &viapar->shared->i2c_stuff[i];
189
190                 if (adap_cfg->type == 0)
191                         break;
192
193                 ret = create_i2c_bus(&i2c_stuff->adapter,
194                                      &i2c_stuff->algo, adap_cfg,
195                                 NULL); /* FIXME: PCIDEV */
196                 if (ret < 0) {
197                         printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
198                                 i, ret);
199                         /* FIXME: properly release previous busses */
200                         return ret;
201                 }
202         }
203
204         return 0;
205 }
206
207 void viafb_delete_i2c_busses(struct viafb_par *par)
208 {
209         int i;
210
211         for (i = 0; i < ARRAY_SIZE(par->shared->i2c_stuff); i++) {
212                 struct via_i2c_stuff *i2c_stuff = &par->shared->i2c_stuff[i];
213                 /* only remove those entries in the array that we've
214                  * actually used (and thus initialized algo_data) */
215                 if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
216                         i2c_del_adapter(&i2c_stuff->adapter);
217         }
218 }