Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * w1-gpio - GPIO w1 bus master driver
  4 *
  5 * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
 
 
 
 
  6 */
  7
  8#include <linux/init.h>
  9#include <linux/module.h>
 10#include <linux/platform_device.h>
 11#include <linux/slab.h>
 12#include <linux/w1-gpio.h>
 13#include <linux/gpio/consumer.h>
 14#include <linux/of_platform.h>
 15#include <linux/err.h>
 16#include <linux/of.h>
 17#include <linux/delay.h>
 18
 19#include <linux/w1.h>
 
 
 
 20
 21static u8 w1_gpio_set_pullup(void *data, int delay)
 22{
 23	struct w1_gpio_platform_data *pdata = data;
 24
 25	if (delay) {
 26		pdata->pullup_duration = delay;
 27	} else {
 28		if (pdata->pullup_duration) {
 29			/*
 30			 * This will OVERRIDE open drain emulation and force-pull
 31			 * the line high for some time.
 32			 */
 33			gpiod_set_raw_value(pdata->gpiod, 1);
 34			msleep(pdata->pullup_duration);
 35			/*
 36			 * This will simply set the line as input since we are doing
 37			 * open drain emulation in the GPIO library.
 38			 */
 39			gpiod_set_value(pdata->gpiod, 1);
 40		}
 41		pdata->pullup_duration = 0;
 42	}
 43
 44	return 0;
 45}
 46
 47static void w1_gpio_write_bit(void *data, u8 bit)
 48{
 49	struct w1_gpio_platform_data *pdata = data;
 50
 51	gpiod_set_value(pdata->gpiod, bit);
 52}
 53
 54static u8 w1_gpio_read_bit(void *data)
 55{
 56	struct w1_gpio_platform_data *pdata = data;
 57
 58	return gpiod_get_value(pdata->gpiod) ? 1 : 0;
 59}
 60
 61#if defined(CONFIG_OF)
 62static const struct of_device_id w1_gpio_dt_ids[] = {
 63	{ .compatible = "w1-gpio" },
 64	{}
 65};
 66MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
 67#endif
 68
 69static int w1_gpio_probe(struct platform_device *pdev)
 70{
 71	struct w1_bus_master *master;
 72	struct w1_gpio_platform_data *pdata;
 73	struct device *dev = &pdev->dev;
 74	struct device_node *np = dev->of_node;
 75	/* Enforce open drain mode by default */
 76	enum gpiod_flags gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
 77	int err;
 78
 79	if (of_have_populated_dt()) {
 80		pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 81		if (!pdata)
 82			return -ENOMEM;
 83
 84		/*
 85		 * This parameter means that something else than the gpiolib has
 86		 * already set the line into open drain mode, so we should just
 87		 * driver it high/low like we are in full control of the line and
 88		 * open drain will happen transparently.
 89		 */
 90		if (of_get_property(np, "linux,open-drain", NULL))
 91			gflags = GPIOD_OUT_LOW;
 92
 93		pdev->dev.platform_data = pdata;
 94	}
 95	pdata = dev_get_platdata(dev);
 96
 97	if (!pdata) {
 98		dev_err(dev, "No configuration data\n");
 99		return -ENXIO;
100	}
101
102	master = devm_kzalloc(dev, sizeof(struct w1_bus_master),
103			GFP_KERNEL);
104	if (!master) {
105		dev_err(dev, "Out of memory\n");
106		return -ENOMEM;
107	}
108
109	pdata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);
110	if (IS_ERR(pdata->gpiod)) {
111		dev_err(dev, "gpio_request (pin) failed\n");
112		return PTR_ERR(pdata->gpiod);
113	}
114
115	pdata->pullup_gpiod =
116		devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);
117	if (IS_ERR(pdata->pullup_gpiod)) {
118		dev_err(dev, "gpio_request_one "
119			"(ext_pullup_enable_pin) failed\n");
120		return PTR_ERR(pdata->pullup_gpiod);
121	}
122
123	master->data = pdata;
124	master->read_bit = w1_gpio_read_bit;
125	gpiod_direction_output(pdata->gpiod, 1);
126	master->write_bit = w1_gpio_write_bit;
127
128	/*
129	 * If we are using open drain emulation from the GPIO library,
130	 * we need to use this pullup function that hammers the line
131	 * high using a raw accessor to provide pull-up for the w1
132	 * line.
133	 */
134	if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
135		master->set_pullup = w1_gpio_set_pullup;
136
137	err = w1_add_master_device(master);
138	if (err) {
139		dev_err(dev, "w1_add_master device failed\n");
140		return err;
141	}
142
143	if (pdata->enable_external_pullup)
144		pdata->enable_external_pullup(1);
145
146	if (pdata->pullup_gpiod)
147		gpiod_set_value(pdata->pullup_gpiod, 1);
148
149	platform_set_drvdata(pdev, master);
150
151	return 0;
 
 
 
 
 
 
 
152}
153
154static int w1_gpio_remove(struct platform_device *pdev)
155{
156	struct w1_bus_master *master = platform_get_drvdata(pdev);
157	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
158
159	if (pdata->enable_external_pullup)
160		pdata->enable_external_pullup(0);
161
162	if (pdata->pullup_gpiod)
163		gpiod_set_value(pdata->pullup_gpiod, 0);
164
165	w1_remove_master_device(master);
 
 
166
167	return 0;
168}
169
170static int __maybe_unused w1_gpio_suspend(struct device *dev)
 
 
171{
172	struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
173
174	if (pdata->enable_external_pullup)
175		pdata->enable_external_pullup(0);
176
177	return 0;
178}
179
180static int __maybe_unused w1_gpio_resume(struct device *dev)
181{
182	struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
183
184	if (pdata->enable_external_pullup)
185		pdata->enable_external_pullup(1);
186
187	return 0;
188}
189
190static SIMPLE_DEV_PM_OPS(w1_gpio_pm_ops, w1_gpio_suspend, w1_gpio_resume);
 
 
 
191
192static struct platform_driver w1_gpio_driver = {
193	.driver = {
194		.name	= "w1-gpio",
195		.pm	= &w1_gpio_pm_ops,
196		.of_match_table = of_match_ptr(w1_gpio_dt_ids),
197	},
198	.probe = w1_gpio_probe,
199	.remove = w1_gpio_remove,
 
200};
201
202module_platform_driver(w1_gpio_driver);
 
 
 
 
 
 
 
 
 
 
 
203
204MODULE_DESCRIPTION("GPIO w1 bus master driver");
205MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
206MODULE_LICENSE("GPL");
v3.1
 
  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
 17#include "../w1.h"
 18#include "../w1_int.h"
 19
 20#include <asm/gpio.h>
 21
 22static void w1_gpio_write_bit_dir(void *data, u8 bit)
 23{
 24	struct w1_gpio_platform_data *pdata = data;
 25
 26	if (bit)
 27		gpio_direction_input(pdata->pin);
 28	else
 29		gpio_direction_output(pdata->pin, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 30}
 31
 32static void w1_gpio_write_bit_val(void *data, u8 bit)
 33{
 34	struct w1_gpio_platform_data *pdata = data;
 35
 36	gpio_set_value(pdata->pin, bit);
 37}
 38
 39static u8 w1_gpio_read_bit(void *data)
 40{
 41	struct w1_gpio_platform_data *pdata = data;
 42
 43	return gpio_get_value(pdata->pin) ? 1 : 0;
 44}
 45
 46static int __init w1_gpio_probe(struct platform_device *pdev)
 
 
 
 
 
 
 
 
 47{
 48	struct w1_bus_master *master;
 49	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
 
 
 
 
 50	int err;
 51
 52	if (!pdata)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 53		return -ENXIO;
 
 54
 55	master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
 56	if (!master)
 
 
 57		return -ENOMEM;
 
 
 
 
 
 
 
 58
 59	err = gpio_request(pdata->pin, "w1");
 60	if (err)
 61		goto free_master;
 
 
 
 
 62
 63	master->data = pdata;
 64	master->read_bit = w1_gpio_read_bit;
 
 
 65
 66	if (pdata->is_open_drain) {
 67		gpio_direction_output(pdata->pin, 1);
 68		master->write_bit = w1_gpio_write_bit_val;
 69	} else {
 70		gpio_direction_input(pdata->pin);
 71		master->write_bit = w1_gpio_write_bit_dir;
 72	}
 
 73
 74	err = w1_add_master_device(master);
 75	if (err)
 76		goto free_gpio;
 
 
 77
 78	if (pdata->enable_external_pullup)
 79		pdata->enable_external_pullup(1);
 80
 
 
 
 81	platform_set_drvdata(pdev, master);
 82
 83	return 0;
 84
 85 free_gpio:
 86	gpio_free(pdata->pin);
 87 free_master:
 88	kfree(master);
 89
 90	return err;
 91}
 92
 93static int __exit w1_gpio_remove(struct platform_device *pdev)
 94{
 95	struct w1_bus_master *master = platform_get_drvdata(pdev);
 96	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
 97
 98	if (pdata->enable_external_pullup)
 99		pdata->enable_external_pullup(0);
100
 
 
 
101	w1_remove_master_device(master);
102	gpio_free(pdata->pin);
103	kfree(master);
104
105	return 0;
106}
107
108#ifdef CONFIG_PM
109
110static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
111{
112	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
113
114	if (pdata->enable_external_pullup)
115		pdata->enable_external_pullup(0);
116
117	return 0;
118}
119
120static int w1_gpio_resume(struct platform_device *pdev)
121{
122	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
123
124	if (pdata->enable_external_pullup)
125		pdata->enable_external_pullup(1);
126
127	return 0;
128}
129
130#else
131#define w1_gpio_suspend	NULL
132#define w1_gpio_resume	NULL
133#endif
134
135static struct platform_driver w1_gpio_driver = {
136	.driver = {
137		.name	= "w1-gpio",
138		.owner	= THIS_MODULE,
 
139	},
140	.remove	= __exit_p(w1_gpio_remove),
141	.suspend = w1_gpio_suspend,
142	.resume = w1_gpio_resume,
143};
144
145static int __init w1_gpio_init(void)
146{
147	return platform_driver_probe(&w1_gpio_driver, w1_gpio_probe);
148}
149
150static void __exit w1_gpio_exit(void)
151{
152	platform_driver_unregister(&w1_gpio_driver);
153}
154
155module_init(w1_gpio_init);
156module_exit(w1_gpio_exit);
157
158MODULE_DESCRIPTION("GPIO w1 bus master driver");
159MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
160MODULE_LICENSE("GPL");