ramips: add common gpio-buttons device
[lede.git] / target / linux / ramips / files / arch / mips / ralink / common / dev-gpio-buttons.c
1 /*
2  *  Ralink SoC GPIO button support
3  *
4  *  Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2 as published
8  *  by the Free Software Foundation.
9  */
10
11 #include "linux/init.h"
12 #include <linux/platform_device.h>
13
14 #include <asm/mach-ralink/dev-gpio-buttons.h>
15
16 void __init ramips_register_gpio_buttons(int id,
17                                          unsigned poll_interval,
18                                          unsigned nbuttons,
19                                          struct gpio_button *buttons)
20 {
21         struct platform_device *pdev;
22         struct gpio_buttons_platform_data pdata;
23         struct gpio_button *p;
24         int err;
25
26         p = kmalloc(nbuttons * sizeof(*p), GFP_KERNEL);
27         if (!p)
28                 return;
29
30         memcpy(p, buttons, nbuttons * sizeof(*p));
31
32         pdev = platform_device_alloc("gpio-buttons", id);
33         if (!pdev)
34                 goto err_free_buttons;
35
36         memset(&pdata, 0, sizeof(pdata));
37         pdata.poll_interval = poll_interval;
38         pdata.nbuttons = nbuttons;
39         pdata.buttons = p;
40
41         err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
42         if (err)
43                 goto err_put_pdev;
44
45
46         err = platform_device_add(pdev);
47         if (err)
48                 goto err_put_pdev;
49
50         return;
51
52 err_put_pdev:
53         platform_device_put(pdev);
54
55 err_free_buttons:
56         kfree(p);
57 }