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* 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/kernel.h>
35#include <sys/module.h>
36#include <sys/clock.h>
37#include <sys/time.h>
38#include <sys/bus.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
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
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
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
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}
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);
107
108	sc = device_get_softc(dev);
109	sc->sc_dev = dev;
110	sc->sc_addr = iicbus_get_addr(dev);
111
112	device_set_desc(dev, "TI TPS65217 Power Management IC");
113
114	return (0);
115}
116
117static void
118am335x_pmic_start(void *xdev)
119{
120	struct am335x_pmic_softc *sc;
121	device_t dev = (device_t)xdev;
122	uint8_t reg;
123	char name[20];
124	char pwr[4][11] = {"Unknown", "USB", "AC", "USB and AC"};
125
126	sc = device_get_softc(dev);
127
128	am335x_pmic_read(dev, TPS65217_CHIPID_REG, &reg, 1);
129	switch (reg>>4) {
130		case TPS65217A:
131			sprintf(name, "TPS65217A ver 1.%u", reg & 0xF);
132			break;
133		case TPS65217B:
134			sprintf(name, "TPS65217B ver 1.%u", reg & 0xF);
135			break;
136		case TPS65217C:
137			sprintf(name, "TPS65217C ver 1.%u", reg & 0xF);
138			break;
139		case TPS65217D:
140			sprintf(name, "TPS65217D ver 1.%u", reg & 0xF);
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
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
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);
185