繼上一篇:http://www.cnblogs.com/linhaostudy/p/8303628.html#_label1_1 一、驅動流程解析: 1、模塊載入: of_device_id與DTS中的匹配,這與內核2.6以前的i2c_board_info不一樣; 內核載入驅動模塊的時候將調用到st ...
繼上一篇:http://www.cnblogs.com/linhaostudy/p/8303628.html#_label1_1
一、驅動流程解析:
1、模塊載入:
1 static struct of_device_id stk_match_table[] = { 2 { .compatible = "stk,stk3x1x", }, 3 { }, 4 }; 5 6 static struct i2c_driver stk_ps_driver = 7 { 8 .driver = { 9 .name = DEVICE_NAME, 10 .owner = THIS_MODULE, 11 .of_match_table = stk_match_table, 12 }, 13 .probe = stk3x1x_probe, 14 .remove = stk3x1x_remove, 15 .id_table = stk_ps_id, 16 }; 17 18 19 static int __init stk3x1x_init(void) 20 { 21 int ret; 22 ret = i2c_add_driver(&stk_ps_driver); 23 if (ret) 24 return ret; 25 26 return 0; 27 } 28 29 static void __exit stk3x1x_exit(void) 30 { 31 i2c_del_driver(&stk_ps_driver); 32 }
of_device_id與DTS中的匹配,這與內核2.6以前的i2c_board_info不一樣;
內核載入驅動模塊的時候將調用到stk3x1x_init()方法:
初始化了i2c_driver結構體給stk_ps_driver變數,將用於將設備註冊到IIC。關鍵在於結構體中的probe()方法,註冊完成的時候將調用;
2、stk3x1x驅動初始化-probe函數:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 static int stk3x1x_probe(struct i2c_client *client, 2 const struct i2c_device_id *id) 3 { 4 int err = -ENODEV; 5 struct stk3x1x_data *ps_data; 6 struct stk3x1x_platform_data *plat_data; 7 printk(KERN_INFO "%s: driver version = %s\n", __func__, DRIVER_VERSION); 8 9 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 10 { 11 printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_BYTE_DATA\n", __func__); 12 return -ENODEV; 13 } 14 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) 15 { 16 printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_WORD_DATA\n", __func__); 17 return -ENODEV; 18 } 19 20 ps_data = kzalloc(sizeof(struct stk3x1x_data),GFP_KERNEL); 21 if(!ps_data) 22 { 23 printk(KERN_ERR "%s: failed to allocate stk3x1x_data\n", __func__); 24 return -ENOMEM; 25 } 26 ps_data->client = client; 27 i2c_set_clientdata(client,ps_data); 28 mutex_init(&ps_data->io_lock); 29 wake_lock_init(&ps_data->ps_wakelock,WAKE_LOCK_SUSPEND, "stk_input_wakelock"); 30 31 #ifdef STK_POLL_PS 32 wake_lock_init(&ps_data->ps_nosuspend_wl,WAKE_LOCK_SUSPEND, "stk_nosuspend_wakelock"); 33 #endif 34 if (client->dev.of_node) { 35 plat_data = devm_kzalloc(&client->dev, 36 sizeof(struct stk3x1x_platform_data), GFP_KERNEL); 37 if (!plat_data) { 38 dev_err(&client->dev, "Failed to allocate memory\n"); 39 return -ENOMEM; 40 } 41 42 err = stk3x1x_parse_dt(&client->dev, plat_data); 43 dev_err(&client->dev, 44 "%s: stk3x1x_parse_dt ret=%d\n", __func__, err); 45 if (err) 46 return err; 47 } else 48 plat_data = client->dev.platform_data; 49 50 if (!plat_data) { 51 dev_err(&client->dev, 52 "%s: no stk3x1x platform data!\n", __func__); 53 goto err_als_input_allocate; 54 } 55 ps_data->als_transmittance = plat_data->transmittance; 56 ps_data->int_pin = plat_data->int_pin; 57 ps_data->use_fir = plat_data->use_fir; 58 ps_data->pdata = plat_data; 59 60 if (ps_data->als_transmittance == 0) { 61 dev_err(&client->dev, 62 "%s: Please set als_transmittance\n", __func__); 63 goto err_als_input_allocate; 64 } 65 66 ps_data->als_input_dev = devm_input_allocate_device(&client->dev); 67 if (ps_data->als_input_dev==NULL) 68 { 69 printk(KERN_ERR "%s: could not allocate als device\n", __func__); 70 err = -ENOMEM; 71 goto err_als_input_allocate; 72 } 73 ps_data->ps_input_dev = devm_input_allocate_device(&client->dev); 74 if (ps_data->ps_input_dev==NULL) 75 { 76 printk(KERN_ERR "%s: could not allocate ps device\n", __func__); 77 err = -ENOMEM; 78 goto err_als_input_allocate; 79 } 80 ps_data->als_input_dev->name = ALS_NAME; 81 ps_data->ps_input_dev->name = PS_NAME; 82 set_bit(EV_ABS, ps_data->als_input_dev->evbit); 83 set_bit(EV_ABS, ps_data->ps_input_dev->evbit); 84 input_set_abs_params(ps_data->als_input_dev, ABS_MISC, 0, stk_alscode2lux(ps_data, (1<<16)-1), 0, 0); 85 input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0,1, 0, 0); 86 err = input_register_device(ps_data->als_input_dev); 87 if (err<0) 88 { 89 printk(KERN_ERR "%s: can not register als input device\n", __func__); 90 goto err_als_input_allocate; 91 } 92 err = input_register_device(ps_data->ps_input_dev); 93 if (err<0) 94 { 95 printk(KERN_ERR "%s: can not register ps input device\n", __func__); 96 goto err_als_input_allocate; 97 } 98 99 err = sysfs_create_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group); 100 if (err < 0) 101 { 102 printk(KERN_ERR "%s:could not create sysfs group for als\n", __func__); 103 goto err_als_input_allocate; 104 } 105 err = sysfs_create_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group); 106 if (err < 0) 107 { 108 printk(KERN_ERR "%s:could not create sysfs group for ps\n", __func__); 109 goto err_ps_sysfs_create_group; 110 } 111 input_set_drvdata(ps_data->als_input_dev, ps_data); 112 input_set_drvdata(ps_data->ps_input_dev, ps_data); 113 114 #ifdef STK_POLL_ALS 115 ps_data->stk_als_wq = create_singlethread_workqueue("stk_als_wq"); 116 INIT_WORK(&ps_data->stk_als_work, stk_als_work_func); 117 hrtimer_init(&ps_data->als_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 118 ps_data->als_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC); 119 ps_data->als_timer.function = stk_als_timer_func; 120 #endif 121 122 ps_data->stk_ps_wq = create_singlethread_workqueue("stk_ps_wq"); 123 INIT_WORK(&ps_data->stk_ps_work, stk_ps_work_func); 124 hrtimer_init(&ps_data->ps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 125 ps_data->ps_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC); 126 ps_data->ps_timer.function = stk_ps_timer_func; 127 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS)) 128 ps_data->stk_wq = create_singlethread_workqueue("stk_wq"); 129 INIT_WORK(&ps_data->stk_work, stk_work_func); 130 err = stk3x1x_setup_irq(client); 131 if(err < 0) 132 goto err_stk3x1x_setup_irq; 133 #endif 134 135 err = stk3x1x_power_init(ps_data, true); 136 if (err) 137 goto err_power_init; 138 139 err = stk3x1x_power_ctl(ps_data, true); 140 if (err) 141 goto err_power_on; 142 143 ps_data->als_enabled = false; 144 ps_data->ps_enabled = false; 145 #ifdef CONFIG_HAS_EARLYSUSPEND 146 ps_data->stk_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; 147 ps_data->stk_early_suspend.suspend = stk3x1x_early_suspend; 148 ps_data->stk_early_suspend.resume = stk3x1x_late_resume; 149 register_early_suspend(&ps_data->stk_early_suspend); 150 #endif 151 /* make sure everything is ok before registering the class device */ 152 ps_data->als_cdev = sensors_light_cdev; 153 ps_data->als_cdev.sensors_enable = stk_als_enable_set; 154 ps_data->als_cdev.sensors_poll_delay = stk_als_poll_delay_set; 155 err = sensors_classdev_register(&client->dev, &ps_data->als_cdev); 156 if (err) 157 goto err_power_on; 158 159 ps_data->ps_cdev = sensors_proximity_cdev; 160 ps_data->ps_cdev.sensors_enable = stk_ps_enable_set; 161 err = sensors_classdev_register(&client->dev, &ps_data->ps_cdev); 162 if (err) 163 goto err_class_sysfs; 164 165 /* enable device power only when it is enabled */ 166 err = stk3x1x_power_ctl(ps_data, false); 167 if (err) 168 goto err_init_all_setting; 169 170 dev_dbg(&client->dev, "%s: probe successfully", __func__); 171 172 return 0; 173 174 err_init_all_setting: 175 stk3x1x_power_ctl(ps_data, false); 176 sensors_classdev_unregister(&ps_data->ps_cdev); 177 err_class_sysfs: 178 sensors_classdev_unregister(&ps_data->als_cdev); 179 err_power_on: 180 stk3x1x_power_init(ps_data, false); 181 err_power_init: 182 #ifndef STK_POLL_PS 183 free_irq(ps_data->irq, ps_data); 184 gpio_free(plat_data->int_pin); 185 #endif 186 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS)) 187 err_stk3x1x_setup_irq: 188 #endif 189 #ifdef STK_POLL_ALS 190 hrtimer_try_to_cancel(&ps_data->als_timer); 191 destroy_workqueue(ps_data->stk_als_wq); 192 #endif 193 destroy_workqueue(ps_data->stk_ps_wq); 194 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS)) 195 destroy_workqueue(ps_data->stk_wq); 196 #endif 197 sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group); 198 err_ps_sysfs_create_group: 199 sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group); 200 err_als_input_allocate: 201 #ifdef STK_POLL_PS 202 wake_lock_destroy(&ps_data->ps_nosuspend_wl); 203 #endif 204 wake_lock_destroy(&ps_data->ps_wakelock); 205 mutex_destroy(&ps_data->io_lock); 206 kfree(ps_data); 207 return err; 208 }View Code
在stk3x1x_probe函數中主要做了:
1、為驅動私有數據結構體stk3x1x_data分配記憶體空間;
2、 將設備驅動的私有數據(stk3x1x_data)連接到設備client(i2c_client)中;(bma255會增加一步:讀取i2c的id);
3、將stk3x1x驅動註冊到linux input子系統;
4、創建工作隊列(主要是對sensor的數據採集);
5、創建sysfs介面;
2.1 創建input子系統:
http://blog.csdn.net/ielife/article/details/7798952
1、 在驅動載入模塊中,設置你的input設備支持的事件類型;
2、 註冊中斷處理函數,例如鍵盤設備需要編寫按鍵的抬起、放下,觸摸屏設備需要編寫按下、抬起、絕對移動,滑鼠設備需要編寫單擊、抬起、相對移動,並且需要在必要的時候提交硬體數據(鍵值/坐標/狀態等等);
3、將輸入設備註冊到輸入子系統中;
1 ps_data->als_input_dev = devm_input_allocate_device(&client->dev); //分配記憶體空間 2 if (ps_data->als_input_dev==NULL) 3 { 4 printk(KERN_ERR "%s: could not allocate als device\n", __func__); 5 err = -ENOMEM; 6 goto err_als_input_allocate; 7 } 8 ps_data->ps_input_dev = devm_input_allocate_device(&client->dev); 9 if (ps_data->ps_input_dev==NULL) 10 { 11 printk(KERN_ERR "%s: could not allocate ps device\n", __func__); 12 err = -ENOMEM; 13 goto err_als_input_allocate; 14 } 15 ps_data->als_input_dev->name = ALS_NAME; 16 ps_data->ps_input_dev->name = PS_NAME; 17 set_bit(EV_ABS, ps_data->als_input_dev->evbit); 18 set_bit(EV_ABS, ps_data->ps_input_dev->evbit); 19 input_set_abs_params(ps_data->als_input_dev, ABS_MISC, 0, stk_alscode2lux(ps_data, (1<<16)-1), 0, 0); //設置input載入類型; 20 input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0,1, 0, 0); 21 err = input_register_device(ps_data->als_input_dev); 22 if (err<0) 23 { 24 printk(KERN_ERR "%s: can not register als input device\n", __func__); 25 goto err_als_input_allocate; 26 } 27 err = input_register_device(ps_data->ps_input_dev); 28 if (err<0) 29 { 30 printk(KERN_ERR "%s: can not register ps input device\n", __func__); 31 goto err_als_input_allocate; 32 }
1 err = stk3x1x_setup_irq(client); //設置驅動中斷函數 2 if(err < 0) 3 goto err_stk3x1x_setup_irq;
2.2 創建工作隊列:
先提一個問題,為什麼要創建工作隊列?在前面的介紹中我們知道,sensor感測器獲取數據後,將數據傳給controller的寄存器中,供主控去查詢讀取數據。所以這裡創建的工作隊列,就是在一個工作者線程,通過IIC不斷的去查詢讀取controller上的數據。
工作隊列的作用就是把工作推後,交由一個內核線程去執行,更直接的說就是如果寫了一個函數,而現在不想馬上執行它,想在將來某個時刻去執行它,那用工作隊列準沒錯.大概會想到中斷也是這樣,提供一個中斷服務函數,在發生中斷的時候去執行,沒錯,和中斷相比,工作隊列最大的好處就是可以調度可以睡眠,靈活性更好。
上面代碼中我們看到INIT_WORK(&ps_data->stk_ps_work, stk_ps_work_func);,其實是一個巨集的定義,在include/linux/workqueue.h中。stk_ps_work_func()就是我們定義的功能函數,用於查詢讀取Sensor的距離感測器數據的,並上報Input子系統,代碼如下:
1 static void stk_ps_work_func(struct work_struct *work) 2 { 3 struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_ps_work); 4 uint32_t reading; 5 int32_t near_far_state; 6 uint8_t org_flag_reg; 7 int32_t ret; 8 uint8_t disable_flag = 0; 9 mutex_lock(&ps_data->io_lock); 10 11 org_flag_reg = stk3x1x_get_flag(ps_data); 12 if(org_flag_reg < 0) 13 { 14 printk(KERN_ERR "%s: get_status_reg fail, ret=%d", __func__, org_flag_reg); 15 goto err_i2c_rw; 16 } 17 near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0; 18 reading = stk3x1x_get_ps_reading(ps_data); 19 if(ps_data->ps_distance_last != near_far_state) 20 { 21 ps_data->ps_distance_last = near_far_state; 22 input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state); //input上報數據 23 input_sync(ps_data->ps_input_dev); //input_sync()在這裡不起關鍵作用。但如果是一個觸摸屏,即有x坐標和y坐標,則需要通過input_sync()函數把x和y坐標完整地傳遞給輸入子系統。 24 wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ); 25 #ifdef STK_DEBUG_PRINTF 26 printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n",__func__, near_far_state, reading); 27 #endif 28 } 29 ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag); 30 if(ret < 0) 31 { 32 printk(KERN_ERR "%s:stk3x1x_set_flag fail, ret=%d\n", __func__, ret); 33 goto err_i2c_rw; 34 } 35 36 mutex_unlock(&ps_data->io_lock); 37 return; 38 39 err_i2c_rw: 40 mutex_unlock(&ps_data->io_lock); 41 msleep(30); 42 return; 43 }
2.3 創建sysfs介面:
為什麼要創建sysfs介面?在驅動層創建了sysfs介面,HAL層通過這些sysfs介面,對Sensor進行操作,如使能、設置delay等。
DEVICE_ATTR的使用:http://blog.csdn.net/njuitjf/article/details/16849333
函數巨集DEVICE_ATTR內封裝的是__ATTR(_name,_mode,_show,_stroe)方法:
_show:表示的是讀方法,_stroe表示的是寫方法。
1、 調用巨集DEVICE_ATTR完成對功能函數的註冊:
1 static struct device_attribute ps_enable_attribute = __ATTR(enable,0664,stk_ps_enable_show,stk_ps_enable_store); 2 static struct device_attribute ps_enable_aso_attribute = __ATTR(enableaso,0664,stk_ps_enable_aso_show,stk_ps_enable_aso_store); 3 static struct device_attribute ps_distance_attribute = __ATTR(distance,0664,stk_ps_distance_show, stk_ps_distance_store); 4 static struct device_attribute ps_offset_attribute = __ATTR(offset,0664,stk_ps_offset_show, stk_ps_offset_store); 5 static struct device_attribute ps_code_attribute = __ATTR(code, 0444, stk_ps_code_show, NULL); 6 static struct device_attribute ps_code_thd_l_attribute = __ATTR(codethdl,0664,stk_ps_code_thd_l_show,stk_ps_code_thd_l_store); 7 static struct device_attribute ps_code_thd_h_attribute = __ATTR(codethdh,0664,stk_ps_code_thd_h_show,stk_ps_code_thd_h_store); 8 static struct device_attribute recv_attribute = __ATTR(recv,0664,stk_recv_show,stk_recv_store); 9 static struct device_attribute send_attribute = __ATTR(send,0664,stk_send_show, stk_send_store); 10 static struct device_attribute all_reg_attribute = __ATTR(allreg, 0444, stk_all_reg_show, NULL); 11 12 static struct attribute *stk_ps_attrs [] = 13 { 14 &ps_enable_attribute.attr, 15 &ps_enable_aso_attribute.attr, 16 &ps_distance_attribute.attr, 17 &ps_offset_attribute.attr, 18 &ps_code_attribute.attr, 19 &ps_code_thd_l_attribute.attr, 20 &ps_code_thd_h_attribute.attr, 21 &recv_attribute.attr, 22 &send_attribute.attr, 23 &all_reg_attribute.attr, 24 NULL 25 }; 26 27 static struct attribute_group stk_ps_attribute_group = { 28 .attrs = stk_ps_attrs, 29 };
在probe函數中:
1 err = sysfs_create_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group); 2 if (err < 0) 3 { 4 printk(KERN_ERR "%s:could not create sysfs group for als\n", __func__); 5 goto err_als_input_allocate; 6 } 7 err = sysfs_create_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group); 8 if (err < 0) 9 { 10 printk(KERN_ERR "%s:could not create sysfs group for ps\n", __func__); 11 goto err_ps_sysfs_create_group; 12 }
到此,完成了sysfs介面的創建,我們可以在根文件系統中看到/sys/class/input/input1/目錄,在該目錄下我們可以看到多個節點,其中就包含了enable和delay。我們以enable為例子,可以有兩種方法完成對Gsensor的使能工作:
3、讀取上報數據:
在Android的HAL層,通過對/sys/class/input/input3/enable節點的寫操作,使能sensor。調用到的方法是stk_ps_enable_store函數:
1 static struct device_attribute ps_enable_attribute = __ATTR(enable,0664,stk_ps_enable_show,stk_ps_enable_store); 2 static struct device_attribute ps_enable_aso_attribute = __ATTR(enableaso,0664,stk_ps_enable_aso_show,stk_ps_enable_aso_store); 3 static struct device_attribute ps_distance_attribute = __ATTR(distance,0664,stk_ps_distance_show, stk_ps_distance_store); 4 static struct device_attribute ps_offset_attribute = __ATTR(offset,0664,stk_ps_offset_show, stk_ps_offset_store); 5 static struct device_attribute ps_code_attribute = __ATTR(code, 0444, stk_ps_code_show, NULL); 6 static struct device_attribute ps_code_thd_l_attribute = __ATTR(codethdl,0664,stk_ps_code_thd_l_show,stk_ps_code_thd_l_store); 7 static struct device_attribute ps_code_thd_h_attribute = __ATTR(codethdh,0664,stk_ps_code_thd_h_show,stk_ps_code_thd_h_store); 8 static struct device_attribute recv_attribute = __ATTR(recv,0664,stk_recv_show,stk_recv_store); 9 static struct device_attribute send_attribute = __ATTR(send,0664,stk_send_show, stk_send_store); 10 static struct device_attribute all_reg_attribute = __ATTR(allreg, 0444, stk_all_reg_show, NULL);
裡面的show和store函數;
1 static ssize_t stk_ps_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) 2 { 3 struct stk3x1x_data *ps_data = dev_get_drvdata(dev); 4 uint8_t en; 5 if (sysfs_streq(buf, "1")) 6 en = 1; 7 else if (sysfs_streq(buf, "0")) 8 en = 0; 9 else 10 { 11 printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf); 12 return -EINVAL; 13 } 14 dev_dbg(dev, "%s: Enable PS : %d\n", __func__, en); 15 mutex_lock(&ps_data->io_lock); 16 stk3x1x_enable_ps(ps_data, en); 17 mutex_unlock(&ps_data->io_lock); 18 return size; 19 }