// SPDX-License-Identifier: GPL-2.0-only
//
// DA9121 Single-channel dual-phase 10A buck converter
//
// Copyright (C) 2020 Axis Communications AB
//
// DA9130 Single-channel dual-phase 10A buck converter (Automotive)
// DA9217 Single-channel dual-phase 6A buck converter
// DA9122 Dual-channel single-phase 5A buck converter
// DA9131 Dual-channel single-phase 5A buck converter (Automotive)
// DA9220 Dual-channel single-phase 3A buck converter
// DA9132 Dual-channel single-phase 3A buck converter (Automotive)
//
// Copyright (C) 2020 Dialog Semiconductor
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/regulator/of_regulator.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/driver.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/regulator/da9121.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include "da9121-regulator.h"
/* Chip data */
struct da9121 {
struct device *dev;
struct delayed_work work;
struct da9121_pdata *pdata;
struct regmap *regmap;
struct regulator_dev *rdev[DA9121_IDX_MAX];
unsigned int persistent[2];
unsigned int passive_delay;
int chip_irq;
int variant_id;
};
/* Define ranges for different variants, enabling translation to/from
* registers. Maximums give scope to allow for transients.
*/
struct da9121_range {
int val_min;
int val_max;
int val_stp;
int reg_min;
int reg_max;
};
static struct da9121_range da9121_10A_2phase_current = {
.val_min = 7000000,
.val_max = 20000000,
.val_stp = 1000000,
.reg_min = 1,
.reg_max = 14,
};
static struct da9121_range da9121_6A_2phase_current = {
.val_min = 7000000,
.val_max = 12000000,
.val_stp = 1000000,
.reg_min = 1,
.reg_max = 6,
};
static struct da9121_range da9121_5A_1phase_current = {
.val_min = 3500000,
.val_max = 10000000,
.val_stp = 500000,
.reg_min = 1,
.reg_max = 14,
};
static struct da9121_range da9121_3A_1phase_current = {
.val_min = 3500000,
.val_max = 6000000,
.val_stp = 500000,
.reg_min = 1,
.reg_max = 6,
};
struct da9121_variant_info {
int num_bucks;
int num_phases;
struct da9121_range *current_range;
};
static const struct da9121_variant_info variant_parameters[] = {
{ 1, 2, &da9121_10A_2phase_current }, //DA9121_TYPE_DA9121_DA9130
{ 2, 1, &da9121_3A_1phase_current }, //DA9121_TYPE_DA9220_DA9132
{ 2, 1, &da9121_5A_1phase_current }, //DA9121_TYPE_DA9122_DA9131
{ 1, 2, &da9121_6A_2phase_current }, //DA9121_TYPE_DA9217
};
struct da9121_field {
unsigned int reg;
unsigned int msk;
};
static const struct da9121_field da9121_current_field[2] = {
{ DA9121_REG_BUCK_BUCK1_2, DA9121_MASK_BUCK_BUCKx_2_CHx_ILIM },
{ DA9xxx_REG_BUCK_BUCK2_2, DA9121_MASK_BUCK_BUCKx_2_CHx_ILIM },
};
static const struct da9121_field da9121_mode_field[2] = {
{ DA9121_REG_BUCK_BUCK1_4, DA9121_MASK_BUCK_BUCKx_4_CHx_A_MODE },
{ DA9xxx_REG_BUCK_BUCK2_4, DA9121_MASK_BUCK_BUCKx_4_CHx_A_MODE },
};
struct status_event_data {
int buck_id; /* 0=core, 1/2-buck */
int reg_index; /* index for status/event/mask register selection */
int status_bit; /* bit masks... */
int event_bit;
int mask_bit;
unsigned long notification; /* Notification for status inception */
char *warn; /* if NULL, notify - otherwise dev_warn this string */
};
#define DA9121_STATUS(id, bank, name, notification, warning) \
{ id, bank, \
DA9121_MASK_SYS_STATUS_##bank##_##name, \
DA9121_MASK_SYS_EVENT_##bank##_E_##name, \
DA9121_MASK_SYS_MASK_##bank##_M_##name, \
notification, warning }
/* For second buck related event bits that are specific to DA9122, DA9220 variants */
#define DA9xxx_STATUS(id, bank, name, notification, warning) \
{ id, bank, \
DA9xxx_MASK_SYS_STATUS_##bank##_##name, \
DA9xxx_MASK_SYS_EVENT_##bank##_E_##name, \
DA9xxx_MASK_SYS_MASK_##bank##_M_##name, \
notification, warning }
/* The status signals that may need servicing, depending on device variant.
* After assertion, they persist; so event is notified, the IRQ disabled,
* and status polled until clear again and IRQ is reenabled.
*
* SG/PG1/PG2 should be set when device first powers up and should never
* re-occur. When this driver starts, it is expected that these will have
* self-cleared for when the IRQs are enabled, so these should never be seen.
* If seen, the implication is that the device has reset.
*
* GPIO0/1/2 are not configured for use by default, so should not be seen.
*/
static const struct status_event_data status_event_handling[] = {
DA9xxx_STATUS(0, 0, SG, 0, "Handled E_SG\n"),
DA9121_STATUS(0, 0, TEMP_CRIT, (REGULATOR_EVENT_OVER_TEMP|REGULATOR_EVENT_DISABLE), NULL),
DA9121_STATUS(0, 0, TEMP_WARN, REGULATOR_EVENT_OVER_TEMP, NULL),
DA9121_STATUS(1, 1, PG1, 0, "Handled E_PG1\n"),
DA9121_STATUS(1, 1, OV1, REGULATOR_EVENT_REGULATION_OUT, NULL),
DA9121_STATUS(1, 1, UV1, REGULATOR_EVENT_UNDER_VOLTAGE, NULL),
DA9121_STATUS(1, 1, OC1, REGULATOR_EVENT_OVER_CURRENT, NULL),
DA9xxx_STATUS(2, 1, PG2, 0, "Handled E_PG2\n"),
DA9xxx_STATUS(2, 1, OV2, REGULATOR_EVENT_REGULATION_OUT, NULL),
DA9xxx_STATUS(2, 1, UV2, REGULATOR_EVENT_UNDER_VOLTAGE, NULL),
DA9xxx_STATUS(2, 1, OC2, REGULATOR_EVENT_OVER_CURRENT, NULL),
DA9121_STATUS(0, 2, GPIO0, 0, "Handled E_GPIO0\n"),
DA9121_STATUS(0, 2, GPIO1, 0, "Handled E_GPIO1\n"),
DA9121_STATUS(0, 2, GPIO2,