blob: e36b18b2817b9e655f524d79e8edf753cabece68 [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{
Jingoo Hanc853b1672013-11-14 14:32:04 -080059 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Mack5f3d1382012-07-23 16:36:35 +020060 struct device_node *np = pdev->dev.of_node;
Markus Pargmann7cf1a122013-10-29 09:19:23 +010061 int gpio;
Daniel Mack5f3d1382012-07-23 16:36:35 +020062
63 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
64 if (!pdata)
65 return -ENOMEM;
66
67 if (of_get_property(np, "linux,open-drain", NULL))
68 pdata->is_open_drain = 1;
69
Markus Pargmann7cf1a122013-10-29 09:19:23 +010070 gpio = of_get_gpio(np, 0);
71 if (gpio < 0)
72 return gpio;
73 pdata->pin = gpio;
74
Daniel Mack5f3d1382012-07-23 16:36:35 +020075 pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
76 pdev->dev.platform_data = pdata;
77
78 return 0;
79}
Daniel Mack5f3d1382012-07-23 16:36:35 +020080
Hauke Mehrtens06a8f1f2013-01-27 21:07:57 +010081static int w1_gpio_probe(struct platform_device *pdev)
Ville Syrjalaad8dc962008-02-06 01:39:01 -080082{
83 struct w1_bus_master *master;
Daniel Mack5f3d1382012-07-23 16:36:35 +020084 struct w1_gpio_platform_data *pdata;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080085 int err;
86
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040087 if (of_have_populated_dt()) {
88 err = w1_gpio_probe_dt(pdev);
89 if (err < 0) {
90 dev_err(&pdev->dev, "Failed to parse DT\n");
91 return err;
92 }
93 }
Daniel Mack5f3d1382012-07-23 16:36:35 +020094
Jingoo Hanc853b1672013-11-14 14:32:04 -080095 pdata = dev_get_platdata(&pdev->dev);
Daniel Mack5f3d1382012-07-23 16:36:35 +020096
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +040097 if (!pdata) {
98 dev_err(&pdev->dev, "No configuration data\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -080099 return -ENXIO;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400100 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800101
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100102 master = devm_kzalloc(&pdev->dev, sizeof(struct w1_bus_master),
103 GFP_KERNEL);
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400104 if (!master) {
105 dev_err(&pdev->dev, "Out of memory\n");
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800106 return -ENOMEM;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400107 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800108
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100109 err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400110 if (err) {
111 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100112 return err;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400113 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800114
Daniel Mackd2323cf2012-07-25 22:54:29 +0200115 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100116 err = devm_gpio_request_one(&pdev->dev,
117 pdata->ext_pullup_enable_pin, GPIOF_INIT_LOW,
118 "w1 pullup");
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400119 if (err < 0) {
120 dev_err(&pdev->dev, "gpio_request_one "
121 "(ext_pullup_enable_pin) failed\n");
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100122 return err;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400123 }
Daniel Mackd2323cf2012-07-25 22:54:29 +0200124 }
125
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800126 master->data = pdata;
127 master->read_bit = w1_gpio_read_bit;
128
129 if (pdata->is_open_drain) {
130 gpio_direction_output(pdata->pin, 1);
131 master->write_bit = w1_gpio_write_bit_val;
132 } else {
133 gpio_direction_input(pdata->pin);
134 master->write_bit = w1_gpio_write_bit_dir;
135 }
136
137 err = w1_add_master_device(master);
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400138 if (err) {
139 dev_err(&pdev->dev, "w1_add_master device failed\n");
Markus Pargmannd27f25c2013-10-29 09:19:24 +0100140 return err;
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400141 }
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800142
Daniel Mackc8a06c12009-06-17 16:28:15 -0700143 if (pdata->enable_external_pullup)
144 pdata->enable_external_pullup(1);
145
Daniel Mackd2323cf2012-07-25 22:54:29 +0200146 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
147 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
148
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800149 platform_set_drvdata(pdev, master);
150
151 return 0;
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800152}
153
Johan Hovold01230552013-03-08 11:07:59 +0100154static int w1_gpio_remove(struct platform_device *pdev)
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800155{
156 struct w1_bus_master *master = platform_get_drvdata(pdev);
Jingoo Hanc853b1672013-11-14 14:32:04 -0800157 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800158
Daniel Mackc8a06c12009-06-17 16:28:15 -0700159 if (pdata->enable_external_pullup)
160 pdata->enable_external_pullup(0);
161
Daniel Mackd2323cf2012-07-25 22:54:29 +0200162 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
163 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
164
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800165 w1_remove_master_device(master);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800166
167 return 0;
168}
169
Daniel Mackc8a06c12009-06-17 16:28:15 -0700170#ifdef CONFIG_PM
171
172static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
173{
Jingoo Hanc853b1672013-11-14 14:32:04 -0800174 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Mackc8a06c12009-06-17 16:28:15 -0700175
176 if (pdata->enable_external_pullup)
177 pdata->enable_external_pullup(0);
178
179 return 0;
180}
181
182static int w1_gpio_resume(struct platform_device *pdev)
183{
Jingoo Hanc853b1672013-11-14 14:32:04 -0800184 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Mackc8a06c12009-06-17 16:28:15 -0700185
186 if (pdata->enable_external_pullup)
187 pdata->enable_external_pullup(1);
188
189 return 0;
190}
191
192#else
193#define w1_gpio_suspend NULL
194#define w1_gpio_resume NULL
195#endif
196
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800197static struct platform_driver w1_gpio_driver = {
198 .driver = {
199 .name = "w1-gpio",
200 .owner = THIS_MODULE,
Daniel Mack5f3d1382012-07-23 16:36:35 +0200201 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800202 },
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400203 .probe = w1_gpio_probe,
Johan Hovold01230552013-03-08 11:07:59 +0100204 .remove = w1_gpio_remove,
Daniel Mackc8a06c12009-06-17 16:28:15 -0700205 .suspend = w1_gpio_suspend,
206 .resume = w1_gpio_resume,
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800207};
208
Pantelis Antoniou8a1861d2012-11-22 01:13:47 +0400209module_platform_driver(w1_gpio_driver);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800210
211MODULE_DESCRIPTION("GPIO w1 bus master driver");
212MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
213MODULE_LICENSE("GPL");