nau7802.c 14 KB

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