xref: /openbmc/linux/drivers/gpio/gpio-gw-pld.c (revision d9565bf4)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Gateworks I2C PLD GPIO expander
4 //
5 // Copyright (C) 2019 Linus Walleij <linus.walleij@linaro.org>
6 //
7 // Based on code and know-how from the OpenWrt driver:
8 // Copyright (C) 2009 Gateworks Corporation
9 // Authors: Chris Lang, Imre Kaloz
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  * struct gw_pld - State container for Gateworks PLD
20  * @chip: GPIO chip instance
21  * @client: I2C client
22  * @out: shadow register for the output bute
23  */
24 struct gw_pld {
25 	struct gpio_chip chip;
26 	struct i2c_client *client;
27 	u8 out;
28 };
29 
30 /*
31  * The Gateworks I2C PLD chip only expose one read and one write register.
32  * Writing a "one" bit (to match the reset state) lets that pin be used as an
33  * input. It is an open-drain model.
34  */
35 static 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 
43 static 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 
53 static 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 
65 static void gw_pld_set8(struct gpio_chip *gc, unsigned offset, int value)
66 {
67 	gw_pld_output8(gc, offset, value);
68 }
69 
70 static int gw_pld_probe(struct i2c_client *client,
71 			const struct i2c_device_id *id)
72 {
73 	struct device *dev = &client->dev;
74 	struct gw_pld *gw;
75 	int ret;
76 
77 	gw = devm_kzalloc(dev, sizeof(*gw), GFP_KERNEL);
78 	if (!gw)
79 		return -ENOMEM;
80 
81 	gw->chip.base = -1;
82 	gw->chip.can_sleep = true;
83 	gw->chip.parent = dev;
84 	gw->chip.owner = THIS_MODULE;
85 	gw->chip.label = dev_name(dev);
86 	gw->chip.ngpio = 8;
87 	gw->chip.direction_input = gw_pld_input8;
88 	gw->chip.get = gw_pld_get8;
89 	gw->chip.direction_output = gw_pld_output8;
90 	gw->chip.set = gw_pld_set8;
91 	gw->client = client;
92 
93 	/*
94 	 * The Gateworks I2C PLD chip does not properly send the acknowledge
95 	 * bit at all times, but we can still use the standard i2c_smbus
96 	 * functions by simply ignoring this bit.
97 	 */
98 	client->flags |= I2C_M_IGNORE_NAK;
99 	gw->out = 0xFF;
100 
101 	i2c_set_clientdata(client, gw);
102 
103 	ret = devm_gpiochip_add_data(dev, &gw->chip, gw);
104 	if (ret)
105 		return ret;
106 
107 	dev_info(dev, "registered Gateworks PLD GPIO device\n");
108 
109 	return 0;
110 }
111 
112 static const struct i2c_device_id gw_pld_id[] = {
113 	{ "gw-pld", },
114 	{ }
115 };
116 MODULE_DEVICE_TABLE(i2c, gw_pld_id);
117 
118 static const struct of_device_id gw_pld_dt_ids[] = {
119 	{ .compatible = "gateworks,pld-gpio", },
120 	{ },
121 };
122 MODULE_DEVICE_TABLE(of, gw_pld_dt_ids);
123 
124 static struct i2c_driver gw_pld_driver = {
125 	.driver = {
126 		.name = "gw_pld",
127 		.of_match_table = gw_pld_dt_ids,
128 	},
129 	.probe = gw_pld_probe,
130 	.id_table = gw_pld_id,
131 };
132 module_i2c_driver(gw_pld_driver);
133 
134 MODULE_LICENSE("GPL");
135 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
136