summaryrefslogtreecommitdiff
path: root/driver/ioexpander_pca9534.c
blob: 4eddd5adab56ba9962cfdc50b88796b9d803d45d (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
/* Copyright 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * NXP PCA9534 I/O expander
 */

#include "i2c.h"
#include "ioexpander_pca9534.h"

static int pca9534_pin_read__7bf(const int port, const uint16_t addr__7bf,
			    int reg, int pin, int *val)
{
	int ret;
	ret = i2c_read8__7bf(port, addr__7bf, reg, val);
	*val = (*val & BIT(pin)) ? 1 : 0;
	return ret;
}

static int pca9534_pin_write__7bf(const int port, const uint16_t addr__7bf,
			     int reg, int pin, int val)
{
	int ret, v;
	ret = i2c_read8__7bf(port, addr__7bf, reg, &v);
	if (ret != EC_SUCCESS)
		return ret;
	v &= ~BIT(pin);
	if (val)
		v |= 1 << pin;
	return i2c_write8__7bf(port, addr__7bf, reg, v);
}

int pca9534_get_level__7bf(const int port, const uint16_t addr__7bf,
		      int pin, int *level)
{
	return pca9534_pin_read__7bf(port, addr__7bf,
				PCA9534_REG_INPUT, pin, level);
}

int pca9534_set_level__7bf(const int port, const uint16_t addr__7bf,
		      int pin, int level)
{
	return pca9534_pin_write__7bf(port, addr__7bf,
				 PCA9534_REG_OUTPUT, pin, level);
}

int pca9534_config_pin__7bf(const int port, const uint16_t addr__7bf,
		       int pin, int is_input)
{
	return pca9534_pin_write__7bf(port, addr__7bf,
				 PCA9534_REG_CONFIG, pin, is_input);
}