From 3a069904282d621fc65ec37713f41f69ba63645f Mon Sep 17 00:00:00 2001 From: Fabrice Gasnier Date: Wed, 18 Oct 2017 13:39:27 +0200 Subject: [PATCH 01/19] iio: adc: stm32: add tim15 trigger Add TIM15_TRGO trigger that is now supported on STM32H7. Signed-off-by: Fabrice Gasnier Signed-off-by: Jonathan Cameron --- drivers/iio/adc/stm32-adc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c index 8b7c24780a8a..c9d96f935dba 100644 --- a/drivers/iio/adc/stm32-adc.c +++ b/drivers/iio/adc/stm32-adc.c @@ -531,6 +531,7 @@ static struct stm32_adc_trig_info stm32h7_adc_trigs[] = { { TIM2_TRGO, STM32_EXT11 }, { TIM4_TRGO, STM32_EXT12 }, { TIM6_TRGO, STM32_EXT13 }, + { TIM15_TRGO, STM32_EXT14 }, { TIM3_CH4, STM32_EXT15 }, { LPTIM1_OUT, STM32_EXT18 }, { LPTIM2_OUT, STM32_EXT19 }, From f66f18e99a253f5cf8c1cf7f2910b7ddcee92970 Mon Sep 17 00:00:00 2001 From: Fabrice Gasnier Date: Wed, 18 Oct 2017 13:40:12 +0200 Subject: [PATCH 02/19] iio: adc: stm32: add check on clock rate Add check on STM32 ADC clock rate to report an explicit error. This may avoid division by 0 later in stm32-adc driver. Signed-off-by: Fabrice Gasnier Signed-off-by: Jonathan Cameron --- drivers/iio/adc/stm32-adc-core.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/iio/adc/stm32-adc-core.c b/drivers/iio/adc/stm32-adc-core.c index 804198eb0eef..6aefef99f935 100644 --- a/drivers/iio/adc/stm32-adc-core.c +++ b/drivers/iio/adc/stm32-adc-core.c @@ -139,6 +139,11 @@ static int stm32f4_adc_clk_sel(struct platform_device *pdev, } rate = clk_get_rate(priv->aclk); + if (!rate) { + dev_err(&pdev->dev, "Invalid clock rate: 0\n"); + return -EINVAL; + } + for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) { if ((rate / stm32f4_pclk_div[i]) <= STM32F4_ADC_MAX_CLK_RATE) break; @@ -216,6 +221,10 @@ static int stm32h7_adc_clk_sel(struct platform_device *pdev, * From spec: PLL output musn't exceed max rate */ rate = clk_get_rate(priv->aclk); + if (!rate) { + dev_err(&pdev->dev, "Invalid adc clock rate: 0\n"); + return -EINVAL; + } for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) { ckmode = stm32h7_adc_ckmodes_spec[i].ckmode; @@ -232,6 +241,10 @@ static int stm32h7_adc_clk_sel(struct platform_device *pdev, /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */ rate = clk_get_rate(priv->bclk); + if (!rate) { + dev_err(&pdev->dev, "Invalid bus clock rate: 0\n"); + return -EINVAL; + } for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) { ckmode = stm32h7_adc_ckmodes_spec[i].ckmode; From 59dba8facb4ba20ffa6b6a1c76c41f3c662e075a Mon Sep 17 00:00:00 2001 From: Colin Ian King Date: Thu, 19 Oct 2017 15:46:22 +0200 Subject: [PATCH 03/19] iio: adc: adc12138: make array ch_to_mux static, makes object code smaller Don't populate const array ch_to_mux on the stack, instead make it static. Makes the object code smaller by over 200 bytes: Before: text data bss dec hex filename 12663 1648 128 14439 3867 drivers/iio/adc/ti-adc12138.o After text data bss dec hex filename 12353 1744 128 14225 3791 drivers/iio/adc/ti-adc12138.o (gcc version 7.2.0 x86_64) Signed-off-by: Colin Ian King Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ti-adc12138.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/adc/ti-adc12138.c b/drivers/iio/adc/ti-adc12138.c index bf890244789a..703d68ae96b7 100644 --- a/drivers/iio/adc/ti-adc12138.c +++ b/drivers/iio/adc/ti-adc12138.c @@ -164,7 +164,7 @@ static int __adc12138_start_conv(struct adc12138 *adc, void *data, int len) { - const u8 ch_to_mux[] = { 0, 4, 1, 5, 2, 6, 3, 7 }; + static const u8 ch_to_mux[] = { 0, 4, 1, 5, 2, 6, 3, 7 }; u8 mode = (ch_to_mux[channel->channel] << 4) | (channel->differential ? 0 : 0x80); From deaecbef366497c3435b573fed7991d89af9f59c Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:20 -0400 Subject: [PATCH 04/19] staging: iio: tsl2x7x: migrate *_thresh_period sysfs attributes to iio_event_spec The sysfs attributes in_intensity0_thresh_period and in_proximity0_thresh_period are currently directly created by the driver. This patch migrates the creation of these sysfs attributes from the driver to using the IIO core via iio_event_spec. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 196 ++++++++-------------------- 1 file changed, 52 insertions(+), 144 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index e6a71f5fc9cb..2dd8c502fd7a 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -932,108 +932,6 @@ static ssize_t in_illuminance0_target_input_store(struct device *dev, return len; } -/* persistence settings */ -static ssize_t in_intensity0_thresh_period_show(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev)); - int y, z, filter_delay; - - /* Determine integration time */ - y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->settings.als_time) + 1; - z = y * TSL2X7X_MIN_ITIME; - filter_delay = z * (chip->settings.persistence & 0x0F); - y = filter_delay / 1000; - z = filter_delay % 1000; - - return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z); -} - -static ssize_t in_intensity0_thresh_period_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t len) -{ - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct tsl2X7X_chip *chip = iio_priv(indio_dev); - struct tsl2x7x_parse_result result; - int y, z, filter_delay; - int ret; - - ret = iio_str_to_fixpoint(buf, 100, &result.integer, &result.fract); - if (ret) - return ret; - - y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->settings.als_time) + 1; - z = y * TSL2X7X_MIN_ITIME; - - filter_delay = - DIV_ROUND_UP((result.integer * 1000) + result.fract, z); - - chip->settings.persistence &= 0xF0; - chip->settings.persistence |= (filter_delay & 0x0F); - - dev_info(&chip->client->dev, "%s: als persistence = %d", - __func__, filter_delay); - - ret = tsl2x7x_invoke_change(indio_dev); - if (ret < 0) - return ret; - - return IIO_VAL_INT_PLUS_MICRO; -} - -static ssize_t in_proximity0_thresh_period_show(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev)); - int y, z, filter_delay; - - /* Determine integration time */ - y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->settings.prx_time) + 1; - z = y * TSL2X7X_MIN_ITIME; - filter_delay = z * ((chip->settings.persistence & 0xF0) >> 4); - y = filter_delay / 1000; - z = filter_delay % 1000; - - return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z); -} - -static ssize_t in_proximity0_thresh_period_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t len) -{ - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct tsl2X7X_chip *chip = iio_priv(indio_dev); - struct tsl2x7x_parse_result result; - int y, z, filter_delay; - int ret; - - ret = iio_str_to_fixpoint(buf, 100, &result.integer, &result.fract); - if (ret) - return ret; - - y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->settings.prx_time) + 1; - z = y * TSL2X7X_MIN_ITIME; - - filter_delay = - DIV_ROUND_UP((result.integer * 1000) + result.fract, z); - - chip->settings.persistence &= 0x0F; - chip->settings.persistence |= ((filter_delay << 4) & 0xF0); - - dev_info(&chip->client->dev, "%s: prox persistence = %d", - __func__, filter_delay); - - ret = tsl2x7x_invoke_change(indio_dev); - if (ret < 0) - return ret; - - - return IIO_VAL_INT_PLUS_MICRO; -} - static ssize_t in_illuminance0_calibrate_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) @@ -1198,7 +1096,8 @@ static int tsl2x7x_write_event_value(struct iio_dev *indio_dev, int val, int val2) { struct tsl2X7X_chip *chip = iio_priv(indio_dev); - int ret = -EINVAL; + int ret = -EINVAL, y, z, filter_delay; + u8 time; switch (info) { case IIO_EV_INFO_VALUE: @@ -1230,6 +1129,33 @@ static int tsl2x7x_write_event_value(struct iio_dev *indio_dev, } } break; + case IIO_EV_INFO_PERIOD: + if (chan->type == IIO_INTENSITY) + time = chip->settings.als_time; + else + time = chip->settings.prx_time; + + y = (TSL2X7X_MAX_TIMER_CNT - time) + 1; + z = y * TSL2X7X_MIN_ITIME; + + filter_delay = DIV_ROUND_UP((val * 1000) + val2, z); + + if (chan->type == IIO_INTENSITY) { + chip->settings.persistence &= 0xF0; + chip->settings.persistence |= + (filter_delay & 0x0F); + dev_info(&chip->client->dev, "%s: ALS persistence = %d", + __func__, filter_delay); + } else { + chip->settings.persistence &= 0x0F; + chip->settings.persistence |= + ((filter_delay << 4) & 0xF0); + dev_info(&chip->client->dev, + "%s: Proximity persistence = %d", + __func__, filter_delay); + } + ret = 0; + break; default: break; } @@ -1248,7 +1174,8 @@ static int tsl2x7x_read_event_value(struct iio_dev *indio_dev, int *val, int *val2) { struct tsl2X7X_chip *chip = iio_priv(indio_dev); - int ret = -EINVAL; + int ret = -EINVAL, filter_delay, mult; + u8 time; switch (info) { case IIO_EV_INFO_VALUE: @@ -1280,6 +1207,23 @@ static int tsl2x7x_read_event_value(struct iio_dev *indio_dev, } } break; + case IIO_EV_INFO_PERIOD: + if (chan->type == IIO_INTENSITY) { + time = chip->settings.als_time; + mult = chip->settings.persistence & 0x0F; + } else { + time = chip->settings.prx_time; + mult = (chip->settings.persistence & 0xF0) >> 4; + } + + /* Determine integration time */ + *val = (TSL2X7X_MAX_TIMER_CNT - time) + 1; + *val2 = *val * TSL2X7X_MIN_ITIME; + filter_delay = *val2 * mult; + *val = filter_delay / 1000; + *val2 = filter_delay % 1000; + ret = IIO_VAL_INT_PLUS_MICRO; + break; default: break; } @@ -1444,10 +1388,6 @@ static DEVICE_ATTR_WO(in_proximity0_calibrate); static DEVICE_ATTR_RW(in_illuminance0_lux_table); -static DEVICE_ATTR_RW(in_intensity0_thresh_period); - -static DEVICE_ATTR_RW(in_proximity0_thresh_period); - /* Use the default register values to identify the Taos device */ static int tsl2x7x_device_id(int *id, int target) { @@ -1554,22 +1494,6 @@ static struct attribute *tsl2x7x_ALSPRX2_device_attrs[] = { NULL }; -static struct attribute *tsl2X7X_ALS_event_attrs[] = { - &dev_attr_in_intensity0_thresh_period.attr, - NULL, -}; - -static struct attribute *tsl2X7X_PRX_event_attrs[] = { - &dev_attr_in_proximity0_thresh_period.attr, - NULL, -}; - -static struct attribute *tsl2X7X_ALSPRX_event_attrs[] = { - &dev_attr_in_intensity0_thresh_period.attr, - &dev_attr_in_proximity0_thresh_period.attr, - NULL, -}; - static const struct attribute_group tsl2X7X_device_attr_group_tbl[] = { [ALS] = { .attrs = tsl2x7x_ALS_device_attrs, @@ -1588,25 +1512,9 @@ static const struct attribute_group tsl2X7X_device_attr_group_tbl[] = { }, }; -static const struct attribute_group tsl2X7X_event_attr_group_tbl[] = { - [ALS] = { - .attrs = tsl2X7X_ALS_event_attrs, - .name = "events", - }, - [PRX] = { - .attrs = tsl2X7X_PRX_event_attrs, - .name = "events", - }, - [ALSPRX] = { - .attrs = tsl2X7X_ALSPRX_event_attrs, - .name = "events", - }, -}; - static const struct iio_info tsl2X7X_device_info[] = { [ALS] = { .attrs = &tsl2X7X_device_attr_group_tbl[ALS], - .event_attrs = &tsl2X7X_event_attr_group_tbl[ALS], .read_raw = &tsl2x7x_read_raw, .write_raw = &tsl2x7x_write_raw, .read_event_value = &tsl2x7x_read_event_value, @@ -1616,7 +1524,6 @@ static const struct iio_info tsl2X7X_device_info[] = { }, [PRX] = { .attrs = &tsl2X7X_device_attr_group_tbl[PRX], - .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX], .read_raw = &tsl2x7x_read_raw, .write_raw = &tsl2x7x_write_raw, .read_event_value = &tsl2x7x_read_event_value, @@ -1626,7 +1533,6 @@ static const struct iio_info tsl2X7X_device_info[] = { }, [ALSPRX] = { .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX], - .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX], .read_raw = &tsl2x7x_read_raw, .write_raw = &tsl2x7x_write_raw, .read_event_value = &tsl2x7x_read_event_value, @@ -1636,7 +1542,6 @@ static const struct iio_info tsl2X7X_device_info[] = { }, [PRX2] = { .attrs = &tsl2X7X_device_attr_group_tbl[PRX2], - .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX], .read_raw = &tsl2x7x_read_raw, .write_raw = &tsl2x7x_write_raw, .read_event_value = &tsl2x7x_read_event_value, @@ -1646,7 +1551,6 @@ static const struct iio_info tsl2X7X_device_info[] = { }, [ALSPRX2] = { .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX2], - .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX], .read_raw = &tsl2x7x_read_raw, .write_raw = &tsl2x7x_write_raw, .read_event_value = &tsl2x7x_read_event_value, @@ -1667,6 +1571,10 @@ static const struct iio_event_spec tsl2x7x_events[] = { .dir = IIO_EV_DIR_FALLING, .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE), + }, { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_EITHER, + .mask_separate = BIT(IIO_EV_INFO_PERIOD), }, }; From 4924c7d3871bdf2cfdaa4bf448fa778d525bc222 Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:21 -0400 Subject: [PATCH 05/19] staging: iio: tsl2x7x: remove unused tsl2x7x_parse_result structure The structure tsl2x7x_parse_result is not used so this patch removes its definition. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index 2dd8c502fd7a..094ab76b5d60 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -142,11 +142,6 @@ enum { TSL2X7X_CHIP_SUSPENDED = 2 }; -struct tsl2x7x_parse_result { - int integer; - int fract; -}; - /* Per-device data */ struct tsl2x7x_als_info { u16 als_ch0; From f4b1c5bf05f90cc86bdc1219106126cb40f5c74f Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:22 -0400 Subject: [PATCH 06/19] staging: iio: tsl2x7x: sort #includes Sort the #include statements for increased code readability. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index 094ab76b5d60..d407c3ad7e2f 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -15,14 +15,14 @@ * more details. */ -#include -#include -#include #include -#include +#include +#include #include -#include +#include #include +#include +#include #include #include #include From 49e22c8ad39156aac0348d68c856e5d07d1fb6c9 Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:23 -0400 Subject: [PATCH 07/19] staging: iio: tsl2x7x: remove unnecessary struct iio_dev definition tsl2x7x.h has a blank definition for 'struct iio_dev' that is not needed. This patch removes that definition. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.h b/drivers/staging/iio/light/tsl2x7x.h index a216c6943a84..df00f2ec1719 100644 --- a/drivers/staging/iio/light/tsl2x7x.h +++ b/drivers/staging/iio/light/tsl2x7x.h @@ -23,8 +23,6 @@ #define __TSL2X7X_H #include -struct iio_dev; - struct tsl2x7x_lux { unsigned int ratio; unsigned int ch0; From 8c1c135f74282c35b6bebd2db6710c7a8e17aab5 Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:24 -0400 Subject: [PATCH 08/19] staging: iio: tsl2x7x: changed #defines to be aligned on the same column Some of the existing #defines have tabs between the name, and the value, while others have spaces. The alignment of the values mostly has a consistent layout, but there are some that don't. Change all of the defines so that the name and value is separated by tabs and all of the values start on the same column to increase code readability. This patch also removes the unnecessary parentheses around the value of TSL2X7X_MAX_TIMER_CNT. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 132 ++++++++++++++-------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index d407c3ad7e2f..3a3340aadc1a 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -29,98 +29,98 @@ #include "tsl2x7x.h" /* Cal defs*/ -#define PROX_STAT_CAL 0 -#define PROX_STAT_SAMP 1 -#define MAX_SAMPLES_CAL 200 +#define PROX_STAT_CAL 0 +#define PROX_STAT_SAMP 1 +#define MAX_SAMPLES_CAL 200 /* TSL2X7X Device ID */ -#define TRITON_ID 0x00 -#define SWORDFISH_ID 0x30 -#define HALIBUT_ID 0x20 +#define TRITON_ID 0x00 +#define SWORDFISH_ID 0x30 +#define HALIBUT_ID 0x20 /* Lux calculation constants */ -#define TSL2X7X_LUX_CALC_OVER_FLOW 65535 +#define TSL2X7X_LUX_CALC_OVER_FLOW 65535 /* TAOS Register definitions - note: * depending on device, some of these register are not used and the * register address is benign. */ /* 2X7X register offsets */ -#define TSL2X7X_MAX_CONFIG_REG 16 +#define TSL2X7X_MAX_CONFIG_REG 16 /* Device Registers and Masks */ -#define TSL2X7X_CNTRL 0x00 -#define TSL2X7X_ALS_TIME 0X01 -#define TSL2X7X_PRX_TIME 0x02 -#define TSL2X7X_WAIT_TIME 0x03 -#define TSL2X7X_ALS_MINTHRESHLO 0X04 -#define TSL2X7X_ALS_MINTHRESHHI 0X05 -#define TSL2X7X_ALS_MAXTHRESHLO 0X06 -#define TSL2X7X_ALS_MAXTHRESHHI 0X07 -#define TSL2X7X_PRX_MINTHRESHLO 0X08 -#define TSL2X7X_PRX_MINTHRESHHI 0X09 -#define TSL2X7X_PRX_MAXTHRESHLO 0X0A -#define TSL2X7X_PRX_MAXTHRESHHI 0X0B -#define TSL2X7X_PERSISTENCE 0x0C -#define TSL2X7X_PRX_CONFIG 0x0D -#define TSL2X7X_PRX_COUNT 0x0E -#define TSL2X7X_GAIN 0x0F -#define TSL2X7X_NOTUSED 0x10 -#define TSL2X7X_REVID 0x11 -#define TSL2X7X_CHIPID 0x12 -#define TSL2X7X_STATUS 0x13 -#define TSL2X7X_ALS_CHAN0LO 0x14 -#define TSL2X7X_ALS_CHAN0HI 0x15 -#define TSL2X7X_ALS_CHAN1LO 0x16 -#define TSL2X7X_ALS_CHAN1HI 0x17 -#define TSL2X7X_PRX_LO 0x18 -#define TSL2X7X_PRX_HI 0x19 +#define TSL2X7X_CNTRL 0x00 +#define TSL2X7X_ALS_TIME 0X01 +#define TSL2X7X_PRX_TIME 0x02 +#define TSL2X7X_WAIT_TIME 0x03 +#define TSL2X7X_ALS_MINTHRESHLO 0X04 +#define TSL2X7X_ALS_MINTHRESHHI 0X05 +#define TSL2X7X_ALS_MAXTHRESHLO 0X06 +#define TSL2X7X_ALS_MAXTHRESHHI 0X07 +#define TSL2X7X_PRX_MINTHRESHLO 0X08 +#define TSL2X7X_PRX_MINTHRESHHI 0X09 +#define TSL2X7X_PRX_MAXTHRESHLO 0X0A +#define TSL2X7X_PRX_MAXTHRESHHI 0X0B +#define TSL2X7X_PERSISTENCE 0x0C +#define TSL2X7X_PRX_CONFIG 0x0D +#define TSL2X7X_PRX_COUNT 0x0E +#define TSL2X7X_GAIN 0x0F +#define TSL2X7X_NOTUSED 0x10 +#define TSL2X7X_REVID 0x11 +#define TSL2X7X_CHIPID 0x12 +#define TSL2X7X_STATUS 0x13 +#define TSL2X7X_ALS_CHAN0LO 0x14 +#define TSL2X7X_ALS_CHAN0HI 0x15 +#define TSL2X7X_ALS_CHAN1LO 0x16 +#define TSL2X7X_ALS_CHAN1HI 0x17 +#define TSL2X7X_PRX_LO 0x18 +#define TSL2X7X_PRX_HI 0x19 /* tsl2X7X cmd reg masks */ -#define TSL2X7X_CMD_REG 0x80 -#define TSL2X7X_CMD_SPL_FN 0x60 +#define TSL2X7X_CMD_REG 0x80 +#define TSL2X7X_CMD_SPL_FN 0x60 -#define TSL2X7X_CMD_PROX_INT_CLR 0X05 -#define TSL2X7X_CMD_ALS_INT_CLR 0x06 -#define TSL2X7X_CMD_PROXALS_INT_CLR 0X07 +#define TSL2X7X_CMD_PROX_INT_CLR 0X05 +#define TSL2X7X_CMD_ALS_INT_CLR 0x06 +#define TSL2X7X_CMD_PROXALS_INT_CLR 0X07 /* tsl2X7X cntrl reg masks */ -#define TSL2X7X_CNTL_ADC_ENBL 0x02 -#define TSL2X7X_CNTL_PWR_ON 0x01 +#define TSL2X7X_CNTL_ADC_ENBL 0x02 +#define TSL2X7X_CNTL_PWR_ON 0x01 /* tsl2X7X status reg masks */ -#define TSL2X7X_STA_ADC_VALID 0x01 -#define TSL2X7X_STA_PRX_VALID 0x02 -#define TSL2X7X_STA_ADC_PRX_VALID (TSL2X7X_STA_ADC_VALID |\ - TSL2X7X_STA_PRX_VALID) -#define TSL2X7X_STA_ALS_INTR 0x10 -#define TSL2X7X_STA_PRX_INTR 0x20 +#define TSL2X7X_STA_ADC_VALID 0x01 +#define TSL2X7X_STA_PRX_VALID 0x02 +#define TSL2X7X_STA_ADC_PRX_VALID (TSL2X7X_STA_ADC_VALID | \ + TSL2X7X_STA_PRX_VALID) +#define TSL2X7X_STA_ALS_INTR 0x10 +#define TSL2X7X_STA_PRX_INTR 0x20 /* tsl2X7X cntrl reg masks */ -#define TSL2X7X_CNTL_REG_CLEAR 0x00 -#define TSL2X7X_CNTL_PROX_INT_ENBL 0X20 -#define TSL2X7X_CNTL_ALS_INT_ENBL 0X10 -#define TSL2X7X_CNTL_WAIT_TMR_ENBL 0X08 -#define TSL2X7X_CNTL_PROX_DET_ENBL 0X04 -#define TSL2X7X_CNTL_PWRON 0x01 -#define TSL2X7X_CNTL_ALSPON_ENBL 0x03 -#define TSL2X7X_CNTL_INTALSPON_ENBL 0x13 -#define TSL2X7X_CNTL_PROXPON_ENBL 0x0F -#define TSL2X7X_CNTL_INTPROXPON_ENBL 0x2F +#define TSL2X7X_CNTL_REG_CLEAR 0x00 +#define TSL2X7X_CNTL_PROX_INT_ENBL 0X20 +#define TSL2X7X_CNTL_ALS_INT_ENBL 0X10 +#define TSL2X7X_CNTL_WAIT_TMR_ENBL 0X08 +#define TSL2X7X_CNTL_PROX_DET_ENBL 0X04 +#define TSL2X7X_CNTL_PWRON 0x01 +#define TSL2X7X_CNTL_ALSPON_ENBL 0x03 +#define TSL2X7X_CNTL_INTALSPON_ENBL 0x13 +#define TSL2X7X_CNTL_PROXPON_ENBL 0x0F +#define TSL2X7X_CNTL_INTPROXPON_ENBL 0x2F /*Prox diode to use */ -#define TSL2X7X_DIODE0 0x10 -#define TSL2X7X_DIODE1 0x20 -#define TSL2X7X_DIODE_BOTH 0x30 +#define TSL2X7X_DIODE0 0x10 +#define TSL2X7X_DIODE1 0x20 +#define TSL2X7X_DIODE_BOTH 0x30 /* LED Power */ -#define TSL2X7X_mA100 0x00 -#define TSL2X7X_mA50 0x40 -#define TSL2X7X_mA25 0x80 -#define TSL2X7X_mA13 0xD0 -#define TSL2X7X_MAX_TIMER_CNT (0xFF) +#define TSL2X7X_mA100 0x00 +#define TSL2X7X_mA50 0x40 +#define TSL2X7X_mA25 0x80 +#define TSL2X7X_mA13 0xD0 +#define TSL2X7X_MAX_TIMER_CNT 0xFF -#define TSL2X7X_MIN_ITIME 3 +#define TSL2X7X_MIN_ITIME 3 /* TAOS txx2x7x Device family members */ enum { From 972845c3cb063ad8183b651cd97e32c4ecb7e831 Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:25 -0400 Subject: [PATCH 09/19] staging: iio: tsl2x7x: convert in_proximity0_calibscale_available to use IIO_CONST_ATTR The sysfs attribute in_proximity0_calibscale_available is currently created by using DEVICE_ATTR_RO(). Convert this over to use IIO_CONST_ATTR(). Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index 3a3340aadc1a..80968dd456a0 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -886,12 +886,7 @@ static ssize_t in_illuminance0_calibscale_available_show(struct device *dev, return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 120"); } -static ssize_t in_proximity0_calibscale_available_show(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - return snprintf(buf, PAGE_SIZE, "%s\n", "1 2 4 8"); -} +static IIO_CONST_ATTR(in_proximity0_calibscale_available, "1 2 4 8"); static IIO_CONST_ATTR(in_illuminance0_integration_time_available, ".00272 - .696"); @@ -1371,8 +1366,6 @@ static int tsl2x7x_write_raw(struct iio_dev *indio_dev, return tsl2x7x_invoke_change(indio_dev); } -static DEVICE_ATTR_RO(in_proximity0_calibscale_available); - static DEVICE_ATTR_RO(in_illuminance0_calibscale_available); static DEVICE_ATTR_RW(in_illuminance0_target_input); @@ -1468,13 +1461,13 @@ static struct attribute *tsl2x7x_ALSPRX_device_attrs[] = { &dev_attr_in_illuminance0_target_input.attr, &dev_attr_in_illuminance0_calibrate.attr, &dev_attr_in_illuminance0_lux_table.attr, - &dev_attr_in_proximity0_calibrate.attr, + &iio_const_attr_in_proximity0_calibscale_available.dev_attr.attr, NULL }; static struct attribute *tsl2x7x_PRX2_device_attrs[] = { &dev_attr_in_proximity0_calibrate.attr, - &dev_attr_in_proximity0_calibscale_available.attr, + &iio_const_attr_in_proximity0_calibscale_available.dev_attr.attr, NULL }; @@ -1485,7 +1478,7 @@ static struct attribute *tsl2x7x_ALSPRX2_device_attrs[] = { &dev_attr_in_illuminance0_calibrate.attr, &dev_attr_in_illuminance0_lux_table.attr, &dev_attr_in_proximity0_calibrate.attr, - &dev_attr_in_proximity0_calibscale_available.attr, + &iio_const_attr_in_proximity0_calibscale_available.dev_attr.attr, NULL }; From e6859c532286b0c2ce1bd048a4b07e2a8f2ea38c Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:26 -0400 Subject: [PATCH 10/19] staging: iio: tsl2x7x: remove unnecessary parentheses This patch fixes the error 'Unnecessary parentheses around 'XXX' from checkpatch.pl. It also fixes several other places with unnecessary parentheses that checkpatch.pl did not detect. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index 80968dd456a0..6cc89cd6505e 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -348,9 +348,9 @@ static int tsl2x7x_get_lux(struct iio_dev *indio_dev) /* clear any existing interrupt status */ ret = i2c_smbus_write_byte(chip->client, - (TSL2X7X_CMD_REG | + TSL2X7X_CMD_REG | TSL2X7X_CMD_SPL_FN | - TSL2X7X_CMD_ALS_INT_CLR)); + TSL2X7X_CMD_ALS_INT_CLR); if (ret < 0) { dev_err(&chip->client->dev, "i2c_write_command failed - err = %d\n", ret); @@ -364,7 +364,7 @@ static int tsl2x7x_get_lux(struct iio_dev *indio_dev) chip->als_cur_info.als_ch0 = ch0; chip->als_cur_info.als_ch1 = ch1; - if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation)) { + if (ch0 >= chip->als_saturation || ch1 >= chip->als_saturation) { lux = TSL2X7X_LUX_CALC_OVER_FLOW; goto return_max; } @@ -697,14 +697,14 @@ static int tsl2x7x_chip_on(struct iio_dev *indio_dev) dev_info(&chip->client->dev, "Setting Up Interrupt(s)\n"); reg_val = TSL2X7X_CNTL_PWR_ON | TSL2X7X_CNTL_ADC_ENBL; - if ((chip->settings.interrupts_en == 0x20) || - (chip->settings.interrupts_en == 0x30)) + if (chip->settings.interrupts_en == 0x20 || + chip->settings.interrupts_en == 0x30) reg_val |= TSL2X7X_CNTL_PROX_DET_ENBL; reg_val |= chip->settings.interrupts_en; ret = i2c_smbus_write_byte_data(chip->client, - (TSL2X7X_CMD_REG | - TSL2X7X_CNTRL), reg_val); + TSL2X7X_CMD_REG | TSL2X7X_CNTRL, + reg_val); if (ret < 0) dev_err(&chip->client->dev, "%s: failed in tsl2x7x_IOCTL_INT_SET.\n", @@ -1721,7 +1721,7 @@ static int tsl2x7x_probe(struct i2c_client *clientp, return -EINVAL; } - ret = i2c_smbus_write_byte(clientp, (TSL2X7X_CMD_REG | TSL2X7X_CNTRL)); + ret = i2c_smbus_write_byte(clientp, TSL2X7X_CMD_REG | TSL2X7X_CNTRL); if (ret < 0) { dev_err(&clientp->dev, "write to cmd reg failed. err = %d\n", ret); From 3e0c8f68a89e2356fa014336830fe4aaf580303b Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:27 -0400 Subject: [PATCH 11/19] staging: iio: tsl2x7x: correct alignment of parenthesis Correct error from checkpatch.pl to improve code readibility: Alignment should match open parenthesis. This involved shortening the name of tsl2x7x_als_gainadj and tsl2x7x_prx_gainadj to tsl2x7x_als_gain and tsl2x7x_prx_gain respectively. This also required removing the ch0lux and ch1lux local variables in order to get the line short enough. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index 6cc89cd6505e..886be9aa3c0f 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -243,14 +243,14 @@ static const struct tsl2x7x_settings tsl2x7x_default_settings = { .prox_pulse_count = 8 }; -static const s16 tsl2X7X_als_gainadj[] = { +static const s16 tsl2x7x_als_gain[] = { 1, 8, 16, 120 }; -static const s16 tsl2X7X_prx_gainadj[] = { +static const s16 tsl2x7x_prx_gain[] = { 1, 2, 4, @@ -384,11 +384,10 @@ static int tsl2x7x_get_lux(struct iio_dev *indio_dev) if (p->ratio == 0) { lux = 0; } else { - ch0lux = DIV_ROUND_UP(ch0 * p->ch0, - tsl2X7X_als_gainadj[chip->settings.als_gain]); - ch1lux = DIV_ROUND_UP(ch1 * p->ch1, - tsl2X7X_als_gainadj[chip->settings.als_gain]); - lux = ch0lux - ch1lux; + lux = DIV_ROUND_UP(ch0 * p->ch0, + tsl2x7x_als_gain[chip->settings.als_gain]) - + DIV_ROUND_UP(ch1 * p->ch1, + tsl2x7x_als_gain[chip->settings.als_gain]); } /* note: lux is 31 bit max at this point */ @@ -1263,9 +1262,9 @@ static int tsl2x7x_read_raw(struct iio_dev *indio_dev, break; case IIO_CHAN_INFO_CALIBSCALE: if (chan->type == IIO_LIGHT) - *val = tsl2X7X_als_gainadj[chip->settings.als_gain]; + *val = tsl2x7x_als_gain[chip->settings.als_gain]; else - *val = tsl2X7X_prx_gainadj[chip->settings.prox_gain]; + *val = tsl2x7x_prx_gain[chip->settings.prox_gain]; ret = IIO_VAL_INT; break; case IIO_CHAN_INFO_CALIBBIAS: From 18ce2d5097b699b2f2a79b7fdcd01cc5ed2b0843 Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:28 -0400 Subject: [PATCH 12/19] staging: iio: tsl2x7x: correct alignment of parenthesis Correct error from checkpatch.pl to improve code readibility: Alignment should match open parenthesis. An unnecessary cast to 'struct tsl2x7x_lux *' was removed and the return value of static definition of in_illuminance0_calibscale_available_show() was put on its own line due to the length of that sysfs attribute. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 35 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index 886be9aa3c0f..70007117d985 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -521,8 +521,8 @@ static void tsl2x7x_defaults(struct tsl2X7X_chip *chip) sizeof(chip->pdata->platform_lux_table)); else memcpy(chip->tsl2x7x_device_lux, - (struct tsl2x7x_lux *)tsl2x7x_default_lux_table_group[chip->id], - TSL2X7X_DEFAULT_TABLE_BYTES); + tsl2x7x_default_lux_table_group[chip->id], + TSL2X7X_DEFAULT_TABLE_BYTES); } /** @@ -867,9 +867,10 @@ static void tsl2x7x_prox_cal(struct iio_dev *indio_dev) tsl2x7x_chip_on(indio_dev); } -static ssize_t in_illuminance0_calibscale_available_show(struct device *dev, - struct device_attribute *attr, - char *buf) +static ssize_t +in_illuminance0_calibscale_available_show(struct device *dev, + struct device_attribute *attr, + char *buf) { struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev)); @@ -891,8 +892,8 @@ static IIO_CONST_ATTR(in_illuminance0_integration_time_available, ".00272 - .696"); static ssize_t in_illuminance0_target_input_show(struct device *dev, - struct device_attribute *attr, - char *buf) + struct device_attribute *attr, + char *buf) { struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev)); @@ -900,8 +901,8 @@ static ssize_t in_illuminance0_target_input_show(struct device *dev, } static ssize_t in_illuminance0_target_input_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t len) + struct device_attribute *attr, + const char *buf, size_t len) { struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct tsl2X7X_chip *chip = iio_priv(indio_dev); @@ -922,8 +923,8 @@ static ssize_t in_illuminance0_target_input_store(struct device *dev, } static ssize_t in_illuminance0_calibrate_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t len) + struct device_attribute *attr, + const char *buf, size_t len) { struct iio_dev *indio_dev = dev_to_iio_dev(dev); bool value; @@ -943,8 +944,8 @@ static ssize_t in_illuminance0_calibrate_store(struct device *dev, } static ssize_t in_illuminance0_lux_table_show(struct device *dev, - struct device_attribute *attr, - char *buf) + struct device_attribute *attr, + char *buf) { struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev)); int i = 0; @@ -971,8 +972,8 @@ static ssize_t in_illuminance0_lux_table_show(struct device *dev, } static ssize_t in_illuminance0_lux_table_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t len) + struct device_attribute *attr, + const char *buf, size_t len) { struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct tsl2X7X_chip *chip = iio_priv(indio_dev); @@ -1013,8 +1014,8 @@ static ssize_t in_illuminance0_lux_table_store(struct device *dev, } static ssize_t in_proximity0_calibrate_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t len) + struct device_attribute *attr, + const char *buf, size_t len) { struct iio_dev *indio_dev = dev_to_iio_dev(dev); bool value; From 271bd2c67677314db3985fcc8bc1fd83356e1d81 Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:29 -0400 Subject: [PATCH 13/19] staging: iio: tsl2x7x: rename power defines to improve code readability The LED power defines are named like TSL2X7X_mAXXX. Rename these values to TSL2X7X_XXX_mA to improve code readability. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index 70007117d985..bb9fb60509cf 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -114,10 +114,10 @@ #define TSL2X7X_DIODE_BOTH 0x30 /* LED Power */ -#define TSL2X7X_mA100 0x00 -#define TSL2X7X_mA50 0x40 -#define TSL2X7X_mA25 0x80 -#define TSL2X7X_mA13 0xD0 +#define TSL2X7X_100_mA 0x00 +#define TSL2X7X_50_mA 0x40 +#define TSL2X7X_25_mA 0x80 +#define TSL2X7X_13_mA 0xD0 #define TSL2X7X_MAX_TIMER_CNT 0xFF #define TSL2X7X_MIN_ITIME 3 @@ -636,7 +636,7 @@ static int tsl2x7x_chip_on(struct iio_dev *indio_dev) /* Set the gain based on tsl2x7x_settings struct */ chip->tsl2x7x_config[TSL2X7X_GAIN] = chip->settings.als_gain | - (TSL2X7X_mA100 | TSL2X7X_DIODE1) | + (TSL2X7X_100_mA | TSL2X7X_DIODE1) | (chip->settings.prox_gain << 2); /* set chip struct re scaling and saturation */ From 3d8e44002906de59836ac39244ea172b1be6165e Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:30 -0400 Subject: [PATCH 14/19] staging: iio: tsl2x7x: fix alignment of break statements Correct the alignment of the break statements to match the alignment of the rest of the code within the case statements. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index bb9fb60509cf..f12ab1239a46 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -466,7 +466,7 @@ static int tsl2x7x_get_prox(struct iio_dev *indio_dev) case tmd2771: if (!(ret & TSL2X7X_STA_ADC_VALID)) goto prox_poll_err; - break; + break; case tsl2572: case tsl2672: case tmd2672: @@ -474,7 +474,7 @@ static int tsl2x7x_get_prox(struct iio_dev *indio_dev) case tmd2772: if (!(ret & TSL2X7X_STA_PRX_VALID)) goto prox_poll_err; - break; + break; } for (i = 0; i < 2; i++) { From 724330a9d61aa61ee20f4895302124490a452629 Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Thu, 19 Oct 2017 16:06:31 -0400 Subject: [PATCH 15/19] staging: iio: tsl2x7x: put function definitions on a single line The functions tsl2x7x_invoke_change() and tsl2x7x_prox_calculate() are short enough that the return value and static declaration can be moved onto the same line with the function name. This patch makes that change to increase code readability. Signed-off-by: Brian Masney Signed-off-by: Jonathan Cameron --- drivers/staging/iio/light/tsl2x7x.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index f12ab1239a46..42ed9c015aaf 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -752,8 +752,7 @@ static int tsl2x7x_chip_off(struct iio_dev *indio_dev) * put device back into proper state, and unlock * resource. */ -static -int tsl2x7x_invoke_change(struct iio_dev *indio_dev) +static int tsl2x7x_invoke_change(struct iio_dev *indio_dev) { struct tsl2X7X_chip *chip = iio_priv(indio_dev); int device_status = chip->tsl2x7x_chip_status; @@ -777,9 +776,8 @@ unlock: return ret; } -static -void tsl2x7x_prox_calculate(int *data, int length, - struct tsl2x7x_prox_stat *statP) +static void tsl2x7x_prox_calculate(int *data, int length, + struct tsl2x7x_prox_stat *statP) { int i; int sample_sum; From 0d87a4aa376983a205b3920dc7a9b15d74110be9 Mon Sep 17 00:00:00 2001 From: Corentin Labbe Date: Fri, 20 Oct 2017 07:37:29 +0200 Subject: [PATCH 16/19] iio: adc: sun4i-gpadc: use of_device_get_match_data The usage of of_device_get_match_data reduce the code size a bit. Furthermore, it prevents an improbable dereference when of_match_device() return NULL. Signed-off-by: Corentin Labbe Signed-off-by: Jonathan Cameron --- drivers/iio/adc/sun4i-gpadc-iio.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/sun4i-gpadc-iio.c b/drivers/iio/adc/sun4i-gpadc-iio.c index c4e70f1cad79..04d7147e0110 100644 --- a/drivers/iio/adc/sun4i-gpadc-iio.c +++ b/drivers/iio/adc/sun4i-gpadc-iio.c @@ -501,17 +501,15 @@ static int sun4i_gpadc_probe_dt(struct platform_device *pdev, struct iio_dev *indio_dev) { struct sun4i_gpadc_iio *info = iio_priv(indio_dev); - const struct of_device_id *of_dev; struct resource *mem; void __iomem *base; int ret; - of_dev = of_match_device(sun4i_gpadc_of_id, &pdev->dev); - if (!of_dev) + info->data = of_device_get_match_data(&pdev->dev); + if (!info->data) return -ENODEV; info->no_irq = true; - info->data = (struct gpadc_data *)of_dev->data; indio_dev->num_channels = ARRAY_SIZE(sun8i_a33_gpadc_channels); indio_dev->channels = sun8i_a33_gpadc_channels; From 930cc2d27847a4235bb3dc1e3817504dcf230dcb Mon Sep 17 00:00:00 2001 From: Lukas Wunner Date: Tue, 17 Oct 2017 12:42:00 +0200 Subject: [PATCH 17/19] dt-bindings: iio: dac: ti-dac082s085: Document new driver Add device tree bindings for Texas Instruments 8/10/12-bit 2/4-channel DAC driver. Cc: Mathias Duckeck Acked-by: Rob Herring Signed-off-by: Lukas Wunner Signed-off-by: Jonathan Cameron --- .../bindings/iio/dac/ti-dac082s085.txt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/dac/ti-dac082s085.txt diff --git a/Documentation/devicetree/bindings/iio/dac/ti-dac082s085.txt b/Documentation/devicetree/bindings/iio/dac/ti-dac082s085.txt new file mode 100644 index 000000000000..9cb0e10df704 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/dac/ti-dac082s085.txt @@ -0,0 +1,34 @@ +Texas Instruments 8/10/12-bit 2/4-channel DAC driver + +Required properties: + - compatible: Must be one of: + "ti,dac082s085" + "ti,dac102s085" + "ti,dac122s085" + "ti,dac084s085" + "ti,dac104s085" + "ti,dac124s085" + - reg: Chip select number. + - spi-cpha, spi-cpol: SPI mode (0,1) or (1,0) must be used, so specify + either spi-cpha or spi-cpol (but not both). + - vref-supply: Phandle to the external reference voltage supply. + +For other required and optional properties of SPI slave nodes please refer to +../../spi/spi-bus.txt. + +Example: + vref_2v5_reg: regulator-vref { + compatible = "regulator-fixed"; + regulator-name = "2v5"; + regulator-min-microvolt = <2500000>; + regulator-max-microvolt = <2500000>; + regulator-always-on; + }; + + dac@0 { + compatible = "ti,dac082s085"; + reg = <0>; + spi-max-frequency = <40000000>; + spi-cpol; + vref-supply = <&vref_2v5_reg>; + }; From 61011264c1afd8c075fb9028ccc78e7f2e63ce48 Mon Sep 17 00:00:00 2001 From: Lukas Wunner Date: Tue, 17 Oct 2017 12:42:00 +0200 Subject: [PATCH 18/19] iio: dac: Add Texas Instruments 8/10/12-bit 2/4-channel DAC driver The DACrrcS085 (rr = 08/10/12, c = 2/4) family of SPI DACs was inherited by TI when they acquired National Semiconductor in 2011. This driver was developed for and tested with the DAC082S085 built into the Revolution Pi by KUNBUS, but should work with any of the other chips as they share the same programming interface. There is also a family of I2C DACs with just a single channel called DACrr1C08x (rr = 08/10/12, x = 1/5). Their programming interface is very similar and it should be possible to extend the driver for these chips with moderate effort. Alternatively they could be integrated into ad5446.c. (The AD5301/AD5311/AD5321 use different power-down modes but otherwise appear to be comparable.) Furthermore there is a family of 8-channel DACs called DACrr8S085 (rr = 08/10/12) as well as two 16-bit DACs called DAC161Sxxx (xxx = 055/997). These are more complicated devices with support for daisy-chaining and the ability to power down each channel separately. They could either be handled by a separate driver or integrated into the present driver with a larger effort. Cc: Mathias Duckeck Signed-off-by: Lukas Wunner Signed-off-by: Jonathan Cameron --- Documentation/ABI/testing/sysfs-bus-iio | 1 + drivers/iio/dac/Kconfig | 10 + drivers/iio/dac/Makefile | 1 + drivers/iio/dac/ti-dac082s085.c | 351 ++++++++++++++++++++++++ 4 files changed, 363 insertions(+) create mode 100644 drivers/iio/dac/ti-dac082s085.c diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio index 3fc79185cc56..2e3f919485f4 100644 --- a/Documentation/ABI/testing/sysfs-bus-iio +++ b/Documentation/ABI/testing/sysfs-bus-iio @@ -522,6 +522,7 @@ Description: Specifies the output powerdown mode. DAC output stage is disconnected from the amplifier and 1kohm_to_gnd: connected to ground via an 1kOhm resistor, + 2.5kohm_to_gnd: connected to ground via a 2.5kOhm resistor, 6kohm_to_gnd: connected to ground via a 6kOhm resistor, 20kohm_to_gnd: connected to ground via a 20kOhm resistor, 90kohm_to_gnd: connected to ground via a 90kOhm resistor, diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig index d187233dec3a..965d5c0d2468 100644 --- a/drivers/iio/dac/Kconfig +++ b/drivers/iio/dac/Kconfig @@ -310,6 +310,16 @@ config STM32_DAC config STM32_DAC_CORE tristate +config TI_DAC082S085 + tristate "Texas Instruments 8/10/12-bit 2/4-channel DAC driver" + depends on SPI_MASTER + help + Driver for the Texas Instruments (formerly National Semiconductor) + DAC082S085, DAC102S085, DAC122S085, DAC084S085, DAC104S085 and + DAC124S085. + + If compiled as a module, it will be called ti-dac082s085. + config VF610_DAC tristate "Vybrid vf610 DAC driver" depends on OF diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile index 8fe5af231698..4785858e04cc 100644 --- a/drivers/iio/dac/Makefile +++ b/drivers/iio/dac/Makefile @@ -33,4 +33,5 @@ obj-$(CONFIG_MCP4725) += mcp4725.o obj-$(CONFIG_MCP4922) += mcp4922.o obj-$(CONFIG_STM32_DAC_CORE) += stm32-dac-core.o obj-$(CONFIG_STM32_DAC) += stm32-dac.o +obj-$(CONFIG_TI_DAC082S085) += ti-dac082s085.o obj-$(CONFIG_VF610_DAC) += vf610_dac.o diff --git a/drivers/iio/dac/ti-dac082s085.c b/drivers/iio/dac/ti-dac082s085.c new file mode 100644 index 000000000000..feac1db592af --- /dev/null +++ b/drivers/iio/dac/ti-dac082s085.c @@ -0,0 +1,351 @@ +/* + * ti-dac082s085.c - Texas Instruments 8/10/12-bit 2/4-channel DAC driver + * + * Copyright (C) 2017 KUNBUS GmbH + * + * http://www.ti.com/lit/ds/symlink/dac082s085.pdf + * http://www.ti.com/lit/ds/symlink/dac102s085.pdf + * http://www.ti.com/lit/ds/symlink/dac122s085.pdf + * http://www.ti.com/lit/ds/symlink/dac084s085.pdf + * http://www.ti.com/lit/ds/symlink/dac104s085.pdf + * http://www.ti.com/lit/ds/symlink/dac124s085.pdf + * + * 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 + +/** + * struct ti_dac_chip - TI DAC chip + * @lock: protects write sequences + * @vref: regulator generating Vref + * @mesg: SPI message to perform a write + * @xfer: SPI transfer used by @mesg + * @val: cached value of each output + * @powerdown: whether the chip is powered down + * @powerdown_mode: selected by the user + * @resolution: resolution of the chip + * @buf: buffer for @xfer + */ +struct ti_dac_chip { + struct mutex lock; + struct regulator *vref; + struct spi_message mesg; + struct spi_transfer xfer; + u16 val[4]; + bool powerdown; + u8 powerdown_mode; + u8 resolution; + u8 buf[2] ____cacheline_aligned; +}; + +#define WRITE_NOT_UPDATE(chan) (0x00 | (chan) << 6) +#define WRITE_AND_UPDATE(chan) (0x10 | (chan) << 6) +#define WRITE_ALL_UPDATE 0x20 +#define POWERDOWN(mode) (0x30 | ((mode) + 1) << 6) + +static int ti_dac_cmd(struct ti_dac_chip *ti_dac, u8 cmd, u16 val) +{ + u8 shift = 12 - ti_dac->resolution; + + ti_dac->buf[0] = cmd | (val >> (8 - shift)); + ti_dac->buf[1] = (val << shift) & 0xff; + return spi_sync(ti_dac->mesg.spi, &ti_dac->mesg); +} + +static const char * const ti_dac_powerdown_modes[] = { + "2.5kohm_to_gnd", "100kohm_to_gnd", "three_state", +}; + +static int ti_dac_get_powerdown_mode(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan) +{ + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); + + return ti_dac->powerdown_mode; +} + +static int ti_dac_set_powerdown_mode(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + unsigned int mode) +{ + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); + int ret = 0; + + if (ti_dac->powerdown_mode == mode) + return 0; + + mutex_lock(&ti_dac->lock); + if (ti_dac->powerdown) { + ret = ti_dac_cmd(ti_dac, POWERDOWN(mode), 0); + if (ret) + goto out; + } + ti_dac->powerdown_mode = mode; + +out: + mutex_unlock(&ti_dac->lock); + return ret; +} + +static const struct iio_enum ti_dac_powerdown_mode = { + .items = ti_dac_powerdown_modes, + .num_items = ARRAY_SIZE(ti_dac_powerdown_modes), + .get = ti_dac_get_powerdown_mode, + .set = ti_dac_set_powerdown_mode, +}; + +static ssize_t ti_dac_read_powerdown(struct iio_dev *indio_dev, + uintptr_t private, + const struct iio_chan_spec *chan, + char *buf) +{ + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); + + return sprintf(buf, "%d\n", ti_dac->powerdown); +} + +static ssize_t ti_dac_write_powerdown(struct iio_dev *indio_dev, + uintptr_t private, + const struct iio_chan_spec *chan, + const char *buf, size_t len) +{ + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); + bool powerdown; + int ret; + + ret = strtobool(buf, &powerdown); + if (ret) + return ret; + + if (ti_dac->powerdown == powerdown) + return len; + + mutex_lock(&ti_dac->lock); + if (powerdown) + ret = ti_dac_cmd(ti_dac, POWERDOWN(ti_dac->powerdown_mode), 0); + else + ret = ti_dac_cmd(ti_dac, WRITE_AND_UPDATE(0), ti_dac->val[0]); + if (!ret) + ti_dac->powerdown = powerdown; + mutex_unlock(&ti_dac->lock); + + return ret ? ret : len; +} + +static const struct iio_chan_spec_ext_info ti_dac_ext_info[] = { + { + .name = "powerdown", + .read = ti_dac_read_powerdown, + .write = ti_dac_write_powerdown, + .shared = IIO_SHARED_BY_TYPE, + }, + IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE, &ti_dac_powerdown_mode), + IIO_ENUM_AVAILABLE("powerdown_mode", &ti_dac_powerdown_mode), + { }, +}; + +#define TI_DAC_CHANNEL(chan) { \ + .type = IIO_VOLTAGE, \ + .channel = (chan), \ + .address = (chan), \ + .indexed = true, \ + .output = true, \ + .datasheet_name = (const char[]){ 'A' + (chan), 0 }, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .ext_info = ti_dac_ext_info, \ +} + +static const struct iio_chan_spec ti_dac_channels[] = { + TI_DAC_CHANNEL(0), + TI_DAC_CHANNEL(1), + TI_DAC_CHANNEL(2), + TI_DAC_CHANNEL(3), +}; + +static int ti_dac_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + *val = ti_dac->val[chan->channel]; + ret = IIO_VAL_INT; + break; + + case IIO_CHAN_INFO_SCALE: + ret = regulator_get_voltage(ti_dac->vref); + if (ret < 0) + return ret; + + *val = ret / 1000; + *val2 = ti_dac->resolution; + ret = IIO_VAL_FRACTIONAL_LOG2; + break; + + default: + ret = -EINVAL; + } + + return ret; +} + +static int ti_dac_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + if (ti_dac->val[chan->channel] == val) + return 0; + + if (val >= (1 << ti_dac->resolution) || val < 0) + return -EINVAL; + + if (ti_dac->powerdown) + return -EBUSY; + + mutex_lock(&ti_dac->lock); + ret = ti_dac_cmd(ti_dac, WRITE_AND_UPDATE(chan->channel), val); + if (!ret) + ti_dac->val[chan->channel] = val; + mutex_unlock(&ti_dac->lock); + break; + + default: + ret = -EINVAL; + } + + return ret; +} + +static int ti_dac_write_raw_get_fmt(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, long mask) +{ + return IIO_VAL_INT; +} + +static const struct iio_info ti_dac_info = { + .read_raw = ti_dac_read_raw, + .write_raw = ti_dac_write_raw, + .write_raw_get_fmt = ti_dac_write_raw_get_fmt, +}; + +static int ti_dac_probe(struct spi_device *spi) +{ + struct device *dev = &spi->dev; + struct ti_dac_chip *ti_dac; + struct iio_dev *indio_dev; + int ret; + + indio_dev = devm_iio_device_alloc(dev, sizeof(*ti_dac)); + if (!indio_dev) + return -ENOMEM; + + indio_dev->dev.parent = dev; + indio_dev->info = &ti_dac_info; + indio_dev->name = spi->modalias; + indio_dev->modes = INDIO_DIRECT_MODE; + indio_dev->channels = ti_dac_channels; + spi_set_drvdata(spi, indio_dev); + + ti_dac = iio_priv(indio_dev); + ti_dac->xfer.tx_buf = &ti_dac->buf; + ti_dac->xfer.len = sizeof(ti_dac->buf); + spi_message_init_with_transfers(&ti_dac->mesg, &ti_dac->xfer, 1); + ti_dac->mesg.spi = spi; + + ret = sscanf(spi->modalias, "dac%2hhu%1d", + &ti_dac->resolution, &indio_dev->num_channels); + WARN_ON(ret != 2); + + ti_dac->vref = devm_regulator_get(dev, "vref"); + if (IS_ERR(ti_dac->vref)) + return PTR_ERR(ti_dac->vref); + + ret = regulator_enable(ti_dac->vref); + if (ret < 0) + return ret; + + mutex_init(&ti_dac->lock); + + ret = ti_dac_cmd(ti_dac, WRITE_ALL_UPDATE, 0); + if (ret) { + dev_err(dev, "failed to initialize outputs to 0\n"); + goto err; + } + + ret = iio_device_register(indio_dev); + if (ret) + goto err; + + return 0; + +err: + mutex_destroy(&ti_dac->lock); + regulator_disable(ti_dac->vref); + return ret; +} + +static int ti_dac_remove(struct spi_device *spi) +{ + struct iio_dev *indio_dev = spi_get_drvdata(spi); + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); + + iio_device_unregister(indio_dev); + mutex_destroy(&ti_dac->lock); + regulator_disable(ti_dac->vref); + + return 0; +} + +#ifdef CONFIG_OF +static const struct of_device_id ti_dac_of_id[] = { + { .compatible = "ti,dac082s085" }, + { .compatible = "ti,dac102s085" }, + { .compatible = "ti,dac122s085" }, + { .compatible = "ti,dac084s085" }, + { .compatible = "ti,dac104s085" }, + { .compatible = "ti,dac124s085" }, + { } +}; +MODULE_DEVICE_TABLE(of, ti_dac_of_id); +#endif + +static const struct spi_device_id ti_dac_spi_id[] = { + { "dac082s085" }, + { "dac102s085" }, + { "dac122s085" }, + { "dac084s085" }, + { "dac104s085" }, + { "dac124s085" }, + { } +}; +MODULE_DEVICE_TABLE(spi, ti_dac_spi_id); + +static struct spi_driver ti_dac_driver = { + .driver = { + .name = "ti-dac082s085", + .of_match_table = of_match_ptr(ti_dac_of_id), + }, + .probe = ti_dac_probe, + .remove = ti_dac_remove, + .id_table = ti_dac_spi_id, +}; +module_spi_driver(ti_dac_driver); + +MODULE_AUTHOR("Lukas Wunner "); +MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 2/4-channel DAC driver"); +MODULE_LICENSE("GPL v2"); From f98677cf315e61403aefef72b056c643dd152c54 Mon Sep 17 00:00:00 2001 From: Lukas Wunner Date: Tue, 17 Oct 2017 12:42:00 +0200 Subject: [PATCH 19/19] iio: dac: ti-dac082s085: Read chip spec from device table The two properties unique to each supported chip, resolution and number of channels, are currently gleaned from the chip's name. E.g. dac102s085 is a dual channel 10-bit DAC. ^^^ This was deemed unmaintainable by the subsystem maintainer once the driver is extended to support further chips, hence it was requested to add an explicit table for chip-specific information and use an enum to reference into it. This adds 17 LoC without any immediate gain, so make the change in a separate commit which can be reverted if we determine in 10 years that it was unnecessary. Signed-off-by: Lukas Wunner Signed-off-by: Jonathan Cameron --- drivers/iio/dac/ti-dac082s085.c | 35 ++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/drivers/iio/dac/ti-dac082s085.c b/drivers/iio/dac/ti-dac082s085.c index feac1db592af..4e1e28339c84 100644 --- a/drivers/iio/dac/ti-dac082s085.c +++ b/drivers/iio/dac/ti-dac082s085.c @@ -20,6 +20,22 @@ #include #include +enum { dual_8bit, dual_10bit, dual_12bit, quad_8bit, quad_10bit, quad_12bit }; + +struct ti_dac_spec { + u8 num_channels; + u8 resolution; +}; + +static const struct ti_dac_spec ti_dac_spec[] = { + [dual_8bit] = { .num_channels = 2, .resolution = 8 }, + [dual_10bit] = { .num_channels = 2, .resolution = 10 }, + [dual_12bit] = { .num_channels = 2, .resolution = 12 }, + [quad_8bit] = { .num_channels = 4, .resolution = 8 }, + [quad_10bit] = { .num_channels = 4, .resolution = 10 }, + [quad_12bit] = { .num_channels = 4, .resolution = 12 }, +}; + /** * struct ti_dac_chip - TI DAC chip * @lock: protects write sequences @@ -246,6 +262,7 @@ static const struct iio_info ti_dac_info = { static int ti_dac_probe(struct spi_device *spi) { struct device *dev = &spi->dev; + const struct ti_dac_spec *spec; struct ti_dac_chip *ti_dac; struct iio_dev *indio_dev; int ret; @@ -267,9 +284,9 @@ static int ti_dac_probe(struct spi_device *spi) spi_message_init_with_transfers(&ti_dac->mesg, &ti_dac->xfer, 1); ti_dac->mesg.spi = spi; - ret = sscanf(spi->modalias, "dac%2hhu%1d", - &ti_dac->resolution, &indio_dev->num_channels); - WARN_ON(ret != 2); + spec = &ti_dac_spec[spi_get_device_id(spi)->driver_data]; + indio_dev->num_channels = spec->num_channels; + ti_dac->resolution = spec->resolution; ti_dac->vref = devm_regulator_get(dev, "vref"); if (IS_ERR(ti_dac->vref)) @@ -325,12 +342,12 @@ MODULE_DEVICE_TABLE(of, ti_dac_of_id); #endif static const struct spi_device_id ti_dac_spi_id[] = { - { "dac082s085" }, - { "dac102s085" }, - { "dac122s085" }, - { "dac084s085" }, - { "dac104s085" }, - { "dac124s085" }, + { "dac082s085", dual_8bit }, + { "dac102s085", dual_10bit }, + { "dac122s085", dual_12bit }, + { "dac084s085", quad_8bit }, + { "dac104s085", quad_10bit }, + { "dac124s085", quad_12bit }, { } }; MODULE_DEVICE_TABLE(spi, ti_dac_spi_id);