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