hx711.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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/gpio/consumer.h>
  28. #include <linux/regulator/consumer.h>
  29. /* gain to pulse and scale conversion */
  30. #define HX711_GAIN_MAX 3
  31. struct hx711_gain_to_scale {
  32. int gain;
  33. int gain_pulse;
  34. int scale;
  35. int channel;
  36. };
  37. /*
  38. * .scale depends on AVDD which in turn is known as soon as the regulator
  39. * is available
  40. * therefore we set .scale in hx711_probe()
  41. *
  42. * channel A in documentation is channel 0 in source code
  43. * channel B in documentation is channel 1 in source code
  44. */
  45. static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX] = {
  46. { 128, 1, 0, 0 },
  47. { 32, 2, 0, 1 },
  48. { 64, 3, 0, 0 }
  49. };
  50. static int hx711_get_gain_to_pulse(int gain)
  51. {
  52. int i;
  53. for (i = 0; i < HX711_GAIN_MAX; i++)
  54. if (hx711_gain_to_scale[i].gain == gain)
  55. return hx711_gain_to_scale[i].gain_pulse;
  56. return 1;
  57. }
  58. static int hx711_get_gain_to_scale(int gain)
  59. {
  60. int i;
  61. for (i = 0; i < HX711_GAIN_MAX; i++)
  62. if (hx711_gain_to_scale[i].gain == gain)
  63. return hx711_gain_to_scale[i].scale;
  64. return 0;
  65. }
  66. static int hx711_get_scale_to_gain(int scale)
  67. {
  68. int i;
  69. for (i = 0; i < HX711_GAIN_MAX; i++)
  70. if (hx711_gain_to_scale[i].scale == scale)
  71. return hx711_gain_to_scale[i].gain;
  72. return -EINVAL;
  73. }
  74. struct hx711_data {
  75. struct device *dev;
  76. struct gpio_desc *gpiod_pd_sck;
  77. struct gpio_desc *gpiod_dout;
  78. struct regulator *reg_avdd;
  79. int gain_set; /* gain set on device */
  80. int gain_chan_a; /* gain for channel A */
  81. struct mutex lock;
  82. };
  83. static int hx711_cycle(struct hx711_data *hx711_data)
  84. {
  85. int val;
  86. /*
  87. * if preempted for more then 60us while PD_SCK is high:
  88. * hx711 is going in reset
  89. * ==> measuring is false
  90. */
  91. preempt_disable();
  92. gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
  93. val = gpiod_get_value(hx711_data->gpiod_dout);
  94. /*
  95. * here we are not waiting for 0.2 us as suggested by the datasheet,
  96. * because the oscilloscope showed in a test scenario
  97. * at least 1.15 us for PD_SCK high (T3 in datasheet)
  98. * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
  99. */
  100. gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
  101. preempt_enable();
  102. return val;
  103. }
  104. static int hx711_read(struct hx711_data *hx711_data)
  105. {
  106. int i, ret;
  107. int value = 0;
  108. int val = gpiod_get_value(hx711_data->gpiod_dout);
  109. /* we double check if it's really down */
  110. if (val)
  111. return -EIO;
  112. for (i = 0; i < 24; i++) {
  113. value <<= 1;
  114. ret = hx711_cycle(hx711_data);
  115. if (ret)
  116. value++;
  117. }
  118. value ^= 0x800000;
  119. for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
  120. hx711_cycle(hx711_data);
  121. return value;
  122. }
  123. static int hx711_wait_for_ready(struct hx711_data *hx711_data)
  124. {
  125. int i, val;
  126. /*
  127. * a maximum reset cycle time of 56 ms was measured.
  128. * we round it up to 100 ms
  129. */
  130. for (i = 0; i < 100; i++) {
  131. val = gpiod_get_value(hx711_data->gpiod_dout);
  132. if (!val)
  133. break;
  134. /* sleep at least 1 ms */
  135. msleep(1);
  136. }
  137. if (val)
  138. return -EIO;
  139. return 0;
  140. }
  141. static int hx711_reset(struct hx711_data *hx711_data)
  142. {
  143. int ret;
  144. int val = gpiod_get_value(hx711_data->gpiod_dout);
  145. if (val) {
  146. /*
  147. * an examination with the oszilloscope indicated
  148. * that the first value read after the reset is not stable
  149. * if we reset too short;
  150. * the shorter the reset cycle
  151. * the less reliable the first value after reset is;
  152. * there were no problems encountered with a value
  153. * of 10 ms or higher
  154. */
  155. gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
  156. msleep(10);
  157. gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
  158. ret = hx711_wait_for_ready(hx711_data);
  159. if (ret)
  160. return ret;
  161. /*
  162. * after a reset the gain is 128 so we do a dummy read
  163. * to set the gain for the next read
  164. */
  165. ret = hx711_read(hx711_data);
  166. if (ret < 0)
  167. return ret;
  168. /*
  169. * after a dummy read we need to wait vor readiness
  170. * for not mixing gain pulses with the clock
  171. */
  172. ret = hx711_wait_for_ready(hx711_data);
  173. if (ret)
  174. return ret;
  175. }
  176. return val;
  177. }
  178. static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
  179. {
  180. int ret;
  181. if (chan == 0) {
  182. if (hx711_data->gain_set == 32) {
  183. hx711_data->gain_set = hx711_data->gain_chan_a;
  184. ret = hx711_read(hx711_data);
  185. if (ret < 0)
  186. return ret;
  187. ret = hx711_wait_for_ready(hx711_data);
  188. if (ret)
  189. return ret;
  190. }
  191. } else {
  192. if (hx711_data->gain_set != 32) {
  193. hx711_data->gain_set = 32;
  194. ret = hx711_read(hx711_data);
  195. if (ret < 0)
  196. return ret;
  197. ret = hx711_wait_for_ready(hx711_data);
  198. if (ret)
  199. return ret;
  200. }
  201. }
  202. return 0;
  203. }
  204. static int hx711_read_raw(struct iio_dev *indio_dev,
  205. const struct iio_chan_spec *chan,
  206. int *val, int *val2, long mask)
  207. {
  208. struct hx711_data *hx711_data = iio_priv(indio_dev);
  209. int ret;
  210. switch (mask) {
  211. case IIO_CHAN_INFO_RAW:
  212. mutex_lock(&hx711_data->lock);
  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. mutex_unlock(&hx711_data->lock);
  219. dev_err(hx711_data->dev, "reset failed!");
  220. return -EIO;
  221. }
  222. ret = hx711_set_gain_for_channel(hx711_data, chan->channel);
  223. if (ret < 0) {
  224. mutex_unlock(&hx711_data->lock);
  225. return ret;
  226. }
  227. *val = hx711_read(hx711_data);
  228. mutex_unlock(&hx711_data->lock);
  229. if (*val < 0)
  230. return *val;
  231. return IIO_VAL_INT;
  232. case IIO_CHAN_INFO_SCALE:
  233. *val = 0;
  234. mutex_lock(&hx711_data->lock);
  235. *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
  236. mutex_unlock(&hx711_data->lock);
  237. return IIO_VAL_INT_PLUS_NANO;
  238. default:
  239. return -EINVAL;
  240. }
  241. }
  242. static int hx711_write_raw(struct iio_dev *indio_dev,
  243. struct iio_chan_spec const *chan,
  244. int val,
  245. int val2,
  246. long mask)
  247. {
  248. struct hx711_data *hx711_data = iio_priv(indio_dev);
  249. int ret;
  250. int gain;
  251. switch (mask) {
  252. case IIO_CHAN_INFO_SCALE:
  253. /*
  254. * a scale greater than 1 mV per LSB is not possible
  255. * with the HX711, therefore val must be 0
  256. */
  257. if (val != 0)
  258. return -EINVAL;
  259. mutex_lock(&hx711_data->lock);
  260. gain = hx711_get_scale_to_gain(val2);
  261. if (gain < 0) {
  262. mutex_unlock(&hx711_data->lock);
  263. return gain;
  264. }
  265. if (gain != hx711_data->gain_set) {
  266. hx711_data->gain_set = gain;
  267. if (gain != 32)
  268. hx711_data->gain_chan_a = gain;
  269. ret = hx711_read(hx711_data);
  270. if (ret < 0) {
  271. mutex_unlock(&hx711_data->lock);
  272. return ret;
  273. }
  274. }
  275. mutex_unlock(&hx711_data->lock);
  276. return 0;
  277. default:
  278. return -EINVAL;
  279. }
  280. return 0;
  281. }
  282. static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
  283. struct iio_chan_spec const *chan,
  284. long mask)
  285. {
  286. return IIO_VAL_INT_PLUS_NANO;
  287. }
  288. static ssize_t hx711_scale_available_show(struct device *dev,
  289. struct device_attribute *attr,
  290. char *buf)
  291. {
  292. struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
  293. int channel = iio_attr->address;
  294. int i, len = 0;
  295. for (i = 0; i < HX711_GAIN_MAX; i++)
  296. if (hx711_gain_to_scale[i].channel == channel)
  297. len += sprintf(buf + len, "0.%09d ",
  298. hx711_gain_to_scale[i].scale);
  299. len += sprintf(buf + len, "\n");
  300. return len;
  301. }
  302. static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
  303. hx711_scale_available_show, NULL, 0);
  304. static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
  305. hx711_scale_available_show, NULL, 1);
  306. static struct attribute *hx711_attributes[] = {
  307. &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
  308. &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
  309. NULL,
  310. };
  311. static const struct attribute_group hx711_attribute_group = {
  312. .attrs = hx711_attributes,
  313. };
  314. static const struct iio_info hx711_iio_info = {
  315. .driver_module = THIS_MODULE,
  316. .read_raw = hx711_read_raw,
  317. .write_raw = hx711_write_raw,
  318. .write_raw_get_fmt = hx711_write_raw_get_fmt,
  319. .attrs = &hx711_attribute_group,
  320. };
  321. static const struct iio_chan_spec hx711_chan_spec[] = {
  322. {
  323. .type = IIO_VOLTAGE,
  324. .channel = 0,
  325. .indexed = 1,
  326. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  327. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  328. },
  329. {
  330. .type = IIO_VOLTAGE,
  331. .channel = 1,
  332. .indexed = 1,
  333. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  334. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  335. },
  336. };
  337. static int hx711_probe(struct platform_device *pdev)
  338. {
  339. struct device *dev = &pdev->dev;
  340. struct hx711_data *hx711_data;
  341. struct iio_dev *indio_dev;
  342. int ret;
  343. int i;
  344. indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
  345. if (!indio_dev) {
  346. dev_err(dev, "failed to allocate IIO device\n");
  347. return -ENOMEM;
  348. }
  349. hx711_data = iio_priv(indio_dev);
  350. hx711_data->dev = dev;
  351. mutex_init(&hx711_data->lock);
  352. /*
  353. * PD_SCK stands for power down and serial clock input of HX711
  354. * in the driver it is an output
  355. */
  356. hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
  357. if (IS_ERR(hx711_data->gpiod_pd_sck)) {
  358. dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
  359. PTR_ERR(hx711_data->gpiod_pd_sck));
  360. return PTR_ERR(hx711_data->gpiod_pd_sck);
  361. }
  362. /*
  363. * DOUT stands for serial data output of HX711
  364. * for the driver it is an input
  365. */
  366. hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
  367. if (IS_ERR(hx711_data->gpiod_dout)) {
  368. dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
  369. PTR_ERR(hx711_data->gpiod_dout));
  370. return PTR_ERR(hx711_data->gpiod_dout);
  371. }
  372. hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
  373. if (IS_ERR(hx711_data->reg_avdd))
  374. return PTR_ERR(hx711_data->reg_avdd);
  375. ret = regulator_enable(hx711_data->reg_avdd);
  376. if (ret < 0)
  377. return ret;
  378. /*
  379. * with
  380. * full scale differential input range: AVDD / GAIN
  381. * full scale output data: 2^24
  382. * we can say:
  383. * AVDD / GAIN = 2^24
  384. * therefore:
  385. * 1 LSB = AVDD / GAIN / 2^24
  386. * AVDD is in uV, but we need 10^-9 mV
  387. * approximately to fit into a 32 bit number:
  388. * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
  389. */
  390. ret = regulator_get_voltage(hx711_data->reg_avdd);
  391. if (ret < 0) {
  392. regulator_disable(hx711_data->reg_avdd);
  393. return ret;
  394. }
  395. /* we need 10^-9 mV */
  396. ret *= 100;
  397. for (i = 0; i < HX711_GAIN_MAX; i++)
  398. hx711_gain_to_scale[i].scale =
  399. ret / hx711_gain_to_scale[i].gain / 1678;
  400. hx711_data->gain_set = 128;
  401. hx711_data->gain_chan_a = 128;
  402. platform_set_drvdata(pdev, indio_dev);
  403. indio_dev->name = "hx711";
  404. indio_dev->dev.parent = &pdev->dev;
  405. indio_dev->info = &hx711_iio_info;
  406. indio_dev->modes = INDIO_DIRECT_MODE;
  407. indio_dev->channels = hx711_chan_spec;
  408. indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
  409. ret = iio_device_register(indio_dev);
  410. if (ret < 0) {
  411. dev_err(dev, "Couldn't register the device\n");
  412. regulator_disable(hx711_data->reg_avdd);
  413. }
  414. return ret;
  415. }
  416. static int hx711_remove(struct platform_device *pdev)
  417. {
  418. struct hx711_data *hx711_data;
  419. struct iio_dev *indio_dev;
  420. indio_dev = platform_get_drvdata(pdev);
  421. hx711_data = iio_priv(indio_dev);
  422. iio_device_unregister(indio_dev);
  423. regulator_disable(hx711_data->reg_avdd);
  424. return 0;
  425. }
  426. static const struct of_device_id of_hx711_match[] = {
  427. { .compatible = "avia,hx711", },
  428. {},
  429. };
  430. MODULE_DEVICE_TABLE(of, of_hx711_match);
  431. static struct platform_driver hx711_driver = {
  432. .probe = hx711_probe,
  433. .remove = hx711_remove,
  434. .driver = {
  435. .name = "hx711-gpio",
  436. .of_match_table = of_hx711_match,
  437. },
  438. };
  439. module_platform_driver(hx711_driver);
  440. MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
  441. MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
  442. MODULE_LICENSE("GPL");
  443. MODULE_ALIAS("platform:hx711-gpio");