Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for ChipOne icn8318 i2c touchscreen controller
0004  *
0005  * Copyright (c) 2015 Red Hat Inc.
0006  *
0007  * Red Hat authors:
0008  * Hans de Goede <hdegoede@redhat.com>
0009  */
0010 
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/i2c.h>
0014 #include <linux/input.h>
0015 #include <linux/input/mt.h>
0016 #include <linux/input/touchscreen.h>
0017 #include <linux/module.h>
0018 #include <linux/of.h>
0019 
0020 #define ICN8318_REG_POWER       4
0021 #define ICN8318_REG_TOUCHDATA       16
0022 
0023 #define ICN8318_POWER_ACTIVE        0
0024 #define ICN8318_POWER_MONITOR       1
0025 #define ICN8318_POWER_HIBERNATE     2
0026 
0027 #define ICN8318_MAX_TOUCHES     5
0028 
0029 struct icn8318_touch {
0030     __u8 slot;
0031     __be16 x;
0032     __be16 y;
0033     __u8 pressure;  /* Seems more like finger width then pressure really */
0034     __u8 event;
0035 /* The difference between 2 and 3 is unclear */
0036 #define ICN8318_EVENT_NO_DATA   1 /* No finger seen yet since wakeup */
0037 #define ICN8318_EVENT_UPDATE1   2 /* New or updated coordinates */
0038 #define ICN8318_EVENT_UPDATE2   3 /* New or updated coordinates */
0039 #define ICN8318_EVENT_END   4 /* Finger lifted */
0040 } __packed;
0041 
0042 struct icn8318_touch_data {
0043     __u8 softbutton;
0044     __u8 touch_count;
0045     struct icn8318_touch touches[ICN8318_MAX_TOUCHES];
0046 } __packed;
0047 
0048 struct icn8318_data {
0049     struct i2c_client *client;
0050     struct input_dev *input;
0051     struct gpio_desc *wake_gpio;
0052     struct touchscreen_properties prop;
0053 };
0054 
0055 static int icn8318_read_touch_data(struct i2c_client *client,
0056                    struct icn8318_touch_data *touch_data)
0057 {
0058     u8 reg = ICN8318_REG_TOUCHDATA;
0059     struct i2c_msg msg[2] = {
0060         {
0061             .addr = client->addr,
0062             .len = 1,
0063             .buf = &reg
0064         },
0065         {
0066             .addr = client->addr,
0067             .flags = I2C_M_RD,
0068             .len = sizeof(struct icn8318_touch_data),
0069             .buf = (u8 *)touch_data
0070         }
0071     };
0072 
0073     return i2c_transfer(client->adapter, msg, 2);
0074 }
0075 
0076 static inline bool icn8318_touch_active(u8 event)
0077 {
0078     return (event == ICN8318_EVENT_UPDATE1) ||
0079            (event == ICN8318_EVENT_UPDATE2);
0080 }
0081 
0082 static irqreturn_t icn8318_irq(int irq, void *dev_id)
0083 {
0084     struct icn8318_data *data = dev_id;
0085     struct device *dev = &data->client->dev;
0086     struct icn8318_touch_data touch_data;
0087     int i, ret;
0088 
0089     ret = icn8318_read_touch_data(data->client, &touch_data);
0090     if (ret < 0) {
0091         dev_err(dev, "Error reading touch data: %d\n", ret);
0092         return IRQ_HANDLED;
0093     }
0094 
0095     if (touch_data.softbutton) {
0096         /*
0097          * Other data is invalid when a softbutton is pressed.
0098          * This needs some extra devicetree bindings to map the icn8318
0099          * softbutton codes to evdev codes. Currently no known devices
0100          * use this.
0101          */
0102         return IRQ_HANDLED;
0103     }
0104 
0105     if (touch_data.touch_count > ICN8318_MAX_TOUCHES) {
0106         dev_warn(dev, "Too much touches %d > %d\n",
0107              touch_data.touch_count, ICN8318_MAX_TOUCHES);
0108         touch_data.touch_count = ICN8318_MAX_TOUCHES;
0109     }
0110 
0111     for (i = 0; i < touch_data.touch_count; i++) {
0112         struct icn8318_touch *touch = &touch_data.touches[i];
0113         bool act = icn8318_touch_active(touch->event);
0114 
0115         input_mt_slot(data->input, touch->slot);
0116         input_mt_report_slot_state(data->input, MT_TOOL_FINGER, act);
0117         if (!act)
0118             continue;
0119 
0120         touchscreen_report_pos(data->input, &data->prop,
0121                        be16_to_cpu(touch->x),
0122                        be16_to_cpu(touch->y), true);
0123     }
0124 
0125     input_mt_sync_frame(data->input);
0126     input_sync(data->input);
0127 
0128     return IRQ_HANDLED;
0129 }
0130 
0131 static int icn8318_start(struct input_dev *dev)
0132 {
0133     struct icn8318_data *data = input_get_drvdata(dev);
0134 
0135     enable_irq(data->client->irq);
0136     gpiod_set_value_cansleep(data->wake_gpio, 1);
0137 
0138     return 0;
0139 }
0140 
0141 static void icn8318_stop(struct input_dev *dev)
0142 {
0143     struct icn8318_data *data = input_get_drvdata(dev);
0144 
0145     disable_irq(data->client->irq);
0146     i2c_smbus_write_byte_data(data->client, ICN8318_REG_POWER,
0147                   ICN8318_POWER_HIBERNATE);
0148     gpiod_set_value_cansleep(data->wake_gpio, 0);
0149 }
0150 
0151 #ifdef CONFIG_PM_SLEEP
0152 static int icn8318_suspend(struct device *dev)
0153 {
0154     struct icn8318_data *data = i2c_get_clientdata(to_i2c_client(dev));
0155 
0156     mutex_lock(&data->input->mutex);
0157     if (input_device_enabled(data->input))
0158         icn8318_stop(data->input);
0159     mutex_unlock(&data->input->mutex);
0160 
0161     return 0;
0162 }
0163 
0164 static int icn8318_resume(struct device *dev)
0165 {
0166     struct icn8318_data *data = i2c_get_clientdata(to_i2c_client(dev));
0167 
0168     mutex_lock(&data->input->mutex);
0169     if (input_device_enabled(data->input))
0170         icn8318_start(data->input);
0171     mutex_unlock(&data->input->mutex);
0172 
0173     return 0;
0174 }
0175 #endif
0176 
0177 static SIMPLE_DEV_PM_OPS(icn8318_pm_ops, icn8318_suspend, icn8318_resume);
0178 
0179 static int icn8318_probe(struct i2c_client *client,
0180              const struct i2c_device_id *id)
0181 {
0182     struct device *dev = &client->dev;
0183     struct icn8318_data *data;
0184     struct input_dev *input;
0185     int error;
0186 
0187     if (!client->irq) {
0188         dev_err(dev, "Error no irq specified\n");
0189         return -EINVAL;
0190     }
0191 
0192     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0193     if (!data)
0194         return -ENOMEM;
0195 
0196     data->wake_gpio = devm_gpiod_get(dev, "wake", GPIOD_OUT_LOW);
0197     if (IS_ERR(data->wake_gpio)) {
0198         error = PTR_ERR(data->wake_gpio);
0199         if (error != -EPROBE_DEFER)
0200             dev_err(dev, "Error getting wake gpio: %d\n", error);
0201         return error;
0202     }
0203 
0204     input = devm_input_allocate_device(dev);
0205     if (!input)
0206         return -ENOMEM;
0207 
0208     input->name = client->name;
0209     input->id.bustype = BUS_I2C;
0210     input->open = icn8318_start;
0211     input->close = icn8318_stop;
0212     input->dev.parent = dev;
0213 
0214     input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
0215     input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
0216 
0217     touchscreen_parse_properties(input, true, &data->prop);
0218     if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
0219         !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
0220         dev_err(dev, "Error touchscreen-size-x and/or -y missing\n");
0221         return -EINVAL;
0222     }
0223 
0224     error = input_mt_init_slots(input, ICN8318_MAX_TOUCHES,
0225                     INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
0226     if (error)
0227         return error;
0228 
0229     data->client = client;
0230     data->input = input;
0231     input_set_drvdata(input, data);
0232 
0233     error = devm_request_threaded_irq(dev, client->irq, NULL, icn8318_irq,
0234                       IRQF_ONESHOT, client->name, data);
0235     if (error) {
0236         dev_err(dev, "Error requesting irq: %d\n", error);
0237         return error;
0238     }
0239 
0240     /* Stop device till opened */
0241     icn8318_stop(data->input);
0242 
0243     error = input_register_device(input);
0244     if (error)
0245         return error;
0246 
0247     i2c_set_clientdata(client, data);
0248 
0249     return 0;
0250 }
0251 
0252 static const struct of_device_id icn8318_of_match[] = {
0253     { .compatible = "chipone,icn8318" },
0254     { }
0255 };
0256 MODULE_DEVICE_TABLE(of, icn8318_of_match);
0257 
0258 /* This is useless for OF-enabled devices, but it is needed by I2C subsystem */
0259 static const struct i2c_device_id icn8318_i2c_id[] = {
0260     { },
0261 };
0262 MODULE_DEVICE_TABLE(i2c, icn8318_i2c_id);
0263 
0264 static struct i2c_driver icn8318_driver = {
0265     .driver = {
0266         .name   = "chipone_icn8318",
0267         .pm = &icn8318_pm_ops,
0268         .of_match_table = icn8318_of_match,
0269     },
0270     .probe = icn8318_probe,
0271     .id_table = icn8318_i2c_id,
0272 };
0273 
0274 module_i2c_driver(icn8318_driver);
0275 
0276 MODULE_DESCRIPTION("ChipOne icn8318 I2C Touchscreen Driver");
0277 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0278 MODULE_LICENSE("GPL");