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