1 /* SPDX-License-Identifier: GPL-2.0 */
3 * CZ.NIC's Turris Omnia MCU driver
8 #ifndef __TURRIS_OMNIA_MCU_H
9 #define __TURRIS_OMNIA_MCU_H
11 #include <linux/bitops.h>
12 #include <linux/completion.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/hw_random.h>
15 #include <linux/if_ether.h>
16 #include <linux/mutex.h>
17 #include <linux/types.h>
18 #include <linux/watchdog.h>
19 #include <linux/workqueue.h>
20 #include <asm/byteorder.h>
21 #include <asm/unaligned.h>
27 struct i2c_client *client;
31 /* board information */
32 u64 board_serial_number;
33 u8 board_first_mac[ETH_ALEN];
36 #ifdef CONFIG_TURRIS_OMNIA_MCU_GPIO
40 unsigned long mask, rising, falling, both, cached, is_cached;
41 /* Old MCU firmware handling needs the following */
42 struct delayed_work button_release_emul_work;
43 unsigned long last_status;
44 bool button_pressed_emul;
47 #ifdef CONFIG_TURRIS_OMNIA_MCU_SYSOFF_WAKEUP
48 /* RTC device for configuring wake-up */
49 struct rtc_device *rtcdev;
51 bool front_button_poweron;
54 #ifdef CONFIG_TURRIS_OMNIA_MCU_WATCHDOG
56 struct watchdog_device wdt;
59 #ifdef CONFIG_TURRIS_OMNIA_MCU_TRNG
60 /* true random number generator */
62 struct completion trng_entropy_ready;
66 int omnia_cmd_write_read(const struct i2c_client *client,
67 void *cmd, unsigned int cmd_len,
68 void *reply, unsigned int reply_len);
70 static inline int omnia_cmd_write(const struct i2c_client *client, void *cmd,
73 return omnia_cmd_write_read(client, cmd, len, NULL, 0);
76 static inline int omnia_cmd_write_u8(const struct i2c_client *client, u8 cmd,
79 u8 buf[2] = { cmd, val };
81 return omnia_cmd_write(client, buf, sizeof(buf));
84 static inline int omnia_cmd_write_u16(const struct i2c_client *client, u8 cmd,
90 put_unaligned_le16(val, &buf[1]);
92 return omnia_cmd_write(client, buf, sizeof(buf));
95 static inline int omnia_cmd_write_u32(const struct i2c_client *client, u8 cmd,
101 put_unaligned_le32(val, &buf[1]);
103 return omnia_cmd_write(client, buf, sizeof(buf));
106 static inline int omnia_cmd_read(const struct i2c_client *client, u8 cmd,
107 void *reply, unsigned int len)
109 return omnia_cmd_write_read(client, &cmd, 1, reply, len);
112 static inline unsigned int
113 omnia_compute_reply_length(unsigned long mask, bool interleaved,
119 return ((__fls(mask) >> 3) << interleaved) + 1 + offset;
122 /* Returns 0 on success */
123 static inline int omnia_cmd_read_bits(const struct i2c_client *client, u8 cmd,
124 unsigned long bits, unsigned long *dst)
134 err = omnia_cmd_read(client, cmd, &reply,
135 omnia_compute_reply_length(bits, false, 0));
139 *dst = le32_to_cpu(reply) & bits;
144 static inline int omnia_cmd_read_bit(const struct i2c_client *client, u8 cmd,
150 err = omnia_cmd_read_bits(client, cmd, bit, &reply);
157 static inline int omnia_cmd_read_u32(const struct i2c_client *client, u8 cmd,
163 err = omnia_cmd_read(client, cmd, &reply, sizeof(reply));
167 *dst = le32_to_cpu(reply);
172 static inline int omnia_cmd_read_u16(const struct i2c_client *client, u8 cmd,
178 err = omnia_cmd_read(client, cmd, &reply, sizeof(reply));
182 *dst = le16_to_cpu(reply);
187 static inline int omnia_cmd_read_u8(const struct i2c_client *client, u8 cmd,
190 return omnia_cmd_read(client, cmd, reply, sizeof(*reply));
193 #ifdef CONFIG_TURRIS_OMNIA_MCU_GPIO
194 extern const u8 omnia_int_to_gpio_idx[32];
195 extern const struct attribute_group omnia_mcu_gpio_group;
196 int omnia_mcu_register_gpiochip(struct omnia_mcu *mcu);
198 static inline int omnia_mcu_register_gpiochip(struct omnia_mcu *mcu)
204 #ifdef CONFIG_TURRIS_OMNIA_MCU_SYSOFF_WAKEUP
205 extern const struct attribute_group omnia_mcu_poweroff_group;
206 int omnia_mcu_register_sys_off_and_wakeup(struct omnia_mcu *mcu);
208 static inline int omnia_mcu_register_sys_off_and_wakeup(struct omnia_mcu *mcu)
214 #ifdef CONFIG_TURRIS_OMNIA_MCU_TRNG
215 int omnia_mcu_register_trng(struct omnia_mcu *mcu);
217 static inline int omnia_mcu_register_trng(struct omnia_mcu *mcu)
223 #ifdef CONFIG_TURRIS_OMNIA_MCU_WATCHDOG
224 int omnia_mcu_register_watchdog(struct omnia_mcu *mcu);
226 static inline int omnia_mcu_register_watchdog(struct omnia_mcu *mcu)
232 #endif /* __TURRIS_OMNIA_MCU_H */