2 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 * TPS65217 PMIC companion chip for AM335x SoC sitting on I2C bus
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>
40 #include <sys/reboot.h>
41 #include <sys/resource.h>
44 #include <dev/iicbus/iicbus.h>
45 #include <dev/iicbus/iiconf.h>
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
51 #include <arm/ti/am335x/am335x_rtcvar.h>
53 #include "iicbus_if.h"
60 /* TPS65217 Reisters */
61 #define TPS65217_CHIPID_REG 0x00
62 #define TPS65217_STATUS_REG 0x0A
63 #define TPS65217_STATUS_OFF (1U << 7)
64 #define TPS65217_STATUS_ACPWR (1U << 3)
65 #define TPS65217_STATUS_USBPWR (1U << 2)
66 #define TPS65217_STATUS_BT (1U << 0)
68 #define MAX_IIC_DATA_SIZE 2
71 struct am335x_pmic_softc {
74 struct intr_config_hook enum_hook;
77 static void am335x_pmic_shutdown(void *, int);
80 am335x_pmic_read(device_t dev, uint8_t addr, uint8_t *data, uint8_t size)
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 },
87 return (iicbus_transfer(dev, msg, 2));
91 am335x_pmic_write(device_t dev, uint8_t address, uint8_t *data, uint8_t size)
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 },
99 if (size > MAX_IIC_DATA_SIZE)
103 memcpy(buffer + 1, data, size);
105 return (iicbus_transfer(dev, msg, 1));
109 am335x_pmic_probe(device_t dev)
111 struct am335x_pmic_softc *sc;
113 if (!ofw_bus_is_compatible(dev, "ti,am335x-pmic"))
116 sc = device_get_softc(dev);
118 sc->sc_addr = iicbus_get_addr(dev);
120 device_set_desc(dev, "TI TPS65217 Power Management IC");
126 am335x_pmic_start(void *xdev)
128 struct am335x_pmic_softc *sc;
129 device_t dev = (device_t)xdev;
132 char pwr[4][11] = {"Unknown", "USB", "AC", "USB and AC"};
134 sc = device_get_softc(dev);
136 am335x_pmic_read(dev, TPS65217_CHIPID_REG, ®, 1);
139 sprintf(name, "TPS65217A ver 1.%u", reg & 0xF);
142 sprintf(name, "TPS65217B ver 1.%u", reg & 0xF);
145 sprintf(name, "TPS65217C ver 1.%u", reg & 0xF);
148 sprintf(name, "TPS65217D ver 1.%u", reg & 0xF);
151 sprintf(name, "Unknown PMIC");
154 am335x_pmic_read(dev, TPS65217_STATUS_REG, ®, 1);
155 device_printf(dev, "%s powered by %s\n", name, pwr[(reg>>2)&0x03]);
157 EVENTHANDLER_REGISTER(shutdown_final, am335x_pmic_shutdown, dev,
160 config_intrhook_disestablish(&sc->enum_hook);
164 am335x_pmic_attach(device_t dev)
166 struct am335x_pmic_softc *sc;
168 sc = device_get_softc(dev);
170 sc->enum_hook.ich_func = am335x_pmic_start;
171 sc->enum_hook.ich_arg = dev;
173 if (config_intrhook_establish(&sc->enum_hook) != 0)
180 am335x_pmic_shutdown(void *xdev, int howto)
185 if (!(howto & RB_POWEROFF))
187 dev = (device_t)xdev;
188 /* Set the OFF bit on status register to start the shutdown sequence. */
189 reg = TPS65217_STATUS_OFF;
190 am335x_pmic_write(dev, TPS65217_STATUS_REG, ®, 1);
191 /* Toggle pmic_pwr_enable to shutdown the PMIC. */
192 am335x_rtc_pmic_pwr_toggle();
195 static device_method_t am335x_pmic_methods[] = {
196 DEVMETHOD(device_probe, am335x_pmic_probe),
197 DEVMETHOD(device_attach, am335x_pmic_attach),
201 static driver_t am335x_pmic_driver = {
204 sizeof(struct am335x_pmic_softc),
207 static devclass_t am335x_pmic_devclass;
209 DRIVER_MODULE(am335x_pmic, iicbus, am335x_pmic_driver, am335x_pmic_devclass, 0, 0);
210 MODULE_VERSION(am335x_pmic, 1);
211 MODULE_DEPEND(am335x_pmic, iicbus, 1, 1, 1);