blob: d39dfa4cc23570b9c6cc8e57e7aa3a8d759a007b [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/pinctrl/consumer.h>
20#include <linux/err.h>
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040021#include <linux/of.h>
Ville Syrjalaad8dc962008-02-06 01:39:01 -080022
23#include "../w1.h"
24#include "../w1_int.h"
25
Ville Syrjalaad8dc962008-02-06 01:39:01 -080026static 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
36static 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
43static u8 w1_gpio_read_bit(void *data)
44{
45 struct w1_gpio_platform_data *pdata = data;
46
Daniel Mack8d0df7a2009-03-12 14:31:25 -070047 return gpio_get_value(pdata->pin) ? 1 : 0;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080048}
49
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);
55
56static 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;
Daniel Mack5f3d1382012-07-23 16:36:35 +020060
61 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
62 if (!pdata)
63 return -ENOMEM;
64
65 if (of_get_property(np, "linux,open-drain", NULL))
66 pdata->is_open_drain = 1;
67
68 pdata->pin = of_get_gpio(np, 0);
69 pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
70 pdev->dev.platform_data = pdata;
71
72 return 0;
73}
Daniel Mack5f3d1382012-07-23 16:36:35 +020074
Hauke Mehrtens06a8f1f2013-01-27 21:07:57 +010075static int w1_gpio_probe(struct platform_device *pdev)
Ville Syrjalaad8dc962008-02-06 01:39:01 -080076{
77 struct w1_bus_master *master;
Daniel Mack5f3d1382012-07-23 16:36:35 +020078 struct w1_gpio_platform_data *pdata;
Pantelis Antoniou277ed0d2012-11-22 01:13:29 +040079 struct pinctrl *pinctrl;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080080 int err;
81
Pantelis Antoniou277ed0d2012-11-22 01:13:29 +040082 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
83 if (IS_ERR(pinctrl))
84 dev_warn(&pdev->dev, "unable to select pin group\n");
85
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040086 if (of_have_populated_dt()) {
87 err = w1_gpio_probe_dt(pdev);
88 if (err < 0) {
89 dev_err(&pdev->dev, "Failed to parse DT\n");
90 return err;
91 }
92 }
Daniel Mack5f3d1382012-07-23 16:36:35 +020093
94 pdata = pdev->dev.platform_data;
95
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040096 if (!pdata) {
97 dev_err(&pdev->dev, "No configuration data\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -080098 return -ENXIO;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040099 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800100
101 master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400102 if (!master) {
103 dev_err(&pdev->dev, "Out of memory\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800104 return -ENOMEM;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400105 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800106
107 err = gpio_request(pdata->pin, "w1");
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400108 if (err) {
109 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800110 goto free_master;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400111 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800112
Daniel Mackd2323cf2012-07-25 22:54:29 +0200113 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
114 err = gpio_request_one(pdata->ext_pullup_enable_pin,
115 GPIOF_INIT_LOW, "w1 pullup");
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400116 if (err < 0) {
117 dev_err(&pdev->dev, "gpio_request_one "
118 "(ext_pullup_enable_pin) failed\n");
Daniel Mackd2323cf2012-07-25 22:54:29 +0200119 goto free_gpio;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400120 }
Daniel Mackd2323cf2012-07-25 22:54:29 +0200121 }
122
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800123 master->data = pdata;
124 master->read_bit = w1_gpio_read_bit;
125
126 if (pdata->is_open_drain) {
127 gpio_direction_output(pdata->pin, 1);
128 master->write_bit = w1_gpio_write_bit_val;
129 } else {
130 gpio_direction_input(pdata->pin);
131 master->write_bit = w1_gpio_write_bit_dir;
132 }
133
134 err = w1_add_master_device(master);
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400135 if (err) {
136 dev_err(&pdev->dev, "w1_add_master device failed\n");
Daniel Mackd2323cf2012-07-25 22:54:29 +0200137 goto free_gpio_ext_pu;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400138 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800139
Daniel Mackc8a06c12009-06-17 16:28:15 -0700140 if (pdata->enable_external_pullup)
141 pdata->enable_external_pullup(1);
142
Daniel Mackd2323cf2012-07-25 22:54:29 +0200143 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
144 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
145
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800146 platform_set_drvdata(pdev, master);
147
148 return 0;
149
Daniel Mackd2323cf2012-07-25 22:54:29 +0200150 free_gpio_ext_pu:
151 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
152 gpio_free(pdata->ext_pullup_enable_pin);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800153 free_gpio:
154 gpio_free(pdata->pin);
155 free_master:
156 kfree(master);
157
158 return err;
159}
160
161static int __exit w1_gpio_remove(struct platform_device *pdev)
162{
163 struct w1_bus_master *master = platform_get_drvdata(pdev);
164 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
165
Daniel Mackc8a06c12009-06-17 16:28:15 -0700166 if (pdata->enable_external_pullup)
167 pdata->enable_external_pullup(0);
168
Daniel Mackd2323cf2012-07-25 22:54:29 +0200169 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
170 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
171
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800172 w1_remove_master_device(master);
173 gpio_free(pdata->pin);
174 kfree(master);
175
176 return 0;
177}
178
Daniel Mackc8a06c12009-06-17 16:28:15 -0700179#ifdef CONFIG_PM
180
181static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
182{
183 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
184
185 if (pdata->enable_external_pullup)
186 pdata->enable_external_pullup(0);
187
188 return 0;
189}
190
191static int w1_gpio_resume(struct platform_device *pdev)
192{
193 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
194
195 if (pdata->enable_external_pullup)
196 pdata->enable_external_pullup(1);
197
198 return 0;
199}
200
201#else
202#define w1_gpio_suspend NULL
203#define w1_gpio_resume NULL
204#endif
205
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800206static struct platform_driver w1_gpio_driver = {
207 .driver = {
208 .name = "w1-gpio",
209 .owner = THIS_MODULE,
Daniel Mack5f3d1382012-07-23 16:36:35 +0200210 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800211 },
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400212 .probe = w1_gpio_probe,
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800213 .remove = __exit_p(w1_gpio_remove),
Daniel Mackc8a06c12009-06-17 16:28:15 -0700214 .suspend = w1_gpio_suspend,
215 .resume = w1_gpio_resume,
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800216};
217
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400218module_platform_driver(w1_gpio_driver);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800219
220MODULE_DESCRIPTION("GPIO w1 bus master driver");
221MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
222MODULE_LICENSE("GPL");