Greg Kroah-Hartman | 971bcfc | 2018-01-11 11:08:42 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 2 | /* |
| 3 | * NVEC: NVIDIA compliant embedded controller interface |
| 4 | * |
| 5 | * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net> |
| 6 | * |
| 7 | * Authors: Pierre-Hugues Husson <phhusson@free.fr> |
| 8 | * Ilya Petrov <ilya.muromec@gmail.com> |
| 9 | * Marc Dietrich <marvin24@gmx.de> |
Julian Andres Klode | bdf034d | 2011-09-27 19:00:56 +0200 | [diff] [blame] | 10 | * Julian Andres Klode <jak@jak-linux.org> |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 13 | #ifndef __LINUX_MFD_NVEC |
| 14 | #define __LINUX_MFD_NVEC |
| 15 | |
Julian Andres Klode | 0b1076c | 2011-09-27 19:00:48 +0200 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Julian Andres Klode | 12b5a55 | 2011-09-27 19:01:06 +0200 | [diff] [blame] | 17 | #include <linux/clk.h> |
| 18 | #include <linux/completion.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/mutex.h> |
Julian Andres Klode | 7974035 | 2011-09-27 19:00:39 +0200 | [diff] [blame] | 21 | #include <linux/notifier.h> |
Stephen Warren | c0df5bf | 2013-11-06 16:46:24 -0700 | [diff] [blame] | 22 | #include <linux/reset.h> |
Julian Andres Klode | 12b5a55 | 2011-09-27 19:01:06 +0200 | [diff] [blame] | 23 | #include <linux/spinlock.h> |
| 24 | #include <linux/workqueue.h> |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 25 | |
Julian Andres Klode | 0b1076c | 2011-09-27 19:00:48 +0200 | [diff] [blame] | 26 | /* NVEC_POOL_SIZE - Size of the pool in &struct nvec_msg */ |
| 27 | #define NVEC_POOL_SIZE 64 |
| 28 | |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 29 | /* |
| 30 | * NVEC_MSG_SIZE - Maximum size of the data field of &struct nvec_msg. |
| 31 | * |
| 32 | * A message must store up to a SMBus block operation which consists of |
| 33 | * one command byte, one count byte, and up to 32 payload bytes = 34 |
| 34 | * byte. |
| 35 | */ |
| 36 | #define NVEC_MSG_SIZE 34 |
| 37 | |
Julian Andres Klode | bdf034d | 2011-09-27 19:00:56 +0200 | [diff] [blame] | 38 | /** |
| 39 | * enum nvec_event_size - The size of an event message |
| 40 | * @NVEC_2BYTES: The message has one command byte and one data byte |
| 41 | * @NVEC_3BYTES: The message has one command byte and two data bytes |
Justin P. Mattock | 535f2a5 | 2012-03-19 08:17:49 -0700 | [diff] [blame] | 42 | * @NVEC_VAR_SIZE: The message has one command byte, one count byte, and has |
Julian Andres Klode | bdf034d | 2011-09-27 19:00:56 +0200 | [diff] [blame] | 43 | * up to as many bytes as the number in the count byte. The |
| 44 | * maximum is 32 |
| 45 | * |
| 46 | * Events can be fixed or variable sized. This is useless on other message |
| 47 | * types, which are always variable sized. |
| 48 | */ |
| 49 | enum nvec_event_size { |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 50 | NVEC_2BYTES, |
| 51 | NVEC_3BYTES, |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 52 | NVEC_VAR_SIZE, |
| 53 | }; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 54 | |
Julian Andres Klode | bdf034d | 2011-09-27 19:00:56 +0200 | [diff] [blame] | 55 | /** |
| 56 | * enum nvec_msg_type - The type of a message |
| 57 | * @NVEC_SYS: A system request/response |
| 58 | * @NVEC_BAT: A battery request/response |
| 59 | * @NVEC_KBD: A keyboard request/response |
| 60 | * @NVEC_PS2: A mouse request/response |
| 61 | * @NVEC_CNTL: A EC control request/response |
| 62 | * @NVEC_KB_EVT: An event from the keyboard |
| 63 | * @NVEC_PS2_EVT: An event from the mouse |
| 64 | * |
| 65 | * Events can be fixed or variable sized. This is useless on other message |
| 66 | * types, which are always variable sized. |
| 67 | */ |
| 68 | enum nvec_msg_type { |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 69 | NVEC_SYS = 1, |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 70 | NVEC_BAT, |
Marc Dietrich | 518945f | 2013-01-27 17:43:41 +0100 | [diff] [blame] | 71 | NVEC_GPIO, |
| 72 | NVEC_SLEEP, |
| 73 | NVEC_KBD, |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 74 | NVEC_PS2, |
| 75 | NVEC_CNTL, |
Marc Dietrich | 93eff83 | 2013-01-27 17:43:43 +0100 | [diff] [blame] | 76 | NVEC_OEM0 = 0x0d, |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 77 | NVEC_KB_EVT = 0x80, |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 78 | NVEC_PS2_EVT, |
| 79 | }; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 80 | |
Julian Andres Klode | bdf034d | 2011-09-27 19:00:56 +0200 | [diff] [blame] | 81 | /** |
| 82 | * struct nvec_msg - A buffer for a single message |
| 83 | * @node: Messages are part of various lists in a &struct nvec_chip |
| 84 | * @data: The data of the message |
| 85 | * @size: For TX messages, the number of bytes used in @data |
| 86 | * @pos: For RX messages, the current position to write to. For TX messages, |
| 87 | * the position to read from. |
| 88 | * @used: Used for the message pool to mark a message as free/allocated. |
| 89 | * |
| 90 | * This structure is used to hold outgoing and incoming messages. Outgoing |
| 91 | * messages have a different format than incoming messages, and that is not |
| 92 | * documented yet. |
| 93 | */ |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 94 | struct nvec_msg { |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 95 | struct list_head node; |
| 96 | unsigned char data[NVEC_MSG_SIZE]; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 97 | unsigned short size; |
| 98 | unsigned short pos; |
Julian Andres Klode | 0b1076c | 2011-09-27 19:00:48 +0200 | [diff] [blame] | 99 | atomic_t used; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Julian Andres Klode | bdf034d | 2011-09-27 19:00:56 +0200 | [diff] [blame] | 102 | /** |
Julian Andres Klode | bdf034d | 2011-09-27 19:00:56 +0200 | [diff] [blame] | 103 | * struct nvec_chip - A single connection to an NVIDIA Embedded controller |
| 104 | * @dev: The device |
| 105 | * @gpio: The same as for &struct nvec_platform_data |
| 106 | * @irq: The IRQ of the I2C device |
| 107 | * @i2c_addr: The address of the I2C slave |
| 108 | * @base: The base of the memory mapped region of the I2C device |
Stephen Warren | c0df5bf | 2013-11-06 16:46:24 -0700 | [diff] [blame] | 109 | * @i2c_clk: The clock of the I2C device |
| 110 | * @rst: The reset of the I2C device |
Julian Andres Klode | bdf034d | 2011-09-27 19:00:56 +0200 | [diff] [blame] | 111 | * @notifier_list: Notifiers to be called on received messages, see |
| 112 | * nvec_register_notifier() |
| 113 | * @rx_data: Received messages that have to be processed |
| 114 | * @tx_data: Messages waiting to be sent to the controller |
| 115 | * @nvec_status_notifier: Internal notifier (see nvec_status_notifier()) |
| 116 | * @rx_work: A work structure for the RX worker nvec_dispatch() |
| 117 | * @tx_work: A work structure for the TX worker nvec_request_master() |
| 118 | * @wq: The work queue in which @rx_work and @tx_work are executed |
| 119 | * @rx: The message currently being retrieved or %NULL |
| 120 | * @msg_pool: A pool of messages for allocation |
| 121 | * @tx: The message currently being transferred |
| 122 | * @tx_scratch: Used for building pseudo messages |
| 123 | * @ec_transfer: A completion that will be completed once a message has been |
| 124 | * received (see nvec_rx_completed()) |
| 125 | * @tx_lock: Spinlock for modifications on @tx_data |
| 126 | * @rx_lock: Spinlock for modifications on @rx_data |
| 127 | * @sync_write_mutex: A mutex for nvec_write_sync() |
| 128 | * @sync_write: A completion to signal that a synchronous message is complete |
| 129 | * @sync_write_pending: The first two bytes of the request (type and subtype) |
| 130 | * @last_sync_msg: The last synchronous message. |
| 131 | * @state: State of our finite state machine used in nvec_interrupt() |
| 132 | */ |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 133 | struct nvec_chip { |
| 134 | struct device *dev; |
Marc Dietrich | 40fbffd | 2018-04-19 13:01:50 +0200 | [diff] [blame] | 135 | struct gpio_desc *gpiod; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 136 | int irq; |
Franck Demathieu | 4cf64a5 | 2017-02-13 09:35:19 +0100 | [diff] [blame] | 137 | u32 i2c_addr; |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 138 | void __iomem *base; |
| 139 | struct clk *i2c_clk; |
Stephen Warren | c0df5bf | 2013-11-06 16:46:24 -0700 | [diff] [blame] | 140 | struct reset_control *rst; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 141 | struct atomic_notifier_head notifier_list; |
| 142 | struct list_head rx_data, tx_data; |
| 143 | struct notifier_block nvec_status_notifier; |
| 144 | struct work_struct rx_work, tx_work; |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 145 | struct workqueue_struct *wq; |
Julian Andres Klode | 0b1076c | 2011-09-27 19:00:48 +0200 | [diff] [blame] | 146 | struct nvec_msg msg_pool[NVEC_POOL_SIZE]; |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 147 | struct nvec_msg *rx; |
| 148 | |
| 149 | struct nvec_msg *tx; |
| 150 | struct nvec_msg tx_scratch; |
| 151 | struct completion ec_transfer; |
| 152 | |
| 153 | spinlock_t tx_lock, rx_lock; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 154 | |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 155 | /* sync write stuff */ |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 156 | struct mutex sync_write_mutex; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 157 | struct completion sync_write; |
| 158 | u16 sync_write_pending; |
| 159 | struct nvec_msg *last_sync_msg; |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 160 | |
| 161 | int state; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 162 | }; |
| 163 | |
Joe Perches | ddc9bce | 2015-08-10 14:51:25 -0700 | [diff] [blame] | 164 | int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data, |
| 165 | short size); |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 166 | |
Tomás Tormo | 73b37c9 | 2016-02-14 20:30:56 +0100 | [diff] [blame] | 167 | int nvec_write_sync(struct nvec_chip *nvec, |
| 168 | const unsigned char *data, short size, |
| 169 | struct nvec_msg **msg); |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 170 | |
Joe Perches | ddc9bce | 2015-08-10 14:51:25 -0700 | [diff] [blame] | 171 | int nvec_register_notifier(struct nvec_chip *nvec, |
| 172 | struct notifier_block *nb, |
| 173 | unsigned int events); |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 174 | |
Joe Perches | ddc9bce | 2015-08-10 14:51:25 -0700 | [diff] [blame] | 175 | int nvec_unregister_notifier(struct nvec_chip *dev, struct notifier_block *nb); |
Julian Andres Klode | 198dd26 | 2011-09-27 19:00:58 +0200 | [diff] [blame] | 176 | |
Joe Perches | ddc9bce | 2015-08-10 14:51:25 -0700 | [diff] [blame] | 177 | void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg); |
Julian Andres Klode | 198dd26 | 2011-09-27 19:00:58 +0200 | [diff] [blame] | 178 | |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 179 | #endif |