1213904Sandreast/*-
2213904Sandreast * Copyright (c) 2010 Andreas Tobler
3213904Sandreast * All rights reserved.
4213904Sandreast *
5213904Sandreast * Redistribution and use in source and binary forms, with or without
6213904Sandreast * modification, are permitted provided that the following conditions
7213904Sandreast * are met:
8213904Sandreast * 1. Redistributions of source code must retain the above copyright
9213904Sandreast *    notice, this list of conditions and the following disclaimer.
10213904Sandreast * 2. Redistributions in binary form must reproduce the above copyright
11213904Sandreast *    notice, this list of conditions and the following disclaimer in the
12213904Sandreast *    documentation and/or other materials provided with the distribution.
13213904Sandreast *
14213904Sandreast * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15213904Sandreast * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16213904Sandreast * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17213904Sandreast * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18213904Sandreast * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19213904Sandreast * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20213904Sandreast * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21213904Sandreast * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22213904Sandreast * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23213904Sandreast * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24213904Sandreast * SUCH DAMAGE.
25213904Sandreast */
26213904Sandreast
27213904Sandreast#include <sys/cdefs.h>
28213904Sandreast__FBSDID("$FreeBSD$");
29213904Sandreast
30213904Sandreast#include <sys/param.h>
31213904Sandreast#include <sys/bus.h>
32213904Sandreast#include <sys/systm.h>
33213904Sandreast#include <sys/module.h>
34213904Sandreast#include <sys/callout.h>
35213904Sandreast#include <sys/conf.h>
36213904Sandreast#include <sys/cpu.h>
37213904Sandreast#include <sys/ctype.h>
38213904Sandreast#include <sys/kernel.h>
39213904Sandreast#include <sys/reboot.h>
40213904Sandreast#include <sys/rman.h>
41213904Sandreast#include <sys/sysctl.h>
42213904Sandreast#include <sys/limits.h>
43213904Sandreast
44213904Sandreast#include <machine/bus.h>
45213904Sandreast#include <machine/md_var.h>
46213904Sandreast
47213904Sandreast#include <dev/iicbus/iicbus.h>
48213904Sandreast#include <dev/iicbus/iiconf.h>
49213904Sandreast
50213904Sandreast#include <dev/ofw/openfirm.h>
51213904Sandreast#include <dev/ofw/ofw_bus.h>
52222458Snwhitehorn#include <powerpc/powermac/powermac_thermal.h>
53213904Sandreast
54213904Sandreast/* Inlet, Backside, U3 Heatsink sensor: MAX6690. */
55213904Sandreast
56213904Sandreast#define MAX6690_INT_TEMP    0x0
57213904Sandreast#define MAX6690_EXT_TEMP    0x1
58222674Sandreast#define MAX6690_RSL_STATUS  0x2
59213904Sandreast#define MAX6690_EEXT_TEMP   0x10
60213904Sandreast#define MAX6690_IEXT_TEMP   0x11
61213904Sandreast#define MAX6690_TEMP_MASK   0xe0
62213904Sandreast
63213904Sandreaststruct max6690_sensor {
64222458Snwhitehorn	struct pmac_therm therm;
65222458Snwhitehorn	device_t dev;
66222458Snwhitehorn
67213904Sandreast	int     id;
68213904Sandreast};
69213904Sandreast
70213904Sandreast/* Regular bus attachment functions */
71213904Sandreaststatic int  max6690_probe(device_t);
72213904Sandreaststatic int  max6690_attach(device_t);
73213904Sandreast
74213904Sandreast/* Utility functions */
75222458Snwhitehornstatic int  max6690_sensor_read(struct max6690_sensor *sens);
76213904Sandreaststatic int  max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS);
77213904Sandreaststatic void max6690_start(void *xdev);
78222674Sandreaststatic int  max6690_read(device_t dev, uint32_t addr, uint8_t reg,
79222674Sandreast			 uint8_t *data);
80213904Sandreast
81213904Sandreaststruct max6690_softc {
82213904Sandreast	device_t		sc_dev;
83213904Sandreast	struct intr_config_hook enum_hook;
84213904Sandreast	uint32_t                sc_addr;
85213904Sandreast	struct max6690_sensor   *sc_sensors;
86213904Sandreast	int                     sc_nsensors;
87213904Sandreast};
88213904Sandreaststatic device_method_t  max6690_methods[] = {
89213904Sandreast	/* Device interface */
90213904Sandreast	DEVMETHOD(device_probe,		max6690_probe),
91213904Sandreast	DEVMETHOD(device_attach,	max6690_attach),
92213904Sandreast	{ 0, 0 },
93213904Sandreast};
94213904Sandreast
95213904Sandreaststatic driver_t max6690_driver = {
96213904Sandreast	"max6690",
97213904Sandreast	max6690_methods,
98213904Sandreast	sizeof(struct max6690_softc)
99213904Sandreast};
100213904Sandreast
101213904Sandreaststatic devclass_t max6690_devclass;
102213904Sandreast
103213904SandreastDRIVER_MODULE(max6690, iicbus, max6690_driver, max6690_devclass, 0, 0);
104227293Sedstatic MALLOC_DEFINE(M_MAX6690, "max6690", "Temp-Monitor MAX6690");
105213904Sandreast
106213904Sandreaststatic int
107222674Sandreastmax6690_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
108213904Sandreast{
109213904Sandreast	uint8_t buf[4];
110222674Sandreast	uint8_t busy[1], rsl;
111222674Sandreast	int err, try = 0;
112213904Sandreast
113222674Sandreast	/* Busy register RSL. */
114222674Sandreast	rsl = MAX6690_RSL_STATUS;
115222674Sandreast	/* first read the status register, 0x2. If busy, retry. */
116222674Sandreast	struct iic_msg msg[4] = {
117222674Sandreast	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &rsl },
118222674Sandreast	    { addr, IIC_M_RD, 1, busy },
119213904Sandreast	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
120213904Sandreast	    { addr, IIC_M_RD, 1, buf },
121213904Sandreast	};
122213904Sandreast
123222674Sandreast	for (;;)
124222674Sandreast	{
125222674Sandreast		err = iicbus_transfer(dev, msg, 4);
126222674Sandreast		if (err != 0)
127222674Sandreast			goto retry;
128222674Sandreast		if (busy[0] & 0x80)
129222674Sandreast			goto retry;
130222674Sandreast		/* Check for invalid value and retry. */
131222674Sandreast		if (buf[0] == 0xff)
132222674Sandreast			goto retry;
133213904Sandreast
134222674Sandreast		*data = *((uint8_t*)buf);
135222674Sandreast		return (0);
136213904Sandreast
137222674Sandreast	retry:
138222674Sandreast		if (++try > 5) {
139222674Sandreast			device_printf(dev, "iicbus read failed\n");
140222674Sandreast			return (-1);
141222674Sandreast		}
142222674Sandreast		pause("max6690_read", hz);
143222674Sandreast	}
144213904Sandreast}
145213904Sandreast
146213904Sandreaststatic int
147213904Sandreastmax6690_probe(device_t dev)
148213904Sandreast{
149213904Sandreast	const char  *name, *compatible;
150213904Sandreast	struct max6690_softc *sc;
151213904Sandreast
152213904Sandreast	name = ofw_bus_get_name(dev);
153213904Sandreast	compatible = ofw_bus_get_compat(dev);
154213904Sandreast
155213904Sandreast	if (!name)
156213904Sandreast		return (ENXIO);
157213904Sandreast
158213904Sandreast	if (strcmp(name, "temp-monitor") != 0 ||
159213904Sandreast	    strcmp(compatible, "max6690") != 0)
160213904Sandreast		return (ENXIO);
161213904Sandreast
162213904Sandreast	sc = device_get_softc(dev);
163213904Sandreast	sc->sc_dev = dev;
164213904Sandreast	sc->sc_addr = iicbus_get_addr(dev);
165213904Sandreast
166213904Sandreast	device_set_desc(dev, "Temp-Monitor MAX6690");
167213904Sandreast
168213904Sandreast	return (0);
169213904Sandreast}
170213904Sandreast
171213904Sandreast/*
172213904Sandreast * This function returns the number of sensors. If we call it the second time
173213904Sandreast * and we have allocated memory for sc->sc_sensors, we fill in the properties.
174213904Sandreast */
175213904Sandreaststatic int
176213904Sandreastmax6690_fill_sensor_prop(device_t dev)
177213904Sandreast{
178213904Sandreast	phandle_t child;
179213904Sandreast	struct max6690_softc *sc;
180213904Sandreast	u_int id[8];
181213904Sandreast	char location[96];
182213904Sandreast	int i = 0, j, len = 0, prop_len, prev_len = 0;
183213904Sandreast
184213904Sandreast	sc = device_get_softc(dev);
185213904Sandreast
186213904Sandreast	child = ofw_bus_get_node(dev);
187213904Sandreast
188213904Sandreast	/* Fill the sensor location property. */
189213904Sandreast	prop_len = OF_getprop(child, "hwsensor-location", location,
190213904Sandreast			      sizeof(location));
191213904Sandreast	while (len < prop_len) {
192213904Sandreast		if (sc->sc_sensors != NULL)
193222458Snwhitehorn			strcpy(sc->sc_sensors[i].therm.name, location + len);
194213904Sandreast		prev_len = strlen(location + len) + 1;
195213904Sandreast		len += prev_len;
196213904Sandreast		i++;
197213904Sandreast	}
198213904Sandreast	if (sc->sc_sensors == NULL)
199213904Sandreast		return (i);
200213904Sandreast
201213904Sandreast	/* Fill the sensor id property. */
202213904Sandreast	prop_len = OF_getprop(child, "hwsensor-id", id, sizeof(id));
203213904Sandreast	for (j = 0; j < i; j++)
204213904Sandreast		sc->sc_sensors[j].id = (id[j] & 0xf);
205213904Sandreast
206222458Snwhitehorn	/* Fill the sensor zone property. */
207222458Snwhitehorn	prop_len = OF_getprop(child, "hwsensor-zone", id, sizeof(id));
208222458Snwhitehorn	for (j = 0; j < i; j++)
209222458Snwhitehorn		sc->sc_sensors[j].therm.zone = id[j];
210222458Snwhitehorn
211222458Snwhitehorn	/* Set up remaining sensor properties */
212222458Snwhitehorn	for (j = 0; j < i; j++) {
213222458Snwhitehorn		sc->sc_sensors[j].dev = dev;
214222458Snwhitehorn
215222674Sandreast		sc->sc_sensors[j].therm.target_temp = 400 + ZERO_C_TO_K;
216222674Sandreast		sc->sc_sensors[j].therm.max_temp = 800 + ZERO_C_TO_K;
217222458Snwhitehorn
218222458Snwhitehorn		sc->sc_sensors[j].therm.read =
219222458Snwhitehorn		    (int (*)(struct pmac_therm *))(max6690_sensor_read);
220222458Snwhitehorn	}
221222458Snwhitehorn
222213904Sandreast	return (i);
223213904Sandreast}
224213904Sandreaststatic int
225213904Sandreastmax6690_attach(device_t dev)
226213904Sandreast{
227213904Sandreast	struct max6690_softc *sc;
228213904Sandreast
229213904Sandreast	sc = device_get_softc(dev);
230213904Sandreast
231213904Sandreast	sc->enum_hook.ich_func = max6690_start;
232213904Sandreast	sc->enum_hook.ich_arg = dev;
233213904Sandreast
234213904Sandreast	/* We have to wait until interrupts are enabled. I2C read and write
235213904Sandreast	 * only works if the interrupts are available.
236213904Sandreast	 * The unin/i2c is controlled by the htpic on unin. But this is not
237213904Sandreast	 * the master. The openpic on mac-io is controlling the htpic.
238213904Sandreast	 * This one gets attached after the mac-io probing and then the
239213904Sandreast	 * interrupts will be available.
240213904Sandreast	 */
241213904Sandreast
242213904Sandreast	if (config_intrhook_establish(&sc->enum_hook) != 0)
243213904Sandreast		return (ENOMEM);
244213904Sandreast
245213904Sandreast	return (0);
246213904Sandreast}
247213904Sandreast
248213904Sandreaststatic void
249213904Sandreastmax6690_start(void *xdev)
250213904Sandreast{
251213904Sandreast	struct max6690_softc *sc;
252213904Sandreast	struct sysctl_oid *oid, *sensroot_oid;
253213904Sandreast	struct sysctl_ctx_list *ctx;
254239398Sandreast	char sysctl_desc[40], sysctl_name[32];
255213904Sandreast	int i, j;
256213904Sandreast
257213904Sandreast	device_t dev = (device_t)xdev;
258213904Sandreast
259213904Sandreast	sc = device_get_softc(dev);
260213904Sandreast
261213904Sandreast	sc->sc_nsensors = 0;
262213904Sandreast
263213904Sandreast	/* Count the actual number of sensors. */
264213904Sandreast	sc->sc_nsensors = max6690_fill_sensor_prop(dev);
265213904Sandreast
266213904Sandreast	device_printf(dev, "%d sensors detected.\n", sc->sc_nsensors);
267213904Sandreast
268213904Sandreast	if (sc->sc_nsensors == 0)
269213904Sandreast		device_printf(dev, "WARNING: No MAX6690 sensors detected!\n");
270213904Sandreast
271213904Sandreast	sc->sc_sensors = malloc (sc->sc_nsensors * sizeof(struct max6690_sensor),
272213904Sandreast				 M_MAX6690, M_WAITOK | M_ZERO);
273213904Sandreast
274213904Sandreast	ctx = device_get_sysctl_ctx(dev);
275213904Sandreast	sensroot_oid = SYSCTL_ADD_NODE(ctx,
276213904Sandreast	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
277213904Sandreast	    CTLFLAG_RD, 0, "MAX6690 Sensor Information");
278213904Sandreast
279213904Sandreast	/* Now we can fill the properties into the allocated struct. */
280213904Sandreast	sc->sc_nsensors = max6690_fill_sensor_prop(dev);
281213904Sandreast
282222458Snwhitehorn	/* Register with powermac_thermal */
283222458Snwhitehorn	for (i = 0; i < sc->sc_nsensors; i++)
284222458Snwhitehorn		pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
285222458Snwhitehorn
286213904Sandreast	/* Add sysctls for the sensors. */
287213904Sandreast	for (i = 0; i < sc->sc_nsensors; i++) {
288222458Snwhitehorn		for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
289222458Snwhitehorn			sysctl_name[j] =
290222458Snwhitehorn			    tolower(sc->sc_sensors[i].therm.name[j]);
291213904Sandreast			if (isspace(sysctl_name[j]))
292213904Sandreast				sysctl_name[j] = '_';
293213904Sandreast		}
294213904Sandreast		sysctl_name[j] = 0;
295213904Sandreast
296239398Sandreast		sprintf(sysctl_desc,"%s %s", sc->sc_sensors[i].therm.name,
297239398Sandreast			"(C)");
298213904Sandreast		oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
299213904Sandreast				      OID_AUTO,
300213904Sandreast				      sysctl_name, CTLFLAG_RD, 0,
301213904Sandreast				      "Sensor Information");
302213904Sandreast		/* I use i to pass the sensor id. */
303213904Sandreast		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
304213904Sandreast				CTLTYPE_INT | CTLFLAG_RD, dev, i % 2,
305239398Sandreast				max6690_sensor_sysctl, "IK", sysctl_desc);
306213904Sandreast
307213904Sandreast	}
308213904Sandreast	/* Dump sensor location & ID. */
309213904Sandreast	if (bootverbose) {
310213904Sandreast		device_printf(dev, "Sensors\n");
311213904Sandreast		for (i = 0; i < sc->sc_nsensors; i++) {
312213904Sandreast			device_printf(dev, "Location : %s ID: %d\n",
313222458Snwhitehorn				      sc->sc_sensors[i].therm.name,
314213904Sandreast				      sc->sc_sensors[i].id);
315213904Sandreast		}
316213904Sandreast	}
317213904Sandreast
318213904Sandreast	config_intrhook_disestablish(&sc->enum_hook);
319213904Sandreast}
320213904Sandreast
321213904Sandreaststatic int
322222458Snwhitehornmax6690_sensor_read(struct max6690_sensor *sens)
323213904Sandreast{
324213904Sandreast	uint8_t reg_int = 0, reg_ext = 0;
325222674Sandreast	uint8_t integer = 0;
326222674Sandreast	uint8_t fraction = 0;
327222674Sandreast	int err, temp;
328222674Sandreast
329213904Sandreast	struct max6690_softc *sc;
330213904Sandreast
331222458Snwhitehorn	sc = device_get_softc(sens->dev);
332213904Sandreast
333222674Sandreast	/* The internal sensor id's are even, the external are odd. */
334213904Sandreast	if ((sens->id % 2) == 0) {
335213904Sandreast		reg_int = MAX6690_INT_TEMP;
336213904Sandreast		reg_ext = MAX6690_IEXT_TEMP;
337213904Sandreast	} else {
338213904Sandreast		reg_int = MAX6690_EXT_TEMP;
339213904Sandreast		reg_ext = MAX6690_EEXT_TEMP;
340213904Sandreast	}
341213904Sandreast
342222674Sandreast	err = max6690_read(sc->sc_dev, sc->sc_addr, reg_int, &integer);
343222674Sandreast	err = max6690_read(sc->sc_dev, sc->sc_addr, reg_ext, &fraction);
344213904Sandreast
345222674Sandreast	if (err < 0)
346222674Sandreast		return (-1);
347213904Sandreast
348213904Sandreast	fraction &= MAX6690_TEMP_MASK;
349213904Sandreast
350213904Sandreast	/* The temperature is in tenth kelvin, the fractional part resolution
351213904Sandreast	   is 0.125.
352213904Sandreast	*/
353222458Snwhitehorn	temp = (integer * 10) + (fraction >> 5) * 10 / 8;
354213904Sandreast
355222674Sandreast	return (temp + ZERO_C_TO_K);
356213904Sandreast}
357213904Sandreast
358213904Sandreaststatic int
359213904Sandreastmax6690_sensor_sysctl(SYSCTL_HANDLER_ARGS)
360213904Sandreast{
361213904Sandreast	device_t dev;
362213904Sandreast	struct max6690_softc *sc;
363213904Sandreast	struct max6690_sensor *sens;
364213904Sandreast	int error;
365213904Sandreast	unsigned int temp;
366213904Sandreast
367213904Sandreast	dev = arg1;
368213904Sandreast	sc = device_get_softc(dev);
369213904Sandreast	sens = &sc->sc_sensors[arg2];
370213904Sandreast
371222461Snwhitehorn	temp = max6690_sensor_read(sens);
372222461Snwhitehorn	if (temp < 0)
373222458Snwhitehorn		return (EIO);
374213904Sandreast
375213904Sandreast	error = sysctl_handle_int(oidp, &temp, 0, req);
376213904Sandreast
377213904Sandreast	return (error);
378213904Sandreast}
379