nau7802.c 14 KB

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