18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Touch Screen driver for Renesas MIGO-R Platform 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2008 Magnus Damm 68c2ecf20Sopenharmony_ci * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>, 78c2ecf20Sopenharmony_ci * Kenati Technologies Pvt Ltd. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/input.h> 128c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 138c2ecf20Sopenharmony_ci#include <linux/pm.h> 148c2ecf20Sopenharmony_ci#include <linux/slab.h> 158c2ecf20Sopenharmony_ci#include <asm/io.h> 168c2ecf20Sopenharmony_ci#include <linux/i2c.h> 178c2ecf20Sopenharmony_ci#include <linux/timer.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define EVENT_PENDOWN 1 208c2ecf20Sopenharmony_ci#define EVENT_REPEAT 2 218c2ecf20Sopenharmony_ci#define EVENT_PENUP 3 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistruct migor_ts_priv { 248c2ecf20Sopenharmony_ci struct i2c_client *client; 258c2ecf20Sopenharmony_ci struct input_dev *input; 268c2ecf20Sopenharmony_ci int irq; 278c2ecf20Sopenharmony_ci}; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11, 308c2ecf20Sopenharmony_ci 0x01, 0x06, 0x07, }; 318c2ecf20Sopenharmony_cistatic const u_int8_t migor_ts_dis_seq[17] = { }; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic irqreturn_t migor_ts_isr(int irq, void *dev_id) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci struct migor_ts_priv *priv = dev_id; 368c2ecf20Sopenharmony_ci unsigned short xpos, ypos; 378c2ecf20Sopenharmony_ci unsigned char event; 388c2ecf20Sopenharmony_ci u_int8_t buf[16]; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci /* 418c2ecf20Sopenharmony_ci * The touch screen controller chip is hooked up to the CPU 428c2ecf20Sopenharmony_ci * using I2C and a single interrupt line. The interrupt line 438c2ecf20Sopenharmony_ci * is pulled low whenever someone taps the screen. To deassert 448c2ecf20Sopenharmony_ci * the interrupt line we need to acknowledge the interrupt by 458c2ecf20Sopenharmony_ci * communicating with the controller over the slow i2c bus. 468c2ecf20Sopenharmony_ci * 478c2ecf20Sopenharmony_ci * Since I2C bus controller may sleep we are using threaded 488c2ecf20Sopenharmony_ci * IRQ here. 498c2ecf20Sopenharmony_ci */ 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci memset(buf, 0, sizeof(buf)); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci /* Set Index 0 */ 548c2ecf20Sopenharmony_ci buf[0] = 0; 558c2ecf20Sopenharmony_ci if (i2c_master_send(priv->client, buf, 1) != 1) { 568c2ecf20Sopenharmony_ci dev_err(&priv->client->dev, "Unable to write i2c index\n"); 578c2ecf20Sopenharmony_ci goto out; 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci /* Now do Page Read */ 618c2ecf20Sopenharmony_ci if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) { 628c2ecf20Sopenharmony_ci dev_err(&priv->client->dev, "Unable to read i2c page\n"); 638c2ecf20Sopenharmony_ci goto out; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci ypos = ((buf[9] & 0x03) << 8 | buf[8]); 678c2ecf20Sopenharmony_ci xpos = ((buf[11] & 0x03) << 8 | buf[10]); 688c2ecf20Sopenharmony_ci event = buf[12]; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci switch (event) { 718c2ecf20Sopenharmony_ci case EVENT_PENDOWN: 728c2ecf20Sopenharmony_ci case EVENT_REPEAT: 738c2ecf20Sopenharmony_ci input_report_key(priv->input, BTN_TOUCH, 1); 748c2ecf20Sopenharmony_ci input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/ 758c2ecf20Sopenharmony_ci input_report_abs(priv->input, ABS_Y, xpos); 768c2ecf20Sopenharmony_ci input_sync(priv->input); 778c2ecf20Sopenharmony_ci break; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci case EVENT_PENUP: 808c2ecf20Sopenharmony_ci input_report_key(priv->input, BTN_TOUCH, 0); 818c2ecf20Sopenharmony_ci input_sync(priv->input); 828c2ecf20Sopenharmony_ci break; 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci out: 868c2ecf20Sopenharmony_ci return IRQ_HANDLED; 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistatic int migor_ts_open(struct input_dev *dev) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci struct migor_ts_priv *priv = input_get_drvdata(dev); 928c2ecf20Sopenharmony_ci struct i2c_client *client = priv->client; 938c2ecf20Sopenharmony_ci int count; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci /* enable controller */ 968c2ecf20Sopenharmony_ci count = i2c_master_send(client, migor_ts_ena_seq, 978c2ecf20Sopenharmony_ci sizeof(migor_ts_ena_seq)); 988c2ecf20Sopenharmony_ci if (count != sizeof(migor_ts_ena_seq)) { 998c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to enable touchscreen.\n"); 1008c2ecf20Sopenharmony_ci return -ENXIO; 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic void migor_ts_close(struct input_dev *dev) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci struct migor_ts_priv *priv = input_get_drvdata(dev); 1098c2ecf20Sopenharmony_ci struct i2c_client *client = priv->client; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci disable_irq(priv->irq); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci /* disable controller */ 1148c2ecf20Sopenharmony_ci i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq)); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci enable_irq(priv->irq); 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic int migor_ts_probe(struct i2c_client *client, 1208c2ecf20Sopenharmony_ci const struct i2c_device_id *idp) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci struct migor_ts_priv *priv; 1238c2ecf20Sopenharmony_ci struct input_dev *input; 1248c2ecf20Sopenharmony_ci int error; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci priv = kzalloc(sizeof(*priv), GFP_KERNEL); 1278c2ecf20Sopenharmony_ci input = input_allocate_device(); 1288c2ecf20Sopenharmony_ci if (!priv || !input) { 1298c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to allocate memory\n"); 1308c2ecf20Sopenharmony_ci error = -ENOMEM; 1318c2ecf20Sopenharmony_ci goto err_free_mem; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci priv->client = client; 1358c2ecf20Sopenharmony_ci priv->input = input; 1368c2ecf20Sopenharmony_ci priv->irq = client->irq; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci __set_bit(BTN_TOUCH, input->keybit); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci input_set_abs_params(input, ABS_X, 95, 955, 0, 0); 1438c2ecf20Sopenharmony_ci input_set_abs_params(input, ABS_Y, 85, 935, 0, 0); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci input->name = client->name; 1468c2ecf20Sopenharmony_ci input->id.bustype = BUS_I2C; 1478c2ecf20Sopenharmony_ci input->dev.parent = &client->dev; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci input->open = migor_ts_open; 1508c2ecf20Sopenharmony_ci input->close = migor_ts_close; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci input_set_drvdata(input, priv); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci error = request_threaded_irq(priv->irq, NULL, migor_ts_isr, 1558c2ecf20Sopenharmony_ci IRQF_TRIGGER_LOW | IRQF_ONESHOT, 1568c2ecf20Sopenharmony_ci client->name, priv); 1578c2ecf20Sopenharmony_ci if (error) { 1588c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); 1598c2ecf20Sopenharmony_ci goto err_free_mem; 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci error = input_register_device(input); 1638c2ecf20Sopenharmony_ci if (error) 1648c2ecf20Sopenharmony_ci goto err_free_irq; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci i2c_set_clientdata(client, priv); 1678c2ecf20Sopenharmony_ci device_init_wakeup(&client->dev, 1); 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci return 0; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci err_free_irq: 1728c2ecf20Sopenharmony_ci free_irq(priv->irq, priv); 1738c2ecf20Sopenharmony_ci err_free_mem: 1748c2ecf20Sopenharmony_ci input_free_device(input); 1758c2ecf20Sopenharmony_ci kfree(priv); 1768c2ecf20Sopenharmony_ci return error; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic int migor_ts_remove(struct i2c_client *client) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci struct migor_ts_priv *priv = i2c_get_clientdata(client); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci free_irq(priv->irq, priv); 1848c2ecf20Sopenharmony_ci input_unregister_device(priv->input); 1858c2ecf20Sopenharmony_ci kfree(priv); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci dev_set_drvdata(&client->dev, NULL); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci return 0; 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic int __maybe_unused migor_ts_suspend(struct device *dev) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 1958c2ecf20Sopenharmony_ci struct migor_ts_priv *priv = i2c_get_clientdata(client); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci if (device_may_wakeup(&client->dev)) 1988c2ecf20Sopenharmony_ci enable_irq_wake(priv->irq); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci return 0; 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic int __maybe_unused migor_ts_resume(struct device *dev) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 2068c2ecf20Sopenharmony_ci struct migor_ts_priv *priv = i2c_get_clientdata(client); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci if (device_may_wakeup(&client->dev)) 2098c2ecf20Sopenharmony_ci disable_irq_wake(priv->irq); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci return 0; 2128c2ecf20Sopenharmony_ci} 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_cistatic const struct i2c_device_id migor_ts_id[] = { 2178c2ecf20Sopenharmony_ci { "migor_ts", 0 }, 2188c2ecf20Sopenharmony_ci { } 2198c2ecf20Sopenharmony_ci}; 2208c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, migor_ts_id); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistatic struct i2c_driver migor_ts_driver = { 2238c2ecf20Sopenharmony_ci .driver = { 2248c2ecf20Sopenharmony_ci .name = "migor_ts", 2258c2ecf20Sopenharmony_ci .pm = &migor_ts_pm, 2268c2ecf20Sopenharmony_ci }, 2278c2ecf20Sopenharmony_ci .probe = migor_ts_probe, 2288c2ecf20Sopenharmony_ci .remove = migor_ts_remove, 2298c2ecf20Sopenharmony_ci .id_table = migor_ts_id, 2308c2ecf20Sopenharmony_ci}; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cimodule_i2c_driver(migor_ts_driver); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MigoR Touchscreen driver"); 2358c2ecf20Sopenharmony_ciMODULE_AUTHOR("Magnus Damm <damm@opensource.se>"); 2368c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 237