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