Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Touch Screen driver for Renesas MIGO-R Platform |
| 3 | * |
| 4 | * Copyright (c) 2008 Magnus Damm |
| 5 | * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>, |
| 6 | * Kenati Technologies Pvt Ltd. |
| 7 | * |
| 8 | * This file is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This file is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/input.h> |
| 25 | #include <linux/interrupt.h> |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 26 | #include <linux/pm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 28 | #include <asm/io.h> |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/timer.h> |
| 31 | |
| 32 | #define EVENT_PENDOWN 1 |
| 33 | #define EVENT_REPEAT 2 |
| 34 | #define EVENT_PENUP 3 |
| 35 | |
| 36 | struct migor_ts_priv { |
| 37 | struct i2c_client *client; |
| 38 | struct input_dev *input; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 39 | int irq; |
| 40 | }; |
| 41 | |
| 42 | static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11, |
| 43 | 0x01, 0x06, 0x07, }; |
| 44 | static const u_int8_t migor_ts_dis_seq[17] = { }; |
| 45 | |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame^] | 46 | static irqreturn_t migor_ts_isr(int irq, void *dev_id) |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 47 | { |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame^] | 48 | struct migor_ts_priv *priv = dev_id; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 49 | unsigned short xpos, ypos; |
| 50 | unsigned char event; |
| 51 | u_int8_t buf[16]; |
| 52 | |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame^] | 53 | /* |
| 54 | * The touch screen controller chip is hooked up to the CPU |
| 55 | * using I2C and a single interrupt line. The interrupt line |
| 56 | * is pulled low whenever someone taps the screen. To deassert |
| 57 | * the interrupt line we need to acknowledge the interrupt by |
| 58 | * communicating with the controller over the slow i2c bus. |
| 59 | * |
| 60 | * Since I2C bus controller may sleep we are using threaded |
| 61 | * IRQ here. |
| 62 | */ |
| 63 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 64 | memset(buf, 0, sizeof(buf)); |
| 65 | |
| 66 | /* Set Index 0 */ |
| 67 | buf[0] = 0; |
| 68 | if (i2c_master_send(priv->client, buf, 1) != 1) { |
| 69 | dev_err(&priv->client->dev, "Unable to write i2c index\n"); |
| 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | /* Now do Page Read */ |
| 74 | if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) { |
| 75 | dev_err(&priv->client->dev, "Unable to read i2c page\n"); |
| 76 | goto out; |
| 77 | } |
| 78 | |
| 79 | ypos = ((buf[9] & 0x03) << 8 | buf[8]); |
| 80 | xpos = ((buf[11] & 0x03) << 8 | buf[10]); |
| 81 | event = buf[12]; |
| 82 | |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame^] | 83 | switch (event) { |
| 84 | case EVENT_PENDOWN: |
| 85 | case EVENT_REPEAT: |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 86 | input_report_key(priv->input, BTN_TOUCH, 1); |
| 87 | input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/ |
| 88 | input_report_abs(priv->input, ABS_Y, xpos); |
| 89 | input_sync(priv->input); |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame^] | 90 | break; |
| 91 | |
| 92 | case EVENT_PENUP: |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 93 | input_report_key(priv->input, BTN_TOUCH, 0); |
| 94 | input_sync(priv->input); |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame^] | 95 | break; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 96 | } |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame^] | 97 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 98 | out: |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 99 | return IRQ_HANDLED; |
| 100 | } |
| 101 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 102 | static int migor_ts_open(struct input_dev *dev) |
| 103 | { |
| 104 | struct migor_ts_priv *priv = input_get_drvdata(dev); |
| 105 | struct i2c_client *client = priv->client; |
| 106 | int count; |
| 107 | |
| 108 | /* enable controller */ |
| 109 | count = i2c_master_send(client, migor_ts_ena_seq, |
| 110 | sizeof(migor_ts_ena_seq)); |
| 111 | if (count != sizeof(migor_ts_ena_seq)) { |
| 112 | dev_err(&client->dev, "Unable to enable touchscreen.\n"); |
| 113 | return -ENXIO; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static void migor_ts_close(struct input_dev *dev) |
| 120 | { |
| 121 | struct migor_ts_priv *priv = input_get_drvdata(dev); |
| 122 | struct i2c_client *client = priv->client; |
| 123 | |
| 124 | disable_irq(priv->irq); |
| 125 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 126 | /* disable controller */ |
| 127 | i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq)); |
| 128 | |
| 129 | enable_irq(priv->irq); |
| 130 | } |
| 131 | |
| 132 | static int migor_ts_probe(struct i2c_client *client, |
| 133 | const struct i2c_device_id *idp) |
| 134 | { |
| 135 | struct migor_ts_priv *priv; |
| 136 | struct input_dev *input; |
| 137 | int error; |
| 138 | |
| 139 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 140 | if (!priv) { |
| 141 | dev_err(&client->dev, "failed to allocate driver data\n"); |
| 142 | error = -ENOMEM; |
| 143 | goto err0; |
| 144 | } |
| 145 | |
| 146 | dev_set_drvdata(&client->dev, priv); |
| 147 | |
| 148 | input = input_allocate_device(); |
| 149 | if (!input) { |
| 150 | dev_err(&client->dev, "Failed to allocate input device.\n"); |
| 151 | error = -ENOMEM; |
| 152 | goto err1; |
| 153 | } |
| 154 | |
| 155 | input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 156 | input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 157 | |
| 158 | input_set_abs_params(input, ABS_X, 95, 955, 0, 0); |
| 159 | input_set_abs_params(input, ABS_Y, 85, 935, 0, 0); |
| 160 | |
Magnus Damm | b6ce9ad | 2008-08-11 14:53:49 +0900 | [diff] [blame] | 161 | input->name = client->name; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 162 | input->id.bustype = BUS_I2C; |
| 163 | input->dev.parent = &client->dev; |
| 164 | |
| 165 | input->open = migor_ts_open; |
| 166 | input->close = migor_ts_close; |
| 167 | |
| 168 | input_set_drvdata(input, priv); |
| 169 | |
| 170 | priv->client = client; |
| 171 | priv->input = input; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 172 | priv->irq = client->irq; |
| 173 | |
| 174 | error = input_register_device(input); |
| 175 | if (error) |
| 176 | goto err1; |
| 177 | |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame^] | 178 | error = request_threaded_irq(priv->irq, NULL, migor_ts_isr, |
| 179 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 180 | client->name, priv); |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 181 | if (error) { |
| 182 | dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); |
| 183 | goto err2; |
| 184 | } |
| 185 | |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 186 | device_init_wakeup(&client->dev, 1); |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 187 | return 0; |
| 188 | |
| 189 | err2: |
| 190 | input_unregister_device(input); |
| 191 | input = NULL; /* so we dont try to free it below */ |
| 192 | err1: |
| 193 | input_free_device(input); |
| 194 | kfree(priv); |
| 195 | err0: |
| 196 | dev_set_drvdata(&client->dev, NULL); |
| 197 | return error; |
| 198 | } |
| 199 | |
| 200 | static int migor_ts_remove(struct i2c_client *client) |
| 201 | { |
| 202 | struct migor_ts_priv *priv = dev_get_drvdata(&client->dev); |
| 203 | |
| 204 | free_irq(priv->irq, priv); |
| 205 | input_unregister_device(priv->input); |
| 206 | kfree(priv); |
| 207 | |
| 208 | dev_set_drvdata(&client->dev, NULL); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 213 | static int migor_ts_suspend(struct device *dev) |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 214 | { |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 215 | struct i2c_client *client = to_i2c_client(dev); |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 216 | struct migor_ts_priv *priv = dev_get_drvdata(&client->dev); |
| 217 | |
| 218 | if (device_may_wakeup(&client->dev)) |
| 219 | enable_irq_wake(priv->irq); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 224 | static int migor_ts_resume(struct device *dev) |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 225 | { |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 226 | struct i2c_client *client = to_i2c_client(dev); |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 227 | struct migor_ts_priv *priv = dev_get_drvdata(&client->dev); |
| 228 | |
| 229 | if (device_may_wakeup(&client->dev)) |
| 230 | disable_irq_wake(priv->irq); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 235 | static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume); |
| 236 | |
Magnus Damm | b6ce9ad | 2008-08-11 14:53:49 +0900 | [diff] [blame] | 237 | static const struct i2c_device_id migor_ts_id[] = { |
| 238 | { "migor_ts", 0 }, |
| 239 | { } |
| 240 | }; |
| 241 | MODULE_DEVICE_TABLE(i2c, migor_ts); |
| 242 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 243 | static struct i2c_driver migor_ts_driver = { |
| 244 | .driver = { |
| 245 | .name = "migor_ts", |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 246 | .pm = &migor_ts_pm, |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 247 | }, |
| 248 | .probe = migor_ts_probe, |
| 249 | .remove = migor_ts_remove, |
Magnus Damm | b6ce9ad | 2008-08-11 14:53:49 +0900 | [diff] [blame] | 250 | .id_table = migor_ts_id, |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | static int __init migor_ts_init(void) |
| 254 | { |
| 255 | return i2c_add_driver(&migor_ts_driver); |
| 256 | } |
| 257 | |
| 258 | static void __exit migor_ts_exit(void) |
| 259 | { |
| 260 | i2c_del_driver(&migor_ts_driver); |
| 261 | } |
| 262 | |
| 263 | MODULE_DESCRIPTION("MigoR Touchscreen driver"); |
| 264 | MODULE_AUTHOR("Magnus Damm <damm@opensource.se>"); |
| 265 | MODULE_LICENSE("GPL"); |
| 266 | |
| 267 | module_init(migor_ts_init); |
| 268 | module_exit(migor_ts_exit); |