perf python: Do not force closing original perf descriptor in evlist.get_pollfd()
[linux/fpc-iii.git] / drivers / iio / chemical / bme680_spi.c
blobc9fb05e8d0b9a926e3e0d750673192cd9eb772c9
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * BME680 - SPI Driver
5 * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com>
6 */
7 #include <linux/acpi.h>
8 #include <linux/module.h>
9 #include <linux/regmap.h>
10 #include <linux/spi/spi.h>
12 #include "bme680.h"
14 static int bme680_regmap_spi_write(void *context, const void *data,
15 size_t count)
17 struct spi_device *spi = context;
18 u8 buf[2];
20 memcpy(buf, data, 2);
22 * The SPI register address (= full register address without bit 7)
23 * and the write command (bit7 = RW = '0')
25 buf[0] &= ~0x80;
27 return spi_write_then_read(spi, buf, 2, NULL, 0);
30 static int bme680_regmap_spi_read(void *context, const void *reg,
31 size_t reg_size, void *val, size_t val_size)
33 struct spi_device *spi = context;
35 return spi_write_then_read(spi, reg, reg_size, val, val_size);
38 static struct regmap_bus bme680_regmap_bus = {
39 .write = bme680_regmap_spi_write,
40 .read = bme680_regmap_spi_read,
41 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
42 .val_format_endian_default = REGMAP_ENDIAN_BIG,
45 static int bme680_spi_probe(struct spi_device *spi)
47 const struct spi_device_id *id = spi_get_device_id(spi);
48 struct regmap *regmap;
49 unsigned int val;
50 int ret;
52 spi->bits_per_word = 8;
53 ret = spi_setup(spi);
54 if (ret < 0) {
55 dev_err(&spi->dev, "spi_setup failed!\n");
56 return ret;
59 regmap = devm_regmap_init(&spi->dev, &bme680_regmap_bus,
60 &spi->dev, &bme680_regmap_config);
61 if (IS_ERR(regmap)) {
62 dev_err(&spi->dev, "Failed to register spi regmap %d\n",
63 (int)PTR_ERR(regmap));
64 return PTR_ERR(regmap);
67 ret = regmap_write(regmap, BME680_REG_SOFT_RESET_SPI,
68 BME680_CMD_SOFTRESET);
69 if (ret < 0) {
70 dev_err(&spi->dev, "Failed to reset chip\n");
71 return ret;
74 /* after power-on reset, Page 0(0x80-0xFF) of spi_mem_page is active */
75 ret = regmap_read(regmap, BME680_REG_CHIP_SPI_ID, &val);
76 if (ret < 0) {
77 dev_err(&spi->dev, "Error reading SPI chip ID\n");
78 return ret;
81 if (val != BME680_CHIP_ID_VAL) {
82 dev_err(&spi->dev, "Wrong chip ID, got %x expected %x\n",
83 val, BME680_CHIP_ID_VAL);
84 return -ENODEV;
87 * select Page 1 of spi_mem_page to enable access to
88 * to registers from address 0x00 to 0x7F.
90 ret = regmap_write_bits(regmap, BME680_REG_STATUS,
91 BME680_SPI_MEM_PAGE_BIT,
92 BME680_SPI_MEM_PAGE_1_VAL);
93 if (ret < 0) {
94 dev_err(&spi->dev, "failed to set page 1 of spi_mem_page\n");
95 return ret;
98 return bme680_core_probe(&spi->dev, regmap, id->name);
101 static const struct spi_device_id bme680_spi_id[] = {
102 {"bme680", 0},
105 MODULE_DEVICE_TABLE(spi, bme680_spi_id);
107 static const struct acpi_device_id bme680_acpi_match[] = {
108 {"BME0680", 0},
111 MODULE_DEVICE_TABLE(acpi, bme680_acpi_match);
113 static struct spi_driver bme680_spi_driver = {
114 .driver = {
115 .name = "bme680_spi",
116 .acpi_match_table = ACPI_PTR(bme680_acpi_match),
118 .probe = bme680_spi_probe,
119 .id_table = bme680_spi_id,
121 module_spi_driver(bme680_spi_driver);
123 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
124 MODULE_DESCRIPTION("Bosch BME680 SPI driver");
125 MODULE_LICENSE("GPL v2");