blob: 6af51feda06f9ca84fcfc297bf01d33ca4b9e0ea [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Thor Thayer26a48c42016-06-02 12:52:24 -05002/*
3 * Copyright Intel Corporation (C) 2014-2016. All Rights Reserved
4 *
Thor Thayer26a48c42016-06-02 12:52:24 -05005 * GPIO driver for Altera Arria10 MAX5 System Resource Chip
6 *
7 * Adapted from gpio-tps65910.c
8 */
9
10#include <linux/gpio/driver.h>
11#include <linux/mfd/altera-a10sr.h>
12#include <linux/module.h>
13
14/**
15 * struct altr_a10sr_gpio - Altera Max5 GPIO device private data structure
16 * @gp: : instance of the gpio_chip
17 * @regmap: the regmap from the parent device.
18 */
19struct altr_a10sr_gpio {
20 struct gpio_chip gp;
21 struct regmap *regmap;
22};
23
24static int altr_a10sr_gpio_get(struct gpio_chip *chip, unsigned int offset)
25{
26 struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
27 int ret, val;
28
29 ret = regmap_read(gpio->regmap, ALTR_A10SR_PBDSW_REG, &val);
30 if (ret < 0)
31 return ret;
32
33 return !!(val & BIT(offset - ALTR_A10SR_LED_VALID_SHIFT));
34}
35
36static void altr_a10sr_gpio_set(struct gpio_chip *chip, unsigned int offset,
37 int value)
38{
39 struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
40
41 regmap_update_bits(gpio->regmap, ALTR_A10SR_LED_REG,
42 BIT(ALTR_A10SR_LED_VALID_SHIFT + offset),
43 value ? BIT(ALTR_A10SR_LED_VALID_SHIFT + offset)
44 : 0);
45}
46
47static int altr_a10sr_gpio_direction_input(struct gpio_chip *gc,
48 unsigned int nr)
49{
Axel Lin68b75872019-01-23 08:00:58 +080050 if (nr < (ALTR_A10SR_IN_VALID_RANGE_LO - ALTR_A10SR_LED_VALID_SHIFT))
51 return -EINVAL;
52
53 return 0;
Thor Thayer26a48c42016-06-02 12:52:24 -050054}
55
56static int altr_a10sr_gpio_direction_output(struct gpio_chip *gc,
57 unsigned int nr, int value)
58{
Axel Lin68b75872019-01-23 08:00:58 +080059 if (nr > (ALTR_A10SR_OUT_VALID_RANGE_HI - ALTR_A10SR_LED_VALID_SHIFT))
60 return -EINVAL;
61
62 altr_a10sr_gpio_set(gc, nr, value);
63 return 0;
Thor Thayer26a48c42016-06-02 12:52:24 -050064}
65
Gustavo A. R. Silva1a20c3f82017-07-11 17:15:38 -050066static const struct gpio_chip altr_a10sr_gc = {
Thor Thayer26a48c42016-06-02 12:52:24 -050067 .label = "altr_a10sr_gpio",
68 .owner = THIS_MODULE,
69 .get = altr_a10sr_gpio_get,
70 .set = altr_a10sr_gpio_set,
71 .direction_input = altr_a10sr_gpio_direction_input,
72 .direction_output = altr_a10sr_gpio_direction_output,
73 .can_sleep = true,
74 .ngpio = 12,
75 .base = -1,
76};
77
78static int altr_a10sr_gpio_probe(struct platform_device *pdev)
79{
80 struct altr_a10sr_gpio *gpio;
Thor Thayer26a48c42016-06-02 12:52:24 -050081 struct altr_a10sr *a10sr = dev_get_drvdata(pdev->dev.parent);
82
83 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
84 if (!gpio)
85 return -ENOMEM;
86
87 gpio->regmap = a10sr->regmap;
88
89 gpio->gp = altr_a10sr_gc;
Thor Thayer9ce9f792017-02-13 13:49:58 -060090 gpio->gp.parent = pdev->dev.parent;
Thor Thayer26a48c42016-06-02 12:52:24 -050091 gpio->gp.of_node = pdev->dev.of_node;
92
Alexandru Ardeleanaa93b0f2021-05-14 12:20:17 +030093 return devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
Thor Thayer26a48c42016-06-02 12:52:24 -050094}
95
Thor Thayer26a48c42016-06-02 12:52:24 -050096static const struct of_device_id altr_a10sr_gpio_of_match[] = {
97 { .compatible = "altr,a10sr-gpio" },
98 { },
99};
100MODULE_DEVICE_TABLE(of, altr_a10sr_gpio_of_match);
101
102static struct platform_driver altr_a10sr_gpio_driver = {
103 .probe = altr_a10sr_gpio_probe,
Thor Thayer26a48c42016-06-02 12:52:24 -0500104 .driver = {
105 .name = "altr_a10sr_gpio",
106 .of_match_table = of_match_ptr(altr_a10sr_gpio_of_match),
107 },
108};
109module_platform_driver(altr_a10sr_gpio_driver);
110
111MODULE_LICENSE("GPL v2");
112MODULE_AUTHOR("Thor Thayer <tthayer@opensource.altera.com>");
113MODULE_DESCRIPTION("Altera Arria10 System Resource Chip GPIO");