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); 140885c316dSMagnus Damm if (!priv) { 141885c316dSMagnus Damm dev_err(&client->dev, "failed to allocate driver data\n"); 142885c316dSMagnus Damm error = -ENOMEM; 143885c316dSMagnus Damm goto err0; 144885c316dSMagnus Damm } 145885c316dSMagnus Damm 146885c316dSMagnus Damm input = input_allocate_device(); 147885c316dSMagnus Damm if (!input) { 148885c316dSMagnus Damm dev_err(&client->dev, "Failed to allocate input device.\n"); 149885c316dSMagnus Damm error = -ENOMEM; 150885c316dSMagnus Damm goto err1; 151885c316dSMagnus Damm } 152885c316dSMagnus Damm 153885c316dSMagnus Damm input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 154885c316dSMagnus Damm input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); 155885c316dSMagnus Damm 156885c316dSMagnus Damm input_set_abs_params(input, ABS_X, 95, 955, 0, 0); 157885c316dSMagnus Damm input_set_abs_params(input, ABS_Y, 85, 935, 0, 0); 158885c316dSMagnus Damm 159b6ce9ad0SMagnus Damm input->name = client->name; 160885c316dSMagnus Damm input->id.bustype = BUS_I2C; 161885c316dSMagnus Damm input->dev.parent = &client->dev; 162885c316dSMagnus Damm 163885c316dSMagnus Damm input->open = migor_ts_open; 164885c316dSMagnus Damm input->close = migor_ts_close; 165885c316dSMagnus Damm 166885c316dSMagnus Damm input_set_drvdata(input, priv); 167885c316dSMagnus Damm 168885c316dSMagnus Damm priv->client = client; 169885c316dSMagnus Damm priv->input = input; 170885c316dSMagnus Damm priv->irq = client->irq; 171885c316dSMagnus Damm 172885c316dSMagnus Damm error = input_register_device(input); 173885c316dSMagnus Damm if (error) 174885c316dSMagnus Damm goto err1; 175885c316dSMagnus Damm 176468792ebSDmitry Torokhov error = request_threaded_irq(priv->irq, NULL, migor_ts_isr, 177468792ebSDmitry Torokhov IRQF_TRIGGER_LOW | IRQF_ONESHOT, 178b6ce9ad0SMagnus Damm client->name, priv); 179885c316dSMagnus Damm if (error) { 180885c316dSMagnus Damm dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); 181885c316dSMagnus Damm goto err2; 182885c316dSMagnus Damm } 183885c316dSMagnus Damm 184*ec208612SDmitry Torokhov i2c_set_clientdata(client, priv); 1850935ade0SMagnus Damm device_init_wakeup(&client->dev, 1); 186885c316dSMagnus Damm return 0; 187885c316dSMagnus Damm 188885c316dSMagnus Damm err2: 189885c316dSMagnus Damm input_unregister_device(input); 190885c316dSMagnus Damm input = NULL; /* so we dont try to free it below */ 191885c316dSMagnus Damm err1: 192885c316dSMagnus Damm input_free_device(input); 193885c316dSMagnus Damm kfree(priv); 194885c316dSMagnus Damm err0: 195885c316dSMagnus Damm dev_set_drvdata(&client->dev, NULL); 196885c316dSMagnus Damm return error; 197885c316dSMagnus Damm } 198885c316dSMagnus Damm 199885c316dSMagnus Damm static int migor_ts_remove(struct i2c_client *client) 200885c316dSMagnus Damm { 201*ec208612SDmitry Torokhov struct migor_ts_priv *priv = i2c_get_clientdata(client); 202885c316dSMagnus Damm 203885c316dSMagnus Damm free_irq(priv->irq, priv); 204885c316dSMagnus Damm input_unregister_device(priv->input); 205885c316dSMagnus Damm kfree(priv); 206885c316dSMagnus Damm 207885c316dSMagnus Damm dev_set_drvdata(&client->dev, NULL); 208885c316dSMagnus Damm 209885c316dSMagnus Damm return 0; 210885c316dSMagnus Damm } 211885c316dSMagnus Damm 212d72e64ecSMark Brown static int migor_ts_suspend(struct device *dev) 2130935ade0SMagnus Damm { 214d72e64ecSMark Brown struct i2c_client *client = to_i2c_client(dev); 215*ec208612SDmitry Torokhov struct migor_ts_priv *priv = i2c_get_clientdata(client); 2160935ade0SMagnus Damm 2170935ade0SMagnus Damm if (device_may_wakeup(&client->dev)) 2180935ade0SMagnus Damm enable_irq_wake(priv->irq); 2190935ade0SMagnus Damm 2200935ade0SMagnus Damm return 0; 2210935ade0SMagnus Damm } 2220935ade0SMagnus Damm 223d72e64ecSMark Brown static int migor_ts_resume(struct device *dev) 2240935ade0SMagnus Damm { 225d72e64ecSMark Brown struct i2c_client *client = to_i2c_client(dev); 226*ec208612SDmitry Torokhov struct migor_ts_priv *priv = i2c_get_clientdata(client); 2270935ade0SMagnus Damm 2280935ade0SMagnus Damm if (device_may_wakeup(&client->dev)) 2290935ade0SMagnus Damm disable_irq_wake(priv->irq); 2300935ade0SMagnus Damm 2310935ade0SMagnus Damm return 0; 2320935ade0SMagnus Damm } 2330935ade0SMagnus Damm 234d72e64ecSMark Brown static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume); 235d72e64ecSMark Brown 236b6ce9ad0SMagnus Damm static const struct i2c_device_id migor_ts_id[] = { 237b6ce9ad0SMagnus Damm { "migor_ts", 0 }, 238b6ce9ad0SMagnus Damm { } 239b6ce9ad0SMagnus Damm }; 240b6ce9ad0SMagnus Damm MODULE_DEVICE_TABLE(i2c, migor_ts); 241b6ce9ad0SMagnus Damm 242885c316dSMagnus Damm static struct i2c_driver migor_ts_driver = { 243885c316dSMagnus Damm .driver = { 244885c316dSMagnus Damm .name = "migor_ts", 245d72e64ecSMark Brown .pm = &migor_ts_pm, 246885c316dSMagnus Damm }, 247885c316dSMagnus Damm .probe = migor_ts_probe, 248885c316dSMagnus Damm .remove = migor_ts_remove, 249b6ce9ad0SMagnus Damm .id_table = migor_ts_id, 250885c316dSMagnus Damm }; 251885c316dSMagnus Damm 252885c316dSMagnus Damm static int __init migor_ts_init(void) 253885c316dSMagnus Damm { 254885c316dSMagnus Damm return i2c_add_driver(&migor_ts_driver); 255885c316dSMagnus Damm } 256885c316dSMagnus Damm 257885c316dSMagnus Damm static void __exit migor_ts_exit(void) 258885c316dSMagnus Damm { 259885c316dSMagnus Damm i2c_del_driver(&migor_ts_driver); 260885c316dSMagnus Damm } 261885c316dSMagnus Damm 262885c316dSMagnus Damm MODULE_DESCRIPTION("MigoR Touchscreen driver"); 263885c316dSMagnus Damm MODULE_AUTHOR("Magnus Damm <damm@opensource.se>"); 264885c316dSMagnus Damm MODULE_LICENSE("GPL"); 265885c316dSMagnus Damm 266885c316dSMagnus Damm module_init(migor_ts_init); 267885c316dSMagnus Damm module_exit(migor_ts_exit); 268