2 * Digital I/O driver for Technologic Systems I2C FPGA Core
4 * Copyright (C) 2015 Technologic Systems
5 * Copyright (C) 2016 Savoir-Faire Linux
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.
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.
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>
23 #define DEFAULT_PIN_NUMBER 32
25 * Register bits used by the GPIO device
26 * Some boards, such as TS-7970 do not have a separate input bit
28 #define TS4900_GPIO_OE 0x01
29 #define TS4900_GPIO_OUT 0x02
30 #define TS4900_GPIO_IN 0x04
31 #define TS7970_GPIO_IN 0x02
33 struct ts4900_gpio_priv
{
34 struct regmap
*regmap
;
35 struct gpio_chip gpio_chip
;
36 unsigned int input_bit
;
39 static int ts4900_gpio_get_direction(struct gpio_chip
*chip
,
42 struct ts4900_gpio_priv
*priv
= gpiochip_get_data(chip
);
45 regmap_read(priv
->regmap
, offset
, ®
);
47 if (reg
& TS4900_GPIO_OE
)
48 return GPIO_LINE_DIRECTION_OUT
;
50 return GPIO_LINE_DIRECTION_IN
;
53 static int ts4900_gpio_direction_input(struct gpio_chip
*chip
,
56 struct ts4900_gpio_priv
*priv
= gpiochip_get_data(chip
);
59 * This will clear the output enable bit, the other bits are
60 * dontcare when this is cleared
62 return regmap_write(priv
->regmap
, offset
, 0);
65 static int ts4900_gpio_direction_output(struct gpio_chip
*chip
,
66 unsigned int offset
, int value
)
68 struct ts4900_gpio_priv
*priv
= gpiochip_get_data(chip
);
72 ret
= regmap_write(priv
->regmap
, offset
, TS4900_GPIO_OE
|
75 ret
= regmap_write(priv
->regmap
, offset
, TS4900_GPIO_OE
);
80 static int ts4900_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
82 struct ts4900_gpio_priv
*priv
= gpiochip_get_data(chip
);
85 regmap_read(priv
->regmap
, offset
, ®
);
87 return !!(reg
& priv
->input_bit
);
90 static void ts4900_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
93 struct ts4900_gpio_priv
*priv
= gpiochip_get_data(chip
);
96 regmap_update_bits(priv
->regmap
, offset
, TS4900_GPIO_OUT
,
99 regmap_update_bits(priv
->regmap
, offset
, TS4900_GPIO_OUT
, 0);
102 static const struct regmap_config ts4900_regmap_config
= {
107 static const struct gpio_chip template_chip
= {
108 .label
= "ts4900-gpio",
109 .owner
= THIS_MODULE
,
110 .get_direction
= ts4900_gpio_get_direction
,
111 .direction_input
= ts4900_gpio_direction_input
,
112 .direction_output
= ts4900_gpio_direction_output
,
113 .get
= ts4900_gpio_get
,
114 .set
= ts4900_gpio_set
,
119 static const struct of_device_id ts4900_gpio_of_match_table
[] = {
121 .compatible
= "technologic,ts4900-gpio",
122 .data
= (void *)TS4900_GPIO_IN
,
124 .compatible
= "technologic,ts7970-gpio",
125 .data
= (void *)TS7970_GPIO_IN
,
129 MODULE_DEVICE_TABLE(of
, ts4900_gpio_of_match_table
);
131 static int ts4900_gpio_probe(struct i2c_client
*client
,
132 const struct i2c_device_id
*id
)
134 struct ts4900_gpio_priv
*priv
;
138 if (of_property_read_u32(client
->dev
.of_node
, "ngpios", &ngpio
))
139 ngpio
= DEFAULT_PIN_NUMBER
;
141 priv
= devm_kzalloc(&client
->dev
, sizeof(*priv
), GFP_KERNEL
);
145 priv
->gpio_chip
= template_chip
;
146 priv
->gpio_chip
.label
= "ts4900-gpio";
147 priv
->gpio_chip
.ngpio
= ngpio
;
148 priv
->gpio_chip
.parent
= &client
->dev
;
149 priv
->input_bit
= (uintptr_t)of_device_get_match_data(&client
->dev
);
151 priv
->regmap
= devm_regmap_init_i2c(client
, &ts4900_regmap_config
);
152 if (IS_ERR(priv
->regmap
)) {
153 ret
= PTR_ERR(priv
->regmap
);
154 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
159 ret
= devm_gpiochip_add_data(&client
->dev
, &priv
->gpio_chip
, priv
);
161 dev_err(&client
->dev
, "Unable to register gpiochip\n");
165 i2c_set_clientdata(client
, priv
);
170 static const struct i2c_device_id ts4900_gpio_id_table
[] = {
174 MODULE_DEVICE_TABLE(i2c
, ts4900_gpio_id_table
);
176 static struct i2c_driver ts4900_gpio_driver
= {
178 .name
= "ts4900-gpio",
179 .of_match_table
= ts4900_gpio_of_match_table
,
181 .probe
= ts4900_gpio_probe
,
182 .id_table
= ts4900_gpio_id_table
,
184 module_i2c_driver(ts4900_gpio_driver
);
186 MODULE_AUTHOR("Technologic Systems");
187 MODULE_DESCRIPTION("GPIO interface for Technologic Systems I2C-FPGA core");
188 MODULE_LICENSE("GPL");