aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-sysmo/misc/sx150x.c
blob: be26d28b22239702bfda9e093e56953ecf53906d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* sx150x - minimal SX150x I2C GPIO expander driver
 * (C) 2015 by sysmocom - s.f.m.c. GmbH, Author: Harald Welte
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <limits.h>
#include <fcntl.h>
#include <getopt.h>

#include <sys/ioctl.h>
#include <sys/stat.h>

/* #include <linux/i2c-dev.h> */
#include "i2c-dev.h"

#include "sx150x.h"

enum sx150x_reg {
	SX150x_REG_DATA		= 0x00,
	SX150x_REG_DIRECTION	= 0x01,
	SX150x_REG_PULL_UP	= 0x02,
	SX150x_REG_PULL_DOWN	= 0x03,
	/* resered */
	SX150x_REG_IRQ_MASK	= 0x05,
	SX150x_REG_SENSE_HIGH	= 0x06,
	SX150x_REG_SENSE_LOW	= 0x07,
	SX150x_REG_IRQ_SOURCE	= 0x08,
	SX150x_REG_EVENT_STATUS	= 0x09,
	/* what about 0x0a - 0x0f? */
	SX150x_REG_PLD_MODE	= 0x10,
	SX150x_REG_PLD_TABLE0	= 0x11,
	SX150x_REG_PLD_TABLE1	= 0x12,
	SX150x_REG_PLD_TABLE2	= 0x13,
	SX150x_REG_PLD_TABLE3	= 0x13,
	SX150x_REG_PLD_TABLE4	= 0x13,
	SX150x_REG_ADVANCED	= 0xAB,
};

int sx150x_gpio_direction_set(int fd, int gpio, enum sx150x_direction dir)
{
	int rc;

	rc = i2c_smbus_read_byte_data(fd, SX150x_REG_DIRECTION);
	if (rc < 0)
		return rc;

	rc &= ~(1 << gpio);
	rc |= (dir << gpio);

	return i2c_smbus_write_byte_data(fd, SX150x_REG_DIRECTION, rc);
}

int sx150x_gpio_direction_get(int fd, int gpio)
{
	int rc;

	rc = i2c_smbus_read_byte_data(fd, SX150x_REG_DIRECTION);
	if (rc < 0)
		return rc;
	return (rc & (1 << gpio));
}

int sx150x_gpio_pull_up_set(int fd, int gpio, int on)
{
	int rc;

	rc = i2c_smbus_read_byte_data(fd, SX150x_REG_PULL_UP);
	if (rc < 0)
		return rc;

	if (on)
		rc |= (1 << gpio);
	else
		rc &= ~(1 << gpio);

	return i2c_smbus_write_byte_data(fd, SX150x_REG_PULL_UP, rc);
}

int sx150x_gpio_pull_down_set(int fd, int gpio, int on)
{
	int rc;

	rc = i2c_smbus_read_byte_data(fd, SX150x_REG_PULL_DOWN);
	if (rc < 0)
		return rc;

	if (on)
		rc |= (1 << gpio);
	else
		rc &= ~(1 << gpio);

	return i2c_smbus_write_byte_data(fd, SX150x_REG_PULL_DOWN, rc);
}

int sx150x_gpio_set(int fd, int gpio, int high)
{
	int rc;

	rc = i2c_smbus_read_byte_data(fd, SX150x_REG_DATA);
	if (rc < 0)
		return rc;

	if (high)
		rc |= (1 << gpio);
	else
		rc &= ~(1 << gpio);

	return i2c_smbus_write_byte_data(fd, SX150x_REG_DATA, rc);
}

int sx150x_gpio_get(int fd, int gpio)
{
	int rc;

	rc = i2c_smbus_read_byte_data(fd, SX150x_REG_DATA);
	if (rc < 0)
		return rc;
	return (rc & (1 << gpio));
}