2 * drivers/i2c/busses/i2c-ixp2000.c
4 * I2C adapter for IXP2000 systems using GPIOs for I2C bus
6 * Author: Deepak Saxena <dsaxena@plexity.net>
7 * Based on IXDP2400 code by: Naeem M. Afzal <naeem.m.afzal@intel.com>
8 * Made generic by: Jeff Daly <jeffrey.daly@intel.com>
10 * Copyright (c) 2003-2004 MontaVista Software Inc.
12 * This file is licensed under the terms of the GNU General Public
13 * License version 2. This program is licensed "as is" without any
14 * warranty of any kind, whether express or implied.
18 * I2C adapter driver for Intel IXDP2xxx platforms. This should work for any
19 * IXP2000 platform if it uses the HW GPIO in the same manner. Basically,
20 * SDA and SCL GPIOs have external pullups. Setting the respective GPIO to
21 * an input will make the signal a '1' via the pullup. Setting them to
22 * outputs will pull them down.
24 * The GPIOs are open drain signals and are used as configuration strap inputs
25 * during power-up so there's generally a buffer on the board that needs to be
26 * 'enabled' to drive the GPIOs.
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/platform_device.h>
32 #include <linux/module.h>
33 #include <linux/i2c.h>
34 #include <linux/i2c-algo-bit.h>
36 #include <asm/hardware.h> /* Pick up IXP2000-specific bits */
37 #include <asm/arch/gpio.h>
39 static struct device_driver ixp2000_i2c_driver;
41 static inline int ixp2000_scl_pin(void *data)
43 return ((struct ixp2000_i2c_pins*)data)->scl_pin;
46 static inline int ixp2000_sda_pin(void *data)
48 return ((struct ixp2000_i2c_pins*)data)->sda_pin;
52 static void ixp2000_bit_setscl(void *data, int val)
57 gpio_line_config(ixp2000_scl_pin(data), GPIO_IN);
58 while(!gpio_line_get(ixp2000_scl_pin(data)) && i--);
60 gpio_line_config(ixp2000_scl_pin(data), GPIO_OUT);
64 static void ixp2000_bit_setsda(void *data, int val)
67 gpio_line_config(ixp2000_sda_pin(data), GPIO_IN);
69 gpio_line_config(ixp2000_sda_pin(data), GPIO_OUT);
73 static int ixp2000_bit_getscl(void *data)
75 return gpio_line_get(ixp2000_scl_pin(data));
78 static int ixp2000_bit_getsda(void *data)
80 return gpio_line_get(ixp2000_sda_pin(data));
83 struct ixp2000_i2c_data {
84 struct ixp2000_i2c_pins *gpio_pins;
85 struct i2c_adapter adapter;
86 struct i2c_algo_bit_data algo_data;
89 static int ixp2000_i2c_remove(struct device *dev)
91 struct platform_device *plat_dev = to_platform_device(dev);
92 struct ixp2000_i2c_data *drv_data = dev_get_drvdata(&plat_dev->dev);
94 dev_set_drvdata(&plat_dev->dev, NULL);
96 i2c_bit_del_bus(&drv_data->adapter);
103 static int ixp2000_i2c_probe(struct device *dev)
106 struct platform_device *plat_dev = to_platform_device(dev);
107 struct ixp2000_i2c_pins *gpio = plat_dev->dev.platform_data;
108 struct ixp2000_i2c_data *drv_data =
109 kzalloc(sizeof(struct ixp2000_i2c_data), GFP_KERNEL);
113 drv_data->gpio_pins = gpio;
115 drv_data->algo_data.data = gpio;
116 drv_data->algo_data.setsda = ixp2000_bit_setsda;
117 drv_data->algo_data.setscl = ixp2000_bit_setscl;
118 drv_data->algo_data.getsda = ixp2000_bit_getsda;
119 drv_data->algo_data.getscl = ixp2000_bit_getscl;
120 drv_data->algo_data.udelay = 6;
121 drv_data->algo_data.mdelay = 6;
122 drv_data->algo_data.timeout = 100;
124 drv_data->adapter.id = I2C_HW_B_IXP2000,
125 strlcpy(drv_data->adapter.name, ixp2000_i2c_driver.name,
127 drv_data->adapter.algo_data = &drv_data->algo_data,
129 drv_data->adapter.dev.parent = &plat_dev->dev;
131 gpio_line_config(gpio->sda_pin, GPIO_IN);
132 gpio_line_config(gpio->scl_pin, GPIO_IN);
133 gpio_line_set(gpio->scl_pin, 0);
134 gpio_line_set(gpio->sda_pin, 0);
136 if ((err = i2c_bit_add_bus(&drv_data->adapter)) != 0) {
137 dev_err(dev, "Could not install, error %d\n", err);
142 dev_set_drvdata(&plat_dev->dev, drv_data);
147 static struct device_driver ixp2000_i2c_driver = {
148 .owner = THIS_MODULE,
149 .name = "IXP2000-I2C",
150 .bus = &platform_bus_type,
151 .probe = ixp2000_i2c_probe,
152 .remove = ixp2000_i2c_remove,
155 static int __init ixp2000_i2c_init(void)
157 return driver_register(&ixp2000_i2c_driver);
160 static void __exit ixp2000_i2c_exit(void)
162 driver_unregister(&ixp2000_i2c_driver);
165 module_init(ixp2000_i2c_init);
166 module_exit(ixp2000_i2c_exit);
168 MODULE_AUTHOR ("Deepak Saxena <dsaxena@plexity.net>");
169 MODULE_DESCRIPTION("IXP2000 GPIO-based I2C bus driver");
170 MODULE_LICENSE("GPL");