linux/drivers/media/usb/usbvision/usbvision-i2c.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * usbvision_i2c.c
   4 *  i2c algorithm for USB-I2C Bridges
   5 *
   6 * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de>
   7 *                         Dwaine Garden <dwainegarden@rogers.com>
   8 *
   9 * This module is part of usbvision driver project.
  10 * Updates to driver completed by Dwaine P. Garden
  11 */
  12
  13
  14#include <linux/kernel.h>
  15#include <linux/module.h>
  16#include <linux/delay.h>
  17#include <linux/init.h>
  18#include <linux/uaccess.h>
  19#include <linux/ioport.h>
  20#include <linux/errno.h>
  21#include <linux/usb.h>
  22#include <linux/i2c.h>
  23#include "usbvision.h"
  24
  25#define DBG_I2C         (1 << 0)
  26
  27static int i2c_debug;
  28
  29module_param(i2c_debug, int, 0644);                     /* debug_i2c_usb mode of the device driver */
  30MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  31
  32#define PDEBUG(level, fmt, args...) { \
  33                if (i2c_debug & (level)) \
  34                        printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
  35                                __func__, __LINE__ , ## args); \
  36        }
  37
  38static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  39                            short len);
  40static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  41                           short len);
  42
  43static inline int try_write_address(struct i2c_adapter *i2c_adap,
  44                                    unsigned char addr, int retries)
  45{
  46        struct usb_usbvision *usbvision;
  47        int i, ret = -1;
  48        char buf[4];
  49
  50        usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  51        buf[0] = 0x00;
  52        for (i = 0; i <= retries; i++) {
  53                ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
  54                if (ret == 1)
  55                        break;  /* success! */
  56                udelay(5);
  57                if (i == retries)       /* no success */
  58                        break;
  59                udelay(10);
  60        }
  61        if (i) {
  62                PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr);
  63                PDEBUG(DBG_I2C, "Maybe there's no device at this address");
  64        }
  65        return ret;
  66}
  67
  68static inline int try_read_address(struct i2c_adapter *i2c_adap,
  69                                   unsigned char addr, int retries)
  70{
  71        struct usb_usbvision *usbvision;
  72        int i, ret = -1;
  73        char buf[4];
  74
  75        usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  76        for (i = 0; i <= retries; i++) {
  77                ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
  78                if (ret == 1)
  79                        break;  /* success! */
  80                udelay(5);
  81                if (i == retries)       /* no success */
  82                        break;
  83                udelay(10);
  84        }
  85        if (i) {
  86                PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr);
  87                PDEBUG(DBG_I2C, "Maybe there's no device at this address");
  88        }
  89        return ret;
  90}
  91
  92static inline int usb_find_address(struct i2c_adapter *i2c_adap,
  93                                   struct i2c_msg *msg, int retries,
  94                                   unsigned char *add)
  95{
  96        unsigned short flags = msg->flags;
  97
  98        unsigned char addr;
  99        int ret;
 100
 101        addr = (msg->addr << 1);
 102        if (flags & I2C_M_RD)
 103                addr |= 1;
 104
 105        add[0] = addr;
 106        if (flags & I2C_M_RD)
 107                ret = try_read_address(i2c_adap, addr, retries);
 108        else
 109                ret = try_write_address(i2c_adap, addr, retries);
 110
 111        if (ret != 1)
 112                return -EREMOTEIO;
 113
 114        return 0;
 115}
 116
 117static int
 118usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
 119{
 120        struct i2c_msg *pmsg;
 121        struct usb_usbvision *usbvision;
 122        int i, ret;
 123        unsigned char addr = 0;
 124
 125        usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
 126
 127        for (i = 0; i < num; i++) {
 128                pmsg = &msgs[i];
 129                ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
 130                if (ret != 0) {
 131                        PDEBUG(DBG_I2C, "got NAK from device, message #%d", i);
 132                        return (ret < 0) ? ret : -EREMOTEIO;
 133                }
 134
 135                if (pmsg->flags & I2C_M_RD) {
 136                        /* read bytes into buffer */
 137                        ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
 138                        if (ret < pmsg->len)
 139                                return (ret < 0) ? ret : -EREMOTEIO;
 140                } else {
 141                        /* write bytes from buffer */
 142                        ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
 143                        if (ret < pmsg->len)
 144                                return (ret < 0) ? ret : -EREMOTEIO;
 145                }
 146        }
 147        return num;
 148}
 149
 150static u32 functionality(struct i2c_adapter *adap)
 151{
 152        return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
 153}
 154
 155/* -----exported algorithm data: -------------------------------------  */
 156
 157static const struct i2c_algorithm usbvision_algo = {
 158        .master_xfer   = usbvision_i2c_xfer,
 159        .smbus_xfer    = NULL,
 160        .functionality = functionality,
 161};
 162
 163
 164/* ----------------------------------------------------------------------- */
 165/* usbvision specific I2C functions                                        */
 166/* ----------------------------------------------------------------------- */
 167static const struct i2c_adapter i2c_adap_template;
 168
 169int usbvision_i2c_register(struct usb_usbvision *usbvision)
 170{
 171        static unsigned short saa711x_addrs[] = {
 172                0x4a >> 1, 0x48 >> 1,   /* SAA7111, SAA7111A and SAA7113 */
 173                0x42 >> 1, 0x40 >> 1,   /* SAA7114, SAA7115 and SAA7118 */
 174                I2C_CLIENT_END };
 175
 176        if (usbvision->registered_i2c)
 177                return 0;
 178
 179        usbvision->i2c_adap = i2c_adap_template;
 180
 181        snprintf(usbvision->i2c_adap.name, sizeof(usbvision->i2c_adap.name),
 182                 "usbvision-%d-%s",
 183                 usbvision->dev->bus->busnum, usbvision->dev->devpath);
 184        PDEBUG(DBG_I2C, "Adaptername: %s", usbvision->i2c_adap.name);
 185        usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
 186
 187        i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev);
 188
 189        if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
 190                printk(KERN_ERR "usbvision_i2c_register: can't write reg\n");
 191                return -EBUSY;
 192        }
 193
 194        PDEBUG(DBG_I2C, "I2C   debugging is enabled [i2c]");
 195        PDEBUG(DBG_I2C, "ALGO   debugging is enabled [i2c]");
 196
 197        /* register new adapter to i2c module... */
 198
 199        usbvision->i2c_adap.algo = &usbvision_algo;
 200
 201        usbvision->i2c_adap.timeout = 100;      /* default values, should       */
 202        usbvision->i2c_adap.retries = 3;        /* be replaced by defines       */
 203
 204        i2c_add_adapter(&usbvision->i2c_adap);
 205
 206        PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name);
 207
 208        /* Request the load of the i2c modules we need */
 209        switch (usbvision_device_data[usbvision->dev_model].codec) {
 210        case CODEC_SAA7113:
 211        case CODEC_SAA7111:
 212                /* Without this delay the detection of the saa711x is
 213                   hit-and-miss. */
 214                mdelay(10);
 215                v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
 216                                &usbvision->i2c_adap,
 217                                "saa7115_auto", 0, saa711x_addrs);
 218                break;
 219        }
 220        if (usbvision_device_data[usbvision->dev_model].tuner == 1) {
 221                struct v4l2_subdev *sd;
 222                enum v4l2_i2c_tuner_type type;
 223                struct tuner_setup tun_setup;
 224
 225                sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
 226                                &usbvision->i2c_adap,
 227                                "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
 228                /* depending on whether we found a demod or not, select
 229                   the tuner type. */
 230                type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
 231
 232                sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
 233                                &usbvision->i2c_adap,
 234                                "tuner", 0, v4l2_i2c_tuner_addrs(type));
 235
 236                if (sd == NULL)
 237                        return -ENODEV;
 238                if (usbvision->tuner_type != -1) {
 239                        tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
 240                        tun_setup.type = usbvision->tuner_type;
 241                        tun_setup.addr = v4l2_i2c_subdev_addr(sd);
 242                        call_all(usbvision, tuner, s_type_addr, &tun_setup);
 243                }
 244        }
 245        usbvision->registered_i2c = 1;
 246
 247        return 0;
 248}
 249
 250int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
 251{
 252        if (!usbvision->registered_i2c)
 253                return 0;
 254
 255        i2c_del_adapter(&(usbvision->i2c_adap));
 256        usbvision->registered_i2c = 0;
 257
 258        PDEBUG(DBG_I2C, "i2c bus for %s unregistered", usbvision->i2c_adap.name);
 259
 260        return 0;
 261}
 262
 263static int
 264usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
 265                     char *buf, short len)
 266{
 267        int rc, retries;
 268
 269        for (retries = 5;;) {
 270                rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
 271                if (rc < 0)
 272                        return rc;
 273
 274                /* Initiate byte read cycle                    */
 275                /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
 276                /*                    d3 0=Wr 1=Rd             */
 277                rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
 278                                      (len & 0x07) | 0x18);
 279                if (rc < 0)
 280                        return rc;
 281
 282                /* Test for Busy and ACK */
 283                do {
 284                        /* USBVISION_SER_CONT -> d4 == 0 busy */
 285                        rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
 286                } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
 287                if (rc < 0)
 288                        return rc;
 289
 290                /* USBVISION_SER_CONT -> d5 == 1 Not ack */
 291                if ((rc & 0x20) == 0)   /* Ack? */
 292                        break;
 293
 294                /* I2C abort */
 295                rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
 296                if (rc < 0)
 297                        return rc;
 298
 299                if (--retries < 0)
 300                        return -1;
 301        }
 302
 303        switch (len) {
 304        case 4:
 305                buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
 306                /* fall through */
 307        case 3:
 308                buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
 309                /* fall through */
 310        case 2:
 311                buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
 312                /* fall through */
 313        case 1:
 314                buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
 315                break;
 316        default:
 317                printk(KERN_ERR
 318                       "usbvision_i2c_read_max4: buffer length > 4\n");
 319        }
 320
 321        if (i2c_debug & DBG_I2C) {
 322                int idx;
 323
 324                for (idx = 0; idx < len; idx++)
 325                        PDEBUG(DBG_I2C, "read %x from address %x", (unsigned char)buf[idx], addr);
 326        }
 327        return len;
 328}
 329
 330
 331static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
 332                                 unsigned char addr, const char *buf,
 333                                 short len)
 334{
 335        int rc, retries;
 336        int i;
 337        unsigned char *value = usbvision->ctrl_urb_buffer;
 338        unsigned char ser_cont;
 339
 340        ser_cont = (len & 0x07) | 0x10;
 341
 342        value[0] = addr;
 343        value[1] = ser_cont;
 344        for (i = 0; i < len; i++)
 345                value[i + 2] = buf[i];
 346
 347        for (retries = 5;;) {
 348                rc = usb_control_msg(usbvision->dev,
 349                                     usb_sndctrlpipe(usbvision->dev, 1),
 350                                     USBVISION_OP_CODE,
 351                                     USB_DIR_OUT | USB_TYPE_VENDOR |
 352                                     USB_RECIP_ENDPOINT, 0,
 353                                     (__u16) USBVISION_SER_ADRS, value,
 354                                     len + 2, HZ);
 355
 356                if (rc < 0)
 357                        return rc;
 358
 359                rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
 360                                      (len & 0x07) | 0x10);
 361                if (rc < 0)
 362                        return rc;
 363
 364                /* Test for Busy and ACK */
 365                do {
 366                        rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
 367                } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
 368                if (rc < 0)
 369                        return rc;
 370
 371                if ((rc & 0x20) == 0)   /* Ack? */
 372                        break;
 373
 374                /* I2C abort */
 375                usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
 376
 377                if (--retries < 0)
 378                        return -1;
 379
 380        }
 381
 382        if (i2c_debug & DBG_I2C) {
 383                int idx;
 384
 385                for (idx = 0; idx < len; idx++)
 386                        PDEBUG(DBG_I2C, "wrote %x at address %x", (unsigned char)buf[idx], addr);
 387        }
 388        return len;
 389}
 390
 391static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
 392                            short len)
 393{
 394        char *buf_ptr = buf;
 395        int retval;
 396        int wrcount = 0;
 397        int count;
 398        int max_len = 4;
 399
 400        while (len > 0) {
 401                count = (len > max_len) ? max_len : len;
 402                retval = usbvision_i2c_write_max4(usbvision, addr, buf_ptr, count);
 403                if (retval > 0) {
 404                        len -= count;
 405                        buf_ptr += count;
 406                        wrcount += count;
 407                } else
 408                        return (retval < 0) ? retval : -EFAULT;
 409        }
 410        return wrcount;
 411}
 412
 413static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
 414                           short len)
 415{
 416        char temp[4];
 417        int retval, i;
 418        int rdcount = 0;
 419        int count;
 420
 421        while (len > 0) {
 422                count = (len > 3) ? 4 : len;
 423                retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
 424                if (retval > 0) {
 425                        for (i = 0; i < len; i++)
 426                                buf[rdcount + i] = temp[i];
 427                        len -= count;
 428                        rdcount += count;
 429                } else
 430                        return (retval < 0) ? retval : -EFAULT;
 431        }
 432        return rdcount;
 433}
 434
 435static const struct i2c_adapter i2c_adap_template = {
 436        .owner = THIS_MODULE,
 437        .name              = "usbvision",
 438};
 439