18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Murata ZPA2326 SPI pressure and temperature sensor driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2016 Parrot S.A.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Gregor Boirie <gregor.boirie@parrot.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/regmap.h>
128c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
138c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
148c2ecf20Sopenharmony_ci#include "zpa2326.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * read_flag_mask:
188c2ecf20Sopenharmony_ci *   - address bit 7 must be set to request a register read operation
198c2ecf20Sopenharmony_ci *   - address bit 6 must be set to request register address auto increment
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_cistatic const struct regmap_config zpa2326_regmap_spi_config = {
228c2ecf20Sopenharmony_ci	.reg_bits       = 8,
238c2ecf20Sopenharmony_ci	.val_bits       = 8,
248c2ecf20Sopenharmony_ci	.writeable_reg  = zpa2326_isreg_writeable,
258c2ecf20Sopenharmony_ci	.readable_reg   = zpa2326_isreg_readable,
268c2ecf20Sopenharmony_ci	.precious_reg   = zpa2326_isreg_precious,
278c2ecf20Sopenharmony_ci	.max_register   = ZPA2326_TEMP_OUT_H_REG,
288c2ecf20Sopenharmony_ci	.read_flag_mask = BIT(7) | BIT(6),
298c2ecf20Sopenharmony_ci	.cache_type     = REGCACHE_NONE,
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int zpa2326_probe_spi(struct spi_device *spi)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct regmap *regmap;
358c2ecf20Sopenharmony_ci	int            err;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_spi(spi, &zpa2326_regmap_spi_config);
388c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
398c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "failed to init registers map");
408c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/*
448c2ecf20Sopenharmony_ci	 * Enforce SPI slave settings to prevent from DT misconfiguration.
458c2ecf20Sopenharmony_ci	 *
468c2ecf20Sopenharmony_ci	 * Clock is idle high. Sampling happens on trailing edge, i.e., rising
478c2ecf20Sopenharmony_ci	 * edge. Maximum bus frequency is 1 MHz. Registers are 8 bits wide.
488c2ecf20Sopenharmony_ci	 */
498c2ecf20Sopenharmony_ci	spi->mode = SPI_MODE_3;
508c2ecf20Sopenharmony_ci	spi->max_speed_hz = min(spi->max_speed_hz, 1000000U);
518c2ecf20Sopenharmony_ci	spi->bits_per_word = 8;
528c2ecf20Sopenharmony_ci	err = spi_setup(spi);
538c2ecf20Sopenharmony_ci	if (err < 0)
548c2ecf20Sopenharmony_ci		return err;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	return zpa2326_probe(&spi->dev, spi_get_device_id(spi)->name,
578c2ecf20Sopenharmony_ci			     spi->irq, ZPA2326_DEVICE_ID, regmap);
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic int zpa2326_remove_spi(struct spi_device *spi)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	zpa2326_remove(&spi->dev);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	return 0;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic const struct spi_device_id zpa2326_spi_ids[] = {
688c2ecf20Sopenharmony_ci	{ "zpa2326", 0 },
698c2ecf20Sopenharmony_ci	{ },
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, zpa2326_spi_ids);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic const struct of_device_id zpa2326_spi_matches[] = {
748c2ecf20Sopenharmony_ci	{ .compatible = "murata,zpa2326" },
758c2ecf20Sopenharmony_ci	{ }
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, zpa2326_spi_matches);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic struct spi_driver zpa2326_spi_driver = {
808c2ecf20Sopenharmony_ci	.driver = {
818c2ecf20Sopenharmony_ci		.name           = "zpa2326-spi",
828c2ecf20Sopenharmony_ci		.of_match_table = zpa2326_spi_matches,
838c2ecf20Sopenharmony_ci		.pm             = ZPA2326_PM_OPS,
848c2ecf20Sopenharmony_ci	},
858c2ecf20Sopenharmony_ci	.probe    = zpa2326_probe_spi,
868c2ecf20Sopenharmony_ci	.remove   = zpa2326_remove_spi,
878c2ecf20Sopenharmony_ci	.id_table = zpa2326_spi_ids,
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_cimodule_spi_driver(zpa2326_spi_driver);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ciMODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
928c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SPI driver for Murata ZPA2326 pressure sensor");
938c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
94