ds1775.c revision 222458
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: head/sys/dev/iicbus/ds1775.c 222458 2011-05-29 18:35:57Z nwhitehorn $");
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#define FCU_ZERO_C_TO_K     2732
55
56/* Drivebay sensor: LM75/DS1775. */
57#define DS1775_TEMP         0x0
58
59/* Regular bus attachment functions */
60static int  ds1775_probe(device_t);
61static int  ds1775_attach(device_t);
62
63struct ds1775_softc {
64	struct pmac_therm	sc_sensor;
65	device_t		sc_dev;
66	struct intr_config_hook enum_hook;
67	uint32_t                sc_addr;
68};
69
70/* Utility functions */
71static int  ds1775_sensor_read(struct ds1775_softc *sc);
72static int  ds1775_sensor_sysctl(SYSCTL_HANDLER_ARGS);
73static void ds1775_start(void *xdev);
74static int  ds1775_read_2(device_t dev, uint32_t addr, uint8_t reg,
75			  uint16_t *data);
76
77static device_method_t  ds1775_methods[] = {
78	/* Device interface */
79	DEVMETHOD(device_probe,		ds1775_probe),
80	DEVMETHOD(device_attach,	ds1775_attach),
81	{ 0, 0 },
82};
83
84static driver_t ds1775_driver = {
85	"ds1775",
86	ds1775_methods,
87	sizeof(struct ds1775_softc)
88};
89
90static devclass_t ds1775_devclass;
91
92DRIVER_MODULE(ds1755, iicbus, ds1775_driver, ds1775_devclass, 0, 0);
93
94static int
95ds1775_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
96{
97	uint8_t buf[4];
98
99	struct iic_msg msg[2] = {
100	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
101	    { addr, IIC_M_RD, 2, buf },
102	};
103
104	if (iicbus_transfer(dev, msg, 2) != 0) {
105		device_printf(dev, "iicbus read failed\n");
106		return (EIO);
107	}
108
109	*data = *((uint16_t*)buf);
110
111	return (0);
112}
113
114static int
115ds1775_probe(device_t dev)
116{
117	const char  *name, *compatible;
118	struct ds1775_softc *sc;
119
120	name = ofw_bus_get_name(dev);
121	compatible = ofw_bus_get_compat(dev);
122
123	if (!name)
124		return (ENXIO);
125
126	if (strcmp(name, "temp-monitor") != 0 ||
127	    (strcmp(compatible, "ds1775") != 0 &&
128	     strcmp(compatible, "lm75") != 0))
129		return (ENXIO);
130
131	sc = device_get_softc(dev);
132	sc->sc_dev = dev;
133	sc->sc_addr = iicbus_get_addr(dev);
134
135	device_set_desc(dev, "Temp-Monitor DS1775");
136
137	return (0);
138}
139
140static int
141ds1775_attach(device_t dev)
142{
143	struct ds1775_softc *sc;
144
145	sc = device_get_softc(dev);
146
147	sc->enum_hook.ich_func = ds1775_start;
148	sc->enum_hook.ich_arg = dev;
149
150	/* We have to wait until interrupts are enabled. I2C read and write
151	 * only works if the interrupts are available.
152	 * The unin/i2c is controlled by the htpic on unin. But this is not
153	 * the master. The openpic on mac-io is controlling the htpic.
154	 * This one gets attached after the mac-io probing and then the
155	 * interrupts will be available.
156	 */
157
158	if (config_intrhook_establish(&sc->enum_hook) != 0)
159		return (ENOMEM);
160
161	return (0);
162}
163
164static void
165ds1775_start(void *xdev)
166{
167	phandle_t child;
168	struct ds1775_softc *sc;
169	struct sysctl_oid *sensroot_oid;
170	struct sysctl_ctx_list *ctx;
171	ssize_t plen;
172	int i;
173	char sysctl_name[40], sysctl_desc[40];
174	const char *units;
175
176	device_t dev = (device_t)xdev;
177
178	sc = device_get_softc(dev);
179
180	child = ofw_bus_get_node(dev);
181
182	ctx = device_get_sysctl_ctx(dev);
183	sensroot_oid = device_get_sysctl_tree(dev);
184
185	OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone, sizeof(int));
186	plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name,
187			  sizeof(sc->sc_sensor.name));
188	units = "C";
189
190	if (plen == -1) {
191		strcpy(sysctl_name, "sensor");
192	} else {
193		for (i = 0; i < strlen(sc->sc_sensor.name); i++) {
194			sysctl_name[i] = tolower(sc->sc_sensor.name[i]);
195			if (isspace(sysctl_name[i]))
196				sysctl_name[i] = '_';
197		}
198		sysctl_name[i] = 0;
199	}
200
201	/* Make up target temperatures. These are low, for the drive bay. */
202	sc->sc_sensor.target_temp = 300 + FCU_ZERO_C_TO_K;
203	sc->sc_sensor.max_temp = 600 + FCU_ZERO_C_TO_K;
204
205	sc->sc_sensor.read =
206	    (int (*)(struct pmac_therm *sc))(ds1775_sensor_read);
207	pmac_thermal_sensor_register(&sc->sc_sensor);
208
209	sprintf(sysctl_desc,"%s (%s)", sc->sc_sensor.name, units);
210	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
211			sysctl_name,
212			CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
213			0, ds1775_sensor_sysctl, "IK", sysctl_desc);
214
215	config_intrhook_disestablish(&sc->enum_hook);
216}
217
218static int
219ds1775_sensor_read(struct ds1775_softc *sc)
220{
221	uint16_t buf[2];
222	uint16_t read;
223
224	ds1775_read_2(sc->sc_dev, sc->sc_addr, DS1775_TEMP, buf);
225
226	read = *((int16_t *)buf);
227
228	/* The default mode of the ADC is 9 bit, the resolution is 0.5 C per
229	   bit. The temperature is in tenth kelvin.
230	*/
231	return (((int16_t)(read) >> 7) * 5 + FCU_ZERO_C_TO_K);
232}
233
234static int
235ds1775_sensor_sysctl(SYSCTL_HANDLER_ARGS)
236{
237	device_t dev;
238	struct ds1775_softc *sc;
239	int error;
240	unsigned int temp;
241
242	dev = arg1;
243	sc = device_get_softc(dev);
244
245	temp = ds1775_sensor_read(sc);
246
247	error = sysctl_handle_int(oidp, &temp, 0, req);
248
249	return (error);
250}
251