2018年4月12日 星期四

Press a key to control a led device on i.mx6

根據前一篇Linux input system and keypad driver on i.mx6

經過上面步驟已經可以正確傳遞keypad event並且透過evdev read function讀到key code. 透過key code可以再control led or pwm device.
透過led.h 中的led_trigger_register_simple以及led_trigger_event便可以trigger event set led brightness.
 
Sample:
evdev.c

#include 
struct led_trigger *test_led_trigger = 0;

static int __init evdev_init(void)
{
    led_trigger_register_simple("debug-leds", &test_led_trigger); //註冊test_led_trigger的trigger led label是debug-leds

    return input_register_handler(&evdev_handler);
}

static void __exit evdev_exit(void)
{
    led_trigger_unregister_simple(test_led_trigger);
    test_led_trigger = 0;
 
    input_unregister_handler(&evdev_handler);
}

在device tree中宣告
    leds {
        debug-leds {
            gpios = <&gpio1 2 0>;
            linux,default-trigger = "debug-leds";
            default-state = "off";
        };
};

接著回到evdev.c
static ssize_t evdev_read(struct file *file, char __user *buffer,
     size_t count, loff_t *ppos)
{
…
//此處會讀input event的data
while (read + input_event_size() <= count &&
       evdev_fetch_next_event(client, &event)) {
 printk("event type = %x, code = %x, value = %x\n",event.type, event.code, event.value);
            if(event.code == 0x1c){ //key code is "enter"
                printk("key code is 0x1c\n");
                led_trigger_event(test_led_trigger, LED_FULL); //設定test_led_trigger control 的led 亮度為FULL(255)
            }
 read += input_event_size();
}
…
}

如此在收到特定key code時, "gpio1 2"這顆led便會被開到全亮.
這邊要注意的是若要控制的是pwm device, 那需要先將device宣告為pwm-leds就一樣可以使用led trigger來控制. 只需在led_trigger_event第二個arguments填入0-255的數字就可以設定pwm cycle.
 
device tree example:
pwmleds {
    compatible = "pwm-leds";
    backlightk {
        label = "backlight-leds";
        pwms = <&pwm1 0 8333333>;
        linux,default-trigger = "debug-leds";
        max-brightness = <255>;
    };
};

詳細code流程如下
 
led_triggers.c
void led_trigger_register_simple(const char *name, struct led_trigger **tp)
{
 struct led_trigger *trig;
 int err;

 trig = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);

 if (trig) {
  trig->name = name;
  err = led_trigger_register(trig); //將led_trigger建立到list中
  if (err < 0) {
   kfree(trig);
…
}

Trigger event時
void led_trigger_event(struct led_trigger *trig,
   enum led_brightness brightness)
{
 struct list_head *entry;

 if (!trig)
  return;

 read_lock(&trig->leddev_list_lock);
 list_for_each(entry, &trig->led_cdevs) { //search list to find the trigger
  struct led_classdev *led_cdev;

  led_cdev = list_entry(entry, struct led_classdev, trig_list);
  led_set_brightness(led_cdev, brightness); //找到device之後最終調用device driver的brightness_set做亮度設定
 }
 read_unlock(&trig->leddev_list_lock);
}

Linux input system and keypad driver on i.mx6

Refer to below, register gpio keypad to a  gpio-keys
https://community.nxp.com/thread/343346

Declare keys in device tree
 
keys{
        compatible = "gpio-keys";
        #address-cells = <1>;
        #size-cells = <0>;
        autorepeat;
        keypad-01{
            label = "keypad-01";
            gpios = <&gpio4 30 1>;
            linux,code = <2>; /* KEY_RESERVED  */
        };
        keypad-02{
            label = "keypad-02";
            gpios = <&gpio4 31 1>;
            linux,code = <3>; /* KEY_RESERVED  */
        };
};   
Note: 若設置autorepeat那按下key不放時, input event會一直發. 若沒設定時就只會發一次, 但是Yocto OS在沒收到release event之前會自動做autorepeat的動作.

宣告為"gpio-keys" 或是 "gpio-keys-polled" keypad都可以正常動作. 差異在"gpio-keys-polled"需多定義"poll-interval". 還有在register input device的時候一個是register成input device, 另一個是register成input polled device. 資料結構不同. 以及一個是使用interupt方式, 另一個則是polling mode.


根據以下兩張圖, linux input system 主要分為三個部分, input core, input device driver, input event driver. Input device driver負責control各interface的hardware device. Input event driver負責處理傳入的事件以及向上傳到application. Input core負責串起device driver以及event driver.





Event driver (Ex: evdev.c) init時, 會註冊一個input handler到input system list.

 
static int __init evdev_init(void)
{
	return input_register_handler(&evdev_handler);
}

Device probe時, 會註冊一個device到input system list.

 
error = input_register_polled_device(poll_dev);
if (error) {
	dev_err(dev, "unable to register polled device, err=%d\n",
		error);
	goto err_free_gpio;
}
當device以及handler做register時, input core都會使用input_attach_handler 來搜尋相對應的device以及handler. 若是有找到相對應的device以及handler, 則建出一個input handle供以後使用.
當有找到相對應的device以及handler時, 會調用handler的connect function, 以evdev來說, 就會在connect function當中建出/dev/input/eventX
便可以將event與device driver串在一起.

 
input.c
int input_register_handler(struct input_handler *handler)
{
	struct input_dev *dev;
	int error;

	error = mutex_lock_interruptible(&input_mutex);
	if (error)
		return error;

	INIT_LIST_HEAD(&handler->h_list);

	list_add_tail(&handler->node, &input_handler_list);

	list_for_each_entry(dev, &input_dev_list, node)
		input_attach_handler(dev, handler);

	input_wakeup_procfs_readers();

	mutex_unlock(&input_mutex);
	return 0;
}

int input_register_device(struct input_dev *dev)
{
…
	list_add_tail(&dev->node, &input_dev_list);

	list_for_each_entry(handler, &input_handler_list, node)
		input_attach_handler(dev, handler);

	input_wakeup_procfs_readers();

	mutex_unlock(&input_mutex);
…
}

static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
{
	const struct input_device_id *id;
	int error;

	id = input_match_device(handler, dev);
	if (!id)
		return -ENODEV;

	error = handler->connect(handler, dev, id); //匹配成功就调用handler的connect函数 
	if (error && error != -ENODEV)
		pr_err("failed to attach handler %s to device %s, error: %d\n",
		       handler->name, kobject_name(&dev->dev.kobj), error);

	return error;
}

evdev.c
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
			 const struct input_device_id *id)
{
…
	dev_set_name(&evdev->dev, "event%d", dev_no);

	evdev->handle.dev = input_get_device(dev);
	evdev->handle.name = dev_name(&evdev->dev);
	evdev->handle.handler = handler;
	evdev->handle.private = evdev;
	
	evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
	evdev->dev.class = &input_class;
	evdev->dev.parent = &dev->dev;
	evdev->dev.release = evdev_free;
	device_initialize(&evdev->dev);

	error = input_register_handle(&evdev->handle);
	if (error)
		goto err_free_evdev;

	cdev_init(&evdev->cdev, &evdev_fops);
…
}
到這邊串起之後, 便可以將input device產生的訊息(touch screen座標, key-code…etc)傳給input handler. 或是將input handler的訊息交給input device(如鼠标的闪灯命令).

實際在使用時, 如果按下一個key, 不管是interupt或是polling mode, 都是在收到動作之後, 透過input_event來向input handler傳遞type, code, value這三項內容. 然後便可以從cat /dev/input/eventX 或是調用evdev的read function來得到輸入的資料.

open /dev/input/eventX 之後便可使用evdev_fops的read/write/ioctl function, Yocto在進入os時有open device, 並且read device. 但是還沒找到Yocto在哪邊做open及read.

到這邊就可以完成按下gpio keypad發出一個linux standard keycode.


Reference:
https://blog.csdn.net/yueqian_scut/article/details/47903853
https://blog.csdn.net/yueqian_scut/article/details/48026955
https://blog.csdn.net/qq_695538007/article/details/9153099
https://blog.csdn.net/qq_695538007/article/details/9153135
http://www.jollen.org/blog/2009/04/linux_input_device_apis.html
https://patchwork.kernel.org/patch/2848259/
https://community.nxp.com/thread/343346