0059-enabling-the-realtime-clock-1-wire-chip-DS1307-and-1.patch 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. From d7a403ac584a5ed4a871c66deeb60fd887074ffc Mon Sep 17 00:00:00 2001
  2. From: popcornmix <popcornmix@gmail.com>
  3. Date: Wed, 8 May 2013 11:46:50 +0100
  4. Subject: [PATCH] enabling the realtime clock 1-wire chip DS1307 and 1-wire on
  5. GPIO4 (as a module)
  6. 1-wire: Add support for configuring pin for w1-gpio kernel module
  7. See: https://github.com/raspberrypi/linux/pull/457
  8. Add bitbanging pullups, use them for w1-gpio
  9. Allows parasite power to work, uses module option pullup=1
  10. bcm2708: Ensure 1-wire pullup is disabled by default, and expose as module parameter
  11. Signed-off-by: Alex J Lennon <ajlennon@dynamicdevices.co.uk>
  12. w1-gpio: Add gpiopin module parameter and correctly free up gpio pull-up pin, if set
  13. Signed-off-by: Alex J Lennon <ajlennon@dynamicdevices.co.uk>
  14. w1-gpio: Sort out the pullup/parasitic power tangle
  15. ---
  16. drivers/w1/masters/w1-gpio.c | 69 ++++++++++++++++++++++++++++++++++++++++----
  17. drivers/w1/w1.h | 6 ++++
  18. drivers/w1/w1_int.c | 14 +++++++++
  19. drivers/w1/w1_io.c | 18 ++++++++++--
  20. include/linux/w1-gpio.h | 1 +
  21. 5 files changed, 99 insertions(+), 9 deletions(-)
  22. --- a/drivers/w1/masters/w1-gpio.c
  23. +++ b/drivers/w1/masters/w1-gpio.c
  24. @@ -23,6 +23,19 @@
  25. #include "../w1.h"
  26. #include "../w1_int.h"
  27. +static int w1_gpio_pullup = 0;
  28. +static int w1_gpio_pullup_orig = 0;
  29. +module_param_named(pullup, w1_gpio_pullup, int, 0);
  30. +MODULE_PARM_DESC(pullup, "Enable parasitic power (power on data) mode");
  31. +static int w1_gpio_pullup_pin = -1;
  32. +static int w1_gpio_pullup_pin_orig = -1;
  33. +module_param_named(extpullup, w1_gpio_pullup_pin, int, 0);
  34. +MODULE_PARM_DESC(extpullup, "GPIO external pullup pin number");
  35. +static int w1_gpio_pin = -1;
  36. +static int w1_gpio_pin_orig = -1;
  37. +module_param_named(gpiopin, w1_gpio_pin, int, 0);
  38. +MODULE_PARM_DESC(gpiopin, "GPIO pin number");
  39. +
  40. static u8 w1_gpio_set_pullup(void *data, int delay)
  41. {
  42. struct w1_gpio_platform_data *pdata = data;
  43. @@ -67,6 +80,16 @@ static u8 w1_gpio_read_bit(void *data)
  44. return gpio_get_value(pdata->pin) ? 1 : 0;
  45. }
  46. +static void w1_gpio_bitbang_pullup(void *data, u8 on)
  47. +{
  48. + struct w1_gpio_platform_data *pdata = data;
  49. +
  50. + if (on)
  51. + gpio_direction_output(pdata->pin, 1);
  52. + else
  53. + gpio_direction_input(pdata->pin);
  54. +}
  55. +
  56. #if defined(CONFIG_OF)
  57. static const struct of_device_id w1_gpio_dt_ids[] = {
  58. { .compatible = "w1-gpio" },
  59. @@ -80,6 +103,7 @@ static int w1_gpio_probe_dt(struct platf
  60. struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  61. struct device_node *np = pdev->dev.of_node;
  62. int gpio;
  63. + u32 value;
  64. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  65. if (!pdata)
  66. @@ -88,6 +112,9 @@ static int w1_gpio_probe_dt(struct platf
  67. if (of_get_property(np, "linux,open-drain", NULL))
  68. pdata->is_open_drain = 1;
  69. + if (of_property_read_u32(np, "rpi,parasitic-power", &value) == 0)
  70. + pdata->parasitic_power = (value != 0);
  71. +
  72. gpio = of_get_gpio(np, 0);
  73. if (gpio < 0) {
  74. if (gpio != -EPROBE_DEFER)
  75. @@ -103,7 +130,7 @@ static int w1_gpio_probe_dt(struct platf
  76. if (gpio == -EPROBE_DEFER)
  77. return gpio;
  78. /* ignore other errors as the pullup gpio is optional */
  79. - pdata->ext_pullup_enable_pin = gpio;
  80. + pdata->ext_pullup_enable_pin = (gpio >= 0) ? gpio : -1;
  81. pdev->dev.platform_data = pdata;
  82. @@ -113,13 +140,15 @@ static int w1_gpio_probe_dt(struct platf
  83. static int w1_gpio_probe(struct platform_device *pdev)
  84. {
  85. struct w1_bus_master *master;
  86. - struct w1_gpio_platform_data *pdata;
  87. + struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  88. int err;
  89. - if (of_have_populated_dt()) {
  90. - err = w1_gpio_probe_dt(pdev);
  91. - if (err < 0)
  92. - return err;
  93. + if(pdata == NULL) {
  94. + if (of_have_populated_dt()) {
  95. + err = w1_gpio_probe_dt(pdev);
  96. + if (err < 0)
  97. + return err;
  98. + }
  99. }
  100. pdata = dev_get_platdata(&pdev->dev);
  101. @@ -136,6 +165,22 @@ static int w1_gpio_probe(struct platform
  102. return -ENOMEM;
  103. }
  104. + w1_gpio_pin_orig = pdata->pin;
  105. + w1_gpio_pullup_pin_orig = pdata->ext_pullup_enable_pin;
  106. + w1_gpio_pullup_orig = pdata->parasitic_power;
  107. +
  108. + if(gpio_is_valid(w1_gpio_pin)) {
  109. + pdata->pin = w1_gpio_pin;
  110. + pdata->ext_pullup_enable_pin = -1;
  111. + pdata->parasitic_power = -1;
  112. + }
  113. + pdata->parasitic_power |= w1_gpio_pullup;
  114. + if(gpio_is_valid(w1_gpio_pullup_pin)) {
  115. + pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin;
  116. + }
  117. +
  118. + dev_info(&pdev->dev, "gpio pin %d, external pullup pin %d, parasitic power %d\n", pdata->pin, pdata->ext_pullup_enable_pin, pdata->parasitic_power);
  119. +
  120. err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
  121. if (err) {
  122. dev_err(&pdev->dev, "gpio_request (pin) failed\n");
  123. @@ -165,6 +210,14 @@ static int w1_gpio_probe(struct platform
  124. master->set_pullup = w1_gpio_set_pullup;
  125. }
  126. + if (pdata->parasitic_power) {
  127. + if (pdata->is_open_drain)
  128. + printk(KERN_ERR "w1-gpio 'pullup'(parasitic power) "
  129. + "option doesn't work with open drain GPIO\n");
  130. + else
  131. + master->bitbang_pullup = w1_gpio_bitbang_pullup;
  132. + }
  133. +
  134. err = w1_add_master_device(master);
  135. if (err) {
  136. dev_err(&pdev->dev, "w1_add_master device failed\n");
  137. @@ -195,6 +248,10 @@ static int w1_gpio_remove(struct platfor
  138. w1_remove_master_device(master);
  139. + pdata->pin = w1_gpio_pin_orig;
  140. + pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin_orig;
  141. + pdata->parasitic_power = w1_gpio_pullup_orig;
  142. +
  143. return 0;
  144. }
  145. --- a/drivers/w1/w1.h
  146. +++ b/drivers/w1/w1.h
  147. @@ -171,6 +171,12 @@ struct w1_bus_master
  148. u8 (*set_pullup)(void *, int);
  149. + /**
  150. + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
  151. + * @return -1=Error, 0=completed
  152. + */
  153. + void (*bitbang_pullup) (void *, u8);
  154. +
  155. void (*search)(void *, struct w1_master *,
  156. u8, w1_slave_found_callback);
  157. };
  158. --- a/drivers/w1/w1_int.c
  159. +++ b/drivers/w1/w1_int.c
  160. @@ -122,6 +122,20 @@ int w1_add_master_device(struct w1_bus_m
  161. return(-EINVAL);
  162. }
  163. + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
  164. + * and takes care of timing itself */
  165. + if (!master->write_byte && !master->touch_bit && master->set_pullup) {
  166. + printk(KERN_ERR "w1_add_master_device: set_pullup requires "
  167. + "write_byte or touch_bit, disabling\n");
  168. + master->set_pullup = NULL;
  169. + }
  170. +
  171. + if (master->set_pullup && master->bitbang_pullup) {
  172. + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
  173. + "be set when bitbang_pullup is used, disabling\n");
  174. + master->set_pullup = NULL;
  175. + }
  176. +
  177. /* Lock until the device is added (or not) to w1_masters. */
  178. mutex_lock(&w1_mlock);
  179. /* Search for the first available id (starting at 1). */
  180. --- a/drivers/w1/w1_io.c
  181. +++ b/drivers/w1/w1_io.c
  182. @@ -134,10 +134,22 @@ static void w1_pre_write(struct w1_maste
  183. static void w1_post_write(struct w1_master *dev)
  184. {
  185. if (dev->pullup_duration) {
  186. - if (dev->enable_pullup && dev->bus_master->set_pullup)
  187. - dev->bus_master->set_pullup(dev->bus_master->data, 0);
  188. - else
  189. + if (dev->enable_pullup) {
  190. + if (dev->bus_master->set_pullup) {
  191. + dev->bus_master->set_pullup(dev->
  192. + bus_master->data,
  193. + 0);
  194. + } else if (dev->bus_master->bitbang_pullup) {
  195. + dev->bus_master->
  196. + bitbang_pullup(dev->bus_master->data, 1);
  197. msleep(dev->pullup_duration);
  198. + dev->bus_master->
  199. + bitbang_pullup(dev->bus_master->data, 0);
  200. + }
  201. + } else {
  202. + msleep(dev->pullup_duration);
  203. + }
  204. +
  205. dev->pullup_duration = 0;
  206. }
  207. }
  208. --- a/include/linux/w1-gpio.h
  209. +++ b/include/linux/w1-gpio.h
  210. @@ -18,6 +18,7 @@
  211. struct w1_gpio_platform_data {
  212. unsigned int pin;
  213. unsigned int is_open_drain:1;
  214. + unsigned int parasitic_power:1;
  215. void (*enable_external_pullup)(int enable);
  216. unsigned int ext_pullup_enable_pin;
  217. unsigned int pullup_duration;