The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/iicbus/adt746x.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2012 Andreas Tobler
    5  * Copyright (c) 2014 Justin Hibbits
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
   24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD$");
   32 
   33 #include <sys/param.h>
   34 #include <sys/bus.h>
   35 #include <sys/systm.h>
   36 #include <sys/module.h>
   37 #include <sys/callout.h>
   38 #include <sys/conf.h>
   39 #include <sys/cpu.h>
   40 #include <sys/ctype.h>
   41 #include <sys/kernel.h>
   42 #include <sys/reboot.h>
   43 #include <sys/rman.h>
   44 #include <sys/sysctl.h>
   45 #include <sys/limits.h>
   46 
   47 #include <machine/bus.h>
   48 #include <machine/md_var.h>
   49 
   50 #include <dev/iicbus/iicbus.h>
   51 #include <dev/iicbus/iiconf.h>
   52 
   53 #include <dev/ofw/openfirm.h>
   54 #include <dev/ofw/ofw_bus.h>
   55 #include <powerpc/powermac/powermac_thermal.h>
   56 
   57 /* ADT746X registers. */
   58 #define ADT746X_TACH1LOW          0x28
   59 #define ADT746X_TACH1HIGH         0x29
   60 #define ADT746X_TACH2LOW          0x2a
   61 #define ADT746X_TACH2HIGH         0x2b
   62 #define ADT746X_PWM1              0x30
   63 #define ADT746X_PWM2              0x31
   64 #define ADT746X_DEVICE_ID         0x3d
   65 #define ADT746X_COMPANY_ID        0x3e
   66 #define ADT746X_REV_ID            0x3f
   67 #define ADT746X_CONFIG            0x40
   68 #define ADT746X_PWM1_CONF         0x5c
   69 #define ADT746X_PWM2_CONF         0x5d
   70 #define ADT746X_MANUAL_MASK       0xe0
   71 
   72 #define ADT7460_DEV_ID            0x27
   73 #define ADT7467_DEV_ID            0x68
   74 
   75 struct adt746x_fan {
   76         struct pmac_fan fan;
   77         device_t        dev;
   78         int             id;
   79         int             setpoint;
   80         int             pwm_reg;
   81         int             conf_reg;
   82 };
   83 
   84 struct adt746x_sensor {
   85         struct pmac_therm therm;
   86         device_t          dev;
   87         int               id;
   88         cell_t            reg;
   89         enum {
   90                 ADT746X_SENSOR_TEMP,
   91                 ADT746X_SENSOR_VOLT,
   92                 ADT746X_SENSOR_SPEED
   93         } type;
   94 };
   95 
   96 struct adt746x_softc {
   97         device_t                sc_dev;
   98         struct intr_config_hook enum_hook;
   99         uint32_t                sc_addr;
  100         /* The 7467 supports up to 4 fans, 2 voltage and 3 temperature sensors. */
  101         struct adt746x_fan      sc_fans[4];
  102         int                     sc_nfans;
  103         struct adt746x_sensor   sc_sensors[9];
  104         int                     sc_nsensors;
  105         int                     device_id;
  106     
  107 };
  108 
  109 
  110 /* Regular bus attachment functions */
  111 
  112 static int  adt746x_probe(device_t);
  113 static int  adt746x_attach(device_t);
  114 
  115 
  116 /* Utility functions */
  117 static void adt746x_attach_fans(device_t dev);
  118 static void adt746x_attach_sensors(device_t dev);
  119 static int  adt746x_fill_fan_prop(device_t dev);
  120 static int  adt746x_fill_sensor_prop(device_t dev);
  121 
  122 static int  adt746x_fan_set_pwm(struct adt746x_fan *fan, int pwm);
  123 static int  adt746x_fan_get_pwm(struct adt746x_fan *fan);
  124 static int  adt746x_sensor_read(struct adt746x_sensor *sens);
  125 static void adt746x_start(void *xdev);
  126 
  127 /* i2c read/write functions. */
  128 static int  adt746x_write(device_t dev, uint32_t addr, uint8_t reg,
  129                           uint8_t *buf);
  130 static int  adt746x_read(device_t dev, uint32_t addr, uint8_t reg,
  131                          uint8_t *data);
  132 
  133 static device_method_t  adt746x_methods[] = {
  134         /* Device interface */
  135         DEVMETHOD(device_probe,  adt746x_probe),
  136         DEVMETHOD(device_attach, adt746x_attach),
  137         { 0, 0 },
  138 };
  139 
  140 static driver_t adt746x_driver = {
  141         "adt746x",
  142         adt746x_methods,
  143         sizeof(struct adt746x_softc)
  144 };
  145 
  146 DRIVER_MODULE(adt746x, iicbus, adt746x_driver, 0, 0);
  147 static MALLOC_DEFINE(M_ADT746X, "adt746x", "ADT Sensor Information");
  148 
  149 
  150 /* i2c read/write functions. */
  151 
  152 static int
  153 adt746x_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff)
  154 {
  155         uint8_t buf[4];
  156         int try = 0;
  157 
  158         struct iic_msg msg[] = {
  159                 {addr, IIC_M_WR, 2, buf }
  160         };
  161 
  162         /* Prepare the write msg. */
  163         buf[0] = reg;
  164         memcpy(buf + 1, buff, 1);
  165 
  166         for (;;)
  167         {
  168                 if (iicbus_transfer(dev, msg, 1) == 0)
  169                         return (0);
  170                 if (++try > 5) {
  171                         device_printf(dev, "iicbus write failed\n");
  172                         return (-1);
  173                 }
  174                 pause("adt746x_write", hz);
  175         }
  176         return (0);
  177 }
  178 
  179 static int
  180 adt746x_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
  181 {
  182         uint8_t buf[4];
  183         int err, try = 0;
  184 
  185         struct iic_msg msg[2] = {
  186                 {addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg},
  187                 {addr, IIC_M_RD, 1, buf},
  188         };
  189 
  190         for (;;)
  191         {
  192                 err = iicbus_transfer(dev, msg, 2);
  193                 if (err != 0)
  194                         goto retry;
  195 
  196                 *data = *((uint8_t*)buf);
  197                 return (0);
  198         retry:
  199                 if (++try > 5) {
  200                         device_printf(dev, "iicbus read failed\n");
  201                         return (-1);
  202                 }
  203                 pause("adt746x_read", hz);
  204         }
  205 }
  206 
  207 static int
  208 adt746x_probe(device_t dev)
  209 {
  210         const char  *name, *compatible;
  211         struct adt746x_softc *sc;
  212 
  213         name = ofw_bus_get_name(dev);
  214         compatible = ofw_bus_get_compat(dev);
  215 
  216         if (!name)
  217                 return (ENXIO);
  218 
  219         if (strcmp(name, "fan") != 0 ||
  220             (strcmp(compatible, "adt7460") != 0 &&
  221              strcmp(compatible, "adt7467") != 0))
  222                 return (ENXIO);
  223 
  224         sc = device_get_softc(dev);
  225         sc->sc_dev = dev;
  226         sc->sc_addr = iicbus_get_addr(dev);
  227 
  228         device_set_desc(dev, "Apple Thermostat Unit ADT746X");
  229 
  230         return (0);
  231 }
  232 
  233 static int
  234 adt746x_attach(device_t dev)
  235 {
  236         struct adt746x_softc *sc;
  237 
  238         sc = device_get_softc(dev);
  239 
  240         sc->enum_hook.ich_func = adt746x_start;
  241         sc->enum_hook.ich_arg = dev;
  242 
  243         /* We have to wait until interrupts are enabled. I2C read and write
  244          * only works if the interrupts are available.
  245          * The unin/i2c is controlled by the htpic on unin. But this is not
  246          * the master. The openpic on mac-io is controlling the htpic.
  247          * This one gets attached after the mac-io probing and then the
  248          * interrupts will be available.
  249          */
  250 
  251         if (config_intrhook_establish(&sc->enum_hook) != 0)
  252                 return (ENOMEM);
  253 
  254         return (0);
  255 }
  256 
  257 static void
  258 adt746x_start(void *xdev)
  259 {
  260         uint8_t did, cid, rev, conf;
  261 
  262         struct adt746x_softc *sc;
  263 
  264         device_t dev = (device_t)xdev;
  265 
  266         sc = device_get_softc(dev);
  267 
  268         adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_DEVICE_ID, &did);
  269         adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_COMPANY_ID, &cid);
  270         adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_REV_ID, &rev);
  271         adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_CONFIG, &conf);
  272 
  273         device_printf(dev, "Dev ID %#x, Company ID %#x, Rev ID %#x CNF: %#x\n",
  274                       did, cid, rev, conf);
  275 
  276         /* We can get the device id either from 'of' properties or from the chip
  277            itself. This method makes sure we can read the chip, otherwise
  278            we return.  */
  279 
  280         sc->device_id = did;
  281 
  282         conf = 1;
  283         /* Start the ADT7460.  */
  284         if (sc->device_id == ADT7460_DEV_ID)
  285                 adt746x_write(sc->sc_dev, sc->sc_addr, ADT746X_CONFIG, &conf);
  286 
  287         /* Detect and attach child devices.  */
  288         adt746x_attach_fans(dev);
  289         adt746x_attach_sensors(dev);
  290         config_intrhook_disestablish(&sc->enum_hook);
  291 }
  292 
  293 /*
  294  * Sensor and fan management
  295  */
  296 static int
  297 adt746x_fan_set_pwm(struct adt746x_fan *fan, int pwm)
  298 {
  299         uint8_t reg = 0, manual, mode = 0;
  300         struct adt746x_softc *sc;
  301         uint8_t buf;
  302 
  303         sc = device_get_softc(fan->dev);
  304 
  305         /* Clamp to allowed range */
  306         pwm = max(fan->fan.min_rpm, pwm);
  307         pwm = min(fan->fan.max_rpm, pwm);
  308 
  309         reg = fan->pwm_reg;
  310         mode = fan->conf_reg;
  311 
  312         /* From the 7460 datasheet:
  313            PWM dutycycle can be programmed from 0% (0x00) to 100% (0xFF)
  314            in steps of 0.39% (256 steps).
  315          */
  316         buf = (pwm * 100 / 39) - (pwm ? 1 : 0);
  317         fan->setpoint = buf;
  318 
  319         /* Manual mode.  */
  320         adt746x_read(sc->sc_dev, sc->sc_addr, mode, &manual);
  321         manual |= ADT746X_MANUAL_MASK;
  322         adt746x_write(sc->sc_dev, sc->sc_addr, mode, &manual);
  323 
  324         /* Write speed.  */
  325         adt746x_write(sc->sc_dev, sc->sc_addr, reg, &buf);
  326 
  327         return (0);
  328 }
  329 
  330 static int
  331 adt746x_fan_get_pwm(struct adt746x_fan *fan)
  332 {
  333         uint8_t buf, reg;
  334         uint16_t pwm;
  335         struct adt746x_softc *sc;
  336 
  337         sc = device_get_softc(fan->dev);
  338 
  339         reg = fan->pwm_reg;
  340 
  341         adt746x_read(sc->sc_dev, sc->sc_addr, reg, &buf);
  342 
  343         pwm = (buf * 39 / 100) + (buf ? 1 : 0);
  344         return (pwm);
  345 }
  346 
  347 static int
  348 adt746x_fill_fan_prop(device_t dev)
  349 {
  350         phandle_t child;
  351         struct adt746x_softc *sc;
  352         u_int *id;
  353         char *location;
  354         int i, id_len, len = 0, location_len, prev_len = 0;
  355 
  356         sc = device_get_softc(dev);
  357 
  358         child = ofw_bus_get_node(dev);
  359 
  360         /* Fill the fan location property. */
  361         location_len = OF_getprop_alloc(child, "hwctrl-location", (void **)&location);
  362         id_len = OF_getprop_alloc_multi(child, "hwctrl-id", sizeof(cell_t), (void **)&id);
  363         if (location_len == -1 || id_len == -1) {
  364                 OF_prop_free(location);
  365                 OF_prop_free(id);
  366                 return 0;
  367         }
  368 
  369         /* Fill in all the properties for each fan. */
  370         for (i = 0; i < id_len; i++) {
  371                 strlcpy(sc->sc_fans[i].fan.name, location + len, 32);
  372                 prev_len = strlen(location + len) + 1;
  373                 len += prev_len;
  374                 sc->sc_fans[i].id = id[i];
  375                 if (id[i] == 6) {
  376                         sc->sc_fans[i].pwm_reg = ADT746X_PWM1;
  377                         sc->sc_fans[i].conf_reg = ADT746X_PWM1_CONF;
  378                 } else if (id[i] == 7) {
  379                         sc->sc_fans[i].pwm_reg = ADT746X_PWM2;
  380                         sc->sc_fans[i].conf_reg = ADT746X_PWM2_CONF;
  381                 } else {
  382                         sc->sc_fans[i].pwm_reg = ADT746X_PWM1 + i;
  383                         sc->sc_fans[i].conf_reg = ADT746X_PWM1_CONF + i;
  384                 }
  385                 sc->sc_fans[i].dev = sc->sc_dev;
  386                 sc->sc_fans[i].fan.min_rpm = 5; /* Percent */
  387                 sc->sc_fans[i].fan.max_rpm = 100;
  388                 sc->sc_fans[i].fan.read = NULL;
  389                 sc->sc_fans[i].fan.set =
  390                         (int (*)(struct pmac_fan *, int))(adt746x_fan_set_pwm);
  391                 sc->sc_fans[i].fan.default_rpm = sc->sc_fans[i].fan.max_rpm;
  392         }
  393         OF_prop_free(location);
  394         OF_prop_free(id);
  395 
  396         return (i);
  397 }
  398 
  399 static int
  400 adt746x_fill_sensor_prop(device_t dev)
  401 {
  402         phandle_t child, node;
  403         struct adt746x_softc *sc;
  404         char sens_type[32];
  405         int i = 0, reg, sensid;
  406 
  407         sc = device_get_softc(dev);
  408 
  409         child = ofw_bus_get_node(dev);
  410 
  411         /* Fill in the sensor properties for each child. */
  412         for (node = OF_child(child); node != 0; node = OF_peer(node)) {
  413                 if (OF_getprop(node, "sensor-id", &sensid, sizeof(sensid)) == -1)
  414                     continue;
  415                 OF_getprop(node, "location", sc->sc_sensors[i].therm.name, 32);
  416                 OF_getprop(node, "device_type", sens_type, sizeof(sens_type));
  417                 if (strcmp(sens_type, "temperature") == 0)
  418                         sc->sc_sensors[i].type = ADT746X_SENSOR_TEMP;
  419                 else if (strcmp(sens_type, "voltage") == 0)
  420                         sc->sc_sensors[i].type = ADT746X_SENSOR_VOLT;
  421                 else
  422                         sc->sc_sensors[i].type = ADT746X_SENSOR_SPEED;
  423                 OF_getprop(node, "reg", &reg, sizeof(reg));
  424                 OF_getprop(node, "sensor-id", &sensid,
  425                         sizeof(sensid));
  426                 /* This is the i2c register of the sensor.  */
  427                 sc->sc_sensors[i].reg = reg;
  428                 sc->sc_sensors[i].id = sensid;
  429                 OF_getprop(node, "zone", &sc->sc_sensors[i].therm.zone,
  430                         sizeof(sc->sc_sensors[i].therm.zone));
  431                 sc->sc_sensors[i].dev = dev;
  432                 sc->sc_sensors[i].therm.read =
  433                     (int (*)(struct pmac_therm *))adt746x_sensor_read;
  434                 if (sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP) {
  435                     /* Make up some ranges */
  436                     sc->sc_sensors[i].therm.target_temp = 500 + ZERO_C_TO_K;
  437                     sc->sc_sensors[i].therm.max_temp = 800 + ZERO_C_TO_K;
  438 
  439                     pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
  440                 }
  441                 i++;
  442         }
  443 
  444         return (i);
  445 }
  446 
  447 static int
  448 adt746x_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
  449 {
  450         device_t adt;
  451         struct adt746x_softc *sc;
  452         struct adt746x_fan *fan;
  453         int pwm = 0, error;
  454 
  455         adt = arg1;
  456         sc = device_get_softc(adt);
  457         fan = &sc->sc_fans[arg2];
  458         pwm = adt746x_fan_get_pwm(fan);
  459         error = sysctl_handle_int(oidp, &pwm, 0, req);
  460 
  461         if (error || !req->newptr)
  462                 return (error);
  463 
  464         return (adt746x_fan_set_pwm(fan, pwm));
  465 }
  466 
  467 static void
  468 adt746x_attach_fans(device_t dev)
  469 {
  470         struct adt746x_softc *sc;
  471         struct sysctl_oid *oid, *fanroot_oid;
  472         struct sysctl_ctx_list *ctx;
  473         char sysctl_name[32];
  474         int i, j;
  475 
  476         sc = device_get_softc(dev);
  477 
  478         sc->sc_nfans = 0;
  479 
  480         /* Count the actual number of fans. */
  481         sc->sc_nfans = adt746x_fill_fan_prop(dev);
  482 
  483         device_printf(dev, "%d fans detected!\n", sc->sc_nfans);
  484 
  485         if (sc->sc_nfans == 0) {
  486                 device_printf(dev, "WARNING: No fans detected!\n");
  487                 return;
  488         }
  489 
  490         ctx = device_get_sysctl_ctx(dev);
  491         fanroot_oid = SYSCTL_ADD_NODE(ctx,
  492             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
  493             CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "ADT Fan Information");
  494 
  495         /* Now we can fill the properties into the allocated struct. */
  496         sc->sc_nfans = adt746x_fill_fan_prop(dev);
  497 
  498         /* Register fans with pmac_thermal */
  499         for (i = 0; i < sc->sc_nfans; i++)
  500                 pmac_thermal_fan_register(&sc->sc_fans[i].fan);
  501 
  502         /* Add sysctls for the fans. */
  503         for (i = 0; i < sc->sc_nfans; i++) {
  504                 for (j = 0; j < strlen(sc->sc_fans[i].fan.name); j++) {
  505                         sysctl_name[j] = tolower(sc->sc_fans[i].fan.name[j]);
  506                         if (isspace(sysctl_name[j]))
  507                                 sysctl_name[j] = '_';
  508                 }
  509                 sysctl_name[j] = 0;
  510 
  511                 sc->sc_fans[i].setpoint =
  512                         adt746x_fan_get_pwm(&sc->sc_fans[i]);
  513 
  514                 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
  515                     OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
  516                     "Fan Information");
  517 
  518                 /* I use i to pass the fan id. */
  519                 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  520                     "pwm", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, dev,
  521                     i, adt746x_fanrpm_sysctl, "I", "Fan PWM in %");
  522         }
  523 
  524         /* Dump fan location & type. */
  525         if (bootverbose) {
  526                 for (i = 0; i < sc->sc_nfans; i++) {
  527                         device_printf(dev, "Fan location: %s",
  528                                       sc->sc_fans[i].fan.name);
  529                         device_printf(dev, " id: %d RPM: %d\n",
  530                                       sc->sc_fans[i].id,
  531                                       sc->sc_fans[i].setpoint);
  532                 }
  533         }
  534 }
  535 
  536 static int
  537 adt746x_sensor_read(struct adt746x_sensor *sens)
  538 {
  539         struct adt746x_softc *sc;
  540         int tmp = 0;
  541         uint16_t val;
  542         uint8_t data[1], data1[1];
  543         int8_t temp;
  544 
  545         sc = device_get_softc(sens->dev);
  546         if (sens->type != ADT746X_SENSOR_SPEED) {
  547                 if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg,
  548                                  &temp) < 0)
  549                         return (-1);
  550                 if (sens->type == ADT746X_SENSOR_TEMP)
  551                         tmp = 10 * temp + ZERO_C_TO_K;
  552                 else
  553                         tmp = temp;
  554         } else {
  555                 if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg,
  556                                  data) < 0)
  557                         return (-1);
  558                 if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg + 1,
  559                                  data1) < 0)
  560                         return (-1);
  561                 val = data[0] + (data1[0] << 8);
  562                 /* A value of 0xffff means the fan is stopped.  */
  563                 if (val == 0 || val == 0xffff)
  564                         tmp = 0;
  565                 else
  566                         tmp = (90000 * 60) / val;
  567         }
  568         return (tmp);
  569 }
  570 
  571 static int
  572 adt746x_sensor_sysctl(SYSCTL_HANDLER_ARGS)
  573 {
  574         device_t dev;
  575         struct adt746x_softc *sc;
  576         struct adt746x_sensor *sens;
  577         int value, error;
  578 
  579         dev = arg1;
  580         sc = device_get_softc(dev);
  581         sens = &sc->sc_sensors[arg2];
  582 
  583         value = sens->therm.read(&sens->therm);
  584         if (value < 0)
  585                 return (ENXIO);
  586 
  587         error = sysctl_handle_int(oidp, &value, 0, req);
  588 
  589         return (error);
  590 }
  591 
  592 static void
  593 adt746x_attach_sensors(device_t dev)
  594 {
  595         struct adt746x_softc *sc;
  596         struct sysctl_oid *oid, *sensroot_oid;
  597         struct sysctl_ctx_list *ctx;
  598         char sysctl_name[40];
  599         const char *unit;
  600         const char *desc;
  601         int i, j;
  602 
  603 
  604         sc = device_get_softc(dev);
  605         sc->sc_nsensors = 0;
  606 
  607         /* Count the actual number of sensors. */
  608         sc->sc_nsensors = adt746x_fill_sensor_prop(dev);
  609         device_printf(dev, "%d sensors detected!\n", sc->sc_nsensors);
  610         if (sc->sc_nsensors == 0) {
  611                 device_printf(dev, "WARNING: No sensors detected!\n");
  612                 return;
  613         }
  614 
  615         ctx = device_get_sysctl_ctx(dev);
  616         sensroot_oid = SYSCTL_ADD_NODE(ctx,
  617             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
  618             CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "ADT Sensor Information");
  619 
  620         /* Add the sysctl for the sensors. */
  621         for (i = 0; i < sc->sc_nsensors; i++) {
  622                 for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
  623                         sysctl_name[j] = tolower(sc->sc_sensors[i].therm.name[j]);
  624                         if (isspace(sysctl_name[j]))
  625                                 sysctl_name[j] = '_';
  626                 }
  627                 sysctl_name[j] = 0;
  628                 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
  629                     OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 
  630                     "Sensor Information");
  631                 if (sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP) {
  632                         unit = "temp";
  633                         desc = "sensor unit (C)";
  634                 } else if (sc->sc_sensors[i].type == ADT746X_SENSOR_VOLT) {
  635                         unit = "volt";
  636                         desc = "sensor unit (mV)";
  637                 } else {
  638                         unit = "rpm";
  639                         desc = "sensor unit (RPM)";
  640                 }
  641                 /* I use i to pass the sensor id. */
  642                 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  643                     unit, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, dev, i,
  644                     adt746x_sensor_sysctl,
  645                     sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP ?
  646                     "IK" : "I", desc);
  647         }
  648 
  649         /* Dump sensor location & type. */
  650         if (bootverbose) {
  651                 for (i = 0; i < sc->sc_nsensors; i++) {
  652                         device_printf(dev, "Sensor location: %s",
  653                                       sc->sc_sensors[i].therm.name);
  654                         device_printf(dev, " type: %d id: %d reg: 0x%x\n",
  655                                       sc->sc_sensors[i].type,
  656                                       sc->sc_sensors[i].id,
  657                                       sc->sc_sensors[i].reg);
  658                 }
  659         }
  660 }

Cache object: 451f08c4802d31cb2f6fff4ad5b50347


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.