hx711.c 13 KB

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