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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/** @file
Support for IO expander TCA6424.
Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available under
the terms and conditions of the BSD License that accompanies this distribution.
The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Library/GpioExpanderLib.h>
#include <Library/I2cAccessLib.h>
//
// Addresses of registers inside expander
//
GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mInputRegister[3] = {0x0,0x1,0x2};
GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mOutputRegister[3] = {0x4,0x5,0x6};
GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mConfigRegister[3] = {0xC,0xD,0xE};
GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mPolarityRegister[3] = {0x8,0x9,0xA};
#define PCH_SERIAL_IO_I2C4 4
#define TCA6424_I2C_ADDRESS 0x22
#define PINS_PER_REGISTER 8
#define GPIO_EXP_PIN_DIRECTION_OUT 1
#define GPIO_EXP_PIN_DIRECTION_IN 0
#define GPIO_EXP_PIN_POLARITY_NORMAL 0
#define GPIO_EXP_PIN_POLARITY_INVERTED 1
#define GPIO_EXP_SET_OUTPUT 0
#define GPIO_EXP_SET_DIR 1
#define GPIO_EXP_GET_INPUT 2
#define GPIO_EXP_SET_POLARITY 3
#define AUTO_INCREMENT 0x80
/**
Returns the Controller on which GPIO expander is present.
This function returns the Controller value
@param[out] Controller Pointer to a Controller value on
which I2C expander is configured.
@retval EFI_SUCCESS non.
**/
EFI_STATUS
GpioExpGetController (
OUT UINT8 *Controller
)
{
*Controller = PCH_SERIAL_IO_I2C4;
return EFI_SUCCESS;
}
/**
Returns the data from register value giving in the input.
This function is to get the data from the Expander
Registers by following the I2C Protocol communication
@param[in] Bar0 Bar address of the SerialIo Controller
@param[in] Address Expander Value with in the Contoller
@param[in] Register Address of Input/Output/Configure/Polarity
registers with in the Expander
@retval UINT8 Value returned from the register
**/
UINT8
GpioExpGetRegister (
IN UINTN Bar0,
IN UINT8 Address,
IN UINT8 Register
)
{
EFI_STATUS Status;
UINT8 WriBuf[1];
UINT8 ReBuf[1] = {0};
WriBuf[0] = Register;
Status = I2cWriteRead( Bar0, TCA6424_I2C_ADDRESS+Address, 1, WriBuf, 1, ReBuf, WAIT_1_SECOND);
return ReBuf[0];
}
/**
Set the input register to a give value mentioned in the function.
This function is to Programm the data value to the Expander
Register by following the I2C Protocol communication.
@param[in] Bar0 Bar address of the SerialIo Controller
@param[in] Address Expander Value with in the Contoller
@param[in] Register Address of Input/Output/Configure/Polarity
registers with in the Expander
@param[in] Value Value to set in the mentioned the register
**/
VOID
GpioExpSetRegister (
IN UINTN Bar0,
IN UINT8 Address,
IN UINT8 Register,
IN UINT8 Value
)
{
EFI_STATUS Status;
UINT8 WriBuf[2];
WriBuf[0] = Register;
WriBuf[1] = Value;
Status = I2cWriteRead( Bar0, TCA6424_I2C_ADDRESS+Address, 2, WriBuf, 0, NULL, WAIT_1_SECOND);
}
/**
Set the input register to a give value mentioned in the function.
This function is to update the status of the Gpio Expander
pin based on the input Operation value of the caller.This
function calculates the exact address of the register with
the help of the Register Bank
@param[in] Controller SerialIo Controller value
@param[in] Expander Expander Value with in the Contoller
@param[in] Pin Pin with in the Expnader Value
@param[in] Value none
@param[in] Operation Type of operation (Setoutput/Setdirection
/Getinput/Setpolarity)
@retval UINT8 Final Value returned from the register
**/
UINT8
GpioExpDecodeRegAccess (
IN UINT8 Controller,
IN UINT8 Expander,
IN UINT8 Pin,
IN UINT8 Value,
IN UINT8 Operation
)
{
UINT8* RegisterBank;
UINT8 OldValue;
UINT8 NewValue;
UINT8 RegisterAddress;
UINT8 PinNumber;
UINT8 ReturnValue = 0;
DEBUG ((DEBUG_INFO, "GpioExpDecodeRegAccess() %x:%x:%x:%x:%x\n", Controller, Expander, Pin, Value, Operation));
ASSERT(Controller<6);
ASSERT(Expander<2);
ASSERT(Pin<24);
ASSERT(Value<2);
ASSERT(Operation<4);
//
// Find the register Address value based on the OPeration
//
switch(Operation) {
case GPIO_EXP_SET_OUTPUT:
RegisterBank = mOutputRegister;
break;
case GPIO_EXP_SET_DIR:
RegisterBank = mConfigRegister;
break;
case GPIO_EXP_GET_INPUT:
RegisterBank = mInputRegister;
break;
case GPIO_EXP_SET_POLARITY:
RegisterBank = mPolarityRegister;
break;
default:
ASSERT(FALSE);
return 0;
}
//
// Each bit of register represents each Pin
// calaulate the register address and Pinnumber(offset with in register)
//
if (Pin >= 24) {
//
// Avoid out-of-bound usage of RegisterBank
//
return 0;
}
RegisterAddress = RegisterBank[(Pin/PINS_PER_REGISTER)];
PinNumber = Pin%PINS_PER_REGISTER;
OldValue = GpioExpGetRegister(FindSerialIoBar(Controller, 0), Expander, RegisterAddress);
//
// If it to get the data ,just returned otherwise mark the input value and write the register
//
if (Operation == GPIO_EXP_GET_INPUT) {
ReturnValue = 0x1 & (OldValue>>PinNumber);
} else {
NewValue = OldValue;
NewValue &= ~(BIT0<<PinNumber);
NewValue |= (Value<<PinNumber);
if(NewValue!=OldValue) {
GpioExpSetRegister(FindSerialIoBar(Controller, 0), Expander, RegisterAddress, NewValue);
}
}
return ReturnValue;
}
/**
Set the Output value for the given Expander Gpio pin.
This function is to Set the Output value for the GPIO
Pin within the giving Expander.
@param[in] Expander Expander Value with in the Contoller
@param[in] Pin Pin with in the Expnader Value
@param[in] Value none
**/
VOID
GpioExpSetOutput (
IN UINT8 Expander,
IN UINT8 Pin,
IN UINT8 Value
)
{
UINT8 Controller;
if(!EFI_ERROR(GpioExpGetController(&Controller))) {
GpioExpDecodeRegAccess(Controller,Expander,Pin,Value,GPIO_EXP_SET_OUTPUT);
}
}
/**
Set the Direction value for the given Expander Gpio pin.
This function is to Set the direction value for the GPIO
Pin within the giving Expander.
@param[in] Expander Expander Value with in the Contoller
@param[in] Pin Pin with in the Expnader Value
@param[in] Value none
**/
VOID
GpioExpSetDirection (
IN UINT8 Expander,
IN UINT8 Pin,
IN UINT8 Value
)
{
UINT8 Controller;
if(!EFI_ERROR(GpioExpGetController(&Controller))) {
GpioExpDecodeRegAccess(Controller,Expander,Pin,Value,GPIO_EXP_SET_DIR);
}
}
/**
Get the input value for the given Expander Gpio pin.
This function is to get the input value for the GPIO
Pin within the giving Expander.
@param[in] Expander Expander Value with in the Contoller
@param[in] Pin Pin with in the Expnader Value
@retval UINT8 Final Value returned from the register
**/
UINT8
GpioExpGetInput (
IN UINT8 Expander,
IN UINT8 Pin
)
{
UINT8 Controller;
if(!EFI_ERROR(GpioExpGetController(&Controller))) {
return GpioExpDecodeRegAccess(Controller,Expander,Pin,0,GPIO_EXP_GET_INPUT);
}
return 0;
}
/**
Configures all registers of a single IO Expander in one go.
@param[in] Expander Expander number (0/1)
@param[in] Direction Bit-encoded direction values. BIT0 is for pin0, etc. 0=output, 1=input
@param[in] Polarity Bit-encoded input inversion values. BIT0 is for pin0, etc. 0=normal, 1=inversion
@param[in] Output Bit-encoded output state, ignores polarity, only applicable if direction=INPUT. BIT0 is for pin0, etc. 0=low, 1=high
**/
VOID
GpioExpBulkConfig (
IN UINT8 Expander,
IN UINT32 Direction,
IN UINT32 Polarity,
IN UINT32 Output
)
{
UINT8 WriteBuf[4];
UINT8 Controller;
GpioExpGetController(&Controller);
WriteBuf[0] = mOutputRegister[0] + AUTO_INCREMENT;
WriteBuf[1] = Output & 0xFF;
WriteBuf[2] = (Output>>8) & 0xFF;
WriteBuf[3] = (Output>>16) & 0xFF;
I2cWriteRead( FindSerialIoBar(Controller,0), TCA6424_I2C_ADDRESS+Expander, 4, WriteBuf, 0, NULL, WAIT_1_SECOND);
WriteBuf[0] = mPolarityRegister[0] + AUTO_INCREMENT;
WriteBuf[1] = Polarity & 0xFF;
WriteBuf[2] = (Polarity>>8) & 0xFF;
WriteBuf[3] = (Polarity>>16) & 0xFF;
I2cWriteRead( FindSerialIoBar(Controller,0), TCA6424_I2C_ADDRESS+Expander, 4, WriteBuf, 0, NULL, WAIT_1_SECOND);
WriteBuf[0] = mConfigRegister[0] + AUTO_INCREMENT;
WriteBuf[1] = Direction & 0xFF;
WriteBuf[2] = (Direction>>8) & 0xFF;
WriteBuf[3] = (Direction>>16) & 0xFF;
I2cWriteRead( FindSerialIoBar(Controller,0), TCA6424_I2C_ADDRESS+Expander, 4, WriteBuf, 0, NULL, WAIT_1_SECOND);
}
|