Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C91D395AC1 for ; Mon, 1 Jun 2026 20:49:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780346959; cv=none; b=alxAn/WxiVsIJkqcpgYtV+ALOj9EfPCnco2wFl7JHumfbFET1grd6NA/0ulegATDLGu7MGrZ4RpKGf3yb0T9NER28o71C380LPtoxAqNT9O0Ws44v1zz9JBl++p0NU0t4eUvijtncSi85W+KBqO7SYJXeCkEIh1MZdx18yA2UIk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780346959; c=relaxed/simple; bh=oUGCDZ6VyJw30v9RH72gzSb4CYsrvE81xZmdqnpnYAM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KNfbvgtXVFFh6oyyiwW0rH+7yXRZ/GRJzctg6prH/AIpiDJUC8PSJUBQwbaYgeXvO3+YLEAwCJC/cXpVLlNu04POW85Zm5xfB2oB2ugdmEZxVKYQeCUxytbuHQeI5cbJJ5KgFq8hJpZQ+ySytiQZZIL/ts3wBdcyTOMXROy3tl0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pinefeat.co.uk; spf=pass smtp.mailfrom=pinefeat.co.uk; dkim=pass (2048-bit key) header.d=pinefeat.co.uk header.i=@pinefeat.co.uk header.b=Ywva03jc; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pinefeat.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pinefeat.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pinefeat.co.uk header.i=@pinefeat.co.uk header.b="Ywva03jc" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-490a762db7aso13069785e9.0 for ; Mon, 01 Jun 2026 13:49:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pinefeat.co.uk; s=google; t=1780346956; x=1780951756; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=O2B43oqjsThtPG2OPv+KMUmTM0hAHT7fkgP2M1PVT5A=; b=Ywva03jcrBB5V2I0aDwU6p6/afM2mfVuAzVBjfXQ5AKFlLYif8CXTrGPXn8Lxx+ryn Yo++d+K5ScmQgDFtsWMiuBS4BWLXWldTEIH1BLG6je/T2+9P0XlollA1gIteVzr2bjlb FaVpqBXz6n/noettdHsupQcrKriYFnIzcX/IUDPmEYGJpg969qysHCJYCO7XWAWL38mj UkO4KXISPoNZIWdmDerUv6bZIp8N+d6xKw6JOWdedPr8ybAobTERLn5bKvseeydzfLA9 QTHqlmMwCEXmJYihaXX6xJlYURfsH0rxhJIRduViF266leghdesD/5uW8NfbaODYwLsQ ZrOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780346956; x=1780951756; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=O2B43oqjsThtPG2OPv+KMUmTM0hAHT7fkgP2M1PVT5A=; b=FBWCzlwQWfwSdKe4YiETtdPPZ8q6vaztE81FUCKXkzFYhsCgHSSNaNALVJ9FUHC97V KcOstLKWu+5ZcNXtbKBRk3YiL3fK4XMKPRMHZ+ktDohBXNfcEAfJs3/Kpz4iL7RiDIJy PjQ8L2mGQ0zq8dGqXClEMDBOidWluVzWrVCEIzClTHhOr/tOO3SUPaypW66A8rCa8fok 7c2gKc8Ae6nqiBHiNBtd1uUT32K+KreNjKo1EfVdqx99sza1j9X08AvIzoFx4y5Tik30 IjMys2UpTE6QTlUx9zGQvnL0BssPUamfJJ+amehxmHdMl+zT7VTUsC/vOMl0V/UI7Gg+ dxEg== X-Forwarded-Encrypted: i=1; AFNElJ8duWmpJkwi9MhEJSOhnwBrXKb17ZwrdN2ktG9YoTWmQywf5ARFiUi3s+/fOZPtK5vw2/u4uE+LaGvU4A==@vger.kernel.org X-Gm-Message-State: AOJu0YwCfuo7RPbwSAk+pYWHQbaMKBOSSblO8+zdmmt36wCmm8gDYbu6 ZIRXj36yVrCkV8zHaRE7JP/e3Dfs4MUcgdrr+cPBqbSvj2W8w+3UABAzBuFpx0qh/k4= X-Gm-Gg: Acq92OECPxkXn9Kz6YrPuA3xqWu0HjhRh4btVstrpjPlGutuyzzB2qiMGuC1u37plgC oYLI5MD+0QYWrvxau87BByFsv171wf7T8Gzyn1xHq8wYn9SQVK07N9FFYiZaOWFNCrRhW2dRc9h lTaUpEjaoLAg1PsTCS/bqpz+iAh61yJYQin7xYsxdWkMdmOW/Ax9WJ6fK0w803ElLhAq3v+8ODc nL5RdQqoHQKDSrlYQuu94Y38CcN+hOB2ItN0/bZadsXzEOsTTR0fY9r+DZ0WXu3ZKLPPQNpoAAc NkrrhSns7/1OhDZGgCSpx8Nd2TLWKF049hpRdUvx9GiF8ywy8KeQtp0lx5xqV9oybpL00xvGM9c VUp9tIaQ7m0+mW3SCOZsse2gG4TkXjMREFjEtrU7ymX5VA5kF6tzqNd/H8RjQc0/G0cBrbuWCDr CZ+ncL3xQjlAcBo84qmBziNvc7B8Tq7kABXFxSmtNdb9CiHjepZmve X-Received: by 2002:a05:600c:828d:b0:490:507c:bdb1 with SMTP id 5b1f17b1804b1-490a291cbbemr250653585e9.15.1780346955768; Mon, 01 Jun 2026 13:49:15 -0700 (PDT) Received: from asmirnov-G751JM.Home ([2a02:c7c:b28c:1f00:93e4:a0af:c443:477e]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490b0e76153sm16493715e9.11.2026.06.01.13.49.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jun 2026 13:49:15 -0700 (PDT) From: Aliaksandr Smirnou To: Alen Karnil Cc: Aliaksandr Smirnou , Mauro Carvalho Chehab , Aliaksandr Smirnou , Sakari Ailus , Hans Verkuil , Hans de Goede , Vladimir Zapolskiy , Mehdi Djait , Laurent Pinchart , Xiaolei Wang , Walter Werner Schneider , Kate Hsuan , Himanshu Bhavani , Svyatoslav Ryhel , Hardevsinh Palaniya , linux-kernel@vger.kernel.org (open list), linux-media@vger.kernel.org (open list:MEDIA INPUT INFRASTRUCTURE (V4L/DVB)) Subject: [PATCH v7 2/2] media: i2c: Pinefeat cef168 lens control board driver Date: Mon, 1 Jun 2026 21:48:14 +0100 Message-Id: <20260601204814.19148-3-asmirnou@pinefeat.co.uk> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260601204814.19148-1-asmirnou@pinefeat.co.uk> References: <20260601204814.19148-1-asmirnou@pinefeat.co.uk> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add support for the Pinefeat cef168 lens control board that provides electronic focus and aperture control for Canon EF & EF-S lenses on non-Canon camera bodies. Signed-off-by: Aliaksandr Smirnou --- MAINTAINERS | 1 + drivers/media/i2c/Kconfig | 9 + drivers/media/i2c/Makefile | 1 + drivers/media/i2c/cef168.c | 317 +++++++++++++++++++++++++++++ include/uapi/linux/v4l2-controls.h | 6 + 5 files changed, 334 insertions(+) create mode 100644 drivers/media/i2c/cef168.c diff --git a/MAINTAINERS b/MAINTAINERS index 06668674e167..842da3419b0d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -21151,6 +21151,7 @@ M: Aliaksandr Smirnou L: linux-media@vger.kernel.org S: Supported F: Documentation/devicetree/bindings/media/i2c/pinefeat,cef168.yaml +F: drivers/media/i2c/cef168.c PLANTOWER PMS7003 AIR POLLUTION SENSOR DRIVER M: Tomasz Duszynski diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig index 8f2ba4121586..f1bfea009512 100644 --- a/drivers/media/i2c/Kconfig +++ b/drivers/media/i2c/Kconfig @@ -893,6 +893,15 @@ config VIDEO_AK7375 capability. This is designed for linear control of voice coil motors, controlled via I2C serial interface. +config VIDEO_CEF168 + tristate "CEF168 lens control support" + select CRC8 + help + This is a driver for the CEF168 lens control board. + The board provides an I2C interface for electronic focus + and aperture control of EF and EF-S lenses. The driver + integrates with the V4L2 sub-device API. + config VIDEO_DW9714 tristate "DW9714 lens voice coil support" depends on GPIOLIB diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile index 90b276a7417a..c153101cd977 100644 --- a/drivers/media/i2c/Makefile +++ b/drivers/media/i2c/Makefile @@ -25,6 +25,7 @@ obj-$(CONFIG_VIDEO_BT856) += bt856.o obj-$(CONFIG_VIDEO_BT866) += bt866.o obj-$(CONFIG_VIDEO_CCS) += ccs/ obj-$(CONFIG_VIDEO_CCS_PLL) += ccs-pll.o +obj-$(CONFIG_VIDEO_CEF168) += cef168.o obj-$(CONFIG_VIDEO_CS3308) += cs3308.o obj-$(CONFIG_VIDEO_CS5345) += cs5345.o obj-$(CONFIG_VIDEO_CS53L32A) += cs53l32a.o diff --git a/drivers/media/i2c/cef168.c b/drivers/media/i2c/cef168.c new file mode 100644 index 000000000000..596bf549bf25 --- /dev/null +++ b/drivers/media/i2c/cef168.c @@ -0,0 +1,317 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2025 Pinefeat LLP + +#include +#include +#include +#include +#include +#include +#include +#include + +#define CEF168_NAME "cef168" + +#define CEF168_V4L2_CID_CUSTOM(ctrl) \ + (V4L2_CID_USER_CEF168_BASE + custom_##ctrl) + +enum { custom_lens_id, custom_data, custom_calibrate }; + +#define INP_CALIBRATE 0x22 +#define INP_SET_FOCUS 0x80 +#define INP_SET_FOCUS_P 0x81 +#define INP_SET_FOCUS_N 0x82 +#define INP_SET_APERTURE 0x7A +#define INP_SET_APERTURE_P 0x7B +#define INP_SET_APERTURE_N 0x7C + +#define CEF_CRC8_POLYNOMIAL 168 + +DECLARE_CRC8_TABLE(cef168_crc8_table); + +struct cef168_data { + __u8 lens_id; + __u8 moving : 1; + __u8 calibrating : 2; + __u16 moving_time; + __u16 focus_position_min; + __u16 focus_position_max; + __u16 focus_position_cur; + __u16 focus_distance_min; + __u16 focus_distance_max; + __u8 crc8; +} __packed; + +struct cef168_device { + struct v4l2_ctrl_handler ctrls; + struct v4l2_subdev sd; +}; + +static inline struct cef168_device *to_cef168(struct v4l2_ctrl *ctrl) +{ + return container_of(ctrl->handler, struct cef168_device, ctrls); +} + +static inline struct cef168_device *sd_to_cef168(struct v4l2_subdev *subdev) +{ + return container_of(subdev, struct cef168_device, sd); +} + +static int cef168_i2c_write(struct cef168_device *cef168_dev, u8 cmd, u16 val) +{ + struct i2c_client *client = v4l2_get_subdevdata(&cef168_dev->sd); + int retry, ret; + + __le16 le_data = cpu_to_le16(val); + char tx_data[4] = { cmd, ((u8 *)&le_data)[0], ((u8 *)&le_data)[1] }; + + tx_data[3] = crc8(cef168_crc8_table, tx_data, 3, CRC8_INIT_VALUE); + + for (retry = 0; retry < 3; retry++) { + ret = i2c_master_send(client, tx_data, sizeof(tx_data)); + if (ret == sizeof(tx_data)) + return 0; + else if (ret != -EIO && ret != -EREMOTEIO) + break; + } + + dev_err(&client->dev, "I2C write fail after %d retries, ret=%d\n", + retry, ret); + return -EIO; +} + +static int cef168_i2c_read(struct cef168_device *cef168_dev, + struct cef168_data *rx_data) +{ + struct i2c_client *client = v4l2_get_subdevdata(&cef168_dev->sd); + + int ret = i2c_master_recv(client, (char *)rx_data, + sizeof(struct cef168_data)); + if (ret != sizeof(struct cef168_data)) { + dev_err(&client->dev, "I2C read fail, ret=%d\n", ret); + return -EIO; + } + + u8 computed_crc = crc8(cef168_crc8_table, (const u8 *)rx_data, + sizeof(struct cef168_data) - 1, CRC8_INIT_VALUE); + if (computed_crc != rx_data->crc8) { + dev_err(&client->dev, + "CRC mismatch calculated=0x%02X read=0x%02X\n", + computed_crc, rx_data->crc8); + return -EIO; + } + + rx_data->moving_time = le16_to_cpup((__le16 *)&rx_data->moving_time); + rx_data->focus_position_min = le16_to_cpup((__le16 *)&rx_data->focus_position_min); + rx_data->focus_position_max = le16_to_cpup((__le16 *)&rx_data->focus_position_max); + rx_data->focus_position_cur = le16_to_cpup((__le16 *)&rx_data->focus_position_cur); + rx_data->focus_distance_min = le16_to_cpup((__le16 *)&rx_data->focus_distance_min); + rx_data->focus_distance_max = le16_to_cpup((__le16 *)&rx_data->focus_distance_max); + + return 0; +} + +static int cef168_set_ctrl(struct v4l2_ctrl *ctrl) +{ + struct cef168_device *dev = to_cef168(ctrl); + u8 cmd; + + switch (ctrl->id) { + case V4L2_CID_FOCUS_ABSOLUTE: + return cef168_i2c_write(dev, INP_SET_FOCUS, ctrl->val); + case V4L2_CID_FOCUS_RELATIVE: + cmd = ctrl->val < 0 ? INP_SET_FOCUS_N : INP_SET_FOCUS_P; + return cef168_i2c_write(dev, cmd, abs(ctrl->val)); + case V4L2_CID_IRIS_ABSOLUTE: + return cef168_i2c_write(dev, INP_SET_APERTURE, ctrl->val); + case V4L2_CID_IRIS_RELATIVE: + cmd = ctrl->val < 0 ? INP_SET_APERTURE_N : INP_SET_APERTURE_P; + return cef168_i2c_write(dev, cmd, abs(ctrl->val)); + case CEF168_V4L2_CID_CUSTOM(calibrate): + return cef168_i2c_write(dev, INP_CALIBRATE, 0); + } + + return -EINVAL; +} + +static int cef168_get_ctrl(struct v4l2_ctrl *ctrl) +{ + struct cef168_data data; + struct cef168_device *dev = to_cef168(ctrl); + int rval; + + rval = cef168_i2c_read(dev, &data); + if (rval < 0) + return rval; + + switch (ctrl->id) { + case V4L2_CID_FOCUS_ABSOLUTE: + __v4l2_ctrl_modify_range(ctrl, + data.focus_position_min, + data.focus_position_max, 1, 0); + ctrl->val = data.focus_position_cur; + return 0; + case CEF168_V4L2_CID_CUSTOM(lens_id): + ctrl->p_new.p_u8[0] = data.lens_id; + return 0; + case CEF168_V4L2_CID_CUSTOM(data): + memcpy(ctrl->p_new.p_u8, &data, sizeof(data)); + return 0; + } + + return -EINVAL; +} + +static const struct v4l2_ctrl_ops cef168_ctrl_ops = { + .g_volatile_ctrl = cef168_get_ctrl, + .s_ctrl = cef168_set_ctrl, +}; + +static const struct v4l2_ctrl_config cef168_lens_id_ctrl = { + .ops = &cef168_ctrl_ops, + .id = CEF168_V4L2_CID_CUSTOM(lens_id), + .type = V4L2_CTRL_TYPE_U8, + .name = "Lens ID", + .min = 0, + .max = U8_MAX, + .step = 1, + .def = 0, + .flags = V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY, +}; + +static const struct v4l2_ctrl_config cef168_data_ctrl = { + .ops = &cef168_ctrl_ops, + .id = CEF168_V4L2_CID_CUSTOM(data), + .type = V4L2_CTRL_TYPE_U8, + .name = "Data", + .min = 0, + .max = U8_MAX, + .step = 1, + .def = 0, + .dims = { sizeof(struct cef168_data) / sizeof(u8) }, + .elem_size = sizeof(u8), + .flags = V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY, +}; + +static const struct v4l2_ctrl_config cef168_calibrate_ctrl = { + .ops = &cef168_ctrl_ops, + .id = CEF168_V4L2_CID_CUSTOM(calibrate), + .type = V4L2_CTRL_TYPE_BUTTON, + .name = "Calibrate", +}; + +static const struct v4l2_subdev_core_ops cef168_core_ops = { + .log_status = v4l2_ctrl_subdev_log_status, + .subscribe_event = v4l2_ctrl_subdev_subscribe_event, + .unsubscribe_event = v4l2_event_subdev_unsubscribe, +}; + +static const struct v4l2_subdev_ops cef168_ops = { + .core = &cef168_core_ops, +}; + +static int cef168_init_controls(struct cef168_device *dev) +{ + struct v4l2_ctrl *ctrl; + struct v4l2_ctrl_handler *hdl = &dev->ctrls; + const struct v4l2_ctrl_ops *ops = &cef168_ctrl_ops; + + v4l2_ctrl_handler_init(hdl, 7); + + ctrl = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE, 0, S16_MAX, + 1, 0); + if (ctrl) + ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE | + V4L2_CTRL_FLAG_EXECUTE_ON_WRITE; + v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_RELATIVE, S16_MIN, S16_MAX, + 1, 0); + ctrl = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_IRIS_ABSOLUTE, 0, S16_MAX, + 1, 0); + if (ctrl) + ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY | + V4L2_CTRL_FLAG_EXECUTE_ON_WRITE; + v4l2_ctrl_new_std(hdl, ops, V4L2_CID_IRIS_RELATIVE, S16_MIN, S16_MAX, 1, + 0); + ctrl = v4l2_ctrl_new_custom(hdl, &cef168_calibrate_ctrl, NULL); + if (ctrl) + ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY | + V4L2_CTRL_FLAG_EXECUTE_ON_WRITE; + v4l2_ctrl_new_custom(hdl, &cef168_data_ctrl, NULL); + v4l2_ctrl_new_custom(hdl, &cef168_lens_id_ctrl, NULL); + + if (hdl->error) + dev_err(dev->sd.dev, "%s fail error: 0x%x\n", __func__, + hdl->error); + dev->sd.ctrl_handler = hdl; + return hdl->error; +} + +static int cef168_probe(struct i2c_client *client) +{ + struct cef168_device *cef168_dev; + int rval; + + cef168_dev = devm_kzalloc(&client->dev, sizeof(*cef168_dev), + GFP_KERNEL); + if (!cef168_dev) + return -ENOMEM; + + v4l2_i2c_subdev_init(&cef168_dev->sd, client, &cef168_ops); + cef168_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | + V4L2_SUBDEV_FL_HAS_EVENTS; + + rval = cef168_init_controls(cef168_dev); + if (rval) + goto err_cleanup; + + rval = media_entity_pads_init(&cef168_dev->sd.entity, 0, NULL); + if (rval < 0) + goto err_cleanup; + + cef168_dev->sd.entity.function = MEDIA_ENT_F_LENS; + + rval = v4l2_async_register_subdev(&cef168_dev->sd); + if (rval < 0) + goto err_cleanup; + + crc8_populate_msb(cef168_crc8_table, CEF_CRC8_POLYNOMIAL); + + return 0; + +err_cleanup: + v4l2_ctrl_handler_free(&cef168_dev->ctrls); + media_entity_cleanup(&cef168_dev->sd.entity); + + return rval; +} + +static void cef168_remove(struct i2c_client *client) +{ + struct v4l2_subdev *sd = i2c_get_clientdata(client); + struct cef168_device *cef168_dev = sd_to_cef168(sd); + + v4l2_async_unregister_subdev(&cef168_dev->sd); + v4l2_ctrl_handler_free(&cef168_dev->ctrls); + media_entity_cleanup(&cef168_dev->sd.entity); +} + +static const struct of_device_id cef168_of_table[] = { + { .compatible = "pinefeat,cef168" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, cef168_of_table); + +static struct i2c_driver cef168_i2c_driver = { + .driver = { + .name = CEF168_NAME, + .of_match_table = cef168_of_table, + }, + .probe = cef168_probe, + .remove = cef168_remove, +}; + +module_i2c_driver(cef168_i2c_driver); + +MODULE_AUTHOR("support@pinefeat.co.uk>"); +MODULE_DESCRIPTION("CEF168 lens driver"); +MODULE_LICENSE("GPL"); diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h index 68dd0c4e47b2..f2cae365612f 100644 --- a/include/uapi/linux/v4l2-controls.h +++ b/include/uapi/linux/v4l2-controls.h @@ -234,6 +234,12 @@ enum v4l2_colorfx { */ #define V4L2_CID_USER_MALI_C55_BASE (V4L2_CID_USER_BASE + 0x1230) +/* + * The base for Pinefeat CEF168 driver controls. + * We reserve 16 controls for this driver. + */ +#define V4L2_CID_USER_CEF168_BASE (V4L2_CID_USER_BASE + 0x1240) + /* MPEG-class control IDs */ /* The MPEG controls are applicable to all codec controls * and the 'MPEG' part of the define is historical */ -- 2.34.1