Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * AD714X CapTouch Programmable Controller driver (SPI bus) |
| 3 | * |
| 4 | * Copyright 2009 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/input.h> /* BUS_I2C */ |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/spi/spi.h> |
Mark Brown | a257090 | 2011-02-11 08:49:37 -0800 | [diff] [blame] | 12 | #include <linux/pm.h> |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 13 | #include <linux/types.h> |
| 14 | #include "ad714x.h" |
| 15 | |
| 16 | #define AD714x_SPI_CMD_PREFIX 0xE000 /* bits 15:11 */ |
| 17 | #define AD714x_SPI_READ BIT(10) |
| 18 | |
| 19 | #ifdef CONFIG_PM |
Mark Brown | a257090 | 2011-02-11 08:49:37 -0800 | [diff] [blame] | 20 | static int ad714x_spi_suspend(struct device *dev) |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 21 | { |
Mark Brown | a257090 | 2011-02-11 08:49:37 -0800 | [diff] [blame] | 22 | return ad714x_disable(spi_get_drvdata(to_spi_device(dev))); |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Mark Brown | a257090 | 2011-02-11 08:49:37 -0800 | [diff] [blame] | 25 | static int ad714x_spi_resume(struct device *dev) |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 26 | { |
Mark Brown | a257090 | 2011-02-11 08:49:37 -0800 | [diff] [blame] | 27 | return ad714x_enable(spi_get_drvdata(to_spi_device(dev))); |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 28 | } |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 29 | #endif |
| 30 | |
Mark Brown | a257090 | 2011-02-11 08:49:37 -0800 | [diff] [blame] | 31 | static SIMPLE_DEV_PM_OPS(ad714x_spi_pm, ad714x_spi_suspend, ad714x_spi_resume); |
| 32 | |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 33 | static int ad714x_spi_read(struct device *dev, unsigned short reg, |
| 34 | unsigned short *data) |
| 35 | { |
| 36 | struct spi_device *spi = to_spi_device(dev); |
| 37 | unsigned short tx = AD714x_SPI_CMD_PREFIX | AD714x_SPI_READ | reg; |
| 38 | |
| 39 | return spi_write_then_read(spi, (u8 *)&tx, 2, (u8 *)data, 2); |
| 40 | } |
| 41 | |
| 42 | static int ad714x_spi_write(struct device *dev, unsigned short reg, |
| 43 | unsigned short data) |
| 44 | { |
| 45 | struct spi_device *spi = to_spi_device(dev); |
| 46 | unsigned short tx[2] = { |
| 47 | AD714x_SPI_CMD_PREFIX | reg, |
| 48 | data |
| 49 | }; |
| 50 | |
| 51 | return spi_write(spi, (u8 *)tx, 4); |
| 52 | } |
| 53 | |
| 54 | static int __devinit ad714x_spi_probe(struct spi_device *spi) |
| 55 | { |
| 56 | struct ad714x_chip *chip; |
Michael Hennerich | 5b9063b | 2011-08-21 21:04:12 -0700 | [diff] [blame^] | 57 | int err; |
| 58 | |
| 59 | spi->bits_per_word = 8; |
| 60 | err = spi_setup(spi); |
| 61 | if (err < 0) |
| 62 | return err; |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 63 | |
| 64 | chip = ad714x_probe(&spi->dev, BUS_SPI, spi->irq, |
| 65 | ad714x_spi_read, ad714x_spi_write); |
| 66 | if (IS_ERR(chip)) |
| 67 | return PTR_ERR(chip); |
| 68 | |
| 69 | spi_set_drvdata(spi, chip); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int __devexit ad714x_spi_remove(struct spi_device *spi) |
| 75 | { |
| 76 | struct ad714x_chip *chip = spi_get_drvdata(spi); |
| 77 | |
| 78 | ad714x_remove(chip); |
| 79 | spi_set_drvdata(spi, NULL); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static struct spi_driver ad714x_spi_driver = { |
| 85 | .driver = { |
| 86 | .name = "ad714x_captouch", |
| 87 | .owner = THIS_MODULE, |
Mark Brown | a257090 | 2011-02-11 08:49:37 -0800 | [diff] [blame] | 88 | .pm = &ad714x_spi_pm, |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 89 | }, |
| 90 | .probe = ad714x_spi_probe, |
| 91 | .remove = __devexit_p(ad714x_spi_remove), |
Bryan Wu | 31a6296 | 2010-03-21 23:23:24 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static __init int ad714x_spi_init(void) |
| 95 | { |
| 96 | return spi_register_driver(&ad714x_spi_driver); |
| 97 | } |
| 98 | module_init(ad714x_spi_init); |
| 99 | |
| 100 | static __exit void ad714x_spi_exit(void) |
| 101 | { |
| 102 | spi_unregister_driver(&ad714x_spi_driver); |
| 103 | } |
| 104 | module_exit(ad714x_spi_exit); |
| 105 | |
| 106 | MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor SPI Bus Driver"); |
| 107 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 108 | MODULE_LICENSE("GPL"); |