Loading...
1/*
2 * AD714X CapTouch Programmable Controller driver (I2C bus)
3 *
4 * Copyright 2009-2011 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
16static int __maybe_unused ad714x_i2c_suspend(struct device *dev)
17{
18 return ad714x_disable(i2c_get_clientdata(to_i2c_client(dev)));
19}
20
21static int __maybe_unused ad714x_i2c_resume(struct device *dev)
22{
23 return ad714x_enable(i2c_get_clientdata(to_i2c_client(dev)));
24}
25
26static SIMPLE_DEV_PM_OPS(ad714x_i2c_pm, ad714x_i2c_suspend, ad714x_i2c_resume);
27
28static int ad714x_i2c_write(struct ad714x_chip *chip,
29 unsigned short reg, unsigned short data)
30{
31 struct i2c_client *client = to_i2c_client(chip->dev);
32 int error;
33
34 chip->xfer_buf[0] = cpu_to_be16(reg);
35 chip->xfer_buf[1] = cpu_to_be16(data);
36
37 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;
42 }
43
44 return 0;
45}
46
47static int ad714x_i2c_read(struct ad714x_chip *chip,
48 unsigned short reg, unsigned short *data, size_t len)
49{
50 struct i2c_client *client = to_i2c_client(chip->dev);
51 int i;
52 int error;
53
54 chip->xfer_buf[0] = cpu_to_be16(reg);
55
56 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,
60 len * sizeof(*chip->xfer_buf));
61
62 if (unlikely(error < 0)) {
63 dev_err(&client->dev, "I2C read error: %d\n", error);
64 return error;
65 }
66
67 for (i = 0; i < len; i++)
68 data[i] = be16_to_cpu(chip->xfer_buf[i]);
69
70 return 0;
71}
72
73static int ad714x_i2c_probe(struct i2c_client *client,
74 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
88static const struct i2c_device_id ad714x_id[] = {
89 { "ad7142_captouch", 0 },
90 { "ad7143_captouch", 0 },
91 { "ad7147_captouch", 0 },
92 { "ad7147a_captouch", 0 },
93 { "ad7148_captouch", 0 },
94 { }
95};
96MODULE_DEVICE_TABLE(i2c, ad714x_id);
97
98static struct i2c_driver ad714x_i2c_driver = {
99 .driver = {
100 .name = "ad714x_captouch",
101 .pm = &ad714x_i2c_pm,
102 },
103 .probe = ad714x_i2c_probe,
104 .id_table = ad714x_id,
105};
106
107module_i2c_driver(ad714x_i2c_driver);
108
109MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor I2C Bus Driver");
110MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
111MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * AD714X CapTouch Programmable Controller driver (I2C bus)
4 *
5 * Copyright 2009-2011 Analog Devices Inc.
6 */
7
8#include <linux/input.h> /* BUS_I2C */
9#include <linux/i2c.h>
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/pm.h>
13#include "ad714x.h"
14
15static int ad714x_i2c_write(struct ad714x_chip *chip,
16 unsigned short reg, unsigned short data)
17{
18 struct i2c_client *client = to_i2c_client(chip->dev);
19 int error;
20
21 chip->xfer_buf[0] = cpu_to_be16(reg);
22 chip->xfer_buf[1] = cpu_to_be16(data);
23
24 error = i2c_master_send(client, (u8 *)chip->xfer_buf,
25 2 * sizeof(*chip->xfer_buf));
26 if (unlikely(error < 0)) {
27 dev_err(&client->dev, "I2C write error: %d\n", error);
28 return error;
29 }
30
31 return 0;
32}
33
34static int ad714x_i2c_read(struct ad714x_chip *chip,
35 unsigned short reg, unsigned short *data, size_t len)
36{
37 struct i2c_client *client = to_i2c_client(chip->dev);
38 int i;
39 int error;
40
41 chip->xfer_buf[0] = cpu_to_be16(reg);
42
43 error = i2c_master_send(client, (u8 *)chip->xfer_buf,
44 sizeof(*chip->xfer_buf));
45 if (error >= 0)
46 error = i2c_master_recv(client, (u8 *)chip->xfer_buf,
47 len * sizeof(*chip->xfer_buf));
48
49 if (unlikely(error < 0)) {
50 dev_err(&client->dev, "I2C read error: %d\n", error);
51 return error;
52 }
53
54 for (i = 0; i < len; i++)
55 data[i] = be16_to_cpu(chip->xfer_buf[i]);
56
57 return 0;
58}
59
60static int ad714x_i2c_probe(struct i2c_client *client)
61{
62 struct ad714x_chip *chip;
63
64 chip = ad714x_probe(&client->dev, BUS_I2C, client->irq,
65 ad714x_i2c_read, ad714x_i2c_write);
66 if (IS_ERR(chip))
67 return PTR_ERR(chip);
68
69 i2c_set_clientdata(client, chip);
70
71 return 0;
72}
73
74static const struct i2c_device_id ad714x_id[] = {
75 { "ad7142_captouch" },
76 { "ad7143_captouch" },
77 { "ad7147_captouch" },
78 { "ad7147a_captouch" },
79 { "ad7148_captouch" },
80 { }
81};
82MODULE_DEVICE_TABLE(i2c, ad714x_id);
83
84static struct i2c_driver ad714x_i2c_driver = {
85 .driver = {
86 .name = "ad714x_captouch",
87 .pm = pm_sleep_ptr(&ad714x_pm),
88 },
89 .probe = ad714x_i2c_probe,
90 .id_table = ad714x_id,
91};
92
93module_i2c_driver(ad714x_i2c_driver);
94
95MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor I2C Bus Driver");
96MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
97MODULE_LICENSE("GPL");