Home | History | Annotate | Line # | Download | only in ic
pcf8584.c revision 1.1
      1  1.1  tnn /*	$NetBSD: pcf8584.c,v 1.1 2007/04/14 19:33:29 tnn Exp $ */
      2  1.1  tnn 
      3  1.1  tnn /*-
      4  1.1  tnn  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.1  tnn  * All rights reserved.
      6  1.1  tnn  *
      7  1.1  tnn  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  tnn  * by Tobias Nygren.
      9  1.1  tnn  *
     10  1.1  tnn  * Redistribution and use in source and binary forms, with or without
     11  1.1  tnn  * modification, are permitted provided that the following conditions
     12  1.1  tnn  * are met:
     13  1.1  tnn  * 1. Redistributions of source code must retain the above copyright
     14  1.1  tnn  *    notice, this list of conditions and the following disclaimer.
     15  1.1  tnn  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  tnn  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  tnn  *    documentation and/or other materials provided with the distribution.
     18  1.1  tnn  * 3. All advertising materials mentioning features or use of this software
     19  1.1  tnn  *    must display the following acknowledgement:
     20  1.1  tnn  *        This product includes software developed by the NetBSD
     21  1.1  tnn  *        Foundation, Inc. and its contributors.
     22  1.1  tnn  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  tnn  *    contributors may be used to endorse or promote products derived
     24  1.1  tnn  *    from this software without specific prior written permission.
     25  1.1  tnn  *
     26  1.1  tnn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  tnn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  tnn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  tnn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  tnn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  tnn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  tnn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  tnn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  tnn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  tnn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  tnn  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  tnn  */
     38  1.1  tnn 
     39  1.1  tnn /*
     40  1.1  tnn  * Philips PCF8584 I2C Bus Controller
     41  1.1  tnn  *
     42  1.1  tnn  * This driver does not yet support multi-master arbitration, concurrent access
     43  1.1  tnn  * or interrupts, but it should be usable for single-master applications.
     44  1.1  tnn  * It is currently used by the envctrl(4) driver on sparc64.
     45  1.1  tnn  */
     46  1.1  tnn 
     47  1.1  tnn #include <sys/cdefs.h>
     48  1.1  tnn __KERNEL_RCSID(0, "$NetBSD: pcf8584.c,v 1.1 2007/04/14 19:33:29 tnn Exp $");
     49  1.1  tnn 
     50  1.1  tnn #include <sys/param.h>
     51  1.1  tnn #include <sys/device.h>
     52  1.1  tnn #include <sys/kernel.h>
     53  1.1  tnn #include <sys/systm.h>
     54  1.1  tnn #include <sys/condvar.h>
     55  1.1  tnn #include <sys/mutex.h>
     56  1.1  tnn #include <machine/bus.h>
     57  1.1  tnn #include <machine/param.h>
     58  1.1  tnn #include <dev/i2c/i2cvar.h>
     59  1.1  tnn #include <dev/ic/pcf8584reg.h>
     60  1.1  tnn #include <dev/ic/pcf8584var.h>
     61  1.1  tnn 
     62  1.1  tnn static void pcf8584_bus_reset(struct pcf8584_handle *, int);
     63  1.1  tnn static int pcf8584_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     64  1.1  tnn 			void *, size_t, int);
     65  1.1  tnn static int pcf8584_acquire_bus(void *, int);
     66  1.1  tnn static void pcf8584_release_bus(void *, int);
     67  1.1  tnn static void pcf8584_wait(struct pcf8584_handle *, int);
     68  1.1  tnn 
     69  1.1  tnn /*  Must delay for 500 ns between bus accesses according to manual. */
     70  1.1  tnn #define DATA_W(x) (DELAY(1), bus_space_write_1(ha->ha_iot, ha->ha_ioh, 0, x))
     71  1.1  tnn #define DATA_R() (DELAY(1), bus_space_read_1(ha->ha_iot, ha->ha_ioh, 0))
     72  1.1  tnn #define CSR_W(x) (DELAY(1), bus_space_write_1(ha->ha_iot, ha->ha_ioh, 1, x))
     73  1.1  tnn #define STATUS_R() (DELAY(1), bus_space_read_1(ha->ha_iot, ha->ha_ioh, 1))
     74  1.1  tnn #define BUSY() ((STATUS_R() & PCF8584_STATUS_BBN) == 0)
     75  1.1  tnn #define PENDING() ((STATUS_R() & PCF8584_STATUS_PIN) == 0)
     76  1.1  tnn #define NAK() ((STATUS_R() & PCF8584_STATUS_LRB) != 0)
     77  1.1  tnn 
     78  1.1  tnn /*
     79  1.1  tnn  * Wait for an interrupt.
     80  1.1  tnn  */
     81  1.1  tnn static void
     82  1.1  tnn pcf8584_wait(struct pcf8584_handle *ha, int flags)
     83  1.1  tnn {
     84  1.1  tnn 	int timeo;
     85  1.1  tnn 
     86  1.1  tnn 	if (flags & I2C_F_POLL) {
     87  1.1  tnn 		timeo = 20;
     88  1.1  tnn 		while (timeo && !PENDING()) {
     89  1.1  tnn 			DELAY(1000);
     90  1.1  tnn 			timeo--;
     91  1.1  tnn 		}
     92  1.1  tnn 	} else {
     93  1.1  tnn 		mutex_enter(&ha->ha_intrmtx);
     94  1.1  tnn 		cv_timedwait(&ha->ha_intrcond, &ha->ha_intrmtx, mstohz(20));
     95  1.1  tnn 		mutex_exit(&ha->ha_intrmtx);
     96  1.1  tnn 	}
     97  1.1  tnn }
     98  1.1  tnn 
     99  1.1  tnn #ifdef notyet
    100  1.1  tnn static void
    101  1.1  tnn pcf8584_intr(struct pcf8584_handle *ha) {
    102  1.1  tnn 
    103  1.1  tnn 	cv_wakeup(&ha->ha_intrcond);
    104  1.1  tnn }
    105  1.1  tnn #endif
    106  1.1  tnn 
    107  1.1  tnn int
    108  1.1  tnn pcf8584_init(struct pcf8584_handle *ha)
    109  1.1  tnn {
    110  1.1  tnn 
    111  1.1  tnn 	ha->ha_i2c.ic_cookie = ha;
    112  1.1  tnn 	ha->ha_i2c.ic_acquire_bus = pcf8584_acquire_bus;
    113  1.1  tnn 	ha->ha_i2c.ic_release_bus = pcf8584_release_bus;
    114  1.1  tnn 	ha->ha_i2c.ic_exec = pcf8584_exec;
    115  1.1  tnn 
    116  1.1  tnn 	mutex_init(&ha->ha_intrmtx, MUTEX_DEFAULT, IPL_NONE);
    117  1.1  tnn 	cv_init(&ha->ha_intrcond, "pcf8584");
    118  1.1  tnn 
    119  1.1  tnn 	pcf8584_bus_reset(ha, I2C_F_POLL);
    120  1.1  tnn 
    121  1.1  tnn 	return 0;
    122  1.1  tnn }
    123  1.1  tnn 
    124  1.1  tnn /*
    125  1.1  tnn  * Reset i2c bus.
    126  1.1  tnn  */
    127  1.1  tnn static void
    128  1.1  tnn pcf8584_bus_reset(struct pcf8584_handle *ha, int flags)
    129  1.1  tnn {
    130  1.1  tnn 
    131  1.1  tnn 	/* initialize PCF8584 */
    132  1.1  tnn 	CSR_W(PCF8584_CTRL_PIN);
    133  1.1  tnn 	DATA_W(0x55);
    134  1.1  tnn 	CSR_W(PCF8584_CTRL_PIN | PCF8584_REG_S2);
    135  1.1  tnn 	DATA_W(PCF8584_CLK_12 | PCF8584_SCL_90);
    136  1.1  tnn 	CSR_W(PCF8584_CTRL_PIN | PCF8584_CTRL_ESO | PCF8584_CTRL_ACK);
    137  1.1  tnn 
    138  1.1  tnn 	/* XXX needs multi-master synchronization delay here */
    139  1.1  tnn 
    140  1.1  tnn 	/*
    141  1.1  tnn 	 * Blindly attempt a write at a nonexistent i2c address (0x7F).
    142  1.1  tnn 	 * This allows hung i2c devices to pick up the stop condition.
    143  1.1  tnn 	 */
    144  1.1  tnn 	DATA_W(0x7F << 1);
    145  1.1  tnn 	CSR_W(PCF8584_CMD_START);
    146  1.1  tnn 	pcf8584_wait(ha, flags);
    147  1.1  tnn 	CSR_W(PCF8584_CMD_STOP);
    148  1.1  tnn 	pcf8584_wait(ha, flags);
    149  1.1  tnn }
    150  1.1  tnn 
    151  1.1  tnn static int
    152  1.1  tnn pcf8584_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
    153  1.1  tnn     const void *cmdbuf, size_t cmdlen, void *buf,
    154  1.1  tnn     size_t len, int flags)
    155  1.1  tnn {
    156  1.1  tnn 	int i;
    157  1.1  tnn 	struct pcf8584_handle *ha = cookie;
    158  1.1  tnn 	uint8_t *p = buf;
    159  1.1  tnn 
    160  1.1  tnn 	KASSERT(cmdlen == 0);
    161  1.1  tnn 	KASSERT(op == I2C_OP_READ_WITH_STOP || op == I2C_OP_WRITE_WITH_STOP);
    162  1.1  tnn 
    163  1.1  tnn 	if (BUSY()) {
    164  1.1  tnn 		/* We're the only master on the bus, something is wrong. */
    165  1.1  tnn 		printf("*%s: i2c bus busy!\n", ha->ha_parent->dv_xname);
    166  1.1  tnn 		pcf8584_bus_reset(ha, flags);
    167  1.1  tnn 	}
    168  1.1  tnn 	if (op == I2C_OP_READ_WITH_STOP)
    169  1.1  tnn 		DATA_W((addr << 1) | 1);
    170  1.1  tnn 	else
    171  1.1  tnn 		DATA_W(addr << 1);
    172  1.1  tnn 
    173  1.1  tnn 	CSR_W(PCF8584_CMD_START);
    174  1.1  tnn 	pcf8584_wait(ha, flags);
    175  1.1  tnn 	if (!PENDING()) {
    176  1.1  tnn 		printf("%s: no intr after i2c sla\n", ha->ha_parent->dv_xname);
    177  1.1  tnn 	}
    178  1.1  tnn 	if (NAK())
    179  1.1  tnn 		goto fail;
    180  1.1  tnn 
    181  1.1  tnn 	if (op == I2C_OP_READ_WITH_STOP) {
    182  1.1  tnn 		(void) DATA_R();/* dummy read */
    183  1.1  tnn 		for (i = 0; i < len; i++) {
    184  1.1  tnn 			/* wait for a byte to arrive */
    185  1.1  tnn 			pcf8584_wait(ha, flags);
    186  1.1  tnn 			if (!PENDING()) {
    187  1.1  tnn 				printf("%s: lost intr during i2c read\n",
    188  1.1  tnn 				    ha->ha_parent->dv_xname);
    189  1.1  tnn 				goto fail;
    190  1.1  tnn 			}
    191  1.1  tnn 			if (NAK())
    192  1.1  tnn 				goto fail;
    193  1.1  tnn 			if (i == len - 1) {
    194  1.1  tnn 				/*
    195  1.1  tnn 				 * we're about to read the final byte, so we
    196  1.1  tnn 				 * set the controller to NAK the following
    197  1.1  tnn 				 * byte, if any.
    198  1.1  tnn 				 */
    199  1.1  tnn 				CSR_W(PCF8584_CMD_NAK);
    200  1.1  tnn 			}
    201  1.1  tnn 			*p++ = DATA_R();
    202  1.1  tnn 		}
    203  1.1  tnn 		pcf8584_wait(ha, flags);
    204  1.1  tnn 		if (!PENDING()) {
    205  1.1  tnn 			printf("%s: no intr on final i2c nak\n",
    206  1.1  tnn 			    ha->ha_parent->dv_xname);
    207  1.1  tnn 			goto fail;
    208  1.1  tnn 		}
    209  1.1  tnn 		CSR_W(PCF8584_CMD_STOP);
    210  1.1  tnn 		(void) DATA_R();/* dummy read */
    211  1.1  tnn 	} else {
    212  1.1  tnn 		for (i = 0; i < len; i++) {
    213  1.1  tnn 			DATA_W(*p++);
    214  1.1  tnn 			pcf8584_wait(ha, flags);
    215  1.1  tnn 			if (!PENDING()) {
    216  1.1  tnn 				printf("%s: no intr during i2c write\n",
    217  1.1  tnn 				    ha->ha_parent->dv_xname);
    218  1.1  tnn 				goto fail;
    219  1.1  tnn 			}
    220  1.1  tnn 			if (NAK())
    221  1.1  tnn 				goto fail;
    222  1.1  tnn 		}
    223  1.1  tnn 		CSR_W(PCF8584_CMD_STOP);
    224  1.1  tnn 	}
    225  1.1  tnn 	pcf8584_wait(ha, flags);
    226  1.1  tnn 	return 0;
    227  1.1  tnn fail:
    228  1.1  tnn 	CSR_W(PCF8584_CMD_STOP);
    229  1.1  tnn 	pcf8584_wait(ha, flags);
    230  1.1  tnn 
    231  1.1  tnn 	return 1;
    232  1.1  tnn }
    233  1.1  tnn 
    234  1.1  tnn static int
    235  1.1  tnn pcf8584_acquire_bus(void *cookie, int flags)
    236  1.1  tnn {
    237  1.1  tnn 
    238  1.1  tnn 	/* XXX concurrent access not yet implemented */
    239  1.1  tnn 	return 0;
    240  1.1  tnn }
    241  1.1  tnn 
    242  1.1  tnn static void
    243  1.1  tnn pcf8584_release_bus(void *cookie, int flags)
    244  1.1  tnn {
    245  1.1  tnn 
    246  1.1  tnn }
    247