1
2
3
4
5
6
7
8
9
10
11#include <linux/bits.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/gpio/driver.h>
15#include <linux/i2c.h>
16#include <linux/module.h>
17
18
19
20
21
22
23
24struct gw_pld {
25 struct gpio_chip chip;
26 struct i2c_client *client;
27 u8 out;
28};
29
30
31
32
33
34
35static int gw_pld_input8(struct gpio_chip *gc, unsigned offset)
36{
37 struct gw_pld *gw = gpiochip_get_data(gc);
38
39 gw->out |= BIT(offset);
40 return i2c_smbus_write_byte(gw->client, gw->out);
41}
42
43static int gw_pld_get8(struct gpio_chip *gc, unsigned offset)
44{
45 struct gw_pld *gw = gpiochip_get_data(gc);
46 s32 val;
47
48 val = i2c_smbus_read_byte(gw->client);
49
50 return (val < 0) ? 0 : !!(val & BIT(offset));
51}
52
53static int gw_pld_output8(struct gpio_chip *gc, unsigned offset, int value)
54{
55 struct gw_pld *gw = gpiochip_get_data(gc);
56
57 if (value)
58 gw->out |= BIT(offset);
59 else
60 gw->out &= ~BIT(offset);
61
62 return i2c_smbus_write_byte(gw->client, gw->out);
63}
64
65static void gw_pld_set8(struct gpio_chip *gc, unsigned offset, int value)
66{
67 gw_pld_output8(gc, offset, value);
68}
69
70static int gw_pld_probe(struct i2c_client *client,
71 const struct i2c_device_id *id)
72{
73 struct device *dev = &client->dev;
74 struct device_node *np = dev->of_node;
75 struct gw_pld *gw;
76 int ret;
77
78 gw = devm_kzalloc(dev, sizeof(*gw), GFP_KERNEL);
79 if (!gw)
80 return -ENOMEM;
81
82 gw->chip.base = -1;
83 gw->chip.can_sleep = true;
84 gw->chip.parent = dev;
85 gw->chip.of_node = np;
86 gw->chip.owner = THIS_MODULE;
87 gw->chip.label = dev_name(dev);
88 gw->chip.ngpio = 8;
89 gw->chip.direction_input = gw_pld_input8;
90 gw->chip.get = gw_pld_get8;
91 gw->chip.direction_output = gw_pld_output8;
92 gw->chip.set = gw_pld_set8;
93 gw->client = client;
94
95
96
97
98
99
100 client->flags |= I2C_M_IGNORE_NAK;
101 gw->out = 0xFF;
102
103 i2c_set_clientdata(client, gw);
104
105 ret = devm_gpiochip_add_data(dev, &gw->chip, gw);
106 if (ret)
107 return ret;
108
109 dev_info(dev, "registered Gateworks PLD GPIO device\n");
110
111 return 0;
112}
113
114static const struct i2c_device_id gw_pld_id[] = {
115 { "gw-pld", },
116 { }
117};
118MODULE_DEVICE_TABLE(i2c, gw_pld_id);
119
120static const struct of_device_id gw_pld_dt_ids[] = {
121 { .compatible = "gateworks,pld-gpio", },
122 { },
123};
124MODULE_DEVICE_TABLE(of, gw_pld_dt_ids);
125
126static struct i2c_driver gw_pld_driver = {
127 .driver = {
128 .name = "gw_pld",
129 .of_match_table = gw_pld_dt_ids,
130 },
131 .probe = gw_pld_probe,
132 .id_table = gw_pld_id,
133};
134module_i2c_driver(gw_pld_driver);
135
136MODULE_LICENSE("GPL");
137MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
138