1 // SPDX-License-Identifier: GPL-2.0-only
3 * Microchip AR1020 and AR1021 driver for I2C
5 * Author: Christian Gmeiner <christian.gmeiner@gmail.com>
8 #include <linux/bitops.h>
9 #include <linux/module.h>
10 #include <linux/input.h>
12 #include <linux/i2c.h>
13 #include <linux/irq.h>
14 #include <linux/interrupt.h>
16 #define AR1021_TOUCH_PKG_SIZE 5
18 #define AR1021_MAX_X 4095
19 #define AR1021_MAX_Y 4095
21 #define AR1021_CMD 0x55
23 #define AR1021_CMD_ENABLE_TOUCH 0x12
26 struct i2c_client
*client
;
27 struct input_dev
*input
;
28 u8 data
[AR1021_TOUCH_PKG_SIZE
];
31 static irqreturn_t
ar1021_i2c_irq(int irq
, void *dev_id
)
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
;
39 retval
= i2c_master_recv(ar1021
->client
,
40 ar1021
->data
, sizeof(ar1021
->data
));
41 if (retval
!= sizeof(ar1021
->data
))
45 if (!(data
[0] & BIT(7)))
48 button
= data
[0] & BIT(0);
49 x
= ((data
[2] & 0x1f) << 7) | (data
[1] & 0x7f);
50 y
= ((data
[4] & 0x1f) << 7) | (data
[3] & 0x7f);
52 input_report_abs(input
, ABS_X
, x
);
53 input_report_abs(input
, ABS_Y
, y
);
54 input_report_key(input
, BTN_TOUCH
, button
);
61 static int ar1021_i2c_open(struct input_dev
*dev
)
63 static const u8 cmd_enable_touch
[] = {
65 0x01, /* number of bytes after this */
66 AR1021_CMD_ENABLE_TOUCH
68 struct ar1021_i2c
*ar1021
= input_get_drvdata(dev
);
69 struct i2c_client
*client
= ar1021
->client
;
72 error
= i2c_master_send(ar1021
->client
, cmd_enable_touch
,
73 sizeof(cmd_enable_touch
));
77 enable_irq(client
->irq
);
82 static void ar1021_i2c_close(struct input_dev
*dev
)
84 struct ar1021_i2c
*ar1021
= input_get_drvdata(dev
);
85 struct i2c_client
*client
= ar1021
->client
;
87 disable_irq(client
->irq
);
90 static int ar1021_i2c_probe(struct i2c_client
*client
)
92 struct ar1021_i2c
*ar1021
;
93 struct input_dev
*input
;
96 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
97 dev_err(&client
->dev
, "i2c_check_functionality error\n");
101 ar1021
= devm_kzalloc(&client
->dev
, sizeof(*ar1021
), GFP_KERNEL
);
105 input
= devm_input_allocate_device(&client
->dev
);
109 ar1021
->client
= client
;
110 ar1021
->input
= input
;
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
;
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);
123 input_set_drvdata(input
, ar1021
);
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
);
130 dev_err(&client
->dev
,
131 "Failed to enable IRQ, error: %d\n", error
);
135 error
= input_register_device(ar1021
->input
);
137 dev_err(&client
->dev
,
138 "Failed to register input device, error: %d\n", error
);
145 static int ar1021_i2c_suspend(struct device
*dev
)
147 struct i2c_client
*client
= to_i2c_client(dev
);
149 disable_irq(client
->irq
);
154 static int ar1021_i2c_resume(struct device
*dev
)
156 struct i2c_client
*client
= to_i2c_client(dev
);
158 enable_irq(client
->irq
);
163 static DEFINE_SIMPLE_DEV_PM_OPS(ar1021_i2c_pm
,
164 ar1021_i2c_suspend
, ar1021_i2c_resume
);
166 static const struct i2c_device_id ar1021_i2c_id
[] = {
170 MODULE_DEVICE_TABLE(i2c
, ar1021_i2c_id
);
172 static const struct of_device_id ar1021_i2c_of_match
[] = {
173 { .compatible
= "microchip,ar1021-i2c", },
176 MODULE_DEVICE_TABLE(of
, ar1021_i2c_of_match
);
178 static struct i2c_driver ar1021_i2c_driver
= {
180 .name
= "ar1021_i2c",
181 .pm
= pm_sleep_ptr(&ar1021_i2c_pm
),
182 .of_match_table
= ar1021_i2c_of_match
,
185 .probe
= ar1021_i2c_probe
,
186 .id_table
= ar1021_i2c_id
,
188 module_i2c_driver(ar1021_i2c_driver
);
190 MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
191 MODULE_DESCRIPTION("Microchip AR1020 and AR1021 I2C Driver");
192 MODULE_LICENSE("GPL");