ad7417.c (227293) | ad7417.c (239397) |
---|---|
1/*- 2 * Copyright (c) 2010 Andreas Tobler 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 --- 11 unchanged lines hidden (view full) --- 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2010 Andreas Tobler 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 --- 11 unchanged lines hidden (view full) --- 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> |
28__FBSDID("$FreeBSD: head/sys/dev/iicbus/ad7417.c 227293 2011-11-07 06:44:47Z ed $"); | 28__FBSDID("$FreeBSD: head/sys/dev/iicbus/ad7417.c 239397 2012-08-19 19:31:36Z andreast $"); |
29 30#include <sys/param.h> 31#include <sys/bus.h> 32#include <sys/systm.h> 33#include <sys/module.h> 34#include <sys/callout.h> 35#include <sys/conf.h> 36#include <sys/cpu.h> --- 62 unchanged lines hidden (view full) --- 99static int ad7417_adc_read(struct ad7417_sensor *sens); 100static int ad7417_sensor_read(struct ad7417_sensor *sens); 101 102struct ad7417_softc { 103 device_t sc_dev; 104 uint32_t sc_addr; 105 struct ad7417_sensor *sc_sensors; 106 int sc_nsensors; | 29 30#include <sys/param.h> 31#include <sys/bus.h> 32#include <sys/systm.h> 33#include <sys/module.h> 34#include <sys/callout.h> 35#include <sys/conf.h> 36#include <sys/cpu.h> --- 62 unchanged lines hidden (view full) --- 99static int ad7417_adc_read(struct ad7417_sensor *sens); 100static int ad7417_sensor_read(struct ad7417_sensor *sens); 101 102struct ad7417_softc { 103 device_t sc_dev; 104 uint32_t sc_addr; 105 struct ad7417_sensor *sc_sensors; 106 int sc_nsensors; |
107 int init_done; |
|
107}; 108static device_method_t ad7417_methods[] = { 109 /* Device interface */ 110 DEVMETHOD(device_probe, ad7417_probe), 111 DEVMETHOD(device_attach, ad7417_attach), 112 { 0, 0 }, 113}; 114 --- 127 unchanged lines hidden (view full) --- 242 } 243} 244 245static int 246ad7417_init_adc(device_t dev, uint32_t addr) 247{ 248 uint8_t buf; 249 int err; | 108}; 109static device_method_t ad7417_methods[] = { 110 /* Device interface */ 111 DEVMETHOD(device_probe, ad7417_probe), 112 DEVMETHOD(device_attach, ad7417_attach), 113 { 0, 0 }, 114}; 115 --- 127 unchanged lines hidden (view full) --- 243 } 244} 245 246static int 247ad7417_init_adc(device_t dev, uint32_t addr) 248{ 249 uint8_t buf; 250 int err; |
251 struct ad7417_softc *sc; |
|
250 | 252 |
253 sc = device_get_softc(dev); 254 |
|
251 adc741x_config = 0; 252 /* Clear Config2 */ 253 buf = 0; 254 255 err = ad7417_write(dev, addr, AD7417_CONFIG2, &buf, 1); 256 257 /* Read & cache Config1 */ 258 buf = 0; 259 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1); 260 err = ad7417_read_1(dev, addr, AD7417_CONFIG, &buf); 261 adc741x_config = (uint8_t)buf; 262 263 /* Disable shutdown mode */ 264 adc741x_config &= 0xfe; 265 buf = adc741x_config; 266 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1); 267 if (err < 0) 268 return (-1); 269 | 255 adc741x_config = 0; 256 /* Clear Config2 */ 257 buf = 0; 258 259 err = ad7417_write(dev, addr, AD7417_CONFIG2, &buf, 1); 260 261 /* Read & cache Config1 */ 262 buf = 0; 263 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1); 264 err = ad7417_read_1(dev, addr, AD7417_CONFIG, &buf); 265 adc741x_config = (uint8_t)buf; 266 267 /* Disable shutdown mode */ 268 adc741x_config &= 0xfe; 269 buf = adc741x_config; 270 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1); 271 if (err < 0) 272 return (-1); 273 |
274 sc->init_done = 1; 275 |
|
270 return (0); 271 272} 273static int 274ad7417_probe(device_t dev) 275{ 276 const char *name, *compatible; 277 struct ad7417_softc *sc; --- 147 unchanged lines hidden (view full) --- 425 426 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid), 427 OID_AUTO, 428 sysctl_name, CTLFLAG_RD, 0, 429 "Sensor Information"); 430 431 if (sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR) { 432 unit = "temp"; | 276 return (0); 277 278} 279static int 280ad7417_probe(device_t dev) 281{ 282 const char *name, *compatible; 283 struct ad7417_softc *sc; --- 147 unchanged lines hidden (view full) --- 431 432 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid), 433 OID_AUTO, 434 sysctl_name, CTLFLAG_RD, 0, 435 "Sensor Information"); 436 437 if (sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR) { 438 unit = "temp"; |
433 desc = "Sensor temp in C"; | 439 desc = "sensor unit (C)"; |
434 } else { 435 unit = "volt"; | 440 } else { 441 unit = "volt"; |
436 desc = "Sensor Volt in V"; | 442 desc = "sensor unit (mV)"; |
437 } 438 /* I use i to pass the sensor id. */ 439 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 440 unit, CTLTYPE_INT | CTLFLAG_RD, dev, 441 i, ad7417_sensor_sysctl, 442 sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR ? 443 "IK" : "I", desc); 444 } --- 134 unchanged lines hidden (view full) --- 579static int 580ad7417_sensor_read(struct ad7417_sensor *sens) 581{ 582 struct ad7417_softc *sc; 583 int temp; 584 585 sc = device_get_softc(sens->dev); 586 | 443 } 444 /* I use i to pass the sensor id. */ 445 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 446 unit, CTLTYPE_INT | CTLFLAG_RD, dev, 447 i, ad7417_sensor_sysctl, 448 sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR ? 449 "IK" : "I", desc); 450 } --- 134 unchanged lines hidden (view full) --- 585static int 586ad7417_sensor_read(struct ad7417_sensor *sens) 587{ 588 struct ad7417_softc *sc; 589 int temp; 590 591 sc = device_get_softc(sens->dev); 592 |
587 /* Init the ADC. */ 588 if (ad7417_init_adc(sc->sc_dev, sc->sc_addr) < 0) 589 return (-1); | 593 /* Init the ADC if not already done.*/ 594 if (!sc->init_done) 595 if (ad7417_init_adc(sc->sc_dev, sc->sc_addr) < 0) 596 return (-1); |
590 591 if (sens->type == ADC7417_TEMP_SENSOR) { 592 if (ad7417_get_temp(sc->sc_dev, sc->sc_addr, &temp) < 0) 593 return (-1); 594 temp += ZERO_C_TO_K; 595 } else { 596 temp = ad7417_adc_read(sens); 597 } --- 24 unchanged lines hidden --- | 597 598 if (sens->type == ADC7417_TEMP_SENSOR) { 599 if (ad7417_get_temp(sc->sc_dev, sc->sc_addr, &temp) < 0) 600 return (-1); 601 temp += ZERO_C_TO_K; 602 } else { 603 temp = ad7417_adc_read(sens); 604 } --- 24 unchanged lines hidden --- |