xref: /linux/drivers/net/pse-pd/realtek-pse-mcu.h (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef _REALTEK_PSE_MCU_H
4 #define _REALTEK_PSE_MCU_H
5 
6 #include <linux/mutex.h>
7 #include <linux/pse-pd/pse.h>
8 #include <linux/types.h>
9 
10 /*
11  * Time the MCU itself needs between accepting a request and having a
12  * response ready. These are properties of the MCU firmware, not of the
13  * underlying transport: the core paces transactions by RTPSE_MCU_RESPONSE_MS
14  * and both transports size their per-transaction recv ceiling from
15  * RTPSE_MCU_RESPONSE_MAX_MS, since some commands are documented as
16  * needing up to ~1s to produce a reply.
17  */
18 #define RTPSE_MCU_RESPONSE_MS			25
19 #define RTPSE_MCU_RESPONSE_MAX_MS		1000
20 
21 /*
22  * Total time to keep retrying the first MCU read at probe, and the pause
23  * between attempts. Right after reset-gpios is deasserted the MCU may not
24  * answer on the bus yet; give it a bounded window to come up before
25  * declaring the probe failed.
26  */
27 #define RTPSE_MCU_BOOT_TIMEOUT_MS		3000
28 #define RTPSE_MCU_BOOT_RETRY_MS			100
29 
30 #define RTPSE_MCU_MSG_SIZE			12
31 
32 struct rtpse_mcu_msg {
33 	u8 opcode;
34 	u8 seq_num;
35 	u8 payload[9];
36 	u8 checksum;
37 } __packed;
38 
39 /*
40  * MCU status opcodes (seen on the Gen1 dialect; Gen2 never emits them).
41  * INCOMPLETE/BAD_CSUM are terminal; NOT_READY is transient.
42  */
43 #define RTPSE_MCU_OPCODE_INCOMPLETE		0xfd	/* -EBADE   */
44 #define RTPSE_MCU_OPCODE_BAD_CSUM		0xfe	/* -EBADMSG */
45 #define RTPSE_MCU_OPCODE_NOT_READY		0xff	/* -EAGAIN  */
46 
47 /*
48  * A polling transport can stop here: the reply to this request (opcode and
49  * seq_num) or a terminal error. The seq_num rejects a stale normal reply; the
50  * terminal errors match unconditionally, as a request the MCU couldn't parse
51  * carries no seq_num to correlate against.
52  */
rtpse_mcu_resp_is_final(const struct rtpse_mcu_msg * req,const struct rtpse_mcu_msg * resp)53 static inline bool rtpse_mcu_resp_is_final(const struct rtpse_mcu_msg *req,
54 					   const struct rtpse_mcu_msg *resp)
55 {
56 	return (resp->opcode == req->opcode && resp->seq_num == req->seq_num) ||
57 	       resp->opcode == RTPSE_MCU_OPCODE_INCOMPLETE ||
58 	       resp->opcode == RTPSE_MCU_OPCODE_BAD_CSUM;
59 }
60 
61 /* Opaque to transports; defined in realtek-pse-mcu-core.c. */
62 struct rtpse_mcu_dialect;
63 struct rtpse_mcu_chip_info;
64 struct rtpse_mcu_ctrl;
65 
66 /* Per-compatible match data (the of_match .data). */
67 struct rtpse_mcu_match_data {
68 	const struct rtpse_mcu_dialect *dialect;
69 	bool native_i2c;	/* raw-I2C framing (vs SMBus); I2C transport only */
70 };
71 
72 struct rtpse_mcu_transport_ops {
73 	int (*send)(struct rtpse_mcu_ctrl *pse, const struct rtpse_mcu_msg *req);
74 	int (*recv)(struct rtpse_mcu_ctrl *pse, const struct rtpse_mcu_msg *req,
75 		    struct rtpse_mcu_msg *resp);
76 };
77 
78 struct rtpse_mcu_ctrl {
79 	struct device *dev;
80 	struct pse_controller_dev pcdev;
81 	struct mutex mutex;	/* serializes MCU request/response transactions */
82 	const struct rtpse_mcu_dialect *dialect;
83 	const struct rtpse_mcu_chip_info *chip;
84 	const struct rtpse_mcu_transport_ops *transport;
85 	u8 seq;			/* rolling request seq_num, echoed by the MCU */
86 };
87 
88 int rtpse_mcu_register(struct rtpse_mcu_ctrl *pse);
89 
90 extern const struct rtpse_mcu_match_data rtpse_mcu_gen1_data;
91 extern const struct rtpse_mcu_match_data rtpse_mcu_gen2_data;
92 extern const struct rtpse_mcu_match_data rtpse_mcu_gen2_i2c_data;
93 
94 #endif
95