Annotation of margi2/i2c.c, revision 1.1

1.1     ! cvs         1: /* 
        !             2:     i2c.h
        !             3: 
        !             4:     Copyright (C) Marcus Metzler for convergence integrated media.
        !             5: 
        !             6:     This program is free software; you can redistribute it and/or modify
        !             7:     it under the terms of the GNU General Public License as published by
        !             8:     the Free Software Foundation; either version 2 of the License, or
        !             9:     (at your option) any later version.
        !            10: 
        !            11:     This program is distributed in the hope that it will be useful,
        !            12:     but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            13:     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        !            14:     GNU General Public License for more details.
        !            15: 
        !            16:     You should have received a copy of the GNU General Public License
        !            17:     along with this program; if not, write to the Free Software
        !            18:     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
        !            19: */
        !            20: 
        !            21: #define __NO_VERSION__
        !            22: 
        !            23: #include "i2c.h"
        !            24: 
        !            25: void out(struct cvdv_cards *card)
        !            26: {
        !            27:        write_indexed_register(card, IIO_GPIO_PINS,
        !            28:                               (card->scl ? SCL : 0) |
        !            29:                               (card->sda ? SDA : 0) | 1);
        !            30:        udelay(10);
        !            31: }
        !            32: 
        !            33: void clkon(struct cvdv_cards *card)
        !            34: {
        !            35:        card->scl = 1;
        !            36: }
        !            37: 
        !            38: void clkoff(struct cvdv_cards *card)
        !            39: {
        !            40:        card->scl = 0;
        !            41: }
        !            42: 
        !            43: void dat(struct cvdv_cards *card, u_char data)
        !            44: {
        !            45:        card->sda = data;
        !            46: }
        !            47: 
        !            48: int rdat(struct cvdv_cards *card)
        !            49: {
        !            50:        return ((read_indexed_register(card, IIO_GPIO_PINS) & SDA) ? 1 :
        !            51:                0);
        !            52: }
        !            53: 
        !            54: 
        !            55: void I2CStart(struct cvdv_cards *card)
        !            56: {
        !            57:        dat(card, 1);
        !            58:        out(card);
        !            59:        clkon(card);
        !            60:        out(card);
        !            61:        dat(card, 0);
        !            62:        out(card);
        !            63:        clkoff(card);
        !            64:        out(card);
        !            65: }
        !            66: 
        !            67: void I2CStop(struct cvdv_cards *card)
        !            68: {
        !            69:        dat(card, 0);
        !            70:        out(card);
        !            71:        clkon(card);
        !            72:        out(card);
        !            73:        dat(card, 1);
        !            74:        out(card);
        !            75:        clkoff(card);
        !            76:        out(card);
        !            77: }
        !            78: 
        !            79: int I2CAck(struct cvdv_cards *card, int ack)
        !            80: {
        !            81:        dat(card, ack);
        !            82:        out(card);
        !            83:        write_indexed_register(card, IIO_GPIO_CONTROL, (~SDA) & 0x07);
        !            84:        clkon(card);
        !            85:        out(card);
        !            86:        ack = rdat(card);
        !            87:        clkoff(card);
        !            88:        out(card);
        !            89:        write_indexed_register(card, IIO_GPIO_CONTROL, 0x07);
        !            90:        out(card);
        !            91:        return ack;
        !            92: }
        !            93: 
        !            94: u_char I2CReadByte(struct cvdv_cards * card, int ack)
        !            95: {
        !            96:        int i;
        !            97:        u_char data = 0;
        !            98: 
        !            99:        clkoff(card);
        !           100:        dat(card, 1);
        !           101:        out(card);
        !           102:        write_indexed_register(card, IIO_GPIO_CONTROL, (~SDA) & 0x07);
        !           103:        for (i = 7; i >= 0; i--) {
        !           104:                clkon(card);
        !           105:                out(card);
        !           106:                data |= (rdat(card) << i);
        !           107:                clkoff(card);
        !           108:                out(card);
        !           109:        }
        !           110:        write_indexed_register(card, IIO_GPIO_CONTROL, 0x07);
        !           111:        I2CAck(card, ack);
        !           112:        return data;
        !           113: }
        !           114: 
        !           115: 
        !           116: int I2CSendByte(struct cvdv_cards *card, u_char data)
        !           117: {
        !           118:        int i;
        !           119: 
        !           120:        for (i = 7; i >= 0; i--) {
        !           121:                dat(card, data & (1 << i));
        !           122:                out(card);
        !           123:                clkon(card);
        !           124:                out(card);
        !           125:                clkoff(card);
        !           126:                out(card);
        !           127:        }
        !           128:        i = I2CAck(card, 1);
        !           129:        //        if (i) 
        !           130:        //                printk("AckErr\n");
        !           131:        return i;
        !           132: }
        !           133: 
        !           134: void I2CWrite(struct cvdv_cards *card, int adr, int reg, int val)
        !           135: {
        !           136:        I2CStart(card);
        !           137:        I2CSendByte(card, adr);
        !           138:        I2CSendByte(card, reg);
        !           139:        I2CSendByte(card, val);
        !           140:        I2CStop(card);
        !           141: }
        !           142: 
        !           143: 
        !           144: u_char I2CRead(struct cvdv_cards *card, int adr, int reg)
        !           145: {
        !           146:        u_char c;
        !           147: 
        !           148:        I2CStart(card);
        !           149:        I2CSendByte(card, adr);
        !           150:        I2CSendByte(card, reg);
        !           151:        I2CStart(card);
        !           152:        I2CSendByte(card, adr | 1);
        !           153:        c = I2CReadByte(card, 1);
        !           154:        I2CStop(card);
        !           155:        return c;
        !           156: }
        !           157: 
        !           158: 
        !           159: int I2CScan(struct cvdv_cards *card, int adr)
        !           160: {
        !           161:        int result;
        !           162:        I2CStart(card);
        !           163:        result = I2CSendByte(card, adr);
        !           164:        I2CStop(card);
        !           165:        return result;
        !           166: }
        !           167: 
        !           168: void I2CScanBus(struct cvdv_cards *card)
        !           169: {
        !           170:        int i;
        !           171: 
        !           172:        for (i = 0; i < 0xff; i += 2) {
        !           173:                if (!I2CScan(card, i))
        !           174:                        printk("Found i2c device at %d\n", i);
        !           175:        }
        !           176: }
        !           177: 
        !           178: void I2CSend(struct cvdv_cards *card, int adr, u_char * vals)
        !           179: {
        !           180:        int reg, val;
        !           181:        while (*vals != 0xff) {
        !           182:                reg = *vals;
        !           183:                vals++;
        !           184:                val = *vals;
        !           185:                vals++;
        !           186:                I2CWrite(card, adr, reg, val);
        !           187:        }
        !           188: }

LinuxTV legacy CVS <linuxtv.org/cvs>