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