How to write I2C Device Driver for i2c sensor? - linux

I want to write i2c device driver for i2c sensor, I'm totally confusing with
what is:
i2c client driver
what is i2c host driver
what is i2c adapter driver
what is algorithm driver
how char driver access i2c device information

Get some information regarding for I2C Here. And This Documentation may help you to program it.

Related

AM335x - i2c slave for linux kernel

I need to have i2c slave Linux kernel driver for TI AM335x.
I googled about and didn't find precise information.
Should I do everything from scratch, or maybe someone has some reference about it? or even a patch
Thanks
Avner
For new device which connected as slave to i2c bus, you should write neither "i2c driver" nor "driver for AM335x" (as far as the processor support already present in kernel).
i2c is a bus and there is kernel infrastructure for the bus, see documentation.
You should figure out what type your device is and then write driver for this type of device using i2c bus primitives.
For example, the driver for DS13xx and compatible IC is rtc driver.
A driver "for" PCF8574 i2c gpio expander can be GPIO driver as well as keypad driver.

Interrupt driven SPI or I2C driver

I am working on a user application, which depends on I2C or SPI device connected with the beaglebone board.
My app is running in userspace. Now whenever an interrupt occurs from I2c device, My application should get the data from I2c device.
My question is how to sync all these. Do I need to write a device driver which gets the data from I2c device or trigger another device driver to read the data from I2c and how it can trigger my user app to get the data?
You'll need to know about three things: i2c drivers, spi drivers and input subsystem.
For i2c drivers, search kernel code for struct i2c_driver and module_i2c_driver helper macro for registering and unregistering the driver.
Similarly, struct spi_driver and module_spi_driver for spi.
For input subsystem, search for struct input_dev. Among other things, you'll need to "hook" into interesting events by setting relevant event bits and implement input_event event handler.
UPDATE: Forgot to mention, aim of input subsystem is to help take care of input events coming in from i2c or spi device.

Bitbanging I2C with /dev/i2c-%d

Is it possible to control the data/clock pins of i2c for bitbanging with /dev/i2c-%d? if no, What other options are available for controling those pins of i2c that are connected to the machine? I've tried SMBus but according to SMBus Protocol Summary:
plain I2C commands can not behandled at all on most pure SMBus
adapters

uart communication in Linux kernel

I have a UART-based device, ESP8266EX, and I need to write to its driver for Linux. Since I am new to writing kernel drivers is there any example or method for accessing uart reading and writing through kernel driver. How is it different from user space uart code?
I have a small iot board with mu device connected to /dev/ttymxc1 port so the uart port will remain fixed for this device and also the device will work at fixed 115200 baud rate. I should be able to read and write to uart from kernel.

How to access i2c device driver node

Situation 1:
I have an i2c chip driver as part of linux kernel. I can verify the i2c chip driver is in the kernel from kernel boot messages (my chip driver is mma8450)
dmesg:
mma8450 0-001c: uevent
I can also see this driver in (0x1c is i2c address of chip)
cat /sys/bus/i2c/devices/0-001c/name
mma8450
I can not see this driver node in /dev interface. My question is how can I create node of this device in /dev so that I can access this device in a user program ?
Situation 2:
I create the module of the same chip driver and does not make it a part of kernel. I can load this module using insmod mma8450, how can I create a node of this device as I don't have its major / minor numbers ? (I can not see major & minor numbers assigned to this driver in mma8450 source code)
Any help is appreciated
Regards
Load the kernel module:
modprobe i2c-dev
ls /dev/i2*
/dev/i2c-0
/dev/i2c-10
/dev/i2c-12
/dev/i2c-14
/dev/i2c-3
/dev/i2c-5
/dev/i2c-7
/dev/i2c-9
/dev/i2c-1
/dev/i2c-11
/dev/i2c-13
/dev/i2c-2
/dev/i2c-4
/dev/i2c-6
/dev/i2c-8
Find the major/minor numbers for your device:
cat /proc/devices
You should see a device for the i2c bus and one for the i2c device itself.
Create the device node for the i2c device driver:
mknod /dev/[device name] [type] [major] [minor]
This is 3-Axis Accelerometer. Linux registers it as a driver for input_polled_dev type.
You can uaccess it using /dev/i2c-x bus (controller) device node, but there is no much sense using it that way directly from userspace.
I2C clients are not meant to be used using /dev device nodes.
They should be registered to Kernel I2C framework and used through higher layers API.
There is sample program for reading similar MMA7455L x,y,z registers from userspace using /dev/i2c-X bus device node.
Reading the Accelerometer With I²C

Resources