summaryrefslogtreecommitdiffstats
path: root/drivers/iio/chemical/ags02ma.c
blob: 8fcd80946543638957fd7bb2ba5bab0388b66629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2023 Anshul Dalal <anshulusr@gmail.com>
 *
 * Driver for Aosong AGS02MA
 *
 * Datasheet:
 *   https://asairsensors.com/wp-content/uploads/2021/09/AGS02MA.pdf
 * Product Page:
 *   http://www.aosong.com/m/en/products-33.html
 */

#include <linux/crc8.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/module.h>

#include <linux/iio/iio.h>

#define AGS02MA_TVOC_READ_REG		   0x00
#define AGS02MA_VERSION_REG		   0x11

#define AGS02MA_VERSION_PROCESSING_DELAY   30
#define AGS02MA_TVOC_READ_PROCESSING_DELAY 1500

#define AGS02MA_CRC8_INIT		   0xff
#define AGS02MA_CRC8_POLYNOMIAL		   0x31

DECLARE_CRC8_TABLE(ags02ma_crc8_table);

struct ags02ma_data {
	struct i2c_client *client;
};

struct ags02ma_reading {
	__be32 data;
	u8 crc;
} __packed;

static int ags02ma_register_read(struct i2c_client *client, u8 reg, u16 delay,
				 u32 *val)
{
	int ret;
	u8 crc;
	struct ags02ma_reading read_buffer;

	ret = i2c_master_send(client, &reg, sizeof(reg));
	if (ret < 0) {
		dev_err(&client->dev,
			"Failed to send data to register 0x%x: %d", reg, ret);
		return ret;
	}

	/* Processing Delay, Check Table 7.7 in the datasheet */
	msleep_interruptible(delay);

	ret = i2c_master_recv(client, (u8 *)&read_buffer, sizeof(read_buffer));
	if (ret < 0) {
		dev_err(&client->dev,
			"Failed to receive from register 0x%x: %d", reg, ret);
		return ret;
	}

	crc = crc8(ags02ma_crc8_table, (u8 *)&read_buffer.data,
		   sizeof(read_buffer.data), AGS02MA_CRC8_INIT);
	if (crc != read_buffer.crc) {
		dev_err(&client->dev, "CRC error\n");
		return -EIO;
	}

	*val = be32_to_cpu(read_buffer.data);
	return 0;
}

static int ags02ma_read_raw(struct iio_dev *iio_device,
			    struct iio_chan_spec const *chan, int *val,
			    int *val2, long mask)
{
	int ret;
	struct ags02ma_data *data = iio_priv(iio_device);

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		ret = ags02ma_register_read(data->client, AGS02MA_TVOC_READ_REG,
					    AGS02MA_TVOC_READ_PROCESSING_DELAY,
					    val);
		if (ret < 0)
			return ret;
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_SCALE:
		/* The sensor reads data as ppb */
		*val = 0;
		*val2 = 100;
		return IIO_VAL_INT_PLUS_NANO;
	default:
		return -EINVAL;
	}
}

static const struct iio_info ags02ma_info = {
	.read_raw = ags02ma_read_raw,
};

static const struct iio_chan_spec ags02ma_channel = {
	.type = IIO_CONCENTRATION,
	.channel2 = IIO_MOD_VOC,
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
		BIT(IIO_CHAN_INFO_SCALE),
};

static int ags02ma_probe(struct i2c_client *client)
{
	int ret;
	struct ags02ma_data *data;
	struct iio_dev *indio_dev;
	u32 version;

	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
	if (!indio_dev)
		return -ENOMEM;

	crc8_populate_msb(ags02ma_crc8_table, AGS02MA_CRC8_POLYNOMIAL);

	ret = ags02ma_register_read(client, AGS02MA_VERSION_REG,
				    AGS02MA_VERSION_PROCESSING_DELAY, &version);
	if (ret < 0)
		return dev_err_probe(&client->dev, ret,
			      "Failed to read device version\n");
	dev_dbg(&client->dev, "Aosong AGS02MA, Version: 0x%x", version);

	data = iio_priv(indio_dev);
	data->client = client;
	indio_dev->info = &ags02ma_info;
	indio_dev->channels = &ags02ma_channel;
	indio_dev->num_channels = 1;
	indio_dev->name = "ags02ma";

	return devm_iio_device_register(&client->dev, indio_dev);
}

static const struct i2c_device_id ags02ma_id_table[] = {
	{ "ags02ma" },
	{ /* Sentinel */ }
};
MODULE_DEVICE_TABLE(i2c, ags02ma_id_table);

static const struct of_device_id ags02ma_of_table[] = {
	{ .compatible = "aosong,ags02ma" },
	{ /* Sentinel */ }
};
MODULE_DEVICE_TABLE(of, ags02ma_of_table);

static struct i2c_driver ags02ma_driver = {
	.driver = {
		.name = "ags02ma",
		.of_match_table = ags02ma_of_table,
	},
	.id_table = ags02ma_id_table,
	.probe = ags02ma_probe,
};
module_i2c_driver(ags02ma_driver);

MODULE_AUTHOR("Anshul Dalal <anshulusr@gmail.com>");
MODULE_DESCRIPTION("Aosong AGS02MA TVOC Driver");
MODULE_LICENSE("GPL");