xref: /linux/drivers/input/rmi4/rmi_bus.h (revision 58d42ec10b7324fa2965d74b9bfbacac3af55c70)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2011-2016 Synaptics Incorporated
4  * Copyright (c) 2011 Unixphere
5  */
6 
7 #ifndef _RMI_BUS_H
8 #define _RMI_BUS_H
9 
10 #include <linux/rmi.h>
11 
12 struct rmi_device;
13 
14 /*
15  * The interrupt source count in the function descriptor can represent up to
16  * 6 interrupt sources in the normal manner.
17  */
18 #define RMI_FN_MAX_IRQS	6
19 
20 /**
21  * struct rmi_function - represents the implementation of an RMI4
22  * function for a particular device (basically, a driver for that RMI4 function)
23  *
24  * @fd: The function descriptor of the RMI function
25  * @rmi_dev: Pointer to the RMI device associated with this function container
26  * @dev: The device associated with this particular function.
27  *
28  * @num_of_irqs: The number of irqs needed by this function
29  * @irq_pos: The position in the irq bitfield this function holds
30  * @irq_mask: For convenience, can be used to mask IRQ bits off during ATTN
31  * interrupt handling.
32  * @irqs: assigned virq numbers (up to num_of_irqs)
33  *
34  * @node: entry in device's list of functions
35  */
36 struct rmi_function {
37 	struct rmi_function_descriptor fd;
38 	struct rmi_device *rmi_dev;
39 	struct device dev;
40 	struct list_head node;
41 
42 	unsigned int num_of_irqs;
43 	int irq[RMI_FN_MAX_IRQS];
44 	unsigned int irq_pos;
45 	unsigned long irq_mask[];
46 };
47 
48 #define to_rmi_function(d)	container_of(d, struct rmi_function, dev)
49 
50 bool rmi_is_function_device(struct device *dev);
51 
52 struct rmi_function *rmi_alloc_function(struct rmi_device *rmi_dev, u8 id);
53 int __must_check rmi_register_function(struct rmi_function *);
54 void rmi_unregister_function(struct rmi_function *);
55 
56 /**
57  * struct rmi_function_handler - driver routines for a particular RMI function.
58  *
59  * @func: The RMI function number
60  * @reset: Called when a reset of the touch sensor is detected.  The routine
61  * should perform any out-of-the-ordinary reset handling that might be
62  * necessary.  Restoring of touch sensor configuration registers should be
63  * handled in the config() callback, below.
64  * @config: Called when the function container is first initialized, and
65  * after a reset is detected.  This routine should write any necessary
66  * configuration settings to the device.
67  * @attention: Called when the IRQ(s) for the function are set by the touch
68  * sensor.
69  * @suspend: Should perform any required operations to suspend the particular
70  * function.
71  * @resume: Should perform any required operations to resume the particular
72  * function.
73  *
74  * All callbacks are expected to return 0 on success, error code on failure.
75  */
76 struct rmi_function_handler {
77 	struct device_driver driver;
78 
79 	u8 func;
80 
81 	int (*probe)(struct rmi_function *fn);
82 	void (*remove)(struct rmi_function *fn);
83 	int (*config)(struct rmi_function *fn);
84 	int (*reset)(struct rmi_function *fn);
85 	irqreturn_t (*attention)(int irq, void *ctx);
86 	int (*suspend)(struct rmi_function *fn);
87 	int (*resume)(struct rmi_function *fn);
88 };
89 
90 #define to_rmi_function_handler(d) \
91 		container_of_const(d, struct rmi_function_handler, driver)
92 
93 int __must_check __rmi_register_function_handler(struct rmi_function_handler *,
94 						 struct module *, const char *);
95 #define rmi_register_function_handler(handler) \
96 	__rmi_register_function_handler(handler, THIS_MODULE, KBUILD_MODNAME)
97 
98 void rmi_unregister_function_handler(struct rmi_function_handler *);
99 
100 #define to_rmi_driver(d) \
101 	container_of(d, struct rmi_driver, driver)
102 
103 #define to_rmi_device(d) container_of(d, struct rmi_device, dev)
104 
105 static inline struct rmi_device_platform_data *
106 rmi_get_platform_data(struct rmi_device *d)
107 {
108 	return &d->xport->pdata;
109 }
110 
111 bool rmi_is_physical_device(struct device *dev);
112 
113 /**
114  * rmi_reset - reset a RMI4 device
115  * @d: Pointer to an RMI device
116  *
117  * Calls for a reset of each function implemented by a specific device.
118  * Returns 0 on success or a negative error code.
119  */
120 static inline int rmi_reset(struct rmi_device *d)
121 {
122 	return d->driver->reset_handler(d);
123 }
124 
125 /**
126  * rmi_read - read a single byte
127  * @d: Pointer to an RMI device
128  * @addr: The address to read from
129  * @buf: The read buffer
130  *
131  * Reads a single byte of data using the underlying transport protocol
132  * into memory pointed by @buf. It returns 0 on success or a negative
133  * error code.
134  */
135 static inline int rmi_read(struct rmi_device *d, u16 addr, u8 *buf)
136 {
137 	return d->xport->ops->read_block(d->xport, addr, buf, 1);
138 }
139 
140 /**
141  * rmi_read_block - read a block of bytes
142  * @d: Pointer to an RMI device
143  * @addr: The start address to read from
144  * @buf: The read buffer
145  * @len: Length of the read buffer
146  *
147  * Reads a block of byte data using the underlying transport protocol
148  * into memory pointed by @buf. It returns 0 on success or a negative
149  * error code.
150  */
151 static inline int rmi_read_block(struct rmi_device *d, u16 addr,
152 				 void *buf, size_t len)
153 {
154 	return d->xport->ops->read_block(d->xport, addr, buf, len);
155 }
156 
157 /**
158  * rmi_write - write a single byte
159  * @d: Pointer to an RMI device
160  * @addr: The address to write to
161  * @data: The data to write
162  *
163  * Writes a single byte using the underlying transport protocol. It
164  * returns zero on success or a negative error code.
165  */
166 static inline int rmi_write(struct rmi_device *d, u16 addr, u8 data)
167 {
168 	return d->xport->ops->write_block(d->xport, addr, &data, 1);
169 }
170 
171 /**
172  * rmi_write_block - write a block of bytes
173  * @d: Pointer to an RMI device
174  * @addr: The start address to write to
175  * @buf: The write buffer
176  * @len: Length of the write buffer
177  *
178  * Writes a block of byte data from buf using the underlaying transport
179  * protocol.  It returns the amount of bytes written or a negative error code.
180  */
181 static inline int rmi_write_block(struct rmi_device *d, u16 addr,
182 				  const void *buf, size_t len)
183 {
184 	return d->xport->ops->write_block(d->xport, addr, buf, len);
185 }
186 
187 int rmi_for_each_dev(void *data, int (*func)(struct device *dev, void *data));
188 
189 extern const struct bus_type rmi_bus_type;
190 
191 int rmi_of_property_read_u32(struct device *dev, u32 *result,
192 				const char *prop, bool optional);
193 
194 #define RMI_DEBUG_CORE			BIT(0)
195 #define RMI_DEBUG_XPORT			BIT(1)
196 #define RMI_DEBUG_FN			BIT(2)
197 #define RMI_DEBUG_2D_SENSOR		BIT(3)
198 
199 void rmi_dbg(int flags, struct device *dev, const char *fmt, ...);
200 #endif
201