Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * mux/driver.h - definitions for the multiplexer driver interface
0004  *
0005  * Copyright (C) 2017 Axentia Technologies AB
0006  *
0007  * Author: Peter Rosin <peda@axentia.se>
0008  */
0009 
0010 #ifndef _LINUX_MUX_DRIVER_H
0011 #define _LINUX_MUX_DRIVER_H
0012 
0013 #include <dt-bindings/mux/mux.h>
0014 #include <linux/device.h>
0015 #include <linux/ktime.h>
0016 #include <linux/semaphore.h>
0017 
0018 struct mux_chip;
0019 struct mux_control;
0020 
0021 /**
0022  * struct mux_control_ops - Mux controller operations for a mux chip.
0023  * @set:            Set the state of the given mux controller.
0024  */
0025 struct mux_control_ops {
0026     int (*set)(struct mux_control *mux, int state);
0027 };
0028 
0029 /**
0030  * struct mux_control - Represents a mux controller.
0031  * @lock:       Protects the mux controller state.
0032  * @chip:       The mux chip that is handling this mux controller.
0033  * @cached_state:   The current mux controller state, or -1 if none.
0034  * @states:     The number of mux controller states.
0035  * @idle_state:     The mux controller state to use when inactive, or one
0036  *          of MUX_IDLE_AS_IS and MUX_IDLE_DISCONNECT.
0037  * @last_change:    Timestamp of last change
0038  *
0039  * Mux drivers may only change @states and @idle_state, and may only do so
0040  * between allocation and registration of the mux controller. Specifically,
0041  * @cached_state is internal to the mux core and should never be written by
0042  * mux drivers.
0043  */
0044 struct mux_control {
0045     struct semaphore lock; /* protects the state of the mux */
0046 
0047     struct mux_chip *chip;
0048     int cached_state;
0049 
0050     unsigned int states;
0051     int idle_state;
0052 
0053     ktime_t last_change;
0054 };
0055 
0056 /**
0057  * struct mux_chip -    Represents a chip holding mux controllers.
0058  * @controllers:    Number of mux controllers handled by the chip.
0059  * @mux:        Array of mux controllers that are handled.
0060  * @dev:        Device structure.
0061  * @id:         Used to identify the device internally.
0062  * @ops:        Mux controller operations.
0063  */
0064 struct mux_chip {
0065     unsigned int controllers;
0066     struct mux_control *mux;
0067     struct device dev;
0068     int id;
0069 
0070     const struct mux_control_ops *ops;
0071 };
0072 
0073 #define to_mux_chip(x) container_of((x), struct mux_chip, dev)
0074 
0075 /**
0076  * mux_chip_priv() - Get the extra memory reserved by mux_chip_alloc().
0077  * @mux_chip: The mux-chip to get the private memory from.
0078  *
0079  * Return: Pointer to the private memory reserved by the allocator.
0080  */
0081 static inline void *mux_chip_priv(struct mux_chip *mux_chip)
0082 {
0083     return &mux_chip->mux[mux_chip->controllers];
0084 }
0085 
0086 struct mux_chip *mux_chip_alloc(struct device *dev,
0087                 unsigned int controllers, size_t sizeof_priv);
0088 int mux_chip_register(struct mux_chip *mux_chip);
0089 void mux_chip_unregister(struct mux_chip *mux_chip);
0090 void mux_chip_free(struct mux_chip *mux_chip);
0091 
0092 struct mux_chip *devm_mux_chip_alloc(struct device *dev,
0093                      unsigned int controllers,
0094                      size_t sizeof_priv);
0095 int devm_mux_chip_register(struct device *dev, struct mux_chip *mux_chip);
0096 
0097 /**
0098  * mux_control_get_index() - Get the index of the given mux controller
0099  * @mux: The mux-control to get the index for.
0100  *
0101  * Return: The index of the mux controller within the mux chip the mux
0102  * controller is a part of.
0103  */
0104 static inline unsigned int mux_control_get_index(struct mux_control *mux)
0105 {
0106     return mux - mux->chip->mux;
0107 }
0108 
0109 #endif /* _LINUX_MUX_DRIVER_H */