axppmic.c revision 1.9.2.2 1 1.9.2.2 pgoyette /* $NetBSD: axppmic.c,v 1.9.2.2 2018/05/21 04:36:05 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.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: axppmic.c,v 1.9.2.2 2018/05/21 04:36:05 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.2 pgoyette
51 1.9.2.2 pgoyette #define AXP_POWER_MODE_REG 0x01
52 1.9.2.2 pgoyette #define AXP_POWER_MODE_BATT_VALID __BIT(4)
53 1.9.2.2 pgoyette #define AXP_POWER_MODE_BATT_PRESENT __BIT(5)
54 1.9.2.2 pgoyette #define AXP_POWER_MODE_BATT_CHARGING __BIT(6)
55 1.9.2.2 pgoyette
56 1.9.2.2 pgoyette #define AXP_POWER_DISABLE_REG 0x32
57 1.9.2.2 pgoyette #define AXP_POWER_DISABLE_CTRL __BIT(7)
58 1.9.2.2 pgoyette
59 1.9.2.2 pgoyette #define AXP_IRQ_ENABLE_REG(n) (0x40 + (n) - 1)
60 1.9.2.2 pgoyette #define AXP_IRQ1_ACIN_RAISE __BIT(6)
61 1.9.2.2 pgoyette #define AXP_IRQ1_ACIN_LOWER __BIT(5)
62 1.9.2.2 pgoyette #define AXP_IRQ1_VBUS_RAISE __BIT(3)
63 1.9.2.2 pgoyette #define AXP_IRQ1_VBUS_LOWER __BIT(2)
64 1.9.2.2 pgoyette #define AXP_IRQ_STATUS_REG(n) (0x48 + (n) - 1)
65 1.9.2.2 pgoyette
66 1.9.2.2 pgoyette #define AXP_FUEL_GAUGE_CTRL_REG 0xb8
67 1.9.2.2 pgoyette #define AXP_FUEL_GAUGE_CTRL_EN __BIT(7)
68 1.9.2.2 pgoyette #define AXP_BATT_CAP_REG 0xb9
69 1.9.2.2 pgoyette #define AXP_BATT_CAP_VALID __BIT(7)
70 1.9.2.2 pgoyette #define AXP_BATT_CAP_PERCENT __BITS(6,0)
71 1.9.2.2 pgoyette
72 1.9.2.2 pgoyette #define AXP_BATT_CAP_WARN_REG 0xe6
73 1.9.2.2 pgoyette #define AXP_BATT_CAP_WARN_LV1 __BITS(7,4)
74 1.9.2.2 pgoyette #define AXP_BATT_CAP_WARN_LV2 __BITS(3,0)
75 1.9.2.2 pgoyette
76 1.9.2.2 pgoyette struct axppmic_ctrl {
77 1.9.2.2 pgoyette device_t c_dev;
78 1.9.2.2 pgoyette
79 1.9.2.2 pgoyette const char * c_name;
80 1.9.2.2 pgoyette u_int c_min;
81 1.9.2.2 pgoyette u_int c_max;
82 1.9.2.2 pgoyette u_int c_step1;
83 1.9.2.2 pgoyette u_int c_step1cnt;
84 1.9.2.2 pgoyette u_int c_step2;
85 1.9.2.2 pgoyette u_int c_step2cnt;
86 1.9.2.2 pgoyette
87 1.9.2.2 pgoyette uint8_t c_enable_reg;
88 1.9.2.2 pgoyette uint8_t c_enable_mask;
89 1.9.2.2 pgoyette
90 1.9.2.2 pgoyette uint8_t c_voltage_reg;
91 1.9.2.2 pgoyette uint8_t c_voltage_mask;
92 1.9.2.2 pgoyette };
93 1.9.2.2 pgoyette
94 1.9.2.2 pgoyette #define AXP_CTRL(name, min, max, step, ereg, emask, vreg, vmask) \
95 1.9.2.2 pgoyette { .c_name = (name), .c_min = (min), .c_max = (max), \
96 1.9.2.2 pgoyette .c_step1 = (step), .c_step1cnt = (((max) - (min)) / (step)) + 1, \
97 1.9.2.2 pgoyette .c_step2 = 0, .c_step2cnt = 0, \
98 1.9.2.2 pgoyette .c_enable_reg = (ereg), .c_enable_mask = (emask), \
99 1.9.2.2 pgoyette .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
100 1.9.2.2 pgoyette
101 1.9.2.2 pgoyette #define AXP_CTRL2(name, min, max, step1, step1cnt, step2, step2cnt, ereg, emask, vreg, vmask) \
102 1.9.2.2 pgoyette { .c_name = (name), .c_min = (min), .c_max = (max), \
103 1.9.2.2 pgoyette .c_step1 = (step1), .c_step1cnt = (step1cnt), \
104 1.9.2.2 pgoyette .c_step2 = (step2), .c_step2cnt = (step2cnt), \
105 1.9.2.2 pgoyette .c_enable_reg = (ereg), .c_enable_mask = (emask), \
106 1.9.2.2 pgoyette .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
107 1.9.2.2 pgoyette
108 1.9.2.2 pgoyette static const struct axppmic_ctrl axp803_ctrls[] = {
109 1.9.2.2 pgoyette AXP_CTRL("dldo1", 700, 3300, 100,
110 1.9.2.2 pgoyette 0x12, __BIT(3), 0x15, __BITS(4,0)),
111 1.9.2.2 pgoyette AXP_CTRL2("dldo2", 700, 4200, 100, 28, 200, 4,
112 1.9.2.2 pgoyette 0x12, __BIT(4), 0x16, __BITS(4,0)),
113 1.9.2.2 pgoyette AXP_CTRL("dldo3", 700, 3300, 100,
114 1.9.2.2 pgoyette 0x12, __BIT(5), 0x17, __BITS(4,0)),
115 1.9.2.2 pgoyette AXP_CTRL("dldo4", 700, 3300, 100,
116 1.9.2.2 pgoyette 0x12, __BIT(6), 0x18, __BITS(4,0)),
117 1.9.2.2 pgoyette AXP_CTRL("eldo1", 700, 1900, 50,
118 1.9.2.2 pgoyette 0x12, __BIT(0), 0x19, __BITS(4,0)),
119 1.9.2.2 pgoyette AXP_CTRL("eldo2", 700, 1900, 50,
120 1.9.2.2 pgoyette 0x12, __BIT(1), 0x1a, __BITS(4,0)),
121 1.9.2.2 pgoyette AXP_CTRL("eldo3", 700, 1900, 50,
122 1.9.2.2 pgoyette 0x12, __BIT(2), 0x1b, __BITS(4,0)),
123 1.9.2.2 pgoyette AXP_CTRL("fldo1", 700, 1450, 50,
124 1.9.2.2 pgoyette 0x13, __BIT(2), 0x1c, __BITS(3,0)),
125 1.9.2.2 pgoyette AXP_CTRL("fldo2", 700, 1450, 50,
126 1.9.2.2 pgoyette 0x13, __BIT(3), 0x1d, __BITS(3,0)),
127 1.9.2.2 pgoyette AXP_CTRL("dcdc1", 1600, 3400, 100,
128 1.9.2.2 pgoyette 0x10, __BIT(0), 0x20, __BITS(4,0)),
129 1.9.2.2 pgoyette AXP_CTRL2("dcdc2", 500, 1300, 10, 70, 20, 5,
130 1.9.2.2 pgoyette 0x10, __BIT(1), 0x21, __BITS(6,0)),
131 1.9.2.2 pgoyette AXP_CTRL2("dcdc3", 500, 1300, 10, 70, 20, 5,
132 1.9.2.2 pgoyette 0x10, __BIT(2), 0x22, __BITS(6,0)),
133 1.9.2.2 pgoyette AXP_CTRL2("dcdc4", 500, 1300, 10, 70, 20, 5,
134 1.9.2.2 pgoyette 0x10, __BIT(3), 0x23, __BITS(6,0)),
135 1.9.2.2 pgoyette AXP_CTRL2("dcdc5", 800, 1840, 10, 33, 20, 36,
136 1.9.2.2 pgoyette 0x10, __BIT(4), 0x24, __BITS(6,0)),
137 1.9.2.2 pgoyette AXP_CTRL2("dcdc6", 600, 1520, 10, 51, 20, 21,
138 1.9.2.2 pgoyette 0x10, __BIT(5), 0x25, __BITS(6,0)),
139 1.9.2.2 pgoyette AXP_CTRL("aldo1", 700, 3300, 100,
140 1.9.2.2 pgoyette 0x13, __BIT(5), 0x28, __BITS(4,0)),
141 1.9.2.2 pgoyette AXP_CTRL("aldo2", 700, 3300, 100,
142 1.9.2.2 pgoyette 0x13, __BIT(6), 0x29, __BITS(4,0)),
143 1.9.2.2 pgoyette AXP_CTRL("aldo3", 700, 3300, 100,
144 1.9.2.2 pgoyette 0x13, __BIT(7), 0x2a, __BITS(4,0)),
145 1.9.2.2 pgoyette };
146 1.9.2.2 pgoyette
147 1.9.2.2 pgoyette static const struct axppmic_ctrl axp805_ctrls[] = {
148 1.9.2.2 pgoyette AXP_CTRL2("dcdca", 600, 1520, 10, 51, 20, 21,
149 1.9.2.2 pgoyette 0x10, __BIT(0), 0x12, __BITS(6,0)),
150 1.9.2.2 pgoyette AXP_CTRL("dcdcb", 1000, 2550, 50,
151 1.9.2.2 pgoyette 0x10, __BIT(1), 0x13, __BITS(4,0)),
152 1.9.2.2 pgoyette AXP_CTRL2("dcdcc", 600, 1520, 10, 51, 20, 21,
153 1.9.2.2 pgoyette 0x10, __BIT(2), 0x14, __BITS(6,0)),
154 1.9.2.2 pgoyette AXP_CTRL2("dcdcd", 600, 3300, 20, 46, 100, 18,
155 1.9.2.2 pgoyette 0x10, __BIT(3), 0x15, __BITS(5,0)),
156 1.9.2.2 pgoyette AXP_CTRL("dcdce", 1100, 3400, 100,
157 1.9.2.2 pgoyette 0x10, __BIT(4), 0x16, __BITS(4,0)),
158 1.9.2.2 pgoyette AXP_CTRL("aldo1", 700, 3300, 100,
159 1.9.2.2 pgoyette 0x10, __BIT(5), 0x17, __BITS(4,0)),
160 1.9.2.2 pgoyette AXP_CTRL("aldo2", 700, 3400, 100,
161 1.9.2.2 pgoyette 0x10, __BIT(6), 0x18, __BITS(4,0)),
162 1.9.2.2 pgoyette AXP_CTRL("aldo3", 700, 3300, 100,
163 1.9.2.2 pgoyette 0x10, __BIT(7), 0x19, __BITS(4,0)),
164 1.9.2.2 pgoyette AXP_CTRL("bldo1", 700, 1900, 100,
165 1.9.2.2 pgoyette 0x11, __BIT(0), 0x20, __BITS(3,0)),
166 1.9.2.2 pgoyette AXP_CTRL("bldo2", 700, 1900, 100,
167 1.9.2.2 pgoyette 0x11, __BIT(1), 0x21, __BITS(3,0)),
168 1.9.2.2 pgoyette AXP_CTRL("bldo3", 700, 1900, 100,
169 1.9.2.2 pgoyette 0x11, __BIT(2), 0x22, __BITS(3,0)),
170 1.9.2.2 pgoyette AXP_CTRL("bldo4", 700, 1900, 100,
171 1.9.2.2 pgoyette 0x11, __BIT(3), 0x23, __BITS(3,0)),
172 1.9.2.2 pgoyette AXP_CTRL("cldo1", 700, 3300, 100,
173 1.9.2.2 pgoyette 0x11, __BIT(4), 0x24, __BITS(4,0)),
174 1.9.2.2 pgoyette AXP_CTRL2("cldo2", 700, 4200, 100, 28, 200, 4,
175 1.9.2.2 pgoyette 0x11, __BIT(5), 0x25, __BITS(4,0)),
176 1.9.2.2 pgoyette AXP_CTRL("cldo3", 700, 3300, 100,
177 1.9.2.2 pgoyette 0x11, __BIT(6), 0x26, __BITS(4,0)),
178 1.9.2.2 pgoyette };
179 1.9.2.2 pgoyette
180 1.9.2.2 pgoyette struct axppmic_irq {
181 1.9.2.2 pgoyette u_int reg;
182 1.9.2.2 pgoyette uint8_t mask;
183 1.9.2.2 pgoyette };
184 1.9.2.2 pgoyette
185 1.9.2.2 pgoyette #define AXPPMIC_IRQ(_reg, _mask) \
186 1.9.2.2 pgoyette { .reg = (_reg), .mask = (_mask) }
187 1.9.2.2 pgoyette
188 1.9.2.2 pgoyette struct axppmic_config {
189 1.9.2.2 pgoyette const char *name;
190 1.9.2.2 pgoyette const struct axppmic_ctrl *controls;
191 1.9.2.2 pgoyette u_int ncontrols;
192 1.9.2.2 pgoyette u_int irq_regs;
193 1.9.2.2 pgoyette bool has_battery;
194 1.9.2.2 pgoyette bool has_fuel_gauge;
195 1.9.2.2 pgoyette struct axppmic_irq poklirq;
196 1.9.2.2 pgoyette struct axppmic_irq acinirq;
197 1.9.2.2 pgoyette struct axppmic_irq vbusirq;
198 1.9.2.2 pgoyette struct axppmic_irq battirq;
199 1.9.2.2 pgoyette struct axppmic_irq chargeirq;
200 1.9.2.2 pgoyette struct axppmic_irq chargestirq;
201 1.9.2.2 pgoyette };
202 1.9.2.2 pgoyette
203 1.9.2.2 pgoyette enum axppmic_sensor {
204 1.9.2.2 pgoyette AXP_SENSOR_ACIN_PRESENT,
205 1.9.2.2 pgoyette AXP_SENSOR_VBUS_PRESENT,
206 1.9.2.2 pgoyette AXP_SENSOR_BATT_PRESENT,
207 1.9.2.2 pgoyette AXP_SENSOR_BATT_CHARGING,
208 1.9.2.2 pgoyette AXP_SENSOR_BATT_CHARGE_STATE,
209 1.9.2.2 pgoyette AXP_SENSOR_BATT_CAPACITY,
210 1.9.2.2 pgoyette AXP_NSENSORS
211 1.9.2.2 pgoyette };
212 1.9.2.2 pgoyette
213 1.9.2.2 pgoyette struct axppmic_softc {
214 1.9.2.2 pgoyette device_t sc_dev;
215 1.9.2.2 pgoyette i2c_tag_t sc_i2c;
216 1.9.2.2 pgoyette i2c_addr_t sc_addr;
217 1.9.2.2 pgoyette int sc_phandle;
218 1.9.2.2 pgoyette
219 1.9.2.2 pgoyette const struct axppmic_config *sc_conf;
220 1.9.2.2 pgoyette
221 1.9.2.2 pgoyette struct sysmon_pswitch sc_smpsw;
222 1.9.2.2 pgoyette
223 1.9.2.2 pgoyette struct sysmon_envsys *sc_sme;
224 1.9.2.2 pgoyette
225 1.9.2.2 pgoyette envsys_data_t sc_sensor[AXP_NSENSORS];
226 1.9.2.2 pgoyette
227 1.9.2.2 pgoyette u_int sc_warn_thres;
228 1.9.2.2 pgoyette u_int sc_shut_thres;
229 1.9.2.2 pgoyette };
230 1.9.2.2 pgoyette
231 1.9.2.2 pgoyette struct axpreg_softc {
232 1.9.2.2 pgoyette device_t sc_dev;
233 1.9.2.2 pgoyette i2c_tag_t sc_i2c;
234 1.9.2.2 pgoyette i2c_addr_t sc_addr;
235 1.9.2.2 pgoyette const struct axppmic_ctrl *sc_ctrl;
236 1.9.2.2 pgoyette };
237 1.9.2.2 pgoyette
238 1.9.2.2 pgoyette struct axpreg_attach_args {
239 1.9.2.2 pgoyette const struct axppmic_ctrl *reg_ctrl;
240 1.9.2.2 pgoyette int reg_phandle;
241 1.9.2.2 pgoyette i2c_tag_t reg_i2c;
242 1.9.2.2 pgoyette i2c_addr_t reg_addr;
243 1.9.2.2 pgoyette };
244 1.9.2.2 pgoyette
245 1.9.2.2 pgoyette static const struct axppmic_config axp803_config = {
246 1.9.2.2 pgoyette .name = "AXP803",
247 1.9.2.2 pgoyette .controls = axp803_ctrls,
248 1.9.2.2 pgoyette .ncontrols = __arraycount(axp803_ctrls),
249 1.9.2.2 pgoyette .irq_regs = 6,
250 1.9.2.2 pgoyette .has_battery = true,
251 1.9.2.2 pgoyette .has_fuel_gauge = true,
252 1.9.2.2 pgoyette .poklirq = AXPPMIC_IRQ(5, __BIT(3)),
253 1.9.2.2 pgoyette .acinirq = AXPPMIC_IRQ(1, __BITS(6,5)),
254 1.9.2.2 pgoyette .vbusirq = AXPPMIC_IRQ(1, __BITS(3,2)),
255 1.9.2.2 pgoyette .battirq = AXPPMIC_IRQ(2, __BITS(7,6)),
256 1.9.2.2 pgoyette .chargeirq = AXPPMIC_IRQ(2, __BITS(3,2)),
257 1.9.2.2 pgoyette .chargestirq = AXPPMIC_IRQ(4, __BITS(1,0)),
258 1.9.2.2 pgoyette };
259 1.9.2.2 pgoyette
260 1.9.2.2 pgoyette static const struct axppmic_config axp805_config = {
261 1.9.2.2 pgoyette .name = "AXP805/806",
262 1.9.2.2 pgoyette .controls = axp805_ctrls,
263 1.9.2.2 pgoyette .ncontrols = __arraycount(axp805_ctrls),
264 1.9.2.2 pgoyette .irq_regs = 2,
265 1.9.2.2 pgoyette .poklirq = AXPPMIC_IRQ(2, __BIT(0)),
266 1.9.2.2 pgoyette };
267 1.9.2.2 pgoyette
268 1.9.2.2 pgoyette static const struct of_compat_data compat_data[] = {
269 1.9.2.2 pgoyette { "x-powers,axp803", (uintptr_t)&axp803_config },
270 1.9.2.2 pgoyette { "x-powers,axp805", (uintptr_t)&axp805_config },
271 1.9.2.2 pgoyette { "x-powers,axp806", (uintptr_t)&axp805_config },
272 1.9.2.2 pgoyette { NULL }
273 1.9.2.2 pgoyette };
274 1.9.2.2 pgoyette
275 1.9.2.2 pgoyette static int
276 1.9.2.2 pgoyette axppmic_read(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, uint8_t *val, int flags)
277 1.9.2.2 pgoyette {
278 1.9.2.2 pgoyette return iic_smbus_read_byte(tag, addr, reg, val, flags);
279 1.9.2.2 pgoyette }
280 1.9.2.2 pgoyette
281 1.9.2.2 pgoyette static int
282 1.9.2.2 pgoyette axppmic_write(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, uint8_t val, int flags)
283 1.9.2.2 pgoyette {
284 1.9.2.2 pgoyette return iic_smbus_write_byte(tag, addr, reg, val, flags);
285 1.9.2.2 pgoyette }
286 1.9.2.2 pgoyette
287 1.9.2.2 pgoyette static int
288 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)
289 1.9.2.2 pgoyette {
290 1.9.2.2 pgoyette const int flags = (cold ? I2C_F_POLL : 0);
291 1.9.2.2 pgoyette u_int vol, reg_val;
292 1.9.2.2 pgoyette int nstep, error;
293 1.9.2.2 pgoyette uint8_t val;
294 1.9.2.2 pgoyette
295 1.9.2.2 pgoyette if (!c->c_voltage_mask)
296 1.9.2.2 pgoyette return EINVAL;
297 1.9.2.2 pgoyette
298 1.9.2.2 pgoyette if (min < c->c_min || min > c->c_max)
299 1.9.2.2 pgoyette return EINVAL;
300 1.9.2.2 pgoyette
301 1.9.2.2 pgoyette reg_val = 0;
302 1.9.2.2 pgoyette nstep = 1;
303 1.9.2.2 pgoyette vol = c->c_min;
304 1.9.2.2 pgoyette
305 1.9.2.2 pgoyette for (nstep = 0; nstep < c->c_step1cnt && vol < min; nstep++) {
306 1.9.2.2 pgoyette ++reg_val;
307 1.9.2.2 pgoyette vol += c->c_step1;
308 1.9.2.2 pgoyette }
309 1.9.2.2 pgoyette for (nstep = 0; nstep < c->c_step2cnt && vol < min; nstep++) {
310 1.9.2.2 pgoyette ++reg_val;
311 1.9.2.2 pgoyette vol += c->c_step2;
312 1.9.2.2 pgoyette }
313 1.9.2.2 pgoyette
314 1.9.2.2 pgoyette if (vol > max)
315 1.9.2.2 pgoyette return EINVAL;
316 1.9.2.2 pgoyette
317 1.9.2.2 pgoyette iic_acquire_bus(tag, flags);
318 1.9.2.2 pgoyette if ((error = axppmic_read(tag, addr, c->c_voltage_reg, &val, flags)) == 0) {
319 1.9.2.2 pgoyette val &= ~c->c_voltage_mask;
320 1.9.2.2 pgoyette val |= __SHIFTIN(reg_val, c->c_voltage_mask);
321 1.9.2.2 pgoyette error = axppmic_write(tag, addr, c->c_voltage_reg, val, flags);
322 1.9.2.2 pgoyette }
323 1.9.2.2 pgoyette iic_release_bus(tag, flags);
324 1.9.2.2 pgoyette
325 1.9.2.2 pgoyette return error;
326 1.9.2.2 pgoyette }
327 1.9.2.2 pgoyette
328 1.9.2.2 pgoyette static int
329 1.9.2.2 pgoyette axppmic_get_voltage(i2c_tag_t tag, i2c_addr_t addr, const struct axppmic_ctrl *c, u_int *pvol)
330 1.9.2.2 pgoyette {
331 1.9.2.2 pgoyette const int flags = (cold ? I2C_F_POLL : 0);
332 1.9.2.2 pgoyette int reg_val, 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 iic_acquire_bus(tag, flags);
339 1.9.2.2 pgoyette error = axppmic_read(tag, addr, c->c_voltage_reg, &val, flags);
340 1.9.2.2 pgoyette iic_release_bus(tag, flags);
341 1.9.2.2 pgoyette if (error)
342 1.9.2.2 pgoyette return error;
343 1.9.2.2 pgoyette
344 1.9.2.2 pgoyette reg_val = __SHIFTOUT(val, c->c_voltage_mask);
345 1.9.2.2 pgoyette if (reg_val < c->c_step1cnt) {
346 1.9.2.2 pgoyette *pvol = c->c_min + reg_val * c->c_step1;
347 1.9.2.2 pgoyette } else {
348 1.9.2.2 pgoyette *pvol = c->c_min + (c->c_step1cnt * c->c_step1) +
349 1.9.2.2 pgoyette ((reg_val - c->c_step1cnt) * c->c_step2);
350 1.9.2.2 pgoyette }
351 1.9.2.2 pgoyette
352 1.9.2.2 pgoyette return 0;
353 1.9.2.2 pgoyette }
354 1.9.2.2 pgoyette
355 1.9.2.2 pgoyette static void
356 1.9.2.2 pgoyette axppmic_power_poweroff(device_t dev)
357 1.9.2.2 pgoyette {
358 1.9.2.2 pgoyette struct axppmic_softc *sc = device_private(dev);
359 1.9.2.2 pgoyette
360 1.9.2.2 pgoyette delay(1000000);
361 1.9.2.2 pgoyette
362 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
363 1.9.2.2 pgoyette axppmic_write(sc->sc_i2c, sc->sc_addr, AXP_POWER_DISABLE_REG, AXP_POWER_DISABLE_CTRL, I2C_F_POLL);
364 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, I2C_F_POLL);
365 1.9.2.2 pgoyette }
366 1.9.2.2 pgoyette
367 1.9.2.2 pgoyette static struct fdtbus_power_controller_func axppmic_power_funcs = {
368 1.9.2.2 pgoyette .poweroff = axppmic_power_poweroff,
369 1.9.2.2 pgoyette };
370 1.9.2.2 pgoyette
371 1.9.2.2 pgoyette static void
372 1.9.2.2 pgoyette axppmic_task_shut(void *priv)
373 1.9.2.2 pgoyette {
374 1.9.2.2 pgoyette struct axppmic_softc *sc = priv;
375 1.9.2.2 pgoyette
376 1.9.2.2 pgoyette sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
377 1.9.2.2 pgoyette }
378 1.9.2.2 pgoyette
379 1.9.2.2 pgoyette static void
380 1.9.2.2 pgoyette axppmic_sensor_update(struct sysmon_envsys *sme, envsys_data_t *e)
381 1.9.2.2 pgoyette {
382 1.9.2.2 pgoyette struct axppmic_softc *sc = sme->sme_cookie;
383 1.9.2.2 pgoyette const int flags = I2C_F_POLL;
384 1.9.2.2 pgoyette uint8_t val;
385 1.9.2.2 pgoyette
386 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
387 1.9.2.2 pgoyette
388 1.9.2.2 pgoyette switch (e->private) {
389 1.9.2.2 pgoyette case AXP_SENSOR_ACIN_PRESENT:
390 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0) {
391 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
392 1.9.2.2 pgoyette e->value_cur = !!(val & AXP_POWER_SOURCE_ACIN_PRESENT);
393 1.9.2.2 pgoyette }
394 1.9.2.2 pgoyette break;
395 1.9.2.2 pgoyette case AXP_SENSOR_VBUS_PRESENT:
396 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0) {
397 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
398 1.9.2.2 pgoyette e->value_cur = !!(val & AXP_POWER_SOURCE_VBUS_PRESENT);
399 1.9.2.2 pgoyette }
400 1.9.2.2 pgoyette break;
401 1.9.2.2 pgoyette case AXP_SENSOR_BATT_PRESENT:
402 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, flags) == 0) {
403 1.9.2.2 pgoyette if (val & AXP_POWER_MODE_BATT_VALID) {
404 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
405 1.9.2.2 pgoyette e->value_cur = !!(val & AXP_POWER_MODE_BATT_PRESENT);
406 1.9.2.2 pgoyette }
407 1.9.2.2 pgoyette }
408 1.9.2.2 pgoyette break;
409 1.9.2.2 pgoyette case AXP_SENSOR_BATT_CHARGING:
410 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, flags) == 0) {
411 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
412 1.9.2.2 pgoyette e->value_cur = !!(val & AXP_POWER_MODE_BATT_CHARGING);
413 1.9.2.2 pgoyette }
414 1.9.2.2 pgoyette break;
415 1.9.2.2 pgoyette case AXP_SENSOR_BATT_CHARGE_STATE:
416 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, flags) == 0 &&
417 1.9.2.2 pgoyette (val & AXP_POWER_MODE_BATT_VALID) != 0 &&
418 1.9.2.2 pgoyette (val & AXP_POWER_MODE_BATT_PRESENT) != 0 &&
419 1.9.2.2 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_REG, &val, flags) == 0 &&
420 1.9.2.2 pgoyette (val & AXP_BATT_CAP_VALID) != 0) {
421 1.9.2.2 pgoyette const u_int batt_val = __SHIFTOUT(val, AXP_BATT_CAP_PERCENT);
422 1.9.2.2 pgoyette if (batt_val <= sc->sc_shut_thres) {
423 1.9.2.2 pgoyette e->state = ENVSYS_SCRITICAL;
424 1.9.2.2 pgoyette e->value_cur = ENVSYS_BATTERY_CAPACITY_CRITICAL;
425 1.9.2.2 pgoyette } else if (batt_val <= sc->sc_warn_thres) {
426 1.9.2.2 pgoyette e->state = ENVSYS_SWARNUNDER;
427 1.9.2.2 pgoyette e->value_cur = ENVSYS_BATTERY_CAPACITY_WARNING;
428 1.9.2.2 pgoyette } else {
429 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
430 1.9.2.2 pgoyette e->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
431 1.9.2.2 pgoyette }
432 1.9.2.2 pgoyette }
433 1.9.2.2 pgoyette break;
434 1.9.2.2 pgoyette case AXP_SENSOR_BATT_CAPACITY:
435 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, flags) == 0 &&
436 1.9.2.2 pgoyette (val & AXP_POWER_MODE_BATT_VALID) != 0 &&
437 1.9.2.2 pgoyette (val & AXP_POWER_MODE_BATT_PRESENT) != 0 &&
438 1.9.2.2 pgoyette axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_REG, &val, flags) == 0 &&
439 1.9.2.2 pgoyette (val & AXP_BATT_CAP_VALID) != 0) {
440 1.9.2.2 pgoyette e->state = ENVSYS_SVALID;
441 1.9.2.2 pgoyette e->value_cur = __SHIFTOUT(val, AXP_BATT_CAP_PERCENT);
442 1.9.2.2 pgoyette }
443 1.9.2.2 pgoyette break;
444 1.9.2.2 pgoyette }
445 1.9.2.2 pgoyette }
446 1.9.2.2 pgoyette
447 1.9.2.2 pgoyette static void
448 1.9.2.2 pgoyette axppmic_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *e)
449 1.9.2.2 pgoyette {
450 1.9.2.2 pgoyette struct axppmic_softc *sc = sme->sme_cookie;
451 1.9.2.2 pgoyette const int flags = I2C_F_POLL;
452 1.9.2.2 pgoyette
453 1.9.2.2 pgoyette switch (e->private) {
454 1.9.2.2 pgoyette case AXP_SENSOR_BATT_CAPACITY:
455 1.9.2.2 pgoyette /* Always update battery capacity (fuel gauge) */
456 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, flags);
457 1.9.2.2 pgoyette axppmic_sensor_update(sme, e);
458 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, flags);
459 1.9.2.2 pgoyette break;
460 1.9.2.2 pgoyette default:
461 1.9.2.2 pgoyette /* Refresh if the sensor is not in valid state */
462 1.9.2.2 pgoyette if (e->state != ENVSYS_SVALID) {
463 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, flags);
464 1.9.2.2 pgoyette axppmic_sensor_update(sme, e);
465 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, flags);
466 1.9.2.2 pgoyette }
467 1.9.2.2 pgoyette break;
468 1.9.2.2 pgoyette }
469 1.9.2.2 pgoyette }
470 1.9.2.2 pgoyette
471 1.9.2.2 pgoyette static int
472 1.9.2.2 pgoyette axppmic_intr(void *priv)
473 1.9.2.2 pgoyette {
474 1.9.2.2 pgoyette struct axppmic_softc *sc = priv;
475 1.9.2.2 pgoyette const struct axppmic_config *c = sc->sc_conf;
476 1.9.2.2 pgoyette const int flags = I2C_F_POLL;
477 1.9.2.2 pgoyette uint8_t stat;
478 1.9.2.2 pgoyette u_int n;
479 1.9.2.2 pgoyette
480 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, flags);
481 1.9.2.2 pgoyette for (n = 1; n <= c->irq_regs; n++) {
482 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_IRQ_STATUS_REG(n), &stat, flags) == 0) {
483 1.9.2.2 pgoyette if (n == c->poklirq.reg && (stat & c->poklirq.mask) != 0)
484 1.9.2.2 pgoyette sysmon_task_queue_sched(0, axppmic_task_shut, sc);
485 1.9.2.2 pgoyette if (n == c->acinirq.reg && (stat & c->acinirq.mask) != 0)
486 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_ACIN_PRESENT]);
487 1.9.2.2 pgoyette if (n == c->vbusirq.reg && (stat & c->vbusirq.mask) != 0)
488 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_VBUS_PRESENT]);
489 1.9.2.2 pgoyette if (n == c->battirq.reg && (stat & c->battirq.mask) != 0)
490 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_PRESENT]);
491 1.9.2.2 pgoyette if (n == c->chargeirq.reg && (stat & c->chargeirq.mask) != 0)
492 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_CHARGING]);
493 1.9.2.2 pgoyette if (n == c->chargestirq.reg && (stat & c->chargestirq.mask) != 0)
494 1.9.2.2 pgoyette axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_STATE]);
495 1.9.2.2 pgoyette
496 1.9.2.2 pgoyette if (stat != 0)
497 1.9.2.2 pgoyette axppmic_write(sc->sc_i2c, sc->sc_addr,
498 1.9.2.2 pgoyette AXP_IRQ_STATUS_REG(n), stat, flags);
499 1.9.2.2 pgoyette }
500 1.9.2.2 pgoyette }
501 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, flags);
502 1.9.2.2 pgoyette
503 1.9.2.2 pgoyette return 1;
504 1.9.2.2 pgoyette }
505 1.9.2.2 pgoyette
506 1.9.2.2 pgoyette static void
507 1.9.2.2 pgoyette axppmic_attach_acadapter(struct axppmic_softc *sc)
508 1.9.2.2 pgoyette {
509 1.9.2.2 pgoyette envsys_data_t *e;
510 1.9.2.2 pgoyette
511 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_ACIN_PRESENT];
512 1.9.2.2 pgoyette e->private = AXP_SENSOR_ACIN_PRESENT;
513 1.9.2.2 pgoyette e->units = ENVSYS_INDICATOR;
514 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
515 1.9.2.2 pgoyette strlcpy(e->desc, "ACIN present", sizeof(e->desc));
516 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
517 1.9.2.2 pgoyette
518 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_VBUS_PRESENT];
519 1.9.2.2 pgoyette e->private = AXP_SENSOR_VBUS_PRESENT;
520 1.9.2.2 pgoyette e->units = ENVSYS_INDICATOR;
521 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
522 1.9.2.2 pgoyette strlcpy(e->desc, "VBUS present", sizeof(e->desc));
523 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
524 1.9.2.2 pgoyette }
525 1.9.2.2 pgoyette
526 1.9.2.2 pgoyette static void
527 1.9.2.2 pgoyette axppmic_attach_battery(struct axppmic_softc *sc)
528 1.9.2.2 pgoyette {
529 1.9.2.2 pgoyette envsys_data_t *e;
530 1.9.2.2 pgoyette uint8_t val;
531 1.9.2.2 pgoyette
532 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
533 1.9.2.2 pgoyette if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_WARN_REG, &val, I2C_F_POLL) == 0) {
534 1.9.2.2 pgoyette sc->sc_warn_thres = __SHIFTOUT(val, AXP_BATT_CAP_WARN_LV1) + 5;
535 1.9.2.2 pgoyette sc->sc_shut_thres = __SHIFTOUT(val, AXP_BATT_CAP_WARN_LV2);
536 1.9.2.2 pgoyette }
537 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, I2C_F_POLL);
538 1.9.2.2 pgoyette
539 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_PRESENT];
540 1.9.2.2 pgoyette e->private = AXP_SENSOR_BATT_PRESENT;
541 1.9.2.2 pgoyette e->units = ENVSYS_INDICATOR;
542 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
543 1.9.2.2 pgoyette strlcpy(e->desc, "battery present", sizeof(e->desc));
544 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
545 1.9.2.2 pgoyette
546 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGING];
547 1.9.2.2 pgoyette e->private = AXP_SENSOR_BATT_CHARGING;
548 1.9.2.2 pgoyette e->units = ENVSYS_BATTERY_CHARGE;
549 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
550 1.9.2.2 pgoyette strlcpy(e->desc, "charging", sizeof(e->desc));
551 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
552 1.9.2.2 pgoyette
553 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_STATE];
554 1.9.2.2 pgoyette e->private = AXP_SENSOR_BATT_CHARGE_STATE;
555 1.9.2.2 pgoyette e->units = ENVSYS_BATTERY_CAPACITY;
556 1.9.2.2 pgoyette e->flags = ENVSYS_FMONSTCHANGED;
557 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
558 1.9.2.2 pgoyette e->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
559 1.9.2.2 pgoyette strlcpy(e->desc, "charge state", sizeof(e->desc));
560 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
561 1.9.2.2 pgoyette
562 1.9.2.2 pgoyette if (sc->sc_conf->has_fuel_gauge) {
563 1.9.2.2 pgoyette e = &sc->sc_sensor[AXP_SENSOR_BATT_CAPACITY];
564 1.9.2.2 pgoyette e->private = AXP_SENSOR_BATT_CAPACITY;
565 1.9.2.2 pgoyette e->units = ENVSYS_INTEGER;
566 1.9.2.2 pgoyette e->state = ENVSYS_SINVALID;
567 1.9.2.2 pgoyette e->flags = ENVSYS_FPERCENT;
568 1.9.2.2 pgoyette strlcpy(e->desc, "battery percent", sizeof(e->desc));
569 1.9.2.2 pgoyette sysmon_envsys_sensor_attach(sc->sc_sme, e);
570 1.9.2.2 pgoyette }
571 1.9.2.2 pgoyette }
572 1.9.2.2 pgoyette
573 1.9.2.2 pgoyette static void
574 1.9.2.2 pgoyette axppmic_attach_sensors(struct axppmic_softc *sc)
575 1.9.2.2 pgoyette {
576 1.9.2.2 pgoyette if (sc->sc_conf->has_battery) {
577 1.9.2.2 pgoyette sc->sc_sme = sysmon_envsys_create();
578 1.9.2.2 pgoyette sc->sc_sme->sme_name = device_xname(sc->sc_dev);
579 1.9.2.2 pgoyette sc->sc_sme->sme_cookie = sc;
580 1.9.2.2 pgoyette sc->sc_sme->sme_refresh = axppmic_sensor_refresh;
581 1.9.2.2 pgoyette sc->sc_sme->sme_class = SME_CLASS_BATTERY;
582 1.9.2.2 pgoyette sc->sc_sme->sme_flags = SME_INIT_REFRESH;
583 1.9.2.2 pgoyette
584 1.9.2.2 pgoyette axppmic_attach_acadapter(sc);
585 1.9.2.2 pgoyette axppmic_attach_battery(sc);
586 1.9.2.2 pgoyette
587 1.9.2.2 pgoyette sysmon_envsys_register(sc->sc_sme);
588 1.9.2.2 pgoyette }
589 1.9.2.2 pgoyette }
590 1.9.2.2 pgoyette
591 1.9.2.2 pgoyette
592 1.9.2.2 pgoyette static int
593 1.9.2.2 pgoyette axppmic_match(device_t parent, cfdata_t match, void *aux)
594 1.9.2.2 pgoyette {
595 1.9.2.2 pgoyette struct i2c_attach_args *ia = aux;
596 1.9.2.2 pgoyette
597 1.9.2.2 pgoyette if (ia->ia_name != NULL) {
598 1.9.2.2 pgoyette if (ia->ia_cookie)
599 1.9.2.2 pgoyette return of_match_compat_data(ia->ia_cookie, compat_data);
600 1.9.2.2 pgoyette else
601 1.9.2.2 pgoyette return 0;
602 1.9.2.2 pgoyette }
603 1.9.2.2 pgoyette
604 1.9.2.2 pgoyette return 1;
605 1.9.2.2 pgoyette }
606 1.9.2.2 pgoyette
607 1.9.2.2 pgoyette static void
608 1.9.2.2 pgoyette axppmic_attach(device_t parent, device_t self, void *aux)
609 1.9.2.2 pgoyette {
610 1.9.2.2 pgoyette struct axppmic_softc *sc = device_private(self);
611 1.9.2.2 pgoyette const struct axppmic_config *c;
612 1.9.2.2 pgoyette struct axpreg_attach_args aaa;
613 1.9.2.2 pgoyette struct i2c_attach_args *ia = aux;
614 1.9.2.2 pgoyette int phandle, child, i;
615 1.9.2.2 pgoyette uint32_t irq_mask;
616 1.9.2.2 pgoyette void *ih;
617 1.9.2.2 pgoyette
618 1.9.2.2 pgoyette c = (void *)of_search_compatible(ia->ia_cookie, compat_data)->data;
619 1.9.2.2 pgoyette
620 1.9.2.2 pgoyette sc->sc_dev = self;
621 1.9.2.2 pgoyette sc->sc_i2c = ia->ia_tag;
622 1.9.2.2 pgoyette sc->sc_addr = ia->ia_addr;
623 1.9.2.2 pgoyette sc->sc_phandle = ia->ia_cookie;
624 1.9.2.2 pgoyette sc->sc_conf = c;
625 1.9.2.2 pgoyette
626 1.9.2.2 pgoyette aprint_naive("\n");
627 1.9.2.2 pgoyette aprint_normal(": %s\n", c->name);
628 1.9.2.2 pgoyette
629 1.9.2.2 pgoyette sc->sc_smpsw.smpsw_name = device_xname(self);
630 1.9.2.2 pgoyette sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
631 1.9.2.2 pgoyette sysmon_pswitch_register(&sc->sc_smpsw);
632 1.9.2.2 pgoyette
633 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
634 1.9.2.2 pgoyette for (i = 1; i <= c->irq_regs; i++) {
635 1.9.2.2 pgoyette irq_mask = 0;
636 1.9.2.2 pgoyette if (i == c->poklirq.reg)
637 1.9.2.2 pgoyette irq_mask |= c->poklirq.mask;
638 1.9.2.2 pgoyette if (i == c->acinirq.reg)
639 1.9.2.2 pgoyette irq_mask |= c->acinirq.mask;
640 1.9.2.2 pgoyette if (i == c->vbusirq.reg)
641 1.9.2.2 pgoyette irq_mask |= c->vbusirq.mask;
642 1.9.2.2 pgoyette if (i == c->battirq.reg)
643 1.9.2.2 pgoyette irq_mask |= c->battirq.mask;
644 1.9.2.2 pgoyette if (i == c->chargeirq.reg)
645 1.9.2.2 pgoyette irq_mask |= c->chargeirq.mask;
646 1.9.2.2 pgoyette if (i == c->chargestirq.reg)
647 1.9.2.2 pgoyette irq_mask |= c->chargestirq.mask;
648 1.9.2.2 pgoyette axppmic_write(sc->sc_i2c, sc->sc_addr, AXP_IRQ_ENABLE_REG(i), irq_mask, I2C_F_POLL);
649 1.9.2.2 pgoyette }
650 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, I2C_F_POLL);
651 1.9.2.2 pgoyette
652 1.9.2.2 pgoyette ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
653 1.9.2.2 pgoyette axppmic_intr, sc);
654 1.9.2.2 pgoyette if (ih == NULL) {
655 1.9.2.2 pgoyette aprint_error_dev(self, "WARNING: couldn't establish interrupt handler\n");
656 1.9.2.2 pgoyette }
657 1.9.2.2 pgoyette
658 1.9.2.2 pgoyette fdtbus_register_power_controller(sc->sc_dev, sc->sc_phandle,
659 1.9.2.2 pgoyette &axppmic_power_funcs);
660 1.9.2.2 pgoyette
661 1.9.2.2 pgoyette phandle = of_find_firstchild_byname(sc->sc_phandle, "regulators");
662 1.9.2.2 pgoyette if (phandle > 0) {
663 1.9.2.2 pgoyette aaa.reg_i2c = sc->sc_i2c;
664 1.9.2.2 pgoyette aaa.reg_addr = sc->sc_addr;
665 1.9.2.2 pgoyette for (i = 0; i < c->ncontrols; i++) {
666 1.9.2.2 pgoyette const struct axppmic_ctrl *ctrl = &c->controls[i];
667 1.9.2.2 pgoyette child = of_find_firstchild_byname(phandle, ctrl->c_name);
668 1.9.2.2 pgoyette if (child <= 0)
669 1.9.2.2 pgoyette continue;
670 1.9.2.2 pgoyette aaa.reg_ctrl = ctrl;
671 1.9.2.2 pgoyette aaa.reg_phandle = child;
672 1.9.2.2 pgoyette config_found(sc->sc_dev, &aaa, NULL);
673 1.9.2.2 pgoyette }
674 1.9.2.2 pgoyette }
675 1.9.2.2 pgoyette
676 1.9.2.2 pgoyette if (c->has_battery)
677 1.9.2.2 pgoyette axppmic_attach_sensors(sc);
678 1.9.2.2 pgoyette }
679 1.9.2.2 pgoyette
680 1.9.2.2 pgoyette static int
681 1.9.2.2 pgoyette axpreg_acquire(device_t dev)
682 1.9.2.2 pgoyette {
683 1.9.2.2 pgoyette return 0;
684 1.9.2.2 pgoyette }
685 1.9.2.2 pgoyette
686 1.9.2.2 pgoyette static void
687 1.9.2.2 pgoyette axpreg_release(device_t dev)
688 1.9.2.2 pgoyette {
689 1.9.2.2 pgoyette }
690 1.9.2.2 pgoyette
691 1.9.2.2 pgoyette static int
692 1.9.2.2 pgoyette axpreg_enable(device_t dev, bool enable)
693 1.9.2.2 pgoyette {
694 1.9.2.2 pgoyette struct axpreg_softc *sc = device_private(dev);
695 1.9.2.2 pgoyette const struct axppmic_ctrl *c = sc->sc_ctrl;
696 1.9.2.2 pgoyette const int flags = (cold ? I2C_F_POLL : 0);
697 1.9.2.2 pgoyette uint8_t val;
698 1.9.2.2 pgoyette int error;
699 1.9.2.2 pgoyette
700 1.9.2.2 pgoyette if (!c->c_enable_mask)
701 1.9.2.2 pgoyette return EINVAL;
702 1.9.2.2 pgoyette
703 1.9.2.2 pgoyette iic_acquire_bus(sc->sc_i2c, flags);
704 1.9.2.2 pgoyette if ((error = axppmic_read(sc->sc_i2c, sc->sc_addr, c->c_enable_reg, &val, flags)) == 0) {
705 1.9.2.2 pgoyette if (enable)
706 1.9.2.2 pgoyette val |= c->c_enable_mask;
707 1.9.2.2 pgoyette else
708 1.9.2.2 pgoyette val &= ~c->c_enable_mask;
709 1.9.2.2 pgoyette error = axppmic_write(sc->sc_i2c, sc->sc_addr, c->c_enable_reg, val, flags);
710 1.9.2.2 pgoyette }
711 1.9.2.2 pgoyette iic_release_bus(sc->sc_i2c, flags);
712 1.9.2.2 pgoyette
713 1.9.2.2 pgoyette return error;
714 1.9.2.2 pgoyette }
715 1.9.2.2 pgoyette
716 1.9.2.2 pgoyette static int
717 1.9.2.2 pgoyette axpreg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
718 1.9.2.2 pgoyette {
719 1.9.2.2 pgoyette struct axpreg_softc *sc = device_private(dev);
720 1.9.2.2 pgoyette const struct axppmic_ctrl *c = sc->sc_ctrl;
721 1.9.2.2 pgoyette
722 1.9.2.2 pgoyette return axppmic_set_voltage(sc->sc_i2c, sc->sc_addr, c,
723 1.9.2.2 pgoyette min_uvol / 1000, max_uvol / 1000);
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 axpreg_get_voltage(device_t dev, u_int *puvol)
728 1.9.2.2 pgoyette {
729 1.9.2.2 pgoyette struct axpreg_softc *sc = device_private(dev);
730 1.9.2.2 pgoyette const struct axppmic_ctrl *c = sc->sc_ctrl;
731 1.9.2.2 pgoyette int error;
732 1.9.2.2 pgoyette u_int vol;
733 1.9.2.2 pgoyette
734 1.9.2.2 pgoyette error = axppmic_get_voltage(sc->sc_i2c, sc->sc_addr, c, &vol);
735 1.9.2.2 pgoyette if (error)
736 1.9.2.2 pgoyette return error;
737 1.9.2.2 pgoyette
738 1.9.2.2 pgoyette *puvol = vol * 1000;
739 1.9.2.2 pgoyette return 0;
740 1.9.2.2 pgoyette }
741 1.9.2.2 pgoyette
742 1.9.2.2 pgoyette static struct fdtbus_regulator_controller_func axpreg_funcs = {
743 1.9.2.2 pgoyette .acquire = axpreg_acquire,
744 1.9.2.2 pgoyette .release = axpreg_release,
745 1.9.2.2 pgoyette .enable = axpreg_enable,
746 1.9.2.2 pgoyette .set_voltage = axpreg_set_voltage,
747 1.9.2.2 pgoyette .get_voltage = axpreg_get_voltage,
748 1.9.2.2 pgoyette };
749 1.9.2.2 pgoyette
750 1.9.2.2 pgoyette static int
751 1.9.2.2 pgoyette axpreg_match(device_t parent, cfdata_t match, void *aux)
752 1.9.2.2 pgoyette {
753 1.9.2.2 pgoyette return 1;
754 1.9.2.2 pgoyette }
755 1.9.2.2 pgoyette
756 1.9.2.2 pgoyette static void
757 1.9.2.2 pgoyette axpreg_attach(device_t parent, device_t self, void *aux)
758 1.9.2.2 pgoyette {
759 1.9.2.2 pgoyette struct axpreg_softc *sc = device_private(self);
760 1.9.2.2 pgoyette struct axpreg_attach_args *aaa = aux;
761 1.9.2.2 pgoyette const int phandle = aaa->reg_phandle;
762 1.9.2.2 pgoyette const char *name;
763 1.9.2.2 pgoyette
764 1.9.2.2 pgoyette sc->sc_dev = self;
765 1.9.2.2 pgoyette sc->sc_i2c = aaa->reg_i2c;
766 1.9.2.2 pgoyette sc->sc_addr = aaa->reg_addr;
767 1.9.2.2 pgoyette sc->sc_ctrl = aaa->reg_ctrl;
768 1.9.2.2 pgoyette
769 1.9.2.2 pgoyette fdtbus_register_regulator_controller(self, phandle,
770 1.9.2.2 pgoyette &axpreg_funcs);
771 1.9.2.2 pgoyette
772 1.9.2.2 pgoyette aprint_naive("\n");
773 1.9.2.2 pgoyette name = fdtbus_get_string(phandle, "regulator-name");
774 1.9.2.2 pgoyette if (name)
775 1.9.2.2 pgoyette aprint_normal(": %s\n", name);
776 1.9.2.2 pgoyette else
777 1.9.2.2 pgoyette aprint_normal("\n");
778 1.9.2.2 pgoyette }
779 1.9.2.2 pgoyette
780 1.9.2.2 pgoyette CFATTACH_DECL_NEW(axppmic, sizeof(struct axppmic_softc),
781 1.9.2.2 pgoyette axppmic_match, axppmic_attach, NULL, NULL);
782 1.9.2.2 pgoyette
783 1.9.2.2 pgoyette CFATTACH_DECL_NEW(axpreg, sizeof(struct axpreg_softc),
784 1.9.2.2 pgoyette axpreg_match, axpreg_attach, NULL, NULL);
785