blob: f54ece268c982d1dce13224c78efce9434dbffbb [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>
Ville Syrjalaad8dc962008-02-06 01:39:01 -080021
22#include "../w1.h"
23#include "../w1_int.h"
24
Ville Syrjalaad8dc962008-02-06 01:39:01 -080025static 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
35static 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
42static u8 w1_gpio_read_bit(void *data)
43{
44 struct w1_gpio_platform_data *pdata = data;
45
Daniel Mack8d0df7a2009-03-12 14:31:25 -070046 return gpio_get_value(pdata->pin) ? 1 : 0;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080047}
48
Johan Hovold34ccd872013-03-08 11:08:00 +010049#if defined(CONFIG_OF)
Daniel Mack5f3d1382012-07-23 16:36:35 +020050static struct of_device_id w1_gpio_dt_ids[] = {
51 { .compatible = "w1-gpio" },
52 {}
53};
54MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
Johan Hovold34ccd872013-03-08 11:08:00 +010055#endif
Daniel Mack5f3d1382012-07-23 16:36:35 +020056
57static int w1_gpio_probe_dt(struct platform_device *pdev)
58{
59 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
60 struct device_node *np = pdev->dev.of_node;
Daniel Mack5f3d1382012-07-23 16:36:35 +020061
62 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
63 if (!pdata)
64 return -ENOMEM;
65
66 if (of_get_property(np, "linux,open-drain", NULL))
67 pdata->is_open_drain = 1;
68
69 pdata->pin = of_get_gpio(np, 0);
70 pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
71 pdev->dev.platform_data = pdata;
72
73 return 0;
74}
Daniel Mack5f3d1382012-07-23 16:36:35 +020075
Hauke Mehrtens06a8f1f2013-01-27 21:07:57 +010076static int w1_gpio_probe(struct platform_device *pdev)
Ville Syrjalaad8dc962008-02-06 01:39:01 -080077{
78 struct w1_bus_master *master;
Daniel Mack5f3d1382012-07-23 16:36:35 +020079 struct w1_gpio_platform_data *pdata;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080080 int err;
81
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040082 if (of_have_populated_dt()) {
83 err = w1_gpio_probe_dt(pdev);
84 if (err < 0) {
85 dev_err(&pdev->dev, "Failed to parse DT\n");
86 return err;
87 }
88 }
Daniel Mack5f3d1382012-07-23 16:36:35 +020089
90 pdata = pdev->dev.platform_data;
91
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040092 if (!pdata) {
93 dev_err(&pdev->dev, "No configuration data\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -080094 return -ENXIO;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040095 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -080096
97 master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040098 if (!master) {
99 dev_err(&pdev->dev, "Out of memory\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800100 return -ENOMEM;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400101 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800102
103 err = gpio_request(pdata->pin, "w1");
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400104 if (err) {
105 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800106 goto free_master;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400107 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800108
Daniel Mackd2323cf2012-07-25 22:54:29 +0200109 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
110 err = gpio_request_one(pdata->ext_pullup_enable_pin,
111 GPIOF_INIT_LOW, "w1 pullup");
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400112 if (err < 0) {
113 dev_err(&pdev->dev, "gpio_request_one "
114 "(ext_pullup_enable_pin) failed\n");
Daniel Mackd2323cf2012-07-25 22:54:29 +0200115 goto free_gpio;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400116 }
Daniel Mackd2323cf2012-07-25 22:54:29 +0200117 }
118
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800119 master->data = pdata;
120 master->read_bit = w1_gpio_read_bit;
121
122 if (pdata->is_open_drain) {
123 gpio_direction_output(pdata->pin, 1);
124 master->write_bit = w1_gpio_write_bit_val;
125 } else {
126 gpio_direction_input(pdata->pin);
127 master->write_bit = w1_gpio_write_bit_dir;
128 }
129
130 err = w1_add_master_device(master);
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400131 if (err) {
132 dev_err(&pdev->dev, "w1_add_master device failed\n");
Daniel Mackd2323cf2012-07-25 22:54:29 +0200133 goto free_gpio_ext_pu;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400134 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800135
Daniel Mackc8a06c12009-06-17 16:28:15 -0700136 if (pdata->enable_external_pullup)
137 pdata->enable_external_pullup(1);
138
Daniel Mackd2323cf2012-07-25 22:54:29 +0200139 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
140 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
141
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800142 platform_set_drvdata(pdev, master);
143
144 return 0;
145
Daniel Mackd2323cf2012-07-25 22:54:29 +0200146 free_gpio_ext_pu:
147 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
148 gpio_free(pdata->ext_pullup_enable_pin);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800149 free_gpio:
150 gpio_free(pdata->pin);
151 free_master:
152 kfree(master);
153
154 return err;
155}
156
Johan Hovold01230552013-03-08 11:07:59 +0100157static int w1_gpio_remove(struct platform_device *pdev)
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800158{
159 struct w1_bus_master *master = platform_get_drvdata(pdev);
160 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
161
Daniel Mackc8a06c12009-06-17 16:28:15 -0700162 if (pdata->enable_external_pullup)
163 pdata->enable_external_pullup(0);
164
Daniel Mackd2323cf2012-07-25 22:54:29 +0200165 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
166 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
167
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800168 w1_remove_master_device(master);
169 gpio_free(pdata->pin);
170 kfree(master);
171
172 return 0;
173}
174
Daniel Mackc8a06c12009-06-17 16:28:15 -0700175#ifdef CONFIG_PM
176
177static 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
187static 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 Syrjalaad8dc962008-02-06 01:39:01 -0800202static struct platform_driver w1_gpio_driver = {
203 .driver = {
204 .name = "w1-gpio",
205 .owner = THIS_MODULE,
Daniel Mack5f3d1382012-07-23 16:36:35 +0200206 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800207 },
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400208 .probe = w1_gpio_probe,
Johan Hovold01230552013-03-08 11:07:59 +0100209 .remove = w1_gpio_remove,
Daniel Mackc8a06c12009-06-17 16:28:15 -0700210 .suspend = w1_gpio_suspend,
211 .resume = w1_gpio_resume,
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800212};
213
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400214module_platform_driver(w1_gpio_driver);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800215
216MODULE_DESCRIPTION("GPIO w1 bus master driver");
217MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
218MODULE_LICENSE("GPL");