scx200_i2c.c 3.3 KB

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