Module Name:    src
Committed By:   khorben
Date:           Mon May  6 22:56:55 UTC 2013

Modified Files:
        src/sys/arch/evbarm/conf: N900 files.n900
Added Files:
        src/sys/arch/evbarm/n900: n900_audjck.c n900_prxmty.c

Log Message:
Added two drivers for the Nokia N900 smartphone, n900_audjck(4) and
n900_prxmty(4), to detect the insertion of audio jacks and obstruction of
the front light sensor, respectively.

Tested on my own device.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/evbarm/conf/N900
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/evbarm/conf/files.n900
cvs rdiff -u -r0 -r1.1 src/sys/arch/evbarm/n900/n900_audjck.c \
    src/sys/arch/evbarm/n900/n900_prxmty.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/evbarm/conf/N900
diff -u src/sys/arch/evbarm/conf/N900:1.12 src/sys/arch/evbarm/conf/N900:1.13
--- src/sys/arch/evbarm/conf/N900:1.12	Mon May  6 22:49:27 2013
+++ src/sys/arch/evbarm/conf/N900	Mon May  6 22:56:55 2013
@@ -1,5 +1,5 @@
 #
-#	$NetBSD: N900,v 1.12 2013/05/06 22:49:27 khorben Exp $
+#	$NetBSD: N900,v 1.13 2013/05/06 22:56:55 khorben Exp $
 #
 #	N900 -- Nokia N900 Kernel
 #
@@ -210,7 +210,7 @@ omapgpio1	at obio2 addr 0x49050000 size 
 omapgpio2	at obio2 addr 0x49052000 size 0x0400 intrbase 160 intr 31
 omapgpio3	at obio2 addr 0x49054000 size 0x0400 intrbase 192 intr 32
 omapgpio4	at obio2 addr 0x49056000 size 0x0400 intrbase 224 intr 33
-#omapgpio5	at obio2 addr 0x49058000 size 0x0400 intrbase 256 intr 34
+omapgpio5	at obio2 addr 0x49058000 size 0x0400 intrbase 256 intr 34
 
 gpio*		at omapgpio?
 
@@ -224,12 +224,18 @@ n900cambtn0	at gpio2 offset 4 mask 0x3 #
 # Keyboard slide
 n900kbdsld0	at gpio2 offset 7 mask 0x1 #intr 167
 
+# Proximity sensor
+n900prxmty0	at gpio2 offset 25 mask 0x1 #intr 185
+
 # Camera cover
 n900camcvr0	at gpio3 offset 14 mask 0x1 #intr 206
 
 # Lock button
 n900lckbtn0	at gpio3 offset 17 mask 0x1 #intr 209
 
+# Audio jack
+n900audjck0	at gpio5 offset 17 mask 0x1 #intr 273
+
 # System Control Module
 omapscm0 	at obio0 addr 0x48002000 size 0x1000
 

Index: src/sys/arch/evbarm/conf/files.n900
diff -u src/sys/arch/evbarm/conf/files.n900:1.4 src/sys/arch/evbarm/conf/files.n900:1.5
--- src/sys/arch/evbarm/conf/files.n900:1.4	Sat Apr 20 03:37:55 2013
+++ src/sys/arch/evbarm/conf/files.n900	Mon May  6 22:56:55 2013
@@ -1,4 +1,4 @@
-#	$NetBSD: files.n900,v 1.4 2013/04/20 03:37:55 khorben Exp $
+#	$NetBSD: files.n900,v 1.5 2013/05/06 22:56:55 khorben Exp $
 #
 # Nokia N900 configuration info
 #
@@ -20,6 +20,11 @@ attach com at obio with obiouart
 file	arch/arm/omap/obio_com.c		obiouart
 defparam opt_com.h				CONSADDR CONSPEED CONMODE
 
+# Audio jack
+device n900audjck: sysmon_envsys
+attach n900audjck at gpio with n900audjck
+file	arch/evbarm/n900/n900_audjck.c		n900audjck
+
 # Camera button
 device n900cambtn: sysmon_envsys
 attach n900cambtn at gpio with n900cambtn
@@ -44,3 +49,8 @@ file	arch/evbarm/n900/n900_kbdsld.c	n900
 device n900lckbtn: sysmon_envsys
 attach n900lckbtn at gpio with n900lckbtn
 file	arch/evbarm/n900/n900_lckbtn.c		n900lckbtn
+
+# Proximity sensor
+device n900prxmty: sysmon_envsys
+attach n900prxmty at gpio with n900prxmty
+file	arch/evbarm/n900/n900_prxmty.c		n900prxmty

Added files:

Index: src/sys/arch/evbarm/n900/n900_audjck.c
diff -u /dev/null src/sys/arch/evbarm/n900/n900_audjck.c:1.1
--- /dev/null	Mon May  6 22:56:55 2013
+++ src/sys/arch/evbarm/n900/n900_audjck.c	Mon May  6 22:56:54 2013
@@ -0,0 +1,197 @@
+/*	$NetBSD: n900_audjck.c,v 1.1 2013/05/06 22:56:54 khorben Exp $ */
+
+/*
+ * Audio jack 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 ([email protected]).
+ *
+ * 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: n900_audjck.c,v 1.1 2013/05/06 22:56:54 khorben Exp $");
+
+#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>
+
+
+/* The base interrupt for the corresponding GPIO device where this driver
+ * attaches. This is an ugly workaround the current limitations of gpio(4),
+ * which does not seem to allow a better way to locate the interrupt yet. */
+#define N900AUDJCK_GPIO_BASE	256
+
+#define N900AUDJCK_PIN_INPUT	0
+#define N900AUDJCK_NPINS	1
+
+
+struct n900audjck_softc
+{
+	device_t		sc_dev;
+	void *			sc_gpio;
+	void *			sc_intr;
+
+	struct gpio_pinmap	sc_map;
+	int			sc_map_pins[N900AUDJCK_NPINS];
+
+	struct sysmon_pswitch	sc_smpsw;
+	int			sc_state;
+};
+
+static int	n900audjck_match(device_t, cfdata_t, void *);
+static void	n900audjck_attach(device_t, device_t, void *);
+static int	n900audjck_detach(device_t, int);
+
+CFATTACH_DECL_NEW(n900audjck, sizeof(struct n900audjck_softc),
+	n900audjck_match, n900audjck_attach, n900audjck_detach, NULL);
+
+static void	n900audjck_refresh(struct n900audjck_softc *);
+
+static int	n900audjck_intr(void *v);
+
+
+static int
+n900audjck_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) != N900AUDJCK_NPINS) {
+		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
+				ga->ga_mask);
+		return 0;
+	}
+
+	return 1;
+}
+
+static void
+n900audjck_attach(device_t parent, device_t self, void *aux)
+{
+	struct n900audjck_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(": couldn't map the pins\n");
+		return;
+	}
+
+	/* configure the input pin */
+	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, N900AUDJCK_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, N900AUDJCK_PIN_INPUT,
+			GPIO_PIN_INPUT);
+
+	sc->sc_intr = intr_establish(N900AUDJCK_GPIO_BASE + ga->ga_offset,
+			IPL_VM, IST_EDGE_BOTH, n900audjck_intr, sc);
+	if (sc->sc_intr == NULL) {
+		aprint_error(": couldn't establish interrupt\n");
+		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
+		return;
+	}
+
+	aprint_normal(": N900 audio jack\n");
+	aprint_naive(": N900 audio jack\n");
+
+	if (!pmf_device_register(sc->sc_dev, NULL, NULL)) {
+		aprint_error_dev(sc->sc_dev,
+		    "couldn't establish power handler\n");
+	}
+
+	sc->sc_smpsw.smpsw_name = device_xname(self);
+	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_HOTKEY;
+	sc->sc_state = PSWITCH_EVENT_RELEASED;
+	sysmon_pswitch_register(&sc->sc_smpsw);
+
+	/* report an event immediately if an audio jack is inserted */
+	n900audjck_refresh(sc);
+}
+
+static int
+n900audjck_detach(device_t self, int flags)
+{
+	struct n900audjck_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
+n900audjck_intr(void *v)
+{
+	struct n900audjck_softc *sc = v;
+
+	n900audjck_refresh(sc);
+	return 1;
+}
+
+static void
+n900audjck_refresh(struct n900audjck_softc *sc)
+{
+	int i;
+	int event;
+
+	i = gpio_pin_read(sc->sc_gpio, &sc->sc_map, N900AUDJCK_PIN_INPUT);
+	event = (i == GPIO_PIN_HIGH)
+		? PSWITCH_EVENT_RELEASED : PSWITCH_EVENT_PRESSED;
+
+	/* crude way to avoid duplicate events */
+	if(event == sc->sc_state)
+		return;
+	sc->sc_state = event;
+
+	/* report the event */
+	sysmon_pswitch_event(&sc->sc_smpsw, event);
+}
Index: src/sys/arch/evbarm/n900/n900_prxmty.c
diff -u /dev/null src/sys/arch/evbarm/n900/n900_prxmty.c:1.1
--- /dev/null	Mon May  6 22:56:55 2013
+++ src/sys/arch/evbarm/n900/n900_prxmty.c	Mon May  6 22:56:54 2013
@@ -0,0 +1,189 @@
+/*	$NetBSD: n900_prxmty.c,v 1.1 2013/05/06 22:56:54 khorben Exp $ */
+
+/*
+ * Proximity sensor 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 ([email protected]).
+ *
+ * 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: n900_prxmty.c,v 1.1 2013/05/06 22:56:54 khorben Exp $");
+
+#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>
+
+
+/* The base interrupt for the corresponding GPIO device where this driver
+ * attaches. This is an ugly workaround the current limitations of gpio(4),
+ * which does not seem to allow a better way to locate the interrupt yet. */
+#define N900PRXMTY_GPIO_BASE	160
+
+#define N900PRXMTY_PIN_INPUT	0
+#define N900PRXMTY_NPINS	1
+
+
+struct n900prxmty_softc
+{
+	device_t		sc_dev;
+	void *			sc_gpio;
+	void *			sc_intr;
+
+	struct gpio_pinmap	sc_map;
+	int			sc_map_pins[N900PRXMTY_NPINS];
+
+	struct sysmon_pswitch	sc_smpsw;
+};
+
+static int	n900prxmty_match(device_t, cfdata_t, void *);
+static void	n900prxmty_attach(device_t, device_t, void *);
+static int	n900prxmty_detach(device_t, int);
+
+CFATTACH_DECL_NEW(n900prxmty, sizeof(struct n900prxmty_softc),
+	n900prxmty_match, n900prxmty_attach, n900prxmty_detach, NULL);
+
+static void	n900prxmty_refresh(struct n900prxmty_softc *);
+
+static int	n900prxmty_intr(void *v);
+
+
+static int
+n900prxmty_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) != N900PRXMTY_NPINS) {
+		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
+				ga->ga_mask);
+		return 0;
+	}
+
+	return 1;
+}
+
+static void
+n900prxmty_attach(device_t parent, device_t self, void *aux)
+{
+	struct n900prxmty_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(": couldn't map the pins\n");
+		return;
+	}
+
+	/* configure the input pin */
+	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, N900PRXMTY_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, N900PRXMTY_PIN_INPUT,
+			GPIO_PIN_INPUT);
+
+	sc->sc_intr = intr_establish(N900PRXMTY_GPIO_BASE + ga->ga_offset,
+			IPL_VM, IST_EDGE_BOTH, n900prxmty_intr, sc);
+	if (sc->sc_intr == NULL) {
+		aprint_error(": couldn't establish interrupt\n");
+		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
+		return;
+	}
+
+	aprint_normal(": N900 proximity sensor\n");
+	aprint_naive(": N900 proximity sensor\n");
+
+	if (!pmf_device_register(sc->sc_dev, NULL, NULL)) {
+		aprint_error_dev(sc->sc_dev,
+		    "couldn't establish power handler\n");
+	}
+
+	sc->sc_smpsw.smpsw_name = device_xname(self);
+	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_HOTKEY;
+	sysmon_pswitch_register(&sc->sc_smpsw);
+
+	n900prxmty_refresh(sc);
+}
+
+static int
+n900prxmty_detach(device_t self, int flags)
+{
+	struct n900prxmty_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
+n900prxmty_intr(void *v)
+{
+	struct n900prxmty_softc *sc = v;
+
+	n900prxmty_refresh(sc);
+	return 1;
+}
+
+static void
+n900prxmty_refresh(struct n900prxmty_softc *sc)
+{
+	int i;
+	int event;
+
+	i = gpio_pin_read(sc->sc_gpio, &sc->sc_map, N900PRXMTY_PIN_INPUT);
+	event = (i == GPIO_PIN_HIGH)
+		? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED;
+
+	/* report the event */
+	sysmon_pswitch_event(&sc->sc_smpsw, event);
+}

Reply via email to