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 static const struct iio_event_spec ad7091r5_events[] = {
16 	{
17 		.type = IIO_EV_TYPE_THRESH,
18 		.dir = IIO_EV_DIR_RISING,
19 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
20 				 BIT(IIO_EV_INFO_ENABLE),
21 	},
22 	{
23 		.type = IIO_EV_TYPE_THRESH,
24 		.dir = IIO_EV_DIR_FALLING,
25 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
26 				 BIT(IIO_EV_INFO_ENABLE),
27 	},
28 	{
29 		.type = IIO_EV_TYPE_THRESH,
30 		.dir = IIO_EV_DIR_EITHER,
31 		.mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
32 	},
33 };
34 
35 #define AD7091R_CHANNEL(idx, bits, ev, num_ev) { \
36 	.type = IIO_VOLTAGE, \
37 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
38 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
39 	.indexed = 1, \
40 	.channel = idx, \
41 	.event_spec = ev, \
42 	.num_event_specs = num_ev, \
43 	.scan_type.storagebits = 16, \
44 	.scan_type.realbits = bits, \
45 }
46 static const struct iio_chan_spec ad7091r5_channels_irq[] = {
47 	AD7091R_CHANNEL(0, 12, ad7091r5_events, ARRAY_SIZE(ad7091r5_events)),
48 	AD7091R_CHANNEL(1, 12, ad7091r5_events, ARRAY_SIZE(ad7091r5_events)),
49 	AD7091R_CHANNEL(2, 12, ad7091r5_events, ARRAY_SIZE(ad7091r5_events)),
50 	AD7091R_CHANNEL(3, 12, ad7091r5_events, ARRAY_SIZE(ad7091r5_events)),
51 };
52 
53 static const struct iio_chan_spec ad7091r5_channels_noirq[] = {
54 	AD7091R_CHANNEL(0, 12, NULL, 0),
55 	AD7091R_CHANNEL(1, 12, NULL, 0),
56 	AD7091R_CHANNEL(2, 12, NULL, 0),
57 	AD7091R_CHANNEL(3, 12, NULL, 0),
58 };
59 
60 static const struct ad7091r_chip_info ad7091r5_chip_info_irq = {
61 	.channels = ad7091r5_channels_irq,
62 	.num_channels = ARRAY_SIZE(ad7091r5_channels_irq),
63 	.vref_mV = 2500,
64 };
65 
66 static const struct ad7091r_chip_info ad7091r5_chip_info_noirq = {
67 	.channels = ad7091r5_channels_noirq,
68 	.num_channels = ARRAY_SIZE(ad7091r5_channels_noirq),
69 	.vref_mV = 2500,
70 };
71 
ad7091r5_i2c_probe(struct i2c_client * i2c,const struct i2c_device_id * id)72 static int ad7091r5_i2c_probe(struct i2c_client *i2c,
73 		const struct i2c_device_id *id)
74 {
75 	const struct ad7091r_chip_info *chip_info;
76 	struct regmap *map = devm_regmap_init_i2c(i2c, &ad7091r_regmap_config);
77 
78 	if (IS_ERR(map))
79 		return PTR_ERR(map);
80 
81 	if (i2c->irq)
82 		chip_info = &ad7091r5_chip_info_irq;
83 	else
84 		chip_info = &ad7091r5_chip_info_noirq;
85 
86 	return ad7091r_probe(&i2c->dev, id->name, chip_info, map, i2c->irq);
87 }
88 
89 static const struct of_device_id ad7091r5_dt_ids[] = {
90 	{ .compatible = "adi,ad7091r5" },
91 	{},
92 };
93 MODULE_DEVICE_TABLE(of, ad7091r5_dt_ids);
94 
95 static const struct i2c_device_id ad7091r5_i2c_ids[] = {
96 	{"ad7091r5", 0},
97 	{}
98 };
99 MODULE_DEVICE_TABLE(i2c, ad7091r5_i2c_ids);
100 
101 static struct i2c_driver ad7091r5_driver = {
102 	.driver = {
103 		.name = "ad7091r5",
104 		.of_match_table = ad7091r5_dt_ids,
105 	},
106 	.probe = ad7091r5_i2c_probe,
107 	.id_table = ad7091r5_i2c_ids,
108 };
109 module_i2c_driver(ad7091r5_driver);
110 
111 MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
112 MODULE_DESCRIPTION("Analog Devices AD7091R5 multi-channel ADC driver");
113 MODULE_LICENSE("GPL v2");
114