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