summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/axp20x.c
blob: abf5e475c68804dd241aac2fd5bc5fbc3b913b8d (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
/* $OpenBSD: axp20x.c,v 1.2 2017/07/24 20:35:26 kettenis Exp $ */
/*
 * Copyright (c) 2014,2016 Artturi Alm
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/sensors.h>

#include <dev/i2c/i2cvar.h>

#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>

#include <armv7/armv7/armv7_machdep.h>	/* needed for powerdownfn */

/* Power Status Register / Input power status */
#define	AXP209_PSR		0x00
#define	AXP209_PSR_ACIN		(1 << 7)	/* ACIN Exists */
#define	AXP209_PSR_VBUS		(1 << 5)	/* VBUS Exists */

/* Shutdown settings, battery detection, and CHGLED Pin control */
#define	AXP209_SDR		0x32
#define	AXP209_SDR_SHUTDOWN	(1 << 7)	/* Shutdown Control */

struct axp20x_softc {
	struct device	sc_dev;
	i2c_tag_t	sc_i2c;
	i2c_addr_t	sc_addr;
};

int	axp20x_match(struct device *, void *, void *);
void	axp20x_attach(struct device *, struct device *, void *);

int	axp20x_readb(uint8_t, uint8_t *);
int	axp20x_writeb(uint8_t, uint8_t);
void	axp20x_shutdown(void);

struct cfattach axppmic_ca = {
	sizeof(struct axp20x_softc), axp20x_match, axp20x_attach
};

struct cfdriver axppmic_cd = {
	NULL, "axppmic", DV_DULL
};

int
axp20x_match(struct device *parent, void *match, void *aux)
{
	struct i2c_attach_args *ia = aux;
	int node = *(int *)ia->ia_cookie;

	return (OF_is_compatible(node, "x-powers,axp152") ||
	    OF_is_compatible(node, "x-powers,axp209"));
}

void
axp20x_attach(struct device *parent, struct device *self, void *aux)
{
	struct axp20x_softc *sc = (struct axp20x_softc *)self;
	struct i2c_attach_args *ia = aux;
	int node = *(int *)ia->ia_cookie;
	uint8_t psr;

	sc->sc_i2c = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;

	if (OF_is_compatible(node, "x-powers,axp152")) {
		printf(": AXP152");
	} else {
		axp20x_readb(AXP209_PSR, &psr);
		printf(": AXP209,");
		if ((psr & (AXP209_PSR_ACIN | AXP209_PSR_VBUS)) == 0)
			printf(" BAT");
		else {
			if (psr & AXP209_PSR_ACIN)
				printf(" ACIN");
			if (psr & AXP209_PSR_VBUS)
				printf(" VBUS");
		}
	}
	printf("\n");

	powerdownfn = axp20x_shutdown;
}

int
axp20x_readb(uint8_t reg, uint8_t *val)
{
	struct axp20x_softc *sc = axppmic_cd.cd_devs[0];
	int flags = I2C_F_POLL;
	int ret;

	if (sc == NULL)
		return ENXIO;

	iic_acquire_bus(sc->sc_i2c, flags);
	ret = iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, val, flags);
	iic_release_bus(sc->sc_i2c, flags);
	return ret;

}

int
axp20x_writeb(uint8_t reg, uint8_t data)
{
	struct axp20x_softc *sc = axppmic_cd.cd_devs[0];
	int flags = I2C_F_POLL;
	int ret;

	if (sc == NULL)
		return ENXIO;

	iic_acquire_bus(sc->sc_i2c, flags);
	ret = iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, data, flags);
	iic_release_bus(sc->sc_i2c, flags);

	return ret;
}

void
axp20x_shutdown(void)
{
	axp20x_writeb(AXP209_SDR, AXP209_SDR_SHUTDOWN);
}