0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/sched.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/device.h>
0014 #include <linux/of.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/input.h>
0017 #include <linux/input/touchscreen.h>
0018 #include <linux/slab.h>
0019 #include <linux/delay.h>
0020 #include <linux/i2c.h>
0021 #include <linux/workqueue.h>
0022
0023 #include <linux/mfd/stmpe.h>
0024
0025
0026
0027
0028 #define STMPE_REG_INT_STA 0x0B
0029 #define STMPE_REG_TSC_CTRL 0x40
0030 #define STMPE_REG_TSC_CFG 0x41
0031 #define STMPE_REG_FIFO_TH 0x4A
0032 #define STMPE_REG_FIFO_STA 0x4B
0033 #define STMPE_REG_FIFO_SIZE 0x4C
0034 #define STMPE_REG_TSC_DATA_XYZ 0x52
0035 #define STMPE_REG_TSC_FRACTION_Z 0x56
0036 #define STMPE_REG_TSC_I_DRIVE 0x58
0037
0038 #define OP_MOD_XYZ 0
0039
0040 #define STMPE_TSC_CTRL_TSC_EN (1<<0)
0041
0042 #define STMPE_FIFO_STA_RESET (1<<0)
0043
0044 #define STMPE_IRQ_TOUCH_DET 0
0045
0046 #define STMPE_TS_NAME "stmpe-ts"
0047 #define XY_MASK 0xfff
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 struct stmpe_touch {
0073 struct stmpe *stmpe;
0074 struct input_dev *idev;
0075 struct delayed_work work;
0076 struct device *dev;
0077 struct touchscreen_properties prop;
0078 u8 ave_ctrl;
0079 u8 touch_det_delay;
0080 u8 settling;
0081 u8 fraction_z;
0082 u8 i_drive;
0083 };
0084
0085 static int __stmpe_reset_fifo(struct stmpe *stmpe)
0086 {
0087 int ret;
0088
0089 ret = stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
0090 STMPE_FIFO_STA_RESET, STMPE_FIFO_STA_RESET);
0091 if (ret)
0092 return ret;
0093
0094 return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
0095 STMPE_FIFO_STA_RESET, 0);
0096 }
0097
0098 static void stmpe_work(struct work_struct *work)
0099 {
0100 int int_sta;
0101 u32 timeout = 40;
0102
0103 struct stmpe_touch *ts =
0104 container_of(work, struct stmpe_touch, work.work);
0105
0106 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
0107
0108
0109
0110
0111
0112
0113
0114
0115 while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {
0116 timeout--;
0117 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
0118 udelay(100);
0119 }
0120
0121
0122 __stmpe_reset_fifo(ts->stmpe);
0123
0124 input_report_abs(ts->idev, ABS_PRESSURE, 0);
0125 input_report_key(ts->idev, BTN_TOUCH, 0);
0126 input_sync(ts->idev);
0127 }
0128
0129 static irqreturn_t stmpe_ts_handler(int irq, void *data)
0130 {
0131 u8 data_set[4];
0132 int x, y, z;
0133 struct stmpe_touch *ts = data;
0134
0135
0136
0137
0138
0139 cancel_delayed_work_sync(&ts->work);
0140
0141
0142
0143
0144
0145
0146
0147 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
0148 STMPE_TSC_CTRL_TSC_EN, 0);
0149
0150 stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
0151
0152 x = (data_set[0] << 4) | (data_set[1] >> 4);
0153 y = ((data_set[1] & 0xf) << 8) | data_set[2];
0154 z = data_set[3];
0155
0156 touchscreen_report_pos(ts->idev, &ts->prop, x, y, false);
0157 input_report_abs(ts->idev, ABS_PRESSURE, z);
0158 input_report_key(ts->idev, BTN_TOUCH, 1);
0159 input_sync(ts->idev);
0160
0161
0162 __stmpe_reset_fifo(ts->stmpe);
0163
0164
0165 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
0166 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
0167
0168
0169 schedule_delayed_work(&ts->work, msecs_to_jiffies(50));
0170
0171 return IRQ_HANDLED;
0172 }
0173
0174 static int stmpe_init_hw(struct stmpe_touch *ts)
0175 {
0176 int ret;
0177 u8 tsc_cfg, tsc_cfg_mask;
0178 struct stmpe *stmpe = ts->stmpe;
0179 struct device *dev = ts->dev;
0180
0181 ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
0182 if (ret) {
0183 dev_err(dev, "Could not enable clock for ADC and TS\n");
0184 return ret;
0185 }
0186
0187 ret = stmpe811_adc_common_init(stmpe);
0188 if (ret) {
0189 stmpe_disable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
0190 return ret;
0191 }
0192
0193 tsc_cfg = STMPE_AVE_CTRL(ts->ave_ctrl) |
0194 STMPE_DET_DELAY(ts->touch_det_delay) |
0195 STMPE_SETTLING(ts->settling);
0196 tsc_cfg_mask = STMPE_AVE_CTRL(0xff) | STMPE_DET_DELAY(0xff) |
0197 STMPE_SETTLING(0xff);
0198
0199 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
0200 if (ret) {
0201 dev_err(dev, "Could not config touch\n");
0202 return ret;
0203 }
0204
0205 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
0206 STMPE_FRACTION_Z(0xff), STMPE_FRACTION_Z(ts->fraction_z));
0207 if (ret) {
0208 dev_err(dev, "Could not config touch\n");
0209 return ret;
0210 }
0211
0212 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
0213 STMPE_I_DRIVE(0xff), STMPE_I_DRIVE(ts->i_drive));
0214 if (ret) {
0215 dev_err(dev, "Could not config touch\n");
0216 return ret;
0217 }
0218
0219
0220 ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
0221 if (ret) {
0222 dev_err(dev, "Could not set FIFO\n");
0223 return ret;
0224 }
0225
0226 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
0227 STMPE_OP_MODE(0xff), STMPE_OP_MODE(OP_MOD_XYZ));
0228 if (ret) {
0229 dev_err(dev, "Could not set mode\n");
0230 return ret;
0231 }
0232
0233 return 0;
0234 }
0235
0236 static int stmpe_ts_open(struct input_dev *dev)
0237 {
0238 struct stmpe_touch *ts = input_get_drvdata(dev);
0239 int ret = 0;
0240
0241 ret = __stmpe_reset_fifo(ts->stmpe);
0242 if (ret)
0243 return ret;
0244
0245 return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
0246 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
0247 }
0248
0249 static void stmpe_ts_close(struct input_dev *dev)
0250 {
0251 struct stmpe_touch *ts = input_get_drvdata(dev);
0252
0253 cancel_delayed_work_sync(&ts->work);
0254
0255 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
0256 STMPE_TSC_CTRL_TSC_EN, 0);
0257 }
0258
0259 static void stmpe_ts_get_platform_info(struct platform_device *pdev,
0260 struct stmpe_touch *ts)
0261 {
0262 struct device_node *np = pdev->dev.of_node;
0263 u32 val;
0264
0265 if (np) {
0266 if (!of_property_read_u32(np, "st,sample-time", &val))
0267 ts->stmpe->sample_time = val;
0268 if (!of_property_read_u32(np, "st,mod-12b", &val))
0269 ts->stmpe->mod_12b = val;
0270 if (!of_property_read_u32(np, "st,ref-sel", &val))
0271 ts->stmpe->ref_sel = val;
0272 if (!of_property_read_u32(np, "st,adc-freq", &val))
0273 ts->stmpe->adc_freq = val;
0274 if (!of_property_read_u32(np, "st,ave-ctrl", &val))
0275 ts->ave_ctrl = val;
0276 if (!of_property_read_u32(np, "st,touch-det-delay", &val))
0277 ts->touch_det_delay = val;
0278 if (!of_property_read_u32(np, "st,settling", &val))
0279 ts->settling = val;
0280 if (!of_property_read_u32(np, "st,fraction-z", &val))
0281 ts->fraction_z = val;
0282 if (!of_property_read_u32(np, "st,i-drive", &val))
0283 ts->i_drive = val;
0284 }
0285 }
0286
0287 static int stmpe_input_probe(struct platform_device *pdev)
0288 {
0289 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
0290 struct stmpe_touch *ts;
0291 struct input_dev *idev;
0292 int error;
0293 int ts_irq;
0294
0295 ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
0296 if (ts_irq < 0)
0297 return ts_irq;
0298
0299 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
0300 if (!ts)
0301 return -ENOMEM;
0302
0303 idev = devm_input_allocate_device(&pdev->dev);
0304 if (!idev)
0305 return -ENOMEM;
0306
0307 platform_set_drvdata(pdev, ts);
0308 ts->stmpe = stmpe;
0309 ts->idev = idev;
0310 ts->dev = &pdev->dev;
0311
0312 stmpe_ts_get_platform_info(pdev, ts);
0313
0314 INIT_DELAYED_WORK(&ts->work, stmpe_work);
0315
0316 error = devm_request_threaded_irq(&pdev->dev, ts_irq,
0317 NULL, stmpe_ts_handler,
0318 IRQF_ONESHOT, STMPE_TS_NAME, ts);
0319 if (error) {
0320 dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
0321 return error;
0322 }
0323
0324 error = stmpe_init_hw(ts);
0325 if (error)
0326 return error;
0327
0328 idev->name = STMPE_TS_NAME;
0329 idev->phys = STMPE_TS_NAME"/input0";
0330 idev->id.bustype = BUS_I2C;
0331
0332 idev->open = stmpe_ts_open;
0333 idev->close = stmpe_ts_close;
0334
0335 input_set_drvdata(idev, ts);
0336
0337 input_set_capability(idev, EV_KEY, BTN_TOUCH);
0338 input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
0339 input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
0340 input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
0341
0342 touchscreen_parse_properties(idev, false, &ts->prop);
0343
0344 error = input_register_device(idev);
0345 if (error) {
0346 dev_err(&pdev->dev, "Could not register input device\n");
0347 return error;
0348 }
0349
0350 return 0;
0351 }
0352
0353 static int stmpe_ts_remove(struct platform_device *pdev)
0354 {
0355 struct stmpe_touch *ts = platform_get_drvdata(pdev);
0356
0357 stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
0358
0359 return 0;
0360 }
0361
0362 static struct platform_driver stmpe_ts_driver = {
0363 .driver = {
0364 .name = STMPE_TS_NAME,
0365 },
0366 .probe = stmpe_input_probe,
0367 .remove = stmpe_ts_remove,
0368 };
0369 module_platform_driver(stmpe_ts_driver);
0370
0371 static const struct of_device_id stmpe_ts_ids[] = {
0372 { .compatible = "st,stmpe-ts", },
0373 { },
0374 };
0375 MODULE_DEVICE_TABLE(of, stmpe_ts_ids);
0376
0377 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
0378 MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
0379 MODULE_LICENSE("GPL");