GNU Linux-libre 4.14.262-gnu1
[releases.git] / drivers / iio / adc / ltc2471.c
1 /*
2  * Driver for Linear Technology LTC2471 and LTC2473 voltage monitors
3  * The LTC2473 is identical to the 2471, but reports a differential signal.
4  *
5  * Copyright (C) 2017 Topic Embedded Products
6  * Author: Mike Looijmans <mike.looijmans@topic.nl>
7  *
8  * License: GPLv2
9  */
10
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17
18 enum ltc2471_chips {
19         ltc2471,
20         ltc2473,
21 };
22
23 struct ltc2471_data {
24         struct i2c_client *client;
25 };
26
27 /* Reference voltage is 1.25V */
28 #define LTC2471_VREF 1250
29
30 /* Read two bytes from the I2C bus to obtain the ADC result */
31 static int ltc2471_get_value(struct i2c_client *client)
32 {
33         int ret;
34         __be16 buf;
35
36         ret = i2c_master_recv(client, (char *)&buf, sizeof(buf));
37         if (ret < 0)
38                 return ret;
39         if (ret != sizeof(buf))
40                 return -EIO;
41
42         /* MSB first */
43         return be16_to_cpu(buf);
44 }
45
46 static int ltc2471_read_raw(struct iio_dev *indio_dev,
47                             struct iio_chan_spec const *chan,
48                             int *val, int *val2, long info)
49 {
50         struct ltc2471_data *data = iio_priv(indio_dev);
51         int ret;
52
53         switch (info) {
54         case IIO_CHAN_INFO_RAW:
55                 ret = ltc2471_get_value(data->client);
56                 if (ret < 0)
57                         return ret;
58                 *val = ret;
59                 return IIO_VAL_INT;
60
61         case IIO_CHAN_INFO_SCALE:
62                 if (chan->differential)
63                         /* Output ranges from -VREF to +VREF */
64                         *val = 2 * LTC2471_VREF;
65                 else
66                         /* Output ranges from 0 to VREF */
67                         *val = LTC2471_VREF;
68                 *val2 = 16;     /* 16 data bits */
69                 return IIO_VAL_FRACTIONAL_LOG2;
70
71         case IIO_CHAN_INFO_OFFSET:
72                 /* Only differential chip has this property */
73                 *val = -LTC2471_VREF;
74                 return IIO_VAL_INT;
75
76         default:
77                 return -EINVAL;
78         }
79 }
80
81 static const struct iio_chan_spec ltc2471_channel[] = {
82         {
83                 .type = IIO_VOLTAGE,
84                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
85                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
86         },
87 };
88
89 static const struct iio_chan_spec ltc2473_channel[] = {
90         {
91                 .type = IIO_VOLTAGE,
92                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
93                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
94                                             BIT(IIO_CHAN_INFO_OFFSET),
95                 .differential = 1,
96         },
97 };
98
99 static const struct iio_info ltc2471_info = {
100         .read_raw = ltc2471_read_raw,
101         .driver_module = THIS_MODULE,
102 };
103
104 static int ltc2471_i2c_probe(struct i2c_client *client,
105                              const struct i2c_device_id *id)
106 {
107         struct iio_dev *indio_dev;
108         struct ltc2471_data *data;
109         int ret;
110
111         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
112                 return -EOPNOTSUPP;
113
114         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
115         if (!indio_dev)
116                 return -ENOMEM;
117
118         data = iio_priv(indio_dev);
119         data->client = client;
120
121         indio_dev->dev.parent = &client->dev;
122         indio_dev->name = id->name;
123         indio_dev->info = &ltc2471_info;
124         indio_dev->modes = INDIO_DIRECT_MODE;
125         if (id->driver_data == ltc2473)
126                 indio_dev->channels = ltc2473_channel;
127         else
128                 indio_dev->channels = ltc2471_channel;
129         indio_dev->num_channels = 1;
130
131         /* Trigger once to start conversion and check if chip is there */
132         ret = ltc2471_get_value(client);
133         if (ret < 0) {
134                 dev_err(&client->dev, "Cannot read from device.\n");
135                 return ret;
136         }
137
138         return devm_iio_device_register(&client->dev, indio_dev);
139 }
140
141 static const struct i2c_device_id ltc2471_i2c_id[] = {
142         { "ltc2471", ltc2471 },
143         { "ltc2473", ltc2473 },
144         {}
145 };
146 MODULE_DEVICE_TABLE(i2c, ltc2471_i2c_id);
147
148 static struct i2c_driver ltc2471_i2c_driver = {
149         .driver = {
150                 .name = "ltc2471",
151         },
152         .probe    = ltc2471_i2c_probe,
153         .id_table = ltc2471_i2c_id,
154 };
155
156 module_i2c_driver(ltc2471_i2c_driver);
157
158 MODULE_DESCRIPTION("LTC2471/LTC2473 ADC driver");
159 MODULE_AUTHOR("Topic Embedded Products");
160 MODULE_LICENSE("GPL v2");