mux.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Based on the linux multiplexer framework
  4. *
  5. * At its core, a multiplexer (or mux), also known as a data selector, is a
  6. * device that selects between several analog or digital input signals and
  7. * forwards it to a single output line. This notion can be extended to work
  8. * with buses, like a I2C bus multiplexer for example.
  9. *
  10. * Copyright (C) 2017 Axentia Technologies AB
  11. * Author: Peter Rosin <peda@axentia.se>
  12. *
  13. * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
  14. * Jean-Jacques Hiblot <jjhiblot@ti.com>
  15. */
  16. #ifndef _MUX_H_
  17. #define _MUX_H_
  18. #include <linux/errno.h>
  19. #include <linux/types.h>
  20. struct udevice;
  21. struct mux_control;
  22. #if CONFIG_IS_ENABLED(MULTIPLEXER)
  23. /**
  24. * mux_control_states() - Query the number of multiplexer states.
  25. * @mux: The mux-control to query.
  26. *
  27. * Return: The number of multiplexer states.
  28. */
  29. unsigned int mux_control_states(struct mux_control *mux);
  30. /**
  31. * mux_control_select() - Select the given multiplexer state.
  32. * @mux: The mux-control to request a change of state from.
  33. * @state: The new requested state.
  34. *
  35. * On successfully selecting the mux-control state, it will be locked until
  36. * there is a call to mux_control_deselect(). If the mux-control is already
  37. * selected when mux_control_select() is called, the function will indicate
  38. * -EBUSY
  39. *
  40. * Therefore, make sure to call mux_control_deselect() when the operation is
  41. * complete and the mux-control is free for others to use, but do not call
  42. * mux_control_deselect() if mux_control_select() fails.
  43. *
  44. * Return: 0 when the mux-control state has the requested state or a negative
  45. * errno on error.
  46. */
  47. int __must_check mux_control_select(struct mux_control *mux,
  48. unsigned int state);
  49. #define mux_control_try_select(mux) mux_control_select(mux)
  50. /**
  51. * mux_control_deselect() - Deselect the previously selected multiplexer state.
  52. * @mux: The mux-control to deselect.
  53. *
  54. * It is required that a single call is made to mux_control_deselect() for
  55. * each and every successful call made to either of mux_control_select() or
  56. * mux_control_try_select().
  57. *
  58. * Return: 0 on success and a negative errno on error. An error can only
  59. * occur if the mux has an idle state. Note that even if an error occurs, the
  60. * mux-control is unlocked and is thus free for the next access.
  61. */
  62. int mux_control_deselect(struct mux_control *mux);
  63. /**
  64. * mux_get_by_index() = Get a mux by integer index.
  65. * @dev: The client device.
  66. * @index: The index of the mux to get.
  67. * @mux: A pointer to the 'mux_control' struct to initialize.
  68. *
  69. * This looks up and initializes a mux. The index is relative to the client
  70. * device.
  71. *
  72. * Return: 0 if OK, or a negative error code.
  73. */
  74. int mux_get_by_index(struct udevice *dev, int index, struct mux_control **mux);
  75. /**
  76. * mux_control_get() - Get the mux-control for a device.
  77. * @dev: The device that needs a mux-control.
  78. * @mux_name: The name identifying the mux-control.
  79. * @mux: A pointer to the mux-control pointer.
  80. *
  81. * Return: 0 of OK, or a negative error code.
  82. */
  83. int mux_control_get(struct udevice *dev, const char *name,
  84. struct mux_control **mux);
  85. /**
  86. * mux_control_put() - Put away the mux-control for good.
  87. * @mux: The mux-control to put away.
  88. *
  89. * mux_control_put() reverses the effects of mux_control_get().
  90. */
  91. void mux_control_put(struct mux_control *mux);
  92. /**
  93. * devm_mux_control_get() - Get the mux-control for a device, with resource
  94. * management.
  95. * @dev: The device that needs a mux-control.
  96. * @mux_name: The name identifying the mux-control.
  97. *
  98. * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
  99. */
  100. struct mux_control *devm_mux_control_get(struct udevice *dev,
  101. const char *mux_name);
  102. /**
  103. * dm_mux_init() - Initialize the multiplexer controls to their default state.
  104. *
  105. * Return: 0 if OK, -errno otherwise.
  106. */
  107. int dm_mux_init(void);
  108. #else
  109. unsigned int mux_control_states(struct mux_control *mux)
  110. {
  111. return -ENOSYS;
  112. }
  113. int __must_check mux_control_select(struct mux_control *mux,
  114. unsigned int state)
  115. {
  116. return -ENOSYS;
  117. }
  118. #define mux_control_try_select(mux) mux_control_select(mux)
  119. int mux_control_deselect(struct mux_control *mux)
  120. {
  121. return -ENOSYS;
  122. }
  123. struct mux_control *mux_control_get(struct udevice *dev, const char *mux_name)
  124. {
  125. return NULL;
  126. }
  127. void mux_control_put(struct mux_control *mux)
  128. {
  129. }
  130. struct mux_control *devm_mux_control_get(struct udevice *dev,
  131. const char *mux_name)
  132. {
  133. return NULL;
  134. }
  135. int dm_mux_init(void)
  136. {
  137. return -ENOSYS;
  138. }
  139. #endif
  140. #endif