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