GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / gpio / gpio-ts4900.c
1 /*
2  * Digital I/O driver for Technologic Systems I2C FPGA Core
3  *
4  * Copyright (C) 2015, 2018 Technologic Systems
5  * Copyright (C) 2016 Savoir-Faire Linux
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12  * kind, whether expressed or implied; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License version 2 for more details.
15  */
16
17 #include <linux/gpio/driver.h>
18 #include <linux/i2c.h>
19 #include <linux/of_device.h>
20 #include <linux/module.h>
21 #include <linux/regmap.h>
22
23 #define DEFAULT_PIN_NUMBER      32
24 /*
25  * Register bits used by the GPIO device
26  * Some boards, such as TS-7970 do not have a separate input bit
27  */
28 #define TS4900_GPIO_OE          0x01
29 #define TS4900_GPIO_OUT         0x02
30 #define TS4900_GPIO_IN          0x04
31 #define TS7970_GPIO_IN          0x02
32
33 struct ts4900_gpio_priv {
34         struct regmap *regmap;
35         struct gpio_chip gpio_chip;
36         unsigned int input_bit;
37 };
38
39 static int ts4900_gpio_get_direction(struct gpio_chip *chip,
40                                      unsigned int offset)
41 {
42         struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
43         unsigned int reg;
44
45         regmap_read(priv->regmap, offset, &reg);
46
47         return !(reg & TS4900_GPIO_OE);
48 }
49
50 static int ts4900_gpio_direction_input(struct gpio_chip *chip,
51                                        unsigned int offset)
52 {
53         struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
54
55         /* Only clear the OE bit here, requires a RMW. Prevents potential issue
56          * with OE and data getting to the physical pin at different times.
57          */
58         return regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OE, 0);
59 }
60
61 static int ts4900_gpio_direction_output(struct gpio_chip *chip,
62                                         unsigned int offset, int value)
63 {
64         struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
65         unsigned int reg;
66         int ret;
67
68         /* If changing from an input to an output, we need to first set the
69          * proper data bit to what is requested and then set OE bit. This
70          * prevents a glitch that can occur on the IO line
71          */
72         regmap_read(priv->regmap, offset, &reg);
73         if (!(reg & TS4900_GPIO_OE)) {
74                 if (value)
75                         reg = TS4900_GPIO_OUT;
76                 else
77                         reg &= ~TS4900_GPIO_OUT;
78
79                 regmap_write(priv->regmap, offset, reg);
80         }
81
82         if (value)
83                 ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE |
84                                                          TS4900_GPIO_OUT);
85         else
86                 ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE);
87
88         return ret;
89 }
90
91 static int ts4900_gpio_get(struct gpio_chip *chip, unsigned int offset)
92 {
93         struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
94         unsigned int reg;
95
96         regmap_read(priv->regmap, offset, &reg);
97
98         return !!(reg & priv->input_bit);
99 }
100
101 static void ts4900_gpio_set(struct gpio_chip *chip, unsigned int offset,
102                             int value)
103 {
104         struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
105
106         if (value)
107                 regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT,
108                                    TS4900_GPIO_OUT);
109         else
110                 regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT, 0);
111 }
112
113 static const struct regmap_config ts4900_regmap_config = {
114         .reg_bits = 16,
115         .val_bits = 8,
116 };
117
118 static const struct gpio_chip template_chip = {
119         .label                  = "ts4900-gpio",
120         .owner                  = THIS_MODULE,
121         .get_direction          = ts4900_gpio_get_direction,
122         .direction_input        = ts4900_gpio_direction_input,
123         .direction_output       = ts4900_gpio_direction_output,
124         .get                    = ts4900_gpio_get,
125         .set                    = ts4900_gpio_set,
126         .base                   = -1,
127         .can_sleep              = true,
128 };
129
130 static const struct of_device_id ts4900_gpio_of_match_table[] = {
131         {
132                 .compatible = "technologic,ts4900-gpio",
133                 .data = (void *)TS4900_GPIO_IN,
134         }, {
135                 .compatible = "technologic,ts7970-gpio",
136                 .data = (void *)TS7970_GPIO_IN,
137         },
138         { /* sentinel */ },
139 };
140 MODULE_DEVICE_TABLE(of, ts4900_gpio_of_match_table);
141
142 static int ts4900_gpio_probe(struct i2c_client *client,
143                         const struct i2c_device_id *id)
144 {
145         const struct of_device_id *match;
146         struct ts4900_gpio_priv *priv;
147         u32 ngpio;
148         int ret;
149
150         match = of_match_device(ts4900_gpio_of_match_table, &client->dev);
151         if (!match)
152                 return -EINVAL;
153
154         if (of_property_read_u32(client->dev.of_node, "ngpios", &ngpio))
155                 ngpio = DEFAULT_PIN_NUMBER;
156
157         priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
158         if (!priv)
159                 return -ENOMEM;
160
161         priv->gpio_chip = template_chip;
162         priv->gpio_chip.label = "ts4900-gpio";
163         priv->gpio_chip.ngpio = ngpio;
164         priv->gpio_chip.parent = &client->dev;
165         priv->input_bit = (uintptr_t)match->data;
166
167         priv->regmap = devm_regmap_init_i2c(client, &ts4900_regmap_config);
168         if (IS_ERR(priv->regmap)) {
169                 ret = PTR_ERR(priv->regmap);
170                 dev_err(&client->dev, "Failed to allocate register map: %d\n",
171                         ret);
172                 return ret;
173         }
174
175         ret = devm_gpiochip_add_data(&client->dev, &priv->gpio_chip, priv);
176         if (ret < 0) {
177                 dev_err(&client->dev, "Unable to register gpiochip\n");
178                 return ret;
179         }
180
181         i2c_set_clientdata(client, priv);
182
183         return 0;
184 }
185
186 static const struct i2c_device_id ts4900_gpio_id_table[] = {
187         { "ts4900-gpio", },
188         { /* sentinel */ }
189 };
190 MODULE_DEVICE_TABLE(i2c, ts4900_gpio_id_table);
191
192 static struct i2c_driver ts4900_gpio_driver = {
193         .driver = {
194                 .name = "ts4900-gpio",
195                 .of_match_table = ts4900_gpio_of_match_table,
196         },
197         .probe = ts4900_gpio_probe,
198         .id_table = ts4900_gpio_id_table,
199 };
200 module_i2c_driver(ts4900_gpio_driver);
201
202 MODULE_AUTHOR("Technologic Systems");
203 MODULE_DESCRIPTION("GPIO interface for Technologic Systems I2C-FPGA core");
204 MODULE_LICENSE("GPL");