0021-enabling-the-realtime-clock-1-wire-chip-DS1307-and-1.patch 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. From bd7d1508a83c544f2d52f668ebabe55c2ea207c5 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 021/114] enabling the realtime clock 1-wire chip DS1307 and
  5. 1-wire on 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. ---
  15. arch/arm/mach-bcm2708/bcm2708.c | 29 ++++++++++++++++++++++
  16. drivers/w1/masters/w1-gpio.c | 55 +++++++++++++++++++++++++++++++++++++----
  17. drivers/w1/w1.h | 6 +++++
  18. drivers/w1/w1_int.c | 14 +++++++++++
  19. drivers/w1/w1_io.c | 18 +++++++++++---
  20. 5 files changed, 114 insertions(+), 8 deletions(-)
  21. --- a/arch/arm/mach-bcm2708/bcm2708.c
  22. +++ b/arch/arm/mach-bcm2708/bcm2708.c
  23. @@ -32,6 +32,7 @@
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/spi/spi.h>
  27. +#include <linux/w1-gpio.h>
  28. #include <linux/version.h>
  29. #include <linux/clkdev.h>
  30. @@ -76,12 +77,19 @@
  31. */
  32. #define DMA_MASK_BITS_COMMON 32
  33. +// use GPIO 4 for the one-wire GPIO pin, if enabled
  34. +#define W1_GPIO 4
  35. +// ensure one-wire GPIO pullup is disabled by default
  36. +#define W1_PULLUP -1
  37. +
  38. /* command line parameters */
  39. static unsigned boardrev, serial;
  40. static unsigned uart_clock;
  41. static unsigned disk_led_gpio = 16;
  42. static unsigned disk_led_active_low = 1;
  43. static unsigned reboot_part = 0;
  44. +static unsigned w1_gpio_pin = W1_GPIO;
  45. +static unsigned w1_gpio_pullup = W1_PULLUP;
  46. static void __init bcm2708_init_led(void);
  47. @@ -258,6 +266,20 @@ static struct platform_device bcm2708_dm
  48. .num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
  49. };
  50. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  51. +static struct w1_gpio_platform_data w1_gpio_pdata = {
  52. + .pin = W1_GPIO,
  53. + .ext_pullup_enable_pin = W1_PULLUP,
  54. + .is_open_drain = 0,
  55. +};
  56. +
  57. +static struct platform_device w1_device = {
  58. + .name = "w1-gpio",
  59. + .id = -1,
  60. + .dev.platform_data = &w1_gpio_pdata,
  61. +};
  62. +#endif
  63. +
  64. static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  65. static struct platform_device bcm2708_fb_device = {
  66. @@ -652,6 +674,11 @@ void __init bcm2708_init(void)
  67. #ifdef CONFIG_BCM2708_GPIO
  68. bcm_register_device(&bcm2708_gpio_device);
  69. #endif
  70. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  71. + w1_gpio_pdata.pin = w1_gpio_pin;
  72. + w1_gpio_pdata.ext_pullup_enable_pin = w1_gpio_pullup;
  73. + platform_device_register(&w1_device);
  74. +#endif
  75. bcm_register_device(&bcm2708_systemtimer_device);
  76. bcm_register_device(&bcm2708_fb_device);
  77. bcm_register_device(&bcm2708_usb_device);
  78. @@ -853,3 +880,5 @@ module_param(uart_clock, uint, 0644);
  79. module_param(disk_led_gpio, uint, 0644);
  80. module_param(disk_led_active_low, uint, 0644);
  81. module_param(reboot_part, uint, 0644);
  82. +module_param(w1_gpio_pin, uint, 0644);
  83. +module_param(w1_gpio_pullup, uint, 0644);
  84. --- a/drivers/w1/masters/w1-gpio.c
  85. +++ b/drivers/w1/masters/w1-gpio.c
  86. @@ -23,6 +23,15 @@
  87. #include "../w1.h"
  88. #include "../w1_int.h"
  89. +static int w1_gpio_pullup = -1;
  90. +static int w1_gpio_pullup_orig = -1;
  91. +module_param_named(pullup, w1_gpio_pullup, int, 0);
  92. +MODULE_PARM_DESC(pullup, "GPIO pin pullup number");
  93. +static int w1_gpio_pin = -1;
  94. +static int w1_gpio_pin_orig = -1;
  95. +module_param_named(gpiopin, w1_gpio_pin, int, 0);
  96. +MODULE_PARM_DESC(gpiopin, "GPIO pin number");
  97. +
  98. static u8 w1_gpio_set_pullup(void *data, int delay)
  99. {
  100. struct w1_gpio_platform_data *pdata = data;
  101. @@ -67,6 +76,16 @@ static u8 w1_gpio_read_bit(void *data)
  102. return gpio_get_value(pdata->pin) ? 1 : 0;
  103. }
  104. +static void w1_gpio_bitbang_pullup(void *data, u8 on)
  105. +{
  106. + struct w1_gpio_platform_data *pdata = data;
  107. +
  108. + if (on)
  109. + gpio_direction_output(pdata->pin, 1);
  110. + else
  111. + gpio_direction_input(pdata->pin);
  112. +}
  113. +
  114. #if defined(CONFIG_OF)
  115. static struct of_device_id w1_gpio_dt_ids[] = {
  116. { .compatible = "w1-gpio" },
  117. @@ -113,13 +132,15 @@ static int w1_gpio_probe_dt(struct platf
  118. static int w1_gpio_probe(struct platform_device *pdev)
  119. {
  120. struct w1_bus_master *master;
  121. - struct w1_gpio_platform_data *pdata;
  122. + struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  123. int err;
  124. - if (of_have_populated_dt()) {
  125. - err = w1_gpio_probe_dt(pdev);
  126. - if (err < 0)
  127. - return err;
  128. + if(pdata == NULL) {
  129. + if (of_have_populated_dt()) {
  130. + err = w1_gpio_probe_dt(pdev);
  131. + if (err < 0)
  132. + return err;
  133. + }
  134. }
  135. pdata = dev_get_platdata(&pdev->dev);
  136. @@ -136,6 +157,19 @@ static int w1_gpio_probe(struct platform
  137. return -ENOMEM;
  138. }
  139. + w1_gpio_pin_orig = pdata->pin;
  140. + w1_gpio_pullup_orig = pdata->ext_pullup_enable_pin;
  141. +
  142. + if(gpio_is_valid(w1_gpio_pin)) {
  143. + pdata->pin = w1_gpio_pin;
  144. + pdata->ext_pullup_enable_pin = -1;
  145. + }
  146. + if(gpio_is_valid(w1_gpio_pullup)) {
  147. + pdata->ext_pullup_enable_pin = w1_gpio_pullup;
  148. + }
  149. +
  150. + dev_info(&pdev->dev, "gpio pin %d, gpio pullup pin %d\n", pdata->pin, pdata->ext_pullup_enable_pin);
  151. +
  152. err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
  153. if (err) {
  154. dev_err(&pdev->dev, "gpio_request (pin) failed\n");
  155. @@ -165,6 +199,14 @@ static int w1_gpio_probe(struct platform
  156. master->set_pullup = w1_gpio_set_pullup;
  157. }
  158. + if (gpio_is_valid(w1_gpio_pullup)) {
  159. + if (pdata->is_open_drain)
  160. + printk(KERN_ERR "w1-gpio 'pullup' option "
  161. + "doesn't work with open drain GPIO\n");
  162. + else
  163. + master->bitbang_pullup = w1_gpio_bitbang_pullup;
  164. + }
  165. +
  166. err = w1_add_master_device(master);
  167. if (err) {
  168. dev_err(&pdev->dev, "w1_add_master device failed\n");
  169. @@ -195,6 +237,9 @@ static int w1_gpio_remove(struct platfor
  170. w1_remove_master_device(master);
  171. + pdata->pin = w1_gpio_pin_orig;
  172. + pdata->ext_pullup_enable_pin = w1_gpio_pullup_orig;
  173. +
  174. return 0;
  175. }
  176. --- a/drivers/w1/w1.h
  177. +++ b/drivers/w1/w1.h
  178. @@ -171,6 +171,12 @@ struct w1_bus_master
  179. u8 (*set_pullup)(void *, int);
  180. + /**
  181. + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
  182. + * @return -1=Error, 0=completed
  183. + */
  184. + void (*bitbang_pullup) (void *, u8);
  185. +
  186. void (*search)(void *, struct w1_master *,
  187. u8, w1_slave_found_callback);
  188. };
  189. --- a/drivers/w1/w1_int.c
  190. +++ b/drivers/w1/w1_int.c
  191. @@ -123,6 +123,20 @@ int w1_add_master_device(struct w1_bus_m
  192. return(-EINVAL);
  193. }
  194. + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
  195. + * and takes care of timing itself */
  196. + if (!master->write_byte && !master->touch_bit && master->set_pullup) {
  197. + printk(KERN_ERR "w1_add_master_device: set_pullup requires "
  198. + "write_byte or touch_bit, disabling\n");
  199. + master->set_pullup = NULL;
  200. + }
  201. +
  202. + if (master->set_pullup && master->bitbang_pullup) {
  203. + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
  204. + "be set when bitbang_pullup is used, disabling\n");
  205. + master->set_pullup = NULL;
  206. + }
  207. +
  208. /* Lock until the device is added (or not) to w1_masters. */
  209. mutex_lock(&w1_mlock);
  210. /* Search for the first available id (starting at 1). */
  211. --- a/drivers/w1/w1_io.c
  212. +++ b/drivers/w1/w1_io.c
  213. @@ -134,10 +134,22 @@ static void w1_pre_write(struct w1_maste
  214. static void w1_post_write(struct w1_master *dev)
  215. {
  216. if (dev->pullup_duration) {
  217. - if (dev->enable_pullup && dev->bus_master->set_pullup)
  218. - dev->bus_master->set_pullup(dev->bus_master->data, 0);
  219. - else
  220. + if (dev->enable_pullup) {
  221. + if (dev->bus_master->set_pullup) {
  222. + dev->bus_master->set_pullup(dev->
  223. + bus_master->data,
  224. + 0);
  225. + } else if (dev->bus_master->bitbang_pullup) {
  226. + dev->bus_master->
  227. + bitbang_pullup(dev->bus_master->data, 1);
  228. msleep(dev->pullup_duration);
  229. + dev->bus_master->
  230. + bitbang_pullup(dev->bus_master->data, 0);
  231. + }
  232. + } else {
  233. + msleep(dev->pullup_duration);
  234. + }
  235. +
  236. dev->pullup_duration = 0;
  237. }
  238. }