]>
Commit | Line | Data |
---|---|---|
af0a33e2 AS |
1 | /* |
2 | * 74xx MMIO GPIO driver | |
3 | * | |
4 | * Copyright (C) 2014 Alexander Shiyan <[email protected]> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/err.h> | |
af0a33e2 AS |
13 | #include <linux/module.h> |
14 | #include <linux/of_device.h> | |
0f4630f3 | 15 | #include <linux/gpio/driver.h> |
af0a33e2 AS |
16 | #include <linux/platform_device.h> |
17 | ||
18 | #define MMIO_74XX_DIR_IN (0 << 8) | |
19 | #define MMIO_74XX_DIR_OUT (1 << 8) | |
20 | #define MMIO_74XX_BIT_CNT(x) ((x) & 0xff) | |
21 | ||
22 | struct mmio_74xx_gpio_priv { | |
0f4630f3 | 23 | struct gpio_chip gc; |
af0a33e2 AS |
24 | unsigned flags; |
25 | }; | |
26 | ||
27 | static const struct of_device_id mmio_74xx_gpio_ids[] = { | |
28 | { | |
29 | .compatible = "ti,741g125", | |
30 | .data = (const void *)(MMIO_74XX_DIR_IN | 1), | |
31 | }, | |
32 | { | |
33 | .compatible = "ti,742g125", | |
34 | .data = (const void *)(MMIO_74XX_DIR_IN | 2), | |
35 | }, | |
36 | { | |
37 | .compatible = "ti,74125", | |
38 | .data = (const void *)(MMIO_74XX_DIR_IN | 4), | |
39 | }, | |
40 | { | |
41 | .compatible = "ti,74365", | |
42 | .data = (const void *)(MMIO_74XX_DIR_IN | 6), | |
43 | }, | |
44 | { | |
45 | .compatible = "ti,74244", | |
46 | .data = (const void *)(MMIO_74XX_DIR_IN | 8), | |
47 | }, | |
48 | { | |
49 | .compatible = "ti,741624", | |
50 | .data = (const void *)(MMIO_74XX_DIR_IN | 16), | |
51 | }, | |
52 | { | |
53 | .compatible = "ti,741g74", | |
54 | .data = (const void *)(MMIO_74XX_DIR_OUT | 1), | |
55 | }, | |
56 | { | |
57 | .compatible = "ti,7474", | |
58 | .data = (const void *)(MMIO_74XX_DIR_OUT | 2), | |
59 | }, | |
60 | { | |
61 | .compatible = "ti,74175", | |
62 | .data = (const void *)(MMIO_74XX_DIR_OUT | 4), | |
63 | }, | |
64 | { | |
65 | .compatible = "ti,74174", | |
66 | .data = (const void *)(MMIO_74XX_DIR_OUT | 6), | |
67 | }, | |
68 | { | |
69 | .compatible = "ti,74273", | |
70 | .data = (const void *)(MMIO_74XX_DIR_OUT | 8), | |
71 | }, | |
72 | { | |
73 | .compatible = "ti,7416374", | |
74 | .data = (const void *)(MMIO_74XX_DIR_OUT | 16), | |
75 | }, | |
76 | { } | |
77 | }; | |
78 | MODULE_DEVICE_TABLE(of, mmio_74xx_gpio_ids); | |
79 | ||
af0a33e2 AS |
80 | static int mmio_74xx_get_direction(struct gpio_chip *gc, unsigned offset) |
81 | { | |
0f4630f3 | 82 | struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc); |
af0a33e2 | 83 | |
0f4630f3 | 84 | return !(priv->flags & MMIO_74XX_DIR_OUT); |
af0a33e2 AS |
85 | } |
86 | ||
87 | static int mmio_74xx_dir_in(struct gpio_chip *gc, unsigned int gpio) | |
88 | { | |
0f4630f3 | 89 | struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc); |
af0a33e2 AS |
90 | |
91 | return (priv->flags & MMIO_74XX_DIR_OUT) ? -ENOTSUPP : 0; | |
92 | } | |
93 | ||
94 | static int mmio_74xx_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) | |
95 | { | |
0f4630f3 | 96 | struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc); |
af0a33e2 AS |
97 | |
98 | if (priv->flags & MMIO_74XX_DIR_OUT) { | |
99 | gc->set(gc, gpio, val); | |
100 | return 0; | |
101 | } | |
102 | ||
103 | return -ENOTSUPP; | |
104 | } | |
105 | ||
106 | static int mmio_74xx_gpio_probe(struct platform_device *pdev) | |
107 | { | |
af0a33e2 AS |
108 | struct mmio_74xx_gpio_priv *priv; |
109 | struct resource *res; | |
110 | void __iomem *dat; | |
111 | int err; | |
112 | ||
113 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); | |
114 | if (!priv) | |
115 | return -ENOMEM; | |
116 | ||
a23ffcf2 TR |
117 | priv->flags = (uintptr_t)of_device_get_match_data(&pdev->dev); |
118 | ||
af0a33e2 AS |
119 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
120 | dat = devm_ioremap_resource(&pdev->dev, res); | |
121 | if (IS_ERR(dat)) | |
122 | return PTR_ERR(dat); | |
123 | ||
0f4630f3 | 124 | err = bgpio_init(&priv->gc, &pdev->dev, |
af0a33e2 AS |
125 | DIV_ROUND_UP(MMIO_74XX_BIT_CNT(priv->flags), 8), |
126 | dat, NULL, NULL, NULL, NULL, 0); | |
127 | if (err) | |
128 | return err; | |
129 | ||
0f4630f3 LW |
130 | priv->gc.direction_input = mmio_74xx_dir_in; |
131 | priv->gc.direction_output = mmio_74xx_dir_out; | |
132 | priv->gc.get_direction = mmio_74xx_get_direction; | |
133 | priv->gc.ngpio = MMIO_74XX_BIT_CNT(priv->flags); | |
134 | priv->gc.owner = THIS_MODULE; | |
af0a33e2 AS |
135 | |
136 | platform_set_drvdata(pdev, priv); | |
137 | ||
a718ed2c | 138 | return devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv); |
af0a33e2 AS |
139 | } |
140 | ||
141 | static struct platform_driver mmio_74xx_gpio_driver = { | |
142 | .driver = { | |
143 | .name = "74xx-mmio-gpio", | |
144 | .of_match_table = mmio_74xx_gpio_ids, | |
145 | }, | |
146 | .probe = mmio_74xx_gpio_probe, | |
af0a33e2 AS |
147 | }; |
148 | module_platform_driver(mmio_74xx_gpio_driver); | |
149 | ||
150 | MODULE_LICENSE("GPL"); | |
151 | MODULE_AUTHOR("Alexander Shiyan <[email protected]>"); | |
152 | MODULE_DESCRIPTION("74xx MMIO GPIO driver"); |