[PATCH V1 1/1] iio: Added Capella cm3232 ambient light sensor driver.

2014-12-31 Thread Kevin Tsai
CM3232 is an advanced ambient light sensor with I2C protocol interface.
The I2C slave address is internally hardwired as 0x10 (7-bit).  Writing
to configure register is byte mode, but reading ALS register requests to
use word mode for 16-bit resolution.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 .../devicetree/bindings/i2c/trivial-devices.txt|   1 +
 MAINTAINERS|   6 +
 drivers/iio/light/Kconfig  |  11 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm3232.c | 403 +
 5 files changed, 422 insertions(+)
 create mode 100644 drivers/iio/light/cm3232.c

diff --git a/Documentation/devicetree/bindings/i2c/trivial-devices.txt 
b/Documentation/devicetree/bindings/i2c/trivial-devices.txt
index 9f4e382..572a7c4 100644
--- a/Documentation/devicetree/bindings/i2c/trivial-devices.txt
+++ b/Documentation/devicetree/bindings/i2c/trivial-devices.txt
@@ -34,6 +34,7 @@ atmel,24c512  i2c serial eeprom  (24cxx)
 atmel,24c1024  i2c serial eeprom  (24cxx)
 atmel,at97sc3204t  i2c trusted platform module (TPM)
 capella,cm32181CM32181: Ambient Light Sensor
+capella,cm3232 CM3232: Ambient Light Sensor
 catalyst,24c32 i2c serial eeprom
 cirrus,cs42l51 Cirrus Logic CS42L51 audio codec
 dallas,ds1307  64 x 8, Serial, I2C Real-Time Clock
diff --git a/MAINTAINERS b/MAINTAINERS
index ddb9ac8..06a613a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2378,6 +2378,12 @@ F:   security/capability.c
 F: security/commoncap.c
 F: kernel/capability.c
 
+CAPELLA MICROSYSTEMS LIGHT SENSOR DRIVER
+M: Kevin Tsai kt...@capellamicro.com
+S: Maintained
+F: drivers/iio/light/cm*
+F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+
 CC2520 IEEE-802.15.4 RADIO DRIVER
 M: Varka Bhadram varkabhad...@gmail.com
 L: linux-w...@vger.kernel.org
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 5bea821..d2318e2 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -48,6 +48,17 @@ config CM32181
 To compile this driver as a module, choose M here:
 the module will be called cm32181.
 
+config CM3232
+   depends on I2C
+   tristate CM3232 driver
+   help
+Say Y here if you use cm3232.
+This option enables ambient light sensor using
+Capella Microsystems cm3232 device driver.
+
+To compile this driver as a module, choose M here:
+the module will be called cm3232.
+
 config CM36651
depends on I2C
tristate CM36651 driver
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 47877a3..f2c8d55 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_ADJD_S311) += adjd_s311.o
 obj-$(CONFIG_AL3320A)  += al3320a.o
 obj-$(CONFIG_APDS9300) += apds9300.o
 obj-$(CONFIG_CM32181)  += cm32181.o
+obj-$(CONFIG_CM3232)   += cm3232.o
 obj-$(CONFIG_CM36651)  += cm36651.o
 obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o
 obj-$(CONFIG_HID_SENSOR_ALS)   += hid-sensor-als.o
diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
new file mode 100644
index 000..fd98eeb
--- /dev/null
+++ b/drivers/iio/light/cm3232.c
@@ -0,0 +1,403 @@
+/*
+ * Copyright (C) 2014 Capella Microsystems Inc.
+ * Author: Kevin Tsai kt...@capellamicro.com
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2, as published
+ * by the Free Software Foundation.
+ *
+ */
+
+#include linux/delay.h
+#include linux/err.h
+#include linux/i2c.h
+#include linux/mutex.h
+#include linux/module.h
+#include linux/interrupt.h
+#include linux/regulator/consumer.h
+#include linux/iio/iio.h
+#include linux/iio/sysfs.h
+#include linux/iio/events.h
+#include linux/init.h
+
+/* Registers Address */
+#define CM3232_REG_ADDR_CMD0x00
+#define CM3232_REG_ADDR_ALS0x50
+#define CM3232_REG_ADDR_ID 0x53
+
+/* CMD register */
+#define CM3232_CMD_ALS_DISABLE BIT(0)
+#defineCM3232_CMD_ALS_HS   BIT(1)
+
+#define CM3232_CMD_ALS_IT_SHIFT 2
+#define CM3232_CMD_ALS_IT_MASK  (0x07  CM3232_CMD_ALS_IT_SHIFT)
+#define CM3232_CMD_ALS_IT_DEFAULT   (0x01  CM3232_CMD_ALS_IT_SHIFT)
+
+#defineCM3232_CMD_ALS_RESETBIT(6)
+
+#define CM3232_CMD_DEFAULT CM3232_CMD_ALS_IT_DEFAULT
+
+#define CM3232_CALIBSCALE_DEFAULT  10
+#define CM3232_CALIBSCALE_RESOLUTION   10
+#define CM3232_MLUX_PER_LUX1000
+
+#define CM3232_MLUX_PER_BIT_DEFAULT64
+#define CM3232_MLUX_PER_BIT_BASE_IT10
+static const int CM3232_als_it_bits[] = { 0, 1, 2, 3, 4, 5};
+static const int CM3232_als_it_values[] = {
+   10, 

Re: [PATCH V1 1/1] iio: Added Capella cm3232 ambient light sensor driver.

2014-12-31 Thread Jeremiah Mahler
Kevin,

On Wed, Dec 31, 2014 at 04:10:30PM -0800, Kevin Tsai wrote:
 CM3232 is an advanced ambient light sensor with I2C protocol interface.
 The I2C slave address is internally hardwired as 0x10 (7-bit).  Writing
 to configure register is byte mode, but reading ALS register requests to
 use word mode for 16-bit resolution.
 
 Signed-off-by: Kevin Tsai kt...@capellamicro.com
 ---
  .../devicetree/bindings/i2c/trivial-devices.txt|   1 +
  MAINTAINERS|   6 +
  drivers/iio/light/Kconfig  |  11 +
[...]
 +static int cm3232_get_lux(struct cm3232_chip *chip);
 +static int cm3232_read_als_it(struct cm3232_chip *chip, int *val2);
 +
 +/**
 + * cm3232_reg_init() - Initialize CM3232 registers
 + * @chip:pointer of struct cm3232.
 + *
 + * Initialize CM3232 ambient light sensor register to default values.
 + *
 +  Return: 0 for success; otherwise for error code.
Is a '*' missing?

 + */
 +static int cm3232_reg_init(struct cm3232_chip *chip)
 +{
 + struct i2c_client *client = chip-client;
 + struct cm3232_als_info *als_info;
 + s32 ret;
 +
 + /* Identify device */
[...]
 +
 + /* Calculate mlux per bit based on als_it */
 + ret = cm3232_read_als_it(chip, als_it);
 + if (ret  0)
 + return -EINVAL;
 + tmp = (__force u64)als_info-mlux_per_bit;
 + tmp *= als_info-mlux_per_bit_base_it;
 + tmp = div_u64 (tmp, als_it);
 ^
 no space after function
 +
 + /* Get als_raw */
 + als_info-als_raw = i2c_smbus_read_word_data(
 + client,
 + CM3232_REG_ADDR_ALS);
 + if (als_info-als_raw  0)
 + return als_info-als_raw;
 +
 + tmp *= als_info-als_raw;
 + tmp *= als_info-calibscale;
 + tmp = div_u64(tmp, CM3232_CALIBSCALE_RESOLUTION);
 + tmp = div_u64(tmp, CM3232_MLUX_PER_LUX);
[...]

It builds clean and there are no checkpatch or sparse errors.
Overall it looks good.

-- 
- Jeremiah Mahler
--
To unsubscribe from this list: send the line unsubscribe linux-i2c in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html