summaryrefslogtreecommitdiffstats
path: root/drivers/misc/pca9551_led.c
blob: 4da031985f732ecb4b55f12ff1f44af5c1ffb334 (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
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
/*
 * Copyright (C) 2015 Stefan Roese <sr@denx.de>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <errno.h>
#include <i2c.h>

#ifndef CONFIG_PCA9551_I2C_ADDR
#error "CONFIG_PCA9551_I2C_ADDR not defined!"
#endif

#define PCA9551_REG_INPUT	0x00	/* Input register (read only) */
#define PCA9551_REG_PSC0	0x01	/* Frequency prescaler 0 */
#define PCA9551_REG_PWM0	0x02	/* PWM0 */
#define PCA9551_REG_PSC1	0x03	/* Frequency prescaler 1 */
#define PCA9551_REG_PWM1	0x04	/* PWM1 */
#define PCA9551_REG_LS0		0x05	/* LED0 to LED3 selector */
#define PCA9551_REG_LS1		0x06	/* LED4 to LED7 selector */

#define PCA9551_CTRL_AI		(1 << 4)	/* Auto-increment flag */

#define PCA9551_LED_STATE_ON		0x00
#define PCA9551_LED_STATE_OFF		0x01
#define PCA9551_LED_STATE_BLINK0	0x02
#define PCA9551_LED_STATE_BLINK1	0x03

struct pca9551_blink_rate {
	u8 psc;	/* Frequency preescaler, see PCA9551_7.pdf p. 6 */
	u8 pwm;	/* Pulse width modulation, see PCA9551_7.pdf p. 6 */
};

static int freq_last = -1;
static int mask_last = -1;
static int idx_last = -1;
static int mode_last;

static int pca9551_led_get_state(int led, int *state)
{
	unsigned int reg;
	u8 shift, buf;
	int ret;

	if (led < 0 || led > 7) {
		return -EINVAL;
	} else if (led < 4) {
		reg = PCA9551_REG_LS0;
		shift = led << 1;
	} else {
		reg = PCA9551_REG_LS1;
		shift = (led - 4) << 1;
	}

	ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
	if (ret)
		return ret;

	*state = (buf >> shift) & 0x03;
	return 0;
}

static int pca9551_led_set_state(int led, int state)
{
	unsigned int reg;
	u8 shift, buf, mask;
	int ret;

	if (led < 0 || led > 7) {
		return -EINVAL;
	} else if (led < 4) {
		reg = PCA9551_REG_LS0;
		shift = led << 1;
	} else {
		reg = PCA9551_REG_LS1;
		shift = (led - 4) << 1;
	}
	mask = 0x03 << shift;

	ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
	if (ret)
		return ret;

	buf = (buf & ~mask) | ((state & 0x03) << shift);

	ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
	if (ret)
		return ret;

	return 0;
}

static int pca9551_led_set_blink_rate(int idx, struct pca9551_blink_rate rate)
{
	unsigned int reg;
	int ret;

	switch (idx) {
	case 0:
		reg = PCA9551_REG_PSC0;
		break;
	case 1:
		reg = PCA9551_REG_PSC1;
		break;
	default:
		return -EINVAL;
	}
	reg |= PCA9551_CTRL_AI;

	ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, (u8 *)&rate, 2);
	if (ret)
		return ret;

	return 0;
}

/*
 * Functions referenced by cmd_led.c or status_led.c
 */
void __led_init(led_id_t id, int state)
{
}

void __led_set(led_id_t mask, int state)
{
	if (state == STATUS_LED_OFF)
		pca9551_led_set_state(mask, PCA9551_LED_STATE_OFF);
	else
		pca9551_led_set_state(mask, PCA9551_LED_STATE_ON);
}

void __led_toggle(led_id_t mask)
{
	int state = 0;

	pca9551_led_get_state(mask, &state);
	pca9551_led_set_state(mask, !state);
}

void __led_blink(led_id_t mask, int freq)
{
	struct pca9551_blink_rate rate;
	int mode;
	int idx;

	if ((freq == freq_last) || (mask == mask_last)) {
		idx = idx_last;
		mode = mode_last;
	} else {
		/* Toggle blink index */
		if (idx_last == 0) {
			idx = 1;
			mode = PCA9551_LED_STATE_BLINK1;
		} else {
			idx = 0;
			mode = PCA9551_LED_STATE_BLINK0;
		}

		idx_last = idx;
		mode_last = mode;
	}
	freq_last = freq;
	mask_last = mask;

	rate.psc = ((freq * 38) / 1000) - 1;
	rate.pwm = 128;		/* 50% duty cycle */

	pca9551_led_set_blink_rate(idx, rate);
	pca9551_led_set_state(mask, mode);
}
OpenPOWER on IntegriCloud