xref: /linux/drivers/iio/pressure/bmp280-i2c.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/i2c.h>
4 #include <linux/acpi.h>
5 #include <linux/of.h>
6 #include <linux/regmap.h>
7 
8 #include "bmp280.h"
9 
10 static int bmp280_i2c_probe(struct i2c_client *client,
11 			    const struct i2c_device_id *id)
12 {
13 	struct regmap *regmap;
14 	const struct regmap_config *regmap_config;
15 
16 	switch (id->driver_data) {
17 	case BMP180_CHIP_ID:
18 		regmap_config = &bmp180_regmap_config;
19 		break;
20 	case BMP280_CHIP_ID:
21 	case BME280_CHIP_ID:
22 		regmap_config = &bmp280_regmap_config;
23 		break;
24 	default:
25 		return -EINVAL;
26 	}
27 
28 	regmap = devm_regmap_init_i2c(client, regmap_config);
29 	if (IS_ERR(regmap)) {
30 		dev_err(&client->dev, "failed to allocate register map\n");
31 		return PTR_ERR(regmap);
32 	}
33 
34 	return bmp280_common_probe(&client->dev,
35 				   regmap,
36 				   id->driver_data,
37 				   id->name,
38 				   client->irq);
39 }
40 
41 static int bmp280_i2c_remove(struct i2c_client *client)
42 {
43 	return bmp280_common_remove(&client->dev);
44 }
45 
46 static const struct acpi_device_id bmp280_acpi_i2c_match[] = {
47 	{"BMP0280", BMP280_CHIP_ID },
48 	{"BMP0180", BMP180_CHIP_ID },
49 	{"BMP0085", BMP180_CHIP_ID },
50 	{"BME0280", BME280_CHIP_ID },
51 	{ },
52 };
53 MODULE_DEVICE_TABLE(acpi, bmp280_acpi_i2c_match);
54 
55 #ifdef CONFIG_OF
56 static const struct of_device_id bmp280_of_i2c_match[] = {
57 	{ .compatible = "bosch,bme280", .data = (void *)BME280_CHIP_ID },
58 	{ .compatible = "bosch,bmp280", .data = (void *)BMP280_CHIP_ID },
59 	{ .compatible = "bosch,bmp180", .data = (void *)BMP180_CHIP_ID },
60 	{ .compatible = "bosch,bmp085", .data = (void *)BMP180_CHIP_ID },
61 	{ },
62 };
63 MODULE_DEVICE_TABLE(of, bmp280_of_i2c_match);
64 #else
65 #define bmp280_of_i2c_match NULL
66 #endif
67 
68 static const struct i2c_device_id bmp280_i2c_id[] = {
69 	{"bmp280", BMP280_CHIP_ID },
70 	{"bmp180", BMP180_CHIP_ID },
71 	{"bmp085", BMP180_CHIP_ID },
72 	{"bme280", BME280_CHIP_ID },
73 	{ },
74 };
75 MODULE_DEVICE_TABLE(i2c, bmp280_i2c_id);
76 
77 static struct i2c_driver bmp280_i2c_driver = {
78 	.driver = {
79 		.name	= "bmp280",
80 		.acpi_match_table = ACPI_PTR(bmp280_acpi_i2c_match),
81 		.of_match_table = of_match_ptr(bmp280_of_i2c_match),
82 		.pm = &bmp280_dev_pm_ops,
83 	},
84 	.probe		= bmp280_i2c_probe,
85 	.remove		= bmp280_i2c_remove,
86 	.id_table	= bmp280_i2c_id,
87 };
88 module_i2c_driver(bmp280_i2c_driver);
89 
90 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
91 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
92 MODULE_LICENSE("GPL v2");
93