viafb: Eliminate some global.h references
[deliverable/linux.git] / drivers / video / via / via_i2c.c
index fe5535cf14806458accde85a3c0a9a299409339b..2291765f2f8efe06929d4799e6b50c7fa8bd3637 100644 (file)
  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-#include "global.h"
+#include <linux/platform_device.h>
+#include <linux/delay.h>
+#include <linux/spinlock.h>
+#include <linux/module.h>
+#include "via-core.h"
+#include "via_i2c.h"
+
+/*
+ * There can only be one set of these, so there's no point in having
+ * them be dynamically allocated...
+ */
+#define VIAFB_NUM_I2C          5
+static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
+struct viafb_dev *i2c_vdev;  /* Passed in from core */
 
 static void via_i2c_setscl(void *data, int state)
 {
        u8 val;
-       struct via_i2c_adap_cfg *adap_data = data;
+       struct via_port_cfg *adap_data = data;
+       unsigned long flags;
 
-       DEBUG_MSG(KERN_DEBUG "reading index 0x%02x from IO 0x%x\n",
-               adap_data->ioport_index, adap_data->io_port);
-       val = viafb_read_reg(adap_data->io_port,
-                            adap_data->ioport_index) & 0xF0;
+       spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
+       val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
        if (state)
                val |= 0x20;
        else
                val &= ~0x20;
        switch (adap_data->type) {
-       case VIA_I2C_I2C:
+       case VIA_PORT_I2C:
                val |= 0x01;
                break;
-       case VIA_I2C_GPIO:
+       case VIA_PORT_GPIO:
                val |= 0x80;
                break;
        default:
-               DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
+               printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
        }
-       viafb_write_reg(adap_data->ioport_index,
-                       adap_data->io_port, val);
+       via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
+       spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
 }
 
 static int via_i2c_getscl(void *data)
 {
-       struct via_i2c_adap_cfg *adap_data = data;
+       struct via_port_cfg *adap_data = data;
+       unsigned long flags;
+       int ret = 0;
 
-       if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
-               return 1;
-       return 0;
+       spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
+       if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
+               ret = 1;
+       spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
+       return ret;
 }
 
 static int via_i2c_getsda(void *data)
 {
-       struct via_i2c_adap_cfg *adap_data = data;
+       struct via_port_cfg *adap_data = data;
+       unsigned long flags;
+       int ret = 0;
 
-       if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
-               return 1;
-       return 0;
+       spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
+       if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
+               ret = 1;
+       spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
+       return ret;
 }
 
 static void via_i2c_setsda(void *data, int state)
 {
        u8 val;
-       struct via_i2c_adap_cfg *adap_data = data;
+       struct via_port_cfg *adap_data = data;
+       unsigned long flags;
 
-       val = viafb_read_reg(adap_data->io_port,
-                            adap_data->ioport_index) & 0xF0;
+       spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
+       val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
        if (state)
                val |= 0x10;
        else
                val &= ~0x10;
        switch (adap_data->type) {
-       case VIA_I2C_I2C:
+       case VIA_PORT_I2C:
                val |= 0x01;
                break;
-       case VIA_I2C_GPIO:
+       case VIA_PORT_GPIO:
                val |= 0x40;
                break;
        default:
-               DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
+               printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
        }
-       viafb_write_reg(adap_data->ioport_index,
-                       adap_data->io_port, val);
+       via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
+       spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
 }
 
 int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
@@ -96,6 +117,8 @@ int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
        u8 mm1[] = {0x00};
        struct i2c_msg msgs[2];
 
+       if (!via_i2c_par[adap].is_active)
+               return -ENODEV;
        *pdata = 0;
        msgs[0].flags = 0;
        msgs[1].flags = I2C_M_RD;
@@ -103,8 +126,7 @@ int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
        mm1[0] = index;
        msgs[0].len = 1; msgs[1].len = 1;
        msgs[0].buf = mm1; msgs[1].buf = pdata;
-       return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter,
-                       msgs, 2);
+       return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
 }
 
 int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
@@ -112,12 +134,13 @@ int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
        u8 msg[2] = { index, data };
        struct i2c_msg msgs;
 
+       if (!via_i2c_par[adap].is_active)
+               return -ENODEV;
        msgs.flags = 0;
        msgs.addr = slave_addr / 2;
        msgs.len = 2;
        msgs.buf = msg;
-       return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter,
-                       &msgs, 1);
+       return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
 }
 
 int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
@@ -125,23 +148,35 @@ int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len
        u8 mm1[] = {0x00};
        struct i2c_msg msgs[2];
 
+       if (!via_i2c_par[adap].is_active)
+               return -ENODEV;
        msgs[0].flags = 0;
        msgs[1].flags = I2C_M_RD;
        msgs[0].addr = msgs[1].addr = slave_addr / 2;
        mm1[0] = index;
        msgs[0].len = 1; msgs[1].len = buff_len;
        msgs[0].buf = mm1; msgs[1].buf = buff;
-       return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter,
-                       msgs, 2);
+       return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
+}
+
+/*
+ * Allow other viafb subdevices to look up a specific adapter
+ * by port name.
+ */
+struct i2c_adapter *viafb_find_i2c_adapter(enum viafb_i2c_adap which)
+{
+       struct via_i2c_stuff *stuff = &via_i2c_par[which];
+
+       return &stuff->adapter;
 }
+EXPORT_SYMBOL_GPL(viafb_find_i2c_adapter);
+
 
 static int create_i2c_bus(struct i2c_adapter *adapter,
                          struct i2c_algo_bit_data *algo,
-                         struct via_i2c_adap_cfg *adap_cfg,
+                         struct via_port_cfg *adap_cfg,
                          struct pci_dev *pdev)
 {
-       DEBUG_MSG(KERN_DEBUG "viafb: creating bus adap=0x%p, algo_bit_data=0x%p, adap_cfg=0x%p\n", adapter, algo, adap_cfg);
-
        algo->setsda = via_i2c_setsda;
        algo->setscl = via_i2c_setscl;
        algo->getsda = via_i2c_getsda;
@@ -170,49 +205,65 @@ static int create_i2c_bus(struct i2c_adapter *adapter,
        return i2c_bit_add_bus(adapter);
 }
 
-static struct via_i2c_adap_cfg adap_configs[] = {
-       [VIA_I2C_ADAP_26]       = { VIA_I2C_I2C,  VIASR, 0x26 },
-       [VIA_I2C_ADAP_31]       = { VIA_I2C_I2C,  VIASR, 0x31 },
-       [VIA_I2C_ADAP_25]       = { VIA_I2C_GPIO, VIASR, 0x25 },
-       [VIA_I2C_ADAP_2C]       = { VIA_I2C_GPIO, VIASR, 0x2c },
-       [VIA_I2C_ADAP_3D]       = { VIA_I2C_GPIO, VIASR, 0x3d },
-       { 0, 0, 0 }
-};
-
-int viafb_create_i2c_busses(struct viafb_par *viapar)
+static int viafb_i2c_probe(struct platform_device *platdev)
 {
        int i, ret;
+       struct via_port_cfg *configs;
 
-       for (i = 0; i < VIAFB_NUM_I2C; i++) {
-               struct via_i2c_adap_cfg *adap_cfg = &adap_configs[i];
-               struct via_i2c_stuff *i2c_stuff = &viapar->shared->i2c_stuff[i];
+       i2c_vdev = platdev->dev.platform_data;
+       configs = i2c_vdev->port_cfg;
 
-               if (adap_cfg->type == 0)
-                       break;
+       for (i = 0; i < VIAFB_NUM_PORTS; i++) {
+               struct via_port_cfg *adap_cfg = configs++;
+               struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
 
+               i2c_stuff->is_active = 0;
+               if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
+                       continue;
                ret = create_i2c_bus(&i2c_stuff->adapter,
                                     &i2c_stuff->algo, adap_cfg,
                                NULL); /* FIXME: PCIDEV */
                if (ret < 0) {
                        printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
                                i, ret);
-                       /* FIXME: properly release previous busses */
-                       return ret;
+                       continue;  /* Still try to make the rest */
                }
+               i2c_stuff->is_active = 1;
        }
 
        return 0;
 }
 
-void viafb_delete_i2c_busses(struct viafb_par *par)
+static int viafb_i2c_remove(struct platform_device *platdev)
 {
        int i;
 
-       for (i = 0; i < ARRAY_SIZE(par->shared->i2c_stuff); i++) {
-               struct via_i2c_stuff *i2c_stuff = &par->shared->i2c_stuff[i];
-               /* only remove those entries in the array that we've
-                * actually used (and thus initialized algo_data) */
-               if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
+       for (i = 0; i < VIAFB_NUM_PORTS; i++) {
+               struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
+               /*
+                * Only remove those entries in the array that we've
+                * actually used (and thus initialized algo_data)
+                */
+               if (i2c_stuff->is_active)
                        i2c_del_adapter(&i2c_stuff->adapter);
        }
+       return 0;
+}
+
+static struct platform_driver via_i2c_driver = {
+       .driver = {
+               .name = "viafb-i2c",
+       },
+       .probe = viafb_i2c_probe,
+       .remove = viafb_i2c_remove,
+};
+
+int viafb_i2c_init(void)
+{
+       return platform_driver_register(&via_i2c_driver);
+}
+
+void viafb_i2c_exit(void)
+{
+       platform_driver_unregister(&via_i2c_driver);
 }
This page took 0.044059 seconds and 5 git commands to generate.