blob: 41a21e986c1a326593a4864d53f2d33882342a98 [file] [log] [blame]
Oskar Anderof5ce4a72013-05-03 10:58:00 +01001/*
2 * Copyright (C) 2013 Oskar Andero <oskar.andero@gmail.com>
Søren Andersenb12206e2014-10-08 20:42:00 +02003 * Copyright (C) 2014 Rose Technology
4 * Allan Bendorff Jensen <abj@rosetechnology.dk>
5 * Soren Andersen <san@rosetechnology.dk>
Oskar Anderof5ce4a72013-05-03 10:58:00 +01006 *
Søren Andersenb12206e2014-10-08 20:42:00 +02007 * Driver for following ADC chips from Microchip Technology's:
8 * 10 Bit converter
9 * MCP3001
10 * MCP3002
11 * MCP3004
12 * MCP3008
13 * ------------
14 * 12 bit converter
15 * MCP3201
16 * MCP3202
17 * MCP3204
18 * MCP3208
19 * ------------
20 *
Oskar Anderof5ce4a72013-05-03 10:58:00 +010021 * Datasheet can be found here:
Søren Andersenb12206e2014-10-08 20:42:00 +020022 * http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf mcp3001
23 * http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf mcp3002
24 * http://ww1.microchip.com/downloads/en/DeviceDoc/21295d.pdf mcp3004/08
25 * http://ww1.microchip.com/downloads/en/DeviceDoc/21290D.pdf mcp3201
26 * http://ww1.microchip.com/downloads/en/DeviceDoc/21034D.pdf mcp3202
27 * http://ww1.microchip.com/downloads/en/DeviceDoc/21298c.pdf mcp3204/08
Andrea Galbuseraf686a362015-07-14 15:36:21 +020028 * http://ww1.microchip.com/downloads/en/DeviceDoc/21700E.pdf mcp3301
Oskar Anderof5ce4a72013-05-03 10:58:00 +010029 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License version 2 as
32 * published by the Free Software Foundation.
33 */
34
35#include <linux/err.h>
Søren Andersenb12206e2014-10-08 20:42:00 +020036#include <linux/delay.h>
Oskar Anderof5ce4a72013-05-03 10:58:00 +010037#include <linux/spi/spi.h>
38#include <linux/module.h>
39#include <linux/iio/iio.h>
40#include <linux/regulator/consumer.h>
41
Oskar Anderof5ce4a72013-05-03 10:58:00 +010042enum {
Søren Andersenb12206e2014-10-08 20:42:00 +020043 mcp3001,
44 mcp3002,
45 mcp3004,
46 mcp3008,
47 mcp3201,
48 mcp3202,
Oskar Anderof5ce4a72013-05-03 10:58:00 +010049 mcp3204,
50 mcp3208,
Andrea Galbuseraf686a362015-07-14 15:36:21 +020051 mcp3301,
Oskar Anderof5ce4a72013-05-03 10:58:00 +010052};
53
Søren Andersenb12206e2014-10-08 20:42:00 +020054struct mcp320x_chip_info {
55 const struct iio_chan_spec *channels;
56 unsigned int num_channels;
57 unsigned int resolution;
58};
59
Oskar Anderof5ce4a72013-05-03 10:58:00 +010060struct mcp320x {
61 struct spi_device *spi;
62 struct spi_message msg;
63 struct spi_transfer transfer[2];
64
Oskar Anderof5ce4a72013-05-03 10:58:00 +010065 struct regulator *reg;
66 struct mutex lock;
Søren Andersenb12206e2014-10-08 20:42:00 +020067 const struct mcp320x_chip_info *chip_info;
Michael Welling0e81bc92015-05-06 11:49:17 -050068
69 u8 tx_buf ____cacheline_aligned;
70 u8 rx_buf[2];
Oskar Anderof5ce4a72013-05-03 10:58:00 +010071};
72
Søren Andersenb12206e2014-10-08 20:42:00 +020073static int mcp320x_channel_to_tx_data(int device_index,
74 const unsigned int channel, bool differential)
75{
76 int start_bit = 1;
77
78 switch (device_index) {
79 case mcp3001:
80 case mcp3201:
Andrea Galbuseraf686a362015-07-14 15:36:21 +020081 case mcp3301:
Søren Andersenb12206e2014-10-08 20:42:00 +020082 return 0;
83 case mcp3002:
84 case mcp3202:
85 return ((start_bit << 4) | (!differential << 3) |
86 (channel << 2));
87 case mcp3004:
88 case mcp3204:
89 case mcp3008:
90 case mcp3208:
91 return ((start_bit << 6) | (!differential << 5) |
92 (channel << 2));
93 default:
94 return -EINVAL;
95 }
96}
97
98static int mcp320x_adc_conversion(struct mcp320x *adc, u8 channel,
99 bool differential, int device_index)
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100100{
101 int ret;
102
Søren Andersenb12206e2014-10-08 20:42:00 +0200103 adc->rx_buf[0] = 0;
104 adc->rx_buf[1] = 0;
105 adc->tx_buf = mcp320x_channel_to_tx_data(device_index,
106 channel, differential);
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100107
Andrea Galbuseraf686a362015-07-14 15:36:21 +0200108 if (device_index != mcp3001 && device_index != mcp3201 && device_index != mcp3301) {
Søren Andersenb12206e2014-10-08 20:42:00 +0200109 ret = spi_sync(adc->spi, &adc->msg);
110 if (ret < 0)
111 return ret;
112 } else {
113 ret = spi_read(adc->spi, &adc->rx_buf, sizeof(adc->rx_buf));
114 if (ret < 0)
115 return ret;
116 }
117
118 switch (device_index) {
119 case mcp3001:
120 return (adc->rx_buf[0] << 5 | adc->rx_buf[1] >> 3);
121 case mcp3002:
122 case mcp3004:
123 case mcp3008:
124 return (adc->rx_buf[0] << 2 | adc->rx_buf[1] >> 6);
125 case mcp3201:
126 return (adc->rx_buf[0] << 7 | adc->rx_buf[1] >> 1);
127 case mcp3202:
128 case mcp3204:
129 case mcp3208:
130 return (adc->rx_buf[0] << 4 | adc->rx_buf[1] >> 4);
Andrea Galbuseraf686a362015-07-14 15:36:21 +0200131 case mcp3301:
132 return sign_extend32((adc->rx_buf[0] & 0x1f) << 8 | adc->rx_buf[1], 12);
Søren Andersenb12206e2014-10-08 20:42:00 +0200133 default:
134 return -EINVAL;
135 }
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100136}
137
138static int mcp320x_read_raw(struct iio_dev *indio_dev,
139 struct iio_chan_spec const *channel, int *val,
140 int *val2, long mask)
141{
142 struct mcp320x *adc = iio_priv(indio_dev);
143 int ret = -EINVAL;
Søren Andersenb12206e2014-10-08 20:42:00 +0200144 int device_index = 0;
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100145
146 mutex_lock(&adc->lock);
147
Søren Andersenb12206e2014-10-08 20:42:00 +0200148 device_index = spi_get_device_id(adc->spi)->driver_data;
149
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100150 switch (mask) {
151 case IIO_CHAN_INFO_RAW:
Søren Andersenb12206e2014-10-08 20:42:00 +0200152 ret = mcp320x_adc_conversion(adc, channel->address,
153 channel->differential, device_index);
154
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100155 if (ret < 0)
156 goto out;
157
158 *val = ret;
159 ret = IIO_VAL_INT;
160 break;
161
162 case IIO_CHAN_INFO_SCALE:
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100163 ret = regulator_get_voltage(adc->reg);
164 if (ret < 0)
165 goto out;
166
Søren Andersenb12206e2014-10-08 20:42:00 +0200167 /* convert regulator output voltage to mV */
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100168 *val = ret / 1000;
Søren Andersenb12206e2014-10-08 20:42:00 +0200169 *val2 = adc->chip_info->resolution;
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100170 ret = IIO_VAL_FRACTIONAL_LOG2;
171 break;
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100172 }
173
174out:
175 mutex_unlock(&adc->lock);
176
177 return ret;
178}
179
180#define MCP320X_VOLTAGE_CHANNEL(num) \
181 { \
182 .type = IIO_VOLTAGE, \
183 .indexed = 1, \
184 .channel = (num), \
185 .address = (num), \
186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
188 }
189
190#define MCP320X_VOLTAGE_CHANNEL_DIFF(num) \
191 { \
192 .type = IIO_VOLTAGE, \
193 .indexed = 1, \
194 .channel = (num * 2), \
195 .channel2 = (num * 2 + 1), \
196 .address = (num * 2), \
197 .differential = 1, \
198 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
199 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
200 }
201
Søren Andersenb12206e2014-10-08 20:42:00 +0200202static const struct iio_chan_spec mcp3201_channels[] = {
203 MCP320X_VOLTAGE_CHANNEL_DIFF(0),
204};
205
206static const struct iio_chan_spec mcp3202_channels[] = {
207 MCP320X_VOLTAGE_CHANNEL(0),
208 MCP320X_VOLTAGE_CHANNEL(1),
209 MCP320X_VOLTAGE_CHANNEL_DIFF(0),
210};
211
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100212static const struct iio_chan_spec mcp3204_channels[] = {
213 MCP320X_VOLTAGE_CHANNEL(0),
214 MCP320X_VOLTAGE_CHANNEL(1),
215 MCP320X_VOLTAGE_CHANNEL(2),
216 MCP320X_VOLTAGE_CHANNEL(3),
217 MCP320X_VOLTAGE_CHANNEL_DIFF(0),
218 MCP320X_VOLTAGE_CHANNEL_DIFF(1),
219};
220
221static const struct iio_chan_spec mcp3208_channels[] = {
222 MCP320X_VOLTAGE_CHANNEL(0),
223 MCP320X_VOLTAGE_CHANNEL(1),
224 MCP320X_VOLTAGE_CHANNEL(2),
225 MCP320X_VOLTAGE_CHANNEL(3),
226 MCP320X_VOLTAGE_CHANNEL(4),
227 MCP320X_VOLTAGE_CHANNEL(5),
228 MCP320X_VOLTAGE_CHANNEL(6),
229 MCP320X_VOLTAGE_CHANNEL(7),
230 MCP320X_VOLTAGE_CHANNEL_DIFF(0),
231 MCP320X_VOLTAGE_CHANNEL_DIFF(1),
232 MCP320X_VOLTAGE_CHANNEL_DIFF(2),
233 MCP320X_VOLTAGE_CHANNEL_DIFF(3),
234};
235
236static const struct iio_info mcp320x_info = {
237 .read_raw = mcp320x_read_raw,
238 .driver_module = THIS_MODULE,
239};
240
Søren Andersenb12206e2014-10-08 20:42:00 +0200241static const struct mcp320x_chip_info mcp320x_chip_infos[] = {
242 [mcp3001] = {
243 .channels = mcp3201_channels,
244 .num_channels = ARRAY_SIZE(mcp3201_channels),
245 .resolution = 10
246 },
247 [mcp3002] = {
248 .channels = mcp3202_channels,
249 .num_channels = ARRAY_SIZE(mcp3202_channels),
250 .resolution = 10
251 },
252 [mcp3004] = {
253 .channels = mcp3204_channels,
254 .num_channels = ARRAY_SIZE(mcp3204_channels),
255 .resolution = 10
256 },
257 [mcp3008] = {
258 .channels = mcp3208_channels,
259 .num_channels = ARRAY_SIZE(mcp3208_channels),
260 .resolution = 10
261 },
262 [mcp3201] = {
263 .channels = mcp3201_channels,
264 .num_channels = ARRAY_SIZE(mcp3201_channels),
265 .resolution = 12
266 },
267 [mcp3202] = {
268 .channels = mcp3202_channels,
269 .num_channels = ARRAY_SIZE(mcp3202_channels),
270 .resolution = 12
271 },
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100272 [mcp3204] = {
273 .channels = mcp3204_channels,
Søren Andersenb12206e2014-10-08 20:42:00 +0200274 .num_channels = ARRAY_SIZE(mcp3204_channels),
275 .resolution = 12
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100276 },
277 [mcp3208] = {
278 .channels = mcp3208_channels,
Søren Andersenb12206e2014-10-08 20:42:00 +0200279 .num_channels = ARRAY_SIZE(mcp3208_channels),
280 .resolution = 12
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100281 },
Andrea Galbuseraf686a362015-07-14 15:36:21 +0200282 [mcp3301] = {
283 .channels = mcp3201_channels,
284 .num_channels = ARRAY_SIZE(mcp3201_channels),
285 .resolution = 13
286 },
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100287};
288
289static int mcp320x_probe(struct spi_device *spi)
290{
291 struct iio_dev *indio_dev;
292 struct mcp320x *adc;
Søren Andersenb12206e2014-10-08 20:42:00 +0200293 const struct mcp320x_chip_info *chip_info;
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100294 int ret;
295
Sachin Kamata726dea2013-07-23 09:58:00 +0100296 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100297 if (!indio_dev)
298 return -ENOMEM;
299
300 adc = iio_priv(indio_dev);
301 adc->spi = spi;
302
303 indio_dev->dev.parent = &spi->dev;
304 indio_dev->name = spi_get_device_id(spi)->name;
305 indio_dev->modes = INDIO_DIRECT_MODE;
306 indio_dev->info = &mcp320x_info;
307
Søren Andersenb12206e2014-10-08 20:42:00 +0200308 chip_info = &mcp320x_chip_infos[spi_get_device_id(spi)->driver_data];
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100309 indio_dev->channels = chip_info->channels;
310 indio_dev->num_channels = chip_info->num_channels;
311
Manfred Schlaegl41be6a02015-07-10 22:55:30 +0200312 adc->chip_info = chip_info;
313
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100314 adc->transfer[0].tx_buf = &adc->tx_buf;
315 adc->transfer[0].len = sizeof(adc->tx_buf);
316 adc->transfer[1].rx_buf = adc->rx_buf;
317 adc->transfer[1].len = sizeof(adc->rx_buf);
318
319 spi_message_init_with_transfers(&adc->msg, adc->transfer,
320 ARRAY_SIZE(adc->transfer));
321
Sachin Kamata726dea2013-07-23 09:58:00 +0100322 adc->reg = devm_regulator_get(&spi->dev, "vref");
323 if (IS_ERR(adc->reg))
324 return PTR_ERR(adc->reg);
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100325
326 ret = regulator_enable(adc->reg);
327 if (ret < 0)
Sachin Kamata726dea2013-07-23 09:58:00 +0100328 return ret;
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100329
330 mutex_init(&adc->lock);
331
332 ret = iio_device_register(indio_dev);
333 if (ret < 0)
334 goto reg_disable;
335
336 return 0;
337
338reg_disable:
339 regulator_disable(adc->reg);
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100340
341 return ret;
342}
343
344static int mcp320x_remove(struct spi_device *spi)
345{
346 struct iio_dev *indio_dev = spi_get_drvdata(spi);
347 struct mcp320x *adc = iio_priv(indio_dev);
348
349 iio_device_unregister(indio_dev);
350 regulator_disable(adc->reg);
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100351
352 return 0;
353}
354
Søren Andersenb12206e2014-10-08 20:42:00 +0200355#if defined(CONFIG_OF)
356static const struct of_device_id mcp320x_dt_ids[] = {
357 {
358 .compatible = "mcp3001",
359 .data = &mcp320x_chip_infos[mcp3001],
360 }, {
361 .compatible = "mcp3002",
362 .data = &mcp320x_chip_infos[mcp3002],
363 }, {
364 .compatible = "mcp3004",
365 .data = &mcp320x_chip_infos[mcp3004],
366 }, {
367 .compatible = "mcp3008",
368 .data = &mcp320x_chip_infos[mcp3008],
369 }, {
370 .compatible = "mcp3201",
371 .data = &mcp320x_chip_infos[mcp3201],
372 }, {
373 .compatible = "mcp3202",
374 .data = &mcp320x_chip_infos[mcp3202],
375 }, {
376 .compatible = "mcp3204",
377 .data = &mcp320x_chip_infos[mcp3204],
378 }, {
379 .compatible = "mcp3208",
380 .data = &mcp320x_chip_infos[mcp3208],
381 }, {
Andrea Galbuseraf686a362015-07-14 15:36:21 +0200382 .compatible = "mcp3301",
383 .data = &mcp320x_chip_infos[mcp3301],
384 }, {
Søren Andersenb12206e2014-10-08 20:42:00 +0200385 }
386};
387MODULE_DEVICE_TABLE(of, mcp320x_dt_ids);
388#endif
389
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100390static const struct spi_device_id mcp320x_id[] = {
Søren Andersenb12206e2014-10-08 20:42:00 +0200391 { "mcp3001", mcp3001 },
392 { "mcp3002", mcp3002 },
393 { "mcp3004", mcp3004 },
394 { "mcp3008", mcp3008 },
395 { "mcp3201", mcp3201 },
396 { "mcp3202", mcp3202 },
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100397 { "mcp3204", mcp3204 },
398 { "mcp3208", mcp3208 },
Andrea Galbuseraf686a362015-07-14 15:36:21 +0200399 { "mcp3301", mcp3301 },
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100400 { }
401};
402MODULE_DEVICE_TABLE(spi, mcp320x_id);
403
404static struct spi_driver mcp320x_driver = {
405 .driver = {
406 .name = "mcp320x",
Javier Martinez Canillasab6ff6c2015-08-20 09:07:26 +0200407 .of_match_table = of_match_ptr(mcp320x_dt_ids),
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100408 .owner = THIS_MODULE,
409 },
410 .probe = mcp320x_probe,
411 .remove = mcp320x_remove,
412 .id_table = mcp320x_id,
413};
414module_spi_driver(mcp320x_driver);
415
416MODULE_AUTHOR("Oskar Andero <oskar.andero@gmail.com>");
Søren Andersenb12206e2014-10-08 20:42:00 +0200417MODULE_DESCRIPTION("Microchip Technology MCP3x01/02/04/08");
Oskar Anderof5ce4a72013-05-03 10:58:00 +0100418MODULE_LICENSE("GPL v2");