nvec.c 26 KB

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