blob: 8c55eafc3a0979bd9519b13720fcb870a3dacfe8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/i2c/i2c-adap-ixp4xx.c
3 *
4 * Intel's IXP4xx 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.
9 *
10 * Author: Deepak Saxena <dsaxena@plexity.net>
11 *
12 * Copyright (c) 2003-2004 MontaVista Software Inc.
13 *
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.
17 *
18 * NOTE: Since different platforms will use different GPIO pins for
19 * I2C, this driver uses an IXP4xx-specific platform_data
20 * pointer to pass the GPIO numbers to the driver. This
21 * allows us to support all the different IXP4xx platforms
22 * w/o having to put #ifdefs in this driver.
23 *
24 * See arch/arm/mach-ixp4xx/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.
27 */
28
29#include <linux/config.h>
30#ifdef CONFIG_I2C_DEBUG_BUS
31#define DEBUG 1
32#endif
33
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/device.h>
37#include <linux/module.h>
38#include <linux/i2c.h>
39#include <linux/i2c-algo-bit.h>
40
41#include <asm/hardware.h> /* Pick up IXP4xx-specific bits */
42
43static inline int ixp4xx_scl_pin(void *data)
44{
45 return ((struct ixp4xx_i2c_pins*)data)->scl_pin;
46}
47
48static inline int ixp4xx_sda_pin(void *data)
49{
50 return ((struct ixp4xx_i2c_pins*)data)->sda_pin;
51}
52
53static void ixp4xx_bit_setscl(void *data, int val)
54{
55 gpio_line_set(ixp4xx_scl_pin(data), 0);
56 gpio_line_config(ixp4xx_scl_pin(data),
57 val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
58}
59
60static void ixp4xx_bit_setsda(void *data, int val)
61{
62 gpio_line_set(ixp4xx_sda_pin(data), 0);
63 gpio_line_config(ixp4xx_sda_pin(data),
64 val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
65}
66
67static int ixp4xx_bit_getscl(void *data)
68{
69 int scl;
70
71 gpio_line_config(ixp4xx_scl_pin(data), IXP4XX_GPIO_IN );
72 gpio_line_get(ixp4xx_scl_pin(data), &scl);
73
74 return scl;
75}
76
77static int ixp4xx_bit_getsda(void *data)
78{
79 int sda;
80
81 gpio_line_config(ixp4xx_sda_pin(data), IXP4XX_GPIO_IN );
82 gpio_line_get(ixp4xx_sda_pin(data), &sda);
83
84 return sda;
85}
86
87struct ixp4xx_i2c_data {
88 struct ixp4xx_i2c_pins *gpio_pins;
89 struct i2c_adapter adapter;
90 struct i2c_algo_bit_data algo_data;
91};
92
93static int ixp4xx_i2c_remove(struct device *dev)
94{
95 struct platform_device *plat_dev = to_platform_device(dev);
96 struct ixp4xx_i2c_data *drv_data = dev_get_drvdata(&plat_dev->dev);
97
98 dev_set_drvdata(&plat_dev->dev, NULL);
99
100 i2c_bit_del_bus(&drv_data->adapter);
101
102 kfree(drv_data);
103
104 return 0;
105}
106
107static int ixp4xx_i2c_probe(struct device *dev)
108{
109 int err;
110 struct platform_device *plat_dev = to_platform_device(dev);
111 struct ixp4xx_i2c_pins *gpio = plat_dev->dev.platform_data;
112 struct ixp4xx_i2c_data *drv_data =
113 kmalloc(sizeof(struct ixp4xx_i2c_data), GFP_KERNEL);
114
115 if(!drv_data)
116 return -ENOMEM;
117
118 memzero(drv_data, sizeof(struct ixp4xx_i2c_data));
119 drv_data->gpio_pins = gpio;
120
121 /*
122 * We could make a lot of these structures static, but
123 * certain platforms may have multiple GPIO-based I2C
124 * buses for various device domains, so we need per-device
125 * algo_data->data.
126 */
127 drv_data->algo_data.data = gpio;
128 drv_data->algo_data.setsda = ixp4xx_bit_setsda;
129 drv_data->algo_data.setscl = ixp4xx_bit_setscl;
130 drv_data->algo_data.getsda = ixp4xx_bit_getsda;
131 drv_data->algo_data.getscl = ixp4xx_bit_getscl;
132 drv_data->algo_data.udelay = 10;
133 drv_data->algo_data.mdelay = 10;
134 drv_data->algo_data.timeout = 100;
135
136 drv_data->adapter.id = I2C_HW_B_IXP4XX;
137 drv_data->adapter.algo_data = &drv_data->algo_data;
138
139 drv_data->adapter.dev.parent = &plat_dev->dev;
140
141 gpio_line_config(gpio->scl_pin, IXP4XX_GPIO_IN);
142 gpio_line_config(gpio->sda_pin, IXP4XX_GPIO_IN);
143 gpio_line_set(gpio->scl_pin, 0);
144 gpio_line_set(gpio->sda_pin, 0);
145
146 if ((err = i2c_bit_add_bus(&drv_data->adapter) != 0)) {
147 printk(KERN_ERR "ERROR: Could not install %s\n", dev->bus_id);
148
149 kfree(drv_data);
150 return err;
151 }
152
153 dev_set_drvdata(&plat_dev->dev, drv_data);
154
155 return 0;
156}
157
158static struct device_driver ixp4xx_i2c_driver = {
159 .name = "IXP4XX-I2C",
160 .bus = &platform_bus_type,
161 .probe = ixp4xx_i2c_probe,
162 .remove = ixp4xx_i2c_remove,
163};
164
165static int __init ixp4xx_i2c_init(void)
166{
167 return driver_register(&ixp4xx_i2c_driver);
168}
169
170static void __exit ixp4xx_i2c_exit(void)
171{
172 driver_unregister(&ixp4xx_i2c_driver);
173}
174
175module_init(ixp4xx_i2c_init);
176module_exit(ixp4xx_i2c_exit);
177
178MODULE_DESCRIPTION("GPIO-based I2C adapter for IXP4xx systems");
179MODULE_LICENSE("GPL");
180MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
181