1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015-2023 Texas Instruments Incorporated - https://www.ti.com/
4 * Andrew Davis <afd@ti.com>
7 #include <linux/gpio/driver.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
12 #define TPIC2810_WS_COMMAND 0x44
15 * struct tpic2810 - GPIO driver data
16 * @chip: GPIO controller chip
17 * @client: I2C device pointer
18 * @buffer: Buffer for device register
19 * @lock: Protects write sequences
22 struct gpio_chip chip;
23 struct i2c_client *client;
28 static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value);
30 static int tpic2810_get_direction(struct gpio_chip *chip,
33 /* This device always output */
34 return GPIO_LINE_DIRECTION_OUT;
37 static int tpic2810_direction_input(struct gpio_chip *chip,
40 /* This device is output only */
44 static int tpic2810_direction_output(struct gpio_chip *chip,
45 unsigned offset, int value)
47 /* This device always output */
48 tpic2810_set(chip, offset, value);
52 static void tpic2810_set_mask_bits(struct gpio_chip *chip, u8 mask, u8 bits)
54 struct tpic2810 *gpio = gpiochip_get_data(chip);
58 mutex_lock(&gpio->lock);
60 buffer = gpio->buffer & ~mask;
61 buffer |= (mask & bits);
63 err = i2c_smbus_write_byte_data(gpio->client, TPIC2810_WS_COMMAND,
66 gpio->buffer = buffer;
68 mutex_unlock(&gpio->lock);
71 static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value)
73 tpic2810_set_mask_bits(chip, BIT(offset), value ? BIT(offset) : 0);
76 static void tpic2810_set_multiple(struct gpio_chip *chip, unsigned long *mask,
79 tpic2810_set_mask_bits(chip, *mask, *bits);
82 static const struct gpio_chip template_chip = {
85 .get_direction = tpic2810_get_direction,
86 .direction_input = tpic2810_direction_input,
87 .direction_output = tpic2810_direction_output,
89 .set_multiple = tpic2810_set_multiple,
95 static const struct of_device_id tpic2810_of_match_table[] = {
96 { .compatible = "ti,tpic2810" },
99 MODULE_DEVICE_TABLE(of, tpic2810_of_match_table);
101 static int tpic2810_probe(struct i2c_client *client)
103 struct tpic2810 *gpio;
105 gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
109 gpio->chip = template_chip;
110 gpio->chip.parent = &client->dev;
112 gpio->client = client;
114 mutex_init(&gpio->lock);
116 return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
119 static const struct i2c_device_id tpic2810_id_table[] = {
123 MODULE_DEVICE_TABLE(i2c, tpic2810_id_table);
125 static struct i2c_driver tpic2810_driver = {
128 .of_match_table = tpic2810_of_match_table,
130 .probe = tpic2810_probe,
131 .id_table = tpic2810_id_table,
133 module_i2c_driver(tpic2810_driver);
135 MODULE_AUTHOR("Andrew Davis <afd@ti.com>");
136 MODULE_DESCRIPTION("TPIC2810 8-Bit LED Driver GPIO Driver");
137 MODULE_LICENSE("GPL v2");