2 * Intel MID Resistive Touch Screen Driver
4 * Copyright (C) 2008 Intel Corp
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 * Questions/Comments/Bug fixes to Sreedhara (sreedhara.ds@intel.com)
22 * Ramesh Agarwal (ramesh.agarwal@intel.com)
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 * review conversion of r/m/w sequences
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/input.h>
32 #include <linux/interrupt.h>
33 #include <linux/err.h>
34 #include <linux/param.h>
35 #include <linux/slab.h>
36 #include <linux/platform_device.h>
37 #include <linux/irq.h>
38 #include <linux/delay.h>
39 #include <asm/intel_scu_ipc.h>
41 /* PMIC Interrupt registers */
42 #define PMIC_REG_ID1 0x00 /* PMIC ID1 register */
44 /* PMIC Interrupt registers */
45 #define PMIC_REG_INT 0x04 /* PMIC interrupt register */
46 #define PMIC_REG_MINT 0x05 /* PMIC interrupt mask register */
48 /* ADC Interrupt registers */
49 #define PMIC_REG_ADCINT 0x5F /* ADC interrupt register */
50 #define PMIC_REG_MADCINT 0x60 /* ADC interrupt mask register */
52 /* ADC Control registers */
53 #define PMIC_REG_ADCCNTL1 0x61 /* ADC control register */
55 /* ADC Channel Selection registers */
56 #define PMICADDR0 0xA4
57 #define END_OF_CHANNEL 0x1F
59 /* ADC Result register */
60 #define PMIC_REG_ADCSNS0H 0x64
62 /* ADC channels for touch screen */
63 #define MRST_TS_CHAN10 0xA /* Touch screen X+ connection */
64 #define MRST_TS_CHAN11 0xB /* Touch screen X- connection */
65 #define MRST_TS_CHAN12 0xC /* Touch screen Y+ connection */
66 #define MRST_TS_CHAN13 0xD /* Touch screen Y- connection */
68 /* Touch screen channel BIAS constants */
69 #define MRST_XBIAS 0x20
70 #define MRST_YBIAS 0x40
71 #define MRST_ZBIAS 0x80
73 /* Touch screen coordinates */
75 #define MRST_X_MAX 1024
78 #define MRST_Y_MAX 1024
80 #define MRST_PRESSURE_MIN 0
81 #define MRST_PRESSURE_NOMINAL 50
82 #define MRST_PRESSURE_MAX 100
84 #define WAIT_ADC_COMPLETION 10 /* msec */
86 /* PMIC ADC round robin delays */
87 #define ADC_LOOP_DELAY0 0x0 /* Continuous loop */
88 #define ADC_LOOP_DELAY1 0x1 /* 4.5 ms approximate */
90 /* PMIC Vendor Identifiers */
91 #define PMIC_VENDOR_FS 0 /* PMIC vendor FreeScale */
92 #define PMIC_VENDOR_MAXIM 1 /* PMIC vendor MAXIM */
93 #define PMIC_VENDOR_NEC 2 /* PMIC vendor NEC */
94 #define MRSTOUCH_MAX_CHANNELS 32 /* Maximum ADC channels */
96 /* Touch screen device structure */
98 struct device
*dev
; /* device associated with touch screen */
99 struct input_dev
*input
;
101 u16 asr
; /* Address selection register */
103 unsigned int vendor
; /* PMIC vendor */
104 unsigned int rev
; /* PMIC revision */
106 int (*read_prepare
)(struct mrstouch_dev
*tsdev
);
107 int (*read
)(struct mrstouch_dev
*tsdev
, u16
*x
, u16
*y
, u16
*z
);
108 int (*read_finish
)(struct mrstouch_dev
*tsdev
);
112 /*************************** NEC and Maxim Interface ************************/
114 static int mrstouch_nec_adc_read_prepare(struct mrstouch_dev
*tsdev
)
116 return intel_scu_ipc_update_register(PMIC_REG_MADCINT
, 0, 0x20);
120 * Enables PENDET interrupt.
122 static int mrstouch_nec_adc_read_finish(struct mrstouch_dev
*tsdev
)
126 err
= intel_scu_ipc_update_register(PMIC_REG_MADCINT
, 0x20, 0x20);
128 err
= intel_scu_ipc_update_register(PMIC_REG_ADCCNTL1
, 0, 0x05);
134 * Reads PMIC ADC touch screen result
135 * Reads ADC storage registers for higher 7 and lower 3 bits and
136 * converts the two readings into a single value and turns off gain bit
138 static int mrstouch_ts_chan_read(u16 offset
, u16 chan
, u16
*vp
, u16
*vm
)
144 result
= PMIC_REG_ADCSNS0H
+ offset
;
146 if (chan
== MRST_TS_CHAN12
)
149 err
= intel_scu_ipc_ioread32(result
, &res
);
153 /* Mash the bits up */
155 *vp
= (res
& 0xFF) << 3; /* Highest 7 bits */
156 *vp
|= (res
>> 8) & 0x07; /* Lower 3 bits */
161 *vm
= (res
& 0xFF) << 3; /* Highest 7 bits */
162 *vm
|= (res
>> 8) & 0x07; /* Lower 3 bits */
169 * Enables X, Y and Z bias values
170 * Enables YPYM for X channels and XPXM for Y channels
172 static int mrstouch_ts_bias_set(uint offset
, uint bias
)
179 chan
= PMICADDR0
+ offset
;
180 start
= MRST_TS_CHAN10
;
182 for (count
= 0; count
<= 3; count
++) {
184 data
[count
] = bias
| (start
+ count
);
187 return intel_scu_ipc_writev(reg
, data
, 4);
190 /* To read touch screen channel values */
191 static int mrstouch_nec_adc_read(struct mrstouch_dev
*tsdev
,
192 u16
*x
, u16
*y
, u16
*z
)
197 /* configure Y bias for X channels */
198 err
= mrstouch_ts_bias_set(tsdev
->asr
, MRST_YBIAS
);
202 msleep(WAIT_ADC_COMPLETION
);
204 /* read x+ and x- channels */
205 err
= mrstouch_ts_chan_read(tsdev
->asr
, MRST_TS_CHAN10
, x
, &xm
);
209 /* configure x bias for y channels */
210 err
= mrstouch_ts_bias_set(tsdev
->asr
, MRST_XBIAS
);
214 msleep(WAIT_ADC_COMPLETION
);
216 /* read y+ and y- channels */
217 err
= mrstouch_ts_chan_read(tsdev
->asr
, MRST_TS_CHAN12
, y
, &ym
);
221 /* configure z bias for x and y channels */
222 err
= mrstouch_ts_bias_set(tsdev
->asr
, MRST_ZBIAS
);
226 msleep(WAIT_ADC_COMPLETION
);
228 /* read z+ and z- channels */
229 err
= mrstouch_ts_chan_read(tsdev
->asr
, MRST_TS_CHAN10
, z
, &zm
);
236 dev_err(tsdev
->dev
, "ipc error during adc read\n");
241 /*************************** Freescale Interface ************************/
243 static int mrstouch_fs_adc_read_prepare(struct mrstouch_dev
*tsdev
)
251 err
= intel_scu_ipc_update_register(PMIC_REG_MADCINT
, 0x00, 0x02);
255 chan
= PMICADDR0
+ tsdev
->asr
;
258 for (count
= 0; count
<= 3; count
++) {
262 reg
[count
] = chan
++; /* Dummy */
265 err
= intel_scu_ipc_writev(reg
, data
, 5);
269 msleep(WAIT_ADC_COMPLETION
);
272 for (count
= 0; count
<= 3; count
++) {
276 reg
[count
] = chan
++; /* Dummy */
279 err
= intel_scu_ipc_writev(reg
, data
, 5);
283 msleep(WAIT_ADC_COMPLETION
);
286 err
= intel_scu_ipc_iowrite32(chan
+ 2, 0x8A8A8A8A);
290 msleep(WAIT_ADC_COMPLETION
);
295 dev_err(tsdev
->dev
, "ipc error during %s\n", __func__
);
299 static int mrstouch_fs_adc_read(struct mrstouch_dev
*tsdev
,
300 u16
*x
, u16
*y
, u16
*z
)
307 result
= PMIC_REG_ADCSNS0H
+ tsdev
->asr
;
311 reg
[2] = result
+ 16;
312 reg
[3] = result
+ 17;
314 err
= intel_scu_ipc_readv(reg
, data
, 4);
318 *x
= data
[0] << 3; /* Higher 7 bits */
319 *x
|= data
[1] & 0x7; /* Lower 3 bits */
322 *y
= data
[2] << 3; /* Higher 7 bits */
323 *y
|= data
[3] & 0x7; /* Lower 3 bits */
327 reg
[0] = result
+ 28;
328 reg
[1] = result
+ 29;
330 err
= intel_scu_ipc_readv(reg
, data
, 4);
334 *z
= data
[0] << 3; /* Higher 7 bits */
335 *z
|= data
[1] & 0x7; /* Lower 3 bits */
341 dev_err(tsdev
->dev
, "ipc error during %s\n", __func__
);
345 static int mrstouch_fs_adc_read_finish(struct mrstouch_dev
*tsdev
)
352 /* Clear all TS channels */
353 chan
= PMICADDR0
+ tsdev
->asr
;
354 for (count
= 0; count
<= 4; count
++) {
358 err
= intel_scu_ipc_writev(reg
, data
, 5);
362 for (count
= 0; count
<= 4; count
++) {
366 err
= intel_scu_ipc_writev(reg
, data
, 5);
370 err
= intel_scu_ipc_iowrite32(chan
+ 2, 0x00000000);
375 err
= intel_scu_ipc_update_register(PMIC_REG_MADCINT
, 0x02, 0x02);
382 dev_err(tsdev
->dev
, "ipc error during %s\n", __func__
);
386 static void mrstouch_report_event(struct input_dev
*input
,
387 unsigned int x
, unsigned int y
, unsigned int z
)
389 if (z
> MRST_PRESSURE_NOMINAL
) {
390 /* Pen touched, report button touch and coordinates */
391 input_report_key(input
, BTN_TOUCH
, 1);
392 input_report_abs(input
, ABS_X
, x
);
393 input_report_abs(input
, ABS_Y
, y
);
395 input_report_key(input
, BTN_TOUCH
, 0);
398 input_report_abs(input
, ABS_PRESSURE
, z
);
402 /* PENDET interrupt handler */
403 static irqreturn_t
mrstouch_pendet_irq(int irq
, void *dev_id
)
405 struct mrstouch_dev
*tsdev
= dev_id
;
409 * Should we lower thread priority? Probably not, since we are
410 * not spinning but sleeping...
413 if (tsdev
->read_prepare(tsdev
))
417 if (tsdev
->read(tsdev
, &x
, &y
, &z
))
420 mrstouch_report_event(tsdev
->input
, x
, y
, z
);
421 } while (z
> MRST_PRESSURE_NOMINAL
);
423 tsdev
->read_finish(tsdev
);
429 /* Utility to read PMIC ID */
430 static int __devinit
mrstouch_read_pmic_id(uint
*vendor
, uint
*rev
)
435 err
= intel_scu_ipc_ioread8(PMIC_REG_ID1
, &r
);
440 *rev
= (r
>> 3) & 0x7;
446 * Parse ADC channels to find end of the channel configured by other ADC user
447 * NEC and MAXIM requires 4 channels and FreeScale needs 18 channels
449 static int __devinit
mrstouch_chan_parse(struct mrstouch_dev
*tsdev
)
456 for (i
= 0; i
< MRSTOUCH_MAX_CHANNELS
; i
++) {
460 err
= intel_scu_ipc_ioread8(PMICADDR0
+ i
, &r8
);
464 if (r8
== END_OF_CHANNEL
) {
472 if (tsdev
->vendor
== PMIC_VENDOR_FS
) {
473 if (found
&& found
> (MRSTOUCH_MAX_CHANNELS
- 18))
476 if (found
&& found
> (MRSTOUCH_MAX_CHANNELS
- 4))
484 * Writes touch screen channels to ADC address selection registers
486 static int __devinit
mrstouch_ts_chan_set(uint offset
)
492 chan
= PMICADDR0
+ offset
;
493 for (count
= 0; count
<= 3; count
++) {
494 ret
= intel_scu_ipc_iowrite8(chan
++, MRST_TS_CHAN10
+ count
);
498 return intel_scu_ipc_iowrite8(chan
++, END_OF_CHANNEL
);
502 static int __devinit
mrstouch_adc_init(struct mrstouch_dev
*tsdev
)
507 err
= mrstouch_read_pmic_id(&tsdev
->vendor
, &tsdev
->rev
);
509 dev_err(tsdev
->dev
, "Unable to read PMIC id\n");
513 switch (tsdev
->vendor
) {
514 case PMIC_VENDOR_NEC
:
515 case PMIC_VENDOR_MAXIM
:
516 tsdev
->read_prepare
= mrstouch_nec_adc_read_prepare
;
517 tsdev
->read
= mrstouch_nec_adc_read
;
518 tsdev
->read_finish
= mrstouch_nec_adc_read_finish
;
522 tsdev
->read_prepare
= mrstouch_fs_adc_read_prepare
;
523 tsdev
->read
= mrstouch_fs_adc_read
;
524 tsdev
->read_finish
= mrstouch_fs_adc_read_finish
;
529 "Unsupported touchscreen: %d\n", tsdev
->vendor
);
533 start
= mrstouch_chan_parse(tsdev
);
535 dev_err(tsdev
->dev
, "Unable to parse channels\n");
542 * ADC power on, start, enable PENDET and set loop delay
543 * ADC loop delay is set to 4.5 ms approximately
544 * Loop delay more than this results in jitter in adc readings
545 * Setting loop delay to 0 (continous loop) in MAXIM stops PENDET
546 * interrupt generation sometimes.
549 if (tsdev
->vendor
== PMIC_VENDOR_FS
) {
550 ra
= 0xE0 | ADC_LOOP_DELAY0
;
553 /* NEC and MAXIm not consistent with loop delay 0 */
554 ra
= 0xE0 | ADC_LOOP_DELAY1
;
557 /* configure touch screen channels */
558 err
= mrstouch_ts_chan_set(tsdev
->asr
);
563 err
= intel_scu_ipc_update_register(PMIC_REG_ADCCNTL1
, ra
, 0xE7);
567 err
= intel_scu_ipc_update_register(PMIC_REG_MADCINT
, rm
, 0x03);
575 /* Probe function for touch screen driver */
576 static int __devinit
mrstouch_probe(struct platform_device
*pdev
)
578 struct mrstouch_dev
*tsdev
;
579 struct input_dev
*input
;
583 irq
= platform_get_irq(pdev
, 0);
585 dev_err(&pdev
->dev
, "no interrupt assigned\n");
589 tsdev
= kzalloc(sizeof(struct mrstouch_dev
), GFP_KERNEL
);
590 input
= input_allocate_device();
591 if (!tsdev
|| !input
) {
592 dev_err(&pdev
->dev
, "unable to allocate memory\n");
597 tsdev
->dev
= &pdev
->dev
;
598 tsdev
->input
= input
;
601 snprintf(tsdev
->phys
, sizeof(tsdev
->phys
),
602 "%s/input0", dev_name(tsdev
->dev
));
604 err
= mrstouch_adc_init(tsdev
);
606 dev_err(&pdev
->dev
, "ADC initialization failed\n");
610 input
->name
= "mrst_touchscreen";
611 input
->phys
= tsdev
->phys
;
612 input
->dev
.parent
= tsdev
->dev
;
614 input
->id
.vendor
= tsdev
->vendor
;
615 input
->id
.version
= tsdev
->rev
;
617 input
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
618 input
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
620 input_set_abs_params(tsdev
->input
, ABS_X
,
621 MRST_X_MIN
, MRST_X_MAX
, MRST_X_FUZZ
, 0);
622 input_set_abs_params(tsdev
->input
, ABS_Y
,
623 MRST_Y_MIN
, MRST_Y_MAX
, MRST_Y_FUZZ
, 0);
624 input_set_abs_params(tsdev
->input
, ABS_PRESSURE
,
625 MRST_PRESSURE_MIN
, MRST_PRESSURE_MAX
, 0, 0);
627 err
= request_threaded_irq(tsdev
->irq
, NULL
, mrstouch_pendet_irq
,
628 0, "mrstouch", tsdev
);
630 dev_err(tsdev
->dev
, "unable to allocate irq\n");
634 err
= input_register_device(tsdev
->input
);
636 dev_err(tsdev
->dev
, "unable to register input device\n");
640 platform_set_drvdata(pdev
, tsdev
);
644 free_irq(tsdev
->irq
, tsdev
);
646 input_free_device(input
);
651 static int __devexit
mrstouch_remove(struct platform_device
*pdev
)
653 struct mrstouch_dev
*tsdev
= platform_get_drvdata(pdev
);
655 free_irq(tsdev
->irq
, tsdev
);
656 input_unregister_device(tsdev
->input
);
659 platform_set_drvdata(pdev
, NULL
);
664 static struct platform_driver mrstouch_driver
= {
666 .name
= "pmic_touch",
667 .owner
= THIS_MODULE
,
669 .probe
= mrstouch_probe
,
670 .remove
= __devexit_p(mrstouch_remove
),
673 static int __init
mrstouch_init(void)
675 return platform_driver_register(&mrstouch_driver
);
677 module_init(mrstouch_init
);
679 static void __exit
mrstouch_exit(void)
681 platform_driver_unregister(&mrstouch_driver
);
683 module_exit(mrstouch_exit
);
685 MODULE_AUTHOR("Sreedhara Murthy. D.S, sreedhara.ds@intel.com");
686 MODULE_DESCRIPTION("Intel Moorestown Resistive Touch Screen Driver");
687 MODULE_LICENSE("GPL");