aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnantha Narayanan <anantha.narayanan@intel.com>2010-12-08 10:57:31 +0000
committerAlan Cox <alan@linux.intel.com>2010-12-08 10:57:31 +0000
commit479f670c7b4797b0842487cf29e2b75b14bb2a28 (patch)
tree70df9261f09f79ce5a883ff9162ea67741d97cee
parenta3461f86d87e73048f6fefbaae7245b5ab53894b (diff)
downloadmrst-s0i3-test-479f670c7b4797b0842487cf29e2b75b14bb2a28.tar.gz
mrst-s0i3-test-479f670c7b4797b0842487cf29e2b75b14bb2a28.tar.xz
mrst-s0i3-test-479f670c7b4797b0842487cf29e2b75b14bb2a28.zip
medfield: add proximity sensor driver for Medfield
Device is apds9802. Signed-off-by: Anantha Narayanan <anantha.narayanan@intel.com> Signed-off-by: Hong Liu <hong.liu@intel.com>
-rw-r--r--drivers/staging/mfld-sensors/Kconfig7
-rw-r--r--drivers/staging/mfld-sensors/Makefile1
-rw-r--r--drivers/staging/mfld-sensors/apds9802ps.c220
3 files changed, 228 insertions, 0 deletions
diff --git a/drivers/staging/mfld-sensors/Kconfig b/drivers/staging/mfld-sensors/Kconfig
index 63ff93db3a7..2447f326f70 100644
--- a/drivers/staging/mfld-sensors/Kconfig
+++ b/drivers/staging/mfld-sensors/Kconfig
@@ -14,4 +14,11 @@ config SENSORS_AK8974COMPASS
depends on I2C
help
To get Compass Sensor output from AK8974 sensor.
+
+config SENSORS_APDS9802PSPROXIMITY
+ tristate "Medfield Avago APDS9802 Proximity Sensor Module"
+ depends on I2C
+ help
+ To get Proximity Sensor output from APDS9802
+ Proximity sensor.
endif #MFLD_SENSORS
diff --git a/drivers/staging/mfld-sensors/Makefile b/drivers/staging/mfld-sensors/Makefile
index 10ec1e2d561..987379ccc36 100644
--- a/drivers/staging/mfld-sensors/Makefile
+++ b/drivers/staging/mfld-sensors/Makefile
@@ -3,3 +3,4 @@
# Medfield sensor drivers
#
obj-$(CONFIG_SENSORS_AK8974COMPASS) += ak8974.o
+obj-$(CONFIG_SENSORS_APDS9802PSPROXIMITY) += apds9802ps.o
diff --git a/drivers/staging/mfld-sensors/apds9802ps.c b/drivers/staging/mfld-sensors/apds9802ps.c
new file mode 100644
index 00000000000..6a472cf95e3
--- /dev/null
+++ b/drivers/staging/mfld-sensors/apds9802ps.c
@@ -0,0 +1,220 @@
+/*
+ * apds9802ps.c - Avago Proximity Sensor Driver
+ *
+ * Copyright (C) 2009 Intel Corp
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/hwmon.h>
+#include <linux/err.h>
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/sysfs.h>
+#include <linux/hwmon-sysfs.h>
+
+MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
+MODULE_DESCRIPTION("Avago apds9802ps Proximity Driver");
+MODULE_LICENSE("GPL v2");
+
+#define POWER_STA_ENABLE 1
+#define POWER_STA_DISABLE 0
+
+#define DRIVER_NAME "i2c_apds9802ps"
+
+struct ps_data{
+ struct device *hwmon_dev;
+ bool needresume;
+ struct mutex write_lock;
+};
+static ssize_t ps_proximity_output_data_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ unsigned int ret_val = 0;
+ unsigned int tempv1, tempv2;
+
+ tempv1 = i2c_smbus_read_byte_data(client, 0x85); /* LSB data */
+ if (tempv1 < 0)
+ printk(KERN_WARNING " apds9802ps: failed proximity out read LSB\n");
+ tempv2 = i2c_smbus_read_byte_data(client, 0x86); /* MSB data */
+ if (tempv2 < 0)
+ printk(KERN_WARNING " apds9802ps: failed proximity out read MSB\n");
+ ret_val = (tempv2 << 8 | tempv1);
+ return sprintf(buf, "%d\n", ret_val);
+}
+static ssize_t ps_power_status_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ int ret_val;
+
+ ret_val = i2c_smbus_read_byte_data(client, 0x80);
+ if (ret_val < 0)
+ printk(KERN_WARNING " apds9802ps: failed power status read MSB\n");
+ ret_val = ret_val & 0x01;
+ return sprintf(buf, "%d\n", ret_val);
+}
+static void ps_set_power_state(struct i2c_client *client, bool on_off)
+{
+ char curr_val = 0;
+ struct ps_data *data = i2c_get_clientdata(client);
+
+ mutex_lock(&data->write_lock);
+ curr_val = i2c_smbus_read_byte_data(client, 0x80);
+ if (on_off)
+ curr_val = curr_val | 0x01;
+ else
+ curr_val = curr_val & 0xFE;
+ if (i2c_smbus_write_byte_data(client, 0x80, curr_val) < 0)
+ printk(KERN_WARNING " apds9802ps: failed power state write\n");
+ mutex_unlock(&data->write_lock);
+}
+static ssize_t ps_power_status_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t count)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct ps_data *data = i2c_get_clientdata(client);
+ unsigned long val = 0;
+
+ if (strict_strtoul(buf, 10, &val))
+ return -EINVAL;
+
+ if (val == POWER_STA_ENABLE) {
+ ps_set_power_state(client, true);
+ data->needresume = true;
+ } else if (val == POWER_STA_DISABLE) {
+ ps_set_power_state(client, false);
+ data->needresume = false;
+ } else
+ return -EINVAL;
+ return count;
+}
+static DEVICE_ATTR(proximity_output, S_IRUGO, ps_proximity_output_data_show, NULL);
+static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
+ ps_power_status_show, ps_power_status_store);
+
+static struct attribute *mid_att_ps[] = {
+ &dev_attr_proximity_output.attr,
+ &dev_attr_power_state.attr,
+ NULL
+};
+
+static struct attribute_group m_ps_gr = {
+ .name = "apds9802ps",
+ .attrs = mid_att_ps
+};
+static void ps_set_default_config(struct i2c_client *client)
+{
+ /*Power ON */
+ if (i2c_smbus_write_byte_data(client, 0x80, 0x01) < 0)
+ printk(KERN_WARNING " apds9802ps: failed default power on write\n");
+ /* 20 pulses, 100Khz Pulse frequency*/
+ if (i2c_smbus_write_byte_data(client, 0x81, 0x86) < 0)
+ printk(KERN_WARNING " apds9802ps: failed pulse frequency write\n");
+ /*Enable measurement, 100MA LED current*/
+ if (i2c_smbus_write_byte_data(client, 0x82, 0x28) < 0)
+ printk(KERN_WARNING " apds9802ps: failed enable measurement write\n");
+}
+static int apds9802ps_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ int res;
+ struct ps_data *data;
+
+ data = kzalloc(sizeof(struct ps_data), GFP_KERNEL);
+ if (data == NULL) {
+ printk(KERN_WARNING " apds9802ps: Memory initialization failed");
+ return -ENOMEM;
+ }
+ i2c_set_clientdata(client, data);
+
+ res = sysfs_create_group(&client->dev.kobj, &m_ps_gr);
+ if (res) {
+ printk(KERN_WARNING "apds9802ps: device create file failed!!\n");
+ goto ps_error1;
+ }
+ dev_info(&client->dev, "%s apds9802ps:chipfound\n", client->name);
+ ps_set_default_config(client);
+ data->needresume = true;
+ mutex_init(&data->write_lock);
+ return res;
+
+ps_error1:
+ i2c_set_clientdata(client, NULL);
+ kfree(data);
+ return res;
+}
+
+static int apds9802ps_remove(struct i2c_client *client)
+{
+ struct ps_data *data = i2c_get_clientdata(client);
+
+ sysfs_remove_group(&client->dev.kobj, &m_ps_gr);
+ kfree(data);
+ return 0;
+}
+static int apds9802ps_suspend(struct i2c_client *client, pm_message_t mesg)
+{
+ ps_set_power_state(client, false);
+ return 0;
+}
+static int apds9802ps_resume(struct i2c_client *client)
+{
+ struct ps_data *data = i2c_get_clientdata(client);
+ if (data->needresume == true)
+ ps_set_power_state(client, true);
+ return 0;
+}
+static struct i2c_device_id apds9802ps_id[] = {
+ { DRIVER_NAME, 0 },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, apds9802ps_id);
+
+static struct i2c_driver apds9802ps_driver = {
+ .driver = {
+ .name = DRIVER_NAME,
+ .owner = THIS_MODULE,
+ .bus = &i2c_bus_type,
+ },
+ .probe = apds9802ps_probe,
+ .remove = apds9802ps_remove,
+ .suspend = apds9802ps_suspend,
+ .resume = apds9802ps_resume,
+ .id_table = apds9802ps_id,
+};
+
+static int __init sensor_apds9802ps_init(void)
+{
+ int res;
+ res = i2c_add_driver(&apds9802ps_driver);
+ return res;
+}
+
+static void __exit sensor_apds9802ps_exit(void)
+{
+ i2c_del_driver(&apds9802ps_driver);
+}
+
+module_init(sensor_apds9802ps_init);
+module_exit(sensor_apds9802ps_exit);