18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Copyright (c) 2015 MediaTek Inc.
48c2ecf20Sopenharmony_ci// Author: Henry Chen <henryc.chen@mediatek.com>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/err.h>
78c2ecf20Sopenharmony_ci#include <linux/gpio.h>
88c2ecf20Sopenharmony_ci#include <linux/i2c.h>
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/regmap.h>
138c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
148c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
158c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
168c2ecf20Sopenharmony_ci#include <linux/regulator/mt6311.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include "mt6311-regulator.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic const struct regmap_config mt6311_regmap_config = {
218c2ecf20Sopenharmony_ci	.reg_bits = 8,
228c2ecf20Sopenharmony_ci	.val_bits = 8,
238c2ecf20Sopenharmony_ci	.max_register = MT6311_FQMTR_CON4,
248c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* Default limits measured in millivolts and milliamps */
288c2ecf20Sopenharmony_ci#define MT6311_MIN_UV		600000
298c2ecf20Sopenharmony_ci#define MT6311_MAX_UV		1393750
308c2ecf20Sopenharmony_ci#define MT6311_STEP_UV		6250
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic const struct regulator_ops mt6311_buck_ops = {
338c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
348c2ecf20Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear,
358c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
368c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
378c2ecf20Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
388c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
398c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
408c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic const struct regulator_ops mt6311_ldo_ops = {
448c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
458c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
468c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define MT6311_BUCK(_id) \
508c2ecf20Sopenharmony_ci{\
518c2ecf20Sopenharmony_ci	.name = #_id,\
528c2ecf20Sopenharmony_ci	.ops = &mt6311_buck_ops,\
538c2ecf20Sopenharmony_ci	.of_match = of_match_ptr(#_id),\
548c2ecf20Sopenharmony_ci	.regulators_node = of_match_ptr("regulators"),\
558c2ecf20Sopenharmony_ci	.type = REGULATOR_VOLTAGE,\
568c2ecf20Sopenharmony_ci	.id = MT6311_ID_##_id,\
578c2ecf20Sopenharmony_ci	.n_voltages = (MT6311_MAX_UV - MT6311_MIN_UV) / MT6311_STEP_UV + 1,\
588c2ecf20Sopenharmony_ci	.min_uV = MT6311_MIN_UV,\
598c2ecf20Sopenharmony_ci	.uV_step = MT6311_STEP_UV,\
608c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,\
618c2ecf20Sopenharmony_ci	.enable_reg = MT6311_VDVFS11_CON9,\
628c2ecf20Sopenharmony_ci	.enable_mask = MT6311_PMIC_VDVFS11_EN_MASK,\
638c2ecf20Sopenharmony_ci	.vsel_reg = MT6311_VDVFS11_CON12,\
648c2ecf20Sopenharmony_ci	.vsel_mask = MT6311_PMIC_VDVFS11_VOSEL_MASK,\
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define MT6311_LDO(_id) \
688c2ecf20Sopenharmony_ci{\
698c2ecf20Sopenharmony_ci	.name = #_id,\
708c2ecf20Sopenharmony_ci	.ops = &mt6311_ldo_ops,\
718c2ecf20Sopenharmony_ci	.of_match = of_match_ptr(#_id),\
728c2ecf20Sopenharmony_ci	.regulators_node = of_match_ptr("regulators"),\
738c2ecf20Sopenharmony_ci	.type = REGULATOR_VOLTAGE,\
748c2ecf20Sopenharmony_ci	.id = MT6311_ID_##_id,\
758c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,\
768c2ecf20Sopenharmony_ci	.enable_reg = MT6311_LDO_CON3,\
778c2ecf20Sopenharmony_ci	.enable_mask = MT6311_PMIC_RG_VBIASN_EN_MASK,\
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic const struct regulator_desc mt6311_regulators[] = {
818c2ecf20Sopenharmony_ci	MT6311_BUCK(VDVFS),
828c2ecf20Sopenharmony_ci	MT6311_LDO(VBIASN),
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * I2C driver interface functions
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_cistatic int mt6311_i2c_probe(struct i2c_client *i2c)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct regulator_config config = { };
918c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
928c2ecf20Sopenharmony_ci	struct regmap *regmap;
938c2ecf20Sopenharmony_ci	int i, ret;
948c2ecf20Sopenharmony_ci	unsigned int data;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_i2c(i2c, &mt6311_regmap_config);
978c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
988c2ecf20Sopenharmony_ci		ret = PTR_ERR(regmap);
998c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1008c2ecf20Sopenharmony_ci			ret);
1018c2ecf20Sopenharmony_ci		return ret;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	ret = regmap_read(regmap, MT6311_SWCID, &data);
1058c2ecf20Sopenharmony_ci	if (ret < 0) {
1068c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
1078c2ecf20Sopenharmony_ci		return ret;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	switch (data) {
1118c2ecf20Sopenharmony_ci	case MT6311_E1_CID_CODE:
1128c2ecf20Sopenharmony_ci	case MT6311_E2_CID_CODE:
1138c2ecf20Sopenharmony_ci	case MT6311_E3_CID_CODE:
1148c2ecf20Sopenharmony_ci		break;
1158c2ecf20Sopenharmony_ci	default:
1168c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Unsupported device id = 0x%x.\n", data);
1178c2ecf20Sopenharmony_ci		return -ENODEV;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	for (i = 0; i < MT6311_MAX_REGULATORS; i++) {
1218c2ecf20Sopenharmony_ci		config.dev = &i2c->dev;
1228c2ecf20Sopenharmony_ci		config.regmap = regmap;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci		rdev = devm_regulator_register(&i2c->dev,
1258c2ecf20Sopenharmony_ci			&mt6311_regulators[i], &config);
1268c2ecf20Sopenharmony_ci		if (IS_ERR(rdev)) {
1278c2ecf20Sopenharmony_ci			dev_err(&i2c->dev,
1288c2ecf20Sopenharmony_ci				"Failed to register MT6311 regulator\n");
1298c2ecf20Sopenharmony_ci			return PTR_ERR(rdev);
1308c2ecf20Sopenharmony_ci		}
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	return 0;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic const struct i2c_device_id mt6311_i2c_id[] = {
1378c2ecf20Sopenharmony_ci	{"mt6311", 0},
1388c2ecf20Sopenharmony_ci	{},
1398c2ecf20Sopenharmony_ci};
1408c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, mt6311_i2c_id);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
1438c2ecf20Sopenharmony_cistatic const struct of_device_id mt6311_dt_ids[] = {
1448c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt6311-regulator",
1458c2ecf20Sopenharmony_ci	  .data = &mt6311_i2c_id[0] },
1468c2ecf20Sopenharmony_ci	{},
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mt6311_dt_ids);
1498c2ecf20Sopenharmony_ci#endif
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic struct i2c_driver mt6311_regulator_driver = {
1528c2ecf20Sopenharmony_ci	.driver = {
1538c2ecf20Sopenharmony_ci		.name = "mt6311",
1548c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(mt6311_dt_ids),
1558c2ecf20Sopenharmony_ci	},
1568c2ecf20Sopenharmony_ci	.probe_new = mt6311_i2c_probe,
1578c2ecf20Sopenharmony_ci	.id_table = mt6311_i2c_id,
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cimodule_i2c_driver(mt6311_regulator_driver);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ciMODULE_AUTHOR("Henry Chen <henryc.chen@mediatek.com>");
1638c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Regulator device driver for Mediatek MT6311");
1648c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
165