w1: Add 1-Wire gpio driver
A message that is not in the archive (sent off-list or not yet received): cover.e445aa83aac1de200cdbb59f8a5572c43341f03d.1478599446.git-series.maxime.ripard@free-electrons.com
A message that is not in the archive (sent off-list or not yet received): 267e4b92e3091503ebeaa8dff343538b32a326af.1478599446.git-series.maxime.ripard@free-electrons.com
PATCH 4/9 w1: Add 1-Wire gpio driver
Hi Maxime,
Quoted 15 lines · Maxime Ripard
Add a bus driver for bitbanging a 1-Wire bus over a GPIO.
Signed-off-by Maxime Ripard <[email protected]>--- drivers/w1/Kconfig | 6 ++- drivers/w1/Makefile | 1 +- drivers/w1/w1-gpio.c | 160 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 167 insertions(+), 0 deletions(-) create mode 100644 drivers/w1/w1-gpio.cdrivers/w1/Kconfig +0
-0
drivers/w1/Kconfig config W1 · line 19
Quoted 8 lines
drivers/w1/Kconfig +5
-0@@ -12,6 +12,12 @@ config W1 if W1 +config W1_GPIO + bool "Enable 1-Wire GPIO bitbanging" + depends on DM_GPIO + help + Emulate a 1-Wire bus using a GPIO.
Any more details? How many GPIOs? Any particular chips that are supported?
Quoted 34 lines
drivers/w1/Kconfig +1
-0+ endif endmenudrivers/w1/Makefile +1
-0@@ -1,2 +1,3 @@ obj-$(CONFIG_W1) += w1-uclass.o +obj-$(CONFIG_W1_GPIO) += w1-gpio.odrivers/w1/w1-gpio.c new file +16
-0@@ -0,0 +1,160 @@ +/* +* Copyright (c) 2015 Free Electrons +* Copyright (c) 2015 NextThing Co +* +* Maxime Ripard <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0+ +*/ + +#include <common.h> +#include <dm.h> +#include <w1.h> + +#include <asm/gpio.h> + +
drivers/w1/w1-gpio.c line 26
Quoted 10 lines
drivers/w1/w1-gpio.c new file +10
-0+#define W1_TIMING_A 6 +#define W1_TIMING_B 64 +#define W1_TIMING_C 60 +#define W1_TIMING_D 10 +#define W1_TIMING_E 9 +#define W1_TIMING_F 55 +#define W1_TIMING_G 0 +#define W1_TIMING_H 480 +#define W1_TIMING_I 70 +#define W1_TIMING_J 410
What do these mean? Perhaps add a _US suffix?
drivers/w1/w1-gpio.c line 38
Quoted 12 lines
drivers/w1/w1-gpio.c new file +12
-0+ +struct w1_gpio_pdata { +struct gpio_desc gpio; +u64 search_id; +}; + +static bool w1_gpio_read_bit (struct udevice *dev) +{ +struct w1_gpio_pdata *pdata =dev_get_platdata (dev); +int val; + +dm_gpio_set_dir_flags (&pdata->gpio,GPIOD_IS_OUT );
Error checks here?
Quoted 19 lines
drivers/w1/w1-gpio.c new file +19
-0+udelay (W1_TIMING_A ); + +dm_gpio_set_dir_flags (&pdata->gpio,GPIOD_IS_IN ); +udelay (W1_TIMING_E ); + + val =dm_gpio_get_value (&pdata->gpio); +udelay (W1_TIMING_F ); + +return val; +} + +static u8 w1_gpio_read_byte (struct udevice *dev) +{ +int i; +u8 ret =0 ; + +for (i =0 ; i <8 ; ++i) + ret |= (w1_gpio_read_bit (dev) ?1 :0 ) << i; +
drivers/w1/w1-gpio.c line 67
Quoted 10 lines
drivers/w1/w1-gpio.c new file +10
-0+return ret; +} + +static void w1_gpio_write_bit (struct udevice *dev,bool bit) +{ +struct w1_gpio_pdata *pdata =dev_get_platdata (dev); + +dm_gpio_set_dir_flags (&pdata->gpio,GPIOD_IS_OUT ); + + bit ?udelay (W1_TIMING_A ) :udelay (W1_TIMING_C );
Can you convert this and below to if/else?
Quoted 40 lines
drivers/w1/w1-gpio.c new file +40
-0+ +dm_gpio_set_value (&pdata->gpio,1 ); + + bit ?udelay (W1_TIMING_B ) :udelay (W1_TIMING_D ); +} + +static void w1_gpio_write_byte (struct udevice *dev,u8 byte) +{ +int i; + +for (i =0 ; i <8 ; ++i) +w1_gpio_write_bit (dev, (byte >> i) &0x1 ); +} + +static bool w1_gpio_reset (struct udevice *dev) +{ +struct w1_gpio_pdata *pdata =dev_get_platdata (dev); +int val; + +dm_gpio_set_dir_flags (&pdata->gpio,GPIOD_IS_OUT |GPIOD_IS_OUT_ACTIVE ); +udelay (W1_TIMING_G ); + +dm_gpio_set_value (&pdata->gpio,0 ); +udelay (W1_TIMING_H ); + +dm_gpio_set_dir_flags (&pdata->gpio,GPIOD_IS_IN ); +udelay (W1_TIMING_I ); + + val =dm_gpio_get_value (&pdata->gpio); +udelay (W1_TIMING_J ); + +return val; +} + +static u8 w1_gpio_triplet (struct udevice *dev,bool bdir) +{ +u8 id_bit =w1_gpio_read_bit (dev); +u8 comp_bit =w1_gpio_read_bit (dev); +u8 retval; +
drivers/w1/w1-gpio.c line 117
Quoted 10 lines
drivers/w1/w1-gpio.c new file +10
-0+if (id_bit && comp_bit) +return 0x03 ;/* error */ + +if (!id_bit && !comp_bit) { +/* Both bits are valid, take the direction given */ + retval = bdir ?0x04 :0 ; + }else { +/* Only one bit is valid, take that direction */ + bdir = id_bit; + retval = id_bit ?0x05 :0x02 ;
Can you use enums / #defines for these magic values?
Quoted 5 lines
drivers/w1/w1-gpio.c new file +5
-0+ } + +w1_gpio_write_bit (dev, bdir); +return retval; +}
drivers/w1/w1-gpio.c line 132
Quoted 10 lines
drivers/w1/w1-gpio.c new file +10
-0+ + +static const struct w1_ops w1_gpio_ops = { + .read_byte = w1_gpio_read_byte, + .reset = w1_gpio_reset, + .triplet = w1_gpio_triplet, + .write_byte = w1_gpio_write_byte, +}; + +static int w1_gpio_ofdata_to_platdata (struct udevice *dev)
This should probably go in probe() since it actually probes the GPIOs.
drivers/w1/w1-gpio.c line 144
Quoted 12 lines
drivers/w1/w1-gpio.c new file +12
-0+{ +struct w1_gpio_pdata *pdata =dev_get_platdata (dev); +int ret; + + ret =gpio_request_by_name (dev,"gpios" ,0 , &pdata->gpio,0 ); +if (ret <0 ) + goto error; + +return 0 ; + +error: +printf ("Error claiming GPIO %d\n" , ret);
debug()?
Quoted 17 lines
drivers/w1/w1-gpio.c new file +16
-1+return ret; +}; + +static const struct udevice_id w1_gpio_id[] = { + {"w1-gpio" ,0 }, + { }, +}; + +U_BOOT_DRIVER (w1_gpio_drv) = { + .id =UCLASS_W1 , + .name ="w1_gpio_drv" , + .of_match = w1_gpio_id, + .ofdata_to_platdata = w1_gpio_ofdata_to_platdata, + .ops = &w1_gpio_ops, + .platdata_auto_alloc_size =sizeof (struct w1_gpio_pdata ), +}; --
Quoted 1 line
git-series 0.8.11
Regards,
SImon
PATCH 4/9 w1: Add 1-Wire gpio driver
Hi Simon,
Quoted 22 lines · Simon Glass
Hi Maxime,
Quoted 18 lines · Maxime Ripard
Add a bus driver for bitbanging a 1-Wire bus over a GPIO.
Signed-off-by Maxime Ripard <[email protected]>--- drivers/w1/Kconfig | 6 ++- drivers/w1/Makefile | 1 +- drivers/w1/w1-gpio.c | 160 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 167 insertions(+), 0 deletions(-) create mode 100644 drivers/w1/w1-gpio.cdrivers/w1/Kconfig +0
-0@@ -12,6 +12,12 @@ config W1 if W1
Quoted 8 lines
drivers/w1/Kconfig config W1 · line 19
Quoted 5 lines
drivers/w1/Kconfig +5
-0+config W1_GPIO + bool "Enable 1-Wire GPIO bitbanging" + depends on DM_GPIO + help + Emulate a 1-Wire bus using a GPIO.Any more details? How many GPIOs? Any particular chips that are supported?
1-Wire works on a single line, hence it's name. You usually have either a controller (which is quite rare, but some SoCs have one) or you can bitbang the bus. It's low bandwidth enough that it doesn't really matter.
I'm not sure if it answers your question. I'll address your other comments.
Thanks!
Maxime
Signature · 10 lines
Maxime Ripard, Free Electrons Embedded Linux and Kernel engineering http://free-electrons.com -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 801 bytes Desc: not available URL: <http://lists.denx.de/pipermail/u-boot/attachments/20161114/340b8e75/attachment.sig>
PATCH 4/9 w1: Add 1-Wire gpio driver
Hi Maxime,
Quoted 34 lines · Maxime Ripard
Hi Simon,
Quoted 22 lines · Simon Glass
Hi Maxime,
Quoted 18 lines · Maxime Ripard
Add a bus driver for bitbanging a 1-Wire bus over a GPIO.
Signed-off-by Maxime Ripard <[email protected]>--- drivers/w1/Kconfig | 6 ++- drivers/w1/Makefile | 1 +- drivers/w1/w1-gpio.c | 160 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 167 insertions(+), 0 deletions(-) create mode 100644 drivers/w1/w1-gpio.cdrivers/w1/Kconfig +0
-0@@ -12,6 +12,12 @@ config W1 if W1Quoted 8 lines
drivers/w1/Kconfig config W1 · line 19
Quoted 5 lines
drivers/w1/Kconfig +5
-0+config W1_GPIO + bool "Enable 1-Wire GPIO bitbanging" + depends on DM_GPIO + help + Emulate a 1-Wire bus using a GPIO.Any more details? How many GPIOs? Any particular chips that are supported?
Quoted 7 lines
1-Wire works on a single line, hence it's name. You usually have either a controller (which is quite rare, but some SoCs have one) or you can bitbang the bus. It's low bandwidth enough that it doesn't really matter.
I'm not sure if it answers your question. I'll address your other comments.
OK thanks - please can you add a bit more detail into the help.
Regards,
Simon
A message that is not in the archive (sent off-list or not yet received): de0f795a85e3a17f639ed2d23dd8b9936bc0b332.1478599446.git-series.maxime.ripard@free-electrons.com
PATCH 3/9 w1: Add 1-Wire uclass
Hi Maxime,
Quoted 4 lines · Maxime Ripard
We might want to use 1-Wire devices connected on boards such as EEPROMs in U-Boot.
Provide a framework to be able to do that.
Also please can you add a sandbox driver and a test for this uclass.
Quoted 48 lines
Signed-off-by Maxime Ripard <[email protected]>--- drivers/Kconfig | 2 +- drivers/Makefile | 1 +- drivers/w1/Kconfig | 17 +++- drivers/w1/Makefile | 2 +- drivers/w1/w1-uclass.c | 259 ++++++++++++++++++++++++++++++++++++++++++- include/dm/uclass-id.h | 1 +- include/w1.h | 47 ++++++++- 7 files changed, 329 insertions(+), 0 deletions(-) create mode 100644 drivers/w1/Kconfig create mode 100644 drivers/w1/Makefile create mode 100644 drivers/w1/w1-uclass.c create mode 100644 include/w1.hdrivers/Kconfig +2
-0@@ -84,6 +84,8 @@ source "drivers/usb/Kconfig" source "drivers/video/Kconfig" +source "drivers/w1/Kconfig" + source "drivers/watchdog/Kconfig" config PHYS_TO_BUSdrivers/Makefile +1
-0@@ -91,6 +91,7 @@ obj-y += input/ obj-y += soc/ obj-$(CONFIG_REMOTEPROC) += remoteproc/ obj-y += thermal/ +obj-y += w1/ obj-$(CONFIG_MACH_PIC32) += ddr/microchip/ endifdrivers/w1/Kconfig new file +1
-0@@ -0,0 +1,17 @@ +#
drivers/w1/Kconfig line 11
Quoted 10 lines
drivers/w1/Kconfig new file +10
-0+# W1 subsystem configuration +# + +menu "1-Wire support" + +config W1 + bool "Enable 1-Wire controllers support" + depends on DM + help + Support for the Dallas 1-Wire bus.
Can you add more explanation of what this is and what support is provided?
Quoted 38 lines
drivers/w1/Kconfig new file +6
-0+ +if W1 + +endif + +endmenudrivers/w1/Makefile new file +2
-0@@ -0,0 +1,2 @@ +obj-$(CONFIG_W1) += w1-uclass.o +drivers/w1/w1-uclass.c new file +18
-0@@ -0,0 +1,259 @@ +/* +* Copyright (c) 2015 Free Electrons +* Copyright (c) 2015 NextThing Co. +* +* Maxime Ripard <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0+ +*/ + +#include <common.h> +#include <dm.h> +#include <w1.h> + +#include <dm/device-internal.h> + +#define W1_MATCH_ROM 0x55 +#define W1_SKIP_ROM 0xcc +#define W1_SEARCH 0xf0
drivers/w1/w1-uclass.c line 28
Quoted 10 lines
drivers/w1/w1-uclass.c new file +10
-0+ +struct w1_bus { +u64 search_id; +}; + +struct w1_device { +u64 id; +}; + +static int w1_new_device (struct udevice *bus,u64 id)
Please add function comment - i.e. what this does, args.
Quoted 29 lines
drivers/w1/w1-uclass.c new file +29
-0+{ +struct w1_driver_entry *start, *entry; +int n_ents, ret = -ENODEV ; +u8 family = id &0xff ; + +debug ("%s: Detected new device 0x%llx (family 0x%x)\n" , + bus->name, id, family); + + start = ll_entry_start(struct w1_driver_entry, w1_driver_entry); + n_ents = ll_entry_count(struct w1_driver_entry, w1_driver_entry); + +for (entry = start; entry != start + n_ents; entry++) { +const struct driver *drv; +struct w1_device *w1; +struct udevice *dev; + +if (entry->family != family) +continue ; + + drv = entry->driver; + ret =device_bind (bus, drv, drv->name,NULL ,-1 , + &dev); +if (ret) + goto error; + +debug ("%s: Match found: %s\n" , __func__, drv->name); + + w1 =dev_get_parent_platdata (dev); + w1->id = id;
drivers/w1/w1-uclass.c line 67
Quoted 10 lines
drivers/w1/w1-uclass.c new file +10
-0+ +return 0 ; + } + +error: +debug ("%s: No matches found: error %d\n" , __func__, ret); +return ret; +} + +static int w1_enumerate (struct udevice *bus)
Please add function comment - i.e. what this does, args.
Quoted 35 lines
drivers/w1/w1-uclass.c new file +35
-0+{ +const struct w1_ops *ops =device_get_ops (bus); +struct w1_bus *w1 =dev_get_uclass_priv (bus); +u64 last_rn, rn = w1->search_id, tmp64; +bool last_device = false; +int search_bit, desc_bit =64 ; +int last_zero =-1 ; +u8 triplet_ret =0 ; +int i; + +if (!ops->reset || !ops->write_byte || !ops->triplet) +return -ENOSYS ; + +while ( !last_device ) { + last_rn = rn; + rn =0 ; + +/* +* Reset bus and all 1-wire device state machines +* so they can respond to our requests. +* +* Return 0 - device(s) present, 1 - no devices present. +*/ +if (ops->reset (bus)) { +debug ("%s: No devices present on the wire.\n" , + __func__); +break ; + } + +/* Start the search */ + ops->write_byte (bus,W1_SEARCH ); +for (i =0 ; i <64 ; ++i) { +/* Determine the direction/search bit */ +if (i == desc_bit) + search_bit =1 ;/* took the 0 path last time, so take the 1 path */
drivers/w1/w1-uclass.c line 112
Quoted 10 lines
drivers/w1/w1-uclass.c new file +10
-0+else if (i > desc_bit) + search_bit =0 ;/* take the 0 path on the next branch */ +else + search_bit = ((last_rn >> i) &0x1 ); + +/* Read two bits and write one bit */ + triplet_ret = ops->triplet (bus, search_bit); + +/* quit if no device responded */ +if ( (triplet_ret &0x03 ) ==0x03 )
No space before bracket. Also what is 3? Can this and the mask be an enum / #define?
Quoted 11 lines
drivers/w1/w1-uclass.c new file +11
-0+break ; + +/* If both directions were valid, and we took the 0 path... */ +if (triplet_ret ==0 ) + last_zero = i; + +/* extract the direction taken & update the device number */ + tmp64 = (triplet_ret >>2 ); + rn |= (tmp64 << i); + } +
drivers/w1/w1-uclass.c line 133
Quoted 10 lines
drivers/w1/w1-uclass.c new file +10
-0+if ( (triplet_ret &0x03 ) !=0x03 ) { +if ((desc_bit == last_zero) || (last_zero <0 )) { + last_device =1 ; + w1->search_id =0 ; + }else { + w1->search_id = rn; + } + desc_bit = last_zero; + +w1_new_device (bus, rn);
Check error?
Quoted 14 lines
drivers/w1/w1-uclass.c new file +14
-0+ } + } + +return 0 ; +} + +int w1_get_bus (int busnum,struct udevice **busp) +{ +int ret; + + ret =uclass_get_device_by_seq (UCLASS_W1 , busnum, busp); +if (ret) { +debug ("Cannot find w1 bus %d\n" , busnum); +return ret;
drivers/w1/w1-uclass.c line 157
Quoted 10 lines
drivers/w1/w1-uclass.c new file +10
-0+ } + +return 0 ; +} + +u8 w1_get_device_family (struct udevice *dev) +{ +struct w1_device *w1 =dev_get_parent_platdata (dev); + +return w1->id &0xff ;
Probably need a #define for the 0xff. W1_FAMILY_MASK?
drivers/w1/w1-uclass.c line 170
Quoted 13 lines
drivers/w1/w1-uclass.c new file +13
-0+} + +int w1_reset_select (struct udevice *dev) +{ +struct w1_device *w1 =dev_get_parent_platdata (dev); +struct udevice *bus =dev_get_parent (dev); +const struct w1_ops *ops =device_get_ops (bus); +int i; + +if (!ops->reset || !ops->write_byte) +return -ENOSYS ; + + ops->reset (bus);
Error check?
drivers/w1/w1-uclass.c line 172
Quoted 2 lines
drivers/w1/w1-uclass.c new file +2
-0+ + ops->write_byte (bus,W1_MATCH_ROM );
Error check?
drivers/w1/w1-uclass.c line 175
Quoted 3 lines
drivers/w1/w1-uclass.c new file +3
-0+ +for (i =0 ; i <sizeof (w1->id); i++) + ops->write_byte (bus, (w1->id >> (i *8 )) &0xff );
Error check? And below.
Quoted 50 lines
drivers/w1/w1-uclass.c new file +50
-0+ +return 0 ; +} + +int w1_read_byte (struct udevice *dev) +{ +struct udevice *bus =dev_get_parent (dev); +const struct w1_ops *ops =device_get_ops (bus); + +if (!ops->read_byte) +return -ENOSYS ; + +return ops->read_byte (bus); +} + +int w1_read_buf (struct udevice *dev,u8 *buf,unsigned count) +{ +int i; + +for (i =0 ; i < count; i++) { +int ret =w1_read_byte (dev); +if (ret <0 ) +return ret; + + buf[i] = ret &0xff ; + } + +return 0 ; +} + +int w1_write_byte (struct udevice *dev,u8 byte) +{ +struct udevice *bus =dev_get_parent (dev); +const struct w1_ops *ops =device_get_ops (bus); + +if (!ops->write_byte) +return -ENOSYS ; + + ops->write_byte (bus, byte); + +return 0 ; +} + +static int w1_post_probe (struct udevice *bus) +{ +w1_enumerate (bus); + +return 0 ; +} +
drivers/w1/w1-uclass.c line 235
Quoted 10 lines
drivers/w1/w1-uclass.c new file +10
-0+UCLASS_DRIVER (w1) = { + .name ="w1" , + .id =UCLASS_W1 , + .flags =DM_UC_FLAG_SEQ_ALIAS , + .per_device_auto_alloc_size =sizeof (struct w1_bus ), + .per_child_platdata_auto_alloc_size =sizeof (struct w1_device ), + .post_probe = w1_post_probe, +}; + +int w1_init (void )
Why do we need this? Is it because we cannot discover the devices dynamically?
Also, can we use device tree to specify the devices? At present it seems to only support probing.
Quoted 63 lines
drivers/w1/w1-uclass.c new file +24
-0+{ +struct udevice *bus; +struct uclass *uc; +int ret; + + ret =uclass_get (UCLASS_W1 , &uc); +if (ret) +return ret; + + uclass_foreach_dev(bus, uc) { + ret =device_probe (bus); +if (ret == -ENODEV ) {/* No such device. */ +printf ("W1 controller not available.\n" ); +continue ; + } + +if (ret) {/* Other error. */ +printf ("W1 controller probe failed, error %d\n" , ret); +continue ; + } + } + +return 0 ; +}include/dm/uclass-id.h +1
-0@@ -82,6 +82,7 @@ enum uclass_id {UCLASS_VIDEO ,/* Video or LCD device */ UCLASS_VIDEO_BRIDGE ,/* Video bridge, e.g. DisplayPort to LVDS */ UCLASS_VIDEO_CONSOLE ,/* Text console driver for video device */ +UCLASS_W1 ,/* Dallas 1-Wire bus */ UCLASS_COUNT , UCLASS_INVALID = -1,include/w1.h new file +21
-0@@ -0,0 +1,47 @@ +/* +* Copyright (c) 2015 Free Electrons +* Copyright (c) 2015 NextThing Co +* +* SPDX-License-Identifier: GPL-2.0+ +*/ + +#ifndef __W1_H +#define __W1_H + +#include <dm.h> + +#define W1_FAMILY_DS2431 0x2d + +/** + * struct w1_driver_entry - Matches a driver to its w1 family + * @driver: Driver to use + * @family: W1 family handled by this driver + */ +struct w1_driver_entry { + struct driver *driver;
include/w1.h line 31
Quoted 10 lines
include/w1.h new file +10
-0+u8 family; +}; + +#define U_BOOT_W1_DEVICE (__name, __family) \ + ll_entry_declare(struct w1_driver_entry, __name, w1_driver_entry) = { \ + .driver = llsym(struct driver, __name, driver), \ + .family = __family, \ + } + +struct w1_ops {
Please can you add function comments here and below.
include/w1.h line 32
Quoted 1 line
include/w1.h new file +1
-0+u8 (*read_byte)(struct udevice *dev);
Please don't use u8 as an arg or return type. It adds masking and can bloat the code. int or uint is fine.
include/w1.h line 34
Quoted 2 lines
include/w1.h new file +2
-0+bool (*reset)(struct udevice *dev); +u8 (*triplet)(struct udevice *dev,bool bdir);
What does this do?
Quoted 14 lines
include/w1.h new file +13
-1+ void (*write_byte)(struct udevice *dev, u8 byte); +}; + +int w1_get_bus (int busnum,struct udevice **busp); +u8 w1_get_device_family (struct udevice *dev); + +int w1_read_buf (struct udevice *dev,u8 *buf,unsigned count); +int w1_read_byte (struct udevice *dev); +int w1_reset_select (struct udevice *dev); +int w1_write_buf (struct udevice *dev,u8 *buf,unsigned count); +int w1_write_byte (struct udevice *dev,u8 byte); + +#endif --
Quoted 1 line
git-series 0.8.11
Regards,
Simon