hx711.c 14 KB

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