1dfa556e4SMarek Behún // SPDX-License-Identifier: GPL-2.0 2dfa556e4SMarek Behún /* 3dfa556e4SMarek Behún * CZ.NIC's Turris Omnia MCU GPIO and IRQ driver 4dfa556e4SMarek Behún * 5dfa556e4SMarek Behún * 2024 by Marek Behún <kabel@kernel.org> 6dfa556e4SMarek Behún */ 7dfa556e4SMarek Behún 8dfa556e4SMarek Behún #include <linux/array_size.h> 9dfa556e4SMarek Behún #include <linux/bitfield.h> 10dfa556e4SMarek Behún #include <linux/bitops.h> 11dfa556e4SMarek Behún #include <linux/bug.h> 12dfa556e4SMarek Behún #include <linux/cleanup.h> 13dfa556e4SMarek Behún #include <linux/device.h> 14dfa556e4SMarek Behún #include <linux/devm-helpers.h> 15dfa556e4SMarek Behún #include <linux/errno.h> 16dfa556e4SMarek Behún #include <linux/gpio/driver.h> 17dfa556e4SMarek Behún #include <linux/i2c.h> 18dfa556e4SMarek Behún #include <linux/interrupt.h> 19dfa556e4SMarek Behún #include <linux/mutex.h> 20dfa556e4SMarek Behún #include <linux/sysfs.h> 21dfa556e4SMarek Behún #include <linux/types.h> 22dfa556e4SMarek Behún #include <linux/workqueue.h> 23dfa556e4SMarek Behún #include <asm/unaligned.h> 24dfa556e4SMarek Behún 25dfa556e4SMarek Behún #include <linux/turris-omnia-mcu-interface.h> 26dfa556e4SMarek Behún #include "turris-omnia-mcu.h" 27dfa556e4SMarek Behún 28dfa556e4SMarek Behún #define OMNIA_CMD_INT_ARG_LEN 8 29dfa556e4SMarek Behún #define FRONT_BUTTON_RELEASE_DELAY_MS 50 30dfa556e4SMarek Behún 31dfa556e4SMarek Behún static const char * const omnia_mcu_gpio_templates[64] = { 32dfa556e4SMarek Behún /* GPIOs with value read from the 16-bit wide status */ 33dfa556e4SMarek Behún [4] = "MiniPCIe0 Card Detect", 34dfa556e4SMarek Behún [5] = "MiniPCIe0 mSATA Indicator", 35dfa556e4SMarek Behún [6] = "Front USB3 port over-current", 36dfa556e4SMarek Behún [7] = "Rear USB3 port over-current", 37dfa556e4SMarek Behún [8] = "Front USB3 port power", 38dfa556e4SMarek Behún [9] = "Rear USB3 port power", 39dfa556e4SMarek Behún [12] = "Front Button", 40dfa556e4SMarek Behún 41dfa556e4SMarek Behún /* GPIOs with value read from the 32-bit wide extended status */ 42dfa556e4SMarek Behún [16] = "SFP nDET", 43dfa556e4SMarek Behún [28] = "MiniPCIe0 LED", 44dfa556e4SMarek Behún [29] = "MiniPCIe1 LED", 45dfa556e4SMarek Behún [30] = "MiniPCIe2 LED", 46dfa556e4SMarek Behún [31] = "MiniPCIe0 PAN LED", 47dfa556e4SMarek Behún [32] = "MiniPCIe1 PAN LED", 48dfa556e4SMarek Behún [33] = "MiniPCIe2 PAN LED", 49dfa556e4SMarek Behún [34] = "WAN PHY LED0", 50dfa556e4SMarek Behún [35] = "WAN PHY LED1", 51dfa556e4SMarek Behún [36] = "LAN switch p0 LED0", 52dfa556e4SMarek Behún [37] = "LAN switch p0 LED1", 53dfa556e4SMarek Behún [38] = "LAN switch p1 LED0", 54dfa556e4SMarek Behún [39] = "LAN switch p1 LED1", 55dfa556e4SMarek Behún [40] = "LAN switch p2 LED0", 56dfa556e4SMarek Behún [41] = "LAN switch p2 LED1", 57dfa556e4SMarek Behún [42] = "LAN switch p3 LED0", 58dfa556e4SMarek Behún [43] = "LAN switch p3 LED1", 59dfa556e4SMarek Behún [44] = "LAN switch p4 LED0", 60dfa556e4SMarek Behún [45] = "LAN switch p4 LED1", 61dfa556e4SMarek Behún [46] = "LAN switch p5 LED0", 62dfa556e4SMarek Behún [47] = "LAN switch p5 LED1", 63dfa556e4SMarek Behún 64dfa556e4SMarek Behún /* GPIOs with value read from the 16-bit wide extended control status */ 65dfa556e4SMarek Behún [48] = "eMMC nRESET", 66dfa556e4SMarek Behún [49] = "LAN switch nRESET", 67dfa556e4SMarek Behún [50] = "WAN PHY nRESET", 68dfa556e4SMarek Behún [51] = "MiniPCIe0 nPERST", 69dfa556e4SMarek Behún [52] = "MiniPCIe1 nPERST", 70dfa556e4SMarek Behún [53] = "MiniPCIe2 nPERST", 71dfa556e4SMarek Behún [54] = "WAN PHY SFP mux", 72dfa556e4SMarek Behún [56] = "VHV power disable", 73dfa556e4SMarek Behún }; 74dfa556e4SMarek Behún 75dfa556e4SMarek Behún struct omnia_gpio { 76dfa556e4SMarek Behún u8 cmd; 77dfa556e4SMarek Behún u8 ctl_cmd; 78dfa556e4SMarek Behún u8 bit; 79dfa556e4SMarek Behún u8 ctl_bit; 80dfa556e4SMarek Behún u8 int_bit; 81dfa556e4SMarek Behún u16 feat; 82dfa556e4SMarek Behún u16 feat_mask; 83dfa556e4SMarek Behún }; 84dfa556e4SMarek Behún 85dfa556e4SMarek Behún #define OMNIA_GPIO_INVALID_INT_BIT 0xff 86dfa556e4SMarek Behún 87dfa556e4SMarek Behún #define _DEF_GPIO(_cmd, _ctl_cmd, _bit, _ctl_bit, _int_bit, _feat, _feat_mask) \ 88dfa556e4SMarek Behún { \ 89dfa556e4SMarek Behún .cmd = _cmd, \ 90dfa556e4SMarek Behún .ctl_cmd = _ctl_cmd, \ 91dfa556e4SMarek Behún .bit = _bit, \ 92dfa556e4SMarek Behún .ctl_bit = _ctl_bit, \ 93dfa556e4SMarek Behún .int_bit = (_int_bit) < 0 ? OMNIA_GPIO_INVALID_INT_BIT \ 94dfa556e4SMarek Behún : (_int_bit), \ 95dfa556e4SMarek Behún .feat = _feat, \ 96dfa556e4SMarek Behún .feat_mask = _feat_mask, \ 97dfa556e4SMarek Behún } 98dfa556e4SMarek Behún 99dfa556e4SMarek Behún #define _DEF_GPIO_STS(_name) \ 100dfa556e4SMarek Behún _DEF_GPIO(OMNIA_CMD_GET_STATUS_WORD, 0, __bf_shf(OMNIA_STS_ ## _name), \ 101dfa556e4SMarek Behún 0, __bf_shf(OMNIA_INT_ ## _name), 0, 0) 102dfa556e4SMarek Behún 103dfa556e4SMarek Behún #define _DEF_GPIO_CTL(_name) \ 104dfa556e4SMarek Behún _DEF_GPIO(OMNIA_CMD_GET_STATUS_WORD, OMNIA_CMD_GENERAL_CONTROL, \ 105dfa556e4SMarek Behún __bf_shf(OMNIA_STS_ ## _name), __bf_shf(OMNIA_CTL_ ## _name), \ 106dfa556e4SMarek Behún -1, 0, 0) 107dfa556e4SMarek Behún 108dfa556e4SMarek Behún #define _DEF_GPIO_EXT_STS(_name, _feat) \ 109dfa556e4SMarek Behún _DEF_GPIO(OMNIA_CMD_GET_EXT_STATUS_DWORD, 0, \ 110dfa556e4SMarek Behún __bf_shf(OMNIA_EXT_STS_ ## _name), 0, \ 111dfa556e4SMarek Behún __bf_shf(OMNIA_INT_ ## _name), \ 112dfa556e4SMarek Behún OMNIA_FEAT_ ## _feat | OMNIA_FEAT_EXT_CMDS, \ 113dfa556e4SMarek Behún OMNIA_FEAT_ ## _feat | OMNIA_FEAT_EXT_CMDS) 114dfa556e4SMarek Behún 115dfa556e4SMarek Behún #define _DEF_GPIO_EXT_STS_LED(_name, _ledext) \ 116dfa556e4SMarek Behún _DEF_GPIO(OMNIA_CMD_GET_EXT_STATUS_DWORD, 0, \ 117dfa556e4SMarek Behún __bf_shf(OMNIA_EXT_STS_ ## _name), 0, \ 118dfa556e4SMarek Behún __bf_shf(OMNIA_INT_ ## _name), \ 119dfa556e4SMarek Behún OMNIA_FEAT_LED_STATE_ ## _ledext, \ 120dfa556e4SMarek Behún OMNIA_FEAT_LED_STATE_EXT_MASK) 121dfa556e4SMarek Behún 122dfa556e4SMarek Behún #define _DEF_GPIO_EXT_STS_LEDALL(_name) \ 123dfa556e4SMarek Behún _DEF_GPIO(OMNIA_CMD_GET_EXT_STATUS_DWORD, 0, \ 124dfa556e4SMarek Behún __bf_shf(OMNIA_EXT_STS_ ## _name), 0, \ 125dfa556e4SMarek Behún __bf_shf(OMNIA_INT_ ## _name), \ 126dfa556e4SMarek Behún OMNIA_FEAT_LED_STATE_EXT_MASK, 0) 127dfa556e4SMarek Behún 128dfa556e4SMarek Behún #define _DEF_GPIO_EXT_CTL(_name, _feat) \ 129dfa556e4SMarek Behún _DEF_GPIO(OMNIA_CMD_GET_EXT_CONTROL_STATUS, OMNIA_CMD_EXT_CONTROL, \ 130dfa556e4SMarek Behún __bf_shf(OMNIA_EXT_CTL_ ## _name), \ 131dfa556e4SMarek Behún __bf_shf(OMNIA_EXT_CTL_ ## _name), -1, \ 132dfa556e4SMarek Behún OMNIA_FEAT_ ## _feat | OMNIA_FEAT_EXT_CMDS, \ 133dfa556e4SMarek Behún OMNIA_FEAT_ ## _feat | OMNIA_FEAT_EXT_CMDS) 134dfa556e4SMarek Behún 135dfa556e4SMarek Behún #define _DEF_INT(_name) \ 136dfa556e4SMarek Behún _DEF_GPIO(0, 0, 0, 0, __bf_shf(OMNIA_INT_ ## _name), 0, 0) 137dfa556e4SMarek Behún 138dfa556e4SMarek Behún static inline bool is_int_bit_valid(const struct omnia_gpio *gpio) 139dfa556e4SMarek Behún { 140dfa556e4SMarek Behún return gpio->int_bit != OMNIA_GPIO_INVALID_INT_BIT; 141dfa556e4SMarek Behún } 142dfa556e4SMarek Behún 143dfa556e4SMarek Behún static const struct omnia_gpio omnia_gpios[64] = { 144dfa556e4SMarek Behún /* GPIOs with value read from the 16-bit wide status */ 145dfa556e4SMarek Behún [4] = _DEF_GPIO_STS(CARD_DET), 146dfa556e4SMarek Behún [5] = _DEF_GPIO_STS(MSATA_IND), 147dfa556e4SMarek Behún [6] = _DEF_GPIO_STS(USB30_OVC), 148dfa556e4SMarek Behún [7] = _DEF_GPIO_STS(USB31_OVC), 149dfa556e4SMarek Behún [8] = _DEF_GPIO_CTL(USB30_PWRON), 150dfa556e4SMarek Behún [9] = _DEF_GPIO_CTL(USB31_PWRON), 151dfa556e4SMarek Behún 152dfa556e4SMarek Behún /* brightness changed interrupt, no GPIO */ 153dfa556e4SMarek Behún [11] = _DEF_INT(BRIGHTNESS_CHANGED), 154dfa556e4SMarek Behún 155dfa556e4SMarek Behún [12] = _DEF_GPIO_STS(BUTTON_PRESSED), 156dfa556e4SMarek Behún 157dfa556e4SMarek Behún /* TRNG interrupt, no GPIO */ 158dfa556e4SMarek Behún [13] = _DEF_INT(TRNG), 159dfa556e4SMarek Behún 160dfa556e4SMarek Behún /* MESSAGE_SIGNED interrupt, no GPIO */ 161dfa556e4SMarek Behún [14] = _DEF_INT(MESSAGE_SIGNED), 162dfa556e4SMarek Behún 163dfa556e4SMarek Behún /* GPIOs with value read from the 32-bit wide extended status */ 164dfa556e4SMarek Behún [16] = _DEF_GPIO_EXT_STS(SFP_nDET, PERIPH_MCU), 165dfa556e4SMarek Behún [28] = _DEF_GPIO_EXT_STS_LEDALL(WLAN0_MSATA_LED), 166dfa556e4SMarek Behún [29] = _DEF_GPIO_EXT_STS_LEDALL(WLAN1_LED), 167dfa556e4SMarek Behún [30] = _DEF_GPIO_EXT_STS_LEDALL(WLAN2_LED), 168dfa556e4SMarek Behún [31] = _DEF_GPIO_EXT_STS_LED(WPAN0_LED, EXT), 169dfa556e4SMarek Behún [32] = _DEF_GPIO_EXT_STS_LED(WPAN1_LED, EXT), 170dfa556e4SMarek Behún [33] = _DEF_GPIO_EXT_STS_LED(WPAN2_LED, EXT), 171dfa556e4SMarek Behún [34] = _DEF_GPIO_EXT_STS_LEDALL(WAN_LED0), 172dfa556e4SMarek Behún [35] = _DEF_GPIO_EXT_STS_LED(WAN_LED1, EXT_V32), 173dfa556e4SMarek Behún [36] = _DEF_GPIO_EXT_STS_LEDALL(LAN0_LED0), 174dfa556e4SMarek Behún [37] = _DEF_GPIO_EXT_STS_LEDALL(LAN0_LED1), 175dfa556e4SMarek Behún [38] = _DEF_GPIO_EXT_STS_LEDALL(LAN1_LED0), 176dfa556e4SMarek Behún [39] = _DEF_GPIO_EXT_STS_LEDALL(LAN1_LED1), 177dfa556e4SMarek Behún [40] = _DEF_GPIO_EXT_STS_LEDALL(LAN2_LED0), 178dfa556e4SMarek Behún [41] = _DEF_GPIO_EXT_STS_LEDALL(LAN2_LED1), 179dfa556e4SMarek Behún [42] = _DEF_GPIO_EXT_STS_LEDALL(LAN3_LED0), 180dfa556e4SMarek Behún [43] = _DEF_GPIO_EXT_STS_LEDALL(LAN3_LED1), 181dfa556e4SMarek Behún [44] = _DEF_GPIO_EXT_STS_LEDALL(LAN4_LED0), 182dfa556e4SMarek Behún [45] = _DEF_GPIO_EXT_STS_LEDALL(LAN4_LED1), 183dfa556e4SMarek Behún [46] = _DEF_GPIO_EXT_STS_LEDALL(LAN5_LED0), 184dfa556e4SMarek Behún [47] = _DEF_GPIO_EXT_STS_LEDALL(LAN5_LED1), 185dfa556e4SMarek Behún 186dfa556e4SMarek Behún /* GPIOs with value read from the 16-bit wide extended control status */ 187dfa556e4SMarek Behún [48] = _DEF_GPIO_EXT_CTL(nRES_MMC, PERIPH_MCU), 188dfa556e4SMarek Behún [49] = _DEF_GPIO_EXT_CTL(nRES_LAN, PERIPH_MCU), 189dfa556e4SMarek Behún [50] = _DEF_GPIO_EXT_CTL(nRES_PHY, PERIPH_MCU), 190dfa556e4SMarek Behún [51] = _DEF_GPIO_EXT_CTL(nPERST0, PERIPH_MCU), 191dfa556e4SMarek Behún [52] = _DEF_GPIO_EXT_CTL(nPERST1, PERIPH_MCU), 192dfa556e4SMarek Behún [53] = _DEF_GPIO_EXT_CTL(nPERST2, PERIPH_MCU), 193dfa556e4SMarek Behún [54] = _DEF_GPIO_EXT_CTL(PHY_SFP, PERIPH_MCU), 194dfa556e4SMarek Behún [56] = _DEF_GPIO_EXT_CTL(nVHV_CTRL, PERIPH_MCU), 195dfa556e4SMarek Behún }; 196dfa556e4SMarek Behún 197dfa556e4SMarek Behún /* mapping from interrupts to indexes of GPIOs in the omnia_gpios array */ 198*41bb142aSMarek Behún const u8 omnia_int_to_gpio_idx[32] = { 199dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_CARD_DET)] = 4, 200dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_MSATA_IND)] = 5, 201dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_USB30_OVC)] = 6, 202dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_USB31_OVC)] = 7, 203dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_BUTTON_PRESSED)] = 12, 204dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_TRNG)] = 13, 205dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_MESSAGE_SIGNED)] = 14, 206dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_SFP_nDET)] = 16, 207dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_BRIGHTNESS_CHANGED)] = 11, 208dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_WLAN0_MSATA_LED)] = 28, 209dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_WLAN1_LED)] = 29, 210dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_WLAN2_LED)] = 30, 211dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_WPAN0_LED)] = 31, 212dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_WPAN1_LED)] = 32, 213dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_WPAN2_LED)] = 33, 214dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_WAN_LED0)] = 34, 215dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_WAN_LED1)] = 35, 216dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN0_LED0)] = 36, 217dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN0_LED1)] = 37, 218dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN1_LED0)] = 38, 219dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN1_LED1)] = 39, 220dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN2_LED0)] = 40, 221dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN2_LED1)] = 41, 222dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN3_LED0)] = 42, 223dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN3_LED1)] = 43, 224dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN4_LED0)] = 44, 225dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN4_LED1)] = 45, 226dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN5_LED0)] = 46, 227dfa556e4SMarek Behún [__bf_shf(OMNIA_INT_LAN5_LED1)] = 47, 228dfa556e4SMarek Behún }; 229dfa556e4SMarek Behún 230dfa556e4SMarek Behún /* index of PHY_SFP GPIO in the omnia_gpios array */ 231dfa556e4SMarek Behún #define OMNIA_GPIO_PHY_SFP_OFFSET 54 232dfa556e4SMarek Behún 233dfa556e4SMarek Behún static int omnia_ctl_cmd_locked(struct omnia_mcu *mcu, u8 cmd, u16 val, u16 mask) 234dfa556e4SMarek Behún { 235dfa556e4SMarek Behún unsigned int len; 236dfa556e4SMarek Behún u8 buf[5]; 237dfa556e4SMarek Behún 238dfa556e4SMarek Behún buf[0] = cmd; 239dfa556e4SMarek Behún 240dfa556e4SMarek Behún switch (cmd) { 241dfa556e4SMarek Behún case OMNIA_CMD_GENERAL_CONTROL: 242dfa556e4SMarek Behún buf[1] = val; 243dfa556e4SMarek Behún buf[2] = mask; 244dfa556e4SMarek Behún len = 3; 245dfa556e4SMarek Behún break; 246dfa556e4SMarek Behún 247dfa556e4SMarek Behún case OMNIA_CMD_EXT_CONTROL: 248dfa556e4SMarek Behún put_unaligned_le16(val, &buf[1]); 249dfa556e4SMarek Behún put_unaligned_le16(mask, &buf[3]); 250dfa556e4SMarek Behún len = 5; 251dfa556e4SMarek Behún break; 252dfa556e4SMarek Behún 253dfa556e4SMarek Behún default: 254dfa556e4SMarek Behún BUG(); 255dfa556e4SMarek Behún } 256dfa556e4SMarek Behún 257dfa556e4SMarek Behún return omnia_cmd_write(mcu->client, buf, len); 258dfa556e4SMarek Behún } 259dfa556e4SMarek Behún 260dfa556e4SMarek Behún static int omnia_ctl_cmd(struct omnia_mcu *mcu, u8 cmd, u16 val, u16 mask) 261dfa556e4SMarek Behún { 262dfa556e4SMarek Behún guard(mutex)(&mcu->lock); 263dfa556e4SMarek Behún 264dfa556e4SMarek Behún return omnia_ctl_cmd_locked(mcu, cmd, val, mask); 265dfa556e4SMarek Behún } 266dfa556e4SMarek Behún 267dfa556e4SMarek Behún static int omnia_gpio_request(struct gpio_chip *gc, unsigned int offset) 268dfa556e4SMarek Behún { 269dfa556e4SMarek Behún if (!omnia_gpios[offset].cmd) 270dfa556e4SMarek Behún return -EINVAL; 271dfa556e4SMarek Behún 272dfa556e4SMarek Behún return 0; 273dfa556e4SMarek Behún } 274dfa556e4SMarek Behún 275dfa556e4SMarek Behún static int omnia_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) 276dfa556e4SMarek Behún { 277dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 278dfa556e4SMarek Behún 279dfa556e4SMarek Behún if (offset == OMNIA_GPIO_PHY_SFP_OFFSET) { 280dfa556e4SMarek Behún int val; 281dfa556e4SMarek Behún 282dfa556e4SMarek Behún scoped_guard(mutex, &mcu->lock) { 283dfa556e4SMarek Behún val = omnia_cmd_read_bit(mcu->client, 284dfa556e4SMarek Behún OMNIA_CMD_GET_EXT_CONTROL_STATUS, 285dfa556e4SMarek Behún OMNIA_EXT_CTL_PHY_SFP_AUTO); 286dfa556e4SMarek Behún if (val < 0) 287dfa556e4SMarek Behún return val; 288dfa556e4SMarek Behún } 289dfa556e4SMarek Behún 290dfa556e4SMarek Behún if (val) 291dfa556e4SMarek Behún return GPIO_LINE_DIRECTION_IN; 292dfa556e4SMarek Behún 293dfa556e4SMarek Behún return GPIO_LINE_DIRECTION_OUT; 294dfa556e4SMarek Behún } 295dfa556e4SMarek Behún 296dfa556e4SMarek Behún if (omnia_gpios[offset].ctl_cmd) 297dfa556e4SMarek Behún return GPIO_LINE_DIRECTION_OUT; 298dfa556e4SMarek Behún 299dfa556e4SMarek Behún return GPIO_LINE_DIRECTION_IN; 300dfa556e4SMarek Behún } 301dfa556e4SMarek Behún 302dfa556e4SMarek Behún static int omnia_gpio_direction_input(struct gpio_chip *gc, unsigned int offset) 303dfa556e4SMarek Behún { 304dfa556e4SMarek Behún const struct omnia_gpio *gpio = &omnia_gpios[offset]; 305dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 306dfa556e4SMarek Behún 307dfa556e4SMarek Behún if (offset == OMNIA_GPIO_PHY_SFP_OFFSET) 308dfa556e4SMarek Behún return omnia_ctl_cmd(mcu, OMNIA_CMD_EXT_CONTROL, 309dfa556e4SMarek Behún OMNIA_EXT_CTL_PHY_SFP_AUTO, 310dfa556e4SMarek Behún OMNIA_EXT_CTL_PHY_SFP_AUTO); 311dfa556e4SMarek Behún 312dfa556e4SMarek Behún if (gpio->ctl_cmd) 313dfa556e4SMarek Behún return -ENOTSUPP; 314dfa556e4SMarek Behún 315dfa556e4SMarek Behún return 0; 316dfa556e4SMarek Behún } 317dfa556e4SMarek Behún 318dfa556e4SMarek Behún static int omnia_gpio_direction_output(struct gpio_chip *gc, 319dfa556e4SMarek Behún unsigned int offset, int value) 320dfa556e4SMarek Behún { 321dfa556e4SMarek Behún const struct omnia_gpio *gpio = &omnia_gpios[offset]; 322dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 323dfa556e4SMarek Behún u16 val, mask; 324dfa556e4SMarek Behún 325dfa556e4SMarek Behún if (!gpio->ctl_cmd) 326dfa556e4SMarek Behún return -ENOTSUPP; 327dfa556e4SMarek Behún 328dfa556e4SMarek Behún mask = BIT(gpio->ctl_bit); 329dfa556e4SMarek Behún val = value ? mask : 0; 330dfa556e4SMarek Behún 331dfa556e4SMarek Behún if (offset == OMNIA_GPIO_PHY_SFP_OFFSET) 332dfa556e4SMarek Behún mask |= OMNIA_EXT_CTL_PHY_SFP_AUTO; 333dfa556e4SMarek Behún 334dfa556e4SMarek Behún return omnia_ctl_cmd(mcu, gpio->ctl_cmd, val, mask); 335dfa556e4SMarek Behún } 336dfa556e4SMarek Behún 337dfa556e4SMarek Behún static int omnia_gpio_get(struct gpio_chip *gc, unsigned int offset) 338dfa556e4SMarek Behún { 339dfa556e4SMarek Behún const struct omnia_gpio *gpio = &omnia_gpios[offset]; 340dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 341dfa556e4SMarek Behún 342dfa556e4SMarek Behún /* 343dfa556e4SMarek Behún * If firmware does not support the new interrupt API, we are informed 344dfa556e4SMarek Behún * of every change of the status word by an interrupt from MCU and save 345dfa556e4SMarek Behún * its value in the interrupt service routine. Simply return the saved 346dfa556e4SMarek Behún * value. 347dfa556e4SMarek Behún */ 348dfa556e4SMarek Behún if (gpio->cmd == OMNIA_CMD_GET_STATUS_WORD && 349dfa556e4SMarek Behún !(mcu->features & OMNIA_FEAT_NEW_INT_API)) 350dfa556e4SMarek Behún return test_bit(gpio->bit, &mcu->last_status); 351dfa556e4SMarek Behún 352dfa556e4SMarek Behún guard(mutex)(&mcu->lock); 353dfa556e4SMarek Behún 354dfa556e4SMarek Behún /* 355dfa556e4SMarek Behún * If firmware does support the new interrupt API, we may have cached 356dfa556e4SMarek Behún * the value of a GPIO in the interrupt service routine. If not, read 357dfa556e4SMarek Behún * the relevant bit now. 358dfa556e4SMarek Behún */ 359dfa556e4SMarek Behún if (is_int_bit_valid(gpio) && test_bit(gpio->int_bit, &mcu->is_cached)) 360dfa556e4SMarek Behún return test_bit(gpio->int_bit, &mcu->cached); 361dfa556e4SMarek Behún 362dfa556e4SMarek Behún return omnia_cmd_read_bit(mcu->client, gpio->cmd, BIT(gpio->bit)); 363dfa556e4SMarek Behún } 364dfa556e4SMarek Behún 365dfa556e4SMarek Behún static unsigned long * 366dfa556e4SMarek Behún _relevant_field_for_sts_cmd(u8 cmd, unsigned long *sts, unsigned long *ext_sts, 367dfa556e4SMarek Behún unsigned long *ext_ctl) 368dfa556e4SMarek Behún { 369dfa556e4SMarek Behún switch (cmd) { 370dfa556e4SMarek Behún case OMNIA_CMD_GET_STATUS_WORD: 371dfa556e4SMarek Behún return sts; 372dfa556e4SMarek Behún case OMNIA_CMD_GET_EXT_STATUS_DWORD: 373dfa556e4SMarek Behún return ext_sts; 374dfa556e4SMarek Behún case OMNIA_CMD_GET_EXT_CONTROL_STATUS: 375dfa556e4SMarek Behún return ext_ctl; 376dfa556e4SMarek Behún default: 377dfa556e4SMarek Behún return NULL; 378dfa556e4SMarek Behún } 379dfa556e4SMarek Behún } 380dfa556e4SMarek Behún 381dfa556e4SMarek Behún static int omnia_gpio_get_multiple(struct gpio_chip *gc, unsigned long *mask, 382dfa556e4SMarek Behún unsigned long *bits) 383dfa556e4SMarek Behún { 384dfa556e4SMarek Behún unsigned long sts = 0, ext_sts = 0, ext_ctl = 0, *field; 385dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 386dfa556e4SMarek Behún struct i2c_client *client = mcu->client; 387dfa556e4SMarek Behún unsigned int i; 388dfa556e4SMarek Behún int err; 389dfa556e4SMarek Behún 390dfa556e4SMarek Behún /* determine which bits to read from the 3 possible commands */ 391dfa556e4SMarek Behún for_each_set_bit(i, mask, ARRAY_SIZE(omnia_gpios)) { 392dfa556e4SMarek Behún field = _relevant_field_for_sts_cmd(omnia_gpios[i].cmd, 393dfa556e4SMarek Behún &sts, &ext_sts, &ext_ctl); 394dfa556e4SMarek Behún if (!field) 395dfa556e4SMarek Behún continue; 396dfa556e4SMarek Behún 397dfa556e4SMarek Behún __set_bit(omnia_gpios[i].bit, field); 398dfa556e4SMarek Behún } 399dfa556e4SMarek Behún 400dfa556e4SMarek Behún guard(mutex)(&mcu->lock); 401dfa556e4SMarek Behún 402dfa556e4SMarek Behún if (mcu->features & OMNIA_FEAT_NEW_INT_API) { 403dfa556e4SMarek Behún /* read relevant bits from status */ 404dfa556e4SMarek Behún err = omnia_cmd_read_bits(client, OMNIA_CMD_GET_STATUS_WORD, 405dfa556e4SMarek Behún sts, &sts); 406dfa556e4SMarek Behún if (err) 407dfa556e4SMarek Behún return err; 408dfa556e4SMarek Behún } else { 409dfa556e4SMarek Behún /* 410dfa556e4SMarek Behún * Use status word value cached in the interrupt service routine 411dfa556e4SMarek Behún * if firmware does not support the new interrupt API. 412dfa556e4SMarek Behún */ 413dfa556e4SMarek Behún sts = mcu->last_status; 414dfa556e4SMarek Behún } 415dfa556e4SMarek Behún 416dfa556e4SMarek Behún /* read relevant bits from extended status */ 417dfa556e4SMarek Behún err = omnia_cmd_read_bits(client, OMNIA_CMD_GET_EXT_STATUS_DWORD, 418dfa556e4SMarek Behún ext_sts, &ext_sts); 419dfa556e4SMarek Behún if (err) 420dfa556e4SMarek Behún return err; 421dfa556e4SMarek Behún 422dfa556e4SMarek Behún /* read relevant bits from extended control */ 423dfa556e4SMarek Behún err = omnia_cmd_read_bits(client, OMNIA_CMD_GET_EXT_CONTROL_STATUS, 424dfa556e4SMarek Behún ext_ctl, &ext_ctl); 425dfa556e4SMarek Behún if (err) 426dfa556e4SMarek Behún return err; 427dfa556e4SMarek Behún 428dfa556e4SMarek Behún /* assign relevant bits in result */ 429dfa556e4SMarek Behún for_each_set_bit(i, mask, ARRAY_SIZE(omnia_gpios)) { 430dfa556e4SMarek Behún field = _relevant_field_for_sts_cmd(omnia_gpios[i].cmd, 431dfa556e4SMarek Behún &sts, &ext_sts, &ext_ctl); 432dfa556e4SMarek Behún if (!field) 433dfa556e4SMarek Behún continue; 434dfa556e4SMarek Behún 435dfa556e4SMarek Behún __assign_bit(i, bits, test_bit(omnia_gpios[i].bit, field)); 436dfa556e4SMarek Behún } 437dfa556e4SMarek Behún 438dfa556e4SMarek Behún return 0; 439dfa556e4SMarek Behún } 440dfa556e4SMarek Behún 441dfa556e4SMarek Behún static void omnia_gpio_set(struct gpio_chip *gc, unsigned int offset, int value) 442dfa556e4SMarek Behún { 443dfa556e4SMarek Behún const struct omnia_gpio *gpio = &omnia_gpios[offset]; 444dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 445dfa556e4SMarek Behún u16 val, mask; 446dfa556e4SMarek Behún 447dfa556e4SMarek Behún if (!gpio->ctl_cmd) 448dfa556e4SMarek Behún return; 449dfa556e4SMarek Behún 450dfa556e4SMarek Behún mask = BIT(gpio->ctl_bit); 451dfa556e4SMarek Behún val = value ? mask : 0; 452dfa556e4SMarek Behún 453dfa556e4SMarek Behún omnia_ctl_cmd(mcu, gpio->ctl_cmd, val, mask); 454dfa556e4SMarek Behún } 455dfa556e4SMarek Behún 456dfa556e4SMarek Behún static void omnia_gpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, 457dfa556e4SMarek Behún unsigned long *bits) 458dfa556e4SMarek Behún { 459dfa556e4SMarek Behún unsigned long ctl = 0, ctl_mask = 0, ext_ctl = 0, ext_ctl_mask = 0; 460dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 461dfa556e4SMarek Behún unsigned int i; 462dfa556e4SMarek Behún 463dfa556e4SMarek Behún for_each_set_bit(i, mask, ARRAY_SIZE(omnia_gpios)) { 464dfa556e4SMarek Behún unsigned long *field, *field_mask; 465dfa556e4SMarek Behún u8 bit = omnia_gpios[i].ctl_bit; 466dfa556e4SMarek Behún 467dfa556e4SMarek Behún switch (omnia_gpios[i].ctl_cmd) { 468dfa556e4SMarek Behún case OMNIA_CMD_GENERAL_CONTROL: 469dfa556e4SMarek Behún field = &ctl; 470dfa556e4SMarek Behún field_mask = &ctl_mask; 471dfa556e4SMarek Behún break; 472dfa556e4SMarek Behún case OMNIA_CMD_EXT_CONTROL: 473dfa556e4SMarek Behún field = &ext_ctl; 474dfa556e4SMarek Behún field_mask = &ext_ctl_mask; 475dfa556e4SMarek Behún break; 476dfa556e4SMarek Behún default: 477dfa556e4SMarek Behún field = field_mask = NULL; 478dfa556e4SMarek Behún break; 479dfa556e4SMarek Behún } 480dfa556e4SMarek Behún 481dfa556e4SMarek Behún if (!field) 482dfa556e4SMarek Behún continue; 483dfa556e4SMarek Behún 484dfa556e4SMarek Behún __set_bit(bit, field_mask); 485dfa556e4SMarek Behún __assign_bit(bit, field, test_bit(i, bits)); 486dfa556e4SMarek Behún } 487dfa556e4SMarek Behún 488dfa556e4SMarek Behún guard(mutex)(&mcu->lock); 489dfa556e4SMarek Behún 490dfa556e4SMarek Behún if (ctl_mask) 491dfa556e4SMarek Behún omnia_ctl_cmd_locked(mcu, OMNIA_CMD_GENERAL_CONTROL, 492dfa556e4SMarek Behún ctl, ctl_mask); 493dfa556e4SMarek Behún 494dfa556e4SMarek Behún if (ext_ctl_mask) 495dfa556e4SMarek Behún omnia_ctl_cmd_locked(mcu, OMNIA_CMD_EXT_CONTROL, 496dfa556e4SMarek Behún ext_ctl, ext_ctl_mask); 497dfa556e4SMarek Behún } 498dfa556e4SMarek Behún 499dfa556e4SMarek Behún static bool omnia_gpio_available(struct omnia_mcu *mcu, 500dfa556e4SMarek Behún const struct omnia_gpio *gpio) 501dfa556e4SMarek Behún { 502dfa556e4SMarek Behún if (gpio->feat_mask) 503dfa556e4SMarek Behún return (mcu->features & gpio->feat_mask) == gpio->feat; 504dfa556e4SMarek Behún 505dfa556e4SMarek Behún if (gpio->feat) 506dfa556e4SMarek Behún return mcu->features & gpio->feat; 507dfa556e4SMarek Behún 508dfa556e4SMarek Behún return true; 509dfa556e4SMarek Behún } 510dfa556e4SMarek Behún 511dfa556e4SMarek Behún static int omnia_gpio_init_valid_mask(struct gpio_chip *gc, 512dfa556e4SMarek Behún unsigned long *valid_mask, 513dfa556e4SMarek Behún unsigned int ngpios) 514dfa556e4SMarek Behún { 515dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 516dfa556e4SMarek Behún 517dfa556e4SMarek Behún for (unsigned int i = 0; i < ngpios; i++) { 518dfa556e4SMarek Behún const struct omnia_gpio *gpio = &omnia_gpios[i]; 519dfa556e4SMarek Behún 520dfa556e4SMarek Behún if (gpio->cmd || is_int_bit_valid(gpio)) 521dfa556e4SMarek Behún __assign_bit(i, valid_mask, 522dfa556e4SMarek Behún omnia_gpio_available(mcu, gpio)); 523dfa556e4SMarek Behún else 524dfa556e4SMarek Behún __clear_bit(i, valid_mask); 525dfa556e4SMarek Behún } 526dfa556e4SMarek Behún 527dfa556e4SMarek Behún return 0; 528dfa556e4SMarek Behún } 529dfa556e4SMarek Behún 530dfa556e4SMarek Behún static int omnia_gpio_of_xlate(struct gpio_chip *gc, 531dfa556e4SMarek Behún const struct of_phandle_args *gpiospec, 532dfa556e4SMarek Behún u32 *flags) 533dfa556e4SMarek Behún { 534dfa556e4SMarek Behún u32 bank, gpio; 535dfa556e4SMarek Behún 536dfa556e4SMarek Behún if (WARN_ON(gpiospec->args_count != 3)) 537dfa556e4SMarek Behún return -EINVAL; 538dfa556e4SMarek Behún 539dfa556e4SMarek Behún if (flags) 540dfa556e4SMarek Behún *flags = gpiospec->args[2]; 541dfa556e4SMarek Behún 542dfa556e4SMarek Behún bank = gpiospec->args[0]; 543dfa556e4SMarek Behún gpio = gpiospec->args[1]; 544dfa556e4SMarek Behún 545dfa556e4SMarek Behún switch (bank) { 546dfa556e4SMarek Behún case 0: 547dfa556e4SMarek Behún return gpio < 16 ? gpio : -EINVAL; 548dfa556e4SMarek Behún case 1: 549dfa556e4SMarek Behún return gpio < 32 ? 16 + gpio : -EINVAL; 550dfa556e4SMarek Behún case 2: 551dfa556e4SMarek Behún return gpio < 16 ? 48 + gpio : -EINVAL; 552dfa556e4SMarek Behún default: 553dfa556e4SMarek Behún return -EINVAL; 554dfa556e4SMarek Behún } 555dfa556e4SMarek Behún } 556dfa556e4SMarek Behún 557dfa556e4SMarek Behún static void omnia_irq_shutdown(struct irq_data *d) 558dfa556e4SMarek Behún { 559dfa556e4SMarek Behún struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 560dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 561dfa556e4SMarek Behún irq_hw_number_t hwirq = irqd_to_hwirq(d); 562dfa556e4SMarek Behún u8 bit = omnia_gpios[hwirq].int_bit; 563dfa556e4SMarek Behún 564dfa556e4SMarek Behún __clear_bit(bit, &mcu->rising); 565dfa556e4SMarek Behún __clear_bit(bit, &mcu->falling); 566dfa556e4SMarek Behún } 567dfa556e4SMarek Behún 568dfa556e4SMarek Behún static void omnia_irq_mask(struct irq_data *d) 569dfa556e4SMarek Behún { 570dfa556e4SMarek Behún struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 571dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 572dfa556e4SMarek Behún irq_hw_number_t hwirq = irqd_to_hwirq(d); 573dfa556e4SMarek Behún u8 bit = omnia_gpios[hwirq].int_bit; 574dfa556e4SMarek Behún 575dfa556e4SMarek Behún if (!omnia_gpios[hwirq].cmd) 576dfa556e4SMarek Behún __clear_bit(bit, &mcu->rising); 577dfa556e4SMarek Behún __clear_bit(bit, &mcu->mask); 578dfa556e4SMarek Behún gpiochip_disable_irq(gc, hwirq); 579dfa556e4SMarek Behún } 580dfa556e4SMarek Behún 581dfa556e4SMarek Behún static void omnia_irq_unmask(struct irq_data *d) 582dfa556e4SMarek Behún { 583dfa556e4SMarek Behún struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 584dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 585dfa556e4SMarek Behún irq_hw_number_t hwirq = irqd_to_hwirq(d); 586dfa556e4SMarek Behún u8 bit = omnia_gpios[hwirq].int_bit; 587dfa556e4SMarek Behún 588dfa556e4SMarek Behún gpiochip_enable_irq(gc, hwirq); 589dfa556e4SMarek Behún __set_bit(bit, &mcu->mask); 590dfa556e4SMarek Behún if (!omnia_gpios[hwirq].cmd) 591dfa556e4SMarek Behún __set_bit(bit, &mcu->rising); 592dfa556e4SMarek Behún } 593dfa556e4SMarek Behún 594dfa556e4SMarek Behún static int omnia_irq_set_type(struct irq_data *d, unsigned int type) 595dfa556e4SMarek Behún { 596dfa556e4SMarek Behún struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 597dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 598dfa556e4SMarek Behún irq_hw_number_t hwirq = irqd_to_hwirq(d); 599dfa556e4SMarek Behún struct device *dev = &mcu->client->dev; 600dfa556e4SMarek Behún u8 bit = omnia_gpios[hwirq].int_bit; 601dfa556e4SMarek Behún 602dfa556e4SMarek Behún if (!(type & IRQ_TYPE_EDGE_BOTH)) { 603dfa556e4SMarek Behún dev_err(dev, "irq %u: unsupported type %u\n", d->irq, type); 604dfa556e4SMarek Behún return -EINVAL; 605dfa556e4SMarek Behún } 606dfa556e4SMarek Behún 607dfa556e4SMarek Behún __assign_bit(bit, &mcu->rising, type & IRQ_TYPE_EDGE_RISING); 608dfa556e4SMarek Behún __assign_bit(bit, &mcu->falling, type & IRQ_TYPE_EDGE_FALLING); 609dfa556e4SMarek Behún 610dfa556e4SMarek Behún return 0; 611dfa556e4SMarek Behún } 612dfa556e4SMarek Behún 613dfa556e4SMarek Behún static void omnia_irq_bus_lock(struct irq_data *d) 614dfa556e4SMarek Behún { 615dfa556e4SMarek Behún struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 616dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 617dfa556e4SMarek Behún 618dfa556e4SMarek Behún /* nothing to do if MCU firmware does not support new interrupt API */ 619dfa556e4SMarek Behún if (!(mcu->features & OMNIA_FEAT_NEW_INT_API)) 620dfa556e4SMarek Behún return; 621dfa556e4SMarek Behún 622dfa556e4SMarek Behún mutex_lock(&mcu->lock); 623dfa556e4SMarek Behún } 624dfa556e4SMarek Behún 625dfa556e4SMarek Behún /** 626dfa556e4SMarek Behún * omnia_mask_interleave - Interleaves the bytes from @rising and @falling 627dfa556e4SMarek Behún * @dst: the destination u8 array of interleaved bytes 628dfa556e4SMarek Behún * @rising: rising mask 629dfa556e4SMarek Behún * @falling: falling mask 630dfa556e4SMarek Behún * 631dfa556e4SMarek Behún * Interleaves the little-endian bytes from @rising and @falling words. 632dfa556e4SMarek Behún * 633dfa556e4SMarek Behún * If @rising = (r0, r1, r2, r3) and @falling = (f0, f1, f2, f3), the result is 634dfa556e4SMarek Behún * @dst = (r0, f0, r1, f1, r2, f2, r3, f3). 635dfa556e4SMarek Behún * 636dfa556e4SMarek Behún * The MCU receives an interrupt mask and reports a pending interrupt bitmap in 637dfa556e4SMarek Behún * this interleaved format. The rationale behind this is that the low-indexed 638dfa556e4SMarek Behún * bits are more important - in many cases, the user will be interested only in 639dfa556e4SMarek Behún * interrupts with indexes 0 to 7, and so the system can stop reading after 640dfa556e4SMarek Behún * first 2 bytes (r0, f0), to save time on the slow I2C bus. 641dfa556e4SMarek Behún * 642dfa556e4SMarek Behún * Feel free to remove this function and its inverse, omnia_mask_deinterleave, 643dfa556e4SMarek Behún * and use an appropriate bitmap_*() function once such a function exists. 644dfa556e4SMarek Behún */ 645dfa556e4SMarek Behún static void 646dfa556e4SMarek Behún omnia_mask_interleave(u8 *dst, unsigned long rising, unsigned long falling) 647dfa556e4SMarek Behún { 648dfa556e4SMarek Behún for (unsigned int i = 0; i < sizeof(u32); i++) { 649dfa556e4SMarek Behún dst[2 * i] = rising >> (8 * i); 650dfa556e4SMarek Behún dst[2 * i + 1] = falling >> (8 * i); 651dfa556e4SMarek Behún } 652dfa556e4SMarek Behún } 653dfa556e4SMarek Behún 654dfa556e4SMarek Behún /** 655dfa556e4SMarek Behún * omnia_mask_deinterleave - Deinterleaves the bytes into @rising and @falling 656dfa556e4SMarek Behún * @src: the source u8 array containing the interleaved bytes 657dfa556e4SMarek Behún * @rising: pointer where to store the rising mask gathered from @src 658dfa556e4SMarek Behún * @falling: pointer where to store the falling mask gathered from @src 659dfa556e4SMarek Behún * 660dfa556e4SMarek Behún * This is the inverse function to omnia_mask_interleave. 661dfa556e4SMarek Behún */ 662dfa556e4SMarek Behún static void omnia_mask_deinterleave(const u8 *src, unsigned long *rising, 663dfa556e4SMarek Behún unsigned long *falling) 664dfa556e4SMarek Behún { 665dfa556e4SMarek Behún *rising = *falling = 0; 666dfa556e4SMarek Behún 667dfa556e4SMarek Behún for (unsigned int i = 0; i < sizeof(u32); i++) { 668dfa556e4SMarek Behún *rising |= src[2 * i] << (8 * i); 669dfa556e4SMarek Behún *falling |= src[2 * i + 1] << (8 * i); 670dfa556e4SMarek Behún } 671dfa556e4SMarek Behún } 672dfa556e4SMarek Behún 673dfa556e4SMarek Behún static void omnia_irq_bus_sync_unlock(struct irq_data *d) 674dfa556e4SMarek Behún { 675dfa556e4SMarek Behún struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 676dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 677dfa556e4SMarek Behún struct device *dev = &mcu->client->dev; 678dfa556e4SMarek Behún u8 cmd[1 + OMNIA_CMD_INT_ARG_LEN]; 679dfa556e4SMarek Behún unsigned long rising, falling; 680dfa556e4SMarek Behún int err; 681dfa556e4SMarek Behún 682dfa556e4SMarek Behún /* nothing to do if MCU firmware does not support new interrupt API */ 683dfa556e4SMarek Behún if (!(mcu->features & OMNIA_FEAT_NEW_INT_API)) 684dfa556e4SMarek Behún return; 685dfa556e4SMarek Behún 686dfa556e4SMarek Behún cmd[0] = OMNIA_CMD_SET_INT_MASK; 687dfa556e4SMarek Behún 688dfa556e4SMarek Behún rising = mcu->rising & mcu->mask; 689dfa556e4SMarek Behún falling = mcu->falling & mcu->mask; 690dfa556e4SMarek Behún 691dfa556e4SMarek Behún /* interleave the rising and falling bytes into the command arguments */ 692dfa556e4SMarek Behún omnia_mask_interleave(&cmd[1], rising, falling); 693dfa556e4SMarek Behún 694dfa556e4SMarek Behún dev_dbg(dev, "set int mask %8ph\n", &cmd[1]); 695dfa556e4SMarek Behún 696dfa556e4SMarek Behún err = omnia_cmd_write(mcu->client, cmd, sizeof(cmd)); 697dfa556e4SMarek Behún if (err) { 698dfa556e4SMarek Behún dev_err(dev, "Cannot set mask: %d\n", err); 699dfa556e4SMarek Behún goto unlock; 700dfa556e4SMarek Behún } 701dfa556e4SMarek Behún 702dfa556e4SMarek Behún /* 703dfa556e4SMarek Behún * Remember which GPIOs have both rising and falling interrupts enabled. 704dfa556e4SMarek Behún * For those we will cache their value so that .get() method is faster. 705dfa556e4SMarek Behún * We also need to forget cached values of GPIOs that aren't cached 706dfa556e4SMarek Behún * anymore. 707dfa556e4SMarek Behún */ 708dfa556e4SMarek Behún mcu->both = rising & falling; 709dfa556e4SMarek Behún mcu->is_cached &= mcu->both; 710dfa556e4SMarek Behún 711dfa556e4SMarek Behún unlock: 712dfa556e4SMarek Behún mutex_unlock(&mcu->lock); 713dfa556e4SMarek Behún } 714dfa556e4SMarek Behún 715dfa556e4SMarek Behún static const struct irq_chip omnia_mcu_irq_chip = { 716dfa556e4SMarek Behún .name = "Turris Omnia MCU interrupts", 717dfa556e4SMarek Behún .irq_shutdown = omnia_irq_shutdown, 718dfa556e4SMarek Behún .irq_mask = omnia_irq_mask, 719dfa556e4SMarek Behún .irq_unmask = omnia_irq_unmask, 720dfa556e4SMarek Behún .irq_set_type = omnia_irq_set_type, 721dfa556e4SMarek Behún .irq_bus_lock = omnia_irq_bus_lock, 722dfa556e4SMarek Behún .irq_bus_sync_unlock = omnia_irq_bus_sync_unlock, 723dfa556e4SMarek Behún .flags = IRQCHIP_IMMUTABLE, 724dfa556e4SMarek Behún GPIOCHIP_IRQ_RESOURCE_HELPERS, 725dfa556e4SMarek Behún }; 726dfa556e4SMarek Behún 727dfa556e4SMarek Behún static void omnia_irq_init_valid_mask(struct gpio_chip *gc, 728dfa556e4SMarek Behún unsigned long *valid_mask, 729dfa556e4SMarek Behún unsigned int ngpios) 730dfa556e4SMarek Behún { 731dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 732dfa556e4SMarek Behún 733dfa556e4SMarek Behún for (unsigned int i = 0; i < ngpios; i++) { 734dfa556e4SMarek Behún const struct omnia_gpio *gpio = &omnia_gpios[i]; 735dfa556e4SMarek Behún 736dfa556e4SMarek Behún if (is_int_bit_valid(gpio)) 737dfa556e4SMarek Behún __assign_bit(i, valid_mask, 738dfa556e4SMarek Behún omnia_gpio_available(mcu, gpio)); 739dfa556e4SMarek Behún else 740dfa556e4SMarek Behún __clear_bit(i, valid_mask); 741dfa556e4SMarek Behún } 742dfa556e4SMarek Behún } 743dfa556e4SMarek Behún 744dfa556e4SMarek Behún static int omnia_irq_init_hw(struct gpio_chip *gc) 745dfa556e4SMarek Behún { 746dfa556e4SMarek Behún struct omnia_mcu *mcu = gpiochip_get_data(gc); 747dfa556e4SMarek Behún u8 cmd[1 + OMNIA_CMD_INT_ARG_LEN] = {}; 748dfa556e4SMarek Behún 749dfa556e4SMarek Behún cmd[0] = OMNIA_CMD_SET_INT_MASK; 750dfa556e4SMarek Behún 751dfa556e4SMarek Behún return omnia_cmd_write(mcu->client, cmd, sizeof(cmd)); 752dfa556e4SMarek Behún } 753dfa556e4SMarek Behún 754dfa556e4SMarek Behún /* 755dfa556e4SMarek Behún * Determine how many bytes we need to read from the reply to the 756dfa556e4SMarek Behún * OMNIA_CMD_GET_INT_AND_CLEAR command in order to retrieve all unmasked 757dfa556e4SMarek Behún * interrupts. 758dfa556e4SMarek Behún */ 759dfa556e4SMarek Behún static unsigned int 760dfa556e4SMarek Behún omnia_irq_compute_pending_length(unsigned long rising, unsigned long falling) 761dfa556e4SMarek Behún { 762dfa556e4SMarek Behún return max(omnia_compute_reply_length(rising, true, 0), 763dfa556e4SMarek Behún omnia_compute_reply_length(falling, true, 1)); 764dfa556e4SMarek Behún } 765dfa556e4SMarek Behún 766dfa556e4SMarek Behún static bool omnia_irq_read_pending_new(struct omnia_mcu *mcu, 767dfa556e4SMarek Behún unsigned long *pending) 768dfa556e4SMarek Behún { 769dfa556e4SMarek Behún struct device *dev = &mcu->client->dev; 770dfa556e4SMarek Behún u8 reply[OMNIA_CMD_INT_ARG_LEN] = {}; 771dfa556e4SMarek Behún unsigned long rising, falling; 772dfa556e4SMarek Behún unsigned int len; 773dfa556e4SMarek Behún int err; 774dfa556e4SMarek Behún 775dfa556e4SMarek Behún len = omnia_irq_compute_pending_length(mcu->rising & mcu->mask, 776dfa556e4SMarek Behún mcu->falling & mcu->mask); 777dfa556e4SMarek Behún if (!len) 778dfa556e4SMarek Behún return false; 779dfa556e4SMarek Behún 780dfa556e4SMarek Behún guard(mutex)(&mcu->lock); 781dfa556e4SMarek Behún 782dfa556e4SMarek Behún err = omnia_cmd_read(mcu->client, OMNIA_CMD_GET_INT_AND_CLEAR, reply, 783dfa556e4SMarek Behún len); 784dfa556e4SMarek Behún if (err) { 785dfa556e4SMarek Behún dev_err(dev, "Cannot read pending IRQs: %d\n", err); 786dfa556e4SMarek Behún return false; 787dfa556e4SMarek Behún } 788dfa556e4SMarek Behún 789dfa556e4SMarek Behún /* deinterleave the reply bytes into rising and falling */ 790dfa556e4SMarek Behún omnia_mask_deinterleave(reply, &rising, &falling); 791dfa556e4SMarek Behún 792dfa556e4SMarek Behún rising &= mcu->mask; 793dfa556e4SMarek Behún falling &= mcu->mask; 794dfa556e4SMarek Behún *pending = rising | falling; 795dfa556e4SMarek Behún 796dfa556e4SMarek Behún /* cache values for GPIOs that have both edges enabled */ 797dfa556e4SMarek Behún mcu->is_cached &= ~(rising & falling); 798dfa556e4SMarek Behún mcu->is_cached |= mcu->both & (rising ^ falling); 799dfa556e4SMarek Behún mcu->cached = (mcu->cached | rising) & ~falling; 800dfa556e4SMarek Behún 801dfa556e4SMarek Behún return true; 802dfa556e4SMarek Behún } 803dfa556e4SMarek Behún 804dfa556e4SMarek Behún static int omnia_read_status_word_old_fw(struct omnia_mcu *mcu, 805dfa556e4SMarek Behún unsigned long *status) 806dfa556e4SMarek Behún { 807dfa556e4SMarek Behún u16 raw_status; 808dfa556e4SMarek Behún int err; 809dfa556e4SMarek Behún 810dfa556e4SMarek Behún err = omnia_cmd_read_u16(mcu->client, OMNIA_CMD_GET_STATUS_WORD, 811dfa556e4SMarek Behún &raw_status); 812dfa556e4SMarek Behún if (err) 813dfa556e4SMarek Behún return err; 814dfa556e4SMarek Behún 815dfa556e4SMarek Behún /* 816dfa556e4SMarek Behún * Old firmware has a bug wherein it never resets the USB port 817dfa556e4SMarek Behún * overcurrent bits back to zero. Ignore them. 818dfa556e4SMarek Behún */ 819dfa556e4SMarek Behún *status = raw_status & ~(OMNIA_STS_USB30_OVC | OMNIA_STS_USB31_OVC); 820dfa556e4SMarek Behún 821dfa556e4SMarek Behún return 0; 822dfa556e4SMarek Behún } 823dfa556e4SMarek Behún 824dfa556e4SMarek Behún static void button_release_emul_fn(struct work_struct *work) 825dfa556e4SMarek Behún { 826dfa556e4SMarek Behún struct omnia_mcu *mcu = container_of(to_delayed_work(work), 827dfa556e4SMarek Behún struct omnia_mcu, 828dfa556e4SMarek Behún button_release_emul_work); 829dfa556e4SMarek Behún 830dfa556e4SMarek Behún mcu->button_pressed_emul = false; 831dfa556e4SMarek Behún generic_handle_irq_safe(mcu->client->irq); 832dfa556e4SMarek Behún } 833dfa556e4SMarek Behún 834dfa556e4SMarek Behún static void 835dfa556e4SMarek Behún fill_int_from_sts(unsigned long *rising, unsigned long *falling, 836dfa556e4SMarek Behún unsigned long rising_sts, unsigned long falling_sts, 837dfa556e4SMarek Behún unsigned long sts_bit, unsigned long int_bit) 838dfa556e4SMarek Behún { 839dfa556e4SMarek Behún if (rising_sts & sts_bit) 840dfa556e4SMarek Behún *rising |= int_bit; 841dfa556e4SMarek Behún if (falling_sts & sts_bit) 842dfa556e4SMarek Behún *falling |= int_bit; 843dfa556e4SMarek Behún } 844dfa556e4SMarek Behún 845dfa556e4SMarek Behún static bool omnia_irq_read_pending_old(struct omnia_mcu *mcu, 846dfa556e4SMarek Behún unsigned long *pending) 847dfa556e4SMarek Behún { 848dfa556e4SMarek Behún unsigned long status, rising_sts, falling_sts, rising, falling; 849dfa556e4SMarek Behún struct device *dev = &mcu->client->dev; 850dfa556e4SMarek Behún int err; 851dfa556e4SMarek Behún 852dfa556e4SMarek Behún guard(mutex)(&mcu->lock); 853dfa556e4SMarek Behún 854dfa556e4SMarek Behún err = omnia_read_status_word_old_fw(mcu, &status); 855dfa556e4SMarek Behún if (err) { 856dfa556e4SMarek Behún dev_err(dev, "Cannot read pending IRQs: %d\n", err); 857dfa556e4SMarek Behún return false; 858dfa556e4SMarek Behún } 859dfa556e4SMarek Behún 860dfa556e4SMarek Behún /* 861dfa556e4SMarek Behún * The old firmware triggers an interrupt whenever status word changes, 862dfa556e4SMarek Behún * but does not inform about which bits rose or fell. We need to compute 863dfa556e4SMarek Behún * this here by comparing with the last status word value. 864dfa556e4SMarek Behún * 865dfa556e4SMarek Behún * The OMNIA_STS_BUTTON_PRESSED bit needs special handling, because the 866dfa556e4SMarek Behún * old firmware clears the OMNIA_STS_BUTTON_PRESSED bit on successful 867dfa556e4SMarek Behún * completion of the OMNIA_CMD_GET_STATUS_WORD command, resulting in 868dfa556e4SMarek Behún * another interrupt: 869dfa556e4SMarek Behún * - first we get an interrupt, we read the status word where 870dfa556e4SMarek Behún * OMNIA_STS_BUTTON_PRESSED is present, 871dfa556e4SMarek Behún * - MCU clears the OMNIA_STS_BUTTON_PRESSED bit because we read the 872dfa556e4SMarek Behún * status word, 873dfa556e4SMarek Behún * - we get another interrupt because the status word changed again 874dfa556e4SMarek Behún * (the OMNIA_STS_BUTTON_PRESSED bit was cleared). 875dfa556e4SMarek Behún * 876dfa556e4SMarek Behún * The gpiolib-cdev, gpiolib-sysfs and gpio-keys input driver all call 877dfa556e4SMarek Behún * the gpiochip's .get() method after an edge event on a requested GPIO 878dfa556e4SMarek Behún * occurs. 879dfa556e4SMarek Behún * 880dfa556e4SMarek Behún * We ensure that the .get() method reads 1 for the button GPIO for some 881dfa556e4SMarek Behún * time. 882dfa556e4SMarek Behún */ 883dfa556e4SMarek Behún 884dfa556e4SMarek Behún if (status & OMNIA_STS_BUTTON_PRESSED) { 885dfa556e4SMarek Behún mcu->button_pressed_emul = true; 886dfa556e4SMarek Behún mod_delayed_work(system_wq, &mcu->button_release_emul_work, 887dfa556e4SMarek Behún msecs_to_jiffies(FRONT_BUTTON_RELEASE_DELAY_MS)); 888dfa556e4SMarek Behún } else if (mcu->button_pressed_emul) { 889dfa556e4SMarek Behún status |= OMNIA_STS_BUTTON_PRESSED; 890dfa556e4SMarek Behún } 891dfa556e4SMarek Behún 892dfa556e4SMarek Behún rising_sts = ~mcu->last_status & status; 893dfa556e4SMarek Behún falling_sts = mcu->last_status & ~status; 894dfa556e4SMarek Behún 895dfa556e4SMarek Behún mcu->last_status = status; 896dfa556e4SMarek Behún 897dfa556e4SMarek Behún /* 898dfa556e4SMarek Behún * Fill in the relevant interrupt bits from status bits for CARD_DET, 899dfa556e4SMarek Behún * MSATA_IND and BUTTON_PRESSED. 900dfa556e4SMarek Behún */ 901dfa556e4SMarek Behún rising = 0; 902dfa556e4SMarek Behún falling = 0; 903dfa556e4SMarek Behún fill_int_from_sts(&rising, &falling, rising_sts, falling_sts, 904dfa556e4SMarek Behún OMNIA_STS_CARD_DET, OMNIA_INT_CARD_DET); 905dfa556e4SMarek Behún fill_int_from_sts(&rising, &falling, rising_sts, falling_sts, 906dfa556e4SMarek Behún OMNIA_STS_MSATA_IND, OMNIA_INT_MSATA_IND); 907dfa556e4SMarek Behún fill_int_from_sts(&rising, &falling, rising_sts, falling_sts, 908dfa556e4SMarek Behún OMNIA_STS_BUTTON_PRESSED, OMNIA_INT_BUTTON_PRESSED); 909dfa556e4SMarek Behún 910dfa556e4SMarek Behún /* Use only bits that are enabled */ 911dfa556e4SMarek Behún rising &= mcu->rising & mcu->mask; 912dfa556e4SMarek Behún falling &= mcu->falling & mcu->mask; 913dfa556e4SMarek Behún *pending = rising | falling; 914dfa556e4SMarek Behún 915dfa556e4SMarek Behún return true; 916dfa556e4SMarek Behún } 917dfa556e4SMarek Behún 918dfa556e4SMarek Behún static bool omnia_irq_read_pending(struct omnia_mcu *mcu, 919dfa556e4SMarek Behún unsigned long *pending) 920dfa556e4SMarek Behún { 921dfa556e4SMarek Behún if (mcu->features & OMNIA_FEAT_NEW_INT_API) 922dfa556e4SMarek Behún return omnia_irq_read_pending_new(mcu, pending); 923dfa556e4SMarek Behún else 924dfa556e4SMarek Behún return omnia_irq_read_pending_old(mcu, pending); 925dfa556e4SMarek Behún } 926dfa556e4SMarek Behún 927dfa556e4SMarek Behún static irqreturn_t omnia_irq_thread_handler(int irq, void *dev_id) 928dfa556e4SMarek Behún { 929dfa556e4SMarek Behún struct omnia_mcu *mcu = dev_id; 930dfa556e4SMarek Behún struct irq_domain *domain; 931dfa556e4SMarek Behún unsigned long pending; 932dfa556e4SMarek Behún unsigned int i; 933dfa556e4SMarek Behún 934dfa556e4SMarek Behún if (!omnia_irq_read_pending(mcu, &pending)) 935dfa556e4SMarek Behún return IRQ_NONE; 936dfa556e4SMarek Behún 937dfa556e4SMarek Behún domain = mcu->gc.irq.domain; 938dfa556e4SMarek Behún 939dfa556e4SMarek Behún for_each_set_bit(i, &pending, 32) { 940dfa556e4SMarek Behún unsigned int nested_irq; 941dfa556e4SMarek Behún 942dfa556e4SMarek Behún nested_irq = irq_find_mapping(domain, omnia_int_to_gpio_idx[i]); 943dfa556e4SMarek Behún 944dfa556e4SMarek Behún handle_nested_irq(nested_irq); 945dfa556e4SMarek Behún } 946dfa556e4SMarek Behún 947dfa556e4SMarek Behún return IRQ_RETVAL(pending); 948dfa556e4SMarek Behún } 949dfa556e4SMarek Behún 950dfa556e4SMarek Behún static const char * const front_button_modes[] = { "mcu", "cpu" }; 951dfa556e4SMarek Behún 952dfa556e4SMarek Behún static ssize_t front_button_mode_show(struct device *dev, 953dfa556e4SMarek Behún struct device_attribute *a, char *buf) 954dfa556e4SMarek Behún { 955dfa556e4SMarek Behún struct omnia_mcu *mcu = dev_get_drvdata(dev); 956dfa556e4SMarek Behún int val; 957dfa556e4SMarek Behún 958dfa556e4SMarek Behún if (mcu->features & OMNIA_FEAT_NEW_INT_API) { 959dfa556e4SMarek Behún val = omnia_cmd_read_bit(mcu->client, OMNIA_CMD_GET_STATUS_WORD, 960dfa556e4SMarek Behún OMNIA_STS_BUTTON_MODE); 961dfa556e4SMarek Behún if (val < 0) 962dfa556e4SMarek Behún return val; 963dfa556e4SMarek Behún } else { 964dfa556e4SMarek Behún val = !!(mcu->last_status & OMNIA_STS_BUTTON_MODE); 965dfa556e4SMarek Behún } 966dfa556e4SMarek Behún 967dfa556e4SMarek Behún return sysfs_emit(buf, "%s\n", front_button_modes[val]); 968dfa556e4SMarek Behún } 969dfa556e4SMarek Behún 970dfa556e4SMarek Behún static ssize_t front_button_mode_store(struct device *dev, 971dfa556e4SMarek Behún struct device_attribute *a, 972dfa556e4SMarek Behún const char *buf, size_t count) 973dfa556e4SMarek Behún { 974dfa556e4SMarek Behún struct omnia_mcu *mcu = dev_get_drvdata(dev); 975dfa556e4SMarek Behún int err, i; 976dfa556e4SMarek Behún 977dfa556e4SMarek Behún i = sysfs_match_string(front_button_modes, buf); 978dfa556e4SMarek Behún if (i < 0) 979dfa556e4SMarek Behún return i; 980dfa556e4SMarek Behún 981dfa556e4SMarek Behún err = omnia_ctl_cmd_locked(mcu, OMNIA_CMD_GENERAL_CONTROL, 982dfa556e4SMarek Behún i ? OMNIA_CTL_BUTTON_MODE : 0, 983dfa556e4SMarek Behún OMNIA_CTL_BUTTON_MODE); 984dfa556e4SMarek Behún if (err) 985dfa556e4SMarek Behún return err; 986dfa556e4SMarek Behún 987dfa556e4SMarek Behún return count; 988dfa556e4SMarek Behún } 989dfa556e4SMarek Behún static DEVICE_ATTR_RW(front_button_mode); 990dfa556e4SMarek Behún 991dfa556e4SMarek Behún static struct attribute *omnia_mcu_gpio_attrs[] = { 992dfa556e4SMarek Behún &dev_attr_front_button_mode.attr, 993dfa556e4SMarek Behún NULL 994dfa556e4SMarek Behún }; 995dfa556e4SMarek Behún 996dfa556e4SMarek Behún const struct attribute_group omnia_mcu_gpio_group = { 997dfa556e4SMarek Behún .attrs = omnia_mcu_gpio_attrs, 998dfa556e4SMarek Behún }; 999dfa556e4SMarek Behún 1000dfa556e4SMarek Behún int omnia_mcu_register_gpiochip(struct omnia_mcu *mcu) 1001dfa556e4SMarek Behún { 1002dfa556e4SMarek Behún bool new_api = mcu->features & OMNIA_FEAT_NEW_INT_API; 1003dfa556e4SMarek Behún struct device *dev = &mcu->client->dev; 1004dfa556e4SMarek Behún unsigned long irqflags; 1005dfa556e4SMarek Behún int err; 1006dfa556e4SMarek Behún 1007dfa556e4SMarek Behún err = devm_mutex_init(dev, &mcu->lock); 1008dfa556e4SMarek Behún if (err) 1009dfa556e4SMarek Behún return err; 1010dfa556e4SMarek Behún 1011dfa556e4SMarek Behún mcu->gc.request = omnia_gpio_request; 1012dfa556e4SMarek Behún mcu->gc.get_direction = omnia_gpio_get_direction; 1013dfa556e4SMarek Behún mcu->gc.direction_input = omnia_gpio_direction_input; 1014dfa556e4SMarek Behún mcu->gc.direction_output = omnia_gpio_direction_output; 1015dfa556e4SMarek Behún mcu->gc.get = omnia_gpio_get; 1016dfa556e4SMarek Behún mcu->gc.get_multiple = omnia_gpio_get_multiple; 1017dfa556e4SMarek Behún mcu->gc.set = omnia_gpio_set; 1018dfa556e4SMarek Behún mcu->gc.set_multiple = omnia_gpio_set_multiple; 1019dfa556e4SMarek Behún mcu->gc.init_valid_mask = omnia_gpio_init_valid_mask; 1020dfa556e4SMarek Behún mcu->gc.can_sleep = true; 1021dfa556e4SMarek Behún mcu->gc.names = omnia_mcu_gpio_templates; 1022dfa556e4SMarek Behún mcu->gc.base = -1; 1023dfa556e4SMarek Behún mcu->gc.ngpio = ARRAY_SIZE(omnia_gpios); 1024dfa556e4SMarek Behún mcu->gc.label = "Turris Omnia MCU GPIOs"; 1025dfa556e4SMarek Behún mcu->gc.parent = dev; 1026dfa556e4SMarek Behún mcu->gc.owner = THIS_MODULE; 1027dfa556e4SMarek Behún mcu->gc.of_gpio_n_cells = 3; 1028dfa556e4SMarek Behún mcu->gc.of_xlate = omnia_gpio_of_xlate; 1029dfa556e4SMarek Behún 1030dfa556e4SMarek Behún gpio_irq_chip_set_chip(&mcu->gc.irq, &omnia_mcu_irq_chip); 1031dfa556e4SMarek Behún /* This will let us handle the parent IRQ in the driver */ 1032dfa556e4SMarek Behún mcu->gc.irq.parent_handler = NULL; 1033dfa556e4SMarek Behún mcu->gc.irq.num_parents = 0; 1034dfa556e4SMarek Behún mcu->gc.irq.parents = NULL; 1035dfa556e4SMarek Behún mcu->gc.irq.default_type = IRQ_TYPE_NONE; 1036dfa556e4SMarek Behún mcu->gc.irq.handler = handle_bad_irq; 1037dfa556e4SMarek Behún mcu->gc.irq.threaded = true; 1038dfa556e4SMarek Behún if (new_api) 1039dfa556e4SMarek Behún mcu->gc.irq.init_hw = omnia_irq_init_hw; 1040dfa556e4SMarek Behún mcu->gc.irq.init_valid_mask = omnia_irq_init_valid_mask; 1041dfa556e4SMarek Behún 1042dfa556e4SMarek Behún err = devm_gpiochip_add_data(dev, &mcu->gc, mcu); 1043dfa556e4SMarek Behún if (err) 1044dfa556e4SMarek Behún return dev_err_probe(dev, err, "Cannot add GPIO chip\n"); 1045dfa556e4SMarek Behún 1046dfa556e4SMarek Behún /* 1047dfa556e4SMarek Behún * Before requesting the interrupt, if firmware does not support the new 1048dfa556e4SMarek Behún * interrupt API, we need to cache the value of the status word, so that 1049dfa556e4SMarek Behún * when it changes, we may compare the new value with the cached one in 1050dfa556e4SMarek Behún * the interrupt handler. 1051dfa556e4SMarek Behún */ 1052dfa556e4SMarek Behún if (!new_api) { 1053dfa556e4SMarek Behún err = omnia_read_status_word_old_fw(mcu, &mcu->last_status); 1054dfa556e4SMarek Behún if (err) 1055dfa556e4SMarek Behún return dev_err_probe(dev, err, 1056dfa556e4SMarek Behún "Cannot read status word\n"); 1057dfa556e4SMarek Behún 1058dfa556e4SMarek Behún INIT_DELAYED_WORK(&mcu->button_release_emul_work, 1059dfa556e4SMarek Behún button_release_emul_fn); 1060dfa556e4SMarek Behún } 1061dfa556e4SMarek Behún 1062dfa556e4SMarek Behún irqflags = IRQF_ONESHOT; 1063dfa556e4SMarek Behún if (new_api) 1064dfa556e4SMarek Behún irqflags |= IRQF_TRIGGER_LOW; 1065dfa556e4SMarek Behún else 1066dfa556e4SMarek Behún irqflags |= IRQF_TRIGGER_FALLING; 1067dfa556e4SMarek Behún 1068dfa556e4SMarek Behún err = devm_request_threaded_irq(dev, mcu->client->irq, NULL, 1069dfa556e4SMarek Behún omnia_irq_thread_handler, irqflags, 1070dfa556e4SMarek Behún "turris-omnia-mcu", mcu); 1071dfa556e4SMarek Behún if (err) 1072dfa556e4SMarek Behún return dev_err_probe(dev, err, "Cannot request IRQ\n"); 1073dfa556e4SMarek Behún 1074dfa556e4SMarek Behún if (!new_api) { 1075dfa556e4SMarek Behún /* 1076dfa556e4SMarek Behún * The button_release_emul_work has to be initialized before the 1077dfa556e4SMarek Behún * thread is requested, and on driver remove it needs to be 1078dfa556e4SMarek Behún * canceled before the thread is freed. Therefore we can't use 1079dfa556e4SMarek Behún * devm_delayed_work_autocancel() directly, because the order 1080dfa556e4SMarek Behún * devm_delayed_work_autocancel(); 1081dfa556e4SMarek Behún * devm_request_threaded_irq(); 1082dfa556e4SMarek Behún * would cause improper release order: 1083dfa556e4SMarek Behún * free_irq(); 1084dfa556e4SMarek Behún * cancel_delayed_work_sync(); 1085dfa556e4SMarek Behún * Instead we first initialize the work above, and only now 1086dfa556e4SMarek Behún * after IRQ is requested we add the work devm action. 1087dfa556e4SMarek Behún */ 1088dfa556e4SMarek Behún err = devm_add_action(dev, devm_delayed_work_drop, 1089dfa556e4SMarek Behún &mcu->button_release_emul_work); 1090dfa556e4SMarek Behún if (err) 1091dfa556e4SMarek Behún return err; 1092dfa556e4SMarek Behún } 1093dfa556e4SMarek Behún 1094dfa556e4SMarek Behún return 0; 1095dfa556e4SMarek Behún } 1096