Linux Audio

Check our new training course

Loading...
v6.2
 
 1/*
 2 * GPIO Driver for Loongson 1 SoC
 3 *
 4 * Copyright (C) 2015-2016 Zhang, Keguang <keguang.zhang@gmail.com>
 5 *
 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.
 9 */
10
11#include <linux/module.h>
12#include <linux/gpio/driver.h>
13#include <linux/platform_device.h>
14#include <linux/bitops.h>
15
16/* Loongson 1 GPIO Register Definitions */
17#define GPIO_CFG		0x0
18#define GPIO_DIR		0x10
19#define GPIO_DATA		0x20
20#define GPIO_OUTPUT		0x30
21
22static void __iomem *gpio_reg_base;
 
 
 
23
24static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
25{
 
26	unsigned long flags;
27
28	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
29	__raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | BIT(offset),
30		     gpio_reg_base + GPIO_CFG);
31	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
32
33	return 0;
34}
35
36static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
37{
 
38	unsigned long flags;
39
40	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
41	__raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~BIT(offset),
42		     gpio_reg_base + GPIO_CFG);
43	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
44}
45
46static int ls1x_gpio_probe(struct platform_device *pdev)
47{
48	struct device *dev = &pdev->dev;
49	struct gpio_chip *gc;
50	int ret;
51
52	gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
53	if (!gc)
54		return -ENOMEM;
55
56	gpio_reg_base = devm_platform_ioremap_resource(pdev, 0);
57	if (IS_ERR(gpio_reg_base))
58		return PTR_ERR(gpio_reg_base);
59
60	ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA,
61			 gpio_reg_base + GPIO_OUTPUT, NULL,
62			 NULL, gpio_reg_base + GPIO_DIR, 0);
63	if (ret)
64		goto err;
65
66	gc->owner = THIS_MODULE;
67	gc->request = ls1x_gpio_request;
68	gc->free = ls1x_gpio_free;
69	gc->base = pdev->id * 32;
 
 
 
 
70
71	ret = devm_gpiochip_add_data(dev, gc, NULL);
72	if (ret)
73		goto err;
74
75	platform_set_drvdata(pdev, gc);
76	dev_info(dev, "Loongson1 GPIO driver registered\n");
 
 
77
78	return 0;
79err:
80	dev_err(dev, "failed to register GPIO device\n");
81	return ret;
82}
83
 
 
 
 
 
 
84static struct platform_driver ls1x_gpio_driver = {
85	.probe	= ls1x_gpio_probe,
86	.driver	= {
87		.name	= "ls1x-gpio",
 
88	},
89};
90
91module_platform_driver(ls1x_gpio_driver);
92
93MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
94MODULE_DESCRIPTION("Loongson1 GPIO driver");
95MODULE_LICENSE("GPL");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * GPIO Driver for Loongson 1 SoC
  4 *
  5 * Copyright (C) 2015-2023 Keguang Zhang <keguang.zhang@gmail.com>
 
 
 
 
  6 */
  7
  8#include <linux/module.h>
  9#include <linux/gpio/driver.h>
 10#include <linux/platform_device.h>
 11#include <linux/bitops.h>
 12
 13/* Loongson 1 GPIO Register Definitions */
 14#define GPIO_CFG		0x0
 15#define GPIO_DIR		0x10
 16#define GPIO_DATA		0x20
 17#define GPIO_OUTPUT		0x30
 18
 19struct ls1x_gpio_chip {
 20	struct gpio_chip gc;
 21	void __iomem *reg_base;
 22};
 23
 24static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
 25{
 26	struct ls1x_gpio_chip *ls1x_gc = gpiochip_get_data(gc);
 27	unsigned long flags;
 28
 29	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
 30	__raw_writel(__raw_readl(ls1x_gc->reg_base + GPIO_CFG) | BIT(offset),
 31		     ls1x_gc->reg_base + GPIO_CFG);
 32	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
 33
 34	return 0;
 35}
 36
 37static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
 38{
 39	struct ls1x_gpio_chip *ls1x_gc = gpiochip_get_data(gc);
 40	unsigned long flags;
 41
 42	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
 43	__raw_writel(__raw_readl(ls1x_gc->reg_base + GPIO_CFG) & ~BIT(offset),
 44		     ls1x_gc->reg_base + GPIO_CFG);
 45	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
 46}
 47
 48static int ls1x_gpio_probe(struct platform_device *pdev)
 49{
 50	struct device *dev = &pdev->dev;
 51	struct ls1x_gpio_chip *ls1x_gc;
 52	int ret;
 53
 54	ls1x_gc = devm_kzalloc(dev, sizeof(*ls1x_gc), GFP_KERNEL);
 55	if (!ls1x_gc)
 56		return -ENOMEM;
 57
 58	ls1x_gc->reg_base = devm_platform_ioremap_resource(pdev, 0);
 59	if (IS_ERR(ls1x_gc->reg_base))
 60		return PTR_ERR(ls1x_gc->reg_base);
 61
 62	ret = bgpio_init(&ls1x_gc->gc, dev, 4, ls1x_gc->reg_base + GPIO_DATA,
 63			 ls1x_gc->reg_base + GPIO_OUTPUT, NULL,
 64			 NULL, ls1x_gc->reg_base + GPIO_DIR, 0);
 65	if (ret)
 66		goto err;
 67
 68	ls1x_gc->gc.owner = THIS_MODULE;
 69	ls1x_gc->gc.request = ls1x_gpio_request;
 70	ls1x_gc->gc.free = ls1x_gpio_free;
 71	/*
 72	 * Clear ngpio to let gpiolib get the correct number
 73	 * by reading ngpios property
 74	 */
 75	ls1x_gc->gc.ngpio = 0;
 76
 77	ret = devm_gpiochip_add_data(dev, &ls1x_gc->gc, ls1x_gc);
 78	if (ret)
 79		goto err;
 80
 81	platform_set_drvdata(pdev, ls1x_gc);
 82
 83	dev_info(dev, "GPIO controller registered with %d pins\n",
 84		 ls1x_gc->gc.ngpio);
 85
 86	return 0;
 87err:
 88	dev_err(dev, "failed to register GPIO controller\n");
 89	return ret;
 90}
 91
 92static const struct of_device_id ls1x_gpio_dt_ids[] = {
 93	{ .compatible = "loongson,ls1x-gpio" },
 94	{ /* sentinel */ }
 95};
 96MODULE_DEVICE_TABLE(of, ls1x_gpio_dt_ids);
 97
 98static struct platform_driver ls1x_gpio_driver = {
 99	.probe	= ls1x_gpio_probe,
100	.driver	= {
101		.name	= "ls1x-gpio",
102		.of_match_table = ls1x_gpio_dt_ids,
103	},
104};
105
106module_platform_driver(ls1x_gpio_driver);
107
108MODULE_AUTHOR("Keguang Zhang <keguang.zhang@gmail.com>");
109MODULE_DESCRIPTION("Loongson1 GPIO driver");
110MODULE_LICENSE("GPL");