2 * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
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.
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
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
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <linux/platform_device.h>
23 #include <linux/delay.h>
24 #include <linux/spinlock.h>
25 #include <linux/module.h>
26 #include <linux/via-core.h>
27 #include <linux/via_i2c.h>
30 * There can only be one set of these, so there's no point in having
31 * them be dynamically allocated...
33 #define VIAFB_NUM_I2C 5
34 static struct via_i2c_stuff via_i2c_par
[VIAFB_NUM_I2C
];
35 static struct viafb_dev
*i2c_vdev
; /* Passed in from core */
37 static void via_i2c_setscl(void *data
, int state
)
40 struct via_port_cfg
*adap_data
= data
;
43 spin_lock_irqsave(&i2c_vdev
->reg_lock
, flags
);
44 val
= via_read_reg(adap_data
->io_port
, adap_data
->ioport_index
) & 0xF0;
49 switch (adap_data
->type
) {
57 printk(KERN_ERR
"viafb_i2c: specify wrong i2c type.\n");
59 via_write_reg(adap_data
->io_port
, adap_data
->ioport_index
, val
);
60 spin_unlock_irqrestore(&i2c_vdev
->reg_lock
, flags
);
63 static int via_i2c_getscl(void *data
)
65 struct via_port_cfg
*adap_data
= data
;
69 spin_lock_irqsave(&i2c_vdev
->reg_lock
, flags
);
70 if (via_read_reg(adap_data
->io_port
, adap_data
->ioport_index
) & 0x08)
72 spin_unlock_irqrestore(&i2c_vdev
->reg_lock
, flags
);
76 static int via_i2c_getsda(void *data
)
78 struct via_port_cfg
*adap_data
= data
;
82 spin_lock_irqsave(&i2c_vdev
->reg_lock
, flags
);
83 if (via_read_reg(adap_data
->io_port
, adap_data
->ioport_index
) & 0x04)
85 spin_unlock_irqrestore(&i2c_vdev
->reg_lock
, flags
);
89 static void via_i2c_setsda(void *data
, int state
)
92 struct via_port_cfg
*adap_data
= data
;
95 spin_lock_irqsave(&i2c_vdev
->reg_lock
, flags
);
96 val
= via_read_reg(adap_data
->io_port
, adap_data
->ioport_index
) & 0xF0;
101 switch (adap_data
->type
) {
109 printk(KERN_ERR
"viafb_i2c: specify wrong i2c type.\n");
111 via_write_reg(adap_data
->io_port
, adap_data
->ioport_index
, val
);
112 spin_unlock_irqrestore(&i2c_vdev
->reg_lock
, flags
);
115 int viafb_i2c_readbyte(u8 adap
, u8 slave_addr
, u8 index
, u8
*pdata
)
119 struct i2c_msg msgs
[2];
121 if (!via_i2c_par
[adap
].is_active
)
125 msgs
[1].flags
= I2C_M_RD
;
126 msgs
[0].addr
= msgs
[1].addr
= slave_addr
/ 2;
128 msgs
[0].len
= 1; msgs
[1].len
= 1;
129 msgs
[0].buf
= mm1
; msgs
[1].buf
= pdata
;
130 ret
= i2c_transfer(&via_i2c_par
[adap
].adapter
, msgs
, 2);
139 int viafb_i2c_writebyte(u8 adap
, u8 slave_addr
, u8 index
, u8 data
)
142 u8 msg
[2] = { index
, data
};
145 if (!via_i2c_par
[adap
].is_active
)
148 msgs
.addr
= slave_addr
/ 2;
151 ret
= i2c_transfer(&via_i2c_par
[adap
].adapter
, &msgs
, 1);
160 int viafb_i2c_readbytes(u8 adap
, u8 slave_addr
, u8 index
, u8
*buff
, int buff_len
)
164 struct i2c_msg msgs
[2];
166 if (!via_i2c_par
[adap
].is_active
)
169 msgs
[1].flags
= I2C_M_RD
;
170 msgs
[0].addr
= msgs
[1].addr
= slave_addr
/ 2;
172 msgs
[0].len
= 1; msgs
[1].len
= buff_len
;
173 msgs
[0].buf
= mm1
; msgs
[1].buf
= buff
;
174 ret
= i2c_transfer(&via_i2c_par
[adap
].adapter
, msgs
, 2);
184 * Allow other viafb subdevices to look up a specific adapter
187 struct i2c_adapter
*viafb_find_i2c_adapter(enum viafb_i2c_adap which
)
189 struct via_i2c_stuff
*stuff
= &via_i2c_par
[which
];
191 return &stuff
->adapter
;
193 EXPORT_SYMBOL_GPL(viafb_find_i2c_adapter
);
196 static int create_i2c_bus(struct i2c_adapter
*adapter
,
197 struct i2c_algo_bit_data
*algo
,
198 struct via_port_cfg
*adap_cfg
,
199 struct pci_dev
*pdev
)
201 algo
->setsda
= via_i2c_setsda
;
202 algo
->setscl
= via_i2c_setscl
;
203 algo
->getsda
= via_i2c_getsda
;
204 algo
->getscl
= via_i2c_getscl
;
207 algo
->data
= adap_cfg
;
209 sprintf(adapter
->name
, "viafb i2c io_port idx 0x%02x",
210 adap_cfg
->ioport_index
);
211 adapter
->owner
= THIS_MODULE
;
212 adapter
->class = I2C_CLASS_DDC
;
213 adapter
->algo_data
= algo
;
215 adapter
->dev
.parent
= &pdev
->dev
;
217 adapter
->dev
.parent
= NULL
;
218 /* i2c_set_adapdata(adapter, adap_cfg); */
220 /* Raise SCL and SDA */
221 via_i2c_setsda(adap_cfg
, 1);
222 via_i2c_setscl(adap_cfg
, 1);
225 return i2c_bit_add_bus(adapter
);
228 static int viafb_i2c_probe(struct platform_device
*platdev
)
231 struct via_port_cfg
*configs
;
233 i2c_vdev
= platdev
->dev
.platform_data
;
234 configs
= i2c_vdev
->port_cfg
;
236 for (i
= 0; i
< VIAFB_NUM_PORTS
; i
++) {
237 struct via_port_cfg
*adap_cfg
= configs
++;
238 struct via_i2c_stuff
*i2c_stuff
= &via_i2c_par
[i
];
240 i2c_stuff
->is_active
= 0;
241 if (adap_cfg
->type
== 0 || adap_cfg
->mode
!= VIA_MODE_I2C
)
243 ret
= create_i2c_bus(&i2c_stuff
->adapter
,
244 &i2c_stuff
->algo
, adap_cfg
,
245 NULL
); /* FIXME: PCIDEV */
247 printk(KERN_ERR
"viafb: cannot create i2c bus %u:%d\n",
249 continue; /* Still try to make the rest */
251 i2c_stuff
->is_active
= 1;
257 static int viafb_i2c_remove(struct platform_device
*platdev
)
261 for (i
= 0; i
< VIAFB_NUM_PORTS
; i
++) {
262 struct via_i2c_stuff
*i2c_stuff
= &via_i2c_par
[i
];
264 * Only remove those entries in the array that we've
265 * actually used (and thus initialized algo_data)
267 if (i2c_stuff
->is_active
)
268 i2c_del_adapter(&i2c_stuff
->adapter
);
273 static struct platform_driver via_i2c_driver
= {
277 .probe
= viafb_i2c_probe
,
278 .remove
= viafb_i2c_remove
,
281 int viafb_i2c_init(void)
283 return platform_driver_register(&via_i2c_driver
);
286 void viafb_i2c_exit(void)
288 platform_driver_unregister(&via_i2c_driver
);