162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Murata ZPA2326 SPI pressure and temperature sensor driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2016 Parrot S.A.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Author: Gregor Boirie <gregor.boirie@parrot.com>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include <linux/regmap.h>
1262306a36Sopenharmony_ci#include <linux/spi/spi.h>
1362306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
1462306a36Sopenharmony_ci#include "zpa2326.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * read_flag_mask:
1862306a36Sopenharmony_ci *   - address bit 7 must be set to request a register read operation
1962306a36Sopenharmony_ci *   - address bit 6 must be set to request register address auto increment
2062306a36Sopenharmony_ci */
2162306a36Sopenharmony_cistatic const struct regmap_config zpa2326_regmap_spi_config = {
2262306a36Sopenharmony_ci	.reg_bits       = 8,
2362306a36Sopenharmony_ci	.val_bits       = 8,
2462306a36Sopenharmony_ci	.writeable_reg  = zpa2326_isreg_writeable,
2562306a36Sopenharmony_ci	.readable_reg   = zpa2326_isreg_readable,
2662306a36Sopenharmony_ci	.precious_reg   = zpa2326_isreg_precious,
2762306a36Sopenharmony_ci	.max_register   = ZPA2326_TEMP_OUT_H_REG,
2862306a36Sopenharmony_ci	.read_flag_mask = BIT(7) | BIT(6),
2962306a36Sopenharmony_ci	.cache_type     = REGCACHE_NONE,
3062306a36Sopenharmony_ci};
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistatic int zpa2326_probe_spi(struct spi_device *spi)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	struct regmap *regmap;
3562306a36Sopenharmony_ci	int            err;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	regmap = devm_regmap_init_spi(spi, &zpa2326_regmap_spi_config);
3862306a36Sopenharmony_ci	if (IS_ERR(regmap)) {
3962306a36Sopenharmony_ci		dev_err(&spi->dev, "failed to init registers map");
4062306a36Sopenharmony_ci		return PTR_ERR(regmap);
4162306a36Sopenharmony_ci	}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	/*
4462306a36Sopenharmony_ci	 * Enforce SPI slave settings to prevent from DT misconfiguration.
4562306a36Sopenharmony_ci	 *
4662306a36Sopenharmony_ci	 * Clock is idle high. Sampling happens on trailing edge, i.e., rising
4762306a36Sopenharmony_ci	 * edge. Maximum bus frequency is 1 MHz. Registers are 8 bits wide.
4862306a36Sopenharmony_ci	 */
4962306a36Sopenharmony_ci	spi->mode = SPI_MODE_3;
5062306a36Sopenharmony_ci	spi->max_speed_hz = min(spi->max_speed_hz, 1000000U);
5162306a36Sopenharmony_ci	spi->bits_per_word = 8;
5262306a36Sopenharmony_ci	err = spi_setup(spi);
5362306a36Sopenharmony_ci	if (err < 0)
5462306a36Sopenharmony_ci		return err;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	return zpa2326_probe(&spi->dev, spi_get_device_id(spi)->name,
5762306a36Sopenharmony_ci			     spi->irq, ZPA2326_DEVICE_ID, regmap);
5862306a36Sopenharmony_ci}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic void zpa2326_remove_spi(struct spi_device *spi)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	zpa2326_remove(&spi->dev);
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cistatic const struct spi_device_id zpa2326_spi_ids[] = {
6662306a36Sopenharmony_ci	{ "zpa2326", 0 },
6762306a36Sopenharmony_ci	{ },
6862306a36Sopenharmony_ci};
6962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(spi, zpa2326_spi_ids);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic const struct of_device_id zpa2326_spi_matches[] = {
7262306a36Sopenharmony_ci	{ .compatible = "murata,zpa2326" },
7362306a36Sopenharmony_ci	{ }
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, zpa2326_spi_matches);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_cistatic struct spi_driver zpa2326_spi_driver = {
7862306a36Sopenharmony_ci	.driver = {
7962306a36Sopenharmony_ci		.name           = "zpa2326-spi",
8062306a36Sopenharmony_ci		.of_match_table = zpa2326_spi_matches,
8162306a36Sopenharmony_ci		.pm             = ZPA2326_PM_OPS,
8262306a36Sopenharmony_ci	},
8362306a36Sopenharmony_ci	.probe    = zpa2326_probe_spi,
8462306a36Sopenharmony_ci	.remove   = zpa2326_remove_spi,
8562306a36Sopenharmony_ci	.id_table = zpa2326_spi_ids,
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_cimodule_spi_driver(zpa2326_spi_driver);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ciMODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
9062306a36Sopenharmony_ciMODULE_DESCRIPTION("SPI driver for Murata ZPA2326 pressure sensor");
9162306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
9262306a36Sopenharmony_ciMODULE_IMPORT_NS(IIO_ZPA2326);
93