GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / iio / pressure / zpa2326_spi.c
1 /*
2  * Murata ZPA2326 SPI pressure and temperature sensor driver
3  *
4  * Copyright (c) 2016 Parrot S.A.
5  *
6  * Author: Gregor Boirie <gregor.boirie@parrot.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  */
17
18 #include <linux/module.h>
19 #include <linux/regmap.h>
20 #include <linux/spi/spi.h>
21 #include <linux/of_device.h>
22 #include "zpa2326.h"
23
24 /*
25  * read_flag_mask:
26  *   - address bit 7 must be set to request a register read operation
27  *   - address bit 6 must be set to request register address auto increment
28  */
29 static const struct regmap_config zpa2326_regmap_spi_config = {
30         .reg_bits       = 8,
31         .val_bits       = 8,
32         .writeable_reg  = zpa2326_isreg_writeable,
33         .readable_reg   = zpa2326_isreg_readable,
34         .precious_reg   = zpa2326_isreg_precious,
35         .max_register   = ZPA2326_TEMP_OUT_H_REG,
36         .read_flag_mask = BIT(7) | BIT(6),
37         .cache_type     = REGCACHE_NONE,
38 };
39
40 static int zpa2326_probe_spi(struct spi_device *spi)
41 {
42         struct regmap *regmap;
43         int            err;
44
45         regmap = devm_regmap_init_spi(spi, &zpa2326_regmap_spi_config);
46         if (IS_ERR(regmap)) {
47                 dev_err(&spi->dev, "failed to init registers map");
48                 return PTR_ERR(regmap);
49         }
50
51         /*
52          * Enforce SPI slave settings to prevent from DT misconfiguration.
53          *
54          * Clock is idle high. Sampling happens on trailing edge, i.e., rising
55          * edge. Maximum bus frequency is 1 MHz. Registers are 8 bits wide.
56          */
57         spi->mode = SPI_MODE_3;
58         spi->max_speed_hz = min(spi->max_speed_hz, 1000000U);
59         spi->bits_per_word = 8;
60         err = spi_setup(spi);
61         if (err < 0)
62                 return err;
63
64         return zpa2326_probe(&spi->dev, spi_get_device_id(spi)->name,
65                              spi->irq, ZPA2326_DEVICE_ID, regmap);
66 }
67
68 static int zpa2326_remove_spi(struct spi_device *spi)
69 {
70         zpa2326_remove(&spi->dev);
71
72         return 0;
73 }
74
75 static const struct spi_device_id zpa2326_spi_ids[] = {
76         { "zpa2326", 0 },
77         { },
78 };
79 MODULE_DEVICE_TABLE(spi, zpa2326_spi_ids);
80
81 #if defined(CONFIG_OF)
82 static const struct of_device_id zpa2326_spi_matches[] = {
83         { .compatible = "murata,zpa2326" },
84         { }
85 };
86 MODULE_DEVICE_TABLE(of, zpa2326_spi_matches);
87 #endif
88
89 static struct spi_driver zpa2326_spi_driver = {
90         .driver = {
91                 .name           = "zpa2326-spi",
92                 .of_match_table = of_match_ptr(zpa2326_spi_matches),
93                 .pm             = ZPA2326_PM_OPS,
94         },
95         .probe    = zpa2326_probe_spi,
96         .remove   = zpa2326_remove_spi,
97         .id_table = zpa2326_spi_ids,
98 };
99 module_spi_driver(zpa2326_spi_driver);
100
101 MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
102 MODULE_DESCRIPTION("SPI driver for Murata ZPA2326 pressure sensor");
103 MODULE_LICENSE("GPL v2");