1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AD714X CapTouch Programmable Controller driver (SPI bus)
5 * Copyright 2009-2011 Analog Devices Inc.
8 #include <linux/input.h> /* BUS_SPI */
9 #include <linux/module.h>
10 #include <linux/spi/spi.h>
12 #include <linux/types.h>
15 #define AD714x_SPI_CMD_PREFIX 0xE000 /* bits 15:11 */
16 #define AD714x_SPI_READ BIT(10)
18 static int __maybe_unused
ad714x_spi_suspend(struct device
*dev
)
20 return ad714x_disable(spi_get_drvdata(to_spi_device(dev
)));
23 static int __maybe_unused
ad714x_spi_resume(struct device
*dev
)
25 return ad714x_enable(spi_get_drvdata(to_spi_device(dev
)));
28 static SIMPLE_DEV_PM_OPS(ad714x_spi_pm
, ad714x_spi_suspend
, ad714x_spi_resume
);
30 static int ad714x_spi_read(struct ad714x_chip
*chip
,
31 unsigned short reg
, unsigned short *data
, size_t len
)
33 struct spi_device
*spi
= to_spi_device(chip
->dev
);
34 struct spi_message message
;
35 struct spi_transfer xfer
[2];
39 spi_message_init(&message
);
40 memset(xfer
, 0, sizeof(xfer
));
42 chip
->xfer_buf
[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX
|
43 AD714x_SPI_READ
| reg
);
44 xfer
[0].tx_buf
= &chip
->xfer_buf
[0];
45 xfer
[0].len
= sizeof(chip
->xfer_buf
[0]);
46 spi_message_add_tail(&xfer
[0], &message
);
48 xfer
[1].rx_buf
= &chip
->xfer_buf
[1];
49 xfer
[1].len
= sizeof(chip
->xfer_buf
[1]) * len
;
50 spi_message_add_tail(&xfer
[1], &message
);
52 error
= spi_sync(spi
, &message
);
53 if (unlikely(error
)) {
54 dev_err(chip
->dev
, "SPI read error: %d\n", error
);
58 for (i
= 0; i
< len
; i
++)
59 data
[i
] = be16_to_cpu(chip
->xfer_buf
[i
+ 1]);
64 static int ad714x_spi_write(struct ad714x_chip
*chip
,
65 unsigned short reg
, unsigned short data
)
67 struct spi_device
*spi
= to_spi_device(chip
->dev
);
70 chip
->xfer_buf
[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX
| reg
);
71 chip
->xfer_buf
[1] = cpu_to_be16(data
);
73 error
= spi_write(spi
, (u8
*)chip
->xfer_buf
,
74 2 * sizeof(*chip
->xfer_buf
));
75 if (unlikely(error
)) {
76 dev_err(chip
->dev
, "SPI write error: %d\n", error
);
83 static int ad714x_spi_probe(struct spi_device
*spi
)
85 struct ad714x_chip
*chip
;
88 spi
->bits_per_word
= 8;
93 chip
= ad714x_probe(&spi
->dev
, BUS_SPI
, spi
->irq
,
94 ad714x_spi_read
, ad714x_spi_write
);
98 spi_set_drvdata(spi
, chip
);
103 static struct spi_driver ad714x_spi_driver
= {
105 .name
= "ad714x_captouch",
106 .pm
= &ad714x_spi_pm
,
108 .probe
= ad714x_spi_probe
,
111 module_spi_driver(ad714x_spi_driver
);
113 MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor SPI Bus Driver");
114 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
115 MODULE_LICENSE("GPL");