From 985e28d021f2e0b1298ce143eda0d0c11ffef63d Mon Sep 17 00:00:00 2001 From: "ramesh.chandrasekaran" Date: Thu, 8 Nov 2012 17:03:07 +0530 Subject: st-mems-sensors: Sensor Code Migration to IIO framework Signed-off-by: ramesh.chandrasekaran --- drivers/hwmon/Kconfig | 39 - drivers/hwmon/Makefile | 3 - drivers/hwmon/l3g4200d.c | 719 -------------- drivers/hwmon/lsm303dlh_a.c | 1375 -------------------------- drivers/hwmon/lsm303dlh_m.c | 928 ----------------- drivers/hwmon/lsm303dlhc_a.c | 708 ------------- drivers/staging/iio/accel/Kconfig | 22 + drivers/staging/iio/accel/Makefile | 3 + drivers/staging/iio/accel/lsm303dlh_core.c | 941 ++++++++++++++++++ drivers/staging/iio/accel/lsm303dlhc_core.c | 838 ++++++++++++++++ drivers/staging/iio/gyro/Kconfig | 11 + drivers/staging/iio/gyro/Makefile | 2 + drivers/staging/iio/gyro/l3g4200d_core.c | 931 +++++++++++++++++ drivers/staging/iio/magnetometer/Kconfig | 11 + drivers/staging/iio/magnetometer/Makefile | 1 + drivers/staging/iio/magnetometer/lsm303dlh.c | 929 +++++++++++++++++ include/linux/l3g4200d.h | 11 +- include/linux/lsm303dlh.h | 4 - 18 files changed, 3698 insertions(+), 3778 deletions(-) delete mode 100644 drivers/hwmon/l3g4200d.c delete mode 100644 drivers/hwmon/lsm303dlh_a.c delete mode 100644 drivers/hwmon/lsm303dlh_m.c delete mode 100644 drivers/hwmon/lsm303dlhc_a.c create mode 100644 drivers/staging/iio/accel/lsm303dlh_core.c create mode 100644 drivers/staging/iio/accel/lsm303dlhc_core.c create mode 100644 drivers/staging/iio/gyro/l3g4200d_core.c create mode 100644 drivers/staging/iio/magnetometer/lsm303dlh.c diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index 0c71c0721c8..f7e70e7f537 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -725,20 +725,6 @@ config SENSORS_LTC4151 This driver can also be built as a module. If so, the module will be called ltc4151. -config SENSORS_LSM303DLH - tristate "ST LSM303DLH 3-axis accelerometer and 3-axis magnetometer" - depends on I2C - default n - help - This driver provides support for the LSM303DLH chip which includes a - 3-axis accelerometer and a 3-axis magnetometer. - - This driver can also be built as modules. If so, the module for - accelerometer will be called lsm303dlh_a and for magnetometer it will - be called lsm303dlh_m. - - Say Y here if you have a device containing lsm303dlh chip. - config SENSORS_LSM303DLH_INPUT_DEVICE bool "ST LSM303DLH INPUT DEVICE" depends on SENSORS_LSM303DLH @@ -748,31 +734,6 @@ config SENSORS_LSM303DLH_INPUT_DEVICE interrupts, need to be enabled only when input device support is required. -config SENSORS_LSM303DLHC - tristate "ST LSM303DLHC 3-axis accelerometer and 3-axis magnetometer" - depends on I2C - default n - help - This driver provides support for the LSM303DLHC chip which includes a - 3-axis accelerometer and a 3-axis magnetometer. - - This driver can also be built as modules. If so, the module for - accelerometer will be called lsm303dlhc_a and for magnetometer it will - be called lsm303dlh_m. - - Say Y here if you have a device containing lsm303dlhc chip. - -config SENSORS_L3G4200D - tristate "ST L3G4200D 3-axis gyroscope" - depends on I2C - default n - help - If you say yes here you get support for 3-axis gyroscope device - L3g4200D. - - This driver can also be built as a module. If so, the module - will be called l3g4200d. - config SENSORS_LTC4215 tristate "Linear Technology LTC4215" depends on I2C && EXPERIMENTAL diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index 06eb80a52b7..1e893cbdb83 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -87,9 +87,6 @@ obj-$(CONFIG_SENSORS_LM93) += lm93.o obj-$(CONFIG_SENSORS_LM95241) += lm95241.o obj-$(CONFIG_SENSORS_LM95245) += lm95245.o obj-$(CONFIG_SENSORS_LTC4151) += ltc4151.o -obj-$(CONFIG_SENSORS_LSM303DLH) += lsm303dlh_a.o lsm303dlh_m.o -obj-$(CONFIG_SENSORS_LSM303DLHC)+= lsm303dlhc_a.o -obj-$(CONFIG_SENSORS_L3G4200D) += l3g4200d.o obj-$(CONFIG_SENSORS_LTC4215) += ltc4215.o obj-$(CONFIG_SENSORS_LTC4245) += ltc4245.o obj-$(CONFIG_SENSORS_LTC4261) += ltc4261.o diff --git a/drivers/hwmon/l3g4200d.c b/drivers/hwmon/l3g4200d.c deleted file mode 100644 index d3e0b46b169..00000000000 --- a/drivers/hwmon/l3g4200d.c +++ /dev/null @@ -1,719 +0,0 @@ -/* - * ST L3G4200D 3-Axis Gyroscope Driver - * - * Copyright (C) ST-Ericsson SA 2011 - * Author: Chethan Krishna N for ST-Ericsson - * Licence terms: GNU General Public Licence (GPL) version 2 - */ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#ifdef CONFIG_HAS_EARLYSUSPEND -#include -#endif - -/* l3g4200d gyroscope registers */ - -#define WHO_AM_I 0x0F - -#define CTRL_REG1 0x20 /* CTRL REG1 */ -#define CTRL_REG2 0x21 /* CTRL REG2 */ -#define CTRL_REG3 0x22 /* CTRL_REG3 */ -#define CTRL_REG4 0x23 /* CTRL_REG4 */ -#define CTRL_REG5 0x24 /* CTRL_REG5 */ -#define OUT_TEMP 0x26 /* OUT_TEMP */ - -#define AXISDATA_REG 0x28 - -/** Registers Contents */ - -#define WHOAMI_L3G4200D 0x00D3 /* Expected content for WAI register*/ - -/* CTRL_REG1 */ -#define PM_OFF 0x00 -#define PM_ON 0x01 -#define ENABLE_ALL_AXES 0x07 -#define BW00 0x00 -#define BW01 0x10 -#define BW10 0x20 -#define BW11 0x30 -#define ODR00 0x00 /* ODR = 100Hz */ -#define ODR01 0x40 /* ODR = 200Hz */ -#define ODR10 0x80 /* ODR = 400Hz */ -#define ODR11 0xC0 /* ODR = 800Hz */ -#define L3G4200D_PM_BIT 3 -#define L3G4200D_PM_MASK (0x01 << L3G4200D_PM_BIT) -#define L3G4200D_ODR_BIT 4 -#define L3G4200D_ODR_MASK (0x0F << L3G4200D_ODR_BIT) -#define L3G4200D_ODR_MIN_VAL 0x00 -#define L3G4200D_ODR_MAX_VAL 0x0F - -/* CTRL_REG4 */ -#define FS250 0x00 -#define FS500 0x01 -#define FS2000 0x03 -#define BDU_ENABLE 0x80 -#define L3G4200D_FS_BIT 4 -#define L3G4200D_FS_MASK (0x3 << L3G4200D_FS_BIT) - -/* multiple byte transfer enable */ -#define MULTIPLE_I2C_TR 0x80 - -/* device status defines */ -#define DEVICE_OFF 0 -#define DEVICE_ON 1 -#define DEVICE_SUSPENDED 2 - -/* - * L3G4200D gyroscope data - * brief structure containing gyroscope values for yaw, pitch and roll in - * signed short - */ - -struct l3g4200d_gyro_values { - short x; /* x-axis angular rate data. */ - short y; /* y-axis angluar rate data. */ - short z; /* z-axis angular rate data. */ -}; - -struct l3g4200d_data { - struct i2c_client *client; - struct mutex lock; - struct l3g4200d_gyro_values data; - struct l3g4200d_gyr_platform_data pdata; - struct regulator *regulator; -#ifdef CONFIG_HAS_EARLYSUSPEND - struct early_suspend early_suspend; -#endif - unsigned char powermode; - unsigned char odr; - unsigned char range; - int device_status; -}; - -#ifdef CONFIG_HAS_EARLYSUSPEND -static void l3g4200d_early_suspend(struct early_suspend *ddata); -static void l3g4200d_late_resume(struct early_suspend *ddata); -#endif - -static int l3g4200d_write(struct l3g4200d_data *ddata, u8 reg, - u8 val, char *msg) -{ - int ret = i2c_smbus_write_byte_data(ddata->client, reg, val); - if (ret < 0) - dev_err(&ddata->client->dev, - "i2c_smbus_write_byte_data failed error %d\ - Register (%s)\n", ret, msg); - return ret; -} - -static int l3g4200d_read(struct l3g4200d_data *ddata, u8 reg, char *msg) -{ - int ret = i2c_smbus_read_byte_data(ddata->client, reg); - if (ret < 0) - dev_err(&ddata->client->dev, - "i2c_smbus_read_byte_data failed error %d\ - Register (%s)\n", ret, msg); - return ret; -} - -static int l3g4200d_readdata(struct l3g4200d_data *ddata) -{ - unsigned char gyro_data[6]; - short data[3]; - int ret; - - ret = i2c_smbus_read_i2c_block_data(ddata->client, - AXISDATA_REG | MULTIPLE_I2C_TR, 6, gyro_data); - if (ret < 0) { - dev_err(&ddata->client->dev, - "i2c_smbus_read_byte_data failed error %d\ - Register AXISDATA_REG\n", ret); - return ret; - } - - data[0] = (short) (((gyro_data[1]) << 8) | gyro_data[0]); - data[1] = (short) (((gyro_data[3]) << 8) | gyro_data[2]); - data[2] = (short) (((gyro_data[5]) << 8) | gyro_data[4]); - - data[ddata->pdata.axis_map_x] = ddata->pdata.negative_x ? - -data[ddata->pdata.axis_map_x] : data[ddata->pdata.axis_map_x]; - data[ddata->pdata.axis_map_y] = ddata->pdata.negative_y ? - -data[ddata->pdata.axis_map_y] : data[ddata->pdata.axis_map_y]; - data[ddata->pdata.axis_map_z] = ddata->pdata.negative_z ? - -data[ddata->pdata.axis_map_z] : data[ddata->pdata.axis_map_z]; - - ddata->data.x = data[ddata->pdata.axis_map_x]; - ddata->data.y = data[ddata->pdata.axis_map_y]; - ddata->data.z = data[ddata->pdata.axis_map_z]; - - return ret; -} - -static ssize_t l3g4200d_show_gyrodata(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct l3g4200d_data *ddata = platform_get_drvdata(pdev); - int ret = 0; - - mutex_lock(&ddata->lock); - - if (ddata->powermode == PM_OFF || - ddata->device_status == DEVICE_SUSPENDED) { - mutex_unlock(&ddata->lock); - return ret; - } - - ret = l3g4200d_readdata(ddata); - - if (ret < 0) { - mutex_unlock(&ddata->lock); - return ret; - } - - mutex_unlock(&ddata->lock); - - return sprintf(buf, "%8x:%8x:%8x\n", ddata->data.x, ddata->data.y, - ddata->data.z); -} - -static ssize_t l3g4200d_show_range(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct l3g4200d_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->range); -} - -static ssize_t l3g4200d_store_range(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct l3g4200d_data *ddata = platform_get_drvdata(pdev); - long received_value; - unsigned char value; - int error; - - error = strict_strtol(buf, 0, &received_value); - if (error) - return error; - - /* check if the received range is in valid range */ - if (received_value < FS250 || received_value > FS2000) - return -EINVAL; - - mutex_lock(&ddata->lock); - - if (ddata->powermode == PM_OFF) { - dev_info(&ddata->client->dev, - "The device is switched off, turn it ON using powermode\n"); - mutex_unlock(&ddata->lock); - return count; - } - - /* enable the BDU bit */ - value = BDU_ENABLE; - value |= ((received_value << L3G4200D_FS_BIT) & L3G4200D_FS_MASK); - - ddata->range = received_value; - - error = l3g4200d_write(ddata, CTRL_REG4, value, "CTRL_REG4"); - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - mutex_unlock(&ddata->lock); - return count; -} - -static ssize_t l3g4200d_show_datarate(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct l3g4200d_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->odr >> L3G4200D_ODR_BIT); -} - -static ssize_t l3g4200d_store_datarate(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct l3g4200d_data *ddata = platform_get_drvdata(pdev); - long received_value; - unsigned char value; - int error; - - error = strict_strtol(buf, 0, &received_value); - if (error) - return error; - - /* check if the received output datarate value is in valid range */ - if (received_value < L3G4200D_ODR_MIN_VAL || - received_value > L3G4200D_ODR_MAX_VAL) - return -EINVAL; - - mutex_lock(&ddata->lock); - - if (ddata->powermode == PM_OFF) { - dev_info(&ddata->client->dev, - "The device is switched off, turn it ON using powermode\n"); - mutex_unlock(&ddata->lock); - return count; - } - - /* - * read the current contents of CTRL_REG1 - * retain any bits set other than the odr bits - */ - error = l3g4200d_read(ddata, CTRL_REG1, "CTRL_REG1"); - - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } else - value = error; - - value &= ~L3G4200D_ODR_MASK; - value |= ((received_value << L3G4200D_ODR_BIT) & L3G4200D_ODR_MASK); - - ddata->odr = received_value << L3G4200D_ODR_BIT; - - error = l3g4200d_write(ddata, CTRL_REG1, value, "CTRL_REG1"); - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - mutex_unlock(&ddata->lock); - return count; -} - -static ssize_t l3g4200d_show_powermode(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct l3g4200d_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->powermode); -} - -static ssize_t l3g4200d_store_powermode(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct l3g4200d_data *ddata = platform_get_drvdata(pdev); - long received_value; - unsigned char value; - int error; - - error = strict_strtol(buf, 0, &received_value); - if (error) - return error; - - /* check if the received power mode is either 0 or 1 */ - if (received_value < PM_OFF || received_value > PM_ON) - return -EINVAL; - - mutex_lock(&ddata->lock); - - if (ddata->device_status == DEVICE_SUSPENDED && - received_value == PM_OFF) { - ddata->powermode = received_value; - mutex_unlock(&ddata->lock); - return count; - } - - /* if sent value is same as current value do nothing */ - if (ddata->powermode == received_value) { - mutex_unlock(&ddata->lock); - return count; - } - - /* turn on the power suppliy if it was turned off previously */ - if (ddata->regulator && ddata->powermode == PM_OFF - && (ddata->device_status == DEVICE_OFF - || ddata->device_status == DEVICE_SUSPENDED)) { - regulator_enable(ddata->regulator); - ddata->device_status = DEVICE_ON; - } - - /* - * read the current contents of CTRL_REG1 - * retain any bits set other than the power bit - */ - error = l3g4200d_read(ddata, CTRL_REG1, "CTRL_REG1"); - - if (error < 0) { - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - mutex_unlock(&ddata->lock); - return error; - } else - value = error; - - value &= ~L3G4200D_PM_MASK; - value |= ((received_value << L3G4200D_PM_BIT) & L3G4200D_PM_MASK); - - ddata->powermode = received_value; - - error = l3g4200d_write(ddata, CTRL_REG1, value, "CTRL_REG1"); - if (error < 0) { - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - mutex_unlock(&ddata->lock); - return error; - } - - if (received_value == PM_OFF) { - /* set the other configuration values to defaults */ - ddata->odr = ODR00 | BW00; - ddata->range = FS250; - - /* turn off the power supply */ - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - } - mutex_unlock(&ddata->lock); - return count; -} - -static ssize_t l3g4200d_show_gyrotemp(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct l3g4200d_data *ddata = platform_get_drvdata(pdev); - int ret; - - if (ddata->powermode == PM_OFF || - ddata->device_status == DEVICE_SUSPENDED) - return -EINVAL; - - ret = l3g4200d_read(ddata, OUT_TEMP, "OUT_TEMP"); - if (ret < 0) - return ret; - - return sprintf(buf, "%d\n", ret); -} - -static DEVICE_ATTR(gyrodata, S_IRUGO, l3g4200d_show_gyrodata, NULL); - -static DEVICE_ATTR(range, S_IRUGO | S_IWUSR, - l3g4200d_show_range, l3g4200d_store_range); - -static DEVICE_ATTR(datarate, S_IRUGO | S_IWUSR, - l3g4200d_show_datarate, l3g4200d_store_datarate); - -static DEVICE_ATTR(powermode, S_IRUGO | S_IWUSR, - l3g4200d_show_powermode, l3g4200d_store_powermode); - -static DEVICE_ATTR(gyrotemp, S_IRUGO, l3g4200d_show_gyrotemp, NULL); - -static struct attribute *l3g4200d_attributes[] = { - &dev_attr_gyrodata.attr, - &dev_attr_range.attr, - &dev_attr_datarate.attr, - &dev_attr_powermode.attr, - &dev_attr_gyrotemp.attr, - NULL -}; - -static const struct attribute_group l3g4200d_attr_group = { - .attrs = l3g4200d_attributes, -}; - -static int __devinit l3g4200d_probe(struct i2c_client *client, - const struct i2c_device_id *devid) -{ - int ret = -1; - struct l3g4200d_data *ddata = NULL; - - if (!i2c_check_functionality(client->adapter, - I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_READ_I2C_BLOCK)) - goto exit; - - ddata = kzalloc(sizeof(struct l3g4200d_data), GFP_KERNEL); - if (ddata == NULL) { - dev_err(&client->dev, "memory alocation failed\n"); - ret = -ENOMEM; - goto exit; - } - - ddata->client = client; - i2c_set_clientdata(client, ddata); - - memcpy(&ddata->pdata, client->dev.platform_data, sizeof(ddata->pdata)); - /* store default values in the data structure */ - ddata->odr = ODR00 | BW00; - ddata->range = FS250; - ddata->powermode = PM_OFF; - ddata->device_status = DEVICE_OFF; - - dev_set_name(&client->dev, ddata->pdata.name_gyr); - - ddata->regulator = regulator_get(&client->dev, "vdd"); - if (IS_ERR(ddata->regulator)) { - dev_err(&client->dev, "failed to get regulator\n"); - ret = PTR_ERR(ddata->regulator); - ddata->regulator = NULL; - goto error_op_failed; - } - - if (ddata->regulator) { - regulator_enable(ddata->regulator); - ddata->device_status = DEVICE_ON; - } - - ret = l3g4200d_read(ddata, WHO_AM_I, "WHO_AM_I"); - if (ret < 0) - goto exit_free_regulator; - - if (ret == WHOAMI_L3G4200D) - dev_info(&client->dev, "3-Axis Gyroscope device identification: %d\n", ret); - else - dev_info(&client->dev, "Gyroscope identification did not match\n"); - - mutex_init(&ddata->lock); - - ret = sysfs_create_group(&client->dev.kobj, &l3g4200d_attr_group); - if (ret) - goto exit_free_regulator; -#ifdef CONFIG_HAS_EARLYSUSPEND - ddata->early_suspend.level = - EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; - ddata->early_suspend.suspend = l3g4200d_early_suspend; - ddata->early_suspend.resume = l3g4200d_late_resume; - register_early_suspend(&ddata->early_suspend); -#endif - - /* - * turn off the supplies until somebody turns on the device - * using l3g4200d_store_powermode - */ - if (ddata->device_status == DEVICE_ON && ddata->regulator) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - - return ret; - -exit_free_regulator: - if (ddata->device_status == DEVICE_ON && ddata->regulator) { - regulator_disable(ddata->regulator); - regulator_put(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } -error_op_failed: - kfree(ddata); -exit: - dev_err(&client->dev, "probe function failed %x\n", ret); - return ret; -} - -static int __devexit l3g4200d_remove(struct i2c_client *client) -{ - struct l3g4200d_data *ddata; - ddata = i2c_get_clientdata(client); - sysfs_remove_group(&client->dev.kobj, &l3g4200d_attr_group); - - /* safer to turn off the device */ - if (ddata->powermode != PM_OFF) { - l3g4200d_write(ddata, CTRL_REG1, PM_OFF, "CONTROL"); - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - regulator_put(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - } - - i2c_set_clientdata(client, NULL); - kfree(ddata); - - return 0; -} -#if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_PM) - -static int l3g4200d_do_suspend(struct l3g4200d_data *ddata) -{ - int ret; - - mutex_lock(&ddata->lock); - - if (ddata->powermode == PM_OFF) { - mutex_unlock(&ddata->lock); - return 0; - } - - ret = l3g4200d_write(ddata, CTRL_REG1, PM_OFF, "CONTROL"); - - /* turn off the power when suspending the device */ - if (ddata->regulator) - regulator_disable(ddata->regulator); - - ddata->device_status = DEVICE_SUSPENDED; - - mutex_unlock(&ddata->lock); - return ret; -} - -static int l3g4200d_do_resume(struct l3g4200d_data *ddata) -{ - unsigned char range_value; - unsigned char shifted_powermode = (ddata->powermode << L3G4200D_PM_BIT); - unsigned char shifted_odr = (ddata->odr << L3G4200D_ODR_BIT); - unsigned context = ((shifted_powermode | shifted_odr) | ENABLE_ALL_AXES); - int ret = 0; - - mutex_lock(&ddata->lock); - - if (ddata->device_status == DEVICE_ON) - goto fail; - - /* in correct mode, no need to change it */ - if (ddata->powermode == PM_OFF) { - ddata->device_status = DEVICE_OFF; - goto fail; - } else { - ddata->device_status = DEVICE_ON; - } - - /* turn on the power when resuming the device */ - if (ddata->regulator) - regulator_enable(ddata->regulator); - - ret = l3g4200d_write(ddata, CTRL_REG1, context, "CONTROL"); - if (ret < 0) - goto fail; - - range_value = ddata->range; - range_value <<= L3G4200D_FS_BIT; - range_value |= BDU_ENABLE; - - ret = l3g4200d_write(ddata, CTRL_REG4, range_value, "RANGE"); - -fail: - mutex_unlock(&ddata->lock); - return ret; -} -#endif - -#ifndef CONFIG_HAS_EARLYSUSPEND -#ifdef CONFIG_PM -static int l3g4200d_suspend(struct device *dev) -{ - struct l3g4200d_data *ddata; - int ret; - - ddata = dev_get_drvdata(dev); - - ret = l3g4200d_do_suspend(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while suspending the device\n"); - - return ret; -} - -static int l3g4200d_resume(struct device *dev) -{ - struct l3g4200d_data *ddata; - int ret; - - ddata = dev_get_drvdata(dev); - - ret = l3g4200d_do_resume(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while resuming the device\n"); - - return ret; -} - -static const struct dev_pm_ops l3g4200d_dev_pm_ops = { - .suspend = l3g4200d_suspend, - .resume = l3g4200d_resume, -}; -#endif -#else -static void l3g4200d_early_suspend(struct early_suspend *data) -{ - struct l3g4200d_data *ddata = - container_of(data, struct l3g4200d_data, early_suspend); - int ret; - - ret = l3g4200d_do_suspend(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while suspending the device\n"); -} - -static void l3g4200d_late_resume(struct early_suspend *data) -{ - struct l3g4200d_data *ddata = - container_of(data, struct l3g4200d_data, early_suspend); - int ret; - - ret = l3g4200d_do_resume(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while resuming the device\n"); -} -#endif - -static const struct i2c_device_id l3g4200d_id[] = { - {"l3g4200d", 0 }, - { }, -}; - -static struct i2c_driver l3g4200d_driver = { - .driver = { - .name = "l3g4200d", -#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) - .pm = &l3g4200d_dev_pm_ops, -#endif - }, - .probe = l3g4200d_probe, - .remove = l3g4200d_remove, - .id_table = l3g4200d_id, -}; - -static int __init l3g4200d_init(void) -{ - return i2c_add_driver(&l3g4200d_driver); -} - -static void __exit l3g4200d_exit(void) -{ - i2c_del_driver(&l3g4200d_driver); -} - -module_init(l3g4200d_init); -module_exit(l3g4200d_exit); - -MODULE_DESCRIPTION("l3g4200d digital gyroscope driver"); -MODULE_AUTHOR("Chethan Krishna N"); -MODULE_LICENSE("GPL"); diff --git a/drivers/hwmon/lsm303dlh_a.c b/drivers/hwmon/lsm303dlh_a.c deleted file mode 100644 index 22600c550f9..00000000000 --- a/drivers/hwmon/lsm303dlh_a.c +++ /dev/null @@ -1,1375 +0,0 @@ -/* - * lsm303dlh_a.c - * ST 3-Axis Accelerometer Driver - * - * Copyright (C) 2010 STMicroelectronics - * Author: Carmine Iascone (carmine.iascone@st.com) - * Author: Matteo Dameno (matteo.dameno@st.com) - * - * Copyright (C) 2010 STEricsson - * Author: Mian Yousaf Kaukab - * Updated:Preetham Rao Kaskurthi - * - * 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. - * - * 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, see . - */ - -#include -#include -#include -#include -#include -#include -#include - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE -#include -#include -#include -#endif - -#include -#ifdef CONFIG_HAS_EARLYSUSPEND -#include -#endif -#include - - /* lsm303dlh accelerometer registers */ - #define WHO_AM_I 0x0F - - /* ctrl 1: pm2 pm1 pm0 dr1 dr0 zenable yenable zenable */ - #define CTRL_REG1 0x20 /* power control reg */ - #define CTRL_REG2 0x21 /* power control reg */ - #define CTRL_REG3 0x22 /* power control reg */ - #define CTRL_REG4 0x23 /* interrupt control reg */ - #define CTRL_REG5 0x24 /* interrupt control reg */ - - #define STATUS_REG 0x27 /* status register */ - - #define AXISDATA_REG 0x28 /* axis data */ - - #define INT1_CFG 0x30 /* interrupt 1 configuration */ - #define INT1_SRC 0x31 /* interrupt 1 source reg */ - #define INT1_THS 0x32 /* interrupt 1 threshold */ - #define INT1_DURATION 0x33 /* interrupt 1 threshold */ - - #define INT2_CFG 0x34 /* interrupt 2 configuration */ - #define INT2_SRC 0x35 /* interrupt 2 source reg */ - #define INT2_THS 0x36 /* interrupt 2 threshold */ - #define INT2_DURATION 0x37 /* interrupt 2 threshold */ - - /* Sensitivity adjustment */ - #define SHIFT_ADJ_2G 4 /* 1/16*/ - #define SHIFT_ADJ_4G 3 /* 2/16*/ - #define SHIFT_ADJ_8G 2 /* ~3.9/16*/ - - /* Control register 1 */ - #define LSM303DLH_A_CR1_PM_BIT 5 - #define LSM303DLH_A_CR1_PM_MASK (0x7 << LSM303DLH_A_CR1_PM_BIT) - #define LSM303DLH_A_CR1_DR_BIT 3 - #define LSM303DLH_A_CR1_DR_MASK (0x3 << LSM303DLH_A_CR1_DR_BIT) - #define LSM303DLH_A_CR1_EN_BIT 0 - #define LSM303DLH_A_CR1_EN_MASK (0x7 << LSM303DLH_A_CR1_EN_BIT) - #define LSM303DLH_A_CR1_AXIS_ENABLE 7 - - /* Control register 2 */ - #define LSM303DLH_A_CR4_ST_BIT 1 - #define LSM303DLH_A_CR4_ST_MASK (0x1 << LSM303DLH_A_CR4_ST_BIT) - #define LSM303DLH_A_CR4_STS_BIT 3 - #define LSM303DLH_A_CR4_STS_MASK (0x1 << LSM303DLH_A_CR4_STS_BIT) - #define LSM303DLH_A_CR4_FS_BIT 4 - #define LSM303DLH_A_CR4_FS_MASK (0x3 << LSM303DLH_A_CR4_FS_BIT) - #define LSM303DLH_A_CR4_BLE_BIT 6 - #define LSM303DLH_A_CR4_BLE_MASK (0x3 << LSM303DLH_A_CR4_BLE_BIT) - #define LSM303DLH_A_CR4_BDU_BIT 7 - #define LSM303DLH_A_CR4_BDU_MASK (0x1 << LSM303DLH_A_CR4_BDU_BIT) - - /* Control register 3 */ - #define LSM303DLH_A_CR3_I1_BIT 0 - #define LSM303DLH_A_CR3_I1_MASK (0x3 << LSM303DLH_A_CR3_I1_BIT) - #define LSM303DLH_A_CR3_LIR1_BIT 2 - #define LSM303DLH_A_CR3_LIR1_MASK (0x1 << LSM303DLH_A_CR3_LIR1_BIT) - #define LSM303DLH_A_CR3_I2_BIT 3 - #define LSM303DLH_A_CR3_I2_MASK (0x3 << LSM303DLH_A_CR3_I2_BIT) - #define LSM303DLH_A_CR3_LIR2_BIT 5 - #define LSM303DLH_A_CR3_LIR2_MASK (0x1 << LSM303DLH_A_CR3_LIR2_BIT) - #define LSM303DLH_A_CR3_PPOD_BIT 6 - #define LSM303DLH_A_CR3_PPOD_MASK (0x1 << LSM303DLH_A_CR3_PPOD_BIT) - #define LSM303DLH_A_CR3_IHL_BIT 7 - #define LSM303DLH_A_CR3_IHL_MASK (0x1 << LSM303DLH_A_CR3_IHL_BIT) - - #define LSM303DLH_A_CR3_I_SELF 0x0 - #define LSM303DLH_A_CR3_I_OR 0x1 - #define LSM303DLH_A_CR3_I_DATA 0x2 - #define LSM303DLH_A_CR3_I_BOOT 0x3 - - #define LSM303DLH_A_CR3_LIR_LATCH 0x1 - - /* Range */ - #define LSM303DLH_A_RANGE_2G 0x00 - #define LSM303DLH_A_RANGE_4G 0x01 - #define LSM303DLH_A_RANGE_8G 0x03 - - /* Mode */ - #define LSM303DLH_A_MODE_OFF 0x00 - #define LSM303DLH_A_MODE_NORMAL 0x01 - #define LSM303DLH_A_MODE_LP_HALF 0x02 - #define LSM303DLH_A_MODE_LP_1 0x03 - #define LSM303DLH_A_MODE_LP_2 0x02 - #define LSM303DLH_A_MODE_LP_5 0x05 - #define LSM303DLH_A_MODE_LP_10 0x06 - - /* Rate */ - #define LSM303DLH_A_RATE_50 0x00 - #define LSM303DLH_A_RATE_100 0x01 - #define LSM303DLH_A_RATE_400 0x02 - #define LSM303DLH_A_RATE_1000 0x03 - - /* Sleep & Wake */ - #define LSM303DLH_A_SLEEPWAKE_DISABLE 0x00 - #define LSM303DLH_A_SLEEPWAKE_ENABLE 0x3 - -/* Multiple byte transfer enable */ -#define MULTIPLE_I2C_TR 0x80 - -/* device status defines */ -#define DEVICE_OFF 0 -#define DEVICE_ON 1 -#define DEVICE_SUSPENDED 2 - -/* Range -2048 to 2047 */ -struct lsm303dlh_a_t { - short x; - short y; - short z; -}; - -/** - * struct lsm303dlh_a_data - data structure used by lsm303dlh_a driver - * @client: i2c client - * @lock: mutex lock for sysfs operations - * @data: lsm303dlh_a_t struct containing x, y and z values - * @input_dev: input device - * @input_dev2: input device - * @pdata: lsm303dlh platform data - * @regulator: regulator - * @range: current range value of accelerometer - * @mode: current mode of operation - * @rate: current sampling rate - * @sleep_wake: sleep wake setting - * @shift_adjust: current shift adjust value set according to range - * @interrupt_control: interrupt control settings - * @interrupt_channel: interrupt channel 0 or 1 - * @interrupt_configure: interrupt configurations for two channels - * @interrupt_duration: interrupt duration for two channels - * @interrupt_threshold: interrupt threshold for two channels - * @early_suspend: early suspend structure - * @device_status: device is ON, OFF or SUSPENDED - * @id: accelerometer device id - */ -struct lsm303dlh_a_data { - struct i2c_client *client; - /* lock for sysfs operations */ - struct mutex lock; - struct lsm303dlh_a_t data; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - struct input_dev *input_dev; - struct input_dev *input_dev2; -#endif - - struct lsm303dlh_platform_data pdata; - struct regulator *regulator; - - unsigned char range; - unsigned char mode; - unsigned char rate; - unsigned char sleep_wake; - int shift_adjust; - - unsigned char interrupt_control; - unsigned int interrupt_channel; - - unsigned char interrupt_configure[2]; - unsigned char interrupt_duration[2]; - unsigned char interrupt_threshold[2]; -#ifdef CONFIG_HAS_EARLYSUSPEND - struct early_suspend early_suspend; -#endif - int device_status; - int id; -}; - -#ifdef CONFIG_HAS_EARLYSUSPEND -static void lsm303dlh_a_early_suspend(struct early_suspend *data); -static void lsm303dlh_a_late_resume(struct early_suspend *data); -#endif - -static int lsm303dlh_a_write(struct lsm303dlh_a_data *ddata, u8 reg, - u8 val, char *msg) -{ - int ret = i2c_smbus_write_byte_data(ddata->client, reg, val); - if (ret < 0) - dev_err(&ddata->client->dev, - "i2c_smbus_write_byte_data failed error %d\ - Register (%s)\n", ret, msg); - return ret; -} - -static int lsm303dlh_a_read(struct lsm303dlh_a_data *ddata, u8 reg, char *msg) -{ - int ret = i2c_smbus_read_byte_data(ddata->client, reg); - if (ret < 0) - dev_err(&ddata->client->dev, - "i2c_smbus_read_byte_data failed error %d\ - Register (%s)\n", ret, msg); - return ret; -} - -#if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_PM) -static int lsm303dlh_a_do_suspend(struct lsm303dlh_a_data *ddata) -{ - int ret; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_A_MODE_OFF) { - mutex_unlock(&ddata->lock); - return 0; - } - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - disable_irq(gpio_to_irq(ddata->pdata.irq_a1)); - disable_irq(gpio_to_irq(ddata->pdata.irq_a2)); -#endif - - ret = lsm303dlh_a_write(ddata, CTRL_REG1, - LSM303DLH_A_MODE_OFF, "CONTROL"); - - if (ddata->regulator) - regulator_disable(ddata->regulator); - - ddata->device_status = DEVICE_SUSPENDED; - - mutex_unlock(&ddata->lock); - - return ret; -} - -static int lsm303dlh_a_restore(struct lsm303dlh_a_data *ddata) -{ - unsigned char reg; - unsigned char shifted_mode = (ddata->mode << LSM303DLH_A_CR1_PM_BIT); - unsigned char shifted_rate = (ddata->rate << LSM303DLH_A_CR1_DR_BIT); - unsigned char context = (shifted_mode | shifted_rate); - int ret = 0; - - mutex_lock(&ddata->lock); - - if (ddata->device_status == DEVICE_ON) { - mutex_unlock(&ddata->lock); - return 0; - } - - /* in correct mode, no need to change it */ - if (ddata->mode == LSM303DLH_A_MODE_OFF) { - ddata->device_status = DEVICE_OFF; - mutex_unlock(&ddata->lock); - return 0; - } else - ddata->device_status = DEVICE_ON; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - enable_irq(gpio_to_irq(ddata->pdata.irq_a1)); - enable_irq(gpio_to_irq(ddata->pdata.irq_a2)); -#endif - - if (ddata->regulator) - regulator_enable(ddata->regulator); - - /* BDU should be enabled by default/recommened */ - reg = ddata->range; - reg |= LSM303DLH_A_CR4_BDU_MASK; - context |= LSM303DLH_A_CR1_AXIS_ENABLE; - - ret = lsm303dlh_a_write(ddata, CTRL_REG1, context, - "CTRL_REG1"); - if (ret < 0) - goto fail; - - ret = lsm303dlh_a_write(ddata, CTRL_REG4, reg, "CTRL_REG4"); - - if (ret < 0) - goto fail; - - /* write to the boot bit to reboot memory content */ - ret = lsm303dlh_a_write(ddata, CTRL_REG2, 0x80, "CTRL_REG2"); - - if (ret < 0) - goto fail; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - ret = lsm303dlh_a_write(ddata, CTRL_REG3, ddata->interrupt_control, - "CTRL_REG3"); - - if (ret < 0) - goto fail; - - ret = lsm303dlh_a_write(ddata, INT1_CFG, ddata->interrupt_configure[0], - "INT1_CFG"); - - if (ret < 0) - goto fail; - - ret = lsm303dlh_a_write(ddata, INT2_CFG, ddata->interrupt_configure[1], - "INT2_CFG"); - - if (ret < 0) - goto fail; - - ret = lsm303dlh_a_write(ddata, INT1_THS, ddata->interrupt_threshold[0], - "INT1_THS"); - - if (ret < 0) - goto fail; - - ret = lsm303dlh_a_write(ddata, INT2_THS, ddata->interrupt_threshold[1], - "INT2_THS"); - - if (ret < 0) - goto fail; - - ret = lsm303dlh_a_write(ddata, INT1_DURATION, - ddata->interrupt_duration[0], "INT1_DURATION"); - - if (ret < 0) - goto fail; - - ret = lsm303dlh_a_write(ddata, INT1_DURATION, - ddata->interrupt_duration[1], "INT1_DURATION"); - - if (ret < 0) - goto fail; -#endif - -fail: - if (ret < 0) - dev_err(&ddata->client->dev, "could not restore the device %d\n", ret); - mutex_unlock(&ddata->lock); - return ret; -} -#endif - -static int lsm303dlh_a_readdata(struct lsm303dlh_a_data *ddata) -{ - unsigned char acc_data[6]; - short data[3]; - - int ret = i2c_smbus_read_i2c_block_data(ddata->client, - AXISDATA_REG | MULTIPLE_I2C_TR, 6, acc_data); - if (ret < 0) { - dev_err(&ddata->client->dev, - "i2c_smbus_read_byte_data failed error %d\ - Register AXISDATA_REG \n", ret); - return ret; - } - - data[0] = (short) (((acc_data[1]) << 8) | acc_data[0]); - data[1] = (short) (((acc_data[3]) << 8) | acc_data[2]); - data[2] = (short) (((acc_data[5]) << 8) | acc_data[4]); - - data[0] >>= ddata->shift_adjust; - data[1] >>= ddata->shift_adjust; - data[2] >>= ddata->shift_adjust; - - /* taking position and orientation of x,y,z axis into account*/ - - data[ddata->pdata.axis_map_x] = ddata->pdata.negative_x ? - -data[ddata->pdata.axis_map_x] : data[ddata->pdata.axis_map_x]; - data[ddata->pdata.axis_map_y] = ddata->pdata.negative_y ? - -data[ddata->pdata.axis_map_y] : data[ddata->pdata.axis_map_y]; - data[ddata->pdata.axis_map_z] = ddata->pdata.negative_z ? - -data[ddata->pdata.axis_map_z] : data[ddata->pdata.axis_map_z]; - - ddata->data.x = data[ddata->pdata.axis_map_x]; - ddata->data.y = data[ddata->pdata.axis_map_y]; - ddata->data.z = data[ddata->pdata.axis_map_z]; - - return ret; -} - -static ssize_t lsm303dlh_a_show_data(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - int ret = 0; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_A_MODE_OFF || - ddata->device_status == DEVICE_SUSPENDED) { - mutex_unlock(&ddata->lock); - return ret; - } - - ret = lsm303dlh_a_readdata(ddata); - - if (ret < 0) { - mutex_unlock(&ddata->lock); - return ret; - } - - mutex_unlock(&ddata->lock); - - return sprintf(buf, "%8x:%8x:%8x\n", ddata->data.x, ddata->data.y, - ddata->data.z); -} - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE -static irqreturn_t lsm303dlh_a_gpio_irq(int irq, void *device_data) -{ - - struct lsm303dlh_a_data *ddata = device_data; - int ret; - unsigned char reg; - struct input_dev *input; - - /* know your interrupt source */ - if (irq == gpio_to_irq(ddata->pdata.irq_a1)) { - reg = INT1_SRC; - input = ddata->input_dev; - } else if (irq == gpio_to_irq(ddata->pdata.irq_a2)) { - reg = INT2_SRC; - input = ddata->input_dev2; - } else { - dev_err(&ddata->client->dev, "spurious interrupt"); - return IRQ_HANDLED; - } - - /* read the axis */ - ret = lsm303dlh_a_readdata(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "reading data of xyz failed error %d\n", ret); - - input_report_abs(input, ABS_X, ddata->data.x); - input_report_abs(input, ABS_Y, ddata->data.y); - input_report_abs(input, ABS_Z, ddata->data.z); - input_sync(input); - - /* clear the value by reading it */ - ret = lsm303dlh_a_read(ddata, reg, "INTTERUPT SOURCE"); - if (ret < 0) - dev_err(&ddata->client->dev, - "clearing interrupt source failed error %d\n", ret); - - return IRQ_HANDLED; - -} - -static ssize_t lsm303dlh_a_show_interrupt_control(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->interrupt_control); -} - -static ssize_t lsm303dlh_a_store_interrupt_control(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - unsigned long val; - int error; - - error = strict_strtoul(buf, 0, &val); - if (error) - return error; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_A_MODE_OFF) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - mutex_unlock(&ddata->lock); - return count; - } - - ddata->interrupt_control = val; - - error = lsm303dlh_a_write(ddata, CTRL_REG3, val, "CTRL_REG3"); - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - mutex_unlock(&ddata->lock); - - return count; -} - -static ssize_t lsm303dlh_a_show_interrupt_channel(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->interrupt_channel); -} - -static ssize_t lsm303dlh_a_store_interrupt_channel(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - unsigned long val; - int error; - - error = strict_strtoul(buf, 0, &val); - if (error) - return error; - - ddata->interrupt_channel = val; - - return count; -} - -static ssize_t lsm303dlh_a_show_interrupt_configure(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", - ddata->interrupt_configure[ddata->interrupt_channel]); -} - -static ssize_t lsm303dlh_a_store_interrupt_configure(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - unsigned long val; - int error; - - error = strict_strtoul(buf, 0, &val); - if (error) - return error; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_A_MODE_OFF) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - mutex_unlock(&ddata->lock); - return count; - } - - ddata->interrupt_configure[ddata->interrupt_channel] = val; - - if (ddata->interrupt_channel == 0x0) - error = lsm303dlh_a_write(ddata, INT1_CFG, val, "INT1_CFG"); - else - error = lsm303dlh_a_write(ddata, INT2_CFG, val, "INT2_CFG"); - - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - mutex_unlock(&ddata->lock); - - return count; -} - -static ssize_t lsm303dlh_a_show_interrupt_duration(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", - ddata->interrupt_duration[ddata->interrupt_channel]); -} - -static ssize_t lsm303dlh_a_store_interrupt_duration(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - unsigned long val; - int error; - - error = strict_strtoul(buf, 0, &val); - if (error) - return error; - - mutex_lock(&ddata->lock); - - - if (ddata->mode == LSM303DLH_A_MODE_OFF) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - mutex_unlock(&ddata->lock); - return count; - } - - ddata->interrupt_duration[ddata->interrupt_channel] = val; - - if (ddata->interrupt_channel == 0x0) - error = lsm303dlh_a_write(ddata, INT1_DURATION, val, - "INT1_DURATION"); - else - error = lsm303dlh_a_write(ddata, INT2_DURATION, val, - "INT2_DURATION"); - - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - mutex_unlock(&ddata->lock); - - return count; -} - -static ssize_t lsm303dlh_a_show_interrupt_threshold(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", - ddata->interrupt_threshold[ddata->interrupt_channel]); -} - -static ssize_t lsm303dlh_a_store_interrupt_threshold(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - unsigned long val; - int error; - - error = strict_strtoul(buf, 0, &val); - if (error) - return error; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_A_MODE_OFF) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - mutex_unlock(&ddata->lock); - return count; - } - - ddata->interrupt_threshold[ddata->interrupt_channel] = val; - - if (ddata->interrupt_channel == 0x0) - error = lsm303dlh_a_write(ddata, INT1_THS, val, "INT1_THS"); - else - error = lsm303dlh_a_write(ddata, INT2_THS, val, "INT2_THS"); - - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - mutex_unlock(&ddata->lock); - - return count; -} -#endif - -static ssize_t lsm303dlh_a_show_range(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->range >> LSM303DLH_A_CR4_FS_BIT); -} - -static ssize_t lsm303dlh_a_store_range(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - long val; - unsigned long bdu_enabled_val; - int error; - - - error = strict_strtol(buf, 0, &val); - if (error) - return error; - - if (val < LSM303DLH_A_RANGE_2G || val > LSM303DLH_A_RANGE_8G) - return -EINVAL; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_A_MODE_OFF) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - mutex_unlock(&ddata->lock); - return count; - } - - ddata->range = val; - ddata->range <<= LSM303DLH_A_CR4_FS_BIT; - - /* - * Block mode update is recommended for not - * ending up reading different values - */ - bdu_enabled_val = ddata->range; - bdu_enabled_val |= LSM303DLH_A_CR4_BDU_MASK; - - error = lsm303dlh_a_write(ddata, CTRL_REG4, bdu_enabled_val, - "CTRL_REG4"); - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - switch (val) { - case LSM303DLH_A_RANGE_2G: - ddata->shift_adjust = SHIFT_ADJ_2G; - break; - case LSM303DLH_A_RANGE_4G: - ddata->shift_adjust = SHIFT_ADJ_4G; - break; - case LSM303DLH_A_RANGE_8G: - ddata->shift_adjust = SHIFT_ADJ_8G; - break; - default: - mutex_unlock(&ddata->lock); - return -EINVAL; - } - - mutex_unlock(&ddata->lock); - - return count; -} - -static ssize_t lsm303dlh_a_show_mode(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->mode); -} - -static ssize_t lsm303dlh_a_store_mode(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - long val; - unsigned char data; - int error; - bool set_boot_bit = false; - - error = strict_strtol(buf, 0, &val); - if (error) - return error; - - mutex_lock(&ddata->lock); - - /* not in correct range */ - - if (val < LSM303DLH_A_MODE_OFF || val > LSM303DLH_A_MODE_LP_10) { - mutex_unlock(&ddata->lock); - return -EINVAL; - } - - if (ddata->device_status == DEVICE_SUSPENDED) { - if (val == LSM303DLH_A_MODE_OFF) { - ddata->mode = val; - mutex_unlock(&ddata->lock); - return count; - } else { - /* device is turning on after suspend, reset memory */ - set_boot_bit = true; - } - } - - /* if same mode as existing, return */ - if (ddata->mode == val) { - mutex_unlock(&ddata->lock); - return count; - } - - /* turn on the supplies if already off */ - if (ddata->regulator && ddata->mode == LSM303DLH_A_MODE_OFF - && (ddata->device_status == DEVICE_OFF - || ddata->device_status == DEVICE_SUSPENDED)) { - regulator_enable(ddata->regulator); - ddata->device_status = DEVICE_ON; -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - enable_irq(gpio_to_irq(ddata->pdata.irq_a1)); - enable_irq(gpio_to_irq(ddata->pdata.irq_a2)); -#endif - } - - data = lsm303dlh_a_read(ddata, CTRL_REG1, "CTRL_REG1"); - - /* - * If chip doesn't get reset during suspend/resume, - * x,y and z axis bits are getting cleared,so set - * these bits to get x,y,z axis data. - */ - data |= LSM303DLH_A_CR1_AXIS_ENABLE; - data &= ~LSM303DLH_A_CR1_PM_MASK; - - ddata->mode = val; - data |= ((val << LSM303DLH_A_CR1_PM_BIT) & LSM303DLH_A_CR1_PM_MASK); - - error = lsm303dlh_a_write(ddata, CTRL_REG1, data, "CTRL_REG1"); - if (error < 0) { - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - mutex_unlock(&ddata->lock); - return error; - } - - /* - * Power on request when device is in suspended state - * write to the boot bit in CTRL_REG2 to reboot memory content - * and ensure correct device behavior after it resumes - */ - if (set_boot_bit) { - error = lsm303dlh_a_write(ddata, CTRL_REG2, 0x80, "CTRL_REG2"); - if (error < 0) { - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - mutex_unlock(&ddata->lock); - return error; - } - } - - if (val == LSM303DLH_A_MODE_OFF) { -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - disable_irq(gpio_to_irq(ddata->pdata.irq_a1)); - disable_irq(gpio_to_irq(ddata->pdata.irq_a2)); -#endif - /* - * No need to store context here - * it is not like suspend/resume - * but fall back to default values - */ - ddata->rate = LSM303DLH_A_RATE_50; - ddata->range = LSM303DLH_A_RANGE_2G; - ddata->shift_adjust = SHIFT_ADJ_2G; - - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - } - mutex_unlock(&ddata->lock); - - return count; -} - -static ssize_t lsm303dlh_a_show_rate(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->rate); -} - -static ssize_t lsm303dlh_a_store_rate(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - long val; - unsigned char data; - int error; - - error = strict_strtol(buf, 0, &val); - if (error) - return error; - - if (val < LSM303DLH_A_RATE_50 || val > LSM303DLH_A_RATE_1000) - return -EINVAL; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_A_MODE_OFF) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - mutex_unlock(&ddata->lock); - return count; - } - - data = lsm303dlh_a_read(ddata, CTRL_REG1, "CTRL_REG1"); - - data &= ~LSM303DLH_A_CR1_DR_MASK; - - ddata->rate = val; - - data |= ((val << LSM303DLH_A_CR1_DR_BIT) & LSM303DLH_A_CR1_DR_MASK); - - error = lsm303dlh_a_write(ddata, CTRL_REG1, data, "CTRL_REG1"); - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - mutex_unlock(&ddata->lock); - - return count; -} - -static ssize_t lsm303dlh_a_show_sleepwake(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->sleep_wake); -} - -static ssize_t lsm303dlh_a_store_sleepwake(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - long val; - int error; - - if (ddata->mode == LSM303DLH_A_MODE_OFF) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - return count; - } - - error = strict_strtoul(buf, 0, &val); - if (error) - return error; - - mutex_lock(&ddata->lock); - - ddata->sleep_wake = val; - - error = lsm303dlh_a_write(ddata, CTRL_REG5, ddata->sleep_wake, - "CTRL_REG5"); - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - mutex_unlock(&ddata->lock); - - return count; -} - -static ssize_t lsm303dlh_a_show_id(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->id); -} - -static DEVICE_ATTR(id, S_IRUGO, lsm303dlh_a_show_id, NULL); - -static DEVICE_ATTR(data, S_IRUGO, lsm303dlh_a_show_data, NULL); - -static DEVICE_ATTR(range, S_IWUSR | S_IRUGO, - lsm303dlh_a_show_range, lsm303dlh_a_store_range); - -static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, - lsm303dlh_a_show_mode, lsm303dlh_a_store_mode); - -static DEVICE_ATTR(rate, S_IWUSR | S_IRUGO, - lsm303dlh_a_show_rate, lsm303dlh_a_store_rate); - -static DEVICE_ATTR(sleep_wake, S_IWUSR | S_IRUGO, - lsm303dlh_a_show_sleepwake, lsm303dlh_a_store_sleepwake); - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE -static DEVICE_ATTR(interrupt_control, S_IWUGO | S_IRUGO, - lsm303dlh_a_show_interrupt_control, - lsm303dlh_a_store_interrupt_control); - -static DEVICE_ATTR(interrupt_channel, S_IWUGO | S_IRUGO, - lsm303dlh_a_show_interrupt_channel, - lsm303dlh_a_store_interrupt_channel); - -static DEVICE_ATTR(interrupt_configure, S_IWUGO | S_IRUGO, - lsm303dlh_a_show_interrupt_configure, - lsm303dlh_a_store_interrupt_configure); - -static DEVICE_ATTR(interrupt_duration, S_IWUGO | S_IRUGO, - lsm303dlh_a_show_interrupt_duration, - lsm303dlh_a_store_interrupt_duration); - -static DEVICE_ATTR(interrupt_threshold, S_IWUGO | S_IRUGO, - lsm303dlh_a_show_interrupt_threshold, - lsm303dlh_a_store_interrupt_threshold); -#endif - -static struct attribute *lsm303dlh_a_attributes[] = { - &dev_attr_id.attr, - &dev_attr_data.attr, - &dev_attr_range.attr, - &dev_attr_mode.attr, - &dev_attr_rate.attr, - &dev_attr_sleep_wake.attr, -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - &dev_attr_interrupt_control.attr, - &dev_attr_interrupt_channel.attr, - &dev_attr_interrupt_configure.attr, - &dev_attr_interrupt_duration.attr, - &dev_attr_interrupt_threshold.attr, -#endif - NULL -}; - -static const struct attribute_group lsm303dlh_a_attr_group = { - .attrs = lsm303dlh_a_attributes, -}; - -static int __devinit lsm303dlh_a_probe(struct i2c_client *client, - const struct i2c_device_id *id) -{ - int ret; - struct lsm303dlh_a_data *ddata = NULL; - - ddata = kzalloc(sizeof(struct lsm303dlh_a_data), GFP_KERNEL); - if (ddata == NULL) { - dev_err(&client->dev, "memory alocation failed\n"); - ret = -ENOMEM; - goto err_alloc; - } - - ddata->client = client; - i2c_set_clientdata(client, ddata); - - /* copy platform specific data */ - memcpy(&ddata->pdata, client->dev.platform_data, sizeof(ddata->pdata)); - ddata->mode = LSM303DLH_A_MODE_OFF; - ddata->rate = LSM303DLH_A_RATE_50; - ddata->range = LSM303DLH_A_RANGE_2G; - ddata->sleep_wake = LSM303DLH_A_SLEEPWAKE_DISABLE; - ddata->shift_adjust = SHIFT_ADJ_2G; - ddata->device_status = DEVICE_OFF; - dev_set_name(&client->dev, ddata->pdata.name_a); - - ddata->regulator = regulator_get(&client->dev, "vdd"); - if (IS_ERR(ddata->regulator)) { - dev_err(&client->dev, "failed to get regulator\n"); - ret = PTR_ERR(ddata->regulator); - ddata->regulator = NULL; - goto err_op_failed; - } - - if (ddata->regulator) { - /* - * 0.83 milliamps typical with magnetic sensor setting ODR = - * 7.5 Hz, Accelerometer sensor ODR = 50 Hz. Double for - * safety. - */ - regulator_set_optimum_mode(ddata->regulator, 830 * 2); - regulator_enable(ddata->regulator); - ddata->device_status = DEVICE_ON; - } - - ret = lsm303dlh_a_read(ddata, WHO_AM_I, "WHO_AM_I"); - if (ret < 0) - goto exit_free_regulator; - - dev_info(&client->dev, "3-Axis Accelerometer, ID : %d\n", - ret); - ddata->id = ret; - - mutex_init(&ddata->lock); - - ret = sysfs_create_group(&client->dev.kobj, &lsm303dlh_a_attr_group); - if (ret) - goto exit_free_regulator; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - - /* accelerometer has two interrupts channels - (thresholds,durations and sources) - and can support two input devices */ - - ddata->input_dev = input_allocate_device(); - if (!ddata->input_dev) { - ret = -ENOMEM; - dev_err(&client->dev, "Failed to allocate input device\n"); - goto exit_free_regulator; - } - - ddata->input_dev2 = input_allocate_device(); - if (!ddata->input_dev2) { - ret = -ENOMEM; - dev_err(&client->dev, "Failed to allocate input device\n"); - goto err_input_alloc_failed; - } - - set_bit(EV_ABS, ddata->input_dev->evbit); - set_bit(EV_ABS, ddata->input_dev2->evbit); - - /* x-axis acceleration */ - input_set_abs_params(ddata->input_dev, ABS_X, -32768, 32767, 0, 0); - input_set_abs_params(ddata->input_dev2, ABS_X, -32768, 32767, 0, 0); - /* y-axis acceleration */ - input_set_abs_params(ddata->input_dev, ABS_Y, -32768, 32767, 0, 0); - input_set_abs_params(ddata->input_dev2, ABS_Y, -32768, 32767, 0, 0); - /* z-axis acceleration */ - input_set_abs_params(ddata->input_dev, ABS_Z, -32768, 32767, 0, 0); - input_set_abs_params(ddata->input_dev2, ABS_Z, -32768, 32767, 0, 0); - - ddata->input_dev->name = "accelerometer"; - ddata->input_dev2->name = "motion"; - - ret = input_register_device(ddata->input_dev); - if (ret) { - dev_err(&client->dev, "Unable to register input device: %s\n", - ddata->input_dev->name); - goto err_input_register_failed; - } - - ret = input_register_device(ddata->input_dev2); - if (ret) { - dev_err(&client->dev, "Unable to register input device: %s\n", - ddata->input_dev->name); - goto err_input_register_failed2; - } - - /* Register interrupt */ - ret = request_threaded_irq(gpio_to_irq(ddata->pdata.irq_a1), NULL, - lsm303dlh_a_gpio_irq, - IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, - "lsm303dlh_a", ddata); - if (ret) { - dev_err(&client->dev, "request irq1 failed\n"); - goto err_input_failed; - } - - ret = request_threaded_irq(gpio_to_irq(ddata->pdata.irq_a2), NULL, - lsm303dlh_a_gpio_irq, - IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, - "lsm303dlh_a", ddata); - if (ret) { - dev_err(&client->dev, "request irq2 failed\n"); - goto err_input_failed; - } - - /* only mode can enable it */ - disable_irq(gpio_to_irq(ddata->pdata.irq_a1)); - disable_irq(gpio_to_irq(ddata->pdata.irq_a2)); - -#endif - -#ifdef CONFIG_HAS_EARLYSUSPEND - ddata->early_suspend.level = - EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; - ddata->early_suspend.suspend = lsm303dlh_a_early_suspend; - ddata->early_suspend.resume = lsm303dlh_a_late_resume; - register_early_suspend(&ddata->early_suspend); -#endif - - if (ddata->device_status == DEVICE_ON && ddata->regulator) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - return ret; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE -err_input_failed: - input_unregister_device(ddata->input_dev2); -err_input_register_failed2: - input_unregister_device(ddata->input_dev); -err_input_register_failed: - input_free_device(ddata->input_dev2); -err_input_alloc_failed: - input_free_device(ddata->input_dev); -#endif -exit_free_regulator: - if (ddata->device_status == DEVICE_ON && ddata->regulator) { - regulator_disable(ddata->regulator); - regulator_put(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - -err_op_failed: - kfree(ddata); -err_alloc: - dev_err(&client->dev, "probe function fails %x", ret); - return ret; -} - -static int __devexit lsm303dlh_a_remove(struct i2c_client *client) -{ - int ret; - struct lsm303dlh_a_data *ddata; - - ddata = i2c_get_clientdata(client); -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - input_unregister_device(ddata->input_dev); - input_unregister_device(ddata->input_dev2); - input_free_device(ddata->input_dev); - input_free_device(ddata->input_dev2); -#endif - sysfs_remove_group(&client->dev.kobj, &lsm303dlh_a_attr_group); - - /* safer to make device off */ - if (ddata->mode != LSM303DLH_A_MODE_OFF) { - ret = lsm303dlh_a_write(ddata, CTRL_REG1, 0, "CONTROL"); - - if (ret < 0) { - dev_err(&client->dev, "could not turn off the device %d", ret); - return ret; - } - - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - regulator_put(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - } - - i2c_set_clientdata(client, NULL); - kfree(ddata); - - return 0; -} - -#ifndef CONFIG_HAS_EARLYSUSPEND -#ifdef CONFIG_PM -static int lsm303dlh_a_suspend(struct device *dev) -{ - struct lsm303dlh_a_data *ddata; - int ret; - - ddata = dev_get_drvdata(dev); - - ret = lsm303dlh_a_do_suspend(ddata); - - return ret; -} - -static int lsm303dlh_a_resume(struct device *dev) -{ - struct lsm303dlh_a_data *ddata; - int ret; - - ddata = dev_get_drvdata(dev); - - ret = lsm303dlh_a_restore(ddata); - - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while resuming the device"); - - return ret; -} -static const struct dev_pm_ops lsm303dlh_a_dev_pm_ops = { - .suspend = lsm303dlh_a_suspend, - .resume = lsm303dlh_a_resume, -}; -#endif -#else -static void lsm303dlh_a_early_suspend(struct early_suspend *data) -{ - struct lsm303dlh_a_data *ddata = - container_of(data, struct lsm303dlh_a_data, early_suspend); - int ret; - - ret = lsm303dlh_a_do_suspend(ddata); -} - -static void lsm303dlh_a_late_resume(struct early_suspend *data) -{ - struct lsm303dlh_a_data *ddata = - container_of(data, struct lsm303dlh_a_data, early_suspend); - int ret; - - ret = lsm303dlh_a_restore(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "lsm303dlh_a late resume failed\n"); -} -#endif /* CONFIG_PM */ - -static const struct i2c_device_id lsm303dlh_a_id[] = { - { "lsm303dlh_a", 0 }, - { }, -}; - -static struct i2c_driver lsm303dlh_a_driver = { - .probe = lsm303dlh_a_probe, - .remove = lsm303dlh_a_remove, - .id_table = lsm303dlh_a_id, - .driver = { - .name = "lsm303dlh_a", -#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) - .pm = &lsm303dlh_a_dev_pm_ops, -#endif - }, -}; - -static int __init lsm303dlh_a_init(void) -{ - return i2c_add_driver(&lsm303dlh_a_driver); -} - -static void __exit lsm303dlh_a_exit(void) -{ - i2c_del_driver(&lsm303dlh_a_driver); -} - -module_init(lsm303dlh_a_init) -module_exit(lsm303dlh_a_exit) - -MODULE_DESCRIPTION("lSM303DLH 3-Axis Accelerometer Driver"); -MODULE_LICENSE("GPL"); -MODULE_AUTHOR("STMicroelectronics"); diff --git a/drivers/hwmon/lsm303dlh_m.c b/drivers/hwmon/lsm303dlh_m.c deleted file mode 100644 index 270e532f78b..00000000000 --- a/drivers/hwmon/lsm303dlh_m.c +++ /dev/null @@ -1,928 +0,0 @@ -/* - * lsm303dlh_m.c - * ST 3-Axis Magnetometer Driver - * - * Copyright (C) 2010 STMicroelectronics - * Author: Carmine Iascone (carmine.iascone@st.com) - * Author: Matteo Dameno (matteo.dameno@st.com) - * - * Copyright (C) 2010 STEricsson - * Author: Mian Yousaf Kaukab - * Updated:Preetham Rao Kaskurthi - * - * 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. - * - * 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, see . - */ - -#include -#include -#include -#include -#include -#include -#include - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE -#include -#include -#include -#endif - -#include -#include -#ifdef CONFIG_HAS_EARLYSUSPEND -#include -#endif -#include - -/* lsm303dlh magnetometer registers */ -#define IRA_REG_M 0x0A - -/* Magnetometer registers */ -#define CRA_REG_M 0x00 /* Configuration register A */ -#define CRB_REG_M 0x01 /* Configuration register B */ -#define MR_REG_M 0x02 /* Mode register */ -#define SR_REG_M 0x09 /* Status register */ - -/* Output register start address*/ -#define OUT_X_M 0x03 -#define OUT_Y_M 0x05 -#define OUT_Z_M 0x07 - -/* Magnetometer X-Y gain */ -#define XY_GAIN_1_3 1055 /* XY gain at 1.3G */ -#define XY_GAIN_1_9 795 /* XY gain at 1.9G */ -#define XY_GAIN_2_5 635 /* XY gain at 2.5G */ -#define XY_GAIN_4_0 430 /* XY gain at 4.0G */ -#define XY_GAIN_4_7 375 /* XY gain at 4.7G */ -#define XY_GAIN_5_6 320 /* XY gain at 5.6G */ -#define XY_GAIN_8_1 230 /* XY gain at 8.1G */ - -/* Magnetometer Z gain */ -#define Z_GAIN_1_3 950 /* Z gain at 1.3G */ -#define Z_GAIN_1_9 710 /* Z gain at 1.9G */ -#define Z_GAIN_2_5 570 /* Z gain at 2.5G */ -#define Z_GAIN_4_0 385 /* Z gain at 4.0G */ -#define Z_GAIN_4_7 335 /* Z gain at 4.7G */ -#define Z_GAIN_5_6 285 /* Z gain at 5.6G */ -#define Z_GAIN_8_1 205 /* Z gain at 8.1G */ - -/* Control A regsiter. */ -#define LSM303DLH_M_CRA_DO_BIT 2 -#define LSM303DLH_M_CRA_DO_MASK (0x7 << LSM303DLH_M_CRA_DO_BIT) -#define LSM303DLH_M_CRA_MS_BIT 0 -#define LSM303DLH_M_CRA_MS_MASK (0x3 << LSM303DLH_M_CRA_MS_BIT) - -/* Control B regsiter. */ -#define LSM303DLH_M_CRB_GN_BIT 5 -#define LSM303DLH_M_CRB_GN_MASK (0x7 << LSM303DLH_M_CRB_GN_BIT) - -/* Control Mode regsiter. */ -#define LSM303DLH_M_MR_MD_BIT 0 -#define LSM303DLH_M_MR_MD_MASK (0x3 << LSM303DLH_M_MR_MD_BIT) - -/* Control Status regsiter. */ -#define LSM303DLH_M_SR_RDY_BIT 0 -#define LSM303DLH_M_SR_RDY_MASK (0x1 << LSM303DLH_M_SR_RDY_BIT) -#define LSM303DLH_M_SR_LOC_BIT 1 -#define LSM303DLH_M_SR_LCO_MASK (0x1 << LSM303DLH_M_SR_LOC_BIT) -#define LSM303DLH_M_SR_REN_BIT 2 -#define LSM303DLH_M_SR_REN_MASK (0x1 << LSM303DLH_M_SR_REN_BIT) - -/* Magnetometer gain setting */ -#define LSM303DLH_M_RANGE_1_3G 0x01 -#define LSM303DLH_M_RANGE_1_9G 0x02 -#define LSM303DLH_M_RANGE_2_5G 0x03 -#define LSM303DLH_M_RANGE_4_0G 0x04 -#define LSM303DLH_M_RANGE_4_7G 0x05 -#define LSM303DLH_M_RANGE_5_6G 0x06 -#define LSM303DLH_M_RANGE_8_1G 0x07 - -/* Magnetometer capturing mode */ -#define LSM303DLH_M_MODE_CONTINUOUS 0 -#define LSM303DLH_M_MODE_SINGLE 1 -#define LSM303DLH_M_MODE_SLEEP 3 - -/* Magnetometer output data rate */ -#define LSM303DLH_M_RATE_00_75 0x00 -#define LSM303DLH_M_RATE_01_50 0x01 -#define LSM303DLH_M_RATE_03_00 0x02 -#define LSM303DLH_M_RATE_07_50 0x03 -#define LSM303DLH_M_RATE_15_00 0x04 -#define LSM303DLH_M_RATE_30_00 0x05 -#define LSM303DLH_M_RATE_75_00 0x06 - -#ifdef CONFIG_SENSORS_LSM303DLHC -#define LSM303DLH_M_RATE_220_00 0x07 -#endif - -/* Multiple byte transfer enable */ -#define MULTIPLE_I2C_TR 0x80 - -/* device status defines */ -#define DEVICE_OFF 0 -#define DEVICE_ON 1 -#define DEVICE_SUSPENDED 2 - -/* device CHIP ID defines */ -#define LSM303DLHC_CHIP_ID 51 - -/** - * struct lsm303dlh_m_data - data structure used by lsm303dlh_m driver - * @client: i2c client - * @lock: mutex lock for sysfs operations - * @input_dev: input device - * @regulator: regulator - * @pdata: lsm303dlh platform data - * @gain: x, y and z axes gain - * @data: Magnetic field values of x, y and z axes - * @mode: current mode of operation - * @rate: current sampling rate - * @range: current range value of magnetometer - * @early_suspend: early suspend structure - * @device_status: device is ON, OFF or SUSPENDED - */ -struct lsm303dlh_m_data { - struct i2c_client *client; - /* lock for sysfs operations */ - struct mutex lock; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - struct input_dev *input_dev; -#endif - struct regulator *regulator; - struct lsm303dlh_platform_data pdata; - - short gain[3]; - short data[3]; - unsigned char mode; - unsigned char rate; - unsigned char range; -#ifdef CONFIG_HAS_EARLYSUSPEND - struct early_suspend early_suspend; -#endif - int device_status; -}; - -#ifdef CONFIG_HAS_EARLYSUSPEND -static void lsm303dlh_m_early_suspend(struct early_suspend *data); -static void lsm303dlh_m_late_resume(struct early_suspend *data); -#endif - -static int lsm303dlh_m_set_mode(struct lsm303dlh_m_data *ddata, - unsigned char mode); -static int lsm303dlh_m_write(struct lsm303dlh_m_data *ddata, - u8 reg, u8 val, char *msg) -{ - int ret = i2c_smbus_write_byte_data(ddata->client, reg, val); - if (ret < 0) - dev_err(&ddata->client->dev, - "i2c_smbus_write_byte_data failed error %d\ - Register (%s)\n", ret, msg); - return ret; -} - -#if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_PM) -static int lsm303dlh_m_do_suspend(struct lsm303dlh_m_data *ddata) -{ - int ret; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_M_MODE_SLEEP) { - mutex_unlock(&ddata->lock); - return 0; - } - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - disable_irq(gpio_to_irq(ddata->pdata.irq_m)); -#endif - - ret = lsm303dlh_m_set_mode(ddata, LSM303DLH_M_MODE_SLEEP); - - if (ddata->regulator) - regulator_disable(ddata->regulator); - - ddata->device_status = DEVICE_SUSPENDED; - - mutex_unlock(&ddata->lock); - - return ret; -} - -static int lsm303dlh_m_restore(struct lsm303dlh_m_data *ddata) -{ - int ret = 0; - - mutex_lock(&ddata->lock); - - if (ddata->device_status == DEVICE_ON) { - mutex_unlock(&ddata->lock); - return 0; - } - - /* in correct mode, no need to change it */ - if (ddata->mode == LSM303DLH_M_MODE_SLEEP) { - ddata->device_status = DEVICE_OFF; - mutex_unlock(&ddata->lock); - return 0; - } else - ddata->device_status = DEVICE_ON; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - enable_irq(gpio_to_irq(ddata->pdata.irq_m)); -#endif - - if (ddata->regulator) - regulator_enable(ddata->regulator); - - ret = lsm303dlh_m_write(ddata, CRB_REG_M, ddata->range, "SET RANGE"); - - if (ret < 0) - goto fail; - - ret = lsm303dlh_m_write(ddata, CRA_REG_M, ddata->rate, "SET RATE"); - - if (ret < 0) - goto fail; - - ret = lsm303dlh_m_set_mode(ddata, ddata->mode); - - if (ret < 0) - goto fail; - -fail: - mutex_unlock(&ddata->lock); - return ret; -} -#endif - -static int lsm303dlh_m_read_multi(struct lsm303dlh_m_data *ddata, u8 reg, - u8 count, u8 *val, char *msg) -{ - int ret = i2c_smbus_read_i2c_block_data(ddata->client, - reg | MULTIPLE_I2C_TR, count, val); - if (ret < 0) - dev_err(&ddata->client->dev, - "i2c_smbus_read_i2c_block_data failed error %d\ - Register (%s)\n", ret, msg); - return ret; -} - -static ssize_t lsm303dlh_m_show_rate(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_m_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->rate >> LSM303DLH_M_CRA_DO_BIT); -} - -/* set lsm303dlh magnetometer bandwidth */ -static ssize_t lsm303dlh_m_store_rate(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_m_data *ddata = platform_get_drvdata(pdev); - unsigned long val; - unsigned char data; - int error; - - error = strict_strtoul(buf, 0, &val); - if (error) - return error; - - mutex_lock(&ddata->lock); - if (ddata->mode == LSM303DLH_M_MODE_SLEEP) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - mutex_unlock(&ddata->lock); - return count; - } - - data = ((val << LSM303DLH_M_CRA_DO_BIT) & LSM303DLH_M_CRA_DO_MASK); - ddata->rate = data; - - error = lsm303dlh_m_write(ddata, CRA_REG_M, data, "SET RATE"); - - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - mutex_unlock(&ddata->lock); - - return count; -} - -static int lsm303dlh_m_xyz_read(struct lsm303dlh_m_data *ddata) -{ - unsigned char xyz_data[6]; - short temp; - int ret = lsm303dlh_m_read_multi(ddata, OUT_X_M, - 6, xyz_data, "OUT_X_M"); - if (ret < 0) - return -EINVAL; - - /* MSB is at lower address */ - ddata->data[0] = (short) - (((xyz_data[0]) << 8) | xyz_data[1]); - ddata->data[1] = (short) - (((xyz_data[2]) << 8) | xyz_data[3]); - ddata->data[2] = (short) - (((xyz_data[4]) << 8) | xyz_data[5]); - - /* check if chip is DHLC */ - if (ddata->pdata.chip_id == LSM303DLHC_CHIP_ID) { - /* - * the out registers are in x, z and y order - * so swap y and z values - */ - temp = ddata->data[1]; - ddata->data[1] = ddata->data[2]; - ddata->data[2] = temp; - } - /* taking orientation of x,y,z axis into account*/ - - ddata->data[ddata->pdata.axis_map_x] = ddata->pdata.negative_x ? - -ddata->data[ddata->pdata.axis_map_x] : - ddata->data[ddata->pdata.axis_map_x]; - ddata->data[ddata->pdata.axis_map_y] = ddata->pdata.negative_y ? - -ddata->data[ddata->pdata.axis_map_y] : - ddata->data[ddata->pdata.axis_map_y]; - ddata->data[ddata->pdata.axis_map_z] = ddata->pdata.negative_z ? - -ddata->data[ddata->pdata.axis_map_z] : - ddata->data[ddata->pdata.axis_map_z]; - - return ret; -} - -static ssize_t lsm303dlh_m_gain(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_m_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%8x:%8x:%8x\n", - ddata->gain[ddata->pdata.axis_map_x], - ddata->gain[ddata->pdata.axis_map_y], - ddata->gain[ddata->pdata.axis_map_z]); -} - -static ssize_t lsm303dlh_m_values(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_m_data *ddata = platform_get_drvdata(pdev); - int ret = 0; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_M_MODE_SLEEP || - ddata->device_status == DEVICE_SUSPENDED) { - mutex_unlock(&ddata->lock); - return ret; - } - - ret = lsm303dlh_m_xyz_read(ddata); - - if (ret < 0) { - mutex_unlock(&ddata->lock); - return -EINVAL; - } - - mutex_unlock(&ddata->lock); - - /* taking orientation of x,y,z axis into account*/ - - return sprintf(buf, "%8x:%8x:%8x\n", - ddata->data[ddata->pdata.axis_map_x], - ddata->data[ddata->pdata.axis_map_y], - ddata->data[ddata->pdata.axis_map_z]); -} - -static int lsm303dlh_m_set_mode(struct lsm303dlh_m_data *ddata, - unsigned char mode) -{ - int ret; - - mode = (mode << LSM303DLH_M_MR_MD_BIT); - - ret = i2c_smbus_write_byte_data(ddata->client, MR_REG_M, mode); - - if (ret < 0) - dev_err(&ddata->client->dev, - "i2c_smbus_write_byte_data failed error %d\ - Register (%s)\n", ret, "MODE CONTROL"); - - return ret; -} - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - -static irqreturn_t lsm303dlh_m_gpio_irq(int irq, void *device_data) -{ - struct lsm303dlh_m_data *ddata = device_data; - int ret; - - ret = lsm303dlh_m_xyz_read(ddata); - - if (ret < 0) { - dev_err(&ddata->client->dev, - "reading data of xyz failed error %d\n", ret); - return IRQ_NONE; - } - - /* taking orientation of x,y,z axis into account*/ - - input_report_abs(ddata->input_dev, ABS_X, - ddata->data[ddata->pdata.axis_map_x]); - input_report_abs(ddata->input_dev, ABS_Y, - ddata->data[ddata->pdata.axis_map_y]); - input_report_abs(ddata->input_dev, ABS_Z, - ddata->data[ddata->pdata.axis_map_z]); - input_sync(ddata->input_dev); - - return IRQ_HANDLED; - -} -#endif - -static ssize_t lsm303dlh_m_show_range(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_m_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->range >> LSM303DLH_M_CRB_GN_BIT); -} - -static ssize_t lsm303dlh_m_store_range(struct device *dev, - struct device_attribute *attr, const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_m_data *ddata = platform_get_drvdata(pdev); - short xy_gain; - short z_gain; - unsigned long range; - int error; - - error = strict_strtoul(buf, 0, &range); - - if (error) - return error; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLH_M_MODE_SLEEP) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - mutex_unlock(&ddata->lock); - return count; - } - - switch (range) { - case LSM303DLH_M_RANGE_1_3G: - xy_gain = XY_GAIN_1_3; - z_gain = Z_GAIN_1_3; - break; - case LSM303DLH_M_RANGE_1_9G: - xy_gain = XY_GAIN_1_9; - z_gain = Z_GAIN_1_9; - break; - case LSM303DLH_M_RANGE_2_5G: - xy_gain = XY_GAIN_2_5; - z_gain = Z_GAIN_2_5; - break; - case LSM303DLH_M_RANGE_4_0G: - xy_gain = XY_GAIN_4_0; - z_gain = Z_GAIN_4_0; - break; - case LSM303DLH_M_RANGE_4_7G: - xy_gain = XY_GAIN_4_7; - z_gain = Z_GAIN_4_7; - break; - case LSM303DLH_M_RANGE_5_6G: - xy_gain = XY_GAIN_5_6; - z_gain = Z_GAIN_5_6; - break; - case LSM303DLH_M_RANGE_8_1G: - xy_gain = XY_GAIN_8_1; - z_gain = Z_GAIN_8_1; - break; - default: - mutex_unlock(&ddata->lock); - return -EINVAL; - } - - ddata->gain[ddata->pdata.axis_map_x] = xy_gain; - ddata->gain[ddata->pdata.axis_map_y] = xy_gain; - ddata->gain[ddata->pdata.axis_map_z] = z_gain; - - range <<= LSM303DLH_M_CRB_GN_BIT; - range &= LSM303DLH_M_CRB_GN_MASK; - - ddata->range = range; - - error = lsm303dlh_m_write(ddata, CRB_REG_M, range, "SET RANGE"); - mutex_unlock(&ddata->lock); - - if (error < 0) - return error; - - return count; -} - -static ssize_t lsm303dlh_m_show_mode(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_m_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->mode); -} - -static ssize_t lsm303dlh_m_store_mode(struct device *dev, - struct device_attribute *attr, const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlh_m_data *ddata = platform_get_drvdata(pdev); - unsigned long mode; - int error; - - error = strict_strtoul(buf, 0, &mode); - if (error) - return error; - - mutex_lock(&ddata->lock); - - if (ddata->device_status == DEVICE_SUSPENDED && - mode == LSM303DLH_M_MODE_SLEEP) { - ddata->mode = (mode >> LSM303DLH_M_MR_MD_BIT); - mutex_unlock(&ddata->lock); - return count; - } - - /* if same mode as existing, return */ - if (ddata->mode == mode) { - mutex_unlock(&ddata->lock); - return count; - } - - /* turn on the supplies if already off */ - if (ddata->mode == LSM303DLH_M_MODE_SLEEP && ddata->regulator - && (ddata->device_status == DEVICE_OFF - || ddata->device_status == DEVICE_SUSPENDED)) { - regulator_enable(ddata->regulator); - ddata->device_status = DEVICE_ON; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - enable_irq(gpio_to_irq(ddata->pdata.irq_m)); -#endif - } - - error = lsm303dlh_m_set_mode(ddata, mode); - - ddata->mode = (mode >> LSM303DLH_M_MR_MD_BIT); - if (error < 0) { - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - mutex_unlock(&ddata->lock); - return error; - } - - if (mode == LSM303DLH_M_MODE_SLEEP) { - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - disable_irq(gpio_to_irq(ddata->pdata.irq_m)); -#endif - - /* - * No need to store context here, it is not like - * suspend/resume but fall back to default values - */ - ddata->rate = LSM303DLH_M_RATE_00_75; - ddata->range = LSM303DLH_M_RANGE_1_3G; - ddata->range <<= LSM303DLH_M_CRB_GN_BIT; - ddata->range &= LSM303DLH_M_CRB_GN_MASK; - ddata->gain[ddata->pdata.axis_map_x] = XY_GAIN_1_3; - ddata->gain[ddata->pdata.axis_map_y] = XY_GAIN_1_3; - ddata->gain[ddata->pdata.axis_map_z] = Z_GAIN_1_3; - - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - } - mutex_unlock(&ddata->lock); - - return count; -} - -static DEVICE_ATTR(gain, S_IRUGO, lsm303dlh_m_gain, NULL); - -static DEVICE_ATTR(data, S_IRUGO, lsm303dlh_m_values, NULL); - -static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, - lsm303dlh_m_show_mode, lsm303dlh_m_store_mode); - -static DEVICE_ATTR(range, S_IWUSR | S_IRUGO, - lsm303dlh_m_show_range, lsm303dlh_m_store_range); - -static DEVICE_ATTR(rate, S_IWUSR | S_IRUGO, - lsm303dlh_m_show_rate, lsm303dlh_m_store_rate); - -static struct attribute *lsm303dlh_m_attributes[] = { - &dev_attr_gain.attr, - &dev_attr_data.attr, - &dev_attr_mode.attr, - &dev_attr_range.attr, - &dev_attr_rate.attr, - NULL -}; - -static const struct attribute_group lsm303dlh_m_attr_group = { - .attrs = lsm303dlh_m_attributes, -}; - -static int __devinit lsm303dlh_m_probe(struct i2c_client *client, - const struct i2c_device_id *id) -{ - int ret; - struct lsm303dlh_m_data *ddata = NULL; - unsigned char version[3]; - - ddata = kzalloc(sizeof(struct lsm303dlh_m_data), GFP_KERNEL); - if (ddata == NULL) { - dev_err(&client->dev, "memory alocation failed\n"); - ret = -ENOMEM; - goto err_alloc; - } - - ddata->client = client; - i2c_set_clientdata(client, ddata); - - /* copy platform specific data */ - memcpy(&ddata->pdata, client->dev.platform_data, sizeof(ddata->pdata)); - - ddata->mode = LSM303DLH_M_MODE_SLEEP; - ddata->rate = LSM303DLH_M_RATE_00_75; - ddata->range = LSM303DLH_M_RANGE_1_3G; - ddata->range <<= LSM303DLH_M_CRB_GN_BIT; - ddata->range &= LSM303DLH_M_CRB_GN_MASK; - ddata->gain[ddata->pdata.axis_map_x] = XY_GAIN_1_3; - ddata->gain[ddata->pdata.axis_map_y] = XY_GAIN_1_3; - ddata->gain[ddata->pdata.axis_map_z] = Z_GAIN_1_3; - ddata->device_status = DEVICE_OFF; - dev_set_name(&client->dev, ddata->pdata.name_m); - - ddata->regulator = regulator_get(&client->dev, "vdd"); - if (IS_ERR(ddata->regulator)) { - dev_err(&client->dev, "failed to get regulator\n"); - ret = PTR_ERR(ddata->regulator); - ddata->regulator = NULL; - goto err_op_failed; - } - - if (ddata->regulator) { - /* - * 0.83 milliamps typical with magnetic sensor setting ODR = - * 7.5 Hz, Accelerometer sensor ODR = 50 Hz. Double for - * safety. - */ - regulator_set_optimum_mode(ddata->regulator, 830 * 2); - regulator_enable(ddata->regulator); - ddata->device_status = DEVICE_ON; - } - - ret = lsm303dlh_m_read_multi(ddata, IRA_REG_M, 3, version, "IRA_REG_M"); - if (ret < 0) - goto exit_free_regulator; - - dev_info(&client->dev, "Magnetometer, ID : %x:%x:%x", - version[0], version[1], version[2]); - - mutex_init(&ddata->lock); - - ret = sysfs_create_group(&client->dev.kobj, &lsm303dlh_m_attr_group); - if (ret) - goto exit_free_regulator; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - - ddata->input_dev = input_allocate_device(); - if (!ddata->input_dev) { - ret = -ENOMEM; - dev_err(&client->dev, "Failed to allocate input device\n"); - goto exit_free_regulator; - } - - set_bit(EV_ABS, ddata->input_dev->evbit); - - /* x-axis acceleration */ - input_set_abs_params(ddata->input_dev, ABS_X, -32768, 32767, 0, 0); - /* y-axis acceleration */ - input_set_abs_params(ddata->input_dev, ABS_Y, -32768, 32767, 0, 0); - /* z-axis acceleration */ - input_set_abs_params(ddata->input_dev, ABS_Z, -32768, 32767, 0, 0); - - ddata->input_dev->name = "magnetometer"; - - ret = input_register_device(ddata->input_dev); - if (ret) { - dev_err(&client->dev, "Unable to register input device: %s\n", - ddata->input_dev->name); - goto err_input_register_failed; - } - - /* register interrupt */ - ret = request_threaded_irq(gpio_to_irq(ddata->pdata.irq_m), NULL, - lsm303dlh_m_gpio_irq, - IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "lsm303dlh_m", - ddata); - if (ret) { - dev_err(&client->dev, "request irq EGPIO_PIN_1 failed\n"); - goto err_input_failed; - } - - disable_irq(gpio_to_irq(ddata->pdata.irq_m)); -#endif - -#ifdef CONFIG_HAS_EARLYSUSPEND - ddata->early_suspend.level = - EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; - ddata->early_suspend.suspend = lsm303dlh_m_early_suspend; - ddata->early_suspend.resume = lsm303dlh_m_late_resume; - register_early_suspend(&ddata->early_suspend); -#endif - - if (ddata->device_status == DEVICE_ON && ddata->regulator) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - - return ret; - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE -err_input_failed: - input_unregister_device(ddata->input_dev); -err_input_register_failed: - input_free_device(ddata->input_dev); -#endif -exit_free_regulator: - if (ddata->device_status == DEVICE_ON && ddata->regulator) { - regulator_disable(ddata->regulator); - regulator_put(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - -err_op_failed: - kfree(ddata); -err_alloc: - dev_err(&client->dev, "lsm303dlh_m_probe failed %x", ret); - return ret; -} - -static int __devexit lsm303dlh_m_remove(struct i2c_client *client) -{ - struct lsm303dlh_m_data *ddata; - - ddata = i2c_get_clientdata(client); - -#ifdef CONFIG_SENSORS_LSM303DLH_INPUT_DEVICE - input_unregister_device(ddata->input_dev); - input_free_device(ddata->input_dev); -#endif - - sysfs_remove_group(&client->dev.kobj, &lsm303dlh_m_attr_group); - - /* safer to make device off */ - if (ddata->mode != LSM303DLH_M_MODE_SLEEP) { - lsm303dlh_m_set_mode(ddata, LSM303DLH_M_MODE_SLEEP); - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - regulator_put(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - } - - i2c_set_clientdata(client, NULL); - kfree(ddata); - - return 0; -} - -#ifndef CONFIG_HAS_EARLYSUSPEND -#ifdef CONFIG_PM -static int lsm303dlh_m_suspend(struct device *dev) -{ - struct lsm303dlh_m_data *ddata; - int ret; - - ddata = dev_get_drvdata(dev); - - ret = lsm303dlh_m_do_suspend(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while suspending the device"); - - return ret; -} - -static int lsm303dlh_m_resume(struct device *dev) -{ - struct lsm303dlh_m_data *ddata; - int ret; - - ddata = dev_get_drvdata(dev); - - ret = lsm303dlh_m_restore(ddata); - - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while resuming the device"); - - return ret; -} -static const struct dev_pm_ops lsm303dlh_m_dev_pm_ops = { - .suspend = lsm303dlh_m_suspend, - .resume = lsm303dlh_m_resume, -}; -#endif -#else -static void lsm303dlh_m_early_suspend(struct early_suspend *data) -{ - struct lsm303dlh_m_data *ddata = - container_of(data, struct lsm303dlh_m_data, early_suspend); - int ret; - - ret = lsm303dlh_m_do_suspend(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while suspending the device"); -} - -static void lsm303dlh_m_late_resume(struct early_suspend *data) -{ - struct lsm303dlh_m_data *ddata = - container_of(data, struct lsm303dlh_m_data, early_suspend); - int ret; - - ret = lsm303dlh_m_restore(ddata); - - if (ret < 0) - dev_err(&ddata->client->dev, - "lsm303dlh_m late resume failed\n"); -} -#endif /* CONFIG_PM */ - -static const struct i2c_device_id lsm303dlh_m_id[] = { - { "lsm303dlh_m", 0 }, - { }, -}; - -static struct i2c_driver lsm303dlh_m_driver = { - .probe = lsm303dlh_m_probe, - .remove = lsm303dlh_m_remove, - .id_table = lsm303dlh_m_id, - .driver = { - .name = "lsm303dlh_m", -#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) - .pm = &lsm303dlh_m_dev_pm_ops, -#endif - }, -}; - -static int __init lsm303dlh_m_init(void) -{ - return i2c_add_driver(&lsm303dlh_m_driver); -} - -static void __exit lsm303dlh_m_exit(void) -{ - i2c_del_driver(&lsm303dlh_m_driver); -} - -module_init(lsm303dlh_m_init); -module_exit(lsm303dlh_m_exit); - -MODULE_DESCRIPTION("lSM303DLH 3-Axis Magnetometer Driver"); -MODULE_LICENSE("GPL"); -MODULE_AUTHOR("STMicroelectronics"); diff --git a/drivers/hwmon/lsm303dlhc_a.c b/drivers/hwmon/lsm303dlhc_a.c deleted file mode 100644 index cf00ce9d50b..00000000000 --- a/drivers/hwmon/lsm303dlhc_a.c +++ /dev/null @@ -1,708 +0,0 @@ -/* - * ST LSM303DLHC 3-Axis Accelerometer Driver - * - * Copyright (C) ST-Ericsson SA 2011 - * Author: Chethan Krishna N for ST-Ericsson - * Licence terms: GNU General Public Licence (GPL) version 2 - */ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#ifdef CONFIG_HAS_EARLYSUSPEND -#include -#endif - -#define WHO_AM_I 0x0F - -/* lsm303dlhc accelerometer registers */ -#define CTRL_REG1 0x20 -#define CTRL_REG2 0x21 -#define CTRL_REG3 0x22 -#define CTRL_REG4 0x23 -#define CTRL_REG5 0x24 -#define CTRL_REG6 0x25 - -/* lsm303dlhc accelerometer defines */ -#define LSM303DLHC_A_MODE_OFF 0x00 -#define LSM303DLHC_A_MODE_ON 0x04 -#define LSM303DLHC_A_MODE_MAX 0x09 -#define LSM303DLHC_A_CR1_MODE_BIT 4 -#define LSM303DLHC_A_CR1_MODE_MASK (0xF << LSM303DLHC_A_CR1_MODE_BIT) - #define LSM303DLHC_A_CR1_AXIS_ENABLE 7 - -/* Range */ -#define LSM303DLHC_A_RANGE_2G 0x00 -#define LSM303DLHC_A_RANGE_4G 0x01 -#define LSM303DLHC_A_RANGE_8G 0x02 -#define LSM303DLHC_A_RANGE_16G 0x03 -#define LSM303DLHC_A_CR4_FS_BIT 4 - -/* Sensitivity adjustment */ -#define SHIFT_ADJ_2G 4 /* 1/16*/ -#define SHIFT_ADJ_4G 3 /* 2/16*/ -#define SHIFT_ADJ_8G 2 /* ~3.9/16*/ -#define SHIFT_ADJ_16G 1 /* ~3.9/16*/ - -#define AXISDATA_REG 0x28 /* axis data */ - -/* lsm303dlh magnetometer registers */ -#define IRA_REG_M 0x0A - -/* multiple byte transfer enable */ -#define MULTIPLE_I2C_TR 0x80 - -/* device status defines */ -#define DEVICE_OFF 0 -#define DEVICE_ON 1 -#define DEVICE_SUSPENDED 2 - -struct lsm303dlhc_a_t { - short x; - short y; - short z; -}; - -/** - * struct lsm303dlhc_a_data - data structure used by lsm303dlhc_a driver - * @client: i2c client - * @lock: mutex lock for sysfs operations - * @data: lsm303dlhc_a_t struct containing x, y and z values - * @pdata: lsm303dlh platform data - * @regulator: regulator - * @range: current range value of accelerometer - * @mode: current mode of operation - * @rate: current sampling rate - * @shift_adjust: current shift adjust value set according to range - * @early_suspend: early suspend structure - * @device_status: device is ON, OFF or SUSPENDED - * @id: accelerometer device id - */ -struct lsm303dlhc_a_data { - struct i2c_client *client; - /* lock for sysfs operations */ - struct mutex lock; - struct lsm303dlhc_a_t data; - struct lsm303dlh_platform_data pdata; - struct regulator *regulator; -#ifdef CONFIG_HAS_EARLYSUSPEND - struct early_suspend early_suspend; -#endif - unsigned char range; - unsigned char mode; - unsigned char rate; - int shift_adjust; - int device_status; - int id; -}; - -#ifdef CONFIG_HAS_EARLYSUSPEND -static void lsm303dlhc_a_early_suspend(struct early_suspend *data); -static void lsm303dlhc_a_late_resume(struct early_suspend *data); -#endif - -static int lsm303dlhc_a_write(struct lsm303dlhc_a_data *ddata, u8 reg, - u8 val, char *msg) -{ - int ret = i2c_smbus_write_byte_data(ddata->client, reg, val); - if (ret < 0) - dev_err(&ddata->client->dev, - "i2c_smbus_write_byte_data failed error %d\ - Register (%s)\n", ret, msg); - return ret; -} - -static int lsm303dlhc_a_read(struct lsm303dlhc_a_data *ddata, u8 reg, char *msg) -{ - int ret = i2c_smbus_read_byte_data(ddata->client, reg); - if (ret < 0) - dev_err(&ddata->client->dev, - "i2c_smbus_read_byte_data failed error %d\ - Register (%s)\n", ret, msg); - return ret; -} - -#if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_PM) -static int lsm303dlhc_a_do_suspend(struct lsm303dlhc_a_data *ddata) -{ - int ret; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLHC_A_MODE_OFF) { - ret = 0; - goto exit; - } - - ret = lsm303dlhc_a_write(ddata, CTRL_REG1, - LSM303DLHC_A_MODE_OFF, "CONTROL"); - - if (ddata->regulator) - regulator_disable(ddata->regulator); - - ddata->device_status = DEVICE_SUSPENDED; - -exit: - mutex_unlock(&ddata->lock); - - return ret; -} - -static int lsm303dlhc_a_restore(struct lsm303dlhc_a_data *ddata) -{ - unsigned char reg; - unsigned char shifted_mode = (ddata->mode << LSM303DLHC_A_CR1_MODE_BIT); - int ret = 0; - - mutex_lock(&ddata->lock); - - if (ddata->device_status == DEVICE_ON) { - mutex_unlock(&ddata->lock); - return 0; - } - - /* in correct mode, no need to change it */ - if (ddata->mode == LSM303DLHC_A_MODE_OFF) { - ddata->device_status = DEVICE_OFF; - goto fail; - } else - ddata->device_status = DEVICE_ON; - - if (ddata->regulator) - regulator_enable(ddata->regulator); - - /* BDU should be enabled by default/recommened */ - reg = ddata->range; - shifted_mode |= LSM303DLHC_A_CR1_AXIS_ENABLE; - - ret = lsm303dlhc_a_write(ddata, CTRL_REG1, shifted_mode, - "CTRL_REG1"); - if (ret < 0) - goto fail; - - ret = lsm303dlhc_a_write(ddata, CTRL_REG4, reg, "CTRL_REG4"); - - if (ret < 0) - goto fail; - - /* write to the boot bit to reboot memory content */ - ret = lsm303dlhc_a_write(ddata, CTRL_REG5, 0x80, "CTRL_REG5"); - - if (ret < 0) - goto fail; - -fail: - if (ret < 0) - dev_err(&ddata->client->dev, - "could not restore the device %d\n", ret); - mutex_unlock(&ddata->lock); - return ret; -} -#endif - -static int lsm303dlhc_a_readdata(struct lsm303dlhc_a_data *ddata) -{ - unsigned char acc_data[6]; - short data[3]; - - int ret = i2c_smbus_read_i2c_block_data(ddata->client, - AXISDATA_REG | MULTIPLE_I2C_TR, 6, acc_data); - if (ret < 0) { - dev_err(&ddata->client->dev, - "i2c_smbus_read_byte_data failed error %d\ - Register AXISDATA_REG \n", ret); - return ret; - } - - data[0] = (short) (((acc_data[1]) << 8) | acc_data[0]); - data[1] = (short) (((acc_data[3]) << 8) | acc_data[2]); - data[2] = (short) (((acc_data[5]) << 8) | acc_data[4]); - - data[0] >>= ddata->shift_adjust; - data[1] >>= ddata->shift_adjust; - data[2] >>= ddata->shift_adjust; - - /* taking position and orientation of x,y,z axis into account*/ - - data[ddata->pdata.axis_map_x] = ddata->pdata.negative_x ? - -data[ddata->pdata.axis_map_x] : data[ddata->pdata.axis_map_x]; - data[ddata->pdata.axis_map_y] = ddata->pdata.negative_y ? - -data[ddata->pdata.axis_map_y] : data[ddata->pdata.axis_map_y]; - data[ddata->pdata.axis_map_z] = ddata->pdata.negative_z ? - -data[ddata->pdata.axis_map_z] : data[ddata->pdata.axis_map_z]; - - ddata->data.x = data[ddata->pdata.axis_map_x]; - ddata->data.y = data[ddata->pdata.axis_map_y]; - ddata->data.z = data[ddata->pdata.axis_map_z]; - - return ret; -} - -static ssize_t lsm303dlhc_a_show_data(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlhc_a_data *ddata = platform_get_drvdata(pdev); - int ret = 0; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLHC_A_MODE_OFF || - ddata->device_status == DEVICE_SUSPENDED) { - mutex_unlock(&ddata->lock); - return ret; - } - - ret = lsm303dlhc_a_readdata(ddata); - - if (ret < 0) { - mutex_unlock(&ddata->lock); - return ret; - } - - mutex_unlock(&ddata->lock); - - return sprintf(buf, "%8x:%8x:%8x\n", ddata->data.x, ddata->data.y, - ddata->data.z); -} - -static ssize_t lsm303dlhc_a_show_range(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlhc_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->range >> LSM303DLHC_A_CR4_FS_BIT); -} - -static ssize_t lsm303dlhc_a_store_range(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlhc_a_data *ddata = platform_get_drvdata(pdev); - long val; - int error; - - error = strict_strtol(buf, 0, &val); - if (error) - return error; - - if (val < LSM303DLHC_A_RANGE_2G || val > LSM303DLHC_A_RANGE_16G) - return -EINVAL; - - mutex_lock(&ddata->lock); - - if (ddata->mode == LSM303DLHC_A_MODE_OFF) { - dev_info(&ddata->client->dev, - "device is switched off,make it ON using MODE"); - mutex_unlock(&ddata->lock); - return count; - } - - ddata->range = val; - ddata->range <<= LSM303DLHC_A_CR4_FS_BIT; - - error = lsm303dlhc_a_write(ddata, CTRL_REG4, ddata->range, - "CTRL_REG4"); - if (error < 0) { - mutex_unlock(&ddata->lock); - return error; - } - - switch (val) { - case LSM303DLHC_A_RANGE_2G: - ddata->shift_adjust = SHIFT_ADJ_2G; - break; - case LSM303DLHC_A_RANGE_4G: - ddata->shift_adjust = SHIFT_ADJ_4G; - break; - case LSM303DLHC_A_RANGE_8G: - ddata->shift_adjust = SHIFT_ADJ_8G; - break; - case LSM303DLHC_A_RANGE_16G: - ddata->shift_adjust = SHIFT_ADJ_16G; - break; - default: - mutex_unlock(&ddata->lock); - return -EINVAL; - } - - mutex_unlock(&ddata->lock); - - return count; -} - -static ssize_t lsm303dlhc_a_show_mode(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlhc_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->mode); -} - -static ssize_t lsm303dlhc_a_store_mode(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlhc_a_data *ddata = platform_get_drvdata(pdev); - long val; - unsigned char data; - int error; - bool set_boot_bit = false; - - error = strict_strtol(buf, 0, &val); - if (error) - return error; - - mutex_lock(&ddata->lock); - - /* not in correct range */ - - if (val < LSM303DLHC_A_MODE_OFF || val > LSM303DLHC_A_MODE_MAX) { - mutex_unlock(&ddata->lock); - return -EINVAL; - } - - if (ddata->device_status == DEVICE_SUSPENDED) { - if (val == LSM303DLHC_A_MODE_OFF) { - ddata->mode = val; - mutex_unlock(&ddata->lock); - return count; - } else { - /* device is turning on after suspend, reset memory */ - set_boot_bit = true; - } - } - - /* if same mode as existing, return */ - if (ddata->mode == val) { - mutex_unlock(&ddata->lock); - return count; - } - - /* turn on the supplies if already off */ - if (ddata->regulator && ddata->mode == LSM303DLHC_A_MODE_OFF - && (ddata->device_status == DEVICE_OFF - || ddata->device_status == DEVICE_SUSPENDED)) { - regulator_enable(ddata->regulator); - ddata->device_status = DEVICE_ON; - } - - data = lsm303dlhc_a_read(ddata, CTRL_REG1, "CTRL_REG1"); - - /* - * If chip doesn't get reset during suspend/resume, - * x,y and z axis bits are getting cleared,so set - * these bits to get x,y,z data. - */ - data |= LSM303DLHC_A_CR1_AXIS_ENABLE; - - data &= ~LSM303DLHC_A_CR1_MODE_MASK; - - ddata->mode = val; - - data |= ((val << LSM303DLHC_A_CR1_MODE_BIT) - & LSM303DLHC_A_CR1_MODE_MASK); - - error = lsm303dlhc_a_write(ddata, CTRL_REG1, data, "CTRL_REG1"); - if (error < 0) { - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - mutex_unlock(&ddata->lock); - return error; - } - - /* - * Power on request when device is in suspended state - * write to the boot bit in CTRL_REG2 to reboot memory content - * and ensure correct device behavior after it resumes - */ - if (set_boot_bit) { - error = lsm303dlhc_a_write(ddata, CTRL_REG5, 0x80, "CTRL_REG5"); - if (error < 0) { - if (ddata->regulator && - ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - mutex_unlock(&ddata->lock); - return error; - } - } - - if (val == LSM303DLHC_A_MODE_OFF) { - - /* - * No need to store context here - * it is not like suspend/resume - * but fall back to default values - */ - ddata->range = LSM303DLHC_A_RANGE_2G; - ddata->shift_adjust = SHIFT_ADJ_2G; - - if (ddata->regulator && ddata->device_status == DEVICE_ON) { - regulator_disable(ddata->regulator); - ddata->device_status = DEVICE_OFF; - } - } - mutex_unlock(&ddata->lock); - - return count; -} - -static ssize_t lsm303dlhc_a_show_id(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct platform_device *pdev = to_platform_device(dev); - struct lsm303dlhc_a_data *ddata = platform_get_drvdata(pdev); - - return sprintf(buf, "%d\n", ddata->id); -} - -static DEVICE_ATTR(id, S_IRUGO, lsm303dlhc_a_show_id, NULL); - -static DEVICE_ATTR(data, S_IRUGO, lsm303dlhc_a_show_data, NULL); - -static DEVICE_ATTR(range, S_IWUSR | S_IRUGO, - lsm303dlhc_a_show_range, lsm303dlhc_a_store_range); - -static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, - lsm303dlhc_a_show_mode, lsm303dlhc_a_store_mode); - -static struct attribute *lsm303dlhc_a_attributes[] = { - &dev_attr_data.attr, - &dev_attr_range.attr, - &dev_attr_mode.attr, - &dev_attr_id.attr, - NULL -}; - -static const struct attribute_group lsm303dlhc_a_attr_group = { - .attrs = lsm303dlhc_a_attributes, -}; - -static int __devinit lsm303dlhc_a_probe(struct i2c_client *client, - const struct i2c_device_id *id) -{ - int ret; - struct lsm303dlhc_a_data *adata = NULL; - - adata = kzalloc(sizeof(struct lsm303dlhc_a_data), GFP_KERNEL); - if (adata == NULL) { - dev_err(&client->dev, "memory alocation failed\n"); - ret = -ENOMEM; - goto err_alloc; - } - - adata->client = client; - i2c_set_clientdata(client, adata); - - /* copy platform specific data */ - memcpy(&adata->pdata, client->dev.platform_data, sizeof(adata->pdata)); - adata->mode = LSM303DLHC_A_MODE_OFF; - adata->range = LSM303DLHC_A_RANGE_2G; - adata->shift_adjust = SHIFT_ADJ_2G; - adata->device_status = DEVICE_OFF; - dev_set_name(&client->dev, adata->pdata.name_a); - - adata->regulator = regulator_get(&client->dev, "vdd"); - if (IS_ERR(adata->regulator)) { - dev_err(&client->dev, "failed to get regulator\n"); - ret = PTR_ERR(adata->regulator); - adata->regulator = NULL; - goto err_op_failed; - } - - if (adata->regulator) { - /* - * 130 microamps typical with magnetic sensor setting ODR = 7.5 - * Hz, Accelerometer sensor ODR = 50 Hz. Double for safety. - */ - regulator_set_optimum_mode(adata->regulator, 130 * 2); - regulator_enable(adata->regulator); - adata->device_status = DEVICE_ON; - } - - ret = lsm303dlhc_a_read(adata, WHO_AM_I, "WHO_AM_I"); - if (ret < 0) - goto exit_free_regulator; - - dev_info(&client->dev, "3-Axis Accelerometer, ID : %d\n", - ret); - adata->id = ret; - - mutex_init(&adata->lock); - - ret = sysfs_create_group(&client->dev.kobj, &lsm303dlhc_a_attr_group); - if (ret) - goto exit_free_regulator; - -#ifdef CONFIG_HAS_EARLYSUSPEND - adata->early_suspend.level = - EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; - adata->early_suspend.suspend = lsm303dlhc_a_early_suspend; - adata->early_suspend.resume = lsm303dlhc_a_late_resume; - register_early_suspend(&adata->early_suspend); -#endif - - if (adata->device_status == DEVICE_ON && adata->regulator) { - regulator_disable(adata->regulator); - adata->device_status = DEVICE_OFF; - } - - return ret; - -exit_free_regulator: - if (adata->device_status == DEVICE_ON && adata->regulator) { - regulator_disable(adata->regulator); - regulator_put(adata->regulator); - adata->device_status = DEVICE_OFF; - } - -err_op_failed: - kfree(adata); -err_alloc: - dev_err(&client->dev, "probe function fails %x", ret); - return ret; -} - -static int __devexit lsm303dlhc_a_remove(struct i2c_client *client) -{ - int ret; - struct lsm303dlhc_a_data *adata; - - adata = i2c_get_clientdata(client); - sysfs_remove_group(&client->dev.kobj, &lsm303dlhc_a_attr_group); - - /* safer to make device off */ - if (adata->mode != LSM303DLHC_A_MODE_OFF) { - ret = lsm303dlhc_a_write(adata, CTRL_REG1, 0, "CONTROL"); - - if (ret < 0) { - dev_err(&client->dev, - "could not turn off the device %d", - ret); - return ret; - } - - if (adata->regulator && adata->device_status == DEVICE_ON) { - regulator_disable(adata->regulator); - regulator_put(adata->regulator); - adata->device_status = DEVICE_OFF; - } - } - - i2c_set_clientdata(client, NULL); - kfree(adata); - - return 0; -} - -#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) -static int lsm303dlhc_a_suspend(struct device *dev) -{ - struct lsm303dlhc_a_data *ddata; - int ret; - - ddata = dev_get_drvdata(dev); - - ret = lsm303dlhc_a_do_suspend(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while suspending the device"); - - return ret; -} - -static int lsm303dlhc_a_resume(struct device *dev) -{ - struct lsm303dlhc_a_data *ddata; - int ret; - - ddata = dev_get_drvdata(dev); - - ret = lsm303dlhc_a_restore(ddata); - - if (ret < 0) - dev_err(&ddata->client->dev, - "Error while resuming the device"); - - return ret; -} -static const struct dev_pm_ops lsm303dlhc_a_dev_pm_ops = { - .suspend = lsm303dlhc_a_suspend, - .resume = lsm303dlhc_a_resume, -}; -#else -static void lsm303dlhc_a_early_suspend(struct early_suspend *data) -{ - struct lsm303dlhc_a_data *ddata = - container_of(data, struct lsm303dlhc_a_data, early_suspend); - int ret; - - ret = lsm303dlhc_a_do_suspend(ddata); -} - -static void lsm303dlhc_a_late_resume(struct early_suspend *data) -{ - struct lsm303dlhc_a_data *ddata = - container_of(data, struct lsm303dlhc_a_data, early_suspend); - int ret; - - ret = lsm303dlhc_a_restore(ddata); - if (ret < 0) - dev_err(&ddata->client->dev, - "lsm303dlhc_a late resume failed\n"); -} -#endif /* CONFIG_PM */ - -static const struct i2c_device_id lsm303dlhc_a_id[] = { - { "lsm303dlhc_a", 0 }, - { }, -}; - -static struct i2c_driver lsm303dlhc_a_driver = { - .probe = lsm303dlhc_a_probe, - .remove = lsm303dlhc_a_remove, - .id_table = lsm303dlhc_a_id, - .driver = { - .name = "lsm303dlhc_a", - #if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) - .pm = &lsm303dlhc_a_dev_pm_ops, - #endif - }, -}; - -static int __init lsm303dlhc_a_init(void) -{ - return i2c_add_driver(&lsm303dlhc_a_driver); -} - -static void __exit lsm303dlhc_a_exit(void) -{ - i2c_del_driver(&lsm303dlhc_a_driver); -} - -module_init(lsm303dlhc_a_init) -module_exit(lsm303dlhc_a_exit) - -MODULE_DESCRIPTION("lSM303DLH 3-Axis Accelerometer Driver"); -MODULE_LICENSE("GPL"); -MODULE_AUTHOR("STMicroelectronics"); diff --git a/drivers/staging/iio/accel/Kconfig b/drivers/staging/iio/accel/Kconfig index 5ab71670b70..552bd0b2422 100644 --- a/drivers/staging/iio/accel/Kconfig +++ b/drivers/staging/iio/accel/Kconfig @@ -62,6 +62,28 @@ config KXSD9 Say yes here to build support for the Kionix KXSD9 accelerometer. Currently this only supports the device via an SPI interface. +config SENSORS_LSM303DLH + tristate "STMicroelectronics LSM303DLH 3-Axis Accelerometer" + depends on I2C + default n + help + Say Y here to add support for the STMicroelectronics + LSM303DLH 3-Axis Accelerometer. + + To compile this driver as a module, choose M here: the module + will be called lsm303dlh_a. + +config SENSORS_LSM303DLHC + tristate "STMicroelectronics LSM303DLHC 3-Axis Accelerometer" + depends on I2C + default n + help + Say Y here to add support for the STMicroelectronics + LSM303DLHC 3-Axis Accelerometer. + + To compile this driver as a module, choose M here: the module + will be called lsm303dlhc_a. + config LIS3L02DQ tristate "ST Microelectronics LIS3L02DQ Accelerometer Driver" depends on SPI diff --git a/drivers/staging/iio/accel/Makefile b/drivers/staging/iio/accel/Makefile index 95c66661e70..d4506a95f5c 100644 --- a/drivers/staging/iio/accel/Makefile +++ b/drivers/staging/iio/accel/Makefile @@ -33,3 +33,6 @@ obj-$(CONFIG_LIS3L02DQ) += lis3l02dq.o sca3000-y := sca3000_core.o sca3000_ring.o obj-$(CONFIG_SCA3000) += sca3000.o + +obj-$(CONFIG_SENSORS_LSM303DLH) += lsm303dlh_core.o +obj-$(CONFIG_SENSORS_LSM303DLHC) += lsm303dlhc_core.o diff --git a/drivers/staging/iio/accel/lsm303dlh_core.c b/drivers/staging/iio/accel/lsm303dlh_core.c new file mode 100644 index 00000000000..9c77073b7cb --- /dev/null +++ b/drivers/staging/iio/accel/lsm303dlh_core.c @@ -0,0 +1,941 @@ +/* + * Copyright (C) ST-Ericsson SA 2012 + * License Terms: GNU General Public License, version 2 + * + * Mostly this accelerometer device is a copy of magnetometer + * driver lsm303dlh or viceversa, so the code is mostly based + * on lsm303dlh driver. + * + * Author: Naga Radhesh Y + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef CONFIG_HAS_EARLYSUSPEND +#include +#endif +#include "../iio.h" +#include "../sysfs.h" + +/* Idenitification register */ +#define LSM303DLH_A_CHIP_ID 0x0F +/* control register1 */ +#define LSM303DLH_A_CTRL_REG1_A 0x20 +/* control register2 */ +#define LSM303DLH_A_CTRL_REG2_A 0x21 +/* control register3 */ +#define LSM303DLH_A_CTRL_REG3_A 0x22 +/* control register4 */ +#define LSM303DLH_A_CTRL_REG4_A 0x23 +/* control register5 */ +#define LSM303DLH_A_CTRL_REG5_A 0x24 +/* data output X register */ +#define LSM303DLH_A_OUT_X_L_A 0x28 +/* data output Y register */ +#define LSM303DLH_A_OUT_Y_L_A 0x2A +/* data output Z register */ +#define LSM303DLH_A_OUT_Z_L_A 0x2C +/* status register */ +#define LSM303DLH_A_STATUS_REG_A 0x27 + +/* sensitivity adjustment */ +#define LSM303DLH_A_SHIFT_ADJ_2G 4 /* 1/16*/ +#define LSM303DLH_A_SHIFT_ADJ_4G 3 /* 2/16*/ +#define LSM303DLH_A_SHIFT_ADJ_8G 2 /* ~3.9/16*/ + +/* control register 1, Mode selection */ +#define LSM303DLH_A_CR1_PM_BIT 5 +#define LSM303DLH_A_CR1_PM_MASK (0x7 << LSM303DLH_A_CR1_PM_BIT) +/* control register 1, Data Rate */ +#define LSM303DLH_A_CR1_DR_BIT 3 +#define LSM303DLH_A_CR1_DR_MASK (0x3 << LSM303DLH_A_CR1_DR_BIT) +/* control register 1, x,y,z enable bits */ +#define LSM303DLH_A_CR1_EN_BIT 0 +#define LSM303DLH_A_CR1_EN_MASK (0x7 << LSM303DLH_A_CR1_EN_BIT) +#define LSM303DLH_A_CR1_AXIS_ENABLE 7 + +/* control register 2, Re-Boot Memory */ +#define LSM303DLH_A_CR2_BOOT_ENABLE 0x80 + +/* control register 4, self test */ +#define LSM303DLH_A_CR4_ST_BIT 1 +#define LSM303DLH_A_CR4_ST_MASK (0x1 << LSM303DLH_A_CR4_ST_BIT) +/* control register 4, self test sign */ +#define LSM303DLH_A_CR4_STS_BIT 3 +#define LSM303DLH_A_CR4_STS_MASK (0x1 << LSM303DLH_A_CR4_STS_BIT) +/* control register 4, full scale */ +#define LSM303DLH_A_CR4_FS_BIT 4 +#define LSM303DLH_A_CR4_FS_MASK (0x3 << LSM303DLH_A_CR4_FS_BIT) +/* control register 4, endianness */ +#define LSM303DLH_A_CR4_BLE_BIT 6 +#define LSM303DLH_A_CR4_BLE_MASK (0x1 << LSM303DLH_A_CR4_BLE_BIT) +/* control register 4, Block data update */ +#define LSM303DLH_A_CR4_BDU_BIT 7 +#define LSM303DLH_A_CR4_BDU_MASK (0x1 << LSM303DLH_A_CR4_BDU_BIT) + +/* Accelerometer operating mode */ +#define LSM303DLH_A_MODE_OFF 0x00 +#define LSM303DLH_A_MODE_NORMAL 0x01 +#define LSM303DLH_A_MODE_LP_HALF 0x02 +#define LSM303DLH_A_MODE_LP_1 0x03 +#define LSM303DLH_A_MODE_LP_2 0x04 +#define LSM303DLH_A_MODE_LP_5 0x05 +#define LSM303DLH_A_MODE_LP_10 0x06 + +/* + * CTRL_REG1_A register rate settings + * + * DR1 DR0 Output data rate[Hz] + * 0 0 50 + * 0 1 100 + * 1 0 400 + * 1 1 1000 + */ +#define LSM303DLH_A_RATE_50 0x00 +#define LSM303DLH_A_RATE_100 0x01 +#define LSM303DLH_A_RATE_400 0x02 +#define LSM303DLH_A_RATE_1000 0x03 + +/* + * CTRL_REG4_A register range settings + * + * FS1 FS0 FUll scale range + * 0 0 2g + * 0 1 4g + * 1 0 Not used + * 1 1 8g + */ +#define LSM303DLH_A_RANGE_2G 0x00 +#define LSM303DLH_A_RANGE_4G 0x01 +#define LSM303DLH_A_RANGE_8G 0x03 + +/* device status defines */ +#define LSM303DLH_A_DEVICE_OFF 0 +#define LSM303DLH_A_DEVICE_ON 1 +#define LSM303DLH_A_DEVICE_SUSPENDED 2 + +/* status register */ +#define LSM303DLH_A_SR_REG_A 0x27 +/* status register, ready */ +#define LSM303DLH_A_XYZ_DATA_RDY 0x08 +#define LSM303DLH_A_XYZ_DATA_RDY_BIT 3 +#define LSM303DLH_A_XYZ_DATA_RDY_MASK (0x1 << LSM303DLH_A_XYZ_DATA_RDY_BIT) + +/* Multiple byte transfer enable */ +#define MULTIPLE_I2C_TR 0x80 + +/* + * struct lsm303dlh_a_data - data structure used by lsm303dlh_a driver + * @client: i2c client + * @indio_dev: iio device structure + * @attr: device attributes + * @lock: mutex lock for sysfs operations + * @regulator: regulator + * @early_suspend: early suspend structure + * @pdata: lsm303dlh platform data + * @mode: current mode of operation + * @rate: current sampling rate + * @range: current range value of accelerometer + * @shift_adjust: output bit shift + * @device_status: device is ON, OFF or SUSPENDED + */ + +struct lsm303dlh_a_data { + struct i2c_client *client; + struct mutex lock; + struct regulator *regulator; + struct lsm303dlh_platform_data *pdata; +#ifdef CONFIG_HAS_EARLYSUSPEND + struct early_suspend early_suspend; +#endif + short data[3]; + u8 mode; + u8 rate; + u8 range; + int shift_adjust; + int device_status; +}; + +#ifdef CONFIG_HAS_EARLYSUSPEND +static void lsm303dlh_a_early_suspend(struct early_suspend *data); +static void lsm303dlh_a_late_resume(struct early_suspend *data); +#endif + +static inline int is_device_on(struct lsm303dlh_a_data *data) +{ + struct i2c_client *client = data->client; + /* * Perform read/write operation only when device is active */ + if (data->device_status != LSM303DLH_A_DEVICE_ON) { + dev_err(&client->dev, + "device is switched off, make it on using mode"); + return -EINVAL; + } + + return 0; +} + +/* To disable regulator and status */ +static int lsm303dlh_a_disable(struct lsm303dlh_a_data *data) +{ + data->device_status = LSM303DLH_A_DEVICE_OFF; + + regulator_disable(data->regulator); + + return 0; +} + +/* To enable regulator and status */ +static int lsm303dlh_a_enable(struct lsm303dlh_a_data *data) +{ + data->device_status = LSM303DLH_A_DEVICE_ON; + + regulator_enable(data->regulator); + + return 0; +} + +static s32 lsm303dlh_a_setbootbit(struct i2c_client *client, u8 reg_val) +{ + /* write to the boot bit to reboot memory content */ + return i2c_smbus_write_byte_data(client, + LSM303DLH_A_CTRL_REG2_A, reg_val); + +} + +static s32 lsm303dlh_a_set_mode(struct i2c_client *client, u8 mode) +{ + int reg_val; + + if (mode > LSM303DLH_A_MODE_LP_10) { + dev_err(&client->dev, "given mode not supported\n"); + return -EINVAL; + } + + reg_val = i2c_smbus_read_byte_data(client, LSM303DLH_A_CTRL_REG1_A); + + reg_val |= LSM303DLH_A_CR1_AXIS_ENABLE; + reg_val &= ~LSM303DLH_A_CR1_PM_MASK; + + reg_val |= ((mode << LSM303DLH_A_CR1_PM_BIT) & LSM303DLH_A_CR1_PM_MASK); + + /* the upper three bits indicates the accelerometer sensor mode */ + return i2c_smbus_write_byte_data(client, + LSM303DLH_A_CTRL_REG1_A, reg_val); +} + +static s32 lsm303dlh_a_set_rate(struct i2c_client *client, u8 rate) +{ + int reg_val; + + if (rate > LSM303DLH_A_RATE_1000) { + dev_err(&client->dev, "given rate not supported\n"); + return -EINVAL; + } + + reg_val = i2c_smbus_read_byte_data(client, LSM303DLH_A_CTRL_REG1_A); + + reg_val &= ~LSM303DLH_A_CR1_DR_MASK; + + reg_val |= ((rate << LSM303DLH_A_CR1_DR_BIT) & LSM303DLH_A_CR1_DR_MASK); + + /* 3rd and 4th bits indicate rate of accelerometer */ + return i2c_smbus_write_byte_data(client, + LSM303DLH_A_CTRL_REG1_A, reg_val); +} + +static s32 lsm303dlh_a_set_range(struct i2c_client *client, u8 range) +{ + int reg_val; + + if (range > LSM303DLH_A_RANGE_8G) { + dev_err(&client->dev, "given range not supported\n"); + return -EINVAL; + } + + reg_val = (range << LSM303DLH_A_CR4_FS_BIT); + + /* + * Block mode update is recommended for not + * ending up reading different values + */ + reg_val |= LSM303DLH_A_CR4_BDU_MASK; + + /* 4th and 5th bits indicate range of accelerometer */ + return i2c_smbus_write_byte_data(client, + LSM303DLH_A_CTRL_REG4_A, reg_val); +} + +static s32 lsm303dlh_a_set_shift(struct lsm303dlh_a_data *data, u8 range) +{ + int ret = 0; + struct i2c_client *client = data->client; + + switch (range) { + case LSM303DLH_A_RANGE_2G: + data->shift_adjust = LSM303DLH_A_SHIFT_ADJ_2G; + break; + case LSM303DLH_A_RANGE_4G: + data->shift_adjust = LSM303DLH_A_SHIFT_ADJ_4G; + break; + case LSM303DLH_A_RANGE_8G: + data->shift_adjust = LSM303DLH_A_SHIFT_ADJ_8G; + break; + default: + dev_err(&client->dev, "Invalid range %d\n", range); + ret = -EINVAL; + } + return ret; +} + +/* + * To read output x/y/z data register, + * in this case x,y and z are not + * mapped w.r.t board orientation. + * Reading just raw data from device + */ +static ssize_t lsm303dlh_a_xyz_read(struct iio_dev *indio_dev, + int address, + int *buf) +{ + + struct lsm303dlh_a_data *data = iio_priv(indio_dev); + int lsb , msb; + int ret; + s16 val; + + /* Perform read/write operation, only when device is active */ + ret = is_device_on(data); + if (ret) + return -EINVAL; + + mutex_lock(&data->lock); + + ret = i2c_smbus_read_byte_data(data->client, LSM303DLH_A_SR_REG_A); + + /* wait till data is written to all six registers */ + while (!(ret & LSM303DLH_A_XYZ_DATA_RDY_MASK)) + ret = i2c_smbus_read_byte_data(data->client, + LSM303DLH_A_SR_REG_A); + + lsb = i2c_smbus_read_byte_data(data->client, address); + if (lsb < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + msb = i2c_smbus_read_byte_data(data->client, (address + 1)); + if (msb < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + val = ((msb << 8) | lsb); + + + val >>= data->shift_adjust; + *buf = (s16)val; + mutex_unlock(&data->lock); + + return IIO_VAL_INT; +} + +/* + * To read output x,y,z data register. After reading change x,y and z values + * w.r.t the orientation of the device. + */ +static ssize_t lsm303dlh_a_readdata(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlh_a_data *data = iio_priv(dev_get_drvdata(dev)); + struct lsm303dlh_platform_data *pdata = data->pdata; + struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); + u8 map_x = pdata->axis_map_x; + u8 map_y = pdata->axis_map_y; + u8 map_z = pdata->axis_map_z; + int ret; + unsigned char accel_data[6]; + s16 val[3]; + + /* + * Perform read/write operation, only when device is active + */ + ret = is_device_on(data); + if (ret) + return -EINVAL; + + mutex_lock(&data->lock); + + ret = i2c_smbus_read_byte_data(data->client, LSM303DLH_A_SR_REG_A); + /* wait till data is written to all six registers */ + while (!((ret & LSM303DLH_A_XYZ_DATA_RDY_MASK))) + ret = i2c_smbus_read_byte_data(data->client, + LSM303DLH_A_SR_REG_A); + + ret = i2c_smbus_read_i2c_block_data(data->client, + this_attr->address | MULTIPLE_I2C_TR, 6, accel_data); + + if (ret < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + + + /* MSB is at lower address */ + val[0] = (s16) + (((accel_data[1]) << 8) | accel_data[0]); + val[1] = (s16) + (((accel_data[3]) << 8) | accel_data[2]); + val[2] = (s16) + (((accel_data[5]) << 8) | accel_data[4]); + + val[0] >>= data->shift_adjust; + val[1] >>= data->shift_adjust; + val[2] >>= data->shift_adjust; + + /* modify the x,y and z values w.r.t orientation of device*/ + if (pdata->negative_x) + val[map_x] = -val[map_x]; + if (pdata->negative_y) + val[map_y] = -val[map_y]; + if (pdata->negative_z) + val[map_z] = -val[map_z]; + + mutex_unlock(&data->lock); + + return sprintf(buf, "%d:%d:%d:%lld\n", val[map_x], val[map_y], + val[map_z], iio_get_time_ns()); +} + +static ssize_t show_chip_id(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlh_a_data *data = iio_priv(dev_get_drvdata(dev)); + + return sprintf(buf, "%d\n", data->pdata->chip_id); +} + +static ssize_t show_operating_mode(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlh_a_data *data = iio_priv(dev_get_drvdata(dev)); + + return sprintf(buf, "%d\n", data->mode); +} + +static ssize_t set_operating_mode(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct lsm303dlh_a_data *data = iio_priv(dev_get_drvdata(dev)); + struct i2c_client *client = data->client; + int error; + unsigned long mode = 0; + bool set_boot_bit = false; + + mutex_lock(&data->lock); + + error = kstrtoul(buf, 10, &mode); + if (error) { + count = error; + goto exit; + } + + if (mode > LSM303DLH_A_MODE_LP_10) { + dev_err(&client->dev, "trying to set invalid mode\n"); + count = -EINVAL; + goto exit; + } + + /* + * If device is drived to sleep mode in suspend, update mode + * and return + */ + if (data->device_status == LSM303DLH_A_DEVICE_SUSPENDED && + mode == LSM303DLH_A_MODE_OFF) { + data->mode = mode; + goto exit; + } + + /* if same mode as existing, return */ + if (data->mode == mode) + goto exit; + + /* + * set boot bit when device comes from suspend state + * to ensure correct device behavior after it resumes + */ + if (data->device_status == LSM303DLH_A_DEVICE_SUSPENDED) + set_boot_bit = true; + + /* Enable the regulator if it is not turned ON earlier */ + if (data->device_status == LSM303DLH_A_DEVICE_OFF || + data->device_status == LSM303DLH_A_DEVICE_SUSPENDED) + lsm303dlh_a_enable(data); + + dev_dbg(dev, "set operating mode to %lu\n", mode); + error = lsm303dlh_a_set_mode(client, mode); + if (error < 0) { + dev_err(&client->dev, "Error in setting the mode\n"); + count = -EINVAL; + goto exit; + } + + data->mode = mode; + + if (set_boot_bit) { + /* set boot bit to reboot memory content */ + lsm303dlh_a_setbootbit(client, LSM303DLH_A_CR2_BOOT_ENABLE); + } + + /* If mode is OFF then disable the regulator */ + if (data->mode == LSM303DLH_A_MODE_OFF) { + /* fall back to default values */ + data->rate = LSM303DLH_A_RATE_50; + data->range = LSM303DLH_A_RANGE_2G; + data->shift_adjust = LSM303DLH_A_SHIFT_ADJ_2G; + lsm303dlh_a_disable(data); + } +exit: + mutex_unlock(&data->lock); + return count; +} + +static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("50 100 400 1000"); + +static ssize_t set_sampling_frequency(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct lsm303dlh_a_data *data = iio_priv(dev_get_drvdata(dev)); + struct i2c_client *client = data->client; + unsigned long rate = 0; + int err; + + /* Perform read/write operation, only when device is active */ + err = is_device_on(data); + if (err) + return -EINVAL; + + if (strncmp(buf, "50" , 2) == 0) + rate = LSM303DLH_A_RATE_50; + + else if (strncmp(buf, "400" , 3) == 0) + rate = LSM303DLH_A_RATE_400; + + else if (strncmp(buf, "1000" , 4) == 0) + rate = LSM303DLH_A_RATE_1000; + + else if (strncmp(buf, "100" , 3) == 0) + rate = LSM303DLH_A_RATE_100; + else + return -EINVAL; + + mutex_lock(&data->lock); + + if (lsm303dlh_a_set_rate(client, rate)) { + dev_err(&client->dev, "set rate failed\n"); + count = -EINVAL; + goto exit; + } + data->rate = rate; + +exit: + mutex_unlock(&data->lock); + return count; +} + +/* sampling frequency - output rate in Hz */ +static const char * const reg_to_rate[] = { + "50", + "100", + "400", + "1000" +}; + +static ssize_t show_sampling_frequency(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlh_a_data *data = iio_priv(dev_get_drvdata(dev)); + + return sprintf(buf, "%s\n", reg_to_rate[data->rate]); +} + +static IIO_CONST_ATTR(accel_xyz_scale_available, "2, 4, 8"); + +static const int xyz_to_scale[] = { + 2, 4, 8 +}; + +static const char const scale_to_range[] = { + LSM303DLH_A_RANGE_2G, + LSM303DLH_A_RANGE_4G, + LSM303DLH_A_RANGE_8G, +}; + +static int lsm303dlh_a_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, + long mask) +{ + struct lsm303dlh_a_data *data = iio_priv(indio_dev); + int ret = -EINVAL, i; + bool flag = false; + char end; + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + ret = is_device_on(data); + if (ret) + return -EINVAL; + mutex_lock(&data->lock); + end = ARRAY_SIZE(xyz_to_scale); + for (i = 0; i < end; i++) { + if (val == xyz_to_scale[i]) { + flag = true; + break; + } + } + if (flag) { + ret = lsm303dlh_a_set_range(data->client, + scale_to_range[i]); + if (ret < 0) { + mutex_unlock(&data->lock); + return -EINVAL; + } + ret = lsm303dlh_a_set_shift(data, scale_to_range[i]); + data->range = i; + } + mutex_unlock(&data->lock); + break; + default: + break; + } + return ret; +} + +static int lsm303dlh_a_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct lsm303dlh_a_data *data = iio_priv(indio_dev); + + switch (mask) { + case 0: + return lsm303dlh_a_xyz_read(indio_dev, + chan->address, val); + + case IIO_CHAN_INFO_SCALE: + *val = xyz_to_scale[data->range]; + return IIO_VAL_INT; + + default: + break; + } + return -EINVAL; +} + +#define LSM303DLH_CHANNEL(axis, addr) \ + { \ + .type = IIO_ACCEL, \ + .modified = 1, \ + .channel2 = IIO_MOD_##axis, \ + .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \ + .address = addr, \ + } + +static const struct iio_chan_spec lsmdlh303_channels[] = { + LSM303DLH_CHANNEL(X, LSM303DLH_A_OUT_X_L_A), + LSM303DLH_CHANNEL(Y, LSM303DLH_A_OUT_Y_L_A), + LSM303DLH_CHANNEL(Z, LSM303DLH_A_OUT_Z_L_A), +}; + + +static IIO_DEVICE_ATTR(accel_raw, + S_IRUGO, + lsm303dlh_a_readdata, + NULL, + LSM303DLH_A_OUT_X_L_A); +static IIO_DEVICE_ATTR(sampling_frequency, + S_IWUSR | S_IRUGO, + show_sampling_frequency, + set_sampling_frequency, + LSM303DLH_A_CTRL_REG1_A); +static IIO_DEVICE_ATTR(mode, + S_IWUSR | S_IRUGO, + show_operating_mode, + set_operating_mode, + LSM303DLH_A_CTRL_REG1_A); +static IIO_DEVICE_ATTR(id, + S_IRUGO, + show_chip_id, + NULL, 0); + +static struct attribute *lsm303dlh_a_attributes[] = { + &iio_dev_attr_mode.dev_attr.attr, + &iio_dev_attr_sampling_frequency.dev_attr.attr, + &iio_dev_attr_id.dev_attr.attr, + &iio_dev_attr_accel_raw.dev_attr.attr, + &iio_const_attr_sampling_frequency_available.dev_attr.attr, + &iio_const_attr_accel_xyz_scale_available.dev_attr.attr, + NULL +}; + +static const struct attribute_group lsmdlh303a_group = { + .attrs = lsm303dlh_a_attributes, +}; + +static const struct iio_info lsmdlh303a_info = { + .attrs = &lsmdlh303a_group, + .read_raw = &lsm303dlh_a_read_raw, + .write_raw = &lsm303dlh_a_write_raw, + .driver_module = THIS_MODULE, +}; + +static void lsm303dlh_a_setup(struct lsm303dlh_a_data *data) +{ + /* set mode */ + lsm303dlh_a_set_mode(data->client, data->mode); + /* set rate */ + lsm303dlh_a_set_rate(data->client, data->rate); + /* set range */ + lsm303dlh_a_set_range(data->client, scale_to_range[data->range]); + /* set boot bit to reboot memory content */ + lsm303dlh_a_setbootbit(data->client, LSM303DLH_A_CR2_BOOT_ENABLE); +} + +#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) +static int lsm303dlh_a_suspend(struct device *dev) +{ + struct lsm303dlh_a_data *data = iio_priv(dev_get_drvdata(dev)); + int ret = 0; + + if (data->mode == LSM303DLH_A_MODE_OFF) + return 0; + + mutex_lock(&data->lock); + + /* Set the device to sleep mode */ + lsm303dlh_a_set_mode(data->client, LSM303DLH_A_MODE_OFF); + + /* Disable regulator */ + lsm303dlh_a_disable(data); + + data->device_status = LSM303DLH_A_DEVICE_SUSPENDED; + + mutex_unlock(&data->lock); + + return ret; +} + +static int lsm303dlh_a_resume(struct device *dev) +{ + struct lsm303dlh_a_data *data = iio_priv(dev_get_drvdata(dev)); + int ret = 0; + + + if (data->device_status == LSM303DLH_A_DEVICE_ON || + data->device_status == LSM303DLH_A_DEVICE_OFF) { + return 0; + } + mutex_lock(&data->lock); + + /* Enable regulator */ + lsm303dlh_a_enable(data); + + /* Set mode,rate and range */ + lsm303dlh_a_setup(data); + + mutex_unlock(&data->lock); + return ret; +} + +static const struct dev_pm_ops lsm303dlh_a_dev_pm_ops = { + .suspend = lsm303dlh_a_suspend, + .resume = lsm303dlh_a_resume, +}; +#endif +#ifdef CONFIG_HAS_EARLYSUSPEND +static void lsm303dlh_a_early_suspend(struct early_suspend *data) +{ + struct lsm303dlh_a_data *ddata = + container_of(data, struct lsm303dlh_a_data, early_suspend); + + if (ddata->mode == LSM303DLH_A_MODE_OFF) + return; + + mutex_lock(&ddata->lock); + + /* Set the device to sleep mode */ + lsm303dlh_a_set_mode(ddata->client, LSM303DLH_A_MODE_OFF); + + /* Disable regulator */ + lsm303dlh_a_disable(ddata); + + ddata->device_status = LSM303DLH_A_DEVICE_SUSPENDED; + + mutex_unlock(&ddata->lock); + +} + +static void lsm303dlh_a_late_resume(struct early_suspend *data) +{ + struct lsm303dlh_a_data *ddata = + container_of(data, struct lsm303dlh_a_data, early_suspend); + + + if (ddata->device_status == LSM303DLH_A_DEVICE_ON || + ddata->device_status == LSM303DLH_A_DEVICE_OFF) { + return; + } + mutex_lock(&ddata->lock); + + /* Enable regulator */ + lsm303dlh_a_enable(ddata); + + /* Set mode,rate and range */ + lsm303dlh_a_setup(ddata); + + mutex_unlock(&ddata->lock); + +} +#endif + +static int lsm303dlh_a_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct lsm303dlh_a_data *data; + struct iio_dev *indio_dev; + int err = 0; + + indio_dev = iio_allocate_device(sizeof(*data)); + if (indio_dev == NULL) { + dev_err(&client->dev, "memory allocation failed\n"); + err = -ENOMEM; + goto exit; + } + data = iio_priv(indio_dev); + + data->mode = LSM303DLH_A_MODE_OFF; + data->range = LSM303DLH_A_RANGE_2G; + data->rate = LSM303DLH_A_RATE_50; + data->device_status = LSM303DLH_A_DEVICE_OFF; + data->client = client; + + /* check for valid platform data */ + if (!client->dev.platform_data) { + dev_err(&client->dev, "Invalid platform data\n"); + err = -ENOMEM; + goto exit1; + } + data->pdata = client->dev.platform_data; + + i2c_set_clientdata(client, indio_dev); + + data->regulator = regulator_get(&client->dev, "vdd"); + if (IS_ERR(data->regulator)) { + err = PTR_ERR(data->regulator); + dev_err(&client->dev, "failed to get regulator = %d\n", err); + goto exit1; + } + /* Enable regulator */ + lsm303dlh_a_enable(data); + + lsm303dlh_a_setup(data); + + mutex_init(&data->lock); + indio_dev->info = &lsmdlh303a_info; + indio_dev->name = id->name; + indio_dev->dev.parent = &client->dev; + indio_dev->channels = lsmdlh303_channels; + indio_dev->num_channels = ARRAY_SIZE(lsmdlh303_channels); + indio_dev->modes = INDIO_DIRECT_MODE; + + err = iio_device_register(indio_dev); + if (err) + goto exit2; + +#ifdef CONFIG_HAS_EARLYSUSPEND + data->early_suspend.level = + EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; + data->early_suspend.suspend = lsm303dlh_a_early_suspend; + data->early_suspend.resume = lsm303dlh_a_late_resume; + register_early_suspend(&data->early_suspend); +#endif + /* Disable regulator */ + lsm303dlh_a_disable(data); + + return 0; + +exit2: + iio_free_device(indio_dev); + mutex_destroy(&data->lock); + regulator_disable(data->regulator); + regulator_put(data->regulator); +exit1: + kfree(data); +exit: + return err; +} + +static int __devexit lsm303dlh_a_remove(struct i2c_client *client) +{ + struct iio_dev *indio_dev = i2c_get_clientdata(client); + struct lsm303dlh_a_data *data = iio_priv(indio_dev); + int ret; + + /* safer to make device off */ + if (data->mode != LSM303DLH_A_MODE_OFF) { + /* set mode to off */ + ret = lsm303dlh_a_set_mode(client, LSM303DLH_A_MODE_OFF); + if (ret < 0) { + dev_err(&client->dev, "could not turn off the device %d", + ret); + return ret; + } + if (data->device_status == LSM303DLH_A_DEVICE_ON) { + regulator_disable(data->regulator); + data->device_status = LSM303DLH_A_DEVICE_OFF; + } + } + regulator_put(data->regulator); + mutex_destroy(&data->lock); + iio_device_unregister(indio_dev); + iio_free_device(indio_dev); + kfree(data); + return 0; +} + +static const struct i2c_device_id lsm303dlh_a_id[] = { + { "lsm303dlh_a", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, lsm303dlh_a_id); + +static struct i2c_driver lsm303dlh_a_driver = { + .driver = { + .name = "lsm303dlh_a", + #if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) + .pm = &lsm303dlh_a_dev_pm_ops, + #endif + }, + .id_table = lsm303dlh_a_id, + .probe = lsm303dlh_a_probe, + .remove = lsm303dlh_a_remove, +}; + +module_i2c_driver(lsm303dlh_a_driver); + +MODULE_DESCRIPTION("lsm303dlh Accelerometer Driver"); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Naga Radhesh Y "); diff --git a/drivers/staging/iio/accel/lsm303dlhc_core.c b/drivers/staging/iio/accel/lsm303dlhc_core.c new file mode 100644 index 00000000000..7fc0bf91204 --- /dev/null +++ b/drivers/staging/iio/accel/lsm303dlhc_core.c @@ -0,0 +1,838 @@ +/* + * Copyright (C) ST-Ericsson SA 2012 + * License Terms: GNU General Public License, version 2 + * + * Mostly this accelerometer device is a copy of magnetometer + * driver lsm303dlh or viceversa, so the code is mostly based + * on lsm303dlh driver. + * + * Author: Naga Radhesh Y + */ +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef CONFIG_HAS_EARLYSUSPEND +#include +#endif +#include + +#include "../iio.h" +#include "../sysfs.h" + +/* Idenitification register */ +#define LSM303DLHC_A_CHIP_ID 0x0F +/* control register1 */ +#define LSM303DLHC_A_CTRL_REG1_A 0x20 +/* control register2 */ +#define LSM303DLHC_A_CTRL_REG2_A 0x21 +/* control register3 */ +#define LSM303DLHC_A_CTRL_REG3_A 0x22 +/* control register4 */ +#define LSM303DLHC_A_CTRL_REG4_A 0x23 +/* control register5 */ +#define LSM303DLHC_A_CTRL_REG5_A 0x24 +/* data output X register */ +#define LSM303DLHC_A_OUT_X_L_A 0x28 +/* data output Y register */ +#define LSM303DLHC_A_OUT_Y_L_A 0x2A +/* data output Z register */ +#define LSM303DLHC_A_OUT_Z_L_A 0x2C +/* status register */ +#define LSM303DLHC_A_STATUS_REG_A 0x27 + +/* control register 1, Mode selection */ +#define LSM303DLHC_A_CR1_PM_BIT 4 +#define LSM303DLHC_A_CR1_PM_MASK (0xF << LSM303DLHC_A_CR1_PM_BIT) +/* control register 1, Lowpower Enable */ +#define LSM303DLHC_A_CR1_LPE_BIT 3 +#define LSM303DLHC_A_CR1_LPE_MASK (0x1 << LSM303DLHC_A_CR1_DR_BIT) +/* control register 1, x,y,z enable bits */ +#define LSM303DLHC_A_CR1_EN_BIT 0 +#define LSM303DLHC_A_CR1_EN_MASK (0x7 << LSM303DLHC_A_CR1_EN_BIT) +#define LSM303DLHC_A_CR1_AXIS_ENABLE 7 + +/* control register 4, self test */ +#define LSM303DLHC_A_CR4_ST_BIT 1 +#define LSM303DLHC_A_CR4_ST_MASK (0x3 << LSM303DLHC_A_CR4_ST_BIT) +/* control register 4, full scale */ +#define LSM303DLHC_A_CR4_FS_BIT 4 +#define LSM303DLHC_A_CR4_FS_MASK (0x3 << LSM303DLHC_A_CR4_FS_BIT) +/* control register 4, endianness */ +#define LSM303DLHC_A_CR4_BLE_BIT 6 +#define LSM303DLHC_A_CR4_BLE_MASK (0x1 << LSM303DLHC_A_CR4_BLE_BIT) + +/* control register 5, Re-Boot Memory */ +#define LSM303DLHC_A_CR5_BOOT_ENABLE 0x80 + +/* Accelerometer operating mode */ +#define LSM303DLHC_A_MODE_OFF 0x00 +#define LSM303DLHC_A_MODE_MAX 0x09 + +/* + * CTRL_REG1_A register rate settings + * + * DR3 DR2 DR1 DR0 Output data rate[Hz] + * 0 0 0 0 0 + * 0 0 0 1 1 + * 0 0 1 0 10 + * 0 0 1 1 25 + * 0 1 0 0 50 + * 0 1 0 1 100 + * 0 1 1 0 200 + * 0 1 1 1 400 + * 1 0 0 0 1.62K + * 1 0 0 1 1.334K + */ +#define LSM303DLHC_A_MODE_NORMAL_1HZ 0x01 +#define LSM303DLHC_A_MODE_NORMAL_10HZ 0x02 +#define LSM303DLHC_A_MODE_NORMAL_25HZ 0x03 +#define LSM303DLHC_A_MODE_NORMAL_50HZ 0x04 +#define LSM303DLHC_A_MODE_NORMAL_100HZ 0x05 +#define LSM303DLHC_A_MODE_NORMAL_200HZ 0x06 +#define LSM303DLHC_A_MODE_NORMAL_400HZ 0x07 +#define LSM303DLHC_A_MODE_NORMAL_162KHZ 0x08 +#define LSM303DLHC_A_MODE_NORMAL_1344KHZ 0x09 + +/* + * CTRL_REG4_A register range settings + * + * FS1 FS0 FUll scale range + * 0 0 2g + * 0 1 4g + * 1 0 Not used + * 1 1 8g + */ +#define LSM303DLHC_A_RANGE_2G 0x00 +#define LSM303DLHC_A_RANGE_4G 0x01 +#define LSM303DLHC_A_RANGE_8G 0x02 +#define LSM303DLHC_A_RANGE_16G 0x03 + +/* Sensitivity adjustment */ +#define LSM303DLHC_A_SHIFT_ADJ_2G 4 /* 1/16*/ +#define LSM303DLHC_A_SHIFT_ADJ_4G 3 /* 2/16*/ +#define LSM303DLHC_A_SHIFT_ADJ_8G 2 /* ~3.9/16*/ +#define LSM303DLHC_A_SHIFT_ADJ_16G 1 /* ~3.9/16*/ + +/* device status defines */ +#define LSM303DLHC_A_DEVICE_OFF 0 +#define LSM303DLHC_A_DEVICE_ON 1 +#define LSM303DLHC_A_DEVICE_SUSPENDED 2 + +/* status register */ +#define LSM303DLHC_A_SR_REG_A 0x27 +/* status register, ready */ +#define LSM303DLHC_A_XYZ_DATA_RDY 0x04 +#define LSM303DLHC_A_XYZ_DATA_RDY_BIT 3 +#define LSM303DLHC_A_XYZ_DATA_RDY_MASK (0x1 << LSM303DLHC_A_XYZ_DATA_RDY_BIT) + +/* Multiple byte transfer enable */ +#define MULTIPLE_I2C_TR 0x80 + +/* + * struct lsm303dlhc_a_data - data structure used by lsm303dlhc_a driver + * @client: i2c client + * @lock: mutex lock for sysfs operations + * @regulator: regulator + * @early_suspend: early suspend structure + * @pdata: lsm303dlh platform data + * @mode: current mode of operation + * @range: current range value of accelerometer + * @shift_adjust: output bit shift + * @device_status: device is ON, OFF or SUSPENDED + */ + +struct lsm303dlhc_a_data { + struct i2c_client *client; + struct mutex lock; + struct regulator *regulator; +#ifdef CONFIG_HAS_EARLYSUSPEND + struct early_suspend early_suspend; +#endif + struct lsm303dlh_platform_data *pdata; + + u8 mode; + u8 range; + int shift_adjust; + int device_status; +}; + +#ifdef CONFIG_HAS_EARLYSUSPEND +static void lsm303dlhc_a_early_suspend(struct early_suspend *data); +static void lsm303dlhc_a_late_resume(struct early_suspend *data); +#endif + +static inline int is_device_on(struct lsm303dlhc_a_data *data) +{ + struct i2c_client *client = data->client; + /* Perform read/write operation only when device is active */ + if (data->device_status != LSM303DLHC_A_DEVICE_ON) { + dev_err(&client->dev, + "device is switched off, make it on using mode"); + return -EINVAL; + } + + return 0; +} + +/* To disable regulator and status */ +static int lsm303dlhc_a_disable(struct lsm303dlhc_a_data *data) +{ + data->device_status = LSM303DLHC_A_DEVICE_OFF; + regulator_disable(data->regulator); + return 0; +} + +/* To enable regulator and status */ +static int lsm303dlhc_a_enable(struct lsm303dlhc_a_data *data) +{ + data->device_status = LSM303DLHC_A_DEVICE_ON; + regulator_enable(data->regulator); + return 0; +} + +static s32 lsm303dlhc_a_setbootbit(struct i2c_client *client, u8 reg_val) +{ + /* write to the boot bit to reboot memory content */ + return i2c_smbus_write_byte_data(client, + LSM303DLHC_A_CTRL_REG5_A, reg_val); +} + +static s32 lsm303dlhc_a_set_mode(struct i2c_client *client, u8 mode) +{ + int reg_val; + + if (mode > LSM303DLHC_A_MODE_NORMAL_1344KHZ) { + dev_err(&client->dev, "given mode not supported\n"); + return -EINVAL; + } + + reg_val = i2c_smbus_read_byte_data(client, LSM303DLHC_A_CTRL_REG1_A); + + reg_val |= LSM303DLHC_A_CR1_AXIS_ENABLE; + reg_val &= ~LSM303DLHC_A_CR1_PM_MASK; + + reg_val |= ((mode << LSM303DLHC_A_CR1_PM_BIT) & LSM303DLHC_A_CR1_PM_MASK); + + /* the upper 4 bits indicates the accelerometer sensor mode and rate */ + return i2c_smbus_write_byte_data(client, + LSM303DLHC_A_CTRL_REG1_A, reg_val); +} + +static s32 lsm303dlhc_a_set_range(struct i2c_client *client, u8 range) +{ + int reg_val; + + if (range > LSM303DLHC_A_RANGE_16G) { + dev_err(&client->dev, "given range not supported\n"); + return -EINVAL; + } + + reg_val = (range << LSM303DLHC_A_CR4_FS_BIT); + + /* 4th and 5th bits indicate range of accelerometer */ + return i2c_smbus_write_byte_data(client, + LSM303DLHC_A_CTRL_REG4_A, reg_val); +} + +static s32 lsm303dlhc_a_set_shift(struct lsm303dlhc_a_data *data, u8 range) +{ + int ret = 0; + struct i2c_client *client = data->client; + + switch (range) { + case LSM303DLHC_A_RANGE_2G: + data->shift_adjust = LSM303DLHC_A_SHIFT_ADJ_2G; + break; + case LSM303DLHC_A_RANGE_4G: + data->shift_adjust = LSM303DLHC_A_SHIFT_ADJ_4G; + break; + case LSM303DLHC_A_RANGE_8G: + data->shift_adjust = LSM303DLHC_A_SHIFT_ADJ_8G; + break; + case LSM303DLHC_A_RANGE_16G: + data->shift_adjust = LSM303DLHC_A_SHIFT_ADJ_16G; + break; + default: + dev_err(&client->dev, "Invalid range %d\n", range); + ret = -EINVAL; + } + return ret; +} + + +/* + * To read output x/y/z data register, + * in this case x,y and z are not + * mapped w.r.t board orientation. + *Reading just raw data from device + */ +static ssize_t lsm303dlhc_a_xyz_read(struct iio_dev *indio_dev, + int address, + int *buf) +{ + + struct lsm303dlhc_a_data *data = iio_priv(indio_dev); + int lsb , msb; + int ret; + s16 val; + + /* Perform read/write operation, only when device is active */ + ret = is_device_on(data); + if (ret) + return -EINVAL; + + mutex_lock(&data->lock); + ret = i2c_smbus_read_byte_data(data->client, LSM303DLHC_A_SR_REG_A); + + /* wait till data is written to all six registers */ + while (!(ret & LSM303DLHC_A_XYZ_DATA_RDY_MASK)) + ret = i2c_smbus_read_byte_data(data->client, + LSM303DLHC_A_SR_REG_A); + + lsb = i2c_smbus_read_byte_data(data->client, address); + if (lsb < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + msb = i2c_smbus_read_byte_data(data->client, (address + 1)); + if (msb < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + val = ((msb << 8) | lsb); + + val >>= data->shift_adjust; + *buf = (s16)val; + + mutex_unlock(&data->lock); + return IIO_VAL_INT; +} + +/* + * To read output x,y,z data register. + * After reading change x,y and z values + * w.r.t the orientation of the device. + */ +static ssize_t lsm303dlhc_a_readdata(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlhc_a_data *data = iio_priv(dev_get_drvdata(dev)); + struct lsm303dlh_platform_data *pdata = data->pdata; + struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); + u8 map_x = pdata->axis_map_x; + u8 map_y = pdata->axis_map_y; + u8 map_z = pdata->axis_map_z; + int ret; + unsigned char accel_data[6]; + s16 val[3]; + + /* Perform read/write operation, only when device is active */ + ret = is_device_on(data); + if (ret) + return -EINVAL; + + mutex_lock(&data->lock); + + ret = i2c_smbus_read_byte_data(data->client, LSM303DLHC_A_SR_REG_A); + /* wait till data is written to all six registers */ + while (!((ret & LSM303DLHC_A_XYZ_DATA_RDY_MASK))) + ret = i2c_smbus_read_byte_data(data->client, + LSM303DLHC_A_SR_REG_A); + + ret = i2c_smbus_read_i2c_block_data(data->client, + this_attr->address | MULTIPLE_I2C_TR, + 6, accel_data); + + if (ret < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + + /* MSB is at lower address */ + val[0] = (s16) + (((accel_data[1]) << 8) | accel_data[0]); + val[1] = (s16) + (((accel_data[3]) << 8) | accel_data[2]); + val[2] = (s16) + (((accel_data[5]) << 8) | accel_data[4]); + + val[0] >>= data->shift_adjust; + val[1] >>= data->shift_adjust; + val[2] >>= data->shift_adjust; + + /* modify the x,y and z values w.r.t orientation of device*/ + if (pdata->negative_x) + val[map_x] = -val[map_x]; + if (pdata->negative_y) + val[map_y] = -val[map_y]; + if (pdata->negative_z) + val[map_z] = -val[map_z]; + + mutex_unlock(&data->lock); + + return sprintf(buf, "%d:%d:%d:%lld\n", val[map_x], val[map_y], + val[map_z], iio_get_time_ns()); +} + +static ssize_t show_chip_id(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlhc_a_data *data = iio_priv(dev_get_drvdata(dev)); + + return sprintf(buf, "%d\n", data->pdata->chip_id); +} + +static ssize_t show_operating_mode(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlhc_a_data *data = iio_priv(dev_get_drvdata(dev)); + + return sprintf(buf, "%d\n", data->mode); +} + +static ssize_t set_operating_mode(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct lsm303dlhc_a_data *data = iio_priv(dev_get_drvdata(dev)); + struct i2c_client *client = data->client; + int error; + unsigned long mode = 0; + bool set_boot_bit = false; + + mutex_lock(&data->lock); + + error = kstrtoul(buf, 10, &mode); + if (error) { + count = error; + goto exit; + } + + if (mode > LSM303DLHC_A_MODE_NORMAL_1344KHZ) { + dev_err(&client->dev, "trying to set invalid mode\n"); + count = -EINVAL; + goto exit; + } + + /* + * If device is drived to sleep mode in suspend, update mode + * and return + */ + if (data->device_status == LSM303DLHC_A_DEVICE_SUSPENDED && + mode == LSM303DLHC_A_MODE_OFF) { + data->mode = mode; + goto exit; + } + + /* if same mode as existing, return */ + if (data->mode == mode) + goto exit; + + /* + * set boot bit when device comes from suspend state + * to ensure correct device behavior after it resumes + */ + if (data->device_status == LSM303DLHC_A_DEVICE_SUSPENDED) + set_boot_bit = true; + + /* Enable the regulator if it is not turned ON earlier */ + if (data->device_status == LSM303DLHC_A_DEVICE_OFF || + data->device_status == LSM303DLHC_A_DEVICE_SUSPENDED) + lsm303dlhc_a_enable(data); + + dev_dbg(dev, "set operating mode to %lu\n", mode); + error = lsm303dlhc_a_set_mode(client, mode); + if (error < 0) { + dev_err(&client->dev, "Error in setting the mode\n"); + count = -EINVAL; + goto exit; + } + + data->mode = mode; + + if (set_boot_bit) { + /* set boot bit to reboot memory content */ + lsm303dlhc_a_setbootbit(client, LSM303DLHC_A_CR5_BOOT_ENABLE); + } + + /* If mode is OFF then disable the regulator */ + if (data->mode == LSM303DLHC_A_MODE_OFF) { + /* fall back to default values */ + data->range = 0; + data->shift_adjust = LSM303DLHC_A_SHIFT_ADJ_2G; + lsm303dlhc_a_disable(data); + } +exit: + mutex_unlock(&data->lock); + return count; +} + +static IIO_CONST_ATTR(accel_xyz_scale_available, "2, 4, 8, 16"); + +static const int xyz_to_scale[] = { + 2, 4, 8, 16 +}; + +static const char const scale_to_range[] = { + LSM303DLHC_A_RANGE_2G, + LSM303DLHC_A_RANGE_4G, + LSM303DLHC_A_RANGE_8G, + LSM303DLHC_A_RANGE_16G, +}; + +static int lsm303dlhc_a_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, + long mask) +{ + struct lsm303dlhc_a_data *data = iio_priv(indio_dev); + int ret = -EINVAL, i; + bool flag = false; + char end; + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + ret = is_device_on(data); + if (ret) + return -EINVAL; + mutex_lock(&data->lock); + end = ARRAY_SIZE(xyz_to_scale); + for (i = 0; i < end; i++) { + if (val == xyz_to_scale[i]) { + flag = true; + break; + } + } + if (flag) { + ret = lsm303dlhc_a_set_range(data->client, + scale_to_range[i]); + if (ret < 0) { + mutex_unlock(&data->lock); + return -EINVAL; + } + ret = lsm303dlhc_a_set_shift(data, scale_to_range[i]); + data->range = i; + } + mutex_unlock(&data->lock); + default: + break; + } + return ret; +} + +static int lsm303dlhc_a_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, + long mask) +{ + struct lsm303dlhc_a_data *data = iio_priv(indio_dev); + switch (mask) { + case 0: + return lsm303dlhc_a_xyz_read(indio_dev, + chan->address, val); + case IIO_CHAN_INFO_SCALE: + *val = xyz_to_scale[data->range]; + return IIO_VAL_INT; + + default: + break; + } + return -EINVAL; +} + +#define LSM303DLHC_CHANNEL(axis, addr) \ + { \ + .type = IIO_ACCEL, \ + .modified = 1, \ + .channel2 = IIO_MOD_##axis, \ + .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \ + .address = addr, \ + } + +static const struct iio_chan_spec lsmdlh303_channels[] = { + LSM303DLHC_CHANNEL(X, LSM303DLHC_A_OUT_X_L_A), + LSM303DLHC_CHANNEL(Y, LSM303DLHC_A_OUT_Y_L_A), + LSM303DLHC_CHANNEL(Z, LSM303DLHC_A_OUT_Z_L_A), +}; + + + +static IIO_DEVICE_ATTR(accel_raw, + S_IRUGO, + lsm303dlhc_a_readdata, + NULL, + LSM303DLHC_A_OUT_X_L_A); +static IIO_DEVICE_ATTR(mode, + S_IWUSR | S_IRUGO, + show_operating_mode, + set_operating_mode, + LSM303DLHC_A_CTRL_REG1_A); +static IIO_DEVICE_ATTR(id, + S_IRUGO, + show_chip_id, + NULL, 0); + +static struct attribute *lsm303dlhc_a_attributes[] = { + &iio_dev_attr_mode.dev_attr.attr, + &iio_dev_attr_id.dev_attr.attr, + &iio_dev_attr_accel_raw.dev_attr.attr, + &iio_const_attr_accel_xyz_scale_available.dev_attr.attr, + NULL +}; + +static const struct attribute_group lsmdlh303a_group = { + .attrs = lsm303dlhc_a_attributes, +}; + +static const struct iio_info lsmdlh303a_info = { + .attrs = &lsmdlh303a_group, + .read_raw = &lsm303dlhc_a_read_raw, + .write_raw = &lsm303dlhc_a_write_raw, + .driver_module = THIS_MODULE, +}; + +static void lsm303dlhc_a_setup(struct lsm303dlhc_a_data *data) +{ + + /* set mode */ + lsm303dlhc_a_set_mode(data->client, data->mode); + /* set range */ + lsm303dlhc_a_set_range(data->client, scale_to_range[data->range]); + /* set boot bit to reboot memory content */ + lsm303dlhc_a_setbootbit(data->client, LSM303DLHC_A_CR5_BOOT_ENABLE); +} + +#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) + +static int lsm303dlhc_a_suspend(struct device *dev) +{ + struct lsm303dlhc_a_data *data = iio_priv(dev_get_drvdata(dev)); + int ret = 0; + + if (data->mode == LSM303DLHC_A_MODE_OFF) + return 0; + + mutex_lock(&data->lock); + + /* Set the device to sleep mode */ + lsm303dlhc_a_set_mode(data->client, LSM303DLHC_A_MODE_OFF); + + /* Disable regulator */ + lsm303dlhc_a_disable(data); + + data->device_status = LSM303DLHC_A_DEVICE_SUSPENDED; + + mutex_unlock(&data->lock); + + return ret; +} + +static int lsm303dlhc_a_resume(struct device *dev) +{ + struct lsm303dlhc_a_data *data = iio_priv(dev_get_drvdata(dev)); + int ret = 0; + + + if (data->device_status == LSM303DLHC_A_DEVICE_ON || + data->device_status == LSM303DLHC_A_DEVICE_OFF) { + return 0; + } + mutex_lock(&data->lock); + + /* Enable regulator */ + lsm303dlhc_a_enable(data); + + /* Set mode and range */ + lsm303dlhc_a_setup(data); + + mutex_unlock(&data->lock); + return ret; +} + + +static const struct dev_pm_ops lsm303dlhc_a_dev_pm_ops = { + .suspend = lsm303dlhc_a_suspend, + .resume = lsm303dlhc_a_resume, +}; +#endif +#ifdef CONFIG_HAS_EARLYSUSPEND +static void lsm303dlhc_a_early_suspend(struct early_suspend *data) +{ + struct lsm303dlhc_a_data *ddata = + container_of(data, struct lsm303dlhc_a_data, early_suspend); + if (ddata->mode == LSM303DLHC_A_MODE_OFF) + return; + + mutex_lock(&ddata->lock); + + /* Set the device to sleep mode */ + lsm303dlhc_a_set_mode(ddata->client, LSM303DLHC_A_MODE_OFF); + + /* Disable regulator */ + lsm303dlhc_a_disable(ddata); + + ddata->device_status = LSM303DLHC_A_DEVICE_SUSPENDED; + + mutex_unlock(&ddata->lock); +} + +static void lsm303dlhc_a_late_resume(struct early_suspend *data) +{ + struct lsm303dlhc_a_data *ddata = + container_of(data, struct lsm303dlhc_a_data, early_suspend); + if (ddata->device_status == LSM303DLHC_A_DEVICE_ON || + ddata->device_status == LSM303DLHC_A_DEVICE_OFF) { + return; + } + mutex_lock(&ddata->lock); + + /* Enable regulator */ + lsm303dlhc_a_enable(ddata); + + /* Set mode,rate and range */ + lsm303dlhc_a_setup(ddata); + + mutex_unlock(&ddata->lock); +} +#endif + +static int lsm303dlhc_a_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct lsm303dlhc_a_data *data; + struct iio_dev *indio_dev; + int err = 0; + + indio_dev = iio_allocate_device(sizeof(*data)); + if (indio_dev == NULL) { + dev_err(&client->dev, "memory allocation failed\n"); + err = -ENOMEM; + goto exit; + } + + data = iio_priv(indio_dev); + + data->mode = LSM303DLHC_A_MODE_OFF; + data->range = LSM303DLHC_A_RANGE_2G; + data->device_status = LSM303DLHC_A_DEVICE_OFF; + data->client = client; + + /* check for valid platform data */ + if (!client->dev.platform_data) { + dev_err(&client->dev, "Invalid platform data\n"); + err = -ENOMEM; + goto exit1; + } + data->pdata = client->dev.platform_data; + + i2c_set_clientdata(client, indio_dev); + + data->regulator = regulator_get(&client->dev, "vdd"); + if (IS_ERR(data->regulator)) { + err = PTR_ERR(data->regulator); + dev_err(&client->dev, "failed to get regulator = %d\n", err); + goto exit1; + } + /* Enable regulator */ + lsm303dlhc_a_enable(data); + + lsm303dlhc_a_setup(data); + + mutex_init(&data->lock); + + indio_dev->info = &lsmdlh303a_info; + indio_dev->name = id->name; + indio_dev->dev.parent = &client->dev; + indio_dev->channels = lsmdlh303_channels; + indio_dev->num_channels = ARRAY_SIZE(lsmdlh303_channels); + indio_dev->modes = INDIO_DIRECT_MODE; + + err = iio_device_register(indio_dev); + if (err) + goto exit2; + +#ifdef CONFIG_HAS_EARLYSUSPEND + data->early_suspend.level = + EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; + data->early_suspend.suspend = lsm303dlhc_a_early_suspend; + data->early_suspend.resume = lsm303dlhc_a_late_resume; + register_early_suspend(&data->early_suspend); +#endif + /* Disable regulator */ + lsm303dlhc_a_disable(data); + + return 0; + +exit2: + iio_free_device(indio_dev); + mutex_destroy(&data->lock); + regulator_disable(data->regulator); + regulator_put(data->regulator); +exit1: + iio_free_device(indio_dev); +exit: + return err; +} + +static int __devexit lsm303dlhc_a_remove(struct i2c_client *client) +{ + struct iio_dev *indio_dev = i2c_get_clientdata(client); + struct lsm303dlhc_a_data *data = iio_priv(indio_dev); + int ret; + + /* safer to make device off */ + if (data->mode != LSM303DLHC_A_MODE_OFF) { + /* set mode to off */ + ret = lsm303dlhc_a_set_mode(client, LSM303DLHC_A_MODE_OFF); + if (ret < 0) { + dev_err(&client->dev, + "could not turn off the device %d", ret); + return ret; + } + if (data->regulator && data->device_status == LSM303DLHC_A_DEVICE_ON) { + regulator_disable(data->regulator); + data->device_status = LSM303DLHC_A_DEVICE_OFF; + } + } + regulator_put(data->regulator); + mutex_destroy(&data->lock); + iio_device_unregister(indio_dev); + iio_free_device(indio_dev); + kfree(data); + return 0; +} + +static const struct i2c_device_id lsm303dlhc_a_id[] = { + { "lsm303dlhc_a", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, lsm303dlhc_a_id); + +static struct i2c_driver lsm303dlhc_a_driver = { + .driver = { + .name = "lsm303dlhc_a", + #if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) + .pm = &lsm303dlhc_a_dev_pm_ops, + #endif + }, + .id_table = lsm303dlhc_a_id, + .probe = lsm303dlhc_a_probe, + .remove = lsm303dlhc_a_remove, +}; + +module_i2c_driver(lsm303dlhc_a_driver); + +MODULE_DESCRIPTION("lsm303dlhc Accelerometer Driver"); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Naga Radhesh Y "); diff --git a/drivers/staging/iio/gyro/Kconfig b/drivers/staging/iio/gyro/Kconfig index ea295b25308..c35e1e01241 100644 --- a/drivers/staging/iio/gyro/Kconfig +++ b/drivers/staging/iio/gyro/Kconfig @@ -46,4 +46,15 @@ config ADXRS450 This driver can also be built as a module. If so, the module will be called adxrs450. +config SENSORS_L3G4200D + tristate "STMicroelectronics L3G4200D 3-Axis Gyroscope" + depends on I2C + default n + help + Say Y here to add support for the STMicroelectronics + L3G4200D 3-Axis Gyroscope. + + To compile this driver as a module, choose M here: the module + will be called l3g4200d. + endmenu diff --git a/drivers/staging/iio/gyro/Makefile b/drivers/staging/iio/gyro/Makefile index 9ba5ec15170..d5f914d7c6e 100644 --- a/drivers/staging/iio/gyro/Makefile +++ b/drivers/staging/iio/gyro/Makefile @@ -20,3 +20,5 @@ obj-$(CONFIG_ADIS16251) += adis16251.o adxrs450-y := adxrs450_core.o obj-$(CONFIG_ADXRS450) += adxrs450.o + +obj-$(CONFIG_SENSORS_L3G4200D) += l3g4200d_core.o diff --git a/drivers/staging/iio/gyro/l3g4200d_core.c b/drivers/staging/iio/gyro/l3g4200d_core.c new file mode 100644 index 00000000000..8455a7994e8 --- /dev/null +++ b/drivers/staging/iio/gyro/l3g4200d_core.c @@ -0,0 +1,931 @@ +/* + * Copyright (C) ST-Ericsson SA 2012 + * License Terms: GNU General Public License, version 2 + * + * Mostly this gyroscope device is a copy of magnetometer + * driver lsm303dlh or viceversa, so the code is mostly based + * on lsm303dlh driver. + * + * Author: Naga Radhesh Y + */ +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef CONFIG_HAS_EARLYSUSPEND +#include +#endif +#include + +#include "../iio.h" +#include "../sysfs.h" + +/* Idenitification register */ +#define L3G4200D_WHO_AM_I 0x0F +/* control register1 */ +#define L3G4200D_CTRL_REG1 0x20 +/* control register2 */ +#define L3G4200D_CTRL_REG2 0x21 +/* control register3 */ +#define L3G4200D_CTRL_REG3 0x22 +/* control register4 */ +#define L3G4200D_CTRL_REG4 0x23 +/* control register5 */ +#define L3G4200D_CTRL_REG5 0x24 +/* out temperature */ +#define L3G4200D_OUT_TEMP 0x26 +/* data output X register */ +#define L3G4200D_OUT_X_L_A 0x28 +/* data output Y register */ +#define L3G4200D_OUT_Y_L_A 0x2A +/* data output Z register */ +#define L3G4200D_OUT_Z_L_A 0x2C +/* status register */ +#define L3G4200D_STATUS_REG_A 0x27 + +/* control register 1, Mode selection */ +#define L3G4200D_CR1_PM_BIT 3 +#define L3G4200D_CR1_PM_MASK (0x01 << L3G4200D_CR1_PM_BIT) +/* control register 1, Data Rate */ +#define L3G4200D_CR1_DR_BIT 4 +#define L3G4200D_CR1_DR_MASK (0x0F << L3G4200D_CR1_DR_BIT) +/* control register 1, x,y,z enable bits */ +#define L3G4200D_CR1_EN_BIT 0 +#define L3G4200D_CR1_EN_MASK (0x7 << L3G4200D_CR1_EN_BIT) +#define L3G4200D_CR1_AXIS_ENABLE 7 + +/* control register 4, self test */ +#define L3G4200D_CR4_ST_BIT 1 +#define L3G4200D_CR4_ST_MASK (0x03 << L3G4200D_CR4_ST_BIT) +/* control register 4, full scale */ +#define L3G4200D_CR4_FS_BIT 4 +#define L3G4200D_CR4_FS_MASK (0x3 << L3G4200D_CR4_FS_BIT) +/* control register 4, endianness */ +#define L3G4200D_CR4_BLE_BIT 6 +#define L3G4200D_CR4_BLE_MASK (0x1 << L3G4200D_CR4_BLE_BIT) +/* control register 4, Block data update */ +#define L3G4200D_CR4_BDU_BIT 7 +#define L3G4200D_CR4_BDU_MASK (0x1 << L3G4200D_CR4_BDU_BIT) + +/* Gyroscope operating mode */ +#define L3G4200D_MODE_OFF 0x00 +#define L3G4200D_MODE_NORMAL 0x01 + +/* Expected content for WAI register */ +#define WHOAMI_L3G4200D 0x00D3 +/* Expected content for WAI register for L3GD20*/ +#define WHOAMI_L3GD20 0x00D4 + +/* + * CTRL_REG1 register rate settings + * + * DR1 DR0 BW1 BW0 Output data rate[Hz] + * 0 0 0 0 100 + * 0 0 0 1 100 + * 0 0 1 0 100 + * 0 0 1 1 100 + * 0 1 0 0 200 + * 0 1 0 1 200 + * 0 1 1 0 200 + * 0 1 1 1 200 + * 1 0 0 0 400 + * 1 0 0 1 400 + * 1 0 1 0 400 + * 1 0 1 1 400 + * 1 1 0 0 800 + * 1 1 0 1 800 + * 1 1 1 0 800 + * 1 1 1 1 800 + */ +#define L3G4200D_ODR_MIN_VAL 0x00 +#define L3G4200D_ODR_MAX_VAL 0x0F +#define L3G4200D_RATE_100 0x00 +#define L3G4200D_RATE_200 0x04 +#define L3G4200D_RATE_400 0x08 +#define L3G4200D_RATE_800 0x0C + +/* + * CTRL_REG4 register range settings + * + * FS1 FS0 FUll scale range + * 0 0 250 + * 0 1 500 + * 1 0 2000 + * 1 1 2000 + */ +#define L3G4200D_RANGE_250 0x00 +#define L3G4200D_RANGE_500 0x01 +#define L3G4200D_RANGE_2000 0x03 + +/* device status defines */ +#define L3G4200D_DEVICE_OFF 0 +#define L3G4200D_DEVICE_ON 1 +#define L3G4200D_DEVICE_SUSPENDED 2 + +/* status register */ +#define L3G4200D_SR_REG_A 0x27 +/* status register, ready */ +#define L3G4200D_XYZ_DATA_RDY 0x80 +#define L3G4200D_XYZ_DATA_RDY_BIT 3 +#define L3G4200D_XYZ_DATA_RDY_MASK (0x1 << L3G4200D_XYZ_DATA_RDY_BIT) + +/* Multiple byte transfer enable */ +#define MULTIPLE_I2C_TR 0x80 + +/* + * struct l3g4200d_data - data structure used by l3g4200d driver + * @client: i2c client + * @lock: mutex lock for sysfs operations + * @regulator: regulator + * @early_suspend: early suspend structure + * @pdata: l3g4200d platform data + * @mode: current mode of operation + * @rate: current sampling rate + * @range: current range value of Gyroscope + * @device_status: device is ON, OFF or SUSPENDED + */ + +struct l3g4200d_data { + struct i2c_client *client; + struct mutex lock; + struct regulator *regulator; +#ifdef CONFIG_HAS_EARLYSUSPEND + struct early_suspend early_suspend; +#endif + struct l3g4200d_gyr_platform_data *pdata; + + u8 mode; + u8 rate; + u8 range; + int device_status; +}; + +#ifdef CONFIG_HAS_EARLYSUSPEND +static void l3g4200d_early_suspend(struct early_suspend *data); +static void l3g4200d_late_resume(struct early_suspend *data); +#endif +static inline int is_device_on(struct l3g4200d_data *data) +{ + struct i2c_client *client = data->client; + /* Perform read/write operation only when device is active */ + if (data->device_status != L3G4200D_DEVICE_ON) { + dev_err(&client->dev, + "device is switched off, make it on using mode"); + return -EINVAL; + } + + return 0; +} + +/* To disable regulator and status */ +static int l3g4200d_disable(struct l3g4200d_data *data) +{ + data->device_status = L3G4200D_DEVICE_OFF; + + regulator_disable(data->regulator); + + return 0; +} + +/* To enable regulator and status */ +static int l3g4200d_enable(struct l3g4200d_data *data) +{ + data->device_status = L3G4200D_DEVICE_ON; + + regulator_enable(data->regulator); + + return 0; +} + +static s32 l3g4200d_set_mode(struct i2c_client *client, u8 mode) +{ + int reg_val; + + if (mode > L3G4200D_MODE_NORMAL) { + dev_err(&client->dev, "given mode not supported\n"); + return -EINVAL; + } + + reg_val = i2c_smbus_read_byte_data(client, L3G4200D_CTRL_REG1); + + reg_val |= L3G4200D_CR1_AXIS_ENABLE; + reg_val &= ~L3G4200D_CR1_PM_MASK; + + reg_val |= ((mode << L3G4200D_CR1_PM_BIT) & L3G4200D_CR1_PM_MASK); + + /* the 4th bit indicates the gyroscope sensor mode */ + return i2c_smbus_write_byte_data(client, L3G4200D_CTRL_REG1, reg_val); +} + +static s32 l3g4200d_set_rate(struct i2c_client *client, u8 rate) +{ + int reg_val; + + if (rate > L3G4200D_ODR_MAX_VAL) { + dev_err(&client->dev, "given rate not supported\n"); + return -EINVAL; + } + reg_val = i2c_smbus_read_byte_data(client, L3G4200D_CTRL_REG1); + + reg_val &= ~L3G4200D_CR1_DR_MASK; + + reg_val |= ((rate << L3G4200D_CR1_DR_BIT) & L3G4200D_CR1_DR_MASK); + + /* upper 4 bits indicate ODR of Gyroscope */ + return i2c_smbus_write_byte_data(client, L3G4200D_CTRL_REG1, reg_val); +} + +static s32 l3g4200d_set_range(struct i2c_client *client, u8 range) +{ + int reg_val; + + if (range > L3G4200D_RANGE_2000) { + dev_err(&client->dev, "given range not supported\n"); + return -EINVAL; + } + + reg_val = (range << L3G4200D_CR4_FS_BIT); + /* + * If BDU is enabled, output registers are not updated until MSB + * and LSB reading completes.Otherwise we will end up reading + * wrong data. + */ + reg_val |= L3G4200D_CR4_BDU_MASK; + + /* 5th and 6th bits indicate rate of gyroscope */ + return i2c_smbus_write_byte_data(client, L3G4200D_CTRL_REG4, reg_val); +} + +/* + * To read output x/y/z data register, + * in this case x,y and z are not + * mapped w.r.t board orientation. + * Reading just raw data from device + */ +static ssize_t l3g4200d_xyz_read(struct iio_dev *indio_dev, + int address, + int *buf) +{ + + struct l3g4200d_data *data = iio_priv(indio_dev); + int lsb , msb; + int ret; + s16 val; + + /* Perform read/write operation, only when device is active */ + ret = is_device_on(data); + if (ret) + return -EINVAL; + mutex_lock(&data->lock); + + ret = i2c_smbus_read_byte_data(data->client, L3G4200D_SR_REG_A); + + /* wait till data is written to all six registers */ + while (!(ret & L3G4200D_XYZ_DATA_RDY_MASK)) + ret = i2c_smbus_read_byte_data(data->client, L3G4200D_SR_REG_A); + + lsb = i2c_smbus_read_byte_data(data->client, address); + if (lsb < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + msb = i2c_smbus_read_byte_data(data->client, (address + 1)); + if (msb < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + val = ((msb << 8) | lsb); + + *buf = (s16)val; + mutex_unlock(&data->lock); + + return IIO_VAL_INT; +} + +/* + * To read output x,y,z data register. + * After reading change x,y and z values + * w.r.t the orientation of the device. + */ +static ssize_t l3g4200d_readdata(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + + struct l3g4200d_data *data = iio_priv(dev_get_drvdata(dev)); + struct l3g4200d_gyr_platform_data *pdata = data->pdata; + struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); + u8 map_x = pdata->axis_map_x; + u8 map_y = pdata->axis_map_y; + u8 map_z = pdata->axis_map_z; + int ret; + unsigned char gyr_data[6]; + s16 val[3]; + + /* Perform read/write operation, only when device is active */ + ret = is_device_on(data); + if (ret) + return -EINVAL; + + mutex_lock(&data->lock); + + ret = i2c_smbus_read_byte_data(data->client, L3G4200D_SR_REG_A); + /* wait till data is written to all six registers */ + while (!((ret & L3G4200D_XYZ_DATA_RDY_MASK))) + ret = i2c_smbus_read_byte_data(data->client, L3G4200D_SR_REG_A); + + ret = i2c_smbus_read_i2c_block_data(data->client, + this_attr->address | MULTIPLE_I2C_TR, 6, gyr_data); + + if (ret < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + + /* MSB is at lower address */ + val[0] = (s16) + (((gyr_data[1]) << 8) | gyr_data[0]); + val[1] = (s16) + (((gyr_data[3]) << 8) | gyr_data[2]); + val[2] = (s16) + (((gyr_data[5]) << 8) | gyr_data[4]); + + /* modify the x,y and z values w.r.t orientation of device*/ + if (pdata->negative_x) + val[map_x] = -val[map_x]; + if (pdata->negative_y) + val[map_y] = -val[map_y]; + if (pdata->negative_z) + val[map_z] = -val[map_z]; + + mutex_unlock(&data->lock); + + return sprintf(buf, "%d:%d:%d:%lld\n", val[map_x], val[map_y], + val[map_z], iio_get_time_ns()); +} + +static ssize_t get_gyrotemp(struct iio_dev *indio_dev, + int address, + int *buf) +{ + struct l3g4200d_data *data = iio_priv(indio_dev); + int ret; + /* Perform read/write operation, only when device is active */ + ret = is_device_on(data); + if (ret) + return -EINVAL; + + ret = i2c_smbus_read_byte_data(data->client, address); + if (ret < 0) { + dev_err(&data->client->dev, "Error in reading Gyro temperature"); + return ret; + } + *buf = ret; + + return IIO_VAL_INT; +} + +static ssize_t show_operating_mode(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct l3g4200d_data *data = iio_priv(dev_get_drvdata(dev)); + + return sprintf(buf, "%d\n", data->mode); +} + +static ssize_t set_operating_mode(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct l3g4200d_data *data = iio_priv(dev_get_drvdata(dev)); + int error; + unsigned long mode = 0; + + mutex_lock(&data->lock); + + error = kstrtoul(buf, 10, &mode); + if (error) { + count = error; + goto exit; + } + + /* check if the received power mode is either 0 or 1 */ + if (mode < L3G4200D_MODE_OFF || mode > L3G4200D_MODE_NORMAL) { + dev_err(&data->client->dev, "trying to set invalid mode\n"); + count = -EINVAL; + goto exit; + } + /* + * If device is drived to sleep mode in suspend, update mode + * and return + */ + if (data->device_status == L3G4200D_DEVICE_SUSPENDED && + mode == L3G4200D_MODE_OFF) { + data->mode = mode; + goto exit; + } + + /* if same mode as existing, return */ + if (data->mode == mode) + goto exit; + + /* Enable the regulator if it is not turned ON earlier */ + if (data->device_status == L3G4200D_DEVICE_OFF || + data->device_status == L3G4200D_DEVICE_SUSPENDED) + l3g4200d_enable(data); + + dev_dbg(dev, "set operating mode to %lu\n", mode); + error = l3g4200d_set_mode(data->client, mode); + if (error < 0) { + dev_err(&data->client->dev, "Error in setting the mode\n"); + count = -EINVAL; + goto exit; + } + + data->mode = mode; + + /* If mode is OFF then disable the regulator */ + if (data->mode == L3G4200D_MODE_OFF) { + /* fall back to default values */ + data->rate = L3G4200D_RATE_100; + data->range = L3G4200D_ODR_MIN_VAL; + l3g4200d_disable(data); + } +exit: + mutex_unlock(&data->lock); + return count; +} + +static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 800"); + +static ssize_t set_sampling_frequency(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct l3g4200d_data *data = iio_priv(dev_get_drvdata(dev)); + unsigned long rate = 0; + int err; + + /* Perform read/write operation, only when device is active */ + err = is_device_on(data); + if (err) + return -EINVAL; + + if (strncmp(buf, "100" , 3) == 0) + rate = L3G4200D_RATE_100; + + else if (strncmp(buf, "200" , 3) == 0) + rate = L3G4200D_RATE_200; + + else if (strncmp(buf, "400" , 3) == 0) + rate = L3G4200D_RATE_400; + + else if (strncmp(buf, "800" , 3) == 0) + rate = L3G4200D_RATE_800; + else + return -EINVAL; + + mutex_lock(&data->lock); + + if (l3g4200d_set_rate(data->client, rate)) { + dev_err(&data->client->dev, "set rate failed\n"); + count = -EINVAL; + goto exit; + } + data->rate = rate; + +exit: + mutex_unlock(&data->lock); + return count; +} + +/* sampling frequency - output rate in Hz */ +static const char * const reg_to_rate[] = { + "100", + "100", + "100", + "100", + "200", + "200", + "200", + "200", + "400", + "400", + "400", + "400", + "800", + "800", + "800", + "800" +}; + +static ssize_t show_sampling_frequency(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct l3g4200d_data *data = iio_priv(dev_get_drvdata(dev)); + + return sprintf(buf, "%s\n", reg_to_rate[data->rate]); +} + +static IIO_CONST_ATTR(gyro_xyz_scale_available, "8750000, 17500000, 70000000"); + +static const int xyz_to_nanoscale[] = { + 8750000, 17500000, 70000000 +}; + +static const char const scale_to_range[] = { + L3G4200D_RANGE_250, + L3G4200D_RANGE_500, + L3G4200D_RANGE_2000, +}; + +static int l3g4200d_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, + long mask) +{ + struct l3g4200d_data *data = iio_priv(indio_dev); + int ret = -EINVAL, i; + bool flag = false; + char end; + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + ret = is_device_on(data); + if (ret) + return -EINVAL; + mutex_lock(&data->lock); + end = ARRAY_SIZE(xyz_to_nanoscale); + for (i = 0; i < end; i++) { + if (val == xyz_to_nanoscale[i]) { + flag = true; + break; + } + } + if (flag) { + ret = l3g4200d_set_range(data->client, scale_to_range[i]); + data->range = i; + } + mutex_unlock(&data->lock); + default: + break; + } + return ret; +} + +static int l3g4200d_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct l3g4200d_data *data = iio_priv(indio_dev); + switch (mask) { + case 0: + switch (chan->type) { + case IIO_ANGL_VEL: + return l3g4200d_xyz_read(indio_dev, + chan->address, val); + case IIO_TEMP: + return get_gyrotemp(indio_dev, chan->address , val); + default: + break; + } + break; + case IIO_CHAN_INFO_SCALE: + switch (chan->type) { + case IIO_ANGL_VEL: + *val = 0; + /* scale for X/Y and Z are different */ + *val2 = xyz_to_nanoscale[data->range]; + return IIO_VAL_INT_PLUS_NANO; + default: + break; + } + break; + default: + break; + } + return -EINVAL; +} + +#define L3G4200D_CHANNEL(axis, addr) \ + { \ + .type = IIO_ANGL_VEL, \ + .modified = 1, \ + .channel2 = IIO_MOD_##axis, \ + .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \ + .address = addr, \ + } + +#define L3G4200D_TEMP_CHANNEL(addr) \ + { \ + .type = IIO_TEMP, \ + .indexed = 1, \ + .channel = 0, \ + .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \ + .address = addr, \ + } + +static const struct iio_chan_spec l3g4200d_channels[] = { + L3G4200D_CHANNEL(X, L3G4200D_OUT_X_L_A), + L3G4200D_CHANNEL(Y, L3G4200D_OUT_Y_L_A), + L3G4200D_CHANNEL(Z, L3G4200D_OUT_Z_L_A), + L3G4200D_TEMP_CHANNEL(L3G4200D_OUT_TEMP), +}; + +static IIO_DEVICE_ATTR(gyro_raw, + S_IRUGO, + l3g4200d_readdata, + NULL, + L3G4200D_OUT_X_L_A); +static IIO_DEVICE_ATTR(sampling_frequency, + S_IWUGO | S_IRUGO, + show_sampling_frequency, + set_sampling_frequency, + L3G4200D_CTRL_REG1); +static IIO_DEVICE_ATTR(mode, + S_IWUGO | S_IRUGO, + show_operating_mode, + set_operating_mode, + L3G4200D_CTRL_REG1); + +static struct attribute *l3g4200d_attributes[] = { + &iio_dev_attr_mode.dev_attr.attr, + &iio_dev_attr_sampling_frequency.dev_attr.attr, + &iio_dev_attr_gyro_raw.dev_attr.attr, + &iio_const_attr_sampling_frequency_available.dev_attr.attr, + &iio_const_attr_gyro_xyz_scale_available.dev_attr.attr, + NULL +}; + +static const struct attribute_group l3g4200d_group = { + .attrs = l3g4200d_attributes, +}; + +static const struct iio_info l3g4200d_info = { + .attrs = &l3g4200d_group, + .read_raw = &l3g4200d_read_raw, + .write_raw = &l3g4200d_write_raw, + .driver_module = THIS_MODULE, +}; + +static void l3g4200d_setup(struct l3g4200d_data *data) +{ + /* set mode */ + l3g4200d_set_mode(data->client, data->mode); + /* set rate */ + l3g4200d_set_rate(data->client, data->rate); + /* set range */ + l3g4200d_set_range(data->client, scale_to_range[data->range]); +} + +#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) +static int l3g4200d_suspend(struct device *dev) +{ + struct l3g4200d_data *data = iio_priv(dev_get_drvdata(dev)); + int ret = 0; + + if (data->mode == L3G4200D_MODE_OFF) + return 0; + + mutex_lock(&data->lock); + + /* Set the device to sleep mode */ + l3g4200d_set_mode(data->client, L3G4200D_MODE_OFF); + + /* Disable regulator */ + l3g4200d_disable(data); + + data->device_status = L3G4200D_DEVICE_SUSPENDED; + + mutex_unlock(&data->lock); + + return ret; +} + +static int l3g4200d_resume(struct device *dev) +{ + struct l3g4200d_data *data = iio_priv(dev_get_drvdata(dev)); + int ret = 0; + + + if (data->device_status == L3G4200D_DEVICE_ON || + data->device_status == L3G4200D_DEVICE_OFF) { + return 0; + } + mutex_lock(&data->lock); + + /* Enable regulator */ + l3g4200d_enable(data); + + /* Set mode,rate and range */ + l3g4200d_setup(data); + + mutex_unlock(&data->lock); + return ret; +} + +static const struct dev_pm_ops l3g4200d_dev_pm_ops = { + .suspend = l3g4200d_suspend, + .resume = l3g4200d_resume, +}; +#endif +#ifdef CONFIG_HAS_EARLYSUSPEND +static void l3g4200d_early_suspend(struct early_suspend *data) +{ + struct l3g4200d_data *ddata = + container_of(data, struct l3g4200d_data, early_suspend); + + if (ddata->mode == L3G4200D_MODE_OFF) + return; + + mutex_lock(&ddata->lock); + + /* Set the device to sleep mode */ + l3g4200d_set_mode(ddata->client, L3G4200D_MODE_OFF); + + /* Disable regulator */ + l3g4200d_disable(ddata); + + ddata->device_status = L3G4200D_DEVICE_SUSPENDED; + + mutex_unlock(&ddata->lock); + +} + +static void l3g4200d_late_resume(struct early_suspend *data) +{ + struct l3g4200d_data *ddata = + container_of(data, struct l3g4200d_data, early_suspend); + + if (ddata->device_status == L3G4200D_DEVICE_ON || + ddata->device_status == L3G4200D_DEVICE_OFF) { + return; + } + mutex_lock(&ddata->lock); + + /* Enable regulator */ + l3g4200d_enable(ddata); + + /* Set mode,rate and range */ + l3g4200d_setup(ddata); + + mutex_unlock(&ddata->lock); + +} +#endif + +static int l3g4200d_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct l3g4200d_data *data; + struct iio_dev *indio_dev; + int err = 0; + + indio_dev = iio_allocate_device(sizeof(*data)); + if (indio_dev == NULL) { + dev_err(&client->dev, "memory allocation failed\n"); + err = -ENOMEM; + goto exit; + } + data = iio_priv(indio_dev); + + data->mode = L3G4200D_MODE_OFF; + data->range = L3G4200D_RANGE_250; + data->rate = L3G4200D_ODR_MIN_VAL; + data->device_status = L3G4200D_DEVICE_OFF; + data->client = client; + + /* check for valid platform data */ + if (!client->dev.platform_data) { + dev_err(&client->dev, "Invalid platform data\n"); + err = -ENOMEM; + goto exit1; + } + data->pdata = client->dev.platform_data; + + i2c_set_clientdata(client, indio_dev); + + data->regulator = regulator_get(&client->dev, "vdd"); + if (IS_ERR(data->regulator)) { + err = PTR_ERR(data->regulator); + dev_err(&client->dev, "failed to get regulator = %d\n", err); + goto exit1; + } + /* Enable regulator */ + l3g4200d_enable(data); + + mutex_init(&data->lock); + + err = i2c_smbus_read_byte_data(client, L3G4200D_WHO_AM_I); + if (err < 0) { + dev_err(&client->dev, "failed to read of the chip\n"); + goto exit2; + } + if (err == WHOAMI_L3G4200D || err == WHOAMI_L3GD20) + dev_info(&client->dev, + "3-Axis Gyroscope device identification: %d\n", + err); + else { + dev_info(&client->dev, + "Gyroscope identification did not match\n"); + goto exit2; + } + + l3g4200d_setup(data); + + indio_dev->info = &l3g4200d_info; + indio_dev->name = id->name; + indio_dev->dev.parent = &client->dev; + indio_dev->channels = l3g4200d_channels; + indio_dev->num_channels = ARRAY_SIZE(l3g4200d_channels); + indio_dev->modes = INDIO_DIRECT_MODE; + + err = iio_device_register(indio_dev); + if (err) + goto exit2; + +#ifdef CONFIG_HAS_EARLYSUSPEND + data->early_suspend.level = + EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; + data->early_suspend.suspend = l3g4200d_early_suspend; + data->early_suspend.resume = l3g4200d_late_resume; + register_early_suspend(&data->early_suspend); +#endif + + /* Disable regulator */ + l3g4200d_disable(data); + + return 0; + +exit2: + iio_free_device(indio_dev); + mutex_destroy(&data->lock); + l3g4200d_disable(data); + regulator_put(data->regulator); +exit1: + kfree(data); +exit: + return err; +} + +static int __devexit l3g4200d_remove(struct i2c_client *client) +{ + struct iio_dev *indio_dev = i2c_get_clientdata(client); + struct l3g4200d_data *data = iio_priv(indio_dev); + int ret; + + /* safer to make device off */ + if (data->mode != L3G4200D_MODE_OFF) { + /* set mode to off */ + ret = l3g4200d_set_mode(client, L3G4200D_MODE_OFF); + if (ret < 0) { + dev_err(&client->dev, + "could not turn off the device %d", + ret); + return ret; + } + if (data->device_status == L3G4200D_DEVICE_ON) { + regulator_disable(data->regulator); + data->device_status = L3G4200D_DEVICE_OFF; + } + } + regulator_put(data->regulator); + mutex_destroy(&data->lock); + iio_device_unregister(indio_dev); + iio_free_device(indio_dev); + kfree(data); + return 0; +} + +static const struct i2c_device_id l3g4200d_id[] = { + { "l3g4200d", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, l3g4200d_id); + +static struct i2c_driver l3g4200d_driver = { + .driver = { + .name = "l3g4200d", + #if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) + .pm = &l3g4200d_dev_pm_ops, + #endif + }, + .id_table = l3g4200d_id, + .probe = l3g4200d_probe, + .remove = l3g4200d_remove, +}; + +module_i2c_driver(l3g4200d_driver); + +MODULE_DESCRIPTION("l3g4200d Gyroscope Driver"); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Naga Radhesh Y "); diff --git a/drivers/staging/iio/magnetometer/Kconfig b/drivers/staging/iio/magnetometer/Kconfig index 722c4e13f71..708279d559c 100644 --- a/drivers/staging/iio/magnetometer/Kconfig +++ b/drivers/staging/iio/magnetometer/Kconfig @@ -24,4 +24,15 @@ config SENSORS_HMC5843 To compile this driver as a module, choose M here: the module will be called hmc5843 +config SENSORS_LSM303DLH + tristate "STMicroelectronics LSM303DLH 3-Axis Magnetometer" + depends on I2C + default n + help + Say Y here to add support for the STMicroelectronics + LSM303DLH 3-Axis Magnetometer. + + To compile this driver as a module, choose M here: the module + will be called lsm303dlh. + endmenu diff --git a/drivers/staging/iio/magnetometer/Makefile b/drivers/staging/iio/magnetometer/Makefile index f2a753f8079..39cc6e997e8 100644 --- a/drivers/staging/iio/magnetometer/Makefile +++ b/drivers/staging/iio/magnetometer/Makefile @@ -4,3 +4,4 @@ obj-$(CONFIG_SENSORS_AK8975) += ak8975.o obj-$(CONFIG_SENSORS_HMC5843) += hmc5843.o +obj-$(CONFIG_SENSORS_LSM303DLH) += lsm303dlh.o diff --git a/drivers/staging/iio/magnetometer/lsm303dlh.c b/drivers/staging/iio/magnetometer/lsm303dlh.c new file mode 100644 index 00000000000..076b60ba163 --- /dev/null +++ b/drivers/staging/iio/magnetometer/lsm303dlh.c @@ -0,0 +1,929 @@ +/* + * Copyright (C) ST-Ericsson SA 2012 + * License Terms: GNU General Public License, version 2 + * + * This code is mostly based on hmc5843 driver + * + * Author: Srinidhi Kasagar + */ +#include +#include +#include +#include +#include +#include +#include +#ifdef CONFIG_HAS_EARLYSUSPEND +#include +#endif +#include + +#include "../iio.h" +#include "../sysfs.h" + +/* configuration register A */ +#define LSM303DLH_M_CRA_REG 0x00 +/* configuration register B */ +#define LSM303DLH_M_CRB_REG 0x01 +/* mode register */ +#define LSM303DLH_M_MR_REG 0x02 +/* data output X register */ +#define LSM303DLH_M_OUT_X 0x03 +/* data output Y register */ +#define LSM303DLH_M_OUT_Y 0x05 +/* data output Z register */ +#define LSM303DLH_M_OUT_Z 0x07 +/* status register */ +#define LSM303DLH_M_SR_REG 0x09 +/* identification registers */ +#define LSM303DLH_M_IRA_REG 0x0A +#define LSM303DLH_M_IRB_REG 0x0B +#define LSM303DLH_M_IRC_REG 0x0C + +/* control register A, Data Output rate */ +#define LSM303DLH_M_CRA_DO_BIT 2 +#define LSM303DLH_M_CRA_DO_MASK (0x7 << LSM303DLH_M_CRA_DO_BIT) +/* control register A, measurement configuration */ +#define LSM303DLH_M_CRA_MS_BIT 0 +#define LSM303DLH_M_CRA_MS_MASK (0x3 << LSM303DLH_M_CRA_MS_BIT) +/* control register B, gain configuration */ +#define LSM303DLH_M_CRB_GN_BIT 5 +#define LSM303DLH_M_CRB_GN_MASK (0x7 << LSM303DLH_M_CRB_GN_BIT) +/* mode register */ +#define LSM303DLH_M_MR_MD_BIT 0 +#define LSM303DLH_M_MR_MD_MASK (0x3 << LSM303DLH_M_MR_MD_BIT) +/* status register, ready */ +#define LSM303DLH_M_SR_RDY_BIT 0 +#define LSM303DLH_M_SR_RDY_MASK (0x1 << LSM303DLH_M_SR_RDY_BIT) +/* status register, data output register lock */ +#define LSM303DLH_M_SR_LOC_BIT 1 +#define LSM303DLH_M_SR_LOC_MASK (0x1 << LSM303DLH_M_SR_LOC_BIT) +/* status register, regulator enabled */ +#define LSM303DLH_M_SR_REN_BIT 2 +#define LSM303DLH_M_SR_REN_MASK (0x1 << LSM303DLH_M_SR_REN_BIT) + +/* + * Control register gain settings + *--------------------------------------------- + *GN2 | GN1| GN0|sensor input| Gain X/Y | Gain Z| + * 0 | 0 | 1 | +/-1.3 | 1055 | 950 | + * 0 | 1 | 0 | +/-1.9 | 795 | 710 | + * 0 | 1 | 1 | +/-2.5 | 635 | 570 | + * 1 | 0 | 0 | +/-4.0 | 430 | 385 | + * 1 | 0 | 1 | +/-4.7 | 375 | 335 | + * 1 | 1 | 0 | +/-5.6 | 320 | 285 | + * 1 | 1 | 1 | +/-8.1 | 230 | 205 | + *--------------------------------------------- + */ +#define LSM303DLH_M_RANGE_1_3G 0x01 +#define LSM303DLH_M_RANGE_1_9G 0x02 +#define LSM303DLH_M_RANGE_2_5G 0x03 +#define LSM303DLH_M_RANGE_4_0G 0x04 +#define LSM303DLH_M_RANGE_4_7G 0x05 +#define LSM303DLH_M_RANGE_5_6G 0x06 +#define LSM303DLH_M_RANGE_8_1G 0x07 + +/* + * CRA register data output rate settings + * + * DO2 DO1 DO0 Minimum data output rate (Hz) + * 0 0 0 0.75 + * 0 0 1 1.5 + * 0 1 0 3.0 + * 0 1 1 7.5 + * 1 0 0 15 + * 1 0 1 30 + * 1 1 0 75 + * 1 1 1 Not used + */ +#define LSM303DLH_M_RATE_00_75 0x00 +#define LSM303DLH_M_RATE_01_50 0x01 +#define LSM303DLH_M_RATE_03_00 0x02 +#define LSM303DLH_M_RATE_07_50 0x03 +#define LSM303DLH_M_RATE_15_00 0x04 +#define LSM303DLH_M_RATE_30_00 0x05 +#define LSM303DLH_M_RATE_75_00 0x06 +#define LSM303DLH_M_RATE_RESERVED 0x07 + +/* device status defines */ +#define LSM303DLH_M_DEVICE_OFF 0 +#define LSM303DLH_M_DEVICE_ON 1 +#define LSM303DLH_M_DEVICE_SUSPENDED 2 + +#define LSM303DLH_M_NORMAL_CFG 0x00 +#define LSM303DLH_M_POSITIVE_BIAS_CFG 0x01 +#define LSM303DLH_M_NEGATIVE_BIAS_CFG 0x02 +#define LSM303DLH_M_NOT_USED_CFG 0x03 + +/* Magnetic sensor operating mode */ +#define LSM303DLH_M_CONTINUOUS_CONVERSION_MODE 0x00 +#define LSM303DLH_M_SINGLE_CONVERSION_MODE 0x01 +#define LSM303DLH_M_UNUSED_MODE 0x02 +#define LSM303DLH_M_SLEEP_MODE 0x03 + +/* Multiple byte transfer enable */ +#define LSM303DLH_MULTIPLE_I2C_TR 0x80 +#define LSM303DLH_M_DATA_RDY 0x01 + +/* device CHIP ID defines */ +#define LSM303DLHC_CHIP_ID 51 + +/* + * The scaling frequencies are different + * for LSM303DLH and LSM303DLHC + * the number of elments of scaling frequency + * is 50 and hence set this as the array size + */ +#define XY_LENGTH 50 +#define Z_LENGTH 50 + +char xy_scale_avail[XY_LENGTH]; +char z_scale_avail[Z_LENGTH]; + +/* + * struct lsm303dlh_m_data - data structure used by lsm303dlh_m driver + * @client: i2c client + * @lock: mutex lock for sysfs operations + * @regulator: regulator + * @early_suspend: early suspend structure + * @pdata: lsm303dlh platform data pointer + * @device_status: device is ON, OFF or SUSPENDED + * @mode: current mode of operation + * @rate: current sampling rate + * @config: device configuration + * @range: current range value of magnetometer + */ + +struct lsm303dlh_m_data { + struct i2c_client *client; + struct mutex lock; + struct regulator *regulator; +#ifdef CONFIG_HAS_EARLYSUSPEND + struct early_suspend early_suspend; +#endif + struct lsm303dlh_platform_data *pdata; + int device_status; + u8 mode; + u8 rate; + u8 config; + u8 range; +}; + +#ifdef CONFIG_HAS_EARLYSUSPEND +static void lsm303dlh_m_early_suspend(struct early_suspend *data); +static void lsm303dlh_m_late_resume(struct early_suspend *data); +#endif + +static s32 lsm303dlh_config(struct i2c_client *client, u8 mode) +{ + /* the lower two bits indicates the magnetic sensor mode */ + return i2c_smbus_write_byte_data(client, + LSM303DLH_M_MR_REG, mode & 0x03); +} + +static inline int is_device_on(struct lsm303dlh_m_data *data) +{ + struct i2c_client *client = data->client; + /* Perform read/write operation only when device is active */ + if (data->device_status != LSM303DLH_M_DEVICE_ON) { + dev_err(&client->dev, + "device is switched off, make it on using mode"); + return -EINVAL; + } + + return 0; +} + +/* disable regulator and update status */ +static int lsm303dlh_m_disable(struct lsm303dlh_m_data *data) +{ + data->device_status = LSM303DLH_M_DEVICE_OFF; + + regulator_disable(data->regulator); + + return 0; +} + +/* enable regulator and update status */ +static int lsm303dlh_m_enable(struct lsm303dlh_m_data *data) +{ + data->device_status = LSM303DLH_M_DEVICE_ON; + + regulator_enable(data->regulator); + + return 0; +} + +/* + * To read output x/y/z data register, + * in this case x,y and z are not + * mapped w.r.t board orientation. + * Reading just raw data from device + */ +static ssize_t lsm303dlh_m_xyz_read(struct iio_dev *indio_dev, + int address, + int *buf) +{ + struct lsm303dlh_m_data *data = iio_priv(indio_dev); + int ret; + + /* Perform read/write operation, only when device is active */ + ret = is_device_on(data); + if (ret) + return -EINVAL; + + mutex_lock(&data->lock); + + ret = i2c_smbus_read_byte_data(data->client, LSM303DLH_M_SR_REG); + + /* wait till data is written to all six registers */ + while (!(ret & LSM303DLH_M_DATA_RDY)) + ret = i2c_smbus_read_byte_data(data->client, + LSM303DLH_M_SR_REG); + + ret = i2c_smbus_read_word_swapped(data->client, address); + + if (ret < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + + mutex_unlock(&data->lock); + + *buf = (s16)ret; + + return IIO_VAL_INT; +} + +/* + * To read output x,y,z data register. + * After reading change x,y and z values + * w.r.t the orientation of the device. + */ +static ssize_t lsm303dlh_m_readdata(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev)); + struct lsm303dlh_platform_data *pdata = data->pdata; + struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); + u8 map_x = pdata->axis_map_x; + u8 map_y = pdata->axis_map_y; + u8 map_z = pdata->axis_map_z; + int ret; + unsigned char magn_data[6]; + s16 val[3]; + + /* Perform read/write operation, only when device is active */ + ret = is_device_on(data); + if (ret) + return -EINVAL; + + mutex_lock(&data->lock); + + ret = i2c_smbus_read_byte_data(data->client, LSM303DLH_M_SR_REG); + + /* wait till data is written to all six registers */ + while (!(ret & LSM303DLH_M_DATA_RDY)) + ret = i2c_smbus_read_byte_data(data->client, + LSM303DLH_M_SR_REG); + + ret = i2c_smbus_read_i2c_block_data(data->client, + this_attr->address | + LSM303DLH_MULTIPLE_I2C_TR, + 6, magn_data); + + if (ret < 0) { + dev_err(&data->client->dev, "reading xyz failed\n"); + mutex_unlock(&data->lock); + return -EINVAL; + } + + /* MSB is at lower address */ + val[0] = (s16) + (((magn_data[0]) << 8) | magn_data[1]); + val[1] = (s16) + (((magn_data[2]) << 8) | magn_data[3]); + val[2] = (s16) + (((magn_data[4]) << 8) | magn_data[5]); + /* check if chip is DHLC */ + if (data->pdata->chip_id == LSM303DLHC_CHIP_ID) + /* + * the out registers are in x, z and y order + * so swap y and z values + */ + swap(val[1], val[2]); + /* modify the x,y and z values w.r.t orientation of device*/ + if (pdata->negative_x) + val[map_x] = -val[map_x]; + if (pdata->negative_y) + val[map_y] = -val[map_y]; + if (pdata->negative_z) + val[map_z] = -val[map_z]; + mutex_unlock(&data->lock); + + return sprintf(buf, "%d:%d:%d:%lld\n", val[map_x], val[map_y], + val[map_z], iio_get_time_ns()); +} + +static ssize_t show_operating_mode(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev)); + + return sprintf(buf, "%d\n", data->mode); +} + +static ssize_t set_operating_mode(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev)); + struct i2c_client *client = data->client; + struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); + int error; + unsigned long mode = 0; + + mutex_lock(&data->lock); + + error = kstrtoul(buf, 10, &mode); + if (error) { + count = error; + goto exit; + } + + if (mode > LSM303DLH_M_SLEEP_MODE) { + dev_err(&client->dev, "trying to set invalid mode\n"); + count = -EINVAL; + goto exit; + } + + /* + * If device is driven to sleep mode in suspend, update mode + * and return + */ + if (data->device_status == LSM303DLH_M_DEVICE_SUSPENDED && + mode == LSM303DLH_M_SLEEP_MODE) { + data->mode = mode; + goto exit; + } + + if (data->mode == mode) + goto exit; + + /* Enable the regulator if it is not turned on earlier */ + if (data->device_status == LSM303DLH_M_DEVICE_OFF || + data->device_status == LSM303DLH_M_DEVICE_SUSPENDED) + lsm303dlh_m_enable(data); + + dev_dbg(dev, "set operating mode to %lu\n", mode); + + error = i2c_smbus_write_byte_data(client, this_attr->address, mode); + if (error < 0) { + dev_err(&client->dev, "Error in setting the mode\n"); + count = -EINVAL; + goto exit; + } + + data->mode = mode; + /* if sleep mode, disable the regulator */ + if (data->mode == LSM303DLH_M_SLEEP_MODE) + lsm303dlh_m_disable(data); +exit: + mutex_unlock(&data->lock); + return count; +} + +/* + * Magnetic sensor operating mode: CRA_REG + * ms1 ms0 + * 0 0 Normal measurement configuration + * 0 1 Positive bias configuration. + * 1 0 Negative bias configuration. + * 1 1 This configuration is not used + */ +static s32 lsm303dlh_set_config(struct i2c_client *client, u8 config) +{ + struct lsm303dlh_m_data *data = i2c_get_clientdata(client); + u8 reg_val; + + reg_val = (config & LSM303DLH_M_CRA_MS_MASK) | + (data->rate << LSM303DLH_M_CRA_DO_BIT); + return i2c_smbus_write_byte_data(client, LSM303DLH_M_CRA_REG, reg_val); +} + + +static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("0.75 1.5 3.0 7.5 15 30 75"); + +static s32 lsm303dlh_m_set_range(struct i2c_client *client, u8 range) +{ + u8 reg_val; + + reg_val = range << LSM303DLH_M_CRB_GN_BIT; + + return i2c_smbus_write_byte_data(client, LSM303DLH_M_CRB_REG, reg_val); +} + +static s32 lsm303dlh_m_set_rate(struct i2c_client *client, u8 rate) +{ + struct lsm303dlh_m_data *data = i2c_get_clientdata(client); + u8 reg_val; + + reg_val = (data->config) | (rate << LSM303DLH_M_CRA_DO_BIT); + if (rate >= LSM303DLH_M_RATE_RESERVED) { + dev_err(&client->dev, "given rate not supported\n"); + return -EINVAL; + } + + return i2c_smbus_write_byte_data(client, LSM303DLH_M_CRA_REG, reg_val); +} + +static ssize_t set_sampling_frequency(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev)); + struct i2c_client *client = data->client; + unsigned long rate = 0; + int err; + + err = is_device_on(data); + if (err) + return err; + + if (strncmp(buf, "0.75" , 4) == 0) + rate = LSM303DLH_M_RATE_00_75; + + else if (strncmp(buf, "1.5" , 3) == 0) + rate = LSM303DLH_M_RATE_01_50; + + else if (strncmp(buf, "3.0" , 3) == 0) + rate = LSM303DLH_M_RATE_03_00; + + else if (strncmp(buf, "7.5" , 3) == 0) + rate = LSM303DLH_M_RATE_07_50; + + else if (strncmp(buf, "15" , 2) == 0) + rate = LSM303DLH_M_RATE_15_00; + + else if (strncmp(buf, "30" , 2) == 0) + rate = LSM303DLH_M_RATE_30_00; + + else if (strncmp(buf, "75" , 2) == 0) + rate = LSM303DLH_M_RATE_75_00; + else + return -EINVAL; + + mutex_lock(&data->lock); + + if (lsm303dlh_m_set_rate(client, rate)) { + dev_err(&client->dev, "set rate failed\n"); + count = -EINVAL; + goto exit; + } + data->rate = rate; + +exit: + mutex_unlock(&data->lock); + return count; +} + +/* sampling frequency - output rate in Hz */ +static const char * const reg_to_rate[] = { + "0.75", + "1.5", + "3.0", + "7.5", + "15", + "30", + "75", + "res", +}; + +static int xy_to_nanoscale[] = { + 947870, 1257860, 1574800, 2325580, 2666670, 3125000, 4347830 +}; + +static int z_to_nanoscale[] = { + 1052630, 1408450, 17543820, 2597400, 2985070, 3508770, 4878050 +}; + +static int xy_to_nanoscale_dlhc[] = { + 909090, 1169590, 1492540, 2222220, 2500000, 3030300, 4347830 +}; + +static int z_to_nanoscale_dlhc[] = { + 1020410, 1315790, 1666660, 2500000, 2816900, 3389830, 4878050 +}; + +static const char const scale_to_range[] = { + LSM303DLH_M_RANGE_1_3G, + LSM303DLH_M_RANGE_1_9G, + LSM303DLH_M_RANGE_2_5G, + LSM303DLH_M_RANGE_4_0G, + LSM303DLH_M_RANGE_4_7G, + LSM303DLH_M_RANGE_5_6G, + LSM303DLH_M_RANGE_8_1G +}; +static ssize_t show_sampling_frequency(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev)); + + return sprintf(buf, "%s\n", reg_to_rate[data->rate]); +} + +static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, + show_sampling_frequency, + set_sampling_frequency); + +static IIO_CONST_ATTR(magnet_xy_scale_available, xy_scale_avail); + +static IIO_CONST_ATTR(magnet_z_scale_available, z_scale_avail); + +static int lsm303dlh_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, + long mask) +{ + struct lsm303dlh_m_data *data = iio_priv(indio_dev); + int ret = -EINVAL, i; + bool flag = false; + char end; + int *xy_scale; + int *z_scale; + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + ret = is_device_on(data); + if (ret) + return -EINVAL; + mutex_lock(&data->lock); + + if (data->pdata->chip_id == LSM303DLHC_CHIP_ID) { + xy_scale = xy_to_nanoscale_dlhc; + z_scale = z_to_nanoscale_dlhc; + } else { + xy_scale = xy_to_nanoscale; + z_scale = z_to_nanoscale; + } + end = ARRAY_SIZE(xy_to_nanoscale); + if (chan->address == LSM303DLH_M_OUT_X || + chan->address == LSM303DLH_M_OUT_Y) { + for (i = 0; i < end; i++) { + if (val == xy_scale[i]) { + flag = true; + break; + } + } + } else if (chan->address == LSM303DLH_M_OUT_Z) { + for (i = 0; i < end; i++) { + if (val == z_scale[i]) { + flag = true; + break; + } + } + } + if (flag) { + ret = lsm303dlh_m_set_range(data->client, + scale_to_range[data->range]); + data->range = i; + } + mutex_unlock(&data->lock); + break; + default: + break; + } + return ret; +} + +static int lsm303dlh_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, + long mask) +{ + struct lsm303dlh_m_data *data = iio_priv(indio_dev); + + switch (mask) { + case 0: + return lsm303dlh_m_xyz_read(indio_dev, + chan->address, val); + case IIO_CHAN_INFO_SCALE: + /* scale for X/Y and Z are different */ + if (chan->address == LSM303DLH_M_OUT_X || + chan->address == LSM303DLH_M_OUT_Y){ + /* check if chip is DHLC */ + if (data->pdata->chip_id == LSM303DLHC_CHIP_ID) + *val2 = xy_to_nanoscale_dlhc[data->range]; + else + *val2 = xy_to_nanoscale[data->range]; + } else { + /* check if chip is DHLC */ + if (data->pdata->chip_id == LSM303DLHC_CHIP_ID) + *val2 = z_to_nanoscale_dlhc[data->range]; + else + *val2 = z_to_nanoscale[data->range]; + } + + return IIO_VAL_INT_PLUS_NANO; + default: + break; + } + return -EINVAL; +} + +#define LSM303DLH_CHANNEL(axis, addr) \ + { \ + .type = IIO_MAGN, \ + .modified = 1, \ + .channel2 = IIO_MOD_##axis, \ + .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \ + .address = addr, \ + } + +static const struct iio_chan_spec lsmdlh303_channels[] = { + LSM303DLH_CHANNEL(X, LSM303DLH_M_OUT_X), + LSM303DLH_CHANNEL(Y, LSM303DLH_M_OUT_Y), + LSM303DLH_CHANNEL(Z, LSM303DLH_M_OUT_Z), +}; + +static IIO_DEVICE_ATTR(mode, + S_IWUSR | S_IRUGO, + show_operating_mode, + set_operating_mode, + LSM303DLH_M_MR_REG); +static IIO_DEVICE_ATTR(magn_raw, S_IRUGO, + lsm303dlh_m_readdata, + NULL, + LSM303DLH_M_OUT_X); + +static struct attribute *lsm303dlh_m_attributes[] = { + &iio_dev_attr_mode.dev_attr.attr, + &iio_dev_attr_sampling_frequency.dev_attr.attr, + &iio_dev_attr_magn_raw.dev_attr.attr, + &iio_const_attr_sampling_frequency_available.dev_attr.attr, + &iio_const_attr_magnet_xy_scale_available.dev_attr.attr, + &iio_const_attr_magnet_z_scale_available.dev_attr.attr, + NULL +}; + +static const struct attribute_group lsmdlh303m_group = { + .attrs = lsm303dlh_m_attributes, +}; + +static const struct iio_info lsmdlh303m_info = { + .attrs = &lsmdlh303m_group, + .read_raw = &lsm303dlh_read_raw, + .write_raw = &lsm303dlh_write_raw, + .driver_module = THIS_MODULE, +}; + +static void lsm303dlh_m_setup(struct lsm303dlh_m_data *data) +{ + /* set the magnetic sensor operating mode */ + lsm303dlh_set_config(data->client, data->config); + /* set to the default rate */ + lsm303dlh_m_set_rate(data->client, data->rate); + /* set the magnetic sensor mode */ + lsm303dlh_config(data->client, data->mode); + /* set the range */ + lsm303dlh_m_set_range(data->client, scale_to_range[data->range]); +} + +#if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) +static int lsm303dlh_m_suspend(struct device *dev) +{ + struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev)); + int ret = 0; + + if (data->mode == LSM303DLH_M_SLEEP_MODE) + return 0; + + mutex_lock(&data->lock); + + /* Set the device to sleep mode */ + lsm303dlh_config(data->client, LSM303DLH_M_SLEEP_MODE); + + /* Disable regulator */ + lsm303dlh_m_disable(data); + + data->device_status = LSM303DLH_M_DEVICE_SUSPENDED; + + mutex_unlock(&data->lock); + + return ret; +} + +static int lsm303dlh_m_resume(struct device *dev) +{ + struct lsm303dlh_m_data *data = iio_priv(dev_get_drvdata(dev)); + int ret = 0; + + if (data->device_status == LSM303DLH_M_DEVICE_ON || + data->device_status == LSM303DLH_M_DEVICE_OFF) { + return 0; + } + mutex_lock(&data->lock); + + /* Enable regulator */ + lsm303dlh_m_enable(data); + + /* Setup device parameters */ + lsm303dlh_m_setup(data); + + mutex_unlock(&data->lock); + return ret; +} + +static const struct dev_pm_ops lsm303dlh_m_dev_pm_ops = { + .suspend = lsm303dlh_m_suspend, + .resume = lsm303dlh_m_resume, +}; +#endif +#ifdef CONFIG_HAS_EARLYSUSPEND +static void lsm303dlh_m_early_suspend(struct early_suspend *data) +{ + struct lsm303dlh_m_data *ddata = + container_of(data, struct lsm303dlh_m_data, early_suspend); + + if (ddata->mode == LSM303DLH_M_SLEEP_MODE) + return; + + mutex_lock(&ddata->lock); + + /* Set the device to sleep mode */ + lsm303dlh_config(ddata->client, LSM303DLH_M_SLEEP_MODE); + + /* Disable regulator */ + lsm303dlh_m_disable(ddata); + + ddata->device_status = LSM303DLH_M_DEVICE_SUSPENDED; + + mutex_unlock(&ddata->lock); +} + +static void lsm303dlh_m_late_resume(struct early_suspend *data) +{ + struct lsm303dlh_m_data *ddata = + container_of(data, struct lsm303dlh_m_data, early_suspend); + + if (ddata->device_status == LSM303DLH_M_DEVICE_ON || + ddata->device_status == LSM303DLH_M_DEVICE_OFF) { + return; + } + mutex_lock(&ddata->lock); + + /* Enable regulator */ + lsm303dlh_m_enable(ddata); + + /* Setup device parameters */ + lsm303dlh_m_setup(ddata); + + mutex_unlock(&ddata->lock); + +} +#endif + +static int lsm303dlh_m_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct lsm303dlh_m_data *data; + struct iio_dev *indio_dev; + int err; + + indio_dev = iio_allocate_device(sizeof(*data)); + if (indio_dev == NULL) { + dev_err(&client->dev, "memory allocation failed\n"); + err = -ENOMEM; + goto exit; + } + + data = iio_priv(indio_dev); + + data->mode = LSM303DLH_M_SLEEP_MODE; + data->config = LSM303DLH_M_NORMAL_CFG; + data->range = LSM303DLH_M_RANGE_1_3G; + data->rate = LSM303DLH_M_RATE_00_75; + + data->client = client; + + /* check for valid platform data */ + if (!client->dev.platform_data) { + dev_err(&client->dev, "Invalid platform data\n"); + err = -ENOMEM; + goto exit1; + } + + data->pdata = client->dev.platform_data; + + i2c_set_clientdata(client, indio_dev); + + data->regulator = regulator_get(&client->dev, "vdd"); + if (IS_ERR(data->regulator)) { + dev_err(&client->dev, "failed to get regulator\n"); + err = PTR_ERR(data->regulator); + goto exit1; + } + + /* enable regulators */ + lsm303dlh_m_enable(data); + + lsm303dlh_m_setup(data); + + mutex_init(&data->lock); + + indio_dev->info = &lsmdlh303m_info; + indio_dev->name = id->name; + indio_dev->dev.parent = &client->dev; + indio_dev->channels = lsmdlh303_channels; + indio_dev->num_channels = ARRAY_SIZE(lsmdlh303_channels); + indio_dev->modes = INDIO_DIRECT_MODE; + + err = iio_device_register(indio_dev); + if (err) + goto exit2; + +#ifdef CONFIG_HAS_EARLYSUSPEND + data->early_suspend.level = + EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; + data->early_suspend.suspend = lsm303dlh_m_early_suspend; + data->early_suspend.resume = lsm303dlh_m_late_resume; + register_early_suspend(&data->early_suspend); +#endif + + /* disable regulator */ + lsm303dlh_m_disable(data); + + if (data->pdata->chip_id == LSM303DLHC_CHIP_ID) { + strcpy(xy_scale_avail, "909090, 1169590, 1492540, 2222220, 2500000, 3030300, 4347830"); + strcpy(z_scale_avail, "1020410, 1315790, 1666660, 2500000, 2816900, 3389830, 4878050"); + } else { + strcpy(xy_scale_avail, "947870, 1257860, 1574800, 2325580, 2666670, 3125000, 4347830"); + strcpy(z_scale_avail, "947870, 1257860, 1574800, 2325580, 2666670, 3125000, 4347830"); + } + + return 0; + +exit2: + regulator_disable(data->regulator); + mutex_destroy(&data->lock); + regulator_put(data->regulator); +exit1: + iio_free_device(indio_dev); +exit: + return err; +} + +static int __devexit lsm303dlh_m_remove(struct i2c_client *client) +{ + struct iio_dev *indio_dev = i2c_get_clientdata(client); + struct lsm303dlh_m_data *data = iio_priv(indio_dev); + int ret; + + /* its safe to set the mode to sleep */ + if (data->mode != LSM303DLH_M_SLEEP_MODE) { + ret = lsm303dlh_config(client, LSM303DLH_M_SLEEP_MODE); + if (ret < 0) { + dev_err(&client->dev, + "could not place the device in sleep mode %d", + ret); + return ret; + } + if (data->device_status == LSM303DLH_M_DEVICE_ON) + regulator_disable(data->regulator); + data->device_status = LSM303DLH_M_DEVICE_OFF; + } + regulator_put(data->regulator); + mutex_destroy(&data->lock); + iio_device_unregister(indio_dev); + iio_free_device(indio_dev); + + return 0; +} + +static const struct i2c_device_id lsm303dlh_m_id[] = { + { "lsm303dlh_m", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, lsm303dlh_m_id); + +static struct i2c_driver lsm303dlh_m_driver = { + .driver = { + .name = "lsm303dlh_m", + #if (!defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_PM)) + .pm = &lsm303dlh_m_dev_pm_ops, + #endif + }, + .id_table = lsm303dlh_m_id, + .probe = lsm303dlh_m_probe, + .remove = lsm303dlh_m_remove, +}; + +module_i2c_driver(lsm303dlh_m_driver); + +MODULE_DESCRIPTION("lsm303dlh Magnetometer Driver"); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("srinidhi kasagar "); diff --git a/include/linux/l3g4200d.h b/include/linux/l3g4200d.h index 28459601e4f..7f2b7abee88 100644 --- a/include/linux/l3g4200d.h +++ b/include/linux/l3g4200d.h @@ -10,9 +10,16 @@ #define __L3G4200D_H__ #ifdef __KERNEL__ +/** + * struct l3g4200d_gyr_platform_data - platform datastructure for l3g4200d + * @axis_map_x: x axis position on the hardware, 0 1 or 2 + * @axis_map_y: y axis position on the hardware, 0 1 or 2 + * @axis_map_z: z axis position on the hardware, 0 1 or 2 + * @negative_x: x axis is orientation, 0 or 1 + * @negative_y: y axis is orientation, 0 or 1 + * @negative_z: z axis is orientation, 0 or 1 + */ struct l3g4200d_gyr_platform_data { - const char *name_gyr; - u8 axis_map_x; u8 axis_map_y; u8 axis_map_z; diff --git a/include/linux/lsm303dlh.h b/include/linux/lsm303dlh.h index a565faa79ba..b9a399d4ee3 100644 --- a/include/linux/lsm303dlh.h +++ b/include/linux/lsm303dlh.h @@ -31,8 +31,6 @@ #ifdef __KERNEL__ /** * struct lsm303dlh_platform_data - platform datastructure for lsm303dlh - * @name_a: accelerometer name - * @name_m: magnetometer name * @irq_a1: interrupt line 1 of accelerometer * @irq_a2: interrupt line 2 of accelerometer * @irq_m: interrupt line of magnetometer @@ -45,8 +43,6 @@ * @chip_id: to store ID of the LSM chip */ struct lsm303dlh_platform_data { - const char *name_a; - const char *name_m; u32 irq_a1; u32 irq_a2; u32 irq_m; -- cgit v1.2.3