KT0913 FM/AM driver
Subsystems (2)
cover KT0913 FM/AM driver
media: adds support for kt0913 FM/AM tuner chip
Adds a driver for the KT0913 FM/AM tuner chip from KT Micro. This chip can be found on many low cost FM/AM radios and DVD/Home Theaters. The chip provides two ways of usage, a manual mode (requiring only a few buttons) or complete control via I2C. This driver uses the latter. It exposes the minimum functionality of this chip, which includes tuning an AM or FM station given its frequency, reading the signal strength, setting Stereo (only on FM) or Mono (available on AM/FM), Mute, Volume and Audio Gain.
I left some TODOs on the code, like supporting the chip's hardware seek feature, using a RW/RO regmaps rather than a single volatile regmap, show the FM SNR as a RO control and the FM/AM AFC deviation as another RO control, and supporting the KT0915 chip.
The module I've used comes from SZZSJDZ.com, a now defunct company. However, it's possible to buy this chip directly from Aliexpress or similar sites.
I tested this on two systems, the first one being a Raspberry Pi 4 with the unstable 5.x kernel, but later I moved to a Banana Pi 2 Zero where I used the (current at this time, 07d999f) master of this repo for testing. I've also compiled the v4l-compliance from current sources (79918a59) and it passed all the tests. The output of that is at the end of this note.
Note: This is the third set of patches for the driver, where I (tried to) address the comments that the reviewers added on the previous sets.
Differences between v1 and v2: Fixed the logic for band selection when running the S_FREQUENCY ioctl. Before this, it would return a -EINVAL error when an improper frequency was selected. Now it tries to select the nearest available band frequency.
Fixed some text alignment issues.
Difference between v2 and v3: Fixed typos (CAMUS -> CAMPUS), text alignment fixes, converted all hex values into lowercase, renamed driver name to just kt0913 rather than kt0913-am-fm.
v4l2-compliance SHA: 79918a591a9ad362f107795ee4046d39e6dfcb67, 32 bits, 32-bit time_t
Compliance test for kt0913 device /dev/radio0:
Driver Info:
Driver name : kt0913 Card type : kt0913 Bus info : I2C:radio0 Driver version : 5.9.0 Capabilities : 0x80250000
Tuner
Radio
Extended Pix Format
Device Capabilities
Device Caps : 0x00250000
Tuner
Radio
Extended Pix Format
Required ioctls:
test VIDIOC_QUERYCAP: OK
Allow for multiple opens:
test second /dev/radio0 open: OK
test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK
test invalid ioctls: OK
Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK
test VIDIOC_LOG_STATUS: OK
Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK test VIDIOC_G/S_FREQUENCY: OK
test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported) test VIDIOC_ENUMAUDIO: OK (Not Supported) test VIDIOC_G/S/ENUMINPUT: OK (Not Supported) test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 0 Audio Inputs: 0 Tuners: 1
Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK
test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0
Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported) test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported) test VIDIOC_G/S_EDID: OK (Not Supported)
Control ioctls:
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK test VIDIOC_QUERYCTRL: OK
test VIDIOC_G/S_CTRL: OK
test VIDIOC_G/S/TRY_EXT_CTRLS: OK test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK test VIDIOC_G/S_JPEGCOMP: OK (Not Supported) Standard Controls: 8 Private Controls: 0
Format ioctls:
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK (Not Supported) test VIDIOC_G/S_PARM: OK (Not Supported)
test VIDIOC_G_FBUF: OK (Not Supported)
test VIDIOC_G_FMT: OK (Not Supported)
test VIDIOC_TRY_FMT: OK (Not Supported)
test VIDIOC_S_FMT: OK (Not Supported)
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK (Not Supported)
Codec ioctls:
test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported) test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)
Buffer ioctls:
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK (Not Supported) test VIDIOC_EXPBUF: OK (Not Supported)
test Requests: OK (Not Supported)
Total for kt0913 device /dev/radio0: 45, Succeeded: 45, Failed: 0, Warnings: 0
Santiago Hormazabal (3):
dt-bindings: vendor-prefixes: Add KT Micro
media: Add support for the AM/FM radio chip KT0913 from KT Micro.
media: kt0913: device tree binding
.../bindings/media/i2c/ktm,kt0913.yaml | 56 + .../devicetree/bindings/vendor-prefixes.yaml | 2 + drivers/media/radio/Kconfig | 10 + drivers/media/radio/Makefile | 1 + drivers/media/radio/radio-kt0913.c | 1196 +++++++++++++++++ 5 files changed, 1265 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/i2c/ktm,kt0913.yaml create mode 100644 drivers/media/radio/radio-kt0913.c
Signature · 1 line
2.24.1
PATCH 1/3 dt-bindings: vendor-prefixes: Add KT Micro
Adds ktm as the prefix of KT Micro, Inc.
--- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
@@ -551,6 +551,8 @@ patternProperties: description: Kontron S&T AG "^kosagi,.*": description: Sutajio Ko-Usagi PTE Ltd. + "^ktm,.*": + description: KT Micro, Inc. "^kyo,.*": description: Kyocera Corporation "^lacie,.*":
Signature · 1 line
2.24.1
Re: [PATCH 1/3] dt-bindings: vendor-prefixes: Add KT Micro
Quoted 7 lines · Santiago Hormazabal
Adds ktm as the prefix of KT Micro, Inc.
Signed-off-by Santiago Hormazabal <[email protected]>--- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+)
PATCH 2/3 media: Add support for the AM/FM radio chip KT0913 from KT Micro.
In reply to Santiago HormazabalThis chip requires almost no support components and can used over I2C. The driver uses the I2C bus and exposes the controls as a V4L2 radio. Tested with a module that contains this chip (from SZZSJDZ.com, part number ZJ-801B, even tho the company seems defunct now), and an H2+ AllWinner SoC running a kernel built off 07d999f of the media_tree.
--- drivers/media/radio/Kconfig | 10 + drivers/media/radio/Makefile | 1 + drivers/media/radio/radio-kt0913.c | 1196 ++++++++++++++++++++++++++++ 3 files changed, 1207 insertions(+) create mode 100644 drivers/media/radio/radio-kt0913.c
drivers/media/radio/Kconfig +10 -0
@@ -226,6 +226,16 @@ config RADIO_WL1273 # TI's ST based wl128x FM radio source "drivers/media/radio/wl128x/Kconfig" +config RADIO_KT0913 + tristate "KT0913 I2C FM/AM radio support" + depends on I2C && VIDEO_V4L2 + help + Say Y here if you want to use the KT0913 FM/AM chip. + This is a low cost chip that uses the I2C bus. + + To compile this driver as a module, choose M here: the + module will be called radio-kt0913. + # # ISA drivers configuration #
drivers/media/radio/Makefile +1 -0
@@ -34,5 +34,6 @@ obj-$(CONFIG_RADIO_WL1273) += radio-wl1273.o obj-$(CONFIG_RADIO_WL128X) += wl128x/ obj-$(CONFIG_RADIO_TEA575X) += tea575x.o obj-$(CONFIG_USB_RAREMONO) += radio-raremono.o +obj-$(CONFIG_RADIO_KT0913) += radio-kt0913.o shark2-objs := radio-shark2.o radio-tea5777.o
drivers/media/radio/radio-kt0913.c new file +1196 -0
1,196 lines not on this page · raw message
Signature · 1 line
2.24.1
Re: [PATCH 2/3] media: Add support for the AM/FM radio chip KT0913 from KT Micro.
Quoted 5 lines · Santiago Hormazabal
This chip requires almost no support components and can used over I2C. The driver uses the I2C bus and exposes the controls as a V4L2 radio. Tested with a module that contains this chip (from SZZSJDZ.com, part number ZJ-801B, even tho the company seems defunct now), and an H2+ AllWinner SoC running a kernel built off 07d999f of the
media_tree.
Thanks.
style trivia:
[]
drivers/media/radio/radio-kt0913.c
Quoted 1 line
drivers/media/radio/radio-kt0913.c +0
-0
[]
drivers/media/radio/radio-kt0913.c line 223
Quoted 3 lines
drivers/media/radio/radio-kt0913.c +3
-0+static const struct reg_sequence kt0913_init_regs_to_defaults[] = { +/* Standby disabled, volume 0dB */ + {KT0913_REG_RXCFG ,0x881f },
These might be more legible on single lines,
ignoring the 80 column limits.
drivers/media/radio/radio-kt0913.c line 225
Quoted 2 lines
drivers/media/radio/radio-kt0913.c +2
-0+/* FM Channel spacing = 50kHz, Right & Left unmuted */ + { KT0913_REG_SEEK, 0x000b },
etc...
[]
drivers/media/radio/radio-kt0913.c line 358
Quoted 5 lines
drivers/media/radio/radio-kt0913.c +5
-0+static int __kt0913_set_fm_frequency (struct kt0913_device *radio, +unsigned int frequency) +{ +return regmap_write (radio->regmap,KT0913_REG_TUNE , +KT0913_TUNE_FMTUNE_ON | (frequency /KT0913_FMCHAN_MUL ));
It might be nicer to align multi-line statements to the open parenthesis.
[]
Quoted 13 lines
drivers/media/radio/radio-kt0913.c +13
-0+static int __kt0913_set_au_gain (struct kt0913_device *radio,s32 gain) +{ +switch (gain) { +case 6 : +return regmap_update_bits (radio->regmap, +KT0913_REG_AMSYSCFG ,KT0913_AMSYSCFG_AU_GAIN_MASK , +KT0913_AMSYSCFG_AU_GAIN_6DB ); +case 3 : +return regmap_update_bits (radio->regmap, +KT0913_REG_AMSYSCFG ,KT0913_AMSYSCFG_AU_GAIN_MASK , +KT0913_AMSYSCFG_AU_GAIN_3DB ); +case 0 : + return regmap_update_bits(radio->regmap,
drivers/media/radio/radio-kt0913.c line 514
Quoted 10 lines
drivers/media/radio/radio-kt0913.c +10
-0+KT0913_REG_AMSYSCFG ,KT0913_AMSYSCFG_AU_GAIN_MASK , + KT0913_AMSYSCFG_AU_GAIN_0DB); +case -3 : +return regmap_update_bits (radio->regmap, +KT0913_REG_AMSYSCFG ,KT0913_AMSYSCFG_AU_GAIN_MASK , +KT0913_AMSYSCFG_AU_GAIN_MIN_3DB ); +default : +return -EINVAL ; + } +}
It's generally more legible to write this with an intermediate variable holding the changed value. It's also most commonly smaller object code.
static int __kt0913_set_au_gain(struct kt0913_device *radio, s32 gain)
{
int val;
switch (gain) {
case 6:
val = KT0913_AMSYSCFG_AU_GAIN_6DB;
break;
case 3:
val = KT0913_AMSYSCFG_AU_GAIN_3DB;
break;
case 0:
val = KT0913_AMSYSCFG_AU_GAIN_0DB;
break;
case -3:
val = KT0913_AMSYSCFG_AU_GAIN_MIN_3DB;
break;
default:
return -EINVAL;
}
return regmap_update_bits(radio->regmap, KT0913_REG_AMSYSCFG,
KT0913_AMSYSCFG_AU_GAIN_MASK, val);
}Re: [PATCH 2/3] media: Add support for the AM/FM radio chip KT0913 from KT Micro.
Hi Joe,
Thanks for the feedback.
Quoted 19 lines · Joe Perches
Quoted 5 lines · Santiago Hormazabal
This chip requires almost no support components and can used over I2C. The driver uses the I2C bus and exposes the controls as a V4L2 radio. Tested with a module that contains this chip (from SZZSJDZ.com, part number ZJ-801B, even tho the company seems defunct now), and an H2+ AllWinner SoC running a kernel built off 07d999f of the
media_tree.Thanks.
style trivia:
[]
drivers/media/radio/radio-kt0913.c
Quoted 1 line
drivers/media/radio/radio-kt0913.c +0
-0[]
drivers/media/radio/radio-kt0913.c line 223
Quoted 3 lines
drivers/media/radio/radio-kt0913.c +3
-0+static const struct reg_sequence kt0913_init_regs_to_defaults[] = { +/* Standby disabled, volume 0dB */ + {KT0913_REG_RXCFG ,0x881f },
Quoted 8 lines
These might be more legible on single lines,
ignoring the 80 column limits.drivers/media/radio/radio-kt0913.c line 225
Quoted 2 lines
drivers/media/radio/radio-kt0913.c +2
-0+/* FM Channel spacing = 50kHz, Right & Left unmuted */ + { KT0913_REG_SEEK, 0x000b },etc...
I agree, didn't know I could exceed the limit in these situations.
Quoted 11 lines
[]
drivers/media/radio/radio-kt0913.c line 358
Quoted 5 lines
drivers/media/radio/radio-kt0913.c +5
-0+static int __kt0913_set_fm_frequency (struct kt0913_device *radio, +unsigned int frequency) +{ +return regmap_write (radio->regmap,KT0913_REG_TUNE , +KT0913_TUNE_FMTUNE_ON | (frequency /KT0913_FMCHAN_MUL ));It might be nicer to align multi-line statements to the open parenthesis.
Yes, that's totally true. What about these cases where the other part of the lines would exceed 80 chars? For instance, if I align the second line to the open parenthesis of the first line, I'll be past the 80 chars limit. Splitting it back again to fit that would make the code not so much readable.
Quoted 46 lines
[]
Quoted 13 lines
drivers/media/radio/radio-kt0913.c +13
-0+static int __kt0913_set_au_gain (struct kt0913_device *radio,s32 gain) +{ +switch (gain) { +case 6 : +return regmap_update_bits (radio->regmap, +KT0913_REG_AMSYSCFG ,KT0913_AMSYSCFG_AU_GAIN_MASK , +KT0913_AMSYSCFG_AU_GAIN_6DB ); +case 3 : +return regmap_update_bits (radio->regmap, +KT0913_REG_AMSYSCFG ,KT0913_AMSYSCFG_AU_GAIN_MASK , +KT0913_AMSYSCFG_AU_GAIN_3DB ); +case 0 : + return regmap_update_bits(radio->regmap,drivers/media/radio/radio-kt0913.c line 514
Quoted 10 lines
drivers/media/radio/radio-kt0913.c +10
-0+KT0913_REG_AMSYSCFG ,KT0913_AMSYSCFG_AU_GAIN_MASK , + KT0913_AMSYSCFG_AU_GAIN_0DB); +case -3 : +return regmap_update_bits (radio->regmap, +KT0913_REG_AMSYSCFG ,KT0913_AMSYSCFG_AU_GAIN_MASK , +KT0913_AMSYSCFG_AU_GAIN_MIN_3DB ); +default : +return -EINVAL ; + } +}It's generally more legible to write this with an intermediate variable holding the changed value. It's also most commonly smaller object code.
static int
__kt0913_set_au_gain(struct kt0913_device *radio, s32 gain){ int val; switch (gain) { case 6: val = KT0913_AMSYSCFG_AU_GAIN_6DB; break; case 3: val = KT0913_AMSYSCFG_AU_GAIN_3DB; break; case 0: val = KT0913_AMSYSCFG_AU_GAIN_0DB; break; case -3: val = KT0913_AMSYSCFG_AU_GAIN_MIN_3DB;
Quoted 10 lines
break;
default:
return -EINVAL;
}
return regmap_update_bits(radio->regmap, KT0913_REG_AMSYSCFG,
KT0913_AMSYSCFG_AU_GAIN_MASK, val);
}I agree, thanks for your feedback.
I'll wait for your reply to fix the other issue you've mentioned, and I'll fix the others in the meantime.
Thanks!
- Santiago H.
Re: [PATCH 2/3] media: Add support for the AM/FM radio chip KT0913 from KT Micro.
In reply to Santiago HormazabalHi Santiago,
Quoted 6 lines · Santiago Hormazabal
This chip requires almost no support components and can used over I2C. The driver uses the I2C bus and exposes the controls as a V4L2 radio. Tested with a module that contains this chip (from SZZSJDZ.com, part number ZJ-801B, even tho the company seems defunct now), and an H2+ AllWinner SoC running a kernel built off 07d999f of the
media_tree.
Quoted 7 lines
Signed-off-by Santiago Hormazabal <[email protected]>--- drivers/media/radio/Kconfig | 10 + drivers/media/radio/Makefile | 1 + drivers/media/radio/radio-kt0913.c | 1196 ++++++++++++++++++++++++++++ 3 files changed, 1207 insertions(+) create mode 100644 drivers/media/radio/radio-kt0913.c
One more thing: you need to add an entry to the MAINTAINERS file for this driver. It can either be part of this patch, or added in a separate patch.
Regards,
Hans
PATCH 3/3 media: kt0913: device tree binding
In reply to Santiago HormazabalDocument bindings for the kt0913 AM/FM radio tuner.
--- .../bindings/media/i2c/ktm,kt0913.yaml | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/i2c/ktm,kt0913.yaml
Documentation/devicetree/bindings/media/i2c/ktm,kt0913.yaml new file +56 -0
@@ -0,0 +1,56 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +# Copyright (c) 2020 Santiago Hormazabal <[email protected]> +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/ktm,kt0913.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Device-Tree bindings for the KTMicro KT0913 FM/AM radio tuner. + +maintainers: + - Santiago Hormazabal <[email protected]> + +description: |- + The KT0913 is a low cost, low components FM/AM radio chip. + It uses the I2C protocol for operation. + +properties: + compatible: + const: ktm,kt0913 + + reg: + description: I2C device address + const: 0x35 + + ktm,anti-pop: + description: | + Selects the DAC Anti-Pop capacitor. Possible values are + 0 thru 3, which corresponds to 100uF (default), 60uF, 20uF or 10uF. + $ref: "/schemas/types.yaml#/definitions/uint32" + enum: [0, 1, 2, 3] + + ktm,refclk: + description: | + Selects the reference clock used on the KT0913. Possible + values are 0 thru 9, which corresponds to 32.768kHz (default), + 6.5MHz, 7.6MHz, 12MHz, 13MHz, 15.2MHz, 19.2MHz, 24MHz, 26MHz, 38kHz. + $ref: "/schemas/types.yaml#/definitions/uint32" + enum: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + kt0913: fm-am-tuner@35 { + compatible = "ktm,kt0913"; + reg = <0x35>; + ktm,anti-pop = <0x01>; + ktm,refclk = <0x00>; + }; + }; +...
Signature · 1 line
2.24.1
Rob Herring
maintainerRe: [PATCH 3/3] media: kt0913: device tree binding
Quoted 8 lines · Santiago Hormazabal
Document bindings for the kt0913 AM/FM radio tuner.
Signed-off-by Santiago Hormazabal <[email protected]>--- .../bindings/media/i2c/ktm,kt0913.yaml | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/i2c/ktm,kt0913.yaml
My bot found errors running 'make dt_binding_check' on your patch:
./Documentation/devicetree/bindings/media/i2c/ktm,kt0913.yaml: $id: relative path/filename doesn't match actual path or filename
expected: http://devicetree.org/schemas/media/i2c/ktm,kt0913.yaml#
Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dts:22.17-30: Warning (reg_format): /example-0/i2c/fm-am-tuner@35:reg: property has invalid length (4 bytes) (#address-cells == 2, #size-cells == 1) Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dt.yaml: Warning (pci_device_reg): Failed prerequisite 'reg_format' Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dt.yaml: Warning (pci_device_bus_num): Failed prerequisite 'reg_format' Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dt.yaml: Warning (simple_bus_reg): Failed prerequisite 'reg_format' Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dts:19.13-26.11: Warning (i2c_bus_bridge): /example-0/i2c: incorrect #address-cells for I2C bus Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dts:19.13-26.11: Warning (i2c_bus_bridge): /example-0/i2c: incorrect #size-cells for I2C bus Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dt.yaml: Warning (i2c_bus_reg): Failed prerequisite 'reg_format' Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dt.yaml: Warning (i2c_bus_reg): Failed prerequisite 'i2c_bus_bridge' Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dt.yaml: Warning (spi_bus_reg): Failed prerequisite 'reg_format' Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dts:20.36-25.15: Warning (avoid_default_addr_size): /example-0/i2c/fm-am-tuner@35: Relying on default #address-cells value Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dts:20.36-25.15: Warning (avoid_default_addr_size): /example-0/i2c/fm-am-tuner@35: Relying on default #size-cells value Documentation/devicetree/bindings/media/i2c/ktm,kt0913.example.dt.yaml: Warning (unique_unit_address): Failed prerequisite 'avoid_default_addr_size'
See https://patchwork.ozlabs.org/patch/1354644
If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure dt-schema is up to date:
pip3 install git+https://github.com/devicetree-org/dt-schema.git@master --upgrade
Please check and re-submit.