Kuninori Morimoto | dda2af7 | 2018-11-12 11:29:27 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Touch Screen driver for Renesas MIGO-R Platform |
| 4 | * |
| 5 | * Copyright (c) 2008 Magnus Damm |
| 6 | * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>, |
| 7 | * Kenati Technologies Pvt Ltd. |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/input.h> |
| 12 | #include <linux/interrupt.h> |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 13 | #include <linux/pm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/timer.h> |
| 18 | |
| 19 | #define EVENT_PENDOWN 1 |
| 20 | #define EVENT_REPEAT 2 |
| 21 | #define EVENT_PENUP 3 |
| 22 | |
| 23 | struct migor_ts_priv { |
| 24 | struct i2c_client *client; |
| 25 | struct input_dev *input; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 26 | int irq; |
| 27 | }; |
| 28 | |
| 29 | static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11, |
| 30 | 0x01, 0x06, 0x07, }; |
| 31 | static const u_int8_t migor_ts_dis_seq[17] = { }; |
| 32 | |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 33 | static irqreturn_t migor_ts_isr(int irq, void *dev_id) |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 34 | { |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 35 | struct migor_ts_priv *priv = dev_id; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 36 | unsigned short xpos, ypos; |
| 37 | unsigned char event; |
| 38 | u_int8_t buf[16]; |
| 39 | |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 40 | /* |
| 41 | * The touch screen controller chip is hooked up to the CPU |
| 42 | * using I2C and a single interrupt line. The interrupt line |
| 43 | * is pulled low whenever someone taps the screen. To deassert |
| 44 | * the interrupt line we need to acknowledge the interrupt by |
| 45 | * communicating with the controller over the slow i2c bus. |
| 46 | * |
| 47 | * Since I2C bus controller may sleep we are using threaded |
| 48 | * IRQ here. |
| 49 | */ |
| 50 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 51 | memset(buf, 0, sizeof(buf)); |
| 52 | |
| 53 | /* Set Index 0 */ |
| 54 | buf[0] = 0; |
| 55 | if (i2c_master_send(priv->client, buf, 1) != 1) { |
| 56 | dev_err(&priv->client->dev, "Unable to write i2c index\n"); |
| 57 | goto out; |
| 58 | } |
| 59 | |
| 60 | /* Now do Page Read */ |
| 61 | if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) { |
| 62 | dev_err(&priv->client->dev, "Unable to read i2c page\n"); |
| 63 | goto out; |
| 64 | } |
| 65 | |
| 66 | ypos = ((buf[9] & 0x03) << 8 | buf[8]); |
| 67 | xpos = ((buf[11] & 0x03) << 8 | buf[10]); |
| 68 | event = buf[12]; |
| 69 | |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 70 | switch (event) { |
| 71 | case EVENT_PENDOWN: |
| 72 | case EVENT_REPEAT: |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 73 | input_report_key(priv->input, BTN_TOUCH, 1); |
| 74 | input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/ |
| 75 | input_report_abs(priv->input, ABS_Y, xpos); |
| 76 | input_sync(priv->input); |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 77 | break; |
| 78 | |
| 79 | case EVENT_PENUP: |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 80 | input_report_key(priv->input, BTN_TOUCH, 0); |
| 81 | input_sync(priv->input); |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 82 | break; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 83 | } |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 84 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 85 | out: |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 86 | return IRQ_HANDLED; |
| 87 | } |
| 88 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 89 | static int migor_ts_open(struct input_dev *dev) |
| 90 | { |
| 91 | struct migor_ts_priv *priv = input_get_drvdata(dev); |
| 92 | struct i2c_client *client = priv->client; |
| 93 | int count; |
| 94 | |
| 95 | /* enable controller */ |
| 96 | count = i2c_master_send(client, migor_ts_ena_seq, |
| 97 | sizeof(migor_ts_ena_seq)); |
| 98 | if (count != sizeof(migor_ts_ena_seq)) { |
| 99 | dev_err(&client->dev, "Unable to enable touchscreen.\n"); |
| 100 | return -ENXIO; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static void migor_ts_close(struct input_dev *dev) |
| 107 | { |
| 108 | struct migor_ts_priv *priv = input_get_drvdata(dev); |
| 109 | struct i2c_client *client = priv->client; |
| 110 | |
| 111 | disable_irq(priv->irq); |
| 112 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 113 | /* disable controller */ |
| 114 | i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq)); |
| 115 | |
| 116 | enable_irq(priv->irq); |
| 117 | } |
| 118 | |
| 119 | static int migor_ts_probe(struct i2c_client *client, |
| 120 | const struct i2c_device_id *idp) |
| 121 | { |
| 122 | struct migor_ts_priv *priv; |
| 123 | struct input_dev *input; |
| 124 | int error; |
| 125 | |
| 126 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Dmitry Torokhov | ff4d049 | 2011-11-30 23:44:31 -0800 | [diff] [blame] | 127 | input = input_allocate_device(); |
| 128 | if (!priv || !input) { |
| 129 | dev_err(&client->dev, "failed to allocate memory\n"); |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 130 | error = -ENOMEM; |
Dmitry Torokhov | ff4d049 | 2011-11-30 23:44:31 -0800 | [diff] [blame] | 131 | goto err_free_mem; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Dmitry Torokhov | ff4d049 | 2011-11-30 23:44:31 -0800 | [diff] [blame] | 134 | priv->client = client; |
| 135 | priv->input = input; |
| 136 | priv->irq = client->irq; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 137 | |
| 138 | input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
Dmitry Torokhov | ff4d049 | 2011-11-30 23:44:31 -0800 | [diff] [blame] | 139 | |
| 140 | __set_bit(BTN_TOUCH, input->keybit); |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 141 | |
| 142 | input_set_abs_params(input, ABS_X, 95, 955, 0, 0); |
| 143 | input_set_abs_params(input, ABS_Y, 85, 935, 0, 0); |
| 144 | |
Magnus Damm | b6ce9ad | 2008-08-11 14:53:49 +0900 | [diff] [blame] | 145 | input->name = client->name; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 146 | input->id.bustype = BUS_I2C; |
| 147 | input->dev.parent = &client->dev; |
| 148 | |
| 149 | input->open = migor_ts_open; |
| 150 | input->close = migor_ts_close; |
| 151 | |
| 152 | input_set_drvdata(input, priv); |
| 153 | |
Dmitry Torokhov | 468792e | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 154 | error = request_threaded_irq(priv->irq, NULL, migor_ts_isr, |
| 155 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 156 | client->name, priv); |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 157 | if (error) { |
| 158 | dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); |
Dmitry Torokhov | ff4d049 | 2011-11-30 23:44:31 -0800 | [diff] [blame] | 159 | goto err_free_mem; |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Dmitry Torokhov | ff4d049 | 2011-11-30 23:44:31 -0800 | [diff] [blame] | 162 | error = input_register_device(input); |
| 163 | if (error) |
| 164 | goto err_free_irq; |
| 165 | |
Dmitry Torokhov | ec20861 | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 166 | i2c_set_clientdata(client, priv); |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 167 | device_init_wakeup(&client->dev, 1); |
Dmitry Torokhov | ff4d049 | 2011-11-30 23:44:31 -0800 | [diff] [blame] | 168 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 169 | return 0; |
| 170 | |
Dmitry Torokhov | ff4d049 | 2011-11-30 23:44:31 -0800 | [diff] [blame] | 171 | err_free_irq: |
| 172 | free_irq(priv->irq, priv); |
| 173 | err_free_mem: |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 174 | input_free_device(input); |
| 175 | kfree(priv); |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 176 | return error; |
| 177 | } |
| 178 | |
| 179 | static int migor_ts_remove(struct i2c_client *client) |
| 180 | { |
Dmitry Torokhov | ec20861 | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 181 | struct migor_ts_priv *priv = i2c_get_clientdata(client); |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 182 | |
| 183 | free_irq(priv->irq, priv); |
| 184 | input_unregister_device(priv->input); |
| 185 | kfree(priv); |
| 186 | |
| 187 | dev_set_drvdata(&client->dev, NULL); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Arnd Bergmann | c32feb0 | 2016-05-19 09:14:53 -0700 | [diff] [blame] | 192 | static int __maybe_unused migor_ts_suspend(struct device *dev) |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 193 | { |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 194 | struct i2c_client *client = to_i2c_client(dev); |
Dmitry Torokhov | ec20861 | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 195 | struct migor_ts_priv *priv = i2c_get_clientdata(client); |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 196 | |
| 197 | if (device_may_wakeup(&client->dev)) |
| 198 | enable_irq_wake(priv->irq); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Arnd Bergmann | c32feb0 | 2016-05-19 09:14:53 -0700 | [diff] [blame] | 203 | static int __maybe_unused migor_ts_resume(struct device *dev) |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 204 | { |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 205 | struct i2c_client *client = to_i2c_client(dev); |
Dmitry Torokhov | ec20861 | 2011-11-30 23:44:30 -0800 | [diff] [blame] | 206 | struct migor_ts_priv *priv = i2c_get_clientdata(client); |
Magnus Damm | 0935ade | 2009-04-01 14:49:41 +0000 | [diff] [blame] | 207 | |
| 208 | if (device_may_wakeup(&client->dev)) |
| 209 | disable_irq_wake(priv->irq); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 214 | static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume); |
| 215 | |
Magnus Damm | b6ce9ad | 2008-08-11 14:53:49 +0900 | [diff] [blame] | 216 | static const struct i2c_device_id migor_ts_id[] = { |
| 217 | { "migor_ts", 0 }, |
| 218 | { } |
| 219 | }; |
Javier Martinez Canillas | 8f4decc | 2016-05-17 11:10:22 -0700 | [diff] [blame] | 220 | MODULE_DEVICE_TABLE(i2c, migor_ts_id); |
Magnus Damm | b6ce9ad | 2008-08-11 14:53:49 +0900 | [diff] [blame] | 221 | |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 222 | static struct i2c_driver migor_ts_driver = { |
| 223 | .driver = { |
| 224 | .name = "migor_ts", |
Mark Brown | d72e64e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 225 | .pm = &migor_ts_pm, |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 226 | }, |
| 227 | .probe = migor_ts_probe, |
| 228 | .remove = migor_ts_remove, |
Magnus Damm | b6ce9ad | 2008-08-11 14:53:49 +0900 | [diff] [blame] | 229 | .id_table = migor_ts_id, |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 230 | }; |
| 231 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 232 | module_i2c_driver(migor_ts_driver); |
Magnus Damm | 885c316 | 2008-05-07 11:15:02 -0400 | [diff] [blame] | 233 | |
| 234 | MODULE_DESCRIPTION("MigoR Touchscreen driver"); |
| 235 | MODULE_AUTHOR("Magnus Damm <damm@opensource.se>"); |
| 236 | MODULE_LICENSE("GPL"); |