@@ -337,6 +337,158 @@ static void linuxgpio_sysfs_close(PROGRAMMER *pgm)
337337
338338#ifdef HAVE_LIBGPIOD
339339
340+ #ifdef HAVE_LIBGPIOD_V2
341+
342+ struct gpiod_line {
343+ struct gpiod_chip * chip ;
344+ struct gpiod_line_request * line_request ;
345+ unsigned int gpio_num ;
346+ };
347+
348+ struct gpiod_line * gpiod_line_get (const char * port , int gpio_num ) {
349+ struct gpiod_line * rv ;
350+
351+ rv = calloc (sizeof (struct gpiod_line ), 1 );
352+ if (!rv )
353+ return NULL ;
354+
355+ rv -> gpio_num = gpio_num ;
356+
357+ rv -> chip = gpiod_chip_open (port );
358+ if (!rv -> chip ) {
359+ free (rv );
360+ return NULL ;
361+ }
362+
363+ return rv ;
364+ }
365+
366+ int gpiod_line_request_input (struct gpiod_line * gpio_line , const char * consumer ) {
367+ struct gpiod_line_settings * line_settings = NULL ;
368+ struct gpiod_line_config * line_config = NULL ;
369+ struct gpiod_request_config * req_cfg = NULL ;
370+ int retval = -1 ;
371+
372+ line_settings = gpiod_line_settings_new ();
373+ line_config = gpiod_line_config_new ();
374+ req_cfg = gpiod_request_config_new ();
375+
376+ if (!line_settings || !line_config || !req_cfg )
377+ goto err_out ;
378+
379+ retval = gpiod_line_settings_set_direction (line_settings , GPIOD_LINE_DIRECTION_INPUT );
380+ if (retval != 0 )
381+ goto err_out ;
382+
383+ retval = gpiod_line_config_add_line_settings (line_config , & gpio_line -> gpio_num , 1 , line_settings );
384+ if (retval != 0 )
385+ goto err_out ;
386+
387+ gpiod_request_config_set_consumer (req_cfg , consumer );
388+
389+ gpio_line -> line_request = gpiod_chip_request_lines (gpio_line -> chip , req_cfg , line_config );
390+ if (!gpio_line -> line_request )
391+ goto err_out ;
392+
393+ retval = 0 ;
394+
395+ err_out :
396+ gpiod_line_settings_free (line_settings );
397+ gpiod_line_config_free (line_config );
398+ gpiod_request_config_free (req_cfg );
399+ return retval ;
400+ }
401+
402+ int gpiod_line_request_output (struct gpiod_line * gpio_line , const char * consumer , int value ) {
403+ struct gpiod_line_settings * line_settings = NULL ;
404+ struct gpiod_line_config * line_config = NULL ;
405+ struct gpiod_request_config * req_cfg = NULL ;
406+ int retval = -1 ;
407+
408+ line_settings = gpiod_line_settings_new ();
409+ line_config = gpiod_line_config_new ();
410+ req_cfg = gpiod_request_config_new ();
411+
412+ if (!line_settings || !line_config || !req_cfg )
413+ goto err_out ;
414+
415+ retval = gpiod_line_settings_set_direction (line_settings , GPIOD_LINE_DIRECTION_OUTPUT );
416+ if (retval != 0 )
417+ goto err_out ;
418+
419+ retval = gpiod_line_settings_set_output_value (line_settings , value ? GPIOD_LINE_VALUE_ACTIVE : GPIOD_LINE_VALUE_INACTIVE );
420+ if (retval != 0 )
421+ goto err_out ;
422+
423+ retval = gpiod_line_config_add_line_settings (line_config , & gpio_line -> gpio_num , 1 , line_settings );
424+ if (retval != 0 )
425+ goto err_out ;
426+
427+ gpiod_request_config_set_consumer (req_cfg , consumer );
428+
429+ gpio_line -> line_request = gpiod_chip_request_lines (gpio_line -> chip , req_cfg , line_config );
430+ if (!gpio_line -> line_request )
431+ goto err_out ;
432+
433+ retval = 0 ;
434+
435+ err_out :
436+ gpiod_line_settings_free (line_settings );
437+ gpiod_line_config_free (line_config );
438+ gpiod_request_config_free (req_cfg );
439+ return retval ;
440+ }
441+
442+ int gpiod_line_set_direction_input (struct gpiod_line * gpio_line ) {
443+ struct gpiod_line_settings * line_settings = NULL ;
444+ struct gpiod_line_config * line_config = NULL ;
445+ int retval = -1 ;
446+
447+ line_settings = gpiod_line_settings_new ();
448+ line_config = gpiod_line_config_new ();
449+
450+ if (!line_settings || !line_config )
451+ goto err_out ;
452+
453+ retval = gpiod_line_settings_set_direction (line_settings , GPIOD_LINE_DIRECTION_INPUT );
454+ if (retval != 0 )
455+ goto err_out ;
456+
457+ retval = gpiod_line_config_add_line_settings (line_config , & gpio_line -> gpio_num , 1 , line_settings );
458+ if (retval != 0 )
459+ goto err_out ;
460+
461+ retval = gpiod_line_request_reconfigure_lines (gpio_line -> line_request , line_config );
462+
463+ err_out :
464+ gpiod_line_settings_free (line_settings );
465+ gpiod_line_config_free (line_config );
466+ return retval ;
467+ }
468+
469+ /* this helper is not thread safe, but we are not using threads... */
470+ char * gpiod_line_name (struct gpiod_line * gpio_line ) {
471+ static char buffer [16 ];
472+ snprintf (buffer , sizeof (buffer ), "%u" , gpio_line -> gpio_num );
473+ return buffer ;
474+ }
475+
476+ void gpiod_line_release (struct gpiod_line * gpio_line ) {
477+ gpiod_line_request_release (gpio_line -> line_request );
478+ gpiod_chip_close (gpio_line -> chip );
479+ free (gpio_line );
480+ }
481+
482+ static inline int gpiod_line_set_value (struct gpiod_line * gpio_line , int value ) {
483+ return gpiod_line_request_set_value (gpio_line -> line_request , gpio_line -> gpio_num , value );
484+ }
485+
486+ static inline int gpiod_line_get_value (struct gpiod_line * gpio_line ) {
487+ return gpiod_line_request_get_value (gpio_line -> line_request , gpio_line -> gpio_num );
488+ }
489+
490+ #endif
491+
340492struct gpiod_line * linuxgpio_libgpiod_lines [N_PINS ];
341493
342494// Try to tell if libgpiod is going to work.
0 commit comments