blob: e0f522516ef54b87f4ba879780a170b3b52edb13 [file] [log] [blame]
Bryan Wu31a62962010-03-21 23:23:24 -07001/*
2 * AD714X CapTouch Programmable Controller driver (I2C bus)
3 *
Michael Hennerich9eff794b72011-08-22 09:45:42 -07004 * Copyright 2009-2011 Analog Devices Inc.
Bryan Wu31a62962010-03-21 23:23:24 -07005 *
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
Jingoo Han84c0c9f2013-03-26 21:38:52 -070016#ifdef CONFIG_PM_SLEEP
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;
Bryan Wu31a62962010-03-21 23:23:24 -070044 }
45
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070046 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,
Michael Hennerich9eff794b72011-08-22 09:45:42 -070050 unsigned short reg, unsigned short *data, size_t len)
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);
Michael Hennerich9eff794b72011-08-22 09:45:42 -070053 int i;
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070054 int error;
Bryan Wu31a62962010-03-21 23:23:24 -070055
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070056 chip->xfer_buf[0] = cpu_to_be16(reg);
Bryan Wu31a62962010-03-21 23:23:24 -070057
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070058 error = i2c_master_send(client, (u8 *)chip->xfer_buf,
59 sizeof(*chip->xfer_buf));
60 if (error >= 0)
61 error = i2c_master_recv(client, (u8 *)chip->xfer_buf,
Michael Hennerich9eff794b72011-08-22 09:45:42 -070062 len * sizeof(*chip->xfer_buf));
Bryan Wu31a62962010-03-21 23:23:24 -070063
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070064 if (unlikely(error < 0)) {
65 dev_err(&client->dev, "I2C read error: %d\n", error);
66 return error;
67 }
68
Michael Hennerich9eff794b72011-08-22 09:45:42 -070069 for (i = 0; i < len; i++)
70 data[i] = be16_to_cpu(chip->xfer_buf[i]);
71
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070072 return 0;
Bryan Wu31a62962010-03-21 23:23:24 -070073}
74
Bill Pemberton5298cc42012-11-23 21:38:25 -080075static int ad714x_i2c_probe(struct i2c_client *client,
Bryan Wu31a62962010-03-21 23:23:24 -070076 const struct i2c_device_id *id)
77{
78 struct ad714x_chip *chip;
79
80 chip = ad714x_probe(&client->dev, BUS_I2C, client->irq,
81 ad714x_i2c_read, ad714x_i2c_write);
82 if (IS_ERR(chip))
83 return PTR_ERR(chip);
84
85 i2c_set_clientdata(client, chip);
86
87 return 0;
88}
89
Bill Pembertone2619cf2012-11-23 21:50:47 -080090static int ad714x_i2c_remove(struct i2c_client *client)
Bryan Wu31a62962010-03-21 23:23:24 -070091{
92 struct ad714x_chip *chip = i2c_get_clientdata(client);
93
94 ad714x_remove(chip);
Bryan Wu31a62962010-03-21 23:23:24 -070095
96 return 0;
97}
98
99static const struct i2c_device_id ad714x_id[] = {
100 { "ad7142_captouch", 0 },
Barry Song6c04d7b2010-03-21 23:23:29 -0700101 { "ad7143_captouch", 0 },
Bryan Wu31a62962010-03-21 23:23:24 -0700102 { "ad7147_captouch", 0 },
Barry Song6c04d7b2010-03-21 23:23:29 -0700103 { "ad7147a_captouch", 0 },
104 { "ad7148_captouch", 0 },
Bryan Wu31a62962010-03-21 23:23:24 -0700105 { }
106};
107MODULE_DEVICE_TABLE(i2c, ad714x_id);
108
109static struct i2c_driver ad714x_i2c_driver = {
110 .driver = {
111 .name = "ad714x_captouch",
Mark Brown6b7cfd12011-02-11 08:49:05 -0800112 .pm = &ad714x_i2c_pm,
Bryan Wu31a62962010-03-21 23:23:24 -0700113 },
114 .probe = ad714x_i2c_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800115 .remove = ad714x_i2c_remove,
Bryan Wu31a62962010-03-21 23:23:24 -0700116 .id_table = ad714x_id,
117};
118
Axel Lin1b92c1c2012-03-16 23:05:41 -0700119module_i2c_driver(ad714x_i2c_driver);
Bryan Wu31a62962010-03-21 23:23:24 -0700120
121MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor I2C Bus Driver");
122MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
123MODULE_LICENSE("GPL");