1c3b2fdd0SAndreas Klinger /* 2c3b2fdd0SAndreas Klinger * HX711: analog to digital converter for weight sensor module 3c3b2fdd0SAndreas Klinger * 4c3b2fdd0SAndreas Klinger * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de> 5c3b2fdd0SAndreas Klinger * 6c3b2fdd0SAndreas Klinger * This program is free software; you can redistribute it and/or modify 7c3b2fdd0SAndreas Klinger * it under the terms of the GNU General Public License as published by 8c3b2fdd0SAndreas Klinger * the Free Software Foundation; either version 2 of the License, or 9c3b2fdd0SAndreas Klinger * (at your option) any later version. 10c3b2fdd0SAndreas Klinger * 11c3b2fdd0SAndreas Klinger * This program is distributed in the hope that it will be useful, 12c3b2fdd0SAndreas Klinger * but WITHOUT ANY WARRANTY; without even the implied warranty of 13c3b2fdd0SAndreas Klinger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14c3b2fdd0SAndreas Klinger * GNU General Public License for more details. 15c3b2fdd0SAndreas Klinger */ 16c3b2fdd0SAndreas Klinger #include <linux/err.h> 17c3b2fdd0SAndreas Klinger #include <linux/kernel.h> 18c3b2fdd0SAndreas Klinger #include <linux/module.h> 19c3b2fdd0SAndreas Klinger #include <linux/of.h> 20c3b2fdd0SAndreas Klinger #include <linux/platform_device.h> 21c3b2fdd0SAndreas Klinger #include <linux/property.h> 22c3b2fdd0SAndreas Klinger #include <linux/slab.h> 23c3b2fdd0SAndreas Klinger #include <linux/sched.h> 24c3b2fdd0SAndreas Klinger #include <linux/delay.h> 25c3b2fdd0SAndreas Klinger #include <linux/iio/iio.h> 26c3b2fdd0SAndreas Klinger #include <linux/iio/sysfs.h> 27d3bf6045SAndreas Klinger #include <linux/iio/buffer.h> 28d3bf6045SAndreas Klinger #include <linux/iio/trigger_consumer.h> 29d3bf6045SAndreas Klinger #include <linux/iio/triggered_buffer.h> 30c3b2fdd0SAndreas Klinger #include <linux/gpio/consumer.h> 31c3b2fdd0SAndreas Klinger #include <linux/regulator/consumer.h> 32c3b2fdd0SAndreas Klinger 33c3b2fdd0SAndreas Klinger /* gain to pulse and scale conversion */ 34c3b2fdd0SAndreas Klinger #define HX711_GAIN_MAX 3 35c3b2fdd0SAndreas Klinger 36c3b2fdd0SAndreas Klinger struct hx711_gain_to_scale { 37c3b2fdd0SAndreas Klinger int gain; 38c3b2fdd0SAndreas Klinger int gain_pulse; 39c3b2fdd0SAndreas Klinger int scale; 40c3b2fdd0SAndreas Klinger int channel; 41c3b2fdd0SAndreas Klinger }; 42c3b2fdd0SAndreas Klinger 43c3b2fdd0SAndreas Klinger /* 44c3b2fdd0SAndreas Klinger * .scale depends on AVDD which in turn is known as soon as the regulator 45c3b2fdd0SAndreas Klinger * is available 46c3b2fdd0SAndreas Klinger * therefore we set .scale in hx711_probe() 47c3b2fdd0SAndreas Klinger * 48c3b2fdd0SAndreas Klinger * channel A in documentation is channel 0 in source code 49c3b2fdd0SAndreas Klinger * channel B in documentation is channel 1 in source code 50c3b2fdd0SAndreas Klinger */ 51c3b2fdd0SAndreas Klinger static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX] = { 52c3b2fdd0SAndreas Klinger { 128, 1, 0, 0 }, 53c3b2fdd0SAndreas Klinger { 32, 2, 0, 1 }, 54c3b2fdd0SAndreas Klinger { 64, 3, 0, 0 } 55c3b2fdd0SAndreas Klinger }; 56c3b2fdd0SAndreas Klinger 57c3b2fdd0SAndreas Klinger static int hx711_get_gain_to_pulse(int gain) 58c3b2fdd0SAndreas Klinger { 59c3b2fdd0SAndreas Klinger int i; 60c3b2fdd0SAndreas Klinger 61c3b2fdd0SAndreas Klinger for (i = 0; i < HX711_GAIN_MAX; i++) 62c3b2fdd0SAndreas Klinger if (hx711_gain_to_scale[i].gain == gain) 63c3b2fdd0SAndreas Klinger return hx711_gain_to_scale[i].gain_pulse; 64c3b2fdd0SAndreas Klinger return 1; 65c3b2fdd0SAndreas Klinger } 66c3b2fdd0SAndreas Klinger 67c3b2fdd0SAndreas Klinger static int hx711_get_gain_to_scale(int gain) 68c3b2fdd0SAndreas Klinger { 69c3b2fdd0SAndreas Klinger int i; 70c3b2fdd0SAndreas Klinger 71c3b2fdd0SAndreas Klinger for (i = 0; i < HX711_GAIN_MAX; i++) 72c3b2fdd0SAndreas Klinger if (hx711_gain_to_scale[i].gain == gain) 73c3b2fdd0SAndreas Klinger return hx711_gain_to_scale[i].scale; 74c3b2fdd0SAndreas Klinger return 0; 75c3b2fdd0SAndreas Klinger } 76c3b2fdd0SAndreas Klinger 77c3b2fdd0SAndreas Klinger static int hx711_get_scale_to_gain(int scale) 78c3b2fdd0SAndreas Klinger { 79c3b2fdd0SAndreas Klinger int i; 80c3b2fdd0SAndreas Klinger 81c3b2fdd0SAndreas Klinger for (i = 0; i < HX711_GAIN_MAX; i++) 82c3b2fdd0SAndreas Klinger if (hx711_gain_to_scale[i].scale == scale) 83c3b2fdd0SAndreas Klinger return hx711_gain_to_scale[i].gain; 84c3b2fdd0SAndreas Klinger return -EINVAL; 85c3b2fdd0SAndreas Klinger } 86c3b2fdd0SAndreas Klinger 87c3b2fdd0SAndreas Klinger struct hx711_data { 88c3b2fdd0SAndreas Klinger struct device *dev; 89c3b2fdd0SAndreas Klinger struct gpio_desc *gpiod_pd_sck; 90c3b2fdd0SAndreas Klinger struct gpio_desc *gpiod_dout; 91c3b2fdd0SAndreas Klinger struct regulator *reg_avdd; 92c3b2fdd0SAndreas Klinger int gain_set; /* gain set on device */ 93c3b2fdd0SAndreas Klinger int gain_chan_a; /* gain for channel A */ 94c3b2fdd0SAndreas Klinger struct mutex lock; 95d3bf6045SAndreas Klinger /* 96d3bf6045SAndreas Klinger * triggered buffer 97d3bf6045SAndreas Klinger * 2x32-bit channel + 64-bit timestamp 98d3bf6045SAndreas Klinger */ 99d3bf6045SAndreas Klinger u32 buffer[4]; 100461631faSAndreas Klinger /* 101461631faSAndreas Klinger * delay after a rising edge on SCK until the data is ready DOUT 102461631faSAndreas Klinger * this is dependent on the hx711 where the datasheet tells a 103461631faSAndreas Klinger * maximum value of 100 ns 104461631faSAndreas Klinger * but also on potential parasitic capacities on the wiring 105461631faSAndreas Klinger */ 106461631faSAndreas Klinger u32 data_ready_delay_ns; 107461631faSAndreas Klinger u32 clock_frequency; 108c3b2fdd0SAndreas Klinger }; 109c3b2fdd0SAndreas Klinger 110c3b2fdd0SAndreas Klinger static int hx711_cycle(struct hx711_data *hx711_data) 111c3b2fdd0SAndreas Klinger { 112c3b2fdd0SAndreas Klinger int val; 113c3b2fdd0SAndreas Klinger 114c3b2fdd0SAndreas Klinger /* 115c3b2fdd0SAndreas Klinger * if preempted for more then 60us while PD_SCK is high: 116c3b2fdd0SAndreas Klinger * hx711 is going in reset 117c3b2fdd0SAndreas Klinger * ==> measuring is false 118c3b2fdd0SAndreas Klinger */ 119c3b2fdd0SAndreas Klinger preempt_disable(); 120c3b2fdd0SAndreas Klinger gpiod_set_value(hx711_data->gpiod_pd_sck, 1); 121461631faSAndreas Klinger 122461631faSAndreas Klinger /* 123461631faSAndreas Klinger * wait until DOUT is ready 124461631faSAndreas Klinger * it turned out that parasitic capacities are extending the time 125461631faSAndreas Klinger * until DOUT has reached it's value 126461631faSAndreas Klinger */ 127461631faSAndreas Klinger ndelay(hx711_data->data_ready_delay_ns); 128461631faSAndreas Klinger 129c3b2fdd0SAndreas Klinger val = gpiod_get_value(hx711_data->gpiod_dout); 130c3b2fdd0SAndreas Klinger /* 131c3b2fdd0SAndreas Klinger * here we are not waiting for 0.2 us as suggested by the datasheet, 132c3b2fdd0SAndreas Klinger * because the oscilloscope showed in a test scenario 133c3b2fdd0SAndreas Klinger * at least 1.15 us for PD_SCK high (T3 in datasheet) 134c3b2fdd0SAndreas Klinger * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz 135c3b2fdd0SAndreas Klinger */ 136c3b2fdd0SAndreas Klinger gpiod_set_value(hx711_data->gpiod_pd_sck, 0); 137c3b2fdd0SAndreas Klinger preempt_enable(); 138c3b2fdd0SAndreas Klinger 139461631faSAndreas Klinger /* 140461631faSAndreas Klinger * make it a square wave for addressing cases with capacitance on 141461631faSAndreas Klinger * PC_SCK 142461631faSAndreas Klinger */ 143461631faSAndreas Klinger ndelay(hx711_data->data_ready_delay_ns); 144461631faSAndreas Klinger 145c3b2fdd0SAndreas Klinger return val; 146c3b2fdd0SAndreas Klinger } 147c3b2fdd0SAndreas Klinger 148c3b2fdd0SAndreas Klinger static int hx711_read(struct hx711_data *hx711_data) 149c3b2fdd0SAndreas Klinger { 150c3b2fdd0SAndreas Klinger int i, ret; 151c3b2fdd0SAndreas Klinger int value = 0; 152c3b2fdd0SAndreas Klinger int val = gpiod_get_value(hx711_data->gpiod_dout); 153c3b2fdd0SAndreas Klinger 154c3b2fdd0SAndreas Klinger /* we double check if it's really down */ 155c3b2fdd0SAndreas Klinger if (val) 156c3b2fdd0SAndreas Klinger return -EIO; 157c3b2fdd0SAndreas Klinger 158c3b2fdd0SAndreas Klinger for (i = 0; i < 24; i++) { 159c3b2fdd0SAndreas Klinger value <<= 1; 160c3b2fdd0SAndreas Klinger ret = hx711_cycle(hx711_data); 161c3b2fdd0SAndreas Klinger if (ret) 162c3b2fdd0SAndreas Klinger value++; 163c3b2fdd0SAndreas Klinger } 164c3b2fdd0SAndreas Klinger 165c3b2fdd0SAndreas Klinger value ^= 0x800000; 166c3b2fdd0SAndreas Klinger 167c3b2fdd0SAndreas Klinger for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++) 168c3b2fdd0SAndreas Klinger hx711_cycle(hx711_data); 169c3b2fdd0SAndreas Klinger 170c3b2fdd0SAndreas Klinger return value; 171c3b2fdd0SAndreas Klinger } 172c3b2fdd0SAndreas Klinger 173c3b2fdd0SAndreas Klinger static int hx711_wait_for_ready(struct hx711_data *hx711_data) 174c3b2fdd0SAndreas Klinger { 175c3b2fdd0SAndreas Klinger int i, val; 176c3b2fdd0SAndreas Klinger 177c3b2fdd0SAndreas Klinger /* 1781fe899e3SAndreas Klinger * in some rare cases the reset takes quite a long time 1791fe899e3SAndreas Klinger * especially when the channel is changed. 1801fe899e3SAndreas Klinger * Allow up to one second for it 181c3b2fdd0SAndreas Klinger */ 182c3b2fdd0SAndreas Klinger for (i = 0; i < 100; i++) { 183c3b2fdd0SAndreas Klinger val = gpiod_get_value(hx711_data->gpiod_dout); 184c3b2fdd0SAndreas Klinger if (!val) 185c3b2fdd0SAndreas Klinger break; 1861fe899e3SAndreas Klinger /* sleep at least 10 ms */ 1871fe899e3SAndreas Klinger msleep(10); 188c3b2fdd0SAndreas Klinger } 189c3b2fdd0SAndreas Klinger if (val) 190c3b2fdd0SAndreas Klinger return -EIO; 191c3b2fdd0SAndreas Klinger 192c3b2fdd0SAndreas Klinger return 0; 193c3b2fdd0SAndreas Klinger } 194c3b2fdd0SAndreas Klinger 195c3b2fdd0SAndreas Klinger static int hx711_reset(struct hx711_data *hx711_data) 196c3b2fdd0SAndreas Klinger { 197c3b2fdd0SAndreas Klinger int ret; 198c3b2fdd0SAndreas Klinger int val = gpiod_get_value(hx711_data->gpiod_dout); 199c3b2fdd0SAndreas Klinger 200c3b2fdd0SAndreas Klinger if (val) { 201c3b2fdd0SAndreas Klinger /* 202c3b2fdd0SAndreas Klinger * an examination with the oszilloscope indicated 203c3b2fdd0SAndreas Klinger * that the first value read after the reset is not stable 204c3b2fdd0SAndreas Klinger * if we reset too short; 205c3b2fdd0SAndreas Klinger * the shorter the reset cycle 206c3b2fdd0SAndreas Klinger * the less reliable the first value after reset is; 207c3b2fdd0SAndreas Klinger * there were no problems encountered with a value 208c3b2fdd0SAndreas Klinger * of 10 ms or higher 209c3b2fdd0SAndreas Klinger */ 210c3b2fdd0SAndreas Klinger gpiod_set_value(hx711_data->gpiod_pd_sck, 1); 211c3b2fdd0SAndreas Klinger msleep(10); 212c3b2fdd0SAndreas Klinger gpiod_set_value(hx711_data->gpiod_pd_sck, 0); 213c3b2fdd0SAndreas Klinger 214c3b2fdd0SAndreas Klinger ret = hx711_wait_for_ready(hx711_data); 215c3b2fdd0SAndreas Klinger if (ret) 216c3b2fdd0SAndreas Klinger return ret; 217c3b2fdd0SAndreas Klinger /* 218c3b2fdd0SAndreas Klinger * after a reset the gain is 128 so we do a dummy read 219c3b2fdd0SAndreas Klinger * to set the gain for the next read 220c3b2fdd0SAndreas Klinger */ 221c3b2fdd0SAndreas Klinger ret = hx711_read(hx711_data); 222c3b2fdd0SAndreas Klinger if (ret < 0) 223c3b2fdd0SAndreas Klinger return ret; 224c3b2fdd0SAndreas Klinger 225c3b2fdd0SAndreas Klinger /* 226c3b2fdd0SAndreas Klinger * after a dummy read we need to wait vor readiness 227c3b2fdd0SAndreas Klinger * for not mixing gain pulses with the clock 228c3b2fdd0SAndreas Klinger */ 2291fe899e3SAndreas Klinger val = hx711_wait_for_ready(hx711_data); 230c3b2fdd0SAndreas Klinger } 231c3b2fdd0SAndreas Klinger 232c3b2fdd0SAndreas Klinger return val; 233c3b2fdd0SAndreas Klinger } 234c3b2fdd0SAndreas Klinger 235c3b2fdd0SAndreas Klinger static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan) 236c3b2fdd0SAndreas Klinger { 237c3b2fdd0SAndreas Klinger int ret; 238c3b2fdd0SAndreas Klinger 239c3b2fdd0SAndreas Klinger if (chan == 0) { 240c3b2fdd0SAndreas Klinger if (hx711_data->gain_set == 32) { 241c3b2fdd0SAndreas Klinger hx711_data->gain_set = hx711_data->gain_chan_a; 242c3b2fdd0SAndreas Klinger 243c3b2fdd0SAndreas Klinger ret = hx711_read(hx711_data); 244c3b2fdd0SAndreas Klinger if (ret < 0) 245c3b2fdd0SAndreas Klinger return ret; 246c3b2fdd0SAndreas Klinger 247c3b2fdd0SAndreas Klinger ret = hx711_wait_for_ready(hx711_data); 248c3b2fdd0SAndreas Klinger if (ret) 249c3b2fdd0SAndreas Klinger return ret; 250c3b2fdd0SAndreas Klinger } 251c3b2fdd0SAndreas Klinger } else { 252c3b2fdd0SAndreas Klinger if (hx711_data->gain_set != 32) { 253c3b2fdd0SAndreas Klinger hx711_data->gain_set = 32; 254c3b2fdd0SAndreas Klinger 255c3b2fdd0SAndreas Klinger ret = hx711_read(hx711_data); 256c3b2fdd0SAndreas Klinger if (ret < 0) 257c3b2fdd0SAndreas Klinger return ret; 258c3b2fdd0SAndreas Klinger 259c3b2fdd0SAndreas Klinger ret = hx711_wait_for_ready(hx711_data); 260c3b2fdd0SAndreas Klinger if (ret) 261c3b2fdd0SAndreas Klinger return ret; 262c3b2fdd0SAndreas Klinger } 263c3b2fdd0SAndreas Klinger } 264c3b2fdd0SAndreas Klinger 265c3b2fdd0SAndreas Klinger return 0; 266c3b2fdd0SAndreas Klinger } 267c3b2fdd0SAndreas Klinger 268d3bf6045SAndreas Klinger static int hx711_reset_read(struct hx711_data *hx711_data, int chan) 269c3b2fdd0SAndreas Klinger { 270c3b2fdd0SAndreas Klinger int ret; 271d3bf6045SAndreas Klinger int val; 272c3b2fdd0SAndreas Klinger 273c3b2fdd0SAndreas Klinger /* 274c3b2fdd0SAndreas Klinger * hx711_reset() must be called from here 275c3b2fdd0SAndreas Klinger * because it could be calling hx711_read() by itself 276c3b2fdd0SAndreas Klinger */ 277c3b2fdd0SAndreas Klinger if (hx711_reset(hx711_data)) { 278c3b2fdd0SAndreas Klinger dev_err(hx711_data->dev, "reset failed!"); 279c3b2fdd0SAndreas Klinger return -EIO; 280c3b2fdd0SAndreas Klinger } 281c3b2fdd0SAndreas Klinger 282d3bf6045SAndreas Klinger ret = hx711_set_gain_for_channel(hx711_data, chan); 283d3bf6045SAndreas Klinger if (ret < 0) 284c3b2fdd0SAndreas Klinger return ret; 285d3bf6045SAndreas Klinger 286d3bf6045SAndreas Klinger val = hx711_read(hx711_data); 287d3bf6045SAndreas Klinger 288d3bf6045SAndreas Klinger return val; 289c3b2fdd0SAndreas Klinger } 290c3b2fdd0SAndreas Klinger 291d3bf6045SAndreas Klinger static int hx711_read_raw(struct iio_dev *indio_dev, 292d3bf6045SAndreas Klinger const struct iio_chan_spec *chan, 293d3bf6045SAndreas Klinger int *val, int *val2, long mask) 294d3bf6045SAndreas Klinger { 295d3bf6045SAndreas Klinger struct hx711_data *hx711_data = iio_priv(indio_dev); 296d3bf6045SAndreas Klinger 297d3bf6045SAndreas Klinger switch (mask) { 298d3bf6045SAndreas Klinger case IIO_CHAN_INFO_RAW: 299d3bf6045SAndreas Klinger mutex_lock(&hx711_data->lock); 300d3bf6045SAndreas Klinger 301d3bf6045SAndreas Klinger *val = hx711_reset_read(hx711_data, chan->channel); 302c3b2fdd0SAndreas Klinger 303c3b2fdd0SAndreas Klinger mutex_unlock(&hx711_data->lock); 304c3b2fdd0SAndreas Klinger 305c3b2fdd0SAndreas Klinger if (*val < 0) 306c3b2fdd0SAndreas Klinger return *val; 307c3b2fdd0SAndreas Klinger return IIO_VAL_INT; 308c3b2fdd0SAndreas Klinger case IIO_CHAN_INFO_SCALE: 309c3b2fdd0SAndreas Klinger *val = 0; 310c3b2fdd0SAndreas Klinger mutex_lock(&hx711_data->lock); 311c3b2fdd0SAndreas Klinger 312c3b2fdd0SAndreas Klinger *val2 = hx711_get_gain_to_scale(hx711_data->gain_set); 313c3b2fdd0SAndreas Klinger 314c3b2fdd0SAndreas Klinger mutex_unlock(&hx711_data->lock); 315c3b2fdd0SAndreas Klinger 316c3b2fdd0SAndreas Klinger return IIO_VAL_INT_PLUS_NANO; 317c3b2fdd0SAndreas Klinger default: 318c3b2fdd0SAndreas Klinger return -EINVAL; 319c3b2fdd0SAndreas Klinger } 320c3b2fdd0SAndreas Klinger } 321c3b2fdd0SAndreas Klinger 322c3b2fdd0SAndreas Klinger static int hx711_write_raw(struct iio_dev *indio_dev, 323c3b2fdd0SAndreas Klinger struct iio_chan_spec const *chan, 324c3b2fdd0SAndreas Klinger int val, 325c3b2fdd0SAndreas Klinger int val2, 326c3b2fdd0SAndreas Klinger long mask) 327c3b2fdd0SAndreas Klinger { 328c3b2fdd0SAndreas Klinger struct hx711_data *hx711_data = iio_priv(indio_dev); 329c3b2fdd0SAndreas Klinger int ret; 330c3b2fdd0SAndreas Klinger int gain; 331c3b2fdd0SAndreas Klinger 332c3b2fdd0SAndreas Klinger switch (mask) { 333c3b2fdd0SAndreas Klinger case IIO_CHAN_INFO_SCALE: 334c3b2fdd0SAndreas Klinger /* 335c3b2fdd0SAndreas Klinger * a scale greater than 1 mV per LSB is not possible 336c3b2fdd0SAndreas Klinger * with the HX711, therefore val must be 0 337c3b2fdd0SAndreas Klinger */ 338c3b2fdd0SAndreas Klinger if (val != 0) 339c3b2fdd0SAndreas Klinger return -EINVAL; 340c3b2fdd0SAndreas Klinger 341c3b2fdd0SAndreas Klinger mutex_lock(&hx711_data->lock); 342c3b2fdd0SAndreas Klinger 343c3b2fdd0SAndreas Klinger gain = hx711_get_scale_to_gain(val2); 344c3b2fdd0SAndreas Klinger if (gain < 0) { 345c3b2fdd0SAndreas Klinger mutex_unlock(&hx711_data->lock); 346c3b2fdd0SAndreas Klinger return gain; 347c3b2fdd0SAndreas Klinger } 348c3b2fdd0SAndreas Klinger 349c3b2fdd0SAndreas Klinger if (gain != hx711_data->gain_set) { 350c3b2fdd0SAndreas Klinger hx711_data->gain_set = gain; 351c3b2fdd0SAndreas Klinger if (gain != 32) 352c3b2fdd0SAndreas Klinger hx711_data->gain_chan_a = gain; 353c3b2fdd0SAndreas Klinger 354c3b2fdd0SAndreas Klinger ret = hx711_read(hx711_data); 355c3b2fdd0SAndreas Klinger if (ret < 0) { 356c3b2fdd0SAndreas Klinger mutex_unlock(&hx711_data->lock); 357c3b2fdd0SAndreas Klinger return ret; 358c3b2fdd0SAndreas Klinger } 359c3b2fdd0SAndreas Klinger } 360c3b2fdd0SAndreas Klinger 361c3b2fdd0SAndreas Klinger mutex_unlock(&hx711_data->lock); 362c3b2fdd0SAndreas Klinger return 0; 363c3b2fdd0SAndreas Klinger default: 364c3b2fdd0SAndreas Klinger return -EINVAL; 365c3b2fdd0SAndreas Klinger } 366c3b2fdd0SAndreas Klinger 367c3b2fdd0SAndreas Klinger return 0; 368c3b2fdd0SAndreas Klinger } 369c3b2fdd0SAndreas Klinger 370c3b2fdd0SAndreas Klinger static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev, 371c3b2fdd0SAndreas Klinger struct iio_chan_spec const *chan, 372c3b2fdd0SAndreas Klinger long mask) 373c3b2fdd0SAndreas Klinger { 374c3b2fdd0SAndreas Klinger return IIO_VAL_INT_PLUS_NANO; 375c3b2fdd0SAndreas Klinger } 376c3b2fdd0SAndreas Klinger 377d3bf6045SAndreas Klinger static irqreturn_t hx711_trigger(int irq, void *p) 378d3bf6045SAndreas Klinger { 379d3bf6045SAndreas Klinger struct iio_poll_func *pf = p; 380d3bf6045SAndreas Klinger struct iio_dev *indio_dev = pf->indio_dev; 381d3bf6045SAndreas Klinger struct hx711_data *hx711_data = iio_priv(indio_dev); 382d3bf6045SAndreas Klinger int i, j = 0; 383d3bf6045SAndreas Klinger 384d3bf6045SAndreas Klinger mutex_lock(&hx711_data->lock); 385d3bf6045SAndreas Klinger 386d3bf6045SAndreas Klinger memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer)); 387d3bf6045SAndreas Klinger 388d3bf6045SAndreas Klinger for (i = 0; i < indio_dev->masklength; i++) { 389d3bf6045SAndreas Klinger if (!test_bit(i, indio_dev->active_scan_mask)) 390d3bf6045SAndreas Klinger continue; 391d3bf6045SAndreas Klinger 392d3bf6045SAndreas Klinger hx711_data->buffer[j] = hx711_reset_read(hx711_data, 393d3bf6045SAndreas Klinger indio_dev->channels[i].channel); 394d3bf6045SAndreas Klinger j++; 395d3bf6045SAndreas Klinger } 396d3bf6045SAndreas Klinger 397d3bf6045SAndreas Klinger iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer, 398d3bf6045SAndreas Klinger pf->timestamp); 399d3bf6045SAndreas Klinger 400d3bf6045SAndreas Klinger mutex_unlock(&hx711_data->lock); 401d3bf6045SAndreas Klinger 402d3bf6045SAndreas Klinger iio_trigger_notify_done(indio_dev->trig); 403d3bf6045SAndreas Klinger 404d3bf6045SAndreas Klinger return IRQ_HANDLED; 405d3bf6045SAndreas Klinger } 406d3bf6045SAndreas Klinger 407c3b2fdd0SAndreas Klinger static ssize_t hx711_scale_available_show(struct device *dev, 408c3b2fdd0SAndreas Klinger struct device_attribute *attr, 409c3b2fdd0SAndreas Klinger char *buf) 410c3b2fdd0SAndreas Klinger { 411c3b2fdd0SAndreas Klinger struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr); 412c3b2fdd0SAndreas Klinger int channel = iio_attr->address; 413c3b2fdd0SAndreas Klinger int i, len = 0; 414c3b2fdd0SAndreas Klinger 415c3b2fdd0SAndreas Klinger for (i = 0; i < HX711_GAIN_MAX; i++) 416c3b2fdd0SAndreas Klinger if (hx711_gain_to_scale[i].channel == channel) 417c3b2fdd0SAndreas Klinger len += sprintf(buf + len, "0.%09d ", 418c3b2fdd0SAndreas Klinger hx711_gain_to_scale[i].scale); 419c3b2fdd0SAndreas Klinger 420c3b2fdd0SAndreas Klinger len += sprintf(buf + len, "\n"); 421c3b2fdd0SAndreas Klinger 422c3b2fdd0SAndreas Klinger return len; 423c3b2fdd0SAndreas Klinger } 424c3b2fdd0SAndreas Klinger 425c3b2fdd0SAndreas Klinger static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO, 426c3b2fdd0SAndreas Klinger hx711_scale_available_show, NULL, 0); 427c3b2fdd0SAndreas Klinger 428c3b2fdd0SAndreas Klinger static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO, 429c3b2fdd0SAndreas Klinger hx711_scale_available_show, NULL, 1); 430c3b2fdd0SAndreas Klinger 431c3b2fdd0SAndreas Klinger static struct attribute *hx711_attributes[] = { 432c3b2fdd0SAndreas Klinger &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr, 433c3b2fdd0SAndreas Klinger &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr, 434c3b2fdd0SAndreas Klinger NULL, 435c3b2fdd0SAndreas Klinger }; 436c3b2fdd0SAndreas Klinger 4374214dc18Ssimran singhal static const struct attribute_group hx711_attribute_group = { 438c3b2fdd0SAndreas Klinger .attrs = hx711_attributes, 439c3b2fdd0SAndreas Klinger }; 440c3b2fdd0SAndreas Klinger 441c3b2fdd0SAndreas Klinger static const struct iio_info hx711_iio_info = { 442c3b2fdd0SAndreas Klinger .read_raw = hx711_read_raw, 443c3b2fdd0SAndreas Klinger .write_raw = hx711_write_raw, 444c3b2fdd0SAndreas Klinger .write_raw_get_fmt = hx711_write_raw_get_fmt, 445c3b2fdd0SAndreas Klinger .attrs = &hx711_attribute_group, 446c3b2fdd0SAndreas Klinger }; 447c3b2fdd0SAndreas Klinger 448c3b2fdd0SAndreas Klinger static const struct iio_chan_spec hx711_chan_spec[] = { 449c3b2fdd0SAndreas Klinger { 450c3b2fdd0SAndreas Klinger .type = IIO_VOLTAGE, 451c3b2fdd0SAndreas Klinger .channel = 0, 452c3b2fdd0SAndreas Klinger .indexed = 1, 453c3b2fdd0SAndreas Klinger .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 454c3b2fdd0SAndreas Klinger .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 455d3bf6045SAndreas Klinger .scan_index = 0, 456d3bf6045SAndreas Klinger .scan_type = { 457d3bf6045SAndreas Klinger .sign = 'u', 458d3bf6045SAndreas Klinger .realbits = 24, 459d3bf6045SAndreas Klinger .storagebits = 32, 460d3bf6045SAndreas Klinger .endianness = IIO_CPU, 461d3bf6045SAndreas Klinger }, 462c3b2fdd0SAndreas Klinger }, 463c3b2fdd0SAndreas Klinger { 464c3b2fdd0SAndreas Klinger .type = IIO_VOLTAGE, 465c3b2fdd0SAndreas Klinger .channel = 1, 466c3b2fdd0SAndreas Klinger .indexed = 1, 467c3b2fdd0SAndreas Klinger .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 468c3b2fdd0SAndreas Klinger .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 469d3bf6045SAndreas Klinger .scan_index = 1, 470d3bf6045SAndreas Klinger .scan_type = { 471d3bf6045SAndreas Klinger .sign = 'u', 472d3bf6045SAndreas Klinger .realbits = 24, 473d3bf6045SAndreas Klinger .storagebits = 32, 474d3bf6045SAndreas Klinger .endianness = IIO_CPU, 475c3b2fdd0SAndreas Klinger }, 476d3bf6045SAndreas Klinger }, 477d3bf6045SAndreas Klinger IIO_CHAN_SOFT_TIMESTAMP(2), 478c3b2fdd0SAndreas Klinger }; 479c3b2fdd0SAndreas Klinger 480c3b2fdd0SAndreas Klinger static int hx711_probe(struct platform_device *pdev) 481c3b2fdd0SAndreas Klinger { 482c3b2fdd0SAndreas Klinger struct device *dev = &pdev->dev; 483461631faSAndreas Klinger struct device_node *np = dev->of_node; 484c3b2fdd0SAndreas Klinger struct hx711_data *hx711_data; 485c3b2fdd0SAndreas Klinger struct iio_dev *indio_dev; 486c3b2fdd0SAndreas Klinger int ret; 487c3b2fdd0SAndreas Klinger int i; 488c3b2fdd0SAndreas Klinger 489c3b2fdd0SAndreas Klinger indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data)); 490c3b2fdd0SAndreas Klinger if (!indio_dev) { 491c3b2fdd0SAndreas Klinger dev_err(dev, "failed to allocate IIO device\n"); 492c3b2fdd0SAndreas Klinger return -ENOMEM; 493c3b2fdd0SAndreas Klinger } 494c3b2fdd0SAndreas Klinger 495c3b2fdd0SAndreas Klinger hx711_data = iio_priv(indio_dev); 496c3b2fdd0SAndreas Klinger hx711_data->dev = dev; 497c3b2fdd0SAndreas Klinger 498c3b2fdd0SAndreas Klinger mutex_init(&hx711_data->lock); 499c3b2fdd0SAndreas Klinger 500c3b2fdd0SAndreas Klinger /* 501c3b2fdd0SAndreas Klinger * PD_SCK stands for power down and serial clock input of HX711 502c3b2fdd0SAndreas Klinger * in the driver it is an output 503c3b2fdd0SAndreas Klinger */ 504c3b2fdd0SAndreas Klinger hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); 505c3b2fdd0SAndreas Klinger if (IS_ERR(hx711_data->gpiod_pd_sck)) { 506c3b2fdd0SAndreas Klinger dev_err(dev, "failed to get sck-gpiod: err=%ld\n", 507c3b2fdd0SAndreas Klinger PTR_ERR(hx711_data->gpiod_pd_sck)); 508c3b2fdd0SAndreas Klinger return PTR_ERR(hx711_data->gpiod_pd_sck); 509c3b2fdd0SAndreas Klinger } 510c3b2fdd0SAndreas Klinger 511c3b2fdd0SAndreas Klinger /* 512c3b2fdd0SAndreas Klinger * DOUT stands for serial data output of HX711 513c3b2fdd0SAndreas Klinger * for the driver it is an input 514c3b2fdd0SAndreas Klinger */ 515c3b2fdd0SAndreas Klinger hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN); 516c3b2fdd0SAndreas Klinger if (IS_ERR(hx711_data->gpiod_dout)) { 517c3b2fdd0SAndreas Klinger dev_err(dev, "failed to get dout-gpiod: err=%ld\n", 518c3b2fdd0SAndreas Klinger PTR_ERR(hx711_data->gpiod_dout)); 519c3b2fdd0SAndreas Klinger return PTR_ERR(hx711_data->gpiod_dout); 520c3b2fdd0SAndreas Klinger } 521c3b2fdd0SAndreas Klinger 522c3b2fdd0SAndreas Klinger hx711_data->reg_avdd = devm_regulator_get(dev, "avdd"); 523c3b2fdd0SAndreas Klinger if (IS_ERR(hx711_data->reg_avdd)) 524c3b2fdd0SAndreas Klinger return PTR_ERR(hx711_data->reg_avdd); 525c3b2fdd0SAndreas Klinger 526c3b2fdd0SAndreas Klinger ret = regulator_enable(hx711_data->reg_avdd); 527c3b2fdd0SAndreas Klinger if (ret < 0) 528c3b2fdd0SAndreas Klinger return ret; 529c3b2fdd0SAndreas Klinger 530c3b2fdd0SAndreas Klinger /* 531c3b2fdd0SAndreas Klinger * with 532c3b2fdd0SAndreas Klinger * full scale differential input range: AVDD / GAIN 533c3b2fdd0SAndreas Klinger * full scale output data: 2^24 534c3b2fdd0SAndreas Klinger * we can say: 535c3b2fdd0SAndreas Klinger * AVDD / GAIN = 2^24 536c3b2fdd0SAndreas Klinger * therefore: 537c3b2fdd0SAndreas Klinger * 1 LSB = AVDD / GAIN / 2^24 538c3b2fdd0SAndreas Klinger * AVDD is in uV, but we need 10^-9 mV 539c3b2fdd0SAndreas Klinger * approximately to fit into a 32 bit number: 540c3b2fdd0SAndreas Klinger * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV] 541c3b2fdd0SAndreas Klinger */ 542c3b2fdd0SAndreas Klinger ret = regulator_get_voltage(hx711_data->reg_avdd); 543d3bf6045SAndreas Klinger if (ret < 0) 544d3bf6045SAndreas Klinger goto error_regulator; 545d3bf6045SAndreas Klinger 546c3b2fdd0SAndreas Klinger /* we need 10^-9 mV */ 547c3b2fdd0SAndreas Klinger ret *= 100; 548c3b2fdd0SAndreas Klinger 549c3b2fdd0SAndreas Klinger for (i = 0; i < HX711_GAIN_MAX; i++) 550c3b2fdd0SAndreas Klinger hx711_gain_to_scale[i].scale = 551c3b2fdd0SAndreas Klinger ret / hx711_gain_to_scale[i].gain / 1678; 552c3b2fdd0SAndreas Klinger 553c3b2fdd0SAndreas Klinger hx711_data->gain_set = 128; 554c3b2fdd0SAndreas Klinger hx711_data->gain_chan_a = 128; 555c3b2fdd0SAndreas Klinger 556461631faSAndreas Klinger hx711_data->clock_frequency = 400000; 557461631faSAndreas Klinger ret = of_property_read_u32(np, "clock-frequency", 558461631faSAndreas Klinger &hx711_data->clock_frequency); 559461631faSAndreas Klinger 560461631faSAndreas Klinger /* 561461631faSAndreas Klinger * datasheet says the high level of PD_SCK has a maximum duration 562461631faSAndreas Klinger * of 50 microseconds 563461631faSAndreas Klinger */ 564461631faSAndreas Klinger if (hx711_data->clock_frequency < 20000) { 565461631faSAndreas Klinger dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n"); 566461631faSAndreas Klinger hx711_data->clock_frequency = 400000; 567461631faSAndreas Klinger } 568461631faSAndreas Klinger 569461631faSAndreas Klinger hx711_data->data_ready_delay_ns = 570461631faSAndreas Klinger 1000000000 / hx711_data->clock_frequency; 571461631faSAndreas Klinger 572c3b2fdd0SAndreas Klinger platform_set_drvdata(pdev, indio_dev); 573c3b2fdd0SAndreas Klinger 574c3b2fdd0SAndreas Klinger indio_dev->name = "hx711"; 575c3b2fdd0SAndreas Klinger indio_dev->dev.parent = &pdev->dev; 576c3b2fdd0SAndreas Klinger indio_dev->info = &hx711_iio_info; 577c3b2fdd0SAndreas Klinger indio_dev->modes = INDIO_DIRECT_MODE; 578c3b2fdd0SAndreas Klinger indio_dev->channels = hx711_chan_spec; 579c3b2fdd0SAndreas Klinger indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec); 580c3b2fdd0SAndreas Klinger 581d3bf6045SAndreas Klinger ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time, 582d3bf6045SAndreas Klinger hx711_trigger, NULL); 583d3bf6045SAndreas Klinger if (ret < 0) { 584d3bf6045SAndreas Klinger dev_err(dev, "setup of iio triggered buffer failed\n"); 585d3bf6045SAndreas Klinger goto error_regulator; 586d3bf6045SAndreas Klinger } 587d3bf6045SAndreas Klinger 588c3b2fdd0SAndreas Klinger ret = iio_device_register(indio_dev); 589c3b2fdd0SAndreas Klinger if (ret < 0) { 590c3b2fdd0SAndreas Klinger dev_err(dev, "Couldn't register the device\n"); 591d3bf6045SAndreas Klinger goto error_buffer; 592c3b2fdd0SAndreas Klinger } 593c3b2fdd0SAndreas Klinger 594d3bf6045SAndreas Klinger return 0; 595d3bf6045SAndreas Klinger 596d3bf6045SAndreas Klinger error_buffer: 597d3bf6045SAndreas Klinger iio_triggered_buffer_cleanup(indio_dev); 598d3bf6045SAndreas Klinger 599d3bf6045SAndreas Klinger error_regulator: 600d3bf6045SAndreas Klinger regulator_disable(hx711_data->reg_avdd); 601d3bf6045SAndreas Klinger 602c3b2fdd0SAndreas Klinger return ret; 603c3b2fdd0SAndreas Klinger } 604c3b2fdd0SAndreas Klinger 605c3b2fdd0SAndreas Klinger static int hx711_remove(struct platform_device *pdev) 606c3b2fdd0SAndreas Klinger { 607c3b2fdd0SAndreas Klinger struct hx711_data *hx711_data; 608c3b2fdd0SAndreas Klinger struct iio_dev *indio_dev; 609c3b2fdd0SAndreas Klinger 610c3b2fdd0SAndreas Klinger indio_dev = platform_get_drvdata(pdev); 611c3b2fdd0SAndreas Klinger hx711_data = iio_priv(indio_dev); 612c3b2fdd0SAndreas Klinger 613c3b2fdd0SAndreas Klinger iio_device_unregister(indio_dev); 614c3b2fdd0SAndreas Klinger 615d3bf6045SAndreas Klinger iio_triggered_buffer_cleanup(indio_dev); 616d3bf6045SAndreas Klinger 617c3b2fdd0SAndreas Klinger regulator_disable(hx711_data->reg_avdd); 618c3b2fdd0SAndreas Klinger 619c3b2fdd0SAndreas Klinger return 0; 620c3b2fdd0SAndreas Klinger } 621c3b2fdd0SAndreas Klinger 622c3b2fdd0SAndreas Klinger static const struct of_device_id of_hx711_match[] = { 623c3b2fdd0SAndreas Klinger { .compatible = "avia,hx711", }, 624c3b2fdd0SAndreas Klinger {}, 625c3b2fdd0SAndreas Klinger }; 626c3b2fdd0SAndreas Klinger 627c3b2fdd0SAndreas Klinger MODULE_DEVICE_TABLE(of, of_hx711_match); 628c3b2fdd0SAndreas Klinger 629c3b2fdd0SAndreas Klinger static struct platform_driver hx711_driver = { 630c3b2fdd0SAndreas Klinger .probe = hx711_probe, 631c3b2fdd0SAndreas Klinger .remove = hx711_remove, 632c3b2fdd0SAndreas Klinger .driver = { 633c3b2fdd0SAndreas Klinger .name = "hx711-gpio", 634c3b2fdd0SAndreas Klinger .of_match_table = of_hx711_match, 635c3b2fdd0SAndreas Klinger }, 636c3b2fdd0SAndreas Klinger }; 637c3b2fdd0SAndreas Klinger 638c3b2fdd0SAndreas Klinger module_platform_driver(hx711_driver); 639c3b2fdd0SAndreas Klinger 640c3b2fdd0SAndreas Klinger MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>"); 641c3b2fdd0SAndreas Klinger MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells"); 642c3b2fdd0SAndreas Klinger MODULE_LICENSE("GPL"); 643c3b2fdd0SAndreas Klinger MODULE_ALIAS("platform:hx711-gpio"); 644c3b2fdd0SAndreas Klinger 645