treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / misc / ad714x-spi.c
blob7d3bf434620f0e39ad286d28c8a224bf6af223b7
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 __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];
36 int i;
37 int error;
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);
55 return error;
58 for (i = 0; i < len; i++)
59 data[i] = be16_to_cpu(chip->xfer_buf[i + 1]);
61 return 0;
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);
68 int error;
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);
77 return error;
80 return 0;
83 static int ad714x_spi_probe(struct spi_device *spi)
85 struct ad714x_chip *chip;
86 int err;
88 spi->bits_per_word = 8;
89 err = spi_setup(spi);
90 if (err < 0)
91 return err;
93 chip = ad714x_probe(&spi->dev, BUS_SPI, spi->irq,
94 ad714x_spi_read, ad714x_spi_write);
95 if (IS_ERR(chip))
96 return PTR_ERR(chip);
98 spi_set_drvdata(spi, chip);
100 return 0;
103 static struct spi_driver ad714x_spi_driver = {
104 .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");