44 *
55 * Copyright 2013 Philipp Zabel, Pengutronix
66 */
7+
8+ #include <linux/acpi.h>
79#include <linux/atomic.h>
10+ #include <linux/auxiliary_bus.h>
811#include <linux/cleanup.h>
912#include <linux/device.h>
1013#include <linux/err.h>
1114#include <linux/export.h>
12- #include <linux/kernel.h>
13- #include <linux/kref.h>
1415#include <linux/gpio/driver.h>
1516#include <linux/gpio/machine.h>
17+ #include <linux/gpio/property.h>
1618#include <linux/idr.h>
19+ #include <linux/kernel.h>
20+ #include <linux/kref.h>
1721#include <linux/module.h>
1822#include <linux/of.h>
19- #include <linux/acpi.h>
20- #include <linux/platform_device.h>
2123#include <linux/reset.h>
2224#include <linux/reset-controller.h>
2325#include <linux/slab.h>
@@ -76,10 +78,12 @@ struct reset_control_array {
7678/**
7779 * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices
7880 * @of_args: phandle to the reset controller with all the args like GPIO number
81+ * @swnode: Software node containing the reference to the GPIO provider
7982 * @list: list entry for the reset_gpio_lookup_list
8083 */
8184struct reset_gpio_lookup {
8285 struct of_phandle_args of_args ;
86+ struct fwnode_handle * swnode ;
8387 struct list_head list ;
8488};
8589
@@ -848,65 +852,56 @@ static void __reset_control_put_internal(struct reset_control *rstc)
848852 kref_put (& rstc -> refcnt , __reset_control_release );
849853}
850854
851- static int __reset_add_reset_gpio_lookup (int id , struct device_node * np ,
852- unsigned int gpio ,
853- unsigned int of_flags )
855+ static void reset_gpio_aux_device_release (struct device * dev )
854856{
855- const struct fwnode_handle * fwnode = of_fwnode_handle (np );
856- unsigned int lookup_flags ;
857- const char * label_tmp ;
857+ struct auxiliary_device * adev = to_auxiliary_dev (dev );
858858
859- /*
860- * Later we map GPIO flags between OF and Linux, however not all
861- * constants from include/dt-bindings/gpio/gpio.h and
862- * include/linux/gpio/machine.h match each other.
863- */
864- if (of_flags > GPIO_ACTIVE_LOW ) {
865- pr_err ("reset-gpio code does not support GPIO flags %u for GPIO %u\n" ,
866- of_flags , gpio );
867- return - EINVAL ;
868- }
869-
870- struct gpio_device * gdev __free (gpio_device_put ) = gpio_device_find_by_fwnode (fwnode );
871- if (!gdev )
872- return - EPROBE_DEFER ;
873-
874- label_tmp = gpio_device_get_label (gdev );
875- if (!label_tmp )
876- return - EINVAL ;
859+ kfree (adev );
860+ }
877861
878- char * label __free (kfree ) = kstrdup (label_tmp , GFP_KERNEL );
879- if (!label )
880- return - ENOMEM ;
862+ static int reset_add_gpio_aux_device (struct device * parent ,
863+ struct fwnode_handle * swnode ,
864+ int id , void * pdata )
865+ {
866+ struct auxiliary_device * adev ;
867+ int ret ;
881868
882- /* Size: one lookup entry plus sentinel */
883- struct gpiod_lookup_table * lookup __free (kfree ) = kzalloc (struct_size (lookup , table , 2 ),
884- GFP_KERNEL );
885- if (!lookup )
869+ adev = kzalloc (sizeof (* adev ), GFP_KERNEL );
870+ if (!adev )
886871 return - ENOMEM ;
887872
888- lookup -> dev_id = kasprintf (GFP_KERNEL , "reset-gpio.%d" , id );
889- if (!lookup -> dev_id )
890- return - ENOMEM ;
873+ adev -> id = id ;
874+ adev -> name = "gpio" ;
875+ adev -> dev .parent = parent ;
876+ adev -> dev .platform_data = pdata ;
877+ adev -> dev .release = reset_gpio_aux_device_release ;
878+ device_set_node (& adev -> dev , swnode );
891879
892- lookup_flags = GPIO_PERSISTENT ;
893- lookup_flags |= of_flags & GPIO_ACTIVE_LOW ;
894- lookup -> table [0 ] = GPIO_LOOKUP (no_free_ptr (label ), gpio , "reset" ,
895- lookup_flags );
880+ ret = auxiliary_device_init (adev );
881+ if (ret ) {
882+ kfree (adev );
883+ return ret ;
884+ }
896885
897- /* Not freed on success, because it is persisent subsystem data. */
898- gpiod_add_lookup_table (no_free_ptr (lookup ));
886+ ret = __auxiliary_device_add (adev , "reset" );
887+ if (ret ) {
888+ auxiliary_device_uninit (adev );
889+ kfree (adev );
890+ return ret ;
891+ }
899892
900- return 0 ;
893+ return ret ;
901894}
902895
903896/*
904897 * @args: phandle to the GPIO provider with all the args like GPIO number
905898 */
906899static int __reset_add_reset_gpio_device (const struct of_phandle_args * args )
907900{
901+ struct property_entry properties [2 ] = { };
902+ unsigned int offset , of_flags , lflags ;
908903 struct reset_gpio_lookup * rgpio_dev ;
909- struct platform_device * pdev ;
904+ struct device * parent ;
910905 int id , ret ;
911906
912907 /*
@@ -925,6 +920,28 @@ static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
925920 */
926921 lockdep_assert_not_held (& reset_list_mutex );
927922
923+ offset = args -> args [0 ];
924+ of_flags = args -> args [1 ];
925+
926+ /*
927+ * Later we map GPIO flags between OF and Linux, however not all
928+ * constants from include/dt-bindings/gpio/gpio.h and
929+ * include/linux/gpio/machine.h match each other.
930+ *
931+ * FIXME: Find a better way of translating OF flags to GPIO lookup
932+ * flags.
933+ */
934+ if (of_flags > GPIO_ACTIVE_LOW ) {
935+ pr_err ("reset-gpio code does not support GPIO flags %u for GPIO %u\n" ,
936+ of_flags , offset );
937+ return - EINVAL ;
938+ }
939+
940+ struct gpio_device * gdev __free (gpio_device_put ) =
941+ gpio_device_find_by_fwnode (of_fwnode_handle (args -> np ));
942+ if (!gdev )
943+ return - EPROBE_DEFER ;
944+
928945 guard (mutex )(& reset_gpio_lookup_mutex );
929946
930947 list_for_each_entry (rgpio_dev , & reset_gpio_lookup_list , list ) {
@@ -934,6 +951,10 @@ static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
934951 }
935952 }
936953
954+ lflags = GPIO_PERSISTENT | (of_flags & GPIO_ACTIVE_LOW );
955+ parent = gpio_device_to_device (gdev );
956+ properties [0 ] = PROPERTY_ENTRY_GPIO ("reset-gpios" , parent -> fwnode , offset , lflags );
957+
937958 id = ida_alloc (& reset_gpio_ida , GFP_KERNEL );
938959 if (id < 0 )
939960 return id ;
@@ -945,32 +966,33 @@ static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
945966 goto err_ida_free ;
946967 }
947968
948- ret = __reset_add_reset_gpio_lookup (id , args -> np , args -> args [0 ],
949- args -> args [1 ]);
950- if (ret < 0 )
951- goto err_kfree ;
952-
953969 rgpio_dev -> of_args = * args ;
954970 /*
955971 * We keep the device_node reference, but of_args.np is put at the end
956972 * of __of_reset_control_get(), so get it one more time.
957973 * Hold reference as long as rgpio_dev memory is valid.
958974 */
959975 of_node_get (rgpio_dev -> of_args .np );
960- pdev = platform_device_register_data (NULL , "reset-gpio" , id ,
961- & rgpio_dev -> of_args ,
962- sizeof (rgpio_dev -> of_args ));
963- ret = PTR_ERR_OR_ZERO (pdev );
976+
977+ rgpio_dev -> swnode = fwnode_create_software_node (properties , NULL );
978+ if (IS_ERR (rgpio_dev -> swnode )) {
979+ ret = PTR_ERR (rgpio_dev -> swnode );
980+ goto err_put_of_node ;
981+ }
982+
983+ ret = reset_add_gpio_aux_device (parent , rgpio_dev -> swnode , id ,
984+ & rgpio_dev -> of_args );
964985 if (ret )
965- goto err_put ;
986+ goto err_del_swnode ;
966987
967988 list_add (& rgpio_dev -> list , & reset_gpio_lookup_list );
968989
969990 return 0 ;
970991
971- err_put :
992+ err_del_swnode :
993+ fwnode_remove_software_node (rgpio_dev -> swnode );
994+ err_put_of_node :
972995 of_node_put (rgpio_dev -> of_args .np );
973- err_kfree :
974996 kfree (rgpio_dev );
975997err_ida_free :
976998 ida_free (& reset_gpio_ida , id );
0 commit comments