1/*-
2 * Copyright (c) 2010 Andreas Tobler
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 ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * 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#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/systm.h>
33#include <sys/module.h>
34#include <sys/callout.h>
35#include <sys/conf.h>
36#include <sys/cpu.h>
37#include <sys/ctype.h>
38#include <sys/kernel.h>
39#include <sys/reboot.h>
40#include <sys/rman.h>
41#include <sys/sysctl.h>
42#include <sys/limits.h>
43
44#include <machine/bus.h>
45#include <machine/md_var.h>
46
47#include <dev/iicbus/iicbus.h>
48#include <dev/iicbus/iiconf.h>
49
50#include <dev/ofw/openfirm.h>
51#include <dev/ofw/ofw_bus.h>
52#include <powerpc/powermac/powermac_thermal.h>
53
54/* Inlet, Backside, U3 Heatsink sensor: MAX6690. */
55
56#define MAX6690_INT_TEMP    0x0
57#define MAX6690_EXT_TEMP    0x1
58#define MAX6690_RSL_STATUS  0x2
59#define MAX6690_EEXT_TEMP   0x10
60#define MAX6690_IEXT_TEMP   0x11
61#define MAX6690_TEMP_MASK   0xe0
62
63struct max6690_sensor {
64	struct pmac_therm therm;
65	device_t dev;
66
67	int     id;
68};
69
70/* Regular bus attachment functions */
71static int  max6690_probe(device_t);
72static int  max6690_attach(device_t);
73
74/* Utility functions */
75static int  max6690_sensor_read(struct max6690_sensor *sens);
76static int  max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS);
77static void max6690_start(void *xdev);
78static int  max6690_read(device_t dev, uint32_t addr, uint8_t reg,
79			 uint8_t *data);
80
81struct max6690_softc {
82	device_t		sc_dev;
83	struct intr_config_hook enum_hook;
84	uint32_t                sc_addr;
85	struct max6690_sensor   *sc_sensors;
86	int                     sc_nsensors;
87};
88static device_method_t  max6690_methods[] = {
89	/* Device interface */
90	DEVMETHOD(device_probe,		max6690_probe),
91	DEVMETHOD(device_attach,	max6690_attach),
92	{ 0, 0 },
93};
94
95static driver_t max6690_driver = {
96	"max6690",
97	max6690_methods,
98	sizeof(struct max6690_softc)
99};
100
101static devclass_t max6690_devclass;
102
103DRIVER_MODULE(max6690, iicbus, max6690_driver, max6690_devclass, 0, 0);
104static MALLOC_DEFINE(M_MAX6690, "max6690", "Temp-Monitor MAX6690");
105
106static int
107max6690_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
108{
109	uint8_t buf[4];
110	uint8_t busy[1], rsl;
111	int err, try = 0;
112
113	/* Busy register RSL. */
114	rsl = MAX6690_RSL_STATUS;
115	/* first read the status register, 0x2. If busy, retry. */
116	struct iic_msg msg[4] = {
117	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &rsl },
118	    { addr, IIC_M_RD, 1, busy },
119	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
120	    { addr, IIC_M_RD, 1, buf },
121	};
122
123	for (;;)
124	{
125		err = iicbus_transfer(dev, msg, 4);
126		if (err != 0)
127			goto retry;
128		if (busy[0] & 0x80)
129			goto retry;
130		/* Check for invalid value and retry. */
131		if (buf[0] == 0xff)
132			goto retry;
133
134		*data = *((uint8_t*)buf);
135		return (0);
136
137	retry:
138		if (++try > 5) {
139			device_printf(dev, "iicbus read failed\n");
140			return (-1);
141		}
142		pause("max6690_read", hz);
143	}
144}
145
146static int
147max6690_probe(device_t dev)
148{
149	const char  *name, *compatible;
150	struct max6690_softc *sc;
151
152	name = ofw_bus_get_name(dev);
153	compatible = ofw_bus_get_compat(dev);
154
155	if (!name)
156		return (ENXIO);
157
158	if (strcmp(name, "temp-monitor") != 0 ||
159	    strcmp(compatible, "max6690") != 0)
160		return (ENXIO);
161
162	sc = device_get_softc(dev);
163	sc->sc_dev = dev;
164	sc->sc_addr = iicbus_get_addr(dev);
165
166	device_set_desc(dev, "Temp-Monitor MAX6690");
167
168	return (0);
169}
170
171/*
172 * This function returns the number of sensors. If we call it the second time
173 * and we have allocated memory for sc->sc_sensors, we fill in the properties.
174 */
175static int
176max6690_fill_sensor_prop(device_t dev)
177{
178	phandle_t child;
179	struct max6690_softc *sc;
180	u_int id[8];
181	char location[96];
182	int i = 0, j, len = 0, prop_len, prev_len = 0;
183
184	sc = device_get_softc(dev);
185
186	child = ofw_bus_get_node(dev);
187
188	/* Fill the sensor location property. */
189	prop_len = OF_getprop(child, "hwsensor-location", location,
190			      sizeof(location));
191	while (len < prop_len) {
192		if (sc->sc_sensors != NULL)
193			strcpy(sc->sc_sensors[i].therm.name, location + len);
194		prev_len = strlen(location + len) + 1;
195		len += prev_len;
196		i++;
197	}
198	if (sc->sc_sensors == NULL)
199		return (i);
200
201	/* Fill the sensor id property. */
202	prop_len = OF_getprop(child, "hwsensor-id", id, sizeof(id));
203	for (j = 0; j < i; j++)
204		sc->sc_sensors[j].id = (id[j] & 0xf);
205
206	/* Fill the sensor zone property. */
207	prop_len = OF_getprop(child, "hwsensor-zone", id, sizeof(id));
208	for (j = 0; j < i; j++)
209		sc->sc_sensors[j].therm.zone = id[j];
210
211	/* Set up remaining sensor properties */
212	for (j = 0; j < i; j++) {
213		sc->sc_sensors[j].dev = dev;
214
215		sc->sc_sensors[j].therm.target_temp = 400 + ZERO_C_TO_K;
216		sc->sc_sensors[j].therm.max_temp = 800 + ZERO_C_TO_K;
217
218		sc->sc_sensors[j].therm.read =
219		    (int (*)(struct pmac_therm *))(max6690_sensor_read);
220	}
221
222	return (i);
223}
224static int
225max6690_attach(device_t dev)
226{
227	struct max6690_softc *sc;
228
229	sc = device_get_softc(dev);
230
231	sc->enum_hook.ich_func = max6690_start;
232	sc->enum_hook.ich_arg = dev;
233
234	/* We have to wait until interrupts are enabled. I2C read and write
235	 * only works if the interrupts are available.
236	 * The unin/i2c is controlled by the htpic on unin. But this is not
237	 * the master. The openpic on mac-io is controlling the htpic.
238	 * This one gets attached after the mac-io probing and then the
239	 * interrupts will be available.
240	 */
241
242	if (config_intrhook_establish(&sc->enum_hook) != 0)
243		return (ENOMEM);
244
245	return (0);
246}
247
248static void
249max6690_start(void *xdev)
250{
251	struct max6690_softc *sc;
252	struct sysctl_oid *oid, *sensroot_oid;
253	struct sysctl_ctx_list *ctx;
254	char sysctl_desc[40], sysctl_name[32];
255	int i, j;
256
257	device_t dev = (device_t)xdev;
258
259	sc = device_get_softc(dev);
260
261	sc->sc_nsensors = 0;
262
263	/* Count the actual number of sensors. */
264	sc->sc_nsensors = max6690_fill_sensor_prop(dev);
265
266	device_printf(dev, "%d sensors detected.\n", sc->sc_nsensors);
267
268	if (sc->sc_nsensors == 0)
269		device_printf(dev, "WARNING: No MAX6690 sensors detected!\n");
270
271	sc->sc_sensors = malloc (sc->sc_nsensors * sizeof(struct max6690_sensor),
272				 M_MAX6690, M_WAITOK | M_ZERO);
273
274	ctx = device_get_sysctl_ctx(dev);
275	sensroot_oid = SYSCTL_ADD_NODE(ctx,
276	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
277	    CTLFLAG_RD, 0, "MAX6690 Sensor Information");
278
279	/* Now we can fill the properties into the allocated struct. */
280	sc->sc_nsensors = max6690_fill_sensor_prop(dev);
281
282	/* Register with powermac_thermal */
283	for (i = 0; i < sc->sc_nsensors; i++)
284		pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
285
286	/* Add sysctls for the sensors. */
287	for (i = 0; i < sc->sc_nsensors; i++) {
288		for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
289			sysctl_name[j] =
290			    tolower(sc->sc_sensors[i].therm.name[j]);
291			if (isspace(sysctl_name[j]))
292				sysctl_name[j] = '_';
293		}
294		sysctl_name[j] = 0;
295
296		sprintf(sysctl_desc,"%s %s", sc->sc_sensors[i].therm.name,
297			"(C)");
298		oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
299				      OID_AUTO,
300				      sysctl_name, CTLFLAG_RD, 0,
301				      "Sensor Information");
302		/* I use i to pass the sensor id. */
303		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
304				CTLTYPE_INT | CTLFLAG_RD, dev, i % 2,
305				max6690_sensor_sysctl, "IK", sysctl_desc);
306
307	}
308	/* Dump sensor location & ID. */
309	if (bootverbose) {
310		device_printf(dev, "Sensors\n");
311		for (i = 0; i < sc->sc_nsensors; i++) {
312			device_printf(dev, "Location : %s ID: %d\n",
313				      sc->sc_sensors[i].therm.name,
314				      sc->sc_sensors[i].id);
315		}
316	}
317
318	config_intrhook_disestablish(&sc->enum_hook);
319}
320
321static int
322max6690_sensor_read(struct max6690_sensor *sens)
323{
324	uint8_t reg_int = 0, reg_ext = 0;
325	uint8_t integer = 0;
326	uint8_t fraction = 0;
327	int err, temp;
328
329	struct max6690_softc *sc;
330
331	sc = device_get_softc(sens->dev);
332
333	/* The internal sensor id's are even, the external are odd. */
334	if ((sens->id % 2) == 0) {
335		reg_int = MAX6690_INT_TEMP;
336		reg_ext = MAX6690_IEXT_TEMP;
337	} else {
338		reg_int = MAX6690_EXT_TEMP;
339		reg_ext = MAX6690_EEXT_TEMP;
340	}
341
342	err = max6690_read(sc->sc_dev, sc->sc_addr, reg_int, &integer);
343
344	if (err < 0)
345		return (-1);
346
347	err = max6690_read(sc->sc_dev, sc->sc_addr, reg_ext, &fraction);
348
349	if (err < 0)
350		return (-1);
351
352	fraction &= MAX6690_TEMP_MASK;
353
354	/* The temperature is in tenth kelvin, the fractional part resolution
355	   is 0.125.
356	*/
357	temp = (integer * 10) + (fraction >> 5) * 10 / 8;
358
359	return (temp + ZERO_C_TO_K);
360}
361
362static int
363max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS)
364{
365	device_t dev;
366	struct max6690_softc *sc;
367	struct max6690_sensor *sens;
368	int error;
369	int temp;
370
371	dev = arg1;
372	sc = device_get_softc(dev);
373	sens = &sc->sc_sensors[arg2];
374
375	temp = max6690_sensor_read(sens);
376	if (temp < 0)
377		return (EIO);
378
379	error = sysctl_handle_int(oidp, &temp, 0, req);
380
381	return (error);
382}
383