// SPDX-License-Identifier: GPL-2.0/* * NVEC: NVIDIA compliant embedded controller interface * * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net> * * Authors: Pierre-Hugues Husson <phhusson@free.fr> * Ilya Petrov <ilya.muromec@gmail.com> * Marc Dietrich <marvin24@gmx.de> * Julian Andres Klode <jak@jak-linux.org> */ #include <linux/kernel.h>#include <linux/module.h>#include <linux/atomic.h>#include <linux/clk.h>#include <linux/completion.h>#include <linux/delay.h>#include <linux/err.h>#include <linux/gpio/consumer.h>#include <linux/interrupt.h>#include <linux/io.h>#include <linux/irq.h>#include <linux/of.h>#include <linux/list.h>#include <linux/mfd/core.h>#include <linux/mutex.h>#include <linux/notifier.h>#include <linux/slab.h>#include <linux/spinlock.h>#include <linux/workqueue.h> #include "nvec.h" #define I2C_CNFG 0x00#define I2C_CNFG_PACKET_MODE_EN BIT(10)#define I2C_CNFG_NEW_MASTER_SFM BIT(11)#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12 #define I2C_SL_CNFG 0x20#define I2C_SL_NEWSL BIT(2)#define I2C_SL_NACK BIT(1)#define I2C_SL_RESP BIT(0)#define I2C_SL_IRQ BIT(3)#define END_TRANS BIT(4)#define RCVD BIT(2)#define RNW BIT(1) #define I2C_SL_RCVD 0x24#define I2C_SL_STATUS 0x28#define I2C_SL_ADDR1 0x2c#define I2C_SL_ADDR2 0x30#define I2C_SL_DELAY_COUNT 0x3c /** * enum nvec_msg_category - Message categories for nvec_msg_alloc() * @NVEC_MSG_RX: The message is an incoming message (from EC) * @NVEC_MSG_TX: The message is an outgoing message (to EC) */enum nvec_msg_category { NVEC_MSG_RX, NVEC_MSG_TX,}; enum nvec_sleep_subcmds { GLOBAL_EVENTS, AP_PWR_DOWN, AP_SUSPEND,}; #define CNF_EVENT_REPORTING 0x01#define GET_FIRMWARE_VERSION 0x15#define LID_SWITCH BIT(1)#define PWR_BUTTON BIT(15) static struct nvec_chip *nvec_power_handle; static const struct mfd_cell nvec_devices[] = { { .name = "nvec-kbd", }, { .name = "nvec-mouse", }, { .name = "nvec-power", .id = 0, }, { .name = "nvec-power", .id = 1, }, { .name = "nvec-paz00", },}; /** * nvec_register_notifier - Register a notifier with nvec * @nvec: A &struct nvec_chip * @nb: The notifier block to register * @events: Unused * * Registers a notifier with @nvec. The notifier will be added to an atomic * notifier chain that is called for all received messages except those that * correspond to a request initiated by nvec_write_sync(). */int nvec_register_notifier(struct nvec_chip *nvec, struct notifier_block *nb, unsigned int events){ return atomic_notifier_chain_register(&nvec->notifier_list, nb);}EXPORT_SYMBOL_GPL(nvec_register_notifier); /** * nvec_unregister_notifier - Unregister a notifier with nvec * @nvec: A &struct nvec_chip * @nb: The notifier block to unregister * * Unregisters a notifier with @nvec. The notifier will be removed from the * atomic notifier chain. */int nvec_unregister_notifier(struct nvec_chip *nvec, struct notifier_block *nb){ return atomic_notifier_chain_unregister(&nvec->notifier_list, nb);}EXPORT_SYMBOL_GPL(nvec_unregister_notifier); /* * nvec_status_notifier - The final notifier * * Prints a message about control events not handled in the notifier * chain. */static int nvec_status_notifier(struct notifier_block *nb, unsigned long event_type, void *data){ struct nvec_chip *nvec = container_of(nb, struct nvec_chip, nvec_status_notifier); unsigned char *msg = data; if (event_type != NVEC_CNTL) return NOTIFY_DONE; dev_warn(nvec->dev, "unhandled msg type %ld\n", event_type); print_hex_dump(KERN_WARNING, "payload: ", DUMP_PREFIX_NONE, 16, 1, msg, msg[1] + 2, true); return NOTIFY_OK;} /** * nvec_msg_alloc: * @nvec: A &struct nvec_chip * @category: Pool category, see &enum nvec_msg_category * * Allocate a single &struct nvec_msg object from the message pool of * @nvec. The result shall be passed to nvec_msg_free() if no longer * used. * * Outgoing messages are placed in the upper 75% of the pool, keeping the * lower 25% available for RX buffers only. The reason is to prevent a * situation where all buffers are full and a message is thus endlessly * retried because the response could never be processed. */static struct nvec_msg *nvec_msg_alloc(struct nvec_chip *nvec, enum nvec_msg_category category){ int i = (category == NVEC_MSG_TX) ? (NVEC_POOL_SIZE / 4) : 0; for (; 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 %s buffer\n", (category == NVEC_MSG_TX) ? "TX" : "RX"); return NULL;} /** * nvec_msg_free: * @nvec: A &struct nvec_chip * @msg: A message (must be allocated by nvec_msg_alloc() and belong to @nvec) * * Free the given message */void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg){ if (msg != &nvec->tx_scratch) dev_vdbg(nvec->dev, "INFO: Free %ti\n", msg - nvec->msg_pool); atomic_set(&msg->used, 0);}EXPORT_SYMBOL_GPL(nvec_msg_free); /** * 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; return 0;} /** * nvec_gpio_set_value - Set the GPIO value * @nvec: A &struct nvec_chip * @value: The value to write (0 or 1) * * Like gpio_set_value(), but generating debugging information */static void nvec_gpio_set_value(struct nvec_chip *nvec, int value){ dev_dbg(nvec->dev, "GPIO changed from %u to %u\n", gpiod_get_value(nvec->gpiod), value); gpiod_set_value(nvec->gpiod, value);} /** * nvec_write_async - Asynchronously write a message to NVEC * @nvec: An nvec_chip instance * @data: The message data, starting with the request type * @size: The size of @data * * Queue a single message to be transferred to the embedded controller * and return immediately. * * Returns: 0 on success, a negative error code on failure. If a failure * occurred, the nvec driver may print an error. */int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data, short size){ struct nvec_msg *msg; unsigned long flags; msg = nvec_msg_alloc(nvec, NVEC_MSG_TX); if (!msg) return -ENOMEM; msg->data[0] = size; memcpy(msg->data + 1, data, size); msg->size = size + 1; spin_lock_irqsave(&nvec->tx_lock, flags); list_add_tail(&msg->node, &nvec->tx_data); spin_unlock_irqrestore(&nvec->tx_lock, flags); schedule_work(&nvec->tx_work); return 0;}EXPORT_SYMBOL(nvec_write_async); /** * nvec_write_sync - Write a message to nvec and read the response * @nvec: An &struct nvec_chip * @data: The data to write * @size: The size of @data * @msg: The response message received * * This is similar to nvec_write_async(), but waits for the * request to be answered before returning. This function * uses a mutex and can thus not be called from e.g. * interrupt handlers. * * Returns: 0 on success, a negative error code on failure. * The response message is returned in @msg. Shall be freed * with nvec_msg_free() once no longer used. * */int nvec_write_sync(struct nvec_chip *nvec, const unsigned char *data, short size, struct nvec_msg **msg){ mutex_lock(&nvec->sync_write_mutex); if (msg) *msg = NULL; nvec->sync_write_pending = (data[1] << 8) + data[0]; if (nvec_write_async(nvec, data, size) < 0) { mutex_unlock(&nvec->sync_write_mutex); return -ENOMEM; } dev_dbg(nvec->dev, "nvec_sync_write: 0x%04x\n", nvec->sync_write_pending); if (!(wait_for_completion_timeout(&nvec->sync_write, msecs_to_jiffies(2000)))) { dev_warn(nvec->dev, "Timeout waiting for sync write to complete\n"); mutex_unlock(&nvec->sync_write_mutex); return -ETIMEDOUT; } dev_dbg(nvec->dev, "nvec_sync_write: pong!\n"); if (msg) *msg = nvec->last_sync_msg; else nvec_msg_free(nvec, nvec->last_sync_msg); mutex_unlock(&nvec->sync_write_mutex); return 0;}EXPORT_SYMBOL(nvec_write_sync); /** * nvec_toggle_global_events - enables or disables global event reporting * @nvec: nvec handle * @state: true for enable, false for disable * * This switches on/off global event reports by the embedded controller. */static void nvec_toggle_global_events(struct nvec_chip *nvec, bool state){ unsigned char global_events[] = { NVEC_SLEEP, GLOBAL_EVENTS, state }; nvec_write_async(nvec, global_events, 3);} /** * nvec_event_mask - fill the command string with event bitfield * @ev: points to event command string * @mask: bit to insert into the event mask * * Configure event command expects a 32 bit bitfield which describes * which events to enable. The bitfield has the following structure * (from highest byte to lowest): * system state bits 7-0 * system state bits 15-8 * oem system state bits 7-0 * oem system state bits 15-8 */static void nvec_event_mask(char *ev, u32 mask){ ev[3] = mask >> 16 & 0xff; ev[4] = mask >> 24 & 0xff; ev[5] = mask >> 0 & 0xff; ev[6] = mask >> 8 & 0xff;} /** * nvec_request_master - Process outgoing messages * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip) * * Processes all outgoing requests by sending the request and awaiting the * response, then continuing with the next request. Once a request has a * matching response, it will be freed and removed from the list. */static void nvec_request_master(struct work_struct *work){ struct nvec_chip *nvec = container_of(work, struct nvec_chip, tx_work); unsigned long flags; long err; struct nvec_msg *msg; spin_lock_irqsave(&nvec->tx_lock, flags); while (!list_empty(&nvec->tx_data)) { msg = list_first_entry(&nvec->tx_data, struct nvec_msg, node); spin_unlock_irqrestore(&nvec->tx_lock, flags); nvec_gpio_set_value(nvec, 0); err = wait_for_completion_interruptible_timeout(&nvec->ec_transfer, msecs_to_jiffies(5000)); if (err == 0) { dev_warn(nvec->dev, "Timeout waiting for ec transfer\n"); nvec_gpio_set_value(nvec, 1); msg->pos = 0; } spin_lock_irqsave(&nvec->tx_lock, flags); if (err > 0) { list_del_init(&msg->node); nvec_msg_free(nvec, msg); } } spin_unlock_irqrestore(&nvec->tx_lock, flags);} /** * parse_msg - Print some information and call the notifiers on an RX message * @nvec: A &struct nvec_chip * @msg: A message received by @nvec * * Paarse some pieces of the message and then call the chain of notifiers * registered via nvec_register_notifier. */static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg){ if ((msg->data[0] & 1 << 7) == 0 && msg->data[3]) { dev_err(nvec->dev, "ec responded %*ph\n", 4, msg->data); return -EINVAL; } 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); atomic_notifier_call_chain(&nvec->notifier_list, msg->data[0] & 0x8f, msg->data); return 0;} /** * nvec_dispatch - Process messages received from the EC * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip) * * Process messages previously received from the EC and put into the RX * queue of the &struct nvec_chip instance associated with @work. */static void nvec_dispatch(struct work_struct *work){ struct nvec_chip *nvec = container_of(work, struct nvec_chip, rx_work); unsigned long flags; struct nvec_msg *msg; spin_lock_irqsave(&nvec->rx_lock, flags); while (!list_empty(&nvec->rx_data)) { msg = list_first_entry(&nvec->rx_data, struct nvec_msg, node); list_del_init(&msg->node); spin_unlock_irqrestore(&nvec->rx_lock, flags); if (nvec->sync_write_pending == (msg->data[2] << 8) + msg->data[0]) { 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); nvec_msg_free(nvec, msg); } spin_lock_irqsave(&nvec->rx_lock, flags); } spin_unlock_irqrestore(&nvec->rx_lock, flags);} /** * nvec_tx_completed - Complete the current transfer * @nvec: A &struct nvec_chip * * This is called when we have received an END_TRANS on a TX transfer. */static void nvec_tx_completed(struct nvec_chip *nvec){ /* We got an END_TRANS, let's skip this, maybe there's an event */ if (nvec->tx->pos != nvec->tx->size) { dev_err(nvec->dev, "Premature END_TRANS, resending\n"); nvec->tx->pos = 0; nvec_gpio_set_value(nvec, 0); } else { nvec->state = 0; }} /** * nvec_rx_completed - Complete the current transfer * @nvec: A &struct nvec_chip * * This is called when we have received an END_TRANS on a RX transfer. */static void nvec_rx_completed(struct nvec_chip *nvec){ if (nvec->rx->pos != nvec_msg_size(nvec->rx)) { unsigned char msg_type = nvec->rx->data[0]; dev_err(nvec->dev, "RX incomplete: Expected %u bytes, got %u\n", (uint)nvec_msg_size(nvec->rx),Lines 1–500 of 971. Load more
// SPDX-License-Identifier: GPL-2.0 /* * NVEC: NVIDIA compliant embedded controller interface * * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net> * * Authors: Pierre-Hugues Husson <phhusson@free.fr> * Ilya Petrov <ilya.muromec@gmail.com> * Marc Dietrich <marvin24@gmx.de> * Julian Andres Klode <jak@jak-linux.org> */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/atomic.h> #include <linux/clk.h> #include <linux/completion.h> #include <linux/delay.h> #include <linux/err.h> #include <linux/gpio/consumer.h> #include <linux/interrupt.h> #include <linux/io.h> #include <linux/irq.h> #include <linux/of.h> #include <linux/list.h> #include <linux/mfd/core.h> #include <linux/mutex.h> #include <linux/notifier.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <linux/workqueue.h> #include "nvec.h" #define I2C_CNFG 0x00 #define I2C_CNFG_PACKET_MODE_EN BIT(10) #define I2C_CNFG_NEW_MASTER_SFM BIT(11) #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12 #define I2C_SL_CNFG 0x20 #define I2C_SL_NEWSL BIT(2) #define I2C_SL_NACK BIT(1) #define I2C_SL_RESP BIT(0) #define I2C_SL_IRQ BIT(3) #define END_TRANS BIT(4) #define RCVD BIT(2) #define RNW BIT(1) #define I2C_SL_RCVD 0x24 #define I2C_SL_STATUS 0x28 #define I2C_SL_ADDR1 0x2c #define I2C_SL_ADDR2 0x30 #define I2C_SL_DELAY_COUNT 0x3c /** * enum nvec_msg_category - Message categories for nvec_msg_alloc() * @NVEC_MSG_RX: The message is an incoming message (from EC) * @NVEC_MSG_TX: The message is an outgoing message (to EC) */ enum nvec_msg_category { NVEC_MSG_RX, NVEC_MSG_TX, }; enum nvec_sleep_subcmds { GLOBAL_EVENTS, AP_PWR_DOWN, AP_SUSPEND, }; #define CNF_EVENT_REPORTING 0x01 #define GET_FIRMWARE_VERSION 0x15 #define LID_SWITCH BIT(1) #define PWR_BUTTON BIT(15) static struct nvec_chip *nvec_power_handle; static const struct mfd_cell nvec_devices[] = { { .name = "nvec-kbd", }, { .name = "nvec-mouse", }, { .name = "nvec-power", .id = 0, }, { .name = "nvec-power", .id = 1, }, { .name = "nvec-paz00", }, }; /** * nvec_register_notifier - Register a notifier with nvec * @nvec: A &struct nvec_chip * @nb: The notifier block to register * @events: Unused * * Registers a notifier with @nvec. The notifier will be added to an atomic * notifier chain that is called for all received messages except those that * correspond to a request initiated by nvec_write_sync(). */ int nvec_register_notifier(struct nvec_chip *nvec, struct notifier_block *nb, unsigned int events) { return atomic_notifier_chain_register(&nvec->notifier_list, nb); } EXPORT_SYMBOL_GPL(nvec_register_notifier); /** * nvec_unregister_notifier - Unregister a notifier with nvec * @nvec: A &struct nvec_chip * @nb: The notifier block to unregister * * Unregisters a notifier with @nvec. The notifier will be removed from the * atomic notifier chain. */ int nvec_unregister_notifier(struct nvec_chip *nvec, struct notifier_block *nb) { return atomic_notifier_chain_unregister(&nvec->notifier_list, nb); } EXPORT_SYMBOL_GPL(nvec_unregister_notifier); /* * nvec_status_notifier - The final notifier * * Prints a message about control events not handled in the notifier * chain. */ static int nvec_status_notifier(struct notifier_block *nb, unsigned long event_type, void *data) { struct nvec_chip *nvec = container_of(nb, struct nvec_chip, nvec_status_notifier); unsigned char *msg = data; if (event_type != NVEC_CNTL) return NOTIFY_DONE; dev_warn(nvec->dev, "unhandled msg type %ld\n", event_type); print_hex_dump(KERN_WARNING, "payload: ", DUMP_PREFIX_NONE, 16, 1, msg, msg[1] + 2, true); return NOTIFY_OK; } /** * nvec_msg_alloc: * @nvec: A &struct nvec_chip * @category: Pool category, see &enum nvec_msg_category * * Allocate a single &struct nvec_msg object from the message pool of * @nvec. The result shall be passed to nvec_msg_free() if no longer * used. * * Outgoing messages are placed in the upper 75% of the pool, keeping the * lower 25% available for RX buffers only. The reason is to prevent a * situation where all buffers are full and a message is thus endlessly * retried because the response could never be processed. */ static struct nvec_msg *nvec_msg_alloc(struct nvec_chip *nvec, enum nvec_msg_category category) { int i = (category == NVEC_MSG_TX) ? (NVEC_POOL_SIZE / 4) : 0; for (; 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 %s buffer\n", (category == NVEC_MSG_TX) ? "TX" : "RX"); return NULL; } /** * nvec_msg_free: * @nvec: A &struct nvec_chip * @msg: A message (must be allocated by nvec_msg_alloc() and belong to @nvec) * * Free the given message */ void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg) { if (msg != &nvec->tx_scratch) dev_vdbg(nvec->dev, "INFO: Free %ti\n", msg - nvec->msg_pool); atomic_set(&msg->used, 0); } EXPORT_SYMBOL_GPL(nvec_msg_free); /** * 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; return 0; } /** * nvec_gpio_set_value - Set the GPIO value * @nvec: A &struct nvec_chip * @value: The value to write (0 or 1) * * Like gpio_set_value(), but generating debugging information */ static void nvec_gpio_set_value(struct nvec_chip *nvec, int value) { dev_dbg(nvec->dev, "GPIO changed from %u to %u\n", gpiod_get_value(nvec->gpiod), value); gpiod_set_value(nvec->gpiod, value); } /** * nvec_write_async - Asynchronously write a message to NVEC * @nvec: An nvec_chip instance * @data: The message data, starting with the request type * @size: The size of @data * * Queue a single message to be transferred to the embedded controller * and return immediately. * * Returns: 0 on success, a negative error code on failure. If a failure * occurred, the nvec driver may print an error. */ int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data, short size) { struct nvec_msg *msg; unsigned long flags; msg = nvec_msg_alloc(nvec, NVEC_MSG_TX); if (!msg) return -ENOMEM; msg->data[0] = size; memcpy(msg->data + 1, data, size); msg->size = size + 1; spin_lock_irqsave(&nvec->tx_lock, flags); list_add_tail(&msg->node, &nvec->tx_data); spin_unlock_irqrestore(&nvec->tx_lock, flags); schedule_work(&nvec->tx_work); return 0; } EXPORT_SYMBOL(nvec_write_async); /** * nvec_write_sync - Write a message to nvec and read the response * @nvec: An &struct nvec_chip * @data: The data to write * @size: The size of @data * @msg: The response message received * * This is similar to nvec_write_async(), but waits for the * request to be answered before returning. This function * uses a mutex and can thus not be called from e.g. * interrupt handlers. * * Returns: 0 on success, a negative error code on failure. * The response message is returned in @msg. Shall be freed * with nvec_msg_free() once no longer used. * */ int nvec_write_sync(struct nvec_chip *nvec, const unsigned char *data, short size, struct nvec_msg **msg) { mutex_lock(&nvec->sync_write_mutex); if (msg) *msg = NULL; nvec->sync_write_pending = (data[1] << 8) + data[0]; if (nvec_write_async(nvec, data, size) < 0) { mutex_unlock(&nvec->sync_write_mutex); return -ENOMEM; } dev_dbg(nvec->dev, "nvec_sync_write: 0x%04x\n", nvec->sync_write_pending); if (!(wait_for_completion_timeout(&nvec->sync_write, msecs_to_jiffies(2000)))) { dev_warn(nvec->dev, "Timeout waiting for sync write to complete\n"); mutex_unlock(&nvec->sync_write_mutex); return -ETIMEDOUT; } dev_dbg(nvec->dev, "nvec_sync_write: pong!\n"); if (msg) *msg = nvec->last_sync_msg; else nvec_msg_free(nvec, nvec->last_sync_msg); mutex_unlock(&nvec->sync_write_mutex); return 0; } EXPORT_SYMBOL(nvec_write_sync); /** * nvec_toggle_global_events - enables or disables global event reporting * @nvec: nvec handle * @state: true for enable, false for disable * * This switches on/off global event reports by the embedded controller. */ static void nvec_toggle_global_events(struct nvec_chip *nvec, bool state) { unsigned char global_events[] = { NVEC_SLEEP, GLOBAL_EVENTS, state }; nvec_write_async(nvec, global_events, 3); } /** * nvec_event_mask - fill the command string with event bitfield * @ev: points to event command string * @mask: bit to insert into the event mask * * Configure event command expects a 32 bit bitfield which describes * which events to enable. The bitfield has the following structure * (from highest byte to lowest): * system state bits 7-0 * system state bits 15-8 * oem system state bits 7-0 * oem system state bits 15-8 */ static void nvec_event_mask(char *ev, u32 mask) { ev[3] = mask >> 16 & 0xff; ev[4] = mask >> 24 & 0xff; ev[5] = mask >> 0 & 0xff; ev[6] = mask >> 8 & 0xff; } /** * nvec_request_master - Process outgoing messages * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip) * * Processes all outgoing requests by sending the request and awaiting the * response, then continuing with the next request. Once a request has a * matching response, it will be freed and removed from the list. */ static void nvec_request_master(struct work_struct *work) { struct nvec_chip *nvec = container_of(work, struct nvec_chip, tx_work); unsigned long flags; long err; struct nvec_msg *msg; spin_lock_irqsave(&nvec->tx_lock, flags); while (!list_empty(&nvec->tx_data)) { msg = list_first_entry(&nvec->tx_data, struct nvec_msg, node); spin_unlock_irqrestore(&nvec->tx_lock, flags); nvec_gpio_set_value(nvec, 0); err = wait_for_completion_interruptible_timeout(&nvec->ec_transfer, msecs_to_jiffies(5000)); if (err == 0) { dev_warn(nvec->dev, "Timeout waiting for ec transfer\n"); nvec_gpio_set_value(nvec, 1); msg->pos = 0; } spin_lock_irqsave(&nvec->tx_lock, flags); if (err > 0) { list_del_init(&msg->node); nvec_msg_free(nvec, msg); } } spin_unlock_irqrestore(&nvec->tx_lock, flags); } /** * parse_msg - Print some information and call the notifiers on an RX message * @nvec: A &struct nvec_chip * @msg: A message received by @nvec * * Paarse some pieces of the message and then call the chain of notifiers * registered via nvec_register_notifier. */ static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg) { if ((msg->data[0] & 1 << 7) == 0 && msg->data[3]) { dev_err(nvec->dev, "ec responded %*ph\n", 4, msg->data); return -EINVAL; } 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); atomic_notifier_call_chain(&nvec->notifier_list, msg->data[0] & 0x8f, msg->data); return 0; } /** * nvec_dispatch - Process messages received from the EC * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip) * * Process messages previously received from the EC and put into the RX * queue of the &struct nvec_chip instance associated with @work. */ static void nvec_dispatch(struct work_struct *work) { struct nvec_chip *nvec = container_of(work, struct nvec_chip, rx_work); unsigned long flags; struct nvec_msg *msg; spin_lock_irqsave(&nvec->rx_lock, flags); while (!list_empty(&nvec->rx_data)) { msg = list_first_entry(&nvec->rx_data, struct nvec_msg, node); list_del_init(&msg->node); spin_unlock_irqrestore(&nvec->rx_lock, flags); if (nvec->sync_write_pending == (msg->data[2] << 8) + msg->data[0]) { 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); nvec_msg_free(nvec, msg); } spin_lock_irqsave(&nvec->rx_lock, flags); } spin_unlock_irqrestore(&nvec->rx_lock, flags); } /** * nvec_tx_completed - Complete the current transfer * @nvec: A &struct nvec_chip * * This is called when we have received an END_TRANS on a TX transfer. */ static void nvec_tx_completed(struct nvec_chip *nvec) { /* We got an END_TRANS, let's skip this, maybe there's an event */ if (nvec->tx->pos != nvec->tx->size) { dev_err(nvec->dev, "Premature END_TRANS, resending\n"); nvec->tx->pos = 0; nvec_gpio_set_value(nvec, 0); } else { nvec->state = 0; } } /** * nvec_rx_completed - Complete the current transfer * @nvec: A &struct nvec_chip * * This is called when we have received an END_TRANS on a RX transfer. */ static void nvec_rx_completed(struct nvec_chip *nvec) { if (nvec->rx->pos != nvec_msg_size(nvec->rx)) { unsigned char msg_type = nvec->rx->data[0]; dev_err(nvec->dev, "RX incomplete: Expected %u bytes, got %u\n", (uint)nvec_msg_size(nvec->rx),