Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ST1232 Touchscreen Controller Driver
0004  *
0005  * Copyright (C) 2010 Renesas Solutions Corp.
0006  *  Tony SIM <chinyeow.sim.xt@renesas.com>
0007  *
0008  * Using code from:
0009  *  - android.git.kernel.org: projects/kernel/common.git: synaptics_i2c_rmi.c
0010  *  Copyright (C) 2007 Google, Inc.
0011  */
0012 
0013 #include <linux/delay.h>
0014 #include <linux/gpio/consumer.h>
0015 #include <linux/i2c.h>
0016 #include <linux/input.h>
0017 #include <linux/input/mt.h>
0018 #include <linux/input/touchscreen.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/module.h>
0021 #include <linux/of.h>
0022 #include <linux/pm_qos.h>
0023 #include <linux/slab.h>
0024 #include <linux/types.h>
0025 
0026 #define ST1232_TS_NAME  "st1232-ts"
0027 #define ST1633_TS_NAME  "st1633-ts"
0028 
0029 #define REG_STATUS      0x01    /* Device Status | Error Code */
0030 
0031 #define STATUS_NORMAL       0x00
0032 #define STATUS_INIT     0x01
0033 #define STATUS_ERROR        0x02
0034 #define STATUS_AUTO_TUNING  0x03
0035 #define STATUS_IDLE     0x04
0036 #define STATUS_POWER_DOWN   0x05
0037 
0038 #define ERROR_NONE      0x00
0039 #define ERROR_INVALID_ADDRESS   0x10
0040 #define ERROR_INVALID_VALUE 0x20
0041 #define ERROR_INVALID_PLATFORM  0x30
0042 
0043 #define REG_XY_RESOLUTION   0x04
0044 #define REG_XY_COORDINATES  0x12
0045 #define ST_TS_MAX_FINGERS   10
0046 
0047 struct st_chip_info {
0048     bool    have_z;
0049     u16 max_area;
0050     u16 max_fingers;
0051 };
0052 
0053 struct st1232_ts_data {
0054     struct i2c_client *client;
0055     struct input_dev *input_dev;
0056     struct touchscreen_properties prop;
0057     struct dev_pm_qos_request low_latency_req;
0058     struct gpio_desc *reset_gpio;
0059     const struct st_chip_info *chip_info;
0060     int read_buf_len;
0061     u8 *read_buf;
0062 };
0063 
0064 static int st1232_ts_read_data(struct st1232_ts_data *ts, u8 reg,
0065                    unsigned int n)
0066 {
0067     struct i2c_client *client = ts->client;
0068     struct i2c_msg msg[] = {
0069         {
0070             .addr   = client->addr,
0071             .len    = sizeof(reg),
0072             .buf    = &reg,
0073         },
0074         {
0075             .addr   = client->addr,
0076             .flags  = I2C_M_RD | I2C_M_DMA_SAFE,
0077             .len    = n,
0078             .buf    = ts->read_buf,
0079         }
0080     };
0081     int ret;
0082 
0083     ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
0084     if (ret != ARRAY_SIZE(msg))
0085         return ret < 0 ? ret : -EIO;
0086 
0087     return 0;
0088 }
0089 
0090 static int st1232_ts_wait_ready(struct st1232_ts_data *ts)
0091 {
0092     unsigned int retries;
0093     int error;
0094 
0095     for (retries = 100; retries; retries--) {
0096         error = st1232_ts_read_data(ts, REG_STATUS, 1);
0097         if (!error) {
0098             switch (ts->read_buf[0]) {
0099             case STATUS_NORMAL | ERROR_NONE:
0100             case STATUS_IDLE | ERROR_NONE:
0101                 return 0;
0102             }
0103         }
0104 
0105         usleep_range(1000, 2000);
0106     }
0107 
0108     return -ENXIO;
0109 }
0110 
0111 static int st1232_ts_read_resolution(struct st1232_ts_data *ts, u16 *max_x,
0112                      u16 *max_y)
0113 {
0114     u8 *buf;
0115     int error;
0116 
0117     /* select resolution register */
0118     error = st1232_ts_read_data(ts, REG_XY_RESOLUTION, 3);
0119     if (error)
0120         return error;
0121 
0122     buf = ts->read_buf;
0123 
0124     *max_x = (((buf[0] & 0x0070) << 4) | buf[1]) - 1;
0125     *max_y = (((buf[0] & 0x0007) << 8) | buf[2]) - 1;
0126 
0127     return 0;
0128 }
0129 
0130 static int st1232_ts_parse_and_report(struct st1232_ts_data *ts)
0131 {
0132     struct input_dev *input = ts->input_dev;
0133     struct input_mt_pos pos[ST_TS_MAX_FINGERS];
0134     u8 z[ST_TS_MAX_FINGERS];
0135     int slots[ST_TS_MAX_FINGERS];
0136     int n_contacts = 0;
0137     int i;
0138 
0139     for (i = 0; i < ts->chip_info->max_fingers; i++) {
0140         u8 *buf = &ts->read_buf[i * 4];
0141 
0142         if (buf[0] & BIT(7)) {
0143             unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
0144             unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
0145 
0146             touchscreen_set_mt_pos(&pos[n_contacts],
0147                            &ts->prop, x, y);
0148 
0149             /* st1232 includes a z-axis / touch strength */
0150             if (ts->chip_info->have_z)
0151                 z[n_contacts] = ts->read_buf[i + 6];
0152 
0153             n_contacts++;
0154         }
0155     }
0156 
0157     input_mt_assign_slots(input, slots, pos, n_contacts, 0);
0158     for (i = 0; i < n_contacts; i++) {
0159         input_mt_slot(input, slots[i]);
0160         input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
0161         input_report_abs(input, ABS_MT_POSITION_X, pos[i].x);
0162         input_report_abs(input, ABS_MT_POSITION_Y, pos[i].y);
0163         if (ts->chip_info->have_z)
0164             input_report_abs(input, ABS_MT_TOUCH_MAJOR, z[i]);
0165     }
0166 
0167     input_mt_sync_frame(input);
0168     input_sync(input);
0169 
0170     return n_contacts;
0171 }
0172 
0173 static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
0174 {
0175     struct st1232_ts_data *ts = dev_id;
0176     int count;
0177     int error;
0178 
0179     error = st1232_ts_read_data(ts, REG_XY_COORDINATES, ts->read_buf_len);
0180     if (error)
0181         goto out;
0182 
0183     count = st1232_ts_parse_and_report(ts);
0184     if (!count) {
0185         if (ts->low_latency_req.dev) {
0186             dev_pm_qos_remove_request(&ts->low_latency_req);
0187             ts->low_latency_req.dev = NULL;
0188         }
0189     } else if (!ts->low_latency_req.dev) {
0190         /* First contact, request 100 us latency. */
0191         dev_pm_qos_add_ancestor_request(&ts->client->dev,
0192                         &ts->low_latency_req,
0193                         DEV_PM_QOS_RESUME_LATENCY, 100);
0194     }
0195 
0196 out:
0197     return IRQ_HANDLED;
0198 }
0199 
0200 static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
0201 {
0202     if (ts->reset_gpio)
0203         gpiod_set_value_cansleep(ts->reset_gpio, !poweron);
0204 }
0205 
0206 static void st1232_ts_power_off(void *data)
0207 {
0208     st1232_ts_power(data, false);
0209 }
0210 
0211 static const struct st_chip_info st1232_chip_info = {
0212     .have_z     = true,
0213     .max_area   = 0xff,
0214     .max_fingers    = 2,
0215 };
0216 
0217 static const struct st_chip_info st1633_chip_info = {
0218     .have_z     = false,
0219     .max_area   = 0x00,
0220     .max_fingers    = 5,
0221 };
0222 
0223 static int st1232_ts_probe(struct i2c_client *client,
0224                const struct i2c_device_id *id)
0225 {
0226     const struct st_chip_info *match;
0227     struct st1232_ts_data *ts;
0228     struct input_dev *input_dev;
0229     u16 max_x, max_y;
0230     int error;
0231 
0232     match = device_get_match_data(&client->dev);
0233     if (!match && id)
0234         match = (const void *)id->driver_data;
0235     if (!match) {
0236         dev_err(&client->dev, "unknown device model\n");
0237         return -ENODEV;
0238     }
0239 
0240     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0241         dev_err(&client->dev, "need I2C_FUNC_I2C\n");
0242         return -EIO;
0243     }
0244 
0245     if (!client->irq) {
0246         dev_err(&client->dev, "no IRQ?\n");
0247         return -EINVAL;
0248     }
0249 
0250     ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
0251     if (!ts)
0252         return -ENOMEM;
0253 
0254     ts->chip_info = match;
0255 
0256     /* allocate a buffer according to the number of registers to read */
0257     ts->read_buf_len = ts->chip_info->max_fingers * 4;
0258     ts->read_buf = devm_kzalloc(&client->dev, ts->read_buf_len, GFP_KERNEL);
0259     if (!ts->read_buf)
0260         return -ENOMEM;
0261 
0262     input_dev = devm_input_allocate_device(&client->dev);
0263     if (!input_dev)
0264         return -ENOMEM;
0265 
0266     ts->client = client;
0267     ts->input_dev = input_dev;
0268 
0269     ts->reset_gpio = devm_gpiod_get_optional(&client->dev, NULL,
0270                          GPIOD_OUT_HIGH);
0271     if (IS_ERR(ts->reset_gpio)) {
0272         error = PTR_ERR(ts->reset_gpio);
0273         dev_err(&client->dev, "Unable to request GPIO pin: %d.\n",
0274             error);
0275         return error;
0276     }
0277 
0278     st1232_ts_power(ts, true);
0279 
0280     error = devm_add_action_or_reset(&client->dev, st1232_ts_power_off, ts);
0281     if (error) {
0282         dev_err(&client->dev,
0283             "Failed to install power off action: %d\n", error);
0284         return error;
0285     }
0286 
0287     input_dev->name = "st1232-touchscreen";
0288     input_dev->id.bustype = BUS_I2C;
0289 
0290     /* Wait until device is ready */
0291     error = st1232_ts_wait_ready(ts);
0292     if (error)
0293         return error;
0294 
0295     /* Read resolution from the chip */
0296     error = st1232_ts_read_resolution(ts, &max_x, &max_y);
0297     if (error) {
0298         dev_err(&client->dev,
0299             "Failed to read resolution: %d\n", error);
0300         return error;
0301     }
0302 
0303     if (ts->chip_info->have_z)
0304         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
0305                      ts->chip_info->max_area, 0, 0);
0306 
0307     input_set_abs_params(input_dev, ABS_MT_POSITION_X,
0308                  0, max_x, 0, 0);
0309     input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
0310                  0, max_y, 0, 0);
0311 
0312     touchscreen_parse_properties(input_dev, true, &ts->prop);
0313 
0314     error = input_mt_init_slots(input_dev, ts->chip_info->max_fingers,
0315                     INPUT_MT_DIRECT | INPUT_MT_TRACK |
0316                     INPUT_MT_DROP_UNUSED);
0317     if (error) {
0318         dev_err(&client->dev, "failed to initialize MT slots\n");
0319         return error;
0320     }
0321 
0322     error = devm_request_threaded_irq(&client->dev, client->irq,
0323                       NULL, st1232_ts_irq_handler,
0324                       IRQF_ONESHOT,
0325                       client->name, ts);
0326     if (error) {
0327         dev_err(&client->dev, "Failed to register interrupt\n");
0328         return error;
0329     }
0330 
0331     error = input_register_device(ts->input_dev);
0332     if (error) {
0333         dev_err(&client->dev, "Unable to register %s input device\n",
0334             input_dev->name);
0335         return error;
0336     }
0337 
0338     i2c_set_clientdata(client, ts);
0339 
0340     return 0;
0341 }
0342 
0343 static int __maybe_unused st1232_ts_suspend(struct device *dev)
0344 {
0345     struct i2c_client *client = to_i2c_client(dev);
0346     struct st1232_ts_data *ts = i2c_get_clientdata(client);
0347 
0348     disable_irq(client->irq);
0349 
0350     if (!device_may_wakeup(&client->dev))
0351         st1232_ts_power(ts, false);
0352 
0353     return 0;
0354 }
0355 
0356 static int __maybe_unused st1232_ts_resume(struct device *dev)
0357 {
0358     struct i2c_client *client = to_i2c_client(dev);
0359     struct st1232_ts_data *ts = i2c_get_clientdata(client);
0360 
0361     if (!device_may_wakeup(&client->dev))
0362         st1232_ts_power(ts, true);
0363 
0364     enable_irq(client->irq);
0365 
0366     return 0;
0367 }
0368 
0369 static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
0370              st1232_ts_suspend, st1232_ts_resume);
0371 
0372 static const struct i2c_device_id st1232_ts_id[] = {
0373     { ST1232_TS_NAME, (unsigned long)&st1232_chip_info },
0374     { ST1633_TS_NAME, (unsigned long)&st1633_chip_info },
0375     { }
0376 };
0377 MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
0378 
0379 static const struct of_device_id st1232_ts_dt_ids[] = {
0380     { .compatible = "sitronix,st1232", .data = &st1232_chip_info },
0381     { .compatible = "sitronix,st1633", .data = &st1633_chip_info },
0382     { }
0383 };
0384 MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
0385 
0386 static struct i2c_driver st1232_ts_driver = {
0387     .probe      = st1232_ts_probe,
0388     .id_table   = st1232_ts_id,
0389     .driver = {
0390         .name   = ST1232_TS_NAME,
0391         .of_match_table = st1232_ts_dt_ids,
0392         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
0393         .pm = &st1232_ts_pm_ops,
0394     },
0395 };
0396 
0397 module_i2c_driver(st1232_ts_driver);
0398 
0399 MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
0400 MODULE_AUTHOR("Martin Kepplinger <martin.kepplinger@ginzinger.com>");
0401 MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
0402 MODULE_LICENSE("GPL v2");