can you show me a rudimentary hardware driver in linux?

@melta101 or @32bitSaviour can either of you show me a rudimentary hardware driver in linux? How much of it is config and how much of it is code?
41 Replies
melta101
melta101•2mo ago
Hardware driver as in?
melta101
melta101•2mo ago
And does userplane program won't work?
ZacckOsiemo
ZacckOsiemo•2mo ago
Show me how to add custom hardware to a embedded linux or even just a linux system
ZacckOsiemo
ZacckOsiemo•2mo ago
say a tof sensor over i2c
ZacckOsiemo
ZacckOsiemo•2mo ago
just an example
melta101
melta101•2mo ago
Yea, this is pretty straightforward
ZacckOsiemo
ZacckOsiemo•2mo ago
The reason I ask is because adding a camera to a pi is enabling i2c and adding an overlay for the camera
ZacckOsiemo
ZacckOsiemo•2mo ago
is that it?
ZacckOsiemo
ZacckOsiemo•2mo ago
what if it is a custom on, without drivers?
melta101
melta101•2mo ago
You could make a userplane program to control the driver
ZacckOsiemo
ZacckOsiemo•2mo ago
what is a userplane program?
melta101
melta101•2mo ago
Much more easier than touching kernel side
ZacckOsiemo
ZacckOsiemo•2mo ago
is what I am doing, touching the kernel
ZacckOsiemo
ZacckOsiemo•2mo ago
is this what loading drivers is?
melta101
melta101•2mo ago
Linux system are divided into 2 part Userplane Kernel plane
melta101
melta101•2mo ago
Yep Drivers mostly come into kernel plane
melta101
melta101•2mo ago
You load it into the kernel
melta101
melta101•2mo ago
Which comm does yours use?
ZacckOsiemo
ZacckOsiemo•2mo ago
what if the hardware I am using does not have drivers and new ones have to be built
ZacckOsiemo
ZacckOsiemo•2mo ago
how does that work
ZacckOsiemo
ZacckOsiemo•2mo ago
I2c, I enabled it in config. Then I added an overlay for the camera in the boot config
melta101
melta101•2mo ago
Oh wait, Is this for the camera or the another sensor?
ZacckOsiemo
ZacckOsiemo•2mo ago
the issue here is not that something is not working. It's that I want to know how it work 🙂
ZacckOsiemo
ZacckOsiemo•2mo ago
So basically lets consider a custom sensor without existing drivers
32bitSaviour
32bitSaviour•2mo ago
For this I would make a device tree definition of the hardware. Write the driver for it.
ZacckOsiemo
ZacckOsiemo•2mo ago
How does this process work in linux land? In firmware - I build comms drivers (i2c) - then over the comms drivers I talk to registers on the sensor
can you show me a sample implementation ? Even a trivial one?
32bitSaviour
32bitSaviour•2mo ago
You would have to work on top of the existing i2c drivers.
32bitSaviour
32bitSaviour•2mo ago
32bitSaviour
32bitSaviour•2mo ago
Depending on the hardware type you may need to recompile the kernel with the driver or have it as a loadable kernel module. For example, hardware that is woken up at early boot need to be recompiled with the kernel.
ZacckOsiemo
ZacckOsiemo•2mo ago
Oh my word, this is nice!
ZacckOsiemo
ZacckOsiemo•2mo ago
this is luxury, so much ground work is done!
melta101
melta101•2mo ago
the site is also very good
melta101
melta101•2mo ago
learnt a lot from there
ZacckOsiemo
ZacckOsiemo•2mo ago
thankfully I have all of you to learn from
32bitSaviour
32bitSaviour•2mo ago
Zephyr followed this model somewhat.
ZacckOsiemo
ZacckOsiemo•2mo ago
ah so if I add a custome driver to zephyr I can transfer than knowledge to linux?
32bitSaviour
32bitSaviour•2mo ago
Yeah sir. Except it doesn't map 1:1
melta101
melta101•2mo ago
its has a similar driver modela and dtc usage
ZacckOsiemo
ZacckOsiemo•2mo ago
nothing maps 1:1 in life
ZacckOsiemo
ZacckOsiemo•2mo ago
amazing lessons from the both of you @32bitSaviour @melta101 thank you.
Ming
Ming•2mo ago
You have only chosen one of the most complex type of driver out there if you were wanting to write your own.... For camera drivers from scratch, you would need to: - Look at the bus subsystem o talk to the camera - Write the interfaces to talk to the camera - Look at the Video4Linux2 subsystem so that it can be picked up and used by most of the userspace applications - Interface between the camera & V4L2 (this normally involves lots of IOCTLs!) Here is the driver for the imx219 as an example https://elixir.bootlin.com/linux/v6.12.6/source/drivers/media/i2c/imx219.c To contrast with that, here is a simple GPIO LED driver:
imx219.c - drivers/media/i2c/imx219.c - Linux source...
Elixir Cross Referencer - source code of Linux v6.12.6: drivers/media/i2c/imx219.c

Did you find this page helpful?