blob: 1d111e56c8c8c6c4a49badcd5f92febcf5e666d3 [file] [log] [blame]
Ville Syrjalaad8dc962008-02-06 01:39:01 -08001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Ville Syrjalaad8dc962008-02-06 01:39:01 -080015#include <linux/w1-gpio.h>
Mark Browne250b342012-02-04 16:33:55 +000016#include <linux/gpio.h>
Daniel Mack5f3d1382012-07-23 16:36:35 +020017#include <linux/of_platform.h>
18#include <linux/of_gpio.h>
Pantelis Antoniou277ed0d2012-11-22 01:13:29 +040019#include <linux/err.h>
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040020#include <linux/of.h>
Evgeny Boger3089a4c2014-01-23 15:56:18 -080021#include <linux/delay.h>
Ville Syrjalaad8dc962008-02-06 01:39:01 -080022
23#include "../w1.h"
24#include "../w1_int.h"
25
Evgeny Boger3089a4c2014-01-23 15:56:18 -080026static 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 Syrjalaad8dc962008-02-06 01:39:01 -080046static 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
56static 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
63static u8 w1_gpio_read_bit(void *data)
64{
65 struct w1_gpio_platform_data *pdata = data;
66
Daniel Mack8d0df7a2009-03-12 14:31:25 -070067 return gpio_get_value(pdata->pin) ? 1 : 0;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080068}
69
Johan Hovold34ccd872013-03-08 11:08:00 +010070#if defined(CONFIG_OF)
Daniel Mack5f3d1382012-07-23 16:36:35 +020071static struct of_device_id w1_gpio_dt_ids[] = {
72 { .compatible = "w1-gpio" },
73 {}
74};
75MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
Johan Hovold34ccd872013-03-08 11:08:00 +010076#endif
Daniel Mack5f3d1382012-07-23 16:36:35 +020077
78static int w1_gpio_probe_dt(struct platform_device *pdev)
79{
Jingoo Hanc853b1672013-11-14 14:32:04 -080080 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Mack5f3d1382012-07-23 16:36:35 +020081 struct device_node *np = pdev->dev.of_node;
Markus Pargmann7cf1a122013-10-29 09:19:23 +010082 int gpio;
Daniel Mack5f3d1382012-07-23 16:36:35 +020083
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 Pargmann7cf1a122013-10-29 09:19:23 +010091 gpio = of_get_gpio(np, 0);
Uwe Kleine-König0b336ce2014-02-13 23:05:28 +010092 if (gpio < 0) {
93 if (gpio != -EPROBE_DEFER)
94 dev_err(&pdev->dev,
95 "Failed to parse gpio property for data pin (%d)\n",
96 gpio);
97
Markus Pargmann7cf1a122013-10-29 09:19:23 +010098 return gpio;
Uwe Kleine-König0b336ce2014-02-13 23:05:28 +010099 }
Markus Pargmann7cf1a122013-10-29 09:19:23 +0100100 pdata->pin = gpio;
101
Uwe Kleine-König0b336ce2014-02-13 23:05:28 +0100102 gpio = of_get_gpio(np, 1);
103 if (gpio == -EPROBE_DEFER)
104 return gpio;
105 /* ignore other errors as the pullup gpio is optional */
106 pdata->ext_pullup_enable_pin = gpio;
107
Daniel Mack5f3d1382012-07-23 16:36:35 +0200108 pdev->dev.platform_data = pdata;
109
110 return 0;
111}
Daniel Mack5f3d1382012-07-23 16:36:35 +0200112
Hauke Mehrtens06a8f1f2013-01-27 21:07:57 +0100113static int w1_gpio_probe(struct platform_device *pdev)
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800114{
115 struct w1_bus_master *master;
Daniel Mack5f3d1382012-07-23 16:36:35 +0200116 struct w1_gpio_platform_data *pdata;
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800117 int err;
118
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400119 if (of_have_populated_dt()) {
120 err = w1_gpio_probe_dt(pdev);
Uwe Kleine-König0b336ce2014-02-13 23:05:28 +0100121 if (err < 0)
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400122 return err;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400123 }
Daniel Mack5f3d1382012-07-23 16:36:35 +0200124
Jingoo Hanc853b1672013-11-14 14:32:04 -0800125 pdata = dev_get_platdata(&pdev->dev);
Daniel Mack5f3d1382012-07-23 16:36:35 +0200126
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400127 if (!pdata) {
128 dev_err(&pdev->dev, "No configuration data\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800129 return -ENXIO;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400130 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800131
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100132 master = devm_kzalloc(&pdev->dev, sizeof(struct w1_bus_master),
133 GFP_KERNEL);
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400134 if (!master) {
135 dev_err(&pdev->dev, "Out of memory\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800136 return -ENOMEM;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400137 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800138
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100139 err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400140 if (err) {
141 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100142 return err;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400143 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800144
Daniel Mackd2323cf2012-07-25 22:54:29 +0200145 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100146 err = devm_gpio_request_one(&pdev->dev,
147 pdata->ext_pullup_enable_pin, GPIOF_INIT_LOW,
148 "w1 pullup");
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400149 if (err < 0) {
150 dev_err(&pdev->dev, "gpio_request_one "
151 "(ext_pullup_enable_pin) failed\n");
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100152 return err;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400153 }
Daniel Mackd2323cf2012-07-25 22:54:29 +0200154 }
155
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800156 master->data = pdata;
157 master->read_bit = w1_gpio_read_bit;
158
159 if (pdata->is_open_drain) {
160 gpio_direction_output(pdata->pin, 1);
161 master->write_bit = w1_gpio_write_bit_val;
162 } else {
163 gpio_direction_input(pdata->pin);
164 master->write_bit = w1_gpio_write_bit_dir;
Evgeny Boger3089a4c2014-01-23 15:56:18 -0800165 master->set_pullup = w1_gpio_set_pullup;
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800166 }
167
168 err = w1_add_master_device(master);
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400169 if (err) {
170 dev_err(&pdev->dev, "w1_add_master device failed\n");
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100171 return err;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400172 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800173
Daniel Mackc8a06c12009-06-17 16:28:15 -0700174 if (pdata->enable_external_pullup)
175 pdata->enable_external_pullup(1);
176
Daniel Mackd2323cf2012-07-25 22:54:29 +0200177 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
178 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
179
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800180 platform_set_drvdata(pdev, master);
181
182 return 0;
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800183}
184
Johan Hovold01230552013-03-08 11:07:59 +0100185static int w1_gpio_remove(struct platform_device *pdev)
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800186{
187 struct w1_bus_master *master = platform_get_drvdata(pdev);
Jingoo Hanc853b1672013-11-14 14:32:04 -0800188 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800189
Daniel Mackc8a06c12009-06-17 16:28:15 -0700190 if (pdata->enable_external_pullup)
191 pdata->enable_external_pullup(0);
192
Daniel Mackd2323cf2012-07-25 22:54:29 +0200193 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
194 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
195
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800196 w1_remove_master_device(master);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800197
198 return 0;
199}
200
Daniel Mackc8a06c12009-06-17 16:28:15 -0700201#ifdef CONFIG_PM
202
203static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
204{
Jingoo Hanc853b1672013-11-14 14:32:04 -0800205 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Mackc8a06c12009-06-17 16:28:15 -0700206
207 if (pdata->enable_external_pullup)
208 pdata->enable_external_pullup(0);
209
210 return 0;
211}
212
213static int w1_gpio_resume(struct platform_device *pdev)
214{
Jingoo Hanc853b1672013-11-14 14:32:04 -0800215 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Mackc8a06c12009-06-17 16:28:15 -0700216
217 if (pdata->enable_external_pullup)
218 pdata->enable_external_pullup(1);
219
220 return 0;
221}
222
223#else
224#define w1_gpio_suspend NULL
225#define w1_gpio_resume NULL
226#endif
227
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800228static struct platform_driver w1_gpio_driver = {
229 .driver = {
230 .name = "w1-gpio",
231 .owner = THIS_MODULE,
Daniel Mack5f3d1382012-07-23 16:36:35 +0200232 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800233 },
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400234 .probe = w1_gpio_probe,
Johan Hovold01230552013-03-08 11:07:59 +0100235 .remove = w1_gpio_remove,
Daniel Mackc8a06c12009-06-17 16:28:15 -0700236 .suspend = w1_gpio_suspend,
237 .resume = w1_gpio_resume,
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800238};
239
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400240module_platform_driver(w1_gpio_driver);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800241
242MODULE_DESCRIPTION("GPIO w1 bus master driver");
243MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
244MODULE_LICENSE("GPL");