xref: /openbsd-src/sys/dev/i2c/lm78_i2c.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: lm78_i2c.c,v 1.3 2011/07/26 18:43:36 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2005 Mark Kettenis
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/device.h>
22 #include <sys/sensors.h>
23 
24 #include <machine/bus.h>
25 
26 #include <dev/i2c/i2cvar.h>
27 #include <dev/ic/lm78var.h>
28 
29 struct lm_i2c_softc {
30 	struct lm_softc sc_lmsc;
31 	i2c_tag_t	sc_tag;
32 	i2c_addr_t	sc_addr;
33 };
34 
35 int lm_i2c_match(struct device *, void *, void *);
36 void lm_i2c_attach(struct device *, struct device *, void *);
37 u_int8_t lm_i2c_readreg(struct lm_softc *, int);
38 void lm_i2c_writereg(struct lm_softc *, int, int);
39 
40 struct cfattach lm_i2c_ca = {
41 	sizeof(struct lm_i2c_softc), lm_i2c_match, lm_i2c_attach
42 };
43 
44 int
45 lm_i2c_match(struct device *parent, void *match, void *aux)
46 {
47 	struct i2c_attach_args *ia = aux;
48 
49 	if (strcmp(ia->ia_name, "as99127f") == 0 ||
50 	    strcmp(ia->ia_name, "w83627dhg") == 0 ||
51 	    strcmp(ia->ia_name, "w83627hf") == 0 ||
52 	    strcmp(ia->ia_name, "w83781d") == 0 ||
53 	    strcmp(ia->ia_name, "w83782d") == 0 ||
54 	    strcmp(ia->ia_name, "w83783s") == 0 ||
55 	    strcmp(ia->ia_name, "w83791d") == 0 ||
56 	    strcmp(ia->ia_name, "w83792d") == 0) {
57 		return (1);
58 	}
59 	/*
60 	 * XXX This chip doesn't have any real sensors, but we match
61 	 * it for now, just to knock out its satellites.
62 	 */
63 	if (strcmp(ia->ia_name, "w83791sd") == 0) {
64 		return (1);
65 	}
66 	return (0);
67 }
68 
69 void
70 lm_i2c_attach(struct device *parent, struct device *self, void *aux)
71 {
72 	struct lm_i2c_softc *sc = (struct lm_i2c_softc *)self;
73 	struct i2c_attach_args *ia = aux;
74 	u_int8_t cmd, data;
75 
76 	sc->sc_tag = ia->ia_tag;
77 	sc->sc_addr = ia->ia_addr;
78 
79 	/* Bus-independent attachment. */
80 	sc->sc_lmsc.lm_writereg = lm_i2c_writereg;
81 	sc->sc_lmsc.lm_readreg = lm_i2c_readreg;
82 	lm_attach(&sc->sc_lmsc);
83 
84 	/* Remember we attached to iic(4). */
85 	sc->sc_lmsc.sbusaddr = ia->ia_addr;
86 
87 	iic_acquire_bus(sc->sc_tag, 0);
88 
89 	cmd = 0x4a;
90 	iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
91 	    sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
92 
93 	iic_release_bus(sc->sc_tag, 0);
94 
95 	/* Make the bus scan ignore the satellites. */
96 	iic_ignore_addr(0x48 + (data & 0x7));
97 	iic_ignore_addr(0x48 + ((data >> 4) & 0x7));
98 }
99 
100 u_int8_t
101 lm_i2c_readreg(struct lm_softc *lmsc, int reg)
102 {
103 	struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
104 	u_int8_t cmd, data;
105 
106 	iic_acquire_bus(sc->sc_tag, 0);
107 
108 	cmd = reg;
109 	iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
110 	     sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
111 
112 	iic_release_bus(sc->sc_tag, 0);
113 
114 	return data;
115 }
116 
117 void
118 lm_i2c_writereg(struct lm_softc *lmsc, int reg, int val)
119 {
120 	struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
121 	u_int8_t cmd, data;
122 
123 	iic_acquire_bus(sc->sc_tag, 0);
124 
125 	cmd = reg;
126 	data = val;
127 	iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
128 	    sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
129 
130 	iic_release_bus(sc->sc_tag, 0);
131 }
132