Skip to contentSkip to conversation

add support for Sensirion SPS30 PM sensor

PATCH Superseded Tomasz Duszynski 7 participants 25 messages Nov 24, 2018 – Dec 4, 2018

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tomasz Duszynski

author maintainer
#0 lore raw

cover add support for Sensirion SPS30 PM sensor

This patch series adds support for Sensirion SPS30 particulate matter sensor. Along with a driver itself, new channel type and two modifiers for distinguishing between coarse and fine particles measurements are introduced.

Sensor datasheet can be downloaded from

https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/0_Datasheets/Particulate_Matter/Sensirion_PM_Sensors_SPS30_Datasheet.pdf

Tomasz Duszynski (3):
iio: add IIO_MASSCONCENTRATION channel type
iio: chemical: add support for Sensirion SPS30 sensor
iio: chemical: sps30: add device tree support

 Documentation/ABI/testing/sysfs-bus-iio       |  11 +-
 .../bindings/iio/chemical/sensirion,sps30.txt |  12 +
 drivers/iio/chemical/Kconfig                  |  11 +
 drivers/iio/chemical/Makefile                 |   1 +
 drivers/iio/chemical/sps30.c                  | 359 ++++++++++++++++++
 drivers/iio/industrialio-core.c               |   3 +
 include/uapi/linux/iio/types.h                |   3 +
 tools/iio/iio_event_monitor.c                 |   6 +
 8 files changed, 405 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt
 create mode 100644 drivers/iio/chemical/sps30.c

--
2.19.2

author maintainer
#1 lore raw

PATCH 1/3 iio: add IIO_MASSCONCENTRATION channel type

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:	4.18
 Contact:	[email protected]
 Description:
 		Raw (unscaled) phase difference reading from channel Y
-		that can be processed to radians.
\ No newline at end of file
+		that can be processed to radians.
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion:	4.21
+Contact:	[email protected]
+Description:
+		Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_GRAVITY]  = "gravity",
 	[IIO_POSITIONRELATIVE]  = "positionrelative",
 	[IIO_PHASE] = "phase",
+	[IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
 	[IIO_MOD_Q] = "q",
 	[IIO_MOD_CO2] = "co2",
 	[IIO_MOD_VOC] = "voc",
+	[IIO_MOD_PM2p5] = "pm2p5",
+	[IIO_MOD_PM10] = "pm10",
 };
 
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +3 -0
index 92baabc103ac..465044b42af5 100644
@@ -46,6 +46,7 @@ enum iio_chan_type {
 	IIO_GRAVITY,
 	IIO_POSITIONRELATIVE,
 	IIO_PHASE,
+	IIO_MASSCONCENTRATION,
 };
 
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
 	IIO_MOD_VOC,
 	IIO_MOD_LIGHT_UV,
 	IIO_MOD_LIGHT_DUV,
+	IIO_MOD_PM2p5,
+	IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -0
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_GRAVITY] = "gravity",
 	[IIO_POSITIONRELATIVE] = "positionrelative",
 	[IIO_PHASE] = "phase",
+	[IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
 	[IIO_MOD_Q] = "q",
 	[IIO_MOD_CO2] = "co2",
 	[IIO_MOD_VOC] = "voc",
+	[IIO_MOD_PM2p5] = "pm2p5",
+	[IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
 	case IIO_GRAVITY:
 	case IIO_POSITIONRELATIVE:
 	case IIO_PHASE:
+	case IIO_MASSCONCENTRATION:
 		break;
 	default:
 		return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
 	case IIO_MOD_Q:
 	case IIO_MOD_CO2:
 	case IIO_MOD_VOC:
+	case IIO_MOD_PM2p5:
+	case IIO_MOD_PM10:
 		break;
 	default:
 		return false;
Signature · 1 line
2.19.2
#2 lore raw

Re: [PATCH 1/3] iio: add IIO_MASSCONCENTRATION channel type

Quoted 11 lines · On Sat, 24 Nov 2018 23:14:13 +0100 Tomasz Duszynski <[email protected]> wrote:

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

I initially wondered why these particular numbers and why don't we provide an attribute specify this separately?

However google suggests these two are pretty much the only ones anyone worries about in pollution sensors. I ended up fairly quickly at the EPA website
https://www.epa.gov/pm-pollution/table-historical-particulate-matter-pm-national-ambient-air-quality-standards-naaqs
which tells me these two have be used since about 1987.

So I think the modifier route is the right approach here (I think we've gotten this wrong in the past such as light sensor colours where the list keeps on growing).

I would like a reference or two in the docs though to point people to these definitions.

Thanks,

Jonathan

Quoted 109 lines
---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:	4.18
 Contact:	[email protected]
 Description:
 		Raw (unscaled) phase difference reading from channel Y
-		that can be processed to radians.
\ No newline at end of file
+		that can be processed to radians.
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion:	4.21
+Contact:	[email protected]
+Description:
+		Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_GRAVITY]  = "gravity",
 	[IIO_POSITIONRELATIVE]  = "positionrelative",
 	[IIO_PHASE] = "phase",
+	[IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
 	[IIO_MOD_Q] = "q",
 	[IIO_MOD_CO2] = "co2",
 	[IIO_MOD_VOC] = "voc",
+	[IIO_MOD_PM2p5] = "pm2p5",
+	[IIO_MOD_PM10] = "pm10",
 };
 
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +3 -0
index 92baabc103ac..465044b42af5 100644
@@ -46,6 +46,7 @@ enum iio_chan_type {
 	IIO_GRAVITY,
 	IIO_POSITIONRELATIVE,
 	IIO_PHASE,
+	IIO_MASSCONCENTRATION,
 };
 
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
 	IIO_MOD_VOC,
 	IIO_MOD_LIGHT_UV,
 	IIO_MOD_LIGHT_DUV,
+	IIO_MOD_PM2p5,
+	IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -0
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_GRAVITY] = "gravity",
 	[IIO_POSITIONRELATIVE] = "positionrelative",
 	[IIO_PHASE] = "phase",
+	[IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
 	[IIO_MOD_Q] = "q",
 	[IIO_MOD_CO2] = "co2",
 	[IIO_MOD_VOC] = "voc",
+	[IIO_MOD_PM2p5] = "pm2p5",
+	[IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
 	case IIO_GRAVITY:
 	case IIO_POSITIONRELATIVE:
 	case IIO_PHASE:
+	case IIO_MASSCONCENTRATION:
 		break;
 	default:
 		return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
 	case IIO_MOD_Q:
 	case IIO_MOD_CO2:
 	case IIO_MOD_VOC:
+	case IIO_MOD_PM2p5:
+	case IIO_MOD_PM10:
 		break;
 	default:
 		return false;
author maintainer
#3 lore raw

Re: [PATCH 1/3] iio: add IIO_MASSCONCENTRATION channel type

Quoted 14 lines · Jonathan Cameron
Quoted 11 lines · On Sat, 24 Nov 2018 23:14:13 +0100 Tomasz Duszynski <[email protected]> wrote:

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

Quoted 9 lines

I initially wondered why these particular numbers and why don't we provide an attribute specify this separately?

However google suggests these two are pretty much the only ones anyone worries about in pollution sensors. I ended up fairly quickly at the EPA website
https://www.epa.gov/pm-pollution/table-historical-particulate-matter-pm-national-ambient-air-quality-standards-naaqs
which tells me these two have be used since about 1987.

Usually sensors can measure more than just PM10 and PM2.5. Fairy common is measurement of PM1.0. SPS30 additionally measures PM4.0 but frankly I don't now know exactly what's the real usecase for that.

Quoted 7 lines

So I think the modifier route is the right approach here (I think we've gotten this wrong in the past such as light sensor colours where the list keeps on growing).

I would like a reference or two in the docs though to point people to these definitions.

Okay, will add something reasonable so everyone interested could get the basic idea quickly.

Quoted 114 lines

Thanks,

Jonathan

Quoted 109 lines
---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:	4.18
 Contact:	[email protected]
 Description:
 		Raw (unscaled) phase difference reading from channel Y
-		that can be processed to radians.
\ No newline at end of file
+		that can be processed to radians.
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:		/sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion:	4.21
+Contact:	[email protected]
+Description:
+		Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_GRAVITY]  = "gravity",
 	[IIO_POSITIONRELATIVE]  = "positionrelative",
 	[IIO_PHASE] = "phase",
+	[IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
 	[IIO_MOD_Q] = "q",
 	[IIO_MOD_CO2] = "co2",
 	[IIO_MOD_VOC] = "voc",
+	[IIO_MOD_PM2p5] = "pm2p5",
+	[IIO_MOD_PM10] = "pm10",
 };
 
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +3 -0
index 92baabc103ac..465044b42af5 100644
@@ -46,6 +46,7 @@ enum iio_chan_type {
 	IIO_GRAVITY,
 	IIO_POSITIONRELATIVE,
 	IIO_PHASE,
+	IIO_MASSCONCENTRATION,
 };
 
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
 	IIO_MOD_VOC,
 	IIO_MOD_LIGHT_UV,
 	IIO_MOD_LIGHT_DUV,
+	IIO_MOD_PM2p5,
+	IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -0
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_GRAVITY] = "gravity",
 	[IIO_POSITIONRELATIVE] = "positionrelative",
 	[IIO_PHASE] = "phase",
+	[IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
 	[IIO_MOD_Q] = "q",
 	[IIO_MOD_CO2] = "co2",
 	[IIO_MOD_VOC] = "voc",
+	[IIO_MOD_PM2p5] = "pm2p5",
+	[IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
 	case IIO_GRAVITY:
 	case IIO_POSITIONRELATIVE:
 	case IIO_PHASE:
+	case IIO_MASSCONCENTRATION:
 		break;
 	default:
 		return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
 	case IIO_MOD_Q:
 	case IIO_MOD_CO2:
 	case IIO_MOD_VOC:
+	case IIO_MOD_PM2p5:
+	case IIO_MOD_PM10:
 		break;
 	default:
 		return false;
#4 lore raw

Re: [PATCH 1/3] iio: add IIO_MASSCONCENTRATION channel type

In reply to Tomasz Duszynski
Quoted 67 lines · Tomasz Duszynski

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:   4.18
 Contact:       [email protected]
 Description:
                Raw (unscaled) phase difference reading from channel Y
-               that can be processed to radians.
\ No newline at end of file
+               that can be processed to radians.
+
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion: 4.21
+Contact:       [email protected]
+Description:
+               Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY]  = "gravity",
        [IIO_POSITIONRELATIVE]  = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +0 -0
index 92baabc103ac..465044b42af5 100644

include/uapi/linux/iio/types.h enum iio_chan_type { · line 49

Quoted 4 lines
include/uapi/linux/iio/types.h +1 -0
@@ -46,6 +46,7 @@ enum iio_chan_type {
        IIO_GRAVITY,
        IIO_POSITIONRELATIVE,
        IIO_PHASE,
+       IIO_MASSCONCENTRATION,

So I'm guessing IIO_CONCENTRATION can't be scaled to the micro-grams per cubic meter?

include/uapi/linux/iio/types.h enum iio_modifier { · line 91

Quoted 8 lines
include/uapi/linux/iio/types.h +1 -0
 };
 
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
        IIO_MOD_VOC,
        IIO_MOD_LIGHT_UV,
        IIO_MOD_LIGHT_DUV,
+       IIO_MOD_PM2p5,

I know this is unit of measure but the lowercase p in IIO_MOD_PM2p5 is a bit non-standard for iio defines/enum.

- Matt

Quoted 45 lines
include/uapi/linux/iio/types.h +1 -0
+       IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -1
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY] = "gravity",
        [IIO_POSITIONRELATIVE] = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_GRAVITY:
        case IIO_POSITIONRELATIVE:
        case IIO_PHASE:
+       case IIO_MASSCONCENTRATION:
                break;
        default:
                return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_MOD_Q:
        case IIO_MOD_CO2:
        case IIO_MOD_VOC:
+       case IIO_MOD_PM2p5:
+       case IIO_MOD_PM10:
                break;
        default:
                return false;
--

2.19.2

#5 lore raw

Re: [PATCH 1/3] iio: add IIO_MASSCONCENTRATION channel type

Quoted 62 lines · On Sun, 25 Nov 2018 05:51:32 -0800 Matt Ranostay <[email protected]>…
Quoted 61 lines · Tomasz Duszynski

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:   4.18
 Contact:       [email protected]
 Description:
                Raw (unscaled) phase difference reading from channel Y
-               that can be processed to radians.
\ No newline at end of file
+               that can be processed to radians.
+
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion: 4.21
+Contact:       [email protected]
+Description:
+               Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY]  = "gravity",
        [IIO_POSITIONRELATIVE]  = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
Quoted 13 lines

include/uapi/linux/iio/types.h enum iio_chan_type { · line 49

Quoted 10 lines
drivers/iio/industrialio-core.c +0 -0
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +1 -0
index 92baabc103ac..465044b42af5 100644
@@ -46,6 +46,7 @@ enum iio_chan_type {
        IIO_GRAVITY,
        IIO_POSITIONRELATIVE,
        IIO_PHASE,
+       IIO_MASSCONCENTRATION,  

So I'm guessing IIO_CONCENTRATION can't be scaled to the micro-grams per cubic meter?

Currently concentration is defined as a percentage reading of a substance (which does make me wonder if it is meant to be percentage of the volume or percentage of the mass?) Either way, if can't convert to a density measurement as it's a unit free ratio (I think).

Quoted 12 lines

include/uapi/linux/iio/types.h enum iio_modifier { · line 91

Quoted 9 lines
include/uapi/linux/iio/types.h +1 -0
 };
 
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
        IIO_MOD_VOC,
        IIO_MOD_LIGHT_UV,
        IIO_MOD_LIGHT_DUV,
+       IIO_MOD_PM2p5,  

I know this is unit of measure but the lowercase p in IIO_MOD_PM2p5 is a bit non-standard for iio defines/enum.

It is a bit odd and will get us scripted reports so maybe best to just go upper case and not worry about it.

Jonathan

Quoted 48 lines

- Matt

Quoted 45 lines
include/uapi/linux/iio/types.h +1 -0
+       IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -1
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY] = "gravity",
        [IIO_POSITIONRELATIVE] = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_GRAVITY:
        case IIO_POSITIONRELATIVE:
        case IIO_PHASE:
+       case IIO_MASSCONCENTRATION:
                break;
        default:
                return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_MOD_Q:
        case IIO_MOD_CO2:
        case IIO_MOD_VOC:
+       case IIO_MOD_PM2p5:
+       case IIO_MOD_PM10:
                break;
        default:
                return false;
--

2.19.2

#6 lore raw

Re: [PATCH 1/3] iio: add IIO_MASSCONCENTRATION channel type

Quoted 80 lines · Jonathan Cameron
Quoted 62 lines · On Sun, 25 Nov 2018 05:51:32 -0800 Matt Ranostay <[email protected]>…
Quoted 61 lines · Tomasz Duszynski

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:   4.18
 Contact:       [email protected]
 Description:
                Raw (unscaled) phase difference reading from channel Y
-               that can be processed to radians.
\ No newline at end of file
+               that can be processed to radians.
+
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion: 4.21
+Contact:       [email protected]
+Description:
+               Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY]  = "gravity",
        [IIO_POSITIONRELATIVE]  = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
Quoted 13 lines

include/uapi/linux/iio/types.h enum iio_chan_type { · line 49

Quoted 10 lines
drivers/iio/industrialio-core.c +0 -0
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +1 -0
index 92baabc103ac..465044b42af5 100644
@@ -46,6 +46,7 @@ enum iio_chan_type {
        IIO_GRAVITY,
        IIO_POSITIONRELATIVE,
        IIO_PHASE,
+       IIO_MASSCONCENTRATION,

So I'm guessing IIO_CONCENTRATION can't be scaled to the micro-grams per cubic meter?

Quoted 4 lines

Currently concentration is defined as a percentage reading of a substance (which does make me wonder if it is meant to be percentage of the volume or percentage of the mass?) Either way, if can't convert to a density measurement as it's a unit free ratio (I think).

Seems like it can be both.. guessing all the atmosphere ( CO2, VOC, etc) ones are mass/density because on how they work.
But the electro-conductivity sensor that is using IIO_CONCENTRATION channels is likely from percentage of volume.

- Matt

Quoted 66 lines
Quoted 4 lines
Quoted 4 lines
include/uapi/linux/iio/types.h +0 -0
 };
 
Quoted 9 lines

include/uapi/linux/iio/types.h enum iio_modifier { · line 91

Quoted 6 lines
include/uapi/linux/iio/types.h +1 -0
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
        IIO_MOD_VOC,
        IIO_MOD_LIGHT_UV,
        IIO_MOD_LIGHT_DUV,
+       IIO_MOD_PM2p5,

I know this is unit of measure but the lowercase p in IIO_MOD_PM2p5 is a bit non-standard for iio defines/enum.

It is a bit odd and will get us scripted reports so maybe best to just go upper case and not worry about it.

Jonathan

Quoted 48 lines

- Matt

Quoted 45 lines
include/uapi/linux/iio/types.h +1 -0
+       IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -1
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY] = "gravity",
        [IIO_POSITIONRELATIVE] = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_GRAVITY:
        case IIO_POSITIONRELATIVE:
        case IIO_PHASE:
+       case IIO_MASSCONCENTRATION:
                break;
        default:
                return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_MOD_Q:
        case IIO_MOD_CO2:
        case IIO_MOD_VOC:
+       case IIO_MOD_PM2p5:
+       case IIO_MOD_PM10:
                break;
        default:
                return false;
--

2.19.2

author maintainer
#7 lore raw

Re: [PATCH 1/3] iio: add IIO_MASSCONCENTRATION channel type

Quoted 82 lines · Matt Ranostay
Quoted 80 lines · Jonathan Cameron
Quoted 75 lines · On Sun, 25 Nov 2018 05:51:32 -0800 Matt Ranostay <[email protected]>…
Quoted 67 lines · Tomasz Duszynski

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:   4.18
 Contact:       [email protected]
 Description:
                Raw (unscaled) phase difference reading from channel Y
-               that can be processed to radians.
\ No newline at end of file
+               that can be processed to radians.
+
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion: 4.21
+Contact:       [email protected]
+Description:
+               Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY]  = "gravity",
        [IIO_POSITIONRELATIVE]  = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +0 -0
index 92baabc103ac..465044b42af5 100644

include/uapi/linux/iio/types.h enum iio_chan_type { · line 49

Quoted 4 lines
include/uapi/linux/iio/types.h +1 -0
@@ -46,6 +46,7 @@ enum iio_chan_type {
        IIO_GRAVITY,
        IIO_POSITIONRELATIVE,
        IIO_PHASE,
+       IIO_MASSCONCENTRATION,

So I'm guessing IIO_CONCENTRATION can't be scaled to the micro-grams per cubic meter?

Quoted 7 lines
Quoted 4 lines

Currently concentration is defined as a percentage reading of a substance (which does make me wonder if it is meant to be percentage of the volume or percentage of the mass?) Either way, if can't convert to a density measurement as it's a unit free ratio (I think).

Seems like it can be both.. guessing all the atmosphere ( CO2, VOC, etc) ones are mass/density because on how they work.

Hmm, but still percentage was picked up for IIO_CONCENTRATION which does really match PM expectations. Perhaps if units were sticked to modifiers it whould be easier to reuse that.

Quoted 71 lines

But the electro-conductivity sensor that is using IIO_CONCENTRATION channels is likely from percentage of volume.

- Matt

Quoted 66 lines
Quoted 4 lines
Quoted 4 lines
include/uapi/linux/iio/types.h +0 -0
 };
 
Quoted 9 lines

include/uapi/linux/iio/types.h enum iio_modifier { · line 91

Quoted 6 lines
include/uapi/linux/iio/types.h +1 -0
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
        IIO_MOD_VOC,
        IIO_MOD_LIGHT_UV,
        IIO_MOD_LIGHT_DUV,
+       IIO_MOD_PM2p5,

I know this is unit of measure but the lowercase p in IIO_MOD_PM2p5 is a bit non-standard for iio defines/enum.

It is a bit odd and will get us scripted reports so maybe best to just go upper case and not worry about it.

Jonathan

Quoted 48 lines

- Matt

Quoted 45 lines
include/uapi/linux/iio/types.h +1 -0
+       IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -1
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY] = "gravity",
        [IIO_POSITIONRELATIVE] = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_GRAVITY:
        case IIO_POSITIONRELATIVE:
        case IIO_PHASE:
+       case IIO_MASSCONCENTRATION:
                break;
        default:
                return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_MOD_Q:
        case IIO_MOD_CO2:
        case IIO_MOD_VOC:
+       case IIO_MOD_PM2p5:
+       case IIO_MOD_PM10:
                break;
        default:
                return false;
--

2.19.2

#8 lore raw

Re: [PATCH 1/3] iio: add IIO_MASSCONCENTRATION channel type

Quoted 88 lines · On Sun, 25 Nov 2018 16:44:23 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 87 lines · Matt Ranostay
Quoted 84 lines · Jonathan Cameron
Quoted 62 lines · On Sun, 25 Nov 2018 05:51:32 -0800 Matt Ranostay <[email protected]>…
Quoted 61 lines · Tomasz Duszynski

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:   4.18
 Contact:       [email protected]
 Description:
                Raw (unscaled) phase difference reading from channel Y
-               that can be processed to radians.
\ No newline at end of file
+               that can be processed to radians.
+
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion: 4.21
+Contact:       [email protected]
+Description:
+               Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY]  = "gravity",
        [IIO_POSITIONRELATIVE]  = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
Quoted 13 lines

include/uapi/linux/iio/types.h enum iio_chan_type { · line 49

Quoted 10 lines
drivers/iio/industrialio-core.c +0 -0
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +1 -0
index 92baabc103ac..465044b42af5 100644
@@ -46,6 +46,7 @@ enum iio_chan_type {
        IIO_GRAVITY,
        IIO_POSITIONRELATIVE,
        IIO_PHASE,
+       IIO_MASSCONCENTRATION,  

So I'm guessing IIO_CONCENTRATION can't be scaled to the micro-grams per cubic meter?

Currently concentration is defined as a percentage reading of a substance (which does make me wonder if it is meant to be percentage of the volume or percentage of the mass?) Either way, if can't convert to a density measurement as it's a unit free ratio (I think).

Quoted 6 lines
Quoted 2 lines

Seems like it can be both.. guessing all the atmosphere ( CO2, VOC, etc) ones are mass/density because on how they work.

Hmm, but still percentage was picked up for IIO_CONCENTRATION which does really match PM expectations. Perhaps if units were sticked to modifiers it whould be easier to reuse that.

It gets very messy if we start doing that modifiers, I'd rather stick to different channel types. We already do this with other channel types position / positionrelative for example.

It isn't as though it's actually possible to convert between the two without knowing what the particles actually are...

Jonathan

Quoted 72 lines
Quoted 72 lines

But the electro-conductivity sensor that is using IIO_CONCENTRATION channels is likely from percentage of volume.

- Matt

Quoted 66 lines
Quoted 4 lines
Quoted 4 lines
include/uapi/linux/iio/types.h +0 -0
 };
 
Quoted 9 lines

include/uapi/linux/iio/types.h enum iio_modifier { · line 91

Quoted 6 lines
include/uapi/linux/iio/types.h +1 -0
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
        IIO_MOD_VOC,
        IIO_MOD_LIGHT_UV,
        IIO_MOD_LIGHT_DUV,
+       IIO_MOD_PM2p5,  

I know this is unit of measure but the lowercase p in IIO_MOD_PM2p5 is a bit non-standard for iio defines/enum.

It is a bit odd and will get us scripted reports so maybe best to just go upper case and not worry about it.

Jonathan

Quoted 48 lines

- Matt

Quoted 45 lines
include/uapi/linux/iio/types.h +1 -0
+       IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -1
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY] = "gravity",
        [IIO_POSITIONRELATIVE] = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_GRAVITY:
        case IIO_POSITIONRELATIVE:
        case IIO_PHASE:
+       case IIO_MASSCONCENTRATION:
                break;
        default:
                return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_MOD_Q:
        case IIO_MOD_CO2:
        case IIO_MOD_VOC:
+       case IIO_MOD_PM2p5:
+       case IIO_MOD_PM10:
                break;
        default:
                return false;
--

2.19.2

#9 lore raw

Re: [PATCH 1/3] iio: add IIO_MASSCONCENTRATION channel type

Quoted 99 lines · Jonathan Cameron
Quoted 88 lines · On Sun, 25 Nov 2018 16:44:23 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 87 lines · Matt Ranostay
Quoted 84 lines · Jonathan Cameron
Quoted 62 lines · On Sun, 25 Nov 2018 05:51:32 -0800 Matt Ranostay <[email protected]>…
Quoted 61 lines · Tomasz Duszynski

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:   4.18
 Contact:       [email protected]
 Description:
                Raw (unscaled) phase difference reading from channel Y
-               that can be processed to radians.
\ No newline at end of file
+               that can be processed to radians.
+
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion: 4.21
+Contact:       [email protected]
+Description:
+               Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY]  = "gravity",
        [IIO_POSITIONRELATIVE]  = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
Quoted 13 lines

include/uapi/linux/iio/types.h enum iio_chan_type { · line 49

Quoted 10 lines
drivers/iio/industrialio-core.c +0 -0
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +1 -0
index 92baabc103ac..465044b42af5 100644
@@ -46,6 +46,7 @@ enum iio_chan_type {
        IIO_GRAVITY,
        IIO_POSITIONRELATIVE,
        IIO_PHASE,
+       IIO_MASSCONCENTRATION,

So I'm guessing IIO_CONCENTRATION can't be scaled to the micro-grams per cubic meter?

Currently concentration is defined as a percentage reading of a substance (which does make me wonder if it is meant to be percentage of the volume or percentage of the mass?) Either way, if can't convert to a density measurement as it's a unit free ratio (I think).

Quoted 6 lines
Quoted 2 lines

Seems like it can be both.. guessing all the atmosphere ( CO2, VOC, etc) ones are mass/density because on how they work.

Hmm, but still percentage was picked up for IIO_CONCENTRATION which does really match PM expectations. Perhaps if units were sticked to modifiers it whould be easier to reuse that.

Quoted 6 lines

It gets very messy if we start doing that modifiers, I'd rather stick to different channel types. We already do this with other channel types position / positionrelative for example.

It isn't as though it's actually possible to convert between the two without knowing what the particles actually are...

Wondering if some of the current chemical sensors in the tree should be switched to IIO_MASSCONCENTRATION, and only one I can think is truly by volume is the electro-conductivity sensor (part of the atlas-ph-sensor.c)

- Matt

Quoted 75 lines

Jonathan

Quoted 72 lines
Quoted 72 lines

But the electro-conductivity sensor that is using IIO_CONCENTRATION channels is likely from percentage of volume.

- Matt

Quoted 66 lines
Quoted 4 lines
Quoted 4 lines
include/uapi/linux/iio/types.h +0 -0
 };
 
Quoted 9 lines

include/uapi/linux/iio/types.h enum iio_modifier { · line 91

Quoted 6 lines
include/uapi/linux/iio/types.h +1 -0
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
        IIO_MOD_VOC,
        IIO_MOD_LIGHT_UV,
        IIO_MOD_LIGHT_DUV,
+       IIO_MOD_PM2p5,

I know this is unit of measure but the lowercase p in IIO_MOD_PM2p5 is a bit non-standard for iio defines/enum.

It is a bit odd and will get us scripted reports so maybe best to just go upper case and not worry about it.

Jonathan

Quoted 48 lines

- Matt

Quoted 45 lines
include/uapi/linux/iio/types.h +1 -0
+       IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -1
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY] = "gravity",
        [IIO_POSITIONRELATIVE] = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_GRAVITY:
        case IIO_POSITIONRELATIVE:
        case IIO_PHASE:
+       case IIO_MASSCONCENTRATION:
                break;
        default:
                return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_MOD_Q:
        case IIO_MOD_CO2:
        case IIO_MOD_VOC:
+       case IIO_MOD_PM2p5:
+       case IIO_MOD_PM10:
                break;
        default:
                return false;
--

2.19.2

author maintainer
#10 lore raw

Re: [PATCH 1/3] iio: add IIO_MASSCONCENTRATION channel type

In reply to Jonathan Cameron
Quoted 79 lines · Jonathan Cameron
Quoted 62 lines · On Sun, 25 Nov 2018 05:51:32 -0800 Matt Ranostay <[email protected]>…
Quoted 61 lines · Tomasz Duszynski

Measuring particulate matter in ug / m3 (micro-grams per cubic meter) is de facto standard. Existing air quality sensors usually follow this convention and are capable of returning measurements using this unit.

IIO currently does not offer suitable channel type for this type of measurements hence this patch adds this.

In addition, two modifiers are introduced used for distinguishing between coarse (PM10) and fine particles (PM2p5) measurements, i.e IIO_MOD_PM10 and IIO_MOD_PM2p5.

---
 Documentation/ABI/testing/sysfs-bus-iio | 11 ++++++++++-
 drivers/iio/industrialio-core.c         |  3 +++
 include/uapi/linux/iio/types.h          |  3 +++
 tools/iio/iio_event_monitor.c           |  6 ++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +10 -1
index 8127a08e366d..ce0ed140660d 100644
@@ -1684,4 +1684,13 @@ KernelVersion:   4.18
 Contact:       [email protected]
 Description:
                Raw (unscaled) phase difference reading from channel Y
-               that can be processed to radians.
\ No newline at end of file
+               that can be processed to radians.
+
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm2p5_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentration_pm10_input
+What:          /sys/bus/iio/devices/iio:deviceX/in_massconcentrationY_pm10_input
+KernelVersion: 4.21
+Contact:       [email protected]
+Description:
+               Mass concentration reading of particulate matter in ug / m3.
drivers/iio/industrialio-core.c +3 -0
index a062cfddc5af..2a9ef600c1fb 100644
@@ -87,6 +87,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY]  = "gravity",
        [IIO_POSITIONRELATIVE]  = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -127,6 +128,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
Quoted 13 lines

include/uapi/linux/iio/types.h enum iio_chan_type { · line 49

Quoted 10 lines
drivers/iio/industrialio-core.c +0 -0
 /* relies on pairs of these shared then separate */
include/uapi/linux/iio/types.h +1 -0
index 92baabc103ac..465044b42af5 100644
@@ -46,6 +46,7 @@ enum iio_chan_type {
        IIO_GRAVITY,
        IIO_POSITIONRELATIVE,
        IIO_PHASE,
+       IIO_MASSCONCENTRATION,

So I'm guessing IIO_CONCENTRATION can't be scaled to the micro-grams per cubic meter?

Quoted 5 lines

Currently concentration is defined as a percentage reading of a substance (which does make me wonder if it is meant to be percentage of the volume or percentage of the mass?) Either way, if can't convert to a density measurement as it's a unit free ratio (I think).

And this is the main reason behind introducing a new channel type.

Quoted 12 lines
Quoted 12 lines

include/uapi/linux/iio/types.h enum iio_modifier { · line 91

Quoted 9 lines
include/uapi/linux/iio/types.h +1 -0
 };
 
 enum iio_modifier {
@@ -87,6 +88,8 @@ enum iio_modifier {
        IIO_MOD_VOC,
        IIO_MOD_LIGHT_UV,
        IIO_MOD_LIGHT_DUV,
+       IIO_MOD_PM2p5,

I know this is unit of measure but the lowercase p in IIO_MOD_PM2p5 is a bit non-standard for iio defines/enum.

Quoted 3 lines

It is a bit odd and will get us scripted reports so maybe best to just go upper case and not worry about it.

Initially I came up with IIO_MOD_PM2_5 but eventually replaced underscore with 'p' meaning 'decimal point'. Anyway, I am okay with the suggested change.

Quoted 50 lines

Jonathan

Quoted 48 lines

- Matt

Quoted 45 lines
include/uapi/linux/iio/types.h +1 -0
+       IIO_MOD_PM10,
 };
 
 enum iio_event_type {
tools/iio/iio_event_monitor.c +6 -1
index ac2de6b7e89f..f0fcfeddba2b 100644
@@ -60,6 +60,7 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_GRAVITY] = "gravity",
        [IIO_POSITIONRELATIVE] = "positionrelative",
        [IIO_PHASE] = "phase",
+       [IIO_MASSCONCENTRATION] = "massconcentration",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -115,6 +116,8 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_Q] = "q",
        [IIO_MOD_CO2] = "co2",
        [IIO_MOD_VOC] = "voc",
+       [IIO_MOD_PM2p5] = "pm2p5",
+       [IIO_MOD_PM10] = "pm10",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -156,6 +159,7 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_GRAVITY:
        case IIO_POSITIONRELATIVE:
        case IIO_PHASE:
+       case IIO_MASSCONCENTRATION:
                break;
        default:
                return false;
@@ -200,6 +204,8 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_MOD_Q:
        case IIO_MOD_CO2:
        case IIO_MOD_VOC:
+       case IIO_MOD_PM2p5:
+       case IIO_MOD_PM10:
                break;
        default:
                return false;
--

2.19.2

author maintainer
#11 lore raw

PATCH 2/3 iio: chemical: add support for Sensirion SPS30 sensor

In reply to Tomasz Duszynski

Add support for Sensirion SPS30 particulate matter sensor.

---
 drivers/iio/chemical/Kconfig  |  11 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sps30.c  | 359 ++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/chemical/sps30.c
drivers/iio/chemical/Kconfig +11 -0
index b8e005be4f87..40057ecf8130 100644
@@ -61,6 +61,17 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config SPS30
+	tristate "SPS30 Particulate Matter sensor"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build support for the Sensirion SPS30 Particulate
+	  Matter sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called sps30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..9f42f4252151 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SPS30) += sps30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sps30.c new file +359 -0

359 lines not on this page · raw message

Signature · 1 line
2.19.2
#12 lore raw

Re: [PATCH 2/3] iio: chemical: add support for Sensirion SPS30 sensor

Quoted 3 lines · On Sat, 24 Nov 2018 23:14:14 +0100 Tomasz Duszynski <[email protected]> wrote:

Add support for Sensirion SPS30 particulate matter sensor.

A few things inline.

I'm particularly curious as to why we are ignoring two of the particle sizes that the sensor seems to capture?

Also, a 'potential' race in remove that I'd like us to make 'obviously' correct rather than requiring hard thought on whether it would be a problem.

Thanks,

Jonathan

Quoted 80 lines
---
 drivers/iio/chemical/Kconfig  |  11 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sps30.c  | 359 ++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/chemical/sps30.c
drivers/iio/chemical/Kconfig +11 -0
index b8e005be4f87..40057ecf8130 100644
@@ -61,6 +61,17 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config SPS30
+	tristate "SPS30 Particulate Matter sensor"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build support for the Sensirion SPS30 Particulate
+	  Matter sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called sps30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..9f42f4252151 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SPS30) += sps30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sps30.c new file +35 -0
new file mode 100644
index 000000000000..bf802621ae7f
@@ -0,0 +1,359 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Sensirion SPS30 Particulate Matter sensor driver
+ *
+ * Copyright (c) Tomasz Duszynski <[email protected]>
+ *
+ * I2C slave address: 0x69
+ *
+ * TODO:
+ *  - support for turning on fan cleaning
+ *  - support for reading/setting auto cleaning interval
+ */
+
+#define pr_fmt(fmt) "sps30: " fmt
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/module.h>
+
+#define SPS30_CRC8_POLYNOMIAL 0x31
+
+/* SPS30 commands */
+#define SPS30_START_MEAS 0x0010
+#define SPS30_STOP_MEAS 0x0104
+#define SPS30_RESET 0xd304
+#define SPS30_READ_DATA_READY_FLAG 0x0202
+#define SPS30_READ_DATA 0x0300
+#define SPS30_READ_SERIAL 0xD033
+

drivers/iio/chemical/sps30.c line 45

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+#define SPS30_CHAN(_index, _mod) { \
+	.type = IIO_MASSCONCENTRATION, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _mod, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
+	.scan_index = _index, \
+	.scan_type = { \
+		.sign = 'u', \
+		.realbits = 12, \
+		.storagebits = 32, \

That is unusual. Why do we need 32 bits to store a 12 bit value? 16 should be plenty. It'll make a difference to the buffer sizes if people just want to stream data and don't care about timestamps as it'll halve the memory usage.

Also, convention has always been to got for next 8,16,32,64 above the realbits if there isn't a reason not to (shifting etc)

How did we end up with 12 bits off the floating point conversion anyway? Needs some docs.

Quoted 9 lines
drivers/iio/chemical/sps30.c new file +9 -0
+		.endianness = IIO_CPU, \
+	}, \
+}
+
+enum {
+	PM1p0, /* just a placeholder */
+	PM2p5,
+	PM4p0, /* just a placeholder */
+	PM10,

drivers/iio/chemical/sps30.c line 64

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+};
+
+struct sps30_state {
+	struct i2c_client *client;
+	/* guards against concurrent access to sensor registers */
+	struct mutex lock;
+};
+
+DECLARE_CRC8_TABLE(sps30_crc8_table);
+

I think you are fine locking wise, but it would be good to add a note on what locks are expected to be held on entering this function.

Without locks it's obviously very racey!

Quoted 38 lines
drivers/iio/chemical/sps30.c new file +38 -0
+static int sps30_write_then_read(struct sps30_state *state, u8 *buf,
+				 int buf_size, u8 *data, int data_size)
+{
+	/* every two received data bytes are checksummed */
+	u8 tmp[data_size + data_size / 2];
+	int ret, i;
+
+	/*
+	 * Sensor does not support repeated start so instead of
+	 * sending two i2c messages in a row we just send one by one.
+	 */
+	ret = i2c_master_send(state->client, buf, buf_size);
+	if (ret != buf_size)
+		return ret < 0 ? ret : -EIO;
+
+	if (!data)
+		return 0;
+
+	ret = i2c_master_recv(state->client, tmp, sizeof(tmp));
+	if (ret != sizeof(tmp))
+		return ret < 0 ? ret : -EIO;
+
+	for (i = 0; i < sizeof(tmp); i += 3) {
+		u8 crc = crc8(sps30_crc8_table, &tmp[i], 2, CRC8_INIT_VALUE);
+
+		if (crc != tmp[i + 2]) {
+			dev_err(&state->client->dev,
+				"data integrity check failed\n");
+			return -EIO;
+		}
+
+		*data++ = tmp[i];
+		*data++ = tmp[i + 1];
+	}
+
+	return 0;
+}
+

drivers/iio/chemical/sps30.c line 112

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
+{
+	/* depending on the command up to 3 bytes may be needed for argument */
+	u8 buf[sizeof(cmd) + 3] = { cmd >> 8, cmd };
+
+	switch (cmd) {
+	case SPS30_START_MEAS:
+		buf[2] = 0x03;
+		buf[3] = 0x00;
+		buf[4] = 0xac; /* precomputed crc */

Could you put that in code and let the compiler do the pre compute? Might be a little more elegant.

Quoted 7 lines
drivers/iio/chemical/sps30.c new file +7 -0
+		return sps30_write_then_read(state, buf, 5, NULL, 0);
+	case SPS30_STOP_MEAS:
+	case SPS30_RESET:
+		return sps30_write_then_read(state, buf, 2, NULL, 0);
+	case SPS30_READ_DATA_READY_FLAG:
+	case SPS30_READ_DATA:
+	case SPS30_READ_SERIAL:

drivers/iio/chemical/sps30.c line 129

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+		return sps30_write_then_read(state, buf, 2, data, size);
+	default:
+		return -EINVAL;
+	};
+}
+
+static int sps30_ieee754_to_int(const u8 *data)
+{
+	u32 val = ((u32)data[0] << 24) | ((u32)data[1] << 16) |
+		  ((u32)data[2] << 8) | (u32)data[3],

Have a separate
u32 mantissa = (val << 9) >> 9 line.
What is this next line actually doing? Kind of looks like a mask? If so just mask it with & as more readable.

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+	    mantissa = (val << 9) >> 9;
+	int exp = (val >> 23) - 127;
+
+	if (!exp && !mantissa)
+		return 0;
+
+	if (exp < 0)
+		return 0;

drivers/iio/chemical/sps30.c line 147

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	return (1 << exp) + (mantissa >> (23 - exp));
+}
+
+static int sps30_do_meas(struct sps30_state *state, int *pm2p5, int *pm10)
+{
+	/*
+	 * Internally sensor stores measurements in a following manner:
+	 *
+	 * PM1p0: upper two bytes, crc8, lower two bytes, crc8

So there are other particle sizes that this sensor reads? Why are we mapping them down to just the two channel types?

Quoted 27 lines
drivers/iio/chemical/sps30.c new file +27 -0
+	 * PM2p5: upper two bytes, crc8, lower two bytes, crc8
+	 * PM4p0: upper two bytes, crc8, lower two bytes, crc8
+	 * PM10:  upper two bytes, crc8, lower two bytes, crc8
+	 *
+	 * What follows next are number concentration measurements and
+	 * typical particle size measurement.
+	 *
+	 * Once data is read from sensor crc bytes are stripped off
+	 * hence we need 16 bytes of buffer space.
+	 */
+	int ret, tries = 5;
+	u8 buf[16];
+
+	while (tries--) {
+		ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, buf, 2);
+		if (ret)
+			return -EIO;
+
+		/* new measurements ready to be read */
+		if (buf[1] == 1)
+			break;
+
+		usleep_range(300000, 400000);
+	}
+
+	if (!tries)
+		return -ETIMEDOUT;

drivers/iio/chemical/sps30.c line 184

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	ret = sps30_do_cmd(state, SPS30_READ_DATA, buf, sizeof(buf));
+	if (ret)
+		return ret;
+
+	/*
+	 * All measurements come in IEEE754 single precision floating point
+	 * format but sensor itself is not precise enough (-+ 10% error)
+	 * to take full advantage of it. Hence converting result to int
+	 * to keep things simple.

Interesting. We have talked about allowing floating point formats for sensors the provide them. Would that be beneficial here?

Quoted 51 lines
drivers/iio/chemical/sps30.c new file +51 -0
+	 */
+	*pm2p5 = sps30_ieee754_to_int(&buf[PM2p5 * 4]);
+	*pm10 = sps30_ieee754_to_int(&buf[PM10 * 4]);
+
+	return 0;
+}
+
+static irqreturn_t sps30_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct sps30_state *state = iio_priv(indio_dev);
+	u32 buf[4]; /* PM2p5, PM10, timestamp */
+	int ret;
+
+	mutex_lock(&state->lock);
+	ret = sps30_do_meas(state, &buf[0], &buf[1]);
+	mutex_unlock(&state->lock);
+	if (ret < 0)
+		goto err;
+
+	iio_push_to_buffers_with_timestamp(indio_dev, buf,
+					   iio_get_time_ns(indio_dev));
+err:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int sps30_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct sps30_state *state = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		switch (chan->type) {
+		case IIO_MASSCONCENTRATION:
+			mutex_lock(&state->lock);
+			switch (chan->channel2) {
+			case IIO_MOD_PM2p5:
+				ret = sps30_do_meas(state, val, val2);
+				break;
+			case IIO_MOD_PM10:
+				ret = sps30_do_meas(state, val2, val);
+				break;
+			default:
+				break;
+			}

drivers/iio/chemical/sps30.c line 245

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+			mutex_unlock(&state->lock);
+			if (ret)
+				return ret;
+
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+		break;
+	default:

You can't get here. Is this a warning suppression? If so just add a comment saying so to prevent it being removed by someone else later.

Quoted 9 lines
drivers/iio/chemical/sps30.c new file +8 -0
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info sps30_info = {
+	.read_raw = sps30_read_raw,
+};
+

From a readability point of view, it would be helpful to pull

the macro SPS30_CHAN down here in the code so we don't need to go jumping around to check what it is doing.

Quoted 38 lines
drivers/iio/chemical/sps30.c new file +38 -0
+static const struct iio_chan_spec sps30_channels[] = {
+	SPS30_CHAN(0, PM2p5),
+	SPS30_CHAN(1, PM10),
+	IIO_CHAN_SOFT_TIMESTAMP(2),
+};
+
+static const unsigned long sps30_scan_masks[] = { 0x03, 0x00 };
+
+static int sps30_probe(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev;
+	struct sps30_state *state;
+	u8 buf[32] = { };
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	state->client = client;
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sps30_info;
+	indio_dev->name = client->name;
+	indio_dev->channels = sps30_channels;
+	indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->available_scan_masks = sps30_scan_masks;
+
+	mutex_init(&state->lock);
+	crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
+
+	/*
+	 * Power-on-reset causes sensor to produce some glitch on i2c bus

drivers/iio/chemical/sps30.c line 301

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+	 * and some controllers end up in error state. Recover simply
+	 * by placing something on the bus.
+	 */
+	ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
+	if (ret) {
+		dev_err(&client->dev, "failed to reset device\n");
+		return ret;
+	}
+	usleep_range(2500000, 3500000);
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);

Talk me through this logic. We stop it then pretty much immediately start it again? Needs a comment.

drivers/iio/chemical/sps30.c line 311

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
+	if (ret) {
+		dev_err(&client->dev, "failed to read serial number\n");
+		return ret;
+	}
+	dev_info(&client->dev, "serial number: %s\n", buf);
+
+	ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
+	if (ret) {

This is not unwound on error. See comment on remove race below.

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+		dev_err(&client->dev, "failed to start measurement\n");
+		return ret;
+	}
+
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      sps30_trigger_handler, NULL);
+	if (ret)
+		return ret;

drivers/iio/chemical/sps30.c line 329

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
+
+static int sps30_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sps30_state *state = iio_priv(indio_dev);
+
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);

This looks like a race to me.

You turn off the measurements before removing the interface to them. It's certainly not in the 'obviously' right category.

As such I would either use a devm action to do this stop, or not use the managed interfaces for everything in probe after the equivalent start.
The devm_add_action_or_reset would be the cleanest option I think.. Will also fix the cleanup on error in probe.

Quoted 30 lines
drivers/iio/chemical/sps30.c new file +30 -0
+
+	return 0;
+}
+
+static const struct i2c_device_id sps30_id[] = {
+	{ "sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, sps30_id);
+
+static const struct of_device_id sps30_of_match[] = {
+	{ .compatible = "sensirion,sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, sps30_of_match);
+
+static struct i2c_driver sps30_driver = {
+	.driver = {
+		.name = "sps30",
+		.of_match_table = sps30_of_match,
+	},
+	.id_table = sps30_id,
+	.probe_new = sps30_probe,
+	.remove = sps30_remove,
+};
+module_i2c_driver(sps30_driver);
+
+MODULE_AUTHOR("Tomasz Duszynski <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor driver");
+MODULE_LICENSE("GPL v2");
author maintainer
#13 lore raw

Re: [PATCH 2/3] iio: chemical: add support for Sensirion SPS30 sensor

Quoted 11 lines · Jonathan Cameron
Quoted 3 lines · On Sat, 24 Nov 2018 23:14:14 +0100 Tomasz Duszynski <[email protected]> wrote:

Add support for Sensirion SPS30 particulate matter sensor.

A few things inline.

I'm particularly curious as to why we are ignoring two of the particle sizes that the sensor seems to capture?

I was thinking about adding first the most common ones i.e PM2.5 and PM10 and the rest of them later on if there's a particular need.

On the other hand I can add PM1.0 and PM4.0 now so that we have everything in place once new dust sensors appear on the market.

It's seems reasonable to assume they will measure the very same subset of particulates.

Quoted 95 lines

Also, a 'potential' race in remove that I'd like us to make 'obviously' correct rather than requiring hard thought on whether it would be a problem.

Thanks,

Jonathan

Quoted 87 lines
---
 drivers/iio/chemical/Kconfig  |  11 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sps30.c  | 359 ++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/chemical/sps30.c
drivers/iio/chemical/Kconfig +11 -0
index b8e005be4f87..40057ecf8130 100644
@@ -61,6 +61,17 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config SPS30
+	tristate "SPS30 Particulate Matter sensor"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build support for the Sensirion SPS30 Particulate
+	  Matter sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called sps30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..9f42f4252151 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SPS30) += sps30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sps30.c new file +42 -0
new file mode 100644
index 000000000000..bf802621ae7f
@@ -0,0 +1,359 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Sensirion SPS30 Particulate Matter sensor driver
+ *
+ * Copyright (c) Tomasz Duszynski <[email protected]>
+ *
+ * I2C slave address: 0x69
+ *
+ * TODO:
+ *  - support for turning on fan cleaning
+ *  - support for reading/setting auto cleaning interval
+ */
+
+#define pr_fmt(fmt) "sps30: " fmt
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/module.h>
+
+#define SPS30_CRC8_POLYNOMIAL 0x31
+
+/* SPS30 commands */
+#define SPS30_START_MEAS 0x0010
+#define SPS30_STOP_MEAS 0x0104
+#define SPS30_RESET 0xd304
+#define SPS30_READ_DATA_READY_FLAG 0x0202
+#define SPS30_READ_DATA 0x0300
+#define SPS30_READ_SERIAL 0xD033
+
+#define SPS30_CHAN(_index, _mod) { \
+	.type = IIO_MASSCONCENTRATION, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _mod, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
+	.scan_index = _index, \
+	.scan_type = { \
Quoted 8 lines

drivers/iio/chemical/sps30.c line 45

Quoted 3 lines
drivers/iio/chemical/sps30.c new file +3 -0
+		.sign = 'u', \
+		.realbits = 12, \
+		.storagebits = 32, \

That is unusual. Why do we need 32 bits to store a 12 bit value? 16 should be plenty. It'll make a difference to the buffer sizes if people just want to stream data and don't care about timestamps as it'll halve the memory usage.

Right, I could have picked up a petter values. But I've got at least one valid reason for doing that. Sensor datasheet specifies 0-1000 measurement range but simple tests showed that SPS30 can measure way beyond that. Interestingly, measurements are consistent with third party sensors.

So having 12/32 bit setting, especially 32 storagebits protects against future changes in datasheet (which is btw preliminary) i.e updating measurement range.

But, I guess that 16 for real and storage bits should be more that enough.

Quoted 6 lines

Also, convention has always been to got for next 8,16,32,64 above the realbits if there isn't a reason not to (shifting etc)

How did we end up with 12 bits off the floating point conversion anyway? Needs some docs.

Matter of simple tests. I was able to trigger measurements of over 3000 ug/m3.

Quoted 25 lines
Quoted 11 lines
drivers/iio/chemical/sps30.c new file +9 -0
+		.endianness = IIO_CPU, \
+	}, \
+}
+
+enum {
+	PM1p0, /* just a placeholder */
+	PM2p5,
+	PM4p0, /* just a placeholder */
+	PM10,

drivers/iio/chemical/sps30.c line 64

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+};
+
+struct sps30_state {
+	struct i2c_client *client;
+	/* guards against concurrent access to sensor registers */
+	struct mutex lock;
+};
+
+DECLARE_CRC8_TABLE(sps30_crc8_table);
+

I think you are fine locking wise, but it would be good to add a note on what locks are expected to be held on entering this function.

Quoted 2 lines

Without locks it's obviously very racey!

Understood.

Quoted 43 lines
Quoted 43 lines
drivers/iio/chemical/sps30.c new file +43 -0
+static int sps30_write_then_read(struct sps30_state *state, u8 *buf,
+				 int buf_size, u8 *data, int data_size)
+{
+	/* every two received data bytes are checksummed */
+	u8 tmp[data_size + data_size / 2];
+	int ret, i;
+
+	/*
+	 * Sensor does not support repeated start so instead of
+	 * sending two i2c messages in a row we just send one by one.
+	 */
+	ret = i2c_master_send(state->client, buf, buf_size);
+	if (ret != buf_size)
+		return ret < 0 ? ret : -EIO;
+
+	if (!data)
+		return 0;
+
+	ret = i2c_master_recv(state->client, tmp, sizeof(tmp));
+	if (ret != sizeof(tmp))
+		return ret < 0 ? ret : -EIO;
+
+	for (i = 0; i < sizeof(tmp); i += 3) {
+		u8 crc = crc8(sps30_crc8_table, &tmp[i], 2, CRC8_INIT_VALUE);
+
+		if (crc != tmp[i + 2]) {
+			dev_err(&state->client->dev,
+				"data integrity check failed\n");
+			return -EIO;
+		}
+
+		*data++ = tmp[i];
+		*data++ = tmp[i + 1];
+	}
+
+	return 0;
+}
+
+static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
+{
+	/* depending on the command up to 3 bytes may be needed for argument */
+	u8 buf[sizeof(cmd) + 3] = { cmd >> 8, cmd };
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 112

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+	switch (cmd) {
+	case SPS30_START_MEAS:
+		buf[2] = 0x03;
+		buf[3] = 0x00;
+		buf[4] = 0xac; /* precomputed crc */

Could you put that in code and let the compiler do the pre compute? Might be a little more elegant.

Okay.

Quoted 13 lines
Quoted 13 lines
drivers/iio/chemical/sps30.c new file +13 -0
+		return sps30_write_then_read(state, buf, 5, NULL, 0);
+	case SPS30_STOP_MEAS:
+	case SPS30_RESET:
+		return sps30_write_then_read(state, buf, 2, NULL, 0);
+	case SPS30_READ_DATA_READY_FLAG:
+	case SPS30_READ_DATA:
+	case SPS30_READ_SERIAL:
+		return sps30_write_then_read(state, buf, 2, data, size);
+	default:
+		return -EINVAL;
+	};
+}
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 129

Quoted 4 lines
drivers/iio/chemical/sps30.c new file +4 -0
+static int sps30_ieee754_to_int(const u8 *data)
+{
+	u32 val = ((u32)data[0] << 24) | ((u32)data[1] << 16) |
+		  ((u32)data[2] << 8) | (u32)data[3],

Have a separate
u32 mantissa = (val << 9) >> 9 line.
What is this next line actually doing? Kind of looks like a mask? If so just mask it with & as more readable.

Do you mean mantissa? We want to get rid of sign bit and 8 exponent bits.

Quoted 13 lines
Quoted 13 lines
drivers/iio/chemical/sps30.c new file +13 -0
+	    mantissa = (val << 9) >> 9;
+	int exp = (val >> 23) - 127;
+
+	if (!exp && !mantissa)
+		return 0;
+
+	if (exp < 0)
+		return 0;
+
+	return (1 << exp) + (mantissa >> (23 - exp));
+}
+
+static int sps30_do_meas(struct sps30_state *state, int *pm2p5, int *pm10)
Quoted 9 lines

drivers/iio/chemical/sps30.c line 147

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+{
+	/*
+	 * Internally sensor stores measurements in a following manner:
+	 *
+	 * PM1p0: upper two bytes, crc8, lower two bytes, crc8

So there are other particle sizes that this sensor reads? Why are we mapping them down to just the two channel types?

As said before, introducing PM1.0 and PM4.0 readings seems a reasonable option.

Quoted 32 lines
Quoted 32 lines
drivers/iio/chemical/sps30.c new file +32 -0
+	 * PM2p5: upper two bytes, crc8, lower two bytes, crc8
+	 * PM4p0: upper two bytes, crc8, lower two bytes, crc8
+	 * PM10:  upper two bytes, crc8, lower two bytes, crc8
+	 *
+	 * What follows next are number concentration measurements and
+	 * typical particle size measurement.
+	 *
+	 * Once data is read from sensor crc bytes are stripped off
+	 * hence we need 16 bytes of buffer space.
+	 */
+	int ret, tries = 5;
+	u8 buf[16];
+
+	while (tries--) {
+		ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, buf, 2);
+		if (ret)
+			return -EIO;
+
+		/* new measurements ready to be read */
+		if (buf[1] == 1)
+			break;
+
+		usleep_range(300000, 400000);
+	}
+
+	if (!tries)
+		return -ETIMEDOUT;
+
+	ret = sps30_do_cmd(state, SPS30_READ_DATA, buf, sizeof(buf));
+	if (ret)
+		return ret;
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 184

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+	/*
+	 * All measurements come in IEEE754 single precision floating point
+	 * format but sensor itself is not precise enough (-+ 10% error)
+	 * to take full advantage of it. Hence converting result to int
+	 * to keep things simple.

Interesting. We have talked about allowing floating point formats for sensors the provide them. Would that be beneficial here?

I recall this was discussed once but unfortunately do not remember final conclusion. Anyway, in case of this device datasheet states that readings have maximum error of -+10% in given range which makes me think that using floats here is an overkill.

Example, reading of 1000.123412ug/m3 has error of -+100ug/m3. Does it really matter if care about fractional part?

Just out of curiosity, if IIO gets support for floats would it be problematic to to add extra channel returning measurements in floats? Or it rather will not fly..

Quoted 56 lines
Quoted 56 lines
drivers/iio/chemical/sps30.c new file +56 -0
+	 */
+	*pm2p5 = sps30_ieee754_to_int(&buf[PM2p5 * 4]);
+	*pm10 = sps30_ieee754_to_int(&buf[PM10 * 4]);
+
+	return 0;
+}
+
+static irqreturn_t sps30_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct sps30_state *state = iio_priv(indio_dev);
+	u32 buf[4]; /* PM2p5, PM10, timestamp */
+	int ret;
+
+	mutex_lock(&state->lock);
+	ret = sps30_do_meas(state, &buf[0], &buf[1]);
+	mutex_unlock(&state->lock);
+	if (ret < 0)
+		goto err;
+
+	iio_push_to_buffers_with_timestamp(indio_dev, buf,
+					   iio_get_time_ns(indio_dev));
+err:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int sps30_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct sps30_state *state = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		switch (chan->type) {
+		case IIO_MASSCONCENTRATION:
+			mutex_lock(&state->lock);
+			switch (chan->channel2) {
+			case IIO_MOD_PM2p5:
+				ret = sps30_do_meas(state, val, val2);
+				break;
+			case IIO_MOD_PM10:
+				ret = sps30_do_meas(state, val2, val);
+				break;
+			default:
+				break;
+			}
+			mutex_unlock(&state->lock);
+			if (ret)
+				return ret;
+
+			return IIO_VAL_INT;
Quoted 9 lines

drivers/iio/chemical/sps30.c line 245

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+		default:
+			return -EINVAL;
+		}
+		break;
+	default:

You can't get here. Is this a warning suppression? If so just add a comment saying so to prevent it being removed by someone else later.

Okay.

Quoted 12 lines

drivers/iio/chemical/sps30.c line 253

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info sps30_info = {
+	.read_raw = sps30_read_raw,
+};
+

From a readability point of view, it would be helpful to pull the macro SPS30_CHAN down here in the code so we don't need to go jumping around to check what it is doing.

Okay.

Quoted 43 lines
Quoted 43 lines
drivers/iio/chemical/sps30.c new file +43 -0
+static const struct iio_chan_spec sps30_channels[] = {
+	SPS30_CHAN(0, PM2p5),
+	SPS30_CHAN(1, PM10),
+	IIO_CHAN_SOFT_TIMESTAMP(2),
+};
+
+static const unsigned long sps30_scan_masks[] = { 0x03, 0x00 };
+
+static int sps30_probe(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev;
+	struct sps30_state *state;
+	u8 buf[32] = { };
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	state->client = client;
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sps30_info;
+	indio_dev->name = client->name;
+	indio_dev->channels = sps30_channels;
+	indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->available_scan_masks = sps30_scan_masks;
+
+	mutex_init(&state->lock);
+	crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
+
+	/*
+	 * Power-on-reset causes sensor to produce some glitch on i2c bus
+	 * and some controllers end up in error state. Recover simply
+	 * by placing something on the bus.
+	 */
+	ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
+	if (ret) {
Quoted 9 lines

drivers/iio/chemical/sps30.c line 301

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+		dev_err(&client->dev, "failed to reset device\n");
+		return ret;
+	}
+	usleep_range(2500000, 3500000);
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);

Talk me through this logic. We stop it then pretty much immediately start it again? Needs a comment.

The idea is to send some message after resetting sensor so that i2c controller recovers from error state. I decided to send STOP_MEAS as it's a NOP in this case. I'll try to do more testing with other SPS30s and perhaps different boards.

Quoted 24 lines

drivers/iio/chemical/sps30.c line 311

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
+	if (ret) {
+		dev_err(&client->dev, "failed to read serial number\n");
+		return ret;
+	}
+	dev_info(&client->dev, "serial number: %s\n", buf);
+
+	ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
+	if (ret) {

This is not unwound on error. See comment on remove race below.

Quoted 11 lines
drivers/iio/chemical/sps30.c new file +11 -0
+		dev_err(&client->dev, "failed to start measurement\n");
+		return ret;
+	}
+
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      sps30_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
Quoted 9 lines

drivers/iio/chemical/sps30.c line 329

Quoted 7 lines
drivers/iio/chemical/sps30.c new file +7 -0
+
+static int sps30_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sps30_state *state = iio_priv(indio_dev);
+
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);

This looks like a race to me.

Right, this needs fixing.

Quoted 40 lines

You turn off the measurements before removing the interface to them. It's certainly not in the 'obviously' right category.

As such I would either use a devm action to do this stop, or not use the managed interfaces for everything in probe after the equivalent start.
The devm_add_action_or_reset would be the cleanest option I think.. Will also fix the cleanup on error in probe.

Quoted 30 lines
drivers/iio/chemical/sps30.c new file +30 -0
+
+	return 0;
+}
+
+static const struct i2c_device_id sps30_id[] = {
+	{ "sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, sps30_id);
+
+static const struct of_device_id sps30_of_match[] = {
+	{ .compatible = "sensirion,sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, sps30_of_match);
+
+static struct i2c_driver sps30_driver = {
+	.driver = {
+		.name = "sps30",
+		.of_match_table = sps30_of_match,
+	},
+	.id_table = sps30_id,
+	.probe_new = sps30_probe,
+	.remove = sps30_remove,
+};
+module_i2c_driver(sps30_driver);
+
+MODULE_AUTHOR("Tomasz Duszynski <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor driver");
+MODULE_LICENSE("GPL v2");
#14 lore raw

Re: [PATCH 2/3] iio: chemical: add support for Sensirion SPS30 sensor

Quoted 13 lines · On Sun, 25 Nov 2018 20:05:09 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 11 lines · Jonathan Cameron
Quoted 3 lines · On Sat, 24 Nov 2018 23:14:14 +0100 Tomasz Duszynski <[email protected]> wrote:

Add support for Sensirion SPS30 particulate matter sensor.

A few things inline.

I'm particularly curious as to why we are ignoring two of the particle sizes that the sensor seems to capture?

Quoted 5 lines

I was thinking about adding first the most common ones i.e PM2.5 and PM10 and the rest of them later on if there's a particular need.

On the other hand I can add PM1.0 and PM4.0 now so that we have everything in place once new dust sensors appear on the market.

I would. I do hope there don't turn out to be 100s of different views of what these values should be though. The various standards should prevent that even if people tuck in one or two extra just because they could.

Quoted 108 lines

It's seems reasonable to assume they will measure the very same subset of particulates.

Quoted 95 lines

Also, a 'potential' race in remove that I'd like us to make 'obviously' correct rather than requiring hard thought on whether it would be a problem.

Thanks,

Jonathan

Quoted 87 lines
---
 drivers/iio/chemical/Kconfig  |  11 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sps30.c  | 359 ++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/chemical/sps30.c
drivers/iio/chemical/Kconfig +11 -0
index b8e005be4f87..40057ecf8130 100644
@@ -61,6 +61,17 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config SPS30
+	tristate "SPS30 Particulate Matter sensor"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build support for the Sensirion SPS30 Particulate
+	  Matter sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called sps30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..9f42f4252151 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SPS30) += sps30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sps30.c new file +42 -0
new file mode 100644
index 000000000000..bf802621ae7f
@@ -0,0 +1,359 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Sensirion SPS30 Particulate Matter sensor driver
+ *
+ * Copyright (c) Tomasz Duszynski <[email protected]>
+ *
+ * I2C slave address: 0x69
+ *
+ * TODO:
+ *  - support for turning on fan cleaning
+ *  - support for reading/setting auto cleaning interval
+ */
+
+#define pr_fmt(fmt) "sps30: " fmt
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/module.h>
+
+#define SPS30_CRC8_POLYNOMIAL 0x31
+
+/* SPS30 commands */
+#define SPS30_START_MEAS 0x0010
+#define SPS30_STOP_MEAS 0x0104
+#define SPS30_RESET 0xd304
+#define SPS30_READ_DATA_READY_FLAG 0x0202
+#define SPS30_READ_DATA 0x0300
+#define SPS30_READ_SERIAL 0xD033
+
+#define SPS30_CHAN(_index, _mod) { \
+	.type = IIO_MASSCONCENTRATION, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _mod, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
+	.scan_index = _index, \
+	.scan_type = { \
Quoted 8 lines

drivers/iio/chemical/sps30.c line 45

Quoted 3 lines
drivers/iio/chemical/sps30.c new file +3 -0
+		.sign = 'u', \
+		.realbits = 12, \
+		.storagebits = 32, \  

That is unusual. Why do we need 32 bits to store a 12 bit value? 16 should be plenty. It'll make a difference to the buffer sizes if people just want to stream data and don't care about timestamps as it'll halve the memory usage.

Quoted 4 lines

Right, I could have picked up a petter values. But I've got at least one valid reason for doing that. Sensor datasheet specifies 0-1000 measurement range but simple tests showed that SPS30 can measure way beyond that. Interestingly, measurements are consistent with third party sensors.

I'm guessing there are certification bodies for this sort of thing. They may only have paid for the low end to be certified (or there may be no official test for higher levels?)

Quoted 4 lines

So having 12/32 bit setting, especially 32 storagebits protects against future changes in datasheet (which is btw preliminary) i.e updating measurement range.

Not really because userspace is going to assume the value is 12 bits and will probably mask it as such, thus any change requires userspace to cope with it. Once you are doing that, might as well just change the padding as well.

Quoted 3 lines

But, I guess that 16 for real and storage bits should be more that enough.

Feels like it to me as well.

Quoted 10 lines
Quoted 7 lines

Also, convention has always been to got for next 8,16,32,64 above the realbits if there isn't a reason not to (shifting etc)

How did we end up with 12 bits off the floating point conversion anyway? Needs some docs.

Matter of simple tests. I was able to trigger measurements of over 3000 ug/m3.

Hmm. So potentially it will give readings with more? If so we should work out a hard justification for that limit. Userspace will mask against it (as the rest of the bits may be garbage - we don't force them to 0 in other drivers and they often contain meta data if not actual garbage).

Quoted 109 lines
Quoted 26 lines
Quoted 12 lines
drivers/iio/chemical/sps30.c new file +9 -0
+		.endianness = IIO_CPU, \
+	}, \
+}
+
+enum {
+	PM1p0, /* just a placeholder */
+	PM2p5,
+	PM4p0, /* just a placeholder */
+	PM10,

drivers/iio/chemical/sps30.c line 64

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+};
+
+struct sps30_state {
+	struct i2c_client *client;
+	/* guards against concurrent access to sensor registers */
+	struct mutex lock;
+};
+
+DECLARE_CRC8_TABLE(sps30_crc8_table);
+  

I think you are fine locking wise, but it would be good to add a note on what locks are expected to be held on entering this function.

Quoted 2 lines

Without locks it's obviously very racey!

Understood.

Quoted 43 lines
Quoted 43 lines
drivers/iio/chemical/sps30.c new file +43 -0
+static int sps30_write_then_read(struct sps30_state *state, u8 *buf,
+				 int buf_size, u8 *data, int data_size)
+{
+	/* every two received data bytes are checksummed */
+	u8 tmp[data_size + data_size / 2];
+	int ret, i;
+
+	/*
+	 * Sensor does not support repeated start so instead of
+	 * sending two i2c messages in a row we just send one by one.
+	 */
+	ret = i2c_master_send(state->client, buf, buf_size);
+	if (ret != buf_size)
+		return ret < 0 ? ret : -EIO;
+
+	if (!data)
+		return 0;
+
+	ret = i2c_master_recv(state->client, tmp, sizeof(tmp));
+	if (ret != sizeof(tmp))
+		return ret < 0 ? ret : -EIO;
+
+	for (i = 0; i < sizeof(tmp); i += 3) {
+		u8 crc = crc8(sps30_crc8_table, &tmp[i], 2, CRC8_INIT_VALUE);
+
+		if (crc != tmp[i + 2]) {
+			dev_err(&state->client->dev,
+				"data integrity check failed\n");
+			return -EIO;
+		}
+
+		*data++ = tmp[i];
+		*data++ = tmp[i + 1];
+	}
+
+	return 0;
+}
+
+static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
+{
+	/* depending on the command up to 3 bytes may be needed for argument */
+	u8 buf[sizeof(cmd) + 3] = { cmd >> 8, cmd };
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 112

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+	switch (cmd) {
+	case SPS30_START_MEAS:
+		buf[2] = 0x03;
+		buf[3] = 0x00;
+		buf[4] = 0xac; /* precomputed crc */  

Could you put that in code and let the compiler do the pre compute? Might be a little more elegant.

Okay.

Quoted 13 lines
Quoted 13 lines
drivers/iio/chemical/sps30.c new file +13 -0
+		return sps30_write_then_read(state, buf, 5, NULL, 0);
+	case SPS30_STOP_MEAS:
+	case SPS30_RESET:
+		return sps30_write_then_read(state, buf, 2, NULL, 0);
+	case SPS30_READ_DATA_READY_FLAG:
+	case SPS30_READ_DATA:
+	case SPS30_READ_SERIAL:
+		return sps30_write_then_read(state, buf, 2, data, size);
+	default:
+		return -EINVAL;
+	};
+}
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 129

Quoted 4 lines
drivers/iio/chemical/sps30.c new file +4 -0
+static int sps30_ieee754_to_int(const u8 *data)
+{
+	u32 val = ((u32)data[0] << 24) | ((u32)data[1] << 16) |
+		  ((u32)data[2] << 8) | (u32)data[3],  

Have a separate
u32 mantissa = (val << 9) >> 9 line.
What is this next line actually doing? Kind of looks like a mask? If so just mask it with & as more readable.

Quoted 1 line

Do you mean mantissa? We want to get rid of sign bit and 8 exponent bits.

Got it, but the above is the same as.

val & 0x7ffffff; I think... Shifting like this immediately made me think you were manually sign extending (but it's unsigned so obviously not).

It's a mask to extract just the mantissa, so code it as one.

Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/chemical/sps30.c new file +13 -0
+	    mantissa = (val << 9) >> 9;
+	int exp = (val >> 23) - 127;
+
+	if (!exp && !mantissa)
+		return 0;
+
+	if (exp < 0)
+		return 0;
+
+	return (1 << exp) + (mantissa >> (23 - exp));
+}
+
+static int sps30_do_meas(struct sps30_state *state, int *pm2p5, int *pm10)
Quoted 9 lines

drivers/iio/chemical/sps30.c line 147

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+{
+	/*
+	 * Internally sensor stores measurements in a following manner:
+	 *
+	 * PM1p0: upper two bytes, crc8, lower two bytes, crc8  

So there are other particle sizes that this sensor reads? Why are we mapping them down to just the two channel types?

Quoted 2 lines

As said before, introducing PM1.0 and PM4.0 readings seems a reasonable option.

Go for it. Userspace who doesn't care won't read them.

Quoted 43 lines
Quoted 33 lines
Quoted 33 lines
drivers/iio/chemical/sps30.c new file +32 -0
+	 * PM2p5: upper two bytes, crc8, lower two bytes, crc8
+	 * PM4p0: upper two bytes, crc8, lower two bytes, crc8
+	 * PM10:  upper two bytes, crc8, lower two bytes, crc8
+	 *
+	 * What follows next are number concentration measurements and
+	 * typical particle size measurement.
+	 *
+	 * Once data is read from sensor crc bytes are stripped off
+	 * hence we need 16 bytes of buffer space.
+	 */
+	int ret, tries = 5;
+	u8 buf[16];
+
+	while (tries--) {
+		ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, buf, 2);
+		if (ret)
+			return -EIO;
+
+		/* new measurements ready to be read */
+		if (buf[1] == 1)
+			break;
+
+		usleep_range(300000, 400000);
+	}
+
+	if (!tries)
+		return -ETIMEDOUT;
+
+	ret = sps30_do_cmd(state, SPS30_READ_DATA, buf, sizeof(buf));
+	if (ret)
+		return ret;
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 184

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+	/*
+	 * All measurements come in IEEE754 single precision floating point
+	 * format but sensor itself is not precise enough (-+ 10% error)
+	 * to take full advantage of it. Hence converting result to int
+	 * to keep things simple.  

Interesting. We have talked about allowing floating point formats for sensors the provide them. Would that be beneficial here?

Quoted 7 lines

I recall this was discussed once but unfortunately do not remember final conclusion. Anyway, in case of this device datasheet states that readings have maximum error of -+10% in given range which makes me think that using floats here is an overkill.

Example, reading of 1000.123412ug/m3 has error of -+100ug/m3. Does it really matter if care about fractional part?

*laughs*. It does make you wonder why they ended up as floating point unless it is sensitive at very low levels? Note I know nothing about particle sensing at all!

Quoted 4 lines

Just out of curiosity, if IIO gets support for floats would it be problematic to to add extra channel returning measurements in floats? Or it rather will not fly..

It's hard for userspace to interpret the case of two channels measuring the same thing. So it could be done (and we have once or twice done this) but it's messy.

Quoted 137 lines
Quoted 57 lines
Quoted 57 lines
drivers/iio/chemical/sps30.c new file +56 -0
+	 */
+	*pm2p5 = sps30_ieee754_to_int(&buf[PM2p5 * 4]);
+	*pm10 = sps30_ieee754_to_int(&buf[PM10 * 4]);
+
+	return 0;
+}
+
+static irqreturn_t sps30_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct sps30_state *state = iio_priv(indio_dev);
+	u32 buf[4]; /* PM2p5, PM10, timestamp */
+	int ret;
+
+	mutex_lock(&state->lock);
+	ret = sps30_do_meas(state, &buf[0], &buf[1]);
+	mutex_unlock(&state->lock);
+	if (ret < 0)
+		goto err;
+
+	iio_push_to_buffers_with_timestamp(indio_dev, buf,
+					   iio_get_time_ns(indio_dev));
+err:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int sps30_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct sps30_state *state = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		switch (chan->type) {
+		case IIO_MASSCONCENTRATION:
+			mutex_lock(&state->lock);
+			switch (chan->channel2) {
+			case IIO_MOD_PM2p5:
+				ret = sps30_do_meas(state, val, val2);
+				break;
+			case IIO_MOD_PM10:
+				ret = sps30_do_meas(state, val2, val);
+				break;
+			default:
+				break;
+			}
+			mutex_unlock(&state->lock);
+			if (ret)
+				return ret;
+
+			return IIO_VAL_INT;
Quoted 9 lines

drivers/iio/chemical/sps30.c line 245

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+		default:
+			return -EINVAL;
+		}
+		break;
+	default:  

You can't get here. Is this a warning suppression? If so just add a comment saying so to prevent it being removed by someone else later.

Okay.

Quoted 12 lines

drivers/iio/chemical/sps30.c line 253

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info sps30_info = {
+	.read_raw = sps30_read_raw,
+};
+  

From a readability point of view, it would be helpful to pull the macro SPS30_CHAN down here in the code so we don't need to go jumping around to check what it is doing.

Okay.

Quoted 43 lines
Quoted 43 lines
drivers/iio/chemical/sps30.c new file +43 -0
+static const struct iio_chan_spec sps30_channels[] = {
+	SPS30_CHAN(0, PM2p5),
+	SPS30_CHAN(1, PM10),
+	IIO_CHAN_SOFT_TIMESTAMP(2),
+};
+
+static const unsigned long sps30_scan_masks[] = { 0x03, 0x00 };
+
+static int sps30_probe(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev;
+	struct sps30_state *state;
+	u8 buf[32] = { };
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	state->client = client;
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sps30_info;
+	indio_dev->name = client->name;
+	indio_dev->channels = sps30_channels;
+	indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->available_scan_masks = sps30_scan_masks;
+
+	mutex_init(&state->lock);
+	crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
+
+	/*
+	 * Power-on-reset causes sensor to produce some glitch on i2c bus
+	 * and some controllers end up in error state. Recover simply
+	 * by placing something on the bus.
+	 */
+	ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
+	if (ret) {
Quoted 9 lines

drivers/iio/chemical/sps30.c line 301

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+		dev_err(&client->dev, "failed to reset device\n");
+		return ret;
+	}
+	usleep_range(2500000, 3500000);
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);  

Talk me through this logic. We stop it then pretty much immediately start it again? Needs a comment.

Quoted 4 lines

The idea is to send some message after resetting sensor so that i2c controller recovers from error state. I decided to send STOP_MEAS as it's a NOP in this case. I'll try to do more testing with other SPS30s and perhaps different boards.

Ah. Makes sense. Just add a comment to the code saying this and it's fine.

Quoted 77 lines
Quoted 25 lines

drivers/iio/chemical/sps30.c line 311

Quoted 11 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
+	if (ret) {
+		dev_err(&client->dev, "failed to read serial number\n");
+		return ret;
+	}
+	dev_info(&client->dev, "serial number: %s\n", buf);
+
+	ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
+	if (ret) {  

This is not unwound on error. See comment on remove race below.

Quoted 11 lines
drivers/iio/chemical/sps30.c new file +11 -0
+		dev_err(&client->dev, "failed to start measurement\n");
+		return ret;
+	}
+
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      sps30_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
Quoted 9 lines

drivers/iio/chemical/sps30.c line 329

Quoted 7 lines
drivers/iio/chemical/sps30.c new file +7 -0
+
+static int sps30_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sps30_state *state = iio_priv(indio_dev);
+
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);  

This looks like a race to me.

Right, this needs fixing.

Quoted 40 lines

You turn off the measurements before removing the interface to them. It's certainly not in the 'obviously' right category.

As such I would either use a devm action to do this stop, or not use the managed interfaces for everything in probe after the equivalent start.
The devm_add_action_or_reset would be the cleanest option I think.. Will also fix the cleanup on error in probe.

Quoted 30 lines
drivers/iio/chemical/sps30.c new file +30 -0
+
+	return 0;
+}
+
+static const struct i2c_device_id sps30_id[] = {
+	{ "sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, sps30_id);
+
+static const struct of_device_id sps30_of_match[] = {
+	{ .compatible = "sensirion,sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, sps30_of_match);
+
+static struct i2c_driver sps30_driver = {
+	.driver = {
+		.name = "sps30",
+		.of_match_table = sps30_of_match,
+	},
+	.id_table = sps30_id,
+	.probe_new = sps30_probe,
+	.remove = sps30_remove,
+};
+module_i2c_driver(sps30_driver);
+
+MODULE_AUTHOR("Tomasz Duszynski <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor driver");
+MODULE_LICENSE("GPL v2");  
#15 lore raw

Re: [PATCH 2/3] iio: chemical: add support for Sensirion SPS30 sensor

From: Andreas Brauchli <[email protected]>

Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Sun, 25 Nov 2018 20:05:09 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 4 lines · Jonathan Cameron
Quoted 1 line · On Sat, 24 Nov 2018 23:14:14 +0100 Tomasz Duszynski <[email protected]> wrote:

Add support for Sensirion SPS30 particulate matter sensor.

Thanks a lot for your work! A few comments inline, I'm also happy to assist with further clarifications.

Since I work for Sensirion, feel free to add me on the Acked-by list. (The company server molests emails..)

Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

A few things inline.

I'm particularly curious as to why we are ignoring two of the particle sizes that the sensor seems to capture?

I was thinking about adding first the most common ones i.e PM2.5 and PM10 and the rest of them later on if there's a particular need.

Quoted 7 lines
Quoted 2 lines

On the other hand I can add PM1.0 and PM4.0 now so that we have everything in place once new dust sensors appear on the market.

I would. I do hope there don't turn out to be 100s of different views of what these values should be though. The various standards should prevent that even if people tuck in one or two extra just because they could.

PM1.0 is definitely used in other sensors (e.g. Plantower), but on a quick search I couldn't find another PM4.0 one.

Our rationale for the value is that, according to medical studies, anything below PM4.0 can affect the lungs.

Quoted 81 lines
Quoted 81 lines

It's seems reasonable to assume they will measure the very same subset of particulates.

Quoted 76 lines

Also, a 'potential' race in remove that I'd like us to make 'obviously' correct rather than requiring hard thought on whether it would be a problem.

Thanks,

Jonathan

Quoted 68 lines
---
 drivers/iio/chemical/Kconfig  |  11 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sps30.c  | 359 ++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/chemical/sps30.c
drivers/iio/chemical/Kconfig +11 -0
index b8e005be4f87..40057ecf8130 100644
@@ -61,6 +61,17 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config SPS30
+	tristate "SPS30 Particulate Matter sensor"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build support for the Sensirion SPS30 Particulate
+	  Matter sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called sps30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..9f42f4252151 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SPS30) += sps30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sps30.c new file +23 -0
new file mode 100644
index 000000000000..bf802621ae7f
@@ -0,0 +1,359 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Sensirion SPS30 Particulate Matter sensor driver
+ *
+ * Copyright (c) Tomasz Duszynski <[email protected]>
+ *
+ * I2C slave address: 0x69
+ *
+ * TODO:
+ *  - support for turning on fan cleaning
+ *  - support for reading/setting auto cleaning interval
+ */
+
+#define pr_fmt(fmt) "sps30: " fmt
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/chemical/sps30.c line 31

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+#include <linux/module.h>
+
+#define SPS30_CRC8_POLYNOMIAL 0x31
+
+/* SPS30 commands */
+#define SPS30_START_MEAS 0x0010
+#define SPS30_STOP_MEAS 0x0104
+#define SPS30_RESET 0xd304

lower case d vs upper case in SPS30_READ_SERIAL

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 4 lines
drivers/iio/chemical/sps30.c new file +4 -0
+#define SPS30_READ_DATA_READY_FLAG 0x0202
+#define SPS30_READ_DATA 0x0300
+#define SPS30_READ_SERIAL 0xD033
+

drivers/iio/chemical/sps30.c line 45

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+#define SPS30_CHAN(_index, _mod) { \
+	.type = IIO_MASSCONCENTRATION, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _mod, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
+	.scan_index = _index, \
+	.scan_type = { \
+		.sign = 'u', \
+		.realbits = 12, \
+		.storagebits = 32, \  

That is unusual. Why do we need 32 bits to store a 12 bit value? 16 should be plenty. It'll make a difference to the buffer sizes if people just want to stream data and don't care about timestamps as it'll halve the memory usage.

Quoted 13 lines
Quoted 4 lines

Right, I could have picked up a petter values. But I've got at least one valid reason for doing that. Sensor datasheet specifies 0-1000 measurement range but simple tests showed that SPS30 can measure way beyond that. Interestingly, measurements are consistent with third party sensors.

I'm guessing there are certification bodies for this sort of thing. They may only have paid for the low end to be certified (or there may be no official test for higher levels?)

Quoted 4 lines

So having 12/32 bit setting, especially 32 storagebits protects against future changes in datasheet (which is btw preliminary) i.e updating measurement range.

The final datasheet is expected in Q1 2019. You could add the following link which always points to the latest version:

https://www.sensirion.com/file/datasheet_sps30

Quoted 20 lines

Not really because userspace is going to assume the value is 12 bits and will probably mask it as such, thus any change requires userspace to cope with it. Once you are doing that, might as well just change the padding as well.

Quoted 3 lines

But, I guess that 16 for real and storage bits should be more that enough.

Feels like it to me as well.

Quoted 8 lines
Quoted 7 lines

Also, convention has always been to got for next 8,16,32,64 above the realbits if there isn't a reason not to (shifting etc)

How did we end up with 12 bits off the floating point conversion anyway? Needs some docs.

Quoted 8 lines
Quoted 2 lines

Matter of simple tests. I was able to trigger measurements of over 3000 ug/m3.

Hmm. So potentially it will give readings with more? If so we should work out a hard justification for that limit. Userspace will mask against it (as the rest of the bits may be garbage - we don't force them to 0 in other drivers and they often contain meta data if not actual garbage).

The SPS30 can output high values (e.g. 60,000), but the measurement principle only applies up to somewhere between 3000-5000 ug/m3. Anything above 3000 is highly imprecise.

Quoted 143 lines
Quoted 110 lines
Quoted 27 lines
Quoted 13 lines
drivers/iio/chemical/sps30.c new file +9 -0
+		.endianness = IIO_CPU, \
+	}, \
+}
+
+enum {
+	PM1p0, /* just a placeholder */
+	PM2p5,
+	PM4p0, /* just a placeholder */
+	PM10,

drivers/iio/chemical/sps30.c line 64

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+};
+
+struct sps30_state {
+	struct i2c_client *client;
+	/* guards against concurrent access to sensor registers */
+	struct mutex lock;
+};
+
+DECLARE_CRC8_TABLE(sps30_crc8_table);
+  

I think you are fine locking wise, but it would be good to add a note on what locks are expected to be held on entering this function.

Quoted 2 lines

Without locks it's obviously very racey!

Understood.

Quoted 43 lines
Quoted 43 lines
drivers/iio/chemical/sps30.c new file +43 -0
+static int sps30_write_then_read(struct sps30_state *state, u8 *buf,
+				 int buf_size, u8 *data, int data_size)
+{
+	/* every two received data bytes are checksummed */
+	u8 tmp[data_size + data_size / 2];
+	int ret, i;
+
+	/*
+	 * Sensor does not support repeated start so instead of
+	 * sending two i2c messages in a row we just send one by one.
+	 */
+	ret = i2c_master_send(state->client, buf, buf_size);
+	if (ret != buf_size)
+		return ret < 0 ? ret : -EIO;
+
+	if (!data)
+		return 0;
+
+	ret = i2c_master_recv(state->client, tmp, sizeof(tmp));
+	if (ret != sizeof(tmp))
+		return ret < 0 ? ret : -EIO;
+
+	for (i = 0; i < sizeof(tmp); i += 3) {
+		u8 crc = crc8(sps30_crc8_table, &tmp[i], 2, CRC8_INIT_VALUE);
+
+		if (crc != tmp[i + 2]) {
+			dev_err(&state->client->dev,
+				"data integrity check failed\n");
+			return -EIO;
+		}
+
+		*data++ = tmp[i];
+		*data++ = tmp[i + 1];
+	}
+
+	return 0;
+}
+
+static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
+{
+	/* depending on the command up to 3 bytes may be needed for argument */
+	u8 buf[sizeof(cmd) + 3] = { cmd >> 8, cmd };
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 112

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+	switch (cmd) {
+	case SPS30_START_MEAS:
+		buf[2] = 0x03;
+		buf[3] = 0x00;
+		buf[4] = 0xac; /* precomputed crc */  

Could you put that in code and let the compiler do the pre compute? Might be a little more elegant.

Okay.

Quoted 13 lines
Quoted 13 lines
drivers/iio/chemical/sps30.c new file +13 -0
+		return sps30_write_then_read(state, buf, 5, NULL, 0);
+	case SPS30_STOP_MEAS:
+	case SPS30_RESET:
+		return sps30_write_then_read(state, buf, 2, NULL, 0);
+	case SPS30_READ_DATA_READY_FLAG:
+	case SPS30_READ_DATA:
+	case SPS30_READ_SERIAL:
+		return sps30_write_then_read(state, buf, 2, data, size);
+	default:
+		return -EINVAL;
+	};
+}
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 129

Quoted 4 lines
drivers/iio/chemical/sps30.c new file +4 -0
+static int sps30_ieee754_to_int(const u8 *data)
+{
+	u32 val = ((u32)data[0] << 24) | ((u32)data[1] << 16) |
+		  ((u32)data[2] << 8) | (u32)data[3],  

Have a separate
u32 mantissa = (val << 9) >> 9 line.
What is this next line actually doing? Kind of looks like a mask? If so just mask it with & as more readable.

Quoted 1 line

Do you mean mantissa? We want to get rid of sign bit and 8 exponent bits.

Got it, but the above is the same as.

val & 0x7ffffff; I think... Shifting like this immediately made me think you were manually sign extending (but it's unsigned so obviously not).

It's a mask to extract just the mantissa, so code it as one.

Quoted 24 lines
Quoted 23 lines
Quoted 9 lines
drivers/iio/chemical/sps30.c new file +8 -0
+	    mantissa = (val << 9) >> 9;
+	int exp = (val >> 23) - 127;
+
+	if (!exp && !mantissa)
+		return 0;
+
+	if (exp < 0)
+		return 0;

drivers/iio/chemical/sps30.c line 147

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	return (1 << exp) + (mantissa >> (23 - exp));
+}
+
+static int sps30_do_meas(struct sps30_state *state, int *pm2p5, int *pm10)
+{
+	/*
+	 * Internally sensor stores measurements in a following manner:
+	 *
+	 * PM1p0: upper two bytes, crc8, lower two bytes, crc8  

So there are other particle sizes that this sensor reads? Why are we mapping them down to just the two channel types?

Quoted 55 lines
Quoted 2 lines

As said before, introducing PM1.0 and PM4.0 readings seems a reasonable option.

Go for it. Userspace who doesn't care won't read them.

Quoted 50 lines
Quoted 33 lines
Quoted 33 lines
drivers/iio/chemical/sps30.c new file +32 -0
+	 * PM2p5: upper two bytes, crc8, lower two bytes, crc8
+	 * PM4p0: upper two bytes, crc8, lower two bytes, crc8
+	 * PM10:  upper two bytes, crc8, lower two bytes, crc8
+	 *
+	 * What follows next are number concentration measurements and
+	 * typical particle size measurement.
+	 *
+	 * Once data is read from sensor crc bytes are stripped off
+	 * hence we need 16 bytes of buffer space.
+	 */
+	int ret, tries = 5;
+	u8 buf[16];
+
+	while (tries--) {
+		ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, buf, 2);
+		if (ret)
+			return -EIO;
+
+		/* new measurements ready to be read */
+		if (buf[1] == 1)
+			break;
+
+		usleep_range(300000, 400000);
+	}
+
+	if (!tries)
+		return -ETIMEDOUT;
+
+	ret = sps30_do_cmd(state, SPS30_READ_DATA, buf, sizeof(buf));
+	if (ret)
+		return ret;
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 184

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+	/*
+	 * All measurements come in IEEE754 single precision floating point
+	 * format but sensor itself is not precise enough (-+ 10% error)
+	 * to take full advantage of it. Hence converting result to int
+	 * to keep things simple.  

Interesting. We have talked about allowing floating point formats for sensors the provide them. Would that be beneficial here?

I recall this was discussed once but unfortunately do not remember final conclusion. Anyway, in case of this device datasheet states that readings have maximum error of -+10% in given range which makes me think that using floats here is an overkill.

Example, reading of 1000.123412ug/m3 has error of -+100ug/m3. Does it really matter if care about fractional part?

The datasheet specifies absolute precision, the relative precision is much better. The noise level is at ca. 1%. We thus suggest using 16bit values with 2bits dedicated to fixed point decimals.

Quoted 5 lines

*laughs*. It does make you wonder why they ended up as floating point unless it is sensitive at very low levels? Note I know nothing about particle sensing at all!

..used internally

Quoted 126 lines
Quoted 4 lines

Just out of curiosity, if IIO gets support for floats would it be problematic to to add extra channel returning measurements in floats? Or it rather will not fly..

It's hard for userspace to interpret the case of two channels measuring the same thing. So it could be done (and we have once or twice done this) but it's messy.

Quoted 117 lines
Quoted 57 lines
Quoted 57 lines
drivers/iio/chemical/sps30.c new file +56 -0
+	 */
+	*pm2p5 = sps30_ieee754_to_int(&buf[PM2p5 * 4]);
+	*pm10 = sps30_ieee754_to_int(&buf[PM10 * 4]);
+
+	return 0;
+}
+
+static irqreturn_t sps30_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct sps30_state *state = iio_priv(indio_dev);
+	u32 buf[4]; /* PM2p5, PM10, timestamp */
+	int ret;
+
+	mutex_lock(&state->lock);
+	ret = sps30_do_meas(state, &buf[0], &buf[1]);
+	mutex_unlock(&state->lock);
+	if (ret < 0)
+		goto err;
+
+	iio_push_to_buffers_with_timestamp(indio_dev, buf,
+					   iio_get_time_ns(indio_dev));
+err:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int sps30_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct sps30_state *state = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		switch (chan->type) {
+		case IIO_MASSCONCENTRATION:
+			mutex_lock(&state->lock);
+			switch (chan->channel2) {
+			case IIO_MOD_PM2p5:
+				ret = sps30_do_meas(state, val, val2);
+				break;
+			case IIO_MOD_PM10:
+				ret = sps30_do_meas(state, val2, val);
+				break;
+			default:
+				break;
+			}
+			mutex_unlock(&state->lock);
+			if (ret)
+				return ret;
+
+			return IIO_VAL_INT;
Quoted 9 lines

drivers/iio/chemical/sps30.c line 245

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+		default:
+			return -EINVAL;
+		}
+		break;
+	default:  

You can't get here. Is this a warning suppression? If so just add a comment saying so to prevent it being removed by someone else later.

Okay.

Quoted 12 lines

drivers/iio/chemical/sps30.c line 253

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info sps30_info = {
+	.read_raw = sps30_read_raw,
+};
+  

From a readability point of view, it would be helpful to pull the macro SPS30_CHAN down here in the code so we don't need to go jumping around to check what it is doing.

Okay.

Quoted 33 lines
Quoted 33 lines
drivers/iio/chemical/sps30.c new file +33 -0
+static const struct iio_chan_spec sps30_channels[] = {
+	SPS30_CHAN(0, PM2p5),
+	SPS30_CHAN(1, PM10),
+	IIO_CHAN_SOFT_TIMESTAMP(2),
+};
+
+static const unsigned long sps30_scan_masks[] = { 0x03, 0x00 };
+
+static int sps30_probe(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev;
+	struct sps30_state *state;
+	u8 buf[32] = { };
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	state->client = client;
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sps30_info;
+	indio_dev->name = client->name;
+	indio_dev->channels = sps30_channels;
+	indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->available_scan_masks = sps30_scan_masks;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/chemical/sps30.c line 294

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+	mutex_init(&state->lock);
+	crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
+
+	/*
+	 * Power-on-reset causes sensor to produce some glitch on i2c bus
+	 * and some controllers end up in error state. Recover simply
+	 * by placing something on the bus.
+	 */

Apologies for that. I talked to the firmware engineer and we'll do our best to have it fixed in the next update scheduled for Q1 2019. The cause seems to be the pin initialization causing the clock to be pulled down for ca. 2.5us.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

drivers/iio/chemical/sps30.c line 301

Quoted 7 lines
drivers/iio/chemical/sps30.c new file +7 -0
+	ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
+	if (ret) {
+		dev_err(&client->dev, "failed to reset device\n");
+		return ret;
+	}
+	usleep_range(2500000, 3500000);
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);  

Talk me through this logic. We stop it then pretty much immediately start it again? Needs a comment.

Quoted 7 lines
Quoted 4 lines

The idea is to send some message after resetting sensor so that i2c controller recovers from error state. I decided to send STOP_MEAS as it's a NOP in this case. I'll try to do more testing with other SPS30s and perhaps different boards.

Ah. Makes sense. Just add a comment to the code saying this and it's fine.

Not to shift the blame, but the recovery is likely controller specific and might be better handled at that level. Consider the case where the faulty device is present but the driver is not loaded after the initialization..

Quoted 78 lines
Quoted 78 lines
Quoted 26 lines

drivers/iio/chemical/sps30.c line 311

Quoted 12 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
+	if (ret) {
+		dev_err(&client->dev, "failed to read serial number\n");
+		return ret;
+	}
+	dev_info(&client->dev, "serial number: %s\n", buf);
+
+	ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
+	if (ret) {  

This is not unwound on error. See comment on remove race below.

Quoted 11 lines
drivers/iio/chemical/sps30.c new file +11 -0
+		dev_err(&client->dev, "failed to start measurement\n");
+		return ret;
+	}
+
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      sps30_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
Quoted 9 lines

drivers/iio/chemical/sps30.c line 329

Quoted 7 lines
drivers/iio/chemical/sps30.c new file +7 -0
+
+static int sps30_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sps30_state *state = iio_priv(indio_dev);
+
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);  

This looks like a race to me.

Right, this needs fixing.

Quoted 40 lines

You turn off the measurements before removing the interface to them. It's certainly not in the 'obviously' right category.

As such I would either use a devm action to do this stop, or not use the managed interfaces for everything in probe after the equivalent start.
The devm_add_action_or_reset would be the cleanest option I think.. Will also fix the cleanup on error in probe.

Quoted 30 lines
drivers/iio/chemical/sps30.c new file +30 -0
+
+	return 0;
+}
+
+static const struct i2c_device_id sps30_id[] = {
+	{ "sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, sps30_id);
+
+static const struct of_device_id sps30_of_match[] = {
+	{ .compatible = "sensirion,sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, sps30_of_match);
+
+static struct i2c_driver sps30_driver = {
+	.driver = {
+		.name = "sps30",
+		.of_match_table = sps30_of_match,
+	},
+	.id_table = sps30_id,
+	.probe_new = sps30_probe,
+	.remove = sps30_remove,
+};
+module_i2c_driver(sps30_driver);
+
+MODULE_AUTHOR("Tomasz Duszynski <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor driver");
+MODULE_LICENSE("GPL v2");  
author maintainer
#16 lore raw

Re: [PATCH 2/3] iio: chemical: add support for Sensirion SPS30 sensor

Quoted 39 lines · Andreas Brauchli

From: Andreas Brauchli <[email protected]>

Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Sun, 25 Nov 2018 20:05:09 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 4 lines · Jonathan Cameron
Quoted 1 line · On Sat, 24 Nov 2018 23:14:14 +0100 Tomasz Duszynski <[email protected]> wrote:

Add support for Sensirion SPS30 particulate matter sensor.

Thanks a lot for your work! A few comments inline, I'm also happy to assist with further clarifications.

Since I work for Sensirion, feel free to add me on the Acked-by list. (The company server molests emails..)

Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

A few things inline.

I'm particularly curious as to why we are ignoring two of the particle sizes that the sensor seems to capture?

I was thinking about adding first the most common ones i.e PM2.5 and PM10 and the rest of them later on if there's a particular need.

Quoted 7 lines
Quoted 2 lines

On the other hand I can add PM1.0 and PM4.0 now so that we have everything in place once new dust sensors appear on the market.

I would. I do hope there don't turn out to be 100s of different views of what these values should be though. The various standards should prevent that even if people tuck in one or two extra just because they could.

Quoted 6 lines

PM1.0 is definitely used in other sensors (e.g. Plantower), but on a quick search I couldn't find another PM4.0 one.

Our rationale for the value is that, according to medical studies, anything below PM4.0 can affect the lungs.

Fair enough.

Quoted 83 lines
Quoted 83 lines
Quoted 83 lines

It's seems reasonable to assume they will measure the very same subset of particulates.

Quoted 78 lines

Also, a 'potential' race in remove that I'd like us to make 'obviously' correct rather than requiring hard thought on whether it would be a problem.

Thanks,

Jonathan

Quoted 70 lines
---
 drivers/iio/chemical/Kconfig  |  11 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sps30.c  | 359 ++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/chemical/sps30.c
drivers/iio/chemical/Kconfig +11 -0
index b8e005be4f87..40057ecf8130 100644
@@ -61,6 +61,17 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config SPS30
+	tristate "SPS30 Particulate Matter sensor"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build support for the Sensirion SPS30 Particulate
+	  Matter sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called sps30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..9f42f4252151 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SPS30) += sps30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sps30.c new file +25 -0
new file mode 100644
index 000000000000..bf802621ae7f
@@ -0,0 +1,359 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Sensirion SPS30 Particulate Matter sensor driver
+ *
+ * Copyright (c) Tomasz Duszynski <[email protected]>
+ *
+ * I2C slave address: 0x69
+ *
+ * TODO:
+ *  - support for turning on fan cleaning
+ *  - support for reading/setting auto cleaning interval
+ */
+
+#define pr_fmt(fmt) "sps30: " fmt
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/module.h>
+
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/chemical/sps30.c line 31

Quoted 6 lines
drivers/iio/chemical/sps30.c new file +6 -0
+#define SPS30_CRC8_POLYNOMIAL 0x31
+
+/* SPS30 commands */
+#define SPS30_START_MEAS 0x0010
+#define SPS30_STOP_MEAS 0x0104
+#define SPS30_RESET 0xd304

lower case d vs upper case in SPS30_READ_SERIAL

Good catch.

Quoted 30 lines
Quoted 30 lines
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/iio/chemical/sps30.c new file +11 -0
+#define SPS30_READ_DATA_READY_FLAG 0x0202
+#define SPS30_READ_DATA 0x0300
+#define SPS30_READ_SERIAL 0xD033
+
+#define SPS30_CHAN(_index, _mod) { \
+	.type = IIO_MASSCONCENTRATION, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _mod, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
+	.scan_index = _index, \
+	.scan_type = { \
Quoted 8 lines

drivers/iio/chemical/sps30.c line 45

Quoted 3 lines
drivers/iio/chemical/sps30.c new file +3 -0
+		.sign = 'u', \
+		.realbits = 12, \
+		.storagebits = 32, \

That is unusual. Why do we need 32 bits to store a 12 bit value? 16 should be plenty. It'll make a difference to the buffer sizes if people just want to stream data and don't care about timestamps as it'll halve the memory usage.

Quoted 4 lines

Right, I could have picked up a petter values. But I've got at least one valid reason for doing that. Sensor datasheet specifies 0-1000 measurement range but simple tests showed that SPS30 can measure way beyond that. Interestingly, measurements are consistent with third party sensors.

I'm guessing there are certification bodies for this sort of thing. They may only have paid for the low end to be certified (or there may be no official test for higher levels?)

Quoted 1 line
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

So having 12/32 bit setting, especially 32 storagebits protects against future changes in datasheet (which is btw preliminary) i.e updating measurement range.

The final datasheet is expected in Q1 2019. You could add the following link which always points to the latest version:

https://www.sensirion.com/file/datasheet_sps30

Personally, I dislike the idea of adding datasheet links to the drivers. Rationale being that they become outdated pretty soon. Anyone interested in the details can find the doc pretty easily himself.

Quoted 29 lines
Quoted 20 lines

Not really because userspace is going to assume the value is 12 bits and will probably mask it as such, thus any change requires userspace to cope with it. Once you are doing that, might as well just change the padding as well.

Quoted 3 lines

But, I guess that 16 for real and storage bits should be more that enough.

Feels like it to me as well.

Quoted 8 lines
Quoted 7 lines

Also, convention has always been to got for next 8,16,32,64 above the realbits if there isn't a reason not to (shifting etc)

How did we end up with 12 bits off the floating point conversion anyway? Needs some docs.

Quoted 8 lines
Quoted 2 lines

Matter of simple tests. I was able to trigger measurements of over 3000 ug/m3.

Hmm. So potentially it will give readings with more? If so we should work out a hard justification for that limit. Userspace will mask against it (as the rest of the bits may be garbage - we don't force them to 0 in other drivers and they often contain meta data if not actual garbage).

Quoted 4 lines

The SPS30 can output high values (e.g. 60,000), but the measurement principle only applies up to somewhere between 3000-5000 ug/m3. Anything above 3000 is highly imprecise.

In that case, clamping the measurements to 3000.00 would be fine I guess.

Quoted 196 lines
Quoted 196 lines
Quoted 110 lines
Quoted 27 lines
Quoted 13 lines
drivers/iio/chemical/sps30.c new file +9 -0
+		.endianness = IIO_CPU, \
+	}, \
+}
+
+enum {
+	PM1p0, /* just a placeholder */
+	PM2p5,
+	PM4p0, /* just a placeholder */
+	PM10,

drivers/iio/chemical/sps30.c line 64

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+};
+
+struct sps30_state {
+	struct i2c_client *client;
+	/* guards against concurrent access to sensor registers */
+	struct mutex lock;
+};
+
+DECLARE_CRC8_TABLE(sps30_crc8_table);
+

I think you are fine locking wise, but it would be good to add a note on what locks are expected to be held on entering this function.

Quoted 2 lines

Without locks it's obviously very racey!

Understood.

Quoted 43 lines
Quoted 43 lines
drivers/iio/chemical/sps30.c new file +43 -0
+static int sps30_write_then_read(struct sps30_state *state, u8 *buf,
+				 int buf_size, u8 *data, int data_size)
+{
+	/* every two received data bytes are checksummed */
+	u8 tmp[data_size + data_size / 2];
+	int ret, i;
+
+	/*
+	 * Sensor does not support repeated start so instead of
+	 * sending two i2c messages in a row we just send one by one.
+	 */
+	ret = i2c_master_send(state->client, buf, buf_size);
+	if (ret != buf_size)
+		return ret < 0 ? ret : -EIO;
+
+	if (!data)
+		return 0;
+
+	ret = i2c_master_recv(state->client, tmp, sizeof(tmp));
+	if (ret != sizeof(tmp))
+		return ret < 0 ? ret : -EIO;
+
+	for (i = 0; i < sizeof(tmp); i += 3) {
+		u8 crc = crc8(sps30_crc8_table, &tmp[i], 2, CRC8_INIT_VALUE);
+
+		if (crc != tmp[i + 2]) {
+			dev_err(&state->client->dev,
+				"data integrity check failed\n");
+			return -EIO;
+		}
+
+		*data++ = tmp[i];
+		*data++ = tmp[i + 1];
+	}
+
+	return 0;
+}
+
+static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
+{
+	/* depending on the command up to 3 bytes may be needed for argument */
+	u8 buf[sizeof(cmd) + 3] = { cmd >> 8, cmd };
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 112

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+	switch (cmd) {
+	case SPS30_START_MEAS:
+		buf[2] = 0x03;
+		buf[3] = 0x00;
+		buf[4] = 0xac; /* precomputed crc */

Could you put that in code and let the compiler do the pre compute? Might be a little more elegant.

Okay.

Quoted 13 lines
Quoted 13 lines
drivers/iio/chemical/sps30.c new file +13 -0
+		return sps30_write_then_read(state, buf, 5, NULL, 0);
+	case SPS30_STOP_MEAS:
+	case SPS30_RESET:
+		return sps30_write_then_read(state, buf, 2, NULL, 0);
+	case SPS30_READ_DATA_READY_FLAG:
+	case SPS30_READ_DATA:
+	case SPS30_READ_SERIAL:
+		return sps30_write_then_read(state, buf, 2, data, size);
+	default:
+		return -EINVAL;
+	};
+}
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 129

Quoted 4 lines
drivers/iio/chemical/sps30.c new file +4 -0
+static int sps30_ieee754_to_int(const u8 *data)
+{
+	u32 val = ((u32)data[0] << 24) | ((u32)data[1] << 16) |
+		  ((u32)data[2] << 8) | (u32)data[3],

Have a separate
u32 mantissa = (val << 9) >> 9 line.
What is this next line actually doing? Kind of looks like a mask? If so just mask it with & as more readable.

Quoted 1 line

Do you mean mantissa? We want to get rid of sign bit and 8 exponent bits.

Got it, but the above is the same as.

val & 0x7ffffff; I think... Shifting like this immediately made me think you were manually sign extending (but it's unsigned so obviously not).

It's a mask to extract just the mantissa, so code it as one.

Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/chemical/sps30.c new file +13 -0
+	    mantissa = (val << 9) >> 9;
+	int exp = (val >> 23) - 127;
+
+	if (!exp && !mantissa)
+		return 0;
+
+	if (exp < 0)
+		return 0;
+
+	return (1 << exp) + (mantissa >> (23 - exp));
+}
+
+static int sps30_do_meas(struct sps30_state *state, int *pm2p5, int *pm10)
Quoted 9 lines

drivers/iio/chemical/sps30.c line 147

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+{
+	/*
+	 * Internally sensor stores measurements in a following manner:
+	 *
+	 * PM1p0: upper two bytes, crc8, lower two bytes, crc8

So there are other particle sizes that this sensor reads? Why are we mapping them down to just the two channel types?

Quoted 2 lines

As said before, introducing PM1.0 and PM4.0 readings seems a reasonable option.

Go for it. Userspace who doesn't care won't read them.

Quoted 48 lines
Quoted 33 lines
Quoted 33 lines
drivers/iio/chemical/sps30.c new file +32 -0
+	 * PM2p5: upper two bytes, crc8, lower two bytes, crc8
+	 * PM4p0: upper two bytes, crc8, lower two bytes, crc8
+	 * PM10:  upper two bytes, crc8, lower two bytes, crc8
+	 *
+	 * What follows next are number concentration measurements and
+	 * typical particle size measurement.
+	 *
+	 * Once data is read from sensor crc bytes are stripped off
+	 * hence we need 16 bytes of buffer space.
+	 */
+	int ret, tries = 5;
+	u8 buf[16];
+
+	while (tries--) {
+		ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, buf, 2);
+		if (ret)
+			return -EIO;
+
+		/* new measurements ready to be read */
+		if (buf[1] == 1)
+			break;
+
+		usleep_range(300000, 400000);
+	}
+
+	if (!tries)
+		return -ETIMEDOUT;
+
+	ret = sps30_do_cmd(state, SPS30_READ_DATA, buf, sizeof(buf));
+	if (ret)
+		return ret;
+
Quoted 9 lines

drivers/iio/chemical/sps30.c line 184

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+	/*
+	 * All measurements come in IEEE754 single precision floating point
+	 * format but sensor itself is not precise enough (-+ 10% error)
+	 * to take full advantage of it. Hence converting result to int
+	 * to keep things simple.

Interesting. We have talked about allowing floating point formats for sensors the provide them. Would that be beneficial here?

I recall this was discussed once but unfortunately do not remember final conclusion. Anyway, in case of this device datasheet states that readings have maximum error of -+10% in given range which makes me think that using floats here is an overkill.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

Example, reading of 1000.123412ug/m3 has error of -+100ug/m3. Does it really matter if care about fractional part?

The datasheet specifies absolute precision, the relative precision is much better. The noise level is at ca. 1%. We thus suggest using 16bit values with 2bits dedicated to fixed point decimals.

Wondering why that was not mentioned in the datasheet in the first place but good to know anyway. Then I am fine with adding that extra level of precision i.e two extra decimal places.

Quoted 148 lines
Quoted 5 lines

*laughs*. It does make you wonder why they ended up as floating point unless it is sensitive at very low levels? Note I know nothing about particle sensing at all!

..used internally

Quoted 126 lines
Quoted 4 lines

Just out of curiosity, if IIO gets support for floats would it be problematic to to add extra channel returning measurements in floats? Or it rather will not fly..

It's hard for userspace to interpret the case of two channels measuring the same thing. So it could be done (and we have once or twice done this) but it's messy.

Quoted 117 lines
Quoted 57 lines
Quoted 57 lines
drivers/iio/chemical/sps30.c new file +56 -0
+	 */
+	*pm2p5 = sps30_ieee754_to_int(&buf[PM2p5 * 4]);
+	*pm10 = sps30_ieee754_to_int(&buf[PM10 * 4]);
+
+	return 0;
+}
+
+static irqreturn_t sps30_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct sps30_state *state = iio_priv(indio_dev);
+	u32 buf[4]; /* PM2p5, PM10, timestamp */
+	int ret;
+
+	mutex_lock(&state->lock);
+	ret = sps30_do_meas(state, &buf[0], &buf[1]);
+	mutex_unlock(&state->lock);
+	if (ret < 0)
+		goto err;
+
+	iio_push_to_buffers_with_timestamp(indio_dev, buf,
+					   iio_get_time_ns(indio_dev));
+err:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int sps30_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct sps30_state *state = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		switch (chan->type) {
+		case IIO_MASSCONCENTRATION:
+			mutex_lock(&state->lock);
+			switch (chan->channel2) {
+			case IIO_MOD_PM2p5:
+				ret = sps30_do_meas(state, val, val2);
+				break;
+			case IIO_MOD_PM10:
+				ret = sps30_do_meas(state, val2, val);
+				break;
+			default:
+				break;
+			}
+			mutex_unlock(&state->lock);
+			if (ret)
+				return ret;
+
+			return IIO_VAL_INT;
Quoted 9 lines

drivers/iio/chemical/sps30.c line 245

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+		default:
+			return -EINVAL;
+		}
+		break;
+	default:

You can't get here. Is this a warning suppression? If so just add a comment saying so to prevent it being removed by someone else later.

Okay.

Quoted 12 lines

drivers/iio/chemical/sps30.c line 253

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info sps30_info = {
+	.read_raw = sps30_read_raw,
+};
+

From a readability point of view, it would be helpful to pull the macro SPS30_CHAN down here in the code so we don't need to go jumping around to check what it is doing.

Okay.

Quoted 33 lines
Quoted 33 lines
drivers/iio/chemical/sps30.c new file +33 -0
+static const struct iio_chan_spec sps30_channels[] = {
+	SPS30_CHAN(0, PM2p5),
+	SPS30_CHAN(1, PM10),
+	IIO_CHAN_SOFT_TIMESTAMP(2),
+};
+
+static const unsigned long sps30_scan_masks[] = { 0x03, 0x00 };
+
+static int sps30_probe(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev;
+	struct sps30_state *state;
+	u8 buf[32] = { };
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	state->client = client;
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sps30_info;
+	indio_dev->name = client->name;
+	indio_dev->channels = sps30_channels;
+	indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->available_scan_masks = sps30_scan_masks;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/chemical/sps30.c line 294

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+	mutex_init(&state->lock);
+	crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
+
+	/*
+	 * Power-on-reset causes sensor to produce some glitch on i2c bus
+	 * and some controllers end up in error state. Recover simply
+	 * by placing something on the bus.
+	 */

Apologies for that. I talked to the firmware engineer and we'll do our best to have it fixed in the next update scheduled for Q1 2019. The cause seems to be the pin initialization causing the clock to be pulled down for ca. 2.5us.

Quoted 1 line

From what I see SDA is pulled low as well for the same amount of time and

interestingly in happens always in about ~10ms after reset.

Quoted 102 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

drivers/iio/chemical/sps30.c line 301

Quoted 7 lines
drivers/iio/chemical/sps30.c new file +7 -0
+	ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
+	if (ret) {
+		dev_err(&client->dev, "failed to reset device\n");
+		return ret;
+	}
+	usleep_range(2500000, 3500000);
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);

Talk me through this logic. We stop it then pretty much immediately start it again? Needs a comment.

Quoted 7 lines
Quoted 4 lines

The idea is to send some message after resetting sensor so that i2c controller recovers from error state. I decided to send STOP_MEAS as it's a NOP in this case. I'll try to do more testing with other SPS30s and perhaps different boards.

Ah. Makes sense. Just add a comment to the code saying this and it's fine.

Not to shift the blame, but the recovery is likely controller specific and might be better handled at that level. Consider the case where the faulty device is present but the driver is not loaded after the initialization..

Quoted 78 lines
Quoted 78 lines
Quoted 26 lines

drivers/iio/chemical/sps30.c line 311

Quoted 12 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
+	if (ret) {
+		dev_err(&client->dev, "failed to read serial number\n");
+		return ret;
+	}
+	dev_info(&client->dev, "serial number: %s\n", buf);
+
+	ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
+	if (ret) {

This is not unwound on error. See comment on remove race below.

Quoted 11 lines
drivers/iio/chemical/sps30.c new file +11 -0
+		dev_err(&client->dev, "failed to start measurement\n");
+		return ret;
+	}
+
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      sps30_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
Quoted 9 lines

drivers/iio/chemical/sps30.c line 329

Quoted 7 lines
drivers/iio/chemical/sps30.c new file +7 -0
+
+static int sps30_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sps30_state *state = iio_priv(indio_dev);
+
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);

This looks like a race to me.

Right, this needs fixing.

Quoted 40 lines

You turn off the measurements before removing the interface to them. It's certainly not in the 'obviously' right category.

As such I would either use a devm action to do this stop, or not use the managed interfaces for everything in probe after the equivalent start.
The devm_add_action_or_reset would be the cleanest option I think.. Will also fix the cleanup on error in probe.

Quoted 30 lines
drivers/iio/chemical/sps30.c new file +30 -0
+
+	return 0;
+}
+
+static const struct i2c_device_id sps30_id[] = {
+	{ "sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, sps30_id);
+
+static const struct of_device_id sps30_of_match[] = {
+	{ .compatible = "sensirion,sps30" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, sps30_of_match);
+
+static struct i2c_driver sps30_driver = {
+	.driver = {
+		.name = "sps30",
+		.of_match_table = sps30_of_match,
+	},
+	.id_table = sps30_id,
+	.probe_new = sps30_probe,
+	.remove = sps30_remove,
+};
+module_i2c_driver(sps30_driver);
+
+MODULE_AUTHOR("Tomasz Duszynski <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor driver");
+MODULE_LICENSE("GPL v2");
#17 lore raw

Re: [PATCH 2/3] iio: chemical: add support for Sensirion SPS30 sensor

In reply to Tomasz Duszynski
Quoted 9 lines · Tomasz Duszynski

Add support for Sensirion SPS30 particulate matter sensor.

---
 drivers/iio/chemical/Kconfig  |  11 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sps30.c  | 359 ++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/chemical/sps30.c

[]

Quoted 1 line

+#define pr_fmt(fmt) "sps30: " fmt

I don't see its usage ?

Quoted 9 lines
drivers/iio/chemical/sps30.c new file +9 -0
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/module.h>

drivers/iio/chemical/sps30.c line 34

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+#define SPS30_CRC8_POLYNOMIAL 0x31
+
+/* SPS30 commands */
+#define SPS30_START_MEAS 0x0010
+#define SPS30_STOP_MEAS 0x0104
+#define SPS30_RESET 0xd304
+#define SPS30_READ_DATA_READY_FLAG 0x0202
+#define SPS30_READ_DATA 0x0300
+#define SPS30_READ_SERIAL 0xD033

Could you please put a tab and align these macros.

  #define SPS30_START_MEAS		0x0010
  #define SPS30_STOP_MEAS		0x0104

drivers/iio/chemical/sps30.c line 69

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+static int sps30_write_then_read(struct sps30_state *state, u8 *buf,
+				 int buf_size, u8 *data, int data_size)
+{
+	/* every two received data bytes are checksummed */
+	u8 tmp[data_size + data_size / 2];

No VLAs!

https://lore.kernel.org/lkml/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com/

Quoted 27 lines
drivers/iio/chemical/sps30.c new file +27 -0
+	int ret, i;
+
+	/*
+	 * Sensor does not support repeated start so instead of
+	 * sending two i2c messages in a row we just send one by one.
+	 */
+	ret = i2c_master_send(state->client, buf, buf_size);
+	if (ret != buf_size)
+		return ret < 0 ? ret : -EIO;
+
+	if (!data)
+		return 0;
+
+	ret = i2c_master_recv(state->client, tmp, sizeof(tmp));
+	if (ret != sizeof(tmp))
+		return ret < 0 ? ret : -EIO;
+
+	for (i = 0; i < sizeof(tmp); i += 3) {
+		u8 crc = crc8(sps30_crc8_table, &tmp[i], 2, CRC8_INIT_VALUE);
+
+		if (crc != tmp[i + 2]) {
+			dev_err(&state->client->dev,
+				"data integrity check failed\n");
+			return -EIO;
+		}
+
+		*data++ = tmp[i];

drivers/iio/chemical/sps30.c line 106

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+		*data++ = tmp[i + 1];
+	}
+
+	return 0;
+}
+
+static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
+{
+	/* depending on the command up to 3 bytes may be needed for argument */
+	u8 buf[sizeof(cmd) + 3] = { cmd >> 8, cmd };

This is fine, since sizeof returns constant integer expression.

Quoted 23 lines
drivers/iio/chemical/sps30.c new file +21 -0
+	switch (cmd) {
+	case SPS30_START_MEAS:
+		buf[2] = 0x03;
+		buf[3] = 0x00;
+		buf[4] = 0xac; /* precomputed crc */
+		return sps30_write_then_read(state, buf, 5, NULL, 0);
+	case SPS30_STOP_MEAS:
+	case SPS30_RESET:
+		return sps30_write_then_read(state, buf, 2, NULL, 0);
+	case SPS30_READ_DATA_READY_FLAG:
+	case SPS30_READ_DATA:
+	case SPS30_READ_SERIAL:
+		return sps30_write_then_read(state, buf, 2, data, size);
+	default:
+		return -EINVAL;
+	};
+}
 
 
+static int sps30_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{

drivers/iio/chemical/sps30.c

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+	struct sps30_state *state = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		switch (chan->type) {
+		case IIO_MASSCONCENTRATION:
+			mutex_lock(&state->lock);
+			switch (chan->channel2) {
+			case IIO_MOD_PM2p5:

I think lock should be placed here

drivers/iio/chemical/sps30.c line 228

Quoted 1 line
drivers/iio/chemical/sps30.c new file +1 -0
+				ret = sps30_do_meas(state, val, val2);

... and unlock here.

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+				break;
+			case IIO_MOD_PM10:
+				ret = sps30_do_meas(state, val2, val);
+				break;
+			default:
+				break;
+			}
+			mutex_unlock(&state->lock);
+			if (ret)
+				return ret;

drivers/iio/chemical/sps30.c line 248

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+}

[]

drivers/iio/chemical/sps30.c line 266

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+static int sps30_probe(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev;
+	struct sps30_state *state;
+	u8 buf[32] = { };

This is not valid in ISO-C but only in C++.

Instead,

	u8 buf[32] = {0};
Quoted 24 lines
drivers/iio/chemical/sps30.c new file +24 -0
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	state->client = client;
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sps30_info;
+	indio_dev->name = client->name;
+	indio_dev->channels = sps30_channels;
+	indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->available_scan_masks = sps30_scan_masks;
+
+	mutex_init(&state->lock);
+	crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
+
+	/*

drivers/iio/chemical/sps30.c line 300

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+	 * Power-on-reset causes sensor to produce some glitch on i2c bus
+	 * and some controllers end up in error state. Recover simply
+	 * by placing something on the bus.
+	 */
+	ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
+	if (ret) {
+		dev_err(&client->dev, "failed to reset device\n");
+		return ret;
+	}
+	usleep_range(2500000, 3500000);

Isn't that range too high ?
msleep_interruptible ?

Quoted 14 lines
drivers/iio/chemical/sps30.c new file +14 -0
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
+
+	ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
+	if (ret) {
+		dev_err(&client->dev, "failed to read serial number\n");
+		return ret;
+	}
+	dev_info(&client->dev, "serial number: %s\n", buf);
+
+	ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
+	if (ret) {
+		dev_err(&client->dev, "failed to start measurement\n");
+		return ret;
+	}

drivers/iio/chemical/sps30.c line 324

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      sps30_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
+
+static int sps30_remove(struct i2c_client *client)

Perfect candidate for devm_add_action_or_reset() ?

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sps30_state *state = iio_priv(indio_dev);
+
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
+
+	return 0;
+}
Signature · 4 lines
Himanshu Jha
Undergraduate Student
Department of Electronics & Communication
Guru Tegh Bahadur Institute of Technology
author maintainer
#18 lore raw

Re: [PATCH 2/3] iio: chemical: add support for Sensirion SPS30 sensor

Quoted 11 lines · Himanshu Jha
Quoted 9 lines · Tomasz Duszynski

Add support for Sensirion SPS30 particulate matter sensor.

---
 drivers/iio/chemical/Kconfig  |  11 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sps30.c  | 359 ++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/chemical/sps30.c
Quoted 6 lines

[]

Quoted 1 line

+#define pr_fmt(fmt) "sps30: " fmt

I don't see its usage ?

Hint: checkout how dev_err() is defined.

Quoted 22 lines
Quoted 9 lines
drivers/iio/chemical/sps30.c new file +9 -0
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/module.h>

drivers/iio/chemical/sps30.c line 34

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+#define SPS30_CRC8_POLYNOMIAL 0x31
+
+/* SPS30 commands */
+#define SPS30_START_MEAS 0x0010
+#define SPS30_STOP_MEAS 0x0104
+#define SPS30_RESET 0xd304
+#define SPS30_READ_DATA_READY_FLAG 0x0202
+#define SPS30_READ_DATA 0x0300
+#define SPS30_READ_SERIAL 0xD033

Could you please put a tab and align these macros.

Quoted 3 lines
  #define SPS30_START_MEAS		0x0010
  #define SPS30_STOP_MEAS		0x0104

In my opinion this sort of alignment does not pay off in the long run. Adding a new definition, a slightly longer one perhaps, can easily break formatting.

So I would stay with current one.

Quoted 11 lines

drivers/iio/chemical/sps30.c line 69

Quoted 6 lines
drivers/iio/chemical/sps30.c new file +5 -0
+static int sps30_write_then_read(struct sps30_state *state, u8 *buf,
+				 int buf_size, u8 *data, int data_size)
+{
+	/* every two received data bytes are checksummed */
+	u8 tmp[data_size + data_size / 2];

No VLAs!

https://lore.kernel.org/lkml/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com/

Looks like -Wvla is some fairly recent addition to KBUILD_CFLAGS.

Quoted 144 lines
Quoted 27 lines
drivers/iio/chemical/sps30.c new file +27 -0
+	int ret, i;
+
+	/*
+	 * Sensor does not support repeated start so instead of
+	 * sending two i2c messages in a row we just send one by one.
+	 */
+	ret = i2c_master_send(state->client, buf, buf_size);
+	if (ret != buf_size)
+		return ret < 0 ? ret : -EIO;
+
+	if (!data)
+		return 0;
+
+	ret = i2c_master_recv(state->client, tmp, sizeof(tmp));
+	if (ret != sizeof(tmp))
+		return ret < 0 ? ret : -EIO;
+
+	for (i = 0; i < sizeof(tmp); i += 3) {
+		u8 crc = crc8(sps30_crc8_table, &tmp[i], 2, CRC8_INIT_VALUE);
+
+		if (crc != tmp[i + 2]) {
+			dev_err(&state->client->dev,
+				"data integrity check failed\n");
+			return -EIO;
+		}
+
+		*data++ = tmp[i];

drivers/iio/chemical/sps30.c line 106

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+		*data++ = tmp[i + 1];
+	}
+
+	return 0;
+}
+
+static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
+{
+	/* depending on the command up to 3 bytes may be needed for argument */
+	u8 buf[sizeof(cmd) + 3] = { cmd >> 8, cmd };

This is fine, since sizeof returns constant integer expression.

Quoted 23 lines
drivers/iio/chemical/sps30.c new file +21 -0
+	switch (cmd) {
+	case SPS30_START_MEAS:
+		buf[2] = 0x03;
+		buf[3] = 0x00;
+		buf[4] = 0xac; /* precomputed crc */
+		return sps30_write_then_read(state, buf, 5, NULL, 0);
+	case SPS30_STOP_MEAS:
+	case SPS30_RESET:
+		return sps30_write_then_read(state, buf, 2, NULL, 0);
+	case SPS30_READ_DATA_READY_FLAG:
+	case SPS30_READ_DATA:
+	case SPS30_READ_SERIAL:
+		return sps30_write_then_read(state, buf, 2, data, size);
+	default:
+		return -EINVAL;
+	};
+}
 
 
+static int sps30_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{

drivers/iio/chemical/sps30.c

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+	struct sps30_state *state = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		switch (chan->type) {
+		case IIO_MASSCONCENTRATION:
+			mutex_lock(&state->lock);
+			switch (chan->channel2) {
+			case IIO_MOD_PM2p5:

I think lock should be placed here

drivers/iio/chemical/sps30.c line 228

Quoted 1 line
drivers/iio/chemical/sps30.c new file +1 -0
+				ret = sps30_do_meas(state, val, val2);

... and unlock here.

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+				break;
+			case IIO_MOD_PM10:
+				ret = sps30_do_meas(state, val2, val);
+				break;
+			default:
+				break;
+			}
+			mutex_unlock(&state->lock);
+			if (ret)
+				return ret;

drivers/iio/chemical/sps30.c line 248

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+}

[]

drivers/iio/chemical/sps30.c line 266

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+static int sps30_probe(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev;
+	struct sps30_state *state;
+	u8 buf[32] = { };

This is not valid in ISO-C but only in C++.

Instead,

	u8 buf[32] = {0};
Quoted 29 lines
drivers/iio/chemical/sps30.c new file +29 -0
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	state->client = client;
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sps30_info;
+	indio_dev->name = client->name;
+	indio_dev->channels = sps30_channels;
+	indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->available_scan_masks = sps30_scan_masks;
+
+	mutex_init(&state->lock);
+	crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
+
+	/*
+	 * Power-on-reset causes sensor to produce some glitch on i2c bus
+	 * and some controllers end up in error state. Recover simply
+	 * by placing something on the bus.
+	 */
+	ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
Quoted 9 lines

drivers/iio/chemical/sps30.c line 300

Quoted 5 lines
drivers/iio/chemical/sps30.c new file +5 -0
+	if (ret) {
+		dev_err(&client->dev, "failed to reset device\n");
+		return ret;
+	}
+	usleep_range(2500000, 3500000);

Isn't that range too high ?
msleep_interruptible ?

Might be.

Quoted 42 lines
Quoted 14 lines
drivers/iio/chemical/sps30.c new file +14 -0
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
+
+	ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
+	if (ret) {
+		dev_err(&client->dev, "failed to read serial number\n");
+		return ret;
+	}
+	dev_info(&client->dev, "serial number: %s\n", buf);
+
+	ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
+	if (ret) {
+		dev_err(&client->dev, "failed to start measurement\n");
+		return ret;
+	}

drivers/iio/chemical/sps30.c line 324

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      sps30_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
+
+static int sps30_remove(struct i2c_client *client)

Perfect candidate for devm_add_action_or_reset() ?

drivers/iio/chemical/sps30.c line 332

Quoted 8 lines
drivers/iio/chemical/sps30.c new file +8 -0
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sps30_state *state = iio_priv(indio_dev);
+
+	sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
+
+	return 0;
+}

--
Himanshu Jha
Undergraduate Student
Department of Electronics & Communication
Guru Tegh Bahadur Institute of Technology

#19 lore raw

Re: [PATCH 2/3] iio: chemical: add support for Sensirion SPS30 sensor

Quoted 47 lines · On Mon, 26 Nov 2018 21:48:07 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 11 lines · Himanshu Jha
Quoted 9 lines · Tomasz Duszynski

Add support for Sensirion SPS30 particulate matter sensor.

---
 drivers/iio/chemical/Kconfig  |  11 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sps30.c  | 359 ++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/chemical/sps30.c  
Quoted 6 lines

[]

Quoted 1 line

+#define pr_fmt(fmt) "sps30: " fmt

I don't see its usage ?

Hint: checkout how dev_err() is defined.

Quoted 22 lines
Quoted 9 lines
drivers/iio/chemical/sps30.c new file +9 -0
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/module.h>

drivers/iio/chemical/sps30.c line 34

Quoted 10 lines
drivers/iio/chemical/sps30.c new file +10 -0
+
+#define SPS30_CRC8_POLYNOMIAL 0x31
+
+/* SPS30 commands */
+#define SPS30_START_MEAS 0x0010
+#define SPS30_STOP_MEAS 0x0104
+#define SPS30_RESET 0xd304
+#define SPS30_READ_DATA_READY_FLAG 0x0202
+#define SPS30_READ_DATA 0x0300
+#define SPS30_READ_SERIAL 0xD033  

Could you please put a tab and align these macros.

Quoted 3 lines
  #define SPS30_START_MEAS		0x0010
  #define SPS30_STOP_MEAS		0x0104
Quoted 5 lines

In my opinion this sort of alignment does not pay off in the long run. Adding a new definition, a slightly longer one perhaps, can easily break formatting.

So I would stay with current one.

Personally I agree with you on this one. I don't care enough to say either way though normally!

Quoted 10 lines
Quoted 10 lines

drivers/iio/chemical/sps30.c line 69

Quoted 7 lines
drivers/iio/chemical/sps30.c new file +5 -0
+static int sps30_write_then_read(struct sps30_state *state, u8 *buf,
+				 int buf_size, u8 *data, int data_size)
+{
+	/* every two received data bytes are checksummed */
+	u8 tmp[data_size + data_size / 2];  

No VLAs!

Quoted 4 lines
Quoted 2 lines

https://lore.kernel.org/lkml/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com/

Looks like -Wvla is some fairly recent addition to KBUILD_CFLAGS.

Yeah, that was only after a 'lot' of effort over years to get the number present down to a small number.

I wrote plenty of them in the early days of IIO so got a lot of those patches as part of the move to introducing the warning :)

Jonathan

...

author maintainer
#20 lore raw

PATCH 3/3 iio: chemical: sps30: add device tree support

In reply to Tomasz Duszynski

Add device tree support for Sensirion SPS30 particulate matter sensor.

---
 .../bindings/iio/chemical/sensirion,sps30.txt        | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +12 -0
new file mode 100644
index 000000000000..6eee2709b5b6
@@ -0,0 +1,12 @@
+* Sensirion SPS30 particulate matter sensor
+
+Required properties:
+- compatible: must be "sensirion,sps30"
+- reg: the I2C address of the sensor
+
+Example:
+
+sps30@69 {
+	compatible = "sensirion,sps30";
+	reg = <0x69>;
+};
Signature · 1 line
2.19.2
#21 lore raw

Re: [PATCH 3/3] iio: chemical: sps30: add device tree support

Quoted 4 lines · On Sat, 24 Nov 2018 23:14:15 +0100 Tomasz Duszynski <[email protected]> wrote:

Add device tree support for Sensirion SPS30 particulate matter sensor.

one comment inine, around the fact we are trying to move to generic names in DT where ever possible. Now we don't have a suitable one (IIRC) yet so time to make one up ;)

+CC Rob for his input on that.

Quoted 11 lines
---
 .../bindings/iio/chemical/sensirion,sps30.txt        | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +0 -0
new file mode 100644
index 000000000000..6eee2709b5b6

Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt line 9

Quoted 9 lines
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +9 -0
@@ -0,0 +1,12 @@
+* Sensirion SPS30 particulate matter sensor
+
+Required properties:
+- compatible: must be "sensirion,sps30"
+- reg: the I2C address of the sensor
+
+Example:
+
+sps30@69 {

We should define a generic type. Rob, what would work for this one?

particlesensor@69?

Quoted 3 lines
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +3 -0
+	compatible = "sensirion,sps30";
+	reg = <0x69>;
+};
author maintainer
#22 lore raw

Re: [PATCH 3/3] iio: chemical: sps30: add device tree support

Quoted 35 lines · Jonathan Cameron
Quoted 4 lines · On Sat, 24 Nov 2018 23:14:15 +0100 Tomasz Duszynski <[email protected]> wrote:

Add device tree support for Sensirion SPS30 particulate matter sensor.

one comment inine, around the fact we are trying to move to generic names in DT where ever possible. Now we don't have a suitable one (IIRC) yet so time to make one up ;)

+CC Rob for his input on that.

Quoted 11 lines
---
 .../bindings/iio/chemical/sensirion,sps30.txt        | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +0 -0
new file mode 100644
index 000000000000..6eee2709b5b6

Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt line 9

Quoted 9 lines
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +9 -0
@@ -0,0 +1,12 @@
+* Sensirion SPS30 particulate matter sensor
+
+Required properties:
+- compatible: must be "sensirion,sps30"
+- reg: the I2C address of the sensor
+
+Example:
+
+sps30@69 {

We should define a generic type. Rob, what would work for this one?

Quoted 2 lines

particlesensor@69?

Wouldn't air-pollution-sensor be somewhat more generic? At least wikipedia has some article about that. Various other names like particle-sensor, pm-sensor, particulate-matter-sensor, air-quality-sensor, tend to return more or less similar number of search hits. Which means there's no universal naming convention.

Quoted 4 lines
Quoted 3 lines
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +3 -0
+	compatible = "sensirion,sps30";
+	reg = <0x69>;
+};
#23 lore raw

Re: [PATCH 3/3] iio: chemical: sps30: add device tree support

Quoted 36 lines · On Sun, 2 Dec 2018 19:29:44 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 35 lines · Jonathan Cameron
Quoted 4 lines · On Sat, 24 Nov 2018 23:14:15 +0100 Tomasz Duszynski <[email protected]> wrote:

Add device tree support for Sensirion SPS30 particulate matter sensor.

one comment inine, around the fact we are trying to move to generic names in DT where ever possible. Now we don't have a suitable one (IIRC) yet so time to make one up ;)

+CC Rob for his input on that.

Quoted 11 lines
---
 .../bindings/iio/chemical/sensirion,sps30.txt        | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +0 -0
new file mode 100644
index 000000000000..6eee2709b5b6

Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt line 9

Quoted 9 lines
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +9 -0
@@ -0,0 +1,12 @@
+* Sensirion SPS30 particulate matter sensor
+
+Required properties:
+- compatible: must be "sensirion,sps30"
+- reg: the I2C address of the sensor
+
+Example:
+
+sps30@69 {  

We should define a generic type. Rob, what would work for this one?

Quoted 8 lines
Quoted 2 lines

particlesensor@69?

Wouldn't air-pollution-sensor be somewhat more generic? At least wikipedia has some article about that. Various other names like particle-sensor, pm-sensor, particulate-matter-sensor, air-quality-sensor, tend to return more or less similar number of search hits. Which means there's no universal naming convention.

I have not strong feeling in favor of a particular choice. Happy with wherever the discussion converges.

Jonathan

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Documentation/devicetree/bindings/iio/chemical/sensirion,sps30.txt new file +3 -0
+	compatible = "sensirion,sps30";
+	reg = <0x69>;
+};  
#24 lore raw

Re: [PATCH 3/3] iio: chemical: sps30: add device tree support

In reply to Tomasz Duszynski
Thank you for the patch! Perhaps something to improve:

Hi Tomasz,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on iio/togreg]
[also build test WARNING on v4.20-rc4 next-20181123]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Tomasz-Duszynski/add-support-for-Sensirion-SPS30-PM-sensor/20181125-172634
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
config: x86_64-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

>> drivers/iio/chemical/sps30.c:69:26: warning: Variable length array is used.
   drivers/iio/chemical/sps30.c: In function 'sps30_read_raw':
   drivers/iio/chemical/sps30.c:237:7: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
       if (ret)
          ^

vim +69 drivers/iio/chemical/sps30.c

0e4f3167f Tomasz Duszynski 2018-11-24   64  
0e4f3167f Tomasz Duszynski 2018-11-24   65  static int sps30_write_then_read(struct sps30_state *state, u8 *buf,
0e4f3167f Tomasz Duszynski 2018-11-24   66  				 int buf_size, u8 *data, int data_size)
0e4f3167f Tomasz Duszynski 2018-11-24   67  {
0e4f3167f Tomasz Duszynski 2018-11-24   68  	/* every two received data bytes are checksummed */
0e4f3167f Tomasz Duszynski 2018-11-24  @69  	u8 tmp[data_size + data_size / 2];
0e4f3167f Tomasz Duszynski 2018-11-24   70  	int ret, i;
0e4f3167f Tomasz Duszynski 2018-11-24   71  
0e4f3167f Tomasz Duszynski 2018-11-24   72  	/*
0e4f3167f Tomasz Duszynski 2018-11-24   73  	 * Sensor does not support repeated start so instead of
0e4f3167f Tomasz Duszynski 2018-11-24   74  	 * sending two i2c messages in a row we just send one by one.
0e4f3167f Tomasz Duszynski 2018-11-24   75  	 */
0e4f3167f Tomasz Duszynski 2018-11-24   76  	ret = i2c_master_send(state->client, buf, buf_size);
0e4f3167f Tomasz Duszynski 2018-11-24   77  	if (ret != buf_size)
0e4f3167f Tomasz Duszynski 2018-11-24   78  		return ret < 0 ? ret : -EIO;
0e4f3167f Tomasz Duszynski 2018-11-24   79  
0e4f3167f Tomasz Duszynski 2018-11-24   80  	if (!data)
0e4f3167f Tomasz Duszynski 2018-11-24   81  		return 0;
0e4f3167f Tomasz Duszynski 2018-11-24   82  
0e4f3167f Tomasz Duszynski 2018-11-24   83  	ret = i2c_master_recv(state->client, tmp, sizeof(tmp));
0e4f3167f Tomasz Duszynski 2018-11-24   84  	if (ret != sizeof(tmp))
0e4f3167f Tomasz Duszynski 2018-11-24   85  		return ret < 0 ? ret : -EIO;
0e4f3167f Tomasz Duszynski 2018-11-24   86  
0e4f3167f Tomasz Duszynski 2018-11-24   87  	for (i = 0; i < sizeof(tmp); i += 3) {
0e4f3167f Tomasz Duszynski 2018-11-24   88  		u8 crc = crc8(sps30_crc8_table, &tmp[i], 2, CRC8_INIT_VALUE);
0e4f3167f Tomasz Duszynski 2018-11-24   89  
0e4f3167f Tomasz Duszynski 2018-11-24   90  		if (crc != tmp[i + 2]) {
0e4f3167f Tomasz Duszynski 2018-11-24   91  			dev_err(&state->client->dev,
0e4f3167f Tomasz Duszynski 2018-11-24   92  				"data integrity check failed\n");
0e4f3167f Tomasz Duszynski 2018-11-24   93  			return -EIO;
0e4f3167f Tomasz Duszynski 2018-11-24   94  		}
0e4f3167f Tomasz Duszynski 2018-11-24   95  
0e4f3167f Tomasz Duszynski 2018-11-24   96  		*data++ = tmp[i];
0e4f3167f Tomasz Duszynski 2018-11-24   97  		*data++ = tmp[i + 1];
0e4f3167f Tomasz Duszynski 2018-11-24   98  	}
0e4f3167f Tomasz Duszynski 2018-11-24   99  
0e4f3167f Tomasz Duszynski 2018-11-24  100  	return 0;
0e4f3167f Tomasz Duszynski 2018-11-24  101  }
0e4f3167f Tomasz Duszynski 2018-11-24  102  

:::::: The code at line 69 was first introduced by commit
:::::: 0e4f3167f739fa067d7e1ba672f0b46569d04d84 iio: chemical: add support for Sensirion SPS30 sensor

:::::: TO: Tomasz Duszynski <[email protected]> :::::: CC: 0day robot <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation