hwmon: add GP2A002 proximity/ambient sensor
Subsystems (3)
PATCH 1/1 hwmon: add GP2A002 proximity/ambient sensor
The GP2A002 is a I2C interfaced Sensor for Proximity/Ambient This patch adds support the GP2A002 sensor
--- drivers/hwmon/Kconfig | 7 + drivers/hwmon/Makefile | 1 + drivers/hwmon/gp2ap002.c | 471 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/gp2ap002.h | 21 ++ 4 files changed, 500 insertions(+), 0 deletions(-) create mode 100644 drivers/hwmon/gp2ap002.c create mode 100644 include/linux/gp2ap002.h
drivers/hwmon/Kconfig +7 -0
@@ -1017,6 +1017,13 @@ config SENSORS_APPLESMC Say Y here if you have an applicable laptop and want to experience the awesome power of applesmc. +config SENSORS_GP2AP002 + tristate "GP2AP002 series Proximity/Ambient Sensor" + depends on HWMON && I2C + help + This driver provides support for Proximity/Ambient Sensor. + + config HWMON_DEBUG_CHIP bool "Hardware Monitoring Chip debugging messages" default n
drivers/hwmon/Makefile +1 -0
@@ -46,6 +46,7 @@ obj-$(CONFIG_SENSORS_FSCPOS) += fscpos.o obj-$(CONFIG_SENSORS_G760A) += g760a.o obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o obj-$(CONFIG_SENSORS_GL520SM) += gl520sm.o +obj-$(CONFIG_SENSORS_GP2AP002) += gp2ap002.o obj-$(CONFIG_SENSORS_ULTRA45) += ultra45_env.o obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o obj-$(CONFIG_SENSORS_I5K_AMB) += i5k_amb.o
drivers/hwmon/gp2ap002.c new file +471 -0
471 lines not on this page · raw message
include/linux/gp2ap002.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* Copyright (C) 2009 Samsung Electronics +* Minkyu Kang <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __GP2AP002_H_ +#define __GP2AP002_H_ + +struct gp2ap002_platform_data { +int (*get_adc)(void ); +void (*power_enable)(int ); +int vo_gpio; +int prox_mode; +int enable; +}; + +#endif
Signature · 1 line
1.5.4.3
Quoted 32 lines · Minkyu Kang
The GP2A002 is a I2C interfaced Sensor for Proximity/Ambient This patch adds support the GP2A002 sensor
Signed-off-by Minkyu Kang <[email protected]>Signed-off-by Suchang Woo <[email protected]>--- drivers/hwmon/Kconfig | 7 + drivers/hwmon/Makefile | 1 + drivers/hwmon/gp2ap002.c | 471 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/gp2ap002.h | 21 ++ 4 files changed, 500 insertions(+), 0 deletions(-) create mode 100644 drivers/hwmon/gp2ap002.c create mode 100644 include/linux/gp2ap002.hinclude/linux/gp2ap002.h new file +11
-0@@ -0,0 +1,21 @@ +/* +* Copyright (C) 2009 Samsung Electronics +* Minkyu Kang <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __GP2AP002_H_ +#define __GP2AP002_H_
include/linux/gp2ap002.h line 21
Quoted 10 lines
include/linux/gp2ap002.h new file +10
-0+ +struct gp2ap002_platform_data { +int (*get_adc)(void ); +void (*power_enable)(int ); +int vo_gpio; +int prox_mode; +int enable; +}; + +#endif
I am curious what is the use of include/linux/gp2ap002.h, it only have gp2ap002_platform_data structure which you can keep in your source file the only file which is using it.
Thanks,
--
JSR
Quoted 19 lines · Minkyu Kang
The GP2A002 is a I2C interfaced Sensor for Proximity/Ambient This patch adds support the GP2A002 sensor
Signed-off-by Minkyu Kang <[email protected]>Signed-off-by Suchang Woo <[email protected]>--- drivers/hwmon/Kconfig | 7 + drivers/hwmon/Makefile | 1 + drivers/hwmon/gp2ap002.c | 471 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/gp2ap002.h | 21 ++ 4 files changed, 500 insertions(+), 0 deletions(-) create mode 100644 drivers/hwmon/gp2ap002.c create mode 100644 include/linux/gp2ap002.hdrivers/hwmon/Kconfig +0
-0
drivers/hwmon/Kconfig config SENSORS_APPLESMC · line 1025
Quoted 9 lines
drivers/hwmon/Kconfig +6
-0@@ -1017,6 +1017,13 @@ config SENSORS_APPLESMC Say Y here if you have an applicable laptop and want to experience the awesome power of applesmc. +config SENSORS_GP2AP002 + tristate "GP2AP002 series Proximity/Ambient Sensor" + depends on HWMON && I2C + help + This driver provides support for Proximity/Ambient Sensor. +
This doesn't have anything to do with hardware monitoring, so this doesn't belong to drivers/hwmon.
Signature · 1 line
Jean Delvare
Quoted 3 lines · Jaswinder Singh Rajput
I am curious what is the use of
include/linux/gp2ap002.h, it only havegp2ap002_platform_datastructure which you can keep in your source file the only file which is using it.
Platform data is data which is passed in from the board-specific code for the system (which will live under arch) to configure the driver.
Hi Minkyu,
Adding linux-input ML, as driver is using linux input subsystem.
Quoted 302 lines · Minkyu Kang
The GP2A002 is a I2C interfaced Sensor for Proximity/Ambient This patch adds support the GP2A002 sensor
Signed-off-by Minkyu Kang <[email protected]>Signed-off-by Suchang Woo <[email protected]>---
drivers/hwmon/Kconfig | 7 +
drivers/hwmon/Makefile | 1 +
drivers/hwmon/gp2ap002.c | 471 ++++++++++++++++++++++++++++++++++++++++++++++include/linux/gp2ap002.h| 21 ++4 files changed, 500 insertions(+), 0 deletions(-)create mode 100644
drivers/hwmon/gp2ap002.c
create mode 100644include/linux/gp2ap002.hdrivers/hwmon/Kconfig +0
-0@@ -1017,6 +1017,13 @@ config SENSORS_APPLESMCSay Y here if you have an applicable laptop and want to experience the awesome power of applesmc.
drivers/hwmon/Kconfig +7
-0+config SENSORS_GP2AP002 + tristate "GP2AP002 series Proximity/Ambient Sensor" + depends on HWMON && I2C + help + This driver provides support for Proximity/Ambient Sensor. + +config
HWMON_DEBUG_CHIP
bool "Hardware Monitoring Chip debugging messages" default ndrivers/hwmon/Makefile +0
-0@@ -46,6 +46,7 @@ obj-$(CONFIG_SENSORS_FSCPOS) += fscpos.oobj-$(CONFIG_SENSORS_G760A) += g760a.o
obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o
obj-$(CONFIG_SENSORS_GL520SM) += gl520sm.o
+obj-$(CONFIG_SENSORS_GP2AP002) += gp2ap002.o
obj-$(CONFIG_SENSORS_ULTRA45) += ultra45_env.o
obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o
obj-$(CONFIG_SENSORS_I5K_AMB) += i5k_amb.odrivers/hwmon/gp2ap002.c new file +252
-0@@ -0,0 +1,471 @@ +/* +* gp2ap002.c - Proximity/Ambient light sensor +* +* Copyright (C) 2009 Samsung Electronics +* Minkyu Kang <[email protected]> +* Suchang Woo <[email protected]> +* Kyungmin Park <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/interrupt.h> +#include <linux/platform_device.h> +#include <linux/workqueue.h> +#include <linux/mutex.h> +#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/delay.h> +#include <linux/gpio.h> +#include <linux/input.h> +#include <linux/gp2ap002.h> + +#define GP2AP002_PROX 0x00/* Read Only */ +#define GP2AP002_GAIN 0x01 +#define GP2AP002_HYS 0x02 +#define GP2AP002_CYCLE 0x03 +#define GP2AP002_OPMOD 0x04 +#define GP2AP002_CON 0x06 + +#define PROX_VO_NO_DETECT (0 << 0) +#define PROX_VO_DETECT (1 << 0) + +#define GAIN_LED0_SMALL (0 << 3) +#define GAIN_LED0_LARGE (1 << 3) + +#define HYS_HYSD (1 << 7) +#define HYS_HYSC1 (1 << 6) +#define HYS_HYSC0 (1 << 5) +#define HYS_HYSF3 (1 << 3) +#define HYS_HYSF2 (1 << 2) +#define HYS_HYSF1 (1 << 1) +#define HYS_HYSF0 (1 << 0) + +#define OPMOD_SSD_SHUTDOWN (0 << 0) +#define OPMOD_SSD_OPERATING (1 << 0) +#define OPMOD_VCON_NORMAL (0 << 1) +#define OPMOD_VCON_IRQ (1 << 1) + +#define CON_OCON1 (1 << 4) +#define CON_OCON0 (1 << 3) + +#define GP2AP002_MAX_LUX 10 + +static int lux_table[GP2AP002_MAX_LUX ] = { + 1, 165, 288, 497, 869, 1532, 2692, 4692, 8280, 100000, +}; + +struct gp2ap002_chip { + struct i2c_client *client; + struct device *dev; + struct input_dev *idev; + struct work_struct work; + struct mutex lock; + + void (*power_enable)(int onoff); + int (*get_adc)(void); + int vo_gpio; + + /* Proximity */ + int enable; + int mode; + int vo; + /* Ambient Light */ + int adc; + int level; +}; + +static int gp2ap002_write_reg (struct i2c_client *client,int reg,u8 value) +{ + int ret; + + ret = i2c_smbus_write_byte_data(client, reg, value); + + if (ret < 0) + dev_err(&client->dev, "%s: err %d\n", __func__, ret); + + return ret; +} + +static int gp2ap002_read_reg (struct i2c_client *client,int reg) +{ + int ret; + + ret = i2c_smbus_read_byte_data(client, reg); + + if (ret < 0) + dev_err(&client->dev, "%s: err %d\n", __func__, ret); + + return ret; +} + +static void gp2ap002_get_vo (struct gp2ap002_chip *chip) +{ + if (chip->mode == OPMOD_VCON_IRQ) { + chip->vo = gp2ap002_read_reg(chip->client, + GP2AP002_PROX) & PROX_VO_DETECT; + } else { + chip->vo = !gpio_get_value(chip->vo_gpio); + } +} + +static void gp2ap002_get_adc (struct gp2ap002_chip *chip) +{ + if (chip->get_adc) + chip->adc = chip->get_adc(); +} + +static void gp2ap002_get_level (struct gp2ap002_chip *chip) +{ + int i; + + gp2ap002_get_adc(chip); + + for (i = 0; i < GP2AP002_MAX_LUX; i++) { + if (lux_table[i] > chip->adc) { + chip->level = i; + break; + } + } +} + +static void gp2ap002_set_mode (struct gp2ap002_chip *chip,int enable) +{ + if (enable == chip->enable) + return; + + chip->enable = enable; + chip->vo = 0; + + gp2ap002_write_reg(chip->client, GP2AP002_OPMOD, OPMOD_SSD_SHUTDOWN); + + if (enable) { + gp2ap002_write_reg(chip->client, GP2AP002_OPMOD, + OPMOD_SSD_OPERATING | chip->mode); + gp2ap002_get_vo(chip); + } +} + +static void gp2ap002_set_enable (struct gp2ap002_chip *chip,const char *buf) +{ + if (!strncmp(buf, "1", 1)) + gp2ap002_set_mode(chip, 1); + else if (!strncmp(buf, "0", 1)) + gp2ap002_set_mode(chip, 0); +} + +static void gp2ap002_update_data (struct gp2ap002_chip *chip) +{ + gp2ap002_get_vo(chip); + enable_irq(chip->client->irq); +} + +static void gp2ap002_work (struct work_struct *work) +{ + struct gp2ap002_chip *chip = container_of(work, + struct gp2ap002_chip, work); + + mutex_lock(&chip->lock); + + gp2ap002_update_data(chip); + + input_report_abs(chip->idev, ABS_DISTANCE, chip->vo); + input_sync(chip->idev); + + mutex_unlock(&chip->lock); +} + +static irqreturn_t gp2ap002_irq (int irq,void *data) +{ + struct gp2ap002_chip *chip = data; + + if (!work_pending(&chip->work)) { + disable_irq_nosync(irq); + schedule_work(&chip->work); + } else { + dev_err(&chip->client->dev, "work pending\n"); + } + + return IRQ_HANDLED; +} + +#define GP2AP002_OUTPUT (name, field) \ +static ssize_t gp2ap002_show_##name(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct gp2ap002_chip *chip = dev_get_drvdata(dev); \ + gp2ap002_get_##name(chip); \ + return sprintf(buf, "%d\n", chip->field); \ +} \ +static SENSOR_DEVICE_ATTR(name, S_IRUGO, gp2ap002_show_##name, NULL, 0); + +#define GP2AP002_INPUT (name, field) \ +static ssize_t gp2ap002_store_##name(struct device *dev, \ + struct device_attribute *attr, const char *buf, size_t count) \ +{ \ + struct gp2ap002_chip *chip = dev_get_drvdata(dev); \ + if (!count) \ + return -EINVAL; \ + gp2ap002_set_##name(chip, buf); \ + return count; \ +} \ +static ssize_t gp2ap002_show_##name(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct gp2ap002_chip *chip = dev_get_drvdata(dev); \ + return sprintf(buf, "%d\n", chip->field); \ +} \ +static SENSOR_DEVICE_ATTR(name, S_IRUGO | S_IWUSR, \ + gp2ap002_show_##name, gp2ap002_store_##name, 0); + +GP2AP002_OUTPUT (vo, vo); +GP2AP002_INPUT (enable, enable); +GP2AP002_OUTPUT (adc, adc); +GP2AP002_OUTPUT (level, level); + +static struct attribute *proximity_attributes[] = { + &sensor_dev_attr_vo.dev_attr.attr, + &sensor_dev_attr_enable.dev_attr.attr, + NULL +}; + +static struct attribute *ambient_attributes[] = { + &sensor_dev_attr_adc.dev_attr.attr, + &sensor_dev_attr_level.dev_attr.attr, + NULL +}; + +static const struct attribute_group proximity_group = { + .name = "proximity", + .attrs = proximity_attributes, +}; + +static const struct attribute_group ambient_group = { + .name = "ambient", + .attrs = ambient_attributes, +};
drivers/hwmon/gp2ap002.c line 262
Quoted 10 lines
drivers/hwmon/gp2ap002.c new file +10
-0+ +static void gp2ap002_unregister_input_device (struct gp2ap002_chip *chip) +{ + struct i2c_client *client = chip->client; + + if (client->irq > 0) + free_irq(client->irq, chip); + + input_unregister_device(chip->idev); + input_free_device(chip->idev);
Please see comment below.
Quoted 6 lines
drivers/hwmon/gp2ap002.c new file +6
-0+} + +static int gp2ap002_register_input_device (struct gp2ap002_chip *chip) +{ + struct i2c_client *client = chip->client; + struct input_dev *idev;
drivers/hwmon/gp2ap002.c line 278
Quoted 10 lines
drivers/hwmon/gp2ap002.c new file +10
-0+ int ret; + + idev = chip->idev = input_allocate_device(); + if (!idev) { + dev_err(&client->dev, "allocating input device is failed\n"); + ret = -ENOMEM; + goto error_alloc; + } + + idev->name = "GP2AP002 OpticalSensor";
maybe idev->phys also good to add.
Quoted 27 lines
drivers/hwmon/gp2ap002.c new file +27
-0+ idev->id.bustype = BUS_I2C; + idev->dev.parent = &client->dev; + idev->evbit[0] = BIT_MASK(EV_ABS); + + input_set_abs_params(idev, ABS_DISTANCE, 0, 1, 0, 0); + + input_set_drvdata(idev, chip); + + ret = input_register_device(idev); + if (ret) { + dev_err(&client->dev, "registering input device is failed\n"); + goto error_reg; + } + + if (client->irq > 0) { + unsigned long irq_flag = IRQF_DISABLED; + + if (chip->mode == OPMOD_VCON_IRQ) + irq_flag |= IRQF_TRIGGER_FALLING; + else + irq_flag |= IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; + + ret = request_irq(client->irq, gp2ap002_irq, irq_flag, + "GP2AP002 OpticalSensor", chip); + if (ret) { + dev_err(&client->dev, "can't get IRQ %d, ret %d\n", + client->irq, ret);
drivers/hwmon/gp2ap002.c line 315
Quoted 10 lines
drivers/hwmon/gp2ap002.c new file +10
-0+ goto error_irq; + } + } + + return 0; + +error_irq: + input_unregister_device(idev); +error_reg: + input_free_device(idev);
Please put idev=NULL after input_unregister_device call, as idev is ref-counted and input_free_device is not required after input_unregister_device.
drivers/hwmon/gp2ap002.c line 323
Quoted 8 lines
drivers/hwmon/gp2ap002.c new file +8
-0+error_alloc: + return ret; +} + + +static int gp2ap002_probe (struct i2c_client *client, + const struct i2c_device_id *id) +{
__devinit.
Quoted 88 lines
drivers/hwmon/gp2ap002.c new file +88
-0+ struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); + struct gp2ap002_chip *chip; + struct gp2ap002_platform_data *pdata; + int ret; + + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) + return -EIO; + + chip = kzalloc(sizeof(struct gp2ap002_chip), GFP_KERNEL); + if (!chip) + return -ENOMEM; + + pdata = client->dev.platform_data; + + chip->client = client; + i2c_set_clientdata(client, chip); + + chip->dev = hwmon_device_register(&client->dev); + if (IS_ERR(chip->dev)) { + dev_err(&client->dev, + "Registering to hwmon device is failed\n"); + ret = PTR_ERR(chip->dev); + goto error_hwmon; + } + + ret = sysfs_create_group(&client->dev.kobj, &proximity_group); + if (ret) { + dev_err(&client->dev, + "Creating proximity attribute group failed\n"); + goto error_sysfs1; + } + + ret = sysfs_create_group(&client->dev.kobj, &ambient_group); + if (ret) { + dev_err(&client->dev, + "Creating light attribute group failed\n"); + goto error_sysfs2; + } + + ret = gp2ap002_register_input_device(chip); + if (ret) { + dev_err(&client->dev, "Registering input device is failed\n"); + goto error_input; + } + + chip->power_enable = pdata->power_enable; + chip->get_adc = pdata->get_adc; + chip->vo_gpio = pdata->vo_gpio; + chip->mode = pdata->prox_mode; + + INIT_WORK(&chip->work, gp2ap002_work); + mutex_init(&chip->lock); + + if (chip->power_enable) + chip->power_enable(0); + + gp2ap002_set_mode(chip, pdata->enable); + + return 0; + +error_input: + sysfs_remove_group(&client->dev.kobj, &ambient_group); +error_sysfs2: + sysfs_remove_group(&client->dev.kobj, &proximity_group); +error_sysfs1: + hwmon_device_unregister(chip->dev); +error_hwmon: + i2c_set_clientdata(client, NULL); + kfree(chip); + return ret; +} + +static int __exit gp2ap002_remove(struct i2c_client *client) +{ + struct gp2ap002_chip *chip = i2c_get_clientdata(client); + + gp2ap002_unregister_input_device(chip); + hwmon_device_unregister(chip->dev); + + sysfs_remove_group(&client->dev.kobj, &proximity_group); + sysfs_remove_group(&client->dev.kobj, &ambient_group); + + i2c_set_clientdata(client, NULL); + + if (chip->power_enable) + chip->power_enable(0); + + kfree(chip);
drivers/hwmon/gp2ap002.c line 421
Quoted 10 lines
drivers/hwmon/gp2ap002.c new file +10
-0+ return 0; +} + +#ifdef CONFIG_PM + +static int gp2ap002_suspend (struct i2c_client *client,pm_message_t mesg) +{ + struct gp2ap002_chip *chip = i2c_get_clientdata(client); + gp2ap002_set_mode(chip, 0); +
cancel_work_sync ?
Quoted 84 lines
drivers/hwmon/gp2ap002.c new file +50
-0+ return 0; +} + +static int gp2ap002_resume (struct i2c_client *client) +{ + struct gp2ap002_chip *chip = i2c_get_clientdata(client); + gp2ap002_set_mode(chip, 1); + + return 0; +} + +#else + +#define gp2ap002_suspend NULL +#define gp2ap002_resume NULL + +#endif /* CONFIG_PM */ + +static const struct i2c_device_id gp2ap002_id[] = { + { "GP2AP002", 0 }, + { } +}; +MODULE_DEVICE_TABLE (i2c, gp2ap002_id); + +static struct i2c_driver gp2ap002_i2c_driver = { + .driver = { + .name = "GP2AP002", + }, + .probe = gp2ap002_probe, + .remove = __exit_p(gp2ap002_remove), + .suspend = gp2ap002_suspend, + .resume = gp2ap002_resume, + .id_table = gp2ap002_id, +}; + +static int __init gp2ap002_init (void ) +{ + return i2c_add_driver(&gp2ap002_i2c_driver); +} +module_init (gp2ap002_init); + +static void __exit gp2ap002_exit(void) +{ + i2c_del_driver(&gp2ap002_i2c_driver); +} +module_exit (gp2ap002_exit); + +MODULE_AUTHOR ("Minkyu Kang <[email protected]>" ); +MODULE_DESCRIPTION ("GP2AP002 Proximity/Ambient Light Sensor driver" ); +MODULE_LICENSE ("GPL" );include/linux/gp2ap002.h new file +21
-1@@ -0,0 +1,21 @@ +/* +* Copyright (C) 2009 Samsung Electronics +* Minkyu Kang <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __GP2AP002_H_ +#define __GP2AP002_H_ + +struct gp2ap002_platform_data { + int (*get_adc)(void); + void (*power_enable)(int); + int vo_gpio; + int prox_mode; + int enable; +}; + +#endif --1.5.4.3
--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 3 lines
---Trilok Soni http://triloksoni.wordpress.com http://www.linkedin.com/in/triloksoni
Hi Trilok,
2009/6/26 Trilok Soni <[email protected]>:
Quoted 19 lines
Hi Minkyu,
Adding linux-input ML, as driver is using linux input subsystem.
Quoted 14 lines · Minkyu Kang
The GP2A002 is a I2C interfaced Sensor for Proximity/Ambient This patch adds support the GP2A002 sensor
Signed-off-by Minkyu Kang <[email protected]>Signed-off-by Suchang Woo <[email protected]>---
drivers/hwmon/Kconfig | 7 +
drivers/hwmon/Makefile | 1 +
drivers/hwmon/gp2ap002.c | 471 ++++++++++++++++++++++++++++++++++++++++++++++include/linux/gp2ap002.h| 21 ++4 files changed, 500 insertions(+), 0 deletions(-)create mode 100644
drivers/hwmon/gp2ap002.c
create mode 100644include/linux/gp2ap002.h
Thanks for review and I'll make new version as you said : )
Signature · 6 lines
from. prom. www.promsoft.net -- To unsubscribe from this list: send the line "unsubscribe linux-input" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Jean,
2009/6/26 Jean Delvare <[email protected]>:
Quoted 23 lines · Minkyu Kang
Quoted 22 lines
The GP2A002 is a I2C interfaced Sensor for Proximity/Ambient This patch adds support the GP2A002 sensor
Signed-off-by Minkyu Kang <[email protected]>Signed-off-by Suchang Woo <[email protected]>---
drivers/hwmon/Kconfig | 7 +
drivers/hwmon/Makefile | 1 +
drivers/hwmon/gp2ap002.c | 471 ++++++++++++++++++++++++++++++++++++++++++++++include/linux/gp2ap002.h| 21 ++4 files changed, 500 insertions(+), 0 deletions(-)create mode 100644
drivers/hwmon/gp2ap002.c
create mode 100644include/linux/gp2ap002.hdrivers/hwmon/Kconfig +0
-0@@ -1017,6 +1017,13 @@ config SENSORS_APPLESMCSay Y here if you have an applicable laptop and want to experience the awesome power of applesmc.
Quoted 10 lines
drivers/hwmon/Kconfig config SENSORS_APPLESMC · line 1025
Quoted 6 lines
drivers/hwmon/Kconfig +6
-0+config SENSORS_GP2AP002 + tristate "GP2AP002 series Proximity/Ambient Sensor" + depends on HWMON && I2C + help + This driver provides support for Proximity/Ambient Sensor. +This doesn't have anything to do with hardware monitoring, so this doesn't belong to
drivers/hwmon.
Can you please explain about belong to hardware monitoring? did i missing something?
many thanks :)
Signature · 2 lines
from. prom. www.promsoft.net
Quoted 5 lines · Minkyu Kang
The GP2A002 is a I2C interfaced Sensor for Proximity/Ambient This patch adds support the GP2A002 sensor
Signed-off-by Minkyu Kang <[email protected]>Signed-off-by Suchang Woo <[email protected]>
It would be nice to document what it does and what interface to userland it provides...
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Quoted 37 lines · Minkyu Kang
Hi Jean,
2009/6/26 Jean Delvare <[email protected]>:
Quoted 23 lines · Minkyu Kang
Quoted 22 lines
The GP2A002 is a I2C interfaced Sensor for Proximity/Ambient This patch adds support the GP2A002 sensor
Signed-off-by Minkyu Kang <[email protected]>Signed-off-by Suchang Woo <[email protected]>---
drivers/hwmon/Kconfig | 7 +
drivers/hwmon/Makefile | 1 +
drivers/hwmon/gp2ap002.c | 471 ++++++++++++++++++++++++++++++++++++++++++++++include/linux/gp2ap002.h| 21 ++4 files changed, 500 insertions(+), 0 deletions(-)create mode 100644
drivers/hwmon/gp2ap002.c
create mode 100644include/linux/gp2ap002.hdrivers/hwmon/Kconfig +0
-0@@ -1017,6 +1017,13 @@ config SENSORS_APPLESMCSay Y here if you have an applicable laptop and want to experience the awesome power of applesmc.
Quoted 10 lines
drivers/hwmon/Kconfig config SENSORS_APPLESMC · line 1025
Quoted 6 lines
drivers/hwmon/Kconfig +6
-0+config SENSORS_GP2AP002 + tristate "GP2AP002 series Proximity/Ambient Sensor" + depends on HWMON && I2C + help + This driver provides support for Proximity/Ambient Sensor. +This doesn't have anything to do with hardware monitoring, so this doesn't belong to
drivers/hwmon.
Quoted 2 lines
Can you please explain about belong to hardware monitoring? did i missing something?
I'm not sure how I can explain it better. Your driver is for a device which is no a hardware monitoring chip, so this driver should not be located in the drivers/hwmon directory.
Signature · 1 line
Jean Delvare
Quoted 203 lines · Trilok Soni
Hi Minkyu,
Adding linux-input ML, as driver is using linux input subsystem.
Quoted 198 lines · Minkyu Kang
The GP2A002 is a I2C interfaced Sensor for Proximity/Ambient This patch adds support the GP2A002 sensor
Signed-off-by Minkyu Kang <[email protected]>Signed-off-by Suchang Woo <[email protected]>---
drivers/hwmon/Kconfig | 7 +
drivers/hwmon/Makefile | 1 +
drivers/hwmon/gp2ap002.c | 471 ++++++++++++++++++++++++++++++++++++++++++++++include/linux/gp2ap002.h| 21 ++4 files changed, 500 insertions(+), 0 deletions(-)create mode 100644
drivers/hwmon/gp2ap002.c
create mode 100644include/linux/gp2ap002.hdrivers/hwmon/Kconfig +0
-0@@ -1017,6 +1017,13 @@ config SENSORS_APPLESMCSay Y here if you have an applicable laptop and want to experience the awesome power of applesmc.
drivers/hwmon/Kconfig +7
-0+config SENSORS_GP2AP002 + tristate "GP2AP002 series Proximity/Ambient Sensor" + depends on HWMON && I2C + help + This driver provides support for Proximity/Ambient Sensor. + +config
HWMON_DEBUG_CHIP
bool "Hardware Monitoring Chip debugging messages" default ndrivers/hwmon/Makefile +0
-0@@ -46,6 +46,7 @@ obj-$(CONFIG_SENSORS_FSCPOS) += fscpos.oobj-$(CONFIG_SENSORS_G760A) += g760a.o
obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o
obj-$(CONFIG_SENSORS_GL520SM) += gl520sm.o
+obj-$(CONFIG_SENSORS_GP2AP002) += gp2ap002.o
obj-$(CONFIG_SENSORS_ULTRA45) += ultra45_env.o
obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o
obj-$(CONFIG_SENSORS_I5K_AMB) += i5k_amb.odrivers/hwmon/gp2ap002.c new file +148
-0@@ -0,0 +1,471 @@ +/* +* gp2ap002.c - Proximity/Ambient light sensor +* +* Copyright (C) 2009 Samsung Electronics +* Minkyu Kang <[email protected]> +* Suchang Woo <[email protected]> +* Kyungmin Park <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/interrupt.h> +#include <linux/platform_device.h> +#include <linux/workqueue.h> +#include <linux/mutex.h> +#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/delay.h> +#include <linux/gpio.h> +#include <linux/input.h> +#include <linux/gp2ap002.h> + +#define GP2AP002_PROX 0x00/* Read Only */ +#define GP2AP002_GAIN 0x01 +#define GP2AP002_HYS 0x02 +#define GP2AP002_CYCLE 0x03 +#define GP2AP002_OPMOD 0x04 +#define GP2AP002_CON 0x06 + +#define PROX_VO_NO_DETECT (0 << 0) +#define PROX_VO_DETECT (1 << 0) + +#define GAIN_LED0_SMALL (0 << 3) +#define GAIN_LED0_LARGE (1 << 3) + +#define HYS_HYSD (1 << 7) +#define HYS_HYSC1 (1 << 6) +#define HYS_HYSC0 (1 << 5) +#define HYS_HYSF3 (1 << 3) +#define HYS_HYSF2 (1 << 2) +#define HYS_HYSF1 (1 << 1) +#define HYS_HYSF0 (1 << 0) + +#define OPMOD_SSD_SHUTDOWN (0 << 0) +#define OPMOD_SSD_OPERATING (1 << 0) +#define OPMOD_VCON_NORMAL (0 << 1) +#define OPMOD_VCON_IRQ (1 << 1) + +#define CON_OCON1 (1 << 4) +#define CON_OCON0 (1 << 3) + +#define GP2AP002_MAX_LUX 10 + +static int lux_table[GP2AP002_MAX_LUX ] = { + 1, 165, 288, 497, 869, 1532, 2692, 4692, 8280, 100000, +}; + +struct gp2ap002_chip { + struct i2c_client *client; + struct device *dev; + struct input_dev *idev; + struct work_struct work; + struct mutex lock; + + void (*power_enable)(int onoff); + int (*get_adc)(void); + int vo_gpio; + + /* Proximity */ + int enable; + int mode; + int vo; + /* Ambient Light */ + int adc; + int level; +}; + +static int gp2ap002_write_reg (struct i2c_client *client,int reg,u8 value) +{ + int ret; + + ret = i2c_smbus_write_byte_data(client, reg, value); + + if (ret < 0) + dev_err(&client->dev, "%s: err %d\n", __func__, ret); + + return ret; +} + +static int gp2ap002_read_reg (struct i2c_client *client,int reg) +{ + int ret; + + ret = i2c_smbus_read_byte_data(client, reg); + + if (ret < 0) + dev_err(&client->dev, "%s: err %d\n", __func__, ret); + + return ret; +} + +static void gp2ap002_get_vo (struct gp2ap002_chip *chip) +{ + if (chip->mode == OPMOD_VCON_IRQ) { + chip->vo = gp2ap002_read_reg(chip->client, + GP2AP002_PROX) & PROX_VO_DETECT; + } else { + chip->vo = !gpio_get_value(chip->vo_gpio); + } +} + +static void gp2ap002_get_adc (struct gp2ap002_chip *chip) +{ + if (chip->get_adc) + chip->adc = chip->get_adc(); +} + +static void gp2ap002_get_level (struct gp2ap002_chip *chip) +{ + int i; + + gp2ap002_get_adc(chip); + + for (i = 0; i < GP2AP002_MAX_LUX; i++) { + if (lux_table[i] > chip->adc) { + chip->level = i; + break; + } + } +} + +static void gp2ap002_set_mode (struct gp2ap002_chip *chip,int enable) +{ + if (enable == chip->enable) + return; + + chip->enable = enable; + chip->vo = 0; + + gp2ap002_write_reg(chip->client, GP2AP002_OPMOD, OPMOD_SSD_SHUTDOWN); + + if (enable) {
Quoted 8 lines
drivers/hwmon/gp2ap002.c line 156
Quoted 8 lines
drivers/hwmon/gp2ap002.c new file +8
-0+ gp2ap002_write_reg(chip->client, GP2AP002_OPMOD, + OPMOD_SSD_OPERATING | chip->mode); + gp2ap002_get_vo(chip); + } +} + +static void gp2ap002_set_enable (struct gp2ap002_chip *chip,const char *buf) +{
simple_strtoul() here please.
Quoted 10 lines
Quoted 10 lines
drivers/hwmon/gp2ap002.c new file +10
-0+ if (!strncmp(buf, "1", 1)) + gp2ap002_set_mode(chip, 1); + else if (!strncmp(buf, "0", 1)) + gp2ap002_set_mode(chip, 0); +} + +static void gp2ap002_update_data (struct gp2ap002_chip *chip) +{ + gp2ap002_get_vo(chip); + enable_irq(chip->client->irq);
Quoted 8 lines
drivers/hwmon/gp2ap002.c line 174
Quoted 8 lines
drivers/hwmon/gp2ap002.c new file +8
-0+} + +static void gp2ap002_work (struct work_struct *work) +{ + struct gp2ap002_chip *chip = container_of(work, + struct gp2ap002_chip, work); + + mutex_lock(&chip->lock);
What exactly do we protect here? I don't see any concurrency...
Quoted 78 lines
Quoted 78 lines
drivers/hwmon/gp2ap002.c new file +78
-0+ + gp2ap002_update_data(chip); + + input_report_abs(chip->idev, ABS_DISTANCE, chip->vo); + input_sync(chip->idev); + + mutex_unlock(&chip->lock); +} + +static irqreturn_t gp2ap002_irq (int irq,void *data) +{ + struct gp2ap002_chip *chip = data; + + if (!work_pending(&chip->work)) { + disable_irq_nosync(irq); + schedule_work(&chip->work); + } else { + dev_err(&chip->client->dev, "work pending\n"); + } + + return IRQ_HANDLED; +} + +#define GP2AP002_OUTPUT (name, field) \ +static ssize_t gp2ap002_show_##name(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct gp2ap002_chip *chip = dev_get_drvdata(dev); \ + gp2ap002_get_##name(chip); \ + return sprintf(buf, "%d\n", chip->field); \ +} \ +static SENSOR_DEVICE_ATTR(name, S_IRUGO, gp2ap002_show_##name, NULL, 0); + +#define GP2AP002_INPUT (name, field) \ +static ssize_t gp2ap002_store_##name(struct device *dev, \ + struct device_attribute *attr, const char *buf, size_t count) \ +{ \ + struct gp2ap002_chip *chip = dev_get_drvdata(dev); \ + if (!count) \ + return -EINVAL; \ + gp2ap002_set_##name(chip, buf); \ + return count; \ +} \ +static ssize_t gp2ap002_show_##name(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct gp2ap002_chip *chip = dev_get_drvdata(dev); \ + return sprintf(buf, "%d\n", chip->field); \ +} \ +static SENSOR_DEVICE_ATTR(name, S_IRUGO | S_IWUSR, \ + gp2ap002_show_##name, gp2ap002_store_##name, 0); + +GP2AP002_OUTPUT (vo, vo); +GP2AP002_INPUT (enable, enable); +GP2AP002_OUTPUT (adc, adc); +GP2AP002_OUTPUT (level, level); + +static struct attribute *proximity_attributes[] = { + &sensor_dev_attr_vo.dev_attr.attr, + &sensor_dev_attr_enable.dev_attr.attr, + NULL +}; + +static struct attribute *ambient_attributes[] = { + &sensor_dev_attr_adc.dev_attr.attr, + &sensor_dev_attr_level.dev_attr.attr, + NULL +}; + +static const struct attribute_group proximity_group = { + .name = "proximity", + .attrs = proximity_attributes, +}; + +static const struct attribute_group ambient_group = { + .name = "ambient", + .attrs = ambient_attributes, +};
Quoted 8 lines
drivers/hwmon/gp2ap002.c line 260
Quoted 8 lines
drivers/hwmon/gp2ap002.c new file +8
-0+ +static void gp2ap002_unregister_input_device (struct gp2ap002_chip *chip) +{ + struct i2c_client *client = chip->client; + + if (client->irq > 0) + free_irq(client->irq, chip); +
I don't see the work being shut off here. That means that it can still try to enable freed irq or even cause oops if kevend is backed up...
Overall this driver could benefit from threaded IRQ facility I think.
Quoted 121 lines
drivers/hwmon/gp2ap002.c line 262
Quoted 2 lines
drivers/hwmon/gp2ap002.c new file +2
-0+ input_unregister_device(chip->idev); + input_free_device(chip->idev);Please see comment below.
Quoted 6 lines
drivers/hwmon/gp2ap002.c new file +6
-0+} + +static int gp2ap002_register_input_device (struct gp2ap002_chip *chip) +{ + struct i2c_client *client = chip->client; + struct input_dev *idev;drivers/hwmon/gp2ap002.c line 278
Quoted 10 lines
drivers/hwmon/gp2ap002.c new file +10
-0+ int ret; + + idev = chip->idev = input_allocate_device(); + if (!idev) { + dev_err(&client->dev, "allocating input device is failed\n"); + ret = -ENOMEM; + goto error_alloc; + } + + idev->name = "GP2AP002 OpticalSensor";maybe
idev->physalso good to add.Quoted 27 lines
drivers/hwmon/gp2ap002.c new file +27
-0+ idev->id.bustype = BUS_I2C; + idev->dev.parent = &client->dev; + idev->evbit[0] = BIT_MASK(EV_ABS); + + input_set_abs_params(idev, ABS_DISTANCE, 0, 1, 0, 0); + + input_set_drvdata(idev, chip); + + ret = input_register_device(idev); + if (ret) { + dev_err(&client->dev, "registering input device is failed\n"); + goto error_reg; + } + + if (client->irq > 0) { + unsigned long irq_flag = IRQF_DISABLED; + + if (chip->mode == OPMOD_VCON_IRQ) + irq_flag |= IRQF_TRIGGER_FALLING; + else + irq_flag |= IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; + + ret = request_irq(client->irq, gp2ap002_irq, irq_flag, + "GP2AP002 OpticalSensor", chip); + if (ret) { + dev_err(&client->dev, "can't get IRQ %d, ret %d\n", + client->irq, ret);drivers/hwmon/gp2ap002.c line 315
Quoted 10 lines
drivers/hwmon/gp2ap002.c new file +10
-0+ goto error_irq; + } + } + + return 0; + +error_irq: + input_unregister_device(idev); +error_reg: + input_free_device(idev);Please put idev=NULL after
input_unregister_devicecall, as idev is ref-counted andinput_free_deviceis not required afterinput_unregister_device.drivers/hwmon/gp2ap002.c line 323
Quoted 8 lines
drivers/hwmon/gp2ap002.c new file +8
-0+error_alloc: + return ret; +} + + +static int gp2ap002_probe (struct i2c_client *client, + const struct i2c_device_id *id) +{
__devinit.Quoted 43 lines
drivers/hwmon/gp2ap002.c new file +43
-0+ struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); + struct gp2ap002_chip *chip; + struct gp2ap002_platform_data *pdata; + int ret; + + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) + return -EIO; + + chip = kzalloc(sizeof(struct gp2ap002_chip), GFP_KERNEL); + if (!chip) + return -ENOMEM; + + pdata = client->dev.platform_data; + + chip->client = client; + i2c_set_clientdata(client, chip); + + chip->dev = hwmon_device_register(&client->dev); + if (IS_ERR(chip->dev)) { + dev_err(&client->dev, + "Registering to hwmon device is failed\n"); + ret = PTR_ERR(chip->dev); + goto error_hwmon; + } + + ret = sysfs_create_group(&client->dev.kobj, &proximity_group); + if (ret) { + dev_err(&client->dev, + "Creating proximity attribute group failed\n"); + goto error_sysfs1; + } + + ret = sysfs_create_group(&client->dev.kobj, &ambient_group); + if (ret) { + dev_err(&client->dev, + "Creating light attribute group failed\n"); + goto error_sysfs2; + } + + ret = gp2ap002_register_input_device(chip); + if (ret) { + dev_err(&client->dev, "Registering input device is failed\n"); + goto error_input;
Quoted 8 lines
drivers/hwmon/gp2ap002.c line 374
Quoted 8 lines
drivers/hwmon/gp2ap002.c new file +8
-0+ } + + chip->power_enable = pdata->power_enable; + chip->get_adc = pdata->get_adc; + chip->vo_gpio = pdata->vo_gpio; + chip->mode = pdata->prox_mode; + + INIT_WORK(&chip->work, gp2ap002_work);
We have already registered IRQ but only initializing work here, this makes me uneasy... Are we certain that chip is disabled and will not raise IRQ here?
Quoted 14 lines
Quoted 14 lines
drivers/hwmon/gp2ap002.c new file +14
-0+ mutex_init(&chip->lock); + + if (chip->power_enable) + chip->power_enable(0); + + gp2ap002_set_mode(chip, pdata->enable); + + return 0; + +error_input: + sysfs_remove_group(&client->dev.kobj, &ambient_group); +error_sysfs2: + sysfs_remove_group(&client->dev.kobj, &proximity_group); +error_sysfs1:
Quoted 8 lines
drivers/hwmon/gp2ap002.c line 396
Quoted 8 lines
drivers/hwmon/gp2ap002.c new file +8
-0+ hwmon_device_unregister(chip->dev); +error_hwmon: + i2c_set_clientdata(client, NULL); + kfree(chip); + return ret; +} + +static int __exit gp2ap002_remove(struct i2c_client *client)
__devexit.
Quoted 50 lines
Quoted 15 lines
drivers/hwmon/gp2ap002.c new file +15
-0+{ + struct gp2ap002_chip *chip = i2c_get_clientdata(client); + + gp2ap002_unregister_input_device(chip); + hwmon_device_unregister(chip->dev); + + sysfs_remove_group(&client->dev.kobj, &proximity_group); + sysfs_remove_group(&client->dev.kobj, &ambient_group); + + i2c_set_clientdata(client, NULL); + + if (chip->power_enable) + chip->power_enable(0); + + kfree(chip);drivers/hwmon/gp2ap002.c line 421
Quoted 10 lines
drivers/hwmon/gp2ap002.c new file +10
-0+ return 0; +} + +#ifdef CONFIG_PM + +static int gp2ap002_suspend (struct i2c_client *client,pm_message_t mesg) +{ + struct gp2ap002_chip *chip = i2c_get_clientdata(client); + gp2ap002_set_mode(chip, 0); +
cancel_work_sync?Quoted 22 lines
drivers/hwmon/gp2ap002.c new file +22
-0+ return 0; +} + +static int gp2ap002_resume (struct i2c_client *client) +{ + struct gp2ap002_chip *chip = i2c_get_clientdata(client); + gp2ap002_set_mode(chip, 1); + + return 0; +} + +#else + +#define gp2ap002_suspend NULL +#define gp2ap002_resume NULL + +#endif /* CONFIG_PM */ + +static const struct i2c_device_id gp2ap002_id[] = { + { "GP2AP002", 0 }, + { } +};
Quoted 8 lines
drivers/hwmon/gp2ap002.c line 451
Quoted 8 lines
drivers/hwmon/gp2ap002.c new file +8
-0+MODULE_DEVICE_TABLE (i2c, gp2ap002_id); + +static struct i2c_driver gp2ap002_i2c_driver = { + .driver = { + .name = "GP2AP002", + }, + .probe = gp2ap002_probe, + .remove = __exit_p(gp2ap002_remove),
__devexit_p()
Quoted 48 lines
Quoted 48 lines
drivers/hwmon/gp2ap002.c new file +20
-0+ .suspend = gp2ap002_suspend, + .resume = gp2ap002_resume, + .id_table = gp2ap002_id, +}; + +static int __init gp2ap002_init (void ) +{ + return i2c_add_driver(&gp2ap002_i2c_driver); +} +module_init (gp2ap002_init); + +static void __exit gp2ap002_exit(void) +{ + i2c_del_driver(&gp2ap002_i2c_driver); +} +module_exit (gp2ap002_exit); + +MODULE_AUTHOR ("Minkyu Kang <[email protected]>" ); +MODULE_DESCRIPTION ("GP2AP002 Proximity/Ambient Light Sensor driver" ); +MODULE_LICENSE ("GPL" );include/linux/gp2ap002.h new file +21
-1@@ -0,0 +1,21 @@ +/* +* Copyright (C) 2009 Samsung Electronics +* Minkyu Kang <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __GP2AP002_H_ +#define __GP2AP002_H_ + +struct gp2ap002_platform_data { + int (*get_adc)(void); + void (*power_enable)(int); + int vo_gpio; + int prox_mode; + int enable; +}; + +#endif --
Signature · 1 line
Dmitry