| /* |
| * Copyright 2012, Google Inc. |
| * All rights reserved. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions are |
| * met: |
| * |
| * * Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * * Redistributions in binary form must reproduce the above |
| * copyright notice, this list of conditions and the following |
| * disclaimer in the documentation and/or other materials provided |
| * with the distribution. |
| * * Neither the name of Google Inc. nor the names of its |
| * contributors may be used to endorse or promote products derived |
| * from this software without specific prior written permission. |
| * |
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| * |
| * I2C (Inter-Integrated Circuit) is a multi-master serial computer bus |
| * invented by Philips that is used to attach low-speed peripherals to |
| * a motherboard, embedded system, or cellphone. |
| */ |
| |
| #ifndef INTF_I2C_H__ |
| #define INTF_I2C_H__ |
| |
| #include <inttypes.h> |
| #include <errno.h> |
| |
| #include "mosys/list.h" |
| |
| #define I2C_HANDLE_MAX 64 |
| |
| struct platform_intf; |
| struct i2c_intf { |
| const char *sys_root; |
| const char *dev_root; |
| |
| /* |
| * setup - prepare interface |
| * |
| * @intf: platform interface |
| * |
| * returns 0 to indicate success |
| * returns <0 to indicate failure |
| */ |
| int (*setup)(struct platform_intf *intf); |
| |
| /* |
| * destroy - teardown interface |
| * |
| * @intf: platform interface |
| */ |
| void (*destroy)(struct platform_intf *intf); |
| |
| /* |
| * read - Read bytes from I2C device |
| * |
| * @intf: platform interface |
| * @bus: I2C bus/adapter |
| * @address: I2C slave address |
| * @reg: I2C register offset |
| * @length: number of bytes to read (1-255) |
| * @data: data buffer |
| * |
| * returns number of bytes read |
| * returns <0 to indicate error |
| */ |
| int (*read_reg)(struct platform_intf *intf, |
| int bus, int address, int reg, |
| int length, void *data); |
| |
| /* |
| * write - Write bytes to I2C device |
| * |
| * @intf: platform interface |
| * @bus: I2C bus/adapter |
| * @address: I2C slave address |
| * @reg: I2C register offset |
| * @length: number of bytes to read (1-255) |
| * @data: data buffer |
| * |
| * returns number of bytes written |
| * returns <0 to indicate error |
| */ |
| int (*write_reg)(struct platform_intf *intf, |
| int bus, int address, int reg, |
| int length, const void *data); |
| |
| /* |
| * read16 - Read bytes from I2C device using 16-bit address |
| * |
| * @intf: platform interface |
| * @bus: I2C bus/adapter |
| * @address: I2C slave address |
| * @reg: I2C register offset (16-bit) |
| * @length: number of bytes to read (1-255) |
| * @data: data buffer |
| * |
| * returns number of bytes read |
| * returns <0 to indicate error |
| */ |
| int (*read16)(struct platform_intf *intf, |
| int bus, int address, int reg, |
| int length, void *data); |
| |
| /* |
| * write16 - Write bytes to I2C device using 16-bit address |
| * |
| * @intf: platform interface |
| * @bus: I2C bus/adapter |
| * @address: I2C slave address |
| * @reg: I2C register offset (16-bit) |
| * @length: number of bytes to read (1-255) |
| * @data: data buffer |
| * |
| * returns number of bytes written |
| * returns <0 to indicate error |
| */ |
| int (*write16)(struct platform_intf *intf, |
| int bus, int address, int reg, |
| int length, const void *data); |
| |
| /* |
| * read_raw - read byte(s) from device without register addressing |
| * |
| * @intf: platform interface |
| * @bus: I2C bus/adapter |
| * @address: I2C slave address |
| * @length: number of bytes to read (1-256) |
| * @data: data buffer |
| * |
| * returns number of bytes read |
| * returns <0 to indicate failure |
| */ |
| int (*read_raw)(struct platform_intf *intf, |
| int bus, int address, int length, void *data); |
| |
| /* |
| * write_raw - write byte(s) to device without register addressing |
| * |
| * @intf: platform interface |
| * @bus: I2C bus/adapter |
| * @address: I2C slave address |
| * @length: number of bytes to write (1-256) |
| * @data: data buffer |
| * |
| * returns number of bytes written |
| * returns <0 to indicate failure |
| */ |
| int (*write_raw)(struct platform_intf *intf, |
| int bus, int address, int length, void *data); |
| |
| /* |
| * find_driver - Find I2C driver |
| * |
| * @intf: platform interface |
| * @name: i2c driver name |
| * |
| * returns 0 if the module is not found |
| * returns 1 if the module is found |
| */ |
| int (*find_driver)(struct platform_intf *intf, |
| const char *name); |
| |
| /* |
| * find_dir - Find I2C directory for device |
| * |
| * @intf: platform interface |
| * @name: i2c driver name |
| * |
| * returns the head of a linked list of matching devices |
| * returns NULL if no device found |
| */ |
| struct ll_node *(*find_dir)(struct platform_intf *intf, |
| const char *name); |
| |
| /* |
| * match_bus - Look for bus name |
| * |
| * @intf: platform interface |
| * @name: bus name |
| * @bus: bus number |
| * |
| * returns 1 if bus name does match |
| * returns 0 if bus name does not match |
| */ |
| int (*match_bus)(struct platform_intf *intf, |
| int bus, const char *name); |
| }; |
| |
| /* I2C operations for Linux /dev interface */ |
| extern struct i2c_intf i2c_dev_intf; |
| |
| struct i2c_addr { |
| int bus; |
| int addr; |
| }; |
| |
| /* Data for storing /sys i2c information */ |
| struct i2c_data { |
| struct i2c_addr addr; |
| char *dir; |
| }; |
| |
| #if defined(__linux__) |
| #include "intf/linux-i2c-dev.h" |
| #else |
| static inline int32_t i2c_smbus_write_quick(int file, uint8_t value) |
| { |
| return -ENOSYS; |
| } |
| |
| static inline int32_t i2c_smbus_read_byte(int file) |
| { |
| return -ENOSYS; |
| } |
| |
| static inline int32_t i2c_smbus_write_byte(int file, uint8_t value) |
| { |
| return -ENOSYS; |
| } |
| |
| static inline int32_t i2c_smbus_read_byte_data(int file, uint8_t command) |
| { |
| return -ENOSYS; |
| } |
| |
| static inline int32_t i2c_smbus_write_byte_data(int file, uint8_t command, |
| uint8_t value) |
| { |
| return -ENOSYS; |
| } |
| |
| static inline int32_t i2c_smbus_read_word_data(int file, uint8_t command) |
| { |
| return -ENOSYS; |
| } |
| |
| static inline int32_t i2c_smbus_write_word_data(int file, uint8_t command, |
| uint16_t value) |
| { |
| return -ENOSYS; |
| } |
| |
| static inline int32_t i2c_smbus_process_call(int file, uint8_t command, |
| uint16_t value) |
| { |
| return -ENOSYS; |
| } |
| |
| |
| /* Returns the number of read bytes */ |
| static inline int32_t i2c_smbus_read_block_data(int file, uint8_t command, |
| uint8_t *values) |
| { |
| return -ENOSYS; |
| } |
| |
| static inline int32_t i2c_smbus_write_block_data(int file, uint8_t command, |
| uint8_t length, uint8_t *values) |
| { |
| return -ENOSYS; |
| } |
| |
| /* Returns the number of read bytes */ |
| static inline int32_t i2c_smbus_read_i2c_block_data(int file, uint8_t command, |
| uint8_t *values) |
| { |
| return -ENOSYS; |
| } |
| |
| static inline int32_t i2c_smbus_write_i2c_block_data(int file, uint8_t command, |
| uint8_t length, uint8_t *values) |
| { |
| return -ENOSYS; |
| } |
| |
| /* Returns the number of read bytes */ |
| static inline int32_t i2c_smbus_block_process_call(int file, uint8_t command, |
| uint8_t length, uint8_t *values) |
| { |
| return -ENOSYS; |
| } |
| #endif |
| |
| #endif /* INTF_I2C_H__ */ |