blob: d918d2df4de2cbf536a67483b496f5079640ebfd [file] [log] [blame]
Lucile Quirion9c668632016-08-12 11:16:49 -04001/*
2 * Digital I/O driver for Technologic Systems I2C FPGA Core
3 *
Mark Featherston3a4cd1c2022-03-09 17:16:16 -08004 * Copyright (C) 2015, 2018 Technologic Systems
Lucile Quirion9c668632016-08-12 11:16:49 -04005 * 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
33struct ts4900_gpio_priv {
34 struct regmap *regmap;
35 struct gpio_chip gpio_chip;
36 unsigned int input_bit;
37};
38
39static 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
Matti Vaittinene42615e2019-11-06 10:54:12 +020047 if (reg & TS4900_GPIO_OE)
48 return GPIO_LINE_DIRECTION_OUT;
49
50 return GPIO_LINE_DIRECTION_IN;
Lucile Quirion9c668632016-08-12 11:16:49 -040051}
52
53static int ts4900_gpio_direction_input(struct gpio_chip *chip,
54 unsigned int offset)
55{
56 struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
57
Mark Featherston3a4cd1c2022-03-09 17:16:16 -080058 /* Only clear the OE bit here, requires a RMW. Prevents potential issue
59 * with OE and data getting to the physical pin at different times.
Lucile Quirion9c668632016-08-12 11:16:49 -040060 */
Mark Featherston3a4cd1c2022-03-09 17:16:16 -080061 return regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OE, 0);
Lucile Quirion9c668632016-08-12 11:16:49 -040062}
63
64static int ts4900_gpio_direction_output(struct gpio_chip *chip,
65 unsigned int offset, int value)
66{
67 struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
Mark Featherston3a4cd1c2022-03-09 17:16:16 -080068 unsigned int reg;
Lucile Quirion9c668632016-08-12 11:16:49 -040069 int ret;
70
Mark Featherston3a4cd1c2022-03-09 17:16:16 -080071 /* If changing from an input to an output, we need to first set the
72 * proper data bit to what is requested and then set OE bit. This
73 * prevents a glitch that can occur on the IO line
74 */
75 regmap_read(priv->regmap, offset, &reg);
76 if (!(reg & TS4900_GPIO_OE)) {
77 if (value)
78 reg = TS4900_GPIO_OUT;
79 else
80 reg &= ~TS4900_GPIO_OUT;
81
82 regmap_write(priv->regmap, offset, reg);
83 }
84
Lucile Quirion9c668632016-08-12 11:16:49 -040085 if (value)
86 ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE |
87 TS4900_GPIO_OUT);
88 else
89 ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE);
90
91 return ret;
92}
93
94static int ts4900_gpio_get(struct gpio_chip *chip, unsigned int offset)
95{
96 struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
97 unsigned int reg;
98
99 regmap_read(priv->regmap, offset, &reg);
100
101 return !!(reg & priv->input_bit);
102}
103
104static void ts4900_gpio_set(struct gpio_chip *chip, unsigned int offset,
105 int value)
106{
107 struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
108
109 if (value)
110 regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT,
111 TS4900_GPIO_OUT);
112 else
113 regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT, 0);
114}
115
116static const struct regmap_config ts4900_regmap_config = {
117 .reg_bits = 16,
118 .val_bits = 8,
119};
120
Julia Lawalle35b5ab2016-09-11 14:14:37 +0200121static const struct gpio_chip template_chip = {
Lucile Quirion9c668632016-08-12 11:16:49 -0400122 .label = "ts4900-gpio",
123 .owner = THIS_MODULE,
124 .get_direction = ts4900_gpio_get_direction,
125 .direction_input = ts4900_gpio_direction_input,
126 .direction_output = ts4900_gpio_direction_output,
127 .get = ts4900_gpio_get,
128 .set = ts4900_gpio_set,
129 .base = -1,
130 .can_sleep = true,
131};
132
133static const struct of_device_id ts4900_gpio_of_match_table[] = {
134 {
135 .compatible = "technologic,ts4900-gpio",
136 .data = (void *)TS4900_GPIO_IN,
137 }, {
138 .compatible = "technologic,ts7970-gpio",
139 .data = (void *)TS7970_GPIO_IN,
140 },
141 { /* sentinel */ },
142};
143MODULE_DEVICE_TABLE(of, ts4900_gpio_of_match_table);
144
145static int ts4900_gpio_probe(struct i2c_client *client,
146 const struct i2c_device_id *id)
147{
Lucile Quirion9c668632016-08-12 11:16:49 -0400148 struct ts4900_gpio_priv *priv;
149 u32 ngpio;
150 int ret;
151
Lucile Quirion9c668632016-08-12 11:16:49 -0400152 if (of_property_read_u32(client->dev.of_node, "ngpios", &ngpio))
153 ngpio = DEFAULT_PIN_NUMBER;
154
155 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
156 if (!priv)
157 return -ENOMEM;
158
159 priv->gpio_chip = template_chip;
160 priv->gpio_chip.label = "ts4900-gpio";
161 priv->gpio_chip.ngpio = ngpio;
162 priv->gpio_chip.parent = &client->dev;
Thierry Reding81317c52018-04-30 09:38:17 +0200163 priv->input_bit = (uintptr_t)of_device_get_match_data(&client->dev);
Lucile Quirion9c668632016-08-12 11:16:49 -0400164
165 priv->regmap = devm_regmap_init_i2c(client, &ts4900_regmap_config);
166 if (IS_ERR(priv->regmap)) {
167 ret = PTR_ERR(priv->regmap);
168 dev_err(&client->dev, "Failed to allocate register map: %d\n",
169 ret);
170 return ret;
171 }
172
173 ret = devm_gpiochip_add_data(&client->dev, &priv->gpio_chip, priv);
174 if (ret < 0) {
175 dev_err(&client->dev, "Unable to register gpiochip\n");
176 return ret;
177 }
178
179 i2c_set_clientdata(client, priv);
180
181 return 0;
182}
183
184static const struct i2c_device_id ts4900_gpio_id_table[] = {
185 { "ts4900-gpio", },
186 { /* sentinel */ }
187};
188MODULE_DEVICE_TABLE(i2c, ts4900_gpio_id_table);
189
190static struct i2c_driver ts4900_gpio_driver = {
191 .driver = {
192 .name = "ts4900-gpio",
193 .of_match_table = ts4900_gpio_of_match_table,
194 },
195 .probe = ts4900_gpio_probe,
196 .id_table = ts4900_gpio_id_table,
197};
198module_i2c_driver(ts4900_gpio_driver);
199
200MODULE_AUTHOR("Technologic Systems");
201MODULE_DESCRIPTION("GPIO interface for Technologic Systems I2C-FPGA core");
202MODULE_LICENSE("GPL");