Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * w1-gpio - GPIO w1 bus master driver |
| 3 | * |
| 4 | * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi> |
| 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 |
| 8 | * as published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 15 | #include <linux/w1-gpio.h> |
Mark Brown | e250b34 | 2012-02-04 16:33:55 +0000 | [diff] [blame] | 16 | #include <linux/gpio.h> |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 17 | #include <linux/of_platform.h> |
| 18 | #include <linux/of_gpio.h> |
Pantelis Antoniou | 277ed0d | 2012-11-22 01:13:29 +0400 | [diff] [blame] | 19 | #include <linux/err.h> |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 20 | #include <linux/of.h> |
Evgeny Boger | 3089a4c | 2014-01-23 15:56:18 -0800 | [diff] [blame] | 21 | #include <linux/delay.h> |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 22 | |
| 23 | #include "../w1.h" |
| 24 | #include "../w1_int.h" |
| 25 | |
Evgeny Boger | 3089a4c | 2014-01-23 15:56:18 -0800 | [diff] [blame] | 26 | static u8 w1_gpio_set_pullup(void *data, int delay) |
| 27 | { |
| 28 | struct w1_gpio_platform_data *pdata = data; |
| 29 | |
| 30 | if (delay) { |
| 31 | pdata->pullup_duration = delay; |
| 32 | } else { |
| 33 | if (pdata->pullup_duration) { |
| 34 | gpio_direction_output(pdata->pin, 1); |
| 35 | |
| 36 | msleep(pdata->pullup_duration); |
| 37 | |
| 38 | gpio_direction_input(pdata->pin); |
| 39 | } |
| 40 | pdata->pullup_duration = 0; |
| 41 | } |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 46 | static void w1_gpio_write_bit_dir(void *data, u8 bit) |
| 47 | { |
| 48 | struct w1_gpio_platform_data *pdata = data; |
| 49 | |
| 50 | if (bit) |
| 51 | gpio_direction_input(pdata->pin); |
| 52 | else |
| 53 | gpio_direction_output(pdata->pin, 0); |
| 54 | } |
| 55 | |
| 56 | static void w1_gpio_write_bit_val(void *data, u8 bit) |
| 57 | { |
| 58 | struct w1_gpio_platform_data *pdata = data; |
| 59 | |
| 60 | gpio_set_value(pdata->pin, bit); |
| 61 | } |
| 62 | |
| 63 | static u8 w1_gpio_read_bit(void *data) |
| 64 | { |
| 65 | struct w1_gpio_platform_data *pdata = data; |
| 66 | |
Daniel Mack | 8d0df7a | 2009-03-12 14:31:25 -0700 | [diff] [blame] | 67 | return gpio_get_value(pdata->pin) ? 1 : 0; |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Johan Hovold | 34ccd87 | 2013-03-08 11:08:00 +0100 | [diff] [blame] | 70 | #if defined(CONFIG_OF) |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 71 | static struct of_device_id w1_gpio_dt_ids[] = { |
| 72 | { .compatible = "w1-gpio" }, |
| 73 | {} |
| 74 | }; |
| 75 | MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids); |
Johan Hovold | 34ccd87 | 2013-03-08 11:08:00 +0100 | [diff] [blame] | 76 | #endif |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 77 | |
| 78 | static int w1_gpio_probe_dt(struct platform_device *pdev) |
| 79 | { |
Jingoo Han | c853b167 | 2013-11-14 14:32:04 -0800 | [diff] [blame] | 80 | struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 81 | struct device_node *np = pdev->dev.of_node; |
Markus Pargmann | 7cf1a12 | 2013-10-29 09:19:23 +0100 | [diff] [blame] | 82 | int gpio; |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 83 | |
| 84 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 85 | if (!pdata) |
| 86 | return -ENOMEM; |
| 87 | |
| 88 | if (of_get_property(np, "linux,open-drain", NULL)) |
| 89 | pdata->is_open_drain = 1; |
| 90 | |
Markus Pargmann | 7cf1a12 | 2013-10-29 09:19:23 +0100 | [diff] [blame] | 91 | gpio = of_get_gpio(np, 0); |
| 92 | if (gpio < 0) |
| 93 | return gpio; |
| 94 | pdata->pin = gpio; |
| 95 | |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 96 | pdata->ext_pullup_enable_pin = of_get_gpio(np, 1); |
| 97 | pdev->dev.platform_data = pdata; |
| 98 | |
| 99 | return 0; |
| 100 | } |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 101 | |
Hauke Mehrtens | 06a8f1f | 2013-01-27 21:07:57 +0100 | [diff] [blame] | 102 | static int w1_gpio_probe(struct platform_device *pdev) |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 103 | { |
| 104 | struct w1_bus_master *master; |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 105 | struct w1_gpio_platform_data *pdata; |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 106 | int err; |
| 107 | |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 108 | if (of_have_populated_dt()) { |
| 109 | err = w1_gpio_probe_dt(pdev); |
| 110 | if (err < 0) { |
| 111 | dev_err(&pdev->dev, "Failed to parse DT\n"); |
| 112 | return err; |
| 113 | } |
| 114 | } |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 115 | |
Jingoo Han | c853b167 | 2013-11-14 14:32:04 -0800 | [diff] [blame] | 116 | pdata = dev_get_platdata(&pdev->dev); |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 117 | |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 118 | if (!pdata) { |
| 119 | dev_err(&pdev->dev, "No configuration data\n"); |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 120 | return -ENXIO; |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 121 | } |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 122 | |
Markus Pargmann | d27f25c | 2013-10-29 09:19:24 +0100 | [diff] [blame] | 123 | master = devm_kzalloc(&pdev->dev, sizeof(struct w1_bus_master), |
| 124 | GFP_KERNEL); |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 125 | if (!master) { |
| 126 | dev_err(&pdev->dev, "Out of memory\n"); |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 127 | return -ENOMEM; |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 128 | } |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 129 | |
Markus Pargmann | d27f25c | 2013-10-29 09:19:24 +0100 | [diff] [blame] | 130 | err = devm_gpio_request(&pdev->dev, pdata->pin, "w1"); |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 131 | if (err) { |
| 132 | dev_err(&pdev->dev, "gpio_request (pin) failed\n"); |
Markus Pargmann | d27f25c | 2013-10-29 09:19:24 +0100 | [diff] [blame] | 133 | return err; |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 134 | } |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 135 | |
Daniel Mack | d2323cf | 2012-07-25 22:54:29 +0200 | [diff] [blame] | 136 | if (gpio_is_valid(pdata->ext_pullup_enable_pin)) { |
Markus Pargmann | d27f25c | 2013-10-29 09:19:24 +0100 | [diff] [blame] | 137 | err = devm_gpio_request_one(&pdev->dev, |
| 138 | pdata->ext_pullup_enable_pin, GPIOF_INIT_LOW, |
| 139 | "w1 pullup"); |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 140 | if (err < 0) { |
| 141 | dev_err(&pdev->dev, "gpio_request_one " |
| 142 | "(ext_pullup_enable_pin) failed\n"); |
Markus Pargmann | d27f25c | 2013-10-29 09:19:24 +0100 | [diff] [blame] | 143 | return err; |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 144 | } |
Daniel Mack | d2323cf | 2012-07-25 22:54:29 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 147 | master->data = pdata; |
| 148 | master->read_bit = w1_gpio_read_bit; |
| 149 | |
| 150 | if (pdata->is_open_drain) { |
| 151 | gpio_direction_output(pdata->pin, 1); |
| 152 | master->write_bit = w1_gpio_write_bit_val; |
| 153 | } else { |
| 154 | gpio_direction_input(pdata->pin); |
| 155 | master->write_bit = w1_gpio_write_bit_dir; |
Evgeny Boger | 3089a4c | 2014-01-23 15:56:18 -0800 | [diff] [blame] | 156 | master->set_pullup = w1_gpio_set_pullup; |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | err = w1_add_master_device(master); |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 160 | if (err) { |
| 161 | dev_err(&pdev->dev, "w1_add_master device failed\n"); |
Markus Pargmann | d27f25c | 2013-10-29 09:19:24 +0100 | [diff] [blame] | 162 | return err; |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 163 | } |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 164 | |
Daniel Mack | c8a06c1 | 2009-06-17 16:28:15 -0700 | [diff] [blame] | 165 | if (pdata->enable_external_pullup) |
| 166 | pdata->enable_external_pullup(1); |
| 167 | |
Daniel Mack | d2323cf | 2012-07-25 22:54:29 +0200 | [diff] [blame] | 168 | if (gpio_is_valid(pdata->ext_pullup_enable_pin)) |
| 169 | gpio_set_value(pdata->ext_pullup_enable_pin, 1); |
| 170 | |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 171 | platform_set_drvdata(pdev, master); |
| 172 | |
| 173 | return 0; |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Johan Hovold | 0123055 | 2013-03-08 11:07:59 +0100 | [diff] [blame] | 176 | static int w1_gpio_remove(struct platform_device *pdev) |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 177 | { |
| 178 | struct w1_bus_master *master = platform_get_drvdata(pdev); |
Jingoo Han | c853b167 | 2013-11-14 14:32:04 -0800 | [diff] [blame] | 179 | struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 180 | |
Daniel Mack | c8a06c1 | 2009-06-17 16:28:15 -0700 | [diff] [blame] | 181 | if (pdata->enable_external_pullup) |
| 182 | pdata->enable_external_pullup(0); |
| 183 | |
Daniel Mack | d2323cf | 2012-07-25 22:54:29 +0200 | [diff] [blame] | 184 | if (gpio_is_valid(pdata->ext_pullup_enable_pin)) |
| 185 | gpio_set_value(pdata->ext_pullup_enable_pin, 0); |
| 186 | |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 187 | w1_remove_master_device(master); |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Daniel Mack | c8a06c1 | 2009-06-17 16:28:15 -0700 | [diff] [blame] | 192 | #ifdef CONFIG_PM |
| 193 | |
| 194 | static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state) |
| 195 | { |
Jingoo Han | c853b167 | 2013-11-14 14:32:04 -0800 | [diff] [blame] | 196 | struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Daniel Mack | c8a06c1 | 2009-06-17 16:28:15 -0700 | [diff] [blame] | 197 | |
| 198 | if (pdata->enable_external_pullup) |
| 199 | pdata->enable_external_pullup(0); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int w1_gpio_resume(struct platform_device *pdev) |
| 205 | { |
Jingoo Han | c853b167 | 2013-11-14 14:32:04 -0800 | [diff] [blame] | 206 | struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Daniel Mack | c8a06c1 | 2009-06-17 16:28:15 -0700 | [diff] [blame] | 207 | |
| 208 | if (pdata->enable_external_pullup) |
| 209 | pdata->enable_external_pullup(1); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | #else |
| 215 | #define w1_gpio_suspend NULL |
| 216 | #define w1_gpio_resume NULL |
| 217 | #endif |
| 218 | |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 219 | static struct platform_driver w1_gpio_driver = { |
| 220 | .driver = { |
| 221 | .name = "w1-gpio", |
| 222 | .owner = THIS_MODULE, |
Daniel Mack | 5f3d138 | 2012-07-23 16:36:35 +0200 | [diff] [blame] | 223 | .of_match_table = of_match_ptr(w1_gpio_dt_ids), |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 224 | }, |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 225 | .probe = w1_gpio_probe, |
Johan Hovold | 0123055 | 2013-03-08 11:07:59 +0100 | [diff] [blame] | 226 | .remove = w1_gpio_remove, |
Daniel Mack | c8a06c1 | 2009-06-17 16:28:15 -0700 | [diff] [blame] | 227 | .suspend = w1_gpio_suspend, |
| 228 | .resume = w1_gpio_resume, |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 229 | }; |
| 230 | |
Pantelis Antoniou | 8a1861d | 2012-11-22 01:13:47 +0400 | [diff] [blame] | 231 | module_platform_driver(w1_gpio_driver); |
Ville Syrjala | ad8dc96 | 2008-02-06 01:39:01 -0800 | [diff] [blame] | 232 | |
| 233 | MODULE_DESCRIPTION("GPIO w1 bus master driver"); |
| 234 | MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>"); |
| 235 | MODULE_LICENSE("GPL"); |