1

I am trying to learn about Linux platform drivers. I have taken a driver from the following tutorial:

http://linuxseekernel.blogspot.com/2014/05/platform-device-driver-practical.html

It is a basic platform driver. I have compiled it and loaded the module. It loads fine, however, its probe function is never executed. There is a lot of documentation that has said as long as the devices id and drivers id match, then the probe function is called. Well I have the following driver:

#include <linux/module.h>
#include <linux/kernel.h>
//for platform drivers....
#include <linux/platform_device.h>
#define DRIVER_NAME "twl12xx"

MODULE_LICENSE("GPL");

/**************/
static int sample_drv_probe(struct platform_device *pdev){
    printk(KERN_ALERT "twl12xx: Probed\n");
    return 0;
}
static int sample_drv_remove(struct platform_device *pdev){
    printk(KERN_ALERT "twl12xx: Removing twl12xx\n");
    return 0;
}

static const struct platform_device_id twl12xx_id_table[] = {
    { "twl12xx", 0},
    {}
};
MODULE_DEVICE_TABLE(platform, twl12xx_id_table);

static struct platform_driver sample_pldriver = {
    .probe          = sample_drv_probe,
    .remove         = sample_drv_remove,
    .driver = {
            .name  = DRIVER_NAME,
    },
};
/**************/

int ourinitmodule(void)
{
    printk(KERN_ALERT "\n Welcome to twl12xx driver.... \n");

    /* Registering with Kernel */
    platform_driver_register(&sample_pldriver);

    return 0;
}

void ourcleanupmodule(void)
{
    printk(KERN_ALERT "\n Thanks....Exiting twl12xx driver... \n");

    /* Unregistering from Kernel */
    platform_driver_unregister(&sample_pldriver);

    return;
}

module_init(ourinitmodule);
module_exit(ourcleanupmodule);

I also have the following entry in my device tree:

twl12xx: twl12xx@2 {
    compatible = "twl12xx";
};

I feel like I am must be missing something or incorrectly defining my device tree.

user2899525
  • 105
  • 10
  • You're using a guide that is not intended for Device Tree or Open Firmware; the author is probably x86-centric. Your driver as written is not DT-ready, e.g. it doesn't include **linux/of.h**, and doesn't have a `struct of_device_id` with the compatible string. There are numerous platform drivers in the kernel to use as examples. Also see http://stackoverflow.com/questions/26840267/driver-code-in-kernel-module-doesnt-execute/26855205#26855205 – sawdust Mar 08 '17 at 21:48
  • Possible duplicate of [Driver code in kernel module doesn't execute?](http://stackoverflow.com/questions/26840267/driver-code-in-kernel-module-doesnt-execute) – sawdust Mar 08 '17 at 21:58
  • @sawdust, there is no more x86-centric code and I wish should be a bit less DT-centric, the mentioned guide might be outdated. Nowadays, unified device properties among the other features allows to write drivers disregard who is the resource provider. So, don't blame architecture, blame developers, who make XYZ-centric code. – 0andriy Mar 09 '17 at 09:18
  • @0andriy -- Your rant makes no sense, as you seem to be reading something from by comment that just is not there, e.g. *"blame architecture"*. – sawdust Mar 09 '17 at 23:57

2 Answers2

1

Whatever you have read is correct; driver and device ID both should match.

You have just create the skeleton of driver and you are using the device tree. So it looks fine. But you are missing the of_match_table entry in your code; which is very important for device ID match (same string as you have pass from the device tree) and driver probe calling.

So add the below changes your code:

#ifdef CONFIG_OF
static const struct of_device_id twl12xx_dt_ids[] = {
 .compatible = "ti,twl12xx",
 {}
};
MODULE_DEVICE_TABLE(of, twl12xx_dt_ids);
#endif

static struct platform_driver sample_pldriver = {
.probe          = sample_drv_probe,
.remove         = sample_drv_remove,
.driver = {
        .name  = DRIVER_NAME,
        .of_match_table = of_match_ptr(twl12xx_dt_ids),
 },
.id_table    = twl12xx_id_table,
};
vinod maverick
  • 670
  • 4
  • 14
0

I had similar problem. Probe function also wasn't able to print anything. The reason in my case was: In my linux I had ready driver that was binded to the device . When I unbinded this driver, Probe function worked successfully.

(Note, to unbind:

    cd /sys/bus/platform/drivers/<driver_name>
    echo "device_name" > unbind

)

moibrahim
  • 89
  • 7