1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
4 * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
8 #include <linux/platform_device.h>
9 #include <linux/delay.h>
10 #include <linux/spinlock.h>
11 #include <linux/module.h>
12 #include <linux/via-core.h>
13 #include <linux/via_i2c.h>
16 * There can only be one set of these, so there's no point in having
17 * them be dynamically allocated...
19 #define VIAFB_NUM_I2C 5
20 static struct via_i2c_stuff via_i2c_par
[VIAFB_NUM_I2C
];
21 static struct viafb_dev
*i2c_vdev
; /* Passed in from core */
23 static void via_i2c_setscl(void *data
, int state
)
26 struct via_port_cfg
*adap_data
= data
;
29 spin_lock_irqsave(&i2c_vdev
->reg_lock
, flags
);
30 val
= via_read_reg(adap_data
->io_port
, adap_data
->ioport_index
) & 0xF0;
35 switch (adap_data
->type
) {
43 printk(KERN_ERR
"viafb_i2c: specify wrong i2c type.\n");
45 via_write_reg(adap_data
->io_port
, adap_data
->ioport_index
, val
);
46 spin_unlock_irqrestore(&i2c_vdev
->reg_lock
, flags
);
49 static int via_i2c_getscl(void *data
)
51 struct via_port_cfg
*adap_data
= data
;
55 spin_lock_irqsave(&i2c_vdev
->reg_lock
, flags
);
56 if (adap_data
->type
== VIA_PORT_GPIO
)
57 via_write_reg_mask(adap_data
->io_port
, adap_data
->ioport_index
,
59 if (via_read_reg(adap_data
->io_port
, adap_data
->ioport_index
) & 0x08)
61 spin_unlock_irqrestore(&i2c_vdev
->reg_lock
, flags
);
65 static int via_i2c_getsda(void *data
)
67 struct via_port_cfg
*adap_data
= data
;
71 spin_lock_irqsave(&i2c_vdev
->reg_lock
, flags
);
72 if (adap_data
->type
== VIA_PORT_GPIO
)
73 via_write_reg_mask(adap_data
->io_port
, adap_data
->ioport_index
,
75 if (via_read_reg(adap_data
->io_port
, adap_data
->ioport_index
) & 0x04)
77 spin_unlock_irqrestore(&i2c_vdev
->reg_lock
, flags
);
81 static void via_i2c_setsda(void *data
, int state
)
84 struct via_port_cfg
*adap_data
= data
;
87 spin_lock_irqsave(&i2c_vdev
->reg_lock
, flags
);
88 val
= via_read_reg(adap_data
->io_port
, adap_data
->ioport_index
) & 0xF0;
93 switch (adap_data
->type
) {
101 printk(KERN_ERR
"viafb_i2c: specify wrong i2c type.\n");
103 via_write_reg(adap_data
->io_port
, adap_data
->ioport_index
, val
);
104 spin_unlock_irqrestore(&i2c_vdev
->reg_lock
, flags
);
107 int viafb_i2c_readbyte(u8 adap
, u8 slave_addr
, u8 index
, u8
*pdata
)
111 struct i2c_msg msgs
[2];
113 if (!via_i2c_par
[adap
].is_active
)
117 msgs
[1].flags
= I2C_M_RD
;
118 msgs
[0].addr
= msgs
[1].addr
= slave_addr
/ 2;
120 msgs
[0].len
= 1; msgs
[1].len
= 1;
121 msgs
[0].buf
= mm1
; msgs
[1].buf
= pdata
;
122 ret
= i2c_transfer(&via_i2c_par
[adap
].adapter
, msgs
, 2);
131 int viafb_i2c_writebyte(u8 adap
, u8 slave_addr
, u8 index
, u8 data
)
134 u8 msg
[2] = { index
, data
};
137 if (!via_i2c_par
[adap
].is_active
)
140 msgs
.addr
= slave_addr
/ 2;
143 ret
= i2c_transfer(&via_i2c_par
[adap
].adapter
, &msgs
, 1);
152 int viafb_i2c_readbytes(u8 adap
, u8 slave_addr
, u8 index
, u8
*buff
, int buff_len
)
156 struct i2c_msg msgs
[2];
158 if (!via_i2c_par
[adap
].is_active
)
161 msgs
[1].flags
= I2C_M_RD
;
162 msgs
[0].addr
= msgs
[1].addr
= slave_addr
/ 2;
164 msgs
[0].len
= 1; msgs
[1].len
= buff_len
;
165 msgs
[0].buf
= mm1
; msgs
[1].buf
= buff
;
166 ret
= i2c_transfer(&via_i2c_par
[adap
].adapter
, msgs
, 2);
176 * Allow other viafb subdevices to look up a specific adapter
179 struct i2c_adapter
*viafb_find_i2c_adapter(enum viafb_i2c_adap which
)
181 struct via_i2c_stuff
*stuff
= &via_i2c_par
[which
];
183 return &stuff
->adapter
;
185 EXPORT_SYMBOL_GPL(viafb_find_i2c_adapter
);
188 static int create_i2c_bus(struct i2c_adapter
*adapter
,
189 struct i2c_algo_bit_data
*algo
,
190 struct via_port_cfg
*adap_cfg
,
191 struct pci_dev
*pdev
)
193 algo
->setsda
= via_i2c_setsda
;
194 algo
->setscl
= via_i2c_setscl
;
195 algo
->getsda
= via_i2c_getsda
;
196 algo
->getscl
= via_i2c_getscl
;
199 algo
->data
= adap_cfg
;
201 sprintf(adapter
->name
, "viafb i2c io_port idx 0x%02x",
202 adap_cfg
->ioport_index
);
203 adapter
->owner
= THIS_MODULE
;
204 adapter
->class = I2C_CLASS_DDC
;
205 adapter
->algo_data
= algo
;
207 adapter
->dev
.parent
= &pdev
->dev
;
209 adapter
->dev
.parent
= NULL
;
210 /* i2c_set_adapdata(adapter, adap_cfg); */
212 /* Raise SCL and SDA */
213 via_i2c_setsda(adap_cfg
, 1);
214 via_i2c_setscl(adap_cfg
, 1);
217 return i2c_bit_add_bus(adapter
);
220 static int viafb_i2c_probe(struct platform_device
*platdev
)
223 struct via_port_cfg
*configs
;
225 i2c_vdev
= platdev
->dev
.platform_data
;
226 configs
= i2c_vdev
->port_cfg
;
228 for (i
= 0; i
< VIAFB_NUM_PORTS
; i
++) {
229 struct via_port_cfg
*adap_cfg
= configs
++;
230 struct via_i2c_stuff
*i2c_stuff
= &via_i2c_par
[i
];
232 i2c_stuff
->is_active
= 0;
233 if (adap_cfg
->type
== 0 || adap_cfg
->mode
!= VIA_MODE_I2C
)
235 ret
= create_i2c_bus(&i2c_stuff
->adapter
,
236 &i2c_stuff
->algo
, adap_cfg
,
237 NULL
); /* FIXME: PCIDEV */
239 printk(KERN_ERR
"viafb: cannot create i2c bus %u:%d\n",
241 continue; /* Still try to make the rest */
243 i2c_stuff
->is_active
= 1;
249 static int viafb_i2c_remove(struct platform_device
*platdev
)
253 for (i
= 0; i
< VIAFB_NUM_PORTS
; i
++) {
254 struct via_i2c_stuff
*i2c_stuff
= &via_i2c_par
[i
];
256 * Only remove those entries in the array that we've
257 * actually used (and thus initialized algo_data)
259 if (i2c_stuff
->is_active
)
260 i2c_del_adapter(&i2c_stuff
->adapter
);
265 static struct platform_driver via_i2c_driver
= {
269 .probe
= viafb_i2c_probe
,
270 .remove
= viafb_i2c_remove
,
273 int viafb_i2c_init(void)
275 return platform_driver_register(&via_i2c_driver
);
278 void viafb_i2c_exit(void)
280 platform_driver_unregister(&via_i2c_driver
);