drm/client: Fix drm client endless Kconfig loop
[drm/drm-misc.git] / drivers / iio / adc / gehc-pmc-adc.c
blobd1167818b17d65987774cd5545cab6b5492de04e
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.
6 * Copyright (C) 2024, GE HealthCare
8 * Authors:
9 * Herve Codina <herve.codina@bootlin.com>
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>
20 struct pmc_adc {
21 struct i2c_client *client;
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))
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), \
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), \
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"),
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"),
82 static int pmc_adc_read_raw_ch(struct pmc_adc *pmc_adc, u8 cmd, int *val)
84 s32 ret;
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;
92 *val = sign_extend32(ret, 15);
93 return 0;
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)
99 struct pmc_adc *pmc_adc = iio_priv(indio_dev);
100 int ret;
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;
111 return -EINVAL;
114 static int pmc_adc_fwnode_xlate(struct iio_dev *indio_dev,
115 const struct fwnode_reference_args *iiospec)
117 enum iio_chan_type expected_type;
118 unsigned int i;
121 * args[0]: Acquisition type (i.e. voltage or current)
122 * args[1]: PMC ADC channel number
124 if (iiospec->nargs != 2)
125 return -EINVAL;
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;
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;
145 dev_err(&indio_dev->dev, "Invalid channel type %llu number %llu\n",
146 iiospec->args[0], iiospec->args[1]);
147 return -EINVAL;
150 static const struct iio_info pmc_adc_info = {
151 .read_raw = pmc_adc_read_raw,
152 .fwnode_xlate = pmc_adc_fwnode_xlate,
155 static const char *const pmc_adc_regulator_names[] = {
156 "vdd",
157 "vdda",
158 "vddio",
159 "vref",
162 static int pmc_adc_probe(struct i2c_client *client)
164 struct iio_dev *indio_dev;
165 struct pmc_adc *pmc_adc;
166 struct clk *clk;
167 s32 val;
168 int ret;
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");
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");
179 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*pmc_adc));
180 if (!indio_dev)
181 return -ENOMEM;
183 pmc_adc = iio_priv(indio_dev);
184 pmc_adc->client = client;
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");
190 if (val != 0x01)
191 return dev_err_probe(&client->dev, -EINVAL,
192 "Unsupported protocol version 0x%02x\n", val);
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);
200 return devm_iio_device_register(&client->dev, indio_dev);
203 static const struct of_device_id pmc_adc_of_match[] = {
204 { .compatible = "gehc,pmc-adc"},
207 MODULE_DEVICE_TABLE(of, pmc_adc_of_match);
209 static const struct i2c_device_id pmc_adc_id_table[] = {
210 { "pmc-adc" },
213 MODULE_DEVICE_TABLE(i2c, pmc_adc_id_table);
215 static struct i2c_driver pmc_adc_i2c_driver = {
216 .driver = {
217 .name = "pmc-adc",
218 .of_match_table = pmc_adc_of_match,
220 .id_table = pmc_adc_id_table,
221 .probe = pmc_adc_probe,
224 module_i2c_driver(pmc_adc_i2c_driver);
226 MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
227 MODULE_DESCRIPTION("GE HealthCare PMC ADC driver");
228 MODULE_LICENSE("GPL");