Skip to content

Commit e97cf30

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 7788099 commit e97cf30

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
#define ADG1414_MAX_DEVICES 4
17+
18+
struct adg1414_state {
19+
struct spi_device *spi;
20+
struct gpio_chip chip;
21+
struct mutex lock;
22+
u8 buffer[32];
23+
24+
__be32 tx __aligned(ARCH_KMALLOC_MINALIGN);
25+
};
26+
27+
static void adg1414_set(struct gpio_chip *chip,
28+
unsigned int offset,
29+
int value)
30+
{
31+
struct adg1414_state *st = gpiochip_get_data(chip);
32+
u32 tx = 0;
33+
int i, ret;
34+
35+
struct spi_transfer xfer = {
36+
.tx_buf = &st->tx,
37+
.len = st->chip.ngpio / 8,
38+
};
39+
40+
mutex_lock(&st->lock);
41+
42+
st->buffer[offset] = value;
43+
44+
for (i = 0; i < st->chip.ngpio; i++)
45+
tx |= st->buffer[i] << i;
46+
47+
st->tx = cpu_to_be32(tx << (32 - st->chip.ngpio));
48+
49+
ret = spi_sync_transfer(st->spi, &xfer, 1);
50+
if (ret)
51+
goto out;
52+
53+
out:
54+
mutex_unlock(&st->lock);
55+
}
56+
57+
static int adg1414_get(struct gpio_chip *chip,
58+
unsigned int offset)
59+
{
60+
struct adg1414_state *st = gpiochip_get_data(chip);
61+
int value;
62+
63+
mutex_lock(&st->lock);
64+
65+
value = st->buffer[offset];
66+
67+
mutex_unlock(&st->lock);
68+
69+
return value;
70+
}
71+
72+
static int adg1414_get_direction(struct gpio_chip *chip,
73+
unsigned int offset)
74+
{
75+
return GPIO_LINE_DIRECTION_OUT;
76+
}
77+
78+
static int adg1414_probe(struct spi_device *spi)
79+
{
80+
struct adg1414_state *st;
81+
struct gpio_desc *reset;
82+
struct device *dev = &spi->dev;
83+
u32 num_devices;
84+
int ret;
85+
86+
st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
87+
if (!st)
88+
return -ENOMEM;
89+
90+
st->spi = spi;
91+
92+
/* Use reset pin to reset the device */
93+
reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
94+
if (IS_ERR(reset))
95+
return dev_err_probe(dev, PTR_ERR(reset),
96+
"Failed to get reset gpio");
97+
98+
if (reset) {
99+
fsleep(0.015);
100+
gpiod_set_value_cansleep(reset, 0);
101+
}
102+
103+
num_devices = 1;
104+
ret = device_property_read_u32(dev, "#daisy-chained-devices",
105+
&num_devices);
106+
if (!ret) {
107+
if (num_devices > ADG1414_MAX_DEVICES || num_devices < 1)
108+
return dev_err_probe(dev, ret,
109+
"Failed to get daisy-chained-devices property\n");
110+
}
111+
112+
st->chip.label = "adg1414";
113+
st->chip.parent = dev;
114+
st->chip.get_direction = adg1414_get_direction;
115+
st->chip.set = adg1414_set;
116+
st->chip.get = adg1414_get;
117+
st->chip.base = -1;
118+
st->chip.ngpio = num_devices * 8;
119+
st->chip.can_sleep = true;
120+
121+
mutex_init(&st->lock);
122+
123+
return devm_gpiochip_add_data(dev, &st->chip, st);
124+
}
125+
126+
static const struct of_device_id adg1414_of_match[] = {
127+
{ .compatible = "adi,adg1414" },
128+
{ }
129+
};
130+
MODULE_DEVICE_TABLE(of, adg1414_of_match);
131+
132+
static struct spi_driver adg1414_driver = {
133+
.driver = {
134+
.name = "adg1414",
135+
.of_match_table = adg1414_of_match,
136+
},
137+
.probe = adg1414_probe,
138+
};
139+
module_spi_driver(adg1414_driver);
140+
141+
MODULE_AUTHOR("Kim Seer Paller <[email protected]>");
142+
MODULE_DESCRIPTION("ADG1414 SPST Switch Driver");
143+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)