^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Murata ZPA2326 SPI pressure and temperature sensor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2016 Parrot S.A.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Gregor Boirie <gregor.boirie@parrot.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "zpa2326.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * read_flag_mask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * - address bit 7 must be set to request a register read operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * - address bit 6 must be set to request register address auto increment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static const struct regmap_config zpa2326_regmap_spi_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .writeable_reg = zpa2326_isreg_writeable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) .readable_reg = zpa2326_isreg_readable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .precious_reg = zpa2326_isreg_precious,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .max_register = ZPA2326_TEMP_OUT_H_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) .read_flag_mask = BIT(7) | BIT(6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .cache_type = REGCACHE_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int zpa2326_probe_spi(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) regmap = devm_regmap_init_spi(spi, &zpa2326_regmap_spi_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (IS_ERR(regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) dev_err(&spi->dev, "failed to init registers map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return PTR_ERR(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Enforce SPI slave settings to prevent from DT misconfiguration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Clock is idle high. Sampling happens on trailing edge, i.e., rising
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * edge. Maximum bus frequency is 1 MHz. Registers are 8 bits wide.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) spi->mode = SPI_MODE_3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) spi->max_speed_hz = min(spi->max_speed_hz, 1000000U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) spi->bits_per_word = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) err = spi_setup(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return zpa2326_probe(&spi->dev, spi_get_device_id(spi)->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) spi->irq, ZPA2326_DEVICE_ID, regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int zpa2326_remove_spi(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) zpa2326_remove(&spi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static const struct spi_device_id zpa2326_spi_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) { "zpa2326", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) MODULE_DEVICE_TABLE(spi, zpa2326_spi_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static const struct of_device_id zpa2326_spi_matches[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) { .compatible = "murata,zpa2326" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) MODULE_DEVICE_TABLE(of, zpa2326_spi_matches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static struct spi_driver zpa2326_spi_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .name = "zpa2326-spi",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .of_match_table = zpa2326_spi_matches,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .pm = ZPA2326_PM_OPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .probe = zpa2326_probe_spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .remove = zpa2326_remove_spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .id_table = zpa2326_spi_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) module_spi_driver(zpa2326_spi_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) MODULE_DESCRIPTION("SPI driver for Murata ZPA2326 pressure sensor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) MODULE_LICENSE("GPL v2");