162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Core driver access RC5T583 power management chip.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2011-2012, NVIDIA CORPORATION.  All rights reserved.
662306a36Sopenharmony_ci * Author: Laxman dewangan <ldewangan@nvidia.com>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Based on code
962306a36Sopenharmony_ci *	Copyright (C) 2011 RICOH COMPANY,LTD
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci#include <linux/interrupt.h>
1262306a36Sopenharmony_ci#include <linux/irq.h>
1362306a36Sopenharmony_ci#include <linux/kernel.h>
1462306a36Sopenharmony_ci#include <linux/init.h>
1562306a36Sopenharmony_ci#include <linux/err.h>
1662306a36Sopenharmony_ci#include <linux/slab.h>
1762306a36Sopenharmony_ci#include <linux/i2c.h>
1862306a36Sopenharmony_ci#include <linux/mfd/core.h>
1962306a36Sopenharmony_ci#include <linux/mfd/rc5t583.h>
2062306a36Sopenharmony_ci#include <linux/regmap.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define RICOH_ONOFFSEL_REG	0x10
2362306a36Sopenharmony_ci#define RICOH_SWCTL_REG		0x5E
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_cistruct deepsleep_control_data {
2662306a36Sopenharmony_ci	u8 reg_add;
2762306a36Sopenharmony_ci	u8 ds_pos_bit;
2862306a36Sopenharmony_ci};
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define DEEPSLEEP_INIT(_id, _reg, _pos)		\
3162306a36Sopenharmony_ci	{					\
3262306a36Sopenharmony_ci		.reg_add = RC5T583_##_reg,	\
3362306a36Sopenharmony_ci		.ds_pos_bit = _pos,		\
3462306a36Sopenharmony_ci	}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic struct deepsleep_control_data deepsleep_data[] = {
3762306a36Sopenharmony_ci	DEEPSLEEP_INIT(DC0, SLPSEQ1, 0),
3862306a36Sopenharmony_ci	DEEPSLEEP_INIT(DC1, SLPSEQ1, 4),
3962306a36Sopenharmony_ci	DEEPSLEEP_INIT(DC2, SLPSEQ2, 0),
4062306a36Sopenharmony_ci	DEEPSLEEP_INIT(DC3, SLPSEQ2, 4),
4162306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO0, SLPSEQ3, 0),
4262306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO1, SLPSEQ3, 4),
4362306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO2, SLPSEQ4, 0),
4462306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO3, SLPSEQ4, 4),
4562306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO4, SLPSEQ5, 0),
4662306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO5, SLPSEQ5, 4),
4762306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO6, SLPSEQ6, 0),
4862306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO7, SLPSEQ6, 4),
4962306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO8, SLPSEQ7, 0),
5062306a36Sopenharmony_ci	DEEPSLEEP_INIT(LDO9, SLPSEQ7, 4),
5162306a36Sopenharmony_ci	DEEPSLEEP_INIT(PSO0, SLPSEQ8, 0),
5262306a36Sopenharmony_ci	DEEPSLEEP_INIT(PSO1, SLPSEQ8, 4),
5362306a36Sopenharmony_ci	DEEPSLEEP_INIT(PSO2, SLPSEQ9, 0),
5462306a36Sopenharmony_ci	DEEPSLEEP_INIT(PSO3, SLPSEQ9, 4),
5562306a36Sopenharmony_ci	DEEPSLEEP_INIT(PSO4, SLPSEQ10, 0),
5662306a36Sopenharmony_ci	DEEPSLEEP_INIT(PSO5, SLPSEQ10, 4),
5762306a36Sopenharmony_ci	DEEPSLEEP_INIT(PSO6, SLPSEQ11, 0),
5862306a36Sopenharmony_ci	DEEPSLEEP_INIT(PSO7, SLPSEQ11, 4),
5962306a36Sopenharmony_ci};
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define EXT_PWR_REQ		\
6262306a36Sopenharmony_ci	(RC5T583_EXT_PWRREQ1_CONTROL | RC5T583_EXT_PWRREQ2_CONTROL)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistatic const struct mfd_cell rc5t583_subdevs[] = {
6562306a36Sopenharmony_ci	{.name = "rc5t583-gpio",},
6662306a36Sopenharmony_ci	{.name = "rc5t583-regulator",},
6762306a36Sopenharmony_ci	{.name = "rc5t583-rtc",      },
6862306a36Sopenharmony_ci	{.name = "rc5t583-key",      }
6962306a36Sopenharmony_ci};
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic int __rc5t583_set_ext_pwrreq1_control(struct device *dev,
7262306a36Sopenharmony_ci	int id, int ext_pwr, int slots)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	int ret;
7562306a36Sopenharmony_ci	uint8_t sleepseq_val = 0;
7662306a36Sopenharmony_ci	unsigned int en_bit;
7762306a36Sopenharmony_ci	unsigned int slot_bit;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	if (id == RC5T583_DS_DC0) {
8062306a36Sopenharmony_ci		dev_err(dev, "PWRREQ1 is invalid control for rail %d\n", id);
8162306a36Sopenharmony_ci		return -EINVAL;
8262306a36Sopenharmony_ci	}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	en_bit = deepsleep_data[id].ds_pos_bit;
8562306a36Sopenharmony_ci	slot_bit = en_bit + 1;
8662306a36Sopenharmony_ci	ret = rc5t583_read(dev, deepsleep_data[id].reg_add, &sleepseq_val);
8762306a36Sopenharmony_ci	if (ret < 0) {
8862306a36Sopenharmony_ci		dev_err(dev, "Error in reading reg 0x%x\n",
8962306a36Sopenharmony_ci				deepsleep_data[id].reg_add);
9062306a36Sopenharmony_ci		return ret;
9162306a36Sopenharmony_ci	}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	sleepseq_val &= ~(0xF << en_bit);
9462306a36Sopenharmony_ci	sleepseq_val |= BIT(en_bit);
9562306a36Sopenharmony_ci	sleepseq_val |= ((slots & 0x7) << slot_bit);
9662306a36Sopenharmony_ci	ret = rc5t583_set_bits(dev, RICOH_ONOFFSEL_REG, BIT(1));
9762306a36Sopenharmony_ci	if (ret < 0) {
9862306a36Sopenharmony_ci		dev_err(dev, "Error in updating the 0x%02x register\n",
9962306a36Sopenharmony_ci				RICOH_ONOFFSEL_REG);
10062306a36Sopenharmony_ci		return ret;
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	ret = rc5t583_write(dev, deepsleep_data[id].reg_add, sleepseq_val);
10462306a36Sopenharmony_ci	if (ret < 0) {
10562306a36Sopenharmony_ci		dev_err(dev, "Error in writing reg 0x%x\n",
10662306a36Sopenharmony_ci				deepsleep_data[id].reg_add);
10762306a36Sopenharmony_ci		return ret;
10862306a36Sopenharmony_ci	}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	if (id == RC5T583_DS_LDO4) {
11162306a36Sopenharmony_ci		ret = rc5t583_write(dev, RICOH_SWCTL_REG, 0x1);
11262306a36Sopenharmony_ci		if (ret < 0)
11362306a36Sopenharmony_ci			dev_err(dev, "Error in writing reg 0x%x\n",
11462306a36Sopenharmony_ci				RICOH_SWCTL_REG);
11562306a36Sopenharmony_ci	}
11662306a36Sopenharmony_ci	return ret;
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistatic int __rc5t583_set_ext_pwrreq2_control(struct device *dev,
12062306a36Sopenharmony_ci	int id, int ext_pwr)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	int ret;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	if (id != RC5T583_DS_DC0) {
12562306a36Sopenharmony_ci		dev_err(dev, "PWRREQ2 is invalid control for rail %d\n", id);
12662306a36Sopenharmony_ci		return -EINVAL;
12762306a36Sopenharmony_ci	}
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	ret = rc5t583_set_bits(dev, RICOH_ONOFFSEL_REG, BIT(2));
13062306a36Sopenharmony_ci	if (ret < 0)
13162306a36Sopenharmony_ci		dev_err(dev, "Error in updating the ONOFFSEL 0x10 register\n");
13262306a36Sopenharmony_ci	return ret;
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciint rc5t583_ext_power_req_config(struct device *dev, int ds_id,
13662306a36Sopenharmony_ci	int ext_pwr_req, int deepsleep_slot_nr)
13762306a36Sopenharmony_ci{
13862306a36Sopenharmony_ci	if ((ext_pwr_req & EXT_PWR_REQ) == EXT_PWR_REQ)
13962306a36Sopenharmony_ci		return -EINVAL;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	if (ext_pwr_req & RC5T583_EXT_PWRREQ1_CONTROL)
14262306a36Sopenharmony_ci		return __rc5t583_set_ext_pwrreq1_control(dev, ds_id,
14362306a36Sopenharmony_ci				ext_pwr_req, deepsleep_slot_nr);
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	if (ext_pwr_req & RC5T583_EXT_PWRREQ2_CONTROL)
14662306a36Sopenharmony_ci		return __rc5t583_set_ext_pwrreq2_control(dev,
14762306a36Sopenharmony_ci			ds_id, ext_pwr_req);
14862306a36Sopenharmony_ci	return 0;
14962306a36Sopenharmony_ci}
15062306a36Sopenharmony_ciEXPORT_SYMBOL(rc5t583_ext_power_req_config);
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistatic int rc5t583_clear_ext_power_req(struct rc5t583 *rc5t583,
15362306a36Sopenharmony_ci	struct rc5t583_platform_data *pdata)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	int ret;
15662306a36Sopenharmony_ci	int i;
15762306a36Sopenharmony_ci	uint8_t on_off_val = 0;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	/*  Clear ONOFFSEL register */
16062306a36Sopenharmony_ci	if (pdata->enable_shutdown)
16162306a36Sopenharmony_ci		on_off_val = 0x1;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	ret = rc5t583_write(rc5t583->dev, RICOH_ONOFFSEL_REG, on_off_val);
16462306a36Sopenharmony_ci	if (ret < 0)
16562306a36Sopenharmony_ci		dev_warn(rc5t583->dev, "Error in writing reg %d error: %d\n",
16662306a36Sopenharmony_ci					RICOH_ONOFFSEL_REG, ret);
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	ret = rc5t583_write(rc5t583->dev, RICOH_SWCTL_REG, 0x0);
16962306a36Sopenharmony_ci	if (ret < 0)
17062306a36Sopenharmony_ci		dev_warn(rc5t583->dev, "Error in writing reg %d error: %d\n",
17162306a36Sopenharmony_ci					RICOH_SWCTL_REG, ret);
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	/* Clear sleep sequence register */
17462306a36Sopenharmony_ci	for (i = RC5T583_SLPSEQ1; i <= RC5T583_SLPSEQ11; ++i) {
17562306a36Sopenharmony_ci		ret = rc5t583_write(rc5t583->dev, i, 0x0);
17662306a36Sopenharmony_ci		if (ret < 0)
17762306a36Sopenharmony_ci			dev_warn(rc5t583->dev,
17862306a36Sopenharmony_ci				"Error in writing reg 0x%02x error: %d\n",
17962306a36Sopenharmony_ci				i, ret);
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci	return 0;
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_cistatic bool volatile_reg(struct device *dev, unsigned int reg)
18562306a36Sopenharmony_ci{
18662306a36Sopenharmony_ci	/* Enable caching in interrupt registers */
18762306a36Sopenharmony_ci	switch (reg) {
18862306a36Sopenharmony_ci	case RC5T583_INT_EN_SYS1:
18962306a36Sopenharmony_ci	case RC5T583_INT_EN_SYS2:
19062306a36Sopenharmony_ci	case RC5T583_INT_EN_DCDC:
19162306a36Sopenharmony_ci	case RC5T583_INT_EN_RTC:
19262306a36Sopenharmony_ci	case RC5T583_INT_EN_ADC1:
19362306a36Sopenharmony_ci	case RC5T583_INT_EN_ADC2:
19462306a36Sopenharmony_ci	case RC5T583_INT_EN_ADC3:
19562306a36Sopenharmony_ci	case RC5T583_GPIO_GPEDGE1:
19662306a36Sopenharmony_ci	case RC5T583_GPIO_GPEDGE2:
19762306a36Sopenharmony_ci	case RC5T583_GPIO_EN_INT:
19862306a36Sopenharmony_ci		return false;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	case RC5T583_GPIO_MON_IOIN:
20162306a36Sopenharmony_ci		/* This is gpio input register */
20262306a36Sopenharmony_ci		return true;
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	default:
20562306a36Sopenharmony_ci		/* Enable caching in gpio registers */
20662306a36Sopenharmony_ci		if ((reg >= RC5T583_GPIO_IOSEL) &&
20762306a36Sopenharmony_ci				(reg <= RC5T583_GPIO_GPOFUNC))
20862306a36Sopenharmony_ci			return false;
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci		/* Enable caching in sleep seq registers */
21162306a36Sopenharmony_ci		if ((reg >= RC5T583_SLPSEQ1) && (reg <= RC5T583_SLPSEQ11))
21262306a36Sopenharmony_ci			return false;
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci		/* Enable caching of regulator registers */
21562306a36Sopenharmony_ci		if ((reg >= RC5T583_REG_DC0CTL) && (reg <= RC5T583_REG_SR3CTL))
21662306a36Sopenharmony_ci			return false;
21762306a36Sopenharmony_ci		if ((reg >= RC5T583_REG_LDOEN1) &&
21862306a36Sopenharmony_ci					(reg <= RC5T583_REG_LDO9DAC_DS))
21962306a36Sopenharmony_ci			return false;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci		break;
22262306a36Sopenharmony_ci	}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	return true;
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic const struct regmap_config rc5t583_regmap_config = {
22862306a36Sopenharmony_ci	.reg_bits = 8,
22962306a36Sopenharmony_ci	.val_bits = 8,
23062306a36Sopenharmony_ci	.volatile_reg = volatile_reg,
23162306a36Sopenharmony_ci	.max_register = RC5T583_MAX_REG,
23262306a36Sopenharmony_ci	.num_reg_defaults_raw = RC5T583_NUM_REGS,
23362306a36Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
23462306a36Sopenharmony_ci};
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistatic int rc5t583_i2c_probe(struct i2c_client *i2c)
23762306a36Sopenharmony_ci{
23862306a36Sopenharmony_ci	struct rc5t583 *rc5t583;
23962306a36Sopenharmony_ci	struct rc5t583_platform_data *pdata = dev_get_platdata(&i2c->dev);
24062306a36Sopenharmony_ci	int ret;
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	if (!pdata) {
24362306a36Sopenharmony_ci		dev_err(&i2c->dev, "Err: Platform data not found\n");
24462306a36Sopenharmony_ci		return -EINVAL;
24562306a36Sopenharmony_ci	}
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	rc5t583 = devm_kzalloc(&i2c->dev, sizeof(*rc5t583), GFP_KERNEL);
24862306a36Sopenharmony_ci	if (!rc5t583)
24962306a36Sopenharmony_ci		return -ENOMEM;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	rc5t583->dev = &i2c->dev;
25262306a36Sopenharmony_ci	i2c_set_clientdata(i2c, rc5t583);
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	rc5t583->regmap = devm_regmap_init_i2c(i2c, &rc5t583_regmap_config);
25562306a36Sopenharmony_ci	if (IS_ERR(rc5t583->regmap)) {
25662306a36Sopenharmony_ci		ret = PTR_ERR(rc5t583->regmap);
25762306a36Sopenharmony_ci		dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
25862306a36Sopenharmony_ci		return ret;
25962306a36Sopenharmony_ci	}
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	ret = rc5t583_clear_ext_power_req(rc5t583, pdata);
26262306a36Sopenharmony_ci	if (ret < 0)
26362306a36Sopenharmony_ci		return ret;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	if (i2c->irq) {
26662306a36Sopenharmony_ci		ret = rc5t583_irq_init(rc5t583, i2c->irq, pdata->irq_base);
26762306a36Sopenharmony_ci		/* Still continue with warning, if irq init fails */
26862306a36Sopenharmony_ci		if (ret)
26962306a36Sopenharmony_ci			dev_warn(&i2c->dev, "IRQ init failed: %d\n", ret);
27062306a36Sopenharmony_ci	}
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	ret = devm_mfd_add_devices(rc5t583->dev, -1, rc5t583_subdevs,
27362306a36Sopenharmony_ci				   ARRAY_SIZE(rc5t583_subdevs), NULL, 0, NULL);
27462306a36Sopenharmony_ci	if (ret) {
27562306a36Sopenharmony_ci		dev_err(&i2c->dev, "add mfd devices failed: %d\n", ret);
27662306a36Sopenharmony_ci		return ret;
27762306a36Sopenharmony_ci	}
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci	return 0;
28062306a36Sopenharmony_ci}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_cistatic const struct i2c_device_id rc5t583_i2c_id[] = {
28362306a36Sopenharmony_ci	{.name = "rc5t583", .driver_data = 0},
28462306a36Sopenharmony_ci	{}
28562306a36Sopenharmony_ci};
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_cistatic struct i2c_driver rc5t583_i2c_driver = {
28862306a36Sopenharmony_ci	.driver = {
28962306a36Sopenharmony_ci		   .name = "rc5t583",
29062306a36Sopenharmony_ci		   },
29162306a36Sopenharmony_ci	.probe = rc5t583_i2c_probe,
29262306a36Sopenharmony_ci	.id_table = rc5t583_i2c_id,
29362306a36Sopenharmony_ci};
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_cistatic int __init rc5t583_i2c_init(void)
29662306a36Sopenharmony_ci{
29762306a36Sopenharmony_ci	return i2c_add_driver(&rc5t583_i2c_driver);
29862306a36Sopenharmony_ci}
29962306a36Sopenharmony_cisubsys_initcall(rc5t583_i2c_init);
300