blob: aa36855fa995470b2e52d29bd7d0076f189d5b8c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
30#include <linux/init.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010031#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
33#include <linux/i2c.h>
34#include <linux/i2c-algo-bit.h>
35
36#include <asm/hardware.h> /* Pick up IXP4xx-specific bits */
37
Jean Delvare8a1b0282005-10-07 23:04:48 +020038static struct device_driver ixp4xx_i2c_driver;
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static inline int ixp4xx_scl_pin(void *data)
41{
42 return ((struct ixp4xx_i2c_pins*)data)->scl_pin;
43}
44
45static inline int ixp4xx_sda_pin(void *data)
46{
47 return ((struct ixp4xx_i2c_pins*)data)->sda_pin;
48}
49
50static void ixp4xx_bit_setscl(void *data, int val)
51{
52 gpio_line_set(ixp4xx_scl_pin(data), 0);
53 gpio_line_config(ixp4xx_scl_pin(data),
54 val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
55}
56
57static void ixp4xx_bit_setsda(void *data, int val)
58{
59 gpio_line_set(ixp4xx_sda_pin(data), 0);
60 gpio_line_config(ixp4xx_sda_pin(data),
61 val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
62}
63
64static int ixp4xx_bit_getscl(void *data)
65{
66 int scl;
67
68 gpio_line_config(ixp4xx_scl_pin(data), IXP4XX_GPIO_IN );
69 gpio_line_get(ixp4xx_scl_pin(data), &scl);
70
71 return scl;
72}
73
74static int ixp4xx_bit_getsda(void *data)
75{
76 int sda;
77
78 gpio_line_config(ixp4xx_sda_pin(data), IXP4XX_GPIO_IN );
79 gpio_line_get(ixp4xx_sda_pin(data), &sda);
80
81 return sda;
82}
83
84struct ixp4xx_i2c_data {
85 struct ixp4xx_i2c_pins *gpio_pins;
86 struct i2c_adapter adapter;
87 struct i2c_algo_bit_data algo_data;
88};
89
Russell King3ae5eae2005-11-09 22:32:44 +000090static int ixp4xx_i2c_remove(struct platform_device *plat_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Russell King3ae5eae2005-11-09 22:32:44 +000092 struct ixp4xx_i2c_data *drv_data = platform_get_drvdata(plat_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Russell King3ae5eae2005-11-09 22:32:44 +000094 platform_set_drvdata(plat_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 i2c_bit_del_bus(&drv_data->adapter);
97
98 kfree(drv_data);
99
100 return 0;
101}
102
Russell King3ae5eae2005-11-09 22:32:44 +0000103static int ixp4xx_i2c_probe(struct platform_device *plat_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct ixp4xx_i2c_pins *gpio = plat_dev->dev.platform_data;
107 struct ixp4xx_i2c_data *drv_data =
Deepak Saxena22860662005-10-17 23:07:05 +0200108 kzalloc(sizeof(struct ixp4xx_i2c_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if(!drv_data)
111 return -ENOMEM;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 drv_data->gpio_pins = gpio;
114
115 /*
116 * We could make a lot of these structures static, but
117 * certain platforms may have multiple GPIO-based I2C
118 * buses for various device domains, so we need per-device
119 * algo_data->data.
120 */
121 drv_data->algo_data.data = gpio;
122 drv_data->algo_data.setsda = ixp4xx_bit_setsda;
123 drv_data->algo_data.setscl = ixp4xx_bit_setscl;
124 drv_data->algo_data.getsda = ixp4xx_bit_getsda;
125 drv_data->algo_data.getscl = ixp4xx_bit_getscl;
126 drv_data->algo_data.udelay = 10;
127 drv_data->algo_data.mdelay = 10;
128 drv_data->algo_data.timeout = 100;
129
130 drv_data->adapter.id = I2C_HW_B_IXP4XX;
Jean Delvare8a1b0282005-10-07 23:04:48 +0200131 strlcpy(drv_data->adapter.name, ixp4xx_i2c_driver.name,
132 I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 drv_data->adapter.algo_data = &drv_data->algo_data;
134
135 drv_data->adapter.dev.parent = &plat_dev->dev;
136
137 gpio_line_config(gpio->scl_pin, IXP4XX_GPIO_IN);
138 gpio_line_config(gpio->sda_pin, IXP4XX_GPIO_IN);
139 gpio_line_set(gpio->scl_pin, 0);
140 gpio_line_set(gpio->sda_pin, 0);
141
142 if ((err = i2c_bit_add_bus(&drv_data->adapter) != 0)) {
143 printk(KERN_ERR "ERROR: Could not install %s\n", dev->bus_id);
144
145 kfree(drv_data);
146 return err;
147 }
148
Russell King3ae5eae2005-11-09 22:32:44 +0000149 platform_set_drvdata(plat_dev, drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return 0;
152}
153
Russell King3ae5eae2005-11-09 22:32:44 +0000154static struct platform_driver ixp4xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .probe = ixp4xx_i2c_probe,
156 .remove = ixp4xx_i2c_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000157 .driver = {
158 .name = "IXP4XX-I2C",
159 .owner = THIS_MODULE,
160 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
163static int __init ixp4xx_i2c_init(void)
164{
Russell King3ae5eae2005-11-09 22:32:44 +0000165 return platform_driver_register(&ixp4xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168static void __exit ixp4xx_i2c_exit(void)
169{
Russell King3ae5eae2005-11-09 22:32:44 +0000170 platform_driver_unregister(&ixp4xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173module_init(ixp4xx_i2c_init);
174module_exit(ixp4xx_i2c_exit);
175
176MODULE_DESCRIPTION("GPIO-based I2C adapter for IXP4xx systems");
177MODULE_LICENSE("GPL");
178MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
179