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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 * review conversion of r/m/w sequences
29 #include <linux/module.h>
30 #include <linux/input.h>
31 #include <linux/interrupt.h>
32 #include <linux/err.h>
33 #include <linux/param.h>
34 #include <linux/slab.h>
35 #include <linux/platform_device.h>
36 #include <linux/irq.h>
37 #include <linux/delay.h>
38 #include <asm/intel_scu_ipc.h>
40 /* PMIC Interrupt registers */
41 #define PMIC_REG_ID1 0x00 /* PMIC ID1 register */
43 /* PMIC Interrupt registers */
44 #define PMIC_REG_INT 0x04 /* PMIC interrupt register */
45 #define PMIC_REG_MINT 0x05 /* PMIC interrupt mask register */
47 /* ADC Interrupt registers */
48 #define PMIC_REG_ADCINT 0x5F /* ADC interrupt register */
49 #define PMIC_REG_MADCINT 0x60 /* ADC interrupt mask register */
51 /* ADC Control registers */
52 #define PMIC_REG_ADCCNTL1 0x61 /* ADC control register */
54 /* ADC Channel Selection registers */
55 #define PMICADDR0 0xA4
56 #define END_OF_CHANNEL 0x1F
58 /* ADC Result register */
59 #define PMIC_REG_ADCSNS0H 0x64
61 /* ADC channels for touch screen */
62 #define MRST_TS_CHAN10 0xA /* Touch screen X+ connection */
63 #define MRST_TS_CHAN11 0xB /* Touch screen X- connection */
64 #define MRST_TS_CHAN12 0xC /* Touch screen Y+ connection */
65 #define MRST_TS_CHAN13 0xD /* Touch screen Y- connection */
67 /* Touch screen channel BIAS constants */
68 #define MRST_XBIAS 0x20
69 #define MRST_YBIAS 0x40
70 #define MRST_ZBIAS 0x80
72 /* Touch screen coordinates */
74 #define MRST_X_MAX 1024
77 #define MRST_Y_MAX 1024
79 #define MRST_PRESSURE_MIN 0
80 #define MRST_PRESSURE_NOMINAL 50
81 #define MRST_PRESSURE_MAX 100
83 #define WAIT_ADC_COMPLETION 10 /* msec */
85 /* PMIC ADC round robin delays */
86 #define ADC_LOOP_DELAY0 0x0 /* Continuous loop */
87 #define ADC_LOOP_DELAY1 0x1 /* 4.5 ms approximate */
89 /* PMIC Vendor Identifiers */
90 #define PMIC_VENDOR_FS 0 /* PMIC vendor FreeScale */
91 #define PMIC_VENDOR_MAXIM 1 /* PMIC vendor MAXIM */
92 #define PMIC_VENDOR_NEC 2 /* PMIC vendor NEC */
93 #define MRSTOUCH_MAX_CHANNELS 32 /* Maximum ADC channels */
95 /* Touch screen device structure */
97 struct device *dev; /* device associated with touch screen */
98 struct input_dev *input;
100 u16 asr; /* Address selection register */
102 unsigned int vendor; /* PMIC vendor */
103 unsigned int rev; /* PMIC revision */
105 int (*read_prepare)(struct mrstouch_dev *tsdev);
106 int (*read)(struct mrstouch_dev *tsdev, u16 *x, u16 *y, u16 *z);
107 int (*read_finish)(struct mrstouch_dev *tsdev);
111 /*************************** NEC and Maxim Interface ************************/
113 static int mrstouch_nec_adc_read_prepare(struct mrstouch_dev *tsdev)
115 return intel_scu_ipc_update_register(PMIC_REG_MADCINT, 0, 0x20);
119 * Enables PENDET interrupt.
121 static int mrstouch_nec_adc_read_finish(struct mrstouch_dev *tsdev)
125 err = intel_scu_ipc_update_register(PMIC_REG_MADCINT, 0x20, 0x20);
127 err = intel_scu_ipc_update_register(PMIC_REG_ADCCNTL1, 0, 0x05);
133 * Reads PMIC ADC touch screen result
134 * Reads ADC storage registers for higher 7 and lower 3 bits and
135 * converts the two readings into a single value and turns off gain bit
137 static int mrstouch_ts_chan_read(u16 offset, u16 chan, u16 *vp, u16 *vm)
143 result = PMIC_REG_ADCSNS0H + offset;
145 if (chan == MRST_TS_CHAN12)
148 err = intel_scu_ipc_ioread32(result, &res);
152 /* Mash the bits up */
154 *vp = (res & 0xFF) << 3; /* Highest 7 bits */
155 *vp |= (res >> 8) & 0x07; /* Lower 3 bits */
160 *vm = (res & 0xFF) << 3; /* Highest 7 bits */
161 *vm |= (res >> 8) & 0x07; /* Lower 3 bits */
168 * Enables X, Y and Z bias values
169 * Enables YPYM for X channels and XPXM for Y channels
171 static int mrstouch_ts_bias_set(uint offset, uint bias)
178 chan = PMICADDR0 + offset;
179 start = MRST_TS_CHAN10;
181 for (count = 0; count <= 3; count++) {
183 data[count] = bias | (start + count);
186 return intel_scu_ipc_writev(reg, data, 4);
189 /* To read touch screen channel values */
190 static int mrstouch_nec_adc_read(struct mrstouch_dev *tsdev,
191 u16 *x, u16 *y, u16 *z)
196 /* configure Y bias for X channels */
197 err = mrstouch_ts_bias_set(tsdev->asr, MRST_YBIAS);
201 msleep(WAIT_ADC_COMPLETION);
203 /* read x+ and x- channels */
204 err = mrstouch_ts_chan_read(tsdev->asr, MRST_TS_CHAN10, x, &xm);
208 /* configure x bias for y channels */
209 err = mrstouch_ts_bias_set(tsdev->asr, MRST_XBIAS);
213 msleep(WAIT_ADC_COMPLETION);
215 /* read y+ and y- channels */
216 err = mrstouch_ts_chan_read(tsdev->asr, MRST_TS_CHAN12, y, &ym);
220 /* configure z bias for x and y channels */
221 err = mrstouch_ts_bias_set(tsdev->asr, MRST_ZBIAS);
225 msleep(WAIT_ADC_COMPLETION);
227 /* read z+ and z- channels */
228 err = mrstouch_ts_chan_read(tsdev->asr, MRST_TS_CHAN10, z, &zm);
235 dev_err(tsdev->dev, "ipc error during adc read\n");
240 /*************************** Freescale Interface ************************/
242 static int mrstouch_fs_adc_read_prepare(struct mrstouch_dev *tsdev)
250 err = intel_scu_ipc_update_register(PMIC_REG_MADCINT, 0x00, 0x02);
254 chan = PMICADDR0 + tsdev->asr;
257 for (count = 0; count <= 3; count++) {
261 reg[count] = chan++; /* Dummy */
264 err = intel_scu_ipc_writev(reg, data, 5);
268 msleep(WAIT_ADC_COMPLETION);
271 for (count = 0; count <= 3; count++) {
275 reg[count] = chan++; /* Dummy */
278 err = intel_scu_ipc_writev(reg, data, 5);
282 msleep(WAIT_ADC_COMPLETION);
285 err = intel_scu_ipc_iowrite32(chan + 2, 0x8A8A8A8A);
289 msleep(WAIT_ADC_COMPLETION);
294 dev_err(tsdev->dev, "ipc error during %s\n", __func__);
298 static int mrstouch_fs_adc_read(struct mrstouch_dev *tsdev,
299 u16 *x, u16 *y, u16 *z)
306 result = PMIC_REG_ADCSNS0H + tsdev->asr;
310 reg[2] = result + 16;
311 reg[3] = result + 17;
313 err = intel_scu_ipc_readv(reg, data, 4);
317 *x = data[0] << 3; /* Higher 7 bits */
318 *x |= data[1] & 0x7; /* Lower 3 bits */
321 *y = data[2] << 3; /* Higher 7 bits */
322 *y |= data[3] & 0x7; /* Lower 3 bits */
326 reg[0] = result + 28;
327 reg[1] = result + 29;
329 err = intel_scu_ipc_readv(reg, data, 4);
333 *z = data[0] << 3; /* Higher 7 bits */
334 *z |= data[1] & 0x7; /* Lower 3 bits */
340 dev_err(tsdev->dev, "ipc error during %s\n", __func__);
344 static int mrstouch_fs_adc_read_finish(struct mrstouch_dev *tsdev)
351 /* Clear all TS channels */
352 chan = PMICADDR0 + tsdev->asr;
353 for (count = 0; count <= 4; count++) {
357 err = intel_scu_ipc_writev(reg, data, 5);
361 for (count = 0; count <= 4; count++) {
365 err = intel_scu_ipc_writev(reg, data, 5);
369 err = intel_scu_ipc_iowrite32(chan + 2, 0x00000000);
374 err = intel_scu_ipc_update_register(PMIC_REG_MADCINT, 0x02, 0x02);
381 dev_err(tsdev->dev, "ipc error during %s\n", __func__);
385 static void mrstouch_report_event(struct input_dev *input,
386 unsigned int x, unsigned int y, unsigned int z)
388 if (z > MRST_PRESSURE_NOMINAL) {
389 /* Pen touched, report button touch and coordinates */
390 input_report_key(input, BTN_TOUCH, 1);
391 input_report_abs(input, ABS_X, x);
392 input_report_abs(input, ABS_Y, y);
394 input_report_key(input, BTN_TOUCH, 0);
397 input_report_abs(input, ABS_PRESSURE, z);
401 /* PENDET interrupt handler */
402 static irqreturn_t mrstouch_pendet_irq(int irq, void *dev_id)
404 struct mrstouch_dev *tsdev = dev_id;
408 * Should we lower thread priority? Probably not, since we are
409 * not spinning but sleeping...
412 if (tsdev->read_prepare(tsdev))
416 if (tsdev->read(tsdev, &x, &y, &z))
419 mrstouch_report_event(tsdev->input, x, y, z);
420 } while (z > MRST_PRESSURE_NOMINAL);
422 tsdev->read_finish(tsdev);
428 /* Utility to read PMIC ID */
429 static int mrstouch_read_pmic_id(uint *vendor, uint *rev)
434 err = intel_scu_ipc_ioread8(PMIC_REG_ID1, &r);
439 *rev = (r >> 3) & 0x7;
445 * Parse ADC channels to find end of the channel configured by other ADC user
446 * NEC and MAXIM requires 4 channels and FreeScale needs 18 channels
448 static int mrstouch_chan_parse(struct mrstouch_dev *tsdev)
454 for (i = 0; i < MRSTOUCH_MAX_CHANNELS; i++) {
455 err = intel_scu_ipc_ioread8(PMICADDR0 + i, &r8);
459 if (r8 == END_OF_CHANNEL) {
465 if (tsdev->vendor == PMIC_VENDOR_FS) {
466 if (found > MRSTOUCH_MAX_CHANNELS - 18)
469 if (found > MRSTOUCH_MAX_CHANNELS - 4)
478 * Writes touch screen channels to ADC address selection registers
480 static int mrstouch_ts_chan_set(uint offset)
486 chan = PMICADDR0 + offset;
487 for (count = 0; count <= 3; count++) {
488 ret = intel_scu_ipc_iowrite8(chan++, MRST_TS_CHAN10 + count);
492 return intel_scu_ipc_iowrite8(chan++, END_OF_CHANNEL);
496 static int mrstouch_adc_init(struct mrstouch_dev *tsdev)
501 err = mrstouch_read_pmic_id(&tsdev->vendor, &tsdev->rev);
503 dev_err(tsdev->dev, "Unable to read PMIC id\n");
507 switch (tsdev->vendor) {
508 case PMIC_VENDOR_NEC:
509 case PMIC_VENDOR_MAXIM:
510 tsdev->read_prepare = mrstouch_nec_adc_read_prepare;
511 tsdev->read = mrstouch_nec_adc_read;
512 tsdev->read_finish = mrstouch_nec_adc_read_finish;
516 tsdev->read_prepare = mrstouch_fs_adc_read_prepare;
517 tsdev->read = mrstouch_fs_adc_read;
518 tsdev->read_finish = mrstouch_fs_adc_read_finish;
523 "Unsupported touchscreen: %d\n", tsdev->vendor);
527 start = mrstouch_chan_parse(tsdev);
529 dev_err(tsdev->dev, "Unable to parse channels\n");
536 * ADC power on, start, enable PENDET and set loop delay
537 * ADC loop delay is set to 4.5 ms approximately
538 * Loop delay more than this results in jitter in adc readings
539 * Setting loop delay to 0 (continuous loop) in MAXIM stops PENDET
540 * interrupt generation sometimes.
543 if (tsdev->vendor == PMIC_VENDOR_FS) {
544 ra = 0xE0 | ADC_LOOP_DELAY0;
547 /* NEC and MAXIm not consistent with loop delay 0 */
548 ra = 0xE0 | ADC_LOOP_DELAY1;
551 /* configure touch screen channels */
552 err = mrstouch_ts_chan_set(tsdev->asr);
557 err = intel_scu_ipc_update_register(PMIC_REG_ADCCNTL1, ra, 0xE7);
561 err = intel_scu_ipc_update_register(PMIC_REG_MADCINT, rm, 0x03);
569 /* Probe function for touch screen driver */
570 static int mrstouch_probe(struct platform_device *pdev)
572 struct mrstouch_dev *tsdev;
573 struct input_dev *input;
577 irq = platform_get_irq(pdev, 0);
579 dev_err(&pdev->dev, "no interrupt assigned\n");
583 tsdev = kzalloc(sizeof(struct mrstouch_dev), GFP_KERNEL);
584 input = input_allocate_device();
585 if (!tsdev || !input) {
586 dev_err(&pdev->dev, "unable to allocate memory\n");
591 tsdev->dev = &pdev->dev;
592 tsdev->input = input;
595 snprintf(tsdev->phys, sizeof(tsdev->phys),
596 "%s/input0", dev_name(tsdev->dev));
598 err = mrstouch_adc_init(tsdev);
600 dev_err(&pdev->dev, "ADC initialization failed\n");
604 input->name = "mrst_touchscreen";
605 input->phys = tsdev->phys;
606 input->dev.parent = tsdev->dev;
608 input->id.vendor = tsdev->vendor;
609 input->id.version = tsdev->rev;
611 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
612 input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
614 input_set_abs_params(tsdev->input, ABS_X,
615 MRST_X_MIN, MRST_X_MAX, MRST_X_FUZZ, 0);
616 input_set_abs_params(tsdev->input, ABS_Y,
617 MRST_Y_MIN, MRST_Y_MAX, MRST_Y_FUZZ, 0);
618 input_set_abs_params(tsdev->input, ABS_PRESSURE,
619 MRST_PRESSURE_MIN, MRST_PRESSURE_MAX, 0, 0);
621 err = request_threaded_irq(tsdev->irq, NULL, mrstouch_pendet_irq,
622 IRQF_ONESHOT, "mrstouch", tsdev);
624 dev_err(tsdev->dev, "unable to allocate irq\n");
628 err = input_register_device(tsdev->input);
630 dev_err(tsdev->dev, "unable to register input device\n");
634 platform_set_drvdata(pdev, tsdev);
638 free_irq(tsdev->irq, tsdev);
640 input_free_device(input);
645 static int mrstouch_remove(struct platform_device *pdev)
647 struct mrstouch_dev *tsdev = platform_get_drvdata(pdev);
649 free_irq(tsdev->irq, tsdev);
650 input_unregister_device(tsdev->input);
656 static struct platform_driver mrstouch_driver = {
658 .name = "pmic_touch",
659 .owner = THIS_MODULE,
661 .probe = mrstouch_probe,
662 .remove = mrstouch_remove,
664 module_platform_driver(mrstouch_driver);
667 MODULE_DESCRIPTION("Intel Moorestown Resistive Touch Screen Driver");
668 MODULE_LICENSE("GPL");