Home | History | Annotate | Line # | Download | only in i2c
i2c.c revision 1.14
      1 /*	$NetBSD: i2c.c,v 1.14 2007/07/09 21:00:32 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/event.h>
     42 #include <sys/conf.h>
     43 #include <sys/malloc.h>
     44 #include <sys/kthread.h>
     45 #include <sys/proc.h>
     46 #include <sys/kernel.h>
     47 
     48 #include <dev/i2c/i2cvar.h>
     49 
     50 #include "locators.h"
     51 
     52 struct iic_softc {
     53 	struct device sc_dev;
     54 	i2c_tag_t sc_tag;
     55 	int sc_type;
     56 };
     57 
     58 static void	iic_smbus_intr_thread(void *);
     59 
     60 int
     61 iicbus_print(void *aux, const char *pnp)
     62 {
     63 
     64 	if (pnp != NULL)
     65 		aprint_normal("iic at %s", pnp);
     66 
     67 	return (UNCONF);
     68 }
     69 
     70 static int
     71 iic_print(void *aux, const char *pnp)
     72 {
     73 	struct i2c_attach_args *ia = aux;
     74 
     75 	if (ia->ia_addr != (i2c_addr_t)-1)
     76 		aprint_normal(" addr 0x%x", ia->ia_addr);
     77 
     78 	return (UNCONF);
     79 }
     80 
     81 static int
     82 iic_search(struct device *parent, struct cfdata *cf,
     83     const int *ldesc, void *aux)
     84 {
     85 	struct iic_softc *sc = (void *) parent;
     86 	struct i2c_attach_args ia;
     87 
     88 	ia.ia_tag = sc->sc_tag;
     89 	ia.ia_addr = cf->cf_loc[IICCF_ADDR];
     90 	ia.ia_size = cf->cf_loc[IICCF_SIZE];
     91 	ia.ia_type = sc->sc_type;
     92 
     93 	if (config_match(parent, cf, &ia) > 0)
     94 		config_attach(parent, cf, &ia, iic_print);
     95 
     96 	return (0);
     97 }
     98 
     99 static int
    100 iic_match(struct device *parent, struct cfdata *cf,
    101     void *aux)
    102 {
    103 
    104 	return (1);
    105 }
    106 
    107 static void
    108 iic_attach(struct device *parent, struct device *self, void *aux)
    109 {
    110 	struct iic_softc *sc = device_private(self);
    111 	struct i2cbus_attach_args *iba = aux;
    112 	i2c_tag_t ic;
    113 	int rv;
    114 
    115 	aprint_naive(": I2C bus\n");
    116 	aprint_normal(": I2C bus\n");
    117 
    118 	sc->sc_tag = iba->iba_tag;
    119 	sc->sc_type = iba->iba_type;
    120 	ic = sc->sc_tag;
    121 	ic->ic_devname = self->dv_xname;
    122 
    123 	LIST_INIT(&(sc->sc_tag->ic_list));
    124 	LIST_INIT(&(sc->sc_tag->ic_proc_list));
    125 
    126 	rv = kthread_create(PRI_NONE, 0, NULL, iic_smbus_intr_thread,
    127 	    ic, &ic->ic_intr_thread, "%s", ic->ic_devname);
    128 	if (rv)
    129 		printf("%s: unable to create intr thread\n", ic->ic_devname);
    130 
    131 	/*
    132 	 * Attach all i2c devices described in the kernel
    133 	 * configuration file.
    134 	 */
    135 	config_search_ia(iic_search, self, "iic", NULL);
    136 }
    137 
    138 static void
    139 iic_smbus_intr_thread(void *aux)
    140 {
    141 	i2c_tag_t ic;
    142 	struct ic_intr_list *il;
    143 	int rv;
    144 
    145 	ic = (i2c_tag_t)aux;
    146 	ic->ic_running = 1;
    147 	ic->ic_pending = 0;
    148 
    149 	while (ic->ic_running) {
    150 		if (ic->ic_pending == 0)
    151 			rv = tsleep(ic, PZERO, "iicintr", hz);
    152 		if (ic->ic_pending > 0) {
    153 			LIST_FOREACH(il, &(ic->ic_proc_list), il_next) {
    154 				(*il->il_intr)(il->il_intrarg);
    155 			}
    156 			ic->ic_pending--;
    157 		}
    158 	}
    159 
    160 	kthread_exit(0);
    161 }
    162 
    163 void *
    164 iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
    165 {
    166 	struct ic_intr_list *il;
    167 
    168 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
    169 	if (il == NULL)
    170 		return NULL;
    171 
    172 	il->il_intr = intr;
    173 	il->il_intrarg = intrarg;
    174 
    175 	LIST_INSERT_HEAD(&(ic->ic_list), il, il_next);
    176 
    177 	return il;
    178 }
    179 
    180 void
    181 iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl)
    182 {
    183 	struct ic_intr_list *il;
    184 
    185 	il = (struct ic_intr_list *)hdl;
    186 
    187 	LIST_REMOVE(il, il_next);
    188 	free(il, M_DEVBUF);
    189 
    190 	return;
    191 }
    192 
    193 void *
    194 iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
    195 {
    196 	struct ic_intr_list *il;
    197 
    198 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
    199 	if (il == NULL)
    200 		return NULL;
    201 
    202 	il->il_intr = intr;
    203 	il->il_intrarg = intrarg;
    204 
    205 	LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next);
    206 
    207 	return il;
    208 }
    209 
    210 void
    211 iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl)
    212 {
    213 	struct ic_intr_list *il;
    214 
    215 	il = (struct ic_intr_list *)hdl;
    216 
    217 	LIST_REMOVE(il, il_next);
    218 	free(il, M_DEVBUF);
    219 
    220 	return;
    221 }
    222 
    223 int
    224 iic_smbus_intr(i2c_tag_t ic)
    225 {
    226 	struct ic_intr_list *il;
    227 
    228 	LIST_FOREACH(il, &(ic->ic_list), il_next) {
    229 		(*il->il_intr)(il->il_intrarg);
    230 	}
    231 
    232 	ic->ic_pending++;
    233 	wakeup(ic);
    234 
    235 	return 1;
    236 }
    237 
    238 CFATTACH_DECL(iic, sizeof(struct iic_softc),
    239     iic_match, iic_attach, NULL, NULL);
    240