nvec.h 5.7 KB

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