diff options
| author | mattis fjallstrom <mattis@acm.org> | 2015-04-28 12:00:44 -0700 |
|---|---|---|
| committer | mattis fjallstrom <mattis@acm.org> | 2015-04-28 12:11:36 -0700 |
| commit | a8b4c3e436848b4d85b7189785912e6e63ec3451 (patch) | |
| tree | c36eae05f72877ea221e2ab03e44bea117b481be | |
| parent | dc2be70fdf8fdfaaba8303755615d6d04a330520 (diff) | |
| download | olio-linux-3.10-a8b4c3e436848b4d85b7189785912e6e63ec3451.tar.xz olio-linux-3.10-a8b4c3e436848b4d85b7189785912e6e63ec3451.zip | |
Adding driver for Cappellas cm3391 ALS, and setting up boardfile and configuration to make use of it.
Change-Id: I29b001f88bffe9a74c712bcb4bb12d2e56e1f7d6
| -rw-r--r-- | arch/arm/configs/omap3_h1_defconfig | 2 | ||||
| -rw-r--r-- | arch/arm/mach-omap2/board-omap3h1.c | 2 | ||||
| -rw-r--r-- | drivers/iio/light/Kconfig | 25 | ||||
| -rw-r--r-- | drivers/iio/light/Makefile | 2 | ||||
| -rw-r--r-- | drivers/iio/light/cm3391.c | 784 |
5 files changed, 813 insertions, 2 deletions
diff --git a/arch/arm/configs/omap3_h1_defconfig b/arch/arm/configs/omap3_h1_defconfig index 46d74964197..866a78d904e 100644 --- a/arch/arm/configs/omap3_h1_defconfig +++ b/arch/arm/configs/omap3_h1_defconfig @@ -2494,7 +2494,7 @@ CONFIG_IIO_CONSUMERS_PER_TRIGGER=2 # # CONFIG_ADJD_S311 is not set # CONFIG_CM32181 is not set -# CONFIG_CM3391 is not set +CONFIG_CM3391=y # CONFIG_SENSORS_TSL2563 is not set # CONFIG_VCNL4000 is not set diff --git a/arch/arm/mach-omap2/board-omap3h1.c b/arch/arm/mach-omap2/board-omap3h1.c index df2d785e59e..4e01a52f9b5 100644 --- a/arch/arm/mach-omap2/board-omap3h1.c +++ b/arch/arm/mach-omap2/board-omap3h1.c @@ -737,7 +737,7 @@ static struct i2c_board_info __initdata omap3h1_i2c2_board_info[] = { .platform_data = &mxt_data, }, { - I2C_BOARD_INFO("cm32181", 0x10), + I2C_BOARD_INFO("cm3391", 0x10), }, #ifdef CONFIG_MACH_OMAP3_H1_EVT1 }; diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig index 5ef1a396e0c..e86ccb49d9d 100644 --- a/drivers/iio/light/Kconfig +++ b/drivers/iio/light/Kconfig @@ -32,6 +32,31 @@ config SENSORS_LM3533 changes. The ALS-control output values can be set per zone for the three current output channels. +config CM32181 + tristate "CM32181 driver" + depends on I2C + 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 CM3391 + tristate "CM3391 driver" + depends on I2C + help + Say Y here if you use cm3391. + + This option enables ambient light sensor using + Capella cm3391 device driver. + + To compile this driver as a module, choose M here: + the module will be called cm3391. + + config SENSORS_TSL2563 tristate "TAOS TSL2560, TSL2561, TSL2562 and TSL2563 ambient light sensors" depends on I2C diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile index 040d9c75f8e..fdf3b87e1f1 100644 --- a/drivers/iio/light/Makefile +++ b/drivers/iio/light/Makefile @@ -7,3 +7,5 @@ obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o obj-$(CONFIG_VCNL4000) += vcnl4000.o obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o +obj-$(CONFIG_CM32181) += cm32181.o +obj-$(CONFIG_CM3391) += cm3391.o
\ No newline at end of file diff --git a/drivers/iio/light/cm3391.c b/drivers/iio/light/cm3391.c new file mode 100644 index 00000000000..ed2d84a61fe --- /dev/null +++ b/drivers/iio/light/cm3391.c @@ -0,0 +1,784 @@ +/* + * Copyright (C) 2014-2015 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 version 2, as published + * by the Free Software Foundation. + * + * Special thanks Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com> + * help to add ACPI support. + * + */ + +#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> + +#ifdef CONFIG_ACPI +#include <linux/acpi.h> +#endif /* CONFIG_ACPI */ + +#define OLDAPI + +/* Registers Address */ +#define CM3391_REG_ADDR_CMD 0x00 +#define CM3391_REG_ADDR_TEST 0x01 +#define CM3391_REG_ADDR_WH 0x02 +#define CM3391_REG_ADDR_WL 0x03 +#define CM3391_REG_ADDR_RED 0x08 +#define CM3391_REG_ADDR_ALS 0x09 +#define CM3391_REG_ADDR_BLUE 0x0A +#define CM3391_REG_ADDR_CLEAR 0x0B +#define CM3391_REG_ADDR_ID 0x0C +#define CM3391_REG_ADDR_STATUS 0x0D + +/* Number of Configurable Registers */ +#define CM3391_CONF_REG_NUM 16 + +/* CMD register */ +#define CM3391_CMD_CS_DISABLE BIT(0) +#define CM3391_CMD_CS_INT_EN BIT(8) + +#define CM3391_CMD_CS_PERS_SHIFT 10 +#define CM3391_CMD_CS_PERS_MASK (0x03 << CM3391_CMD_CS_PERS_SHIFT) +#define CM3391_CMD_CS_PERS_DEFAULT (0x01 << CM3391_CMD_CS_PERS_SHIFT) + +#define CM3391_CMD_CS_IT_SHIFT 4 +#define CM3391_CMD_CS_IT_MASK (0x07 << CM3391_CMD_CS_IT_SHIFT) +#define CM3391_CMD_CS_IT_DEFAULT (0x01 << CM3391_CMD_CS_IT_SHIFT) + +#define CM3391_CMD_CS_HS_SHIFT 3 +#define CM3391_CMD_CS_HS_MASK (0x01 << CM3391_CMD_CS_HS_SHIFT) +#define CM3391_CMD_CS_HS_DEFAULT (0x00 << CM3391_CMD_CS_HS_SHIFT) + +#define CM3391_CMD_DEFAULT (\ + CM3391_CMD_CS_PERS_DEFAULT |\ + CM3391_CMD_CS_IT_DEFAULT |\ + CM3391_CMD_CS_HS_DEFAULT) + +#define CM3391_WH_DEFAULT 0xFFFF +#define CM3391_WL_DEFAULT 0x0000 + +#define CM3391_CALIBSCALE_DEFAULT 100000 +#define CM3391_CALIBSCALE_RESOLUTION 100000 +#define CM3391_MLUX_PER_LUX 1000 +#define CM3391_THRESHOLD_PERCENT 10 /* 10 percent */ + +#define CM3391_MLUX_PER_BIT_DEFAULT 550 +#define CM3391_MLUX_PER_BIT_BASE_IT 100000 +static const int CM3391_cs_it_bits[] = { 0, 1, 2, 3, 4}; +static const int CM3391_cs_it_values[] = { + 50000, 100000, 200000, 400000, 800000}; + +struct cm3391_cs_info { + u32 id; + int regs_bmp; + int calibscale; + int mlux_per_bit; + int mlux_per_bit_base_it; + const int *cs_it_bits; + const int *cs_it_values; + const int num_cs_it; + int als_raw; +}; + +static struct cm3391_cs_info cm3391_cs_info_default = { + .id = 3391, + .regs_bmp = 0x0F, + .calibscale = CM3391_CALIBSCALE_DEFAULT, + .mlux_per_bit = CM3391_MLUX_PER_BIT_DEFAULT, + .mlux_per_bit_base_it = CM3391_MLUX_PER_BIT_BASE_IT, + .cs_it_bits = CM3391_cs_it_bits, + .cs_it_values = CM3391_cs_it_values, + .num_cs_it = ARRAY_SIZE(CM3391_cs_it_bits), +}; + +struct cm3391_chip { + struct i2c_client *client; + struct mutex lock; + u16 conf_regs[CM3391_CONF_REG_NUM]; + struct cm3391_cs_info *cs_info; +}; + +static int cm3391_get_lux(struct cm3391_chip *chip); +static int cm3391_threshold_update(struct cm3391_chip *chip, int percent); +static int cm3391_read_cs_it(struct cm3391_chip *chip, int *val2); + +/** + * cm3391_interrupt_config() - Enable/Disable CM3391 interrupt + * @chip: pointer of struct cm3391. + * @enable: 0 to disable; otherwise to enable + * + * Config CM3391 interrupt control bit. + * + * Return: 0 for success; otherwise for error code. + */ +static int cm3391_interrupt_config(struct cm3391_chip *chip, int enable) +{ + struct i2c_client *client = chip->client; + struct cm3391_cs_info *cs_info = chip->cs_info; + int status; + + if (!cs_info) + return -ENODEV; + + /* Force to clean interrupt */ + status = i2c_smbus_read_word_data(client, + CM3391_REG_ADDR_STATUS); + if (status < 0) + return -ENODEV; + + if (enable) + chip->conf_regs[CM3391_REG_ADDR_CMD] |= + CM3391_CMD_CS_INT_EN; + else + chip->conf_regs[CM3391_REG_ADDR_CMD] &= + ~CM3391_CMD_CS_INT_EN; + + status = i2c_smbus_write_word_data(client, CM3391_REG_ADDR_CMD, + chip->conf_regs[CM3391_REG_ADDR_CMD]); + + if (status < 0) + return -ENODEV; + + return status; +} + +#ifdef CONFIG_ACPI +/** + * cm3391_acpi_get_cpm_info() - Get CPM object from ACPI + * @client pointer of struct i2c_client. + * @obj_name pointer 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 cm3391_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; +} +#endif /* CONFIG_ACPI */ + +/** + * cm3391_reg_init() - Initialize CM3391 registers + * @chip: pointer of struct cm3391. + * + * Initialize CM3391 color sensor register to default values. + * + Return: 0 for success; otherwise for error code. + */ +static int cm3391_reg_init(struct cm3391_chip *chip) +{ + struct i2c_client *client = chip->client; + int i; + s32 ret; + struct cm3391_cs_info *cs_info; +#ifdef CONFIG_ACPI + int cpm_elem_count; + u64 cpm_elems[20]; +#endif /* CONFIG_ACPI */ + + + /* Default device */ + cs_info = chip->cs_info = &cm3391_cs_info_default; + chip->conf_regs[CM3391_REG_ADDR_CMD] = CM3391_CMD_DEFAULT; + chip->conf_regs[CM3391_REG_ADDR_WH] = CM3391_WH_DEFAULT; + chip->conf_regs[CM3391_REG_ADDR_WL] = CM3391_WL_DEFAULT; + + /* Disable interrupt */ + cm3391_interrupt_config(chip, 0); + + /* Disable Test Mode */ + i2c_smbus_write_word_data(client, CM3391_REG_ADDR_TEST, 0x0000); + + /* Disable device */ + i2c_smbus_write_word_data(client, CM3391_REG_ADDR_CMD, + CM3391_CMD_CS_DISABLE); + + /* Identify device */ + ret = i2c_smbus_read_word_data(client, CM3391_REG_ADDR_ID); + if (ret < 0) + return ret; + + if ((ret & 0xFF) != 0x91) + return -ENODEV; + +#ifdef CONFIG_ACPI +#error ACPI is enabled, strange! OLIO MFJ + + if (ACPI_HANDLE(&client->dev)) { + /* Load from ACPI */ + cpm_elem_count = cm3391_acpi_get_cpm_info(client, "CPM0", + ARRAY_SIZE(cpm_elems), + cpm_elems); + if (cpm_elem_count > 0) { + int header_num = 3; + int reg_num = cpm_elem_count - header_num; + + cs_info->id = cpm_elems[0]; + cs_info->regs_bmp = cpm_elems[2]; + for (i = 0; i < reg_num; i++) + if (cs_info->regs_bmp & (1<<i)) + chip->conf_regs[i] = + cpm_elems[header_num+i]; + } + + cpm_elem_count = cm3391_acpi_get_cpm_info(client, "CPM1", + ARRAY_SIZE(cpm_elems), + cpm_elems); + if (cpm_elem_count > 0) { + cs_info->mlux_per_bit = (int)cpm_elems[0] / 100; + cs_info->calibscale = (int)cpm_elems[1]; + } + } +#endif /* CONFIG_ACPI */ + + /* Force to disable interrupt */ + chip->conf_regs[CM3391_REG_ADDR_CMD] &= ~CM3391_CMD_CS_INT_EN; + + /* Initialize registers */ + for (i = 0; i < CM3391_CONF_REG_NUM; i++) { + if (cs_info->regs_bmp & (1<<i)) { + ret = i2c_smbus_write_word_data(client, i, + chip->conf_regs[i]); + if (ret < 0) + return ret; + } + } + + return 0; +} + +/** + * cm3391_read_cs_it() - Get sensor integration time (ms) + * @chip: pointer of struct cm3391 + * @val2: pointer of int to load the cs_it value. + * + * Report the current integration time in milliseconds. + * + * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL. + */ +static int cm3391_read_cs_it(struct cm3391_chip *chip, int *val2) +{ + struct cm3391_cs_info *cs_info = chip->cs_info; + u16 cs_it; + int i; + + cs_it = chip->conf_regs[CM3391_REG_ADDR_CMD]; + cs_it &= CM3391_CMD_CS_IT_MASK; + cs_it >>= CM3391_CMD_CS_IT_SHIFT; + for (i = 0; i < cs_info->num_cs_it; i++) { + if (cs_it == cs_info->cs_it_bits[i]) { + *val2 = cs_info->cs_it_values[i]; + return IIO_VAL_INT_PLUS_MICRO; + } + } + + return -EINVAL; +} + +#ifndef OLDAPI +/** + * cm3391_write_cs_it() - Write sensor integration time + * @chip: pointer of struct cm3391. + * @val: integration time in milliseconds. + * + * Convert integration time (ms) to sensor value. + * + * Return: i2c_smbus_write_word_data command return value. + */ +static int cm3391_write_cs_it(struct cm3391_chip *chip, int val) +{ + struct i2c_client *client = chip->client; + struct cm3391_cs_info *cs_info = chip->cs_info; + u16 cs_it; + int ret, i; + + for (i = 0; i < cs_info->num_cs_it; i++) + if (val <= cs_info->cs_it_values[i]) + break; + if (i >= cs_info->num_cs_it) + i = cs_info->num_cs_it - 1; + + cs_it = cs_info->cs_it_bits[i]; + cs_it <<= CM3391_CMD_CS_IT_SHIFT; + + mutex_lock(&chip->lock); + chip->conf_regs[CM3391_REG_ADDR_CMD] &= + ~CM3391_CMD_CS_IT_MASK; + chip->conf_regs[CM3391_REG_ADDR_CMD] |= + cs_it; + ret = i2c_smbus_write_word_data(client, CM3391_REG_ADDR_CMD, + chip->conf_regs[CM3391_REG_ADDR_CMD]); + mutex_unlock(&chip->lock); + + return ret; +} +#endif /* !OLDAPI */ + +/** + * cm3391_get_lux() - report current lux value + * @chip: pointer of struct cm3391. + * + * Convert sensor raw data to lux. It depends on integration + * time and calibscale variable. + * + * Return: Positive value is lux, otherwise is error code. + */ +static int cm3391_get_lux(struct cm3391_chip *chip) +{ + struct i2c_client *client = chip->client; + struct cm3391_cs_info *cs_info = chip->cs_info; + int ret; + int cs_it; + u64 tmp; + + /* Calculate mlux per bit based on cs_it */ + ret = cm3391_read_cs_it(chip, &cs_it); + if (ret < 0) + return -EINVAL; + tmp = (__force u64)cs_info->mlux_per_bit; + tmp *= cs_info->mlux_per_bit_base_it; + tmp = div_u64(tmp, cs_it); + + /* Get als_raw */ + if (!(chip->conf_regs[CM3391_REG_ADDR_CMD] & CM3391_CMD_CS_INT_EN)) + cs_info->als_raw = i2c_smbus_read_word_data( + client, + CM3391_REG_ADDR_ALS); + if (cs_info->als_raw < 0) + return cs_info->als_raw; + + tmp *= cs_info->als_raw; + tmp *= cs_info->calibscale; + tmp = div_u64(tmp, CM3391_CALIBSCALE_RESOLUTION); + tmp = div_u64(tmp, CM3391_MLUX_PER_LUX); + + if (tmp > 0xFFFF) + tmp = 0xFFFF; + + return (int)tmp; +} + +static int cm3391_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct cm3391_chip *chip = iio_priv(indio_dev); + struct cm3391_cs_info *cs_info = chip->cs_info; + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + ret = i2c_smbus_read_word_data(chip->client, chan->address); + if (ret < 0) { + return ret; + } + *val = ret; + return IIO_VAL_INT; + case IIO_CHAN_INFO_PROCESSED: + ret = cm3391_get_lux(chip); + if (ret < 0) + return ret; + *val = ret; + return IIO_VAL_INT; + case IIO_CHAN_INFO_CALIBSCALE: + *val = cs_info->calibscale; + return IIO_VAL_INT; +#ifndef OLDAPI + case IIO_CHAN_INFO_INT_TIME: + *val = 0; + ret = cm3391_read_cs_it(chip, val2); + return ret; +#endif /* !OLDAPI */ + } + + return -EINVAL; +} + +static int cm3391_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct cm3391_chip *chip = iio_priv(indio_dev); + struct cm3391_cs_info *cs_info = chip->cs_info; +#ifndef OLDAPI + long ms; +#endif /* !OLDAPI */ + + switch (mask) { + case IIO_CHAN_INFO_CALIBSCALE: + cs_info->calibscale = val; + return val; +#ifndef OLDAPI + case IIO_CHAN_INFO_INT_TIME: + ms = val * 1000000 + val2; + return cm3391_write_cs_it(chip, (int)ms); +#endif /* !OLDAPI */ + } + + return -EINVAL; +} + +/** + * cm3391_get_it_available() - Get available IT value + * @dev: pointer of struct device. + * @attr: pointer of struct device_attribute. + * @buf: pointer of return string buffer. + * + * Display the available integration time in milliseconds. + * + * Return: string length. + */ +static ssize_t cm3391_get_it_available(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct cm3391_chip *chip = iio_priv(dev_to_iio_dev(dev)); + struct cm3391_cs_info *cs_info = chip->cs_info; + int i, len; + + for (i = 0, len = 0; i < cs_info->num_cs_it; i++) + len += scnprintf(buf + len, PAGE_SIZE - len, "%u.%06u ", + cs_info->cs_it_values[i]/1000000, + cs_info->cs_it_values[i]%1000000); + return len + scnprintf(buf + len, PAGE_SIZE - len, "\n"); +} + +/** + * cm3391_threshold_update() - Update the threshold registers. + * @chip: pointer of struct cm3391_chip. + * @percent: +/- percent. + * + * Based on the current ALS value, update the hi and low threshold registers. + * + * Return: 0 for success; otherwise for error code. + */ +static int cm3391_threshold_update(struct cm3391_chip *chip, int percent) +{ + struct i2c_client *client = chip->client; + struct cm3391_cs_info *cs_info = chip->cs_info; + int ret; + int wh, wl; + + ret = cs_info->als_raw = i2c_smbus_read_word_data(client, + CM3391_REG_ADDR_ALS); + if (ret < 0) + return ret; + + wh = wl = ret; + ret *= percent; + ret /= 100; + if (ret < 1) + ret = 1; + wh += ret; + wl -= ret; + if (wh > 65535) + wh = 65535; + if (wl < 0) + wl = 0; + + chip->conf_regs[CM3391_REG_ADDR_WH] = wh; + ret = i2c_smbus_write_word_data( + client, + CM3391_REG_ADDR_WH, + chip->conf_regs[CM3391_REG_ADDR_WH]); + if (ret < 0) + return ret; + + chip->conf_regs[CM3391_REG_ADDR_WL] = wl; + ret = i2c_smbus_write_word_data( + client, + CM3391_REG_ADDR_WL, + chip->conf_regs[CM3391_REG_ADDR_WL]); + + return ret; +} + +/** + * cm3391_event_handler() - Interrupt handling routine. + * @irq: irq number. + * @private: pointer of void. + * + * Clean interrupt and reset threshold registers. + * + * Return: IRQ_HANDLED. + */ +static irqreturn_t cm3391_event_handler(int irq, void *private) +{ + struct iio_dev *dev_info = private; + struct cm3391_chip *chip = iio_priv(dev_info); + int ret; + + mutex_lock(&chip->lock); + + /* Disable interrupt */ + ret = cm3391_interrupt_config(chip, 0); + if (ret < 0) + goto error_handler_unlock; + + /* Update Hi/Lo windows */ + ret = cm3391_threshold_update(chip, CM3391_THRESHOLD_PERCENT); + if (ret < 0) + goto error_handler_unlock; + + /* Enable interrupt */ + cm3391_interrupt_config(chip, 1); + +error_handler_unlock: + mutex_unlock(&chip->lock); + return IRQ_HANDLED; +} + +#ifdef OLDAPI +#define CM3391_COLOR_CHANNEL(_color, _addr) { \ + .type = IIO_INTENSITY, \ + .modified = 1, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .channel2 = IIO_MOD_LIGHT_##_color, \ + .address = _addr, \ +} + +static const struct iio_chan_spec cm3391_channels[] = { + { + .type = IIO_LIGHT, + .info_mask_separate = + BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_CALIBSCALE) + }, + CM3391_COLOR_CHANNEL(RED, CM3391_REG_ADDR_RED), + CM3391_COLOR_CHANNEL(GREEN, CM3391_REG_ADDR_ALS), + CM3391_COLOR_CHANNEL(BLUE, CM3391_REG_ADDR_BLUE), + CM3391_COLOR_CHANNEL(CLEAR, CM3391_REG_ADDR_CLEAR), +}; + +#else +#define CM3391_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, \ +} + +static const struct iio_chan_spec cm3391_channels[] = { + { + .type = IIO_LIGHT, + .info_mask_separate = + BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_CALIBSCALE) | + BIT(IIO_CHAN_INFO_INT_TIME), + }, + CM3391_COLOR_CHANNEL(RED, CM3391_REG_ADDR_RED), + CM3391_COLOR_CHANNEL(GREEN, CM3391_REG_ADDR_ALS), + CM3391_COLOR_CHANNEL(BLUE, CM3391_REG_ADDR_BLUE), + CM3391_COLOR_CHANNEL(CLEAR, CM3391_REG_ADDR_CLEAR), +}; +#endif /* !OLDAPI */ + +static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, + S_IRUGO, cm3391_get_it_available, NULL, 0); + +static struct attribute *cm3391_attributes[] = { + &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr, + NULL, +}; + +static const struct attribute_group cm3391_attribute_group = { + .attrs = cm3391_attributes +}; + +static const struct iio_info cm3391_info = { + .driver_module = THIS_MODULE, + .read_raw = &cm3391_read_raw, + .write_raw = &cm3391_write_raw, + .attrs = &cm3391_attribute_group, +}; + +static int cm3391_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct cm3391_chip *chip; + struct iio_dev *indio_dev; + int ret; + +#ifdef OLDAPI + indio_dev = iio_device_alloc(sizeof(*chip)); + if (!indio_dev) { + dev_err(&client->dev, "iio_device_alloc fails\n"); + return -ENOMEM; + } +#else + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); + if (!indio_dev) { + dev_err(&client->dev, "devm_iio_device_alloc failed\n"); + return -ENOMEM; + } +#endif /* OLDAPI */ + + chip = iio_priv(indio_dev); + i2c_set_clientdata(client, indio_dev); + chip->client = client; + + mutex_init(&chip->lock); + indio_dev->dev.parent = &client->dev; + indio_dev->channels = cm3391_channels; + indio_dev->num_channels = ARRAY_SIZE(cm3391_channels); + indio_dev->info = &cm3391_info; + if (id && id->name) + indio_dev->name = id->name; + else + indio_dev->name = (char *)dev_name(&client->dev); + indio_dev->channels = cm3391_channels; + indio_dev->num_channels = ARRAY_SIZE(cm3391_channels); + indio_dev->modes = INDIO_DIRECT_MODE; + + ret = cm3391_reg_init(chip); + if (ret) { + dev_err(&client->dev, + "%s: register init failed\n", + __func__); + return ret; + } + + if (client->irq) { + ret = request_threaded_irq(client->irq, + NULL, + cm3391_event_handler, + IRQF_TRIGGER_FALLING | IRQF_ONESHOT, + "cm3391_event", + indio_dev); + + if (ret < 0) { + dev_err(&client->dev, "irq request error %d\n", + -ret); + goto error_disable_int; + } + } + + ret = iio_device_register(indio_dev); + if (ret < 0) { + dev_err(&client->dev, + "%s: regist device failed\n", + __func__); + goto error_free_irq; + } + + if (client->irq) { + ret = cm3391_threshold_update(chip, CM3391_THRESHOLD_PERCENT); + if (ret < 0) + goto error_free_irq; + + ret = cm3391_interrupt_config(chip, 1); + if (ret < 0) + goto error_free_irq; + } + + return 0; + +error_free_irq: + if (client->irq) + free_irq(client->irq, indio_dev); +#ifdef OLDAPI + iio_device_free(indio_dev); +#endif /* OLDAPI */ +error_disable_int: + cm3391_interrupt_config(chip, 0); + + return ret; +} + +static int cm3391_remove(struct i2c_client *client) +{ + struct iio_dev *indio_dev = i2c_get_clientdata(client); + struct cm3391_chip *chip = iio_priv(indio_dev); + + cm3391_interrupt_config(chip, 0); + if (client->irq) + free_irq(client->irq, indio_dev); + iio_device_unregister(indio_dev); +#ifdef OLDAPI + iio_device_free(indio_dev); +#endif /* OLDAPI */ + return 0; +} + +static const struct i2c_device_id cm3391_id[] = { + { "cm3391", 0}, + { } +}; + +MODULE_DEVICE_TABLE(i2c, cm3391_id); + +static const struct of_device_id cm3391_of_match[] = { + { .compatible = "capella,cm3391" }, + { } +}; + +#ifdef CONFIG_ACPI +static const struct acpi_device_id cm3391_acpi_match[] = { + { "CPLM3391", 0}, + {}, +}; + +MODULE_DEVICE_TABLE(acpi, cm3391_acpi_match); +#endif /* CONFIG_ACPI */ + +static struct i2c_driver cm3391_driver = { + .driver = { + .name = "cm3391", +#ifdef CONFIG_ACPI + .acpi_match_table = ACPI_PTR(cm3391_acpi_match), +#endif /* CONFIG_ACPI */ + .of_match_table = of_match_ptr(cm3391_of_match), + .owner = THIS_MODULE, + }, + .id_table = cm3391_id, + .probe = cm3391_probe, + .remove = cm3391_remove, +}; + +module_i2c_driver(cm3391_driver); + +MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>"); +MODULE_DESCRIPTION("CM3391 color sensor driver"); +MODULE_LICENSE("GPL"); |