Commit | Line | Data |
---|---|---|
162c7d8c MD |
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> | |
bdf034d9 | 9 | * Julian Andres Klode <jak@jak-linux.org> |
162c7d8c MD |
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 | ||
32890b98 MD |
17 | #ifndef __LINUX_MFD_NVEC |
18 | #define __LINUX_MFD_NVEC | |
19 | ||
0b1076c4 | 20 | #include <linux/atomic.h> |
12b5a55d JAK |
21 | #include <linux/clk.h> |
22 | #include <linux/completion.h> | |
23 | #include <linux/list.h> | |
24 | #include <linux/mutex.h> | |
7974035c | 25 | #include <linux/notifier.h> |
c0df5bf5 | 26 | #include <linux/reset.h> |
12b5a55d JAK |
27 | #include <linux/spinlock.h> |
28 | #include <linux/workqueue.h> | |
32890b98 | 29 | |
0b1076c4 JAK |
30 | /* NVEC_POOL_SIZE - Size of the pool in &struct nvec_msg */ |
31 | #define NVEC_POOL_SIZE 64 | |
32 | ||
0cab4cb8 JAK |
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 | ||
bdf034d9 JAK |
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 | |
535f2a5f | 46 | * @NVEC_VAR_SIZE: The message has one command byte, one count byte, and has |
bdf034d9 JAK |
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 { | |
32890b98 MD |
54 | NVEC_2BYTES, |
55 | NVEC_3BYTES, | |
0cab4cb8 JAK |
56 | NVEC_VAR_SIZE, |
57 | }; | |
32890b98 | 58 | |
bdf034d9 JAK |
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 { | |
162c7d8c | 73 | NVEC_SYS = 1, |
32890b98 | 74 | NVEC_BAT, |
518945fb MD |
75 | NVEC_GPIO, |
76 | NVEC_SLEEP, | |
77 | NVEC_KBD, | |
32890b98 MD |
78 | NVEC_PS2, |
79 | NVEC_CNTL, | |
93eff83f | 80 | NVEC_OEM0 = 0x0d, |
32890b98 | 81 | NVEC_KB_EVT = 0x80, |
0cab4cb8 JAK |
82 | NVEC_PS2_EVT, |
83 | }; | |
32890b98 | 84 | |
bdf034d9 JAK |
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 | */ | |
32890b98 | 98 | struct nvec_msg { |
0cab4cb8 JAK |
99 | struct list_head node; |
100 | unsigned char data[NVEC_MSG_SIZE]; | |
32890b98 MD |
101 | unsigned short size; |
102 | unsigned short pos; | |
0b1076c4 | 103 | atomic_t used; |
32890b98 MD |
104 | }; |
105 | ||
bdf034d9 JAK |
106 | /** |
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 | |
c0df5bf5 SW |
113 | * @i2c_clk: The clock of the I2C device |
114 | * @rst: The reset of the I2C device | |
bdf034d9 JAK |
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 | */ | |
32890b98 MD |
137 | struct nvec_chip { |
138 | struct device *dev; | |
139 | int gpio; | |
140 | int irq; | |
f686e9af MD |
141 | int i2c_addr; |
142 | void __iomem *base; | |
143 | struct clk *i2c_clk; | |
c0df5bf5 | 144 | struct reset_control *rst; |
32890b98 MD |
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; | |
0cab4cb8 | 149 | struct workqueue_struct *wq; |
0b1076c4 | 150 | struct nvec_msg msg_pool[NVEC_POOL_SIZE]; |
0cab4cb8 JAK |
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; | |
32890b98 | 158 | |
162c7d8c | 159 | /* sync write stuff */ |
0cab4cb8 | 160 | struct mutex sync_write_mutex; |
32890b98 MD |
161 | struct completion sync_write; |
162 | u16 sync_write_pending; | |
163 | struct nvec_msg *last_sync_msg; | |
0cab4cb8 JAK |
164 | |
165 | int state; | |
32890b98 MD |
166 | }; |
167 | ||
ddc9bceb JP |
168 | int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data, |
169 | short size); | |
32890b98 | 170 | |
ddc9bceb JP |
171 | struct nvec_msg *nvec_write_sync(struct nvec_chip *nvec, |
172 | const unsigned char *data, short size); | |
0cab4cb8 | 173 | |
ddc9bceb JP |
174 | int nvec_register_notifier(struct nvec_chip *nvec, |
175 | struct notifier_block *nb, | |
176 | unsigned int events); | |
32890b98 | 177 | |
ddc9bceb | 178 | int nvec_unregister_notifier(struct nvec_chip *dev, struct notifier_block *nb); |
198dd267 | 179 | |
ddc9bceb | 180 | void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg); |
198dd267 | 181 | |
32890b98 | 182 | #endif |