scx200_i2c.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <linux/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 = HZ,
  62. };
  63. static struct i2c_adapter scx200_i2c_ops = {
  64. .owner = THIS_MODULE,
  65. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  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_del_adapter(&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. */