Home | History | Annotate | Line # | Download | only in ic
pcf8584.c revision 1.13
      1  1.13       jdc /*	$NetBSD: pcf8584.c,v 1.13 2016/01/03 17:32:17 jdc Exp $	*/
      2   1.6    martin /*	$OpenBSD: pcf8584.c,v 1.9 2007/10/20 18:46:21 kettenis Exp $ */
      3   1.1       tnn 
      4   1.6    martin /*
      5   1.6    martin  * Copyright (c) 2006 David Gwynne <dlg (at) openbsd.org>
      6   1.1       tnn  *
      7   1.6    martin  * Permission to use, copy, modify, and distribute this software for any
      8   1.6    martin  * purpose with or without fee is hereby granted, provided that the above
      9   1.6    martin  * copyright notice and this permission notice appear in all copies.
     10   1.1       tnn  *
     11   1.6    martin  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.6    martin  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.6    martin  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.6    martin  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.6    martin  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.6    martin  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.6    martin  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1       tnn  */
     19   1.1       tnn 
     20   1.1       tnn #include <sys/param.h>
     21   1.6    martin #include <sys/systm.h>
     22   1.1       tnn #include <sys/device.h>
     23   1.6    martin #include <sys/malloc.h>
     24   1.1       tnn #include <sys/kernel.h>
     25   1.6    martin #include <sys/rwlock.h>
     26   1.6    martin #include <sys/proc.h>
     27   1.9    dyoung #include <sys/bus.h>
     28   1.6    martin 
     29   1.1       tnn #include <dev/i2c/i2cvar.h>
     30   1.6    martin 
     31   1.1       tnn #include <dev/ic/pcf8584var.h>
     32  1.13       jdc #include <dev/ic/pcf8584reg.h>
     33   1.1       tnn 
     34  1.13       jdc /* Internal egisters */
     35  1.13       jdc #define PCF8584_S0		0x00
     36  1.13       jdc #define PCF8584_S1		0x01
     37  1.13       jdc #define PCF8584_S2		0x02
     38  1.13       jdc #define PCF8584_S3		0x03
     39   1.6    martin 
     40   1.6    martin void		pcfiic_init(struct pcfiic_softc *);
     41   1.6    martin int		pcfiic_i2c_acquire_bus(void *, int);
     42   1.6    martin void		pcfiic_i2c_release_bus(void *, int);
     43   1.6    martin int		pcfiic_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     44   1.6    martin 		    size_t, void *, size_t, int);
     45   1.6    martin 
     46   1.6    martin int		pcfiic_xmit(struct pcfiic_softc *, u_int8_t, const u_int8_t *,
     47   1.6    martin 		    size_t);
     48   1.6    martin int		pcfiic_recv(struct pcfiic_softc *, u_int8_t, u_int8_t *,
     49   1.6    martin 		    size_t);
     50   1.6    martin 
     51   1.6    martin u_int8_t	pcfiic_read(struct pcfiic_softc *, bus_size_t);
     52   1.6    martin void		pcfiic_write(struct pcfiic_softc *, bus_size_t, u_int8_t);
     53   1.6    martin void		pcfiic_choose_bus(struct pcfiic_softc *, u_int8_t);
     54  1.13       jdc int		pcfiic_wait_BBN(struct pcfiic_softc *);
     55   1.6    martin int		pcfiic_wait_pin(struct pcfiic_softc *, volatile u_int8_t *);
     56   1.6    martin 
     57   1.6    martin void
     58   1.6    martin pcfiic_init(struct pcfiic_softc *sc)
     59   1.6    martin {
     60   1.6    martin 	/* init S1 */
     61  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CTRL_PIN);
     62   1.6    martin 	/* own address */
     63  1.13       jdc 	pcfiic_write(sc, PCF8584_S0, sc->sc_addr);
     64   1.6    martin 
     65   1.6    martin 	/* select clock reg */
     66  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CTRL_PIN | PCF8584_CTRL_ES1);
     67  1.13       jdc 	pcfiic_write(sc, PCF8584_S0, sc->sc_clock);
     68   1.6    martin 
     69  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_IDLE);
     70   1.1       tnn 
     71   1.6    martin 	delay(200000);	/* Multi-Master mode, wait for longest i2c message */
     72   1.6    martin }
     73   1.6    martin 
     74   1.6    martin void
     75   1.6    martin pcfiic_attach(struct pcfiic_softc *sc, i2c_addr_t addr, u_int8_t clock,
     76   1.6    martin     int swapregs)
     77   1.1       tnn {
     78   1.6    martin 	struct i2cbus_attach_args		iba;
     79   1.1       tnn 
     80   1.6    martin 	if (swapregs) {
     81  1.13       jdc 		sc->sc_regmap[PCF8584_S1] = PCF8584_S0;
     82  1.13       jdc 		sc->sc_regmap[PCF8584_S0] = PCF8584_S1;
     83   1.1       tnn 	} else {
     84  1.13       jdc 		sc->sc_regmap[PCF8584_S0] = PCF8584_S0;
     85  1.13       jdc 		sc->sc_regmap[PCF8584_S1] = PCF8584_S1;
     86   1.1       tnn 	}
     87   1.6    martin 	sc->sc_clock = clock;
     88   1.6    martin 	sc->sc_addr = addr;
     89   1.6    martin 
     90   1.6    martin 	pcfiic_init(sc);
     91   1.6    martin 
     92   1.6    martin 	printf("\n");
     93   1.6    martin 
     94   1.6    martin 	if (sc->sc_master)
     95   1.6    martin 		pcfiic_choose_bus(sc, 0);
     96   1.6    martin 
     97   1.6    martin 	rw_init(&sc->sc_lock);
     98   1.6    martin 	sc->sc_i2c.ic_cookie = sc;
     99   1.6    martin 	sc->sc_i2c.ic_acquire_bus = pcfiic_i2c_acquire_bus;
    100   1.6    martin 	sc->sc_i2c.ic_release_bus = pcfiic_i2c_release_bus;
    101   1.6    martin 	sc->sc_i2c.ic_exec = pcfiic_i2c_exec;
    102   1.6    martin 
    103   1.6    martin 	bzero(&iba, sizeof(iba));
    104   1.6    martin 	iba.iba_tag = &sc->sc_i2c;
    105   1.6    martin 	config_found(sc->sc_dev, &iba, iicbus_print);
    106   1.6    martin }
    107   1.6    martin 
    108   1.6    martin int
    109   1.6    martin pcfiic_intr(void *arg)
    110   1.6    martin {
    111   1.6    martin 	return (0);
    112   1.1       tnn }
    113   1.1       tnn 
    114   1.6    martin int
    115   1.6    martin pcfiic_i2c_acquire_bus(void *arg, int flags)
    116   1.6    martin {
    117   1.6    martin 	struct pcfiic_softc	*sc = arg;
    118   1.6    martin 
    119   1.6    martin 	if (cold || sc->sc_poll || (flags & I2C_F_POLL))
    120   1.6    martin 		return (0);
    121   1.6    martin 
    122   1.6    martin 	rw_enter(&sc->sc_lock, RW_WRITER);
    123   1.6    martin 	return 0;
    124   1.6    martin }
    125   1.6    martin 
    126   1.6    martin void
    127   1.6    martin pcfiic_i2c_release_bus(void *arg, int flags)
    128   1.6    martin {
    129   1.6    martin 	struct pcfiic_softc	*sc = arg;
    130   1.6    martin 
    131   1.6    martin 	if (cold || sc->sc_poll || (flags & I2C_F_POLL))
    132   1.6    martin 		return;
    133   1.1       tnn 
    134   1.6    martin 	rw_exit(&sc->sc_lock);
    135   1.1       tnn }
    136   1.6    martin 
    137   1.6    martin int
    138   1.6    martin pcfiic_i2c_exec(void *arg, i2c_op_t op, i2c_addr_t addr,
    139   1.6    martin     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    140   1.6    martin {
    141   1.6    martin 	struct pcfiic_softc	*sc = arg;
    142   1.6    martin 	int			ret = 0;
    143   1.6    martin 
    144   1.6    martin #if 0
    145   1.6    martin         printf("%s: exec op: %d addr: 0x%x cmdlen: %d len: %d flags 0x%x\n",
    146   1.7  macallan             device_xname(sc->sc_dev), op, addr, (int)cmdlen, (int)len, flags);
    147   1.1       tnn #endif
    148   1.1       tnn 
    149   1.6    martin 	if (cold || sc->sc_poll)
    150   1.6    martin 		flags |= I2C_F_POLL;
    151   1.6    martin 
    152   1.6    martin 	if (sc->sc_master)
    153   1.6    martin 		pcfiic_choose_bus(sc, addr >> 7);
    154   1.6    martin 
    155  1.12       jdc 	/*
    156  1.12       jdc 	 * If we are writing, write address, cmdbuf, buf.
    157  1.12       jdc 	 * If we are reading, write address, cmdbuf, then read address, buf.
    158  1.12       jdc 	 */
    159  1.12       jdc 	if (I2C_OP_WRITE_P(op)) {
    160  1.12       jdc 		if (len > 0) {
    161  1.12       jdc 			uint8_t *tmp;
    162  1.12       jdc 
    163  1.12       jdc 			tmp = malloc(cmdlen + len, M_DEVBUF,
    164  1.12       jdc 			   flags & I2C_F_POLL ? M_NOWAIT : M_WAITOK);
    165  1.12       jdc 			if (tmp == NULL)
    166  1.12       jdc 				return (1);
    167  1.12       jdc 			memcpy(tmp, cmdbuf, cmdlen);
    168  1.12       jdc 			memcpy(tmp + cmdlen, buf, len);
    169  1.12       jdc 			ret = pcfiic_xmit(sc, addr & 0x7f, tmp, cmdlen + len);
    170  1.12       jdc 			free(tmp, M_DEVBUF);
    171  1.12       jdc 		} else
    172  1.12       jdc 			ret = pcfiic_xmit(sc, addr & 0x7f, cmdbuf, cmdlen);
    173  1.12       jdc 	} else {
    174  1.12       jdc 		if (pcfiic_xmit(sc, addr & 0x7f, cmdbuf, cmdlen) != 0)
    175  1.12       jdc 			return (1);
    176  1.12       jdc 		ret = pcfiic_recv(sc, addr & 0x7f, buf, len);
    177   1.6    martin 	}
    178   1.6    martin 	return (ret);
    179   1.6    martin }
    180   1.6    martin 
    181   1.1       tnn int
    182   1.6    martin pcfiic_xmit(struct pcfiic_softc *sc, u_int8_t addr, const u_int8_t *buf,
    183   1.6    martin     size_t len)
    184   1.1       tnn {
    185   1.6    martin 	int			i, err = 0;
    186   1.6    martin 	volatile u_int8_t	r;
    187   1.1       tnn 
    188  1.13       jdc 	if (pcfiic_wait_BBN(sc) != 0)
    189   1.6    martin 		return (1);
    190   1.1       tnn 
    191  1.13       jdc 	pcfiic_write(sc, PCF8584_S0, addr << 1);
    192  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_START);
    193   1.1       tnn 
    194   1.6    martin 	for (i = 0; i <= len; i++) {
    195   1.6    martin 		if (pcfiic_wait_pin(sc, &r) != 0) {
    196  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    197   1.6    martin 			return (1);
    198   1.6    martin 		}
    199   1.1       tnn 
    200  1.13       jdc 		if (r & PCF8584_STATUS_LRB) {
    201   1.6    martin 			err = 1;
    202   1.6    martin 			break;
    203   1.6    martin 		}
    204   1.6    martin 
    205   1.6    martin 		if (i < len)
    206  1.13       jdc 			pcfiic_write(sc, PCF8584_S0, buf[i]);
    207   1.6    martin 	}
    208  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    209   1.6    martin 	return (err);
    210   1.1       tnn }
    211   1.1       tnn 
    212   1.6    martin int
    213   1.6    martin pcfiic_recv(struct pcfiic_softc *sc, u_int8_t addr, u_int8_t *buf, size_t len)
    214   1.1       tnn {
    215   1.6    martin 	int			i = 0, err = 0;
    216   1.6    martin 	volatile u_int8_t	r;
    217   1.6    martin 
    218  1.13       jdc 	if (pcfiic_wait_BBN(sc) != 0)
    219   1.6    martin 		return (1);
    220   1.6    martin 
    221  1.13       jdc 	pcfiic_write(sc, PCF8584_S0, (addr << 1) | 0x01);
    222  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_START);
    223   1.1       tnn 
    224   1.6    martin 	for (i = 0; i <= len; i++) {
    225   1.6    martin 		if (pcfiic_wait_pin(sc, &r) != 0) {
    226  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    227   1.6    martin 			return (1);
    228   1.1       tnn 		}
    229   1.6    martin 
    230  1.13       jdc 		if ((i != len) && (r & PCF8584_STATUS_LRB)) {
    231  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    232   1.6    martin 			return (1);
    233   1.1       tnn 		}
    234   1.6    martin 
    235   1.6    martin 		if (i == len - 1) {
    236  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_NAK);
    237   1.6    martin 		} else if (i == len) {
    238  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    239   1.1       tnn 		}
    240   1.6    martin 
    241  1.13       jdc 		r = pcfiic_read(sc, PCF8584_S0);
    242   1.6    martin 		if (i > 0)
    243   1.6    martin 			buf[i - 1] = r;
    244   1.1       tnn 	}
    245   1.6    martin 	return (err);
    246   1.6    martin }
    247   1.6    martin 
    248   1.6    martin u_int8_t
    249   1.6    martin pcfiic_read(struct pcfiic_softc *sc, bus_size_t r)
    250   1.6    martin {
    251   1.6    martin 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, sc->sc_regmap[r], 1,
    252   1.6    martin 	    BUS_SPACE_BARRIER_READ);
    253   1.6    martin 	return (bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regmap[r]));
    254   1.6    martin }
    255   1.6    martin 
    256   1.6    martin void
    257   1.6    martin pcfiic_write(struct pcfiic_softc *sc, bus_size_t r, u_int8_t v)
    258   1.6    martin {
    259   1.6    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_regmap[r], v);
    260  1.13       jdc 	(void)bus_space_read_1(sc->sc_iot, sc->sc_ioh, PCF8584_S1);
    261   1.6    martin }
    262   1.1       tnn 
    263   1.6    martin void
    264   1.6    martin pcfiic_choose_bus(struct pcfiic_softc *sc, u_int8_t bus)
    265   1.6    martin {
    266   1.6    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh2, 0, bus);
    267   1.6    martin 	bus_space_barrier(sc->sc_iot, sc->sc_ioh2, 0, 1,
    268   1.6    martin 	    BUS_SPACE_BARRIER_WRITE);
    269   1.1       tnn }
    270   1.1       tnn 
    271   1.6    martin int
    272  1.13       jdc pcfiic_wait_BBN(struct pcfiic_softc *sc)
    273   1.1       tnn {
    274   1.6    martin 	int		i;
    275   1.1       tnn 
    276   1.6    martin 	for (i = 0; i < 1000; i++) {
    277  1.13       jdc 		if (pcfiic_read(sc, PCF8584_S1) & PCF8584_STATUS_BBN)
    278   1.6    martin 			return (0);
    279   1.6    martin 		delay(1000);
    280   1.6    martin 	}
    281   1.6    martin 	return (1);
    282   1.1       tnn }
    283   1.1       tnn 
    284   1.6    martin int
    285   1.6    martin pcfiic_wait_pin(struct pcfiic_softc *sc, volatile u_int8_t *r)
    286   1.1       tnn {
    287   1.6    martin 	int		i;
    288   1.1       tnn 
    289   1.6    martin 	for (i = 0; i < 1000; i++) {
    290  1.13       jdc 		*r = pcfiic_read(sc, PCF8584_S1);
    291  1.13       jdc 		if ((*r & PCF8584_STATUS_PIN) == 0)
    292   1.6    martin 			return (0);
    293   1.6    martin 		delay(1000);
    294   1.6    martin 	}
    295   1.6    martin 	return (1);
    296   1.1       tnn }
    297