hx711.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * HX711: analog to digital converter for weight sensor module
  3. *
  4. * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/property.h>
  22. #include <linux/slab.h>
  23. #include <linux/sched.h>
  24. #include <linux/delay.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/sysfs.h>
  27. #include <linux/iio/buffer.h>
  28. #include <linux/iio/trigger_consumer.h>
  29. #include <linux/iio/triggered_buffer.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/regulator/consumer.h>
  32. /* gain to pulse and scale conversion */
  33. #define HX711_GAIN_MAX 3
  34. struct hx711_gain_to_scale {
  35. int gain;
  36. int gain_pulse;
  37. int scale;
  38. int channel;
  39. };
  40. /*
  41. * .scale depends on AVDD which in turn is known as soon as the regulator
  42. * is available
  43. * therefore we set .scale in hx711_probe()
  44. *
  45. * channel A in documentation is channel 0 in source code
  46. * channel B in documentation is channel 1 in source code
  47. */
  48. static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX] = {
  49. { 128, 1, 0, 0 },
  50. { 32, 2, 0, 1 },
  51. { 64, 3, 0, 0 }
  52. };
  53. static int hx711_get_gain_to_pulse(int gain)
  54. {
  55. int i;
  56. for (i = 0; i < HX711_GAIN_MAX; i++)
  57. if (hx711_gain_to_scale[i].gain == gain)
  58. return hx711_gain_to_scale[i].gain_pulse;
  59. return 1;
  60. }
  61. static int hx711_get_gain_to_scale(int gain)
  62. {
  63. int i;
  64. for (i = 0; i < HX711_GAIN_MAX; i++)
  65. if (hx711_gain_to_scale[i].gain == gain)
  66. return hx711_gain_to_scale[i].scale;
  67. return 0;
  68. }
  69. static int hx711_get_scale_to_gain(int scale)
  70. {
  71. int i;
  72. for (i = 0; i < HX711_GAIN_MAX; i++)
  73. if (hx711_gain_to_scale[i].scale == scale)
  74. return hx711_gain_to_scale[i].gain;
  75. return -EINVAL;
  76. }
  77. struct hx711_data {
  78. struct device *dev;
  79. struct gpio_desc *gpiod_pd_sck;
  80. struct gpio_desc *gpiod_dout;
  81. struct regulator *reg_avdd;
  82. int gain_set; /* gain set on device */
  83. int gain_chan_a; /* gain for channel A */
  84. struct mutex lock;
  85. /*
  86. * triggered buffer
  87. * 2x32-bit channel + 64-bit timestamp
  88. */
  89. u32 buffer[4];
  90. /*
  91. * delay after a rising edge on SCK until the data is ready DOUT
  92. * this is dependent on the hx711 where the datasheet tells a
  93. * maximum value of 100 ns
  94. * but also on potential parasitic capacities on the wiring
  95. */
  96. u32 data_ready_delay_ns;
  97. u32 clock_frequency;
  98. };
  99. static int hx711_cycle(struct hx711_data *hx711_data)
  100. {
  101. unsigned long flags;
  102. /*
  103. * if preempted for more then 60us while PD_SCK is high:
  104. * hx711 is going in reset
  105. * ==> measuring is false
  106. */
  107. local_irq_save(flags);
  108. gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
  109. /*
  110. * wait until DOUT is ready
  111. * it turned out that parasitic capacities are extending the time
  112. * until DOUT has reached it's value
  113. */
  114. ndelay(hx711_data->data_ready_delay_ns);
  115. /*
  116. * here we are not waiting for 0.2 us as suggested by the datasheet,
  117. * because the oscilloscope showed in a test scenario
  118. * at least 1.15 us for PD_SCK high (T3 in datasheet)
  119. * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
  120. */
  121. gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
  122. local_irq_restore(flags);
  123. /*
  124. * make it a square wave for addressing cases with capacitance on
  125. * PC_SCK
  126. */
  127. ndelay(hx711_data->data_ready_delay_ns);
  128. /* sample as late as possible */
  129. return gpiod_get_value(hx711_data->gpiod_dout);
  130. }
  131. static int hx711_read(struct hx711_data *hx711_data)
  132. {
  133. int i, ret;
  134. int value = 0;
  135. int val = gpiod_get_value(hx711_data->gpiod_dout);
  136. /* we double check if it's really down */
  137. if (val)
  138. return -EIO;
  139. for (i = 0; i < 24; i++) {
  140. value <<= 1;
  141. ret = hx711_cycle(hx711_data);
  142. if (ret)
  143. value++;
  144. }
  145. value ^= 0x800000;
  146. for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
  147. hx711_cycle(hx711_data);
  148. return value;
  149. }
  150. static int hx711_wait_for_ready(struct hx711_data *hx711_data)
  151. {
  152. int i, val;
  153. /*
  154. * in some rare cases the reset takes quite a long time
  155. * especially when the channel is changed.
  156. * Allow up to one second for it
  157. */
  158. for (i = 0; i < 100; i++) {
  159. val = gpiod_get_value(hx711_data->gpiod_dout);
  160. if (!val)
  161. break;
  162. /* sleep at least 10 ms */
  163. msleep(10);
  164. }
  165. if (val)
  166. return -EIO;
  167. return 0;
  168. }
  169. static int hx711_reset(struct hx711_data *hx711_data)
  170. {
  171. int ret;
  172. int val = gpiod_get_value(hx711_data->gpiod_dout);
  173. if (val) {
  174. /*
  175. * an examination with the oszilloscope indicated
  176. * that the first value read after the reset is not stable
  177. * if we reset too short;
  178. * the shorter the reset cycle
  179. * the less reliable the first value after reset is;
  180. * there were no problems encountered with a value
  181. * of 10 ms or higher
  182. */
  183. gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
  184. msleep(10);
  185. gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
  186. ret = hx711_wait_for_ready(hx711_data);
  187. if (ret)
  188. return ret;
  189. /*
  190. * after a reset the gain is 128 so we do a dummy read
  191. * to set the gain for the next read
  192. */
  193. ret = hx711_read(hx711_data);
  194. if (ret < 0)
  195. return ret;
  196. /*
  197. * after a dummy read we need to wait vor readiness
  198. * for not mixing gain pulses with the clock
  199. */
  200. val = hx711_wait_for_ready(hx711_data);
  201. }
  202. return val;
  203. }
  204. static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
  205. {
  206. int ret;
  207. if (chan == 0) {
  208. if (hx711_data->gain_set == 32) {
  209. hx711_data->gain_set = hx711_data->gain_chan_a;
  210. ret = hx711_read(hx711_data);
  211. if (ret < 0)
  212. return ret;
  213. ret = hx711_wait_for_ready(hx711_data);
  214. if (ret)
  215. return ret;
  216. }
  217. } else {
  218. if (hx711_data->gain_set != 32) {
  219. hx711_data->gain_set = 32;
  220. ret = hx711_read(hx711_data);
  221. if (ret < 0)
  222. return ret;
  223. ret = hx711_wait_for_ready(hx711_data);
  224. if (ret)
  225. return ret;
  226. }
  227. }
  228. return 0;
  229. }
  230. static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
  231. {
  232. int ret;
  233. int val;
  234. /*
  235. * hx711_reset() must be called from here
  236. * because it could be calling hx711_read() by itself
  237. */
  238. if (hx711_reset(hx711_data)) {
  239. dev_err(hx711_data->dev, "reset failed!");
  240. return -EIO;
  241. }
  242. ret = hx711_set_gain_for_channel(hx711_data, chan);
  243. if (ret < 0)
  244. return ret;
  245. val = hx711_read(hx711_data);
  246. return val;
  247. }
  248. static int hx711_read_raw(struct iio_dev *indio_dev,
  249. const struct iio_chan_spec *chan,
  250. int *val, int *val2, long mask)
  251. {
  252. struct hx711_data *hx711_data = iio_priv(indio_dev);
  253. switch (mask) {
  254. case IIO_CHAN_INFO_RAW:
  255. mutex_lock(&hx711_data->lock);
  256. *val = hx711_reset_read(hx711_data, chan->channel);
  257. mutex_unlock(&hx711_data->lock);
  258. if (*val < 0)
  259. return *val;
  260. return IIO_VAL_INT;
  261. case IIO_CHAN_INFO_SCALE:
  262. *val = 0;
  263. mutex_lock(&hx711_data->lock);
  264. *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
  265. mutex_unlock(&hx711_data->lock);
  266. return IIO_VAL_INT_PLUS_NANO;
  267. default:
  268. return -EINVAL;
  269. }
  270. }
  271. static int hx711_write_raw(struct iio_dev *indio_dev,
  272. struct iio_chan_spec const *chan,
  273. int val,
  274. int val2,
  275. long mask)
  276. {
  277. struct hx711_data *hx711_data = iio_priv(indio_dev);
  278. int ret;
  279. int gain;
  280. switch (mask) {
  281. case IIO_CHAN_INFO_SCALE:
  282. /*
  283. * a scale greater than 1 mV per LSB is not possible
  284. * with the HX711, therefore val must be 0
  285. */
  286. if (val != 0)
  287. return -EINVAL;
  288. mutex_lock(&hx711_data->lock);
  289. gain = hx711_get_scale_to_gain(val2);
  290. if (gain < 0) {
  291. mutex_unlock(&hx711_data->lock);
  292. return gain;
  293. }
  294. if (gain != hx711_data->gain_set) {
  295. hx711_data->gain_set = gain;
  296. if (gain != 32)
  297. hx711_data->gain_chan_a = gain;
  298. ret = hx711_read(hx711_data);
  299. if (ret < 0) {
  300. mutex_unlock(&hx711_data->lock);
  301. return ret;
  302. }
  303. }
  304. mutex_unlock(&hx711_data->lock);
  305. return 0;
  306. default:
  307. return -EINVAL;
  308. }
  309. return 0;
  310. }
  311. static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
  312. struct iio_chan_spec const *chan,
  313. long mask)
  314. {
  315. return IIO_VAL_INT_PLUS_NANO;
  316. }
  317. static irqreturn_t hx711_trigger(int irq, void *p)
  318. {
  319. struct iio_poll_func *pf = p;
  320. struct iio_dev *indio_dev = pf->indio_dev;
  321. struct hx711_data *hx711_data = iio_priv(indio_dev);
  322. int i, j = 0;
  323. mutex_lock(&hx711_data->lock);
  324. memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
  325. for (i = 0; i < indio_dev->masklength; i++) {
  326. if (!test_bit(i, indio_dev->active_scan_mask))
  327. continue;
  328. hx711_data->buffer[j] = hx711_reset_read(hx711_data,
  329. indio_dev->channels[i].channel);
  330. j++;
  331. }
  332. iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
  333. pf->timestamp);
  334. mutex_unlock(&hx711_data->lock);
  335. iio_trigger_notify_done(indio_dev->trig);
  336. return IRQ_HANDLED;
  337. }
  338. static ssize_t hx711_scale_available_show(struct device *dev,
  339. struct device_attribute *attr,
  340. char *buf)
  341. {
  342. struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
  343. int channel = iio_attr->address;
  344. int i, len = 0;
  345. for (i = 0; i < HX711_GAIN_MAX; i++)
  346. if (hx711_gain_to_scale[i].channel == channel)
  347. len += sprintf(buf + len, "0.%09d ",
  348. hx711_gain_to_scale[i].scale);
  349. len += sprintf(buf + len, "\n");
  350. return len;
  351. }
  352. static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
  353. hx711_scale_available_show, NULL, 0);
  354. static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
  355. hx711_scale_available_show, NULL, 1);
  356. static struct attribute *hx711_attributes[] = {
  357. &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
  358. &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
  359. NULL,
  360. };
  361. static const struct attribute_group hx711_attribute_group = {
  362. .attrs = hx711_attributes,
  363. };
  364. static const struct iio_info hx711_iio_info = {
  365. .read_raw = hx711_read_raw,
  366. .write_raw = hx711_write_raw,
  367. .write_raw_get_fmt = hx711_write_raw_get_fmt,
  368. .attrs = &hx711_attribute_group,
  369. };
  370. static const struct iio_chan_spec hx711_chan_spec[] = {
  371. {
  372. .type = IIO_VOLTAGE,
  373. .channel = 0,
  374. .indexed = 1,
  375. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  376. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  377. .scan_index = 0,
  378. .scan_type = {
  379. .sign = 'u',
  380. .realbits = 24,
  381. .storagebits = 32,
  382. .endianness = IIO_CPU,
  383. },
  384. },
  385. {
  386. .type = IIO_VOLTAGE,
  387. .channel = 1,
  388. .indexed = 1,
  389. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  390. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  391. .scan_index = 1,
  392. .scan_type = {
  393. .sign = 'u',
  394. .realbits = 24,
  395. .storagebits = 32,
  396. .endianness = IIO_CPU,
  397. },
  398. },
  399. IIO_CHAN_SOFT_TIMESTAMP(2),
  400. };
  401. static int hx711_probe(struct platform_device *pdev)
  402. {
  403. struct device *dev = &pdev->dev;
  404. struct device_node *np = dev->of_node;
  405. struct hx711_data *hx711_data;
  406. struct iio_dev *indio_dev;
  407. int ret;
  408. int i;
  409. indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
  410. if (!indio_dev) {
  411. dev_err(dev, "failed to allocate IIO device\n");
  412. return -ENOMEM;
  413. }
  414. hx711_data = iio_priv(indio_dev);
  415. hx711_data->dev = dev;
  416. mutex_init(&hx711_data->lock);
  417. /*
  418. * PD_SCK stands for power down and serial clock input of HX711
  419. * in the driver it is an output
  420. */
  421. hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
  422. if (IS_ERR(hx711_data->gpiod_pd_sck)) {
  423. dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
  424. PTR_ERR(hx711_data->gpiod_pd_sck));
  425. return PTR_ERR(hx711_data->gpiod_pd_sck);
  426. }
  427. /*
  428. * DOUT stands for serial data output of HX711
  429. * for the driver it is an input
  430. */
  431. hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
  432. if (IS_ERR(hx711_data->gpiod_dout)) {
  433. dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
  434. PTR_ERR(hx711_data->gpiod_dout));
  435. return PTR_ERR(hx711_data->gpiod_dout);
  436. }
  437. hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
  438. if (IS_ERR(hx711_data->reg_avdd))
  439. return PTR_ERR(hx711_data->reg_avdd);
  440. ret = regulator_enable(hx711_data->reg_avdd);
  441. if (ret < 0)
  442. return ret;
  443. /*
  444. * with
  445. * full scale differential input range: AVDD / GAIN
  446. * full scale output data: 2^24
  447. * we can say:
  448. * AVDD / GAIN = 2^24
  449. * therefore:
  450. * 1 LSB = AVDD / GAIN / 2^24
  451. * AVDD is in uV, but we need 10^-9 mV
  452. * approximately to fit into a 32 bit number:
  453. * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
  454. */
  455. ret = regulator_get_voltage(hx711_data->reg_avdd);
  456. if (ret < 0)
  457. goto error_regulator;
  458. /* we need 10^-9 mV */
  459. ret *= 100;
  460. for (i = 0; i < HX711_GAIN_MAX; i++)
  461. hx711_gain_to_scale[i].scale =
  462. ret / hx711_gain_to_scale[i].gain / 1678;
  463. hx711_data->gain_set = 128;
  464. hx711_data->gain_chan_a = 128;
  465. hx711_data->clock_frequency = 400000;
  466. ret = of_property_read_u32(np, "clock-frequency",
  467. &hx711_data->clock_frequency);
  468. /*
  469. * datasheet says the high level of PD_SCK has a maximum duration
  470. * of 50 microseconds
  471. */
  472. if (hx711_data->clock_frequency < 20000) {
  473. dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n");
  474. hx711_data->clock_frequency = 400000;
  475. }
  476. hx711_data->data_ready_delay_ns =
  477. 1000000000 / hx711_data->clock_frequency;
  478. platform_set_drvdata(pdev, indio_dev);
  479. indio_dev->name = "hx711";
  480. indio_dev->dev.parent = &pdev->dev;
  481. indio_dev->info = &hx711_iio_info;
  482. indio_dev->modes = INDIO_DIRECT_MODE;
  483. indio_dev->channels = hx711_chan_spec;
  484. indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
  485. ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
  486. hx711_trigger, NULL);
  487. if (ret < 0) {
  488. dev_err(dev, "setup of iio triggered buffer failed\n");
  489. goto error_regulator;
  490. }
  491. ret = iio_device_register(indio_dev);
  492. if (ret < 0) {
  493. dev_err(dev, "Couldn't register the device\n");
  494. goto error_buffer;
  495. }
  496. return 0;
  497. error_buffer:
  498. iio_triggered_buffer_cleanup(indio_dev);
  499. error_regulator:
  500. regulator_disable(hx711_data->reg_avdd);
  501. return ret;
  502. }
  503. static int hx711_remove(struct platform_device *pdev)
  504. {
  505. struct hx711_data *hx711_data;
  506. struct iio_dev *indio_dev;
  507. indio_dev = platform_get_drvdata(pdev);
  508. hx711_data = iio_priv(indio_dev);
  509. iio_device_unregister(indio_dev);
  510. iio_triggered_buffer_cleanup(indio_dev);
  511. regulator_disable(hx711_data->reg_avdd);
  512. return 0;
  513. }
  514. static const struct of_device_id of_hx711_match[] = {
  515. { .compatible = "avia,hx711", },
  516. {},
  517. };
  518. MODULE_DEVICE_TABLE(of, of_hx711_match);
  519. static struct platform_driver hx711_driver = {
  520. .probe = hx711_probe,
  521. .remove = hx711_remove,
  522. .driver = {
  523. .name = "hx711-gpio",
  524. .of_match_table = of_hx711_match,
  525. },
  526. };
  527. module_platform_driver(hx711_driver);
  528. MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
  529. MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
  530. MODULE_LICENSE("GPL");
  531. MODULE_ALIAS("platform:hx711-gpio");