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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/*
* Copyright (C) 2014 Freescale Semiconductor, Inc.
*
* See file CREDITS for list of people who contributed to this
* project.
*
* 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.
*/
#include <common.h>
#include <asm/gpio.h>
#include <asm/io.h>
#include <errno.h>
#include <i2c.h>
#include <gpio_exp.h>
#define MAX7310_REG_INPUT_PORT 0x00
#define MAX7310_REG_OUTPUT_PORT 0x01
#define MAX7310_REG_POLARITY 0x02
#define MAX7310_REG_CONFIGURATION 0x03
#define MAX7310_REG_TIMEOUT 0x04
enum max7310_gpio_direction {
MAX7310_GPIO_DIRECTION_IN,
MAX7310_GPIO_DIRECTION_OUT,
};
struct max7310_config_struct {
int i2c_slave_addr;
int i2c_bus_id;
};
static struct max7310_config_struct max7310_configs[CONFIG_IOEXP_DEVICES_NUM];
static int max7310_gpio_direction(unsigned int gpio,
enum max7310_gpio_direction direction)
{
unsigned int dev = IOEXP_GPIO_TO_DEVICE(gpio);
unsigned int pin = IOEXP_GPIO_TO_PIN(gpio);
unsigned char value, val2, val3;
unsigned char chip;
if (dev >= CONFIG_IOEXP_DEVICES_NUM)
return -EPERM;
chip = max7310_configs[dev].i2c_slave_addr;
i2c_set_bus_num(max7310_configs[dev].i2c_bus_id);
if (i2c_probe(chip))
return -ENXIO;
i2c_read(chip, MAX7310_REG_CONFIGURATION, 1, &value, 1);
switch (direction) {
case MAX7310_GPIO_DIRECTION_OUT:
value &= ~(1 << pin);
i2c_write(chip, MAX7310_REG_CONFIGURATION, 1, &value, 1);
break;
case MAX7310_GPIO_DIRECTION_IN:
i2c_read(chip, MAX7310_REG_POLARITY, 1, &val2, 1);
i2c_read(chip, MAX7310_REG_OUTPUT_PORT, 1, &val3, 1);
value |= (1 << pin);
val2 &= ~(1 << pin);
val3 &= ~(1 << pin);
i2c_write(chip, MAX7310_REG_POLARITY, 1, &val2, 1);
i2c_write(chip, MAX7310_REG_CONFIGURATION, 1, &value, 1);
i2c_write(chip, MAX7310_REG_OUTPUT_PORT, 1, &val3, 1);
break;
}
return 0;
}
static int max7310_gpio_set_value(unsigned gpio, int value)
{
unsigned int dev = IOEXP_GPIO_TO_DEVICE(gpio);
unsigned int pin = IOEXP_GPIO_TO_PIN(gpio);
unsigned char reg_val;
unsigned char chip;
if (dev >= CONFIG_IOEXP_DEVICES_NUM)
return -EPERM;
chip = max7310_configs[dev].i2c_slave_addr;
i2c_set_bus_num(max7310_configs[dev].i2c_bus_id);
if (i2c_probe(chip))
return -ENXIO;
i2c_read(chip, MAX7310_REG_OUTPUT_PORT, 1, ®_val, 1);
if (value)
reg_val |= 1 << pin;
else
reg_val &= ~(1 << pin);
i2c_write(chip, MAX7310_REG_OUTPUT_PORT, 1, ®_val, 1);
return 0;
}
static int max7310_gpio_get_value(unsigned gpio)
{
unsigned int dev = IOEXP_GPIO_TO_DEVICE(gpio);
unsigned int pin = IOEXP_GPIO_TO_PIN(gpio);
unsigned char reg_val;
unsigned char chip;
if (dev >= CONFIG_IOEXP_DEVICES_NUM)
return -EPERM;
chip = max7310_configs[dev].i2c_slave_addr;
i2c_set_bus_num(max7310_configs[dev].i2c_bus_id);
if (i2c_probe(chip))
return -ENXIO;
i2c_read(chip, MAX7310_REG_INPUT_PORT, 1, ®_val, 1);
reg_val = (reg_val >> pin) & 0x01;
return reg_val;
}
int gpio_exp_direction_input(unsigned gpio)
{
int ret = max7310_gpio_direction(gpio, MAX7310_GPIO_DIRECTION_IN);
if (ret < 0)
return ret;
return max7310_gpio_get_value(gpio);
}
int gpio_exp_direction_output(unsigned gpio, int value)
{
int ret = max7310_gpio_direction(gpio, MAX7310_GPIO_DIRECTION_OUT);
if (ret < 0)
return ret;
return max7310_gpio_set_value(gpio, value);
}
int gpio_exp_setup_port(int port, int i2c_bus_id, int i2c_slave_addr)
{
if (port > CONFIG_IOEXP_DEVICES_NUM || port <= 0)
return -EPERM;
max7310_configs[port-1].i2c_bus_id = i2c_bus_id;
max7310_configs[port-1].i2c_slave_addr = i2c_slave_addr;
return 0;
}
|