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