Skip to content

Commit cdb150c

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 <kimseer.paller@analog.com>
1 parent e410a34 commit cdb150c

File tree

3 files changed

+151
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)