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