Deleted Added
full compact
am335x_pmic.c (253025) am335x_pmic.c (277042)
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

--- 11 unchanged lines hidden (view full) ---

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>
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

--- 11 unchanged lines hidden (view full) ---

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 253025 2013-07-08 05:06:32Z gonzo $");
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>
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>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#include <sys/clock.h>
37#include <sys/time.h>
38#include <sys/bus.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>
39#include <sys/resource.h>
40#include <sys/rman.h>
41
42#include <dev/iicbus/iicbus.h>
43#include <dev/iicbus/iiconf.h>
44
45#include <dev/ofw/openfirm.h>
46#include <dev/ofw/ofw_bus.h>
47#include <dev/ofw/ofw_bus_subr.h>
48
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
49#include "iicbus_if.h"
50
51#define TPS65217A 0x7
52#define TPS65217B 0xF
53#define TPS65217C 0xE
54#define TPS65217D 0x6
55
56/* TPS65217 Reisters */
57#define TPS65217_CHIPID_REG 0x00
58#define TPS65217_STATUS_REG 0x0A
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)
59
60#define MAX_IIC_DATA_SIZE 2
61
62
63struct am335x_pmic_softc {
64 device_t sc_dev;
65 uint32_t sc_addr;
66 struct intr_config_hook enum_hook;
67};
68
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
69static int
70am335x_pmic_read(device_t dev, uint8_t addr, uint8_t *data, uint8_t size)
71{
72 struct am335x_pmic_softc *sc = device_get_softc(dev);
73 struct iic_msg msg[] = {
74 { sc->sc_addr, IIC_M_WR, 1, &addr },
75 { sc->sc_addr, IIC_M_RD, size, data },
76 };
77 return (iicbus_transfer(dev, msg, 2));
78}
79
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
80#ifdef notyet
81static int
82am335x_pmic_write(device_t dev, uint8_t address, uint8_t *data, uint8_t size)
83{
84 uint8_t buffer[MAX_IIC_DATA_SIZE + 1];
85 struct am335x_pmic_softc *sc = device_get_softc(dev);
86 struct iic_msg msg[] = {
87 { sc->sc_addr, IIC_M_WR, size + 1, buffer },
88 };
89
90 if (size > MAX_IIC_DATA_SIZE)
91 return (ENOMEM);
92
93 buffer[0] = address;
94 memcpy(buffer + 1, data, size);
95
96 return (iicbus_transfer(dev, msg, 1));
97}
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}
98#endif
99
100static int
101am335x_pmic_probe(device_t dev)
102{
103 struct am335x_pmic_softc *sc;
104
105 if (!ofw_bus_is_compatible(dev, "ti,am335x-pmic"))
106 return (ENXIO);

--- 34 unchanged lines hidden (view full) ---

141 break;
142 default:
143 sprintf(name, "Unknown PMIC");
144 }
145
146 am335x_pmic_read(dev, TPS65217_STATUS_REG, &reg, 1);
147 device_printf(dev, "%s powered by %s\n", name, pwr[(reg>>2)&0x03]);
148
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);

--- 34 unchanged lines hidden (view full) ---

149 break;
150 default:
151 sprintf(name, "Unknown PMIC");
152 }
153
154 am335x_pmic_read(dev, TPS65217_STATUS_REG, &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
149 config_intrhook_disestablish(&sc->enum_hook);
150}
151
152static int
153am335x_pmic_attach(device_t dev)
154{
155 struct am335x_pmic_softc *sc;
156
157 sc = device_get_softc(dev);
158
159 sc->enum_hook.ich_func = am335x_pmic_start;
160 sc->enum_hook.ich_arg = dev;
161
162 if (config_intrhook_establish(&sc->enum_hook) != 0)
163 return (ENOMEM);
164
165 return (0);
166}
167
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, &reg, 1);
191 /* Toggle pmic_pwr_enable to shutdown the PMIC. */
192 am335x_rtc_pmic_pwr_toggle();
193}
194
168static device_method_t am335x_pmic_methods[] = {
169 DEVMETHOD(device_probe, am335x_pmic_probe),
170 DEVMETHOD(device_attach, am335x_pmic_attach),
171 {0, 0},
172};
173
174static driver_t am335x_pmic_driver = {
175 "am335x_pmic",
176 am335x_pmic_methods,
177 sizeof(struct am335x_pmic_softc),
178};
179
180static devclass_t am335x_pmic_devclass;
181
182DRIVER_MODULE(am335x_pmic, iicbus, am335x_pmic_driver, am335x_pmic_devclass, 0, 0);
183MODULE_VERSION(am335x_pmic, 1);
184MODULE_DEPEND(am335x_pmic, iicbus, 1, 1, 1);
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);