162306a36Sopenharmony_ci=============================== 262306a36Sopenharmony_ciImplementing I2C device drivers 362306a36Sopenharmony_ci=============================== 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciThis is a small guide for those who want to write kernel drivers for I2C 662306a36Sopenharmony_cior SMBus devices, using Linux as the protocol host/master (not slave). 762306a36Sopenharmony_ci 862306a36Sopenharmony_ciTo set up a driver, you need to do several things. Some are optional, and 962306a36Sopenharmony_cisome things can be done slightly or completely different. Use this as a 1062306a36Sopenharmony_ciguide, not as a rule book! 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ciGeneral remarks 1462306a36Sopenharmony_ci=============== 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ciTry to keep the kernel namespace as clean as possible. The best way to 1762306a36Sopenharmony_cido this is to use a unique prefix for all global symbols. This is 1862306a36Sopenharmony_ciespecially important for exported symbols, but it is a good idea to do 1962306a36Sopenharmony_ciit for non-exported symbols too. We will use the prefix ``foo_`` in this 2062306a36Sopenharmony_citutorial. 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ciThe driver structure 2462306a36Sopenharmony_ci==================== 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ciUsually, you will implement a single driver structure, and instantiate 2762306a36Sopenharmony_ciall clients from it. Remember, a driver structure contains general access 2862306a36Sopenharmony_ciroutines, and should be zero-initialized except for fields with data you 2962306a36Sopenharmony_ciprovide. A client structure holds device-specific information like the 3062306a36Sopenharmony_cidriver model device node, and its I2C address. 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci:: 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci static struct i2c_device_id foo_idtable[] = { 3562306a36Sopenharmony_ci { "foo", my_id_for_foo }, 3662306a36Sopenharmony_ci { "bar", my_id_for_bar }, 3762306a36Sopenharmony_ci { } 3862306a36Sopenharmony_ci }; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci MODULE_DEVICE_TABLE(i2c, foo_idtable); 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci static struct i2c_driver foo_driver = { 4362306a36Sopenharmony_ci .driver = { 4462306a36Sopenharmony_ci .name = "foo", 4562306a36Sopenharmony_ci .pm = &foo_pm_ops, /* optional */ 4662306a36Sopenharmony_ci }, 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci .id_table = foo_idtable, 4962306a36Sopenharmony_ci .probe = foo_probe, 5062306a36Sopenharmony_ci .remove = foo_remove, 5162306a36Sopenharmony_ci /* if device autodetection is needed: */ 5262306a36Sopenharmony_ci .class = I2C_CLASS_SOMETHING, 5362306a36Sopenharmony_ci .detect = foo_detect, 5462306a36Sopenharmony_ci .address_list = normal_i2c, 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci .shutdown = foo_shutdown, /* optional */ 5762306a36Sopenharmony_ci .command = foo_command, /* optional, deprecated */ 5862306a36Sopenharmony_ci } 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ciThe name field is the driver name, and must not contain spaces. It 6162306a36Sopenharmony_cishould match the module name (if the driver can be compiled as a module), 6262306a36Sopenharmony_cialthough you can use MODULE_ALIAS (passing "foo" in this example) to add 6362306a36Sopenharmony_cianother name for the module. If the driver name doesn't match the module 6462306a36Sopenharmony_ciname, the module won't be automatically loaded (hotplug/coldplug). 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ciAll other fields are for call-back functions which will be explained 6762306a36Sopenharmony_cibelow. 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ciExtra client data 7162306a36Sopenharmony_ci================= 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ciEach client structure has a special ``data`` field that can point to any 7462306a36Sopenharmony_cistructure at all. You should use this to keep device-specific data. 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci:: 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci /* store the value */ 7962306a36Sopenharmony_ci void i2c_set_clientdata(struct i2c_client *client, void *data); 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci /* retrieve the value */ 8262306a36Sopenharmony_ci void *i2c_get_clientdata(const struct i2c_client *client); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ciNote that starting with kernel 2.6.34, you don't have to set the ``data`` field 8562306a36Sopenharmony_cito NULL in remove() or if probe() failed anymore. The i2c-core does this 8662306a36Sopenharmony_ciautomatically on these occasions. Those are also the only times the core will 8762306a36Sopenharmony_citouch this field. 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ciAccessing the client 9162306a36Sopenharmony_ci==================== 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ciLet's say we have a valid client structure. At some time, we will need 9462306a36Sopenharmony_cito gather information from the client, or write new information to the 9562306a36Sopenharmony_ciclient. 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ciI have found it useful to define foo_read and foo_write functions for this. 9862306a36Sopenharmony_ciFor some cases, it will be easier to call the I2C functions directly, 9962306a36Sopenharmony_cibut many chips have some kind of register-value idea that can easily 10062306a36Sopenharmony_cibe encapsulated. 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ciThe below functions are simple examples, and should not be copied 10362306a36Sopenharmony_ciliterally:: 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci int foo_read_value(struct i2c_client *client, u8 reg) 10662306a36Sopenharmony_ci { 10762306a36Sopenharmony_ci if (reg < 0x10) /* byte-sized register */ 10862306a36Sopenharmony_ci return i2c_smbus_read_byte_data(client, reg); 10962306a36Sopenharmony_ci else /* word-sized register */ 11062306a36Sopenharmony_ci return i2c_smbus_read_word_data(client, reg); 11162306a36Sopenharmony_ci } 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci int foo_write_value(struct i2c_client *client, u8 reg, u16 value) 11462306a36Sopenharmony_ci { 11562306a36Sopenharmony_ci if (reg == 0x10) /* Impossible to write - driver error! */ 11662306a36Sopenharmony_ci return -EINVAL; 11762306a36Sopenharmony_ci else if (reg < 0x10) /* byte-sized register */ 11862306a36Sopenharmony_ci return i2c_smbus_write_byte_data(client, reg, value); 11962306a36Sopenharmony_ci else /* word-sized register */ 12062306a36Sopenharmony_ci return i2c_smbus_write_word_data(client, reg, value); 12162306a36Sopenharmony_ci } 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ciProbing and attaching 12562306a36Sopenharmony_ci===================== 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ciThe Linux I2C stack was originally written to support access to hardware 12862306a36Sopenharmony_cimonitoring chips on PC motherboards, and thus used to embed some assumptions 12962306a36Sopenharmony_cithat were more appropriate to SMBus (and PCs) than to I2C. One of these 13062306a36Sopenharmony_ciassumptions was that most adapters and devices drivers support the SMBUS_QUICK 13162306a36Sopenharmony_ciprotocol to probe device presence. Another was that devices and their drivers 13262306a36Sopenharmony_cican be sufficiently configured using only such probe primitives. 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ciAs Linux and its I2C stack became more widely used in embedded systems 13562306a36Sopenharmony_ciand complex components such as DVB adapters, those assumptions became more 13662306a36Sopenharmony_ciproblematic. Drivers for I2C devices that issue interrupts need more (and 13762306a36Sopenharmony_cidifferent) configuration information, as do drivers handling chip variants 13862306a36Sopenharmony_cithat can't be distinguished by protocol probing, or which need some board 13962306a36Sopenharmony_cispecific information to operate correctly. 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ciDevice/Driver Binding 14362306a36Sopenharmony_ci--------------------- 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciSystem infrastructure, typically board-specific initialization code or 14662306a36Sopenharmony_ciboot firmware, reports what I2C devices exist. For example, there may be 14762306a36Sopenharmony_cia table, in the kernel or from the boot loader, identifying I2C devices 14862306a36Sopenharmony_ciand linking them to board-specific configuration information about IRQs 14962306a36Sopenharmony_ciand other wiring artifacts, chip type, and so on. That could be used to 15062306a36Sopenharmony_cicreate i2c_client objects for each I2C device. 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ciI2C device drivers using this binding model work just like any other 15362306a36Sopenharmony_cikind of driver in Linux: they provide a probe() method to bind to 15462306a36Sopenharmony_cithose devices, and a remove() method to unbind. 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci:: 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci static int foo_probe(struct i2c_client *client); 15962306a36Sopenharmony_ci static void foo_remove(struct i2c_client *client); 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ciRemember that the i2c_driver does not create those client handles. The 16262306a36Sopenharmony_cihandle may be used during foo_probe(). If foo_probe() reports success 16362306a36Sopenharmony_ci(zero not a negative status code) it may save the handle and use it until 16462306a36Sopenharmony_cifoo_remove() returns. That binding model is used by most Linux drivers. 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ciThe probe function is called when an entry in the id_table name field 16762306a36Sopenharmony_cimatches the device's name. If the probe function needs that entry, it 16862306a36Sopenharmony_cican retrieve it using 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci:: 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci const struct i2c_device_id *id = i2c_match_id(foo_idtable, client); 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ciDevice Creation 17662306a36Sopenharmony_ci--------------- 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ciIf you know for a fact that an I2C device is connected to a given I2C bus, 17962306a36Sopenharmony_ciyou can instantiate that device by simply filling an i2c_board_info 18062306a36Sopenharmony_cistructure with the device address and driver name, and calling 18162306a36Sopenharmony_cii2c_new_client_device(). This will create the device, then the driver core 18262306a36Sopenharmony_ciwill take care of finding the right driver and will call its probe() method. 18362306a36Sopenharmony_ciIf a driver supports different device types, you can specify the type you 18462306a36Sopenharmony_ciwant using the type field. You can also specify an IRQ and platform data 18562306a36Sopenharmony_ciif needed. 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ciSometimes you know that a device is connected to a given I2C bus, but you 18862306a36Sopenharmony_cidon't know the exact address it uses. This happens on TV adapters for 18962306a36Sopenharmony_ciexample, where the same driver supports dozens of slightly different 19062306a36Sopenharmony_cimodels, and I2C device addresses change from one model to the next. In 19162306a36Sopenharmony_cithat case, you can use the i2c_new_scanned_device() variant, which is 19262306a36Sopenharmony_cisimilar to i2c_new_client_device(), except that it takes an additional list 19362306a36Sopenharmony_ciof possible I2C addresses to probe. A device is created for the first 19462306a36Sopenharmony_ciresponsive address in the list. If you expect more than one device to be 19562306a36Sopenharmony_cipresent in the address range, simply call i2c_new_scanned_device() that 19662306a36Sopenharmony_cimany times. 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ciThe call to i2c_new_client_device() or i2c_new_scanned_device() typically 19962306a36Sopenharmony_cihappens in the I2C bus driver. You may want to save the returned i2c_client 20062306a36Sopenharmony_cireference for later use. 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ciDevice Detection 20462306a36Sopenharmony_ci---------------- 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ciSometimes you do not know in advance which I2C devices are connected to 20762306a36Sopenharmony_cia given I2C bus. This is for example the case of hardware monitoring 20862306a36Sopenharmony_cidevices on a PC's SMBus. In that case, you may want to let your driver 20962306a36Sopenharmony_cidetect supported devices automatically. This is how the legacy model 21062306a36Sopenharmony_ciwas working, and is now available as an extension to the standard 21162306a36Sopenharmony_cidriver model. 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ciYou simply have to define a detect callback which will attempt to 21462306a36Sopenharmony_ciidentify supported devices (returning 0 for supported ones and -ENODEV 21562306a36Sopenharmony_cifor unsupported ones), a list of addresses to probe, and a device type 21662306a36Sopenharmony_ci(or class) so that only I2C buses which may have that type of device 21762306a36Sopenharmony_ciconnected (and not otherwise enumerated) will be probed. For example, 21862306a36Sopenharmony_cia driver for a hardware monitoring chip for which auto-detection is 21962306a36Sopenharmony_cineeded would set its class to I2C_CLASS_HWMON, and only I2C adapters 22062306a36Sopenharmony_ciwith a class including I2C_CLASS_HWMON would be probed by this driver. 22162306a36Sopenharmony_ciNote that the absence of matching classes does not prevent the use of 22262306a36Sopenharmony_cia device of that type on the given I2C adapter. All it prevents is 22362306a36Sopenharmony_ciauto-detection; explicit instantiation of devices is still possible. 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ciNote that this mechanism is purely optional and not suitable for all 22662306a36Sopenharmony_cidevices. You need some reliable way to identify the supported devices 22762306a36Sopenharmony_ci(typically using device-specific, dedicated identification registers), 22862306a36Sopenharmony_ciotherwise misdetections are likely to occur and things can get wrong 22962306a36Sopenharmony_ciquickly. Keep in mind that the I2C protocol doesn't include any 23062306a36Sopenharmony_cistandard way to detect the presence of a chip at a given address, let 23162306a36Sopenharmony_cialone a standard way to identify devices. Even worse is the lack of 23262306a36Sopenharmony_cisemantics associated to bus transfers, which means that the same 23362306a36Sopenharmony_citransfer can be seen as a read operation by a chip and as a write 23462306a36Sopenharmony_cioperation by another chip. For these reasons, explicit device 23562306a36Sopenharmony_ciinstantiation should always be preferred to auto-detection where 23662306a36Sopenharmony_cipossible. 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ciDevice Deletion 24062306a36Sopenharmony_ci--------------- 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ciEach I2C device which has been created using i2c_new_client_device() 24362306a36Sopenharmony_cior i2c_new_scanned_device() can be unregistered by calling 24462306a36Sopenharmony_cii2c_unregister_device(). If you don't call it explicitly, it will be 24562306a36Sopenharmony_cicalled automatically before the underlying I2C bus itself is removed, 24662306a36Sopenharmony_cias a device can't survive its parent in the device driver model. 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ciInitializing the driver 25062306a36Sopenharmony_ci======================= 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ciWhen the kernel is booted, or when your foo driver module is inserted, 25362306a36Sopenharmony_ciyou have to do some initializing. Fortunately, just registering the 25462306a36Sopenharmony_cidriver module is usually enough. 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci:: 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ci static int __init foo_init(void) 25962306a36Sopenharmony_ci { 26062306a36Sopenharmony_ci return i2c_add_driver(&foo_driver); 26162306a36Sopenharmony_ci } 26262306a36Sopenharmony_ci module_init(foo_init); 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci static void __exit foo_cleanup(void) 26562306a36Sopenharmony_ci { 26662306a36Sopenharmony_ci i2c_del_driver(&foo_driver); 26762306a36Sopenharmony_ci } 26862306a36Sopenharmony_ci module_exit(foo_cleanup); 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci The module_i2c_driver() macro can be used to reduce above code. 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci module_i2c_driver(foo_driver); 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ciNote that some functions are marked by ``__init``. These functions can 27562306a36Sopenharmony_cibe removed after kernel booting (or module loading) is completed. 27662306a36Sopenharmony_ciLikewise, functions marked by ``__exit`` are dropped by the compiler when 27762306a36Sopenharmony_cithe code is built into the kernel, as they would never be called. 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ciDriver Information 28162306a36Sopenharmony_ci================== 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ci:: 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci /* Substitute your own name and email address */ 28662306a36Sopenharmony_ci MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>" 28762306a36Sopenharmony_ci MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices"); 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci /* a few non-GPL license types are also allowed */ 29062306a36Sopenharmony_ci MODULE_LICENSE("GPL"); 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ciPower Management 29462306a36Sopenharmony_ci================ 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ciIf your I2C device needs special handling when entering a system low 29762306a36Sopenharmony_cipower state -- like putting a transceiver into a low power mode, or 29862306a36Sopenharmony_ciactivating a system wakeup mechanism -- do that by implementing the 29962306a36Sopenharmony_ciappropriate callbacks for the dev_pm_ops of the driver (like suspend 30062306a36Sopenharmony_ciand resume). 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ciThese are standard driver model calls, and they work just like they 30362306a36Sopenharmony_ciwould for any other driver stack. The calls can sleep, and can use 30462306a36Sopenharmony_ciI2C messaging to the device being suspended or resumed (since their 30562306a36Sopenharmony_ciparent I2C adapter is active when these calls are issued, and IRQs 30662306a36Sopenharmony_ciare still enabled). 30762306a36Sopenharmony_ci 30862306a36Sopenharmony_ci 30962306a36Sopenharmony_ciSystem Shutdown 31062306a36Sopenharmony_ci=============== 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ciIf your I2C device needs special handling when the system shuts down 31362306a36Sopenharmony_cior reboots (including kexec) -- like turning something off -- use a 31462306a36Sopenharmony_cishutdown() method. 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ciAgain, this is a standard driver model call, working just like it 31762306a36Sopenharmony_ciwould for any other driver stack: the calls can sleep, and can use 31862306a36Sopenharmony_ciI2C messaging. 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ciCommand function 32262306a36Sopenharmony_ci================ 32362306a36Sopenharmony_ci 32462306a36Sopenharmony_ciA generic ioctl-like function call back is supported. You will seldom 32562306a36Sopenharmony_cineed this, and its use is deprecated anyway, so newer design should not 32662306a36Sopenharmony_ciuse it. 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ciSending and receiving 33062306a36Sopenharmony_ci===================== 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ciIf you want to communicate with your device, there are several functions 33362306a36Sopenharmony_cito do this. You can find all of them in <linux/i2c.h>. 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ciIf you can choose between plain I2C communication and SMBus level 33662306a36Sopenharmony_cicommunication, please use the latter. All adapters understand SMBus level 33762306a36Sopenharmony_cicommands, but only some of them understand plain I2C! 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_ciPlain I2C communication 34162306a36Sopenharmony_ci----------------------- 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci:: 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci int i2c_master_send(struct i2c_client *client, const char *buf, 34662306a36Sopenharmony_ci int count); 34762306a36Sopenharmony_ci int i2c_master_recv(struct i2c_client *client, char *buf, int count); 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ciThese routines read and write some bytes from/to a client. The client 35062306a36Sopenharmony_cicontains the I2C address, so you do not have to include it. The second 35162306a36Sopenharmony_ciparameter contains the bytes to read/write, the third the number of bytes 35262306a36Sopenharmony_cito read/write (must be less than the length of the buffer, also should be 35362306a36Sopenharmony_ciless than 64k since msg.len is u16.) Returned is the actual number of bytes 35462306a36Sopenharmony_ciread/written. 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ci:: 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg, 35962306a36Sopenharmony_ci int num); 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_ciThis sends a series of messages. Each message can be a read or write, 36262306a36Sopenharmony_ciand they can be mixed in any way. The transactions are combined: no 36362306a36Sopenharmony_cistop condition is issued between transaction. The i2c_msg structure 36462306a36Sopenharmony_cicontains for each message the client address, the number of bytes of the 36562306a36Sopenharmony_cimessage and the message data itself. 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ciYou can read the file i2c-protocol.rst for more information about the 36862306a36Sopenharmony_ciactual I2C protocol. 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ciSMBus communication 37262306a36Sopenharmony_ci------------------- 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_ci:: 37562306a36Sopenharmony_ci 37662306a36Sopenharmony_ci s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, 37762306a36Sopenharmony_ci unsigned short flags, char read_write, u8 command, 37862306a36Sopenharmony_ci int size, union i2c_smbus_data *data); 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ciThis is the generic SMBus function. All functions below are implemented 38162306a36Sopenharmony_ciin terms of it. Never use this function directly! 38262306a36Sopenharmony_ci 38362306a36Sopenharmony_ci:: 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_ci s32 i2c_smbus_read_byte(struct i2c_client *client); 38662306a36Sopenharmony_ci s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value); 38762306a36Sopenharmony_ci s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command); 38862306a36Sopenharmony_ci s32 i2c_smbus_write_byte_data(struct i2c_client *client, 38962306a36Sopenharmony_ci u8 command, u8 value); 39062306a36Sopenharmony_ci s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command); 39162306a36Sopenharmony_ci s32 i2c_smbus_write_word_data(struct i2c_client *client, 39262306a36Sopenharmony_ci u8 command, u16 value); 39362306a36Sopenharmony_ci s32 i2c_smbus_read_block_data(struct i2c_client *client, 39462306a36Sopenharmony_ci u8 command, u8 *values); 39562306a36Sopenharmony_ci s32 i2c_smbus_write_block_data(struct i2c_client *client, 39662306a36Sopenharmony_ci u8 command, u8 length, const u8 *values); 39762306a36Sopenharmony_ci s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, 39862306a36Sopenharmony_ci u8 command, u8 length, u8 *values); 39962306a36Sopenharmony_ci s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, 40062306a36Sopenharmony_ci u8 command, u8 length, 40162306a36Sopenharmony_ci const u8 *values); 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_ciThese ones were removed from i2c-core because they had no users, but could 40462306a36Sopenharmony_cibe added back later if needed:: 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value); 40762306a36Sopenharmony_ci s32 i2c_smbus_process_call(struct i2c_client *client, 40862306a36Sopenharmony_ci u8 command, u16 value); 40962306a36Sopenharmony_ci s32 i2c_smbus_block_process_call(struct i2c_client *client, 41062306a36Sopenharmony_ci u8 command, u8 length, u8 *values); 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ciAll these transactions return a negative errno value on failure. The 'write' 41362306a36Sopenharmony_citransactions return 0 on success; the 'read' transactions return the read 41462306a36Sopenharmony_civalue, except for block transactions, which return the number of values 41562306a36Sopenharmony_ciread. The block buffers need not be longer than 32 bytes. 41662306a36Sopenharmony_ci 41762306a36Sopenharmony_ciYou can read the file smbus-protocol.rst for more information about the 41862306a36Sopenharmony_ciactual SMBus protocol. 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci 42162306a36Sopenharmony_ciGeneral purpose routines 42262306a36Sopenharmony_ci======================== 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ciBelow all general purpose routines are listed, that were not mentioned 42562306a36Sopenharmony_cibefore:: 42662306a36Sopenharmony_ci 42762306a36Sopenharmony_ci /* Return the adapter number for a specific adapter */ 42862306a36Sopenharmony_ci int i2c_adapter_id(struct i2c_adapter *adap); 429