19 #include <linux/kernel.h>
20 #include <linux/module.h>
32 #include <linux/list.h>
37 #include <linux/slab.h>
47 #define I2C_CNFG_PACKET_MODE_EN (1<<10)
48 #define I2C_CNFG_NEW_MASTER_SFM (1<<11)
49 #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
51 #define I2C_SL_CNFG 0x20
52 #define I2C_SL_NEWSL (1<<2)
53 #define I2C_SL_NACK (1<<1)
54 #define I2C_SL_RESP (1<<0)
55 #define I2C_SL_IRQ (1<<3)
56 #define END_TRANS (1<<4)
60 #define I2C_SL_RCVD 0x24
61 #define I2C_SL_STATUS 0x28
62 #define I2C_SL_ADDR1 0x2c
63 #define I2C_SL_ADDR2 0x30
64 #define I2C_SL_DELAY_COUNT 0x3c
76 static const unsigned char EC_DISABLE_EVENT_REPORTING[3] =
"\x04\x00\x00";
77 static const unsigned char EC_ENABLE_EVENT_REPORTING[3] =
"\x04\x00\x01";
78 static const unsigned char EC_GET_FIRMWARE_VERSION[2] =
"\x07\x15";
80 static struct nvec_chip *nvec_power_handle;
82 static struct mfd_cell nvec_devices[] = {
100 .name =
"nvec-paz00",
132 unsigned char *
msg = (
unsigned char *)data;
137 dev_warn(nvec->
dev,
"unhandled msg type %ld\n", event_type);
139 msg, msg[1] + 2,
true);
170 dev_err(nvec->
dev,
"could not allocate %s buffer\n",
195 static bool nvec_msg_is_event(
struct nvec_msg *msg)
197 return msg->
data[0] >> 7;
206 static size_t nvec_msg_size(
struct nvec_msg *msg)
208 bool is_event = nvec_msg_is_event(msg);
209 int event_length = (msg->
data[0] & 0x60) >> 5;
213 return (msg->
pos || msg->
size) ? (msg->
data[1] + 2) : 0;
229 static void nvec_gpio_set_value(
struct nvec_chip *nvec,
int value)
231 dev_dbg(nvec->
dev,
"GPIO changed from %u to %u\n",
261 msg->
size = size + 1;
265 spin_unlock_irqrestore(&nvec->
tx_lock, flags);
289 const unsigned char *data,
short size)
302 dev_dbg(nvec->
dev,
"nvec_sync_write: 0x%04x\n",
306 dev_warn(nvec->
dev,
"timeout waiting for sync write to complete\n");
311 dev_dbg(nvec->
dev,
"nvec_sync_write: pong!\n");
337 while (!list_empty(&nvec->
tx_data)) {
339 spin_unlock_irqrestore(&nvec->
tx_lock, flags);
340 nvec_gpio_set_value(nvec, 0);
345 dev_warn(nvec->
dev,
"timeout waiting for ec transfer\n");
346 nvec_gpio_set_value(nvec, 1);
353 list_del_init(&msg->
node);
357 spin_unlock_irqrestore(&nvec->
tx_lock, flags);
370 if ((msg->
data[0] & 1 << 7) == 0 && msg->
data[3]) {
375 if ((msg->
data[0] >> 7) == 1 && (msg->
data[0] & 0x0f) == 5)
378 msg->
data[1] + 2,
true);
393 static void nvec_dispatch(
struct work_struct *work)
400 while (!list_empty(&nvec->rx_data)) {
402 list_del_init(&msg->
node);
403 spin_unlock_irqrestore(&nvec->
rx_lock, flags);
406 (msg->
data[2] << 8) + msg->
data[0]) {
407 dev_dbg(nvec->
dev,
"sync write completed!\n");
412 parse_msg(nvec, msg);
417 spin_unlock_irqrestore(&nvec->
rx_lock, flags);
426 static void nvec_tx_completed(
struct nvec_chip *nvec)
429 if (nvec->
tx->pos != nvec->
tx->size) {
430 dev_err(nvec->
dev,
"premature END_TRANS, resending\n");
432 nvec_gpio_set_value(nvec, 0);
444 static void nvec_rx_completed(
struct nvec_chip *nvec)
446 if (nvec->
rx->pos != nvec_msg_size(nvec->
rx)) {
447 dev_err(nvec->
dev,
"RX incomplete: Expected %u bytes, got %u\n",
448 (
uint) nvec_msg_size(nvec->
rx),
471 if (!nvec_msg_is_event(nvec->
rx))
483 static void nvec_invalid_flags(
struct nvec_chip *nvec,
unsigned int status,
486 dev_err(nvec->
dev,
"unexpected status flags 0x%02x during state %i\n",
487 status, nvec->
state);
500 static void nvec_tx_set(
struct nvec_chip *nvec)
503 if (list_empty(&nvec->
tx_data)) {
504 dev_err(nvec->
dev,
"empty tx - sending no-op\n");
517 dev_dbg(nvec->
dev,
"Sending message of length %u, command 0x%x\n",
518 (
uint)nvec->
tx->size, nvec->
tx->data[1]);
533 unsigned int received = 0;
534 unsigned char to_send = 0xff;
542 if ((status & irq_mask) == 0 && (status & ~irq_mask) != 0) {
543 dev_err(nvec->
dev,
"unexpected irq mask %lx\n", status);
552 if ((status &
RNW) == 0) {
558 if (status == (I2C_SL_IRQ |
RCVD))
561 switch (nvec->
state) {
563 if (status != (I2C_SL_IRQ |
RCVD))
564 nvec_invalid_flags(nvec, status,
false);
567 if (status != I2C_SL_IRQ) {
568 nvec_invalid_flags(nvec, status,
true);
576 nvec->
rx->data[0] = received;
582 if (status == (I2C_SL_IRQ | RNW |
RCVD)) {
584 if (nvec->
rx->data[0] != 0x01) {
586 "Read without prior read command\n");
594 to_send = nvec->
tx->data[0];
596 }
else if (status == (I2C_SL_IRQ)) {
598 nvec->
rx->data[1] = received;
602 nvec_invalid_flags(nvec, status,
true);
607 nvec_tx_completed(nvec);
608 }
else if ((status & RNW) == 0 || (status &
RCVD)) {
609 nvec_invalid_flags(nvec, status,
true);
610 }
else if (nvec->
tx && nvec->
tx->pos < nvec->
tx->size) {
611 to_send = nvec->
tx->data[nvec->
tx->pos++];
613 dev_err(nvec->
dev,
"tx buffer underflow on %p (%u > %u)\n",
615 (
uint) (nvec->
tx ? nvec->
tx->pos : 0),
616 (
uint) (nvec->
tx ? nvec->
tx->size : 0));
621 if ((status & (END_TRANS | RNW)) == END_TRANS)
622 nvec_rx_completed(nvec);
623 else if (status & (RNW | RCVD))
624 nvec_invalid_flags(nvec, status,
true);
626 nvec->
rx->data[nvec->
rx->pos++] = received;
629 "RX buffer overflow on %p: "
630 "Trying to write byte %u of %u\n",
638 if ((status & (RCVD | RNW)) == RCVD) {
641 "received address 0x%02x, expected 0x%02x\n",
647 if ((status & (RNW | END_TRANS)) == RNW)
651 if (status == (I2C_SL_IRQ | RNW | RCVD))
652 nvec_gpio_set_value(nvec, 1);
655 "Handled: %s 0x%02x, %s 0x%02x in state %u [%s%s%s]\n",
656 (status & RNW) == 0 ?
"received" :
"R=",
658 (status & (RNW | END_TRANS)) ?
"sent" :
"S=",
661 status & END_TRANS ?
" END_TRANS" :
"",
662 status & RCVD ?
" RCVD" :
"",
663 status & RNW ?
" RNW" :
"");
677 static void tegra_init_i2c_slave(
struct nvec_chip *nvec)
681 clk_prepare_enable(nvec->
i2c_clk);
701 clk_disable_unprepare(nvec->
i2c_clk);
704 #ifdef CONFIG_PM_SLEEP
705 static void nvec_disable_i2c_slave(
struct nvec_chip *nvec)
709 clk_disable_unprepare(nvec->
i2c_clk);
713 static void nvec_power_off(
void)
731 dev_err(&pdev->
dev,
"failed to reserve memory\n");
734 platform_set_drvdata(pdev, nvec);
740 }
else if (nvec->
dev->of_node) {
741 nvec->
gpio = of_get_named_gpio(nvec->
dev->of_node,
743 if (nvec->
gpio < 0) {
747 if (of_property_read_u32(nvec->
dev->of_node,
749 dev_err(&pdev->
dev,
"no i2c address specified");
765 dev_err(&pdev->
dev,
"Can't ioremap I2C region\n");
776 if (IS_ERR(i2c_clk)) {
777 dev_err(nvec->
dev,
"failed to get controller clock\n");
793 INIT_LIST_HEAD(&nvec->rx_data);
794 INIT_LIST_HEAD(&nvec->
tx_data);
795 INIT_WORK(&nvec->rx_work, nvec_dispatch);
801 dev_err(nvec->
dev,
"couldn't request gpio\n");
805 err = devm_request_irq(&pdev->
dev, nvec->
irq, nvec_interrupt, 0,
813 tegra_init_i2c_slave(nvec);
815 clk_prepare_enable(i2c_clk);
820 sizeof(EC_ENABLE_EVENT_REPORTING));
825 nvec_power_handle = nvec;
830 sizeof(EC_GET_FIRMWARE_VERSION));
833 dev_warn(nvec->
dev,
"ec firmware version %02x.%02x.%02x / %02x\n",
842 dev_err(nvec->
dev,
"error adding subdevices\n");
858 struct nvec_chip *nvec = platform_get_drvdata(pdev);
868 #ifdef CONFIG_PM_SLEEP
869 static int nvec_suspend(
struct device *dev)
872 struct nvec_chip *nvec = platform_get_drvdata(pdev);
883 nvec_disable_i2c_slave(nvec);
888 static int nvec_resume(
struct device *dev)
891 struct nvec_chip *nvec = platform_get_drvdata(pdev);
894 tegra_init_i2c_slave(nvec);
905 { .compatible =
"nvidia,nvec", },
911 .probe = tegra_nvec_probe,
917 .of_match_table = nvidia_nvec_of_match,