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