1885c316dSMagnus Damm /* 2885c316dSMagnus Damm * Touch Screen driver for Renesas MIGO-R Platform 3885c316dSMagnus Damm * 4885c316dSMagnus Damm * Copyright (c) 2008 Magnus Damm 5885c316dSMagnus Damm * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>, 6885c316dSMagnus Damm * Kenati Technologies Pvt Ltd. 7885c316dSMagnus Damm * 8885c316dSMagnus Damm * This file is free software; you can redistribute it and/or 9885c316dSMagnus Damm * modify it under the terms of the GNU General Public 10885c316dSMagnus Damm * License as published by the Free Software Foundation; either 11885c316dSMagnus Damm * version 2 of the License, or (at your option) any later version. 12885c316dSMagnus Damm * 13885c316dSMagnus Damm * This file is distributed in the hope that it will be useful, 14885c316dSMagnus Damm * but WITHOUT ANY WARRANTY; without even the implied warranty of 15885c316dSMagnus Damm * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16885c316dSMagnus Damm * General Public License for more details. 17885c316dSMagnus Damm * 18885c316dSMagnus Damm * You should have received a copy of the GNU General Public 19885c316dSMagnus Damm * License along with this library; if not, write to the Free Software 20885c316dSMagnus Damm * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21885c316dSMagnus Damm */ 22885c316dSMagnus Damm #include <linux/module.h> 23885c316dSMagnus Damm #include <linux/kernel.h> 24885c316dSMagnus Damm #include <linux/input.h> 25885c316dSMagnus Damm #include <linux/interrupt.h> 26d72e64ecSMark Brown #include <linux/pm.h> 275a0e3ad6STejun Heo #include <linux/slab.h> 28885c316dSMagnus Damm #include <asm/io.h> 29885c316dSMagnus Damm #include <linux/i2c.h> 30885c316dSMagnus Damm #include <linux/timer.h> 31885c316dSMagnus Damm 32885c316dSMagnus Damm #define EVENT_PENDOWN 1 33885c316dSMagnus Damm #define EVENT_REPEAT 2 34885c316dSMagnus Damm #define EVENT_PENUP 3 35885c316dSMagnus Damm 36885c316dSMagnus Damm struct migor_ts_priv { 37885c316dSMagnus Damm struct i2c_client *client; 38885c316dSMagnus Damm struct input_dev *input; 39885c316dSMagnus Damm int irq; 40885c316dSMagnus Damm }; 41885c316dSMagnus Damm 42885c316dSMagnus Damm static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11, 43885c316dSMagnus Damm 0x01, 0x06, 0x07, }; 44885c316dSMagnus Damm static const u_int8_t migor_ts_dis_seq[17] = { }; 45885c316dSMagnus Damm 46468792ebSDmitry Torokhov static irqreturn_t migor_ts_isr(int irq, void *dev_id) 47885c316dSMagnus Damm { 48468792ebSDmitry Torokhov struct migor_ts_priv *priv = dev_id; 49885c316dSMagnus Damm unsigned short xpos, ypos; 50885c316dSMagnus Damm unsigned char event; 51885c316dSMagnus Damm u_int8_t buf[16]; 52885c316dSMagnus Damm 53468792ebSDmitry Torokhov /* 54468792ebSDmitry Torokhov * The touch screen controller chip is hooked up to the CPU 55468792ebSDmitry Torokhov * using I2C and a single interrupt line. The interrupt line 56468792ebSDmitry Torokhov * is pulled low whenever someone taps the screen. To deassert 57468792ebSDmitry Torokhov * the interrupt line we need to acknowledge the interrupt by 58468792ebSDmitry Torokhov * communicating with the controller over the slow i2c bus. 59468792ebSDmitry Torokhov * 60468792ebSDmitry Torokhov * Since I2C bus controller may sleep we are using threaded 61468792ebSDmitry Torokhov * IRQ here. 62468792ebSDmitry Torokhov */ 63468792ebSDmitry Torokhov 64885c316dSMagnus Damm memset(buf, 0, sizeof(buf)); 65885c316dSMagnus Damm 66885c316dSMagnus Damm /* Set Index 0 */ 67885c316dSMagnus Damm buf[0] = 0; 68885c316dSMagnus Damm if (i2c_master_send(priv->client, buf, 1) != 1) { 69885c316dSMagnus Damm dev_err(&priv->client->dev, "Unable to write i2c index\n"); 70885c316dSMagnus Damm goto out; 71885c316dSMagnus Damm } 72885c316dSMagnus Damm 73885c316dSMagnus Damm /* Now do Page Read */ 74885c316dSMagnus Damm if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) { 75885c316dSMagnus Damm dev_err(&priv->client->dev, "Unable to read i2c page\n"); 76885c316dSMagnus Damm goto out; 77885c316dSMagnus Damm } 78885c316dSMagnus Damm 79885c316dSMagnus Damm ypos = ((buf[9] & 0x03) << 8 | buf[8]); 80885c316dSMagnus Damm xpos = ((buf[11] & 0x03) << 8 | buf[10]); 81885c316dSMagnus Damm event = buf[12]; 82885c316dSMagnus Damm 83468792ebSDmitry Torokhov switch (event) { 84468792ebSDmitry Torokhov case EVENT_PENDOWN: 85468792ebSDmitry Torokhov case EVENT_REPEAT: 86885c316dSMagnus Damm input_report_key(priv->input, BTN_TOUCH, 1); 87885c316dSMagnus Damm input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/ 88885c316dSMagnus Damm input_report_abs(priv->input, ABS_Y, xpos); 89885c316dSMagnus Damm input_sync(priv->input); 90468792ebSDmitry Torokhov break; 91468792ebSDmitry Torokhov 92468792ebSDmitry Torokhov case EVENT_PENUP: 93885c316dSMagnus Damm input_report_key(priv->input, BTN_TOUCH, 0); 94885c316dSMagnus Damm input_sync(priv->input); 95468792ebSDmitry Torokhov break; 96885c316dSMagnus Damm } 97468792ebSDmitry Torokhov 98885c316dSMagnus Damm out: 99885c316dSMagnus Damm return IRQ_HANDLED; 100885c316dSMagnus Damm } 101885c316dSMagnus Damm 102885c316dSMagnus Damm static int migor_ts_open(struct input_dev *dev) 103885c316dSMagnus Damm { 104885c316dSMagnus Damm struct migor_ts_priv *priv = input_get_drvdata(dev); 105885c316dSMagnus Damm struct i2c_client *client = priv->client; 106885c316dSMagnus Damm int count; 107885c316dSMagnus Damm 108885c316dSMagnus Damm /* enable controller */ 109885c316dSMagnus Damm count = i2c_master_send(client, migor_ts_ena_seq, 110885c316dSMagnus Damm sizeof(migor_ts_ena_seq)); 111885c316dSMagnus Damm if (count != sizeof(migor_ts_ena_seq)) { 112885c316dSMagnus Damm dev_err(&client->dev, "Unable to enable touchscreen.\n"); 113885c316dSMagnus Damm return -ENXIO; 114885c316dSMagnus Damm } 115885c316dSMagnus Damm 116885c316dSMagnus Damm return 0; 117885c316dSMagnus Damm } 118885c316dSMagnus Damm 119885c316dSMagnus Damm static void migor_ts_close(struct input_dev *dev) 120885c316dSMagnus Damm { 121885c316dSMagnus Damm struct migor_ts_priv *priv = input_get_drvdata(dev); 122885c316dSMagnus Damm struct i2c_client *client = priv->client; 123885c316dSMagnus Damm 124885c316dSMagnus Damm disable_irq(priv->irq); 125885c316dSMagnus Damm 126885c316dSMagnus Damm /* disable controller */ 127885c316dSMagnus Damm i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq)); 128885c316dSMagnus Damm 129885c316dSMagnus Damm enable_irq(priv->irq); 130885c316dSMagnus Damm } 131885c316dSMagnus Damm 132885c316dSMagnus Damm static int migor_ts_probe(struct i2c_client *client, 133885c316dSMagnus Damm const struct i2c_device_id *idp) 134885c316dSMagnus Damm { 135885c316dSMagnus Damm struct migor_ts_priv *priv; 136885c316dSMagnus Damm struct input_dev *input; 137885c316dSMagnus Damm int error; 138885c316dSMagnus Damm 139885c316dSMagnus Damm priv = kzalloc(sizeof(*priv), GFP_KERNEL); 140*ff4d0492SDmitry Torokhov input = input_allocate_device(); 141*ff4d0492SDmitry Torokhov if (!priv || !input) { 142*ff4d0492SDmitry Torokhov dev_err(&client->dev, "failed to allocate memory\n"); 143885c316dSMagnus Damm error = -ENOMEM; 144*ff4d0492SDmitry Torokhov goto err_free_mem; 145885c316dSMagnus Damm } 146885c316dSMagnus Damm 147*ff4d0492SDmitry Torokhov priv->client = client; 148*ff4d0492SDmitry Torokhov priv->input = input; 149*ff4d0492SDmitry Torokhov priv->irq = client->irq; 150885c316dSMagnus Damm 151885c316dSMagnus Damm input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 152*ff4d0492SDmitry Torokhov 153*ff4d0492SDmitry Torokhov __set_bit(BTN_TOUCH, input->keybit); 154885c316dSMagnus Damm 155885c316dSMagnus Damm input_set_abs_params(input, ABS_X, 95, 955, 0, 0); 156885c316dSMagnus Damm input_set_abs_params(input, ABS_Y, 85, 935, 0, 0); 157885c316dSMagnus Damm 158b6ce9ad0SMagnus Damm input->name = client->name; 159885c316dSMagnus Damm input->id.bustype = BUS_I2C; 160885c316dSMagnus Damm input->dev.parent = &client->dev; 161885c316dSMagnus Damm 162885c316dSMagnus Damm input->open = migor_ts_open; 163885c316dSMagnus Damm input->close = migor_ts_close; 164885c316dSMagnus Damm 165885c316dSMagnus Damm input_set_drvdata(input, priv); 166885c316dSMagnus Damm 167468792ebSDmitry Torokhov error = request_threaded_irq(priv->irq, NULL, migor_ts_isr, 168468792ebSDmitry Torokhov IRQF_TRIGGER_LOW | IRQF_ONESHOT, 169b6ce9ad0SMagnus Damm client->name, priv); 170885c316dSMagnus Damm if (error) { 171885c316dSMagnus Damm dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); 172*ff4d0492SDmitry Torokhov goto err_free_mem; 173885c316dSMagnus Damm } 174885c316dSMagnus Damm 175*ff4d0492SDmitry Torokhov error = input_register_device(input); 176*ff4d0492SDmitry Torokhov if (error) 177*ff4d0492SDmitry Torokhov goto err_free_irq; 178*ff4d0492SDmitry Torokhov 179ec208612SDmitry Torokhov i2c_set_clientdata(client, priv); 1800935ade0SMagnus Damm device_init_wakeup(&client->dev, 1); 181*ff4d0492SDmitry Torokhov 182885c316dSMagnus Damm return 0; 183885c316dSMagnus Damm 184*ff4d0492SDmitry Torokhov err_free_irq: 185*ff4d0492SDmitry Torokhov free_irq(priv->irq, priv); 186*ff4d0492SDmitry Torokhov err_free_mem: 187885c316dSMagnus Damm input_free_device(input); 188885c316dSMagnus Damm kfree(priv); 189885c316dSMagnus Damm return error; 190885c316dSMagnus Damm } 191885c316dSMagnus Damm 192885c316dSMagnus Damm static int migor_ts_remove(struct i2c_client *client) 193885c316dSMagnus Damm { 194ec208612SDmitry Torokhov struct migor_ts_priv *priv = i2c_get_clientdata(client); 195885c316dSMagnus Damm 196885c316dSMagnus Damm free_irq(priv->irq, priv); 197885c316dSMagnus Damm input_unregister_device(priv->input); 198885c316dSMagnus Damm kfree(priv); 199885c316dSMagnus Damm 200885c316dSMagnus Damm dev_set_drvdata(&client->dev, NULL); 201885c316dSMagnus Damm 202885c316dSMagnus Damm return 0; 203885c316dSMagnus Damm } 204885c316dSMagnus Damm 205d72e64ecSMark Brown static int migor_ts_suspend(struct device *dev) 2060935ade0SMagnus Damm { 207d72e64ecSMark Brown struct i2c_client *client = to_i2c_client(dev); 208ec208612SDmitry Torokhov struct migor_ts_priv *priv = i2c_get_clientdata(client); 2090935ade0SMagnus Damm 2100935ade0SMagnus Damm if (device_may_wakeup(&client->dev)) 2110935ade0SMagnus Damm enable_irq_wake(priv->irq); 2120935ade0SMagnus Damm 2130935ade0SMagnus Damm return 0; 2140935ade0SMagnus Damm } 2150935ade0SMagnus Damm 216d72e64ecSMark Brown static int migor_ts_resume(struct device *dev) 2170935ade0SMagnus Damm { 218d72e64ecSMark Brown struct i2c_client *client = to_i2c_client(dev); 219ec208612SDmitry Torokhov struct migor_ts_priv *priv = i2c_get_clientdata(client); 2200935ade0SMagnus Damm 2210935ade0SMagnus Damm if (device_may_wakeup(&client->dev)) 2220935ade0SMagnus Damm disable_irq_wake(priv->irq); 2230935ade0SMagnus Damm 2240935ade0SMagnus Damm return 0; 2250935ade0SMagnus Damm } 2260935ade0SMagnus Damm 227d72e64ecSMark Brown static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume); 228d72e64ecSMark Brown 229b6ce9ad0SMagnus Damm static const struct i2c_device_id migor_ts_id[] = { 230b6ce9ad0SMagnus Damm { "migor_ts", 0 }, 231b6ce9ad0SMagnus Damm { } 232b6ce9ad0SMagnus Damm }; 233b6ce9ad0SMagnus Damm MODULE_DEVICE_TABLE(i2c, migor_ts); 234b6ce9ad0SMagnus Damm 235885c316dSMagnus Damm static struct i2c_driver migor_ts_driver = { 236885c316dSMagnus Damm .driver = { 237885c316dSMagnus Damm .name = "migor_ts", 238d72e64ecSMark Brown .pm = &migor_ts_pm, 239885c316dSMagnus Damm }, 240885c316dSMagnus Damm .probe = migor_ts_probe, 241885c316dSMagnus Damm .remove = migor_ts_remove, 242b6ce9ad0SMagnus Damm .id_table = migor_ts_id, 243885c316dSMagnus Damm }; 244885c316dSMagnus Damm 245885c316dSMagnus Damm static int __init migor_ts_init(void) 246885c316dSMagnus Damm { 247885c316dSMagnus Damm return i2c_add_driver(&migor_ts_driver); 248885c316dSMagnus Damm } 249885c316dSMagnus Damm 250885c316dSMagnus Damm static void __exit migor_ts_exit(void) 251885c316dSMagnus Damm { 252885c316dSMagnus Damm i2c_del_driver(&migor_ts_driver); 253885c316dSMagnus Damm } 254885c316dSMagnus Damm 255885c316dSMagnus Damm MODULE_DESCRIPTION("MigoR Touchscreen driver"); 256885c316dSMagnus Damm MODULE_AUTHOR("Magnus Damm <damm@opensource.se>"); 257885c316dSMagnus Damm MODULE_LICENSE("GPL"); 258885c316dSMagnus Damm 259885c316dSMagnus Damm module_init(migor_ts_init); 260885c316dSMagnus Damm module_exit(migor_ts_exit); 261