brintos

brintos / linux-shallow public Read only

0
0
Text · 3.8 KiB · a39fa8b Raw
157 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * w1-gpio - GPIO w1 bus master driver4 *5 * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>6 */7 8#include <linux/delay.h>9#include <linux/device.h>10#include <linux/err.h>11#include <linux/gpio/consumer.h>12#include <linux/mod_devicetable.h>13#include <linux/module.h>14#include <linux/platform_device.h>15#include <linux/property.h>16#include <linux/types.h>17 18#include <linux/w1.h>19 20struct w1_gpio_ddata {21	struct gpio_desc *gpiod;22	struct gpio_desc *pullup_gpiod;23	unsigned int pullup_duration;24};25 26static u8 w1_gpio_set_pullup(void *data, int delay)27{28	struct w1_gpio_ddata *ddata = data;29 30	if (delay) {31		ddata->pullup_duration = delay;32	} else {33		if (ddata->pullup_duration) {34			/*35			 * This will OVERRIDE open drain emulation and force-pull36			 * the line high for some time.37			 */38			gpiod_set_raw_value(ddata->gpiod, 1);39			msleep(ddata->pullup_duration);40			/*41			 * This will simply set the line as input since we are doing42			 * open drain emulation in the GPIO library.43			 */44			gpiod_set_value(ddata->gpiod, 1);45		}46		ddata->pullup_duration = 0;47	}48 49	return 0;50}51 52static void w1_gpio_write_bit(void *data, u8 bit)53{54	struct w1_gpio_ddata *ddata = data;55 56	gpiod_set_value(ddata->gpiod, bit);57}58 59static u8 w1_gpio_read_bit(void *data)60{61	struct w1_gpio_ddata *ddata = data;62 63	return gpiod_get_value(ddata->gpiod) ? 1 : 0;64}65 66static int w1_gpio_probe(struct platform_device *pdev)67{68	struct w1_bus_master *master;69	struct w1_gpio_ddata *ddata;70	struct device *dev = &pdev->dev;71	/* Enforce open drain mode by default */72	enum gpiod_flags gflags = GPIOD_OUT_LOW_OPEN_DRAIN;73	int err;74 75	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);76	if (!ddata)77		return -ENOMEM;78 79	/*80	 * This parameter means that something else than the gpiolib has81	 * already set the line into open drain mode, so we should just82	 * driver it high/low like we are in full control of the line and83	 * open drain will happen transparently.84	 */85	if (device_property_present(dev, "linux,open-drain"))86		gflags = GPIOD_OUT_LOW;87 88	master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);89	if (!master)90		return -ENOMEM;91 92	ddata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);93	if (IS_ERR(ddata->gpiod))94		return dev_err_probe(dev, PTR_ERR(ddata->gpiod), "gpio_request (pin) failed\n");95 96	ddata->pullup_gpiod =97		devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);98	if (IS_ERR(ddata->pullup_gpiod))99		return dev_err_probe(dev, PTR_ERR(ddata->pullup_gpiod),100				     "gpio_request (ext_pullup_enable_pin) failed\n");101 102	master->data = ddata;103	master->read_bit = w1_gpio_read_bit;104	gpiod_direction_output(ddata->gpiod, 1);105	master->write_bit = w1_gpio_write_bit;106 107	/*108	 * If we are using open drain emulation from the GPIO library,109	 * we need to use this pullup function that hammers the line110	 * high using a raw accessor to provide pull-up for the w1111	 * line.112	 */113	if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)114		master->set_pullup = w1_gpio_set_pullup;115 116	err = w1_add_master_device(master);117	if (err)118		return dev_err_probe(dev, err, "w1_add_master device failed\n");119 120	gpiod_set_value(ddata->pullup_gpiod, 1);121 122	platform_set_drvdata(pdev, master);123 124	return 0;125}126 127static void w1_gpio_remove(struct platform_device *pdev)128{129	struct w1_bus_master *master = platform_get_drvdata(pdev);130	struct w1_gpio_ddata *ddata = master->data;131 132	gpiod_set_value(ddata->pullup_gpiod, 0);133 134	w1_remove_master_device(master);135}136 137static const struct of_device_id w1_gpio_dt_ids[] = {138	{ .compatible = "w1-gpio" },139	{}140};141MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);142 143static struct platform_driver w1_gpio_driver = {144	.driver = {145		.name	= "w1-gpio",146		.of_match_table = w1_gpio_dt_ids,147	},148	.probe = w1_gpio_probe,149	.remove_new = w1_gpio_remove,150};151 152module_platform_driver(w1_gpio_driver);153 154MODULE_DESCRIPTION("GPIO w1 bus master driver");155MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");156MODULE_LICENSE("GPL");157