1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/bitfield.h>
4 #include <linux/bits.h>
5 #include <linux/delay.h>
7 #include <linux/input.h>
8 #include <linux/input/mt.h>
9 #include <linux/input/touchscreen.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
13 #include <linux/regulator/consumer.h>
15 #define IST30XX_REG_STATUS 0x20
16 #define IST30XX_REG_CHIPID (0x40000000 | IST3038C_DIRECT_ACCESS)
18 #define IST30XX_WHOAMI 0x30003000
19 #define IST30XXA_WHOAMI 0x300a300a
20 #define IST30XXB_WHOAMI 0x300b300b
21 #define IST3038_WHOAMI 0x30383038
23 #define IST3032C_WHOAMI 0x32c
24 #define IST3038C_WHOAMI 0x38c
26 #define IST3038B_REG_CHIPID 0x30
27 #define IST3038B_WHOAMI 0x30380b
29 #define IST3038C_HIB_ACCESS (0x800B << 16)
30 #define IST3038C_DIRECT_ACCESS BIT(31)
31 #define IST3038C_REG_CHIPID (0x40001000 | IST3038C_DIRECT_ACCESS)
32 #define IST3038C_REG_HIB_BASE 0x30000100
33 #define IST3038C_REG_TOUCH_STATUS (IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS)
34 #define IST3038C_REG_TOUCH_COORD (IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x8)
35 #define IST3038C_REG_INTR_MESSAGE (IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x4)
36 #define IST3038C_CHIP_ON_DELAY_MS 60
37 #define IST3038C_I2C_RETRY_COUNT 3
38 #define IST3038C_MAX_FINGER_NUM 10
39 #define IST3038C_X_MASK GENMASK(23, 12)
40 #define IST3038C_Y_MASK GENMASK(11, 0)
41 #define IST3038C_AREA_MASK GENMASK(27, 24)
42 #define IST3038C_FINGER_COUNT_MASK GENMASK(15, 12)
43 #define IST3038C_FINGER_STATUS_MASK GENMASK(9, 0)
44 #define IST3032C_KEY_STATUS_MASK GENMASK(20, 16)
46 struct imagis_properties
{
47 unsigned int interrupt_msg_cmd
;
48 unsigned int touch_coord_cmd
;
49 unsigned int whoami_cmd
;
50 unsigned int whoami_val
;
52 bool touch_keys_supported
;
56 struct i2c_client
*client
;
57 const struct imagis_properties
*tdata
;
58 struct input_dev
*input_dev
;
59 struct touchscreen_properties prop
;
60 struct regulator_bulk_data supplies
[2];
65 static int imagis_i2c_read_reg(struct imagis_ts
*ts
,
66 unsigned int reg
, u32
*data
)
69 __be32 reg_be
= cpu_to_be32(reg
);
70 struct i2c_msg msg
[] = {
72 .addr
= ts
->client
->addr
,
74 .buf
= (unsigned char *)®_be
,
75 .len
= sizeof(reg_be
),
77 .addr
= ts
->client
->addr
,
79 .buf
= (unsigned char *)&ret_be
,
80 .len
= sizeof(ret_be
),
84 int retry
= IST3038C_I2C_RETRY_COUNT
;
86 /* Retry in case the controller fails to respond */
88 ret
= i2c_transfer(ts
->client
->adapter
, msg
, ARRAY_SIZE(msg
));
89 if (ret
== ARRAY_SIZE(msg
)) {
90 *data
= be32_to_cpu(ret_be
);
94 error
= ret
< 0 ? ret
: -EIO
;
95 dev_err(&ts
->client
->dev
,
96 "%s - i2c_transfer failed: %d (%d)\n",
97 __func__
, error
, ret
);
103 static irqreturn_t
imagis_interrupt(int irq
, void *dev_id
)
105 struct imagis_ts
*ts
= dev_id
;
106 u32 intr_message
, finger_status
;
107 unsigned int finger_count
, finger_pressed
, key_pressed
;
111 error
= imagis_i2c_read_reg(ts
, ts
->tdata
->interrupt_msg_cmd
, &intr_message
);
113 dev_err(&ts
->client
->dev
,
114 "failed to read the interrupt message: %d\n", error
);
118 finger_count
= FIELD_GET(IST3038C_FINGER_COUNT_MASK
, intr_message
);
119 if (finger_count
> IST3038C_MAX_FINGER_NUM
) {
120 dev_err(&ts
->client
->dev
,
121 "finger count %d is more than maximum supported\n",
126 finger_pressed
= FIELD_GET(IST3038C_FINGER_STATUS_MASK
, intr_message
);
128 for (i
= 0; i
< finger_count
; i
++) {
129 if (ts
->tdata
->protocol_b
)
130 error
= imagis_i2c_read_reg(ts
,
131 ts
->tdata
->touch_coord_cmd
+ (i
* 4),
134 error
= imagis_i2c_read_reg(ts
,
135 ts
->tdata
->touch_coord_cmd
, &finger_status
);
137 dev_err(&ts
->client
->dev
,
138 "failed to read coordinates for finger %d: %d\n",
143 input_mt_slot(ts
->input_dev
, i
);
144 input_mt_report_slot_state(ts
->input_dev
, MT_TOOL_FINGER
,
145 finger_pressed
& BIT(i
));
146 touchscreen_report_pos(ts
->input_dev
, &ts
->prop
,
147 FIELD_GET(IST3038C_X_MASK
, finger_status
),
148 FIELD_GET(IST3038C_Y_MASK
, finger_status
),
150 input_report_abs(ts
->input_dev
, ABS_MT_TOUCH_MAJOR
,
151 FIELD_GET(IST3038C_AREA_MASK
, finger_status
));
154 key_pressed
= FIELD_GET(IST3032C_KEY_STATUS_MASK
, intr_message
);
156 for (int i
= 0; i
< ts
->num_keycodes
; i
++)
157 input_report_key(ts
->input_dev
, ts
->keycodes
[i
],
158 key_pressed
& BIT(i
));
160 input_mt_sync_frame(ts
->input_dev
);
161 input_sync(ts
->input_dev
);
167 static void imagis_power_off(void *_ts
)
169 struct imagis_ts
*ts
= _ts
;
171 regulator_bulk_disable(ARRAY_SIZE(ts
->supplies
), ts
->supplies
);
174 static int imagis_power_on(struct imagis_ts
*ts
)
178 error
= regulator_bulk_enable(ARRAY_SIZE(ts
->supplies
), ts
->supplies
);
182 msleep(IST3038C_CHIP_ON_DELAY_MS
);
187 static int imagis_start(struct imagis_ts
*ts
)
191 error
= imagis_power_on(ts
);
195 enable_irq(ts
->client
->irq
);
200 static int imagis_stop(struct imagis_ts
*ts
)
202 disable_irq(ts
->client
->irq
);
204 imagis_power_off(ts
);
209 static int imagis_input_open(struct input_dev
*dev
)
211 struct imagis_ts
*ts
= input_get_drvdata(dev
);
213 return imagis_start(ts
);
216 static void imagis_input_close(struct input_dev
*dev
)
218 struct imagis_ts
*ts
= input_get_drvdata(dev
);
223 static int imagis_init_input_dev(struct imagis_ts
*ts
)
225 struct input_dev
*input_dev
;
228 input_dev
= devm_input_allocate_device(&ts
->client
->dev
);
232 ts
->input_dev
= input_dev
;
234 input_dev
->name
= "Imagis capacitive touchscreen";
235 input_dev
->phys
= "input/ts";
236 input_dev
->id
.bustype
= BUS_I2C
;
237 input_dev
->open
= imagis_input_open
;
238 input_dev
->close
= imagis_input_close
;
240 input_set_drvdata(input_dev
, ts
);
242 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
243 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
244 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
, 0, 16, 0, 0);
245 if (ts
->tdata
->touch_keys_supported
) {
246 ts
->num_keycodes
= of_property_read_variable_u32_array(
247 ts
->client
->dev
.of_node
, "linux,keycodes",
248 ts
->keycodes
, 0, ARRAY_SIZE(ts
->keycodes
));
249 if (ts
->num_keycodes
<= 0) {
250 ts
->keycodes
[0] = KEY_APPSELECT
;
251 ts
->keycodes
[1] = KEY_BACK
;
252 ts
->num_keycodes
= 2;
255 input_dev
->keycodemax
= ts
->num_keycodes
;
256 input_dev
->keycodesize
= sizeof(ts
->keycodes
[0]);
257 input_dev
->keycode
= ts
->keycodes
;
260 for (int i
= 0; i
< ts
->num_keycodes
; i
++)
261 input_set_capability(input_dev
, EV_KEY
, ts
->keycodes
[i
]);
263 touchscreen_parse_properties(input_dev
, true, &ts
->prop
);
264 if (!ts
->prop
.max_x
|| !ts
->prop
.max_y
) {
265 dev_err(&ts
->client
->dev
,
266 "Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
270 error
= input_mt_init_slots(input_dev
,
271 IST3038C_MAX_FINGER_NUM
,
272 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
274 dev_err(&ts
->client
->dev
,
275 "Failed to initialize MT slots: %d", error
);
279 error
= input_register_device(input_dev
);
281 dev_err(&ts
->client
->dev
,
282 "Failed to register input device: %d", error
);
289 static int imagis_init_regulators(struct imagis_ts
*ts
)
291 struct i2c_client
*client
= ts
->client
;
293 ts
->supplies
[0].supply
= "vdd";
294 ts
->supplies
[1].supply
= "vddio";
295 return devm_regulator_bulk_get(&client
->dev
,
296 ARRAY_SIZE(ts
->supplies
),
300 static int imagis_probe(struct i2c_client
*i2c
)
302 struct device
*dev
= &i2c
->dev
;
303 struct imagis_ts
*ts
;
306 ts
= devm_kzalloc(dev
, sizeof(*ts
), GFP_KERNEL
);
312 ts
->tdata
= device_get_match_data(dev
);
314 dev_err(dev
, "missing chip data\n");
318 error
= imagis_init_regulators(ts
);
320 dev_err(dev
, "regulator init error: %d\n", error
);
324 error
= imagis_power_on(ts
);
326 dev_err(dev
, "failed to enable regulators: %d\n", error
);
330 error
= devm_add_action_or_reset(dev
, imagis_power_off
, ts
);
332 dev_err(dev
, "failed to install poweroff action: %d\n", error
);
336 error
= imagis_i2c_read_reg(ts
, ts
->tdata
->whoami_cmd
, &chip_id
);
338 dev_err(dev
, "chip ID read failure: %d\n", error
);
342 if (chip_id
!= ts
->tdata
->whoami_val
) {
343 dev_err(dev
, "unknown chip ID: 0x%x\n", chip_id
);
347 error
= devm_request_threaded_irq(dev
, i2c
->irq
,
348 NULL
, imagis_interrupt
,
349 IRQF_ONESHOT
| IRQF_NO_AUTOEN
,
350 "imagis-touchscreen", ts
);
352 dev_err(dev
, "IRQ %d allocation failure: %d\n",
357 error
= imagis_init_input_dev(ts
);
364 static int imagis_suspend(struct device
*dev
)
366 struct i2c_client
*client
= to_i2c_client(dev
);
367 struct imagis_ts
*ts
= i2c_get_clientdata(client
);
370 mutex_lock(&ts
->input_dev
->mutex
);
372 if (input_device_enabled(ts
->input_dev
))
373 retval
= imagis_stop(ts
);
375 mutex_unlock(&ts
->input_dev
->mutex
);
380 static int imagis_resume(struct device
*dev
)
382 struct i2c_client
*client
= to_i2c_client(dev
);
383 struct imagis_ts
*ts
= i2c_get_clientdata(client
);
386 mutex_lock(&ts
->input_dev
->mutex
);
388 if (input_device_enabled(ts
->input_dev
))
389 retval
= imagis_start(ts
);
391 mutex_unlock(&ts
->input_dev
->mutex
);
396 static DEFINE_SIMPLE_DEV_PM_OPS(imagis_pm_ops
, imagis_suspend
, imagis_resume
);
398 static const struct imagis_properties imagis_3032c_data
= {
399 .interrupt_msg_cmd
= IST3038C_REG_INTR_MESSAGE
,
400 .touch_coord_cmd
= IST3038C_REG_TOUCH_COORD
,
401 .whoami_cmd
= IST3038C_REG_CHIPID
,
402 .whoami_val
= IST3032C_WHOAMI
,
403 .touch_keys_supported
= true,
407 static const struct imagis_properties imagis_3038_data
= {
408 .interrupt_msg_cmd
= IST30XX_REG_STATUS
,
409 .touch_coord_cmd
= IST30XX_REG_STATUS
,
410 .whoami_cmd
= IST30XX_REG_CHIPID
,
411 .whoami_val
= IST3038_WHOAMI
,
412 .touch_keys_supported
= true,
415 static const struct imagis_properties imagis_3038b_data
= {
416 .interrupt_msg_cmd
= IST30XX_REG_STATUS
,
417 .touch_coord_cmd
= IST30XX_REG_STATUS
,
418 .whoami_cmd
= IST3038B_REG_CHIPID
,
419 .whoami_val
= IST3038B_WHOAMI
,
422 static const struct imagis_properties imagis_3038c_data
= {
423 .interrupt_msg_cmd
= IST3038C_REG_INTR_MESSAGE
,
424 .touch_coord_cmd
= IST3038C_REG_TOUCH_COORD
,
425 .whoami_cmd
= IST3038C_REG_CHIPID
,
426 .whoami_val
= IST3038C_WHOAMI
,
431 static const struct of_device_id imagis_of_match
[] = {
432 { .compatible
= "imagis,ist3032c", .data
= &imagis_3032c_data
},
433 { .compatible
= "imagis,ist3038", .data
= &imagis_3038_data
},
434 { .compatible
= "imagis,ist3038b", .data
= &imagis_3038b_data
},
435 { .compatible
= "imagis,ist3038c", .data
= &imagis_3038c_data
},
438 MODULE_DEVICE_TABLE(of
, imagis_of_match
);
441 static struct i2c_driver imagis_ts_driver
= {
443 .name
= "imagis-touchscreen",
444 .pm
= pm_sleep_ptr(&imagis_pm_ops
),
445 .of_match_table
= of_match_ptr(imagis_of_match
),
447 .probe
= imagis_probe
,
450 module_i2c_driver(imagis_ts_driver
);
452 MODULE_DESCRIPTION("Imagis IST3038C Touchscreen Driver");
453 MODULE_AUTHOR("Markuss Broks <markuss.broks@gmail.com>");
454 MODULE_LICENSE("GPL");