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
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/atomic.h>
22 #include <linux/clk.h>
23 #include <linux/completion.h>
24 #include <linux/delay.h>
25 #include <linux/err.h>
26 #include <linux/gpio.h>
27 #include <linux/interrupt.h>
29 #include <linux/irq.h>
31 #include <linux/of_gpio.h>
32 #include <linux/list.h>
33 #include <linux/mfd/core.h>
34 #include <linux/mutex.h>
35 #include <linux/notifier.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <linux/workqueue.h>
39 #include <linux/clk/tegra.h>
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_NEWSL (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)
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
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
{
73 enum nvec_sleep_subcmds
{
79 #define CNF_EVENT_REPORTING 0x01
80 #define GET_FIRMWARE_VERSION 0x15
81 #define LID_SWITCH BIT(1)
82 #define PWR_BUTTON BIT(15)
84 static struct nvec_chip
*nvec_power_handle
;
86 static struct mfd_cell nvec_devices
[] = {
100 .name
= "nvec-power",
104 .name
= "nvec-paz00",
110 * nvec_register_notifier - Register a notifier with nvec
111 * @nvec: A &struct nvec_chip
112 * @nb: The notifier block to register
114 * Registers a notifier with @nvec. The notifier will be added to an atomic
115 * notifier chain that is called for all received messages except those that
116 * correspond to a request initiated by nvec_write_sync().
118 int nvec_register_notifier(struct nvec_chip
*nvec
, struct notifier_block
*nb
,
121 return atomic_notifier_chain_register(&nvec
->notifier_list
, nb
);
123 EXPORT_SYMBOL_GPL(nvec_register_notifier
);
126 * nvec_unregister_notifier - Unregister a notifier with nvec
127 * @nvec: A &struct nvec_chip
128 * @nb: The notifier block to unregister
130 * Unregisters a notifier with @nvec. The notifier will be removed from the
131 * atomic notifier chain.
133 int nvec_unregister_notifier(struct nvec_chip
*nvec
, struct notifier_block
*nb
)
135 return atomic_notifier_chain_unregister(&nvec
->notifier_list
, nb
);
137 EXPORT_SYMBOL_GPL(nvec_unregister_notifier
);
140 * nvec_status_notifier - The final notifier
142 * Prints a message about control events not handled in the notifier
145 static int nvec_status_notifier(struct notifier_block
*nb
,
146 unsigned long event_type
, void *data
)
148 struct nvec_chip
*nvec
= container_of(nb
, struct nvec_chip
,
149 nvec_status_notifier
);
150 unsigned char *msg
= (unsigned char *)data
;
152 if (event_type
!= NVEC_CNTL
)
155 dev_warn(nvec
->dev
, "unhandled msg type %ld\n", event_type
);
156 print_hex_dump(KERN_WARNING
, "payload: ", DUMP_PREFIX_NONE
, 16, 1,
157 msg
, msg
[1] + 2, true);
164 * @nvec: A &struct nvec_chip
165 * @category: Pool category, see &enum nvec_msg_category
167 * Allocate a single &struct nvec_msg object from the message pool of
168 * @nvec. The result shall be passed to nvec_msg_free() if no longer
171 * Outgoing messages are placed in the upper 75% of the pool, keeping the
172 * lower 25% available for RX buffers only. The reason is to prevent a
173 * situation where all buffers are full and a message is thus endlessly
174 * retried because the response could never be processed.
176 static struct nvec_msg
*nvec_msg_alloc(struct nvec_chip
*nvec
,
177 enum nvec_msg_category category
)
179 int i
= (category
== NVEC_MSG_TX
) ? (NVEC_POOL_SIZE
/ 4) : 0;
181 for (; i
< NVEC_POOL_SIZE
; i
++) {
182 if (atomic_xchg(&nvec
->msg_pool
[i
].used
, 1) == 0) {
183 dev_vdbg(nvec
->dev
, "INFO: Allocate %i\n", i
);
184 return &nvec
->msg_pool
[i
];
188 dev_err(nvec
->dev
, "could not allocate %s buffer\n",
189 (category
== NVEC_MSG_TX
) ? "TX" : "RX");
196 * @nvec: A &struct nvec_chip
197 * @msg: A message (must be allocated by nvec_msg_alloc() and belong to @nvec)
199 * Free the given message
201 void nvec_msg_free(struct nvec_chip
*nvec
, struct nvec_msg
*msg
)
203 if (msg
!= &nvec
->tx_scratch
)
204 dev_vdbg(nvec
->dev
, "INFO: Free %ti\n", msg
- nvec
->msg_pool
);
205 atomic_set(&msg
->used
, 0);
207 EXPORT_SYMBOL_GPL(nvec_msg_free
);
210 * nvec_msg_is_event - Return %true if @msg is an event
213 static bool nvec_msg_is_event(struct nvec_msg
*msg
)
215 return msg
->data
[0] >> 7;
219 * nvec_msg_size - Get the size of a message
220 * @msg: The message to get the size for
222 * This only works for received messages, not for outgoing messages.
224 static size_t nvec_msg_size(struct nvec_msg
*msg
)
226 bool is_event
= nvec_msg_is_event(msg
);
227 int event_length
= (msg
->data
[0] & 0x60) >> 5;
229 /* for variable size, payload size in byte 1 + count (1) + cmd (1) */
230 if (!is_event
|| event_length
== NVEC_VAR_SIZE
)
231 return (msg
->pos
|| msg
->size
) ? (msg
->data
[1] + 2) : 0;
232 else if (event_length
== NVEC_2BYTES
)
234 else if (event_length
== NVEC_3BYTES
)
241 * nvec_gpio_set_value - Set the GPIO value
242 * @nvec: A &struct nvec_chip
243 * @value: The value to write (0 or 1)
245 * Like gpio_set_value(), but generating debugging information
247 static void nvec_gpio_set_value(struct nvec_chip
*nvec
, int value
)
249 dev_dbg(nvec
->dev
, "GPIO changed from %u to %u\n",
250 gpio_get_value(nvec
->gpio
), value
);
251 gpio_set_value(nvec
->gpio
, value
);
255 * nvec_write_async - Asynchronously write a message to NVEC
256 * @nvec: An nvec_chip instance
257 * @data: The message data, starting with the request type
258 * @size: The size of @data
260 * Queue a single message to be transferred to the embedded controller
261 * and return immediately.
263 * Returns: 0 on success, a negative error code on failure. If a failure
264 * occured, the nvec driver may print an error.
266 int nvec_write_async(struct nvec_chip
*nvec
, const unsigned char *data
,
269 struct nvec_msg
*msg
;
272 msg
= nvec_msg_alloc(nvec
, NVEC_MSG_TX
);
278 memcpy(msg
->data
+ 1, data
, size
);
279 msg
->size
= size
+ 1;
281 spin_lock_irqsave(&nvec
->tx_lock
, flags
);
282 list_add_tail(&msg
->node
, &nvec
->tx_data
);
283 spin_unlock_irqrestore(&nvec
->tx_lock
, flags
);
285 schedule_work(&nvec
->tx_work
);
289 EXPORT_SYMBOL(nvec_write_async
);
292 * nvec_write_sync - Write a message to nvec and read the response
293 * @nvec: An &struct nvec_chip
294 * @data: The data to write
295 * @size: The size of @data
297 * This is similar to nvec_write_async(), but waits for the
298 * request to be answered before returning. This function
299 * uses a mutex and can thus not be called from e.g.
300 * interrupt handlers.
302 * Returns: A pointer to the response message on success,
303 * %NULL on failure. Free with nvec_msg_free() once no longer
306 struct nvec_msg
*nvec_write_sync(struct nvec_chip
*nvec
,
307 const unsigned char *data
, short size
)
309 struct nvec_msg
*msg
;
311 mutex_lock(&nvec
->sync_write_mutex
);
313 nvec
->sync_write_pending
= (data
[1] << 8) + data
[0];
315 if (nvec_write_async(nvec
, data
, size
) < 0) {
316 mutex_unlock(&nvec
->sync_write_mutex
);
320 dev_dbg(nvec
->dev
, "nvec_sync_write: 0x%04x\n",
321 nvec
->sync_write_pending
);
322 if (!(wait_for_completion_timeout(&nvec
->sync_write
,
323 msecs_to_jiffies(2000)))) {
324 dev_warn(nvec
->dev
, "timeout waiting for sync write to complete\n");
325 mutex_unlock(&nvec
->sync_write_mutex
);
329 dev_dbg(nvec
->dev
, "nvec_sync_write: pong!\n");
331 msg
= nvec
->last_sync_msg
;
333 mutex_unlock(&nvec
->sync_write_mutex
);
337 EXPORT_SYMBOL(nvec_write_sync
);
340 * nvec_toggle_global_events - enables or disables global event reporting
342 * @state: true for enable, false for disable
344 * This switches on/off global event reports by the embedded controller.
346 static void nvec_toggle_global_events(struct nvec_chip
*nvec
, bool state
)
348 unsigned char global_events
[] = { NVEC_SLEEP
, GLOBAL_EVENTS
, state
};
350 nvec_write_async(nvec
, global_events
, 3);
354 * nvec_event_mask - fill the command string with event bitfield
355 * ev: points to event command string
356 * mask: bit to insert into the event mask
358 * Configure event command expects a 32 bit bitfield which describes
359 * which events to enable. The bitfield has the following structure
360 * (from highest byte to lowest):
361 * system state bits 7-0
362 * system state bits 15-8
363 * oem system state bits 7-0
364 * oem system state bits 15-8
366 static void nvec_event_mask(char *ev
, u32 mask
)
368 ev
[3] = mask
>> 16 & 0xff;
369 ev
[4] = mask
>> 24 & 0xff;
370 ev
[5] = mask
>> 0 & 0xff;
371 ev
[6] = mask
>> 8 & 0xff;
375 * nvec_request_master - Process outgoing messages
376 * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
378 * Processes all outgoing requests by sending the request and awaiting the
379 * response, then continuing with the next request. Once a request has a
380 * matching response, it will be freed and removed from the list.
382 static void nvec_request_master(struct work_struct
*work
)
384 struct nvec_chip
*nvec
= container_of(work
, struct nvec_chip
, tx_work
);
387 struct nvec_msg
*msg
;
389 spin_lock_irqsave(&nvec
->tx_lock
, flags
);
390 while (!list_empty(&nvec
->tx_data
)) {
391 msg
= list_first_entry(&nvec
->tx_data
, struct nvec_msg
, node
);
392 spin_unlock_irqrestore(&nvec
->tx_lock
, flags
);
393 nvec_gpio_set_value(nvec
, 0);
394 err
= wait_for_completion_interruptible_timeout(
395 &nvec
->ec_transfer
, msecs_to_jiffies(5000));
398 dev_warn(nvec
->dev
, "timeout waiting for ec transfer\n");
399 nvec_gpio_set_value(nvec
, 1);
403 spin_lock_irqsave(&nvec
->tx_lock
, flags
);
406 list_del_init(&msg
->node
);
407 nvec_msg_free(nvec
, msg
);
410 spin_unlock_irqrestore(&nvec
->tx_lock
, flags
);
414 * parse_msg - Print some information and call the notifiers on an RX message
415 * @nvec: A &struct nvec_chip
416 * @msg: A message received by @nvec
418 * Paarse some pieces of the message and then call the chain of notifiers
419 * registered via nvec_register_notifier.
421 static int parse_msg(struct nvec_chip
*nvec
, struct nvec_msg
*msg
)
423 if ((msg
->data
[0] & 1 << 7) == 0 && msg
->data
[3]) {
424 dev_err(nvec
->dev
, "ec responded %*ph\n", 4, msg
->data
);
428 if ((msg
->data
[0] >> 7) == 1 && (msg
->data
[0] & 0x0f) == 5)
429 print_hex_dump(KERN_WARNING
, "ec system event ",
430 DUMP_PREFIX_NONE
, 16, 1, msg
->data
,
431 msg
->data
[1] + 2, true);
433 atomic_notifier_call_chain(&nvec
->notifier_list
, msg
->data
[0] & 0x8f,
440 * nvec_dispatch - Process messages received from the EC
441 * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
443 * Process messages previously received from the EC and put into the RX
444 * queue of the &struct nvec_chip instance associated with @work.
446 static void nvec_dispatch(struct work_struct
*work
)
448 struct nvec_chip
*nvec
= container_of(work
, struct nvec_chip
, rx_work
);
450 struct nvec_msg
*msg
;
452 spin_lock_irqsave(&nvec
->rx_lock
, flags
);
453 while (!list_empty(&nvec
->rx_data
)) {
454 msg
= list_first_entry(&nvec
->rx_data
, struct nvec_msg
, node
);
455 list_del_init(&msg
->node
);
456 spin_unlock_irqrestore(&nvec
->rx_lock
, flags
);
458 if (nvec
->sync_write_pending
==
459 (msg
->data
[2] << 8) + msg
->data
[0]) {
460 dev_dbg(nvec
->dev
, "sync write completed!\n");
461 nvec
->sync_write_pending
= 0;
462 nvec
->last_sync_msg
= msg
;
463 complete(&nvec
->sync_write
);
465 parse_msg(nvec
, msg
);
466 nvec_msg_free(nvec
, msg
);
468 spin_lock_irqsave(&nvec
->rx_lock
, flags
);
470 spin_unlock_irqrestore(&nvec
->rx_lock
, flags
);
474 * nvec_tx_completed - Complete the current transfer
475 * @nvec: A &struct nvec_chip
477 * This is called when we have received an END_TRANS on a TX transfer.
479 static void nvec_tx_completed(struct nvec_chip
*nvec
)
481 /* We got an END_TRANS, let's skip this, maybe there's an event */
482 if (nvec
->tx
->pos
!= nvec
->tx
->size
) {
483 dev_err(nvec
->dev
, "premature END_TRANS, resending\n");
485 nvec_gpio_set_value(nvec
, 0);
492 * nvec_rx_completed - Complete the current transfer
493 * @nvec: A &struct nvec_chip
495 * This is called when we have received an END_TRANS on a RX transfer.
497 static void nvec_rx_completed(struct nvec_chip
*nvec
)
499 if (nvec
->rx
->pos
!= nvec_msg_size(nvec
->rx
)) {
500 dev_err(nvec
->dev
, "RX incomplete: Expected %u bytes, got %u\n",
501 (uint
) nvec_msg_size(nvec
->rx
),
502 (uint
) nvec
->rx
->pos
);
504 nvec_msg_free(nvec
, nvec
->rx
);
507 /* Battery quirk - Often incomplete, and likes to crash */
508 if (nvec
->rx
->data
[0] == NVEC_BAT
)
509 complete(&nvec
->ec_transfer
);
514 spin_lock(&nvec
->rx_lock
);
516 /* add the received data to the work list
517 and move the ring buffer pointer to the next entry */
518 list_add_tail(&nvec
->rx
->node
, &nvec
->rx_data
);
520 spin_unlock(&nvec
->rx_lock
);
524 if (!nvec_msg_is_event(nvec
->rx
))
525 complete(&nvec
->ec_transfer
);
527 schedule_work(&nvec
->rx_work
);
531 * nvec_invalid_flags - Send an error message about invalid flags and jump
532 * @nvec: The nvec device
533 * @status: The status flags
534 * @reset: Whether we shall jump to state 0.
536 static void nvec_invalid_flags(struct nvec_chip
*nvec
, unsigned int status
,
539 dev_err(nvec
->dev
, "unexpected status flags 0x%02x during state %i\n",
540 status
, nvec
->state
);
546 * nvec_tx_set - Set the message to transfer (nvec->tx)
547 * @nvec: A &struct nvec_chip
549 * Gets the first entry from the tx_data list of @nvec and sets the
550 * tx member to it. If the tx_data list is empty, this uses the
551 * tx_scratch message to send a no operation message.
553 static void nvec_tx_set(struct nvec_chip
*nvec
)
555 spin_lock(&nvec
->tx_lock
);
556 if (list_empty(&nvec
->tx_data
)) {
557 dev_err(nvec
->dev
, "empty tx - sending no-op\n");
558 memcpy(nvec
->tx_scratch
.data
, "\x02\x07\x02", 3);
559 nvec
->tx_scratch
.size
= 3;
560 nvec
->tx_scratch
.pos
= 0;
561 nvec
->tx
= &nvec
->tx_scratch
;
562 list_add_tail(&nvec
->tx
->node
, &nvec
->tx_data
);
564 nvec
->tx
= list_first_entry(&nvec
->tx_data
, struct nvec_msg
,
568 spin_unlock(&nvec
->tx_lock
);
570 dev_dbg(nvec
->dev
, "Sending message of length %u, command 0x%x\n",
571 (uint
)nvec
->tx
->size
, nvec
->tx
->data
[1]);
575 * nvec_interrupt - Interrupt handler
577 * @dev: The nvec device
579 * Interrupt handler that fills our RX buffers and empties our TX
580 * buffers. This uses a finite state machine with ridiculous amounts
581 * of error checking, in order to be fairly reliable.
583 static irqreturn_t
nvec_interrupt(int irq
, void *dev
)
585 unsigned long status
;
586 unsigned int received
= 0;
587 unsigned char to_send
= 0xff;
588 const unsigned long irq_mask
= I2C_SL_IRQ
| END_TRANS
| RCVD
| RNW
;
589 struct nvec_chip
*nvec
= dev
;
590 unsigned int state
= nvec
->state
;
592 status
= readl(nvec
->base
+ I2C_SL_STATUS
);
594 /* Filter out some errors */
595 if ((status
& irq_mask
) == 0 && (status
& ~irq_mask
) != 0) {
596 dev_err(nvec
->dev
, "unexpected irq mask %lx\n", status
);
599 if ((status
& I2C_SL_IRQ
) == 0) {
600 dev_err(nvec
->dev
, "Spurious IRQ\n");
604 /* The EC did not request a read, so it send us something, read it */
605 if ((status
& RNW
) == 0) {
606 received
= readl(nvec
->base
+ I2C_SL_RCVD
);
608 writel(0, nvec
->base
+ I2C_SL_RCVD
);
611 if (status
== (I2C_SL_IRQ
| RCVD
))
614 switch (nvec
->state
) {
615 case 0: /* Verify that its a transfer start, the rest later */
616 if (status
!= (I2C_SL_IRQ
| RCVD
))
617 nvec_invalid_flags(nvec
, status
, false);
619 case 1: /* command byte */
620 if (status
!= I2C_SL_IRQ
) {
621 nvec_invalid_flags(nvec
, status
, true);
623 nvec
->rx
= nvec_msg_alloc(nvec
, NVEC_MSG_RX
);
624 /* Should not happen in a normal world */
625 if (unlikely(nvec
->rx
== NULL
)) {
629 nvec
->rx
->data
[0] = received
;
634 case 2: /* first byte after command */
635 if (status
== (I2C_SL_IRQ
| RNW
| RCVD
)) {
637 if (nvec
->rx
->data
[0] != 0x01) {
639 "Read without prior read command\n");
643 nvec_msg_free(nvec
, nvec
->rx
);
646 BUG_ON(nvec
->tx
->size
< 1);
647 to_send
= nvec
->tx
->data
[0];
649 } else if (status
== (I2C_SL_IRQ
)) {
650 BUG_ON(nvec
->rx
== NULL
);
651 nvec
->rx
->data
[1] = received
;
655 nvec_invalid_flags(nvec
, status
, true);
658 case 3: /* EC does a block read, we transmit data */
659 if (status
& END_TRANS
) {
660 nvec_tx_completed(nvec
);
661 } else if ((status
& RNW
) == 0 || (status
& RCVD
)) {
662 nvec_invalid_flags(nvec
, status
, true);
663 } else if (nvec
->tx
&& nvec
->tx
->pos
< nvec
->tx
->size
) {
664 to_send
= nvec
->tx
->data
[nvec
->tx
->pos
++];
666 dev_err(nvec
->dev
, "tx buffer underflow on %p (%u > %u)\n",
668 (uint
) (nvec
->tx
? nvec
->tx
->pos
: 0),
669 (uint
) (nvec
->tx
? nvec
->tx
->size
: 0));
673 case 4: /* EC does some write, we read the data */
674 if ((status
& (END_TRANS
| RNW
)) == END_TRANS
)
675 nvec_rx_completed(nvec
);
676 else if (status
& (RNW
| RCVD
))
677 nvec_invalid_flags(nvec
, status
, true);
678 else if (nvec
->rx
&& nvec
->rx
->pos
< NVEC_MSG_SIZE
)
679 nvec
->rx
->data
[nvec
->rx
->pos
++] = received
;
682 "RX buffer overflow on %p: "
683 "Trying to write byte %u of %u\n",
684 nvec
->rx
, nvec
->rx
->pos
, NVEC_MSG_SIZE
);
690 /* If we are told that a new transfer starts, verify it */
691 if ((status
& (RCVD
| RNW
)) == RCVD
) {
692 if (received
!= nvec
->i2c_addr
)
694 "received address 0x%02x, expected 0x%02x\n",
695 received
, nvec
->i2c_addr
);
699 /* Send data if requested, but not on end of transmission */
700 if ((status
& (RNW
| END_TRANS
)) == RNW
)
701 writel(to_send
, nvec
->base
+ I2C_SL_RCVD
);
703 /* If we have send the first byte */
704 if (status
== (I2C_SL_IRQ
| RNW
| RCVD
))
705 nvec_gpio_set_value(nvec
, 1);
708 "Handled: %s 0x%02x, %s 0x%02x in state %u [%s%s%s]\n",
709 (status
& RNW
) == 0 ? "received" : "R=",
711 (status
& (RNW
| END_TRANS
)) ? "sent" : "S=",
714 status
& END_TRANS
? " END_TRANS" : "",
715 status
& RCVD
? " RCVD" : "",
716 status
& RNW
? " RNW" : "");
720 * TODO: A correct fix needs to be found for this.
722 * We experience less incomplete messages with this delay than without
723 * it, but we don't know why. Help is appreciated.
730 static void tegra_init_i2c_slave(struct nvec_chip
*nvec
)
734 clk_prepare_enable(nvec
->i2c_clk
);
736 tegra_periph_reset_assert(nvec
->i2c_clk
);
738 tegra_periph_reset_deassert(nvec
->i2c_clk
);
740 val
= I2C_CNFG_NEW_MASTER_SFM
| I2C_CNFG_PACKET_MODE_EN
|
741 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT
);
742 writel(val
, nvec
->base
+ I2C_CNFG
);
744 clk_set_rate(nvec
->i2c_clk
, 8 * 80000);
746 writel(I2C_SL_NEWSL
, nvec
->base
+ I2C_SL_CNFG
);
747 writel(0x1E, nvec
->base
+ I2C_SL_DELAY_COUNT
);
749 writel(nvec
->i2c_addr
>>1, nvec
->base
+ I2C_SL_ADDR1
);
750 writel(0, nvec
->base
+ I2C_SL_ADDR2
);
752 enable_irq(nvec
->irq
);
755 #ifdef CONFIG_PM_SLEEP
756 static void nvec_disable_i2c_slave(struct nvec_chip
*nvec
)
758 disable_irq(nvec
->irq
);
759 writel(I2C_SL_NEWSL
| I2C_SL_NACK
, nvec
->base
+ I2C_SL_CNFG
);
760 clk_disable_unprepare(nvec
->i2c_clk
);
764 static void nvec_power_off(void)
766 char ap_pwr_down
[] = { NVEC_SLEEP
, AP_PWR_DOWN
};
768 nvec_toggle_global_events(nvec_power_handle
, false);
769 nvec_write_async(nvec_power_handle
, ap_pwr_down
, 2);
773 * Parse common device tree data
775 static int nvec_i2c_parse_dt_pdata(struct nvec_chip
*nvec
)
777 nvec
->gpio
= of_get_named_gpio(nvec
->dev
->of_node
, "request-gpios", 0);
779 if (nvec
->gpio
< 0) {
780 dev_err(nvec
->dev
, "no gpio specified");
784 if (of_property_read_u32(nvec
->dev
->of_node
, "slave-addr",
786 dev_err(nvec
->dev
, "no i2c address specified");
793 static int tegra_nvec_probe(struct platform_device
*pdev
)
797 struct nvec_chip
*nvec
;
798 struct nvec_msg
*msg
;
799 struct resource
*res
;
801 char get_firmware_version
[] = { NVEC_CNTL
, GET_FIRMWARE_VERSION
},
802 unmute_speakers
[] = { NVEC_OEM0
, 0x10, 0x59, 0x95 },
803 enable_event
[7] = { NVEC_SYS
, CNF_EVENT_REPORTING
, true };
805 if(!pdev
->dev
.of_node
) {
806 dev_err(&pdev
->dev
, "must be instantiated using device tree\n");
810 nvec
= devm_kzalloc(&pdev
->dev
, sizeof(struct nvec_chip
), GFP_KERNEL
);
812 dev_err(&pdev
->dev
, "failed to reserve memory\n");
815 platform_set_drvdata(pdev
, nvec
);
816 nvec
->dev
= &pdev
->dev
;
818 err
= nvec_i2c_parse_dt_pdata(nvec
);
822 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
823 base
= devm_ioremap_resource(&pdev
->dev
, res
);
825 return PTR_ERR(base
);
827 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
829 dev_err(&pdev
->dev
, "no irq resource?\n");
833 i2c_clk
= devm_clk_get(&pdev
->dev
, "div-clk");
834 if (IS_ERR(i2c_clk
)) {
835 dev_err(nvec
->dev
, "failed to get controller clock\n");
840 nvec
->irq
= res
->start
;
841 nvec
->i2c_clk
= i2c_clk
;
842 nvec
->rx
= &nvec
->msg_pool
[0];
844 ATOMIC_INIT_NOTIFIER_HEAD(&nvec
->notifier_list
);
846 init_completion(&nvec
->sync_write
);
847 init_completion(&nvec
->ec_transfer
);
848 mutex_init(&nvec
->sync_write_mutex
);
849 spin_lock_init(&nvec
->tx_lock
);
850 spin_lock_init(&nvec
->rx_lock
);
851 INIT_LIST_HEAD(&nvec
->rx_data
);
852 INIT_LIST_HEAD(&nvec
->tx_data
);
853 INIT_WORK(&nvec
->rx_work
, nvec_dispatch
);
854 INIT_WORK(&nvec
->tx_work
, nvec_request_master
);
856 err
= devm_gpio_request_one(&pdev
->dev
, nvec
->gpio
, GPIOF_OUT_INIT_HIGH
,
859 dev_err(nvec
->dev
, "couldn't request gpio\n");
863 err
= devm_request_irq(&pdev
->dev
, nvec
->irq
, nvec_interrupt
, 0,
866 dev_err(nvec
->dev
, "couldn't request irq\n");
869 disable_irq(nvec
->irq
);
871 tegra_init_i2c_slave(nvec
);
873 /* enable event reporting */
874 nvec_toggle_global_events(nvec
, true);
876 nvec
->nvec_status_notifier
.notifier_call
= nvec_status_notifier
;
877 nvec_register_notifier(nvec
, &nvec
->nvec_status_notifier
, 0);
879 nvec_power_handle
= nvec
;
880 pm_power_off
= nvec_power_off
;
882 /* Get Firmware Version */
883 msg
= nvec_write_sync(nvec
, get_firmware_version
, 2);
886 dev_warn(nvec
->dev
, "ec firmware version %02x.%02x.%02x / %02x\n",
887 msg
->data
[4], msg
->data
[5], msg
->data
[6], msg
->data
[7]);
889 nvec_msg_free(nvec
, msg
);
892 ret
= mfd_add_devices(nvec
->dev
, -1, nvec_devices
,
893 ARRAY_SIZE(nvec_devices
), base
, 0, NULL
);
895 dev_err(nvec
->dev
, "error adding subdevices\n");
897 /* unmute speakers? */
898 nvec_write_async(nvec
, unmute_speakers
, 4);
900 /* enable lid switch event */
901 nvec_event_mask(enable_event
, LID_SWITCH
);
902 nvec_write_async(nvec
, enable_event
, 7);
904 /* enable power button event */
905 nvec_event_mask(enable_event
, PWR_BUTTON
);
906 nvec_write_async(nvec
, enable_event
, 7);
911 static int tegra_nvec_remove(struct platform_device
*pdev
)
913 struct nvec_chip
*nvec
= platform_get_drvdata(pdev
);
915 nvec_toggle_global_events(nvec
, false);
916 mfd_remove_devices(nvec
->dev
);
917 nvec_unregister_notifier(nvec
, &nvec
->nvec_status_notifier
);
918 cancel_work_sync(&nvec
->rx_work
);
919 cancel_work_sync(&nvec
->tx_work
);
920 /* FIXME: needs check wether nvec is responsible for power off */
926 #ifdef CONFIG_PM_SLEEP
927 static int nvec_suspend(struct device
*dev
)
929 struct platform_device
*pdev
= to_platform_device(dev
);
930 struct nvec_chip
*nvec
= platform_get_drvdata(pdev
);
931 struct nvec_msg
*msg
;
932 char ap_suspend
[] = { NVEC_SLEEP
, AP_SUSPEND
};
934 dev_dbg(nvec
->dev
, "suspending\n");
936 /* keep these sync or you'll break suspend */
937 nvec_toggle_global_events(nvec
, false);
939 msg
= nvec_write_sync(nvec
, ap_suspend
, sizeof(ap_suspend
));
940 nvec_msg_free(nvec
, msg
);
942 nvec_disable_i2c_slave(nvec
);
947 static int nvec_resume(struct device
*dev
)
949 struct platform_device
*pdev
= to_platform_device(dev
);
950 struct nvec_chip
*nvec
= platform_get_drvdata(pdev
);
952 dev_dbg(nvec
->dev
, "resuming\n");
953 tegra_init_i2c_slave(nvec
);
954 nvec_toggle_global_events(nvec
, true);
960 static const SIMPLE_DEV_PM_OPS(nvec_pm_ops
, nvec_suspend
, nvec_resume
);
962 /* Match table for of_platform binding */
963 static const struct of_device_id nvidia_nvec_of_match
[] = {
964 { .compatible
= "nvidia,nvec", },
967 MODULE_DEVICE_TABLE(of
, nvidia_nvec_of_match
);
969 static struct platform_driver nvec_device_driver
= {
970 .probe
= tegra_nvec_probe
,
971 .remove
= tegra_nvec_remove
,
974 .owner
= THIS_MODULE
,
976 .of_match_table
= nvidia_nvec_of_match
,
980 module_platform_driver(nvec_device_driver
);
982 MODULE_ALIAS("platform:nvec");
983 MODULE_DESCRIPTION("NVIDIA compliant embedded controller interface");
984 MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
985 MODULE_LICENSE("GPL");