blob: 1956af382cd896d8421db7373c2d7c395d30a8ad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/i2c/busses/i2c-ixp2000.c
3 *
4 * I2C adapter for IXP2000 systems using GPIOs for I2C bus
5 *
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>
9 *
10 * Copyright (c) 2003-2004 MontaVista Software Inc.
11 *
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.
15 *
16 * From Jeff Daly:
17 *
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.
23 *
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.
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/device.h>
32#include <linux/module.h>
33#include <linux/i2c.h>
34#include <linux/i2c-algo-bit.h>
35
Lennert Buytenhekc4982882005-06-24 20:54:35 +010036#include <asm/hardware.h> /* Pick up IXP2000-specific bits */
37#include <asm/arch/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static inline int ixp2000_scl_pin(void *data)
40{
41 return ((struct ixp2000_i2c_pins*)data)->scl_pin;
42}
43
44static inline int ixp2000_sda_pin(void *data)
45{
46 return ((struct ixp2000_i2c_pins*)data)->sda_pin;
47}
48
49
50static void ixp2000_bit_setscl(void *data, int val)
51{
52 int i = 5000;
53
54 if (val) {
55 gpio_line_config(ixp2000_scl_pin(data), GPIO_IN);
56 while(!gpio_line_get(ixp2000_scl_pin(data)) && i--);
57 } else {
58 gpio_line_config(ixp2000_scl_pin(data), GPIO_OUT);
59 }
60}
61
62static void ixp2000_bit_setsda(void *data, int val)
63{
64 if (val) {
65 gpio_line_config(ixp2000_sda_pin(data), GPIO_IN);
66 } else {
67 gpio_line_config(ixp2000_sda_pin(data), GPIO_OUT);
68 }
69}
70
71static int ixp2000_bit_getscl(void *data)
72{
73 return gpio_line_get(ixp2000_scl_pin(data));
74}
75
76static int ixp2000_bit_getsda(void *data)
77{
78 return gpio_line_get(ixp2000_sda_pin(data));
79}
80
81struct ixp2000_i2c_data {
82 struct ixp2000_i2c_pins *gpio_pins;
83 struct i2c_adapter adapter;
84 struct i2c_algo_bit_data algo_data;
85};
86
87static int ixp2000_i2c_remove(struct device *dev)
88{
89 struct platform_device *plat_dev = to_platform_device(dev);
90 struct ixp2000_i2c_data *drv_data = dev_get_drvdata(&plat_dev->dev);
91
92 dev_set_drvdata(&plat_dev->dev, NULL);
93
94 i2c_bit_del_bus(&drv_data->adapter);
95
96 kfree(drv_data);
97
98 return 0;
99}
100
101static int ixp2000_i2c_probe(struct device *dev)
102{
103 int err;
104 struct platform_device *plat_dev = to_platform_device(dev);
105 struct ixp2000_i2c_pins *gpio = plat_dev->dev.platform_data;
106 struct ixp2000_i2c_data *drv_data =
107 kmalloc(sizeof(struct ixp2000_i2c_data), GFP_KERNEL);
108
109 if (!drv_data)
110 return -ENOMEM;
111 memzero(drv_data, sizeof(*drv_data));
112 drv_data->gpio_pins = gpio;
113
114 drv_data->algo_data.data = gpio;
115 drv_data->algo_data.setsda = ixp2000_bit_setsda;
116 drv_data->algo_data.setscl = ixp2000_bit_setscl;
117 drv_data->algo_data.getsda = ixp2000_bit_getsda;
118 drv_data->algo_data.getscl = ixp2000_bit_getscl;
119 drv_data->algo_data.udelay = 6;
120 drv_data->algo_data.mdelay = 6;
121 drv_data->algo_data.timeout = 100;
122
123 drv_data->adapter.id = I2C_HW_B_IXP2000,
124 drv_data->adapter.algo_data = &drv_data->algo_data,
125
126 drv_data->adapter.dev.parent = &plat_dev->dev;
127
128 gpio_line_config(gpio->sda_pin, GPIO_IN);
129 gpio_line_config(gpio->scl_pin, GPIO_IN);
130 gpio_line_set(gpio->scl_pin, 0);
131 gpio_line_set(gpio->sda_pin, 0);
132
133 if ((err = i2c_bit_add_bus(&drv_data->adapter)) != 0) {
134 dev_err(dev, "Could not install, error %d\n", err);
135 kfree(drv_data);
136 return err;
137 }
138
139 dev_set_drvdata(&plat_dev->dev, drv_data);
140
141 return 0;
142}
143
144static struct device_driver ixp2000_i2c_driver = {
145 .name = "IXP2000-I2C",
146 .bus = &platform_bus_type,
147 .probe = ixp2000_i2c_probe,
148 .remove = ixp2000_i2c_remove,
149};
150
151static int __init ixp2000_i2c_init(void)
152{
153 return driver_register(&ixp2000_i2c_driver);
154}
155
156static void __exit ixp2000_i2c_exit(void)
157{
158 driver_unregister(&ixp2000_i2c_driver);
159}
160
161module_init(ixp2000_i2c_init);
162module_exit(ixp2000_i2c_exit);
163
164MODULE_AUTHOR ("Deepak Saxena <dsaxena@plexity.net>");
165MODULE_DESCRIPTION("IXP2000 GPIO-based I2C bus driver");
166MODULE_LICENSE("GPL");
167