dt-bindings: Pinefeat cef168 lens control board
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v7 1/2 dt-bindings: Pinefeat cef168 lens control board
Add the Device Tree schema and examples for the Pinefeat cef168 lens control board. This board interfaces Canon EF & EF-S lenses with non-Canon camera bodies, enabling electronic control of focus and aperture via V4L2.
Power supply is derived from fixed supplies via connector or GPIO header. Therefore, the driver does not manage any regulator, so representing any supply in the binding is redundant.
--- .../bindings/media/i2c/pinefeat,cef168.yaml | 47 +++++++++++++++++++ .../devicetree/bindings/vendor-prefixes.yaml | 2 + MAINTAINERS | 6 +++ 3 files changed, 55 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/i2c/pinefeat,cef168.yaml
Documentation/devicetree/bindings/media/i2c/pinefeat,cef168.yaml new file +47 -0
@@ -0,0 +1,47 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +# Copyright (c) 2025 Pinefeat LLP +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/i2c/pinefeat,cef168.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Pinefeat cef168 lens driver + +maintainers: + - Aliaksandr Smirnou <[email protected]> + +description: | + Pinefeat produces an adapter designed to interface between + Canon EF & EF-S lenses and non-Canon camera bodies, incorporating + features for electronic focus and aperture adjustment. The cef168 + circuit board, included with the adapter, provides a software + programming interface that allows control of lens focus and + aperture positions. + +properties: + compatible: + enum: + - pinefeat,cef168 + + reg: + maxItems: 1 + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + camera-lens@d { + compatible = "pinefeat,cef168"; + reg = <0x0d>; + }; + }; + +...
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
@@ -1289,6 +1289,8 @@ patternProperties: description: Picochip Ltd "^pine64,.*": description: Pine64 + "^pinefeat,.*": + description: Pinefeat LLP "^pineriver,.*": description: Shenzhen PineRiver Designs Co., Ltd. "^pixcir,.*":
MAINTAINERS +6 -0
@@ -21146,6 +21146,12 @@ S: Supported F: Documentation/devicetree/bindings/input/pine64,pinephone-keyboard.yaml F: drivers/input/keyboard/pinephone-keyboard.c +PINEFEAT CEF168 LENS DRIVER +M: Aliaksandr Smirnou <[email protected]> +L: [email protected] +S: Supported +F: Documentation/devicetree/bindings/media/i2c/pinefeat,cef168.yaml + PLANTOWER PMS7003 AIR POLLUTION SENSOR DRIVER M: Tomasz Duszynski <[email protected]> S: Maintained
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The Device Tree schema describes software rather than the physical hardware, violating Device Tree…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The Device Tree schema describes software rather than the physical hardware, violating Device Tree design principles. The title explicitly calls the binding a 'driver' instead of the hardware device, and the commit message intentionally omits power supply definitions because 'the driver does not manage any regulator', which couples the hardware description to current software limitations. --
commit 5752dd4b724a501f53ddad43b87fa9c0328d6fb5
Author: Aliaksandr Smirnou <[email protected]>
dt-bindings: Pinefeat cef168 lens control board
This commit adds the Device Tree schema and examples for the Pinefeat cef168 lens control board. It supports controlling Canon EF and EF-S lenses via V4L2.
Quoted 3 lines
Power supply is derived from fixed supplies via connector or GPIO header. Therefore, the driver does not manage any regulator, so representing any supply in the binding is redundant.
[Severity: Low]
Device tree is intended to describe the physical hardware independent of the operating system or current driver features. Shouldn't the physical power rails be described in the binding, even if the current driver does not actively manage them?
Quoted 6 lines
Documentation/devicetree/bindings/media/i2c/pinefeat,cef168.yaml new file +0
-0@@ -0,0 +1,47 @@
[ ... ]
Documentation/devicetree/bindings/media/i2c/pinefeat,cef168.yaml line 8
Quoted 4 lines
+$id: http://devicetree.org/schemas/media/i2c/pinefeat,cef168.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#Documentation/devicetree/bindings/media/i2c/pinefeat,cef168.yaml new file +2
-0+ +title: Pinefeat cef168 lens driver
[Severity: Low]
Does this title reflect a conceptual mismatch by referring to a software driver rather than the hardware device? Would it be better to title this "Pinefeat cef168 lens control board" to match standard device tree naming practices?
[ ... ]
Documentation/devicetree/bindings/media/i2c/pinefeat,cef168.yaml line 33
Quoted 5 lines
Documentation/devicetree/bindings/media/i2c/pinefeat,cef168.yaml new file +5
-0+required: + - compatible + - reg + +additionalProperties: false
[Severity: Low]
Because physical power supplies are omitted from the schema, does setting additionalProperties to false prevent creating a complete, hardware-accurate device tree? If someone describes the board's actual power rails in their device tree, it will fail schema validation. Should the physical supply properties be added here to allow accurate hardware descriptions?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v7 2/2 media: i2c: Pinefeat cef168 lens control board driver
In reply to a message not in the archiveAdd 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.
--- 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
MAINTAINERS +1 -0
@@ -21151,6 +21151,7 @@ M: Aliaksandr Smirnou <[email protected]> L: [email protected] 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 <[email protected]>
drivers/media/i2c/Kconfig +9 -0
@@ -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
drivers/media/i2c/Makefile +1 -0
@@ -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
drivers/media/i2c/cef168.c new file +317 -0
317 lines not on this page · raw message
include/uapi/linux/v4l2-controls.h +6 -0
@@ -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 */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v7 2/2] media: i2c: Pinefeat cef168 lens control board driver
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] Data race on global CRC table and early userspace exposure. - [High] Unintended hardware writes…
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] Data race on global CRC table and early userspace exposure.
- [High] Unintended hardware writes triggered from a read-only volatile getter.
- [High] UAPI ABI breakage due to architecture-dependent bitfields and endianness exposure.
- [High] I2C transfers performed using stack-allocated buffers.
- [High] Use-After-Free risk due to combining devm_kzalloc with V4L2 device nodes.
- [Medium] Pointer cast of a packed struct member triggers warning and risks unaligned access.
- [Low] Pointer signedness compiler warning in CRC function call.
--
commit b6486dc31f9f1b48eddfb7b13dda9a4b186db9ac
Author: Aliaksandr Smirnou <[email protected]>
media: i2c: Pinefeat cef168 lens control board driver
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.
drivers/media/i2c/cef168.c new file +0 -0
@@ -0,0 +1,317 @@
[ ... ]
Review
Quoted 12 lines
(quoted diff) +12
-0+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;
[Severity: High]
Does this struct definition break the UAPI ABI since it is mapped directly to userspace later?
The C bitfields (moving, calibrating) will have different memory layouts depending on the compiler and architecture endianness, meaning userspace cannot reliably decode this data.
[ ... ]
Review
Quoted 7 lines
(quoted diff) +7
-0+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] };
[Severity: High]
Can this cause memory corruption or kernel panics when CONFIG_VMAP_STACK is enabled?
The local array tx_data is allocated on the stack and passed directly to i2c_master_send(). If the I2C adapter driver uses DMA, passing stack memory for DMA operations is unsafe.
Should this buffer be dynamically allocated and embedded in the heap-allocated device structure instead?
Review
Quoted 2 lines
(quoted diff) +2
-0+ + tx_data[3] = crc8(cef168_crc8_table, tx_data, 3, CRC8_INIT_VALUE);
[Severity: Low]
Does passing a char array to crc8() trigger a -Wpointer-sign warning?
crc8() expects a const u8 *pdata. Should tx_data be declared as a u8 array instead?
Quoted 8 lines
(quoted diff) +8
-0+ + 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; + }
[ ... ]
Review
Quoted 7 lines
(quoted diff) +7
-0+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));
[Severity: High]
Is this passing stack memory to the I2C core again?
rx_data references a struct cef168_data instantiated locally on the stack in cef168_get_ctrl(). This can cause DMA corruption if the I2C adapter uses DMA for transfers.
Quoted 5 lines
(quoted diff) +5
-0+ if (ret != sizeof(struct cef168_data)) { + dev_err(&client->dev, "I2C read fail, ret=%d\n", ret); + return -EIO; + } +
Review
Quoted 10 lines
(quoted diff) +10
-0+ 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);
[Severity: Medium]
Will this pointer cast of a packed struct member cause unaligned access issues or trigger a -Waddress-of-packed-member warning?
Casting the address of rx_data->moving_time to a __le16 pointer strips its unaligned qualification. If the struct is allocated on an odd boundary, this could result in an unaligned load exception.
Could this just use le16_to_cpu(rx_data->moving_time) or get_unaligned_le16() instead?
Quoted 1 line
(quoted diff) +1
-0+ rx_data->focus_position_min = le16_to_cpup((__le16 *)&rx_data->focus_position_min);
[ ... ]
Quoted 5 lines
(quoted diff) +5
-0+static int cef168_get_ctrl(struct v4l2_ctrl *ctrl) +{ + struct cef168_data data; + struct cef168_device *dev = to_cef168(ctrl); + int rval;
Review
Quoted 10 lines
(quoted diff) +10
-0+ + 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);
[Severity: High]
Can modifying the control range here inadvertently trigger a hardware write?
Since this is the g_volatile_ctrl callback, it executes during a read operation. If the newly retrieved hardware limits invalidate the control's current cached value, the V4L2 framework will automatically clamp it and call s_ctrl(), which in turn issues an I2C command to move the lens.
This means a read-only GET ioctl could cause physical hardware changes.
Review
Quoted 7 lines
(quoted diff) +7
-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));
[Severity: High]
Does this expose in-place converted endianness and architecture-dependent bitfields to userspace?
The struct cef168_data has had its fields modified in-place using le16_to_cpup() during cef168_i2c_read(). Copying it directly into the U8 array means the raw bytes have an unstable, arch-dependent byte order, making it unreliable for userspace to decode.
Quoted 5 lines
(quoted diff) +5
-0+ return 0; + } + + return -EINVAL; +}
[ ... ]
Review
Quoted 7 lines
(quoted diff) +7
-0+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);
[Severity: High]
Does allocating this memory with devm_kzalloc() create a use-after-free risk?
Since the driver registers a V4L2 subdevice node with the V4L2_SUBDEV_FL_HAS_DEVNODE flag, userspace can hold an open file descriptor to it. If the I2C driver is unbound via sysfs, devres will free cef168_dev immediately.
When the file descriptor is later closed, the V4L2 core will dereference the freed memory in v4l2_subdev_release().
Should this use dynamic allocation with a proper release callback?
Quoted 12 lines
(quoted diff) +12
-0+ 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);
Review
Quoted 10 lines
(quoted diff) +10
-0+ 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);
[Severity: High]
Is there a data race on the global cef168_crc8_table here?
crc8_populate_msb() modifies a shared global array. Because v4l2_async_register_subdev() has already been called, the device is exposed to userspace and could trigger I2C commands using an uninitialized or partially initialized CRC table. Furthermore, if multiple devices are probed concurrently, this will corrupt the table values.
Quoted 2 lines
(quoted diff) +2
-0+ + return 0;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v7 2/2] media: i2c: Pinefeat cef168 lens control board driver
In reply to Aliaksandr SmirnouHi Aliaksandr,
Quoted 5 lines · Aliaksandr Smirnou
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 <[email protected]>
I have successfully tested this on a RPi 5 with a Canon EF-S 18-55mm lens.
I have no further questions in regards to the driver implementation.
Quoted 393 lines
--- 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.cMAINTAINERS +1
-0@@ -21151,6 +21151,7 @@ M: Aliaksandr Smirnou <[email protected]> L: [email protected] 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 <[email protected]>drivers/media/i2c/Kconfig +9
-0@@ -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 GPIOLIBdrivers/media/i2c/Makefile +1
-0@@ -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.odrivers/media/i2c/cef168.c new file +317
-0@@ -0,0 +1,317 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2025 Pinefeat LLP + +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/i2c.h> +#include <linux/module.h> +#include <linux/v4l2-controls.h> +#include <media/v4l2-ctrls.h> +#include <media/v4l2-device.h> +#include <media/v4l2-event.h> + +#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 ("[email protected]>" ); +MODULE_DESCRIPTION ("CEF168 lens driver" ); +MODULE_LICENSE ("GPL" );include/uapi/linux/v4l2-controls.h +6
-0@@ -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 */