Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * GPIO Driver for Loongson 1 SoC
0003  *
0004  * Copyright (C) 2015-2016 Zhang, Keguang <keguang.zhang@gmail.com>
0005  *
0006  * This file is licensed under the terms of the GNU General Public
0007  * License version 2. This program is licensed "as is" without any
0008  * warranty of any kind, whether express or implied.
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/gpio/driver.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/bitops.h>
0015 
0016 /* Loongson 1 GPIO Register Definitions */
0017 #define GPIO_CFG        0x0
0018 #define GPIO_DIR        0x10
0019 #define GPIO_DATA       0x20
0020 #define GPIO_OUTPUT     0x30
0021 
0022 static void __iomem *gpio_reg_base;
0023 
0024 static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
0025 {
0026     unsigned long flags;
0027 
0028     raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
0029     __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | BIT(offset),
0030              gpio_reg_base + GPIO_CFG);
0031     raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
0032 
0033     return 0;
0034 }
0035 
0036 static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
0037 {
0038     unsigned long flags;
0039 
0040     raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
0041     __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~BIT(offset),
0042              gpio_reg_base + GPIO_CFG);
0043     raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
0044 }
0045 
0046 static int ls1x_gpio_probe(struct platform_device *pdev)
0047 {
0048     struct device *dev = &pdev->dev;
0049     struct gpio_chip *gc;
0050     int ret;
0051 
0052     gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
0053     if (!gc)
0054         return -ENOMEM;
0055 
0056     gpio_reg_base = devm_platform_ioremap_resource(pdev, 0);
0057     if (IS_ERR(gpio_reg_base))
0058         return PTR_ERR(gpio_reg_base);
0059 
0060     ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA,
0061              gpio_reg_base + GPIO_OUTPUT, NULL,
0062              NULL, gpio_reg_base + GPIO_DIR, 0);
0063     if (ret)
0064         goto err;
0065 
0066     gc->owner = THIS_MODULE;
0067     gc->request = ls1x_gpio_request;
0068     gc->free = ls1x_gpio_free;
0069     gc->base = pdev->id * 32;
0070 
0071     ret = devm_gpiochip_add_data(dev, gc, NULL);
0072     if (ret)
0073         goto err;
0074 
0075     platform_set_drvdata(pdev, gc);
0076     dev_info(dev, "Loongson1 GPIO driver registered\n");
0077 
0078     return 0;
0079 err:
0080     dev_err(dev, "failed to register GPIO device\n");
0081     return ret;
0082 }
0083 
0084 static struct platform_driver ls1x_gpio_driver = {
0085     .probe  = ls1x_gpio_probe,
0086     .driver = {
0087         .name   = "ls1x-gpio",
0088     },
0089 };
0090 
0091 module_platform_driver(ls1x_gpio_driver);
0092 
0093 MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
0094 MODULE_DESCRIPTION("Loongson1 GPIO driver");
0095 MODULE_LICENSE("GPL");