2 * Copyright (C) 2013 Oskar Andero <oskar.andero@gmail.com>
3 * Copyright (C) 2014 Rose Technology
4 * Allan Bendorff Jensen <abj@rosetechnology.dk>
5 * Soren Andersen <san@rosetechnology.dk>
7 * Driver for following ADC chips from Microchip Technology's:
21 * Datasheet can be found here:
22 * 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
28 * http://ww1.microchip.com/downloads/en/DeviceDoc/21700E.pdf mcp3301
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.
35 #include <linux/err.h>
36 #include <linux/delay.h>
37 #include <linux/spi/spi.h>
38 #include <linux/module.h>
39 #include <linux/iio/iio.h>
40 #include <linux/regulator/consumer.h>
54 struct mcp320x_chip_info
{
55 const struct iio_chan_spec
*channels
;
56 unsigned int num_channels
;
57 unsigned int resolution
;
61 struct spi_device
*spi
;
62 struct spi_message msg
;
63 struct spi_transfer transfer
[2];
65 struct regulator
*reg
;
67 const struct mcp320x_chip_info
*chip_info
;
69 u8 tx_buf ____cacheline_aligned
;
73 static int mcp320x_channel_to_tx_data(int device_index
,
74 const unsigned int channel
, bool differential
)
78 switch (device_index
) {
85 return ((start_bit
<< 4) | (!differential
<< 3) |
91 return ((start_bit
<< 6) | (!differential
<< 5) |
98 static int mcp320x_adc_conversion(struct mcp320x
*adc
, u8 channel
,
99 bool differential
, int device_index
)
105 adc
->tx_buf
= mcp320x_channel_to_tx_data(device_index
,
106 channel
, differential
);
108 if (device_index
!= mcp3001
&& device_index
!= mcp3201
&& device_index
!= mcp3301
) {
109 ret
= spi_sync(adc
->spi
, &adc
->msg
);
113 ret
= spi_read(adc
->spi
, &adc
->rx_buf
, sizeof(adc
->rx_buf
));
118 switch (device_index
) {
120 return (adc
->rx_buf
[0] << 5 | adc
->rx_buf
[1] >> 3);
124 return (adc
->rx_buf
[0] << 2 | adc
->rx_buf
[1] >> 6);
126 return (adc
->rx_buf
[0] << 7 | adc
->rx_buf
[1] >> 1);
130 return (adc
->rx_buf
[0] << 4 | adc
->rx_buf
[1] >> 4);
132 return sign_extend32((adc
->rx_buf
[0] & 0x1f) << 8 | adc
->rx_buf
[1], 12);
138 static int mcp320x_read_raw(struct iio_dev
*indio_dev
,
139 struct iio_chan_spec
const *channel
, int *val
,
140 int *val2
, long mask
)
142 struct mcp320x
*adc
= iio_priv(indio_dev
);
144 int device_index
= 0;
146 mutex_lock(&adc
->lock
);
148 device_index
= spi_get_device_id(adc
->spi
)->driver_data
;
151 case IIO_CHAN_INFO_RAW
:
152 ret
= mcp320x_adc_conversion(adc
, channel
->address
,
153 channel
->differential
, device_index
);
162 case IIO_CHAN_INFO_SCALE
:
163 ret
= regulator_get_voltage(adc
->reg
);
167 /* convert regulator output voltage to mV */
169 *val2
= adc
->chip_info
->resolution
;
170 ret
= IIO_VAL_FRACTIONAL_LOG2
;
175 mutex_unlock(&adc
->lock
);
180 #define MCP320X_VOLTAGE_CHANNEL(num) \
182 .type = IIO_VOLTAGE, \
186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
190 #define MCP320X_VOLTAGE_CHANNEL_DIFF(num) \
192 .type = IIO_VOLTAGE, \
194 .channel = (num * 2), \
195 .channel2 = (num * 2 + 1), \
196 .address = (num * 2), \
198 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
199 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
202 static const struct iio_chan_spec mcp3201_channels
[] = {
203 MCP320X_VOLTAGE_CHANNEL_DIFF(0),
206 static const struct iio_chan_spec mcp3202_channels
[] = {
207 MCP320X_VOLTAGE_CHANNEL(0),
208 MCP320X_VOLTAGE_CHANNEL(1),
209 MCP320X_VOLTAGE_CHANNEL_DIFF(0),
212 static 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),
221 static 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),
236 static const struct iio_info mcp320x_info
= {
237 .read_raw
= mcp320x_read_raw
,
238 .driver_module
= THIS_MODULE
,
241 static const struct mcp320x_chip_info mcp320x_chip_infos
[] = {
243 .channels
= mcp3201_channels
,
244 .num_channels
= ARRAY_SIZE(mcp3201_channels
),
248 .channels
= mcp3202_channels
,
249 .num_channels
= ARRAY_SIZE(mcp3202_channels
),
253 .channels
= mcp3204_channels
,
254 .num_channels
= ARRAY_SIZE(mcp3204_channels
),
258 .channels
= mcp3208_channels
,
259 .num_channels
= ARRAY_SIZE(mcp3208_channels
),
263 .channels
= mcp3201_channels
,
264 .num_channels
= ARRAY_SIZE(mcp3201_channels
),
268 .channels
= mcp3202_channels
,
269 .num_channels
= ARRAY_SIZE(mcp3202_channels
),
273 .channels
= mcp3204_channels
,
274 .num_channels
= ARRAY_SIZE(mcp3204_channels
),
278 .channels
= mcp3208_channels
,
279 .num_channels
= ARRAY_SIZE(mcp3208_channels
),
283 .channels
= mcp3201_channels
,
284 .num_channels
= ARRAY_SIZE(mcp3201_channels
),
289 static int mcp320x_probe(struct spi_device
*spi
)
291 struct iio_dev
*indio_dev
;
293 const struct mcp320x_chip_info
*chip_info
;
296 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
300 adc
= iio_priv(indio_dev
);
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
;
308 chip_info
= &mcp320x_chip_infos
[spi_get_device_id(spi
)->driver_data
];
309 indio_dev
->channels
= chip_info
->channels
;
310 indio_dev
->num_channels
= chip_info
->num_channels
;
312 adc
->chip_info
= chip_info
;
314 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
);
319 spi_message_init_with_transfers(&adc
->msg
, adc
->transfer
,
320 ARRAY_SIZE(adc
->transfer
));
322 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
323 if (IS_ERR(adc
->reg
))
324 return PTR_ERR(adc
->reg
);
326 ret
= regulator_enable(adc
->reg
);
330 mutex_init(&adc
->lock
);
332 ret
= iio_device_register(indio_dev
);
339 regulator_disable(adc
->reg
);
344 static int mcp320x_remove(struct spi_device
*spi
)
346 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
347 struct mcp320x
*adc
= iio_priv(indio_dev
);
349 iio_device_unregister(indio_dev
);
350 regulator_disable(adc
->reg
);
355 #if defined(CONFIG_OF)
356 static const struct of_device_id mcp320x_dt_ids
[] = {
357 /* NOTE: The use of compatibles with no vendor prefix is deprecated. */
359 .compatible
= "mcp3001",
360 .data
= &mcp320x_chip_infos
[mcp3001
],
362 .compatible
= "mcp3002",
363 .data
= &mcp320x_chip_infos
[mcp3002
],
365 .compatible
= "mcp3004",
366 .data
= &mcp320x_chip_infos
[mcp3004
],
368 .compatible
= "mcp3008",
369 .data
= &mcp320x_chip_infos
[mcp3008
],
371 .compatible
= "mcp3201",
372 .data
= &mcp320x_chip_infos
[mcp3201
],
374 .compatible
= "mcp3202",
375 .data
= &mcp320x_chip_infos
[mcp3202
],
377 .compatible
= "mcp3204",
378 .data
= &mcp320x_chip_infos
[mcp3204
],
380 .compatible
= "mcp3208",
381 .data
= &mcp320x_chip_infos
[mcp3208
],
383 .compatible
= "mcp3301",
384 .data
= &mcp320x_chip_infos
[mcp3301
],
386 .compatible
= "microchip,mcp3001",
387 .data
= &mcp320x_chip_infos
[mcp3001
],
389 .compatible
= "microchip,mcp3002",
390 .data
= &mcp320x_chip_infos
[mcp3002
],
392 .compatible
= "microchip,mcp3004",
393 .data
= &mcp320x_chip_infos
[mcp3004
],
395 .compatible
= "microchip,mcp3008",
396 .data
= &mcp320x_chip_infos
[mcp3008
],
398 .compatible
= "microchip,mcp3201",
399 .data
= &mcp320x_chip_infos
[mcp3201
],
401 .compatible
= "microchip,mcp3202",
402 .data
= &mcp320x_chip_infos
[mcp3202
],
404 .compatible
= "microchip,mcp3204",
405 .data
= &mcp320x_chip_infos
[mcp3204
],
407 .compatible
= "microchip,mcp3208",
408 .data
= &mcp320x_chip_infos
[mcp3208
],
410 .compatible
= "microchip,mcp3301",
411 .data
= &mcp320x_chip_infos
[mcp3301
],
415 MODULE_DEVICE_TABLE(of
, mcp320x_dt_ids
);
418 static const struct spi_device_id mcp320x_id
[] = {
419 { "mcp3001", mcp3001
},
420 { "mcp3002", mcp3002
},
421 { "mcp3004", mcp3004
},
422 { "mcp3008", mcp3008
},
423 { "mcp3201", mcp3201
},
424 { "mcp3202", mcp3202
},
425 { "mcp3204", mcp3204
},
426 { "mcp3208", mcp3208
},
427 { "mcp3301", mcp3301
},
430 MODULE_DEVICE_TABLE(spi
, mcp320x_id
);
432 static struct spi_driver mcp320x_driver
= {
435 .of_match_table
= of_match_ptr(mcp320x_dt_ids
),
437 .probe
= mcp320x_probe
,
438 .remove
= mcp320x_remove
,
439 .id_table
= mcp320x_id
,
441 module_spi_driver(mcp320x_driver
);
443 MODULE_AUTHOR("Oskar Andero <oskar.andero@gmail.com>");
444 MODULE_DESCRIPTION("Microchip Technology MCP3x01/02/04/08");
445 MODULE_LICENSE("GPL v2");