xref: /linux/drivers/iio/adc/ad7091r5.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
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_chan_spec ad7091r5_channels_irq[] = {
16 	AD7091R_CHANNEL(0, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
17 	AD7091R_CHANNEL(1, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
18 	AD7091R_CHANNEL(2, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
19 	AD7091R_CHANNEL(3, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
20 };
21 
22 static const struct iio_chan_spec ad7091r5_channels_noirq[] = {
23 	AD7091R_CHANNEL(0, 12, NULL, 0),
24 	AD7091R_CHANNEL(1, 12, NULL, 0),
25 	AD7091R_CHANNEL(2, 12, NULL, 0),
26 	AD7091R_CHANNEL(3, 12, NULL, 0),
27 };
28 
29 static int ad7091r5_set_mode(struct ad7091r_state *st, enum ad7091r_mode mode)
30 {
31 	int ret, conf;
32 
33 	switch (mode) {
34 	case AD7091R_MODE_SAMPLE:
35 		conf = 0;
36 		break;
37 	case AD7091R_MODE_COMMAND:
38 		conf = AD7091R_REG_CONF_CMD;
39 		break;
40 	case AD7091R_MODE_AUTOCYCLE:
41 		conf = AD7091R_REG_CONF_AUTO;
42 		break;
43 	default:
44 		return -EINVAL;
45 	}
46 
47 	ret = regmap_update_bits(st->map, AD7091R_REG_CONF,
48 				 AD7091R_REG_CONF_MODE_MASK, conf);
49 	if (ret)
50 		return ret;
51 
52 	st->mode = mode;
53 
54 	return 0;
55 }
56 
57 static unsigned int ad7091r5_reg_result_chan_id(unsigned int val)
58 {
59 	return AD7091R5_REG_RESULT_CH_ID(val);
60 }
61 
62 static const struct ad7091r_chip_info ad7091r5_chip_info_irq = {
63 	.name = "ad7091r-5",
64 	.channels = ad7091r5_channels_irq,
65 	.num_channels = ARRAY_SIZE(ad7091r5_channels_irq),
66 	.vref_mV = 2500,
67 	.reg_result_chan_id = &ad7091r5_reg_result_chan_id,
68 	.set_mode = &ad7091r5_set_mode,
69 };
70 
71 static const struct ad7091r_chip_info ad7091r5_chip_info_noirq = {
72 	.name = "ad7091r-5",
73 	.channels = ad7091r5_channels_noirq,
74 	.num_channels = ARRAY_SIZE(ad7091r5_channels_noirq),
75 	.vref_mV = 2500,
76 	.reg_result_chan_id = &ad7091r5_reg_result_chan_id,
77 	.set_mode = &ad7091r5_set_mode,
78 };
79 
80 static const struct regmap_config ad7091r_regmap_config = {
81 	.reg_bits = 8,
82 	.val_bits = 16,
83 	.writeable_reg = ad7091r_writeable_reg,
84 	.volatile_reg = ad7091r_volatile_reg,
85 };
86 
87 static void ad7091r5_regmap_init(struct ad7091r_state *st,
88 				 const struct regmap_config *regmap_conf)
89 {
90 	struct i2c_client *i2c = container_of(st->dev, struct i2c_client, dev);
91 
92 	st->map = devm_regmap_init_i2c(i2c, regmap_conf);
93 }
94 
95 static struct ad7091r_init_info ad7091r5_init_info = {
96 	.info_irq = &ad7091r5_chip_info_irq,
97 	.info_no_irq = &ad7091r5_chip_info_noirq,
98 	.regmap_config = &ad7091r_regmap_config,
99 	.init_adc_regmap = &ad7091r5_regmap_init
100 };
101 
102 static int ad7091r5_i2c_probe(struct i2c_client *i2c)
103 {
104 	const struct ad7091r_init_info *init_info;
105 
106 	init_info = i2c_get_match_data(i2c);
107 	if (!init_info)
108 		return -EINVAL;
109 
110 	return ad7091r_probe(&i2c->dev, init_info, i2c->irq);
111 }
112 
113 static const struct of_device_id ad7091r5_dt_ids[] = {
114 	{ .compatible = "adi,ad7091r5", .data = &ad7091r5_init_info },
115 	{},
116 };
117 MODULE_DEVICE_TABLE(of, ad7091r5_dt_ids);
118 
119 static const struct i2c_device_id ad7091r5_i2c_ids[] = {
120 	{"ad7091r5", (kernel_ulong_t)&ad7091r5_init_info },
121 	{}
122 };
123 MODULE_DEVICE_TABLE(i2c, ad7091r5_i2c_ids);
124 
125 static struct i2c_driver ad7091r5_driver = {
126 	.driver = {
127 		.name = "ad7091r5",
128 		.of_match_table = ad7091r5_dt_ids,
129 	},
130 	.probe = ad7091r5_i2c_probe,
131 	.id_table = ad7091r5_i2c_ids,
132 };
133 module_i2c_driver(ad7091r5_driver);
134 
135 MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
136 MODULE_DESCRIPTION("Analog Devices AD7091R5 multi-channel ADC driver");
137 MODULE_LICENSE("GPL v2");
138 MODULE_IMPORT_NS(IIO_AD7091R);
139