turris-omnia-mcu-gpio.c 30 KB

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