Commit | Line | Data |
---|---|---|
9a9982d4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
ead6db08 MG |
2 | /* |
3 | * 74Hx164 - Generic serial-in/parallel-out 8-bits shift register GPIO driver | |
4 | * | |
5 | * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org> | |
6 | * Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com> | |
ead6db08 MG |
7 | */ |
8 | ||
b2ca9ebf | 9 | #include <linux/bitops.h> |
bdd603ac | 10 | #include <linux/cleanup.h> |
91f6a4af | 11 | #include <linux/gpio/consumer.h> |
517ec439 AS |
12 | #include <linux/gpio/driver.h> |
13 | #include <linux/module.h> | |
14 | #include <linux/mutex.h> | |
3c746951 | 15 | #include <linux/property.h> |
ead6db08 | 16 | #include <linux/slab.h> |
517ec439 | 17 | #include <linux/spi/spi.h> |
ead6db08 | 18 | |
20bc4d5d MR |
19 | #define GEN_74X164_NUMBER_GPIOS 8 |
20 | ||
ead6db08 | 21 | struct gen_74x164_chip { |
ead6db08 MG |
22 | struct gpio_chip gpio_chip; |
23 | struct mutex lock; | |
a158531f | 24 | struct gpio_desc *gpiod_oe; |
20bc4d5d | 25 | u32 registers; |
902e7e60 GU |
26 | /* |
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. | |
32 | */ | |
d746cc6e | 33 | u8 buffer[] __counted_by(registers); |
ead6db08 MG |
34 | }; |
35 | ||
ead6db08 MG |
36 | static int __gen_74x164_write_config(struct gen_74x164_chip *chip) |
37 | { | |
771d899a GU |
38 | return spi_write(to_spi_device(chip->gpio_chip.parent), chip->buffer, |
39 | chip->registers); | |
ead6db08 MG |
40 | } |
41 | ||
ead6db08 MG |
42 | static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset) |
43 | { | |
b2afc6f3 | 44 | struct gen_74x164_chip *chip = gpiochip_get_data(gc); |
902e7e60 | 45 | u8 bank = chip->registers - 1 - offset / 8; |
20bc4d5d | 46 | u8 pin = offset % 8; |
ead6db08 | 47 | |
bdd603ac | 48 | guard(mutex)(&chip->lock); |
ead6db08 | 49 | |
e742e6b0 | 50 | return !!(chip->buffer[bank] & BIT(pin)); |
ead6db08 MG |
51 | } |
52 | ||
56f16c9f BG |
53 | static int gen_74x164_set_value(struct gpio_chip *gc, |
54 | unsigned int offset, int val) | |
ead6db08 | 55 | { |
b2afc6f3 | 56 | struct gen_74x164_chip *chip = gpiochip_get_data(gc); |
902e7e60 | 57 | u8 bank = chip->registers - 1 - offset / 8; |
20bc4d5d | 58 | u8 pin = offset % 8; |
ead6db08 | 59 | |
bdd603ac AS |
60 | guard(mutex)(&chip->lock); |
61 | ||
ead6db08 | 62 | if (val) |
e742e6b0 | 63 | chip->buffer[bank] |= BIT(pin); |
ead6db08 | 64 | else |
e742e6b0 | 65 | chip->buffer[bank] &= ~BIT(pin); |
ead6db08 | 66 | |
56f16c9f | 67 | return __gen_74x164_write_config(chip); |
ead6db08 MG |
68 | } |
69 | ||
56f16c9f BG |
70 | static int gen_74x164_set_multiple(struct gpio_chip *gc, unsigned long *mask, |
71 | unsigned long *bits) | |
d46ab682 GU |
72 | { |
73 | struct gen_74x164_chip *chip = gpiochip_get_data(gc); | |
b2ca9ebf WBG |
74 | unsigned long offset; |
75 | unsigned long bankmask; | |
76 | size_t bank; | |
77 | unsigned long bitmask; | |
d46ab682 | 78 | |
bdd603ac AS |
79 | guard(mutex)(&chip->lock); |
80 | ||
b2ca9ebf WBG |
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; | |
d46ab682 GU |
84 | |
85 | chip->buffer[bank] &= ~bankmask; | |
b2ca9ebf | 86 | chip->buffer[bank] |= bitmask; |
d46ab682 | 87 | } |
56f16c9f | 88 | return __gen_74x164_write_config(chip); |
d46ab682 GU |
89 | } |
90 | ||
a3cc68c3 HS |
91 | static int gen_74x164_direction_output(struct gpio_chip *gc, |
92 | unsigned offset, int val) | |
93 | { | |
94 | gen_74x164_set_value(gc, offset, val); | |
95 | return 0; | |
96 | } | |
97 | ||
abe3817f AS |
98 | static void gen_74x164_deactivate(void *data) |
99 | { | |
100 | struct gen_74x164_chip *chip = data; | |
101 | ||
102 | gpiod_set_value_cansleep(chip->gpiod_oe, 0); | |
103 | } | |
104 | ||
105 | static int gen_74x164_activate(struct device *dev, struct gen_74x164_chip *chip) | |
106 | { | |
107 | gpiod_set_value_cansleep(chip->gpiod_oe, 1); | |
108 | return devm_add_action_or_reset(dev, gen_74x164_deactivate, chip); | |
109 | } | |
110 | ||
3836309d | 111 | static int gen_74x164_probe(struct spi_device *spi) |
ead6db08 | 112 | { |
d746cc6e | 113 | struct device *dev = &spi->dev; |
ead6db08 | 114 | struct gen_74x164_chip *chip; |
410f4574 | 115 | u32 nregs; |
ead6db08 MG |
116 | int ret; |
117 | ||
ead6db08 MG |
118 | /* |
119 | * bits_per_word cannot be configured in platform data | |
120 | */ | |
121 | spi->bits_per_word = 8; | |
122 | ||
123 | ret = spi_setup(spi); | |
124 | if (ret < 0) | |
125 | return ret; | |
126 | ||
5892cfc7 | 127 | ret = device_property_read_u32(dev, "registers-number", &nregs); |
9bd2dbe4 AS |
128 | if (ret) |
129 | return dev_err_probe(dev, ret, "Missing 'registers-number' property.\n"); | |
410f4574 | 130 | |
d746cc6e | 131 | chip = devm_kzalloc(dev, struct_size(chip, buffer, nregs), GFP_KERNEL); |
ead6db08 MG |
132 | if (!chip) |
133 | return -ENOMEM; | |
134 | ||
d746cc6e AS |
135 | chip->registers = nregs; |
136 | ||
5892cfc7 | 137 | chip->gpiod_oe = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW); |
7ebc194d FE |
138 | if (IS_ERR(chip->gpiod_oe)) |
139 | return PTR_ERR(chip->gpiod_oe); | |
140 | ||
a3cc68c3 HS |
141 | chip->gpio_chip.label = spi->modalias; |
142 | chip->gpio_chip.direction_output = gen_74x164_direction_output; | |
ead6db08 | 143 | chip->gpio_chip.get = gen_74x164_get_value; |
56f16c9f BG |
144 | chip->gpio_chip.set_rv = gen_74x164_set_value; |
145 | chip->gpio_chip.set_multiple_rv = gen_74x164_set_multiple; | |
61e73804 | 146 | chip->gpio_chip.base = -1; |
20bc4d5d | 147 | chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers; |
9fb1f39e | 148 | chip->gpio_chip.can_sleep = true; |
5892cfc7 | 149 | chip->gpio_chip.parent = dev; |
ead6db08 MG |
150 | chip->gpio_chip.owner = THIS_MODULE; |
151 | ||
5892cfc7 | 152 | ret = devm_mutex_init(dev, &chip->lock); |
dacd8ed2 BG |
153 | if (ret) |
154 | return ret; | |
bcc0562c | 155 | |
ead6db08 | 156 | ret = __gen_74x164_write_config(chip); |
dacd8ed2 | 157 | if (ret) |
5892cfc7 | 158 | return dev_err_probe(dev, ret, "Config write failed\n"); |
ead6db08 | 159 | |
abe3817f AS |
160 | ret = gen_74x164_activate(dev, chip); |
161 | if (ret) | |
162 | return ret; | |
530b1dbd | 163 | |
5892cfc7 | 164 | return devm_gpiochip_add_data(dev, &chip->gpio_chip, chip); |
ead6db08 MG |
165 | } |
166 | ||
be449183 MB |
167 | static const struct spi_device_id gen_74x164_spi_ids[] = { |
168 | { .name = "74hc595" }, | |
169 | { .name = "74lvc594" }, | |
170 | {}, | |
171 | }; | |
172 | MODULE_DEVICE_TABLE(spi, gen_74x164_spi_ids); | |
173 | ||
0a90a9fb MR |
174 | static const struct of_device_id gen_74x164_dt_ids[] = { |
175 | { .compatible = "fairchild,74hc595" }, | |
80018bd9 | 176 | { .compatible = "nxp,74lvc594" }, |
0a90a9fb MR |
177 | {}, |
178 | }; | |
179 | MODULE_DEVICE_TABLE(of, gen_74x164_dt_ids); | |
180 | ||
ead6db08 MG |
181 | static struct spi_driver gen_74x164_driver = { |
182 | .driver = { | |
a3cc68c3 | 183 | .name = "74x164", |
187a53a5 | 184 | .of_match_table = gen_74x164_dt_ids, |
ead6db08 MG |
185 | }, |
186 | .probe = gen_74x164_probe, | |
be449183 | 187 | .id_table = gen_74x164_spi_ids, |
ead6db08 | 188 | }; |
ab3b8782 | 189 | module_spi_driver(gen_74x164_driver); |
ead6db08 MG |
190 | |
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"); |