18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Core driver access RC5T583 power management chip.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2011-2012, NVIDIA CORPORATION.  All rights reserved.
68c2ecf20Sopenharmony_ci * Author: Laxman dewangan <ldewangan@nvidia.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Based on code
98c2ecf20Sopenharmony_ci *	Copyright (C) 2011 RICOH COMPANY,LTD
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/irq.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/err.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/mfd/core.h>
198c2ecf20Sopenharmony_ci#include <linux/mfd/rc5t583.h>
208c2ecf20Sopenharmony_ci#include <linux/regmap.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define RICOH_ONOFFSEL_REG	0x10
238c2ecf20Sopenharmony_ci#define RICOH_SWCTL_REG		0x5E
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct deepsleep_control_data {
268c2ecf20Sopenharmony_ci	u8 reg_add;
278c2ecf20Sopenharmony_ci	u8 ds_pos_bit;
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define DEEPSLEEP_INIT(_id, _reg, _pos)		\
318c2ecf20Sopenharmony_ci	{					\
328c2ecf20Sopenharmony_ci		.reg_add = RC5T583_##_reg,	\
338c2ecf20Sopenharmony_ci		.ds_pos_bit = _pos,		\
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic struct deepsleep_control_data deepsleep_data[] = {
378c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(DC0, SLPSEQ1, 0),
388c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(DC1, SLPSEQ1, 4),
398c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(DC2, SLPSEQ2, 0),
408c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(DC3, SLPSEQ2, 4),
418c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO0, SLPSEQ3, 0),
428c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO1, SLPSEQ3, 4),
438c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO2, SLPSEQ4, 0),
448c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO3, SLPSEQ4, 4),
458c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO4, SLPSEQ5, 0),
468c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO5, SLPSEQ5, 4),
478c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO6, SLPSEQ6, 0),
488c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO7, SLPSEQ6, 4),
498c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO8, SLPSEQ7, 0),
508c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(LDO9, SLPSEQ7, 4),
518c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(PSO0, SLPSEQ8, 0),
528c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(PSO1, SLPSEQ8, 4),
538c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(PSO2, SLPSEQ9, 0),
548c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(PSO3, SLPSEQ9, 4),
558c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(PSO4, SLPSEQ10, 0),
568c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(PSO5, SLPSEQ10, 4),
578c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(PSO6, SLPSEQ11, 0),
588c2ecf20Sopenharmony_ci	DEEPSLEEP_INIT(PSO7, SLPSEQ11, 4),
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define EXT_PWR_REQ		\
628c2ecf20Sopenharmony_ci	(RC5T583_EXT_PWRREQ1_CONTROL | RC5T583_EXT_PWRREQ2_CONTROL)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic const struct mfd_cell rc5t583_subdevs[] = {
658c2ecf20Sopenharmony_ci	{.name = "rc5t583-gpio",},
668c2ecf20Sopenharmony_ci	{.name = "rc5t583-regulator",},
678c2ecf20Sopenharmony_ci	{.name = "rc5t583-rtc",      },
688c2ecf20Sopenharmony_ci	{.name = "rc5t583-key",      }
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic int __rc5t583_set_ext_pwrreq1_control(struct device *dev,
728c2ecf20Sopenharmony_ci	int id, int ext_pwr, int slots)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	int ret;
758c2ecf20Sopenharmony_ci	uint8_t sleepseq_val = 0;
768c2ecf20Sopenharmony_ci	unsigned int en_bit;
778c2ecf20Sopenharmony_ci	unsigned int slot_bit;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (id == RC5T583_DS_DC0) {
808c2ecf20Sopenharmony_ci		dev_err(dev, "PWRREQ1 is invalid control for rail %d\n", id);
818c2ecf20Sopenharmony_ci		return -EINVAL;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	en_bit = deepsleep_data[id].ds_pos_bit;
858c2ecf20Sopenharmony_ci	slot_bit = en_bit + 1;
868c2ecf20Sopenharmony_ci	ret = rc5t583_read(dev, deepsleep_data[id].reg_add, &sleepseq_val);
878c2ecf20Sopenharmony_ci	if (ret < 0) {
888c2ecf20Sopenharmony_ci		dev_err(dev, "Error in reading reg 0x%x\n",
898c2ecf20Sopenharmony_ci				deepsleep_data[id].reg_add);
908c2ecf20Sopenharmony_ci		return ret;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	sleepseq_val &= ~(0xF << en_bit);
948c2ecf20Sopenharmony_ci	sleepseq_val |= BIT(en_bit);
958c2ecf20Sopenharmony_ci	sleepseq_val |= ((slots & 0x7) << slot_bit);
968c2ecf20Sopenharmony_ci	ret = rc5t583_set_bits(dev, RICOH_ONOFFSEL_REG, BIT(1));
978c2ecf20Sopenharmony_ci	if (ret < 0) {
988c2ecf20Sopenharmony_ci		dev_err(dev, "Error in updating the 0x%02x register\n",
998c2ecf20Sopenharmony_ci				RICOH_ONOFFSEL_REG);
1008c2ecf20Sopenharmony_ci		return ret;
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	ret = rc5t583_write(dev, deepsleep_data[id].reg_add, sleepseq_val);
1048c2ecf20Sopenharmony_ci	if (ret < 0) {
1058c2ecf20Sopenharmony_ci		dev_err(dev, "Error in writing reg 0x%x\n",
1068c2ecf20Sopenharmony_ci				deepsleep_data[id].reg_add);
1078c2ecf20Sopenharmony_ci		return ret;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (id == RC5T583_DS_LDO4) {
1118c2ecf20Sopenharmony_ci		ret = rc5t583_write(dev, RICOH_SWCTL_REG, 0x1);
1128c2ecf20Sopenharmony_ci		if (ret < 0)
1138c2ecf20Sopenharmony_ci			dev_err(dev, "Error in writing reg 0x%x\n",
1148c2ecf20Sopenharmony_ci				RICOH_SWCTL_REG);
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci	return ret;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int __rc5t583_set_ext_pwrreq2_control(struct device *dev,
1208c2ecf20Sopenharmony_ci	int id, int ext_pwr)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	int ret;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (id != RC5T583_DS_DC0) {
1258c2ecf20Sopenharmony_ci		dev_err(dev, "PWRREQ2 is invalid control for rail %d\n", id);
1268c2ecf20Sopenharmony_ci		return -EINVAL;
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	ret = rc5t583_set_bits(dev, RICOH_ONOFFSEL_REG, BIT(2));
1308c2ecf20Sopenharmony_ci	if (ret < 0)
1318c2ecf20Sopenharmony_ci		dev_err(dev, "Error in updating the ONOFFSEL 0x10 register\n");
1328c2ecf20Sopenharmony_ci	return ret;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciint rc5t583_ext_power_req_config(struct device *dev, int ds_id,
1368c2ecf20Sopenharmony_ci	int ext_pwr_req, int deepsleep_slot_nr)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	if ((ext_pwr_req & EXT_PWR_REQ) == EXT_PWR_REQ)
1398c2ecf20Sopenharmony_ci		return -EINVAL;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (ext_pwr_req & RC5T583_EXT_PWRREQ1_CONTROL)
1428c2ecf20Sopenharmony_ci		return __rc5t583_set_ext_pwrreq1_control(dev, ds_id,
1438c2ecf20Sopenharmony_ci				ext_pwr_req, deepsleep_slot_nr);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	if (ext_pwr_req & RC5T583_EXT_PWRREQ2_CONTROL)
1468c2ecf20Sopenharmony_ci		return __rc5t583_set_ext_pwrreq2_control(dev,
1478c2ecf20Sopenharmony_ci			ds_id, ext_pwr_req);
1488c2ecf20Sopenharmony_ci	return 0;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rc5t583_ext_power_req_config);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic int rc5t583_clear_ext_power_req(struct rc5t583 *rc5t583,
1538c2ecf20Sopenharmony_ci	struct rc5t583_platform_data *pdata)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	int ret;
1568c2ecf20Sopenharmony_ci	int i;
1578c2ecf20Sopenharmony_ci	uint8_t on_off_val = 0;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	/*  Clear ONOFFSEL register */
1608c2ecf20Sopenharmony_ci	if (pdata->enable_shutdown)
1618c2ecf20Sopenharmony_ci		on_off_val = 0x1;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	ret = rc5t583_write(rc5t583->dev, RICOH_ONOFFSEL_REG, on_off_val);
1648c2ecf20Sopenharmony_ci	if (ret < 0)
1658c2ecf20Sopenharmony_ci		dev_warn(rc5t583->dev, "Error in writing reg %d error: %d\n",
1668c2ecf20Sopenharmony_ci					RICOH_ONOFFSEL_REG, ret);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	ret = rc5t583_write(rc5t583->dev, RICOH_SWCTL_REG, 0x0);
1698c2ecf20Sopenharmony_ci	if (ret < 0)
1708c2ecf20Sopenharmony_ci		dev_warn(rc5t583->dev, "Error in writing reg %d error: %d\n",
1718c2ecf20Sopenharmony_ci					RICOH_SWCTL_REG, ret);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* Clear sleep sequence register */
1748c2ecf20Sopenharmony_ci	for (i = RC5T583_SLPSEQ1; i <= RC5T583_SLPSEQ11; ++i) {
1758c2ecf20Sopenharmony_ci		ret = rc5t583_write(rc5t583->dev, i, 0x0);
1768c2ecf20Sopenharmony_ci		if (ret < 0)
1778c2ecf20Sopenharmony_ci			dev_warn(rc5t583->dev,
1788c2ecf20Sopenharmony_ci				"Error in writing reg 0x%02x error: %d\n",
1798c2ecf20Sopenharmony_ci				i, ret);
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci	return 0;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic bool volatile_reg(struct device *dev, unsigned int reg)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	/* Enable caching in interrupt registers */
1878c2ecf20Sopenharmony_ci	switch (reg) {
1888c2ecf20Sopenharmony_ci	case RC5T583_INT_EN_SYS1:
1898c2ecf20Sopenharmony_ci	case RC5T583_INT_EN_SYS2:
1908c2ecf20Sopenharmony_ci	case RC5T583_INT_EN_DCDC:
1918c2ecf20Sopenharmony_ci	case RC5T583_INT_EN_RTC:
1928c2ecf20Sopenharmony_ci	case RC5T583_INT_EN_ADC1:
1938c2ecf20Sopenharmony_ci	case RC5T583_INT_EN_ADC2:
1948c2ecf20Sopenharmony_ci	case RC5T583_INT_EN_ADC3:
1958c2ecf20Sopenharmony_ci	case RC5T583_GPIO_GPEDGE1:
1968c2ecf20Sopenharmony_ci	case RC5T583_GPIO_GPEDGE2:
1978c2ecf20Sopenharmony_ci	case RC5T583_GPIO_EN_INT:
1988c2ecf20Sopenharmony_ci		return false;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	case RC5T583_GPIO_MON_IOIN:
2018c2ecf20Sopenharmony_ci		/* This is gpio input register */
2028c2ecf20Sopenharmony_ci		return true;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	default:
2058c2ecf20Sopenharmony_ci		/* Enable caching in gpio registers */
2068c2ecf20Sopenharmony_ci		if ((reg >= RC5T583_GPIO_IOSEL) &&
2078c2ecf20Sopenharmony_ci				(reg <= RC5T583_GPIO_GPOFUNC))
2088c2ecf20Sopenharmony_ci			return false;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci		/* Enable caching in sleep seq registers */
2118c2ecf20Sopenharmony_ci		if ((reg >= RC5T583_SLPSEQ1) && (reg <= RC5T583_SLPSEQ11))
2128c2ecf20Sopenharmony_ci			return false;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci		/* Enable caching of regulator registers */
2158c2ecf20Sopenharmony_ci		if ((reg >= RC5T583_REG_DC0CTL) && (reg <= RC5T583_REG_SR3CTL))
2168c2ecf20Sopenharmony_ci			return false;
2178c2ecf20Sopenharmony_ci		if ((reg >= RC5T583_REG_LDOEN1) &&
2188c2ecf20Sopenharmony_ci					(reg <= RC5T583_REG_LDO9DAC_DS))
2198c2ecf20Sopenharmony_ci			return false;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		break;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return true;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic const struct regmap_config rc5t583_regmap_config = {
2288c2ecf20Sopenharmony_ci	.reg_bits = 8,
2298c2ecf20Sopenharmony_ci	.val_bits = 8,
2308c2ecf20Sopenharmony_ci	.volatile_reg = volatile_reg,
2318c2ecf20Sopenharmony_ci	.max_register = RC5T583_MAX_REG,
2328c2ecf20Sopenharmony_ci	.num_reg_defaults_raw = RC5T583_NUM_REGS,
2338c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
2348c2ecf20Sopenharmony_ci};
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic int rc5t583_i2c_probe(struct i2c_client *i2c,
2378c2ecf20Sopenharmony_ci			      const struct i2c_device_id *id)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct rc5t583 *rc5t583;
2408c2ecf20Sopenharmony_ci	struct rc5t583_platform_data *pdata = dev_get_platdata(&i2c->dev);
2418c2ecf20Sopenharmony_ci	int ret;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	if (!pdata) {
2448c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Err: Platform data not found\n");
2458c2ecf20Sopenharmony_ci		return -EINVAL;
2468c2ecf20Sopenharmony_ci	}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	rc5t583 = devm_kzalloc(&i2c->dev, sizeof(*rc5t583), GFP_KERNEL);
2498c2ecf20Sopenharmony_ci	if (!rc5t583)
2508c2ecf20Sopenharmony_ci		return -ENOMEM;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	rc5t583->dev = &i2c->dev;
2538c2ecf20Sopenharmony_ci	i2c_set_clientdata(i2c, rc5t583);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	rc5t583->regmap = devm_regmap_init_i2c(i2c, &rc5t583_regmap_config);
2568c2ecf20Sopenharmony_ci	if (IS_ERR(rc5t583->regmap)) {
2578c2ecf20Sopenharmony_ci		ret = PTR_ERR(rc5t583->regmap);
2588c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
2598c2ecf20Sopenharmony_ci		return ret;
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	ret = rc5t583_clear_ext_power_req(rc5t583, pdata);
2638c2ecf20Sopenharmony_ci	if (ret < 0)
2648c2ecf20Sopenharmony_ci		return ret;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	if (i2c->irq) {
2678c2ecf20Sopenharmony_ci		ret = rc5t583_irq_init(rc5t583, i2c->irq, pdata->irq_base);
2688c2ecf20Sopenharmony_ci		/* Still continue with warning, if irq init fails */
2698c2ecf20Sopenharmony_ci		if (ret)
2708c2ecf20Sopenharmony_ci			dev_warn(&i2c->dev, "IRQ init failed: %d\n", ret);
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	ret = devm_mfd_add_devices(rc5t583->dev, -1, rc5t583_subdevs,
2748c2ecf20Sopenharmony_ci				   ARRAY_SIZE(rc5t583_subdevs), NULL, 0, NULL);
2758c2ecf20Sopenharmony_ci	if (ret) {
2768c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "add mfd devices failed: %d\n", ret);
2778c2ecf20Sopenharmony_ci		return ret;
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	return 0;
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic const struct i2c_device_id rc5t583_i2c_id[] = {
2848c2ecf20Sopenharmony_ci	{.name = "rc5t583", .driver_data = 0},
2858c2ecf20Sopenharmony_ci	{}
2868c2ecf20Sopenharmony_ci};
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic struct i2c_driver rc5t583_i2c_driver = {
2898c2ecf20Sopenharmony_ci	.driver = {
2908c2ecf20Sopenharmony_ci		   .name = "rc5t583",
2918c2ecf20Sopenharmony_ci		   },
2928c2ecf20Sopenharmony_ci	.probe = rc5t583_i2c_probe,
2938c2ecf20Sopenharmony_ci	.id_table = rc5t583_i2c_id,
2948c2ecf20Sopenharmony_ci};
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic int __init rc5t583_i2c_init(void)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	return i2c_add_driver(&rc5t583_i2c_driver);
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_cisubsys_initcall(rc5t583_i2c_init);
301