1216390Sjchandra/*-
2216390Sjchandra * Copyright (c) 2003-2009 RMI Corporation
3216390Sjchandra * All rights reserved.
4216390Sjchandra *
5216390Sjchandra * Redistribution and use in source and binary forms, with or without
6216390Sjchandra * modification, are permitted provided that the following conditions
7216390Sjchandra * are met:
8216390Sjchandra * 1. Redistributions of source code must retain the above copyright
9216390Sjchandra *    notice, this list of conditions and the following disclaimer.
10216390Sjchandra * 2. Redistributions in binary form must reproduce the above copyright
11216390Sjchandra *    notice, this list of conditions and the following disclaimer in the
12216390Sjchandra *    documentation and/or other materials provided with the distribution.
13216390Sjchandra * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14216390Sjchandra *    may be used to endorse or promote products derived from this software
15216390Sjchandra *    without specific prior written permission.
16216390Sjchandra *
17216390Sjchandra * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18216390Sjchandra * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19216390Sjchandra * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20216390Sjchandra * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21216390Sjchandra * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22216390Sjchandra * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23216390Sjchandra * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24216390Sjchandra * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25216390Sjchandra * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26216390Sjchandra * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27216390Sjchandra * SUCH DAMAGE.
28216390Sjchandra *
29216390Sjchandra * RMI_BSD */
30216390Sjchandra
31216390Sjchandra#include <sys/cdefs.h>
32216390Sjchandra__FBSDID("$FreeBSD$");
33216390Sjchandra/*
34216390Sjchandra * temperature sensor chip sitting on the I2C bus.
35216390Sjchandra */
36216390Sjchandra#include <sys/param.h>
37216390Sjchandra#include <sys/systm.h>
38216390Sjchandra#include <sys/kernel.h>
39216390Sjchandra#include <sys/lock.h>
40216390Sjchandra#include <sys/module.h>
41216390Sjchandra#include <sys/mutex.h>
42216390Sjchandra#include <sys/bus.h>
43216390Sjchandra#include <sys/resource.h>
44216390Sjchandra#include <sys/rman.h>
45216390Sjchandra#include <sys/sysctl.h>
46216390Sjchandra
47216390Sjchandra#include <machine/bus.h>
48216390Sjchandra#include <machine/cpu.h>
49216390Sjchandra#include <machine/cpufunc.h>
50216390Sjchandra#include <machine/frame.h>
51216390Sjchandra#include <machine/resource.h>
52216390Sjchandra
53216390Sjchandra#include <dev/iicbus/iiconf.h>
54216390Sjchandra#include <dev/iicbus/iicbus.h>
55216390Sjchandra
56216390Sjchandra#include <mips/rmi/board.h>
57216390Sjchandra#include <mips/rmi/rmi_boot_info.h>
58216390Sjchandra#include "iicbus_if.h"
59216390Sjchandra
60216390Sjchandra#define	MAX6657_EXT_TEMP	1
61216390Sjchandra
62216390Sjchandrastruct max6657_softc {
63216390Sjchandra	uint32_t	sc_addr;
64216390Sjchandra	device_t	sc_dev;
65216390Sjchandra	int		sc_curtemp;
66216390Sjchandra	int		sc_lastupdate;	/* in ticks */
67216390Sjchandra};
68216390Sjchandra
69216390Sjchandrastatic void max6657_update(struct max6657_softc *);
70216390Sjchandrastatic int max6657_read(device_t dev, uint32_t addr, int reg) ;
71216390Sjchandra
72216390Sjchandrastatic int
73216390Sjchandramax6657_probe(device_t dev)
74216390Sjchandra{
75216390Sjchandra	device_set_desc(dev, "MAX6657MSA Temperature Sensor");
76216390Sjchandra	return (0);
77216390Sjchandra}
78216390Sjchandra
79216390Sjchandrastatic int
80216390Sjchandramax6657_sysctl_temp(SYSCTL_HANDLER_ARGS)
81216390Sjchandra{
82216390Sjchandra	struct max6657_softc *sc = arg1;
83216390Sjchandra	int temp;
84216390Sjchandra
85216390Sjchandra	max6657_update(sc);
86216390Sjchandra	temp = sc->sc_curtemp ;
87216390Sjchandra	return sysctl_handle_int(oidp, &temp, 0, req);
88216390Sjchandra}
89216390Sjchandra
90216390Sjchandrastatic int
91216390Sjchandramax6657_attach(device_t dev)
92216390Sjchandra{
93216390Sjchandra	struct max6657_softc *sc = device_get_softc(dev);
94216390Sjchandra	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
95216390Sjchandra	struct sysctl_oid *tree = device_get_sysctl_tree(dev);
96216390Sjchandra
97216390Sjchandra	if(sc==NULL) {
98216390Sjchandra		printf("max6657_attach device_get_softc failed\n");
99216390Sjchandra		return (0);
100216390Sjchandra	}
101216390Sjchandra	sc->sc_dev = dev;
102216390Sjchandra	sc->sc_addr = iicbus_get_addr(dev);
103216390Sjchandra
104216390Sjchandra	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
105216390Sjchandra		"temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
106216390Sjchandra		max6657_sysctl_temp, "I", "operating temperature");
107216390Sjchandra
108216390Sjchandra	device_printf(dev, "Chip temperature {%d} Degree Celsius\n",
109216390Sjchandra		max6657_read(sc->sc_dev, sc->sc_addr, MAX6657_EXT_TEMP));
110216390Sjchandra
111216390Sjchandra	return (0);
112216390Sjchandra}
113216390Sjchandra
114216390Sjchandrastatic int
115216390Sjchandramax6657_read(device_t dev, uint32_t slave_addr, int reg)
116216390Sjchandra{
117216390Sjchandra	uint8_t addr = reg;
118216390Sjchandra	uint8_t data[1];
119216390Sjchandra	struct iic_msg msgs[2] = {
120216390Sjchandra	     { slave_addr, IIC_M_WR, 1, &addr },
121216390Sjchandra	     { slave_addr, IIC_M_RD, 1, data },
122216390Sjchandra	};
123216390Sjchandra
124216390Sjchandra	return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0];
125216390Sjchandra}
126216390Sjchandra
127216390Sjchandra
128216390Sjchandrastatic void
129216390Sjchandramax6657_update(struct max6657_softc *sc)
130216390Sjchandra{
131216390Sjchandra	int v;
132216390Sjchandra
133216390Sjchandra	/* NB: no point in updating any faster than the chip */
134216390Sjchandra	if (ticks - sc->sc_lastupdate > hz) {
135216390Sjchandra		v = max6657_read(sc->sc_dev, sc->sc_addr, MAX6657_EXT_TEMP);
136216390Sjchandra		if (v >= 0)
137216390Sjchandra			sc->sc_curtemp = v;
138216390Sjchandra		sc->sc_lastupdate = ticks;
139216390Sjchandra	}
140216390Sjchandra}
141216390Sjchandra
142216390Sjchandrastatic device_method_t max6657_methods[] = {
143216390Sjchandra	DEVMETHOD(device_probe,		max6657_probe),
144216390Sjchandra	DEVMETHOD(device_attach,	max6657_attach),
145216390Sjchandra
146216390Sjchandra	{0, 0},
147216390Sjchandra};
148216390Sjchandra
149216390Sjchandrastatic driver_t max6657_driver = {
150216390Sjchandra	"max6657",
151216390Sjchandra	max6657_methods,
152216390Sjchandra	sizeof(struct max6657_softc),
153216390Sjchandra};
154216390Sjchandrastatic devclass_t max6657_devclass;
155216390Sjchandra
156216390SjchandraDRIVER_MODULE(max6657, iicbus, max6657_driver, max6657_devclass, 0, 0);
157216390SjchandraMODULE_VERSION(max6657, 1);
158216390SjchandraMODULE_DEPEND(max6657, iicbus, 1, 1, 1);
159