Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Rohm BU21029 touchscreen controller driver
0004  *
0005  * Copyright (C) 2015-2018 Bosch Sicherheitssysteme GmbH
0006  *
0007  * This program is free software; you can redistribute it and/or modify
0008  * it under the terms of the GNU General Public License version 2 as
0009  * published by the Free Software Foundation.
0010  */
0011 
0012 #include <linux/delay.h>
0013 #include <linux/gpio/consumer.h>
0014 #include <linux/i2c.h>
0015 #include <linux/input.h>
0016 #include <linux/input/touchscreen.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/irq.h>
0019 #include <linux/module.h>
0020 #include <linux/regulator/consumer.h>
0021 #include <linux/timer.h>
0022 
0023 /*
0024  * HW_ID1 Register (PAGE=0, ADDR=0x0E, Reset value=0x02, Read only)
0025  * +--------+--------+--------+--------+--------+--------+--------+--------+
0026  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
0027  * +--------+--------+--------+--------+--------+--------+--------+--------+
0028  * |                                 HW_IDH                                |
0029  * +--------+--------+--------+--------+--------+--------+--------+--------+
0030  * HW_ID2 Register (PAGE=0, ADDR=0x0F, Reset value=0x29, Read only)
0031  * +--------+--------+--------+--------+--------+--------+--------+--------+
0032  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
0033  * +--------+--------+--------+--------+--------+--------+--------+--------+
0034  * |                                 HW_IDL                                |
0035  * +--------+--------+--------+--------+--------+--------+--------+--------+
0036  * HW_IDH: high 8bits of IC's ID
0037  * HW_IDL: low  8bits of IC's ID
0038  */
0039 #define BU21029_HWID_REG    (0x0E << 3)
0040 #define SUPPORTED_HWID      0x0229
0041 
0042 /*
0043  * CFR0 Register (PAGE=0, ADDR=0x00, Reset value=0x20)
0044  * +--------+--------+--------+--------+--------+--------+--------+--------+
0045  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
0046  * +--------+--------+--------+--------+--------+--------+--------+--------+
0047  * |   0    |   0    |  CALIB |  INTRM |   0    |   0    |   0    |   0    |
0048  * +--------+--------+--------+--------+--------+--------+--------+--------+
0049  * CALIB: 0 = not to use calibration result (*)
0050  *        1 = use calibration result
0051  * INTRM: 0 = INT output depend on "pen down" (*)
0052  *        1 = INT output always "0"
0053  */
0054 #define BU21029_CFR0_REG    (0x00 << 3)
0055 #define CFR0_VALUE      0x00
0056 
0057 /*
0058  * CFR1 Register (PAGE=0, ADDR=0x01, Reset value=0xA6)
0059  * +--------+--------+--------+--------+--------+--------+--------+--------+
0060  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
0061  * +--------+--------+--------+--------+--------+--------+--------+--------+
0062  * |  MAV   |         AVE[2:0]         |   0    |         SMPL[2:0]        |
0063  * +--------+--------+--------+--------+--------+--------+--------+--------+
0064  * MAV:  0 = median average filter off
0065  *       1 = median average filter on (*)
0066  * AVE:  AVE+1 = number of average samples for MAV,
0067  *               if AVE>SMPL, then AVE=SMPL (=3)
0068  * SMPL: SMPL+1 = number of conversion samples for MAV (=7)
0069  */
0070 #define BU21029_CFR1_REG    (0x01 << 3)
0071 #define CFR1_VALUE      0xA6
0072 
0073 /*
0074  * CFR2 Register (PAGE=0, ADDR=0x02, Reset value=0x04)
0075  * +--------+--------+--------+--------+--------+--------+--------+--------+
0076  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
0077  * +--------+--------+--------+--------+--------+--------+--------+--------+
0078  * |          INTVL_TIME[3:0]          |          TIME_ST_ADC[3:0]         |
0079  * +--------+--------+--------+--------+--------+--------+--------+--------+
0080  * INTVL_TIME: waiting time between completion of conversion
0081  *             and start of next conversion, only usable in
0082  *             autoscan mode (=20.480ms)
0083  * TIME_ST_ADC: waiting time between application of voltage
0084  *              to panel and start of A/D conversion (=100us)
0085  */
0086 #define BU21029_CFR2_REG    (0x02 << 3)
0087 #define CFR2_VALUE      0xC9
0088 
0089 /*
0090  * CFR3 Register (PAGE=0, ADDR=0x0B, Reset value=0x72)
0091  * +--------+--------+--------+--------+--------+--------+--------+--------+
0092  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
0093  * +--------+--------+--------+--------+--------+--------+--------+--------+
0094  * |  RM8   | STRETCH|  PU90K |  DUAL  |           PIDAC_OFS[3:0]          |
0095  * +--------+--------+--------+--------+--------+--------+--------+--------+
0096  * RM8: 0 = coordinate resolution is 12bit (*)
0097  *      1 = coordinate resolution is 8bit
0098  * STRETCH: 0 = SCL_STRETCH function off
0099  *          1 = SCL_STRETCH function on (*)
0100  * PU90K: 0 = internal pull-up resistance for touch detection is ~50kohms (*)
0101  *        1 = internal pull-up resistance for touch detection is ~90kohms
0102  * DUAL: 0 = dual touch detection off (*)
0103  *       1 = dual touch detection on
0104  * PIDAC_OFS: dual touch detection circuit adjustment, it is not necessary
0105  *            to change this from initial value
0106  */
0107 #define BU21029_CFR3_REG    (0x0B << 3)
0108 #define CFR3_VALUE      0x42
0109 
0110 /*
0111  * LDO Register (PAGE=0, ADDR=0x0C, Reset value=0x00)
0112  * +--------+--------+--------+--------+--------+--------+--------+--------+
0113  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
0114  * +--------+--------+--------+--------+--------+--------+--------+--------+
0115  * |   0    |         PVDD[2:0]        |   0    |         AVDD[2:0]        |
0116  * +--------+--------+--------+--------+--------+--------+--------+--------+
0117  * PVDD: output voltage of panel output regulator (=2.000V)
0118  * AVDD: output voltage of analog circuit regulator (=2.000V)
0119  */
0120 #define BU21029_LDO_REG     (0x0C << 3)
0121 #define LDO_VALUE       0x77
0122 
0123 /*
0124  * Serial Interface Command Byte 1 (CID=1)
0125  * +--------+--------+--------+--------+--------+--------+--------+--------+
0126  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
0127  * +--------+--------+--------+--------+--------+--------+--------+--------+
0128  * |   1    |                 CF                |  CMSK  |  PDM   |  STP   |
0129  * +--------+--------+--------+--------+--------+--------+--------+--------+
0130  * CF: conversion function, see table 3 in datasheet p6 (=0000, automatic scan)
0131  * CMSK: 0 = executes convert function (*)
0132  *       1 = reads the convert result
0133  * PDM: 0 = power down after convert function stops (*)
0134  *      1 = keep power on after convert function stops
0135  * STP: 1 = abort current conversion and power down, set to "0" automatically
0136  */
0137 #define BU21029_AUTOSCAN    0x80
0138 
0139 /*
0140  * The timeout value needs to be larger than INTVL_TIME + tConv4 (sample and
0141  * conversion time), where tConv4 is calculated by formula:
0142  * tPON + tDLY1 + (tTIME_ST_ADC + (tADC * tSMPL) * 2 + tDLY2) * 3
0143  * see figure 8 in datasheet p15 for details of each field.
0144  */
0145 #define PEN_UP_TIMEOUT_MS   50
0146 
0147 #define STOP_DELAY_MIN_US   50
0148 #define STOP_DELAY_MAX_US   1000
0149 #define START_DELAY_MS      2
0150 #define BUF_LEN         8
0151 #define SCALE_12BIT     (1 << 12)
0152 #define MAX_12BIT       ((1 << 12) - 1)
0153 #define DRIVER_NAME     "bu21029"
0154 
0155 struct bu21029_ts_data {
0156     struct i2c_client       *client;
0157     struct input_dev        *in_dev;
0158     struct timer_list       timer;
0159     struct regulator        *vdd;
0160     struct gpio_desc        *reset_gpios;
0161     u32             x_plate_ohms;
0162     struct touchscreen_properties   prop;
0163 };
0164 
0165 static void bu21029_touch_report(struct bu21029_ts_data *bu21029, const u8 *buf)
0166 {
0167     u16 x, y, z1, z2;
0168     u32 rz;
0169     s32 max_pressure = input_abs_get_max(bu21029->in_dev, ABS_PRESSURE);
0170 
0171     /*
0172      * compose upper 8 and lower 4 bits into a 12bit value:
0173      * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0174      * |            ByteH              |            ByteL              |
0175      * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0176      * |b07|b06|b05|b04|b03|b02|b01|b00|b07|b06|b05|b04|b03|b02|b01|b00|
0177      * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0178      * |v11|v10|v09|v08|v07|v06|v05|v04|v03|v02|v01|v00| 0 | 0 | 0 | 0 |
0179      * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0180      */
0181     x  = (buf[0] << 4) | (buf[1] >> 4);
0182     y  = (buf[2] << 4) | (buf[3] >> 4);
0183     z1 = (buf[4] << 4) | (buf[5] >> 4);
0184     z2 = (buf[6] << 4) | (buf[7] >> 4);
0185 
0186     if (z1 && z2) {
0187         /*
0188          * calculate Rz (pressure resistance value) by equation:
0189          * Rz = Rx * (x/Q) * ((z2/z1) - 1), where
0190          * Rx is x-plate resistance,
0191          * Q  is the touch screen resolution (8bit = 256, 12bit = 4096)
0192          * x, z1, z2 are the measured positions.
0193          */
0194         rz  = z2 - z1;
0195         rz *= x;
0196         rz *= bu21029->x_plate_ohms;
0197         rz /= z1;
0198         rz  = DIV_ROUND_CLOSEST(rz, SCALE_12BIT);
0199         if (rz <= max_pressure) {
0200             touchscreen_report_pos(bu21029->in_dev, &bu21029->prop,
0201                            x, y, false);
0202             input_report_abs(bu21029->in_dev, ABS_PRESSURE,
0203                      max_pressure - rz);
0204             input_report_key(bu21029->in_dev, BTN_TOUCH, 1);
0205             input_sync(bu21029->in_dev);
0206         }
0207     }
0208 }
0209 
0210 static void bu21029_touch_release(struct timer_list *t)
0211 {
0212     struct bu21029_ts_data *bu21029 = from_timer(bu21029, t, timer);
0213 
0214     input_report_abs(bu21029->in_dev, ABS_PRESSURE, 0);
0215     input_report_key(bu21029->in_dev, BTN_TOUCH, 0);
0216     input_sync(bu21029->in_dev);
0217 }
0218 
0219 static irqreturn_t bu21029_touch_soft_irq(int irq, void *data)
0220 {
0221     struct bu21029_ts_data *bu21029 = data;
0222     u8 buf[BUF_LEN];
0223     int error;
0224 
0225     /*
0226      * Read touch data and deassert interrupt (will assert again after
0227      * INTVL_TIME + tConv4 for continuous touch)
0228      */
0229     error = i2c_smbus_read_i2c_block_data(bu21029->client, BU21029_AUTOSCAN,
0230                           sizeof(buf), buf);
0231     if (error < 0)
0232         goto out;
0233 
0234     bu21029_touch_report(bu21029, buf);
0235 
0236     /* reset timer for pen up detection */
0237     mod_timer(&bu21029->timer,
0238           jiffies + msecs_to_jiffies(PEN_UP_TIMEOUT_MS));
0239 
0240 out:
0241     return IRQ_HANDLED;
0242 }
0243 
0244 static void bu21029_put_chip_in_reset(struct bu21029_ts_data *bu21029)
0245 {
0246     if (bu21029->reset_gpios) {
0247         gpiod_set_value_cansleep(bu21029->reset_gpios, 1);
0248         usleep_range(STOP_DELAY_MIN_US, STOP_DELAY_MAX_US);
0249     }
0250 }
0251 
0252 static int bu21029_start_chip(struct input_dev *dev)
0253 {
0254     struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
0255     struct i2c_client *i2c = bu21029->client;
0256     struct {
0257         u8 reg;
0258         u8 value;
0259     } init_table[] = {
0260         {BU21029_CFR0_REG, CFR0_VALUE},
0261         {BU21029_CFR1_REG, CFR1_VALUE},
0262         {BU21029_CFR2_REG, CFR2_VALUE},
0263         {BU21029_CFR3_REG, CFR3_VALUE},
0264         {BU21029_LDO_REG,  LDO_VALUE}
0265     };
0266     int error, i;
0267     __be16 hwid;
0268 
0269     error = regulator_enable(bu21029->vdd);
0270     if (error) {
0271         dev_err(&i2c->dev, "failed to power up chip: %d", error);
0272         return error;
0273     }
0274 
0275     /* take chip out of reset */
0276     if (bu21029->reset_gpios) {
0277         gpiod_set_value_cansleep(bu21029->reset_gpios, 0);
0278         msleep(START_DELAY_MS);
0279     }
0280 
0281     error = i2c_smbus_read_i2c_block_data(i2c, BU21029_HWID_REG,
0282                           sizeof(hwid), (u8 *)&hwid);
0283     if (error < 0) {
0284         dev_err(&i2c->dev, "failed to read HW ID\n");
0285         goto err_out;
0286     }
0287 
0288     if (be16_to_cpu(hwid) != SUPPORTED_HWID) {
0289         dev_err(&i2c->dev,
0290             "unsupported HW ID 0x%x\n", be16_to_cpu(hwid));
0291         error = -ENODEV;
0292         goto err_out;
0293     }
0294 
0295     for (i = 0; i < ARRAY_SIZE(init_table); ++i) {
0296         error = i2c_smbus_write_byte_data(i2c,
0297                           init_table[i].reg,
0298                           init_table[i].value);
0299         if (error < 0) {
0300             dev_err(&i2c->dev,
0301                 "failed to write %#02x to register %#02x: %d\n",
0302                 init_table[i].value, init_table[i].reg,
0303                 error);
0304             goto err_out;
0305         }
0306     }
0307 
0308     error = i2c_smbus_write_byte(i2c, BU21029_AUTOSCAN);
0309     if (error < 0) {
0310         dev_err(&i2c->dev, "failed to start autoscan\n");
0311         goto err_out;
0312     }
0313 
0314     enable_irq(bu21029->client->irq);
0315     return 0;
0316 
0317 err_out:
0318     bu21029_put_chip_in_reset(bu21029);
0319     regulator_disable(bu21029->vdd);
0320     return error;
0321 }
0322 
0323 static void bu21029_stop_chip(struct input_dev *dev)
0324 {
0325     struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
0326 
0327     disable_irq(bu21029->client->irq);
0328     del_timer_sync(&bu21029->timer);
0329 
0330     bu21029_put_chip_in_reset(bu21029);
0331     regulator_disable(bu21029->vdd);
0332 }
0333 
0334 static int bu21029_probe(struct i2c_client *client,
0335              const struct i2c_device_id *id)
0336 {
0337     struct bu21029_ts_data *bu21029;
0338     struct input_dev *in_dev;
0339     int error;
0340 
0341     if (!i2c_check_functionality(client->adapter,
0342                      I2C_FUNC_SMBUS_WRITE_BYTE |
0343                      I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
0344                      I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
0345         dev_err(&client->dev,
0346             "i2c functionality support is not sufficient\n");
0347         return -EIO;
0348     }
0349 
0350     bu21029 = devm_kzalloc(&client->dev, sizeof(*bu21029), GFP_KERNEL);
0351     if (!bu21029)
0352         return -ENOMEM;
0353 
0354     error = device_property_read_u32(&client->dev, "rohm,x-plate-ohms",
0355                      &bu21029->x_plate_ohms);
0356     if (error) {
0357         dev_err(&client->dev,
0358             "invalid 'x-plate-ohms' supplied: %d\n", error);
0359         return error;
0360     }
0361 
0362     bu21029->vdd = devm_regulator_get(&client->dev, "vdd");
0363     if (IS_ERR(bu21029->vdd)) {
0364         error = PTR_ERR(bu21029->vdd);
0365         if (error != -EPROBE_DEFER)
0366             dev_err(&client->dev,
0367                 "failed to acquire 'vdd' supply: %d\n", error);
0368         return error;
0369     }
0370 
0371     bu21029->reset_gpios = devm_gpiod_get_optional(&client->dev,
0372                                "reset", GPIOD_OUT_HIGH);
0373     if (IS_ERR(bu21029->reset_gpios)) {
0374         error = PTR_ERR(bu21029->reset_gpios);
0375         if (error != -EPROBE_DEFER)
0376             dev_err(&client->dev,
0377                 "failed to acquire 'reset' gpio: %d\n", error);
0378         return error;
0379     }
0380 
0381     in_dev = devm_input_allocate_device(&client->dev);
0382     if (!in_dev) {
0383         dev_err(&client->dev, "unable to allocate input device\n");
0384         return -ENOMEM;
0385     }
0386 
0387     bu21029->client = client;
0388     bu21029->in_dev = in_dev;
0389     timer_setup(&bu21029->timer, bu21029_touch_release, 0);
0390 
0391     in_dev->name        = DRIVER_NAME;
0392     in_dev->id.bustype  = BUS_I2C;
0393     in_dev->open        = bu21029_start_chip;
0394     in_dev->close       = bu21029_stop_chip;
0395 
0396     input_set_capability(in_dev, EV_KEY, BTN_TOUCH);
0397     input_set_abs_params(in_dev, ABS_X, 0, MAX_12BIT, 0, 0);
0398     input_set_abs_params(in_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
0399     input_set_abs_params(in_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
0400     touchscreen_parse_properties(in_dev, false, &bu21029->prop);
0401 
0402     input_set_drvdata(in_dev, bu21029);
0403 
0404     error = devm_request_threaded_irq(&client->dev, client->irq,
0405                       NULL, bu21029_touch_soft_irq,
0406                       IRQF_ONESHOT | IRQF_NO_AUTOEN,
0407                       DRIVER_NAME, bu21029);
0408     if (error) {
0409         dev_err(&client->dev,
0410             "unable to request touch irq: %d\n", error);
0411         return error;
0412     }
0413 
0414     error = input_register_device(in_dev);
0415     if (error) {
0416         dev_err(&client->dev,
0417             "unable to register input device: %d\n", error);
0418         return error;
0419     }
0420 
0421     i2c_set_clientdata(client, bu21029);
0422 
0423     return 0;
0424 }
0425 
0426 static int __maybe_unused bu21029_suspend(struct device *dev)
0427 {
0428     struct i2c_client *i2c = to_i2c_client(dev);
0429     struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
0430 
0431     if (!device_may_wakeup(dev)) {
0432         mutex_lock(&bu21029->in_dev->mutex);
0433         if (input_device_enabled(bu21029->in_dev))
0434             bu21029_stop_chip(bu21029->in_dev);
0435         mutex_unlock(&bu21029->in_dev->mutex);
0436     }
0437 
0438     return 0;
0439 }
0440 
0441 static int __maybe_unused bu21029_resume(struct device *dev)
0442 {
0443     struct i2c_client *i2c = to_i2c_client(dev);
0444     struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
0445 
0446     if (!device_may_wakeup(dev)) {
0447         mutex_lock(&bu21029->in_dev->mutex);
0448         if (input_device_enabled(bu21029->in_dev))
0449             bu21029_start_chip(bu21029->in_dev);
0450         mutex_unlock(&bu21029->in_dev->mutex);
0451     }
0452 
0453     return 0;
0454 }
0455 static SIMPLE_DEV_PM_OPS(bu21029_pm_ops, bu21029_suspend, bu21029_resume);
0456 
0457 static const struct i2c_device_id bu21029_ids[] = {
0458     { DRIVER_NAME, 0 },
0459     { /* sentinel */ }
0460 };
0461 MODULE_DEVICE_TABLE(i2c, bu21029_ids);
0462 
0463 #ifdef CONFIG_OF
0464 static const struct of_device_id bu21029_of_ids[] = {
0465     { .compatible = "rohm,bu21029" },
0466     { /* sentinel */ }
0467 };
0468 MODULE_DEVICE_TABLE(of, bu21029_of_ids);
0469 #endif
0470 
0471 static struct i2c_driver bu21029_driver = {
0472     .driver = {
0473         .name       = DRIVER_NAME,
0474         .of_match_table = of_match_ptr(bu21029_of_ids),
0475         .pm     = &bu21029_pm_ops,
0476     },
0477     .id_table   = bu21029_ids,
0478     .probe      = bu21029_probe,
0479 };
0480 module_i2c_driver(bu21029_driver);
0481 
0482 MODULE_AUTHOR("Zhu Yi <yi.zhu5@cn.bosch.com>");
0483 MODULE_DESCRIPTION("Rohm BU21029 touchscreen controller driver");
0484 MODULE_LICENSE("GPL v2");