| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 |  * drivers/i2c/busses/i2c-ixp4xx.c | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kernel.h> | 
 | 30 | #include <linux/init.h> | 
| Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 31 | #include <linux/platform_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #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 |  | 
 | 38 | static inline int ixp4xx_scl_pin(void *data) | 
 | 39 | { | 
 | 40 | 	return ((struct ixp4xx_i2c_pins*)data)->scl_pin; | 
 | 41 | } | 
 | 42 |  | 
 | 43 | static inline int ixp4xx_sda_pin(void *data) | 
 | 44 | { | 
 | 45 | 	return ((struct ixp4xx_i2c_pins*)data)->sda_pin; | 
 | 46 | } | 
 | 47 |  | 
 | 48 | static void ixp4xx_bit_setscl(void *data, int val) | 
 | 49 | { | 
 | 50 | 	gpio_line_set(ixp4xx_scl_pin(data), 0); | 
 | 51 | 	gpio_line_config(ixp4xx_scl_pin(data), | 
 | 52 | 		val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT ); | 
 | 53 | } | 
 | 54 |  | 
 | 55 | static void ixp4xx_bit_setsda(void *data, int val) | 
 | 56 | { | 
 | 57 | 	gpio_line_set(ixp4xx_sda_pin(data), 0); | 
 | 58 | 	gpio_line_config(ixp4xx_sda_pin(data), | 
 | 59 | 		val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT ); | 
 | 60 | } | 
 | 61 |  | 
 | 62 | static int ixp4xx_bit_getscl(void *data) | 
 | 63 | { | 
 | 64 | 	int scl; | 
 | 65 |  | 
 | 66 | 	gpio_line_config(ixp4xx_scl_pin(data), IXP4XX_GPIO_IN ); | 
 | 67 | 	gpio_line_get(ixp4xx_scl_pin(data), &scl); | 
 | 68 |  | 
 | 69 | 	return scl; | 
 | 70 | }	 | 
 | 71 |  | 
 | 72 | static int ixp4xx_bit_getsda(void *data) | 
 | 73 | { | 
 | 74 | 	int sda; | 
 | 75 |  | 
 | 76 | 	gpio_line_config(ixp4xx_sda_pin(data), IXP4XX_GPIO_IN ); | 
 | 77 | 	gpio_line_get(ixp4xx_sda_pin(data), &sda); | 
 | 78 |  | 
 | 79 | 	return sda; | 
 | 80 | }	 | 
 | 81 |  | 
 | 82 | struct ixp4xx_i2c_data { | 
 | 83 | 	struct ixp4xx_i2c_pins *gpio_pins; | 
 | 84 | 	struct i2c_adapter adapter; | 
 | 85 | 	struct i2c_algo_bit_data algo_data; | 
 | 86 | }; | 
 | 87 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 88 | static int ixp4xx_i2c_remove(struct platform_device *plat_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 90 | 	struct ixp4xx_i2c_data *drv_data = platform_get_drvdata(plat_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 92 | 	platform_set_drvdata(plat_dev, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 |  | 
| Jean Delvare | 3269711 | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 94 | 	i2c_del_adapter(&drv_data->adapter); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 |  | 
 | 96 | 	kfree(drv_data); | 
 | 97 |  | 
 | 98 | 	return 0; | 
 | 99 | } | 
 | 100 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 101 | static int ixp4xx_i2c_probe(struct platform_device *plat_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { | 
 | 103 | 	int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | 	struct ixp4xx_i2c_pins *gpio = plat_dev->dev.platform_data; | 
 | 105 | 	struct ixp4xx_i2c_data *drv_data =  | 
| Deepak Saxena | 2286066 | 2005-10-17 23:07:05 +0200 | [diff] [blame] | 106 | 		kzalloc(sizeof(struct ixp4xx_i2c_data), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 |  | 
 | 108 | 	if(!drv_data) | 
 | 109 | 		return -ENOMEM; | 
 | 110 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | 	drv_data->gpio_pins = gpio; | 
 | 112 |  | 
 | 113 | 	/* | 
 | 114 | 	 * We could make a lot of these structures static, but | 
 | 115 | 	 * certain platforms may have multiple GPIO-based I2C | 
 | 116 | 	 * buses for various device domains, so we need per-device | 
 | 117 | 	 * algo_data->data.  | 
 | 118 | 	 */ | 
 | 119 | 	drv_data->algo_data.data = gpio; | 
 | 120 | 	drv_data->algo_data.setsda = ixp4xx_bit_setsda; | 
 | 121 | 	drv_data->algo_data.setscl = ixp4xx_bit_setscl; | 
 | 122 | 	drv_data->algo_data.getsda = ixp4xx_bit_getsda; | 
 | 123 | 	drv_data->algo_data.getscl = ixp4xx_bit_getscl; | 
 | 124 | 	drv_data->algo_data.udelay = 10; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | 	drv_data->algo_data.timeout = 100; | 
 | 126 |  | 
 | 127 | 	drv_data->adapter.id = I2C_HW_B_IXP4XX; | 
| Alessandro Zummo | 7cd30b2 | 2006-02-27 23:12:58 +0100 | [diff] [blame] | 128 | 	drv_data->adapter.class = I2C_CLASS_HWMON; | 
| Russell King | 7d78c88 | 2005-11-17 15:47:30 +0000 | [diff] [blame] | 129 | 	strlcpy(drv_data->adapter.name, plat_dev->dev.driver->name, | 
| Jean Delvare | 8a1b028 | 2005-10-07 23:04:48 +0200 | [diff] [blame] | 130 | 		I2C_NAME_SIZE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | 	drv_data->adapter.algo_data = &drv_data->algo_data; | 
 | 132 |  | 
 | 133 | 	drv_data->adapter.dev.parent = &plat_dev->dev; | 
 | 134 |  | 
 | 135 | 	gpio_line_config(gpio->scl_pin, IXP4XX_GPIO_IN); | 
 | 136 | 	gpio_line_config(gpio->sda_pin, IXP4XX_GPIO_IN); | 
 | 137 | 	gpio_line_set(gpio->scl_pin, 0); | 
 | 138 | 	gpio_line_set(gpio->sda_pin, 0); | 
 | 139 |  | 
| Alexey Dobriyan | a6cd2d9 | 2006-11-18 22:19:36 -0800 | [diff] [blame] | 140 | 	err = i2c_bit_add_bus(&drv_data->adapter); | 
| Jean Delvare | 0b1082e | 2006-11-23 13:28:50 +0100 | [diff] [blame] | 141 | 	if (err) { | 
| Russell King | 7d78c88 | 2005-11-17 15:47:30 +0000 | [diff] [blame] | 142 | 		printk(KERN_ERR "ERROR: Could not install %s\n", plat_dev->dev.bus_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 |  | 
 | 144 | 		kfree(drv_data); | 
 | 145 | 		return err; | 
 | 146 | 	} | 
 | 147 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 148 | 	platform_set_drvdata(plat_dev, drv_data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 |  | 
 | 150 | 	return 0; | 
 | 151 | } | 
 | 152 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 153 | static struct platform_driver ixp4xx_i2c_driver = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | 	.probe		= ixp4xx_i2c_probe, | 
 | 155 | 	.remove		= ixp4xx_i2c_remove, | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 156 | 	.driver		= { | 
 | 157 | 		.name	= "IXP4XX-I2C", | 
 | 158 | 		.owner	= THIS_MODULE, | 
 | 159 | 	}, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | }; | 
 | 161 |  | 
 | 162 | static int __init ixp4xx_i2c_init(void) | 
 | 163 | { | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 164 | 	return platform_driver_register(&ixp4xx_i2c_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } | 
 | 166 |  | 
 | 167 | static void __exit ixp4xx_i2c_exit(void) | 
 | 168 | { | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 169 | 	platform_driver_unregister(&ixp4xx_i2c_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } | 
 | 171 |  | 
 | 172 | module_init(ixp4xx_i2c_init); | 
 | 173 | module_exit(ixp4xx_i2c_exit); | 
 | 174 |  | 
 | 175 | MODULE_DESCRIPTION("GPIO-based I2C adapter for IXP4xx systems"); | 
 | 176 | MODULE_LICENSE("GPL"); | 
 | 177 | MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>"); | 
 | 178 |  |