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