blob: 6c6121865f0ed644c6694058ecbcc5b16366bdbc [file] [log] [blame]
Bryan Wu31a62962010-03-21 23:23:24 -07001/*
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>
Mark Brown6b7cfd12011-02-11 08:49:05 -080013#include <linux/pm.h>
Bryan Wu31a62962010-03-21 23:23:24 -070014#include "ad714x.h"
15
16#ifdef CONFIG_PM
Mark Brown6b7cfd12011-02-11 08:49:05 -080017static int ad714x_i2c_suspend(struct device *dev)
Bryan Wu31a62962010-03-21 23:23:24 -070018{
Mark Brown6b7cfd12011-02-11 08:49:05 -080019 return ad714x_disable(i2c_get_clientdata(to_i2c_client(dev)));
Bryan Wu31a62962010-03-21 23:23:24 -070020}
21
Mark Brown6b7cfd12011-02-11 08:49:05 -080022static int ad714x_i2c_resume(struct device *dev)
Bryan Wu31a62962010-03-21 23:23:24 -070023{
Mark Brown6b7cfd12011-02-11 08:49:05 -080024 return ad714x_enable(i2c_get_clientdata(to_i2c_client(dev)));
Bryan Wu31a62962010-03-21 23:23:24 -070025}
Bryan Wu31a62962010-03-21 23:23:24 -070026#endif
27
Mark Brown6b7cfd12011-02-11 08:49:05 -080028static SIMPLE_DEV_PM_OPS(ad714x_i2c_pm, ad714x_i2c_suspend, ad714x_i2c_resume);
29
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070030static int ad714x_i2c_write(struct ad714x_chip *chip,
31 unsigned short reg, unsigned short data)
Bryan Wu31a62962010-03-21 23:23:24 -070032{
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070033 struct i2c_client *client = to_i2c_client(chip->dev);
34 int error;
Bryan Wu31a62962010-03-21 23:23:24 -070035
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070036 chip->xfer_buf[0] = cpu_to_be16(reg);
37 chip->xfer_buf[1] = cpu_to_be16(data);
Bryan Wu31a62962010-03-21 23:23:24 -070038
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070039 error = i2c_master_send(client, (u8 *)chip->xfer_buf,
40 2 * sizeof(*chip->xfer_buf));
41 if (unlikely(error < 0)) {
42 dev_err(&client->dev, "I2C write error: %d\n", error);
43 return error;
44 }
45
46 return 0;
Bryan Wu31a62962010-03-21 23:23:24 -070047}
48
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070049static int ad714x_i2c_read(struct ad714x_chip *chip,
50 unsigned short reg, unsigned short *data)
Bryan Wu31a62962010-03-21 23:23:24 -070051{
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070052 struct i2c_client *client = to_i2c_client(chip->dev);
53 int error;
Bryan Wu31a62962010-03-21 23:23:24 -070054
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070055 chip->xfer_buf[0] = cpu_to_be16(reg);
Bryan Wu31a62962010-03-21 23:23:24 -070056
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070057 error = i2c_master_send(client, (u8 *)chip->xfer_buf,
58 sizeof(*chip->xfer_buf));
59 if (error >= 0)
60 error = i2c_master_recv(client, (u8 *)chip->xfer_buf,
61 sizeof(*chip->xfer_buf));
Bryan Wu31a62962010-03-21 23:23:24 -070062
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070063 if (unlikely(error < 0)) {
64 dev_err(&client->dev, "I2C read error: %d\n", error);
65 return error;
66 }
67
68 *data = be16_to_cpup(chip->xfer_buf);
69 return 0;
Bryan Wu31a62962010-03-21 23:23:24 -070070}
71
72static int __devinit ad714x_i2c_probe(struct i2c_client *client,
73 const struct i2c_device_id *id)
74{
75 struct ad714x_chip *chip;
76
77 chip = ad714x_probe(&client->dev, BUS_I2C, client->irq,
78 ad714x_i2c_read, ad714x_i2c_write);
79 if (IS_ERR(chip))
80 return PTR_ERR(chip);
81
82 i2c_set_clientdata(client, chip);
83
84 return 0;
85}
86
87static int __devexit ad714x_i2c_remove(struct i2c_client *client)
88{
89 struct ad714x_chip *chip = i2c_get_clientdata(client);
90
91 ad714x_remove(chip);
Bryan Wu31a62962010-03-21 23:23:24 -070092
93 return 0;
94}
95
96static const struct i2c_device_id ad714x_id[] = {
97 { "ad7142_captouch", 0 },
Barry Song6c04d7b2010-03-21 23:23:29 -070098 { "ad7143_captouch", 0 },
Bryan Wu31a62962010-03-21 23:23:24 -070099 { "ad7147_captouch", 0 },
Barry Song6c04d7b2010-03-21 23:23:29 -0700100 { "ad7147a_captouch", 0 },
101 { "ad7148_captouch", 0 },
Bryan Wu31a62962010-03-21 23:23:24 -0700102 { }
103};
104MODULE_DEVICE_TABLE(i2c, ad714x_id);
105
106static struct i2c_driver ad714x_i2c_driver = {
107 .driver = {
108 .name = "ad714x_captouch",
Mark Brown6b7cfd12011-02-11 08:49:05 -0800109 .pm = &ad714x_i2c_pm,
Bryan Wu31a62962010-03-21 23:23:24 -0700110 },
111 .probe = ad714x_i2c_probe,
112 .remove = __devexit_p(ad714x_i2c_remove),
Bryan Wu31a62962010-03-21 23:23:24 -0700113 .id_table = ad714x_id,
114};
115
116static __init int ad714x_i2c_init(void)
117{
118 return i2c_add_driver(&ad714x_i2c_driver);
119}
120module_init(ad714x_i2c_init);
121
122static __exit void ad714x_i2c_exit(void)
123{
124 i2c_del_driver(&ad714x_i2c_driver);
125}
126module_exit(ad714x_i2c_exit);
127
128MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor I2C Bus Driver");
129MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
130MODULE_LICENSE("GPL");