driver.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <peda@axentia.se>
  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/semaphore.h>
  14. struct mux_chip;
  15. struct mux_control;
  16. /**
  17. * struct mux_control_ops - Mux controller operations for a mux chip.
  18. * @set: Set the state of the given mux controller.
  19. */
  20. struct mux_control_ops {
  21. int (*set)(struct mux_control *mux, int state);
  22. };
  23. /**
  24. * struct mux_control - Represents a mux controller.
  25. * @lock: Protects the mux controller state.
  26. * @chip: The mux chip that is handling this mux controller.
  27. * @cached_state: The current mux controller state, or -1 if none.
  28. * @states: The number of mux controller states.
  29. * @idle_state: The mux controller state to use when inactive, or one
  30. * of MUX_IDLE_AS_IS and MUX_IDLE_DISCONNECT.
  31. *
  32. * Mux drivers may only change @states and @idle_state, and may only do so
  33. * between allocation and registration of the mux controller. Specifically,
  34. * @cached_state is internal to the mux core and should never be written by
  35. * mux drivers.
  36. */
  37. struct mux_control {
  38. struct semaphore lock; /* protects the state of the mux */
  39. struct mux_chip *chip;
  40. int cached_state;
  41. unsigned int states;
  42. int idle_state;
  43. };
  44. /**
  45. * struct mux_chip - Represents a chip holding mux controllers.
  46. * @controllers: Number of mux controllers handled by the chip.
  47. * @mux: Array of mux controllers that are handled.
  48. * @dev: Device structure.
  49. * @id: Used to identify the device internally.
  50. * @ops: Mux controller operations.
  51. */
  52. struct mux_chip {
  53. unsigned int controllers;
  54. struct mux_control *mux;
  55. struct device dev;
  56. int id;
  57. const struct mux_control_ops *ops;
  58. };
  59. #define to_mux_chip(x) container_of((x), struct mux_chip, dev)
  60. /**
  61. * mux_chip_priv() - Get the extra memory reserved by mux_chip_alloc().
  62. * @mux_chip: The mux-chip to get the private memory from.
  63. *
  64. * Return: Pointer to the private memory reserved by the allocator.
  65. */
  66. static inline void *mux_chip_priv(struct mux_chip *mux_chip)
  67. {
  68. return &mux_chip->mux[mux_chip->controllers];
  69. }
  70. struct mux_chip *mux_chip_alloc(struct device *dev,
  71. unsigned int controllers, size_t sizeof_priv);
  72. int mux_chip_register(struct mux_chip *mux_chip);
  73. void mux_chip_unregister(struct mux_chip *mux_chip);
  74. void mux_chip_free(struct mux_chip *mux_chip);
  75. struct mux_chip *devm_mux_chip_alloc(struct device *dev,
  76. unsigned int controllers,
  77. size_t sizeof_priv);
  78. int devm_mux_chip_register(struct device *dev, struct mux_chip *mux_chip);
  79. /**
  80. * mux_control_get_index() - Get the index of the given mux controller
  81. * @mux: The mux-control to get the index for.
  82. *
  83. * Return: The index of the mux controller within the mux chip the mux
  84. * controller is a part of.
  85. */
  86. static inline unsigned int mux_control_get_index(struct mux_control *mux)
  87. {
  88. return mux - mux->chip->mux;
  89. }
  90. #endif /* _LINUX_MUX_DRIVER_H */