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/acpica/acpi_smbat.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  * Copyright (c) 2005 Hans Petter Selasky
    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
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, 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$");
   29 
   30 #include "opt_acpi.h"
   31 #include <sys/param.h>
   32 #include <sys/kernel.h>
   33 #include <sys/module.h>
   34 #include <sys/bus.h>
   35 
   36 #include <contrib/dev/acpica/include/acpi.h>
   37 
   38 #include <dev/acpica/acpivar.h>
   39 #include <dev/acpica/acpiio.h>
   40 #include <dev/acpica/acpi_smbus.h>
   41 
   42 /* Transactions have failed after 500 ms. */
   43 #define SMBUS_TIMEOUT   50
   44 
   45 struct acpi_smbat_softc {
   46         uint8_t         sb_base_addr;
   47         device_t        ec_dev;
   48 
   49         struct acpi_bif bif;
   50         struct acpi_bst bst;
   51         struct timespec bif_lastupdated;
   52         struct timespec bst_lastupdated;
   53 };
   54 
   55 static int      acpi_smbat_probe(device_t dev);
   56 static int      acpi_smbat_attach(device_t dev);
   57 static int      acpi_smbat_shutdown(device_t dev);
   58 static int      acpi_smbat_info_expired(struct timespec *lastupdated);
   59 static void     acpi_smbat_info_updated(struct timespec *lastupdated);
   60 static int      acpi_smbat_get_bif(device_t dev, struct acpi_bif *bif);
   61 static int      acpi_smbat_get_bst(device_t dev, struct acpi_bst *bst);
   62 
   63 ACPI_SERIAL_DECL(smbat, "ACPI Smart Battery");
   64 
   65 SYSCTL_NODE(_debug_acpi, OID_AUTO, batt, CTLFLAG_RD, NULL, "Battery debugging");
   66 
   67 /* On some laptops with smart batteries, enabling battery monitoring
   68  * software causes keystrokes from atkbd to be lost.  This has also been
   69  * reported on Linux, and is apparently due to the keyboard and I2C line
   70  * for the battery being routed through the same chip.  Whether that's
   71  * accurate or not, adding extra sleeps to the status checking code
   72  * causes the problem to go away.
   73  *
   74  * If you experience that problem, try a value of 10ms and move up
   75  * from there.
   76  */
   77 static int      batt_sleep_ms;
   78 SYSCTL_INT(_debug_acpi_batt, OID_AUTO, batt_sleep_ms, CTLFLAG_RW, &batt_sleep_ms, 0,
   79     "Sleep during battery status updates to prevent keystroke loss.");
   80 
   81 static device_method_t acpi_smbat_methods[] = {
   82         /* device interface */
   83         DEVMETHOD(device_probe, acpi_smbat_probe),
   84         DEVMETHOD(device_attach, acpi_smbat_attach),
   85         DEVMETHOD(device_shutdown, acpi_smbat_shutdown),
   86 
   87         /* ACPI battery interface */
   88         DEVMETHOD(acpi_batt_get_status, acpi_smbat_get_bst),
   89         DEVMETHOD(acpi_batt_get_info, acpi_smbat_get_bif),
   90 
   91         {0, 0}
   92 };
   93 
   94 static driver_t acpi_smbat_driver = {
   95         "battery",
   96         acpi_smbat_methods,
   97         sizeof(struct acpi_smbat_softc),
   98 };
   99 
  100 static devclass_t acpi_smbat_devclass;
  101 DRIVER_MODULE(acpi_smbat, acpi, acpi_smbat_driver, acpi_smbat_devclass, 0, 0);
  102 MODULE_DEPEND(acpi_smbat, acpi, 1, 1, 1);
  103 
  104 static int
  105 acpi_smbat_probe(device_t dev)
  106 {
  107         static char *smbat_ids[] = {"ACPI0001", "ACPI0005", NULL};
  108         ACPI_STATUS status;
  109 
  110         if (acpi_disabled("smbat") ||
  111             ACPI_ID_PROBE(device_get_parent(dev), dev, smbat_ids) == NULL)
  112                 return (ENXIO);
  113         status = AcpiEvaluateObject(acpi_get_handle(dev), "_EC", NULL, NULL);
  114         if (ACPI_FAILURE(status))
  115                 return (ENXIO);
  116 
  117         device_set_desc(dev, "ACPI Smart Battery");
  118         return (0);
  119 }
  120 
  121 static int
  122 acpi_smbat_attach(device_t dev)
  123 {
  124         struct acpi_smbat_softc *sc;
  125         uint32_t base;
  126 
  127         sc = device_get_softc(dev);
  128         if (ACPI_FAILURE(acpi_GetInteger(acpi_get_handle(dev), "_EC", &base))) {
  129                 device_printf(dev, "cannot get EC base address\n");
  130                 return (ENXIO);
  131         }
  132         sc->sb_base_addr = (base >> 8) & 0xff;
  133 
  134         /* XXX Only works with one EC, but nearly all systems only have one. */
  135         sc->ec_dev = devclass_get_device(devclass_find("acpi_ec"), 0);
  136         if (sc->ec_dev == NULL) {
  137                 device_printf(dev, "cannot find EC device\n");
  138                 return (ENXIO);
  139         }
  140 
  141         timespecclear(&sc->bif_lastupdated);
  142         timespecclear(&sc->bst_lastupdated);
  143 
  144         if (acpi_battery_register(dev) != 0) {
  145                 device_printf(dev, "cannot register battery\n");
  146                 return (ENXIO);
  147         }
  148         return (0);
  149 }
  150 
  151 static int
  152 acpi_smbat_shutdown(device_t dev)
  153 {
  154 
  155         acpi_battery_remove(dev);
  156         return (0);
  157 }
  158 
  159 static int
  160 acpi_smbat_info_expired(struct timespec *lastupdated)
  161 {
  162         struct timespec curtime;
  163 
  164         ACPI_SERIAL_ASSERT(smbat);
  165 
  166         if (lastupdated == NULL)
  167                 return (TRUE);
  168         if (!timespecisset(lastupdated))
  169                 return (TRUE);
  170 
  171         getnanotime(&curtime);
  172         timespecsub(&curtime, lastupdated);
  173         return (curtime.tv_sec < 0 ||
  174             curtime.tv_sec > acpi_battery_get_info_expire());
  175 }
  176 
  177 static void
  178 acpi_smbat_info_updated(struct timespec *lastupdated)
  179 {
  180 
  181         ACPI_SERIAL_ASSERT(smbat);
  182 
  183         if (lastupdated != NULL)
  184                 getnanotime(lastupdated);
  185 }
  186 
  187 static int
  188 acpi_smbus_read_2(struct acpi_smbat_softc *sc, uint8_t addr, uint8_t cmd,
  189     uint16_t *ptr)
  190 {
  191         int error, to;
  192         UINT64 val;
  193 
  194         ACPI_SERIAL_ASSERT(smbat);
  195 
  196         if (batt_sleep_ms)
  197             AcpiOsSleep(batt_sleep_ms);
  198 
  199         val = addr;
  200         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_ADDR,
  201             val, 1);
  202         if (error)
  203                 goto out;
  204 
  205         val = cmd;
  206         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_CMD,
  207             val, 1);
  208         if (error)
  209                 goto out;
  210 
  211         val = 0x09; /* | 0x80 if PEC */
  212         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_PRTCL,
  213             val, 1);
  214         if (error)
  215                 goto out;
  216 
  217         if (batt_sleep_ms)
  218             AcpiOsSleep(batt_sleep_ms);
  219 
  220         for (to = SMBUS_TIMEOUT; to != 0; to--) {
  221                 error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_PRTCL,
  222                     &val, 1);
  223                 if (error)
  224                         goto out;
  225                 if (val == 0)
  226                         break;
  227                 AcpiOsSleep(10);
  228         }
  229         if (to == 0) {
  230                 error = ETIMEDOUT;
  231                 goto out;
  232         }
  233 
  234         error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_STS, &val, 1);
  235         if (error)
  236                 goto out;
  237         if (val & SMBUS_STS_MASK) {
  238                 printf("%s: AE_ERROR 0x%x\n",
  239                        __FUNCTION__, (int)(val & SMBUS_STS_MASK));
  240                 error = EIO;
  241                 goto out;
  242         }
  243 
  244         error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_DATA,
  245             &val, 2);
  246         if (error)
  247                 goto out;
  248 
  249         *ptr = val;
  250 
  251 out:
  252         return (error);
  253 }
  254 
  255 static int
  256 acpi_smbus_read_multi_1(struct acpi_smbat_softc *sc, uint8_t addr, uint8_t cmd,
  257     uint8_t *ptr, uint16_t len)
  258 {
  259         UINT64 val;
  260         uint8_t to;
  261         int error;
  262 
  263         ACPI_SERIAL_ASSERT(smbat);
  264 
  265         if (batt_sleep_ms)
  266             AcpiOsSleep(batt_sleep_ms);
  267 
  268         val = addr;
  269         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_ADDR,
  270             val, 1);
  271         if (error)
  272                 goto out;
  273 
  274         val = cmd;
  275         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_CMD,
  276             val, 1);
  277         if (error)
  278                 goto out;
  279 
  280         val = 0x0B /* | 0x80 if PEC */ ;
  281         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_PRTCL,
  282             val, 1);
  283         if (error)
  284                 goto out;
  285 
  286         if (batt_sleep_ms)
  287             AcpiOsSleep(batt_sleep_ms);
  288 
  289         for (to = SMBUS_TIMEOUT; to != 0; to--) {
  290                 error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_PRTCL,
  291                     &val, 1);
  292                 if (error)
  293                         goto out;
  294                 if (val == 0)
  295                         break;
  296                 AcpiOsSleep(10);
  297         }
  298         if (to == 0) {
  299                 error = ETIMEDOUT;
  300                 goto out;
  301         }
  302 
  303         error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_STS, &val, 1);
  304         if (error)
  305                 goto out;
  306         if (val & SMBUS_STS_MASK) {
  307                 printf("%s: AE_ERROR 0x%x\n",
  308                        __FUNCTION__, (int)(val & SMBUS_STS_MASK));
  309                 error = EIO;
  310                 goto out;
  311         }
  312 
  313         /* get length */
  314         error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_BCNT,
  315             &val, 1);
  316         if (error)
  317                 goto out;
  318         val = (val & 0x1f) + 1;
  319 
  320         bzero(ptr, len);
  321         if (len > val)
  322                 len = val;
  323 
  324         if (batt_sleep_ms)
  325             AcpiOsSleep(batt_sleep_ms);
  326 
  327         while (len--) {
  328                 error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_DATA
  329                     + len, &val, 1);
  330                 if (error)
  331                         goto out;
  332 
  333                 ptr[len] = val;
  334                 if (batt_sleep_ms)
  335                     AcpiOsSleep(batt_sleep_ms);
  336         }
  337 
  338 out:
  339         return (error);
  340 }
  341 
  342 static int
  343 acpi_smbat_get_bst(device_t dev, struct acpi_bst *bst)
  344 {
  345         struct acpi_smbat_softc *sc;
  346         int error;
  347         uint32_t cap_units, factor;
  348         int16_t val;
  349         uint8_t addr;
  350 
  351         ACPI_SERIAL_BEGIN(smbat);
  352 
  353         addr = SMBATT_ADDRESS;
  354         error = ENXIO;
  355         sc = device_get_softc(dev);
  356 
  357         if (!acpi_smbat_info_expired(&sc->bst_lastupdated)) {
  358                 error = 0;
  359                 goto out;
  360         }
  361 
  362         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_BATTERY_MODE, &val))
  363                 goto out;
  364         if (val & SMBATT_BM_CAPACITY_MODE) {
  365                 factor = 10;
  366                 cap_units = ACPI_BIF_UNITS_MW;
  367         } else {
  368                 factor = 1;
  369                 cap_units = ACPI_BIF_UNITS_MA;
  370         }
  371 
  372         /* get battery status */
  373         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_BATTERY_STATUS, &val))
  374                 goto out;
  375 
  376         sc->bst.state = 0;
  377         if (val & SMBATT_BS_DISCHARGING)
  378                 sc->bst.state |= ACPI_BATT_STAT_DISCHARG;
  379 
  380         if (val & SMBATT_BS_REMAINING_CAPACITY_ALARM)
  381                 sc->bst.state |= ACPI_BATT_STAT_CRITICAL;
  382 
  383         /*
  384          * If the rate is negative, it is discharging.  Otherwise,
  385          * it is charging.
  386          */
  387         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_CURRENT, &val))
  388                 goto out;
  389 
  390         if (val > 0) {
  391                 sc->bst.rate = val * factor;
  392                 sc->bst.state &= ~SMBATT_BS_DISCHARGING;
  393                 sc->bst.state |= ACPI_BATT_STAT_CHARGING;
  394         } else if (val < 0)
  395                 sc->bst.rate = (-val) * factor;
  396         else
  397                 sc->bst.rate = 0;
  398 
  399         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_REMAINING_CAPACITY, &val))
  400                 goto out;
  401         sc->bst.cap = val * factor;
  402 
  403         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_VOLTAGE, &val))
  404                 goto out;
  405         sc->bst.volt = val;
  406 
  407         acpi_smbat_info_updated(&sc->bst_lastupdated);
  408         error = 0;
  409 
  410 out:
  411         if (error == 0)
  412                 memcpy(bst, &sc->bst, sizeof(sc->bst));
  413         ACPI_SERIAL_END(smbat);
  414         return (error);
  415 }
  416 
  417 static int
  418 acpi_smbat_get_bif(device_t dev, struct acpi_bif *bif)
  419 {
  420         struct acpi_smbat_softc *sc;
  421         int error;
  422         uint32_t factor;
  423         uint16_t val;
  424         uint8_t addr;
  425 
  426         ACPI_SERIAL_BEGIN(smbat);
  427 
  428         addr = SMBATT_ADDRESS;
  429         error = ENXIO;
  430         sc = device_get_softc(dev);
  431 
  432         if (!acpi_smbat_info_expired(&sc->bif_lastupdated)) {
  433                 error = 0;
  434                 goto out;
  435         }
  436 
  437         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_BATTERY_MODE, &val))
  438                 goto out;
  439         if (val & SMBATT_BM_CAPACITY_MODE) {
  440                 factor = 10;
  441                 sc->bif.units = ACPI_BIF_UNITS_MW;
  442         } else {
  443                 factor = 1;
  444                 sc->bif.units = ACPI_BIF_UNITS_MA;
  445         }
  446 
  447         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_DESIGN_CAPACITY, &val))
  448                 goto out;
  449         sc->bif.dcap = val * factor;
  450 
  451         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_FULL_CHARGE_CAPACITY, &val))
  452                 goto out;
  453         sc->bif.lfcap = val * factor;
  454         sc->bif.btech = 1;              /* secondary (rechargeable) */
  455 
  456         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_DESIGN_VOLTAGE, &val))
  457                 goto out;
  458         sc->bif.dvol = val;
  459 
  460         sc->bif.wcap = sc->bif.dcap / 10;
  461         sc->bif.lcap = sc->bif.dcap / 10;
  462 
  463         sc->bif.gra1 = factor;  /* not supported */
  464         sc->bif.gra2 = factor;  /* not supported */
  465 
  466         if (acpi_smbus_read_multi_1(sc, addr, SMBATT_CMD_DEVICE_NAME,
  467             sc->bif.model, sizeof(sc->bif.model)))
  468                 goto out;
  469 
  470         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_SERIAL_NUMBER, &val))
  471                 goto out;
  472         snprintf(sc->bif.serial, sizeof(sc->bif.serial), "0x%04x", val);
  473 
  474         if (acpi_smbus_read_multi_1(sc, addr, SMBATT_CMD_DEVICE_CHEMISTRY,
  475             sc->bif.type, sizeof(sc->bif.type)))
  476                 goto out;
  477 
  478         if (acpi_smbus_read_multi_1(sc, addr, SMBATT_CMD_MANUFACTURER_DATA,
  479             sc->bif.oeminfo, sizeof(sc->bif.oeminfo)))
  480                 goto out;
  481 
  482         /* XXX check if device was replugged during read? */
  483 
  484         acpi_smbat_info_updated(&sc->bif_lastupdated);
  485         error = 0;
  486 
  487 out:
  488         if (error == 0)
  489                 memcpy(bif, &sc->bif, sizeof(sc->bif));
  490         ACPI_SERIAL_END(smbat);
  491         return (error);
  492 }

Cache object: ca8bf07f547acf7a4f586284901861cf


[ 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.