2 * GPIO Driver for Loongson 1 SoC
4 * Copyright (C) 2015-2016 Zhang, Keguang <keguang.zhang@gmail.com>
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
11 #include <linux/gpio/driver.h>
12 #include <linux/platform_device.h>
14 /* Loongson 1 GPIO Register Definitions */
17 #define GPIO_DATA 0x20
18 #define GPIO_OUTPUT 0x30
20 static void __iomem
*gpio_reg_base
;
22 static int ls1x_gpio_request(struct gpio_chip
*gc
, unsigned int offset
)
24 unsigned long pinmask
= gc
->pin2mask(gc
, offset
);
27 spin_lock_irqsave(&gc
->bgpio_lock
, flags
);
28 __raw_writel(__raw_readl(gpio_reg_base
+ GPIO_CFG
) | pinmask
,
29 gpio_reg_base
+ GPIO_CFG
);
30 spin_unlock_irqrestore(&gc
->bgpio_lock
, flags
);
35 static void ls1x_gpio_free(struct gpio_chip
*gc
, unsigned int offset
)
37 unsigned long pinmask
= gc
->pin2mask(gc
, offset
);
40 spin_lock_irqsave(&gc
->bgpio_lock
, flags
);
41 __raw_writel(__raw_readl(gpio_reg_base
+ GPIO_CFG
) & ~pinmask
,
42 gpio_reg_base
+ GPIO_CFG
);
43 spin_unlock_irqrestore(&gc
->bgpio_lock
, flags
);
46 static int ls1x_gpio_probe(struct platform_device
*pdev
)
48 struct device
*dev
= &pdev
->dev
;
53 gc
= devm_kzalloc(dev
, sizeof(*gc
), GFP_KERNEL
);
57 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
59 dev_err(dev
, "failed to get I/O memory\n");
63 gpio_reg_base
= devm_ioremap_resource(dev
, res
);
64 if (IS_ERR(gpio_reg_base
))
65 return PTR_ERR(gpio_reg_base
);
67 ret
= bgpio_init(gc
, dev
, 4, gpio_reg_base
+ GPIO_DATA
,
68 gpio_reg_base
+ GPIO_OUTPUT
, NULL
,
69 NULL
, gpio_reg_base
+ GPIO_DIR
, 0);
73 gc
->owner
= THIS_MODULE
;
74 gc
->request
= ls1x_gpio_request
;
75 gc
->free
= ls1x_gpio_free
;
76 gc
->base
= pdev
->id
* 32;
78 ret
= devm_gpiochip_add_data(dev
, gc
, NULL
);
82 platform_set_drvdata(pdev
, gc
);
83 dev_info(dev
, "Loongson1 GPIO driver registered\n");
87 dev_err(dev
, "failed to register GPIO device\n");
91 static struct platform_driver ls1x_gpio_driver
= {
92 .probe
= ls1x_gpio_probe
,
98 module_platform_driver(ls1x_gpio_driver
);
100 MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
101 MODULE_DESCRIPTION("Loongson1 GPIO driver");
102 MODULE_LICENSE("GPL");