18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// da9210-regulator.c - Regulator device driver for DA9210
48c2ecf20Sopenharmony_ci// Copyright (C) 2013  Dialog Semiconductor Ltd.
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/err.h>
78c2ecf20Sopenharmony_ci#include <linux/i2c.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
108c2ecf20Sopenharmony_ci#include <linux/irq.h>
118c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
128c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
138c2ecf20Sopenharmony_ci#include <linux/of_device.h>
148c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
158c2ecf20Sopenharmony_ci#include <linux/regmap.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "da9210-regulator.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct da9210 {
208c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
218c2ecf20Sopenharmony_ci	struct regmap *regmap;
228c2ecf20Sopenharmony_ci};
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic const struct regmap_config da9210_regmap_config = {
258c2ecf20Sopenharmony_ci	.reg_bits = 8,
268c2ecf20Sopenharmony_ci	.val_bits = 8,
278c2ecf20Sopenharmony_ci};
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic const struct regulator_ops da9210_buck_ops = {
308c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
318c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
328c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
338c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
348c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
358c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
368c2ecf20Sopenharmony_ci	.set_current_limit = regulator_set_current_limit_regmap,
378c2ecf20Sopenharmony_ci	.get_current_limit = regulator_get_current_limit_regmap,
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* Default limits measured in millivolts and milliamps */
418c2ecf20Sopenharmony_ci#define DA9210_MIN_MV		300
428c2ecf20Sopenharmony_ci#define DA9210_MAX_MV		1570
438c2ecf20Sopenharmony_ci#define DA9210_STEP_MV		10
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* Current limits for buck (uA) indices corresponds with register values */
468c2ecf20Sopenharmony_cistatic const unsigned int da9210_buck_limits[] = {
478c2ecf20Sopenharmony_ci	1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000,
488c2ecf20Sopenharmony_ci	3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic const struct regulator_desc da9210_reg = {
528c2ecf20Sopenharmony_ci	.name = "DA9210",
538c2ecf20Sopenharmony_ci	.id = 0,
548c2ecf20Sopenharmony_ci	.ops = &da9210_buck_ops,
558c2ecf20Sopenharmony_ci	.type = REGULATOR_VOLTAGE,
568c2ecf20Sopenharmony_ci	.n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1,
578c2ecf20Sopenharmony_ci	.min_uV = (DA9210_MIN_MV * 1000),
588c2ecf20Sopenharmony_ci	.uV_step = (DA9210_STEP_MV * 1000),
598c2ecf20Sopenharmony_ci	.vsel_reg = DA9210_REG_VBUCK_A,
608c2ecf20Sopenharmony_ci	.vsel_mask = DA9210_VBUCK_MASK,
618c2ecf20Sopenharmony_ci	.enable_reg = DA9210_REG_BUCK_CONT,
628c2ecf20Sopenharmony_ci	.enable_mask = DA9210_BUCK_EN,
638c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
648c2ecf20Sopenharmony_ci	.curr_table = da9210_buck_limits,
658c2ecf20Sopenharmony_ci	.n_current_limits = ARRAY_SIZE(da9210_buck_limits),
668c2ecf20Sopenharmony_ci	.csel_reg = DA9210_REG_BUCK_ILIM,
678c2ecf20Sopenharmony_ci	.csel_mask = DA9210_BUCK_ILIM_MASK,
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic irqreturn_t da9210_irq_handler(int irq, void *data)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct da9210 *chip = data;
738c2ecf20Sopenharmony_ci	unsigned int val, handled = 0;
748c2ecf20Sopenharmony_ci	int error, ret = IRQ_NONE;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	error = regmap_read(chip->regmap, DA9210_REG_EVENT_B, &val);
778c2ecf20Sopenharmony_ci	if (error < 0)
788c2ecf20Sopenharmony_ci		goto error_i2c;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (val & DA9210_E_OVCURR) {
818c2ecf20Sopenharmony_ci		regulator_notifier_call_chain(chip->rdev,
828c2ecf20Sopenharmony_ci					      REGULATOR_EVENT_OVER_CURRENT,
838c2ecf20Sopenharmony_ci					      NULL);
848c2ecf20Sopenharmony_ci		handled |= DA9210_E_OVCURR;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci	if (val & DA9210_E_NPWRGOOD) {
878c2ecf20Sopenharmony_ci		regulator_notifier_call_chain(chip->rdev,
888c2ecf20Sopenharmony_ci					      REGULATOR_EVENT_UNDER_VOLTAGE,
898c2ecf20Sopenharmony_ci					      NULL);
908c2ecf20Sopenharmony_ci		handled |= DA9210_E_NPWRGOOD;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci	if (val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT)) {
938c2ecf20Sopenharmony_ci		regulator_notifier_call_chain(chip->rdev,
948c2ecf20Sopenharmony_ci					      REGULATOR_EVENT_OVER_TEMP, NULL);
958c2ecf20Sopenharmony_ci		handled |= val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT);
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci	if (val & DA9210_E_VMAX) {
988c2ecf20Sopenharmony_ci		regulator_notifier_call_chain(chip->rdev,
998c2ecf20Sopenharmony_ci					      REGULATOR_EVENT_REGULATION_OUT,
1008c2ecf20Sopenharmony_ci					      NULL);
1018c2ecf20Sopenharmony_ci		handled |= DA9210_E_VMAX;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (handled) {
1058c2ecf20Sopenharmony_ci		/* Clear handled events */
1068c2ecf20Sopenharmony_ci		error = regmap_write(chip->regmap, DA9210_REG_EVENT_B, handled);
1078c2ecf20Sopenharmony_ci		if (error < 0)
1088c2ecf20Sopenharmony_ci			goto error_i2c;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci		ret = IRQ_HANDLED;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return ret;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cierror_i2c:
1168c2ecf20Sopenharmony_ci	dev_err(regmap_get_device(chip->regmap), "I2C error : %d\n", error);
1178c2ecf20Sopenharmony_ci	return ret;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/*
1218c2ecf20Sopenharmony_ci * I2C driver interface functions
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic const struct of_device_id __maybe_unused da9210_dt_ids[] = {
1258c2ecf20Sopenharmony_ci	{ .compatible = "dlg,da9210", },
1268c2ecf20Sopenharmony_ci	{ }
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, da9210_dt_ids);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic int da9210_i2c_probe(struct i2c_client *i2c)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	struct da9210 *chip;
1338c2ecf20Sopenharmony_ci	struct device *dev = &i2c->dev;
1348c2ecf20Sopenharmony_ci	struct da9210_pdata *pdata = dev_get_platdata(dev);
1358c2ecf20Sopenharmony_ci	struct regulator_dev *rdev = NULL;
1368c2ecf20Sopenharmony_ci	struct regulator_config config = { };
1378c2ecf20Sopenharmony_ci	int error;
1388c2ecf20Sopenharmony_ci	const struct of_device_id *match;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (i2c->dev.of_node && !pdata) {
1418c2ecf20Sopenharmony_ci		match = of_match_device(of_match_ptr(da9210_dt_ids),
1428c2ecf20Sopenharmony_ci						&i2c->dev);
1438c2ecf20Sopenharmony_ci		if (!match) {
1448c2ecf20Sopenharmony_ci			dev_err(&i2c->dev, "Error: No device match found\n");
1458c2ecf20Sopenharmony_ci			return -ENODEV;
1468c2ecf20Sopenharmony_ci		}
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL);
1508c2ecf20Sopenharmony_ci	if (!chip)
1518c2ecf20Sopenharmony_ci		return -ENOMEM;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config);
1548c2ecf20Sopenharmony_ci	if (IS_ERR(chip->regmap)) {
1558c2ecf20Sopenharmony_ci		error = PTR_ERR(chip->regmap);
1568c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1578c2ecf20Sopenharmony_ci			error);
1588c2ecf20Sopenharmony_ci		return error;
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	config.dev = &i2c->dev;
1628c2ecf20Sopenharmony_ci	config.init_data = pdata ? &pdata->da9210_constraints :
1638c2ecf20Sopenharmony_ci		of_get_regulator_init_data(dev, dev->of_node, &da9210_reg);
1648c2ecf20Sopenharmony_ci	config.driver_data = chip;
1658c2ecf20Sopenharmony_ci	config.regmap = chip->regmap;
1668c2ecf20Sopenharmony_ci	config.of_node = dev->of_node;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/* Mask all interrupt sources to deassert interrupt line */
1698c2ecf20Sopenharmony_ci	error = regmap_write(chip->regmap, DA9210_REG_MASK_A, ~0);
1708c2ecf20Sopenharmony_ci	if (!error)
1718c2ecf20Sopenharmony_ci		error = regmap_write(chip->regmap, DA9210_REG_MASK_B, ~0);
1728c2ecf20Sopenharmony_ci	if (error) {
1738c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Failed to write to mask reg: %d\n", error);
1748c2ecf20Sopenharmony_ci		return error;
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	rdev = devm_regulator_register(&i2c->dev, &da9210_reg, &config);
1788c2ecf20Sopenharmony_ci	if (IS_ERR(rdev)) {
1798c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Failed to register DA9210 regulator\n");
1808c2ecf20Sopenharmony_ci		return PTR_ERR(rdev);
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	chip->rdev = rdev;
1848c2ecf20Sopenharmony_ci	if (i2c->irq) {
1858c2ecf20Sopenharmony_ci		error = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
1868c2ecf20Sopenharmony_ci						  da9210_irq_handler,
1878c2ecf20Sopenharmony_ci						  IRQF_TRIGGER_LOW |
1888c2ecf20Sopenharmony_ci						  IRQF_ONESHOT | IRQF_SHARED,
1898c2ecf20Sopenharmony_ci						  "da9210", chip);
1908c2ecf20Sopenharmony_ci		if (error) {
1918c2ecf20Sopenharmony_ci			dev_err(&i2c->dev, "Failed to request IRQ%u: %d\n",
1928c2ecf20Sopenharmony_ci				i2c->irq, error);
1938c2ecf20Sopenharmony_ci			return error;
1948c2ecf20Sopenharmony_ci		}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		error = regmap_update_bits(chip->regmap, DA9210_REG_MASK_B,
1978c2ecf20Sopenharmony_ci					 DA9210_M_OVCURR | DA9210_M_NPWRGOOD |
1988c2ecf20Sopenharmony_ci					 DA9210_M_TEMP_WARN |
1998c2ecf20Sopenharmony_ci					 DA9210_M_TEMP_CRIT | DA9210_M_VMAX, 0);
2008c2ecf20Sopenharmony_ci		if (error < 0) {
2018c2ecf20Sopenharmony_ci			dev_err(&i2c->dev, "Failed to update mask reg: %d\n",
2028c2ecf20Sopenharmony_ci				error);
2038c2ecf20Sopenharmony_ci			return error;
2048c2ecf20Sopenharmony_ci		}
2058c2ecf20Sopenharmony_ci	} else {
2068c2ecf20Sopenharmony_ci		dev_warn(&i2c->dev, "No IRQ configured\n");
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	i2c_set_clientdata(i2c, chip);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	return 0;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic const struct i2c_device_id da9210_i2c_id[] = {
2158c2ecf20Sopenharmony_ci	{"da9210", 0},
2168c2ecf20Sopenharmony_ci	{},
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, da9210_i2c_id);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic struct i2c_driver da9210_regulator_driver = {
2228c2ecf20Sopenharmony_ci	.driver = {
2238c2ecf20Sopenharmony_ci		.name = "da9210",
2248c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(da9210_dt_ids),
2258c2ecf20Sopenharmony_ci	},
2268c2ecf20Sopenharmony_ci	.probe_new = da9210_i2c_probe,
2278c2ecf20Sopenharmony_ci	.id_table = da9210_i2c_id,
2288c2ecf20Sopenharmony_ci};
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cimodule_i2c_driver(da9210_regulator_driver);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ciMODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
2338c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Regulator device driver for Dialog DA9210");
2348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
235