axppmic.c revision 1.9.2.5 1 1.9.2.5 pgoyette /* $NetBSD: axppmic.c,v 1.9.2.5 2018/11/26 01:52:31 pgoyette Exp $ */
2 1.9.2.2 pgoyette
3 1.9.2.2 pgoyette /*-
4 1.9.2.2 pgoyette * Copyright (c) 2014-2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.9.2.2 pgoyette * All rights reserved.
6 1.9.2.2 pgoyette *
7 1.9.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.9.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.9.2.2 pgoyette * are met:
10 1.9.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.9.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.9.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.9.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.9.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.9.2.2 pgoyette *
16 1.9.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.9.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.9.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.9.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.9.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.9.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.9.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.9.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.9.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.9.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.9.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.9.2.2 pgoyette */
28 1.9.2.2 pgoyette
29 1.9.2.2 pgoyette #include <sys/cdefs.h>
30 1.9.2.5 pgoyette __KERNEL_RCSID(0, "$NetBSD: axppmic.c,v 1.9.2.5 2018/11/26 01:52:31 pgoyette Exp $");
31 1.9.2.2 pgoyette
32 1.9.2.2 pgoyette #include <sys/param.h>
33 1.9.2.2 pgoyette #include <sys/systm.h>
34 1.9.2.2 pgoyette #include <sys/kernel.h>
35 1.9.2.2 pgoyette #include <sys/device.h>
36 1.9.2.2 pgoyette #include <sys/conf.h>
37 1.9.2.2 pgoyette #include <sys/bus.h>
38 1.9.2.2 pgoyette #include <sys/kmem.h>
39 1.9.2.2 pgoyette
40 1.9.2.2 pgoyette #include <dev/i2c/i2cvar.h>
41 1.9.2.2 pgoyette
42 1.9.2.2 pgoyette #include <dev/sysmon/sysmonvar.h>
43 1.9.2.2 pgoyette #include <dev/sysmon/sysmon_taskq.h>
44 1.9.2.2 pgoyette
45 1.9.2.2 pgoyette #include <dev/fdt/fdtvar.h>
46 1.9.2.2 pgoyette
47 1.9.2.2 pgoyette #define AXP_POWER_SOURCE_REG 0x00
48 1.9.2.2 pgoyette #define AXP_POWER_SOURCE_ACIN_PRESENT __BIT(7)
49 1.9.2.2 pgoyette #define AXP_POWER_SOURCE_VBUS_PRESENT __BIT(5)
50 1.9.2.3 pgoyette #define AXP_POWER_SOURCE_CHARGE_DIRECTION __BIT(2)
51 1.9.2.2 pgoyette
52 1.9.2.2 pgoyette #define AXP_POWER_MODE_REG 0x01
53 1.9.2.2 pgoyette #define AXP_POWER_MODE_BATT_VALID __BIT(4)
54 1.9.2.2 pgoyette #define AXP_POWER_MODE_BATT_PRESENT __BIT(5)
55 1.9.2.2 pgoyette #define AXP_POWER_MODE_BATT_CHARGING __BIT(6)
56 1.9.2.2 pgoyette
57 1.9.2.2 pgoyette #define AXP_POWER_DISABLE_REG 0x32
58 1.9.2.2 pgoyette #define AXP_POWER_DISABLE_CTRL __BIT(7)
59 1.9.2.2 pgoyette
60 1.9.2.2 pgoyette #define AXP_IRQ_ENABLE_REG(n) (0x40 + (n) - 1)
61 1.9.2.2 pgoyette #define AXP_IRQ1_ACIN_RAISE __BIT(6)
62 1.9.2.2 pgoyette #define AXP_IRQ1_ACIN_LOWER __BIT(5)
63 1.9.2.2 pgoyette #define AXP_IRQ1_VBUS_RAISE __BIT(3)
64 1.9.2.2 pgoyette #define AXP_IRQ1_VBUS_LOWER __BIT(2)
65 1.9.2.2 pgoyette #define AXP_IRQ_STATUS_REG(n) (0x48 + (n) - 1)
66 1.9.2.2 pgoyette
67 1.9.2.3 pgoyette #define AXP_BATSENSE_HI_REG 0x78
68 1.9.2.3 pgoyette #define AXP_BATSENSE_LO_REG 0x79
69 1.9.2.3 pgoyette
70 1.9.2.3 pgoyette #define AXP_BATTCHG_HI_REG 0x7a
71 1.9.2.3 pgoyette #define AXP_BATTCHG_LO_REG 0x7b
72 1.9.2.3 pgoyette
73 1.9.2.3 pgoyette #define AXP_BATTDISCHG_HI_REG 0x7c
74 1.9.2.3 pgoyette #define AXP_BATTDISCHG_LO_REG 0x7d
75 1.9.2.3 pgoyette
76 1.9.2.3 pgoyette #define AXP_ADC_RAW(_hi, _lo) \
77 1.9.2.5 pgoyette (((u_int)(_hi) << 4) | ((_lo) & 0xf))
78 1.9.2.3 pgoyette
79 1.9.2.2 pgoyette #define AXP_FUEL_GAUGE_CTRL_REG 0xb8
80 1.9.2.2 pgoyette #define AXP_FUEL_GAUGE_CTRL_EN __BIT(7)
81 1.9.2.3 pgoyette
82 1.9.2.2 pgoyette #define AXP_BATT_CAP_REG 0xb9
83 1.9.2.2 pgoyette #define AXP_BATT_CAP_VALID __BIT(7)
84 1.9.2.2 pgoyette #define AXP_BATT_CAP_PERCENT __BITS(6,0)
85 1.9.2.2 pgoyette
86 1.9.2.5 pgoyette #define AXP_BATT_MAX_CAP_HI_REG 0xe0
87 1.9.2.5 pgoyette #define AXP_BATT_MAX_CAP_VALID __BIT(7)
88 1.9.2.5 pgoyette #define AXP_BATT_MAX_CAP_LO_REG 0xe1
89 1.9.2.5 pgoyette
90 1.9.2.5 pgoyette #define AXP_BATT_COULOMB_HI_REG 0xe2
91 1.9.2.5 pgoyette #define AXP_BATT_COULOMB_VALID __BIT(7)
92 1.9.2.5 pgoyette #define AXP_BATT_COULOMB_LO_REG 0xe3
93 1.9.2.5 pgoyette
94 1.9.2.5 pgoyette #define AXP_COULOMB_RAW(_hi, _lo) \
95 1.9.2.5 pgoyette (((u_int)(_hi & ~__BIT(7)) << 8) | (_lo))
96 1.9.2.5 pgoyette
97 1.9.2.2 pgoyette #define AXP_BATT_CAP_WARN_REG 0xe6
98 1.9.2.2 pgoyette #define AXP_BATT_CAP_WARN_LV1 __BITS(7,4)
99 1.9.2.2 pgoyette #define AXP_BATT_CAP_WARN_LV2 __BITS(3,0)
100 1.9.2.2 pgoyette
101 1.9.2.2 pgoyette struct axppmic_ctrl {
102 1.9.2.2 pgoyette device_t c_dev;
103 1.9.2.2 pgoyette
104 1.9.2.2 pgoyette const char * c_name;
105 1.9.2.2 pgoyette u_int c_min;
106 1.9.2.2 pgoyette u_int c_max;
107 1.9.2.2 pgoyette u_int c_step1;
108 1.9.2.2 pgoyette u_int c_step1cnt;
109 1.9.2.2 pgoyette u_int c_step2;
110 1.9.2.2 pgoyette u_int c_step2cnt;
111 1.9.2.2 pgoyette
112 1.9.2.2 pgoyette uint8_t c_enable_reg;
113 1.9.2.2 pgoyette uint8_t c_enable_mask;
114 1.9.2.2 pgoyette
115 1.9.2.2 pgoyette uint8_t c_voltage_reg;
116 1.9.2.2 pgoyette uint8_t c_voltage_mask;
117 1.9.2.2 pgoyette };
118 1.9.2.2 pgoyette
119 1.9.2.2 pgoyette #define AXP_CTRL(name, min, max, step, ereg, emask, vreg, vmask) \
120 1.9.2.2 pgoyette { .c_name = (name), .c_min = (min), .c_max = (max), \
121 1.9.2.2 pgoyette .c_step1 = (step), .c_step1cnt = (((max) - (min)) / (step)) + 1, \
122 1.9.2.2 pgoyette .c_step2 = 0, .c_step2cnt = 0, \
123 1.9.2.2 pgoyette .c_enable_reg = (ereg), .c_enable_mask = (emask), \
124 1.9.2.2 pgoyette .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
125 1.9.2.2 pgoyette
126 1.9.2.2 pgoyette #define AXP_CTRL2(name, min, max, step1, step1cnt, step2, step2cnt, ereg, emask, vreg, vmask) \
127 1.9.2.2 pgoyette { .c_name = (name), .c_min = (min), .c_max = (max), \
128 1.9.2.2 pgoyette .c_step1 = (step1), .c_step1cnt = (step1cnt), \
129 1.9.2.2 pgoyette .c_step2 = (step2), .c_step2cnt = (step2cnt), \
130 1.9.2.2 pgoyette .c_enable_reg = (ereg), .c_enable_mask = (emask), \
131 1.9.2.2 pgoyette .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
132 1.9.2.2 pgoyette
133 1.9.2.2 pgoyette static const struct axppmic_ctrl axp803_ctrls[] = {
134 1.9.2.2 pgoyette AXP_CTRL("dldo1", 700, 3300, 100,
135 1.9.2.2 pgoyette 0x12, __BIT(3), 0x15, __BITS(4,0)),
136 1.9.2.2 pgoyette AXP_CTRL2("dldo2", 700, 4200, 100, 28, 200, 4,
137 1.9.2.2 pgoyette 0x12, __BIT(4), 0x16, __BITS(4,0)),
138 1.9.2.2 pgoyette AXP_CTRL("dldo3", 700, 3300, 100,
139 1.9.2.2 pgoyette 0x12, __BIT(5), 0x17, __BITS(4,0)),
140 1.9.2.2 pgoyette AXP_CTRL("dldo4", 700, 3300, 100,
141 1.9.2.2 pgoyette 0x12, __BIT(6), 0x18, __BITS(4,0)),
142 1.9.2.2 pgoyette AXP_CTRL("eldo1", 700, 1900, 50,
143 1.9.2.2 pgoyette 0x12, __BIT(0), 0x19, __BITS(4,0)),
144 1.9.2.2 pgoyette AXP_CTRL("eldo2", 700, 1900, 50,
145 1.9.2.2 pgoyette 0x12, __BIT(1), 0x1a, __BITS(4,0)),
146 1.9.2.2 pgoyette AXP_CTRL("eldo3", 700, 1900, 50,
147 1.9.2.2 pgoyette 0x12, __BIT(2), 0x1b, __BITS(4,0)),
148 1.9.2.2 pgoyette AXP_CTRL("fldo1", 700, 1450, 50,
149 1.9.2.2 pgoyette 0x13, __BIT(2), 0x1c, __BITS(3,0)),
150 1.9.2.2 pgoyette AXP_CTRL("fldo2", 700, 1450, 50,
151 1.9.2.2 pgoyette 0x13, __BIT(3), 0x1d, __BITS(3,0)),
152 1.9.2.2 pgoyette AXP_CTRL("dcdc1", 1600, 3400, 100,
153 1.9.2.2 pgoyette 0x10, __BIT(0), 0x20, __BITS(4,0)),
154 1.9.2.2 pgoyette AXP_CTRL2("dcdc2", 500, 1300, 10, 70, 20, 5,
155 1.9.2.2 pgoyette 0x10, __BIT(1), 0x21, __BITS(6,0)),
156 1.9.2.2 pgoyette AXP_CTRL2("dcdc3", 500, 1300, 10, 70, 20, 5,
157 1.9.2.2 pgoyette 0x10, __BIT(2), 0x22, __BITS(6,0)),
158 1.9.2.2 pgoyette AXP_CTRL2("dcdc4", 500, 1300, 10, 70, 20, 5,
159 1.9.2.2 pgoyette 0x10, __BIT(3), 0x23, __BITS(6,0)),
160 1.9.2.2 pgoyette AXP_CTRL2("dcdc5", 800, 1840, 10, 33, 20, 36,
161 1.9.2.2 pgoyette 0x10, __BIT(4), 0x24, __BITS(6,0)),
162 1.9.2.2 pgoyette AXP_CTRL2("dcdc6", 600, 1520, 10, 51, 20, 21,
163 1.9.2.2 pgoyette 0x10, __BIT(5), 0x25, __BITS(6,0)),
164 1.9.2.2 pgoyette AXP_CTRL("aldo1", 700, 3300, 100,
165 1.9.2.2 pgoyette 0x13, __BIT(5), 0x28, __BITS(4,0)),
166 1.9.2.2 pgoyette AXP_CTRL("aldo2", 700, 3300, 100,
167 1.9.2.2 pgoyette 0x13, __BIT(6), 0x29, __BITS(4,0)),
168 1.9.2.2 pgoyette AXP_CTRL("aldo3", 700, 3300, 100,
169 1.9.2.2 pgoyette 0x13, __BIT(7), 0x2a, __BITS(4,0)),
170 1.9.2.2 pgoyette };
171 1.9.2.2 pgoyette
172 1.9.2.2 pgoyette static const struct axppmic_ctrl axp805_ctrls[] = {
173 1.9.2.2 pgoyette AXP_CTRL2("dcdca", 600, 1520, 10, 51, 20, 21,
174 1.9.2.2 pgoyette 0x10, __BIT(0), 0x12, __BITS(6,0)),
175 1.9.2.2 pgoyette AXP_CTRL("dcdcb", 1000, 2550, 50,
176 1.9.2.2 pgoyette 0x10, __BIT(1), 0x13, __BITS(4,0)),
177 1.9.2.2 pgoyette AXP_CTRL2("dcdcc", 600, 1520, 10, 51, 20, 21,
178 1.9.2.2 pgoyette 0x10, __BIT(2), 0x14, __BITS(6,0)),
179 1.9.2.2 pgoyette AXP_CTRL2("dcdcd", 600, 3300, 20, 46, 100, 18,
180 1.9.2.2 pgoyette 0x10, __BIT(3), 0x15, __BITS(5,0)),
181 1.9.2.2 pgoyette AXP_CTRL("dcdce", 1100, 3400, 100,
182 1.9.2.2 pgoyette 0x10, __BIT(4), 0x16, __BITS(4,0)),
183 1.9.2.2 pgoyette AXP_CTRL("aldo1", 700, 3300, 100,
184 1.9.2.2 pgoyette 0x10, __BIT(5), 0x17, __BITS(4,0)),
185 1.9.2.2 pgoyette AXP_CTRL("aldo2", 700, 3400, 100,
186 1.9.2.2 pgoyette 0x10, __BIT(6), 0x18, __BITS(4,0)),
187 1.9.2.2 pgoyette AXP_CTRL("aldo3", 700, 3300, 100,
188 1.9.2.2 pgoyette 0x10, __BIT(7), 0x19, __BITS(4,0)),
189 1.9.2.2 pgoyette AXP_CTRL("bldo1", 700, 1900, 100,
190 1.9.2.2 pgoyette 0x11, __BIT(0), 0x20, __BITS(3,0)),
191 1.9.2.2 pgoyette AXP_CTRL("bldo2", 700, 1900, 100,
192 1.9.2.2 pgoyette 0x11, __BIT(1), 0x21, __BITS(3,0)),
193 1.9.2.2 pgoyette AXP_CTRL("bldo3", 700, 1900, 100,
194 1.9.2.2 pgoyette 0x11, __BIT(2), 0x22, __BITS(3,0)),
195 1.9.2.2 pgoyette AXP_CTRL("bldo4", 700, 1900, 100,
196 1.9.2.2 pgoyette 0x11, __BIT(3), 0x23, __BITS(3,0)),
197 1.9.2.2 pgoyette AXP_CTRL("cldo1", 700, 3300, 100,
198 1.9.2.2 pgoyette 0x11, __BIT(4), 0x24, __BITS(4,0)),
199 1.9.2.2 pgoyette AXP_CTRL2("cldo2", 700, 4200, 100, 28, 200, 4,
200 1.9.2.2 pgoyette 0x11, __BIT(5), 0x25, __BITS(4,0)),
201 1.9.2.2 pgoyette AXP_CTRL("cldo3", 700, 3300, 100,
202 1.9.2.2 pgoyette 0x11, __BIT(6), 0x26, __BITS(4,0)),
203 1.9.2.2 pgoyette };
204 1.9.2.2 pgoyette
205 1.9.2.2 pgoyette struct axppmic_irq {
206 1.9.2.2 pgoyette u_int reg;
207 1.9.2.2 pgoyette uint8_t mask;
208 1.9.2.2 pgoyette };
209 1.9.2.2 pgoyette
210 1.9.2.2 pgoyette #define AXPPMIC_IRQ(_reg, _mask) \
211 1.9.2.2 pgoyette { .reg = (_reg), .mask = (_mask) }
212 1.9.2.2 pgoyette
213 1.9.2.2 pgoyette struct axppmic_config {
214 1.9.2.2 pgoyette const char *name;
215 1.9.2.2 pgoyette const struct axppmic_ctrl *controls;
216 1.9.2.2 pgoyette u_int ncontrols;
217 1.9.2.2 pgoyette u_int irq_regs;
218 1.9.2.2 pgoyette bool has_battery;
219 1.9.2.2 pgoyette bool has_fuel_gauge;
220 1.9.2.2 pgoyette struct axppmic_irq poklirq;
221 1.9.2.2 pgoyette struct axppmic_irq acinirq;
222 1.9.2.2 pgoyette struct axppmic_irq vbusirq;
223 1.9.2.2 pgoyette struct axppmic_irq battirq;
224 1.9.2.2 pgoyette struct axppmic_irq chargeirq;
225 1.9.2.2 pgoyette struct axppmic_irq chargestirq;
226 1.9.2.3 pgoyette u_int batsense_step; /* uV */
227 1.9.2.3 pgoyette u_int charge_step; /* uA */
228 1.9.2.3 pgoyette u_int discharge_step; /* uA */
229 1.9.2.3 pgoyette u_int maxcap_step; /* uAh */
230 1.9.2.3 pgoyette u_int coulomb_step; /* uAh */
231 1.9.2.2 pgoyette };
232 1.9.2.2 pgoyette
233 1.9.2.2 pgoyette enum axppmic_sensor {
234 1.9.2.2 pgoyette AXP_SENSOR_ACIN_PRESENT,
235 1.9.2.2 pgoyette AXP_SENSOR_VBUS_PRESENT,
236 1.9.2.2 pgoyette AXP_SENSOR_BATT_PRESENT,
237 1.9.2.2 pgoyette AXP_SENSOR_BATT_CHARGING,
238 1.9.2.2 pgoyette AXP_SENSOR_BATT_CHARGE_STATE,
239 1.9.2.3 pgoyette AXP_SENSOR_BATT_VOLTAGE,
240 1.9.2.3 pgoyette AXP_SENSOR_BATT_CHARGE_CURRENT,
241 1.9.2.3 pgoyette AXP_SENSOR_BATT_DISCHARGE_CURRENT,
242 1.9.2.3 pgoyette AXP_SENSOR_BATT_CAPACITY_PERCENT,
243 1.9.2.5 pgoyette AXP_SENSOR_BATT_MAXIMUM_CAPACITY,
244 1.9.2.5 pgoyette AXP_SENSOR_BATT_CURRENT_CAPACITY,
245 1.9.2.2 pgoyette AXP_NSENSORS
246 1.9.2.2 pgoyette };
247 1.9.2.2 pgoyette
248 1.9.2.2 pgoyette struct axppmic_softc {
249 1.9.2.2 pgoyette device_t sc_dev;
250 1.9.2.2 pgoyette i2c_tag_t sc_i2c;
251 1.9.2.2 pgoyette i2c_addr_t sc_addr;
252 1.9.2.2 pgoyette int sc_phandle;
253 1.9.2.2 pgoyette
254 1.9.2.2 pgoyette const struct axppmic_config *sc_conf;
255 1.9.2.2 pgoyette
256 1.9.2.2 pgoyette struct sysmon_pswitch sc_smpsw;
257 1.9.2.2 pgoyette
258 1.9.2.2 pgoyette struct sysmon_envsys *sc_sme;
259 1.9.2.2 pgoyette
260 1.9.2.2 pgoyette envsys_data_t sc_sensor[AXP_NSENSORS];
261 1.9.2.2 pgoyette
262 1.9.2.2 pgoyette u_int sc_warn_thres;
263 1.9.2.2 pgoyette u_int sc_shut_thres;
264 1.9.2.2 pgoyette };
265 1.9.2.2 pgoyette
266 1.9.2.2 pgoyette struct axpreg_softc {
267 1.9.2.2 pgoyette device_t sc_dev;
268 1.9.2.2 pgoyette i2c_tag_t sc_i2c;
269 1.9.2.2 pgoyette i2c_addr_t sc_addr;
270 1.9.2.2 pgoyette const struct axppmic_ctrl *sc_ctrl;
271 1.9.2.2 pgoyette };
272 1.9.2.2 pgoyette
273 1.9.2.2 pgoyette struct axpreg_attach_args {
274 1.9.2.2 pgoyette const struct axppmic_ctrl *reg_ctrl;
275 1.9.2.2 pgoyette int reg_phandle;
276 1.9.2.2 pgoyette i2c_tag_t reg_i2c;
277 1.9.2.2 pgoyette i2c_addr_t reg_addr;
278 1.9.2.2 pgoyette };
279 1.9.2.2 pgoyette
280 1.9.2.2 pgoyette static const struct axppmic_config axp803_config = {
281 1.9.2.2 pgoyette .name = "AXP803",
282 1.9.2.2 pgoyette .controls = axp803_ctrls,
283 1.9.2.2 pgoyette .ncontrols = __arraycount(axp803_ctrls),
284 1.9.2.2 pgoyette .irq_regs = 6,
285 1.9.2.2 pgoyette .has_battery = true,
286 1.9.2.2 pgoyette .has_fuel_gauge = true,
287 1.9.2.3 pgoyette .batsense_step = 1100,
288 1.9.2.3 pgoyette .charge_step = 1000,
289 1.9.2.3 pgoyette .discharge_step = 1000,
290 1.9.2.5 pgoyette .maxcap_step = 1456,
291 1.9.2.5 pgoyette .coulomb_step = 1456,
292 1.9.2.2 pgoyette .poklirq = AXPPMIC_IRQ(5, __BIT(3)),
293 1.9.2.2 pgoyette .acinirq = AXPPMIC_IRQ(1, __BITS(6,5)),
294 1.9.2.2 pgoyette .vbusirq = AXPPMIC_IRQ(1, __BITS(3,2)),
295 1.9.2.2 pgoyette .battirq = AXPPMIC_IRQ(2, __BITS(7,6)),
296 1.9.2.2 pgoyette .chargeirq = AXPPMIC_IRQ(2, __BITS(3,2)),
297 1.9.2.2 pgoyette .chargestirq = AXPPMIC_IRQ(4, __BITS(1,0)),
298 1.9.2.2 pgoyette };
299 1.9.2.2 pgoyette
300 1.9.2.2 pgoyette static const struct axppmic_config axp805_config = {
301 1.9.2.2 pgoyette .name = "AXP805/806",
302 1.9.2.2 pgoyette .controls = axp805_ctrls,
303 1.9.2.2 pgoyette .ncontrols = __arraycount(axp805_ctrls),
304 1.9.2.2 pgoyette .irq_regs = 2,
305 1.9.2.2 pgoyette .poklirq = AXPPMIC_IRQ(2, __BIT(0)),
306 1.9.2.2 pgoyette };
307 1.9.2.2 pgoyette
308 1.9.2.4 pgoyette static const struct device_compatible_entry compat_data[] = {
309 1.9.2.4 pgoyette { "x-powers,axp803", (uintptr_t)&axp803_config },
310 1.9.2.4 pgoyette { "x-powers,axp805", (uintptr_t)&axp805_config },
311 1.9.2.4 pgoyette { "x-powers,axp806", (uintptr_t)&axp805_config },
312 1.9.2.4 pgoyette { NULL, 0 }
313 1.9.2.2 pgoyette };
314 1.9.2.2 pgoyette
315 1.9.2.2 pgoyette static int
316 1.9.2.2 pgoyette axppmic_read(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, uint8_t *val, int flags)
317 1.9.2.2 pgoyette {
318 1.9.2.2 pgoyette return iic_smbus_read_byte(tag, addr, reg, val, flags);
319 1.9.2.2 pgoyette }
320 1.9.2.2 pgoyette
321 1.9.2.2 pgoyette static int
322 1.9.2.2 pgoyette axppmic_write(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, uint8_t val, int flags)
323 1.9.2.2 pgoyette {
324 1.9.2.2 pgoyette return iic_smbus_write_byte(tag, addr, reg, val, flags);
325 1.9.2.2 pgoyette }
326 1.9.2.2 pgoyette
327 1.9.2.2 pgoyette static int
328 1.9.2.2 pgoyette axppmic_set_voltage(i2c_tag_t tag, i2c_addr_t addr, const struct axppmic_ctrl *c, u_int min, u_int max)
329 1.9.2.2 pgoyette {
330 1.9.2.2 pgoyette const int flags = (cold ? I2C_F_POLL : 0);
331 1.9.2.2 pgoyette u_int vol, reg_val;
332 1.9.2.2 pgoyette int nstep, error;
333 1.9.2.2 pgoyette uint8_t val;
334 1.9.2.2 pgoyette
335 1.9.2.2 pgoyette if (!c->c_voltage_mask)
336 1.9.2.2 pgoyette return EINVAL;
337 1.9.2.2 pgoyette
338 1.9.2.2 pgoyette if (min < c->c_min || min > c->c_max)
339 1.9.2.2 pgoyette return EINVAL;
340 1.9.2.2 pgoyette
341 1.9.2.2 pgoyette reg_val = 0;
342 1.9.2.2 pgoyette nstep = 1;
343 1.9.2.2 pgoyette vol = c->c_min;
344 1.9.2.2 pgoyette
345 1.9.2.2 pgoyette for (nstep = 0; nstep < c->c_step1cnt && vol < min; nstep++) {
346 1.9.2.2 pgoyette ++reg_val;
347 1.9.2.2 pgoyette vol += c->c_step1;
348 1.9.2.2 pgoyette }
349 1.9.2.2 pgoyette for (nstep = 0; nstep < c->c_step2cnt && vol < min; nstep++) {
350 1.9.2.2 pgoyette ++reg_val;
351 1.9.2.2 pgoyette vol += c->c_step2;
352 1.9.2.2 pgoyette }
353 1.9.2.2 pgoyette
354 1.9.2.2 pgoyette if (vol > max)
355 1.9.2.2 pgoyette return EINVAL;
356 1.9.2.2 pgoyette
357 1.9.2.2 pgoyette iic_acquire_bus(tag, flags);
358 1.9.2.2 pgoyette if ((error = axppmic_read(tag, addr, c->c_voltage_reg, &val, flags)) == 0) {
359 1.9.2.2 pgoyette val &= ~c->c_voltage_mask;
360 1.9.2.2 pgoyette val |= __SHIFTIN(reg_val, c->c_voltage_mask);
361 1.9.2.2 pgoyette error = axppmic_write(tag, addr, c->c_voltage_reg, val, flags);
362 1.9.2.2 pgoyette }
363 1.9.2.2 pgoyette iic_release_bus(tag, flags);
364 1.9.2.2 pgoyette
365 1.9.2.2 pgoyette return error;
366 1.9.2.2 pgoyette }
367 1.9.2.2 pgoyette
368 1.9.2.2 pgoyette static int
369 1.9.2.2 pgoyette axppmic_get_voltage(i2c_tag_t tag, i2c_addr_t addr, const struct axppmic_ctrl *c, u_int *pvol)
370 1.9.2.2 pgoyette {
371 1.9.2.2 pgoyette const int flags = (cold ? I2C_F_POLL : 0);
372 1.9.2.2 pgoyette int reg_val, error;
373 1.9.2.2 pgoyette uint8_t val;
374 1.9.2.2 pgoyette
375 1.9.2.2 pgoyette if (!c->c_voltage_mask)
376 1.9.2.2 pgoyette return EINVAL;
377 1.9.2.2 pgoyette
378 1.9.2.2 pgoyette iic_acquire_bus(tag, flags);
379 1.9.2.2 pgoyette error = axppmic_read(tag, addr, c->c_voltage_reg, &val, flags);
380 1.9.2.2 pgoyette iic_release_bus(tag, flags);
381 1.9.2.2 pgoyette if (error)
382 1.9.2.2 pgoyette return error;
383 1.9.2.2 pgoyette
384 1.9.2.2 pgoyette reg_val = __SHIFTOUT(val, c->c_voltage_mask);
385 1.9.2.2 pgoyette if (reg_val < c->c_step1cnt) {
386 1.9.2.2 pgoyette *pvol = c->c_min + reg_val * c->c_step1;
387 1.9.2.2 pgoyette } else {
388 1.9.2.2 pgoyette *pvol = c->c_min + (c->c_step1cnt * c->c_step1) +
389 1.9.2.2 pgoyette ((reg_val - c->c_step1cnt) * c->c_step2);
390 1.9.2.2 pgoyette }
391 1.9.2.2 pgoyette
392 1.9.2.2 pgoyette return 0;
393 1.9.2.2 pgoyette }
394 1.9.2.2 pgoyette
395 1.9.2.2 pgoyette static void
396 1.9.2.2 pgoyette axppmic_power_poweroff(device_t dev)
397 1.9.2.2 pgoyette {
398 1.9.2.2 pgoyette struct axppmic_softc *sc = device_private(dev);
399 1.9.2.2 pgoyette
400 1.9.2.2 pgoyette delay(1000000);
401 1.9.2.2 pgoyette
402 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
403 1.9.2.2 pgoyette axppmic_write(sc->sc_i2c, sc->sc_addr, AXP_POWER_DISABLE_REG, AXP_POWER_DISABLE_CTRL, I2C_F_POLL);
404 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, I2C_F_POLL);
405 1.9.2.2 pgoyette }
406 1.9.2.2 pgoyette
407 1.9.2.2 pgoyette static struct fdtbus_power_controller_func axppmic_power_funcs = {
408 1.9.2.2 pgoyette .poweroff = axppmic_power_poweroff,
409 1.9.2.2 pgoyette };
410 1.9.2.2 pgoyette
411 1.9.2.2 pgoyette static void
412 1.9.2.2 pgoyette axppmic_task_shut(void *priv)
413 1.9.2.2 pgoyette {
414 1.9.2.2 pgoyette struct axppmic_softc *sc = priv;
415 1.9.2.2 pgoyette
416 1.9.2.2 pgoyette sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
417 1.9.2.2 pgoyette }
418 1.9.2.2 pgoyette
419 1.9.2.2 pgoyette static void
420 1.9.2.2 pgoyette axppmic_sensor_update(struct sysmon_envsys *sme, envsys_data_t *e)
421 1.9.2.2 pgoyette {
422 1.9.2.2 pgoyette struct axppmic_softc *sc = sme->sme_cookie;
423 1.9.2.3 pgoyette const struct axppmic_config *c = sc->sc_conf;
424 1.9.2.2 pgoyette const int flags = I2C_F_POLL;
425 1.9.2.3 pgoyette uint8_t val, lo, hi;
426 1.9.2.2 pgoyette
427 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
428 1.9.2.2 pgoyette
429 1.9.2.3 pgoyette const bool battery_present =
430 1.9.2.3 pgoyette sc->sc_sensor[AXP_SENSOR_BATT_PRESENT].state == ENVSYS_SVALID &&
431 1.9.2.3 pgoyette sc->sc_sensor[AXP_SENSOR_BATT_PRESENT].value_cur == 1;
432 1.9.2.3 pgoyette
433 1.9.2.2 pgoyette switch (e->private) {
434 1.9.2.2 pgoyette case AXP_SENSOR_ACIN_PRESENT:
435 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0) {
436 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
437 1.9.2.2 pgoyette e->value_cur = !!(val & AXP_POWER_SOURCE_ACIN_PRESENT);
438 1.9.2.2 pgoyette }
439 1.9.2.2 pgoyette break;
440 1.9.2.2 pgoyette case AXP_SENSOR_VBUS_PRESENT:
441 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0) {
442 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
443 1.9.2.2 pgoyette e->value_cur = !!(val & AXP_POWER_SOURCE_VBUS_PRESENT);
444 1.9.2.2 pgoyette }
445 1.9.2.2 pgoyette break;
446 1.9.2.2 pgoyette case AXP_SENSOR_BATT_PRESENT:
447 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, flags) == 0) {
448 1.9.2.2 pgoyette if (val & AXP_POWER_MODE_BATT_VALID) {
449 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
450 1.9.2.2 pgoyette e->value_cur = !!(val & AXP_POWER_MODE_BATT_PRESENT);
451 1.9.2.2 pgoyette }
452 1.9.2.2 pgoyette }
453 1.9.2.2 pgoyette break;
454 1.9.2.2 pgoyette case AXP_SENSOR_BATT_CHARGING:
455 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, flags) == 0) {
456 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
457 1.9.2.2 pgoyette e->value_cur = !!(val & AXP_POWER_MODE_BATT_CHARGING);
458 1.9.2.2 pgoyette }
459 1.9.2.2 pgoyette break;
460 1.9.2.2 pgoyette case AXP_SENSOR_BATT_CHARGE_STATE:
461 1.9.2.3 pgoyette if (battery_present &&
462 1.9.2.2 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_REG, &val, flags) == 0 &&
463 1.9.2.2 pgoyette (val & AXP_BATT_CAP_VALID) != 0) {
464 1.9.2.2 pgoyette const u_int batt_val = __SHIFTOUT(val, AXP_BATT_CAP_PERCENT);
465 1.9.2.2 pgoyette if (batt_val <= sc->sc_shut_thres) {
466 1.9.2.2 pgoyette e->state = ENVSYS_SCRITICAL;
467 1.9.2.2 pgoyette e->value_cur = ENVSYS_BATTERY_CAPACITY_CRITICAL;
468 1.9.2.2 pgoyette } else if (batt_val <= sc->sc_warn_thres) {
469 1.9.2.2 pgoyette e->state = ENVSYS_SWARNUNDER;
470 1.9.2.2 pgoyette e->value_cur = ENVSYS_BATTERY_CAPACITY_WARNING;
471 1.9.2.2 pgoyette } else {
472 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
473 1.9.2.2 pgoyette e->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
474 1.9.2.2 pgoyette }
475 1.9.2.2 pgoyette }
476 1.9.2.2 pgoyette break;
477 1.9.2.3 pgoyette case AXP_SENSOR_BATT_CAPACITY_PERCENT:
478 1.9.2.3 pgoyette if (battery_present &&
479 1.9.2.2 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_REG, &val, flags) == 0 &&
480 1.9.2.2 pgoyette (val & AXP_BATT_CAP_VALID) != 0) {
481 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
482 1.9.2.2 pgoyette e->value_cur = __SHIFTOUT(val, AXP_BATT_CAP_PERCENT);
483 1.9.2.2 pgoyette }
484 1.9.2.2 pgoyette break;
485 1.9.2.3 pgoyette case AXP_SENSOR_BATT_VOLTAGE:
486 1.9.2.3 pgoyette if (battery_present &&
487 1.9.2.3 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATSENSE_HI_REG, &hi, flags) == 0 &&
488 1.9.2.3 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATSENSE_LO_REG, &lo, flags) == 0) {
489 1.9.2.3 pgoyette e->state = ENVSYS_SVALID;
490 1.9.2.3 pgoyette e->value_cur = AXP_ADC_RAW(hi, lo) * c->batsense_step;
491 1.9.2.3 pgoyette }
492 1.9.2.3 pgoyette break;
493 1.9.2.3 pgoyette case AXP_SENSOR_BATT_CHARGE_CURRENT:
494 1.9.2.3 pgoyette if (battery_present &&
495 1.9.2.3 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0 &&
496 1.9.2.3 pgoyette (val & AXP_POWER_SOURCE_CHARGE_DIRECTION) != 0 &&
497 1.9.2.3 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTCHG_HI_REG, &hi, flags) == 0 &&
498 1.9.2.3 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTCHG_LO_REG, &lo, flags) == 0) {
499 1.9.2.3 pgoyette e->state = ENVSYS_SVALID;
500 1.9.2.3 pgoyette e->value_cur = AXP_ADC_RAW(hi, lo) * c->charge_step;
501 1.9.2.3 pgoyette }
502 1.9.2.3 pgoyette break;
503 1.9.2.3 pgoyette case AXP_SENSOR_BATT_DISCHARGE_CURRENT:
504 1.9.2.3 pgoyette if (battery_present &&
505 1.9.2.3 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0 &&
506 1.9.2.3 pgoyette (val & AXP_POWER_SOURCE_CHARGE_DIRECTION) == 0 &&
507 1.9.2.3 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTDISCHG_HI_REG, &hi, flags) == 0 &&
508 1.9.2.3 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTDISCHG_LO_REG, &lo, flags) == 0) {
509 1.9.2.3 pgoyette e->state = ENVSYS_SVALID;
510 1.9.2.3 pgoyette e->value_cur = AXP_ADC_RAW(hi, lo) * c->discharge_step;
511 1.9.2.3 pgoyette }
512 1.9.2.3 pgoyette break;
513 1.9.2.5 pgoyette case AXP_SENSOR_BATT_MAXIMUM_CAPACITY:
514 1.9.2.5 pgoyette if (battery_present &&
515 1.9.2.5 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_MAX_CAP_HI_REG, &hi, flags) == 0 &&
516 1.9.2.5 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_MAX_CAP_LO_REG, &lo, flags) == 0) {
517 1.9.2.5 pgoyette e->state = (hi & AXP_BATT_MAX_CAP_VALID) ? ENVSYS_SVALID : ENVSYS_SINVALID;
518 1.9.2.5 pgoyette e->value_cur = AXP_COULOMB_RAW(hi, lo) * c->maxcap_step;
519 1.9.2.5 pgoyette }
520 1.9.2.5 pgoyette break;
521 1.9.2.5 pgoyette case AXP_SENSOR_BATT_CURRENT_CAPACITY:
522 1.9.2.5 pgoyette if (battery_present &&
523 1.9.2.5 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_COULOMB_HI_REG, &hi, flags) == 0 &&
524 1.9.2.5 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_COULOMB_LO_REG, &lo, flags) == 0) {
525 1.9.2.5 pgoyette e->state = (hi & AXP_BATT_COULOMB_VALID) ? ENVSYS_SVALID : ENVSYS_SINVALID;
526 1.9.2.5 pgoyette e->value_cur = AXP_COULOMB_RAW(hi, lo) * c->coulomb_step;
527 1.9.2.5 pgoyette }
528 1.9.2.5 pgoyette break;
529 1.9.2.2 pgoyette }
530 1.9.2.2 pgoyette }
531 1.9.2.2 pgoyette
532 1.9.2.2 pgoyette static void
533 1.9.2.2 pgoyette axppmic_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *e)
534 1.9.2.2 pgoyette {
535 1.9.2.2 pgoyette struct axppmic_softc *sc = sme->sme_cookie;
536 1.9.2.2 pgoyette const int flags = I2C_F_POLL;
537 1.9.2.2 pgoyette
538 1.9.2.2 pgoyette switch (e->private) {
539 1.9.2.3 pgoyette case AXP_SENSOR_BATT_CAPACITY_PERCENT:
540 1.9.2.3 pgoyette case AXP_SENSOR_BATT_VOLTAGE:
541 1.9.2.3 pgoyette case AXP_SENSOR_BATT_CHARGE_CURRENT:
542 1.9.2.3 pgoyette case AXP_SENSOR_BATT_DISCHARGE_CURRENT:
543 1.9.2.3 pgoyette /* Always update battery capacity and ADCs */
544 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, flags);
545 1.9.2.2 pgoyette axppmic_sensor_update(sme, e);
546 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, flags);
547 1.9.2.2 pgoyette break;
548 1.9.2.2 pgoyette default:
549 1.9.2.2 pgoyette /* Refresh if the sensor is not in valid state */
550 1.9.2.2 pgoyette if (e->state != ENVSYS_SVALID) {
551 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, flags);
552 1.9.2.2 pgoyette axppmic_sensor_update(sme, e);
553 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, flags);
554 1.9.2.2 pgoyette }
555 1.9.2.2 pgoyette break;
556 1.9.2.2 pgoyette }
557 1.9.2.2 pgoyette }
558 1.9.2.2 pgoyette
559 1.9.2.2 pgoyette static int
560 1.9.2.2 pgoyette axppmic_intr(void *priv)
561 1.9.2.2 pgoyette {
562 1.9.2.2 pgoyette struct axppmic_softc *sc = priv;
563 1.9.2.2 pgoyette const struct axppmic_config *c = sc->sc_conf;
564 1.9.2.2 pgoyette const int flags = I2C_F_POLL;
565 1.9.2.2 pgoyette uint8_t stat;
566 1.9.2.2 pgoyette u_int n;
567 1.9.2.2 pgoyette
568 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, flags);
569 1.9.2.2 pgoyette for (n = 1; n <= c->irq_regs; n++) {
570 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_IRQ_STATUS_REG(n), &stat, flags) == 0) {
571 1.9.2.2 pgoyette if (n == c->poklirq.reg && (stat & c->poklirq.mask) != 0)
572 1.9.2.2 pgoyette sysmon_task_queue_sched(0, axppmic_task_shut, sc);
573 1.9.2.2 pgoyette if (n == c->acinirq.reg && (stat & c->acinirq.mask) != 0)
574 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_ACIN_PRESENT]);
575 1.9.2.2 pgoyette if (n == c->vbusirq.reg && (stat & c->vbusirq.mask) != 0)
576 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_VBUS_PRESENT]);
577 1.9.2.2 pgoyette if (n == c->battirq.reg && (stat & c->battirq.mask) != 0)
578 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_PRESENT]);
579 1.9.2.2 pgoyette if (n == c->chargeirq.reg && (stat & c->chargeirq.mask) != 0)
580 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_CHARGING]);
581 1.9.2.2 pgoyette if (n == c->chargestirq.reg && (stat & c->chargestirq.mask) != 0)
582 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_STATE]);
583 1.9.2.2 pgoyette
584 1.9.2.2 pgoyette if (stat != 0)
585 1.9.2.2 pgoyette axppmic_write(sc->sc_i2c, sc->sc_addr,
586 1.9.2.2 pgoyette AXP_IRQ_STATUS_REG(n), stat, flags);
587 1.9.2.2 pgoyette }
588 1.9.2.2 pgoyette }
589 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, flags);
590 1.9.2.2 pgoyette
591 1.9.2.2 pgoyette return 1;
592 1.9.2.2 pgoyette }
593 1.9.2.2 pgoyette
594 1.9.2.2 pgoyette static void
595 1.9.2.2 pgoyette axppmic_attach_acadapter(struct axppmic_softc *sc)
596 1.9.2.2 pgoyette {
597 1.9.2.2 pgoyette envsys_data_t *e;
598 1.9.2.2 pgoyette
599 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_ACIN_PRESENT];
600 1.9.2.2 pgoyette e->private = AXP_SENSOR_ACIN_PRESENT;
601 1.9.2.2 pgoyette e->units = ENVSYS_INDICATOR;
602 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
603 1.9.2.2 pgoyette strlcpy(e->desc, "ACIN present", sizeof(e->desc));
604 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
605 1.9.2.2 pgoyette
606 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_VBUS_PRESENT];
607 1.9.2.2 pgoyette e->private = AXP_SENSOR_VBUS_PRESENT;
608 1.9.2.2 pgoyette e->units = ENVSYS_INDICATOR;
609 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
610 1.9.2.2 pgoyette strlcpy(e->desc, "VBUS present", sizeof(e->desc));
611 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
612 1.9.2.2 pgoyette }
613 1.9.2.2 pgoyette
614 1.9.2.2 pgoyette static void
615 1.9.2.2 pgoyette axppmic_attach_battery(struct axppmic_softc *sc)
616 1.9.2.2 pgoyette {
617 1.9.2.3 pgoyette const struct axppmic_config *c = sc->sc_conf;
618 1.9.2.2 pgoyette envsys_data_t *e;
619 1.9.2.2 pgoyette uint8_t val;
620 1.9.2.2 pgoyette
621 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
622 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_WARN_REG, &val, I2C_F_POLL) == 0) {
623 1.9.2.2 pgoyette sc->sc_warn_thres = __SHIFTOUT(val, AXP_BATT_CAP_WARN_LV1) + 5;
624 1.9.2.2 pgoyette sc->sc_shut_thres = __SHIFTOUT(val, AXP_BATT_CAP_WARN_LV2);
625 1.9.2.2 pgoyette }
626 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, I2C_F_POLL);
627 1.9.2.2 pgoyette
628 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_PRESENT];
629 1.9.2.2 pgoyette e->private = AXP_SENSOR_BATT_PRESENT;
630 1.9.2.2 pgoyette e->units = ENVSYS_INDICATOR;
631 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
632 1.9.2.2 pgoyette strlcpy(e->desc, "battery present", sizeof(e->desc));
633 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
634 1.9.2.2 pgoyette
635 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGING];
636 1.9.2.2 pgoyette e->private = AXP_SENSOR_BATT_CHARGING;
637 1.9.2.2 pgoyette e->units = ENVSYS_BATTERY_CHARGE;
638 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
639 1.9.2.2 pgoyette strlcpy(e->desc, "charging", sizeof(e->desc));
640 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
641 1.9.2.2 pgoyette
642 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_STATE];
643 1.9.2.2 pgoyette e->private = AXP_SENSOR_BATT_CHARGE_STATE;
644 1.9.2.2 pgoyette e->units = ENVSYS_BATTERY_CAPACITY;
645 1.9.2.2 pgoyette e->flags = ENVSYS_FMONSTCHANGED;
646 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
647 1.9.2.2 pgoyette e->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
648 1.9.2.2 pgoyette strlcpy(e->desc, "charge state", sizeof(e->desc));
649 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
650 1.9.2.2 pgoyette
651 1.9.2.3 pgoyette if (c->batsense_step) {
652 1.9.2.3 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_VOLTAGE];
653 1.9.2.3 pgoyette e->private = AXP_SENSOR_BATT_VOLTAGE;
654 1.9.2.3 pgoyette e->units = ENVSYS_SVOLTS_DC;
655 1.9.2.3 pgoyette e->state = ENVSYS_SINVALID;
656 1.9.2.3 pgoyette strlcpy(e->desc, "battery voltage", sizeof(e->desc));
657 1.9.2.3 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
658 1.9.2.3 pgoyette }
659 1.9.2.3 pgoyette
660 1.9.2.3 pgoyette if (c->charge_step) {
661 1.9.2.3 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_CURRENT];
662 1.9.2.3 pgoyette e->private = AXP_SENSOR_BATT_CHARGE_CURRENT;
663 1.9.2.3 pgoyette e->units = ENVSYS_SAMPS;
664 1.9.2.3 pgoyette e->state = ENVSYS_SINVALID;
665 1.9.2.3 pgoyette strlcpy(e->desc, "battery charge current", sizeof(e->desc));
666 1.9.2.3 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
667 1.9.2.3 pgoyette }
668 1.9.2.3 pgoyette
669 1.9.2.3 pgoyette if (c->discharge_step) {
670 1.9.2.3 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_DISCHARGE_CURRENT];
671 1.9.2.3 pgoyette e->private = AXP_SENSOR_BATT_DISCHARGE_CURRENT;
672 1.9.2.3 pgoyette e->units = ENVSYS_SAMPS;
673 1.9.2.3 pgoyette e->state = ENVSYS_SINVALID;
674 1.9.2.3 pgoyette strlcpy(e->desc, "battery discharge current", sizeof(e->desc));
675 1.9.2.3 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
676 1.9.2.3 pgoyette }
677 1.9.2.3 pgoyette
678 1.9.2.3 pgoyette if (c->has_fuel_gauge) {
679 1.9.2.3 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_CAPACITY_PERCENT];
680 1.9.2.3 pgoyette e->private = AXP_SENSOR_BATT_CAPACITY_PERCENT;
681 1.9.2.2 pgoyette e->units = ENVSYS_INTEGER;
682 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
683 1.9.2.2 pgoyette e->flags = ENVSYS_FPERCENT;
684 1.9.2.2 pgoyette strlcpy(e->desc, "battery percent", sizeof(e->desc));
685 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
686 1.9.2.2 pgoyette }
687 1.9.2.5 pgoyette
688 1.9.2.5 pgoyette if (c->maxcap_step) {
689 1.9.2.5 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_MAXIMUM_CAPACITY];
690 1.9.2.5 pgoyette e->private = AXP_SENSOR_BATT_MAXIMUM_CAPACITY;
691 1.9.2.5 pgoyette e->units = ENVSYS_SAMPHOUR;
692 1.9.2.5 pgoyette e->state = ENVSYS_SINVALID;
693 1.9.2.5 pgoyette strlcpy(e->desc, "battery maximum capacity", sizeof(e->desc));
694 1.9.2.5 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
695 1.9.2.5 pgoyette }
696 1.9.2.5 pgoyette
697 1.9.2.5 pgoyette if (c->coulomb_step) {
698 1.9.2.5 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_CURRENT_CAPACITY];
699 1.9.2.5 pgoyette e->private = AXP_SENSOR_BATT_CURRENT_CAPACITY;
700 1.9.2.5 pgoyette e->units = ENVSYS_SAMPHOUR;
701 1.9.2.5 pgoyette e->state = ENVSYS_SINVALID;
702 1.9.2.5 pgoyette strlcpy(e->desc, "battery current capacity", sizeof(e->desc));
703 1.9.2.5 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
704 1.9.2.5 pgoyette }
705 1.9.2.2 pgoyette }
706 1.9.2.2 pgoyette
707 1.9.2.2 pgoyette static void
708 1.9.2.2 pgoyette axppmic_attach_sensors(struct axppmic_softc *sc)
709 1.9.2.2 pgoyette {
710 1.9.2.2 pgoyette if (sc->sc_conf->has_battery) {
711 1.9.2.2 pgoyette sc->sc_sme = sysmon_envsys_create();
712 1.9.2.2 pgoyette sc->sc_sme->sme_name = device_xname(sc->sc_dev);
713 1.9.2.2 pgoyette sc->sc_sme->sme_cookie = sc;
714 1.9.2.2 pgoyette sc->sc_sme->sme_refresh = axppmic_sensor_refresh;
715 1.9.2.2 pgoyette sc->sc_sme->sme_class = SME_CLASS_BATTERY;
716 1.9.2.2 pgoyette sc->sc_sme->sme_flags = SME_INIT_REFRESH;
717 1.9.2.2 pgoyette
718 1.9.2.2 pgoyette axppmic_attach_acadapter(sc);
719 1.9.2.2 pgoyette axppmic_attach_battery(sc);
720 1.9.2.2 pgoyette
721 1.9.2.2 pgoyette sysmon_envsys_register(sc->sc_sme);
722 1.9.2.2 pgoyette }
723 1.9.2.2 pgoyette }
724 1.9.2.2 pgoyette
725 1.9.2.2 pgoyette
726 1.9.2.2 pgoyette static int
727 1.9.2.2 pgoyette axppmic_match(device_t parent, cfdata_t match, void *aux)
728 1.9.2.2 pgoyette {
729 1.9.2.2 pgoyette struct i2c_attach_args *ia = aux;
730 1.9.2.3 pgoyette int match_result;
731 1.9.2.2 pgoyette
732 1.9.2.4 pgoyette if (iic_use_direct_match(ia, match, compat_data, &match_result))
733 1.9.2.3 pgoyette return match_result;
734 1.9.2.2 pgoyette
735 1.9.2.3 pgoyette /* This device is direct-config only. */
736 1.9.2.3 pgoyette
737 1.9.2.3 pgoyette return 0;
738 1.9.2.2 pgoyette }
739 1.9.2.2 pgoyette
740 1.9.2.2 pgoyette static void
741 1.9.2.2 pgoyette axppmic_attach(device_t parent, device_t self, void *aux)
742 1.9.2.2 pgoyette {
743 1.9.2.2 pgoyette struct axppmic_softc *sc = device_private(self);
744 1.9.2.4 pgoyette const struct device_compatible_entry *dce = NULL;
745 1.9.2.2 pgoyette const struct axppmic_config *c;
746 1.9.2.2 pgoyette struct axpreg_attach_args aaa;
747 1.9.2.2 pgoyette struct i2c_attach_args *ia = aux;
748 1.9.2.2 pgoyette int phandle, child, i;
749 1.9.2.2 pgoyette uint32_t irq_mask;
750 1.9.2.2 pgoyette void *ih;
751 1.9.2.2 pgoyette
752 1.9.2.4 pgoyette (void) iic_compatible_match(ia, compat_data, &dce);
753 1.9.2.3 pgoyette KASSERT(dce != NULL);
754 1.9.2.4 pgoyette c = (void *)dce->data;
755 1.9.2.2 pgoyette
756 1.9.2.2 pgoyette sc->sc_dev = self;
757 1.9.2.2 pgoyette sc->sc_i2c = ia->ia_tag;
758 1.9.2.2 pgoyette sc->sc_addr = ia->ia_addr;
759 1.9.2.2 pgoyette sc->sc_phandle = ia->ia_cookie;
760 1.9.2.2 pgoyette sc->sc_conf = c;
761 1.9.2.2 pgoyette
762 1.9.2.2 pgoyette aprint_naive("\n");
763 1.9.2.2 pgoyette aprint_normal(": %s\n", c->name);
764 1.9.2.2 pgoyette
765 1.9.2.2 pgoyette sc->sc_smpsw.smpsw_name = device_xname(self);
766 1.9.2.2 pgoyette sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
767 1.9.2.2 pgoyette sysmon_pswitch_register(&sc->sc_smpsw);
768 1.9.2.2 pgoyette
769 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
770 1.9.2.2 pgoyette for (i = 1; i <= c->irq_regs; i++) {
771 1.9.2.2 pgoyette irq_mask = 0;
772 1.9.2.2 pgoyette if (i == c->poklirq.reg)
773 1.9.2.2 pgoyette irq_mask |= c->poklirq.mask;
774 1.9.2.2 pgoyette if (i == c->acinirq.reg)
775 1.9.2.2 pgoyette irq_mask |= c->acinirq.mask;
776 1.9.2.2 pgoyette if (i == c->vbusirq.reg)
777 1.9.2.2 pgoyette irq_mask |= c->vbusirq.mask;
778 1.9.2.2 pgoyette if (i == c->battirq.reg)
779 1.9.2.2 pgoyette irq_mask |= c->battirq.mask;
780 1.9.2.2 pgoyette if (i == c->chargeirq.reg)
781 1.9.2.2 pgoyette irq_mask |= c->chargeirq.mask;
782 1.9.2.2 pgoyette if (i == c->chargestirq.reg)
783 1.9.2.2 pgoyette irq_mask |= c->chargestirq.mask;
784 1.9.2.2 pgoyette axppmic_write(sc->sc_i2c, sc->sc_addr, AXP_IRQ_ENABLE_REG(i), irq_mask, I2C_F_POLL);
785 1.9.2.2 pgoyette }
786 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, I2C_F_POLL);
787 1.9.2.2 pgoyette
788 1.9.2.2 pgoyette ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
789 1.9.2.2 pgoyette axppmic_intr, sc);
790 1.9.2.2 pgoyette if (ih == NULL) {
791 1.9.2.2 pgoyette aprint_error_dev(self, "WARNING: couldn't establish interrupt handler\n");
792 1.9.2.2 pgoyette }
793 1.9.2.2 pgoyette
794 1.9.2.2 pgoyette fdtbus_register_power_controller(sc->sc_dev, sc->sc_phandle,
795 1.9.2.2 pgoyette &axppmic_power_funcs);
796 1.9.2.2 pgoyette
797 1.9.2.2 pgoyette phandle = of_find_firstchild_byname(sc->sc_phandle, "regulators");
798 1.9.2.2 pgoyette if (phandle > 0) {
799 1.9.2.2 pgoyette aaa.reg_i2c = sc->sc_i2c;
800 1.9.2.2 pgoyette aaa.reg_addr = sc->sc_addr;
801 1.9.2.2 pgoyette for (i = 0; i < c->ncontrols; i++) {
802 1.9.2.2 pgoyette const struct axppmic_ctrl *ctrl = &c->controls[i];
803 1.9.2.2 pgoyette child = of_find_firstchild_byname(phandle, ctrl->c_name);
804 1.9.2.2 pgoyette if (child <= 0)
805 1.9.2.2 pgoyette continue;
806 1.9.2.2 pgoyette aaa.reg_ctrl = ctrl;
807 1.9.2.2 pgoyette aaa.reg_phandle = child;
808 1.9.2.2 pgoyette config_found(sc->sc_dev, &aaa, NULL);
809 1.9.2.2 pgoyette }
810 1.9.2.2 pgoyette }
811 1.9.2.2 pgoyette
812 1.9.2.2 pgoyette if (c->has_battery)
813 1.9.2.2 pgoyette axppmic_attach_sensors(sc);
814 1.9.2.2 pgoyette }
815 1.9.2.2 pgoyette
816 1.9.2.2 pgoyette static int
817 1.9.2.2 pgoyette axpreg_acquire(device_t dev)
818 1.9.2.2 pgoyette {
819 1.9.2.2 pgoyette return 0;
820 1.9.2.2 pgoyette }
821 1.9.2.2 pgoyette
822 1.9.2.2 pgoyette static void
823 1.9.2.2 pgoyette axpreg_release(device_t dev)
824 1.9.2.2 pgoyette {
825 1.9.2.2 pgoyette }
826 1.9.2.2 pgoyette
827 1.9.2.2 pgoyette static int
828 1.9.2.2 pgoyette axpreg_enable(device_t dev, bool enable)
829 1.9.2.2 pgoyette {
830 1.9.2.2 pgoyette struct axpreg_softc *sc = device_private(dev);
831 1.9.2.2 pgoyette const struct axppmic_ctrl *c = sc->sc_ctrl;
832 1.9.2.2 pgoyette const int flags = (cold ? I2C_F_POLL : 0);
833 1.9.2.2 pgoyette uint8_t val;
834 1.9.2.2 pgoyette int error;
835 1.9.2.2 pgoyette
836 1.9.2.2 pgoyette if (!c->c_enable_mask)
837 1.9.2.2 pgoyette return EINVAL;
838 1.9.2.2 pgoyette
839 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, flags);
840 1.9.2.2 pgoyette if ((error = axppmic_read(sc->sc_i2c, sc->sc_addr, c->c_enable_reg, &val, flags)) == 0) {
841 1.9.2.2 pgoyette if (enable)
842 1.9.2.2 pgoyette val |= c->c_enable_mask;
843 1.9.2.2 pgoyette else
844 1.9.2.2 pgoyette val &= ~c->c_enable_mask;
845 1.9.2.2 pgoyette error = axppmic_write(sc->sc_i2c, sc->sc_addr, c->c_enable_reg, val, flags);
846 1.9.2.2 pgoyette }
847 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, flags);
848 1.9.2.2 pgoyette
849 1.9.2.2 pgoyette return error;
850 1.9.2.2 pgoyette }
851 1.9.2.2 pgoyette
852 1.9.2.2 pgoyette static int
853 1.9.2.2 pgoyette axpreg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
854 1.9.2.2 pgoyette {
855 1.9.2.2 pgoyette struct axpreg_softc *sc = device_private(dev);
856 1.9.2.2 pgoyette const struct axppmic_ctrl *c = sc->sc_ctrl;
857 1.9.2.2 pgoyette
858 1.9.2.2 pgoyette return axppmic_set_voltage(sc->sc_i2c, sc->sc_addr, c,
859 1.9.2.2 pgoyette min_uvol / 1000, max_uvol / 1000);
860 1.9.2.2 pgoyette }
861 1.9.2.2 pgoyette
862 1.9.2.2 pgoyette static int
863 1.9.2.2 pgoyette axpreg_get_voltage(device_t dev, u_int *puvol)
864 1.9.2.2 pgoyette {
865 1.9.2.2 pgoyette struct axpreg_softc *sc = device_private(dev);
866 1.9.2.2 pgoyette const struct axppmic_ctrl *c = sc->sc_ctrl;
867 1.9.2.2 pgoyette int error;
868 1.9.2.2 pgoyette u_int vol;
869 1.9.2.2 pgoyette
870 1.9.2.2 pgoyette error = axppmic_get_voltage(sc->sc_i2c, sc->sc_addr, c, &vol);
871 1.9.2.2 pgoyette if (error)
872 1.9.2.2 pgoyette return error;
873 1.9.2.2 pgoyette
874 1.9.2.2 pgoyette *puvol = vol * 1000;
875 1.9.2.2 pgoyette return 0;
876 1.9.2.2 pgoyette }
877 1.9.2.2 pgoyette
878 1.9.2.2 pgoyette static struct fdtbus_regulator_controller_func axpreg_funcs = {
879 1.9.2.2 pgoyette .acquire = axpreg_acquire,
880 1.9.2.2 pgoyette .release = axpreg_release,
881 1.9.2.2 pgoyette .enable = axpreg_enable,
882 1.9.2.2 pgoyette .set_voltage = axpreg_set_voltage,
883 1.9.2.2 pgoyette .get_voltage = axpreg_get_voltage,
884 1.9.2.2 pgoyette };
885 1.9.2.2 pgoyette
886 1.9.2.2 pgoyette static int
887 1.9.2.2 pgoyette axpreg_match(device_t parent, cfdata_t match, void *aux)
888 1.9.2.2 pgoyette {
889 1.9.2.2 pgoyette return 1;
890 1.9.2.2 pgoyette }
891 1.9.2.2 pgoyette
892 1.9.2.2 pgoyette static void
893 1.9.2.2 pgoyette axpreg_attach(device_t parent, device_t self, void *aux)
894 1.9.2.2 pgoyette {
895 1.9.2.2 pgoyette struct axpreg_softc *sc = device_private(self);
896 1.9.2.2 pgoyette struct axpreg_attach_args *aaa = aux;
897 1.9.2.2 pgoyette const int phandle = aaa->reg_phandle;
898 1.9.2.2 pgoyette const char *name;
899 1.9.2.2 pgoyette
900 1.9.2.2 pgoyette sc->sc_dev = self;
901 1.9.2.2 pgoyette sc->sc_i2c = aaa->reg_i2c;
902 1.9.2.2 pgoyette sc->sc_addr = aaa->reg_addr;
903 1.9.2.2 pgoyette sc->sc_ctrl = aaa->reg_ctrl;
904 1.9.2.2 pgoyette
905 1.9.2.2 pgoyette fdtbus_register_regulator_controller(self, phandle,
906 1.9.2.2 pgoyette &axpreg_funcs);
907 1.9.2.2 pgoyette
908 1.9.2.2 pgoyette aprint_naive("\n");
909 1.9.2.2 pgoyette name = fdtbus_get_string(phandle, "regulator-name");
910 1.9.2.2 pgoyette if (name)
911 1.9.2.2 pgoyette aprint_normal(": %s\n", name);
912 1.9.2.2 pgoyette else
913 1.9.2.2 pgoyette aprint_normal("\n");
914 1.9.2.2 pgoyette }
915 1.9.2.2 pgoyette
916 1.9.2.2 pgoyette CFATTACH_DECL_NEW(axppmic, sizeof(struct axppmic_softc),
917 1.9.2.2 pgoyette axppmic_match, axppmic_attach, NULL, NULL);
918 1.9.2.2 pgoyette
919 1.9.2.2 pgoyette CFATTACH_DECL_NEW(axpreg, sizeof(struct axpreg_softc),
920 1.9.2.2 pgoyette axpreg_match, axpreg_attach, NULL, NULL);
921