svn commit: r276314 - head/sys/arm/broadcom/bcm2835

Luiz Otavio O Souza loos at FreeBSD.org
Sat Dec 27 18:54:41 UTC 2014


Author: loos
Date: Sat Dec 27 18:54:39 2014
New Revision: 276314
URL: https://svnweb.freebsd.org/changeset/base/276314

Log:
  Convert the BSC (i2c) driver to use the new iicbus_get_frequency().
  
  Tested on:	Raspberry pi

Modified:
  head/sys/arm/broadcom/bcm2835/bcm2835_bsc.c
  head/sys/arm/broadcom/bcm2835/bcm2835_bscvar.h

Modified: head/sys/arm/broadcom/bcm2835/bcm2835_bsc.c
==============================================================================
--- head/sys/arm/broadcom/bcm2835/bcm2835_bsc.c	Sat Dec 27 18:24:15 2014	(r276313)
+++ head/sys/arm/broadcom/bcm2835/bcm2835_bsc.c	Sat Dec 27 18:54:39 2014	(r276314)
@@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$");
 #include "iicbus_if.h"
 
 static void bcm_bsc_intr(void *);
+static int bcm_bsc_detach(device_t);
 
 static void
 bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask,
@@ -72,10 +73,8 @@ bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)
 {
 	struct bcm_bsc_softc *sc;
 	uint32_t clk;
-	int error;
 
 	sc = (struct bcm_bsc_softc *)arg1;
-
 	BCM_BSC_LOCK(sc);
 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
 	BCM_BSC_UNLOCK(sc);
@@ -83,20 +82,8 @@ bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)
 	if (clk == 0)
 		clk = 32768;
 	clk = BCM_BSC_CORE_CLK / clk;
-	error = sysctl_handle_int(oidp, &clk, sizeof(clk), req);
-	if (error != 0 || req->newptr == NULL)
-		return (error);
 
-	clk = BCM_BSC_CORE_CLK / clk;
-	if (clk % 2)
-		clk--;
-	if (clk > 0xffff)
-		clk = 0xffff;
-	BCM_BSC_LOCK(sc);
-	BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, clk);
-	BCM_BSC_UNLOCK(sc);
-
-	return (0);
+	return (sysctl_handle_int(oidp, &clk, 0, req));
 }
 
 static int
@@ -192,7 +179,7 @@ bcm_bsc_sysctl_init(struct bcm_bsc_softc
 	ctx = device_get_sysctl_ctx(sc->sc_dev);
 	tree_node = device_get_sysctl_tree(sc->sc_dev);
 	tree = SYSCTL_CHILDREN(tree_node);
-	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock",
+	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "frequency",
 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
 	    bcm_bsc_clock_proc, "IU", "I2C BUS clock frequency");
 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock_stretch",
@@ -307,7 +294,11 @@ bcm_bsc_attach(device_t dev)
 	bcm_bsc_reset(sc);
 	BCM_BSC_UNLOCK(sc);
 
-	device_add_child(dev, "iicbus", -1);
+	sc->sc_iicbus = device_add_child(dev, "iicbus", -1);
+	if (sc->sc_iicbus == NULL) {
+		bcm_bsc_detach(dev);
+		return (ENXIO);
+	}
 
 	return (bus_generic_attach(dev));
 }
@@ -462,29 +453,16 @@ static int
 bcm_bsc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
 {
 	struct bcm_bsc_softc *sc;
-	uint32_t freq;
-        
+	uint32_t busfreq;
+
 	sc = device_get_softc(dev);
 	BCM_BSC_LOCK(sc);
 	bcm_bsc_reset(sc);
-	freq = 0;
-	switch (speed) {
-	case IIC_SLOW:
-		freq = BCM_BSC_SLOW;
-		break;
-	case IIC_FAST:
-		freq = BCM_BSC_FAST;
-		break;
-	case IIC_FASTEST:
-		freq = BCM_BSC_FASTEST;
-		break;
-	case IIC_UNKNOWN:
-	default:
-		/* Reuse last frequency. */
-		break;
-	}
-	if (freq != 0)
-		BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / freq);
+	if (sc->sc_iicbus == NULL)
+		busfreq = 100000;
+	else
+		busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);
+	BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / busfreq);
 	BCM_BSC_UNLOCK(sc);
 
 	return (IIC_ENOADDR);

Modified: head/sys/arm/broadcom/bcm2835/bcm2835_bscvar.h
==============================================================================
--- head/sys/arm/broadcom/bcm2835/bcm2835_bscvar.h	Sat Dec 27 18:24:15 2014	(r276313)
+++ head/sys/arm/broadcom/bcm2835/bcm2835_bscvar.h	Sat Dec 27 18:54:39 2014	(r276314)
@@ -41,6 +41,7 @@ struct {
 
 struct bcm_bsc_softc {
 	device_t		sc_dev;
+	device_t		sc_iicbus;
 	struct mtx		sc_mtx;
 	struct resource *	sc_mem_res;
 	struct resource *	sc_irq_res;
@@ -56,10 +57,6 @@ struct bcm_bsc_softc {
 #define	BCM_I2C_READ		0x02
 #define	BCM_I2C_ERROR		0x04
 
-#define	BCM_BSC_SLOW		10000	/*  10 kHz. */
-#define	BCM_BSC_FAST		50000	/*  50 kHz. */
-#define	BCM_BSC_FASTEST		100000	/* 100 kHz. */
-
 #define	BCM_BSC_WRITE(_sc, _off, _val)		\
     bus_space_write_4(_sc->sc_bst, _sc->sc_bsh, _off, _val)
 #define	BCM_BSC_READ(_sc, _off)			\


More information about the svn-src-all mailing list