Subversion Repositories Kolibri OS

Rev

Rev 1125 | Rev 4104 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* i2c-core.c - a device driver for the iic-bus interface                    */
  2. /* ------------------------------------------------------------------------- */
  3. /*   Copyright (C) 1995-99 Simon G. Vogl
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  18. /* ------------------------------------------------------------------------- */
  19.  
  20. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
  21.    All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
  22.    SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
  23.    Jean Delvare <khali@linux-fr.org> */
  24.  
  25. #include <types.h>
  26. #include <list.h>
  27. #include <errno.h>
  28. #include <linux/i2c.h>
  29. #include <syscall.h>
  30.  
  31.  
  32. /**
  33.  * i2c_transfer - execute a single or combined I2C message
  34.  * @adap: Handle to I2C bus
  35.  * @msgs: One or more messages to execute before STOP is issued to
  36.  *  terminate the operation; each message begins with a START.
  37.  * @num: Number of messages to be executed.
  38.  *
  39.  * Returns negative errno, else the number of messages executed.
  40.  *
  41.  * Note that there is no requirement that each message be sent to
  42.  * the same slave address, although that is the most common model.
  43.  */
  44. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  45. {
  46.     unsigned long orig_jiffies;
  47.     int ret, try;
  48.  
  49.     /* REVISIT the fault reporting model here is weak:
  50.      *
  51.      *  - When we get an error after receiving N bytes from a slave,
  52.      *    there is no way to report "N".
  53.      *
  54.      *  - When we get a NAK after transmitting N bytes to a slave,
  55.      *    there is no way to report "N" ... or to let the master
  56.      *    continue executing the rest of this combined message, if
  57.      *    that's the appropriate response.
  58.      *
  59.      *  - When for example "num" is two and we successfully complete
  60.      *    the first message but get an error part way through the
  61.      *    second, it's unclear whether that should be reported as
  62.      *    one (discarding status on the second message) or errno
  63.      *    (discarding status on the first one).
  64.      */
  65.  
  66.     if (adap->algo->master_xfer) {
  67.  
  68.  
  69.         /* Retry automatically on arbitration loss */
  70.         orig_jiffies = 0;
  71.         for (ret = 0, try = 0; try <= adap->retries; try++) {
  72.  
  73.             ret = adap->algo->master_xfer(adap, msgs, num);
  74.             if (ret != -EAGAIN)
  75.                 break;
  76. //            if (time_after(jiffies, orig_jiffies + adap->timeout))
  77. //                break;
  78.                 delay(1);
  79.         }
  80. //        mutex_unlock(&adap->bus_lock);
  81.         return ret;
  82.     } else {
  83. //        dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  84.         return -EOPNOTSUPP;
  85.     }
  86. }
  87. EXPORT_SYMBOL(i2c_transfer);
  88.  
  89.  
  90. /**
  91.  * i2c_new_device - instantiate an i2c device
  92.  * @adap: the adapter managing the device
  93.  * @info: describes one I2C device; bus_num is ignored
  94.  * Context: can sleep
  95.  *
  96.  * Create an i2c device. Binding is handled through driver model
  97.  * probe()/remove() methods.  A driver may be bound to this device when we
  98.  * return from this function, or any later moment (e.g. maybe hotplugging will
  99.  * load the driver module).  This call is not appropriate for use by mainboard
  100.  * initialization logic, which usually runs during an arch_initcall() long
  101.  * before any i2c_adapter could exist.
  102.  *
  103.  * This returns the new i2c client, which may be saved for later use with
  104.  * i2c_unregister_device(); or NULL to indicate an error.
  105.  */
  106. struct i2c_client *
  107. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  108. {
  109.     struct i2c_client   *client;
  110.     int         status;
  111.  
  112.     client = kzalloc(sizeof *client, GFP_KERNEL);
  113.     if (!client)
  114.         return NULL;
  115.  
  116.     client->adapter = adap;
  117.  
  118.     client->flags = info->flags;
  119.     client->addr = info->addr;
  120.     client->irq = info->irq;
  121.  
  122.     strlcpy(client->name, info->type, sizeof(client->name));
  123.  
  124.     return client;
  125. }
  126.  
  127.  
  128.  
  129.