blob: dd53058bbbb7cf0b2c19d27561936d894b05ac14 [file] [log] [blame]
Joseph Chan9f291632008-10-15 22:03:29 -07001/*
Harald Welte277d32a2009-05-23 00:35:39 +08002 * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
Joseph Chan9f291632008-10-15 22:03:29 -07003 * 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
Jonathan Corbet7582eb92010-04-22 17:39:34 -060022#include <linux/platform_device.h>
Jonathan Corbet9221fc62010-05-05 14:24:18 -060023#include <linux/delay.h>
24#include <linux/spinlock.h>
25#include <linux/module.h>
Jonathan Corbetec668412010-05-05 14:44:55 -060026#include <linux/via-core.h>
27#include <linux/via_i2c.h>
Joseph Chan9f291632008-10-15 22:03:29 -070028
Jonathan Corbetf045f772009-12-01 20:29:39 -070029/*
30 * There can only be one set of these, so there's no point in having
31 * them be dynamically allocated...
32 */
33#define VIAFB_NUM_I2C 5
34static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
Stephen Hemminger23e5abd2011-03-03 10:00:08 -080035static struct viafb_dev *i2c_vdev; /* Passed in from core */
Jonathan Corbetf045f772009-12-01 20:29:39 -070036
Joseph Chan9f291632008-10-15 22:03:29 -070037static void via_i2c_setscl(void *data, int state)
38{
39 u8 val;
Jonathan Corbetf045f772009-12-01 20:29:39 -070040 struct via_port_cfg *adap_data = data;
Jonathan Corbet75b035a2010-04-22 14:36:04 -060041 unsigned long flags;
Joseph Chan9f291632008-10-15 22:03:29 -070042
Jonathan Corbet75b035a2010-04-22 14:36:04 -060043 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
44 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -070045 if (state)
46 val |= 0x20;
47 else
48 val &= ~0x20;
Harald Welte277d32a2009-05-23 00:35:39 +080049 switch (adap_data->type) {
Jonathan Corbetf045f772009-12-01 20:29:39 -070050 case VIA_PORT_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -070051 val |= 0x01;
52 break;
Jonathan Corbetf045f772009-12-01 20:29:39 -070053 case VIA_PORT_GPIO:
Florian Tobias Schandinat2c4c8a82011-12-29 23:37:07 +000054 val |= 0x82;
Joseph Chan9f291632008-10-15 22:03:29 -070055 break;
56 default:
Jonathan Corbet4a28ea92010-05-05 14:28:53 -060057 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -070058 }
Jonathan Corbet75b035a2010-04-22 14:36:04 -060059 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
60 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
Joseph Chan9f291632008-10-15 22:03:29 -070061}
62
63static int via_i2c_getscl(void *data)
64{
Jonathan Corbetf045f772009-12-01 20:29:39 -070065 struct via_port_cfg *adap_data = data;
Jonathan Corbet75b035a2010-04-22 14:36:04 -060066 unsigned long flags;
67 int ret = 0;
Joseph Chan9f291632008-10-15 22:03:29 -070068
Jonathan Corbet75b035a2010-04-22 14:36:04 -060069 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
Florian Tobias Schandinat2c4c8a82011-12-29 23:37:07 +000070 if (adap_data->type == VIA_PORT_GPIO)
71 via_write_reg_mask(adap_data->io_port, adap_data->ioport_index,
72 0, 0x80);
Jonathan Corbet75b035a2010-04-22 14:36:04 -060073 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
74 ret = 1;
75 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
76 return ret;
Joseph Chan9f291632008-10-15 22:03:29 -070077}
78
79static int via_i2c_getsda(void *data)
80{
Jonathan Corbetf045f772009-12-01 20:29:39 -070081 struct via_port_cfg *adap_data = data;
Jonathan Corbet75b035a2010-04-22 14:36:04 -060082 unsigned long flags;
83 int ret = 0;
Joseph Chan9f291632008-10-15 22:03:29 -070084
Jonathan Corbet75b035a2010-04-22 14:36:04 -060085 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
Florian Tobias Schandinat2c4c8a82011-12-29 23:37:07 +000086 if (adap_data->type == VIA_PORT_GPIO)
87 via_write_reg_mask(adap_data->io_port, adap_data->ioport_index,
88 0, 0x40);
Jonathan Corbet75b035a2010-04-22 14:36:04 -060089 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
90 ret = 1;
91 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
92 return ret;
Joseph Chan9f291632008-10-15 22:03:29 -070093}
94
95static void via_i2c_setsda(void *data, int state)
96{
97 u8 val;
Jonathan Corbetf045f772009-12-01 20:29:39 -070098 struct via_port_cfg *adap_data = data;
Jonathan Corbet75b035a2010-04-22 14:36:04 -060099 unsigned long flags;
Joseph Chan9f291632008-10-15 22:03:29 -0700100
Jonathan Corbet75b035a2010-04-22 14:36:04 -0600101 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
102 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -0700103 if (state)
104 val |= 0x10;
105 else
106 val &= ~0x10;
Harald Welte277d32a2009-05-23 00:35:39 +0800107 switch (adap_data->type) {
Jonathan Corbetf045f772009-12-01 20:29:39 -0700108 case VIA_PORT_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -0700109 val |= 0x01;
110 break;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700111 case VIA_PORT_GPIO:
Florian Tobias Schandinat2c4c8a82011-12-29 23:37:07 +0000112 val |= 0x42;
Joseph Chan9f291632008-10-15 22:03:29 -0700113 break;
114 default:
Jonathan Corbet4a28ea92010-05-05 14:28:53 -0600115 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -0700116 }
Jonathan Corbet75b035a2010-04-22 14:36:04 -0600117 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
118 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
Joseph Chan9f291632008-10-15 22:03:29 -0700119}
120
Harald Welte277d32a2009-05-23 00:35:39 +0800121int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
Joseph Chan9f291632008-10-15 22:03:29 -0700122{
Florian Tobias Schandinat85c57022010-09-17 01:16:25 +0000123 int ret;
Joseph Chan9f291632008-10-15 22:03:29 -0700124 u8 mm1[] = {0x00};
125 struct i2c_msg msgs[2];
126
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700127 if (!via_i2c_par[adap].is_active)
128 return -ENODEV;
Joseph Chan9f291632008-10-15 22:03:29 -0700129 *pdata = 0;
130 msgs[0].flags = 0;
131 msgs[1].flags = I2C_M_RD;
132 msgs[0].addr = msgs[1].addr = slave_addr / 2;
133 mm1[0] = index;
134 msgs[0].len = 1; msgs[1].len = 1;
135 msgs[0].buf = mm1; msgs[1].buf = pdata;
Florian Tobias Schandinat85c57022010-09-17 01:16:25 +0000136 ret = i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
137 if (ret == 2)
138 ret = 0;
139 else if (ret >= 0)
140 ret = -EIO;
141
142 return ret;
Joseph Chan9f291632008-10-15 22:03:29 -0700143}
144
Harald Welte277d32a2009-05-23 00:35:39 +0800145int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
Joseph Chan9f291632008-10-15 22:03:29 -0700146{
Florian Tobias Schandinat85c57022010-09-17 01:16:25 +0000147 int ret;
Joseph Chan9f291632008-10-15 22:03:29 -0700148 u8 msg[2] = { index, data };
149 struct i2c_msg msgs;
150
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700151 if (!via_i2c_par[adap].is_active)
152 return -ENODEV;
Joseph Chan9f291632008-10-15 22:03:29 -0700153 msgs.flags = 0;
154 msgs.addr = slave_addr / 2;
155 msgs.len = 2;
156 msgs.buf = msg;
Florian Tobias Schandinat85c57022010-09-17 01:16:25 +0000157 ret = i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
158 if (ret == 1)
159 ret = 0;
160 else if (ret >= 0)
161 ret = -EIO;
162
163 return ret;
Joseph Chan9f291632008-10-15 22:03:29 -0700164}
165
Harald Welte277d32a2009-05-23 00:35:39 +0800166int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
Joseph Chan9f291632008-10-15 22:03:29 -0700167{
Florian Tobias Schandinat85c57022010-09-17 01:16:25 +0000168 int ret;
Joseph Chan9f291632008-10-15 22:03:29 -0700169 u8 mm1[] = {0x00};
170 struct i2c_msg msgs[2];
171
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700172 if (!via_i2c_par[adap].is_active)
173 return -ENODEV;
Joseph Chan9f291632008-10-15 22:03:29 -0700174 msgs[0].flags = 0;
175 msgs[1].flags = I2C_M_RD;
176 msgs[0].addr = msgs[1].addr = slave_addr / 2;
177 mm1[0] = index;
178 msgs[0].len = 1; msgs[1].len = buff_len;
179 msgs[0].buf = mm1; msgs[1].buf = buff;
Florian Tobias Schandinat85c57022010-09-17 01:16:25 +0000180 ret = i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
181 if (ret == 2)
182 ret = 0;
183 else if (ret >= 0)
184 ret = -EIO;
185
186 return ret;
Harald Welte277d32a2009-05-23 00:35:39 +0800187}
188
Jonathan Corbetb8f7e5d2010-04-23 08:56:55 -0600189/*
190 * Allow other viafb subdevices to look up a specific adapter
191 * by port name.
192 */
193struct i2c_adapter *viafb_find_i2c_adapter(enum viafb_i2c_adap which)
194{
195 struct via_i2c_stuff *stuff = &via_i2c_par[which];
196
197 return &stuff->adapter;
198}
199EXPORT_SYMBOL_GPL(viafb_find_i2c_adapter);
200
201
Harald Welte277d32a2009-05-23 00:35:39 +0800202static int create_i2c_bus(struct i2c_adapter *adapter,
203 struct i2c_algo_bit_data *algo,
Jonathan Corbetf045f772009-12-01 20:29:39 -0700204 struct via_port_cfg *adap_cfg,
Harald Welte277d32a2009-05-23 00:35:39 +0800205 struct pci_dev *pdev)
206{
Harald Welte277d32a2009-05-23 00:35:39 +0800207 algo->setsda = via_i2c_setsda;
208 algo->setscl = via_i2c_setscl;
209 algo->getsda = via_i2c_getsda;
210 algo->getscl = via_i2c_getscl;
Florian Tobias Schandinate029ab02010-09-17 02:10:33 +0000211 algo->udelay = 10;
212 algo->timeout = 2;
Harald Welte277d32a2009-05-23 00:35:39 +0800213 algo->data = adap_cfg;
214
215 sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
216 adap_cfg->ioport_index);
217 adapter->owner = THIS_MODULE;
Harald Welte277d32a2009-05-23 00:35:39 +0800218 adapter->class = I2C_CLASS_DDC;
219 adapter->algo_data = algo;
220 if (pdev)
221 adapter->dev.parent = &pdev->dev;
222 else
223 adapter->dev.parent = NULL;
224 /* i2c_set_adapdata(adapter, adap_cfg); */
225
226 /* Raise SCL and SDA */
227 via_i2c_setsda(adap_cfg, 1);
228 via_i2c_setscl(adap_cfg, 1);
229 udelay(20);
230
231 return i2c_bit_add_bus(adapter);
232}
233
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600234static int viafb_i2c_probe(struct platform_device *platdev)
Harald Welte277d32a2009-05-23 00:35:39 +0800235{
236 int i, ret;
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600237 struct via_port_cfg *configs;
Harald Welte277d32a2009-05-23 00:35:39 +0800238
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600239 i2c_vdev = platdev->dev.platform_data;
240 configs = i2c_vdev->port_cfg;
241
Jonathan Corbetf045f772009-12-01 20:29:39 -0700242 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
243 struct via_port_cfg *adap_cfg = configs++;
244 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
Harald Welte277d32a2009-05-23 00:35:39 +0800245
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700246 i2c_stuff->is_active = 0;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700247 if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
Jonathan Corbet4da62e62010-04-25 08:30:41 -0600248 continue;
Harald Welte277d32a2009-05-23 00:35:39 +0800249 ret = create_i2c_bus(&i2c_stuff->adapter,
250 &i2c_stuff->algo, adap_cfg,
251 NULL); /* FIXME: PCIDEV */
252 if (ret < 0) {
253 printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
254 i, ret);
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700255 continue; /* Still try to make the rest */
Harald Welte277d32a2009-05-23 00:35:39 +0800256 }
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700257 i2c_stuff->is_active = 1;
Harald Welte277d32a2009-05-23 00:35:39 +0800258 }
259
Joseph Chan9f291632008-10-15 22:03:29 -0700260 return 0;
261}
262
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600263static int viafb_i2c_remove(struct platform_device *platdev)
Joseph Chan9f291632008-10-15 22:03:29 -0700264{
Harald Welte277d32a2009-05-23 00:35:39 +0800265 int i;
Joseph Chan9f291632008-10-15 22:03:29 -0700266
Jonathan Corbetf045f772009-12-01 20:29:39 -0700267 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
268 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
269 /*
270 * Only remove those entries in the array that we've
271 * actually used (and thus initialized algo_data)
272 */
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700273 if (i2c_stuff->is_active)
Harald Welte277d32a2009-05-23 00:35:39 +0800274 i2c_del_adapter(&i2c_stuff->adapter);
275 }
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600276 return 0;
277}
278
279static struct platform_driver via_i2c_driver = {
280 .driver = {
281 .name = "viafb-i2c",
282 },
283 .probe = viafb_i2c_probe,
284 .remove = viafb_i2c_remove,
285};
286
287int viafb_i2c_init(void)
288{
289 return platform_driver_register(&via_i2c_driver);
290}
291
292void viafb_i2c_exit(void)
293{
294 platform_driver_unregister(&via_i2c_driver);
Joseph Chan9f291632008-10-15 22:03:29 -0700295}