Skip to content

Commit 56060ba

Browse files
committed
gpio: gpio-adg1414: new driver
The ADG1414 is a 9.5 Ω RON ±15 V/+12 V/±5 V iCMOS Serially-Controlled Octal SPST Switches Signed-off-by: Kim Seer Paller <[email protected]>
1 parent 0e35a62 commit 56060ba

3 files changed

Lines changed: 135 additions & 0 deletions

File tree

drivers/gpio/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,16 @@ config GPIO_74X164
16061606
shift registers. This driver can be used to provide access
16071607
to more GPIO outputs.
16081608

1609+
config GPIO_ADG1414
1610+
tristate "ADG1414 SPST Switch Driver"
1611+
depends on GPIOLIB && SPI
1612+
help
1613+
Say yes here to build support for Analog Devices ADG1414 SPST
1614+
Switch Driver
1615+
1616+
To compile this driver as a module, choose M here: the
1617+
module will be called gpio-adg1414.
1618+
16091619
config GPIO_ADI_DAQ1
16101620
tristate "Analog Devices AD-FMCDAQ1-EBZ SPI-GPIO expander driver"
16111621
help

drivers/gpio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ obj-$(CONFIG_GPIO_104_IDI_48) += gpio-104-idi-48.o
2323
obj-$(CONFIG_GPIO_104_IDIO_16) += gpio-104-idio-16.o
2424
obj-$(CONFIG_GPIO_74X164) += gpio-74x164.o
2525
obj-$(CONFIG_GPIO_74XX_MMIO) += gpio-74xx-mmio.o
26+
obj-$(CONFIG_GPIO_ADG1414) += gpio-adg1414.o
2627
obj-$(CONFIG_GPIO_ADI_DAQ1) += gpio-adi-daq1.o
2728
obj-$(CONFIG_GPIO_ADNP) += gpio-adnp.o
2829
obj-$(CONFIG_GPIO_ADP5520) += gpio-adp5520.o

drivers/gpio/gpio-adg1414.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* ADG1414 SPST Switch Driver
4+
*
5+
* Copyright 2023 Analog Devices Inc.
6+
*/
7+
8+
#include <linux/delay.h>
9+
#include <linux/gpio/consumer.h>
10+
#include <linux/gpio/driver.h>
11+
#include <linux/spi/spi.h>
12+
#include <linux/module.h>
13+
#include <linux/mutex.h>
14+
#include <linux/property.h>
15+
16+
struct adg1414_state {
17+
struct spi_device *spi;
18+
struct gpio_chip chip;
19+
u8 buffer[32];
20+
21+
__be32 tx __aligned(ARCH_KMALLOC_MINALIGN);
22+
};
23+
24+
static void adg1414_set(struct gpio_chip *chip,
25+
unsigned int offset,
26+
int value)
27+
{
28+
struct adg1414_state *st = gpiochip_get_data(chip);
29+
int i;
30+
u32 tx = 0;
31+
32+
struct spi_transfer xfer = {
33+
.tx_buf = &st->tx,
34+
.len = st->chip.ngpio / 8,
35+
};
36+
37+
st->buffer[offset] = value;
38+
39+
for (i = 0; i < st->chip.ngpio; i++)
40+
tx |= st->buffer[i] << i;
41+
42+
st->tx = cpu_to_be32(tx << (32 - st->chip.ngpio));
43+
44+
spi_sync_transfer(st->spi, &xfer, 1);
45+
}
46+
47+
static int adg1414_get(struct gpio_chip *chip,
48+
unsigned int offset)
49+
{
50+
struct adg1414_state *st = gpiochip_get_data(chip);
51+
52+
return st->buffer[offset];
53+
}
54+
55+
static int adg1414_get_direction(struct gpio_chip *chip,
56+
unsigned int offset)
57+
{
58+
return GPIO_LINE_DIRECTION_OUT;
59+
}
60+
61+
static int adg1414_probe(struct spi_device *spi)
62+
{
63+
struct adg1414_state *st;
64+
struct gpio_desc *reset;
65+
struct device *dev = &spi->dev;
66+
u32 num_devices;
67+
int ret;
68+
69+
st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
70+
if (!st)
71+
return -ENOMEM;
72+
73+
st->spi = spi;
74+
75+
/* Use reset pin to reset the device */
76+
reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
77+
if (IS_ERR(reset))
78+
return dev_err_probe(dev, PTR_ERR(reset), "Failed to get reset gpio");
79+
80+
if (reset) {
81+
ndelay(15);
82+
gpiod_set_value_cansleep(reset, 1);
83+
}
84+
85+
num_devices = 1;
86+
ret = device_property_read_u32(dev, "daisy-chained-devices",
87+
&num_devices);
88+
if (!ret) {
89+
if (num_devices > 4 || num_devices < 1)
90+
return dev_err_probe(dev, ret,
91+
"Failed to get daisy-chained-devices property\n");
92+
}
93+
94+
st->chip.label = "adg1414";
95+
st->chip.parent = dev;
96+
st->chip.owner = THIS_MODULE;
97+
st->chip.get_direction = adg1414_get_direction;
98+
st->chip.set = adg1414_set;
99+
st->chip.get = adg1414_get;
100+
st->chip.base = -1;
101+
st->chip.ngpio = num_devices * 8;
102+
st->chip.can_sleep = true;
103+
104+
return devm_gpiochip_add_data(dev, &st->chip, st);
105+
}
106+
107+
static const struct of_device_id adg1414_of_match[] = {
108+
{ .compatible = "adi,adg1414" },
109+
{ }
110+
};
111+
MODULE_DEVICE_TABLE(of, adg1414_of_match);
112+
113+
static struct spi_driver adg1414_driver = {
114+
.driver = {
115+
.name = "adg1414",
116+
.of_match_table = adg1414_of_match,
117+
},
118+
.probe = adg1414_probe,
119+
};
120+
module_spi_driver(adg1414_driver);
121+
122+
MODULE_AUTHOR("Kim Seer Paller <[email protected]>");
123+
MODULE_DESCRIPTION("ADG1414 SPST Switch Driver");
124+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)