xfrm: Fix NULL pointer dereference in xfrm_input when skb_dst_force clears the dst_entry.
[linux/fpc-iii.git] / drivers / iio / adc / ti-adc081c.c
blob405e3779c0c564f45dde2b701bd3393ce4f759e0
1 /*
2 * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver
4 * Copyright (C) 2012 Avionic Design GmbH
5 * Copyright (C) 2016 Intel
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Datasheets:
12 * http://www.ti.com/lit/ds/symlink/adc081c021.pdf
13 * http://www.ti.com/lit/ds/symlink/adc101c021.pdf
14 * http://www.ti.com/lit/ds/symlink/adc121c021.pdf
16 * The devices have a very similar interface and differ mostly in the number of
17 * bits handled. For the 8-bit and 10-bit models the least-significant 4 or 2
18 * bits of value registers are reserved.
21 #include <linux/err.h>
22 #include <linux/i2c.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/acpi.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
31 #include <linux/regulator/consumer.h>
33 struct adc081c {
34 struct i2c_client *i2c;
35 struct regulator *ref;
37 /* 8, 10 or 12 */
38 int bits;
41 #define REG_CONV_RES 0x00
43 static int adc081c_read_raw(struct iio_dev *iio,
44 struct iio_chan_spec const *channel, int *value,
45 int *shift, long mask)
47 struct adc081c *adc = iio_priv(iio);
48 int err;
50 switch (mask) {
51 case IIO_CHAN_INFO_RAW:
52 err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
53 if (err < 0)
54 return err;
56 *value = (err & 0xFFF) >> (12 - adc->bits);
57 return IIO_VAL_INT;
59 case IIO_CHAN_INFO_SCALE:
60 err = regulator_get_voltage(adc->ref);
61 if (err < 0)
62 return err;
64 *value = err / 1000;
65 *shift = adc->bits;
67 return IIO_VAL_FRACTIONAL_LOG2;
69 default:
70 break;
73 return -EINVAL;
76 #define ADCxx1C_CHAN(_bits) { \
77 .type = IIO_VOLTAGE, \
78 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
79 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
80 .scan_type = { \
81 .sign = 'u', \
82 .realbits = (_bits), \
83 .storagebits = 16, \
84 .shift = 12 - (_bits), \
85 .endianness = IIO_CPU, \
86 }, \
89 #define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \
90 static const struct iio_chan_spec _name ## _channels[] = { \
91 ADCxx1C_CHAN((_bits)), \
92 IIO_CHAN_SOFT_TIMESTAMP(1), \
93 }; \
95 #define ADC081C_NUM_CHANNELS 2
97 struct adcxx1c_model {
98 const struct iio_chan_spec* channels;
99 int bits;
102 #define ADCxx1C_MODEL(_name, _bits) \
104 .channels = _name ## _channels, \
105 .bits = (_bits), \
108 DEFINE_ADCxx1C_CHANNELS(adc081c, 8);
109 DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
110 DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
112 /* Model ids are indexes in _models array */
113 enum adcxx1c_model_id {
114 ADC081C = 0,
115 ADC101C = 1,
116 ADC121C = 2,
119 static struct adcxx1c_model adcxx1c_models[] = {
120 ADCxx1C_MODEL(adc081c, 8),
121 ADCxx1C_MODEL(adc101c, 10),
122 ADCxx1C_MODEL(adc121c, 12),
125 static const struct iio_info adc081c_info = {
126 .read_raw = adc081c_read_raw,
129 static irqreturn_t adc081c_trigger_handler(int irq, void *p)
131 struct iio_poll_func *pf = p;
132 struct iio_dev *indio_dev = pf->indio_dev;
133 struct adc081c *data = iio_priv(indio_dev);
134 u16 buf[8]; /* 2 bytes data + 6 bytes padding + 8 bytes timestamp */
135 int ret;
137 ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
138 if (ret < 0)
139 goto out;
140 buf[0] = ret;
141 iio_push_to_buffers_with_timestamp(indio_dev, buf,
142 iio_get_time_ns(indio_dev));
143 out:
144 iio_trigger_notify_done(indio_dev->trig);
145 return IRQ_HANDLED;
148 static int adc081c_probe(struct i2c_client *client,
149 const struct i2c_device_id *id)
151 struct iio_dev *iio;
152 struct adc081c *adc;
153 struct adcxx1c_model *model;
154 int err;
156 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
157 return -EOPNOTSUPP;
159 if (ACPI_COMPANION(&client->dev)) {
160 const struct acpi_device_id *ad_id;
162 ad_id = acpi_match_device(client->dev.driver->acpi_match_table,
163 &client->dev);
164 if (!ad_id)
165 return -ENODEV;
166 model = &adcxx1c_models[ad_id->driver_data];
167 } else {
168 model = &adcxx1c_models[id->driver_data];
171 iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
172 if (!iio)
173 return -ENOMEM;
175 adc = iio_priv(iio);
176 adc->i2c = client;
177 adc->bits = model->bits;
179 adc->ref = devm_regulator_get(&client->dev, "vref");
180 if (IS_ERR(adc->ref))
181 return PTR_ERR(adc->ref);
183 err = regulator_enable(adc->ref);
184 if (err < 0)
185 return err;
187 iio->dev.parent = &client->dev;
188 iio->dev.of_node = client->dev.of_node;
189 iio->name = dev_name(&client->dev);
190 iio->modes = INDIO_DIRECT_MODE;
191 iio->info = &adc081c_info;
193 iio->channels = model->channels;
194 iio->num_channels = ADC081C_NUM_CHANNELS;
196 err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL);
197 if (err < 0) {
198 dev_err(&client->dev, "iio triggered buffer setup failed\n");
199 goto err_regulator_disable;
202 err = iio_device_register(iio);
203 if (err < 0)
204 goto err_buffer_cleanup;
206 i2c_set_clientdata(client, iio);
208 return 0;
210 err_buffer_cleanup:
211 iio_triggered_buffer_cleanup(iio);
212 err_regulator_disable:
213 regulator_disable(adc->ref);
215 return err;
218 static int adc081c_remove(struct i2c_client *client)
220 struct iio_dev *iio = i2c_get_clientdata(client);
221 struct adc081c *adc = iio_priv(iio);
223 iio_device_unregister(iio);
224 iio_triggered_buffer_cleanup(iio);
225 regulator_disable(adc->ref);
227 return 0;
230 static const struct i2c_device_id adc081c_id[] = {
231 { "adc081c", ADC081C },
232 { "adc101c", ADC101C },
233 { "adc121c", ADC121C },
236 MODULE_DEVICE_TABLE(i2c, adc081c_id);
238 #ifdef CONFIG_OF
239 static const struct of_device_id adc081c_of_match[] = {
240 { .compatible = "ti,adc081c" },
241 { .compatible = "ti,adc101c" },
242 { .compatible = "ti,adc121c" },
245 MODULE_DEVICE_TABLE(of, adc081c_of_match);
246 #endif
248 #ifdef CONFIG_ACPI
249 static const struct acpi_device_id adc081c_acpi_match[] = {
250 { "ADC081C", ADC081C },
251 { "ADC101C", ADC101C },
252 { "ADC121C", ADC121C },
255 MODULE_DEVICE_TABLE(acpi, adc081c_acpi_match);
256 #endif
258 static struct i2c_driver adc081c_driver = {
259 .driver = {
260 .name = "adc081c",
261 .of_match_table = of_match_ptr(adc081c_of_match),
262 .acpi_match_table = ACPI_PTR(adc081c_acpi_match),
264 .probe = adc081c_probe,
265 .remove = adc081c_remove,
266 .id_table = adc081c_id,
268 module_i2c_driver(adc081c_driver);
270 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
271 MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
272 MODULE_LICENSE("GPL v2");