blob: e880518935fb016bbf15ef8495c64527489be8ef [file] [log] [blame]
Marc Dietrich162c7d82011-09-27 19:00:40 +02001/*
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 Klodebdf034d2011-09-27 19:00:56 +02009 * Julian Andres Klode <jak@jak-linux.org>
Marc Dietrich162c7d82011-09-27 19:00:40 +020010 *
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 Dietrich32890b92011-05-19 16:34:42 +020017#ifndef __LINUX_MFD_NVEC
18#define __LINUX_MFD_NVEC
19
Julian Andres Klode0b1076c2011-09-27 19:00:48 +020020#include <linux/atomic.h>
Julian Andres Klode12b5a552011-09-27 19:01:06 +020021#include <linux/clk.h>
22#include <linux/completion.h>
23#include <linux/list.h>
24#include <linux/mutex.h>
Julian Andres Klode79740352011-09-27 19:00:39 +020025#include <linux/notifier.h>
Julian Andres Klode12b5a552011-09-27 19:01:06 +020026#include <linux/spinlock.h>
27#include <linux/workqueue.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020028
Julian Andres Klode0b1076c2011-09-27 19:00:48 +020029/* NVEC_POOL_SIZE - Size of the pool in &struct nvec_msg */
30#define NVEC_POOL_SIZE 64
31
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020032/*
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
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020041/**
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
Justin P. Mattock535f2a52012-03-19 08:17:49 -070045 * @NVEC_VAR_SIZE: The message has one command byte, one count byte, and has
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020046 * 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 */
52enum nvec_event_size {
Marc Dietrich32890b92011-05-19 16:34:42 +020053 NVEC_2BYTES,
54 NVEC_3BYTES,
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020055 NVEC_VAR_SIZE,
56};
Marc Dietrich32890b92011-05-19 16:34:42 +020057
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020058/**
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 */
71enum nvec_msg_type {
Marc Dietrich162c7d82011-09-27 19:00:40 +020072 NVEC_SYS = 1,
Marc Dietrich32890b92011-05-19 16:34:42 +020073 NVEC_BAT,
Marc Dietrich518945f2013-01-27 17:43:41 +010074 NVEC_GPIO,
75 NVEC_SLEEP,
76 NVEC_KBD,
Marc Dietrich32890b92011-05-19 16:34:42 +020077 NVEC_PS2,
78 NVEC_CNTL,
Marc Dietrich93eff832013-01-27 17:43:43 +010079 NVEC_OEM0 = 0x0d,
Marc Dietrich32890b92011-05-19 16:34:42 +020080 NVEC_KB_EVT = 0x80,
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020081 NVEC_PS2_EVT,
82};
Marc Dietrich32890b92011-05-19 16:34:42 +020083
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020084/**
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 */
Marc Dietrich32890b92011-05-19 16:34:42 +020097struct nvec_msg {
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020098 struct list_head node;
99 unsigned char data[NVEC_MSG_SIZE];
Marc Dietrich32890b92011-05-19 16:34:42 +0200100 unsigned short size;
101 unsigned short pos;
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200102 atomic_t used;
Marc Dietrich32890b92011-05-19 16:34:42 +0200103};
104
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200105/**
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200106 * struct nvec_chip - A single connection to an NVIDIA Embedded controller
107 * @dev: The device
108 * @gpio: The same as for &struct nvec_platform_data
109 * @irq: The IRQ of the I2C device
110 * @i2c_addr: The address of the I2C slave
111 * @base: The base of the memory mapped region of the I2C device
112 * @clk: The clock of the I2C device
113 * @notifier_list: Notifiers to be called on received messages, see
114 * nvec_register_notifier()
115 * @rx_data: Received messages that have to be processed
116 * @tx_data: Messages waiting to be sent to the controller
117 * @nvec_status_notifier: Internal notifier (see nvec_status_notifier())
118 * @rx_work: A work structure for the RX worker nvec_dispatch()
119 * @tx_work: A work structure for the TX worker nvec_request_master()
120 * @wq: The work queue in which @rx_work and @tx_work are executed
121 * @rx: The message currently being retrieved or %NULL
122 * @msg_pool: A pool of messages for allocation
123 * @tx: The message currently being transferred
124 * @tx_scratch: Used for building pseudo messages
125 * @ec_transfer: A completion that will be completed once a message has been
126 * received (see nvec_rx_completed())
127 * @tx_lock: Spinlock for modifications on @tx_data
128 * @rx_lock: Spinlock for modifications on @rx_data
129 * @sync_write_mutex: A mutex for nvec_write_sync()
130 * @sync_write: A completion to signal that a synchronous message is complete
131 * @sync_write_pending: The first two bytes of the request (type and subtype)
132 * @last_sync_msg: The last synchronous message.
133 * @state: State of our finite state machine used in nvec_interrupt()
134 */
Marc Dietrich32890b92011-05-19 16:34:42 +0200135struct nvec_chip {
136 struct device *dev;
137 int gpio;
138 int irq;
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200139 int i2c_addr;
140 void __iomem *base;
141 struct clk *i2c_clk;
Marc Dietrich32890b92011-05-19 16:34:42 +0200142 struct atomic_notifier_head notifier_list;
143 struct list_head rx_data, tx_data;
144 struct notifier_block nvec_status_notifier;
145 struct work_struct rx_work, tx_work;
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200146 struct workqueue_struct *wq;
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200147 struct nvec_msg msg_pool[NVEC_POOL_SIZE];
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200148 struct nvec_msg *rx;
149
150 struct nvec_msg *tx;
151 struct nvec_msg tx_scratch;
152 struct completion ec_transfer;
153
154 spinlock_t tx_lock, rx_lock;
Marc Dietrich32890b92011-05-19 16:34:42 +0200155
Marc Dietrich162c7d82011-09-27 19:00:40 +0200156 /* sync write stuff */
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200157 struct mutex sync_write_mutex;
Marc Dietrich32890b92011-05-19 16:34:42 +0200158 struct completion sync_write;
159 u16 sync_write_pending;
160 struct nvec_msg *last_sync_msg;
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200161
162 int state;
Marc Dietrich32890b92011-05-19 16:34:42 +0200163};
164
Julian Andres Klode1b9bf622011-09-27 19:00:55 +0200165extern int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200166 short size);
Marc Dietrich32890b92011-05-19 16:34:42 +0200167
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200168extern struct nvec_msg *nvec_write_sync(struct nvec_chip *nvec,
169 const unsigned char *data, short size);
170
Marc Dietrich32890b92011-05-19 16:34:42 +0200171extern int nvec_register_notifier(struct nvec_chip *nvec,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200172 struct notifier_block *nb,
173 unsigned int events);
Marc Dietrich32890b92011-05-19 16:34:42 +0200174
Marc Dietrich218468f2013-04-29 23:14:51 +0200175extern int nvec_unregister_notifier(struct nvec_chip *dev,
176 struct notifier_block *nb);
Julian Andres Klode198dd262011-09-27 19:00:58 +0200177
178extern void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg);
179
Marc Dietrich32890b92011-05-19 16:34:42 +0200180#endif