1 // SPDX-License-Identifier: GPL-2.0-only
3 * 74Hx164 - Generic serial-in/parallel-out 8-bits shift register GPIO driver
5 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
6 * Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
9 #include <linux/bitops.h>
10 #include <linux/cleanup.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/property.h>
16 #include <linux/slab.h>
17 #include <linux/spi/spi.h>
19 #define GEN_74X164_NUMBER_GPIOS 8
21 struct gen_74x164_chip {
22 struct gpio_chip gpio_chip;
24 struct gpio_desc *gpiod_oe;
27 * Since the registers are chained, every byte sent will make
28 * the previous byte shift to the next register in the
29 * chain. Thus, the first byte sent will end up in the last
30 * register at the end of the transfer. So, to have a logical
31 * numbering, store the bytes in reverse order.
33 u8 buffer[] __counted_by(registers);
36 static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
38 return spi_write(to_spi_device(chip->gpio_chip.parent), chip->buffer,
42 static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
44 struct gen_74x164_chip *chip = gpiochip_get_data(gc);
45 u8 bank = chip->registers - 1 - offset / 8;
48 guard(mutex)(&chip->lock);
50 return !!(chip->buffer[bank] & BIT(pin));
53 static int gen_74x164_set_value(struct gpio_chip *gc,
54 unsigned int offset, int val)
56 struct gen_74x164_chip *chip = gpiochip_get_data(gc);
57 u8 bank = chip->registers - 1 - offset / 8;
60 guard(mutex)(&chip->lock);
63 chip->buffer[bank] |= BIT(pin);
65 chip->buffer[bank] &= ~BIT(pin);
67 return __gen_74x164_write_config(chip);
70 static int gen_74x164_set_multiple(struct gpio_chip *gc, unsigned long *mask,
73 struct gen_74x164_chip *chip = gpiochip_get_data(gc);
75 unsigned long bankmask;
77 unsigned long bitmask;
79 guard(mutex)(&chip->lock);
81 for_each_set_clump8(offset, bankmask, mask, chip->registers * 8) {
82 bank = chip->registers - 1 - offset / 8;
83 bitmask = bitmap_get_value8(bits, offset) & bankmask;
85 chip->buffer[bank] &= ~bankmask;
86 chip->buffer[bank] |= bitmask;
88 return __gen_74x164_write_config(chip);
91 static int gen_74x164_direction_output(struct gpio_chip *gc,
92 unsigned offset, int val)
94 gen_74x164_set_value(gc, offset, val);
98 static void gen_74x164_deactivate(void *data)
100 struct gen_74x164_chip *chip = data;
102 gpiod_set_value_cansleep(chip->gpiod_oe, 0);
105 static int gen_74x164_activate(struct device *dev, struct gen_74x164_chip *chip)
107 gpiod_set_value_cansleep(chip->gpiod_oe, 1);
108 return devm_add_action_or_reset(dev, gen_74x164_deactivate, chip);
111 static int gen_74x164_probe(struct spi_device *spi)
113 struct device *dev = &spi->dev;
114 struct gen_74x164_chip *chip;
119 * bits_per_word cannot be configured in platform data
121 spi->bits_per_word = 8;
123 ret = spi_setup(spi);
127 ret = device_property_read_u32(dev, "registers-number", &nregs);
129 return dev_err_probe(dev, ret, "Missing 'registers-number' property.\n");
131 chip = devm_kzalloc(dev, struct_size(chip, buffer, nregs), GFP_KERNEL);
135 chip->registers = nregs;
137 chip->gpiod_oe = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
138 if (IS_ERR(chip->gpiod_oe))
139 return PTR_ERR(chip->gpiod_oe);
141 chip->gpio_chip.label = spi->modalias;
142 chip->gpio_chip.direction_output = gen_74x164_direction_output;
143 chip->gpio_chip.get = gen_74x164_get_value;
144 chip->gpio_chip.set_rv = gen_74x164_set_value;
145 chip->gpio_chip.set_multiple_rv = gen_74x164_set_multiple;
146 chip->gpio_chip.base = -1;
147 chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers;
148 chip->gpio_chip.can_sleep = true;
149 chip->gpio_chip.parent = dev;
150 chip->gpio_chip.owner = THIS_MODULE;
152 ret = devm_mutex_init(dev, &chip->lock);
156 ret = __gen_74x164_write_config(chip);
158 return dev_err_probe(dev, ret, "Config write failed\n");
160 ret = gen_74x164_activate(dev, chip);
164 return devm_gpiochip_add_data(dev, &chip->gpio_chip, chip);
167 static const struct spi_device_id gen_74x164_spi_ids[] = {
168 { .name = "74hc595" },
169 { .name = "74lvc594" },
172 MODULE_DEVICE_TABLE(spi, gen_74x164_spi_ids);
174 static const struct of_device_id gen_74x164_dt_ids[] = {
175 { .compatible = "fairchild,74hc595" },
176 { .compatible = "nxp,74lvc594" },
179 MODULE_DEVICE_TABLE(of, gen_74x164_dt_ids);
181 static struct spi_driver gen_74x164_driver = {
184 .of_match_table = gen_74x164_dt_ids,
186 .probe = gen_74x164_probe,
187 .id_table = gen_74x164_spi_ids,
189 module_spi_driver(gen_74x164_driver);
191 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
192 MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
193 MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
194 MODULE_LICENSE("GPL v2");