Home | History | Annotate | Line # | Download | only in ic
pcf8584.c revision 1.16
      1  1.16   thorpej /*	$NetBSD: pcf8584.c,v 1.16 2019/12/22 23:23:32 thorpej 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/proc.h>
     26   1.9    dyoung #include <sys/bus.h>
     27   1.6    martin 
     28   1.1       tnn #include <dev/i2c/i2cvar.h>
     29   1.6    martin 
     30   1.1       tnn #include <dev/ic/pcf8584var.h>
     31  1.13       jdc #include <dev/ic/pcf8584reg.h>
     32   1.1       tnn 
     33  1.14       jdc /* Internal registers */
     34  1.13       jdc #define PCF8584_S0		0x00
     35  1.13       jdc #define PCF8584_S1		0x01
     36  1.13       jdc #define PCF8584_S2		0x02
     37  1.13       jdc #define PCF8584_S3		0x03
     38   1.6    martin 
     39   1.6    martin void		pcfiic_init(struct pcfiic_softc *);
     40   1.6    martin int		pcfiic_i2c_acquire_bus(void *, int);
     41   1.6    martin void		pcfiic_i2c_release_bus(void *, int);
     42   1.6    martin int		pcfiic_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     43   1.6    martin 		    size_t, void *, size_t, int);
     44   1.6    martin 
     45   1.6    martin int		pcfiic_xmit(struct pcfiic_softc *, u_int8_t, const u_int8_t *,
     46  1.14       jdc 		    size_t, const u_int8_t *, size_t);
     47   1.6    martin int		pcfiic_recv(struct pcfiic_softc *, u_int8_t, u_int8_t *,
     48   1.6    martin 		    size_t);
     49   1.6    martin 
     50   1.6    martin u_int8_t	pcfiic_read(struct pcfiic_softc *, bus_size_t);
     51   1.6    martin void		pcfiic_write(struct pcfiic_softc *, bus_size_t, u_int8_t);
     52   1.6    martin void		pcfiic_choose_bus(struct pcfiic_softc *, u_int8_t);
     53  1.13       jdc int		pcfiic_wait_BBN(struct pcfiic_softc *);
     54   1.6    martin int		pcfiic_wait_pin(struct pcfiic_softc *, volatile u_int8_t *);
     55   1.6    martin 
     56   1.6    martin void
     57   1.6    martin pcfiic_init(struct pcfiic_softc *sc)
     58   1.6    martin {
     59   1.6    martin 	/* init S1 */
     60  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CTRL_PIN);
     61   1.6    martin 	/* own address */
     62  1.13       jdc 	pcfiic_write(sc, PCF8584_S0, sc->sc_addr);
     63   1.6    martin 
     64   1.6    martin 	/* select clock reg */
     65  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CTRL_PIN | PCF8584_CTRL_ES1);
     66  1.13       jdc 	pcfiic_write(sc, PCF8584_S0, sc->sc_clock);
     67   1.6    martin 
     68  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_IDLE);
     69   1.1       tnn 
     70   1.6    martin 	delay(200000);	/* Multi-Master mode, wait for longest i2c message */
     71   1.6    martin }
     72   1.6    martin 
     73   1.6    martin void
     74   1.6    martin pcfiic_attach(struct pcfiic_softc *sc, i2c_addr_t addr, u_int8_t clock,
     75   1.6    martin     int swapregs)
     76   1.1       tnn {
     77   1.6    martin 	struct i2cbus_attach_args		iba;
     78   1.1       tnn 
     79   1.6    martin 	if (swapregs) {
     80  1.13       jdc 		sc->sc_regmap[PCF8584_S1] = PCF8584_S0;
     81  1.13       jdc 		sc->sc_regmap[PCF8584_S0] = PCF8584_S1;
     82   1.1       tnn 	} else {
     83  1.13       jdc 		sc->sc_regmap[PCF8584_S0] = PCF8584_S0;
     84  1.13       jdc 		sc->sc_regmap[PCF8584_S1] = PCF8584_S1;
     85   1.1       tnn 	}
     86   1.6    martin 	sc->sc_clock = clock;
     87   1.6    martin 	sc->sc_addr = addr;
     88   1.6    martin 
     89   1.6    martin 	pcfiic_init(sc);
     90   1.6    martin 
     91   1.6    martin 	printf("\n");
     92   1.6    martin 
     93   1.6    martin 	if (sc->sc_master)
     94   1.6    martin 		pcfiic_choose_bus(sc, 0);
     95   1.6    martin 
     96  1.16   thorpej 	iic_tag_init(&sc->sc_i2c);
     97   1.6    martin 	sc->sc_i2c.ic_cookie = sc;
     98   1.6    martin 	sc->sc_i2c.ic_exec = pcfiic_i2c_exec;
     99   1.6    martin 
    100   1.6    martin 	bzero(&iba, sizeof(iba));
    101   1.6    martin 	iba.iba_tag = &sc->sc_i2c;
    102   1.6    martin 	config_found(sc->sc_dev, &iba, iicbus_print);
    103   1.6    martin }
    104   1.6    martin 
    105   1.6    martin int
    106   1.6    martin pcfiic_intr(void *arg)
    107   1.6    martin {
    108   1.6    martin 	return (0);
    109   1.1       tnn }
    110   1.1       tnn 
    111   1.6    martin int
    112   1.6    martin pcfiic_i2c_exec(void *arg, i2c_op_t op, i2c_addr_t addr,
    113   1.6    martin     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    114   1.6    martin {
    115   1.6    martin 	struct pcfiic_softc	*sc = arg;
    116   1.6    martin 	int			ret = 0;
    117   1.6    martin 
    118   1.6    martin #if 0
    119   1.6    martin         printf("%s: exec op: %d addr: 0x%x cmdlen: %d len: %d flags 0x%x\n",
    120   1.7  macallan             device_xname(sc->sc_dev), op, addr, (int)cmdlen, (int)len, flags);
    121   1.1       tnn #endif
    122   1.1       tnn 
    123   1.6    martin 	if (cold || sc->sc_poll)
    124   1.6    martin 		flags |= I2C_F_POLL;
    125   1.6    martin 
    126   1.6    martin 	if (sc->sc_master)
    127   1.6    martin 		pcfiic_choose_bus(sc, addr >> 7);
    128   1.6    martin 
    129  1.12       jdc 	/*
    130  1.12       jdc 	 * If we are writing, write address, cmdbuf, buf.
    131  1.12       jdc 	 * If we are reading, write address, cmdbuf, then read address, buf.
    132  1.12       jdc 	 */
    133  1.12       jdc 	if (I2C_OP_WRITE_P(op)) {
    134  1.14       jdc 		ret = pcfiic_xmit(sc, addr & 0x7f, cmdbuf, cmdlen, buf, len);
    135  1.12       jdc 	} else {
    136  1.14       jdc 		if (pcfiic_xmit(sc, addr & 0x7f, cmdbuf, cmdlen, NULL, 0) != 0)
    137  1.12       jdc 			return (1);
    138  1.12       jdc 		ret = pcfiic_recv(sc, addr & 0x7f, buf, len);
    139   1.6    martin 	}
    140   1.6    martin 	return (ret);
    141   1.6    martin }
    142   1.6    martin 
    143   1.1       tnn int
    144  1.14       jdc pcfiic_xmit(struct pcfiic_softc *sc, u_int8_t addr, const u_int8_t *cmdbuf,
    145  1.14       jdc     size_t cmdlen, const u_int8_t *buf, size_t len)
    146   1.1       tnn {
    147   1.6    martin 	int			i, err = 0;
    148   1.6    martin 	volatile u_int8_t	r;
    149   1.1       tnn 
    150  1.13       jdc 	if (pcfiic_wait_BBN(sc) != 0)
    151   1.6    martin 		return (1);
    152   1.1       tnn 
    153  1.13       jdc 	pcfiic_write(sc, PCF8584_S0, addr << 1);
    154  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_START);
    155   1.1       tnn 
    156  1.14       jdc 	for (i = 0; i <= cmdlen + len; i++) {
    157   1.6    martin 		if (pcfiic_wait_pin(sc, &r) != 0) {
    158  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    159   1.6    martin 			return (1);
    160   1.6    martin 		}
    161   1.1       tnn 
    162  1.13       jdc 		if (r & PCF8584_STATUS_LRB) {
    163   1.6    martin 			err = 1;
    164   1.6    martin 			break;
    165   1.6    martin 		}
    166   1.6    martin 
    167  1.14       jdc 		if (i < cmdlen)
    168  1.14       jdc 			pcfiic_write(sc, PCF8584_S0, cmdbuf[i]);
    169  1.14       jdc 		else if (i < cmdlen + len)
    170  1.14       jdc 			pcfiic_write(sc, PCF8584_S0, buf[i - cmdlen]);
    171   1.6    martin 	}
    172  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    173   1.6    martin 	return (err);
    174   1.1       tnn }
    175   1.1       tnn 
    176   1.6    martin int
    177   1.6    martin pcfiic_recv(struct pcfiic_softc *sc, u_int8_t addr, u_int8_t *buf, size_t len)
    178   1.1       tnn {
    179   1.6    martin 	int			i = 0, err = 0;
    180   1.6    martin 	volatile u_int8_t	r;
    181   1.6    martin 
    182  1.13       jdc 	if (pcfiic_wait_BBN(sc) != 0)
    183   1.6    martin 		return (1);
    184   1.6    martin 
    185  1.13       jdc 	pcfiic_write(sc, PCF8584_S0, (addr << 1) | 0x01);
    186  1.13       jdc 	pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_START);
    187   1.1       tnn 
    188   1.6    martin 	for (i = 0; i <= len; i++) {
    189   1.6    martin 		if (pcfiic_wait_pin(sc, &r) != 0) {
    190  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    191   1.6    martin 			return (1);
    192   1.1       tnn 		}
    193   1.6    martin 
    194  1.13       jdc 		if ((i != len) && (r & PCF8584_STATUS_LRB)) {
    195  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    196   1.6    martin 			return (1);
    197   1.1       tnn 		}
    198   1.6    martin 
    199   1.6    martin 		if (i == len - 1) {
    200  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_NAK);
    201   1.6    martin 		} else if (i == len) {
    202  1.13       jdc 			pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
    203   1.1       tnn 		}
    204   1.6    martin 
    205  1.13       jdc 		r = pcfiic_read(sc, PCF8584_S0);
    206   1.6    martin 		if (i > 0)
    207   1.6    martin 			buf[i - 1] = r;
    208   1.1       tnn 	}
    209   1.6    martin 	return (err);
    210   1.6    martin }
    211   1.6    martin 
    212   1.6    martin u_int8_t
    213   1.6    martin pcfiic_read(struct pcfiic_softc *sc, bus_size_t r)
    214   1.6    martin {
    215   1.6    martin 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, sc->sc_regmap[r], 1,
    216   1.6    martin 	    BUS_SPACE_BARRIER_READ);
    217   1.6    martin 	return (bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regmap[r]));
    218   1.6    martin }
    219   1.6    martin 
    220   1.6    martin void
    221   1.6    martin pcfiic_write(struct pcfiic_softc *sc, bus_size_t r, u_int8_t v)
    222   1.6    martin {
    223   1.6    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_regmap[r], v);
    224  1.13       jdc 	(void)bus_space_read_1(sc->sc_iot, sc->sc_ioh, PCF8584_S1);
    225   1.6    martin }
    226   1.1       tnn 
    227   1.6    martin void
    228   1.6    martin pcfiic_choose_bus(struct pcfiic_softc *sc, u_int8_t bus)
    229   1.6    martin {
    230   1.6    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh2, 0, bus);
    231   1.6    martin 	bus_space_barrier(sc->sc_iot, sc->sc_ioh2, 0, 1,
    232   1.6    martin 	    BUS_SPACE_BARRIER_WRITE);
    233   1.1       tnn }
    234   1.1       tnn 
    235   1.6    martin int
    236  1.13       jdc pcfiic_wait_BBN(struct pcfiic_softc *sc)
    237   1.1       tnn {
    238   1.6    martin 	int		i;
    239   1.1       tnn 
    240   1.6    martin 	for (i = 0; i < 1000; i++) {
    241  1.13       jdc 		if (pcfiic_read(sc, PCF8584_S1) & PCF8584_STATUS_BBN)
    242   1.6    martin 			return (0);
    243   1.6    martin 		delay(1000);
    244   1.6    martin 	}
    245   1.6    martin 	return (1);
    246   1.1       tnn }
    247   1.1       tnn 
    248   1.6    martin int
    249   1.6    martin pcfiic_wait_pin(struct pcfiic_softc *sc, volatile u_int8_t *r)
    250   1.1       tnn {
    251   1.6    martin 	int		i;
    252   1.1       tnn 
    253   1.6    martin 	for (i = 0; i < 1000; i++) {
    254  1.13       jdc 		*r = pcfiic_read(sc, PCF8584_S1);
    255  1.13       jdc 		if ((*r & PCF8584_STATUS_PIN) == 0)
    256   1.6    martin 			return (0);
    257   1.6    martin 		delay(1000);
    258   1.6    martin 	}
    259   1.6    martin 	return (1);
    260   1.1       tnn }
    261