xref: /linux/drivers/iio/pressure/zpa2326_spi.c (revision a409d2b6391376180d4440a4336d4e67ee4fd754)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
203b262f2SGregor Boirie /*
303b262f2SGregor Boirie  * Murata ZPA2326 SPI pressure and temperature sensor driver
403b262f2SGregor Boirie  *
503b262f2SGregor Boirie  * Copyright (c) 2016 Parrot S.A.
603b262f2SGregor Boirie  *
703b262f2SGregor Boirie  * Author: Gregor Boirie <gregor.boirie@parrot.com>
803b262f2SGregor Boirie  */
903b262f2SGregor Boirie 
1003b262f2SGregor Boirie #include <linux/module.h>
1103b262f2SGregor Boirie #include <linux/regmap.h>
1203b262f2SGregor Boirie #include <linux/spi/spi.h>
13*a409d2b6SJonathan Cameron #include <linux/mod_devicetable.h>
1403b262f2SGregor Boirie #include "zpa2326.h"
1503b262f2SGregor Boirie 
1603b262f2SGregor Boirie /*
1703b262f2SGregor Boirie  * read_flag_mask:
1803b262f2SGregor Boirie  *   - address bit 7 must be set to request a register read operation
1903b262f2SGregor Boirie  *   - address bit 6 must be set to request register address auto increment
2003b262f2SGregor Boirie  */
2103b262f2SGregor Boirie static const struct regmap_config zpa2326_regmap_spi_config = {
2203b262f2SGregor Boirie 	.reg_bits       = 8,
2303b262f2SGregor Boirie 	.val_bits       = 8,
2403b262f2SGregor Boirie 	.writeable_reg  = zpa2326_isreg_writeable,
2503b262f2SGregor Boirie 	.readable_reg   = zpa2326_isreg_readable,
2603b262f2SGregor Boirie 	.precious_reg   = zpa2326_isreg_precious,
2703b262f2SGregor Boirie 	.max_register   = ZPA2326_TEMP_OUT_H_REG,
2803b262f2SGregor Boirie 	.read_flag_mask = BIT(7) | BIT(6),
2903b262f2SGregor Boirie 	.cache_type     = REGCACHE_NONE,
3003b262f2SGregor Boirie };
3103b262f2SGregor Boirie 
3203b262f2SGregor Boirie static int zpa2326_probe_spi(struct spi_device *spi)
3303b262f2SGregor Boirie {
3403b262f2SGregor Boirie 	struct regmap *regmap;
3503b262f2SGregor Boirie 	int            err;
3603b262f2SGregor Boirie 
3703b262f2SGregor Boirie 	regmap = devm_regmap_init_spi(spi, &zpa2326_regmap_spi_config);
3803b262f2SGregor Boirie 	if (IS_ERR(regmap)) {
3903b262f2SGregor Boirie 		dev_err(&spi->dev, "failed to init registers map");
4003b262f2SGregor Boirie 		return PTR_ERR(regmap);
4103b262f2SGregor Boirie 	}
4203b262f2SGregor Boirie 
4303b262f2SGregor Boirie 	/*
4403b262f2SGregor Boirie 	 * Enforce SPI slave settings to prevent from DT misconfiguration.
4503b262f2SGregor Boirie 	 *
4603b262f2SGregor Boirie 	 * Clock is idle high. Sampling happens on trailing edge, i.e., rising
4703b262f2SGregor Boirie 	 * edge. Maximum bus frequency is 1 MHz. Registers are 8 bits wide.
4803b262f2SGregor Boirie 	 */
4903b262f2SGregor Boirie 	spi->mode = SPI_MODE_3;
5003b262f2SGregor Boirie 	spi->max_speed_hz = min(spi->max_speed_hz, 1000000U);
5103b262f2SGregor Boirie 	spi->bits_per_word = 8;
5203b262f2SGregor Boirie 	err = spi_setup(spi);
5303b262f2SGregor Boirie 	if (err < 0)
5403b262f2SGregor Boirie 		return err;
5503b262f2SGregor Boirie 
5603b262f2SGregor Boirie 	return zpa2326_probe(&spi->dev, spi_get_device_id(spi)->name,
5703b262f2SGregor Boirie 			     spi->irq, ZPA2326_DEVICE_ID, regmap);
5803b262f2SGregor Boirie }
5903b262f2SGregor Boirie 
6003b262f2SGregor Boirie static int zpa2326_remove_spi(struct spi_device *spi)
6103b262f2SGregor Boirie {
6203b262f2SGregor Boirie 	zpa2326_remove(&spi->dev);
6303b262f2SGregor Boirie 
6403b262f2SGregor Boirie 	return 0;
6503b262f2SGregor Boirie }
6603b262f2SGregor Boirie 
6703b262f2SGregor Boirie static const struct spi_device_id zpa2326_spi_ids[] = {
6803b262f2SGregor Boirie 	{ "zpa2326", 0 },
6903b262f2SGregor Boirie 	{ },
7003b262f2SGregor Boirie };
7103b262f2SGregor Boirie MODULE_DEVICE_TABLE(spi, zpa2326_spi_ids);
7203b262f2SGregor Boirie 
7303b262f2SGregor Boirie static const struct of_device_id zpa2326_spi_matches[] = {
7403b262f2SGregor Boirie 	{ .compatible = "murata,zpa2326" },
7503b262f2SGregor Boirie 	{ }
7603b262f2SGregor Boirie };
7703b262f2SGregor Boirie MODULE_DEVICE_TABLE(of, zpa2326_spi_matches);
7803b262f2SGregor Boirie 
7903b262f2SGregor Boirie static struct spi_driver zpa2326_spi_driver = {
8003b262f2SGregor Boirie 	.driver = {
8103b262f2SGregor Boirie 		.name           = "zpa2326-spi",
82*a409d2b6SJonathan Cameron 		.of_match_table = zpa2326_spi_matches,
8303b262f2SGregor Boirie 		.pm             = ZPA2326_PM_OPS,
8403b262f2SGregor Boirie 	},
8503b262f2SGregor Boirie 	.probe    = zpa2326_probe_spi,
8603b262f2SGregor Boirie 	.remove   = zpa2326_remove_spi,
8703b262f2SGregor Boirie 	.id_table = zpa2326_spi_ids,
8803b262f2SGregor Boirie };
8903b262f2SGregor Boirie module_spi_driver(zpa2326_spi_driver);
9003b262f2SGregor Boirie 
9103b262f2SGregor Boirie MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
9203b262f2SGregor Boirie MODULE_DESCRIPTION("SPI driver for Murata ZPA2326 pressure sensor");
9303b262f2SGregor Boirie MODULE_LICENSE("GPL v2");
94