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

itg3200_core.c (9494B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * itg3200_core.c -- support InvenSense ITG3200
      4 *                   Digital 3-Axis Gyroscope driver
      5 *
      6 * Copyright (c) 2011 Christian Strobel <christian.strobel@iis.fraunhofer.de>
      7 * Copyright (c) 2011 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
      8 * Copyright (c) 2012 Thorsten Nowak <thorsten.nowak@iis.fraunhofer.de>
      9 *
     10 * TODO:
     11 * - Support digital low pass filter
     12 * - Support power management
     13 */
     14
     15#include <linux/interrupt.h>
     16#include <linux/irq.h>
     17#include <linux/i2c.h>
     18#include <linux/slab.h>
     19#include <linux/stat.h>
     20#include <linux/module.h>
     21#include <linux/delay.h>
     22
     23#include <linux/iio/iio.h>
     24#include <linux/iio/sysfs.h>
     25#include <linux/iio/events.h>
     26#include <linux/iio/buffer.h>
     27
     28#include <linux/iio/gyro/itg3200.h>
     29
     30
     31int itg3200_write_reg_8(struct iio_dev *indio_dev,
     32		u8 reg_address, u8 val)
     33{
     34	struct itg3200 *st = iio_priv(indio_dev);
     35
     36	return i2c_smbus_write_byte_data(st->i2c, 0x80 | reg_address, val);
     37}
     38
     39int itg3200_read_reg_8(struct iio_dev *indio_dev,
     40		u8 reg_address, u8 *val)
     41{
     42	struct itg3200 *st = iio_priv(indio_dev);
     43	int ret;
     44
     45	ret = i2c_smbus_read_byte_data(st->i2c, reg_address);
     46	if (ret < 0)
     47		return ret;
     48	*val = ret;
     49	return 0;
     50}
     51
     52static int itg3200_read_reg_s16(struct iio_dev *indio_dev, u8 lower_reg_address,
     53		int *val)
     54{
     55	struct itg3200 *st = iio_priv(indio_dev);
     56	struct i2c_client *client = st->i2c;
     57	int ret;
     58	s16 out;
     59
     60	struct i2c_msg msg[2] = {
     61		{
     62			.addr = client->addr,
     63			.flags = client->flags,
     64			.len = 1,
     65			.buf = (char *)&lower_reg_address,
     66		},
     67		{
     68			.addr = client->addr,
     69			.flags = client->flags | I2C_M_RD,
     70			.len = 2,
     71			.buf = (char *)&out,
     72		},
     73	};
     74
     75	lower_reg_address |= 0x80;
     76	ret = i2c_transfer(client->adapter, msg, 2);
     77	be16_to_cpus(&out);
     78	*val = out;
     79
     80	return (ret == 2) ? 0 : ret;
     81}
     82
     83static int itg3200_read_raw(struct iio_dev *indio_dev,
     84		const struct iio_chan_spec *chan,
     85		int *val, int *val2, long info)
     86{
     87	int ret = 0;
     88	u8 reg;
     89	u8 regval;
     90
     91	switch (info) {
     92	case IIO_CHAN_INFO_RAW:
     93		reg = (u8)chan->address;
     94		ret = itg3200_read_reg_s16(indio_dev, reg, val);
     95		return IIO_VAL_INT;
     96	case IIO_CHAN_INFO_SCALE:
     97		*val = 0;
     98		if (chan->type == IIO_TEMP)
     99			*val2 = 1000000000/280;
    100		else
    101			*val2 = 1214142; /* (1 / 14,375) * (PI / 180) */
    102		return IIO_VAL_INT_PLUS_NANO;
    103	case IIO_CHAN_INFO_OFFSET:
    104		/* Only the temperature channel has an offset */
    105		*val = 23000;
    106		return IIO_VAL_INT;
    107	case IIO_CHAN_INFO_SAMP_FREQ:
    108		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &regval);
    109		if (ret)
    110			return ret;
    111
    112		*val = (regval & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000;
    113
    114		ret = itg3200_read_reg_8(indio_dev,
    115					 ITG3200_REG_SAMPLE_RATE_DIV,
    116					 &regval);
    117		if (ret)
    118			return ret;
    119
    120		*val /= regval + 1;
    121		return IIO_VAL_INT;
    122
    123	default:
    124		return -EINVAL;
    125	}
    126}
    127
    128static int itg3200_write_raw(struct iio_dev *indio_dev,
    129			     struct iio_chan_spec const *chan,
    130			     int val,
    131			     int val2,
    132			     long mask)
    133{
    134	int ret;
    135	u8 t;
    136
    137	switch (mask) {
    138	case IIO_CHAN_INFO_SAMP_FREQ:
    139		if (val == 0 || val2 != 0)
    140			return -EINVAL;
    141
    142		mutex_lock(&indio_dev->mlock);
    143
    144		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
    145		if (ret) {
    146			mutex_unlock(&indio_dev->mlock);
    147			return ret;
    148		}
    149		t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
    150
    151		ret = itg3200_write_reg_8(indio_dev,
    152					  ITG3200_REG_SAMPLE_RATE_DIV,
    153					  t);
    154
    155		mutex_unlock(&indio_dev->mlock);
    156		return ret;
    157
    158	default:
    159		return -EINVAL;
    160	}
    161}
    162
    163/*
    164 * Reset device and internal registers to the power-up-default settings
    165 * Use the gyro clock as reference, as suggested by the datasheet
    166 */
    167static int itg3200_reset(struct iio_dev *indio_dev)
    168{
    169	struct itg3200 *st = iio_priv(indio_dev);
    170	int ret;
    171
    172	dev_dbg(&st->i2c->dev, "reset device");
    173
    174	ret = itg3200_write_reg_8(indio_dev,
    175			ITG3200_REG_POWER_MANAGEMENT,
    176			ITG3200_RESET);
    177	if (ret) {
    178		dev_err(&st->i2c->dev, "error resetting device");
    179		goto error_ret;
    180	}
    181
    182	/* Wait for PLL (1ms according to datasheet) */
    183	udelay(1500);
    184
    185	ret = itg3200_write_reg_8(indio_dev,
    186			ITG3200_REG_IRQ_CONFIG,
    187			ITG3200_IRQ_ACTIVE_HIGH |
    188			ITG3200_IRQ_PUSH_PULL |
    189			ITG3200_IRQ_LATCH_50US_PULSE |
    190			ITG3200_IRQ_LATCH_CLEAR_ANY);
    191
    192	if (ret)
    193		dev_err(&st->i2c->dev, "error init device");
    194
    195error_ret:
    196	return ret;
    197}
    198
    199/* itg3200_enable_full_scale() - Disables the digital low pass filter */
    200static int itg3200_enable_full_scale(struct iio_dev *indio_dev)
    201{
    202	u8 val;
    203	int ret;
    204
    205	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
    206	if (ret)
    207		goto err_ret;
    208
    209	val |= ITG3200_DLPF_FS_SEL_2000;
    210	return itg3200_write_reg_8(indio_dev, ITG3200_REG_DLPF, val);
    211
    212err_ret:
    213	return ret;
    214}
    215
    216static int itg3200_initial_setup(struct iio_dev *indio_dev)
    217{
    218	struct itg3200 *st = iio_priv(indio_dev);
    219	int ret;
    220	u8 val;
    221
    222	ret = itg3200_reset(indio_dev);
    223	if (ret)
    224		goto err_ret;
    225
    226	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val);
    227	if (ret)
    228		goto err_ret;
    229
    230	if (((val >> 1) & 0x3f) != 0x34) {
    231		dev_err(&st->i2c->dev, "invalid reg value 0x%02x", val);
    232		ret = -ENXIO;
    233		goto err_ret;
    234	}
    235
    236	ret = itg3200_enable_full_scale(indio_dev);
    237err_ret:
    238	return ret;
    239}
    240
    241static const struct iio_mount_matrix *
    242itg3200_get_mount_matrix(const struct iio_dev *indio_dev,
    243			  const struct iio_chan_spec *chan)
    244{
    245	struct itg3200 *data = iio_priv(indio_dev);
    246
    247	return &data->orientation;
    248}
    249
    250static const struct iio_chan_spec_ext_info itg3200_ext_info[] = {
    251	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, itg3200_get_mount_matrix),
    252	{ }
    253};
    254
    255#define ITG3200_ST						\
    256	{ .sign = 's', .realbits = 16, .storagebits = 16, .endianness = IIO_BE }
    257
    258#define ITG3200_GYRO_CHAN(_mod) { \
    259	.type = IIO_ANGL_VEL, \
    260	.modified = 1, \
    261	.channel2 = IIO_MOD_ ## _mod, \
    262	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
    263	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
    264	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
    265	.address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
    266	.scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
    267	.scan_type = ITG3200_ST, \
    268	.ext_info = itg3200_ext_info, \
    269}
    270
    271static const struct iio_chan_spec itg3200_channels[] = {
    272	{
    273		.type = IIO_TEMP,
    274		.channel2 = IIO_NO_MOD,
    275		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
    276		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
    277		BIT(IIO_CHAN_INFO_SCALE),
    278		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
    279		.address = ITG3200_REG_TEMP_OUT_H,
    280		.scan_index = ITG3200_SCAN_TEMP,
    281		.scan_type = ITG3200_ST,
    282	},
    283	ITG3200_GYRO_CHAN(X),
    284	ITG3200_GYRO_CHAN(Y),
    285	ITG3200_GYRO_CHAN(Z),
    286	IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
    287};
    288
    289static const struct iio_info itg3200_info = {
    290	.read_raw = &itg3200_read_raw,
    291	.write_raw = &itg3200_write_raw,
    292};
    293
    294static const unsigned long itg3200_available_scan_masks[] = { 0xffffffff, 0x0 };
    295
    296static int itg3200_probe(struct i2c_client *client,
    297		const struct i2c_device_id *id)
    298{
    299	int ret;
    300	struct itg3200 *st;
    301	struct iio_dev *indio_dev;
    302
    303	dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq);
    304
    305	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
    306	if (!indio_dev)
    307		return -ENOMEM;
    308
    309	st = iio_priv(indio_dev);
    310
    311	ret = iio_read_mount_matrix(&client->dev, &st->orientation);
    312	if (ret)
    313		return ret;
    314
    315	i2c_set_clientdata(client, indio_dev);
    316	st->i2c = client;
    317
    318	indio_dev->name = client->dev.driver->name;
    319	indio_dev->channels = itg3200_channels;
    320	indio_dev->num_channels = ARRAY_SIZE(itg3200_channels);
    321	indio_dev->available_scan_masks = itg3200_available_scan_masks;
    322	indio_dev->info = &itg3200_info;
    323	indio_dev->modes = INDIO_DIRECT_MODE;
    324
    325	ret = itg3200_buffer_configure(indio_dev);
    326	if (ret)
    327		return ret;
    328
    329	if (client->irq) {
    330		ret = itg3200_probe_trigger(indio_dev);
    331		if (ret)
    332			goto error_unconfigure_buffer;
    333	}
    334
    335	ret = itg3200_initial_setup(indio_dev);
    336	if (ret)
    337		goto error_remove_trigger;
    338
    339	ret = iio_device_register(indio_dev);
    340	if (ret)
    341		goto error_remove_trigger;
    342
    343	return 0;
    344
    345error_remove_trigger:
    346	if (client->irq)
    347		itg3200_remove_trigger(indio_dev);
    348error_unconfigure_buffer:
    349	itg3200_buffer_unconfigure(indio_dev);
    350	return ret;
    351}
    352
    353static int itg3200_remove(struct i2c_client *client)
    354{
    355	struct iio_dev *indio_dev = i2c_get_clientdata(client);
    356
    357	iio_device_unregister(indio_dev);
    358
    359	if (client->irq)
    360		itg3200_remove_trigger(indio_dev);
    361
    362	itg3200_buffer_unconfigure(indio_dev);
    363
    364	return 0;
    365}
    366
    367static int __maybe_unused itg3200_suspend(struct device *dev)
    368{
    369	struct iio_dev *indio_dev = dev_get_drvdata(dev);
    370	struct itg3200 *st = iio_priv(indio_dev);
    371
    372	dev_dbg(&st->i2c->dev, "suspend device");
    373
    374	return itg3200_write_reg_8(indio_dev, ITG3200_REG_POWER_MANAGEMENT,
    375				   ITG3200_SLEEP);
    376}
    377
    378static int __maybe_unused itg3200_resume(struct device *dev)
    379{
    380	struct iio_dev *indio_dev = dev_get_drvdata(dev);
    381
    382	return itg3200_initial_setup(indio_dev);
    383}
    384
    385static SIMPLE_DEV_PM_OPS(itg3200_pm_ops, itg3200_suspend, itg3200_resume);
    386
    387static const struct i2c_device_id itg3200_id[] = {
    388	{ "itg3200", 0 },
    389	{ }
    390};
    391MODULE_DEVICE_TABLE(i2c, itg3200_id);
    392
    393static const struct of_device_id itg3200_of_match[] = {
    394	{ .compatible = "invensense,itg3200" },
    395	{ }
    396};
    397MODULE_DEVICE_TABLE(of, itg3200_of_match);
    398
    399static struct i2c_driver itg3200_driver = {
    400	.driver = {
    401		.name	= "itg3200",
    402		.of_match_table = itg3200_of_match,
    403		.pm	= &itg3200_pm_ops,
    404	},
    405	.id_table	= itg3200_id,
    406	.probe		= itg3200_probe,
    407	.remove		= itg3200_remove,
    408};
    409
    410module_i2c_driver(itg3200_driver);
    411
    412MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
    413MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
    414MODULE_LICENSE("GPL v2");