131a62963SBryan Wu /* 231a62963SBryan Wu * AD714X CapTouch Programmable Controller driver (SPI bus) 331a62963SBryan Wu * 4*9eff794bSMichael Hennerich * Copyright 2009-2011 Analog Devices Inc. 531a62963SBryan Wu * 631a62963SBryan Wu * Licensed under the GPL-2 or later. 731a62963SBryan Wu */ 831a62963SBryan Wu 96337de22SMichael Hennerich #include <linux/input.h> /* BUS_SPI */ 1031a62963SBryan Wu #include <linux/module.h> 1131a62963SBryan Wu #include <linux/spi/spi.h> 12a257090cSMark Brown #include <linux/pm.h> 1331a62963SBryan Wu #include <linux/types.h> 1431a62963SBryan Wu #include "ad714x.h" 1531a62963SBryan Wu 1631a62963SBryan Wu #define AD714x_SPI_CMD_PREFIX 0xE000 /* bits 15:11 */ 1731a62963SBryan Wu #define AD714x_SPI_READ BIT(10) 1831a62963SBryan Wu 1931a62963SBryan Wu #ifdef CONFIG_PM 20a257090cSMark Brown static int ad714x_spi_suspend(struct device *dev) 2131a62963SBryan Wu { 22a257090cSMark Brown return ad714x_disable(spi_get_drvdata(to_spi_device(dev))); 2331a62963SBryan Wu } 2431a62963SBryan Wu 25a257090cSMark Brown static int ad714x_spi_resume(struct device *dev) 2631a62963SBryan Wu { 27a257090cSMark Brown return ad714x_enable(spi_get_drvdata(to_spi_device(dev))); 2831a62963SBryan Wu } 2931a62963SBryan Wu #endif 3031a62963SBryan Wu 31a257090cSMark Brown static SIMPLE_DEV_PM_OPS(ad714x_spi_pm, ad714x_spi_suspend, ad714x_spi_resume); 32a257090cSMark Brown 33c0409febSDmitry Torokhov static int ad714x_spi_read(struct ad714x_chip *chip, 34*9eff794bSMichael Hennerich unsigned short reg, unsigned short *data, size_t len) 3531a62963SBryan Wu { 36c0409febSDmitry Torokhov struct spi_device *spi = to_spi_device(chip->dev); 37c0409febSDmitry Torokhov struct spi_message message; 38c0409febSDmitry Torokhov struct spi_transfer xfer[2]; 39*9eff794bSMichael Hennerich int i; 40c0409febSDmitry Torokhov int error; 41c0409febSDmitry Torokhov 42c0409febSDmitry Torokhov spi_message_init(&message); 43c0409febSDmitry Torokhov memset(xfer, 0, sizeof(xfer)); 44c0409febSDmitry Torokhov 45c0409febSDmitry Torokhov chip->xfer_buf[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX | 466337de22SMichael Hennerich AD714x_SPI_READ | reg); 47c0409febSDmitry Torokhov xfer[0].tx_buf = &chip->xfer_buf[0]; 48c0409febSDmitry Torokhov xfer[0].len = sizeof(chip->xfer_buf[0]); 49c0409febSDmitry Torokhov spi_message_add_tail(&xfer[0], &message); 5031a62963SBryan Wu 51c0409febSDmitry Torokhov xfer[1].rx_buf = &chip->xfer_buf[1]; 52*9eff794bSMichael Hennerich xfer[1].len = sizeof(chip->xfer_buf[1]) * len; 53c0409febSDmitry Torokhov spi_message_add_tail(&xfer[1], &message); 546337de22SMichael Hennerich 55c0409febSDmitry Torokhov error = spi_sync(spi, &message); 56c0409febSDmitry Torokhov if (unlikely(error)) { 57c0409febSDmitry Torokhov dev_err(chip->dev, "SPI read error: %d\n", error); 58c0409febSDmitry Torokhov return error; 5931a62963SBryan Wu } 6031a62963SBryan Wu 61*9eff794bSMichael Hennerich for (i = 0; i < len; i++) 62*9eff794bSMichael Hennerich data[i] = be16_to_cpu(chip->xfer_buf[i + 1]); 63*9eff794bSMichael Hennerich 64c0409febSDmitry Torokhov return 0; 65c0409febSDmitry Torokhov } 66c0409febSDmitry Torokhov 67c0409febSDmitry Torokhov static int ad714x_spi_write(struct ad714x_chip *chip, 686337de22SMichael Hennerich unsigned short reg, unsigned short data) 6931a62963SBryan Wu { 70c0409febSDmitry Torokhov struct spi_device *spi = to_spi_device(chip->dev); 71c0409febSDmitry Torokhov int error; 7231a62963SBryan Wu 73c0409febSDmitry Torokhov chip->xfer_buf[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX | reg); 74c0409febSDmitry Torokhov chip->xfer_buf[1] = cpu_to_be16(data); 75c0409febSDmitry Torokhov 76c0409febSDmitry Torokhov error = spi_write(spi, (u8 *)chip->xfer_buf, 77c0409febSDmitry Torokhov 2 * sizeof(*chip->xfer_buf)); 78c0409febSDmitry Torokhov if (unlikely(error)) { 79c0409febSDmitry Torokhov dev_err(chip->dev, "SPI write error: %d\n", error); 80c0409febSDmitry Torokhov return error; 81c0409febSDmitry Torokhov } 82c0409febSDmitry Torokhov 83c0409febSDmitry Torokhov return 0; 8431a62963SBryan Wu } 8531a62963SBryan Wu 8631a62963SBryan Wu static int __devinit ad714x_spi_probe(struct spi_device *spi) 8731a62963SBryan Wu { 8831a62963SBryan Wu struct ad714x_chip *chip; 895b9063b1SMichael Hennerich int err; 905b9063b1SMichael Hennerich 915b9063b1SMichael Hennerich spi->bits_per_word = 8; 925b9063b1SMichael Hennerich err = spi_setup(spi); 935b9063b1SMichael Hennerich if (err < 0) 945b9063b1SMichael Hennerich return err; 9531a62963SBryan Wu 9631a62963SBryan Wu chip = ad714x_probe(&spi->dev, BUS_SPI, spi->irq, 9731a62963SBryan Wu ad714x_spi_read, ad714x_spi_write); 9831a62963SBryan Wu if (IS_ERR(chip)) 9931a62963SBryan Wu return PTR_ERR(chip); 10031a62963SBryan Wu 10131a62963SBryan Wu spi_set_drvdata(spi, chip); 10231a62963SBryan Wu 10331a62963SBryan Wu return 0; 10431a62963SBryan Wu } 10531a62963SBryan Wu 10631a62963SBryan Wu static int __devexit ad714x_spi_remove(struct spi_device *spi) 10731a62963SBryan Wu { 10831a62963SBryan Wu struct ad714x_chip *chip = spi_get_drvdata(spi); 10931a62963SBryan Wu 11031a62963SBryan Wu ad714x_remove(chip); 11131a62963SBryan Wu spi_set_drvdata(spi, NULL); 11231a62963SBryan Wu 11331a62963SBryan Wu return 0; 11431a62963SBryan Wu } 11531a62963SBryan Wu 11631a62963SBryan Wu static struct spi_driver ad714x_spi_driver = { 11731a62963SBryan Wu .driver = { 11831a62963SBryan Wu .name = "ad714x_captouch", 11931a62963SBryan Wu .owner = THIS_MODULE, 120a257090cSMark Brown .pm = &ad714x_spi_pm, 12131a62963SBryan Wu }, 12231a62963SBryan Wu .probe = ad714x_spi_probe, 12331a62963SBryan Wu .remove = __devexit_p(ad714x_spi_remove), 12431a62963SBryan Wu }; 12531a62963SBryan Wu 12631a62963SBryan Wu static __init int ad714x_spi_init(void) 12731a62963SBryan Wu { 12831a62963SBryan Wu return spi_register_driver(&ad714x_spi_driver); 12931a62963SBryan Wu } 13031a62963SBryan Wu module_init(ad714x_spi_init); 13131a62963SBryan Wu 13231a62963SBryan Wu static __exit void ad714x_spi_exit(void) 13331a62963SBryan Wu { 13431a62963SBryan Wu spi_unregister_driver(&ad714x_spi_driver); 13531a62963SBryan Wu } 13631a62963SBryan Wu module_exit(ad714x_spi_exit); 13731a62963SBryan Wu 13831a62963SBryan Wu MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor SPI Bus Driver"); 13931a62963SBryan Wu MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); 14031a62963SBryan Wu MODULE_LICENSE("GPL"); 141