1
0

i2c-gpio-custom.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Custom GPIO-based I2C driver
  3. *
  4. * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * ---------------------------------------------------------------------------
  11. *
  12. * The behaviour of this driver can be altered by setting some parameters
  13. * from the insmod command line.
  14. *
  15. * The following parameters are adjustable:
  16. *
  17. * bus0 These four arguments can be arrays of
  18. * bus1 1-8 unsigned integers as follows:
  19. * bus2
  20. * bus3 <id>,<sda>,<scl>,<udelay>,<timeout>,<sda_od>,<scl_od>,<scl_oo>
  21. *
  22. * where:
  23. *
  24. * <id> ID to used as device_id for the corresponding bus (required)
  25. * <sda> GPIO pin ID to used for SDA (required)
  26. * <scl> GPIO pin ID to used for SCL (required)
  27. * <udelay> signal toggle delay.
  28. * <timeout> clock stretching timeout.
  29. * <sda_od> SDA is configured as open drain.
  30. * <scl_od> SCL is configured as open drain.
  31. * <scl_oo> SCL output drivers cannot be turned off.
  32. *
  33. * See include/i2c-gpio.h for more information about the parameters.
  34. *
  35. * If this driver is built into the kernel, you can use the following kernel
  36. * command line parameters, with the same values as the corresponding module
  37. * parameters listed above:
  38. *
  39. * i2c-gpio-custom.bus0
  40. * i2c-gpio-custom.bus1
  41. * i2c-gpio-custom.bus2
  42. * i2c-gpio-custom.bus3
  43. */
  44. #include <linux/kernel.h>
  45. #include <linux/module.h>
  46. #include <linux/init.h>
  47. #include <linux/platform_device.h>
  48. #include <linux/i2c-gpio.h>
  49. #define DRV_NAME "i2c-gpio-custom"
  50. #define DRV_DESC "Custom GPIO-based I2C driver"
  51. #define DRV_VERSION "0.1.1"
  52. #define PFX DRV_NAME ": "
  53. #define BUS_PARAM_ID 0
  54. #define BUS_PARAM_SDA 1
  55. #define BUS_PARAM_SCL 2
  56. #define BUS_PARAM_UDELAY 3
  57. #define BUS_PARAM_TIMEOUT 4
  58. #define BUS_PARAM_SDA_OD 5
  59. #define BUS_PARAM_SCL_OD 6
  60. #define BUS_PARAM_SCL_OO 7
  61. #define BUS_PARAM_REQUIRED 3
  62. #define BUS_PARAM_COUNT 8
  63. #define BUS_COUNT_MAX 4
  64. static unsigned int bus0[BUS_PARAM_COUNT] __initdata;
  65. static unsigned int bus1[BUS_PARAM_COUNT] __initdata;
  66. static unsigned int bus2[BUS_PARAM_COUNT] __initdata;
  67. static unsigned int bus3[BUS_PARAM_COUNT] __initdata;
  68. static unsigned int bus_nump[BUS_COUNT_MAX] __initdata;
  69. #define BUS_PARM_DESC \
  70. " config -> id,sda,scl[,udelay,timeout,sda_od,scl_od,scl_oo]"
  71. module_param_array(bus0, uint, &bus_nump[0], 0);
  72. MODULE_PARM_DESC(bus0, "bus0" BUS_PARM_DESC);
  73. module_param_array(bus1, uint, &bus_nump[1], 0);
  74. MODULE_PARM_DESC(bus1, "bus1" BUS_PARM_DESC);
  75. module_param_array(bus2, uint, &bus_nump[2], 0);
  76. MODULE_PARM_DESC(bus2, "bus2" BUS_PARM_DESC);
  77. module_param_array(bus3, uint, &bus_nump[3], 0);
  78. MODULE_PARM_DESC(bus3, "bus3" BUS_PARM_DESC);
  79. static struct platform_device *devices[BUS_COUNT_MAX];
  80. static unsigned int nr_devices;
  81. static void i2c_gpio_custom_cleanup(void)
  82. {
  83. int i;
  84. for (i = 0; i < nr_devices; i++)
  85. if (devices[i])
  86. platform_device_put(devices[i]);
  87. }
  88. static int __init i2c_gpio_custom_add_one(unsigned int id, unsigned int *params)
  89. {
  90. struct platform_device *pdev;
  91. struct i2c_gpio_platform_data pdata;
  92. int err;
  93. if (!bus_nump[id])
  94. return 0;
  95. if (bus_nump[id] < BUS_PARAM_REQUIRED) {
  96. printk(KERN_ERR PFX "not enough parameters for bus%d\n", id);
  97. err = -EINVAL;
  98. goto err;
  99. }
  100. pdev = platform_device_alloc("i2c-gpio", params[BUS_PARAM_ID]);
  101. if (!pdev) {
  102. err = -ENOMEM;
  103. goto err;
  104. }
  105. pdata.sda_pin = params[BUS_PARAM_SDA];
  106. pdata.scl_pin = params[BUS_PARAM_SCL];
  107. pdata.udelay = params[BUS_PARAM_UDELAY];
  108. pdata.timeout = params[BUS_PARAM_TIMEOUT];
  109. pdata.sda_is_open_drain = params[BUS_PARAM_SDA_OD] != 0;
  110. pdata.scl_is_open_drain = params[BUS_PARAM_SCL_OD] != 0;
  111. pdata.scl_is_output_only = params[BUS_PARAM_SCL_OO] != 0;
  112. err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
  113. if (err)
  114. goto err_put;
  115. err = platform_device_add(pdev);
  116. if (err)
  117. goto err_put;
  118. devices[nr_devices++] = pdev;
  119. return 0;
  120. err_put:
  121. platform_device_put(pdev);
  122. err:
  123. return err;
  124. }
  125. static int __init i2c_gpio_custom_probe(void)
  126. {
  127. int err;
  128. printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
  129. err = i2c_gpio_custom_add_one(0, bus0);
  130. if (err)
  131. goto err;
  132. err = i2c_gpio_custom_add_one(1, bus1);
  133. if (err)
  134. goto err;
  135. err = i2c_gpio_custom_add_one(2, bus2);
  136. if (err)
  137. goto err;
  138. err = i2c_gpio_custom_add_one(3, bus3);
  139. if (err)
  140. goto err;
  141. if (!nr_devices) {
  142. printk(KERN_ERR PFX "no bus parameter(s) specified\n");
  143. err = -ENODEV;
  144. goto err;
  145. }
  146. return 0;
  147. err:
  148. i2c_gpio_custom_cleanup();
  149. return err;
  150. }
  151. #ifdef MODULE
  152. static int __init i2c_gpio_custom_init(void)
  153. {
  154. return i2c_gpio_custom_probe();
  155. }
  156. module_init(i2c_gpio_custom_init);
  157. static void __exit i2c_gpio_custom_exit(void)
  158. {
  159. i2c_gpio_custom_cleanup();
  160. }
  161. module_exit(i2c_gpio_custom_exit);
  162. #else
  163. subsys_initcall(i2c_gpio_custom_probe);
  164. #endif /* MODULE*/
  165. MODULE_LICENSE("GPL v2");
  166. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org >");
  167. MODULE_DESCRIPTION(DRV_DESC);
  168. MODULE_VERSION(DRV_VERSION);