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 
hmc5843_spi_probe(struct spi_device * spi)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 
hmc5843_spi_remove(struct spi_device * spi)77 static int hmc5843_spi_remove(struct spi_device *spi)
78 {
79 	hmc5843_common_remove(&spi->dev);
80 
81 	return 0;
82 }
83 
84 static const struct spi_device_id hmc5843_id[] = {
85 	{ "hmc5983", HMC5983_ID },
86 	{ }
87 };
88 MODULE_DEVICE_TABLE(spi, hmc5843_id);
89 
90 static struct spi_driver hmc5843_driver = {
91 		.driver = {
92 				.name = "hmc5843",
93 				.pm = HMC5843_PM_OPS,
94 		},
95 		.id_table = hmc5843_id,
96 		.probe = hmc5843_spi_probe,
97 		.remove = hmc5843_spi_remove,
98 };
99 
100 module_spi_driver(hmc5843_driver);
101 
102 MODULE_AUTHOR("Josef Gajdusek <atx@atx.name>");
103 MODULE_DESCRIPTION("HMC5983 SPI driver");
104 MODULE_LICENSE("GPL");
105