6b13f685e
김민수
BSP 최초 추가
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
* Microchip I2C Touchscreen Driver
*
* Copyright (c) 2011 Microchip Technology, Inc.
*
* http://www.microchip.com/mtouch
*/
/*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*/
#include <linux/input.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/slab.h>
static int calibration[12];
module_param_array(calibration, int, NULL, S_IRUGO | S_IWUSR | S_IWGRP);
static void translate(int *px, int *py)
{
if (calibration[0] || calibration[1] || calibration[2]) {
int x = *px;
int y = *py;
u32 s[6];
s64 xsum, ysum;
int i;
s[0] = 1 << 16;
s[1] = x << (16 - 12);
s[2] = y << (16 - 12);
s[3] = (s[1] * s[2]) >> 16;
s[4] = (s[1] * s[1]) >> 16;
s[5] = (s[2] * s[2]) >> 16;
xsum = 0;
ysum = 0;
for (i = 0; i < 6 ; i++) {
xsum += (calibration[i] * (s64)s[i]);
ysum += (calibration[i + 6] * (s64)s[i]);
}
x = (s32)(xsum >> (32 - 12));
y = (s32)(ysum >> (32 - 12));
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (x > 4095)
x = 4095;
if (y > 4095)
y = 4095;
*px = x ;
*py = y ;
}
}
/* The private data structure that is referenced within the I2C bus driver */
struct ar1020_i2c_priv {
struct i2c_client *client;
struct input_dev *input;
int irq;
int testCount;
int command_pending;
int use_count;
int button;
struct delayed_work reenable_work;
};
/*
* These are all the sysfs variables used to store and retrieve information
* from a user-level application
*/
static char sendBuffer[100];
static char receiveBuffer[100];
static int commandDataPending;
/*
* These variables allows the IRQ to be specified via a module parameter
* or kernel parameter. To configuration of these value, please see
* driver documentation.
*/
static int touchIRQ;
module_param(touchIRQ, int, S_IRUGO);
/*
* Since the reference to private data is stored within the I2C
* bus driver, we will store another reference within this driver
* so the sysfs related function may also access this data
*/
struct ar1020_i2c_priv *g_priv;
/*
* Display value of "commandDataPending" variable to application that is
* requesting it's value.
*/
static ssize_t commandDataPending_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%d", commandDataPending);
}
/*
* Save value to "commandDataPending" variable from application that is
* requesting this.
*/
static ssize_t commandDataPending_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%d", &commandDataPending);
return count;
}
static struct kobj_attribute commandDataPending_attribute =
__ATTR(commandDataPending, 0666, commandDataPending_show,
commandDataPending_store);
/*
* Display value of "receiveBuffer" variable to application that is
* requesting it's value.
*/
static ssize_t receiveBuffer_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
/* receive data is no longer pending */
commandDataPending = 0;
return sprintf(buf, "%s", receiveBuffer);
}
/*
* Save value to "receiveBuffer" variable from application that is
* requesting this.
*/
static ssize_t receiveBuffer_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buf, size_t count)
{
return snprintf(receiveBuffer, sizeof(receiveBuffer), "%s", buf);
}
static struct kobj_attribute receiveBuffer_attribute =
__ATTR(receiveBuffer, 0666, receiveBuffer_show, receiveBuffer_store);
/*
* Display value of "sendBuffer" variable to application that is
* requesting it's value.
*/
static ssize_t sendBuffer_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%s", sendBuffer);
}
/*
* Save value to "sendBuffer" variable from application that is
* requesting this.
*/
static ssize_t sendBuffer_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buf, size_t count)
{
int data[8];
char buff[8];
int cnt;
int i;
struct ar1020_i2c_priv *priv = g_priv;
commandDataPending = 0;
cnt = sscanf(buf, "%x %x %x %x %x %x %x %x", &data[0], &data[1],
&data[2], &data[3], &data[4], &data[5], &data[6], &data[7]);
pr_debug("AR1020 I2C: Processed %d bytes.
", cnt);
/* Verify command string to send to controller is valid */
if (cnt < 3)
pr_info("AR1020 I2C: Insufficient command bytes to process.
");
else if (data[0] != 0x0)
pr_info("AR1020 I2C: Leading zero required when sending I2C commands.
");
else if (data[1] != 0x55)
pr_info("AR1020 I2C: Invalid header byte (0x55 expected).
");
else if (data[2] != (cnt - 3))
pr_info("AR1020 I2C: Number of command bytes specified not "
"valid for current string.
");
strcpy(sendBuffer, "");
pr_debug("AR1020 I2C: sending command bytes: ");
for (i = 0; i < cnt; i++) {
buff[i] = (char)data[i];
pr_debug("0x%02x ", data[i]);
}
pr_debug("
");
if (priv) {
priv->command_pending = 1;
i2c_master_send(priv->client, buff, cnt);
}
return snprintf(sendBuffer, sizeof(sendBuffer), "%s", buf);
}
static struct kobj_attribute sendBuffer_attribute =
__ATTR(sendBuffer, 0666, sendBuffer_show, sendBuffer_store);
/*
* Create a group of calibration attributes so we may work with them
* as a set.
*/
static struct attribute *attrs[] = {
&commandDataPending_attribute.attr,
&receiveBuffer_attribute.attr,
&sendBuffer_attribute.attr,
NULL,
};
static struct attribute_group attr_group = {
.attrs = attrs,
};
static struct kobject *ar1020_kobj;
static void irq_reenable_work(struct work_struct *work)
{
struct ar1020_i2c_priv *priv = container_of(work,
struct ar1020_i2c_priv, reenable_work.work);
enable_irq(priv->irq);
}
/*
* When the controller interrupt is asserted, this function is scheduled
* to be called to read the controller data.
*/
static irqreturn_t touch_irq_handler_func(int irq, void *dev_id)
{
struct ar1020_i2c_priv *priv = (struct ar1020_i2c_priv *)dev_id;
int i;
int cnt;
unsigned char packet[9];
int x;
int y;
int button;
for (i = 0; i < 5; i++)
packet[i] = 0;
/*
* We want to ensure we only read packets when we are not in the middle
* of command communication. Disable command mode after receiving
* command response to resume receiving packets.
*/
cnt = i2c_master_recv(priv->client, packet,
(priv->command_pending) ? 9 : 5);
if (cnt <= 0)
goto error;
if (packet[0] == 0x55) {
unsigned char *p = receiveBuffer;
unsigned char *pend = p + sizeof(receiveBuffer) - 2;
/* process up to 9 bytes */
strcpy(receiveBuffer, "");
priv->command_pending = 0;
if (packet[1] > 6)
pr_info("AR1020 I2C: invalid byte count
");
else if (cnt > packet[1] + 2)
cnt = packet[1] + 2;
for (i = 0; i < cnt; i++) {
int ret = snprintf(p, pend - p, " 0x%02x", packet[i]);
if (ret <= 0)
break;
p += ret;
if (p >= pend)
break;
}
*p++ = '
';
*p++ = 0;
pr_info("AR1020 I2C: command response: %s", receiveBuffer);
commandDataPending = 1;
return IRQ_HANDLED;
}
/*
* Decode packets of data from a device path using AR1XXX protocol.
* Data format, 5 bytes: SYNC, DATA1, DATA2, DATA3, DATA4
* SYNC [7:0]: 1,0,0,0,0,TOUCHSTATUS[0:0]
* DATA1[7:0]: 0,X-LOW[6:0]
* DATA2[7:0]: 0,X-HIGH[4:0]
* DATA3[7:0]: 0,Y-LOW[6:0]
* DATA4[7:0]: 0,Y-HIGH[4:0]
*
* TOUCHSTATUS: 0 = Touch up, 1 = Touch down
*/
if ((packet[0] & 0xfe) != 0x80) {
pr_err("AR1020 I2C: 1st Touch byte not valid. Value: 0x%02x
",
packet[0]);
goto error; /* irq line may be shorted high, let's delay */
}
for (i = 1; i < 5; i++) {
/* verify byte is valid for current index */
if (0x80 & packet[i]) {
pr_err("AR1020 I2C: Touch byte not valid. Value: "
"0x%02x Index: 0x%02x
", packet[i], i);
goto error;
}
}
x = ((packet[2] & 0x1f) << 7) | (packet[1] & 0x7f);
y = ((packet[4] & 0x1f) << 7) | (packet[3] & 0x7f);
button = (packet[0] & 1);
if (!button && !priv->button)
return IRQ_HANDLED;
priv->button = button;
translate(&x, &y);
input_report_abs(priv->input, ABS_X, x);
input_report_abs(priv->input, ABS_Y, y);
input_report_abs(priv->input, ABS_PRESSURE, button);
input_report_key(priv->input, BTN_TOUCH, button);
input_sync(priv->input);
return IRQ_HANDLED;
error:
disable_irq_nosync(priv->irq);
schedule_delayed_work(&priv->reenable_work, 100);
return IRQ_HANDLED;
}
static int ar1020_i2c_open(struct input_dev *idev)
{
struct ar1020_i2c_priv *priv = input_get_drvdata(idev);
if (priv->use_count++ == 0)
enable_irq(priv->irq);
return 0;
}
static void ar1020_i2c_close(struct input_dev *idev)
{
struct ar1020_i2c_priv *priv = input_get_drvdata(idev);
if (--priv->use_count == 0)
disable_irq(priv->irq);
}
/*
* After the kernel's platform specific source files have been modified to
* reference the "ar1020_i2c" driver, this function will then be called.
* This function needs to be called to finish registering the driver.
*/
static int ar1020_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct ar1020_i2c_priv *priv = NULL;
struct input_dev *input_dev = NULL;
struct irq_data *irqd;
int err = 0;
pr_info("%s: begin
", __func__);
if (!client) {
pr_err("AR1020 I2C: client pointer is NULL
");
err = -EINVAL;
goto error;
}
if (!client->irq) {
pr_err("AR1020 I2C: client irq is NULL
");
err = -EINVAL;
goto error;
}
irqd = irq_get_irq_data(client->irq);
if (!irqd) {
pr_err("%s:invalid IRQ %d
", __func__,client->irq);
err = -EINVAL;
goto error;
}
priv = kzalloc(sizeof(struct ar1020_i2c_priv), GFP_KERNEL);
input_dev = input_allocate_device();
if (!priv) {
pr_err("AR1020 I2C: kzalloc error
");
err = -ENOMEM;
goto error;
}
if (!input_dev) {
pr_err("AR1020 I2C: input allocate error
");
err = -ENOMEM;
goto error;
}
priv->client = client;
priv->irq = client->irq;
priv->input = input_dev;
INIT_DELAYED_WORK(&priv->reenable_work, irq_reenable_work);
input_dev->name = "AR1020 Touchscreen";
input_dev->id.bustype = BUS_I2C;
input_dev->open = ar1020_i2c_open;
input_dev->close = ar1020_i2c_close;
__set_bit(EV_ABS, input_dev->evbit);
__set_bit(EV_KEY, input_dev->evbit);
__set_bit(BTN_TOUCH, input_dev->keybit);
__set_bit(ABS_X, input_dev->absbit);
__set_bit(ABS_Y, input_dev->absbit);
__set_bit(ABS_PRESSURE, input_dev->absbit);
__set_bit(EV_SYN, input_dev->evbit);
input_set_abs_params(input_dev, ABS_X, 0, 4095, 0, 0);
input_set_abs_params(input_dev, ABS_Y, 0, 4095, 0, 0);
input_set_abs_params(input_dev, ABS_PRESSURE, 0, 1, 0, 0);
input_set_drvdata(input_dev, priv);
err = input_register_device(input_dev);
if (err) {
pr_err("AR1020 I2C: error registering input device
");
goto error;
}
pr_info("AR1020 I2C: irq %d, flags %08x
", priv->irq, irqd_get_trigger_type(irqd));
/* set type and register gpio pin as our interrupt */
err = request_threaded_irq(priv->irq, NULL, touch_irq_handler_func,
irqd_get_trigger_type(irqd) | IRQF_ONESHOT,
"AR1020 I2C IRQ", priv);
if (err < 0)
goto error1;
disable_irq(priv->irq); /* wait for open */
i2c_set_clientdata(client, priv);
/*
* save pointer so sysfs helper functions may also have access
* to private data
*/
g_priv = priv;
return 0;
error1:
input_unregister_device(input_dev);
error:
if (input_dev)
input_free_device(input_dev);
kfree(priv);
return err;
}
/*
* Unregister/remove the kernel driver from memory.
*/
static int ar1020_i2c_remove(struct i2c_client *client)
{
struct ar1020_i2c_priv *priv =
(struct ar1020_i2c_priv *)i2c_get_clientdata(client);
free_irq(priv->irq, priv);
input_unregister_device(priv->input);
kfree(priv);
g_priv = NULL;
return 0;
}
/* This structure describe a list of supported slave chips */
static const struct i2c_device_id ar1020_i2c_id[] = {
{ "ar1020_i2c", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, ar1020_i2c_id);
/*
* This is the initial set of information the kernel has
* before probing drivers on the system
*/
static struct i2c_driver ar1020_i2c_driver = {
.driver = {
.name = "ar1020_i2c",
},
.probe = ar1020_i2c_probe,
.remove = ar1020_i2c_remove,
/*
* suspend/resume functions not needed since controller automatically
* put's itself to sleep mode after configurable short period of time
*/
.suspend = NULL,
.resume = NULL,
.id_table = ar1020_i2c_id,
};
/*
* This function is called during startup even if the platform specific
* files have not been setup yet.
*/
static int __init ar1020_i2c_init(void)
{
int retval;
pr_debug("AR1020 I2C: ar1020_i2c_init: begin
");
strcpy(receiveBuffer, "");
strcpy(sendBuffer, "");
/*
* Creates a kobject "ar1020" that appears as a sub-directory
* under "/sys/kernel".
*/
ar1020_kobj = kobject_create_and_add("ar1020", kernel_kobj);
if (!ar1020_kobj) {
pr_err("AR1020 I2C: cannot create kobject
");
return -ENOMEM;
}
/* Create the files associated with this kobject */
retval = sysfs_create_group(ar1020_kobj, &attr_group);
if (retval) {
pr_err("AR1020 I2C: error registering ar1020-i2c driver's sysfs interface
");
kobject_put(ar1020_kobj);
}
return i2c_add_driver(&ar1020_i2c_driver);
}
/*
* This function is called after ar1020_i2c_remove() immediately before
* being removed from the kernel.
*/
static void __exit ar1020_i2c_exit(void)
{
pr_debug("AR1020 I2C: ar1020_i2c_exit begin
");
kobject_put(ar1020_kobj);
i2c_del_driver(&ar1020_i2c_driver);
}
MODULE_AUTHOR("Steve Grahovac <steve.grahovac@microchip.com>");
MODULE_DESCRIPTION("AR1020 touchscreen I2C bus driver");
MODULE_LICENSE("GPL");
module_init(ar1020_i2c_init);
module_exit(ar1020_i2c_exit);
|