1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics hts221 spi driver
4  *
5  * Copyright 2016 STMicroelectronics Inc.
6  *
7  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/spi/spi.h>
13 #include <linux/slab.h>
14 #include <linux/regmap.h>
15 
16 #include "hts221.h"
17 
18 #define HTS221_SPI_READ			BIT(7)
19 #define HTS221_SPI_AUTO_INCREMENT	BIT(6)
20 
21 static const struct regmap_config hts221_spi_regmap_config = {
22 	.reg_bits = 8,
23 	.val_bits = 8,
24 	.write_flag_mask = HTS221_SPI_AUTO_INCREMENT,
25 	.read_flag_mask = HTS221_SPI_READ | HTS221_SPI_AUTO_INCREMENT,
26 };
27 
28 static int hts221_spi_probe(struct spi_device *spi)
29 {
30 	struct regmap *regmap;
31 
32 	regmap = devm_regmap_init_spi(spi, &hts221_spi_regmap_config);
33 	if (IS_ERR(regmap)) {
34 		dev_err(&spi->dev, "Failed to register spi regmap %ld\n",
35 			PTR_ERR(regmap));
36 		return PTR_ERR(regmap);
37 	}
38 
39 	return hts221_probe(&spi->dev, spi->irq,
40 			    spi->modalias, regmap);
41 }
42 
43 static const struct of_device_id hts221_spi_of_match[] = {
44 	{ .compatible = "st,hts221", },
45 	{},
46 };
47 MODULE_DEVICE_TABLE(of, hts221_spi_of_match);
48 
49 static const struct spi_device_id hts221_spi_id_table[] = {
50 	{ HTS221_DEV_NAME },
51 	{},
52 };
53 MODULE_DEVICE_TABLE(spi, hts221_spi_id_table);
54 
55 static struct spi_driver hts221_driver = {
56 	.driver = {
57 		.name = "hts221_spi",
58 		.pm = &hts221_pm_ops,
59 		.of_match_table = hts221_spi_of_match,
60 	},
61 	.probe = hts221_spi_probe,
62 	.id_table = hts221_spi_id_table,
63 };
64 module_spi_driver(hts221_driver);
65 
66 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
67 MODULE_DESCRIPTION("STMicroelectronics hts221 spi driver");
68 MODULE_LICENSE("GPL v2");
69