cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

hx711.c (14202B)


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