1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Nuvoton NAU7802 ADC
5 * Copyright 2013 Free Electrons
8 #include <linux/delay.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/wait.h>
13 #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 ssize_t
nau7802_show_scales(struct device
*dev
,
82 struct device_attribute
*attr
, char *buf
)
84 struct nau7802_state
*st
= iio_priv(dev_to_iio_dev(dev
));
87 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
88 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%09d ",
96 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 40 80 320");
98 static IIO_DEVICE_ATTR(in_voltage_scale_available
, S_IRUGO
, nau7802_show_scales
,
101 static struct attribute
*nau7802_attributes
[] = {
102 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
103 &iio_dev_attr_in_voltage_scale_available
.dev_attr
.attr
,
107 static const struct attribute_group nau7802_attribute_group
= {
108 .attrs
= nau7802_attributes
,
111 static int nau7802_set_gain(struct nau7802_state
*st
, int gain
)
115 mutex_lock(&st
->lock
);
116 st
->conversion_count
= 0;
118 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_CTRL1
);
120 goto nau7802_sysfs_set_gain_out
;
121 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_CTRL1
,
122 (ret
& (~NAU7802_CTRL1_GAINS_BITS
)) |
125 nau7802_sysfs_set_gain_out
:
126 mutex_unlock(&st
->lock
);
131 static int nau7802_read_conversion(struct nau7802_state
*st
)
135 mutex_lock(&st
->data_lock
);
136 data
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_ADC_B2
);
138 goto nau7802_read_conversion_out
;
139 st
->last_value
= data
<< 16;
141 data
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_ADC_B1
);
143 goto nau7802_read_conversion_out
;
144 st
->last_value
|= data
<< 8;
146 data
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_ADC_B0
);
148 goto nau7802_read_conversion_out
;
149 st
->last_value
|= data
;
151 st
->last_value
= sign_extend32(st
->last_value
, 23);
153 nau7802_read_conversion_out
:
154 mutex_unlock(&st
->data_lock
);
160 * Conversions are synchronised on the rising edge of NAU7802_PUCTRL_CS_BIT
162 static int nau7802_sync(struct nau7802_state
*st
)
166 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
169 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
,
170 ret
| NAU7802_PUCTRL_CS_BIT
);
175 static irqreturn_t
nau7802_eoc_trigger(int irq
, void *private)
177 struct iio_dev
*indio_dev
= private;
178 struct nau7802_state
*st
= iio_priv(indio_dev
);
181 status
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
185 if (!(status
& NAU7802_PUCTRL_CR_BIT
))
188 if (nau7802_read_conversion(st
) < 0)
192 * Because there is actually only one ADC for both channels, we have to
193 * wait for enough conversions to happen before getting a significant
194 * value when changing channels and the values are far apart.
196 if (st
->conversion_count
< NAU7802_MIN_CONVERSIONS
)
197 st
->conversion_count
++;
198 if (st
->conversion_count
>= NAU7802_MIN_CONVERSIONS
)
199 complete(&st
->value_ok
);
204 static int nau7802_read_irq(struct iio_dev
*indio_dev
,
205 struct iio_chan_spec
const *chan
,
208 struct nau7802_state
*st
= iio_priv(indio_dev
);
211 reinit_completion(&st
->value_ok
);
212 enable_irq(st
->client
->irq
);
216 /* read registers to ensure we flush everything */
217 ret
= nau7802_read_conversion(st
);
219 goto read_chan_info_failure
;
221 /* Wait for a conversion to finish */
222 ret
= wait_for_completion_interruptible_timeout(&st
->value_ok
,
223 msecs_to_jiffies(1000));
228 goto read_chan_info_failure
;
230 disable_irq(st
->client
->irq
);
232 *val
= st
->last_value
;
236 read_chan_info_failure
:
237 disable_irq(st
->client
->irq
);
242 static int nau7802_read_poll(struct iio_dev
*indio_dev
,
243 struct iio_chan_spec
const *chan
,
246 struct nau7802_state
*st
= iio_priv(indio_dev
);
251 /* read registers to ensure we flush everything */
252 ret
= nau7802_read_conversion(st
);
257 * Because there is actually only one ADC for both channels, we have to
258 * wait for enough conversions to happen before getting a significant
259 * value when changing channels and the values are far appart.
262 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
266 while (!(ret
& NAU7802_PUCTRL_CR_BIT
)) {
267 if (st
->sample_rate
!= NAU7802_SAMP_FREQ_320
)
271 ret
= i2c_smbus_read_byte_data(st
->client
,
277 ret
= nau7802_read_conversion(st
);
280 if (st
->conversion_count
< NAU7802_MIN_CONVERSIONS
)
281 st
->conversion_count
++;
282 } while (st
->conversion_count
< NAU7802_MIN_CONVERSIONS
);
284 *val
= st
->last_value
;
289 static int nau7802_read_raw(struct iio_dev
*indio_dev
,
290 struct iio_chan_spec
const *chan
,
291 int *val
, int *val2
, long mask
)
293 struct nau7802_state
*st
= iio_priv(indio_dev
);
297 case IIO_CHAN_INFO_RAW
:
298 mutex_lock(&st
->lock
);
300 * Select the channel to use
301 * - Channel 1 is value 0 in the CHS register
302 * - Channel 2 is value 1 in the CHS register
304 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_CTRL2
);
306 mutex_unlock(&st
->lock
);
310 if (((ret
& NAU7802_CTRL2_CHS_BIT
) && !chan
->channel
) ||
311 (!(ret
& NAU7802_CTRL2_CHS_BIT
) &&
313 st
->conversion_count
= 0;
314 ret
= i2c_smbus_write_byte_data(st
->client
,
316 NAU7802_CTRL2_CHS(chan
->channel
) |
317 NAU7802_CTRL2_CRS(st
->sample_rate
));
320 mutex_unlock(&st
->lock
);
326 ret
= nau7802_read_irq(indio_dev
, chan
, val
);
328 ret
= nau7802_read_poll(indio_dev
, chan
, val
);
330 mutex_unlock(&st
->lock
);
333 case IIO_CHAN_INFO_SCALE
:
334 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_CTRL1
);
339 * We have 24 bits of signed data, that means 23 bits of data
343 *val2
= 23 + (ret
& NAU7802_CTRL1_GAINS_BITS
);
345 return IIO_VAL_FRACTIONAL_LOG2
;
347 case IIO_CHAN_INFO_SAMP_FREQ
:
348 *val
= nau7802_sample_freq_avail
[st
->sample_rate
];
359 static int nau7802_write_raw(struct iio_dev
*indio_dev
,
360 struct iio_chan_spec
const *chan
,
361 int val
, int val2
, long mask
)
363 struct nau7802_state
*st
= iio_priv(indio_dev
);
367 case IIO_CHAN_INFO_SCALE
:
368 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
369 if (val2
== st
->scale_avail
[i
])
370 return nau7802_set_gain(st
, i
);
374 case IIO_CHAN_INFO_SAMP_FREQ
:
375 for (i
= 0; i
< ARRAY_SIZE(nau7802_sample_freq_avail
); i
++)
376 if (val
== nau7802_sample_freq_avail
[i
]) {
377 mutex_lock(&st
->lock
);
379 st
->conversion_count
= 0;
380 ret
= i2c_smbus_write_byte_data(st
->client
,
382 NAU7802_CTRL2_CRS(st
->sample_rate
));
383 mutex_unlock(&st
->lock
);
396 static int nau7802_write_raw_get_fmt(struct iio_dev
*indio_dev
,
397 struct iio_chan_spec
const *chan
,
400 return IIO_VAL_INT_PLUS_NANO
;
403 static const struct iio_info nau7802_info
= {
404 .read_raw
= &nau7802_read_raw
,
405 .write_raw
= &nau7802_write_raw
,
406 .write_raw_get_fmt
= nau7802_write_raw_get_fmt
,
407 .attrs
= &nau7802_attribute_group
,
410 static int nau7802_probe(struct i2c_client
*client
,
411 const struct i2c_device_id
*id
)
413 struct iio_dev
*indio_dev
;
414 struct nau7802_state
*st
;
415 struct device_node
*np
= client
->dev
.of_node
;
420 if (!client
->dev
.of_node
) {
421 dev_err(&client
->dev
, "No device tree node available.\n");
425 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*st
));
426 if (indio_dev
== NULL
)
429 st
= iio_priv(indio_dev
);
431 i2c_set_clientdata(client
, indio_dev
);
433 indio_dev
->name
= dev_name(&client
->dev
);
434 indio_dev
->modes
= INDIO_DIRECT_MODE
;
435 indio_dev
->info
= &nau7802_info
;
439 /* Reset the device */
440 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
,
441 NAU7802_PUCTRL_RR_BIT
);
445 /* Enter normal operation mode */
446 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
,
447 NAU7802_PUCTRL_PUD_BIT
);
452 * After about 200 usecs, the device should be ready and then
453 * the Power Up bit will be set to 1. If not, wait for it.
456 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
459 if (!(ret
& NAU7802_PUCTRL_PUR_BIT
))
462 of_property_read_u32(np
, "nuvoton,vldo", &tmp
);
465 data
= NAU7802_PUCTRL_PUD_BIT
| NAU7802_PUCTRL_PUA_BIT
|
466 NAU7802_PUCTRL_CS_BIT
;
468 data
|= NAU7802_PUCTRL_AVDDS_BIT
;
470 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
, data
);
473 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_ADC_CTRL
, 0x30);
478 data
= NAU7802_CTRL1_VLDO((4500 - tmp
) / 300);
479 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_CTRL1
,
485 /* Populate available ADC input ranges */
486 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
487 st
->scale_avail
[i
] = (((u64
)st
->vref_mv
) * 1000000000ULL)
490 init_completion(&st
->value_ok
);
493 * The ADC fires continuously and we can't do anything about
494 * it. So we need to have the IRQ disabled by default, and we
495 * will enable them back when we will need them..
498 ret
= request_threaded_irq(client
->irq
,
501 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
502 client
->dev
.driver
->name
,
506 * What may happen here is that our IRQ controller is
507 * not able to get level interrupt but this is required
508 * by this ADC as when going over 40 sample per second,
509 * the interrupt line may stay high between conversions.
510 * So, we continue no matter what but we switch to
513 dev_info(&client
->dev
,
514 "Failed to allocate IRQ, using polling mode\n");
517 disable_irq(client
->irq
);
522 * We are polling, use the fastest sample rate by
525 st
->sample_rate
= NAU7802_SAMP_FREQ_320
;
526 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_CTRL2
,
527 NAU7802_CTRL2_CRS(st
->sample_rate
));
532 /* Setup the ADC channels available on the board */
533 indio_dev
->num_channels
= ARRAY_SIZE(nau7802_chan_array
);
534 indio_dev
->channels
= nau7802_chan_array
;
536 mutex_init(&st
->lock
);
537 mutex_init(&st
->data_lock
);
539 ret
= iio_device_register(indio_dev
);
541 dev_err(&client
->dev
, "Couldn't register the device.\n");
542 goto error_device_register
;
547 error_device_register
:
548 mutex_destroy(&st
->lock
);
549 mutex_destroy(&st
->data_lock
);
552 free_irq(client
->irq
, indio_dev
);
557 static int nau7802_remove(struct i2c_client
*client
)
559 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
560 struct nau7802_state
*st
= iio_priv(indio_dev
);
562 iio_device_unregister(indio_dev
);
563 mutex_destroy(&st
->lock
);
564 mutex_destroy(&st
->data_lock
);
566 free_irq(client
->irq
, indio_dev
);
571 static const struct i2c_device_id nau7802_i2c_id
[] = {
575 MODULE_DEVICE_TABLE(i2c
, nau7802_i2c_id
);
577 static const struct of_device_id nau7802_dt_ids
[] = {
578 { .compatible
= "nuvoton,nau7802" },
581 MODULE_DEVICE_TABLE(of
, nau7802_dt_ids
);
583 static struct i2c_driver nau7802_driver
= {
584 .probe
= nau7802_probe
,
585 .remove
= nau7802_remove
,
586 .id_table
= nau7802_i2c_id
,
589 .of_match_table
= nau7802_dt_ids
,
593 module_i2c_driver(nau7802_driver
);
595 MODULE_LICENSE("GPL");
596 MODULE_DESCRIPTION("Nuvoton NAU7802 ADC Driver");
597 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
598 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");