2014-04-11 15:27:43 +01:00
|
|
|
/*
|
2014-07-14 17:49:31 +01:00
|
|
|
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
2014-04-22 15:51:28 +01:00
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
2014-04-11 15:27:43 +01:00
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-04-27 21:17:54 +01:00
|
|
|
#include "i2c.h"
|
|
|
|
|
#include "smbus.h"
|
2014-06-24 17:24:54 +01:00
|
|
|
#include "mraa_internal.h"
|
2014-04-11 15:27:43 +01:00
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_i2c_context
|
|
|
|
|
mraa_i2c_init(int bus)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2014-11-03 15:44:40 +00:00
|
|
|
if (plat == NULL) {
|
|
|
|
|
syslog(LOG_ERR, "i2c: Platform Not Initialised");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (plat->i2c_bus_count == 0) {
|
|
|
|
|
syslog(LOG_ERR, "No i2c buses defined in platform");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (bus >= plat->i2c_bus_count) {
|
|
|
|
|
syslog(LOG_ERR, "Above i2c bus count");
|
|
|
|
|
return NULL;
|
2014-05-02 16:07:18 +01:00
|
|
|
}
|
2014-11-03 15:44:40 +00:00
|
|
|
|
|
|
|
|
if (plat->i2c_bus[bus].bus_id == -1) {
|
|
|
|
|
syslog(LOG_ERR, "Invalid i2c bus, moving to default i2c bus");
|
|
|
|
|
bus = plat->def_i2c_bus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pos = plat->i2c_bus[bus].sda;
|
|
|
|
|
if (plat->pins[pos].i2c.mux_total > 0)
|
|
|
|
|
if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS) {
|
|
|
|
|
syslog(LOG_ERR, "i2c: Failed to set-up i2c sda multiplexer");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pos = plat->i2c_bus[bus].scl;
|
|
|
|
|
if (plat->pins[pos].i2c.mux_total > 0)
|
|
|
|
|
if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS) {
|
|
|
|
|
syslog(LOG_ERR, "i2c: Failed to set-up i2c scl multiplexer");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mraa_i2c_init_raw((unsigned int) plat->i2c_bus[bus].bus_id);
|
2014-05-02 16:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_i2c_context
|
|
|
|
|
mraa_i2c_init_raw(unsigned int bus)
|
2014-05-02 16:07:18 +01:00
|
|
|
{
|
2014-07-14 18:19:07 +01:00
|
|
|
if (advance_func->i2c_init_pre != NULL) {
|
|
|
|
|
if (advance_func->i2c_init_pre(bus) != MRAA_SUCCESS)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
|
2014-10-03 22:50:18 +01:00
|
|
|
if (dev == NULL) {
|
2014-10-22 21:06:45 +01:00
|
|
|
syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
|
2014-04-28 11:31:53 +01:00
|
|
|
return NULL;
|
2014-10-03 22:50:18 +01:00
|
|
|
}
|
2014-04-27 23:11:31 +01:00
|
|
|
|
2014-05-02 16:07:18 +01:00
|
|
|
char filepath[32];
|
|
|
|
|
snprintf(filepath, 32, "/dev/i2c-%u", bus);
|
|
|
|
|
if ((dev->fh = open(filepath, O_RDWR)) < 1) {
|
2014-10-22 21:06:45 +01:00
|
|
|
syslog(LOG_ERR, "i2c: Failed to open requested i2c port %s", filepath);
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
2014-07-14 18:19:07 +01:00
|
|
|
|
|
|
|
|
if (advance_func->i2c_init_post != NULL) {
|
|
|
|
|
mraa_result_t ret = advance_func->i2c_init_post(dev);
|
|
|
|
|
if (ret != MRAA_SUCCESS) {
|
|
|
|
|
free(dev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-29 14:01:30 +01:00
|
|
|
return dev;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
|
|
|
|
mraa_i2c_frequency(mraa_i2c_context dev, int hz)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2014-04-27 21:17:54 +01:00
|
|
|
dev->hz = hz;
|
2014-04-30 11:35:29 +01:00
|
|
|
|
2014-10-20 10:01:53 +01:00
|
|
|
return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-11 10:47:13 +01:00
|
|
|
int
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2014-06-24 17:24:54 +01:00
|
|
|
// this is the read(3) syscall not mraa_i2c_read()
|
2014-04-27 21:17:54 +01:00
|
|
|
if (read(dev->fh, data, length) == length) {
|
2014-04-11 15:46:27 +01:00
|
|
|
return length;
|
|
|
|
|
}
|
2014-06-11 10:47:13 +01:00
|
|
|
return 0;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-23 14:21:53 +01:00
|
|
|
uint8_t
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_i2c_read_byte(mraa_i2c_context dev)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2014-05-23 14:21:53 +01:00
|
|
|
uint8_t byte = i2c_smbus_read_byte(dev->fh);
|
2014-04-11 15:27:43 +01:00
|
|
|
return byte;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 23:25:10 +00:00
|
|
|
uint8_t
|
|
|
|
|
mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
|
|
|
|
|
{
|
|
|
|
|
uint8_t byte = i2c_smbus_read_byte_data(dev->fh, command);
|
|
|
|
|
return byte;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
|
|
|
|
mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2014-04-27 21:17:54 +01:00
|
|
|
if (i2c_smbus_write_i2c_block_data(dev->fh, data[0], length-1, (uint8_t*) data+1) < 0) {
|
2014-10-22 21:06:45 +01:00
|
|
|
syslog(LOG_ERR, "i2c: Failed to write");
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_ERROR_INVALID_HANDLE;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_SUCCESS;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
|
|
|
|
mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2014-04-27 21:17:54 +01:00
|
|
|
if (i2c_smbus_write_byte(dev->fh, data) < 0) {
|
2014-10-22 21:06:45 +01:00
|
|
|
syslog(LOG_ERR, "i2c: Failed to write");
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_ERROR_INVALID_HANDLE;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_SUCCESS;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
2014-10-31 15:10:15 +00:00
|
|
|
mraa_i2c_address(mraa_i2c_context dev, uint8_t addr)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2014-10-31 15:10:15 +00:00
|
|
|
dev->addr = (int) addr;
|
2014-04-27 21:17:54 +01:00
|
|
|
if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
|
2014-10-22 21:06:45 +01:00
|
|
|
syslog(LOG_ERR, "i2c: Failed to set slave address %d", addr);
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_ERROR_INVALID_HANDLE;
|
2014-04-11 15:46:27 +01:00
|
|
|
}
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_SUCCESS;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_result_t
|
|
|
|
|
mraa_i2c_stop(mraa_i2c_context dev)
|
2014-04-11 15:27:43 +01:00
|
|
|
{
|
2014-04-27 21:17:54 +01:00
|
|
|
free(dev);
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_SUCCESS;
|
2014-04-11 15:27:43 +01:00
|
|
|
}
|