w1-gpio-custom.c 4.4 KB

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