18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * SPI driver for hmc5983
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) Josef Gajdusek <atx@atx.name>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
108c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "hmc5843.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic const struct regmap_range hmc5843_readable_ranges[] = {
158c2ecf20Sopenharmony_ci		regmap_reg_range(0, HMC5843_ID_END),
168c2ecf20Sopenharmony_ci};
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic const struct regmap_access_table hmc5843_readable_table = {
198c2ecf20Sopenharmony_ci		.yes_ranges = hmc5843_readable_ranges,
208c2ecf20Sopenharmony_ci		.n_yes_ranges = ARRAY_SIZE(hmc5843_readable_ranges),
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic const struct regmap_range hmc5843_writable_ranges[] = {
248c2ecf20Sopenharmony_ci		regmap_reg_range(0, HMC5843_MODE_REG),
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic const struct regmap_access_table hmc5843_writable_table = {
288c2ecf20Sopenharmony_ci		.yes_ranges = hmc5843_writable_ranges,
298c2ecf20Sopenharmony_ci		.n_yes_ranges = ARRAY_SIZE(hmc5843_writable_ranges),
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic const struct regmap_range hmc5843_volatile_ranges[] = {
338c2ecf20Sopenharmony_ci		regmap_reg_range(HMC5843_DATA_OUT_MSB_REGS, HMC5843_STATUS_REG),
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic const struct regmap_access_table hmc5843_volatile_table = {
378c2ecf20Sopenharmony_ci		.yes_ranges = hmc5843_volatile_ranges,
388c2ecf20Sopenharmony_ci		.n_yes_ranges = ARRAY_SIZE(hmc5843_volatile_ranges),
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic const struct regmap_config hmc5843_spi_regmap_config = {
428c2ecf20Sopenharmony_ci		.reg_bits = 8,
438c2ecf20Sopenharmony_ci		.val_bits = 8,
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci		.rd_table = &hmc5843_readable_table,
468c2ecf20Sopenharmony_ci		.wr_table = &hmc5843_writable_table,
478c2ecf20Sopenharmony_ci		.volatile_table = &hmc5843_volatile_table,
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci		/* Autoincrement address pointer */
508c2ecf20Sopenharmony_ci		.read_flag_mask = 0xc0,
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci		.cache_type = REGCACHE_RBTREE,
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic int hmc5843_spi_probe(struct spi_device *spi)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	int ret;
588c2ecf20Sopenharmony_ci	struct regmap *regmap;
598c2ecf20Sopenharmony_ci	const struct spi_device_id *id = spi_get_device_id(spi);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	spi->mode = SPI_MODE_3;
628c2ecf20Sopenharmony_ci	spi->max_speed_hz = 8000000;
638c2ecf20Sopenharmony_ci	spi->bits_per_word = 8;
648c2ecf20Sopenharmony_ci	ret = spi_setup(spi);
658c2ecf20Sopenharmony_ci	if (ret)
668c2ecf20Sopenharmony_ci		return ret;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_spi(spi, &hmc5843_spi_regmap_config);
698c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
708c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	return hmc5843_common_probe(&spi->dev,
738c2ecf20Sopenharmony_ci			regmap,
748c2ecf20Sopenharmony_ci			id->driver_data, id->name);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int hmc5843_spi_remove(struct spi_device *spi)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	return hmc5843_common_remove(&spi->dev);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic const struct spi_device_id hmc5843_id[] = {
838c2ecf20Sopenharmony_ci	{ "hmc5983", HMC5983_ID },
848c2ecf20Sopenharmony_ci	{ }
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, hmc5843_id);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic struct spi_driver hmc5843_driver = {
898c2ecf20Sopenharmony_ci		.driver = {
908c2ecf20Sopenharmony_ci				.name = "hmc5843",
918c2ecf20Sopenharmony_ci				.pm = HMC5843_PM_OPS,
928c2ecf20Sopenharmony_ci		},
938c2ecf20Sopenharmony_ci		.id_table = hmc5843_id,
948c2ecf20Sopenharmony_ci		.probe = hmc5843_spi_probe,
958c2ecf20Sopenharmony_ci		.remove = hmc5843_spi_remove,
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cimodule_spi_driver(hmc5843_driver);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ciMODULE_AUTHOR("Josef Gajdusek <atx@atx.name>");
1018c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HMC5983 SPI driver");
1028c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
103