MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / nios2nommu / drivers / i2c-gpio.c
blob4b88753b3e9126788cf70441c1a78818d4728166
1 /*
2 * drivers/i2c/busses/i2c-gpio.c for Nios2
4 * drivers/i2c/busses/i2c-ixp2000.c
6 * I2C adapter for IXP2000 systems using GPIOs for I2C bus
8 * Author: Deepak Saxena <dsaxena@plexity.net>
9 * Based on IXDP2400 code by: Naeem M. Afzal <naeem.m.afzal@intel.com>
10 * Made generic by: Jeff Daly <jeffrey.daly@intel.com>
12 * Copyright (c) 2003-2004 MontaVista Software Inc.
14 * This file is licensed under the terms of the GNU General Public
15 * License version 2. This program is licensed "as is" without any
16 * warranty of any kind, whether express or implied.
18 * From Jeff Daly:
20 * I2C adapter driver for Intel IXDP2xxx platforms. This should work for any
21 * IXP2000 platform if it uses the HW GPIO in the same manner. Basically,
22 * SDA and SCL GPIOs have external pullups. Setting the respective GPIO to
23 * an input will make the signal a '1' via the pullup. Setting them to
24 * outputs will pull them down.
26 * The GPIOs are open drain signals and are used as configuration strap inputs
27 * during power-up so there's generally a buffer on the board that needs to be
28 * 'enabled' to drive the GPIOs.
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/platform_device.h>
34 #include <linux/module.h>
35 #include <linux/i2c.h>
36 #include <linux/i2c-algo-bit.h>
37 #include <linux/i2c-id.h>
39 #include <asm/io.h>
40 #include <asm/gpio.h>
42 static inline int gpio_scl_pin(void *data)
44 return ((struct gpio_i2c_pins*)data)->scl_pin;
47 static inline int gpio_sda_pin(void *data)
49 return ((struct gpio_i2c_pins*)data)->sda_pin;
53 static void gpio_bit_setscl(void *data, int val)
55 int i = 5000;
57 if (val) {
58 outl(3,gpio_scl_pin(data));
59 while(!(inl(gpio_scl_pin(data)) & 1) && i--);
60 } else {
61 outl(2,gpio_scl_pin(data));
65 static void gpio_bit_setsda(void *data, int val)
67 if (val) {
68 outl(1,gpio_sda_pin(data));
69 } else {
70 outl(0,gpio_sda_pin(data));
74 static int gpio_bit_getscl(void *data)
76 return inl(gpio_scl_pin(data)) & 1;
79 static int gpio_bit_getsda(void *data)
81 return inl(gpio_sda_pin(data)) & 1;
84 struct gpio_i2c_data {
85 struct gpio_i2c_pins *gpio_pins;
86 struct i2c_adapter adapter;
87 struct i2c_algo_bit_data algo_data;
90 static int gpio_i2c_remove(struct platform_device *plat_dev)
92 struct gpio_i2c_data *drv_data = platform_get_drvdata(plat_dev);
94 platform_set_drvdata(plat_dev, NULL);
96 i2c_bit_del_bus(&drv_data->adapter);
98 kfree(drv_data);
100 return 0;
103 static int gpio_i2c_probe(struct platform_device *plat_dev)
105 int err;
106 struct gpio_i2c_pins *gpio = plat_dev->dev.platform_data;
107 struct gpio_i2c_data *drv_data =
108 kzalloc(sizeof(struct gpio_i2c_data), GFP_KERNEL);
110 if (!drv_data)
111 return -ENOMEM;
112 drv_data->gpio_pins = gpio;
114 drv_data->algo_data.data = gpio;
115 drv_data->algo_data.setsda = gpio_bit_setsda;
116 drv_data->algo_data.setscl = gpio_bit_setscl;
117 drv_data->algo_data.getsda = gpio_bit_getsda;
118 drv_data->algo_data.getscl = gpio_bit_getscl;
119 drv_data->algo_data.udelay = 6;
120 drv_data->algo_data.timeout = 100;
122 drv_data->adapter.id = I2C_HW_B_IXP2000, // borrowed,
123 strlcpy(drv_data->adapter.name, plat_dev->dev.driver->name,
124 I2C_NAME_SIZE);
125 drv_data->adapter.algo_data = &drv_data->algo_data,
127 drv_data->adapter.dev.parent = &plat_dev->dev;
128 drv_data->adapter.class = I2C_CLASS_ALL;
130 outl(1,gpio->sda_pin);
131 outl(1,gpio->scl_pin);
133 if ((err = i2c_bit_add_bus(&drv_data->adapter)) != 0) {
134 dev_err(&plat_dev->dev, "Could not install, error %d\n", err);
135 kfree(drv_data);
136 return err;
139 platform_set_drvdata(plat_dev, drv_data);
140 printk("i2c-gpio driver at %08x\n",gpio->sda_pin);
142 return 0;
145 static struct platform_driver gpio_i2c_driver = {
146 .probe = gpio_i2c_probe,
147 .remove = gpio_i2c_remove,
148 .driver = {
149 .name = "GPIO-I2C",
150 .owner = THIS_MODULE,
154 static int __init gpio_i2c_init(void)
156 return platform_driver_register(&gpio_i2c_driver);
159 static void __exit gpio_i2c_exit(void)
161 platform_driver_unregister(&gpio_i2c_driver);
164 module_init(gpio_i2c_init);
165 module_exit(gpio_i2c_exit);