xref: /openbmc/linux/drivers/iio/humidity/hts221_spi.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1fda8d26eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e4a70e3eSLorenzo Bianconi /*
3e4a70e3eSLorenzo Bianconi  * STMicroelectronics hts221 spi driver
4e4a70e3eSLorenzo Bianconi  *
5e4a70e3eSLorenzo Bianconi  * Copyright 2016 STMicroelectronics Inc.
6e4a70e3eSLorenzo Bianconi  *
7e4a70e3eSLorenzo Bianconi  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
8e4a70e3eSLorenzo Bianconi  */
9e4a70e3eSLorenzo Bianconi 
10e4a70e3eSLorenzo Bianconi #include <linux/kernel.h>
11e4a70e3eSLorenzo Bianconi #include <linux/module.h>
12e4a70e3eSLorenzo Bianconi #include <linux/spi/spi.h>
13e4a70e3eSLorenzo Bianconi #include <linux/slab.h>
1462177922SLorenzo Bianconi #include <linux/regmap.h>
1562177922SLorenzo Bianconi 
16e4a70e3eSLorenzo Bianconi #include "hts221.h"
17e4a70e3eSLorenzo Bianconi 
1862177922SLorenzo Bianconi #define HTS221_SPI_READ			BIT(7)
1962177922SLorenzo Bianconi #define HTS221_SPI_AUTO_INCREMENT	BIT(6)
20e4a70e3eSLorenzo Bianconi 
2162177922SLorenzo Bianconi static const struct regmap_config hts221_spi_regmap_config = {
2262177922SLorenzo Bianconi 	.reg_bits = 8,
2362177922SLorenzo Bianconi 	.val_bits = 8,
2462177922SLorenzo Bianconi 	.write_flag_mask = HTS221_SPI_AUTO_INCREMENT,
2562177922SLorenzo Bianconi 	.read_flag_mask = HTS221_SPI_READ | HTS221_SPI_AUTO_INCREMENT,
26e4a70e3eSLorenzo Bianconi };
27e4a70e3eSLorenzo Bianconi 
hts221_spi_probe(struct spi_device * spi)28e4a70e3eSLorenzo Bianconi static int hts221_spi_probe(struct spi_device *spi)
29e4a70e3eSLorenzo Bianconi {
3062177922SLorenzo Bianconi 	struct regmap *regmap;
3162177922SLorenzo Bianconi 
3262177922SLorenzo Bianconi 	regmap = devm_regmap_init_spi(spi, &hts221_spi_regmap_config);
3362177922SLorenzo Bianconi 	if (IS_ERR(regmap)) {
34144eb562SAndy Shevchenko 		dev_err(&spi->dev, "Failed to register spi regmap %ld\n",
35144eb562SAndy Shevchenko 			PTR_ERR(regmap));
3662177922SLorenzo Bianconi 		return PTR_ERR(regmap);
3762177922SLorenzo Bianconi 	}
3862177922SLorenzo Bianconi 
39e1ca1141SLorenzo Bianconi 	return hts221_probe(&spi->dev, spi->irq,
4062177922SLorenzo Bianconi 			    spi->modalias, regmap);
41e4a70e3eSLorenzo Bianconi }
42e4a70e3eSLorenzo Bianconi 
43e4a70e3eSLorenzo Bianconi static const struct of_device_id hts221_spi_of_match[] = {
44e4a70e3eSLorenzo Bianconi 	{ .compatible = "st,hts221", },
45e4a70e3eSLorenzo Bianconi 	{},
46e4a70e3eSLorenzo Bianconi };
47e4a70e3eSLorenzo Bianconi MODULE_DEVICE_TABLE(of, hts221_spi_of_match);
48e4a70e3eSLorenzo Bianconi 
49e4a70e3eSLorenzo Bianconi static const struct spi_device_id hts221_spi_id_table[] = {
50e4a70e3eSLorenzo Bianconi 	{ HTS221_DEV_NAME },
51e4a70e3eSLorenzo Bianconi 	{},
52e4a70e3eSLorenzo Bianconi };
53e4a70e3eSLorenzo Bianconi MODULE_DEVICE_TABLE(spi, hts221_spi_id_table);
54e4a70e3eSLorenzo Bianconi 
55e4a70e3eSLorenzo Bianconi static struct spi_driver hts221_driver = {
56e4a70e3eSLorenzo Bianconi 	.driver = {
57e4a70e3eSLorenzo Bianconi 		.name = "hts221_spi",
582129f25dSJonathan Cameron 		.pm = pm_sleep_ptr(&hts221_pm_ops),
5992c3e93bSAndy Shevchenko 		.of_match_table = hts221_spi_of_match,
60e4a70e3eSLorenzo Bianconi 	},
61e4a70e3eSLorenzo Bianconi 	.probe = hts221_spi_probe,
62e4a70e3eSLorenzo Bianconi 	.id_table = hts221_spi_id_table,
63e4a70e3eSLorenzo Bianconi };
64e4a70e3eSLorenzo Bianconi module_spi_driver(hts221_driver);
65e4a70e3eSLorenzo Bianconi 
66e4a70e3eSLorenzo Bianconi MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
67e4a70e3eSLorenzo Bianconi MODULE_DESCRIPTION("STMicroelectronics hts221 spi driver");
68e4a70e3eSLorenzo Bianconi MODULE_LICENSE("GPL v2");
69*1300ab39SJonathan Cameron MODULE_IMPORT_NS(IIO_HTS221);
70