Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Microchip AR1020 and AR1021 driver for I2C
4 *
5 * Author: Christian Gmeiner <christian.gmeiner@gmail.com>
6 */
7
8#include <linux/bitops.h>
9#include <linux/module.h>
10#include <linux/input.h>
11#include <linux/of.h>
12#include <linux/i2c.h>
13#include <linux/irq.h>
14#include <linux/interrupt.h>
15
16#define AR1021_TOUCH_PKG_SIZE 5
17
18#define AR1021_MAX_X 4095
19#define AR1021_MAX_Y 4095
20
21#define AR1021_CMD 0x55
22
23#define AR1021_CMD_ENABLE_TOUCH 0x12
24
25struct ar1021_i2c {
26 struct i2c_client *client;
27 struct input_dev *input;
28 u8 data[AR1021_TOUCH_PKG_SIZE];
29};
30
31static irqreturn_t ar1021_i2c_irq(int irq, void *dev_id)
32{
33 struct ar1021_i2c *ar1021 = dev_id;
34 struct input_dev *input = ar1021->input;
35 u8 *data = ar1021->data;
36 unsigned int x, y, button;
37 int retval;
38
39 retval = i2c_master_recv(ar1021->client,
40 ar1021->data, sizeof(ar1021->data));
41 if (retval != sizeof(ar1021->data))
42 goto out;
43
44 /* sync bit set ? */
45 if (!(data[0] & BIT(7)))
46 goto out;
47
48 button = data[0] & BIT(0);
49 x = ((data[2] & 0x1f) << 7) | (data[1] & 0x7f);
50 y = ((data[4] & 0x1f) << 7) | (data[3] & 0x7f);
51
52 input_report_abs(input, ABS_X, x);
53 input_report_abs(input, ABS_Y, y);
54 input_report_key(input, BTN_TOUCH, button);
55 input_sync(input);
56
57out:
58 return IRQ_HANDLED;
59}
60
61static int ar1021_i2c_open(struct input_dev *dev)
62{
63 static const u8 cmd_enable_touch[] = {
64 AR1021_CMD,
65 0x01, /* number of bytes after this */
66 AR1021_CMD_ENABLE_TOUCH
67 };
68 struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
69 struct i2c_client *client = ar1021->client;
70 int error;
71
72 error = i2c_master_send(ar1021->client, cmd_enable_touch,
73 sizeof(cmd_enable_touch));
74 if (error < 0)
75 return error;
76
77 enable_irq(client->irq);
78
79 return 0;
80}
81
82static void ar1021_i2c_close(struct input_dev *dev)
83{
84 struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
85 struct i2c_client *client = ar1021->client;
86
87 disable_irq(client->irq);
88}
89
90static int ar1021_i2c_probe(struct i2c_client *client)
91{
92 struct ar1021_i2c *ar1021;
93 struct input_dev *input;
94 int error;
95
96 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
97 dev_err(&client->dev, "i2c_check_functionality error\n");
98 return -ENXIO;
99 }
100
101 ar1021 = devm_kzalloc(&client->dev, sizeof(*ar1021), GFP_KERNEL);
102 if (!ar1021)
103 return -ENOMEM;
104
105 input = devm_input_allocate_device(&client->dev);
106 if (!input)
107 return -ENOMEM;
108
109 ar1021->client = client;
110 ar1021->input = input;
111
112 input->name = "ar1021 I2C Touchscreen";
113 input->id.bustype = BUS_I2C;
114 input->dev.parent = &client->dev;
115 input->open = ar1021_i2c_open;
116 input->close = ar1021_i2c_close;
117
118 __set_bit(INPUT_PROP_DIRECT, input->propbit);
119 input_set_capability(input, EV_KEY, BTN_TOUCH);
120 input_set_abs_params(input, ABS_X, 0, AR1021_MAX_X, 0, 0);
121 input_set_abs_params(input, ABS_Y, 0, AR1021_MAX_Y, 0, 0);
122
123 input_set_drvdata(input, ar1021);
124
125 error = devm_request_threaded_irq(&client->dev, client->irq,
126 NULL, ar1021_i2c_irq,
127 IRQF_ONESHOT | IRQF_NO_AUTOEN,
128 "ar1021_i2c", ar1021);
129 if (error) {
130 dev_err(&client->dev,
131 "Failed to enable IRQ, error: %d\n", error);
132 return error;
133 }
134
135 error = input_register_device(ar1021->input);
136 if (error) {
137 dev_err(&client->dev,
138 "Failed to register input device, error: %d\n", error);
139 return error;
140 }
141
142 return 0;
143}
144
145static int ar1021_i2c_suspend(struct device *dev)
146{
147 struct i2c_client *client = to_i2c_client(dev);
148
149 disable_irq(client->irq);
150
151 return 0;
152}
153
154static int ar1021_i2c_resume(struct device *dev)
155{
156 struct i2c_client *client = to_i2c_client(dev);
157
158 enable_irq(client->irq);
159
160 return 0;
161}
162
163static DEFINE_SIMPLE_DEV_PM_OPS(ar1021_i2c_pm,
164 ar1021_i2c_suspend, ar1021_i2c_resume);
165
166static const struct i2c_device_id ar1021_i2c_id[] = {
167 { "ar1021", 0 },
168 { },
169};
170MODULE_DEVICE_TABLE(i2c, ar1021_i2c_id);
171
172static const struct of_device_id ar1021_i2c_of_match[] = {
173 { .compatible = "microchip,ar1021-i2c", },
174 { }
175};
176MODULE_DEVICE_TABLE(of, ar1021_i2c_of_match);
177
178static struct i2c_driver ar1021_i2c_driver = {
179 .driver = {
180 .name = "ar1021_i2c",
181 .pm = pm_sleep_ptr(&ar1021_i2c_pm),
182 .of_match_table = ar1021_i2c_of_match,
183 },
184
185 .probe = ar1021_i2c_probe,
186 .id_table = ar1021_i2c_id,
187};
188module_i2c_driver(ar1021_i2c_driver);
189
190MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
191MODULE_DESCRIPTION("Microchip AR1020 and AR1021 I2C Driver");
192MODULE_LICENSE("GPL");
1/*
2 * Microchip AR1020 and AR1021 driver for I2C
3 *
4 * Author: Christian Gmeiner <christian.gmeiner@gmail.com>
5 *
6 * License: GPLv2 as published by the FSF.
7 */
8
9#include <linux/bitops.h>
10#include <linux/module.h>
11#include <linux/input.h>
12#include <linux/of.h>
13#include <linux/i2c.h>
14#include <linux/irq.h>
15#include <linux/interrupt.h>
16
17#define AR1021_TOCUH_PKG_SIZE 5
18
19#define AR1021_MAX_X 4095
20#define AR1021_MAX_Y 4095
21
22#define AR1021_CMD 0x55
23
24#define AR1021_CMD_ENABLE_TOUCH 0x12
25
26struct ar1021_i2c {
27 struct i2c_client *client;
28 struct input_dev *input;
29 u8 data[AR1021_TOCUH_PKG_SIZE];
30};
31
32static irqreturn_t ar1021_i2c_irq(int irq, void *dev_id)
33{
34 struct ar1021_i2c *ar1021 = dev_id;
35 struct input_dev *input = ar1021->input;
36 u8 *data = ar1021->data;
37 unsigned int x, y, button;
38 int retval;
39
40 retval = i2c_master_recv(ar1021->client,
41 ar1021->data, sizeof(ar1021->data));
42 if (retval != sizeof(ar1021->data))
43 goto out;
44
45 /* sync bit set ? */
46 if (!(data[0] & BIT(7)))
47 goto out;
48
49 button = data[0] & BIT(0);
50 x = ((data[2] & 0x1f) << 7) | (data[1] & 0x7f);
51 y = ((data[4] & 0x1f) << 7) | (data[3] & 0x7f);
52
53 input_report_abs(input, ABS_X, x);
54 input_report_abs(input, ABS_Y, y);
55 input_report_key(input, BTN_TOUCH, button);
56 input_sync(input);
57
58out:
59 return IRQ_HANDLED;
60}
61
62static int ar1021_i2c_open(struct input_dev *dev)
63{
64 static const u8 cmd_enable_touch[] = {
65 AR1021_CMD,
66 0x01, /* number of bytes after this */
67 AR1021_CMD_ENABLE_TOUCH
68 };
69 struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
70 struct i2c_client *client = ar1021->client;
71 int error;
72
73 error = i2c_master_send(ar1021->client, cmd_enable_touch,
74 sizeof(cmd_enable_touch));
75 if (error < 0)
76 return error;
77
78 enable_irq(client->irq);
79
80 return 0;
81}
82
83static void ar1021_i2c_close(struct input_dev *dev)
84{
85 struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
86 struct i2c_client *client = ar1021->client;
87
88 disable_irq(client->irq);
89}
90
91static int ar1021_i2c_probe(struct i2c_client *client,
92 const struct i2c_device_id *id)
93{
94 struct ar1021_i2c *ar1021;
95 struct input_dev *input;
96 int error;
97
98 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
99 dev_err(&client->dev, "i2c_check_functionality error\n");
100 return -ENXIO;
101 }
102
103 ar1021 = devm_kzalloc(&client->dev, sizeof(*ar1021), GFP_KERNEL);
104 if (!ar1021)
105 return -ENOMEM;
106
107 input = devm_input_allocate_device(&client->dev);
108 if (!input)
109 return -ENOMEM;
110
111 ar1021->client = client;
112 ar1021->input = input;
113
114 input->name = "ar1021 I2C Touchscreen";
115 input->id.bustype = BUS_I2C;
116 input->dev.parent = &client->dev;
117 input->open = ar1021_i2c_open;
118 input->close = ar1021_i2c_close;
119
120 __set_bit(INPUT_PROP_DIRECT, input->propbit);
121 input_set_capability(input, EV_KEY, BTN_TOUCH);
122 input_set_abs_params(input, ABS_X, 0, AR1021_MAX_X, 0, 0);
123 input_set_abs_params(input, ABS_Y, 0, AR1021_MAX_Y, 0, 0);
124
125 input_set_drvdata(input, ar1021);
126
127 error = devm_request_threaded_irq(&client->dev, client->irq,
128 NULL, ar1021_i2c_irq,
129 IRQF_ONESHOT,
130 "ar1021_i2c", ar1021);
131 if (error) {
132 dev_err(&client->dev,
133 "Failed to enable IRQ, error: %d\n", error);
134 return error;
135 }
136
137 /* Disable the IRQ, we'll enable it in ar1021_i2c_open() */
138 disable_irq(client->irq);
139
140 error = input_register_device(ar1021->input);
141 if (error) {
142 dev_err(&client->dev,
143 "Failed to register input device, error: %d\n", error);
144 return error;
145 }
146
147 return 0;
148}
149
150static int __maybe_unused ar1021_i2c_suspend(struct device *dev)
151{
152 struct i2c_client *client = to_i2c_client(dev);
153
154 disable_irq(client->irq);
155
156 return 0;
157}
158
159static int __maybe_unused ar1021_i2c_resume(struct device *dev)
160{
161 struct i2c_client *client = to_i2c_client(dev);
162
163 enable_irq(client->irq);
164
165 return 0;
166}
167
168static SIMPLE_DEV_PM_OPS(ar1021_i2c_pm, ar1021_i2c_suspend, ar1021_i2c_resume);
169
170static const struct i2c_device_id ar1021_i2c_id[] = {
171 { "ar1021", 0 },
172 { },
173};
174MODULE_DEVICE_TABLE(i2c, ar1021_i2c_id);
175
176static const struct of_device_id ar1021_i2c_of_match[] = {
177 { .compatible = "microchip,ar1021-i2c", },
178 { }
179};
180MODULE_DEVICE_TABLE(of, ar1021_i2c_of_match);
181
182static struct i2c_driver ar1021_i2c_driver = {
183 .driver = {
184 .name = "ar1021_i2c",
185 .pm = &ar1021_i2c_pm,
186 .of_match_table = ar1021_i2c_of_match,
187 },
188
189 .probe = ar1021_i2c_probe,
190 .id_table = ar1021_i2c_id,
191};
192module_i2c_driver(ar1021_i2c_driver);
193
194MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
195MODULE_DESCRIPTION("Microchip AR1020 and AR1021 I2C Driver");
196MODULE_LICENSE("GPL");