2 * Driver for the Nuvoton NAU7802 ADC
4 * Copyright 2013 Free Electrons
6 * Licensed under the GPLv2 or later.
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/wait.h>
14 #include <linux/log2.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
19 #define NAU7802_REG_PUCTRL 0x00
20 #define NAU7802_PUCTRL_RR(x) (x << 0)
21 #define NAU7802_PUCTRL_RR_BIT NAU7802_PUCTRL_RR(1)
22 #define NAU7802_PUCTRL_PUD(x) (x << 1)
23 #define NAU7802_PUCTRL_PUD_BIT NAU7802_PUCTRL_PUD(1)
24 #define NAU7802_PUCTRL_PUA(x) (x << 2)
25 #define NAU7802_PUCTRL_PUA_BIT NAU7802_PUCTRL_PUA(1)
26 #define NAU7802_PUCTRL_PUR(x) (x << 3)
27 #define NAU7802_PUCTRL_PUR_BIT NAU7802_PUCTRL_PUR(1)
28 #define NAU7802_PUCTRL_CS(x) (x << 4)
29 #define NAU7802_PUCTRL_CS_BIT NAU7802_PUCTRL_CS(1)
30 #define NAU7802_PUCTRL_CR(x) (x << 5)
31 #define NAU7802_PUCTRL_CR_BIT NAU7802_PUCTRL_CR(1)
32 #define NAU7802_PUCTRL_AVDDS(x) (x << 7)
33 #define NAU7802_PUCTRL_AVDDS_BIT NAU7802_PUCTRL_AVDDS(1)
34 #define NAU7802_REG_CTRL1 0x01
35 #define NAU7802_CTRL1_VLDO(x) (x << 3)
36 #define NAU7802_CTRL1_GAINS(x) (x)
37 #define NAU7802_CTRL1_GAINS_BITS 0x07
38 #define NAU7802_REG_CTRL2 0x02
39 #define NAU7802_CTRL2_CHS(x) (x << 7)
40 #define NAU7802_CTRL2_CRS(x) (x << 4)
41 #define NAU7802_SAMP_FREQ_320 0x07
42 #define NAU7802_CTRL2_CHS_BIT NAU7802_CTRL2_CHS(1)
43 #define NAU7802_REG_ADC_B2 0x12
44 #define NAU7802_REG_ADC_B1 0x13
45 #define NAU7802_REG_ADC_B0 0x14
46 #define NAU7802_REG_ADC_CTRL 0x15
48 #define NAU7802_MIN_CONVERSIONS 6
50 struct nau7802_state
{
51 struct i2c_client
*client
;
54 struct mutex data_lock
;
60 struct completion value_ok
;
63 #define NAU7802_CHANNEL(chan) { \
64 .type = IIO_VOLTAGE, \
67 .scan_index = (chan), \
68 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
69 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
70 BIT(IIO_CHAN_INFO_SAMP_FREQ) \
73 static const struct iio_chan_spec nau7802_chan_array
[] = {
78 static const u16 nau7802_sample_freq_avail
[] = {10, 20, 40, 80,
81 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 40 80 320");
83 static struct attribute
*nau7802_attributes
[] = {
84 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
88 static const struct attribute_group nau7802_attribute_group
= {
89 .attrs
= nau7802_attributes
,
92 static int nau7802_set_gain(struct nau7802_state
*st
, int gain
)
96 mutex_lock(&st
->lock
);
97 st
->conversion_count
= 0;
99 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_CTRL1
);
101 goto nau7802_sysfs_set_gain_out
;
102 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_CTRL1
,
103 (ret
& (~NAU7802_CTRL1_GAINS_BITS
)) |
106 nau7802_sysfs_set_gain_out
:
107 mutex_unlock(&st
->lock
);
112 static int nau7802_read_conversion(struct nau7802_state
*st
)
116 mutex_lock(&st
->data_lock
);
117 data
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_ADC_B2
);
119 goto nau7802_read_conversion_out
;
120 st
->last_value
= data
<< 16;
122 data
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_ADC_B1
);
124 goto nau7802_read_conversion_out
;
125 st
->last_value
|= data
<< 8;
127 data
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_ADC_B0
);
129 goto nau7802_read_conversion_out
;
130 st
->last_value
|= data
;
132 st
->last_value
= sign_extend32(st
->last_value
, 23);
134 nau7802_read_conversion_out
:
135 mutex_unlock(&st
->data_lock
);
141 * Conversions are synchronised on the rising edge of NAU7802_PUCTRL_CS_BIT
143 static int nau7802_sync(struct nau7802_state
*st
)
147 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
150 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
,
151 ret
| NAU7802_PUCTRL_CS_BIT
);
156 static irqreturn_t
nau7802_eoc_trigger(int irq
, void *private)
158 struct iio_dev
*indio_dev
= private;
159 struct nau7802_state
*st
= iio_priv(indio_dev
);
162 status
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
166 if (!(status
& NAU7802_PUCTRL_CR_BIT
))
169 if (nau7802_read_conversion(st
) < 0)
173 * Because there is actually only one ADC for both channels, we have to
174 * wait for enough conversions to happen before getting a significant
175 * value when changing channels and the values are far apart.
177 if (st
->conversion_count
< NAU7802_MIN_CONVERSIONS
)
178 st
->conversion_count
++;
179 if (st
->conversion_count
>= NAU7802_MIN_CONVERSIONS
)
180 complete_all(&st
->value_ok
);
185 static int nau7802_read_irq(struct iio_dev
*indio_dev
,
186 struct iio_chan_spec
const *chan
,
189 struct nau7802_state
*st
= iio_priv(indio_dev
);
192 INIT_COMPLETION(st
->value_ok
);
193 enable_irq(st
->client
->irq
);
197 /* read registers to ensure we flush everything */
198 ret
= nau7802_read_conversion(st
);
200 goto read_chan_info_failure
;
202 /* Wait for a conversion to finish */
203 ret
= wait_for_completion_interruptible_timeout(&st
->value_ok
,
204 msecs_to_jiffies(1000));
209 goto read_chan_info_failure
;
211 disable_irq(st
->client
->irq
);
213 *val
= st
->last_value
;
217 read_chan_info_failure
:
218 disable_irq(st
->client
->irq
);
223 static int nau7802_read_poll(struct iio_dev
*indio_dev
,
224 struct iio_chan_spec
const *chan
,
227 struct nau7802_state
*st
= iio_priv(indio_dev
);
232 /* read registers to ensure we flush everything */
233 ret
= nau7802_read_conversion(st
);
238 * Because there is actually only one ADC for both channels, we have to
239 * wait for enough conversions to happen before getting a significant
240 * value when changing channels and the values are far appart.
243 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
247 while (!(ret
& NAU7802_PUCTRL_CR_BIT
)) {
248 if (st
->sample_rate
!= NAU7802_SAMP_FREQ_320
)
252 ret
= i2c_smbus_read_byte_data(st
->client
,
258 ret
= nau7802_read_conversion(st
);
261 if (st
->conversion_count
< NAU7802_MIN_CONVERSIONS
)
262 st
->conversion_count
++;
263 } while (st
->conversion_count
< NAU7802_MIN_CONVERSIONS
);
265 *val
= st
->last_value
;
270 static int nau7802_read_raw(struct iio_dev
*indio_dev
,
271 struct iio_chan_spec
const *chan
,
272 int *val
, int *val2
, long mask
)
274 struct nau7802_state
*st
= iio_priv(indio_dev
);
278 case IIO_CHAN_INFO_RAW
:
279 mutex_lock(&st
->lock
);
281 * Select the channel to use
282 * - Channel 1 is value 0 in the CHS register
283 * - Channel 2 is value 1 in the CHS register
285 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_CTRL2
);
287 mutex_unlock(&st
->lock
);
291 if (((ret
& NAU7802_CTRL2_CHS_BIT
) && !chan
->channel
) ||
292 (!(ret
& NAU7802_CTRL2_CHS_BIT
) &&
294 st
->conversion_count
= 0;
295 ret
= i2c_smbus_write_byte_data(st
->client
,
297 NAU7802_CTRL2_CHS(chan
->channel
) |
298 NAU7802_CTRL2_CRS(st
->sample_rate
));
301 mutex_unlock(&st
->lock
);
307 ret
= nau7802_read_irq(indio_dev
, chan
, val
);
309 ret
= nau7802_read_poll(indio_dev
, chan
, val
);
311 mutex_unlock(&st
->lock
);
314 case IIO_CHAN_INFO_SCALE
:
315 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_CTRL1
);
320 * We have 24 bits of signed data, that means 23 bits of data
324 *val2
= 23 + (ret
& NAU7802_CTRL1_GAINS_BITS
);
326 return IIO_VAL_FRACTIONAL_LOG2
;
328 case IIO_CHAN_INFO_SAMP_FREQ
:
329 *val
= nau7802_sample_freq_avail
[st
->sample_rate
];
340 static int nau7802_write_raw(struct iio_dev
*indio_dev
,
341 struct iio_chan_spec
const *chan
,
342 int val
, int val2
, long mask
)
344 struct nau7802_state
*st
= iio_priv(indio_dev
);
348 case IIO_CHAN_INFO_SCALE
:
349 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
350 if (val2
== st
->scale_avail
[i
])
351 return nau7802_set_gain(st
, i
);
355 case IIO_CHAN_INFO_SAMP_FREQ
:
356 for (i
= 0; i
< ARRAY_SIZE(nau7802_sample_freq_avail
); i
++)
357 if (val
== nau7802_sample_freq_avail
[i
]) {
358 mutex_lock(&st
->lock
);
360 st
->conversion_count
= 0;
361 ret
= i2c_smbus_write_byte_data(st
->client
,
363 NAU7802_CTRL2_CRS(st
->sample_rate
));
364 mutex_unlock(&st
->lock
);
377 static int nau7802_write_raw_get_fmt(struct iio_dev
*indio_dev
,
378 struct iio_chan_spec
const *chan
,
381 return IIO_VAL_INT_PLUS_NANO
;
384 static const struct iio_info nau7802_info
= {
385 .driver_module
= THIS_MODULE
,
386 .read_raw
= &nau7802_read_raw
,
387 .write_raw
= &nau7802_write_raw
,
388 .write_raw_get_fmt
= nau7802_write_raw_get_fmt
,
389 .attrs
= &nau7802_attribute_group
,
392 static int nau7802_probe(struct i2c_client
*client
,
393 const struct i2c_device_id
*id
)
395 struct iio_dev
*indio_dev
;
396 struct nau7802_state
*st
;
397 struct device_node
*np
= client
->dev
.of_node
;
402 if (!client
->dev
.of_node
) {
403 dev_err(&client
->dev
, "No device tree node available.\n");
407 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*st
));
408 if (indio_dev
== NULL
)
411 st
= iio_priv(indio_dev
);
413 i2c_set_clientdata(client
, indio_dev
);
415 indio_dev
->dev
.parent
= &client
->dev
;
416 indio_dev
->name
= dev_name(&client
->dev
);
417 indio_dev
->modes
= INDIO_DIRECT_MODE
;
418 indio_dev
->info
= &nau7802_info
;
422 /* Reset the device */
423 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
,
424 NAU7802_PUCTRL_RR_BIT
);
428 /* Enter normal operation mode */
429 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
,
430 NAU7802_PUCTRL_PUD_BIT
);
435 * After about 200 usecs, the device should be ready and then
436 * the Power Up bit will be set to 1. If not, wait for it.
439 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
442 if (!(ret
& NAU7802_PUCTRL_PUR_BIT
))
445 of_property_read_u32(np
, "nuvoton,vldo", &tmp
);
448 data
= NAU7802_PUCTRL_PUD_BIT
| NAU7802_PUCTRL_PUA_BIT
|
449 NAU7802_PUCTRL_CS_BIT
;
451 data
|= NAU7802_PUCTRL_AVDDS_BIT
;
453 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
, data
);
456 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_ADC_CTRL
, 0x30);
461 data
= NAU7802_CTRL1_VLDO((4500 - tmp
) / 300);
462 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_CTRL1
,
468 /* Populate available ADC input ranges */
469 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
470 st
->scale_avail
[i
] = (((u64
)st
->vref_mv
) * 1000000000ULL)
473 init_completion(&st
->value_ok
);
476 * The ADC fires continuously and we can't do anything about
477 * it. So we need to have the IRQ disabled by default, and we
478 * will enable them back when we will need them..
481 ret
= request_threaded_irq(client
->irq
,
484 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
485 client
->dev
.driver
->name
,
489 * What may happen here is that our IRQ controller is
490 * not able to get level interrupt but this is required
491 * by this ADC as when going over 40 sample per second,
492 * the interrupt line may stay high between conversions.
493 * So, we continue no matter what but we switch to
496 dev_info(&client
->dev
,
497 "Failed to allocate IRQ, using polling mode\n");
500 disable_irq(client
->irq
);
505 * We are polling, use the fastest sample rate by
508 st
->sample_rate
= NAU7802_SAMP_FREQ_320
;
509 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_CTRL2
,
510 NAU7802_CTRL2_CRS(st
->sample_rate
));
515 /* Setup the ADC channels available on the board */
516 indio_dev
->num_channels
= ARRAY_SIZE(nau7802_chan_array
);
517 indio_dev
->channels
= nau7802_chan_array
;
519 mutex_init(&st
->lock
);
520 mutex_init(&st
->data_lock
);
522 ret
= iio_device_register(indio_dev
);
524 dev_err(&client
->dev
, "Couldn't register the device.\n");
525 goto error_device_register
;
530 error_device_register
:
531 mutex_destroy(&st
->lock
);
532 mutex_destroy(&st
->data_lock
);
535 free_irq(client
->irq
, indio_dev
);
540 static int nau7802_remove(struct i2c_client
*client
)
542 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
543 struct nau7802_state
*st
= iio_priv(indio_dev
);
545 iio_device_unregister(indio_dev
);
546 mutex_destroy(&st
->lock
);
547 mutex_destroy(&st
->data_lock
);
549 free_irq(client
->irq
, indio_dev
);
554 static const struct i2c_device_id nau7802_i2c_id
[] = {
558 MODULE_DEVICE_TABLE(i2c
, nau7802_i2c_id
);
560 static const struct of_device_id nau7802_dt_ids
[] = {
561 { .compatible
= "nuvoton,nau7802" },
564 MODULE_DEVICE_TABLE(of
, nau7802_dt_ids
);
566 static struct i2c_driver nau7802_driver
= {
567 .probe
= nau7802_probe
,
568 .remove
= nau7802_remove
,
569 .id_table
= nau7802_i2c_id
,
572 .of_match_table
= of_match_ptr(nau7802_dt_ids
),
576 module_i2c_driver(nau7802_driver
);
578 MODULE_LICENSE("GPL");
579 MODULE_DESCRIPTION("Nuvoton NAU7802 ADC Driver");
580 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
581 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");