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 ad714x_spi_read(struct ad714x_chip
*chip
,
19 unsigned short reg
, unsigned short *data
, size_t len
)
21 struct spi_device
*spi
= to_spi_device(chip
->dev
);
22 struct spi_message message
;
23 struct spi_transfer xfer
[2];
27 spi_message_init(&message
);
28 memset(xfer
, 0, sizeof(xfer
));
30 chip
->xfer_buf
[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX
|
31 AD714x_SPI_READ
| reg
);
32 xfer
[0].tx_buf
= &chip
->xfer_buf
[0];
33 xfer
[0].len
= sizeof(chip
->xfer_buf
[0]);
34 spi_message_add_tail(&xfer
[0], &message
);
36 xfer
[1].rx_buf
= &chip
->xfer_buf
[1];
37 xfer
[1].len
= sizeof(chip
->xfer_buf
[1]) * len
;
38 spi_message_add_tail(&xfer
[1], &message
);
40 error
= spi_sync(spi
, &message
);
41 if (unlikely(error
)) {
42 dev_err(chip
->dev
, "SPI read error: %d\n", error
);
46 for (i
= 0; i
< len
; i
++)
47 data
[i
] = be16_to_cpu(chip
->xfer_buf
[i
+ 1]);
52 static int ad714x_spi_write(struct ad714x_chip
*chip
,
53 unsigned short reg
, unsigned short data
)
55 struct spi_device
*spi
= to_spi_device(chip
->dev
);
58 chip
->xfer_buf
[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX
| reg
);
59 chip
->xfer_buf
[1] = cpu_to_be16(data
);
61 error
= spi_write(spi
, (u8
*)chip
->xfer_buf
,
62 2 * sizeof(*chip
->xfer_buf
));
63 if (unlikely(error
)) {
64 dev_err(chip
->dev
, "SPI write error: %d\n", error
);
71 static int ad714x_spi_probe(struct spi_device
*spi
)
73 struct ad714x_chip
*chip
;
76 spi
->bits_per_word
= 8;
81 chip
= ad714x_probe(&spi
->dev
, BUS_SPI
, spi
->irq
,
82 ad714x_spi_read
, ad714x_spi_write
);
86 spi_set_drvdata(spi
, chip
);
91 static struct spi_driver ad714x_spi_driver
= {
93 .name
= "ad714x_captouch",
94 .pm
= pm_sleep_ptr(&ad714x_pm
),
96 .probe
= ad714x_spi_probe
,
99 module_spi_driver(ad714x_spi_driver
);
101 MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor SPI Bus Driver");
102 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
103 MODULE_LICENSE("GPL");