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