static ssize_t proximity_thresh_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t size)
{
	unsigned long threshold;
	int err = 0;

	err = strict_strtoul(buf, 10, &threshold);

	if (err) {
		pr_err("%s, conversion %s to number.\n",
			__func__, buf);
		return err;
	}

	err = gp2a_update_threshold(is_gp2a030a() ?
			gp2a_original_image_030a : gp2a_original_image,
			threshold, true);

	if (err) {
		pr_err("gp2a threshold(with register) update fail.\n");
		return err;
	}

	return size;
}
예제 #2
0
static ssize_t proximity_thresh_diff_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t size)
{
	struct gp2a_data *gp2a = dev_get_drvdata(dev);
	struct gp2a_platform_data *pdata = gp2a->pdata;
	u8 threshold_diff = 0;
	int err;

	err = kstrtou8(buf, 10, &threshold_diff);
	if (err) {
		pr_err("%s, conversion %s to number.\n",
			__func__, buf);
		return err;
	}

	if ((threshold_diff > 0) && (threshold_diff < 5)) { /* update diff */
		gp2a->thresh_diff = threshold_diff;
	} else if (threshold_diff == 0) { /* reset to default */
		pdata->gp2a_get_threshold(&gp2a->thresh_diff);
	} else {
		pr_err("%s: invalid value %d\n", __func__, *buf);
		return -EINVAL;
	}

	gp2a_update_threshold(gp2a, is_gp2a030a() ?
			gp2a_original_image_030a : gp2a_original_image,
			(is_gp2a030a() ?
			gp2a_original_image_030a[3][1] :
			gp2a_original_image[3][1]), true);

	return size;
}
예제 #3
0
static ssize_t proximity_thresh_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t size)
{
	struct gp2a_data *data = dev_get_drvdata(dev);
	u8 threshold = 0;
	int err = 0;

	err = kstrtou8(buf, 10, &threshold);

	if (err) {
		pr_err("%s, conversion %s to number.\n",
			__func__, buf);
		return err;
	}

	err = gp2a_update_threshold(data, is_gp2a030a() ?
			gp2a_original_image_030a : gp2a_original_image,
			threshold, true);

	if (err) {
		pr_err("gp2a threshold(with register) update fail.\n");
		return err;
	}

	return size;
}
static ssize_t proximity_thresh_store(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t size)
{
	unsigned long threshold;
	int err = 0;

	err = strict_strtoul(buf, 10, &threshold);

	if (err) {
		pr_err("%s, conversion %s to number.\n", __func__, buf);
		return err;
	}

        printk(KERN_INFO "[GP2A] %s : threshold=%ld\n",__func__, threshold);

	err = gp2a_update_threshold(threshold, true);

	if (err) {
		pr_err("gp2a threshold(with register) update fail.\n");
		return err;
	}

	return size;
}
예제 #5
0
static int proximity_store_calibration(struct device *dev, bool do_calib)
{
	struct gp2a_data *gp2a = dev_get_drvdata(dev);
	struct file *cancel_filp = NULL;
	mm_segment_t old_fs;
	int err = 0;

	if (do_calib) {
		gp2a->cal_data = proximity_adc_read(gp2a);
		if (is_gp2a030a())
			gp2a_original_image_030a[3][1] += gp2a->cal_data;
		else
			gp2a_original_image[3][1] += gp2a->cal_data;
	} else { /* reset */
		gp2a->cal_data = 0;
		if (is_gp2a030a())
			gp2a_original_image_030a[3][1] =
				gp2a->default_threshold;
		else
			gp2a_original_image[3][1] = gp2a->default_threshold;
	}

	/* Update cal data. */
	gp2a_update_threshold(gp2a, is_gp2a030a() ?
			gp2a_original_image_030a : gp2a_original_image,
			(is_gp2a030a() ?
			gp2a_original_image_030a[3][1] :
			gp2a_original_image[3][1]), true);

	pr_info("%s: prox_cal = %d, prox_thresh = 0x%x\n",
		__func__, gp2a->cal_data, (is_gp2a030a() ?
			gp2a_original_image_030a[3][1] :
			gp2a_original_image[3][1]));

	old_fs = get_fs();
	set_fs(KERNEL_DS);

	cancel_filp = filp_open(CALIBRATION_FILE_PATH,
			O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 0666);
	if (IS_ERR(cancel_filp)) {
		pr_err("%s: Can't open cancelation file\n", __func__);
		set_fs(old_fs);
		err = PTR_ERR(cancel_filp);
		return err;
	}

	err = cancel_filp->f_op->write(cancel_filp,
		(char *)&gp2a->cal_data, sizeof(u8), &cancel_filp->f_pos);
	if (err != sizeof(u8)) {
		pr_err("%s: Can't write the cancel data to file\n", __func__);
		err = -EIO;
	}

	filp_close(cancel_filp, current->files);
	set_fs(old_fs);

	return err;
}
static int gp2a_opt_probe(struct platform_device *pdev)
{
	struct gp2a_data *gp2a;
	struct gp2a_platform_data *pdata = pdev->dev.platform_data;
	u8 value = 0;
	int err = 0;

	gprintk("probe start!\n");

	if (!pdata) {
		pr_err("%s: missing pdata!\n", __func__);
		return err;
	}

	if (!pdata->gp2a_led_on) {
		pr_err("%s: incomplete pdata!\n", __func__);
		return err;
	}
	/* gp2a power on */
	pdata->gp2a_led_on(true);

	if (pdata->gp2a_get_threshold) {
		gp2a_update_threshold(is_gp2a030a() ?
			gp2a_original_image_030a : gp2a_original_image,
			pdata->gp2a_get_threshold(), false);
	}

	/* allocate driver_data */
	gp2a = kzalloc(sizeof(struct gp2a_data), GFP_KERNEL);
	if (!gp2a) {
		pr_err("kzalloc error\n");
		return -ENOMEM;
	}

	proximity_enable = 0;
	proximity_sensor_detection = 0;
	proximity_avg_on = 0;
	gp2a->enabled = 0;
	gp2a->pdata = pdata;

	/* prox_timer settings. we poll for prox_avg values using a timer. */
	hrtimer_init(&gp2a->prox_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	gp2a->prox_poll_delay = ns_to_ktime(2000 * NSEC_PER_MSEC);
	gp2a->prox_timer.function = gp2a_prox_timer_func;

	gp2a->prox_wq = create_singlethread_workqueue("gp2a_prox_wq");
	if (!gp2a->prox_wq) {
		err = -ENOMEM;
		pr_err("%s: could not create prox workqueue\n", __func__);
		goto err_create_prox_workqueue;
	}

	INIT_WORK(&gp2a->work_prox, gp2a_work_func_prox_avg);
	INIT_WORK(&gp2a->work, gp2a_work_func_prox);

	err = proximity_input_init(gp2a);
	if (err < 0)
		goto error_setup_reg;

	err = sysfs_create_group(&gp2a->input_dev->dev.kobj,
				 &proximity_attribute_group);
	if (err < 0)
		goto err_sysfs_create_group_proximity;

	/* set platdata */
	platform_set_drvdata(pdev, gp2a);

	/* wake lock init */
	wake_lock_init(&gp2a->prx_wake_lock, WAKE_LOCK_SUSPEND,
		       "prx_wake_lock");

	/* init i2c */
	opt_i2c_init();

	if (opt_i2c_client == NULL) {
		pr_err("opt_probe failed : i2c_client is NULL\n");
		goto err_no_device;
	} else
		printk(KERN_INFO "opt_i2c_client : (0x%p), address = %x\n",
		       opt_i2c_client, opt_i2c_client->addr);

	/* GP2A Regs INIT SETTINGS  and Check I2C communication */
	value = 0x00;
	/* shutdown mode op[3]=0 */
	err = opt_i2c_write((u8) (COMMAND1), &value);

	if (err < 0) {
		pr_err("%s failed : threre is no such device.\n", __func__);
		goto err_no_device;
	}

	/* Setup irq */
	err = gp2a_setup_irq(gp2a);
	if (err) {
		pr_err("%s: could not setup irq\n", __func__);
		goto err_setup_irq;
	}

	/* set sysfs for proximity sensor */
	gp2a->proximity_dev = sensors_classdev_register("proximity_sensor");
	if (IS_ERR(gp2a->proximity_dev)) {
		pr_err("%s: could not create proximity_dev\n", __func__);
		goto err_proximity_device_create;
	}

	if (device_create_file(gp2a->proximity_dev, &dev_attr_state) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_state.attr.name);
		goto err_proximity_device_create_file1;
	}

	if (device_create_file(gp2a->proximity_dev, &dev_attr_prox_avg) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_prox_avg.attr.name);
		goto err_proximity_device_create_file2;
	}

	if (device_create_file(gp2a->proximity_dev,
						&dev_attr_prox_thresh) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_prox_thresh.attr.name);
		goto err_proximity_device_create_file3;
	}

	if (device_create_file(gp2a->proximity_dev,
						&dev_attr_vendor) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_vendor.attr.name);
		goto err_proximity_device_create_file4;
	}

	if (device_create_file(gp2a->proximity_dev,
						&dev_attr_name) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_name.attr.name);
		goto err_proximity_device_create_file5;
	}

	if (device_create_file(gp2a->proximity_dev, &dev_attr_raw_data) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_raw_data.attr.name);
		goto err_proximity_device_create_file6;
	}

#ifdef CONFIG_SLP
	device_init_wakeup(gp2a->proximity_dev, true);
#endif
	dev_set_drvdata(gp2a->proximity_dev, gp2a);

	device_init_wakeup(&pdev->dev, 1);

	gprintk("probe success!\n");

	return 0;

err_proximity_device_create_file6:
	device_remove_file(gp2a->proximity_dev, &dev_attr_raw_data);
err_proximity_device_create_file5:
	device_remove_file(gp2a->proximity_dev, &dev_attr_name);
err_proximity_device_create_file4:
	device_remove_file(gp2a->proximity_dev, &dev_attr_vendor);
err_proximity_device_create_file3:
	device_remove_file(gp2a->proximity_dev, &dev_attr_prox_avg);
err_proximity_device_create_file2:
	device_remove_file(gp2a->proximity_dev, &dev_attr_state);
err_proximity_device_create_file1:
	sensors_classdev_unregister(gp2a->proximity_dev);
err_proximity_device_create:
	gpio_free(pdata->p_out);
err_setup_irq:
err_no_device:
	sysfs_remove_group(&gp2a->input_dev->dev.kobj,
			   &proximity_attribute_group);
	wake_lock_destroy(&gp2a->prx_wake_lock);
err_sysfs_create_group_proximity:
	input_unregister_device(gp2a->input_dev);
error_setup_reg:
	destroy_workqueue(gp2a->prox_wq);
err_create_prox_workqueue:
	kfree(gp2a);
	return err;
}
static int gp2a_opt_probe(struct platform_device *pdev)
{
	struct gp2a_data *gp2a;
	struct gp2a_platform_data *pdata = pdev->dev.platform_data;
	u8 value = 0;
	int err = 0;

	printk(KERN_INFO"[GP2A] %s : probe start!\n", __func__);

	if (!pdata) {
		pr_err("%s: missing pdata!\n", __func__);
		return err;
	}

        /*PROXY_EN*/
	if (gpio_request(pdata ->power_gpio, "PROXY_EN")) {
		printk(KERN_ERR "Request GPIO_%d failed!\n", pdata ->power_gpio);
	}

	if (pdata->gp2a_led_on)
		pdata->gp2a_led_on(1);    
		
	if (pdata->gp2a_get_threshold) {
		gp2a_update_threshold(pdata->gp2a_get_threshold(), false);
	}
	
	/* allocate driver_data */
	gp2a = kzalloc(sizeof(struct gp2a_data), GFP_KERNEL);
	if (!gp2a) {
		pr_err("kzalloc error\n");
		return -ENOMEM;
	}

	gp2a->proximity_enabled = 0;
	gp2a->pdata = pdata;

	gp2a->light_enabled = 0;
	gp2a->light_delay = SENSOR_DEFAULT_DELAY;
	gp2a->testmode = 0;
	gp2a->light_level_state = 0;

	if (pdata->power_on) {
		pdata->power_on(1);
        	msleep(5);
	}

	INIT_DELAYED_WORK(&gp2a->light_work, gp2a_work_func_light);
	INIT_WORK(&gp2a->proximity_work, gp2a_work_func_prox);

	/*misc device registration*/
        err = misc_register(&gp2a_opt_misc_device);
	if( err < 0 )
		goto error_setup_reg_misc;

	err = proximity_input_init(gp2a);
	if (err < 0)
		goto error_setup_reg_prox;

	err = light_input_init(gp2a);
	if (err < 0)
		goto error_setup_reg_light;

	err = sysfs_create_group(&gp2a->proximity_input_dev->dev.kobj,
				 &proximity_attribute_group);
	if (err < 0)
		goto err_sysfs_create_group_proximity;

	err = sysfs_create_group(&gp2a->light_input_dev->dev.kobj,
				&lightsensor_attribute_group);
	if (err)
		goto err_sysfs_create_group_light;

	mutex_init(&gp2a->light_mutex);
	mutex_init(&gp2a->data_mutex);

	/* set platdata */
	platform_set_drvdata(pdev, gp2a);

	/* wake lock init */
	wake_lock_init(&gp2a->prx_wake_lock, WAKE_LOCK_SUSPEND,
		"prx_wake_lock");

	/* init i2c */
	err = opt_i2c_init();
	if(err < 0)
	{
		pr_err("opt_probe failed : i2c_client is NULL\n"); 
		goto err_no_device;        
	}
	else
		printk(KERN_INFO "[GP2A] opt_i2c_client : (0x%p)\n",opt_i2c_client);
	
	/* GP2A Regs INIT SETTINGS  and Check I2C communication */

	value = 0x00;
	err = opt_i2c_write((u8) (COMMAND1), &value);	/* shutdown mode op[3]=0 */
	if (err < 0) {
		pr_err("%s failed : threre is no such device.\n", __func__);
		goto err_no_device;
	}

	/* Setup irq */
	err = gp2a_setup_irq(gp2a);
	if (err) {
		pr_err("%s: could not setup irq\n", __func__);
		goto err_setup_irq;
	}

	/* set sysfs for proximity sensor */
	gp2a->proximity_dev = device_create(sensors_class,
					    NULL, 0, NULL, "proximity_sensor");
	if (IS_ERR(gp2a->proximity_dev)) {
		pr_err("%s: could not create proximity_dev\n", __func__);
		goto err_proximity_device_create;
	}

	gp2a->light_dev = device_create(sensors_class,
					NULL, 0, NULL, "light_sensor");
	if (IS_ERR(gp2a->light_dev)) {
		pr_err("%s: could not create light_dev\n", __func__);
		goto err_light_device_create;
	}


	if (device_create_file(gp2a->proximity_dev,
		&dev_attr_state) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_state.attr.name);
		goto err_proximity_device_create_file1;
	}

	if (device_create_file(gp2a->proximity_dev,
		&dev_attr_prox_avg) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_prox_avg.attr.name);
		goto err_proximity_device_create_file2;
	}

	if (device_create_file(gp2a->proximity_dev,
		&dev_attr_proximity_enable) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_proximity_enable.attr.name);
		goto err_proximity_device_create_file3;
	}

	if (device_create_file(gp2a->proximity_dev,
		&dev_attr_vendor) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_vendor.attr.name);
		goto err_proximity_device_create_file4;
	}

	if (device_create_file(gp2a->proximity_dev,
		&dev_attr_name) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_name.attr.name);
		goto err_proximity_device_create_file5;
	}

	if (device_create_file(gp2a->proximity_dev,
		&dev_attr_prox_cal) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
			dev_attr_prox_cal.attr.name);
		goto err_proximity_device_create_file6;
	}

	if (device_create_file(gp2a->proximity_dev,
		&dev_attr_prox_offset_pass) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
			dev_attr_prox_offset_pass.attr.name);
		goto err_proximity_device_create_file7;
	}

	if (device_create_file(gp2a->proximity_dev,
		&dev_attr_prox_thresh) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
			dev_attr_prox_thresh.attr.name);
		goto err_proximity_device_create_file8;
	}
   
	if (device_create_file(gp2a->light_dev,
		&dev_attr_lux) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_lux.attr.name);
		goto err_light_device_create_file1;
	}

	if (device_create_file(gp2a->light_dev,
		&dev_attr_light_enable) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_light_enable.attr.name);
		goto err_light_device_create_file2;
	}

	if (device_create_file(gp2a->light_dev,
		&dev_attr_vendor) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_vendor.attr.name);
		goto err_light_device_create_file3;
	}

	if (device_create_file(gp2a->light_dev,
		&dev_attr_name) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_name.attr.name);
		goto err_light_device_create_file4;
	}

	if (device_create_file(gp2a->light_dev,
		&dev_attr_raw_data) < 0) {
		pr_err("%s: could not create device file(%s)!\n", __func__,
		       dev_attr_raw_data.attr.name);
		goto err_light_device_create_file5;
	}

	dev_set_drvdata(gp2a->proximity_dev, gp2a);
	dev_set_drvdata(gp2a->light_dev, gp2a);

	device_init_wakeup(&pdev->dev, 1);

	if (pdata->gp2a_led_on) {       
            pdata->gp2a_led_on(0);   
            printk(KERN_INFO "[GP2A] gpio_get_value of GPIO(%d) is %d\n",pdata ->power_gpio, 
                gpio_get_value(pdata ->power_gpio));
	}

	/* set initial proximity value as 1 */
	input_report_abs(gp2a->proximity_input_dev, ABS_DISTANCE, 1);
	input_sync(gp2a->proximity_input_dev);

        gp2a_opt_data = gp2a;
        
	printk(KERN_INFO"[GP2A] %s : probe success!\n", __func__);

	return 0;

err_light_device_create_file5:
	device_remove_file(gp2a->light_dev, &dev_attr_name);
err_light_device_create_file4:
	device_remove_file(gp2a->light_dev, &dev_attr_vendor);
err_light_device_create_file3:
	device_remove_file(gp2a->light_dev, &dev_attr_light_enable);
err_light_device_create_file2:
	device_remove_file(gp2a->light_dev, &dev_attr_lux);
err_light_device_create_file1:
	device_remove_file(gp2a->proximity_dev, &dev_attr_prox_thresh);
err_proximity_device_create_file8:
	device_remove_file(gp2a->proximity_dev, &dev_attr_prox_offset_pass);
err_proximity_device_create_file7:
	device_remove_file(gp2a->proximity_dev, &dev_attr_prox_cal);
err_proximity_device_create_file6:
	device_remove_file(gp2a->proximity_dev, &dev_attr_name);
err_proximity_device_create_file5:
	device_remove_file(gp2a->proximity_dev, &dev_attr_vendor);
err_proximity_device_create_file4:
	device_remove_file(gp2a->proximity_dev, &dev_attr_proximity_enable);
err_proximity_device_create_file3:
	device_remove_file(gp2a->proximity_dev, &dev_attr_prox_avg);
err_proximity_device_create_file2:
	device_remove_file(gp2a->proximity_dev, &dev_attr_state);
err_proximity_device_create_file1:
err_light_device_create:
	device_destroy(sensors_class, 0);
err_proximity_device_create:
	gpio_free(pdata->p_out);
err_setup_irq:
err_no_device:
	wake_lock_destroy(&gp2a->prx_wake_lock);
	mutex_destroy(&gp2a->light_mutex);
	mutex_destroy(&gp2a->data_mutex);
	sysfs_remove_group(&gp2a->light_input_dev->dev.kobj,
			   &lightsensor_attribute_group);
err_sysfs_create_group_light:
	sysfs_remove_group(&gp2a->proximity_input_dev->dev.kobj,
			   &proximity_attribute_group);
err_sysfs_create_group_proximity:
	input_unregister_device(gp2a->light_input_dev);
error_setup_reg_light:
	input_unregister_device(gp2a->proximity_input_dev);
error_setup_reg_prox:
	misc_deregister(&gp2a_opt_misc_device);    
error_setup_reg_misc:
	if (pdata->power_on)
		pdata->power_on(0);
	kfree(gp2a);
	return err;
}