1 /* 2 * Touchscreen driver for the TS-4800 board 3 * 4 * Copyright (c) 2015 - Savoir-faire Linux 5 * 6 * This file is licensed under the terms of the GNU General Public 7 * License version 2. This program is licensed "as is" without any 8 * warranty of any kind, whether express or implied. 9 */ 10 11 #include <linux/bitops.h> 12 #include <linux/input.h> 13 #include <linux/input-polldev.h> 14 #include <linux/io.h> 15 #include <linux/kernel.h> 16 #include <linux/mfd/syscon.h> 17 #include <linux/module.h> 18 #include <linux/of.h> 19 #include <linux/platform_device.h> 20 #include <linux/regmap.h> 21 22 /* polling interval in ms */ 23 #define POLL_INTERVAL 3 24 25 #define DEBOUNCE_COUNT 1 26 27 /* sensor values are 12-bit wide */ 28 #define MAX_12BIT ((1 << 12) - 1) 29 30 #define PENDOWN_MASK 0x1 31 32 #define X_OFFSET 0x0 33 #define Y_OFFSET 0x2 34 35 struct ts4800_ts { 36 struct input_polled_dev *poll_dev; 37 struct device *dev; 38 char phys[32]; 39 40 void __iomem *base; 41 struct regmap *regmap; 42 unsigned int reg; 43 unsigned int bit; 44 45 bool pendown; 46 int debounce; 47 }; 48 49 static void ts4800_ts_open(struct input_polled_dev *dev) 50 { 51 struct ts4800_ts *ts = dev->private; 52 int ret; 53 54 ts->pendown = false; 55 ts->debounce = DEBOUNCE_COUNT; 56 57 ret = regmap_update_bits(ts->regmap, ts->reg, ts->bit, ts->bit); 58 if (ret) 59 dev_warn(ts->dev, "Failed to enable touchscreen\n"); 60 } 61 62 static void ts4800_ts_close(struct input_polled_dev *dev) 63 { 64 struct ts4800_ts *ts = dev->private; 65 int ret; 66 67 ret = regmap_update_bits(ts->regmap, ts->reg, ts->bit, 0); 68 if (ret) 69 dev_warn(ts->dev, "Failed to disable touchscreen\n"); 70 71 } 72 73 static void ts4800_ts_poll(struct input_polled_dev *dev) 74 { 75 struct input_dev *input_dev = dev->input; 76 struct ts4800_ts *ts = dev->private; 77 u16 last_x = readw(ts->base + X_OFFSET); 78 u16 last_y = readw(ts->base + Y_OFFSET); 79 bool pendown = last_x & PENDOWN_MASK; 80 81 if (pendown) { 82 if (ts->debounce) { 83 ts->debounce--; 84 return; 85 } 86 87 if (!ts->pendown) { 88 input_report_key(input_dev, BTN_TOUCH, 1); 89 ts->pendown = true; 90 } 91 92 last_x = ((~last_x) >> 4) & MAX_12BIT; 93 last_y = ((~last_y) >> 4) & MAX_12BIT; 94 95 input_report_abs(input_dev, ABS_X, last_x); 96 input_report_abs(input_dev, ABS_Y, last_y); 97 input_sync(input_dev); 98 } else if (ts->pendown) { 99 ts->pendown = false; 100 ts->debounce = DEBOUNCE_COUNT; 101 input_report_key(input_dev, BTN_TOUCH, 0); 102 input_sync(input_dev); 103 } 104 } 105 106 static int ts4800_parse_dt(struct platform_device *pdev, 107 struct ts4800_ts *ts) 108 { 109 struct device *dev = &pdev->dev; 110 struct device_node *np = dev->of_node; 111 struct device_node *syscon_np; 112 u32 reg, bit; 113 int error; 114 115 syscon_np = of_parse_phandle(np, "syscon", 0); 116 if (!syscon_np) { 117 dev_err(dev, "no syscon property\n"); 118 return -ENODEV; 119 } 120 121 ts->regmap = syscon_node_to_regmap(syscon_np); 122 of_node_put(syscon_np); 123 if (IS_ERR(ts->regmap)) { 124 dev_err(dev, "cannot get parent's regmap\n"); 125 return PTR_ERR(ts->regmap); 126 } 127 128 error = of_property_read_u32_index(np, "syscon", 1, ®); 129 if (error < 0) { 130 dev_err(dev, "no offset in syscon\n"); 131 return error; 132 } 133 134 ts->reg = reg; 135 136 error = of_property_read_u32_index(np, "syscon", 2, &bit); 137 if (error < 0) { 138 dev_err(dev, "no bit in syscon\n"); 139 return error; 140 } 141 142 ts->bit = BIT(bit); 143 144 return 0; 145 } 146 147 static int ts4800_ts_probe(struct platform_device *pdev) 148 { 149 struct input_polled_dev *poll_dev; 150 struct ts4800_ts *ts; 151 int error; 152 153 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL); 154 if (!ts) 155 return -ENOMEM; 156 157 error = ts4800_parse_dt(pdev, ts); 158 if (error) 159 return error; 160 161 ts->base = devm_platform_ioremap_resource(pdev, 0); 162 if (IS_ERR(ts->base)) 163 return PTR_ERR(ts->base); 164 165 poll_dev = devm_input_allocate_polled_device(&pdev->dev); 166 if (!poll_dev) 167 return -ENOMEM; 168 169 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&pdev->dev)); 170 ts->poll_dev = poll_dev; 171 ts->dev = &pdev->dev; 172 173 poll_dev->private = ts; 174 poll_dev->poll_interval = POLL_INTERVAL; 175 poll_dev->open = ts4800_ts_open; 176 poll_dev->close = ts4800_ts_close; 177 poll_dev->poll = ts4800_ts_poll; 178 179 poll_dev->input->name = "TS-4800 Touchscreen"; 180 poll_dev->input->phys = ts->phys; 181 182 input_set_capability(poll_dev->input, EV_KEY, BTN_TOUCH); 183 input_set_abs_params(poll_dev->input, ABS_X, 0, MAX_12BIT, 0, 0); 184 input_set_abs_params(poll_dev->input, ABS_Y, 0, MAX_12BIT, 0, 0); 185 186 error = input_register_polled_device(poll_dev); 187 if (error) { 188 dev_err(&pdev->dev, 189 "Unabled to register polled input device (%d)\n", 190 error); 191 return error; 192 } 193 194 return 0; 195 } 196 197 static const struct of_device_id ts4800_ts_of_match[] = { 198 { .compatible = "technologic,ts4800-ts", }, 199 { }, 200 }; 201 MODULE_DEVICE_TABLE(of, ts4800_ts_of_match); 202 203 static struct platform_driver ts4800_ts_driver = { 204 .driver = { 205 .name = "ts4800-ts", 206 .of_match_table = ts4800_ts_of_match, 207 }, 208 .probe = ts4800_ts_probe, 209 }; 210 module_platform_driver(ts4800_ts_driver); 211 212 MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>"); 213 MODULE_DESCRIPTION("TS-4800 Touchscreen Driver"); 214 MODULE_LICENSE("GPL v2"); 215 MODULE_ALIAS("platform:ts4800_ts"); 216