blob: 4ae44a5168f94778c9ad3fec504f37608b63055d [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.lauchpad.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 Klode791c4a62011-09-27 19:00:53 +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 */
Marc Dietrich32890b92011-05-19 16:34:42 +020016
Marc Dietrich162c7d82011-09-27 19:00:40 +020017/* #define DEBUG */
Marc Dietrich32890b92011-05-19 16:34:42 +020018
Julian Andres Klode12b5a552011-09-27 19:01:06 +020019#include <linux/kernel.h>
Marc Dietrich3b769ed2011-10-10 11:35:45 +020020#include <linux/module.h>
Julian Andres Klode0b1076c2011-09-27 19:00:48 +020021#include <linux/atomic.h>
Julian Andres Klode12b5a552011-09-27 19:01:06 +020022#include <linux/clk.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020023#include <linux/completion.h>
Julian Andres Klode12b5a552011-09-27 19:01:06 +020024#include <linux/delay.h>
25#include <linux/err.h>
26#include <linux/gpio.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020027#include <linux/interrupt.h>
Marc Dietrich162c7d82011-09-27 19:00:40 +020028#include <linux/io.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020029#include <linux/irq.h>
Marc Dietrich7990b0d2011-11-01 21:37:04 +010030#include <linux/of.h>
31#include <linux/of_gpio.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020032#include <linux/list.h>
Julian Andres Klode12b5a552011-09-27 19:01:06 +020033#include <linux/mfd/core.h>
34#include <linux/mutex.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020035#include <linux/notifier.h>
Julian Andres Klode12b5a552011-09-27 19:01:06 +020036#include <linux/slab.h>
37#include <linux/spinlock.h>
38#include <linux/workqueue.h>
Marc Dietrich162c7d82011-09-27 19:00:40 +020039
Marc Dietrich32890b92011-05-19 16:34:42 +020040#include "nvec.h"
41
Julian Andres Klode391d2fa2011-09-27 19:00:57 +020042#define I2C_CNFG 0x00
Burcin Akalinb4129f22015-10-30 02:49:01 +030043#define I2C_CNFG_PACKET_MODE_EN (1 << 10)
44#define I2C_CNFG_NEW_MASTER_SFM (1 << 11)
Julian Andres Klode391d2fa2011-09-27 19:00:57 +020045#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
46
47#define I2C_SL_CNFG 0x20
Burcin Akalinb4129f22015-10-30 02:49:01 +030048#define I2C_SL_NEWSL (1 << 2)
49#define I2C_SL_NACK (1 << 1)
50#define I2C_SL_RESP (1 << 0)
51#define I2C_SL_IRQ (1 << 3)
52#define END_TRANS (1 << 4)
53#define RCVD (1 << 2)
54#define RNW (1 << 1)
Julian Andres Klode391d2fa2011-09-27 19:00:57 +020055
56#define I2C_SL_RCVD 0x24
57#define I2C_SL_STATUS 0x28
58#define I2C_SL_ADDR1 0x2c
59#define I2C_SL_ADDR2 0x30
60#define I2C_SL_DELAY_COUNT 0x3c
61
Julian Andres Klodebb0590e2011-09-27 19:00:59 +020062/**
63 * enum nvec_msg_category - Message categories for nvec_msg_alloc()
64 * @NVEC_MSG_RX: The message is an incoming message (from EC)
65 * @NVEC_MSG_TX: The message is an outgoing message (to EC)
66 */
67enum nvec_msg_category {
68 NVEC_MSG_RX,
69 NVEC_MSG_TX,
70};
71
Marc Dietrich518945f2013-01-27 17:43:41 +010072enum nvec_sleep_subcmds {
73 GLOBAL_EVENTS,
Marc Dietrich93eff832013-01-27 17:43:43 +010074 AP_PWR_DOWN,
75 AP_SUSPEND,
Marc Dietrich518945f2013-01-27 17:43:41 +010076};
77
Marc Dietrich93eff832013-01-27 17:43:43 +010078#define CNF_EVENT_REPORTING 0x01
79#define GET_FIRMWARE_VERSION 0x15
80#define LID_SWITCH BIT(1)
81#define PWR_BUTTON BIT(15)
Marc Dietrich32890b92011-05-19 16:34:42 +020082
83static struct nvec_chip *nvec_power_handle;
84
Geert Uytterhoeven28397dc2013-11-18 14:33:05 +010085static const struct mfd_cell nvec_devices[] = {
Marc Dietrichf686e9a2011-08-24 20:23:07 +020086 {
Marc Dietrich162c7d82011-09-27 19:00:40 +020087 .name = "nvec-kbd",
Marc Dietrichf686e9a2011-08-24 20:23:07 +020088 },
89 {
Marc Dietrich162c7d82011-09-27 19:00:40 +020090 .name = "nvec-mouse",
Marc Dietrich3ec69882015-01-20 23:01:28 +010091 },
92 {
93 .name = "nvec-power",
94 .id = 0,
Marc Dietrichf686e9a2011-08-24 20:23:07 +020095 },
96 {
Marc Dietrich162c7d82011-09-27 19:00:40 +020097 .name = "nvec-power",
98 .id = 1,
Marc Dietrichf686e9a2011-08-24 20:23:07 +020099 },
100 {
Marc Dietrichac562682012-06-24 23:25:20 +0200101 .name = "nvec-paz00",
Ilya Petrov97cc2652011-09-27 19:00:44 +0200102 },
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200103};
104
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200105/**
106 * nvec_register_notifier - Register a notifier with nvec
107 * @nvec: A &struct nvec_chip
108 * @nb: The notifier block to register
109 *
110 * Registers a notifier with @nvec. The notifier will be added to an atomic
111 * notifier chain that is called for all received messages except those that
112 * correspond to a request initiated by nvec_write_sync().
113 */
Marc Dietrich32890b92011-05-19 16:34:42 +0200114int nvec_register_notifier(struct nvec_chip *nvec, struct notifier_block *nb,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200115 unsigned int events)
Marc Dietrich32890b92011-05-19 16:34:42 +0200116{
117 return atomic_notifier_chain_register(&nvec->notifier_list, nb);
118}
119EXPORT_SYMBOL_GPL(nvec_register_notifier);
120
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200121/**
Marc Dietrich111c1582013-04-29 23:14:51 +0200122 * nvec_unregister_notifier - Unregister a notifier with nvec
123 * @nvec: A &struct nvec_chip
124 * @nb: The notifier block to unregister
125 *
126 * Unregisters a notifier with @nvec. The notifier will be removed from the
127 * atomic notifier chain.
128 */
129int nvec_unregister_notifier(struct nvec_chip *nvec, struct notifier_block *nb)
130{
131 return atomic_notifier_chain_unregister(&nvec->notifier_list, nb);
132}
133EXPORT_SYMBOL_GPL(nvec_unregister_notifier);
134
135/**
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200136 * nvec_status_notifier - The final notifier
137 *
138 * Prints a message about control events not handled in the notifier
139 * chain.
140 */
Marc Dietrich162c7d82011-09-27 19:00:40 +0200141static int nvec_status_notifier(struct notifier_block *nb,
142 unsigned long event_type, void *data)
Marc Dietrich32890b92011-05-19 16:34:42 +0200143{
Marc Dietrich50d46562012-06-24 23:25:17 +0200144 struct nvec_chip *nvec = container_of(nb, struct nvec_chip,
145 nvec_status_notifier);
Marc Dietrich32890b92011-05-19 16:34:42 +0200146 unsigned char *msg = (unsigned char *)data;
Marc Dietrich32890b92011-05-19 16:34:42 +0200147
Marc Dietrich162c7d82011-09-27 19:00:40 +0200148 if (event_type != NVEC_CNTL)
Marc Dietrich32890b92011-05-19 16:34:42 +0200149 return NOTIFY_DONE;
150
Marc Dietrich50d46562012-06-24 23:25:17 +0200151 dev_warn(nvec->dev, "unhandled msg type %ld\n", event_type);
Marc Dietricha3a9aa12011-09-27 19:00:41 +0200152 print_hex_dump(KERN_WARNING, "payload: ", DUMP_PREFIX_NONE, 16, 1,
153 msg, msg[1] + 2, true);
Marc Dietrich32890b92011-05-19 16:34:42 +0200154
155 return NOTIFY_OK;
156}
157
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200158/**
159 * nvec_msg_alloc:
160 * @nvec: A &struct nvec_chip
Julian Andres Klodebb0590e2011-09-27 19:00:59 +0200161 * @category: Pool category, see &enum nvec_msg_category
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200162 *
163 * Allocate a single &struct nvec_msg object from the message pool of
164 * @nvec. The result shall be passed to nvec_msg_free() if no longer
165 * used.
Julian Andres Klodebb0590e2011-09-27 19:00:59 +0200166 *
167 * Outgoing messages are placed in the upper 75% of the pool, keeping the
168 * lower 25% available for RX buffers only. The reason is to prevent a
169 * situation where all buffers are full and a message is thus endlessly
170 * retried because the response could never be processed.
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200171 */
Julian Andres Klodebb0590e2011-09-27 19:00:59 +0200172static struct nvec_msg *nvec_msg_alloc(struct nvec_chip *nvec,
173 enum nvec_msg_category category)
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200174{
Julian Andres Klodebb0590e2011-09-27 19:00:59 +0200175 int i = (category == NVEC_MSG_TX) ? (NVEC_POOL_SIZE / 4) : 0;
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200176
Julian Andres Klodebb0590e2011-09-27 19:00:59 +0200177 for (; i < NVEC_POOL_SIZE; i++) {
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200178 if (atomic_xchg(&nvec->msg_pool[i].used, 1) == 0) {
179 dev_vdbg(nvec->dev, "INFO: Allocate %i\n", i);
180 return &nvec->msg_pool[i];
181 }
182 }
183
Julian Andres Klodebb0590e2011-09-27 19:00:59 +0200184 dev_err(nvec->dev, "could not allocate %s buffer\n",
185 (category == NVEC_MSG_TX) ? "TX" : "RX");
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200186
187 return NULL;
188}
189
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200190/**
191 * nvec_msg_free:
192 * @nvec: A &struct nvec_chip
193 * @msg: A message (must be allocated by nvec_msg_alloc() and belong to @nvec)
194 *
195 * Free the given message
196 */
Denis Efremov705a4212013-05-09 13:19:37 +0400197void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg)
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200198{
Julian Andres Klode7b770652011-09-27 19:00:52 +0200199 if (msg != &nvec->tx_scratch)
200 dev_vdbg(nvec->dev, "INFO: Free %ti\n", msg - nvec->msg_pool);
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200201 atomic_set(&msg->used, 0);
202}
Julian Andres Klode198dd262011-09-27 19:00:58 +0200203EXPORT_SYMBOL_GPL(nvec_msg_free);
Julian Andres Klode0b1076c2011-09-27 19:00:48 +0200204
Julian Andres Klode8517e872011-09-27 19:00:50 +0200205/**
206 * nvec_msg_is_event - Return %true if @msg is an event
207 * @msg: A message
208 */
209static bool nvec_msg_is_event(struct nvec_msg *msg)
210{
211 return msg->data[0] >> 7;
212}
213
214/**
215 * nvec_msg_size - Get the size of a message
216 * @msg: The message to get the size for
217 *
218 * This only works for received messages, not for outgoing messages.
219 */
220static size_t nvec_msg_size(struct nvec_msg *msg)
221{
222 bool is_event = nvec_msg_is_event(msg);
223 int event_length = (msg->data[0] & 0x60) >> 5;
224
225 /* for variable size, payload size in byte 1 + count (1) + cmd (1) */
226 if (!is_event || event_length == NVEC_VAR_SIZE)
227 return (msg->pos || msg->size) ? (msg->data[1] + 2) : 0;
228 else if (event_length == NVEC_2BYTES)
229 return 2;
230 else if (event_length == NVEC_3BYTES)
231 return 3;
Pawel Lebiodaaea2cda2014-07-05 22:30:55 +0200232 return 0;
Julian Andres Klode8517e872011-09-27 19:00:50 +0200233}
234
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200235/**
236 * nvec_gpio_set_value - Set the GPIO value
237 * @nvec: A &struct nvec_chip
238 * @value: The value to write (0 or 1)
239 *
240 * Like gpio_set_value(), but generating debugging information
241 */
Julian Andres Klodee7c40852011-09-27 19:00:49 +0200242static void nvec_gpio_set_value(struct nvec_chip *nvec, int value)
243{
244 dev_dbg(nvec->dev, "GPIO changed from %u to %u\n",
245 gpio_get_value(nvec->gpio), value);
246 gpio_set_value(nvec->gpio, value);
247}
248
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200249/**
250 * nvec_write_async - Asynchronously write a message to NVEC
251 * @nvec: An nvec_chip instance
252 * @data: The message data, starting with the request type
253 * @size: The size of @data
254 *
255 * Queue a single message to be transferred to the embedded controller
256 * and return immediately.
257 *
258 * Returns: 0 on success, a negative error code on failure. If a failure
Gustavo A. R. Silva9b872a72015-01-11 15:49:13 -0600259 * occurred, the nvec driver may print an error.
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200260 */
Julian Andres Klode1b9bf622011-09-27 19:00:55 +0200261int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200262 short size)
Marc Dietrich32890b92011-05-19 16:34:42 +0200263{
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200264 struct nvec_msg *msg;
265 unsigned long flags;
Marc Dietrich32890b92011-05-19 16:34:42 +0200266
Julian Andres Klodebb0590e2011-09-27 19:00:59 +0200267 msg = nvec_msg_alloc(nvec, NVEC_MSG_TX);
268
Julian Andres Klode1b9bf622011-09-27 19:00:55 +0200269 if (msg == NULL)
270 return -ENOMEM;
271
Marc Dietrich32890b92011-05-19 16:34:42 +0200272 msg->data[0] = size;
273 memcpy(msg->data + 1, data, size);
274 msg->size = size + 1;
Marc Dietrich32890b92011-05-19 16:34:42 +0200275
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200276 spin_lock_irqsave(&nvec->tx_lock, flags);
Marc Dietrich32890b92011-05-19 16:34:42 +0200277 list_add_tail(&msg->node, &nvec->tx_data);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200278 spin_unlock_irqrestore(&nvec->tx_lock, flags);
Marc Dietrich32890b92011-05-19 16:34:42 +0200279
Linus Torvalds033d9952012-10-02 09:54:49 -0700280 schedule_work(&nvec->tx_work);
Julian Andres Klode1b9bf622011-09-27 19:00:55 +0200281
282 return 0;
Marc Dietrich32890b92011-05-19 16:34:42 +0200283}
284EXPORT_SYMBOL(nvec_write_async);
285
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200286/**
287 * nvec_write_sync - Write a message to nvec and read the response
288 * @nvec: An &struct nvec_chip
289 * @data: The data to write
290 * @size: The size of @data
291 *
292 * This is similar to nvec_write_async(), but waits for the
293 * request to be answered before returning. This function
294 * uses a mutex and can thus not be called from e.g.
295 * interrupt handlers.
296 *
297 * Returns: A pointer to the response message on success,
Julian Andres Klode198dd262011-09-27 19:00:58 +0200298 * %NULL on failure. Free with nvec_msg_free() once no longer
299 * used.
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200300 */
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200301struct nvec_msg *nvec_write_sync(struct nvec_chip *nvec,
302 const unsigned char *data, short size)
303{
304 struct nvec_msg *msg;
305
306 mutex_lock(&nvec->sync_write_mutex);
307
308 nvec->sync_write_pending = (data[1] << 8) + data[0];
Julian Andres Klode1b9bf622011-09-27 19:00:55 +0200309
Marc Dietrich4b8bf032012-08-19 19:03:07 +0200310 if (nvec_write_async(nvec, data, size) < 0) {
311 mutex_unlock(&nvec->sync_write_mutex);
Julian Andres Klode1b9bf622011-09-27 19:00:55 +0200312 return NULL;
Marc Dietrich4b8bf032012-08-19 19:03:07 +0200313 }
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200314
315 dev_dbg(nvec->dev, "nvec_sync_write: 0x%04x\n",
316 nvec->sync_write_pending);
317 if (!(wait_for_completion_timeout(&nvec->sync_write,
318 msecs_to_jiffies(2000)))) {
319 dev_warn(nvec->dev, "timeout waiting for sync write to complete\n");
320 mutex_unlock(&nvec->sync_write_mutex);
321 return NULL;
322 }
323
324 dev_dbg(nvec->dev, "nvec_sync_write: pong!\n");
325
326 msg = nvec->last_sync_msg;
327
328 mutex_unlock(&nvec->sync_write_mutex);
329
330 return msg;
331}
332EXPORT_SYMBOL(nvec_write_sync);
333
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200334/**
Marc Dietrich518945f2013-01-27 17:43:41 +0100335 * nvec_toggle_global_events - enables or disables global event reporting
336 * @nvec: nvec handle
337 * @state: true for enable, false for disable
338 *
339 * This switches on/off global event reports by the embedded controller.
340 */
341static void nvec_toggle_global_events(struct nvec_chip *nvec, bool state)
342{
343 unsigned char global_events[] = { NVEC_SLEEP, GLOBAL_EVENTS, state };
344
345 nvec_write_async(nvec, global_events, 3);
346}
347
348/**
Marc Dietrich93eff832013-01-27 17:43:43 +0100349 * nvec_event_mask - fill the command string with event bitfield
350 * ev: points to event command string
351 * mask: bit to insert into the event mask
352 *
353 * Configure event command expects a 32 bit bitfield which describes
354 * which events to enable. The bitfield has the following structure
355 * (from highest byte to lowest):
356 * system state bits 7-0
357 * system state bits 15-8
358 * oem system state bits 7-0
359 * oem system state bits 15-8
360 */
361static void nvec_event_mask(char *ev, u32 mask)
362{
Wei Yongjun815fb012013-03-16 22:51:25 +0800363 ev[3] = mask >> 16 & 0xff;
364 ev[4] = mask >> 24 & 0xff;
365 ev[5] = mask >> 0 & 0xff;
366 ev[6] = mask >> 8 & 0xff;
Marc Dietrich93eff832013-01-27 17:43:43 +0100367}
368
369/**
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200370 * nvec_request_master - Process outgoing messages
371 * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
372 *
373 * Processes all outgoing requests by sending the request and awaiting the
374 * response, then continuing with the next request. Once a request has a
375 * matching response, it will be freed and removed from the list.
376 */
Marc Dietrich32890b92011-05-19 16:34:42 +0200377static void nvec_request_master(struct work_struct *work)
378{
379 struct nvec_chip *nvec = container_of(work, struct nvec_chip, tx_work);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200380 unsigned long flags;
381 long err;
382 struct nvec_msg *msg;
Marc Dietrich32890b92011-05-19 16:34:42 +0200383
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200384 spin_lock_irqsave(&nvec->tx_lock, flags);
385 while (!list_empty(&nvec->tx_data)) {
386 msg = list_first_entry(&nvec->tx_data, struct nvec_msg, node);
387 spin_unlock_irqrestore(&nvec->tx_lock, flags);
388 nvec_gpio_set_value(nvec, 0);
389 err = wait_for_completion_interruptible_timeout(
390 &nvec->ec_transfer, msecs_to_jiffies(5000));
391
392 if (err == 0) {
393 dev_warn(nvec->dev, "timeout waiting for ec transfer\n");
394 nvec_gpio_set_value(nvec, 1);
395 msg->pos = 0;
396 }
397
398 spin_lock_irqsave(&nvec->tx_lock, flags);
399
400 if (err > 0) {
401 list_del_init(&msg->node);
402 nvec_msg_free(nvec, msg);
403 }
404 }
405 spin_unlock_irqrestore(&nvec->tx_lock, flags);
Marc Dietrich32890b92011-05-19 16:34:42 +0200406}
407
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200408/**
409 * parse_msg - Print some information and call the notifiers on an RX message
410 * @nvec: A &struct nvec_chip
411 * @msg: A message received by @nvec
412 *
413 * Paarse some pieces of the message and then call the chain of notifiers
414 * registered via nvec_register_notifier.
415 */
Marc Dietrich32890b92011-05-19 16:34:42 +0200416static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg)
417{
Marc Dietrich162c7d82011-09-27 19:00:40 +0200418 if ((msg->data[0] & 1 << 7) == 0 && msg->data[3]) {
Andy Shevchenko6a371972012-08-02 19:05:46 +0300419 dev_err(nvec->dev, "ec responded %*ph\n", 4, msg->data);
Marc Dietrich32890b92011-05-19 16:34:42 +0200420 return -EINVAL;
421 }
422
Marc Dietricha3a9aa12011-09-27 19:00:41 +0200423 if ((msg->data[0] >> 7) == 1 && (msg->data[0] & 0x0f) == 5)
424 print_hex_dump(KERN_WARNING, "ec system event ",
425 DUMP_PREFIX_NONE, 16, 1, msg->data,
426 msg->data[1] + 2, true);
Marc Dietrich32890b92011-05-19 16:34:42 +0200427
Marc Dietrich162c7d82011-09-27 19:00:40 +0200428 atomic_notifier_call_chain(&nvec->notifier_list, msg->data[0] & 0x8f,
429 msg->data);
Marc Dietrich32890b92011-05-19 16:34:42 +0200430
431 return 0;
432}
433
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200434/**
435 * nvec_dispatch - Process messages received from the EC
436 * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
437 *
438 * Process messages previously received from the EC and put into the RX
439 * queue of the &struct nvec_chip instance associated with @work.
440 */
Marc Dietrich32890b92011-05-19 16:34:42 +0200441static void nvec_dispatch(struct work_struct *work)
442{
443 struct nvec_chip *nvec = container_of(work, struct nvec_chip, rx_work);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200444 unsigned long flags;
Marc Dietrich32890b92011-05-19 16:34:42 +0200445 struct nvec_msg *msg;
446
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200447 spin_lock_irqsave(&nvec->rx_lock, flags);
Marc Dietrich162c7d82011-09-27 19:00:40 +0200448 while (!list_empty(&nvec->rx_data)) {
Marc Dietrich32890b92011-05-19 16:34:42 +0200449 msg = list_first_entry(&nvec->rx_data, struct nvec_msg, node);
450 list_del_init(&msg->node);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200451 spin_unlock_irqrestore(&nvec->rx_lock, flags);
Marc Dietrich32890b92011-05-19 16:34:42 +0200452
Marc Dietrich162c7d82011-09-27 19:00:40 +0200453 if (nvec->sync_write_pending ==
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200454 (msg->data[2] << 8) + msg->data[0]) {
Marc Dietrich32890b92011-05-19 16:34:42 +0200455 dev_dbg(nvec->dev, "sync write completed!\n");
456 nvec->sync_write_pending = 0;
457 nvec->last_sync_msg = msg;
458 complete(&nvec->sync_write);
459 } else {
460 parse_msg(nvec, msg);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200461 nvec_msg_free(nvec, msg);
Marc Dietrich32890b92011-05-19 16:34:42 +0200462 }
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200463 spin_lock_irqsave(&nvec->rx_lock, flags);
464 }
465 spin_unlock_irqrestore(&nvec->rx_lock, flags);
466}
467
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200468/**
469 * nvec_tx_completed - Complete the current transfer
470 * @nvec: A &struct nvec_chip
471 *
472 * This is called when we have received an END_TRANS on a TX transfer.
473 */
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200474static void nvec_tx_completed(struct nvec_chip *nvec)
475{
476 /* We got an END_TRANS, let's skip this, maybe there's an event */
477 if (nvec->tx->pos != nvec->tx->size) {
478 dev_err(nvec->dev, "premature END_TRANS, resending\n");
479 nvec->tx->pos = 0;
480 nvec_gpio_set_value(nvec, 0);
481 } else {
482 nvec->state = 0;
Marc Dietrich32890b92011-05-19 16:34:42 +0200483 }
484}
485
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200486/**
487 * nvec_rx_completed - Complete the current transfer
488 * @nvec: A &struct nvec_chip
489 *
490 * This is called when we have received an END_TRANS on a RX transfer.
491 */
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200492static void nvec_rx_completed(struct nvec_chip *nvec)
493{
Julian Andres Klode210ceb42011-09-27 19:01:01 +0200494 if (nvec->rx->pos != nvec_msg_size(nvec->rx)) {
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200495 dev_err(nvec->dev, "RX incomplete: Expected %u bytes, got %u\n",
496 (uint) nvec_msg_size(nvec->rx),
497 (uint) nvec->rx->pos);
498
Julian Andres Klode210ceb42011-09-27 19:01:01 +0200499 nvec_msg_free(nvec, nvec->rx);
500 nvec->state = 0;
Julian Andres Kloded6bdcf22011-09-27 19:01:04 +0200501
502 /* Battery quirk - Often incomplete, and likes to crash */
503 if (nvec->rx->data[0] == NVEC_BAT)
504 complete(&nvec->ec_transfer);
505
Julian Andres Klode210ceb42011-09-27 19:01:01 +0200506 return;
507 }
508
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200509 spin_lock(&nvec->rx_lock);
510
511 /* add the received data to the work list
512 and move the ring buffer pointer to the next entry */
513 list_add_tail(&nvec->rx->node, &nvec->rx_data);
514
515 spin_unlock(&nvec->rx_lock);
516
517 nvec->state = 0;
518
519 if (!nvec_msg_is_event(nvec->rx))
520 complete(&nvec->ec_transfer);
521
Linus Torvalds033d9952012-10-02 09:54:49 -0700522 schedule_work(&nvec->rx_work);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200523}
524
525/**
526 * nvec_invalid_flags - Send an error message about invalid flags and jump
527 * @nvec: The nvec device
528 * @status: The status flags
529 * @reset: Whether we shall jump to state 0.
530 */
531static void nvec_invalid_flags(struct nvec_chip *nvec, unsigned int status,
532 bool reset)
533{
534 dev_err(nvec->dev, "unexpected status flags 0x%02x during state %i\n",
535 status, nvec->state);
536 if (reset)
537 nvec->state = 0;
538}
539
540/**
541 * nvec_tx_set - Set the message to transfer (nvec->tx)
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200542 * @nvec: A &struct nvec_chip
543 *
544 * Gets the first entry from the tx_data list of @nvec and sets the
545 * tx member to it. If the tx_data list is empty, this uses the
546 * tx_scratch message to send a no operation message.
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200547 */
548static void nvec_tx_set(struct nvec_chip *nvec)
549{
550 spin_lock(&nvec->tx_lock);
551 if (list_empty(&nvec->tx_data)) {
552 dev_err(nvec->dev, "empty tx - sending no-op\n");
553 memcpy(nvec->tx_scratch.data, "\x02\x07\x02", 3);
554 nvec->tx_scratch.size = 3;
555 nvec->tx_scratch.pos = 0;
556 nvec->tx = &nvec->tx_scratch;
557 list_add_tail(&nvec->tx->node, &nvec->tx_data);
558 } else {
559 nvec->tx = list_first_entry(&nvec->tx_data, struct nvec_msg,
560 node);
561 nvec->tx->pos = 0;
562 }
563 spin_unlock(&nvec->tx_lock);
564
565 dev_dbg(nvec->dev, "Sending message of length %u, command 0x%x\n",
566 (uint)nvec->tx->size, nvec->tx->data[1]);
567}
568
569/**
570 * nvec_interrupt - Interrupt handler
571 * @irq: The IRQ
572 * @dev: The nvec device
Julian Andres Klodebdf034d2011-09-27 19:00:56 +0200573 *
574 * Interrupt handler that fills our RX buffers and empties our TX
575 * buffers. This uses a finite state machine with ridiculous amounts
576 * of error checking, in order to be fairly reliable.
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200577 */
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200578static irqreturn_t nvec_interrupt(int irq, void *dev)
Marc Dietrich32890b92011-05-19 16:34:42 +0200579{
580 unsigned long status;
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200581 unsigned int received = 0;
582 unsigned char to_send = 0xff;
583 const unsigned long irq_mask = I2C_SL_IRQ | END_TRANS | RCVD | RNW;
584 struct nvec_chip *nvec = dev;
585 unsigned int state = nvec->state;
Marc Dietrich32890b92011-05-19 16:34:42 +0200586
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200587 status = readl(nvec->base + I2C_SL_STATUS);
Marc Dietrich32890b92011-05-19 16:34:42 +0200588
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200589 /* Filter out some errors */
590 if ((status & irq_mask) == 0 && (status & ~irq_mask) != 0) {
591 dev_err(nvec->dev, "unexpected irq mask %lx\n", status);
Marc Dietrich32890b92011-05-19 16:34:42 +0200592 return IRQ_HANDLED;
Marc Dietrich32890b92011-05-19 16:34:42 +0200593 }
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200594 if ((status & I2C_SL_IRQ) == 0) {
595 dev_err(nvec->dev, "Spurious IRQ\n");
596 return IRQ_HANDLED;
597 }
598
599 /* The EC did not request a read, so it send us something, read it */
600 if ((status & RNW) == 0) {
601 received = readl(nvec->base + I2C_SL_RCVD);
602 if (status & RCVD)
603 writel(0, nvec->base + I2C_SL_RCVD);
604 }
605
606 if (status == (I2C_SL_IRQ | RCVD))
607 nvec->state = 0;
608
609 switch (nvec->state) {
610 case 0: /* Verify that its a transfer start, the rest later */
611 if (status != (I2C_SL_IRQ | RCVD))
612 nvec_invalid_flags(nvec, status, false);
613 break;
614 case 1: /* command byte */
615 if (status != I2C_SL_IRQ) {
616 nvec_invalid_flags(nvec, status, true);
617 } else {
Julian Andres Klodebb0590e2011-09-27 19:00:59 +0200618 nvec->rx = nvec_msg_alloc(nvec, NVEC_MSG_RX);
Julian Andres Klode8da79862011-09-27 19:01:00 +0200619 /* Should not happen in a normal world */
620 if (unlikely(nvec->rx == NULL)) {
621 nvec->state = 0;
622 break;
623 }
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200624 nvec->rx->data[0] = received;
625 nvec->rx->pos = 1;
626 nvec->state = 2;
627 }
628 break;
629 case 2: /* first byte after command */
630 if (status == (I2C_SL_IRQ | RNW | RCVD)) {
631 udelay(33);
632 if (nvec->rx->data[0] != 0x01) {
633 dev_err(nvec->dev,
634 "Read without prior read command\n");
635 nvec->state = 0;
636 break;
637 }
638 nvec_msg_free(nvec, nvec->rx);
639 nvec->state = 3;
640 nvec_tx_set(nvec);
641 BUG_ON(nvec->tx->size < 1);
642 to_send = nvec->tx->data[0];
643 nvec->tx->pos = 1;
644 } else if (status == (I2C_SL_IRQ)) {
645 BUG_ON(nvec->rx == NULL);
646 nvec->rx->data[1] = received;
647 nvec->rx->pos = 2;
648 nvec->state = 4;
649 } else {
650 nvec_invalid_flags(nvec, status, true);
651 }
652 break;
653 case 3: /* EC does a block read, we transmit data */
654 if (status & END_TRANS) {
655 nvec_tx_completed(nvec);
656 } else if ((status & RNW) == 0 || (status & RCVD)) {
657 nvec_invalid_flags(nvec, status, true);
658 } else if (nvec->tx && nvec->tx->pos < nvec->tx->size) {
659 to_send = nvec->tx->data[nvec->tx->pos++];
660 } else {
661 dev_err(nvec->dev, "tx buffer underflow on %p (%u > %u)\n",
662 nvec->tx,
663 (uint) (nvec->tx ? nvec->tx->pos : 0),
664 (uint) (nvec->tx ? nvec->tx->size : 0));
665 nvec->state = 0;
666 }
667 break;
668 case 4: /* EC does some write, we read the data */
669 if ((status & (END_TRANS | RNW)) == END_TRANS)
670 nvec_rx_completed(nvec);
671 else if (status & (RNW | RCVD))
672 nvec_invalid_flags(nvec, status, true);
673 else if (nvec->rx && nvec->rx->pos < NVEC_MSG_SIZE)
674 nvec->rx->data[nvec->rx->pos++] = received;
675 else
676 dev_err(nvec->dev,
Ebru Akagunduzb77f2762014-03-05 22:48:39 +0200677 "RX buffer overflow on %p: Trying to write byte %u of %u\n",
Dan Carpenter6330f9c2013-11-07 14:25:32 +0300678 nvec->rx, nvec->rx ? nvec->rx->pos : 0,
679 NVEC_MSG_SIZE);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200680 break;
681 default:
682 nvec->state = 0;
683 }
684
685 /* If we are told that a new transfer starts, verify it */
686 if ((status & (RCVD | RNW)) == RCVD) {
687 if (received != nvec->i2c_addr)
688 dev_err(nvec->dev,
689 "received address 0x%02x, expected 0x%02x\n",
690 received, nvec->i2c_addr);
691 nvec->state = 1;
692 }
693
694 /* Send data if requested, but not on end of transmission */
695 if ((status & (RNW | END_TRANS)) == RNW)
696 writel(to_send, nvec->base + I2C_SL_RCVD);
697
698 /* If we have send the first byte */
699 if (status == (I2C_SL_IRQ | RNW | RCVD))
700 nvec_gpio_set_value(nvec, 1);
701
702 dev_dbg(nvec->dev,
703 "Handled: %s 0x%02x, %s 0x%02x in state %u [%s%s%s]\n",
704 (status & RNW) == 0 ? "received" : "R=",
705 received,
706 (status & (RNW | END_TRANS)) ? "sent" : "S=",
707 to_send,
708 state,
709 status & END_TRANS ? " END_TRANS" : "",
710 status & RCVD ? " RCVD" : "",
711 status & RNW ? " RNW" : "");
712
Julian Andres Klodede839b82011-09-27 19:01:07 +0200713 /*
714 * TODO: A correct fix needs to be found for this.
715 *
716 * We experience less incomplete messages with this delay than without
717 * it, but we don't know why. Help is appreciated.
718 */
719 udelay(100);
720
Marc Dietrich32890b92011-05-19 16:34:42 +0200721 return IRQ_HANDLED;
722}
723
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200724static void tegra_init_i2c_slave(struct nvec_chip *nvec)
Marc Dietrich32890b92011-05-19 16:34:42 +0200725{
726 u32 val;
727
Prashant Gaikwad61c3b192012-06-05 09:59:41 +0530728 clk_prepare_enable(nvec->i2c_clk);
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200729
Stephen Warrenc0df5bf2013-11-06 16:46:24 -0700730 reset_control_assert(nvec->rst);
Marc Dietrich32890b92011-05-19 16:34:42 +0200731 udelay(2);
Stephen Warrenc0df5bf2013-11-06 16:46:24 -0700732 reset_control_deassert(nvec->rst);
Marc Dietrich32890b92011-05-19 16:34:42 +0200733
Marc Dietrichac810752011-09-27 19:00:42 +0200734 val = I2C_CNFG_NEW_MASTER_SFM | I2C_CNFG_PACKET_MODE_EN |
735 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
736 writel(val, nvec->base + I2C_CNFG);
737
738 clk_set_rate(nvec->i2c_clk, 8 * 80000);
739
Julian Andres Kloded3f862a2011-12-26 17:57:38 +0100740 writel(I2C_SL_NEWSL, nvec->base + I2C_SL_CNFG);
Marc Dietrichac810752011-09-27 19:00:42 +0200741 writel(0x1E, nvec->base + I2C_SL_DELAY_COUNT);
742
Burcin Akalina9548c22015-10-30 03:04:03 +0300743 writel(nvec->i2c_addr >> 1, nvec->base + I2C_SL_ADDR1);
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200744 writel(0, nvec->base + I2C_SL_ADDR2);
Marc Dietrich32890b92011-05-19 16:34:42 +0200745
Marc Dietrichac810752011-09-27 19:00:42 +0200746 enable_irq(nvec->irq);
Marc Dietrich32890b92011-05-19 16:34:42 +0200747}
748
Marc Dietrichebefae22012-06-24 23:25:19 +0200749#ifdef CONFIG_PM_SLEEP
Marc Dietrichac810752011-09-27 19:00:42 +0200750static void nvec_disable_i2c_slave(struct nvec_chip *nvec)
751{
752 disable_irq(nvec->irq);
Julian Andres Kloded3f862a2011-12-26 17:57:38 +0100753 writel(I2C_SL_NEWSL | I2C_SL_NACK, nvec->base + I2C_SL_CNFG);
Prashant Gaikwad61c3b192012-06-05 09:59:41 +0530754 clk_disable_unprepare(nvec->i2c_clk);
Marc Dietrichac810752011-09-27 19:00:42 +0200755}
Marc Dietrichebefae22012-06-24 23:25:19 +0200756#endif
Marc Dietrichac810752011-09-27 19:00:42 +0200757
Marc Dietrich32890b92011-05-19 16:34:42 +0200758static void nvec_power_off(void)
759{
Marc Dietrich93eff832013-01-27 17:43:43 +0100760 char ap_pwr_down[] = { NVEC_SLEEP, AP_PWR_DOWN };
761
Marc Dietrich518945f2013-01-27 17:43:41 +0100762 nvec_toggle_global_events(nvec_power_handle, false);
Marc Dietrich93eff832013-01-27 17:43:43 +0100763 nvec_write_async(nvec_power_handle, ap_pwr_down, 2);
Marc Dietrich32890b92011-05-19 16:34:42 +0200764}
765
Marc Dietrich95cd1862013-06-21 10:28:55 +0200766/*
767 * Parse common device tree data
768 */
769static int nvec_i2c_parse_dt_pdata(struct nvec_chip *nvec)
770{
771 nvec->gpio = of_get_named_gpio(nvec->dev->of_node, "request-gpios", 0);
772
773 if (nvec->gpio < 0) {
774 dev_err(nvec->dev, "no gpio specified");
775 return -ENODEV;
776 }
777
778 if (of_property_read_u32(nvec->dev->of_node, "slave-addr",
779 &nvec->i2c_addr)) {
780 dev_err(nvec->dev, "no i2c address specified");
781 return -ENODEV;
782 }
783
784 return 0;
785}
786
Bill Pemberton46620802012-11-19 13:22:07 -0500787static int tegra_nvec_probe(struct platform_device *pdev)
Marc Dietrich32890b92011-05-19 16:34:42 +0200788{
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200789 int err, ret;
Marc Dietrich32890b92011-05-19 16:34:42 +0200790 struct clk *i2c_clk;
Marc Dietrich32890b92011-05-19 16:34:42 +0200791 struct nvec_chip *nvec;
792 struct nvec_msg *msg;
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200793 struct resource *res;
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200794 void __iomem *base;
Marc Dietrich93eff832013-01-27 17:43:43 +0100795 char get_firmware_version[] = { NVEC_CNTL, GET_FIRMWARE_VERSION },
796 unmute_speakers[] = { NVEC_OEM0, 0x10, 0x59, 0x95 },
797 enable_event[7] = { NVEC_SYS, CNF_EVENT_REPORTING, true };
Marc Dietrich32890b92011-05-19 16:34:42 +0200798
Tugce Sirin15f1df52013-10-02 20:04:31 +0300799 if (!pdev->dev.of_node) {
Marc Dietrich2c6cbdd2013-06-21 10:28:54 +0200800 dev_err(&pdev->dev, "must be instantiated using device tree\n");
801 return -ENODEV;
802 }
803
Marc Dietrichf5e33522012-06-24 23:25:16 +0200804 nvec = devm_kzalloc(&pdev->dev, sizeof(struct nvec_chip), GFP_KERNEL);
Somya Anand4c42d972015-03-16 19:34:11 +0530805 if (!nvec)
Marc Dietrich32890b92011-05-19 16:34:42 +0200806 return -ENOMEM;
Pawel Lebioda5a9077a2014-07-05 22:30:56 +0200807
Marc Dietrich32890b92011-05-19 16:34:42 +0200808 platform_set_drvdata(pdev, nvec);
809 nvec->dev = &pdev->dev;
Marc Dietrich7990b0d2011-11-01 21:37:04 +0100810
Marc Dietrich95cd1862013-06-21 10:28:55 +0200811 err = nvec_i2c_parse_dt_pdata(nvec);
812 if (err < 0)
813 return err;
Marc Dietrich32890b92011-05-19 16:34:42 +0200814
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200815 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding97f4be602013-01-21 11:09:19 +0100816 base = devm_ioremap_resource(&pdev->dev, res);
817 if (IS_ERR(base))
818 return PTR_ERR(base);
Marc Dietrich32890b92011-05-19 16:34:42 +0200819
Thierry Redingb5b628b2014-07-29 13:16:15 +0200820 nvec->irq = platform_get_irq(pdev, 0);
821 if (nvec->irq < 0) {
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200822 dev_err(&pdev->dev, "no irq resource?\n");
Marc Dietrichf5e33522012-06-24 23:25:16 +0200823 return -ENODEV;
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200824 }
825
Marc Dietrichc2b62f62013-04-29 23:14:52 +0200826 i2c_clk = devm_clk_get(&pdev->dev, "div-clk");
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200827 if (IS_ERR(i2c_clk)) {
828 dev_err(nvec->dev, "failed to get controller clock\n");
Marc Dietrichf5e33522012-06-24 23:25:16 +0200829 return -ENODEV;
Marc Dietrich32890b92011-05-19 16:34:42 +0200830 }
831
Stephen Warrenc0df5bf2013-11-06 16:46:24 -0700832 nvec->rst = devm_reset_control_get(&pdev->dev, "i2c");
833 if (IS_ERR(nvec->rst)) {
834 dev_err(nvec->dev, "failed to get controller reset\n");
835 return PTR_ERR(nvec->rst);
836 }
837
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200838 nvec->base = base;
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200839 nvec->i2c_clk = i2c_clk;
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200840 nvec->rx = &nvec->msg_pool[0];
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200841
Marc Dietrich32890b92011-05-19 16:34:42 +0200842 ATOMIC_INIT_NOTIFIER_HEAD(&nvec->notifier_list);
843
844 init_completion(&nvec->sync_write);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200845 init_completion(&nvec->ec_transfer);
846 mutex_init(&nvec->sync_write_mutex);
847 spin_lock_init(&nvec->tx_lock);
848 spin_lock_init(&nvec->rx_lock);
Marc Dietrich32890b92011-05-19 16:34:42 +0200849 INIT_LIST_HEAD(&nvec->rx_data);
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200850 INIT_LIST_HEAD(&nvec->tx_data);
Marc Dietrich32890b92011-05-19 16:34:42 +0200851 INIT_WORK(&nvec->rx_work, nvec_dispatch);
852 INIT_WORK(&nvec->tx_work, nvec_request_master);
853
Marc Dietrichf5e33522012-06-24 23:25:16 +0200854 err = devm_gpio_request_one(&pdev->dev, nvec->gpio, GPIOF_OUT_INIT_HIGH,
855 "nvec gpio");
Julian Andres Klodeaed92bb2011-12-26 17:57:39 +0100856 if (err < 0) {
857 dev_err(nvec->dev, "couldn't request gpio\n");
Marc Dietrichf5e33522012-06-24 23:25:16 +0200858 return -ENODEV;
Julian Andres Klodeaed92bb2011-12-26 17:57:39 +0100859 }
860
Marc Dietrichf5e33522012-06-24 23:25:16 +0200861 err = devm_request_irq(&pdev->dev, nvec->irq, nvec_interrupt, 0,
862 "nvec", nvec);
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200863 if (err) {
864 dev_err(nvec->dev, "couldn't request irq\n");
Marc Dietrichf5e33522012-06-24 23:25:16 +0200865 return -ENODEV;
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200866 }
Marc Dietrichac810752011-09-27 19:00:42 +0200867 disable_irq(nvec->irq);
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200868
869 tegra_init_i2c_slave(nvec);
870
Marc Dietrich32890b92011-05-19 16:34:42 +0200871 /* enable event reporting */
Marc Dietrich518945f2013-01-27 17:43:41 +0100872 nvec_toggle_global_events(nvec, true);
Marc Dietrich32890b92011-05-19 16:34:42 +0200873
Marc Dietrich32890b92011-05-19 16:34:42 +0200874 nvec->nvec_status_notifier.notifier_call = nvec_status_notifier;
875 nvec_register_notifier(nvec, &nvec->nvec_status_notifier, 0);
876
877 nvec_power_handle = nvec;
878 pm_power_off = nvec_power_off;
879
880 /* Get Firmware Version */
Marc Dietrich93eff832013-01-27 17:43:43 +0100881 msg = nvec_write_sync(nvec, get_firmware_version, 2);
Marc Dietrich32890b92011-05-19 16:34:42 +0200882
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200883 if (msg) {
884 dev_warn(nvec->dev, "ec firmware version %02x.%02x.%02x / %02x\n",
885 msg->data[4], msg->data[5], msg->data[6], msg->data[7]);
Marc Dietrich32890b92011-05-19 16:34:42 +0200886
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +0200887 nvec_msg_free(nvec, msg);
888 }
Marc Dietrich32890b92011-05-19 16:34:42 +0200889
Marc Dietrich3ec69882015-01-20 23:01:28 +0100890 ret = mfd_add_devices(nvec->dev, 0, nvec_devices,
Thierry Reding47e7b052014-07-29 13:16:14 +0200891 ARRAY_SIZE(nvec_devices), NULL, 0, NULL);
Marc Dietrich162c7d82011-09-27 19:00:40 +0200892 if (ret)
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200893 dev_err(nvec->dev, "error adding subdevices\n");
894
Marc Dietrich32890b92011-05-19 16:34:42 +0200895 /* unmute speakers? */
Marc Dietrich93eff832013-01-27 17:43:43 +0100896 nvec_write_async(nvec, unmute_speakers, 4);
Marc Dietrich32890b92011-05-19 16:34:42 +0200897
898 /* enable lid switch event */
Marc Dietrich93eff832013-01-27 17:43:43 +0100899 nvec_event_mask(enable_event, LID_SWITCH);
900 nvec_write_async(nvec, enable_event, 7);
Marc Dietrich32890b92011-05-19 16:34:42 +0200901
902 /* enable power button event */
Marc Dietrich93eff832013-01-27 17:43:43 +0100903 nvec_event_mask(enable_event, PWR_BUTTON);
904 nvec_write_async(nvec, enable_event, 7);
Marc Dietrich32890b92011-05-19 16:34:42 +0200905
906 return 0;
Marc Dietrich32890b92011-05-19 16:34:42 +0200907}
908
Bill Pemberton1a6a8a82012-11-19 13:26:41 -0500909static int tegra_nvec_remove(struct platform_device *pdev)
Marc Dietrich32890b92011-05-19 16:34:42 +0200910{
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200911 struct nvec_chip *nvec = platform_get_drvdata(pdev);
912
Marc Dietrich518945f2013-01-27 17:43:41 +0100913 nvec_toggle_global_events(nvec, false);
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200914 mfd_remove_devices(nvec->dev);
Marc Dietrichc2b62f62013-04-29 23:14:52 +0200915 nvec_unregister_notifier(nvec, &nvec->nvec_status_notifier);
Tejun Heoeb1e40a2012-08-22 15:32:38 -0700916 cancel_work_sync(&nvec->rx_work);
917 cancel_work_sync(&nvec->tx_work);
Shailendra Vermafd90ae22015-05-27 07:00:54 +0530918 /* FIXME: needs check whether nvec is responsible for power off */
Marc Dietrichc2b62f62013-04-29 23:14:52 +0200919 pm_power_off = NULL;
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200920
Marc Dietrich32890b92011-05-19 16:34:42 +0200921 return 0;
922}
923
Marc Dietrichebefae22012-06-24 23:25:19 +0200924#ifdef CONFIG_PM_SLEEP
925static int nvec_suspend(struct device *dev)
Marc Dietrich32890b92011-05-19 16:34:42 +0200926{
Marc Dietrichebefae22012-06-24 23:25:19 +0200927 struct platform_device *pdev = to_platform_device(dev);
Marc Dietrich32890b92011-05-19 16:34:42 +0200928 struct nvec_chip *nvec = platform_get_drvdata(pdev);
Marc Dietrich9feeb012011-09-27 19:01:08 +0200929 struct nvec_msg *msg;
Marc Dietrich93eff832013-01-27 17:43:43 +0100930 char ap_suspend[] = { NVEC_SLEEP, AP_SUSPEND };
Marc Dietrich32890b92011-05-19 16:34:42 +0200931
932 dev_dbg(nvec->dev, "suspending\n");
Marc Dietrich9feeb012011-09-27 19:01:08 +0200933
934 /* keep these sync or you'll break suspend */
Marc Dietrich93eff832013-01-27 17:43:43 +0100935 nvec_toggle_global_events(nvec, false);
936
937 msg = nvec_write_sync(nvec, ap_suspend, sizeof(ap_suspend));
Marc Dietrich9feeb012011-09-27 19:01:08 +0200938 nvec_msg_free(nvec, msg);
939
Marc Dietrichac810752011-09-27 19:00:42 +0200940 nvec_disable_i2c_slave(nvec);
Marc Dietrich32890b92011-05-19 16:34:42 +0200941
942 return 0;
943}
944
Marc Dietrichebefae22012-06-24 23:25:19 +0200945static int nvec_resume(struct device *dev)
Marc Dietrich162c7d82011-09-27 19:00:40 +0200946{
Marc Dietrichebefae22012-06-24 23:25:19 +0200947 struct platform_device *pdev = to_platform_device(dev);
Marc Dietrich32890b92011-05-19 16:34:42 +0200948 struct nvec_chip *nvec = platform_get_drvdata(pdev);
949
950 dev_dbg(nvec->dev, "resuming\n");
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200951 tegra_init_i2c_slave(nvec);
Marc Dietrich518945f2013-01-27 17:43:41 +0100952 nvec_toggle_global_events(nvec, true);
Marc Dietrich32890b92011-05-19 16:34:42 +0200953
954 return 0;
955}
Marc Dietrich32890b92011-05-19 16:34:42 +0200956#endif
957
Thierry Reding5d305662014-07-21 13:54:41 +0200958static SIMPLE_DEV_PM_OPS(nvec_pm_ops, nvec_suspend, nvec_resume);
Marc Dietrichebefae22012-06-24 23:25:19 +0200959
Marc Dietrich7990b0d2011-11-01 21:37:04 +0100960/* Match table for of_platform binding */
Bill Pemberton063f9f62012-11-19 13:25:26 -0500961static const struct of_device_id nvidia_nvec_of_match[] = {
Marc Dietrich7990b0d2011-11-01 21:37:04 +0100962 { .compatible = "nvidia,nvec", },
963 {},
964};
965MODULE_DEVICE_TABLE(of, nvidia_nvec_of_match);
966
Marc Dietrich162c7d82011-09-27 19:00:40 +0200967static struct platform_driver nvec_device_driver = {
968 .probe = tegra_nvec_probe,
Bill Pemberton44b90a32012-11-19 13:20:55 -0500969 .remove = tegra_nvec_remove,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200970 .driver = {
Marc Dietrich32890b92011-05-19 16:34:42 +0200971 .name = "nvec",
Marc Dietrichebefae22012-06-24 23:25:19 +0200972 .pm = &nvec_pm_ops,
Marc Dietrich7990b0d2011-11-01 21:37:04 +0100973 .of_match_table = nvidia_nvec_of_match,
Marc Dietrich32890b92011-05-19 16:34:42 +0200974 }
975};
976
Marc Dietrich9891b1c2012-06-24 23:25:18 +0200977module_platform_driver(nvec_device_driver);
Marc Dietrich162c7d82011-09-27 19:00:40 +0200978
Marc Dietrich32890b92011-05-19 16:34:42 +0200979MODULE_ALIAS("platform:nvec");
Marc Dietrich162c7d82011-09-27 19:00:40 +0200980MODULE_DESCRIPTION("NVIDIA compliant embedded controller interface");
981MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
982MODULE_LICENSE("GPL");