Skip to contentSkip to conversation

TAOS 258x: Device Driver

PATCH Superseded Jon Brenner 3 participants 6 messages Mar 25, 2011 – Apr 9, 2011

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 TAOS 258x: Device Driver

[PATCH v3] for TAOS tsl2580/81/83 Device driver

Added suspend/resume functions.
Changed attribute names to match existing where applicable and updated or documented new ABI as discussed. Changed integration time ABI from using index (0 to 3) to use actual gain values (1x,8x, etc.). Removed various unused variables, declarations, and functions.
Revised code to accommodate different endianess (le16_to_cpu).
Updated error return codes in various functions.
Changed from mdelay to msleep after determining that longer wait would be acceptable.

---
 drivers/staging/iio/Documentation/sysfs-bus-iio    |    6 +
 .../staging/iio/Documentation/sysfs-bus-iio-light  |   13 +
 .../iio/Documentation/sysfs-bus-iio-light-tsl2583  |   20 +
 drivers/staging/iio/light/Kconfig                  |    9 +
 drivers/staging/iio/light/Makefile                 |    1 +
 drivers/staging/iio/light/tsl2583.c                |  962 ++++++++++++++++++++
 6 files changed, 1011 insertions(+), 0 deletions(-)
drivers/staging/iio/Documentation/sysfs-bus-iio +6 -0
index 2dde97d..6a86ad2 100644
@@ -6,6 +6,12 @@ Description:
 		Corresponds to a grouping of sensor channels. X is the IIO
 		index of the device.
 
+What:		/sys/bus/iio/devices/device[n]/power_state
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the device power state.
+
 What:		/sys/bus/iio/devices/triggerX
 KernelVersion:	2.6.35
 Contact:	[email protected]
drivers/staging/iio/Documentation/sysfs-bus-iio-light +13 -0
index 5d84856..21d2774 100644
@@ -62,3 +62,16 @@ Description:
 		sensing mode. This value should be the output from a reading
 		and if expressed in SI units, should include _input. If this
 		value is not in SI units, then it should include _raw.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the last known external
+		lux measurement used in/for calibration.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_integration_time
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the sensors ADC analog integration time.
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +20 -0
new file mode 100644
index 0000000..660781d
@@ -0,0 +1,20 @@
+What:		/sys/bus/iio/devices/device[n]/lux_table
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the table of coefficients
+		used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_calibrate
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property causes an internal calibration of the als gain trim
+		value which is later used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_input_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property is the known externally illuminance (in lux).
+		It is used in the process of calibrating the device accuracy.
drivers/staging/iio/light/Kconfig +9 -0
index 36d8bbe..55c146c 100644
@@ -13,6 +13,15 @@ config SENSORS_TSL2563
 	 This driver can also be built as a module.  If so, the module
 	 will be called tsl2563.
 
+config TSL2583
+	tristate "TAOS TSL2580, TSL2581, and TSL2583 light-to-digital converters"
+	depends on I2C
+	help
+	  Y = in kernel.
+	  M = as module.
+	  Provides support for the TAOS tsl2580, tsl2581, and tsl2583 devices.
+	  Access ALS data via iio, sysfs.
+
 config SENSORS_ISL29018
         tristate "ISL 29018 light and proximity sensor"
         depends on I2C
drivers/staging/iio/light/Makefile +1 -0
index 9142c0e..8a10815 100644
@@ -3,4 +3,5 @@
 #
 
 obj-$(CONFIG_SENSORS_TSL2563)	+= tsl2563.o
+obj-$(CONFIG_TSL2583)	+= tsl2583.o
 obj-$(CONFIG_SENSORS_ISL29018)	+= isl29018.o
drivers/staging/iio/light/tsl2583.c new file +962 -0

962 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#1 lore raw

Hi Jon,
Some minor comments.

Quoted 269 lines · Jon Brenner

[PATCH v3] for TAOS tsl2580/81/83 Device driver

Added suspend/resume functions.
Changed attribute names to match existing where applicable and updated or documented new ABI as discussed. Changed integration time ABI from using index (0 to 3) to use actual gain values (1x,8x, etc.). Removed various unused variables, declarations, and functions.
Revised code to accommodate different endianess (le16_to_cpu).
Updated error return codes in various functions.
Changed from mdelay to msleep after determining that longer wait would be acceptable.

---
  drivers/staging/iio/Documentation/sysfs-bus-iio    |    6 +
  .../staging/iio/Documentation/sysfs-bus-iio-light  |   13 +
  .../iio/Documentation/sysfs-bus-iio-light-tsl2583  |   20 +
  drivers/staging/iio/light/Kconfig                  |    9 +
  drivers/staging/iio/light/Makefile                 |    1 +
  drivers/staging/iio/light/tsl2583.c                |  962 ++++++++++++++++++++
  6 files changed, 1011 insertions(+), 0 deletions(-)
drivers/staging/iio/Documentation/sysfs-bus-iio +6 -0
index 2dde97d..6a86ad2 100644
@@ -6,6 +6,12 @@ Description:
  		Corresponds to a grouping of sensor channels. X is the IIO
  		index of the device.
 
+What:		/sys/bus/iio/devices/device[n]/power_state
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the device power state.
+
  What:		/sys/bus/iio/devices/triggerX
  KernelVersion:	2.6.35
  Contact:	[email protected]
drivers/staging/iio/Documentation/sysfs-bus-iio-light +13 -0
index 5d84856..21d2774 100644
@@ -62,3 +62,16 @@ Description:
  		sensing mode. This value should be the output from a reading
  		and if expressed in SI units, should include _input. If this
  		value is not in SI units, then it should include _raw.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the last known external
+		lux measurement used in/for calibration.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_integration_time
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the sensors ADC analog integration time.
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +20 -0
new file mode 100644
index 0000000..660781d
@@ -0,0 +1,20 @@
+What:		/sys/bus/iio/devices/device[n]/lux_table
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the table of coefficients
+		used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_calibrate
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property causes an internal calibration of the als gain trim
+		value which is later used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_input_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property is the known externally illuminance (in lux).
+		It is used in the process of calibrating the device accuracy.
drivers/staging/iio/light/Kconfig +9 -0
index 36d8bbe..55c146c 100644
@@ -13,6 +13,15 @@ config SENSORS_TSL2563
  	 This driver can also be built as a module.  If so, the module
  	 will be called tsl2563.
 
+config TSL2583
+	tristate "TAOS TSL2580, TSL2581, and TSL2583 light-to-digital converters"
+	depends on I2C
+	help
+	  Y = in kernel.
+	  M = as module.
+	  Provides support for the TAOS tsl2580, tsl2581, and tsl2583 devices.
+	  Access ALS data via iio, sysfs.
+
  config SENSORS_ISL29018
          tristate "ISL 29018 light and proximity sensor"
          depends on I2C
drivers/staging/iio/light/Makefile +1 -0
index 9142c0e..8a10815 100644
@@ -3,4 +3,5 @@
  #
 
  obj-$(CONFIG_SENSORS_TSL2563)	+= tsl2563.o
+obj-$(CONFIG_TSL2583)	+= tsl2583.o
  obj-$(CONFIG_SENSORS_ISL29018)	+= isl29018.o
drivers/staging/iio/light/tsl2583.c new file +148 -0
new file mode 100644
index 0000000..013a742
@@ -0,0 +1,962 @@
+/*
+ * Device driver for monitoring ambient light intensity (lux)
+ * within the TAOS tsl258x family of devices (tsl2580, tsl2581).
+ *
+ * Copyright (c) 2011, TAOS Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA	02110-1301, USA.
+ */
+
+#include<linux/kernel.h>
+#include<linux/i2c.h>
+#include<linux/errno.h>
+#include<linux/delay.h>
+#include<linux/string.h>
+#include<linux/mutex.h>
+#include<linux/unistd.h>
+#include "../iio.h"
+
+
+#define TSL258X_MAX_DEVICE_REGS		32
+
+/* Triton register offsets */
+#define	TSL258X_REG_MAX		8
+
+/* Device Registers and Masks */
+#define TSL258X_CNTRL			0x00
+#define TSL258X_ALS_TIME		0X01
+#define TSL258X_INTERRUPT		0x02
+#define TSL258X_GAIN			0x07
+#define TSL258X_REVID			0x11
+#define TSL258X_CHIPID			0x12
+#define TSL258X_ALS_CHAN0LO		0x14
+#define TSL258X_ALS_CHAN0HI		0x15
+#define TSL258X_ALS_CHAN1LO		0x16
+#define TSL258X_ALS_CHAN1HI		0x17
+#define TSL258X_TMR_LO			0x18
+#define TSL258X_TMR_HI			0x19
+
+/* tsl2583 cmd reg masks */
+#define TSL258X_CMD_REG			0x80
+#define TSL258X_CMD_SPL_FN		0x60
+#define TSL258X_CMD_ALS_INT_CLR	0X01
+
+/* tsl2583 cntrl reg masks */
+#define TSL258X_CNTL_ADC_ENBL	0x02
+#define TSL258X_CNTL_PWR_ON		0x01
+
+/* tsl2583 status reg masks */
+#define TSL258X_STA_ADC_VALID	0x01
+#define TSL258X_STA_ADC_INTR	0x10
+
+/* Lux calculation constants */
+#define	TSL258X_LUX_CALC_OVER_FLOW		65535
+
+enum {
+	TSL258X_CHIP_UNKNOWN = 0,
+	TSL258X_CHIP_WORKING = 1,
+	TSL258X_CHIP_SUSPENDED = 2
+} TSL258X_CHIP_WORKING_STATUS;
+
+/* Per-device data */
+struct taos_als_info {
+	u16 als_ch0;
+	u16 als_ch1;
+	u16 lux;
+};
+
+struct taos_settings {
+	int als_time;
+	int als_gain;
+	int als_gain_trim;
+	int als_cal_target;
+};
+
+struct tsl2583_chip {
+	struct mutex als_mutex;
+	struct i2c_client *client;
+	struct iio_dev *iio_dev;
+	struct taos_als_info als_cur_info;
+	struct taos_settings taos_settings;
+	int als_time_scale;
+	int als_saturation;
+	int taos_chip_status;
+	u8 taos_config[8];
+};
+
+/*
+ * Initial values for device - this values can/will be changed by driver.
+ * and applications as needed.
+ * These values are dynamic.
+ */
+static const u8 taos_config[8] = {
+		0x00, 0xee, 0x00, 0x03, 0x00, 0xFF, 0xFF, 0x00
+}; /*	cntrl atime intC  Athl0 Athl1 Athh0 Athh1 gain */
+
+struct taos_lux {
+	unsigned int ratio;
+	unsigned int ch0;
+	unsigned int ch1;
+};
+
+/* This structure is intentionally large to accommodate updates via sysfs. */
+/* Sized to 11 = max 10 segments + 1 termination segment */
+/* Assumption is is one and only one type of glass used  */
+struct taos_lux taos_device_lux[11] = {
+	{  9830,  8520, 15729 },
+	{ 12452, 10807, 23344 },
+	{ 14746,  6383, 11705 },
+	{ 17695,  4063,  6554 },
+};
+
+struct gainadj {
+	s16 ch0;
+	s16 ch1;
+};
+
+/* Index = (0 - 3) Used to validate the gain selection index */
+static const struct gainadj gainadj[] = {
+	{ 1, 1 },
+	{ 8, 8 },
+	{ 16, 16 },
+	{ 107, 115 }
+};
+
+/*
+ * Provides initial operational parameter defaults.
+ * These defaults may be changed through the device's sysfs files.
+ */
+static void taos_defaults(struct tsl2583_chip *chip)
+{
+	/* Operational parameters */
+	chip->taos_settings.als_time = 450;
+	/* must be a multiple of 50mS */
+	chip->taos_settings.als_gain = 2;
+	/* this is actually an index into the gain table */
+	/* assume clear glass as default */

drivers/staging/iio/light/tsl2583.c line 158

Quoted 10 lines
drivers/staging/iio/light/tsl2583.c new file +10 -0
+	chip->taos_settings.als_gain_trim = 1000;
+	/* default gain trim to account for aperture effects */
+	chip->taos_settings.als_cal_target = 130;
+	/* Known external ALS reading used for calibration */
+}
+
+/*
+ * Read a number of bytes starting at register (reg) location.
+ * Return 0, or i2c_smbus_write_byte ERROR code.
+ */

Could we use i2c_smbus_read_block_data ?

Quoted 271 lines
drivers/staging/iio/light/tsl2583.c new file +271 -0
+static int
+taos_i2c_read(struct i2c_client *client, u8 reg, u8 *val, unsigned int len)
+{
+	int ret;
+	int i;
+
+	for (i = 0; i<  len; i++) {
+		/* select register to write */
+		ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | reg));
+		if (ret<  0) {
+			dev_err(&client->dev, "taos_i2c_read failed to write"
+				" register %x\n", reg);
+			return ret;
+		}
+		/* read the data */
+		*val = i2c_smbus_read_byte(client);
+		val++;
+		reg++;
+	}
+	return 0;
+}
+
+
+/*
+ * Reads and calculates current lux value.
+ * The raw ch0 and ch1 values of the ambient light sensed in the last
+ * integration cycle are read from the device.
+ * Time scale factor array values are adjusted based on the integration time.
+ * The raw values are multiplied by a scale factor, and device gain is obtained
+ * using gain index. Limit checks are done next, then the ratio of a multiple
+ * of ch1 value, to the ch0 value, is calculated. The array taos_device_lux[]
+ * declared above is then scanned to find the first ratio value that is just
+ * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
+ * the array are then used along with the time scale factor array values, to
+ * calculate the lux.
+ */
+static int taos_get_lux(struct i2c_client *client)
+{
+	u16 ch0, ch1; /* separated ch0/ch1 data from device */
+	u32 lux; /* raw lux calculated from device data */
+	u32 ratio;
+	u8 buf[5];
+	struct taos_lux *p;
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	int i, ret;
+	u32 ch0lux = 0;
+	u32 ch1lux = 0;
+
+	if (mutex_trylock(&chip->als_mutex) == 0) {
+		dev_info(&client->dev, "taos_get_lux device is busy\n");
+		return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
+	}
+
+	if (chip->taos_chip_status != TSL258X_CHIP_WORKING) {
+		/* device is not enabled */
+		dev_err(&client->dev, "taos_get_lux device is not enabled\n");
+		ret = -EBUSY ;
+		goto out_unlock;
+	}
+
+	ret = taos_i2c_read(client, (TSL258X_CMD_REG),&buf[0], 1);
+	if (ret<  0) {
+		dev_err(&client->dev, "taos_get_lux failed to read CMD_REG\n");
+		goto out_unlock;
+	}
+	/* is data new&  valid */
+	if (!(buf[0]&  TSL258X_STA_ADC_INTR)) {
+		dev_err(&client->dev, "taos_get_lux data not valid\n");
+		ret = chip->als_cur_info.lux; /* return LAST VALUE */
+		goto out_unlock;
+	}
+
+	for (i = 0; i<  4; i++) {
+		int reg = TSL258X_CMD_REG | (TSL258X_ALS_CHAN0LO + i);
+		ret = taos_i2c_read(client, reg,&buf[i], 1);
+		if (ret<  0) {
+			dev_err(&client->dev, "taos_get_lux failed to read"
+				" register %x\n", reg);
+			goto out_unlock;
+		}
+	}
+
+	/* clear status, really interrupt status (interrupts are off), but
+	 * we use the bit anyway - don't forget 0x80 - this is a command*/
+	ret = i2c_smbus_write_byte(client,
+		(TSL258X_CMD_REG | TSL258X_CMD_SPL_FN | TSL258X_CMD_ALS_INT_CLR));
+
+	if (ret<  0) {
+		dev_err(&client->dev,
+			"taos_i2c_write_command failed in taos_get_lux, err = %d\n",
+			ret);
+		goto out_unlock; /* have no data, so return failure */
+	}
+
+	/* extract ALS/lux data */
+	ch0 = le16_to_cpup((const __le16 *)&buf[0]);
+	ch1 = le16_to_cpup((const __le16 *)&buf[2]);
+
+	chip->als_cur_info.als_ch0 = ch0;
+	chip->als_cur_info.als_ch1 = ch1;
+
+	if ((ch0>= chip->als_saturation) || (ch1>= chip->als_saturation))
+		goto return_max;
+
+	if (ch0 == 0) {
+		/* have no data, so return LAST VALUE */
+		ret = chip->als_cur_info.lux = 0;
+		goto out_unlock;
+	}
+	/* calculate ratio */
+	ratio = (ch1<<  15) / ch0;
+	/* convert to unscaled lux using the pointer to the table */
+	for (p = (struct taos_lux *) taos_device_lux;
+	     p->ratio != 0&&  p->ratio<  ratio; p++)
+		;
+
+	if (p->ratio == 0) {
+		lux = 0;
+	} else {
+		ch0lux = ((ch0 * p->ch0) +
+			  (gainadj[chip->taos_settings.als_gain].ch0>>  1))
+			 / gainadj[chip->taos_settings.als_gain].ch0;
+		ch1lux = ((ch1 * p->ch1) +
+			  (gainadj[chip->taos_settings.als_gain].ch1>>  1))
+			 / gainadj[chip->taos_settings.als_gain].ch1;
+		lux = ch0lux - ch1lux;
+	}
+
+	/* note: lux is 31 bit max at this point */
+	if (ch1lux>  ch0lux) {
+		dev_dbg(&client->dev, "No Data - Return last value\n");
+		ret = chip->als_cur_info.lux = 0;
+		goto out_unlock;
+	}
+
+	/* adjust for active time scale */
+	if (chip->als_time_scale == 0)
+		lux = 0;
+	else
+		lux = (lux + (chip->als_time_scale>>  1)) /
+			chip->als_time_scale;
+
+	/* adjust for active gain scale */
+	lux>>= 13; /* tables have factor of 8192 builtin for accuracy */
+	lux = (lux * chip->taos_settings.als_gain_trim + 500) / 1000;
+	if (lux>  TSL258X_LUX_CALC_OVER_FLOW) { /* check for overflow */
+return_max:
+		lux = TSL258X_LUX_CALC_OVER_FLOW;
+	}
+
+	/* Update the structure with the latest VALID lux. */
+	chip->als_cur_info.lux = lux;
+	ret = lux;
+
+out_unlock:
+	mutex_unlock(&chip->als_mutex);
+	return ret;
+}
+
+/*
+ * Obtain single reading and calculate the als_gain_trim (later used
+ * to derive actual lux).
+ * Return updated gain_trim value.
+ */
+int taos_als_calibrate(struct i2c_client *client)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	u8 reg_val;
+	unsigned int gain_trim_val;
+	int ret;
+	int lux_val;
+
+	ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | TSL258X_CNTRL));
+	if (ret<  0) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed to reach the CNTRL register, ret=%d\n",
+			ret);
+		return ret;
+	}
+
+	reg_val = i2c_smbus_read_byte(client);
+	if ((reg_val&  (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON))
+			!= (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON)) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed: device not powered on with ADC enabled\n");
+		return -1;
+	}
+
+	ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | TSL258X_CNTRL));
+	if (ret<  0) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed to reach the STATUS register, ret=%d\n",
+			ret);
+		return ret;
+	}
+	reg_val = i2c_smbus_read_byte(client);
+
+	if ((reg_val&  TSL258X_STA_ADC_VALID) != TSL258X_STA_ADC_VALID) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed: STATUS - ADC not valid.\n");
+		return -ENODATA;
+	}
+	lux_val = taos_get_lux(client);
+	if (lux_val<  0) {
+		dev_err(&client->dev, "taos_als_calibrate failed to get lux\n");
+		return lux_val;
+	}
+	gain_trim_val = (unsigned int) (((chip->taos_settings.als_cal_target)
+			* chip->taos_settings.als_gain_trim) / lux_val);
+
+	if ((gain_trim_val<  250) || (gain_trim_val>  4000)) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed: trim_val of %d is out of range\n",
+			gain_trim_val);
+		return -ENODATA;
+	}
+	chip->taos_settings.als_gain_trim = (int) gain_trim_val;
+
+	return (int) gain_trim_val;
+}
+
+/*
+ * Turn the device on.
+ * Configuration must be set before calling this function.
+ */
+static int taos_chip_on(struct i2c_client *client)
+{
+	int i;
+	int ret = 0;
+	u8 *uP;
+	u8 utmp;
+	int als_count;
+	int als_time;
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+
+	/* and make sure we're not already on */
+	if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
+		/* if forcing a register update - turn off, then on */
+		dev_info(&client->dev, "device is already enabled\n");
+		return   -EINVAL;
+	}
+
+	/* determine als integration regster */
+	als_count = (chip->taos_settings.als_time * 100 + 135) / 270;
+	if (als_count == 0)
+		als_count = 1; /* ensure at least one cycle */
+
+	/* convert back to time (encompasses overrides) */
+	als_time = (als_count * 27 + 5) / 10;
+	chip->taos_config[TSL258X_ALS_TIME] = 256 - als_count;
+
+	/* Set the gain based on taos_settings struct */
+	chip->taos_config[TSL258X_GAIN] = chip->taos_settings.als_gain;
+
+	/* set chip struct re scaling and saturation */
+	chip->als_saturation = als_count * 922; /* 90% of full scale */
+	chip->als_time_scale = (als_time + 25) / 50;
+
+	/* TSL258x Specific power-on / adc enable sequence
+	 * Power on the device 1st. */
+	utmp = TSL258X_CNTL_PWR_ON;
+	ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG | TSL258X_CNTRL,
+					utmp);
+	if (ret<  0) {
+		dev_err(&client->dev, "taos_chip_on failed on CNTRL reg.\n");
+		return -1;
+	}
+
+	/* Use the following shadow copy for our delay before enabling ADC.
+	 * Write all the registers. */
+	for (i = 0, uP = chip->taos_config; i<  TSL258X_REG_MAX; i++) {

drivers/staging/iio/light/tsl2583.c line 439

Quoted 10 lines
drivers/staging/iio/light/tsl2583.c new file +10 -0
+		ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG + i,
+						*uP++);
+		if (ret<  0) {
+			dev_err(&client->dev,
+				"taos_chip_on failed on reg %d.\n", i);
+			return -1;
+		}
+	}
+
+	msleep(3);

I think we could have a macro here.

Quoted 523 lines
drivers/staging/iio/light/tsl2583.c new file +523 -0
+	/* NOW enable the ADC
+	 * initialize the desired mode of operation */
+	utmp = TSL258X_CNTL_PWR_ON | TSL258X_CNTL_ADC_ENBL;
+	ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG | TSL258X_CNTRL,
+					utmp);
+	if (ret<  0) {
+		dev_err(&client->dev, "taos_chip_on failed on 2nd CTRL reg.\n");
+		return -1;
+	}
+	chip->taos_chip_status = TSL258X_CHIP_WORKING;
+
+	return ret;
+}
+
+static int taos_chip_off(struct i2c_client *client)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	int ret;
+
+	/* turn device off */
+	chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
+	ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG | TSL258X_CNTRL,
+					0x00);
+	return ret;
+}
+
+/* Sysfs Interface Functions */
+static ssize_t taos_device_id(struct device *dev,
+struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%s\n", chip->client->name);
+}
+
+static ssize_t taos_power_state_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%d\n", chip->taos_chip_status);
+}
+
+static ssize_t taos_power_state_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if (value == 0)
+		taos_chip_off(chip->client);
+	else
+		taos_chip_on(chip->client);
+
+	return len;
+}
+
+static ssize_t taos_gain_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	char gain[4] = {0};
+
+	switch (chip->taos_settings.als_gain) {
+	case 0:
+		strcpy(gain, "001");
+		break;
+	case 1:
+		strcpy(gain, "008");
+		break;
+	case 2:
+		strcpy(gain, "016");
+		break;
+	case 3:
+		strcpy(gain, "111");
+		break;
+	}
+
+	return sprintf(buf, "%s\n", gain);
+}
+
+static ssize_t taos_gain_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	switch (value) {
+	case 1:
+		chip->taos_settings.als_gain = 0;
+		break;
+	case 8:
+		chip->taos_settings.als_gain = 1;
+		break;
+	case 16:
+		chip->taos_settings.als_gain = 2;
+		break;
+	case 111:
+		chip->taos_settings.als_gain = 3;
+		break;
+	default:
+		dev_err(dev, "Invalid Gain Index (must be 1,8,16,111)\n");
+		return -1;
+	}
+
+	return len;
+}
+
+static ssize_t taos_gain_available_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%s\n", "1 8 16 111");
+}
+
+static ssize_t taos_als_time_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%d\n", chip->taos_settings.als_time);
+}
+
+static ssize_t taos_als_time_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if ((value<  50) || (value>  650))
+		return -EINVAL;
+
+	if (value % 50)
+		return -EINVAL;
+
+	 chip->taos_settings.als_time = value;
+
+	return len;
+}
+
+static ssize_t taos_als_time_available_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%s\n",
+		"50 100 150 200 250 300 350 400 450 500 550 600 650");
+}
+
+static ssize_t taos_als_trim_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%d\n", chip->taos_settings.als_gain_trim);
+}
+
+static ssize_t taos_als_trim_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if (value)
+		chip->taos_settings.als_gain_trim = value;
+
+	return len;
+}
+
+static ssize_t taos_als_cal_target_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%d\n", chip->taos_settings.als_cal_target);
+}
+
+static ssize_t taos_als_cal_target_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if (value)
+		chip->taos_settings.als_cal_target = value;
+
+	return len;
+}
+
+static ssize_t taos_lux_show(struct device *dev, struct device_attribute *attr,
+	char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	int lux;
+
+	lux = taos_get_lux(chip->client);
+
+	return sprintf(buf, "%d\n", lux);
+}
+
+static ssize_t taos_do_calibrate(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if (value == 1)
+		taos_als_calibrate(chip->client);
+
+	return len;
+}
+
+static ssize_t taos_luxtable_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	int i;
+	int offset = 0;
+
+	for (i = 0; i<  ARRAY_SIZE(taos_device_lux); i++) {
+		offset += sprintf(buf + offset, "%d,%d,%d,",
+				  taos_device_lux[i].ratio,
+				  taos_device_lux[i].ch0,
+				  taos_device_lux[i].ch1);
+		if (taos_device_lux[i].ratio == 0) {
+			/* We just printed the first "0" entry.
+			 * Now get rid of the extra "," and break. */
+			offset--;
+			break;
+		}
+	}
+
+	offset += sprintf(buf + offset, "\n");
+	return offset;
+}
+
+static ssize_t taos_luxtable_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	int value[ARRAY_SIZE(taos_device_lux)];
+	int n;
+
+	get_options(buf, ARRAY_SIZE(value), value);
+
+	/* We now have an array of ints starting at value[1], and
+	 * enumerated by value[0].
+	 * We expect each group of three ints is one table entry,
+	 * and the last table entry is all 0.
+	 */
+	n = value[0];
+	if ((n % 3) || n<  6 || n>  (ARRAY_SIZE(taos_device_lux) - 3)) {
+		dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
+		return -EINVAL;
+	}
+	if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
+		dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
+		return -EINVAL;
+	}
+
+	if (chip->taos_chip_status == TSL258X_CHIP_WORKING)
+		taos_chip_off(chip->client);
+
+	/* Zero out the table */
+	memset(taos_device_lux, 0, sizeof(taos_device_lux));
+	memcpy(taos_device_lux,&value[1], (value[0] * 4));
+
+	taos_chip_on(chip->client);
+
+	return len;
+}
+
+static DEVICE_ATTR(name, S_IRUGO, taos_device_id, NULL);
+static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
+		taos_power_state_show, taos_power_state_store);
+
+static DEVICE_ATTR(illuminance0_calibscale, S_IRUGO | S_IWUSR,
+		taos_gain_show, taos_gain_store);
+static DEVICE_ATTR(illuminance0_calibscale_available, S_IRUGO,
+		taos_gain_available_show, NULL);
+
+static DEVICE_ATTR(illuminance0_integration_time, S_IRUGO | S_IWUSR,
+		taos_als_time_show, taos_als_time_store);
+static DEVICE_ATTR(illuminance0_integration_time_available, S_IRUGO,
+		taos_als_time_available_show, NULL);
+
+static DEVICE_ATTR(illuminance0_calibbias, S_IRUGO | S_IWUSR,
+		taos_als_trim_show, taos_als_trim_store);
+
+static DEVICE_ATTR(illuminance0_input_target, S_IRUGO | S_IWUSR,
+		taos_als_cal_target_show, taos_als_cal_target_store);
+
+static DEVICE_ATTR(illuminance0_input, S_IRUGO, taos_lux_show, NULL);
+static DEVICE_ATTR(illuminance0_calibrate, S_IWUSR, NULL, taos_do_calibrate);
+static DEVICE_ATTR(illuminance0_lux_table, S_IRUGO | S_IWUSR,
+		taos_luxtable_show, taos_luxtable_store);
+
+static struct attribute *sysfs_attrs_ctrl[] = {
+	&dev_attr_name.attr,
+	&dev_attr_power_state.attr,
+	&dev_attr_illuminance0_calibscale.attr,			/* Gain  */
+	&dev_attr_illuminance0_calibscale_available.attr,
+	&dev_attr_illuminance0_integration_time.attr,	/* I time*/
+	&dev_attr_illuminance0_integration_time_available.attr,
+	&dev_attr_illuminance0_calibbias.attr,			/* trim  */
+	&dev_attr_illuminance0_input_target.attr,
+	&dev_attr_illuminance0_input.attr,
+	&dev_attr_illuminance0_calibrate.attr,
+	&dev_attr_illuminance0_lux_table.attr,
+	NULL
+};
+
+static struct attribute_group tsl2583_attribute_group = {
+	.attrs = sysfs_attrs_ctrl,
+};
+
+/* Use the default register values to identify the Taos device */
+static int taos_tsl258x_device(unsigned char *bufp)
+{
+	return ((bufp[TSL258X_CHIPID]&  0xf0) == 0x90);
+}
+
+/*
+ * Client probe function - When a valid device is found, the driver's device
+ * data structure is updated, and initialization completes successfully.
+ */
+static int __devinit taos_probe(struct i2c_client *clientp,
+		      const struct i2c_device_id *idp)
+{
+	int i, ret = 0;
+	unsigned char buf[TSL258X_MAX_DEVICE_REGS];
+	static struct tsl2583_chip *chip;
+
+	if (!i2c_check_functionality(clientp->adapter,
+		I2C_FUNC_SMBUS_BYTE_DATA)) {
+		dev_err(&clientp->dev,
+			"taos_probe() - i2c smbus byte data "
+			"functions unsupported\n");
+		return -EOPNOTSUPP;
+	}
+
+	chip = kzalloc(sizeof(struct tsl2583_chip), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
+
+	chip->client = clientp;
+	i2c_set_clientdata(clientp, chip);
+
+	mutex_init(&chip->als_mutex);
+	chip->taos_chip_status = TSL258X_CHIP_UNKNOWN;
+	memcpy(chip->taos_config, taos_config, sizeof(chip->taos_config));
+
+	for (i = 0; i<  TSL258X_MAX_DEVICE_REGS; i++) {
+		ret = i2c_smbus_write_byte(clientp,
+				(TSL258X_CMD_REG | (TSL258X_CNTRL + i)));
+		if (ret<  0) {
+			dev_err(&clientp->dev, "i2c_smbus_write_bytes() to cmd "
+				"reg failed in taos_probe(), err = %d\n", ret);
+			goto fail1;
+		}
+		ret = i2c_smbus_read_byte(clientp);
+		if (ret<  0) {
+			dev_err(&clientp->dev, "i2c_smbus_read_byte from "
+				"reg failed in taos_probe(), err = %d\n", ret);
+
+			goto fail1;
+		}
+		buf[i] = ret;
+	}
+
+	if (!taos_tsl258x_device(buf)) {
+		dev_info(&clientp->dev, "i2c device found but does not match "
+			"expected id in taos_probe()\n");
+		goto fail1;
+	}
+
+	ret = i2c_smbus_write_byte(clientp, (TSL258X_CMD_REG | TSL258X_CNTRL));
+	if (ret<  0) {
+		dev_err(&clientp->dev, "i2c_smbus_write_byte() to cmd reg "
+			"failed in taos_probe(), err = %d\n", ret);
+		goto fail1;
+	}
+
+	chip->iio_dev = iio_allocate_device();
+	if (!chip->iio_dev) {
+		ret = -ENOMEM;
+		dev_err(&clientp->dev, "iio allocation failed\n");
+		goto fail1;
+	}
+
+	chip->iio_dev->attrs =&tsl2583_attribute_group;
+	chip->iio_dev->dev.parent =&clientp->dev;
+	chip->iio_dev->dev_data = (void *)(chip);
+	chip->iio_dev->driver_module = THIS_MODULE;
+	chip->iio_dev->modes = INDIO_DIRECT_MODE;
+	ret = iio_device_register(chip->iio_dev);
+	if (ret) {
+		dev_err(&clientp->dev, "iio registration failed\n");
+		goto fail1;
+	}
+
+	/* Load up the V2 defaults (these are hard coded defaults for now) */
+	taos_defaults(chip);
+
+	/* Make sure the chip is on */
+	taos_chip_on(clientp);
+
+	dev_info(&clientp->dev, "Light sensor found.\n");
+
+	return 0;
+
+fail1:
+	kfree(chip);
+
+	return ret;
+}
+
+static int taos_suspend(struct i2c_client *client, pm_message_t state)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	int ret = 0;
+
+	mutex_lock(&chip->als_mutex);
+
+	if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
+		ret = taos_chip_off(client);
+		chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
+	}
+
+	mutex_unlock(&chip->als_mutex);
+	return ret;
+}
+
+static int taos_resume(struct i2c_client *client)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	int ret = 0;
+
+	mutex_lock(&chip->als_mutex);
+
+	if (chip->taos_chip_status == TSL258X_CHIP_SUSPENDED)
+		ret = taos_chip_on(client);
+
+	mutex_unlock(&chip->als_mutex);
+	return ret;
+}
+
+
+static int __devexit taos_remove(struct i2c_client *client)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+
+	iio_device_unregister(chip->iio_dev);
+
+	kfree(chip);
+	return 0;
+}
+
+static struct i2c_device_id taos_idtable[] = {
+	{ "tsl2580", 0 },
+	{ "tsl2581", 1 },
+	{ "tsl2583", 2 },
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, taos_idtable);
+
+/* Driver definition */
+static struct i2c_driver taos_driver = {
+	.driver = {
+		.name = "tsl2583",
+	},
+	.id_table = taos_idtable,
+	.suspend	= taos_suspend,
+	.resume		= taos_resume,
+	.probe = taos_probe,
+	.remove = __devexit_p(taos_remove),
+};
+
+static int __init taos_init(void)
+{
+	return i2c_add_driver(&taos_driver);
+}
+
+static void __exit taos_exit(void)
+{
+	i2c_del_driver(&taos_driver);
+}
+
+module_init(taos_init);
+module_exit(taos_exit);
+
+MODULE_AUTHOR("J. August Brenner<[email protected]>");
+MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
+MODULE_LICENSE("GPL");
+
#2 lore raw

HI Shubhrajyoti:
Thanks for taking time to review the code. Please see my response to your comments throughout.

Jon

Quoted 276 lines · Shubhrajyoti

Hi Jon,
Some minor comments.

Quoted 272 lines · Jon Brenner

[PATCH v3] for TAOS tsl2580/81/83 Device driver

Added suspend/resume functions.
Changed attribute names to match existing where applicable and updated or documented new ABI as discussed. Changed integration time ABI from using index (0 to 3) to use actual gain values (1x,8x, etc.). Removed various unused variables, declarations, and functions.
Revised code to accommodate different endianess (le16_to_cpu).
Updated error return codes in various functions.
Changed from mdelay to msleep after determining that longer wait would be acceptable.

---
  drivers/staging/iio/Documentation/sysfs-bus-iio    |    6 +
  .../staging/iio/Documentation/sysfs-bus-iio-light  |   13 +
  .../iio/Documentation/sysfs-bus-iio-light-tsl2583  |   20 +
  drivers/staging/iio/light/Kconfig                  |    9 +
  drivers/staging/iio/light/Makefile                 |    1 +
  drivers/staging/iio/light/tsl2583.c                |  962 ++++++++++++++++++++
  6 files changed, 1011 insertions(+), 0 deletions(-)
drivers/staging/iio/Documentation/sysfs-bus-iio +6 -0
index 2dde97d..6a86ad2 100644
@@ -6,6 +6,12 @@ Description:
  		Corresponds to a grouping of sensor channels. X is the IIO
  		index of the device.
 
+What:		/sys/bus/iio/devices/device[n]/power_state
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the device power state.
+
  What:		/sys/bus/iio/devices/triggerX
  KernelVersion:	2.6.35
  Contact:	[email protected]
drivers/staging/iio/Documentation/sysfs-bus-iio-light +13 -0
index 5d84856..21d2774 100644
@@ -62,3 +62,16 @@ Description:
  		sensing mode. This value should be the output from a reading
  		and if expressed in SI units, should include _input. If this
  		value is not in SI units, then it should include _raw.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the last known external
+		lux measurement used in/for calibration.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_integration_time
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the sensors ADC analog integration time.
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +20 -0
new file mode 100644
index 0000000..660781d
@@ -0,0 +1,20 @@
+What:		/sys/bus/iio/devices/device[n]/lux_table
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the table of coefficients
+		used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_calibrate
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property causes an internal calibration of the als gain trim
+		value which is later used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_input_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property is the known externally illuminance (in lux).
+		It is used in the process of calibrating the device accuracy.
drivers/staging/iio/light/Kconfig +9 -0
index 36d8bbe..55c146c 100644
@@ -13,6 +13,15 @@ config SENSORS_TSL2563
  	 This driver can also be built as a module.  If so, the module
  	 will be called tsl2563.
 
+config TSL2583
+	tristate "TAOS TSL2580, TSL2581, and TSL2583 light-to-digital converters"
+	depends on I2C
+	help
+	  Y = in kernel.
+	  M = as module.
+	  Provides support for the TAOS tsl2580, tsl2581, and tsl2583 devices.
+	  Access ALS data via iio, sysfs.
+
  config SENSORS_ISL29018
          tristate "ISL 29018 light and proximity sensor"
          depends on I2C
drivers/staging/iio/light/Makefile +1 -0
index 9142c0e..8a10815 100644
@@ -3,4 +3,5 @@
  #
 
  obj-$(CONFIG_SENSORS_TSL2563)	+= tsl2563.o
+obj-$(CONFIG_TSL2583)	+= tsl2583.o
  obj-$(CONFIG_SENSORS_ISL29018)	+= isl29018.o
drivers/staging/iio/light/tsl2583.c new file +151 -0
new file mode 100644
index 0000000..013a742
@@ -0,0 +1,962 @@
+/*
+ * Device driver for monitoring ambient light intensity (lux)
+ * within the TAOS tsl258x family of devices (tsl2580, tsl2581).
+ *
+ * Copyright (c) 2011, TAOS Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA	02110-1301, USA.
+ */
+
+#include<linux/kernel.h>
+#include<linux/i2c.h>
+#include<linux/errno.h>
+#include<linux/delay.h>
+#include<linux/string.h>
+#include<linux/mutex.h>
+#include<linux/unistd.h>
+#include "../iio.h"
+
+
+#define TSL258X_MAX_DEVICE_REGS		32
+
+/* Triton register offsets */
+#define	TSL258X_REG_MAX		8
+
+/* Device Registers and Masks */
+#define TSL258X_CNTRL			0x00
+#define TSL258X_ALS_TIME		0X01
+#define TSL258X_INTERRUPT		0x02
+#define TSL258X_GAIN			0x07
+#define TSL258X_REVID			0x11
+#define TSL258X_CHIPID			0x12
+#define TSL258X_ALS_CHAN0LO		0x14
+#define TSL258X_ALS_CHAN0HI		0x15
+#define TSL258X_ALS_CHAN1LO		0x16
+#define TSL258X_ALS_CHAN1HI		0x17
+#define TSL258X_TMR_LO			0x18
+#define TSL258X_TMR_HI			0x19
+
+/* tsl2583 cmd reg masks */
+#define TSL258X_CMD_REG			0x80
+#define TSL258X_CMD_SPL_FN		0x60
+#define TSL258X_CMD_ALS_INT_CLR	0X01
+
+/* tsl2583 cntrl reg masks */
+#define TSL258X_CNTL_ADC_ENBL	0x02
+#define TSL258X_CNTL_PWR_ON		0x01
+
+/* tsl2583 status reg masks */
+#define TSL258X_STA_ADC_VALID	0x01
+#define TSL258X_STA_ADC_INTR	0x10
+
+/* Lux calculation constants */
+#define	TSL258X_LUX_CALC_OVER_FLOW		65535
+
+enum {
+	TSL258X_CHIP_UNKNOWN = 0,
+	TSL258X_CHIP_WORKING = 1,
+	TSL258X_CHIP_SUSPENDED = 2
+} TSL258X_CHIP_WORKING_STATUS;
+
+/* Per-device data */
+struct taos_als_info {
+	u16 als_ch0;
+	u16 als_ch1;
+	u16 lux;
+};
+
+struct taos_settings {
+	int als_time;
+	int als_gain;
+	int als_gain_trim;
+	int als_cal_target;
+};
+
+struct tsl2583_chip {
+	struct mutex als_mutex;
+	struct i2c_client *client;
+	struct iio_dev *iio_dev;
+	struct taos_als_info als_cur_info;
+	struct taos_settings taos_settings;
+	int als_time_scale;
+	int als_saturation;
+	int taos_chip_status;
+	u8 taos_config[8];
+};
+
+/*
+ * Initial values for device - this values can/will be changed by driver.
+ * and applications as needed.
+ * These values are dynamic.
+ */
+static const u8 taos_config[8] = {
+		0x00, 0xee, 0x00, 0x03, 0x00, 0xFF, 0xFF, 0x00
+}; /*	cntrl atime intC  Athl0 Athl1 Athh0 Athh1 gain */
+
+struct taos_lux {
+	unsigned int ratio;
+	unsigned int ch0;
+	unsigned int ch1;
+};
+
+/* This structure is intentionally large to accommodate updates via sysfs. */
+/* Sized to 11 = max 10 segments + 1 termination segment */
+/* Assumption is is one and only one type of glass used  */
+struct taos_lux taos_device_lux[11] = {
+	{  9830,  8520, 15729 },
+	{ 12452, 10807, 23344 },
+	{ 14746,  6383, 11705 },
+	{ 17695,  4063,  6554 },
+};
+
+struct gainadj {
+	s16 ch0;
+	s16 ch1;
+};
+
+/* Index = (0 - 3) Used to validate the gain selection index */
+static const struct gainadj gainadj[] = {
+	{ 1, 1 },
+	{ 8, 8 },
+	{ 16, 16 },
+	{ 107, 115 }
+};
+
+/*
+ * Provides initial operational parameter defaults.
+ * These defaults may be changed through the device's sysfs files.
+ */
+static void taos_defaults(struct tsl2583_chip *chip)
+{
+	/* Operational parameters */
+	chip->taos_settings.als_time = 450;
+	/* must be a multiple of 50mS */
+	chip->taos_settings.als_gain = 2;
+	/* this is actually an index into the gain table */
+	/* assume clear glass as default */
+	chip->taos_settings.als_gain_trim = 1000;
+	/* default gain trim to account for aperture effects */
+	chip->taos_settings.als_cal_target = 130;
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c line 158

Quoted 7 lines
drivers/staging/iio/light/tsl2583.c new file +7 -0
+	/* Known external ALS reading used for calibration */
+}
+
+/*
+ * Read a number of bytes starting at register (reg) location.
+ * Return 0, or i2c_smbus_write_byte ERROR code.
+ */

Could we use i2c_smbus_read_block_data ?

NO -Many controllers no longer seem to support block mode read/write. To use block mode reads could exclude some.

Quoted 274 lines
Quoted 274 lines
drivers/staging/iio/light/tsl2583.c new file +274 -0
+static int
+taos_i2c_read(struct i2c_client *client, u8 reg, u8 *val, unsigned int len)
+{
+	int ret;
+	int i;
+
+	for (i = 0; i<  len; i++) {
+		/* select register to write */
+		ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | reg));
+		if (ret<  0) {
+			dev_err(&client->dev, "taos_i2c_read failed to write"
+				" register %x\n", reg);
+			return ret;
+		}
+		/* read the data */
+		*val = i2c_smbus_read_byte(client);
+		val++;
+		reg++;
+	}
+	return 0;
+}
+
+
+/*
+ * Reads and calculates current lux value.
+ * The raw ch0 and ch1 values of the ambient light sensed in the last
+ * integration cycle are read from the device.
+ * Time scale factor array values are adjusted based on the integration time.
+ * The raw values are multiplied by a scale factor, and device gain is obtained
+ * using gain index. Limit checks are done next, then the ratio of a multiple
+ * of ch1 value, to the ch0 value, is calculated. The array taos_device_lux[]
+ * declared above is then scanned to find the first ratio value that is just
+ * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
+ * the array are then used along with the time scale factor array values, to
+ * calculate the lux.
+ */
+static int taos_get_lux(struct i2c_client *client)
+{
+	u16 ch0, ch1; /* separated ch0/ch1 data from device */
+	u32 lux; /* raw lux calculated from device data */
+	u32 ratio;
+	u8 buf[5];
+	struct taos_lux *p;
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	int i, ret;
+	u32 ch0lux = 0;
+	u32 ch1lux = 0;
+
+	if (mutex_trylock(&chip->als_mutex) == 0) {
+		dev_info(&client->dev, "taos_get_lux device is busy\n");
+		return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
+	}
+
+	if (chip->taos_chip_status != TSL258X_CHIP_WORKING) {
+		/* device is not enabled */
+		dev_err(&client->dev, "taos_get_lux device is not enabled\n");
+		ret = -EBUSY ;
+		goto out_unlock;
+	}
+
+	ret = taos_i2c_read(client, (TSL258X_CMD_REG),&buf[0], 1);
+	if (ret<  0) {
+		dev_err(&client->dev, "taos_get_lux failed to read CMD_REG\n");
+		goto out_unlock;
+	}
+	/* is data new&  valid */
+	if (!(buf[0]&  TSL258X_STA_ADC_INTR)) {
+		dev_err(&client->dev, "taos_get_lux data not valid\n");
+		ret = chip->als_cur_info.lux; /* return LAST VALUE */
+		goto out_unlock;
+	}
+
+	for (i = 0; i<  4; i++) {
+		int reg = TSL258X_CMD_REG | (TSL258X_ALS_CHAN0LO + i);
+		ret = taos_i2c_read(client, reg,&buf[i], 1);
+		if (ret<  0) {
+			dev_err(&client->dev, "taos_get_lux failed to read"
+				" register %x\n", reg);
+			goto out_unlock;
+		}
+	}
+
+	/* clear status, really interrupt status (interrupts are off), but
+	 * we use the bit anyway - don't forget 0x80 - this is a command*/
+	ret = i2c_smbus_write_byte(client,
+		(TSL258X_CMD_REG | TSL258X_CMD_SPL_FN | TSL258X_CMD_ALS_INT_CLR));
+
+	if (ret<  0) {
+		dev_err(&client->dev,
+			"taos_i2c_write_command failed in taos_get_lux, err = %d\n",
+			ret);
+		goto out_unlock; /* have no data, so return failure */
+	}
+
+	/* extract ALS/lux data */
+	ch0 = le16_to_cpup((const __le16 *)&buf[0]);
+	ch1 = le16_to_cpup((const __le16 *)&buf[2]);
+
+	chip->als_cur_info.als_ch0 = ch0;
+	chip->als_cur_info.als_ch1 = ch1;
+
+	if ((ch0>= chip->als_saturation) || (ch1>= chip->als_saturation))
+		goto return_max;
+
+	if (ch0 == 0) {
+		/* have no data, so return LAST VALUE */
+		ret = chip->als_cur_info.lux = 0;
+		goto out_unlock;
+	}
+	/* calculate ratio */
+	ratio = (ch1<<  15) / ch0;
+	/* convert to unscaled lux using the pointer to the table */
+	for (p = (struct taos_lux *) taos_device_lux;
+	     p->ratio != 0&&  p->ratio<  ratio; p++)
+		;
+
+	if (p->ratio == 0) {
+		lux = 0;
+	} else {
+		ch0lux = ((ch0 * p->ch0) +
+			  (gainadj[chip->taos_settings.als_gain].ch0>>  1))
+			 / gainadj[chip->taos_settings.als_gain].ch0;
+		ch1lux = ((ch1 * p->ch1) +
+			  (gainadj[chip->taos_settings.als_gain].ch1>>  1))
+			 / gainadj[chip->taos_settings.als_gain].ch1;
+		lux = ch0lux - ch1lux;
+	}
+
+	/* note: lux is 31 bit max at this point */
+	if (ch1lux>  ch0lux) {
+		dev_dbg(&client->dev, "No Data - Return last value\n");
+		ret = chip->als_cur_info.lux = 0;
+		goto out_unlock;
+	}
+
+	/* adjust for active time scale */
+	if (chip->als_time_scale == 0)
+		lux = 0;
+	else
+		lux = (lux + (chip->als_time_scale>>  1)) /
+			chip->als_time_scale;
+
+	/* adjust for active gain scale */
+	lux>>= 13; /* tables have factor of 8192 builtin for accuracy */
+	lux = (lux * chip->taos_settings.als_gain_trim + 500) / 1000;
+	if (lux>  TSL258X_LUX_CALC_OVER_FLOW) { /* check for overflow */
+return_max:
+		lux = TSL258X_LUX_CALC_OVER_FLOW;
+	}
+
+	/* Update the structure with the latest VALID lux. */
+	chip->als_cur_info.lux = lux;
+	ret = lux;
+
+out_unlock:
+	mutex_unlock(&chip->als_mutex);
+	return ret;
+}
+
+/*
+ * Obtain single reading and calculate the als_gain_trim (later used
+ * to derive actual lux).
+ * Return updated gain_trim value.
+ */
+int taos_als_calibrate(struct i2c_client *client)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	u8 reg_val;
+	unsigned int gain_trim_val;
+	int ret;
+	int lux_val;
+
+	ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | TSL258X_CNTRL));
+	if (ret<  0) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed to reach the CNTRL register, ret=%d\n",
+			ret);
+		return ret;
+	}
+
+	reg_val = i2c_smbus_read_byte(client);
+	if ((reg_val&  (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON))
+			!= (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON)) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed: device not powered on with ADC enabled\n");
+		return -1;
+	}
+
+	ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | TSL258X_CNTRL));
+	if (ret<  0) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed to reach the STATUS register, ret=%d\n",
+			ret);
+		return ret;
+	}
+	reg_val = i2c_smbus_read_byte(client);
+
+	if ((reg_val&  TSL258X_STA_ADC_VALID) != TSL258X_STA_ADC_VALID) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed: STATUS - ADC not valid.\n");
+		return -ENODATA;
+	}
+	lux_val = taos_get_lux(client);
+	if (lux_val<  0) {
+		dev_err(&client->dev, "taos_als_calibrate failed to get lux\n");
+		return lux_val;
+	}
+	gain_trim_val = (unsigned int) (((chip->taos_settings.als_cal_target)
+			* chip->taos_settings.als_gain_trim) / lux_val);
+
+	if ((gain_trim_val<  250) || (gain_trim_val>  4000)) {
+		dev_err(&client->dev,
+			"taos_als_calibrate failed: trim_val of %d is out of range\n",
+			gain_trim_val);
+		return -ENODATA;
+	}
+	chip->taos_settings.als_gain_trim = (int) gain_trim_val;
+
+	return (int) gain_trim_val;
+}
+
+/*
+ * Turn the device on.
+ * Configuration must be set before calling this function.
+ */
+static int taos_chip_on(struct i2c_client *client)
+{
+	int i;
+	int ret = 0;
+	u8 *uP;
+	u8 utmp;
+	int als_count;
+	int als_time;
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+
+	/* and make sure we're not already on */
+	if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
+		/* if forcing a register update - turn off, then on */
+		dev_info(&client->dev, "device is already enabled\n");
+		return   -EINVAL;
+	}
+
+	/* determine als integration regster */
+	als_count = (chip->taos_settings.als_time * 100 + 135) / 270;
+	if (als_count == 0)
+		als_count = 1; /* ensure at least one cycle */
+
+	/* convert back to time (encompasses overrides) */
+	als_time = (als_count * 27 + 5) / 10;
+	chip->taos_config[TSL258X_ALS_TIME] = 256 - als_count;
+
+	/* Set the gain based on taos_settings struct */
+	chip->taos_config[TSL258X_GAIN] = chip->taos_settings.als_gain;
+
+	/* set chip struct re scaling and saturation */
+	chip->als_saturation = als_count * 922; /* 90% of full scale */
+	chip->als_time_scale = (als_time + 25) / 50;
+
+	/* TSL258x Specific power-on / adc enable sequence
+	 * Power on the device 1st. */
+	utmp = TSL258X_CNTL_PWR_ON;
+	ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG | TSL258X_CNTRL,
+					utmp);
+	if (ret<  0) {
+		dev_err(&client->dev, "taos_chip_on failed on CNTRL reg.\n");
+		return -1;
+	}
+
+	/* Use the following shadow copy for our delay before enabling ADC.
+	 * Write all the registers. */
+	for (i = 0, uP = chip->taos_config; i<  TSL258X_REG_MAX; i++) {
+		ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG + i,
+						*uP++);
+		if (ret<  0) {
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c line 439

Quoted 7 lines
drivers/staging/iio/light/tsl2583.c new file +7 -0
+			dev_err(&client->dev,
+				"taos_chip_on failed on reg %d.\n", i);
+			return -1;
+		}
+	}
+
+	msleep(3);

I think we could have a macro here.

What macro would you suggest?

Quoted 524 lines
Quoted 523 lines
drivers/staging/iio/light/tsl2583.c new file +523 -0
+	/* NOW enable the ADC
+	 * initialize the desired mode of operation */
+	utmp = TSL258X_CNTL_PWR_ON | TSL258X_CNTL_ADC_ENBL;
+	ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG | TSL258X_CNTRL,
+					utmp);
+	if (ret<  0) {
+		dev_err(&client->dev, "taos_chip_on failed on 2nd CTRL reg.\n");
+		return -1;
+	}
+	chip->taos_chip_status = TSL258X_CHIP_WORKING;
+
+	return ret;
+}
+
+static int taos_chip_off(struct i2c_client *client)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	int ret;
+
+	/* turn device off */
+	chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
+	ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG | TSL258X_CNTRL,
+					0x00);
+	return ret;
+}
+
+/* Sysfs Interface Functions */
+static ssize_t taos_device_id(struct device *dev,
+struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%s\n", chip->client->name);
+}
+
+static ssize_t taos_power_state_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%d\n", chip->taos_chip_status);
+}
+
+static ssize_t taos_power_state_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if (value == 0)
+		taos_chip_off(chip->client);
+	else
+		taos_chip_on(chip->client);
+
+	return len;
+}
+
+static ssize_t taos_gain_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	char gain[4] = {0};
+
+	switch (chip->taos_settings.als_gain) {
+	case 0:
+		strcpy(gain, "001");
+		break;
+	case 1:
+		strcpy(gain, "008");
+		break;
+	case 2:
+		strcpy(gain, "016");
+		break;
+	case 3:
+		strcpy(gain, "111");
+		break;
+	}
+
+	return sprintf(buf, "%s\n", gain);
+}
+
+static ssize_t taos_gain_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	switch (value) {
+	case 1:
+		chip->taos_settings.als_gain = 0;
+		break;
+	case 8:
+		chip->taos_settings.als_gain = 1;
+		break;
+	case 16:
+		chip->taos_settings.als_gain = 2;
+		break;
+	case 111:
+		chip->taos_settings.als_gain = 3;
+		break;
+	default:
+		dev_err(dev, "Invalid Gain Index (must be 1,8,16,111)\n");
+		return -1;
+	}
+
+	return len;
+}
+
+static ssize_t taos_gain_available_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%s\n", "1 8 16 111");
+}
+
+static ssize_t taos_als_time_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%d\n", chip->taos_settings.als_time);
+}
+
+static ssize_t taos_als_time_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if ((value<  50) || (value>  650))
+		return -EINVAL;
+
+	if (value % 50)
+		return -EINVAL;
+
+	 chip->taos_settings.als_time = value;
+
+	return len;
+}
+
+static ssize_t taos_als_time_available_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%s\n",
+		"50 100 150 200 250 300 350 400 450 500 550 600 650");
+}
+
+static ssize_t taos_als_trim_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%d\n", chip->taos_settings.als_gain_trim);
+}
+
+static ssize_t taos_als_trim_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if (value)
+		chip->taos_settings.als_gain_trim = value;
+
+	return len;
+}
+
+static ssize_t taos_als_cal_target_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%d\n", chip->taos_settings.als_cal_target);
+}
+
+static ssize_t taos_als_cal_target_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if (value)
+		chip->taos_settings.als_cal_target = value;
+
+	return len;
+}
+
+static ssize_t taos_lux_show(struct device *dev, struct device_attribute *attr,
+	char *buf)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	int lux;
+
+	lux = taos_get_lux(chip->client);
+
+	return sprintf(buf, "%d\n", lux);
+}
+
+static ssize_t taos_do_calibrate(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	unsigned long value;
+
+	if (strict_strtoul(buf, 0,&value))
+		return -EINVAL;
+
+	if (value == 1)
+		taos_als_calibrate(chip->client);
+
+	return len;
+}
+
+static ssize_t taos_luxtable_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	int i;
+	int offset = 0;
+
+	for (i = 0; i<  ARRAY_SIZE(taos_device_lux); i++) {
+		offset += sprintf(buf + offset, "%d,%d,%d,",
+				  taos_device_lux[i].ratio,
+				  taos_device_lux[i].ch0,
+				  taos_device_lux[i].ch1);
+		if (taos_device_lux[i].ratio == 0) {
+			/* We just printed the first "0" entry.
+			 * Now get rid of the extra "," and break. */
+			offset--;
+			break;
+		}
+	}
+
+	offset += sprintf(buf + offset, "\n");
+	return offset;
+}
+
+static ssize_t taos_luxtable_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+	int value[ARRAY_SIZE(taos_device_lux)];
+	int n;
+
+	get_options(buf, ARRAY_SIZE(value), value);
+
+	/* We now have an array of ints starting at value[1], and
+	 * enumerated by value[0].
+	 * We expect each group of three ints is one table entry,
+	 * and the last table entry is all 0.
+	 */
+	n = value[0];
+	if ((n % 3) || n<  6 || n>  (ARRAY_SIZE(taos_device_lux) - 3)) {
+		dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
+		return -EINVAL;
+	}
+	if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
+		dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
+		return -EINVAL;
+	}
+
+	if (chip->taos_chip_status == TSL258X_CHIP_WORKING)
+		taos_chip_off(chip->client);
+
+	/* Zero out the table */
+	memset(taos_device_lux, 0, sizeof(taos_device_lux));
+	memcpy(taos_device_lux,&value[1], (value[0] * 4));
+
+	taos_chip_on(chip->client);
+
+	return len;
+}
+
+static DEVICE_ATTR(name, S_IRUGO, taos_device_id, NULL);
+static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
+		taos_power_state_show, taos_power_state_store);
+
+static DEVICE_ATTR(illuminance0_calibscale, S_IRUGO | S_IWUSR,
+		taos_gain_show, taos_gain_store);
+static DEVICE_ATTR(illuminance0_calibscale_available, S_IRUGO,
+		taos_gain_available_show, NULL);
+
+static DEVICE_ATTR(illuminance0_integration_time, S_IRUGO | S_IWUSR,
+		taos_als_time_show, taos_als_time_store);
+static DEVICE_ATTR(illuminance0_integration_time_available, S_IRUGO,
+		taos_als_time_available_show, NULL);
+
+static DEVICE_ATTR(illuminance0_calibbias, S_IRUGO | S_IWUSR,
+		taos_als_trim_show, taos_als_trim_store);
+
+static DEVICE_ATTR(illuminance0_input_target, S_IRUGO | S_IWUSR,
+		taos_als_cal_target_show, taos_als_cal_target_store);
+
+static DEVICE_ATTR(illuminance0_input, S_IRUGO, taos_lux_show, NULL);
+static DEVICE_ATTR(illuminance0_calibrate, S_IWUSR, NULL, taos_do_calibrate);
+static DEVICE_ATTR(illuminance0_lux_table, S_IRUGO | S_IWUSR,
+		taos_luxtable_show, taos_luxtable_store);
+
+static struct attribute *sysfs_attrs_ctrl[] = {
+	&dev_attr_name.attr,
+	&dev_attr_power_state.attr,
+	&dev_attr_illuminance0_calibscale.attr,			/* Gain  */
+	&dev_attr_illuminance0_calibscale_available.attr,
+	&dev_attr_illuminance0_integration_time.attr,	/* I time*/
+	&dev_attr_illuminance0_integration_time_available.attr,
+	&dev_attr_illuminance0_calibbias.attr,			/* trim  */
+	&dev_attr_illuminance0_input_target.attr,
+	&dev_attr_illuminance0_input.attr,
+	&dev_attr_illuminance0_calibrate.attr,
+	&dev_attr_illuminance0_lux_table.attr,
+	NULL
+};
+
+static struct attribute_group tsl2583_attribute_group = {
+	.attrs = sysfs_attrs_ctrl,
+};
+
+/* Use the default register values to identify the Taos device */
+static int taos_tsl258x_device(unsigned char *bufp)
+{
+	return ((bufp[TSL258X_CHIPID]&  0xf0) == 0x90);
+}
+
+/*
+ * Client probe function - When a valid device is found, the driver's device
+ * data structure is updated, and initialization completes successfully.
+ */
+static int __devinit taos_probe(struct i2c_client *clientp,
+		      const struct i2c_device_id *idp)
+{
+	int i, ret = 0;
+	unsigned char buf[TSL258X_MAX_DEVICE_REGS];
+	static struct tsl2583_chip *chip;
+
+	if (!i2c_check_functionality(clientp->adapter,
+		I2C_FUNC_SMBUS_BYTE_DATA)) {
+		dev_err(&clientp->dev,
+			"taos_probe() - i2c smbus byte data "
+			"functions unsupported\n");
+		return -EOPNOTSUPP;
+	}
+
+	chip = kzalloc(sizeof(struct tsl2583_chip), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
+
+	chip->client = clientp;
+	i2c_set_clientdata(clientp, chip);
+
+	mutex_init(&chip->als_mutex);
+	chip->taos_chip_status = TSL258X_CHIP_UNKNOWN;
+	memcpy(chip->taos_config, taos_config, sizeof(chip->taos_config));
+
+	for (i = 0; i<  TSL258X_MAX_DEVICE_REGS; i++) {
+		ret = i2c_smbus_write_byte(clientp,
+				(TSL258X_CMD_REG | (TSL258X_CNTRL + i)));
+		if (ret<  0) {
+			dev_err(&clientp->dev, "i2c_smbus_write_bytes() to cmd "
+				"reg failed in taos_probe(), err = %d\n", ret);
+			goto fail1;
+		}
+		ret = i2c_smbus_read_byte(clientp);
+		if (ret<  0) {
+			dev_err(&clientp->dev, "i2c_smbus_read_byte from "
+				"reg failed in taos_probe(), err = %d\n", ret);
+
+			goto fail1;
+		}
+		buf[i] = ret;
+	}
+
+	if (!taos_tsl258x_device(buf)) {
+		dev_info(&clientp->dev, "i2c device found but does not match "
+			"expected id in taos_probe()\n");
+		goto fail1;
+	}
+
+	ret = i2c_smbus_write_byte(clientp, (TSL258X_CMD_REG | TSL258X_CNTRL));
+	if (ret<  0) {
+		dev_err(&clientp->dev, "i2c_smbus_write_byte() to cmd reg "
+			"failed in taos_probe(), err = %d\n", ret);
+		goto fail1;
+	}
+
+	chip->iio_dev = iio_allocate_device();
+	if (!chip->iio_dev) {
+		ret = -ENOMEM;
+		dev_err(&clientp->dev, "iio allocation failed\n");
+		goto fail1;
+	}
+
+	chip->iio_dev->attrs =&tsl2583_attribute_group;
+	chip->iio_dev->dev.parent =&clientp->dev;
+	chip->iio_dev->dev_data = (void *)(chip);
+	chip->iio_dev->driver_module = THIS_MODULE;
+	chip->iio_dev->modes = INDIO_DIRECT_MODE;
+	ret = iio_device_register(chip->iio_dev);
+	if (ret) {
+		dev_err(&clientp->dev, "iio registration failed\n");
+		goto fail1;
+	}
+
+	/* Load up the V2 defaults (these are hard coded defaults for now) */
+	taos_defaults(chip);
+
+	/* Make sure the chip is on */
+	taos_chip_on(clientp);
+
+	dev_info(&clientp->dev, "Light sensor found.\n");
+
+	return 0;
+
+fail1:
+	kfree(chip);
+
+	return ret;
+}
+
+static int taos_suspend(struct i2c_client *client, pm_message_t state)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	int ret = 0;
+
+	mutex_lock(&chip->als_mutex);
+
+	if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
+		ret = taos_chip_off(client);
+		chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
+	}
+
+	mutex_unlock(&chip->als_mutex);
+	return ret;
+}
+
+static int taos_resume(struct i2c_client *client)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	int ret = 0;
+
+	mutex_lock(&chip->als_mutex);
+
+	if (chip->taos_chip_status == TSL258X_CHIP_SUSPENDED)
+		ret = taos_chip_on(client);
+
+	mutex_unlock(&chip->als_mutex);
+	return ret;
+}
+
+
+static int __devexit taos_remove(struct i2c_client *client)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+
+	iio_device_unregister(chip->iio_dev);
+
+	kfree(chip);
+	return 0;
+}
+
+static struct i2c_device_id taos_idtable[] = {
+	{ "tsl2580", 0 },
+	{ "tsl2581", 1 },
+	{ "tsl2583", 2 },
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, taos_idtable);
+
+/* Driver definition */
+static struct i2c_driver taos_driver = {
+	.driver = {
+		.name = "tsl2583",
+	},
+	.id_table = taos_idtable,
+	.suspend	= taos_suspend,
+	.resume		= taos_resume,
+	.probe = taos_probe,
+	.remove = __devexit_p(taos_remove),
+};
+
+static int __init taos_init(void)
+{
+	return i2c_add_driver(&taos_driver);
+}
+
+static void __exit taos_exit(void)
+{
+	i2c_del_driver(&taos_driver);
+}
+
+module_init(taos_init);
+module_exit(taos_exit);
+
+MODULE_AUTHOR("J. August Brenner<[email protected]>");
+MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
+MODULE_LICENSE("GPL");
+
#3 lore raw
Quoted 326 lines

-----Original Message-----
From: Jon Brenner [mailto:[email protected]]
Sent: Tuesday, March 29, 2011 12:21 AM
To: Shubhrajyoti
Cc: Jonathan Cameron; linux-iio; Linux Kernel
Subject: Re: [PATCH V3]TAOS 258x: Device Driver

HI Shubhrajyoti:
Thanks for taking time to review the code. Please see my response to your comments throughout.

Jon

Quoted 8 lines · Shubhrajyoti

Hi Jon,
Some minor comments.

Quoted 4 lines · Jon Brenner

[PATCH v3] for TAOS tsl2580/81/83 Device driver

Added suspend/resume functions.
Changed attribute names to match existing where applicable and updated

or documented new ABI as discussed.

Quoted 1 line
Quoted 1 line

Changed integration time ABI from using index (0 to 3) to use actual

gain values (1x,8x, etc.).

Quoted 4 lines
Quoted 4 lines

Removed various unused variables, declarations, and functions. Revised code to accommodate different endianess (le16_to_cpu). Updated error return codes in various functions.
Changed from mdelay to msleep after determining that longer wait would

be acceptable.

Quoted 10 lines
Quoted 10 lines
---
  drivers/staging/iio/Documentation/sysfs-bus-iio    |    6 +
  .../staging/iio/Documentation/sysfs-bus-iio-light  |   13 +
  .../iio/Documentation/sysfs-bus-iio-light-tsl2583  |   20 +
  drivers/staging/iio/light/Kconfig                  |    9 +
  drivers/staging/iio/light/Makefile                 |    1 +
  drivers/staging/iio/light/tsl2583.c                |  962

++++++++++++++++++++

Quoted 3 lines

Review

Quoted 3 lines
  6 files changed, 1011 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/iio/Documentation/sysfs-bus-iio

Quoted 5 lines

drivers/staging/iio/Documentation/sysfs-bus-iio Description: · line 6

Quoted 5 lines

index 2dde97d..6a86ad2 100644

drivers/staging/iio/Documentation/sysfs-bus-iio +0 -0
@@ -6,6 +6,12 @@ Description:
  		Corresponds to a grouping of sensor channels. X is the

IIO

Quoted 12 lines

Review

Quoted 12 lines
drivers/staging/iio/Documentation/sysfs-bus-iio +6 -0
  		index of the device.
 
+What:		/sys/bus/iio/devices/device[n]/power_state
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the device power state.
+
  What:		/sys/bus/iio/devices/triggerX
  KernelVersion:	2.6.35
  Contact:	[email protected]
(quoted diff) +0 -0

b/drivers/staging/iio/Documentation/sysfs-bus-iio-light

Quoted 5 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description: · line 62

Quoted 5 lines

index 5d84856..21d2774 100644

drivers/staging/iio/Documentation/sysfs-bus-iio-light +0 -0
@@ -62,3 +62,16 @@ Description:
  		sensing mode. This value should be the output from a

reading

Quoted 1 line

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description: · line 63

Quoted 1 line
drivers/staging/iio/Documentation/sysfs-bus-iio-light +0 -0
  		and if expressed in SI units, should include _input. If

this

Quoted 10 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description:

Quoted 10 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light +9 -0
  		value is not in SI units, then it should include _raw.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the last known external
+		lux measurement used in/for calibration.
+
+What:

/sys/bus/iio/devices/device[n]/illuminance0_integration_time

Quoted 4 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description: · line 77

Quoted 4 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light +4 -0
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the sensors ADC analog integration

time.

Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

tsl2583 b/drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583

Quoted 9 lines
Quoted 9 lines

new file mode 100644
index 0000000..660781d

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +4 -0
@@ -0,0 +1,20 @@
+What:		/sys/bus/iio/devices/device[n]/lux_table
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
Quoted 8 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583

Quoted 8 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +8 -0
+		This property gets/sets the table of coefficients
+		used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_calibrate
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property causes an internal calibration of the als gain

trim

Quoted 9 lines

Review

Quoted 9 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +8 -0
+		value which is later used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_input_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property is the known externally illuminance (in lux).
+		It is used in the process of calibrating the device accuracy.
(quoted diff) +0 -0

b/drivers/staging/iio/light/Kconfig

Quoted 9 lines

drivers/staging/iio/light/Kconfig config SENSORS_TSL2563 · line 17

Quoted 9 lines

index 36d8bbe..55c146c 100644

drivers/staging/iio/light/Kconfig +2 -0
@@ -13,6 +13,15 @@ config SENSORS_TSL2563
  	 This driver can also be built as a module.  If so, the module
  	 will be called tsl2563.
 
+config TSL2583
+	tristate "TAOS TSL2580, TSL2581, and TSL2583 light-to-digital

converters"

Quoted 5 lines

drivers/staging/iio/light/Kconfig config SENSORS_TSL2563

Quoted 5 lines
drivers/staging/iio/light/Kconfig +5 -0
+	depends on I2C
+	help
+	  Y = in kernel.
+	  M = as module.
+	  Provides support for the TAOS tsl2580, tsl2581, and tsl2583

devices.

Quoted 6 lines

Review

Quoted 6 lines
drivers/staging/iio/light/Kconfig +2 -0
+	  Access ALS data via iio, sysfs.
+
  config SENSORS_ISL29018
          tristate "ISL 29018 light and proximity sensor"
          depends on I2C
(quoted diff) +0 -0

b/drivers/staging/iio/light/Makefile

Quoted 10 lines

Review

Quoted 10 lines

index 9142c0e..8a10815 100644

drivers/staging/iio/light/Makefile +1 -0
@@ -3,4 +3,5 @@
  #
 
  obj-$(CONFIG_SENSORS_TSL2563)	+= tsl2563.o
+obj-$(CONFIG_TSL2583)	+= tsl2583.o
  obj-$(CONFIG_SENSORS_ISL29018)	+= isl29018.o
(quoted diff) +0 -0

b/drivers/staging/iio/light/tsl2583.c

Quoted 4 lines
Quoted 4 lines

new file mode 100644
index 0000000..013a742

drivers/staging/iio/light/tsl2583.c new file +0 -0
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c line 7

Quoted 8 lines
drivers/staging/iio/light/tsl2583.c new file +7 -0
@@ -0,0 +1,962 @@
+/*
+ * Device driver for monitoring ambient light intensity (lux)
+ * within the TAOS tsl258x family of devices (tsl2580, tsl2581).
+ *
+ * Copyright (c) 2011, TAOS Corporation.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/staging/iio/light/tsl2583.c line 8

Quoted 1 line
drivers/staging/iio/light/tsl2583.c new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 4 lines

drivers/staging/iio/light/tsl2583.c

Quoted 4 lines
drivers/staging/iio/light/tsl2583.c new file +4 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,

but WITHOUT

Quoted 1 line

drivers/staging/iio/light/tsl2583.c line 13

Quoted 1 line
drivers/staging/iio/light/tsl2583.c new file +1 -0
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY

or

Quoted 1 line

drivers/staging/iio/light/tsl2583.c line 14

Quoted 1 line
drivers/staging/iio/light/tsl2583.c new file +1 -0
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public

License for

Quoted 3 lines

drivers/staging/iio/light/tsl2583.c

Quoted 3 lines
drivers/staging/iio/light/tsl2583.c new file +3 -0
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License

along

Quoted 1 line

drivers/staging/iio/light/tsl2583.c line 18

Quoted 1 line
drivers/staging/iio/light/tsl2583.c new file +1 -0
+ * with this program; if not, write to the Free Software Foundation,

Inc.,

Quoted 74 lines
Quoted 74 lines
drivers/staging/iio/light/tsl2583.c new file +74 -0
+ * 51 Franklin Street, Fifth Floor, Boston, MA	02110-1301, USA.
+ */
+
+#include<linux/kernel.h>
+#include<linux/i2c.h>
+#include<linux/errno.h>
+#include<linux/delay.h>
+#include<linux/string.h>
+#include<linux/mutex.h>
+#include<linux/unistd.h>
+#include "../iio.h"
+
+
+#define TSL258X_MAX_DEVICE_REGS		32
+
+/* Triton register offsets */
+#define	TSL258X_REG_MAX		8
+
+/* Device Registers and Masks */
+#define TSL258X_CNTRL			0x00
+#define TSL258X_ALS_TIME		0X01
+#define TSL258X_INTERRUPT		0x02
+#define TSL258X_GAIN			0x07
+#define TSL258X_REVID			0x11
+#define TSL258X_CHIPID			0x12
+#define TSL258X_ALS_CHAN0LO		0x14
+#define TSL258X_ALS_CHAN0HI		0x15
+#define TSL258X_ALS_CHAN1LO		0x16
+#define TSL258X_ALS_CHAN1HI		0x17
+#define TSL258X_TMR_LO			0x18
+#define TSL258X_TMR_HI			0x19
+
+/* tsl2583 cmd reg masks */
+#define TSL258X_CMD_REG			0x80
+#define TSL258X_CMD_SPL_FN		0x60
+#define TSL258X_CMD_ALS_INT_CLR	0X01
+
+/* tsl2583 cntrl reg masks */
+#define TSL258X_CNTL_ADC_ENBL	0x02
+#define TSL258X_CNTL_PWR_ON		0x01
+
+/* tsl2583 status reg masks */
+#define TSL258X_STA_ADC_VALID	0x01
+#define TSL258X_STA_ADC_INTR	0x10
+
+/* Lux calculation constants */
+#define	TSL258X_LUX_CALC_OVER_FLOW		65535
+
+enum {
+	TSL258X_CHIP_UNKNOWN = 0,
+	TSL258X_CHIP_WORKING = 1,
+	TSL258X_CHIP_SUSPENDED = 2
+} TSL258X_CHIP_WORKING_STATUS;
+
+/* Per-device data */
+struct taos_als_info {
+	u16 als_ch0;
+	u16 als_ch1;
+	u16 lux;
+};
+
+struct taos_settings {
+	int als_time;
+	int als_gain;
+	int als_gain_trim;
+	int als_cal_target;
+};
+
+struct tsl2583_chip {
+	struct mutex als_mutex;
+	struct i2c_client *client;
+	struct iio_dev *iio_dev;
+	struct taos_als_info als_cur_info;
+	struct taos_settings taos_settings;
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c

Quoted 8 lines
drivers/staging/iio/light/tsl2583.c new file +8 -0
+	int als_time_scale;
+	int als_saturation;
+	int taos_chip_status;
+	u8 taos_config[8];
+};
+
+/*
+ * Initial values for device - this values can/will be changed by

driver.

Quoted 6 lines
Quoted 6 lines
drivers/staging/iio/light/tsl2583.c new file +6 -0
+ * and applications as needed.
+ * These values are dynamic.
+ */
+static const u8 taos_config[8] = {
+		0x00, 0xee, 0x00, 0x03, 0x00, 0xFF, 0xFF, 0x00
+}; /*	cntrl atime intC  Athl0 Athl1 Athh0 Athh1 gain */
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c

Quoted 8 lines
drivers/staging/iio/light/tsl2583.c new file +8 -0
+
+struct taos_lux {
+	unsigned int ratio;
+	unsigned int ch0;
+	unsigned int ch1;
+};
+
+/* This structure is intentionally large to accommodate updates via

sysfs. */

Quoted 37 lines
Quoted 37 lines
drivers/staging/iio/light/tsl2583.c new file +37 -0
+/* Sized to 11 = max 10 segments + 1 termination segment */
+/* Assumption is is one and only one type of glass used  */
+struct taos_lux taos_device_lux[11] = {
+	{  9830,  8520, 15729 },
+	{ 12452, 10807, 23344 },
+	{ 14746,  6383, 11705 },
+	{ 17695,  4063,  6554 },
+};
+
+struct gainadj {
+	s16 ch0;
+	s16 ch1;
+};
+
+/* Index = (0 - 3) Used to validate the gain selection index */
+static const struct gainadj gainadj[] = {
+	{ 1, 1 },
+	{ 8, 8 },
+	{ 16, 16 },
+	{ 107, 115 }
+};
+
+/*
+ * Provides initial operational parameter defaults.
+ * These defaults may be changed through the device's sysfs files.
+ */
+static void taos_defaults(struct tsl2583_chip *chip)
+{
+	/* Operational parameters */
+	chip->taos_settings.als_time = 450;
+	/* must be a multiple of 50mS */
+	chip->taos_settings.als_gain = 2;
+	/* this is actually an index into the gain table */
+	/* assume clear glass as default */
+	chip->taos_settings.als_gain_trim = 1000;
+	/* default gain trim to account for aperture effects */
+	chip->taos_settings.als_cal_target = 130;
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c line 158

Quoted 7 lines
drivers/staging/iio/light/tsl2583.c new file +7 -0
+	/* Known external ALS reading used for calibration */
+}
+
+/*
+ * Read a number of bytes starting at register (reg) location.
+ * Return 0, or i2c_smbus_write_byte ERROR code.
+ */

Could we use i2c_smbus_read_block_data ?

Quoted 2 lines

NO -Many controllers no longer seem to support block mode read/write. To use block mode reads could exclude some.

By controller you mean the i2c controller?

Quoted 6 lines
Quoted 3 lines

drivers/staging/iio/light/tsl2583.c line 160

Quoted 3 lines
drivers/staging/iio/light/tsl2583.c new file +2 -0
+static int
+taos_i2c_read(struct i2c_client *client, u8 reg, u8 *val, unsigned

int len)

Quoted 2 lines
Quoted 2 lines
drivers/staging/iio/light/tsl2583.c new file +2 -0
+{
+	int ret;
#4 lore raw

-----Original Message-----
From: Shubhrajyoti Datta [mailto:[email protected]]
Sent: Tuesday, March 29, 2011 12:38 AM
To: Jon Brenner
Cc: Jonathan Cameron; linux-iio; Linux Kernel
Subject: RE: [PATCH V3]TAOS 258x: Device Driver

Quoted 47 lines

-----Original Message-----
From: Jon Brenner [mailto:[email protected]]
Sent: Tuesday, March 29, 2011 12:21 AM
To: Shubhrajyoti
Cc: Jonathan Cameron; linux-iio; Linux Kernel
Subject: Re: [PATCH V3]TAOS 258x: Device Driver

HI Shubhrajyoti:
Thanks for taking time to review the code. Please see my response to your comments throughout.

Jon

Quoted 9 lines · Shubhrajyoti

Hi Jon,
Some minor comments.

Quoted 5 lines · Jon Brenner

[PATCH v3] for TAOS tsl2580/81/83 Device driver

Added suspend/resume functions.
Changed attribute names to match existing where applicable and updated

or documented new ABI as discussed.

Quoted 2 lines
Quoted 2 lines

Changed integration time ABI from using index (0 to 3) to use actual

gain values (1x,8x, etc.).

Quoted 5 lines
Quoted 5 lines

Removed various unused variables, declarations, and functions. Revised code to accommodate different endianess (le16_to_cpu). Updated error return codes in various functions. Changed from mdelay to msleep after determining that longer wait would

be acceptable.

Quoted 10 lines
Quoted 10 lines
---
  drivers/staging/iio/Documentation/sysfs-bus-iio    |    6 +
  .../staging/iio/Documentation/sysfs-bus-iio-light  |   13 +
  .../iio/Documentation/sysfs-bus-iio-light-tsl2583  |   20 +
  drivers/staging/iio/light/Kconfig                  |    9 +
  drivers/staging/iio/light/Makefile                 |    1 +
  drivers/staging/iio/light/tsl2583.c                |  962

++++++++++++++++++++

Quoted 2 lines
Quoted 2 lines
  6 files changed, 1011 insertions(+), 0 deletions(-)
Quoted 7 lines
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/staging/iio/Documentation/sysfs-bus-iio

Quoted 5 lines

drivers/staging/iio/Documentation/sysfs-bus-iio Description: · line 6

Quoted 5 lines

index 2dde97d..6a86ad2 100644

drivers/staging/iio/Documentation/sysfs-bus-iio +0 -0
@@ -6,6 +6,12 @@ Description:
  		Corresponds to a grouping of sensor channels. X

is the

Quoted 14 lines

IIO

Quoted 12 lines

Review

Quoted 12 lines
drivers/staging/iio/Documentation/sysfs-bus-iio +6 -0
  		index of the device.
 
+What:		/sys/bus/iio/devices/device[n]/power_state
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the device power state.
+
  What:		/sys/bus/iio/devices/triggerX
  KernelVersion:	2.6.35
  Contact:	[email protected]
(quoted diff) +0 -0

b/drivers/staging/iio/Documentation/sysfs-bus-iio-light

Quoted 5 lines
Quoted 5 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description: · line 62

Quoted 5 lines

index 5d84856..21d2774 100644

drivers/staging/iio/Documentation/sysfs-bus-iio-light +0 -0
@@ -62,3 +62,16 @@ Description:
  		sensing mode. This value should be the output

from a

Quoted 2 lines

reading

Quoted 1 line
Quoted 1 line
drivers/staging/iio/Documentation/sysfs-bus-iio-light +0 -0
  		and if expressed in SI units, should include

_input. If

Quoted 2 lines

this

Quoted 1 line
Quoted 1 line
drivers/staging/iio/Documentation/sysfs-bus-iio-light +0 -0
  		value is not in SI units, then it should include

_raw.

Quoted 2 lines
Quoted 2 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description: · line 66

Quoted 2 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light +2 -0
+
+What:

/sys/bus/iio/devices/device[n]/illuminance0_target

Quoted 8 lines
Quoted 7 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description:

Quoted 7 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light +7 -0
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the last known external
+		lux measurement used in/for calibration.
+
+What:

/sys/bus/iio/devices/device[n]/illuminance0_integration_time

Quoted 4 lines
Quoted 4 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description: · line 77

Quoted 4 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light +4 -0
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the sensors ADC analog

integration

Quoted 5 lines

time.

Quoted 2 lines
Quoted 2 lines
(quoted diff) +0 -0

a/drivers/staging/iio/Documentation/sysfs-bus-iio-light-

tsl2583 b/drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines

new file mode 100644
index 0000000..660781d

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 new file +1 -0
+++ 3
@@ -0,0 +1,20 @@
Quoted 8 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 line 8

Quoted 8 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 new file +8 -0
+What:		/sys/bus/iio/devices/device[n]/lux_table
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the table of coefficients
+		used in calculating illuminance in lux.
+
+What:

/sys/bus/iio/devices/device[n]/illuminance0_calibrate

Quoted 4 lines
Quoted 4 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 line 12

Quoted 4 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 new file +4 -0
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property causes an internal calibration of the als

gain

Quoted 2 lines

trim

Quoted 1 line

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 line 13

Quoted 1 line
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 new file +1 -0
+		value which is later used in calculating illuminance in

lux.

Quoted 2 lines
Quoted 2 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 line 15

Quoted 2 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 new file +2 -0
+
+What:

/sys/bus/iio/devices/device[n]/illuminance0_input_target

Quoted 4 lines
Quoted 4 lines

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 line 19

Quoted 4 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 new file +4 -0
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property is the known externally illuminance (in

lux).

Quoted 1 line
Quoted 1 line

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 line 20

Quoted 1 line
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl258 new file +1 -0
+		It is used in the process of calibrating the device

accuracy.

Quoted 7 lines
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/staging/iio/light/Kconfig

Quoted 5 lines

drivers/staging/iio/light/Kconfig config SENSORS_TSL2563 · line 13

Quoted 5 lines

index 36d8bbe..55c146c 100644

drivers/staging/iio/light/Kconfig +0 -0
@@ -13,6 +13,15 @@ config SENSORS_TSL2563
  	 This driver can also be built as a module.  If so, the

module

Quoted 182 lines
Quoted 4 lines

drivers/staging/iio/light/Kconfig config SENSORS_TSL2563

Quoted 4 lines
drivers/staging/iio/light/Kconfig +2 -0
  	 will be called tsl2563.
 
+config TSL2583
+	tristate "TAOS TSL2580, TSL2581, and TSL2583 light-to-digital

converters"

Quoted 5 lines

drivers/staging/iio/light/Kconfig config SENSORS_TSL2563

Quoted 5 lines
drivers/staging/iio/light/Kconfig +5 -0
+	depends on I2C
+	help
+	  Y = in kernel.
+	  M = as module.
+	  Provides support for the TAOS tsl2580, tsl2581, and tsl2583

devices.

Quoted 6 lines

Review

Quoted 6 lines
drivers/staging/iio/light/Kconfig +2 -0
+	  Access ALS data via iio, sysfs.
+
  config SENSORS_ISL29018
          tristate "ISL 29018 light and proximity sensor"
          depends on I2C
(quoted diff) +0 -0

b/drivers/staging/iio/light/Makefile

Quoted 10 lines

Review

Quoted 10 lines

index 9142c0e..8a10815 100644

drivers/staging/iio/light/Makefile +1 -0
@@ -3,4 +3,5 @@
  #
 
  obj-$(CONFIG_SENSORS_TSL2563)	+= tsl2563.o
+obj-$(CONFIG_TSL2583)	+= tsl2583.o
  obj-$(CONFIG_SENSORS_ISL29018)	+= isl29018.o
(quoted diff) +0 -0

b/drivers/staging/iio/light/tsl2583.c

Quoted 4 lines
Quoted 4 lines

new file mode 100644
index 0000000..013a742

drivers/staging/iio/light/tsl2583.c new file +0 -0
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c line 7

Quoted 8 lines
drivers/staging/iio/light/tsl2583.c new file +7 -0
@@ -0,0 +1,962 @@
+/*
+ * Device driver for monitoring ambient light intensity (lux)
+ * within the TAOS tsl258x family of devices (tsl2580, tsl2581).
+ *
+ * Copyright (c) 2011, TAOS Corporation.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 2 lines

drivers/staging/iio/light/tsl2583.c line 9

Quoted 2 lines
drivers/staging/iio/light/tsl2583.c new file +2 -0
+ * it under the terms of the GNU General Public License as 
+ published

by

Quoted 7 lines

drivers/staging/iio/light/tsl2583.c

Quoted 7 lines
drivers/staging/iio/light/tsl2583.c new file +6 -0
+ * the Free Software Foundation; either version 2 of the License,
 
+ or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be 
+ useful,

but WITHOUT

Quoted 2 lines
Quoted 2 lines

+ * ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY

or

Quoted 1 line
Quoted 1 line

+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public

License for

Quoted 4 lines
Quoted 4 lines

+ * more details.
+ *
+ * You should have received a copy of the GNU General Public
+ License

along

Quoted 2 lines
Quoted 2 lines

+ * with this program; if not, write to the Free Software
+ Foundation,

Inc.,

Quoted 74 lines
Quoted 74 lines

+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */

drivers/staging/iio/light/tsl2583.c new file +72 -0
+
+#include<linux/kernel.h>
+#include<linux/i2c.h>
+#include<linux/errno.h>
+#include<linux/delay.h>
+#include<linux/string.h>
+#include<linux/mutex.h>
+#include<linux/unistd.h>
+#include "../iio.h"
+
+
+#define TSL258X_MAX_DEVICE_REGS		32
+
+/* Triton register offsets */
+#define	TSL258X_REG_MAX		8
+
+/* Device Registers and Masks */
+#define TSL258X_CNTRL			0x00
+#define TSL258X_ALS_TIME		0X01
+#define TSL258X_INTERRUPT		0x02
+#define TSL258X_GAIN			0x07
+#define TSL258X_REVID			0x11
+#define TSL258X_CHIPID			0x12
+#define TSL258X_ALS_CHAN0LO		0x14
+#define TSL258X_ALS_CHAN0HI		0x15
+#define TSL258X_ALS_CHAN1LO		0x16
+#define TSL258X_ALS_CHAN1HI		0x17
+#define TSL258X_TMR_LO			0x18
+#define TSL258X_TMR_HI			0x19
+
+/* tsl2583 cmd reg masks */
+#define TSL258X_CMD_REG			0x80
+#define TSL258X_CMD_SPL_FN		0x60
+#define TSL258X_CMD_ALS_INT_CLR	0X01
+
+/* tsl2583 cntrl reg masks */
+#define TSL258X_CNTL_ADC_ENBL	0x02
+#define TSL258X_CNTL_PWR_ON		0x01
+
+/* tsl2583 status reg masks */
+#define TSL258X_STA_ADC_VALID	0x01
+#define TSL258X_STA_ADC_INTR	0x10
+
+/* Lux calculation constants */
+#define	TSL258X_LUX_CALC_OVER_FLOW		65535
+
+enum {
+	TSL258X_CHIP_UNKNOWN = 0,
+	TSL258X_CHIP_WORKING = 1,
+	TSL258X_CHIP_SUSPENDED = 2
+} TSL258X_CHIP_WORKING_STATUS;
+
+/* Per-device data */
+struct taos_als_info {
+	u16 als_ch0;
+	u16 als_ch1;
+	u16 lux;
+};
+
+struct taos_settings {
+	int als_time;
+	int als_gain;
+	int als_gain_trim;
+	int als_cal_target;
+};
+
+struct tsl2583_chip {
+	struct mutex als_mutex;
+	struct i2c_client *client;
+	struct iio_dev *iio_dev;
+	struct taos_als_info als_cur_info;
+	struct taos_settings taos_settings;
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c

Quoted 8 lines
drivers/staging/iio/light/tsl2583.c new file +8 -0
+	int als_time_scale;
+	int als_saturation;
+	int taos_chip_status;
+	u8 taos_config[8];
+};
+
+/*
+ * Initial values for device - this values can/will be changed by

driver.

Quoted 7 lines
Quoted 7 lines

+ * and applications as needed.
+ * These values are dynamic.
+ */

drivers/staging/iio/light/tsl2583.c new file +4 -0
+static const u8 taos_config[8] = {
+		0x00, 0xee, 0x00, 0x03, 0x00, 0xFF, 0xFF, 0x00
+}; /*	cntrl atime intC  Athl0 Athl1 Athh0 Athh1 gain */
+
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c

Quoted 8 lines
drivers/staging/iio/light/tsl2583.c new file +8 -0
+struct taos_lux {
+	unsigned int ratio;
+	unsigned int ch0;
+	unsigned int ch1;
+};
+
+/* This structure is intentionally large to accommodate updates 
+via

sysfs. */

Quoted 17 lines
Quoted 17 lines
drivers/staging/iio/light/tsl2583.c new file +17 -0
+/* Sized to 11 = max 10 segments + 1 termination segment */
+/* Assumption is is one and only one type of glass used  */ 
+struct taos_lux taos_device_lux[11] = {
+	{  9830,  8520, 15729 },
+	{ 12452, 10807, 23344 },
+	{ 14746,  6383, 11705 },
+	{ 17695,  4063,  6554 },
+};
+
+struct gainadj {
+	s16 ch0;
+	s16 ch1;
+};
+
+/* Index = (0 - 3) Used to validate the gain selection index */ 
+static const struct gainadj gainadj[] = {
+	{ 1, 1 },
Quoted 8 lines
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c

Quoted 8 lines
drivers/staging/iio/light/tsl2583.c new file +8 -0
+	{ 8, 8 },
+	{ 16, 16 },
+	{ 107, 115 }
+};
+
+/*
+ * Provides initial operational parameter defaults.
+ * These defaults may be changed through the device's sysfs

files.

Quoted 18 lines
Quoted 10 lines
Quoted 10 lines

+ */

drivers/staging/iio/light/tsl2583.c new file +9 -0
+static void taos_defaults(struct tsl2583_chip *chip) {
+	/* Operational parameters */
+	chip->taos_settings.als_time = 450;
+	/* must be a multiple of 50mS */
+	chip->taos_settings.als_gain = 2;
+	/* this is actually an index into the gain table */
+	/* assume clear glass as default */
+	chip->taos_settings.als_gain_trim = 1000;
+	/* default gain trim to account for aperture effects */
Quoted 8 lines

drivers/staging/iio/light/tsl2583.c line 158

Quoted 7 lines
drivers/staging/iio/light/tsl2583.c new file +7 -0
+	chip->taos_settings.als_cal_target = 130;
+	/* Known external ALS reading used for calibration */ }
+
+/*
+ * Read a number of bytes starting at register (reg) location.
+ * Return 0, or i2c_smbus_write_byte ERROR code.
+ */

Could we use i2c_smbus_read_block_data ?

Quoted 2 lines

NO -Many controllers no longer seem to support block mode read/write. To use block mode reads could exclude some.

By controller you mean the i2c controller?
(ie. - msg from older driver)
taos 0-0039: taos_probe() - i2c smbus block data functions unsupported

Quoted 7 lines
Quoted 4 lines

drivers/staging/iio/light/tsl2583.c

Quoted 4 lines
drivers/staging/iio/light/tsl2583.c new file +3 -0
+static int
+taos_i2c_read(struct i2c_client *client, u8 reg, u8 *val, 
+unsigned

int len)

Quoted 2 lines
Quoted 2 lines
drivers/staging/iio/light/tsl2583.c new file +2 -0
+{
+	int ret;
Acked-by on v3 1/1
#5 lore raw
In reply to Jon Brenner
Quoted 9 lines · Jon Brenner

[PATCH v3] for TAOS tsl2580/81/83 Device driver

Added suspend/resume functions.
Changed attribute names to match existing where applicable and updated or documented new ABI as discussed. Changed integration time ABI from using index (0 to 3) to use actual gain values (1x,8x, etc.). Removed various unused variables, declarations, and functions.
Revised code to accommodate different endianess (le16_to_cpu).
Updated error return codes in various functions.
Changed from mdelay to msleep after determining that longer wait would be acceptable.

Hi Jon,

Check patch is giving me the following that you'll want to clear up before sending this to Greg KH.
WARNING: space required after Signed-off-by:
#60:

WARNING: line over 80 characters
#414: FILE: drivers/staging/iio/light/tsl2583.c:244:
+ (TSL258X_CMD_REG | TSL258X_CMD_SPL_FN | TSL258X_CMD_ALS_INT_CLR));

Otherwise couple of nitpicks in line.

It's rather late to moan about it but I'd have prefered to not have the generic prefix 'taos' used, but rather the name of some part. Afterall this isn't the only taos part out there! Still it's unlikely a name clash will occur so I'm not suggesting you change it at this point, more suggesting you are careful about that in future drivers.

All the nitpicks are totally trivial changes to comments, or layout of code, so no need to post for review again assuming Datta is happy (looks to me like you answered all of his points) Hence fix this stuff up or tell me I'm being silly then send it on to Greg KH <[email protected]> with my Ack. Do verify it applies and builds against staging next before sending it on. (It's obviously but I'm always forgetting to do that for starters!).

Quoted 2 lines

Nice driver. I'm looking forward to lots more in future ;)

Jonathan

Quoted 32 lines
---
 drivers/staging/iio/Documentation/sysfs-bus-iio    |    6 +
 .../staging/iio/Documentation/sysfs-bus-iio-light  |   13 +
 .../iio/Documentation/sysfs-bus-iio-light-tsl2583  |   20 +
 drivers/staging/iio/light/Kconfig                  |    9 +
 drivers/staging/iio/light/Makefile                 |    1 +
 drivers/staging/iio/light/tsl2583.c                |  962 ++++++++++++++++++++
 6 files changed, 1011 insertions(+), 0 deletions(-)
drivers/staging/iio/Documentation/sysfs-bus-iio +6 -0
index 2dde97d..6a86ad2 100644
@@ -6,6 +6,12 @@ Description:
 		Corresponds to a grouping of sensor channels. X is the IIO
 		index of the device.
 
+What:		/sys/bus/iio/devices/device[n]/power_state
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the device power state.
+
 What:		/sys/bus/iio/devices/triggerX
 KernelVersion:	2.6.35
 Contact:	[email protected]
drivers/staging/iio/Documentation/sysfs-bus-iio-light +0 -0
index 5d84856..21d2774 100644

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description: · line 71

Quoted 10 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light +7 -0
@@ -62,3 +62,16 @@ Description:
 		sensing mode. This value should be the output from a reading
 		and if expressed in SI units, should include _input. If this
 		value is not in SI units, then it should include _raw.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the last known external
+		lux measurement used in/for calibration.

Really not keen on the word property. Doesn't to my mind really apply when these are separate files... Still don't really care about that if you are really keen on it. It is pretty obvious what means.

drivers/staging/iio/Documentation/sysfs-bus-iio-light Description: · line 77

Quoted 6 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light +6 -0
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_integration_time
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the sensors ADC analog integration time.

Units are? (I know we are lax in this elsewhere in the docs, but might as well fix up new stuff!)

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 line 5

Quoted 11 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +5 -0
new file mode 100644
index 0000000..660781d
@@ -0,0 +1,20 @@
+What:		/sys/bus/iio/devices/device[n]/lux_table
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property gets/sets the table of coefficients

property -> 'attribute' or 'control'

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 line 12

Quoted 7 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +7 -0
+		used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_calibrate
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property causes an internal calibration of the als gain trim

property -> 'attribute' or 'control'

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 line 19

Quoted 7 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +7 -0
+		value which is later used in calculating illuminance in lux.
+
+What:		/sys/bus/iio/devices/device[n]/illuminance0_input_target
+KernelVersion:	2.6.37
+Contact:	[email protected]
+Description:
+		This property is the known externally illuminance (in lux).

externally -> external ? eternally measured ?

Quoted 1 line
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +1 -0
+		It is used in the process of calibrating the device accuracy.

...

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583

Quoted 9 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +9 -0
+struct taos_lux {
+	unsigned int ratio;
+	unsigned int ch0;
+	unsigned int ch1;
+};
+
+/* This structure is intentionally large to accommodate updates via sysfs. */
+/* Sized to 11 = max 10 segments + 1 termination segment */
+/* Assumption is is one and only one type of glass used  */

This is a multiline comment, so do the formatting the same as all of the others.

Quoted 5 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +5 -0
+struct taos_lux taos_device_lux[11] = {
+	{  9830,  8520, 15729 },
+	{ 12452, 10807, 23344 },
+	{ 14746,  6383, 11705 },
+	{ 17695,  4063,  6554 },

...

Quoted 79 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +79 -0
+/*
+ * Turn the device on.
+ * Configuration must be set before calling this function.
+ */
+static int taos_chip_on(struct i2c_client *client)
+{
+	int i;
+	int ret = 0;
+	u8 *uP;
+	u8 utmp;
+	int als_count;
+	int als_time;
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+
+	/* and make sure we're not already on */
+	if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
+		/* if forcing a register update - turn off, then on */
+		dev_info(&client->dev, "device is already enabled\n");
+		return   -EINVAL;
+	}
+
+	/* determine als integration regster */
+	als_count = (chip->taos_settings.als_time * 100 + 135) / 270;
+	if (als_count == 0)
+		als_count = 1; /* ensure at least one cycle */
+
+	/* convert back to time (encompasses overrides) */
+	als_time = (als_count * 27 + 5) / 10;
+	chip->taos_config[TSL258X_ALS_TIME] = 256 - als_count;
+
+	/* Set the gain based on taos_settings struct */
+	chip->taos_config[TSL258X_GAIN] = chip->taos_settings.als_gain;
+
+	/* set chip struct re scaling and saturation */
+	chip->als_saturation = als_count * 922; /* 90% of full scale */
+	chip->als_time_scale = (als_time + 25) / 50;
+
+	/* TSL258x Specific power-on / adc enable sequence
+	 * Power on the device 1st. */
+	utmp = TSL258X_CNTL_PWR_ON;
+	ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG | TSL258X_CNTRL,
+					utmp);
+	if (ret < 0) {
+		dev_err(&client->dev, "taos_chip_on failed on CNTRL reg.\n");
+		return -1;
+	}
+
+	/* Use the following shadow copy for our delay before enabling ADC.
+	 * Write all the registers. */
+	for (i = 0, uP = chip->taos_config; i < TSL258X_REG_MAX; i++) {
+		ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG + i,
+						*uP++);
+		if (ret < 0) {
+			dev_err(&client->dev,
+				"taos_chip_on failed on reg %d.\n", i);
+			return -1;
+		}
+	}
+
+	msleep(3);
+	/* NOW enable the ADC
+	 * initialize the desired mode of operation */
+	utmp = TSL258X_CNTL_PWR_ON | TSL258X_CNTL_ADC_ENBL;
+	ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG | TSL258X_CNTRL,
+					utmp);
+	if (ret < 0) {
+		dev_err(&client->dev, "taos_chip_on failed on 2nd CTRL reg.\n");
+		return -1;
+	}
+	chip->taos_chip_status = TSL258X_CHIP_WORKING;
+
+	return ret;
+}
+
+static int taos_chip_off(struct i2c_client *client)
+{
+	struct tsl2583_chip *chip = i2c_get_clientdata(client);
+	int ret;
+

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583

Quoted 10 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +10 -0
+	/* turn device off */
+	chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
+	ret = i2c_smbus_write_byte_data(client, TSL258X_CMD_REG | TSL258X_CNTRL,
+					0x00);
+	return ret;
+}
+
+/* Sysfs Interface Functions */
+static ssize_t taos_device_id(struct device *dev,
+struct device_attribute *attr, char *buf)

Indentation on this parameter list looks rather odd. Please check.

Quoted 7 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +7 -0
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct tsl2583_chip *chip = indio_dev->dev_data;
+
+	return sprintf(buf, "%s\n", chip->client->name);
+}
+

...

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583

Quoted 12 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +12 -0
+	if ((value < 50) || (value > 650))
+		return -EINVAL;
+
+	if (value % 50)
+		return -EINVAL;
+
+	 chip->taos_settings.als_time = value;
+
+	return len;
+}
+
+static ssize_t taos_als_time_available_show(struct device *dev,

taos_als_int_time_avaialble_show perhaps?

Quoted 6 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +6 -0
+	struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%s\n",
+		"50 100 150 200 250 300 350 400 450 500 550 600 650");
+}
+

...

Quoted 25 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +25 -0
+	int i, ret = 0;
+	unsigned char buf[TSL258X_MAX_DEVICE_REGS];
+	static struct tsl2583_chip *chip;
+
+	if (!i2c_check_functionality(clientp->adapter,
+		I2C_FUNC_SMBUS_BYTE_DATA)) {
+		dev_err(&clientp->dev,
+			"taos_probe() - i2c smbus byte data "
+			"functions unsupported\n");
+		return -EOPNOTSUPP;
+	}
+
+	chip = kzalloc(sizeof(struct tsl2583_chip), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
+
+	chip->client = clientp;
+	i2c_set_clientdata(clientp, chip);
+
+	mutex_init(&chip->als_mutex);
+	chip->taos_chip_status = TSL258X_CHIP_UNKNOWN;
+	memcpy(chip->taos_config, taos_config, sizeof(chip->taos_config));
+
+	for (i = 0; i < TSL258X_MAX_DEVICE_REGS; i++) {
+		ret = i2c_smbus_write_byte(clientp,

drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583

Quoted 10 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +10 -0
+				(TSL258X_CMD_REG | (TSL258X_CNTRL + i)));
+		if (ret < 0) {
+			dev_err(&clientp->dev, "i2c_smbus_write_bytes() to cmd "
+				"reg failed in taos_probe(), err = %d\n", ret);
+			goto fail1;
+		}
+		ret = i2c_smbus_read_byte(clientp);
+		if (ret < 0) {
+			dev_err(&clientp->dev, "i2c_smbus_read_byte from "
+				"reg failed in taos_probe(), err = %d\n", ret);

Please don't break strings in error messages across lines. This is the one place where it is preferable to ignore checkpatch. Otherwise people can't grep the tree for where the heck the message they are seeing came from!

Quoted 18 lines
drivers/staging/iio/Documentation/sysfs-bus-iio-light-tsl2583 new file +18 -0
+
+			goto fail1;
+		}
+		buf[i] = ret;
+	}
+
+	if (!taos_tsl258x_device(buf)) {
+		dev_info(&clientp->dev, "i2c device found but does not match "
+			"expected id in taos_probe()\n");
+		goto fail1;
+	}
+
+	ret = i2c_smbus_write_byte(clientp, (TSL258X_CMD_REG | TSL258X_CNTRL));
+	if (ret < 0) {
+		dev_err(&clientp->dev, "i2c_smbus_write_byte() to cmd reg "
+			"failed in taos_probe(), err = %d\n", ret);
+		goto fail1;
+	}

...