scx200_i2c.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* linux/drivers/i2c/busses/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/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c-algo-bit.h>
  23. #include <asm/io.h>
  24. #include <linux/scx200_gpio.h>
  25. #define NAME "scx200_i2c"
  26. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  27. MODULE_DESCRIPTION("NatSemi SCx200 I2C Driver");
  28. MODULE_LICENSE("GPL");
  29. static int scl = CONFIG_SCx200_I2C_SCL;
  30. static int sda = CONFIG_SCx200_I2C_SDA;
  31. module_param(scl, int, 0);
  32. MODULE_PARM_DESC(scl, "GPIO line for SCL");
  33. module_param(sda, int, 0);
  34. MODULE_PARM_DESC(sda, "GPIO line for SDA");
  35. static void scx200_i2c_setscl(void *data, int state)
  36. {
  37. scx200_gpio_set(scl, state);
  38. }
  39. static void scx200_i2c_setsda(void *data, int state)
  40. {
  41. scx200_gpio_set(sda, state);
  42. }
  43. static int scx200_i2c_getscl(void *data)
  44. {
  45. return scx200_gpio_get(scl);
  46. }
  47. static int scx200_i2c_getsda(void *data)
  48. {
  49. return scx200_gpio_get(sda);
  50. }
  51. /* ------------------------------------------------------------------------
  52. * Encapsulate the above functions in the correct operations structure.
  53. * This is only done when more than one hardware adapter is supported.
  54. */
  55. static struct i2c_algo_bit_data scx200_i2c_data = {
  56. .setsda = scx200_i2c_setsda,
  57. .setscl = scx200_i2c_setscl,
  58. .getsda = scx200_i2c_getsda,
  59. .getscl = scx200_i2c_getscl,
  60. .udelay = 10,
  61. .timeout = 100,
  62. };
  63. static struct i2c_adapter scx200_i2c_ops = {
  64. .owner = THIS_MODULE,
  65. .algo_data = &scx200_i2c_data,
  66. .name = "NatSemi SCx200 I2C",
  67. };
  68. static int scx200_i2c_init(void)
  69. {
  70. pr_debug(NAME ": NatSemi SCx200 I2C Driver\n");
  71. if (!scx200_gpio_present()) {
  72. printk(KERN_ERR NAME ": no SCx200 gpio pins available\n");
  73. return -ENODEV;
  74. }
  75. pr_debug(NAME ": SCL=GPIO%02u, SDA=GPIO%02u\n", scl, sda);
  76. if (scl == -1 || sda == -1 || scl == sda) {
  77. printk(KERN_ERR NAME ": scl and sda must be specified\n");
  78. return -EINVAL;
  79. }
  80. /* Configure GPIOs as open collector outputs */
  81. scx200_gpio_configure(scl, ~2, 5);
  82. scx200_gpio_configure(sda, ~2, 5);
  83. if (i2c_bit_add_bus(&scx200_i2c_ops) < 0) {
  84. printk(KERN_ERR NAME ": adapter %s registration failed\n",
  85. scx200_i2c_ops.name);
  86. return -ENODEV;
  87. }
  88. return 0;
  89. }
  90. static void scx200_i2c_cleanup(void)
  91. {
  92. i2c_bit_del_bus(&scx200_i2c_ops);
  93. }
  94. module_init(scx200_i2c_init);
  95. module_exit(scx200_i2c_cleanup);
  96. /*
  97. Local variables:
  98. compile-command: "make -k -C ../.. SUBDIRS=drivers/i2c modules"
  99. c-basic-offset: 8
  100. End:
  101. */