1 /* $OpenBSD: lm78_i2c.c,v 1.1 2006/01/28 11:18:41 kettenis 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 int lm_i2c_detach(struct device *, int); 38 u_int8_t lm_i2c_readreg(struct lm_softc *, int); 39 void lm_i2c_writereg(struct lm_softc *, int, int); 40 41 struct cfattach lm_i2c_ca = { 42 sizeof(struct lm_i2c_softc), lm_i2c_match, 43 lm_i2c_attach, lm_i2c_detach 44 }; 45 46 int 47 lm_i2c_match(struct device *parent, void *match, void *aux) 48 { 49 struct i2c_attach_args *ia = aux; 50 51 if (strcmp(ia->ia_name, "as99127f") == 0 || 52 strcmp(ia->ia_name, "w83627hf") == 0 || 53 strcmp(ia->ia_name, "w83781d") == 0 || 54 strcmp(ia->ia_name, "w83782d") == 0 || 55 strcmp(ia->ia_name, "w83783s") == 0 || 56 strcmp(ia->ia_name, "w83791d") == 0 || 57 strcmp(ia->ia_name, "w83792d") == 0) { 58 return (1); 59 } 60 /* 61 * XXX This chip doesn't have any real sensors, but we match 62 * it for now, just to knock out its satellites. 63 */ 64 if (strcmp(ia->ia_name, "w83791sd") == 0) { 65 return (1); 66 } 67 return (0); 68 } 69 70 void 71 lm_i2c_attach(struct device *parent, struct device *self, void *aux) 72 { 73 struct lm_i2c_softc *sc = (struct lm_i2c_softc *)self; 74 struct i2c_attach_args *ia = aux; 75 u_int8_t cmd, data; 76 77 sc->sc_tag = ia->ia_tag; 78 sc->sc_addr = ia->ia_addr; 79 80 /* Bus-independent attachment. */ 81 sc->sc_lmsc.lm_writereg = lm_i2c_writereg; 82 sc->sc_lmsc.lm_readreg = lm_i2c_readreg; 83 lm_attach(&sc->sc_lmsc); 84 85 /* Remember we attached to iic(4). */ 86 sc->sc_lmsc.sbusaddr = ia->ia_addr; 87 88 iic_acquire_bus(sc->sc_tag, 0); 89 90 cmd = 0x4a; 91 iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, 92 sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0); 93 94 iic_release_bus(sc->sc_tag, 0); 95 96 /* Make the bus scan ignore the satellites. */ 97 iic_ignore_addr(0x48 + (data & 0x7)); 98 iic_ignore_addr(0x48 + ((data >> 4) & 0x7)); 99 } 100 101 int 102 lm_i2c_detach(struct device *self, int flags) 103 { 104 struct lm_i2c_softc *sc = (struct lm_i2c_softc *)self; 105 106 return lm_detach(&sc->sc_lmsc); 107 } 108 109 u_int8_t 110 lm_i2c_readreg(struct lm_softc *lmsc, int reg) 111 { 112 struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc; 113 u_int8_t cmd, data; 114 115 iic_acquire_bus(sc->sc_tag, 0); 116 117 cmd = reg; 118 iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, 119 sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0); 120 121 iic_release_bus(sc->sc_tag, 0); 122 123 return data; 124 } 125 126 void 127 lm_i2c_writereg(struct lm_softc *lmsc, int reg, int val) 128 { 129 struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc; 130 u_int8_t cmd, data; 131 132 iic_acquire_bus(sc->sc_tag, 0); 133 134 cmd = reg; 135 data = val; 136 iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, 137 sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0); 138 139 iic_release_bus(sc->sc_tag, 0); 140 } 141