low_i2c.c 36.5 KB
Newer Older
1
/*
2
 * arch/powerpc/platforms/powermac/low_i2c.c
3
 *
4
 *  Copyright (C) 2003-2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
5 6 7 8 9 10
 *
 *  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.
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * The linux i2c layer isn't completely suitable for our needs for various
 * reasons ranging from too late initialisation to semantics not perfectly
 * matching some requirements of the apple platform functions etc...
 *
 * This file thus provides a simple low level unified i2c interface for
 * powermac that covers the various types of i2c busses used in Apple machines.
 * For now, keywest, PMU and SMU, though we could add Cuda, or other bit
 * banging busses found on older chipstes in earlier machines if we ever need
 * one of them.
 *
 * The drivers in this file are synchronous/blocking. In addition, the
 * keywest one is fairly slow due to the use of msleep instead of interrupts
 * as the interrupt is currently used by i2c-keywest. In the long run, we
 * might want to get rid of those high-level interfaces to linux i2c layer
 * either completely (converting all drivers) or replacing them all with a
 * single stub driver on top of this one. Once done, the interrupt will be
 * available for our use.
28 29 30
 */

#undef DEBUG
31
#undef DEBUG_LOW
32 33 34 35

#include <linux/types.h>
#include <linux/sched.h>
#include <linux/init.h>
36
#include <linux/export.h>
37 38
#include <linux/adb.h>
#include <linux/pmu.h>
39 40
#include <linux/delay.h>
#include <linux/completion.h>
41 42 43
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/timer.h>
44
#include <linux/mutex.h>
45
#include <linux/i2c.h>
46
#include <linux/slab.h>
47 48 49 50 51
#include <asm/keylargo.h>
#include <asm/uninorth.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/machdep.h>
52
#include <asm/smu.h>
53
#include <asm/pmac_pfunc.h>
54 55 56 57
#include <asm/pmac_low_i2c.h>

#ifdef DEBUG
#define DBG(x...) do {\
58
		printk(KERN_DEBUG "low_i2c:" x);	\
59 60 61 62 63
	} while(0)
#else
#define DBG(x...)
#endif

64 65 66 67 68 69 70
#ifdef DEBUG_LOW
#define DBG_LOW(x...) do {\
		printk(KERN_DEBUG "low_i2c:" x);	\
	} while(0)
#else
#define DBG_LOW(x...)
#endif
71

72 73 74

static int pmac_i2c_force_poll = 1;

75 76
/*
 * A bus structure. Each bus in the system has such a structure associated.
77
 */
78
struct pmac_i2c_bus
79
{
80 81 82 83 84
	struct list_head	link;
	struct device_node	*controller;
	struct device_node	*busnode;
	int			type;
	int			flags;
85
	struct i2c_adapter	adapter;
86 87 88
	void			*hostdata;
	int			channel;	/* some hosts have multiple */
	int			mode;		/* current mode */
89
	struct mutex		mutex;
90 91
	int			opened;
	int			polled;		/* open mode */
92
	struct platform_device	*platform_dev;
93 94 95 96 97 98 99

	/* ops */
	int (*open)(struct pmac_i2c_bus *bus);
	void (*close)(struct pmac_i2c_bus *bus);
	int (*xfer)(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
		    u32 subaddr, u8 *data, int len);
};
100

101
static LIST_HEAD(pmac_i2c_busses);
102 103

/*
104
 * Keywest implementation
105 106
 */

107 108
struct pmac_i2c_host_kw
{
109
	struct mutex		mutex;		/* Access mutex for use by
110 111 112 113
						 * i2c-keywest */
	void __iomem		*base;		/* register base address */
	int			bsteps;		/* register stepping */
	int			speed;		/* speed */
114 115 116 117 118 119 120 121 122 123
	int			irq;
	u8			*data;
	unsigned		len;
	int			state;
	int			rw;
	int			polled;
	int			result;
	struct completion	complete;
	spinlock_t		lock;
	struct timer_list	timeout_timer;
124 125
};

126 127 128 129 130 131 132 133 134 135 136 137
/* Register indices */
typedef enum {
	reg_mode = 0,
	reg_control,
	reg_status,
	reg_isr,
	reg_ier,
	reg_addr,
	reg_subaddr,
	reg_data
} reg_t;

138 139
/* The Tumbler audio equalizer can be really slow sometimes */
#define KW_POLL_TIMEOUT		(2*HZ)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

/* Mode register */
#define KW_I2C_MODE_100KHZ	0x00
#define KW_I2C_MODE_50KHZ	0x01
#define KW_I2C_MODE_25KHZ	0x02
#define KW_I2C_MODE_DUMB	0x00
#define KW_I2C_MODE_STANDARD	0x04
#define KW_I2C_MODE_STANDARDSUB	0x08
#define KW_I2C_MODE_COMBINED	0x0C
#define KW_I2C_MODE_MODE_MASK	0x0C
#define KW_I2C_MODE_CHAN_MASK	0xF0

/* Control register */
#define KW_I2C_CTL_AAK		0x01
#define KW_I2C_CTL_XADDR	0x02
#define KW_I2C_CTL_STOP		0x04
#define KW_I2C_CTL_START	0x08

/* Status register */
#define KW_I2C_STAT_BUSY	0x01
#define KW_I2C_STAT_LAST_AAK	0x02
#define KW_I2C_STAT_LAST_RW	0x04
#define KW_I2C_STAT_SDA		0x08
#define KW_I2C_STAT_SCL		0x10

/* IER & ISR registers */
#define KW_I2C_IRQ_DATA		0x01
#define KW_I2C_IRQ_ADDR		0x02
#define KW_I2C_IRQ_STOP		0x04
#define KW_I2C_IRQ_START	0x08
#define KW_I2C_IRQ_MASK		0x0F

/* State machine states */
enum {
	state_idle,
	state_addr,
	state_read,
	state_write,
	state_stop,
	state_dead
};

#define WRONG_STATE(name) do {\
183 184 185
		printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s " \
		       "(isr: %02x)\n",	\
		       name, __kw_state_names[host->state], isr); \
186 187 188 189 190 191 192 193 194 195 196
	} while(0)

static const char *__kw_state_names[] = {
	"state_idle",
	"state_addr",
	"state_read",
	"state_write",
	"state_stop",
	"state_dead"
};

197
static inline u8 __kw_read_reg(struct pmac_i2c_host_kw *host, reg_t reg)
198 199 200 201
{
	return readb(host->base + (((unsigned int)reg) << host->bsteps));
}

202 203
static inline void __kw_write_reg(struct pmac_i2c_host_kw *host,
				  reg_t reg, u8 val)
204 205
{
	writeb(val, host->base + (((unsigned)reg) << host->bsteps));
206
	(void)__kw_read_reg(host, reg_subaddr);
207 208
}

209 210
#define kw_write_reg(reg, val)	__kw_write_reg(host, reg, val)
#define kw_read_reg(reg)	__kw_read_reg(host, reg)
211

212
static u8 kw_i2c_wait_interrupt(struct pmac_i2c_host_kw *host)
213 214 215 216
{
	int i, j;
	u8 isr;
	
217
	for (i = 0; i < 1000; i++) {
218 219 220 221 222
		isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;
		if (isr != 0)
			return isr;

		/* This code is used with the timebase frozen, we cannot rely
223 224
		 * on udelay nor schedule when in polled mode !
		 * For now, just use a bogus loop....
225
		 */
226 227
		if (host->polled) {
			for (j = 1; j < 100000; j++)
228 229 230
				mb();
		} else
			msleep(1);
231 232 233 234
	}
	return isr;
}

235 236 237 238 239 240 241 242
static void kw_i2c_do_stop(struct pmac_i2c_host_kw *host, int result)
{
	kw_write_reg(reg_control, KW_I2C_CTL_STOP);
	host->state = state_stop;
	host->result = result;
}


243
static void kw_i2c_handle_interrupt(struct pmac_i2c_host_kw *host, u8 isr)
244 245 246
{
	u8 ack;

247
	DBG_LOW("kw_handle_interrupt(%s, isr: %x)\n",
248 249 250 251 252 253 254 255
		__kw_state_names[host->state], isr);

	if (host->state == state_idle) {
		printk(KERN_WARNING "low_i2c: Keywest got an out of state"
		       " interrupt, ignoring\n");
		kw_write_reg(reg_isr, isr);
		return;
	}
256 257

	if (isr == 0) {
258 259
		printk(KERN_WARNING "low_i2c: Timeout in i2c transfer"
		       " on keywest !\n");
260
		if (host->state != state_stop) {
261 262
			kw_i2c_do_stop(host, -EIO);
			return;
263
		}
264 265 266 267 268 269 270
		ack = kw_read_reg(reg_status);
		if (ack & KW_I2C_STAT_BUSY)
			kw_write_reg(reg_status, 0);
		host->state = state_idle;
		kw_write_reg(reg_ier, 0x00);
		if (!host->polled)
			complete(&host->complete);
271
		return;
272 273 274 275
	}

	if (isr & KW_I2C_IRQ_ADDR) {
		ack = kw_read_reg(reg_status);
276
		if (host->state != state_addr) {
277
			WRONG_STATE("KW_I2C_IRQ_ADDR"); 
278
			kw_i2c_do_stop(host, -EIO);
279
		}
280
		if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
281
			host->result = -ENXIO;
282
			host->state = state_stop;
283
			DBG_LOW("KW: NAK on address\n");
284
		} else {
285 286 287
			if (host->len == 0)
				kw_i2c_do_stop(host, 0);
			else if (host->rw) {
288 289
				host->state = state_read;
				if (host->len > 1)
290 291
					kw_write_reg(reg_control,
						     KW_I2C_CTL_AAK);
292
			} else {
293 294 295
				host->state = state_write;
				kw_write_reg(reg_data, *(host->data++));
				host->len--;
296 297 298 299 300 301
			}
		}
		kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
	}

	if (isr & KW_I2C_IRQ_DATA) {
302 303 304
		if (host->state == state_read) {
			*(host->data++) = kw_read_reg(reg_data);
			host->len--;
305
			kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
306 307 308
			if (host->len == 0)
				host->state = state_stop;
			else if (host->len == 1)
309
				kw_write_reg(reg_control, 0);
310
		} else if (host->state == state_write) {
311 312
			ack = kw_read_reg(reg_status);
			if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
313
				DBG_LOW("KW: nack on data write\n");
314 315
				host->result = -EFBIG;
				host->state = state_stop;
316 317 318
			} else if (host->len) {
				kw_write_reg(reg_data, *(host->data++));
				host->len--;
319 320
			} else
				kw_i2c_do_stop(host, 0);
321 322
		} else {
			WRONG_STATE("KW_I2C_IRQ_DATA"); 
323 324
			if (host->state != state_stop)
				kw_i2c_do_stop(host, -EIO);
325
		}
326
		kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
327 328 329 330
	}

	if (isr & KW_I2C_IRQ_STOP) {
		kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);
331
		if (host->state != state_stop) {
332
			WRONG_STATE("KW_I2C_IRQ_STOP");
333
			host->result = -EIO;
334
		}
335 336 337
		host->state = state_idle;
		if (!host->polled)
			complete(&host->complete);
338 339
	}

340
	/* Below should only happen in manual mode which we don't use ... */
341 342 343
	if (isr & KW_I2C_IRQ_START)
		kw_write_reg(reg_isr, KW_I2C_IRQ_START);

344 345 346
}

/* Interrupt handler */
347
static irqreturn_t kw_i2c_irq(int irq, void *dev_id)
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
{
	struct pmac_i2c_host_kw *host = dev_id;
	unsigned long flags;

	spin_lock_irqsave(&host->lock, flags);
	del_timer(&host->timeout_timer);
	kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));
	if (host->state != state_idle) {
		host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
		add_timer(&host->timeout_timer);
	}
	spin_unlock_irqrestore(&host->lock, flags);
	return IRQ_HANDLED;
}

static void kw_i2c_timeout(unsigned long data)
{
	struct pmac_i2c_host_kw *host = (struct pmac_i2c_host_kw *)data;
	unsigned long flags;

	spin_lock_irqsave(&host->lock, flags);
369 370 371 372 373 374 375 376

	/*
	 * If the timer is pending, that means we raced with the
	 * irq, in which case we just return
	 */
	if (timer_pending(&host->timeout_timer))
		goto skip;

377 378 379 380 381
	kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));
	if (host->state != state_idle) {
		host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
		add_timer(&host->timeout_timer);
	}
382
 skip:
383
	spin_unlock_irqrestore(&host->lock, flags);
384 385
}

386
static int kw_i2c_open(struct pmac_i2c_bus *bus)
387
{
388
	struct pmac_i2c_host_kw *host = bus->hostdata;
389
	mutex_lock(&host->mutex);
390 391 392 393 394 395
	return 0;
}

static void kw_i2c_close(struct pmac_i2c_bus *bus)
{
	struct pmac_i2c_host_kw *host = bus->hostdata;
396
	mutex_unlock(&host->mutex);
397 398 399 400 401 402
}

static int kw_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
		       u32 subaddr, u8 *data, int len)
{
	struct pmac_i2c_host_kw *host = bus->hostdata;
403
	u8 mode_reg = host->speed;
404
	int use_irq = host->irq != NO_IRQ && !bus->polled;
405 406

	/* Setup mode & subaddress if any */
407 408
	switch(bus->mode) {
	case pmac_i2c_mode_dumb:
409
		return -EINVAL;
410
	case pmac_i2c_mode_std:
411
		mode_reg |= KW_I2C_MODE_STANDARD;
412 413
		if (subsize != 0)
			return -EINVAL;
414
		break;
415
	case pmac_i2c_mode_stdsub:
416
		mode_reg |= KW_I2C_MODE_STANDARDSUB;
417 418
		if (subsize != 1)
			return -EINVAL;
419
		break;
420
	case pmac_i2c_mode_combined:
421
		mode_reg |= KW_I2C_MODE_COMBINED;
422 423
		if (subsize != 1)
			return -EINVAL;
424 425 426 427 428
		break;
	}

	/* Setup channel & clear pending irqs */
	kw_write_reg(reg_isr, kw_read_reg(reg_isr));
429
	kw_write_reg(reg_mode, mode_reg | (bus->channel << 4));
430 431
	kw_write_reg(reg_status, 0);

432 433 434 435
	/* Set up address and r/w bit, strip possible stale bus number from
	 * address top bits
	 */
	kw_write_reg(reg_addr, addrdir & 0xff);
436 437 438 439 440 441

	/* Set up the sub address */
	if ((mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
	    || (mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
		kw_write_reg(reg_subaddr, subaddr);

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	/* Prepare for async operations */
	host->data = data;
	host->len = len;
	host->state = state_addr;
	host->result = 0;
	host->rw = (addrdir & 1);
	host->polled = bus->polled;

	/* Enable interrupt if not using polled mode and interrupt is
	 * available
	 */
	if (use_irq) {
		/* Clear completion */
		INIT_COMPLETION(host->complete);
		/* Ack stale interrupts */
		kw_write_reg(reg_isr, kw_read_reg(reg_isr));
		/* Arm timeout */
		host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
		add_timer(&host->timeout_timer);
		/* Enable emission */
		kw_write_reg(reg_ier, KW_I2C_IRQ_MASK);
	}

	/* Start sending address */
466 467
	kw_write_reg(reg_control, KW_I2C_CTL_XADDR);

468 469 470 471 472 473 474 475 476 477 478 479
	/* Wait for completion */
	if (use_irq)
		wait_for_completion(&host->complete);
	else {
		while(host->state != state_idle) {
			unsigned long flags;

			u8 isr = kw_i2c_wait_interrupt(host);
			spin_lock_irqsave(&host->lock, flags);
			kw_i2c_handle_interrupt(host, isr);
			spin_unlock_irqrestore(&host->lock, flags);
		}
480 481
	}

482 483 484 485
	/* Disable emission */
	kw_write_reg(reg_ier, 0);

	return host->result;
486 487
}

488
static struct pmac_i2c_host_kw *__init kw_i2c_host_init(struct device_node *np)
489
{
490
	struct pmac_i2c_host_kw *host;
491 492
	const u32		*psteps, *prate, *addrp;
	u32			steps;
493

494
	host = kzalloc(sizeof(struct pmac_i2c_host_kw), GFP_KERNEL);
495 496 497
	if (host == NULL) {
		printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
		       np->full_name);
498
		return NULL;
499 500
	}

501 502 503 504
	/* Apple is kind enough to provide a valid AAPL,address property
	 * on all i2c keywest nodes so far ... we would have to fallback
	 * to macio parsing if that wasn't the case
	 */
505
	addrp = of_get_property(np, "AAPL,address", NULL);
506 507 508
	if (addrp == NULL) {
		printk(KERN_ERR "low_i2c: Can't find address for %s\n",
		       np->full_name);
509 510
		kfree(host);
		return NULL;
511
	}
512
	mutex_init(&host->mutex);
513 514 515 516 517 518
	init_completion(&host->complete);
	spin_lock_init(&host->lock);
	init_timer(&host->timeout_timer);
	host->timeout_timer.function = kw_i2c_timeout;
	host->timeout_timer.data = (unsigned long)host;

519
	psteps = of_get_property(np, "AAPL,address-step", NULL);
520 521 522 523
	steps = psteps ? (*psteps) : 0x10;
	for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)
		steps >>= 1;
	/* Select interface rate */
524
	host->speed = KW_I2C_MODE_25KHZ;
525
	prate = of_get_property(np, "AAPL,i2c-rate", NULL);
526 527 528 529 530 531 532 533 534 535 536
	if (prate) switch(*prate) {
	case 100:
		host->speed = KW_I2C_MODE_100KHZ;
		break;
	case 50:
		host->speed = KW_I2C_MODE_50KHZ;
		break;
	case 25:
		host->speed = KW_I2C_MODE_25KHZ;
		break;
	}	
537 538 539 540 541
	host->irq = irq_of_parse_and_map(np, 0);
	if (host->irq == NO_IRQ)
		printk(KERN_WARNING
		       "low_i2c: Failed to map interrupt for %s\n",
		       np->full_name);
542

543
	host->base = ioremap((*addrp), 0x1000);
544 545 546 547 548 549 550
	if (host->base == NULL) {
		printk(KERN_ERR "low_i2c: Can't map registers for %s\n",
		       np->full_name);
		kfree(host);
		return NULL;
	}

551
	/* Make sure IRQ is disabled */
552 553
	kw_write_reg(reg_ier, 0);

554
	/* Request chip interrupt. We set IRQF_NO_SUSPEND because we don't
555 556 557
	 * want that interrupt disabled between the 2 passes of driver
	 * suspend or we'll have issues running the pfuncs
	 */
558 559
	if (request_irq(host->irq, kw_i2c_irq, IRQF_NO_SUSPEND,
			"keywest i2c", host))
560 561 562 563
		host->irq = NO_IRQ;

	printk(KERN_INFO "KeyWest i2c @0x%08x irq %d %s\n",
	       *addrp, host->irq, np->full_name);
564 565

	return host;
566 567
}

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

static void __init kw_i2c_add(struct pmac_i2c_host_kw *host,
			      struct device_node *controller,
			      struct device_node *busnode,
			      int channel)
{
	struct pmac_i2c_bus *bus;

	bus = kzalloc(sizeof(struct pmac_i2c_bus), GFP_KERNEL);
	if (bus == NULL)
		return;

	bus->controller = of_node_get(controller);
	bus->busnode = of_node_get(busnode);
	bus->type = pmac_i2c_bus_keywest;
	bus->hostdata = host;
	bus->channel = channel;
	bus->mode = pmac_i2c_mode_std;
	bus->open = kw_i2c_open;
	bus->close = kw_i2c_close;
	bus->xfer = kw_i2c_xfer;
589
	mutex_init(&bus->mutex);
590 591 592 593 594 595 596 597 598 599 600 601 602
	if (controller == busnode)
		bus->flags = pmac_i2c_multibus;
	list_add(&bus->link, &pmac_i2c_busses);

	printk(KERN_INFO " channel %d bus %s\n", channel,
	       (controller == busnode) ? "<multibus>" : busnode->full_name);
}

static void __init kw_i2c_probe(void)
{
	struct device_node *np, *child, *parent;

	/* Probe keywest-i2c busses */
603
	for_each_compatible_node(np, "i2c","keywest-i2c") {
604
		struct pmac_i2c_host_kw *host;
605
		int multibus;
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

		/* Found one, init a host structure */
		host = kw_i2c_host_init(np);
		if (host == NULL)
			continue;

		/* Now check if we have a multibus setup (old style) or if we
		 * have proper bus nodes. Note that the "new" way (proper bus
		 * nodes) might cause us to not create some busses that are
		 * kept hidden in the device-tree. In the future, we might
		 * want to work around that by creating busses without a node
		 * but not for now
		 */
		child = of_get_next_child(np, NULL);
		multibus = !child || strcmp(child->name, "i2c-bus");
		of_node_put(child);

		/* For a multibus setup, we get the bus count based on the
		 * parent type
		 */
		if (multibus) {
627 628
			int chans, i;

629 630 631 632 633 634 635 636 637
			parent = of_get_parent(np);
			if (parent == NULL)
				continue;
			chans = parent->name[0] == 'u' ? 2 : 1;
			for (i = 0; i < chans; i++)
				kw_i2c_add(host, np, np, i);
		} else {
			for (child = NULL;
			     (child = of_get_next_child(np, child)) != NULL;) {
638
				const u32 *reg = of_get_property(child,
639
						"reg", NULL);
640 641 642 643 644 645 646 647 648
				if (reg == NULL)
					continue;
				kw_i2c_add(host, np, child, *reg);
			}
		}
	}
}


649 650 651 652 653 654 655 656
/*
 *
 * PMU implementation
 *
 */

#ifdef CONFIG_ADB_PMU

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
/*
 * i2c command block to the PMU
 */
struct pmu_i2c_hdr {
	u8	bus;
	u8	mode;
	u8	bus2;
	u8	address;
	u8	sub_addr;
	u8	comb_addr;
	u8	count;
	u8	data[];
};

static void pmu_i2c_complete(struct adb_request *req)
672
{
673
	complete(req->arg);
674 675
}

676 677
static int pmu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
			u32 subaddr, u8 *data, int len)
678
{
679 680 681 682 683 684
	struct adb_request *req = bus->hostdata;
	struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req->data[1];
	struct completion comp;
	int read = addrdir & 1;
	int retry;
	int rc = 0;
685

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	/* For now, limit ourselves to 16 bytes transfers */
	if (len > 16)
		return -EINVAL;

	init_completion(&comp);

	for (retry = 0; retry < 16; retry++) {
		memset(req, 0, sizeof(struct adb_request));
		hdr->bus = bus->channel;
		hdr->count = len;

		switch(bus->mode) {
		case pmac_i2c_mode_std:
			if (subsize != 0)
				return -EINVAL;
			hdr->address = addrdir;
			hdr->mode = PMU_I2C_MODE_SIMPLE;
			break;
		case pmac_i2c_mode_stdsub:
		case pmac_i2c_mode_combined:
			if (subsize != 1)
				return -EINVAL;
			hdr->address = addrdir & 0xfe;
			hdr->comb_addr = addrdir;
			hdr->sub_addr = subaddr;
			if (bus->mode == pmac_i2c_mode_stdsub)
				hdr->mode = PMU_I2C_MODE_STDSUB;
			else
				hdr->mode = PMU_I2C_MODE_COMBINED;
			break;
		default:
			return -EINVAL;
		}

		INIT_COMPLETION(comp);
		req->data[0] = PMU_I2C_CMD;
		req->reply[0] = 0xff;
		req->nbytes = sizeof(struct pmu_i2c_hdr) + 1;
		req->done = pmu_i2c_complete;
		req->arg = &comp;
726
		if (!read && len) {
727 728 729 730 731 732 733 734 735 736
			memcpy(hdr->data, data, len);
			req->nbytes += len;
		}
		rc = pmu_queue_request(req);
		if (rc)
			return rc;
		wait_for_completion(&comp);
		if (req->reply[0] == PMU_I2C_STATUS_OK)
			break;
		msleep(15);
737
	}
738 739
	if (req->reply[0] != PMU_I2C_STATUS_OK)
		return -EIO;
740

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	for (retry = 0; retry < 16; retry++) {
		memset(req, 0, sizeof(struct adb_request));

		/* I know that looks like a lot, slow as hell, but darwin
		 * does it so let's be on the safe side for now
		 */
		msleep(15);

		hdr->bus = PMU_I2C_BUS_STATUS;

		INIT_COMPLETION(comp);
		req->data[0] = PMU_I2C_CMD;
		req->reply[0] = 0xff;
		req->nbytes = 2;
		req->done = pmu_i2c_complete;
		req->arg = &comp;
		rc = pmu_queue_request(req);
		if (rc)
			return rc;
		wait_for_completion(&comp);

		if (req->reply[0] == PMU_I2C_STATUS_OK && !read)
			return 0;
		if (req->reply[0] == PMU_I2C_STATUS_DATAREAD && read) {
			int rlen = req->reply_len - 1;

			if (rlen != len) {
				printk(KERN_WARNING "low_i2c: PMU returned %d"
				       " bytes, expected %d !\n", rlen, len);
				return -EIO;
			}
772 773
			if (len)
				memcpy(data, &req->reply[1], len);
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
			return 0;
		}
	}
	return -EIO;
}

static void __init pmu_i2c_probe(void)
{
	struct pmac_i2c_bus *bus;
	struct device_node *busnode;
	int channel, sz;

	if (!pmu_present())
		return;

	/* There might or might not be a "pmu-i2c" node, we use that
	 * or via-pmu itself, whatever we find. I haven't seen a machine
	 * with separate bus nodes, so we assume a multibus setup
	 */
	busnode = of_find_node_by_name(NULL, "pmu-i2c");
	if (busnode == NULL)
		busnode = of_find_node_by_name(NULL, "via-pmu");
	if (busnode == NULL)
		return;

	printk(KERN_INFO "PMU i2c %s\n", busnode->full_name);

	/*
	 * We add bus 1 and 2 only for now, bus 0 is "special"
	 */
	for (channel = 1; channel <= 2; channel++) {
		sz = sizeof(struct pmac_i2c_bus) + sizeof(struct adb_request);
		bus = kzalloc(sz, GFP_KERNEL);
		if (bus == NULL)
			return;

		bus->controller = busnode;
		bus->busnode = busnode;
		bus->type = pmac_i2c_bus_pmu;
		bus->channel = channel;
		bus->mode = pmac_i2c_mode_std;
		bus->hostdata = bus + 1;
		bus->xfer = pmu_i2c_xfer;
817
		mutex_init(&bus->mutex);
818 819 820 821 822
		bus->flags = pmac_i2c_multibus;
		list_add(&bus->link, &pmac_i2c_busses);

		printk(KERN_INFO " channel %d bus <multibus>\n", channel);
	}
823 824 825 826
}

#endif /* CONFIG_ADB_PMU */

827 828 829 830 831 832 833 834 835 836

/*
 *
 * SMU implementation
 *
 */

#ifdef CONFIG_PMAC_SMU

static void smu_i2c_complete(struct smu_i2c_cmd *cmd, void *misc)
837
{
838 839
	complete(misc);
}
840

841 842 843 844 845 846 847 848
static int smu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
			u32 subaddr, u8 *data, int len)
{
	struct smu_i2c_cmd *cmd = bus->hostdata;
	struct completion comp;
	int read = addrdir & 1;
	int rc = 0;

849 850 851 852
	if ((read && len > SMU_I2C_READ_MAX) ||
	    ((!read) && len > SMU_I2C_WRITE_MAX))
		return -EINVAL;

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	memset(cmd, 0, sizeof(struct smu_i2c_cmd));
	cmd->info.bus = bus->channel;
	cmd->info.devaddr = addrdir;
	cmd->info.datalen = len;

	switch(bus->mode) {
	case pmac_i2c_mode_std:
		if (subsize != 0)
			return -EINVAL;
		cmd->info.type = SMU_I2C_TRANSFER_SIMPLE;
		break;
	case pmac_i2c_mode_stdsub:
	case pmac_i2c_mode_combined:
		if (subsize > 3 || subsize < 1)
			return -EINVAL;
		cmd->info.sublen = subsize;
		/* that's big-endian only but heh ! */
		memcpy(&cmd->info.subaddr, ((char *)&subaddr) + (4 - subsize),
		       subsize);
		if (bus->mode == pmac_i2c_mode_stdsub)
			cmd->info.type = SMU_I2C_TRANSFER_STDSUB;
		else
			cmd->info.type = SMU_I2C_TRANSFER_COMBINED;
		break;
	default:
		return -EINVAL;
879
	}
880
	if (!read && len)
881 882 883 884 885 886 887 888 889 890 891
		memcpy(cmd->info.data, data, len);

	init_completion(&comp);
	cmd->done = smu_i2c_complete;
	cmd->misc = &comp;
	rc = smu_queue_i2c(cmd);
	if (rc < 0)
		return rc;
	wait_for_completion(&comp);
	rc = cmd->status;

892
	if (read && len)
893 894 895
		memcpy(data, cmd->info.data, len);
	return rc < 0 ? rc : 0;
}
896

897 898 899 900
static void __init smu_i2c_probe(void)
{
	struct device_node *controller, *busnode;
	struct pmac_i2c_bus *bus;
901
	const u32 *reg;
902 903 904 905 906
	int sz;

	if (!smu_present())
		return;

907
	controller = of_find_node_by_name(NULL, "smu-i2c-control");
908 909 910 911 912 913 914 915
	if (controller == NULL)
		controller = of_find_node_by_name(NULL, "smu");
	if (controller == NULL)
		return;

	printk(KERN_INFO "SMU i2c %s\n", controller->full_name);

	/* Look for childs, note that they might not be of the right
L
Lucas De Marchi 已提交
916
	 * type as older device trees mix i2c busses and other things
917 918 919 920 921 922 923
	 * at the same level
	 */
	for (busnode = NULL;
	     (busnode = of_get_next_child(controller, busnode)) != NULL;) {
		if (strcmp(busnode->type, "i2c") &&
		    strcmp(busnode->type, "i2c-bus"))
			continue;
924
		reg = of_get_property(busnode, "reg", NULL);
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
		if (reg == NULL)
			continue;

		sz = sizeof(struct pmac_i2c_bus) + sizeof(struct smu_i2c_cmd);
		bus = kzalloc(sz, GFP_KERNEL);
		if (bus == NULL)
			return;

		bus->controller = controller;
		bus->busnode = of_node_get(busnode);
		bus->type = pmac_i2c_bus_smu;
		bus->channel = *reg;
		bus->mode = pmac_i2c_mode_std;
		bus->hostdata = bus + 1;
		bus->xfer = smu_i2c_xfer;
940
		mutex_init(&bus->mutex);
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
		bus->flags = 0;
		list_add(&bus->link, &pmac_i2c_busses);

		printk(KERN_INFO " channel %x bus %s\n",
		       bus->channel, busnode->full_name);
	}
}

#endif /* CONFIG_PMAC_SMU */

/*
 *
 * Core code
 *
 */


struct pmac_i2c_bus *pmac_i2c_find_bus(struct device_node *node)
{
	struct device_node *p = of_node_get(node);
	struct device_node *prev = NULL;
	struct pmac_i2c_bus *bus;

	while(p) {
		list_for_each_entry(bus, &pmac_i2c_busses, link) {
			if (p == bus->busnode) {
				if (prev && bus->flags & pmac_i2c_multibus) {
968
					const u32 *reg;
969 970
					reg = of_get_property(prev, "reg",
								NULL);
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
					if (!reg)
						continue;
					if (((*reg) >> 8) != bus->channel)
						continue;
				}
				of_node_put(p);
				of_node_put(prev);
				return bus;
			}
		}
		of_node_put(prev);
		prev = p;
		p = of_get_parent(p);
	}
	return NULL;
}
EXPORT_SYMBOL_GPL(pmac_i2c_find_bus);

u8 pmac_i2c_get_dev_addr(struct device_node *device)
{
991
	const u32 *reg = of_get_property(device, "reg", NULL);
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

	if (reg == NULL)
		return 0;

	return (*reg) & 0xff;
}
EXPORT_SYMBOL_GPL(pmac_i2c_get_dev_addr);

struct device_node *pmac_i2c_get_controller(struct pmac_i2c_bus *bus)
{
	return bus->controller;
}
EXPORT_SYMBOL_GPL(pmac_i2c_get_controller);

struct device_node *pmac_i2c_get_bus_node(struct pmac_i2c_bus *bus)
{
	return bus->busnode;
}
EXPORT_SYMBOL_GPL(pmac_i2c_get_bus_node);

int pmac_i2c_get_type(struct pmac_i2c_bus *bus)
{
	return bus->type;
}
EXPORT_SYMBOL_GPL(pmac_i2c_get_type);

int pmac_i2c_get_flags(struct pmac_i2c_bus *bus)
{
	return bus->flags;
}
EXPORT_SYMBOL_GPL(pmac_i2c_get_flags);
1023

1024 1025 1026 1027 1028 1029 1030
int pmac_i2c_get_channel(struct pmac_i2c_bus *bus)
{
	return bus->channel;
}
EXPORT_SYMBOL_GPL(pmac_i2c_get_channel);


1031 1032
struct i2c_adapter *pmac_i2c_get_adapter(struct pmac_i2c_bus *bus)
{
1033
	return &bus->adapter;
1034 1035 1036
}
EXPORT_SYMBOL_GPL(pmac_i2c_get_adapter);

1037 1038 1039 1040 1041
struct pmac_i2c_bus *pmac_i2c_adapter_to_bus(struct i2c_adapter *adapter)
{
	struct pmac_i2c_bus *bus;

	list_for_each_entry(bus, &pmac_i2c_busses, link)
1042
		if (&bus->adapter == adapter)
1043 1044 1045 1046 1047
			return bus;
	return NULL;
}
EXPORT_SYMBOL_GPL(pmac_i2c_adapter_to_bus);

A
Al Viro 已提交
1048
int pmac_i2c_match_adapter(struct device_node *dev, struct i2c_adapter *adapter)
1049 1050 1051 1052 1053
{
	struct pmac_i2c_bus *bus = pmac_i2c_find_bus(dev);

	if (bus == NULL)
		return 0;
1054
	return (&bus->adapter == adapter);
1055 1056
}
EXPORT_SYMBOL_GPL(pmac_i2c_match_adapter);
1057 1058 1059

int pmac_low_i2c_lock(struct device_node *np)
{
1060
	struct pmac_i2c_bus *bus, *found = NULL;
1061

1062 1063 1064 1065 1066 1067 1068
	list_for_each_entry(bus, &pmac_i2c_busses, link) {
		if (np == bus->controller) {
			found = bus;
			break;
		}
	}
	if (!found)
1069
		return -ENODEV;
1070
	return pmac_i2c_open(bus, 0);
1071
}
1072
EXPORT_SYMBOL_GPL(pmac_low_i2c_lock);
1073 1074 1075

int pmac_low_i2c_unlock(struct device_node *np)
{
1076
	struct pmac_i2c_bus *bus, *found = NULL;
1077

1078 1079 1080 1081 1082 1083 1084
	list_for_each_entry(bus, &pmac_i2c_busses, link) {
		if (np == bus->controller) {
			found = bus;
			break;
		}
	}
	if (!found)
1085
		return -ENODEV;
1086
	pmac_i2c_close(bus);
1087 1088
	return 0;
}
1089
EXPORT_SYMBOL_GPL(pmac_low_i2c_unlock);
1090 1091


1092
int pmac_i2c_open(struct pmac_i2c_bus *bus, int polled)
1093
{
1094 1095
	int rc;

1096
	mutex_lock(&bus->mutex);
1097
	bus->polled = polled || pmac_i2c_force_poll;
1098 1099 1100 1101
	bus->opened = 1;
	bus->mode = pmac_i2c_mode_std;
	if (bus->open && (rc = bus->open(bus)) != 0) {
		bus->opened = 0;
1102
		mutex_unlock(&bus->mutex);
1103 1104 1105 1106 1107
		return rc;
	}
	return 0;
}
EXPORT_SYMBOL_GPL(pmac_i2c_open);
1108

1109 1110 1111 1112 1113 1114
void pmac_i2c_close(struct pmac_i2c_bus *bus)
{
	WARN_ON(!bus->opened);
	if (bus->close)
		bus->close(bus);
	bus->opened = 0;
1115
	mutex_unlock(&bus->mutex);
1116 1117
}
EXPORT_SYMBOL_GPL(pmac_i2c_close);
1118

1119 1120 1121
int pmac_i2c_setmode(struct pmac_i2c_bus *bus, int mode)
{
	WARN_ON(!bus->opened);
1122

1123 1124 1125 1126 1127 1128 1129 1130 1131
	/* Report me if you see the error below as there might be a new
	 * "combined4" mode that I need to implement for the SMU bus
	 */
	if (mode < pmac_i2c_mode_dumb || mode > pmac_i2c_mode_combined) {
		printk(KERN_ERR "low_i2c: Invalid mode %d requested on"
		       " bus %s !\n", mode, bus->busnode->full_name);
		return -EINVAL;
	}
	bus->mode = mode;
1132 1133 1134

	return 0;
}
1135
EXPORT_SYMBOL_GPL(pmac_i2c_setmode);
1136

1137 1138
int pmac_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
		  u32 subaddr, u8 *data, int len)
1139
{
1140
	int rc;
1141

1142
	WARN_ON(!bus->opened);
1143

1144 1145 1146
	DBG("xfer() chan=%d, addrdir=0x%x, mode=%d, subsize=%d, subaddr=0x%x,"
	    " %d bytes, bus %s\n", bus->channel, addrdir, bus->mode, subsize,
	    subaddr, len, bus->busnode->full_name);
1147

1148 1149 1150 1151 1152 1153 1154
	rc = bus->xfer(bus, addrdir, subsize, subaddr, data, len);

#ifdef DEBUG
	if (rc)
		DBG("xfer error %d\n", rc);
#endif
	return rc;
1155
}
1156
EXPORT_SYMBOL_GPL(pmac_i2c_xfer);
1157

1158 1159 1160
/* some quirks for platform function decoding */
enum {
	pmac_i2c_quirk_invmask = 0x00000001u,
1161
	pmac_i2c_quirk_skip = 0x00000002u,
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
};

static void pmac_i2c_devscan(void (*callback)(struct device_node *dev,
					      int quirks))
{
	struct pmac_i2c_bus *bus;
	struct device_node *np;
	static struct whitelist_ent {
		char *name;
		char *compatible;
		int quirks;
	} whitelist[] = {
		/* XXX Study device-tree's & apple drivers are get the quirks
		 * right !
		 */
1177 1178 1179 1180 1181 1182 1183 1184 1185
		/* Workaround: It seems that running the clockspreading
		 * properties on the eMac will cause lockups during boot.
		 * The machine seems to work fine without that. So for now,
		 * let's make sure i2c-hwclock doesn't match about "imic"
		 * clocks and we'll figure out if we really need to do
		 * something special about those later.
		 */
		{ "i2c-hwclock", "imic5002", pmac_i2c_quirk_skip },
		{ "i2c-hwclock", "imic5003", pmac_i2c_quirk_skip },
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
		{ "i2c-hwclock", NULL, pmac_i2c_quirk_invmask },
		{ "i2c-cpu-voltage", NULL, 0},
		{  "temp-monitor", NULL, 0 },
		{  "supply-monitor", NULL, 0 },
		{ NULL, NULL, 0 },
	};

	/* Only some devices need to have platform functions instanciated
	 * here. For now, we have a table. Others, like 9554 i2c GPIOs used
	 * on Xserve, if we ever do a driver for them, will use their own
	 * platform function instance
	 */
	list_for_each_entry(bus, &pmac_i2c_busses, link) {
		for (np = NULL;
		     (np = of_get_next_child(bus->busnode, np)) != NULL;) {
			struct whitelist_ent *p;
			/* If multibus, check if device is on that bus */
			if (bus->flags & pmac_i2c_multibus)
				if (bus != pmac_i2c_find_bus(np))
					continue;
			for (p = whitelist; p->name != NULL; p++) {
				if (strcmp(np->name, p->name))
					continue;
				if (p->compatible &&
1210
				    !of_device_is_compatible(np, p->compatible))
1211
					continue;
1212 1213
				if (p->quirks & pmac_i2c_quirk_skip)
					break;
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
				callback(np, p->quirks);
				break;
			}
		}
	}
}

#define MAX_I2C_DATA	64

struct pmac_i2c_pf_inst
{
	struct pmac_i2c_bus	*bus;
	u8			addr;
	u8			buffer[MAX_I2C_DATA];
	u8			scratch[MAX_I2C_DATA];
	int			bytes;
	int			quirks;
};

static void* pmac_i2c_do_begin(struct pmf_function *func, struct pmf_args *args)
{
	struct pmac_i2c_pf_inst *inst;
	struct pmac_i2c_bus	*bus;

	bus = pmac_i2c_find_bus(func->node);
	if (bus == NULL) {
		printk(KERN_ERR "low_i2c: Can't find bus for %s (pfunc)\n",
		       func->node->full_name);
		return NULL;
	}
	if (pmac_i2c_open(bus, 0)) {
		printk(KERN_ERR "low_i2c: Can't open i2c bus for %s (pfunc)\n",
		       func->node->full_name);
		return NULL;
	}

	/* XXX might need GFP_ATOMIC when called during the suspend process,
	 * but then, there are already lots of issues with suspending when
	 * near OOM that need to be resolved, the allocator itself should
	 * probably make GFP_NOIO implicit during suspend
	 */
	inst = kzalloc(sizeof(struct pmac_i2c_pf_inst), GFP_KERNEL);
	if (inst == NULL) {
		pmac_i2c_close(bus);
		return NULL;
	}
	inst->bus = bus;
	inst->addr = pmac_i2c_get_dev_addr(func->node);
	inst->quirks = (int)(long)func->driver_data;
	return inst;
}

static void pmac_i2c_do_end(struct pmf_function *func, void *instdata)
{
	struct pmac_i2c_pf_inst *inst = instdata;

	if (inst == NULL)
		return;
	pmac_i2c_close(inst->bus);
1273
	kfree(inst);
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
}

static int pmac_i2c_do_read(PMF_STD_ARGS, u32 len)
{
	struct pmac_i2c_pf_inst *inst = instdata;

	inst->bytes = len;
	return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 0, 0,
			     inst->buffer, len);
}

static int pmac_i2c_do_write(PMF_STD_ARGS, u32 len, const u8 *data)
{
	struct pmac_i2c_pf_inst *inst = instdata;

	return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
			     (u8 *)data, len);
}

/* This function is used to do the masking & OR'ing for the "rmw" type
 * callbacks. Ze should apply the mask and OR in the values in the
 * buffer before writing back. The problem is that it seems that
 * various darwin drivers implement the mask/or differently, thus
 * we need to check the quirks first
 */
static void pmac_i2c_do_apply_rmw(struct pmac_i2c_pf_inst *inst,
				  u32 len, const u8 *mask, const u8 *val)
{
	int i;

	if (inst->quirks & pmac_i2c_quirk_invmask) {
		for (i = 0; i < len; i ++)
			inst->scratch[i] = (inst->buffer[i] & mask[i]) | val[i];
	} else {
		for (i = 0; i < len; i ++)
			inst->scratch[i] = (inst->buffer[i] & ~mask[i])
				| (val[i] & mask[i]);
	}
}

static int pmac_i2c_do_rmw(PMF_STD_ARGS, u32 masklen, u32 valuelen,
			   u32 totallen, const u8 *maskdata,
			   const u8 *valuedata)
{
	struct pmac_i2c_pf_inst *inst = instdata;

	if (masklen > inst->bytes || valuelen > inst->bytes ||
	    totallen > inst->bytes || valuelen > masklen)
		return -EINVAL;

	pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);

	return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
			     inst->scratch, totallen);
}

static int pmac_i2c_do_read_sub(PMF_STD_ARGS, u8 subaddr, u32 len)
{
	struct pmac_i2c_pf_inst *inst = instdata;

	inst->bytes = len;
	return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 1, subaddr,
			     inst->buffer, len);
}

static int pmac_i2c_do_write_sub(PMF_STD_ARGS, u8 subaddr, u32 len,
				     const u8 *data)
{
	struct pmac_i2c_pf_inst *inst = instdata;

	return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
			     subaddr, (u8 *)data, len);
}

static int pmac_i2c_do_set_mode(PMF_STD_ARGS, int mode)
{
	struct pmac_i2c_pf_inst *inst = instdata;

	return pmac_i2c_setmode(inst->bus, mode);
}

static int pmac_i2c_do_rmw_sub(PMF_STD_ARGS, u8 subaddr, u32 masklen,
			       u32 valuelen, u32 totallen, const u8 *maskdata,
			       const u8 *valuedata)
{
	struct pmac_i2c_pf_inst *inst = instdata;

	if (masklen > inst->bytes || valuelen > inst->bytes ||
	    totallen > inst->bytes || valuelen > masklen)
		return -EINVAL;

	pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);

	return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
			     subaddr, inst->scratch, totallen);
}

static int pmac_i2c_do_mask_and_comp(PMF_STD_ARGS, u32 len,
				     const u8 *maskdata,
				     const u8 *valuedata)
{
	struct pmac_i2c_pf_inst *inst = instdata;
	int i, match;

	/* Get return value pointer, it's assumed to be a u32 */
	if (!args || !args->count || !args->u[0].p)
		return -EINVAL;

	/* Check buffer */
	if (len > inst->bytes)
		return -EINVAL;

	for (i = 0, match = 1; match && i < len; i ++)
		if ((inst->buffer[i] & maskdata[i]) != valuedata[i])
			match = 0;
	*args->u[0].p = match;
	return 0;
}

static int pmac_i2c_do_delay(PMF_STD_ARGS, u32 duration)
{
	msleep((duration + 999) / 1000);
	return 0;
}


static struct pmf_handlers pmac_i2c_pfunc_handlers = {
	.begin			= pmac_i2c_do_begin,
	.end			= pmac_i2c_do_end,
	.read_i2c		= pmac_i2c_do_read,
	.write_i2c		= pmac_i2c_do_write,
	.rmw_i2c		= pmac_i2c_do_rmw,
	.read_i2c_sub		= pmac_i2c_do_read_sub,
	.write_i2c_sub		= pmac_i2c_do_write_sub,
	.rmw_i2c_sub		= pmac_i2c_do_rmw_sub,
	.set_i2c_mode		= pmac_i2c_do_set_mode,
	.mask_and_compare	= pmac_i2c_do_mask_and_comp,
	.delay			= pmac_i2c_do_delay,
};

static void __init pmac_i2c_dev_create(struct device_node *np, int quirks)
{
	DBG("dev_create(%s)\n", np->full_name);

	pmf_register_driver(np, &pmac_i2c_pfunc_handlers,
			    (void *)(long)quirks);
}

static void __init pmac_i2c_dev_init(struct device_node *np, int quirks)
{
	DBG("dev_create(%s)\n", np->full_name);

	pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
}

static void pmac_i2c_dev_suspend(struct device_node *np, int quirks)
{
	DBG("dev_suspend(%s)\n", np->full_name);
	pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_SLEEP, NULL);
}

static void pmac_i2c_dev_resume(struct device_node *np, int quirks)
{
	DBG("dev_resume(%s)\n", np->full_name);
	pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_WAKE, NULL);
}

void pmac_pfunc_i2c_suspend(void)
{
	pmac_i2c_devscan(pmac_i2c_dev_suspend);
}

void pmac_pfunc_i2c_resume(void)
{
	pmac_i2c_devscan(pmac_i2c_dev_resume);
}

1451
/*
1452 1453
 * Initialize us: probe all i2c busses on the machine, instantiate
 * busses and platform functions as needed.
1454 1455 1456
 */
/* This is non-static as it might be called early by smp code */
int __init pmac_i2c_init(void)
1457
{
1458
	static int i2c_inited;
1459

1460 1461 1462
	if (i2c_inited)
		return 0;
	i2c_inited = 1;
1463

1464 1465
	/* Probe keywest-i2c busses */
	kw_i2c_probe();
1466

1467
#ifdef CONFIG_ADB_PMU
1468
	/* Probe PMU i2c busses */
1469 1470
	pmu_i2c_probe();
#endif
1471

1472
#ifdef CONFIG_PMAC_SMU
1473
	/* Probe SMU i2c busses */
1474 1475
	smu_i2c_probe();
#endif
1476 1477 1478 1479

	/* Now add plaform functions for some known devices */
	pmac_i2c_devscan(pmac_i2c_dev_create);

1480
	return 0;
1481
}
1482
machine_arch_initcall(powermac, pmac_i2c_init);
1483

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
/* Since pmac_i2c_init can be called too early for the platform device
 * registration, we need to do it at a later time. In our case, subsys
 * happens to fit well, though I agree it's a bit of a hack...
 */
static int __init pmac_i2c_create_platform_devices(void)
{
	struct pmac_i2c_bus *bus;
	int i = 0;

	/* In the case where we are initialized from smp_init(), we must
	 * not use the timer (and thus the irq). It's safe from now on
	 * though
	 */
	pmac_i2c_force_poll = 0;

	/* Create platform devices */
	list_for_each_entry(bus, &pmac_i2c_busses, link) {
		bus->platform_dev =
			platform_device_alloc("i2c-powermac", i++);
		if (bus->platform_dev == NULL)
			return -ENOMEM;
		bus->platform_dev->dev.platform_data = bus;
1506
		bus->platform_dev->dev.of_node = bus->busnode;
1507 1508 1509
		platform_device_add(bus->platform_dev);
	}

1510 1511 1512
	/* Now call platform "init" functions */
	pmac_i2c_devscan(pmac_i2c_dev_init);

1513 1514
	return 0;
}
1515
machine_subsys_initcall(powermac, pmac_i2c_create_platform_devices);