nvec.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.launchpad.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. #ifndef __LINUX_MFD_NVEC
  13. #define __LINUX_MFD_NVEC
  14. #include <linux/atomic.h>
  15. #include <linux/clk.h>
  16. #include <linux/completion.h>
  17. #include <linux/list.h>
  18. #include <linux/mutex.h>
  19. #include <linux/notifier.h>
  20. #include <linux/reset.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/workqueue.h>
  23. /* NVEC_POOL_SIZE - Size of the pool in &struct nvec_msg */
  24. #define NVEC_POOL_SIZE 64
  25. /*
  26. * NVEC_MSG_SIZE - Maximum size of the data field of &struct nvec_msg.
  27. *
  28. * A message must store up to a SMBus block operation which consists of
  29. * one command byte, one count byte, and up to 32 payload bytes = 34
  30. * byte.
  31. */
  32. #define NVEC_MSG_SIZE 34
  33. /**
  34. * enum nvec_event_size - The size of an event message
  35. * @NVEC_2BYTES: The message has one command byte and one data byte
  36. * @NVEC_3BYTES: The message has one command byte and two data bytes
  37. * @NVEC_VAR_SIZE: The message has one command byte, one count byte, and has
  38. * up to as many bytes as the number in the count byte. The
  39. * maximum is 32
  40. *
  41. * Events can be fixed or variable sized. This is useless on other message
  42. * types, which are always variable sized.
  43. */
  44. enum nvec_event_size {
  45. NVEC_2BYTES,
  46. NVEC_3BYTES,
  47. NVEC_VAR_SIZE,
  48. };
  49. /**
  50. * enum nvec_msg_type - The type of a message
  51. * @NVEC_SYS: A system request/response
  52. * @NVEC_BAT: A battery request/response
  53. * @NVEC_KBD: A keyboard request/response
  54. * @NVEC_PS2: A mouse request/response
  55. * @NVEC_CNTL: A EC control request/response
  56. * @NVEC_KB_EVT: An event from the keyboard
  57. * @NVEC_PS2_EVT: An event from the mouse
  58. *
  59. * Events can be fixed or variable sized. This is useless on other message
  60. * types, which are always variable sized.
  61. */
  62. enum nvec_msg_type {
  63. NVEC_SYS = 1,
  64. NVEC_BAT,
  65. NVEC_GPIO,
  66. NVEC_SLEEP,
  67. NVEC_KBD,
  68. NVEC_PS2,
  69. NVEC_CNTL,
  70. NVEC_OEM0 = 0x0d,
  71. NVEC_KB_EVT = 0x80,
  72. NVEC_PS2_EVT,
  73. };
  74. /**
  75. * struct nvec_msg - A buffer for a single message
  76. * @node: Messages are part of various lists in a &struct nvec_chip
  77. * @data: The data of the message
  78. * @size: For TX messages, the number of bytes used in @data
  79. * @pos: For RX messages, the current position to write to. For TX messages,
  80. * the position to read from.
  81. * @used: Used for the message pool to mark a message as free/allocated.
  82. *
  83. * This structure is used to hold outgoing and incoming messages. Outgoing
  84. * messages have a different format than incoming messages, and that is not
  85. * documented yet.
  86. */
  87. struct nvec_msg {
  88. struct list_head node;
  89. unsigned char data[NVEC_MSG_SIZE];
  90. unsigned short size;
  91. unsigned short pos;
  92. atomic_t used;
  93. };
  94. /**
  95. * struct nvec_chip - A single connection to an NVIDIA Embedded controller
  96. * @dev: The device
  97. * @gpio: The same as for &struct nvec_platform_data
  98. * @irq: The IRQ of the I2C device
  99. * @i2c_addr: The address of the I2C slave
  100. * @base: The base of the memory mapped region of the I2C device
  101. * @i2c_clk: The clock of the I2C device
  102. * @rst: The reset of the I2C device
  103. * @notifier_list: Notifiers to be called on received messages, see
  104. * nvec_register_notifier()
  105. * @rx_data: Received messages that have to be processed
  106. * @tx_data: Messages waiting to be sent to the controller
  107. * @nvec_status_notifier: Internal notifier (see nvec_status_notifier())
  108. * @rx_work: A work structure for the RX worker nvec_dispatch()
  109. * @tx_work: A work structure for the TX worker nvec_request_master()
  110. * @wq: The work queue in which @rx_work and @tx_work are executed
  111. * @rx: The message currently being retrieved or %NULL
  112. * @msg_pool: A pool of messages for allocation
  113. * @tx: The message currently being transferred
  114. * @tx_scratch: Used for building pseudo messages
  115. * @ec_transfer: A completion that will be completed once a message has been
  116. * received (see nvec_rx_completed())
  117. * @tx_lock: Spinlock for modifications on @tx_data
  118. * @rx_lock: Spinlock for modifications on @rx_data
  119. * @sync_write_mutex: A mutex for nvec_write_sync()
  120. * @sync_write: A completion to signal that a synchronous message is complete
  121. * @sync_write_pending: The first two bytes of the request (type and subtype)
  122. * @last_sync_msg: The last synchronous message.
  123. * @state: State of our finite state machine used in nvec_interrupt()
  124. */
  125. struct nvec_chip {
  126. struct device *dev;
  127. struct gpio_desc *gpiod;
  128. int irq;
  129. u32 i2c_addr;
  130. void __iomem *base;
  131. struct clk *i2c_clk;
  132. struct reset_control *rst;
  133. struct atomic_notifier_head notifier_list;
  134. struct list_head rx_data, tx_data;
  135. struct notifier_block nvec_status_notifier;
  136. struct work_struct rx_work, tx_work;
  137. struct workqueue_struct *wq;
  138. struct nvec_msg msg_pool[NVEC_POOL_SIZE];
  139. struct nvec_msg *rx;
  140. struct nvec_msg *tx;
  141. struct nvec_msg tx_scratch;
  142. struct completion ec_transfer;
  143. spinlock_t tx_lock, rx_lock;
  144. /* sync write stuff */
  145. struct mutex sync_write_mutex;
  146. struct completion sync_write;
  147. u16 sync_write_pending;
  148. struct nvec_msg *last_sync_msg;
  149. int state;
  150. };
  151. int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
  152. short size);
  153. int nvec_write_sync(struct nvec_chip *nvec,
  154. const unsigned char *data, short size,
  155. struct nvec_msg **msg);
  156. int nvec_register_notifier(struct nvec_chip *nvec,
  157. struct notifier_block *nb,
  158. unsigned int events);
  159. int nvec_unregister_notifier(struct nvec_chip *dev, struct notifier_block *nb);
  160. void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg);
  161. #endif