nvec.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVEC: NVIDIA compliant embedded controller interface
  4. *
  5. * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
  6. *
  7. * Authors: Pierre-Hugues Husson <phhusson@free.fr>
  8. * Ilya Petrov <ilya.muromec@gmail.com>
  9. * Marc Dietrich <marvin24@gmx.de>
  10. * Julian Andres Klode <jak@jak-linux.org>
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/atomic.h>
  15. #include <linux/clk.h>
  16. #include <linux/completion.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/irq.h>
  23. #include <linux/of.h>
  24. #include <linux/list.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/mutex.h>
  27. #include <linux/notifier.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/workqueue.h>
  31. #include "nvec.h"
  32. #define I2C_CNFG 0x00
  33. #define I2C_CNFG_PACKET_MODE_EN BIT(10)
  34. #define I2C_CNFG_NEW_MASTER_SFM BIT(11)
  35. #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
  36. #define I2C_SL_CNFG 0x20
  37. #define I2C_SL_NEWSL BIT(2)
  38. #define I2C_SL_NACK BIT(1)
  39. #define I2C_SL_RESP BIT(0)
  40. #define I2C_SL_IRQ BIT(3)
  41. #define END_TRANS BIT(4)
  42. #define RCVD BIT(2)
  43. #define RNW BIT(1)
  44. #define I2C_SL_RCVD 0x24
  45. #define I2C_SL_STATUS 0x28
  46. #define I2C_SL_ADDR1 0x2c
  47. #define I2C_SL_ADDR2 0x30
  48. #define I2C_SL_DELAY_COUNT 0x3c
  49. /**
  50. * enum nvec_msg_category - Message categories for nvec_msg_alloc()
  51. * @NVEC_MSG_RX: The message is an incoming message (from EC)
  52. * @NVEC_MSG_TX: The message is an outgoing message (to EC)
  53. */
  54. enum nvec_msg_category {
  55. NVEC_MSG_RX,
  56. NVEC_MSG_TX,
  57. };
  58. enum nvec_sleep_subcmds {
  59. GLOBAL_EVENTS,
  60. AP_PWR_DOWN,
  61. AP_SUSPEND,
  62. };
  63. #define CNF_EVENT_REPORTING 0x01
  64. #define GET_FIRMWARE_VERSION 0x15
  65. #define LID_SWITCH BIT(1)
  66. #define PWR_BUTTON BIT(15)
  67. static struct nvec_chip *nvec_power_handle;
  68. static const struct mfd_cell nvec_devices[] = {
  69. {
  70. .name = "nvec-kbd",
  71. },
  72. {
  73. .name = "nvec-mouse",
  74. },
  75. {
  76. .name = "nvec-power",
  77. .id = 0,
  78. },
  79. {
  80. .name = "nvec-power",
  81. .id = 1,
  82. },
  83. {
  84. .name = "nvec-paz00",
  85. },
  86. };
  87. /**
  88. * nvec_register_notifier - Register a notifier with nvec
  89. * @nvec: A &struct nvec_chip
  90. * @nb: The notifier block to register
  91. *
  92. * Registers a notifier with @nvec. The notifier will be added to an atomic
  93. * notifier chain that is called for all received messages except those that
  94. * correspond to a request initiated by nvec_write_sync().
  95. */
  96. int nvec_register_notifier(struct nvec_chip *nvec, struct notifier_block *nb,
  97. unsigned int events)
  98. {
  99. return atomic_notifier_chain_register(&nvec->notifier_list, nb);
  100. }
  101. EXPORT_SYMBOL_GPL(nvec_register_notifier);
  102. /**
  103. * nvec_unregister_notifier - Unregister a notifier with nvec
  104. * @nvec: A &struct nvec_chip
  105. * @nb: The notifier block to unregister
  106. *
  107. * Unregisters a notifier with @nvec. The notifier will be removed from the
  108. * atomic notifier chain.
  109. */
  110. int nvec_unregister_notifier(struct nvec_chip *nvec, struct notifier_block *nb)
  111. {
  112. return atomic_notifier_chain_unregister(&nvec->notifier_list, nb);
  113. }
  114. EXPORT_SYMBOL_GPL(nvec_unregister_notifier);
  115. /**
  116. * nvec_status_notifier - The final notifier
  117. *
  118. * Prints a message about control events not handled in the notifier
  119. * chain.
  120. */
  121. static int nvec_status_notifier(struct notifier_block *nb,
  122. unsigned long event_type, void *data)
  123. {
  124. struct nvec_chip *nvec = container_of(nb, struct nvec_chip,
  125. nvec_status_notifier);
  126. unsigned char *msg = data;
  127. if (event_type != NVEC_CNTL)
  128. return NOTIFY_DONE;
  129. dev_warn(nvec->dev, "unhandled msg type %ld\n", event_type);
  130. print_hex_dump(KERN_WARNING, "payload: ", DUMP_PREFIX_NONE, 16, 1,
  131. msg, msg[1] + 2, true);
  132. return NOTIFY_OK;
  133. }
  134. /**
  135. * nvec_msg_alloc:
  136. * @nvec: A &struct nvec_chip
  137. * @category: Pool category, see &enum nvec_msg_category
  138. *
  139. * Allocate a single &struct nvec_msg object from the message pool of
  140. * @nvec. The result shall be passed to nvec_msg_free() if no longer
  141. * used.
  142. *
  143. * Outgoing messages are placed in the upper 75% of the pool, keeping the
  144. * lower 25% available for RX buffers only. The reason is to prevent a
  145. * situation where all buffers are full and a message is thus endlessly
  146. * retried because the response could never be processed.
  147. */
  148. static struct nvec_msg *nvec_msg_alloc(struct nvec_chip *nvec,
  149. enum nvec_msg_category category)
  150. {
  151. int i = (category == NVEC_MSG_TX) ? (NVEC_POOL_SIZE / 4) : 0;
  152. for (; i < NVEC_POOL_SIZE; i++) {
  153. if (atomic_xchg(&nvec->msg_pool[i].used, 1) == 0) {
  154. dev_vdbg(nvec->dev, "INFO: Allocate %i\n", i);
  155. return &nvec->msg_pool[i];
  156. }
  157. }
  158. dev_err(nvec->dev, "could not allocate %s buffer\n",
  159. (category == NVEC_MSG_TX) ? "TX" : "RX");
  160. return NULL;
  161. }
  162. /**
  163. * nvec_msg_free:
  164. * @nvec: A &struct nvec_chip
  165. * @msg: A message (must be allocated by nvec_msg_alloc() and belong to @nvec)
  166. *
  167. * Free the given message
  168. */
  169. void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg)
  170. {
  171. if (msg != &nvec->tx_scratch)
  172. dev_vdbg(nvec->dev, "INFO: Free %ti\n", msg - nvec->msg_pool);
  173. atomic_set(&msg->used, 0);
  174. }
  175. EXPORT_SYMBOL_GPL(nvec_msg_free);
  176. /**
  177. * nvec_msg_is_event - Return %true if @msg is an event
  178. * @msg: A message
  179. */
  180. static bool nvec_msg_is_event(struct nvec_msg *msg)
  181. {
  182. return msg->data[0] >> 7;
  183. }
  184. /**
  185. * nvec_msg_size - Get the size of a message
  186. * @msg: The message to get the size for
  187. *
  188. * This only works for received messages, not for outgoing messages.
  189. */
  190. static size_t nvec_msg_size(struct nvec_msg *msg)
  191. {
  192. bool is_event = nvec_msg_is_event(msg);
  193. int event_length = (msg->data[0] & 0x60) >> 5;
  194. /* for variable size, payload size in byte 1 + count (1) + cmd (1) */
  195. if (!is_event || event_length == NVEC_VAR_SIZE)
  196. return (msg->pos || msg->size) ? (msg->data[1] + 2) : 0;
  197. else if (event_length == NVEC_2BYTES)
  198. return 2;
  199. else if (event_length == NVEC_3BYTES)
  200. return 3;
  201. return 0;
  202. }
  203. /**
  204. * nvec_gpio_set_value - Set the GPIO value
  205. * @nvec: A &struct nvec_chip
  206. * @value: The value to write (0 or 1)
  207. *
  208. * Like gpio_set_value(), but generating debugging information
  209. */
  210. static void nvec_gpio_set_value(struct nvec_chip *nvec, int value)
  211. {
  212. dev_dbg(nvec->dev, "GPIO changed from %u to %u\n",
  213. gpiod_get_value(nvec->gpiod), value);
  214. gpiod_set_value(nvec->gpiod, value);
  215. }
  216. /**
  217. * nvec_write_async - Asynchronously write a message to NVEC
  218. * @nvec: An nvec_chip instance
  219. * @data: The message data, starting with the request type
  220. * @size: The size of @data
  221. *
  222. * Queue a single message to be transferred to the embedded controller
  223. * and return immediately.
  224. *
  225. * Returns: 0 on success, a negative error code on failure. If a failure
  226. * occurred, the nvec driver may print an error.
  227. */
  228. int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
  229. short size)
  230. {
  231. struct nvec_msg *msg;
  232. unsigned long flags;
  233. msg = nvec_msg_alloc(nvec, NVEC_MSG_TX);
  234. if (!msg)
  235. return -ENOMEM;
  236. msg->data[0] = size;
  237. memcpy(msg->data + 1, data, size);
  238. msg->size = size + 1;
  239. spin_lock_irqsave(&nvec->tx_lock, flags);
  240. list_add_tail(&msg->node, &nvec->tx_data);
  241. spin_unlock_irqrestore(&nvec->tx_lock, flags);
  242. schedule_work(&nvec->tx_work);
  243. return 0;
  244. }
  245. EXPORT_SYMBOL(nvec_write_async);
  246. /**
  247. * nvec_write_sync - Write a message to nvec and read the response
  248. * @nvec: An &struct nvec_chip
  249. * @data: The data to write
  250. * @size: The size of @data
  251. * @msg: The response message received
  252. *
  253. * This is similar to nvec_write_async(), but waits for the
  254. * request to be answered before returning. This function
  255. * uses a mutex and can thus not be called from e.g.
  256. * interrupt handlers.
  257. *
  258. * Returns: 0 on success, a negative error code on failure.
  259. * The response message is returned in @msg. Shall be freed with
  260. * with nvec_msg_free() once no longer used.
  261. *
  262. */
  263. int nvec_write_sync(struct nvec_chip *nvec,
  264. const unsigned char *data, short size,
  265. struct nvec_msg **msg)
  266. {
  267. mutex_lock(&nvec->sync_write_mutex);
  268. *msg = NULL;
  269. nvec->sync_write_pending = (data[1] << 8) + data[0];
  270. if (nvec_write_async(nvec, data, size) < 0) {
  271. mutex_unlock(&nvec->sync_write_mutex);
  272. return -ENOMEM;
  273. }
  274. dev_dbg(nvec->dev, "nvec_sync_write: 0x%04x\n",
  275. nvec->sync_write_pending);
  276. if (!(wait_for_completion_timeout(&nvec->sync_write,
  277. msecs_to_jiffies(2000)))) {
  278. dev_warn(nvec->dev,
  279. "timeout waiting for sync write to complete\n");
  280. mutex_unlock(&nvec->sync_write_mutex);
  281. return -ETIMEDOUT;
  282. }
  283. dev_dbg(nvec->dev, "nvec_sync_write: pong!\n");
  284. *msg = nvec->last_sync_msg;
  285. mutex_unlock(&nvec->sync_write_mutex);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL(nvec_write_sync);
  289. /**
  290. * nvec_toggle_global_events - enables or disables global event reporting
  291. * @nvec: nvec handle
  292. * @state: true for enable, false for disable
  293. *
  294. * This switches on/off global event reports by the embedded controller.
  295. */
  296. static void nvec_toggle_global_events(struct nvec_chip *nvec, bool state)
  297. {
  298. unsigned char global_events[] = { NVEC_SLEEP, GLOBAL_EVENTS, state };
  299. nvec_write_async(nvec, global_events, 3);
  300. }
  301. /**
  302. * nvec_event_mask - fill the command string with event bitfield
  303. * ev: points to event command string
  304. * mask: bit to insert into the event mask
  305. *
  306. * Configure event command expects a 32 bit bitfield which describes
  307. * which events to enable. The bitfield has the following structure
  308. * (from highest byte to lowest):
  309. * system state bits 7-0
  310. * system state bits 15-8
  311. * oem system state bits 7-0
  312. * oem system state bits 15-8
  313. */
  314. static void nvec_event_mask(char *ev, u32 mask)
  315. {
  316. ev[3] = mask >> 16 & 0xff;
  317. ev[4] = mask >> 24 & 0xff;
  318. ev[5] = mask >> 0 & 0xff;
  319. ev[6] = mask >> 8 & 0xff;
  320. }
  321. /**
  322. * nvec_request_master - Process outgoing messages
  323. * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
  324. *
  325. * Processes all outgoing requests by sending the request and awaiting the
  326. * response, then continuing with the next request. Once a request has a
  327. * matching response, it will be freed and removed from the list.
  328. */
  329. static void nvec_request_master(struct work_struct *work)
  330. {
  331. struct nvec_chip *nvec = container_of(work, struct nvec_chip, tx_work);
  332. unsigned long flags;
  333. long err;
  334. struct nvec_msg *msg;
  335. spin_lock_irqsave(&nvec->tx_lock, flags);
  336. while (!list_empty(&nvec->tx_data)) {
  337. msg = list_first_entry(&nvec->tx_data, struct nvec_msg, node);
  338. spin_unlock_irqrestore(&nvec->tx_lock, flags);
  339. nvec_gpio_set_value(nvec, 0);
  340. err = wait_for_completion_interruptible_timeout(
  341. &nvec->ec_transfer, msecs_to_jiffies(5000));
  342. if (err == 0) {
  343. dev_warn(nvec->dev, "timeout waiting for ec transfer\n");
  344. nvec_gpio_set_value(nvec, 1);
  345. msg->pos = 0;
  346. }
  347. spin_lock_irqsave(&nvec->tx_lock, flags);
  348. if (err > 0) {
  349. list_del_init(&msg->node);
  350. nvec_msg_free(nvec, msg);
  351. }
  352. }
  353. spin_unlock_irqrestore(&nvec->tx_lock, flags);
  354. }
  355. /**
  356. * parse_msg - Print some information and call the notifiers on an RX message
  357. * @nvec: A &struct nvec_chip
  358. * @msg: A message received by @nvec
  359. *
  360. * Paarse some pieces of the message and then call the chain of notifiers
  361. * registered via nvec_register_notifier.
  362. */
  363. static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg)
  364. {
  365. if ((msg->data[0] & 1 << 7) == 0 && msg->data[3]) {
  366. dev_err(nvec->dev, "ec responded %*ph\n", 4, msg->data);
  367. return -EINVAL;
  368. }
  369. if ((msg->data[0] >> 7) == 1 && (msg->data[0] & 0x0f) == 5)
  370. print_hex_dump(KERN_WARNING, "ec system event ",
  371. DUMP_PREFIX_NONE, 16, 1, msg->data,
  372. msg->data[1] + 2, true);
  373. atomic_notifier_call_chain(&nvec->notifier_list, msg->data[0] & 0x8f,
  374. msg->data);
  375. return 0;
  376. }
  377. /**
  378. * nvec_dispatch - Process messages received from the EC
  379. * @work: A &struct work_struct (the tx_worker member of &struct nvec_chip)
  380. *
  381. * Process messages previously received from the EC and put into the RX
  382. * queue of the &struct nvec_chip instance associated with @work.
  383. */
  384. static void nvec_dispatch(struct work_struct *work)
  385. {
  386. struct nvec_chip *nvec = container_of(work, struct nvec_chip, rx_work);
  387. unsigned long flags;
  388. struct nvec_msg *msg;
  389. spin_lock_irqsave(&nvec->rx_lock, flags);
  390. while (!list_empty(&nvec->rx_data)) {
  391. msg = list_first_entry(&nvec->rx_data, struct nvec_msg, node);
  392. list_del_init(&msg->node);
  393. spin_unlock_irqrestore(&nvec->rx_lock, flags);
  394. if (nvec->sync_write_pending ==
  395. (msg->data[2] << 8) + msg->data[0]) {
  396. dev_dbg(nvec->dev, "sync write completed!\n");
  397. nvec->sync_write_pending = 0;
  398. nvec->last_sync_msg = msg;
  399. complete(&nvec->sync_write);
  400. } else {
  401. parse_msg(nvec, msg);
  402. nvec_msg_free(nvec, msg);
  403. }
  404. spin_lock_irqsave(&nvec->rx_lock, flags);
  405. }
  406. spin_unlock_irqrestore(&nvec->rx_lock, flags);
  407. }
  408. /**
  409. * nvec_tx_completed - Complete the current transfer
  410. * @nvec: A &struct nvec_chip
  411. *
  412. * This is called when we have received an END_TRANS on a TX transfer.
  413. */
  414. static void nvec_tx_completed(struct nvec_chip *nvec)
  415. {
  416. /* We got an END_TRANS, let's skip this, maybe there's an event */
  417. if (nvec->tx->pos != nvec->tx->size) {
  418. dev_err(nvec->dev, "premature END_TRANS, resending\n");
  419. nvec->tx->pos = 0;
  420. nvec_gpio_set_value(nvec, 0);
  421. } else {
  422. nvec->state = 0;
  423. }
  424. }
  425. /**
  426. * nvec_rx_completed - Complete the current transfer
  427. * @nvec: A &struct nvec_chip
  428. *
  429. * This is called when we have received an END_TRANS on a RX transfer.
  430. */
  431. static void nvec_rx_completed(struct nvec_chip *nvec)
  432. {
  433. if (nvec->rx->pos != nvec_msg_size(nvec->rx)) {
  434. dev_err(nvec->dev, "RX incomplete: Expected %u bytes, got %u\n",
  435. (uint)nvec_msg_size(nvec->rx),
  436. (uint)nvec->rx->pos);
  437. nvec_msg_free(nvec, nvec->rx);
  438. nvec->state = 0;
  439. /* Battery quirk - Often incomplete, and likes to crash */
  440. if (nvec->rx->data[0] == NVEC_BAT)
  441. complete(&nvec->ec_transfer);
  442. return;
  443. }
  444. spin_lock(&nvec->rx_lock);
  445. /*
  446. * Add the received data to the work list and move the ring buffer
  447. * pointer to the next entry.
  448. */
  449. list_add_tail(&nvec->rx->node, &nvec->rx_data);
  450. spin_unlock(&nvec->rx_lock);
  451. nvec->state = 0;
  452. if (!nvec_msg_is_event(nvec->rx))
  453. complete(&nvec->ec_transfer);
  454. schedule_work(&nvec->rx_work);
  455. }
  456. /**
  457. * nvec_invalid_flags - Send an error message about invalid flags and jump
  458. * @nvec: The nvec device
  459. * @status: The status flags
  460. * @reset: Whether we shall jump to state 0.
  461. */
  462. static void nvec_invalid_flags(struct nvec_chip *nvec, unsigned int status,
  463. bool reset)
  464. {
  465. dev_err(nvec->dev, "unexpected status flags 0x%02x during state %i\n",
  466. status, nvec->state);
  467. if (reset)
  468. nvec->state = 0;
  469. }
  470. /**
  471. * nvec_tx_set - Set the message to transfer (nvec->tx)
  472. * @nvec: A &struct nvec_chip
  473. *
  474. * Gets the first entry from the tx_data list of @nvec and sets the
  475. * tx member to it. If the tx_data list is empty, this uses the
  476. * tx_scratch message to send a no operation message.
  477. */
  478. static void nvec_tx_set(struct nvec_chip *nvec)
  479. {
  480. spin_lock(&nvec->tx_lock);
  481. if (list_empty(&nvec->tx_data)) {
  482. dev_err(nvec->dev, "empty tx - sending no-op\n");
  483. memcpy(nvec->tx_scratch.data, "\x02\x07\x02", 3);
  484. nvec->tx_scratch.size = 3;
  485. nvec->tx_scratch.pos = 0;
  486. nvec->tx = &nvec->tx_scratch;
  487. list_add_tail(&nvec->tx->node, &nvec->tx_data);
  488. } else {
  489. nvec->tx = list_first_entry(&nvec->tx_data, struct nvec_msg,
  490. node);
  491. nvec->tx->pos = 0;
  492. }
  493. spin_unlock(&nvec->tx_lock);
  494. dev_dbg(nvec->dev, "Sending message of length %u, command 0x%x\n",
  495. (uint)nvec->tx->size, nvec->tx->data[1]);
  496. }
  497. /**
  498. * nvec_interrupt - Interrupt handler
  499. * @irq: The IRQ
  500. * @dev: The nvec device
  501. *
  502. * Interrupt handler that fills our RX buffers and empties our TX
  503. * buffers. This uses a finite state machine with ridiculous amounts
  504. * of error checking, in order to be fairly reliable.
  505. */
  506. static irqreturn_t nvec_interrupt(int irq, void *dev)
  507. {
  508. unsigned long status;
  509. unsigned int received = 0;
  510. unsigned char to_send = 0xff;
  511. const unsigned long irq_mask = I2C_SL_IRQ | END_TRANS | RCVD | RNW;
  512. struct nvec_chip *nvec = dev;
  513. unsigned int state = nvec->state;
  514. status = readl(nvec->base + I2C_SL_STATUS);
  515. /* Filter out some errors */
  516. if ((status & irq_mask) == 0 && (status & ~irq_mask) != 0) {
  517. dev_err(nvec->dev, "unexpected irq mask %lx\n", status);
  518. return IRQ_HANDLED;
  519. }
  520. if ((status & I2C_SL_IRQ) == 0) {
  521. dev_err(nvec->dev, "Spurious IRQ\n");
  522. return IRQ_HANDLED;
  523. }
  524. /* The EC did not request a read, so it send us something, read it */
  525. if ((status & RNW) == 0) {
  526. received = readl(nvec->base + I2C_SL_RCVD);
  527. if (status & RCVD)
  528. writel(0, nvec->base + I2C_SL_RCVD);
  529. }
  530. if (status == (I2C_SL_IRQ | RCVD))
  531. nvec->state = 0;
  532. switch (nvec->state) {
  533. case 0: /* Verify that its a transfer start, the rest later */
  534. if (status != (I2C_SL_IRQ | RCVD))
  535. nvec_invalid_flags(nvec, status, false);
  536. break;
  537. case 1: /* command byte */
  538. if (status != I2C_SL_IRQ) {
  539. nvec_invalid_flags(nvec, status, true);
  540. } else {
  541. nvec->rx = nvec_msg_alloc(nvec, NVEC_MSG_RX);
  542. /* Should not happen in a normal world */
  543. if (unlikely(!nvec->rx)) {
  544. nvec->state = 0;
  545. break;
  546. }
  547. nvec->rx->data[0] = received;
  548. nvec->rx->pos = 1;
  549. nvec->state = 2;
  550. }
  551. break;
  552. case 2: /* first byte after command */
  553. if (status == (I2C_SL_IRQ | RNW | RCVD)) {
  554. udelay(33);
  555. if (nvec->rx->data[0] != 0x01) {
  556. dev_err(nvec->dev,
  557. "Read without prior read command\n");
  558. nvec->state = 0;
  559. break;
  560. }
  561. nvec_msg_free(nvec, nvec->rx);
  562. nvec->state = 3;
  563. nvec_tx_set(nvec);
  564. to_send = nvec->tx->data[0];
  565. nvec->tx->pos = 1;
  566. } else if (status == (I2C_SL_IRQ)) {
  567. nvec->rx->data[1] = received;
  568. nvec->rx->pos = 2;
  569. nvec->state = 4;
  570. } else {
  571. nvec_invalid_flags(nvec, status, true);
  572. }
  573. break;
  574. case 3: /* EC does a block read, we transmit data */
  575. if (status & END_TRANS) {
  576. nvec_tx_completed(nvec);
  577. } else if ((status & RNW) == 0 || (status & RCVD)) {
  578. nvec_invalid_flags(nvec, status, true);
  579. } else if (nvec->tx && nvec->tx->pos < nvec->tx->size) {
  580. to_send = nvec->tx->data[nvec->tx->pos++];
  581. } else {
  582. dev_err(nvec->dev,
  583. "tx buffer underflow on %p (%u > %u)\n",
  584. nvec->tx,
  585. (uint)(nvec->tx ? nvec->tx->pos : 0),
  586. (uint)(nvec->tx ? nvec->tx->size : 0));
  587. nvec->state = 0;
  588. }
  589. break;
  590. case 4: /* EC does some write, we read the data */
  591. if ((status & (END_TRANS | RNW)) == END_TRANS)
  592. nvec_rx_completed(nvec);
  593. else if (status & (RNW | RCVD))
  594. nvec_invalid_flags(nvec, status, true);
  595. else if (nvec->rx && nvec->rx->pos < NVEC_MSG_SIZE)
  596. nvec->rx->data[nvec->rx->pos++] = received;
  597. else
  598. dev_err(nvec->dev,
  599. "RX buffer overflow on %p: Trying to write byte %u of %u\n",
  600. nvec->rx, nvec->rx ? nvec->rx->pos : 0,
  601. NVEC_MSG_SIZE);
  602. break;
  603. default:
  604. nvec->state = 0;
  605. }
  606. /* If we are told that a new transfer starts, verify it */
  607. if ((status & (RCVD | RNW)) == RCVD) {
  608. if (received != nvec->i2c_addr)
  609. dev_err(nvec->dev,
  610. "received address 0x%02x, expected 0x%02x\n",
  611. received, nvec->i2c_addr);
  612. nvec->state = 1;
  613. }
  614. /* Send data if requested, but not on end of transmission */
  615. if ((status & (RNW | END_TRANS)) == RNW)
  616. writel(to_send, nvec->base + I2C_SL_RCVD);
  617. /* If we have send the first byte */
  618. if (status == (I2C_SL_IRQ | RNW | RCVD))
  619. nvec_gpio_set_value(nvec, 1);
  620. dev_dbg(nvec->dev,
  621. "Handled: %s 0x%02x, %s 0x%02x in state %u [%s%s%s]\n",
  622. (status & RNW) == 0 ? "received" : "R=",
  623. received,
  624. (status & (RNW | END_TRANS)) ? "sent" : "S=",
  625. to_send,
  626. state,
  627. status & END_TRANS ? " END_TRANS" : "",
  628. status & RCVD ? " RCVD" : "",
  629. status & RNW ? " RNW" : "");
  630. /*
  631. * TODO: A correct fix needs to be found for this.
  632. *
  633. * We experience less incomplete messages with this delay than without
  634. * it, but we don't know why. Help is appreciated.
  635. */
  636. udelay(100);
  637. return IRQ_HANDLED;
  638. }
  639. static void tegra_init_i2c_slave(struct nvec_chip *nvec)
  640. {
  641. u32 val;
  642. clk_prepare_enable(nvec->i2c_clk);
  643. reset_control_assert(nvec->rst);
  644. udelay(2);
  645. reset_control_deassert(nvec->rst);
  646. val = I2C_CNFG_NEW_MASTER_SFM | I2C_CNFG_PACKET_MODE_EN |
  647. (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
  648. writel(val, nvec->base + I2C_CNFG);
  649. clk_set_rate(nvec->i2c_clk, 8 * 80000);
  650. writel(I2C_SL_NEWSL, nvec->base + I2C_SL_CNFG);
  651. writel(0x1E, nvec->base + I2C_SL_DELAY_COUNT);
  652. writel(nvec->i2c_addr >> 1, nvec->base + I2C_SL_ADDR1);
  653. writel(0, nvec->base + I2C_SL_ADDR2);
  654. enable_irq(nvec->irq);
  655. }
  656. #ifdef CONFIG_PM_SLEEP
  657. static void nvec_disable_i2c_slave(struct nvec_chip *nvec)
  658. {
  659. disable_irq(nvec->irq);
  660. writel(I2C_SL_NEWSL | I2C_SL_NACK, nvec->base + I2C_SL_CNFG);
  661. clk_disable_unprepare(nvec->i2c_clk);
  662. }
  663. #endif
  664. static void nvec_power_off(void)
  665. {
  666. char ap_pwr_down[] = { NVEC_SLEEP, AP_PWR_DOWN };
  667. nvec_toggle_global_events(nvec_power_handle, false);
  668. nvec_write_async(nvec_power_handle, ap_pwr_down, 2);
  669. }
  670. static int tegra_nvec_probe(struct platform_device *pdev)
  671. {
  672. int err, ret;
  673. struct clk *i2c_clk;
  674. struct device *dev = &pdev->dev;
  675. struct nvec_chip *nvec;
  676. struct nvec_msg *msg;
  677. struct resource *res;
  678. void __iomem *base;
  679. char get_firmware_version[] = { NVEC_CNTL, GET_FIRMWARE_VERSION },
  680. unmute_speakers[] = { NVEC_OEM0, 0x10, 0x59, 0x95 },
  681. enable_event[7] = { NVEC_SYS, CNF_EVENT_REPORTING, true };
  682. if (!dev->of_node) {
  683. dev_err(dev, "must be instantiated using device tree\n");
  684. return -ENODEV;
  685. }
  686. nvec = devm_kzalloc(dev, sizeof(struct nvec_chip), GFP_KERNEL);
  687. if (!nvec)
  688. return -ENOMEM;
  689. platform_set_drvdata(pdev, nvec);
  690. nvec->dev = dev;
  691. if (of_property_read_u32(dev->of_node, "slave-addr", &nvec->i2c_addr)) {
  692. dev_err(dev, "no i2c address specified");
  693. return -ENODEV;
  694. }
  695. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  696. base = devm_ioremap_resource(dev, res);
  697. if (IS_ERR(base))
  698. return PTR_ERR(base);
  699. nvec->irq = platform_get_irq(pdev, 0);
  700. if (nvec->irq < 0) {
  701. dev_err(dev, "no irq resource?\n");
  702. return -ENODEV;
  703. }
  704. i2c_clk = devm_clk_get(dev, "div-clk");
  705. if (IS_ERR(i2c_clk)) {
  706. dev_err(dev, "failed to get controller clock\n");
  707. return -ENODEV;
  708. }
  709. nvec->rst = devm_reset_control_get_exclusive(dev, "i2c");
  710. if (IS_ERR(nvec->rst)) {
  711. dev_err(dev, "failed to get controller reset\n");
  712. return PTR_ERR(nvec->rst);
  713. }
  714. nvec->base = base;
  715. nvec->i2c_clk = i2c_clk;
  716. nvec->rx = &nvec->msg_pool[0];
  717. ATOMIC_INIT_NOTIFIER_HEAD(&nvec->notifier_list);
  718. init_completion(&nvec->sync_write);
  719. init_completion(&nvec->ec_transfer);
  720. mutex_init(&nvec->sync_write_mutex);
  721. spin_lock_init(&nvec->tx_lock);
  722. spin_lock_init(&nvec->rx_lock);
  723. INIT_LIST_HEAD(&nvec->rx_data);
  724. INIT_LIST_HEAD(&nvec->tx_data);
  725. INIT_WORK(&nvec->rx_work, nvec_dispatch);
  726. INIT_WORK(&nvec->tx_work, nvec_request_master);
  727. nvec->gpiod = devm_gpiod_get(dev, "request", GPIOD_OUT_HIGH);
  728. if (IS_ERR(nvec->gpiod)) {
  729. dev_err(dev, "couldn't request gpio\n");
  730. return PTR_ERR(nvec->gpiod);
  731. }
  732. err = devm_request_irq(dev, nvec->irq, nvec_interrupt, 0,
  733. "nvec", nvec);
  734. if (err) {
  735. dev_err(dev, "couldn't request irq\n");
  736. return -ENODEV;
  737. }
  738. disable_irq(nvec->irq);
  739. tegra_init_i2c_slave(nvec);
  740. /* enable event reporting */
  741. nvec_toggle_global_events(nvec, true);
  742. nvec->nvec_status_notifier.notifier_call = nvec_status_notifier;
  743. nvec_register_notifier(nvec, &nvec->nvec_status_notifier, 0);
  744. nvec_power_handle = nvec;
  745. pm_power_off = nvec_power_off;
  746. /* Get Firmware Version */
  747. err = nvec_write_sync(nvec, get_firmware_version, 2, &msg);
  748. if (!err) {
  749. dev_warn(dev,
  750. "ec firmware version %02x.%02x.%02x / %02x\n",
  751. msg->data[4], msg->data[5],
  752. msg->data[6], msg->data[7]);
  753. nvec_msg_free(nvec, msg);
  754. }
  755. ret = mfd_add_devices(dev, 0, nvec_devices,
  756. ARRAY_SIZE(nvec_devices), NULL, 0, NULL);
  757. if (ret)
  758. dev_err(dev, "error adding subdevices\n");
  759. /* unmute speakers? */
  760. nvec_write_async(nvec, unmute_speakers, 4);
  761. /* enable lid switch event */
  762. nvec_event_mask(enable_event, LID_SWITCH);
  763. nvec_write_async(nvec, enable_event, 7);
  764. /* enable power button event */
  765. nvec_event_mask(enable_event, PWR_BUTTON);
  766. nvec_write_async(nvec, enable_event, 7);
  767. return 0;
  768. }
  769. static int tegra_nvec_remove(struct platform_device *pdev)
  770. {
  771. struct nvec_chip *nvec = platform_get_drvdata(pdev);
  772. nvec_toggle_global_events(nvec, false);
  773. mfd_remove_devices(nvec->dev);
  774. nvec_unregister_notifier(nvec, &nvec->nvec_status_notifier);
  775. cancel_work_sync(&nvec->rx_work);
  776. cancel_work_sync(&nvec->tx_work);
  777. /* FIXME: needs check whether nvec is responsible for power off */
  778. pm_power_off = NULL;
  779. return 0;
  780. }
  781. #ifdef CONFIG_PM_SLEEP
  782. static int nvec_suspend(struct device *dev)
  783. {
  784. int err;
  785. struct nvec_chip *nvec = dev_get_drvdata(dev);
  786. struct nvec_msg *msg;
  787. char ap_suspend[] = { NVEC_SLEEP, AP_SUSPEND };
  788. dev_dbg(nvec->dev, "suspending\n");
  789. /* keep these sync or you'll break suspend */
  790. nvec_toggle_global_events(nvec, false);
  791. err = nvec_write_sync(nvec, ap_suspend, sizeof(ap_suspend), &msg);
  792. if (!err)
  793. nvec_msg_free(nvec, msg);
  794. nvec_disable_i2c_slave(nvec);
  795. return 0;
  796. }
  797. static int nvec_resume(struct device *dev)
  798. {
  799. struct nvec_chip *nvec = dev_get_drvdata(dev);
  800. dev_dbg(nvec->dev, "resuming\n");
  801. tegra_init_i2c_slave(nvec);
  802. nvec_toggle_global_events(nvec, true);
  803. return 0;
  804. }
  805. #endif
  806. static SIMPLE_DEV_PM_OPS(nvec_pm_ops, nvec_suspend, nvec_resume);
  807. /* Match table for of_platform binding */
  808. static const struct of_device_id nvidia_nvec_of_match[] = {
  809. { .compatible = "nvidia,nvec", },
  810. {},
  811. };
  812. MODULE_DEVICE_TABLE(of, nvidia_nvec_of_match);
  813. static struct platform_driver nvec_device_driver = {
  814. .probe = tegra_nvec_probe,
  815. .remove = tegra_nvec_remove,
  816. .driver = {
  817. .name = "nvec",
  818. .pm = &nvec_pm_ops,
  819. .of_match_table = nvidia_nvec_of_match,
  820. }
  821. };
  822. module_platform_driver(nvec_device_driver);
  823. MODULE_ALIAS("platform:nvec");
  824. MODULE_DESCRIPTION("NVIDIA compliant embedded controller interface");
  825. MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
  826. MODULE_LICENSE("GPL");