nvec.c 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * NVEC: NVIDIA compliant embedded controller interface
 *
 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
 *
 * Authors:  Pierre-Hugues Husson <phhusson@free.fr>
 *           Ilya Petrov <ilya.muromec@gmail.com>
 *           Marc Dietrich <marvin24@gmx.de>
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 */

/* #define DEBUG */
17 18

#include <asm/irq.h>
19

20
#include <linux/atomic.h>
21 22
#include <linux/completion.h>
#include <linux/interrupt.h>
23
#include <linux/io.h>
24 25 26 27 28 29 30 31
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/clk.h>
32

33 34 35 36
#include <linux/semaphore.h>
#include <linux/list.h>
#include <linux/notifier.h>
#include <linux/platform_device.h>
37
#include <linux/mfd/core.h>
38 39 40 41

#include <mach/iomap.h>
#include <mach/clk.h>

42 43
#include "nvec.h"

44 45 46
static const unsigned char EC_DISABLE_EVENT_REPORTING[3] = "\x04\x00\x00";
static const unsigned char EC_ENABLE_EVENT_REPORTING[3]  = "\x04\x00\x01";
static const unsigned char EC_GET_FIRMWARE_VERSION[2]    = "\x07\x15";
47 48 49

static struct nvec_chip *nvec_power_handle;

50 51
static struct mfd_cell nvec_devices[] = {
	{
52 53
		.name = "nvec-kbd",
		.id = 1,
54 55
	},
	{
56 57
		.name = "nvec-mouse",
		.id = 1,
58 59
	},
	{
60 61
		.name = "nvec-power",
		.id = 1,
62 63
	},
	{
64 65
		.name = "nvec-power",
		.id = 2,
66
	},
I
Ilya Petrov 已提交
67 68 69 70
	{
		.name = "nvec-leds",
		.id = 1,
	},
71 72
};

73
int nvec_register_notifier(struct nvec_chip *nvec, struct notifier_block *nb,
74
			   unsigned int events)
75 76 77 78 79
{
	return atomic_notifier_chain_register(&nvec->notifier_list, nb);
}
EXPORT_SYMBOL_GPL(nvec_register_notifier);

80 81
static int nvec_status_notifier(struct notifier_block *nb,
				unsigned long event_type, void *data)
82 83 84
{
	unsigned char *msg = (unsigned char *)data;

85
	if (event_type != NVEC_CNTL)
86 87
		return NOTIFY_DONE;

88 89 90
	printk(KERN_WARNING "unhandled msg type %ld\n", event_type);
	print_hex_dump(KERN_WARNING, "payload: ", DUMP_PREFIX_NONE, 16, 1,
		msg, msg[1] + 2, true);
91 92 93 94

	return NOTIFY_OK;
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static struct nvec_msg *nvec_msg_alloc(struct nvec_chip *nvec)
{
	int i;

	for (i = 0; i < NVEC_POOL_SIZE; i++) {
		if (atomic_xchg(&nvec->msg_pool[i].used, 1) == 0) {
			dev_vdbg(nvec->dev, "INFO: Allocate %i\n", i);
			return &nvec->msg_pool[i];
		}
	}

	dev_err(nvec->dev, "could not allocate buffer\n");

	return NULL;
}

static void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg)
{
	dev_vdbg(nvec->dev, "INFO: Free %ti\n", msg - nvec->msg_pool);
	atomic_set(&msg->used, 0);
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/**
 * nvec_msg_is_event - Return %true if @msg is an event
 * @msg: A message
 */
static bool nvec_msg_is_event(struct nvec_msg *msg)
{
	return msg->data[0] >> 7;
}

/**
 * nvec_msg_size - Get the size of a message
 * @msg: The message to get the size for
 *
 * This only works for received messages, not for outgoing messages.
 */
static size_t nvec_msg_size(struct nvec_msg *msg)
{
	bool is_event = nvec_msg_is_event(msg);
	int event_length = (msg->data[0] & 0x60) >> 5;

	/* for variable size, payload size in byte 1 + count (1) + cmd (1) */
	if (!is_event || event_length == NVEC_VAR_SIZE)
		return (msg->pos || msg->size) ? (msg->data[1] + 2) : 0;
	else if (event_length == NVEC_2BYTES)
		return 2;
	else if (event_length == NVEC_3BYTES)
		return 3;
	else
		return 0;
}

148 149 150 151 152 153 154
static void nvec_gpio_set_value(struct nvec_chip *nvec, int value)
{
	dev_dbg(nvec->dev, "GPIO changed from %u to %u\n",
		gpio_get_value(nvec->gpio), value);
	gpio_set_value(nvec->gpio, value);
}

155 156
void nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
			short size)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
{
	struct nvec_msg *msg = kzalloc(sizeof(struct nvec_msg), GFP_NOWAIT);

	msg->data = kzalloc(size, GFP_NOWAIT);
	msg->data[0] = size;
	memcpy(msg->data + 1, data, size);
	msg->size = size + 1;
	msg->pos = 0;
	INIT_LIST_HEAD(&msg->node);

	list_add_tail(&msg->node, &nvec->tx_data);

	gpio_set_value(nvec->gpio, 0);
}
EXPORT_SYMBOL(nvec_write_async);

static void nvec_request_master(struct work_struct *work)
{
	struct nvec_chip *nvec = container_of(work, struct nvec_chip, tx_work);

177
	if (!list_empty(&nvec->tx_data))
178 179 180 181 182
		gpio_set_value(nvec->gpio, 0);
}

static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg)
{
183 184 185
	if ((msg->data[0] & 1 << 7) == 0 && msg->data[3]) {
		dev_err(nvec->dev, "ec responded %02x %02x %02x %02x\n",
			msg->data[0], msg->data[1], msg->data[2], msg->data[3]);
186 187 188
		return -EINVAL;
	}

189 190 191 192
	if ((msg->data[0] >> 7) == 1 && (msg->data[0] & 0x0f) == 5)
		print_hex_dump(KERN_WARNING, "ec system event ",
				DUMP_PREFIX_NONE, 16, 1, msg->data,
				msg->data[1] + 2, true);
193

194 195
	atomic_notifier_call_chain(&nvec->notifier_list, msg->data[0] & 0x8f,
				   msg->data);
196 197 198 199

	return 0;
}

200 201
static struct nvec_msg *nvec_write_sync(struct nvec_chip *nvec,
					const unsigned char *data, short size)
202 203 204 205 206 207
{
	down(&nvec->sync_write_mutex);

	nvec->sync_write_pending = (data[1] << 8) + data[0];
	nvec_write_async(nvec, data, size);

208 209
	dev_dbg(nvec->dev, "nvec_sync_write: 0x%04x\n",
		nvec->sync_write_pending);
210 211 212 213 214 215 216 217 218 219 220 221 222 223
	wait_for_completion(&nvec->sync_write);
	dev_dbg(nvec->dev, "nvec_sync_write: pong!\n");

	up(&nvec->sync_write_mutex);

	return nvec->last_sync_msg;
}

/* RX worker */
static void nvec_dispatch(struct work_struct *work)
{
	struct nvec_chip *nvec = container_of(work, struct nvec_chip, rx_work);
	struct nvec_msg *msg;

224
	while (!list_empty(&nvec->rx_data)) {
225 226 227
		msg = list_first_entry(&nvec->rx_data, struct nvec_msg, node);
		list_del_init(&msg->node);

228 229
		if (nvec->sync_write_pending ==
		    (msg->data[2] << 8) + msg->data[0]) {
230 231 232 233 234 235
			dev_dbg(nvec->dev, "sync write completed!\n");
			nvec->sync_write_pending = 0;
			nvec->last_sync_msg = msg;
			complete(&nvec->sync_write);
		} else {
			parse_msg(nvec, msg);
236 237 238
			if ((!msg) || (!msg->data))
				dev_warn(nvec->dev,
					"attempt access zero pointer\n");
239 240 241 242 243 244 245 246
			else {
				kfree(msg->data);
				kfree(msg);
			}
		}
	}
}

247
static irqreturn_t nvec_interrupt(int irq, void *dev)
248 249 250 251 252 253
{
	unsigned long status;
	unsigned long received;
	unsigned char to_send;
	struct nvec_msg *msg;
	struct nvec_chip *nvec = (struct nvec_chip *)dev;
254
	void __iomem *base = nvec->base;
255

256
	status = readl(base + I2C_SL_STATUS);
257

258
	if (!(status & I2C_SL_IRQ)) {
259 260 261
		dev_warn(nvec->dev, "nvec Spurious IRQ\n");
		goto handled;
	}
262
	if (status & END_TRANS && !(status & RCVD)) {
263
		nvec->state = NVEC_WAIT;
264
		if (nvec->rx->size > 1) {
265 266 267 268 269 270 271
			list_add_tail(&nvec->rx->node, &nvec->rx_data);
			schedule_work(&nvec->rx_work);
		} else {
			kfree(nvec->rx->data);
			kfree(nvec->rx);
		}
		return IRQ_HANDLED;
272 273
	} else if (status & RNW) {
		if (status & RCVD)
274 275
			udelay(3);

276
		if (status & RCVD)
277
			nvec->state = NVEC_WRITE;
278 279

		if (list_empty(&nvec->tx_data)) {
280 281 282 283
			dev_err(nvec->dev, "nvec empty tx - sending no-op\n");
			to_send = 0x8a;
			nvec_write_async(nvec, "\x07\x02", 2);
		} else {
284 285 286 287
			msg =
			    list_first_entry(&nvec->tx_data, struct nvec_msg,
					     node);
			if (msg->pos < msg->size) {
288 289 290
				to_send = msg->data[msg->pos];
				msg->pos++;
			} else {
291 292
				dev_err(nvec->dev, "nvec crap! %d\n",
					msg->size);
293 294 295
				to_send = 0x01;
			}

296
			if (msg->pos >= msg->size) {
297 298 299 300 301 302 303
				list_del_init(&msg->node);
				kfree(msg->data);
				kfree(msg);
				schedule_work(&nvec->tx_work);
				nvec->state = NVEC_WAIT;
			}
		}
304
		writel(to_send, base + I2C_SL_RCVD);
305 306 307 308 309 310 311

		gpio_set_value(nvec->gpio, 1);

		dev_dbg(nvec->dev, "nvec sent %x\n", to_send);

		goto handled;
	} else {
312
		received = readl(base + I2C_SL_RCVD);
313 314

		if (status & RCVD) {
315
			writel(0, base + I2C_SL_RCVD);
316 317 318
			goto handled;
		}

319
		if (nvec->state == NVEC_WAIT) {
320 321 322 323 324 325 326 327 328 329 330 331 332
			nvec->state = NVEC_READ;
			msg = kzalloc(sizeof(struct nvec_msg), GFP_NOWAIT);
			msg->data = kzalloc(32, GFP_NOWAIT);
			INIT_LIST_HEAD(&msg->node);
			nvec->rx = msg;
		} else
			msg = nvec->rx;

		BUG_ON(msg->pos > 32);

		msg->data[msg->pos] = received;
		msg->pos++;
		msg->size = msg->pos;
333 334
		dev_dbg(nvec->dev, "Got %02lx from Master (pos: %d)!\n",
			received, msg->pos);
335 336 337 338 339
	}
handled:
	return IRQ_HANDLED;
}

340
static void tegra_init_i2c_slave(struct nvec_chip *nvec)
341 342 343
{
	u32 val;

344 345 346
	clk_enable(nvec->i2c_clk);

	tegra_periph_reset_assert(nvec->i2c_clk);
347
	udelay(2);
348
	tegra_periph_reset_deassert(nvec->i2c_clk);
349 350

	val = I2C_CNFG_NEW_MASTER_SFM | I2C_CNFG_PACKET_MODE_EN |
351
	    (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
352
	writel(val, nvec->base + I2C_CNFG);
353 354 355

	clk_set_rate(nvec->i2c_clk, 8 * 80000);

356
	writel(I2C_SL_NEWL, nvec->base + I2C_SL_CNFG);
357 358 359 360
	writel(0x1E, nvec->base + I2C_SL_DELAY_COUNT);

	writel(nvec->i2c_addr>>1, nvec->base + I2C_SL_ADDR1);
	writel(0, nvec->base + I2C_SL_ADDR2);
361

362 363 364 365 366 367 368 369 370
	enable_irq(nvec->irq);

	clk_disable(nvec->i2c_clk);
}

static void nvec_disable_i2c_slave(struct nvec_chip *nvec)
{
	disable_irq(nvec->irq);
	writel(I2C_SL_NEWL | I2C_SL_NACK, nvec->base + I2C_SL_CNFG);
371
	clk_disable(nvec->i2c_clk);
372 373 374 375 376 377 378 379 380 381
}

static void nvec_power_off(void)
{
	nvec_write_async(nvec_power_handle, EC_DISABLE_EVENT_REPORTING, 3);
	nvec_write_async(nvec_power_handle, "\x04\x01", 2);
}

static int __devinit tegra_nvec_probe(struct platform_device *pdev)
{
382
	int err, ret;
383 384 385 386
	struct clk *i2c_clk;
	struct nvec_platform_data *pdata = pdev->dev.platform_data;
	struct nvec_chip *nvec;
	struct nvec_msg *msg;
387 388 389
	struct resource *res;
	struct resource *iomem;
	void __iomem *base;
390 391

	nvec = kzalloc(sizeof(struct nvec_chip), GFP_KERNEL);
392
	if (nvec == NULL) {
393 394 395 396 397 398
		dev_err(&pdev->dev, "failed to reserve memory\n");
		return -ENOMEM;
	}
	platform_set_drvdata(pdev, nvec);
	nvec->dev = &pdev->dev;
	nvec->gpio = pdata->gpio;
399 400 401 402 403 404
	nvec->i2c_addr = pdata->i2c_addr;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "no mem resource?\n");
		return -ENODEV;
405 406
	}

407 408 409 410 411
	iomem = request_mem_region(res->start, resource_size(res), pdev->name);
	if (!iomem) {
		dev_err(&pdev->dev, "I2C region already claimed\n");
		return -EBUSY;
	}
412

413 414 415 416
	base = ioremap(iomem->start, resource_size(iomem));
	if (!base) {
		dev_err(&pdev->dev, "Can't ioremap I2C region\n");
		return -ENOMEM;
417 418
	}

419 420 421 422 423 424
	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!res) {
		dev_err(&pdev->dev, "no irq resource?\n");
		ret = -ENODEV;
		goto err_iounmap;
	}
425

426 427 428 429
	i2c_clk = clk_get_sys("tegra-i2c.2", NULL);
	if (IS_ERR(i2c_clk)) {
		dev_err(nvec->dev, "failed to get controller clock\n");
		goto err_iounmap;
430 431
	}

432 433 434 435
	nvec->base = base;
	nvec->irq = res->start;
	nvec->i2c_clk = i2c_clk;

436 437
	/* Set the gpio to low when we've got something to say */
	err = gpio_request(nvec->gpio, "nvec gpio");
438
	if (err < 0)
439 440 441 442 443 444 445 446 447 448 449
		dev_err(nvec->dev, "couldn't request gpio\n");

	ATOMIC_INIT_NOTIFIER_HEAD(&nvec->notifier_list);

	init_completion(&nvec->sync_write);
	sema_init(&nvec->sync_write_mutex, 1);
	INIT_LIST_HEAD(&nvec->tx_data);
	INIT_LIST_HEAD(&nvec->rx_data);
	INIT_WORK(&nvec->rx_work, nvec_dispatch);
	INIT_WORK(&nvec->tx_work, nvec_request_master);

450 451 452 453 454
	err = request_irq(nvec->irq, nvec_interrupt, 0, "nvec", nvec);
	if (err) {
		dev_err(nvec->dev, "couldn't request irq\n");
		goto failed;
	}
455
	disable_irq(nvec->irq);
456 457 458

	tegra_init_i2c_slave(nvec);

459 460
	clk_enable(i2c_clk);

461 462 463
	gpio_direction_output(nvec->gpio, 1);
	gpio_set_value(nvec->gpio, 1);

464 465
	/* enable event reporting */
	nvec_write_async(nvec, EC_ENABLE_EVENT_REPORTING,
466
			 sizeof(EC_ENABLE_EVENT_REPORTING));
467 468 469 470 471 472 473 474 475

	nvec->nvec_status_notifier.notifier_call = nvec_status_notifier;
	nvec_register_notifier(nvec, &nvec->nvec_status_notifier, 0);

	nvec_power_handle = nvec;
	pm_power_off = nvec_power_off;

	/* Get Firmware Version */
	msg = nvec_write_sync(nvec, EC_GET_FIRMWARE_VERSION,
476
			      sizeof(EC_GET_FIRMWARE_VERSION));
477 478

	dev_warn(nvec->dev, "ec firmware version %02x.%02x.%02x / %02x\n",
479
		 msg->data[4], msg->data[5], msg->data[6], msg->data[7]);
480 481 482 483

	kfree(msg->data);
	kfree(msg);

484
	ret = mfd_add_devices(nvec->dev, -1, nvec_devices,
485 486
			      ARRAY_SIZE(nvec_devices), base, 0);
	if (ret)
487 488
		dev_err(nvec->dev, "error adding subdevices\n");

489
	/* unmute speakers? */
490
	nvec_write_async(nvec, "\x0d\x10\x59\x95", 4);
491 492 493 494 495 496 497 498 499

	/* enable lid switch event */
	nvec_write_async(nvec, "\x01\x01\x01\x00\x00\x02\x00", 7);

	/* enable power button event */
	nvec_write_async(nvec, "\x01\x01\x01\x00\x00\x80\x00", 7);

	return 0;

500 501
err_iounmap:
	iounmap(base);
502 503 504 505 506 507 508
failed:
	kfree(nvec);
	return -ENOMEM;
}

static int __devexit tegra_nvec_remove(struct platform_device *pdev)
{
509 510 511 512 513 514 515 516 517
	struct nvec_chip *nvec = platform_get_drvdata(pdev);

	nvec_write_async(nvec, EC_DISABLE_EVENT_REPORTING, 3);
	mfd_remove_devices(nvec->dev);
	free_irq(nvec->irq, &nvec_interrupt);
	iounmap(nvec->base);
	gpio_free(nvec->gpio);
	kfree(nvec);

518 519 520 521 522 523 524 525 526 527 528 529
	return 0;
}

#ifdef CONFIG_PM

static int tegra_nvec_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct nvec_chip *nvec = platform_get_drvdata(pdev);

	dev_dbg(nvec->dev, "suspending\n");
	nvec_write_async(nvec, EC_DISABLE_EVENT_REPORTING, 3);
	nvec_write_async(nvec, "\x04\x02", 2);
530
	nvec_disable_i2c_slave(nvec);
531 532 533 534

	return 0;
}

535 536
static int tegra_nvec_resume(struct platform_device *pdev)
{
537 538 539
	struct nvec_chip *nvec = platform_get_drvdata(pdev);

	dev_dbg(nvec->dev, "resuming\n");
540
	tegra_init_i2c_slave(nvec);
541 542 543 544 545 546 547 548 549 550
	nvec_write_async(nvec, EC_ENABLE_EVENT_REPORTING, 3);

	return 0;
}

#else
#define tegra_nvec_suspend NULL
#define tegra_nvec_resume NULL
#endif

551 552 553
static struct platform_driver nvec_device_driver = {
	.probe   = tegra_nvec_probe,
	.remove  = __devexit_p(tegra_nvec_remove),
554
	.suspend = tegra_nvec_suspend,
555 556
	.resume  = tegra_nvec_resume,
	.driver  = {
557 558 559 560 561 562 563 564 565 566 567
		.name = "nvec",
		.owner = THIS_MODULE,
	}
};

static int __init tegra_nvec_init(void)
{
	return platform_driver_register(&nvec_device_driver);
}

module_init(tegra_nvec_init);
568

569
MODULE_ALIAS("platform:nvec");
570 571 572
MODULE_DESCRIPTION("NVIDIA compliant embedded controller interface");
MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
MODULE_LICENSE("GPL");