blob: e81307f9754e38a99c3f1d50a68e3b16c062ffc5 [file] [log] [blame]
Andy Shevchenko9a9982d2019-03-25 15:47:48 +02001// SPDX-License-Identifier: GPL-2.0-only
Miguel Gaioead6db082010-10-27 15:33:18 -07002/*
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>
Miguel Gaioead6db082010-10-27 15:33:18 -07007 */
8
Linus Walleij91f6a4a2018-01-13 22:07:09 +01009#include <linux/gpio/consumer.h>
Andy Shevchenko517ec432019-03-25 15:47:47 +020010#include <linux/gpio/driver.h>
11#include <linux/module.h>
12#include <linux/mutex.h>
Andy Shevchenko3c746952019-03-25 15:47:46 +020013#include <linux/property.h>
Miguel Gaioead6db082010-10-27 15:33:18 -070014#include <linux/slab.h>
Andy Shevchenko517ec432019-03-25 15:47:47 +020015#include <linux/spi/spi.h>
Miguel Gaioead6db082010-10-27 15:33:18 -070016
Maxime Ripard20bc4d52012-09-10 22:35:39 +020017#define GEN_74X164_NUMBER_GPIOS 8
18
Miguel Gaioead6db082010-10-27 15:33:18 -070019struct gen_74x164_chip {
Miguel Gaioead6db082010-10-27 15:33:18 -070020 struct gpio_chip gpio_chip;
21 struct mutex lock;
Geert Uytterhoevena1585312017-11-21 15:18:10 +010022 struct gpio_desc *gpiod_oe;
Maxime Ripard20bc4d52012-09-10 22:35:39 +020023 u32 registers;
Geert Uytterhoeven902e7e62015-11-30 15:35:26 +010024 /*
25 * Since the registers are chained, every byte sent will make
26 * the previous byte shift to the next register in the
27 * chain. Thus, the first byte sent will end up in the last
28 * register at the end of the transfer. So, to have a logical
29 * numbering, store the bytes in reverse order.
30 */
Geert Uytterhoevena1585312017-11-21 15:18:10 +010031 u8 buffer[];
Miguel Gaioead6db082010-10-27 15:33:18 -070032};
33
Miguel Gaioead6db082010-10-27 15:33:18 -070034static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
35{
Geert Uytterhoeven771d8992016-06-17 18:39:28 +020036 return spi_write(to_spi_device(chip->gpio_chip.parent), chip->buffer,
37 chip->registers);
Miguel Gaioead6db082010-10-27 15:33:18 -070038}
39
Miguel Gaioead6db082010-10-27 15:33:18 -070040static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
41{
Linus Walleijb2afc6f2015-12-03 18:20:29 +010042 struct gen_74x164_chip *chip = gpiochip_get_data(gc);
Geert Uytterhoeven902e7e62015-11-30 15:35:26 +010043 u8 bank = chip->registers - 1 - offset / 8;
Maxime Ripard20bc4d52012-09-10 22:35:39 +020044 u8 pin = offset % 8;
Miguel Gaioead6db082010-10-27 15:33:18 -070045 int ret;
46
47 mutex_lock(&chip->lock);
Maxime Ripard20bc4d52012-09-10 22:35:39 +020048 ret = (chip->buffer[bank] >> pin) & 0x1;
Miguel Gaioead6db082010-10-27 15:33:18 -070049 mutex_unlock(&chip->lock);
50
51 return ret;
52}
53
54static void gen_74x164_set_value(struct gpio_chip *gc,
55 unsigned offset, int val)
56{
Linus Walleijb2afc6f2015-12-03 18:20:29 +010057 struct gen_74x164_chip *chip = gpiochip_get_data(gc);
Geert Uytterhoeven902e7e62015-11-30 15:35:26 +010058 u8 bank = chip->registers - 1 - offset / 8;
Maxime Ripard20bc4d52012-09-10 22:35:39 +020059 u8 pin = offset % 8;
Miguel Gaioead6db082010-10-27 15:33:18 -070060
61 mutex_lock(&chip->lock);
62 if (val)
Maxime Ripard20bc4d52012-09-10 22:35:39 +020063 chip->buffer[bank] |= (1 << pin);
Miguel Gaioead6db082010-10-27 15:33:18 -070064 else
Maxime Ripard20bc4d52012-09-10 22:35:39 +020065 chip->buffer[bank] &= ~(1 << pin);
Miguel Gaioead6db082010-10-27 15:33:18 -070066
67 __gen_74x164_write_config(chip);
68 mutex_unlock(&chip->lock);
69}
70
Geert Uytterhoevend46ab682016-03-14 16:19:18 +010071static void gen_74x164_set_multiple(struct gpio_chip *gc, unsigned long *mask,
72 unsigned long *bits)
73{
74 struct gen_74x164_chip *chip = gpiochip_get_data(gc);
75 unsigned int i, idx, shift;
76 u8 bank, bankmask;
77
78 mutex_lock(&chip->lock);
79 for (i = 0, bank = chip->registers - 1; i < chip->registers;
80 i++, bank--) {
81 idx = i / sizeof(*mask);
82 shift = i % sizeof(*mask) * BITS_PER_BYTE;
83 bankmask = mask[idx] >> shift;
84 if (!bankmask)
85 continue;
86
87 chip->buffer[bank] &= ~bankmask;
88 chip->buffer[bank] |= bankmask & (bits[idx] >> shift);
89 }
90 __gen_74x164_write_config(chip);
91 mutex_unlock(&chip->lock);
92}
93
H Hartley Sweetena3cc68c2011-05-27 16:35:59 -070094static int gen_74x164_direction_output(struct gpio_chip *gc,
95 unsigned offset, int val)
96{
97 gen_74x164_set_value(gc, offset, val);
98 return 0;
99}
100
Bill Pemberton38363092012-11-19 13:22:34 -0500101static int gen_74x164_probe(struct spi_device *spi)
Miguel Gaioead6db082010-10-27 15:33:18 -0700102{
103 struct gen_74x164_chip *chip;
Geert Uytterhoeven410f4572015-11-30 15:35:25 +0100104 u32 nregs;
Miguel Gaioead6db082010-10-27 15:33:18 -0700105 int ret;
106
Miguel Gaioead6db082010-10-27 15:33:18 -0700107 /*
108 * bits_per_word cannot be configured in platform data
109 */
110 spi->bits_per_word = 8;
111
112 ret = spi_setup(spi);
113 if (ret < 0)
114 return ret;
115
Andy Shevchenko3c746952019-03-25 15:47:46 +0200116 ret = device_property_read_u32(&spi->dev, "registers-number", &nregs);
117 if (ret) {
118 dev_err(&spi->dev, "Missing 'registers-number' property.\n");
Geert Uytterhoeven410f4572015-11-30 15:35:25 +0100119 return -EINVAL;
120 }
121
122 chip = devm_kzalloc(&spi->dev, sizeof(*chip) + nregs, GFP_KERNEL);
Miguel Gaioead6db082010-10-27 15:33:18 -0700123 if (!chip)
124 return -ENOMEM;
125
Fabio Estevam7ebc1942017-08-07 09:41:50 -0300126 chip->gpiod_oe = devm_gpiod_get_optional(&spi->dev, "enable",
127 GPIOD_OUT_LOW);
128 if (IS_ERR(chip->gpiod_oe))
129 return PTR_ERR(chip->gpiod_oe);
130
131 gpiod_set_value_cansleep(chip->gpiod_oe, 1);
132
Jingoo Han6c0cf422013-03-15 18:17:18 +0900133 spi_set_drvdata(spi, chip);
Miguel Gaioead6db082010-10-27 15:33:18 -0700134
H Hartley Sweetena3cc68c2011-05-27 16:35:59 -0700135 chip->gpio_chip.label = spi->modalias;
136 chip->gpio_chip.direction_output = gen_74x164_direction_output;
Miguel Gaioead6db082010-10-27 15:33:18 -0700137 chip->gpio_chip.get = gen_74x164_get_value;
138 chip->gpio_chip.set = gen_74x164_set_value;
Geert Uytterhoevend46ab682016-03-14 16:19:18 +0100139 chip->gpio_chip.set_multiple = gen_74x164_set_multiple;
Alexander Shiyan61e73802013-12-07 14:08:22 +0400140 chip->gpio_chip.base = -1;
Maxime Ripard20bc4d52012-09-10 22:35:39 +0200141
Geert Uytterhoeven410f4572015-11-30 15:35:25 +0100142 chip->registers = nregs;
Maxime Ripard20bc4d52012-09-10 22:35:39 +0200143 chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers;
Maxime Ripard20bc4d52012-09-10 22:35:39 +0200144
Linus Walleij9fb1f392013-12-04 14:42:46 +0100145 chip->gpio_chip.can_sleep = true;
Linus Walleij58383c782015-11-04 09:56:26 +0100146 chip->gpio_chip.parent = &spi->dev;
Miguel Gaioead6db082010-10-27 15:33:18 -0700147 chip->gpio_chip.owner = THIS_MODULE;
148
Alexander Shiyanbcc05622013-12-07 14:08:23 +0400149 mutex_init(&chip->lock);
150
Miguel Gaioead6db082010-10-27 15:33:18 -0700151 ret = __gen_74x164_write_config(chip);
152 if (ret) {
153 dev_err(&spi->dev, "Failed writing: %d\n", ret);
154 goto exit_destroy;
155 }
156
Linus Walleijb2afc6f2015-12-03 18:20:29 +0100157 ret = gpiochip_add_data(&chip->gpio_chip, chip);
Alexander Shiyanbcc05622013-12-07 14:08:23 +0400158 if (!ret)
159 return 0;
Miguel Gaioead6db082010-10-27 15:33:18 -0700160
161exit_destroy:
Miguel Gaioead6db082010-10-27 15:33:18 -0700162 mutex_destroy(&chip->lock);
Alexander Shiyanbcc05622013-12-07 14:08:23 +0400163
Miguel Gaioead6db082010-10-27 15:33:18 -0700164 return ret;
165}
166
Bill Pemberton206210c2012-11-19 13:25:50 -0500167static int gen_74x164_remove(struct spi_device *spi)
Miguel Gaioead6db082010-10-27 15:33:18 -0700168{
Alexander Shiyanbcc05622013-12-07 14:08:23 +0400169 struct gen_74x164_chip *chip = spi_get_drvdata(spi);
Miguel Gaioead6db082010-10-27 15:33:18 -0700170
Fabio Estevam7ebc1942017-08-07 09:41:50 -0300171 gpiod_set_value_cansleep(chip->gpiod_oe, 0);
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200172 gpiochip_remove(&chip->gpio_chip);
173 mutex_destroy(&chip->lock);
Miguel Gaioead6db082010-10-27 15:33:18 -0700174
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200175 return 0;
Miguel Gaioead6db082010-10-27 15:33:18 -0700176}
177
Maxime Ripard0a90a9f2012-09-07 14:18:13 +0200178static const struct of_device_id gen_74x164_dt_ids[] = {
179 { .compatible = "fairchild,74hc595" },
Nicolas Saenz Julienne80018bd2016-03-14 23:32:10 +0000180 { .compatible = "nxp,74lvc594" },
Maxime Ripard0a90a9f2012-09-07 14:18:13 +0200181 {},
182};
183MODULE_DEVICE_TABLE(of, gen_74x164_dt_ids);
184
Miguel Gaioead6db082010-10-27 15:33:18 -0700185static struct spi_driver gen_74x164_driver = {
186 .driver = {
H Hartley Sweetena3cc68c2011-05-27 16:35:59 -0700187 .name = "74x164",
Sachin Kamat187a53a2013-09-19 17:28:08 +0530188 .of_match_table = gen_74x164_dt_ids,
Miguel Gaioead6db082010-10-27 15:33:18 -0700189 },
190 .probe = gen_74x164_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500191 .remove = gen_74x164_remove,
Miguel Gaioead6db082010-10-27 15:33:18 -0700192};
Maxime Ripardab3b8782012-09-05 10:40:50 +0200193module_spi_driver(gen_74x164_driver);
Miguel Gaioead6db082010-10-27 15:33:18 -0700194
195MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
196MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
197MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
198MODULE_LICENSE("GPL v2");