Blame Documentation/i2c/writing-clients

Packit 7b02f3
This is a small guide for those who want to write kernel drivers for I2C
Packit 7b02f3
or SMBus devices, using Linux as the protocol host/master (not slave).
Packit 7b02f3
Packit 7b02f3
To set up a driver, you need to do several things. Some are optional, and
Packit 7b02f3
some things can be done slightly or completely different. Use this as a
Packit 7b02f3
guide, not as a rule book!
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
General remarks
Packit 7b02f3
===============
Packit 7b02f3
Packit 7b02f3
Try to keep the kernel namespace as clean as possible. The best way to
Packit 7b02f3
do this is to use a unique prefix for all global symbols. This is
Packit 7b02f3
especially important for exported symbols, but it is a good idea to do
Packit 7b02f3
it for non-exported symbols too. We will use the prefix `foo_' in this
Packit 7b02f3
tutorial.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
The driver structure
Packit 7b02f3
====================
Packit 7b02f3
Packit 7b02f3
Usually, you will implement a single driver structure, and instantiate
Packit 7b02f3
all clients from it. Remember, a driver structure contains general access
Packit 7b02f3
routines, and should be zero-initialized except for fields with data you
Packit 7b02f3
provide.  A client structure holds device-specific information like the
Packit 7b02f3
driver model device node, and its I2C address.
Packit 7b02f3
Packit 7b02f3
static struct i2c_device_id foo_idtable[] = {
Packit 7b02f3
	{ "foo", my_id_for_foo },
Packit 7b02f3
	{ "bar", my_id_for_bar },
Packit 7b02f3
	{ }
Packit 7b02f3
};
Packit 7b02f3
Packit 7b02f3
MODULE_DEVICE_TABLE(i2c, foo_idtable);
Packit 7b02f3
Packit 7b02f3
static struct i2c_driver foo_driver = {
Packit 7b02f3
	.driver = {
Packit 7b02f3
		.name	= "foo",
Packit 7b02f3
		.pm	= &foo_pm_ops,	/* optional */
Packit 7b02f3
	},
Packit 7b02f3
Packit 7b02f3
	.id_table	= foo_idtable,
Packit 7b02f3
	.probe		= foo_probe,
Packit 7b02f3
	.remove		= foo_remove,
Packit 7b02f3
	/* if device autodetection is needed: */
Packit 7b02f3
	.class		= I2C_CLASS_SOMETHING,
Packit 7b02f3
	.detect		= foo_detect,
Packit 7b02f3
	.address_list	= normal_i2c,
Packit 7b02f3
Packit 7b02f3
	.shutdown	= foo_shutdown,	/* optional */
Packit 7b02f3
	.command	= foo_command,	/* optional, deprecated */
Packit 7b02f3
}
Packit 7b02f3
Packit 7b02f3
The name field is the driver name, and must not contain spaces.  It
Packit 7b02f3
should match the module name (if the driver can be compiled as a module),
Packit 7b02f3
although you can use MODULE_ALIAS (passing "foo" in this example) to add
Packit 7b02f3
another name for the module.  If the driver name doesn't match the module
Packit 7b02f3
name, the module won't be automatically loaded (hotplug/coldplug).
Packit 7b02f3
Packit 7b02f3
All other fields are for call-back functions which will be explained
Packit 7b02f3
below.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Extra client data
Packit 7b02f3
=================
Packit 7b02f3
Packit 7b02f3
Each client structure has a special `data' field that can point to any
Packit 7b02f3
structure at all.  You should use this to keep device-specific data.
Packit 7b02f3
Packit 7b02f3
	/* store the value */
Packit 7b02f3
	void i2c_set_clientdata(struct i2c_client *client, void *data);
Packit 7b02f3
Packit 7b02f3
	/* retrieve the value */
Packit 7b02f3
	void *i2c_get_clientdata(const struct i2c_client *client);
Packit 7b02f3
Packit 7b02f3
Note that starting with kernel 2.6.34, you don't have to set the `data' field
Packit 7b02f3
to NULL in remove() or if probe() failed anymore. The i2c-core does this
Packit 7b02f3
automatically on these occasions. Those are also the only times the core will
Packit 7b02f3
touch this field.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Accessing the client
Packit 7b02f3
====================
Packit 7b02f3
Packit 7b02f3
Let's say we have a valid client structure. At some time, we will need
Packit 7b02f3
to gather information from the client, or write new information to the
Packit 7b02f3
client.
Packit 7b02f3
Packit 7b02f3
I have found it useful to define foo_read and foo_write functions for this.
Packit 7b02f3
For some cases, it will be easier to call the i2c functions directly,
Packit 7b02f3
but many chips have some kind of register-value idea that can easily
Packit 7b02f3
be encapsulated.
Packit 7b02f3
Packit 7b02f3
The below functions are simple examples, and should not be copied
Packit 7b02f3
literally.
Packit 7b02f3
Packit 7b02f3
int foo_read_value(struct i2c_client *client, u8 reg)
Packit 7b02f3
{
Packit 7b02f3
	if (reg < 0x10)	/* byte-sized register */
Packit 7b02f3
		return i2c_smbus_read_byte_data(client, reg);
Packit 7b02f3
	else		/* word-sized register */
Packit 7b02f3
		return i2c_smbus_read_word_data(client, reg);
Packit 7b02f3
}
Packit 7b02f3
Packit 7b02f3
int foo_write_value(struct i2c_client *client, u8 reg, u16 value)
Packit 7b02f3
{
Packit 7b02f3
	if (reg == 0x10)	/* Impossible to write - driver error! */
Packit 7b02f3
		return -EINVAL;
Packit 7b02f3
	else if (reg < 0x10)	/* byte-sized register */
Packit 7b02f3
		return i2c_smbus_write_byte_data(client, reg, value);
Packit 7b02f3
	else			/* word-sized register */
Packit 7b02f3
		return i2c_smbus_write_word_data(client, reg, value);
Packit 7b02f3
}
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Probing and attaching
Packit 7b02f3
=====================
Packit 7b02f3
Packit 7b02f3
The Linux I2C stack was originally written to support access to hardware
Packit 7b02f3
monitoring chips on PC motherboards, and thus used to embed some assumptions
Packit 7b02f3
that were more appropriate to SMBus (and PCs) than to I2C.  One of these
Packit 7b02f3
assumptions was that most adapters and devices drivers support the SMBUS_QUICK
Packit 7b02f3
protocol to probe device presence.  Another was that devices and their drivers
Packit 7b02f3
can be sufficiently configured using only such probe primitives.
Packit 7b02f3
Packit 7b02f3
As Linux and its I2C stack became more widely used in embedded systems
Packit 7b02f3
and complex components such as DVB adapters, those assumptions became more
Packit 7b02f3
problematic.  Drivers for I2C devices that issue interrupts need more (and
Packit 7b02f3
different) configuration information, as do drivers handling chip variants
Packit 7b02f3
that can't be distinguished by protocol probing, or which need some board
Packit 7b02f3
specific information to operate correctly.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Device/Driver Binding
Packit 7b02f3
---------------------
Packit 7b02f3
Packit 7b02f3
System infrastructure, typically board-specific initialization code or
Packit 7b02f3
boot firmware, reports what I2C devices exist.  For example, there may be
Packit 7b02f3
a table, in the kernel or from the boot loader, identifying I2C devices
Packit 7b02f3
and linking them to board-specific configuration information about IRQs
Packit 7b02f3
and other wiring artifacts, chip type, and so on.  That could be used to
Packit 7b02f3
create i2c_client objects for each I2C device.
Packit 7b02f3
Packit 7b02f3
I2C device drivers using this binding model work just like any other
Packit 7b02f3
kind of driver in Linux:  they provide a probe() method to bind to
Packit 7b02f3
those devices, and a remove() method to unbind.
Packit 7b02f3
Packit 7b02f3
	static int foo_probe(struct i2c_client *client,
Packit 7b02f3
			     const struct i2c_device_id *id);
Packit 7b02f3
	static int foo_remove(struct i2c_client *client);
Packit 7b02f3
Packit 7b02f3
Remember that the i2c_driver does not create those client handles.  The
Packit 7b02f3
handle may be used during foo_probe().  If foo_probe() reports success
Packit 7b02f3
(zero not a negative status code) it may save the handle and use it until
Packit 7b02f3
foo_remove() returns.  That binding model is used by most Linux drivers.
Packit 7b02f3
Packit 7b02f3
The probe function is called when an entry in the id_table name field
Packit 7b02f3
matches the device's name. It is passed the entry that was matched so
Packit 7b02f3
the driver knows which one in the table matched.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Device Creation
Packit 7b02f3
---------------
Packit 7b02f3
Packit 7b02f3
If you know for a fact that an I2C device is connected to a given I2C bus,
Packit 7b02f3
you can instantiate that device by simply filling an i2c_board_info
Packit 7b02f3
structure with the device address and driver name, and calling
Packit 7b02f3
i2c_new_device().  This will create the device, then the driver core will
Packit 7b02f3
take care of finding the right driver and will call its probe() method.
Packit 7b02f3
If a driver supports different device types, you can specify the type you
Packit 7b02f3
want using the type field.  You can also specify an IRQ and platform data
Packit 7b02f3
if needed.
Packit 7b02f3
Packit 7b02f3
Sometimes you know that a device is connected to a given I2C bus, but you
Packit 7b02f3
don't know the exact address it uses.  This happens on TV adapters for
Packit 7b02f3
example, where the same driver supports dozens of slightly different
Packit 7b02f3
models, and I2C device addresses change from one model to the next.  In
Packit 7b02f3
that case, you can use the i2c_new_probed_device() variant, which is
Packit 7b02f3
similar to i2c_new_device(), except that it takes an additional list of
Packit 7b02f3
possible I2C addresses to probe.  A device is created for the first
Packit 7b02f3
responsive address in the list.  If you expect more than one device to be
Packit 7b02f3
present in the address range, simply call i2c_new_probed_device() that
Packit 7b02f3
many times.
Packit 7b02f3
Packit 7b02f3
The call to i2c_new_device() or i2c_new_probed_device() typically happens
Packit 7b02f3
in the I2C bus driver. You may want to save the returned i2c_client
Packit 7b02f3
reference for later use.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Device Detection
Packit 7b02f3
----------------
Packit 7b02f3
Packit 7b02f3
Sometimes you do not know in advance which I2C devices are connected to
Packit 7b02f3
a given I2C bus.  This is for example the case of hardware monitoring
Packit 7b02f3
devices on a PC's SMBus.  In that case, you may want to let your driver
Packit 7b02f3
detect supported devices automatically.  This is how the legacy model
Packit 7b02f3
was working, and is now available as an extension to the standard
Packit 7b02f3
driver model.
Packit 7b02f3
Packit 7b02f3
You simply have to define a detect callback which will attempt to
Packit 7b02f3
identify supported devices (returning 0 for supported ones and -ENODEV
Packit 7b02f3
for unsupported ones), a list of addresses to probe, and a device type
Packit 7b02f3
(or class) so that only I2C buses which may have that type of device
Packit 7b02f3
connected (and not otherwise enumerated) will be probed.  For example,
Packit 7b02f3
a driver for a hardware monitoring chip for which auto-detection is
Packit 7b02f3
needed would set its class to I2C_CLASS_HWMON, and only I2C adapters
Packit 7b02f3
with a class including I2C_CLASS_HWMON would be probed by this driver.
Packit 7b02f3
Note that the absence of matching classes does not prevent the use of
Packit 7b02f3
a device of that type on the given I2C adapter.  All it prevents is
Packit 7b02f3
auto-detection; explicit instantiation of devices is still possible.
Packit 7b02f3
Packit 7b02f3
Note that this mechanism is purely optional and not suitable for all
Packit 7b02f3
devices.  You need some reliable way to identify the supported devices
Packit 7b02f3
(typically using device-specific, dedicated identification registers),
Packit 7b02f3
otherwise misdetections are likely to occur and things can get wrong
Packit 7b02f3
quickly.  Keep in mind that the I2C protocol doesn't include any
Packit 7b02f3
standard way to detect the presence of a chip at a given address, let
Packit 7b02f3
alone a standard way to identify devices.  Even worse is the lack of
Packit 7b02f3
semantics associated to bus transfers, which means that the same
Packit 7b02f3
transfer can be seen as a read operation by a chip and as a write
Packit 7b02f3
operation by another chip.  For these reasons, explicit device
Packit 7b02f3
instantiation should always be preferred to auto-detection where
Packit 7b02f3
possible.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Device Deletion
Packit 7b02f3
---------------
Packit 7b02f3
Packit 7b02f3
Each I2C device which has been created using i2c_new_device() or
Packit 7b02f3
i2c_new_probed_device() can be unregistered by calling
Packit 7b02f3
i2c_unregister_device().  If you don't call it explicitly, it will be
Packit 7b02f3
called automatically before the underlying I2C bus itself is removed, as a
Packit 7b02f3
device can't survive its parent in the device driver model.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Initializing the driver
Packit 7b02f3
=======================
Packit 7b02f3
Packit 7b02f3
When the kernel is booted, or when your foo driver module is inserted,
Packit 7b02f3
you have to do some initializing. Fortunately, just registering the
Packit 7b02f3
driver module is usually enough.
Packit 7b02f3
Packit 7b02f3
static int __init foo_init(void)
Packit 7b02f3
{
Packit 7b02f3
	return i2c_add_driver(&foo_driver);
Packit 7b02f3
}
Packit 7b02f3
module_init(foo_init);
Packit 7b02f3
Packit 7b02f3
static void __exit foo_cleanup(void)
Packit 7b02f3
{
Packit 7b02f3
	i2c_del_driver(&foo_driver);
Packit 7b02f3
}
Packit 7b02f3
module_exit(foo_cleanup);
Packit 7b02f3
Packit 7b02f3
The module_i2c_driver() macro can be used to reduce above code.
Packit 7b02f3
Packit 7b02f3
module_i2c_driver(foo_driver);
Packit 7b02f3
Packit 7b02f3
Note that some functions are marked by `__init'.  These functions can
Packit 7b02f3
be removed after kernel booting (or module loading) is completed.
Packit 7b02f3
Likewise, functions marked by `__exit' are dropped by the compiler when
Packit 7b02f3
the code is built into the kernel, as they would never be called.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Driver Information
Packit 7b02f3
==================
Packit 7b02f3
Packit 7b02f3
/* Substitute your own name and email address */
Packit 7b02f3
MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"
Packit 7b02f3
MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices");
Packit 7b02f3
Packit 7b02f3
/* a few non-GPL license types are also allowed */
Packit 7b02f3
MODULE_LICENSE("GPL");
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Power Management
Packit 7b02f3
================
Packit 7b02f3
Packit 7b02f3
If your I2C device needs special handling when entering a system low
Packit 7b02f3
power state -- like putting a transceiver into a low power mode, or
Packit 7b02f3
activating a system wakeup mechanism -- do that by implementing the
Packit 7b02f3
appropriate callbacks for the dev_pm_ops of the driver (like suspend
Packit 7b02f3
and resume).
Packit 7b02f3
Packit 7b02f3
These are standard driver model calls, and they work just like they
Packit 7b02f3
would for any other driver stack.  The calls can sleep, and can use
Packit 7b02f3
I2C messaging to the device being suspended or resumed (since their
Packit 7b02f3
parent I2C adapter is active when these calls are issued, and IRQs
Packit 7b02f3
are still enabled).
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
System Shutdown
Packit 7b02f3
===============
Packit 7b02f3
Packit 7b02f3
If your I2C device needs special handling when the system shuts down
Packit 7b02f3
or reboots (including kexec) -- like turning something off -- use a
Packit 7b02f3
shutdown() method.
Packit 7b02f3
Packit 7b02f3
Again, this is a standard driver model call, working just like it
Packit 7b02f3
would for any other driver stack:  the calls can sleep, and can use
Packit 7b02f3
I2C messaging.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Command function
Packit 7b02f3
================
Packit 7b02f3
Packit 7b02f3
A generic ioctl-like function call back is supported. You will seldom
Packit 7b02f3
need this, and its use is deprecated anyway, so newer design should not
Packit 7b02f3
use it.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Sending and receiving
Packit 7b02f3
=====================
Packit 7b02f3
Packit 7b02f3
If you want to communicate with your device, there are several functions
Packit 7b02f3
to do this. You can find all of them in <linux/i2c.h>.
Packit 7b02f3
Packit 7b02f3
If you can choose between plain I2C communication and SMBus level
Packit 7b02f3
communication, please use the latter. All adapters understand SMBus level
Packit 7b02f3
commands, but only some of them understand plain I2C!
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
Plain I2C communication
Packit 7b02f3
-----------------------
Packit 7b02f3
Packit 7b02f3
	int i2c_master_send(struct i2c_client *client, const char *buf,
Packit 7b02f3
			    int count);
Packit 7b02f3
	int i2c_master_recv(struct i2c_client *client, char *buf, int count);
Packit 7b02f3
Packit 7b02f3
These routines read and write some bytes from/to a client. The client
Packit 7b02f3
contains the i2c address, so you do not have to include it. The second
Packit 7b02f3
parameter contains the bytes to read/write, the third the number of bytes
Packit 7b02f3
to read/write (must be less than the length of the buffer, also should be
Packit 7b02f3
less than 64k since msg.len is u16.) Returned is the actual number of bytes
Packit 7b02f3
read/written.
Packit 7b02f3
Packit 7b02f3
	int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,
Packit 7b02f3
			 int num);
Packit 7b02f3
Packit 7b02f3
This sends a series of messages. Each message can be a read or write,
Packit 7b02f3
and they can be mixed in any way. The transactions are combined: no
Packit 7b02f3
stop bit is sent between transaction. The i2c_msg structure contains
Packit 7b02f3
for each message the client address, the number of bytes of the message
Packit 7b02f3
and the message data itself.
Packit 7b02f3
Packit 7b02f3
You can read the file `i2c-protocol' for more information about the
Packit 7b02f3
actual I2C protocol.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
SMBus communication
Packit 7b02f3
-------------------
Packit 7b02f3
Packit 7b02f3
	s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
Packit 7b02f3
			   unsigned short flags, char read_write, u8 command,
Packit 7b02f3
			   int size, union i2c_smbus_data *data);
Packit 7b02f3
Packit 7b02f3
This is the generic SMBus function. All functions below are implemented
Packit 7b02f3
in terms of it. Never use this function directly!
Packit 7b02f3
Packit 7b02f3
	s32 i2c_smbus_read_byte(struct i2c_client *client);
Packit 7b02f3
	s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value);
Packit 7b02f3
	s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command);
Packit 7b02f3
	s32 i2c_smbus_write_byte_data(struct i2c_client *client,
Packit 7b02f3
				      u8 command, u8 value);
Packit 7b02f3
	s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command);
Packit 7b02f3
	s32 i2c_smbus_write_word_data(struct i2c_client *client,
Packit 7b02f3
				      u8 command, u16 value);
Packit 7b02f3
	s32 i2c_smbus_read_block_data(struct i2c_client *client,
Packit 7b02f3
				      u8 command, u8 *values);
Packit 7b02f3
	s32 i2c_smbus_write_block_data(struct i2c_client *client,
Packit 7b02f3
				       u8 command, u8 length, const u8 *values);
Packit 7b02f3
	s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client,
Packit 7b02f3
					  u8 command, u8 length, u8 *values);
Packit 7b02f3
	s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client,
Packit 7b02f3
					   u8 command, u8 length,
Packit 7b02f3
					   const u8 *values);
Packit 7b02f3
Packit 7b02f3
These ones were removed from i2c-core because they had no users, but could
Packit 7b02f3
be added back later if needed:
Packit 7b02f3
Packit 7b02f3
	s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value);
Packit 7b02f3
	s32 i2c_smbus_process_call(struct i2c_client *client,
Packit 7b02f3
				   u8 command, u16 value);
Packit 7b02f3
	s32 i2c_smbus_block_process_call(struct i2c_client *client,
Packit 7b02f3
					 u8 command, u8 length, u8 *values);
Packit 7b02f3
Packit 7b02f3
All these transactions return a negative errno value on failure. The 'write'
Packit 7b02f3
transactions return 0 on success; the 'read' transactions return the read
Packit 7b02f3
value, except for block transactions, which return the number of values
Packit 7b02f3
read. The block buffers need not be longer than 32 bytes.
Packit 7b02f3
Packit 7b02f3
You can read the file `smbus-protocol' for more information about the
Packit 7b02f3
actual SMBus protocol.
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
General purpose routines
Packit 7b02f3
========================
Packit 7b02f3
Packit 7b02f3
Below all general purpose routines are listed, that were not mentioned
Packit 7b02f3
before.
Packit 7b02f3
Packit 7b02f3
	/* Return the adapter number for a specific adapter */
Packit 7b02f3
	int i2c_adapter_id(struct i2c_adapter *adap);