int alsps_driver_add(struct alsps_init_info* obj) { int err=0; int i =0; ALSPS_FUN(); for(i =0; i < MAX_CHOOSE_ALSPS_NUM; i++ ) { if((i == 0) && (NULL == alsps_init_list[0])){ ALSPS_LOG("register alsps driver for the first time\n"); if(platform_driver_register(&als_ps_driver)) { ALSPS_ERR("failed to register alsps driver already exist\n"); } } if(NULL == alsps_init_list[i]) { obj->platform_diver_addr = &als_ps_driver; alsps_init_list[i] = obj; break; } } if(NULL==alsps_init_list[i]) { ALSPS_ERR("ALSPS driver add err \n"); err=-1; } return err; }
//AAL functions**************************************** int alsps_aal_enable(int enable) { int ret = 0; struct alsps_context *cxt = NULL; if(!alsps_context_obj){ ALSPS_ERR("null pointer of alsps_context_obj!!\n"); return -1; } if(alsps_context_obj->als_ctl.enable_nodata == NULL){ ALSPS_ERR("alsps context obj not exsit in alsps_aal_enable\n"); return -1; } cxt = alsps_context_obj; if(enable == 1){ if(alsps_context_obj->is_als_active_data == false) ret = cxt->als_ctl.enable_nodata(enable); }else if(enable == 0){ if(alsps_context_obj->is_als_active_data == false) ret = cxt->als_ctl.enable_nodata(enable); } return ret; }
static int alsps_probe(struct platform_device *pdev) { int err; ALSPS_LOG("alsps_probe func called"); ALSPS_LOG("+++++++++++++alsps_probe!!\n"); alsps_context_obj = alsps_context_alloc_object(); if (!alsps_context_obj) { err = -ENOMEM; ALSPS_ERR("unable to allocate devobj!\n"); goto exit_alloc_data_failed; } //init real alspseleration driver err = alsps_real_driver_init(); if(err) { ALSPS_ERR("alsps real driver init fail\n"); goto real_driver_init_fail; } //init input dev err = alsps_input_init(alsps_context_obj); if(err) { ALSPS_ERR("unable to register alsps input device!\n"); goto exit_alloc_input_dev_failed; } atomic_set(&(alsps_context_obj->early_suspend), 0); alsps_context_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1, alsps_context_obj->early_drv.suspend = alsps_early_suspend, alsps_context_obj->early_drv.resume = alsps_late_resume, register_early_suspend(&alsps_context_obj->early_drv); ALSPS_LOG("----alsps_probe OK !!\n"); return 0; real_driver_init_fail: exit_alloc_input_dev_failed: kfree(alsps_context_obj); exit_alloc_data_failed: ALSPS_LOG("----alsps_probe fail !!!\n"); return err; }
static ssize_t ps_store_active(struct device* dev, struct device_attribute *attr, const char *buf, size_t count) { struct alsps_context *cxt = NULL; //int err =0; ALSPS_LOG("ps_store_active buf=%s\n",buf); mutex_lock(&alsps_context_obj->alsps_op_mutex); cxt = alsps_context_obj; if (!strncmp(buf, "1", 1)) { ps_enable_data(1); } else if (!strncmp(buf, "0", 1)) { ps_enable_data(0); } else { ALSPS_ERR(" ps_store_active error !!\n"); } mutex_unlock(&alsps_context_obj->alsps_op_mutex); ALSPS_LOG(" ps_store_active done\n"); return count; }
static ssize_t ps_store_delay(struct device* dev, struct device_attribute *attr, const char *buf, size_t count) { int delay=0; int m_delay=0; struct alsps_context *cxt = NULL; mutex_lock(&alsps_context_obj->alsps_op_mutex); cxt = alsps_context_obj; if(NULL == cxt->ps_ctl.set_delay) { ALSPS_LOG("ps_ctl set_delay NULL\n"); mutex_unlock(&alsps_context_obj->alsps_op_mutex); return count; } if (1 != sscanf(buf, "%d", &delay)) { ALSPS_ERR("invalid format!!\n"); mutex_unlock(&alsps_context_obj->alsps_op_mutex); return count; } if(false == cxt->ps_ctl.is_report_input_direct) { m_delay = (int)delay/1000/1000; atomic_set(&alsps_context_obj->delay_ps, m_delay); } cxt->ps_ctl.set_delay(delay); ALSPS_LOG(" ps_delay %d ns\n",delay); mutex_unlock(&alsps_context_obj->alsps_op_mutex); return count; }
static ssize_t ps_store_batch(struct device* dev, struct device_attribute *attr, const char *buf, size_t count) { struct alsps_context *cxt = NULL; //int err =0; ALSPS_LOG("ps_store_batch buf=%s\n",buf); mutex_lock(&alsps_context_obj->alsps_op_mutex); cxt = alsps_context_obj; if(cxt->ps_ctl.is_support_batch){ if (!strncmp(buf, "1", 1)) { cxt->is_ps_batch_enable = true; } else if (!strncmp(buf, "0", 1)) { cxt->is_ps_batch_enable = false; cxt->is_get_valid_ps_data_after_enable = false; } else { ALSPS_ERR(" ps_store_batch error !!\n"); } }else{ ALSPS_LOG(" ps_store_batch not supported\n"); } mutex_unlock(&alsps_context_obj->alsps_op_mutex); ALSPS_LOG(" ps_store_batch done: %d\n", cxt->is_ps_batch_enable); return count; }
static int als_real_enable(int enable) { int err =0; struct alsps_context *cxt = NULL; cxt = alsps_context_obj; if(1==enable) { if(true==cxt->is_als_active_data || true ==cxt->is_als_active_nodata) { err = cxt->als_ctl.enable_nodata(1); if(err) { err = cxt->als_ctl.enable_nodata(1); if(err) { err = cxt->als_ctl.enable_nodata(1); if(err) ALSPS_ERR("alsps enable(%d) err 3 timers = %d\n", enable, err); } } ALSPS_LOG("alsps real enable \n" ); } } if(0 == enable) { if(false==cxt->is_als_active_data && false ==cxt->is_als_active_nodata) { ALSPS_LOG("AAL status is %d\n", aal_use); if(aal_use == 0){ err = cxt->als_ctl.enable_nodata(0); if(err) { ALSPS_ERR("alsps enable(%d) err = %d\n", enable, err); } } ALSPS_LOG("alsps real disable \n" ); } } return err; }
static int ps_real_enable(int enable) { int err =0; struct alsps_context *cxt = NULL; cxt = alsps_context_obj; ALSPS_LOG("ps_real_enable func called"); if(1==enable) { if(true==cxt->is_ps_active_data || true ==cxt->is_ps_active_nodata) { err = cxt->ps_ctl.enable_nodata(1); if(err) { err = cxt->ps_ctl.enable_nodata(1); if(err) { err = cxt->ps_ctl.enable_nodata(1); if(err) ALSPS_ERR("ps enable(%d) err 3 timers = %d\n", enable, err); } } ALSPS_LOG("ps real enable \n" ); } } if(0==enable) { if(false==cxt->is_ps_active_data && false ==cxt->is_ps_active_nodata) { err = cxt->ps_ctl.enable_nodata(0); if(err) { ALSPS_ERR("ps enable(%d) err = %d\n", enable, err); } ALSPS_LOG("ps real disable \n" ); } } return err; }
static int alsps_misc_init(struct alsps_context *cxt) { int err=0; cxt->mdev.minor = MISC_DYNAMIC_MINOR; cxt->mdev.name = ALSPS_MISC_DEV_NAME; if((err = misc_register(&cxt->mdev))) { ALSPS_ERR("unable to register alsps misc device!!\n"); } return err; }
static int __init alsps_init(void) { ALSPS_FUN(); if(platform_driver_register(&alsps_driver)) { ALSPS_ERR("failed to register alsps driver\n"); return -ENODEV; } return 0; }
int als_register_control_path(struct als_control_path *ctl) { struct alsps_context *cxt = NULL; int err =0; cxt = alsps_context_obj; cxt->als_ctl.set_delay = ctl->set_delay; cxt->als_ctl.open_report_data= ctl->open_report_data; cxt->als_ctl.enable_nodata = ctl->enable_nodata; cxt->als_ctl.is_support_batch = ctl->is_support_batch; cxt->als_ctl.is_report_input_direct= ctl->is_report_input_direct; cxt->als_ctl.is_use_common_factory = ctl->is_use_common_factory; if(NULL==cxt->als_ctl.set_delay || NULL==cxt->als_ctl.open_report_data || NULL==cxt->als_ctl.enable_nodata) { ALSPS_LOG("als register control path fail \n"); return -1; } //add misc dev for sensor hal control cmd err = alsps_misc_init(alsps_context_obj); if(err) { ALSPS_ERR("unable to register alsps misc device!!\n"); return -2; } err = sysfs_create_group(&alsps_context_obj->mdev.this_device->kobj, &alsps_attribute_group); if (err < 0) { ALSPS_ERR("unable to create alsps attribute file\n"); return -3; } kobject_uevent(&alsps_context_obj->mdev.this_device->kobj, KOBJ_ADD); return 0; }
static int ps_enable_data(int enable) { struct alsps_context *cxt = NULL; //int err =0; cxt = alsps_context_obj; ALSPS_LOG("ps_enable_data func called"); if(NULL == cxt->ps_ctl.open_report_data) { ALSPS_ERR("no ps control path\n"); return -1; } if(1 == enable) { ALSPS_LOG("PS enable data\n"); cxt->is_ps_active_data =true; cxt->is_ps_first_data_after_enable = true; cxt->ps_ctl.open_report_data(1); if(false == cxt->is_ps_polling_run && cxt->is_ps_batch_enable == false) { if(false == cxt->ps_ctl.is_report_input_direct) { mod_timer(&cxt->timer_ps, jiffies + atomic_read(&cxt->delay_ps)/(1000/HZ)); cxt->is_ps_polling_run = true; cxt->is_get_valid_ps_data_after_enable = false; } } } if(0 == enable) { ALSPS_LOG("PS disable \n"); cxt->is_ps_active_data =false; cxt->ps_ctl.open_report_data(0); if(true == cxt->is_ps_polling_run) { if(false == cxt->ps_ctl.is_report_input_direct ) { cxt->is_ps_polling_run = false; smp_mb(); del_timer_sync(&cxt->timer_ps); smp_mb(); cancel_work_sync(&cxt->report_ps); cxt->drv_data.ps_data.values[0] = ALSPS_INVALID_VALUE; } } } ps_real_enable(enable); return 0; }
int alsps_aal_get_data() { int ret = 0; struct alsps_context *cxt = NULL; int value = 0; int status = 0; if(!alsps_context_obj){ ALSPS_ERR("alsps_context_obj null pointer!!\n"); return -1; } if(alsps_context_obj->als_data.get_data == NULL){ ALSPS_ERR("aal:get_data not exsit\n"); return -1; } cxt = alsps_context_obj; ret = cxt->als_data.get_data(&value,&status); if(ret < 0) return -1; return value; }
static int alsps_remove(struct platform_device *pdev) { int err=0; ALSPS_FUN(f); input_unregister_device(alsps_context_obj->idev); sysfs_remove_group(&alsps_context_obj->idev->dev.kobj, &alsps_attribute_group); if((err = misc_deregister(&alsps_context_obj->mdev))) { ALSPS_ERR("misc_deregister fail: %d\n", err); } kfree(alsps_context_obj); return 0; }
static ssize_t ps_store_batch(struct device* dev, struct device_attribute *attr, const char *buf, size_t count) { struct alsps_context *cxt = NULL; //int err =0; ALSPS_LOG("ps_store_batch buf=%s\n",buf); mutex_lock(&alsps_context_obj->alsps_op_mutex); cxt = alsps_context_obj; if(cxt->ps_ctl.is_support_batch){ if (!strncmp(buf, "1", 1)) { cxt->is_ps_batch_enable = true; if(true == cxt->is_ps_polling_run) { cxt->is_ps_polling_run = false; del_timer_sync(&cxt->timer_ps); cancel_work_sync(&cxt->report_ps); cxt->drv_data.ps_data.values[0] = ALSPS_INVALID_VALUE; cxt->drv_data.ps_data.values[1] = ALSPS_INVALID_VALUE; cxt->drv_data.ps_data.values[2] = ALSPS_INVALID_VALUE; } } else if (!strncmp(buf, "0", 1)) { cxt->is_ps_batch_enable = false; if(false == cxt->is_ps_polling_run) { if(false == cxt->ps_ctl.is_report_input_direct) { mod_timer(&cxt->timer_ps, jiffies + atomic_read(&cxt->delay_ps)/(1000/HZ)); cxt->is_ps_polling_run = true; cxt->is_get_valid_ps_data_after_enable = false; } } } else { ALSPS_ERR(" ps_store_batch error !!\n"); } }else{ ALSPS_LOG(" ps_store_batch not supported\n"); } mutex_unlock(&alsps_context_obj->alsps_op_mutex); ALSPS_LOG(" ps_store_batch done: %d\n", cxt->is_ps_batch_enable); return count; }
static struct alsps_context *alsps_context_alloc_object(void) { struct alsps_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL); ALSPS_LOG("alsps_context_alloc_object++++\n"); ALSPS_LOG("alsps_context func called"); if(!obj) { ALSPS_ERR("Alloc alsps object error!\n"); return NULL; } atomic_set(&obj->delay_als, 200); /*5Hz*/// set work queue delay time 200ms atomic_set(&obj->delay_ps, 200); /*5Hz*/// set work queue delay time 200ms atomic_set(&obj->wake, 0); INIT_WORK(&obj->report_als, als_work_func); INIT_WORK(&obj->report_ps, ps_work_func); init_timer(&obj->timer_als); init_timer(&obj->timer_ps); obj->timer_als.expires = jiffies + atomic_read(&obj->delay_als)/(1000/HZ); obj->timer_als.function = als_poll; obj->timer_als.data = (unsigned long)obj; obj->timer_ps.expires = jiffies + atomic_read(&obj->delay_ps)/(1000/HZ); obj->timer_ps.function = ps_poll; obj->timer_ps.data = (unsigned long)obj; obj->is_als_first_data_after_enable = false; obj->is_als_polling_run = false; obj->is_ps_first_data_after_enable = false; obj->is_ps_polling_run = false; mutex_init(&obj->alsps_op_mutex); obj->is_als_batch_enable = false;//for batch mode init obj->is_ps_batch_enable = false;//for batch mode init ALSPS_LOG("alsps_context_alloc_object----\n"); return obj; }
static int alsps_probe(struct platform_device *pdev) { int err; ALSPS_LOG("+++++++++++++alsps_probe!!\n"); alsps_context_obj = alsps_context_alloc_object(); if (!alsps_context_obj) { err = -ENOMEM; ALSPS_ERR("unable to allocate devobj!\n"); goto exit_alloc_data_failed; } //init real alspseleration driver err = alsps_real_driver_init(); if(err) { ALSPS_ERR("alsps real driver init fail\n"); goto real_driver_init_fail; } //init alsps common factory mode misc device err = alsps_factory_device_init(); if(err) { ALSPS_ERR("alsps factory device already registed\n"); } //init input dev err = alsps_input_init(alsps_context_obj); if(err) { ALSPS_ERR("unable to register alsps input device!\n"); goto exit_alloc_input_dev_failed; } #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND) atomic_set(&(alsps_context_obj->early_suspend), 0); alsps_context_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1, alsps_context_obj->early_drv.suspend = alsps_early_suspend, alsps_context_obj->early_drv.resume = alsps_late_resume, register_early_suspend(&alsps_context_obj->early_drv); #endif ALSPS_LOG("----alsps_probe OK !!\n"); return 0; //exit_hwmsen_create_attr_failed: //exit_misc_register_failed: //exit_err_sysfs: real_driver_init_fail: exit_alloc_input_dev_failed: kfree(alsps_context_obj); alsps_context_obj = NULL; exit_alloc_data_failed: ALSPS_LOG("----alsps_probe fail !!!\n"); return err; }
static void als_work_func(struct work_struct *work) { struct alsps_context *cxt = NULL; //int out_size; //hwm_sensor_data sensor_data; int value=0,status=0; int64_t nt; struct timespec time; int err; cxt = alsps_context_obj; if(NULL == cxt->als_data.get_data) { ALSPS_ERR("alsps driver not register data path\n"); return; } time.tv_sec = time.tv_nsec = 0; time = get_monotonic_coarse(); nt = time.tv_sec*1000000000LL+time.tv_nsec; //add wake lock to make sure data can be read before system suspend err = cxt->als_data.get_data(&value,&status); if(err) { ALSPS_ERR("get alsps data fails!!\n" ); goto als_loop; } else { { cxt->drv_data.als_data.values[0] = value; cxt->drv_data.als_data.status = status; cxt->drv_data.als_data.time = nt; } } if(true == cxt->is_als_first_data_after_enable) { cxt->is_als_first_data_after_enable = false; //filter -1 value if(ALSPS_INVALID_VALUE == cxt->drv_data.als_data.values[0] ) { ALSPS_LOG(" read invalid data \n"); goto als_loop; } } //report data to input device //printk("new alsps work run....\n"); //ALSPS_LOG("als data[%d] \n" ,cxt->drv_data.als_data.values[0]); als_data_report(cxt->idev, cxt->drv_data.als_data.values[0], cxt->drv_data.als_data.status); als_loop: if(true == cxt->is_als_polling_run) { { mod_timer(&cxt->timer_als, jiffies + atomic_read(&cxt->delay_als)/(1000/HZ)); } } }