blob: 84ec2d6e98c4f1a77bd2a6d12cfb2a3ba99a6c26 [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 Corbet24b4d822010-04-22 13:48:09 -060023#include "via-core.h"
24#include "via_i2c.h"
Joseph Chan9f291632008-10-15 22:03:29 -070025#include "global.h"
26
Jonathan Corbetf045f772009-12-01 20:29:39 -070027/*
28 * There can only be one set of these, so there's no point in having
29 * them be dynamically allocated...
30 */
31#define VIAFB_NUM_I2C 5
32static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
Jonathan Corbet75b035a2010-04-22 14:36:04 -060033struct viafb_dev *i2c_vdev; /* Passed in from core */
Jonathan Corbetf045f772009-12-01 20:29:39 -070034
Joseph Chan9f291632008-10-15 22:03:29 -070035static void via_i2c_setscl(void *data, int state)
36{
37 u8 val;
Jonathan Corbetf045f772009-12-01 20:29:39 -070038 struct via_port_cfg *adap_data = data;
Jonathan Corbet75b035a2010-04-22 14:36:04 -060039 unsigned long flags;
Joseph Chan9f291632008-10-15 22:03:29 -070040
Jonathan Corbet75b035a2010-04-22 14:36:04 -060041 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
42 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -070043 if (state)
44 val |= 0x20;
45 else
46 val &= ~0x20;
Harald Welte277d32a2009-05-23 00:35:39 +080047 switch (adap_data->type) {
Jonathan Corbetf045f772009-12-01 20:29:39 -070048 case VIA_PORT_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -070049 val |= 0x01;
50 break;
Jonathan Corbetf045f772009-12-01 20:29:39 -070051 case VIA_PORT_GPIO:
Joseph Chan9f291632008-10-15 22:03:29 -070052 val |= 0x80;
53 break;
54 default:
Jonathan Corbet4a28ea92010-05-05 14:28:53 -060055 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -070056 }
Jonathan Corbet75b035a2010-04-22 14:36:04 -060057 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
58 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
Joseph Chan9f291632008-10-15 22:03:29 -070059}
60
61static int via_i2c_getscl(void *data)
62{
Jonathan Corbetf045f772009-12-01 20:29:39 -070063 struct via_port_cfg *adap_data = data;
Jonathan Corbet75b035a2010-04-22 14:36:04 -060064 unsigned long flags;
65 int ret = 0;
Joseph Chan9f291632008-10-15 22:03:29 -070066
Jonathan Corbet75b035a2010-04-22 14:36:04 -060067 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
68 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
69 ret = 1;
70 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
71 return ret;
Joseph Chan9f291632008-10-15 22:03:29 -070072}
73
74static int via_i2c_getsda(void *data)
75{
Jonathan Corbetf045f772009-12-01 20:29:39 -070076 struct via_port_cfg *adap_data = data;
Jonathan Corbet75b035a2010-04-22 14:36:04 -060077 unsigned long flags;
78 int ret = 0;
Joseph Chan9f291632008-10-15 22:03:29 -070079
Jonathan Corbet75b035a2010-04-22 14:36:04 -060080 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
81 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
82 ret = 1;
83 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
84 return ret;
Joseph Chan9f291632008-10-15 22:03:29 -070085}
86
87static void via_i2c_setsda(void *data, int state)
88{
89 u8 val;
Jonathan Corbetf045f772009-12-01 20:29:39 -070090 struct via_port_cfg *adap_data = data;
Jonathan Corbet75b035a2010-04-22 14:36:04 -060091 unsigned long flags;
Joseph Chan9f291632008-10-15 22:03:29 -070092
Jonathan Corbet75b035a2010-04-22 14:36:04 -060093 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
94 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -070095 if (state)
96 val |= 0x10;
97 else
98 val &= ~0x10;
Harald Welte277d32a2009-05-23 00:35:39 +080099 switch (adap_data->type) {
Jonathan Corbetf045f772009-12-01 20:29:39 -0700100 case VIA_PORT_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -0700101 val |= 0x01;
102 break;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700103 case VIA_PORT_GPIO:
Joseph Chan9f291632008-10-15 22:03:29 -0700104 val |= 0x40;
105 break;
106 default:
Jonathan Corbet4a28ea92010-05-05 14:28:53 -0600107 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -0700108 }
Jonathan Corbet75b035a2010-04-22 14:36:04 -0600109 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
110 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
Joseph Chan9f291632008-10-15 22:03:29 -0700111}
112
Harald Welte277d32a2009-05-23 00:35:39 +0800113int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
Joseph Chan9f291632008-10-15 22:03:29 -0700114{
115 u8 mm1[] = {0x00};
116 struct i2c_msg msgs[2];
117
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700118 if (!via_i2c_par[adap].is_active)
119 return -ENODEV;
Joseph Chan9f291632008-10-15 22:03:29 -0700120 *pdata = 0;
121 msgs[0].flags = 0;
122 msgs[1].flags = I2C_M_RD;
123 msgs[0].addr = msgs[1].addr = slave_addr / 2;
124 mm1[0] = index;
125 msgs[0].len = 1; msgs[1].len = 1;
126 msgs[0].buf = mm1; msgs[1].buf = pdata;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700127 return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
Joseph Chan9f291632008-10-15 22:03:29 -0700128}
129
Harald Welte277d32a2009-05-23 00:35:39 +0800130int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
Joseph Chan9f291632008-10-15 22:03:29 -0700131{
132 u8 msg[2] = { index, data };
133 struct i2c_msg msgs;
134
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700135 if (!via_i2c_par[adap].is_active)
136 return -ENODEV;
Joseph Chan9f291632008-10-15 22:03:29 -0700137 msgs.flags = 0;
138 msgs.addr = slave_addr / 2;
139 msgs.len = 2;
140 msgs.buf = msg;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700141 return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
Joseph Chan9f291632008-10-15 22:03:29 -0700142}
143
Harald Welte277d32a2009-05-23 00:35:39 +0800144int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
Joseph Chan9f291632008-10-15 22:03:29 -0700145{
146 u8 mm1[] = {0x00};
147 struct i2c_msg msgs[2];
148
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700149 if (!via_i2c_par[adap].is_active)
150 return -ENODEV;
Joseph Chan9f291632008-10-15 22:03:29 -0700151 msgs[0].flags = 0;
152 msgs[1].flags = I2C_M_RD;
153 msgs[0].addr = msgs[1].addr = slave_addr / 2;
154 mm1[0] = index;
155 msgs[0].len = 1; msgs[1].len = buff_len;
156 msgs[0].buf = mm1; msgs[1].buf = buff;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700157 return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
Harald Welte277d32a2009-05-23 00:35:39 +0800158}
159
Jonathan Corbetb8f7e5d2010-04-23 08:56:55 -0600160/*
161 * Allow other viafb subdevices to look up a specific adapter
162 * by port name.
163 */
164struct i2c_adapter *viafb_find_i2c_adapter(enum viafb_i2c_adap which)
165{
166 struct via_i2c_stuff *stuff = &via_i2c_par[which];
167
168 return &stuff->adapter;
169}
170EXPORT_SYMBOL_GPL(viafb_find_i2c_adapter);
171
172
Harald Welte277d32a2009-05-23 00:35:39 +0800173static int create_i2c_bus(struct i2c_adapter *adapter,
174 struct i2c_algo_bit_data *algo,
Jonathan Corbetf045f772009-12-01 20:29:39 -0700175 struct via_port_cfg *adap_cfg,
Harald Welte277d32a2009-05-23 00:35:39 +0800176 struct pci_dev *pdev)
177{
Harald Welte277d32a2009-05-23 00:35:39 +0800178 algo->setsda = via_i2c_setsda;
179 algo->setscl = via_i2c_setscl;
180 algo->getsda = via_i2c_getsda;
181 algo->getscl = via_i2c_getscl;
182 algo->udelay = 40;
183 algo->timeout = 20;
184 algo->data = adap_cfg;
185
186 sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
187 adap_cfg->ioport_index);
188 adapter->owner = THIS_MODULE;
189 adapter->id = 0x01FFFF;
190 adapter->class = I2C_CLASS_DDC;
191 adapter->algo_data = algo;
192 if (pdev)
193 adapter->dev.parent = &pdev->dev;
194 else
195 adapter->dev.parent = NULL;
196 /* i2c_set_adapdata(adapter, adap_cfg); */
197
198 /* Raise SCL and SDA */
199 via_i2c_setsda(adap_cfg, 1);
200 via_i2c_setscl(adap_cfg, 1);
201 udelay(20);
202
203 return i2c_bit_add_bus(adapter);
204}
205
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600206static int viafb_i2c_probe(struct platform_device *platdev)
Harald Welte277d32a2009-05-23 00:35:39 +0800207{
208 int i, ret;
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600209 struct via_port_cfg *configs;
Harald Welte277d32a2009-05-23 00:35:39 +0800210
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600211 i2c_vdev = platdev->dev.platform_data;
212 configs = i2c_vdev->port_cfg;
213
Jonathan Corbetf045f772009-12-01 20:29:39 -0700214 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
215 struct via_port_cfg *adap_cfg = configs++;
216 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
Harald Welte277d32a2009-05-23 00:35:39 +0800217
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700218 i2c_stuff->is_active = 0;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700219 if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
Jonathan Corbet4da62e62010-04-25 08:30:41 -0600220 continue;
Harald Welte277d32a2009-05-23 00:35:39 +0800221 ret = create_i2c_bus(&i2c_stuff->adapter,
222 &i2c_stuff->algo, adap_cfg,
223 NULL); /* FIXME: PCIDEV */
224 if (ret < 0) {
225 printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
226 i, ret);
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700227 continue; /* Still try to make the rest */
Harald Welte277d32a2009-05-23 00:35:39 +0800228 }
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700229 i2c_stuff->is_active = 1;
Harald Welte277d32a2009-05-23 00:35:39 +0800230 }
231
Joseph Chan9f291632008-10-15 22:03:29 -0700232 return 0;
233}
234
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600235static int viafb_i2c_remove(struct platform_device *platdev)
Joseph Chan9f291632008-10-15 22:03:29 -0700236{
Harald Welte277d32a2009-05-23 00:35:39 +0800237 int i;
Joseph Chan9f291632008-10-15 22:03:29 -0700238
Jonathan Corbetf045f772009-12-01 20:29:39 -0700239 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
240 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
241 /*
242 * Only remove those entries in the array that we've
243 * actually used (and thus initialized algo_data)
244 */
Jonathan Corbetb052d7f2009-12-28 10:04:02 -0700245 if (i2c_stuff->is_active)
Harald Welte277d32a2009-05-23 00:35:39 +0800246 i2c_del_adapter(&i2c_stuff->adapter);
247 }
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600248 return 0;
249}
250
251static struct platform_driver via_i2c_driver = {
252 .driver = {
253 .name = "viafb-i2c",
254 },
255 .probe = viafb_i2c_probe,
256 .remove = viafb_i2c_remove,
257};
258
259int viafb_i2c_init(void)
260{
261 return platform_driver_register(&via_i2c_driver);
262}
263
264void viafb_i2c_exit(void)
265{
266 platform_driver_unregister(&via_i2c_driver);
Joseph Chan9f291632008-10-15 22:03:29 -0700267}