]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/nvec/nvec.c
staging: nvec: Reject incomplete messages
[karo-tx-linux.git] / drivers / staging / nvec / nvec.c
1 /*
2  * NVEC: NVIDIA compliant embedded controller interface
3  *
4  * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
5  *
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>
10  *
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.
14  *
15  */
16
17 /* #define DEBUG */
18
19 #include <asm/irq.h>
20
21 #include <linux/atomic.h>
22 #include <linux/completion.h>
23 #include <linux/interrupt.h>
24 #include <linux/io.h>
25 #include <linux/irq.h>
26 #include <linux/slab.h>
27 #include <linux/gpio.h>
28 #include <linux/serio.h>
29 #include <linux/delay.h>
30 #include <linux/input.h>
31 #include <linux/workqueue.h>
32 #include <linux/clk.h>
33
34 #include <linux/semaphore.h>
35 #include <linux/list.h>
36 #include <linux/notifier.h>
37 #include <linux/platform_device.h>
38 #include <linux/mfd/core.h>
39
40 #include <mach/iomap.h>
41 #include <mach/clk.h>
42
43 #include "nvec.h"
44
45 #define I2C_CNFG                        0x00
46 #define I2C_CNFG_PACKET_MODE_EN         (1<<10)
47 #define I2C_CNFG_NEW_MASTER_SFM         (1<<11)
48 #define I2C_CNFG_DEBOUNCE_CNT_SHIFT     12
49
50 #define I2C_SL_CNFG             0x20
51 #define I2C_SL_NEWL             (1<<2)
52 #define I2C_SL_NACK             (1<<1)
53 #define I2C_SL_RESP             (1<<0)
54 #define I2C_SL_IRQ              (1<<3)
55 #define END_TRANS               (1<<4)
56 #define RCVD                    (1<<2)
57 #define RNW                     (1<<1)
58
59 #define I2C_SL_RCVD             0x24
60 #define I2C_SL_STATUS           0x28
61 #define I2C_SL_ADDR1            0x2c
62 #define I2C_SL_ADDR2            0x30
63 #define I2C_SL_DELAY_COUNT      0x3c
64
65 /**
66  * enum nvec_msg_category - Message categories for nvec_msg_alloc()
67  * @NVEC_MSG_RX: The message is an incoming message (from EC)
68  * @NVEC_MSG_TX: The message is an outgoing message (to EC)
69  */
70 enum nvec_msg_category  {
71         NVEC_MSG_RX,
72         NVEC_MSG_TX,
73 };
74
75 static const unsigned char EC_DISABLE_EVENT_REPORTING[3] = "\x04\x00\x00";
76 static const unsigned char EC_ENABLE_EVENT_REPORTING[3]  = "\x04\x00\x01";
77 static const unsigned char EC_GET_FIRMWARE_VERSION[2]    = "\x07\x15";
78
79 static struct nvec_chip *nvec_power_handle;
80
81 static struct mfd_cell nvec_devices[] = {
82         {
83                 .name = "nvec-kbd",
84                 .id = 1,
85         },
86         {
87                 .name = "nvec-mouse",
88                 .id = 1,
89         },
90         {
91                 .name = "nvec-power",
92                 .id = 1,
93         },
94         {
95                 .name = "nvec-power",
96                 .id = 2,
97         },
98         {
99                 .name = "nvec-leds",
100                 .id = 1,
101         },
102 };
103
104 /**
105  * nvec_register_notifier - Register a notifier with nvec
106  * @nvec: A &struct nvec_chip
107  * @nb: The notifier block to register
108  *
109  * Registers a notifier with @nvec. The notifier will be added to an atomic
110  * notifier chain that is called for all received messages except those that
111  * correspond to a request initiated by nvec_write_sync().
112  */
113 int nvec_register_notifier(struct nvec_chip *nvec, struct notifier_block *nb,
114                            unsigned int events)
115 {
116         return atomic_notifier_chain_register(&nvec->notifier_list, nb);
117 }
118 EXPORT_SYMBOL_GPL(nvec_register_notifier);
119
120 /**
121  * nvec_status_notifier - The final notifier
122  *
123  * Prints a message about control events not handled in the notifier
124  * chain.
125  */
126 static int nvec_status_notifier(struct notifier_block *nb,
127                                 unsigned long event_type, void *data)
128 {
129         unsigned char *msg = (unsigned char *)data;
130
131         if (event_type != NVEC_CNTL)
132                 return NOTIFY_DONE;
133
134         printk(KERN_WARNING "unhandled msg type %ld\n", event_type);
135         print_hex_dump(KERN_WARNING, "payload: ", DUMP_PREFIX_NONE, 16, 1,
136                 msg, msg[1] + 2, true);
137
138         return NOTIFY_OK;
139 }
140
141 /**
142  * nvec_msg_alloc:
143  * @nvec: A &struct nvec_chip
144  * @category: Pool category, see &enum nvec_msg_category
145  *
146  * Allocate a single &struct nvec_msg object from the message pool of
147  * @nvec. The result shall be passed to nvec_msg_free() if no longer
148  * used.
149  *
150  * Outgoing messages are placed in the upper 75% of the pool, keeping the
151  * lower 25% available for RX buffers only. The reason is to prevent a
152  * situation where all buffers are full and a message is thus endlessly
153  * retried because the response could never be processed.
154  */
155 static struct nvec_msg *nvec_msg_alloc(struct nvec_chip *nvec,
156                                        enum nvec_msg_category category)
157 {
158         int i = (category == NVEC_MSG_TX) ? (NVEC_POOL_SIZE / 4) : 0;
159
160         for (; i < NVEC_POOL_SIZE; i++) {
161                 if (atomic_xchg(&nvec->msg_pool[i].used, 1) == 0) {
162                         dev_vdbg(nvec->dev, "INFO: Allocate %i\n", i);
163                         return &nvec->msg_pool[i];
164                 }
165         }
166
167         dev_err(nvec->dev, "could not allocate %s buffer\n",
168                 (category == NVEC_MSG_TX) ? "TX" : "RX");
169
170         return NULL;
171 }
172
173 /**
174  * nvec_msg_free:
175  * @nvec: A &struct nvec_chip
176  * @msg:  A message (must be allocated by nvec_msg_alloc() and belong to @nvec)
177  *
178  * Free the given message
179  */
180 inline void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg)
181 {
182         if (msg != &nvec->tx_scratch)
183                 dev_vdbg(nvec->dev, "INFO: Free %ti\n", msg - nvec->msg_pool);
184         atomic_set(&msg->used, 0);
185 }
186 EXPORT_SYMBOL_GPL(nvec_msg_free);
187
188 /**
189  * nvec_msg_is_event - Return %true if @msg is an event
190  * @msg: A message
191  */
192 static bool nvec_msg_is_event(struct nvec_msg *msg)
193 {
194         return msg->data[0] >> 7;
195 }
196
197 /**
198  * nvec_msg_size - Get the size of a message
199  * @msg: The message to get the size for
200  *
201  * This only works for received messages, not for outgoing messages.
202  */
203 static size_t nvec_msg_size(struct nvec_msg *msg)
204 {
205         bool is_event = nvec_msg_is_event(msg);
206         int event_length = (msg->data[0] & 0x60) >> 5;
207
208         /* for variable size, payload size in byte 1 + count (1) + cmd (1) */
209         if (!is_event || event_length == NVEC_VAR_SIZE)
210                 return (msg->pos || msg->size) ? (msg->data[1] + 2) : 0;
211         else if (event_length == NVEC_2BYTES)
212                 return 2;
213         else if (event_length == NVEC_3BYTES)
214                 return 3;
215         else
216                 return 0;
217 }
218
219 /**
220  * nvec_gpio_set_value - Set the GPIO value
221  * @nvec: A &struct nvec_chip
222  * @value: The value to write (0 or 1)
223  *
224  * Like gpio_set_value(), but generating debugging information
225  */
226 static void nvec_gpio_set_value(struct nvec_chip *nvec, int value)
227 {
228         dev_dbg(nvec->dev, "GPIO changed from %u to %u\n",
229                 gpio_get_value(nvec->gpio), value);
230         gpio_set_value(nvec->gpio, value);
231 }
232
233 /**
234  * nvec_write_async - Asynchronously write a message to NVEC
235  * @nvec: An nvec_chip instance
236  * @data: The message data, starting with the request type
237  * @size: The size of @data
238  *
239  * Queue a single message to be transferred to the embedded controller
240  * and return immediately.
241  *
242  * Returns: 0 on success, a negative error code on failure. If a failure
243  * occured, the nvec driver may print an error.
244  */
245 int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
246                         short size)
247 {
248         struct nvec_msg *msg;
249         unsigned long flags;
250
251         msg = nvec_msg_alloc(nvec, NVEC_MSG_TX);
252
253         if (msg == NULL)
254                 return -ENOMEM;
255
256         msg->data[0] = size;
257         memcpy(msg->data + 1, data, size);
258         msg->size = size + 1;
259
260         spin_lock_irqsave(&nvec->tx_lock, flags);
261         list_add_tail(&msg->node, &nvec->tx_data);
262         spin_unlock_irqrestore(&nvec->tx_lock, flags);
263
264         queue_work(nvec->wq, &nvec->tx_work);
265
266         return 0;
267 }
268 EXPORT_SYMBOL(nvec_write_async);
269
270 /**
271  * nvec_write_sync - Write a message to nvec and read the response
272  * @nvec: An &struct nvec_chip
273  * @data: The data to write
274  * @size: The size of @data
275  *
276  * This is similar to nvec_write_async(), but waits for the
277  * request to be answered before returning. This function
278  * uses a mutex and can thus not be called from e.g.
279  * interrupt handlers.
280  *
281  * Returns: A pointer to the response message on success,
282  * %NULL on failure. Free with nvec_msg_free() once no longer
283  * used.
284  */
285 struct nvec_msg *nvec_write_sync(struct nvec_chip *nvec,
286                 const unsigned char *data, short size)
287 {
288         struct nvec_msg *msg;
289
290         mutex_lock(&nvec->sync_write_mutex);
291
292         nvec->sync_write_pending = (data[1] << 8) + data[0];
293
294         if (nvec_write_async(nvec, data, size) < 0)
295                 return NULL;
296
297         dev_dbg(nvec->dev, "nvec_sync_write: 0x%04x\n",
298                                         nvec->sync_write_pending);
299         if (!(wait_for_completion_timeout(&nvec->sync_write,
300                                 msecs_to_jiffies(2000)))) {
301                 dev_warn(nvec->dev, "timeout waiting for sync write to complete\n");
302                 mutex_unlock(&nvec->sync_write_mutex);
303                 return NULL;
304         }
305
306         dev_dbg(nvec->dev, "nvec_sync_write: pong!\n");
307
308         msg = nvec->last_sync_msg;
309
310         mutex_unlock(&nvec->sync_write_mutex);
311
312         return msg;
313 }
314 EXPORT_SYMBOL(nvec_write_sync);
315
316 /**
317  * nvec_request_master - Process outgoing messages
318  * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
319  *
320  * Processes all outgoing requests by sending the request and awaiting the
321  * response, then continuing with the next request. Once a request has a
322  * matching response, it will be freed and removed from the list.
323  */
324 static void nvec_request_master(struct work_struct *work)
325 {
326         struct nvec_chip *nvec = container_of(work, struct nvec_chip, tx_work);
327         unsigned long flags;
328         long err;
329         struct nvec_msg *msg;
330
331         spin_lock_irqsave(&nvec->tx_lock, flags);
332         while (!list_empty(&nvec->tx_data)) {
333                 msg = list_first_entry(&nvec->tx_data, struct nvec_msg, node);
334                 spin_unlock_irqrestore(&nvec->tx_lock, flags);
335                 nvec_gpio_set_value(nvec, 0);
336                 err = wait_for_completion_interruptible_timeout(
337                                 &nvec->ec_transfer, msecs_to_jiffies(5000));
338
339                 if (err == 0) {
340                         dev_warn(nvec->dev, "timeout waiting for ec transfer\n");
341                         nvec_gpio_set_value(nvec, 1);
342                         msg->pos = 0;
343                 }
344
345                 spin_lock_irqsave(&nvec->tx_lock, flags);
346
347                 if (err > 0) {
348                         list_del_init(&msg->node);
349                         nvec_msg_free(nvec, msg);
350                 }
351         }
352         spin_unlock_irqrestore(&nvec->tx_lock, flags);
353 }
354
355 /**
356  * parse_msg - Print some information and call the notifiers on an RX message
357  * @nvec: A &struct nvec_chip
358  * @msg: A message received by @nvec
359  *
360  * Paarse some pieces of the message and then call the chain of notifiers
361  * registered via nvec_register_notifier.
362  */
363 static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg)
364 {
365         if ((msg->data[0] & 1 << 7) == 0 && msg->data[3]) {
366                 dev_err(nvec->dev, "ec responded %02x %02x %02x %02x\n",
367                         msg->data[0], msg->data[1], msg->data[2], msg->data[3]);
368                 return -EINVAL;
369         }
370
371         if ((msg->data[0] >> 7) == 1 && (msg->data[0] & 0x0f) == 5)
372                 print_hex_dump(KERN_WARNING, "ec system event ",
373                                 DUMP_PREFIX_NONE, 16, 1, msg->data,
374                                 msg->data[1] + 2, true);
375
376         atomic_notifier_call_chain(&nvec->notifier_list, msg->data[0] & 0x8f,
377                                    msg->data);
378
379         return 0;
380 }
381
382 /**
383  * nvec_dispatch - Process messages received from the EC
384  * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
385  *
386  * Process messages previously received from the EC and put into the RX
387  * queue of the &struct nvec_chip instance associated with @work.
388  */
389 static void nvec_dispatch(struct work_struct *work)
390 {
391         struct nvec_chip *nvec = container_of(work, struct nvec_chip, rx_work);
392         unsigned long flags;
393         struct nvec_msg *msg;
394
395         spin_lock_irqsave(&nvec->rx_lock, flags);
396         while (!list_empty(&nvec->rx_data)) {
397                 msg = list_first_entry(&nvec->rx_data, struct nvec_msg, node);
398                 list_del_init(&msg->node);
399                 spin_unlock_irqrestore(&nvec->rx_lock, flags);
400
401                 if (nvec->sync_write_pending ==
402                       (msg->data[2] << 8) + msg->data[0]) {
403                         dev_dbg(nvec->dev, "sync write completed!\n");
404                         nvec->sync_write_pending = 0;
405                         nvec->last_sync_msg = msg;
406                         complete(&nvec->sync_write);
407                 } else {
408                         parse_msg(nvec, msg);
409                         nvec_msg_free(nvec, msg);
410                 }
411                 spin_lock_irqsave(&nvec->rx_lock, flags);
412         }
413         spin_unlock_irqrestore(&nvec->rx_lock, flags);
414 }
415
416 /**
417  * nvec_tx_completed - Complete the current transfer
418  * @nvec: A &struct nvec_chip
419  *
420  * This is called when we have received an END_TRANS on a TX transfer.
421  */
422 static void nvec_tx_completed(struct nvec_chip *nvec)
423 {
424         /* We got an END_TRANS, let's skip this, maybe there's an event */
425         if (nvec->tx->pos != nvec->tx->size) {
426                 dev_err(nvec->dev, "premature END_TRANS, resending\n");
427                 nvec->tx->pos = 0;
428                 nvec_gpio_set_value(nvec, 0);
429         } else {
430                 nvec->state = 0;
431         }
432 }
433
434 /**
435  * nvec_rx_completed - Complete the current transfer
436  * @nvec: A &struct nvec_chip
437  *
438  * This is called when we have received an END_TRANS on a RX transfer.
439  */
440 static void nvec_rx_completed(struct nvec_chip *nvec)
441 {
442         if (nvec->rx->pos != nvec_msg_size(nvec->rx)) {
443                 dev_err(nvec->dev, "RX incomplete: Expected %u bytes, got %u\n",
444                            (uint) nvec_msg_size(nvec->rx),
445                            (uint) nvec->rx->pos);
446
447                 nvec_msg_free(nvec, nvec->rx);
448                 nvec->state = 0;
449                 return;
450         }
451
452         spin_lock(&nvec->rx_lock);
453
454         /* add the received data to the work list
455            and move the ring buffer pointer to the next entry */
456         list_add_tail(&nvec->rx->node, &nvec->rx_data);
457
458         spin_unlock(&nvec->rx_lock);
459
460         nvec->state = 0;
461
462         if (!nvec_msg_is_event(nvec->rx))
463                 complete(&nvec->ec_transfer);
464
465         queue_work(nvec->wq, &nvec->rx_work);
466 }
467
468 /**
469  * nvec_invalid_flags - Send an error message about invalid flags and jump
470  * @nvec: The nvec device
471  * @status: The status flags
472  * @reset: Whether we shall jump to state 0.
473  */
474 static void nvec_invalid_flags(struct nvec_chip *nvec, unsigned int status,
475                                bool reset)
476 {
477         dev_err(nvec->dev, "unexpected status flags 0x%02x during state %i\n",
478                 status, nvec->state);
479         if (reset)
480                 nvec->state = 0;
481 }
482
483 /**
484  * nvec_tx_set - Set the message to transfer (nvec->tx)
485  * @nvec: A &struct nvec_chip
486  *
487  * Gets the first entry from the tx_data list of @nvec and sets the
488  * tx member to it. If the tx_data list is empty, this uses the
489  * tx_scratch message to send a no operation message.
490  */
491 static void nvec_tx_set(struct nvec_chip *nvec)
492 {
493         spin_lock(&nvec->tx_lock);
494         if (list_empty(&nvec->tx_data)) {
495                 dev_err(nvec->dev, "empty tx - sending no-op\n");
496                 memcpy(nvec->tx_scratch.data, "\x02\x07\x02", 3);
497                 nvec->tx_scratch.size = 3;
498                 nvec->tx_scratch.pos = 0;
499                 nvec->tx = &nvec->tx_scratch;
500                 list_add_tail(&nvec->tx->node, &nvec->tx_data);
501         } else {
502                 nvec->tx = list_first_entry(&nvec->tx_data, struct nvec_msg,
503                                             node);
504                 nvec->tx->pos = 0;
505         }
506         spin_unlock(&nvec->tx_lock);
507
508         dev_dbg(nvec->dev, "Sending message of length %u, command 0x%x\n",
509                 (uint)nvec->tx->size, nvec->tx->data[1]);
510 }
511
512 /**
513  * nvec_interrupt - Interrupt handler
514  * @irq: The IRQ
515  * @dev: The nvec device
516  *
517  * Interrupt handler that fills our RX buffers and empties our TX
518  * buffers. This uses a finite state machine with ridiculous amounts
519  * of error checking, in order to be fairly reliable.
520  */
521 static irqreturn_t nvec_interrupt(int irq, void *dev)
522 {
523         unsigned long status;
524         unsigned int received = 0;
525         unsigned char to_send = 0xff;
526         const unsigned long irq_mask = I2C_SL_IRQ | END_TRANS | RCVD | RNW;
527         struct nvec_chip *nvec = dev;
528         unsigned int state = nvec->state;
529
530         status = readl(nvec->base + I2C_SL_STATUS);
531
532         /* Filter out some errors */
533         if ((status & irq_mask) == 0 && (status & ~irq_mask) != 0) {
534                 dev_err(nvec->dev, "unexpected irq mask %lx\n", status);
535                 return IRQ_HANDLED;
536         }
537         if ((status & I2C_SL_IRQ) == 0) {
538                 dev_err(nvec->dev, "Spurious IRQ\n");
539                 return IRQ_HANDLED;
540         }
541
542         /* The EC did not request a read, so it send us something, read it */
543         if ((status & RNW) == 0) {
544                 received = readl(nvec->base + I2C_SL_RCVD);
545                 if (status & RCVD)
546                         writel(0, nvec->base + I2C_SL_RCVD);
547         }
548
549         if (status == (I2C_SL_IRQ | RCVD))
550                 nvec->state = 0;
551
552         switch (nvec->state) {
553         case 0:         /* Verify that its a transfer start, the rest later */
554                 if (status != (I2C_SL_IRQ | RCVD))
555                         nvec_invalid_flags(nvec, status, false);
556                 break;
557         case 1:         /* command byte */
558                 if (status != I2C_SL_IRQ) {
559                         nvec_invalid_flags(nvec, status, true);
560                 } else {
561                         nvec->rx = nvec_msg_alloc(nvec, NVEC_MSG_RX);
562                         /* Should not happen in a normal world */
563                         if (unlikely(nvec->rx == NULL)) {
564                                 nvec->state = 0;
565                                 break;
566                         }
567                         nvec->rx->data[0] = received;
568                         nvec->rx->pos = 1;
569                         nvec->state = 2;
570                 }
571                 break;
572         case 2:         /* first byte after command */
573                 if (status == (I2C_SL_IRQ | RNW | RCVD)) {
574                         udelay(33);
575                         if (nvec->rx->data[0] != 0x01) {
576                                 dev_err(nvec->dev,
577                                         "Read without prior read command\n");
578                                 nvec->state = 0;
579                                 break;
580                         }
581                         nvec_msg_free(nvec, nvec->rx);
582                         nvec->state = 3;
583                         nvec_tx_set(nvec);
584                         BUG_ON(nvec->tx->size < 1);
585                         to_send = nvec->tx->data[0];
586                         nvec->tx->pos = 1;
587                 } else if (status == (I2C_SL_IRQ)) {
588                         BUG_ON(nvec->rx == NULL);
589                         nvec->rx->data[1] = received;
590                         nvec->rx->pos = 2;
591                         nvec->state = 4;
592                 } else {
593                         nvec_invalid_flags(nvec, status, true);
594                 }
595                 break;
596         case 3:         /* EC does a block read, we transmit data */
597                 if (status & END_TRANS) {
598                         nvec_tx_completed(nvec);
599                 } else if ((status & RNW) == 0 || (status & RCVD)) {
600                         nvec_invalid_flags(nvec, status, true);
601                 } else if (nvec->tx && nvec->tx->pos < nvec->tx->size) {
602                         to_send = nvec->tx->data[nvec->tx->pos++];
603                 } else {
604                         dev_err(nvec->dev, "tx buffer underflow on %p (%u > %u)\n",
605                                 nvec->tx,
606                                 (uint) (nvec->tx ? nvec->tx->pos : 0),
607                                 (uint) (nvec->tx ? nvec->tx->size : 0));
608                         nvec->state = 0;
609                 }
610                 break;
611         case 4:         /* EC does some write, we read the data */
612                 if ((status & (END_TRANS | RNW)) == END_TRANS)
613                         nvec_rx_completed(nvec);
614                 else if (status & (RNW | RCVD))
615                         nvec_invalid_flags(nvec, status, true);
616                 else if (nvec->rx && nvec->rx->pos < NVEC_MSG_SIZE)
617                         nvec->rx->data[nvec->rx->pos++] = received;
618                 else
619                         dev_err(nvec->dev,
620                                 "RX buffer overflow on %p: "
621                                 "Trying to write byte %u of %u\n",
622                                 nvec->rx, nvec->rx->pos, NVEC_MSG_SIZE);
623                 break;
624         default:
625                 nvec->state = 0;
626         }
627
628         /* If we are told that a new transfer starts, verify it */
629         if ((status & (RCVD | RNW)) == RCVD) {
630                 if (received != nvec->i2c_addr)
631                         dev_err(nvec->dev,
632                         "received address 0x%02x, expected 0x%02x\n",
633                         received, nvec->i2c_addr);
634                 nvec->state = 1;
635         }
636
637         /* Send data if requested, but not on end of transmission */
638         if ((status & (RNW | END_TRANS)) == RNW)
639                 writel(to_send, nvec->base + I2C_SL_RCVD);
640
641         /* If we have send the first byte */
642         if (status == (I2C_SL_IRQ | RNW | RCVD))
643                 nvec_gpio_set_value(nvec, 1);
644
645         dev_dbg(nvec->dev,
646                 "Handled: %s 0x%02x, %s 0x%02x in state %u [%s%s%s]\n",
647                 (status & RNW) == 0 ? "received" : "R=",
648                 received,
649                 (status & (RNW | END_TRANS)) ? "sent" : "S=",
650                 to_send,
651                 state,
652                 status & END_TRANS ? " END_TRANS" : "",
653                 status & RCVD ? " RCVD" : "",
654                 status & RNW ? " RNW" : "");
655
656         return IRQ_HANDLED;
657 }
658
659 static void tegra_init_i2c_slave(struct nvec_chip *nvec)
660 {
661         u32 val;
662
663         clk_enable(nvec->i2c_clk);
664
665         tegra_periph_reset_assert(nvec->i2c_clk);
666         udelay(2);
667         tegra_periph_reset_deassert(nvec->i2c_clk);
668
669         val = I2C_CNFG_NEW_MASTER_SFM | I2C_CNFG_PACKET_MODE_EN |
670             (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
671         writel(val, nvec->base + I2C_CNFG);
672
673         clk_set_rate(nvec->i2c_clk, 8 * 80000);
674
675         writel(I2C_SL_NEWL, nvec->base + I2C_SL_CNFG);
676         writel(0x1E, nvec->base + I2C_SL_DELAY_COUNT);
677
678         writel(nvec->i2c_addr>>1, nvec->base + I2C_SL_ADDR1);
679         writel(0, nvec->base + I2C_SL_ADDR2);
680
681         enable_irq(nvec->irq);
682
683         clk_disable(nvec->i2c_clk);
684 }
685
686 static void nvec_disable_i2c_slave(struct nvec_chip *nvec)
687 {
688         disable_irq(nvec->irq);
689         writel(I2C_SL_NEWL | I2C_SL_NACK, nvec->base + I2C_SL_CNFG);
690         clk_disable(nvec->i2c_clk);
691 }
692
693 static void nvec_power_off(void)
694 {
695         nvec_write_async(nvec_power_handle, EC_DISABLE_EVENT_REPORTING, 3);
696         nvec_write_async(nvec_power_handle, "\x04\x01", 2);
697 }
698
699 static int __devinit tegra_nvec_probe(struct platform_device *pdev)
700 {
701         int err, ret;
702         struct clk *i2c_clk;
703         struct nvec_platform_data *pdata = pdev->dev.platform_data;
704         struct nvec_chip *nvec;
705         struct nvec_msg *msg;
706         struct resource *res;
707         struct resource *iomem;
708         void __iomem *base;
709
710         nvec = kzalloc(sizeof(struct nvec_chip), GFP_KERNEL);
711         if (nvec == NULL) {
712                 dev_err(&pdev->dev, "failed to reserve memory\n");
713                 return -ENOMEM;
714         }
715         platform_set_drvdata(pdev, nvec);
716         nvec->dev = &pdev->dev;
717         nvec->gpio = pdata->gpio;
718         nvec->i2c_addr = pdata->i2c_addr;
719
720         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
721         if (!res) {
722                 dev_err(&pdev->dev, "no mem resource?\n");
723                 return -ENODEV;
724         }
725
726         iomem = request_mem_region(res->start, resource_size(res), pdev->name);
727         if (!iomem) {
728                 dev_err(&pdev->dev, "I2C region already claimed\n");
729                 return -EBUSY;
730         }
731
732         base = ioremap(iomem->start, resource_size(iomem));
733         if (!base) {
734                 dev_err(&pdev->dev, "Can't ioremap I2C region\n");
735                 return -ENOMEM;
736         }
737
738         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
739         if (!res) {
740                 dev_err(&pdev->dev, "no irq resource?\n");
741                 ret = -ENODEV;
742                 goto err_iounmap;
743         }
744
745         i2c_clk = clk_get_sys("tegra-i2c.2", NULL);
746         if (IS_ERR(i2c_clk)) {
747                 dev_err(nvec->dev, "failed to get controller clock\n");
748                 goto err_iounmap;
749         }
750
751         nvec->base = base;
752         nvec->irq = res->start;
753         nvec->i2c_clk = i2c_clk;
754         nvec->rx = &nvec->msg_pool[0];
755
756         /* Set the gpio to low when we've got something to say */
757         err = gpio_request(nvec->gpio, "nvec gpio");
758         if (err < 0)
759                 dev_err(nvec->dev, "couldn't request gpio\n");
760
761         ATOMIC_INIT_NOTIFIER_HEAD(&nvec->notifier_list);
762
763         init_completion(&nvec->sync_write);
764         init_completion(&nvec->ec_transfer);
765         mutex_init(&nvec->sync_write_mutex);
766         spin_lock_init(&nvec->tx_lock);
767         spin_lock_init(&nvec->rx_lock);
768         INIT_LIST_HEAD(&nvec->rx_data);
769         INIT_LIST_HEAD(&nvec->tx_data);
770         INIT_WORK(&nvec->rx_work, nvec_dispatch);
771         INIT_WORK(&nvec->tx_work, nvec_request_master);
772         nvec->wq = alloc_workqueue("nvec", WQ_NON_REENTRANT, 2);
773
774         err = request_irq(nvec->irq, nvec_interrupt, 0, "nvec", nvec);
775         if (err) {
776                 dev_err(nvec->dev, "couldn't request irq\n");
777                 goto failed;
778         }
779         disable_irq(nvec->irq);
780
781         tegra_init_i2c_slave(nvec);
782
783         clk_enable(i2c_clk);
784
785         gpio_direction_output(nvec->gpio, 1);
786         gpio_set_value(nvec->gpio, 1);
787
788         /* enable event reporting */
789         nvec_write_async(nvec, EC_ENABLE_EVENT_REPORTING,
790                          sizeof(EC_ENABLE_EVENT_REPORTING));
791
792         nvec->nvec_status_notifier.notifier_call = nvec_status_notifier;
793         nvec_register_notifier(nvec, &nvec->nvec_status_notifier, 0);
794
795         nvec_power_handle = nvec;
796         pm_power_off = nvec_power_off;
797
798         /* Get Firmware Version */
799         msg = nvec_write_sync(nvec, EC_GET_FIRMWARE_VERSION,
800                 sizeof(EC_GET_FIRMWARE_VERSION));
801
802         if (msg) {
803                 dev_warn(nvec->dev, "ec firmware version %02x.%02x.%02x / %02x\n",
804                         msg->data[4], msg->data[5], msg->data[6], msg->data[7]);
805
806                 nvec_msg_free(nvec, msg);
807         }
808
809         ret = mfd_add_devices(nvec->dev, -1, nvec_devices,
810                               ARRAY_SIZE(nvec_devices), base, 0);
811         if (ret)
812                 dev_err(nvec->dev, "error adding subdevices\n");
813
814         /* unmute speakers? */
815         nvec_write_async(nvec, "\x0d\x10\x59\x95", 4);
816
817         /* enable lid switch event */
818         nvec_write_async(nvec, "\x01\x01\x01\x00\x00\x02\x00", 7);
819
820         /* enable power button event */
821         nvec_write_async(nvec, "\x01\x01\x01\x00\x00\x80\x00", 7);
822
823         return 0;
824
825 err_iounmap:
826         iounmap(base);
827 failed:
828         kfree(nvec);
829         return -ENOMEM;
830 }
831
832 static int __devexit tegra_nvec_remove(struct platform_device *pdev)
833 {
834         struct nvec_chip *nvec = platform_get_drvdata(pdev);
835
836         nvec_write_async(nvec, EC_DISABLE_EVENT_REPORTING, 3);
837         mfd_remove_devices(nvec->dev);
838         free_irq(nvec->irq, &nvec_interrupt);
839         iounmap(nvec->base);
840         gpio_free(nvec->gpio);
841         destroy_workqueue(nvec->wq);
842         kfree(nvec);
843
844         return 0;
845 }
846
847 #ifdef CONFIG_PM
848
849 static int tegra_nvec_suspend(struct platform_device *pdev, pm_message_t state)
850 {
851         struct nvec_chip *nvec = platform_get_drvdata(pdev);
852
853         dev_dbg(nvec->dev, "suspending\n");
854         nvec_write_async(nvec, EC_DISABLE_EVENT_REPORTING, 3);
855         nvec_write_async(nvec, "\x04\x02", 2);
856         nvec_disable_i2c_slave(nvec);
857
858         return 0;
859 }
860
861 static int tegra_nvec_resume(struct platform_device *pdev)
862 {
863         struct nvec_chip *nvec = platform_get_drvdata(pdev);
864
865         dev_dbg(nvec->dev, "resuming\n");
866         tegra_init_i2c_slave(nvec);
867         nvec_write_async(nvec, EC_ENABLE_EVENT_REPORTING, 3);
868
869         return 0;
870 }
871
872 #else
873 #define tegra_nvec_suspend NULL
874 #define tegra_nvec_resume NULL
875 #endif
876
877 static struct platform_driver nvec_device_driver = {
878         .probe   = tegra_nvec_probe,
879         .remove  = __devexit_p(tegra_nvec_remove),
880         .suspend = tegra_nvec_suspend,
881         .resume  = tegra_nvec_resume,
882         .driver  = {
883                 .name = "nvec",
884                 .owner = THIS_MODULE,
885         }
886 };
887
888 static int __init tegra_nvec_init(void)
889 {
890         return platform_driver_register(&nvec_device_driver);
891 }
892
893 module_init(tegra_nvec_init);
894
895 MODULE_ALIAS("platform:nvec");
896 MODULE_DESCRIPTION("NVIDIA compliant embedded controller interface");
897 MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
898 MODULE_LICENSE("GPL");