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