0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/bits.h>
0012 #include <linux/kernel.h>
0013 #include <linux/slab.h>
0014 #include <linux/gpio/driver.h>
0015 #include <linux/i2c.h>
0016 #include <linux/module.h>
0017
0018
0019
0020
0021
0022
0023
0024 struct gw_pld {
0025 struct gpio_chip chip;
0026 struct i2c_client *client;
0027 u8 out;
0028 };
0029
0030
0031
0032
0033
0034
0035 static int gw_pld_input8(struct gpio_chip *gc, unsigned offset)
0036 {
0037 struct gw_pld *gw = gpiochip_get_data(gc);
0038
0039 gw->out |= BIT(offset);
0040 return i2c_smbus_write_byte(gw->client, gw->out);
0041 }
0042
0043 static int gw_pld_get8(struct gpio_chip *gc, unsigned offset)
0044 {
0045 struct gw_pld *gw = gpiochip_get_data(gc);
0046 s32 val;
0047
0048 val = i2c_smbus_read_byte(gw->client);
0049
0050 return (val < 0) ? 0 : !!(val & BIT(offset));
0051 }
0052
0053 static int gw_pld_output8(struct gpio_chip *gc, unsigned offset, int value)
0054 {
0055 struct gw_pld *gw = gpiochip_get_data(gc);
0056
0057 if (value)
0058 gw->out |= BIT(offset);
0059 else
0060 gw->out &= ~BIT(offset);
0061
0062 return i2c_smbus_write_byte(gw->client, gw->out);
0063 }
0064
0065 static void gw_pld_set8(struct gpio_chip *gc, unsigned offset, int value)
0066 {
0067 gw_pld_output8(gc, offset, value);
0068 }
0069
0070 static int gw_pld_probe(struct i2c_client *client,
0071 const struct i2c_device_id *id)
0072 {
0073 struct device *dev = &client->dev;
0074 struct gw_pld *gw;
0075 int ret;
0076
0077 gw = devm_kzalloc(dev, sizeof(*gw), GFP_KERNEL);
0078 if (!gw)
0079 return -ENOMEM;
0080
0081 gw->chip.base = -1;
0082 gw->chip.can_sleep = true;
0083 gw->chip.parent = dev;
0084 gw->chip.owner = THIS_MODULE;
0085 gw->chip.label = dev_name(dev);
0086 gw->chip.ngpio = 8;
0087 gw->chip.direction_input = gw_pld_input8;
0088 gw->chip.get = gw_pld_get8;
0089 gw->chip.direction_output = gw_pld_output8;
0090 gw->chip.set = gw_pld_set8;
0091 gw->client = client;
0092
0093
0094
0095
0096
0097
0098 client->flags |= I2C_M_IGNORE_NAK;
0099 gw->out = 0xFF;
0100
0101 i2c_set_clientdata(client, gw);
0102
0103 ret = devm_gpiochip_add_data(dev, &gw->chip, gw);
0104 if (ret)
0105 return ret;
0106
0107 dev_info(dev, "registered Gateworks PLD GPIO device\n");
0108
0109 return 0;
0110 }
0111
0112 static const struct i2c_device_id gw_pld_id[] = {
0113 { "gw-pld", },
0114 { }
0115 };
0116 MODULE_DEVICE_TABLE(i2c, gw_pld_id);
0117
0118 static const struct of_device_id gw_pld_dt_ids[] = {
0119 { .compatible = "gateworks,pld-gpio", },
0120 { },
0121 };
0122 MODULE_DEVICE_TABLE(of, gw_pld_dt_ids);
0123
0124 static struct i2c_driver gw_pld_driver = {
0125 .driver = {
0126 .name = "gw_pld",
0127 .of_match_table = gw_pld_dt_ids,
0128 },
0129 .probe = gw_pld_probe,
0130 .id_table = gw_pld_id,
0131 };
0132 module_i2c_driver(gw_pld_driver);
0133
0134 MODULE_LICENSE("GPL");
0135 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");