xref: /linux/drivers/iio/adc/gehc-pmc-adc.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * The GE HealthCare PMC ADC is a 16-Channel (Voltage and current), 16-Bit
4  * ADC with an I2C Interface.
5  *
6  * Copyright (C) 2024, GE HealthCare
7  *
8  * Authors:
9  * Herve Codina <herve.codina@bootlin.com>
10  */
11 #include <dt-bindings/iio/adc/gehc,pmc-adc.h>
12 #include <linux/bitops.h>
13 #include <linux/clk.h>
14 #include <linux/i2c.h>
15 #include <linux/iio/iio.h>
16 #include <linux/module.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/slab.h>
19 
20 struct pmc_adc {
21 	struct i2c_client *client;
22 };
23 
24 #define PMC_ADC_CMD_REQUEST_PROTOCOL_VERSION	0x01
25 #define PMC_ADC_CMD_READ_VOLTAGE(_ch)		(0x10 | (_ch))
26 #define PMC_ADC_CMD_READ_CURRENT(_ch)		(0x20 | (_ch))
27 
28 #define PMC_ADC_VOLTAGE_CHANNEL(_ch, _ds_name) {			\
29 	.type = IIO_VOLTAGE,						\
30 	.indexed = 1,							\
31 	.channel = (_ch),						\
32 	.address = PMC_ADC_CMD_READ_VOLTAGE(_ch),			\
33 	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),		\
34 	.datasheet_name = (_ds_name),					\
35 }
36 
37 #define PMC_ADC_CURRENT_CHANNEL(_ch, _ds_name) {			\
38 	.type = IIO_CURRENT,						\
39 	.indexed = 1,							\
40 	.channel = (_ch),						\
41 	.address = PMC_ADC_CMD_READ_CURRENT(_ch),			\
42 	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),		\
43 	.datasheet_name = (_ds_name),					\
44 }
45 
46 static const struct iio_chan_spec pmc_adc_channels[] = {
47 	PMC_ADC_VOLTAGE_CHANNEL(0, "CH0_V"),
48 	PMC_ADC_VOLTAGE_CHANNEL(1, "CH1_V"),
49 	PMC_ADC_VOLTAGE_CHANNEL(2, "CH2_V"),
50 	PMC_ADC_VOLTAGE_CHANNEL(3, "CH3_V"),
51 	PMC_ADC_VOLTAGE_CHANNEL(4, "CH4_V"),
52 	PMC_ADC_VOLTAGE_CHANNEL(5, "CH5_V"),
53 	PMC_ADC_VOLTAGE_CHANNEL(6, "CH6_V"),
54 	PMC_ADC_VOLTAGE_CHANNEL(7, "CH7_V"),
55 	PMC_ADC_VOLTAGE_CHANNEL(8, "CH8_V"),
56 	PMC_ADC_VOLTAGE_CHANNEL(9, "CH9_V"),
57 	PMC_ADC_VOLTAGE_CHANNEL(10, "CH10_V"),
58 	PMC_ADC_VOLTAGE_CHANNEL(11, "CH11_V"),
59 	PMC_ADC_VOLTAGE_CHANNEL(12, "CH12_V"),
60 	PMC_ADC_VOLTAGE_CHANNEL(13, "CH13_V"),
61 	PMC_ADC_VOLTAGE_CHANNEL(14, "CH14_V"),
62 	PMC_ADC_VOLTAGE_CHANNEL(15, "CH15_V"),
63 
64 	PMC_ADC_CURRENT_CHANNEL(0, "CH0_I"),
65 	PMC_ADC_CURRENT_CHANNEL(1, "CH1_I"),
66 	PMC_ADC_CURRENT_CHANNEL(2, "CH2_I"),
67 	PMC_ADC_CURRENT_CHANNEL(3, "CH3_I"),
68 	PMC_ADC_CURRENT_CHANNEL(4, "CH4_I"),
69 	PMC_ADC_CURRENT_CHANNEL(5, "CH5_I"),
70 	PMC_ADC_CURRENT_CHANNEL(6, "CH6_I"),
71 	PMC_ADC_CURRENT_CHANNEL(7, "CH7_I"),
72 	PMC_ADC_CURRENT_CHANNEL(8, "CH8_I"),
73 	PMC_ADC_CURRENT_CHANNEL(9, "CH9_I"),
74 	PMC_ADC_CURRENT_CHANNEL(10, "CH10_I"),
75 	PMC_ADC_CURRENT_CHANNEL(11, "CH11_I"),
76 	PMC_ADC_CURRENT_CHANNEL(12, "CH12_I"),
77 	PMC_ADC_CURRENT_CHANNEL(13, "CH13_I"),
78 	PMC_ADC_CURRENT_CHANNEL(14, "CH14_I"),
79 	PMC_ADC_CURRENT_CHANNEL(15, "CH15_I"),
80 };
81 
82 static int pmc_adc_read_raw_ch(struct pmc_adc *pmc_adc, u8 cmd, int *val)
83 {
84 	s32 ret;
85 
86 	ret = i2c_smbus_read_word_swapped(pmc_adc->client, cmd);
87 	if (ret < 0) {
88 		dev_err(&pmc_adc->client->dev, "i2c read word failed (%d)\n", ret);
89 		return ret;
90 	}
91 
92 	*val = sign_extend32(ret, 15);
93 	return 0;
94 }
95 
96 static int pmc_adc_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
97 			    int *val, int *val2, long mask)
98 {
99 	struct pmc_adc *pmc_adc = iio_priv(indio_dev);
100 	int ret;
101 
102 	switch (mask) {
103 	case IIO_CHAN_INFO_PROCESSED:
104 		/* Values are directly read in mV or mA */
105 		ret = pmc_adc_read_raw_ch(pmc_adc, chan->address, val);
106 		if (ret)
107 			return ret;
108 		return IIO_VAL_INT;
109 	}
110 
111 	return -EINVAL;
112 }
113 
114 static int pmc_adc_fwnode_xlate(struct iio_dev *indio_dev,
115 				const struct fwnode_reference_args *iiospec)
116 {
117 	enum iio_chan_type expected_type;
118 	unsigned int i;
119 
120 	/*
121 	 * args[0]: Acquisition type (i.e. voltage or current)
122 	 * args[1]: PMC ADC channel number
123 	 */
124 	if (iiospec->nargs != 2)
125 		return -EINVAL;
126 
127 	switch (iiospec->args[0]) {
128 	case GEHC_PMC_ADC_VOLTAGE:
129 		expected_type = IIO_VOLTAGE;
130 		break;
131 	case GEHC_PMC_ADC_CURRENT:
132 		expected_type = IIO_CURRENT;
133 		break;
134 	default:
135 		dev_err(&indio_dev->dev, "Invalid channel type %llu\n",
136 			iiospec->args[0]);
137 		return -EINVAL;
138 	}
139 
140 	for (i = 0; i < indio_dev->num_channels; i++)
141 		if (indio_dev->channels[i].type == expected_type &&
142 		    indio_dev->channels[i].channel == iiospec->args[1])
143 			return i;
144 
145 	dev_err(&indio_dev->dev, "Invalid channel type %llu number %llu\n",
146 		iiospec->args[0], iiospec->args[1]);
147 	return -EINVAL;
148 }
149 
150 static const struct iio_info pmc_adc_info = {
151 	.read_raw = pmc_adc_read_raw,
152 	.fwnode_xlate = pmc_adc_fwnode_xlate,
153 };
154 
155 static const char *const pmc_adc_regulator_names[] = {
156 	"vdd",
157 	"vdda",
158 	"vddio",
159 	"vref",
160 };
161 
162 static int pmc_adc_probe(struct i2c_client *client)
163 {
164 	struct iio_dev *indio_dev;
165 	struct pmc_adc *pmc_adc;
166 	struct clk *clk;
167 	s32 val;
168 	int ret;
169 
170 	ret = devm_regulator_bulk_get_enable(&client->dev, ARRAY_SIZE(pmc_adc_regulator_names),
171 					     pmc_adc_regulator_names);
172 	if (ret)
173 		return dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
174 
175 	clk = devm_clk_get_optional_enabled(&client->dev, "osc");
176 	if (IS_ERR(clk))
177 		return dev_err_probe(&client->dev, PTR_ERR(clk), "Failed to get osc clock\n");
178 
179 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*pmc_adc));
180 	if (!indio_dev)
181 		return -ENOMEM;
182 
183 	pmc_adc = iio_priv(indio_dev);
184 	pmc_adc->client = client;
185 
186 	val = i2c_smbus_read_byte_data(pmc_adc->client, PMC_ADC_CMD_REQUEST_PROTOCOL_VERSION);
187 	if (val < 0)
188 		return dev_err_probe(&client->dev, val, "Failed to get protocol version\n");
189 
190 	if (val != 0x01)
191 		return dev_err_probe(&client->dev, -EINVAL,
192 				     "Unsupported protocol version 0x%02x\n", val);
193 
194 	indio_dev->name = "pmc_adc";
195 	indio_dev->info = &pmc_adc_info;
196 	indio_dev->modes = INDIO_DIRECT_MODE;
197 	indio_dev->channels = pmc_adc_channels;
198 	indio_dev->num_channels = ARRAY_SIZE(pmc_adc_channels);
199 
200 	return devm_iio_device_register(&client->dev, indio_dev);
201 }
202 
203 static const struct of_device_id pmc_adc_of_match[] = {
204 	{ .compatible = "gehc,pmc-adc"},
205 	{ }
206 };
207 MODULE_DEVICE_TABLE(of, pmc_adc_of_match);
208 
209 static const struct i2c_device_id pmc_adc_id_table[] = {
210 	{ "pmc-adc" },
211 	{ }
212 };
213 MODULE_DEVICE_TABLE(i2c, pmc_adc_id_table);
214 
215 static struct i2c_driver pmc_adc_i2c_driver = {
216 	.driver  = {
217 		.name = "pmc-adc",
218 		.of_match_table = pmc_adc_of_match,
219 	},
220 	.id_table = pmc_adc_id_table,
221 	.probe  = pmc_adc_probe,
222 };
223 
224 module_i2c_driver(pmc_adc_i2c_driver);
225 
226 MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
227 MODULE_DESCRIPTION("GE HealthCare PMC ADC driver");
228 MODULE_LICENSE("GPL");
229