I found the answer to the question:
static inline const char *pci_slot_name(const struct pci_slot *slot)
https://elixir.bootlin.com/linux/v6.12/source/include/linux/pci.h#L84
is the function that returns the physical slot number, same as lspci. It can be obtained from a struct pci_dev
pointer with:
const char *phys_slot_num=pci_slot_name(/*struct pci_dev*/ pdev->slot);