18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * drivers/hwmon/lis3lv02d_i2c.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Implements I2C interface for lis3lv02d (STMicroelectronics) accelerometer.
68c2ecf20Sopenharmony_ci * Driver is based on corresponding SPI driver written by Daniel Mack
78c2ecf20Sopenharmony_ci * (lis3lv02d_spi.c (C) 2009 Daniel Mack <daniel@caiaq.de> ).
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
198c2ecf20Sopenharmony_ci#include <linux/delay.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
228c2ecf20Sopenharmony_ci#include <linux/of_device.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "lis3lv02d.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define DRV_NAME	"lis3lv02d_i2c"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic const char reg_vdd[]    = "Vdd";
298c2ecf20Sopenharmony_cistatic const char reg_vdd_io[] = "Vdd_IO";
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic int lis3_reg_ctrl(struct lis3lv02d *lis3, bool state)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	int ret;
348c2ecf20Sopenharmony_ci	if (state == LIS3_REG_OFF) {
358c2ecf20Sopenharmony_ci		ret = regulator_bulk_disable(ARRAY_SIZE(lis3->regulators),
368c2ecf20Sopenharmony_ci					lis3->regulators);
378c2ecf20Sopenharmony_ci	} else {
388c2ecf20Sopenharmony_ci		ret = regulator_bulk_enable(ARRAY_SIZE(lis3->regulators),
398c2ecf20Sopenharmony_ci					lis3->regulators);
408c2ecf20Sopenharmony_ci		/* Chip needs time to wakeup. Not mentioned in datasheet */
418c2ecf20Sopenharmony_ci		usleep_range(10000, 20000);
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci	return ret;
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct i2c_client *c = lis3->bus_priv;
498c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(c, reg, value);
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct i2c_client *c = lis3->bus_priv;
558c2ecf20Sopenharmony_ci	*v = i2c_smbus_read_byte_data(c, reg);
568c2ecf20Sopenharmony_ci	return 0;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic inline s32 lis3_i2c_blockread(struct lis3lv02d *lis3, int reg, int len,
608c2ecf20Sopenharmony_ci				u8 *v)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct i2c_client *c = lis3->bus_priv;
638c2ecf20Sopenharmony_ci	reg |= (1 << 7); /* 7th bit enables address auto incrementation */
648c2ecf20Sopenharmony_ci	return i2c_smbus_read_i2c_block_data(c, reg, len, v);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int lis3_i2c_init(struct lis3lv02d *lis3)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	u8 reg;
708c2ecf20Sopenharmony_ci	int ret;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	lis3_reg_ctrl(lis3, LIS3_REG_ON);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	lis3->read(lis3, WHO_AM_I, &reg);
758c2ecf20Sopenharmony_ci	if (reg != lis3->whoami)
768c2ecf20Sopenharmony_ci		printk(KERN_ERR "lis3: power on failure\n");
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* power up the device */
798c2ecf20Sopenharmony_ci	ret = lis3->read(lis3, CTRL_REG1, &reg);
808c2ecf20Sopenharmony_ci	if (ret < 0)
818c2ecf20Sopenharmony_ci		return ret;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (lis3->whoami == WAI_3DLH)
848c2ecf20Sopenharmony_ci		reg |= CTRL1_PM0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
858c2ecf20Sopenharmony_ci	else
868c2ecf20Sopenharmony_ci		reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return lis3->write(lis3, CTRL_REG1, reg);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* Default axis mapping but it can be overwritten by platform data */
928c2ecf20Sopenharmony_cistatic union axis_conversion lis3lv02d_axis_map =
938c2ecf20Sopenharmony_ci	{ .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
968c2ecf20Sopenharmony_cistatic const struct of_device_id lis3lv02d_i2c_dt_ids[] = {
978c2ecf20Sopenharmony_ci	{ .compatible = "st,lis3lv02d" },
988c2ecf20Sopenharmony_ci	{}
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, lis3lv02d_i2c_dt_ids);
1018c2ecf20Sopenharmony_ci#endif
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic int lis3lv02d_i2c_probe(struct i2c_client *client,
1048c2ecf20Sopenharmony_ci					const struct i2c_device_id *id)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	int ret = 0;
1078c2ecf20Sopenharmony_ci	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
1108c2ecf20Sopenharmony_ci	if (of_match_device(lis3lv02d_i2c_dt_ids, &client->dev)) {
1118c2ecf20Sopenharmony_ci		lis3_dev.of_node = client->dev.of_node;
1128c2ecf20Sopenharmony_ci		ret = lis3lv02d_init_dt(&lis3_dev);
1138c2ecf20Sopenharmony_ci		if (ret)
1148c2ecf20Sopenharmony_ci			return ret;
1158c2ecf20Sopenharmony_ci		pdata = lis3_dev.pdata;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci#endif
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (pdata) {
1208c2ecf20Sopenharmony_ci		if ((pdata->driver_features & LIS3_USE_BLOCK_READ) &&
1218c2ecf20Sopenharmony_ci			(i2c_check_functionality(client->adapter,
1228c2ecf20Sopenharmony_ci						I2C_FUNC_SMBUS_I2C_BLOCK)))
1238c2ecf20Sopenharmony_ci			lis3_dev.blkread  = lis3_i2c_blockread;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci		if (pdata->axis_x)
1268c2ecf20Sopenharmony_ci			lis3lv02d_axis_map.x = pdata->axis_x;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci		if (pdata->axis_y)
1298c2ecf20Sopenharmony_ci			lis3lv02d_axis_map.y = pdata->axis_y;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		if (pdata->axis_z)
1328c2ecf20Sopenharmony_ci			lis3lv02d_axis_map.z = pdata->axis_z;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		if (pdata->setup_resources)
1358c2ecf20Sopenharmony_ci			ret = pdata->setup_resources();
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci		if (ret)
1388c2ecf20Sopenharmony_ci			goto fail;
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	lis3_dev.regulators[0].supply = reg_vdd;
1428c2ecf20Sopenharmony_ci	lis3_dev.regulators[1].supply = reg_vdd_io;
1438c2ecf20Sopenharmony_ci	ret = regulator_bulk_get(&client->dev,
1448c2ecf20Sopenharmony_ci				 ARRAY_SIZE(lis3_dev.regulators),
1458c2ecf20Sopenharmony_ci				 lis3_dev.regulators);
1468c2ecf20Sopenharmony_ci	if (ret < 0)
1478c2ecf20Sopenharmony_ci		goto fail;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	lis3_dev.pdata	  = pdata;
1508c2ecf20Sopenharmony_ci	lis3_dev.bus_priv = client;
1518c2ecf20Sopenharmony_ci	lis3_dev.init	  = lis3_i2c_init;
1528c2ecf20Sopenharmony_ci	lis3_dev.read	  = lis3_i2c_read;
1538c2ecf20Sopenharmony_ci	lis3_dev.write	  = lis3_i2c_write;
1548c2ecf20Sopenharmony_ci	lis3_dev.irq	  = client->irq;
1558c2ecf20Sopenharmony_ci	lis3_dev.ac	  = lis3lv02d_axis_map;
1568c2ecf20Sopenharmony_ci	lis3_dev.pm_dev	  = &client->dev;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, &lis3_dev);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* Provide power over the init call */
1618c2ecf20Sopenharmony_ci	lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	ret = lis3lv02d_init_device(&lis3_dev);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (ret)
1688c2ecf20Sopenharmony_ci		goto fail2;
1698c2ecf20Sopenharmony_ci	return 0;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cifail2:
1728c2ecf20Sopenharmony_ci	regulator_bulk_free(ARRAY_SIZE(lis3_dev.regulators),
1738c2ecf20Sopenharmony_ci				lis3_dev.regulators);
1748c2ecf20Sopenharmony_cifail:
1758c2ecf20Sopenharmony_ci	if (pdata && pdata->release_resources)
1768c2ecf20Sopenharmony_ci		pdata->release_resources();
1778c2ecf20Sopenharmony_ci	return ret;
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic int lis3lv02d_i2c_remove(struct i2c_client *client)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
1838c2ecf20Sopenharmony_ci	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	if (pdata && pdata->release_resources)
1868c2ecf20Sopenharmony_ci		pdata->release_resources();
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	lis3lv02d_joystick_disable(lis3);
1898c2ecf20Sopenharmony_ci	lis3lv02d_remove_fs(&lis3_dev);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	regulator_bulk_free(ARRAY_SIZE(lis3->regulators),
1928c2ecf20Sopenharmony_ci			    lis3_dev.regulators);
1938c2ecf20Sopenharmony_ci	return 0;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
1978c2ecf20Sopenharmony_cistatic int lis3lv02d_i2c_suspend(struct device *dev)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
2008c2ecf20Sopenharmony_ci	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (!lis3->pdata || !lis3->pdata->wakeup_flags)
2038c2ecf20Sopenharmony_ci		lis3lv02d_poweroff(lis3);
2048c2ecf20Sopenharmony_ci	return 0;
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic int lis3lv02d_i2c_resume(struct device *dev)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
2108c2ecf20Sopenharmony_ci	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	/*
2138c2ecf20Sopenharmony_ci	 * pm_runtime documentation says that devices should always
2148c2ecf20Sopenharmony_ci	 * be powered on at resume. Pm_runtime turns them off after system
2158c2ecf20Sopenharmony_ci	 * wide resume is complete.
2168c2ecf20Sopenharmony_ci	 */
2178c2ecf20Sopenharmony_ci	if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
2188c2ecf20Sopenharmony_ci		pm_runtime_suspended(dev))
2198c2ecf20Sopenharmony_ci		lis3lv02d_poweron(lis3);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return 0;
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
2268c2ecf20Sopenharmony_cistatic int lis3_i2c_runtime_suspend(struct device *dev)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
2298c2ecf20Sopenharmony_ci	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	lis3lv02d_poweroff(lis3);
2328c2ecf20Sopenharmony_ci	return 0;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int lis3_i2c_runtime_resume(struct device *dev)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
2388c2ecf20Sopenharmony_ci	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	lis3lv02d_poweron(lis3);
2418c2ecf20Sopenharmony_ci	return 0;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci#endif /* CONFIG_PM */
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic const struct i2c_device_id lis3lv02d_id[] = {
2468c2ecf20Sopenharmony_ci	{"lis3lv02d", LIS3LV02D},
2478c2ecf20Sopenharmony_ci	{"lis331dlh", LIS331DLH},
2488c2ecf20Sopenharmony_ci	{}
2498c2ecf20Sopenharmony_ci};
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic const struct dev_pm_ops lis3_pm_ops = {
2548c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
2558c2ecf20Sopenharmony_ci				lis3lv02d_i2c_resume)
2568c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
2578c2ecf20Sopenharmony_ci			   lis3_i2c_runtime_resume,
2588c2ecf20Sopenharmony_ci			   NULL)
2598c2ecf20Sopenharmony_ci};
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic struct i2c_driver lis3lv02d_i2c_driver = {
2628c2ecf20Sopenharmony_ci	.driver	 = {
2638c2ecf20Sopenharmony_ci		.name   = DRV_NAME,
2648c2ecf20Sopenharmony_ci		.pm     = &lis3_pm_ops,
2658c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(lis3lv02d_i2c_dt_ids),
2668c2ecf20Sopenharmony_ci	},
2678c2ecf20Sopenharmony_ci	.probe	= lis3lv02d_i2c_probe,
2688c2ecf20Sopenharmony_ci	.remove	= lis3lv02d_i2c_remove,
2698c2ecf20Sopenharmony_ci	.id_table = lis3lv02d_id,
2708c2ecf20Sopenharmony_ci};
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cimodule_i2c_driver(lis3lv02d_i2c_driver);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ciMODULE_AUTHOR("Nokia Corporation");
2758c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("lis3lv02d I2C interface");
2768c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
277