Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Driver for the charging sensor on the N900



                        Hi current-users@,

I am slowly understanding how to write the first batch of drivers for
the Nokia N900, starting with what's available on the main GPIO pins.

The driver attached signals whenever the device is connected to a
charger and reports it to envsys. It requires the following changes to
function properly:

in sys/arch/evbarm/conf/N900:
# GPIO devices
# Charging sensor
n900acad0       at gpio0 offset 7 mask 0x1 #intr 103

in sys/arch/evbarm/conf/files.n900:
# Charging sensor
device n900acad: sysmon_envsys
attach n900acad at gpio with n900acad
file    arch/evbarm/n900/n900_acad.c            n900acad

and then the driver code attached goes into arch/evbarm/n900/n900_acad.c.

Before committing anything, I would like to know if I have done things
properly there:
- the GPIO pin is mapped as interrupt 103 (omapgpio0, offset 7)
- the driver uses intr_establish() to hook it;
- it also configures and uses that same pin as an input.

The ugly part is when calling intr_establish(), where I had to hard-code
omapgpio0's base interrupt (96).

Is there a better way to do this?

Cheers,
-- 
khorben
/*      $NetBSD$ */

/*
 * AC adapter driver for the Nokia N900.
 *
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Pierre Pronchery (khorben%defora.org@localhost).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/gpio.h>

#include <dev/gpio/gpiovar.h>
#include <dev/sysmon/sysmonvar.h>

#include <arm/omap/omap2_gpio.h>


#define N900ACAD_PIN_INPUT      0
#define N900ACAD_NPINS  1


struct n900acad_softc
{
        device_t                sc_dev;
        void *                  sc_gpio;
        void *                  sc_intr;

        struct gpio_pinmap      sc_map;
        int                     sc_map_pins[N900ACAD_NPINS];

        struct sysmon_pswitch   sc_smpsw;
};

static int      n900acad_match(device_t, cfdata_t, void *);
static void     n900acad_attach(device_t, device_t, void *);
static int      n900acad_detach(device_t, int);

CFATTACH_DECL_NEW(n900acad, sizeof(struct n900acad_softc),
        n900acad_match, n900acad_attach, n900acad_detach, NULL);

static int      n900acad_intr(void *v);


static int
n900acad_match(device_t parent, cfdata_t cf, void * aux)
{
        struct gpio_attach_args *ga = aux;

        if (strcmp(ga->ga_dvname, cf->cf_name))
                return 0;

        if (ga->ga_offset == -1)
                return 0;

        /* check that we have enough pins */
        if (gpio_npins(ga->ga_mask) != N900ACAD_NPINS) {
                aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
                                ga->ga_mask);
                return 0;
        }

        return 1;
}

static void
n900acad_attach(device_t parent, device_t self, void *aux)
{
        struct n900acad_softc *sc = device_private(self);
        struct gpio_attach_args *ga = aux;
        int caps;

        sc->sc_dev = self;
        sc->sc_gpio = ga->ga_gpio;

        /* map pins */
        sc->sc_map.pm_map = sc->sc_map_pins;
        if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
                                &sc->sc_map)) {
                aprint_error(": can't map pins\n");
                return;
        }

        /* configure the input pin */
        caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, N900ACAD_PIN_INPUT);
        if (!(caps & GPIO_PIN_INPUT)) {
                aprint_error(": pin is unable to read input\n");
                gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
                return;
        }
        gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, N900ACAD_PIN_INPUT,
                        GPIO_PIN_INPUT);

        /* FIXME no longer hardcode this */
        sc->sc_intr = intr_establish(96 + ga->ga_offset, IST_EDGE_BOTH, IPL_VM,
                        n900acad_intr, sc);
        if (sc->sc_intr == NULL) {
                aprint_error(": could not establish interrupt\n");
                return;
        }

        aprint_normal("N900 keyboard acad\n");
        aprint_naive("N900 keyboard acad\n");

        if (!pmf_device_register(sc->sc_dev, NULL, NULL))
                aprint_error_dev(sc->sc_dev,
                    "could not establish power handler\n");

        sc->sc_smpsw.smpsw_name = device_xname(self);
        sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
        sysmon_pswitch_register(&sc->sc_smpsw);
}

static int
n900acad_detach(device_t self, int flags)
{
        struct n900acad_softc *sc = device_private(self);

        if (sc->sc_intr != NULL) {
                intr_disestablish(sc->sc_intr);
        }

        gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
        pmf_device_deregister(self);
        return 0;
}

static int
n900acad_intr(void *v)
{
        struct n900acad_softc *sc = v;
        int i;

        i = gpio_pin_read(sc->sc_gpio, &sc->sc_map, N900ACAD_PIN_INPUT);
        sysmon_pswitch_event(&sc->sc_smpsw,
                        i ? PSWITCH_EVENT_RELEASED : PSWITCH_EVENT_PRESSED);
        return 1;
}


Home | Main Index | Thread Index | Old Index