^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1995-1997 Simon G. Vogl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * 1998-2000 Hans Berglund
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Frodo Looijaard <frodol@dds.nl>, and also from Martin Bailey
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * <mbailey@littlefeet-inc.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * messages, proper stop/repstart signaling during receive, added detect code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/i2c-algo-pcf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "i2c-algo-pcf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define DEB2(x) if (i2c_debug >= 2) x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define DEB3(x) if (i2c_debug >= 3) x /* print several statistical values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define DEBPROTO(x) if (i2c_debug >= 9) x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* debug the protocol by showing transferred bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define DEF_TIMEOUT 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * module parameters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int i2c_debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* setting states on the bus with the right timing: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define get_own(adap) adap->getown(adap->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define get_clock(adap) adap->getclock(adap->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define i2c_inb(adap) adap->getpcf(adap->data, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* other auxiliary functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static void i2c_start(struct i2c_algo_pcf_data *adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) DEBPROTO(printk(KERN_DEBUG "S "));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) set_pcf(adap, 1, I2C_PCF_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void i2c_repstart(struct i2c_algo_pcf_data *adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) DEBPROTO(printk(" Sr "));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) set_pcf(adap, 1, I2C_PCF_REPSTART);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void i2c_stop(struct i2c_algo_pcf_data *adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) DEBPROTO(printk("P\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) set_pcf(adap, 1, I2C_PCF_STOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) DEB2(printk(KERN_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * Cleanup from LAB -- reset and enable ESO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * This resets the PCF8584; since we've lost the bus, no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * further attempts should be made by callers to clean up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * (no i2c_stop() etc.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) set_pcf(adap, 1, I2C_PCF_PIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) set_pcf(adap, 1, I2C_PCF_ESO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * We pause for a time period sufficient for any running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * I2C transaction to complete -- the arbitration logic won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * work properly until the next START is seen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * It is assumed the bus driver or client has set a proper value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * REVISIT: should probably use msleep instead of mdelay if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * know we can sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (adap->lab_mdelay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) mdelay(adap->lab_mdelay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) DEB2(printk(KERN_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) get_pcf(adap, 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static int wait_for_bb(struct i2c_algo_pcf_data *adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int timeout = DEF_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) status = get_pcf(adap, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) while (!(status & I2C_PCF_BB) && --timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) udelay(100); /* wait for 100 us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) status = get_pcf(adap, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (timeout == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) printk(KERN_ERR "Timeout waiting for Bus Busy\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int timeout = DEF_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *status = get_pcf(adap, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) while ((*status & I2C_PCF_PIN) && --timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) adap->waitforpin(adap->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *status = get_pcf(adap, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (*status & I2C_PCF_LAB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) handle_lab(adap, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (timeout == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * This should perform the 'PCF8584 initialization sequence' as described
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * in the Philips IC12 data book (1995, Aug 29).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * There should be a 30 clock cycle wait after reset, I assume this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * has been fulfilled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * There should be a delay at the end equal to the longest I2C message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * to synchronize the BB-bit (in multimaster systems). How long is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * this? I assume 1 second is always long enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * vdovikin: added detect code for PCF8584
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) unsigned char temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) get_pcf(adap, 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* S1=0x80: S0 selected, serial interface off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) set_pcf(adap, 1, I2C_PCF_PIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * check to see S1 now used as R/W ctrl -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * PCF8584 does that when ESO is zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return -ENXIO; /* definitely not PCF8584 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* load own address in S0, effective address is (own << 1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) i2c_outb(adap, get_own(adap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* check it's really written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if ((temp = i2c_inb(adap)) != get_own(adap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /* S1=0xA0, next byte in S2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* check to see S2 now selected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* load clock register S2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) i2c_outb(adap, get_clock(adap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /* check it's really written, the only 5 lowest bits does matter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* Enable serial interface, idle, S0 selected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) set_pcf(adap, 1, I2C_PCF_IDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* check to see PCF is really idled and we can access status register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int count, int last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int wrcount, status, timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) for (wrcount=0; wrcount<count; ++wrcount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) buf[wrcount] & 0xff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) i2c_outb(adap, buf[wrcount]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) timeout = wait_for_pin(adap, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (timeout == -EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return -EINTR; /* arbitration lost */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) i2c_stop(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return -EREMOTEIO; /* got a better one ?? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (status & I2C_PCF_LRB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) i2c_stop(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return -EREMOTEIO; /* got a better one ?? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) i2c_stop(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) i2c_repstart(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return wrcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int count, int last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int i, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int wfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) /* increment number of bytes to read by one -- read dummy byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) for (i = 0; i <= count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if ((wfp = wait_for_pin(adap, &status))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (wfp == -EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return -EINTR; /* arbitration lost */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) i2c_stop(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if ((status & I2C_PCF_LRB) && (i != count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) i2c_stop(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (i == count - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) set_pcf(adap, 1, I2C_PCF_ESO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) } else if (i == count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) i2c_stop(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) i2c_repstart(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) buf[i - 1] = i2c_inb(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) i2c_inb(adap); /* dummy read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int pcf_doAddress(struct i2c_algo_pcf_data *adap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct i2c_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) unsigned char addr = i2c_8bit_addr_from_msg(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (msg->flags & I2C_M_REV_DIR_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) addr ^= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) i2c_outb(adap, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int pcf_xfer(struct i2c_adapter *i2c_adap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct i2c_msg *msgs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct i2c_msg *pmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int ret=0, timeout, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (adap->xfer_begin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) adap->xfer_begin(adap->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* Check for bus busy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) timeout = wait_for_bb(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) "Timeout waiting for BB in pcf_xfer\n");)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) i = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) for (i = 0;ret >= 0 && i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) pmsg = &msgs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) pmsg->flags & I2C_M_RD ? "read" : "write",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) pmsg->len, pmsg->addr, i + 1, num);)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ret = pcf_doAddress(adap, pmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /* Send START */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) i2c_start(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /* Wait for PIN (pending interrupt NOT) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) timeout = wait_for_pin(adap, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (timeout == -EINTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* arbitration lost */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) i = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) i2c_stop(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) "for PIN(1) in pcf_xfer\n");)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) i = -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /* Check LRB (last rcvd bit - slave ack) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (status & I2C_PCF_LRB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) i2c_stop(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) i = -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (pmsg->flags & I2C_M_RD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) (i + 1 == num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (ret != pmsg->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) "only read %d bytes.\n",ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) (i + 1 == num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (ret != pmsg->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) "only wrote %d bytes.\n",ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (adap->xfer_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) adap->xfer_end(adap->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static u32 pcf_func(struct i2c_adapter *adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) I2C_FUNC_PROTOCOL_MANGLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /* exported algorithm data: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static const struct i2c_algorithm pcf_algo = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .master_xfer = pcf_xfer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) .functionality = pcf_func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * registering functions to load algorithms at runtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int i2c_pcf_add_bus(struct i2c_adapter *adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /* register new adapter to i2c module... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) adap->algo = &pcf_algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if ((rval = pcf_init_8584(pcf_adap)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) rval = i2c_add_adapter(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) EXPORT_SYMBOL(i2c_pcf_add_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) MODULE_PARM_DESC(i2c_debug,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");