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