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>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
20 #define NAU7802_REG_PUCTRL 0x00
21 #define NAU7802_PUCTRL_RR(x) (x << 0)
22 #define NAU7802_PUCTRL_RR_BIT NAU7802_PUCTRL_RR(1)
23 #define NAU7802_PUCTRL_PUD(x) (x << 1)
24 #define NAU7802_PUCTRL_PUD_BIT NAU7802_PUCTRL_PUD(1)
25 #define NAU7802_PUCTRL_PUA(x) (x << 2)
26 #define NAU7802_PUCTRL_PUA_BIT NAU7802_PUCTRL_PUA(1)
27 #define NAU7802_PUCTRL_PUR(x) (x << 3)
28 #define NAU7802_PUCTRL_PUR_BIT NAU7802_PUCTRL_PUR(1)
29 #define NAU7802_PUCTRL_CS(x) (x << 4)
30 #define NAU7802_PUCTRL_CS_BIT NAU7802_PUCTRL_CS(1)
31 #define NAU7802_PUCTRL_CR(x) (x << 5)
32 #define NAU7802_PUCTRL_CR_BIT NAU7802_PUCTRL_CR(1)
33 #define NAU7802_PUCTRL_AVDDS(x) (x << 7)
34 #define NAU7802_PUCTRL_AVDDS_BIT NAU7802_PUCTRL_AVDDS(1)
35 #define NAU7802_REG_CTRL1 0x01
36 #define NAU7802_CTRL1_VLDO(x) (x << 3)
37 #define NAU7802_CTRL1_GAINS(x) (x)
38 #define NAU7802_CTRL1_GAINS_BITS 0x07
39 #define NAU7802_REG_CTRL2 0x02
40 #define NAU7802_CTRL2_CHS(x) (x << 7)
41 #define NAU7802_CTRL2_CRS(x) (x << 4)
42 #define NAU7802_SAMP_FREQ_320 0x07
43 #define NAU7802_CTRL2_CHS_BIT NAU7802_CTRL2_CHS(1)
44 #define NAU7802_REG_ADC_B2 0x12
45 #define NAU7802_REG_ADC_B1 0x13
46 #define NAU7802_REG_ADC_B0 0x14
47 #define NAU7802_REG_ADC_CTRL 0x15
49 #define NAU7802_MIN_CONVERSIONS 6
51 struct nau7802_state
{
52 struct i2c_client
*client
;
55 struct mutex data_lock
;
61 struct completion value_ok
;
64 #define NAU7802_CHANNEL(chan) { \
65 .type = IIO_VOLTAGE, \
68 .scan_index = (chan), \
69 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
70 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
71 BIT(IIO_CHAN_INFO_SAMP_FREQ) \
74 static const struct iio_chan_spec nau7802_chan_array
[] = {
79 static const u16 nau7802_sample_freq_avail
[] = {10, 20, 40, 80,
82 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 40 80 320");
84 static struct attribute
*nau7802_attributes
[] = {
85 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
89 static const struct attribute_group nau7802_attribute_group
= {
90 .attrs
= nau7802_attributes
,
93 static int nau7802_set_gain(struct nau7802_state
*st
, int gain
)
97 mutex_lock(&st
->lock
);
98 st
->conversion_count
= 0;
100 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_CTRL1
);
102 goto nau7802_sysfs_set_gain_out
;
103 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_CTRL1
,
104 (ret
& (~NAU7802_CTRL1_GAINS_BITS
)) |
107 nau7802_sysfs_set_gain_out
:
108 mutex_unlock(&st
->lock
);
113 static int nau7802_read_conversion(struct nau7802_state
*st
)
117 mutex_lock(&st
->data_lock
);
118 data
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_ADC_B2
);
120 goto nau7802_read_conversion_out
;
121 st
->last_value
= data
<< 16;
123 data
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_ADC_B1
);
125 goto nau7802_read_conversion_out
;
126 st
->last_value
|= data
<< 8;
128 data
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_ADC_B0
);
130 goto nau7802_read_conversion_out
;
131 st
->last_value
|= data
;
133 st
->last_value
= sign_extend32(st
->last_value
, 23);
135 nau7802_read_conversion_out
:
136 mutex_unlock(&st
->data_lock
);
142 * Conversions are synchronised on the rising edge of NAU7802_PUCTRL_CS_BIT
144 static int nau7802_sync(struct nau7802_state
*st
)
148 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
151 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
,
152 ret
| NAU7802_PUCTRL_CS_BIT
);
157 static irqreturn_t
nau7802_eoc_trigger(int irq
, void *private)
159 struct iio_dev
*indio_dev
= private;
160 struct nau7802_state
*st
= iio_priv(indio_dev
);
163 status
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
167 if (!(status
& NAU7802_PUCTRL_CR_BIT
))
170 if (nau7802_read_conversion(st
) < 0)
174 * Because there is actually only one ADC for both channels, we have to
175 * wait for enough conversions to happen before getting a significant
176 * value when changing channels and the values are far apart.
178 if (st
->conversion_count
< NAU7802_MIN_CONVERSIONS
)
179 st
->conversion_count
++;
180 if (st
->conversion_count
>= NAU7802_MIN_CONVERSIONS
)
181 complete_all(&st
->value_ok
);
186 static int nau7802_read_irq(struct iio_dev
*indio_dev
,
187 struct iio_chan_spec
const *chan
,
190 struct nau7802_state
*st
= iio_priv(indio_dev
);
193 reinit_completion(&st
->value_ok
);
194 enable_irq(st
->client
->irq
);
198 /* read registers to ensure we flush everything */
199 ret
= nau7802_read_conversion(st
);
201 goto read_chan_info_failure
;
203 /* Wait for a conversion to finish */
204 ret
= wait_for_completion_interruptible_timeout(&st
->value_ok
,
205 msecs_to_jiffies(1000));
210 goto read_chan_info_failure
;
212 disable_irq(st
->client
->irq
);
214 *val
= st
->last_value
;
218 read_chan_info_failure
:
219 disable_irq(st
->client
->irq
);
224 static int nau7802_read_poll(struct iio_dev
*indio_dev
,
225 struct iio_chan_spec
const *chan
,
228 struct nau7802_state
*st
= iio_priv(indio_dev
);
233 /* read registers to ensure we flush everything */
234 ret
= nau7802_read_conversion(st
);
239 * Because there is actually only one ADC for both channels, we have to
240 * wait for enough conversions to happen before getting a significant
241 * value when changing channels and the values are far appart.
244 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
248 while (!(ret
& NAU7802_PUCTRL_CR_BIT
)) {
249 if (st
->sample_rate
!= NAU7802_SAMP_FREQ_320
)
253 ret
= i2c_smbus_read_byte_data(st
->client
,
259 ret
= nau7802_read_conversion(st
);
262 if (st
->conversion_count
< NAU7802_MIN_CONVERSIONS
)
263 st
->conversion_count
++;
264 } while (st
->conversion_count
< NAU7802_MIN_CONVERSIONS
);
266 *val
= st
->last_value
;
271 static int nau7802_read_raw(struct iio_dev
*indio_dev
,
272 struct iio_chan_spec
const *chan
,
273 int *val
, int *val2
, long mask
)
275 struct nau7802_state
*st
= iio_priv(indio_dev
);
279 case IIO_CHAN_INFO_RAW
:
280 mutex_lock(&st
->lock
);
282 * Select the channel to use
283 * - Channel 1 is value 0 in the CHS register
284 * - Channel 2 is value 1 in the CHS register
286 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_CTRL2
);
288 mutex_unlock(&st
->lock
);
292 if (((ret
& NAU7802_CTRL2_CHS_BIT
) && !chan
->channel
) ||
293 (!(ret
& NAU7802_CTRL2_CHS_BIT
) &&
295 st
->conversion_count
= 0;
296 ret
= i2c_smbus_write_byte_data(st
->client
,
298 NAU7802_CTRL2_CHS(chan
->channel
) |
299 NAU7802_CTRL2_CRS(st
->sample_rate
));
302 mutex_unlock(&st
->lock
);
308 ret
= nau7802_read_irq(indio_dev
, chan
, val
);
310 ret
= nau7802_read_poll(indio_dev
, chan
, val
);
312 mutex_unlock(&st
->lock
);
315 case IIO_CHAN_INFO_SCALE
:
316 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_CTRL1
);
321 * We have 24 bits of signed data, that means 23 bits of data
325 *val2
= 23 + (ret
& NAU7802_CTRL1_GAINS_BITS
);
327 return IIO_VAL_FRACTIONAL_LOG2
;
329 case IIO_CHAN_INFO_SAMP_FREQ
:
330 *val
= nau7802_sample_freq_avail
[st
->sample_rate
];
341 static int nau7802_write_raw(struct iio_dev
*indio_dev
,
342 struct iio_chan_spec
const *chan
,
343 int val
, int val2
, long mask
)
345 struct nau7802_state
*st
= iio_priv(indio_dev
);
349 case IIO_CHAN_INFO_SCALE
:
350 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
351 if (val2
== st
->scale_avail
[i
])
352 return nau7802_set_gain(st
, i
);
356 case IIO_CHAN_INFO_SAMP_FREQ
:
357 for (i
= 0; i
< ARRAY_SIZE(nau7802_sample_freq_avail
); i
++)
358 if (val
== nau7802_sample_freq_avail
[i
]) {
359 mutex_lock(&st
->lock
);
361 st
->conversion_count
= 0;
362 ret
= i2c_smbus_write_byte_data(st
->client
,
364 NAU7802_CTRL2_CRS(st
->sample_rate
));
365 mutex_unlock(&st
->lock
);
378 static int nau7802_write_raw_get_fmt(struct iio_dev
*indio_dev
,
379 struct iio_chan_spec
const *chan
,
382 return IIO_VAL_INT_PLUS_NANO
;
385 static const struct iio_info nau7802_info
= {
386 .driver_module
= THIS_MODULE
,
387 .read_raw
= &nau7802_read_raw
,
388 .write_raw
= &nau7802_write_raw
,
389 .write_raw_get_fmt
= nau7802_write_raw_get_fmt
,
390 .attrs
= &nau7802_attribute_group
,
393 static int nau7802_probe(struct i2c_client
*client
,
394 const struct i2c_device_id
*id
)
396 struct iio_dev
*indio_dev
;
397 struct nau7802_state
*st
;
398 struct device_node
*np
= client
->dev
.of_node
;
403 if (!client
->dev
.of_node
) {
404 dev_err(&client
->dev
, "No device tree node available.\n");
408 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*st
));
409 if (indio_dev
== NULL
)
412 st
= iio_priv(indio_dev
);
414 i2c_set_clientdata(client
, indio_dev
);
416 indio_dev
->dev
.parent
= &client
->dev
;
417 indio_dev
->name
= dev_name(&client
->dev
);
418 indio_dev
->modes
= INDIO_DIRECT_MODE
;
419 indio_dev
->info
= &nau7802_info
;
423 /* Reset the device */
424 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
,
425 NAU7802_PUCTRL_RR_BIT
);
429 /* Enter normal operation mode */
430 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
,
431 NAU7802_PUCTRL_PUD_BIT
);
436 * After about 200 usecs, the device should be ready and then
437 * the Power Up bit will be set to 1. If not, wait for it.
440 ret
= i2c_smbus_read_byte_data(st
->client
, NAU7802_REG_PUCTRL
);
443 if (!(ret
& NAU7802_PUCTRL_PUR_BIT
))
446 of_property_read_u32(np
, "nuvoton,vldo", &tmp
);
449 data
= NAU7802_PUCTRL_PUD_BIT
| NAU7802_PUCTRL_PUA_BIT
|
450 NAU7802_PUCTRL_CS_BIT
;
452 data
|= NAU7802_PUCTRL_AVDDS_BIT
;
454 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_PUCTRL
, data
);
457 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_ADC_CTRL
, 0x30);
462 data
= NAU7802_CTRL1_VLDO((4500 - tmp
) / 300);
463 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_CTRL1
,
469 /* Populate available ADC input ranges */
470 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
471 st
->scale_avail
[i
] = (((u64
)st
->vref_mv
) * 1000000000ULL)
474 init_completion(&st
->value_ok
);
477 * The ADC fires continuously and we can't do anything about
478 * it. So we need to have the IRQ disabled by default, and we
479 * will enable them back when we will need them..
482 ret
= request_threaded_irq(client
->irq
,
485 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
486 client
->dev
.driver
->name
,
490 * What may happen here is that our IRQ controller is
491 * not able to get level interrupt but this is required
492 * by this ADC as when going over 40 sample per second,
493 * the interrupt line may stay high between conversions.
494 * So, we continue no matter what but we switch to
497 dev_info(&client
->dev
,
498 "Failed to allocate IRQ, using polling mode\n");
501 disable_irq(client
->irq
);
506 * We are polling, use the fastest sample rate by
509 st
->sample_rate
= NAU7802_SAMP_FREQ_320
;
510 ret
= i2c_smbus_write_byte_data(st
->client
, NAU7802_REG_CTRL2
,
511 NAU7802_CTRL2_CRS(st
->sample_rate
));
516 /* Setup the ADC channels available on the board */
517 indio_dev
->num_channels
= ARRAY_SIZE(nau7802_chan_array
);
518 indio_dev
->channels
= nau7802_chan_array
;
520 mutex_init(&st
->lock
);
521 mutex_init(&st
->data_lock
);
523 ret
= iio_device_register(indio_dev
);
525 dev_err(&client
->dev
, "Couldn't register the device.\n");
526 goto error_device_register
;
531 error_device_register
:
532 mutex_destroy(&st
->lock
);
533 mutex_destroy(&st
->data_lock
);
536 free_irq(client
->irq
, indio_dev
);
541 static int nau7802_remove(struct i2c_client
*client
)
543 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
544 struct nau7802_state
*st
= iio_priv(indio_dev
);
546 iio_device_unregister(indio_dev
);
547 mutex_destroy(&st
->lock
);
548 mutex_destroy(&st
->data_lock
);
550 free_irq(client
->irq
, indio_dev
);
555 static const struct i2c_device_id nau7802_i2c_id
[] = {
559 MODULE_DEVICE_TABLE(i2c
, nau7802_i2c_id
);
561 static const struct of_device_id nau7802_dt_ids
[] = {
562 { .compatible
= "nuvoton,nau7802" },
565 MODULE_DEVICE_TABLE(of
, nau7802_dt_ids
);
567 static struct i2c_driver nau7802_driver
= {
568 .probe
= nau7802_probe
,
569 .remove
= nau7802_remove
,
570 .id_table
= nau7802_i2c_id
,
573 .of_match_table
= nau7802_dt_ids
,
577 module_i2c_driver(nau7802_driver
);
579 MODULE_LICENSE("GPL");
580 MODULE_DESCRIPTION("Nuvoton NAU7802 ADC Driver");
581 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
582 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");