Re: [PATCH 2/2] Input: add FocalTech FT3683G touchscreen driver
From: Alexander Koskovich
Date: Thu Nov 05 2026 - 20:50:13 EST
On Tuesday, November 3rd, 2026 at 3:02 PM, Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx> wrote:
> Hi Alexander,
>
> On Fri, Oct 30, 2026 at 05:27:15AM +0000, Alexander Koskovich wrote:
> > +
> > +struct ft3683g_data {
> > + struct spi_device *spi;
> > + struct input_dev *input;
> > + struct touchscreen_properties props;
> > + struct regulator_bulk_data supplies[2];
> > + struct gpio_desc *reset_gpio;
> > + struct device *dev;
> > + struct mutex spi_lock; /* protects tx_buf/rx_buf */
>
> In what cases will there be concurrent accss?
None (yet), carried over from downstream for firmware updates, etc. Will drop for v2.
>
> > + u8 tx_buf[256] ____cacheline_aligned;
> > + u8 rx_buf[256] ____cacheline_aligned;
> > +};
> > +
> > +static int ft3683g_spi_read(struct ft3683g_data *ts, u8 addr,
> > + u8 *data, u32 len)
> > +{
> > + struct spi_transfer xfer = { };
> > + u32 dp = 4 + FT3683G_SPI_DUMMY;
> > + u32 txlen = dp + len + 2;
> > + u16 crc_calc, crc_read;
> > + int error, i;
> > +
> > + guard(mutex)(&ts->spi_lock);
> > +
> > + memset(ts->tx_buf, 0, txlen);
> > + ts->tx_buf[0] = addr;
> > + ts->tx_buf[1] = FT3683G_SPI_CMD_READ;
> > + ts->tx_buf[2] = (len >> 8) & 0xff;
> > + ts->tx_buf[3] = len & 0xff;
>
> cpu_to_be16p()? or put_unaligned_be16()?
>
> > +
> > + xfer.tx_buf = ts->tx_buf;
> > + xfer.rx_buf = ts->rx_buf;
> > + xfer.len = txlen;
> > +
> > + for (i = 0; i < FT3683G_SPI_RETRIES; i++) {
> > + error = spi_sync_transfer(ts->spi, &xfer, 1);
> > + if (error)
> > + continue;
> > +
> > + if (ts->rx_buf[3] & 0xa0) {
>
> What is 0xa0? GENMASK()? Something els?
Not sure, downstream doesn't say. I tried to find a version of the driver that
had this named but they all just check these magic bits. Since I can't say for
sure what it is, didn't define it.
I guess I could put that in a comment but "idk" as a comment might be ehhhh.
I added some logging to see if it ever even happens, did some reloads,
suspend/resume cycles, various touches and only fired once, and the CRC also
failed too so can probably rely on the CRC? Right now it seems redundant
and I can't explain what its checking.
Can just drop in v2 unless you'd rather keep it.
>
> > + fsleep(FT3683G_SPI_CS_DELAY_US);
> > + continue;
> > + }
> > +
> > + crc_calc = crc_ccitt(0xffff, &ts->rx_buf[dp], len);
> > + crc_read = ts->rx_buf[dp + len] |
> > + (ts->rx_buf[dp + len + 1] << 8);
>
> get_unaligned_le16()?
>
> > + if (crc_calc != crc_read) {
> > + fsleep(FT3683G_SPI_CS_DELAY_US);
> > + continue;
> > + }
> > +
> > + memcpy(data, &ts->rx_buf[dp], len);
> > + return 0;
> > + }
> > +
> > + return error ? error : -EIO;
> > +}
> > +
> > +static int ft3683g_read_reg(struct ft3683g_data *ts, u8 addr, u8 *val)
> > +{
> > + return ft3683g_spi_read(ts, addr, val, 1);
> > +}
> > +
> > +static int ft3683g_check_chip_id(struct ft3683g_data *ts)
> > +{
> > + u8 idh, idl;
> > + int error;
> > +
> > + error = ft3683g_read_reg(ts, FT3683G_REG_CHIP_ID, &idh);
> > + if (error)
> > + return error;
> > +
> > + error = ft3683g_read_reg(ts, FT3683G_REG_CHIP_ID2, &idl);
> > + if (error)
> > + return error;
> > +
> > + if (idh != FT3683G_CHIP_IDH || idl != FT3683G_CHIP_IDL) {
> > + dev_err(ts->dev,
> > + "chip ID mismatch: expected %02x%02x, got %02x%02x\n",
> > + FT3683G_CHIP_IDH, FT3683G_CHIP_IDL, idh, idl);
> > + return -ENODEV;
> > + }
> > +
> > + dev_dbg(ts->dev, "chip ID: %02x%02x\n", idh, idl);
> > + return 0;
> > +}
> > +
> > +static irqreturn_t ft3683g_irq(int irq, void *dev_id)
> > +{
> > + struct ft3683g_data *ts = dev_id;
> > + u8 buf[FT3683G_TOUCH_BUF_SIZE];
> > + unsigned int x, y, id, event;
> > + int event_num, i, base;
> > + u8 protocol;
> > + int error;
> > +
> > + error = ft3683g_spi_read(ts, FT3683G_REG_TOUCH_DATA,
> > + buf, sizeof(buf));
> > + if (error) {
> > + dev_err_ratelimited(ts->dev, "failed to read touch data: %d\n",
> > + error);
> > + return IRQ_HANDLED;
>
> Why not IRQ_NONE?
No particular reason, downstream does IRQ_HANDLED so copied it. Will
switch to IRQ_NONE in v2.
>
> > + }
> > +
> > + if (!memchr_inv(&buf[1], FT3683G_FW_INIT, 4)) {
> > + input_mt_sync_frame(ts->input);
> > + input_sync(ts->input);
> > + return IRQ_HANDLED;
>
> I'd do "goto out".
>
> > + }
> > +
> > + protocol = (buf[1] >> 4) & 0x0f;
> > + if (protocol != FT3683G_PROTOCOL_V2)
> > + return IRQ_HANDLED;
> > +
> > + event_num = buf[1] & 0x0f;
> > + if (!event_num || event_num > FT3683G_MAX_POINTS)
> > + return IRQ_HANDLED;
> > +
> > + for (i = 0; i < event_num; i++) {
> > + base = FT3683G_TOUCH_LEN * i + FT3683G_TOUCH_HEADER;
> > +
> > + event = buf[base + FT3683G_TOUCH_OFF_XH] >> 6;
> > + id = buf[base + FT3683G_TOUCH_OFF_YH] >> 4;
> > + if (id >= FT3683G_MAX_POINTS)
> > + continue;
> > +
> > + input_mt_slot(ts->input, id);
> > +
> > + if (event == FT3683G_EVENT_DOWN ||
> > + event == FT3683G_EVENT_CONTACT) {
>
> Could be written as:
>
> if (input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
> event == FT3683G_EVENT_DOWN ||
> event == FT3683G_EVENT_CONTACT) {
> ...
>
> >
> > + x = ((buf[base + FT3683G_TOUCH_OFF_XH] & 0x0f) << 8) |
> > + buf[base + FT3683G_TOUCH_OFF_XL];
>
> get_unaligned_xx16()?
>
> > + y = ((buf[base + FT3683G_TOUCH_OFF_YH] & 0x0f) << 8) |
> > + buf[base + FT3683G_TOUCH_OFF_YL];
> > +
> > + input_mt_report_slot_state(ts->input,
> > + MT_TOOL_FINGER, true);
> > + touchscreen_report_pos(ts->input, &ts->props,
> > + x, y, true);
> > + } else {
> > + input_mt_report_slot_state(ts->input,
> > + MT_TOOL_FINGER, false);
> > + }
> > + }
> > +
> > + input_mt_sync_frame(ts->input);
> > + input_sync(ts->input);
> > + return IRQ_HANDLED;
> > +}
> > +
> > +static int ft3683g_power_on(struct ft3683g_data *ts)
> > +{
> > + int error;
> > +
> > + gpiod_set_value_cansleep(ts->reset_gpio, 1);
> > + usleep_range(2000, 3000);
> > +
> > + error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies);
> > + if (error) {
> > + dev_err(ts->dev, "failed to enable regulators: %d\n", error);
> > + return error;
> > + }
> > +
> > + usleep_range(2000, 3000);
> > + gpiod_set_value_cansleep(ts->reset_gpio, 0);
> > + msleep(50);
> > +
> > + return 0;
> > +}
> > +
> > +static void ft3683g_power_off(struct ft3683g_data *ts)
> > +{
> > + gpiod_set_value_cansleep(ts->reset_gpio, 1);
> > + regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies);
> > +}
> > +
> > +static void ft3683g_power_off_act(void *data)
> > +{
> > + struct ft3683g_data *ts = data;
> > +
> > + ft3683g_power_off(ts);
> > +}
> > +
> > +static int ft3683g_probe(struct spi_device *spi)
> > +{
> > + struct device *dev = &spi->dev;
> > + struct ft3683g_data *ts;
> > + int error;
> > +
> > + ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
> > + if (!ts)
> > + return -ENOMEM;
> > +
> > + ts->dev = dev;
> > + ts->spi = spi;
> > + spi_set_drvdata(spi, ts);
> > + mutex_init(&ts->spi_lock);
> > +
> > + ts->supplies[0].supply = "vddio";
> > + ts->supplies[1].supply = "vci";
> > + error = devm_regulator_bulk_get(dev, ARRAY_SIZE(ts->supplies),
> > + ts->supplies);
> > + if (error)
> > + return dev_err_probe(dev, error, "failed to get regulators\n");
> > +
> > + ts->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
> > + if (IS_ERR(ts->reset_gpio))
> > + return dev_err_probe(dev, PTR_ERR(ts->reset_gpio),
> > + "failed to request reset gpio\n");
> > +
> > + error = ft3683g_power_on(ts);
> > + if (error)
> > + return error;
> > +
> > + error = devm_add_action_or_reset(dev, ft3683g_power_off_act, ts);
> > + if (error)
> > + return error;
> > +
> > + error = ft3683g_check_chip_id(ts);
> > + if (error)
> > + return error;
> > +
> > + ts->input = devm_input_allocate_device(dev);
> > + if (!ts->input)
> > + return -ENOMEM;
> > +
> > + ts->input->name = "FocalTech FT3683G Touchscreen";
> > + ts->input->id.bustype = BUS_SPI;
> > + input_set_drvdata(ts->input, ts);
> > +
> > + input_set_capability(ts->input, EV_ABS, ABS_MT_POSITION_X);
> > + input_set_capability(ts->input, EV_ABS, ABS_MT_POSITION_Y);
> > + touchscreen_parse_properties(ts->input, true, &ts->props);
> > +
> > + error = input_mt_init_slots(ts->input, FT3683G_MAX_POINTS,
> > + INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
> > + if (error)
> > + return dev_err_probe(dev, error, "failed to init MT slots\n");
> > +
> > + error = devm_request_threaded_irq(dev, spi->irq, NULL, ft3683g_irq,
> > + IRQF_ONESHOT, "ft3683g", ts);
> > + if (error)
> > + return dev_err_probe(dev, error, "failed to request IRQ\n");
> > +
> > + error = input_register_device(ts->input);
> > + if (error)
> > + return dev_err_probe(dev, error,
> > + "failed to register input device\n");
> > +
> > + return 0;
> > +}
> > +
> > +static int ft3683g_suspend(struct device *dev)
> > +{
> > + struct ft3683g_data *ts = dev_get_drvdata(dev);
> > +
> > + disable_irq(ts->spi->irq);
> > +
> > + input_mt_sync_frame(ts->input);
> > + input_sync(ts->input);
>
> Why is this needed? We do not do that in any other drivers.
Otherwise if a touch is held when it enters suspend, it'll stay active
afterwards. On suspend core only releases BTN_TOUCH, not MT slot.
Though, I guess this should probably be a core fix and not here?
Also looking at mms114, they do something similar for their suspend:
static int mms114_suspend(struct device *dev)
{
...
/* Release all touch */
for (id = 0; id < MMS114_MAX_TOUCH; id++) {
input_mt_slot(input_dev, id);
input_mt_report_slot_inactive(input_dev);
}
input_mt_report_pointer_emulation(input_dev, true);
input_sync(input_dev);
...
}
Will also address the other reviews in v2.
Thanks,
Alex