driver.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * mux/driver.h - definitions for the multiplexer driver interface
  4. *
  5. * Copyright (C) 2017 Axentia Technologies AB
  6. *
  7. * Author: Peter Rosin <[email protected]>
  8. */
  9. #ifndef _LINUX_MUX_DRIVER_H
  10. #define _LINUX_MUX_DRIVER_H
  11. #include <dt-bindings/mux/mux.h>
  12. #include <linux/device.h>
  13. #include <linux/ktime.h>
  14. #include <linux/semaphore.h>
  15. struct mux_chip;
  16. struct mux_control;
  17. /**
  18. * struct mux_control_ops - Mux controller operations for a mux chip.
  19. * @set: Set the state of the given mux controller.
  20. */
  21. struct mux_control_ops {
  22. int (*set)(struct mux_control *mux, int state);
  23. };
  24. /**
  25. * struct mux_control - Represents a mux controller.
  26. * @lock: Protects the mux controller state.
  27. * @chip: The mux chip that is handling this mux controller.
  28. * @cached_state: The current mux controller state, or -1 if none.
  29. * @states: The number of mux controller states.
  30. * @idle_state: The mux controller state to use when inactive, or one
  31. * of MUX_IDLE_AS_IS and MUX_IDLE_DISCONNECT.
  32. * @last_change: Timestamp of last change
  33. *
  34. * Mux drivers may only change @states and @idle_state, and may only do so
  35. * between allocation and registration of the mux controller. Specifically,
  36. * @cached_state is internal to the mux core and should never be written by
  37. * mux drivers.
  38. */
  39. struct mux_control {
  40. struct semaphore lock; /* protects the state of the mux */
  41. struct mux_chip *chip;
  42. int cached_state;
  43. unsigned int states;
  44. int idle_state;
  45. ktime_t last_change;
  46. };
  47. /**
  48. * struct mux_chip - Represents a chip holding mux controllers.
  49. * @controllers: Number of mux controllers handled by the chip.
  50. * @mux: Array of mux controllers that are handled.
  51. * @dev: Device structure.
  52. * @id: Used to identify the device internally.
  53. * @ops: Mux controller operations.
  54. */
  55. struct mux_chip {
  56. unsigned int controllers;
  57. struct mux_control *mux;
  58. struct device dev;
  59. int id;
  60. const struct mux_control_ops *ops;
  61. };
  62. #define to_mux_chip(x) container_of((x), struct mux_chip, dev)
  63. /**
  64. * mux_chip_priv() - Get the extra memory reserved by mux_chip_alloc().
  65. * @mux_chip: The mux-chip to get the private memory from.
  66. *
  67. * Return: Pointer to the private memory reserved by the allocator.
  68. */
  69. static inline void *mux_chip_priv(struct mux_chip *mux_chip)
  70. {
  71. return &mux_chip->mux[mux_chip->controllers];
  72. }
  73. struct mux_chip *mux_chip_alloc(struct device *dev,
  74. unsigned int controllers, size_t sizeof_priv);
  75. int mux_chip_register(struct mux_chip *mux_chip);
  76. void mux_chip_unregister(struct mux_chip *mux_chip);
  77. void mux_chip_free(struct mux_chip *mux_chip);
  78. struct mux_chip *devm_mux_chip_alloc(struct device *dev,
  79. unsigned int controllers,
  80. size_t sizeof_priv);
  81. int devm_mux_chip_register(struct device *dev, struct mux_chip *mux_chip);
  82. /**
  83. * mux_control_get_index() - Get the index of the given mux controller
  84. * @mux: The mux-control to get the index for.
  85. *
  86. * Return: The index of the mux controller within the mux chip the mux
  87. * controller is a part of.
  88. */
  89. static inline unsigned int mux_control_get_index(struct mux_control *mux)
  90. {
  91. return mux - mux->chip->mux;
  92. }
  93. #endif /* _LINUX_MUX_DRIVER_H */