aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Cox <alan@linux.intel.com>2010-12-08 10:57:31 +0000
committerAlan Cox <alan@linux.intel.com>2010-12-08 10:57:31 +0000
commita3461f86d87e73048f6fefbaae7245b5ab53894b (patch)
tree24a1f98950175474929fdfcb0cae41e0f2931157
parente9ecf6336cda1573ed009715dde17efa4d3af09c (diff)
downloadmrst-s0i3-test-a3461f86d87e73048f6fefbaae7245b5ab53894b.tar.gz
mrst-s0i3-test-a3461f86d87e73048f6fefbaae7245b5ab53894b.tar.xz
mrst-s0i3-test-a3461f86d87e73048f6fefbaae7245b5ab53894b.zip
From@ Anantha Narayanan <anantha.narayanan@intel.com>
medfield: add compass sensor driver for Medfield The device is ak8974, which hooks on i2c bus 5 on Medfield. Signed-off-by: Anantha Narayanan <anantha.narayanan@intel.com> Signed-off-by: Hong Liu <hong.liu@intel.com>
-rw-r--r--drivers/staging/Kconfig2
-rw-r--r--drivers/staging/Makefile1
-rw-r--r--drivers/staging/mfld-sensors/Kconfig17
-rw-r--r--drivers/staging/mfld-sensors/Makefile5
-rw-r--r--drivers/staging/mfld-sensors/ak8974.c234
5 files changed, 259 insertions, 0 deletions
diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
index 0b0210dc1f4..52e81a92b21 100644
--- a/drivers/staging/Kconfig
+++ b/drivers/staging/Kconfig
@@ -177,5 +177,7 @@ source "drivers/staging/intel_sst/Kconfig"
source "drivers/staging/speakup/Kconfig"
+source "drivers/staging/mfld-sensors/Kconfig"
+
endif # !STAGING_EXCLUDE_BUILD
endif # STAGING
diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
index 62df61d1833..a22a7077424 100644
--- a/drivers/staging/Makefile
+++ b/drivers/staging/Makefile
@@ -69,3 +69,4 @@ obj-$(CONFIG_BCM_WIMAX) += bcm/
obj-$(CONFIG_FT1000) += ft1000/
obj-$(CONFIG_SND_INTEL_SST) += intel_sst/
obj-$(CONFIG_SPEAKUP) += speakup/
+obj-$(CONFIG_MFLD_SENSORS) += mfld-sensors/
diff --git a/drivers/staging/mfld-sensors/Kconfig b/drivers/staging/mfld-sensors/Kconfig
new file mode 100644
index 00000000000..63ff93db3a7
--- /dev/null
+++ b/drivers/staging/mfld-sensors/Kconfig
@@ -0,0 +1,17 @@
+menuconfig MFLD_SENSORS
+ bool "Intel Medfield Sensor Device Drivers"
+ default y
+ ---help---
+ Say Y here to get to see options for device drivers for various
+ sensor devices on Medfiled.
+ This option alone does not add any kernel code.
+
+ If you say N, all options in this submenu will be skipped and disabled.
+
+if MFLD_SENSORS
+config SENSORS_AK8974COMPASS
+ tristate "Ak8974 Compass Module"
+ depends on I2C
+ help
+ To get Compass Sensor output from AK8974 sensor.
+endif #MFLD_SENSORS
diff --git a/drivers/staging/mfld-sensors/Makefile b/drivers/staging/mfld-sensors/Makefile
new file mode 100644
index 00000000000..10ec1e2d561
--- /dev/null
+++ b/drivers/staging/mfld-sensors/Makefile
@@ -0,0 +1,5 @@
+#
+# Makefile for linux/drivers/staging/mfld-sensors
+# Medfield sensor drivers
+#
+obj-$(CONFIG_SENSORS_AK8974COMPASS) += ak8974.o
diff --git a/drivers/staging/mfld-sensors/ak8974.c b/drivers/staging/mfld-sensors/ak8974.c
new file mode 100644
index 00000000000..5b18835acf7
--- /dev/null
+++ b/drivers/staging/mfld-sensors/ak8974.c
@@ -0,0 +1,234 @@
+/*
+ * ak8974.c - AKEMD Compass Driver
+ *
+ * Copyright (C) 2010 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/err.h>
+#include <linux/mutex.h>
+#include <linux/sysfs.h>
+#include <linux/hwmon-sysfs.h>
+
+MODULE_AUTHOR("KalhanTrisal,Anantha Narayanan<anantha.narayanan@intel.com>");
+MODULE_DESCRIPTION("ak8974 Compass Driver");
+MODULE_LICENSE("GPL v2");
+
+/* register address */
+#define DEVICE_ID 0x0F
+#define ADDR_TMPS 0x31
+#define DATA_XL 0x10
+#define DATA_XM 0x11
+#define DATA_YL 0x12
+#define DATA_YM 0x13
+#define DATA_ZL 0x14
+#define DATA_ZM 0x15
+#define STAT_REG 0x18
+#define CNTL_1 0x1B
+#define CNTL_2 0x1C
+#define CNTL_3 0x1D
+#define PRET_REG 0x30
+
+struct compass_data {
+ struct device *hwmon_dev;
+ bool needresume;
+ struct mutex write_lock;
+};
+
+static ssize_t power_mode_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ unsigned int ret_val;
+ struct i2c_client *client = to_i2c_client(dev);
+ client->addr = 0x0F;/*Remove HC address after FW Fix*/
+
+ ret_val = i2c_smbus_read_byte_data(client, CNTL_1);
+ if (ret_val < 0)
+ printk(KERN_WARNING "ak8974: failed power mode read MSB\n");
+ return sprintf(buf, "%d\n", ((char)ret_val & 0x80)>>7);
+}
+
+static void set_power_state(struct i2c_client *client, bool on_off)
+{
+ char read_cntl1 = 0;
+ struct compass_data *data = i2c_get_clientdata(client);
+ client->addr = 0x0F;/* Remove HC address after FW Fix*/
+
+ mutex_lock(&data->write_lock);
+ read_cntl1 = i2c_smbus_read_byte_data(client, CNTL_1);
+ if (on_off)
+ read_cntl1 = (read_cntl1 | 0x80);
+ else
+ read_cntl1 = (read_cntl1 & 0x7F);
+ if (i2c_smbus_write_byte_data(client, CNTL_1, read_cntl1) < 0)
+ printk(KERN_WARNING "ak8974: failed power state write\n");
+
+ mutex_unlock(&data->write_lock);
+}
+
+static ssize_t power_mode_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t count)
+{
+ unsigned long set_val;
+ struct i2c_client *client = to_i2c_client(dev);
+ struct compass_data *data = i2c_get_clientdata(client);
+
+ client->addr = 0x0F;/* Remove HC address after FW Fix*/
+ if (strict_strtoul(buf, 10, &set_val))
+ return -EINVAL;
+
+ if (set_val == 1) {
+ set_power_state(client, true);
+ data->needresume = true;
+ } else if (set_val == 0) {
+ set_power_state(client, false);
+ data->needresume = false;
+ } else
+ return -EINVAL;
+
+ return count;
+}
+
+static ssize_t curr_xyz_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ unsigned short x, y, z;
+ unsigned char temp;
+ struct i2c_client *client = to_i2c_client(dev);
+ client->addr = 0x0F;/* Remove HC address after FW Fix*/
+ if (i2c_smbus_write_byte_data(client, CNTL_3, 0x40) < 0)
+ printk(KERN_WARNING "ak8974: failed xyz control3 register write\n");
+
+ /*Force Read*/
+ x = i2c_smbus_read_byte_data(client, DATA_XM);
+ temp = i2c_smbus_read_byte_data(client, DATA_XL);
+ x = x << 8 | temp;
+ y = i2c_smbus_read_byte_data(client, DATA_YM);
+ temp = i2c_smbus_read_byte_data(client, DATA_YL);
+ y = y << 8 | temp;
+ z = i2c_smbus_read_byte_data(client, DATA_ZM);
+ temp = i2c_smbus_read_byte_data(client, DATA_ZL);
+ z = z << 8 | temp;
+ return sprintf(buf, "%x:%x:%x", x, y, z);
+}
+static void ps_set_default_config(struct i2c_client *client)
+{
+ /*Power ON and set default*/
+ if (i2c_smbus_write_byte_data(client, CNTL_1, 0x82) < 0)
+ printk(KERN_WARNING "ak8974: failed default power on write\n");
+ if (i2c_smbus_write_byte_data(client, PRET_REG, 0x00) < 0)
+ printk(KERN_WARNING "ak8974: failed default control write\n");
+}
+
+static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
+ power_mode_show, power_mode_store);
+static DEVICE_ATTR(curr_pos, S_IRUGO, curr_xyz_show, NULL);
+
+static struct attribute *mid_att_compass[] = {
+ &dev_attr_power_state.attr,
+ &dev_attr_curr_pos.attr,
+ NULL
+};
+
+static struct attribute_group m_compass_gr = {
+ .name = "ak8974",
+ .attrs = mid_att_compass
+};
+
+static int ak8974_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ int res;
+ struct compass_data *data;
+ client->addr = 0x0F;/* Remove HC address after FW Fix*/
+ data = kzalloc(sizeof(struct compass_data), GFP_KERNEL);
+ if (data == NULL) {
+ printk(KERN_WARNING "ak8974: Memory initialization failed");
+ return -ENOMEM;
+ }
+ i2c_set_clientdata(client, data);
+
+ res = sysfs_create_group(&client->dev.kobj, &m_compass_gr);
+ if (res) {
+ printk(KERN_WARNING "ak8974: device_create_file failed!!\n");
+ goto compass_error1;
+ }
+ dev_info(&client->dev, "%s compass chip found \n", client->name);
+ ps_set_default_config(client);
+ data->needresume = true;
+ mutex_init(&data->write_lock);
+ return 0;
+
+compass_error1:
+ i2c_set_clientdata(client, NULL);
+ kfree(data);
+ return res;
+}
+
+static int ak8974_remove(struct i2c_client *client)
+{
+ struct compass_data *data = i2c_get_clientdata(client);
+ client->addr = 0x0F;/* Remove HC address after FW Fix*/
+
+ sysfs_remove_group(&client->dev.kobj, &m_compass_gr);
+ kfree(data);
+ return 0;
+}
+static int ak8974_suspend(struct i2c_client *client, pm_message_t mesg)
+{
+ set_power_state(client, false);
+ return 0;
+}
+static int ak8974_resume(struct i2c_client *client)
+{
+ struct compass_data *data = i2c_get_clientdata(client);
+ if (data->needresume == true)
+ set_power_state(client, true);
+ return 0;
+}
+static struct i2c_device_id ak8974_id[] = {
+ { "i2c_ak8974", 0 },
+ { }
+};
+static struct i2c_driver ak8974_driver = {
+ .driver = {
+ .name = "ak8974",
+ },
+ .probe = ak8974_probe,
+ .remove = ak8974_remove,
+ .suspend = ak8974_suspend,
+ .resume = ak8974_resume,
+ .id_table = ak8974_id,
+};
+
+static int __init sensor_ak8974_init(void)
+{
+ return i2c_add_driver(&ak8974_driver);
+}
+
+static void __exit sensor_ak8974_exit(void)
+{
+ i2c_del_driver(&ak8974_driver);
+}
+module_init(sensor_ak8974_init);
+module_exit(sensor_ak8974_exit);