nau7802.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * Driver for the Nuvoton NAU7802 ADC
  3. *
  4. * Copyright 2013 Free Electrons
  5. *
  6. * Licensed under the GPLv2 or later.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/i2c.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/wait.h>
  13. #include <linux/log2.h>
  14. #include <linux/of.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. #define NAU7802_REG_PUCTRL 0x00
  18. #define NAU7802_PUCTRL_RR(x) (x << 0)
  19. #define NAU7802_PUCTRL_RR_BIT NAU7802_PUCTRL_RR(1)
  20. #define NAU7802_PUCTRL_PUD(x) (x << 1)
  21. #define NAU7802_PUCTRL_PUD_BIT NAU7802_PUCTRL_PUD(1)
  22. #define NAU7802_PUCTRL_PUA(x) (x << 2)
  23. #define NAU7802_PUCTRL_PUA_BIT NAU7802_PUCTRL_PUA(1)
  24. #define NAU7802_PUCTRL_PUR(x) (x << 3)
  25. #define NAU7802_PUCTRL_PUR_BIT NAU7802_PUCTRL_PUR(1)
  26. #define NAU7802_PUCTRL_CS(x) (x << 4)
  27. #define NAU7802_PUCTRL_CS_BIT NAU7802_PUCTRL_CS(1)
  28. #define NAU7802_PUCTRL_CR(x) (x << 5)
  29. #define NAU7802_PUCTRL_CR_BIT NAU7802_PUCTRL_CR(1)
  30. #define NAU7802_PUCTRL_AVDDS(x) (x << 7)
  31. #define NAU7802_PUCTRL_AVDDS_BIT NAU7802_PUCTRL_AVDDS(1)
  32. #define NAU7802_REG_CTRL1 0x01
  33. #define NAU7802_CTRL1_VLDO(x) (x << 3)
  34. #define NAU7802_CTRL1_GAINS(x) (x)
  35. #define NAU7802_CTRL1_GAINS_BITS 0x07
  36. #define NAU7802_REG_CTRL2 0x02
  37. #define NAU7802_CTRL2_CHS(x) (x << 7)
  38. #define NAU7802_CTRL2_CRS(x) (x << 4)
  39. #define NAU7802_SAMP_FREQ_320 0x07
  40. #define NAU7802_CTRL2_CHS_BIT NAU7802_CTRL2_CHS(1)
  41. #define NAU7802_REG_ADC_B2 0x12
  42. #define NAU7802_REG_ADC_B1 0x13
  43. #define NAU7802_REG_ADC_B0 0x14
  44. #define NAU7802_REG_ADC_CTRL 0x15
  45. #define NAU7802_MIN_CONVERSIONS 6
  46. struct nau7802_state {
  47. struct i2c_client *client;
  48. s32 last_value;
  49. struct mutex lock;
  50. struct mutex data_lock;
  51. u32 vref_mv;
  52. u32 conversion_count;
  53. u32 min_conversions;
  54. u8 sample_rate;
  55. u32 scale_avail[8];
  56. struct completion value_ok;
  57. };
  58. #define NAU7802_CHANNEL(chan) { \
  59. .type = IIO_VOLTAGE, \
  60. .indexed = 1, \
  61. .channel = (chan), \
  62. .scan_index = (chan), \
  63. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  64. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  65. BIT(IIO_CHAN_INFO_SAMP_FREQ) \
  66. }
  67. static const struct iio_chan_spec nau7802_chan_array[] = {
  68. NAU7802_CHANNEL(0),
  69. NAU7802_CHANNEL(1),
  70. };
  71. static const u16 nau7802_sample_freq_avail[] = {10, 20, 40, 80,
  72. 10, 10, 10, 320};
  73. static ssize_t nau7802_show_scales(struct device *dev,
  74. struct device_attribute *attr, char *buf)
  75. {
  76. struct nau7802_state *st = iio_priv(dev_to_iio_dev(dev));
  77. int i, len = 0;
  78. for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
  79. len += scnprintf(buf + len, PAGE_SIZE - len, "0.%09d ",
  80. st->scale_avail[i]);
  81. buf[len-1] = '\n';
  82. return len;
  83. }
  84. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 40 80 320");
  85. static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO, nau7802_show_scales,
  86. NULL, 0);
  87. static struct attribute *nau7802_attributes[] = {
  88. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  89. &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
  90. NULL
  91. };
  92. static const struct attribute_group nau7802_attribute_group = {
  93. .attrs = nau7802_attributes,
  94. };
  95. static int nau7802_set_gain(struct nau7802_state *st, int gain)
  96. {
  97. int ret;
  98. mutex_lock(&st->lock);
  99. st->conversion_count = 0;
  100. ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL1);
  101. if (ret < 0)
  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)) |
  105. gain);
  106. nau7802_sysfs_set_gain_out:
  107. mutex_unlock(&st->lock);
  108. return ret;
  109. }
  110. static int nau7802_read_conversion(struct nau7802_state *st)
  111. {
  112. int data;
  113. mutex_lock(&st->data_lock);
  114. data = i2c_smbus_read_byte_data(st->client, NAU7802_REG_ADC_B2);
  115. if (data < 0)
  116. goto nau7802_read_conversion_out;
  117. st->last_value = data << 16;
  118. data = i2c_smbus_read_byte_data(st->client, NAU7802_REG_ADC_B1);
  119. if (data < 0)
  120. goto nau7802_read_conversion_out;
  121. st->last_value |= data << 8;
  122. data = i2c_smbus_read_byte_data(st->client, NAU7802_REG_ADC_B0);
  123. if (data < 0)
  124. goto nau7802_read_conversion_out;
  125. st->last_value |= data;
  126. st->last_value = sign_extend32(st->last_value, 23);
  127. nau7802_read_conversion_out:
  128. mutex_unlock(&st->data_lock);
  129. return data;
  130. }
  131. /*
  132. * Conversions are synchronised on the rising edge of NAU7802_PUCTRL_CS_BIT
  133. */
  134. static int nau7802_sync(struct nau7802_state *st)
  135. {
  136. int ret;
  137. ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL);
  138. if (ret < 0)
  139. return ret;
  140. ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL,
  141. ret | NAU7802_PUCTRL_CS_BIT);
  142. return ret;
  143. }
  144. static irqreturn_t nau7802_eoc_trigger(int irq, void *private)
  145. {
  146. struct iio_dev *indio_dev = private;
  147. struct nau7802_state *st = iio_priv(indio_dev);
  148. int status;
  149. status = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL);
  150. if (status < 0)
  151. return IRQ_HANDLED;
  152. if (!(status & NAU7802_PUCTRL_CR_BIT))
  153. return IRQ_NONE;
  154. if (nau7802_read_conversion(st) < 0)
  155. return IRQ_HANDLED;
  156. /*
  157. * Because there is actually only one ADC for both channels, we have to
  158. * wait for enough conversions to happen before getting a significant
  159. * value when changing channels and the values are far apart.
  160. */
  161. if (st->conversion_count < NAU7802_MIN_CONVERSIONS)
  162. st->conversion_count++;
  163. if (st->conversion_count >= NAU7802_MIN_CONVERSIONS)
  164. complete(&st->value_ok);
  165. return IRQ_HANDLED;
  166. }
  167. static int nau7802_read_irq(struct iio_dev *indio_dev,
  168. struct iio_chan_spec const *chan,
  169. int *val)
  170. {
  171. struct nau7802_state *st = iio_priv(indio_dev);
  172. int ret;
  173. reinit_completion(&st->value_ok);
  174. enable_irq(st->client->irq);
  175. nau7802_sync(st);
  176. /* read registers to ensure we flush everything */
  177. ret = nau7802_read_conversion(st);
  178. if (ret < 0)
  179. goto read_chan_info_failure;
  180. /* Wait for a conversion to finish */
  181. ret = wait_for_completion_interruptible_timeout(&st->value_ok,
  182. msecs_to_jiffies(1000));
  183. if (ret == 0)
  184. ret = -ETIMEDOUT;
  185. if (ret < 0)
  186. goto read_chan_info_failure;
  187. disable_irq(st->client->irq);
  188. *val = st->last_value;
  189. return IIO_VAL_INT;
  190. read_chan_info_failure:
  191. disable_irq(st->client->irq);
  192. return ret;
  193. }
  194. static int nau7802_read_poll(struct iio_dev *indio_dev,
  195. struct iio_chan_spec const *chan,
  196. int *val)
  197. {
  198. struct nau7802_state *st = iio_priv(indio_dev);
  199. int ret;
  200. nau7802_sync(st);
  201. /* read registers to ensure we flush everything */
  202. ret = nau7802_read_conversion(st);
  203. if (ret < 0)
  204. return ret;
  205. /*
  206. * Because there is actually only one ADC for both channels, we have to
  207. * wait for enough conversions to happen before getting a significant
  208. * value when changing channels and the values are far appart.
  209. */
  210. do {
  211. ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL);
  212. if (ret < 0)
  213. return ret;
  214. while (!(ret & NAU7802_PUCTRL_CR_BIT)) {
  215. if (st->sample_rate != NAU7802_SAMP_FREQ_320)
  216. msleep(20);
  217. else
  218. mdelay(4);
  219. ret = i2c_smbus_read_byte_data(st->client,
  220. NAU7802_REG_PUCTRL);
  221. if (ret < 0)
  222. return ret;
  223. }
  224. ret = nau7802_read_conversion(st);
  225. if (ret < 0)
  226. return ret;
  227. if (st->conversion_count < NAU7802_MIN_CONVERSIONS)
  228. st->conversion_count++;
  229. } while (st->conversion_count < NAU7802_MIN_CONVERSIONS);
  230. *val = st->last_value;
  231. return IIO_VAL_INT;
  232. }
  233. static int nau7802_read_raw(struct iio_dev *indio_dev,
  234. struct iio_chan_spec const *chan,
  235. int *val, int *val2, long mask)
  236. {
  237. struct nau7802_state *st = iio_priv(indio_dev);
  238. int ret;
  239. switch (mask) {
  240. case IIO_CHAN_INFO_RAW:
  241. mutex_lock(&st->lock);
  242. /*
  243. * Select the channel to use
  244. * - Channel 1 is value 0 in the CHS register
  245. * - Channel 2 is value 1 in the CHS register
  246. */
  247. ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL2);
  248. if (ret < 0) {
  249. mutex_unlock(&st->lock);
  250. return ret;
  251. }
  252. if (((ret & NAU7802_CTRL2_CHS_BIT) && !chan->channel) ||
  253. (!(ret & NAU7802_CTRL2_CHS_BIT) &&
  254. chan->channel)) {
  255. st->conversion_count = 0;
  256. ret = i2c_smbus_write_byte_data(st->client,
  257. NAU7802_REG_CTRL2,
  258. NAU7802_CTRL2_CHS(chan->channel) |
  259. NAU7802_CTRL2_CRS(st->sample_rate));
  260. if (ret < 0) {
  261. mutex_unlock(&st->lock);
  262. return ret;
  263. }
  264. }
  265. if (st->client->irq)
  266. ret = nau7802_read_irq(indio_dev, chan, val);
  267. else
  268. ret = nau7802_read_poll(indio_dev, chan, val);
  269. mutex_unlock(&st->lock);
  270. return ret;
  271. case IIO_CHAN_INFO_SCALE:
  272. ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL1);
  273. if (ret < 0)
  274. return ret;
  275. /*
  276. * We have 24 bits of signed data, that means 23 bits of data
  277. * plus the sign bit
  278. */
  279. *val = st->vref_mv;
  280. *val2 = 23 + (ret & NAU7802_CTRL1_GAINS_BITS);
  281. return IIO_VAL_FRACTIONAL_LOG2;
  282. case IIO_CHAN_INFO_SAMP_FREQ:
  283. *val = nau7802_sample_freq_avail[st->sample_rate];
  284. *val2 = 0;
  285. return IIO_VAL_INT;
  286. default:
  287. break;
  288. }
  289. return -EINVAL;
  290. }
  291. static int nau7802_write_raw(struct iio_dev *indio_dev,
  292. struct iio_chan_spec const *chan,
  293. int val, int val2, long mask)
  294. {
  295. struct nau7802_state *st = iio_priv(indio_dev);
  296. int i, ret;
  297. switch (mask) {
  298. case IIO_CHAN_INFO_SCALE:
  299. for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
  300. if (val2 == st->scale_avail[i])
  301. return nau7802_set_gain(st, i);
  302. break;
  303. case IIO_CHAN_INFO_SAMP_FREQ:
  304. for (i = 0; i < ARRAY_SIZE(nau7802_sample_freq_avail); i++)
  305. if (val == nau7802_sample_freq_avail[i]) {
  306. mutex_lock(&st->lock);
  307. st->sample_rate = i;
  308. st->conversion_count = 0;
  309. ret = i2c_smbus_write_byte_data(st->client,
  310. NAU7802_REG_CTRL2,
  311. NAU7802_CTRL2_CRS(st->sample_rate));
  312. mutex_unlock(&st->lock);
  313. return ret;
  314. }
  315. break;
  316. default:
  317. break;
  318. }
  319. return -EINVAL;
  320. }
  321. static int nau7802_write_raw_get_fmt(struct iio_dev *indio_dev,
  322. struct iio_chan_spec const *chan,
  323. long mask)
  324. {
  325. return IIO_VAL_INT_PLUS_NANO;
  326. }
  327. static const struct iio_info nau7802_info = {
  328. .driver_module = THIS_MODULE,
  329. .read_raw = &nau7802_read_raw,
  330. .write_raw = &nau7802_write_raw,
  331. .write_raw_get_fmt = nau7802_write_raw_get_fmt,
  332. .attrs = &nau7802_attribute_group,
  333. };
  334. static int nau7802_probe(struct i2c_client *client,
  335. const struct i2c_device_id *id)
  336. {
  337. struct iio_dev *indio_dev;
  338. struct nau7802_state *st;
  339. struct device_node *np = client->dev.of_node;
  340. int i, ret;
  341. u8 data;
  342. u32 tmp = 0;
  343. if (!client->dev.of_node) {
  344. dev_err(&client->dev, "No device tree node available.\n");
  345. return -EINVAL;
  346. }
  347. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
  348. if (indio_dev == NULL)
  349. return -ENOMEM;
  350. st = iio_priv(indio_dev);
  351. i2c_set_clientdata(client, indio_dev);
  352. indio_dev->dev.parent = &client->dev;
  353. indio_dev->dev.of_node = client->dev.of_node;
  354. indio_dev->name = dev_name(&client->dev);
  355. indio_dev->modes = INDIO_DIRECT_MODE;
  356. indio_dev->info = &nau7802_info;
  357. st->client = client;
  358. /* Reset the device */
  359. ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL,
  360. NAU7802_PUCTRL_RR_BIT);
  361. if (ret < 0)
  362. return ret;
  363. /* Enter normal operation mode */
  364. ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL,
  365. NAU7802_PUCTRL_PUD_BIT);
  366. if (ret < 0)
  367. return ret;
  368. /*
  369. * After about 200 usecs, the device should be ready and then
  370. * the Power Up bit will be set to 1. If not, wait for it.
  371. */
  372. udelay(210);
  373. ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL);
  374. if (ret < 0)
  375. return ret;
  376. if (!(ret & NAU7802_PUCTRL_PUR_BIT))
  377. return ret;
  378. of_property_read_u32(np, "nuvoton,vldo", &tmp);
  379. st->vref_mv = tmp;
  380. data = NAU7802_PUCTRL_PUD_BIT | NAU7802_PUCTRL_PUA_BIT |
  381. NAU7802_PUCTRL_CS_BIT;
  382. if (tmp >= 2400)
  383. data |= NAU7802_PUCTRL_AVDDS_BIT;
  384. ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL, data);
  385. if (ret < 0)
  386. return ret;
  387. ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_ADC_CTRL, 0x30);
  388. if (ret < 0)
  389. return ret;
  390. if (tmp >= 2400) {
  391. data = NAU7802_CTRL1_VLDO((4500 - tmp) / 300);
  392. ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL1,
  393. data);
  394. if (ret < 0)
  395. return ret;
  396. }
  397. /* Populate available ADC input ranges */
  398. for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
  399. st->scale_avail[i] = (((u64)st->vref_mv) * 1000000000ULL)
  400. >> (23 + i);
  401. init_completion(&st->value_ok);
  402. /*
  403. * The ADC fires continuously and we can't do anything about
  404. * it. So we need to have the IRQ disabled by default, and we
  405. * will enable them back when we will need them..
  406. */
  407. if (client->irq) {
  408. ret = request_threaded_irq(client->irq,
  409. NULL,
  410. nau7802_eoc_trigger,
  411. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  412. client->dev.driver->name,
  413. indio_dev);
  414. if (ret) {
  415. /*
  416. * What may happen here is that our IRQ controller is
  417. * not able to get level interrupt but this is required
  418. * by this ADC as when going over 40 sample per second,
  419. * the interrupt line may stay high between conversions.
  420. * So, we continue no matter what but we switch to
  421. * polling mode.
  422. */
  423. dev_info(&client->dev,
  424. "Failed to allocate IRQ, using polling mode\n");
  425. client->irq = 0;
  426. } else
  427. disable_irq(client->irq);
  428. }
  429. if (!client->irq) {
  430. /*
  431. * We are polling, use the fastest sample rate by
  432. * default
  433. */
  434. st->sample_rate = NAU7802_SAMP_FREQ_320;
  435. ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL2,
  436. NAU7802_CTRL2_CRS(st->sample_rate));
  437. if (ret)
  438. goto error_free_irq;
  439. }
  440. /* Setup the ADC channels available on the board */
  441. indio_dev->num_channels = ARRAY_SIZE(nau7802_chan_array);
  442. indio_dev->channels = nau7802_chan_array;
  443. mutex_init(&st->lock);
  444. mutex_init(&st->data_lock);
  445. ret = iio_device_register(indio_dev);
  446. if (ret < 0) {
  447. dev_err(&client->dev, "Couldn't register the device.\n");
  448. goto error_device_register;
  449. }
  450. return 0;
  451. error_device_register:
  452. mutex_destroy(&st->lock);
  453. mutex_destroy(&st->data_lock);
  454. error_free_irq:
  455. if (client->irq)
  456. free_irq(client->irq, indio_dev);
  457. return ret;
  458. }
  459. static int nau7802_remove(struct i2c_client *client)
  460. {
  461. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  462. struct nau7802_state *st = iio_priv(indio_dev);
  463. iio_device_unregister(indio_dev);
  464. mutex_destroy(&st->lock);
  465. mutex_destroy(&st->data_lock);
  466. if (client->irq)
  467. free_irq(client->irq, indio_dev);
  468. return 0;
  469. }
  470. static const struct i2c_device_id nau7802_i2c_id[] = {
  471. { "nau7802", 0 },
  472. { }
  473. };
  474. MODULE_DEVICE_TABLE(i2c, nau7802_i2c_id);
  475. static const struct of_device_id nau7802_dt_ids[] = {
  476. { .compatible = "nuvoton,nau7802" },
  477. {},
  478. };
  479. MODULE_DEVICE_TABLE(of, nau7802_dt_ids);
  480. static struct i2c_driver nau7802_driver = {
  481. .probe = nau7802_probe,
  482. .remove = nau7802_remove,
  483. .id_table = nau7802_i2c_id,
  484. .driver = {
  485. .name = "nau7802",
  486. .of_match_table = nau7802_dt_ids,
  487. },
  488. };
  489. module_i2c_driver(nau7802_driver);
  490. MODULE_LICENSE("GPL");
  491. MODULE_DESCRIPTION("Nuvoton NAU7802 ADC Driver");
  492. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  493. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");