Skip to content

Commit 4020c77

Browse files
marcanjannau
authored andcommitted
gpio: macsmc: Add IRQ support
Signed-off-by: Hector Martin <marcan@marcan.st>
1 parent b9a8545 commit 4020c77

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

drivers/gpio/gpio-macsmc.c

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/bitmap.h>
1111
#include <linux/device.h>
1212
#include <linux/gpio/driver.h>
13+
#include <linux/irq.h>
1314
#include <linux/mfd/core.h>
1415
#include <linux/mfd/macsmc.h>
1516

@@ -68,10 +69,21 @@
6869
* 3 = ?
6970
*/
7071

72+
#define SMC_EV_GPIO 0x7202
73+
7174
struct macsmc_gpio {
7275
struct device *dev;
7376
struct apple_smc *smc;
7477
struct gpio_chip gc;
78+
struct irq_chip ic;
79+
struct notifier_block nb;
80+
81+
struct mutex irq_mutex;
82+
DECLARE_BITMAP(irq_supported, MAX_GPIO);
83+
DECLARE_BITMAP(irq_enable_shadow, MAX_GPIO);
84+
DECLARE_BITMAP(irq_enable, MAX_GPIO);
85+
u32 irq_mode_shadow[MAX_GPIO];
86+
u32 irq_mode[MAX_GPIO];
7587

7688
int first_index;
7789
};
@@ -161,6 +173,7 @@ static int macsmc_gpio_init_valid_mask(struct gpio_chip *gc,
161173
for (i = 0; i < count; i++) {
162174
smc_key key;
163175
int gpio_nr;
176+
u32 val;
164177
int ret = apple_smc_get_key_by_index(smcgp->smc, smcgp->first_index + i, &key);
165178

166179
if (ret < 0)
@@ -176,11 +189,127 @@ static int macsmc_gpio_init_valid_mask(struct gpio_chip *gc,
176189
}
177190

178191
set_bit(gpio_nr, valid_mask);
192+
193+
/* Check for IRQ support */
194+
ret = apple_smc_rw_u32(smcgp->smc, key, CMD_IRQ_MODE, &val);
195+
if (!ret)
196+
set_bit(gpio_nr, smcgp->irq_supported);
197+
}
198+
199+
return 0;
200+
}
201+
202+
static int macsmc_gpio_event(struct notifier_block *nb, unsigned long event, void *data)
203+
{
204+
struct macsmc_gpio *smcgp = container_of(nb, struct macsmc_gpio, nb);
205+
u16 type = event >> 16;
206+
u8 offset = (event >> 8) & 0xff;
207+
smc_key key = macsmc_gpio_key(offset);
208+
unsigned long flags;
209+
210+
if (type != SMC_EV_GPIO)
211+
return NOTIFY_DONE;
212+
213+
if (offset > MAX_GPIO) {
214+
dev_err(smcgp->dev, "GPIO event index %d out of range\n", offset);
215+
return NOTIFY_BAD;
216+
}
217+
218+
local_irq_save(flags);
219+
generic_handle_irq_desc(irq_resolve_mapping(smcgp->gc.irq.domain, offset));
220+
local_irq_restore(flags);
221+
222+
if (apple_smc_write_u32(smcgp->smc, key, CMD_IRQ_ACK | 1) < 0)
223+
dev_err(smcgp->dev, "GPIO IRQ ack failed for %p4ch\n", &key);
224+
225+
return NOTIFY_OK;
226+
}
227+
228+
static void macsmc_gpio_irq_enable(struct irq_data *d)
229+
{
230+
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
231+
struct macsmc_gpio *smcgp = gpiochip_get_data(gc);
232+
233+
set_bit(irqd_to_hwirq(d), smcgp->irq_enable_shadow);
234+
}
235+
236+
static void macsmc_gpio_irq_disable(struct irq_data *d)
237+
{
238+
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
239+
struct macsmc_gpio *smcgp = gpiochip_get_data(gc);
240+
241+
clear_bit(irqd_to_hwirq(d), smcgp->irq_enable_shadow);
242+
}
243+
244+
static int macsmc_gpio_irq_set_type(struct irq_data *d, unsigned int type)
245+
{
246+
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
247+
struct macsmc_gpio *smcgp = gpiochip_get_data(gc);
248+
int offset = irqd_to_hwirq(d);
249+
u32 mode;
250+
251+
if (!test_bit(offset, smcgp->irq_supported))
252+
return -EINVAL;
253+
254+
switch (type & IRQ_TYPE_SENSE_MASK) {
255+
case IRQ_TYPE_LEVEL_HIGH:
256+
mode = IRQ_MODE_HIGH;
257+
break;
258+
case IRQ_TYPE_LEVEL_LOW:
259+
mode = IRQ_MODE_LOW;
260+
break;
261+
case IRQ_TYPE_EDGE_RISING:
262+
mode = IRQ_MODE_RISING;
263+
break;
264+
case IRQ_TYPE_EDGE_FALLING:
265+
mode = IRQ_MODE_FALLING;
266+
break;
267+
case IRQ_TYPE_EDGE_BOTH:
268+
mode = IRQ_MODE_BOTH;
269+
break;
270+
default:
271+
return -EINVAL;
179272
}
180273

274+
smcgp->irq_mode_shadow[offset] = mode;
181275
return 0;
182276
}
183277

278+
static void macsmc_gpio_irq_bus_lock(struct irq_data *d)
279+
{
280+
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
281+
struct macsmc_gpio *smcgp = gpiochip_get_data(gc);
282+
283+
mutex_lock(&smcgp->irq_mutex);
284+
}
285+
286+
static void macsmc_gpio_irq_bus_sync_unlock(struct irq_data *d)
287+
{
288+
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
289+
struct macsmc_gpio *smcgp = gpiochip_get_data(gc);
290+
smc_key key = macsmc_gpio_key(irqd_to_hwirq(d));
291+
int offset = irqd_to_hwirq(d);
292+
bool val;
293+
294+
if (smcgp->irq_mode_shadow[offset] != smcgp->irq_mode[offset]) {
295+
u32 cmd = CMD_IRQ_MODE | smcgp->irq_mode_shadow[offset];
296+
if (apple_smc_write_u32(smcgp->smc, key, cmd) < 0)
297+
dev_err(smcgp->dev, "GPIO IRQ config failed for %p4ch = 0x%x\n", &key, cmd);
298+
else
299+
smcgp->irq_mode_shadow[offset] = smcgp->irq_mode[offset];
300+
}
301+
302+
val = test_bit(offset, smcgp->irq_enable_shadow);
303+
if (test_bit(offset, smcgp->irq_enable) != val) {
304+
if (apple_smc_write_u32(smcgp->smc, key, CMD_IRQ_ENABLE | val) < 0)
305+
dev_err(smcgp->dev, "GPIO IRQ en/disable failed for %p4ch\n", &key);
306+
else
307+
change_bit(offset, smcgp->irq_enable);
308+
}
309+
310+
mutex_unlock(&smcgp->irq_mutex);
311+
}
312+
184313
static int macsmc_gpio_probe(struct platform_device *pdev)
185314
{
186315
struct macsmc_gpio *smcgp;
@@ -221,6 +350,27 @@ static int macsmc_gpio_probe(struct platform_device *pdev)
221350
smcgp->gc.base = -1;
222351
smcgp->gc.parent = &pdev->dev;
223352

353+
smcgp->ic.name = "macsmc-pmu-gpio";
354+
smcgp->ic.irq_mask = macsmc_gpio_irq_disable;
355+
smcgp->ic.irq_unmask = macsmc_gpio_irq_enable;
356+
smcgp->ic.irq_set_type = macsmc_gpio_irq_set_type;
357+
smcgp->ic.irq_bus_lock = macsmc_gpio_irq_bus_lock;
358+
smcgp->ic.irq_bus_sync_unlock = macsmc_gpio_irq_bus_sync_unlock;
359+
smcgp->ic.irq_set_type = macsmc_gpio_irq_set_type;
360+
smcgp->ic.flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
361+
362+
smcgp->gc.irq.chip = &smcgp->ic;
363+
smcgp->gc.irq.parent_handler = NULL;
364+
smcgp->gc.irq.num_parents = 0;
365+
smcgp->gc.irq.parents = NULL;
366+
smcgp->gc.irq.default_type = IRQ_TYPE_NONE;
367+
smcgp->gc.irq.handler = handle_simple_irq;
368+
369+
mutex_init(&smcgp->irq_mutex);
370+
371+
smcgp->nb.notifier_call = macsmc_gpio_event;
372+
apple_smc_register_notifier(smc, &smcgp->nb);
373+
224374
return devm_gpiochip_add_data(&pdev->dev, &smcgp->gc, smcgp);
225375
}
226376

0 commit comments

Comments
 (0)