Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * cyttsp4_core.c
0004  * Cypress TrueTouch(TM) Standard Product V4 Core driver module.
0005  * For use with Cypress Txx4xx parts.
0006  * Supported parts include:
0007  * TMA4XX
0008  * TMA1036
0009  *
0010  * Copyright (C) 2012 Cypress Semiconductor
0011  *
0012  * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
0013  */
0014 
0015 #include "cyttsp4_core.h"
0016 #include <linux/delay.h>
0017 #include <linux/gpio.h>
0018 #include <linux/input/mt.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/pm_runtime.h>
0021 #include <linux/sched.h>
0022 #include <linux/slab.h>
0023 
0024 /* Timeout in ms. */
0025 #define CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT   500
0026 #define CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT 5000
0027 #define CY_CORE_MODE_CHANGE_TIMEOUT     1000
0028 #define CY_CORE_RESET_AND_WAIT_TIMEOUT      500
0029 #define CY_CORE_WAKEUP_TIMEOUT          500
0030 
0031 #define CY_CORE_STARTUP_RETRY_COUNT     3
0032 
0033 static const char * const cyttsp4_tch_abs_string[] = {
0034     [CY_TCH_X]  = "X",
0035     [CY_TCH_Y]  = "Y",
0036     [CY_TCH_P]  = "P",
0037     [CY_TCH_T]  = "T",
0038     [CY_TCH_E]  = "E",
0039     [CY_TCH_O]  = "O",
0040     [CY_TCH_W]  = "W",
0041     [CY_TCH_MAJ]    = "MAJ",
0042     [CY_TCH_MIN]    = "MIN",
0043     [CY_TCH_OR] = "OR",
0044     [CY_TCH_NUM_ABS] = "INVALID"
0045 };
0046 
0047 static const u8 ldr_exit[] = {
0048     0xFF, 0x01, 0x3B, 0x00, 0x00, 0x4F, 0x6D, 0x17
0049 };
0050 
0051 static const u8 ldr_err_app[] = {
0052     0x01, 0x02, 0x00, 0x00, 0x55, 0xDD, 0x17
0053 };
0054 
0055 static inline size_t merge_bytes(u8 high, u8 low)
0056 {
0057     return (high << 8) + low;
0058 }
0059 
0060 #ifdef VERBOSE_DEBUG
0061 static void cyttsp4_pr_buf(struct device *dev, u8 *pr_buf, u8 *dptr, int size,
0062         const char *data_name)
0063 {
0064     int i, k;
0065     const char fmt[] = "%02X ";
0066     int max;
0067 
0068     if (!size)
0069         return;
0070 
0071     max = (CY_MAX_PRBUF_SIZE - 1) - sizeof(CY_PR_TRUNCATED);
0072 
0073     pr_buf[0] = 0;
0074     for (i = k = 0; i < size && k < max; i++, k += 3)
0075         scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, dptr[i]);
0076 
0077     dev_vdbg(dev, "%s:  %s[0..%d]=%s%s\n", __func__, data_name, size - 1,
0078             pr_buf, size <= max ? "" : CY_PR_TRUNCATED);
0079 }
0080 #else
0081 #define cyttsp4_pr_buf(dev, pr_buf, dptr, size, data_name) do { } while (0)
0082 #endif
0083 
0084 static int cyttsp4_load_status_regs(struct cyttsp4 *cd)
0085 {
0086     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0087     struct device *dev = cd->dev;
0088     int rc;
0089 
0090     rc = cyttsp4_adap_read(cd, CY_REG_BASE, si->si_ofs.mode_size,
0091             si->xy_mode);
0092     if (rc < 0)
0093         dev_err(dev, "%s: fail read mode regs r=%d\n",
0094             __func__, rc);
0095     else
0096         cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_mode,
0097             si->si_ofs.mode_size, "xy_mode");
0098 
0099     return rc;
0100 }
0101 
0102 static int cyttsp4_handshake(struct cyttsp4 *cd, u8 mode)
0103 {
0104     u8 cmd = mode ^ CY_HST_TOGGLE;
0105     int rc;
0106 
0107     /*
0108      * Mode change issued, handshaking now will cause endless mode change
0109      * requests, for sync mode modechange will do same with handshake
0110      * */
0111     if (mode & CY_HST_MODE_CHANGE)
0112         return 0;
0113 
0114     rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
0115     if (rc < 0)
0116         dev_err(cd->dev, "%s: bus write fail on handshake (ret=%d)\n",
0117                 __func__, rc);
0118 
0119     return rc;
0120 }
0121 
0122 static int cyttsp4_hw_soft_reset(struct cyttsp4 *cd)
0123 {
0124     u8 cmd = CY_HST_RESET;
0125     int rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
0126     if (rc < 0) {
0127         dev_err(cd->dev, "%s: FAILED to execute SOFT reset\n",
0128                 __func__);
0129         return rc;
0130     }
0131     return 0;
0132 }
0133 
0134 static int cyttsp4_hw_hard_reset(struct cyttsp4 *cd)
0135 {
0136     if (cd->cpdata->xres) {
0137         cd->cpdata->xres(cd->cpdata, cd->dev);
0138         dev_dbg(cd->dev, "%s: execute HARD reset\n", __func__);
0139         return 0;
0140     }
0141     dev_err(cd->dev, "%s: FAILED to execute HARD reset\n", __func__);
0142     return -ENOSYS;
0143 }
0144 
0145 static int cyttsp4_hw_reset(struct cyttsp4 *cd)
0146 {
0147     int rc = cyttsp4_hw_hard_reset(cd);
0148     if (rc == -ENOSYS)
0149         rc = cyttsp4_hw_soft_reset(cd);
0150     return rc;
0151 }
0152 
0153 /*
0154  * Gets number of bits for a touch filed as parameter,
0155  * sets maximum value for field which is used as bit mask
0156  * and returns number of bytes required for that field
0157  */
0158 static int cyttsp4_bits_2_bytes(unsigned int nbits, size_t *max)
0159 {
0160     *max = 1UL << nbits;
0161     return (nbits + 7) / 8;
0162 }
0163 
0164 static int cyttsp4_si_data_offsets(struct cyttsp4 *cd)
0165 {
0166     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0167     int rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(si->si_data),
0168             &si->si_data);
0169     if (rc < 0) {
0170         dev_err(cd->dev, "%s: fail read sysinfo data offsets r=%d\n",
0171             __func__, rc);
0172         return rc;
0173     }
0174 
0175     /* Print sysinfo data offsets */
0176     cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)&si->si_data,
0177                sizeof(si->si_data), "sysinfo_data_offsets");
0178 
0179     /* convert sysinfo data offset bytes into integers */
0180 
0181     si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
0182             si->si_data.map_szl);
0183     si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
0184             si->si_data.map_szl);
0185     si->si_ofs.cydata_ofs = merge_bytes(si->si_data.cydata_ofsh,
0186             si->si_data.cydata_ofsl);
0187     si->si_ofs.test_ofs = merge_bytes(si->si_data.test_ofsh,
0188             si->si_data.test_ofsl);
0189     si->si_ofs.pcfg_ofs = merge_bytes(si->si_data.pcfg_ofsh,
0190             si->si_data.pcfg_ofsl);
0191     si->si_ofs.opcfg_ofs = merge_bytes(si->si_data.opcfg_ofsh,
0192             si->si_data.opcfg_ofsl);
0193     si->si_ofs.ddata_ofs = merge_bytes(si->si_data.ddata_ofsh,
0194             si->si_data.ddata_ofsl);
0195     si->si_ofs.mdata_ofs = merge_bytes(si->si_data.mdata_ofsh,
0196             si->si_data.mdata_ofsl);
0197     return rc;
0198 }
0199 
0200 static int cyttsp4_si_get_cydata(struct cyttsp4 *cd)
0201 {
0202     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0203     int read_offset;
0204     int mfgid_sz, calc_mfgid_sz;
0205     void *p;
0206     int rc;
0207 
0208     if (si->si_ofs.test_ofs <= si->si_ofs.cydata_ofs) {
0209         dev_err(cd->dev,
0210             "%s: invalid offset test_ofs: %zu, cydata_ofs: %zu\n",
0211             __func__, si->si_ofs.test_ofs, si->si_ofs.cydata_ofs);
0212         return -EINVAL;
0213     }
0214 
0215     si->si_ofs.cydata_size = si->si_ofs.test_ofs - si->si_ofs.cydata_ofs;
0216     dev_dbg(cd->dev, "%s: cydata size: %zd\n", __func__,
0217             si->si_ofs.cydata_size);
0218 
0219     p = krealloc(si->si_ptrs.cydata, si->si_ofs.cydata_size, GFP_KERNEL);
0220     if (p == NULL) {
0221         dev_err(cd->dev, "%s: failed to allocate cydata memory\n",
0222             __func__);
0223         return -ENOMEM;
0224     }
0225     si->si_ptrs.cydata = p;
0226 
0227     read_offset = si->si_ofs.cydata_ofs;
0228 
0229     /* Read the CYDA registers up to MFGID field */
0230     rc = cyttsp4_adap_read(cd, read_offset,
0231             offsetof(struct cyttsp4_cydata, mfgid_sz)
0232                 + sizeof(si->si_ptrs.cydata->mfgid_sz),
0233             si->si_ptrs.cydata);
0234     if (rc < 0) {
0235         dev_err(cd->dev, "%s: fail read cydata r=%d\n",
0236             __func__, rc);
0237         return rc;
0238     }
0239 
0240     /* Check MFGID size */
0241     mfgid_sz = si->si_ptrs.cydata->mfgid_sz;
0242     calc_mfgid_sz = si->si_ofs.cydata_size - sizeof(struct cyttsp4_cydata);
0243     if (mfgid_sz != calc_mfgid_sz) {
0244         dev_err(cd->dev, "%s: mismatch in MFGID size, reported:%d calculated:%d\n",
0245             __func__, mfgid_sz, calc_mfgid_sz);
0246         return -EINVAL;
0247     }
0248 
0249     read_offset += offsetof(struct cyttsp4_cydata, mfgid_sz)
0250             + sizeof(si->si_ptrs.cydata->mfgid_sz);
0251 
0252     /* Read the CYDA registers for MFGID field */
0253     rc = cyttsp4_adap_read(cd, read_offset, si->si_ptrs.cydata->mfgid_sz,
0254             si->si_ptrs.cydata->mfg_id);
0255     if (rc < 0) {
0256         dev_err(cd->dev, "%s: fail read cydata r=%d\n",
0257             __func__, rc);
0258         return rc;
0259     }
0260 
0261     read_offset += si->si_ptrs.cydata->mfgid_sz;
0262 
0263     /* Read the rest of the CYDA registers */
0264     rc = cyttsp4_adap_read(cd, read_offset,
0265             sizeof(struct cyttsp4_cydata)
0266                 - offsetof(struct cyttsp4_cydata, cyito_idh),
0267             &si->si_ptrs.cydata->cyito_idh);
0268     if (rc < 0) {
0269         dev_err(cd->dev, "%s: fail read cydata r=%d\n",
0270             __func__, rc);
0271         return rc;
0272     }
0273 
0274     cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.cydata,
0275         si->si_ofs.cydata_size, "sysinfo_cydata");
0276     return rc;
0277 }
0278 
0279 static int cyttsp4_si_get_test_data(struct cyttsp4 *cd)
0280 {
0281     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0282     void *p;
0283     int rc;
0284 
0285     if (si->si_ofs.pcfg_ofs <= si->si_ofs.test_ofs) {
0286         dev_err(cd->dev,
0287             "%s: invalid offset pcfg_ofs: %zu, test_ofs: %zu\n",
0288             __func__, si->si_ofs.pcfg_ofs, si->si_ofs.test_ofs);
0289         return -EINVAL;
0290     }
0291 
0292     si->si_ofs.test_size = si->si_ofs.pcfg_ofs - si->si_ofs.test_ofs;
0293 
0294     p = krealloc(si->si_ptrs.test, si->si_ofs.test_size, GFP_KERNEL);
0295     if (p == NULL) {
0296         dev_err(cd->dev, "%s: failed to allocate test memory\n",
0297             __func__);
0298         return -ENOMEM;
0299     }
0300     si->si_ptrs.test = p;
0301 
0302     rc = cyttsp4_adap_read(cd, si->si_ofs.test_ofs, si->si_ofs.test_size,
0303             si->si_ptrs.test);
0304     if (rc < 0) {
0305         dev_err(cd->dev, "%s: fail read test data r=%d\n",
0306             __func__, rc);
0307         return rc;
0308     }
0309 
0310     cyttsp4_pr_buf(cd->dev, cd->pr_buf,
0311                (u8 *)si->si_ptrs.test, si->si_ofs.test_size,
0312                "sysinfo_test_data");
0313     if (si->si_ptrs.test->post_codel &
0314         CY_POST_CODEL_WDG_RST)
0315         dev_info(cd->dev, "%s: %s codel=%02X\n",
0316              __func__, "Reset was a WATCHDOG RESET",
0317              si->si_ptrs.test->post_codel);
0318 
0319     if (!(si->si_ptrs.test->post_codel &
0320           CY_POST_CODEL_CFG_DATA_CRC_FAIL))
0321         dev_info(cd->dev, "%s: %s codel=%02X\n", __func__,
0322              "Config Data CRC FAIL",
0323              si->si_ptrs.test->post_codel);
0324 
0325     if (!(si->si_ptrs.test->post_codel &
0326           CY_POST_CODEL_PANEL_TEST_FAIL))
0327         dev_info(cd->dev, "%s: %s codel=%02X\n",
0328              __func__, "PANEL TEST FAIL",
0329              si->si_ptrs.test->post_codel);
0330 
0331     dev_info(cd->dev, "%s: SCANNING is %s codel=%02X\n",
0332          __func__, si->si_ptrs.test->post_codel & 0x08 ?
0333          "ENABLED" : "DISABLED",
0334          si->si_ptrs.test->post_codel);
0335     return rc;
0336 }
0337 
0338 static int cyttsp4_si_get_pcfg_data(struct cyttsp4 *cd)
0339 {
0340     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0341     void *p;
0342     int rc;
0343 
0344     if (si->si_ofs.opcfg_ofs <= si->si_ofs.pcfg_ofs) {
0345         dev_err(cd->dev,
0346             "%s: invalid offset opcfg_ofs: %zu, pcfg_ofs: %zu\n",
0347             __func__, si->si_ofs.opcfg_ofs, si->si_ofs.pcfg_ofs);
0348         return -EINVAL;
0349     }
0350 
0351     si->si_ofs.pcfg_size = si->si_ofs.opcfg_ofs - si->si_ofs.pcfg_ofs;
0352 
0353     p = krealloc(si->si_ptrs.pcfg, si->si_ofs.pcfg_size, GFP_KERNEL);
0354     if (p == NULL) {
0355         dev_err(cd->dev, "%s: failed to allocate pcfg memory\n",
0356             __func__);
0357         return -ENOMEM;
0358     }
0359     si->si_ptrs.pcfg = p;
0360 
0361     rc = cyttsp4_adap_read(cd, si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size,
0362             si->si_ptrs.pcfg);
0363     if (rc < 0) {
0364         dev_err(cd->dev, "%s: fail read pcfg data r=%d\n",
0365             __func__, rc);
0366         return rc;
0367     }
0368 
0369     si->si_ofs.max_x = merge_bytes((si->si_ptrs.pcfg->res_xh
0370             & CY_PCFG_RESOLUTION_X_MASK), si->si_ptrs.pcfg->res_xl);
0371     si->si_ofs.x_origin = !!(si->si_ptrs.pcfg->res_xh
0372             & CY_PCFG_ORIGIN_X_MASK);
0373     si->si_ofs.max_y = merge_bytes((si->si_ptrs.pcfg->res_yh
0374             & CY_PCFG_RESOLUTION_Y_MASK), si->si_ptrs.pcfg->res_yl);
0375     si->si_ofs.y_origin = !!(si->si_ptrs.pcfg->res_yh
0376             & CY_PCFG_ORIGIN_Y_MASK);
0377     si->si_ofs.max_p = merge_bytes(si->si_ptrs.pcfg->max_zh,
0378             si->si_ptrs.pcfg->max_zl);
0379 
0380     cyttsp4_pr_buf(cd->dev, cd->pr_buf,
0381                (u8 *)si->si_ptrs.pcfg,
0382                si->si_ofs.pcfg_size, "sysinfo_pcfg_data");
0383     return rc;
0384 }
0385 
0386 static int cyttsp4_si_get_opcfg_data(struct cyttsp4 *cd)
0387 {
0388     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0389     struct cyttsp4_tch_abs_params *tch;
0390     struct cyttsp4_tch_rec_params *tch_old, *tch_new;
0391     enum cyttsp4_tch_abs abs;
0392     int i;
0393     void *p;
0394     int rc;
0395 
0396     if (si->si_ofs.ddata_ofs <= si->si_ofs.opcfg_ofs) {
0397         dev_err(cd->dev,
0398             "%s: invalid offset ddata_ofs: %zu, opcfg_ofs: %zu\n",
0399             __func__, si->si_ofs.ddata_ofs, si->si_ofs.opcfg_ofs);
0400         return -EINVAL;
0401     }
0402 
0403     si->si_ofs.opcfg_size = si->si_ofs.ddata_ofs - si->si_ofs.opcfg_ofs;
0404 
0405     p = krealloc(si->si_ptrs.opcfg, si->si_ofs.opcfg_size, GFP_KERNEL);
0406     if (p == NULL) {
0407         dev_err(cd->dev, "%s: failed to allocate opcfg memory\n",
0408             __func__);
0409         return -ENOMEM;
0410     }
0411     si->si_ptrs.opcfg = p;
0412 
0413     rc = cyttsp4_adap_read(cd, si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size,
0414             si->si_ptrs.opcfg);
0415     if (rc < 0) {
0416         dev_err(cd->dev, "%s: fail read opcfg data r=%d\n",
0417             __func__, rc);
0418         return rc;
0419     }
0420     si->si_ofs.cmd_ofs = si->si_ptrs.opcfg->cmd_ofs;
0421     si->si_ofs.rep_ofs = si->si_ptrs.opcfg->rep_ofs;
0422     si->si_ofs.rep_sz = (si->si_ptrs.opcfg->rep_szh * 256) +
0423         si->si_ptrs.opcfg->rep_szl;
0424     si->si_ofs.num_btns = si->si_ptrs.opcfg->num_btns;
0425     si->si_ofs.num_btn_regs = (si->si_ofs.num_btns +
0426         CY_NUM_BTN_PER_REG - 1) / CY_NUM_BTN_PER_REG;
0427     si->si_ofs.tt_stat_ofs = si->si_ptrs.opcfg->tt_stat_ofs;
0428     si->si_ofs.obj_cfg0 = si->si_ptrs.opcfg->obj_cfg0;
0429     si->si_ofs.max_tchs = si->si_ptrs.opcfg->max_tchs &
0430         CY_BYTE_OFS_MASK;
0431     si->si_ofs.tch_rec_size = si->si_ptrs.opcfg->tch_rec_size &
0432         CY_BYTE_OFS_MASK;
0433 
0434     /* Get the old touch fields */
0435     for (abs = CY_TCH_X; abs < CY_NUM_TCH_FIELDS; abs++) {
0436         tch = &si->si_ofs.tch_abs[abs];
0437         tch_old = &si->si_ptrs.opcfg->tch_rec_old[abs];
0438 
0439         tch->ofs = tch_old->loc & CY_BYTE_OFS_MASK;
0440         tch->size = cyttsp4_bits_2_bytes(tch_old->size,
0441                          &tch->max);
0442         tch->bofs = (tch_old->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
0443     }
0444 
0445     /* button fields */
0446     si->si_ofs.btn_rec_size = si->si_ptrs.opcfg->btn_rec_size;
0447     si->si_ofs.btn_diff_ofs = si->si_ptrs.opcfg->btn_diff_ofs;
0448     si->si_ofs.btn_diff_size = si->si_ptrs.opcfg->btn_diff_size;
0449 
0450     if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
0451         /* Get the extended touch fields */
0452         for (i = 0; i < CY_NUM_EXT_TCH_FIELDS; abs++, i++) {
0453             tch = &si->si_ofs.tch_abs[abs];
0454             tch_new = &si->si_ptrs.opcfg->tch_rec_new[i];
0455 
0456             tch->ofs = tch_new->loc & CY_BYTE_OFS_MASK;
0457             tch->size = cyttsp4_bits_2_bytes(tch_new->size,
0458                              &tch->max);
0459             tch->bofs = (tch_new->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
0460         }
0461     }
0462 
0463     for (abs = 0; abs < CY_TCH_NUM_ABS; abs++) {
0464         dev_dbg(cd->dev, "%s: tch_rec_%s\n", __func__,
0465             cyttsp4_tch_abs_string[abs]);
0466         dev_dbg(cd->dev, "%s:     ofs =%2zd\n", __func__,
0467             si->si_ofs.tch_abs[abs].ofs);
0468         dev_dbg(cd->dev, "%s:     siz =%2zd\n", __func__,
0469             si->si_ofs.tch_abs[abs].size);
0470         dev_dbg(cd->dev, "%s:     max =%2zd\n", __func__,
0471             si->si_ofs.tch_abs[abs].max);
0472         dev_dbg(cd->dev, "%s:     bofs=%2zd\n", __func__,
0473             si->si_ofs.tch_abs[abs].bofs);
0474     }
0475 
0476     si->si_ofs.mode_size = si->si_ofs.tt_stat_ofs + 1;
0477     si->si_ofs.data_size = si->si_ofs.max_tchs *
0478         si->si_ptrs.opcfg->tch_rec_size;
0479 
0480     cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.opcfg,
0481         si->si_ofs.opcfg_size, "sysinfo_opcfg_data");
0482 
0483     return 0;
0484 }
0485 
0486 static int cyttsp4_si_get_ddata(struct cyttsp4 *cd)
0487 {
0488     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0489     void *p;
0490     int rc;
0491 
0492     si->si_ofs.ddata_size = si->si_ofs.mdata_ofs - si->si_ofs.ddata_ofs;
0493 
0494     p = krealloc(si->si_ptrs.ddata, si->si_ofs.ddata_size, GFP_KERNEL);
0495     if (p == NULL) {
0496         dev_err(cd->dev, "%s: fail alloc ddata memory\n", __func__);
0497         return -ENOMEM;
0498     }
0499     si->si_ptrs.ddata = p;
0500 
0501     rc = cyttsp4_adap_read(cd, si->si_ofs.ddata_ofs, si->si_ofs.ddata_size,
0502             si->si_ptrs.ddata);
0503     if (rc < 0)
0504         dev_err(cd->dev, "%s: fail read ddata data r=%d\n",
0505             __func__, rc);
0506     else
0507         cyttsp4_pr_buf(cd->dev, cd->pr_buf,
0508                    (u8 *)si->si_ptrs.ddata,
0509                    si->si_ofs.ddata_size, "sysinfo_ddata");
0510     return rc;
0511 }
0512 
0513 static int cyttsp4_si_get_mdata(struct cyttsp4 *cd)
0514 {
0515     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0516     void *p;
0517     int rc;
0518 
0519     si->si_ofs.mdata_size = si->si_ofs.map_sz - si->si_ofs.mdata_ofs;
0520 
0521     p = krealloc(si->si_ptrs.mdata, si->si_ofs.mdata_size, GFP_KERNEL);
0522     if (p == NULL) {
0523         dev_err(cd->dev, "%s: fail alloc mdata memory\n", __func__);
0524         return -ENOMEM;
0525     }
0526     si->si_ptrs.mdata = p;
0527 
0528     rc = cyttsp4_adap_read(cd, si->si_ofs.mdata_ofs, si->si_ofs.mdata_size,
0529             si->si_ptrs.mdata);
0530     if (rc < 0)
0531         dev_err(cd->dev, "%s: fail read mdata data r=%d\n",
0532             __func__, rc);
0533     else
0534         cyttsp4_pr_buf(cd->dev, cd->pr_buf,
0535                    (u8 *)si->si_ptrs.mdata,
0536                    si->si_ofs.mdata_size, "sysinfo_mdata");
0537     return rc;
0538 }
0539 
0540 static int cyttsp4_si_get_btn_data(struct cyttsp4 *cd)
0541 {
0542     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0543     int btn;
0544     int num_defined_keys;
0545     u16 *key_table;
0546     void *p;
0547     int rc = 0;
0548 
0549     if (si->si_ofs.num_btns) {
0550         si->si_ofs.btn_keys_size = si->si_ofs.num_btns *
0551             sizeof(struct cyttsp4_btn);
0552 
0553         p = krealloc(si->btn, si->si_ofs.btn_keys_size,
0554                 GFP_KERNEL|__GFP_ZERO);
0555         if (p == NULL) {
0556             dev_err(cd->dev, "%s: %s\n", __func__,
0557                 "fail alloc btn_keys memory");
0558             return -ENOMEM;
0559         }
0560         si->btn = p;
0561 
0562         if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS] == NULL)
0563             num_defined_keys = 0;
0564         else if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS]->data == NULL)
0565             num_defined_keys = 0;
0566         else
0567             num_defined_keys = cd->cpdata->sett
0568                 [CY_IC_GRPNUM_BTN_KEYS]->size;
0569 
0570         for (btn = 0; btn < si->si_ofs.num_btns &&
0571             btn < num_defined_keys; btn++) {
0572             key_table = (u16 *)cd->cpdata->sett
0573                 [CY_IC_GRPNUM_BTN_KEYS]->data;
0574             si->btn[btn].key_code = key_table[btn];
0575             si->btn[btn].state = CY_BTN_RELEASED;
0576             si->btn[btn].enabled = true;
0577         }
0578         for (; btn < si->si_ofs.num_btns; btn++) {
0579             si->btn[btn].key_code = KEY_RESERVED;
0580             si->btn[btn].state = CY_BTN_RELEASED;
0581             si->btn[btn].enabled = true;
0582         }
0583 
0584         return rc;
0585     }
0586 
0587     si->si_ofs.btn_keys_size = 0;
0588     kfree(si->btn);
0589     si->btn = NULL;
0590     return rc;
0591 }
0592 
0593 static int cyttsp4_si_get_op_data_ptrs(struct cyttsp4 *cd)
0594 {
0595     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0596     void *p;
0597 
0598     p = krealloc(si->xy_mode, si->si_ofs.mode_size, GFP_KERNEL|__GFP_ZERO);
0599     if (p == NULL)
0600         return -ENOMEM;
0601     si->xy_mode = p;
0602 
0603     p = krealloc(si->xy_data, si->si_ofs.data_size, GFP_KERNEL|__GFP_ZERO);
0604     if (p == NULL)
0605         return -ENOMEM;
0606     si->xy_data = p;
0607 
0608     p = krealloc(si->btn_rec_data,
0609             si->si_ofs.btn_rec_size * si->si_ofs.num_btns,
0610             GFP_KERNEL|__GFP_ZERO);
0611     if (p == NULL)
0612         return -ENOMEM;
0613     si->btn_rec_data = p;
0614 
0615     return 0;
0616 }
0617 
0618 static void cyttsp4_si_put_log_data(struct cyttsp4 *cd)
0619 {
0620     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0621     dev_dbg(cd->dev, "%s: cydata_ofs =%4zd siz=%4zd\n", __func__,
0622         si->si_ofs.cydata_ofs, si->si_ofs.cydata_size);
0623     dev_dbg(cd->dev, "%s: test_ofs   =%4zd siz=%4zd\n", __func__,
0624         si->si_ofs.test_ofs, si->si_ofs.test_size);
0625     dev_dbg(cd->dev, "%s: pcfg_ofs   =%4zd siz=%4zd\n", __func__,
0626         si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size);
0627     dev_dbg(cd->dev, "%s: opcfg_ofs  =%4zd siz=%4zd\n", __func__,
0628         si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size);
0629     dev_dbg(cd->dev, "%s: ddata_ofs  =%4zd siz=%4zd\n", __func__,
0630         si->si_ofs.ddata_ofs, si->si_ofs.ddata_size);
0631     dev_dbg(cd->dev, "%s: mdata_ofs  =%4zd siz=%4zd\n", __func__,
0632         si->si_ofs.mdata_ofs, si->si_ofs.mdata_size);
0633 
0634     dev_dbg(cd->dev, "%s: cmd_ofs       =%4zd\n", __func__,
0635         si->si_ofs.cmd_ofs);
0636     dev_dbg(cd->dev, "%s: rep_ofs       =%4zd\n", __func__,
0637         si->si_ofs.rep_ofs);
0638     dev_dbg(cd->dev, "%s: rep_sz        =%4zd\n", __func__,
0639         si->si_ofs.rep_sz);
0640     dev_dbg(cd->dev, "%s: num_btns      =%4zd\n", __func__,
0641         si->si_ofs.num_btns);
0642     dev_dbg(cd->dev, "%s: num_btn_regs  =%4zd\n", __func__,
0643         si->si_ofs.num_btn_regs);
0644     dev_dbg(cd->dev, "%s: tt_stat_ofs   =%4zd\n", __func__,
0645         si->si_ofs.tt_stat_ofs);
0646     dev_dbg(cd->dev, "%s: tch_rec_size  =%4zd\n", __func__,
0647         si->si_ofs.tch_rec_size);
0648     dev_dbg(cd->dev, "%s: max_tchs      =%4zd\n", __func__,
0649         si->si_ofs.max_tchs);
0650     dev_dbg(cd->dev, "%s: mode_size     =%4zd\n", __func__,
0651         si->si_ofs.mode_size);
0652     dev_dbg(cd->dev, "%s: data_size     =%4zd\n", __func__,
0653         si->si_ofs.data_size);
0654     dev_dbg(cd->dev, "%s: map_sz        =%4zd\n", __func__,
0655         si->si_ofs.map_sz);
0656 
0657     dev_dbg(cd->dev, "%s: btn_rec_size   =%2zd\n", __func__,
0658         si->si_ofs.btn_rec_size);
0659     dev_dbg(cd->dev, "%s: btn_diff_ofs   =%2zd\n", __func__,
0660         si->si_ofs.btn_diff_ofs);
0661     dev_dbg(cd->dev, "%s: btn_diff_size  =%2zd\n", __func__,
0662         si->si_ofs.btn_diff_size);
0663 
0664     dev_dbg(cd->dev, "%s: max_x    = 0x%04zX (%zd)\n", __func__,
0665         si->si_ofs.max_x, si->si_ofs.max_x);
0666     dev_dbg(cd->dev, "%s: x_origin = %zd (%s)\n", __func__,
0667         si->si_ofs.x_origin,
0668         si->si_ofs.x_origin == CY_NORMAL_ORIGIN ?
0669         "left corner" : "right corner");
0670     dev_dbg(cd->dev, "%s: max_y    = 0x%04zX (%zd)\n", __func__,
0671         si->si_ofs.max_y, si->si_ofs.max_y);
0672     dev_dbg(cd->dev, "%s: y_origin = %zd (%s)\n", __func__,
0673         si->si_ofs.y_origin,
0674         si->si_ofs.y_origin == CY_NORMAL_ORIGIN ?
0675         "upper corner" : "lower corner");
0676     dev_dbg(cd->dev, "%s: max_p    = 0x%04zX (%zd)\n", __func__,
0677         si->si_ofs.max_p, si->si_ofs.max_p);
0678 
0679     dev_dbg(cd->dev, "%s: xy_mode=%p xy_data=%p\n", __func__,
0680         si->xy_mode, si->xy_data);
0681 }
0682 
0683 static int cyttsp4_get_sysinfo_regs(struct cyttsp4 *cd)
0684 {
0685     struct cyttsp4_sysinfo *si = &cd->sysinfo;
0686     int rc;
0687 
0688     rc = cyttsp4_si_data_offsets(cd);
0689     if (rc < 0)
0690         return rc;
0691 
0692     rc = cyttsp4_si_get_cydata(cd);
0693     if (rc < 0)
0694         return rc;
0695 
0696     rc = cyttsp4_si_get_test_data(cd);
0697     if (rc < 0)
0698         return rc;
0699 
0700     rc = cyttsp4_si_get_pcfg_data(cd);
0701     if (rc < 0)
0702         return rc;
0703 
0704     rc = cyttsp4_si_get_opcfg_data(cd);
0705     if (rc < 0)
0706         return rc;
0707 
0708     rc = cyttsp4_si_get_ddata(cd);
0709     if (rc < 0)
0710         return rc;
0711 
0712     rc = cyttsp4_si_get_mdata(cd);
0713     if (rc < 0)
0714         return rc;
0715 
0716     rc = cyttsp4_si_get_btn_data(cd);
0717     if (rc < 0)
0718         return rc;
0719 
0720     rc = cyttsp4_si_get_op_data_ptrs(cd);
0721     if (rc < 0) {
0722         dev_err(cd->dev, "%s: failed to get_op_data\n",
0723             __func__);
0724         return rc;
0725     }
0726 
0727     cyttsp4_si_put_log_data(cd);
0728 
0729     /* provide flow control handshake */
0730     rc = cyttsp4_handshake(cd, si->si_data.hst_mode);
0731     if (rc < 0)
0732         dev_err(cd->dev, "%s: handshake fail on sysinfo reg\n",
0733             __func__);
0734 
0735     si->ready = true;
0736     return rc;
0737 }
0738 
0739 static void cyttsp4_queue_startup_(struct cyttsp4 *cd)
0740 {
0741     if (cd->startup_state == STARTUP_NONE) {
0742         cd->startup_state = STARTUP_QUEUED;
0743         schedule_work(&cd->startup_work);
0744         dev_dbg(cd->dev, "%s: cyttsp4_startup queued\n", __func__);
0745     } else {
0746         dev_dbg(cd->dev, "%s: startup_state = %d\n", __func__,
0747             cd->startup_state);
0748     }
0749 }
0750 
0751 static void cyttsp4_report_slot_liftoff(struct cyttsp4_mt_data *md,
0752         int max_slots)
0753 {
0754     int t;
0755 
0756     if (md->num_prv_tch == 0)
0757         return;
0758 
0759     for (t = 0; t < max_slots; t++) {
0760         input_mt_slot(md->input, t);
0761         input_mt_report_slot_inactive(md->input);
0762     }
0763 }
0764 
0765 static void cyttsp4_lift_all(struct cyttsp4_mt_data *md)
0766 {
0767     if (!md->si)
0768         return;
0769 
0770     if (md->num_prv_tch != 0) {
0771         cyttsp4_report_slot_liftoff(md,
0772                 md->si->si_ofs.tch_abs[CY_TCH_T].max);
0773         input_sync(md->input);
0774         md->num_prv_tch = 0;
0775     }
0776 }
0777 
0778 static void cyttsp4_get_touch_axis(struct cyttsp4_mt_data *md,
0779     int *axis, int size, int max, u8 *xy_data, int bofs)
0780 {
0781     int nbyte;
0782     int next;
0783 
0784     for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
0785         dev_vdbg(&md->input->dev,
0786             "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
0787             " xy_data[%d]=%02X(%d) bofs=%d\n",
0788             __func__, *axis, *axis, size, max, xy_data, next,
0789             xy_data[next], xy_data[next], bofs);
0790         *axis = (*axis * 256) + (xy_data[next] >> bofs);
0791         next++;
0792     }
0793 
0794     *axis &= max - 1;
0795 
0796     dev_vdbg(&md->input->dev,
0797         "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
0798         " xy_data[%d]=%02X(%d)\n",
0799         __func__, *axis, *axis, size, max, xy_data, next,
0800         xy_data[next], xy_data[next]);
0801 }
0802 
0803 static void cyttsp4_get_touch(struct cyttsp4_mt_data *md,
0804     struct cyttsp4_touch *touch, u8 *xy_data)
0805 {
0806     struct device *dev = &md->input->dev;
0807     struct cyttsp4_sysinfo *si = md->si;
0808     enum cyttsp4_tch_abs abs;
0809     bool flipped;
0810 
0811     for (abs = CY_TCH_X; abs < CY_TCH_NUM_ABS; abs++) {
0812         cyttsp4_get_touch_axis(md, &touch->abs[abs],
0813             si->si_ofs.tch_abs[abs].size,
0814             si->si_ofs.tch_abs[abs].max,
0815             xy_data + si->si_ofs.tch_abs[abs].ofs,
0816             si->si_ofs.tch_abs[abs].bofs);
0817         dev_vdbg(dev, "%s: get %s=%04X(%d)\n", __func__,
0818             cyttsp4_tch_abs_string[abs],
0819             touch->abs[abs], touch->abs[abs]);
0820     }
0821 
0822     if (md->pdata->flags & CY_FLAG_FLIP) {
0823         swap(touch->abs[CY_TCH_X], touch->abs[CY_TCH_Y]);
0824         flipped = true;
0825     } else
0826         flipped = false;
0827 
0828     if (md->pdata->flags & CY_FLAG_INV_X) {
0829         if (flipped)
0830             touch->abs[CY_TCH_X] = md->si->si_ofs.max_y -
0831                 touch->abs[CY_TCH_X];
0832         else
0833             touch->abs[CY_TCH_X] = md->si->si_ofs.max_x -
0834                 touch->abs[CY_TCH_X];
0835     }
0836     if (md->pdata->flags & CY_FLAG_INV_Y) {
0837         if (flipped)
0838             touch->abs[CY_TCH_Y] = md->si->si_ofs.max_x -
0839                 touch->abs[CY_TCH_Y];
0840         else
0841             touch->abs[CY_TCH_Y] = md->si->si_ofs.max_y -
0842                 touch->abs[CY_TCH_Y];
0843     }
0844 
0845     dev_vdbg(dev, "%s: flip=%s inv-x=%s inv-y=%s x=%04X(%d) y=%04X(%d)\n",
0846         __func__, flipped ? "true" : "false",
0847         md->pdata->flags & CY_FLAG_INV_X ? "true" : "false",
0848         md->pdata->flags & CY_FLAG_INV_Y ? "true" : "false",
0849         touch->abs[CY_TCH_X], touch->abs[CY_TCH_X],
0850         touch->abs[CY_TCH_Y], touch->abs[CY_TCH_Y]);
0851 }
0852 
0853 static void cyttsp4_final_sync(struct input_dev *input, int max_slots, int *ids)
0854 {
0855     int t;
0856 
0857     for (t = 0; t < max_slots; t++) {
0858         if (ids[t])
0859             continue;
0860         input_mt_slot(input, t);
0861         input_mt_report_slot_inactive(input);
0862     }
0863 
0864     input_sync(input);
0865 }
0866 
0867 static void cyttsp4_get_mt_touches(struct cyttsp4_mt_data *md, int num_cur_tch)
0868 {
0869     struct device *dev = &md->input->dev;
0870     struct cyttsp4_sysinfo *si = md->si;
0871     struct cyttsp4_touch tch;
0872     int sig;
0873     int i, j, t = 0;
0874     int ids[max(CY_TMA1036_MAX_TCH, CY_TMA4XX_MAX_TCH)];
0875 
0876     memset(ids, 0, si->si_ofs.tch_abs[CY_TCH_T].max * sizeof(int));
0877     for (i = 0; i < num_cur_tch; i++) {
0878         cyttsp4_get_touch(md, &tch, si->xy_data +
0879             (i * si->si_ofs.tch_rec_size));
0880         if ((tch.abs[CY_TCH_T] < md->pdata->frmwrk->abs
0881             [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST]) ||
0882             (tch.abs[CY_TCH_T] > md->pdata->frmwrk->abs
0883             [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MAX_OST])) {
0884             dev_err(dev, "%s: tch=%d -> bad trk_id=%d max_id=%d\n",
0885                 __func__, i, tch.abs[CY_TCH_T],
0886                 md->pdata->frmwrk->abs[(CY_ABS_ID_OST *
0887                 CY_NUM_ABS_SET) + CY_MAX_OST]);
0888             continue;
0889         }
0890 
0891         /* use 0 based track id's */
0892         sig = md->pdata->frmwrk->abs
0893             [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + 0];
0894         if (sig != CY_IGNORE_VALUE) {
0895             t = tch.abs[CY_TCH_T] - md->pdata->frmwrk->abs
0896                 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST];
0897             if (tch.abs[CY_TCH_E] == CY_EV_LIFTOFF) {
0898                 dev_dbg(dev, "%s: t=%d e=%d lift-off\n",
0899                     __func__, t, tch.abs[CY_TCH_E]);
0900                 goto cyttsp4_get_mt_touches_pr_tch;
0901             }
0902             input_mt_slot(md->input, t);
0903             input_mt_report_slot_state(md->input, MT_TOOL_FINGER,
0904                     true);
0905             ids[t] = true;
0906         }
0907 
0908         /* all devices: position and pressure fields */
0909         for (j = 0; j <= CY_ABS_W_OST; j++) {
0910             sig = md->pdata->frmwrk->abs[((CY_ABS_X_OST + j) *
0911                 CY_NUM_ABS_SET) + 0];
0912             if (sig != CY_IGNORE_VALUE)
0913                 input_report_abs(md->input, sig,
0914                     tch.abs[CY_TCH_X + j]);
0915         }
0916         if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
0917             /*
0918              * TMA400 size and orientation fields:
0919              * if pressure is non-zero and major touch
0920              * signal is zero, then set major and minor touch
0921              * signals to minimum non-zero value
0922              */
0923             if (tch.abs[CY_TCH_P] > 0 && tch.abs[CY_TCH_MAJ] == 0)
0924                 tch.abs[CY_TCH_MAJ] = tch.abs[CY_TCH_MIN] = 1;
0925 
0926             /* Get the extended touch fields */
0927             for (j = 0; j < CY_NUM_EXT_TCH_FIELDS; j++) {
0928                 sig = md->pdata->frmwrk->abs
0929                     [((CY_ABS_MAJ_OST + j) *
0930                     CY_NUM_ABS_SET) + 0];
0931                 if (sig != CY_IGNORE_VALUE)
0932                     input_report_abs(md->input, sig,
0933                         tch.abs[CY_TCH_MAJ + j]);
0934             }
0935         }
0936 
0937 cyttsp4_get_mt_touches_pr_tch:
0938         if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE)
0939             dev_dbg(dev,
0940                 "%s: t=%d x=%d y=%d z=%d M=%d m=%d o=%d e=%d\n",
0941                 __func__, t,
0942                 tch.abs[CY_TCH_X],
0943                 tch.abs[CY_TCH_Y],
0944                 tch.abs[CY_TCH_P],
0945                 tch.abs[CY_TCH_MAJ],
0946                 tch.abs[CY_TCH_MIN],
0947                 tch.abs[CY_TCH_OR],
0948                 tch.abs[CY_TCH_E]);
0949         else
0950             dev_dbg(dev,
0951                 "%s: t=%d x=%d y=%d z=%d e=%d\n", __func__,
0952                 t,
0953                 tch.abs[CY_TCH_X],
0954                 tch.abs[CY_TCH_Y],
0955                 tch.abs[CY_TCH_P],
0956                 tch.abs[CY_TCH_E]);
0957     }
0958 
0959     cyttsp4_final_sync(md->input, si->si_ofs.tch_abs[CY_TCH_T].max, ids);
0960 
0961     md->num_prv_tch = num_cur_tch;
0962 
0963     return;
0964 }
0965 
0966 /* read xy_data for all current touches */
0967 static int cyttsp4_xy_worker(struct cyttsp4 *cd)
0968 {
0969     struct cyttsp4_mt_data *md = &cd->md;
0970     struct device *dev = &md->input->dev;
0971     struct cyttsp4_sysinfo *si = md->si;
0972     u8 num_cur_tch;
0973     u8 hst_mode;
0974     u8 rep_len;
0975     u8 rep_stat;
0976     u8 tt_stat;
0977     int rc = 0;
0978 
0979     /*
0980      * Get event data from cyttsp4 device.
0981      * The event data includes all data
0982      * for all active touches.
0983      * Event data also includes button data
0984      */
0985     /*
0986      * Use 2 reads:
0987      * 1st read to get mode + button bytes + touch count (core)
0988      * 2nd read (optional) to get touch 1 - touch n data
0989      */
0990     hst_mode = si->xy_mode[CY_REG_BASE];
0991     rep_len = si->xy_mode[si->si_ofs.rep_ofs];
0992     rep_stat = si->xy_mode[si->si_ofs.rep_ofs + 1];
0993     tt_stat = si->xy_mode[si->si_ofs.tt_stat_ofs];
0994     dev_vdbg(dev, "%s: %s%02X %s%d %s%02X %s%02X\n", __func__,
0995         "hst_mode=", hst_mode, "rep_len=", rep_len,
0996         "rep_stat=", rep_stat, "tt_stat=", tt_stat);
0997 
0998     num_cur_tch = GET_NUM_TOUCHES(tt_stat);
0999     dev_vdbg(dev, "%s: num_cur_tch=%d\n", __func__, num_cur_tch);
1000 
1001     if (rep_len == 0 && num_cur_tch > 0) {
1002         dev_err(dev, "%s: report length error rep_len=%d num_tch=%d\n",
1003             __func__, rep_len, num_cur_tch);
1004         goto cyttsp4_xy_worker_exit;
1005     }
1006 
1007     /* read touches */
1008     if (num_cur_tch > 0) {
1009         rc = cyttsp4_adap_read(cd, si->si_ofs.tt_stat_ofs + 1,
1010                 num_cur_tch * si->si_ofs.tch_rec_size,
1011                 si->xy_data);
1012         if (rc < 0) {
1013             dev_err(dev, "%s: read fail on touch regs r=%d\n",
1014                 __func__, rc);
1015             goto cyttsp4_xy_worker_exit;
1016         }
1017     }
1018 
1019     /* print xy data */
1020     cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_data, num_cur_tch *
1021         si->si_ofs.tch_rec_size, "xy_data");
1022 
1023     /* check any error conditions */
1024     if (IS_BAD_PKT(rep_stat)) {
1025         dev_dbg(dev, "%s: Invalid buffer detected\n", __func__);
1026         rc = 0;
1027         goto cyttsp4_xy_worker_exit;
1028     }
1029 
1030     if (IS_LARGE_AREA(tt_stat))
1031         dev_dbg(dev, "%s: Large area detected\n", __func__);
1032 
1033     if (num_cur_tch > si->si_ofs.max_tchs) {
1034         dev_err(dev, "%s: too many tch; set to max tch (n=%d c=%zd)\n",
1035                 __func__, num_cur_tch, si->si_ofs.max_tchs);
1036         num_cur_tch = si->si_ofs.max_tchs;
1037     }
1038 
1039     /* extract xy_data for all currently reported touches */
1040     dev_vdbg(dev, "%s: extract data num_cur_tch=%d\n", __func__,
1041         num_cur_tch);
1042     if (num_cur_tch)
1043         cyttsp4_get_mt_touches(md, num_cur_tch);
1044     else
1045         cyttsp4_lift_all(md);
1046 
1047     rc = 0;
1048 
1049 cyttsp4_xy_worker_exit:
1050     return rc;
1051 }
1052 
1053 static int cyttsp4_mt_attention(struct cyttsp4 *cd)
1054 {
1055     struct device *dev = cd->dev;
1056     struct cyttsp4_mt_data *md = &cd->md;
1057     int rc = 0;
1058 
1059     if (!md->si)
1060         return 0;
1061 
1062     mutex_lock(&md->report_lock);
1063     if (!md->is_suspended) {
1064         /* core handles handshake */
1065         rc = cyttsp4_xy_worker(cd);
1066     } else {
1067         dev_vdbg(dev, "%s: Ignoring report while suspended\n",
1068             __func__);
1069     }
1070     mutex_unlock(&md->report_lock);
1071     if (rc < 0)
1072         dev_err(dev, "%s: xy_worker error r=%d\n", __func__, rc);
1073 
1074     return rc;
1075 }
1076 
1077 static irqreturn_t cyttsp4_irq(int irq, void *handle)
1078 {
1079     struct cyttsp4 *cd = handle;
1080     struct device *dev = cd->dev;
1081     enum cyttsp4_mode cur_mode;
1082     u8 cmd_ofs = cd->sysinfo.si_ofs.cmd_ofs;
1083     u8 mode[3];
1084     int rc;
1085 
1086     /*
1087      * Check whether this IRQ should be ignored (external)
1088      * This should be the very first thing to check since
1089      * ignore_irq may be set for a very short period of time
1090      */
1091     if (atomic_read(&cd->ignore_irq)) {
1092         dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
1093         return IRQ_HANDLED;
1094     }
1095 
1096     dev_dbg(dev, "%s int:0x%x\n", __func__, cd->int_status);
1097 
1098     mutex_lock(&cd->system_lock);
1099 
1100     /* Just to debug */
1101     if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING)
1102         dev_vdbg(dev, "%s: Received IRQ while in sleep\n", __func__);
1103 
1104     rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), mode);
1105     if (rc) {
1106         dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
1107         goto cyttsp4_irq_exit;
1108     }
1109     dev_vdbg(dev, "%s mode[0-2]:0x%X 0x%X 0x%X\n", __func__,
1110             mode[0], mode[1], mode[2]);
1111 
1112     if (IS_BOOTLOADER(mode[0], mode[1])) {
1113         cur_mode = CY_MODE_BOOTLOADER;
1114         dev_vdbg(dev, "%s: bl running\n", __func__);
1115         if (cd->mode == CY_MODE_BOOTLOADER) {
1116             /* Signal bootloader heartbeat heard */
1117             wake_up(&cd->wait_q);
1118             goto cyttsp4_irq_exit;
1119         }
1120 
1121         /* switch to bootloader */
1122         dev_dbg(dev, "%s: restart switch to bl m=%d -> m=%d\n",
1123             __func__, cd->mode, cur_mode);
1124 
1125         /* catch operation->bl glitch */
1126         if (cd->mode != CY_MODE_UNKNOWN) {
1127             /* Incase startup_state do not let startup_() */
1128             cd->mode = CY_MODE_UNKNOWN;
1129             cyttsp4_queue_startup_(cd);
1130             goto cyttsp4_irq_exit;
1131         }
1132 
1133         /*
1134          * do not wake thread on this switch since
1135          * it is possible to get an early heartbeat
1136          * prior to performing the reset
1137          */
1138         cd->mode = cur_mode;
1139 
1140         goto cyttsp4_irq_exit;
1141     }
1142 
1143     switch (mode[0] & CY_HST_MODE) {
1144     case CY_HST_OPERATE:
1145         cur_mode = CY_MODE_OPERATIONAL;
1146         dev_vdbg(dev, "%s: operational\n", __func__);
1147         break;
1148     case CY_HST_CAT:
1149         cur_mode = CY_MODE_CAT;
1150         dev_vdbg(dev, "%s: CaT\n", __func__);
1151         break;
1152     case CY_HST_SYSINFO:
1153         cur_mode = CY_MODE_SYSINFO;
1154         dev_vdbg(dev, "%s: sysinfo\n", __func__);
1155         break;
1156     default:
1157         cur_mode = CY_MODE_UNKNOWN;
1158         dev_err(dev, "%s: unknown HST mode 0x%02X\n", __func__,
1159             mode[0]);
1160         break;
1161     }
1162 
1163     /* Check whether this IRQ should be ignored (internal) */
1164     if (cd->int_status & CY_INT_IGNORE) {
1165         dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
1166         goto cyttsp4_irq_exit;
1167     }
1168 
1169     /* Check for wake up interrupt */
1170     if (cd->int_status & CY_INT_AWAKE) {
1171         cd->int_status &= ~CY_INT_AWAKE;
1172         wake_up(&cd->wait_q);
1173         dev_vdbg(dev, "%s: Received wake up interrupt\n", __func__);
1174         goto cyttsp4_irq_handshake;
1175     }
1176 
1177     /* Expecting mode change interrupt */
1178     if ((cd->int_status & CY_INT_MODE_CHANGE)
1179             && (mode[0] & CY_HST_MODE_CHANGE) == 0) {
1180         cd->int_status &= ~CY_INT_MODE_CHANGE;
1181         dev_dbg(dev, "%s: finish mode switch m=%d -> m=%d\n",
1182                 __func__, cd->mode, cur_mode);
1183         cd->mode = cur_mode;
1184         wake_up(&cd->wait_q);
1185         goto cyttsp4_irq_handshake;
1186     }
1187 
1188     /* compare current core mode to current device mode */
1189     dev_vdbg(dev, "%s: cd->mode=%d cur_mode=%d\n",
1190             __func__, cd->mode, cur_mode);
1191     if ((mode[0] & CY_HST_MODE_CHANGE) == 0 && cd->mode != cur_mode) {
1192         /* Unexpected mode change occurred */
1193         dev_err(dev, "%s %d->%d 0x%x\n", __func__, cd->mode,
1194                 cur_mode, cd->int_status);
1195         dev_dbg(dev, "%s: Unexpected mode change, startup\n",
1196                 __func__);
1197         cyttsp4_queue_startup_(cd);
1198         goto cyttsp4_irq_exit;
1199     }
1200 
1201     /* Expecting command complete interrupt */
1202     dev_vdbg(dev, "%s: command byte:0x%x\n", __func__, mode[cmd_ofs]);
1203     if ((cd->int_status & CY_INT_EXEC_CMD)
1204             && mode[cmd_ofs] & CY_CMD_COMPLETE) {
1205         cd->int_status &= ~CY_INT_EXEC_CMD;
1206         dev_vdbg(dev, "%s: Received command complete interrupt\n",
1207                 __func__);
1208         wake_up(&cd->wait_q);
1209         /*
1210          * It is possible to receive a single interrupt for
1211          * command complete and touch/button status report.
1212          * Continue processing for a possible status report.
1213          */
1214     }
1215 
1216     /* This should be status report, read status regs */
1217     if (cd->mode == CY_MODE_OPERATIONAL) {
1218         dev_vdbg(dev, "%s: Read status registers\n", __func__);
1219         rc = cyttsp4_load_status_regs(cd);
1220         if (rc < 0)
1221             dev_err(dev, "%s: fail read mode regs r=%d\n",
1222                 __func__, rc);
1223     }
1224 
1225     cyttsp4_mt_attention(cd);
1226 
1227 cyttsp4_irq_handshake:
1228     /* handshake the event */
1229     dev_vdbg(dev, "%s: Handshake mode=0x%02X r=%d\n",
1230             __func__, mode[0], rc);
1231     rc = cyttsp4_handshake(cd, mode[0]);
1232     if (rc < 0)
1233         dev_err(dev, "%s: Fail handshake mode=0x%02X r=%d\n",
1234                 __func__, mode[0], rc);
1235 
1236     /*
1237      * a non-zero udelay period is required for using
1238      * IRQF_TRIGGER_LOW in order to delay until the
1239      * device completes isr deassert
1240      */
1241     udelay(cd->cpdata->level_irq_udelay);
1242 
1243 cyttsp4_irq_exit:
1244     mutex_unlock(&cd->system_lock);
1245     return IRQ_HANDLED;
1246 }
1247 
1248 static void cyttsp4_start_wd_timer(struct cyttsp4 *cd)
1249 {
1250     if (!CY_WATCHDOG_TIMEOUT)
1251         return;
1252 
1253     mod_timer(&cd->watchdog_timer, jiffies +
1254             msecs_to_jiffies(CY_WATCHDOG_TIMEOUT));
1255 }
1256 
1257 static void cyttsp4_stop_wd_timer(struct cyttsp4 *cd)
1258 {
1259     if (!CY_WATCHDOG_TIMEOUT)
1260         return;
1261 
1262     /*
1263      * Ensure we wait until the watchdog timer
1264      * running on a different CPU finishes
1265      */
1266     del_timer_sync(&cd->watchdog_timer);
1267     cancel_work_sync(&cd->watchdog_work);
1268     del_timer_sync(&cd->watchdog_timer);
1269 }
1270 
1271 static void cyttsp4_watchdog_timer(struct timer_list *t)
1272 {
1273     struct cyttsp4 *cd = from_timer(cd, t, watchdog_timer);
1274 
1275     dev_vdbg(cd->dev, "%s: Watchdog timer triggered\n", __func__);
1276 
1277     schedule_work(&cd->watchdog_work);
1278 
1279     return;
1280 }
1281 
1282 static int cyttsp4_request_exclusive(struct cyttsp4 *cd, void *ownptr,
1283         int timeout_ms)
1284 {
1285     int t = msecs_to_jiffies(timeout_ms);
1286     bool with_timeout = (timeout_ms != 0);
1287 
1288     mutex_lock(&cd->system_lock);
1289     if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
1290         cd->exclusive_dev = ownptr;
1291         goto exit;
1292     }
1293 
1294     cd->exclusive_waits++;
1295 wait:
1296     mutex_unlock(&cd->system_lock);
1297     if (with_timeout) {
1298         t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
1299         if (IS_TMO(t)) {
1300             dev_err(cd->dev, "%s: tmo waiting exclusive access\n",
1301                 __func__);
1302             mutex_lock(&cd->system_lock);
1303             cd->exclusive_waits--;
1304             mutex_unlock(&cd->system_lock);
1305             return -ETIME;
1306         }
1307     } else {
1308         wait_event(cd->wait_q, !cd->exclusive_dev);
1309     }
1310     mutex_lock(&cd->system_lock);
1311     if (cd->exclusive_dev)
1312         goto wait;
1313     cd->exclusive_dev = ownptr;
1314     cd->exclusive_waits--;
1315 exit:
1316     mutex_unlock(&cd->system_lock);
1317 
1318     return 0;
1319 }
1320 
1321 /*
1322  * returns error if was not owned
1323  */
1324 static int cyttsp4_release_exclusive(struct cyttsp4 *cd, void *ownptr)
1325 {
1326     mutex_lock(&cd->system_lock);
1327     if (cd->exclusive_dev != ownptr) {
1328         mutex_unlock(&cd->system_lock);
1329         return -EINVAL;
1330     }
1331 
1332     dev_vdbg(cd->dev, "%s: exclusive_dev %p freed\n",
1333         __func__, cd->exclusive_dev);
1334     cd->exclusive_dev = NULL;
1335     wake_up(&cd->wait_q);
1336     mutex_unlock(&cd->system_lock);
1337     return 0;
1338 }
1339 
1340 static int cyttsp4_wait_bl_heartbeat(struct cyttsp4 *cd)
1341 {
1342     long t;
1343     int rc = 0;
1344 
1345     /* wait heartbeat */
1346     dev_vdbg(cd->dev, "%s: wait heartbeat...\n", __func__);
1347     t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_BOOTLOADER,
1348             msecs_to_jiffies(CY_CORE_RESET_AND_WAIT_TIMEOUT));
1349     if (IS_TMO(t)) {
1350         dev_err(cd->dev, "%s: tmo waiting bl heartbeat cd->mode=%d\n",
1351             __func__, cd->mode);
1352         rc = -ETIME;
1353     }
1354 
1355     return rc;
1356 }
1357 
1358 static int cyttsp4_wait_sysinfo_mode(struct cyttsp4 *cd)
1359 {
1360     long t;
1361 
1362     dev_vdbg(cd->dev, "%s: wait sysinfo...\n", __func__);
1363 
1364     t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_SYSINFO,
1365             msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1366     if (IS_TMO(t)) {
1367         dev_err(cd->dev, "%s: tmo waiting exit bl cd->mode=%d\n",
1368             __func__, cd->mode);
1369         mutex_lock(&cd->system_lock);
1370         cd->int_status &= ~CY_INT_MODE_CHANGE;
1371         mutex_unlock(&cd->system_lock);
1372         return -ETIME;
1373     }
1374 
1375     return 0;
1376 }
1377 
1378 static int cyttsp4_reset_and_wait(struct cyttsp4 *cd)
1379 {
1380     int rc;
1381 
1382     /* reset hardware */
1383     mutex_lock(&cd->system_lock);
1384     dev_dbg(cd->dev, "%s: reset hw...\n", __func__);
1385     rc = cyttsp4_hw_reset(cd);
1386     cd->mode = CY_MODE_UNKNOWN;
1387     mutex_unlock(&cd->system_lock);
1388     if (rc < 0) {
1389         dev_err(cd->dev, "%s:Fail hw reset r=%d\n", __func__, rc);
1390         return rc;
1391     }
1392 
1393     return cyttsp4_wait_bl_heartbeat(cd);
1394 }
1395 
1396 /*
1397  * returns err if refused or timeout; block until mode change complete
1398  * bit is set (mode change interrupt)
1399  */
1400 static int cyttsp4_set_mode(struct cyttsp4 *cd, int new_mode)
1401 {
1402     u8 new_dev_mode;
1403     u8 mode;
1404     long t;
1405     int rc;
1406 
1407     switch (new_mode) {
1408     case CY_MODE_OPERATIONAL:
1409         new_dev_mode = CY_HST_OPERATE;
1410         break;
1411     case CY_MODE_SYSINFO:
1412         new_dev_mode = CY_HST_SYSINFO;
1413         break;
1414     case CY_MODE_CAT:
1415         new_dev_mode = CY_HST_CAT;
1416         break;
1417     default:
1418         dev_err(cd->dev, "%s: invalid mode: %02X(%d)\n",
1419             __func__, new_mode, new_mode);
1420         return -EINVAL;
1421     }
1422 
1423     /* change mode */
1424     dev_dbg(cd->dev, "%s: %s=%p new_dev_mode=%02X new_mode=%d\n",
1425             __func__, "have exclusive", cd->exclusive_dev,
1426             new_dev_mode, new_mode);
1427 
1428     mutex_lock(&cd->system_lock);
1429     rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1430     if (rc < 0) {
1431         mutex_unlock(&cd->system_lock);
1432         dev_err(cd->dev, "%s: Fail read mode r=%d\n",
1433             __func__, rc);
1434         goto exit;
1435     }
1436 
1437     /* Clear device mode bits and set to new mode */
1438     mode &= ~CY_HST_MODE;
1439     mode |= new_dev_mode | CY_HST_MODE_CHANGE;
1440 
1441     cd->int_status |= CY_INT_MODE_CHANGE;
1442     rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode), &mode);
1443     mutex_unlock(&cd->system_lock);
1444     if (rc < 0) {
1445         dev_err(cd->dev, "%s: Fail write mode change r=%d\n",
1446                 __func__, rc);
1447         goto exit;
1448     }
1449 
1450     /* wait for mode change done interrupt */
1451     t = wait_event_timeout(cd->wait_q,
1452             (cd->int_status & CY_INT_MODE_CHANGE) == 0,
1453             msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1454     dev_dbg(cd->dev, "%s: back from wait t=%ld cd->mode=%d\n",
1455             __func__, t, cd->mode);
1456 
1457     if (IS_TMO(t)) {
1458         dev_err(cd->dev, "%s: %s\n", __func__,
1459                 "tmo waiting mode change");
1460         mutex_lock(&cd->system_lock);
1461         cd->int_status &= ~CY_INT_MODE_CHANGE;
1462         mutex_unlock(&cd->system_lock);
1463         rc = -EINVAL;
1464     }
1465 
1466 exit:
1467     return rc;
1468 }
1469 
1470 static void cyttsp4_watchdog_work(struct work_struct *work)
1471 {
1472     struct cyttsp4 *cd =
1473         container_of(work, struct cyttsp4, watchdog_work);
1474     u8 *mode;
1475     int retval;
1476 
1477     mutex_lock(&cd->system_lock);
1478     retval = cyttsp4_load_status_regs(cd);
1479     if (retval < 0) {
1480         dev_err(cd->dev,
1481             "%s: failed to access device in watchdog timer r=%d\n",
1482             __func__, retval);
1483         cyttsp4_queue_startup_(cd);
1484         goto cyttsp4_timer_watchdog_exit_error;
1485     }
1486     mode = &cd->sysinfo.xy_mode[CY_REG_BASE];
1487     if (IS_BOOTLOADER(mode[0], mode[1])) {
1488         dev_err(cd->dev,
1489             "%s: device found in bootloader mode when operational mode\n",
1490             __func__);
1491         cyttsp4_queue_startup_(cd);
1492         goto cyttsp4_timer_watchdog_exit_error;
1493     }
1494 
1495     cyttsp4_start_wd_timer(cd);
1496 cyttsp4_timer_watchdog_exit_error:
1497     mutex_unlock(&cd->system_lock);
1498     return;
1499 }
1500 
1501 static int cyttsp4_core_sleep_(struct cyttsp4 *cd)
1502 {
1503     enum cyttsp4_sleep_state ss = SS_SLEEP_ON;
1504     enum cyttsp4_int_state int_status = CY_INT_IGNORE;
1505     int rc = 0;
1506     u8 mode[2];
1507 
1508     /* Already in sleep mode? */
1509     mutex_lock(&cd->system_lock);
1510     if (cd->sleep_state == SS_SLEEP_ON) {
1511         mutex_unlock(&cd->system_lock);
1512         return 0;
1513     }
1514     cd->sleep_state = SS_SLEEPING;
1515     mutex_unlock(&cd->system_lock);
1516 
1517     cyttsp4_stop_wd_timer(cd);
1518 
1519     /* Wait until currently running IRQ handler exits and disable IRQ */
1520     disable_irq(cd->irq);
1521 
1522     dev_vdbg(cd->dev, "%s: write DEEP SLEEP...\n", __func__);
1523     mutex_lock(&cd->system_lock);
1524     rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1525     if (rc) {
1526         mutex_unlock(&cd->system_lock);
1527         dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
1528         goto error;
1529     }
1530 
1531     if (IS_BOOTLOADER(mode[0], mode[1])) {
1532         mutex_unlock(&cd->system_lock);
1533         dev_err(cd->dev, "%s: Device in BOOTLOADER mode.\n", __func__);
1534         rc = -EINVAL;
1535         goto error;
1536     }
1537 
1538     mode[0] |= CY_HST_SLEEP;
1539     rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode[0]), &mode[0]);
1540     mutex_unlock(&cd->system_lock);
1541     if (rc) {
1542         dev_err(cd->dev, "%s: Fail write adapter r=%d\n", __func__, rc);
1543         goto error;
1544     }
1545     dev_vdbg(cd->dev, "%s: write DEEP SLEEP succeeded\n", __func__);
1546 
1547     if (cd->cpdata->power) {
1548         dev_dbg(cd->dev, "%s: Power down HW\n", __func__);
1549         rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, &cd->ignore_irq);
1550     } else {
1551         dev_dbg(cd->dev, "%s: No power function\n", __func__);
1552         rc = 0;
1553     }
1554     if (rc < 0) {
1555         dev_err(cd->dev, "%s: HW Power down fails r=%d\n",
1556                 __func__, rc);
1557         goto error;
1558     }
1559 
1560     /* Give time to FW to sleep */
1561     msleep(50);
1562 
1563     goto exit;
1564 
1565 error:
1566     ss = SS_SLEEP_OFF;
1567     int_status = CY_INT_NONE;
1568     cyttsp4_start_wd_timer(cd);
1569 
1570 exit:
1571     mutex_lock(&cd->system_lock);
1572     cd->sleep_state = ss;
1573     cd->int_status |= int_status;
1574     mutex_unlock(&cd->system_lock);
1575     enable_irq(cd->irq);
1576     return rc;
1577 }
1578 
1579 static int cyttsp4_startup_(struct cyttsp4 *cd)
1580 {
1581     int retry = CY_CORE_STARTUP_RETRY_COUNT;
1582     int rc;
1583 
1584     cyttsp4_stop_wd_timer(cd);
1585 
1586 reset:
1587     if (retry != CY_CORE_STARTUP_RETRY_COUNT)
1588         dev_dbg(cd->dev, "%s: Retry %d\n", __func__,
1589             CY_CORE_STARTUP_RETRY_COUNT - retry);
1590 
1591     /* reset hardware and wait for heartbeat */
1592     rc = cyttsp4_reset_and_wait(cd);
1593     if (rc < 0) {
1594         dev_err(cd->dev, "%s: Error on h/w reset r=%d\n", __func__, rc);
1595         if (retry--)
1596             goto reset;
1597         goto exit;
1598     }
1599 
1600     /* exit bl into sysinfo mode */
1601     dev_vdbg(cd->dev, "%s: write exit ldr...\n", __func__);
1602     mutex_lock(&cd->system_lock);
1603     cd->int_status &= ~CY_INT_IGNORE;
1604     cd->int_status |= CY_INT_MODE_CHANGE;
1605 
1606     rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(ldr_exit),
1607             (u8 *)ldr_exit);
1608     mutex_unlock(&cd->system_lock);
1609     if (rc < 0) {
1610         dev_err(cd->dev, "%s: Fail write r=%d\n", __func__, rc);
1611         if (retry--)
1612             goto reset;
1613         goto exit;
1614     }
1615 
1616     rc = cyttsp4_wait_sysinfo_mode(cd);
1617     if (rc < 0) {
1618         u8 buf[sizeof(ldr_err_app)];
1619         int rc1;
1620 
1621         /* Check for invalid/corrupted touch application */
1622         rc1 = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(ldr_err_app),
1623                 buf);
1624         if (rc1) {
1625             dev_err(cd->dev, "%s: Fail read r=%d\n", __func__, rc1);
1626         } else if (!memcmp(buf, ldr_err_app, sizeof(ldr_err_app))) {
1627             dev_err(cd->dev, "%s: Error launching touch application\n",
1628                 __func__);
1629             mutex_lock(&cd->system_lock);
1630             cd->invalid_touch_app = true;
1631             mutex_unlock(&cd->system_lock);
1632             goto exit_no_wd;
1633         }
1634 
1635         if (retry--)
1636             goto reset;
1637         goto exit;
1638     }
1639 
1640     mutex_lock(&cd->system_lock);
1641     cd->invalid_touch_app = false;
1642     mutex_unlock(&cd->system_lock);
1643 
1644     /* read sysinfo data */
1645     dev_vdbg(cd->dev, "%s: get sysinfo regs..\n", __func__);
1646     rc = cyttsp4_get_sysinfo_regs(cd);
1647     if (rc < 0) {
1648         dev_err(cd->dev, "%s: failed to get sysinfo regs rc=%d\n",
1649             __func__, rc);
1650         if (retry--)
1651             goto reset;
1652         goto exit;
1653     }
1654 
1655     rc = cyttsp4_set_mode(cd, CY_MODE_OPERATIONAL);
1656     if (rc < 0) {
1657         dev_err(cd->dev, "%s: failed to set mode to operational rc=%d\n",
1658             __func__, rc);
1659         if (retry--)
1660             goto reset;
1661         goto exit;
1662     }
1663 
1664     cyttsp4_lift_all(&cd->md);
1665 
1666     /* restore to sleep if was suspended */
1667     mutex_lock(&cd->system_lock);
1668     if (cd->sleep_state == SS_SLEEP_ON) {
1669         cd->sleep_state = SS_SLEEP_OFF;
1670         mutex_unlock(&cd->system_lock);
1671         cyttsp4_core_sleep_(cd);
1672         goto exit_no_wd;
1673     }
1674     mutex_unlock(&cd->system_lock);
1675 
1676 exit:
1677     cyttsp4_start_wd_timer(cd);
1678 exit_no_wd:
1679     return rc;
1680 }
1681 
1682 static int cyttsp4_startup(struct cyttsp4 *cd)
1683 {
1684     int rc;
1685 
1686     mutex_lock(&cd->system_lock);
1687     cd->startup_state = STARTUP_RUNNING;
1688     mutex_unlock(&cd->system_lock);
1689 
1690     rc = cyttsp4_request_exclusive(cd, cd->dev,
1691             CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
1692     if (rc < 0) {
1693         dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1694                 __func__, cd->exclusive_dev, cd->dev);
1695         goto exit;
1696     }
1697 
1698     rc = cyttsp4_startup_(cd);
1699 
1700     if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1701         /* Don't return fail code, mode is already changed. */
1702         dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1703     else
1704         dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1705 
1706 exit:
1707     mutex_lock(&cd->system_lock);
1708     cd->startup_state = STARTUP_NONE;
1709     mutex_unlock(&cd->system_lock);
1710 
1711     /* Wake the waiters for end of startup */
1712     wake_up(&cd->wait_q);
1713 
1714     return rc;
1715 }
1716 
1717 static void cyttsp4_startup_work_function(struct work_struct *work)
1718 {
1719     struct cyttsp4 *cd =  container_of(work, struct cyttsp4, startup_work);
1720     int rc;
1721 
1722     rc = cyttsp4_startup(cd);
1723     if (rc < 0)
1724         dev_err(cd->dev, "%s: Fail queued startup r=%d\n",
1725             __func__, rc);
1726 }
1727 
1728 static void cyttsp4_free_si_ptrs(struct cyttsp4 *cd)
1729 {
1730     struct cyttsp4_sysinfo *si = &cd->sysinfo;
1731 
1732     if (!si)
1733         return;
1734 
1735     kfree(si->si_ptrs.cydata);
1736     kfree(si->si_ptrs.test);
1737     kfree(si->si_ptrs.pcfg);
1738     kfree(si->si_ptrs.opcfg);
1739     kfree(si->si_ptrs.ddata);
1740     kfree(si->si_ptrs.mdata);
1741     kfree(si->btn);
1742     kfree(si->xy_mode);
1743     kfree(si->xy_data);
1744     kfree(si->btn_rec_data);
1745 }
1746 
1747 #ifdef CONFIG_PM
1748 static int cyttsp4_core_sleep(struct cyttsp4 *cd)
1749 {
1750     int rc;
1751 
1752     rc = cyttsp4_request_exclusive(cd, cd->dev,
1753             CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT);
1754     if (rc < 0) {
1755         dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1756                 __func__, cd->exclusive_dev, cd->dev);
1757         return 0;
1758     }
1759 
1760     rc = cyttsp4_core_sleep_(cd);
1761 
1762     if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1763         dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1764     else
1765         dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1766 
1767     return rc;
1768 }
1769 
1770 static int cyttsp4_core_wake_(struct cyttsp4 *cd)
1771 {
1772     struct device *dev = cd->dev;
1773     int rc;
1774     u8 mode;
1775     int t;
1776 
1777     /* Already woken? */
1778     mutex_lock(&cd->system_lock);
1779     if (cd->sleep_state == SS_SLEEP_OFF) {
1780         mutex_unlock(&cd->system_lock);
1781         return 0;
1782     }
1783     cd->int_status &= ~CY_INT_IGNORE;
1784     cd->int_status |= CY_INT_AWAKE;
1785     cd->sleep_state = SS_WAKING;
1786 
1787     if (cd->cpdata->power) {
1788         dev_dbg(dev, "%s: Power up HW\n", __func__);
1789         rc = cd->cpdata->power(cd->cpdata, 1, dev, &cd->ignore_irq);
1790     } else {
1791         dev_dbg(dev, "%s: No power function\n", __func__);
1792         rc = -ENOSYS;
1793     }
1794     if (rc < 0) {
1795         dev_err(dev, "%s: HW Power up fails r=%d\n",
1796                 __func__, rc);
1797 
1798         /* Initiate a read transaction to wake up */
1799         cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1800     } else
1801         dev_vdbg(cd->dev, "%s: HW power up succeeds\n",
1802             __func__);
1803     mutex_unlock(&cd->system_lock);
1804 
1805     t = wait_event_timeout(cd->wait_q,
1806             (cd->int_status & CY_INT_AWAKE) == 0,
1807             msecs_to_jiffies(CY_CORE_WAKEUP_TIMEOUT));
1808     if (IS_TMO(t)) {
1809         dev_err(dev, "%s: TMO waiting for wakeup\n", __func__);
1810         mutex_lock(&cd->system_lock);
1811         cd->int_status &= ~CY_INT_AWAKE;
1812         /* Try starting up */
1813         cyttsp4_queue_startup_(cd);
1814         mutex_unlock(&cd->system_lock);
1815     }
1816 
1817     mutex_lock(&cd->system_lock);
1818     cd->sleep_state = SS_SLEEP_OFF;
1819     mutex_unlock(&cd->system_lock);
1820 
1821     cyttsp4_start_wd_timer(cd);
1822 
1823     return 0;
1824 }
1825 
1826 static int cyttsp4_core_wake(struct cyttsp4 *cd)
1827 {
1828     int rc;
1829 
1830     rc = cyttsp4_request_exclusive(cd, cd->dev,
1831             CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
1832     if (rc < 0) {
1833         dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1834                 __func__, cd->exclusive_dev, cd->dev);
1835         return 0;
1836     }
1837 
1838     rc = cyttsp4_core_wake_(cd);
1839 
1840     if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1841         dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1842     else
1843         dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1844 
1845     return rc;
1846 }
1847 
1848 static int cyttsp4_core_suspend(struct device *dev)
1849 {
1850     struct cyttsp4 *cd = dev_get_drvdata(dev);
1851     struct cyttsp4_mt_data *md = &cd->md;
1852     int rc;
1853 
1854     md->is_suspended = true;
1855 
1856     rc = cyttsp4_core_sleep(cd);
1857     if (rc < 0) {
1858         dev_err(dev, "%s: Error on sleep\n", __func__);
1859         return -EAGAIN;
1860     }
1861     return 0;
1862 }
1863 
1864 static int cyttsp4_core_resume(struct device *dev)
1865 {
1866     struct cyttsp4 *cd = dev_get_drvdata(dev);
1867     struct cyttsp4_mt_data *md = &cd->md;
1868     int rc;
1869 
1870     md->is_suspended = false;
1871 
1872     rc = cyttsp4_core_wake(cd);
1873     if (rc < 0) {
1874         dev_err(dev, "%s: Error on wake\n", __func__);
1875         return -EAGAIN;
1876     }
1877 
1878     return 0;
1879 }
1880 #endif
1881 
1882 const struct dev_pm_ops cyttsp4_pm_ops = {
1883     SET_SYSTEM_SLEEP_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume)
1884     SET_RUNTIME_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume, NULL)
1885 };
1886 EXPORT_SYMBOL_GPL(cyttsp4_pm_ops);
1887 
1888 static int cyttsp4_mt_open(struct input_dev *input)
1889 {
1890     pm_runtime_get(input->dev.parent);
1891     return 0;
1892 }
1893 
1894 static void cyttsp4_mt_close(struct input_dev *input)
1895 {
1896     struct cyttsp4_mt_data *md = input_get_drvdata(input);
1897     mutex_lock(&md->report_lock);
1898     if (!md->is_suspended)
1899         pm_runtime_put(input->dev.parent);
1900     mutex_unlock(&md->report_lock);
1901 }
1902 
1903 
1904 static int cyttsp4_setup_input_device(struct cyttsp4 *cd)
1905 {
1906     struct device *dev = cd->dev;
1907     struct cyttsp4_mt_data *md = &cd->md;
1908     int signal = CY_IGNORE_VALUE;
1909     int max_x, max_y, max_p, min, max;
1910     int max_x_tmp, max_y_tmp;
1911     int i;
1912     int rc;
1913 
1914     dev_vdbg(dev, "%s: Initialize event signals\n", __func__);
1915     __set_bit(EV_ABS, md->input->evbit);
1916     __set_bit(EV_REL, md->input->evbit);
1917     __set_bit(EV_KEY, md->input->evbit);
1918 
1919     max_x_tmp = md->si->si_ofs.max_x;
1920     max_y_tmp = md->si->si_ofs.max_y;
1921 
1922     /* get maximum values from the sysinfo data */
1923     if (md->pdata->flags & CY_FLAG_FLIP) {
1924         max_x = max_y_tmp - 1;
1925         max_y = max_x_tmp - 1;
1926     } else {
1927         max_x = max_x_tmp - 1;
1928         max_y = max_y_tmp - 1;
1929     }
1930     max_p = md->si->si_ofs.max_p;
1931 
1932     /* set event signal capabilities */
1933     for (i = 0; i < (md->pdata->frmwrk->size / CY_NUM_ABS_SET); i++) {
1934         signal = md->pdata->frmwrk->abs
1935             [(i * CY_NUM_ABS_SET) + CY_SIGNAL_OST];
1936         if (signal != CY_IGNORE_VALUE) {
1937             __set_bit(signal, md->input->absbit);
1938             min = md->pdata->frmwrk->abs
1939                 [(i * CY_NUM_ABS_SET) + CY_MIN_OST];
1940             max = md->pdata->frmwrk->abs
1941                 [(i * CY_NUM_ABS_SET) + CY_MAX_OST];
1942             if (i == CY_ABS_ID_OST) {
1943                 /* shift track ids down to start at 0 */
1944                 max = max - min;
1945                 min = min - min;
1946             } else if (i == CY_ABS_X_OST)
1947                 max = max_x;
1948             else if (i == CY_ABS_Y_OST)
1949                 max = max_y;
1950             else if (i == CY_ABS_P_OST)
1951                 max = max_p;
1952             input_set_abs_params(md->input, signal, min, max,
1953                 md->pdata->frmwrk->abs
1954                 [(i * CY_NUM_ABS_SET) + CY_FUZZ_OST],
1955                 md->pdata->frmwrk->abs
1956                 [(i * CY_NUM_ABS_SET) + CY_FLAT_OST]);
1957             dev_dbg(dev, "%s: register signal=%02X min=%d max=%d\n",
1958                 __func__, signal, min, max);
1959             if ((i == CY_ABS_ID_OST) &&
1960                 (md->si->si_ofs.tch_rec_size <
1961                 CY_TMA4XX_TCH_REC_SIZE))
1962                 break;
1963         }
1964     }
1965 
1966     input_mt_init_slots(md->input, md->si->si_ofs.tch_abs[CY_TCH_T].max,
1967             INPUT_MT_DIRECT);
1968     rc = input_register_device(md->input);
1969     if (rc < 0)
1970         dev_err(dev, "%s: Error, failed register input device r=%d\n",
1971             __func__, rc);
1972     return rc;
1973 }
1974 
1975 static int cyttsp4_mt_probe(struct cyttsp4 *cd)
1976 {
1977     struct device *dev = cd->dev;
1978     struct cyttsp4_mt_data *md = &cd->md;
1979     struct cyttsp4_mt_platform_data *pdata = cd->pdata->mt_pdata;
1980     int rc = 0;
1981 
1982     mutex_init(&md->report_lock);
1983     md->pdata = pdata;
1984     /* Create the input device and register it. */
1985     dev_vdbg(dev, "%s: Create the input device and register it\n",
1986         __func__);
1987     md->input = input_allocate_device();
1988     if (md->input == NULL) {
1989         dev_err(dev, "%s: Error, failed to allocate input device\n",
1990             __func__);
1991         rc = -ENOSYS;
1992         goto error_alloc_failed;
1993     }
1994 
1995     md->input->name = pdata->inp_dev_name;
1996     scnprintf(md->phys, sizeof(md->phys)-1, "%s", dev_name(dev));
1997     md->input->phys = md->phys;
1998     md->input->id.bustype = cd->bus_ops->bustype;
1999     md->input->dev.parent = dev;
2000     md->input->open = cyttsp4_mt_open;
2001     md->input->close = cyttsp4_mt_close;
2002     input_set_drvdata(md->input, md);
2003 
2004     /* get sysinfo */
2005     md->si = &cd->sysinfo;
2006 
2007     rc = cyttsp4_setup_input_device(cd);
2008     if (rc)
2009         goto error_init_input;
2010 
2011     return 0;
2012 
2013 error_init_input:
2014     input_free_device(md->input);
2015 error_alloc_failed:
2016     dev_err(dev, "%s failed.\n", __func__);
2017     return rc;
2018 }
2019 
2020 struct cyttsp4 *cyttsp4_probe(const struct cyttsp4_bus_ops *ops,
2021         struct device *dev, u16 irq, size_t xfer_buf_size)
2022 {
2023     struct cyttsp4 *cd;
2024     struct cyttsp4_platform_data *pdata = dev_get_platdata(dev);
2025     unsigned long irq_flags;
2026     int rc = 0;
2027 
2028     if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
2029         dev_err(dev, "%s: Missing platform data\n", __func__);
2030         rc = -ENODEV;
2031         goto error_no_pdata;
2032     }
2033 
2034     cd = kzalloc(sizeof(*cd), GFP_KERNEL);
2035     if (!cd) {
2036         dev_err(dev, "%s: Error, kzalloc\n", __func__);
2037         rc = -ENOMEM;
2038         goto error_alloc_data;
2039     }
2040 
2041     cd->xfer_buf = kzalloc(xfer_buf_size, GFP_KERNEL);
2042     if (!cd->xfer_buf) {
2043         dev_err(dev, "%s: Error, kzalloc\n", __func__);
2044         rc = -ENOMEM;
2045         goto error_free_cd;
2046     }
2047 
2048     /* Initialize device info */
2049     cd->dev = dev;
2050     cd->pdata = pdata;
2051     cd->cpdata = pdata->core_pdata;
2052     cd->bus_ops = ops;
2053 
2054     /* Initialize mutexes and spinlocks */
2055     mutex_init(&cd->system_lock);
2056     mutex_init(&cd->adap_lock);
2057 
2058     /* Initialize wait queue */
2059     init_waitqueue_head(&cd->wait_q);
2060 
2061     /* Initialize works */
2062     INIT_WORK(&cd->startup_work, cyttsp4_startup_work_function);
2063     INIT_WORK(&cd->watchdog_work, cyttsp4_watchdog_work);
2064 
2065     /* Initialize IRQ */
2066     cd->irq = gpio_to_irq(cd->cpdata->irq_gpio);
2067     if (cd->irq < 0) {
2068         rc = -EINVAL;
2069         goto error_free_xfer;
2070     }
2071 
2072     dev_set_drvdata(dev, cd);
2073 
2074     /* Call platform init function */
2075     if (cd->cpdata->init) {
2076         dev_dbg(cd->dev, "%s: Init HW\n", __func__);
2077         rc = cd->cpdata->init(cd->cpdata, 1, cd->dev);
2078     } else {
2079         dev_dbg(cd->dev, "%s: No HW INIT function\n", __func__);
2080         rc = 0;
2081     }
2082     if (rc < 0)
2083         dev_err(cd->dev, "%s: HW Init fail r=%d\n", __func__, rc);
2084 
2085     dev_dbg(dev, "%s: initialize threaded irq=%d\n", __func__, cd->irq);
2086     if (cd->cpdata->level_irq_udelay > 0)
2087         /* use level triggered interrupts */
2088         irq_flags = IRQF_TRIGGER_LOW | IRQF_ONESHOT;
2089     else
2090         /* use edge triggered interrupts */
2091         irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
2092 
2093     rc = request_threaded_irq(cd->irq, NULL, cyttsp4_irq, irq_flags,
2094         dev_name(dev), cd);
2095     if (rc < 0) {
2096         dev_err(dev, "%s: Error, could not request irq\n", __func__);
2097         goto error_request_irq;
2098     }
2099 
2100     /* Setup watchdog timer */
2101     timer_setup(&cd->watchdog_timer, cyttsp4_watchdog_timer, 0);
2102 
2103     /*
2104      * call startup directly to ensure that the device
2105      * is tested before leaving the probe
2106      */
2107     rc = cyttsp4_startup(cd);
2108 
2109     /* Do not fail probe if startup fails but the device is detected */
2110     if (rc < 0 && cd->mode == CY_MODE_UNKNOWN) {
2111         dev_err(cd->dev, "%s: Fail initial startup r=%d\n",
2112             __func__, rc);
2113         goto error_startup;
2114     }
2115 
2116     rc = cyttsp4_mt_probe(cd);
2117     if (rc < 0) {
2118         dev_err(dev, "%s: Error, fail mt probe\n", __func__);
2119         goto error_startup;
2120     }
2121 
2122     pm_runtime_enable(dev);
2123 
2124     return cd;
2125 
2126 error_startup:
2127     cancel_work_sync(&cd->startup_work);
2128     cyttsp4_stop_wd_timer(cd);
2129     pm_runtime_disable(dev);
2130     cyttsp4_free_si_ptrs(cd);
2131     free_irq(cd->irq, cd);
2132 error_request_irq:
2133     if (cd->cpdata->init)
2134         cd->cpdata->init(cd->cpdata, 0, dev);
2135 error_free_xfer:
2136     kfree(cd->xfer_buf);
2137 error_free_cd:
2138     kfree(cd);
2139 error_alloc_data:
2140 error_no_pdata:
2141     dev_err(dev, "%s failed.\n", __func__);
2142     return ERR_PTR(rc);
2143 }
2144 EXPORT_SYMBOL_GPL(cyttsp4_probe);
2145 
2146 static void cyttsp4_mt_release(struct cyttsp4_mt_data *md)
2147 {
2148     input_unregister_device(md->input);
2149     input_set_drvdata(md->input, NULL);
2150 }
2151 
2152 int cyttsp4_remove(struct cyttsp4 *cd)
2153 {
2154     struct device *dev = cd->dev;
2155 
2156     cyttsp4_mt_release(&cd->md);
2157 
2158     /*
2159      * Suspend the device before freeing the startup_work and stopping
2160      * the watchdog since sleep function restarts watchdog on failure
2161      */
2162     pm_runtime_suspend(dev);
2163     pm_runtime_disable(dev);
2164 
2165     cancel_work_sync(&cd->startup_work);
2166 
2167     cyttsp4_stop_wd_timer(cd);
2168 
2169     free_irq(cd->irq, cd);
2170     if (cd->cpdata->init)
2171         cd->cpdata->init(cd->cpdata, 0, dev);
2172     cyttsp4_free_si_ptrs(cd);
2173     kfree(cd);
2174     return 0;
2175 }
2176 EXPORT_SYMBOL_GPL(cyttsp4_remove);
2177 
2178 MODULE_LICENSE("GPL");
2179 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen core driver");
2180 MODULE_AUTHOR("Cypress");