xref: /openbmc/linux/drivers/iio/adc/ad7091r5.c (revision 9429b12d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * AD7091R5 Analog to Digital converter driver
4  *
5  * Copyright 2014-2019 Analog Devices Inc.
6  */
7 
8 #include <linux/i2c.h>
9 #include <linux/iio/iio.h>
10 #include <linux/module.h>
11 #include <linux/regmap.h>
12 
13 #include "ad7091r-base.h"
14 
15 #define AD7091R_CHANNEL(idx, bits, ev, num_ev) { \
16 	.type = IIO_VOLTAGE, \
17 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
18 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
19 	.indexed = 1, \
20 	.channel = idx, \
21 	.event_spec = ev, \
22 	.num_event_specs = num_ev, \
23 	.scan_type.storagebits = 16, \
24 	.scan_type.realbits = bits, \
25 }
26 static const struct iio_chan_spec ad7091r5_channels_irq[] = {
27 	AD7091R_CHANNEL(0, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
28 	AD7091R_CHANNEL(1, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
29 	AD7091R_CHANNEL(2, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
30 	AD7091R_CHANNEL(3, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
31 };
32 
33 static const struct iio_chan_spec ad7091r5_channels_noirq[] = {
34 	AD7091R_CHANNEL(0, 12, NULL, 0),
35 	AD7091R_CHANNEL(1, 12, NULL, 0),
36 	AD7091R_CHANNEL(2, 12, NULL, 0),
37 	AD7091R_CHANNEL(3, 12, NULL, 0),
38 };
39 
40 static const struct ad7091r_chip_info ad7091r5_chip_info_irq = {
41 	.channels = ad7091r5_channels_irq,
42 	.num_channels = ARRAY_SIZE(ad7091r5_channels_irq),
43 	.vref_mV = 2500,
44 };
45 
46 static const struct ad7091r_chip_info ad7091r5_chip_info_noirq = {
47 	.channels = ad7091r5_channels_noirq,
48 	.num_channels = ARRAY_SIZE(ad7091r5_channels_noirq),
49 	.vref_mV = 2500,
50 };
51 
52 static int ad7091r5_i2c_probe(struct i2c_client *i2c)
53 {
54 	const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
55 	const struct ad7091r_chip_info *chip_info;
56 	struct regmap *map = devm_regmap_init_i2c(i2c, &ad7091r_regmap_config);
57 
58 	if (IS_ERR(map))
59 		return PTR_ERR(map);
60 
61 	if (i2c->irq)
62 		chip_info = &ad7091r5_chip_info_irq;
63 	else
64 		chip_info = &ad7091r5_chip_info_noirq;
65 
66 	return ad7091r_probe(&i2c->dev, id->name, chip_info, map, i2c->irq);
67 }
68 
69 static const struct of_device_id ad7091r5_dt_ids[] = {
70 	{ .compatible = "adi,ad7091r5" },
71 	{},
72 };
73 MODULE_DEVICE_TABLE(of, ad7091r5_dt_ids);
74 
75 static const struct i2c_device_id ad7091r5_i2c_ids[] = {
76 	{"ad7091r5", 0},
77 	{}
78 };
79 MODULE_DEVICE_TABLE(i2c, ad7091r5_i2c_ids);
80 
81 static struct i2c_driver ad7091r5_driver = {
82 	.driver = {
83 		.name = "ad7091r5",
84 		.of_match_table = ad7091r5_dt_ids,
85 	},
86 	.probe = ad7091r5_i2c_probe,
87 	.id_table = ad7091r5_i2c_ids,
88 };
89 module_i2c_driver(ad7091r5_driver);
90 
91 MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
92 MODULE_DESCRIPTION("Analog Devices AD7091R5 multi-channel ADC driver");
93 MODULE_LICENSE("GPL v2");
94 MODULE_IMPORT_NS(IIO_AD7091R);
95