Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 1 | /* linux/drivers/i2c/busses/scx200_i2c.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
| 3 | Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> |
| 4 | |
| 5 | National Semiconductor SCx200 I2C bus on GPIO pins |
| 6 | |
| 7 | Based on i2c-velleman.c Copyright (C) 1995-96, 2000 Simon G. Vogl |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/i2c.h> |
| 29 | #include <linux/i2c-algo-bit.h> |
| 30 | #include <asm/io.h> |
| 31 | |
| 32 | #include <linux/scx200_gpio.h> |
| 33 | |
| 34 | #define NAME "scx200_i2c" |
| 35 | |
| 36 | MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>"); |
| 37 | MODULE_DESCRIPTION("NatSemi SCx200 I2C Driver"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | |
| 40 | static int scl = CONFIG_SCx200_I2C_SCL; |
| 41 | static int sda = CONFIG_SCx200_I2C_SDA; |
| 42 | |
| 43 | module_param(scl, int, 0); |
| 44 | MODULE_PARM_DESC(scl, "GPIO line for SCL"); |
| 45 | module_param(sda, int, 0); |
| 46 | MODULE_PARM_DESC(sda, "GPIO line for SDA"); |
| 47 | |
| 48 | static void scx200_i2c_setscl(void *data, int state) |
| 49 | { |
| 50 | scx200_gpio_set(scl, state); |
| 51 | } |
| 52 | |
| 53 | static void scx200_i2c_setsda(void *data, int state) |
| 54 | { |
| 55 | scx200_gpio_set(sda, state); |
| 56 | } |
| 57 | |
| 58 | static int scx200_i2c_getscl(void *data) |
| 59 | { |
| 60 | return scx200_gpio_get(scl); |
| 61 | } |
| 62 | |
| 63 | static int scx200_i2c_getsda(void *data) |
| 64 | { |
| 65 | return scx200_gpio_get(sda); |
| 66 | } |
| 67 | |
| 68 | /* ------------------------------------------------------------------------ |
| 69 | * Encapsulate the above functions in the correct operations structure. |
| 70 | * This is only done when more than one hardware adapter is supported. |
| 71 | */ |
| 72 | |
| 73 | static struct i2c_algo_bit_data scx200_i2c_data = { |
Jean Delvare | a0d9c63 | 2006-08-27 11:46:49 +0200 | [diff] [blame] | 74 | .setsda = scx200_i2c_setsda, |
| 75 | .setscl = scx200_i2c_setscl, |
| 76 | .getsda = scx200_i2c_getsda, |
| 77 | .getscl = scx200_i2c_getscl, |
| 78 | .udelay = 10, |
| 79 | .timeout = 100, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | static struct i2c_adapter scx200_i2c_ops = { |
| 83 | .owner = THIS_MODULE, |
Lennart Sorensen | 4a029ab | 2008-10-30 15:55:47 +0100 | [diff] [blame^] | 84 | .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, |
Stephen Hemminger | 9ace555 | 2007-02-13 22:09:01 +0100 | [diff] [blame] | 85 | .id = I2C_HW_B_SCX200, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | .algo_data = &scx200_i2c_data, |
| 87 | .name = "NatSemi SCx200 I2C", |
| 88 | }; |
| 89 | |
| 90 | static int scx200_i2c_init(void) |
| 91 | { |
| 92 | pr_debug(NAME ": NatSemi SCx200 I2C Driver\n"); |
| 93 | |
| 94 | if (!scx200_gpio_present()) { |
| 95 | printk(KERN_ERR NAME ": no SCx200 gpio pins available\n"); |
| 96 | return -ENODEV; |
| 97 | } |
| 98 | |
| 99 | pr_debug(NAME ": SCL=GPIO%02u, SDA=GPIO%02u\n", scl, sda); |
| 100 | |
| 101 | if (scl == -1 || sda == -1 || scl == sda) { |
| 102 | printk(KERN_ERR NAME ": scl and sda must be specified\n"); |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | |
| 106 | /* Configure GPIOs as open collector outputs */ |
| 107 | scx200_gpio_configure(scl, ~2, 5); |
| 108 | scx200_gpio_configure(sda, ~2, 5); |
| 109 | |
| 110 | if (i2c_bit_add_bus(&scx200_i2c_ops) < 0) { |
| 111 | printk(KERN_ERR NAME ": adapter %s registration failed\n", |
| 112 | scx200_i2c_ops.name); |
| 113 | return -ENODEV; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static void scx200_i2c_cleanup(void) |
| 120 | { |
Jean Delvare | 3269711 | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 121 | i2c_del_adapter(&scx200_i2c_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | module_init(scx200_i2c_init); |
| 125 | module_exit(scx200_i2c_cleanup); |
| 126 | |
| 127 | /* |
| 128 | Local variables: |
| 129 | compile-command: "make -k -C ../.. SUBDIRS=drivers/i2c modules" |
| 130 | c-basic-offset: 8 |
| 131 | End: |
| 132 | */ |