blob: e271375053faa01729e1a91f096a4491e148dba0 [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>
Stephen Warrenc0df5bf2013-11-06 16:46:24 -070026#include <linux/reset.h>
Julian Andres Klode12b5a552011-09-27 19:01:06 +020027#include <linux/spinlock.h>
28#include <linux/workqueue.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020029
Julian Andres Klode0b1076c2011-09-27 19:00:48 +020030/* NVEC_POOL_SIZE - Size of the pool in &struct nvec_msg */
31#define NVEC_POOL_SIZE 64
32
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020033/*
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 Klodebdf034d2011-09-27 19:00:56 +020042/**
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. Mattock535f2a52012-03-19 08:17:49 -070046 * @NVEC_VAR_SIZE: The message has one command byte, one count byte, and has
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020047 * 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 */
53enum nvec_event_size {
Marc Dietrich32890b92011-05-19 16:34:42 +020054 NVEC_2BYTES,
55 NVEC_3BYTES,
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020056 NVEC_VAR_SIZE,
57};
Marc Dietrich32890b92011-05-19 16:34:42 +020058
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020059/**
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 */
72enum nvec_msg_type {
Marc Dietrich162c7d82011-09-27 19:00:40 +020073 NVEC_SYS = 1,
Marc Dietrich32890b92011-05-19 16:34:42 +020074 NVEC_BAT,
Marc Dietrich518945f2013-01-27 17:43:41 +010075 NVEC_GPIO,
76 NVEC_SLEEP,
77 NVEC_KBD,
Marc Dietrich32890b92011-05-19 16:34:42 +020078 NVEC_PS2,
79 NVEC_CNTL,
Marc Dietrich93eff832013-01-27 17:43:43 +010080 NVEC_OEM0 = 0x0d,
Marc Dietrich32890b92011-05-19 16:34:42 +020081 NVEC_KB_EVT = 0x80,
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020082 NVEC_PS2_EVT,
83};
Marc Dietrich32890b92011-05-19 16:34:42 +020084
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020085/**
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 Dietrich32890b92011-05-19 16:34:42 +020098struct nvec_msg {
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020099 struct list_head node;
100 unsigned char data[NVEC_MSG_SIZE];
Marc Dietrich32890b92011-05-19 16:34:42 +0200101 unsigned short size;
102 unsigned short pos;
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200103 atomic_t used;
Marc Dietrich32890b92011-05-19 16:34:42 +0200104};
105
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200106/**
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200107 * 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 Warrenc0df5bf2013-11-06 16:46:24 -0700113 * @i2c_clk: The clock of the I2C device
114 * @rst: The reset of the I2C device
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200115 * @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 Dietrich32890b92011-05-19 16:34:42 +0200137struct nvec_chip {
138 struct device *dev;
139 int gpio;
140 int irq;
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200141 int i2c_addr;
142 void __iomem *base;
143 struct clk *i2c_clk;
Stephen Warrenc0df5bf2013-11-06 16:46:24 -0700144 struct reset_control *rst;
Marc Dietrich32890b92011-05-19 16:34:42 +0200145 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 Klode0cab4cb2011-09-27 19:00:51 +0200149 struct workqueue_struct *wq;
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200150 struct nvec_msg msg_pool[NVEC_POOL_SIZE];
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200151 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 Dietrich32890b92011-05-19 16:34:42 +0200158
Marc Dietrich162c7d82011-09-27 19:00:40 +0200159 /* sync write stuff */
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200160 struct mutex sync_write_mutex;
Marc Dietrich32890b92011-05-19 16:34:42 +0200161 struct completion sync_write;
162 u16 sync_write_pending;
163 struct nvec_msg *last_sync_msg;
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200164
165 int state;
Marc Dietrich32890b92011-05-19 16:34:42 +0200166};
167
Julian Andres Klode1b9bf622011-09-27 19:00:55 +0200168extern int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200169 short size);
Marc Dietrich32890b92011-05-19 16:34:42 +0200170
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200171extern struct nvec_msg *nvec_write_sync(struct nvec_chip *nvec,
172 const unsigned char *data, short size);
173
Marc Dietrich32890b92011-05-19 16:34:42 +0200174extern int nvec_register_notifier(struct nvec_chip *nvec,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200175 struct notifier_block *nb,
176 unsigned int events);
Marc Dietrich32890b92011-05-19 16:34:42 +0200177
Marc Dietrich218468f2013-04-29 23:14:51 +0200178extern int nvec_unregister_notifier(struct nvec_chip *dev,
179 struct notifier_block *nb);
Julian Andres Klode198dd262011-09-27 19:00:58 +0200180
181extern void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg);
182
Marc Dietrich32890b92011-05-19 16:34:42 +0200183#endif