14397c98aSMike Frysinger /* 24397c98aSMike Frysinger * AD7879-1/AD7889-1 touchscreen (I2C bus) 34397c98aSMike Frysinger * 44397c98aSMike Frysinger * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc. 54397c98aSMike Frysinger * 64397c98aSMike Frysinger * Licensed under the GPL-2 or later. 74397c98aSMike Frysinger */ 84397c98aSMike Frysinger 94397c98aSMike Frysinger #include <linux/input.h> /* BUS_I2C */ 104397c98aSMike Frysinger #include <linux/i2c.h> 114397c98aSMike Frysinger #include <linux/module.h> 124397c98aSMike Frysinger #include <linux/types.h> 134397c98aSMike Frysinger 144397c98aSMike Frysinger #include "ad7879.h" 154397c98aSMike Frysinger 164397c98aSMike Frysinger #define AD7879_DEVID 0x79 /* AD7879-1/AD7889-1 */ 174397c98aSMike Frysinger 184397c98aSMike Frysinger #ifdef CONFIG_PM 194397c98aSMike Frysinger static int ad7879_i2c_suspend(struct i2c_client *client, pm_message_t message) 204397c98aSMike Frysinger { 214397c98aSMike Frysinger struct ad7879 *ts = i2c_get_clientdata(client); 224397c98aSMike Frysinger 2314fbbc36SDmitry Torokhov ad7879_suspend(ts); 244397c98aSMike Frysinger 254397c98aSMike Frysinger return 0; 264397c98aSMike Frysinger } 274397c98aSMike Frysinger 284397c98aSMike Frysinger static int ad7879_i2c_resume(struct i2c_client *client) 294397c98aSMike Frysinger { 304397c98aSMike Frysinger struct ad7879 *ts = i2c_get_clientdata(client); 314397c98aSMike Frysinger 3214fbbc36SDmitry Torokhov ad7879_resume(ts); 334397c98aSMike Frysinger 344397c98aSMike Frysinger return 0; 354397c98aSMike Frysinger } 364397c98aSMike Frysinger #else 374397c98aSMike Frysinger # define ad7879_i2c_suspend NULL 384397c98aSMike Frysinger # define ad7879_i2c_resume NULL 394397c98aSMike Frysinger #endif 404397c98aSMike Frysinger 414397c98aSMike Frysinger /* All registers are word-sized. 424397c98aSMike Frysinger * AD7879 uses a high-byte first convention. 434397c98aSMike Frysinger */ 444397c98aSMike Frysinger static int ad7879_i2c_read(struct device *dev, u8 reg) 454397c98aSMike Frysinger { 464397c98aSMike Frysinger struct i2c_client *client = to_i2c_client(dev); 474397c98aSMike Frysinger 484397c98aSMike Frysinger return swab16(i2c_smbus_read_word_data(client, reg)); 494397c98aSMike Frysinger } 504397c98aSMike Frysinger 514397c98aSMike Frysinger static int ad7879_i2c_multi_read(struct device *dev, 524397c98aSMike Frysinger u8 first_reg, u8 count, u16 *buf) 534397c98aSMike Frysinger { 544397c98aSMike Frysinger u8 idx; 554397c98aSMike Frysinger 564397c98aSMike Frysinger for (idx = 0; idx < count; ++idx) 574397c98aSMike Frysinger buf[idx] = ad7879_i2c_read(dev, first_reg + idx); 584397c98aSMike Frysinger 594397c98aSMike Frysinger return 0; 604397c98aSMike Frysinger } 614397c98aSMike Frysinger 624397c98aSMike Frysinger static int ad7879_i2c_write(struct device *dev, u8 reg, u16 val) 634397c98aSMike Frysinger { 644397c98aSMike Frysinger struct i2c_client *client = to_i2c_client(dev); 654397c98aSMike Frysinger 664397c98aSMike Frysinger return i2c_smbus_write_word_data(client, reg, swab16(val)); 674397c98aSMike Frysinger } 684397c98aSMike Frysinger 694397c98aSMike Frysinger static const struct ad7879_bus_ops ad7879_i2c_bus_ops = { 704397c98aSMike Frysinger .bustype = BUS_I2C, 714397c98aSMike Frysinger .read = ad7879_i2c_read, 724397c98aSMike Frysinger .multi_read = ad7879_i2c_multi_read, 734397c98aSMike Frysinger .write = ad7879_i2c_write, 744397c98aSMike Frysinger }; 754397c98aSMike Frysinger 764397c98aSMike Frysinger static int __devinit ad7879_i2c_probe(struct i2c_client *client, 774397c98aSMike Frysinger const struct i2c_device_id *id) 784397c98aSMike Frysinger { 794397c98aSMike Frysinger struct ad7879 *ts; 804397c98aSMike Frysinger 814397c98aSMike Frysinger if (!i2c_check_functionality(client->adapter, 824397c98aSMike Frysinger I2C_FUNC_SMBUS_WORD_DATA)) { 834397c98aSMike Frysinger dev_err(&client->dev, "SMBUS Word Data not Supported\n"); 844397c98aSMike Frysinger return -EIO; 854397c98aSMike Frysinger } 864397c98aSMike Frysinger 874397c98aSMike Frysinger ts = ad7879_probe(&client->dev, AD7879_DEVID, client->irq, 884397c98aSMike Frysinger &ad7879_i2c_bus_ops); 894397c98aSMike Frysinger if (IS_ERR(ts)) 904397c98aSMike Frysinger return PTR_ERR(ts); 914397c98aSMike Frysinger 924397c98aSMike Frysinger i2c_set_clientdata(client, ts); 934397c98aSMike Frysinger 944397c98aSMike Frysinger return 0; 954397c98aSMike Frysinger } 964397c98aSMike Frysinger 974397c98aSMike Frysinger static int __devexit ad7879_i2c_remove(struct i2c_client *client) 984397c98aSMike Frysinger { 994397c98aSMike Frysinger struct ad7879 *ts = i2c_get_clientdata(client); 1004397c98aSMike Frysinger 1014397c98aSMike Frysinger ad7879_remove(ts); 1024397c98aSMike Frysinger 1034397c98aSMike Frysinger return 0; 1044397c98aSMike Frysinger } 1054397c98aSMike Frysinger 1064397c98aSMike Frysinger static const struct i2c_device_id ad7879_id[] = { 1074397c98aSMike Frysinger { "ad7879", 0 }, 1084397c98aSMike Frysinger { "ad7889", 0 }, 1094397c98aSMike Frysinger { } 1104397c98aSMike Frysinger }; 1114397c98aSMike Frysinger MODULE_DEVICE_TABLE(i2c, ad7879_id); 1124397c98aSMike Frysinger 1134397c98aSMike Frysinger static struct i2c_driver ad7879_i2c_driver = { 1144397c98aSMike Frysinger .driver = { 1154397c98aSMike Frysinger .name = "ad7879", 1164397c98aSMike Frysinger .owner = THIS_MODULE, 1174397c98aSMike Frysinger }, 1184397c98aSMike Frysinger .probe = ad7879_i2c_probe, 1194397c98aSMike Frysinger .remove = __devexit_p(ad7879_i2c_remove), 1204397c98aSMike Frysinger .suspend = ad7879_i2c_suspend, 1214397c98aSMike Frysinger .resume = ad7879_i2c_resume, 1224397c98aSMike Frysinger .id_table = ad7879_id, 1234397c98aSMike Frysinger }; 1244397c98aSMike Frysinger 1254397c98aSMike Frysinger static int __init ad7879_i2c_init(void) 1264397c98aSMike Frysinger { 1274397c98aSMike Frysinger return i2c_add_driver(&ad7879_i2c_driver); 1284397c98aSMike Frysinger } 1294397c98aSMike Frysinger module_init(ad7879_i2c_init); 1304397c98aSMike Frysinger 1314397c98aSMike Frysinger static void __exit ad7879_i2c_exit(void) 1324397c98aSMike Frysinger { 1334397c98aSMike Frysinger i2c_del_driver(&ad7879_i2c_driver); 1344397c98aSMike Frysinger } 1354397c98aSMike Frysinger module_exit(ad7879_i2c_exit); 1364397c98aSMike Frysinger 1374397c98aSMike Frysinger MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 1384397c98aSMike Frysinger MODULE_DESCRIPTION("AD7879(-1) touchscreen I2C bus driver"); 1394397c98aSMike Frysinger MODULE_LICENSE("GPL"); 1404397c98aSMike Frysinger MODULE_ALIAS("i2c:ad7879"); 141