• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
9  *           Julian Andres Klode <jak@jak-linux.org>
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 
17 #ifndef __LINUX_MFD_NVEC
18 #define __LINUX_MFD_NVEC
19 
20 #include <linux/atomic.h>
21 #include <linux/clk.h>
22 #include <linux/completion.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/notifier.h>
26 #include <linux/spinlock.h>
27 #include <linux/workqueue.h>
28 
29 /* NVEC_POOL_SIZE - Size of the pool in &struct nvec_msg */
30 #define NVEC_POOL_SIZE	64
31 
32 /*
33  * NVEC_MSG_SIZE - Maximum size of the data field of &struct nvec_msg.
34  *
35  * A message must store up to a SMBus block operation which consists of
36  * one command byte, one count byte, and up to 32 payload bytes = 34
37  * byte.
38  */
39 #define NVEC_MSG_SIZE	34
40 
41 /**
42  * enum nvec_event_size - The size of an event message
43  * @NVEC_2BYTES: The message has one command byte and one data byte
44  * @NVEC_3BYTES: The message has one command byte and two data bytes
45  * @NVEC_VAR_SIZE: The message has one command byte, one count byte, and has
46  *                 up to as many bytes as the number in the count byte. The
47  *                 maximum is 32
48  *
49  * Events can be fixed or variable sized. This is useless on other message
50  * types, which are always variable sized.
51  */
52 enum nvec_event_size {
53 	NVEC_2BYTES,
54 	NVEC_3BYTES,
55 	NVEC_VAR_SIZE,
56 };
57 
58 /**
59  * enum nvec_msg_type - The type of a message
60  * @NVEC_SYS: A system request/response
61  * @NVEC_BAT: A battery request/response
62  * @NVEC_KBD: A keyboard request/response
63  * @NVEC_PS2: A mouse request/response
64  * @NVEC_CNTL: A EC control request/response
65  * @NVEC_KB_EVT: An event from the keyboard
66  * @NVEC_PS2_EVT: An event from the mouse
67  *
68  * Events can be fixed or variable sized. This is useless on other message
69  * types, which are always variable sized.
70  */
71 enum nvec_msg_type {
72 	NVEC_SYS = 1,
73 	NVEC_BAT,
74 	NVEC_GPIO,
75 	NVEC_SLEEP,
76 	NVEC_KBD,
77 	NVEC_PS2,
78 	NVEC_CNTL,
79 	NVEC_OEM0 = 0x0d,
80 	NVEC_KB_EVT = 0x80,
81 	NVEC_PS2_EVT,
82 };
83 
84 /**
85  * struct nvec_msg - A buffer for a single message
86  * @node: Messages are part of various lists in a &struct nvec_chip
87  * @data: The data of the message
88  * @size: For TX messages, the number of bytes used in @data
89  * @pos:  For RX messages, the current position to write to. For TX messages,
90  *        the position to read from.
91  * @used: Used for the message pool to mark a message as free/allocated.
92  *
93  * This structure is used to hold outgoing and incoming messages. Outgoing
94  * messages have a different format than incoming messages, and that is not
95  * documented yet.
96  */
97 struct nvec_msg {
98 	struct list_head node;
99 	unsigned char data[NVEC_MSG_SIZE];
100 	unsigned short size;
101 	unsigned short pos;
102 	atomic_t used;
103 };
104 
105 /**
106  * struct nvec_subdev - A subdevice of nvec, such as nvec_kbd
107  * @name: The name of the sub device
108  * @platform_data: Platform data
109  * @id: Identifier of the sub device
110  */
111 struct nvec_subdev {
112 	const char *name;
113 	void *platform_data;
114 	int id;
115 };
116 
117 /**
118  * struct nvec_platform_data - platform data for a tegra slave controller
119  * @i2c_addr: number of i2c slave adapter the ec is connected to
120  * @gpio: gpio number for the ec request line
121  *
122  * Platform data, to be used in board definitions. For an example, take a
123  * look at the paz00 board in arch/arm/mach-tegra/board-paz00.c
124  */
125 struct nvec_platform_data {
126 	int i2c_addr;
127 	int gpio;
128 };
129 
130 /**
131  * struct nvec_chip - A single connection to an NVIDIA Embedded controller
132  * @dev: The device
133  * @gpio: The same as for &struct nvec_platform_data
134  * @irq: The IRQ of the I2C device
135  * @i2c_addr: The address of the I2C slave
136  * @base: The base of the memory mapped region of the I2C device
137  * @clk: The clock of the I2C device
138  * @notifier_list: Notifiers to be called on received messages, see
139  *                 nvec_register_notifier()
140  * @rx_data: Received messages that have to be processed
141  * @tx_data: Messages waiting to be sent to the controller
142  * @nvec_status_notifier: Internal notifier (see nvec_status_notifier())
143  * @rx_work: A work structure for the RX worker nvec_dispatch()
144  * @tx_work: A work structure for the TX worker nvec_request_master()
145  * @wq: The work queue in which @rx_work and @tx_work are executed
146  * @rx: The message currently being retrieved or %NULL
147  * @msg_pool: A pool of messages for allocation
148  * @tx: The message currently being transferred
149  * @tx_scratch: Used for building pseudo messages
150  * @ec_transfer: A completion that will be completed once a message has been
151  *               received (see nvec_rx_completed())
152  * @tx_lock: Spinlock for modifications on @tx_data
153  * @rx_lock: Spinlock for modifications on @rx_data
154  * @sync_write_mutex: A mutex for nvec_write_sync()
155  * @sync_write: A completion to signal that a synchronous message is complete
156  * @sync_write_pending: The first two bytes of the request (type and subtype)
157  * @last_sync_msg: The last synchronous message.
158  * @state: State of our finite state machine used in nvec_interrupt()
159  */
160 struct nvec_chip {
161 	struct device *dev;
162 	int gpio;
163 	int irq;
164 	int i2c_addr;
165 	void __iomem *base;
166 	struct clk *i2c_clk;
167 	struct atomic_notifier_head notifier_list;
168 	struct list_head rx_data, tx_data;
169 	struct notifier_block nvec_status_notifier;
170 	struct work_struct rx_work, tx_work;
171 	struct workqueue_struct *wq;
172 	struct nvec_msg msg_pool[NVEC_POOL_SIZE];
173 	struct nvec_msg *rx;
174 
175 	struct nvec_msg *tx;
176 	struct nvec_msg tx_scratch;
177 	struct completion ec_transfer;
178 
179 	spinlock_t tx_lock, rx_lock;
180 
181 	/* sync write stuff */
182 	struct mutex sync_write_mutex;
183 	struct completion sync_write;
184 	u16 sync_write_pending;
185 	struct nvec_msg *last_sync_msg;
186 
187 	int state;
188 };
189 
190 extern int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
191 			     short size);
192 
193 extern struct nvec_msg *nvec_write_sync(struct nvec_chip *nvec,
194 					const unsigned char *data, short size);
195 
196 extern int nvec_register_notifier(struct nvec_chip *nvec,
197 				  struct notifier_block *nb,
198 				  unsigned int events);
199 
200 extern int nvec_unregister_notifier(struct nvec_chip *dev,
201 				    struct notifier_block *nb);
202 
203 extern void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg);
204 
205 #endif
206