161d1eb02SSaravanan Sekar // SPDX-License-Identifier: GPL-2.0+
261d1eb02SSaravanan Sekar /*
361d1eb02SSaravanan Sekar * mpq7932.c - hwmon with optional regulator driver for mps mpq7932
461d1eb02SSaravanan Sekar * Copyright 2022 Monolithic Power Systems, Inc
561d1eb02SSaravanan Sekar *
661d1eb02SSaravanan Sekar * Author: Saravanan Sekar <saravanan@linumiz.com>
761d1eb02SSaravanan Sekar */
861d1eb02SSaravanan Sekar
961d1eb02SSaravanan Sekar #include <linux/bits.h>
1061d1eb02SSaravanan Sekar #include <linux/err.h>
1161d1eb02SSaravanan Sekar #include <linux/i2c.h>
1261d1eb02SSaravanan Sekar #include <linux/init.h>
1361d1eb02SSaravanan Sekar #include <linux/kernel.h>
1461d1eb02SSaravanan Sekar #include <linux/module.h>
1539f03438SRob Herring #include <linux/of.h>
1661d1eb02SSaravanan Sekar #include <linux/pmbus.h>
1761d1eb02SSaravanan Sekar #include "pmbus.h"
1861d1eb02SSaravanan Sekar
1961d1eb02SSaravanan Sekar #define MPQ7932_BUCK_UV_MIN 206250
2061d1eb02SSaravanan Sekar #define MPQ7932_UV_STEP 6250
2161d1eb02SSaravanan Sekar #define MPQ7932_N_VOLTAGES 256
2261d1eb02SSaravanan Sekar #define MPQ7932_VOUT_MAX 0xFF
2361d1eb02SSaravanan Sekar #define MPQ7932_NUM_PAGES 6
24fe0eba17SSaravanan Sekar #define MPQ2286_NUM_PAGES 1
2561d1eb02SSaravanan Sekar
2661d1eb02SSaravanan Sekar #define MPQ7932_TON_DELAY 0x60
2761d1eb02SSaravanan Sekar #define MPQ7932_VOUT_STARTUP_SLEW 0xA3
2861d1eb02SSaravanan Sekar #define MPQ7932_VOUT_SHUTDOWN_SLEW 0xA5
2961d1eb02SSaravanan Sekar #define MPQ7932_VOUT_SLEW_MASK GENMASK(1, 0)
3061d1eb02SSaravanan Sekar #define MPQ7932_TON_DELAY_MASK GENMASK(4, 0)
3161d1eb02SSaravanan Sekar
3261d1eb02SSaravanan Sekar struct mpq7932_data {
3361d1eb02SSaravanan Sekar struct pmbus_driver_info info;
3461d1eb02SSaravanan Sekar struct pmbus_platform_data pdata;
3561d1eb02SSaravanan Sekar };
3661d1eb02SSaravanan Sekar
3761d1eb02SSaravanan Sekar #if IS_ENABLED(CONFIG_SENSORS_MPQ7932_REGULATOR)
38*d22bd451SChristophe JAILLET static const struct regulator_desc mpq7932_regulators_desc[] = {
3961d1eb02SSaravanan Sekar PMBUS_REGULATOR_STEP("buck", 0, MPQ7932_N_VOLTAGES,
4061d1eb02SSaravanan Sekar MPQ7932_UV_STEP, MPQ7932_BUCK_UV_MIN),
4161d1eb02SSaravanan Sekar PMBUS_REGULATOR_STEP("buck", 1, MPQ7932_N_VOLTAGES,
4261d1eb02SSaravanan Sekar MPQ7932_UV_STEP, MPQ7932_BUCK_UV_MIN),
4361d1eb02SSaravanan Sekar PMBUS_REGULATOR_STEP("buck", 2, MPQ7932_N_VOLTAGES,
4461d1eb02SSaravanan Sekar MPQ7932_UV_STEP, MPQ7932_BUCK_UV_MIN),
4561d1eb02SSaravanan Sekar PMBUS_REGULATOR_STEP("buck", 3, MPQ7932_N_VOLTAGES,
4661d1eb02SSaravanan Sekar MPQ7932_UV_STEP, MPQ7932_BUCK_UV_MIN),
4761d1eb02SSaravanan Sekar PMBUS_REGULATOR_STEP("buck", 4, MPQ7932_N_VOLTAGES,
4861d1eb02SSaravanan Sekar MPQ7932_UV_STEP, MPQ7932_BUCK_UV_MIN),
4961d1eb02SSaravanan Sekar PMBUS_REGULATOR_STEP("buck", 5, MPQ7932_N_VOLTAGES,
5061d1eb02SSaravanan Sekar MPQ7932_UV_STEP, MPQ7932_BUCK_UV_MIN),
5161d1eb02SSaravanan Sekar };
52fe0eba17SSaravanan Sekar
53fe0eba17SSaravanan Sekar static const struct regulator_desc mpq7932_regulators_desc_one[] = {
54fe0eba17SSaravanan Sekar PMBUS_REGULATOR_STEP_ONE("buck", MPQ7932_N_VOLTAGES,
55fe0eba17SSaravanan Sekar MPQ7932_UV_STEP, MPQ7932_BUCK_UV_MIN),
56fe0eba17SSaravanan Sekar };
5761d1eb02SSaravanan Sekar #endif
5861d1eb02SSaravanan Sekar
mpq7932_write_word_data(struct i2c_client * client,int page,int reg,u16 word)5961d1eb02SSaravanan Sekar static int mpq7932_write_word_data(struct i2c_client *client, int page, int reg,
6061d1eb02SSaravanan Sekar u16 word)
6161d1eb02SSaravanan Sekar {
6261d1eb02SSaravanan Sekar switch (reg) {
6361d1eb02SSaravanan Sekar /*
6461d1eb02SSaravanan Sekar * chip supports only byte access for VOUT_COMMAND otherwise
6561d1eb02SSaravanan Sekar * access results -EREMOTEIO
6661d1eb02SSaravanan Sekar */
6761d1eb02SSaravanan Sekar case PMBUS_VOUT_COMMAND:
6861d1eb02SSaravanan Sekar return pmbus_write_byte_data(client, page, reg, word & 0xFF);
6961d1eb02SSaravanan Sekar
7061d1eb02SSaravanan Sekar default:
7161d1eb02SSaravanan Sekar return -ENODATA;
7261d1eb02SSaravanan Sekar }
7361d1eb02SSaravanan Sekar }
7461d1eb02SSaravanan Sekar
mpq7932_read_word_data(struct i2c_client * client,int page,int phase,int reg)7561d1eb02SSaravanan Sekar static int mpq7932_read_word_data(struct i2c_client *client, int page,
7661d1eb02SSaravanan Sekar int phase, int reg)
7761d1eb02SSaravanan Sekar {
7861d1eb02SSaravanan Sekar switch (reg) {
7961d1eb02SSaravanan Sekar /*
8061d1eb02SSaravanan Sekar * chip supports neither (PMBUS_VOUT_MARGIN_HIGH, PMBUS_VOUT_MARGIN_LOW)
8161d1eb02SSaravanan Sekar * nor (PMBUS_MFR_VOUT_MIN, PMBUS_MFR_VOUT_MAX). As a result set voltage
8261d1eb02SSaravanan Sekar * fails due to error in pmbus_regulator_get_low_margin, so faked.
8361d1eb02SSaravanan Sekar */
8461d1eb02SSaravanan Sekar case PMBUS_MFR_VOUT_MIN:
8561d1eb02SSaravanan Sekar return 0;
8661d1eb02SSaravanan Sekar
8761d1eb02SSaravanan Sekar case PMBUS_MFR_VOUT_MAX:
8861d1eb02SSaravanan Sekar return MPQ7932_VOUT_MAX;
8961d1eb02SSaravanan Sekar
9061d1eb02SSaravanan Sekar /*
9161d1eb02SSaravanan Sekar * chip supports only byte access for VOUT_COMMAND otherwise
9261d1eb02SSaravanan Sekar * access results in -EREMOTEIO
9361d1eb02SSaravanan Sekar */
9461d1eb02SSaravanan Sekar case PMBUS_READ_VOUT:
9561d1eb02SSaravanan Sekar return pmbus_read_byte_data(client, page, PMBUS_VOUT_COMMAND);
9661d1eb02SSaravanan Sekar
9761d1eb02SSaravanan Sekar default:
9861d1eb02SSaravanan Sekar return -ENODATA;
9961d1eb02SSaravanan Sekar }
10061d1eb02SSaravanan Sekar }
10161d1eb02SSaravanan Sekar
mpq7932_probe(struct i2c_client * client)10261d1eb02SSaravanan Sekar static int mpq7932_probe(struct i2c_client *client)
10361d1eb02SSaravanan Sekar {
10461d1eb02SSaravanan Sekar struct mpq7932_data *data;
10561d1eb02SSaravanan Sekar struct pmbus_driver_info *info;
10661d1eb02SSaravanan Sekar struct device *dev = &client->dev;
10761d1eb02SSaravanan Sekar int i;
10861d1eb02SSaravanan Sekar
10961d1eb02SSaravanan Sekar data = devm_kzalloc(dev, sizeof(struct mpq7932_data), GFP_KERNEL);
11061d1eb02SSaravanan Sekar if (!data)
11161d1eb02SSaravanan Sekar return -ENOMEM;
11261d1eb02SSaravanan Sekar
11361d1eb02SSaravanan Sekar info = &data->info;
114b1a55c0aSSaravanan Sekar info->pages = (int)(unsigned long)device_get_match_data(&client->dev);
11561d1eb02SSaravanan Sekar info->format[PSC_VOLTAGE_OUT] = direct;
11661d1eb02SSaravanan Sekar info->m[PSC_VOLTAGE_OUT] = 160;
11761d1eb02SSaravanan Sekar info->b[PSC_VOLTAGE_OUT] = -33;
11861d1eb02SSaravanan Sekar for (i = 0; i < info->pages; i++) {
11961d1eb02SSaravanan Sekar info->func[i] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
12061d1eb02SSaravanan Sekar | PMBUS_HAVE_STATUS_TEMP;
12161d1eb02SSaravanan Sekar }
12261d1eb02SSaravanan Sekar
12361d1eb02SSaravanan Sekar #if IS_ENABLED(CONFIG_SENSORS_MPQ7932_REGULATOR)
124b1a55c0aSSaravanan Sekar info->num_regulators = info->pages;
125fe0eba17SSaravanan Sekar if (info->num_regulators == 1)
126fe0eba17SSaravanan Sekar info->reg_desc = mpq7932_regulators_desc_one;
127fe0eba17SSaravanan Sekar else
12861d1eb02SSaravanan Sekar info->reg_desc = mpq7932_regulators_desc;
12961d1eb02SSaravanan Sekar #endif
13061d1eb02SSaravanan Sekar
13161d1eb02SSaravanan Sekar info->read_word_data = mpq7932_read_word_data;
13261d1eb02SSaravanan Sekar info->write_word_data = mpq7932_write_word_data;
13361d1eb02SSaravanan Sekar
13461d1eb02SSaravanan Sekar data->pdata.flags = PMBUS_NO_CAPABILITY;
13561d1eb02SSaravanan Sekar dev->platform_data = &data->pdata;
13661d1eb02SSaravanan Sekar
13761d1eb02SSaravanan Sekar return pmbus_do_probe(client, info);
13861d1eb02SSaravanan Sekar }
13961d1eb02SSaravanan Sekar
14061d1eb02SSaravanan Sekar static const struct of_device_id mpq7932_of_match[] = {
141fe0eba17SSaravanan Sekar { .compatible = "mps,mpq2286", .data = (void *)MPQ2286_NUM_PAGES },
142b1a55c0aSSaravanan Sekar { .compatible = "mps,mpq7932", .data = (void *)MPQ7932_NUM_PAGES },
14361d1eb02SSaravanan Sekar {},
14461d1eb02SSaravanan Sekar };
14561d1eb02SSaravanan Sekar MODULE_DEVICE_TABLE(of, mpq7932_of_match);
14661d1eb02SSaravanan Sekar
14761d1eb02SSaravanan Sekar static const struct i2c_device_id mpq7932_id[] = {
148fe0eba17SSaravanan Sekar { "mpq2286", },
14961d1eb02SSaravanan Sekar { "mpq7932", },
15061d1eb02SSaravanan Sekar { },
15161d1eb02SSaravanan Sekar };
15261d1eb02SSaravanan Sekar MODULE_DEVICE_TABLE(i2c, mpq7932_id);
15361d1eb02SSaravanan Sekar
15461d1eb02SSaravanan Sekar static struct i2c_driver mpq7932_regulator_driver = {
15561d1eb02SSaravanan Sekar .driver = {
15661d1eb02SSaravanan Sekar .name = "mpq7932",
15761d1eb02SSaravanan Sekar .of_match_table = mpq7932_of_match,
15861d1eb02SSaravanan Sekar },
1591975d167SUwe Kleine-König .probe = mpq7932_probe,
16061d1eb02SSaravanan Sekar .id_table = mpq7932_id,
16161d1eb02SSaravanan Sekar };
16261d1eb02SSaravanan Sekar module_i2c_driver(mpq7932_regulator_driver);
16361d1eb02SSaravanan Sekar
16461d1eb02SSaravanan Sekar MODULE_AUTHOR("Saravanan Sekar <saravanan@linumiz.com>");
16561d1eb02SSaravanan Sekar MODULE_DESCRIPTION("MPQ7932 PMIC regulator driver");
16661d1eb02SSaravanan Sekar MODULE_LICENSE("GPL");
16761d1eb02SSaravanan Sekar MODULE_IMPORT_NS(PMBUS);
168