i2c-powermac.c 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
    i2c Support for Apple SMU Controller

    Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp.
                       <benh@kernel.crashing.org>

    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 of the License, or
    (at your option) any later version.

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

*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/i2c.h>
#include <linux/device.h>
#include <linux/platform_device.h>
25
#include <linux/of_irq.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <asm/prom.h>
#include <asm/pmac_low_i2c.h>

MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
MODULE_DESCRIPTION("I2C driver for Apple PowerMac");
MODULE_LICENSE("GPL");

/*
 * SMBUS-type transfer entrypoint
 */
static s32 i2c_powermac_smbus_xfer(	struct i2c_adapter*	adap,
					u16			addr,
					unsigned short		flags,
					char			read_write,
					u8			command,
					int			size,
					union i2c_smbus_data*	data)
{
	struct pmac_i2c_bus	*bus = i2c_get_adapdata(adap);
	int			rc = 0;
	int			read = (read_write == I2C_SMBUS_READ);
	int			addrdir = (addr << 1) | read;
48 49 50
	int			mode, subsize, len;
	u32			subaddr;
	u8			*buf;
51 52
	u8			local[2];

53 54 55 56 57 58 59 60 61
	if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) {
		mode = pmac_i2c_mode_std;
		subsize = 0;
		subaddr = 0;
	} else {
		mode = read ? pmac_i2c_mode_combined : pmac_i2c_mode_stdsub;
		subsize = 1;
		subaddr = command;
	}
62 63 64

	switch (size) {
        case I2C_SMBUS_QUICK:
65 66
		buf = NULL;
		len = 0;
67 68 69
	    	break;
        case I2C_SMBUS_BYTE:
        case I2C_SMBUS_BYTE_DATA:
70 71
		buf = &data->byte;
		len = 1;
72 73 74 75 76 77
	    	break;
        case I2C_SMBUS_WORD_DATA:
		if (!read) {
			local[0] = data->word & 0xff;
			local[1] = (data->word >> 8) & 0xff;
		}
78 79
		buf = local;
		len = 2;
80 81 82
	    	break;

	/* Note that these are broken vs. the expected smbus API where
Joe Perches's avatar
Joe Perches committed
83
	 * on reads, the length is actually returned from the function,
84 85 86 87 88 89 90 91 92 93
	 * but I think the current API makes no sense and I don't want
	 * any driver that I haven't verified for correctness to go
	 * anywhere near a pmac i2c bus anyway ...
	 *
	 * I'm also not completely sure what kind of phases to do between
	 * the actual command and the data (what I am _supposed_ to do that
	 * is). For now, I assume writes are a single stream and reads have
	 * a repeat start/addr phase (but not stop in between)
	 */
        case I2C_SMBUS_BLOCK_DATA:
94 95
		buf = data->block;
		len = data->block[0] + 1;
96 97
		break;
	case I2C_SMBUS_I2C_BLOCK_DATA:
98 99
		buf = &data->block[1];
		len = data->block[0];
100 101 102
		break;

        default:
103
		return -EINVAL;
104
	}
105 106

	rc = pmac_i2c_open(bus, 0);
Jean Delvare's avatar
Jean Delvare committed
107 108
	if (rc) {
		dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
109
		return rc;
Jean Delvare's avatar
Jean Delvare committed
110
	}
111 112

	rc = pmac_i2c_setmode(bus, mode);
Jean Delvare's avatar
Jean Delvare committed
113 114 115
	if (rc) {
		dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
			mode, rc);
116
		goto bail;
Jean Delvare's avatar
Jean Delvare committed
117
	}
118 119

	rc = pmac_i2c_xfer(bus, addrdir, subsize, subaddr, buf, len);
Jean Delvare's avatar
Jean Delvare committed
120
	if (rc) {
121 122 123 124 125 126 127 128
		if (rc == -ENXIO)
			dev_dbg(&adap->dev,
				"I2C transfer at 0x%02x failed, size %d, "
				"err %d\n", addrdir >> 1, size, rc);
		else
			dev_err(&adap->dev,
				"I2C transfer at 0x%02x failed, size %d, "
				"err %d\n", addrdir >> 1, size, rc);
129
		goto bail;
Jean Delvare's avatar
Jean Delvare committed
130
	}
131 132 133 134 135 136

	if (size == I2C_SMBUS_WORD_DATA && read) {
		data->word = ((u16)local[1]) << 8;
		data->word |= local[0];
	}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
 bail:
	pmac_i2c_close(bus);
	return rc;
}

/*
 * Generic i2c master transfer entrypoint. This driver only support single
 * messages (for "lame i2c" transfers). Anything else should use the smbus
 * entry point
 */
static int i2c_powermac_master_xfer(	struct i2c_adapter *adap,
					struct i2c_msg *msgs,
					int num)
{
	struct pmac_i2c_bus	*bus = i2c_get_adapdata(adap);
	int			rc = 0;
	int			read;
	int			addrdir;

	if (msgs->flags & I2C_M_TEN)
		return -EINVAL;
	read = (msgs->flags & I2C_M_RD) != 0;
	addrdir = (msgs->addr << 1) | read;

	rc = pmac_i2c_open(bus, 0);
Jean Delvare's avatar
Jean Delvare committed
162 163
	if (rc) {
		dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
164
		return rc;
Jean Delvare's avatar
Jean Delvare committed
165
	}
166
	rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
Jean Delvare's avatar
Jean Delvare committed
167 168 169
	if (rc) {
		dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
			pmac_i2c_mode_std, rc);
170
		goto bail;
Jean Delvare's avatar
Jean Delvare committed
171
	}
172
	rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
173 174 175 176 177 178 179 180 181 182
	if (rc < 0) {
		if (rc == -ENXIO)
			dev_dbg(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
				addrdir & 1 ? "read from" : "write to",
				addrdir >> 1, rc);
		else
			dev_err(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
				addrdir & 1 ? "read from" : "write to",
				addrdir >> 1, rc);
	}
183 184
 bail:
	pmac_i2c_close(bus);
185
	return rc < 0 ? rc : 1;
186 187 188 189 190 191 192 193 194 195
}

static u32 i2c_powermac_func(struct i2c_adapter * adapter)
{
	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
		I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
		I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
}

/* For now, we only handle smbus */
196
static const struct i2c_algorithm i2c_powermac_algorithm = {
197 198 199 200 201
	.smbus_xfer	= i2c_powermac_smbus_xfer,
	.master_xfer	= i2c_powermac_master_xfer,
	.functionality	= i2c_powermac_func,
};

202 203 204
static struct i2c_adapter_quirks i2c_powermac_quirks = {
	.max_num_msgs = 1,
};
205

206
static int i2c_powermac_remove(struct platform_device *dev)
207
{
208
	struct i2c_adapter	*adapter = platform_get_drvdata(dev);
209 210

	i2c_del_adapter(adapter);
211
	memset(adapter, 0, sizeof(*adapter));
212 213 214 215

	return 0;
}

216
static u32 i2c_powermac_get_addr(struct i2c_adapter *adap,
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
					   struct pmac_i2c_bus *bus,
					   struct device_node *node)
{
	const __be32 *prop;
	int len;

	/* First check for valid "reg" */
	prop = of_get_property(node, "reg", &len);
	if (prop && (len >= sizeof(int)))
		return (be32_to_cpup(prop) & 0xff) >> 1;

	/* Then check old-style "i2c-address" */
	prop = of_get_property(node, "i2c-address", &len);
	if (prop && (len >= sizeof(int)))
		return (be32_to_cpup(prop) & 0xff) >> 1;

	/* Now handle some devices with missing "reg" properties */
	if (!strcmp(node->name, "cereal"))
		return 0x60;
	else if (!strcmp(node->name, "deq"))
		return 0x34;

	dev_warn(&adap->dev, "No i2c address for %s\n", node->full_name);

	return 0xffffffff;
}

244
static void i2c_powermac_create_one(struct i2c_adapter *adap,
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
					      const char *type,
					      u32 addr)
{
	struct i2c_board_info info = {};
	struct i2c_client *newdev;

	strncpy(info.type, type, sizeof(info.type));
	info.addr = addr;
	newdev = i2c_new_device(adap, &info);
	if (!newdev)
		dev_err(&adap->dev,
			"i2c-powermac: Failure to register missing %s\n",
			type);
}

260
static void i2c_powermac_add_missing(struct i2c_adapter *adap,
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
					       struct pmac_i2c_bus *bus,
					       bool found_onyx)
{
	struct device_node *busnode = pmac_i2c_get_bus_node(bus);
	int rc;

	/* Check for the onyx audio codec */
#define ONYX_REG_CONTROL		67
	if (of_device_is_compatible(busnode, "k2-i2c") && !found_onyx) {
		union i2c_smbus_data data;

		rc = i2c_smbus_xfer(adap, 0x46, 0, I2C_SMBUS_READ,
				    ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
				    &data);
		if (rc >= 0)
			i2c_powermac_create_one(adap, "MAC,pcm3052", 0x46);

		rc = i2c_smbus_xfer(adap, 0x47, 0, I2C_SMBUS_READ,
				    ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
				    &data);
		if (rc >= 0)
			i2c_powermac_create_one(adap, "MAC,pcm3052", 0x47);
	}
}

286
static bool i2c_powermac_get_type(struct i2c_adapter *adap,
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
					    struct device_node *node,
					    u32 addr, char *type, int type_size)
{
	char tmp[16];

	/* Note: we to _NOT_ want the standard
	 * i2c drivers to match with any of our powermac stuff
	 * unless they have been specifically modified to handle
	 * it on a case by case basis. For example, for thermal
	 * control, things like lm75 etc... shall match with their
	 * corresponding windfarm drivers, _NOT_ the generic ones,
	 * so we force a prefix of AAPL, onto the modalias to
	 * make that happen
	 */

	/* First try proper modalias */
	if (of_modalias_node(node, tmp, sizeof(tmp)) >= 0) {
		snprintf(type, type_size, "MAC,%s", tmp);
		return true;
	}

	/* Now look for known workarounds */
	if (!strcmp(node->name, "deq")) {
		/* Apple uses address 0x34 for TAS3001 and 0x35 for TAS3004 */
		if (addr == 0x34) {
			snprintf(type, type_size, "MAC,tas3001");
			return true;
		} else if (addr == 0x35) {
			snprintf(type, type_size, "MAC,tas3004");
			return true;
		}
	}

	dev_err(&adap->dev, "i2c-powermac: modalias failure"
		" on %s\n", node->full_name);
	return false;
}

325
static void i2c_powermac_register_devices(struct i2c_adapter *adap,
326 327 328 329
						    struct pmac_i2c_bus *bus)
{
	struct i2c_client *newdev;
	struct device_node *node;
330 331 332 333 334 335 336 337 338
	bool found_onyx = 0;

	/*
	 * In some cases we end up with the via-pmu node itself, in this
	 * case we skip this function completely as the device-tree will
	 * not contain anything useful.
	 */
	if (!strcmp(adap->dev.of_node->name, "via-pmu"))
		return;
339 340 341 342 343 344

	for_each_child_of_node(adap->dev.of_node, node) {
		struct i2c_board_info info = {};
		u32 addr;

		/* Get address & channel */
345 346
		addr = i2c_powermac_get_addr(adap, bus, node);
		if (addr == 0xffffffff)
347 348 349 350 351 352 353 354 355
			continue;

		/* Multibus setup, check channel */
		if (!pmac_i2c_match_adapter(node, adap))
			continue;

		dev_dbg(&adap->dev, "i2c-powermac: register %s\n",
			node->full_name);

356 357 358
		/*
		 * Keep track of some device existence to handle
		 * workarounds later.
359
		 */
360 361 362 363 364 365
		if (of_device_is_compatible(node, "pcm3052"))
			found_onyx = true;

		/* Make up a modalias */
		if (!i2c_powermac_get_type(adap, node, addr,
					   info.type, sizeof(info.type))) {
366 367 368 369
			continue;
		}

		/* Fill out the rest of the info structure */
370
		info.addr = addr;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
		info.irq = irq_of_parse_and_map(node, 0);
		info.of_node = of_node_get(node);

		newdev = i2c_new_device(adap, &info);
		if (!newdev) {
			dev_err(&adap->dev, "i2c-powermac: Failure to register"
				" %s\n", node->full_name);
			of_node_put(node);
			/* We do not dispose of the interrupt mapping on
			 * purpose. It's not necessary (interrupt cannot be
			 * re-used) and somebody else might have grabbed it
			 * via direct DT lookup so let's not bother
			 */
			continue;
		}
	}
387 388 389

	/* Additional workarounds */
	i2c_powermac_add_missing(adap, bus, found_onyx);
390
}
391

392
static int i2c_powermac_probe(struct platform_device *dev)
393
{
Jingoo Han's avatar
Jingoo Han committed
394
	struct pmac_i2c_bus *bus = dev_get_platdata(&dev->dev);
395 396
	struct device_node *parent = NULL;
	struct i2c_adapter *adapter;
397
	const char *basename;
398 399 400 401
	int rc;

	if (bus == NULL)
		return -EINVAL;
402
	adapter = pmac_i2c_get_adapter(bus);
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

	/* Ok, now we need to make up a name for the interface that will
	 * match what we used to do in the past, that is basically the
	 * controller's parent device node for keywest. PMU didn't have a
	 * naming convention and SMU has a different one
	 */
	switch(pmac_i2c_get_type(bus)) {
	case pmac_i2c_bus_keywest:
		parent = of_get_parent(pmac_i2c_get_controller(bus));
		if (parent == NULL)
			return -EINVAL;
		basename = parent->name;
		break;
	case pmac_i2c_bus_pmu:
		basename = "pmu";
		break;
	case pmac_i2c_bus_smu:
		/* This is not what we used to do but I'm fixing drivers at
		 * the same time as this change
		 */
		basename = "smu";
		break;
	default:
		return -EINVAL;
	}
428 429
	snprintf(adapter->name, sizeof(adapter->name), "%s %d", basename,
		 pmac_i2c_get_channel(bus));
430 431
	of_node_put(parent);

432
	platform_set_drvdata(dev, adapter);
433
	adapter->algo = &i2c_powermac_algorithm;
434
	adapter->quirks = &i2c_powermac_quirks;
435
	i2c_set_adapdata(adapter, bus);
436
	adapter->dev.parent = &dev->dev;
437 438 439

	/* Clear of_node to skip automatic registration of i2c child nodes */
	adapter->dev.of_node = NULL;
440 441 442
	rc = i2c_add_adapter(adapter);
	if (rc) {
		printk(KERN_ERR "i2c-powermac: Adapter %s registration "
443
		       "failed\n", adapter->name);
444
		memset(adapter, 0, sizeof(*adapter));
445
		return rc;
446 447
	}

448
	printk(KERN_INFO "PowerMac i2c bus %s registered\n", adapter->name);
449

450 451
	/* Use custom child registration due to Apple device-tree funkyness */
	adapter->dev.of_node = dev->dev.of_node;
452
	i2c_powermac_register_devices(adapter, bus);
453

454
	return 0;
455 456
}

457
static struct platform_driver i2c_powermac_driver = {
458
	.probe = i2c_powermac_probe,
459
	.remove = i2c_powermac_remove,
460 461 462 463
	.driver = {
		.name = "i2c-powermac",
		.bus = &platform_bus_type,
	},
464 465
};

466
module_platform_driver(i2c_powermac_driver);
467

468
MODULE_ALIAS("platform:i2c-powermac");