1*80503b23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 231a62963SBryan Wu /* 331a62963SBryan Wu * AD714X CapTouch Programmable Controller driver (SPI bus) 431a62963SBryan Wu * 59eff794bSMichael Hennerich * Copyright 2009-2011 Analog Devices Inc. 631a62963SBryan Wu */ 731a62963SBryan Wu 86337de22SMichael Hennerich #include <linux/input.h> /* BUS_SPI */ 931a62963SBryan Wu #include <linux/module.h> 1031a62963SBryan Wu #include <linux/spi/spi.h> 11a257090cSMark Brown #include <linux/pm.h> 1231a62963SBryan Wu #include <linux/types.h> 1331a62963SBryan Wu #include "ad714x.h" 1431a62963SBryan Wu 1531a62963SBryan Wu #define AD714x_SPI_CMD_PREFIX 0xE000 /* bits 15:11 */ 1631a62963SBryan Wu #define AD714x_SPI_READ BIT(10) 1731a62963SBryan Wu 1897a652a8SJingoo Han static int __maybe_unused ad714x_spi_suspend(struct device *dev) 1931a62963SBryan Wu { 20a257090cSMark Brown return ad714x_disable(spi_get_drvdata(to_spi_device(dev))); 2131a62963SBryan Wu } 2231a62963SBryan Wu 2397a652a8SJingoo Han static int __maybe_unused ad714x_spi_resume(struct device *dev) 2431a62963SBryan Wu { 25a257090cSMark Brown return ad714x_enable(spi_get_drvdata(to_spi_device(dev))); 2631a62963SBryan Wu } 2731a62963SBryan Wu 28a257090cSMark Brown static SIMPLE_DEV_PM_OPS(ad714x_spi_pm, ad714x_spi_suspend, ad714x_spi_resume); 29a257090cSMark Brown 30c0409febSDmitry Torokhov static int ad714x_spi_read(struct ad714x_chip *chip, 319eff794bSMichael Hennerich unsigned short reg, unsigned short *data, size_t len) 3231a62963SBryan Wu { 33c0409febSDmitry Torokhov struct spi_device *spi = to_spi_device(chip->dev); 34c0409febSDmitry Torokhov struct spi_message message; 35c0409febSDmitry Torokhov struct spi_transfer xfer[2]; 369eff794bSMichael Hennerich int i; 37c0409febSDmitry Torokhov int error; 38c0409febSDmitry Torokhov 39c0409febSDmitry Torokhov spi_message_init(&message); 40c0409febSDmitry Torokhov memset(xfer, 0, sizeof(xfer)); 41c0409febSDmitry Torokhov 42c0409febSDmitry Torokhov chip->xfer_buf[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX | 436337de22SMichael Hennerich AD714x_SPI_READ | reg); 44c0409febSDmitry Torokhov xfer[0].tx_buf = &chip->xfer_buf[0]; 45c0409febSDmitry Torokhov xfer[0].len = sizeof(chip->xfer_buf[0]); 46c0409febSDmitry Torokhov spi_message_add_tail(&xfer[0], &message); 4731a62963SBryan Wu 48c0409febSDmitry Torokhov xfer[1].rx_buf = &chip->xfer_buf[1]; 499eff794bSMichael Hennerich xfer[1].len = sizeof(chip->xfer_buf[1]) * len; 50c0409febSDmitry Torokhov spi_message_add_tail(&xfer[1], &message); 516337de22SMichael Hennerich 52c0409febSDmitry Torokhov error = spi_sync(spi, &message); 53c0409febSDmitry Torokhov if (unlikely(error)) { 54c0409febSDmitry Torokhov dev_err(chip->dev, "SPI read error: %d\n", error); 55c0409febSDmitry Torokhov return error; 5631a62963SBryan Wu } 5731a62963SBryan Wu 589eff794bSMichael Hennerich for (i = 0; i < len; i++) 599eff794bSMichael Hennerich data[i] = be16_to_cpu(chip->xfer_buf[i + 1]); 609eff794bSMichael Hennerich 61c0409febSDmitry Torokhov return 0; 62c0409febSDmitry Torokhov } 63c0409febSDmitry Torokhov 64c0409febSDmitry Torokhov static int ad714x_spi_write(struct ad714x_chip *chip, 656337de22SMichael Hennerich unsigned short reg, unsigned short data) 6631a62963SBryan Wu { 67c0409febSDmitry Torokhov struct spi_device *spi = to_spi_device(chip->dev); 68c0409febSDmitry Torokhov int error; 6931a62963SBryan Wu 70c0409febSDmitry Torokhov chip->xfer_buf[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX | reg); 71c0409febSDmitry Torokhov chip->xfer_buf[1] = cpu_to_be16(data); 72c0409febSDmitry Torokhov 73c0409febSDmitry Torokhov error = spi_write(spi, (u8 *)chip->xfer_buf, 74c0409febSDmitry Torokhov 2 * sizeof(*chip->xfer_buf)); 75c0409febSDmitry Torokhov if (unlikely(error)) { 76c0409febSDmitry Torokhov dev_err(chip->dev, "SPI write error: %d\n", error); 77c0409febSDmitry Torokhov return error; 78c0409febSDmitry Torokhov } 79c0409febSDmitry Torokhov 80c0409febSDmitry Torokhov return 0; 8131a62963SBryan Wu } 8231a62963SBryan Wu 835298cc4cSBill Pemberton static int ad714x_spi_probe(struct spi_device *spi) 8431a62963SBryan Wu { 8531a62963SBryan Wu struct ad714x_chip *chip; 865b9063b1SMichael Hennerich int err; 875b9063b1SMichael Hennerich 885b9063b1SMichael Hennerich spi->bits_per_word = 8; 895b9063b1SMichael Hennerich err = spi_setup(spi); 905b9063b1SMichael Hennerich if (err < 0) 915b9063b1SMichael Hennerich return err; 9231a62963SBryan Wu 9331a62963SBryan Wu chip = ad714x_probe(&spi->dev, BUS_SPI, spi->irq, 9431a62963SBryan Wu ad714x_spi_read, ad714x_spi_write); 9531a62963SBryan Wu if (IS_ERR(chip)) 9631a62963SBryan Wu return PTR_ERR(chip); 9731a62963SBryan Wu 9831a62963SBryan Wu spi_set_drvdata(spi, chip); 9931a62963SBryan Wu 10031a62963SBryan Wu return 0; 10131a62963SBryan Wu } 10231a62963SBryan Wu 10331a62963SBryan Wu static struct spi_driver ad714x_spi_driver = { 10431a62963SBryan Wu .driver = { 10531a62963SBryan Wu .name = "ad714x_captouch", 106a257090cSMark Brown .pm = &ad714x_spi_pm, 10731a62963SBryan Wu }, 10831a62963SBryan Wu .probe = ad714x_spi_probe, 10931a62963SBryan Wu }; 11031a62963SBryan Wu 111ca83922eSAxel Lin module_spi_driver(ad714x_spi_driver); 11231a62963SBryan Wu 11331a62963SBryan Wu MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor SPI Bus Driver"); 11431a62963SBryan Wu MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); 11531a62963SBryan Wu MODULE_LICENSE("GPL"); 116