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 target_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
= target_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 target_addr
, u8 index
, u8 data
)
134 u8 msg
[2] = { index
, data
};
137 if (!via_i2c_par
[adap
].is_active
)
140 msgs
.addr
= target_addr
/ 2;
143 ret
= i2c_transfer(&via_i2c_par
[adap
].adapter
, &msgs
, 1);
152 int viafb_i2c_readbytes(u8 adap
, u8 target_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
= target_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
->algo_data
= algo
;
206 adapter
->dev
.parent
= &pdev
->dev
;
208 adapter
->dev
.parent
= NULL
;
209 /* i2c_set_adapdata(adapter, adap_cfg); */
211 /* Raise SCL and SDA */
212 via_i2c_setsda(adap_cfg
, 1);
213 via_i2c_setscl(adap_cfg
, 1);
216 return i2c_bit_add_bus(adapter
);
219 static int viafb_i2c_probe(struct platform_device
*platdev
)
222 struct via_port_cfg
*configs
;
224 i2c_vdev
= platdev
->dev
.platform_data
;
225 configs
= i2c_vdev
->port_cfg
;
227 for (i
= 0; i
< VIAFB_NUM_PORTS
; i
++) {
228 struct via_port_cfg
*adap_cfg
= configs
++;
229 struct via_i2c_stuff
*i2c_stuff
= &via_i2c_par
[i
];
231 i2c_stuff
->is_active
= 0;
232 if (adap_cfg
->type
== 0 || adap_cfg
->mode
!= VIA_MODE_I2C
)
234 ret
= create_i2c_bus(&i2c_stuff
->adapter
,
235 &i2c_stuff
->algo
, adap_cfg
,
236 NULL
); /* FIXME: PCIDEV */
238 printk(KERN_ERR
"viafb: cannot create i2c bus %u:%d\n",
240 continue; /* Still try to make the rest */
242 i2c_stuff
->is_active
= 1;
248 static void viafb_i2c_remove(struct platform_device
*platdev
)
252 for (i
= 0; i
< VIAFB_NUM_PORTS
; i
++) {
253 struct via_i2c_stuff
*i2c_stuff
= &via_i2c_par
[i
];
255 * Only remove those entries in the array that we've
256 * actually used (and thus initialized algo_data)
258 if (i2c_stuff
->is_active
)
259 i2c_del_adapter(&i2c_stuff
->adapter
);
263 static struct platform_driver via_i2c_driver
= {
267 .probe
= viafb_i2c_probe
,
268 .remove
= viafb_i2c_remove
,
271 int viafb_i2c_init(void)
273 return platform_driver_register(&via_i2c_driver
);
276 void viafb_i2c_exit(void)
278 platform_driver_unregister(&via_i2c_driver
);