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

lis3lv02d_i2c.c (6828B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * drivers/hwmon/lis3lv02d_i2c.c
      4 *
      5 * Implements I2C interface for lis3lv02d (STMicroelectronics) accelerometer.
      6 * Driver is based on corresponding SPI driver written by Daniel Mack
      7 * (lis3lv02d_spi.c (C) 2009 Daniel Mack <daniel@caiaq.de> ).
      8 *
      9 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     10 *
     11 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
     12 */
     13
     14#include <linux/module.h>
     15#include <linux/kernel.h>
     16#include <linux/err.h>
     17#include <linux/i2c.h>
     18#include <linux/pm_runtime.h>
     19#include <linux/delay.h>
     20#include <linux/of.h>
     21#include <linux/of_platform.h>
     22#include <linux/of_device.h>
     23
     24#include "lis3lv02d.h"
     25
     26#define DRV_NAME	"lis3lv02d_i2c"
     27
     28static const char reg_vdd[]    = "Vdd";
     29static const char reg_vdd_io[] = "Vdd_IO";
     30
     31static int lis3_reg_ctrl(struct lis3lv02d *lis3, bool state)
     32{
     33	int ret;
     34	if (state == LIS3_REG_OFF) {
     35		ret = regulator_bulk_disable(ARRAY_SIZE(lis3->regulators),
     36					lis3->regulators);
     37	} else {
     38		ret = regulator_bulk_enable(ARRAY_SIZE(lis3->regulators),
     39					lis3->regulators);
     40		/* Chip needs time to wakeup. Not mentioned in datasheet */
     41		usleep_range(10000, 20000);
     42	}
     43	return ret;
     44}
     45
     46static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
     47{
     48	struct i2c_client *c = lis3->bus_priv;
     49	return i2c_smbus_write_byte_data(c, reg, value);
     50}
     51
     52static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
     53{
     54	struct i2c_client *c = lis3->bus_priv;
     55	*v = i2c_smbus_read_byte_data(c, reg);
     56	return 0;
     57}
     58
     59static inline s32 lis3_i2c_blockread(struct lis3lv02d *lis3, int reg, int len,
     60				u8 *v)
     61{
     62	struct i2c_client *c = lis3->bus_priv;
     63	reg |= (1 << 7); /* 7th bit enables address auto incrementation */
     64	return i2c_smbus_read_i2c_block_data(c, reg, len, v);
     65}
     66
     67static int lis3_i2c_init(struct lis3lv02d *lis3)
     68{
     69	u8 reg;
     70	int ret;
     71
     72	lis3_reg_ctrl(lis3, LIS3_REG_ON);
     73
     74	lis3->read(lis3, WHO_AM_I, &reg);
     75	if (reg != lis3->whoami)
     76		printk(KERN_ERR "lis3: power on failure\n");
     77
     78	/* power up the device */
     79	ret = lis3->read(lis3, CTRL_REG1, &reg);
     80	if (ret < 0)
     81		return ret;
     82
     83	if (lis3->whoami == WAI_3DLH)
     84		reg |= CTRL1_PM0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
     85	else
     86		reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
     87
     88	return lis3->write(lis3, CTRL_REG1, reg);
     89}
     90
     91/* Default axis mapping but it can be overwritten by platform data */
     92static union axis_conversion lis3lv02d_axis_map =
     93	{ .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
     94
     95#ifdef CONFIG_OF
     96static const struct of_device_id lis3lv02d_i2c_dt_ids[] = {
     97	{ .compatible = "st,lis3lv02d" },
     98	{}
     99};
    100MODULE_DEVICE_TABLE(of, lis3lv02d_i2c_dt_ids);
    101#endif
    102
    103static int lis3lv02d_i2c_probe(struct i2c_client *client,
    104					const struct i2c_device_id *id)
    105{
    106	int ret = 0;
    107	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
    108
    109#ifdef CONFIG_OF
    110	if (of_match_device(lis3lv02d_i2c_dt_ids, &client->dev)) {
    111		lis3_dev.of_node = client->dev.of_node;
    112		ret = lis3lv02d_init_dt(&lis3_dev);
    113		if (ret)
    114			return ret;
    115		pdata = lis3_dev.pdata;
    116	}
    117#endif
    118
    119	if (pdata) {
    120		if ((pdata->driver_features & LIS3_USE_BLOCK_READ) &&
    121			(i2c_check_functionality(client->adapter,
    122						I2C_FUNC_SMBUS_I2C_BLOCK)))
    123			lis3_dev.blkread  = lis3_i2c_blockread;
    124
    125		if (pdata->axis_x)
    126			lis3lv02d_axis_map.x = pdata->axis_x;
    127
    128		if (pdata->axis_y)
    129			lis3lv02d_axis_map.y = pdata->axis_y;
    130
    131		if (pdata->axis_z)
    132			lis3lv02d_axis_map.z = pdata->axis_z;
    133
    134		if (pdata->setup_resources)
    135			ret = pdata->setup_resources();
    136
    137		if (ret)
    138			goto fail;
    139	}
    140
    141	lis3_dev.regulators[0].supply = reg_vdd;
    142	lis3_dev.regulators[1].supply = reg_vdd_io;
    143	ret = regulator_bulk_get(&client->dev,
    144				 ARRAY_SIZE(lis3_dev.regulators),
    145				 lis3_dev.regulators);
    146	if (ret < 0)
    147		goto fail;
    148
    149	lis3_dev.pdata	  = pdata;
    150	lis3_dev.bus_priv = client;
    151	lis3_dev.init	  = lis3_i2c_init;
    152	lis3_dev.read	  = lis3_i2c_read;
    153	lis3_dev.write	  = lis3_i2c_write;
    154	lis3_dev.irq	  = client->irq;
    155	lis3_dev.ac	  = lis3lv02d_axis_map;
    156	lis3_dev.pm_dev	  = &client->dev;
    157
    158	i2c_set_clientdata(client, &lis3_dev);
    159
    160	/* Provide power over the init call */
    161	lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON);
    162
    163	ret = lis3lv02d_init_device(&lis3_dev);
    164
    165	lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF);
    166
    167	if (ret)
    168		goto fail2;
    169	return 0;
    170
    171fail2:
    172	regulator_bulk_free(ARRAY_SIZE(lis3_dev.regulators),
    173				lis3_dev.regulators);
    174fail:
    175	if (pdata && pdata->release_resources)
    176		pdata->release_resources();
    177	return ret;
    178}
    179
    180static int lis3lv02d_i2c_remove(struct i2c_client *client)
    181{
    182	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
    183	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
    184
    185	if (pdata && pdata->release_resources)
    186		pdata->release_resources();
    187
    188	lis3lv02d_joystick_disable(lis3);
    189	lis3lv02d_remove_fs(&lis3_dev);
    190
    191	regulator_bulk_free(ARRAY_SIZE(lis3->regulators),
    192			    lis3_dev.regulators);
    193	return 0;
    194}
    195
    196#ifdef CONFIG_PM_SLEEP
    197static int lis3lv02d_i2c_suspend(struct device *dev)
    198{
    199	struct i2c_client *client = to_i2c_client(dev);
    200	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
    201
    202	if (!lis3->pdata || !lis3->pdata->wakeup_flags)
    203		lis3lv02d_poweroff(lis3);
    204	return 0;
    205}
    206
    207static int lis3lv02d_i2c_resume(struct device *dev)
    208{
    209	struct i2c_client *client = to_i2c_client(dev);
    210	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
    211
    212	/*
    213	 * pm_runtime documentation says that devices should always
    214	 * be powered on at resume. Pm_runtime turns them off after system
    215	 * wide resume is complete.
    216	 */
    217	if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
    218		pm_runtime_suspended(dev))
    219		lis3lv02d_poweron(lis3);
    220
    221	return 0;
    222}
    223#endif /* CONFIG_PM_SLEEP */
    224
    225#ifdef CONFIG_PM
    226static int lis3_i2c_runtime_suspend(struct device *dev)
    227{
    228	struct i2c_client *client = to_i2c_client(dev);
    229	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
    230
    231	lis3lv02d_poweroff(lis3);
    232	return 0;
    233}
    234
    235static int lis3_i2c_runtime_resume(struct device *dev)
    236{
    237	struct i2c_client *client = to_i2c_client(dev);
    238	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
    239
    240	lis3lv02d_poweron(lis3);
    241	return 0;
    242}
    243#endif /* CONFIG_PM */
    244
    245static const struct i2c_device_id lis3lv02d_id[] = {
    246	{"lis3lv02d", LIS3LV02D},
    247	{"lis331dlh", LIS331DLH},
    248	{}
    249};
    250
    251MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
    252
    253static const struct dev_pm_ops lis3_pm_ops = {
    254	SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
    255				lis3lv02d_i2c_resume)
    256	SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
    257			   lis3_i2c_runtime_resume,
    258			   NULL)
    259};
    260
    261static struct i2c_driver lis3lv02d_i2c_driver = {
    262	.driver	 = {
    263		.name   = DRV_NAME,
    264		.pm     = &lis3_pm_ops,
    265		.of_match_table = of_match_ptr(lis3lv02d_i2c_dt_ids),
    266	},
    267	.probe	= lis3lv02d_i2c_probe,
    268	.remove	= lis3lv02d_i2c_remove,
    269	.id_table = lis3lv02d_id,
    270};
    271
    272module_i2c_driver(lis3lv02d_i2c_driver);
    273
    274MODULE_AUTHOR("Nokia Corporation");
    275MODULE_DESCRIPTION("lis3lv02d I2C interface");
    276MODULE_LICENSE("GPL");