blob: 80c0353f141c3fad47015ccbe868a7d81300f782 [file] [log] [blame]
Greg Kroah-Hartman971bcfc2018-01-11 11:08:42 +01001/* SPDX-License-Identifier: GPL-2.0 */
Marc Dietrich162c7d82011-09-27 19:00:40 +02002/*
3 * NVEC: NVIDIA compliant embedded controller interface
4 *
5 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net>
6 *
7 * Authors: Pierre-Hugues Husson <phhusson@free.fr>
8 * Ilya Petrov <ilya.muromec@gmail.com>
9 * Marc Dietrich <marvin24@gmx.de>
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020010 * Julian Andres Klode <jak@jak-linux.org>
Marc Dietrich162c7d82011-09-27 19:00:40 +020011 */
12
Marc Dietrich32890b92011-05-19 16:34:42 +020013#ifndef __LINUX_MFD_NVEC
14#define __LINUX_MFD_NVEC
15
Julian Andres Klode0b1076c2011-09-27 19:00:48 +020016#include <linux/atomic.h>
Julian Andres Klode12b5a552011-09-27 19:01:06 +020017#include <linux/clk.h>
18#include <linux/completion.h>
19#include <linux/list.h>
20#include <linux/mutex.h>
Julian Andres Klode79740352011-09-27 19:00:39 +020021#include <linux/notifier.h>
Stephen Warrenc0df5bf2013-11-06 16:46:24 -070022#include <linux/reset.h>
Julian Andres Klode12b5a552011-09-27 19:01:06 +020023#include <linux/spinlock.h>
24#include <linux/workqueue.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020025
Julian Andres Klode0b1076c2011-09-27 19:00:48 +020026/* NVEC_POOL_SIZE - Size of the pool in &struct nvec_msg */
27#define NVEC_POOL_SIZE 64
28
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020029/*
30 * NVEC_MSG_SIZE - Maximum size of the data field of &struct nvec_msg.
31 *
32 * A message must store up to a SMBus block operation which consists of
33 * one command byte, one count byte, and up to 32 payload bytes = 34
34 * byte.
35 */
36#define NVEC_MSG_SIZE 34
37
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020038/**
39 * enum nvec_event_size - The size of an event message
40 * @NVEC_2BYTES: The message has one command byte and one data byte
41 * @NVEC_3BYTES: The message has one command byte and two data bytes
Justin P. Mattock535f2a52012-03-19 08:17:49 -070042 * @NVEC_VAR_SIZE: The message has one command byte, one count byte, and has
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020043 * up to as many bytes as the number in the count byte. The
44 * maximum is 32
45 *
46 * Events can be fixed or variable sized. This is useless on other message
47 * types, which are always variable sized.
48 */
49enum nvec_event_size {
Marc Dietrich32890b92011-05-19 16:34:42 +020050 NVEC_2BYTES,
51 NVEC_3BYTES,
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020052 NVEC_VAR_SIZE,
53};
Marc Dietrich32890b92011-05-19 16:34:42 +020054
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020055/**
56 * enum nvec_msg_type - The type of a message
57 * @NVEC_SYS: A system request/response
58 * @NVEC_BAT: A battery request/response
59 * @NVEC_KBD: A keyboard request/response
60 * @NVEC_PS2: A mouse request/response
61 * @NVEC_CNTL: A EC control request/response
62 * @NVEC_KB_EVT: An event from the keyboard
63 * @NVEC_PS2_EVT: An event from the mouse
64 *
65 * Events can be fixed or variable sized. This is useless on other message
66 * types, which are always variable sized.
67 */
68enum nvec_msg_type {
Marc Dietrich162c7d82011-09-27 19:00:40 +020069 NVEC_SYS = 1,
Marc Dietrich32890b92011-05-19 16:34:42 +020070 NVEC_BAT,
Marc Dietrich518945f2013-01-27 17:43:41 +010071 NVEC_GPIO,
72 NVEC_SLEEP,
73 NVEC_KBD,
Marc Dietrich32890b92011-05-19 16:34:42 +020074 NVEC_PS2,
75 NVEC_CNTL,
Marc Dietrich93eff832013-01-27 17:43:43 +010076 NVEC_OEM0 = 0x0d,
Marc Dietrich32890b92011-05-19 16:34:42 +020077 NVEC_KB_EVT = 0x80,
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020078 NVEC_PS2_EVT,
79};
Marc Dietrich32890b92011-05-19 16:34:42 +020080
Julian Andres Klodebdf034d2011-09-27 19:00:56 +020081/**
82 * struct nvec_msg - A buffer for a single message
83 * @node: Messages are part of various lists in a &struct nvec_chip
84 * @data: The data of the message
85 * @size: For TX messages, the number of bytes used in @data
86 * @pos: For RX messages, the current position to write to. For TX messages,
87 * the position to read from.
88 * @used: Used for the message pool to mark a message as free/allocated.
89 *
90 * This structure is used to hold outgoing and incoming messages. Outgoing
91 * messages have a different format than incoming messages, and that is not
92 * documented yet.
93 */
Marc Dietrich32890b92011-05-19 16:34:42 +020094struct nvec_msg {
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020095 struct list_head node;
96 unsigned char data[NVEC_MSG_SIZE];
Marc Dietrich32890b92011-05-19 16:34:42 +020097 unsigned short size;
98 unsigned short pos;
Julian Andres Klode0b1076c2011-09-27 19:00:48 +020099 atomic_t used;
Marc Dietrich32890b92011-05-19 16:34:42 +0200100};
101
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200102/**
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200103 * struct nvec_chip - A single connection to an NVIDIA Embedded controller
104 * @dev: The device
105 * @gpio: The same as for &struct nvec_platform_data
106 * @irq: The IRQ of the I2C device
107 * @i2c_addr: The address of the I2C slave
108 * @base: The base of the memory mapped region of the I2C device
Stephen Warrenc0df5bf2013-11-06 16:46:24 -0700109 * @i2c_clk: The clock of the I2C device
110 * @rst: The reset of the I2C device
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200111 * @notifier_list: Notifiers to be called on received messages, see
112 * nvec_register_notifier()
113 * @rx_data: Received messages that have to be processed
114 * @tx_data: Messages waiting to be sent to the controller
115 * @nvec_status_notifier: Internal notifier (see nvec_status_notifier())
116 * @rx_work: A work structure for the RX worker nvec_dispatch()
117 * @tx_work: A work structure for the TX worker nvec_request_master()
118 * @wq: The work queue in which @rx_work and @tx_work are executed
119 * @rx: The message currently being retrieved or %NULL
120 * @msg_pool: A pool of messages for allocation
121 * @tx: The message currently being transferred
122 * @tx_scratch: Used for building pseudo messages
123 * @ec_transfer: A completion that will be completed once a message has been
124 * received (see nvec_rx_completed())
125 * @tx_lock: Spinlock for modifications on @tx_data
126 * @rx_lock: Spinlock for modifications on @rx_data
127 * @sync_write_mutex: A mutex for nvec_write_sync()
128 * @sync_write: A completion to signal that a synchronous message is complete
129 * @sync_write_pending: The first two bytes of the request (type and subtype)
130 * @last_sync_msg: The last synchronous message.
131 * @state: State of our finite state machine used in nvec_interrupt()
132 */
Marc Dietrich32890b92011-05-19 16:34:42 +0200133struct nvec_chip {
134 struct device *dev;
Marc Dietrich40fbffd2018-04-19 13:01:50 +0200135 struct gpio_desc *gpiod;
Marc Dietrich32890b92011-05-19 16:34:42 +0200136 int irq;
Franck Demathieu4cf64a52017-02-13 09:35:19 +0100137 u32 i2c_addr;
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200138 void __iomem *base;
139 struct clk *i2c_clk;
Stephen Warrenc0df5bf2013-11-06 16:46:24 -0700140 struct reset_control *rst;
Marc Dietrich32890b92011-05-19 16:34:42 +0200141 struct atomic_notifier_head notifier_list;
142 struct list_head rx_data, tx_data;
143 struct notifier_block nvec_status_notifier;
144 struct work_struct rx_work, tx_work;
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200145 struct workqueue_struct *wq;
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200146 struct nvec_msg msg_pool[NVEC_POOL_SIZE];
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200147 struct nvec_msg *rx;
148
149 struct nvec_msg *tx;
150 struct nvec_msg tx_scratch;
151 struct completion ec_transfer;
152
153 spinlock_t tx_lock, rx_lock;
Marc Dietrich32890b92011-05-19 16:34:42 +0200154
Marc Dietrich162c7d82011-09-27 19:00:40 +0200155 /* sync write stuff */
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200156 struct mutex sync_write_mutex;
Marc Dietrich32890b92011-05-19 16:34:42 +0200157 struct completion sync_write;
158 u16 sync_write_pending;
159 struct nvec_msg *last_sync_msg;
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200160
161 int state;
Marc Dietrich32890b92011-05-19 16:34:42 +0200162};
163
Joe Perchesddc9bce2015-08-10 14:51:25 -0700164int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
165 short size);
Marc Dietrich32890b92011-05-19 16:34:42 +0200166
Tomás Tormo73b37c92016-02-14 20:30:56 +0100167int nvec_write_sync(struct nvec_chip *nvec,
168 const unsigned char *data, short size,
169 struct nvec_msg **msg);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200170
Joe Perchesddc9bce2015-08-10 14:51:25 -0700171int nvec_register_notifier(struct nvec_chip *nvec,
172 struct notifier_block *nb,
173 unsigned int events);
Marc Dietrich32890b92011-05-19 16:34:42 +0200174
Joe Perchesddc9bce2015-08-10 14:51:25 -0700175int nvec_unregister_notifier(struct nvec_chip *dev, struct notifier_block *nb);
Julian Andres Klode198dd262011-09-27 19:00:58 +0200176
Joe Perchesddc9bce2015-08-10 14:51:25 -0700177void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg);
Julian Andres Klode198dd262011-09-27 19:00:58 +0200178
Marc Dietrich32890b92011-05-19 16:34:42 +0200179#endif