[PATCH V2 1/1] iio: light: Added CM36672 Proximity Sensor Driver.

2016-06-09 Thread Kevin Tsai
Added Vishay Capella CM36672 Proximity Sensor IIO driver.  Support both
ACPI and Device Tree.

Signed-off-by: Kevin Tsai <capellami...@gmail.com>
---
V2:
Thanks commends from Peter Meerwald-Stadler, Jonathan Cameron, and Linux
Walleij.  Updated for the following:
 - Remove unused defines.
 - Rewrite event registration.
 - Remove direct register values in the device tree.
 - Rewrite by regmap API.
 - Remove irq module parameter.
 - Correct cm36672_remove().

 .../devicetree/bindings/iio/light/cm36672.txt  |  37 +
 MAINTAINERS|   4 +-
 drivers/iio/light/Kconfig  |  11 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm36672.c| 820 +
 5 files changed, 871 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm36672.txt
 create mode 100644 drivers/iio/light/cm36672.c

diff --git a/Documentation/devicetree/bindings/iio/light/cm36672.txt 
b/Documentation/devicetree/bindings/iio/light/cm36672.txt
new file mode 100644
index 000..9681d2c
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm36672.txt
@@ -0,0 +1,37 @@
+* Vishay Capella CM36672 I2C Proximity sensor
+
+Required properties:
+- compatible: must be capella,cm36672"
+- reg: the I2C slave address, usually 0x60
+- interrupts: interrupt mapping for GPIO IRQ
+
+Optional properties:
+- interrupt-parent: interrupt controller
+- interrupts: interrupt mapping for GPIO IRQ
+- cm36672,prx_led_current: LED current, must be one of the following values:
+- 0: 50mA
+- 1: 75mA
+- 2: 100mA
+- 3: 120mA
+- 4: 140mA
+- 5: 160mA
+- 6: 180mA
+- 7: 200mA
+- cm36672,prx_hd: width of proximity sensor output data, must be one of the
+  following values:
+- 0: 12-bit
+- 1: 16-bit
+
+Example:
+
+cm36672@60 {
+compatible = "capella,cm36672";
+reg = <0x60>;
+interrupt-parent = <>;
+interrupts = <30 0>;
+
+/* cm36672-specific properties */
+cm36672,prx_led_current = <2>;
+cm36672,prx_hd = <1>;
+};
+
diff --git a/MAINTAINERS b/MAINTAINERS
index ed42cb6..9618af4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2822,10 +2822,10 @@ F:  security/commoncap.c
 F: kernel/capability.c
 
 CAPELLA MICROSYSTEMS LIGHT SENSOR DRIVER
-M:     Kevin Tsai <kt...@capellamicro.com>
+M: Kevin Tsai <capellami...@gmail.com>
 S: Maintained
 F: drivers/iio/light/cm*
-F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+F: Documentation/devicetree/bindings/iio/light/cm*
 
 CAVIUM LIQUIDIO NETWORK DRIVER
 M: Derek Chickles <derek.chick...@caviumnetworks.com>
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 7c566f5..cb5052a 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -127,6 +127,17 @@ config CM36651
 To compile this driver as a module, choose M here:
 the module will be called cm36651.
 
+config CM36672
+   depends on I2C
+   tristate "CM36672 driver"
+   help
+Say Y here if you use cm36672.
+This option enables proximity sensors using Vishay
+Capella cm36672 device driver.
+
+To compile this driver as a module, choose M here:
+the module will be called cm36672.
+
 config GP2AP020A00F
tristate "Sharp GP2AP020A00F Proximity/ALS sensor"
depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 6f2a3c6..b155425 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_CM32181) += cm32181.o
 obj-$(CONFIG_CM3232)   += cm3232.o
 obj-$(CONFIG_CM3323)   += cm3323.o
 obj-$(CONFIG_CM36651)  += cm36651.o
+obj-$(CONFIG_CM36672)  += cm36672.o
 obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o
 obj-$(CONFIG_HID_SENSOR_ALS)   += hid-sensor-als.o
 obj-$(CONFIG_HID_SENSOR_PROX)  += hid-sensor-prox.o
diff --git a/drivers/iio/light/cm36672.c b/drivers/iio/light/cm36672.c
new file mode 100644
index 000..975629f
--- /dev/null
+++ b/drivers/iio/light/cm36672.c
@@ -0,0 +1,820 @@
+/*
+ * CM36672 Proximity Sensor
+ *
+ * Copyright (C) 2014-2016 Vishay Capella
+ * Author: Kevin Tsai <capellami...@gmail.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.
+ *
+ * IIO driver for CM36672 (7-bit I2C slave address 0x60).
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_ACPI
+#include 
+#endif
+
+#define CM36672_DRIVER_NAME"cm36672"
+#define CM36672_REGMAP_NAME"cm36672_regmap"
+
+/* Sen

[PATCH V2 1/1] iio: light: Added CM36672 Proximity Sensor Driver.

2016-06-09 Thread Kevin Tsai
Added Vishay Capella CM36672 Proximity Sensor IIO driver.  Support both
ACPI and Device Tree.

Signed-off-by: Kevin Tsai 
---
V2:
Thanks commends from Peter Meerwald-Stadler, Jonathan Cameron, and Linux
Walleij.  Updated for the following:
 - Remove unused defines.
 - Rewrite event registration.
 - Remove direct register values in the device tree.
 - Rewrite by regmap API.
 - Remove irq module parameter.
 - Correct cm36672_remove().

 .../devicetree/bindings/iio/light/cm36672.txt  |  37 +
 MAINTAINERS|   4 +-
 drivers/iio/light/Kconfig  |  11 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm36672.c| 820 +
 5 files changed, 871 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm36672.txt
 create mode 100644 drivers/iio/light/cm36672.c

diff --git a/Documentation/devicetree/bindings/iio/light/cm36672.txt 
b/Documentation/devicetree/bindings/iio/light/cm36672.txt
new file mode 100644
index 000..9681d2c
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm36672.txt
@@ -0,0 +1,37 @@
+* Vishay Capella CM36672 I2C Proximity sensor
+
+Required properties:
+- compatible: must be capella,cm36672"
+- reg: the I2C slave address, usually 0x60
+- interrupts: interrupt mapping for GPIO IRQ
+
+Optional properties:
+- interrupt-parent: interrupt controller
+- interrupts: interrupt mapping for GPIO IRQ
+- cm36672,prx_led_current: LED current, must be one of the following values:
+- 0: 50mA
+- 1: 75mA
+- 2: 100mA
+- 3: 120mA
+- 4: 140mA
+- 5: 160mA
+- 6: 180mA
+- 7: 200mA
+- cm36672,prx_hd: width of proximity sensor output data, must be one of the
+  following values:
+- 0: 12-bit
+- 1: 16-bit
+
+Example:
+
+cm36672@60 {
+compatible = "capella,cm36672";
+reg = <0x60>;
+interrupt-parent = <>;
+interrupts = <30 0>;
+
+/* cm36672-specific properties */
+cm36672,prx_led_current = <2>;
+cm36672,prx_hd = <1>;
+};
+
diff --git a/MAINTAINERS b/MAINTAINERS
index ed42cb6..9618af4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2822,10 +2822,10 @@ F:  security/commoncap.c
 F: kernel/capability.c
 
 CAPELLA MICROSYSTEMS LIGHT SENSOR DRIVER
-M: Kevin Tsai 
+M: Kevin Tsai 
 S: Maintained
 F: drivers/iio/light/cm*
-F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+F: Documentation/devicetree/bindings/iio/light/cm*
 
 CAVIUM LIQUIDIO NETWORK DRIVER
 M: Derek Chickles 
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 7c566f5..cb5052a 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -127,6 +127,17 @@ config CM36651
 To compile this driver as a module, choose M here:
 the module will be called cm36651.
 
+config CM36672
+   depends on I2C
+   tristate "CM36672 driver"
+   help
+Say Y here if you use cm36672.
+This option enables proximity sensors using Vishay
+Capella cm36672 device driver.
+
+To compile this driver as a module, choose M here:
+the module will be called cm36672.
+
 config GP2AP020A00F
tristate "Sharp GP2AP020A00F Proximity/ALS sensor"
depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 6f2a3c6..b155425 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_CM32181) += cm32181.o
 obj-$(CONFIG_CM3232)   += cm3232.o
 obj-$(CONFIG_CM3323)   += cm3323.o
 obj-$(CONFIG_CM36651)  += cm36651.o
+obj-$(CONFIG_CM36672)  += cm36672.o
 obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o
 obj-$(CONFIG_HID_SENSOR_ALS)   += hid-sensor-als.o
 obj-$(CONFIG_HID_SENSOR_PROX)  += hid-sensor-prox.o
diff --git a/drivers/iio/light/cm36672.c b/drivers/iio/light/cm36672.c
new file mode 100644
index 000..975629f
--- /dev/null
+++ b/drivers/iio/light/cm36672.c
@@ -0,0 +1,820 @@
+/*
+ * CM36672 Proximity Sensor
+ *
+ * Copyright (C) 2014-2016 Vishay Capella
+ * Author: Kevin Tsai 
+ *
+ * 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.
+ *
+ * IIO driver for CM36672 (7-bit I2C slave address 0x60).
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_ACPI
+#include 
+#endif
+
+#define CM36672_DRIVER_NAME"cm36672"
+#define CM36672_REGMAP_NAME"cm36672_regmap"
+
+/* Sensor registers */
+#define CM36672_ADDR_PRX_CONF  0x03
+#define CM36672_ADDR_PRX_CONF3 0x04
+#define CM36672_ADDR_PRX_THDL  0x06
+#define CM36672_A

[PATCH 1/1] iio: light: Added CM36672 Proximity Sensor Driver.

2016-05-28 Thread Kevin Tsai
Added Vishay Capella CM36672 Proximity Sensor IIO driver.  Support both
ACPI and Device Tree.

Signed-off-by: Kevin Tsai <capellami...@gmail.com>
---
 .../devicetree/bindings/i2c/trivial-devices.txt|   1 +
 .../devicetree/bindings/iio/light/cm36672.txt  |  54 ++
 MAINTAINERS|   3 +-
 drivers/iio/light/Kconfig  |  11 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm36672.c| 925 +
 6 files changed, 994 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm36672.txt
 create mode 100644 drivers/iio/light/cm36672.c

diff --git a/Documentation/devicetree/bindings/i2c/trivial-devices.txt 
b/Documentation/devicetree/bindings/i2c/trivial-devices.txt
index 53987449..a66f052 100644
--- a/Documentation/devicetree/bindings/i2c/trivial-devices.txt
+++ b/Documentation/devicetree/bindings/i2c/trivial-devices.txt
@@ -25,6 +25,7 @@ at,24c08  i2c serial eeprom  (24cxx)
 atmel,at97sc3204t  i2c trusted platform module (TPM)
 capella,cm32181CM32181: Ambient Light Sensor
 capella,cm3232 CM3232: Ambient Light Sensor
+capella,cm36672CM36672: Proximity Sensor
 cirrus,cs42l51 Cirrus Logic CS42L51 audio codec
 dallas,ds1307  64 x 8, Serial, I2C Real-Time Clock
 dallas,ds1338  I2C RTC with 56-Byte NV RAM
diff --git a/Documentation/devicetree/bindings/iio/light/cm36672.txt 
b/Documentation/devicetree/bindings/iio/light/cm36672.txt
new file mode 100644
index 000..2c29f9f
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm36672.txt
@@ -0,0 +1,54 @@
+* Vishay Capella CM36672 Proximity Sensor
+
+Required properties:
+- compatible: must be "capella,cm36672"
+- reg: the I2C address of the device
+- interrupts: interrupt-specifier for the sole interrupt generated by the 
device
+
+Optionoal properties:
+- capella,reg_prx_conf: PS_CONF1 and PS_CONF2 registers
+- capella,reg_prx_conf3: PS_CONF3 and PS_MS registers
+- capella,reg_prx_canc: PS cancellation level setting
+- capella,reg_prx_thdl: PS low interrupt threshold setting
+- capella,reg_prx_thdh: PS high interrupt threshold setting
+- capella,name_prx_int_rising: enable/disable rising interrupt
+- capella,name_prx_int_falling: enable/disable falling interrupt
+- capella,name_prx_duty: PS duty ratio setting
+- capella,name_prx_pers: persistence setting
+- capella,name_prx_it: integration time setting
+- capella,name_prx_led_current: LED current selection
+- capella,name_prx_threshold_falling: interrupt falling threshold
+- capella,name_prx_threshold_rising: interrupt rising threshold
+
+Example:
+
+   i2c_cm36672: i2c-gpio {
+   /* ... */
+
+   cm36672@60 {
+   compatible = "capella,cm36672";
+   reg = <0x60>;
+   interrupt-parent = <>;
+   interrupts = <30 0>;
+
+   /* Initialized by register */
+   capella,reg_prx_conf = <0x0322>;
+   capella,reg_prx_conf3 = <0x>;
+   capella,reg_prx_canc = <0x>;
+   capella,reg_prx_thdl = <0x0007>;
+   capella,reg_prx_thdh = <0x000A>;
+
+   /* Or, initialized by name */
+   capella,name_prx_int_rising = <1>;
+   capella,name_prx_int_falling = <1>;
+   capella,name_prx_duty = <0>;
+   capella,name_prx_pers = <2>;
+   capella,name_prx_it = <1>;
+   capella,name_prx_led_current = <0>;
+   capella,name_prx_threshold_falling = <7>;
+   capella,name_prx_threshold_rising = <10>;
+   };
+
+   /* ... */
+   };
+
diff --git a/MAINTAINERS b/MAINTAINERS
index ed1229e..e9962a1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2805,10 +2805,11 @@ F:  security/commoncap.c
 F: kernel/capability.c
 
 CAPELLA MICROSYSTEMS LIGHT SENSOR DRIVER
-M: Kevin Tsai <kt...@capellamicro.com>
+M: Kevin Tsai <capellami...@gmail.com>
 S: Maintained
 F: drivers/iio/light/cm*
 F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+F: Documentation/devicetree/bindings/iio/light/cm*.txt
 
 CAVIUM LIQUIDIO NETWORK DRIVER
 M: Derek Chickles <derek.chick...@caviumnetworks.com>
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 7c566f5..1fa91ae 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -127,6 +127,17 @@ config CM36651
 To compile this driver as a module, choose M here:
 the module will be called cm36651.
 
+config CM36672
+

[PATCH 1/1] iio: light: Added CM36672 Proximity Sensor Driver.

2016-05-28 Thread Kevin Tsai
Added Vishay Capella CM36672 Proximity Sensor IIO driver.  Support both
ACPI and Device Tree.

Signed-off-by: Kevin Tsai 
---
 .../devicetree/bindings/i2c/trivial-devices.txt|   1 +
 .../devicetree/bindings/iio/light/cm36672.txt  |  54 ++
 MAINTAINERS|   3 +-
 drivers/iio/light/Kconfig  |  11 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm36672.c| 925 +
 6 files changed, 994 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm36672.txt
 create mode 100644 drivers/iio/light/cm36672.c

diff --git a/Documentation/devicetree/bindings/i2c/trivial-devices.txt 
b/Documentation/devicetree/bindings/i2c/trivial-devices.txt
index 53987449..a66f052 100644
--- a/Documentation/devicetree/bindings/i2c/trivial-devices.txt
+++ b/Documentation/devicetree/bindings/i2c/trivial-devices.txt
@@ -25,6 +25,7 @@ at,24c08  i2c serial eeprom  (24cxx)
 atmel,at97sc3204t  i2c trusted platform module (TPM)
 capella,cm32181CM32181: Ambient Light Sensor
 capella,cm3232 CM3232: Ambient Light Sensor
+capella,cm36672CM36672: Proximity Sensor
 cirrus,cs42l51 Cirrus Logic CS42L51 audio codec
 dallas,ds1307  64 x 8, Serial, I2C Real-Time Clock
 dallas,ds1338  I2C RTC with 56-Byte NV RAM
diff --git a/Documentation/devicetree/bindings/iio/light/cm36672.txt 
b/Documentation/devicetree/bindings/iio/light/cm36672.txt
new file mode 100644
index 000..2c29f9f
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm36672.txt
@@ -0,0 +1,54 @@
+* Vishay Capella CM36672 Proximity Sensor
+
+Required properties:
+- compatible: must be "capella,cm36672"
+- reg: the I2C address of the device
+- interrupts: interrupt-specifier for the sole interrupt generated by the 
device
+
+Optionoal properties:
+- capella,reg_prx_conf: PS_CONF1 and PS_CONF2 registers
+- capella,reg_prx_conf3: PS_CONF3 and PS_MS registers
+- capella,reg_prx_canc: PS cancellation level setting
+- capella,reg_prx_thdl: PS low interrupt threshold setting
+- capella,reg_prx_thdh: PS high interrupt threshold setting
+- capella,name_prx_int_rising: enable/disable rising interrupt
+- capella,name_prx_int_falling: enable/disable falling interrupt
+- capella,name_prx_duty: PS duty ratio setting
+- capella,name_prx_pers: persistence setting
+- capella,name_prx_it: integration time setting
+- capella,name_prx_led_current: LED current selection
+- capella,name_prx_threshold_falling: interrupt falling threshold
+- capella,name_prx_threshold_rising: interrupt rising threshold
+
+Example:
+
+   i2c_cm36672: i2c-gpio {
+   /* ... */
+
+   cm36672@60 {
+   compatible = "capella,cm36672";
+   reg = <0x60>;
+   interrupt-parent = <>;
+   interrupts = <30 0>;
+
+   /* Initialized by register */
+   capella,reg_prx_conf = <0x0322>;
+   capella,reg_prx_conf3 = <0x>;
+   capella,reg_prx_canc = <0x>;
+   capella,reg_prx_thdl = <0x0007>;
+   capella,reg_prx_thdh = <0x000A>;
+
+   /* Or, initialized by name */
+   capella,name_prx_int_rising = <1>;
+   capella,name_prx_int_falling = <1>;
+   capella,name_prx_duty = <0>;
+   capella,name_prx_pers = <2>;
+   capella,name_prx_it = <1>;
+   capella,name_prx_led_current = <0>;
+   capella,name_prx_threshold_falling = <7>;
+   capella,name_prx_threshold_rising = <10>;
+   };
+
+   /* ... */
+   };
+
diff --git a/MAINTAINERS b/MAINTAINERS
index ed1229e..e9962a1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2805,10 +2805,11 @@ F:  security/commoncap.c
 F: kernel/capability.c
 
 CAPELLA MICROSYSTEMS LIGHT SENSOR DRIVER
-M: Kevin Tsai 
+M: Kevin Tsai 
 S: Maintained
 F: drivers/iio/light/cm*
 F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+F: Documentation/devicetree/bindings/iio/light/cm*.txt
 
 CAVIUM LIQUIDIO NETWORK DRIVER
 M: Derek Chickles 
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 7c566f5..1fa91ae 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -127,6 +127,17 @@ config CM36651
 To compile this driver as a module, choose M here:
 the module will be called cm36651.
 
+config CM36672
+   depends on I2C
+   tristate "CM36672 proximity sensor"
+help
+Say Y here if you use cm3

[PATCH V2 1/1] iio: Add iio_mod_light_uva, iio_mod_light_uvb, and iio_mod_light_uvc.

2015-06-04 Thread Kevin Tsai
Add Ultraviolet(UV) support:
UVA: 315 ~ 400 nm
UVB: 280 ~ 315 nm
UVC: 100 ~ 280 nm

/sys/bus/iio/devices/iio:deviceX/in_intensity_uva_input
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvb_input
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvc_input
The SI unit is "uW/mm^2".

/sys/bus/iio/devices/iio:deviceX/in_intensity_uva_min_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uva_max_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvb_min_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvb_max_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvc_min_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvc_max_wavelength
The SI unit is "nm".

Signed-off-by: Kevin Tsai 
---
 Documentation/ABI/testing/sysfs-bus-iio | 23 +++
 drivers/iio/industrialio-core.c |  3 +++
 include/uapi/linux/iio/types.h  |  3 +++
 tools/iio/iio_event_monitor.c   |  6 ++
 4 files changed, 35 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-bus-iio 
b/Documentation/ABI/testing/sysfs-bus-iio
index bbed111..2960142 100644
--- a/Documentation/ABI/testing/sysfs-bus-iio
+++ b/Documentation/ABI/testing/sysfs-bus-iio
@@ -1463,3 +1463,26 @@ Description:
measurements and return the average value as output data. Each
value resulted from [_name]_oversampling_ratio 
measurements
is considered as one sample for 
[_name]_sampling_frequency.
+
+What:  /sys/.../iio:deviceX/in_intensity_uva[_input|_raw]
+What:  /sys/.../iio:deviceX/in_intensity_uvb[_input|_raw]
+What:  /sys/.../iio:deviceX/in_intensity_uvc[_input|_raw]
+KernelVersion: 4.1.1
+Description:
+   uva is the ultraviolet wavelength from 315nm to 400nm.
+   uvb is the ultraviolet wavelength from 280nm to 315nm.
+   uvc is the ultraviolet wavelength from 100nm to 280nm.
+   The SI unit is "uW/mm^2". Otherwise it should include _raw.
+
+What:  /sys/.../iio:deviceX/in_intensity_uva_min_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uva_max_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uvb_min_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uvb_max_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uvc_min_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uvc_max_wavelength
+KernelVersion: 4.1.1
+Description:
+   Sensor may not cover the whole uva/uvb/uvc channel. Add the
+   min/max wavelength properties to descript sensor range.
+   The SI unit is "nm".
+
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 3524b0d..ecbdd1e 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -89,6 +89,9 @@ static const char * const iio_modifier_names[] = {
[IIO_MOD_LIGHT_RED] = "red",
[IIO_MOD_LIGHT_GREEN] = "green",
[IIO_MOD_LIGHT_BLUE] = "blue",
+   [IIO_MOD_LIGHT_UVA] = "uva",
+   [IIO_MOD_LIGHT_UVB] = "uvb",
+   [IIO_MOD_LIGHT_UVC] = "uvc",
[IIO_MOD_QUATERNION] = "quaternion",
[IIO_MOD_TEMP_AMBIENT] = "ambient",
[IIO_MOD_TEMP_OBJECT] = "object",
diff --git a/include/uapi/linux/iio/types.h b/include/uapi/linux/iio/types.h
index 2f8b117..5c99090 100644
--- a/include/uapi/linux/iio/types.h
+++ b/include/uapi/linux/iio/types.h
@@ -72,6 +72,9 @@ enum iio_modifier {
IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
IIO_MOD_I,
IIO_MOD_Q,
+   IIO_MOD_LIGHT_UVA,
+   IIO_MOD_LIGHT_UVB,
+   IIO_MOD_LIGHT_UVC,
 };
 
 enum iio_event_type {
diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c
index 427c271..46dd27f 100644
--- a/tools/iio/iio_event_monitor.c
+++ b/tools/iio/iio_event_monitor.c
@@ -88,6 +88,9 @@ static const char * const iio_modifier_names[] = {
[IIO_MOD_LIGHT_RED] = "red",
[IIO_MOD_LIGHT_GREEN] = "green",
[IIO_MOD_LIGHT_BLUE] = "blue",
+   [IIO_MOD_LIGHT_UVA] = "uva",
+   [IIO_MOD_LIGHT_UVB] = "uvb",
+   [IIO_MOD_LIGHT_UVC] = "uvc",
[IIO_MOD_QUATERNION] = "quaternion",
[IIO_MOD_TEMP_AMBIENT] = "ambient",
[IIO_MOD_TEMP_OBJECT] = "object",
@@ -156,6 +159,9 @@ static bool event_is_known(struct iio_event_data *event)
case IIO_MOD_LIGHT_RED:
case IIO_MOD_LIGHT_GREEN:
case IIO_MOD_LIGHT_BLUE:
+   case IIO_MOD_LIGHT_UVA:
+   case IIO_MOD_LIGHT_UVB:
+   case IIO_MOD_LIGHT_UVC:
case IIO_MOD_QUATERNION:
case IIO_MOD_TEMP_AMBIENT:
case IIO_MOD_TEMP_OBJECT:
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH V2 1/1] iio: Add iio_mod_light_uva, iio_mod_light_uvb, and iio_mod_light_uvc.

2015-06-04 Thread Kevin Tsai
Add Ultraviolet(UV) support:
UVA: 315 ~ 400 nm
UVB: 280 ~ 315 nm
UVC: 100 ~ 280 nm

/sys/bus/iio/devices/iio:deviceX/in_intensity_uva_input
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvb_input
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvc_input
The SI unit is uW/mm^2.

/sys/bus/iio/devices/iio:deviceX/in_intensity_uva_min_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uva_max_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvb_min_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvb_max_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvc_min_wavelength
/sys/bus/iio/devices/iio:deviceX/in_intensity_uvc_max_wavelength
The SI unit is nm.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 Documentation/ABI/testing/sysfs-bus-iio | 23 +++
 drivers/iio/industrialio-core.c |  3 +++
 include/uapi/linux/iio/types.h  |  3 +++
 tools/iio/iio_event_monitor.c   |  6 ++
 4 files changed, 35 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-bus-iio 
b/Documentation/ABI/testing/sysfs-bus-iio
index bbed111..2960142 100644
--- a/Documentation/ABI/testing/sysfs-bus-iio
+++ b/Documentation/ABI/testing/sysfs-bus-iio
@@ -1463,3 +1463,26 @@ Description:
measurements and return the average value as output data. Each
value resulted from type[_name]_oversampling_ratio 
measurements
is considered as one sample for 
type[_name]_sampling_frequency.
+
+What:  /sys/.../iio:deviceX/in_intensity_uva[_input|_raw]
+What:  /sys/.../iio:deviceX/in_intensity_uvb[_input|_raw]
+What:  /sys/.../iio:deviceX/in_intensity_uvc[_input|_raw]
+KernelVersion: 4.1.1
+Description:
+   uva is the ultraviolet wavelength from 315nm to 400nm.
+   uvb is the ultraviolet wavelength from 280nm to 315nm.
+   uvc is the ultraviolet wavelength from 100nm to 280nm.
+   The SI unit is uW/mm^2. Otherwise it should include _raw.
+
+What:  /sys/.../iio:deviceX/in_intensity_uva_min_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uva_max_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uvb_min_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uvb_max_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uvc_min_wavelength
+What:  /sys/.../iio:deviceX/in_intensity_uvc_max_wavelength
+KernelVersion: 4.1.1
+Description:
+   Sensor may not cover the whole uva/uvb/uvc channel. Add the
+   min/max wavelength properties to descript sensor range.
+   The SI unit is nm.
+
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 3524b0d..ecbdd1e 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -89,6 +89,9 @@ static const char * const iio_modifier_names[] = {
[IIO_MOD_LIGHT_RED] = red,
[IIO_MOD_LIGHT_GREEN] = green,
[IIO_MOD_LIGHT_BLUE] = blue,
+   [IIO_MOD_LIGHT_UVA] = uva,
+   [IIO_MOD_LIGHT_UVB] = uvb,
+   [IIO_MOD_LIGHT_UVC] = uvc,
[IIO_MOD_QUATERNION] = quaternion,
[IIO_MOD_TEMP_AMBIENT] = ambient,
[IIO_MOD_TEMP_OBJECT] = object,
diff --git a/include/uapi/linux/iio/types.h b/include/uapi/linux/iio/types.h
index 2f8b117..5c99090 100644
--- a/include/uapi/linux/iio/types.h
+++ b/include/uapi/linux/iio/types.h
@@ -72,6 +72,9 @@ enum iio_modifier {
IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
IIO_MOD_I,
IIO_MOD_Q,
+   IIO_MOD_LIGHT_UVA,
+   IIO_MOD_LIGHT_UVB,
+   IIO_MOD_LIGHT_UVC,
 };
 
 enum iio_event_type {
diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c
index 427c271..46dd27f 100644
--- a/tools/iio/iio_event_monitor.c
+++ b/tools/iio/iio_event_monitor.c
@@ -88,6 +88,9 @@ static const char * const iio_modifier_names[] = {
[IIO_MOD_LIGHT_RED] = red,
[IIO_MOD_LIGHT_GREEN] = green,
[IIO_MOD_LIGHT_BLUE] = blue,
+   [IIO_MOD_LIGHT_UVA] = uva,
+   [IIO_MOD_LIGHT_UVB] = uvb,
+   [IIO_MOD_LIGHT_UVC] = uvc,
[IIO_MOD_QUATERNION] = quaternion,
[IIO_MOD_TEMP_AMBIENT] = ambient,
[IIO_MOD_TEMP_OBJECT] = object,
@@ -156,6 +159,9 @@ static bool event_is_known(struct iio_event_data *event)
case IIO_MOD_LIGHT_RED:
case IIO_MOD_LIGHT_GREEN:
case IIO_MOD_LIGHT_BLUE:
+   case IIO_MOD_LIGHT_UVA:
+   case IIO_MOD_LIGHT_UVB:
+   case IIO_MOD_LIGHT_UVC:
case IIO_MOD_QUATERNION:
case IIO_MOD_TEMP_AMBIENT:
case IIO_MOD_TEMP_OBJECT:
-- 
1.8.3.1

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 1/1] iio: Add iio_mod_light_uva, iio_mod_light_uvb, and iio_mod_light_uvc.

2015-05-29 Thread Kevin Tsai
Add Ultraviolet(UV) support:
UVA: 315 ~ 400 nm
UVB: 280 ~ 315 nm
UVC: 100 ~ 280 nm

Signed-off-by: Kevin Tsai 
---
 drivers/iio/industrialio-core.c | 3 +++
 include/uapi/linux/iio/types.h  | 3 +++
 tools/iio/iio_event_monitor.c   | 6 ++
 3 files changed, 12 insertions(+)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 3524b0d..ecbdd1e 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -89,6 +89,9 @@ static const char * const iio_modifier_names[] = {
[IIO_MOD_LIGHT_RED] = "red",
[IIO_MOD_LIGHT_GREEN] = "green",
[IIO_MOD_LIGHT_BLUE] = "blue",
+   [IIO_MOD_LIGHT_UVA] = "uva",
+   [IIO_MOD_LIGHT_UVB] = "uvb",
+   [IIO_MOD_LIGHT_UVC] = "uvc",
[IIO_MOD_QUATERNION] = "quaternion",
[IIO_MOD_TEMP_AMBIENT] = "ambient",
[IIO_MOD_TEMP_OBJECT] = "object",
diff --git a/include/uapi/linux/iio/types.h b/include/uapi/linux/iio/types.h
index 2f8b117..5c99090 100644
--- a/include/uapi/linux/iio/types.h
+++ b/include/uapi/linux/iio/types.h
@@ -72,6 +72,9 @@ enum iio_modifier {
IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
IIO_MOD_I,
IIO_MOD_Q,
+   IIO_MOD_LIGHT_UVA,
+   IIO_MOD_LIGHT_UVB,
+   IIO_MOD_LIGHT_UVC,
 };
 
 enum iio_event_type {
diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c
index 427c271..46dd27f 100644
--- a/tools/iio/iio_event_monitor.c
+++ b/tools/iio/iio_event_monitor.c
@@ -88,6 +88,9 @@ static const char * const iio_modifier_names[] = {
[IIO_MOD_LIGHT_RED] = "red",
[IIO_MOD_LIGHT_GREEN] = "green",
[IIO_MOD_LIGHT_BLUE] = "blue",
+   [IIO_MOD_LIGHT_UVA] = "uva",
+   [IIO_MOD_LIGHT_UVB] = "uvb",
+   [IIO_MOD_LIGHT_UVC] = "uvc",
[IIO_MOD_QUATERNION] = "quaternion",
[IIO_MOD_TEMP_AMBIENT] = "ambient",
[IIO_MOD_TEMP_OBJECT] = "object",
@@ -156,6 +159,9 @@ static bool event_is_known(struct iio_event_data *event)
case IIO_MOD_LIGHT_RED:
case IIO_MOD_LIGHT_GREEN:
case IIO_MOD_LIGHT_BLUE:
+   case IIO_MOD_LIGHT_UVA:
+   case IIO_MOD_LIGHT_UVB:
+   case IIO_MOD_LIGHT_UVC:
case IIO_MOD_QUATERNION:
case IIO_MOD_TEMP_AMBIENT:
case IIO_MOD_TEMP_OBJECT:
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 1/1] iio: Add iio_mod_light_uva, iio_mod_light_uvb, and iio_mod_light_uvc.

2015-05-29 Thread Kevin Tsai
Add Ultraviolet(UV) support:
UVA: 315 ~ 400 nm
UVB: 280 ~ 315 nm
UVC: 100 ~ 280 nm

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 drivers/iio/industrialio-core.c | 3 +++
 include/uapi/linux/iio/types.h  | 3 +++
 tools/iio/iio_event_monitor.c   | 6 ++
 3 files changed, 12 insertions(+)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 3524b0d..ecbdd1e 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -89,6 +89,9 @@ static const char * const iio_modifier_names[] = {
[IIO_MOD_LIGHT_RED] = red,
[IIO_MOD_LIGHT_GREEN] = green,
[IIO_MOD_LIGHT_BLUE] = blue,
+   [IIO_MOD_LIGHT_UVA] = uva,
+   [IIO_MOD_LIGHT_UVB] = uvb,
+   [IIO_MOD_LIGHT_UVC] = uvc,
[IIO_MOD_QUATERNION] = quaternion,
[IIO_MOD_TEMP_AMBIENT] = ambient,
[IIO_MOD_TEMP_OBJECT] = object,
diff --git a/include/uapi/linux/iio/types.h b/include/uapi/linux/iio/types.h
index 2f8b117..5c99090 100644
--- a/include/uapi/linux/iio/types.h
+++ b/include/uapi/linux/iio/types.h
@@ -72,6 +72,9 @@ enum iio_modifier {
IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
IIO_MOD_I,
IIO_MOD_Q,
+   IIO_MOD_LIGHT_UVA,
+   IIO_MOD_LIGHT_UVB,
+   IIO_MOD_LIGHT_UVC,
 };
 
 enum iio_event_type {
diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c
index 427c271..46dd27f 100644
--- a/tools/iio/iio_event_monitor.c
+++ b/tools/iio/iio_event_monitor.c
@@ -88,6 +88,9 @@ static const char * const iio_modifier_names[] = {
[IIO_MOD_LIGHT_RED] = red,
[IIO_MOD_LIGHT_GREEN] = green,
[IIO_MOD_LIGHT_BLUE] = blue,
+   [IIO_MOD_LIGHT_UVA] = uva,
+   [IIO_MOD_LIGHT_UVB] = uvb,
+   [IIO_MOD_LIGHT_UVC] = uvc,
[IIO_MOD_QUATERNION] = quaternion,
[IIO_MOD_TEMP_AMBIENT] = ambient,
[IIO_MOD_TEMP_OBJECT] = object,
@@ -156,6 +159,9 @@ static bool event_is_known(struct iio_event_data *event)
case IIO_MOD_LIGHT_RED:
case IIO_MOD_LIGHT_GREEN:
case IIO_MOD_LIGHT_BLUE:
+   case IIO_MOD_LIGHT_UVA:
+   case IIO_MOD_LIGHT_UVB:
+   case IIO_MOD_LIGHT_UVC:
case IIO_MOD_QUATERNION:
case IIO_MOD_TEMP_AMBIENT:
case IIO_MOD_TEMP_OBJECT:
-- 
1.8.3.1

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 1/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Renamed company name.
- Removed cm32181_reg.
- Removed white space.
- Removed unused include files.
- Updated macro definitions.
- Renamed cm32181_chip pointer to chip.

Signed-off-by: Kevin Tsai 
---
 drivers/iio/light/Kconfig   |   4 +-
 drivers/iio/light/cm32181.c | 126 +++-
 2 files changed, 67 insertions(+), 63 deletions(-)

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index a437bad..583aa6a 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -39,11 +39,11 @@ config APDS9300
 
 config CM32181
depends on I2C
-   tristate "CM32181 driver"
+   tristate "Vishay Capella CM32181 driver"
help
 Say Y here if you use cm32181.
 This option enables ambient light sensor using
-Capella cm32181 device driver.
+Vishay Capella cm32181 device driver.
 
 To compile this driver as a module, choose M here:
 the module will be called cm32181.
diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 5d12ae54..0491d73 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -1,19 +1,15 @@
 /*
- * Copyright (C) 2013 Capella Microsystems Inc.
- * Author: Kevin Tsai 
+ * Copyright (C) 2013-2015 Vishay Capella
  *
  * 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 
-#include 
 #include 
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -21,36 +17,43 @@
 
 /* Registers Address */
 #define CM32181_REG_ADDR_CMD   0x00
+#define CM32181_REG_ADDR_ALS_WH0x01
+#define CM32181_REG_ADDR_ALS_WL0x02
 #define CM32181_REG_ADDR_ALS   0x04
 #define CM32181_REG_ADDR_STATUS0x06
 #define CM32181_REG_ADDR_ID0x07
 
 /* Number of Configurable Registers */
-#define CM32181_CONF_REG_NUM   0x01
+#define CM32181_CONF_REG_NUM   3
 
 /* CMD register */
-#define CM32181_CMD_ALS_ENABLE 0x00
-#define CM32181_CMD_ALS_DISABLE0x01
-#define CM32181_CMD_ALS_INT_EN 0x02
+#define CM32181_CMD_ALS_DISABLEBIT(0)
+#define CM32181_CMD_ALS_INT_EN BIT(1)
 
 #define CM32181_CMD_ALS_IT_SHIFT   6
-#define CM32181_CMD_ALS_IT_MASK(0x0F << 
CM32181_CMD_ALS_IT_SHIFT)
+#define CM32181_CMD_ALS_IT_MASK(BIT(6) | BIT(7) | BIT(8) | 
BIT(9))
 #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT)
 
 #define CM32181_CMD_ALS_SM_SHIFT   11
-#define CM32181_CMD_ALS_SM_MASK(0x03 << 
CM32181_CMD_ALS_SM_SHIFT)
+#define CM32181_CMD_ALS_SM_MASK(BIT(11) | BIT(12))
 #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT)
 
+#define CM32181_CMD_DEFAULT(CM32181_CMD_ALS_IT_DEFAULT | \
+   CM32181_CMD_ALS_SM_DEFAULT)
+
+/* ALS_WH register */
+#define CM32181_ALS_WH_DEFAULT 0x
+
+/* ALS_WL register */
+#define CM32181_ALS_WL_DEFAULT 0x
+
+/* Software parameters */
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define MLUX_PER_LUX   1000
 
-static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
-   CM32181_REG_ADDR_CMD,
-};
-
 static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
@@ -64,15 +67,15 @@ struct cm32181_chip {
 
 /**
  * cm32181_reg_init() - Initialize CM32181 registers
- * @cm32181:   pointer of struct cm32181.
+ * @chip:  pointer of struct cm32181_chip
  *
  * Initialize CM32181 ambient light sensor register to default values.
  *
  * Return: 0 for success; otherwise for error code.
  */
-static int cm32181_reg_init(struct cm32181_chip *cm32181)
+static int cm32181_reg_init(struct cm32181_chip *chip)
 {
-   struct i2c_client *client = cm32181->client;
+   struct i2c_client *client = chip->client;
int i;
s32 ret;
 
@@ -85,14 +88,15 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
return -ENODEV;
 
/* Default Values */
-   cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE |
-   CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
-   cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
+   chip->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_DEFAULT;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WH] = CM32181_ALS_WH_DEFAULT;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WL] = CM32181_ALS_WL_DEFAULT;
+   chip-

[PATCH 4/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Replaced als_it_bits and als_it_value by cm32181_als_it_scale.

Signed-off-by: Kevin Tsai 
---
 drivers/iio/light/cm32181.c | 95 -
 1 file changed, 76 insertions(+), 19 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 9bc3e1f..54bf0cb 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -53,12 +53,25 @@
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
-#define MLUX_PER_LUX   1000
+#define CM32181_MLUX_PER_LUX   1000
 
 static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
 
+static const struct {
+   int val;
+   int val2;
+   u16 it;
+} cm32181_als_it_scales[] = {
+   {0, 25000, 12}, /* 0.025000 */
+   {0, 5, 8},  /* 0.05 */
+   {0, 10, 0}, /* 0.10 */
+   {0, 20, 1}, /* 0.20 */
+   {0, 40, 2}, /* 0.40 */
+   {0, 80, 3}, /* 0.80 */
+};
+
 struct cm32181_als_info {
u8 hw_id;
u16 reg_cmd;
@@ -129,15 +142,16 @@ static int cm32181_reg_init(struct cm32181_chip *chip)
 }
 
 /**
- * cm32181_read_als_it() - Get sensor integration time (ms)
+ * cm32181_read_als_it() - Get sensor integration time
  * @chip:  pointer of struct cm32181_chip
- * @val2:  pointer of int to load the als_it value.
+ * @val:   pointer of int to load the integration (sec)
+ * @val2:  pointer of int to load the integration time (microsecond)
  *
  * Report the current integartion time by millisecond.
  *
  * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
  */
-static int cm32181_read_als_it(struct cm32181_chip *chip, int *val2)
+static int cm32181_read_als_it(struct cm32181_chip *chip, int *val, int *val2)
 {
u16 als_it;
int i;
@@ -145,9 +159,10 @@ static int cm32181_read_als_it(struct cm32181_chip *chip, 
int *val2)
als_it = chip->conf_regs[CM32181_REG_ADDR_CMD];
als_it &= CM32181_CMD_ALS_IT_MASK;
als_it >>= CM32181_CMD_ALS_IT_SHIFT;
-   for (i = 0; i < ARRAY_SIZE(als_it_bits); i++) {
-   if (als_it == als_it_bits[i]) {
-   *val2 = als_it_value[i];
+   for (i = 0; i < ARRAY_SIZE(cm32181_als_it_scales); i++) {
+   if (als_it == cm32181_als_it_scales[i].it) {
+   *val = cm32181_als_it_scales[i].val;
+   *val2 = cm32181_als_it_scales[i].val2;
return IIO_VAL_INT_PLUS_MICRO;
}
}
@@ -158,15 +173,44 @@ static int cm32181_read_als_it(struct cm32181_chip *chip, 
int *val2)
 /**
  * cm32181_write_als_it() - Write sensor integration time
  * @chip:  pointer of struct cm32181_chip.
- * @val:   integration time by millisecond.
+ * @val:   integration time in second.
+ * @val2:  integration time in microsecond.
  *
  * Convert integration time (ms) to sensor value.
  *
  * Return: i2c_smbus_write_word_data command return value.
  */
-static int cm32181_write_als_it(struct cm32181_chip *chip, int val)
+static int cm32181_write_als_it(struct cm32181_chip *chip, int val, int val2)
 {
struct i2c_client *client = chip->client;
+   u16 als_it, cmd;
+   int i;
+   s32 ret;
+
+   for (i = 0; i < ARRAY_SIZE(cm32181_als_it_scales); i++) {
+   if (val == cm32181_als_it_scales[i].val &&
+   val2 == cm32181_als_it_scales[i].val2) {
+
+   als_it = cm32181_als_it_scales[i].it;
+   als_it <<= CM32181_CMD_ALS_IT_SHIFT;
+
+   cmd = chip->conf_regs[CM32181_REG_ADDR_CMD];
+   cmd &= ~CM32181_CMD_ALS_IT_MASK;
+   cmd |= als_it;
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_CMD,
+   cmd);
+   if (ret < 0)
+   return ret;
+
+   chip->conf_regs[CM32181_REG_ADDR_CMD] = cmd;
+   return 0;
+   }
+   }
+   return -EINVAL;
+
+/*
+   struct i2c_client *client = chip->client;
u16 als_it;
int ret, i, n;
 
@@ -190,6 +234,7 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val)
mutex_unlock(>lock);
 
return ret;
+*/
 }
 
 /**
@@ -204,26 +249,29 @@ static int cm32181_write_als_it(struct cm32181_chip 
*chip, int val)
 static int cm32181_get_lux(struct cm32181_chip *chip)
 {
struct i2c_client *client = chip->client;
+   struct cm32181_als_info *als_info = chip->als_info;
int ret;
+   int val, val2;
int a

[PATCH 6/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Added Interrupt support.

Signed-off-by: Kevin Tsai 
---
 drivers/iio/light/cm32181.c | 156 +++-
 1 file changed, 153 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index b7abd46..1ae32a0 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -47,6 +47,10 @@
 /* ALS_WL register */
 #define CM32181_ALS_WL_DEFAULT 0x
 
+/* STATUS register */
+#define CM32181_STATUS_ALS_IF_LBIT(15)
+#define CM32181_STATUS_ALS_IF_HBIT(14)
+
 /* Software parameters */
 #define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
@@ -122,7 +126,8 @@ void cm32181_parse_dt(struct cm32181_chip *chip)
if (!of_property_read_u32(dn, "capella,mlux_per_bit", _val))
als_info->mlux_per_bit = (int)temp_val;
if (!of_property_read_u32(dn, "capella,thd_percent", _val))
-   als_info->thd_percent = (int)temp_val;
+   if (((int)temp_val >= 0) && ((int)temp_val < 100))
+   als_info->thd_percent = (int)temp_val;
 }
 #endif
 
@@ -173,6 +178,63 @@ static int cm32181_reg_init(struct cm32181_chip *chip)
 }
 
 /**
+ * cm32181_interrupt() - enable/disable interrupt
+ * @chip:   pointer of struct cm32181_chip
+ * @ int_en:   truen for enable; false for disable
+ *
+ * Enable/disable interrupt mode
+ *
+ * Return: 0 for success; otherwise for error code.
+ */
+static int cm32181_interrupt(struct cm32181_chip *cm32181, bool int_en)
+{
+   int ret;
+
+   mutex_lock(>lock);
+   if (int_en)
+   cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
+   CM32181_CMD_ALS_INT_EN;
+   else
+   cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
+   ~CM32181_CMD_ALS_INT_EN;
+
+   ret = i2c_smbus_write_word_data(cm32181->client,
+   CM32181_REG_ADDR_CMD,
+   cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
+   mutex_unlock(>lock);
+   return ret;
+}
+
+static irqreturn_t cm32181_irq_handler(int irq, void *data)
+{
+   struct iio_dev *indio_dev = data;
+   struct cm32181_chip *cm32181 = iio_priv(indio_dev);
+   struct i2c_client *client = cm32181->client;
+   int ret;
+   u64 ev_code;
+
+   ret = i2c_smbus_read_word_data(cm32181->client,
+   CM32181_REG_ADDR_STATUS);
+   if (ret < 0) {
+   dev_err(>dev,
+   "%s: Data read failed: %d\n", __func__, ret);
+   return IRQ_NONE;
+   }
+
+   if (!(ret & (CM32181_STATUS_ALS_IF_H | CM32181_STATUS_ALS_IF_L)))
+   return IRQ_NONE;
+
+   ev_code = IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
+   0,
+   IIO_EV_TYPE_THRESH,
+   IIO_EV_DIR_EITHER);
+
+   iio_push_event(indio_dev, ev_code, iio_get_time_ns());
+
+   return IRQ_HANDLED;
+}
+
+/**
  * cm32181_read_als_it() - Get sensor integration time
  * @chip:  pointer of struct cm32181_chip
  * @val:   pointer of int to load the integration (sec)
@@ -242,6 +304,51 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val, int val2)
 }
 
 /**
+ * cm32181_ials_read() - read ALS raw data
+ * @chip:   pointer of struct cm32181_chip
+ *
+ * Read the ALS raw data and update the interrupt threshold windows.
+ *
+ * Return: Positive value is ALS raw data, otherwise is error code.
+ */
+static int cm32181_als_read(struct cm32181_chip *chip)
+{
+   struct i2c_client *client = chip->client;
+   struct cm32181_als_info *als_info = chip->als_info;
+   u16 als, wh, wl, delta;
+   int ret;
+
+   ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
+   if (ret < 0)
+   return ret;
+
+   als = (u16)ret;
+
+   if (als_info->thd_percent) {
+   delta = als * als_info->thd_percent / 100;
+   if (delta < 3)
+   delta = 3;
+   wh = (als + delta > 0x) ? 0x : (als + delta);
+   wl = (als < delta) ? 0 : (als - delta);
+
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_ALS_WH, wh);
+   if (ret < 0)
+   return ret;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WH] = wh;
+
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_ALS_WL, wl);
+   if (ret < 0)
+   return ret;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WL] = wl;
+   ret = als;
+   }
+
+   return ret;
+}
+
+/**
  * cm32181_get_lux() - report current lux value
  * @chip:  pointer of struct c

[PATCH 3/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Added Power Management support.
- Added driver remove routine.

Signed-off-by: Kevin Tsai 
---
 drivers/iio/light/cm32181.c | 44 
 1 file changed, 44 insertions(+)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 6b11145..9bc3e1f 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -370,11 +370,51 @@ static int cm32181_probe(struct i2c_client *client,
return 0;
 }
 
+static int cm32181_remove(struct i2c_client *client)
+{
+   i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   CM32181_CMD_ALS_DISABLE);
+
+   return 0;
+}
+
 static const struct i2c_device_id cm32181_id[] = {
{ "cm32181", 0 },
{ }
 };
 
+#ifdef CONFIG_PM_SLEEP
+static int cm32181_suspend(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm32181_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip->client;
+   int ret;
+
+   ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   CM32181_CMD_ALS_DISABLE);
+
+   return ret;
+}
+
+static int cm32181_resume(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm32181_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip->client;
+   int ret;
+
+   chip->conf_regs[CM32181_REG_ADDR_CMD] &= ~CM32181_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   chip->conf_regs[CM32181_REG_ADDR_CMD]);
+
+   return ret;
+}
+
+static const struct dev_pm_ops cm32181_pm_ops = {
+   SET_SYSTEM_SLEEP_PM_OPS(cm32181_suspend, cm32181_resume)};
+#endif
+
 MODULE_DEVICE_TABLE(i2c, cm32181_id);
 
 static const struct of_device_id cm32181_of_match[] = {
@@ -387,9 +427,13 @@ static struct i2c_driver cm32181_driver = {
.name   = "cm32181",
.of_match_table = of_match_ptr(cm32181_of_match),
.owner  = THIS_MODULE,
+#ifdef CONFIG_PM_SLEEP
+   .pm = _pm_ops,
+#endif
},
.id_table   = cm32181_id,
.probe  = cm32181_probe,
+   .remove = cm32181_remove,
 };
 
 module_i2c_driver(cm32181_driver);
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 5/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Added Device Tree support.

Signed-off-by: Kevin Tsai 
---
 .../devicetree/bindings/iio/light/cm32181.txt  | 33 +++
 MAINTAINERS| 12 ++--
 drivers/iio/light/cm32181.c| 66 ++
 3 files changed, 70 insertions(+), 41 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm32181.txt

diff --git a/Documentation/devicetree/bindings/iio/light/cm32181.txt 
b/Documentation/devicetree/bindings/iio/light/cm32181.txt
new file mode 100644
index 000..b81a3e3
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm32181.txt
@@ -0,0 +1,33 @@
+* Vishay Capella CM32181 Ambient Light sensor
+
+Required properties:
+- compatible: must be "capella,cm32181"
+- reg: the I2C address of the device
+
+Optional properties:
+- capella,hw_id: hardware device id.
+- capella,reg_cmd: command register initialization.
+- capella,reg_als_wh: high threshold register initialization.
+- capella,reg_als_wl: low threshold register initialization.
+- capella,calibscale: calibrated factor with 10^-5 notation.
+- capella,hw_id: hardware device id.
+- capella,mlux_per_bit: millilux per bit under the default IT.
+- capella,thd_percent: threshold percentage change to trigger.
+
+Example:
+
+cm32181@10 {
+   compatible = "capella,cm32181";
+   reg = <0x10>;
+   interrupt-parent = <>;
+   interrupts = <17 0>;
+
+   capella,hw_id = <0x81>;
+   capella,reg_cmd = <0x04>;
+   capella,reg_als_wh = <0x>;
+   capella,reg_als_wl = <0x>;
+   capella,calibscale = <1>;
+   capella,mlux_per_bit = <5>;
+   capella,thd_percent = <5>;
+};
+
diff --git a/MAINTAINERS b/MAINTAINERS
index f8e0afb..ee6a8f6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2431,12 +2431,6 @@ F:   security/capability.c
 F: security/commoncap.c
 F:     kernel/capability.c
 
-CAPELLA MICROSYSTEMS LIGHT SENSOR DRIVER
-M: Kevin Tsai 
-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 
 L: linux-w...@vger.kernel.org
@@ -10610,6 +10604,12 @@ L: net...@vger.kernel.org
 S: Maintained
 F: drivers/net/ethernet/via/via-velocity.*
 
+VISHAY CAPELLA LIGHT SENSOR DRIVER
+M: Kevin Tsai 
+S: Maintained
+F: drivers/iio/light/cm*
+F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+
 VIVID VIRTUAL VIDEO DRIVER
 M: Hans Verkuil 
 L: linux-me...@vger.kernel.org
diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 54bf0cb..b7abd46 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -51,6 +51,7 @@
 #define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
+#define CM32181_THD_PERCENT5   /* 0 for polling mode */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define CM32181_MLUX_PER_LUX   1000
@@ -80,6 +81,7 @@ struct cm32181_als_info {
int calibscale;
int mlux_per_bit;
int mlux_per_bit_base_it;
+   int thd_percent;
 };
 
 static struct cm32181_als_info cm32181_als_info_default = {
@@ -90,6 +92,7 @@ static struct cm32181_als_info cm32181_als_info_default = {
.calibscale = CM32181_CALIBSCALE_DEFAULT,
.mlux_per_bit = CM32181_MLUX_PER_BIT,
.mlux_per_bit_base_it = CM32181_MLUX_PER_BIT_BASE_IT,
+   .thd_percent = CM32181_THD_PERCENT,
 };
 
 struct cm32181_chip {
@@ -99,6 +102,30 @@ struct cm32181_chip {
u16 conf_regs[CM32181_CONF_REG_NUM];
 };
 
+#ifdef CONFIG_OF
+void cm32181_parse_dt(struct cm32181_chip *chip)
+{
+   struct device_node *dn = chip->client->dev.of_node;
+   struct cm32181_als_info *als_info = chip->als_info;
+   u32 temp_val;
+
+   if (!of_property_read_u32(dn, "capella,hw_id", _val))
+   als_info->hw_id = (uint8_t)temp_val;
+   if (!of_property_read_u32(dn, "capella,reg_cmd", _val))
+   als_info->reg_cmd = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, "capella,reg_als_wh", _val))
+   als_info->reg_als_wh = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, "capella,reg_als_wl", _val))
+   als_info->reg_als_wl = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, "capella,calibscale", _val))
+   als_info->calibscale = (int)temp_val;
+   if (!of_property_read_u32(dn, "capella,mlux_per_bit", _val))
+   als_info->mlux_per_bit = (int)temp_val;
+   if (!of_property_read_u32(dn, "capella,thd_percent", _val))
+ 

[PATCH 2/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Added cm32181_als_info structure.

Signed-off-by: Kevin Tsai 
---
 drivers/iio/light/cm32181.c | 42 +-
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 0491d73..6b11145 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -48,6 +48,7 @@
 #define CM32181_ALS_WL_DEFAULT 0x
 
 /* Software parameters */
+#define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
@@ -58,11 +59,31 @@ static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
 
+struct cm32181_als_info {
+   u8 hw_id;
+   u16 reg_cmd;
+   u16 reg_als_wh;
+   u16 reg_als_wl;
+   int calibscale;
+   int mlux_per_bit;
+   int mlux_per_bit_base_it;
+};
+
+static struct cm32181_als_info cm32181_als_info_default = {
+   .hw_id = CM32181_HW_ID,
+   .reg_cmd = CM32181_CMD_DEFAULT,
+   .reg_als_wh = CM32181_ALS_WH_DEFAULT,
+   .reg_als_wl = CM32181_ALS_WL_DEFAULT,
+   .calibscale = CM32181_CALIBSCALE_DEFAULT,
+   .mlux_per_bit = CM32181_MLUX_PER_BIT,
+   .mlux_per_bit_base_it = CM32181_MLUX_PER_BIT_BASE_IT,
+};
+
 struct cm32181_chip {
struct i2c_client *client;
struct mutex lock;
+   struct cm32181_als_info *als_info;
u16 conf_regs[CM32181_CONF_REG_NUM];
-   int calibscale;
 };
 
 /**
@@ -76,22 +97,25 @@ struct cm32181_chip {
 static int cm32181_reg_init(struct cm32181_chip *chip)
 {
struct i2c_client *client = chip->client;
+   struct cm32181_als_info *als_info;
int i;
s32 ret;
 
+   chip->als_info = _als_info_default;
+   als_info = chip->als_info;
+
ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
if (ret < 0)
return ret;
 
/* check device ID */
-   if ((ret & 0xFF) != 0x81)
+   if ((ret & 0xFF) != als_info->hw_id)
return -ENODEV;
 
/* Default Values */
-   chip->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_DEFAULT;
-   chip->conf_regs[CM32181_REG_ADDR_ALS_WH] = CM32181_ALS_WH_DEFAULT;
-   chip->conf_regs[CM32181_REG_ADDR_ALS_WL] = CM32181_ALS_WL_DEFAULT;
-   chip->calibscale = CM32181_CALIBSCALE_DEFAULT;
+   chip->conf_regs[CM32181_REG_ADDR_CMD] = als_info->reg_cmd;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WH] = als_info->reg_als_wh;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WL] = als_info->reg_als_wl;
 
/* Initialize registers*/
for (i = 0; i < CM32181_CONF_REG_NUM; i++) {
@@ -197,7 +221,7 @@ static int cm32181_get_lux(struct cm32181_chip *chip)
return ret;
 
lux *= ret;
-   lux *= chip->calibscale;
+   lux *= chip->als_info->calibscale;
lux /= CM32181_CALIBSCALE_RESOLUTION;
lux /= MLUX_PER_LUX;
 
@@ -222,7 +246,7 @@ static int cm32181_read_raw(struct iio_dev *indio_dev,
*val = ret;
return IIO_VAL_INT;
case IIO_CHAN_INFO_CALIBSCALE:
-   *val = chip->calibscale;
+   *val = chip->als_info->calibscale;
return IIO_VAL_INT;
case IIO_CHAN_INFO_INT_TIME:
*val = 0;
@@ -242,7 +266,7 @@ static int cm32181_write_raw(struct iio_dev *indio_dev,
 
switch (mask) {
case IIO_CHAN_INFO_CALIBSCALE:
-   chip->calibscale = val;
+   chip->als_info->calibscale = val;
return val;
case IIO_CHAN_INFO_INT_TIME:
ret = cm32181_write_als_it(chip, val2);
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


RE: [PATCH 1/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Renamed company name. - Removed cm32181_reg. - Removed white space. - Removed unused include files. - Updated

2015-05-21 Thread Kevin Tsai
Sorry for the mistake.  Let me resubmit with correct subject.

Thanks.

Kevin Tsai
05/21/15

-Original Message-
From: Greg KH [mailto:gre...@linuxfoundation.org] 
Sent: Thursday, May 21, 2015 6:56 PM
To: Kevin Tsai
Cc: Rob Herring; Pawel Moll; Mark Rutland; Ian Campbell; Kumar Gala;
Jonathan Cameron; Hartmut Knaack; Lars-Peter Clausen; Peter Meerwald; Andrew
Morton; David S. Miller; Mauro Carvalho Chehab; Arnd Bergmann; Joe Perches;
Jingoo Han; Daniel Baluta; Roberta Dobrescu; Kuppuswamy Sathyanarayanan;
devicet...@vger.kernel.org; linux-kernel@vger.kernel.org;
linux-...@vger.kernel.org
Subject: Re: [PATCH 1/6] iio: light: Updated Vishay Capella cm32181 ambient
light sensor driver. - Renamed company name. - Removed cm32181_reg. -
Removed white space. - Removed unused include files. - Updated macro
definitions. - Renamed cm32181_chip pointer to ch

On Thu, May 21, 2015 at 05:30:40PM -0700, Kevin Tsai wrote:
> Signed-off-by: Kevin Tsai 
> ---

Your whole changelog ended up in the Subject line :(

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 1/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Renamed company name. - Removed cm32181_reg. - Removed white space. - Removed unused include files. - Updated macr

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai 
---
Renamed company name.
Removed cm32181_reg.
Removed white space.
Removed unused include files.
Updated macro definitions.
Renamed cm32181_chip pointer to chip.

 drivers/iio/light/Kconfig   |   4 +-
 drivers/iio/light/cm32181.c | 126 +++-
 2 files changed, 67 insertions(+), 63 deletions(-)

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index a437bad..583aa6a 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -39,11 +39,11 @@ config APDS9300
 
 config CM32181
depends on I2C
-   tristate "CM32181 driver"
+   tristate "Vishay Capella CM32181 driver"
help
 Say Y here if you use cm32181.
 This option enables ambient light sensor using
-Capella cm32181 device driver.
+Vishay Capella cm32181 device driver.
 
 To compile this driver as a module, choose M here:
 the module will be called cm32181.
diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 5d12ae54..0491d73 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -1,19 +1,15 @@
 /*
- * Copyright (C) 2013 Capella Microsystems Inc.
- * Author: Kevin Tsai 
+ * Copyright (C) 2013-2015 Vishay Capella
  *
  * 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 
-#include 
 #include 
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -21,36 +17,43 @@
 
 /* Registers Address */
 #define CM32181_REG_ADDR_CMD   0x00
+#define CM32181_REG_ADDR_ALS_WH0x01
+#define CM32181_REG_ADDR_ALS_WL0x02
 #define CM32181_REG_ADDR_ALS   0x04
 #define CM32181_REG_ADDR_STATUS0x06
 #define CM32181_REG_ADDR_ID0x07
 
 /* Number of Configurable Registers */
-#define CM32181_CONF_REG_NUM   0x01
+#define CM32181_CONF_REG_NUM   3
 
 /* CMD register */
-#define CM32181_CMD_ALS_ENABLE 0x00
-#define CM32181_CMD_ALS_DISABLE0x01
-#define CM32181_CMD_ALS_INT_EN 0x02
+#define CM32181_CMD_ALS_DISABLEBIT(0)
+#define CM32181_CMD_ALS_INT_EN BIT(1)
 
 #define CM32181_CMD_ALS_IT_SHIFT   6
-#define CM32181_CMD_ALS_IT_MASK(0x0F << 
CM32181_CMD_ALS_IT_SHIFT)
+#define CM32181_CMD_ALS_IT_MASK(BIT(6) | BIT(7) | BIT(8) | 
BIT(9))
 #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT)
 
 #define CM32181_CMD_ALS_SM_SHIFT   11
-#define CM32181_CMD_ALS_SM_MASK(0x03 << 
CM32181_CMD_ALS_SM_SHIFT)
+#define CM32181_CMD_ALS_SM_MASK(BIT(11) | BIT(12))
 #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT)
 
+#define CM32181_CMD_DEFAULT(CM32181_CMD_ALS_IT_DEFAULT | \
+   CM32181_CMD_ALS_SM_DEFAULT)
+
+/* ALS_WH register */
+#define CM32181_ALS_WH_DEFAULT 0x
+
+/* ALS_WL register */
+#define CM32181_ALS_WL_DEFAULT 0x
+
+/* Software parameters */
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define MLUX_PER_LUX   1000
 
-static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
-   CM32181_REG_ADDR_CMD,
-};
-
 static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
@@ -64,15 +67,15 @@ struct cm32181_chip {
 
 /**
  * cm32181_reg_init() - Initialize CM32181 registers
- * @cm32181:   pointer of struct cm32181.
+ * @chip:  pointer of struct cm32181_chip
  *
  * Initialize CM32181 ambient light sensor register to default values.
  *
  * Return: 0 for success; otherwise for error code.
  */
-static int cm32181_reg_init(struct cm32181_chip *cm32181)
+static int cm32181_reg_init(struct cm32181_chip *chip)
 {
-   struct i2c_client *client = cm32181->client;
+   struct i2c_client *client = chip->client;
int i;
s32 ret;
 
@@ -85,14 +88,15 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
return -ENODEV;
 
/* Default Values */
-   cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE |
-   CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
-   cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
+   chip->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_DEFAULT;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WH] = CM32181_ALS_WH_DEFAULT;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WL] = CM32181_ALS_WL_DEFAULT;
+   chip->calibs

[PATCH 6/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Added Interrupt support.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai 
---
Added Interrupt support.

 drivers/iio/light/cm32181.c | 156 +++-
 1 file changed, 153 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index b7abd46..1ae32a0 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -47,6 +47,10 @@
 /* ALS_WL register */
 #define CM32181_ALS_WL_DEFAULT 0x
 
+/* STATUS register */
+#define CM32181_STATUS_ALS_IF_LBIT(15)
+#define CM32181_STATUS_ALS_IF_HBIT(14)
+
 /* Software parameters */
 #define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
@@ -122,7 +126,8 @@ void cm32181_parse_dt(struct cm32181_chip *chip)
if (!of_property_read_u32(dn, "capella,mlux_per_bit", _val))
als_info->mlux_per_bit = (int)temp_val;
if (!of_property_read_u32(dn, "capella,thd_percent", _val))
-   als_info->thd_percent = (int)temp_val;
+   if (((int)temp_val >= 0) && ((int)temp_val < 100))
+   als_info->thd_percent = (int)temp_val;
 }
 #endif
 
@@ -173,6 +178,63 @@ static int cm32181_reg_init(struct cm32181_chip *chip)
 }
 
 /**
+ * cm32181_interrupt() - enable/disable interrupt
+ * @chip:   pointer of struct cm32181_chip
+ * @ int_en:   truen for enable; false for disable
+ *
+ * Enable/disable interrupt mode
+ *
+ * Return: 0 for success; otherwise for error code.
+ */
+static int cm32181_interrupt(struct cm32181_chip *cm32181, bool int_en)
+{
+   int ret;
+
+   mutex_lock(>lock);
+   if (int_en)
+   cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
+   CM32181_CMD_ALS_INT_EN;
+   else
+   cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
+   ~CM32181_CMD_ALS_INT_EN;
+
+   ret = i2c_smbus_write_word_data(cm32181->client,
+   CM32181_REG_ADDR_CMD,
+   cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
+   mutex_unlock(>lock);
+   return ret;
+}
+
+static irqreturn_t cm32181_irq_handler(int irq, void *data)
+{
+   struct iio_dev *indio_dev = data;
+   struct cm32181_chip *cm32181 = iio_priv(indio_dev);
+   struct i2c_client *client = cm32181->client;
+   int ret;
+   u64 ev_code;
+
+   ret = i2c_smbus_read_word_data(cm32181->client,
+   CM32181_REG_ADDR_STATUS);
+   if (ret < 0) {
+   dev_err(>dev,
+   "%s: Data read failed: %d\n", __func__, ret);
+   return IRQ_NONE;
+   }
+
+   if (!(ret & (CM32181_STATUS_ALS_IF_H | CM32181_STATUS_ALS_IF_L)))
+   return IRQ_NONE;
+
+   ev_code = IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
+   0,
+   IIO_EV_TYPE_THRESH,
+   IIO_EV_DIR_EITHER);
+
+   iio_push_event(indio_dev, ev_code, iio_get_time_ns());
+
+   return IRQ_HANDLED;
+}
+
+/**
  * cm32181_read_als_it() - Get sensor integration time
  * @chip:  pointer of struct cm32181_chip
  * @val:   pointer of int to load the integration (sec)
@@ -242,6 +304,51 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val, int val2)
 }
 
 /**
+ * cm32181_ials_read() - read ALS raw data
+ * @chip:   pointer of struct cm32181_chip
+ *
+ * Read the ALS raw data and update the interrupt threshold windows.
+ *
+ * Return: Positive value is ALS raw data, otherwise is error code.
+ */
+static int cm32181_als_read(struct cm32181_chip *chip)
+{
+   struct i2c_client *client = chip->client;
+   struct cm32181_als_info *als_info = chip->als_info;
+   u16 als, wh, wl, delta;
+   int ret;
+
+   ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
+   if (ret < 0)
+   return ret;
+
+   als = (u16)ret;
+
+   if (als_info->thd_percent) {
+   delta = als * als_info->thd_percent / 100;
+   if (delta < 3)
+   delta = 3;
+   wh = (als + delta > 0x) ? 0x : (als + delta);
+   wl = (als < delta) ? 0 : (als - delta);
+
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_ALS_WH, wh);
+   if (ret < 0)
+   return ret;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WH] = wh;
+
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_ALS_WL, wl);
+   if (ret < 0)
+   return ret;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WL] = wl;
+   ret = als;
+   }
+
+   return ret;
+}
+
+/**
  * cm32181_get_lux() - report current lux value
  * @chip:  pointer of struct c

[PATCH 3/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Added Power Management support. - Added driver remove routine.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai 
---
Added Power Management support.
Added remove routine.

 drivers/iio/light/cm32181.c | 44 
 1 file changed, 44 insertions(+)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 6b11145..9bc3e1f 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -370,11 +370,51 @@ static int cm32181_probe(struct i2c_client *client,
return 0;
 }
 
+static int cm32181_remove(struct i2c_client *client)
+{
+   i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   CM32181_CMD_ALS_DISABLE);
+
+   return 0;
+}
+
 static const struct i2c_device_id cm32181_id[] = {
{ "cm32181", 0 },
{ }
 };
 
+#ifdef CONFIG_PM_SLEEP
+static int cm32181_suspend(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm32181_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip->client;
+   int ret;
+
+   ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   CM32181_CMD_ALS_DISABLE);
+
+   return ret;
+}
+
+static int cm32181_resume(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm32181_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip->client;
+   int ret;
+
+   chip->conf_regs[CM32181_REG_ADDR_CMD] &= ~CM32181_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   chip->conf_regs[CM32181_REG_ADDR_CMD]);
+
+   return ret;
+}
+
+static const struct dev_pm_ops cm32181_pm_ops = {
+   SET_SYSTEM_SLEEP_PM_OPS(cm32181_suspend, cm32181_resume)};
+#endif
+
 MODULE_DEVICE_TABLE(i2c, cm32181_id);
 
 static const struct of_device_id cm32181_of_match[] = {
@@ -387,9 +427,13 @@ static struct i2c_driver cm32181_driver = {
.name   = "cm32181",
.of_match_table = of_match_ptr(cm32181_of_match),
.owner  = THIS_MODULE,
+#ifdef CONFIG_PM_SLEEP
+   .pm = _pm_ops,
+#endif
},
.id_table   = cm32181_id,
.probe  = cm32181_probe,
+   .remove = cm32181_remove,
 };
 
 module_i2c_driver(cm32181_driver);
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 4/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Replaced als_it_bits and als_it_value by cm32181_als_it_scale.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai 
---
Replaced als_it_bits and als_it_value by cm32181_als_it_scale.

 drivers/iio/light/cm32181.c | 95 -
 1 file changed, 76 insertions(+), 19 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 9bc3e1f..54bf0cb 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -53,12 +53,25 @@
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
-#define MLUX_PER_LUX   1000
+#define CM32181_MLUX_PER_LUX   1000
 
 static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
 
+static const struct {
+   int val;
+   int val2;
+   u16 it;
+} cm32181_als_it_scales[] = {
+   {0, 25000, 12}, /* 0.025000 */
+   {0, 5, 8},  /* 0.05 */
+   {0, 10, 0}, /* 0.10 */
+   {0, 20, 1}, /* 0.20 */
+   {0, 40, 2}, /* 0.40 */
+   {0, 80, 3}, /* 0.80 */
+};
+
 struct cm32181_als_info {
u8 hw_id;
u16 reg_cmd;
@@ -129,15 +142,16 @@ static int cm32181_reg_init(struct cm32181_chip *chip)
 }
 
 /**
- * cm32181_read_als_it() - Get sensor integration time (ms)
+ * cm32181_read_als_it() - Get sensor integration time
  * @chip:  pointer of struct cm32181_chip
- * @val2:  pointer of int to load the als_it value.
+ * @val:   pointer of int to load the integration (sec)
+ * @val2:  pointer of int to load the integration time (microsecond)
  *
  * Report the current integartion time by millisecond.
  *
  * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
  */
-static int cm32181_read_als_it(struct cm32181_chip *chip, int *val2)
+static int cm32181_read_als_it(struct cm32181_chip *chip, int *val, int *val2)
 {
u16 als_it;
int i;
@@ -145,9 +159,10 @@ static int cm32181_read_als_it(struct cm32181_chip *chip, 
int *val2)
als_it = chip->conf_regs[CM32181_REG_ADDR_CMD];
als_it &= CM32181_CMD_ALS_IT_MASK;
als_it >>= CM32181_CMD_ALS_IT_SHIFT;
-   for (i = 0; i < ARRAY_SIZE(als_it_bits); i++) {
-   if (als_it == als_it_bits[i]) {
-   *val2 = als_it_value[i];
+   for (i = 0; i < ARRAY_SIZE(cm32181_als_it_scales); i++) {
+   if (als_it == cm32181_als_it_scales[i].it) {
+   *val = cm32181_als_it_scales[i].val;
+   *val2 = cm32181_als_it_scales[i].val2;
return IIO_VAL_INT_PLUS_MICRO;
}
}
@@ -158,15 +173,44 @@ static int cm32181_read_als_it(struct cm32181_chip *chip, 
int *val2)
 /**
  * cm32181_write_als_it() - Write sensor integration time
  * @chip:  pointer of struct cm32181_chip.
- * @val:   integration time by millisecond.
+ * @val:   integration time in second.
+ * @val2:  integration time in microsecond.
  *
  * Convert integration time (ms) to sensor value.
  *
  * Return: i2c_smbus_write_word_data command return value.
  */
-static int cm32181_write_als_it(struct cm32181_chip *chip, int val)
+static int cm32181_write_als_it(struct cm32181_chip *chip, int val, int val2)
 {
struct i2c_client *client = chip->client;
+   u16 als_it, cmd;
+   int i;
+   s32 ret;
+
+   for (i = 0; i < ARRAY_SIZE(cm32181_als_it_scales); i++) {
+   if (val == cm32181_als_it_scales[i].val &&
+   val2 == cm32181_als_it_scales[i].val2) {
+
+   als_it = cm32181_als_it_scales[i].it;
+   als_it <<= CM32181_CMD_ALS_IT_SHIFT;
+
+   cmd = chip->conf_regs[CM32181_REG_ADDR_CMD];
+   cmd &= ~CM32181_CMD_ALS_IT_MASK;
+   cmd |= als_it;
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_CMD,
+   cmd);
+   if (ret < 0)
+   return ret;
+
+   chip->conf_regs[CM32181_REG_ADDR_CMD] = cmd;
+   return 0;
+   }
+   }
+   return -EINVAL;
+
+/*
+   struct i2c_client *client = chip->client;
u16 als_it;
int ret, i, n;
 
@@ -190,6 +234,7 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val)
mutex_unlock(>lock);
 
return ret;
+*/
 }
 
 /**
@@ -204,26 +249,29 @@ static int cm32181_write_als_it(struct cm32181_chip 
*chip, int val)
 static int cm32181_get_lux(struct cm32181_chip *chip)
 {
struct i2c_client *client = chip->client;
+   struct cm32181_als_info *als_info = chip->als_info;
int ret;
+   int val, val2;
int a

[PATCH 5/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Added Device Tree support.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai 
---
Added Device Tree support.

 .../devicetree/bindings/iio/light/cm32181.txt  | 33 +++
 MAINTAINERS| 13 +++--
 drivers/iio/light/cm32181.c| 66 ++
 3 files changed, 71 insertions(+), 41 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm32181.txt

diff --git a/Documentation/devicetree/bindings/iio/light/cm32181.txt 
b/Documentation/devicetree/bindings/iio/light/cm32181.txt
new file mode 100644
index 000..b81a3e3
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm32181.txt
@@ -0,0 +1,33 @@
+* Vishay Capella CM32181 Ambient Light sensor
+
+Required properties:
+- compatible: must be "capella,cm32181"
+- reg: the I2C address of the device
+
+Optional properties:
+- capella,hw_id: hardware device id.
+- capella,reg_cmd: command register initialization.
+- capella,reg_als_wh: high threshold register initialization.
+- capella,reg_als_wl: low threshold register initialization.
+- capella,calibscale: calibrated factor with 10^-5 notation.
+- capella,hw_id: hardware device id.
+- capella,mlux_per_bit: millilux per bit under the default IT.
+- capella,thd_percent: threshold percentage change to trigger.
+
+Example:
+
+cm32181@10 {
+   compatible = "capella,cm32181";
+   reg = <0x10>;
+   interrupt-parent = <>;
+   interrupts = <17 0>;
+
+   capella,hw_id = <0x81>;
+   capella,reg_cmd = <0x04>;
+   capella,reg_als_wh = <0x>;
+   capella,reg_als_wl = <0x>;
+   capella,calibscale = <1>;
+   capella,mlux_per_bit = <5>;
+   capella,thd_percent = <5>;
+};
+
diff --git a/MAINTAINERS b/MAINTAINERS
index f8e0afb..3af7eda 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2431,12 +2431,6 @@ F:   security/capability.c
 F: security/commoncap.c
 F:     kernel/capability.c
 
-CAPELLA MICROSYSTEMS LIGHT SENSOR DRIVER
-M: Kevin Tsai 
-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 
 L: linux-w...@vger.kernel.org
@@ -10610,6 +10604,13 @@ L: net...@vger.kernel.org
 S: Maintained
 F: drivers/net/ethernet/via/via-velocity.*
 
+VISHAY CAPELLA LIGHT SENSOR DRIVER
+M: Kevin Tsai 
+S: Maintained
+F: drivers/iio/light/cm*
+F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+F: Documentation/devicetree/bindings/iio/light/cm*
+
 VIVID VIRTUAL VIDEO DRIVER
 M: Hans Verkuil 
 L: linux-me...@vger.kernel.org
diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 54bf0cb..b7abd46 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -51,6 +51,7 @@
 #define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
+#define CM32181_THD_PERCENT5   /* 0 for polling mode */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define CM32181_MLUX_PER_LUX   1000
@@ -80,6 +81,7 @@ struct cm32181_als_info {
int calibscale;
int mlux_per_bit;
int mlux_per_bit_base_it;
+   int thd_percent;
 };
 
 static struct cm32181_als_info cm32181_als_info_default = {
@@ -90,6 +92,7 @@ static struct cm32181_als_info cm32181_als_info_default = {
.calibscale = CM32181_CALIBSCALE_DEFAULT,
.mlux_per_bit = CM32181_MLUX_PER_BIT,
.mlux_per_bit_base_it = CM32181_MLUX_PER_BIT_BASE_IT,
+   .thd_percent = CM32181_THD_PERCENT,
 };
 
 struct cm32181_chip {
@@ -99,6 +102,30 @@ struct cm32181_chip {
u16 conf_regs[CM32181_CONF_REG_NUM];
 };
 
+#ifdef CONFIG_OF
+void cm32181_parse_dt(struct cm32181_chip *chip)
+{
+   struct device_node *dn = chip->client->dev.of_node;
+   struct cm32181_als_info *als_info = chip->als_info;
+   u32 temp_val;
+
+   if (!of_property_read_u32(dn, "capella,hw_id", _val))
+   als_info->hw_id = (uint8_t)temp_val;
+   if (!of_property_read_u32(dn, "capella,reg_cmd", _val))
+   als_info->reg_cmd = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, "capella,reg_als_wh", _val))
+   als_info->reg_als_wh = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, "capella,reg_als_wl", _val))
+   als_info->reg_als_wl = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, "capella,calibscale", _val))
+   als_info->calibscale = (int)temp_val;
+   if (!of_property_read_u32(dn, "capella,mlux_per_bit", _val))
+   als_info->mlux_per_bit = (int)temp_val;
+   if (!of_property_read_u32(dn,

[PATCH 2/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Added cm32181_als_info structure.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai 
---
Added cm32181_als_info structure.

 drivers/iio/light/cm32181.c | 42 +-
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 0491d73..6b11145 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -48,6 +48,7 @@
 #define CM32181_ALS_WL_DEFAULT 0x
 
 /* Software parameters */
+#define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
@@ -58,11 +59,31 @@ static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
 
+struct cm32181_als_info {
+   u8 hw_id;
+   u16 reg_cmd;
+   u16 reg_als_wh;
+   u16 reg_als_wl;
+   int calibscale;
+   int mlux_per_bit;
+   int mlux_per_bit_base_it;
+};
+
+static struct cm32181_als_info cm32181_als_info_default = {
+   .hw_id = CM32181_HW_ID,
+   .reg_cmd = CM32181_CMD_DEFAULT,
+   .reg_als_wh = CM32181_ALS_WH_DEFAULT,
+   .reg_als_wl = CM32181_ALS_WL_DEFAULT,
+   .calibscale = CM32181_CALIBSCALE_DEFAULT,
+   .mlux_per_bit = CM32181_MLUX_PER_BIT,
+   .mlux_per_bit_base_it = CM32181_MLUX_PER_BIT_BASE_IT,
+};
+
 struct cm32181_chip {
struct i2c_client *client;
struct mutex lock;
+   struct cm32181_als_info *als_info;
u16 conf_regs[CM32181_CONF_REG_NUM];
-   int calibscale;
 };
 
 /**
@@ -76,22 +97,25 @@ struct cm32181_chip {
 static int cm32181_reg_init(struct cm32181_chip *chip)
 {
struct i2c_client *client = chip->client;
+   struct cm32181_als_info *als_info;
int i;
s32 ret;
 
+   chip->als_info = _als_info_default;
+   als_info = chip->als_info;
+
ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
if (ret < 0)
return ret;
 
/* check device ID */
-   if ((ret & 0xFF) != 0x81)
+   if ((ret & 0xFF) != als_info->hw_id)
return -ENODEV;
 
/* Default Values */
-   chip->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_DEFAULT;
-   chip->conf_regs[CM32181_REG_ADDR_ALS_WH] = CM32181_ALS_WH_DEFAULT;
-   chip->conf_regs[CM32181_REG_ADDR_ALS_WL] = CM32181_ALS_WL_DEFAULT;
-   chip->calibscale = CM32181_CALIBSCALE_DEFAULT;
+   chip->conf_regs[CM32181_REG_ADDR_CMD] = als_info->reg_cmd;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WH] = als_info->reg_als_wh;
+   chip->conf_regs[CM32181_REG_ADDR_ALS_WL] = als_info->reg_als_wl;
 
/* Initialize registers*/
for (i = 0; i < CM32181_CONF_REG_NUM; i++) {
@@ -197,7 +221,7 @@ static int cm32181_get_lux(struct cm32181_chip *chip)
return ret;
 
lux *= ret;
-   lux *= chip->calibscale;
+   lux *= chip->als_info->calibscale;
lux /= CM32181_CALIBSCALE_RESOLUTION;
lux /= MLUX_PER_LUX;
 
@@ -222,7 +246,7 @@ static int cm32181_read_raw(struct iio_dev *indio_dev,
*val = ret;
return IIO_VAL_INT;
case IIO_CHAN_INFO_CALIBSCALE:
-   *val = chip->calibscale;
+   *val = chip->als_info->calibscale;
return IIO_VAL_INT;
case IIO_CHAN_INFO_INT_TIME:
*val = 0;
@@ -242,7 +266,7 @@ static int cm32181_write_raw(struct iio_dev *indio_dev,
 
switch (mask) {
case IIO_CHAN_INFO_CALIBSCALE:
-   chip->calibscale = val;
+   chip->als_info->calibscale = val;
return val;
case IIO_CHAN_INFO_INT_TIME:
ret = cm32181_write_als_it(chip, val2);
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 1/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Renamed company name. - Removed cm32181_reg. - Removed white space. - Removed unused include files. - Updated macr

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
Renamed company name.
Removed cm32181_reg.
Removed white space.
Removed unused include files.
Updated macro definitions.
Renamed cm32181_chip pointer to chip.

 drivers/iio/light/Kconfig   |   4 +-
 drivers/iio/light/cm32181.c | 126 +++-
 2 files changed, 67 insertions(+), 63 deletions(-)

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index a437bad..583aa6a 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -39,11 +39,11 @@ config APDS9300
 
 config CM32181
depends on I2C
-   tristate CM32181 driver
+   tristate Vishay Capella CM32181 driver
help
 Say Y here if you use cm32181.
 This option enables ambient light sensor using
-Capella cm32181 device driver.
+Vishay Capella cm32181 device driver.
 
 To compile this driver as a module, choose M here:
 the module will be called cm32181.
diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 5d12ae54..0491d73 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -1,19 +1,15 @@
 /*
- * Copyright (C) 2013 Capella Microsystems Inc.
- * Author: Kevin Tsai kt...@capellamicro.com
+ * Copyright (C) 2013-2015 Vishay Capella
  *
  * 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
@@ -21,36 +17,43 @@
 
 /* Registers Address */
 #define CM32181_REG_ADDR_CMD   0x00
+#define CM32181_REG_ADDR_ALS_WH0x01
+#define CM32181_REG_ADDR_ALS_WL0x02
 #define CM32181_REG_ADDR_ALS   0x04
 #define CM32181_REG_ADDR_STATUS0x06
 #define CM32181_REG_ADDR_ID0x07
 
 /* Number of Configurable Registers */
-#define CM32181_CONF_REG_NUM   0x01
+#define CM32181_CONF_REG_NUM   3
 
 /* CMD register */
-#define CM32181_CMD_ALS_ENABLE 0x00
-#define CM32181_CMD_ALS_DISABLE0x01
-#define CM32181_CMD_ALS_INT_EN 0x02
+#define CM32181_CMD_ALS_DISABLEBIT(0)
+#define CM32181_CMD_ALS_INT_EN BIT(1)
 
 #define CM32181_CMD_ALS_IT_SHIFT   6
-#define CM32181_CMD_ALS_IT_MASK(0x0F  
CM32181_CMD_ALS_IT_SHIFT)
+#define CM32181_CMD_ALS_IT_MASK(BIT(6) | BIT(7) | BIT(8) | 
BIT(9))
 #define CM32181_CMD_ALS_IT_DEFAULT (0x00  CM32181_CMD_ALS_IT_SHIFT)
 
 #define CM32181_CMD_ALS_SM_SHIFT   11
-#define CM32181_CMD_ALS_SM_MASK(0x03  
CM32181_CMD_ALS_SM_SHIFT)
+#define CM32181_CMD_ALS_SM_MASK(BIT(11) | BIT(12))
 #define CM32181_CMD_ALS_SM_DEFAULT (0x01  CM32181_CMD_ALS_SM_SHIFT)
 
+#define CM32181_CMD_DEFAULT(CM32181_CMD_ALS_IT_DEFAULT | \
+   CM32181_CMD_ALS_SM_DEFAULT)
+
+/* ALS_WH register */
+#define CM32181_ALS_WH_DEFAULT 0x
+
+/* ALS_WL register */
+#define CM32181_ALS_WL_DEFAULT 0x
+
+/* Software parameters */
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define MLUX_PER_LUX   1000
 
-static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
-   CM32181_REG_ADDR_CMD,
-};
-
 static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
@@ -64,15 +67,15 @@ struct cm32181_chip {
 
 /**
  * cm32181_reg_init() - Initialize CM32181 registers
- * @cm32181:   pointer of struct cm32181.
+ * @chip:  pointer of struct cm32181_chip
  *
  * Initialize CM32181 ambient light sensor register to default values.
  *
  * Return: 0 for success; otherwise for error code.
  */
-static int cm32181_reg_init(struct cm32181_chip *cm32181)
+static int cm32181_reg_init(struct cm32181_chip *chip)
 {
-   struct i2c_client *client = cm32181-client;
+   struct i2c_client *client = chip-client;
int i;
s32 ret;
 
@@ -85,14 +88,15 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
return -ENODEV;
 
/* Default Values */
-   cm32181-conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE |
-   CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
-   cm32181-calibscale = CM32181_CALIBSCALE_DEFAULT;
+   chip-conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_DEFAULT;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WH

[PATCH 2/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Added cm32181_als_info structure.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
Added cm32181_als_info structure.

 drivers/iio/light/cm32181.c | 42 +-
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 0491d73..6b11145 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -48,6 +48,7 @@
 #define CM32181_ALS_WL_DEFAULT 0x
 
 /* Software parameters */
+#define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
@@ -58,11 +59,31 @@ static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
 
+struct cm32181_als_info {
+   u8 hw_id;
+   u16 reg_cmd;
+   u16 reg_als_wh;
+   u16 reg_als_wl;
+   int calibscale;
+   int mlux_per_bit;
+   int mlux_per_bit_base_it;
+};
+
+static struct cm32181_als_info cm32181_als_info_default = {
+   .hw_id = CM32181_HW_ID,
+   .reg_cmd = CM32181_CMD_DEFAULT,
+   .reg_als_wh = CM32181_ALS_WH_DEFAULT,
+   .reg_als_wl = CM32181_ALS_WL_DEFAULT,
+   .calibscale = CM32181_CALIBSCALE_DEFAULT,
+   .mlux_per_bit = CM32181_MLUX_PER_BIT,
+   .mlux_per_bit_base_it = CM32181_MLUX_PER_BIT_BASE_IT,
+};
+
 struct cm32181_chip {
struct i2c_client *client;
struct mutex lock;
+   struct cm32181_als_info *als_info;
u16 conf_regs[CM32181_CONF_REG_NUM];
-   int calibscale;
 };
 
 /**
@@ -76,22 +97,25 @@ struct cm32181_chip {
 static int cm32181_reg_init(struct cm32181_chip *chip)
 {
struct i2c_client *client = chip-client;
+   struct cm32181_als_info *als_info;
int i;
s32 ret;
 
+   chip-als_info = cm32181_als_info_default;
+   als_info = chip-als_info;
+
ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
if (ret  0)
return ret;
 
/* check device ID */
-   if ((ret  0xFF) != 0x81)
+   if ((ret  0xFF) != als_info-hw_id)
return -ENODEV;
 
/* Default Values */
-   chip-conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_DEFAULT;
-   chip-conf_regs[CM32181_REG_ADDR_ALS_WH] = CM32181_ALS_WH_DEFAULT;
-   chip-conf_regs[CM32181_REG_ADDR_ALS_WL] = CM32181_ALS_WL_DEFAULT;
-   chip-calibscale = CM32181_CALIBSCALE_DEFAULT;
+   chip-conf_regs[CM32181_REG_ADDR_CMD] = als_info-reg_cmd;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WH] = als_info-reg_als_wh;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WL] = als_info-reg_als_wl;
 
/* Initialize registers*/
for (i = 0; i  CM32181_CONF_REG_NUM; i++) {
@@ -197,7 +221,7 @@ static int cm32181_get_lux(struct cm32181_chip *chip)
return ret;
 
lux *= ret;
-   lux *= chip-calibscale;
+   lux *= chip-als_info-calibscale;
lux /= CM32181_CALIBSCALE_RESOLUTION;
lux /= MLUX_PER_LUX;
 
@@ -222,7 +246,7 @@ static int cm32181_read_raw(struct iio_dev *indio_dev,
*val = ret;
return IIO_VAL_INT;
case IIO_CHAN_INFO_CALIBSCALE:
-   *val = chip-calibscale;
+   *val = chip-als_info-calibscale;
return IIO_VAL_INT;
case IIO_CHAN_INFO_INT_TIME:
*val = 0;
@@ -242,7 +266,7 @@ static int cm32181_write_raw(struct iio_dev *indio_dev,
 
switch (mask) {
case IIO_CHAN_INFO_CALIBSCALE:
-   chip-calibscale = val;
+   chip-als_info-calibscale = val;
return val;
case IIO_CHAN_INFO_INT_TIME:
ret = cm32181_write_als_it(chip, val2);
-- 
1.8.3.1

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 4/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Replaced als_it_bits and als_it_value by cm32181_als_it_scale.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
Replaced als_it_bits and als_it_value by cm32181_als_it_scale.

 drivers/iio/light/cm32181.c | 95 -
 1 file changed, 76 insertions(+), 19 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 9bc3e1f..54bf0cb 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -53,12 +53,25 @@
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
-#define MLUX_PER_LUX   1000
+#define CM32181_MLUX_PER_LUX   1000
 
 static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
 
+static const struct {
+   int val;
+   int val2;
+   u16 it;
+} cm32181_als_it_scales[] = {
+   {0, 25000, 12}, /* 0.025000 */
+   {0, 5, 8},  /* 0.05 */
+   {0, 10, 0}, /* 0.10 */
+   {0, 20, 1}, /* 0.20 */
+   {0, 40, 2}, /* 0.40 */
+   {0, 80, 3}, /* 0.80 */
+};
+
 struct cm32181_als_info {
u8 hw_id;
u16 reg_cmd;
@@ -129,15 +142,16 @@ static int cm32181_reg_init(struct cm32181_chip *chip)
 }
 
 /**
- * cm32181_read_als_it() - Get sensor integration time (ms)
+ * cm32181_read_als_it() - Get sensor integration time
  * @chip:  pointer of struct cm32181_chip
- * @val2:  pointer of int to load the als_it value.
+ * @val:   pointer of int to load the integration (sec)
+ * @val2:  pointer of int to load the integration time (microsecond)
  *
  * Report the current integartion time by millisecond.
  *
  * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
  */
-static int cm32181_read_als_it(struct cm32181_chip *chip, int *val2)
+static int cm32181_read_als_it(struct cm32181_chip *chip, int *val, int *val2)
 {
u16 als_it;
int i;
@@ -145,9 +159,10 @@ static int cm32181_read_als_it(struct cm32181_chip *chip, 
int *val2)
als_it = chip-conf_regs[CM32181_REG_ADDR_CMD];
als_it = CM32181_CMD_ALS_IT_MASK;
als_it = CM32181_CMD_ALS_IT_SHIFT;
-   for (i = 0; i  ARRAY_SIZE(als_it_bits); i++) {
-   if (als_it == als_it_bits[i]) {
-   *val2 = als_it_value[i];
+   for (i = 0; i  ARRAY_SIZE(cm32181_als_it_scales); i++) {
+   if (als_it == cm32181_als_it_scales[i].it) {
+   *val = cm32181_als_it_scales[i].val;
+   *val2 = cm32181_als_it_scales[i].val2;
return IIO_VAL_INT_PLUS_MICRO;
}
}
@@ -158,15 +173,44 @@ static int cm32181_read_als_it(struct cm32181_chip *chip, 
int *val2)
 /**
  * cm32181_write_als_it() - Write sensor integration time
  * @chip:  pointer of struct cm32181_chip.
- * @val:   integration time by millisecond.
+ * @val:   integration time in second.
+ * @val2:  integration time in microsecond.
  *
  * Convert integration time (ms) to sensor value.
  *
  * Return: i2c_smbus_write_word_data command return value.
  */
-static int cm32181_write_als_it(struct cm32181_chip *chip, int val)
+static int cm32181_write_als_it(struct cm32181_chip *chip, int val, int val2)
 {
struct i2c_client *client = chip-client;
+   u16 als_it, cmd;
+   int i;
+   s32 ret;
+
+   for (i = 0; i  ARRAY_SIZE(cm32181_als_it_scales); i++) {
+   if (val == cm32181_als_it_scales[i].val 
+   val2 == cm32181_als_it_scales[i].val2) {
+
+   als_it = cm32181_als_it_scales[i].it;
+   als_it = CM32181_CMD_ALS_IT_SHIFT;
+
+   cmd = chip-conf_regs[CM32181_REG_ADDR_CMD];
+   cmd = ~CM32181_CMD_ALS_IT_MASK;
+   cmd |= als_it;
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_CMD,
+   cmd);
+   if (ret  0)
+   return ret;
+
+   chip-conf_regs[CM32181_REG_ADDR_CMD] = cmd;
+   return 0;
+   }
+   }
+   return -EINVAL;
+
+/*
+   struct i2c_client *client = chip-client;
u16 als_it;
int ret, i, n;
 
@@ -190,6 +234,7 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val)
mutex_unlock(chip-lock);
 
return ret;
+*/
 }
 
 /**
@@ -204,26 +249,29 @@ static int cm32181_write_als_it(struct cm32181_chip 
*chip, int val)
 static int cm32181_get_lux(struct cm32181_chip *chip)
 {
struct i2c_client *client = chip-client;
+   struct cm32181_als_info *als_info = chip-als_info;
int ret;
+   int val, val2;
int als_it;
-   unsigned long lux;
+   u64 lux

[PATCH 6/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Added Interrupt support.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
Added Interrupt support.

 drivers/iio/light/cm32181.c | 156 +++-
 1 file changed, 153 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index b7abd46..1ae32a0 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -47,6 +47,10 @@
 /* ALS_WL register */
 #define CM32181_ALS_WL_DEFAULT 0x
 
+/* STATUS register */
+#define CM32181_STATUS_ALS_IF_LBIT(15)
+#define CM32181_STATUS_ALS_IF_HBIT(14)
+
 /* Software parameters */
 #define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
@@ -122,7 +126,8 @@ void cm32181_parse_dt(struct cm32181_chip *chip)
if (!of_property_read_u32(dn, capella,mlux_per_bit, temp_val))
als_info-mlux_per_bit = (int)temp_val;
if (!of_property_read_u32(dn, capella,thd_percent, temp_val))
-   als_info-thd_percent = (int)temp_val;
+   if (((int)temp_val = 0)  ((int)temp_val  100))
+   als_info-thd_percent = (int)temp_val;
 }
 #endif
 
@@ -173,6 +178,63 @@ static int cm32181_reg_init(struct cm32181_chip *chip)
 }
 
 /**
+ * cm32181_interrupt() - enable/disable interrupt
+ * @chip:   pointer of struct cm32181_chip
+ * @ int_en:   truen for enable; false for disable
+ *
+ * Enable/disable interrupt mode
+ *
+ * Return: 0 for success; otherwise for error code.
+ */
+static int cm32181_interrupt(struct cm32181_chip *cm32181, bool int_en)
+{
+   int ret;
+
+   mutex_lock(cm32181-lock);
+   if (int_en)
+   cm32181-conf_regs[CM32181_REG_ADDR_CMD] |=
+   CM32181_CMD_ALS_INT_EN;
+   else
+   cm32181-conf_regs[CM32181_REG_ADDR_CMD] =
+   ~CM32181_CMD_ALS_INT_EN;
+
+   ret = i2c_smbus_write_word_data(cm32181-client,
+   CM32181_REG_ADDR_CMD,
+   cm32181-conf_regs[CM32181_REG_ADDR_CMD]);
+   mutex_unlock(cm32181-lock);
+   return ret;
+}
+
+static irqreturn_t cm32181_irq_handler(int irq, void *data)
+{
+   struct iio_dev *indio_dev = data;
+   struct cm32181_chip *cm32181 = iio_priv(indio_dev);
+   struct i2c_client *client = cm32181-client;
+   int ret;
+   u64 ev_code;
+
+   ret = i2c_smbus_read_word_data(cm32181-client,
+   CM32181_REG_ADDR_STATUS);
+   if (ret  0) {
+   dev_err(client-dev,
+   %s: Data read failed: %d\n, __func__, ret);
+   return IRQ_NONE;
+   }
+
+   if (!(ret  (CM32181_STATUS_ALS_IF_H | CM32181_STATUS_ALS_IF_L)))
+   return IRQ_NONE;
+
+   ev_code = IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
+   0,
+   IIO_EV_TYPE_THRESH,
+   IIO_EV_DIR_EITHER);
+
+   iio_push_event(indio_dev, ev_code, iio_get_time_ns());
+
+   return IRQ_HANDLED;
+}
+
+/**
  * cm32181_read_als_it() - Get sensor integration time
  * @chip:  pointer of struct cm32181_chip
  * @val:   pointer of int to load the integration (sec)
@@ -242,6 +304,51 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val, int val2)
 }
 
 /**
+ * cm32181_ials_read() - read ALS raw data
+ * @chip:   pointer of struct cm32181_chip
+ *
+ * Read the ALS raw data and update the interrupt threshold windows.
+ *
+ * Return: Positive value is ALS raw data, otherwise is error code.
+ */
+static int cm32181_als_read(struct cm32181_chip *chip)
+{
+   struct i2c_client *client = chip-client;
+   struct cm32181_als_info *als_info = chip-als_info;
+   u16 als, wh, wl, delta;
+   int ret;
+
+   ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
+   if (ret  0)
+   return ret;
+
+   als = (u16)ret;
+
+   if (als_info-thd_percent) {
+   delta = als * als_info-thd_percent / 100;
+   if (delta  3)
+   delta = 3;
+   wh = (als + delta  0x) ? 0x : (als + delta);
+   wl = (als  delta) ? 0 : (als - delta);
+
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_ALS_WH, wh);
+   if (ret  0)
+   return ret;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WH] = wh;
+
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_ALS_WL, wl);
+   if (ret  0)
+   return ret;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WL] = wl;
+   ret = als;
+   }
+
+   return ret;
+}
+
+/**
  * cm32181_get_lux() - report current lux value
  * @chip:  pointer of struct cm32181_chip
  *
@@ -252,7 +359,6 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val, int val2

[PATCH 5/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Added Device Tree support.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
Added Device Tree support.

 .../devicetree/bindings/iio/light/cm32181.txt  | 33 +++
 MAINTAINERS| 13 +++--
 drivers/iio/light/cm32181.c| 66 ++
 3 files changed, 71 insertions(+), 41 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm32181.txt

diff --git a/Documentation/devicetree/bindings/iio/light/cm32181.txt 
b/Documentation/devicetree/bindings/iio/light/cm32181.txt
new file mode 100644
index 000..b81a3e3
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm32181.txt
@@ -0,0 +1,33 @@
+* Vishay Capella CM32181 Ambient Light sensor
+
+Required properties:
+- compatible: must be capella,cm32181
+- reg: the I2C address of the device
+
+Optional properties:
+- capella,hw_id: hardware device id.
+- capella,reg_cmd: command register initialization.
+- capella,reg_als_wh: high threshold register initialization.
+- capella,reg_als_wl: low threshold register initialization.
+- capella,calibscale: calibrated factor with 10^-5 notation.
+- capella,hw_id: hardware device id.
+- capella,mlux_per_bit: millilux per bit under the default IT.
+- capella,thd_percent: threshold percentage change to trigger.
+
+Example:
+
+cm32181@10 {
+   compatible = capella,cm32181;
+   reg = 0x10;
+   interrupt-parent = gpio1;
+   interrupts = 17 0;
+
+   capella,hw_id = 0x81;
+   capella,reg_cmd = 0x04;
+   capella,reg_als_wh = 0x;
+   capella,reg_als_wl = 0x;
+   capella,calibscale = 1;
+   capella,mlux_per_bit = 5;
+   capella,thd_percent = 5;
+};
+
diff --git a/MAINTAINERS b/MAINTAINERS
index f8e0afb..3af7eda 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2431,12 +2431,6 @@ 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
@@ -10610,6 +10604,13 @@ L: net...@vger.kernel.org
 S: Maintained
 F: drivers/net/ethernet/via/via-velocity.*
 
+VISHAY CAPELLA LIGHT SENSOR DRIVER
+M: Kevin Tsai kt...@capellamicro.com
+S: Maintained
+F: drivers/iio/light/cm*
+F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+F: Documentation/devicetree/bindings/iio/light/cm*
+
 VIVID VIRTUAL VIDEO DRIVER
 M: Hans Verkuil hverk...@xs4all.nl
 L: linux-me...@vger.kernel.org
diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 54bf0cb..b7abd46 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -51,6 +51,7 @@
 #define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
+#define CM32181_THD_PERCENT5   /* 0 for polling mode */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define CM32181_MLUX_PER_LUX   1000
@@ -80,6 +81,7 @@ struct cm32181_als_info {
int calibscale;
int mlux_per_bit;
int mlux_per_bit_base_it;
+   int thd_percent;
 };
 
 static struct cm32181_als_info cm32181_als_info_default = {
@@ -90,6 +92,7 @@ static struct cm32181_als_info cm32181_als_info_default = {
.calibscale = CM32181_CALIBSCALE_DEFAULT,
.mlux_per_bit = CM32181_MLUX_PER_BIT,
.mlux_per_bit_base_it = CM32181_MLUX_PER_BIT_BASE_IT,
+   .thd_percent = CM32181_THD_PERCENT,
 };
 
 struct cm32181_chip {
@@ -99,6 +102,30 @@ struct cm32181_chip {
u16 conf_regs[CM32181_CONF_REG_NUM];
 };
 
+#ifdef CONFIG_OF
+void cm32181_parse_dt(struct cm32181_chip *chip)
+{
+   struct device_node *dn = chip-client-dev.of_node;
+   struct cm32181_als_info *als_info = chip-als_info;
+   u32 temp_val;
+
+   if (!of_property_read_u32(dn, capella,hw_id, temp_val))
+   als_info-hw_id = (uint8_t)temp_val;
+   if (!of_property_read_u32(dn, capella,reg_cmd, temp_val))
+   als_info-reg_cmd = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, capella,reg_als_wh, temp_val))
+   als_info-reg_als_wh = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, capella,reg_als_wl, temp_val))
+   als_info-reg_als_wl = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, capella,calibscale, temp_val))
+   als_info-calibscale = (int)temp_val;
+   if (!of_property_read_u32(dn, capella,mlux_per_bit, temp_val))
+   als_info-mlux_per_bit = (int)temp_val;
+   if (!of_property_read_u32(dn, capella,thd_percent, temp_val))
+   als_info-thd_percent = (int)temp_val

[PATCH 3/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Added Power Management support. - Added driver remove routine.

2015-05-21 Thread Kevin Tsai
Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
Added Power Management support.
Added remove routine.

 drivers/iio/light/cm32181.c | 44 
 1 file changed, 44 insertions(+)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 6b11145..9bc3e1f 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -370,11 +370,51 @@ static int cm32181_probe(struct i2c_client *client,
return 0;
 }
 
+static int cm32181_remove(struct i2c_client *client)
+{
+   i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   CM32181_CMD_ALS_DISABLE);
+
+   return 0;
+}
+
 static const struct i2c_device_id cm32181_id[] = {
{ cm32181, 0 },
{ }
 };
 
+#ifdef CONFIG_PM_SLEEP
+static int cm32181_suspend(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm32181_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip-client;
+   int ret;
+
+   ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   CM32181_CMD_ALS_DISABLE);
+
+   return ret;
+}
+
+static int cm32181_resume(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm32181_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip-client;
+   int ret;
+
+   chip-conf_regs[CM32181_REG_ADDR_CMD] = ~CM32181_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   chip-conf_regs[CM32181_REG_ADDR_CMD]);
+
+   return ret;
+}
+
+static const struct dev_pm_ops cm32181_pm_ops = {
+   SET_SYSTEM_SLEEP_PM_OPS(cm32181_suspend, cm32181_resume)};
+#endif
+
 MODULE_DEVICE_TABLE(i2c, cm32181_id);
 
 static const struct of_device_id cm32181_of_match[] = {
@@ -387,9 +427,13 @@ static struct i2c_driver cm32181_driver = {
.name   = cm32181,
.of_match_table = of_match_ptr(cm32181_of_match),
.owner  = THIS_MODULE,
+#ifdef CONFIG_PM_SLEEP
+   .pm = cm32181_pm_ops,
+#endif
},
.id_table   = cm32181_id,
.probe  = cm32181_probe,
+   .remove = cm32181_remove,
 };
 
 module_i2c_driver(cm32181_driver);
-- 
1.8.3.1

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 3/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Added Power Management support.
- Added driver remove routine.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 drivers/iio/light/cm32181.c | 44 
 1 file changed, 44 insertions(+)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 6b11145..9bc3e1f 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -370,11 +370,51 @@ static int cm32181_probe(struct i2c_client *client,
return 0;
 }
 
+static int cm32181_remove(struct i2c_client *client)
+{
+   i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   CM32181_CMD_ALS_DISABLE);
+
+   return 0;
+}
+
 static const struct i2c_device_id cm32181_id[] = {
{ cm32181, 0 },
{ }
 };
 
+#ifdef CONFIG_PM_SLEEP
+static int cm32181_suspend(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm32181_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip-client;
+   int ret;
+
+   ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   CM32181_CMD_ALS_DISABLE);
+
+   return ret;
+}
+
+static int cm32181_resume(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm32181_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip-client;
+   int ret;
+
+   chip-conf_regs[CM32181_REG_ADDR_CMD] = ~CM32181_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
+   chip-conf_regs[CM32181_REG_ADDR_CMD]);
+
+   return ret;
+}
+
+static const struct dev_pm_ops cm32181_pm_ops = {
+   SET_SYSTEM_SLEEP_PM_OPS(cm32181_suspend, cm32181_resume)};
+#endif
+
 MODULE_DEVICE_TABLE(i2c, cm32181_id);
 
 static const struct of_device_id cm32181_of_match[] = {
@@ -387,9 +427,13 @@ static struct i2c_driver cm32181_driver = {
.name   = cm32181,
.of_match_table = of_match_ptr(cm32181_of_match),
.owner  = THIS_MODULE,
+#ifdef CONFIG_PM_SLEEP
+   .pm = cm32181_pm_ops,
+#endif
},
.id_table   = cm32181_id,
.probe  = cm32181_probe,
+   .remove = cm32181_remove,
 };
 
 module_i2c_driver(cm32181_driver);
-- 
1.8.3.1

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 5/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Added Device Tree support.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 .../devicetree/bindings/iio/light/cm32181.txt  | 33 +++
 MAINTAINERS| 12 ++--
 drivers/iio/light/cm32181.c| 66 ++
 3 files changed, 70 insertions(+), 41 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm32181.txt

diff --git a/Documentation/devicetree/bindings/iio/light/cm32181.txt 
b/Documentation/devicetree/bindings/iio/light/cm32181.txt
new file mode 100644
index 000..b81a3e3
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm32181.txt
@@ -0,0 +1,33 @@
+* Vishay Capella CM32181 Ambient Light sensor
+
+Required properties:
+- compatible: must be capella,cm32181
+- reg: the I2C address of the device
+
+Optional properties:
+- capella,hw_id: hardware device id.
+- capella,reg_cmd: command register initialization.
+- capella,reg_als_wh: high threshold register initialization.
+- capella,reg_als_wl: low threshold register initialization.
+- capella,calibscale: calibrated factor with 10^-5 notation.
+- capella,hw_id: hardware device id.
+- capella,mlux_per_bit: millilux per bit under the default IT.
+- capella,thd_percent: threshold percentage change to trigger.
+
+Example:
+
+cm32181@10 {
+   compatible = capella,cm32181;
+   reg = 0x10;
+   interrupt-parent = gpio1;
+   interrupts = 17 0;
+
+   capella,hw_id = 0x81;
+   capella,reg_cmd = 0x04;
+   capella,reg_als_wh = 0x;
+   capella,reg_als_wl = 0x;
+   capella,calibscale = 1;
+   capella,mlux_per_bit = 5;
+   capella,thd_percent = 5;
+};
+
diff --git a/MAINTAINERS b/MAINTAINERS
index f8e0afb..ee6a8f6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2431,12 +2431,6 @@ 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
@@ -10610,6 +10604,12 @@ L: net...@vger.kernel.org
 S: Maintained
 F: drivers/net/ethernet/via/via-velocity.*
 
+VISHAY CAPELLA LIGHT SENSOR DRIVER
+M: Kevin Tsai kt...@capellamicro.com
+S: Maintained
+F: drivers/iio/light/cm*
+F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+
 VIVID VIRTUAL VIDEO DRIVER
 M: Hans Verkuil hverk...@xs4all.nl
 L: linux-me...@vger.kernel.org
diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 54bf0cb..b7abd46 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -51,6 +51,7 @@
 #define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
+#define CM32181_THD_PERCENT5   /* 0 for polling mode */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define CM32181_MLUX_PER_LUX   1000
@@ -80,6 +81,7 @@ struct cm32181_als_info {
int calibscale;
int mlux_per_bit;
int mlux_per_bit_base_it;
+   int thd_percent;
 };
 
 static struct cm32181_als_info cm32181_als_info_default = {
@@ -90,6 +92,7 @@ static struct cm32181_als_info cm32181_als_info_default = {
.calibscale = CM32181_CALIBSCALE_DEFAULT,
.mlux_per_bit = CM32181_MLUX_PER_BIT,
.mlux_per_bit_base_it = CM32181_MLUX_PER_BIT_BASE_IT,
+   .thd_percent = CM32181_THD_PERCENT,
 };
 
 struct cm32181_chip {
@@ -99,6 +102,30 @@ struct cm32181_chip {
u16 conf_regs[CM32181_CONF_REG_NUM];
 };
 
+#ifdef CONFIG_OF
+void cm32181_parse_dt(struct cm32181_chip *chip)
+{
+   struct device_node *dn = chip-client-dev.of_node;
+   struct cm32181_als_info *als_info = chip-als_info;
+   u32 temp_val;
+
+   if (!of_property_read_u32(dn, capella,hw_id, temp_val))
+   als_info-hw_id = (uint8_t)temp_val;
+   if (!of_property_read_u32(dn, capella,reg_cmd, temp_val))
+   als_info-reg_cmd = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, capella,reg_als_wh, temp_val))
+   als_info-reg_als_wh = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, capella,reg_als_wl, temp_val))
+   als_info-reg_als_wl = (uint16_t)temp_val;
+   if (!of_property_read_u32(dn, capella,calibscale, temp_val))
+   als_info-calibscale = (int)temp_val;
+   if (!of_property_read_u32(dn, capella,mlux_per_bit, temp_val))
+   als_info-mlux_per_bit = (int)temp_val;
+   if (!of_property_read_u32(dn, capella,thd_percent, temp_val))
+   als_info-thd_percent = (int)temp_val;
+}
+#endif
+
 /**
  * cm32181_reg_init() - Initialize

[PATCH 2/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Added cm32181_als_info structure.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 drivers/iio/light/cm32181.c | 42 +-
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 0491d73..6b11145 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -48,6 +48,7 @@
 #define CM32181_ALS_WL_DEFAULT 0x
 
 /* Software parameters */
+#define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
@@ -58,11 +59,31 @@ static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
 
+struct cm32181_als_info {
+   u8 hw_id;
+   u16 reg_cmd;
+   u16 reg_als_wh;
+   u16 reg_als_wl;
+   int calibscale;
+   int mlux_per_bit;
+   int mlux_per_bit_base_it;
+};
+
+static struct cm32181_als_info cm32181_als_info_default = {
+   .hw_id = CM32181_HW_ID,
+   .reg_cmd = CM32181_CMD_DEFAULT,
+   .reg_als_wh = CM32181_ALS_WH_DEFAULT,
+   .reg_als_wl = CM32181_ALS_WL_DEFAULT,
+   .calibscale = CM32181_CALIBSCALE_DEFAULT,
+   .mlux_per_bit = CM32181_MLUX_PER_BIT,
+   .mlux_per_bit_base_it = CM32181_MLUX_PER_BIT_BASE_IT,
+};
+
 struct cm32181_chip {
struct i2c_client *client;
struct mutex lock;
+   struct cm32181_als_info *als_info;
u16 conf_regs[CM32181_CONF_REG_NUM];
-   int calibscale;
 };
 
 /**
@@ -76,22 +97,25 @@ struct cm32181_chip {
 static int cm32181_reg_init(struct cm32181_chip *chip)
 {
struct i2c_client *client = chip-client;
+   struct cm32181_als_info *als_info;
int i;
s32 ret;
 
+   chip-als_info = cm32181_als_info_default;
+   als_info = chip-als_info;
+
ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
if (ret  0)
return ret;
 
/* check device ID */
-   if ((ret  0xFF) != 0x81)
+   if ((ret  0xFF) != als_info-hw_id)
return -ENODEV;
 
/* Default Values */
-   chip-conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_DEFAULT;
-   chip-conf_regs[CM32181_REG_ADDR_ALS_WH] = CM32181_ALS_WH_DEFAULT;
-   chip-conf_regs[CM32181_REG_ADDR_ALS_WL] = CM32181_ALS_WL_DEFAULT;
-   chip-calibscale = CM32181_CALIBSCALE_DEFAULT;
+   chip-conf_regs[CM32181_REG_ADDR_CMD] = als_info-reg_cmd;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WH] = als_info-reg_als_wh;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WL] = als_info-reg_als_wl;
 
/* Initialize registers*/
for (i = 0; i  CM32181_CONF_REG_NUM; i++) {
@@ -197,7 +221,7 @@ static int cm32181_get_lux(struct cm32181_chip *chip)
return ret;
 
lux *= ret;
-   lux *= chip-calibscale;
+   lux *= chip-als_info-calibscale;
lux /= CM32181_CALIBSCALE_RESOLUTION;
lux /= MLUX_PER_LUX;
 
@@ -222,7 +246,7 @@ static int cm32181_read_raw(struct iio_dev *indio_dev,
*val = ret;
return IIO_VAL_INT;
case IIO_CHAN_INFO_CALIBSCALE:
-   *val = chip-calibscale;
+   *val = chip-als_info-calibscale;
return IIO_VAL_INT;
case IIO_CHAN_INFO_INT_TIME:
*val = 0;
@@ -242,7 +266,7 @@ static int cm32181_write_raw(struct iio_dev *indio_dev,
 
switch (mask) {
case IIO_CHAN_INFO_CALIBSCALE:
-   chip-calibscale = val;
+   chip-als_info-calibscale = val;
return val;
case IIO_CHAN_INFO_INT_TIME:
ret = cm32181_write_als_it(chip, val2);
-- 
1.8.3.1

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


RE: [PATCH 1/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver. - Renamed company name. - Removed cm32181_reg. - Removed white space. - Removed unused include files. - Updated

2015-05-21 Thread Kevin Tsai
Sorry for the mistake.  Let me resubmit with correct subject.

Thanks.

Kevin Tsai
05/21/15

-Original Message-
From: Greg KH [mailto:gre...@linuxfoundation.org] 
Sent: Thursday, May 21, 2015 6:56 PM
To: Kevin Tsai
Cc: Rob Herring; Pawel Moll; Mark Rutland; Ian Campbell; Kumar Gala;
Jonathan Cameron; Hartmut Knaack; Lars-Peter Clausen; Peter Meerwald; Andrew
Morton; David S. Miller; Mauro Carvalho Chehab; Arnd Bergmann; Joe Perches;
Jingoo Han; Daniel Baluta; Roberta Dobrescu; Kuppuswamy Sathyanarayanan;
devicet...@vger.kernel.org; linux-kernel@vger.kernel.org;
linux-...@vger.kernel.org
Subject: Re: [PATCH 1/6] iio: light: Updated Vishay Capella cm32181 ambient
light sensor driver. - Renamed company name. - Removed cm32181_reg. -
Removed white space. - Removed unused include files. - Updated macro
definitions. - Renamed cm32181_chip pointer to ch

On Thu, May 21, 2015 at 05:30:40PM -0700, Kevin Tsai wrote:
 Signed-off-by: Kevin Tsai kt...@capellamicro.com
 ---

Your whole changelog ended up in the Subject line :(

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 4/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Replaced als_it_bits and als_it_value by cm32181_als_it_scale.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 drivers/iio/light/cm32181.c | 95 -
 1 file changed, 76 insertions(+), 19 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 9bc3e1f..54bf0cb 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -53,12 +53,25 @@
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
-#define MLUX_PER_LUX   1000
+#define CM32181_MLUX_PER_LUX   1000
 
 static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
 
+static const struct {
+   int val;
+   int val2;
+   u16 it;
+} cm32181_als_it_scales[] = {
+   {0, 25000, 12}, /* 0.025000 */
+   {0, 5, 8},  /* 0.05 */
+   {0, 10, 0}, /* 0.10 */
+   {0, 20, 1}, /* 0.20 */
+   {0, 40, 2}, /* 0.40 */
+   {0, 80, 3}, /* 0.80 */
+};
+
 struct cm32181_als_info {
u8 hw_id;
u16 reg_cmd;
@@ -129,15 +142,16 @@ static int cm32181_reg_init(struct cm32181_chip *chip)
 }
 
 /**
- * cm32181_read_als_it() - Get sensor integration time (ms)
+ * cm32181_read_als_it() - Get sensor integration time
  * @chip:  pointer of struct cm32181_chip
- * @val2:  pointer of int to load the als_it value.
+ * @val:   pointer of int to load the integration (sec)
+ * @val2:  pointer of int to load the integration time (microsecond)
  *
  * Report the current integartion time by millisecond.
  *
  * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
  */
-static int cm32181_read_als_it(struct cm32181_chip *chip, int *val2)
+static int cm32181_read_als_it(struct cm32181_chip *chip, int *val, int *val2)
 {
u16 als_it;
int i;
@@ -145,9 +159,10 @@ static int cm32181_read_als_it(struct cm32181_chip *chip, 
int *val2)
als_it = chip-conf_regs[CM32181_REG_ADDR_CMD];
als_it = CM32181_CMD_ALS_IT_MASK;
als_it = CM32181_CMD_ALS_IT_SHIFT;
-   for (i = 0; i  ARRAY_SIZE(als_it_bits); i++) {
-   if (als_it == als_it_bits[i]) {
-   *val2 = als_it_value[i];
+   for (i = 0; i  ARRAY_SIZE(cm32181_als_it_scales); i++) {
+   if (als_it == cm32181_als_it_scales[i].it) {
+   *val = cm32181_als_it_scales[i].val;
+   *val2 = cm32181_als_it_scales[i].val2;
return IIO_VAL_INT_PLUS_MICRO;
}
}
@@ -158,15 +173,44 @@ static int cm32181_read_als_it(struct cm32181_chip *chip, 
int *val2)
 /**
  * cm32181_write_als_it() - Write sensor integration time
  * @chip:  pointer of struct cm32181_chip.
- * @val:   integration time by millisecond.
+ * @val:   integration time in second.
+ * @val2:  integration time in microsecond.
  *
  * Convert integration time (ms) to sensor value.
  *
  * Return: i2c_smbus_write_word_data command return value.
  */
-static int cm32181_write_als_it(struct cm32181_chip *chip, int val)
+static int cm32181_write_als_it(struct cm32181_chip *chip, int val, int val2)
 {
struct i2c_client *client = chip-client;
+   u16 als_it, cmd;
+   int i;
+   s32 ret;
+
+   for (i = 0; i  ARRAY_SIZE(cm32181_als_it_scales); i++) {
+   if (val == cm32181_als_it_scales[i].val 
+   val2 == cm32181_als_it_scales[i].val2) {
+
+   als_it = cm32181_als_it_scales[i].it;
+   als_it = CM32181_CMD_ALS_IT_SHIFT;
+
+   cmd = chip-conf_regs[CM32181_REG_ADDR_CMD];
+   cmd = ~CM32181_CMD_ALS_IT_MASK;
+   cmd |= als_it;
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_CMD,
+   cmd);
+   if (ret  0)
+   return ret;
+
+   chip-conf_regs[CM32181_REG_ADDR_CMD] = cmd;
+   return 0;
+   }
+   }
+   return -EINVAL;
+
+/*
+   struct i2c_client *client = chip-client;
u16 als_it;
int ret, i, n;
 
@@ -190,6 +234,7 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val)
mutex_unlock(chip-lock);
 
return ret;
+*/
 }
 
 /**
@@ -204,26 +249,29 @@ static int cm32181_write_als_it(struct cm32181_chip 
*chip, int val)
 static int cm32181_get_lux(struct cm32181_chip *chip)
 {
struct i2c_client *client = chip-client;
+   struct cm32181_als_info *als_info = chip-als_info;
int ret;
+   int val, val2;
int als_it;
-   unsigned long lux;
+   u64 lux

[PATCH 6/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Added Interrupt support.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 drivers/iio/light/cm32181.c | 156 +++-
 1 file changed, 153 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index b7abd46..1ae32a0 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -47,6 +47,10 @@
 /* ALS_WL register */
 #define CM32181_ALS_WL_DEFAULT 0x
 
+/* STATUS register */
+#define CM32181_STATUS_ALS_IF_LBIT(15)
+#define CM32181_STATUS_ALS_IF_HBIT(14)
+
 /* Software parameters */
 #define CM32181_HW_ID  0x81
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
@@ -122,7 +126,8 @@ void cm32181_parse_dt(struct cm32181_chip *chip)
if (!of_property_read_u32(dn, capella,mlux_per_bit, temp_val))
als_info-mlux_per_bit = (int)temp_val;
if (!of_property_read_u32(dn, capella,thd_percent, temp_val))
-   als_info-thd_percent = (int)temp_val;
+   if (((int)temp_val = 0)  ((int)temp_val  100))
+   als_info-thd_percent = (int)temp_val;
 }
 #endif
 
@@ -173,6 +178,63 @@ static int cm32181_reg_init(struct cm32181_chip *chip)
 }
 
 /**
+ * cm32181_interrupt() - enable/disable interrupt
+ * @chip:   pointer of struct cm32181_chip
+ * @ int_en:   truen for enable; false for disable
+ *
+ * Enable/disable interrupt mode
+ *
+ * Return: 0 for success; otherwise for error code.
+ */
+static int cm32181_interrupt(struct cm32181_chip *cm32181, bool int_en)
+{
+   int ret;
+
+   mutex_lock(cm32181-lock);
+   if (int_en)
+   cm32181-conf_regs[CM32181_REG_ADDR_CMD] |=
+   CM32181_CMD_ALS_INT_EN;
+   else
+   cm32181-conf_regs[CM32181_REG_ADDR_CMD] =
+   ~CM32181_CMD_ALS_INT_EN;
+
+   ret = i2c_smbus_write_word_data(cm32181-client,
+   CM32181_REG_ADDR_CMD,
+   cm32181-conf_regs[CM32181_REG_ADDR_CMD]);
+   mutex_unlock(cm32181-lock);
+   return ret;
+}
+
+static irqreturn_t cm32181_irq_handler(int irq, void *data)
+{
+   struct iio_dev *indio_dev = data;
+   struct cm32181_chip *cm32181 = iio_priv(indio_dev);
+   struct i2c_client *client = cm32181-client;
+   int ret;
+   u64 ev_code;
+
+   ret = i2c_smbus_read_word_data(cm32181-client,
+   CM32181_REG_ADDR_STATUS);
+   if (ret  0) {
+   dev_err(client-dev,
+   %s: Data read failed: %d\n, __func__, ret);
+   return IRQ_NONE;
+   }
+
+   if (!(ret  (CM32181_STATUS_ALS_IF_H | CM32181_STATUS_ALS_IF_L)))
+   return IRQ_NONE;
+
+   ev_code = IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
+   0,
+   IIO_EV_TYPE_THRESH,
+   IIO_EV_DIR_EITHER);
+
+   iio_push_event(indio_dev, ev_code, iio_get_time_ns());
+
+   return IRQ_HANDLED;
+}
+
+/**
  * cm32181_read_als_it() - Get sensor integration time
  * @chip:  pointer of struct cm32181_chip
  * @val:   pointer of int to load the integration (sec)
@@ -242,6 +304,51 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val, int val2)
 }
 
 /**
+ * cm32181_ials_read() - read ALS raw data
+ * @chip:   pointer of struct cm32181_chip
+ *
+ * Read the ALS raw data and update the interrupt threshold windows.
+ *
+ * Return: Positive value is ALS raw data, otherwise is error code.
+ */
+static int cm32181_als_read(struct cm32181_chip *chip)
+{
+   struct i2c_client *client = chip-client;
+   struct cm32181_als_info *als_info = chip-als_info;
+   u16 als, wh, wl, delta;
+   int ret;
+
+   ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
+   if (ret  0)
+   return ret;
+
+   als = (u16)ret;
+
+   if (als_info-thd_percent) {
+   delta = als * als_info-thd_percent / 100;
+   if (delta  3)
+   delta = 3;
+   wh = (als + delta  0x) ? 0x : (als + delta);
+   wl = (als  delta) ? 0 : (als - delta);
+
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_ALS_WH, wh);
+   if (ret  0)
+   return ret;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WH] = wh;
+
+   ret = i2c_smbus_write_word_data(client,
+   CM32181_REG_ADDR_ALS_WL, wl);
+   if (ret  0)
+   return ret;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WL] = wl;
+   ret = als;
+   }
+
+   return ret;
+}
+
+/**
  * cm32181_get_lux() - report current lux value
  * @chip:  pointer of struct cm32181_chip
  *
@@ -252,7 +359,6 @@ static int cm32181_write_als_it(struct cm32181_chip *chip, 
int val, int

[PATCH 1/6] iio: light: Updated Vishay Capella cm32181 ambient light sensor driver.

2015-05-21 Thread Kevin Tsai
- Renamed company name.
- Removed cm32181_reg.
- Removed white space.
- Removed unused include files.
- Updated macro definitions.
- Renamed cm32181_chip pointer to chip.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 drivers/iio/light/Kconfig   |   4 +-
 drivers/iio/light/cm32181.c | 126 +++-
 2 files changed, 67 insertions(+), 63 deletions(-)

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index a437bad..583aa6a 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -39,11 +39,11 @@ config APDS9300
 
 config CM32181
depends on I2C
-   tristate CM32181 driver
+   tristate Vishay Capella CM32181 driver
help
 Say Y here if you use cm32181.
 This option enables ambient light sensor using
-Capella cm32181 device driver.
+Vishay Capella cm32181 device driver.
 
 To compile this driver as a module, choose M here:
 the module will be called cm32181.
diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index 5d12ae54..0491d73 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -1,19 +1,15 @@
 /*
- * Copyright (C) 2013 Capella Microsystems Inc.
- * Author: Kevin Tsai kt...@capellamicro.com
+ * Copyright (C) 2013-2015 Vishay Capella
  *
  * 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
@@ -21,36 +17,43 @@
 
 /* Registers Address */
 #define CM32181_REG_ADDR_CMD   0x00
+#define CM32181_REG_ADDR_ALS_WH0x01
+#define CM32181_REG_ADDR_ALS_WL0x02
 #define CM32181_REG_ADDR_ALS   0x04
 #define CM32181_REG_ADDR_STATUS0x06
 #define CM32181_REG_ADDR_ID0x07
 
 /* Number of Configurable Registers */
-#define CM32181_CONF_REG_NUM   0x01
+#define CM32181_CONF_REG_NUM   3
 
 /* CMD register */
-#define CM32181_CMD_ALS_ENABLE 0x00
-#define CM32181_CMD_ALS_DISABLE0x01
-#define CM32181_CMD_ALS_INT_EN 0x02
+#define CM32181_CMD_ALS_DISABLEBIT(0)
+#define CM32181_CMD_ALS_INT_EN BIT(1)
 
 #define CM32181_CMD_ALS_IT_SHIFT   6
-#define CM32181_CMD_ALS_IT_MASK(0x0F  
CM32181_CMD_ALS_IT_SHIFT)
+#define CM32181_CMD_ALS_IT_MASK(BIT(6) | BIT(7) | BIT(8) | 
BIT(9))
 #define CM32181_CMD_ALS_IT_DEFAULT (0x00  CM32181_CMD_ALS_IT_SHIFT)
 
 #define CM32181_CMD_ALS_SM_SHIFT   11
-#define CM32181_CMD_ALS_SM_MASK(0x03  
CM32181_CMD_ALS_SM_SHIFT)
+#define CM32181_CMD_ALS_SM_MASK(BIT(11) | BIT(12))
 #define CM32181_CMD_ALS_SM_DEFAULT (0x01  CM32181_CMD_ALS_SM_SHIFT)
 
+#define CM32181_CMD_DEFAULT(CM32181_CMD_ALS_IT_DEFAULT | \
+   CM32181_CMD_ALS_SM_DEFAULT)
+
+/* ALS_WH register */
+#define CM32181_ALS_WH_DEFAULT 0x
+
+/* ALS_WL register */
+#define CM32181_ALS_WL_DEFAULT 0x
+
+/* Software parameters */
 #define CM32181_MLUX_PER_BIT   5   /* ALS_SM=01 IT=800ms */
 #define CM32181_MLUX_PER_BIT_BASE_IT   80  /* Based on IT=800ms */
 #defineCM32181_CALIBSCALE_DEFAULT  1000
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define MLUX_PER_LUX   1000
 
-static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
-   CM32181_REG_ADDR_CMD,
-};
-
 static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
 static const int als_it_value[] = {25000, 5, 10, 20, 40,
80};
@@ -64,15 +67,15 @@ struct cm32181_chip {
 
 /**
  * cm32181_reg_init() - Initialize CM32181 registers
- * @cm32181:   pointer of struct cm32181.
+ * @chip:  pointer of struct cm32181_chip
  *
  * Initialize CM32181 ambient light sensor register to default values.
  *
  * Return: 0 for success; otherwise for error code.
  */
-static int cm32181_reg_init(struct cm32181_chip *cm32181)
+static int cm32181_reg_init(struct cm32181_chip *chip)
 {
-   struct i2c_client *client = cm32181-client;
+   struct i2c_client *client = chip-client;
int i;
s32 ret;
 
@@ -85,14 +88,15 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
return -ENODEV;
 
/* Default Values */
-   cm32181-conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE |
-   CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
-   cm32181-calibscale = CM32181_CALIBSCALE_DEFAULT;
+   chip-conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_DEFAULT;
+   chip-conf_regs[CM32181_REG_ADDR_ALS_WH

[PATCH 1/1] iio: light: Added Device Tree support for CM3232 ambient light sensor driver.

2015-05-19 Thread Kevin Tsai
Added Device Tree Support.

Signed-off-by: Kevin Tsai 
---
Added Device Tree support.
Updated company name.
Renamed register name.

 .../devicetree/bindings/iio/light/cm3232.txt   | 22 +++
 MAINTAINERS| 12 ++--
 drivers/iio/light/Kconfig  |  4 +-
 drivers/iio/light/cm3232.c | 71 ++
 4 files changed, 76 insertions(+), 33 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm3232.txt

diff --git a/Documentation/devicetree/bindings/iio/light/cm3232.txt 
b/Documentation/devicetree/bindings/iio/light/cm3232.txt
new file mode 100644
index 000..00dde87
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm3232.txt
@@ -0,0 +1,22 @@
+* Vishay Capella CM3232 Ambient Light sensor
+
+Required properties:
+- compatible: must be "capella,cm3232"
+- reg: the I2C address of the device
+
+Optional properties:
+- capella,reg_cmd: command register initialization.
+- capella,calibscale: calibrated factor with 10^-5 notation.
+- capella,hw_id: hardware device id.
+- capella,mlux_per_bit: millilux per bit under the default IT.
+
+Example:
+
+cm3232@10 {
+   compatible = "capella,cm3232";
+   reg = <0x10>;
+   capella,reg_cmd = <0x04>;
+   capella,calibscale = <10>;
+   capella,hw_id = <0x32>;
+   capella,mlux_per_bit = <64>;
+};
diff --git a/MAINTAINERS b/MAINTAINERS
index f8e0afb..ee6a8f6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2431,12 +2431,6 @@ F:   security/capability.c
 F: security/commoncap.c
 F: kernel/capability.c
 
-CAPELLA MICROSYSTEMS LIGHT SENSOR DRIVER
-M: Kevin Tsai 
-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 
 L: linux-w...@vger.kernel.org
@@ -10610,6 +10604,12 @@ L: net...@vger.kernel.org
 S: Maintained
 F: drivers/net/ethernet/via/via-velocity.*
 
+VISHAY CAPELLA LIGHT SENSOR DRIVER
+M: Kevin Tsai 
+S: Maintained
+F: drivers/iio/light/cm*
+F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+
 VIVID VIRTUAL VIDEO DRIVER
 M: Hans Verkuil 
 L: linux-me...@vger.kernel.org
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index a437bad..5e7e311 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -50,11 +50,11 @@ config CM32181
 
 config CM3232
depends on I2C
-   tristate "CM3232 ambient light sensor"
+   tristate "Vishay Capella CM3232 ambient light sensor"
help
 Say Y here if you use cm3232.
 This option enables ambient light sensor using
-Capella Microsystems cm3232 device driver.
+Vishay Capella cm3232 device driver.
 
 To compile this driver as a module, choose M here:
 the module will be called cm3232.
diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 39c8d99..5354bfa 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -1,8 +1,7 @@
 /*
  * CM3232 Ambient Light Sensor
  *
- * Copyright (C) 2014-2015 Capella Microsystems Inc.
- * Author: Kevin Tsai 
+ * Copyright (C) 2014-2015 Vishay Capella
  *
  * 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
@@ -54,7 +53,7 @@ static const struct {
 };
 
 struct cm3232_als_info {
-   u8 regs_cmd_default;
+   u8 reg_cmd;
u8 hw_id;
int calibscale;
int mlux_per_bit;
@@ -62,7 +61,7 @@ struct cm3232_als_info {
 };
 
 static struct cm3232_als_info cm3232_als_info_default = {
-   .regs_cmd_default = CM3232_CMD_DEFAULT,
+   .reg_cmd = CM3232_CMD_DEFAULT,
.hw_id = CM3232_HW_ID,
.calibscale = CM3232_CALIBSCALE_DEFAULT,
.mlux_per_bit = CM3232_MLUX_PER_BIT_DEFAULT,
@@ -72,10 +71,27 @@ static struct cm3232_als_info cm3232_als_info_default = {
 struct cm3232_chip {
struct i2c_client *client;
struct cm3232_als_info *als_info;
-   u8 regs_cmd;
-   u16 regs_als;
+   u8 reg_cmd;
+   u16 reg_als;
 };
 
+#ifdef CONFIG_OF
+void cm3232_parse_dt(struct cm3232_chip *chip)
+{
+   struct device_node *dn = chip->client->dev.of_node;
+   u32 temp_val;
+
+   if (!of_property_read_u32(dn, "capella,reg_cmd", _val))
+   chip->als_info->reg_cmd = (uint8_t)temp_val;
+   if (!of_property_read_u32(dn, "capella,hw_id", _val))
+   chip->als_info->hw_id = (int)temp_val;
+   if (!of_property_read_u32(dn, "capella,calibscale", _val))
+   chip->als_info->calibscale = (int)temp_val;
+   if (!of_property_read_u32(dn, "capella,mlux_per_bit", _val))
+   chip->als_info->mlux_

[PATCH 1/1] iio: light: Added Device Tree support for CM3232 ambient light sensor driver.

2015-05-19 Thread Kevin Tsai
Added Device Tree Support.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
Added Device Tree support.
Updated company name.
Renamed register name.

 .../devicetree/bindings/iio/light/cm3232.txt   | 22 +++
 MAINTAINERS| 12 ++--
 drivers/iio/light/Kconfig  |  4 +-
 drivers/iio/light/cm3232.c | 71 ++
 4 files changed, 76 insertions(+), 33 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/cm3232.txt

diff --git a/Documentation/devicetree/bindings/iio/light/cm3232.txt 
b/Documentation/devicetree/bindings/iio/light/cm3232.txt
new file mode 100644
index 000..00dde87
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/cm3232.txt
@@ -0,0 +1,22 @@
+* Vishay Capella CM3232 Ambient Light sensor
+
+Required properties:
+- compatible: must be capella,cm3232
+- reg: the I2C address of the device
+
+Optional properties:
+- capella,reg_cmd: command register initialization.
+- capella,calibscale: calibrated factor with 10^-5 notation.
+- capella,hw_id: hardware device id.
+- capella,mlux_per_bit: millilux per bit under the default IT.
+
+Example:
+
+cm3232@10 {
+   compatible = capella,cm3232;
+   reg = 0x10;
+   capella,reg_cmd = 0x04;
+   capella,calibscale = 10;
+   capella,hw_id = 0x32;
+   capella,mlux_per_bit = 64;
+};
diff --git a/MAINTAINERS b/MAINTAINERS
index f8e0afb..ee6a8f6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2431,12 +2431,6 @@ 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
@@ -10610,6 +10604,12 @@ L: net...@vger.kernel.org
 S: Maintained
 F: drivers/net/ethernet/via/via-velocity.*
 
+VISHAY CAPELLA LIGHT SENSOR DRIVER
+M: Kevin Tsai kt...@capellamicro.com
+S: Maintained
+F: drivers/iio/light/cm*
+F: Documentation/devicetree/bindings/i2c/trivial-devices.txt
+
 VIVID VIRTUAL VIDEO DRIVER
 M: Hans Verkuil hverk...@xs4all.nl
 L: linux-me...@vger.kernel.org
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index a437bad..5e7e311 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -50,11 +50,11 @@ config CM32181
 
 config CM3232
depends on I2C
-   tristate CM3232 ambient light sensor
+   tristate Vishay Capella CM3232 ambient light sensor
help
 Say Y here if you use cm3232.
 This option enables ambient light sensor using
-Capella Microsystems cm3232 device driver.
+Vishay Capella cm3232 device driver.
 
 To compile this driver as a module, choose M here:
 the module will be called cm3232.
diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 39c8d99..5354bfa 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -1,8 +1,7 @@
 /*
  * CM3232 Ambient Light Sensor
  *
- * Copyright (C) 2014-2015 Capella Microsystems Inc.
- * Author: Kevin Tsai kt...@capellamicro.com
+ * Copyright (C) 2014-2015 Vishay Capella
  *
  * 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
@@ -54,7 +53,7 @@ static const struct {
 };
 
 struct cm3232_als_info {
-   u8 regs_cmd_default;
+   u8 reg_cmd;
u8 hw_id;
int calibscale;
int mlux_per_bit;
@@ -62,7 +61,7 @@ struct cm3232_als_info {
 };
 
 static struct cm3232_als_info cm3232_als_info_default = {
-   .regs_cmd_default = CM3232_CMD_DEFAULT,
+   .reg_cmd = CM3232_CMD_DEFAULT,
.hw_id = CM3232_HW_ID,
.calibscale = CM3232_CALIBSCALE_DEFAULT,
.mlux_per_bit = CM3232_MLUX_PER_BIT_DEFAULT,
@@ -72,10 +71,27 @@ static struct cm3232_als_info cm3232_als_info_default = {
 struct cm3232_chip {
struct i2c_client *client;
struct cm3232_als_info *als_info;
-   u8 regs_cmd;
-   u16 regs_als;
+   u8 reg_cmd;
+   u16 reg_als;
 };
 
+#ifdef CONFIG_OF
+void cm3232_parse_dt(struct cm3232_chip *chip)
+{
+   struct device_node *dn = chip-client-dev.of_node;
+   u32 temp_val;
+
+   if (!of_property_read_u32(dn, capella,reg_cmd, temp_val))
+   chip-als_info-reg_cmd = (uint8_t)temp_val;
+   if (!of_property_read_u32(dn, capella,hw_id, temp_val))
+   chip-als_info-hw_id = (int)temp_val;
+   if (!of_property_read_u32(dn, capella,calibscale, temp_val))
+   chip-als_info-calibscale = (int)temp_val;
+   if (!of_property_read_u32(dn, capella,mlux_per_bit, temp_val))
+   chip-als_info-mlux_per_bit = (int)temp_val;
+}
+#endif

RE: [PATCH] iio: light: Add support for Capella CM3323 color/light sensor

2015-03-02 Thread Kevin Tsai
Hi Daniel,

Ambient light sensor is trying to match the brightness sensitivity of human 
visual system.  Please see the following links:
http://en.wikipedia.org/wiki/Color_vision#mediaviewer/File:Eyesensitivity.svg
http://en.wikipedia.org/wiki/Color_vision

You can compare the spectrum with the datasheet.  The green channel is matched 
with ALS spectrum.

Kevin Tsai
03/02/15

-Original Message-
From: daniel.bal...@gmail.com [mailto:daniel.bal...@gmail.com] On Behalf Of 
Daniel Baluta
Sent: Monday, March 02, 2015 1:57 AM
To: Daniel Baluta
Cc: Jonathan Cameron; Kevin Tsai; Hartmut Knaack; Lars-Peter Clausen; Peter 
Meerwald; linux-...@vger.kernel.org; Linux Kernel Mailing List
Subject: Re: [PATCH] iio: light: Add support for Capella CM3323 color/light 
sensor

On Sun, Jan 25, 2015 at 12:50 PM, Daniel Baluta  wrote:
> On Sun, Jan 25, 2015 at 12:27 PM, Jonathan Cameron  wrote:
>> On 22/01/15 10:10, Daniel Baluta wrote:
>>> Minimal implementation providing raw light intensity and illuminance 
>>> readings. For illuminance user can compute lux values using raw 
>>> readings and scale.
>>>
>>> This driver also supports CM3323E sensor chip.
>>>
>>> Cc: Kevin Tsai 
>>> Signed-off-by: Daniel Baluta 
>>
>> Hi Daniel,
>>
>> My only real question on this one is whether using the 'green' 
>> channel and pretending it is a measure of illuminance is a good idea 
>> or whether we are better leaving that decision to userspace...
>>
>> I'm guessing the reason it is green rather than clear is that the 
>> clear is letting infrared through and we don't have an additional 
>> infrared sensor available to allow that component to be removed?
>
> Hi Jonathan,
>
> 
>>> +#define CM3323_COLOR_CHANNEL(_color, _addr) {\
>>> + .type = IIO_INTENSITY, \
>>> + .modified = 1, \
>>> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
>>> + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \
>>> + .channel2 = IIO_MOD_LIGHT_##_color, \
>>> + .address = _addr, \
>>> +}
>>> +
>>> +/*
>>> + * CM3323's GREEN channel is used for reading illuminance
>> That's impressively random and unlikely to be anywhere near correct 
>> pretty much all the time!
>>
>> I'd personally prefer just not providing an illuminance channel and 
>> leaving it up to userspace to make the decision on whether the green 
>> channel is good enough...
>
> Datasheet says that the GREEN channel should be used for getting 
> illuminance readings. I'm not sure if this is totally random.
>
> I understand your worries, perhaps Kevin could 'enlighten' us here.
>
> I would  prefer to have the illuminance channel exported from IIO 
> because there are already userspace applications/frameworks  written 
> and tested for ALS that use IIO illuminance channel.
> We want to use this driver without modifying user space.
>
> Of course, one option for the moment would be to only export the 
> INTENSITY channels as per your recommendation and to decide later what 
> to do for the LIGHT channel.

Hi Kevin,

Could you tell us how reliable is using the GREEN channel for getting 
illuminance readings?

If no objection, I will resend this driver exposing only INTENSITY channels for 
the moment.

thanks,
Daniel.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


RE: [PATCH] iio: light: Add support for Capella CM3323 color/light sensor

2015-03-02 Thread Kevin Tsai
Hi Daniel,

Ambient light sensor is trying to match the brightness sensitivity of human 
visual system.  Please see the following links:
http://en.wikipedia.org/wiki/Color_vision#mediaviewer/File:Eyesensitivity.svg
http://en.wikipedia.org/wiki/Color_vision

You can compare the spectrum with the datasheet.  The green channel is matched 
with ALS spectrum.

Kevin Tsai
03/02/15

-Original Message-
From: daniel.bal...@gmail.com [mailto:daniel.bal...@gmail.com] On Behalf Of 
Daniel Baluta
Sent: Monday, March 02, 2015 1:57 AM
To: Daniel Baluta
Cc: Jonathan Cameron; Kevin Tsai; Hartmut Knaack; Lars-Peter Clausen; Peter 
Meerwald; linux-...@vger.kernel.org; Linux Kernel Mailing List
Subject: Re: [PATCH] iio: light: Add support for Capella CM3323 color/light 
sensor

On Sun, Jan 25, 2015 at 12:50 PM, Daniel Baluta daniel.bal...@intel.com wrote:
 On Sun, Jan 25, 2015 at 12:27 PM, Jonathan Cameron ji...@kernel.org wrote:
 On 22/01/15 10:10, Daniel Baluta wrote:
 Minimal implementation providing raw light intensity and illuminance 
 readings. For illuminance user can compute lux values using raw 
 readings and scale.

 This driver also supports CM3323E sensor chip.

 Cc: Kevin Tsai kt...@capellamicro.com
 Signed-off-by: Daniel Baluta daniel.bal...@intel.com

 Hi Daniel,

 My only real question on this one is whether using the 'green' 
 channel and pretending it is a measure of illuminance is a good idea 
 or whether we are better leaving that decision to userspace...

 I'm guessing the reason it is green rather than clear is that the 
 clear is letting infrared through and we don't have an additional 
 infrared sensor available to allow that component to be removed?

 Hi Jonathan,

 snip
 +#define CM3323_COLOR_CHANNEL(_color, _addr) {\
 + .type = IIO_INTENSITY, \
 + .modified = 1, \
 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \
 + .channel2 = IIO_MOD_LIGHT_##_color, \
 + .address = _addr, \
 +}
 +
 +/*
 + * CM3323's GREEN channel is used for reading illuminance
 That's impressively random and unlikely to be anywhere near correct 
 pretty much all the time!

 I'd personally prefer just not providing an illuminance channel and 
 leaving it up to userspace to make the decision on whether the green 
 channel is good enough...

 Datasheet says that the GREEN channel should be used for getting 
 illuminance readings. I'm not sure if this is totally random.

 I understand your worries, perhaps Kevin could 'enlighten' us here.

 I would  prefer to have the illuminance channel exported from IIO 
 because there are already userspace applications/frameworks  written 
 and tested for ALS that use IIO illuminance channel.
 We want to use this driver without modifying user space.

 Of course, one option for the moment would be to only export the 
 INTENSITY channels as per your recommendation and to decide later what 
 to do for the LIGHT channel.

Hi Kevin,

Could you tell us how reliable is using the GREEN channel for getting 
illuminance readings?

If no objection, I will resend this driver exposing only INTENSITY channels for 
the moment.

thanks,
Daniel.

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH V2 1/1] iio: light: Added PM support for Capella CM3232 ambient light sensor driver.

2015-02-19 Thread Kevin Tsai
Added Power Management Support.

Signed-off-by: Kevin Tsai 
---
v2:
Added CONFIG_PM_SLEEP to suspend/resume functions to fix the build warning
when CONFIG_PM_SLEEP is not selected.

v1:
Added Power Management support.

 drivers/iio/light/cm3232.c | 36 
 1 file changed, 36 insertions(+)

diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 90e3519..39c8d99 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -378,6 +378,39 @@ static const struct i2c_device_id cm3232_id[] = {
{}
 };
 
+#ifdef CONFIG_PM_SLEEP
+static int cm3232_suspend(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm3232_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip->client;
+   int ret;
+
+   chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
+   chip->regs_cmd);
+
+   return ret;
+}
+
+static int cm3232_resume(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm3232_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip->client;
+   int ret;
+
+   chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
+   chip->regs_cmd | CM3232_CMD_ALS_RESET);
+
+   return ret;
+}
+
+static const struct dev_pm_ops cm3232_pm_ops = {
+   SET_SYSTEM_SLEEP_PM_OPS(cm3232_suspend, cm3232_resume)};
+#endif
+
 MODULE_DEVICE_TABLE(i2c, cm3232_id);
 
 static const struct of_device_id cm3232_of_match[] = {
@@ -390,6 +423,9 @@ static struct i2c_driver cm3232_driver = {
.name   = "cm3232",
.owner  = THIS_MODULE,
.of_match_table = of_match_ptr(cm3232_of_match),
+#ifdef CONFIG_PM_SLEEP
+   .pm = _pm_ops,
+#endif
},
.id_table   = cm3232_id,
.probe  = cm3232_probe,
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH V2 1/1] iio: light: Added PM support for Capella CM3232 ambient light sensor driver.

2015-02-19 Thread Kevin Tsai
Added Power Management Support.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
v2:
Added CONFIG_PM_SLEEP to suspend/resume functions to fix the build warning
when CONFIG_PM_SLEEP is not selected.

v1:
Added Power Management support.

 drivers/iio/light/cm3232.c | 36 
 1 file changed, 36 insertions(+)

diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 90e3519..39c8d99 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -378,6 +378,39 @@ static const struct i2c_device_id cm3232_id[] = {
{}
 };
 
+#ifdef CONFIG_PM_SLEEP
+static int cm3232_suspend(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm3232_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip-client;
+   int ret;
+
+   chip-regs_cmd |= CM3232_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
+   chip-regs_cmd);
+
+   return ret;
+}
+
+static int cm3232_resume(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm3232_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip-client;
+   int ret;
+
+   chip-regs_cmd = ~CM3232_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
+   chip-regs_cmd | CM3232_CMD_ALS_RESET);
+
+   return ret;
+}
+
+static const struct dev_pm_ops cm3232_pm_ops = {
+   SET_SYSTEM_SLEEP_PM_OPS(cm3232_suspend, cm3232_resume)};
+#endif
+
 MODULE_DEVICE_TABLE(i2c, cm3232_id);
 
 static const struct of_device_id cm3232_of_match[] = {
@@ -390,6 +423,9 @@ static struct i2c_driver cm3232_driver = {
.name   = cm3232,
.owner  = THIS_MODULE,
.of_match_table = of_match_ptr(cm3232_of_match),
+#ifdef CONFIG_PM_SLEEP
+   .pm = cm3232_pm_ops,
+#endif
},
.id_table   = cm3232_id,
.probe  = cm3232_probe,
-- 
1.8.3.1

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH V1 1/1] iio: light: Added PM support for Capella CM3232 ambient light sensor driver.

2015-02-18 Thread Kevin Tsai
Added Power Management Support.

Signed-off-by: Kevin Tsai 
---
v1:
Added Power Management support.

 drivers/iio/light/cm3232.c | 32 
 1 file changed, 32 insertions(+)

diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 90e3519..9e54ea7 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -378,6 +378,34 @@ static const struct i2c_device_id cm3232_id[] = {
{}
 };
 
+static int cm3232_suspend(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm3232_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip->client;
+   int ret;
+
+   chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
+   chip->regs_cmd);
+
+   return ret;
+}
+
+static int cm3232_resume(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm3232_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip->client;
+   int ret;
+
+   chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
+   chip->regs_cmd | CM3232_CMD_ALS_RESET);
+
+   return ret;
+}
+
 MODULE_DEVICE_TABLE(i2c, cm3232_id);
 
 static const struct of_device_id cm3232_of_match[] = {
@@ -385,11 +413,15 @@ static const struct of_device_id cm3232_of_match[] = {
{}
 };
 
+static const struct dev_pm_ops cm3232_pm_ops = {
+   SET_SYSTEM_SLEEP_PM_OPS(cm3232_suspend, cm3232_resume)};
+
 static struct i2c_driver cm3232_driver = {
.driver = {
.name   = "cm3232",
.owner  = THIS_MODULE,
.of_match_table = of_match_ptr(cm3232_of_match),
+   .pm = _pm_ops,
},
.id_table   = cm3232_id,
.probe  = cm3232_probe,
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH V1 1/1] iio: light: Added PM support for Capella CM3232 ambient light sensor driver.

2015-02-18 Thread Kevin Tsai
Added Power Management Support.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
v1:
Added Power Management support.

 drivers/iio/light/cm3232.c | 32 
 1 file changed, 32 insertions(+)

diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 90e3519..9e54ea7 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -378,6 +378,34 @@ static const struct i2c_device_id cm3232_id[] = {
{}
 };
 
+static int cm3232_suspend(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm3232_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip-client;
+   int ret;
+
+   chip-regs_cmd |= CM3232_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
+   chip-regs_cmd);
+
+   return ret;
+}
+
+static int cm3232_resume(struct device *dev)
+{
+   struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+   struct cm3232_chip *chip = iio_priv(indio_dev);
+   struct i2c_client *client = chip-client;
+   int ret;
+
+   chip-regs_cmd = ~CM3232_CMD_ALS_DISABLE;
+   ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
+   chip-regs_cmd | CM3232_CMD_ALS_RESET);
+
+   return ret;
+}
+
 MODULE_DEVICE_TABLE(i2c, cm3232_id);
 
 static const struct of_device_id cm3232_of_match[] = {
@@ -385,11 +413,15 @@ static const struct of_device_id cm3232_of_match[] = {
{}
 };
 
+static const struct dev_pm_ops cm3232_pm_ops = {
+   SET_SYSTEM_SLEEP_PM_OPS(cm3232_suspend, cm3232_resume)};
+
 static struct i2c_driver cm3232_driver = {
.driver = {
.name   = cm3232,
.owner  = THIS_MODULE,
.of_match_table = of_match_ptr(cm3232_of_match),
+   .pm = cm3232_pm_ops,
},
.id_table   = cm3232_id,
.probe  = cm3232_probe,
-- 
1.8.3.1

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH V4 1/1] iio: Updated Capella cm3232 ambient light sensor driver.

2015-02-12 Thread Kevin Tsai
Added ACPI and Power Management support.

Signed-off-by: Kevin Tsai 
---
v4:
Added ACPI and Power Management support.

Thanks Srinivas Pandruvada's ACPI routines.

v3:
Added hw_id to als_info structure.
Removed unused include files.
Modified cm3232_write_als_it() to handle register update fail.

Thanks comments from Daniel Baluta.

v2:
Removed unused CM3232_CMD_ALS_HS.
Modified cm3232_als_info structure.  Removed id field.
Modified cm3232_chip structure.
Merged CM3232_als_it_bits and CM3232_als_it_values to cm3232_it_scale.
Removed mutex lock.
Renamed als_raw to regs_als.  Moved it to cm3232_chip structure.
Modified cm3232_read_als_it() and cm3232_write_als_it() to support val2.

Thanks comments from Jeremiah Mahler, Peter Meerwald, Daniel Baluta,
and Joe Perches.

v1:
Added cm3232.c to support Capella Microsystems CM3232 Ambient Light
Sensor.

 drivers/iio/light/cm3232.c | 117 +++--
 1 file changed, 114 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 90e3519..7a8a624 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -2,7 +2,6 @@
  * CM3232 Ambient Light Sensor
  *
  * Copyright (C) 2014-2015 Capella Microsystems Inc.
- * Author: Kevin Tsai 
  *
  * 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
@@ -16,6 +15,7 @@
 #include 
 #include 
 #include 
+#include 
 
 /* Registers Address */
 #define CM3232_REG_ADDR_CMD0x00
@@ -77,6 +77,50 @@ struct cm3232_chip {
 };
 
 /**
+ *  cm32181_acpi_get_cpm_info() - Get CPM object from ACPI
+ *  @client  pointer of struct i2c_client.
+ *  @obj_namepointer of ACPI object name.
+ *  @count   maximum size of return array.
+ *  @vals  pointer of array for return elements.
+ *
+ *  Convert ACPI CPM table to array. Special thanks Srinivas Pandruvada's
+ *  help to implement this routine.
+ *
+ *  Return: -ENODEV for fail.  Otherwise is number of elements.
+ */
+static int cm32181_acpi_get_cpm_info(struct i2c_client *client, char *obj_name,
+   int count, u64 *vals)
+{
+   acpi_handle handle;
+   struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
+   int i;
+   acpi_status status;
+   union acpi_object *cpm;
+
+   handle = ACPI_HANDLE(>dev);
+   if (!handle)
+   return -ENODEV;
+
+   status = acpi_evaluate_object(handle, obj_name, NULL, );
+   if (ACPI_FAILURE(status)) {
+   dev_err(>dev, "object %s not found\n", obj_name);
+   return -ENODEV;
+   }
+
+   cpm = buffer.pointer;
+   for (i = 0; i < cpm->package.count && i < count; ++i) {
+   union acpi_object *elem;
+
+   elem = &(cpm->package.elements[i]);
+   vals[i] = elem->integer.value;
+   }
+
+   kfree(buffer.pointer);
+
+   return cpm->package.count;
+}
+
+/**
  * cm3232_reg_init() - Initialize CM3232
  * @chip:  pointer of struct cm3232_chip.
  *
@@ -88,9 +132,35 @@ static int cm3232_reg_init(struct cm3232_chip *chip)
 {
struct i2c_client *client = chip->client;
s32 ret;
+   int cpm_elem_count;
+   u64 cpm_elems[20];
 
chip->als_info = _als_info_default;
 
+   if (ACPI_HANDLE(>dev)) {
+   /* Load from ACPI */
+   cpm_elem_count = cm32181_acpi_get_cpm_info(client, "CPM0",
+   ARRAY_SIZE(cpm_elems),
+   cpm_elems);
+   if (cpm_elem_count > 0) {
+   int header_num = 3;
+   int regs_bmp = cpm_elems[2];
+
+   chip->als_info->hw_id = (u8)cpm_elems[0];
+   if (regs_bmp & BIT(0))
+   chip->als_info->regs_cmd_default =
+cpm_elems[header_num];
+   }
+
+   cpm_elem_count = cm32181_acpi_get_cpm_info(client, "CPM1",
+   ARRAY_SIZE(cpm_elems),
+   cpm_elems);
+   if (cpm_elem_count > 0) {
+   chip->als_info->mlux_per_bit = (int)cpm_elems[0] / 100;
+   chip->als_info->calibscale = (int)cpm_elems[1];
+   }
+   }
+
/* Identify device */
ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ID);
if (ret < 0) {
@@ -211,6 +281,7 @@ static int cm3232_get_lux(struct cm3232_chip *chip)
ret = cm3232_read_als_it(chip, , );
if (ret < 0)
return -EINVAL;
+
als_it = val * 100 + val2;
lux = (

[PATCH V4 1/1] iio: Updated Capella cm3232 ambient light sensor driver.

2015-02-12 Thread Kevin Tsai
Added ACPI and Power Management support.

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
v4:
Added ACPI and Power Management support.

Thanks Srinivas Pandruvada's ACPI routines.

v3:
Added hw_id to als_info structure.
Removed unused include files.
Modified cm3232_write_als_it() to handle register update fail.

Thanks comments from Daniel Baluta.

v2:
Removed unused CM3232_CMD_ALS_HS.
Modified cm3232_als_info structure.  Removed id field.
Modified cm3232_chip structure.
Merged CM3232_als_it_bits and CM3232_als_it_values to cm3232_it_scale.
Removed mutex lock.
Renamed als_raw to regs_als.  Moved it to cm3232_chip structure.
Modified cm3232_read_als_it() and cm3232_write_als_it() to support val2.

Thanks comments from Jeremiah Mahler, Peter Meerwald, Daniel Baluta,
and Joe Perches.

v1:
Added cm3232.c to support Capella Microsystems CM3232 Ambient Light
Sensor.

 drivers/iio/light/cm3232.c | 117 +++--
 1 file changed, 114 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 90e3519..7a8a624 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -2,7 +2,6 @@
  * CM3232 Ambient Light Sensor
  *
  * Copyright (C) 2014-2015 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
@@ -16,6 +15,7 @@
 #include linux/iio/iio.h
 #include linux/iio/sysfs.h
 #include linux/init.h
+#include linux/acpi.h
 
 /* Registers Address */
 #define CM3232_REG_ADDR_CMD0x00
@@ -77,6 +77,50 @@ struct cm3232_chip {
 };
 
 /**
+ *  cm32181_acpi_get_cpm_info() - Get CPM object from ACPI
+ *  @client  pointer of struct i2c_client.
+ *  @obj_namepointer of ACPI object name.
+ *  @count   maximum size of return array.
+ *  @vals  pointer of array for return elements.
+ *
+ *  Convert ACPI CPM table to array. Special thanks Srinivas Pandruvada's
+ *  help to implement this routine.
+ *
+ *  Return: -ENODEV for fail.  Otherwise is number of elements.
+ */
+static int cm32181_acpi_get_cpm_info(struct i2c_client *client, char *obj_name,
+   int count, u64 *vals)
+{
+   acpi_handle handle;
+   struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
+   int i;
+   acpi_status status;
+   union acpi_object *cpm;
+
+   handle = ACPI_HANDLE(client-dev);
+   if (!handle)
+   return -ENODEV;
+
+   status = acpi_evaluate_object(handle, obj_name, NULL, buffer);
+   if (ACPI_FAILURE(status)) {
+   dev_err(client-dev, object %s not found\n, obj_name);
+   return -ENODEV;
+   }
+
+   cpm = buffer.pointer;
+   for (i = 0; i  cpm-package.count  i  count; ++i) {
+   union acpi_object *elem;
+
+   elem = (cpm-package.elements[i]);
+   vals[i] = elem-integer.value;
+   }
+
+   kfree(buffer.pointer);
+
+   return cpm-package.count;
+}
+
+/**
  * cm3232_reg_init() - Initialize CM3232
  * @chip:  pointer of struct cm3232_chip.
  *
@@ -88,9 +132,35 @@ static int cm3232_reg_init(struct cm3232_chip *chip)
 {
struct i2c_client *client = chip-client;
s32 ret;
+   int cpm_elem_count;
+   u64 cpm_elems[20];
 
chip-als_info = cm3232_als_info_default;
 
+   if (ACPI_HANDLE(client-dev)) {
+   /* Load from ACPI */
+   cpm_elem_count = cm32181_acpi_get_cpm_info(client, CPM0,
+   ARRAY_SIZE(cpm_elems),
+   cpm_elems);
+   if (cpm_elem_count  0) {
+   int header_num = 3;
+   int regs_bmp = cpm_elems[2];
+
+   chip-als_info-hw_id = (u8)cpm_elems[0];
+   if (regs_bmp  BIT(0))
+   chip-als_info-regs_cmd_default =
+cpm_elems[header_num];
+   }
+
+   cpm_elem_count = cm32181_acpi_get_cpm_info(client, CPM1,
+   ARRAY_SIZE(cpm_elems),
+   cpm_elems);
+   if (cpm_elem_count  0) {
+   chip-als_info-mlux_per_bit = (int)cpm_elems[0] / 100;
+   chip-als_info-calibscale = (int)cpm_elems[1];
+   }
+   }
+
/* Identify device */
ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ID);
if (ret  0) {
@@ -211,6 +281,7 @@ static int cm3232_get_lux(struct cm3232_chip *chip)
ret = cm3232_read_als_it(chip, val, val2);
if (ret  0)
return -EINVAL;
+
als_it = val * 100 + val2;
lux = (__force u64

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

2015-01-15 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 
---
v3:
Added hw_id to als_info structure.
Removed unused include files.
Modified cm3232_write_als_it() to handle register update fail.

Thanks comments from Daniel Baluta.

v2:
Removed unused CM3232_CMD_ALS_HS.
Modified cm3232_als_info structure.  Removed id field.
Modified cm3232_chip structure.
Merged CM3232_als_it_bits and CM3232_als_it_values to cm3232_it_scale.
Removed mutex lock.
Renamed als_raw to regs_als.  Moved it to cm3232_chip structure.
Modified cm3232_read_als_it() and cm3232_write_als_it() to support val2.

Thanks comments from Jeremiah Mahler, Peter Meerwald, Daniel Baluta,
and Joe Perches.

v1:
Added cm3232.c to support Capella Microsystems CM3232 Ambient Light
Sensor.

 .../devicetree/bindings/i2c/trivial-devices.txt|   1 +
 MAINTAINERS|   6 +
 drivers/iio/light/Kconfig  |  11 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm3232.c | 409 +
 5 files changed, 428 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 
+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 
 L: linux-w...@vger.kernel.org
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 5bea821..cd5028e 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 ambient light sensor"
+   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..8573f98
--- /dev/null
+++ b/drivers/iio/light/cm3232.c
@@ -0,0 +1,409 @@
+/*
+ * CM3232 Ambient Light Sensor
+ *
+ * Copyright (C) 2014-2015 Capella Microsystems Inc.
+ * Author: Kevin Tsai 
+ *
+ * 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.
+ *
+ * IIO driver for CM3232 (7-bit I2C slave address 0x10).
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Registers Address */
+#define CM3232_REG_ADDR_CMD0x00
+#define CM3232_REG_ADDR_ALS0x50
+#define CM3232_REG_ADDR_ID 0x53
+
+#define CM3232_CMD_ALS_DISABLE BIT(0)
+
+#define CM3232_CMD_ALS_IT_SHIFT2
+#define CM3232_CMD_ALS_IT_MASK (BIT(2) | BIT(3) | BIT(4))
+#define CM3232_CMD_ALS_IT_DEFAULT  (0x01 << CM3232_CMD_ALS_IT_SHIFT)
+
+#define CM3232_CMD_ALS_RESET   BIT(6)
+
+

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

2015-01-15 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
---
v3:
Added hw_id to als_info structure.
Removed unused include files.
Modified cm3232_write_als_it() to handle register update fail.

Thanks comments from Daniel Baluta.

v2:
Removed unused CM3232_CMD_ALS_HS.
Modified cm3232_als_info structure.  Removed id field.
Modified cm3232_chip structure.
Merged CM3232_als_it_bits and CM3232_als_it_values to cm3232_it_scale.
Removed mutex lock.
Renamed als_raw to regs_als.  Moved it to cm3232_chip structure.
Modified cm3232_read_als_it() and cm3232_write_als_it() to support val2.

Thanks comments from Jeremiah Mahler, Peter Meerwald, Daniel Baluta,
and Joe Perches.

v1:
Added cm3232.c to support Capella Microsystems CM3232 Ambient Light
Sensor.

 .../devicetree/bindings/i2c/trivial-devices.txt|   1 +
 MAINTAINERS|   6 +
 drivers/iio/light/Kconfig  |  11 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm3232.c | 409 +
 5 files changed, 428 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..cd5028e 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 ambient light sensor
+   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..8573f98
--- /dev/null
+++ b/drivers/iio/light/cm3232.c
@@ -0,0 +1,409 @@
+/*
+ * CM3232 Ambient Light Sensor
+ *
+ * Copyright (C) 2014-2015 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.
+ *
+ * IIO driver for CM3232 (7-bit I2C slave address 0x10).
+ *
+ */
+
+#include linux/i2c.h
+#include linux/module.h
+#include linux/iio/iio.h
+#include linux/iio/sysfs.h
+#include linux/init.h
+
+/* Registers Address */
+#define CM3232_REG_ADDR_CMD0x00
+#define CM3232_REG_ADDR_ALS0x50
+#define CM3232_REG_ADDR_ID 0x53
+
+#define CM3232_CMD_ALS_DISABLE BIT(0)
+
+#define CM3232_CMD_ALS_IT_SHIFT2
+#define CM3232_CMD_ALS_IT_MASK (BIT(2) | BIT(3) | BIT(4

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

2015-01-06 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.

v2:
Removed unused CM3232_CMD_ALS_HS.
Modified cm3232_als_info structure.  Removed id field.
Modified cm3232_chip structure.
Merged CM3232_als_it_bits and CM3232_als_it_values to cm3232_it_scale.
Removed mutex lock.
Renamed als_raw to regs_als.  Moved it to cm3232_chip structure.
Modified cm3232_read_als_it() and cm3232_write_als_it() to support val2.

Thanks comments from Jeremiah Mahler, Peter Meerwald, Daniel Baluta,
and Joe Perches.

v1:
Added cm3232.c to support Capella Microsystems CM3232 Ambient Light
Sensor.

Signed-off-by: Kevin Tsai 
---
 .../devicetree/bindings/i2c/trivial-devices.txt|   1 +
 MAINTAINERS|   6 +
 drivers/iio/light/Kconfig  |  11 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm3232.c | 411 +
 5 files changed, 430 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 
+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 
 L: linux-w...@vger.kernel.org
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 5bea821..cd5028e 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 ambient light sensor"
+   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..b4b7d1b
--- /dev/null
+++ b/drivers/iio/light/cm3232.c
@@ -0,0 +1,411 @@
+/*
+ * CM3232 Ambient Light Sensor
+ *
+ * Copyright (C) 2014-2015 Capella Microsystems Inc.
+ * Author: Kevin Tsai 
+ *
+ * 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.
+ *
+ * IIO driver for CM3232 (7-bit I2C slave address 0x10).
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Registers Address */
+#define CM3232_REG_ADDR_CMD 0x00
+#define CM3232_REG_ADDR_ALS 0x50
+#define CM3232_REG_ADDR_ID 0x53
+
+/* CMD register */
+#define CM3232_CMD_ALS_DISABLE BIT(0)
+
+#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_RESET BIT(6)
+
+#define CM3232_CMD_DEFAULT CM3232_CMD_ALS_IT_DEFAULT
+
+#defineCM3232_HW_ID 0x32
+#define CM3232_CALIBSCALE_DEFAULT 10
+#define CM3232_CALIB

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

2015-01-06 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.

v2:
Removed unused CM3232_CMD_ALS_HS.
Modified cm3232_als_info structure.  Removed id field.
Modified cm3232_chip structure.
Merged CM3232_als_it_bits and CM3232_als_it_values to cm3232_it_scale.
Removed mutex lock.
Renamed als_raw to regs_als.  Moved it to cm3232_chip structure.
Modified cm3232_read_als_it() and cm3232_write_als_it() to support val2.

Thanks comments from Jeremiah Mahler, Peter Meerwald, Daniel Baluta,
and Joe Perches.

v1:
Added cm3232.c to support Capella Microsystems CM3232 Ambient Light
Sensor.

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 | 411 +
 5 files changed, 430 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..cd5028e 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 ambient light sensor
+   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..b4b7d1b
--- /dev/null
+++ b/drivers/iio/light/cm3232.c
@@ -0,0 +1,411 @@
+/*
+ * CM3232 Ambient Light Sensor
+ *
+ * Copyright (C) 2014-2015 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.
+ *
+ * IIO driver for CM3232 (7-bit I2C slave address 0x10).
+ *
+ */
+
+#include linux/delay.h
+#include linux/err.h
+#include linux/i2c.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_CMD 0x00
+#define CM3232_REG_ADDR_ALS 0x50
+#define CM3232_REG_ADDR_ID 0x53
+
+/* CMD register */
+#define CM3232_CMD_ALS_DISABLE BIT(0)
+
+#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

[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 
---
 .../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 
+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 
 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 
+ *
+ * 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* 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, 20, 40, 80, 160, 320};
+
+struct cm3232_als_info {
+   u32 id;
+   int calibscale;
+   int mlux_per_bit;
+   int mlux_per_bit_base_it;
+   const int *als_it_bits;
+   const i

[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

[PATCH 1/1] Added Capella CM32181 Ambient Light Sensor IIO Driver.

2013-12-13 Thread Kevin Tsai
Signed-off-by: Kevin Tsai 
---
 drivers/iio/light/Kconfig   |  11 ++
 drivers/iio/light/Makefile  |   1 +
 drivers/iio/light/cm32181.c | 437 
 3 files changed, 449 insertions(+)
 create mode 100644 drivers/iio/light/cm32181.c

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index a022f27..d12b2a0 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -27,6 +27,17 @@ config APDS9300
 To compile this driver as a module, choose M here: the
 module will be called apds9300.
 
+config CM32181
+   depends on I2C
+   tristate "CM32181 driver"
+   help
+Say Y here if you use cm32181.
+This option enables ambient light sensor using
+Capella cm32181 device driver.
+
+To compile this driver as a module, choose M here:
+the module will be called cm32181.
+
 config CM36651
depends on I2C
tristate "CM36651 driver"
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index daa327f..60e35ac 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -5,6 +5,7 @@
 # When adding new entries keep the list in alphabetical order
 obj-$(CONFIG_ADJD_S311)+= adjd_s311.o
 obj-$(CONFIG_APDS9300) += apds9300.o
+obj-$(CONFIG_CM32181)  += cm32181.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/cm32181.c b/drivers/iio/light/cm32181.c
new file mode 100644
index 000..58ab1b2
--- /dev/null
+++ b/drivers/iio/light/cm32181.c
@@ -0,0 +1,437 @@
+/*
+ * Copyright (C) 2013 Capella Microsystems Inc.
+ * Author: Kevin Tsai 
+ *
+ * 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Registers Address */
+#defineCM32181_REG_ADDR_CMD0x00
+#defineCM32181_REG_ADDR_ALS0x04
+#defineCM32181_REG_ADDR_STATUS 0x06
+#defineCM32181_REG_ADDR_ID 0x07
+
+/* Number of Configurable Registers */
+#define CM32181_CONF_REG_NUM   0x01
+
+/* CMD register */
+#define CM32181_CMD_ALS_ENABLE 0x00
+#define CM32181_CMD_ALS_DISABLE0x01
+#define CM32181_CMD_ALS_INT_EN 0x02
+
+#defineCM32181_CMD_ALS_IT_SHIFT6
+#defineCM32181_CMD_ALS_IT_MASK (0x0F << 
CM32181_CMD_ALS_IT_SHIFT)
+#defineCM32181_CMD_ALS_IT_DEFAULT  (0x00 << 
CM32181_CMD_ALS_IT_SHIFT)
+
+#define CM32181_CMD_ALS_SM_SHIFT   11
+#defineCM32181_CMD_ALS_SM_MASK (0x03 << 
CM32181_CMD_ALS_SM_SHIFT)
+#defineCM32181_CMD_ALS_SM_DEFAULT  (0x01 << 
CM32181_CMD_ALS_SM_SHIFT)
+
+#defineCM32181_MLUX_PER_BIT5   /* ALS_SM=01 IT=800ms */
+#defineCM32181_MLUX_PER_BIT_BASE_IT80  /* Based on IT=800ms */
+#defineCM32181_CALIBSCALE_RESOLUTION   1000
+#defineMLUX_PER_LUX1000
+
+enum cm32181_command {
+   CM32181_CMD_READ_RAW_LIGHT,
+};
+
+static const u16 normal_i2c[] = {
+   0x10, 0x48, I2C_CLIENT_END };
+
+static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
+   CM32181_REG_ADDR_CMD,
+};
+
+static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
+static int als_it_value[] = {25000, 5, 10, 20, 40, 80};
+
+struct cm32181_chip {
+   struct i2c_client *client;
+   struct mutex lock;
+   u16 conf_regs[CM32181_CONF_REG_NUM];
+   int calibscale;
+};
+
+static int cm32181_read_reg(struct i2c_client *client, u8 reg, u16 *reg_data)
+{
+   s32 ret;
+   int wd;
+
+   wd = 5;
+   while ((ret = i2c_smbus_read_word_data(client, reg)) < 0 &&
+   wd-- > 0)
+   ;
+
+   if (ret < 0) {
+   dev_err(>dev,
+   "Error in reading Reg.0x%02X\n", reg);
+   } else {
+   if (ret > 0x)
+   ret = 0x;
+   *reg_data = (u16)ret;
+   }
+
+   return ret;
+}
+
+static int cm32181_write_reg(struct i2c_client *client, u8 reg, u16 reg_data)
+{
+   int ret;
+   int wd;
+
+   wd = 5;
+   while ((ret = i2c_smbus_write_word_data(client, reg, reg_data)) < 0 &&
+   wd-- > 0)
+   ;
+
+   if (ret < 0) {
+   dev_err(>dev,
+   "Error in writing 0x%04X to Reg.0x%02X\n",
+   reg_data, reg);
+   }
+
+   return ret;
+}
+
+static int cm32181_reg_init(struct cm32181_chip *cm32181)
+{
+   struct 

[PATCH 1/1] Added Capella CM32181 Ambient Light Sensor IIO Driver.

2013-12-13 Thread Kevin Tsai
Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 drivers/iio/light/Kconfig   |  11 ++
 drivers/iio/light/Makefile  |   1 +
 drivers/iio/light/cm32181.c | 437 
 3 files changed, 449 insertions(+)
 create mode 100644 drivers/iio/light/cm32181.c

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index a022f27..d12b2a0 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -27,6 +27,17 @@ config APDS9300
 To compile this driver as a module, choose M here: the
 module will be called apds9300.
 
+config CM32181
+   depends on I2C
+   tristate CM32181 driver
+   help
+Say Y here if you use cm32181.
+This option enables ambient light sensor using
+Capella cm32181 device driver.
+
+To compile this driver as a module, choose M here:
+the module will be called cm32181.
+
 config CM36651
depends on I2C
tristate CM36651 driver
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index daa327f..60e35ac 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -5,6 +5,7 @@
 # When adding new entries keep the list in alphabetical order
 obj-$(CONFIG_ADJD_S311)+= adjd_s311.o
 obj-$(CONFIG_APDS9300) += apds9300.o
+obj-$(CONFIG_CM32181)  += cm32181.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/cm32181.c b/drivers/iio/light/cm32181.c
new file mode 100644
index 000..58ab1b2
--- /dev/null
+++ b/drivers/iio/light/cm32181.c
@@ -0,0 +1,437 @@
+/*
+ * Copyright (C) 2013 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 */
+#defineCM32181_REG_ADDR_CMD0x00
+#defineCM32181_REG_ADDR_ALS0x04
+#defineCM32181_REG_ADDR_STATUS 0x06
+#defineCM32181_REG_ADDR_ID 0x07
+
+/* Number of Configurable Registers */
+#define CM32181_CONF_REG_NUM   0x01
+
+/* CMD register */
+#define CM32181_CMD_ALS_ENABLE 0x00
+#define CM32181_CMD_ALS_DISABLE0x01
+#define CM32181_CMD_ALS_INT_EN 0x02
+
+#defineCM32181_CMD_ALS_IT_SHIFT6
+#defineCM32181_CMD_ALS_IT_MASK (0x0F  
CM32181_CMD_ALS_IT_SHIFT)
+#defineCM32181_CMD_ALS_IT_DEFAULT  (0x00  
CM32181_CMD_ALS_IT_SHIFT)
+
+#define CM32181_CMD_ALS_SM_SHIFT   11
+#defineCM32181_CMD_ALS_SM_MASK (0x03  
CM32181_CMD_ALS_SM_SHIFT)
+#defineCM32181_CMD_ALS_SM_DEFAULT  (0x01  
CM32181_CMD_ALS_SM_SHIFT)
+
+#defineCM32181_MLUX_PER_BIT5   /* ALS_SM=01 IT=800ms */
+#defineCM32181_MLUX_PER_BIT_BASE_IT80  /* Based on IT=800ms */
+#defineCM32181_CALIBSCALE_RESOLUTION   1000
+#defineMLUX_PER_LUX1000
+
+enum cm32181_command {
+   CM32181_CMD_READ_RAW_LIGHT,
+};
+
+static const u16 normal_i2c[] = {
+   0x10, 0x48, I2C_CLIENT_END };
+
+static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
+   CM32181_REG_ADDR_CMD,
+};
+
+static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
+static int als_it_value[] = {25000, 5, 10, 20, 40, 80};
+
+struct cm32181_chip {
+   struct i2c_client *client;
+   struct mutex lock;
+   u16 conf_regs[CM32181_CONF_REG_NUM];
+   int calibscale;
+};
+
+static int cm32181_read_reg(struct i2c_client *client, u8 reg, u16 *reg_data)
+{
+   s32 ret;
+   int wd;
+
+   wd = 5;
+   while ((ret = i2c_smbus_read_word_data(client, reg))  0 
+   wd--  0)
+   ;
+
+   if (ret  0) {
+   dev_err(client-dev,
+   Error in reading Reg.0x%02X\n, reg);
+   } else {
+   if (ret  0x)
+   ret = 0x;
+   *reg_data = (u16)ret;
+   }
+
+   return ret;
+}
+
+static int cm32181_write_reg(struct i2c_client *client, u8 reg, u16 reg_data)
+{
+   int ret;
+   int wd;
+
+   wd = 5;
+   while ((ret = i2c_smbus_write_word_data(client, reg, reg_data))  0 
+   wd--  0)
+   ;
+
+   if (ret  0) {
+   dev_err(client-dev,
+   Error in writing 0x%04X to Reg.0x%02X\n,
+   reg_data, reg);
+   }
+
+   return ret

[PATCH 1/1] Added Capella CM3218 Ambient Light Sensor IIO driver.

2013-08-06 Thread Kevin Tsai
Signed-off-by: Kevin Tsai 
---
 drivers/iio/light/Kconfig  |  10 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm3218.c | 589 +
 3 files changed, 600 insertions(+)
 create mode 100644 drivers/iio/light/cm3218.c

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 5ef1a39..dd1ea80 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -15,6 +15,16 @@ config ADJD_S311
 This driver can also be built as a module.  If so, the module
 will be called adjd_s311.
 
+config SENSORS_CM3218
+tristate "CM3218 Ambient Light Sensor"
+depends on I2C
+help
+ Say Y here if you have a Capella Micro CM3218 Ambient Light Sensor.
+
+ To compile this driver as a module, choose M here.  This module
+ will be called to 'cm3218'.  It will access ALS data via iio sysfs.
+ This is recommended.
+
 config SENSORS_LM3533
tristate "LM3533 ambient light sensor"
depends on MFD_LM3533
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 040d9c7..c4ebe37 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -3,6 +3,7 @@
 #
 
 obj-$(CONFIG_ADJD_S311)+= adjd_s311.o
+obj-$(CONFIG_SENSORS_CM3218)+= cm3218.o
 obj-$(CONFIG_SENSORS_LM3533)   += lm3533-als.o
 obj-$(CONFIG_SENSORS_TSL2563)  += tsl2563.o
 obj-$(CONFIG_VCNL4000) += vcnl4000.o
diff --git a/drivers/iio/light/cm3218.c b/drivers/iio/light/cm3218.c
new file mode 100644
index 000..1989fbb
--- /dev/null
+++ b/drivers/iio/light/cm3218.c
@@ -0,0 +1,589 @@
+/*
+ * A iio driver for CM3218 Ambient Light Sensor.
+ *
+ * IIO driver for monitoring ambient light intensity in lux.
+ *
+ * Copyright (c) 2013, Capella Microsystems Inc.
+ *
+ * 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 
+#include 
+#include 
+#include 
+#include 
+
+#if 0  /* ChromeOS still uses old API */
+#include "../iio.h"
+#include "../sysfs.h"
+#defineIIO_DEVICE_ALLOCiio_allocate_device
+#defineIIO_DEVICE_FREE iio_free_device
+#else
+#include 
+#include 
+#defineIIO_DEVICE_ALLOCiio_device_alloc
+#defineIIO_DEVICE_FREE iio_device_free
+#endif /* 0 */
+
+/*
+ * SMBus ARA address
+ */
+#defineCM3218_ADDR_ARA 0x0C
+
+/*
+ * CM3218 CMD Registers
+ */
+#defineCM3218_REG_ADDR_CMD 0x00
+#defineCM3218_CMD_ALS_SD   0x0001
+#defineCM3218_CMD_ALS_INT_EN   0x0002
+#defineCM3218_CMD_ALS_IT_SHIFT 6
+#defineCM3218_CMD_ALS_IT_MASK  (3 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_05T   (0 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_1T(1 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_2T(2 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_4T(3 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_DEFAULT_CMD  (CM3218_CMD_ALS_IT_1T)
+
+#defineCM3218_REG_ADDR_ALS_WH  0x01
+#defineCM3218_DEFAULT_ALS_WH   0x
+
+#defineCM3218_REG_ADDR_ALS_WL  0x02
+#defineCM3218_DEFAULT_ALS_WL   0x
+
+#defineCM3218_REG_ADDR_ALS 0x04
+
+#defineCM3218_REG_ADDR_STATUS  0x06
+
+#defineCM3218_REG_ADDR_ID  0x07
+
+/*
+ * Software Parameters
+ */
+#defineCM3218_MAX_CACHE_REGS   (0x03+1)/* Reg.0x00 to 
0x03 */
+
+/*
+ * Features
+ */
+#defineCM3218_DEBUG
+
+static const unsigned short normal_i2c[] = {
+   0x10, 0x48, I2C_CLIENT_END };
+
+struct cm3218_chip {
+   struct i2c_client   *client;
+   struct mutexlock;
+   unsigned intlensfactor;
+   boolsuspended;
+   u16 reg_cache[CM3218_MAX_CACHE_REGS];
+};
+
+static int cm3218_read_ara(struct i2c_client *client)
+{
+   int status;
+   unsigned short addr;
+
+   addr = client->addr;
+   client->addr = CM3218_ADDR_ARA;
+   status = i2c_smbus_read_byte(client);
+

[PATCH 1/1] Added Capella CM3218 Ambient Light Sensor IIO driver.

2013-08-06 Thread Kevin Tsai
Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 drivers/iio/light/Kconfig  |  10 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/cm3218.c | 589 +
 3 files changed, 600 insertions(+)
 create mode 100644 drivers/iio/light/cm3218.c

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 5ef1a39..dd1ea80 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -15,6 +15,16 @@ config ADJD_S311
 This driver can also be built as a module.  If so, the module
 will be called adjd_s311.
 
+config SENSORS_CM3218
+tristate CM3218 Ambient Light Sensor
+depends on I2C
+help
+ Say Y here if you have a Capella Micro CM3218 Ambient Light Sensor.
+
+ To compile this driver as a module, choose M here.  This module
+ will be called to 'cm3218'.  It will access ALS data via iio sysfs.
+ This is recommended.
+
 config SENSORS_LM3533
tristate LM3533 ambient light sensor
depends on MFD_LM3533
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 040d9c7..c4ebe37 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -3,6 +3,7 @@
 #
 
 obj-$(CONFIG_ADJD_S311)+= adjd_s311.o
+obj-$(CONFIG_SENSORS_CM3218)+= cm3218.o
 obj-$(CONFIG_SENSORS_LM3533)   += lm3533-als.o
 obj-$(CONFIG_SENSORS_TSL2563)  += tsl2563.o
 obj-$(CONFIG_VCNL4000) += vcnl4000.o
diff --git a/drivers/iio/light/cm3218.c b/drivers/iio/light/cm3218.c
new file mode 100644
index 000..1989fbb
--- /dev/null
+++ b/drivers/iio/light/cm3218.c
@@ -0,0 +1,589 @@
+/*
+ * A iio driver for CM3218 Ambient Light Sensor.
+ *
+ * IIO driver for monitoring ambient light intensity in lux.
+ *
+ * Copyright (c) 2013, Capella Microsystems Inc.
+ *
+ * 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/module.h
+#include linux/i2c.h
+#include linux/err.h
+#include linux/mutex.h
+#include linux/slab.h
+
+#if 0  /* ChromeOS still uses old API */
+#include ../iio.h
+#include ../sysfs.h
+#defineIIO_DEVICE_ALLOCiio_allocate_device
+#defineIIO_DEVICE_FREE iio_free_device
+#else
+#include linux/iio/iio.h
+#include linux/iio/sysfs.h
+#defineIIO_DEVICE_ALLOCiio_device_alloc
+#defineIIO_DEVICE_FREE iio_device_free
+#endif /* 0 */
+
+/*
+ * SMBus ARA address
+ */
+#defineCM3218_ADDR_ARA 0x0C
+
+/*
+ * CM3218 CMD Registers
+ */
+#defineCM3218_REG_ADDR_CMD 0x00
+#defineCM3218_CMD_ALS_SD   0x0001
+#defineCM3218_CMD_ALS_INT_EN   0x0002
+#defineCM3218_CMD_ALS_IT_SHIFT 6
+#defineCM3218_CMD_ALS_IT_MASK  (3  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_05T   (0  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_1T(1  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_2T(2  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_4T(3  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_DEFAULT_CMD  (CM3218_CMD_ALS_IT_1T)
+
+#defineCM3218_REG_ADDR_ALS_WH  0x01
+#defineCM3218_DEFAULT_ALS_WH   0x
+
+#defineCM3218_REG_ADDR_ALS_WL  0x02
+#defineCM3218_DEFAULT_ALS_WL   0x
+
+#defineCM3218_REG_ADDR_ALS 0x04
+
+#defineCM3218_REG_ADDR_STATUS  0x06
+
+#defineCM3218_REG_ADDR_ID  0x07
+
+/*
+ * Software Parameters
+ */
+#defineCM3218_MAX_CACHE_REGS   (0x03+1)/* Reg.0x00 to 
0x03 */
+
+/*
+ * Features
+ */
+#defineCM3218_DEBUG
+
+static const unsigned short normal_i2c[] = {
+   0x10, 0x48, I2C_CLIENT_END };
+
+struct cm3218_chip {
+   struct i2c_client   *client;
+   struct mutexlock;
+   unsigned intlensfactor;
+   boolsuspended;
+   u16 reg_cache[CM3218_MAX_CACHE_REGS];
+};
+
+static int cm3218_read_ara(struct i2c_client *client)
+{
+   int status;
+   unsigned short addr;
+
+   addr = client-addr;
+   client-addr = CM3218_ADDR_ARA;
+   status

[PATCH 1/1] Added Capella CM3218 Ambient Light Sensor IIO Driver.

2013-07-03 Thread Kevin Tsai

Signed-off-by: Kevin Tsai 
---
 drivers/staging/iio/light/Kconfig  |   10 +
 drivers/staging/iio/light/Makefile |1 +
 drivers/staging/iio/light/cm3218.c |  581 
 3 files changed, 592 insertions(+)
 create mode 100644 drivers/staging/iio/light/cm3218.c

diff --git a/drivers/staging/iio/light/Kconfig 
b/drivers/staging/iio/light/Kconfig
index ca8d6e6..647af0c 100644
--- a/drivers/staging/iio/light/Kconfig
+++ b/drivers/staging/iio/light/Kconfig
@@ -40,4 +40,14 @@ config TSL2x7x
 tmd2672, tsl2772, tmd2772 devices.
 Provides iio_events and direct access via sysfs.
 
+config SENSORS_CM3218
+tristate "CM3218 Ambient Light Sensor"
+depends on I2C
+help
+ Say Y here if you have a Capella Micro CM3218 Ambient Light Sensor.
+
+ To compile this driver as a module, choose M here.  This module
+ will be called to 'cm3218'.  It will access ALS data via iio sysfs.
+ This is recommended.
+
 endmenu
diff --git a/drivers/staging/iio/light/Makefile 
b/drivers/staging/iio/light/Makefile
index 9960fdf..63020ab 100644
--- a/drivers/staging/iio/light/Makefile
+++ b/drivers/staging/iio/light/Makefile
@@ -6,3 +6,4 @@ obj-$(CONFIG_SENSORS_ISL29018)  += isl29018.o
 obj-$(CONFIG_SENSORS_ISL29028) += isl29028.o
 obj-$(CONFIG_TSL2583)  += tsl2583.o
 obj-$(CONFIG_TSL2x7x)  += tsl2x7x_core.o
+obj-$(CONFIG_SENSORS_CM3218)+= cm3218.o
diff --git a/drivers/staging/iio/light/cm3218.c 
b/drivers/staging/iio/light/cm3218.c
new file mode 100644
index 000..9c2584d
--- /dev/null
+++ b/drivers/staging/iio/light/cm3218.c
@@ -0,0 +1,581 @@
+/*
+ * A iio driver for CM3218 Ambient Light Sensor.
+ *
+ * IIO driver for monitoring ambient light intensity in lux.
+ *
+ * Copyright (c) 2013, Capella Microsystems Inc.
+ *
+ * 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/*
+ * SMBus ARA address
+ */
+#defineCM3218_ADDR_ARA 0x0C
+
+/*
+ * CM3218 CMD Registers
+ */
+#defineCM3218_REG_ADDR_CMD 0x00
+#defineCM3218_CMD_ALS_SD   0x0001
+#defineCM3218_CMD_ALS_INT_EN   0x0002
+#defineCM3218_CMD_ALS_IT_SHIFT 6
+#defineCM3218_CMD_ALS_IT_MASK  (3 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_05T   (0 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_1T(1 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_2T(2 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_4T(3 << CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_DEFAULT_CMD  (CM3218_CMD_ALS_IT_1T)
+
+#defineCM3218_REG_ADDR_ALS_WH  0x01
+#defineCM3218_DEFAULT_ALS_WH   0x
+
+#defineCM3218_REG_ADDR_ALS_WL  0x02
+#defineCM3218_DEFAULT_ALS_WL   0x
+
+#defineCM3218_REG_ADDR_ALS 0x04
+
+#defineCM3218_REG_ADDR_STATUS  0x06
+
+#defineCM3218_REG_ADDR_ID  0x07
+
+/*
+ * Software Parameters
+ */
+#defineCM3218_MAX_CACHE_REGS   (0x03+1)/* Reg.0x00 to 
0x03 */
+
+/*
+ * Features
+ */
+#defineCM3218_DEBUG
+
+static const unsigned short normal_i2c[] = {
+   0x10, 0x48, I2C_CLIENT_END };
+
+struct cm3218_chip {
+   struct i2c_client   *client;
+   struct mutexlock;
+   unsigned intlensfactor;
+   boolsuspended;
+   u16 reg_cache[CM3218_MAX_CACHE_REGS];
+};
+
+static int cm3218_read_ara(struct i2c_client *client)
+{
+   int status;
+   unsigned short addr;
+
+   addr = client->addr;
+   client->addr = CM3218_ADDR_ARA;
+   status = i2c_smbus_read_byte(client);
+   client->addr = addr;
+
+   if (status < 0)
+   return -ENODEV;
+
+   return 0;
+}
+
+static int cm3218_write(struct i2c_client *client, u8 reg, u16 value)
+{
+   u16 regval;
+   int ret;
+   struct cm3218_chip *chip = iio_priv(i2c_get_clientdata(client));
+
+#ifdef CM3218_DEBUG
+   dev_err(>dev,
+   &quo

[PATCH 1/1] Added Capella CM3218 Ambient Light Sensor IIO Driver.

2013-07-03 Thread Kevin Tsai

Signed-off-by: Kevin Tsai kt...@capellamicro.com
---
 drivers/staging/iio/light/Kconfig  |   10 +
 drivers/staging/iio/light/Makefile |1 +
 drivers/staging/iio/light/cm3218.c |  581 
 3 files changed, 592 insertions(+)
 create mode 100644 drivers/staging/iio/light/cm3218.c

diff --git a/drivers/staging/iio/light/Kconfig 
b/drivers/staging/iio/light/Kconfig
index ca8d6e6..647af0c 100644
--- a/drivers/staging/iio/light/Kconfig
+++ b/drivers/staging/iio/light/Kconfig
@@ -40,4 +40,14 @@ config TSL2x7x
 tmd2672, tsl2772, tmd2772 devices.
 Provides iio_events and direct access via sysfs.
 
+config SENSORS_CM3218
+tristate CM3218 Ambient Light Sensor
+depends on I2C
+help
+ Say Y here if you have a Capella Micro CM3218 Ambient Light Sensor.
+
+ To compile this driver as a module, choose M here.  This module
+ will be called to 'cm3218'.  It will access ALS data via iio sysfs.
+ This is recommended.
+
 endmenu
diff --git a/drivers/staging/iio/light/Makefile 
b/drivers/staging/iio/light/Makefile
index 9960fdf..63020ab 100644
--- a/drivers/staging/iio/light/Makefile
+++ b/drivers/staging/iio/light/Makefile
@@ -6,3 +6,4 @@ obj-$(CONFIG_SENSORS_ISL29018)  += isl29018.o
 obj-$(CONFIG_SENSORS_ISL29028) += isl29028.o
 obj-$(CONFIG_TSL2583)  += tsl2583.o
 obj-$(CONFIG_TSL2x7x)  += tsl2x7x_core.o
+obj-$(CONFIG_SENSORS_CM3218)+= cm3218.o
diff --git a/drivers/staging/iio/light/cm3218.c 
b/drivers/staging/iio/light/cm3218.c
new file mode 100644
index 000..9c2584d
--- /dev/null
+++ b/drivers/staging/iio/light/cm3218.c
@@ -0,0 +1,581 @@
+/*
+ * A iio driver for CM3218 Ambient Light Sensor.
+ *
+ * IIO driver for monitoring ambient light intensity in lux.
+ *
+ * Copyright (c) 2013, Capella Microsystems Inc.
+ *
+ * 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/module.h
+#include linux/i2c.h
+#include linux/err.h
+#include linux/mutex.h
+#include linux/delay.h
+#include linux/slab.h
+#include linux/iio/iio.h
+#include linux/iio/sysfs.h
+
+/*
+ * SMBus ARA address
+ */
+#defineCM3218_ADDR_ARA 0x0C
+
+/*
+ * CM3218 CMD Registers
+ */
+#defineCM3218_REG_ADDR_CMD 0x00
+#defineCM3218_CMD_ALS_SD   0x0001
+#defineCM3218_CMD_ALS_INT_EN   0x0002
+#defineCM3218_CMD_ALS_IT_SHIFT 6
+#defineCM3218_CMD_ALS_IT_MASK  (3  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_05T   (0  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_1T(1  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_2T(2  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_CMD_ALS_IT_4T(3  CM3218_CMD_ALS_IT_SHIFT)
+#defineCM3218_DEFAULT_CMD  (CM3218_CMD_ALS_IT_1T)
+
+#defineCM3218_REG_ADDR_ALS_WH  0x01
+#defineCM3218_DEFAULT_ALS_WH   0x
+
+#defineCM3218_REG_ADDR_ALS_WL  0x02
+#defineCM3218_DEFAULT_ALS_WL   0x
+
+#defineCM3218_REG_ADDR_ALS 0x04
+
+#defineCM3218_REG_ADDR_STATUS  0x06
+
+#defineCM3218_REG_ADDR_ID  0x07
+
+/*
+ * Software Parameters
+ */
+#defineCM3218_MAX_CACHE_REGS   (0x03+1)/* Reg.0x00 to 
0x03 */
+
+/*
+ * Features
+ */
+#defineCM3218_DEBUG
+
+static const unsigned short normal_i2c[] = {
+   0x10, 0x48, I2C_CLIENT_END };
+
+struct cm3218_chip {
+   struct i2c_client   *client;
+   struct mutexlock;
+   unsigned intlensfactor;
+   boolsuspended;
+   u16 reg_cache[CM3218_MAX_CACHE_REGS];
+};
+
+static int cm3218_read_ara(struct i2c_client *client)
+{
+   int status;
+   unsigned short addr;
+
+   addr = client-addr;
+   client-addr = CM3218_ADDR_ARA;
+   status = i2c_smbus_read_byte(client);
+   client-addr = addr;
+
+   if (status  0)
+   return -ENODEV;
+
+   return 0;
+}
+
+static int cm3218_write(struct i2c_client *client, u8 reg, u16 value)
+{
+   u16 regval;
+   int ret;
+   struct cm3218_chip *chip = iio_priv(i2c_get_clientdata(client));
+
+#ifdef