summaryrefslogtreecommitdiffstats
path: root/drivers/iio/pressure/mprls0025pa_i2c.c
blob: 7a5c5aa2b456b9af50637143b823b1bd4cbbb172 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * MPRLS0025PA - Honeywell MicroPressure pressure sensor series driver
 *
 * Copyright (c) Andreas Klinger <ak@it-klinger.de>
 *
 * Data sheet:
 *  https://prod-edam.honeywell.com/content/dam/honeywell-edam/sps/siot/en-us/products/sensors/pressure-sensors/board-mount-pressure-sensors/micropressure-mpr-series/documents/sps-siot-mpr-series-datasheet-32332628-ciid-172626.pdf
 */

#include <linux/device.h>
#include <linux/errno.h>
#include <linux/i2c.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/types.h>

#include "mprls0025pa.h"

static int mpr_i2c_init(struct device *unused)
{
	return 0;
}

static int mpr_i2c_read(struct mpr_data *data, const u8 unused, const u8 cnt)
{
	int ret;
	struct i2c_client *client = to_i2c_client(data->dev);

	if (cnt > MPR_MEASUREMENT_RD_SIZE)
		return -EOVERFLOW;

	memset(data->buffer, 0, MPR_MEASUREMENT_RD_SIZE);
	ret = i2c_master_recv(client, data->buffer, cnt);
	if (ret < 0)
		return ret;
	else if (ret != cnt)
		return -EIO;

	return 0;
}

static int mpr_i2c_write(struct mpr_data *data, const u8 cmd, const u8 unused)
{
	int ret;
	struct i2c_client *client = to_i2c_client(data->dev);
	u8 wdata[MPR_PKT_SYNC_LEN];

	memset(wdata, 0, sizeof(wdata));
	wdata[0] = cmd;

	ret = i2c_master_send(client, wdata, MPR_PKT_SYNC_LEN);
	if (ret < 0)
		return ret;
	else if (ret != MPR_PKT_SYNC_LEN)
		return -EIO;

	return 0;
}

static const struct mpr_ops mpr_i2c_ops = {
	.init = mpr_i2c_init,
	.read = mpr_i2c_read,
	.write = mpr_i2c_write,
};

static int mpr_i2c_probe(struct i2c_client *client)
{
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE))
		return -EOPNOTSUPP;

	return mpr_common_probe(&client->dev, &mpr_i2c_ops, client->irq);
}

static const struct of_device_id mpr_i2c_match[] = {
	{ .compatible = "honeywell,mprls0025pa" },
	{}
};
MODULE_DEVICE_TABLE(of, mpr_i2c_match);

static const struct i2c_device_id mpr_i2c_id[] = {
	{ "mprls0025pa" },
	{}
};
MODULE_DEVICE_TABLE(i2c, mpr_i2c_id);

static struct i2c_driver mpr_i2c_driver = {
	.probe = mpr_i2c_probe,
	.id_table = mpr_i2c_id,
	.driver = {
		.name = "mprls0025pa",
		.of_match_table = mpr_i2c_match,
	},
};
module_i2c_driver(mpr_i2c_driver);

MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
MODULE_DESCRIPTION("Honeywell MPR pressure sensor i2c driver");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS(IIO_HONEYWELL_MPRLS0025PA);