blob: 5d263f9014d647a325373ce76fe26c4e7ba75177 [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>
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/hardware.h> /* Pick up IXP2000-specific bits */
Linus Walleij87f911a2011-08-22 08:44:18 +010038#include <mach/gpio-ixp2000.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static inline int ixp2000_scl_pin(void *data)
41{
42 return ((struct ixp2000_i2c_pins*)data)->scl_pin;
43}
44
45static inline int ixp2000_sda_pin(void *data)
46{
47 return ((struct ixp2000_i2c_pins*)data)->sda_pin;
48}
49
50
51static void ixp2000_bit_setscl(void *data, int val)
52{
53 int i = 5000;
54
55 if (val) {
56 gpio_line_config(ixp2000_scl_pin(data), GPIO_IN);
57 while(!gpio_line_get(ixp2000_scl_pin(data)) && i--);
58 } else {
59 gpio_line_config(ixp2000_scl_pin(data), GPIO_OUT);
60 }
61}
62
63static void ixp2000_bit_setsda(void *data, int val)
64{
65 if (val) {
66 gpio_line_config(ixp2000_sda_pin(data), GPIO_IN);
67 } else {
68 gpio_line_config(ixp2000_sda_pin(data), GPIO_OUT);
69 }
70}
71
72static int ixp2000_bit_getscl(void *data)
73{
74 return gpio_line_get(ixp2000_scl_pin(data));
75}
76
77static int ixp2000_bit_getsda(void *data)
78{
79 return gpio_line_get(ixp2000_sda_pin(data));
80}
81
82struct ixp2000_i2c_data {
83 struct ixp2000_i2c_pins *gpio_pins;
84 struct i2c_adapter adapter;
85 struct i2c_algo_bit_data algo_data;
86};
87
Russell King3ae5eae2005-11-09 22:32:44 +000088static int ixp2000_i2c_remove(struct platform_device *plat_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Russell King3ae5eae2005-11-09 22:32:44 +000090 struct ixp2000_i2c_data *drv_data = platform_get_drvdata(plat_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Russell King3ae5eae2005-11-09 22:32:44 +000092 platform_set_drvdata(plat_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Jean Delvare32697112006-12-10 21:21:33 +010094 i2c_del_adapter(&drv_data->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 kfree(drv_data);
97
98 return 0;
99}
100
Russell King3ae5eae2005-11-09 22:32:44 +0000101static int ixp2000_i2c_probe(struct platform_device *plat_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct ixp2000_i2c_pins *gpio = plat_dev->dev.platform_data;
105 struct ixp2000_i2c_data *drv_data =
Deepak Saxena22860662005-10-17 23:07:05 +0200106 kzalloc(sizeof(struct ixp2000_i2c_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (!drv_data)
109 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 drv_data->gpio_pins = gpio;
111
112 drv_data->algo_data.data = gpio;
113 drv_data->algo_data.setsda = ixp2000_bit_setsda;
114 drv_data->algo_data.setscl = ixp2000_bit_setscl;
115 drv_data->algo_data.getsda = ixp2000_bit_getsda;
116 drv_data->algo_data.getscl = ixp2000_bit_getscl;
117 drv_data->algo_data.udelay = 6;
Jean Delvare082a4cf2009-02-24 19:19:49 +0100118 drv_data->algo_data.timeout = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Russell King7d78c882005-11-17 15:47:30 +0000120 strlcpy(drv_data->adapter.name, plat_dev->dev.driver->name,
David Brownell2096b952007-05-01 23:26:28 +0200121 sizeof(drv_data->adapter.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 drv_data->adapter.algo_data = &drv_data->algo_data,
123
124 drv_data->adapter.dev.parent = &plat_dev->dev;
125
126 gpio_line_config(gpio->sda_pin, GPIO_IN);
127 gpio_line_config(gpio->scl_pin, GPIO_IN);
128 gpio_line_set(gpio->scl_pin, 0);
129 gpio_line_set(gpio->sda_pin, 0);
130
131 if ((err = i2c_bit_add_bus(&drv_data->adapter)) != 0) {
Russell King7d78c882005-11-17 15:47:30 +0000132 dev_err(&plat_dev->dev, "Could not install, error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 kfree(drv_data);
134 return err;
135 }
136
Russell King3ae5eae2005-11-09 22:32:44 +0000137 platform_set_drvdata(plat_dev, drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 return 0;
140}
141
Russell King3ae5eae2005-11-09 22:32:44 +0000142static struct platform_driver ixp2000_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .probe = ixp2000_i2c_probe,
144 .remove = ixp2000_i2c_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000145 .driver = {
146 .name = "IXP2000-I2C",
147 .owner = THIS_MODULE,
148 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Axel Lina3664b52012-01-12 20:32:04 +0100151module_platform_driver(ixp2000_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153MODULE_AUTHOR ("Deepak Saxena <dsaxena@plexity.net>");
154MODULE_DESCRIPTION("IXP2000 GPIO-based I2C bus driver");
155MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200156MODULE_ALIAS("platform:IXP2000-I2C");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157