2019-05-02 22:36:37 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
//
|
|
|
|
// da9210-regulator.c - Regulator device driver for DA9210
|
|
|
|
// Copyright (C) 2013 Dialog Semiconductor Ltd.
|
2013-08-06 07:30:48 -07:00
|
|
|
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/module.h>
|
2015-06-24 05:14:21 -07:00
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/irq.h>
|
2013-08-06 07:30:48 -07:00
|
|
|
#include <linux/regulator/driver.h>
|
|
|
|
#include <linux/regulator/machine.h>
|
2023-10-17 13:35:06 -07:00
|
|
|
#include <linux/of.h>
|
2013-09-12 05:28:35 -07:00
|
|
|
#include <linux/regulator/of_regulator.h>
|
2013-08-06 07:30:48 -07:00
|
|
|
#include <linux/regmap.h>
|
|
|
|
|
|
|
|
#include "da9210-regulator.h"
|
|
|
|
|
|
|
|
struct da9210 {
|
|
|
|
struct regulator_dev *rdev;
|
|
|
|
struct regmap *regmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct regmap_config da9210_regmap_config = {
|
|
|
|
.reg_bits = 8,
|
|
|
|
.val_bits = 8,
|
|
|
|
};
|
|
|
|
|
2015-12-19 08:07:09 -07:00
|
|
|
static const struct regulator_ops da9210_buck_ops = {
|
2013-08-06 07:30:48 -07:00
|
|
|
.enable = regulator_enable_regmap,
|
|
|
|
.disable = regulator_disable_regmap,
|
|
|
|
.is_enabled = regulator_is_enabled_regmap,
|
|
|
|
.set_voltage_sel = regulator_set_voltage_sel_regmap,
|
|
|
|
.get_voltage_sel = regulator_get_voltage_sel_regmap,
|
|
|
|
.list_voltage = regulator_list_voltage_linear,
|
2019-02-28 06:40:15 -07:00
|
|
|
.set_current_limit = regulator_set_current_limit_regmap,
|
|
|
|
.get_current_limit = regulator_get_current_limit_regmap,
|
2013-08-06 07:30:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Default limits measured in millivolts and milliamps */
|
|
|
|
#define DA9210_MIN_MV 300
|
|
|
|
#define DA9210_MAX_MV 1570
|
|
|
|
#define DA9210_STEP_MV 10
|
|
|
|
|
|
|
|
/* Current limits for buck (uA) indices corresponds with register values */
|
2019-02-28 06:40:15 -07:00
|
|
|
static const unsigned int da9210_buck_limits[] = {
|
2013-08-06 07:30:48 -07:00
|
|
|
1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000,
|
|
|
|
3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct regulator_desc da9210_reg = {
|
|
|
|
.name = "DA9210",
|
|
|
|
.id = 0,
|
|
|
|
.ops = &da9210_buck_ops,
|
|
|
|
.type = REGULATOR_VOLTAGE,
|
|
|
|
.n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1,
|
|
|
|
.min_uV = (DA9210_MIN_MV * 1000),
|
|
|
|
.uV_step = (DA9210_STEP_MV * 1000),
|
|
|
|
.vsel_reg = DA9210_REG_VBUCK_A,
|
|
|
|
.vsel_mask = DA9210_VBUCK_MASK,
|
|
|
|
.enable_reg = DA9210_REG_BUCK_CONT,
|
|
|
|
.enable_mask = DA9210_BUCK_EN,
|
|
|
|
.owner = THIS_MODULE,
|
2019-02-28 06:40:15 -07:00
|
|
|
.curr_table = da9210_buck_limits,
|
|
|
|
.n_current_limits = ARRAY_SIZE(da9210_buck_limits),
|
|
|
|
.csel_reg = DA9210_REG_BUCK_ILIM,
|
|
|
|
.csel_mask = DA9210_BUCK_ILIM_MASK,
|
2013-08-06 07:30:48 -07:00
|
|
|
};
|
|
|
|
|
2015-06-24 05:14:21 -07:00
|
|
|
static irqreturn_t da9210_irq_handler(int irq, void *data)
|
|
|
|
{
|
|
|
|
struct da9210 *chip = data;
|
|
|
|
unsigned int val, handled = 0;
|
|
|
|
int error, ret = IRQ_NONE;
|
|
|
|
|
|
|
|
error = regmap_read(chip->regmap, DA9210_REG_EVENT_B, &val);
|
|
|
|
if (error < 0)
|
|
|
|
goto error_i2c;
|
|
|
|
|
|
|
|
if (val & DA9210_E_OVCURR) {
|
|
|
|
regulator_notifier_call_chain(chip->rdev,
|
|
|
|
REGULATOR_EVENT_OVER_CURRENT,
|
|
|
|
NULL);
|
|
|
|
handled |= DA9210_E_OVCURR;
|
|
|
|
}
|
|
|
|
if (val & DA9210_E_NPWRGOOD) {
|
|
|
|
regulator_notifier_call_chain(chip->rdev,
|
|
|
|
REGULATOR_EVENT_UNDER_VOLTAGE,
|
|
|
|
NULL);
|
|
|
|
handled |= DA9210_E_NPWRGOOD;
|
|
|
|
}
|
|
|
|
if (val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT)) {
|
|
|
|
regulator_notifier_call_chain(chip->rdev,
|
|
|
|
REGULATOR_EVENT_OVER_TEMP, NULL);
|
|
|
|
handled |= val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT);
|
|
|
|
}
|
|
|
|
if (val & DA9210_E_VMAX) {
|
|
|
|
regulator_notifier_call_chain(chip->rdev,
|
|
|
|
REGULATOR_EVENT_REGULATION_OUT,
|
|
|
|
NULL);
|
|
|
|
handled |= DA9210_E_VMAX;
|
|
|
|
}
|
2016-01-12 10:28:51 -07:00
|
|
|
|
2015-06-24 05:14:21 -07:00
|
|
|
if (handled) {
|
|
|
|
/* Clear handled events */
|
|
|
|
error = regmap_write(chip->regmap, DA9210_REG_EVENT_B, handled);
|
|
|
|
if (error < 0)
|
|
|
|
goto error_i2c;
|
|
|
|
|
|
|
|
ret = IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
error_i2c:
|
|
|
|
dev_err(regmap_get_device(chip->regmap), "I2C error : %d\n", error);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-06 07:30:48 -07:00
|
|
|
/*
|
|
|
|
* I2C driver interface functions
|
|
|
|
*/
|
2016-07-15 02:29:41 -07:00
|
|
|
|
2020-08-20 20:12:35 -07:00
|
|
|
static const struct of_device_id __maybe_unused da9210_dt_ids[] = {
|
2016-07-15 02:29:41 -07:00
|
|
|
{ .compatible = "dlg,da9210", },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, da9210_dt_ids);
|
|
|
|
|
2020-01-09 08:58:08 -07:00
|
|
|
static int da9210_i2c_probe(struct i2c_client *i2c)
|
2013-08-06 07:30:48 -07:00
|
|
|
{
|
|
|
|
struct da9210 *chip;
|
2013-09-12 05:28:35 -07:00
|
|
|
struct device *dev = &i2c->dev;
|
|
|
|
struct da9210_pdata *pdata = dev_get_platdata(dev);
|
2013-08-06 07:30:48 -07:00
|
|
|
struct regulator_dev *rdev = NULL;
|
|
|
|
struct regulator_config config = { };
|
|
|
|
int error;
|
|
|
|
|
|
|
|
chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL);
|
2014-02-20 01:53:02 -07:00
|
|
|
if (!chip)
|
2013-08-06 07:30:48 -07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config);
|
|
|
|
if (IS_ERR(chip->regmap)) {
|
|
|
|
error = PTR_ERR(chip->regmap);
|
|
|
|
dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
|
|
|
|
error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
config.dev = &i2c->dev;
|
2013-09-12 05:28:35 -07:00
|
|
|
config.init_data = pdata ? &pdata->da9210_constraints :
|
2014-11-10 06:43:53 -07:00
|
|
|
of_get_regulator_init_data(dev, dev->of_node, &da9210_reg);
|
2013-08-06 07:30:48 -07:00
|
|
|
config.driver_data = chip;
|
|
|
|
config.regmap = chip->regmap;
|
2013-09-12 05:28:35 -07:00
|
|
|
config.of_node = dev->of_node;
|
2013-08-06 07:30:48 -07:00
|
|
|
|
2015-02-17 05:11:11 -07:00
|
|
|
/* Mask all interrupt sources to deassert interrupt line */
|
|
|
|
error = regmap_write(chip->regmap, DA9210_REG_MASK_A, ~0);
|
|
|
|
if (!error)
|
|
|
|
error = regmap_write(chip->regmap, DA9210_REG_MASK_B, ~0);
|
|
|
|
if (error) {
|
|
|
|
dev_err(&i2c->dev, "Failed to write to mask reg: %d\n", error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-09-29 17:53:57 -07:00
|
|
|
rdev = devm_regulator_register(&i2c->dev, &da9210_reg, &config);
|
2013-08-06 07:30:48 -07:00
|
|
|
if (IS_ERR(rdev)) {
|
|
|
|
dev_err(&i2c->dev, "Failed to register DA9210 regulator\n");
|
|
|
|
return PTR_ERR(rdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
chip->rdev = rdev;
|
2015-06-24 05:14:21 -07:00
|
|
|
if (i2c->irq) {
|
|
|
|
error = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
|
|
|
|
da9210_irq_handler,
|
|
|
|
IRQF_TRIGGER_LOW |
|
|
|
|
IRQF_ONESHOT | IRQF_SHARED,
|
|
|
|
"da9210", chip);
|
|
|
|
if (error) {
|
|
|
|
dev_err(&i2c->dev, "Failed to request IRQ%u: %d\n",
|
|
|
|
i2c->irq, error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = regmap_update_bits(chip->regmap, DA9210_REG_MASK_B,
|
|
|
|
DA9210_M_OVCURR | DA9210_M_NPWRGOOD |
|
|
|
|
DA9210_M_TEMP_WARN |
|
|
|
|
DA9210_M_TEMP_CRIT | DA9210_M_VMAX, 0);
|
|
|
|
if (error < 0) {
|
|
|
|
dev_err(&i2c->dev, "Failed to update mask reg: %d\n",
|
|
|
|
error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dev_warn(&i2c->dev, "No IRQ configured\n");
|
|
|
|
}
|
2013-08-06 07:30:48 -07:00
|
|
|
|
|
|
|
i2c_set_clientdata(i2c, chip);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_device_id da9210_i2c_id[] = {
|
|
|
|
{"da9210", 0},
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(i2c, da9210_i2c_id);
|
|
|
|
|
|
|
|
static struct i2c_driver da9210_regulator_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "da9210",
|
regulator: Set PROBE_PREFER_ASYNCHRONOUS for drivers that existed in 4.14
Probing of regulators can be a slow operation and can contribute to
slower boot times. This is especially true if a regulator is turned on
at probe time (with regulator-boot-on or regulator-always-on) and the
regulator requires delays (off-on-time, ramp time, etc).
While the overall kernel is not ready to switch to async probe by
default, as per the discussion on the mailing lists [1] it is believed
that the regulator subsystem is in good shape and we can move
regulator drivers over wholesale. There is no way to just magically
opt in all regulators (regulators are just normal drivers like
platform_driver), so we set PROBE_PREFER_ASYNCHRONOUS for all
regulators found in 'drivers/regulator' individually.
Given the number of drivers touched and the impossibility to test this
ahead of time, it wouldn't be shocking at all if this caused a
regression for someone. If there is a regression caused by this patch,
it's likely to be one of the cases talked about in [1]. As a "quick
fix", drivers involved in the regression could be fixed by changing
them to PROBE_FORCE_SYNCHRONOUS. That being said, the correct fix
would be to directly fix the problem that caused the issue with async
probe.
The approach here follows a similar approach that was used for the mmc
subsystem several years ago [2]. In fact, I ran nearly the same python
script to auto-generate the changes. The only thing I changed was to
search for "i2c_driver", "spmi_driver", and "spi_driver" in addition
to "platform_driver".
[1] https://lore.kernel.org/r/06db017f-e985-4434-8d1d-02ca2100cca0@sirena.org.uk
[2] https://lore.kernel.org/r/20200903232441.2694866-1-dianders@chromium.org/
Signed-off-by: Douglas Anderson <dianders@chromium.org>
Link: https://lore.kernel.org/r/20230316125351.1.I2a4677392a38db5758dee0788b2cea5872562a82@changeid
Signed-off-by: Mark Brown <broonie@kernel.org>
2023-03-16 12:54:38 -07:00
|
|
|
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
2016-07-15 02:29:41 -07:00
|
|
|
.of_match_table = of_match_ptr(da9210_dt_ids),
|
2013-08-06 07:30:48 -07:00
|
|
|
},
|
2023-05-05 15:02:18 -07:00
|
|
|
.probe = da9210_i2c_probe,
|
2013-08-06 07:30:48 -07:00
|
|
|
.id_table = da9210_i2c_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
module_i2c_driver(da9210_regulator_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
|
|
|
|
MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210");
|
|
|
|
MODULE_LICENSE("GPL v2");
|