1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SPI driver for hmc5983
4  *
5  * Copyright (C) Josef Gajdusek <atx@atx.name>
6  */
7 
8 #include <linux/module.h>
9 #include <linux/spi/spi.h>
10 #include <linux/iio/iio.h>
11 
12 #include "hmc5843.h"
13 
14 static const struct regmap_range hmc5843_readable_ranges[] = {
15 		regmap_reg_range(0, HMC5843_ID_END),
16 };
17 
18 static const struct regmap_access_table hmc5843_readable_table = {
19 		.yes_ranges = hmc5843_readable_ranges,
20 		.n_yes_ranges = ARRAY_SIZE(hmc5843_readable_ranges),
21 };
22 
23 static const struct regmap_range hmc5843_writable_ranges[] = {
24 		regmap_reg_range(0, HMC5843_MODE_REG),
25 };
26 
27 static const struct regmap_access_table hmc5843_writable_table = {
28 		.yes_ranges = hmc5843_writable_ranges,
29 		.n_yes_ranges = ARRAY_SIZE(hmc5843_writable_ranges),
30 };
31 
32 static const struct regmap_range hmc5843_volatile_ranges[] = {
33 		regmap_reg_range(HMC5843_DATA_OUT_MSB_REGS, HMC5843_STATUS_REG),
34 };
35 
36 static const struct regmap_access_table hmc5843_volatile_table = {
37 		.yes_ranges = hmc5843_volatile_ranges,
38 		.n_yes_ranges = ARRAY_SIZE(hmc5843_volatile_ranges),
39 };
40 
41 static const struct regmap_config hmc5843_spi_regmap_config = {
42 		.reg_bits = 8,
43 		.val_bits = 8,
44 
45 		.rd_table = &hmc5843_readable_table,
46 		.wr_table = &hmc5843_writable_table,
47 		.volatile_table = &hmc5843_volatile_table,
48 
49 		/* Autoincrement address pointer */
50 		.read_flag_mask = 0xc0,
51 
52 		.cache_type = REGCACHE_RBTREE,
53 };
54 
55 static int hmc5843_spi_probe(struct spi_device *spi)
56 {
57 	int ret;
58 	struct regmap *regmap;
59 	const struct spi_device_id *id = spi_get_device_id(spi);
60 
61 	spi->mode = SPI_MODE_3;
62 	spi->max_speed_hz = 8000000;
63 	spi->bits_per_word = 8;
64 	ret = spi_setup(spi);
65 	if (ret)
66 		return ret;
67 
68 	regmap = devm_regmap_init_spi(spi, &hmc5843_spi_regmap_config);
69 	if (IS_ERR(regmap))
70 		return PTR_ERR(regmap);
71 
72 	return hmc5843_common_probe(&spi->dev,
73 			regmap,
74 			id->driver_data, id->name);
75 }
76 
77 static int hmc5843_spi_remove(struct spi_device *spi)
78 {
79 	return hmc5843_common_remove(&spi->dev);
80 }
81 
82 static const struct spi_device_id hmc5843_id[] = {
83 	{ "hmc5983", HMC5983_ID },
84 	{ }
85 };
86 MODULE_DEVICE_TABLE(spi, hmc5843_id);
87 
88 static struct spi_driver hmc5843_driver = {
89 		.driver = {
90 				.name = "hmc5843",
91 				.pm = HMC5843_PM_OPS,
92 		},
93 		.id_table = hmc5843_id,
94 		.probe = hmc5843_spi_probe,
95 		.remove = hmc5843_spi_remove,
96 };
97 
98 module_spi_driver(hmc5843_driver);
99 
100 MODULE_AUTHOR("Josef Gajdusek <atx@atx.name>");
101 MODULE_DESCRIPTION("HMC5983 SPI driver");
102 MODULE_LICENSE("GPL");
103