staging:iio:adc:ad7314 removal. Supported via hwmon.
[zen-stable.git] / drivers / staging / nvec / nvec.c
blobfb0f0959eafaea403c45c8fe8a96531789174b3e
1 /*
2 * NVEC: NVIDIA compliant embedded controller interface
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
6 * Authors: Pierre-Hugues Husson <phhusson@free.fr>
7 * Ilya Petrov <ilya.muromec@gmail.com>
8 * Marc Dietrich <marvin24@gmx.de>
9 * Julian Andres Klode <jak@jak-linux.org>
11 * This file is subject to the terms and conditions of the GNU General Public
12 * License. See the file "COPYING" in the main directory of this archive
13 * for more details.
17 /* #define DEBUG */
19 #include <linux/kernel.h>
20 #include <linux/atomic.h>
21 #include <linux/clk.h>
22 #include <linux/completion.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/gpio.h>
26 #include <linux/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/list.h>
30 #include <linux/mfd/core.h>
31 #include <linux/mutex.h>
32 #include <linux/notifier.h>
33 #include <linux/platform_device.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/workqueue.h>
38 #include <mach/clk.h>
39 #include <mach/iomap.h>
41 #include "nvec.h"
43 #define I2C_CNFG 0x00
44 #define I2C_CNFG_PACKET_MODE_EN (1<<10)
45 #define I2C_CNFG_NEW_MASTER_SFM (1<<11)
46 #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
48 #define I2C_SL_CNFG 0x20
49 #define I2C_SL_NEWL (1<<2)
50 #define I2C_SL_NACK (1<<1)
51 #define I2C_SL_RESP (1<<0)
52 #define I2C_SL_IRQ (1<<3)
53 #define END_TRANS (1<<4)
54 #define RCVD (1<<2)
55 #define RNW (1<<1)
57 #define I2C_SL_RCVD 0x24
58 #define I2C_SL_STATUS 0x28
59 #define I2C_SL_ADDR1 0x2c
60 #define I2C_SL_ADDR2 0x30
61 #define I2C_SL_DELAY_COUNT 0x3c
63 /**
64 * enum nvec_msg_category - Message categories for nvec_msg_alloc()
65 * @NVEC_MSG_RX: The message is an incoming message (from EC)
66 * @NVEC_MSG_TX: The message is an outgoing message (to EC)
68 enum nvec_msg_category {
69 NVEC_MSG_RX,
70 NVEC_MSG_TX,
73 static const unsigned char EC_DISABLE_EVENT_REPORTING[3] = "\x04\x00\x00";
74 static const unsigned char EC_ENABLE_EVENT_REPORTING[3] = "\x04\x00\x01";
75 static const unsigned char EC_GET_FIRMWARE_VERSION[2] = "\x07\x15";
77 static struct nvec_chip *nvec_power_handle;
79 static struct mfd_cell nvec_devices[] = {
81 .name = "nvec-kbd",
82 .id = 1,
85 .name = "nvec-mouse",
86 .id = 1,
89 .name = "nvec-power",
90 .id = 1,
93 .name = "nvec-power",
94 .id = 2,
97 .name = "nvec-leds",
98 .id = 1,
103 * nvec_register_notifier - Register a notifier with nvec
104 * @nvec: A &struct nvec_chip
105 * @nb: The notifier block to register
107 * Registers a notifier with @nvec. The notifier will be added to an atomic
108 * notifier chain that is called for all received messages except those that
109 * correspond to a request initiated by nvec_write_sync().
111 int nvec_register_notifier(struct nvec_chip *nvec, struct notifier_block *nb,
112 unsigned int events)
114 return atomic_notifier_chain_register(&nvec->notifier_list, nb);
116 EXPORT_SYMBOL_GPL(nvec_register_notifier);
119 * nvec_status_notifier - The final notifier
121 * Prints a message about control events not handled in the notifier
122 * chain.
124 static int nvec_status_notifier(struct notifier_block *nb,
125 unsigned long event_type, void *data)
127 unsigned char *msg = (unsigned char *)data;
129 if (event_type != NVEC_CNTL)
130 return NOTIFY_DONE;
132 printk(KERN_WARNING "unhandled msg type %ld\n", event_type);
133 print_hex_dump(KERN_WARNING, "payload: ", DUMP_PREFIX_NONE, 16, 1,
134 msg, msg[1] + 2, true);
136 return NOTIFY_OK;
140 * nvec_msg_alloc:
141 * @nvec: A &struct nvec_chip
142 * @category: Pool category, see &enum nvec_msg_category
144 * Allocate a single &struct nvec_msg object from the message pool of
145 * @nvec. The result shall be passed to nvec_msg_free() if no longer
146 * used.
148 * Outgoing messages are placed in the upper 75% of the pool, keeping the
149 * lower 25% available for RX buffers only. The reason is to prevent a
150 * situation where all buffers are full and a message is thus endlessly
151 * retried because the response could never be processed.
153 static struct nvec_msg *nvec_msg_alloc(struct nvec_chip *nvec,
154 enum nvec_msg_category category)
156 int i = (category == NVEC_MSG_TX) ? (NVEC_POOL_SIZE / 4) : 0;
158 for (; i < NVEC_POOL_SIZE; i++) {
159 if (atomic_xchg(&nvec->msg_pool[i].used, 1) == 0) {
160 dev_vdbg(nvec->dev, "INFO: Allocate %i\n", i);
161 return &nvec->msg_pool[i];
165 dev_err(nvec->dev, "could not allocate %s buffer\n",
166 (category == NVEC_MSG_TX) ? "TX" : "RX");
168 return NULL;
172 * nvec_msg_free:
173 * @nvec: A &struct nvec_chip
174 * @msg: A message (must be allocated by nvec_msg_alloc() and belong to @nvec)
176 * Free the given message
178 inline void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg)
180 if (msg != &nvec->tx_scratch)
181 dev_vdbg(nvec->dev, "INFO: Free %ti\n", msg - nvec->msg_pool);
182 atomic_set(&msg->used, 0);
184 EXPORT_SYMBOL_GPL(nvec_msg_free);
187 * nvec_msg_is_event - Return %true if @msg is an event
188 * @msg: A message
190 static bool nvec_msg_is_event(struct nvec_msg *msg)
192 return msg->data[0] >> 7;
196 * nvec_msg_size - Get the size of a message
197 * @msg: The message to get the size for
199 * This only works for received messages, not for outgoing messages.
201 static size_t nvec_msg_size(struct nvec_msg *msg)
203 bool is_event = nvec_msg_is_event(msg);
204 int event_length = (msg->data[0] & 0x60) >> 5;
206 /* for variable size, payload size in byte 1 + count (1) + cmd (1) */
207 if (!is_event || event_length == NVEC_VAR_SIZE)
208 return (msg->pos || msg->size) ? (msg->data[1] + 2) : 0;
209 else if (event_length == NVEC_2BYTES)
210 return 2;
211 else if (event_length == NVEC_3BYTES)
212 return 3;
213 else
214 return 0;
218 * nvec_gpio_set_value - Set the GPIO value
219 * @nvec: A &struct nvec_chip
220 * @value: The value to write (0 or 1)
222 * Like gpio_set_value(), but generating debugging information
224 static void nvec_gpio_set_value(struct nvec_chip *nvec, int value)
226 dev_dbg(nvec->dev, "GPIO changed from %u to %u\n",
227 gpio_get_value(nvec->gpio), value);
228 gpio_set_value(nvec->gpio, value);
232 * nvec_write_async - Asynchronously write a message to NVEC
233 * @nvec: An nvec_chip instance
234 * @data: The message data, starting with the request type
235 * @size: The size of @data
237 * Queue a single message to be transferred to the embedded controller
238 * and return immediately.
240 * Returns: 0 on success, a negative error code on failure. If a failure
241 * occured, the nvec driver may print an error.
243 int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
244 short size)
246 struct nvec_msg *msg;
247 unsigned long flags;
249 msg = nvec_msg_alloc(nvec, NVEC_MSG_TX);
251 if (msg == NULL)
252 return -ENOMEM;
254 msg->data[0] = size;
255 memcpy(msg->data + 1, data, size);
256 msg->size = size + 1;
258 spin_lock_irqsave(&nvec->tx_lock, flags);
259 list_add_tail(&msg->node, &nvec->tx_data);
260 spin_unlock_irqrestore(&nvec->tx_lock, flags);
262 queue_work(nvec->wq, &nvec->tx_work);
264 return 0;
266 EXPORT_SYMBOL(nvec_write_async);
269 * nvec_write_sync - Write a message to nvec and read the response
270 * @nvec: An &struct nvec_chip
271 * @data: The data to write
272 * @size: The size of @data
274 * This is similar to nvec_write_async(), but waits for the
275 * request to be answered before returning. This function
276 * uses a mutex and can thus not be called from e.g.
277 * interrupt handlers.
279 * Returns: A pointer to the response message on success,
280 * %NULL on failure. Free with nvec_msg_free() once no longer
281 * used.
283 struct nvec_msg *nvec_write_sync(struct nvec_chip *nvec,
284 const unsigned char *data, short size)
286 struct nvec_msg *msg;
288 mutex_lock(&nvec->sync_write_mutex);
290 nvec->sync_write_pending = (data[1] << 8) + data[0];
292 if (nvec_write_async(nvec, data, size) < 0)
293 return NULL;
295 dev_dbg(nvec->dev, "nvec_sync_write: 0x%04x\n",
296 nvec->sync_write_pending);
297 if (!(wait_for_completion_timeout(&nvec->sync_write,
298 msecs_to_jiffies(2000)))) {
299 dev_warn(nvec->dev, "timeout waiting for sync write to complete\n");
300 mutex_unlock(&nvec->sync_write_mutex);
301 return NULL;
304 dev_dbg(nvec->dev, "nvec_sync_write: pong!\n");
306 msg = nvec->last_sync_msg;
308 mutex_unlock(&nvec->sync_write_mutex);
310 return msg;
312 EXPORT_SYMBOL(nvec_write_sync);
315 * nvec_request_master - Process outgoing messages
316 * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
318 * Processes all outgoing requests by sending the request and awaiting the
319 * response, then continuing with the next request. Once a request has a
320 * matching response, it will be freed and removed from the list.
322 static void nvec_request_master(struct work_struct *work)
324 struct nvec_chip *nvec = container_of(work, struct nvec_chip, tx_work);
325 unsigned long flags;
326 long err;
327 struct nvec_msg *msg;
329 spin_lock_irqsave(&nvec->tx_lock, flags);
330 while (!list_empty(&nvec->tx_data)) {
331 msg = list_first_entry(&nvec->tx_data, struct nvec_msg, node);
332 spin_unlock_irqrestore(&nvec->tx_lock, flags);
333 nvec_gpio_set_value(nvec, 0);
334 err = wait_for_completion_interruptible_timeout(
335 &nvec->ec_transfer, msecs_to_jiffies(5000));
337 if (err == 0) {
338 dev_warn(nvec->dev, "timeout waiting for ec transfer\n");
339 nvec_gpio_set_value(nvec, 1);
340 msg->pos = 0;
343 spin_lock_irqsave(&nvec->tx_lock, flags);
345 if (err > 0) {
346 list_del_init(&msg->node);
347 nvec_msg_free(nvec, msg);
350 spin_unlock_irqrestore(&nvec->tx_lock, flags);
354 * parse_msg - Print some information and call the notifiers on an RX message
355 * @nvec: A &struct nvec_chip
356 * @msg: A message received by @nvec
358 * Paarse some pieces of the message and then call the chain of notifiers
359 * registered via nvec_register_notifier.
361 static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg)
363 if ((msg->data[0] & 1 << 7) == 0 && msg->data[3]) {
364 dev_err(nvec->dev, "ec responded %02x %02x %02x %02x\n",
365 msg->data[0], msg->data[1], msg->data[2], msg->data[3]);
366 return -EINVAL;
369 if ((msg->data[0] >> 7) == 1 && (msg->data[0] & 0x0f) == 5)
370 print_hex_dump(KERN_WARNING, "ec system event ",
371 DUMP_PREFIX_NONE, 16, 1, msg->data,
372 msg->data[1] + 2, true);
374 atomic_notifier_call_chain(&nvec->notifier_list, msg->data[0] & 0x8f,
375 msg->data);
377 return 0;
381 * nvec_dispatch - Process messages received from the EC
382 * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
384 * Process messages previously received from the EC and put into the RX
385 * queue of the &struct nvec_chip instance associated with @work.
387 static void nvec_dispatch(struct work_struct *work)
389 struct nvec_chip *nvec = container_of(work, struct nvec_chip, rx_work);
390 unsigned long flags;
391 struct nvec_msg *msg;
393 spin_lock_irqsave(&nvec->rx_lock, flags);
394 while (!list_empty(&nvec->rx_data)) {
395 msg = list_first_entry(&nvec->rx_data, struct nvec_msg, node);
396 list_del_init(&msg->node);
397 spin_unlock_irqrestore(&nvec->rx_lock, flags);
399 if (nvec->sync_write_pending ==
400 (msg->data[2] << 8) + msg->data[0]) {
401 dev_dbg(nvec->dev, "sync write completed!\n");
402 nvec->sync_write_pending = 0;
403 nvec->last_sync_msg = msg;
404 complete(&nvec->sync_write);
405 } else {
406 parse_msg(nvec, msg);
407 nvec_msg_free(nvec, msg);
409 spin_lock_irqsave(&nvec->rx_lock, flags);
411 spin_unlock_irqrestore(&nvec->rx_lock, flags);
415 * nvec_tx_completed - Complete the current transfer
416 * @nvec: A &struct nvec_chip
418 * This is called when we have received an END_TRANS on a TX transfer.
420 static void nvec_tx_completed(struct nvec_chip *nvec)
422 /* We got an END_TRANS, let's skip this, maybe there's an event */
423 if (nvec->tx->pos != nvec->tx->size) {
424 dev_err(nvec->dev, "premature END_TRANS, resending\n");
425 nvec->tx->pos = 0;
426 nvec_gpio_set_value(nvec, 0);
427 } else {
428 nvec->state = 0;
433 * nvec_rx_completed - Complete the current transfer
434 * @nvec: A &struct nvec_chip
436 * This is called when we have received an END_TRANS on a RX transfer.
438 static void nvec_rx_completed(struct nvec_chip *nvec)
440 if (nvec->rx->pos != nvec_msg_size(nvec->rx)) {
441 dev_err(nvec->dev, "RX incomplete: Expected %u bytes, got %u\n",
442 (uint) nvec_msg_size(nvec->rx),
443 (uint) nvec->rx->pos);
445 nvec_msg_free(nvec, nvec->rx);
446 nvec->state = 0;
448 /* Battery quirk - Often incomplete, and likes to crash */
449 if (nvec->rx->data[0] == NVEC_BAT)
450 complete(&nvec->ec_transfer);
452 return;
455 spin_lock(&nvec->rx_lock);
457 /* add the received data to the work list
458 and move the ring buffer pointer to the next entry */
459 list_add_tail(&nvec->rx->node, &nvec->rx_data);
461 spin_unlock(&nvec->rx_lock);
463 nvec->state = 0;
465 if (!nvec_msg_is_event(nvec->rx))
466 complete(&nvec->ec_transfer);
468 queue_work(nvec->wq, &nvec->rx_work);
472 * nvec_invalid_flags - Send an error message about invalid flags and jump
473 * @nvec: The nvec device
474 * @status: The status flags
475 * @reset: Whether we shall jump to state 0.
477 static void nvec_invalid_flags(struct nvec_chip *nvec, unsigned int status,
478 bool reset)
480 dev_err(nvec->dev, "unexpected status flags 0x%02x during state %i\n",
481 status, nvec->state);
482 if (reset)
483 nvec->state = 0;
487 * nvec_tx_set - Set the message to transfer (nvec->tx)
488 * @nvec: A &struct nvec_chip
490 * Gets the first entry from the tx_data list of @nvec and sets the
491 * tx member to it. If the tx_data list is empty, this uses the
492 * tx_scratch message to send a no operation message.
494 static void nvec_tx_set(struct nvec_chip *nvec)
496 spin_lock(&nvec->tx_lock);
497 if (list_empty(&nvec->tx_data)) {
498 dev_err(nvec->dev, "empty tx - sending no-op\n");
499 memcpy(nvec->tx_scratch.data, "\x02\x07\x02", 3);
500 nvec->tx_scratch.size = 3;
501 nvec->tx_scratch.pos = 0;
502 nvec->tx = &nvec->tx_scratch;
503 list_add_tail(&nvec->tx->node, &nvec->tx_data);
504 } else {
505 nvec->tx = list_first_entry(&nvec->tx_data, struct nvec_msg,
506 node);
507 nvec->tx->pos = 0;
509 spin_unlock(&nvec->tx_lock);
511 dev_dbg(nvec->dev, "Sending message of length %u, command 0x%x\n",
512 (uint)nvec->tx->size, nvec->tx->data[1]);
516 * nvec_interrupt - Interrupt handler
517 * @irq: The IRQ
518 * @dev: The nvec device
520 * Interrupt handler that fills our RX buffers and empties our TX
521 * buffers. This uses a finite state machine with ridiculous amounts
522 * of error checking, in order to be fairly reliable.
524 static irqreturn_t nvec_interrupt(int irq, void *dev)
526 unsigned long status;
527 unsigned int received = 0;
528 unsigned char to_send = 0xff;
529 const unsigned long irq_mask = I2C_SL_IRQ | END_TRANS | RCVD | RNW;
530 struct nvec_chip *nvec = dev;
531 unsigned int state = nvec->state;
533 status = readl(nvec->base + I2C_SL_STATUS);
535 /* Filter out some errors */
536 if ((status & irq_mask) == 0 && (status & ~irq_mask) != 0) {
537 dev_err(nvec->dev, "unexpected irq mask %lx\n", status);
538 return IRQ_HANDLED;
540 if ((status & I2C_SL_IRQ) == 0) {
541 dev_err(nvec->dev, "Spurious IRQ\n");
542 return IRQ_HANDLED;
545 /* The EC did not request a read, so it send us something, read it */
546 if ((status & RNW) == 0) {
547 received = readl(nvec->base + I2C_SL_RCVD);
548 if (status & RCVD)
549 writel(0, nvec->base + I2C_SL_RCVD);
552 if (status == (I2C_SL_IRQ | RCVD))
553 nvec->state = 0;
555 switch (nvec->state) {
556 case 0: /* Verify that its a transfer start, the rest later */
557 if (status != (I2C_SL_IRQ | RCVD))
558 nvec_invalid_flags(nvec, status, false);
559 break;
560 case 1: /* command byte */
561 if (status != I2C_SL_IRQ) {
562 nvec_invalid_flags(nvec, status, true);
563 } else {
564 nvec->rx = nvec_msg_alloc(nvec, NVEC_MSG_RX);
565 /* Should not happen in a normal world */
566 if (unlikely(nvec->rx == NULL)) {
567 nvec->state = 0;
568 break;
570 nvec->rx->data[0] = received;
571 nvec->rx->pos = 1;
572 nvec->state = 2;
574 break;
575 case 2: /* first byte after command */
576 if (status == (I2C_SL_IRQ | RNW | RCVD)) {
577 udelay(33);
578 if (nvec->rx->data[0] != 0x01) {
579 dev_err(nvec->dev,
580 "Read without prior read command\n");
581 nvec->state = 0;
582 break;
584 nvec_msg_free(nvec, nvec->rx);
585 nvec->state = 3;
586 nvec_tx_set(nvec);
587 BUG_ON(nvec->tx->size < 1);
588 to_send = nvec->tx->data[0];
589 nvec->tx->pos = 1;
590 } else if (status == (I2C_SL_IRQ)) {
591 BUG_ON(nvec->rx == NULL);
592 nvec->rx->data[1] = received;
593 nvec->rx->pos = 2;
594 nvec->state = 4;
595 } else {
596 nvec_invalid_flags(nvec, status, true);
598 break;
599 case 3: /* EC does a block read, we transmit data */
600 if (status & END_TRANS) {
601 nvec_tx_completed(nvec);
602 } else if ((status & RNW) == 0 || (status & RCVD)) {
603 nvec_invalid_flags(nvec, status, true);
604 } else if (nvec->tx && nvec->tx->pos < nvec->tx->size) {
605 to_send = nvec->tx->data[nvec->tx->pos++];
606 } else {
607 dev_err(nvec->dev, "tx buffer underflow on %p (%u > %u)\n",
608 nvec->tx,
609 (uint) (nvec->tx ? nvec->tx->pos : 0),
610 (uint) (nvec->tx ? nvec->tx->size : 0));
611 nvec->state = 0;
613 break;
614 case 4: /* EC does some write, we read the data */
615 if ((status & (END_TRANS | RNW)) == END_TRANS)
616 nvec_rx_completed(nvec);
617 else if (status & (RNW | RCVD))
618 nvec_invalid_flags(nvec, status, true);
619 else if (nvec->rx && nvec->rx->pos < NVEC_MSG_SIZE)
620 nvec->rx->data[nvec->rx->pos++] = received;
621 else
622 dev_err(nvec->dev,
623 "RX buffer overflow on %p: "
624 "Trying to write byte %u of %u\n",
625 nvec->rx, nvec->rx->pos, NVEC_MSG_SIZE);
626 break;
627 default:
628 nvec->state = 0;
631 /* If we are told that a new transfer starts, verify it */
632 if ((status & (RCVD | RNW)) == RCVD) {
633 if (received != nvec->i2c_addr)
634 dev_err(nvec->dev,
635 "received address 0x%02x, expected 0x%02x\n",
636 received, nvec->i2c_addr);
637 nvec->state = 1;
640 /* Send data if requested, but not on end of transmission */
641 if ((status & (RNW | END_TRANS)) == RNW)
642 writel(to_send, nvec->base + I2C_SL_RCVD);
644 /* If we have send the first byte */
645 if (status == (I2C_SL_IRQ | RNW | RCVD))
646 nvec_gpio_set_value(nvec, 1);
648 dev_dbg(nvec->dev,
649 "Handled: %s 0x%02x, %s 0x%02x in state %u [%s%s%s]\n",
650 (status & RNW) == 0 ? "received" : "R=",
651 received,
652 (status & (RNW | END_TRANS)) ? "sent" : "S=",
653 to_send,
654 state,
655 status & END_TRANS ? " END_TRANS" : "",
656 status & RCVD ? " RCVD" : "",
657 status & RNW ? " RNW" : "");
661 * TODO: A correct fix needs to be found for this.
663 * We experience less incomplete messages with this delay than without
664 * it, but we don't know why. Help is appreciated.
666 udelay(100);
668 return IRQ_HANDLED;
671 static void tegra_init_i2c_slave(struct nvec_chip *nvec)
673 u32 val;
675 clk_enable(nvec->i2c_clk);
677 tegra_periph_reset_assert(nvec->i2c_clk);
678 udelay(2);
679 tegra_periph_reset_deassert(nvec->i2c_clk);
681 val = I2C_CNFG_NEW_MASTER_SFM | I2C_CNFG_PACKET_MODE_EN |
682 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
683 writel(val, nvec->base + I2C_CNFG);
685 clk_set_rate(nvec->i2c_clk, 8 * 80000);
687 writel(I2C_SL_NEWL, nvec->base + I2C_SL_CNFG);
688 writel(0x1E, nvec->base + I2C_SL_DELAY_COUNT);
690 writel(nvec->i2c_addr>>1, nvec->base + I2C_SL_ADDR1);
691 writel(0, nvec->base + I2C_SL_ADDR2);
693 enable_irq(nvec->irq);
695 clk_disable(nvec->i2c_clk);
698 static void nvec_disable_i2c_slave(struct nvec_chip *nvec)
700 disable_irq(nvec->irq);
701 writel(I2C_SL_NEWL | I2C_SL_NACK, nvec->base + I2C_SL_CNFG);
702 clk_disable(nvec->i2c_clk);
705 static void nvec_power_off(void)
707 nvec_write_async(nvec_power_handle, EC_DISABLE_EVENT_REPORTING, 3);
708 nvec_write_async(nvec_power_handle, "\x04\x01", 2);
711 static int __devinit tegra_nvec_probe(struct platform_device *pdev)
713 int err, ret;
714 struct clk *i2c_clk;
715 struct nvec_platform_data *pdata = pdev->dev.platform_data;
716 struct nvec_chip *nvec;
717 struct nvec_msg *msg;
718 struct resource *res;
719 struct resource *iomem;
720 void __iomem *base;
722 nvec = kzalloc(sizeof(struct nvec_chip), GFP_KERNEL);
723 if (nvec == NULL) {
724 dev_err(&pdev->dev, "failed to reserve memory\n");
725 return -ENOMEM;
727 platform_set_drvdata(pdev, nvec);
728 nvec->dev = &pdev->dev;
729 nvec->gpio = pdata->gpio;
730 nvec->i2c_addr = pdata->i2c_addr;
732 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
733 if (!res) {
734 dev_err(&pdev->dev, "no mem resource?\n");
735 return -ENODEV;
738 iomem = request_mem_region(res->start, resource_size(res), pdev->name);
739 if (!iomem) {
740 dev_err(&pdev->dev, "I2C region already claimed\n");
741 return -EBUSY;
744 base = ioremap(iomem->start, resource_size(iomem));
745 if (!base) {
746 dev_err(&pdev->dev, "Can't ioremap I2C region\n");
747 return -ENOMEM;
750 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
751 if (!res) {
752 dev_err(&pdev->dev, "no irq resource?\n");
753 ret = -ENODEV;
754 goto err_iounmap;
757 i2c_clk = clk_get_sys("tegra-i2c.2", NULL);
758 if (IS_ERR(i2c_clk)) {
759 dev_err(nvec->dev, "failed to get controller clock\n");
760 goto err_iounmap;
763 nvec->base = base;
764 nvec->irq = res->start;
765 nvec->i2c_clk = i2c_clk;
766 nvec->rx = &nvec->msg_pool[0];
768 /* Set the gpio to low when we've got something to say */
769 err = gpio_request(nvec->gpio, "nvec gpio");
770 if (err < 0)
771 dev_err(nvec->dev, "couldn't request gpio\n");
773 ATOMIC_INIT_NOTIFIER_HEAD(&nvec->notifier_list);
775 init_completion(&nvec->sync_write);
776 init_completion(&nvec->ec_transfer);
777 mutex_init(&nvec->sync_write_mutex);
778 spin_lock_init(&nvec->tx_lock);
779 spin_lock_init(&nvec->rx_lock);
780 INIT_LIST_HEAD(&nvec->rx_data);
781 INIT_LIST_HEAD(&nvec->tx_data);
782 INIT_WORK(&nvec->rx_work, nvec_dispatch);
783 INIT_WORK(&nvec->tx_work, nvec_request_master);
784 nvec->wq = alloc_workqueue("nvec", WQ_NON_REENTRANT, 2);
786 err = request_irq(nvec->irq, nvec_interrupt, 0, "nvec", nvec);
787 if (err) {
788 dev_err(nvec->dev, "couldn't request irq\n");
789 goto failed;
791 disable_irq(nvec->irq);
793 tegra_init_i2c_slave(nvec);
795 clk_enable(i2c_clk);
797 gpio_direction_output(nvec->gpio, 1);
798 gpio_set_value(nvec->gpio, 1);
800 /* enable event reporting */
801 nvec_write_async(nvec, EC_ENABLE_EVENT_REPORTING,
802 sizeof(EC_ENABLE_EVENT_REPORTING));
804 nvec->nvec_status_notifier.notifier_call = nvec_status_notifier;
805 nvec_register_notifier(nvec, &nvec->nvec_status_notifier, 0);
807 nvec_power_handle = nvec;
808 pm_power_off = nvec_power_off;
810 /* Get Firmware Version */
811 msg = nvec_write_sync(nvec, EC_GET_FIRMWARE_VERSION,
812 sizeof(EC_GET_FIRMWARE_VERSION));
814 if (msg) {
815 dev_warn(nvec->dev, "ec firmware version %02x.%02x.%02x / %02x\n",
816 msg->data[4], msg->data[5], msg->data[6], msg->data[7]);
818 nvec_msg_free(nvec, msg);
821 ret = mfd_add_devices(nvec->dev, -1, nvec_devices,
822 ARRAY_SIZE(nvec_devices), base, 0);
823 if (ret)
824 dev_err(nvec->dev, "error adding subdevices\n");
826 /* unmute speakers? */
827 nvec_write_async(nvec, "\x0d\x10\x59\x95", 4);
829 /* enable lid switch event */
830 nvec_write_async(nvec, "\x01\x01\x01\x00\x00\x02\x00", 7);
832 /* enable power button event */
833 nvec_write_async(nvec, "\x01\x01\x01\x00\x00\x80\x00", 7);
835 return 0;
837 err_iounmap:
838 iounmap(base);
839 failed:
840 kfree(nvec);
841 return -ENOMEM;
844 static int __devexit tegra_nvec_remove(struct platform_device *pdev)
846 struct nvec_chip *nvec = platform_get_drvdata(pdev);
848 nvec_write_async(nvec, EC_DISABLE_EVENT_REPORTING, 3);
849 mfd_remove_devices(nvec->dev);
850 free_irq(nvec->irq, &nvec_interrupt);
851 iounmap(nvec->base);
852 gpio_free(nvec->gpio);
853 destroy_workqueue(nvec->wq);
854 kfree(nvec);
856 return 0;
859 #ifdef CONFIG_PM
861 static int tegra_nvec_suspend(struct platform_device *pdev, pm_message_t state)
863 struct nvec_chip *nvec = platform_get_drvdata(pdev);
864 struct nvec_msg *msg;
866 dev_dbg(nvec->dev, "suspending\n");
868 /* keep these sync or you'll break suspend */
869 msg = nvec_write_sync(nvec, EC_DISABLE_EVENT_REPORTING, 3);
870 nvec_msg_free(nvec, msg);
871 msg = nvec_write_sync(nvec, "\x04\x02", 2);
872 nvec_msg_free(nvec, msg);
874 nvec_disable_i2c_slave(nvec);
876 return 0;
879 static int tegra_nvec_resume(struct platform_device *pdev)
881 struct nvec_chip *nvec = platform_get_drvdata(pdev);
883 dev_dbg(nvec->dev, "resuming\n");
884 tegra_init_i2c_slave(nvec);
885 nvec_write_async(nvec, EC_ENABLE_EVENT_REPORTING, 3);
887 return 0;
890 #else
891 #define tegra_nvec_suspend NULL
892 #define tegra_nvec_resume NULL
893 #endif
895 static struct platform_driver nvec_device_driver = {
896 .probe = tegra_nvec_probe,
897 .remove = __devexit_p(tegra_nvec_remove),
898 .suspend = tegra_nvec_suspend,
899 .resume = tegra_nvec_resume,
900 .driver = {
901 .name = "nvec",
902 .owner = THIS_MODULE,
906 static int __init tegra_nvec_init(void)
908 return platform_driver_register(&nvec_device_driver);
911 module_init(tegra_nvec_init);
913 MODULE_ALIAS("platform:nvec");
914 MODULE_DESCRIPTION("NVIDIA compliant embedded controller interface");
915 MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
916 MODULE_LICENSE("GPL");