Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / input / misc / ad714x-spi.c
blobeb13b4cd6594b660b7118ff97902cc2466ed780b
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * AD714X CapTouch Programmable Controller driver (SPI bus)
5 * Copyright 2009-2011 Analog Devices Inc.
6 */
8 #include <linux/input.h> /* BUS_SPI */
9 #include <linux/module.h>
10 #include <linux/spi/spi.h>
11 #include <linux/pm.h>
12 #include <linux/types.h>
13 #include "ad714x.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];
24 int i;
25 int error;
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);
43 return error;
46 for (i = 0; i < len; i++)
47 data[i] = be16_to_cpu(chip->xfer_buf[i + 1]);
49 return 0;
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);
56 int error;
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);
65 return error;
68 return 0;
71 static int ad714x_spi_probe(struct spi_device *spi)
73 struct ad714x_chip *chip;
74 int err;
76 spi->bits_per_word = 8;
77 err = spi_setup(spi);
78 if (err < 0)
79 return err;
81 chip = ad714x_probe(&spi->dev, BUS_SPI, spi->irq,
82 ad714x_spi_read, ad714x_spi_write);
83 if (IS_ERR(chip))
84 return PTR_ERR(chip);
86 spi_set_drvdata(spi, chip);
88 return 0;
91 static struct spi_driver ad714x_spi_driver = {
92 .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");