/*
 * Filename: eeprom_read.c
 *
 * Function:
 *
 * demonstrate accesses to the i2c WR eeprom on the sis1160
 *
 * Author:			TF
 * Date:			09.02.2018
 * Last modified:	09.02.2018
 *
 * -------------------------------------------
 *
 * SIS Struck Innovative Systeme GmbH
 *
 * Harksheider Straße 102a
 * 22399 Hamburg
 *
 * Tel. +49 (0)40 60 87 305 0
 * Fax	+49 (0)40 60 87 305 20
 *
 * http://www.struck.de
 *
 * (c) 2018
 */

#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <linux/fs.h>

#include "sis1100_var.h"

typedef struct {
	uint32_t offset; /* offset */
	uint32_t data;   /* data which will be read / written */
	uint32_t error;
}sis1100_reg_t;



/*
 *
 */
int sis1100_control_read(int device, uint32_t offset, uint32_t *data){

	sis1100_reg_t reg;
	reg.offset = offset;

	ioctl(device, SIS1100_CTRL_READ, &reg);

	*data = reg.data;

	return reg.error;
}

/*
 *
 */
int sis1100_control_write(int device, uint32_t offset, uint32_t data){

	sis1100_reg_t reg;
	reg.offset 	= offset;
	reg.data	= data;

	ioctl(device, SIS1100_CTRL_WRITE, &reg);

	return reg.error;
}

int main(int argc, char **argv) {

	int fp;
	uint32_t data;

	if(argc != 3){
		printf("usage: %s [device] [offset]\n", argv[0]);
		return -1;
	}

	fp = open(argv[1], O_RDWR);
		if(fp < 0){
			printf("can't open device %s\n", argv[1]);
			return -1;
	}

	sis1100_control_read(fp, 0x0, &data);
	printf("Data: %x\n", data);


	close(fp);

	return EXIT_SUCCESS;
}
