18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * MS5611 pressure and temperature sensor driver (SPI bus)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
128c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "ms5611.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic int ms5611_spi_reset(struct ms5611_state *st)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	u8 cmd = MS5611_RESET;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	return spi_write_then_read(st->client, &cmd, 1, NULL, 0);
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic int ms5611_spi_read_prom_word(struct ms5611_state *st, int index,
268c2ecf20Sopenharmony_ci				     u16 *word)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	int ret;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	ret = spi_w8r16be(st->client, MS5611_READ_PROM_WORD + (index << 1));
318c2ecf20Sopenharmony_ci	if (ret < 0)
328c2ecf20Sopenharmony_ci		return ret;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	*word = ret;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	return 0;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic int ms5611_spi_read_adc(struct ms5611_state *st, s32 *val)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	int ret;
428c2ecf20Sopenharmony_ci	u8 buf[3] = { MS5611_READ_ADC };
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	ret = spi_write_then_read(st->client, buf, 1, buf, 3);
458c2ecf20Sopenharmony_ci	if (ret < 0)
468c2ecf20Sopenharmony_ci		return ret;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	*val = get_unaligned_be24(&buf[0]);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	return 0;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic int ms5611_spi_read_adc_temp_and_pressure(struct ms5611_state *st,
548c2ecf20Sopenharmony_ci						 s32 *temp, s32 *pressure)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	int ret;
578c2ecf20Sopenharmony_ci	const struct ms5611_osr *osr = st->temp_osr;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	/*
608c2ecf20Sopenharmony_ci	 * Warning: &osr->cmd MUST be aligned on a word boundary since used as
618c2ecf20Sopenharmony_ci	 * 2nd argument (void*) of spi_write_then_read.
628c2ecf20Sopenharmony_ci	 */
638c2ecf20Sopenharmony_ci	ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
648c2ecf20Sopenharmony_ci	if (ret < 0)
658c2ecf20Sopenharmony_ci		return ret;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
688c2ecf20Sopenharmony_ci	ret = ms5611_spi_read_adc(st, temp);
698c2ecf20Sopenharmony_ci	if (ret < 0)
708c2ecf20Sopenharmony_ci		return ret;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	osr = st->pressure_osr;
738c2ecf20Sopenharmony_ci	ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
748c2ecf20Sopenharmony_ci	if (ret < 0)
758c2ecf20Sopenharmony_ci		return ret;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
788c2ecf20Sopenharmony_ci	return ms5611_spi_read_adc(st, pressure);
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic int ms5611_spi_probe(struct spi_device *spi)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	int ret;
848c2ecf20Sopenharmony_ci	struct ms5611_state *st;
858c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
888c2ecf20Sopenharmony_ci	if (!indio_dev)
898c2ecf20Sopenharmony_ci		return -ENOMEM;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	spi_set_drvdata(spi, indio_dev);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	spi->mode = SPI_MODE_0;
948c2ecf20Sopenharmony_ci	spi->max_speed_hz = min(spi->max_speed_hz, 20000000U);
958c2ecf20Sopenharmony_ci	spi->bits_per_word = 8;
968c2ecf20Sopenharmony_ci	ret = spi_setup(spi);
978c2ecf20Sopenharmony_ci	if (ret < 0)
988c2ecf20Sopenharmony_ci		return ret;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	st = iio_priv(indio_dev);
1018c2ecf20Sopenharmony_ci	st->reset = ms5611_spi_reset;
1028c2ecf20Sopenharmony_ci	st->read_prom_word = ms5611_spi_read_prom_word;
1038c2ecf20Sopenharmony_ci	st->read_adc_temp_and_pressure = ms5611_spi_read_adc_temp_and_pressure;
1048c2ecf20Sopenharmony_ci	st->client = spi;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name,
1078c2ecf20Sopenharmony_ci			    spi_get_device_id(spi)->driver_data);
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic int ms5611_spi_remove(struct spi_device *spi)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	return ms5611_remove(spi_get_drvdata(spi));
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic const struct of_device_id ms5611_spi_matches[] = {
1168c2ecf20Sopenharmony_ci	{ .compatible = "meas,ms5611" },
1178c2ecf20Sopenharmony_ci	{ .compatible = "meas,ms5607" },
1188c2ecf20Sopenharmony_ci	{ }
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ms5611_spi_matches);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic const struct spi_device_id ms5611_id[] = {
1238c2ecf20Sopenharmony_ci	{ "ms5611", MS5611 },
1248c2ecf20Sopenharmony_ci	{ "ms5607", MS5607 },
1258c2ecf20Sopenharmony_ci	{ }
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, ms5611_id);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic struct spi_driver ms5611_driver = {
1308c2ecf20Sopenharmony_ci	.driver = {
1318c2ecf20Sopenharmony_ci		.name = "ms5611",
1328c2ecf20Sopenharmony_ci		.of_match_table = ms5611_spi_matches
1338c2ecf20Sopenharmony_ci	},
1348c2ecf20Sopenharmony_ci	.id_table = ms5611_id,
1358c2ecf20Sopenharmony_ci	.probe = ms5611_spi_probe,
1368c2ecf20Sopenharmony_ci	.remove = ms5611_spi_remove,
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_cimodule_spi_driver(ms5611_driver);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
1418c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MS5611 spi driver");
1428c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
143