180503b23SThomas 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 18c0409febSDmitry Torokhov static int ad714x_spi_read(struct ad714x_chip *chip, 199eff794bSMichael Hennerich unsigned short reg, unsigned short *data, size_t len) 2031a62963SBryan Wu { 21c0409febSDmitry Torokhov struct spi_device *spi = to_spi_device(chip->dev); 22c0409febSDmitry Torokhov struct spi_message message; 23c0409febSDmitry Torokhov struct spi_transfer xfer[2]; 249eff794bSMichael Hennerich int i; 25c0409febSDmitry Torokhov int error; 26c0409febSDmitry Torokhov 27c0409febSDmitry Torokhov spi_message_init(&message); 28c0409febSDmitry Torokhov memset(xfer, 0, sizeof(xfer)); 29c0409febSDmitry Torokhov 30c0409febSDmitry Torokhov chip->xfer_buf[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX | 316337de22SMichael Hennerich AD714x_SPI_READ | reg); 32c0409febSDmitry Torokhov xfer[0].tx_buf = &chip->xfer_buf[0]; 33c0409febSDmitry Torokhov xfer[0].len = sizeof(chip->xfer_buf[0]); 34c0409febSDmitry Torokhov spi_message_add_tail(&xfer[0], &message); 3531a62963SBryan Wu 36c0409febSDmitry Torokhov xfer[1].rx_buf = &chip->xfer_buf[1]; 379eff794bSMichael Hennerich xfer[1].len = sizeof(chip->xfer_buf[1]) * len; 38c0409febSDmitry Torokhov spi_message_add_tail(&xfer[1], &message); 396337de22SMichael Hennerich 40c0409febSDmitry Torokhov error = spi_sync(spi, &message); 41c0409febSDmitry Torokhov if (unlikely(error)) { 42c0409febSDmitry Torokhov dev_err(chip->dev, "SPI read error: %d\n", error); 43c0409febSDmitry Torokhov return error; 4431a62963SBryan Wu } 4531a62963SBryan Wu 469eff794bSMichael Hennerich for (i = 0; i < len; i++) 479eff794bSMichael Hennerich data[i] = be16_to_cpu(chip->xfer_buf[i + 1]); 489eff794bSMichael Hennerich 49c0409febSDmitry Torokhov return 0; 50c0409febSDmitry Torokhov } 51c0409febSDmitry Torokhov 52c0409febSDmitry Torokhov static int ad714x_spi_write(struct ad714x_chip *chip, 536337de22SMichael Hennerich unsigned short reg, unsigned short data) 5431a62963SBryan Wu { 55c0409febSDmitry Torokhov struct spi_device *spi = to_spi_device(chip->dev); 56c0409febSDmitry Torokhov int error; 5731a62963SBryan Wu 58c0409febSDmitry Torokhov chip->xfer_buf[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX | reg); 59c0409febSDmitry Torokhov chip->xfer_buf[1] = cpu_to_be16(data); 60c0409febSDmitry Torokhov 61c0409febSDmitry Torokhov error = spi_write(spi, (u8 *)chip->xfer_buf, 62c0409febSDmitry Torokhov 2 * sizeof(*chip->xfer_buf)); 63c0409febSDmitry Torokhov if (unlikely(error)) { 64c0409febSDmitry Torokhov dev_err(chip->dev, "SPI write error: %d\n", error); 65c0409febSDmitry Torokhov return error; 66c0409febSDmitry Torokhov } 67c0409febSDmitry Torokhov 68c0409febSDmitry Torokhov return 0; 6931a62963SBryan Wu } 7031a62963SBryan Wu 715298cc4cSBill Pemberton static int ad714x_spi_probe(struct spi_device *spi) 7231a62963SBryan Wu { 7331a62963SBryan Wu struct ad714x_chip *chip; 745b9063b1SMichael Hennerich int err; 755b9063b1SMichael Hennerich 765b9063b1SMichael Hennerich spi->bits_per_word = 8; 775b9063b1SMichael Hennerich err = spi_setup(spi); 785b9063b1SMichael Hennerich if (err < 0) 795b9063b1SMichael Hennerich return err; 8031a62963SBryan Wu 8131a62963SBryan Wu chip = ad714x_probe(&spi->dev, BUS_SPI, spi->irq, 8231a62963SBryan Wu ad714x_spi_read, ad714x_spi_write); 8331a62963SBryan Wu if (IS_ERR(chip)) 8431a62963SBryan Wu return PTR_ERR(chip); 8531a62963SBryan Wu 8631a62963SBryan Wu spi_set_drvdata(spi, chip); 8731a62963SBryan Wu 8831a62963SBryan Wu return 0; 8931a62963SBryan Wu } 9031a62963SBryan Wu 9131a62963SBryan Wu static struct spi_driver ad714x_spi_driver = { 9231a62963SBryan Wu .driver = { 9331a62963SBryan Wu .name = "ad714x_captouch", 94*c0a150eeSJonathan Cameron .pm = pm_sleep_ptr(&ad714x_pm), 9531a62963SBryan Wu }, 9631a62963SBryan Wu .probe = ad714x_spi_probe, 9731a62963SBryan Wu }; 9831a62963SBryan Wu 99ca83922eSAxel Lin module_spi_driver(ad714x_spi_driver); 10031a62963SBryan Wu 10131a62963SBryan Wu MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor SPI Bus Driver"); 10231a62963SBryan Wu MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); 10331a62963SBryan Wu MODULE_LICENSE("GPL"); 104