Input: iqs7222 - preserve system status register

Some register groups reserve a byte at the end of their continuous
address space. Depending on the variant of silicon, this field may
share the same memory space as the lower byte of the system status
register (0x10).

In these cases, caching the reserved byte and writing it later may
effectively reset the device depending on what happened in between
the read and write operations.

Solve this problem by avoiding any access to this last byte within
offending register groups. This method replaces a workaround which
attempted to write the reserved byte with up-to-date contents, but
left a small window in which updates by the device could have been
clobbered.

Now that the driver does not touch these reserved bytes, the order
in which the device's registers are written no longer matters, and
they can be written in their natural order. The new method is also
much more generic, and can be more easily extended to new variants
of silicon with different register maps.

As part of this change, the register read and write functions must
be gently updated to support byte access instead of word access.

Fixes: 2e70ef525b73 ("Input: iqs7222 - acknowledge reset before writing registers")
Signed-off-by: Jeff LaBundy <jeff@labundy.com>
Link: https://lore.kernel.org/r/Z85Alw+d9EHKXx2e@nixie71
Cc: stable@vger.kernel.org
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Jeff LaBundy and committed by
Dmitry Torokhov
a2add513 d85862cc

+22 -28
+22 -28
drivers/input/misc/iqs7222.c
··· 100 100 101 101 enum iqs7222_reg_grp_id { 102 102 IQS7222_REG_GRP_STAT, 103 - IQS7222_REG_GRP_FILT, 104 103 IQS7222_REG_GRP_CYCLE, 105 104 IQS7222_REG_GRP_GLBL, 106 105 IQS7222_REG_GRP_BTN, 107 106 IQS7222_REG_GRP_CHAN, 107 + IQS7222_REG_GRP_FILT, 108 108 IQS7222_REG_GRP_SLDR, 109 109 IQS7222_REG_GRP_TPAD, 110 110 IQS7222_REG_GRP_GPIO, ··· 286 286 287 287 struct iqs7222_reg_grp_desc { 288 288 u16 base; 289 + u16 val_len; 289 290 int num_row; 290 291 int num_col; 291 292 }; ··· 343 342 }, 344 343 [IQS7222_REG_GRP_FILT] = { 345 344 .base = 0xAC00, 345 + .val_len = 3, 346 346 .num_row = 1, 347 347 .num_col = 2, 348 348 }, ··· 402 400 }, 403 401 [IQS7222_REG_GRP_FILT] = { 404 402 .base = 0xAC00, 403 + .val_len = 3, 405 404 .num_row = 1, 406 405 .num_col = 2, 407 406 }, ··· 457 454 }, 458 455 [IQS7222_REG_GRP_FILT] = { 459 456 .base = 0xC400, 457 + .val_len = 3, 460 458 .num_row = 1, 461 459 .num_col = 2, 462 460 }, ··· 500 496 }, 501 497 [IQS7222_REG_GRP_FILT] = { 502 498 .base = 0xC400, 499 + .val_len = 3, 503 500 .num_row = 1, 504 501 .num_col = 2, 505 502 }, ··· 548 543 }, 549 544 [IQS7222_REG_GRP_FILT] = { 550 545 .base = 0xAA00, 546 + .val_len = 3, 551 547 .num_row = 1, 552 548 .num_col = 2, 553 549 }, ··· 606 600 }, 607 601 [IQS7222_REG_GRP_FILT] = { 608 602 .base = 0xAA00, 603 + .val_len = 3, 609 604 .num_row = 1, 610 605 .num_col = 2, 611 606 }, ··· 663 656 }, 664 657 [IQS7222_REG_GRP_FILT] = { 665 658 .base = 0xAE00, 659 + .val_len = 3, 666 660 .num_row = 1, 667 661 .num_col = 2, 668 662 }, ··· 720 712 }, 721 713 [IQS7222_REG_GRP_FILT] = { 722 714 .base = 0xAE00, 715 + .val_len = 3, 723 716 .num_row = 1, 724 717 .num_col = 2, 725 718 }, ··· 777 768 }, 778 769 [IQS7222_REG_GRP_FILT] = { 779 770 .base = 0xAE00, 771 + .val_len = 3, 780 772 .num_row = 1, 781 773 .num_col = 2, 782 774 }, ··· 1614 1604 } 1615 1605 1616 1606 static int iqs7222_read_burst(struct iqs7222_private *iqs7222, 1617 - u16 reg, void *val, u16 num_val) 1607 + u16 reg, void *val, u16 val_len) 1618 1608 { 1619 1609 u8 reg_buf[sizeof(__be16)]; 1620 1610 int ret, i; ··· 1629 1619 { 1630 1620 .addr = client->addr, 1631 1621 .flags = I2C_M_RD, 1632 - .len = num_val * sizeof(__le16), 1622 + .len = val_len, 1633 1623 .buf = (u8 *)val, 1634 1624 }, 1635 1625 }; ··· 1685 1675 __le16 val_buf; 1686 1676 int error; 1687 1677 1688 - error = iqs7222_read_burst(iqs7222, reg, &val_buf, 1); 1678 + error = iqs7222_read_burst(iqs7222, reg, &val_buf, sizeof(val_buf)); 1689 1679 if (error) 1690 1680 return error; 1691 1681 ··· 1695 1685 } 1696 1686 1697 1687 static int iqs7222_write_burst(struct iqs7222_private *iqs7222, 1698 - u16 reg, const void *val, u16 num_val) 1688 + u16 reg, const void *val, u16 val_len) 1699 1689 { 1700 1690 int reg_len = reg > U8_MAX ? sizeof(reg) : sizeof(u8); 1701 - int val_len = num_val * sizeof(__le16); 1702 1691 int msg_len = reg_len + val_len; 1703 1692 int ret, i; 1704 1693 struct i2c_client *client = iqs7222->client; ··· 1756 1747 { 1757 1748 __le16 val_buf = cpu_to_le16(val); 1758 1749 1759 - return iqs7222_write_burst(iqs7222, reg, &val_buf, 1); 1750 + return iqs7222_write_burst(iqs7222, reg, &val_buf, sizeof(val_buf)); 1760 1751 } 1761 1752 1762 1753 static int iqs7222_ati_trigger(struct iqs7222_private *iqs7222) ··· 1840 1831 1841 1832 /* 1842 1833 * Acknowledge reset before writing any registers in case the device 1843 - * suffers a spurious reset during initialization. Because this step 1844 - * may change the reserved fields of the second filter beta register, 1845 - * its cache must be updated. 1846 - * 1847 - * Writing the second filter beta register, in turn, may clobber the 1848 - * system status register. As such, the filter beta register pair is 1849 - * written first to protect against this hazard. 1834 + * suffers a spurious reset during initialization. 1850 1835 */ 1851 1836 if (dir == WRITE) { 1852 - u16 reg = dev_desc->reg_grps[IQS7222_REG_GRP_FILT].base + 1; 1853 - u16 filt_setup; 1854 - 1855 1837 error = iqs7222_write_word(iqs7222, IQS7222_SYS_SETUP, 1856 1838 iqs7222->sys_setup[0] | 1857 1839 IQS7222_SYS_SETUP_ACK_RESET); 1858 1840 if (error) 1859 1841 return error; 1860 - 1861 - error = iqs7222_read_word(iqs7222, reg, &filt_setup); 1862 - if (error) 1863 - return error; 1864 - 1865 - iqs7222->filt_setup[1] &= GENMASK(7, 0); 1866 - iqs7222->filt_setup[1] |= (filt_setup & ~GENMASK(7, 0)); 1867 1842 } 1868 1843 1869 1844 /* ··· 1876 1883 int num_col = dev_desc->reg_grps[i].num_col; 1877 1884 u16 reg = dev_desc->reg_grps[i].base; 1878 1885 __le16 *val_buf; 1886 + u16 val_len = dev_desc->reg_grps[i].val_len ? : num_col * sizeof(*val_buf); 1879 1887 u16 *val; 1880 1888 1881 1889 if (!num_col) ··· 1894 1900 switch (dir) { 1895 1901 case READ: 1896 1902 error = iqs7222_read_burst(iqs7222, reg, 1897 - val_buf, num_col); 1903 + val_buf, val_len); 1898 1904 for (k = 0; k < num_col; k++) 1899 1905 val[k] = le16_to_cpu(val_buf[k]); 1900 1906 break; ··· 1903 1909 for (k = 0; k < num_col; k++) 1904 1910 val_buf[k] = cpu_to_le16(val[k]); 1905 1911 error = iqs7222_write_burst(iqs7222, reg, 1906 - val_buf, num_col); 1912 + val_buf, val_len); 1907 1913 break; 1908 1914 1909 1915 default: ··· 1956 1962 int error, i; 1957 1963 1958 1964 error = iqs7222_read_burst(iqs7222, IQS7222_PROD_NUM, dev_id, 1959 - ARRAY_SIZE(dev_id)); 1965 + sizeof(dev_id)); 1960 1966 if (error) 1961 1967 return error; 1962 1968 ··· 2909 2915 __le16 status[IQS7222_MAX_COLS_STAT]; 2910 2916 2911 2917 error = iqs7222_read_burst(iqs7222, IQS7222_SYS_STATUS, status, 2912 - num_stat); 2918 + num_stat * sizeof(*status)); 2913 2919 if (error) 2914 2920 return error; 2915 2921