summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorThierry Reding <treding@nvidia.com>2014-08-26 17:34:01 +0200
committerMarcel Ziswiler <marcel.ziswiler@toradex.com>2014-10-11 01:08:46 +0200
commit8f8a5a20b7475551bbd7d732e50ac3c9017dfb38 (patch)
tree47eb8a33f1ef6d080d4471087e69617d110ccafc /drivers
parent255e0ca96f808faf560b8d4d644d6e6f75c4fe9a (diff)
i2c: Add high-level API
This API operates on I2C adapters or I2C clients (a new type of object that refers to a particular slave connected to an adapter). This is useful to avoid having to call i2c_set_bus_num() whenever a device is being accessed. Drivers for I2C devices are supposed to embed a struct i2c_client within a driver-specific data structure and call i2c_client_init() on it, passing in a pointer to the parent I2C adapter and the slave address of the device. Signed-off-by: Thierry Reding <treding@nvidia.com>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/i2c/i2c_core.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/drivers/i2c/i2c_core.c b/drivers/i2c/i2c_core.c
index f6179a16244..88c7af546b0 100644
--- a/drivers/i2c/i2c_core.c
+++ b/drivers/i2c/i2c_core.c
@@ -410,3 +410,56 @@ void __i2c_init(int speed, int slaveaddr)
}
void i2c_init(int speed, int slaveaddr)
__attribute__((weak, alias("__i2c_init")));
+
+struct i2c_adapter *i2c_adapter_get(unsigned int index)
+{
+ struct i2c_adapter *adapter = ll_entry_start(struct i2c_adapter, i2c);
+ unsigned int num = ll_entry_count(struct i2c_adapter, i2c);
+ unsigned int i;
+
+ if (index >= num)
+ return NULL;
+
+ for (i = 0; i < index; i++)
+ adapter++;
+
+ i2c_adapter_init(adapter, adapter->speed, adapter->slaveaddr);
+ return adapter;
+}
+
+int i2c_adapter_read(struct i2c_adapter *adapter, uint8_t chip,
+ unsigned int address, size_t alen, void *buffer,
+ size_t size)
+{
+ return adapter->read(adapter, chip, address, alen, buffer, size);
+}
+
+int i2c_adapter_write(struct i2c_adapter *adapter, uint8_t chip,
+ unsigned int address, size_t alen, void *buffer,
+ size_t size)
+{
+ return adapter->write(adapter, chip, address, alen, buffer, size);
+}
+
+int i2c_client_init(struct i2c_client *client, struct i2c_adapter *adapter,
+ uint8_t address)
+{
+ client->adapter = adapter;
+ client->address = address;
+
+ return 0;
+}
+
+int i2c_client_read(struct i2c_client *client, unsigned int address,
+ size_t alen, void *buffer, size_t size)
+{
+ return i2c_adapter_read(client->adapter, client->address, address,
+ alen, buffer, size);
+}
+
+int i2c_client_write(struct i2c_client *client, unsigned int address,
+ size_t alen, void *buffer, size_t size)
+{
+ return i2c_adapter_write(client->adapter, client->address, address,
+ alen, buffer, size);
+}