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