1 /* 2 * TSI driver for Dialog DA9052 3 * 4 * Copyright(c) 2012 Dialog Semiconductor Ltd. 5 * 6 * Author: David Dajun Chen <dchen@diasemi.com> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2 of the License, or (at your 11 * option) any later version. 12 * 13 */ 14 #include <linux/module.h> 15 #include <linux/input.h> 16 #include <linux/delay.h> 17 #include <linux/platform_device.h> 18 #include <linux/interrupt.h> 19 20 #include <linux/mfd/da9052/reg.h> 21 #include <linux/mfd/da9052/da9052.h> 22 23 #define TSI_PEN_DOWN_STATUS 0x40 24 25 struct da9052_tsi { 26 struct da9052 *da9052; 27 struct input_dev *dev; 28 struct delayed_work ts_pen_work; 29 struct mutex mutex; 30 bool stopped; 31 bool adc_on; 32 }; 33 34 static void da9052_ts_adc_toggle(struct da9052_tsi *tsi, bool on) 35 { 36 da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 0, on); 37 tsi->adc_on = on; 38 } 39 40 static irqreturn_t da9052_ts_pendwn_irq(int irq, void *data) 41 { 42 struct da9052_tsi *tsi = data; 43 44 if (!tsi->stopped) { 45 /* Mask PEN_DOWN event and unmask TSI_READY event */ 46 da9052_disable_irq_nosync(tsi->da9052, DA9052_IRQ_PENDOWN); 47 da9052_enable_irq(tsi->da9052, DA9052_IRQ_TSIREADY); 48 49 da9052_ts_adc_toggle(tsi, true); 50 51 schedule_delayed_work(&tsi->ts_pen_work, HZ / 50); 52 } 53 54 return IRQ_HANDLED; 55 } 56 57 static void da9052_ts_read(struct da9052_tsi *tsi) 58 { 59 struct input_dev *input = tsi->dev; 60 int ret; 61 u16 x, y, z; 62 u8 v; 63 64 ret = da9052_reg_read(tsi->da9052, DA9052_TSI_X_MSB_REG); 65 if (ret < 0) 66 return; 67 68 x = (u16) ret; 69 70 ret = da9052_reg_read(tsi->da9052, DA9052_TSI_Y_MSB_REG); 71 if (ret < 0) 72 return; 73 74 y = (u16) ret; 75 76 ret = da9052_reg_read(tsi->da9052, DA9052_TSI_Z_MSB_REG); 77 if (ret < 0) 78 return; 79 80 z = (u16) ret; 81 82 ret = da9052_reg_read(tsi->da9052, DA9052_TSI_LSB_REG); 83 if (ret < 0) 84 return; 85 86 v = (u8) ret; 87 88 x = ((x << 2) & 0x3fc) | (v & 0x3); 89 y = ((y << 2) & 0x3fc) | ((v & 0xc) >> 2); 90 z = ((z << 2) & 0x3fc) | ((v & 0x30) >> 4); 91 92 input_report_key(input, BTN_TOUCH, 1); 93 input_report_abs(input, ABS_X, x); 94 input_report_abs(input, ABS_Y, y); 95 input_report_abs(input, ABS_PRESSURE, z); 96 input_sync(input); 97 } 98 99 static irqreturn_t da9052_ts_datardy_irq(int irq, void *data) 100 { 101 struct da9052_tsi *tsi = data; 102 103 da9052_ts_read(tsi); 104 105 return IRQ_HANDLED; 106 } 107 108 static void da9052_ts_pen_work(struct work_struct *work) 109 { 110 struct da9052_tsi *tsi = container_of(work, struct da9052_tsi, 111 ts_pen_work.work); 112 if (!tsi->stopped) { 113 int ret = da9052_reg_read(tsi->da9052, DA9052_TSI_LSB_REG); 114 if (ret < 0 || (ret & TSI_PEN_DOWN_STATUS)) { 115 /* Pen is still DOWN (or read error) */ 116 schedule_delayed_work(&tsi->ts_pen_work, HZ / 50); 117 } else { 118 struct input_dev *input = tsi->dev; 119 120 /* Pen UP */ 121 da9052_ts_adc_toggle(tsi, false); 122 123 /* Report Pen UP */ 124 input_report_key(input, BTN_TOUCH, 0); 125 input_report_abs(input, ABS_PRESSURE, 0); 126 input_sync(input); 127 128 /* 129 * FIXME: Fixes the unhandled irq issue when quick 130 * pen down and pen up events occurs 131 */ 132 ret = da9052_reg_update(tsi->da9052, 133 DA9052_EVENT_B_REG, 0xC0, 0xC0); 134 if (ret < 0) 135 return; 136 137 /* Mask TSI_READY event and unmask PEN_DOWN event */ 138 da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY); 139 da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN); 140 } 141 } 142 } 143 144 static int da9052_ts_configure_gpio(struct da9052 *da9052) 145 { 146 int error; 147 148 error = da9052_reg_update(da9052, DA9052_GPIO_2_3_REG, 0x30, 0); 149 if (error < 0) 150 return error; 151 152 error = da9052_reg_update(da9052, DA9052_GPIO_4_5_REG, 0x33, 0); 153 if (error < 0) 154 return error; 155 156 error = da9052_reg_update(da9052, DA9052_GPIO_6_7_REG, 0x33, 0); 157 if (error < 0) 158 return error; 159 160 return 0; 161 } 162 163 static int da9052_configure_tsi(struct da9052_tsi *tsi) 164 { 165 int error; 166 167 error = da9052_ts_configure_gpio(tsi->da9052); 168 if (error) 169 return error; 170 171 /* Measure TSI sample every 1ms */ 172 error = da9052_reg_update(tsi->da9052, DA9052_ADC_CONT_REG, 173 1 << 6, 1 << 6); 174 if (error < 0) 175 return error; 176 177 /* TSI_DELAY: 3 slots, TSI_SKIP: 0 slots, TSI_MODE: XYZP */ 178 error = da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 0xFC, 0xC0); 179 if (error < 0) 180 return error; 181 182 /* Supply TSIRef through LD09 */ 183 error = da9052_reg_write(tsi->da9052, DA9052_LDO9_REG, 0x59); 184 if (error < 0) 185 return error; 186 187 return 0; 188 } 189 190 static int da9052_ts_input_open(struct input_dev *input_dev) 191 { 192 struct da9052_tsi *tsi = input_get_drvdata(input_dev); 193 194 tsi->stopped = false; 195 mb(); 196 197 /* Unmask PEN_DOWN event */ 198 da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN); 199 200 /* Enable Pen Detect Circuit */ 201 return da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 202 1 << 1, 1 << 1); 203 } 204 205 static void da9052_ts_input_close(struct input_dev *input_dev) 206 { 207 struct da9052_tsi *tsi = input_get_drvdata(input_dev); 208 209 tsi->stopped = true; 210 mb(); 211 da9052_disable_irq(tsi->da9052, DA9052_IRQ_PENDOWN); 212 cancel_delayed_work_sync(&tsi->ts_pen_work); 213 214 if (tsi->adc_on) { 215 da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY); 216 da9052_ts_adc_toggle(tsi, false); 217 218 /* 219 * If ADC was on that means that pendwn IRQ was disabled 220 * twice and we need to enable it to keep enable/disable 221 * counter balanced. IRQ is still off though. 222 */ 223 da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN); 224 } 225 226 /* Disable Pen Detect Circuit */ 227 da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 1, 0); 228 } 229 230 static int da9052_ts_probe(struct platform_device *pdev) 231 { 232 struct da9052 *da9052; 233 struct da9052_tsi *tsi; 234 struct input_dev *input_dev; 235 int error; 236 237 da9052 = dev_get_drvdata(pdev->dev.parent); 238 if (!da9052) 239 return -EINVAL; 240 241 tsi = kzalloc(sizeof(struct da9052_tsi), GFP_KERNEL); 242 input_dev = input_allocate_device(); 243 if (!tsi || !input_dev) { 244 error = -ENOMEM; 245 goto err_free_mem; 246 } 247 248 tsi->da9052 = da9052; 249 tsi->dev = input_dev; 250 tsi->stopped = true; 251 INIT_DELAYED_WORK(&tsi->ts_pen_work, da9052_ts_pen_work); 252 253 input_dev->id.version = 0x0101; 254 input_dev->id.vendor = 0x15B6; 255 input_dev->id.product = 0x9052; 256 input_dev->name = "Dialog DA9052 TouchScreen Driver"; 257 input_dev->dev.parent = &pdev->dev; 258 input_dev->open = da9052_ts_input_open; 259 input_dev->close = da9052_ts_input_close; 260 261 __set_bit(EV_ABS, input_dev->evbit); 262 __set_bit(EV_KEY, input_dev->evbit); 263 __set_bit(BTN_TOUCH, input_dev->keybit); 264 265 input_set_abs_params(input_dev, ABS_X, 0, 1023, 0, 0); 266 input_set_abs_params(input_dev, ABS_Y, 0, 1023, 0, 0); 267 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 1023, 0, 0); 268 269 input_set_drvdata(input_dev, tsi); 270 271 /* Disable Pen Detect Circuit */ 272 da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 1, 0); 273 274 /* Disable ADC */ 275 da9052_ts_adc_toggle(tsi, false); 276 277 error = da9052_request_irq(tsi->da9052, DA9052_IRQ_PENDOWN, 278 "pendown-irq", da9052_ts_pendwn_irq, tsi); 279 if (error) { 280 dev_err(tsi->da9052->dev, 281 "Failed to register PENDWN IRQ: %d\n", error); 282 goto err_free_mem; 283 } 284 285 error = da9052_request_irq(tsi->da9052, DA9052_IRQ_TSIREADY, 286 "tsiready-irq", da9052_ts_datardy_irq, tsi); 287 if (error) { 288 dev_err(tsi->da9052->dev, 289 "Failed to register TSIRDY IRQ :%d\n", error); 290 goto err_free_pendwn_irq; 291 } 292 293 /* Mask PEN_DOWN and TSI_READY events */ 294 da9052_disable_irq(tsi->da9052, DA9052_IRQ_PENDOWN); 295 da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY); 296 297 error = da9052_configure_tsi(tsi); 298 if (error) 299 goto err_free_datardy_irq; 300 301 error = input_register_device(tsi->dev); 302 if (error) 303 goto err_free_datardy_irq; 304 305 platform_set_drvdata(pdev, tsi); 306 307 return 0; 308 309 err_free_datardy_irq: 310 da9052_free_irq(tsi->da9052, DA9052_IRQ_TSIREADY, tsi); 311 err_free_pendwn_irq: 312 da9052_free_irq(tsi->da9052, DA9052_IRQ_PENDOWN, tsi); 313 err_free_mem: 314 kfree(tsi); 315 input_free_device(input_dev); 316 317 return error; 318 } 319 320 static int da9052_ts_remove(struct platform_device *pdev) 321 { 322 struct da9052_tsi *tsi = platform_get_drvdata(pdev); 323 324 da9052_reg_write(tsi->da9052, DA9052_LDO9_REG, 0x19); 325 326 da9052_free_irq(tsi->da9052, DA9052_IRQ_TSIREADY, tsi); 327 da9052_free_irq(tsi->da9052, DA9052_IRQ_PENDOWN, tsi); 328 329 input_unregister_device(tsi->dev); 330 kfree(tsi); 331 332 platform_set_drvdata(pdev, NULL); 333 334 return 0; 335 } 336 337 static struct platform_driver da9052_tsi_driver = { 338 .probe = da9052_ts_probe, 339 .remove = da9052_ts_remove, 340 .driver = { 341 .name = "da9052-tsi", 342 .owner = THIS_MODULE, 343 }, 344 }; 345 346 module_platform_driver(da9052_tsi_driver); 347 348 MODULE_DESCRIPTION("Touchscreen driver for Dialog Semiconductor DA9052"); 349 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>"); 350 MODULE_LICENSE("GPL"); 351 MODULE_ALIAS("platform:da9052-tsi"); 352