blob: 189bdc8e91a5d2d116cda4d429ca8474eb071c26 [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 Han97a652a2014-11-02 00:02:46 -070016static int __maybe_unused ad714x_i2c_suspend(struct device *dev)
Bryan Wu31a62962010-03-21 23:23:24 -070017{
Mark Brown6b7cfd12011-02-11 08:49:05 -080018 return ad714x_disable(i2c_get_clientdata(to_i2c_client(dev)));
Bryan Wu31a62962010-03-21 23:23:24 -070019}
20
Jingoo Han97a652a2014-11-02 00:02:46 -070021static int __maybe_unused ad714x_i2c_resume(struct device *dev)
Bryan Wu31a62962010-03-21 23:23:24 -070022{
Mark Brown6b7cfd12011-02-11 08:49:05 -080023 return ad714x_enable(i2c_get_clientdata(to_i2c_client(dev)));
Bryan Wu31a62962010-03-21 23:23:24 -070024}
Bryan Wu31a62962010-03-21 23:23:24 -070025
Mark Brown6b7cfd12011-02-11 08:49:05 -080026static SIMPLE_DEV_PM_OPS(ad714x_i2c_pm, ad714x_i2c_suspend, ad714x_i2c_resume);
27
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070028static int ad714x_i2c_write(struct ad714x_chip *chip,
29 unsigned short reg, unsigned short data)
Bryan Wu31a62962010-03-21 23:23:24 -070030{
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070031 struct i2c_client *client = to_i2c_client(chip->dev);
32 int error;
Bryan Wu31a62962010-03-21 23:23:24 -070033
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070034 chip->xfer_buf[0] = cpu_to_be16(reg);
35 chip->xfer_buf[1] = cpu_to_be16(data);
Bryan Wu31a62962010-03-21 23:23:24 -070036
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070037 error = i2c_master_send(client, (u8 *)chip->xfer_buf,
38 2 * sizeof(*chip->xfer_buf));
39 if (unlikely(error < 0)) {
40 dev_err(&client->dev, "I2C write error: %d\n", error);
41 return error;
Bryan Wu31a62962010-03-21 23:23:24 -070042 }
43
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070044 return 0;
Bryan Wu31a62962010-03-21 23:23:24 -070045}
46
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070047static int ad714x_i2c_read(struct ad714x_chip *chip,
Michael Hennerich9eff794b72011-08-22 09:45:42 -070048 unsigned short reg, unsigned short *data, size_t len)
Bryan Wu31a62962010-03-21 23:23:24 -070049{
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070050 struct i2c_client *client = to_i2c_client(chip->dev);
Michael Hennerich9eff794b72011-08-22 09:45:42 -070051 int i;
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070052 int error;
Bryan Wu31a62962010-03-21 23:23:24 -070053
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070054 chip->xfer_buf[0] = cpu_to_be16(reg);
Bryan Wu31a62962010-03-21 23:23:24 -070055
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070056 error = i2c_master_send(client, (u8 *)chip->xfer_buf,
57 sizeof(*chip->xfer_buf));
58 if (error >= 0)
59 error = i2c_master_recv(client, (u8 *)chip->xfer_buf,
Michael Hennerich9eff794b72011-08-22 09:45:42 -070060 len * sizeof(*chip->xfer_buf));
Bryan Wu31a62962010-03-21 23:23:24 -070061
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070062 if (unlikely(error < 0)) {
63 dev_err(&client->dev, "I2C read error: %d\n", error);
64 return error;
65 }
66
Michael Hennerich9eff794b72011-08-22 09:45:42 -070067 for (i = 0; i < len; i++)
68 data[i] = be16_to_cpu(chip->xfer_buf[i]);
69
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -070070 return 0;
Bryan Wu31a62962010-03-21 23:23:24 -070071}
72
Bill Pemberton5298cc42012-11-23 21:38:25 -080073static int ad714x_i2c_probe(struct i2c_client *client,
Bryan Wu31a62962010-03-21 23:23:24 -070074 const struct i2c_device_id *id)
75{
76 struct ad714x_chip *chip;
77
78 chip = ad714x_probe(&client->dev, BUS_I2C, client->irq,
79 ad714x_i2c_read, ad714x_i2c_write);
80 if (IS_ERR(chip))
81 return PTR_ERR(chip);
82
83 i2c_set_clientdata(client, chip);
84
85 return 0;
86}
87
Bill Pembertone2619cf2012-11-23 21:50:47 -080088static int ad714x_i2c_remove(struct i2c_client *client)
Bryan Wu31a62962010-03-21 23:23:24 -070089{
90 struct ad714x_chip *chip = i2c_get_clientdata(client);
91
92 ad714x_remove(chip);
Bryan Wu31a62962010-03-21 23:23:24 -070093
94 return 0;
95}
96
97static const struct i2c_device_id ad714x_id[] = {
98 { "ad7142_captouch", 0 },
Barry Song6c04d7b2010-03-21 23:23:29 -070099 { "ad7143_captouch", 0 },
Bryan Wu31a62962010-03-21 23:23:24 -0700100 { "ad7147_captouch", 0 },
Barry Song6c04d7b2010-03-21 23:23:29 -0700101 { "ad7147a_captouch", 0 },
102 { "ad7148_captouch", 0 },
Bryan Wu31a62962010-03-21 23:23:24 -0700103 { }
104};
105MODULE_DEVICE_TABLE(i2c, ad714x_id);
106
107static struct i2c_driver ad714x_i2c_driver = {
108 .driver = {
109 .name = "ad714x_captouch",
Mark Brown6b7cfd12011-02-11 08:49:05 -0800110 .pm = &ad714x_i2c_pm,
Bryan Wu31a62962010-03-21 23:23:24 -0700111 },
112 .probe = ad714x_i2c_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800113 .remove = ad714x_i2c_remove,
Bryan Wu31a62962010-03-21 23:23:24 -0700114 .id_table = ad714x_id,
115};
116
Axel Lin1b92c1c2012-03-16 23:05:41 -0700117module_i2c_driver(ad714x_i2c_driver);
Bryan Wu31a62962010-03-21 23:23:24 -0700118
119MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor I2C Bus Driver");
120MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
121MODULE_LICENSE("GPL");