hx711.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. static int hx711_cycle(struct hx711_data *hx711_data)
  92. {
  93. int val;
  94. /*
  95. * if preempted for more then 60us while PD_SCK is high:
  96. * hx711 is going in reset
  97. * ==> measuring is false
  98. */
  99. preempt_disable();
  100. gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
  101. val = gpiod_get_value(hx711_data->gpiod_dout);
  102. /*
  103. * here we are not waiting for 0.2 us as suggested by the datasheet,
  104. * because the oscilloscope showed in a test scenario
  105. * at least 1.15 us for PD_SCK high (T3 in datasheet)
  106. * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
  107. */
  108. gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
  109. preempt_enable();
  110. return val;
  111. }
  112. static int hx711_read(struct hx711_data *hx711_data)
  113. {
  114. int i, ret;
  115. int value = 0;
  116. int val = gpiod_get_value(hx711_data->gpiod_dout);
  117. /* we double check if it's really down */
  118. if (val)
  119. return -EIO;
  120. for (i = 0; i < 24; i++) {
  121. value <<= 1;
  122. ret = hx711_cycle(hx711_data);
  123. if (ret)
  124. value++;
  125. }
  126. value ^= 0x800000;
  127. for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
  128. hx711_cycle(hx711_data);
  129. return value;
  130. }
  131. static int hx711_wait_for_ready(struct hx711_data *hx711_data)
  132. {
  133. int i, val;
  134. /*
  135. * in some rare cases the reset takes quite a long time
  136. * especially when the channel is changed.
  137. * Allow up to one second for it
  138. */
  139. for (i = 0; i < 100; i++) {
  140. val = gpiod_get_value(hx711_data->gpiod_dout);
  141. if (!val)
  142. break;
  143. /* sleep at least 10 ms */
  144. msleep(10);
  145. }
  146. if (val)
  147. return -EIO;
  148. return 0;
  149. }
  150. static int hx711_reset(struct hx711_data *hx711_data)
  151. {
  152. int ret;
  153. int val = gpiod_get_value(hx711_data->gpiod_dout);
  154. if (val) {
  155. /*
  156. * an examination with the oszilloscope indicated
  157. * that the first value read after the reset is not stable
  158. * if we reset too short;
  159. * the shorter the reset cycle
  160. * the less reliable the first value after reset is;
  161. * there were no problems encountered with a value
  162. * of 10 ms or higher
  163. */
  164. gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
  165. msleep(10);
  166. gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
  167. ret = hx711_wait_for_ready(hx711_data);
  168. if (ret)
  169. return ret;
  170. /*
  171. * after a reset the gain is 128 so we do a dummy read
  172. * to set the gain for the next read
  173. */
  174. ret = hx711_read(hx711_data);
  175. if (ret < 0)
  176. return ret;
  177. /*
  178. * after a dummy read we need to wait vor readiness
  179. * for not mixing gain pulses with the clock
  180. */
  181. val = hx711_wait_for_ready(hx711_data);
  182. }
  183. return val;
  184. }
  185. static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
  186. {
  187. int ret;
  188. if (chan == 0) {
  189. if (hx711_data->gain_set == 32) {
  190. hx711_data->gain_set = hx711_data->gain_chan_a;
  191. ret = hx711_read(hx711_data);
  192. if (ret < 0)
  193. return ret;
  194. ret = hx711_wait_for_ready(hx711_data);
  195. if (ret)
  196. return ret;
  197. }
  198. } else {
  199. if (hx711_data->gain_set != 32) {
  200. hx711_data->gain_set = 32;
  201. ret = hx711_read(hx711_data);
  202. if (ret < 0)
  203. return ret;
  204. ret = hx711_wait_for_ready(hx711_data);
  205. if (ret)
  206. return ret;
  207. }
  208. }
  209. return 0;
  210. }
  211. static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
  212. {
  213. int ret;
  214. int val;
  215. /*
  216. * hx711_reset() must be called from here
  217. * because it could be calling hx711_read() by itself
  218. */
  219. if (hx711_reset(hx711_data)) {
  220. dev_err(hx711_data->dev, "reset failed!");
  221. return -EIO;
  222. }
  223. ret = hx711_set_gain_for_channel(hx711_data, chan);
  224. if (ret < 0)
  225. return ret;
  226. val = hx711_read(hx711_data);
  227. return val;
  228. }
  229. static int hx711_read_raw(struct iio_dev *indio_dev,
  230. const struct iio_chan_spec *chan,
  231. int *val, int *val2, long mask)
  232. {
  233. struct hx711_data *hx711_data = iio_priv(indio_dev);
  234. switch (mask) {
  235. case IIO_CHAN_INFO_RAW:
  236. mutex_lock(&hx711_data->lock);
  237. *val = hx711_reset_read(hx711_data, chan->channel);
  238. mutex_unlock(&hx711_data->lock);
  239. if (*val < 0)
  240. return *val;
  241. return IIO_VAL_INT;
  242. case IIO_CHAN_INFO_SCALE:
  243. *val = 0;
  244. mutex_lock(&hx711_data->lock);
  245. *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
  246. mutex_unlock(&hx711_data->lock);
  247. return IIO_VAL_INT_PLUS_NANO;
  248. default:
  249. return -EINVAL;
  250. }
  251. }
  252. static int hx711_write_raw(struct iio_dev *indio_dev,
  253. struct iio_chan_spec const *chan,
  254. int val,
  255. int val2,
  256. long mask)
  257. {
  258. struct hx711_data *hx711_data = iio_priv(indio_dev);
  259. int ret;
  260. int gain;
  261. switch (mask) {
  262. case IIO_CHAN_INFO_SCALE:
  263. /*
  264. * a scale greater than 1 mV per LSB is not possible
  265. * with the HX711, therefore val must be 0
  266. */
  267. if (val != 0)
  268. return -EINVAL;
  269. mutex_lock(&hx711_data->lock);
  270. gain = hx711_get_scale_to_gain(val2);
  271. if (gain < 0) {
  272. mutex_unlock(&hx711_data->lock);
  273. return gain;
  274. }
  275. if (gain != hx711_data->gain_set) {
  276. hx711_data->gain_set = gain;
  277. if (gain != 32)
  278. hx711_data->gain_chan_a = gain;
  279. ret = hx711_read(hx711_data);
  280. if (ret < 0) {
  281. mutex_unlock(&hx711_data->lock);
  282. return ret;
  283. }
  284. }
  285. mutex_unlock(&hx711_data->lock);
  286. return 0;
  287. default:
  288. return -EINVAL;
  289. }
  290. return 0;
  291. }
  292. static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
  293. struct iio_chan_spec const *chan,
  294. long mask)
  295. {
  296. return IIO_VAL_INT_PLUS_NANO;
  297. }
  298. static irqreturn_t hx711_trigger(int irq, void *p)
  299. {
  300. struct iio_poll_func *pf = p;
  301. struct iio_dev *indio_dev = pf->indio_dev;
  302. struct hx711_data *hx711_data = iio_priv(indio_dev);
  303. int i, j = 0;
  304. mutex_lock(&hx711_data->lock);
  305. memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
  306. for (i = 0; i < indio_dev->masklength; i++) {
  307. if (!test_bit(i, indio_dev->active_scan_mask))
  308. continue;
  309. hx711_data->buffer[j] = hx711_reset_read(hx711_data,
  310. indio_dev->channels[i].channel);
  311. j++;
  312. }
  313. iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
  314. pf->timestamp);
  315. mutex_unlock(&hx711_data->lock);
  316. iio_trigger_notify_done(indio_dev->trig);
  317. return IRQ_HANDLED;
  318. }
  319. static ssize_t hx711_scale_available_show(struct device *dev,
  320. struct device_attribute *attr,
  321. char *buf)
  322. {
  323. struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
  324. int channel = iio_attr->address;
  325. int i, len = 0;
  326. for (i = 0; i < HX711_GAIN_MAX; i++)
  327. if (hx711_gain_to_scale[i].channel == channel)
  328. len += sprintf(buf + len, "0.%09d ",
  329. hx711_gain_to_scale[i].scale);
  330. len += sprintf(buf + len, "\n");
  331. return len;
  332. }
  333. static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
  334. hx711_scale_available_show, NULL, 0);
  335. static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
  336. hx711_scale_available_show, NULL, 1);
  337. static struct attribute *hx711_attributes[] = {
  338. &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
  339. &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
  340. NULL,
  341. };
  342. static const struct attribute_group hx711_attribute_group = {
  343. .attrs = hx711_attributes,
  344. };
  345. static const struct iio_info hx711_iio_info = {
  346. .read_raw = hx711_read_raw,
  347. .write_raw = hx711_write_raw,
  348. .write_raw_get_fmt = hx711_write_raw_get_fmt,
  349. .attrs = &hx711_attribute_group,
  350. };
  351. static const struct iio_chan_spec hx711_chan_spec[] = {
  352. {
  353. .type = IIO_VOLTAGE,
  354. .channel = 0,
  355. .indexed = 1,
  356. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  357. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  358. .scan_index = 0,
  359. .scan_type = {
  360. .sign = 'u',
  361. .realbits = 24,
  362. .storagebits = 32,
  363. .endianness = IIO_CPU,
  364. },
  365. },
  366. {
  367. .type = IIO_VOLTAGE,
  368. .channel = 1,
  369. .indexed = 1,
  370. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  371. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  372. .scan_index = 1,
  373. .scan_type = {
  374. .sign = 'u',
  375. .realbits = 24,
  376. .storagebits = 32,
  377. .endianness = IIO_CPU,
  378. },
  379. },
  380. IIO_CHAN_SOFT_TIMESTAMP(2),
  381. };
  382. static int hx711_probe(struct platform_device *pdev)
  383. {
  384. struct device *dev = &pdev->dev;
  385. struct hx711_data *hx711_data;
  386. struct iio_dev *indio_dev;
  387. int ret;
  388. int i;
  389. indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
  390. if (!indio_dev) {
  391. dev_err(dev, "failed to allocate IIO device\n");
  392. return -ENOMEM;
  393. }
  394. hx711_data = iio_priv(indio_dev);
  395. hx711_data->dev = dev;
  396. mutex_init(&hx711_data->lock);
  397. /*
  398. * PD_SCK stands for power down and serial clock input of HX711
  399. * in the driver it is an output
  400. */
  401. hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
  402. if (IS_ERR(hx711_data->gpiod_pd_sck)) {
  403. dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
  404. PTR_ERR(hx711_data->gpiod_pd_sck));
  405. return PTR_ERR(hx711_data->gpiod_pd_sck);
  406. }
  407. /*
  408. * DOUT stands for serial data output of HX711
  409. * for the driver it is an input
  410. */
  411. hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
  412. if (IS_ERR(hx711_data->gpiod_dout)) {
  413. dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
  414. PTR_ERR(hx711_data->gpiod_dout));
  415. return PTR_ERR(hx711_data->gpiod_dout);
  416. }
  417. hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
  418. if (IS_ERR(hx711_data->reg_avdd))
  419. return PTR_ERR(hx711_data->reg_avdd);
  420. ret = regulator_enable(hx711_data->reg_avdd);
  421. if (ret < 0)
  422. return ret;
  423. /*
  424. * with
  425. * full scale differential input range: AVDD / GAIN
  426. * full scale output data: 2^24
  427. * we can say:
  428. * AVDD / GAIN = 2^24
  429. * therefore:
  430. * 1 LSB = AVDD / GAIN / 2^24
  431. * AVDD is in uV, but we need 10^-9 mV
  432. * approximately to fit into a 32 bit number:
  433. * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
  434. */
  435. ret = regulator_get_voltage(hx711_data->reg_avdd);
  436. if (ret < 0)
  437. goto error_regulator;
  438. /* we need 10^-9 mV */
  439. ret *= 100;
  440. for (i = 0; i < HX711_GAIN_MAX; i++)
  441. hx711_gain_to_scale[i].scale =
  442. ret / hx711_gain_to_scale[i].gain / 1678;
  443. hx711_data->gain_set = 128;
  444. hx711_data->gain_chan_a = 128;
  445. platform_set_drvdata(pdev, indio_dev);
  446. indio_dev->name = "hx711";
  447. indio_dev->dev.parent = &pdev->dev;
  448. indio_dev->info = &hx711_iio_info;
  449. indio_dev->modes = INDIO_DIRECT_MODE;
  450. indio_dev->channels = hx711_chan_spec;
  451. indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
  452. ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
  453. hx711_trigger, NULL);
  454. if (ret < 0) {
  455. dev_err(dev, "setup of iio triggered buffer failed\n");
  456. goto error_regulator;
  457. }
  458. ret = iio_device_register(indio_dev);
  459. if (ret < 0) {
  460. dev_err(dev, "Couldn't register the device\n");
  461. goto error_buffer;
  462. }
  463. return 0;
  464. error_buffer:
  465. iio_triggered_buffer_cleanup(indio_dev);
  466. error_regulator:
  467. regulator_disable(hx711_data->reg_avdd);
  468. return ret;
  469. }
  470. static int hx711_remove(struct platform_device *pdev)
  471. {
  472. struct hx711_data *hx711_data;
  473. struct iio_dev *indio_dev;
  474. indio_dev = platform_get_drvdata(pdev);
  475. hx711_data = iio_priv(indio_dev);
  476. iio_device_unregister(indio_dev);
  477. iio_triggered_buffer_cleanup(indio_dev);
  478. regulator_disable(hx711_data->reg_avdd);
  479. return 0;
  480. }
  481. static const struct of_device_id of_hx711_match[] = {
  482. { .compatible = "avia,hx711", },
  483. {},
  484. };
  485. MODULE_DEVICE_TABLE(of, of_hx711_match);
  486. static struct platform_driver hx711_driver = {
  487. .probe = hx711_probe,
  488. .remove = hx711_remove,
  489. .driver = {
  490. .name = "hx711-gpio",
  491. .of_match_table = of_hx711_match,
  492. },
  493. };
  494. module_platform_driver(hx711_driver);
  495. MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
  496. MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
  497. MODULE_LICENSE("GPL");
  498. MODULE_ALIAS("platform:hx711-gpio");