w1-gpio.c (c25141062a82ae8bddced1b3ce2b57a1c0efabe0) w1-gpio.c (e0fc62a6552f3d9c21e73cc65844f9aad1892cf7)
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>
14#include <linux/slab.h>
15#include <linux/w1-gpio.h>
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>
14#include <linux/slab.h>
15#include <linux/w1-gpio.h>
16#include <linux/gpio.h>
16#include <linux/gpio/consumer.h>
17#include <linux/of_platform.h>
17#include <linux/of_platform.h>
18#include <linux/of_gpio.h>
19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/delay.h>
22
23#include <linux/w1.h>
24
25static u8 w1_gpio_set_pullup(void *data, int delay)
26{
27 struct w1_gpio_platform_data *pdata = data;
28
29 if (delay) {
30 pdata->pullup_duration = delay;
31 } else {
32 if (pdata->pullup_duration) {
18#include <linux/err.h>
19#include <linux/of.h>
20#include <linux/delay.h>
21
22#include <linux/w1.h>
23
24static u8 w1_gpio_set_pullup(void *data, int delay)
25{
26 struct w1_gpio_platform_data *pdata = data;
27
28 if (delay) {
29 pdata->pullup_duration = delay;
30 } else {
31 if (pdata->pullup_duration) {
33 gpio_direction_output(pdata->pin, 1);
34
32 /*
33 * This will OVERRIDE open drain emulation and force-pull
34 * the line high for some time.
35 */
36 gpiod_set_raw_value(pdata->gpiod, 1);
35 msleep(pdata->pullup_duration);
37 msleep(pdata->pullup_duration);
36
37 gpio_direction_input(pdata->pin);
38 /*
39 * This will simply set the line as input since we are doing
40 * open drain emulation in the GPIO library.
41 */
42 gpiod_set_value(pdata->gpiod, 1);
38 }
39 pdata->pullup_duration = 0;
40 }
41
42 return 0;
43}
44
43 }
44 pdata->pullup_duration = 0;
45 }
46
47 return 0;
48}
49
45static void w1_gpio_write_bit_dir(void *data, u8 bit)
50static void w1_gpio_write_bit(void *data, u8 bit)
46{
47 struct w1_gpio_platform_data *pdata = data;
48
51{
52 struct w1_gpio_platform_data *pdata = data;
53
49 if (bit)
50 gpio_direction_input(pdata->pin);
51 else
52 gpio_direction_output(pdata->pin, 0);
54 gpiod_set_value(pdata->gpiod, bit);
53}
54
55}
56
55static void w1_gpio_write_bit_val(void *data, u8 bit)
56{
57 struct w1_gpio_platform_data *pdata = data;
58
59 gpio_set_value(pdata->pin, bit);
60}
61
62static u8 w1_gpio_read_bit(void *data)
63{
64 struct w1_gpio_platform_data *pdata = data;
65
57static u8 w1_gpio_read_bit(void *data)
58{
59 struct w1_gpio_platform_data *pdata = data;
60
66 return gpio_get_value(pdata->pin) ? 1 : 0;
61 return gpiod_get_value(pdata->gpiod) ? 1 : 0;
67}
68
69#if defined(CONFIG_OF)
70static const struct of_device_id w1_gpio_dt_ids[] = {
71 { .compatible = "w1-gpio" },
72 {}
73};
74MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
75#endif
76
62}
63
64#if defined(CONFIG_OF)
65static const struct of_device_id w1_gpio_dt_ids[] = {
66 { .compatible = "w1-gpio" },
67 {}
68};
69MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
70#endif
71
77static int w1_gpio_probe_dt(struct platform_device *pdev)
78{
79 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
80 struct device_node *np = pdev->dev.of_node;
81 int gpio;
82
83 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
84 if (!pdata)
85 return -ENOMEM;
86
87 if (of_get_property(np, "linux,open-drain", NULL))
88 pdata->is_open_drain = 1;
89
90 gpio = of_get_gpio(np, 0);
91 if (gpio < 0) {
92 if (gpio != -EPROBE_DEFER)
93 dev_err(&pdev->dev,
94 "Failed to parse gpio property for data pin (%d)\n",
95 gpio);
96
97 return gpio;
98 }
99 pdata->pin = gpio;
100
101 gpio = of_get_gpio(np, 1);
102 if (gpio == -EPROBE_DEFER)
103 return gpio;
104 /* ignore other errors as the pullup gpio is optional */
105 pdata->ext_pullup_enable_pin = gpio;
106
107 pdev->dev.platform_data = pdata;
108
109 return 0;
110}
111
112static int w1_gpio_probe(struct platform_device *pdev)
113{
114 struct w1_bus_master *master;
115 struct w1_gpio_platform_data *pdata;
72static int w1_gpio_probe(struct platform_device *pdev)
73{
74 struct w1_bus_master *master;
75 struct w1_gpio_platform_data *pdata;
76 struct device *dev = &pdev->dev;
77 struct device_node *np = dev->of_node;
78 /* Enforce open drain mode by default */
79 enum gpiod_flags gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
116 int err;
117
118 if (of_have_populated_dt()) {
80 int err;
81
82 if (of_have_populated_dt()) {
119 err = w1_gpio_probe_dt(pdev);
120 if (err < 0)
121 return err;
122 }
83 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
84 if (!pdata)
85 return -ENOMEM;
123
86
124 pdata = dev_get_platdata(&pdev->dev);
87 /*
88 * This parameter means that something else than the gpiolib has
89 * already set the line into open drain mode, so we should just
90 * driver it high/low like we are in full control of the line and
91 * open drain will happen transparently.
92 */
93 if (of_get_property(np, "linux,open-drain", NULL))
94 gflags = GPIOD_OUT_LOW;
125
95
96 pdev->dev.platform_data = pdata;
97 }
98 pdata = dev_get_platdata(dev);
99
126 if (!pdata) {
100 if (!pdata) {
127 dev_err(&pdev->dev, "No configuration data\n");
101 dev_err(dev, "No configuration data\n");
128 return -ENXIO;
129 }
130
102 return -ENXIO;
103 }
104
131 master = devm_kzalloc(&pdev->dev, sizeof(struct w1_bus_master),
105 master = devm_kzalloc(dev, sizeof(struct w1_bus_master),
132 GFP_KERNEL);
133 if (!master) {
106 GFP_KERNEL);
107 if (!master) {
134 dev_err(&pdev->dev, "Out of memory\n");
108 dev_err(dev, "Out of memory\n");
135 return -ENOMEM;
136 }
137
109 return -ENOMEM;
110 }
111
138 err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
139 if (err) {
140 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
141 return err;
112 pdata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);
113 if (IS_ERR(pdata->gpiod)) {
114 dev_err(dev, "gpio_request (pin) failed\n");
115 return PTR_ERR(pdata->gpiod);
142 }
143
116 }
117
144 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
145 err = devm_gpio_request_one(&pdev->dev,
146 pdata->ext_pullup_enable_pin, GPIOF_INIT_LOW,
147 "w1 pullup");
148 if (err < 0) {
149 dev_err(&pdev->dev, "gpio_request_one "
150 "(ext_pullup_enable_pin) failed\n");
151 return err;
152 }
118 pdata->pullup_gpiod =
119 devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);
120 if (IS_ERR(pdata->pullup_gpiod)) {
121 dev_err(dev, "gpio_request_one "
122 "(ext_pullup_enable_pin) failed\n");
123 return PTR_ERR(pdata->pullup_gpiod);
153 }
154
155 master->data = pdata;
156 master->read_bit = w1_gpio_read_bit;
124 }
125
126 master->data = pdata;
127 master->read_bit = w1_gpio_read_bit;
128 gpiod_direction_output(pdata->gpiod, 1);
129 master->write_bit = w1_gpio_write_bit;
157
130
158 if (pdata->is_open_drain) {
159 gpio_direction_output(pdata->pin, 1);
160 master->write_bit = w1_gpio_write_bit_val;
161 } else {
162 gpio_direction_input(pdata->pin);
163 master->write_bit = w1_gpio_write_bit_dir;
131 /*
132 * If we are using open drain emulation from the GPIO library,
133 * we need to use this pullup function that hammers the line
134 * high using a raw accessor to provide pull-up for the w1
135 * line.
136 */
137 if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
164 master->set_pullup = w1_gpio_set_pullup;
138 master->set_pullup = w1_gpio_set_pullup;
165 }
166
167 err = w1_add_master_device(master);
168 if (err) {
139
140 err = w1_add_master_device(master);
141 if (err) {
169 dev_err(&pdev->dev, "w1_add_master device failed\n");
142 dev_err(dev, "w1_add_master device failed\n");
170 return err;
171 }
172
173 if (pdata->enable_external_pullup)
174 pdata->enable_external_pullup(1);
175
143 return err;
144 }
145
146 if (pdata->enable_external_pullup)
147 pdata->enable_external_pullup(1);
148
176 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
177 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
149 if (pdata->pullup_gpiod)
150 gpiod_set_value(pdata->pullup_gpiod, 1);
178
179 platform_set_drvdata(pdev, master);
180
181 return 0;
182}
183
184static int w1_gpio_remove(struct platform_device *pdev)
185{
186 struct w1_bus_master *master = platform_get_drvdata(pdev);
187 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
188
189 if (pdata->enable_external_pullup)
190 pdata->enable_external_pullup(0);
191
151
152 platform_set_drvdata(pdev, master);
153
154 return 0;
155}
156
157static int 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 = dev_get_platdata(&pdev->dev);
161
162 if (pdata->enable_external_pullup)
163 pdata->enable_external_pullup(0);
164
192 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
193 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
165 if (pdata->pullup_gpiod)
166 gpiod_set_value(pdata->pullup_gpiod, 0);
194
195 w1_remove_master_device(master);
196
197 return 0;
198}
199
200static int __maybe_unused w1_gpio_suspend(struct device *dev)
201{

--- 35 unchanged lines hidden ---
167
168 w1_remove_master_device(master);
169
170 return 0;
171}
172
173static int __maybe_unused w1_gpio_suspend(struct device *dev)
174{

--- 35 unchanged lines hidden ---