]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/rmi/dev/iic/max6657.c
I2C drivers for XLR/XLS processors.
[FreeBSD/FreeBSD.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         struct mtx      sc_mtx;
66         int             sc_curtemp;
67         int             sc_lastupdate;  /* in ticks */
68 };
69
70 static void max6657_update(struct max6657_softc *);
71 static int max6657_read(device_t dev, uint32_t addr, int reg) ;
72
73 static int
74 max6657_probe(device_t dev)
75 {
76         device_set_desc(dev, "MAX6657MSA Temperature Sensor");
77         return (0);
78 }
79
80 static int
81 max6657_sysctl_temp(SYSCTL_HANDLER_ARGS)
82 {
83         struct max6657_softc *sc = arg1;
84         int temp;
85
86         max6657_update(sc);
87         temp = sc->sc_curtemp ;
88         return sysctl_handle_int(oidp, &temp, 0, req);
89 }
90
91 static int
92 max6657_attach(device_t dev)
93 {
94         struct max6657_softc *sc = device_get_softc(dev);
95         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
96         struct sysctl_oid *tree = device_get_sysctl_tree(dev);
97
98         if(sc==NULL) {
99                 printf("max6657_attach device_get_softc failed\n");
100                 return (0);
101         }
102         sc->sc_dev = dev;
103         sc->sc_addr = iicbus_get_addr(dev);
104         mtx_init(&sc->sc_mtx, "max6657", "max6657", MTX_DEF);
105
106         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
107                 "temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
108                 max6657_sysctl_temp, "I", "operating temperature");
109
110         device_printf(dev, "Chip temperature {%d} Degree Celsius\n",
111                 max6657_read(sc->sc_dev, sc->sc_addr, MAX6657_EXT_TEMP));
112
113         return (0);
114 }
115
116 static int
117 max6657_read(device_t dev, uint32_t slave_addr, int reg) 
118 {
119         uint8_t addr = reg;
120         uint8_t data[1];
121         struct iic_msg msgs[2] = {
122              { slave_addr, IIC_M_WR, 1, &addr },
123              { slave_addr, IIC_M_RD, 1, data },
124         };
125
126         return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0];
127 }
128
129
130 static void
131 max6657_update(struct max6657_softc *sc)
132 {
133         int v;
134
135         mtx_lock(&sc->sc_mtx);
136         /* NB: no point in updating any faster than the chip */
137         if (ticks - sc->sc_lastupdate > hz) {
138                 v = max6657_read(sc->sc_dev, sc->sc_addr, MAX6657_EXT_TEMP);
139                 if (v >= 0)
140                         sc->sc_curtemp = v;
141                 sc->sc_lastupdate = ticks;
142         }
143         mtx_unlock(&sc->sc_mtx);
144 }
145
146 static device_method_t max6657_methods[] = {
147         DEVMETHOD(device_probe,         max6657_probe),
148         DEVMETHOD(device_attach,        max6657_attach),
149
150         {0, 0},
151 };
152
153 static driver_t max6657_driver = {
154         "max6657",
155         max6657_methods,
156         sizeof(struct max6657_softc),
157 };
158 static devclass_t max6657_devclass;
159
160 DRIVER_MODULE(max6657, iicbus, max6657_driver, max6657_devclass, 0, 0);
161 MODULE_VERSION(max6657, 1);
162 MODULE_DEPEND(max6657, iicbus, 1, 1, 1);