]> Pileus Git - ~andy/linux/blob - drivers/input/misc/ad714x-i2c.c
Input: ad714x - fix endianness issues
[~andy/linux] / drivers / input / misc / ad714x-i2c.c
1 /*
2  * AD714X CapTouch Programmable Controller driver (I2C 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/i2c.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/pm.h>
14 #include "ad714x.h"
15
16 #ifdef CONFIG_PM
17 static int ad714x_i2c_suspend(struct device *dev)
18 {
19         return ad714x_disable(i2c_get_clientdata(to_i2c_client(dev)));
20 }
21
22 static int ad714x_i2c_resume(struct device *dev)
23 {
24         return ad714x_enable(i2c_get_clientdata(to_i2c_client(dev)));
25 }
26 #endif
27
28 static SIMPLE_DEV_PM_OPS(ad714x_i2c_pm, ad714x_i2c_suspend, ad714x_i2c_resume);
29
30 static int ad714x_i2c_write(struct device *dev, unsigned short reg,
31                                 unsigned short data)
32 {
33         struct i2c_client *client = to_i2c_client(dev);
34         int ret = 0;
35         unsigned short tx[2] = {
36                 cpu_to_be16(reg),
37                 cpu_to_be16(data)
38         };
39
40         ret = i2c_master_send(client, (u8 *)tx, 4);
41         if (ret < 0)
42                 dev_err(&client->dev, "I2C write error\n");
43
44         return ret;
45 }
46
47 static int ad714x_i2c_read(struct device *dev, unsigned short reg,
48                                 unsigned short *data)
49 {
50         struct i2c_client *client = to_i2c_client(dev);
51         int ret = 0;
52         unsigned short tx = cpu_to_be16(reg);
53
54         ret = i2c_master_send(client, (u8 *)&tx, 2);
55         if (ret >= 0)
56                 ret = i2c_master_recv(client, (u8 *)data, 2);
57
58         if (unlikely(ret < 0))
59                 dev_err(&client->dev, "I2C read error\n");
60         else
61                 *data = be16_to_cpu(*data);
62
63         return ret;
64 }
65
66 static int __devinit ad714x_i2c_probe(struct i2c_client *client,
67                                         const struct i2c_device_id *id)
68 {
69         struct ad714x_chip *chip;
70
71         chip = ad714x_probe(&client->dev, BUS_I2C, client->irq,
72                             ad714x_i2c_read, ad714x_i2c_write);
73         if (IS_ERR(chip))
74                 return PTR_ERR(chip);
75
76         i2c_set_clientdata(client, chip);
77
78         return 0;
79 }
80
81 static int __devexit ad714x_i2c_remove(struct i2c_client *client)
82 {
83         struct ad714x_chip *chip = i2c_get_clientdata(client);
84
85         ad714x_remove(chip);
86
87         return 0;
88 }
89
90 static const struct i2c_device_id ad714x_id[] = {
91         { "ad7142_captouch", 0 },
92         { "ad7143_captouch", 0 },
93         { "ad7147_captouch", 0 },
94         { "ad7147a_captouch", 0 },
95         { "ad7148_captouch", 0 },
96         { }
97 };
98 MODULE_DEVICE_TABLE(i2c, ad714x_id);
99
100 static struct i2c_driver ad714x_i2c_driver = {
101         .driver = {
102                 .name = "ad714x_captouch",
103                 .pm   = &ad714x_i2c_pm,
104         },
105         .probe    = ad714x_i2c_probe,
106         .remove   = __devexit_p(ad714x_i2c_remove),
107         .id_table = ad714x_id,
108 };
109
110 static __init int ad714x_i2c_init(void)
111 {
112         return i2c_add_driver(&ad714x_i2c_driver);
113 }
114 module_init(ad714x_i2c_init);
115
116 static __exit void ad714x_i2c_exit(void)
117 {
118         i2c_del_driver(&ad714x_i2c_driver);
119 }
120 module_exit(ad714x_i2c_exit);
121
122 MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor I2C Bus Driver");
123 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
124 MODULE_LICENSE("GPL");