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: head/sys/arm/ti/am335x/am335x_pmic.c 277042 2015-01-12 03:23:16Z loos $");
29/*
30* 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/reboot.h>
41#include <sys/resource.h>
42#include <sys/rman.h>
43
44#include <dev/iicbus/iicbus.h>
45#include <dev/iicbus/iiconf.h>
46
47#include <dev/ofw/openfirm.h>
48#include <dev/ofw/ofw_bus.h>
49#include <dev/ofw/ofw_bus_subr.h>
50
51#include <arm/ti/am335x/am335x_rtcvar.h>
52
53#include "iicbus_if.h"
54
55#define TPS65217A 0x7
56#define TPS65217B 0xF
57#define TPS65217C 0xE
58#define TPS65217D 0x6
59
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)
67
68#define MAX_IIC_DATA_SIZE 2
69
70
71struct am335x_pmic_softc {
72 device_t sc_dev;
73 uint32_t sc_addr;
74 struct intr_config_hook enum_hook;
75};
76
77static void am335x_pmic_shutdown(void *, int);
78
79static int
80am335x_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
90static int
91am335x_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
108static int
109am335x_pmic_probe(device_t dev)
110{
111 struct am335x_pmic_softc *sc;
112
113 if (!ofw_bus_is_compatible(dev, "ti,am335x-pmic"))
114 return (ENXIO);
115
116 sc = device_get_softc(dev);
117 sc->sc_dev = dev;
118 sc->sc_addr = iicbus_get_addr(dev);
119
120 device_set_desc(dev, "TI TPS65217 Power Management IC");
121
122 return (0);
123}
124
125static void
126am335x_pmic_start(void *xdev)
127{
128 struct am335x_pmic_softc *sc;
129 device_t dev = (device_t)xdev;
130 uint8_t reg;
131 char name[20];
132 char pwr[4][11] = {"Unknown", "USB", "AC", "USB and AC"};
133
134 sc = device_get_softc(dev);
135
136 am335x_pmic_read(dev, TPS65217_CHIPID_REG, ®, 1);
137 switch (reg>>4) {
138 case TPS65217A:
139 sprintf(name, "TPS65217A ver 1.%u", reg & 0xF);
140 break;
141 case TPS65217B:
142 sprintf(name, "TPS65217B ver 1.%u", reg & 0xF);
143 break;
144 case TPS65217C:
145 sprintf(name, "TPS65217C ver 1.%u", reg & 0xF);
146 break;
147 case TPS65217D:
148 sprintf(name, "TPS65217D ver 1.%u", reg & 0xF);
149 break;
150 default:
151 sprintf(name, "Unknown PMIC");
152 }
153
154 am335x_pmic_read(dev, TPS65217_STATUS_REG, ®, 1);
155 device_printf(dev, "%s powered by %s\n", name, pwr[(reg>>2)&0x03]);
156
157 EVENTHANDLER_REGISTER(shutdown_final, am335x_pmic_shutdown, dev,
158 SHUTDOWN_PRI_LAST);
159
160 config_intrhook_disestablish(&sc->enum_hook);
161}
162
163static int
164am335x_pmic_attach(device_t dev)
165{
166 struct am335x_pmic_softc *sc;
167
168 sc = device_get_softc(dev);
169
170 sc->enum_hook.ich_func = am335x_pmic_start;
171 sc->enum_hook.ich_arg = dev;
172
173 if (config_intrhook_establish(&sc->enum_hook) != 0)
174 return (ENOMEM);
175
176 return (0);
177}
178
179static void
180am335x_pmic_shutdown(void *xdev, int howto)
181{
182 device_t dev;
183 uint8_t reg;
184
185 if (!(howto & RB_POWEROFF))
186 return;
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();
193}
194
195static device_method_t am335x_pmic_methods[] = {
196 DEVMETHOD(device_probe, am335x_pmic_probe),
197 DEVMETHOD(device_attach, am335x_pmic_attach),
198 {0, 0},
199};
200
201static driver_t am335x_pmic_driver = {
202 "am335x_pmic",
203 am335x_pmic_methods,
204 sizeof(struct am335x_pmic_softc),
205};
206
207static devclass_t am335x_pmic_devclass;
208
209DRIVER_MODULE(am335x_pmic, iicbus, am335x_pmic_driver, am335x_pmic_devclass, 0, 0);
210MODULE_VERSION(am335x_pmic, 1);
211MODULE_DEPEND(am335x_pmic, iicbus, 1, 1, 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: head/sys/arm/ti/am335x/am335x_pmic.c 277042 2015-01-12 03:23:16Z loos $");
29/*
30* 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/reboot.h>
41#include <sys/resource.h>
42#include <sys/rman.h>
43
44#include <dev/iicbus/iicbus.h>
45#include <dev/iicbus/iiconf.h>
46
47#include <dev/ofw/openfirm.h>
48#include <dev/ofw/ofw_bus.h>
49#include <dev/ofw/ofw_bus_subr.h>
50
51#include <arm/ti/am335x/am335x_rtcvar.h>
52
53#include "iicbus_if.h"
54
55#define TPS65217A 0x7
56#define TPS65217B 0xF
57#define TPS65217C 0xE
58#define TPS65217D 0x6
59
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)
67
68#define MAX_IIC_DATA_SIZE 2
69
70
71struct am335x_pmic_softc {
72 device_t sc_dev;
73 uint32_t sc_addr;
74 struct intr_config_hook enum_hook;
75};
76
77static void am335x_pmic_shutdown(void *, int);
78
79static int
80am335x_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
90static int
91am335x_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
108static int
109am335x_pmic_probe(device_t dev)
110{
111 struct am335x_pmic_softc *sc;
112
113 if (!ofw_bus_is_compatible(dev, "ti,am335x-pmic"))
114 return (ENXIO);
115
116 sc = device_get_softc(dev);
117 sc->sc_dev = dev;
118 sc->sc_addr = iicbus_get_addr(dev);
119
120 device_set_desc(dev, "TI TPS65217 Power Management IC");
121
122 return (0);
123}
124
125static void
126am335x_pmic_start(void *xdev)
127{
128 struct am335x_pmic_softc *sc;
129 device_t dev = (device_t)xdev;
130 uint8_t reg;
131 char name[20];
132 char pwr[4][11] = {"Unknown", "USB", "AC", "USB and AC"};
133
134 sc = device_get_softc(dev);
135
136 am335x_pmic_read(dev, TPS65217_CHIPID_REG, ®, 1);
137 switch (reg>>4) {
138 case TPS65217A:
139 sprintf(name, "TPS65217A ver 1.%u", reg & 0xF);
140 break;
141 case TPS65217B:
142 sprintf(name, "TPS65217B ver 1.%u", reg & 0xF);
143 break;
144 case TPS65217C:
145 sprintf(name, "TPS65217C ver 1.%u", reg & 0xF);
146 break;
147 case TPS65217D:
148 sprintf(name, "TPS65217D ver 1.%u", reg & 0xF);
149 break;
150 default:
151 sprintf(name, "Unknown PMIC");
152 }
153
154 am335x_pmic_read(dev, TPS65217_STATUS_REG, ®, 1);
155 device_printf(dev, "%s powered by %s\n", name, pwr[(reg>>2)&0x03]);
156
157 EVENTHANDLER_REGISTER(shutdown_final, am335x_pmic_shutdown, dev,
158 SHUTDOWN_PRI_LAST);
159
160 config_intrhook_disestablish(&sc->enum_hook);
161}
162
163static int
164am335x_pmic_attach(device_t dev)
165{
166 struct am335x_pmic_softc *sc;
167
168 sc = device_get_softc(dev);
169
170 sc->enum_hook.ich_func = am335x_pmic_start;
171 sc->enum_hook.ich_arg = dev;
172
173 if (config_intrhook_establish(&sc->enum_hook) != 0)
174 return (ENOMEM);
175
176 return (0);
177}
178
179static void
180am335x_pmic_shutdown(void *xdev, int howto)
181{
182 device_t dev;
183 uint8_t reg;
184
185 if (!(howto & RB_POWEROFF))
186 return;
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();
193}
194
195static device_method_t am335x_pmic_methods[] = {
196 DEVMETHOD(device_probe, am335x_pmic_probe),
197 DEVMETHOD(device_attach, am335x_pmic_attach),
198 {0, 0},
199};
200
201static driver_t am335x_pmic_driver = {
202 "am335x_pmic",
203 am335x_pmic_methods,
204 sizeof(struct am335x_pmic_softc),
205};
206
207static devclass_t am335x_pmic_devclass;
208
209DRIVER_MODULE(am335x_pmic, iicbus, am335x_pmic_driver, am335x_pmic_devclass, 0, 0);
210MODULE_VERSION(am335x_pmic, 1);
211MODULE_DEPEND(am335x_pmic, iicbus, 1, 1, 1);