xref: /freebsd/sys/arm/ti/am335x/am335x_pmic.c (revision 56e53cb8ef000c3ef72337a4095987a932cdedef)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 /*
32 * TI TPS65217 PMIC companion chip for AM335x SoC sitting on I2C bus
33 */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/clock.h>
40 #include <sys/time.h>
41 #include <sys/bus.h>
42 #include <sys/proc.h>
43 #include <sys/reboot.h>
44 #include <sys/resource.h>
45 #include <sys/rman.h>
46 
47 #include <dev/iicbus/iicbus.h>
48 #include <dev/iicbus/iiconf.h>
49 
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53 
54 #include <arm/ti/am335x/am335x_rtcvar.h>
55 #include <arm/ti/am335x/tps65217x.h>
56 
57 #include "iicbus_if.h"
58 
59 #define MAX_IIC_DATA_SIZE	2
60 
61 
62 struct am335x_pmic_softc {
63 	device_t		sc_dev;
64 	uint32_t		sc_addr;
65 	struct intr_config_hook enum_hook;
66 	struct resource		*sc_irq_res;
67 	void			*sc_intrhand;
68 };
69 
70 static const char *tps65217_voreg_c[4] = {"4.10V", "4.15V", "4.20V", "4.25V"};
71 
72 static int am335x_pmic_bootverbose = 0;
73 TUNABLE_INT("hw.am335x_pmic.bootverbose", &am335x_pmic_bootverbose);
74 static char am335x_pmic_vo[6];
75 TUNABLE_STR("hw.am335x_pmic.vo", am335x_pmic_vo, sizeof(am335x_pmic_vo));
76 
77 static void am335x_pmic_shutdown(void *, int);
78 
79 static int
80 am335x_pmic_read(device_t dev, uint8_t addr, uint8_t *data, uint8_t size)
81 {
82 	struct am335x_pmic_softc *sc = device_get_softc(dev);
83 	struct iic_msg msg[] = {
84 		{ sc->sc_addr, IIC_M_WR, 1, &addr },
85 		{ sc->sc_addr, IIC_M_RD, size, data },
86 	};
87 	return (iicbus_transfer(dev, msg, 2));
88 }
89 
90 static int
91 am335x_pmic_write(device_t dev, uint8_t address, uint8_t *data, uint8_t size)
92 {
93 	uint8_t buffer[MAX_IIC_DATA_SIZE + 1];
94 	struct am335x_pmic_softc *sc = device_get_softc(dev);
95 	struct iic_msg msg[] = {
96 		{ sc->sc_addr, IIC_M_WR, size + 1, buffer },
97 	};
98 
99 	if (size > MAX_IIC_DATA_SIZE)
100 		return (ENOMEM);
101 
102 	buffer[0] = address;
103 	memcpy(buffer + 1, data, size);
104 
105 	return (iicbus_transfer(dev, msg, 1));
106 }
107 
108 static void
109 am335x_pmic_intr(void *arg)
110 {
111 	struct am335x_pmic_softc *sc = (struct am335x_pmic_softc *)arg;
112 	struct tps65217_status_reg status_reg;
113 	struct tps65217_int_reg int_reg;
114 	int rv;
115 	char notify_buf[16];
116 
117 	THREAD_SLEEPING_OK();
118 	rv = am335x_pmic_read(sc->sc_dev, TPS65217_INT_REG, (uint8_t *)&int_reg, 1);
119 	if (rv != 0) {
120 		device_printf(sc->sc_dev, "Cannot read interrupt register\n");
121 		THREAD_NO_SLEEPING();
122 		return;
123 	}
124 	rv = am335x_pmic_read(sc->sc_dev, TPS65217_STATUS_REG, (uint8_t *)&status_reg, 1);
125 	if (rv != 0) {
126 		device_printf(sc->sc_dev, "Cannot read status register\n");
127 		THREAD_NO_SLEEPING();
128 		return;
129 	}
130 	THREAD_NO_SLEEPING();
131 
132 	if (int_reg.pbi && status_reg.pb)
133 		shutdown_nice(RB_POWEROFF);
134 	if (int_reg.aci) {
135 		snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x",
136 		    status_reg.acpwr);
137 		devctl_notify_f("ACPI", "ACAD", "power", notify_buf, M_NOWAIT);
138 	}
139 }
140 
141 static int
142 am335x_pmic_probe(device_t dev)
143 {
144 	struct am335x_pmic_softc *sc;
145 
146 	if (!ofw_bus_is_compatible(dev, "ti,tps65217"))
147 		return (ENXIO);
148 
149 	sc = device_get_softc(dev);
150 	sc->sc_dev = dev;
151 	/* Convert to 8-bit addressing */
152 	sc->sc_addr = iicbus_get_addr(dev);
153 
154 	device_set_desc(dev, "TI TPS65217 Power Management IC");
155 
156 	return (0);
157 }
158 
159 static void
160 am335x_pmic_dump_chgconfig(device_t dev)
161 {
162 	struct tps65217_chgconfig0_reg reg0;
163 	struct tps65217_chgconfig1_reg reg1;
164 	struct tps65217_chgconfig2_reg reg2;
165 	struct tps65217_chgconfig3_reg reg3;
166 	const char *e_d[] = {"enabled", "disabled"};
167 	const char *d_e[] = {"disabled", "enabled"};
168 	const char *i_a[] = {"inactive", "active"};
169 	const char *f_t[] = {"false", "true"};
170 	const char *timer_c[] = {"4h", "5h", "6h", "8h"};
171 	const char *ntc_type_c[] = {"100k", "10k"};
172 	const char *vprechg_c[] = {"2.9V", "2.5V"};
173 	const char *trange_c[] = {"0-45 C", "0-60 C"};
174 	const char *termif_c[] = {"2.5%", "7.5%", "15%", "18%"};
175 	const char *pchrgt_c[] = {"30 min", "60 min"};
176 	const char *dppmth_c[] = {"3.50V", "3.75V", "4.00V", "4.25V"};
177 	const char *ichrg_c[] = {"300mA", "400mA", "500mA", "700mA"};
178 
179 	am335x_pmic_read(dev, TPS65217_CHGCONFIG0_REG, (uint8_t *)&reg0, 1);
180 	device_printf(dev, " BAT TEMP/NTC ERROR: %s\n", f_t[reg0.battemp]);
181 	device_printf(dev, " Pre-charge timer time-out: %s\n", f_t[reg0.pchgtout]);
182 	device_printf(dev, " Charge timer time-out: %s\n", f_t[reg0.chgtout]);
183 	device_printf(dev, " Charger active: %s\n", f_t[reg0.active]);
184 	device_printf(dev, " Termination current detected: %s\n", f_t[reg0.termi]);
185 	device_printf(dev, " Thermal suspend: %s\n", f_t[reg0.tsusp]);
186 	device_printf(dev, " DPPM active: %s\n", f_t[reg0.dppm]);
187 	device_printf(dev, " Thermal regulation: %s\n", i_a[reg0.treg]);
188 
189 	am335x_pmic_read(dev, TPS65217_CHGCONFIG1_REG, (uint8_t *)&reg1, 1);
190 	device_printf(dev, " Charger: %s\n", d_e[reg1.chg_en]);
191 	device_printf(dev, " Suspend charge: %s\n", i_a[reg1.susp]);
192 	device_printf(dev, " Charge termination: %s\n", e_d[reg1.term]);
193 	device_printf(dev, " Charger reset: %s\n", i_a[reg1.reset]);
194 	device_printf(dev, " NTC TYPE: %s\n", ntc_type_c[reg1.ntc_type]);
195 	device_printf(dev, " Safety timer: %s\n", d_e[reg1.tmr_en]);
196 	device_printf(dev, " Charge safety timer: %s\n", timer_c[reg1.timer]);
197 
198 	am335x_pmic_read(dev, TPS65217_CHGCONFIG2_REG, (uint8_t *)&reg2, 1);
199 	device_printf(dev, " Charge voltage: %s\n", tps65217_voreg_c[reg2.voreg]);
200 	device_printf(dev, " Pre-charge to fast charge transition voltage: %s\n",
201 	    vprechg_c[reg2.vprechg]);
202 	device_printf(dev, " Dynamic timer function: %s\n", d_e[reg2.dyntmr]);
203 
204 	am335x_pmic_read(dev, TPS65217_CHGCONFIG3_REG, (uint8_t *)&reg3, 1);
205 	device_printf(dev, " Temperature range for charging: %s\n", trange_c[reg3.trange]);
206 	device_printf(dev, " Termination current factor: %s\n", termif_c[reg3.termif]);
207 	device_printf(dev, " Pre-charge time: %s\n", pchrgt_c[reg3.pchrgt]);
208 	device_printf(dev, " Power path DPPM threshold: %s\n", dppmth_c[reg3.dppmth]);
209 	device_printf(dev, " Charge current: %s\n", ichrg_c[reg3.ichrg]);
210 }
211 
212 static void
213 am335x_pmic_setvo(device_t dev, uint8_t vo)
214 {
215 	struct tps65217_chgconfig2_reg reg2;
216 
217 	am335x_pmic_read(dev, TPS65217_CHGCONFIG2_REG, (uint8_t *)&reg2, 1);
218 	reg2.voreg = vo;
219 	am335x_pmic_write(dev, TPS65217_CHGCONFIG2_REG, (uint8_t *)&reg2, 1);
220 }
221 
222 static void
223 am335x_pmic_start(void *xdev)
224 {
225 	struct am335x_pmic_softc *sc;
226 	device_t dev = (device_t)xdev;
227 	struct tps65217_status_reg status_reg;
228 	struct tps65217_chipid_reg chipid_reg;
229 	uint8_t reg, vo;
230 	char name[20];
231 	char pwr[4][11] = {"Battery", "USB", "AC", "USB and AC"};
232 	int rv;
233 
234 	sc = device_get_softc(dev);
235 
236 	am335x_pmic_read(dev, TPS65217_CHIPID_REG, (uint8_t *)&chipid_reg, 1);
237 	switch (chipid_reg.chip) {
238 		case TPS65217A:
239 			sprintf(name, "TPS65217A ver 1.%u", chipid_reg.rev);
240 			break;
241 		case TPS65217B:
242 			sprintf(name, "TPS65217B ver 1.%u", chipid_reg.rev);
243 			break;
244 		case TPS65217C:
245 			sprintf(name, "TPS65217C ver 1.%u", chipid_reg.rev);
246 			break;
247 		case TPS65217D:
248 			sprintf(name, "TPS65217D ver 1.%u", chipid_reg.rev);
249 			break;
250 		default:
251 			sprintf(name, "Unknown PMIC");
252 	}
253 
254 	am335x_pmic_read(dev, TPS65217_STATUS_REG, (uint8_t *)&status_reg, 1);
255 	device_printf(dev, "%s powered by %s\n", name,
256 	    pwr[status_reg.usbpwr | (status_reg.acpwr << 1)]);
257 
258 	if (am335x_pmic_vo[0] != '\0') {
259 		for (vo = 0; vo < 4; vo++) {
260 			if (strcmp(tps65217_voreg_c[vo], am335x_pmic_vo) == 0)
261 				break;
262 		}
263 		if (vo == 4) {
264 			device_printf(dev, "WARNING: hw.am335x_pmic.vo=\"%s\""
265 			    ": unsupported value\n", am335x_pmic_vo);
266 		} else {
267 			am335x_pmic_setvo(dev, vo);
268 		}
269 	}
270 
271 	if (bootverbose || am335x_pmic_bootverbose) {
272 		am335x_pmic_dump_chgconfig(dev);
273 	}
274 
275 	EVENTHANDLER_REGISTER(shutdown_final, am335x_pmic_shutdown, dev,
276 	    SHUTDOWN_PRI_LAST);
277 
278 	config_intrhook_disestablish(&sc->enum_hook);
279 
280 	/* Unmask all interrupts and clear pending status */
281 	reg = 0;
282 	am335x_pmic_write(dev, TPS65217_INT_REG, &reg, 1);
283 	am335x_pmic_read(dev, TPS65217_INT_REG, &reg, 1);
284 
285 	if (sc->sc_irq_res != NULL) {
286 		rv = bus_setup_intr(dev, sc->sc_irq_res,
287 		    INTR_TYPE_MISC | INTR_MPSAFE, NULL, am335x_pmic_intr,
288 		    sc, &sc->sc_intrhand);
289 		if (rv != 0)
290 			device_printf(dev,
291 			    "Unable to setup the irq handler.\n");
292 	}
293 }
294 
295 static int
296 am335x_pmic_attach(device_t dev)
297 {
298 	struct am335x_pmic_softc *sc;
299 	int rid;
300 
301 	sc = device_get_softc(dev);
302 
303 	rid = 0;
304 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
305 	    RF_ACTIVE);
306 	if (!sc->sc_irq_res) {
307 		device_printf(dev, "cannot allocate interrupt\n");
308 		/* return (ENXIO); */
309 	}
310 
311 	sc->enum_hook.ich_func = am335x_pmic_start;
312 	sc->enum_hook.ich_arg = dev;
313 
314 	if (config_intrhook_establish(&sc->enum_hook) != 0)
315 		return (ENOMEM);
316 
317 	return (0);
318 }
319 
320 static void
321 am335x_pmic_shutdown(void *xdev, int howto)
322 {
323 	device_t dev;
324 	struct tps65217_status_reg reg;
325 
326 	if (!(howto & RB_POWEROFF))
327 		return;
328 	dev = (device_t)xdev;
329 	am335x_pmic_read(dev, TPS65217_STATUS_REG, (uint8_t *)&reg, 1);
330 	/* Set the OFF bit on status register to start the shutdown sequence. */
331 	reg.off = 1;
332 	am335x_pmic_write(dev, TPS65217_STATUS_REG, (uint8_t *)&reg, 1);
333 	/* Toggle pmic_pwr_enable to shutdown the PMIC. */
334 	am335x_rtc_pmic_pwr_toggle();
335 }
336 
337 static device_method_t am335x_pmic_methods[] = {
338 	DEVMETHOD(device_probe,		am335x_pmic_probe),
339 	DEVMETHOD(device_attach,	am335x_pmic_attach),
340 	{0, 0},
341 };
342 
343 static driver_t am335x_pmic_driver = {
344 	"am335x_pmic",
345 	am335x_pmic_methods,
346 	sizeof(struct am335x_pmic_softc),
347 };
348 
349 static devclass_t am335x_pmic_devclass;
350 
351 DRIVER_MODULE(am335x_pmic, iicbus, am335x_pmic_driver, am335x_pmic_devclass, 0, 0);
352 MODULE_VERSION(am335x_pmic, 1);
353 MODULE_DEPEND(am335x_pmic, iicbus, 1, 1, 1);
354