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] / drivers / i2c / busses / i2c-ixp4xx.c
blob4d503e26b8d4a72815b6273233dc190fa96a90f5
1 /*
2 * drivers/i2c/busses/i2c-ixp4xx.c
4 * Intel's IXP42x XScale NPU chipsets (IXP420, 421, 422, 425) do not have
5 * an on board I2C controller but provide 16 GPIO pins that are often
6 * used to create an I2C bus. This driver provides an i2c_adapter
7 * interface that plugs in under algo_bit and drives the GPIO pins
8 * as instructed by the alogorithm driver.
10 * Author: Deepak Saxena <dsaxena@plexity.net>
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 * NOTE: Since different platforms will use different GPIO pins for
19 * I2C, this driver uses an IXP42x-specific platform_data
20 * pointer to pass the GPIO numbers to the driver. This
21 * allows us to support all the different IXP42x platforms
22 * w/o having to put #ifdefs in this driver.
24 * See arch/arm/mach-ixp42x/ixdp425.c for an example of building a
25 * device list and filling in the ixp4xx_i2c_pins data structure
26 * that is passed as the platform_data to this driver.
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>
35 #include <linux/i2c-id.h>
37 #include <asm/hardware.h> /* Pick up IXP42x-specific bits */
39 static inline int ixp42x_scl_pin(void *data)
41 return ((struct ixp4xx_i2c_pins*)data)->scl_pin;
44 static inline int ixp42x_sda_pin(void *data)
46 return ((struct ixp4xx_i2c_pins*)data)->sda_pin;
49 static void ixp42x_bit_setscl(void *data, int val)
51 gpio_line_set(ixp42x_scl_pin(data), 0);
52 gpio_line_config(ixp42x_scl_pin(data),
53 val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
56 static void ixp42x_bit_setsda(void *data, int val)
58 gpio_line_set(ixp42x_sda_pin(data), 0);
59 gpio_line_config(ixp42x_sda_pin(data),
60 val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
63 static int ixp42x_bit_getscl(void *data)
65 int scl;
67 gpio_line_config(ixp42x_scl_pin(data), IXP4XX_GPIO_IN );
68 gpio_line_get(ixp42x_scl_pin(data), &scl);
70 return scl;
73 static int ixp42x_bit_getsda(void *data)
75 int sda;
77 gpio_line_config(ixp42x_sda_pin(data), IXP4XX_GPIO_IN );
78 gpio_line_get(ixp42x_sda_pin(data), &sda);
80 return sda;
83 struct ixp4xx_i2c_data {
84 struct ixp4xx_i2c_pins *gpio_pins;
85 struct i2c_adapter adapter;
86 struct i2c_algo_bit_data algo_data;
89 static int ixp4xx_i2c_remove(struct platform_device *plat_dev)
91 struct ixp4xx_i2c_data *drv_data = platform_get_drvdata(plat_dev);
93 platform_set_drvdata(plat_dev, NULL);
95 i2c_bit_del_bus(&drv_data->adapter);
97 kfree(drv_data);
99 return 0;
102 static int ixp4xx_i2c_probe(struct platform_device *plat_dev)
104 int err;
105 struct ixp4xx_i2c_pins *gpio = plat_dev->dev.platform_data;
106 struct ixp4xx_i2c_data *drv_data =
107 kzalloc(sizeof(struct ixp4xx_i2c_data), GFP_KERNEL);
109 if(!drv_data)
110 return -ENOMEM;
112 drv_data->gpio_pins = gpio;
115 * We could make a lot of these structures static, but
116 * certain platforms may have multiple GPIO-based I2C
117 * buses for various device domains, so we need per-device
118 * algo_data->data.
120 drv_data->algo_data.data = gpio;
121 drv_data->algo_data.setsda = ixp42x_bit_setsda;
122 drv_data->algo_data.setscl = ixp42x_bit_setscl;
123 drv_data->algo_data.getsda = ixp42x_bit_getsda;
124 drv_data->algo_data.getscl = ixp42x_bit_getscl;
125 drv_data->algo_data.udelay = 10;
126 drv_data->algo_data.timeout = 100;
128 drv_data->adapter.id = I2C_HW_B_IXP4XX;
129 drv_data->adapter.class = I2C_CLASS_HWMON;
130 strlcpy(drv_data->adapter.name, plat_dev->dev.driver->name,
131 I2C_NAME_SIZE);
132 drv_data->adapter.algo_data = &drv_data->algo_data;
134 drv_data->adapter.dev.parent = &plat_dev->dev;
136 gpio_line_config(gpio->scl_pin, IXP4XX_GPIO_IN);
137 gpio_line_config(gpio->sda_pin, IXP4XX_GPIO_IN);
138 gpio_line_set(gpio->scl_pin, 0);
139 gpio_line_set(gpio->sda_pin, 0);
141 err = i2c_bit_add_bus(&drv_data->adapter);
142 if (err) {
143 printk(KERN_ERR "ERROR: Could not install %s\n", plat_dev->dev.bus_id);
145 kfree(drv_data);
146 return err;
149 platform_set_drvdata(plat_dev, drv_data);
151 return 0;
154 static struct platform_driver ixp4xx_i2c_driver = {
155 .probe = ixp4xx_i2c_probe,
156 .remove = ixp4xx_i2c_remove,
157 .driver = {
158 .name = "IXP4XX-I2C",
159 .owner = THIS_MODULE,
163 static int __init ixp42x_i2c_init(void)
165 return platform_driver_register(&ixp4xx_i2c_driver);
168 static void __exit ixp42x_i2c_exit(void)
170 platform_driver_unregister(&ixp4xx_i2c_driver);
173 module_init(ixp42x_i2c_init);
174 module_exit(ixp42x_i2c_exit);
176 MODULE_DESCRIPTION("GPIO-based I2C driver for IXP42x systems");
177 MODULE_LICENSE("GPL");
178 MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");