Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ |
| 2 | * |
| 3 | * Copyright (c) 2015 Free Electrons |
| 4 | * Copyright (c) 2015 NextThing Co |
| 5 | * |
| 6 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 7 | * |
| 8 | */ |
| 9 | |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 12 | #include <w1.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 14 | |
| 15 | #include <asm/gpio.h> |
| 16 | |
| 17 | #define W1_TIMING_A 6 |
| 18 | #define W1_TIMING_B 64 |
| 19 | #define W1_TIMING_C 60 |
| 20 | #define W1_TIMING_D 10 |
| 21 | #define W1_TIMING_E 9 |
| 22 | #define W1_TIMING_F 55 |
| 23 | #define W1_TIMING_G 0 |
| 24 | #define W1_TIMING_H 480 |
| 25 | #define W1_TIMING_I 70 |
| 26 | #define W1_TIMING_J 410 |
| 27 | |
| 28 | struct w1_gpio_pdata { |
| 29 | struct gpio_desc gpio; |
| 30 | u64 search_id; |
| 31 | }; |
| 32 | |
| 33 | static bool w1_gpio_read_bit(struct udevice *dev) |
| 34 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 35 | struct w1_gpio_pdata *pdata = dev_get_plat(dev); |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 36 | int val; |
| 37 | |
| 38 | dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT); |
| 39 | udelay(W1_TIMING_A); |
| 40 | |
| 41 | dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN); |
| 42 | udelay(W1_TIMING_E); |
| 43 | |
| 44 | val = dm_gpio_get_value(&pdata->gpio); |
| 45 | if (val < 0) |
| 46 | debug("error in retrieving GPIO value"); |
| 47 | udelay(W1_TIMING_F); |
| 48 | |
| 49 | return val; |
| 50 | } |
| 51 | |
| 52 | static u8 w1_gpio_read_byte(struct udevice *dev) |
| 53 | { |
| 54 | int i; |
| 55 | u8 ret = 0; |
| 56 | |
| 57 | for (i = 0; i < 8; ++i) |
| 58 | ret |= (w1_gpio_read_bit(dev) ? 1 : 0) << i; |
| 59 | |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | static void w1_gpio_write_bit(struct udevice *dev, bool bit) |
| 64 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 65 | struct w1_gpio_pdata *pdata = dev_get_plat(dev); |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 66 | |
| 67 | dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT); |
| 68 | |
| 69 | bit ? udelay(W1_TIMING_A) : udelay(W1_TIMING_C); |
| 70 | |
| 71 | dm_gpio_set_value(&pdata->gpio, 1); |
| 72 | |
| 73 | bit ? udelay(W1_TIMING_B) : udelay(W1_TIMING_D); |
| 74 | } |
| 75 | |
| 76 | static void w1_gpio_write_byte(struct udevice *dev, u8 byte) |
| 77 | { |
| 78 | int i; |
| 79 | |
| 80 | for (i = 0; i < 8; ++i) |
| 81 | w1_gpio_write_bit(dev, (byte >> i) & 0x1); |
| 82 | } |
| 83 | |
| 84 | static bool w1_gpio_reset(struct udevice *dev) |
| 85 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 86 | struct w1_gpio_pdata *pdata = dev_get_plat(dev); |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 87 | int val; |
| 88 | |
| 89 | /* initiate the reset pulse. first we must pull the bus to low */ |
| 90 | dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 91 | udelay(W1_TIMING_G); |
| 92 | |
| 93 | dm_gpio_set_value(&pdata->gpio, 0); |
| 94 | /* wait for the specified time with the bus kept low */ |
| 95 | udelay(W1_TIMING_H); |
| 96 | |
| 97 | /* now we must read the presence pulse */ |
| 98 | dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN); |
| 99 | udelay(W1_TIMING_I); |
| 100 | |
| 101 | val = dm_gpio_get_value(&pdata->gpio); |
| 102 | if (val < 0) |
| 103 | debug("error in retrieving GPIO value"); |
| 104 | |
| 105 | /* if nobody pulled the bus down , it means nobody is on the bus */ |
| 106 | if (val != 0) |
| 107 | return 1; |
| 108 | /* we have the bus pulled down, let's wait for the specified presence time */ |
| 109 | udelay(W1_TIMING_J); |
| 110 | |
| 111 | /* read again, the other end should leave the bus free */ |
| 112 | val = dm_gpio_get_value(&pdata->gpio); |
| 113 | if (val < 0) |
| 114 | debug("error in retrieving GPIO value"); |
| 115 | |
| 116 | /* bus is not going up again, so we have an error */ |
| 117 | if (val != 1) |
| 118 | return 1; |
| 119 | |
| 120 | /* all good, presence detected */ |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static u8 w1_gpio_triplet(struct udevice *dev, bool bdir) |
| 125 | { |
| 126 | u8 id_bit = w1_gpio_read_bit(dev); |
| 127 | u8 comp_bit = w1_gpio_read_bit(dev); |
| 128 | u8 retval; |
| 129 | |
| 130 | if (id_bit && comp_bit) |
| 131 | return 0x03; /* error */ |
| 132 | |
| 133 | if (!id_bit && !comp_bit) { |
| 134 | /* Both bits are valid, take the direction given */ |
| 135 | retval = bdir ? 0x04 : 0; |
| 136 | } else { |
| 137 | /* Only one bit is valid, take that direction */ |
| 138 | bdir = id_bit; |
| 139 | retval = id_bit ? 0x05 : 0x02; |
| 140 | } |
| 141 | |
| 142 | w1_gpio_write_bit(dev, bdir); |
| 143 | return retval; |
| 144 | } |
| 145 | |
| 146 | static const struct w1_ops w1_gpio_ops = { |
| 147 | .read_byte = w1_gpio_read_byte, |
| 148 | .reset = w1_gpio_reset, |
| 149 | .triplet = w1_gpio_triplet, |
| 150 | .write_byte = w1_gpio_write_byte, |
| 151 | }; |
| 152 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 153 | static int w1_gpio_of_to_plat(struct udevice *dev) |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 154 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 155 | struct w1_gpio_pdata *pdata = dev_get_plat(dev); |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 156 | int ret; |
| 157 | |
Eugen Hristev | dffea44 | 2021-06-23 16:11:58 +0300 | [diff] [blame] | 158 | ret = gpio_request_by_name(dev, "gpios", 0, &pdata->gpio, GPIOD_IS_IN); |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 159 | if (ret < 0) |
| 160 | printf("Error claiming GPIO %d\n", ret); |
| 161 | |
| 162 | return ret; |
| 163 | }; |
| 164 | |
| 165 | static const struct udevice_id w1_gpio_id[] = { |
| 166 | { "w1-gpio", 0 }, |
| 167 | { }, |
| 168 | }; |
| 169 | |
| 170 | U_BOOT_DRIVER(w1_gpio_drv) = { |
| 171 | .id = UCLASS_W1, |
| 172 | .name = "w1_gpio_drv", |
| 173 | .of_match = w1_gpio_id, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 174 | .of_to_plat = w1_gpio_of_to_plat, |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 175 | .ops = &w1_gpio_ops, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 176 | .plat_auto = sizeof(struct w1_gpio_pdata), |
Maxime Ripard | 73aea28 | 2018-09-18 10:35:25 +0300 | [diff] [blame] | 177 | }; |