]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/mips/rmi/dev/iic/max6657.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / mips / rmi / dev / iic / max6657.c
1 /*-
2  * Copyright (c) 2003-2009 RMI Corporation
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  * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * RMI_BSD */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 /*
34  * temperature sensor chip sitting on the I2C bus.
35  */
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/bus.h>
43 #include <sys/resource.h>
44 #include <sys/rman.h>
45 #include <sys/sysctl.h>
46
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/cpufunc.h>
50 #include <machine/frame.h>
51 #include <machine/resource.h>
52
53 #include <dev/iicbus/iiconf.h>
54 #include <dev/iicbus/iicbus.h>
55
56 #include <mips/rmi/board.h>
57 #include <mips/rmi/rmi_boot_info.h>
58 #include "iicbus_if.h"
59
60 #define MAX6657_EXT_TEMP        1       
61
62 struct max6657_softc {
63         uint32_t        sc_addr;
64         device_t        sc_dev;
65         int             sc_curtemp;
66         int             sc_lastupdate;  /* in ticks */
67 };
68
69 static void max6657_update(struct max6657_softc *);
70 static int max6657_read(device_t dev, uint32_t addr, int reg) ;
71
72 static int
73 max6657_probe(device_t dev)
74 {
75         device_set_desc(dev, "MAX6657MSA Temperature Sensor");
76         return (0);
77 }
78
79 static int
80 max6657_sysctl_temp(SYSCTL_HANDLER_ARGS)
81 {
82         struct max6657_softc *sc = arg1;
83         int temp;
84
85         max6657_update(sc);
86         temp = sc->sc_curtemp ;
87         return sysctl_handle_int(oidp, &temp, 0, req);
88 }
89
90 static int
91 max6657_attach(device_t dev)
92 {
93         struct max6657_softc *sc = device_get_softc(dev);
94         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
95         struct sysctl_oid *tree = device_get_sysctl_tree(dev);
96
97         if(sc==NULL) {
98                 printf("max6657_attach device_get_softc failed\n");
99                 return (0);
100         }
101         sc->sc_dev = dev;
102         sc->sc_addr = iicbus_get_addr(dev);
103
104         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
105                 "temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
106                 max6657_sysctl_temp, "I", "operating temperature");
107
108         device_printf(dev, "Chip temperature {%d} Degree Celsius\n",
109                 max6657_read(sc->sc_dev, sc->sc_addr, MAX6657_EXT_TEMP));
110
111         return (0);
112 }
113
114 static int
115 max6657_read(device_t dev, uint32_t slave_addr, int reg) 
116 {
117         uint8_t addr = reg;
118         uint8_t data[1];
119         struct iic_msg msgs[2] = {
120              { slave_addr, IIC_M_WR, 1, &addr },
121              { slave_addr, IIC_M_RD, 1, data },
122         };
123
124         return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0];
125 }
126
127
128 static void
129 max6657_update(struct max6657_softc *sc)
130 {
131         int v;
132
133         /* NB: no point in updating any faster than the chip */
134         if (ticks - sc->sc_lastupdate > hz) {
135                 v = max6657_read(sc->sc_dev, sc->sc_addr, MAX6657_EXT_TEMP);
136                 if (v >= 0)
137                         sc->sc_curtemp = v;
138                 sc->sc_lastupdate = ticks;
139         }
140 }
141
142 static device_method_t max6657_methods[] = {
143         DEVMETHOD(device_probe,         max6657_probe),
144         DEVMETHOD(device_attach,        max6657_attach),
145
146         {0, 0},
147 };
148
149 static driver_t max6657_driver = {
150         "max6657",
151         max6657_methods,
152         sizeof(struct max6657_softc),
153 };
154 static devclass_t max6657_devclass;
155
156 DRIVER_MODULE(max6657, iicbus, max6657_driver, max6657_devclass, 0, 0);
157 MODULE_VERSION(max6657, 1);
158 MODULE_DEPEND(max6657, iicbus, 1, 1, 1);