via_i2c.c 6.2 KB
Newer Older
1
/*
2
 * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation;
 * either version 2, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTIES OR REPRESENTATIONS; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.See the GNU General Public License
 * for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

22
#include <linux/platform_device.h>
23 24
#include "via-core.h"
#include "via_i2c.h"
25 26
#include "global.h"

27 28 29 30 31 32
/*
 * There can only be one set of these, so there's no point in having
 * them be dynamically allocated...
 */
#define VIAFB_NUM_I2C		5
static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
33
struct viafb_dev *i2c_vdev;  /* Passed in from core */
34

35 36 37
static void via_i2c_setscl(void *data, int state)
{
	u8 val;
38
	struct via_port_cfg *adap_data = data;
39
	unsigned long flags;
40

41 42
	spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
	val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
43 44 45 46
	if (state)
		val |= 0x20;
	else
		val &= ~0x20;
47
	switch (adap_data->type) {
48
	case VIA_PORT_I2C:
49 50
		val |= 0x01;
		break;
51
	case VIA_PORT_GPIO:
52 53 54
		val |= 0x80;
		break;
	default:
55
		DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
56
	}
57 58
	via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
	spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
59 60 61 62
}

static int via_i2c_getscl(void *data)
{
63
	struct via_port_cfg *adap_data = data;
64 65 66 67 68 69 70 71
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
	if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
		ret = 1;
	spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
	return ret;
72 73 74 75
}

static int via_i2c_getsda(void *data)
{
76
	struct via_port_cfg *adap_data = data;
77 78 79 80 81 82 83 84
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
	if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
		ret = 1;
	spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
	return ret;
85 86 87 88 89
}

static void via_i2c_setsda(void *data, int state)
{
	u8 val;
90
	struct via_port_cfg *adap_data = data;
91
	unsigned long flags;
92

93 94
	spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
	val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
95 96 97 98
	if (state)
		val |= 0x10;
	else
		val &= ~0x10;
99
	switch (adap_data->type) {
100
	case VIA_PORT_I2C:
101 102
		val |= 0x01;
		break;
103
	case VIA_PORT_GPIO:
104 105 106
		val |= 0x40;
		break;
	default:
107
		DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
108
	}
109 110
	via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
	spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
111 112
}

113
int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
114 115 116 117 118 119 120 121 122 123 124
{
	u8 mm1[] = {0x00};
	struct i2c_msg msgs[2];

	*pdata = 0;
	msgs[0].flags = 0;
	msgs[1].flags = I2C_M_RD;
	msgs[0].addr = msgs[1].addr = slave_addr / 2;
	mm1[0] = index;
	msgs[0].len = 1; msgs[1].len = 1;
	msgs[0].buf = mm1; msgs[1].buf = pdata;
125
	return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
126 127
}

128
int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
129 130 131 132 133 134 135 136
{
	u8 msg[2] = { index, data };
	struct i2c_msg msgs;

	msgs.flags = 0;
	msgs.addr = slave_addr / 2;
	msgs.len = 2;
	msgs.buf = msg;
137
	return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
138 139
}

140
int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
141 142 143 144 145 146 147 148 149 150
{
	u8 mm1[] = {0x00};
	struct i2c_msg msgs[2];

	msgs[0].flags = 0;
	msgs[1].flags = I2C_M_RD;
	msgs[0].addr = msgs[1].addr = slave_addr / 2;
	mm1[0] = index;
	msgs[0].len = 1; msgs[1].len = buff_len;
	msgs[0].buf = mm1; msgs[1].buf = buff;
151
	return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
152 153
}

154 155
static int create_i2c_bus(struct i2c_adapter *adapter,
			  struct i2c_algo_bit_data *algo,
156
			  struct via_port_cfg *adap_cfg,
157
			  struct pci_dev *pdev)
158
{
159
	DEBUG_MSG(KERN_DEBUG "viafb: creating bus adap=0x%p, algo_bit_data=0x%p, adap_cfg=0x%p\n", adapter, algo, adap_cfg);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

	algo->setsda = via_i2c_setsda;
	algo->setscl = via_i2c_setscl;
	algo->getsda = via_i2c_getsda;
	algo->getscl = via_i2c_getscl;
	algo->udelay = 40;
	algo->timeout = 20;
	algo->data = adap_cfg;

	sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
		adap_cfg->ioport_index);
	adapter->owner = THIS_MODULE;
	adapter->id = 0x01FFFF;
	adapter->class = I2C_CLASS_DDC;
	adapter->algo_data = algo;
	if (pdev)
		adapter->dev.parent = &pdev->dev;
	else
		adapter->dev.parent = NULL;
	/* i2c_set_adapdata(adapter, adap_cfg); */
180 181

	/* Raise SCL and SDA */
182 183
	via_i2c_setsda(adap_cfg, 1);
	via_i2c_setscl(adap_cfg, 1);
184 185
	udelay(20);

186 187 188
	return i2c_bit_add_bus(adapter);
}

189
static int viafb_i2c_probe(struct platform_device *platdev)
190 191
{
	int i, ret;
192 193 194 195
	struct via_port_cfg *configs;

	i2c_vdev = platdev->dev.platform_data;
	configs = i2c_vdev->port_cfg;
196

197 198 199
	for (i = 0; i < VIAFB_NUM_PORTS; i++) {
		struct via_port_cfg *adap_cfg = configs++;
		struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
200

201
		if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
202
			continue;
203 204 205 206 207 208 209 210 211 212 213 214 215

		ret = create_i2c_bus(&i2c_stuff->adapter,
				     &i2c_stuff->algo, adap_cfg,
				NULL); /* FIXME: PCIDEV */
		if (ret < 0) {
			printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
				i, ret);
			/* FIXME: properly release previous busses */
			return ret;
		}
	}

	return 0;
216 217
}

218
static int viafb_i2c_remove(struct platform_device *platdev)
219
{
220 221
	int i;

222 223 224 225 226 227
	for (i = 0; i < VIAFB_NUM_PORTS; i++) {
		struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
		/*
		 * Only remove those entries in the array that we've
		 * actually used (and thus initialized algo_data)
		 */
228 229 230
		if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
			i2c_del_adapter(&i2c_stuff->adapter);
	}
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	return 0;
}

static struct platform_driver via_i2c_driver = {
	.driver = {
		.name = "viafb-i2c",
	},
	.probe = viafb_i2c_probe,
	.remove = viafb_i2c_remove,
};

int viafb_i2c_init(void)
{
	return platform_driver_register(&via_i2c_driver);
}

void viafb_i2c_exit(void)
{
	platform_driver_unregister(&via_i2c_driver);
250
}