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