blob: 173e06758e6c2a46e77442a2e9ad77df17f1b8dc [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +04002/*
3 * 74xx MMIO GPIO driver
4 *
5 * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +04006 */
7
8#include <linux/err.h>
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +04009#include <linux/module.h>
10#include <linux/of_device.h>
Linus Walleij0f4630f2015-12-04 14:02:58 +010011#include <linux/gpio/driver.h>
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +040012#include <linux/platform_device.h>
13
14#define MMIO_74XX_DIR_IN (0 << 8)
15#define MMIO_74XX_DIR_OUT (1 << 8)
16#define MMIO_74XX_BIT_CNT(x) ((x) & 0xff)
17
18struct mmio_74xx_gpio_priv {
Linus Walleij0f4630f2015-12-04 14:02:58 +010019 struct gpio_chip gc;
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +040020 unsigned flags;
21};
22
23static const struct of_device_id mmio_74xx_gpio_ids[] = {
24 {
25 .compatible = "ti,741g125",
26 .data = (const void *)(MMIO_74XX_DIR_IN | 1),
27 },
28 {
29 .compatible = "ti,742g125",
30 .data = (const void *)(MMIO_74XX_DIR_IN | 2),
31 },
32 {
33 .compatible = "ti,74125",
34 .data = (const void *)(MMIO_74XX_DIR_IN | 4),
35 },
36 {
37 .compatible = "ti,74365",
38 .data = (const void *)(MMIO_74XX_DIR_IN | 6),
39 },
40 {
41 .compatible = "ti,74244",
42 .data = (const void *)(MMIO_74XX_DIR_IN | 8),
43 },
44 {
45 .compatible = "ti,741624",
46 .data = (const void *)(MMIO_74XX_DIR_IN | 16),
47 },
48 {
49 .compatible = "ti,741g74",
50 .data = (const void *)(MMIO_74XX_DIR_OUT | 1),
51 },
52 {
53 .compatible = "ti,7474",
54 .data = (const void *)(MMIO_74XX_DIR_OUT | 2),
55 },
56 {
57 .compatible = "ti,74175",
58 .data = (const void *)(MMIO_74XX_DIR_OUT | 4),
59 },
60 {
61 .compatible = "ti,74174",
62 .data = (const void *)(MMIO_74XX_DIR_OUT | 6),
63 },
64 {
65 .compatible = "ti,74273",
66 .data = (const void *)(MMIO_74XX_DIR_OUT | 8),
67 },
68 {
69 .compatible = "ti,7416374",
70 .data = (const void *)(MMIO_74XX_DIR_OUT | 16),
71 },
72 { }
73};
74MODULE_DEVICE_TABLE(of, mmio_74xx_gpio_ids);
75
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +040076static int mmio_74xx_get_direction(struct gpio_chip *gc, unsigned offset)
77{
Linus Walleij0f4630f2015-12-04 14:02:58 +010078 struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +040079
Matti Vaittinene42615e2019-11-06 10:54:12 +020080 if (priv->flags & MMIO_74XX_DIR_OUT)
81 return GPIO_LINE_DIRECTION_OUT;
82
83 return GPIO_LINE_DIRECTION_IN;
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +040084}
85
86static int mmio_74xx_dir_in(struct gpio_chip *gc, unsigned int gpio)
87{
Linus Walleij0f4630f2015-12-04 14:02:58 +010088 struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +040089
90 return (priv->flags & MMIO_74XX_DIR_OUT) ? -ENOTSUPP : 0;
91}
92
93static int mmio_74xx_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
94{
Linus Walleij0f4630f2015-12-04 14:02:58 +010095 struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +040096
97 if (priv->flags & MMIO_74XX_DIR_OUT) {
98 gc->set(gc, gpio, val);
99 return 0;
100 }
101
102 return -ENOTSUPP;
103}
104
105static int mmio_74xx_gpio_probe(struct platform_device *pdev)
106{
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +0400107 struct mmio_74xx_gpio_priv *priv;
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +0400108 void __iomem *dat;
109 int err;
110
111 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
112 if (!priv)
113 return -ENOMEM;
114
Thierry Redinga23ffcf2018-04-30 09:38:08 +0200115 priv->flags = (uintptr_t)of_device_get_match_data(&pdev->dev);
116
Enrico Weigelt, metux IT consult3faf1e62019-03-11 19:54:40 +0100117 dat = devm_platform_ioremap_resource(pdev, 0);
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +0400118 if (IS_ERR(dat))
119 return PTR_ERR(dat);
120
Linus Walleij0f4630f2015-12-04 14:02:58 +0100121 err = bgpio_init(&priv->gc, &pdev->dev,
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +0400122 DIV_ROUND_UP(MMIO_74XX_BIT_CNT(priv->flags), 8),
123 dat, NULL, NULL, NULL, NULL, 0);
124 if (err)
125 return err;
126
Linus Walleij0f4630f2015-12-04 14:02:58 +0100127 priv->gc.direction_input = mmio_74xx_dir_in;
128 priv->gc.direction_output = mmio_74xx_dir_out;
129 priv->gc.get_direction = mmio_74xx_get_direction;
130 priv->gc.ngpio = MMIO_74XX_BIT_CNT(priv->flags);
131 priv->gc.owner = THIS_MODULE;
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +0400132
133 platform_set_drvdata(pdev, priv);
134
Laxman Dewangana718ed22016-02-22 17:43:28 +0530135 return devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +0400136}
137
138static struct platform_driver mmio_74xx_gpio_driver = {
139 .driver = {
140 .name = "74xx-mmio-gpio",
141 .of_match_table = mmio_74xx_gpio_ids,
142 },
143 .probe = mmio_74xx_gpio_probe,
Alexander Shiyanaf0a33e2014-10-03 11:31:57 +0400144};
145module_platform_driver(mmio_74xx_gpio_driver);
146
147MODULE_LICENSE("GPL");
148MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
149MODULE_DESCRIPTION("74xx MMIO GPIO driver");