1 /*
2 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include "wlan_hdd_main.h"
18 #include <linux/gpio.h>
19 #include "wlan_hdd_gpio_wakeup.h"
20
21 static int32_t gpio_wakeup_irq_num = -1;
22
23 static uint32_t
hdd_gpio_wakeup_mode_pmo_to_linux(enum pmo_gpio_wakeup_mode mode)24 hdd_gpio_wakeup_mode_pmo_to_linux(enum pmo_gpio_wakeup_mode mode)
25 {
26 uint32_t irq_flag;
27
28 switch (mode) {
29 case PMO_GPIO_WAKEUP_MODE_RISING:
30 irq_flag = IRQF_TRIGGER_RISING;
31 break;
32 case PMO_GPIO_WAKEUP_MODE_FALLING:
33 irq_flag = IRQF_TRIGGER_FALLING;
34 break;
35 case PMO_GPIO_WAKEUP_MODE_HIGH:
36 irq_flag = IRQF_TRIGGER_HIGH;
37 break;
38 case PMO_GPIO_WAKEUP_MODE_LOW:
39 irq_flag = IRQF_TRIGGER_LOW;
40 break;
41 default:
42 irq_flag = IRQF_TRIGGER_NONE;
43 break;
44 }
45
46 return irq_flag;
47 }
48
hdd_gpio_wakeup_isr(int irq,void * dev)49 static irqreturn_t hdd_gpio_wakeup_isr(int irq, void *dev)
50 {
51 hdd_debug("gpio_wakeup_isr");
52
53 return IRQ_HANDLED;
54 }
55
wlan_hdd_gpio_wakeup_init(struct hdd_context * hdd_ctx)56 int wlan_hdd_gpio_wakeup_init(struct hdd_context *hdd_ctx)
57 {
58 uint32_t gpio_wakeup_pin;
59 enum pmo_gpio_wakeup_mode gpio_wakeup_mode;
60 int32_t ret;
61 uint32_t irq_flag;
62
63 if (!ucfg_pmo_is_gpio_wakeup_enabled(hdd_ctx->psoc)) {
64 hdd_debug("gpio wakeup is not enabled");
65 return 0;
66 }
67
68 gpio_wakeup_pin = ucfg_pmo_get_gpio_wakeup_pin(hdd_ctx->psoc);
69
70 ret = gpio_request(gpio_wakeup_pin, "gpio_wakeup");
71 if (ret) {
72 hdd_err("failed to request gpio%d", gpio_wakeup_pin);
73 return -EIO;
74 }
75
76 ret = gpio_direction_input(gpio_wakeup_pin);
77 if (ret) {
78 hdd_err("failed to set input direction");
79 goto fail_free_gpio;
80 }
81
82 gpio_wakeup_irq_num = gpio_to_irq(gpio_wakeup_pin);
83 if (gpio_wakeup_irq_num < 0) {
84 hdd_err("failed to get irq num");
85 goto fail_free_gpio;
86 }
87
88 gpio_wakeup_mode = ucfg_pmo_get_gpio_wakeup_mode(hdd_ctx->psoc);
89 if (gpio_wakeup_mode == PMO_GPIO_WAKEUP_MODE_INVALID) {
90 hdd_err("failed to get invalid wakeup mode");
91 goto fail_free_gpio;
92 }
93
94 irq_flag = hdd_gpio_wakeup_mode_pmo_to_linux(gpio_wakeup_mode);
95 ret = request_irq(gpio_wakeup_irq_num, hdd_gpio_wakeup_isr, irq_flag,
96 "gpio_wakeup_irq", hdd_ctx);
97 if (ret) {
98 hdd_err("failed to request irq %d", ret);
99 goto fail_free_gpio;
100 }
101
102 ret = enable_irq_wake(gpio_wakeup_irq_num);
103 if (ret) {
104 hdd_err("failed to enable irq wake %d", ret);
105 goto fail_free_irq;
106 }
107
108 hdd_debug("succeed to set gpio wakeup");
109
110 return 0;
111
112 fail_free_irq:
113 free_irq(gpio_wakeup_irq_num, hdd_ctx);
114 gpio_wakeup_irq_num = -1;
115 fail_free_gpio:
116 gpio_free(gpio_wakeup_pin);
117
118 return -EIO;
119 }
120
wlan_hdd_gpio_wakeup_deinit(struct hdd_context * hdd_ctx)121 int wlan_hdd_gpio_wakeup_deinit(struct hdd_context *hdd_ctx)
122 {
123 uint32_t gpio_wakeup_pin;
124
125 if (!ucfg_pmo_is_gpio_wakeup_enabled(hdd_ctx->psoc)) {
126 hdd_debug("gpio wakeup is not enabled");
127 return 0;
128 }
129
130 if (gpio_wakeup_irq_num < 0) {
131 hdd_debug("gpio wakeup irq is not enabled");
132 return 0;
133 }
134
135 free_irq(gpio_wakeup_irq_num, hdd_ctx);
136 gpio_wakeup_irq_num = -1;
137
138 gpio_wakeup_pin = ucfg_pmo_get_gpio_wakeup_pin(hdd_ctx->psoc);
139 gpio_free(gpio_wakeup_pin);
140
141 return 0;
142 }
143