Gabriel Jacob Perin
← Back to blog

Linux Kernel Contribution 1

2026-04-21

This post describes my first Linux kernel patch submission. We are working on suggestion 2.1, Replace manual bitfield manipulations with FIELD_GET(), FIELD_PREP(), and FIELD_MODIFY(). The file we worked on was sound/soc/atmel/atmel-classd.c, which implements the Atmel Class D driver in the ALSA SoC subsystem.

The main goal of this cleanup was straightforward: identify places where the code manually prepares bitfields using patterns such as x << shift, and replace them with the kernel helper macros designed specifically for this purpose. In our case, the most common transformation was replacing manual left shifts with FIELD_PREP(mask, value). These helpers make the code clearer, reduce the chance of mistakes, and match the style encouraged in modern kernel code.

Enabling the driver for compilation

Before compiling the target file, I had to enable the corresponding kernel configuration option. For that, I opened the kernel configuration menu and enabled CONFIG_SND_ATMEL_SOC_CLASSD. On my setup, this option only became available after enabling COMPILE_TEST, which is commonly needed when building drivers for hardware that is not part of the current machine.

make menuconfig

After enabling the relevant options, I prepared the tree and compiled only the driver directory I was modifying:

make modules_prepare
 make V=1 M=sound/soc/atmel

This was enough to verify that the modified file still compiled correctly.

Example of the change

One representative example of the patch is shown below. The original code manually shifted the value according to the field position:

- val = pdata->pwm_type << CLASSD_MR_PWMTYP_SHIFT;
 + val = FIELD_PREP(CLASSD_MR_PWMTYP_MASK, pdata->pwm_type);

Conceptually, both lines are trying to place pdata->pwm_type into the correct bit range of the register value. The second version is preferable because it expresses the intent more directly: instead of saying “shift this value by some amount,” it says “prepare this value for this specific field mask.”

Validation and submission

After testing the patch and checking that it did not introduce any obvious formatting or compilation errors, we sent it to the CI infrastructure prepared by the course monitors. The patch passed CI, and after that we submitted it to the maintainers.

The response we received was the following, from Mark Brown:

Please submit patches using subject lines reflecting the style for the subsystem, this makes it easier for people to identify relevant patches. Look at what existing commits in the area you're changing are doing and make sure your subject lines visually resemble what they're doing. There's no need to resubmit to fix this alone.

This was useful feedback. The code change itself was fine, but the patch subject line did not match the style typically used in that subsystem. This is a good reminder that contributing to the kernel is not only about the technical correctness of the patch, but also about following the conventions that make review easier for maintainers and other developers.

Overall, this was a good first experience with a real kernel cleanup patch: understand the local coding pattern, make a targeted change, verify that it builds, send it through the expected workflow, and learn from the feedback that comes back.